bcm27xx: update patches from RPi foundation
[oweals/openwrt.git] / target / linux / bcm27xx / patches-5.4 / 950-0119-Add-rpi-poe-fan-driver.patch
1 From 67cfd3ee025f1c47e5921df4bad77d42b1219b67 Mon Sep 17 00:00:00 2001
2 From: Serge Schneider <serge@raspberrypi.org>
3 Date: Mon, 9 Jul 2018 12:54:25 +0100
4 Subject: [PATCH] Add rpi-poe-fan driver
5
6 Signed-off-by: Serge Schneider <serge@raspberrypi.org>
7
8 PoE HAT driver cleanup
9
10 * Fix undeclared variable in rpi_poe_fan_suspend
11 * Add SPDX-License-Identifier
12 * Expand PoE acronym in Kconfig help
13 * Give clearer error message on of_property_count_u32_elems fail
14 * Add documentation
15 * Add vendor to of_device_id compatible string.
16 * Rename m_data_s struct to fw_data_s
17 * Fix typos
18
19 Fixes: #2665
20
21 Signed-off-by: Serge Schneider <serge@raspberrypi.org>
22 ---
23  .../devicetree/bindings/hwmon/rpi-poe-fan.txt |  55 +++
24  Documentation/hwmon/rpi-poe-fan               |  15 +
25  drivers/hwmon/Kconfig                         |  11 +
26  drivers/hwmon/Makefile                        |   1 +
27  drivers/hwmon/rpi-poe-fan.c                   | 436 ++++++++++++++++++
28  5 files changed, 518 insertions(+)
29  create mode 100644 Documentation/devicetree/bindings/hwmon/rpi-poe-fan.txt
30  create mode 100644 Documentation/hwmon/rpi-poe-fan
31  create mode 100644 drivers/hwmon/rpi-poe-fan.c
32
33 --- /dev/null
34 +++ b/Documentation/devicetree/bindings/hwmon/rpi-poe-fan.txt
35 @@ -0,0 +1,55 @@
36 +Bindings for the Raspberry Pi PoE HAT fan
37 +
38 +Required properties:
39 +- compatible   : "raspberrypi,rpi-poe-fan"
40 +- firmware     : Reference to the RPi firmware device node
41 +- pwms         : the PWM that is used to control the PWM fan
42 +- cooling-levels      : PWM duty cycle values in a range from 0 to 255
43 +                       which correspond to thermal cooling states
44 +
45 +Example:
46 +       fan0: rpi-poe-fan@0 {
47 +               compatible = "raspberrypi,rpi-poe-fan";
48 +               firmware = <&firmware>;
49 +               cooling-min-state = <0>;
50 +               cooling-max-state = <3>;
51 +               #cooling-cells = <2>;
52 +               cooling-levels = <0 50 150 255>;
53 +               status = "okay";
54 +       };
55 +
56 +       thermal-zones {
57 +               cpu_thermal: cpu-thermal {
58 +                       trips {
59 +                               threshold: trip-point@0 {
60 +                                       temperature = <45000>;
61 +                                       hysteresis = <5000>;
62 +                                       type = "active";
63 +                               };
64 +                               target: trip-point@1 {
65 +                                       temperature = <50000>;
66 +                                       hysteresis = <2000>;
67 +                                       type = "active";
68 +                               };
69 +                               cpu_hot: cpu_hot@0 {
70 +                                       temperature = <55000>;
71 +                                       hysteresis = <2000>;
72 +                                       type = "active";
73 +                               };
74 +                       };
75 +                       cooling-maps {
76 +                               map0 {
77 +                                       trip = <&threshold>;
78 +                                       cooling-device = <&fan0 0 1>;
79 +                               };
80 +                               map1 {
81 +                                       trip = <&target>;
82 +                                       cooling-device = <&fan0 1 2>;
83 +                               };
84 +                               map2 {
85 +                                       trip = <&cpu_hot>;
86 +                                       cooling-device = <&fan0 2 3>;
87 +                               };
88 +                       };
89 +               };
90 +       };
91 --- /dev/null
92 +++ b/Documentation/hwmon/rpi-poe-fan
93 @@ -0,0 +1,15 @@
94 +Kernel driver rpi-poe-fan
95 +=====================
96 +
97 +This driver enables the use of the Raspberry Pi PoE HAT fan.
98 +
99 +Author: Serge Schneider <serge@raspberrypi.org>
100 +
101 +Description
102 +-----------
103 +
104 +The driver implements a simple interface for driving the Raspberry Pi PoE
105 +(Power over Ethernet) HAT fan. The driver passes commands to the Raspberry Pi
106 +firmware through the mailbox property interface. The firmware then forwards
107 +the commands to the board over I2C on the ID_EEPROM pins. The driver exposes
108 +the fan to the user space through the hwmon sysfs interface.
109 --- a/drivers/hwmon/Kconfig
110 +++ b/drivers/hwmon/Kconfig
111 @@ -1356,6 +1356,17 @@ config SENSORS_RASPBERRYPI_HWMON
112           This driver can also be built as a module. If so, the module
113           will be called raspberrypi-hwmon.
114  
115 +config SENSORS_RPI_POE_FAN
116 +       tristate "Raspberry Pi PoE HAT fan"
117 +       depends on RASPBERRYPI_FIRMWARE
118 +       depends on THERMAL || THERMAL=n
119 +       help
120 +         If you say yes here you get support for Raspberry Pi PoE (Power over
121 +         Ethernet) HAT fan.
122 +
123 +         This driver can also be built as a module.  If so, the module
124 +         will be called rpi-poe-fan.
125 +
126  config SENSORS_SHT15
127         tristate "Sensiron humidity and temperature sensors. SHT15 and compat."
128         depends on GPIOLIB || COMPILE_TEST
129 --- a/drivers/hwmon/Makefile
130 +++ b/drivers/hwmon/Makefile
131 @@ -145,6 +145,7 @@ obj-$(CONFIG_SENSORS_PCF8591)       += pcf8591
132  obj-$(CONFIG_SENSORS_POWR1220)  += powr1220.o
133  obj-$(CONFIG_SENSORS_PWM_FAN)  += pwm-fan.o
134  obj-$(CONFIG_SENSORS_RASPBERRYPI_HWMON)        += raspberrypi-hwmon.o
135 +obj-$(CONFIG_SENSORS_RPI_POE_FAN)      += rpi-poe-fan.o
136  obj-$(CONFIG_SENSORS_S3C)      += s3c-hwmon.o
137  obj-$(CONFIG_SENSORS_SCH56XX_COMMON)+= sch56xx-common.o
138  obj-$(CONFIG_SENSORS_SCH5627)  += sch5627.o
139 --- /dev/null
140 +++ b/drivers/hwmon/rpi-poe-fan.c
141 @@ -0,0 +1,436 @@
142 +// SPDX-License-Identifier: GPL-2.0
143 +/*
144 + * rpi-poe-fan.c - Hwmon driver for Raspberry Pi PoE HAT fan.
145 + *
146 + * Copyright (C) 2018 Raspberry Pi (Trading) Ltd.
147 + * Based on pwm-fan.c by Kamil Debski <k.debski@samsung.com>
148 + *
149 + * Author: Serge Schneider <serge@raspberrypi.org>
150 + */
151 +
152 +#include <linux/hwmon.h>
153 +#include <linux/hwmon-sysfs.h>
154 +#include <linux/module.h>
155 +#include <linux/mutex.h>
156 +#include <linux/notifier.h>
157 +#include <linux/of.h>
158 +#include <linux/platform_device.h>
159 +#include <linux/reboot.h>
160 +#include <linux/sysfs.h>
161 +#include <linux/thermal.h>
162 +#include <soc/bcm2835/raspberrypi-firmware.h>
163 +
164 +#define MAX_PWM 255
165 +
166 +#define POE_CUR_PWM 0x0
167 +#define POE_DEF_PWM 0x1
168 +
169 +struct rpi_poe_fan_ctx {
170 +       struct mutex lock;
171 +       struct rpi_firmware *fw;
172 +       unsigned int pwm_value;
173 +       unsigned int def_pwm_value;
174 +       unsigned int rpi_poe_fan_state;
175 +       unsigned int rpi_poe_fan_max_state;
176 +       unsigned int *rpi_poe_fan_cooling_levels;
177 +       struct thermal_cooling_device *cdev;
178 +       struct notifier_block nb;
179 +};
180 +
181 +struct fw_tag_data_s{
182 +       u32 reg;
183 +       u32 val;
184 +       u32 ret;
185 +};
186 +
187 +static int write_reg(struct rpi_firmware *fw, u32 reg, u32 *val){
188 +       struct fw_tag_data_s fw_tag_data = {
189 +               .reg = reg,
190 +               .val = *val
191 +       };
192 +       int ret;
193 +       ret = rpi_firmware_property(fw, RPI_FIRMWARE_SET_POE_HAT_VAL,
194 +                                   &fw_tag_data, sizeof(fw_tag_data));
195 +       if (ret) {
196 +               return ret;
197 +       } else if (fw_tag_data.ret) {
198 +               return -EIO;
199 +       }
200 +       return 0;
201 +}
202 +
203 +static int read_reg(struct rpi_firmware *fw, u32 reg, u32 *val){
204 +       struct fw_tag_data_s fw_tag_data = {
205 +               .reg = reg,
206 +       };
207 +       int ret;
208 +       ret = rpi_firmware_property(fw, RPI_FIRMWARE_GET_POE_HAT_VAL,
209 +                                   &fw_tag_data, sizeof(fw_tag_data));
210 +       if (ret) {
211 +               return ret;
212 +       } else if (fw_tag_data.ret) {
213 +               return -EIO;
214 +       }
215 +       *val = fw_tag_data.val;
216 +       return 0;
217 +}
218 +
219 +static int rpi_poe_reboot(struct notifier_block *nb, unsigned long code,
220 +                         void *unused)
221 +{
222 +       struct rpi_poe_fan_ctx *ctx = container_of(nb, struct rpi_poe_fan_ctx,
223 +                                                  nb);
224 +
225 +       if (ctx->pwm_value != ctx->def_pwm_value)
226 +               write_reg(ctx->fw, POE_CUR_PWM, &ctx->def_pwm_value);
227 +
228 +       return NOTIFY_DONE;
229 +}
230 +
231 +static int  __set_pwm(struct rpi_poe_fan_ctx *ctx, u32 pwm)
232 +{
233 +       int ret = 0;
234 +
235 +       mutex_lock(&ctx->lock);
236 +       if (ctx->pwm_value == pwm)
237 +               goto exit_set_pwm_err;
238 +
239 +       ret = write_reg(ctx->fw, POE_CUR_PWM, &pwm);
240 +       if (!ret)
241 +               ctx->pwm_value = pwm;
242 +exit_set_pwm_err:
243 +       mutex_unlock(&ctx->lock);
244 +       return ret;
245 +}
246 +
247 +static int  __set_def_pwm(struct rpi_poe_fan_ctx *ctx, u32 def_pwm)
248 +{
249 +       int ret = 0;
250 +       mutex_lock(&ctx->lock);
251 +       if (ctx->def_pwm_value == def_pwm)
252 +               goto exit_set_def_pwm_err;
253 +
254 +       ret = write_reg(ctx->fw, POE_CUR_PWM, &def_pwm);
255 +       if (!ret)
256 +               ctx->def_pwm_value = def_pwm;
257 +exit_set_def_pwm_err:
258 +       mutex_unlock(&ctx->lock);
259 +       return ret;
260 +}
261 +
262 +static void rpi_poe_fan_update_state(struct rpi_poe_fan_ctx *ctx,
263 +                                    unsigned long pwm)
264 +{
265 +       int i;
266 +
267 +       for (i = 0; i < ctx->rpi_poe_fan_max_state; ++i)
268 +               if (pwm < ctx->rpi_poe_fan_cooling_levels[i + 1])
269 +                       break;
270 +
271 +       ctx->rpi_poe_fan_state = i;
272 +}
273 +
274 +static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
275 +                      const char *buf, size_t count)
276 +{
277 +       struct rpi_poe_fan_ctx *ctx = dev_get_drvdata(dev);
278 +       unsigned long pwm;
279 +       int ret;
280 +
281 +       if (kstrtoul(buf, 10, &pwm) || pwm > MAX_PWM)
282 +               return -EINVAL;
283 +
284 +       ret = __set_pwm(ctx, pwm);
285 +       if (ret)
286 +               return ret;
287 +
288 +       rpi_poe_fan_update_state(ctx, pwm);
289 +       return count;
290 +}
291 +
292 +static ssize_t set_def_pwm(struct device *dev, struct device_attribute *attr,
293 +                          const char *buf, size_t count)
294 +{
295 +       struct rpi_poe_fan_ctx *ctx = dev_get_drvdata(dev);
296 +       unsigned long def_pwm;
297 +       int ret;
298 +
299 +       if (kstrtoul(buf, 10, &def_pwm) || def_pwm > MAX_PWM)
300 +               return -EINVAL;
301 +
302 +       ret = __set_def_pwm(ctx, def_pwm);
303 +       if (ret)
304 +               return ret;
305 +       return count;
306 +}
307 +
308 +static ssize_t show_pwm(struct device *dev,
309 +                       struct device_attribute *attr, char *buf)
310 +{
311 +       struct rpi_poe_fan_ctx *ctx = dev_get_drvdata(dev);
312 +
313 +       return sprintf(buf, "%u\n", ctx->pwm_value);
314 +}
315 +
316 +static ssize_t show_def_pwm(struct device *dev,
317 +                       struct device_attribute *attr, char *buf)
318 +{
319 +       struct rpi_poe_fan_ctx *ctx = dev_get_drvdata(dev);
320 +
321 +       return sprintf(buf, "%u\n", ctx->def_pwm_value);
322 +}
323 +
324 +
325 +static SENSOR_DEVICE_ATTR(pwm1, 0644, show_pwm, set_pwm, 0);
326 +static SENSOR_DEVICE_ATTR(def_pwm1, 0644, show_def_pwm, set_def_pwm, 1);
327 +
328 +static struct attribute *rpi_poe_fan_attrs[] = {
329 +       &sensor_dev_attr_pwm1.dev_attr.attr,
330 +       &sensor_dev_attr_def_pwm1.dev_attr.attr,
331 +       NULL,
332 +};
333 +
334 +ATTRIBUTE_GROUPS(rpi_poe_fan);
335 +
336 +/* thermal cooling device callbacks */
337 +static int rpi_poe_fan_get_max_state(struct thermal_cooling_device *cdev,
338 +                                    unsigned long *state)
339 +{
340 +       struct rpi_poe_fan_ctx *ctx = cdev->devdata;
341 +
342 +       if (!ctx)
343 +               return -EINVAL;
344 +
345 +       *state = ctx->rpi_poe_fan_max_state;
346 +
347 +       return 0;
348 +}
349 +
350 +static int rpi_poe_fan_get_cur_state(struct thermal_cooling_device *cdev,
351 +                                    unsigned long *state)
352 +{
353 +       struct rpi_poe_fan_ctx *ctx = cdev->devdata;
354 +
355 +       if (!ctx)
356 +               return -EINVAL;
357 +
358 +       *state = ctx->rpi_poe_fan_state;
359 +
360 +       return 0;
361 +}
362 +
363 +static int rpi_poe_fan_set_cur_state(struct thermal_cooling_device *cdev,
364 +                                    unsigned long state)
365 +{
366 +       struct rpi_poe_fan_ctx *ctx = cdev->devdata;
367 +       int ret;
368 +
369 +       if (!ctx || (state > ctx->rpi_poe_fan_max_state))
370 +               return -EINVAL;
371 +
372 +       if (state == ctx->rpi_poe_fan_state)
373 +               return 0;
374 +
375 +       ret = __set_pwm(ctx, ctx->rpi_poe_fan_cooling_levels[state]);
376 +       if (ret) {
377 +               dev_err(&cdev->device, "Cannot set pwm!\n");
378 +               return ret;
379 +       }
380 +
381 +       ctx->rpi_poe_fan_state = state;
382 +
383 +       return ret;
384 +}
385 +
386 +static const struct thermal_cooling_device_ops rpi_poe_fan_cooling_ops = {
387 +       .get_max_state = rpi_poe_fan_get_max_state,
388 +       .get_cur_state = rpi_poe_fan_get_cur_state,
389 +       .set_cur_state = rpi_poe_fan_set_cur_state,
390 +};
391 +
392 +static int rpi_poe_fan_of_get_cooling_data(struct device *dev,
393 +                                      struct rpi_poe_fan_ctx *ctx)
394 +{
395 +       struct device_node *np = dev->of_node;
396 +       int num, i, ret;
397 +
398 +       if (!of_find_property(np, "cooling-levels", NULL))
399 +               return 0;
400 +
401 +       ret = of_property_count_u32_elems(np, "cooling-levels");
402 +       if (ret <= 0) {
403 +               dev_err(dev, "cooling-levels property missing or invalid: %d\n",
404 +                       ret);
405 +               return ret ? : -EINVAL;
406 +       }
407 +
408 +       num = ret;
409 +       ctx->rpi_poe_fan_cooling_levels = devm_kzalloc(dev, num * sizeof(u32),
410 +                                                  GFP_KERNEL);
411 +       if (!ctx->rpi_poe_fan_cooling_levels)
412 +               return -ENOMEM;
413 +
414 +       ret = of_property_read_u32_array(np, "cooling-levels",
415 +                                        ctx->rpi_poe_fan_cooling_levels, num);
416 +       if (ret) {
417 +               dev_err(dev, "Property 'cooling-levels' cannot be read!\n");
418 +               return ret;
419 +       }
420 +
421 +       for (i = 0; i < num; i++) {
422 +               if (ctx->rpi_poe_fan_cooling_levels[i] > MAX_PWM) {
423 +                       dev_err(dev, "PWM fan state[%d]:%d > %d\n", i,
424 +                               ctx->rpi_poe_fan_cooling_levels[i], MAX_PWM);
425 +                       return -EINVAL;
426 +               }
427 +       }
428 +
429 +       ctx->rpi_poe_fan_max_state = num - 1;
430 +
431 +       return 0;
432 +}
433 +
434 +static int rpi_poe_fan_probe(struct platform_device *pdev)
435 +{
436 +       struct thermal_cooling_device *cdev;
437 +       struct rpi_poe_fan_ctx *ctx;
438 +       struct device *hwmon;
439 +       struct device_node *np = pdev->dev.of_node;
440 +       struct device_node *fw_node;
441 +       int ret;
442 +
443 +       fw_node = of_parse_phandle(np, "firmware", 0);
444 +       if (!fw_node) {
445 +               dev_err(&pdev->dev, "Missing firmware node\n");
446 +               return -ENOENT;
447 +       }
448 +
449 +       ctx = devm_kzalloc(&pdev->dev, sizeof(*ctx), GFP_KERNEL);
450 +       if (!ctx)
451 +               return -ENOMEM;
452 +
453 +       mutex_init(&ctx->lock);
454 +
455 +       ctx->fw = rpi_firmware_get(fw_node);
456 +       if (!ctx->fw)
457 +               return -EPROBE_DEFER;
458 +
459 +       platform_set_drvdata(pdev, ctx);
460 +
461 +       ctx->nb.notifier_call = rpi_poe_reboot;
462 +       ret = register_reboot_notifier(&ctx->nb);
463 +       if (ret) {
464 +               dev_err(&pdev->dev, "Failed to register reboot notifier: %i\n",
465 +                       ret);
466 +               return ret;
467 +       }
468 +       ret = read_reg(ctx->fw, POE_DEF_PWM, &ctx->def_pwm_value);
469 +       if (ret) {
470 +               dev_err(&pdev->dev, "Failed to get default PWM value: %i\n",
471 +                       ret);
472 +               goto err;
473 +       }
474 +       ret = read_reg(ctx->fw, POE_CUR_PWM, &ctx->pwm_value);
475 +       if (ret) {
476 +               dev_err(&pdev->dev, "Failed to get current PWM value: %i\n",
477 +                       ret);
478 +               goto err;
479 +       }
480 +
481 +       hwmon = devm_hwmon_device_register_with_groups(&pdev->dev, "rpipoefan",
482 +                                                      ctx, rpi_poe_fan_groups);
483 +       if (IS_ERR(hwmon)) {
484 +               dev_err(&pdev->dev, "Failed to register hwmon device\n");
485 +               ret = PTR_ERR(hwmon);
486 +               goto err;
487 +       }
488 +
489 +       ret = rpi_poe_fan_of_get_cooling_data(&pdev->dev, ctx);
490 +       if (ret)
491 +               return ret;
492 +
493 +       rpi_poe_fan_update_state(ctx, ctx->pwm_value);
494 +       if (!IS_ENABLED(CONFIG_THERMAL))
495 +               return 0;
496 +
497 +       cdev = thermal_of_cooling_device_register(np,
498 +                                                 "rpi-poe-fan", ctx,
499 +                                                 &rpi_poe_fan_cooling_ops);
500 +       if (IS_ERR(cdev)) {
501 +               dev_err(&pdev->dev,
502 +                       "Failed to register rpi-poe-fan as cooling device");
503 +               ret = PTR_ERR(cdev);
504 +               goto err;
505 +       }
506 +       ctx->cdev = cdev;
507 +       thermal_cdev_update(cdev);
508 +
509 +       return 0;
510 +err:
511 +       unregister_reboot_notifier(&ctx->nb);
512 +       return ret;
513 +}
514 +
515 +static int rpi_poe_fan_remove(struct platform_device *pdev)
516 +{
517 +       struct rpi_poe_fan_ctx *ctx = platform_get_drvdata(pdev);
518 +       u32 value = ctx->def_pwm_value;
519 +
520 +       unregister_reboot_notifier(&ctx->nb);
521 +       thermal_cooling_device_unregister(ctx->cdev);
522 +       if (ctx->pwm_value != value) {
523 +               write_reg(ctx->fw, POE_CUR_PWM, &value);
524 +       }
525 +       return 0;
526 +}
527 +
528 +#ifdef CONFIG_PM_SLEEP
529 +static int rpi_poe_fan_suspend(struct device *dev)
530 +{
531 +       struct rpi_poe_fan_ctx *ctx = dev_get_drvdata(dev);
532 +       u32 value = 0;
533 +       int ret = 0;
534 +
535 +       if (ctx->pwm_value != value)
536 +               ret = write_reg(ctx->fw, POE_CUR_PWM, &value);
537 +       return ret;
538 +}
539 +
540 +static int rpi_poe_fan_resume(struct device *dev)
541 +{
542 +       struct rpi_poe_fan_ctx *ctx = dev_get_drvdata(dev);
543 +       u32 value = ctx->pwm_value;
544 +       int ret = 0;
545 +
546 +       if (value != 0)
547 +               ret = write_reg(ctx->fw, POE_CUR_PWM, &value);
548 +
549 +       return ret;
550 +}
551 +#endif
552 +
553 +static SIMPLE_DEV_PM_OPS(rpi_poe_fan_pm, rpi_poe_fan_suspend,
554 +                        rpi_poe_fan_resume);
555 +
556 +static const struct of_device_id of_rpi_poe_fan_match[] = {
557 +       { .compatible = "raspberrypi,rpi-poe-fan", },
558 +       {},
559 +};
560 +MODULE_DEVICE_TABLE(of, of_rpi_poe_fan_match);
561 +
562 +static struct platform_driver rpi_poe_fan_driver = {
563 +       .probe          = rpi_poe_fan_probe,
564 +       .remove         = rpi_poe_fan_remove,
565 +       .driver = {
566 +               .name           = "rpi-poe-fan",
567 +               .pm             = &rpi_poe_fan_pm,
568 +               .of_match_table = of_rpi_poe_fan_match,
569 +       },
570 +};
571 +
572 +module_platform_driver(rpi_poe_fan_driver);
573 +
574 +MODULE_AUTHOR("Serge Schneider <serge@raspberrypi.org>");
575 +MODULE_ALIAS("platform:rpi-poe-fan");
576 +MODULE_DESCRIPTION("Raspberry Pi PoE HAT fan driver");
577 +MODULE_LICENSE("GPL");