1 From 23395c6df3c8ef7b6e43b4968de402169ec7c755 Mon Sep 17 00:00:00 2001
2 From: Michael Shych <michaelsh@nvidia.com>
3 Date: Sat, 30 Apr 2022 14:49:03 +0300
4 Subject: [PATCH] hwmon: (emc2305) add support for EMC2301/2/3/5
5 RPM-based PWM Fan Speed Controller.
7 Submitted to linux-hwmon mailing list at
8 https://patchwork.kernel.org/project/linux-hwmon/patch/20220430114905.53448-2-michaelsh@nvidia.com/
10 Add driver for Microchip EMC2301/2/3/5 RPM-based PWM Fan Speed Controller.
11 Modify Makefile and Kconfig to support Microchip EMC2305 RPM-based
12 PWM Fan Speed Controller.
14 Signed-off-by: Michael Shych <michaelsh@nvidia.com>
15 Reviewed-by: Vadim Pasternak <vadimp@nvidia.com>
17 drivers/hwmon/Kconfig | 13 +
18 drivers/hwmon/Makefile | 1 +
19 drivers/hwmon/emc2305.c | 629 ++++++++++++++++++++++++++++++++++++++++
20 3 files changed, 643 insertions(+)
21 create mode 100644 drivers/hwmon/emc2305.c
23 --- a/drivers/hwmon/Kconfig
24 +++ b/drivers/hwmon/Kconfig
25 @@ -1685,6 +1685,19 @@ config SENSORS_EMC2103
26 This driver can also be built as a module. If so, the module
27 will be called emc2103.
29 +config SENSORS_EMC2305
30 + tristate "Microchip EMC2305 and compatible EMC2301/2/3"
34 + If you say yes here you get support for the Microchip EMC2305
35 + fan controller chips.
36 + The Microchip EMC2305 is a fan controller for up to 5 fans.
37 + Fan rotation speeds are reported in RPM.
39 + This driver can also be built as a module. If so, the module
40 + will be called emc2305.
42 config SENSORS_EMC6W201
43 tristate "SMSC EMC6W201"
45 --- a/drivers/hwmon/Makefile
46 +++ b/drivers/hwmon/Makefile
47 @@ -67,6 +67,7 @@ obj-$(CONFIG_SENSORS_DS620) += ds620.o
48 obj-$(CONFIG_SENSORS_DS1621) += ds1621.o
49 obj-$(CONFIG_SENSORS_EMC1403) += emc1403.o
50 obj-$(CONFIG_SENSORS_EMC2103) += emc2103.o
51 +obj-$(CONFIG_SENSORS_EMC2305) += emc2305.o
52 obj-$(CONFIG_SENSORS_EMC6W201) += emc6w201.o
53 obj-$(CONFIG_SENSORS_F71805F) += f71805f.o
54 obj-$(CONFIG_SENSORS_F71882FG) += f71882fg.o
56 +++ b/drivers/hwmon/emc2305.c
58 +// SPDX-License-Identifier: GPL-2.0+
60 + * Hardware monitoring driver for EMC2305 fan controller
62 + * Copyright (C) 2022 Nvidia Technologies Ltd and Delta Networks, Inc.
65 +#include <linux/err.h>
66 +#include <linux/hwmon.h>
67 +#include <linux/hwmon-sysfs.h>
68 +#include <linux/i2c.h>
69 +#include <linux/module.h>
70 +#include <linux/of.h>
71 +#include <linux/thermal.h>
72 +#include <linux/version.h>
74 +static const unsigned short
75 +emc2305_normal_i2c[] = { 0x27, 0x2c, 0x2d, 0x2e, 0x2f, 0x4c, 0x4d, I2C_CLIENT_END };
77 +#define EMC2305_REG_DRIVE_FAIL_STATUS 0x27
78 +#define EMC2305_REG_DEVICE 0xfd
79 +#define EMC2305_REG_VENDOR 0xfe
80 +#define EMC2305_FAN_MAX 0xff
81 +#define EMC2305_FAN_MIN 0x00
82 +#define EMC2305_FAN_MAX_STATE 10
83 +#define EMC2305_DEVICE 0x34
84 +#define EMC2305_VENDOR 0x5d
85 +#define EMC2305_REG_PRODUCT_ID 0xfd
86 +#define EMC2305_TACH_REGS_UNUSE_BITS 3
87 +#define EMC2305_TACH_CNT_MULTIPLIER 0x02
88 +#define EMC2305_PWM_MAX 5
89 +#define EMC2305_PWM_CHNL_CMN 0
91 +#define EMC2305_PWM_DUTY2STATE(duty, max_state, pwm_max) \
92 + (DIV_ROUND_CLOSEST((duty) * (max_state), (pwm_max)))
93 +#define EMC2305_PWM_STATE2DUTY(state, max_state, pwm_max) \
94 + (DIV_ROUND_CLOSEST((state) * (pwm_max), (max_state)))
96 +/* Factor by equations [2] and [3] from data sheet; valid for fans where the number of edges
97 + * equal (poles * 2 + 1).
99 +#define EMC2305_RPM_FACTOR 3932160
101 +#define EMC2305_REG_FAN_DRIVE(n) (0x30 + 0x10 * (n))
102 +#define EMC2305_REG_FAN_MIN_DRIVE(n) (0x38 + 0x10 * (n))
103 +#define EMC2305_REG_FAN_TACH(n) (0x3e + 0x10 * (n))
105 +enum emc230x_product_id {
112 +static const struct i2c_device_id emc2305_ids[] = {
119 +MODULE_DEVICE_TABLE(i2c, emc2305_ids);
121 +static const struct of_device_id emc2305_dt_ids[] = {
122 + { .compatible = "smsc,emc2305" },
125 +MODULE_DEVICE_TABLE(of, emc2305_dt_ids);
128 + * @cdev: cooling device;
129 + * @curr_state: cooling current state;
130 + * @last_hwmon_state: last cooling state updated by hwmon subsystem;
131 + * @last_thermal_state: last cooling state updated by thermal subsystem;
133 + * The 'last_hwmon_state' and 'last_thermal_state' fields are provided to support fan low limit
134 + * speed feature. The purpose of this feature is to provides ability to limit fan speed
135 + * according to some system wise considerations, like absence of some replaceable units (PSU or
136 + * line cards), high system ambient temperature, unreliable transceivers temperature sensing or
137 + * some other factors which indirectly impacts system's airflow
138 + * Fan low limit feature is supported through 'hwmon' interface: 'hwmon' 'pwm' attribute is
139 + * used for setting low limit for fan speed in case 'thermal' subsystem is configured in
140 + * kernel. In this case setting fan speed through 'hwmon' will never let the 'thermal'
141 + * subsystem to select a lower duty cycle than the duty cycle selected with the 'pwm'
143 + * From other side, fan speed is to be updated in hardware through 'pwm' only in case the
144 + * requested fan speed is above last speed set by 'thermal' subsystem, otherwise requested fan
145 + * speed will be just stored with no PWM update.
147 +struct emc2305_cdev_data {
148 + struct thermal_cooling_device *cdev;
149 + unsigned int cur_state;
150 + unsigned long last_hwmon_state;
151 + unsigned long last_thermal_state;
155 + * @client: i2c client;
156 + * @hwmon_dev: hwmon device;
157 + * @max_state: maximum cooling state of the cooling device;
158 + * @pwm_max: maximum PWM;
159 + * @pwm_min: minimum PWM;
160 + * @pwm_channel: maximum number of PWM channels;
161 + * @cdev_data: array of cooling devices data;
163 +struct emc2305_data {
164 + struct i2c_client *client;
165 + struct device *hwmon_dev;
171 + struct emc2305_cdev_data cdev_data[EMC2305_PWM_MAX];
174 +static char *emc2305_fan_name[] = {
183 +static int emc2305_get_max_channel(struct emc2305_data *data)
185 + if (data->pwm_channel == EMC2305_PWM_CHNL_CMN)
186 + return data->pwm_num;
188 + return data->pwm_channel;
191 +static int emc2305_get_cdev_idx(struct thermal_cooling_device *cdev)
193 + struct emc2305_data *data = cdev->devdata;
194 + size_t len = strlen(cdev->type);
200 + /* Retuns index of cooling device 0..4 in case of separate PWM setting.
201 + * Zero index is used in case of one common PWM setting.
202 + * If the mode is set as EMC2305_PWM_CHNL_CMN, all PWMs are to be bound
203 + * to the common thermal zone and should work at the same speed
204 + * to perform cooling for the same thermal junction.
205 + * Otherwise, return specific channel that will be used in bound
206 + * related PWM to the thermal zone.
208 + if (data->pwm_channel == EMC2305_PWM_CHNL_CMN)
211 + ret = cdev->type[len - 1];
221 +static int emc2305_get_cur_state(struct thermal_cooling_device *cdev, unsigned long *state)
224 + struct emc2305_data *data = cdev->devdata;
226 + cdev_idx = emc2305_get_cdev_idx(cdev);
230 + *state = data->cdev_data[cdev_idx].cur_state;
234 +static int emc2305_get_max_state(struct thermal_cooling_device *cdev, unsigned long *state)
236 + struct emc2305_data *data = cdev->devdata;
237 + *state = data->max_state;
241 +static int emc2305_set_cur_state(struct thermal_cooling_device *cdev, unsigned long state)
244 + struct emc2305_data *data = cdev->devdata;
245 + struct i2c_client *client = data->client;
248 + if (state > data->max_state)
251 + cdev_idx = emc2305_get_cdev_idx(cdev);
255 + /* Save thermal state. */
256 + data->cdev_data[cdev_idx].last_thermal_state = state;
257 + state = max_t(unsigned long, state, data->cdev_data[cdev_idx].last_hwmon_state);
259 + val = EMC2305_PWM_STATE2DUTY(state, data->max_state, data->pwm_max);
260 + if (val > EMC2305_FAN_MAX)
263 + data->cdev_data[cdev_idx].cur_state = state;
264 + if (data->pwm_channel == EMC2305_PWM_CHNL_CMN)
265 + /* Set the same PWM value in all channels
266 + * if common PWM channel is used.
268 + for (i = 0; i < data->pwm_num; i++)
269 + i2c_smbus_write_byte_data(client, EMC2305_REG_FAN_DRIVE(i), val);
271 + i2c_smbus_write_byte_data(client, EMC2305_REG_FAN_DRIVE(cdev_idx), val);
276 +static const struct thermal_cooling_device_ops emc2305_cooling_ops = {
277 + .get_max_state = emc2305_get_max_state,
278 + .get_cur_state = emc2305_get_cur_state,
279 + .set_cur_state = emc2305_set_cur_state,
282 +static int emc2305_show_fault(struct device *dev, int channel)
284 + struct emc2305_data *data = dev_get_drvdata(dev);
285 + struct i2c_client *client = data->client;
288 + status_reg = i2c_smbus_read_byte_data(client, EMC2305_REG_DRIVE_FAIL_STATUS);
289 + return status_reg & (1 << channel) ? 1 : 0;
292 +static int emc2305_show_fan(struct device *dev, int channel)
294 + struct emc2305_data *data = dev_get_drvdata(dev);
295 + struct i2c_client *client = data->client;
298 + ret = i2c_smbus_read_word_swapped(client, EMC2305_REG_FAN_TACH(channel));
302 + ret = ret >> EMC2305_TACH_REGS_UNUSE_BITS;
303 + return EMC2305_RPM_FACTOR * EMC2305_TACH_CNT_MULTIPLIER / (ret > 0 ? ret : 1);
306 +static int emc2305_show_pwm(struct device *dev, int channel)
308 + struct emc2305_data *data = dev_get_drvdata(dev);
309 + struct i2c_client *client = data->client;
311 + return i2c_smbus_read_byte_data(client, EMC2305_REG_FAN_DRIVE(channel));
314 +static int emc2305_set_pwm(struct device *dev, long val, int channel)
316 + struct emc2305_data *data = dev_get_drvdata(dev);
317 + struct i2c_client *client = data->client;
319 + if (val < data->pwm_min || val > data->pwm_max)
322 + i2c_smbus_write_byte_data(client, EMC2305_REG_FAN_DRIVE(channel), val);
323 + data->cdev_data[channel].cur_state = EMC2305_PWM_DUTY2STATE(val, data->max_state,
328 +static int emc2305_get_tz_of(struct device *dev)
330 + struct device_node *np = dev->of_node;
331 + struct emc2305_data *data = dev_get_drvdata(dev);
334 + /* OF parameters are optional - overwrite default setting
335 + * if some of them are provided.
338 + if (of_find_property(np, "emc2305,cooling-levels", NULL)) {
339 + ret = of_property_read_u8(np, "emc2305,cooling-levels", &data->max_state);
344 + if (of_find_property(np, "emc2305,pwm-max", NULL)) {
345 + ret = of_property_read_u8(np, "emc2305,pwm-max", &data->pwm_max);
350 + if (of_find_property(np, "emc2305,pwm-min", NULL)) {
351 + ret = of_property_read_u8(np, "emc2305,pwm-min", &data->pwm_min);
356 + /* Not defined or 0 means one thermal zone over all colling devices.
357 + * Otherwise - separted thermal zones for each PWM channel.
359 + if (of_find_property(np, "emc2305,pwm-channel", NULL)) {
360 + ret = of_property_read_u8(np, "emc2305,pwm-channel", &data->pwm_channel);
368 +static int emc2305_set_single_tz(struct device *dev, int idx)
370 + struct emc2305_data *data = dev_get_drvdata(dev);
371 + long pwm = data->pwm_max;
374 + cdev_idx = (idx) ? idx - 1 : 0;
377 + data->cdev_data[cdev_idx].cdev =
378 + devm_thermal_of_cooling_device_register(dev, dev->of_node,
379 + emc2305_fan_name[idx], data,
380 + &emc2305_cooling_ops);
382 + data->cdev_data[cdev_idx].cdev =
383 + thermal_cooling_device_register(emc2305_fan_name[idx], data,
384 + &emc2305_cooling_ops);
386 + if (IS_ERR(data->cdev_data[cdev_idx].cdev)) {
387 + dev_err(dev, "Failed to register cooling device %s\n", emc2305_fan_name[idx]);
388 + return PTR_ERR(data->cdev_data[cdev_idx].cdev);
390 + emc2305_set_pwm(dev, pwm, cdev_idx);
391 + data->cdev_data[cdev_idx].cur_state = data->max_state;
392 + /* Set minimal PWM speed. */
393 + data->cdev_data[cdev_idx].last_hwmon_state = EMC2305_PWM_DUTY2STATE(data->pwm_min,
399 +static int emc2305_set_tz(struct device *dev)
401 + struct emc2305_data *data = dev_get_drvdata(dev);
404 + if (data->pwm_channel == EMC2305_PWM_CHNL_CMN)
405 + return emc2305_set_single_tz(dev, 0);
407 + for (i = 0; i < data->pwm_channel; i++) {
408 + ret = emc2305_set_single_tz(dev, i + 1);
410 + goto thermal_cooling_device_register_fail;
414 +thermal_cooling_device_register_fail:
415 + emc2305_unset_tz(dev);
419 +static void emc2305_unset_tz(struct device *dev)
421 + struct emc2305_data *data = dev_get_drvdata(dev);
424 + /* Unregister cooling device in case they have been registered by
425 + * thermal_cooling_device_unregister(). No need for clean-up flow in case they
426 + * have been registered by devm_thermal_of_cooling_device_register()
428 + if (!dev->of_node) {
429 + for (i = 0; i < EMC2305_PWM_MAX; i++)
430 + if (data->cdev_data[i].cdev)
431 + thermal_cooling_device_unregister(data->cdev_data[i].cdev);
436 +emc2305_is_visible(const void *data, enum hwmon_sensor_types type, u32 attr, int channel)
438 + int max_channel = emc2305_get_max_channel((struct emc2305_data *)data);
440 + /* Don't show channels which are not physically connected. */
441 + if ((channel + 1) > max_channel)
446 + case hwmon_fan_input:
448 + case hwmon_fan_fault:
456 + case hwmon_pwm_input:
470 +emc2305_write(struct device *dev, enum hwmon_sensor_types type, u32 attr, int channel, long val)
472 + struct emc2305_data *data = dev_get_drvdata(dev);
477 + case hwmon_pwm_input:
478 + /* If thermal is configured - handle PWM limit setting. */
479 + if (IS_REACHABLE(CONFIG_THERMAL)) {
480 + data->cdev_data[channel].last_hwmon_state =
481 + EMC2305_PWM_DUTY2STATE(val, data->max_state, data->pwm_max);
482 + /* Update PWM only in case requested state is not less than the
483 + * last thermal state.
485 + if (data->cdev_data[channel].last_hwmon_state >=
486 + data->cdev_data[channel].last_thermal_state)
487 + return emc2305_set_cur_state(data->cdev_data[channel].cdev,
488 + data->cdev_data[channel].last_hwmon_state);
491 + return emc2305_set_pwm(dev, val, channel);
500 + return -EOPNOTSUPP;
504 +emc2305_read(struct device *dev, enum hwmon_sensor_types type, u32 attr, int channel, long *val)
511 + case hwmon_fan_input:
512 + ret = emc2305_show_fan(dev, channel);
517 + case hwmon_fan_fault:
518 + ret = emc2305_show_fault(dev, channel);
529 + case hwmon_pwm_input:
530 + ret = emc2305_show_pwm(dev, channel);
543 + return -EOPNOTSUPP;
546 +static const struct hwmon_ops emc2305_ops = {
547 + .is_visible = emc2305_is_visible,
548 + .read = emc2305_read,
549 + .write = emc2305_write,
552 +static const struct hwmon_channel_info *emc2305_info[] = {
553 + HWMON_CHANNEL_INFO(fan,
554 + HWMON_F_INPUT | HWMON_F_FAULT,
555 + HWMON_F_INPUT | HWMON_F_FAULT,
556 + HWMON_F_INPUT | HWMON_F_FAULT,
557 + HWMON_F_INPUT | HWMON_F_FAULT,
558 + HWMON_F_INPUT | HWMON_F_FAULT),
559 + HWMON_CHANNEL_INFO(pwm,
568 +static const struct hwmon_chip_info emc2305_chip_info = {
569 + .ops = &emc2305_ops,
570 + .info = emc2305_info,
573 +static int emc2305_identify(struct device *dev)
575 + struct i2c_client *client = to_i2c_client(dev);
576 + struct emc2305_data *data = i2c_get_clientdata(client);
579 + ret = i2c_smbus_read_byte_data(client, EMC2305_REG_PRODUCT_ID);
603 +static int emc2305_probe(struct i2c_client *client, const struct i2c_device_id *id)
605 + struct i2c_adapter *adapter = client->adapter;
606 + struct device *dev = &client->dev;
607 + struct emc2305_data *data;
608 + int vendor, device;
612 + if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA))
615 + vendor = i2c_smbus_read_byte_data(client, EMC2305_REG_VENDOR);
616 + if (vendor != EMC2305_VENDOR)
619 + device = i2c_smbus_read_byte_data(client, EMC2305_REG_DEVICE);
620 + if (device != EMC2305_DEVICE)
623 + data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
627 + i2c_set_clientdata(client, data);
628 + data->client = client;
630 + ret = emc2305_identify(dev);
634 + data->max_state = EMC2305_FAN_MAX_STATE;
635 + data->pwm_max = EMC2305_FAN_MAX;
636 + data->pwm_min = EMC2305_FAN_MIN;
637 + data->pwm_channel = EMC2305_PWM_CHNL_CMN;
638 + if (dev->of_node) {
639 + ret = emc2305_get_tz_of(dev);
644 + data->hwmon_dev = devm_hwmon_device_register_with_info(dev, "emc2305", data,
645 + &emc2305_chip_info, NULL);
646 + if (IS_ERR(data->hwmon_dev))
647 + return PTR_ERR(data->hwmon_dev);
649 + if (IS_REACHABLE(CONFIG_THERMAL)) {
650 + ret = emc2305_set_tz(dev);
655 + for (i = 0; i < data->pwm_num; i++)
656 + i2c_smbus_write_byte_data(client, EMC2305_REG_FAN_MIN_DRIVE(i), data->pwm_min);
661 +static int emc2305_remove(struct i2c_client *client)
663 + struct device *dev = &client->dev;
665 + if (IS_REACHABLE(CONFIG_THERMAL))
666 + emc2305_unset_tz(dev);
670 +static struct i2c_driver emc2305_driver = {
671 + .class = I2C_CLASS_HWMON,
674 + .of_match_table = emc2305_dt_ids,
676 + .probe = emc2305_probe,
677 + .remove = emc2305_remove,
678 + .id_table = emc2305_ids,
679 + .address_list = emc2305_normal_i2c,
682 +module_i2c_driver(emc2305_driver);
684 +MODULE_AUTHOR("Nvidia");
685 +MODULE_DESCRIPTION("Microchip EMC2305 fan controller driver");
686 +MODULE_LICENSE("GPL");