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