1 // SPDX-License-Identifier: GPL-2.0+
3 * Hardware monitoring driver for EMC2305 fan controller
5 * Copyright (C) 2022 Nvidia Technologies Ltd.
9 #include <linux/hwmon.h>
10 #include <linux/i2c.h>
11 #include <linux/module.h>
12 #include <linux/platform_data/emc2305.h>
13 #include <linux/thermal.h>
15 static const unsigned short
16 emc2305_normal_i2c[] = { 0x27, 0x2c, 0x2d, 0x2e, 0x2f, 0x4c, 0x4d, I2C_CLIENT_END };
18 #define EMC2305_REG_DRIVE_FAIL_STATUS 0x27
19 #define EMC2305_REG_DEVICE 0xfd
20 #define EMC2305_REG_VENDOR 0xfe
21 #define EMC2305_FAN_MAX 0xff
22 #define EMC2305_FAN_MIN 0x00
23 #define EMC2305_FAN_MAX_STATE 10
24 #define EMC2305_DEVICE 0x34
25 #define EMC2305_VENDOR 0x5d
26 #define EMC2305_REG_PRODUCT_ID 0xfd
27 #define EMC2305_TACH_REGS_UNUSE_BITS 3
28 #define EMC2305_TACH_CNT_MULTIPLIER 0x02
29 #define EMC2305_TACH_RANGE_MIN 480
31 #define EMC2305_PWM_DUTY2STATE(duty, max_state, pwm_max) \
32 DIV_ROUND_CLOSEST((duty) * (max_state), (pwm_max))
33 #define EMC2305_PWM_STATE2DUTY(state, max_state, pwm_max) \
34 DIV_ROUND_CLOSEST((state) * (pwm_max), (max_state))
37 * Factor by equations [2] and [3] from data sheet; valid for fans where the number of edges
38 * equal (poles * 2 + 1).
40 #define EMC2305_RPM_FACTOR 3932160
42 #define EMC2305_REG_FAN_DRIVE(n) (0x30 + 0x10 * (n))
43 #define EMC2305_REG_FAN_MIN_DRIVE(n) (0x38 + 0x10 * (n))
44 #define EMC2305_REG_FAN_TACH(n) (0x3e + 0x10 * (n))
46 enum emc230x_product_id {
53 static const struct i2c_device_id emc2305_ids[] = {
60 MODULE_DEVICE_TABLE(i2c, emc2305_ids);
63 * @cdev: cooling device;
64 * @curr_state: cooling current state;
65 * @last_hwmon_state: last cooling state updated by hwmon subsystem;
66 * @last_thermal_state: last cooling state updated by thermal subsystem;
68 * The 'last_hwmon_state' and 'last_thermal_state' fields are provided to support fan low limit
69 * speed feature. The purpose of this feature is to provides ability to limit fan speed
70 * according to some system wise considerations, like absence of some replaceable units (PSU or
71 * line cards), high system ambient temperature, unreliable transceivers temperature sensing or
72 * some other factors which indirectly impacts system's airflow
73 * Fan low limit feature is supported through 'hwmon' interface: 'hwmon' 'pwm' attribute is
74 * used for setting low limit for fan speed in case 'thermal' subsystem is configured in
75 * kernel. In this case setting fan speed through 'hwmon' will never let the 'thermal'
76 * subsystem to select a lower duty cycle than the duty cycle selected with the 'pwm'
78 * From other side, fan speed is to be updated in hardware through 'pwm' only in case the
79 * requested fan speed is above last speed set by 'thermal' subsystem, otherwise requested fan
80 * speed will be just stored with no PWM update.
82 struct emc2305_cdev_data {
83 struct thermal_cooling_device *cdev;
84 unsigned int cur_state;
85 unsigned long last_hwmon_state;
86 unsigned long last_thermal_state;
90 * @client: i2c client;
91 * @hwmon_dev: hwmon device;
92 * @max_state: maximum cooling state of the cooling device;
93 * @pwm_num: number of PWM channels;
94 * @pwm_separate: separate PWM settings for every channel;
95 * @pwm_min: array of minimum PWM per channel;
96 * @cdev_data: array of cooling devices data;
99 struct i2c_client *client;
100 struct device *hwmon_dev;
104 u8 pwm_min[EMC2305_PWM_MAX];
105 struct emc2305_cdev_data cdev_data[EMC2305_PWM_MAX];
108 static char *emc2305_fan_name[] = {
117 static void emc2305_unset_tz(struct device *dev);
119 static int emc2305_get_max_channel(const struct emc2305_data *data)
121 return data->pwm_num;
124 static int emc2305_get_cdev_idx(struct thermal_cooling_device *cdev)
126 struct emc2305_data *data = cdev->devdata;
127 size_t len = strlen(cdev->type);
134 * Returns index of cooling device 0..4 in case of separate PWM setting.
135 * Zero index is used in case of one common PWM setting.
136 * If the mode is not set as pwm_separate, all PWMs are to be bound
137 * to the common thermal zone and should work at the same speed
138 * to perform cooling for the same thermal junction.
139 * Otherwise, return specific channel that will be used in bound
140 * related PWM to the thermal zone.
142 if (!data->pwm_separate)
145 ret = cdev->type[len - 1];
155 static int emc2305_get_cur_state(struct thermal_cooling_device *cdev, unsigned long *state)
158 struct emc2305_data *data = cdev->devdata;
160 cdev_idx = emc2305_get_cdev_idx(cdev);
164 *state = data->cdev_data[cdev_idx].cur_state;
168 static int emc2305_get_max_state(struct thermal_cooling_device *cdev, unsigned long *state)
170 struct emc2305_data *data = cdev->devdata;
171 *state = data->max_state;
175 static int emc2305_set_cur_state(struct thermal_cooling_device *cdev, unsigned long state)
178 struct emc2305_data *data = cdev->devdata;
179 struct i2c_client *client = data->client;
182 if (state > data->max_state)
185 cdev_idx = emc2305_get_cdev_idx(cdev);
189 /* Save thermal state. */
190 data->cdev_data[cdev_idx].last_thermal_state = state;
191 state = max_t(unsigned long, state, data->cdev_data[cdev_idx].last_hwmon_state);
193 val = EMC2305_PWM_STATE2DUTY(state, data->max_state, EMC2305_FAN_MAX);
195 data->cdev_data[cdev_idx].cur_state = state;
196 if (data->pwm_separate) {
197 ret = i2c_smbus_write_byte_data(client, EMC2305_REG_FAN_DRIVE(cdev_idx), val);
202 * Set the same PWM value in all channels
203 * if common PWM channel is used.
205 for (i = 0; i < data->pwm_num; i++) {
206 ret = i2c_smbus_write_byte_data(client, EMC2305_REG_FAN_DRIVE(i), val);
215 static const struct thermal_cooling_device_ops emc2305_cooling_ops = {
216 .get_max_state = emc2305_get_max_state,
217 .get_cur_state = emc2305_get_cur_state,
218 .set_cur_state = emc2305_set_cur_state,
221 static int emc2305_show_fault(struct device *dev, int channel)
223 struct emc2305_data *data = dev_get_drvdata(dev);
224 struct i2c_client *client = data->client;
227 status_reg = i2c_smbus_read_byte_data(client, EMC2305_REG_DRIVE_FAIL_STATUS);
231 return status_reg & (1 << channel) ? 1 : 0;
234 static int emc2305_show_fan(struct device *dev, int channel)
236 struct emc2305_data *data = dev_get_drvdata(dev);
237 struct i2c_client *client = data->client;
240 ret = i2c_smbus_read_word_swapped(client, EMC2305_REG_FAN_TACH(channel));
244 ret = ret >> EMC2305_TACH_REGS_UNUSE_BITS;
245 ret = EMC2305_RPM_FACTOR / ret;
246 if (ret <= EMC2305_TACH_RANGE_MIN)
249 return ret * EMC2305_TACH_CNT_MULTIPLIER;
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;
266 if (val < data->pwm_min[channel] || val > EMC2305_FAN_MAX)
269 ret = i2c_smbus_write_byte_data(client, EMC2305_REG_FAN_DRIVE(channel), val);
272 data->cdev_data[channel].cur_state = EMC2305_PWM_DUTY2STATE(val, data->max_state,
277 static int emc2305_set_single_tz(struct device *dev, int idx)
279 struct emc2305_data *data = dev_get_drvdata(dev);
281 int i, cdev_idx, ret;
283 cdev_idx = (idx) ? idx - 1 : 0;
284 pwm = data->pwm_min[cdev_idx];
286 data->cdev_data[cdev_idx].cdev =
287 thermal_cooling_device_register(emc2305_fan_name[idx], data,
288 &emc2305_cooling_ops);
290 if (IS_ERR(data->cdev_data[cdev_idx].cdev)) {
291 dev_err(dev, "Failed to register cooling device %s\n", emc2305_fan_name[idx]);
292 return PTR_ERR(data->cdev_data[cdev_idx].cdev);
294 /* Set minimal PWM speed. */
295 if (data->pwm_separate) {
296 ret = emc2305_set_pwm(dev, pwm, cdev_idx);
300 for (i = 0; i < data->pwm_num; i++) {
301 ret = emc2305_set_pwm(dev, pwm, i);
306 data->cdev_data[cdev_idx].cur_state =
307 EMC2305_PWM_DUTY2STATE(data->pwm_min[cdev_idx], data->max_state,
309 data->cdev_data[cdev_idx].last_hwmon_state =
310 EMC2305_PWM_DUTY2STATE(data->pwm_min[cdev_idx], data->max_state,
315 static int emc2305_set_tz(struct device *dev)
317 struct emc2305_data *data = dev_get_drvdata(dev);
320 if (!data->pwm_separate)
321 return emc2305_set_single_tz(dev, 0);
323 for (i = 0; i < data->pwm_num; i++) {
324 ret = emc2305_set_single_tz(dev, i + 1);
326 goto thermal_cooling_device_register_fail;
330 thermal_cooling_device_register_fail:
331 emc2305_unset_tz(dev);
335 static void emc2305_unset_tz(struct device *dev)
337 struct emc2305_data *data = dev_get_drvdata(dev);
340 /* Unregister cooling device. */
341 for (i = 0; i < EMC2305_PWM_MAX; i++)
342 if (data->cdev_data[i].cdev)
343 thermal_cooling_device_unregister(data->cdev_data[i].cdev);
347 emc2305_is_visible(const void *data, enum hwmon_sensor_types type, u32 attr, int channel)
349 int max_channel = emc2305_get_max_channel(data);
351 /* Don't show channels which are not physically connected. */
352 if (channel >= max_channel)
357 case hwmon_fan_input:
359 case hwmon_fan_fault:
367 case hwmon_pwm_input:
381 emc2305_write(struct device *dev, enum hwmon_sensor_types type, u32 attr, int channel, long val)
383 struct emc2305_data *data = dev_get_drvdata(dev);
389 case hwmon_pwm_input:
390 /* If thermal is configured - handle PWM limit setting. */
391 if (IS_REACHABLE(CONFIG_THERMAL)) {
392 if (data->pwm_separate)
396 data->cdev_data[cdev_idx].last_hwmon_state =
397 EMC2305_PWM_DUTY2STATE(val, data->max_state,
400 * Update PWM only in case requested state is not less than the
401 * last thermal state.
403 if (data->cdev_data[cdev_idx].last_hwmon_state >=
404 data->cdev_data[cdev_idx].last_thermal_state)
405 return emc2305_set_cur_state(data->cdev_data[cdev_idx].cdev,
406 data->cdev_data[cdev_idx].last_hwmon_state);
409 return emc2305_set_pwm(dev, val, channel);
422 emc2305_read(struct device *dev, enum hwmon_sensor_types type, u32 attr, int channel, long *val)
429 case hwmon_fan_input:
430 ret = emc2305_show_fan(dev, channel);
435 case hwmon_fan_fault:
436 ret = emc2305_show_fault(dev, channel);
447 case hwmon_pwm_input:
448 ret = emc2305_show_pwm(dev, channel);
464 static const struct hwmon_ops emc2305_ops = {
465 .is_visible = emc2305_is_visible,
466 .read = emc2305_read,
467 .write = emc2305_write,
470 static const struct hwmon_channel_info *emc2305_info[] = {
471 HWMON_CHANNEL_INFO(fan,
472 HWMON_F_INPUT | HWMON_F_FAULT,
473 HWMON_F_INPUT | HWMON_F_FAULT,
474 HWMON_F_INPUT | HWMON_F_FAULT,
475 HWMON_F_INPUT | HWMON_F_FAULT,
476 HWMON_F_INPUT | HWMON_F_FAULT),
477 HWMON_CHANNEL_INFO(pwm,
486 static const struct hwmon_chip_info emc2305_chip_info = {
488 .info = emc2305_info,
491 static int emc2305_identify(struct device *dev)
493 struct i2c_client *client = to_i2c_client(dev);
494 struct emc2305_data *data = i2c_get_clientdata(client);
497 ret = i2c_smbus_read_byte_data(client, EMC2305_REG_PRODUCT_ID);
521 static int emc2305_probe(struct i2c_client *client, const struct i2c_device_id *id)
523 struct i2c_adapter *adapter = client->adapter;
524 struct device *dev = &client->dev;
525 struct emc2305_data *data;
526 struct emc2305_platform_data *pdata;
531 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA))
534 vendor = i2c_smbus_read_byte_data(client, EMC2305_REG_VENDOR);
535 if (vendor != EMC2305_VENDOR)
538 device = i2c_smbus_read_byte_data(client, EMC2305_REG_DEVICE);
539 if (device != EMC2305_DEVICE)
542 data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
546 i2c_set_clientdata(client, data);
547 data->client = client;
549 ret = emc2305_identify(dev);
553 pdata = dev_get_platdata(&client->dev);
555 if (!pdata->max_state || pdata->max_state > EMC2305_FAN_MAX_STATE)
557 data->max_state = pdata->max_state;
559 * Validate a number of active PWM channels. Note that
560 * configured number can be less than the actual maximum
561 * supported by the device.
563 if (!pdata->pwm_num || pdata->pwm_num > EMC2305_PWM_MAX)
565 data->pwm_num = pdata->pwm_num;
566 data->pwm_separate = pdata->pwm_separate;
567 for (i = 0; i < EMC2305_PWM_MAX; i++)
568 data->pwm_min[i] = pdata->pwm_min[i];
570 data->max_state = EMC2305_FAN_MAX_STATE;
571 data->pwm_separate = false;
572 for (i = 0; i < EMC2305_PWM_MAX; i++)
573 data->pwm_min[i] = EMC2305_FAN_MIN;
576 data->hwmon_dev = devm_hwmon_device_register_with_info(dev, "emc2305", data,
577 &emc2305_chip_info, NULL);
578 if (IS_ERR(data->hwmon_dev))
579 return PTR_ERR(data->hwmon_dev);
581 if (IS_REACHABLE(CONFIG_THERMAL)) {
582 ret = emc2305_set_tz(dev);
587 for (i = 0; i < data->pwm_num; i++) {
588 ret = i2c_smbus_write_byte_data(client, EMC2305_REG_FAN_MIN_DRIVE(i),
597 static void emc2305_remove(struct i2c_client *client)
599 struct device *dev = &client->dev;
601 if (IS_REACHABLE(CONFIG_THERMAL))
602 emc2305_unset_tz(dev);
605 static struct i2c_driver emc2305_driver = {
606 .class = I2C_CLASS_HWMON,
610 .probe = emc2305_probe,
611 .remove = emc2305_remove,
612 .id_table = emc2305_ids,
613 .address_list = emc2305_normal_i2c,
616 module_i2c_driver(emc2305_driver);
618 MODULE_AUTHOR("Nvidia");
619 MODULE_DESCRIPTION("Microchip EMC2305 fan controller driver");
620 MODULE_LICENSE("GPL");