2eb971f89109451c2c2cb074512ef89ba182b7d8
[openwrt/staging/hauke.git] /
1 From fd864dfdcaa04271c09767bba5d77247b0899a6e Mon Sep 17 00:00:00 2001
2 From: Dave Stevenson <dave.stevenson@raspberrypi.com>
3 Date: Wed, 19 Jan 2022 17:26:22 +0000
4 Subject: [PATCH] pwm: raspberrypi-poe: Add option of being created by
5 MFD or FW
6
7 The firmware can only use I2C0 if the kernel isn't, therefore
8 with libcamera and DRM using it the PoE HAT fan control needs
9 to move to the kernel.
10
11 Add the option for the driver to be created by the PoE HAT core
12 MFD driver, and use the I2C regmap that provides to control fan
13 functions.
14
15 Signed-off-by: Dave Stevenson <dave.stevenson@raspberrypi.com>
16 ---
17 drivers/pwm/pwm-raspberrypi-poe.c | 81 ++++++++++++++++++-------------
18 1 file changed, 48 insertions(+), 33 deletions(-)
19
20 --- a/drivers/pwm/pwm-raspberrypi-poe.c
21 +++ b/drivers/pwm/pwm-raspberrypi-poe.c
22 @@ -16,6 +16,7 @@
23 #include <linux/of.h>
24 #include <linux/platform_device.h>
25 #include <linux/pwm.h>
26 +#include <linux/regmap.h>
27
28 #include <soc/bcm2835/raspberrypi-firmware.h>
29 #include <dt-bindings/pwm/raspberrypi,firmware-poe-pwm.h>
30 @@ -27,6 +28,10 @@
31
32 struct raspberrypi_pwm {
33 struct rpi_firmware *firmware;
34 +
35 + struct regmap *regmap;
36 + u32 offset;
37 +
38 struct pwm_chip chip;
39 unsigned int duty_cycle;
40 };
41 @@ -43,7 +48,7 @@ struct raspberrypi_pwm *raspberrypi_pwm_
42 return container_of(chip, struct raspberrypi_pwm, chip);
43 }
44
45 -static int raspberrypi_pwm_set_property(struct rpi_firmware *firmware,
46 +static int raspberrypi_pwm_set_property(struct raspberrypi_pwm *pwm,
47 u32 reg, u32 val)
48 {
49 struct raspberrypi_pwm_prop msg = {
50 @@ -52,17 +57,19 @@ static int raspberrypi_pwm_set_property(
51 };
52 int ret;
53
54 - ret = rpi_firmware_property(firmware, RPI_FIRMWARE_SET_POE_HAT_VAL,
55 - &msg, sizeof(msg));
56 - if (ret)
57 - return ret;
58 - if (msg.ret)
59 - return -EIO;
60 + if (pwm->firmware) {
61 + ret = rpi_firmware_property(pwm->firmware, RPI_FIRMWARE_SET_POE_HAT_VAL,
62 + &msg, sizeof(msg));
63 + if (!ret && msg.ret)
64 + ret = -EIO;
65 + } else {
66 + ret = regmap_write(pwm->regmap, pwm->offset + reg, val);
67 + }
68
69 - return 0;
70 + return ret;
71 }
72
73 -static int raspberrypi_pwm_get_property(struct rpi_firmware *firmware,
74 +static int raspberrypi_pwm_get_property(struct raspberrypi_pwm *pwm,
75 u32 reg, u32 *val)
76 {
77 struct raspberrypi_pwm_prop msg = {
78 @@ -70,16 +77,17 @@ static int raspberrypi_pwm_get_property(
79 };
80 int ret;
81
82 - ret = rpi_firmware_property(firmware, RPI_FIRMWARE_GET_POE_HAT_VAL,
83 - &msg, sizeof(msg));
84 - if (ret)
85 - return ret;
86 - if (msg.ret)
87 - return -EIO;
88 -
89 - *val = le32_to_cpu(msg.val);
90 + if (pwm->firmware) {
91 + ret = rpi_firmware_property(pwm->firmware, RPI_FIRMWARE_GET_POE_HAT_VAL,
92 + &msg, sizeof(msg));
93 + if (!ret && msg.ret)
94 + ret = -EIO;
95 + *val = le32_to_cpu(msg.val);
96 + } else {
97 + ret = regmap_read(pwm->regmap, pwm->offset + reg, val);
98 + }
99
100 - return 0;
101 + return ret;
102 }
103
104 static void raspberrypi_pwm_get_state(struct pwm_chip *chip,
105 @@ -117,7 +125,7 @@ static int raspberrypi_pwm_apply(struct
106 if (duty_cycle == rpipwm->duty_cycle)
107 return 0;
108
109 - ret = raspberrypi_pwm_set_property(rpipwm->firmware, RPI_PWM_CUR_DUTY_REG,
110 + ret = raspberrypi_pwm_set_property(rpipwm, RPI_PWM_CUR_DUTY_REG,
111 duty_cycle);
112 if (ret) {
113 dev_err(chip->dev, "Failed to set duty cycle: %pe\n",
114 @@ -144,29 +152,35 @@ static int raspberrypi_pwm_probe(struct
115 struct raspberrypi_pwm *rpipwm;
116 int ret;
117
118 - firmware_node = of_get_parent(dev->of_node);
119 - if (!firmware_node) {
120 - dev_err(dev, "Missing firmware node\n");
121 - return -ENOENT;
122 - }
123 -
124 - firmware = devm_rpi_firmware_get(&pdev->dev, firmware_node);
125 - of_node_put(firmware_node);
126 - if (!firmware)
127 - return dev_err_probe(dev, -EPROBE_DEFER,
128 - "Failed to get firmware handle\n");
129 -
130 rpipwm = devm_kzalloc(&pdev->dev, sizeof(*rpipwm), GFP_KERNEL);
131 if (!rpipwm)
132 return -ENOMEM;
133
134 - rpipwm->firmware = firmware;
135 + if (pdev->dev.parent)
136 + rpipwm->regmap = dev_get_regmap(pdev->dev.parent, NULL);
137 +
138 + if (rpipwm->regmap) {
139 + ret = device_property_read_u32(&pdev->dev, "reg", &rpipwm->offset);
140 + if (ret)
141 + return -EINVAL;
142 + } else {
143 + firmware_node = of_get_parent(dev->of_node);
144 +
145 + firmware = devm_rpi_firmware_get(&pdev->dev, firmware_node);
146 + of_node_put(firmware_node);
147 + if (!firmware)
148 + return dev_err_probe(dev, -EPROBE_DEFER,
149 + "Failed to get firmware handle\n");
150 +
151 + rpipwm->firmware = firmware;
152 + }
153 +
154 rpipwm->chip.dev = dev;
155 rpipwm->chip.ops = &raspberrypi_pwm_ops;
156 rpipwm->chip.base = -1;
157 rpipwm->chip.npwm = RASPBERRYPI_FIRMWARE_PWM_NUM;
158
159 - ret = raspberrypi_pwm_get_property(rpipwm->firmware, RPI_PWM_CUR_DUTY_REG,
160 + ret = raspberrypi_pwm_get_property(rpipwm, RPI_PWM_CUR_DUTY_REG,
161 &rpipwm->duty_cycle);
162 if (ret) {
163 dev_err(dev, "Failed to get duty cycle: %pe\n", ERR_PTR(ret));
164 @@ -178,6 +192,7 @@ static int raspberrypi_pwm_probe(struct
165
166 static const struct of_device_id raspberrypi_pwm_of_match[] = {
167 { .compatible = "raspberrypi,firmware-poe-pwm", },
168 + { .compatible = "raspberrypi,poe-pwm", },
169 { }
170 };
171 MODULE_DEVICE_TABLE(of, raspberrypi_pwm_of_match);