blob: e7d234b59312f7ed479c9fcc0ed309500e230c93 [file] [log] [blame]
Steve Glendinning9df73052010-08-14 21:08:54 +02001/*
Guenter Roeckbf0f3a02012-01-19 11:02:17 -08002 * 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 */
Steve Glendinning9df73052010-08-14 21:08:54 +020019
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
Guenter Roeckbf0f3a02012-01-19 11:02:17 -080051/*
52 * 2103-2 and 2103-4's 3rd temperature sensor can be connected to two diodes
Steve Glendinning9df73052010-08-14 21:08:54 +020053 * 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).
Guenter Roeckbf0f3a02012-01-19 11:02:17 -080057 * This parameter allows APD mode to be optionally forced on or off
58 */
Steve Glendinning9df73052010-08-14 21:08:54 +020059static int apd = -1;
Rusty Russell69116f22012-01-13 09:32:17 +103060module_param(apd, bint, 0);
Steve Glendinning9df73052010-08-14 21:08:54 +020061MODULE_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 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
85static 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
97static 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
112static 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
126static 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
134static 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
147static 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
186static ssize_t
187show_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
196static ssize_t
197show_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
205static ssize_t
206show_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
214static ssize_t
215show_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
223static ssize_t
224show_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
232static ssize_t
233show_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
241static 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
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100249 int result = kstrtol(buf, 10, &val);
Steve Glendinning9df73052010-08-14 21:08:54 +0200250 if (result < 0)
251 return -EINVAL;
252
253 val = DIV_ROUND_CLOSEST(val, 1000);
254 if ((val < -63) || (val > 127))
255 return -EINVAL;
256
257 mutex_lock(&data->update_lock);
258 data->temp_min[nr] = val;
259 i2c_smbus_write_byte_data(client, REG_TEMP_MIN[nr], val);
260 mutex_unlock(&data->update_lock);
261
262 return count;
263}
264
265static ssize_t set_temp_max(struct device *dev, struct device_attribute *da,
266 const char *buf, size_t count)
267{
268 int nr = to_sensor_dev_attr(da)->index;
269 struct i2c_client *client = to_i2c_client(dev);
270 struct emc2103_data *data = i2c_get_clientdata(client);
271 long val;
272
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100273 int result = kstrtol(buf, 10, &val);
Steve Glendinning9df73052010-08-14 21:08:54 +0200274 if (result < 0)
275 return -EINVAL;
276
277 val = DIV_ROUND_CLOSEST(val, 1000);
278 if ((val < -63) || (val > 127))
279 return -EINVAL;
280
281 mutex_lock(&data->update_lock);
282 data->temp_max[nr] = val;
283 i2c_smbus_write_byte_data(client, REG_TEMP_MAX[nr], val);
284 mutex_unlock(&data->update_lock);
285
286 return count;
287}
288
289static ssize_t
290show_fan(struct device *dev, struct device_attribute *da, char *buf)
291{
292 struct emc2103_data *data = emc2103_update_device(dev);
293 int rpm = 0;
294 if (data->fan_tach != 0)
295 rpm = (FAN_RPM_FACTOR * data->fan_multiplier) / data->fan_tach;
296 return sprintf(buf, "%d\n", rpm);
297}
298
299static ssize_t
300show_fan_div(struct device *dev, struct device_attribute *da, char *buf)
301{
302 struct emc2103_data *data = emc2103_update_device(dev);
303 int fan_div = 8 / data->fan_multiplier;
304 return sprintf(buf, "%d\n", fan_div);
305}
306
Guenter Roeckbf0f3a02012-01-19 11:02:17 -0800307/*
308 * Note: we also update the fan target here, because its value is
309 * determined in part by the fan clock divider. This follows the principle
310 * of least surprise; the user doesn't expect the fan target to change just
311 * because the divider changed.
312 */
Steve Glendinning9df73052010-08-14 21:08:54 +0200313static ssize_t set_fan_div(struct device *dev, struct device_attribute *da,
314 const char *buf, size_t count)
315{
316 struct emc2103_data *data = emc2103_update_device(dev);
317 struct i2c_client *client = to_i2c_client(dev);
318 int new_range_bits, old_div = 8 / data->fan_multiplier;
319 long new_div;
320
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100321 int status = kstrtol(buf, 10, &new_div);
Steve Glendinning9df73052010-08-14 21:08:54 +0200322 if (status < 0)
323 return -EINVAL;
324
325 if (new_div == old_div) /* No change */
326 return count;
327
328 switch (new_div) {
329 case 1:
330 new_range_bits = 3;
331 break;
332 case 2:
333 new_range_bits = 2;
334 break;
335 case 4:
336 new_range_bits = 1;
337 break;
338 case 8:
339 new_range_bits = 0;
340 break;
341 default:
342 return -EINVAL;
343 }
344
345 mutex_lock(&data->update_lock);
346
347 status = i2c_smbus_read_byte_data(client, REG_FAN_CONF1);
348 if (status < 0) {
349 dev_dbg(&client->dev, "reg 0x%02x, err %d\n",
350 REG_FAN_CONF1, status);
351 mutex_unlock(&data->update_lock);
352 return -EIO;
353 }
354 status &= 0x9F;
355 status |= (new_range_bits << 5);
356 i2c_smbus_write_byte_data(client, REG_FAN_CONF1, status);
357
358 data->fan_multiplier = 8 / new_div;
359
360 /* update fan target if high byte is not disabled */
361 if ((data->fan_target & 0x1fe0) != 0x1fe0) {
362 u16 new_target = (data->fan_target * old_div) / new_div;
363 data->fan_target = min(new_target, (u16)0x1fff);
364 write_fan_target_to_i2c(client, data->fan_target);
365 }
366
367 /* invalidate data to force re-read from hardware */
368 data->valid = false;
369
370 mutex_unlock(&data->update_lock);
371 return count;
372}
373
374static ssize_t
375show_fan_target(struct device *dev, struct device_attribute *da, char *buf)
376{
377 struct emc2103_data *data = emc2103_update_device(dev);
378 int rpm = 0;
379
380 /* high byte of 0xff indicates disabled so return 0 */
381 if ((data->fan_target != 0) && ((data->fan_target & 0x1fe0) != 0x1fe0))
382 rpm = (FAN_RPM_FACTOR * data->fan_multiplier)
383 / data->fan_target;
384
385 return sprintf(buf, "%d\n", rpm);
386}
387
388static ssize_t set_fan_target(struct device *dev, struct device_attribute *da,
389 const char *buf, size_t count)
390{
391 struct emc2103_data *data = emc2103_update_device(dev);
392 struct i2c_client *client = to_i2c_client(dev);
393 long rpm_target;
394
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100395 int result = kstrtol(buf, 10, &rpm_target);
Steve Glendinning9df73052010-08-14 21:08:54 +0200396 if (result < 0)
397 return -EINVAL;
398
399 /* Datasheet states 16384 as maximum RPM target (table 3.2) */
400 if ((rpm_target < 0) || (rpm_target > 16384))
401 return -EINVAL;
402
403 mutex_lock(&data->update_lock);
404
405 if (rpm_target == 0)
406 data->fan_target = 0x1fff;
407 else
408 data->fan_target = SENSORS_LIMIT(
409 (FAN_RPM_FACTOR * data->fan_multiplier) / rpm_target,
410 0, 0x1fff);
411
412 write_fan_target_to_i2c(client, data->fan_target);
413
414 mutex_unlock(&data->update_lock);
415 return count;
416}
417
418static ssize_t
419show_fan_fault(struct device *dev, struct device_attribute *da, char *buf)
420{
421 struct emc2103_data *data = emc2103_update_device(dev);
422 bool fault = ((data->fan_tach & 0x1fe0) == 0x1fe0);
423 return sprintf(buf, "%d\n", fault ? 1 : 0);
424}
425
426static ssize_t
427show_pwm_enable(struct device *dev, struct device_attribute *da, char *buf)
428{
429 struct emc2103_data *data = emc2103_update_device(dev);
430 return sprintf(buf, "%d\n", data->fan_rpm_control ? 3 : 0);
431}
432
433static ssize_t set_pwm_enable(struct device *dev, struct device_attribute *da,
434 const char *buf, size_t count)
435{
436 struct i2c_client *client = to_i2c_client(dev);
437 struct emc2103_data *data = i2c_get_clientdata(client);
438 long new_value;
439 u8 conf_reg;
440
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100441 int result = kstrtol(buf, 10, &new_value);
Steve Glendinning9df73052010-08-14 21:08:54 +0200442 if (result < 0)
443 return -EINVAL;
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:
Guenter Roeck23553752012-06-02 10:16:32 -0700454 count = -EINVAL;
455 goto err;
Steve Glendinning9df73052010-08-14 21:08:54 +0200456 }
457
Guenter Roeck23553752012-06-02 10:16:32 -0700458 result = read_u8_from_i2c(client, REG_FAN_CONF1, &conf_reg);
459 if (result) {
460 count = result;
461 goto err;
462 }
Steve Glendinning9df73052010-08-14 21:08:54 +0200463
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);
Guenter Roeck23553752012-06-02 10:16:32 -0700470err:
Steve Glendinning9df73052010-08-14 21:08:54 +0200471 mutex_unlock(&data->update_lock);
472 return count;
473}
474
475static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0);
476static SENSOR_DEVICE_ATTR(temp1_min, S_IRUGO | S_IWUSR, show_temp_min,
477 set_temp_min, 0);
478static SENSOR_DEVICE_ATTR(temp1_max, S_IRUGO | S_IWUSR, show_temp_max,
479 set_temp_max, 0);
480static SENSOR_DEVICE_ATTR(temp1_fault, S_IRUGO, show_temp_fault, NULL, 0);
481static SENSOR_DEVICE_ATTR(temp1_min_alarm, S_IRUGO, show_temp_min_alarm,
482 NULL, 0);
483static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, show_temp_max_alarm,
484 NULL, 0);
485
486static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp, NULL, 1);
487static SENSOR_DEVICE_ATTR(temp2_min, S_IRUGO | S_IWUSR, show_temp_min,
488 set_temp_min, 1);
489static SENSOR_DEVICE_ATTR(temp2_max, S_IRUGO | S_IWUSR, show_temp_max,
490 set_temp_max, 1);
491static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO, show_temp_fault, NULL, 1);
492static SENSOR_DEVICE_ATTR(temp2_min_alarm, S_IRUGO, show_temp_min_alarm,
493 NULL, 1);
494static SENSOR_DEVICE_ATTR(temp2_max_alarm, S_IRUGO, show_temp_max_alarm,
495 NULL, 1);
496
497static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO, show_temp, NULL, 2);
498static SENSOR_DEVICE_ATTR(temp3_min, S_IRUGO | S_IWUSR, show_temp_min,
499 set_temp_min, 2);
500static SENSOR_DEVICE_ATTR(temp3_max, S_IRUGO | S_IWUSR, show_temp_max,
501 set_temp_max, 2);
502static SENSOR_DEVICE_ATTR(temp3_fault, S_IRUGO, show_temp_fault, NULL, 2);
503static SENSOR_DEVICE_ATTR(temp3_min_alarm, S_IRUGO, show_temp_min_alarm,
504 NULL, 2);
505static SENSOR_DEVICE_ATTR(temp3_max_alarm, S_IRUGO, show_temp_max_alarm,
506 NULL, 2);
507
508static SENSOR_DEVICE_ATTR(temp4_input, S_IRUGO, show_temp, NULL, 3);
509static SENSOR_DEVICE_ATTR(temp4_min, S_IRUGO | S_IWUSR, show_temp_min,
510 set_temp_min, 3);
511static SENSOR_DEVICE_ATTR(temp4_max, S_IRUGO | S_IWUSR, show_temp_max,
512 set_temp_max, 3);
513static SENSOR_DEVICE_ATTR(temp4_fault, S_IRUGO, show_temp_fault, NULL, 3);
514static SENSOR_DEVICE_ATTR(temp4_min_alarm, S_IRUGO, show_temp_min_alarm,
515 NULL, 3);
516static SENSOR_DEVICE_ATTR(temp4_max_alarm, S_IRUGO, show_temp_max_alarm,
517 NULL, 3);
518
519static DEVICE_ATTR(fan1_input, S_IRUGO, show_fan, NULL);
520static DEVICE_ATTR(fan1_div, S_IRUGO | S_IWUSR, show_fan_div, set_fan_div);
521static DEVICE_ATTR(fan1_target, S_IRUGO | S_IWUSR, show_fan_target,
522 set_fan_target);
523static DEVICE_ATTR(fan1_fault, S_IRUGO, show_fan_fault, NULL);
524
525static DEVICE_ATTR(pwm1_enable, S_IRUGO | S_IWUSR, show_pwm_enable,
526 set_pwm_enable);
527
528/* sensors present on all models */
529static struct attribute *emc2103_attributes[] = {
530 &sensor_dev_attr_temp1_input.dev_attr.attr,
531 &sensor_dev_attr_temp1_min.dev_attr.attr,
532 &sensor_dev_attr_temp1_max.dev_attr.attr,
533 &sensor_dev_attr_temp1_fault.dev_attr.attr,
534 &sensor_dev_attr_temp1_min_alarm.dev_attr.attr,
535 &sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
536 &sensor_dev_attr_temp2_input.dev_attr.attr,
537 &sensor_dev_attr_temp2_min.dev_attr.attr,
538 &sensor_dev_attr_temp2_max.dev_attr.attr,
539 &sensor_dev_attr_temp2_fault.dev_attr.attr,
540 &sensor_dev_attr_temp2_min_alarm.dev_attr.attr,
541 &sensor_dev_attr_temp2_max_alarm.dev_attr.attr,
542 &dev_attr_fan1_input.attr,
543 &dev_attr_fan1_div.attr,
544 &dev_attr_fan1_target.attr,
545 &dev_attr_fan1_fault.attr,
546 &dev_attr_pwm1_enable.attr,
547 NULL
548};
549
550/* extra temperature sensors only present on 2103-2 and 2103-4 */
551static struct attribute *emc2103_attributes_temp3[] = {
552 &sensor_dev_attr_temp3_input.dev_attr.attr,
553 &sensor_dev_attr_temp3_min.dev_attr.attr,
554 &sensor_dev_attr_temp3_max.dev_attr.attr,
555 &sensor_dev_attr_temp3_fault.dev_attr.attr,
556 &sensor_dev_attr_temp3_min_alarm.dev_attr.attr,
557 &sensor_dev_attr_temp3_max_alarm.dev_attr.attr,
558 NULL
559};
560
561/* extra temperature sensors only present on 2103-2 and 2103-4 in APD mode */
562static struct attribute *emc2103_attributes_temp4[] = {
563 &sensor_dev_attr_temp4_input.dev_attr.attr,
564 &sensor_dev_attr_temp4_min.dev_attr.attr,
565 &sensor_dev_attr_temp4_max.dev_attr.attr,
566 &sensor_dev_attr_temp4_fault.dev_attr.attr,
567 &sensor_dev_attr_temp4_min_alarm.dev_attr.attr,
568 &sensor_dev_attr_temp4_max_alarm.dev_attr.attr,
569 NULL
570};
571
572static const struct attribute_group emc2103_group = {
573 .attrs = emc2103_attributes,
574};
575
576static const struct attribute_group emc2103_temp3_group = {
577 .attrs = emc2103_attributes_temp3,
578};
579
580static const struct attribute_group emc2103_temp4_group = {
581 .attrs = emc2103_attributes_temp4,
582};
583
584static int
585emc2103_probe(struct i2c_client *client, const struct i2c_device_id *id)
586{
587 struct emc2103_data *data;
588 int status;
589
590 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
591 return -EIO;
592
593 data = kzalloc(sizeof(struct emc2103_data), GFP_KERNEL);
594 if (!data)
595 return -ENOMEM;
596
597 i2c_set_clientdata(client, data);
598 mutex_init(&data->update_lock);
599
600 /* 2103-2 and 2103-4 have 3 external diodes, 2103-1 has 1 */
601 status = i2c_smbus_read_byte_data(client, REG_PRODUCT_ID);
602 if (status == 0x24) {
603 /* 2103-1 only has 1 external diode */
604 data->temp_count = 2;
605 } else {
606 /* 2103-2 and 2103-4 have 3 or 4 external diodes */
607 status = i2c_smbus_read_byte_data(client, REG_CONF1);
608 if (status < 0) {
609 dev_dbg(&client->dev, "reg 0x%02x, err %d\n", REG_CONF1,
610 status);
611 goto exit_free;
612 }
613
614 /* detect current state of hardware */
615 data->temp_count = (status & 0x01) ? 4 : 3;
616
617 /* force APD state if module parameter is set */
618 if (apd == 0) {
619 /* force APD mode off */
620 data->temp_count = 3;
621 status &= ~(0x01);
622 i2c_smbus_write_byte_data(client, REG_CONF1, status);
623 } else if (apd == 1) {
624 /* force APD mode on */
625 data->temp_count = 4;
626 status |= 0x01;
627 i2c_smbus_write_byte_data(client, REG_CONF1, status);
628 }
629 }
630
631 /* Register sysfs hooks */
632 status = sysfs_create_group(&client->dev.kobj, &emc2103_group);
633 if (status)
634 goto exit_free;
635
636 if (data->temp_count >= 3) {
637 status = sysfs_create_group(&client->dev.kobj,
638 &emc2103_temp3_group);
639 if (status)
640 goto exit_remove;
641 }
642
643 if (data->temp_count == 4) {
644 status = sysfs_create_group(&client->dev.kobj,
645 &emc2103_temp4_group);
646 if (status)
647 goto exit_remove_temp3;
648 }
649
650 data->hwmon_dev = hwmon_device_register(&client->dev);
651 if (IS_ERR(data->hwmon_dev)) {
652 status = PTR_ERR(data->hwmon_dev);
653 goto exit_remove_temp4;
654 }
655
656 dev_info(&client->dev, "%s: sensor '%s'\n",
657 dev_name(data->hwmon_dev), client->name);
658
659 return 0;
660
661exit_remove_temp4:
662 if (data->temp_count == 4)
663 sysfs_remove_group(&client->dev.kobj, &emc2103_temp4_group);
664exit_remove_temp3:
665 if (data->temp_count >= 3)
666 sysfs_remove_group(&client->dev.kobj, &emc2103_temp3_group);
667exit_remove:
668 sysfs_remove_group(&client->dev.kobj, &emc2103_group);
669exit_free:
670 kfree(data);
671 return status;
672}
673
674static int emc2103_remove(struct i2c_client *client)
675{
676 struct emc2103_data *data = i2c_get_clientdata(client);
677
678 hwmon_device_unregister(data->hwmon_dev);
679
680 if (data->temp_count == 4)
681 sysfs_remove_group(&client->dev.kobj, &emc2103_temp4_group);
682
683 if (data->temp_count >= 3)
684 sysfs_remove_group(&client->dev.kobj, &emc2103_temp3_group);
685
686 sysfs_remove_group(&client->dev.kobj, &emc2103_group);
687
688 kfree(data);
689 return 0;
690}
691
692static const struct i2c_device_id emc2103_ids[] = {
693 { "emc2103", 0, },
694 { /* LIST END */ }
695};
696MODULE_DEVICE_TABLE(i2c, emc2103_ids);
697
698/* Return 0 if detection is successful, -ENODEV otherwise */
699static int
700emc2103_detect(struct i2c_client *new_client, struct i2c_board_info *info)
701{
702 struct i2c_adapter *adapter = new_client->adapter;
703 int manufacturer, product;
704
705 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
706 return -ENODEV;
707
708 manufacturer = i2c_smbus_read_byte_data(new_client, REG_MFG_ID);
709 if (manufacturer != 0x5D)
710 return -ENODEV;
711
712 product = i2c_smbus_read_byte_data(new_client, REG_PRODUCT_ID);
713 if ((product != 0x24) && (product != 0x26))
714 return -ENODEV;
715
716 strlcpy(info->type, "emc2103", I2C_NAME_SIZE);
717
718 return 0;
719}
720
721static struct i2c_driver emc2103_driver = {
722 .class = I2C_CLASS_HWMON,
723 .driver = {
724 .name = "emc2103",
725 },
726 .probe = emc2103_probe,
727 .remove = emc2103_remove,
728 .id_table = emc2103_ids,
729 .detect = emc2103_detect,
730 .address_list = normal_i2c,
731};
732
Axel Linf0967ee2012-01-20 15:38:18 +0800733module_i2c_driver(emc2103_driver);
Steve Glendinning9df73052010-08-14 21:08:54 +0200734
735MODULE_AUTHOR("Steve Glendinning <steve.glendinning@smsc.com>");
736MODULE_DESCRIPTION("SMSC EMC2103 hwmon driver");
737MODULE_LICENSE("GPL");