xref: /linux/drivers/hwmon/emc2103.c (revision 0be3ff0c)
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 */
18 static const unsigned short normal_i2c[] = { 0x2E, I2C_CLIENT_END };
19 
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 };
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  */
46 static int apd = -1;
47 module_param(apd, bint, 0);
48 MODULE_PARM_DESC(apd, "Set to zero to disable anti-parallel diode mode");
49 
50 struct temperature {
51 	s8	degrees;
52 	u8	fraction;	/* 0-7 multiples of 0.125 */
53 };
54 
55 struct 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 
73 static 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 
85 static 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, &degrees) < 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 
100 static 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 
114 static 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 
122 static 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 
135 static 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 
174 static ssize_t
175 temp_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 
184 static ssize_t
185 temp_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 
193 static ssize_t
194 temp_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 
202 static ssize_t
203 temp_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 
211 static ssize_t
212 temp_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 
221 static ssize_t
222 temp_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 
231 static 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 
253 static 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 
275 static ssize_t
276 fan1_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 
285 static ssize_t
286 fan1_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  */
299 static 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 
360 static ssize_t
361 fan1_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 
374 static 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 
404 static ssize_t
405 fan1_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 
412 static ssize_t
413 pwm1_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 
419 static 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);
457 err:
458 	mutex_unlock(&data->update_lock);
459 	return count;
460 }
461 
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);
468 
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);
475 
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);
482 
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);
489 
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);
494 
495 static DEVICE_ATTR_RW(pwm1_enable);
496 
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,
516 	NULL
517 };
518 
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,
527 	NULL
528 };
529 
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,
538 	NULL
539 };
540 
541 static const struct attribute_group emc2103_group = {
542 	.attrs = emc2103_attributes,
543 };
544 
545 static const struct attribute_group emc2103_temp3_group = {
546 	.attrs = emc2103_attributes_temp3,
547 };
548 
549 static const struct attribute_group emc2103_temp4_group = {
550 	.attrs = emc2103_attributes_temp4,
551 };
552 
553 static int
554 emc2103_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 
622 static const struct i2c_device_id emc2103_ids[] = {
623 	{ "emc2103", 0, },
624 	{ /* LIST END */ }
625 };
626 MODULE_DEVICE_TABLE(i2c, emc2103_ids);
627 
628 /* Return 0 if detection is successful, -ENODEV otherwise */
629 static int
630 emc2103_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 	strlcpy(info->type, "emc2103", I2C_NAME_SIZE);
647 
648 	return 0;
649 }
650 
651 static struct i2c_driver emc2103_driver = {
652 	.class		= I2C_CLASS_HWMON,
653 	.driver = {
654 		.name	= "emc2103",
655 	},
656 	.probe_new	= emc2103_probe,
657 	.id_table	= emc2103_ids,
658 	.detect		= emc2103_detect,
659 	.address_list	= normal_i2c,
660 };
661 
662 module_i2c_driver(emc2103_driver);
663 
664 MODULE_AUTHOR("Steve Glendinning <steve.glendinning@shawell.net>");
665 MODULE_DESCRIPTION("SMSC EMC2103 hwmon driver");
666 MODULE_LICENSE("GPL");
667