xref: /openbmc/linux/drivers/hwmon/emc2103.c (revision c67e8ec0)
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 */
31 static const unsigned short normal_i2c[] = { 0x2E, I2C_CLIENT_END };
32 
33 static const u8 REG_TEMP[4] = { 0x00, 0x02, 0x04, 0x06 };
34 static const u8 REG_TEMP_MIN[4] = { 0x3c, 0x38, 0x39, 0x3a };
35 static 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  */
59 static int apd = -1;
60 module_param(apd, bint, 0);
61 MODULE_PARM_DESC(apd, "Set to zero to disable anti-parallel diode mode");
62 
63 struct temperature {
64 	s8	degrees;
65 	u8	fraction;	/* 0-7 multiples of 0.125 */
66 };
67 
68 struct 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 
86 static 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 
98 static 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, &degrees) < 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 
113 static 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 
127 static 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 
135 static 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 
148 static 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 
187 static ssize_t
188 temp_show(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 
197 static ssize_t
198 temp_min_show(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 
206 static ssize_t
207 temp_max_show(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 
215 static ssize_t
216 temp_fault_show(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 
224 static ssize_t
225 temp_min_alarm_show(struct device *dev, struct device_attribute *da,
226 		    char *buf)
227 {
228 	int nr = to_sensor_dev_attr(da)->index;
229 	struct emc2103_data *data = emc2103_update_device(dev);
230 	bool alarm = data->temp_min_alarm & (1 << nr);
231 	return sprintf(buf, "%d\n", alarm ? 1 : 0);
232 }
233 
234 static ssize_t
235 temp_max_alarm_show(struct device *dev, struct device_attribute *da,
236 		    char *buf)
237 {
238 	int nr = to_sensor_dev_attr(da)->index;
239 	struct emc2103_data *data = emc2103_update_device(dev);
240 	bool alarm = data->temp_max_alarm & (1 << nr);
241 	return sprintf(buf, "%d\n", alarm ? 1 : 0);
242 }
243 
244 static ssize_t temp_min_store(struct device *dev, struct device_attribute *da,
245 			      const char *buf, size_t count)
246 {
247 	int nr = to_sensor_dev_attr(da)->index;
248 	struct emc2103_data *data = dev_get_drvdata(dev);
249 	struct i2c_client *client = data->client;
250 	long val;
251 
252 	int result = kstrtol(buf, 10, &val);
253 	if (result < 0)
254 		return result;
255 
256 	val = DIV_ROUND_CLOSEST(clamp_val(val, -63000, 127000), 1000);
257 
258 	mutex_lock(&data->update_lock);
259 	data->temp_min[nr] = val;
260 	i2c_smbus_write_byte_data(client, REG_TEMP_MIN[nr], val);
261 	mutex_unlock(&data->update_lock);
262 
263 	return count;
264 }
265 
266 static ssize_t temp_max_store(struct device *dev, struct device_attribute *da,
267 			      const char *buf, size_t count)
268 {
269 	int nr = to_sensor_dev_attr(da)->index;
270 	struct emc2103_data *data = dev_get_drvdata(dev);
271 	struct i2c_client *client = data->client;
272 	long val;
273 
274 	int result = kstrtol(buf, 10, &val);
275 	if (result < 0)
276 		return result;
277 
278 	val = DIV_ROUND_CLOSEST(clamp_val(val, -63000, 127000), 1000);
279 
280 	mutex_lock(&data->update_lock);
281 	data->temp_max[nr] = val;
282 	i2c_smbus_write_byte_data(client, REG_TEMP_MAX[nr], val);
283 	mutex_unlock(&data->update_lock);
284 
285 	return count;
286 }
287 
288 static ssize_t
289 fan1_input_show(struct device *dev, struct device_attribute *da, char *buf)
290 {
291 	struct emc2103_data *data = emc2103_update_device(dev);
292 	int rpm = 0;
293 	if (data->fan_tach != 0)
294 		rpm = (FAN_RPM_FACTOR * data->fan_multiplier) / data->fan_tach;
295 	return sprintf(buf, "%d\n", rpm);
296 }
297 
298 static ssize_t
299 fan1_div_show(struct device *dev, struct device_attribute *da, char *buf)
300 {
301 	struct emc2103_data *data = emc2103_update_device(dev);
302 	int fan_div = 8 / data->fan_multiplier;
303 	return sprintf(buf, "%d\n", fan_div);
304 }
305 
306 /*
307  * Note: we also update the fan target here, because its value is
308  * determined in part by the fan clock divider.  This follows the principle
309  * of least surprise; the user doesn't expect the fan target to change just
310  * because the divider changed.
311  */
312 static ssize_t fan1_div_store(struct device *dev, struct device_attribute *da,
313 			      const char *buf, size_t count)
314 {
315 	struct emc2103_data *data = emc2103_update_device(dev);
316 	struct i2c_client *client = data->client;
317 	int new_range_bits, old_div = 8 / data->fan_multiplier;
318 	long new_div;
319 
320 	int status = kstrtol(buf, 10, &new_div);
321 	if (status < 0)
322 		return status;
323 
324 	if (new_div == old_div) /* No change */
325 		return count;
326 
327 	switch (new_div) {
328 	case 1:
329 		new_range_bits = 3;
330 		break;
331 	case 2:
332 		new_range_bits = 2;
333 		break;
334 	case 4:
335 		new_range_bits = 1;
336 		break;
337 	case 8:
338 		new_range_bits = 0;
339 		break;
340 	default:
341 		return -EINVAL;
342 	}
343 
344 	mutex_lock(&data->update_lock);
345 
346 	status = i2c_smbus_read_byte_data(client, REG_FAN_CONF1);
347 	if (status < 0) {
348 		dev_dbg(&client->dev, "reg 0x%02x, err %d\n",
349 			REG_FAN_CONF1, status);
350 		mutex_unlock(&data->update_lock);
351 		return status;
352 	}
353 	status &= 0x9F;
354 	status |= (new_range_bits << 5);
355 	i2c_smbus_write_byte_data(client, REG_FAN_CONF1, status);
356 
357 	data->fan_multiplier = 8 / new_div;
358 
359 	/* update fan target if high byte is not disabled */
360 	if ((data->fan_target & 0x1fe0) != 0x1fe0) {
361 		u16 new_target = (data->fan_target * old_div) / new_div;
362 		data->fan_target = min(new_target, (u16)0x1fff);
363 		write_fan_target_to_i2c(client, data->fan_target);
364 	}
365 
366 	/* invalidate data to force re-read from hardware */
367 	data->valid = false;
368 
369 	mutex_unlock(&data->update_lock);
370 	return count;
371 }
372 
373 static ssize_t
374 fan1_target_show(struct device *dev, struct device_attribute *da, char *buf)
375 {
376 	struct emc2103_data *data = emc2103_update_device(dev);
377 	int rpm = 0;
378 
379 	/* high byte of 0xff indicates disabled so return 0 */
380 	if ((data->fan_target != 0) && ((data->fan_target & 0x1fe0) != 0x1fe0))
381 		rpm = (FAN_RPM_FACTOR * data->fan_multiplier)
382 			/ data->fan_target;
383 
384 	return sprintf(buf, "%d\n", rpm);
385 }
386 
387 static ssize_t fan1_target_store(struct device *dev,
388 				 struct device_attribute *da, const char *buf,
389 				 size_t count)
390 {
391 	struct emc2103_data *data = emc2103_update_device(dev);
392 	struct i2c_client *client = data->client;
393 	unsigned long rpm_target;
394 
395 	int result = kstrtoul(buf, 10, &rpm_target);
396 	if (result < 0)
397 		return result;
398 
399 	/* Datasheet states 16384 as maximum RPM target (table 3.2) */
400 	rpm_target = clamp_val(rpm_target, 0, 16384);
401 
402 	mutex_lock(&data->update_lock);
403 
404 	if (rpm_target == 0)
405 		data->fan_target = 0x1fff;
406 	else
407 		data->fan_target = clamp_val(
408 			(FAN_RPM_FACTOR * data->fan_multiplier) / rpm_target,
409 			0, 0x1fff);
410 
411 	write_fan_target_to_i2c(client, data->fan_target);
412 
413 	mutex_unlock(&data->update_lock);
414 	return count;
415 }
416 
417 static ssize_t
418 fan1_fault_show(struct device *dev, struct device_attribute *da, char *buf)
419 {
420 	struct emc2103_data *data = emc2103_update_device(dev);
421 	bool fault = ((data->fan_tach & 0x1fe0) == 0x1fe0);
422 	return sprintf(buf, "%d\n", fault ? 1 : 0);
423 }
424 
425 static ssize_t
426 pwm1_enable_show(struct device *dev, struct device_attribute *da, char *buf)
427 {
428 	struct emc2103_data *data = emc2103_update_device(dev);
429 	return sprintf(buf, "%d\n", data->fan_rpm_control ? 3 : 0);
430 }
431 
432 static ssize_t pwm1_enable_store(struct device *dev,
433 				 struct device_attribute *da, const char *buf,
434 				 size_t count)
435 {
436 	struct emc2103_data *data = dev_get_drvdata(dev);
437 	struct i2c_client *client = data->client;
438 	long new_value;
439 	u8 conf_reg;
440 
441 	int result = kstrtol(buf, 10, &new_value);
442 	if (result < 0)
443 		return result;
444 
445 	mutex_lock(&data->update_lock);
446 	switch (new_value) {
447 	case 0:
448 		data->fan_rpm_control = false;
449 		break;
450 	case 3:
451 		data->fan_rpm_control = true;
452 		break;
453 	default:
454 		count = -EINVAL;
455 		goto err;
456 	}
457 
458 	result = read_u8_from_i2c(client, REG_FAN_CONF1, &conf_reg);
459 	if (result) {
460 		count = result;
461 		goto err;
462 	}
463 
464 	if (data->fan_rpm_control)
465 		conf_reg |= 0x80;
466 	else
467 		conf_reg &= ~0x80;
468 
469 	i2c_smbus_write_byte_data(client, REG_FAN_CONF1, conf_reg);
470 err:
471 	mutex_unlock(&data->update_lock);
472 	return count;
473 }
474 
475 static SENSOR_DEVICE_ATTR_RO(temp1_input, temp, 0);
476 static SENSOR_DEVICE_ATTR_RW(temp1_min, temp_min, 0);
477 static SENSOR_DEVICE_ATTR_RW(temp1_max, temp_max, 0);
478 static SENSOR_DEVICE_ATTR_RO(temp1_fault, temp_fault, 0);
479 static SENSOR_DEVICE_ATTR_RO(temp1_min_alarm, temp_min_alarm, 0);
480 static SENSOR_DEVICE_ATTR_RO(temp1_max_alarm, temp_max_alarm, 0);
481 
482 static SENSOR_DEVICE_ATTR_RO(temp2_input, temp, 1);
483 static SENSOR_DEVICE_ATTR_RW(temp2_min, temp_min, 1);
484 static SENSOR_DEVICE_ATTR_RW(temp2_max, temp_max, 1);
485 static SENSOR_DEVICE_ATTR_RO(temp2_fault, temp_fault, 1);
486 static SENSOR_DEVICE_ATTR_RO(temp2_min_alarm, temp_min_alarm, 1);
487 static SENSOR_DEVICE_ATTR_RO(temp2_max_alarm, temp_max_alarm, 1);
488 
489 static SENSOR_DEVICE_ATTR_RO(temp3_input, temp, 2);
490 static SENSOR_DEVICE_ATTR_RW(temp3_min, temp_min, 2);
491 static SENSOR_DEVICE_ATTR_RW(temp3_max, temp_max, 2);
492 static SENSOR_DEVICE_ATTR_RO(temp3_fault, temp_fault, 2);
493 static SENSOR_DEVICE_ATTR_RO(temp3_min_alarm, temp_min_alarm, 2);
494 static SENSOR_DEVICE_ATTR_RO(temp3_max_alarm, temp_max_alarm, 2);
495 
496 static SENSOR_DEVICE_ATTR_RO(temp4_input, temp, 3);
497 static SENSOR_DEVICE_ATTR_RW(temp4_min, temp_min, 3);
498 static SENSOR_DEVICE_ATTR_RW(temp4_max, temp_max, 3);
499 static SENSOR_DEVICE_ATTR_RO(temp4_fault, temp_fault, 3);
500 static SENSOR_DEVICE_ATTR_RO(temp4_min_alarm, temp_min_alarm, 3);
501 static SENSOR_DEVICE_ATTR_RO(temp4_max_alarm, temp_max_alarm, 3);
502 
503 static DEVICE_ATTR_RO(fan1_input);
504 static DEVICE_ATTR_RW(fan1_div);
505 static DEVICE_ATTR_RW(fan1_target);
506 static DEVICE_ATTR_RO(fan1_fault);
507 
508 static DEVICE_ATTR_RW(pwm1_enable);
509 
510 /* sensors present on all models */
511 static struct attribute *emc2103_attributes[] = {
512 	&sensor_dev_attr_temp1_input.dev_attr.attr,
513 	&sensor_dev_attr_temp1_min.dev_attr.attr,
514 	&sensor_dev_attr_temp1_max.dev_attr.attr,
515 	&sensor_dev_attr_temp1_fault.dev_attr.attr,
516 	&sensor_dev_attr_temp1_min_alarm.dev_attr.attr,
517 	&sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
518 	&sensor_dev_attr_temp2_input.dev_attr.attr,
519 	&sensor_dev_attr_temp2_min.dev_attr.attr,
520 	&sensor_dev_attr_temp2_max.dev_attr.attr,
521 	&sensor_dev_attr_temp2_fault.dev_attr.attr,
522 	&sensor_dev_attr_temp2_min_alarm.dev_attr.attr,
523 	&sensor_dev_attr_temp2_max_alarm.dev_attr.attr,
524 	&dev_attr_fan1_input.attr,
525 	&dev_attr_fan1_div.attr,
526 	&dev_attr_fan1_target.attr,
527 	&dev_attr_fan1_fault.attr,
528 	&dev_attr_pwm1_enable.attr,
529 	NULL
530 };
531 
532 /* extra temperature sensors only present on 2103-2 and 2103-4 */
533 static struct attribute *emc2103_attributes_temp3[] = {
534 	&sensor_dev_attr_temp3_input.dev_attr.attr,
535 	&sensor_dev_attr_temp3_min.dev_attr.attr,
536 	&sensor_dev_attr_temp3_max.dev_attr.attr,
537 	&sensor_dev_attr_temp3_fault.dev_attr.attr,
538 	&sensor_dev_attr_temp3_min_alarm.dev_attr.attr,
539 	&sensor_dev_attr_temp3_max_alarm.dev_attr.attr,
540 	NULL
541 };
542 
543 /* extra temperature sensors only present on 2103-2 and 2103-4 in APD mode */
544 static struct attribute *emc2103_attributes_temp4[] = {
545 	&sensor_dev_attr_temp4_input.dev_attr.attr,
546 	&sensor_dev_attr_temp4_min.dev_attr.attr,
547 	&sensor_dev_attr_temp4_max.dev_attr.attr,
548 	&sensor_dev_attr_temp4_fault.dev_attr.attr,
549 	&sensor_dev_attr_temp4_min_alarm.dev_attr.attr,
550 	&sensor_dev_attr_temp4_max_alarm.dev_attr.attr,
551 	NULL
552 };
553 
554 static const struct attribute_group emc2103_group = {
555 	.attrs = emc2103_attributes,
556 };
557 
558 static const struct attribute_group emc2103_temp3_group = {
559 	.attrs = emc2103_attributes_temp3,
560 };
561 
562 static const struct attribute_group emc2103_temp4_group = {
563 	.attrs = emc2103_attributes_temp4,
564 };
565 
566 static int
567 emc2103_probe(struct i2c_client *client, const struct i2c_device_id *id)
568 {
569 	struct emc2103_data *data;
570 	struct device *hwmon_dev;
571 	int status, idx = 0;
572 
573 	if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
574 		return -EIO;
575 
576 	data = devm_kzalloc(&client->dev, sizeof(struct emc2103_data),
577 			    GFP_KERNEL);
578 	if (!data)
579 		return -ENOMEM;
580 
581 	i2c_set_clientdata(client, data);
582 	data->client = client;
583 	mutex_init(&data->update_lock);
584 
585 	/* 2103-2 and 2103-4 have 3 external diodes, 2103-1 has 1 */
586 	status = i2c_smbus_read_byte_data(client, REG_PRODUCT_ID);
587 	if (status == 0x24) {
588 		/* 2103-1 only has 1 external diode */
589 		data->temp_count = 2;
590 	} else {
591 		/* 2103-2 and 2103-4 have 3 or 4 external diodes */
592 		status = i2c_smbus_read_byte_data(client, REG_CONF1);
593 		if (status < 0) {
594 			dev_dbg(&client->dev, "reg 0x%02x, err %d\n", REG_CONF1,
595 				status);
596 			return status;
597 		}
598 
599 		/* detect current state of hardware */
600 		data->temp_count = (status & 0x01) ? 4 : 3;
601 
602 		/* force APD state if module parameter is set */
603 		if (apd == 0) {
604 			/* force APD mode off */
605 			data->temp_count = 3;
606 			status &= ~(0x01);
607 			i2c_smbus_write_byte_data(client, REG_CONF1, status);
608 		} else if (apd == 1) {
609 			/* force APD mode on */
610 			data->temp_count = 4;
611 			status |= 0x01;
612 			i2c_smbus_write_byte_data(client, REG_CONF1, status);
613 		}
614 	}
615 
616 	/* sysfs hooks */
617 	data->groups[idx++] = &emc2103_group;
618 	if (data->temp_count >= 3)
619 		data->groups[idx++] = &emc2103_temp3_group;
620 	if (data->temp_count == 4)
621 		data->groups[idx++] = &emc2103_temp4_group;
622 
623 	hwmon_dev = devm_hwmon_device_register_with_groups(&client->dev,
624 							   client->name, data,
625 							   data->groups);
626 	if (IS_ERR(hwmon_dev))
627 		return PTR_ERR(hwmon_dev);
628 
629 	dev_info(&client->dev, "%s: sensor '%s'\n",
630 		 dev_name(hwmon_dev), client->name);
631 
632 	return 0;
633 }
634 
635 static const struct i2c_device_id emc2103_ids[] = {
636 	{ "emc2103", 0, },
637 	{ /* LIST END */ }
638 };
639 MODULE_DEVICE_TABLE(i2c, emc2103_ids);
640 
641 /* Return 0 if detection is successful, -ENODEV otherwise */
642 static int
643 emc2103_detect(struct i2c_client *new_client, struct i2c_board_info *info)
644 {
645 	struct i2c_adapter *adapter = new_client->adapter;
646 	int manufacturer, product;
647 
648 	if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
649 		return -ENODEV;
650 
651 	manufacturer = i2c_smbus_read_byte_data(new_client, REG_MFG_ID);
652 	if (manufacturer != 0x5D)
653 		return -ENODEV;
654 
655 	product = i2c_smbus_read_byte_data(new_client, REG_PRODUCT_ID);
656 	if ((product != 0x24) && (product != 0x26))
657 		return -ENODEV;
658 
659 	strlcpy(info->type, "emc2103", I2C_NAME_SIZE);
660 
661 	return 0;
662 }
663 
664 static struct i2c_driver emc2103_driver = {
665 	.class		= I2C_CLASS_HWMON,
666 	.driver = {
667 		.name	= "emc2103",
668 	},
669 	.probe		= emc2103_probe,
670 	.id_table	= emc2103_ids,
671 	.detect		= emc2103_detect,
672 	.address_list	= normal_i2c,
673 };
674 
675 module_i2c_driver(emc2103_driver);
676 
677 MODULE_AUTHOR("Steve Glendinning <steve.glendinning@shawell.net>");
678 MODULE_DESCRIPTION("SMSC EMC2103 hwmon driver");
679 MODULE_LICENSE("GPL");
680