blob: 562cc3881d3345465f0605c5f19c36d8b0b1f127 [file] [log] [blame]
Darrick J. Wongc0b4e3a2008-11-12 13:27:03 -08001/*
2 * A hwmon driver for the Analog Devices ADT7462
3 * Copyright (C) 2008 IBM
4 *
Darrick J. Wong5407e0512013-08-26 15:42:27 -07005 * Author: Darrick J. Wong <darrick.wong@oracle.com>
Darrick J. Wongc0b4e3a2008-11-12 13:27:03 -08006 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21
22#include <linux/module.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>
Darrick J. Wongc0b4e3a2008-11-12 13:27:03 -080029#include <linux/log2.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090030#include <linux/slab.h>
Darrick J. Wongc0b4e3a2008-11-12 13:27:03 -080031
32/* Addresses to scan */
33static const unsigned short normal_i2c[] = { 0x58, 0x5C, I2C_CLIENT_END };
34
Darrick J. Wongc0b4e3a2008-11-12 13:27:03 -080035/* ADT7462 registers */
36#define ADT7462_REG_DEVICE 0x3D
37#define ADT7462_REG_VENDOR 0x3E
38#define ADT7462_REG_REVISION 0x3F
39
40#define ADT7462_REG_MIN_TEMP_BASE_ADDR 0x44
41#define ADT7462_REG_MIN_TEMP_MAX_ADDR 0x47
42#define ADT7462_REG_MAX_TEMP_BASE_ADDR 0x48
43#define ADT7462_REG_MAX_TEMP_MAX_ADDR 0x4B
44#define ADT7462_REG_TEMP_BASE_ADDR 0x88
45#define ADT7462_REG_TEMP_MAX_ADDR 0x8F
46
47#define ADT7462_REG_FAN_BASE_ADDR 0x98
48#define ADT7462_REG_FAN_MAX_ADDR 0x9F
49#define ADT7462_REG_FAN2_BASE_ADDR 0xA2
50#define ADT7462_REG_FAN2_MAX_ADDR 0xA9
51#define ADT7462_REG_FAN_ENABLE 0x07
52#define ADT7462_REG_FAN_MIN_BASE_ADDR 0x78
53#define ADT7462_REG_FAN_MIN_MAX_ADDR 0x7F
54
55#define ADT7462_REG_CFG2 0x02
56#define ADT7462_FSPD_MASK 0x20
57
58#define ADT7462_REG_PWM_BASE_ADDR 0xAA
59#define ADT7462_REG_PWM_MAX_ADDR 0xAD
60#define ADT7462_REG_PWM_MIN_BASE_ADDR 0x28
61#define ADT7462_REG_PWM_MIN_MAX_ADDR 0x2B
62#define ADT7462_REG_PWM_MAX 0x2C
63#define ADT7462_REG_PWM_TEMP_MIN_BASE_ADDR 0x5C
64#define ADT7462_REG_PWM_TEMP_MIN_MAX_ADDR 0x5F
65#define ADT7462_REG_PWM_TEMP_RANGE_BASE_ADDR 0x60
66#define ADT7462_REG_PWM_TEMP_RANGE_MAX_ADDR 0x63
Guenter Roeck724cc332012-01-14 13:16:53 -080067#define ADT7462_PWM_HYST_MASK 0x0F
68#define ADT7462_PWM_RANGE_MASK 0xF0
Darrick J. Wongc0b4e3a2008-11-12 13:27:03 -080069#define ADT7462_PWM_RANGE_SHIFT 4
70#define ADT7462_REG_PWM_CFG_BASE_ADDR 0x21
71#define ADT7462_REG_PWM_CFG_MAX_ADDR 0x24
72#define ADT7462_PWM_CHANNEL_MASK 0xE0
73#define ADT7462_PWM_CHANNEL_SHIFT 5
74
75#define ADT7462_REG_PIN_CFG_BASE_ADDR 0x10
76#define ADT7462_REG_PIN_CFG_MAX_ADDR 0x13
77#define ADT7462_PIN7_INPUT 0x01 /* cfg0 */
78#define ADT7462_DIODE3_INPUT 0x20
79#define ADT7462_DIODE1_INPUT 0x40
80#define ADT7462_VID_INPUT 0x80
81#define ADT7462_PIN22_INPUT 0x04 /* cfg1 */
82#define ADT7462_PIN21_INPUT 0x08
83#define ADT7462_PIN19_INPUT 0x10
84#define ADT7462_PIN15_INPUT 0x20
85#define ADT7462_PIN13_INPUT 0x40
86#define ADT7462_PIN8_INPUT 0x80
Guenter Roeck724cc332012-01-14 13:16:53 -080087#define ADT7462_PIN23_MASK 0x03
Darrick J. Wongc0b4e3a2008-11-12 13:27:03 -080088#define ADT7462_PIN23_SHIFT 0
89#define ADT7462_PIN26_MASK 0x0C /* cfg2 */
90#define ADT7462_PIN26_SHIFT 2
91#define ADT7462_PIN25_MASK 0x30
92#define ADT7462_PIN25_SHIFT 4
93#define ADT7462_PIN24_MASK 0xC0
94#define ADT7462_PIN24_SHIFT 6
95#define ADT7462_PIN26_VOLT_INPUT 0x08
96#define ADT7462_PIN25_VOLT_INPUT 0x20
Roger Blofeldbb595c92010-01-10 20:52:32 +010097#define ADT7462_PIN28_SHIFT 4 /* cfg3 */
Darrick J. Wongc0b4e3a2008-11-12 13:27:03 -080098#define ADT7462_PIN28_VOLT 0x5
99
100#define ADT7462_REG_ALARM1 0xB8
Guenter Roeck724cc332012-01-14 13:16:53 -0800101#define ADT7462_LT_ALARM 0x02
Darrick J. Wongc0b4e3a2008-11-12 13:27:03 -0800102#define ADT7462_R1T_ALARM 0x04
103#define ADT7462_R2T_ALARM 0x08
104#define ADT7462_R3T_ALARM 0x10
105#define ADT7462_REG_ALARM2 0xBB
106#define ADT7462_V0_ALARM 0x01
107#define ADT7462_V1_ALARM 0x02
108#define ADT7462_V2_ALARM 0x04
109#define ADT7462_V3_ALARM 0x08
110#define ADT7462_V4_ALARM 0x10
111#define ADT7462_V5_ALARM 0x20
112#define ADT7462_V6_ALARM 0x40
113#define ADT7462_V7_ALARM 0x80
114#define ADT7462_REG_ALARM3 0xBC
115#define ADT7462_V8_ALARM 0x08
116#define ADT7462_V9_ALARM 0x10
117#define ADT7462_V10_ALARM 0x20
118#define ADT7462_V11_ALARM 0x40
119#define ADT7462_V12_ALARM 0x80
120#define ADT7462_REG_ALARM4 0xBD
121#define ADT7462_F0_ALARM 0x01
122#define ADT7462_F1_ALARM 0x02
123#define ADT7462_F2_ALARM 0x04
124#define ADT7462_F3_ALARM 0x08
125#define ADT7462_F4_ALARM 0x10
126#define ADT7462_F5_ALARM 0x20
127#define ADT7462_F6_ALARM 0x40
128#define ADT7462_F7_ALARM 0x80
129#define ADT7462_ALARM1 0x0000
130#define ADT7462_ALARM2 0x0100
131#define ADT7462_ALARM3 0x0200
132#define ADT7462_ALARM4 0x0300
133#define ADT7462_ALARM_REG_SHIFT 8
134#define ADT7462_ALARM_FLAG_MASK 0x0F
135
136#define ADT7462_TEMP_COUNT 4
Guenter Roeck724cc332012-01-14 13:16:53 -0800137#define ADT7462_TEMP_REG(x) (ADT7462_REG_TEMP_BASE_ADDR + ((x) * 2))
138#define ADT7462_TEMP_MIN_REG(x) (ADT7462_REG_MIN_TEMP_BASE_ADDR + (x))
139#define ADT7462_TEMP_MAX_REG(x) (ADT7462_REG_MAX_TEMP_BASE_ADDR + (x))
Darrick J. Wongc0b4e3a2008-11-12 13:27:03 -0800140#define TEMP_FRAC_OFFSET 6
141
142#define ADT7462_FAN_COUNT 8
143#define ADT7462_REG_FAN_MIN(x) (ADT7462_REG_FAN_MIN_BASE_ADDR + (x))
144
145#define ADT7462_PWM_COUNT 4
146#define ADT7462_REG_PWM(x) (ADT7462_REG_PWM_BASE_ADDR + (x))
147#define ADT7462_REG_PWM_MIN(x) (ADT7462_REG_PWM_MIN_BASE_ADDR + (x))
148#define ADT7462_REG_PWM_TMIN(x) \
149 (ADT7462_REG_PWM_TEMP_MIN_BASE_ADDR + (x))
150#define ADT7462_REG_PWM_TRANGE(x) \
151 (ADT7462_REG_PWM_TEMP_RANGE_BASE_ADDR + (x))
152
153#define ADT7462_PIN_CFG_REG_COUNT 4
154#define ADT7462_REG_PIN_CFG(x) (ADT7462_REG_PIN_CFG_BASE_ADDR + (x))
155#define ADT7462_REG_PWM_CFG(x) (ADT7462_REG_PWM_CFG_BASE_ADDR + (x))
156
157#define ADT7462_ALARM_REG_COUNT 4
158
159/*
160 * The chip can measure 13 different voltage sources:
161 *
162 * 1. +12V1 (pin 7)
163 * 2. Vccp1/+2.5V/+1.8V/+1.5V (pin 23)
164 * 3. +12V3 (pin 22)
165 * 4. +5V (pin 21)
166 * 5. +1.25V/+0.9V (pin 19)
167 * 6. +2.5V/+1.8V (pin 15)
168 * 7. +3.3v (pin 13)
169 * 8. +12V2 (pin 8)
170 * 9. Vbatt/FSB_Vtt (pin 26)
171 * A. +3.3V/+1.2V1 (pin 25)
172 * B. Vccp2/+2.5V/+1.8V/+1.5V (pin 24)
173 * C. +1.5V ICH (only if BOTH pin 28/29 are set to +1.5V)
174 * D. +1.5V 3GPIO (only if BOTH pin 28/29 are set to +1.5V)
175 *
176 * Each of these 13 has a factor to convert raw to voltage. Even better,
177 * the pins can be connected to other sensors (tach/gpio/hot/etc), which
178 * makes the bookkeeping tricky.
179 *
180 * Some, but not all, of these voltages have low/high limits.
181 */
Ray Copeland85f8d3e2010-02-05 19:58:35 +0100182#define ADT7462_VOLT_COUNT 13
Darrick J. Wongc0b4e3a2008-11-12 13:27:03 -0800183
184#define ADT7462_VENDOR 0x41
185#define ADT7462_DEVICE 0x62
186/* datasheet only mentions a revision 4 */
187#define ADT7462_REVISION 0x04
188
189/* How often do we reread sensors values? (In jiffies) */
190#define SENSOR_REFRESH_INTERVAL (2 * HZ)
191
192/* How often do we reread sensor limit values? (In jiffies) */
193#define LIMIT_REFRESH_INTERVAL (60 * HZ)
194
195/* datasheet says to divide this number by the fan reading to get fan rpm */
196#define FAN_PERIOD_TO_RPM(x) ((90000 * 60) / (x))
197#define FAN_RPM_TO_PERIOD FAN_PERIOD_TO_RPM
198#define FAN_PERIOD_INVALID 65535
199#define FAN_DATA_VALID(x) ((x) && (x) != FAN_PERIOD_INVALID)
200
201#define MASK_AND_SHIFT(value, prefix) \
202 (((value) & prefix##_MASK) >> prefix##_SHIFT)
203
Darrick J. Wongc0b4e3a2008-11-12 13:27:03 -0800204struct adt7462_data {
205 struct device *hwmon_dev;
206 struct attribute_group attrs;
207 struct mutex lock;
208 char sensors_valid;
209 char limits_valid;
210 unsigned long sensors_last_updated; /* In jiffies */
211 unsigned long limits_last_updated; /* In jiffies */
212
213 u8 temp[ADT7462_TEMP_COUNT];
214 /* bits 6-7 are quarter pieces of temp */
215 u8 temp_frac[ADT7462_TEMP_COUNT];
216 u8 temp_min[ADT7462_TEMP_COUNT];
217 u8 temp_max[ADT7462_TEMP_COUNT];
218 u16 fan[ADT7462_FAN_COUNT];
219 u8 fan_enabled;
220 u8 fan_min[ADT7462_FAN_COUNT];
221 u8 cfg2;
222 u8 pwm[ADT7462_PWM_COUNT];
223 u8 pin_cfg[ADT7462_PIN_CFG_REG_COUNT];
224 u8 voltages[ADT7462_VOLT_COUNT];
225 u8 volt_max[ADT7462_VOLT_COUNT];
226 u8 volt_min[ADT7462_VOLT_COUNT];
227 u8 pwm_min[ADT7462_PWM_COUNT];
228 u8 pwm_tmin[ADT7462_PWM_COUNT];
229 u8 pwm_trange[ADT7462_PWM_COUNT];
230 u8 pwm_max; /* only one per chip */
231 u8 pwm_cfg[ADT7462_PWM_COUNT];
232 u8 alarms[ADT7462_ALARM_REG_COUNT];
233};
234
235static int adt7462_probe(struct i2c_client *client,
236 const struct i2c_device_id *id);
Jean Delvare310ec792009-12-14 21:17:23 +0100237static int adt7462_detect(struct i2c_client *client,
Darrick J. Wongc0b4e3a2008-11-12 13:27:03 -0800238 struct i2c_board_info *info);
239static int adt7462_remove(struct i2c_client *client);
240
241static const struct i2c_device_id adt7462_id[] = {
Jean Delvare1f86df42009-12-14 21:17:26 +0100242 { "adt7462", 0 },
Darrick J. Wongc0b4e3a2008-11-12 13:27:03 -0800243 { }
244};
245MODULE_DEVICE_TABLE(i2c, adt7462_id);
246
247static struct i2c_driver adt7462_driver = {
248 .class = I2C_CLASS_HWMON,
249 .driver = {
250 .name = "adt7462",
251 },
252 .probe = adt7462_probe,
253 .remove = adt7462_remove,
254 .id_table = adt7462_id,
255 .detect = adt7462_detect,
Jean Delvarec3813d62009-12-14 21:17:25 +0100256 .address_list = normal_i2c,
Darrick J. Wongc0b4e3a2008-11-12 13:27:03 -0800257};
258
259/*
260 * 16-bit registers on the ADT7462 are low-byte first. The data sheet says
261 * that the low byte must be read before the high byte.
262 */
263static inline int adt7462_read_word_data(struct i2c_client *client, u8 reg)
264{
265 u16 foo;
266 foo = i2c_smbus_read_byte_data(client, reg);
267 foo |= ((u16)i2c_smbus_read_byte_data(client, reg + 1) << 8);
268 return foo;
269}
270
271/* For some reason these registers are not contiguous. */
272static int ADT7462_REG_FAN(int fan)
273{
274 if (fan < 4)
275 return ADT7462_REG_FAN_BASE_ADDR + (2 * fan);
276 return ADT7462_REG_FAN2_BASE_ADDR + (2 * (fan - 4));
277}
278
279/* Voltage registers are scattered everywhere */
280static int ADT7462_REG_VOLT_MAX(struct adt7462_data *data, int which)
281{
282 switch (which) {
283 case 0:
284 if (!(data->pin_cfg[0] & ADT7462_PIN7_INPUT))
285 return 0x7C;
286 break;
287 case 1:
288 return 0x69;
289 case 2:
290 if (!(data->pin_cfg[1] & ADT7462_PIN22_INPUT))
291 return 0x7F;
292 break;
293 case 3:
294 if (!(data->pin_cfg[1] & ADT7462_PIN21_INPUT))
295 return 0x7E;
296 break;
297 case 4:
298 if (!(data->pin_cfg[0] & ADT7462_DIODE3_INPUT))
299 return 0x4B;
300 break;
301 case 5:
302 if (!(data->pin_cfg[0] & ADT7462_DIODE1_INPUT))
303 return 0x49;
304 break;
305 case 6:
306 if (!(data->pin_cfg[1] & ADT7462_PIN13_INPUT))
307 return 0x68;
308 break;
309 case 7:
310 if (!(data->pin_cfg[1] & ADT7462_PIN8_INPUT))
311 return 0x7D;
312 break;
313 case 8:
314 if (!(data->pin_cfg[2] & ADT7462_PIN26_VOLT_INPUT))
315 return 0x6C;
316 break;
317 case 9:
318 if (!(data->pin_cfg[2] & ADT7462_PIN25_VOLT_INPUT))
319 return 0x6B;
320 break;
321 case 10:
322 return 0x6A;
323 case 11:
324 if (data->pin_cfg[3] >> ADT7462_PIN28_SHIFT ==
325 ADT7462_PIN28_VOLT &&
326 !(data->pin_cfg[0] & ADT7462_VID_INPUT))
327 return 0x50;
328 break;
329 case 12:
330 if (data->pin_cfg[3] >> ADT7462_PIN28_SHIFT ==
331 ADT7462_PIN28_VOLT &&
332 !(data->pin_cfg[0] & ADT7462_VID_INPUT))
333 return 0x4C;
334 break;
335 }
Dan Carpentera4bf06d2013-08-14 12:45:26 +0300336 return 0;
Darrick J. Wongc0b4e3a2008-11-12 13:27:03 -0800337}
338
339static int ADT7462_REG_VOLT_MIN(struct adt7462_data *data, int which)
340{
341 switch (which) {
342 case 0:
343 if (!(data->pin_cfg[0] & ADT7462_PIN7_INPUT))
344 return 0x6D;
345 break;
346 case 1:
347 return 0x72;
348 case 2:
349 if (!(data->pin_cfg[1] & ADT7462_PIN22_INPUT))
350 return 0x6F;
351 break;
352 case 3:
353 if (!(data->pin_cfg[1] & ADT7462_PIN21_INPUT))
354 return 0x71;
355 break;
356 case 4:
357 if (!(data->pin_cfg[0] & ADT7462_DIODE3_INPUT))
358 return 0x47;
359 break;
360 case 5:
361 if (!(data->pin_cfg[0] & ADT7462_DIODE1_INPUT))
362 return 0x45;
363 break;
364 case 6:
365 if (!(data->pin_cfg[1] & ADT7462_PIN13_INPUT))
366 return 0x70;
367 break;
368 case 7:
369 if (!(data->pin_cfg[1] & ADT7462_PIN8_INPUT))
370 return 0x6E;
371 break;
372 case 8:
373 if (!(data->pin_cfg[2] & ADT7462_PIN26_VOLT_INPUT))
374 return 0x75;
375 break;
376 case 9:
377 if (!(data->pin_cfg[2] & ADT7462_PIN25_VOLT_INPUT))
378 return 0x74;
379 break;
380 case 10:
381 return 0x73;
382 case 11:
383 if (data->pin_cfg[3] >> ADT7462_PIN28_SHIFT ==
384 ADT7462_PIN28_VOLT &&
385 !(data->pin_cfg[0] & ADT7462_VID_INPUT))
386 return 0x76;
387 break;
388 case 12:
389 if (data->pin_cfg[3] >> ADT7462_PIN28_SHIFT ==
390 ADT7462_PIN28_VOLT &&
391 !(data->pin_cfg[0] & ADT7462_VID_INPUT))
392 return 0x77;
393 break;
394 }
Dan Carpentera4bf06d2013-08-14 12:45:26 +0300395 return 0;
Darrick J. Wongc0b4e3a2008-11-12 13:27:03 -0800396}
397
398static int ADT7462_REG_VOLT(struct adt7462_data *data, int which)
399{
400 switch (which) {
401 case 0:
402 if (!(data->pin_cfg[0] & ADT7462_PIN7_INPUT))
403 return 0xA3;
404 break;
405 case 1:
406 return 0x90;
407 case 2:
408 if (!(data->pin_cfg[1] & ADT7462_PIN22_INPUT))
409 return 0xA9;
410 break;
411 case 3:
412 if (!(data->pin_cfg[1] & ADT7462_PIN21_INPUT))
413 return 0xA7;
414 break;
415 case 4:
416 if (!(data->pin_cfg[0] & ADT7462_DIODE3_INPUT))
417 return 0x8F;
418 break;
419 case 5:
420 if (!(data->pin_cfg[0] & ADT7462_DIODE1_INPUT))
421 return 0x8B;
422 break;
423 case 6:
424 if (!(data->pin_cfg[1] & ADT7462_PIN13_INPUT))
425 return 0x96;
426 break;
427 case 7:
428 if (!(data->pin_cfg[1] & ADT7462_PIN8_INPUT))
429 return 0xA5;
430 break;
431 case 8:
432 if (!(data->pin_cfg[2] & ADT7462_PIN26_VOLT_INPUT))
433 return 0x93;
434 break;
435 case 9:
436 if (!(data->pin_cfg[2] & ADT7462_PIN25_VOLT_INPUT))
437 return 0x92;
438 break;
439 case 10:
440 return 0x91;
441 case 11:
442 if (data->pin_cfg[3] >> ADT7462_PIN28_SHIFT ==
443 ADT7462_PIN28_VOLT &&
444 !(data->pin_cfg[0] & ADT7462_VID_INPUT))
445 return 0x94;
446 break;
447 case 12:
448 if (data->pin_cfg[3] >> ADT7462_PIN28_SHIFT ==
449 ADT7462_PIN28_VOLT &&
450 !(data->pin_cfg[0] & ADT7462_VID_INPUT))
451 return 0x95;
452 break;
453 }
454 return -ENODEV;
455}
456
457/* Provide labels for sysfs */
458static const char *voltage_label(struct adt7462_data *data, int which)
459{
460 switch (which) {
461 case 0:
462 if (!(data->pin_cfg[0] & ADT7462_PIN7_INPUT))
463 return "+12V1";
464 break;
465 case 1:
466 switch (MASK_AND_SHIFT(data->pin_cfg[1], ADT7462_PIN23)) {
467 case 0:
468 return "Vccp1";
469 case 1:
470 return "+2.5V";
471 case 2:
472 return "+1.8V";
473 case 3:
474 return "+1.5V";
475 }
476 case 2:
477 if (!(data->pin_cfg[1] & ADT7462_PIN22_INPUT))
478 return "+12V3";
479 break;
480 case 3:
481 if (!(data->pin_cfg[1] & ADT7462_PIN21_INPUT))
482 return "+5V";
483 break;
484 case 4:
485 if (!(data->pin_cfg[0] & ADT7462_DIODE3_INPUT)) {
486 if (data->pin_cfg[1] & ADT7462_PIN19_INPUT)
487 return "+0.9V";
488 return "+1.25V";
489 }
490 break;
491 case 5:
492 if (!(data->pin_cfg[0] & ADT7462_DIODE1_INPUT)) {
493 if (data->pin_cfg[1] & ADT7462_PIN19_INPUT)
494 return "+1.8V";
495 return "+2.5V";
496 }
497 break;
498 case 6:
499 if (!(data->pin_cfg[1] & ADT7462_PIN13_INPUT))
500 return "+3.3V";
501 break;
502 case 7:
503 if (!(data->pin_cfg[1] & ADT7462_PIN8_INPUT))
504 return "+12V2";
505 break;
506 case 8:
507 switch (MASK_AND_SHIFT(data->pin_cfg[2], ADT7462_PIN26)) {
508 case 0:
509 return "Vbatt";
510 case 1:
511 return "FSB_Vtt";
512 }
513 break;
514 case 9:
515 switch (MASK_AND_SHIFT(data->pin_cfg[2], ADT7462_PIN25)) {
516 case 0:
517 return "+3.3V";
518 case 1:
519 return "+1.2V1";
520 }
521 break;
522 case 10:
523 switch (MASK_AND_SHIFT(data->pin_cfg[2], ADT7462_PIN24)) {
524 case 0:
525 return "Vccp2";
526 case 1:
527 return "+2.5V";
528 case 2:
529 return "+1.8V";
530 case 3:
531 return "+1.5";
532 }
533 case 11:
534 if (data->pin_cfg[3] >> ADT7462_PIN28_SHIFT ==
535 ADT7462_PIN28_VOLT &&
536 !(data->pin_cfg[0] & ADT7462_VID_INPUT))
537 return "+1.5V ICH";
538 break;
539 case 12:
540 if (data->pin_cfg[3] >> ADT7462_PIN28_SHIFT ==
541 ADT7462_PIN28_VOLT &&
542 !(data->pin_cfg[0] & ADT7462_VID_INPUT))
543 return "+1.5V 3GPIO";
544 break;
545 }
546 return "N/A";
547}
548
549/* Multipliers are actually in uV, not mV. */
550static int voltage_multiplier(struct adt7462_data *data, int which)
551{
552 switch (which) {
553 case 0:
554 if (!(data->pin_cfg[0] & ADT7462_PIN7_INPUT))
555 return 62500;
556 break;
557 case 1:
558 switch (MASK_AND_SHIFT(data->pin_cfg[1], ADT7462_PIN23)) {
559 case 0:
560 if (data->pin_cfg[0] & ADT7462_VID_INPUT)
561 return 12500;
562 return 6250;
563 case 1:
564 return 13000;
565 case 2:
566 return 9400;
567 case 3:
568 return 7800;
569 }
570 case 2:
571 if (!(data->pin_cfg[1] & ADT7462_PIN22_INPUT))
572 return 62500;
573 break;
574 case 3:
575 if (!(data->pin_cfg[1] & ADT7462_PIN21_INPUT))
576 return 26000;
577 break;
578 case 4:
579 if (!(data->pin_cfg[0] & ADT7462_DIODE3_INPUT)) {
580 if (data->pin_cfg[1] & ADT7462_PIN19_INPUT)
581 return 4690;
582 return 6500;
583 }
584 break;
585 case 5:
586 if (!(data->pin_cfg[0] & ADT7462_DIODE1_INPUT)) {
587 if (data->pin_cfg[1] & ADT7462_PIN15_INPUT)
588 return 9400;
589 return 13000;
590 }
591 break;
592 case 6:
593 if (!(data->pin_cfg[1] & ADT7462_PIN13_INPUT))
594 return 17200;
595 break;
596 case 7:
597 if (!(data->pin_cfg[1] & ADT7462_PIN8_INPUT))
598 return 62500;
599 break;
600 case 8:
601 switch (MASK_AND_SHIFT(data->pin_cfg[2], ADT7462_PIN26)) {
602 case 0:
603 return 15600;
604 case 1:
605 return 6250;
606 }
607 break;
608 case 9:
609 switch (MASK_AND_SHIFT(data->pin_cfg[2], ADT7462_PIN25)) {
610 case 0:
611 return 17200;
612 case 1:
613 return 6250;
614 }
615 break;
616 case 10:
617 switch (MASK_AND_SHIFT(data->pin_cfg[2], ADT7462_PIN24)) {
618 case 0:
619 return 6250;
620 case 1:
621 return 13000;
622 case 2:
623 return 9400;
624 case 3:
625 return 7800;
626 }
627 case 11:
628 case 12:
629 if (data->pin_cfg[3] >> ADT7462_PIN28_SHIFT ==
630 ADT7462_PIN28_VOLT &&
631 !(data->pin_cfg[0] & ADT7462_VID_INPUT))
632 return 7800;
633 }
634 return 0;
635}
636
637static int temp_enabled(struct adt7462_data *data, int which)
638{
639 switch (which) {
640 case 0:
641 case 2:
642 return 1;
643 case 1:
644 if (data->pin_cfg[0] & ADT7462_DIODE1_INPUT)
645 return 1;
646 break;
647 case 3:
648 if (data->pin_cfg[0] & ADT7462_DIODE3_INPUT)
649 return 1;
650 break;
651 }
652 return 0;
653}
654
655static const char *temp_label(struct adt7462_data *data, int which)
656{
657 switch (which) {
658 case 0:
659 return "local";
660 case 1:
661 if (data->pin_cfg[0] & ADT7462_DIODE1_INPUT)
662 return "remote1";
663 break;
664 case 2:
665 return "remote2";
666 case 3:
667 if (data->pin_cfg[0] & ADT7462_DIODE3_INPUT)
668 return "remote3";
669 break;
670 }
671 return "N/A";
672}
673
674/* Map Trange register values to mC */
675#define NUM_TRANGE_VALUES 16
676static const int trange_values[NUM_TRANGE_VALUES] = {
677 2000,
678 2500,
679 3300,
680 4000,
681 5000,
682 6700,
683 8000,
684 10000,
685 13300,
686 16000,
687 20000,
688 26700,
689 32000,
690 40000,
691 53300,
692 80000
693};
694
695static int find_trange_value(int trange)
696{
697 int i;
698
699 for (i = 0; i < NUM_TRANGE_VALUES; i++)
700 if (trange_values[i] == trange)
701 return i;
702
Guenter Roeckbb34c0d2013-09-13 10:38:09 -0700703 return -EINVAL;
Darrick J. Wongc0b4e3a2008-11-12 13:27:03 -0800704}
705
706static struct adt7462_data *adt7462_update_device(struct device *dev)
707{
708 struct i2c_client *client = to_i2c_client(dev);
709 struct adt7462_data *data = i2c_get_clientdata(client);
710 unsigned long local_jiffies = jiffies;
711 int i;
712
713 mutex_lock(&data->lock);
714 if (time_before(local_jiffies, data->sensors_last_updated +
715 SENSOR_REFRESH_INTERVAL)
716 && data->sensors_valid)
717 goto no_sensor_update;
718
719 for (i = 0; i < ADT7462_TEMP_COUNT; i++) {
720 /*
721 * Reading the fractional register locks the integral
722 * register until both have been read.
723 */
724 data->temp_frac[i] = i2c_smbus_read_byte_data(client,
725 ADT7462_TEMP_REG(i));
726 data->temp[i] = i2c_smbus_read_byte_data(client,
727 ADT7462_TEMP_REG(i) + 1);
728 }
729
730 for (i = 0; i < ADT7462_FAN_COUNT; i++)
731 data->fan[i] = adt7462_read_word_data(client,
732 ADT7462_REG_FAN(i));
733
734 data->fan_enabled = i2c_smbus_read_byte_data(client,
735 ADT7462_REG_FAN_ENABLE);
736
737 for (i = 0; i < ADT7462_PWM_COUNT; i++)
738 data->pwm[i] = i2c_smbus_read_byte_data(client,
739 ADT7462_REG_PWM(i));
740
741 for (i = 0; i < ADT7462_PIN_CFG_REG_COUNT; i++)
742 data->pin_cfg[i] = i2c_smbus_read_byte_data(client,
743 ADT7462_REG_PIN_CFG(i));
744
745 for (i = 0; i < ADT7462_VOLT_COUNT; i++) {
746 int reg = ADT7462_REG_VOLT(data, i);
747 if (!reg)
748 data->voltages[i] = 0;
749 else
750 data->voltages[i] = i2c_smbus_read_byte_data(client,
751 reg);
752 }
753
754 data->alarms[0] = i2c_smbus_read_byte_data(client, ADT7462_REG_ALARM1);
755 data->alarms[1] = i2c_smbus_read_byte_data(client, ADT7462_REG_ALARM2);
756 data->alarms[2] = i2c_smbus_read_byte_data(client, ADT7462_REG_ALARM3);
757 data->alarms[3] = i2c_smbus_read_byte_data(client, ADT7462_REG_ALARM4);
758
759 data->sensors_last_updated = local_jiffies;
760 data->sensors_valid = 1;
761
762no_sensor_update:
763 if (time_before(local_jiffies, data->limits_last_updated +
764 LIMIT_REFRESH_INTERVAL)
765 && data->limits_valid)
766 goto out;
767
768 for (i = 0; i < ADT7462_TEMP_COUNT; i++) {
769 data->temp_min[i] = i2c_smbus_read_byte_data(client,
770 ADT7462_TEMP_MIN_REG(i));
771 data->temp_max[i] = i2c_smbus_read_byte_data(client,
772 ADT7462_TEMP_MAX_REG(i));
773 }
774
775 for (i = 0; i < ADT7462_FAN_COUNT; i++)
776 data->fan_min[i] = i2c_smbus_read_byte_data(client,
777 ADT7462_REG_FAN_MIN(i));
778
779 for (i = 0; i < ADT7462_VOLT_COUNT; i++) {
780 int reg = ADT7462_REG_VOLT_MAX(data, i);
781 data->volt_max[i] =
782 (reg ? i2c_smbus_read_byte_data(client, reg) : 0);
783
784 reg = ADT7462_REG_VOLT_MIN(data, i);
785 data->volt_min[i] =
786 (reg ? i2c_smbus_read_byte_data(client, reg) : 0);
787 }
788
789 for (i = 0; i < ADT7462_PWM_COUNT; i++) {
790 data->pwm_min[i] = i2c_smbus_read_byte_data(client,
791 ADT7462_REG_PWM_MIN(i));
792 data->pwm_tmin[i] = i2c_smbus_read_byte_data(client,
793 ADT7462_REG_PWM_TMIN(i));
794 data->pwm_trange[i] = i2c_smbus_read_byte_data(client,
795 ADT7462_REG_PWM_TRANGE(i));
796 data->pwm_cfg[i] = i2c_smbus_read_byte_data(client,
797 ADT7462_REG_PWM_CFG(i));
798 }
799
800 data->pwm_max = i2c_smbus_read_byte_data(client, ADT7462_REG_PWM_MAX);
801
802 data->cfg2 = i2c_smbus_read_byte_data(client, ADT7462_REG_CFG2);
803
804 data->limits_last_updated = local_jiffies;
805 data->limits_valid = 1;
806
807out:
808 mutex_unlock(&data->lock);
809 return data;
810}
811
812static ssize_t show_temp_min(struct device *dev,
813 struct device_attribute *devattr,
814 char *buf)
815{
816 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
817 struct adt7462_data *data = adt7462_update_device(dev);
818
819 if (!temp_enabled(data, attr->index))
820 return sprintf(buf, "0\n");
821
822 return sprintf(buf, "%d\n", 1000 * (data->temp_min[attr->index] - 64));
823}
824
825static ssize_t set_temp_min(struct device *dev,
826 struct device_attribute *devattr,
827 const char *buf,
828 size_t count)
829{
830 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
831 struct i2c_client *client = to_i2c_client(dev);
832 struct adt7462_data *data = i2c_get_clientdata(client);
833 long temp;
834
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100835 if (kstrtol(buf, 10, &temp) || !temp_enabled(data, attr->index))
Darrick J. Wongc0b4e3a2008-11-12 13:27:03 -0800836 return -EINVAL;
837
Darrick J. Wong8f8c1fb2009-01-06 14:41:31 -0800838 temp = DIV_ROUND_CLOSEST(temp, 1000) + 64;
Guenter Roeck2a844c12013-01-09 08:09:34 -0800839 temp = clamp_val(temp, 0, 255);
Darrick J. Wongc0b4e3a2008-11-12 13:27:03 -0800840
841 mutex_lock(&data->lock);
842 data->temp_min[attr->index] = temp;
843 i2c_smbus_write_byte_data(client, ADT7462_TEMP_MIN_REG(attr->index),
844 temp);
845 mutex_unlock(&data->lock);
846
847 return count;
848}
849
850static ssize_t show_temp_max(struct device *dev,
851 struct device_attribute *devattr,
852 char *buf)
853{
854 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
855 struct adt7462_data *data = adt7462_update_device(dev);
856
857 if (!temp_enabled(data, attr->index))
858 return sprintf(buf, "0\n");
859
860 return sprintf(buf, "%d\n", 1000 * (data->temp_max[attr->index] - 64));
861}
862
863static ssize_t set_temp_max(struct device *dev,
864 struct device_attribute *devattr,
865 const char *buf,
866 size_t count)
867{
868 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
869 struct i2c_client *client = to_i2c_client(dev);
870 struct adt7462_data *data = i2c_get_clientdata(client);
871 long temp;
872
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100873 if (kstrtol(buf, 10, &temp) || !temp_enabled(data, attr->index))
Darrick J. Wongc0b4e3a2008-11-12 13:27:03 -0800874 return -EINVAL;
875
Darrick J. Wong8f8c1fb2009-01-06 14:41:31 -0800876 temp = DIV_ROUND_CLOSEST(temp, 1000) + 64;
Guenter Roeck2a844c12013-01-09 08:09:34 -0800877 temp = clamp_val(temp, 0, 255);
Darrick J. Wongc0b4e3a2008-11-12 13:27:03 -0800878
879 mutex_lock(&data->lock);
880 data->temp_max[attr->index] = temp;
881 i2c_smbus_write_byte_data(client, ADT7462_TEMP_MAX_REG(attr->index),
882 temp);
883 mutex_unlock(&data->lock);
884
885 return count;
886}
887
888static ssize_t show_temp(struct device *dev, struct device_attribute *devattr,
889 char *buf)
890{
891 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
892 struct adt7462_data *data = adt7462_update_device(dev);
893 u8 frac = data->temp_frac[attr->index] >> TEMP_FRAC_OFFSET;
894
895 if (!temp_enabled(data, attr->index))
896 return sprintf(buf, "0\n");
897
898 return sprintf(buf, "%d\n", 1000 * (data->temp[attr->index] - 64) +
899 250 * frac);
900}
901
902static ssize_t show_temp_label(struct device *dev,
903 struct device_attribute *devattr,
904 char *buf)
905{
906 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
907 struct adt7462_data *data = adt7462_update_device(dev);
908
909 return sprintf(buf, "%s\n", temp_label(data, attr->index));
910}
911
912static ssize_t show_volt_max(struct device *dev,
913 struct device_attribute *devattr,
914 char *buf)
915{
916 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
917 struct adt7462_data *data = adt7462_update_device(dev);
918 int x = voltage_multiplier(data, attr->index);
919
920 x *= data->volt_max[attr->index];
921 x /= 1000; /* convert from uV to mV */
922
923 return sprintf(buf, "%d\n", x);
924}
925
926static ssize_t set_volt_max(struct device *dev,
927 struct device_attribute *devattr,
928 const char *buf,
929 size_t count)
930{
931 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
932 struct i2c_client *client = to_i2c_client(dev);
933 struct adt7462_data *data = i2c_get_clientdata(client);
934 int x = voltage_multiplier(data, attr->index);
935 long temp;
936
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100937 if (kstrtol(buf, 10, &temp) || !x)
Darrick J. Wongc0b4e3a2008-11-12 13:27:03 -0800938 return -EINVAL;
939
940 temp *= 1000; /* convert mV to uV */
Darrick J. Wong8f8c1fb2009-01-06 14:41:31 -0800941 temp = DIV_ROUND_CLOSEST(temp, x);
Guenter Roeck2a844c12013-01-09 08:09:34 -0800942 temp = clamp_val(temp, 0, 255);
Darrick J. Wongc0b4e3a2008-11-12 13:27:03 -0800943
944 mutex_lock(&data->lock);
945 data->volt_max[attr->index] = temp;
946 i2c_smbus_write_byte_data(client,
947 ADT7462_REG_VOLT_MAX(data, attr->index),
948 temp);
949 mutex_unlock(&data->lock);
950
951 return count;
952}
953
954static ssize_t show_volt_min(struct device *dev,
955 struct device_attribute *devattr,
956 char *buf)
957{
958 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
959 struct adt7462_data *data = adt7462_update_device(dev);
960 int x = voltage_multiplier(data, attr->index);
961
962 x *= data->volt_min[attr->index];
963 x /= 1000; /* convert from uV to mV */
964
965 return sprintf(buf, "%d\n", x);
966}
967
968static ssize_t set_volt_min(struct device *dev,
969 struct device_attribute *devattr,
970 const char *buf,
971 size_t count)
972{
973 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
974 struct i2c_client *client = to_i2c_client(dev);
975 struct adt7462_data *data = i2c_get_clientdata(client);
976 int x = voltage_multiplier(data, attr->index);
977 long temp;
978
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100979 if (kstrtol(buf, 10, &temp) || !x)
Darrick J. Wongc0b4e3a2008-11-12 13:27:03 -0800980 return -EINVAL;
981
982 temp *= 1000; /* convert mV to uV */
Darrick J. Wong8f8c1fb2009-01-06 14:41:31 -0800983 temp = DIV_ROUND_CLOSEST(temp, x);
Guenter Roeck2a844c12013-01-09 08:09:34 -0800984 temp = clamp_val(temp, 0, 255);
Darrick J. Wongc0b4e3a2008-11-12 13:27:03 -0800985
986 mutex_lock(&data->lock);
987 data->volt_min[attr->index] = temp;
988 i2c_smbus_write_byte_data(client,
989 ADT7462_REG_VOLT_MIN(data, attr->index),
990 temp);
991 mutex_unlock(&data->lock);
992
993 return count;
994}
995
996static ssize_t show_voltage(struct device *dev,
997 struct device_attribute *devattr,
998 char *buf)
999{
1000 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
1001 struct adt7462_data *data = adt7462_update_device(dev);
1002 int x = voltage_multiplier(data, attr->index);
1003
1004 x *= data->voltages[attr->index];
1005 x /= 1000; /* convert from uV to mV */
1006
1007 return sprintf(buf, "%d\n", x);
1008}
1009
1010static ssize_t show_voltage_label(struct device *dev,
1011 struct device_attribute *devattr,
1012 char *buf)
1013{
1014 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
1015 struct adt7462_data *data = adt7462_update_device(dev);
1016
1017 return sprintf(buf, "%s\n", voltage_label(data, attr->index));
1018}
1019
1020static ssize_t show_alarm(struct device *dev,
1021 struct device_attribute *devattr,
1022 char *buf)
1023{
1024 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
1025 struct adt7462_data *data = adt7462_update_device(dev);
1026 int reg = attr->index >> ADT7462_ALARM_REG_SHIFT;
1027 int mask = attr->index & ADT7462_ALARM_FLAG_MASK;
1028
1029 if (data->alarms[reg] & mask)
1030 return sprintf(buf, "1\n");
1031 else
1032 return sprintf(buf, "0\n");
1033}
1034
1035static int fan_enabled(struct adt7462_data *data, int fan)
1036{
1037 return data->fan_enabled & (1 << fan);
1038}
1039
1040static ssize_t show_fan_min(struct device *dev,
1041 struct device_attribute *devattr,
1042 char *buf)
1043{
1044 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
1045 struct adt7462_data *data = adt7462_update_device(dev);
1046 u16 temp;
1047
1048 /* Only the MSB of the min fan period is stored... */
1049 temp = data->fan_min[attr->index];
1050 temp <<= 8;
1051
1052 if (!fan_enabled(data, attr->index) ||
1053 !FAN_DATA_VALID(temp))
1054 return sprintf(buf, "0\n");
1055
1056 return sprintf(buf, "%d\n", FAN_PERIOD_TO_RPM(temp));
1057}
1058
1059static ssize_t set_fan_min(struct device *dev,
1060 struct device_attribute *devattr,
1061 const char *buf, size_t count)
1062{
1063 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
1064 struct i2c_client *client = to_i2c_client(dev);
1065 struct adt7462_data *data = i2c_get_clientdata(client);
1066 long temp;
1067
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001068 if (kstrtol(buf, 10, &temp) || !temp ||
Darrick J. Wongc0b4e3a2008-11-12 13:27:03 -08001069 !fan_enabled(data, attr->index))
1070 return -EINVAL;
1071
1072 temp = FAN_RPM_TO_PERIOD(temp);
1073 temp >>= 8;
Guenter Roeck2a844c12013-01-09 08:09:34 -08001074 temp = clamp_val(temp, 1, 255);
Darrick J. Wongc0b4e3a2008-11-12 13:27:03 -08001075
1076 mutex_lock(&data->lock);
1077 data->fan_min[attr->index] = temp;
1078 i2c_smbus_write_byte_data(client, ADT7462_REG_FAN_MIN(attr->index),
1079 temp);
1080 mutex_unlock(&data->lock);
1081
1082 return count;
1083}
1084
1085static ssize_t show_fan(struct device *dev, struct device_attribute *devattr,
1086 char *buf)
1087{
1088 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
1089 struct adt7462_data *data = adt7462_update_device(dev);
1090
1091 if (!fan_enabled(data, attr->index) ||
1092 !FAN_DATA_VALID(data->fan[attr->index]))
1093 return sprintf(buf, "0\n");
1094
1095 return sprintf(buf, "%d\n",
1096 FAN_PERIOD_TO_RPM(data->fan[attr->index]));
1097}
1098
1099static ssize_t show_force_pwm_max(struct device *dev,
1100 struct device_attribute *devattr,
1101 char *buf)
1102{
1103 struct adt7462_data *data = adt7462_update_device(dev);
1104 return sprintf(buf, "%d\n", (data->cfg2 & ADT7462_FSPD_MASK ? 1 : 0));
1105}
1106
1107static ssize_t set_force_pwm_max(struct device *dev,
1108 struct device_attribute *devattr,
1109 const char *buf,
1110 size_t count)
1111{
1112 struct i2c_client *client = to_i2c_client(dev);
1113 struct adt7462_data *data = i2c_get_clientdata(client);
1114 long temp;
1115 u8 reg;
1116
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001117 if (kstrtol(buf, 10, &temp))
Darrick J. Wongc0b4e3a2008-11-12 13:27:03 -08001118 return -EINVAL;
1119
1120 mutex_lock(&data->lock);
1121 reg = i2c_smbus_read_byte_data(client, ADT7462_REG_CFG2);
1122 if (temp)
1123 reg |= ADT7462_FSPD_MASK;
1124 else
1125 reg &= ~ADT7462_FSPD_MASK;
1126 data->cfg2 = reg;
1127 i2c_smbus_write_byte_data(client, ADT7462_REG_CFG2, reg);
1128 mutex_unlock(&data->lock);
1129
1130 return count;
1131}
1132
1133static ssize_t show_pwm(struct device *dev, struct device_attribute *devattr,
1134 char *buf)
1135{
1136 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
1137 struct adt7462_data *data = adt7462_update_device(dev);
1138 return sprintf(buf, "%d\n", data->pwm[attr->index]);
1139}
1140
1141static ssize_t set_pwm(struct device *dev, struct device_attribute *devattr,
1142 const char *buf, size_t count)
1143{
1144 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
1145 struct i2c_client *client = to_i2c_client(dev);
1146 struct adt7462_data *data = i2c_get_clientdata(client);
1147 long temp;
1148
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001149 if (kstrtol(buf, 10, &temp))
Darrick J. Wongc0b4e3a2008-11-12 13:27:03 -08001150 return -EINVAL;
1151
Guenter Roeck2a844c12013-01-09 08:09:34 -08001152 temp = clamp_val(temp, 0, 255);
Darrick J. Wongc0b4e3a2008-11-12 13:27:03 -08001153
1154 mutex_lock(&data->lock);
1155 data->pwm[attr->index] = temp;
1156 i2c_smbus_write_byte_data(client, ADT7462_REG_PWM(attr->index), temp);
1157 mutex_unlock(&data->lock);
1158
1159 return count;
1160}
1161
1162static ssize_t show_pwm_max(struct device *dev,
1163 struct device_attribute *devattr,
1164 char *buf)
1165{
1166 struct adt7462_data *data = adt7462_update_device(dev);
1167 return sprintf(buf, "%d\n", data->pwm_max);
1168}
1169
1170static ssize_t set_pwm_max(struct device *dev,
1171 struct device_attribute *devattr,
1172 const char *buf,
1173 size_t count)
1174{
1175 struct i2c_client *client = to_i2c_client(dev);
1176 struct adt7462_data *data = i2c_get_clientdata(client);
1177 long temp;
1178
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001179 if (kstrtol(buf, 10, &temp))
Darrick J. Wongc0b4e3a2008-11-12 13:27:03 -08001180 return -EINVAL;
1181
Guenter Roeck2a844c12013-01-09 08:09:34 -08001182 temp = clamp_val(temp, 0, 255);
Darrick J. Wongc0b4e3a2008-11-12 13:27:03 -08001183
1184 mutex_lock(&data->lock);
1185 data->pwm_max = temp;
1186 i2c_smbus_write_byte_data(client, ADT7462_REG_PWM_MAX, temp);
1187 mutex_unlock(&data->lock);
1188
1189 return count;
1190}
1191
1192static ssize_t show_pwm_min(struct device *dev,
1193 struct device_attribute *devattr,
1194 char *buf)
1195{
1196 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
1197 struct adt7462_data *data = adt7462_update_device(dev);
1198 return sprintf(buf, "%d\n", data->pwm_min[attr->index]);
1199}
1200
1201static ssize_t set_pwm_min(struct device *dev,
1202 struct device_attribute *devattr,
1203 const char *buf,
1204 size_t count)
1205{
1206 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
1207 struct i2c_client *client = to_i2c_client(dev);
1208 struct adt7462_data *data = i2c_get_clientdata(client);
1209 long temp;
1210
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001211 if (kstrtol(buf, 10, &temp))
Darrick J. Wongc0b4e3a2008-11-12 13:27:03 -08001212 return -EINVAL;
1213
Guenter Roeck2a844c12013-01-09 08:09:34 -08001214 temp = clamp_val(temp, 0, 255);
Darrick J. Wongc0b4e3a2008-11-12 13:27:03 -08001215
1216 mutex_lock(&data->lock);
1217 data->pwm_min[attr->index] = temp;
1218 i2c_smbus_write_byte_data(client, ADT7462_REG_PWM_MIN(attr->index),
1219 temp);
1220 mutex_unlock(&data->lock);
1221
1222 return count;
1223}
1224
1225static ssize_t show_pwm_hyst(struct device *dev,
1226 struct device_attribute *devattr,
1227 char *buf)
1228{
1229 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
1230 struct adt7462_data *data = adt7462_update_device(dev);
1231 return sprintf(buf, "%d\n", 1000 *
1232 (data->pwm_trange[attr->index] & ADT7462_PWM_HYST_MASK));
1233}
1234
1235static ssize_t set_pwm_hyst(struct device *dev,
1236 struct device_attribute *devattr,
1237 const char *buf,
1238 size_t count)
1239{
1240 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
1241 struct i2c_client *client = to_i2c_client(dev);
1242 struct adt7462_data *data = i2c_get_clientdata(client);
1243 long temp;
1244
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001245 if (kstrtol(buf, 10, &temp))
Darrick J. Wongc0b4e3a2008-11-12 13:27:03 -08001246 return -EINVAL;
1247
Darrick J. Wong8f8c1fb2009-01-06 14:41:31 -08001248 temp = DIV_ROUND_CLOSEST(temp, 1000);
Guenter Roeck2a844c12013-01-09 08:09:34 -08001249 temp = clamp_val(temp, 0, 15);
Darrick J. Wongc0b4e3a2008-11-12 13:27:03 -08001250
1251 /* package things up */
1252 temp &= ADT7462_PWM_HYST_MASK;
1253 temp |= data->pwm_trange[attr->index] & ADT7462_PWM_RANGE_MASK;
1254
1255 mutex_lock(&data->lock);
1256 data->pwm_trange[attr->index] = temp;
1257 i2c_smbus_write_byte_data(client, ADT7462_REG_PWM_TRANGE(attr->index),
1258 temp);
1259 mutex_unlock(&data->lock);
1260
1261 return count;
1262}
1263
1264static ssize_t show_pwm_tmax(struct device *dev,
1265 struct device_attribute *devattr,
1266 char *buf)
1267{
1268 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
1269 struct adt7462_data *data = adt7462_update_device(dev);
1270
1271 /* tmax = tmin + trange */
1272 int trange = trange_values[data->pwm_trange[attr->index] >>
1273 ADT7462_PWM_RANGE_SHIFT];
1274 int tmin = (data->pwm_tmin[attr->index] - 64) * 1000;
1275
1276 return sprintf(buf, "%d\n", tmin + trange);
1277}
1278
1279static ssize_t set_pwm_tmax(struct device *dev,
1280 struct device_attribute *devattr,
1281 const char *buf,
1282 size_t count)
1283{
1284 int temp;
1285 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
1286 struct i2c_client *client = to_i2c_client(dev);
1287 struct adt7462_data *data = i2c_get_clientdata(client);
1288 int tmin, trange_value;
1289 long trange;
1290
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001291 if (kstrtol(buf, 10, &trange))
Darrick J. Wongc0b4e3a2008-11-12 13:27:03 -08001292 return -EINVAL;
1293
1294 /* trange = tmax - tmin */
1295 tmin = (data->pwm_tmin[attr->index] - 64) * 1000;
1296 trange_value = find_trange_value(trange - tmin);
Darrick J. Wongc0b4e3a2008-11-12 13:27:03 -08001297 if (trange_value < 0)
Guenter Roeckbb34c0d2013-09-13 10:38:09 -07001298 return trange_value;
Darrick J. Wongc0b4e3a2008-11-12 13:27:03 -08001299
1300 temp = trange_value << ADT7462_PWM_RANGE_SHIFT;
1301 temp |= data->pwm_trange[attr->index] & ADT7462_PWM_HYST_MASK;
1302
1303 mutex_lock(&data->lock);
1304 data->pwm_trange[attr->index] = temp;
1305 i2c_smbus_write_byte_data(client, ADT7462_REG_PWM_TRANGE(attr->index),
1306 temp);
1307 mutex_unlock(&data->lock);
1308
1309 return count;
1310}
1311
1312static ssize_t show_pwm_tmin(struct device *dev,
1313 struct device_attribute *devattr,
1314 char *buf)
1315{
1316 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
1317 struct adt7462_data *data = adt7462_update_device(dev);
1318 return sprintf(buf, "%d\n", 1000 * (data->pwm_tmin[attr->index] - 64));
1319}
1320
1321static ssize_t set_pwm_tmin(struct device *dev,
1322 struct device_attribute *devattr,
1323 const char *buf,
1324 size_t count)
1325{
1326 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
1327 struct i2c_client *client = to_i2c_client(dev);
1328 struct adt7462_data *data = i2c_get_clientdata(client);
1329 long temp;
1330
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001331 if (kstrtol(buf, 10, &temp))
Darrick J. Wongc0b4e3a2008-11-12 13:27:03 -08001332 return -EINVAL;
1333
Darrick J. Wong8f8c1fb2009-01-06 14:41:31 -08001334 temp = DIV_ROUND_CLOSEST(temp, 1000) + 64;
Guenter Roeck2a844c12013-01-09 08:09:34 -08001335 temp = clamp_val(temp, 0, 255);
Darrick J. Wongc0b4e3a2008-11-12 13:27:03 -08001336
1337 mutex_lock(&data->lock);
1338 data->pwm_tmin[attr->index] = temp;
1339 i2c_smbus_write_byte_data(client, ADT7462_REG_PWM_TMIN(attr->index),
1340 temp);
1341 mutex_unlock(&data->lock);
1342
1343 return count;
1344}
1345
1346static ssize_t show_pwm_auto(struct device *dev,
1347 struct device_attribute *devattr,
1348 char *buf)
1349{
1350 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
1351 struct adt7462_data *data = adt7462_update_device(dev);
1352 int cfg = data->pwm_cfg[attr->index] >> ADT7462_PWM_CHANNEL_SHIFT;
1353
1354 switch (cfg) {
1355 case 4: /* off */
1356 return sprintf(buf, "0\n");
1357 case 7: /* manual */
1358 return sprintf(buf, "1\n");
1359 default: /* automatic */
1360 return sprintf(buf, "2\n");
1361 }
1362}
1363
1364static void set_pwm_channel(struct i2c_client *client,
1365 struct adt7462_data *data,
1366 int which,
1367 int value)
1368{
1369 int temp = data->pwm_cfg[which] & ~ADT7462_PWM_CHANNEL_MASK;
1370 temp |= value << ADT7462_PWM_CHANNEL_SHIFT;
1371
1372 mutex_lock(&data->lock);
1373 data->pwm_cfg[which] = temp;
1374 i2c_smbus_write_byte_data(client, ADT7462_REG_PWM_CFG(which), temp);
1375 mutex_unlock(&data->lock);
1376}
1377
1378static ssize_t set_pwm_auto(struct device *dev,
1379 struct device_attribute *devattr,
1380 const char *buf,
1381 size_t count)
1382{
1383 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
1384 struct i2c_client *client = to_i2c_client(dev);
1385 struct adt7462_data *data = i2c_get_clientdata(client);
1386 long temp;
1387
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001388 if (kstrtol(buf, 10, &temp))
Darrick J. Wongc0b4e3a2008-11-12 13:27:03 -08001389 return -EINVAL;
1390
1391 switch (temp) {
1392 case 0: /* off */
1393 set_pwm_channel(client, data, attr->index, 4);
1394 return count;
1395 case 1: /* manual */
1396 set_pwm_channel(client, data, attr->index, 7);
1397 return count;
1398 default:
1399 return -EINVAL;
1400 }
1401}
1402
1403static ssize_t show_pwm_auto_temp(struct device *dev,
1404 struct device_attribute *devattr,
1405 char *buf)
1406{
1407 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
1408 struct adt7462_data *data = adt7462_update_device(dev);
1409 int channel = data->pwm_cfg[attr->index] >> ADT7462_PWM_CHANNEL_SHIFT;
1410
1411 switch (channel) {
1412 case 0: /* temp[1234] only */
1413 case 1:
1414 case 2:
1415 case 3:
1416 return sprintf(buf, "%d\n", (1 << channel));
1417 case 5: /* temp1 & temp4 */
1418 return sprintf(buf, "9\n");
1419 case 6:
1420 return sprintf(buf, "15\n");
1421 default:
1422 return sprintf(buf, "0\n");
1423 }
1424}
1425
1426static int cvt_auto_temp(int input)
1427{
1428 if (input == 0xF)
1429 return 6;
1430 if (input == 0x9)
1431 return 5;
1432 if (input < 1 || !is_power_of_2(input))
1433 return -EINVAL;
1434 return ilog2(input);
1435}
1436
1437static ssize_t set_pwm_auto_temp(struct device *dev,
1438 struct device_attribute *devattr,
1439 const char *buf,
1440 size_t count)
1441{
1442 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
1443 struct i2c_client *client = to_i2c_client(dev);
1444 struct adt7462_data *data = i2c_get_clientdata(client);
1445 long temp;
1446
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +01001447 if (kstrtol(buf, 10, &temp))
Darrick J. Wongc0b4e3a2008-11-12 13:27:03 -08001448 return -EINVAL;
1449
1450 temp = cvt_auto_temp(temp);
1451 if (temp < 0)
1452 return temp;
1453
1454 set_pwm_channel(client, data, attr->index, temp);
1455
1456 return count;
1457}
1458
1459static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp_max,
1460 set_temp_max, 0);
1461static SENSOR_DEVICE_ATTR(temp2_max, S_IWUSR | S_IRUGO, show_temp_max,
1462 set_temp_max, 1);
1463static SENSOR_DEVICE_ATTR(temp3_max, S_IWUSR | S_IRUGO, show_temp_max,
1464 set_temp_max, 2);
1465static SENSOR_DEVICE_ATTR(temp4_max, S_IWUSR | S_IRUGO, show_temp_max,
1466 set_temp_max, 3);
1467
1468static SENSOR_DEVICE_ATTR(temp1_min, S_IWUSR | S_IRUGO, show_temp_min,
1469 set_temp_min, 0);
1470static SENSOR_DEVICE_ATTR(temp2_min, S_IWUSR | S_IRUGO, show_temp_min,
1471 set_temp_min, 1);
1472static SENSOR_DEVICE_ATTR(temp3_min, S_IWUSR | S_IRUGO, show_temp_min,
1473 set_temp_min, 2);
1474static SENSOR_DEVICE_ATTR(temp4_min, S_IWUSR | S_IRUGO, show_temp_min,
1475 set_temp_min, 3);
1476
1477static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0);
1478static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp, NULL, 1);
1479static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO, show_temp, NULL, 2);
1480static SENSOR_DEVICE_ATTR(temp4_input, S_IRUGO, show_temp, NULL, 3);
1481
1482static SENSOR_DEVICE_ATTR(temp1_label, S_IRUGO, show_temp_label, NULL, 0);
1483static SENSOR_DEVICE_ATTR(temp2_label, S_IRUGO, show_temp_label, NULL, 1);
1484static SENSOR_DEVICE_ATTR(temp3_label, S_IRUGO, show_temp_label, NULL, 2);
1485static SENSOR_DEVICE_ATTR(temp4_label, S_IRUGO, show_temp_label, NULL, 3);
1486
1487static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL,
1488 ADT7462_ALARM1 | ADT7462_LT_ALARM);
1489static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL,
1490 ADT7462_ALARM1 | ADT7462_R1T_ALARM);
1491static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL,
1492 ADT7462_ALARM1 | ADT7462_R2T_ALARM);
1493static SENSOR_DEVICE_ATTR(temp4_alarm, S_IRUGO, show_alarm, NULL,
1494 ADT7462_ALARM1 | ADT7462_R3T_ALARM);
1495
1496static SENSOR_DEVICE_ATTR(in1_max, S_IWUSR | S_IRUGO, show_volt_max,
1497 set_volt_max, 0);
1498static SENSOR_DEVICE_ATTR(in2_max, S_IWUSR | S_IRUGO, show_volt_max,
1499 set_volt_max, 1);
1500static SENSOR_DEVICE_ATTR(in3_max, S_IWUSR | S_IRUGO, show_volt_max,
1501 set_volt_max, 2);
1502static SENSOR_DEVICE_ATTR(in4_max, S_IWUSR | S_IRUGO, show_volt_max,
1503 set_volt_max, 3);
1504static SENSOR_DEVICE_ATTR(in5_max, S_IWUSR | S_IRUGO, show_volt_max,
1505 set_volt_max, 4);
1506static SENSOR_DEVICE_ATTR(in6_max, S_IWUSR | S_IRUGO, show_volt_max,
1507 set_volt_max, 5);
1508static SENSOR_DEVICE_ATTR(in7_max, S_IWUSR | S_IRUGO, show_volt_max,
1509 set_volt_max, 6);
1510static SENSOR_DEVICE_ATTR(in8_max, S_IWUSR | S_IRUGO, show_volt_max,
1511 set_volt_max, 7);
1512static SENSOR_DEVICE_ATTR(in9_max, S_IWUSR | S_IRUGO, show_volt_max,
1513 set_volt_max, 8);
1514static SENSOR_DEVICE_ATTR(in10_max, S_IWUSR | S_IRUGO, show_volt_max,
1515 set_volt_max, 9);
1516static SENSOR_DEVICE_ATTR(in11_max, S_IWUSR | S_IRUGO, show_volt_max,
1517 set_volt_max, 10);
1518static SENSOR_DEVICE_ATTR(in12_max, S_IWUSR | S_IRUGO, show_volt_max,
1519 set_volt_max, 11);
1520static SENSOR_DEVICE_ATTR(in13_max, S_IWUSR | S_IRUGO, show_volt_max,
1521 set_volt_max, 12);
1522
1523static SENSOR_DEVICE_ATTR(in1_min, S_IWUSR | S_IRUGO, show_volt_min,
1524 set_volt_min, 0);
1525static SENSOR_DEVICE_ATTR(in2_min, S_IWUSR | S_IRUGO, show_volt_min,
1526 set_volt_min, 1);
1527static SENSOR_DEVICE_ATTR(in3_min, S_IWUSR | S_IRUGO, show_volt_min,
1528 set_volt_min, 2);
1529static SENSOR_DEVICE_ATTR(in4_min, S_IWUSR | S_IRUGO, show_volt_min,
1530 set_volt_min, 3);
1531static SENSOR_DEVICE_ATTR(in5_min, S_IWUSR | S_IRUGO, show_volt_min,
1532 set_volt_min, 4);
1533static SENSOR_DEVICE_ATTR(in6_min, S_IWUSR | S_IRUGO, show_volt_min,
1534 set_volt_min, 5);
1535static SENSOR_DEVICE_ATTR(in7_min, S_IWUSR | S_IRUGO, show_volt_min,
1536 set_volt_min, 6);
1537static SENSOR_DEVICE_ATTR(in8_min, S_IWUSR | S_IRUGO, show_volt_min,
1538 set_volt_min, 7);
1539static SENSOR_DEVICE_ATTR(in9_min, S_IWUSR | S_IRUGO, show_volt_min,
1540 set_volt_min, 8);
1541static SENSOR_DEVICE_ATTR(in10_min, S_IWUSR | S_IRUGO, show_volt_min,
1542 set_volt_min, 9);
1543static SENSOR_DEVICE_ATTR(in11_min, S_IWUSR | S_IRUGO, show_volt_min,
1544 set_volt_min, 10);
1545static SENSOR_DEVICE_ATTR(in12_min, S_IWUSR | S_IRUGO, show_volt_min,
1546 set_volt_min, 11);
1547static SENSOR_DEVICE_ATTR(in13_min, S_IWUSR | S_IRUGO, show_volt_min,
1548 set_volt_min, 12);
1549
1550static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, show_voltage, NULL, 0);
1551static SENSOR_DEVICE_ATTR(in2_input, S_IRUGO, show_voltage, NULL, 1);
1552static SENSOR_DEVICE_ATTR(in3_input, S_IRUGO, show_voltage, NULL, 2);
1553static SENSOR_DEVICE_ATTR(in4_input, S_IRUGO, show_voltage, NULL, 3);
1554static SENSOR_DEVICE_ATTR(in5_input, S_IRUGO, show_voltage, NULL, 4);
1555static SENSOR_DEVICE_ATTR(in6_input, S_IRUGO, show_voltage, NULL, 5);
1556static SENSOR_DEVICE_ATTR(in7_input, S_IRUGO, show_voltage, NULL, 6);
1557static SENSOR_DEVICE_ATTR(in8_input, S_IRUGO, show_voltage, NULL, 7);
1558static SENSOR_DEVICE_ATTR(in9_input, S_IRUGO, show_voltage, NULL, 8);
1559static SENSOR_DEVICE_ATTR(in10_input, S_IRUGO, show_voltage, NULL, 9);
1560static SENSOR_DEVICE_ATTR(in11_input, S_IRUGO, show_voltage, NULL, 10);
1561static SENSOR_DEVICE_ATTR(in12_input, S_IRUGO, show_voltage, NULL, 11);
1562static SENSOR_DEVICE_ATTR(in13_input, S_IRUGO, show_voltage, NULL, 12);
1563
1564static SENSOR_DEVICE_ATTR(in1_label, S_IRUGO, show_voltage_label, NULL, 0);
1565static SENSOR_DEVICE_ATTR(in2_label, S_IRUGO, show_voltage_label, NULL, 1);
1566static SENSOR_DEVICE_ATTR(in3_label, S_IRUGO, show_voltage_label, NULL, 2);
1567static SENSOR_DEVICE_ATTR(in4_label, S_IRUGO, show_voltage_label, NULL, 3);
1568static SENSOR_DEVICE_ATTR(in5_label, S_IRUGO, show_voltage_label, NULL, 4);
1569static SENSOR_DEVICE_ATTR(in6_label, S_IRUGO, show_voltage_label, NULL, 5);
1570static SENSOR_DEVICE_ATTR(in7_label, S_IRUGO, show_voltage_label, NULL, 6);
1571static SENSOR_DEVICE_ATTR(in8_label, S_IRUGO, show_voltage_label, NULL, 7);
1572static SENSOR_DEVICE_ATTR(in9_label, S_IRUGO, show_voltage_label, NULL, 8);
1573static SENSOR_DEVICE_ATTR(in10_label, S_IRUGO, show_voltage_label, NULL, 9);
1574static SENSOR_DEVICE_ATTR(in11_label, S_IRUGO, show_voltage_label, NULL, 10);
1575static SENSOR_DEVICE_ATTR(in12_label, S_IRUGO, show_voltage_label, NULL, 11);
1576static SENSOR_DEVICE_ATTR(in13_label, S_IRUGO, show_voltage_label, NULL, 12);
1577
1578static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL,
1579 ADT7462_ALARM2 | ADT7462_V0_ALARM);
1580static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL,
1581 ADT7462_ALARM2 | ADT7462_V7_ALARM);
1582static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL,
1583 ADT7462_ALARM2 | ADT7462_V2_ALARM);
1584static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL,
1585 ADT7462_ALARM2 | ADT7462_V6_ALARM);
1586static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL,
1587 ADT7462_ALARM2 | ADT7462_V5_ALARM);
1588static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL,
1589 ADT7462_ALARM2 | ADT7462_V4_ALARM);
1590static SENSOR_DEVICE_ATTR(in7_alarm, S_IRUGO, show_alarm, NULL,
1591 ADT7462_ALARM2 | ADT7462_V3_ALARM);
1592static SENSOR_DEVICE_ATTR(in8_alarm, S_IRUGO, show_alarm, NULL,
1593 ADT7462_ALARM2 | ADT7462_V1_ALARM);
1594static SENSOR_DEVICE_ATTR(in9_alarm, S_IRUGO, show_alarm, NULL,
1595 ADT7462_ALARM3 | ADT7462_V10_ALARM);
1596static SENSOR_DEVICE_ATTR(in10_alarm, S_IRUGO, show_alarm, NULL,
1597 ADT7462_ALARM3 | ADT7462_V9_ALARM);
1598static SENSOR_DEVICE_ATTR(in11_alarm, S_IRUGO, show_alarm, NULL,
1599 ADT7462_ALARM3 | ADT7462_V8_ALARM);
1600static SENSOR_DEVICE_ATTR(in12_alarm, S_IRUGO, show_alarm, NULL,
1601 ADT7462_ALARM3 | ADT7462_V11_ALARM);
1602static SENSOR_DEVICE_ATTR(in13_alarm, S_IRUGO, show_alarm, NULL,
1603 ADT7462_ALARM3 | ADT7462_V12_ALARM);
1604
1605static SENSOR_DEVICE_ATTR(fan1_min, S_IWUSR | S_IRUGO, show_fan_min,
1606 set_fan_min, 0);
1607static SENSOR_DEVICE_ATTR(fan2_min, S_IWUSR | S_IRUGO, show_fan_min,
1608 set_fan_min, 1);
1609static SENSOR_DEVICE_ATTR(fan3_min, S_IWUSR | S_IRUGO, show_fan_min,
1610 set_fan_min, 2);
1611static SENSOR_DEVICE_ATTR(fan4_min, S_IWUSR | S_IRUGO, show_fan_min,
1612 set_fan_min, 3);
1613static SENSOR_DEVICE_ATTR(fan5_min, S_IWUSR | S_IRUGO, show_fan_min,
1614 set_fan_min, 4);
1615static SENSOR_DEVICE_ATTR(fan6_min, S_IWUSR | S_IRUGO, show_fan_min,
1616 set_fan_min, 5);
1617static SENSOR_DEVICE_ATTR(fan7_min, S_IWUSR | S_IRUGO, show_fan_min,
1618 set_fan_min, 6);
1619static SENSOR_DEVICE_ATTR(fan8_min, S_IWUSR | S_IRUGO, show_fan_min,
1620 set_fan_min, 7);
1621
1622static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, show_fan, NULL, 0);
1623static SENSOR_DEVICE_ATTR(fan2_input, S_IRUGO, show_fan, NULL, 1);
1624static SENSOR_DEVICE_ATTR(fan3_input, S_IRUGO, show_fan, NULL, 2);
1625static SENSOR_DEVICE_ATTR(fan4_input, S_IRUGO, show_fan, NULL, 3);
1626static SENSOR_DEVICE_ATTR(fan5_input, S_IRUGO, show_fan, NULL, 4);
1627static SENSOR_DEVICE_ATTR(fan6_input, S_IRUGO, show_fan, NULL, 5);
1628static SENSOR_DEVICE_ATTR(fan7_input, S_IRUGO, show_fan, NULL, 6);
1629static SENSOR_DEVICE_ATTR(fan8_input, S_IRUGO, show_fan, NULL, 7);
1630
1631static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL,
1632 ADT7462_ALARM4 | ADT7462_F0_ALARM);
1633static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL,
1634 ADT7462_ALARM4 | ADT7462_F1_ALARM);
1635static SENSOR_DEVICE_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL,
1636 ADT7462_ALARM4 | ADT7462_F2_ALARM);
1637static SENSOR_DEVICE_ATTR(fan4_alarm, S_IRUGO, show_alarm, NULL,
1638 ADT7462_ALARM4 | ADT7462_F3_ALARM);
1639static SENSOR_DEVICE_ATTR(fan5_alarm, S_IRUGO, show_alarm, NULL,
1640 ADT7462_ALARM4 | ADT7462_F4_ALARM);
1641static SENSOR_DEVICE_ATTR(fan6_alarm, S_IRUGO, show_alarm, NULL,
1642 ADT7462_ALARM4 | ADT7462_F5_ALARM);
1643static SENSOR_DEVICE_ATTR(fan7_alarm, S_IRUGO, show_alarm, NULL,
1644 ADT7462_ALARM4 | ADT7462_F6_ALARM);
1645static SENSOR_DEVICE_ATTR(fan8_alarm, S_IRUGO, show_alarm, NULL,
1646 ADT7462_ALARM4 | ADT7462_F7_ALARM);
1647
1648static SENSOR_DEVICE_ATTR(force_pwm_max, S_IWUSR | S_IRUGO,
1649 show_force_pwm_max, set_force_pwm_max, 0);
1650
1651static SENSOR_DEVICE_ATTR(pwm1, S_IWUSR | S_IRUGO, show_pwm, set_pwm, 0);
1652static SENSOR_DEVICE_ATTR(pwm2, S_IWUSR | S_IRUGO, show_pwm, set_pwm, 1);
1653static SENSOR_DEVICE_ATTR(pwm3, S_IWUSR | S_IRUGO, show_pwm, set_pwm, 2);
1654static SENSOR_DEVICE_ATTR(pwm4, S_IWUSR | S_IRUGO, show_pwm, set_pwm, 3);
1655
1656static SENSOR_DEVICE_ATTR(pwm1_auto_point1_pwm, S_IWUSR | S_IRUGO,
1657 show_pwm_min, set_pwm_min, 0);
1658static SENSOR_DEVICE_ATTR(pwm2_auto_point1_pwm, S_IWUSR | S_IRUGO,
1659 show_pwm_min, set_pwm_min, 1);
1660static SENSOR_DEVICE_ATTR(pwm3_auto_point1_pwm, S_IWUSR | S_IRUGO,
1661 show_pwm_min, set_pwm_min, 2);
1662static SENSOR_DEVICE_ATTR(pwm4_auto_point1_pwm, S_IWUSR | S_IRUGO,
1663 show_pwm_min, set_pwm_min, 3);
1664
1665static SENSOR_DEVICE_ATTR(pwm1_auto_point2_pwm, S_IWUSR | S_IRUGO,
1666 show_pwm_max, set_pwm_max, 0);
1667static SENSOR_DEVICE_ATTR(pwm2_auto_point2_pwm, S_IWUSR | S_IRUGO,
1668 show_pwm_max, set_pwm_max, 1);
1669static SENSOR_DEVICE_ATTR(pwm3_auto_point2_pwm, S_IWUSR | S_IRUGO,
1670 show_pwm_max, set_pwm_max, 2);
1671static SENSOR_DEVICE_ATTR(pwm4_auto_point2_pwm, S_IWUSR | S_IRUGO,
1672 show_pwm_max, set_pwm_max, 3);
1673
1674static SENSOR_DEVICE_ATTR(temp1_auto_point1_hyst, S_IWUSR | S_IRUGO,
1675 show_pwm_hyst, set_pwm_hyst, 0);
1676static SENSOR_DEVICE_ATTR(temp2_auto_point1_hyst, S_IWUSR | S_IRUGO,
1677 show_pwm_hyst, set_pwm_hyst, 1);
1678static SENSOR_DEVICE_ATTR(temp3_auto_point1_hyst, S_IWUSR | S_IRUGO,
1679 show_pwm_hyst, set_pwm_hyst, 2);
1680static SENSOR_DEVICE_ATTR(temp4_auto_point1_hyst, S_IWUSR | S_IRUGO,
1681 show_pwm_hyst, set_pwm_hyst, 3);
1682
1683static SENSOR_DEVICE_ATTR(temp1_auto_point2_hyst, S_IWUSR | S_IRUGO,
1684 show_pwm_hyst, set_pwm_hyst, 0);
1685static SENSOR_DEVICE_ATTR(temp2_auto_point2_hyst, S_IWUSR | S_IRUGO,
1686 show_pwm_hyst, set_pwm_hyst, 1);
1687static SENSOR_DEVICE_ATTR(temp3_auto_point2_hyst, S_IWUSR | S_IRUGO,
1688 show_pwm_hyst, set_pwm_hyst, 2);
1689static SENSOR_DEVICE_ATTR(temp4_auto_point2_hyst, S_IWUSR | S_IRUGO,
1690 show_pwm_hyst, set_pwm_hyst, 3);
1691
1692static SENSOR_DEVICE_ATTR(temp1_auto_point1_temp, S_IWUSR | S_IRUGO,
1693 show_pwm_tmin, set_pwm_tmin, 0);
1694static SENSOR_DEVICE_ATTR(temp2_auto_point1_temp, S_IWUSR | S_IRUGO,
1695 show_pwm_tmin, set_pwm_tmin, 1);
1696static SENSOR_DEVICE_ATTR(temp3_auto_point1_temp, S_IWUSR | S_IRUGO,
1697 show_pwm_tmin, set_pwm_tmin, 2);
1698static SENSOR_DEVICE_ATTR(temp4_auto_point1_temp, S_IWUSR | S_IRUGO,
1699 show_pwm_tmin, set_pwm_tmin, 3);
1700
1701static SENSOR_DEVICE_ATTR(temp1_auto_point2_temp, S_IWUSR | S_IRUGO,
1702 show_pwm_tmax, set_pwm_tmax, 0);
1703static SENSOR_DEVICE_ATTR(temp2_auto_point2_temp, S_IWUSR | S_IRUGO,
1704 show_pwm_tmax, set_pwm_tmax, 1);
1705static SENSOR_DEVICE_ATTR(temp3_auto_point2_temp, S_IWUSR | S_IRUGO,
1706 show_pwm_tmax, set_pwm_tmax, 2);
1707static SENSOR_DEVICE_ATTR(temp4_auto_point2_temp, S_IWUSR | S_IRUGO,
1708 show_pwm_tmax, set_pwm_tmax, 3);
1709
1710static SENSOR_DEVICE_ATTR(pwm1_enable, S_IWUSR | S_IRUGO, show_pwm_auto,
1711 set_pwm_auto, 0);
1712static SENSOR_DEVICE_ATTR(pwm2_enable, S_IWUSR | S_IRUGO, show_pwm_auto,
1713 set_pwm_auto, 1);
1714static SENSOR_DEVICE_ATTR(pwm3_enable, S_IWUSR | S_IRUGO, show_pwm_auto,
1715 set_pwm_auto, 2);
1716static SENSOR_DEVICE_ATTR(pwm4_enable, S_IWUSR | S_IRUGO, show_pwm_auto,
1717 set_pwm_auto, 3);
1718
1719static SENSOR_DEVICE_ATTR(pwm1_auto_channels_temp, S_IWUSR | S_IRUGO,
1720 show_pwm_auto_temp, set_pwm_auto_temp, 0);
1721static SENSOR_DEVICE_ATTR(pwm2_auto_channels_temp, S_IWUSR | S_IRUGO,
1722 show_pwm_auto_temp, set_pwm_auto_temp, 1);
1723static SENSOR_DEVICE_ATTR(pwm3_auto_channels_temp, S_IWUSR | S_IRUGO,
1724 show_pwm_auto_temp, set_pwm_auto_temp, 2);
1725static SENSOR_DEVICE_ATTR(pwm4_auto_channels_temp, S_IWUSR | S_IRUGO,
1726 show_pwm_auto_temp, set_pwm_auto_temp, 3);
1727
Guenter Roeck724cc332012-01-14 13:16:53 -08001728static struct attribute *adt7462_attr[] = {
Darrick J. Wongc0b4e3a2008-11-12 13:27:03 -08001729 &sensor_dev_attr_temp1_max.dev_attr.attr,
1730 &sensor_dev_attr_temp2_max.dev_attr.attr,
1731 &sensor_dev_attr_temp3_max.dev_attr.attr,
1732 &sensor_dev_attr_temp4_max.dev_attr.attr,
1733
1734 &sensor_dev_attr_temp1_min.dev_attr.attr,
1735 &sensor_dev_attr_temp2_min.dev_attr.attr,
1736 &sensor_dev_attr_temp3_min.dev_attr.attr,
1737 &sensor_dev_attr_temp4_min.dev_attr.attr,
1738
1739 &sensor_dev_attr_temp1_input.dev_attr.attr,
1740 &sensor_dev_attr_temp2_input.dev_attr.attr,
1741 &sensor_dev_attr_temp3_input.dev_attr.attr,
1742 &sensor_dev_attr_temp4_input.dev_attr.attr,
1743
1744 &sensor_dev_attr_temp1_label.dev_attr.attr,
1745 &sensor_dev_attr_temp2_label.dev_attr.attr,
1746 &sensor_dev_attr_temp3_label.dev_attr.attr,
1747 &sensor_dev_attr_temp4_label.dev_attr.attr,
1748
1749 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
1750 &sensor_dev_attr_temp2_alarm.dev_attr.attr,
1751 &sensor_dev_attr_temp3_alarm.dev_attr.attr,
1752 &sensor_dev_attr_temp4_alarm.dev_attr.attr,
1753
1754 &sensor_dev_attr_in1_max.dev_attr.attr,
1755 &sensor_dev_attr_in2_max.dev_attr.attr,
1756 &sensor_dev_attr_in3_max.dev_attr.attr,
1757 &sensor_dev_attr_in4_max.dev_attr.attr,
1758 &sensor_dev_attr_in5_max.dev_attr.attr,
1759 &sensor_dev_attr_in6_max.dev_attr.attr,
1760 &sensor_dev_attr_in7_max.dev_attr.attr,
1761 &sensor_dev_attr_in8_max.dev_attr.attr,
1762 &sensor_dev_attr_in9_max.dev_attr.attr,
1763 &sensor_dev_attr_in10_max.dev_attr.attr,
1764 &sensor_dev_attr_in11_max.dev_attr.attr,
1765 &sensor_dev_attr_in12_max.dev_attr.attr,
1766 &sensor_dev_attr_in13_max.dev_attr.attr,
1767
1768 &sensor_dev_attr_in1_min.dev_attr.attr,
1769 &sensor_dev_attr_in2_min.dev_attr.attr,
1770 &sensor_dev_attr_in3_min.dev_attr.attr,
1771 &sensor_dev_attr_in4_min.dev_attr.attr,
1772 &sensor_dev_attr_in5_min.dev_attr.attr,
1773 &sensor_dev_attr_in6_min.dev_attr.attr,
1774 &sensor_dev_attr_in7_min.dev_attr.attr,
1775 &sensor_dev_attr_in8_min.dev_attr.attr,
1776 &sensor_dev_attr_in9_min.dev_attr.attr,
1777 &sensor_dev_attr_in10_min.dev_attr.attr,
1778 &sensor_dev_attr_in11_min.dev_attr.attr,
1779 &sensor_dev_attr_in12_min.dev_attr.attr,
1780 &sensor_dev_attr_in13_min.dev_attr.attr,
1781
1782 &sensor_dev_attr_in1_input.dev_attr.attr,
1783 &sensor_dev_attr_in2_input.dev_attr.attr,
1784 &sensor_dev_attr_in3_input.dev_attr.attr,
1785 &sensor_dev_attr_in4_input.dev_attr.attr,
1786 &sensor_dev_attr_in5_input.dev_attr.attr,
1787 &sensor_dev_attr_in6_input.dev_attr.attr,
1788 &sensor_dev_attr_in7_input.dev_attr.attr,
1789 &sensor_dev_attr_in8_input.dev_attr.attr,
1790 &sensor_dev_attr_in9_input.dev_attr.attr,
1791 &sensor_dev_attr_in10_input.dev_attr.attr,
1792 &sensor_dev_attr_in11_input.dev_attr.attr,
1793 &sensor_dev_attr_in12_input.dev_attr.attr,
1794 &sensor_dev_attr_in13_input.dev_attr.attr,
1795
1796 &sensor_dev_attr_in1_label.dev_attr.attr,
1797 &sensor_dev_attr_in2_label.dev_attr.attr,
1798 &sensor_dev_attr_in3_label.dev_attr.attr,
1799 &sensor_dev_attr_in4_label.dev_attr.attr,
1800 &sensor_dev_attr_in5_label.dev_attr.attr,
1801 &sensor_dev_attr_in6_label.dev_attr.attr,
1802 &sensor_dev_attr_in7_label.dev_attr.attr,
1803 &sensor_dev_attr_in8_label.dev_attr.attr,
1804 &sensor_dev_attr_in9_label.dev_attr.attr,
1805 &sensor_dev_attr_in10_label.dev_attr.attr,
1806 &sensor_dev_attr_in11_label.dev_attr.attr,
1807 &sensor_dev_attr_in12_label.dev_attr.attr,
1808 &sensor_dev_attr_in13_label.dev_attr.attr,
1809
1810 &sensor_dev_attr_in1_alarm.dev_attr.attr,
1811 &sensor_dev_attr_in2_alarm.dev_attr.attr,
1812 &sensor_dev_attr_in3_alarm.dev_attr.attr,
1813 &sensor_dev_attr_in4_alarm.dev_attr.attr,
1814 &sensor_dev_attr_in5_alarm.dev_attr.attr,
1815 &sensor_dev_attr_in6_alarm.dev_attr.attr,
1816 &sensor_dev_attr_in7_alarm.dev_attr.attr,
1817 &sensor_dev_attr_in8_alarm.dev_attr.attr,
1818 &sensor_dev_attr_in9_alarm.dev_attr.attr,
1819 &sensor_dev_attr_in10_alarm.dev_attr.attr,
1820 &sensor_dev_attr_in11_alarm.dev_attr.attr,
1821 &sensor_dev_attr_in12_alarm.dev_attr.attr,
1822 &sensor_dev_attr_in13_alarm.dev_attr.attr,
1823
1824 &sensor_dev_attr_fan1_min.dev_attr.attr,
1825 &sensor_dev_attr_fan2_min.dev_attr.attr,
1826 &sensor_dev_attr_fan3_min.dev_attr.attr,
1827 &sensor_dev_attr_fan4_min.dev_attr.attr,
1828 &sensor_dev_attr_fan5_min.dev_attr.attr,
1829 &sensor_dev_attr_fan6_min.dev_attr.attr,
1830 &sensor_dev_attr_fan7_min.dev_attr.attr,
1831 &sensor_dev_attr_fan8_min.dev_attr.attr,
1832
1833 &sensor_dev_attr_fan1_input.dev_attr.attr,
1834 &sensor_dev_attr_fan2_input.dev_attr.attr,
1835 &sensor_dev_attr_fan3_input.dev_attr.attr,
1836 &sensor_dev_attr_fan4_input.dev_attr.attr,
1837 &sensor_dev_attr_fan5_input.dev_attr.attr,
1838 &sensor_dev_attr_fan6_input.dev_attr.attr,
1839 &sensor_dev_attr_fan7_input.dev_attr.attr,
1840 &sensor_dev_attr_fan8_input.dev_attr.attr,
1841
1842 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
1843 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
1844 &sensor_dev_attr_fan3_alarm.dev_attr.attr,
1845 &sensor_dev_attr_fan4_alarm.dev_attr.attr,
1846 &sensor_dev_attr_fan5_alarm.dev_attr.attr,
1847 &sensor_dev_attr_fan6_alarm.dev_attr.attr,
1848 &sensor_dev_attr_fan7_alarm.dev_attr.attr,
1849 &sensor_dev_attr_fan8_alarm.dev_attr.attr,
1850
1851 &sensor_dev_attr_force_pwm_max.dev_attr.attr,
1852 &sensor_dev_attr_pwm1.dev_attr.attr,
1853 &sensor_dev_attr_pwm2.dev_attr.attr,
1854 &sensor_dev_attr_pwm3.dev_attr.attr,
1855 &sensor_dev_attr_pwm4.dev_attr.attr,
1856
1857 &sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr,
1858 &sensor_dev_attr_pwm2_auto_point1_pwm.dev_attr.attr,
1859 &sensor_dev_attr_pwm3_auto_point1_pwm.dev_attr.attr,
1860 &sensor_dev_attr_pwm4_auto_point1_pwm.dev_attr.attr,
1861
1862 &sensor_dev_attr_pwm1_auto_point2_pwm.dev_attr.attr,
1863 &sensor_dev_attr_pwm2_auto_point2_pwm.dev_attr.attr,
1864 &sensor_dev_attr_pwm3_auto_point2_pwm.dev_attr.attr,
1865 &sensor_dev_attr_pwm4_auto_point2_pwm.dev_attr.attr,
1866
1867 &sensor_dev_attr_temp1_auto_point1_hyst.dev_attr.attr,
1868 &sensor_dev_attr_temp2_auto_point1_hyst.dev_attr.attr,
1869 &sensor_dev_attr_temp3_auto_point1_hyst.dev_attr.attr,
1870 &sensor_dev_attr_temp4_auto_point1_hyst.dev_attr.attr,
1871
1872 &sensor_dev_attr_temp1_auto_point2_hyst.dev_attr.attr,
1873 &sensor_dev_attr_temp2_auto_point2_hyst.dev_attr.attr,
1874 &sensor_dev_attr_temp3_auto_point2_hyst.dev_attr.attr,
1875 &sensor_dev_attr_temp4_auto_point2_hyst.dev_attr.attr,
1876
1877 &sensor_dev_attr_temp1_auto_point1_temp.dev_attr.attr,
1878 &sensor_dev_attr_temp2_auto_point1_temp.dev_attr.attr,
1879 &sensor_dev_attr_temp3_auto_point1_temp.dev_attr.attr,
1880 &sensor_dev_attr_temp4_auto_point1_temp.dev_attr.attr,
1881
1882 &sensor_dev_attr_temp1_auto_point2_temp.dev_attr.attr,
1883 &sensor_dev_attr_temp2_auto_point2_temp.dev_attr.attr,
1884 &sensor_dev_attr_temp3_auto_point2_temp.dev_attr.attr,
1885 &sensor_dev_attr_temp4_auto_point2_temp.dev_attr.attr,
1886
1887 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
1888 &sensor_dev_attr_pwm2_enable.dev_attr.attr,
1889 &sensor_dev_attr_pwm3_enable.dev_attr.attr,
1890 &sensor_dev_attr_pwm4_enable.dev_attr.attr,
1891
1892 &sensor_dev_attr_pwm1_auto_channels_temp.dev_attr.attr,
1893 &sensor_dev_attr_pwm2_auto_channels_temp.dev_attr.attr,
1894 &sensor_dev_attr_pwm3_auto_channels_temp.dev_attr.attr,
1895 &sensor_dev_attr_pwm4_auto_channels_temp.dev_attr.attr,
1896 NULL
1897};
1898
1899/* Return 0 if detection is successful, -ENODEV otherwise */
Jean Delvare310ec792009-12-14 21:17:23 +01001900static int adt7462_detect(struct i2c_client *client,
Darrick J. Wongc0b4e3a2008-11-12 13:27:03 -08001901 struct i2c_board_info *info)
1902{
1903 struct i2c_adapter *adapter = client->adapter;
Jean Delvare52df6442009-12-09 20:35:57 +01001904 int vendor, device, revision;
Darrick J. Wongc0b4e3a2008-11-12 13:27:03 -08001905
1906 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
1907 return -ENODEV;
1908
Jean Delvare52df6442009-12-09 20:35:57 +01001909 vendor = i2c_smbus_read_byte_data(client, ADT7462_REG_VENDOR);
1910 if (vendor != ADT7462_VENDOR)
1911 return -ENODEV;
Darrick J. Wongc0b4e3a2008-11-12 13:27:03 -08001912
Jean Delvare52df6442009-12-09 20:35:57 +01001913 device = i2c_smbus_read_byte_data(client, ADT7462_REG_DEVICE);
1914 if (device != ADT7462_DEVICE)
1915 return -ENODEV;
Darrick J. Wongc0b4e3a2008-11-12 13:27:03 -08001916
Jean Delvare52df6442009-12-09 20:35:57 +01001917 revision = i2c_smbus_read_byte_data(client, ADT7462_REG_REVISION);
1918 if (revision != ADT7462_REVISION)
1919 return -ENODEV;
Darrick J. Wongc0b4e3a2008-11-12 13:27:03 -08001920
1921 strlcpy(info->type, "adt7462", I2C_NAME_SIZE);
1922
1923 return 0;
1924}
1925
1926static int adt7462_probe(struct i2c_client *client,
1927 const struct i2c_device_id *id)
1928{
1929 struct adt7462_data *data;
1930 int err;
1931
Guenter Roeck08808302012-06-02 09:58:01 -07001932 data = devm_kzalloc(&client->dev, sizeof(struct adt7462_data),
1933 GFP_KERNEL);
1934 if (!data)
1935 return -ENOMEM;
Darrick J. Wongc0b4e3a2008-11-12 13:27:03 -08001936
1937 i2c_set_clientdata(client, data);
1938 mutex_init(&data->lock);
1939
1940 dev_info(&client->dev, "%s chip found\n", client->name);
1941
1942 /* Register sysfs hooks */
1943 data->attrs.attrs = adt7462_attr;
1944 err = sysfs_create_group(&client->dev.kobj, &data->attrs);
1945 if (err)
Guenter Roeck08808302012-06-02 09:58:01 -07001946 return err;
Darrick J. Wongc0b4e3a2008-11-12 13:27:03 -08001947
1948 data->hwmon_dev = hwmon_device_register(&client->dev);
1949 if (IS_ERR(data->hwmon_dev)) {
1950 err = PTR_ERR(data->hwmon_dev);
1951 goto exit_remove;
1952 }
1953
1954 return 0;
1955
1956exit_remove:
1957 sysfs_remove_group(&client->dev.kobj, &data->attrs);
Darrick J. Wongc0b4e3a2008-11-12 13:27:03 -08001958 return err;
1959}
1960
1961static int adt7462_remove(struct i2c_client *client)
1962{
1963 struct adt7462_data *data = i2c_get_clientdata(client);
1964
1965 hwmon_device_unregister(data->hwmon_dev);
1966 sysfs_remove_group(&client->dev.kobj, &data->attrs);
Darrick J. Wongc0b4e3a2008-11-12 13:27:03 -08001967 return 0;
1968}
1969
Axel Linf0967ee2012-01-20 15:38:18 +08001970module_i2c_driver(adt7462_driver);
Darrick J. Wongc0b4e3a2008-11-12 13:27:03 -08001971
Darrick J. Wong5407e0512013-08-26 15:42:27 -07001972MODULE_AUTHOR("Darrick J. Wong <darrick.wong@oracle.com>");
Darrick J. Wongc0b4e3a2008-11-12 13:27:03 -08001973MODULE_DESCRIPTION("ADT7462 driver");
1974MODULE_LICENSE("GPL");