]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - drivers/hwmon/emc2103.c
ENGR00294114 PXP: correct the PS U/V buffer settings when format is YVU420P
[karo-tx-linux.git] / drivers / hwmon / emc2103.c
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(init, "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 device           *hwmon_dev;
70         struct mutex            update_lock;
71         bool                    valid;          /* registers are valid */
72         bool                    fan_rpm_control;
73         int                     temp_count;     /* num of temp sensors */
74         unsigned long           last_updated;   /* in jiffies */
75         struct temperature      temp[4];        /* internal + 3 external */
76         s8                      temp_min[4];    /* no fractional part */
77         s8                      temp_max[4];    /* no fractional part */
78         u8                      temp_min_alarm;
79         u8                      temp_max_alarm;
80         u8                      fan_multiplier;
81         u16                     fan_tach;
82         u16                     fan_target;
83 };
84
85 static int read_u8_from_i2c(struct i2c_client *client, u8 i2c_reg, u8 *output)
86 {
87         int status = i2c_smbus_read_byte_data(client, i2c_reg);
88         if (status < 0) {
89                 dev_warn(&client->dev, "reg 0x%02x, err %d\n",
90                         i2c_reg, status);
91         } else {
92                 *output = status;
93         }
94         return status;
95 }
96
97 static void read_temp_from_i2c(struct i2c_client *client, u8 i2c_reg,
98                                struct temperature *temp)
99 {
100         u8 degrees, fractional;
101
102         if (read_u8_from_i2c(client, i2c_reg, &degrees) < 0)
103                 return;
104
105         if (read_u8_from_i2c(client, i2c_reg + 1, &fractional) < 0)
106                 return;
107
108         temp->degrees = degrees;
109         temp->fraction = (fractional & 0xe0) >> 5;
110 }
111
112 static void read_fan_from_i2c(struct i2c_client *client, u16 *output,
113                               u8 hi_addr, u8 lo_addr)
114 {
115         u8 high_byte, lo_byte;
116
117         if (read_u8_from_i2c(client, hi_addr, &high_byte) < 0)
118                 return;
119
120         if (read_u8_from_i2c(client, lo_addr, &lo_byte) < 0)
121                 return;
122
123         *output = ((u16)high_byte << 5) | (lo_byte >> 3);
124 }
125
126 static void write_fan_target_to_i2c(struct i2c_client *client, u16 new_target)
127 {
128         u8 high_byte = (new_target & 0x1fe0) >> 5;
129         u8 low_byte = (new_target & 0x001f) << 3;
130         i2c_smbus_write_byte_data(client, REG_FAN_TARGET_LO, low_byte);
131         i2c_smbus_write_byte_data(client, REG_FAN_TARGET_HI, high_byte);
132 }
133
134 static void read_fan_config_from_i2c(struct i2c_client *client)
135
136 {
137         struct emc2103_data *data = i2c_get_clientdata(client);
138         u8 conf1;
139
140         if (read_u8_from_i2c(client, REG_FAN_CONF1, &conf1) < 0)
141                 return;
142
143         data->fan_multiplier = 1 << ((conf1 & 0x60) >> 5);
144         data->fan_rpm_control = (conf1 & 0x80) != 0;
145 }
146
147 static struct emc2103_data *emc2103_update_device(struct device *dev)
148 {
149         struct i2c_client *client = to_i2c_client(dev);
150         struct emc2103_data *data = i2c_get_clientdata(client);
151
152         mutex_lock(&data->update_lock);
153
154         if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
155             || !data->valid) {
156                 int i;
157
158                 for (i = 0; i < data->temp_count; i++) {
159                         read_temp_from_i2c(client, REG_TEMP[i], &data->temp[i]);
160                         read_u8_from_i2c(client, REG_TEMP_MIN[i],
161                                 &data->temp_min[i]);
162                         read_u8_from_i2c(client, REG_TEMP_MAX[i],
163                                 &data->temp_max[i]);
164                 }
165
166                 read_u8_from_i2c(client, REG_TEMP_MIN_ALARM,
167                         &data->temp_min_alarm);
168                 read_u8_from_i2c(client, REG_TEMP_MAX_ALARM,
169                         &data->temp_max_alarm);
170
171                 read_fan_from_i2c(client, &data->fan_tach,
172                         REG_FAN_TACH_HI, REG_FAN_TACH_LO);
173                 read_fan_from_i2c(client, &data->fan_target,
174                         REG_FAN_TARGET_HI, REG_FAN_TARGET_LO);
175                 read_fan_config_from_i2c(client);
176
177                 data->last_updated = jiffies;
178                 data->valid = true;
179         }
180
181         mutex_unlock(&data->update_lock);
182
183         return data;
184 }
185
186 static ssize_t
187 show_temp(struct device *dev, struct device_attribute *da, char *buf)
188 {
189         int nr = to_sensor_dev_attr(da)->index;
190         struct emc2103_data *data = emc2103_update_device(dev);
191         int millidegrees = data->temp[nr].degrees * 1000
192                 + data->temp[nr].fraction * 125;
193         return sprintf(buf, "%d\n", millidegrees);
194 }
195
196 static ssize_t
197 show_temp_min(struct device *dev, struct device_attribute *da, char *buf)
198 {
199         int nr = to_sensor_dev_attr(da)->index;
200         struct emc2103_data *data = emc2103_update_device(dev);
201         int millidegrees = data->temp_min[nr] * 1000;
202         return sprintf(buf, "%d\n", millidegrees);
203 }
204
205 static ssize_t
206 show_temp_max(struct device *dev, struct device_attribute *da, char *buf)
207 {
208         int nr = to_sensor_dev_attr(da)->index;
209         struct emc2103_data *data = emc2103_update_device(dev);
210         int millidegrees = data->temp_max[nr] * 1000;
211         return sprintf(buf, "%d\n", millidegrees);
212 }
213
214 static ssize_t
215 show_temp_fault(struct device *dev, struct device_attribute *da, char *buf)
216 {
217         int nr = to_sensor_dev_attr(da)->index;
218         struct emc2103_data *data = emc2103_update_device(dev);
219         bool fault = (data->temp[nr].degrees == -128);
220         return sprintf(buf, "%d\n", fault ? 1 : 0);
221 }
222
223 static ssize_t
224 show_temp_min_alarm(struct device *dev, struct device_attribute *da, char *buf)
225 {
226         int nr = to_sensor_dev_attr(da)->index;
227         struct emc2103_data *data = emc2103_update_device(dev);
228         bool alarm = data->temp_min_alarm & (1 << nr);
229         return sprintf(buf, "%d\n", alarm ? 1 : 0);
230 }
231
232 static ssize_t
233 show_temp_max_alarm(struct device *dev, struct device_attribute *da, char *buf)
234 {
235         int nr = to_sensor_dev_attr(da)->index;
236         struct emc2103_data *data = emc2103_update_device(dev);
237         bool alarm = data->temp_max_alarm & (1 << nr);
238         return sprintf(buf, "%d\n", alarm ? 1 : 0);
239 }
240
241 static ssize_t set_temp_min(struct device *dev, struct device_attribute *da,
242                             const char *buf, size_t count)
243 {
244         int nr = to_sensor_dev_attr(da)->index;
245         struct i2c_client *client = to_i2c_client(dev);
246         struct emc2103_data *data = i2c_get_clientdata(client);
247         long val;
248
249         int result = kstrtol(buf, 10, &val);
250         if (result < 0)
251                 return result;
252
253         val = clamp_val(DIV_ROUND_CLOSEST(val, 1000), -63, 127);
254
255         mutex_lock(&data->update_lock);
256         data->temp_min[nr] = val;
257         i2c_smbus_write_byte_data(client, REG_TEMP_MIN[nr], val);
258         mutex_unlock(&data->update_lock);
259
260         return count;
261 }
262
263 static ssize_t set_temp_max(struct device *dev, struct device_attribute *da,
264                             const char *buf, size_t count)
265 {
266         int nr = to_sensor_dev_attr(da)->index;
267         struct i2c_client *client = to_i2c_client(dev);
268         struct emc2103_data *data = i2c_get_clientdata(client);
269         long val;
270
271         int result = kstrtol(buf, 10, &val);
272         if (result < 0)
273                 return result;
274
275         val = clamp_val(DIV_ROUND_CLOSEST(val, 1000), -63, 127);
276
277         mutex_lock(&data->update_lock);
278         data->temp_max[nr] = val;
279         i2c_smbus_write_byte_data(client, REG_TEMP_MAX[nr], val);
280         mutex_unlock(&data->update_lock);
281
282         return count;
283 }
284
285 static ssize_t
286 show_fan(struct device *dev, struct device_attribute *da, char *buf)
287 {
288         struct emc2103_data *data = emc2103_update_device(dev);
289         int rpm = 0;
290         if (data->fan_tach != 0)
291                 rpm = (FAN_RPM_FACTOR * data->fan_multiplier) / data->fan_tach;
292         return sprintf(buf, "%d\n", rpm);
293 }
294
295 static ssize_t
296 show_fan_div(struct device *dev, struct device_attribute *da, char *buf)
297 {
298         struct emc2103_data *data = emc2103_update_device(dev);
299         int fan_div = 8 / data->fan_multiplier;
300         return sprintf(buf, "%d\n", fan_div);
301 }
302
303 /*
304  * Note: we also update the fan target here, because its value is
305  * determined in part by the fan clock divider.  This follows the principle
306  * of least surprise; the user doesn't expect the fan target to change just
307  * because the divider changed.
308  */
309 static ssize_t set_fan_div(struct device *dev, struct device_attribute *da,
310                            const char *buf, size_t count)
311 {
312         struct emc2103_data *data = emc2103_update_device(dev);
313         struct i2c_client *client = to_i2c_client(dev);
314         int new_range_bits, old_div = 8 / data->fan_multiplier;
315         long new_div;
316
317         int status = kstrtol(buf, 10, &new_div);
318         if (status < 0)
319                 return status;
320
321         if (new_div == old_div) /* No change */
322                 return count;
323
324         switch (new_div) {
325         case 1:
326                 new_range_bits = 3;
327                 break;
328         case 2:
329                 new_range_bits = 2;
330                 break;
331         case 4:
332                 new_range_bits = 1;
333                 break;
334         case 8:
335                 new_range_bits = 0;
336                 break;
337         default:
338                 return -EINVAL;
339         }
340
341         mutex_lock(&data->update_lock);
342
343         status = i2c_smbus_read_byte_data(client, REG_FAN_CONF1);
344         if (status < 0) {
345                 dev_dbg(&client->dev, "reg 0x%02x, err %d\n",
346                         REG_FAN_CONF1, status);
347                 mutex_unlock(&data->update_lock);
348                 return status;
349         }
350         status &= 0x9F;
351         status |= (new_range_bits << 5);
352         i2c_smbus_write_byte_data(client, REG_FAN_CONF1, status);
353
354         data->fan_multiplier = 8 / new_div;
355
356         /* update fan target if high byte is not disabled */
357         if ((data->fan_target & 0x1fe0) != 0x1fe0) {
358                 u16 new_target = (data->fan_target * old_div) / new_div;
359                 data->fan_target = min(new_target, (u16)0x1fff);
360                 write_fan_target_to_i2c(client, data->fan_target);
361         }
362
363         /* invalidate data to force re-read from hardware */
364         data->valid = false;
365
366         mutex_unlock(&data->update_lock);
367         return count;
368 }
369
370 static ssize_t
371 show_fan_target(struct device *dev, struct device_attribute *da, char *buf)
372 {
373         struct emc2103_data *data = emc2103_update_device(dev);
374         int rpm = 0;
375
376         /* high byte of 0xff indicates disabled so return 0 */
377         if ((data->fan_target != 0) && ((data->fan_target & 0x1fe0) != 0x1fe0))
378                 rpm = (FAN_RPM_FACTOR * data->fan_multiplier)
379                         / data->fan_target;
380
381         return sprintf(buf, "%d\n", rpm);
382 }
383
384 static ssize_t set_fan_target(struct device *dev, struct device_attribute *da,
385                               const char *buf, size_t count)
386 {
387         struct emc2103_data *data = emc2103_update_device(dev);
388         struct i2c_client *client = to_i2c_client(dev);
389         unsigned long rpm_target;
390
391         int result = kstrtoul(buf, 10, &rpm_target);
392         if (result < 0)
393                 return result;
394
395         /* Datasheet states 16384 as maximum RPM target (table 3.2) */
396         rpm_target = clamp_val(rpm_target, 0, 16384);
397
398         mutex_lock(&data->update_lock);
399
400         if (rpm_target == 0)
401                 data->fan_target = 0x1fff;
402         else
403                 data->fan_target = clamp_val(
404                         (FAN_RPM_FACTOR * data->fan_multiplier) / rpm_target,
405                         0, 0x1fff);
406
407         write_fan_target_to_i2c(client, data->fan_target);
408
409         mutex_unlock(&data->update_lock);
410         return count;
411 }
412
413 static ssize_t
414 show_fan_fault(struct device *dev, struct device_attribute *da, char *buf)
415 {
416         struct emc2103_data *data = emc2103_update_device(dev);
417         bool fault = ((data->fan_tach & 0x1fe0) == 0x1fe0);
418         return sprintf(buf, "%d\n", fault ? 1 : 0);
419 }
420
421 static ssize_t
422 show_pwm_enable(struct device *dev, struct device_attribute *da, char *buf)
423 {
424         struct emc2103_data *data = emc2103_update_device(dev);
425         return sprintf(buf, "%d\n", data->fan_rpm_control ? 3 : 0);
426 }
427
428 static ssize_t set_pwm_enable(struct device *dev, struct device_attribute *da,
429                               const char *buf, size_t count)
430 {
431         struct i2c_client *client = to_i2c_client(dev);
432         struct emc2103_data *data = i2c_get_clientdata(client);
433         long new_value;
434         u8 conf_reg;
435
436         int result = kstrtol(buf, 10, &new_value);
437         if (result < 0)
438                 return result;
439
440         mutex_lock(&data->update_lock);
441         switch (new_value) {
442         case 0:
443                 data->fan_rpm_control = false;
444                 break;
445         case 3:
446                 data->fan_rpm_control = true;
447                 break;
448         default:
449                 count = -EINVAL;
450                 goto err;
451         }
452
453         result = read_u8_from_i2c(client, REG_FAN_CONF1, &conf_reg);
454         if (result) {
455                 count = result;
456                 goto err;
457         }
458
459         if (data->fan_rpm_control)
460                 conf_reg |= 0x80;
461         else
462                 conf_reg &= ~0x80;
463
464         i2c_smbus_write_byte_data(client, REG_FAN_CONF1, conf_reg);
465 err:
466         mutex_unlock(&data->update_lock);
467         return count;
468 }
469
470 static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0);
471 static SENSOR_DEVICE_ATTR(temp1_min, S_IRUGO | S_IWUSR, show_temp_min,
472         set_temp_min, 0);
473 static SENSOR_DEVICE_ATTR(temp1_max, S_IRUGO | S_IWUSR, show_temp_max,
474         set_temp_max, 0);
475 static SENSOR_DEVICE_ATTR(temp1_fault, S_IRUGO, show_temp_fault, NULL, 0);
476 static SENSOR_DEVICE_ATTR(temp1_min_alarm, S_IRUGO, show_temp_min_alarm,
477         NULL, 0);
478 static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, show_temp_max_alarm,
479         NULL, 0);
480
481 static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp, NULL, 1);
482 static SENSOR_DEVICE_ATTR(temp2_min, S_IRUGO | S_IWUSR, show_temp_min,
483         set_temp_min, 1);
484 static SENSOR_DEVICE_ATTR(temp2_max, S_IRUGO | S_IWUSR, show_temp_max,
485         set_temp_max, 1);
486 static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO, show_temp_fault, NULL, 1);
487 static SENSOR_DEVICE_ATTR(temp2_min_alarm, S_IRUGO, show_temp_min_alarm,
488         NULL, 1);
489 static SENSOR_DEVICE_ATTR(temp2_max_alarm, S_IRUGO, show_temp_max_alarm,
490         NULL, 1);
491
492 static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO, show_temp, NULL, 2);
493 static SENSOR_DEVICE_ATTR(temp3_min, S_IRUGO | S_IWUSR, show_temp_min,
494         set_temp_min, 2);
495 static SENSOR_DEVICE_ATTR(temp3_max, S_IRUGO | S_IWUSR, show_temp_max,
496         set_temp_max, 2);
497 static SENSOR_DEVICE_ATTR(temp3_fault, S_IRUGO, show_temp_fault, NULL, 2);
498 static SENSOR_DEVICE_ATTR(temp3_min_alarm, S_IRUGO, show_temp_min_alarm,
499         NULL, 2);
500 static SENSOR_DEVICE_ATTR(temp3_max_alarm, S_IRUGO, show_temp_max_alarm,
501         NULL, 2);
502
503 static SENSOR_DEVICE_ATTR(temp4_input, S_IRUGO, show_temp, NULL, 3);
504 static SENSOR_DEVICE_ATTR(temp4_min, S_IRUGO | S_IWUSR, show_temp_min,
505         set_temp_min, 3);
506 static SENSOR_DEVICE_ATTR(temp4_max, S_IRUGO | S_IWUSR, show_temp_max,
507         set_temp_max, 3);
508 static SENSOR_DEVICE_ATTR(temp4_fault, S_IRUGO, show_temp_fault, NULL, 3);
509 static SENSOR_DEVICE_ATTR(temp4_min_alarm, S_IRUGO, show_temp_min_alarm,
510         NULL, 3);
511 static SENSOR_DEVICE_ATTR(temp4_max_alarm, S_IRUGO, show_temp_max_alarm,
512         NULL, 3);
513
514 static DEVICE_ATTR(fan1_input, S_IRUGO, show_fan, NULL);
515 static DEVICE_ATTR(fan1_div, S_IRUGO | S_IWUSR, show_fan_div, set_fan_div);
516 static DEVICE_ATTR(fan1_target, S_IRUGO | S_IWUSR, show_fan_target,
517         set_fan_target);
518 static DEVICE_ATTR(fan1_fault, S_IRUGO, show_fan_fault, NULL);
519
520 static DEVICE_ATTR(pwm1_enable, S_IRUGO | S_IWUSR, show_pwm_enable,
521         set_pwm_enable);
522
523 /* sensors present on all models */
524 static struct attribute *emc2103_attributes[] = {
525         &sensor_dev_attr_temp1_input.dev_attr.attr,
526         &sensor_dev_attr_temp1_min.dev_attr.attr,
527         &sensor_dev_attr_temp1_max.dev_attr.attr,
528         &sensor_dev_attr_temp1_fault.dev_attr.attr,
529         &sensor_dev_attr_temp1_min_alarm.dev_attr.attr,
530         &sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
531         &sensor_dev_attr_temp2_input.dev_attr.attr,
532         &sensor_dev_attr_temp2_min.dev_attr.attr,
533         &sensor_dev_attr_temp2_max.dev_attr.attr,
534         &sensor_dev_attr_temp2_fault.dev_attr.attr,
535         &sensor_dev_attr_temp2_min_alarm.dev_attr.attr,
536         &sensor_dev_attr_temp2_max_alarm.dev_attr.attr,
537         &dev_attr_fan1_input.attr,
538         &dev_attr_fan1_div.attr,
539         &dev_attr_fan1_target.attr,
540         &dev_attr_fan1_fault.attr,
541         &dev_attr_pwm1_enable.attr,
542         NULL
543 };
544
545 /* extra temperature sensors only present on 2103-2 and 2103-4 */
546 static struct attribute *emc2103_attributes_temp3[] = {
547         &sensor_dev_attr_temp3_input.dev_attr.attr,
548         &sensor_dev_attr_temp3_min.dev_attr.attr,
549         &sensor_dev_attr_temp3_max.dev_attr.attr,
550         &sensor_dev_attr_temp3_fault.dev_attr.attr,
551         &sensor_dev_attr_temp3_min_alarm.dev_attr.attr,
552         &sensor_dev_attr_temp3_max_alarm.dev_attr.attr,
553         NULL
554 };
555
556 /* extra temperature sensors only present on 2103-2 and 2103-4 in APD mode */
557 static struct attribute *emc2103_attributes_temp4[] = {
558         &sensor_dev_attr_temp4_input.dev_attr.attr,
559         &sensor_dev_attr_temp4_min.dev_attr.attr,
560         &sensor_dev_attr_temp4_max.dev_attr.attr,
561         &sensor_dev_attr_temp4_fault.dev_attr.attr,
562         &sensor_dev_attr_temp4_min_alarm.dev_attr.attr,
563         &sensor_dev_attr_temp4_max_alarm.dev_attr.attr,
564         NULL
565 };
566
567 static const struct attribute_group emc2103_group = {
568         .attrs = emc2103_attributes,
569 };
570
571 static const struct attribute_group emc2103_temp3_group = {
572         .attrs = emc2103_attributes_temp3,
573 };
574
575 static const struct attribute_group emc2103_temp4_group = {
576         .attrs = emc2103_attributes_temp4,
577 };
578
579 static int
580 emc2103_probe(struct i2c_client *client, const struct i2c_device_id *id)
581 {
582         struct emc2103_data *data;
583         int status;
584
585         if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
586                 return -EIO;
587
588         data = devm_kzalloc(&client->dev, sizeof(struct emc2103_data),
589                             GFP_KERNEL);
590         if (!data)
591                 return -ENOMEM;
592
593         i2c_set_clientdata(client, data);
594         mutex_init(&data->update_lock);
595
596         /* 2103-2 and 2103-4 have 3 external diodes, 2103-1 has 1 */
597         status = i2c_smbus_read_byte_data(client, REG_PRODUCT_ID);
598         if (status == 0x24) {
599                 /* 2103-1 only has 1 external diode */
600                 data->temp_count = 2;
601         } else {
602                 /* 2103-2 and 2103-4 have 3 or 4 external diodes */
603                 status = i2c_smbus_read_byte_data(client, REG_CONF1);
604                 if (status < 0) {
605                         dev_dbg(&client->dev, "reg 0x%02x, err %d\n", REG_CONF1,
606                                 status);
607                         return status;
608                 }
609
610                 /* detect current state of hardware */
611                 data->temp_count = (status & 0x01) ? 4 : 3;
612
613                 /* force APD state if module parameter is set */
614                 if (apd == 0) {
615                         /* force APD mode off */
616                         data->temp_count = 3;
617                         status &= ~(0x01);
618                         i2c_smbus_write_byte_data(client, REG_CONF1, status);
619                 } else if (apd == 1) {
620                         /* force APD mode on */
621                         data->temp_count = 4;
622                         status |= 0x01;
623                         i2c_smbus_write_byte_data(client, REG_CONF1, status);
624                 }
625         }
626
627         /* Register sysfs hooks */
628         status = sysfs_create_group(&client->dev.kobj, &emc2103_group);
629         if (status)
630                 return status;
631
632         if (data->temp_count >= 3) {
633                 status = sysfs_create_group(&client->dev.kobj,
634                         &emc2103_temp3_group);
635                 if (status)
636                         goto exit_remove;
637         }
638
639         if (data->temp_count == 4) {
640                 status = sysfs_create_group(&client->dev.kobj,
641                         &emc2103_temp4_group);
642                 if (status)
643                         goto exit_remove_temp3;
644         }
645
646         data->hwmon_dev = hwmon_device_register(&client->dev);
647         if (IS_ERR(data->hwmon_dev)) {
648                 status = PTR_ERR(data->hwmon_dev);
649                 goto exit_remove_temp4;
650         }
651
652         dev_info(&client->dev, "%s: sensor '%s'\n",
653                  dev_name(data->hwmon_dev), client->name);
654
655         return 0;
656
657 exit_remove_temp4:
658         if (data->temp_count == 4)
659                 sysfs_remove_group(&client->dev.kobj, &emc2103_temp4_group);
660 exit_remove_temp3:
661         if (data->temp_count >= 3)
662                 sysfs_remove_group(&client->dev.kobj, &emc2103_temp3_group);
663 exit_remove:
664         sysfs_remove_group(&client->dev.kobj, &emc2103_group);
665         return status;
666 }
667
668 static int emc2103_remove(struct i2c_client *client)
669 {
670         struct emc2103_data *data = i2c_get_clientdata(client);
671
672         hwmon_device_unregister(data->hwmon_dev);
673
674         if (data->temp_count == 4)
675                 sysfs_remove_group(&client->dev.kobj, &emc2103_temp4_group);
676
677         if (data->temp_count >= 3)
678                 sysfs_remove_group(&client->dev.kobj, &emc2103_temp3_group);
679
680         sysfs_remove_group(&client->dev.kobj, &emc2103_group);
681
682         return 0;
683 }
684
685 static const struct i2c_device_id emc2103_ids[] = {
686         { "emc2103", 0, },
687         { /* LIST END */ }
688 };
689 MODULE_DEVICE_TABLE(i2c, emc2103_ids);
690
691 /* Return 0 if detection is successful, -ENODEV otherwise */
692 static int
693 emc2103_detect(struct i2c_client *new_client, struct i2c_board_info *info)
694 {
695         struct i2c_adapter *adapter = new_client->adapter;
696         int manufacturer, product;
697
698         if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
699                 return -ENODEV;
700
701         manufacturer = i2c_smbus_read_byte_data(new_client, REG_MFG_ID);
702         if (manufacturer != 0x5D)
703                 return -ENODEV;
704
705         product = i2c_smbus_read_byte_data(new_client, REG_PRODUCT_ID);
706         if ((product != 0x24) && (product != 0x26))
707                 return -ENODEV;
708
709         strlcpy(info->type, "emc2103", I2C_NAME_SIZE);
710
711         return 0;
712 }
713
714 static struct i2c_driver emc2103_driver = {
715         .class          = I2C_CLASS_HWMON,
716         .driver = {
717                 .name   = "emc2103",
718         },
719         .probe          = emc2103_probe,
720         .remove         = emc2103_remove,
721         .id_table       = emc2103_ids,
722         .detect         = emc2103_detect,
723         .address_list   = normal_i2c,
724 };
725
726 module_i2c_driver(emc2103_driver);
727
728 MODULE_AUTHOR("Steve Glendinning <steve.glendinning@shawell.net>");
729 MODULE_DESCRIPTION("SMSC EMC2103 hwmon driver");
730 MODULE_LICENSE("GPL");