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 | 445 ++++++++++++++++++++++
24 3 files changed, 454 insertions(+)
25 create mode 100644 drivers/hwmon/iei-wt61p803-puzzle-hwmon.c
27 --- a/drivers/hwmon/Kconfig
28 +++ b/drivers/hwmon/Kconfig
29 @@ -755,6 +755,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 @@ -87,6 +87,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)
87 + * @cur_level: Current cooling level
88 + * @num_levels: Number of cooling levels
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];
95 + u32 *cooling_levels;
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
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 */
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)
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,
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,
136 + if (reply_size != 7) {
141 + /* Check the number of NTC values */
142 + if (resp_buf[3] != '2') {
147 + *value = raw_temp_to_milidegree_celsius(resp_buf[4 + channel]);
149 + mutex_unlock(&mcu_hwmon->lock);
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)
157 + unsigned char *resp_buf = mcu_hwmon->response_buffer;
158 + unsigned char fan_speed_cmd[4] = {};
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);
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,
173 + if (reply_size != 7) {
178 + *value = raw_fan_val_to_rpm(resp_buf[3], resp_buf[4]);
180 + mutex_unlock(&mcu_hwmon->lock);
184 +static int iei_wt61p803_puzzle_write_pwm_channel(struct iei_wt61p803_puzzle_hwmon *mcu_hwmon,
185 + int channel, long pwm_set_val)
187 + unsigned char *resp_buf = mcu_hwmon->response_buffer;
188 + unsigned char pwm_set_cmd[6] = {};
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;
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,
205 + if (reply_size != 3) {
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)) {
217 + mutex_unlock(&mcu_hwmon->lock);
221 +static int iei_wt61p803_puzzle_read_pwm_channel(struct iei_wt61p803_puzzle_hwmon *mcu_hwmon,
222 + int channel, long *value)
224 + unsigned char *resp_buf = mcu_hwmon->response_buffer;
225 + unsigned char pwm_get_cmd[5] = {};
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);
234 + ret = iei_wt61p803_puzzle_write_command(mcu_hwmon->mcu, pwm_get_cmd,
235 + sizeof(pwm_get_cmd), resp_buf,
240 + if (reply_size != 5)
243 + if (resp_buf[2] != IEI_WT61P803_PUZZLE_CMD_FAN_PWM_READ)
246 + *value = resp_buf[3];
251 +static int iei_wt61p803_puzzle_read(struct device *dev, enum hwmon_sensor_types type,
252 + u32 attr, int channel, long *val)
254 + struct iei_wt61p803_puzzle_hwmon *mcu_hwmon = dev_get_drvdata(dev->parent);
258 + return iei_wt61p803_puzzle_read_pwm_channel(mcu_hwmon, channel, val);
260 + return iei_wt61p803_puzzle_read_fan_speed(mcu_hwmon, channel, val);
262 + return iei_wt61p803_puzzle_read_temp_sensor(mcu_hwmon, channel, val);
268 +static int iei_wt61p803_puzzle_write(struct device *dev, enum hwmon_sensor_types type,
269 + u32 attr, int channel, long val)
271 + struct iei_wt61p803_puzzle_hwmon *mcu_hwmon = dev_get_drvdata(dev->parent);
273 + return iei_wt61p803_puzzle_write_pwm_channel(mcu_hwmon, channel, val);
276 +static umode_t iei_wt61p803_puzzle_is_visible(const void *data, enum hwmon_sensor_types type,
277 + u32 attr, int channel)
279 + const struct iei_wt61p803_puzzle_hwmon *mcu_hwmon = data;
283 + if (mcu_hwmon->thermal_cooling_dev_present[channel])
285 + if (attr == hwmon_pwm_input)
289 + if (attr == hwmon_fan_input)
293 + if (attr == hwmon_temp_input)
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,
309 +static const struct hwmon_channel_info *iei_wt61p803_puzzle_info[] = {
310 + HWMON_CHANNEL_INFO(pwm,
313 + HWMON_CHANNEL_INFO(fan,
319 + HWMON_CHANNEL_INFO(temp,
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,
330 +static int iei_wt61p803_puzzle_get_max_state(struct thermal_cooling_device *tcdev,
331 + unsigned long *state)
333 + struct iei_wt61p803_puzzle_thermal_cooling_device *cdev = tcdev->devdata;
338 + *state = cdev->num_levels - 1;
342 +static int iei_wt61p803_puzzle_get_cur_state(struct thermal_cooling_device *tcdev,
343 + unsigned long *state)
345 + struct iei_wt61p803_puzzle_thermal_cooling_device *cdev = tcdev->devdata;
350 + if (cdev->cur_level < 0)
353 + *state = cdev->cur_level;
357 +static int iei_wt61p803_puzzle_set_cur_state(struct thermal_cooling_device *tcdev,
358 + unsigned long state)
360 + struct iei_wt61p803_puzzle_thermal_cooling_device *cdev = tcdev->devdata;
366 + if (state >= cdev->num_levels)
369 + if (state == cdev->cur_level)
372 + cdev->cur_level = state;
373 + pwm_level = cdev->cooling_levels[state];
375 + return iei_wt61p803_puzzle_write_pwm_channel(cdev->mcu_hwmon, cdev->pwm_channel, pwm_level);
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,
385 +iei_wt61p803_puzzle_enable_thermal_cooling_dev(struct device *dev,
386 + struct fwnode_handle *child,
387 + struct iei_wt61p803_puzzle_hwmon *mcu_hwmon)
389 + struct iei_wt61p803_puzzle_thermal_cooling_device *cdev;
394 + ret = fwnode_property_read_u32(child, "reg", &pwm_channel);
398 + mcu_hwmon->thermal_cooling_dev_present[pwm_channel] = true;
400 + num_levels = fwnode_property_count_u32(child, "cooling-levels");
404 + cdev = devm_kzalloc(dev, sizeof(*cdev), GFP_KERNEL);
408 + cdev->cooling_levels = devm_kmalloc_array(dev, num_levels, sizeof(u32), GFP_KERNEL);
409 + if (!cdev->cooling_levels)
412 + ret = fwnode_property_read_u32_array(child, "cooling-levels",
413 + cdev->cooling_levels,
416 + dev_err(dev, "Couldn't read property 'cooling-levels'\n");
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);
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;
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);
445 +static int iei_wt61p803_puzzle_hwmon_probe(struct platform_device *pdev)
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;
454 + mcu_hwmon = devm_kzalloc(dev, sizeof(*mcu_hwmon), GFP_KERNEL);
458 + mcu_hwmon->mcu = mcu;
459 + platform_set_drvdata(pdev, mcu_hwmon);
460 + mutex_init(&mcu_hwmon->lock);
462 + hwmon_dev = devm_hwmon_device_register_with_info(dev, "iei_wt61p803_puzzle",
464 + &iei_wt61p803_puzzle_chip_info,
466 + if (IS_ERR(hwmon_dev))
467 + return PTR_ERR(hwmon_dev);
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);
474 + dev_err(dev, "Enabling the PWM fan failed\n");
475 + fwnode_handle_put(child);
483 +static const struct of_device_id iei_wt61p803_puzzle_hwmon_id_table[] = {
484 + { .compatible = "iei,wt61p803-puzzle-hwmon" },
487 +MODULE_DEVICE_TABLE(of, iei_wt61p803_puzzle_hwmon_id_table);
489 +static struct platform_driver iei_wt61p803_puzzle_hwmon_driver = {
491 + .name = "iei-wt61p803-puzzle-hwmon",
492 + .of_match_table = iei_wt61p803_puzzle_hwmon_id_table,
494 + .probe = iei_wt61p803_puzzle_hwmon_probe,
497 +module_platform_driver(iei_wt61p803_puzzle_hwmon_driver);
499 +MODULE_DESCRIPTION("IEI WT61P803 PUZZLE MCU HWMON Driver");
500 +MODULE_AUTHOR("Luka Kovacic <luka.kovacic@sartura.hr>");
501 +MODULE_LICENSE("GPL v2");