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_VENDOR 0xfe
20 #define EMC2305_FAN_MAX 0xff
21 #define EMC2305_FAN_MIN 0x00
22 #define EMC2305_FAN_MAX_STATE 10
23 #define EMC2305_DEVICE 0x34
24 #define EMC2305_VENDOR 0x5d
25 #define EMC2305_REG_PRODUCT_ID 0xfd
26 #define EMC2305_TACH_REGS_UNUSE_BITS 3
27 #define EMC2305_TACH_CNT_MULTIPLIER 0x02
28 #define EMC2305_TACH_RANGE_MIN 480
30 #define EMC2305_PWM_DUTY2STATE(duty, max_state, pwm_max) \
31 DIV_ROUND_CLOSEST((duty) * (max_state), (pwm_max))
32 #define EMC2305_PWM_STATE2DUTY(state, max_state, pwm_max) \
33 DIV_ROUND_CLOSEST((state) * (pwm_max), (max_state))
36 * Factor by equations [2] and [3] from data sheet; valid for fans where the number of edges
37 * equal (poles * 2 + 1).
39 #define EMC2305_RPM_FACTOR 3932160
41 #define EMC2305_REG_FAN_DRIVE(n) (0x30 + 0x10 * (n))
42 #define EMC2305_REG_FAN_MIN_DRIVE(n) (0x38 + 0x10 * (n))
43 #define EMC2305_REG_FAN_TACH(n) (0x3e + 0x10 * (n))
45 enum emc230x_product_id {
52 static const struct i2c_device_id emc2305_ids[] = {
59 MODULE_DEVICE_TABLE(i2c, emc2305_ids);
62 * @cdev: cooling device;
63 * @curr_state: cooling current state;
64 * @last_hwmon_state: last cooling state updated by hwmon subsystem;
65 * @last_thermal_state: last cooling state updated by thermal subsystem;
67 * The 'last_hwmon_state' and 'last_thermal_state' fields are provided to support fan low limit
68 * speed feature. The purpose of this feature is to provides ability to limit fan speed
69 * according to some system wise considerations, like absence of some replaceable units (PSU or
70 * line cards), high system ambient temperature, unreliable transceivers temperature sensing or
71 * some other factors which indirectly impacts system's airflow
72 * Fan low limit feature is supported through 'hwmon' interface: 'hwmon' 'pwm' attribute is
73 * used for setting low limit for fan speed in case 'thermal' subsystem is configured in
74 * kernel. In this case setting fan speed through 'hwmon' will never let the 'thermal'
75 * subsystem to select a lower duty cycle than the duty cycle selected with the 'pwm'
77 * From other side, fan speed is to be updated in hardware through 'pwm' only in case the
78 * requested fan speed is above last speed set by 'thermal' subsystem, otherwise requested fan
79 * speed will be just stored with no PWM update.
81 struct emc2305_cdev_data {
82 struct thermal_cooling_device *cdev;
83 unsigned int cur_state;
84 unsigned long last_hwmon_state;
85 unsigned long last_thermal_state;
89 * @client: i2c client;
90 * @hwmon_dev: hwmon device;
91 * @max_state: maximum cooling state of the cooling device;
92 * @pwm_num: number of PWM channels;
93 * @pwm_separate: separate PWM settings for every channel;
94 * @pwm_min: array of minimum PWM per channel;
95 * @cdev_data: array of cooling devices data;
98 struct i2c_client *client;
99 struct device *hwmon_dev;
103 u8 pwm_min[EMC2305_PWM_MAX];
104 struct emc2305_cdev_data cdev_data[EMC2305_PWM_MAX];
107 static char *emc2305_fan_name[] = {
116 static void emc2305_unset_tz(struct device *dev);
118 static int emc2305_get_max_channel(const struct emc2305_data *data)
120 return data->pwm_num;
123 static int emc2305_get_cdev_idx(struct thermal_cooling_device *cdev)
125 struct emc2305_data *data = cdev->devdata;
126 size_t len = strlen(cdev->type);
133 * Returns index of cooling device 0..4 in case of separate PWM setting.
134 * Zero index is used in case of one common PWM setting.
135 * If the mode is not set as pwm_separate, all PWMs are to be bound
136 * to the common thermal zone and should work at the same speed
137 * to perform cooling for the same thermal junction.
138 * Otherwise, return specific channel that will be used in bound
139 * related PWM to the thermal zone.
141 if (!data->pwm_separate)
144 ret = cdev->type[len - 1];
154 static int emc2305_get_cur_state(struct thermal_cooling_device *cdev, unsigned long *state)
157 struct emc2305_data *data = cdev->devdata;
159 cdev_idx = emc2305_get_cdev_idx(cdev);
163 *state = data->cdev_data[cdev_idx].cur_state;
167 static int emc2305_get_max_state(struct thermal_cooling_device *cdev, unsigned long *state)
169 struct emc2305_data *data = cdev->devdata;
170 *state = data->max_state;
174 static int __emc2305_set_cur_state(struct emc2305_data *data, int cdev_idx, unsigned long state)
177 struct i2c_client *client = data->client;
180 state = max_t(unsigned long, state, data->cdev_data[cdev_idx].last_hwmon_state);
182 val = EMC2305_PWM_STATE2DUTY(state, data->max_state, EMC2305_FAN_MAX);
184 data->cdev_data[cdev_idx].cur_state = state;
185 if (data->pwm_separate) {
186 ret = i2c_smbus_write_byte_data(client, EMC2305_REG_FAN_DRIVE(cdev_idx), val);
191 * Set the same PWM value in all channels
192 * if common PWM channel is used.
194 for (i = 0; i < data->pwm_num; i++) {
195 ret = i2c_smbus_write_byte_data(client, EMC2305_REG_FAN_DRIVE(i), val);
204 static int emc2305_set_cur_state(struct thermal_cooling_device *cdev, unsigned long state)
207 struct emc2305_data *data = cdev->devdata;
209 if (state > data->max_state)
212 cdev_idx = emc2305_get_cdev_idx(cdev);
216 /* Save thermal state. */
217 data->cdev_data[cdev_idx].last_thermal_state = state;
218 ret = __emc2305_set_cur_state(data, cdev_idx, state);
225 static const struct thermal_cooling_device_ops emc2305_cooling_ops = {
226 .get_max_state = emc2305_get_max_state,
227 .get_cur_state = emc2305_get_cur_state,
228 .set_cur_state = emc2305_set_cur_state,
231 static int emc2305_show_fault(struct device *dev, int channel)
233 struct emc2305_data *data = dev_get_drvdata(dev);
234 struct i2c_client *client = data->client;
237 status_reg = i2c_smbus_read_byte_data(client, EMC2305_REG_DRIVE_FAIL_STATUS);
241 return status_reg & (1 << channel) ? 1 : 0;
244 static int emc2305_show_fan(struct device *dev, int channel)
246 struct emc2305_data *data = dev_get_drvdata(dev);
247 struct i2c_client *client = data->client;
250 ret = i2c_smbus_read_word_swapped(client, EMC2305_REG_FAN_TACH(channel));
254 ret = ret >> EMC2305_TACH_REGS_UNUSE_BITS;
255 ret = EMC2305_RPM_FACTOR / ret;
256 if (ret <= EMC2305_TACH_RANGE_MIN)
259 return ret * EMC2305_TACH_CNT_MULTIPLIER;
262 static int emc2305_show_pwm(struct device *dev, int channel)
264 struct emc2305_data *data = dev_get_drvdata(dev);
265 struct i2c_client *client = data->client;
267 return i2c_smbus_read_byte_data(client, EMC2305_REG_FAN_DRIVE(channel));
270 static int emc2305_set_pwm(struct device *dev, long val, int channel)
272 struct emc2305_data *data = dev_get_drvdata(dev);
273 struct i2c_client *client = data->client;
276 if (val < data->pwm_min[channel] || val > EMC2305_FAN_MAX)
279 ret = i2c_smbus_write_byte_data(client, EMC2305_REG_FAN_DRIVE(channel), val);
282 data->cdev_data[channel].cur_state = EMC2305_PWM_DUTY2STATE(val, data->max_state,
287 static int emc2305_set_single_tz(struct device *dev, int idx)
289 struct emc2305_data *data = dev_get_drvdata(dev);
291 int i, cdev_idx, ret;
293 cdev_idx = (idx) ? idx - 1 : 0;
294 pwm = data->pwm_min[cdev_idx];
296 data->cdev_data[cdev_idx].cdev =
297 thermal_cooling_device_register(emc2305_fan_name[idx], data,
298 &emc2305_cooling_ops);
300 if (IS_ERR(data->cdev_data[cdev_idx].cdev)) {
301 dev_err(dev, "Failed to register cooling device %s\n", emc2305_fan_name[idx]);
302 return PTR_ERR(data->cdev_data[cdev_idx].cdev);
304 /* Set minimal PWM speed. */
305 if (data->pwm_separate) {
306 ret = emc2305_set_pwm(dev, pwm, cdev_idx);
310 for (i = 0; i < data->pwm_num; i++) {
311 ret = emc2305_set_pwm(dev, pwm, i);
316 data->cdev_data[cdev_idx].cur_state =
317 EMC2305_PWM_DUTY2STATE(data->pwm_min[cdev_idx], data->max_state,
319 data->cdev_data[cdev_idx].last_hwmon_state =
320 EMC2305_PWM_DUTY2STATE(data->pwm_min[cdev_idx], data->max_state,
325 static int emc2305_set_tz(struct device *dev)
327 struct emc2305_data *data = dev_get_drvdata(dev);
330 if (!data->pwm_separate)
331 return emc2305_set_single_tz(dev, 0);
333 for (i = 0; i < data->pwm_num; i++) {
334 ret = emc2305_set_single_tz(dev, i + 1);
336 goto thermal_cooling_device_register_fail;
340 thermal_cooling_device_register_fail:
341 emc2305_unset_tz(dev);
345 static void emc2305_unset_tz(struct device *dev)
347 struct emc2305_data *data = dev_get_drvdata(dev);
350 /* Unregister cooling device. */
351 for (i = 0; i < EMC2305_PWM_MAX; i++)
352 if (data->cdev_data[i].cdev)
353 thermal_cooling_device_unregister(data->cdev_data[i].cdev);
357 emc2305_is_visible(const void *data, enum hwmon_sensor_types type, u32 attr, int channel)
359 int max_channel = emc2305_get_max_channel(data);
361 /* Don't show channels which are not physically connected. */
362 if (channel >= max_channel)
367 case hwmon_fan_input:
369 case hwmon_fan_fault:
377 case hwmon_pwm_input:
391 emc2305_write(struct device *dev, enum hwmon_sensor_types type, u32 attr, int channel, long val)
393 struct emc2305_data *data = dev_get_drvdata(dev);
399 case hwmon_pwm_input:
400 /* If thermal is configured - handle PWM limit setting. */
401 if (IS_REACHABLE(CONFIG_THERMAL)) {
402 if (data->pwm_separate)
406 data->cdev_data[cdev_idx].last_hwmon_state =
407 EMC2305_PWM_DUTY2STATE(val, data->max_state,
410 * Update PWM only in case requested state is not less than the
411 * last thermal state.
413 if (data->cdev_data[cdev_idx].last_hwmon_state >=
414 data->cdev_data[cdev_idx].last_thermal_state)
415 return __emc2305_set_cur_state(data, cdev_idx,
416 data->cdev_data[cdev_idx].last_hwmon_state);
419 return emc2305_set_pwm(dev, val, channel);
432 emc2305_read(struct device *dev, enum hwmon_sensor_types type, u32 attr, int channel, long *val)
439 case hwmon_fan_input:
440 ret = emc2305_show_fan(dev, channel);
445 case hwmon_fan_fault:
446 ret = emc2305_show_fault(dev, channel);
457 case hwmon_pwm_input:
458 ret = emc2305_show_pwm(dev, channel);
474 static const struct hwmon_ops emc2305_ops = {
475 .is_visible = emc2305_is_visible,
476 .read = emc2305_read,
477 .write = emc2305_write,
480 static const struct hwmon_channel_info *emc2305_info[] = {
481 HWMON_CHANNEL_INFO(fan,
482 HWMON_F_INPUT | HWMON_F_FAULT,
483 HWMON_F_INPUT | HWMON_F_FAULT,
484 HWMON_F_INPUT | HWMON_F_FAULT,
485 HWMON_F_INPUT | HWMON_F_FAULT,
486 HWMON_F_INPUT | HWMON_F_FAULT),
487 HWMON_CHANNEL_INFO(pwm,
496 static const struct hwmon_chip_info emc2305_chip_info = {
498 .info = emc2305_info,
501 static int emc2305_identify(struct device *dev)
503 struct i2c_client *client = to_i2c_client(dev);
504 struct emc2305_data *data = i2c_get_clientdata(client);
507 ret = i2c_smbus_read_byte_data(client, EMC2305_REG_PRODUCT_ID);
531 static int emc2305_probe(struct i2c_client *client, const struct i2c_device_id *id)
533 struct i2c_adapter *adapter = client->adapter;
534 struct device *dev = &client->dev;
535 struct emc2305_data *data;
536 struct emc2305_platform_data *pdata;
541 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA))
544 vendor = i2c_smbus_read_byte_data(client, EMC2305_REG_VENDOR);
545 if (vendor != EMC2305_VENDOR)
548 data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
552 i2c_set_clientdata(client, data);
553 data->client = client;
555 ret = emc2305_identify(dev);
559 pdata = dev_get_platdata(&client->dev);
561 if (!pdata->max_state || pdata->max_state > EMC2305_FAN_MAX_STATE)
563 data->max_state = pdata->max_state;
565 * Validate a number of active PWM channels. Note that
566 * configured number can be less than the actual maximum
567 * supported by the device.
569 if (!pdata->pwm_num || pdata->pwm_num > EMC2305_PWM_MAX)
571 data->pwm_num = pdata->pwm_num;
572 data->pwm_separate = pdata->pwm_separate;
573 for (i = 0; i < EMC2305_PWM_MAX; i++)
574 data->pwm_min[i] = pdata->pwm_min[i];
576 data->max_state = EMC2305_FAN_MAX_STATE;
577 data->pwm_separate = false;
578 for (i = 0; i < EMC2305_PWM_MAX; i++)
579 data->pwm_min[i] = EMC2305_FAN_MIN;
582 data->hwmon_dev = devm_hwmon_device_register_with_info(dev, "emc2305", data,
583 &emc2305_chip_info, NULL);
584 if (IS_ERR(data->hwmon_dev))
585 return PTR_ERR(data->hwmon_dev);
587 if (IS_REACHABLE(CONFIG_THERMAL)) {
588 ret = emc2305_set_tz(dev);
593 for (i = 0; i < data->pwm_num; i++) {
594 ret = i2c_smbus_write_byte_data(client, EMC2305_REG_FAN_MIN_DRIVE(i),
603 static void emc2305_remove(struct i2c_client *client)
605 struct device *dev = &client->dev;
607 if (IS_REACHABLE(CONFIG_THERMAL))
608 emc2305_unset_tz(dev);
611 static struct i2c_driver emc2305_driver = {
612 .class = I2C_CLASS_HWMON,
616 .probe = emc2305_probe,
617 .remove = emc2305_remove,
618 .id_table = emc2305_ids,
619 .address_list = emc2305_normal_i2c,
622 module_i2c_driver(emc2305_driver);
624 MODULE_AUTHOR("Nvidia");
625 MODULE_DESCRIPTION("Microchip EMC2305 fan controller driver");
626 MODULE_LICENSE("GPL");