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
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
10 The driver registers a HWMON device and a simple thermal cooling device to
11 enable in-kernel fan management.
13 This driver depends on the IEI WT61P803 PUZZLE MFD driver.
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>
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
27 --- a/drivers/hwmon/Kconfig
28 +++ b/drivers/hwmon/Kconfig
29 @@ -722,6 +722,14 @@ config SENSORS_IBMPOWERNV
30 This driver can also be built as a module. If so, the module
31 will be called ibmpowernv.
33 +config SENSORS_IEI_WT61P803_PUZZLE_HWMON
34 + tristate "IEI WT61P803 PUZZLE MFD HWMON Driver"
35 + depends on MFD_IEI_WT61P803_PUZZLE
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.
41 config SENSORS_IIO_HWMON
42 tristate "Hwmon driver that uses channels specified via iio maps"
44 --- a/drivers/hwmon/Makefile
45 +++ b/drivers/hwmon/Makefile
46 @@ -83,6 +83,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
55 +++ b/drivers/hwmon/iei-wt61p803-puzzle-hwmon.c
57 +// SPDX-License-Identifier: GPL-2.0-only
58 +/* IEI WT61P803 PUZZLE MCU HWMON Driver
60 + * Copyright (C) 2020 Sartura Ltd.
61 + * Author: Luka Kovacic <luka.kovacic@sartura.hr>
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>
77 +#define IEI_WT61P803_PUZZLE_HWMON_MAX_PWM 2
78 +#define IEI_WT61P803_PUZZLE_HWMON_MAX_PWM_VAL 255
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)
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];
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
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 */
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)
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,
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,
132 + if (reply_size != 7) {
137 + /* Check the number of NTC values */
138 + if (resp_buf[3] != '2') {
143 + *value = raw_temp_to_milidegree_celsius(resp_buf[4 + channel]);
145 + mutex_unlock(&mcu_hwmon->lock);
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)
153 + unsigned char *resp_buf = mcu_hwmon->response_buffer;
154 + unsigned char fan_speed_cmd[4] = {};
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);
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,
169 + if (reply_size != 7) {
174 + *value = raw_fan_val_to_rpm(resp_buf[3], resp_buf[4]);
176 + mutex_unlock(&mcu_hwmon->lock);
180 +static int iei_wt61p803_puzzle_write_pwm_channel(struct iei_wt61p803_puzzle_hwmon *mcu_hwmon,
181 + int channel, long pwm_set_val)
183 + unsigned char *resp_buf = mcu_hwmon->response_buffer;
184 + unsigned char pwm_set_cmd[6] = {};
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;
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,
201 + if (reply_size != 3) {
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)) {
213 + mutex_unlock(&mcu_hwmon->lock);
217 +static int iei_wt61p803_puzzle_read_pwm_channel(struct iei_wt61p803_puzzle_hwmon *mcu_hwmon,
218 + int channel, long *value)
220 + unsigned char *resp_buf = mcu_hwmon->response_buffer;
221 + unsigned char pwm_get_cmd[5] = {};
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);
230 + ret = iei_wt61p803_puzzle_write_command(mcu_hwmon->mcu, pwm_get_cmd,
231 + sizeof(pwm_get_cmd), resp_buf,
236 + if (reply_size != 5)
239 + if (resp_buf[2] != IEI_WT61P803_PUZZLE_CMD_FAN_PWM_READ)
242 + *value = resp_buf[3];
247 +static int iei_wt61p803_puzzle_read(struct device *dev, enum hwmon_sensor_types type,
248 + u32 attr, int channel, long *val)
250 + struct iei_wt61p803_puzzle_hwmon *mcu_hwmon = dev_get_drvdata(dev->parent);
254 + return iei_wt61p803_puzzle_read_pwm_channel(mcu_hwmon, channel, val);
256 + return iei_wt61p803_puzzle_read_fan_speed(mcu_hwmon, channel, val);
258 + return iei_wt61p803_puzzle_read_temp_sensor(mcu_hwmon, channel, val);
264 +static int iei_wt61p803_puzzle_write(struct device *dev, enum hwmon_sensor_types type,
265 + u32 attr, int channel, long val)
267 + struct iei_wt61p803_puzzle_hwmon *mcu_hwmon = dev_get_drvdata(dev->parent);
269 + return iei_wt61p803_puzzle_write_pwm_channel(mcu_hwmon, channel, val);
272 +static umode_t iei_wt61p803_puzzle_is_visible(const void *data, enum hwmon_sensor_types type,
273 + u32 attr, int channel)
275 + const struct iei_wt61p803_puzzle_hwmon *mcu_hwmon = data;
279 + if (mcu_hwmon->thermal_cooling_dev_present[channel])
281 + if (attr == hwmon_pwm_input)
285 + if (attr == hwmon_fan_input)
289 + if (attr == hwmon_temp_input)
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,
305 +static const struct hwmon_channel_info *iei_wt61p803_puzzle_info[] = {
306 + HWMON_CHANNEL_INFO(pwm,
309 + HWMON_CHANNEL_INFO(fan,
315 + HWMON_CHANNEL_INFO(temp,
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,
326 +static int iei_wt61p803_puzzle_get_max_state(struct thermal_cooling_device *tcdev,
327 + unsigned long *state)
329 + *state = IEI_WT61P803_PUZZLE_HWMON_MAX_PWM_VAL;
334 +static int iei_wt61p803_puzzle_get_cur_state(struct thermal_cooling_device *tcdev,
335 + unsigned long *state)
337 + struct iei_wt61p803_puzzle_thermal_cooling_device *cdev = tcdev->devdata;
338 + struct iei_wt61p803_puzzle_hwmon *mcu_hwmon = cdev->mcu_hwmon;
342 + ret = iei_wt61p803_puzzle_read_pwm_channel(mcu_hwmon, cdev->pwm_channel, &value);
349 +static int iei_wt61p803_puzzle_set_cur_state(struct thermal_cooling_device *tcdev,
350 + unsigned long state)
352 + struct iei_wt61p803_puzzle_thermal_cooling_device *cdev = tcdev->devdata;
353 + struct iei_wt61p803_puzzle_hwmon *mcu_hwmon = cdev->mcu_hwmon;
355 + return iei_wt61p803_puzzle_write_pwm_channel(mcu_hwmon, cdev->pwm_channel, state);
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,
365 +iei_wt61p803_puzzle_enable_thermal_cooling_dev(struct device *dev,
366 + struct fwnode_handle *child,
367 + struct iei_wt61p803_puzzle_hwmon *mcu_hwmon)
369 + struct iei_wt61p803_puzzle_thermal_cooling_device *cdev;
374 + ret = fwnode_property_read_u32(child, "reg", &pwm_channel);
378 + mcu_hwmon->thermal_cooling_dev_present[pwm_channel] = true;
380 + num_levels = fwnode_property_count_u8(child, "cooling-levels");
384 + cdev = devm_kzalloc(dev, sizeof(*cdev), GFP_KERNEL);
388 + cdev->cooling_levels = devm_kmalloc_array(dev, num_levels, sizeof(u8), GFP_KERNEL);
389 + if (!cdev->cooling_levels)
392 + ret = fwnode_property_read_u8_array(child, "cooling-levels",
393 + cdev->cooling_levels,
396 + dev_err(dev, "Couldn't read property 'cooling-levels'\n");
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);
406 + cdev->mcu_hwmon = mcu_hwmon;
407 + cdev->pwm_channel = pwm_channel;
408 + mcu_hwmon->cdev[pwm_channel] = cdev;
413 +static int iei_wt61p803_puzzle_hwmon_probe(struct platform_device *pdev)
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;
422 + mcu_hwmon = devm_kzalloc(dev, sizeof(*mcu_hwmon), GFP_KERNEL);
426 + mcu_hwmon->mcu = mcu;
427 + platform_set_drvdata(pdev, mcu_hwmon);
428 + mutex_init(&mcu_hwmon->lock);
430 + hwmon_dev = devm_hwmon_device_register_with_info(dev, "iei_wt61p803_puzzle",
432 + &iei_wt61p803_puzzle_chip_info,
434 + if (IS_ERR(hwmon_dev))
435 + return PTR_ERR(hwmon_dev);
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);
442 + dev_err(dev, "Enabling the PWM fan failed\n");
443 + fwnode_handle_put(child);
451 +static const struct of_device_id iei_wt61p803_puzzle_hwmon_id_table[] = {
452 + { .compatible = "iei,wt61p803-puzzle-hwmon" },
455 +MODULE_DEVICE_TABLE(of, iei_wt61p803_puzzle_hwmon_id_table);
457 +static struct platform_driver iei_wt61p803_puzzle_hwmon_driver = {
459 + .name = "iei-wt61p803-puzzle-hwmon",
460 + .of_match_table = iei_wt61p803_puzzle_hwmon_id_table,
462 + .probe = iei_wt61p803_puzzle_hwmon_probe,
465 +module_platform_driver(iei_wt61p803_puzzle_hwmon_driver);
467 +MODULE_DESCRIPTION("IEI WT61P803 PUZZLE MCU HWMON Driver");
468 +MODULE_AUTHOR("Luka Kovacic <luka.kovacic@sartura.hr>");
469 +MODULE_LICENSE("GPL v2");