mvebu: copy 5.15 patches to 6.1
[openwrt/openwrt.git] / target / linux / mvebu / patches-6.1 / 903-drivers-hwmon-Add-the-IEI-WT61P803-PUZZLE-HWMON-driv.patch
1 From e3310a638cd310bfd93dbbc6d2732ab6aea18dd2 Mon Sep 17 00:00:00 2001
2 From: Luka Kovacic <luka.kovacic () sartura ! hr>
3 Date: Tue, 24 Aug 2021 12:44:34 +0000
4 Subject: [PATCH 3/7] drivers: hwmon: Add the IEI WT61P803 PUZZLE HWMON driver
5
6 Add the IEI WT61P803 PUZZLE HWMON driver, that handles the fan speed
7 control via PWM, reading fan speed and reading on-board temperature
8 sensors.
9
10 The driver registers a HWMON device and a simple thermal cooling device to
11 enable in-kernel fan management.
12
13 This driver depends on the IEI WT61P803 PUZZLE MFD driver.
14
15 Signed-off-by: Luka Kovacic <luka.kovacic@sartura.hr>
16 Signed-off-by: Pavo Banicevic <pavo.banicevic@sartura.hr>
17 Acked-by: Guenter Roeck <linux@roeck-us.net>
18 Cc: Luka Perkov <luka.perkov@sartura.hr>
19 Cc: Robert Marko <robert.marko@sartura.hr>
20 ---
21 drivers/hwmon/Kconfig | 8 +
22 drivers/hwmon/Makefile | 1 +
23 drivers/hwmon/iei-wt61p803-puzzle-hwmon.c | 413 ++++++++++++++++++++++
24 3 files changed, 422 insertions(+)
25 create mode 100644 drivers/hwmon/iei-wt61p803-puzzle-hwmon.c
26
27 --- a/drivers/hwmon/Kconfig
28 +++ b/drivers/hwmon/Kconfig
29 @@ -732,6 +732,14 @@ config SENSORS_IBMPOWERNV
30 This driver can also be built as a module. If so, the module
31 will be called ibmpowernv.
32
33 +config SENSORS_IEI_WT61P803_PUZZLE_HWMON
34 + tristate "IEI WT61P803 PUZZLE MFD HWMON Driver"
35 + depends on MFD_IEI_WT61P803_PUZZLE
36 + help
37 + The IEI WT61P803 PUZZLE MFD HWMON Driver handles reading fan speed
38 + and writing fan PWM values. It also supports reading on-board
39 + temperature sensors.
40 +
41 config SENSORS_IIO_HWMON
42 tristate "Hwmon driver that uses channels specified via iio maps"
43 depends on IIO
44 --- a/drivers/hwmon/Makefile
45 +++ b/drivers/hwmon/Makefile
46 @@ -84,6 +84,7 @@ obj-$(CONFIG_SENSORS_HIH6130) += hih6130
47 obj-$(CONFIG_SENSORS_ULTRA45) += ultra45_env.o
48 obj-$(CONFIG_SENSORS_I5500) += i5500_temp.o
49 obj-$(CONFIG_SENSORS_I5K_AMB) += i5k_amb.o
50 +obj-$(CONFIG_SENSORS_IEI_WT61P803_PUZZLE_HWMON) += iei-wt61p803-puzzle-hwmon.o
51 obj-$(CONFIG_SENSORS_IBMAEM) += ibmaem.o
52 obj-$(CONFIG_SENSORS_IBMPEX) += ibmpex.o
53 obj-$(CONFIG_SENSORS_IBMPOWERNV)+= ibmpowernv.o
54 --- /dev/null
55 +++ b/drivers/hwmon/iei-wt61p803-puzzle-hwmon.c
56 @@ -0,0 +1,445 @@
57 +// SPDX-License-Identifier: GPL-2.0-only
58 +/* IEI WT61P803 PUZZLE MCU HWMON Driver
59 + *
60 + * Copyright (C) 2020 Sartura Ltd.
61 + * Author: Luka Kovacic <luka.kovacic@sartura.hr>
62 + */
63 +
64 +#include <linux/err.h>
65 +#include <linux/hwmon.h>
66 +#include <linux/interrupt.h>
67 +#include <linux/irq.h>
68 +#include <linux/math64.h>
69 +#include <linux/mfd/iei-wt61p803-puzzle.h>
70 +#include <linux/mod_devicetable.h>
71 +#include <linux/module.h>
72 +#include <linux/platform_device.h>
73 +#include <linux/property.h>
74 +#include <linux/slab.h>
75 +#include <linux/thermal.h>
76 +
77 +#define IEI_WT61P803_PUZZLE_HWMON_MAX_PWM 2
78 +#define IEI_WT61P803_PUZZLE_HWMON_MAX_PWM_VAL 255
79 +
80 +/**
81 + * struct iei_wt61p803_puzzle_thermal_cooling_device - Thermal cooling device instance
82 + * @mcu_hwmon: Parent driver struct pointer
83 + * @tcdev: Thermal cooling device pointer
84 + * @name: Thermal cooling device name
85 + * @pwm_channel: Controlled PWM channel (0 or 1)
86 + * @cooling_levels: Thermal cooling device cooling levels (DT)
87 + * @cur_level: Current cooling level
88 + * @num_levels: Number of cooling levels
89 + */
90 +struct iei_wt61p803_puzzle_thermal_cooling_device {
91 + struct iei_wt61p803_puzzle_hwmon *mcu_hwmon;
92 + struct thermal_cooling_device *tcdev;
93 + char name[THERMAL_NAME_LENGTH];
94 + int pwm_channel;
95 + u32 *cooling_levels;
96 + int cur_level;
97 + u8 num_levels;
98 +};
99 +
100 +/**
101 + * struct iei_wt61p803_puzzle_hwmon - MCU HWMON Driver
102 + * @mcu: MCU struct pointer
103 + * @response_buffer Global MCU response buffer
104 + * @thermal_cooling_dev_present: Per-channel thermal cooling device control indicator
105 + * @cdev: Per-channel thermal cooling device private structure
106 + */
107 +struct iei_wt61p803_puzzle_hwmon {
108 + struct iei_wt61p803_puzzle *mcu;
109 + unsigned char response_buffer[IEI_WT61P803_PUZZLE_BUF_SIZE];
110 + bool thermal_cooling_dev_present[IEI_WT61P803_PUZZLE_HWMON_MAX_PWM];
111 + struct iei_wt61p803_puzzle_thermal_cooling_device
112 + *cdev[IEI_WT61P803_PUZZLE_HWMON_MAX_PWM];
113 + struct mutex lock; /* mutex to protect response_buffer array */
114 +};
115 +
116 +#define raw_temp_to_milidegree_celsius(x) (((x) - 0x80) * 1000)
117 +static int iei_wt61p803_puzzle_read_temp_sensor(struct iei_wt61p803_puzzle_hwmon *mcu_hwmon,
118 + int channel, long *value)
119 +{
120 + unsigned char *resp_buf = mcu_hwmon->response_buffer;
121 + unsigned char temp_sensor_ntc_cmd[4] = {
122 + IEI_WT61P803_PUZZLE_CMD_HEADER_START,
123 + IEI_WT61P803_PUZZLE_CMD_TEMP,
124 + IEI_WT61P803_PUZZLE_CMD_TEMP_ALL,
125 + };
126 + size_t reply_size;
127 + int ret;
128 +
129 + mutex_lock(&mcu_hwmon->lock);
130 + ret = iei_wt61p803_puzzle_write_command(mcu_hwmon->mcu, temp_sensor_ntc_cmd,
131 + sizeof(temp_sensor_ntc_cmd), resp_buf,
132 + &reply_size);
133 + if (ret)
134 + goto exit;
135 +
136 + if (reply_size != 7) {
137 + ret = -EIO;
138 + goto exit;
139 + }
140 +
141 + /* Check the number of NTC values */
142 + if (resp_buf[3] != '2') {
143 + ret = -EIO;
144 + goto exit;
145 + }
146 +
147 + *value = raw_temp_to_milidegree_celsius(resp_buf[4 + channel]);
148 +exit:
149 + mutex_unlock(&mcu_hwmon->lock);
150 + return ret;
151 +}
152 +
153 +#define raw_fan_val_to_rpm(x, y) ((((x) << 8 | (y)) / 2) * 60)
154 +static int iei_wt61p803_puzzle_read_fan_speed(struct iei_wt61p803_puzzle_hwmon *mcu_hwmon,
155 + int channel, long *value)
156 +{
157 + unsigned char *resp_buf = mcu_hwmon->response_buffer;
158 + unsigned char fan_speed_cmd[4] = {};
159 + size_t reply_size;
160 + int ret;
161 +
162 + fan_speed_cmd[0] = IEI_WT61P803_PUZZLE_CMD_HEADER_START;
163 + fan_speed_cmd[1] = IEI_WT61P803_PUZZLE_CMD_FAN;
164 + fan_speed_cmd[2] = IEI_WT61P803_PUZZLE_CMD_FAN_RPM(channel);
165 +
166 + mutex_lock(&mcu_hwmon->lock);
167 + ret = iei_wt61p803_puzzle_write_command(mcu_hwmon->mcu, fan_speed_cmd,
168 + sizeof(fan_speed_cmd), resp_buf,
169 + &reply_size);
170 + if (ret)
171 + goto exit;
172 +
173 + if (reply_size != 7) {
174 + ret = -EIO;
175 + goto exit;
176 + }
177 +
178 + *value = raw_fan_val_to_rpm(resp_buf[3], resp_buf[4]);
179 +exit:
180 + mutex_unlock(&mcu_hwmon->lock);
181 + return ret;
182 +}
183 +
184 +static int iei_wt61p803_puzzle_write_pwm_channel(struct iei_wt61p803_puzzle_hwmon *mcu_hwmon,
185 + int channel, long pwm_set_val)
186 +{
187 + unsigned char *resp_buf = mcu_hwmon->response_buffer;
188 + unsigned char pwm_set_cmd[6] = {};
189 + size_t reply_size;
190 + int ret;
191 +
192 + pwm_set_cmd[0] = IEI_WT61P803_PUZZLE_CMD_HEADER_START;
193 + pwm_set_cmd[1] = IEI_WT61P803_PUZZLE_CMD_FAN;
194 + pwm_set_cmd[2] = IEI_WT61P803_PUZZLE_CMD_FAN_PWM_WRITE;
195 + pwm_set_cmd[3] = IEI_WT61P803_PUZZLE_CMD_FAN_PWM(channel);
196 + pwm_set_cmd[4] = pwm_set_val;
197 +
198 + mutex_lock(&mcu_hwmon->lock);
199 + ret = iei_wt61p803_puzzle_write_command(mcu_hwmon->mcu, pwm_set_cmd,
200 + sizeof(pwm_set_cmd), resp_buf,
201 + &reply_size);
202 + if (ret)
203 + goto exit;
204 +
205 + if (reply_size != 3) {
206 + ret = -EIO;
207 + goto exit;
208 + }
209 +
210 + if (!(resp_buf[0] == IEI_WT61P803_PUZZLE_CMD_HEADER_START &&
211 + resp_buf[1] == IEI_WT61P803_PUZZLE_CMD_RESPONSE_OK &&
212 + resp_buf[2] == IEI_WT61P803_PUZZLE_CHECKSUM_RESPONSE_OK)) {
213 + ret = -EIO;
214 + goto exit;
215 + }
216 +exit:
217 + mutex_unlock(&mcu_hwmon->lock);
218 + return ret;
219 +}
220 +
221 +static int iei_wt61p803_puzzle_read_pwm_channel(struct iei_wt61p803_puzzle_hwmon *mcu_hwmon,
222 + int channel, long *value)
223 +{
224 + unsigned char *resp_buf = mcu_hwmon->response_buffer;
225 + unsigned char pwm_get_cmd[5] = {};
226 + size_t reply_size;
227 + int ret;
228 +
229 + pwm_get_cmd[0] = IEI_WT61P803_PUZZLE_CMD_HEADER_START;
230 + pwm_get_cmd[1] = IEI_WT61P803_PUZZLE_CMD_FAN;
231 + pwm_get_cmd[2] = IEI_WT61P803_PUZZLE_CMD_FAN_PWM_READ;
232 + pwm_get_cmd[3] = IEI_WT61P803_PUZZLE_CMD_FAN_PWM(channel);
233 +
234 + ret = iei_wt61p803_puzzle_write_command(mcu_hwmon->mcu, pwm_get_cmd,
235 + sizeof(pwm_get_cmd), resp_buf,
236 + &reply_size);
237 + if (ret)
238 + return ret;
239 +
240 + if (reply_size != 5)
241 + return -EIO;
242 +
243 + if (resp_buf[2] != IEI_WT61P803_PUZZLE_CMD_FAN_PWM_READ)
244 + return -EIO;
245 +
246 + *value = resp_buf[3];
247 +
248 + return 0;
249 +}
250 +
251 +static int iei_wt61p803_puzzle_read(struct device *dev, enum hwmon_sensor_types type,
252 + u32 attr, int channel, long *val)
253 +{
254 + struct iei_wt61p803_puzzle_hwmon *mcu_hwmon = dev_get_drvdata(dev->parent);
255 +
256 + switch (type) {
257 + case hwmon_pwm:
258 + return iei_wt61p803_puzzle_read_pwm_channel(mcu_hwmon, channel, val);
259 + case hwmon_fan:
260 + return iei_wt61p803_puzzle_read_fan_speed(mcu_hwmon, channel, val);
261 + case hwmon_temp:
262 + return iei_wt61p803_puzzle_read_temp_sensor(mcu_hwmon, channel, val);
263 + default:
264 + return -EINVAL;
265 + }
266 +}
267 +
268 +static int iei_wt61p803_puzzle_write(struct device *dev, enum hwmon_sensor_types type,
269 + u32 attr, int channel, long val)
270 +{
271 + struct iei_wt61p803_puzzle_hwmon *mcu_hwmon = dev_get_drvdata(dev->parent);
272 +
273 + return iei_wt61p803_puzzle_write_pwm_channel(mcu_hwmon, channel, val);
274 +}
275 +
276 +static umode_t iei_wt61p803_puzzle_is_visible(const void *data, enum hwmon_sensor_types type,
277 + u32 attr, int channel)
278 +{
279 + const struct iei_wt61p803_puzzle_hwmon *mcu_hwmon = data;
280 +
281 + switch (type) {
282 + case hwmon_pwm:
283 + if (mcu_hwmon->thermal_cooling_dev_present[channel])
284 + return 0444;
285 + if (attr == hwmon_pwm_input)
286 + return 0644;
287 + break;
288 + case hwmon_fan:
289 + if (attr == hwmon_fan_input)
290 + return 0444;
291 + break;
292 + case hwmon_temp:
293 + if (attr == hwmon_temp_input)
294 + return 0444;
295 + break;
296 + default:
297 + return 0;
298 + }
299 +
300 + return 0;
301 +}
302 +
303 +static const struct hwmon_ops iei_wt61p803_puzzle_hwmon_ops = {
304 + .is_visible = iei_wt61p803_puzzle_is_visible,
305 + .read = iei_wt61p803_puzzle_read,
306 + .write = iei_wt61p803_puzzle_write,
307 +};
308 +
309 +static const struct hwmon_channel_info *iei_wt61p803_puzzle_info[] = {
310 + HWMON_CHANNEL_INFO(pwm,
311 + HWMON_PWM_INPUT,
312 + HWMON_PWM_INPUT),
313 + HWMON_CHANNEL_INFO(fan,
314 + HWMON_F_INPUT,
315 + HWMON_F_INPUT,
316 + HWMON_F_INPUT,
317 + HWMON_F_INPUT,
318 + HWMON_F_INPUT),
319 + HWMON_CHANNEL_INFO(temp,
320 + HWMON_T_INPUT,
321 + HWMON_T_INPUT),
322 + NULL
323 +};
324 +
325 +static const struct hwmon_chip_info iei_wt61p803_puzzle_chip_info = {
326 + .ops = &iei_wt61p803_puzzle_hwmon_ops,
327 + .info = iei_wt61p803_puzzle_info,
328 +};
329 +
330 +static int iei_wt61p803_puzzle_get_max_state(struct thermal_cooling_device *tcdev,
331 + unsigned long *state)
332 +{
333 + struct iei_wt61p803_puzzle_thermal_cooling_device *cdev = tcdev->devdata;
334 +
335 + if (!cdev)
336 + return -EINVAL;
337 +
338 + *state = cdev->num_levels - 1;
339 + return 0;
340 +}
341 +
342 +static int iei_wt61p803_puzzle_get_cur_state(struct thermal_cooling_device *tcdev,
343 + unsigned long *state)
344 +{
345 + struct iei_wt61p803_puzzle_thermal_cooling_device *cdev = tcdev->devdata;
346 +
347 + if (!cdev)
348 + return -EINVAL;
349 +
350 + if (cdev->cur_level < 0)
351 + return -EAGAIN;
352 +
353 + *state = cdev->cur_level;
354 + return 0;
355 +}
356 +
357 +static int iei_wt61p803_puzzle_set_cur_state(struct thermal_cooling_device *tcdev,
358 + unsigned long state)
359 +{
360 + struct iei_wt61p803_puzzle_thermal_cooling_device *cdev = tcdev->devdata;
361 + u8 pwm_level;
362 +
363 + if (!cdev)
364 + return -EINVAL;
365 +
366 + if (state >= cdev->num_levels)
367 + return -EINVAL;
368 +
369 + if (state == cdev->cur_level)
370 + return 0;
371 +
372 + cdev->cur_level = state;
373 + pwm_level = cdev->cooling_levels[state];
374 +
375 + return iei_wt61p803_puzzle_write_pwm_channel(cdev->mcu_hwmon, cdev->pwm_channel, pwm_level);
376 +}
377 +
378 +static const struct thermal_cooling_device_ops iei_wt61p803_puzzle_cooling_ops = {
379 + .get_max_state = iei_wt61p803_puzzle_get_max_state,
380 + .get_cur_state = iei_wt61p803_puzzle_get_cur_state,
381 + .set_cur_state = iei_wt61p803_puzzle_set_cur_state,
382 +};
383 +
384 +static int
385 +iei_wt61p803_puzzle_enable_thermal_cooling_dev(struct device *dev,
386 + struct fwnode_handle *child,
387 + struct iei_wt61p803_puzzle_hwmon *mcu_hwmon)
388 +{
389 + struct iei_wt61p803_puzzle_thermal_cooling_device *cdev;
390 + u32 pwm_channel;
391 + u8 num_levels;
392 + int i, ret;
393 +
394 + ret = fwnode_property_read_u32(child, "reg", &pwm_channel);
395 + if (ret)
396 + return ret;
397 +
398 + mcu_hwmon->thermal_cooling_dev_present[pwm_channel] = true;
399 +
400 + num_levels = fwnode_property_count_u32(child, "cooling-levels");
401 + if (!num_levels)
402 + return -EINVAL;
403 +
404 + cdev = devm_kzalloc(dev, sizeof(*cdev), GFP_KERNEL);
405 + if (!cdev)
406 + return -ENOMEM;
407 +
408 + cdev->cooling_levels = devm_kmalloc_array(dev, num_levels, sizeof(u32), GFP_KERNEL);
409 + if (!cdev->cooling_levels)
410 + return -ENOMEM;
411 +
412 + ret = fwnode_property_read_u32_array(child, "cooling-levels",
413 + cdev->cooling_levels,
414 + num_levels);
415 + if (ret) {
416 + dev_err(dev, "Couldn't read property 'cooling-levels'\n");
417 + return ret;
418 + }
419 +
420 + for (i = 0; i < num_levels; i++) {
421 + if (cdev->cooling_levels[i] >
422 + IEI_WT61P803_PUZZLE_HWMON_MAX_PWM_VAL) {
423 + dev_err(dev, "iei_wt61p803_fan state[%d]:%d > %d\n", i,
424 + cdev->cooling_levels[i],
425 + IEI_WT61P803_PUZZLE_HWMON_MAX_PWM_VAL);
426 + return -EINVAL;
427 + }
428 + }
429 +
430 + cdev->mcu_hwmon = mcu_hwmon;
431 + cdev->pwm_channel = pwm_channel;
432 + cdev->num_levels = num_levels;
433 + cdev->cur_level = -1;
434 + mcu_hwmon->cdev[pwm_channel] = cdev;
435 +
436 + snprintf(cdev->name, THERMAL_NAME_LENGTH, "wt61p803_puzzle_%d", pwm_channel);
437 + cdev->tcdev = devm_thermal_of_cooling_device_register(dev, to_of_node(child), cdev->name,
438 + cdev, &iei_wt61p803_puzzle_cooling_ops);
439 + if (IS_ERR(cdev->tcdev))
440 + return PTR_ERR(cdev->tcdev);
441 +
442 + return 0;
443 +}
444 +
445 +static int iei_wt61p803_puzzle_hwmon_probe(struct platform_device *pdev)
446 +{
447 + struct device *dev = &pdev->dev;
448 + struct iei_wt61p803_puzzle *mcu = dev_get_drvdata(dev->parent);
449 + struct iei_wt61p803_puzzle_hwmon *mcu_hwmon;
450 + struct fwnode_handle *child;
451 + struct device *hwmon_dev;
452 + int ret;
453 +
454 + mcu_hwmon = devm_kzalloc(dev, sizeof(*mcu_hwmon), GFP_KERNEL);
455 + if (!mcu_hwmon)
456 + return -ENOMEM;
457 +
458 + mcu_hwmon->mcu = mcu;
459 + platform_set_drvdata(pdev, mcu_hwmon);
460 + mutex_init(&mcu_hwmon->lock);
461 +
462 + hwmon_dev = devm_hwmon_device_register_with_info(dev, "iei_wt61p803_puzzle",
463 + mcu_hwmon,
464 + &iei_wt61p803_puzzle_chip_info,
465 + NULL);
466 + if (IS_ERR(hwmon_dev))
467 + return PTR_ERR(hwmon_dev);
468 +
469 + /* Control fans via PWM lines via Linux Kernel */
470 + if (IS_ENABLED(CONFIG_THERMAL)) {
471 + device_for_each_child_node(dev, child) {
472 + ret = iei_wt61p803_puzzle_enable_thermal_cooling_dev(dev, child, mcu_hwmon);
473 + if (ret) {
474 + dev_err(dev, "Enabling the PWM fan failed\n");
475 + fwnode_handle_put(child);
476 + return ret;
477 + }
478 + }
479 + }
480 + return 0;
481 +}
482 +
483 +static const struct of_device_id iei_wt61p803_puzzle_hwmon_id_table[] = {
484 + { .compatible = "iei,wt61p803-puzzle-hwmon" },
485 + {}
486 +};
487 +MODULE_DEVICE_TABLE(of, iei_wt61p803_puzzle_hwmon_id_table);
488 +
489 +static struct platform_driver iei_wt61p803_puzzle_hwmon_driver = {
490 + .driver = {
491 + .name = "iei-wt61p803-puzzle-hwmon",
492 + .of_match_table = iei_wt61p803_puzzle_hwmon_id_table,
493 + },
494 + .probe = iei_wt61p803_puzzle_hwmon_probe,
495 +};
496 +
497 +module_platform_driver(iei_wt61p803_puzzle_hwmon_driver);
498 +
499 +MODULE_DESCRIPTION("IEI WT61P803 PUZZLE MCU HWMON Driver");
500 +MODULE_AUTHOR("Luka Kovacic <luka.kovacic@sartura.hr>");
501 +MODULE_LICENSE("GPL v2");