14e85e535SNicolas Saenz Julienne // SPDX-License-Identifier: GPL-2.0+
24e85e535SNicolas Saenz Julienne /*
34e85e535SNicolas Saenz Julienne * Raspberry Pi driver for firmware controlled clocks
44e85e535SNicolas Saenz Julienne *
54e85e535SNicolas Saenz Julienne * Even though clk-bcm2835 provides an interface to the hardware registers for
64e85e535SNicolas Saenz Julienne * the system clocks we've had to factor out 'pllb' as the firmware 'owns' it.
74e85e535SNicolas Saenz Julienne * We're not allowed to change it directly as we might race with the
84e85e535SNicolas Saenz Julienne * over-temperature and under-voltage protections provided by the firmware.
94e85e535SNicolas Saenz Julienne *
104e85e535SNicolas Saenz Julienne * Copyright (C) 2019 Nicolas Saenz Julienne <nsaenzjulienne@suse.de>
114e85e535SNicolas Saenz Julienne */
124e85e535SNicolas Saenz Julienne
134e85e535SNicolas Saenz Julienne #include <linux/clkdev.h>
144e85e535SNicolas Saenz Julienne #include <linux/clk-provider.h>
154e85e535SNicolas Saenz Julienne #include <linux/io.h>
164e85e535SNicolas Saenz Julienne #include <linux/module.h>
174e85e535SNicolas Saenz Julienne #include <linux/platform_device.h>
184e85e535SNicolas Saenz Julienne
194e85e535SNicolas Saenz Julienne #include <soc/bcm2835/raspberrypi-firmware.h>
204e85e535SNicolas Saenz Julienne
217dad8a61SMaxime Ripard static char *rpi_firmware_clk_names[] = {
227dad8a61SMaxime Ripard [RPI_FIRMWARE_EMMC_CLK_ID] = "emmc",
237dad8a61SMaxime Ripard [RPI_FIRMWARE_UART_CLK_ID] = "uart",
247dad8a61SMaxime Ripard [RPI_FIRMWARE_ARM_CLK_ID] = "arm",
257dad8a61SMaxime Ripard [RPI_FIRMWARE_CORE_CLK_ID] = "core",
267dad8a61SMaxime Ripard [RPI_FIRMWARE_V3D_CLK_ID] = "v3d",
277dad8a61SMaxime Ripard [RPI_FIRMWARE_H264_CLK_ID] = "h264",
287dad8a61SMaxime Ripard [RPI_FIRMWARE_ISP_CLK_ID] = "isp",
297dad8a61SMaxime Ripard [RPI_FIRMWARE_SDRAM_CLK_ID] = "sdram",
307dad8a61SMaxime Ripard [RPI_FIRMWARE_PIXEL_CLK_ID] = "pixel",
317dad8a61SMaxime Ripard [RPI_FIRMWARE_PWM_CLK_ID] = "pwm",
327dad8a61SMaxime Ripard [RPI_FIRMWARE_HEVC_CLK_ID] = "hevc",
337dad8a61SMaxime Ripard [RPI_FIRMWARE_EMMC2_CLK_ID] = "emmc2",
347dad8a61SMaxime Ripard [RPI_FIRMWARE_M2MC_CLK_ID] = "m2mc",
357dad8a61SMaxime Ripard [RPI_FIRMWARE_PIXEL_BVB_CLK_ID] = "pixel-bvb",
361777cb60SDom Cobley [RPI_FIRMWARE_VEC_CLK_ID] = "vec",
377dad8a61SMaxime Ripard };
387dad8a61SMaxime Ripard
394e85e535SNicolas Saenz Julienne #define RPI_FIRMWARE_STATE_ENABLE_BIT BIT(0)
404e85e535SNicolas Saenz Julienne #define RPI_FIRMWARE_STATE_WAIT_BIT BIT(1)
414e85e535SNicolas Saenz Julienne
4212c90f3fSMaxime Ripard struct raspberrypi_clk_variant;
4312c90f3fSMaxime Ripard
444e85e535SNicolas Saenz Julienne struct raspberrypi_clk {
454e85e535SNicolas Saenz Julienne struct device *dev;
464e85e535SNicolas Saenz Julienne struct rpi_firmware *firmware;
47e2bb1834SNicolas Saenz Julienne struct platform_device *cpufreq;
48f922c560SMaxime Ripard };
494e85e535SNicolas Saenz Julienne
50f922c560SMaxime Ripard struct raspberrypi_clk_data {
51f922c560SMaxime Ripard struct clk_hw hw;
528a1f3ebcSMaxime Ripard
538a1f3ebcSMaxime Ripard unsigned int id;
5412c90f3fSMaxime Ripard struct raspberrypi_clk_variant *variant;
558a1f3ebcSMaxime Ripard
56f922c560SMaxime Ripard struct raspberrypi_clk *rpi;
574e85e535SNicolas Saenz Julienne };
584e85e535SNicolas Saenz Julienne
5912c90f3fSMaxime Ripard struct raspberrypi_clk_variant {
6012c90f3fSMaxime Ripard bool export;
6112c90f3fSMaxime Ripard char *clkdev;
62542acfecSMaxime Ripard unsigned long min_rate;
63e9d6cea2SMaxime Ripard bool minimize;
6412c90f3fSMaxime Ripard };
6512c90f3fSMaxime Ripard
6612c90f3fSMaxime Ripard static struct raspberrypi_clk_variant
6712c90f3fSMaxime Ripard raspberrypi_clk_variants[RPI_FIRMWARE_NUM_CLK_ID] = {
6812c90f3fSMaxime Ripard [RPI_FIRMWARE_ARM_CLK_ID] = {
6912c90f3fSMaxime Ripard .export = true,
7012c90f3fSMaxime Ripard .clkdev = "cpu0",
7112c90f3fSMaxime Ripard },
7212c90f3fSMaxime Ripard [RPI_FIRMWARE_CORE_CLK_ID] = {
7312c90f3fSMaxime Ripard .export = true,
74e9d6cea2SMaxime Ripard
75e9d6cea2SMaxime Ripard /*
76e9d6cea2SMaxime Ripard * The clock is shared between the HVS and the CSI
77e9d6cea2SMaxime Ripard * controllers, on the BCM2711 and will change depending
78e9d6cea2SMaxime Ripard * on the pixels composited on the HVS and the capture
79e9d6cea2SMaxime Ripard * resolution on Unicam.
80e9d6cea2SMaxime Ripard *
81e9d6cea2SMaxime Ripard * Since the rate can get quite large, and we need to
82e9d6cea2SMaxime Ripard * coordinate between both driver instances, let's
83e9d6cea2SMaxime Ripard * always use the minimum the drivers will let us.
84e9d6cea2SMaxime Ripard */
85e9d6cea2SMaxime Ripard .minimize = true,
8612c90f3fSMaxime Ripard },
8712c90f3fSMaxime Ripard [RPI_FIRMWARE_M2MC_CLK_ID] = {
8812c90f3fSMaxime Ripard .export = true,
89542acfecSMaxime Ripard
90542acfecSMaxime Ripard /*
91542acfecSMaxime Ripard * If we boot without any cable connected to any of the
92542acfecSMaxime Ripard * HDMI connector, the firmware will skip the HSM
93542acfecSMaxime Ripard * initialization and leave it with a rate of 0,
94542acfecSMaxime Ripard * resulting in a bus lockup when we're accessing the
95542acfecSMaxime Ripard * registers even if it's enabled.
96542acfecSMaxime Ripard *
97542acfecSMaxime Ripard * Let's put a sensible default so that we don't end up
98542acfecSMaxime Ripard * in this situation.
99542acfecSMaxime Ripard */
100542acfecSMaxime Ripard .min_rate = 120000000,
101e9d6cea2SMaxime Ripard
102e9d6cea2SMaxime Ripard /*
103e9d6cea2SMaxime Ripard * The clock is shared between the two HDMI controllers
104e9d6cea2SMaxime Ripard * on the BCM2711 and will change depending on the
105e9d6cea2SMaxime Ripard * resolution output on each. Since the rate can get
106e9d6cea2SMaxime Ripard * quite large, and we need to coordinate between both
107e9d6cea2SMaxime Ripard * driver instances, let's always use the minimum the
108e9d6cea2SMaxime Ripard * drivers will let us.
109e9d6cea2SMaxime Ripard */
110e9d6cea2SMaxime Ripard .minimize = true,
11112c90f3fSMaxime Ripard },
11212c90f3fSMaxime Ripard [RPI_FIRMWARE_V3D_CLK_ID] = {
11312c90f3fSMaxime Ripard .export = true,
11412c90f3fSMaxime Ripard },
11516baa8c1SIvan T. Ivanov [RPI_FIRMWARE_PIXEL_CLK_ID] = {
11616baa8c1SIvan T. Ivanov .export = true,
11716baa8c1SIvan T. Ivanov },
1184c68a345SIvan T. Ivanov [RPI_FIRMWARE_HEVC_CLK_ID] = {
1194c68a345SIvan T. Ivanov .export = true,
1204c68a345SIvan T. Ivanov },
12112c90f3fSMaxime Ripard [RPI_FIRMWARE_PIXEL_BVB_CLK_ID] = {
12212c90f3fSMaxime Ripard .export = true,
12312c90f3fSMaxime Ripard },
1241777cb60SDom Cobley [RPI_FIRMWARE_VEC_CLK_ID] = {
1251777cb60SDom Cobley .export = true,
1261777cb60SDom Cobley },
12712c90f3fSMaxime Ripard };
12812c90f3fSMaxime Ripard
1294e85e535SNicolas Saenz Julienne /*
1304e85e535SNicolas Saenz Julienne * Structure of the message passed to Raspberry Pi's firmware in order to
1314e85e535SNicolas Saenz Julienne * change clock rates. The 'disable_turbo' option is only available to the ARM
1324e85e535SNicolas Saenz Julienne * clock (pllb) which we enable by default as turbo mode will alter multiple
1334e85e535SNicolas Saenz Julienne * clocks at once.
1344e85e535SNicolas Saenz Julienne *
1354e85e535SNicolas Saenz Julienne * Even though we're able to access the clock registers directly we're bound to
1364e85e535SNicolas Saenz Julienne * use the firmware interface as the firmware ultimately takes care of
1374e85e535SNicolas Saenz Julienne * mitigating overheating/undervoltage situations and we would be changing
1384e85e535SNicolas Saenz Julienne * frequencies behind his back.
1394e85e535SNicolas Saenz Julienne *
1404e85e535SNicolas Saenz Julienne * For more information on the firmware interface check:
1414e85e535SNicolas Saenz Julienne * https://github.com/raspberrypi/firmware/wiki/Mailbox-property-interface
1424e85e535SNicolas Saenz Julienne */
1434e85e535SNicolas Saenz Julienne struct raspberrypi_firmware_prop {
1444e85e535SNicolas Saenz Julienne __le32 id;
1454e85e535SNicolas Saenz Julienne __le32 val;
1464e85e535SNicolas Saenz Julienne __le32 disable_turbo;
1474e85e535SNicolas Saenz Julienne } __packed;
1484e85e535SNicolas Saenz Julienne
raspberrypi_clock_property(struct rpi_firmware * firmware,const struct raspberrypi_clk_data * data,u32 tag,u32 * val)14981df0151SMaxime Ripard static int raspberrypi_clock_property(struct rpi_firmware *firmware,
15081df0151SMaxime Ripard const struct raspberrypi_clk_data *data,
15181df0151SMaxime Ripard u32 tag, u32 *val)
1524e85e535SNicolas Saenz Julienne {
1534e85e535SNicolas Saenz Julienne struct raspberrypi_firmware_prop msg = {
15481df0151SMaxime Ripard .id = cpu_to_le32(data->id),
1554e85e535SNicolas Saenz Julienne .val = cpu_to_le32(*val),
1564e85e535SNicolas Saenz Julienne .disable_turbo = cpu_to_le32(1),
1574e85e535SNicolas Saenz Julienne };
1584e85e535SNicolas Saenz Julienne int ret;
1594e85e535SNicolas Saenz Julienne
1604e85e535SNicolas Saenz Julienne ret = rpi_firmware_property(firmware, tag, &msg, sizeof(msg));
1614e85e535SNicolas Saenz Julienne if (ret)
1624e85e535SNicolas Saenz Julienne return ret;
1634e85e535SNicolas Saenz Julienne
1644e85e535SNicolas Saenz Julienne *val = le32_to_cpu(msg.val);
1654e85e535SNicolas Saenz Julienne
1664e85e535SNicolas Saenz Julienne return 0;
1674e85e535SNicolas Saenz Julienne }
1684e85e535SNicolas Saenz Julienne
raspberrypi_fw_is_prepared(struct clk_hw * hw)169c1ce3509SMaxime Ripard static int raspberrypi_fw_is_prepared(struct clk_hw *hw)
1704e85e535SNicolas Saenz Julienne {
171f922c560SMaxime Ripard struct raspberrypi_clk_data *data =
172f922c560SMaxime Ripard container_of(hw, struct raspberrypi_clk_data, hw);
173f922c560SMaxime Ripard struct raspberrypi_clk *rpi = data->rpi;
1744e85e535SNicolas Saenz Julienne u32 val = 0;
1754e85e535SNicolas Saenz Julienne int ret;
1764e85e535SNicolas Saenz Julienne
17781df0151SMaxime Ripard ret = raspberrypi_clock_property(rpi->firmware, data,
17881df0151SMaxime Ripard RPI_FIRMWARE_GET_CLOCK_STATE, &val);
1794e85e535SNicolas Saenz Julienne if (ret)
1804e85e535SNicolas Saenz Julienne return 0;
1814e85e535SNicolas Saenz Julienne
1824e85e535SNicolas Saenz Julienne return !!(val & RPI_FIRMWARE_STATE_ENABLE_BIT);
1834e85e535SNicolas Saenz Julienne }
1844e85e535SNicolas Saenz Julienne
1854e85e535SNicolas Saenz Julienne
raspberrypi_fw_get_rate(struct clk_hw * hw,unsigned long parent_rate)1863ea59aceSMaxime Ripard static unsigned long raspberrypi_fw_get_rate(struct clk_hw *hw,
1874e85e535SNicolas Saenz Julienne unsigned long parent_rate)
1884e85e535SNicolas Saenz Julienne {
189f922c560SMaxime Ripard struct raspberrypi_clk_data *data =
190f922c560SMaxime Ripard container_of(hw, struct raspberrypi_clk_data, hw);
191f922c560SMaxime Ripard struct raspberrypi_clk *rpi = data->rpi;
1924e85e535SNicolas Saenz Julienne u32 val = 0;
1934e85e535SNicolas Saenz Julienne int ret;
1944e85e535SNicolas Saenz Julienne
19581df0151SMaxime Ripard ret = raspberrypi_clock_property(rpi->firmware, data,
19681df0151SMaxime Ripard RPI_FIRMWARE_GET_CLOCK_RATE, &val);
1974e85e535SNicolas Saenz Julienne if (ret)
19835f73ccaSStefan Wahren return 0;
1994e85e535SNicolas Saenz Julienne
2003ea59aceSMaxime Ripard return val;
2014e85e535SNicolas Saenz Julienne }
2024e85e535SNicolas Saenz Julienne
raspberrypi_fw_set_rate(struct clk_hw * hw,unsigned long rate,unsigned long parent_rate)2033ea59aceSMaxime Ripard static int raspberrypi_fw_set_rate(struct clk_hw *hw, unsigned long rate,
2044e85e535SNicolas Saenz Julienne unsigned long parent_rate)
2054e85e535SNicolas Saenz Julienne {
206f922c560SMaxime Ripard struct raspberrypi_clk_data *data =
207f922c560SMaxime Ripard container_of(hw, struct raspberrypi_clk_data, hw);
208f922c560SMaxime Ripard struct raspberrypi_clk *rpi = data->rpi;
2093ea59aceSMaxime Ripard u32 _rate = rate;
2104e85e535SNicolas Saenz Julienne int ret;
2114e85e535SNicolas Saenz Julienne
21281df0151SMaxime Ripard ret = raspberrypi_clock_property(rpi->firmware, data,
2133ea59aceSMaxime Ripard RPI_FIRMWARE_SET_CLOCK_RATE, &_rate);
2144e85e535SNicolas Saenz Julienne if (ret)
21513b5cf8dSStefan Wahren dev_err_ratelimited(rpi->dev, "Failed to change %s frequency: %d\n",
2164e85e535SNicolas Saenz Julienne clk_hw_get_name(hw), ret);
2174e85e535SNicolas Saenz Julienne
2184e85e535SNicolas Saenz Julienne return ret;
2194e85e535SNicolas Saenz Julienne }
2204e85e535SNicolas Saenz Julienne
raspberrypi_fw_dumb_determine_rate(struct clk_hw * hw,struct clk_rate_request * req)22193d2725aSMaxime Ripard static int raspberrypi_fw_dumb_determine_rate(struct clk_hw *hw,
22293d2725aSMaxime Ripard struct clk_rate_request *req)
22393d2725aSMaxime Ripard {
224e9d6cea2SMaxime Ripard struct raspberrypi_clk_data *data =
225e9d6cea2SMaxime Ripard container_of(hw, struct raspberrypi_clk_data, hw);
226e9d6cea2SMaxime Ripard struct raspberrypi_clk_variant *variant = data->variant;
227e9d6cea2SMaxime Ripard
22893d2725aSMaxime Ripard /*
22993d2725aSMaxime Ripard * The firmware will do the rounding but that isn't part of
23093d2725aSMaxime Ripard * the interface with the firmware, so we just do our best
23193d2725aSMaxime Ripard * here.
23293d2725aSMaxime Ripard */
233e9d6cea2SMaxime Ripard
23493d2725aSMaxime Ripard req->rate = clamp(req->rate, req->min_rate, req->max_rate);
235e9d6cea2SMaxime Ripard
236e9d6cea2SMaxime Ripard /*
237e9d6cea2SMaxime Ripard * We want to aggressively reduce the clock rate here, so let's
238e9d6cea2SMaxime Ripard * just ignore the requested rate and return the bare minimum
239e9d6cea2SMaxime Ripard * rate we can get away with.
240e9d6cea2SMaxime Ripard */
241e9d6cea2SMaxime Ripard if (variant->minimize && req->min_rate > 0)
242e9d6cea2SMaxime Ripard req->rate = req->min_rate;
243e9d6cea2SMaxime Ripard
24493d2725aSMaxime Ripard return 0;
24593d2725aSMaxime Ripard }
24693d2725aSMaxime Ripard
24793d2725aSMaxime Ripard static const struct clk_ops raspberrypi_firmware_clk_ops = {
24893d2725aSMaxime Ripard .is_prepared = raspberrypi_fw_is_prepared,
24993d2725aSMaxime Ripard .recalc_rate = raspberrypi_fw_get_rate,
25093d2725aSMaxime Ripard .determine_rate = raspberrypi_fw_dumb_determine_rate,
25193d2725aSMaxime Ripard .set_rate = raspberrypi_fw_set_rate,
25293d2725aSMaxime Ripard };
25393d2725aSMaxime Ripard
raspberrypi_clk_register(struct raspberrypi_clk * rpi,unsigned int parent,unsigned int id,struct raspberrypi_clk_variant * variant)25493d2725aSMaxime Ripard static struct clk_hw *raspberrypi_clk_register(struct raspberrypi_clk *rpi,
25593d2725aSMaxime Ripard unsigned int parent,
25612c90f3fSMaxime Ripard unsigned int id,
25712c90f3fSMaxime Ripard struct raspberrypi_clk_variant *variant)
25893d2725aSMaxime Ripard {
25993d2725aSMaxime Ripard struct raspberrypi_clk_data *data;
26093d2725aSMaxime Ripard struct clk_init_data init = {};
26193d2725aSMaxime Ripard u32 min_rate, max_rate;
26293d2725aSMaxime Ripard int ret;
26393d2725aSMaxime Ripard
26493d2725aSMaxime Ripard data = devm_kzalloc(rpi->dev, sizeof(*data), GFP_KERNEL);
26593d2725aSMaxime Ripard if (!data)
26693d2725aSMaxime Ripard return ERR_PTR(-ENOMEM);
26793d2725aSMaxime Ripard data->rpi = rpi;
26893d2725aSMaxime Ripard data->id = id;
26912c90f3fSMaxime Ripard data->variant = variant;
27093d2725aSMaxime Ripard
2717dad8a61SMaxime Ripard init.name = devm_kasprintf(rpi->dev, GFP_KERNEL,
2727dad8a61SMaxime Ripard "fw-clk-%s",
2737dad8a61SMaxime Ripard rpi_firmware_clk_names[id]);
27493d2725aSMaxime Ripard init.ops = &raspberrypi_firmware_clk_ops;
27593d2725aSMaxime Ripard init.flags = CLK_GET_RATE_NOCACHE;
27693d2725aSMaxime Ripard
27793d2725aSMaxime Ripard data->hw.init = &init;
27893d2725aSMaxime Ripard
27993d2725aSMaxime Ripard ret = raspberrypi_clock_property(rpi->firmware, data,
28093d2725aSMaxime Ripard RPI_FIRMWARE_GET_MIN_CLOCK_RATE,
28193d2725aSMaxime Ripard &min_rate);
28293d2725aSMaxime Ripard if (ret) {
28313b5cf8dSStefan Wahren dev_err(rpi->dev, "Failed to get clock %d min freq: %d\n",
28493d2725aSMaxime Ripard id, ret);
28593d2725aSMaxime Ripard return ERR_PTR(ret);
28693d2725aSMaxime Ripard }
28793d2725aSMaxime Ripard
28893d2725aSMaxime Ripard ret = raspberrypi_clock_property(rpi->firmware, data,
28993d2725aSMaxime Ripard RPI_FIRMWARE_GET_MAX_CLOCK_RATE,
29093d2725aSMaxime Ripard &max_rate);
29193d2725aSMaxime Ripard if (ret) {
29293d2725aSMaxime Ripard dev_err(rpi->dev, "Failed to get clock %d max freq: %d\n",
29393d2725aSMaxime Ripard id, ret);
29493d2725aSMaxime Ripard return ERR_PTR(ret);
29593d2725aSMaxime Ripard }
29693d2725aSMaxime Ripard
29793d2725aSMaxime Ripard ret = devm_clk_hw_register(rpi->dev, &data->hw);
29893d2725aSMaxime Ripard if (ret)
29993d2725aSMaxime Ripard return ERR_PTR(ret);
30093d2725aSMaxime Ripard
30193d2725aSMaxime Ripard clk_hw_set_rate_range(&data->hw, min_rate, max_rate);
30293d2725aSMaxime Ripard
30312c90f3fSMaxime Ripard if (variant->clkdev) {
30493d2725aSMaxime Ripard ret = devm_clk_hw_register_clkdev(rpi->dev, &data->hw,
30512c90f3fSMaxime Ripard NULL, variant->clkdev);
30693d2725aSMaxime Ripard if (ret) {
30793d2725aSMaxime Ripard dev_err(rpi->dev, "Failed to initialize clkdev\n");
30893d2725aSMaxime Ripard return ERR_PTR(ret);
30993d2725aSMaxime Ripard }
31093d2725aSMaxime Ripard }
31193d2725aSMaxime Ripard
312542acfecSMaxime Ripard if (variant->min_rate) {
313542acfecSMaxime Ripard unsigned long rate;
314542acfecSMaxime Ripard
315542acfecSMaxime Ripard clk_hw_set_rate_range(&data->hw, variant->min_rate, max_rate);
316542acfecSMaxime Ripard
317542acfecSMaxime Ripard rate = raspberrypi_fw_get_rate(&data->hw, 0);
318542acfecSMaxime Ripard if (rate < variant->min_rate) {
319542acfecSMaxime Ripard ret = raspberrypi_fw_set_rate(&data->hw, variant->min_rate, 0);
320542acfecSMaxime Ripard if (ret)
321542acfecSMaxime Ripard return ERR_PTR(ret);
322542acfecSMaxime Ripard }
323542acfecSMaxime Ripard }
324542acfecSMaxime Ripard
32593d2725aSMaxime Ripard return &data->hw;
32693d2725aSMaxime Ripard }
32793d2725aSMaxime Ripard
32893d2725aSMaxime Ripard struct rpi_firmware_get_clocks_response {
32993d2725aSMaxime Ripard u32 parent;
33093d2725aSMaxime Ripard u32 id;
33193d2725aSMaxime Ripard };
33293d2725aSMaxime Ripard
raspberrypi_discover_clocks(struct raspberrypi_clk * rpi,struct clk_hw_onecell_data * data)33393d2725aSMaxime Ripard static int raspberrypi_discover_clocks(struct raspberrypi_clk *rpi,
33493d2725aSMaxime Ripard struct clk_hw_onecell_data *data)
33593d2725aSMaxime Ripard {
33693d2725aSMaxime Ripard struct rpi_firmware_get_clocks_response *clks;
33793d2725aSMaxime Ripard int ret;
33893d2725aSMaxime Ripard
339bc163555SStefan Wahren /*
340bc163555SStefan Wahren * The firmware doesn't guarantee that the last element of
341bc163555SStefan Wahren * RPI_FIRMWARE_GET_CLOCKS is zeroed. So allocate an additional
342bc163555SStefan Wahren * zero element as sentinel.
343bc163555SStefan Wahren */
34493d2725aSMaxime Ripard clks = devm_kcalloc(rpi->dev,
345bc163555SStefan Wahren RPI_FIRMWARE_NUM_CLK_ID + 1, sizeof(*clks),
34693d2725aSMaxime Ripard GFP_KERNEL);
34793d2725aSMaxime Ripard if (!clks)
34893d2725aSMaxime Ripard return -ENOMEM;
34993d2725aSMaxime Ripard
35093d2725aSMaxime Ripard ret = rpi_firmware_property(rpi->firmware, RPI_FIRMWARE_GET_CLOCKS,
35193d2725aSMaxime Ripard clks,
35293d2725aSMaxime Ripard sizeof(*clks) * RPI_FIRMWARE_NUM_CLK_ID);
35393d2725aSMaxime Ripard if (ret)
35493d2725aSMaxime Ripard return ret;
35593d2725aSMaxime Ripard
35693d2725aSMaxime Ripard while (clks->id) {
35712c90f3fSMaxime Ripard struct raspberrypi_clk_variant *variant;
35812c90f3fSMaxime Ripard
359da2edb3eSDan Carpenter if (clks->id >= RPI_FIRMWARE_NUM_CLK_ID) {
3601a6052e1SStefan Wahren dev_err(rpi->dev, "Unknown clock id: %u (max: %u)\n",
361da2edb3eSDan Carpenter clks->id, RPI_FIRMWARE_NUM_CLK_ID - 1);
36212c90f3fSMaxime Ripard return -EINVAL;
36312c90f3fSMaxime Ripard }
36412c90f3fSMaxime Ripard
36512c90f3fSMaxime Ripard variant = &raspberrypi_clk_variants[clks->id];
36612c90f3fSMaxime Ripard if (variant->export) {
36793d2725aSMaxime Ripard struct clk_hw *hw;
36893d2725aSMaxime Ripard
36993d2725aSMaxime Ripard hw = raspberrypi_clk_register(rpi, clks->parent,
37012c90f3fSMaxime Ripard clks->id, variant);
37193d2725aSMaxime Ripard if (IS_ERR(hw))
37293d2725aSMaxime Ripard return PTR_ERR(hw);
37393d2725aSMaxime Ripard
37493d2725aSMaxime Ripard data->num = clks->id + 1;
3756dc445c1SNathan Chancellor data->hws[clks->id] = hw;
37693d2725aSMaxime Ripard }
37712c90f3fSMaxime Ripard
37812c90f3fSMaxime Ripard clks++;
37993d2725aSMaxime Ripard }
38093d2725aSMaxime Ripard
38193d2725aSMaxime Ripard return 0;
38293d2725aSMaxime Ripard }
38393d2725aSMaxime Ripard
raspberrypi_clk_probe(struct platform_device * pdev)3844e85e535SNicolas Saenz Julienne static int raspberrypi_clk_probe(struct platform_device *pdev)
3854e85e535SNicolas Saenz Julienne {
386d4b4f1b6SMaxime Ripard struct clk_hw_onecell_data *clk_data;
3874e85e535SNicolas Saenz Julienne struct device_node *firmware_node;
3884e85e535SNicolas Saenz Julienne struct device *dev = &pdev->dev;
3894e85e535SNicolas Saenz Julienne struct rpi_firmware *firmware;
3904e85e535SNicolas Saenz Julienne struct raspberrypi_clk *rpi;
391d4b4f1b6SMaxime Ripard int ret;
3924e85e535SNicolas Saenz Julienne
393fbac2e77SMaxime Ripard /*
394fbac2e77SMaxime Ripard * We can be probed either through the an old-fashioned
395fbac2e77SMaxime Ripard * platform device registration or through a DT node that is a
396fbac2e77SMaxime Ripard * child of the firmware node. Handle both cases.
397fbac2e77SMaxime Ripard */
398fbac2e77SMaxime Ripard if (dev->of_node)
399fbac2e77SMaxime Ripard firmware_node = of_get_parent(dev->of_node);
400fbac2e77SMaxime Ripard else
4014e85e535SNicolas Saenz Julienne firmware_node = of_find_compatible_node(NULL, NULL,
4024e85e535SNicolas Saenz Julienne "raspberrypi,bcm2835-firmware");
4034e85e535SNicolas Saenz Julienne if (!firmware_node) {
4044e85e535SNicolas Saenz Julienne dev_err(dev, "Missing firmware node\n");
4054e85e535SNicolas Saenz Julienne return -ENOENT;
4064e85e535SNicolas Saenz Julienne }
4074e85e535SNicolas Saenz Julienne
4083c4084f9SNicolas Saenz Julienne firmware = devm_rpi_firmware_get(&pdev->dev, firmware_node);
4094e85e535SNicolas Saenz Julienne of_node_put(firmware_node);
4104e85e535SNicolas Saenz Julienne if (!firmware)
4114e85e535SNicolas Saenz Julienne return -EPROBE_DEFER;
4124e85e535SNicolas Saenz Julienne
4134e85e535SNicolas Saenz Julienne rpi = devm_kzalloc(dev, sizeof(*rpi), GFP_KERNEL);
4144e85e535SNicolas Saenz Julienne if (!rpi)
4154e85e535SNicolas Saenz Julienne return -ENOMEM;
4164e85e535SNicolas Saenz Julienne
4174e85e535SNicolas Saenz Julienne rpi->dev = dev;
4184e85e535SNicolas Saenz Julienne rpi->firmware = firmware;
419e2bb1834SNicolas Saenz Julienne platform_set_drvdata(pdev, rpi);
4204e85e535SNicolas Saenz Julienne
421be1559f6SMaxime Ripard clk_data = devm_kzalloc(dev, struct_size(clk_data, hws,
422be1559f6SMaxime Ripard RPI_FIRMWARE_NUM_CLK_ID),
423d4b4f1b6SMaxime Ripard GFP_KERNEL);
424d4b4f1b6SMaxime Ripard if (!clk_data)
425d4b4f1b6SMaxime Ripard return -ENOMEM;
426d4b4f1b6SMaxime Ripard
42793d2725aSMaxime Ripard ret = raspberrypi_discover_clocks(rpi, clk_data);
42893d2725aSMaxime Ripard if (ret)
42993d2725aSMaxime Ripard return ret;
430d4b4f1b6SMaxime Ripard
431d4b4f1b6SMaxime Ripard ret = devm_of_clk_add_hw_provider(dev, of_clk_hw_onecell_get,
432d4b4f1b6SMaxime Ripard clk_data);
433d4b4f1b6SMaxime Ripard if (ret)
434d4b4f1b6SMaxime Ripard return ret;
4354e85e535SNicolas Saenz Julienne
436e2bb1834SNicolas Saenz Julienne rpi->cpufreq = platform_device_register_data(dev, "raspberrypi-cpufreq",
437e2bb1834SNicolas Saenz Julienne -1, NULL, 0);
438e2bb1834SNicolas Saenz Julienne
439e2bb1834SNicolas Saenz Julienne return 0;
440e2bb1834SNicolas Saenz Julienne }
441e2bb1834SNicolas Saenz Julienne
raspberrypi_clk_remove(struct platform_device * pdev)44204d19184SUwe Kleine-König static void raspberrypi_clk_remove(struct platform_device *pdev)
443e2bb1834SNicolas Saenz Julienne {
444e2bb1834SNicolas Saenz Julienne struct raspberrypi_clk *rpi = platform_get_drvdata(pdev);
445e2bb1834SNicolas Saenz Julienne
446e2bb1834SNicolas Saenz Julienne platform_device_unregister(rpi->cpufreq);
4474e85e535SNicolas Saenz Julienne }
4484e85e535SNicolas Saenz Julienne
449fbac2e77SMaxime Ripard static const struct of_device_id raspberrypi_clk_match[] = {
450fbac2e77SMaxime Ripard { .compatible = "raspberrypi,firmware-clocks" },
451fbac2e77SMaxime Ripard { },
452fbac2e77SMaxime Ripard };
453fbac2e77SMaxime Ripard MODULE_DEVICE_TABLE(of, raspberrypi_clk_match);
454fbac2e77SMaxime Ripard
4554e85e535SNicolas Saenz Julienne static struct platform_driver raspberrypi_clk_driver = {
4564e85e535SNicolas Saenz Julienne .driver = {
4574e85e535SNicolas Saenz Julienne .name = "raspberrypi-clk",
458fbac2e77SMaxime Ripard .of_match_table = raspberrypi_clk_match,
4594e85e535SNicolas Saenz Julienne },
4604e85e535SNicolas Saenz Julienne .probe = raspberrypi_clk_probe,
461*f00b45dbSUwe Kleine-König .remove = raspberrypi_clk_remove,
4624e85e535SNicolas Saenz Julienne };
4634e85e535SNicolas Saenz Julienne module_platform_driver(raspberrypi_clk_driver);
4644e85e535SNicolas Saenz Julienne
4654e85e535SNicolas Saenz Julienne MODULE_AUTHOR("Nicolas Saenz Julienne <nsaenzjulienne@suse.de>");
4664e85e535SNicolas Saenz Julienne MODULE_DESCRIPTION("Raspberry Pi firmware clock driver");
4674e85e535SNicolas Saenz Julienne MODULE_LICENSE("GPL");
4684e85e535SNicolas Saenz Julienne MODULE_ALIAS("platform:raspberrypi-clk");
469