Loading...
1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * emc2103.c - Support for SMSC EMC2103
4 * Copyright (c) 2010 SMSC
5 */
6
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>
16
17/* Addresses scanned */
18static const unsigned short normal_i2c[] = { 0x2E, I2C_CLIENT_END };
19
20static const u8 REG_TEMP[4] = { 0x00, 0x02, 0x04, 0x06 };
21static const u8 REG_TEMP_MIN[4] = { 0x3c, 0x38, 0x39, 0x3a };
22static const u8 REG_TEMP_MAX[4] = { 0x34, 0x30, 0x31, 0x32 };
23
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
34
35/* equation 4 from datasheet: rpm = (3932160 * multipler) / count */
36#define FAN_RPM_FACTOR 3932160
37
38/*
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
45 */
46static int apd = -1;
47module_param(apd, bint, 0);
48MODULE_PARM_DESC(apd, "Set to zero to disable anti-parallel diode mode");
49
50struct temperature {
51 s8 degrees;
52 u8 fraction; /* 0-7 multiples of 0.125 */
53};
54
55struct emc2103_data {
56 struct i2c_client *client;
57 const struct attribute_group *groups[4];
58 struct mutex update_lock;
59 bool valid; /* registers are valid */
60 bool fan_rpm_control;
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 */
66 u8 temp_min_alarm;
67 u8 temp_max_alarm;
68 u8 fan_multiplier;
69 u16 fan_tach;
70 u16 fan_target;
71};
72
73static int read_u8_from_i2c(struct i2c_client *client, u8 i2c_reg, u8 *output)
74{
75 int status = i2c_smbus_read_byte_data(client, i2c_reg);
76 if (status < 0) {
77 dev_warn(&client->dev, "reg 0x%02x, err %d\n",
78 i2c_reg, status);
79 } else {
80 *output = status;
81 }
82 return status;
83}
84
85static void read_temp_from_i2c(struct i2c_client *client, u8 i2c_reg,
86 struct temperature *temp)
87{
88 u8 degrees, fractional;
89
90 if (read_u8_from_i2c(client, i2c_reg, °rees) < 0)
91 return;
92
93 if (read_u8_from_i2c(client, i2c_reg + 1, &fractional) < 0)
94 return;
95
96 temp->degrees = degrees;
97 temp->fraction = (fractional & 0xe0) >> 5;
98}
99
100static void read_fan_from_i2c(struct i2c_client *client, u16 *output,
101 u8 hi_addr, u8 lo_addr)
102{
103 u8 high_byte, lo_byte;
104
105 if (read_u8_from_i2c(client, hi_addr, &high_byte) < 0)
106 return;
107
108 if (read_u8_from_i2c(client, lo_addr, &lo_byte) < 0)
109 return;
110
111 *output = ((u16)high_byte << 5) | (lo_byte >> 3);
112}
113
114static void write_fan_target_to_i2c(struct i2c_client *client, u16 new_target)
115{
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);
120}
121
122static void read_fan_config_from_i2c(struct i2c_client *client)
123
124{
125 struct emc2103_data *data = i2c_get_clientdata(client);
126 u8 conf1;
127
128 if (read_u8_from_i2c(client, REG_FAN_CONF1, &conf1) < 0)
129 return;
130
131 data->fan_multiplier = 1 << ((conf1 & 0x60) >> 5);
132 data->fan_rpm_control = (conf1 & 0x80) != 0;
133}
134
135static struct emc2103_data *emc2103_update_device(struct device *dev)
136{
137 struct emc2103_data *data = dev_get_drvdata(dev);
138 struct i2c_client *client = data->client;
139
140 mutex_lock(&data->update_lock);
141
142 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
143 || !data->valid) {
144 int i;
145
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],
149 &data->temp_min[i]);
150 read_u8_from_i2c(client, REG_TEMP_MAX[i],
151 &data->temp_max[i]);
152 }
153
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);
158
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);
164
165 data->last_updated = jiffies;
166 data->valid = true;
167 }
168
169 mutex_unlock(&data->update_lock);
170
171 return data;
172}
173
174static ssize_t
175temp_show(struct device *dev, struct device_attribute *da, char *buf)
176{
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);
182}
183
184static ssize_t
185temp_min_show(struct device *dev, struct device_attribute *da, char *buf)
186{
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);
191}
192
193static ssize_t
194temp_max_show(struct device *dev, struct device_attribute *da, char *buf)
195{
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);
200}
201
202static ssize_t
203temp_fault_show(struct device *dev, struct device_attribute *da, char *buf)
204{
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);
209}
210
211static ssize_t
212temp_min_alarm_show(struct device *dev, struct device_attribute *da,
213 char *buf)
214{
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);
219}
220
221static ssize_t
222temp_max_alarm_show(struct device *dev, struct device_attribute *da,
223 char *buf)
224{
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);
229}
230
231static ssize_t temp_min_store(struct device *dev, struct device_attribute *da,
232 const char *buf, size_t count)
233{
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;
237 long val;
238
239 int result = kstrtol(buf, 10, &val);
240 if (result < 0)
241 return result;
242
243 val = DIV_ROUND_CLOSEST(clamp_val(val, -63000, 127000), 1000);
244
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);
249
250 return count;
251}
252
253static ssize_t temp_max_store(struct device *dev, struct device_attribute *da,
254 const char *buf, size_t count)
255{
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;
259 long val;
260
261 int result = kstrtol(buf, 10, &val);
262 if (result < 0)
263 return result;
264
265 val = DIV_ROUND_CLOSEST(clamp_val(val, -63000, 127000), 1000);
266
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);
271
272 return count;
273}
274
275static ssize_t
276fan1_input_show(struct device *dev, struct device_attribute *da, char *buf)
277{
278 struct emc2103_data *data = emc2103_update_device(dev);
279 int rpm = 0;
280 if (data->fan_tach != 0)
281 rpm = (FAN_RPM_FACTOR * data->fan_multiplier) / data->fan_tach;
282 return sprintf(buf, "%d\n", rpm);
283}
284
285static ssize_t
286fan1_div_show(struct device *dev, struct device_attribute *da, char *buf)
287{
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);
291}
292
293/*
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.
298 */
299static ssize_t fan1_div_store(struct device *dev, struct device_attribute *da,
300 const char *buf, size_t count)
301{
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;
305 long new_div;
306
307 int status = kstrtol(buf, 10, &new_div);
308 if (status < 0)
309 return status;
310
311 if (new_div == old_div) /* No change */
312 return count;
313
314 switch (new_div) {
315 case 1:
316 new_range_bits = 3;
317 break;
318 case 2:
319 new_range_bits = 2;
320 break;
321 case 4:
322 new_range_bits = 1;
323 break;
324 case 8:
325 new_range_bits = 0;
326 break;
327 default:
328 return -EINVAL;
329 }
330
331 mutex_lock(&data->update_lock);
332
333 status = i2c_smbus_read_byte_data(client, REG_FAN_CONF1);
334 if (status < 0) {
335 dev_dbg(&client->dev, "reg 0x%02x, err %d\n",
336 REG_FAN_CONF1, status);
337 mutex_unlock(&data->update_lock);
338 return status;
339 }
340 status &= 0x9F;
341 status |= (new_range_bits << 5);
342 i2c_smbus_write_byte_data(client, REG_FAN_CONF1, status);
343
344 data->fan_multiplier = 8 / new_div;
345
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);
351 }
352
353 /* invalidate data to force re-read from hardware */
354 data->valid = false;
355
356 mutex_unlock(&data->update_lock);
357 return count;
358}
359
360static ssize_t
361fan1_target_show(struct device *dev, struct device_attribute *da, char *buf)
362{
363 struct emc2103_data *data = emc2103_update_device(dev);
364 int rpm = 0;
365
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)
369 / data->fan_target;
370
371 return sprintf(buf, "%d\n", rpm);
372}
373
374static ssize_t fan1_target_store(struct device *dev,
375 struct device_attribute *da, const char *buf,
376 size_t count)
377{
378 struct emc2103_data *data = emc2103_update_device(dev);
379 struct i2c_client *client = data->client;
380 unsigned long rpm_target;
381
382 int result = kstrtoul(buf, 10, &rpm_target);
383 if (result < 0)
384 return result;
385
386 /* Datasheet states 16384 as maximum RPM target (table 3.2) */
387 rpm_target = clamp_val(rpm_target, 0, 16384);
388
389 mutex_lock(&data->update_lock);
390
391 if (rpm_target == 0)
392 data->fan_target = 0x1fff;
393 else
394 data->fan_target = clamp_val(
395 (FAN_RPM_FACTOR * data->fan_multiplier) / rpm_target,
396 0, 0x1fff);
397
398 write_fan_target_to_i2c(client, data->fan_target);
399
400 mutex_unlock(&data->update_lock);
401 return count;
402}
403
404static ssize_t
405fan1_fault_show(struct device *dev, struct device_attribute *da, char *buf)
406{
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);
410}
411
412static ssize_t
413pwm1_enable_show(struct device *dev, struct device_attribute *da, char *buf)
414{
415 struct emc2103_data *data = emc2103_update_device(dev);
416 return sprintf(buf, "%d\n", data->fan_rpm_control ? 3 : 0);
417}
418
419static ssize_t pwm1_enable_store(struct device *dev,
420 struct device_attribute *da, const char *buf,
421 size_t count)
422{
423 struct emc2103_data *data = dev_get_drvdata(dev);
424 struct i2c_client *client = data->client;
425 long new_value;
426 u8 conf_reg;
427
428 int result = kstrtol(buf, 10, &new_value);
429 if (result < 0)
430 return result;
431
432 mutex_lock(&data->update_lock);
433 switch (new_value) {
434 case 0:
435 data->fan_rpm_control = false;
436 break;
437 case 3:
438 data->fan_rpm_control = true;
439 break;
440 default:
441 count = -EINVAL;
442 goto err;
443 }
444
445 result = read_u8_from_i2c(client, REG_FAN_CONF1, &conf_reg);
446 if (result < 0) {
447 count = result;
448 goto err;
449 }
450
451 if (data->fan_rpm_control)
452 conf_reg |= 0x80;
453 else
454 conf_reg &= ~0x80;
455
456 i2c_smbus_write_byte_data(client, REG_FAN_CONF1, conf_reg);
457err:
458 mutex_unlock(&data->update_lock);
459 return count;
460}
461
462static SENSOR_DEVICE_ATTR_RO(temp1_input, temp, 0);
463static SENSOR_DEVICE_ATTR_RW(temp1_min, temp_min, 0);
464static SENSOR_DEVICE_ATTR_RW(temp1_max, temp_max, 0);
465static SENSOR_DEVICE_ATTR_RO(temp1_fault, temp_fault, 0);
466static SENSOR_DEVICE_ATTR_RO(temp1_min_alarm, temp_min_alarm, 0);
467static SENSOR_DEVICE_ATTR_RO(temp1_max_alarm, temp_max_alarm, 0);
468
469static SENSOR_DEVICE_ATTR_RO(temp2_input, temp, 1);
470static SENSOR_DEVICE_ATTR_RW(temp2_min, temp_min, 1);
471static SENSOR_DEVICE_ATTR_RW(temp2_max, temp_max, 1);
472static SENSOR_DEVICE_ATTR_RO(temp2_fault, temp_fault, 1);
473static SENSOR_DEVICE_ATTR_RO(temp2_min_alarm, temp_min_alarm, 1);
474static SENSOR_DEVICE_ATTR_RO(temp2_max_alarm, temp_max_alarm, 1);
475
476static SENSOR_DEVICE_ATTR_RO(temp3_input, temp, 2);
477static SENSOR_DEVICE_ATTR_RW(temp3_min, temp_min, 2);
478static SENSOR_DEVICE_ATTR_RW(temp3_max, temp_max, 2);
479static SENSOR_DEVICE_ATTR_RO(temp3_fault, temp_fault, 2);
480static SENSOR_DEVICE_ATTR_RO(temp3_min_alarm, temp_min_alarm, 2);
481static SENSOR_DEVICE_ATTR_RO(temp3_max_alarm, temp_max_alarm, 2);
482
483static SENSOR_DEVICE_ATTR_RO(temp4_input, temp, 3);
484static SENSOR_DEVICE_ATTR_RW(temp4_min, temp_min, 3);
485static SENSOR_DEVICE_ATTR_RW(temp4_max, temp_max, 3);
486static SENSOR_DEVICE_ATTR_RO(temp4_fault, temp_fault, 3);
487static SENSOR_DEVICE_ATTR_RO(temp4_min_alarm, temp_min_alarm, 3);
488static SENSOR_DEVICE_ATTR_RO(temp4_max_alarm, temp_max_alarm, 3);
489
490static DEVICE_ATTR_RO(fan1_input);
491static DEVICE_ATTR_RW(fan1_div);
492static DEVICE_ATTR_RW(fan1_target);
493static DEVICE_ATTR_RO(fan1_fault);
494
495static DEVICE_ATTR_RW(pwm1_enable);
496
497/* sensors present on all models */
498static 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,
516 NULL
517};
518
519/* extra temperature sensors only present on 2103-2 and 2103-4 */
520static 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,
527 NULL
528};
529
530/* extra temperature sensors only present on 2103-2 and 2103-4 in APD mode */
531static 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,
538 NULL
539};
540
541static const struct attribute_group emc2103_group = {
542 .attrs = emc2103_attributes,
543};
544
545static const struct attribute_group emc2103_temp3_group = {
546 .attrs = emc2103_attributes_temp3,
547};
548
549static const struct attribute_group emc2103_temp4_group = {
550 .attrs = emc2103_attributes_temp4,
551};
552
553static int
554emc2103_probe(struct i2c_client *client)
555{
556 struct emc2103_data *data;
557 struct device *hwmon_dev;
558 int status, idx = 0;
559
560 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
561 return -EIO;
562
563 data = devm_kzalloc(&client->dev, sizeof(struct emc2103_data),
564 GFP_KERNEL);
565 if (!data)
566 return -ENOMEM;
567
568 i2c_set_clientdata(client, data);
569 data->client = client;
570 mutex_init(&data->update_lock);
571
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;
577 } else {
578 /* 2103-2 and 2103-4 have 3 or 4 external diodes */
579 status = i2c_smbus_read_byte_data(client, REG_CONF1);
580 if (status < 0) {
581 dev_dbg(&client->dev, "reg 0x%02x, err %d\n", REG_CONF1,
582 status);
583 return status;
584 }
585
586 /* detect current state of hardware */
587 data->temp_count = (status & 0x01) ? 4 : 3;
588
589 /* force APD state if module parameter is set */
590 if (apd == 0) {
591 /* force APD mode off */
592 data->temp_count = 3;
593 status &= ~(0x01);
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;
598 status |= 0x01;
599 i2c_smbus_write_byte_data(client, REG_CONF1, status);
600 }
601 }
602
603 /* sysfs hooks */
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;
609
610 hwmon_dev = devm_hwmon_device_register_with_groups(&client->dev,
611 client->name, data,
612 data->groups);
613 if (IS_ERR(hwmon_dev))
614 return PTR_ERR(hwmon_dev);
615
616 dev_info(&client->dev, "%s: sensor '%s'\n",
617 dev_name(hwmon_dev), client->name);
618
619 return 0;
620}
621
622static const struct i2c_device_id emc2103_ids[] = {
623 { "emc2103", 0, },
624 { /* LIST END */ }
625};
626MODULE_DEVICE_TABLE(i2c, emc2103_ids);
627
628/* Return 0 if detection is successful, -ENODEV otherwise */
629static int
630emc2103_detect(struct i2c_client *new_client, struct i2c_board_info *info)
631{
632 struct i2c_adapter *adapter = new_client->adapter;
633 int manufacturer, product;
634
635 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
636 return -ENODEV;
637
638 manufacturer = i2c_smbus_read_byte_data(new_client, REG_MFG_ID);
639 if (manufacturer != 0x5D)
640 return -ENODEV;
641
642 product = i2c_smbus_read_byte_data(new_client, REG_PRODUCT_ID);
643 if ((product != 0x24) && (product != 0x26))
644 return -ENODEV;
645
646 strscpy(info->type, "emc2103", I2C_NAME_SIZE);
647
648 return 0;
649}
650
651static struct i2c_driver emc2103_driver = {
652 .class = I2C_CLASS_HWMON,
653 .driver = {
654 .name = "emc2103",
655 },
656 .probe = emc2103_probe,
657 .id_table = emc2103_ids,
658 .detect = emc2103_detect,
659 .address_list = normal_i2c,
660};
661
662module_i2c_driver(emc2103_driver);
663
664MODULE_AUTHOR("Steve Glendinning <steve.glendinning@shawell.net>");
665MODULE_DESCRIPTION("SMSC EMC2103 hwmon driver");
666MODULE_LICENSE("GPL");
1/*
2 * emc2103.c - Support for SMSC EMC2103
3 * Copyright (c) 2010 SMSC
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19
20#include <linux/module.h>
21#include <linux/init.h>
22#include <linux/slab.h>
23#include <linux/jiffies.h>
24#include <linux/i2c.h>
25#include <linux/hwmon.h>
26#include <linux/hwmon-sysfs.h>
27#include <linux/err.h>
28#include <linux/mutex.h>
29
30/* Addresses scanned */
31static const unsigned short normal_i2c[] = { 0x2E, I2C_CLIENT_END };
32
33static const u8 REG_TEMP[4] = { 0x00, 0x02, 0x04, 0x06 };
34static const u8 REG_TEMP_MIN[4] = { 0x3c, 0x38, 0x39, 0x3a };
35static const u8 REG_TEMP_MAX[4] = { 0x34, 0x30, 0x31, 0x32 };
36
37#define REG_CONF1 0x20
38#define REG_TEMP_MAX_ALARM 0x24
39#define REG_TEMP_MIN_ALARM 0x25
40#define REG_FAN_CONF1 0x42
41#define REG_FAN_TARGET_LO 0x4c
42#define REG_FAN_TARGET_HI 0x4d
43#define REG_FAN_TACH_HI 0x4e
44#define REG_FAN_TACH_LO 0x4f
45#define REG_PRODUCT_ID 0xfd
46#define REG_MFG_ID 0xfe
47
48/* equation 4 from datasheet: rpm = (3932160 * multipler) / count */
49#define FAN_RPM_FACTOR 3932160
50
51/*
52 * 2103-2 and 2103-4's 3rd temperature sensor can be connected to two diodes
53 * in anti-parallel mode, and in this configuration both can be read
54 * independently (so we have 4 temperature inputs). The device can't
55 * detect if it's connected in this mode, so we have to manually enable
56 * it. Default is to leave the device in the state it's already in (-1).
57 * This parameter allows APD mode to be optionally forced on or off
58 */
59static int apd = -1;
60module_param(apd, bint, 0);
61MODULE_PARM_DESC(init, "Set to zero to disable anti-parallel diode mode");
62
63struct temperature {
64 s8 degrees;
65 u8 fraction; /* 0-7 multiples of 0.125 */
66};
67
68struct emc2103_data {
69 struct i2c_client *client;
70 const struct attribute_group *groups[4];
71 struct mutex update_lock;
72 bool valid; /* registers are valid */
73 bool fan_rpm_control;
74 int temp_count; /* num of temp sensors */
75 unsigned long last_updated; /* in jiffies */
76 struct temperature temp[4]; /* internal + 3 external */
77 s8 temp_min[4]; /* no fractional part */
78 s8 temp_max[4]; /* no fractional part */
79 u8 temp_min_alarm;
80 u8 temp_max_alarm;
81 u8 fan_multiplier;
82 u16 fan_tach;
83 u16 fan_target;
84};
85
86static int read_u8_from_i2c(struct i2c_client *client, u8 i2c_reg, u8 *output)
87{
88 int status = i2c_smbus_read_byte_data(client, i2c_reg);
89 if (status < 0) {
90 dev_warn(&client->dev, "reg 0x%02x, err %d\n",
91 i2c_reg, status);
92 } else {
93 *output = status;
94 }
95 return status;
96}
97
98static void read_temp_from_i2c(struct i2c_client *client, u8 i2c_reg,
99 struct temperature *temp)
100{
101 u8 degrees, fractional;
102
103 if (read_u8_from_i2c(client, i2c_reg, °rees) < 0)
104 return;
105
106 if (read_u8_from_i2c(client, i2c_reg + 1, &fractional) < 0)
107 return;
108
109 temp->degrees = degrees;
110 temp->fraction = (fractional & 0xe0) >> 5;
111}
112
113static void read_fan_from_i2c(struct i2c_client *client, u16 *output,
114 u8 hi_addr, u8 lo_addr)
115{
116 u8 high_byte, lo_byte;
117
118 if (read_u8_from_i2c(client, hi_addr, &high_byte) < 0)
119 return;
120
121 if (read_u8_from_i2c(client, lo_addr, &lo_byte) < 0)
122 return;
123
124 *output = ((u16)high_byte << 5) | (lo_byte >> 3);
125}
126
127static void write_fan_target_to_i2c(struct i2c_client *client, u16 new_target)
128{
129 u8 high_byte = (new_target & 0x1fe0) >> 5;
130 u8 low_byte = (new_target & 0x001f) << 3;
131 i2c_smbus_write_byte_data(client, REG_FAN_TARGET_LO, low_byte);
132 i2c_smbus_write_byte_data(client, REG_FAN_TARGET_HI, high_byte);
133}
134
135static void read_fan_config_from_i2c(struct i2c_client *client)
136
137{
138 struct emc2103_data *data = i2c_get_clientdata(client);
139 u8 conf1;
140
141 if (read_u8_from_i2c(client, REG_FAN_CONF1, &conf1) < 0)
142 return;
143
144 data->fan_multiplier = 1 << ((conf1 & 0x60) >> 5);
145 data->fan_rpm_control = (conf1 & 0x80) != 0;
146}
147
148static struct emc2103_data *emc2103_update_device(struct device *dev)
149{
150 struct emc2103_data *data = dev_get_drvdata(dev);
151 struct i2c_client *client = data->client;
152
153 mutex_lock(&data->update_lock);
154
155 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
156 || !data->valid) {
157 int i;
158
159 for (i = 0; i < data->temp_count; i++) {
160 read_temp_from_i2c(client, REG_TEMP[i], &data->temp[i]);
161 read_u8_from_i2c(client, REG_TEMP_MIN[i],
162 &data->temp_min[i]);
163 read_u8_from_i2c(client, REG_TEMP_MAX[i],
164 &data->temp_max[i]);
165 }
166
167 read_u8_from_i2c(client, REG_TEMP_MIN_ALARM,
168 &data->temp_min_alarm);
169 read_u8_from_i2c(client, REG_TEMP_MAX_ALARM,
170 &data->temp_max_alarm);
171
172 read_fan_from_i2c(client, &data->fan_tach,
173 REG_FAN_TACH_HI, REG_FAN_TACH_LO);
174 read_fan_from_i2c(client, &data->fan_target,
175 REG_FAN_TARGET_HI, REG_FAN_TARGET_LO);
176 read_fan_config_from_i2c(client);
177
178 data->last_updated = jiffies;
179 data->valid = true;
180 }
181
182 mutex_unlock(&data->update_lock);
183
184 return data;
185}
186
187static ssize_t
188show_temp(struct device *dev, struct device_attribute *da, char *buf)
189{
190 int nr = to_sensor_dev_attr(da)->index;
191 struct emc2103_data *data = emc2103_update_device(dev);
192 int millidegrees = data->temp[nr].degrees * 1000
193 + data->temp[nr].fraction * 125;
194 return sprintf(buf, "%d\n", millidegrees);
195}
196
197static ssize_t
198show_temp_min(struct device *dev, struct device_attribute *da, char *buf)
199{
200 int nr = to_sensor_dev_attr(da)->index;
201 struct emc2103_data *data = emc2103_update_device(dev);
202 int millidegrees = data->temp_min[nr] * 1000;
203 return sprintf(buf, "%d\n", millidegrees);
204}
205
206static ssize_t
207show_temp_max(struct device *dev, struct device_attribute *da, char *buf)
208{
209 int nr = to_sensor_dev_attr(da)->index;
210 struct emc2103_data *data = emc2103_update_device(dev);
211 int millidegrees = data->temp_max[nr] * 1000;
212 return sprintf(buf, "%d\n", millidegrees);
213}
214
215static ssize_t
216show_temp_fault(struct device *dev, struct device_attribute *da, char *buf)
217{
218 int nr = to_sensor_dev_attr(da)->index;
219 struct emc2103_data *data = emc2103_update_device(dev);
220 bool fault = (data->temp[nr].degrees == -128);
221 return sprintf(buf, "%d\n", fault ? 1 : 0);
222}
223
224static ssize_t
225show_temp_min_alarm(struct device *dev, struct device_attribute *da, char *buf)
226{
227 int nr = to_sensor_dev_attr(da)->index;
228 struct emc2103_data *data = emc2103_update_device(dev);
229 bool alarm = data->temp_min_alarm & (1 << nr);
230 return sprintf(buf, "%d\n", alarm ? 1 : 0);
231}
232
233static ssize_t
234show_temp_max_alarm(struct device *dev, struct device_attribute *da, char *buf)
235{
236 int nr = to_sensor_dev_attr(da)->index;
237 struct emc2103_data *data = emc2103_update_device(dev);
238 bool alarm = data->temp_max_alarm & (1 << nr);
239 return sprintf(buf, "%d\n", alarm ? 1 : 0);
240}
241
242static ssize_t set_temp_min(struct device *dev, struct device_attribute *da,
243 const char *buf, size_t count)
244{
245 int nr = to_sensor_dev_attr(da)->index;
246 struct emc2103_data *data = dev_get_drvdata(dev);
247 struct i2c_client *client = data->client;
248 long val;
249
250 int result = kstrtol(buf, 10, &val);
251 if (result < 0)
252 return result;
253
254 val = clamp_val(DIV_ROUND_CLOSEST(val, 1000), -63, 127);
255
256 mutex_lock(&data->update_lock);
257 data->temp_min[nr] = val;
258 i2c_smbus_write_byte_data(client, REG_TEMP_MIN[nr], val);
259 mutex_unlock(&data->update_lock);
260
261 return count;
262}
263
264static ssize_t set_temp_max(struct device *dev, struct device_attribute *da,
265 const char *buf, size_t count)
266{
267 int nr = to_sensor_dev_attr(da)->index;
268 struct emc2103_data *data = dev_get_drvdata(dev);
269 struct i2c_client *client = data->client;
270 long val;
271
272 int result = kstrtol(buf, 10, &val);
273 if (result < 0)
274 return result;
275
276 val = clamp_val(DIV_ROUND_CLOSEST(val, 1000), -63, 127);
277
278 mutex_lock(&data->update_lock);
279 data->temp_max[nr] = val;
280 i2c_smbus_write_byte_data(client, REG_TEMP_MAX[nr], val);
281 mutex_unlock(&data->update_lock);
282
283 return count;
284}
285
286static ssize_t
287show_fan(struct device *dev, struct device_attribute *da, char *buf)
288{
289 struct emc2103_data *data = emc2103_update_device(dev);
290 int rpm = 0;
291 if (data->fan_tach != 0)
292 rpm = (FAN_RPM_FACTOR * data->fan_multiplier) / data->fan_tach;
293 return sprintf(buf, "%d\n", rpm);
294}
295
296static ssize_t
297show_fan_div(struct device *dev, struct device_attribute *da, char *buf)
298{
299 struct emc2103_data *data = emc2103_update_device(dev);
300 int fan_div = 8 / data->fan_multiplier;
301 return sprintf(buf, "%d\n", fan_div);
302}
303
304/*
305 * Note: we also update the fan target here, because its value is
306 * determined in part by the fan clock divider. This follows the principle
307 * of least surprise; the user doesn't expect the fan target to change just
308 * because the divider changed.
309 */
310static ssize_t set_fan_div(struct device *dev, struct device_attribute *da,
311 const char *buf, size_t count)
312{
313 struct emc2103_data *data = emc2103_update_device(dev);
314 struct i2c_client *client = data->client;
315 int new_range_bits, old_div = 8 / data->fan_multiplier;
316 long new_div;
317
318 int status = kstrtol(buf, 10, &new_div);
319 if (status < 0)
320 return status;
321
322 if (new_div == old_div) /* No change */
323 return count;
324
325 switch (new_div) {
326 case 1:
327 new_range_bits = 3;
328 break;
329 case 2:
330 new_range_bits = 2;
331 break;
332 case 4:
333 new_range_bits = 1;
334 break;
335 case 8:
336 new_range_bits = 0;
337 break;
338 default:
339 return -EINVAL;
340 }
341
342 mutex_lock(&data->update_lock);
343
344 status = i2c_smbus_read_byte_data(client, REG_FAN_CONF1);
345 if (status < 0) {
346 dev_dbg(&client->dev, "reg 0x%02x, err %d\n",
347 REG_FAN_CONF1, status);
348 mutex_unlock(&data->update_lock);
349 return status;
350 }
351 status &= 0x9F;
352 status |= (new_range_bits << 5);
353 i2c_smbus_write_byte_data(client, REG_FAN_CONF1, status);
354
355 data->fan_multiplier = 8 / new_div;
356
357 /* update fan target if high byte is not disabled */
358 if ((data->fan_target & 0x1fe0) != 0x1fe0) {
359 u16 new_target = (data->fan_target * old_div) / new_div;
360 data->fan_target = min(new_target, (u16)0x1fff);
361 write_fan_target_to_i2c(client, data->fan_target);
362 }
363
364 /* invalidate data to force re-read from hardware */
365 data->valid = false;
366
367 mutex_unlock(&data->update_lock);
368 return count;
369}
370
371static ssize_t
372show_fan_target(struct device *dev, struct device_attribute *da, char *buf)
373{
374 struct emc2103_data *data = emc2103_update_device(dev);
375 int rpm = 0;
376
377 /* high byte of 0xff indicates disabled so return 0 */
378 if ((data->fan_target != 0) && ((data->fan_target & 0x1fe0) != 0x1fe0))
379 rpm = (FAN_RPM_FACTOR * data->fan_multiplier)
380 / data->fan_target;
381
382 return sprintf(buf, "%d\n", rpm);
383}
384
385static ssize_t set_fan_target(struct device *dev, struct device_attribute *da,
386 const char *buf, size_t count)
387{
388 struct emc2103_data *data = emc2103_update_device(dev);
389 struct i2c_client *client = data->client;
390 unsigned long rpm_target;
391
392 int result = kstrtoul(buf, 10, &rpm_target);
393 if (result < 0)
394 return result;
395
396 /* Datasheet states 16384 as maximum RPM target (table 3.2) */
397 rpm_target = clamp_val(rpm_target, 0, 16384);
398
399 mutex_lock(&data->update_lock);
400
401 if (rpm_target == 0)
402 data->fan_target = 0x1fff;
403 else
404 data->fan_target = clamp_val(
405 (FAN_RPM_FACTOR * data->fan_multiplier) / rpm_target,
406 0, 0x1fff);
407
408 write_fan_target_to_i2c(client, data->fan_target);
409
410 mutex_unlock(&data->update_lock);
411 return count;
412}
413
414static ssize_t
415show_fan_fault(struct device *dev, struct device_attribute *da, char *buf)
416{
417 struct emc2103_data *data = emc2103_update_device(dev);
418 bool fault = ((data->fan_tach & 0x1fe0) == 0x1fe0);
419 return sprintf(buf, "%d\n", fault ? 1 : 0);
420}
421
422static ssize_t
423show_pwm_enable(struct device *dev, struct device_attribute *da, char *buf)
424{
425 struct emc2103_data *data = emc2103_update_device(dev);
426 return sprintf(buf, "%d\n", data->fan_rpm_control ? 3 : 0);
427}
428
429static ssize_t set_pwm_enable(struct device *dev, struct device_attribute *da,
430 const char *buf, size_t count)
431{
432 struct emc2103_data *data = dev_get_drvdata(dev);
433 struct i2c_client *client = data->client;
434 long new_value;
435 u8 conf_reg;
436
437 int result = kstrtol(buf, 10, &new_value);
438 if (result < 0)
439 return result;
440
441 mutex_lock(&data->update_lock);
442 switch (new_value) {
443 case 0:
444 data->fan_rpm_control = false;
445 break;
446 case 3:
447 data->fan_rpm_control = true;
448 break;
449 default:
450 count = -EINVAL;
451 goto err;
452 }
453
454 result = read_u8_from_i2c(client, REG_FAN_CONF1, &conf_reg);
455 if (result) {
456 count = result;
457 goto err;
458 }
459
460 if (data->fan_rpm_control)
461 conf_reg |= 0x80;
462 else
463 conf_reg &= ~0x80;
464
465 i2c_smbus_write_byte_data(client, REG_FAN_CONF1, conf_reg);
466err:
467 mutex_unlock(&data->update_lock);
468 return count;
469}
470
471static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0);
472static SENSOR_DEVICE_ATTR(temp1_min, S_IRUGO | S_IWUSR, show_temp_min,
473 set_temp_min, 0);
474static SENSOR_DEVICE_ATTR(temp1_max, S_IRUGO | S_IWUSR, show_temp_max,
475 set_temp_max, 0);
476static SENSOR_DEVICE_ATTR(temp1_fault, S_IRUGO, show_temp_fault, NULL, 0);
477static SENSOR_DEVICE_ATTR(temp1_min_alarm, S_IRUGO, show_temp_min_alarm,
478 NULL, 0);
479static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, show_temp_max_alarm,
480 NULL, 0);
481
482static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp, NULL, 1);
483static SENSOR_DEVICE_ATTR(temp2_min, S_IRUGO | S_IWUSR, show_temp_min,
484 set_temp_min, 1);
485static SENSOR_DEVICE_ATTR(temp2_max, S_IRUGO | S_IWUSR, show_temp_max,
486 set_temp_max, 1);
487static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO, show_temp_fault, NULL, 1);
488static SENSOR_DEVICE_ATTR(temp2_min_alarm, S_IRUGO, show_temp_min_alarm,
489 NULL, 1);
490static SENSOR_DEVICE_ATTR(temp2_max_alarm, S_IRUGO, show_temp_max_alarm,
491 NULL, 1);
492
493static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO, show_temp, NULL, 2);
494static SENSOR_DEVICE_ATTR(temp3_min, S_IRUGO | S_IWUSR, show_temp_min,
495 set_temp_min, 2);
496static SENSOR_DEVICE_ATTR(temp3_max, S_IRUGO | S_IWUSR, show_temp_max,
497 set_temp_max, 2);
498static SENSOR_DEVICE_ATTR(temp3_fault, S_IRUGO, show_temp_fault, NULL, 2);
499static SENSOR_DEVICE_ATTR(temp3_min_alarm, S_IRUGO, show_temp_min_alarm,
500 NULL, 2);
501static SENSOR_DEVICE_ATTR(temp3_max_alarm, S_IRUGO, show_temp_max_alarm,
502 NULL, 2);
503
504static SENSOR_DEVICE_ATTR(temp4_input, S_IRUGO, show_temp, NULL, 3);
505static SENSOR_DEVICE_ATTR(temp4_min, S_IRUGO | S_IWUSR, show_temp_min,
506 set_temp_min, 3);
507static SENSOR_DEVICE_ATTR(temp4_max, S_IRUGO | S_IWUSR, show_temp_max,
508 set_temp_max, 3);
509static SENSOR_DEVICE_ATTR(temp4_fault, S_IRUGO, show_temp_fault, NULL, 3);
510static SENSOR_DEVICE_ATTR(temp4_min_alarm, S_IRUGO, show_temp_min_alarm,
511 NULL, 3);
512static SENSOR_DEVICE_ATTR(temp4_max_alarm, S_IRUGO, show_temp_max_alarm,
513 NULL, 3);
514
515static DEVICE_ATTR(fan1_input, S_IRUGO, show_fan, NULL);
516static DEVICE_ATTR(fan1_div, S_IRUGO | S_IWUSR, show_fan_div, set_fan_div);
517static DEVICE_ATTR(fan1_target, S_IRUGO | S_IWUSR, show_fan_target,
518 set_fan_target);
519static DEVICE_ATTR(fan1_fault, S_IRUGO, show_fan_fault, NULL);
520
521static DEVICE_ATTR(pwm1_enable, S_IRUGO | S_IWUSR, show_pwm_enable,
522 set_pwm_enable);
523
524/* sensors present on all models */
525static struct attribute *emc2103_attributes[] = {
526 &sensor_dev_attr_temp1_input.dev_attr.attr,
527 &sensor_dev_attr_temp1_min.dev_attr.attr,
528 &sensor_dev_attr_temp1_max.dev_attr.attr,
529 &sensor_dev_attr_temp1_fault.dev_attr.attr,
530 &sensor_dev_attr_temp1_min_alarm.dev_attr.attr,
531 &sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
532 &sensor_dev_attr_temp2_input.dev_attr.attr,
533 &sensor_dev_attr_temp2_min.dev_attr.attr,
534 &sensor_dev_attr_temp2_max.dev_attr.attr,
535 &sensor_dev_attr_temp2_fault.dev_attr.attr,
536 &sensor_dev_attr_temp2_min_alarm.dev_attr.attr,
537 &sensor_dev_attr_temp2_max_alarm.dev_attr.attr,
538 &dev_attr_fan1_input.attr,
539 &dev_attr_fan1_div.attr,
540 &dev_attr_fan1_target.attr,
541 &dev_attr_fan1_fault.attr,
542 &dev_attr_pwm1_enable.attr,
543 NULL
544};
545
546/* extra temperature sensors only present on 2103-2 and 2103-4 */
547static struct attribute *emc2103_attributes_temp3[] = {
548 &sensor_dev_attr_temp3_input.dev_attr.attr,
549 &sensor_dev_attr_temp3_min.dev_attr.attr,
550 &sensor_dev_attr_temp3_max.dev_attr.attr,
551 &sensor_dev_attr_temp3_fault.dev_attr.attr,
552 &sensor_dev_attr_temp3_min_alarm.dev_attr.attr,
553 &sensor_dev_attr_temp3_max_alarm.dev_attr.attr,
554 NULL
555};
556
557/* extra temperature sensors only present on 2103-2 and 2103-4 in APD mode */
558static struct attribute *emc2103_attributes_temp4[] = {
559 &sensor_dev_attr_temp4_input.dev_attr.attr,
560 &sensor_dev_attr_temp4_min.dev_attr.attr,
561 &sensor_dev_attr_temp4_max.dev_attr.attr,
562 &sensor_dev_attr_temp4_fault.dev_attr.attr,
563 &sensor_dev_attr_temp4_min_alarm.dev_attr.attr,
564 &sensor_dev_attr_temp4_max_alarm.dev_attr.attr,
565 NULL
566};
567
568static const struct attribute_group emc2103_group = {
569 .attrs = emc2103_attributes,
570};
571
572static const struct attribute_group emc2103_temp3_group = {
573 .attrs = emc2103_attributes_temp3,
574};
575
576static const struct attribute_group emc2103_temp4_group = {
577 .attrs = emc2103_attributes_temp4,
578};
579
580static int
581emc2103_probe(struct i2c_client *client, const struct i2c_device_id *id)
582{
583 struct emc2103_data *data;
584 struct device *hwmon_dev;
585 int status, idx = 0;
586
587 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
588 return -EIO;
589
590 data = devm_kzalloc(&client->dev, sizeof(struct emc2103_data),
591 GFP_KERNEL);
592 if (!data)
593 return -ENOMEM;
594
595 i2c_set_clientdata(client, data);
596 data->client = client;
597 mutex_init(&data->update_lock);
598
599 /* 2103-2 and 2103-4 have 3 external diodes, 2103-1 has 1 */
600 status = i2c_smbus_read_byte_data(client, REG_PRODUCT_ID);
601 if (status == 0x24) {
602 /* 2103-1 only has 1 external diode */
603 data->temp_count = 2;
604 } else {
605 /* 2103-2 and 2103-4 have 3 or 4 external diodes */
606 status = i2c_smbus_read_byte_data(client, REG_CONF1);
607 if (status < 0) {
608 dev_dbg(&client->dev, "reg 0x%02x, err %d\n", REG_CONF1,
609 status);
610 return status;
611 }
612
613 /* detect current state of hardware */
614 data->temp_count = (status & 0x01) ? 4 : 3;
615
616 /* force APD state if module parameter is set */
617 if (apd == 0) {
618 /* force APD mode off */
619 data->temp_count = 3;
620 status &= ~(0x01);
621 i2c_smbus_write_byte_data(client, REG_CONF1, status);
622 } else if (apd == 1) {
623 /* force APD mode on */
624 data->temp_count = 4;
625 status |= 0x01;
626 i2c_smbus_write_byte_data(client, REG_CONF1, status);
627 }
628 }
629
630 /* sysfs hooks */
631 data->groups[idx++] = &emc2103_group;
632 if (data->temp_count >= 3)
633 data->groups[idx++] = &emc2103_temp3_group;
634 if (data->temp_count == 4)
635 data->groups[idx++] = &emc2103_temp4_group;
636
637 hwmon_dev = devm_hwmon_device_register_with_groups(&client->dev,
638 client->name, data,
639 data->groups);
640 if (IS_ERR(hwmon_dev))
641 return PTR_ERR(hwmon_dev);
642
643 dev_info(&client->dev, "%s: sensor '%s'\n",
644 dev_name(hwmon_dev), client->name);
645
646 return 0;
647}
648
649static const struct i2c_device_id emc2103_ids[] = {
650 { "emc2103", 0, },
651 { /* LIST END */ }
652};
653MODULE_DEVICE_TABLE(i2c, emc2103_ids);
654
655/* Return 0 if detection is successful, -ENODEV otherwise */
656static int
657emc2103_detect(struct i2c_client *new_client, struct i2c_board_info *info)
658{
659 struct i2c_adapter *adapter = new_client->adapter;
660 int manufacturer, product;
661
662 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
663 return -ENODEV;
664
665 manufacturer = i2c_smbus_read_byte_data(new_client, REG_MFG_ID);
666 if (manufacturer != 0x5D)
667 return -ENODEV;
668
669 product = i2c_smbus_read_byte_data(new_client, REG_PRODUCT_ID);
670 if ((product != 0x24) && (product != 0x26))
671 return -ENODEV;
672
673 strlcpy(info->type, "emc2103", I2C_NAME_SIZE);
674
675 return 0;
676}
677
678static struct i2c_driver emc2103_driver = {
679 .class = I2C_CLASS_HWMON,
680 .driver = {
681 .name = "emc2103",
682 },
683 .probe = emc2103_probe,
684 .id_table = emc2103_ids,
685 .detect = emc2103_detect,
686 .address_list = normal_i2c,
687};
688
689module_i2c_driver(emc2103_driver);
690
691MODULE_AUTHOR("Steve Glendinning <steve.glendinning@shawell.net>");
692MODULE_DESCRIPTION("SMSC EMC2103 hwmon driver");
693MODULE_LICENSE("GPL");