1 // SPDX-License-Identifier: GPL-2.0+
3 * Hardware monitoring driver for Microchip EMC2305 fan controller
5 * Copyright (C) 2022 Nvidia Technologies Ltd and Delta Networks, Inc.
9 #include <linux/hwmon.h>
10 #include <linux/hwmon-sysfs.h>
11 #include <linux/i2c.h>
12 #include <linux/module.h>
14 #include <linux/thermal.h>
15 #include <linux/version.h>
17 static const unsigned short
18 emc2305_normal_i2c[] = { 0x27, 0x2c, 0x2d, 0x2e, 0x2f, 0x4c, 0x4d, I2C_CLIENT_END };
20 #define EMC2305_REG_FAN_STATUS 0x24
21 #define EMC2305_REG_FAN_STALL_STATUS 0x25
22 #define EMC2305_REG_DRIVE_FAIL_STATUS 0x27
23 #define EMC2305_REG_VENDOR 0xfe
24 #define EMC2305_FAN_MAX 0xff
25 #define EMC2305_FAN_MIN 0x00
26 #define EMC2305_FAN_MAX_STATE 10
27 #define EMC2305_VENDOR 0x5d
28 #define EMC2305_REG_PRODUCT_ID 0xfd
29 #define EMC2305_TACH_REGS_UNUSE_BITS 3
30 #define EMC2305_TACH_CNT_MULTIPLIER 1 /* Set by FAN_CFG RNGx bits */
31 #define EMC2305_PWM_MAX 5
32 #define EMC2305_PWM_CHNL_CMN 0
34 #define EMC2305_PWM_DUTY2STATE(duty, max_state, pwm_max) \
35 (DIV_ROUND_CLOSEST((duty) * (max_state), (pwm_max)))
36 #define EMC2305_PWM_STATE2DUTY(state, max_state, pwm_max) \
37 (DIV_ROUND_CLOSEST((state) * (pwm_max), (max_state)))
39 /* Factor by equations [2] and [3] from data sheet; valid for fans where the number of edges
40 * equal (poles * 2 + 1).
42 #define EMC2305_RPM_FACTOR 3932160
44 #define EMC2305_REG_FAN_DRIVE(n) (0x30 + 0x10 * (n))
45 #define EMC2305_REG_FAN_CFG(n) (0x32 + 0x10 * (n))
46 #define EMC2305_REG_FAN_MIN_DRIVE(n) (0x38 + 0x10 * (n))
47 #define EMC2305_REG_FAN_TACH(n) (0x3e + 0x10 * (n))
49 enum emc230x_product_id {
56 static const struct i2c_device_id emc2305_ids[] = {
63 MODULE_DEVICE_TABLE(i2c, emc2305_ids);
65 static const struct of_device_id emc2305_dt_ids[] = {
66 { .compatible = "microchip,emc2305" },
67 { .compatible = "microchip,emc2301" },
70 MODULE_DEVICE_TABLE(of, emc2305_dt_ids);
73 * @cdev: cooling device;
74 * @curr_state: cooling current state;
75 * @last_hwmon_state: last cooling state updated by hwmon subsystem;
76 * @last_thermal_state: last cooling state updated by thermal subsystem;
78 * The 'last_hwmon_state' and 'last_thermal_state' fields are provided to support fan low limit
79 * speed feature. The purpose of this feature is to provides ability to limit fan speed
80 * according to some system wise considerations, like absence of some replaceable units (PSU or
81 * line cards), high system ambient temperature, unreliable transceivers temperature sensing or
82 * some other factors which indirectly impacts system's airflow
83 * Fan low limit feature is supported through 'hwmon' interface: 'hwmon' 'pwm' attribute is
84 * used for setting low limit for fan speed in case 'thermal' subsystem is configured in
85 * kernel. In this case setting fan speed through 'hwmon' will never let the 'thermal'
86 * subsystem to select a lower duty cycle than the duty cycle selected with the 'pwm'
88 * From other side, fan speed is to be updated in hardware through 'pwm' only in case the
89 * requested fan speed is above last speed set by 'thermal' subsystem, otherwise requested fan
90 * speed will be just stored with no PWM update.
92 struct emc2305_cdev_data {
93 struct thermal_cooling_device *cdev;
94 unsigned int cur_state;
95 unsigned long last_hwmon_state;
96 unsigned long last_thermal_state;
100 * @client: i2c client;
101 * @hwmon_dev: hwmon device;
102 * @max_state: maximum cooling state of the cooling device;
103 * @pwm_max: maximum PWM;
104 * @pwm_min: minimum PWM;
105 * @pwm_channel: maximum number of PWM channels;
106 * @cdev_data: array of cooling devices data;
108 struct emc2305_data {
109 struct i2c_client *client;
110 struct device *hwmon_dev;
116 struct emc2305_cdev_data cdev_data[EMC2305_PWM_MAX];
119 static char *emc2305_fan_name[] = {
128 static int emc2305_get_max_channel(struct emc2305_data *data)
130 if (data->pwm_channel == EMC2305_PWM_CHNL_CMN)
131 return data->pwm_num;
133 return data->pwm_channel;
136 static int emc2305_get_cdev_idx(struct thermal_cooling_device *cdev)
138 struct emc2305_data *data = cdev->devdata;
139 size_t len = strlen(cdev->type);
145 /* Retuns index of cooling device 0..4 in case of separate PWM setting.
146 * Zero index is used in case of one common PWM setting.
147 * If the mode is set as EMC2305_PWM_CHNL_CMN, all PWMs are to be bound
148 * to the common thermal zone and should work at the same speed
149 * to perform cooling for the same thermal junction.
150 * Otherwise, return specific channel that will be used in bound
151 * related PWM to the thermal zone.
153 if (data->pwm_channel == EMC2305_PWM_CHNL_CMN)
156 ret = cdev->type[len - 1];
166 static int emc2305_get_cur_state(struct thermal_cooling_device *cdev, unsigned long *state)
169 struct emc2305_data *data = cdev->devdata;
171 cdev_idx = emc2305_get_cdev_idx(cdev);
175 *state = data->cdev_data[cdev_idx].cur_state;
179 static int emc2305_get_max_state(struct thermal_cooling_device *cdev, unsigned long *state)
181 struct emc2305_data *data = cdev->devdata;
182 *state = data->max_state;
186 static int emc2305_set_cur_state(struct thermal_cooling_device *cdev, unsigned long state)
189 struct emc2305_data *data = cdev->devdata;
190 struct i2c_client *client = data->client;
193 if (state > data->max_state)
196 cdev_idx = emc2305_get_cdev_idx(cdev);
200 /* Save thermal state. */
201 data->cdev_data[cdev_idx].last_thermal_state = state;
202 state = max_t(unsigned long, state, data->cdev_data[cdev_idx].last_hwmon_state);
204 val = EMC2305_PWM_STATE2DUTY(state, data->max_state, data->pwm_max);
206 if (val > EMC2305_FAN_MAX)
209 data->cdev_data[cdev_idx].cur_state = state;
210 if (data->pwm_channel == EMC2305_PWM_CHNL_CMN)
211 /* Set the same PWM value in all channels
212 * if common PWM channel is used.
214 for (i = 0; i < data->pwm_num; i++)
215 i2c_smbus_write_byte_data(client, EMC2305_REG_FAN_DRIVE(i), val);
217 i2c_smbus_write_byte_data(client, EMC2305_REG_FAN_DRIVE(cdev_idx), val);
222 static const struct thermal_cooling_device_ops emc2305_cooling_ops = {
223 .get_max_state = emc2305_get_max_state,
224 .get_cur_state = emc2305_get_cur_state,
225 .set_cur_state = emc2305_set_cur_state,
228 static int emc2305_show_fault(struct device *dev, int channel)
230 struct emc2305_data *data = dev_get_drvdata(dev);
231 struct i2c_client *client = data->client;
234 status_reg = i2c_smbus_read_byte_data(client, EMC2305_REG_DRIVE_FAIL_STATUS);
235 return status_reg & (1 << channel) ? 1 : 0;
238 static int emc2305_show_fan(struct device *dev, int channel)
240 struct emc2305_data *data = dev_get_drvdata(dev);
241 struct i2c_client *client = data->client;
244 ret = i2c_smbus_read_word_swapped(client, EMC2305_REG_FAN_TACH(channel));
248 ret = ret >> EMC2305_TACH_REGS_UNUSE_BITS;
249 return EMC2305_RPM_FACTOR * EMC2305_TACH_CNT_MULTIPLIER / (ret > 0 ? ret : 1);
252 static int emc2305_show_pwm(struct device *dev, int channel)
254 struct emc2305_data *data = dev_get_drvdata(dev);
255 struct i2c_client *client = data->client;
257 return i2c_smbus_read_byte_data(client, EMC2305_REG_FAN_DRIVE(channel));
260 static int emc2305_set_pwm(struct device *dev, long val, int channel)
262 struct emc2305_data *data = dev_get_drvdata(dev);
263 struct i2c_client *client = data->client;
265 if (val < data->pwm_min || val > data->pwm_max)
268 i2c_smbus_write_byte_data(client, EMC2305_REG_FAN_DRIVE(channel), val);
269 data->cdev_data[channel].cur_state = EMC2305_PWM_DUTY2STATE(val, data->max_state,
274 static int emc2305_get_tz_of(struct device *dev)
276 struct device_node *np = dev->of_node;
277 struct emc2305_data *data = dev_get_drvdata(dev);
280 /* OF parameters are optional - overwrite default setting
281 * if some of them are provided.
284 if (of_find_property(np, "emc2305,cooling-levels", NULL)) {
285 ret = of_property_read_u8(np, "emc2305,cooling-levels", &data->max_state);
290 if (of_find_property(np, "emc2305,pwm-max", NULL)) {
291 ret = of_property_read_u8(np, "emc2305,pwm-max", &data->pwm_max);
296 if (of_find_property(np, "emc2305,pwm-min", NULL)) {
297 ret = of_property_read_u8(np, "emc2305,pwm-min", &data->pwm_min);
302 /* Not defined or 0 means one thermal zone over all cooling devices.
303 * Otherwise - separated thermal zones for each PWM channel.
305 if (of_find_property(np, "emc2305,pwm-channel", NULL)) {
306 ret = of_property_read_u8(np, "emc2305,pwm-channel", &data->pwm_channel);
314 static int emc2305_set_single_tz(struct device *dev, int idx)
316 struct emc2305_data *data = dev_get_drvdata(dev);
317 long pwm = data->pwm_max;
320 cdev_idx = (idx) ? idx - 1 : 0;
323 data->cdev_data[cdev_idx].cdev =
324 devm_thermal_of_cooling_device_register(dev, dev->of_node,
325 emc2305_fan_name[idx], data,
326 &emc2305_cooling_ops);
328 data->cdev_data[cdev_idx].cdev =
329 thermal_cooling_device_register(emc2305_fan_name[idx], data,
330 &emc2305_cooling_ops);
332 if (IS_ERR(data->cdev_data[cdev_idx].cdev)) {
333 dev_err(dev, "Failed to register cooling device %s\n", emc2305_fan_name[idx]);
334 return PTR_ERR(data->cdev_data[cdev_idx].cdev);
336 emc2305_set_pwm(dev, pwm, cdev_idx);
337 data->cdev_data[cdev_idx].cur_state = data->max_state;
338 /* Set minimal PWM speed. */
339 data->cdev_data[cdev_idx].last_hwmon_state = EMC2305_PWM_DUTY2STATE(data->pwm_min,
345 static void emc2305_unset_tz(struct device *dev)
347 struct emc2305_data *data = dev_get_drvdata(dev);
350 /* Unregister cooling device in case they have been registered by
351 * thermal_cooling_device_unregister(). No need for clean-up flow in case they
352 * have been registered by devm_thermal_of_cooling_device_register()
355 for (i = 0; i < EMC2305_PWM_MAX; i++)
356 if (data->cdev_data[i].cdev)
357 thermal_cooling_device_unregister(data->cdev_data[i].cdev);
361 static int emc2305_set_tz(struct device *dev)
363 struct emc2305_data *data = dev_get_drvdata(dev);
366 if (data->pwm_channel == EMC2305_PWM_CHNL_CMN)
367 return emc2305_set_single_tz(dev, 0);
369 for (i = 0; i < data->pwm_channel; i++) {
370 ret = emc2305_set_single_tz(dev, i + 1);
372 goto thermal_cooling_device_register_fail;
376 thermal_cooling_device_register_fail:
377 emc2305_unset_tz(dev);
382 emc2305_is_visible(const void *data, enum hwmon_sensor_types type, u32 attr, int channel)
384 int max_channel = emc2305_get_max_channel((struct emc2305_data *)data);
386 /* Don't show channels which are not physically connected. */
387 if ((channel + 1) > max_channel)
392 case hwmon_fan_input:
394 case hwmon_fan_fault:
402 case hwmon_pwm_input:
416 emc2305_write(struct device *dev, enum hwmon_sensor_types type, u32 attr, int channel, long val)
418 struct emc2305_data *data = dev_get_drvdata(dev);
423 case hwmon_pwm_input:
424 /* If thermal is configured - handle PWM limit setting. */
425 if (IS_REACHABLE(CONFIG_THERMAL)) {
426 data->cdev_data[channel].last_hwmon_state =
427 EMC2305_PWM_DUTY2STATE(val, data->max_state, data->pwm_max);
428 /* Update PWM only in case requested state is not less than the
429 * last thermal state.
431 if (data->cdev_data[channel].last_hwmon_state >=
432 data->cdev_data[channel].last_thermal_state)
433 return emc2305_set_cur_state(data->cdev_data[channel].cdev,
434 data->cdev_data[channel].last_hwmon_state);
437 return emc2305_set_pwm(dev, val, channel);
450 emc2305_read(struct device *dev, enum hwmon_sensor_types type, u32 attr, int channel, long *val)
457 case hwmon_fan_input:
458 ret = emc2305_show_fan(dev, channel);
463 case hwmon_fan_fault:
464 ret = emc2305_show_fault(dev, channel);
475 case hwmon_pwm_input:
476 ret = emc2305_show_pwm(dev, channel);
492 static const struct hwmon_ops emc2305_ops = {
493 .is_visible = emc2305_is_visible,
494 .read = emc2305_read,
495 .write = emc2305_write,
498 static const struct hwmon_channel_info *emc2305_info[] = {
499 HWMON_CHANNEL_INFO(fan,
500 HWMON_F_INPUT | HWMON_F_FAULT,
501 HWMON_F_INPUT | HWMON_F_FAULT,
502 HWMON_F_INPUT | HWMON_F_FAULT,
503 HWMON_F_INPUT | HWMON_F_FAULT,
504 HWMON_F_INPUT | HWMON_F_FAULT),
505 HWMON_CHANNEL_INFO(pwm,
514 static const struct hwmon_chip_info emc2305_chip_info = {
516 .info = emc2305_info,
519 static int emc2305_identify(struct device *dev)
521 struct i2c_client *client = to_i2c_client(dev);
522 struct emc2305_data *data = i2c_get_clientdata(client);
525 ret = i2c_smbus_read_byte_data(client, EMC2305_REG_PRODUCT_ID);
549 static int emc2305_probe(struct i2c_client *client, const struct i2c_device_id *id)
551 struct i2c_adapter *adapter = client->adapter;
552 struct device *dev = &client->dev;
553 struct emc2305_data *data;
558 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA))
561 vendor = i2c_smbus_read_byte_data(client, EMC2305_REG_VENDOR);
562 if (vendor != EMC2305_VENDOR)
565 data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
569 i2c_set_clientdata(client, data);
570 data->client = client;
572 ret = emc2305_identify(dev);
576 data->max_state = EMC2305_FAN_MAX_STATE;
577 data->pwm_max = EMC2305_FAN_MAX;
578 data->pwm_min = EMC2305_FAN_MIN;
579 data->pwm_channel = EMC2305_PWM_CHNL_CMN;
581 ret = emc2305_get_tz_of(dev);
586 data->hwmon_dev = devm_hwmon_device_register_with_info(dev, "emc2305", data,
587 &emc2305_chip_info, NULL);
588 if (IS_ERR(data->hwmon_dev))
589 return PTR_ERR(data->hwmon_dev);
591 if (IS_REACHABLE(CONFIG_THERMAL)) {
592 ret = emc2305_set_tz(dev);
597 for (i = 0; i < data->pwm_num; i++) {
600 i2c_smbus_write_byte_data(client, EMC2305_REG_FAN_MIN_DRIVE(i),
603 val = i2c_smbus_read_byte_data(client, EMC2305_REG_FAN_CFG(i));
604 /* Set RNGx so minRPM=500 */
606 i2c_smbus_write_byte_data(client, EMC2305_REG_FAN_CFG(i), val);
609 /* Acknowledge any existing faults. Stops the device responding on the
610 * SMBus alert address.
612 i2c_smbus_read_byte_data(client, EMC2305_REG_FAN_STALL_STATUS);
613 i2c_smbus_read_byte_data(client, EMC2305_REG_FAN_STATUS);
618 static int emc2305_remove(struct i2c_client *client)
620 struct device *dev = &client->dev;
622 if (IS_REACHABLE(CONFIG_THERMAL))
623 emc2305_unset_tz(dev);
627 static struct i2c_driver emc2305_driver = {
628 .class = I2C_CLASS_HWMON,
631 .of_match_table = emc2305_dt_ids,
633 .probe = emc2305_probe,
634 .remove = emc2305_remove,
635 .id_table = emc2305_ids,
636 .address_list = emc2305_normal_i2c,
639 module_i2c_driver(emc2305_driver);
641 MODULE_AUTHOR("Nvidia");
642 MODULE_DESCRIPTION("Microchip EMC2305 fan controller driver");
643 MODULE_LICENSE("GPL");