mvebu: refresh 5.15 patches
[openwrt/openwrt.git] / target / linux / mvebu / patches-5.15 / 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,413 @@
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 + */
88 +struct iei_wt61p803_puzzle_thermal_cooling_device {
89 + struct iei_wt61p803_puzzle_hwmon *mcu_hwmon;
90 + struct thermal_cooling_device *tcdev;
91 + char name[THERMAL_NAME_LENGTH];
92 + int pwm_channel;
93 + u8 *cooling_levels;
94 +};
95 +
96 +/**
97 + * struct iei_wt61p803_puzzle_hwmon - MCU HWMON Driver
98 + * @mcu: MCU struct pointer
99 + * @response_buffer Global MCU response buffer
100 + * @thermal_cooling_dev_present: Per-channel thermal cooling device control indicator
101 + * @cdev: Per-channel thermal cooling device private structure
102 + */
103 +struct iei_wt61p803_puzzle_hwmon {
104 + struct iei_wt61p803_puzzle *mcu;
105 + unsigned char response_buffer[IEI_WT61P803_PUZZLE_BUF_SIZE];
106 + bool thermal_cooling_dev_present[IEI_WT61P803_PUZZLE_HWMON_MAX_PWM];
107 + struct iei_wt61p803_puzzle_thermal_cooling_device
108 + *cdev[IEI_WT61P803_PUZZLE_HWMON_MAX_PWM];
109 + struct mutex lock; /* mutex to protect response_buffer array */
110 +};
111 +
112 +#define raw_temp_to_milidegree_celsius(x) (((x) - 0x80) * 1000)
113 +static int iei_wt61p803_puzzle_read_temp_sensor(struct iei_wt61p803_puzzle_hwmon *mcu_hwmon,
114 + int channel, long *value)
115 +{
116 + unsigned char *resp_buf = mcu_hwmon->response_buffer;
117 + unsigned char temp_sensor_ntc_cmd[4] = {
118 + IEI_WT61P803_PUZZLE_CMD_HEADER_START,
119 + IEI_WT61P803_PUZZLE_CMD_TEMP,
120 + IEI_WT61P803_PUZZLE_CMD_TEMP_ALL,
121 + };
122 + size_t reply_size;
123 + int ret;
124 +
125 + mutex_lock(&mcu_hwmon->lock);
126 + ret = iei_wt61p803_puzzle_write_command(mcu_hwmon->mcu, temp_sensor_ntc_cmd,
127 + sizeof(temp_sensor_ntc_cmd), resp_buf,
128 + &reply_size);
129 + if (ret)
130 + goto exit;
131 +
132 + if (reply_size != 7) {
133 + ret = -EIO;
134 + goto exit;
135 + }
136 +
137 + /* Check the number of NTC values */
138 + if (resp_buf[3] != '2') {
139 + ret = -EIO;
140 + goto exit;
141 + }
142 +
143 + *value = raw_temp_to_milidegree_celsius(resp_buf[4 + channel]);
144 +exit:
145 + mutex_unlock(&mcu_hwmon->lock);
146 + return ret;
147 +}
148 +
149 +#define raw_fan_val_to_rpm(x, y) ((((x) << 8 | (y)) / 2) * 60)
150 +static int iei_wt61p803_puzzle_read_fan_speed(struct iei_wt61p803_puzzle_hwmon *mcu_hwmon,
151 + int channel, long *value)
152 +{
153 + unsigned char *resp_buf = mcu_hwmon->response_buffer;
154 + unsigned char fan_speed_cmd[4] = {};
155 + size_t reply_size;
156 + int ret;
157 +
158 + fan_speed_cmd[0] = IEI_WT61P803_PUZZLE_CMD_HEADER_START;
159 + fan_speed_cmd[1] = IEI_WT61P803_PUZZLE_CMD_FAN;
160 + fan_speed_cmd[2] = IEI_WT61P803_PUZZLE_CMD_FAN_RPM(channel);
161 +
162 + mutex_lock(&mcu_hwmon->lock);
163 + ret = iei_wt61p803_puzzle_write_command(mcu_hwmon->mcu, fan_speed_cmd,
164 + sizeof(fan_speed_cmd), resp_buf,
165 + &reply_size);
166 + if (ret)
167 + goto exit;
168 +
169 + if (reply_size != 7) {
170 + ret = -EIO;
171 + goto exit;
172 + }
173 +
174 + *value = raw_fan_val_to_rpm(resp_buf[3], resp_buf[4]);
175 +exit:
176 + mutex_unlock(&mcu_hwmon->lock);
177 + return ret;
178 +}
179 +
180 +static int iei_wt61p803_puzzle_write_pwm_channel(struct iei_wt61p803_puzzle_hwmon *mcu_hwmon,
181 + int channel, long pwm_set_val)
182 +{
183 + unsigned char *resp_buf = mcu_hwmon->response_buffer;
184 + unsigned char pwm_set_cmd[6] = {};
185 + size_t reply_size;
186 + int ret;
187 +
188 + pwm_set_cmd[0] = IEI_WT61P803_PUZZLE_CMD_HEADER_START;
189 + pwm_set_cmd[1] = IEI_WT61P803_PUZZLE_CMD_FAN;
190 + pwm_set_cmd[2] = IEI_WT61P803_PUZZLE_CMD_FAN_PWM_WRITE;
191 + pwm_set_cmd[3] = IEI_WT61P803_PUZZLE_CMD_FAN_PWM(channel);
192 + pwm_set_cmd[4] = pwm_set_val;
193 +
194 + mutex_lock(&mcu_hwmon->lock);
195 + ret = iei_wt61p803_puzzle_write_command(mcu_hwmon->mcu, pwm_set_cmd,
196 + sizeof(pwm_set_cmd), resp_buf,
197 + &reply_size);
198 + if (ret)
199 + goto exit;
200 +
201 + if (reply_size != 3) {
202 + ret = -EIO;
203 + goto exit;
204 + }
205 +
206 + if (!(resp_buf[0] == IEI_WT61P803_PUZZLE_CMD_HEADER_START &&
207 + resp_buf[1] == IEI_WT61P803_PUZZLE_CMD_RESPONSE_OK &&
208 + resp_buf[2] == IEI_WT61P803_PUZZLE_CHECKSUM_RESPONSE_OK)) {
209 + ret = -EIO;
210 + goto exit;
211 + }
212 +exit:
213 + mutex_unlock(&mcu_hwmon->lock);
214 + return ret;
215 +}
216 +
217 +static int iei_wt61p803_puzzle_read_pwm_channel(struct iei_wt61p803_puzzle_hwmon *mcu_hwmon,
218 + int channel, long *value)
219 +{
220 + unsigned char *resp_buf = mcu_hwmon->response_buffer;
221 + unsigned char pwm_get_cmd[5] = {};
222 + size_t reply_size;
223 + int ret;
224 +
225 + pwm_get_cmd[0] = IEI_WT61P803_PUZZLE_CMD_HEADER_START;
226 + pwm_get_cmd[1] = IEI_WT61P803_PUZZLE_CMD_FAN;
227 + pwm_get_cmd[2] = IEI_WT61P803_PUZZLE_CMD_FAN_PWM_READ;
228 + pwm_get_cmd[3] = IEI_WT61P803_PUZZLE_CMD_FAN_PWM(channel);
229 +
230 + ret = iei_wt61p803_puzzle_write_command(mcu_hwmon->mcu, pwm_get_cmd,
231 + sizeof(pwm_get_cmd), resp_buf,
232 + &reply_size);
233 + if (ret)
234 + return ret;
235 +
236 + if (reply_size != 5)
237 + return -EIO;
238 +
239 + if (resp_buf[2] != IEI_WT61P803_PUZZLE_CMD_FAN_PWM_READ)
240 + return -EIO;
241 +
242 + *value = resp_buf[3];
243 +
244 + return 0;
245 +}
246 +
247 +static int iei_wt61p803_puzzle_read(struct device *dev, enum hwmon_sensor_types type,
248 + u32 attr, int channel, long *val)
249 +{
250 + struct iei_wt61p803_puzzle_hwmon *mcu_hwmon = dev_get_drvdata(dev->parent);
251 +
252 + switch (type) {
253 + case hwmon_pwm:
254 + return iei_wt61p803_puzzle_read_pwm_channel(mcu_hwmon, channel, val);
255 + case hwmon_fan:
256 + return iei_wt61p803_puzzle_read_fan_speed(mcu_hwmon, channel, val);
257 + case hwmon_temp:
258 + return iei_wt61p803_puzzle_read_temp_sensor(mcu_hwmon, channel, val);
259 + default:
260 + return -EINVAL;
261 + }
262 +}
263 +
264 +static int iei_wt61p803_puzzle_write(struct device *dev, enum hwmon_sensor_types type,
265 + u32 attr, int channel, long val)
266 +{
267 + struct iei_wt61p803_puzzle_hwmon *mcu_hwmon = dev_get_drvdata(dev->parent);
268 +
269 + return iei_wt61p803_puzzle_write_pwm_channel(mcu_hwmon, channel, val);
270 +}
271 +
272 +static umode_t iei_wt61p803_puzzle_is_visible(const void *data, enum hwmon_sensor_types type,
273 + u32 attr, int channel)
274 +{
275 + const struct iei_wt61p803_puzzle_hwmon *mcu_hwmon = data;
276 +
277 + switch (type) {
278 + case hwmon_pwm:
279 + if (mcu_hwmon->thermal_cooling_dev_present[channel])
280 + return 0444;
281 + if (attr == hwmon_pwm_input)
282 + return 0644;
283 + break;
284 + case hwmon_fan:
285 + if (attr == hwmon_fan_input)
286 + return 0444;
287 + break;
288 + case hwmon_temp:
289 + if (attr == hwmon_temp_input)
290 + return 0444;
291 + break;
292 + default:
293 + return 0;
294 + }
295 +
296 + return 0;
297 +}
298 +
299 +static const struct hwmon_ops iei_wt61p803_puzzle_hwmon_ops = {
300 + .is_visible = iei_wt61p803_puzzle_is_visible,
301 + .read = iei_wt61p803_puzzle_read,
302 + .write = iei_wt61p803_puzzle_write,
303 +};
304 +
305 +static const struct hwmon_channel_info *iei_wt61p803_puzzle_info[] = {
306 + HWMON_CHANNEL_INFO(pwm,
307 + HWMON_PWM_INPUT,
308 + HWMON_PWM_INPUT),
309 + HWMON_CHANNEL_INFO(fan,
310 + HWMON_F_INPUT,
311 + HWMON_F_INPUT,
312 + HWMON_F_INPUT,
313 + HWMON_F_INPUT,
314 + HWMON_F_INPUT),
315 + HWMON_CHANNEL_INFO(temp,
316 + HWMON_T_INPUT,
317 + HWMON_T_INPUT),
318 + NULL
319 +};
320 +
321 +static const struct hwmon_chip_info iei_wt61p803_puzzle_chip_info = {
322 + .ops = &iei_wt61p803_puzzle_hwmon_ops,
323 + .info = iei_wt61p803_puzzle_info,
324 +};
325 +
326 +static int iei_wt61p803_puzzle_get_max_state(struct thermal_cooling_device *tcdev,
327 + unsigned long *state)
328 +{
329 + *state = IEI_WT61P803_PUZZLE_HWMON_MAX_PWM_VAL;
330 +
331 + return 0;
332 +}
333 +
334 +static int iei_wt61p803_puzzle_get_cur_state(struct thermal_cooling_device *tcdev,
335 + unsigned long *state)
336 +{
337 + struct iei_wt61p803_puzzle_thermal_cooling_device *cdev = tcdev->devdata;
338 + struct iei_wt61p803_puzzle_hwmon *mcu_hwmon = cdev->mcu_hwmon;
339 + long value;
340 + int ret;
341 +
342 + ret = iei_wt61p803_puzzle_read_pwm_channel(mcu_hwmon, cdev->pwm_channel, &value);
343 + if (ret)
344 + return ret;
345 + *state = value;
346 + return 0;
347 +}
348 +
349 +static int iei_wt61p803_puzzle_set_cur_state(struct thermal_cooling_device *tcdev,
350 + unsigned long state)
351 +{
352 + struct iei_wt61p803_puzzle_thermal_cooling_device *cdev = tcdev->devdata;
353 + struct iei_wt61p803_puzzle_hwmon *mcu_hwmon = cdev->mcu_hwmon;
354 +
355 + return iei_wt61p803_puzzle_write_pwm_channel(mcu_hwmon, cdev->pwm_channel, state);
356 +}
357 +
358 +static const struct thermal_cooling_device_ops iei_wt61p803_puzzle_cooling_ops = {
359 + .get_max_state = iei_wt61p803_puzzle_get_max_state,
360 + .get_cur_state = iei_wt61p803_puzzle_get_cur_state,
361 + .set_cur_state = iei_wt61p803_puzzle_set_cur_state,
362 +};
363 +
364 +static int
365 +iei_wt61p803_puzzle_enable_thermal_cooling_dev(struct device *dev,
366 + struct fwnode_handle *child,
367 + struct iei_wt61p803_puzzle_hwmon *mcu_hwmon)
368 +{
369 + struct iei_wt61p803_puzzle_thermal_cooling_device *cdev;
370 + u32 pwm_channel;
371 + u8 num_levels;
372 + int ret;
373 +
374 + ret = fwnode_property_read_u32(child, "reg", &pwm_channel);
375 + if (ret)
376 + return ret;
377 +
378 + mcu_hwmon->thermal_cooling_dev_present[pwm_channel] = true;
379 +
380 + num_levels = fwnode_property_count_u8(child, "cooling-levels");
381 + if (!num_levels)
382 + return -EINVAL;
383 +
384 + cdev = devm_kzalloc(dev, sizeof(*cdev), GFP_KERNEL);
385 + if (!cdev)
386 + return -ENOMEM;
387 +
388 + cdev->cooling_levels = devm_kmalloc_array(dev, num_levels, sizeof(u8), GFP_KERNEL);
389 + if (!cdev->cooling_levels)
390 + return -ENOMEM;
391 +
392 + ret = fwnode_property_read_u8_array(child, "cooling-levels",
393 + cdev->cooling_levels,
394 + num_levels);
395 + if (ret) {
396 + dev_err(dev, "Couldn't read property 'cooling-levels'\n");
397 + return ret;
398 + }
399 +
400 + snprintf(cdev->name, THERMAL_NAME_LENGTH, "wt61p803_puzzle_%d", pwm_channel);
401 + cdev->tcdev = devm_thermal_of_cooling_device_register(dev, NULL, cdev->name, cdev,
402 + &iei_wt61p803_puzzle_cooling_ops);
403 + if (IS_ERR(cdev->tcdev))
404 + return PTR_ERR(cdev->tcdev);
405 +
406 + cdev->mcu_hwmon = mcu_hwmon;
407 + cdev->pwm_channel = pwm_channel;
408 + mcu_hwmon->cdev[pwm_channel] = cdev;
409 +
410 + return 0;
411 +}
412 +
413 +static int iei_wt61p803_puzzle_hwmon_probe(struct platform_device *pdev)
414 +{
415 + struct device *dev = &pdev->dev;
416 + struct iei_wt61p803_puzzle *mcu = dev_get_drvdata(dev->parent);
417 + struct iei_wt61p803_puzzle_hwmon *mcu_hwmon;
418 + struct fwnode_handle *child;
419 + struct device *hwmon_dev;
420 + int ret;
421 +
422 + mcu_hwmon = devm_kzalloc(dev, sizeof(*mcu_hwmon), GFP_KERNEL);
423 + if (!mcu_hwmon)
424 + return -ENOMEM;
425 +
426 + mcu_hwmon->mcu = mcu;
427 + platform_set_drvdata(pdev, mcu_hwmon);
428 + mutex_init(&mcu_hwmon->lock);
429 +
430 + hwmon_dev = devm_hwmon_device_register_with_info(dev, "iei_wt61p803_puzzle",
431 + mcu_hwmon,
432 + &iei_wt61p803_puzzle_chip_info,
433 + NULL);
434 + if (IS_ERR(hwmon_dev))
435 + return PTR_ERR(hwmon_dev);
436 +
437 + /* Control fans via PWM lines via Linux Kernel */
438 + if (IS_ENABLED(CONFIG_THERMAL)) {
439 + device_for_each_child_node(dev, child) {
440 + ret = iei_wt61p803_puzzle_enable_thermal_cooling_dev(dev, child, mcu_hwmon);
441 + if (ret) {
442 + dev_err(dev, "Enabling the PWM fan failed\n");
443 + fwnode_handle_put(child);
444 + return ret;
445 + }
446 + }
447 + }
448 + return 0;
449 +}
450 +
451 +static const struct of_device_id iei_wt61p803_puzzle_hwmon_id_table[] = {
452 + { .compatible = "iei,wt61p803-puzzle-hwmon" },
453 + {}
454 +};
455 +MODULE_DEVICE_TABLE(of, iei_wt61p803_puzzle_hwmon_id_table);
456 +
457 +static struct platform_driver iei_wt61p803_puzzle_hwmon_driver = {
458 + .driver = {
459 + .name = "iei-wt61p803-puzzle-hwmon",
460 + .of_match_table = iei_wt61p803_puzzle_hwmon_id_table,
461 + },
462 + .probe = iei_wt61p803_puzzle_hwmon_probe,
463 +};
464 +
465 +module_platform_driver(iei_wt61p803_puzzle_hwmon_driver);
466 +
467 +MODULE_DESCRIPTION("IEI WT61P803 PUZZLE MCU HWMON Driver");
468 +MODULE_AUTHOR("Luka Kovacic <luka.kovacic@sartura.hr>");
469 +MODULE_LICENSE("GPL v2");