1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * emc2103.c - Support for SMSC EMC2103
4 * Copyright (c) 2010 SMSC
7 #include <linux/module.h>
8 #include <linux/init.h>
9 #include <linux/slab.h>
10 #include <linux/jiffies.h>
11 #include <linux/i2c.h>
12 #include <linux/hwmon.h>
13 #include <linux/hwmon-sysfs.h>
14 #include <linux/err.h>
15 #include <linux/mutex.h>
17 /* Addresses scanned */
18 static const unsigned short normal_i2c[] = { 0x2E, I2C_CLIENT_END };
20 static const u8 REG_TEMP[4] = { 0x00, 0x02, 0x04, 0x06 };
21 static const u8 REG_TEMP_MIN[4] = { 0x3c, 0x38, 0x39, 0x3a };
22 static const u8 REG_TEMP_MAX[4] = { 0x34, 0x30, 0x31, 0x32 };
24 #define REG_CONF1 0x20
25 #define REG_TEMP_MAX_ALARM 0x24
26 #define REG_TEMP_MIN_ALARM 0x25
27 #define REG_FAN_CONF1 0x42
28 #define REG_FAN_TARGET_LO 0x4c
29 #define REG_FAN_TARGET_HI 0x4d
30 #define REG_FAN_TACH_HI 0x4e
31 #define REG_FAN_TACH_LO 0x4f
32 #define REG_PRODUCT_ID 0xfd
33 #define REG_MFG_ID 0xfe
35 /* equation 4 from datasheet: rpm = (3932160 * multipler) / count */
36 #define FAN_RPM_FACTOR 3932160
39 * 2103-2 and 2103-4's 3rd temperature sensor can be connected to two diodes
40 * in anti-parallel mode, and in this configuration both can be read
41 * independently (so we have 4 temperature inputs). The device can't
42 * detect if it's connected in this mode, so we have to manually enable
43 * it. Default is to leave the device in the state it's already in (-1).
44 * This parameter allows APD mode to be optionally forced on or off
47 module_param(apd, bint, 0);
48 MODULE_PARM_DESC(apd, "Set to zero to disable anti-parallel diode mode");
52 u8 fraction; /* 0-7 multiples of 0.125 */
56 struct i2c_client *client;
57 const struct attribute_group *groups[4];
58 struct mutex update_lock;
59 bool valid; /* registers are valid */
61 int temp_count; /* num of temp sensors */
62 unsigned long last_updated; /* in jiffies */
63 struct temperature temp[4]; /* internal + 3 external */
64 s8 temp_min[4]; /* no fractional part */
65 s8 temp_max[4]; /* no fractional part */
73 static int read_u8_from_i2c(struct i2c_client *client, u8 i2c_reg, u8 *output)
75 int status = i2c_smbus_read_byte_data(client, i2c_reg);
77 dev_warn(&client->dev, "reg 0x%02x, err %d\n",
85 static void read_temp_from_i2c(struct i2c_client *client, u8 i2c_reg,
86 struct temperature *temp)
88 u8 degrees, fractional;
90 if (read_u8_from_i2c(client, i2c_reg, °rees) < 0)
93 if (read_u8_from_i2c(client, i2c_reg + 1, &fractional) < 0)
96 temp->degrees = degrees;
97 temp->fraction = (fractional & 0xe0) >> 5;
100 static void read_fan_from_i2c(struct i2c_client *client, u16 *output,
101 u8 hi_addr, u8 lo_addr)
103 u8 high_byte, lo_byte;
105 if (read_u8_from_i2c(client, hi_addr, &high_byte) < 0)
108 if (read_u8_from_i2c(client, lo_addr, &lo_byte) < 0)
111 *output = ((u16)high_byte << 5) | (lo_byte >> 3);
114 static void write_fan_target_to_i2c(struct i2c_client *client, u16 new_target)
116 u8 high_byte = (new_target & 0x1fe0) >> 5;
117 u8 low_byte = (new_target & 0x001f) << 3;
118 i2c_smbus_write_byte_data(client, REG_FAN_TARGET_LO, low_byte);
119 i2c_smbus_write_byte_data(client, REG_FAN_TARGET_HI, high_byte);
122 static void read_fan_config_from_i2c(struct i2c_client *client)
125 struct emc2103_data *data = i2c_get_clientdata(client);
128 if (read_u8_from_i2c(client, REG_FAN_CONF1, &conf1) < 0)
131 data->fan_multiplier = 1 << ((conf1 & 0x60) >> 5);
132 data->fan_rpm_control = (conf1 & 0x80) != 0;
135 static struct emc2103_data *emc2103_update_device(struct device *dev)
137 struct emc2103_data *data = dev_get_drvdata(dev);
138 struct i2c_client *client = data->client;
140 mutex_lock(&data->update_lock);
142 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
146 for (i = 0; i < data->temp_count; i++) {
147 read_temp_from_i2c(client, REG_TEMP[i], &data->temp[i]);
148 read_u8_from_i2c(client, REG_TEMP_MIN[i],
150 read_u8_from_i2c(client, REG_TEMP_MAX[i],
154 read_u8_from_i2c(client, REG_TEMP_MIN_ALARM,
155 &data->temp_min_alarm);
156 read_u8_from_i2c(client, REG_TEMP_MAX_ALARM,
157 &data->temp_max_alarm);
159 read_fan_from_i2c(client, &data->fan_tach,
160 REG_FAN_TACH_HI, REG_FAN_TACH_LO);
161 read_fan_from_i2c(client, &data->fan_target,
162 REG_FAN_TARGET_HI, REG_FAN_TARGET_LO);
163 read_fan_config_from_i2c(client);
165 data->last_updated = jiffies;
169 mutex_unlock(&data->update_lock);
175 temp_show(struct device *dev, struct device_attribute *da, char *buf)
177 int nr = to_sensor_dev_attr(da)->index;
178 struct emc2103_data *data = emc2103_update_device(dev);
179 int millidegrees = data->temp[nr].degrees * 1000
180 + data->temp[nr].fraction * 125;
181 return sprintf(buf, "%d\n", millidegrees);
185 temp_min_show(struct device *dev, struct device_attribute *da, char *buf)
187 int nr = to_sensor_dev_attr(da)->index;
188 struct emc2103_data *data = emc2103_update_device(dev);
189 int millidegrees = data->temp_min[nr] * 1000;
190 return sprintf(buf, "%d\n", millidegrees);
194 temp_max_show(struct device *dev, struct device_attribute *da, char *buf)
196 int nr = to_sensor_dev_attr(da)->index;
197 struct emc2103_data *data = emc2103_update_device(dev);
198 int millidegrees = data->temp_max[nr] * 1000;
199 return sprintf(buf, "%d\n", millidegrees);
203 temp_fault_show(struct device *dev, struct device_attribute *da, char *buf)
205 int nr = to_sensor_dev_attr(da)->index;
206 struct emc2103_data *data = emc2103_update_device(dev);
207 bool fault = (data->temp[nr].degrees == -128);
208 return sprintf(buf, "%d\n", fault ? 1 : 0);
212 temp_min_alarm_show(struct device *dev, struct device_attribute *da,
215 int nr = to_sensor_dev_attr(da)->index;
216 struct emc2103_data *data = emc2103_update_device(dev);
217 bool alarm = data->temp_min_alarm & (1 << nr);
218 return sprintf(buf, "%d\n", alarm ? 1 : 0);
222 temp_max_alarm_show(struct device *dev, struct device_attribute *da,
225 int nr = to_sensor_dev_attr(da)->index;
226 struct emc2103_data *data = emc2103_update_device(dev);
227 bool alarm = data->temp_max_alarm & (1 << nr);
228 return sprintf(buf, "%d\n", alarm ? 1 : 0);
231 static ssize_t temp_min_store(struct device *dev, struct device_attribute *da,
232 const char *buf, size_t count)
234 int nr = to_sensor_dev_attr(da)->index;
235 struct emc2103_data *data = dev_get_drvdata(dev);
236 struct i2c_client *client = data->client;
239 int result = kstrtol(buf, 10, &val);
243 val = DIV_ROUND_CLOSEST(clamp_val(val, -63000, 127000), 1000);
245 mutex_lock(&data->update_lock);
246 data->temp_min[nr] = val;
247 i2c_smbus_write_byte_data(client, REG_TEMP_MIN[nr], val);
248 mutex_unlock(&data->update_lock);
253 static ssize_t temp_max_store(struct device *dev, struct device_attribute *da,
254 const char *buf, size_t count)
256 int nr = to_sensor_dev_attr(da)->index;
257 struct emc2103_data *data = dev_get_drvdata(dev);
258 struct i2c_client *client = data->client;
261 int result = kstrtol(buf, 10, &val);
265 val = DIV_ROUND_CLOSEST(clamp_val(val, -63000, 127000), 1000);
267 mutex_lock(&data->update_lock);
268 data->temp_max[nr] = val;
269 i2c_smbus_write_byte_data(client, REG_TEMP_MAX[nr], val);
270 mutex_unlock(&data->update_lock);
276 fan1_input_show(struct device *dev, struct device_attribute *da, char *buf)
278 struct emc2103_data *data = emc2103_update_device(dev);
280 if (data->fan_tach != 0)
281 rpm = (FAN_RPM_FACTOR * data->fan_multiplier) / data->fan_tach;
282 return sprintf(buf, "%d\n", rpm);
286 fan1_div_show(struct device *dev, struct device_attribute *da, char *buf)
288 struct emc2103_data *data = emc2103_update_device(dev);
289 int fan_div = 8 / data->fan_multiplier;
290 return sprintf(buf, "%d\n", fan_div);
294 * Note: we also update the fan target here, because its value is
295 * determined in part by the fan clock divider. This follows the principle
296 * of least surprise; the user doesn't expect the fan target to change just
297 * because the divider changed.
299 static ssize_t fan1_div_store(struct device *dev, struct device_attribute *da,
300 const char *buf, size_t count)
302 struct emc2103_data *data = emc2103_update_device(dev);
303 struct i2c_client *client = data->client;
304 int new_range_bits, old_div = 8 / data->fan_multiplier;
307 int status = kstrtol(buf, 10, &new_div);
311 if (new_div == old_div) /* No change */
331 mutex_lock(&data->update_lock);
333 status = i2c_smbus_read_byte_data(client, REG_FAN_CONF1);
335 dev_dbg(&client->dev, "reg 0x%02x, err %d\n",
336 REG_FAN_CONF1, status);
337 mutex_unlock(&data->update_lock);
341 status |= (new_range_bits << 5);
342 i2c_smbus_write_byte_data(client, REG_FAN_CONF1, status);
344 data->fan_multiplier = 8 / new_div;
346 /* update fan target if high byte is not disabled */
347 if ((data->fan_target & 0x1fe0) != 0x1fe0) {
348 u16 new_target = (data->fan_target * old_div) / new_div;
349 data->fan_target = min(new_target, (u16)0x1fff);
350 write_fan_target_to_i2c(client, data->fan_target);
353 /* invalidate data to force re-read from hardware */
356 mutex_unlock(&data->update_lock);
361 fan1_target_show(struct device *dev, struct device_attribute *da, char *buf)
363 struct emc2103_data *data = emc2103_update_device(dev);
366 /* high byte of 0xff indicates disabled so return 0 */
367 if ((data->fan_target != 0) && ((data->fan_target & 0x1fe0) != 0x1fe0))
368 rpm = (FAN_RPM_FACTOR * data->fan_multiplier)
371 return sprintf(buf, "%d\n", rpm);
374 static ssize_t fan1_target_store(struct device *dev,
375 struct device_attribute *da, const char *buf,
378 struct emc2103_data *data = emc2103_update_device(dev);
379 struct i2c_client *client = data->client;
380 unsigned long rpm_target;
382 int result = kstrtoul(buf, 10, &rpm_target);
386 /* Datasheet states 16384 as maximum RPM target (table 3.2) */
387 rpm_target = clamp_val(rpm_target, 0, 16384);
389 mutex_lock(&data->update_lock);
392 data->fan_target = 0x1fff;
394 data->fan_target = clamp_val(
395 (FAN_RPM_FACTOR * data->fan_multiplier) / rpm_target,
398 write_fan_target_to_i2c(client, data->fan_target);
400 mutex_unlock(&data->update_lock);
405 fan1_fault_show(struct device *dev, struct device_attribute *da, char *buf)
407 struct emc2103_data *data = emc2103_update_device(dev);
408 bool fault = ((data->fan_tach & 0x1fe0) == 0x1fe0);
409 return sprintf(buf, "%d\n", fault ? 1 : 0);
413 pwm1_enable_show(struct device *dev, struct device_attribute *da, char *buf)
415 struct emc2103_data *data = emc2103_update_device(dev);
416 return sprintf(buf, "%d\n", data->fan_rpm_control ? 3 : 0);
419 static ssize_t pwm1_enable_store(struct device *dev,
420 struct device_attribute *da, const char *buf,
423 struct emc2103_data *data = dev_get_drvdata(dev);
424 struct i2c_client *client = data->client;
428 int result = kstrtol(buf, 10, &new_value);
432 mutex_lock(&data->update_lock);
435 data->fan_rpm_control = false;
438 data->fan_rpm_control = true;
445 result = read_u8_from_i2c(client, REG_FAN_CONF1, &conf_reg);
451 if (data->fan_rpm_control)
456 i2c_smbus_write_byte_data(client, REG_FAN_CONF1, conf_reg);
458 mutex_unlock(&data->update_lock);
462 static SENSOR_DEVICE_ATTR_RO(temp1_input, temp, 0);
463 static SENSOR_DEVICE_ATTR_RW(temp1_min, temp_min, 0);
464 static SENSOR_DEVICE_ATTR_RW(temp1_max, temp_max, 0);
465 static SENSOR_DEVICE_ATTR_RO(temp1_fault, temp_fault, 0);
466 static SENSOR_DEVICE_ATTR_RO(temp1_min_alarm, temp_min_alarm, 0);
467 static SENSOR_DEVICE_ATTR_RO(temp1_max_alarm, temp_max_alarm, 0);
469 static SENSOR_DEVICE_ATTR_RO(temp2_input, temp, 1);
470 static SENSOR_DEVICE_ATTR_RW(temp2_min, temp_min, 1);
471 static SENSOR_DEVICE_ATTR_RW(temp2_max, temp_max, 1);
472 static SENSOR_DEVICE_ATTR_RO(temp2_fault, temp_fault, 1);
473 static SENSOR_DEVICE_ATTR_RO(temp2_min_alarm, temp_min_alarm, 1);
474 static SENSOR_DEVICE_ATTR_RO(temp2_max_alarm, temp_max_alarm, 1);
476 static SENSOR_DEVICE_ATTR_RO(temp3_input, temp, 2);
477 static SENSOR_DEVICE_ATTR_RW(temp3_min, temp_min, 2);
478 static SENSOR_DEVICE_ATTR_RW(temp3_max, temp_max, 2);
479 static SENSOR_DEVICE_ATTR_RO(temp3_fault, temp_fault, 2);
480 static SENSOR_DEVICE_ATTR_RO(temp3_min_alarm, temp_min_alarm, 2);
481 static SENSOR_DEVICE_ATTR_RO(temp3_max_alarm, temp_max_alarm, 2);
483 static SENSOR_DEVICE_ATTR_RO(temp4_input, temp, 3);
484 static SENSOR_DEVICE_ATTR_RW(temp4_min, temp_min, 3);
485 static SENSOR_DEVICE_ATTR_RW(temp4_max, temp_max, 3);
486 static SENSOR_DEVICE_ATTR_RO(temp4_fault, temp_fault, 3);
487 static SENSOR_DEVICE_ATTR_RO(temp4_min_alarm, temp_min_alarm, 3);
488 static SENSOR_DEVICE_ATTR_RO(temp4_max_alarm, temp_max_alarm, 3);
490 static DEVICE_ATTR_RO(fan1_input);
491 static DEVICE_ATTR_RW(fan1_div);
492 static DEVICE_ATTR_RW(fan1_target);
493 static DEVICE_ATTR_RO(fan1_fault);
495 static DEVICE_ATTR_RW(pwm1_enable);
497 /* sensors present on all models */
498 static struct attribute *emc2103_attributes[] = {
499 &sensor_dev_attr_temp1_input.dev_attr.attr,
500 &sensor_dev_attr_temp1_min.dev_attr.attr,
501 &sensor_dev_attr_temp1_max.dev_attr.attr,
502 &sensor_dev_attr_temp1_fault.dev_attr.attr,
503 &sensor_dev_attr_temp1_min_alarm.dev_attr.attr,
504 &sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
505 &sensor_dev_attr_temp2_input.dev_attr.attr,
506 &sensor_dev_attr_temp2_min.dev_attr.attr,
507 &sensor_dev_attr_temp2_max.dev_attr.attr,
508 &sensor_dev_attr_temp2_fault.dev_attr.attr,
509 &sensor_dev_attr_temp2_min_alarm.dev_attr.attr,
510 &sensor_dev_attr_temp2_max_alarm.dev_attr.attr,
511 &dev_attr_fan1_input.attr,
512 &dev_attr_fan1_div.attr,
513 &dev_attr_fan1_target.attr,
514 &dev_attr_fan1_fault.attr,
515 &dev_attr_pwm1_enable.attr,
519 /* extra temperature sensors only present on 2103-2 and 2103-4 */
520 static struct attribute *emc2103_attributes_temp3[] = {
521 &sensor_dev_attr_temp3_input.dev_attr.attr,
522 &sensor_dev_attr_temp3_min.dev_attr.attr,
523 &sensor_dev_attr_temp3_max.dev_attr.attr,
524 &sensor_dev_attr_temp3_fault.dev_attr.attr,
525 &sensor_dev_attr_temp3_min_alarm.dev_attr.attr,
526 &sensor_dev_attr_temp3_max_alarm.dev_attr.attr,
530 /* extra temperature sensors only present on 2103-2 and 2103-4 in APD mode */
531 static struct attribute *emc2103_attributes_temp4[] = {
532 &sensor_dev_attr_temp4_input.dev_attr.attr,
533 &sensor_dev_attr_temp4_min.dev_attr.attr,
534 &sensor_dev_attr_temp4_max.dev_attr.attr,
535 &sensor_dev_attr_temp4_fault.dev_attr.attr,
536 &sensor_dev_attr_temp4_min_alarm.dev_attr.attr,
537 &sensor_dev_attr_temp4_max_alarm.dev_attr.attr,
541 static const struct attribute_group emc2103_group = {
542 .attrs = emc2103_attributes,
545 static const struct attribute_group emc2103_temp3_group = {
546 .attrs = emc2103_attributes_temp3,
549 static const struct attribute_group emc2103_temp4_group = {
550 .attrs = emc2103_attributes_temp4,
554 emc2103_probe(struct i2c_client *client)
556 struct emc2103_data *data;
557 struct device *hwmon_dev;
560 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
563 data = devm_kzalloc(&client->dev, sizeof(struct emc2103_data),
568 i2c_set_clientdata(client, data);
569 data->client = client;
570 mutex_init(&data->update_lock);
572 /* 2103-2 and 2103-4 have 3 external diodes, 2103-1 has 1 */
573 status = i2c_smbus_read_byte_data(client, REG_PRODUCT_ID);
574 if (status == 0x24) {
575 /* 2103-1 only has 1 external diode */
576 data->temp_count = 2;
578 /* 2103-2 and 2103-4 have 3 or 4 external diodes */
579 status = i2c_smbus_read_byte_data(client, REG_CONF1);
581 dev_dbg(&client->dev, "reg 0x%02x, err %d\n", REG_CONF1,
586 /* detect current state of hardware */
587 data->temp_count = (status & 0x01) ? 4 : 3;
589 /* force APD state if module parameter is set */
591 /* force APD mode off */
592 data->temp_count = 3;
594 i2c_smbus_write_byte_data(client, REG_CONF1, status);
595 } else if (apd == 1) {
596 /* force APD mode on */
597 data->temp_count = 4;
599 i2c_smbus_write_byte_data(client, REG_CONF1, status);
604 data->groups[idx++] = &emc2103_group;
605 if (data->temp_count >= 3)
606 data->groups[idx++] = &emc2103_temp3_group;
607 if (data->temp_count == 4)
608 data->groups[idx++] = &emc2103_temp4_group;
610 hwmon_dev = devm_hwmon_device_register_with_groups(&client->dev,
613 if (IS_ERR(hwmon_dev))
614 return PTR_ERR(hwmon_dev);
616 dev_info(&client->dev, "%s: sensor '%s'\n",
617 dev_name(hwmon_dev), client->name);
622 static const struct i2c_device_id emc2103_ids[] = {
626 MODULE_DEVICE_TABLE(i2c, emc2103_ids);
628 /* Return 0 if detection is successful, -ENODEV otherwise */
630 emc2103_detect(struct i2c_client *new_client, struct i2c_board_info *info)
632 struct i2c_adapter *adapter = new_client->adapter;
633 int manufacturer, product;
635 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
638 manufacturer = i2c_smbus_read_byte_data(new_client, REG_MFG_ID);
639 if (manufacturer != 0x5D)
642 product = i2c_smbus_read_byte_data(new_client, REG_PRODUCT_ID);
643 if ((product != 0x24) && (product != 0x26))
646 strscpy(info->type, "emc2103", I2C_NAME_SIZE);
651 static struct i2c_driver emc2103_driver = {
652 .class = I2C_CLASS_HWMON,
656 .probe = emc2103_probe,
657 .id_table = emc2103_ids,
658 .detect = emc2103_detect,
659 .address_list = normal_i2c,
662 module_i2c_driver(emc2103_driver);
664 MODULE_AUTHOR("Steve Glendinning <steve.glendinning@shawell.net>");
665 MODULE_DESCRIPTION("SMSC EMC2103 hwmon driver");
666 MODULE_LICENSE("GPL");