blob: 520773b16544b46207694ee1536ae4f10e345d53 [file] [log] [blame]
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001/*
2 * adt7475 - Thermal sensor driver for the ADT7475 chip and derivatives
3 * Copyright (C) 2007-2008, Advanced Micro Devices, Inc.
4 * Copyright (C) 2008 Jordan Crouse <jordan@cosmicpenguin.net>
5 * Copyright (C) 2008 Hans de Goede <hdegoede@redhat.com>
6
7 * Derived from the lm83 driver by Jean Delvare
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
12 */
13
14#include <linux/module.h>
15#include <linux/init.h>
16#include <linux/slab.h>
17#include <linux/i2c.h>
18#include <linux/hwmon.h>
19#include <linux/hwmon-sysfs.h>
20#include <linux/err.h>
21
22/* Indexes for the sysfs hooks */
23
24#define INPUT 0
25#define MIN 1
26#define MAX 2
27#define CONTROL 3
28#define OFFSET 3
29#define AUTOMIN 4
30#define THERM 5
31#define HYSTERSIS 6
32
33/* These are unique identifiers for the sysfs functions - unlike the
34 numbers above, these are not also indexes into an array
35*/
36
37#define ALARM 9
38#define FAULT 10
39
40/* 7475 Common Registers */
41
Jean Delvarecffb9dd2009-12-09 20:36:03 +010042#define REG_VOLTAGE_BASE 0x20
Jordan Crouse1c301fc2009-01-15 22:27:47 +010043#define REG_TEMP_BASE 0x25
44#define REG_TACH_BASE 0x28
45#define REG_PWM_BASE 0x30
46#define REG_PWM_MAX_BASE 0x38
47
48#define REG_DEVID 0x3D
49#define REG_VENDID 0x3E
Jean Delvared656b6f2009-12-09 20:36:04 +010050#define REG_DEVID2 0x3F
Jordan Crouse1c301fc2009-01-15 22:27:47 +010051
52#define REG_STATUS1 0x41
53#define REG_STATUS2 0x42
54
Jean Delvarecffb9dd2009-12-09 20:36:03 +010055#define REG_VOLTAGE_MIN_BASE 0x44
56#define REG_VOLTAGE_MAX_BASE 0x45
Jordan Crouse1c301fc2009-01-15 22:27:47 +010057
58#define REG_TEMP_MIN_BASE 0x4E
59#define REG_TEMP_MAX_BASE 0x4F
60
61#define REG_TACH_MIN_BASE 0x54
62
63#define REG_PWM_CONFIG_BASE 0x5C
64
65#define REG_TEMP_TRANGE_BASE 0x5F
66
67#define REG_PWM_MIN_BASE 0x64
68
69#define REG_TEMP_TMIN_BASE 0x67
70#define REG_TEMP_THERM_BASE 0x6A
71
72#define REG_REMOTE1_HYSTERSIS 0x6D
73#define REG_REMOTE2_HYSTERSIS 0x6E
74
75#define REG_TEMP_OFFSET_BASE 0x70
76
77#define REG_EXTEND1 0x76
78#define REG_EXTEND2 0x77
79#define REG_CONFIG5 0x7C
Jean Delvaref99318b2009-12-09 20:36:03 +010080#define REG_CONFIG4 0x7D
81
82#define CONFIG4_MAXDUTY 0x08
Jordan Crouse1c301fc2009-01-15 22:27:47 +010083
84#define CONFIG5_TWOSCOMP 0x01
85#define CONFIG5_TEMPOFFSET 0x02
86
87/* ADT7475 Settings */
88
Jean Delvarecffb9dd2009-12-09 20:36:03 +010089#define ADT7475_VOLTAGE_COUNT 5 /* Not counting Vtt */
Jordan Crouse1c301fc2009-01-15 22:27:47 +010090#define ADT7475_TEMP_COUNT 3
91#define ADT7475_TACH_COUNT 4
92#define ADT7475_PWM_COUNT 3
93
94/* Macro to read the registers */
95
96#define adt7475_read(reg) i2c_smbus_read_byte_data(client, (reg))
97
98/* Macros to easily index the registers */
99
100#define TACH_REG(idx) (REG_TACH_BASE + ((idx) * 2))
101#define TACH_MIN_REG(idx) (REG_TACH_MIN_BASE + ((idx) * 2))
102
103#define PWM_REG(idx) (REG_PWM_BASE + (idx))
104#define PWM_MAX_REG(idx) (REG_PWM_MAX_BASE + (idx))
105#define PWM_MIN_REG(idx) (REG_PWM_MIN_BASE + (idx))
106#define PWM_CONFIG_REG(idx) (REG_PWM_CONFIG_BASE + (idx))
107
108#define VOLTAGE_REG(idx) (REG_VOLTAGE_BASE + (idx))
109#define VOLTAGE_MIN_REG(idx) (REG_VOLTAGE_MIN_BASE + ((idx) * 2))
110#define VOLTAGE_MAX_REG(idx) (REG_VOLTAGE_MAX_BASE + ((idx) * 2))
111
112#define TEMP_REG(idx) (REG_TEMP_BASE + (idx))
113#define TEMP_MIN_REG(idx) (REG_TEMP_MIN_BASE + ((idx) * 2))
114#define TEMP_MAX_REG(idx) (REG_TEMP_MAX_BASE + ((idx) * 2))
115#define TEMP_TMIN_REG(idx) (REG_TEMP_TMIN_BASE + (idx))
116#define TEMP_THERM_REG(idx) (REG_TEMP_THERM_BASE + (idx))
117#define TEMP_OFFSET_REG(idx) (REG_TEMP_OFFSET_BASE + (idx))
118#define TEMP_TRANGE_REG(idx) (REG_TEMP_TRANGE_BASE + (idx))
119
Jean Delvareb180d052009-12-09 20:36:02 +0100120static unsigned short normal_i2c[] = { 0x2c, 0x2d, 0x2e, I2C_CLIENT_END };
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100121
Jean Delvareb180d052009-12-09 20:36:02 +0100122I2C_CLIENT_INSMOD_2(adt7473, adt7475);
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100123
124static const struct i2c_device_id adt7475_id[] = {
Jean Delvareb180d052009-12-09 20:36:02 +0100125 { "adt7473", adt7473 },
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100126 { "adt7475", adt7475 },
127 { }
128};
129MODULE_DEVICE_TABLE(i2c, adt7475_id);
130
131struct adt7475_data {
132 struct device *hwmon_dev;
133 struct mutex lock;
134
135 unsigned long measure_updated;
136 unsigned long limits_updated;
137 char valid;
138
Jean Delvaref99318b2009-12-09 20:36:03 +0100139 u8 config4;
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100140 u8 config5;
Jean Delvarecffb9dd2009-12-09 20:36:03 +0100141 u8 has_voltage;
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100142 u16 alarms;
Jean Delvarecffb9dd2009-12-09 20:36:03 +0100143 u16 voltage[3][6];
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100144 u16 temp[7][3];
145 u16 tach[2][4];
146 u8 pwm[4][3];
147 u8 range[3];
148 u8 pwmctl[3];
149 u8 pwmchan[3];
150};
151
152static struct i2c_driver adt7475_driver;
153static struct adt7475_data *adt7475_update_device(struct device *dev);
154static void adt7475_read_hystersis(struct i2c_client *client);
155static void adt7475_read_pwm(struct i2c_client *client, int index);
156
157/* Given a temp value, convert it to register value */
158
159static inline u16 temp2reg(struct adt7475_data *data, long val)
160{
161 u16 ret;
162
163 if (!(data->config5 & CONFIG5_TWOSCOMP)) {
164 val = SENSORS_LIMIT(val, -64000, 191000);
165 ret = (val + 64500) / 1000;
166 } else {
167 val = SENSORS_LIMIT(val, -128000, 127000);
168 if (val < -500)
169 ret = (256500 + val) / 1000;
170 else
171 ret = (val + 500) / 1000;
172 }
173
174 return ret << 2;
175}
176
177/* Given a register value, convert it to a real temp value */
178
179static inline int reg2temp(struct adt7475_data *data, u16 reg)
180{
181 if (data->config5 & CONFIG5_TWOSCOMP) {
182 if (reg >= 512)
183 return (reg - 1024) * 250;
184 else
185 return reg * 250;
186 } else
187 return (reg - 256) * 250;
188}
189
190static inline int tach2rpm(u16 tach)
191{
192 if (tach == 0 || tach == 0xFFFF)
193 return 0;
194
195 return (90000 * 60) / tach;
196}
197
198static inline u16 rpm2tach(unsigned long rpm)
199{
200 if (rpm == 0)
201 return 0;
202
203 return SENSORS_LIMIT((90000 * 60) / rpm, 1, 0xFFFF);
204}
205
Jean Delvarecffb9dd2009-12-09 20:36:03 +0100206/* Scaling factors for voltage inputs, taken from the ADT7490 datasheet */
207static const int adt7473_in_scaling[ADT7475_VOLTAGE_COUNT + 1][2] = {
208 { 45, 94 }, /* +2.5V */
209 { 175, 525 }, /* Vccp */
210 { 68, 71 }, /* Vcc */
211 { 93, 47 }, /* +5V */
212 { 120, 20 }, /* +12V */
213 { 45, 45 }, /* Vtt */
214};
215
216static inline int reg2volt(int channel, u16 reg)
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100217{
Jean Delvarecffb9dd2009-12-09 20:36:03 +0100218 const int *r = adt7473_in_scaling[channel];
219
220 return DIV_ROUND_CLOSEST(reg * (r[0] + r[1]) * 2250, r[1] * 1024);
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100221}
222
Jean Delvarecffb9dd2009-12-09 20:36:03 +0100223static inline u16 volt2reg(int channel, long volt)
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100224{
Jean Delvarecffb9dd2009-12-09 20:36:03 +0100225 const int *r = adt7473_in_scaling[channel];
226 long reg;
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100227
Jean Delvarecffb9dd2009-12-09 20:36:03 +0100228 reg = (volt * r[1] * 1024) / ((r[0] + r[1]) * 2250);
229 return SENSORS_LIMIT(reg, 0, 1023) & (0xff << 2);
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100230}
231
232static u16 adt7475_read_word(struct i2c_client *client, int reg)
233{
234 u16 val;
235
236 val = i2c_smbus_read_byte_data(client, reg);
237 val |= (i2c_smbus_read_byte_data(client, reg + 1) << 8);
238
239 return val;
240}
241
242static void adt7475_write_word(struct i2c_client *client, int reg, u16 val)
243{
244 i2c_smbus_write_byte_data(client, reg + 1, val >> 8);
245 i2c_smbus_write_byte_data(client, reg, val & 0xFF);
246}
247
248/* Find the nearest value in a table - used for pwm frequency and
249 auto temp range */
250static int find_nearest(long val, const int *array, int size)
251{
252 int i;
253
254 if (val < array[0])
255 return 0;
256
257 if (val > array[size - 1])
258 return size - 1;
259
260 for (i = 0; i < size - 1; i++) {
261 int a, b;
262
263 if (val > array[i + 1])
264 continue;
265
266 a = val - array[i];
267 b = array[i + 1] - val;
268
269 return (a <= b) ? i : i + 1;
270 }
271
272 return 0;
273}
274
275static ssize_t show_voltage(struct device *dev, struct device_attribute *attr,
276 char *buf)
277{
278 struct adt7475_data *data = adt7475_update_device(dev);
279 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
280 unsigned short val;
281
282 switch (sattr->nr) {
283 case ALARM:
284 return sprintf(buf, "%d\n",
Jean Delvarecffb9dd2009-12-09 20:36:03 +0100285 (data->alarms >> sattr->index) & 1);
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100286 default:
287 val = data->voltage[sattr->nr][sattr->index];
Jean Delvarecffb9dd2009-12-09 20:36:03 +0100288 return sprintf(buf, "%d\n", reg2volt(sattr->index, val));
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100289 }
290}
291
292static ssize_t set_voltage(struct device *dev, struct device_attribute *attr,
293 const char *buf, size_t count)
294{
295
296 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
297 struct i2c_client *client = to_i2c_client(dev);
298 struct adt7475_data *data = i2c_get_clientdata(client);
299 unsigned char reg;
300 long val;
301
302 if (strict_strtol(buf, 10, &val))
303 return -EINVAL;
304
305 mutex_lock(&data->lock);
306
Jean Delvarecffb9dd2009-12-09 20:36:03 +0100307 data->voltage[sattr->nr][sattr->index] = volt2reg(sattr->index, val);
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100308
309 if (sattr->nr == MIN)
310 reg = VOLTAGE_MIN_REG(sattr->index);
311 else
312 reg = VOLTAGE_MAX_REG(sattr->index);
313
314 i2c_smbus_write_byte_data(client, reg,
315 data->voltage[sattr->nr][sattr->index] >> 2);
316 mutex_unlock(&data->lock);
317
318 return count;
319}
320
321static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
322 char *buf)
323{
324 struct adt7475_data *data = adt7475_update_device(dev);
325 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
326 int out;
327
328 switch (sattr->nr) {
329 case HYSTERSIS:
330 mutex_lock(&data->lock);
331 out = data->temp[sattr->nr][sattr->index];
332 if (sattr->index != 1)
333 out = (out >> 4) & 0xF;
334 else
335 out = (out & 0xF);
336 /* Show the value as an absolute number tied to
337 * THERM */
338 out = reg2temp(data, data->temp[THERM][sattr->index]) -
339 out * 1000;
340 mutex_unlock(&data->lock);
341 break;
342
343 case OFFSET:
344 /* Offset is always 2's complement, regardless of the
345 * setting in CONFIG5 */
346 mutex_lock(&data->lock);
347 out = (s8)data->temp[sattr->nr][sattr->index];
348 if (data->config5 & CONFIG5_TEMPOFFSET)
349 out *= 1000;
350 else
351 out *= 500;
352 mutex_unlock(&data->lock);
353 break;
354
355 case ALARM:
356 out = (data->alarms >> (sattr->index + 4)) & 1;
357 break;
358
359 case FAULT:
360 /* Note - only for remote1 and remote2 */
Jean Delvarecf312e02009-11-16 12:45:39 +0100361 out = !!(data->alarms & (sattr->index ? 0x8000 : 0x4000));
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100362 break;
363
364 default:
365 /* All other temp values are in the configured format */
366 out = reg2temp(data, data->temp[sattr->nr][sattr->index]);
367 }
368
369 return sprintf(buf, "%d\n", out);
370}
371
372static ssize_t set_temp(struct device *dev, struct device_attribute *attr,
373 const char *buf, size_t count)
374{
375 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
376 struct i2c_client *client = to_i2c_client(dev);
377 struct adt7475_data *data = i2c_get_clientdata(client);
378 unsigned char reg = 0;
379 u8 out;
380 int temp;
381 long val;
382
383 if (strict_strtol(buf, 10, &val))
384 return -EINVAL;
385
386 mutex_lock(&data->lock);
387
388 /* We need the config register in all cases for temp <-> reg conv. */
389 data->config5 = adt7475_read(REG_CONFIG5);
390
391 switch (sattr->nr) {
392 case OFFSET:
393 if (data->config5 & CONFIG5_TEMPOFFSET) {
394 val = SENSORS_LIMIT(val, -63000, 127000);
395 out = data->temp[OFFSET][sattr->index] = val / 1000;
396 } else {
397 val = SENSORS_LIMIT(val, -63000, 64000);
398 out = data->temp[OFFSET][sattr->index] = val / 500;
399 }
400 break;
401
402 case HYSTERSIS:
403 /* The value will be given as an absolute value, turn it
404 into an offset based on THERM */
405
406 /* Read fresh THERM and HYSTERSIS values from the chip */
407 data->temp[THERM][sattr->index] =
408 adt7475_read(TEMP_THERM_REG(sattr->index)) << 2;
409 adt7475_read_hystersis(client);
410
411 temp = reg2temp(data, data->temp[THERM][sattr->index]);
412 val = SENSORS_LIMIT(val, temp - 15000, temp);
413 val = (temp - val) / 1000;
414
415 if (sattr->index != 1) {
416 data->temp[HYSTERSIS][sattr->index] &= 0xF0;
417 data->temp[HYSTERSIS][sattr->index] |= (val & 0xF) << 4;
418 } else {
419 data->temp[HYSTERSIS][sattr->index] &= 0x0F;
420 data->temp[HYSTERSIS][sattr->index] |= (val & 0xF);
421 }
422
423 out = data->temp[HYSTERSIS][sattr->index];
424 break;
425
426 default:
427 data->temp[sattr->nr][sattr->index] = temp2reg(data, val);
428
429 /* We maintain an extra 2 digits of precision for simplicity
430 * - shift those back off before writing the value */
431 out = (u8) (data->temp[sattr->nr][sattr->index] >> 2);
432 }
433
434 switch (sattr->nr) {
435 case MIN:
436 reg = TEMP_MIN_REG(sattr->index);
437 break;
438 case MAX:
439 reg = TEMP_MAX_REG(sattr->index);
440 break;
441 case OFFSET:
442 reg = TEMP_OFFSET_REG(sattr->index);
443 break;
444 case AUTOMIN:
445 reg = TEMP_TMIN_REG(sattr->index);
446 break;
447 case THERM:
448 reg = TEMP_THERM_REG(sattr->index);
449 break;
450 case HYSTERSIS:
451 if (sattr->index != 2)
452 reg = REG_REMOTE1_HYSTERSIS;
453 else
454 reg = REG_REMOTE2_HYSTERSIS;
455
456 break;
457 }
458
459 i2c_smbus_write_byte_data(client, reg, out);
460
461 mutex_unlock(&data->lock);
462 return count;
463}
464
465/* Table of autorange values - the user will write the value in millidegrees,
466 and we'll convert it */
467static const int autorange_table[] = {
468 2000, 2500, 3330, 4000, 5000, 6670, 8000,
469 10000, 13330, 16000, 20000, 26670, 32000, 40000,
470 53330, 80000
471};
472
473static ssize_t show_point2(struct device *dev, struct device_attribute *attr,
474 char *buf)
475{
476 struct adt7475_data *data = adt7475_update_device(dev);
477 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
478 int out, val;
479
480 mutex_lock(&data->lock);
481 out = (data->range[sattr->index] >> 4) & 0x0F;
482 val = reg2temp(data, data->temp[AUTOMIN][sattr->index]);
483 mutex_unlock(&data->lock);
484
485 return sprintf(buf, "%d\n", val + autorange_table[out]);
486}
487
488static ssize_t set_point2(struct device *dev, struct device_attribute *attr,
489 const char *buf, size_t count)
490{
491 struct i2c_client *client = to_i2c_client(dev);
492 struct adt7475_data *data = i2c_get_clientdata(client);
493 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
494 int temp;
495 long val;
496
497 if (strict_strtol(buf, 10, &val))
498 return -EINVAL;
499
500 mutex_lock(&data->lock);
501
502 /* Get a fresh copy of the needed registers */
503 data->config5 = adt7475_read(REG_CONFIG5);
504 data->temp[AUTOMIN][sattr->index] =
505 adt7475_read(TEMP_TMIN_REG(sattr->index)) << 2;
506 data->range[sattr->index] =
507 adt7475_read(TEMP_TRANGE_REG(sattr->index));
508
509 /* The user will write an absolute value, so subtract the start point
510 to figure the range */
511 temp = reg2temp(data, data->temp[AUTOMIN][sattr->index]);
512 val = SENSORS_LIMIT(val, temp + autorange_table[0],
513 temp + autorange_table[ARRAY_SIZE(autorange_table) - 1]);
514 val -= temp;
515
516 /* Find the nearest table entry to what the user wrote */
517 val = find_nearest(val, autorange_table, ARRAY_SIZE(autorange_table));
518
519 data->range[sattr->index] &= ~0xF0;
520 data->range[sattr->index] |= val << 4;
521
522 i2c_smbus_write_byte_data(client, TEMP_TRANGE_REG(sattr->index),
523 data->range[sattr->index]);
524
525 mutex_unlock(&data->lock);
526 return count;
527}
528
529static ssize_t show_tach(struct device *dev, struct device_attribute *attr,
530 char *buf)
531{
532 struct adt7475_data *data = adt7475_update_device(dev);
533 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
534 int out;
535
536 if (sattr->nr == ALARM)
537 out = (data->alarms >> (sattr->index + 10)) & 1;
538 else
539 out = tach2rpm(data->tach[sattr->nr][sattr->index]);
540
541 return sprintf(buf, "%d\n", out);
542}
543
544static ssize_t set_tach(struct device *dev, struct device_attribute *attr,
545 const char *buf, size_t count)
546{
547
548 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
549 struct i2c_client *client = to_i2c_client(dev);
550 struct adt7475_data *data = i2c_get_clientdata(client);
551 unsigned long val;
552
553 if (strict_strtoul(buf, 10, &val))
554 return -EINVAL;
555
556 mutex_lock(&data->lock);
557
558 data->tach[MIN][sattr->index] = rpm2tach(val);
559
560 adt7475_write_word(client, TACH_MIN_REG(sattr->index),
561 data->tach[MIN][sattr->index]);
562
563 mutex_unlock(&data->lock);
564 return count;
565}
566
567static ssize_t show_pwm(struct device *dev, struct device_attribute *attr,
568 char *buf)
569{
570 struct adt7475_data *data = adt7475_update_device(dev);
571 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
572
573 return sprintf(buf, "%d\n", data->pwm[sattr->nr][sattr->index]);
574}
575
576static ssize_t show_pwmchan(struct device *dev, struct device_attribute *attr,
577 char *buf)
578{
579 struct adt7475_data *data = adt7475_update_device(dev);
580 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
581
582 return sprintf(buf, "%d\n", data->pwmchan[sattr->index]);
583}
584
585static ssize_t show_pwmctrl(struct device *dev, struct device_attribute *attr,
586 char *buf)
587{
588 struct adt7475_data *data = adt7475_update_device(dev);
589 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
590
591 return sprintf(buf, "%d\n", data->pwmctl[sattr->index]);
592}
593
594static ssize_t set_pwm(struct device *dev, struct device_attribute *attr,
595 const char *buf, size_t count)
596{
597
598 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
599 struct i2c_client *client = to_i2c_client(dev);
600 struct adt7475_data *data = i2c_get_clientdata(client);
601 unsigned char reg = 0;
602 long val;
603
604 if (strict_strtol(buf, 10, &val))
605 return -EINVAL;
606
607 mutex_lock(&data->lock);
608
609 switch (sattr->nr) {
610 case INPUT:
611 /* Get a fresh value for CONTROL */
612 data->pwm[CONTROL][sattr->index] =
613 adt7475_read(PWM_CONFIG_REG(sattr->index));
614
615 /* If we are not in manual mode, then we shouldn't allow
616 * the user to set the pwm speed */
617 if (((data->pwm[CONTROL][sattr->index] >> 5) & 7) != 7) {
618 mutex_unlock(&data->lock);
619 return count;
620 }
621
622 reg = PWM_REG(sattr->index);
623 break;
624
625 case MIN:
626 reg = PWM_MIN_REG(sattr->index);
627 break;
628
629 case MAX:
630 reg = PWM_MAX_REG(sattr->index);
631 break;
632 }
633
634 data->pwm[sattr->nr][sattr->index] = SENSORS_LIMIT(val, 0, 0xFF);
635 i2c_smbus_write_byte_data(client, reg,
636 data->pwm[sattr->nr][sattr->index]);
637
638 mutex_unlock(&data->lock);
639
640 return count;
641}
642
643/* Called by set_pwmctrl and set_pwmchan */
644
645static int hw_set_pwm(struct i2c_client *client, int index,
646 unsigned int pwmctl, unsigned int pwmchan)
647{
648 struct adt7475_data *data = i2c_get_clientdata(client);
649 long val = 0;
650
651 switch (pwmctl) {
652 case 0:
653 val = 0x03; /* Run at full speed */
654 break;
655 case 1:
656 val = 0x07; /* Manual mode */
657 break;
658 case 2:
659 switch (pwmchan) {
660 case 1:
661 /* Remote1 controls PWM */
662 val = 0x00;
663 break;
664 case 2:
665 /* local controls PWM */
666 val = 0x01;
667 break;
668 case 4:
669 /* remote2 controls PWM */
670 val = 0x02;
671 break;
672 case 6:
673 /* local/remote2 control PWM */
674 val = 0x05;
675 break;
676 case 7:
677 /* All three control PWM */
678 val = 0x06;
679 break;
680 default:
681 return -EINVAL;
682 }
683 break;
684 default:
685 return -EINVAL;
686 }
687
688 data->pwmctl[index] = pwmctl;
689 data->pwmchan[index] = pwmchan;
690
691 data->pwm[CONTROL][index] &= ~0xE0;
692 data->pwm[CONTROL][index] |= (val & 7) << 5;
693
694 i2c_smbus_write_byte_data(client, PWM_CONFIG_REG(index),
695 data->pwm[CONTROL][index]);
696
697 return 0;
698}
699
700static ssize_t set_pwmchan(struct device *dev, struct device_attribute *attr,
701 const char *buf, size_t count)
702{
703 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
704 struct i2c_client *client = to_i2c_client(dev);
705 struct adt7475_data *data = i2c_get_clientdata(client);
706 int r;
707 long val;
708
709 if (strict_strtol(buf, 10, &val))
710 return -EINVAL;
711
712 mutex_lock(&data->lock);
713 /* Read Modify Write PWM values */
714 adt7475_read_pwm(client, sattr->index);
715 r = hw_set_pwm(client, sattr->index, data->pwmctl[sattr->index], val);
716 if (r)
717 count = r;
718 mutex_unlock(&data->lock);
719
720 return count;
721}
722
723static ssize_t set_pwmctrl(struct device *dev, struct device_attribute *attr,
724 const char *buf, size_t count)
725{
726 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
727 struct i2c_client *client = to_i2c_client(dev);
728 struct adt7475_data *data = i2c_get_clientdata(client);
729 int r;
730 long val;
731
732 if (strict_strtol(buf, 10, &val))
733 return -EINVAL;
734
735 mutex_lock(&data->lock);
736 /* Read Modify Write PWM values */
737 adt7475_read_pwm(client, sattr->index);
738 r = hw_set_pwm(client, sattr->index, val, data->pwmchan[sattr->index]);
739 if (r)
740 count = r;
741 mutex_unlock(&data->lock);
742
743 return count;
744}
745
746/* List of frequencies for the PWM */
747static const int pwmfreq_table[] = {
748 11, 14, 22, 29, 35, 44, 58, 88
749};
750
751static ssize_t show_pwmfreq(struct device *dev, struct device_attribute *attr,
752 char *buf)
753{
754 struct adt7475_data *data = adt7475_update_device(dev);
755 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
756
757 return sprintf(buf, "%d\n",
758 pwmfreq_table[data->range[sattr->index] & 7]);
759}
760
761static ssize_t set_pwmfreq(struct device *dev, struct device_attribute *attr,
762 const char *buf, size_t count)
763{
764 struct sensor_device_attribute_2 *sattr = to_sensor_dev_attr_2(attr);
765 struct i2c_client *client = to_i2c_client(dev);
766 struct adt7475_data *data = i2c_get_clientdata(client);
767 int out;
768 long val;
769
770 if (strict_strtol(buf, 10, &val))
771 return -EINVAL;
772
773 out = find_nearest(val, pwmfreq_table, ARRAY_SIZE(pwmfreq_table));
774
775 mutex_lock(&data->lock);
776
777 data->range[sattr->index] =
778 adt7475_read(TEMP_TRANGE_REG(sattr->index));
779 data->range[sattr->index] &= ~7;
780 data->range[sattr->index] |= out;
781
782 i2c_smbus_write_byte_data(client, TEMP_TRANGE_REG(sattr->index),
783 data->range[sattr->index]);
784
785 mutex_unlock(&data->lock);
786 return count;
787}
788
Jean Delvaref99318b2009-12-09 20:36:03 +0100789static ssize_t show_pwm_at_crit(struct device *dev,
790 struct device_attribute *devattr, char *buf)
791{
792 struct adt7475_data *data = adt7475_update_device(dev);
793 return sprintf(buf, "%d\n", !!(data->config4 & CONFIG4_MAXDUTY));
794}
795
796static ssize_t set_pwm_at_crit(struct device *dev,
797 struct device_attribute *devattr,
798 const char *buf, size_t count)
799{
800 struct i2c_client *client = to_i2c_client(dev);
801 struct adt7475_data *data = i2c_get_clientdata(client);
802 long val;
803
804 if (strict_strtol(buf, 10, &val))
805 return -EINVAL;
806 if (val != 0 && val != 1)
807 return -EINVAL;
808
809 mutex_lock(&data->lock);
810 data->config4 = i2c_smbus_read_byte_data(client, REG_CONFIG4);
811 if (val)
812 data->config4 |= CONFIG4_MAXDUTY;
813 else
814 data->config4 &= ~CONFIG4_MAXDUTY;
815 i2c_smbus_write_byte_data(client, REG_CONFIG4, data->config4);
816 mutex_unlock(&data->lock);
817
818 return count;
819}
820
Jean Delvarecffb9dd2009-12-09 20:36:03 +0100821static SENSOR_DEVICE_ATTR_2(in1_input, S_IRUGO, show_voltage, NULL, INPUT, 1);
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100822static SENSOR_DEVICE_ATTR_2(in1_max, S_IRUGO | S_IWUSR, show_voltage,
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100823 set_voltage, MAX, 1);
Jean Delvarecffb9dd2009-12-09 20:36:03 +0100824static SENSOR_DEVICE_ATTR_2(in1_min, S_IRUGO | S_IWUSR, show_voltage,
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100825 set_voltage, MIN, 1);
Jean Delvarecffb9dd2009-12-09 20:36:03 +0100826static SENSOR_DEVICE_ATTR_2(in1_alarm, S_IRUGO, show_voltage, NULL, ALARM, 1);
827static SENSOR_DEVICE_ATTR_2(in2_input, S_IRUGO, show_voltage, NULL, INPUT, 2);
828static SENSOR_DEVICE_ATTR_2(in2_max, S_IRUGO | S_IWUSR, show_voltage,
829 set_voltage, MAX, 2);
830static SENSOR_DEVICE_ATTR_2(in2_min, S_IRUGO | S_IWUSR, show_voltage,
831 set_voltage, MIN, 2);
832static SENSOR_DEVICE_ATTR_2(in2_alarm, S_IRUGO, show_voltage, NULL, ALARM, 2);
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100833static SENSOR_DEVICE_ATTR_2(temp1_input, S_IRUGO, show_temp, NULL, INPUT, 0);
834static SENSOR_DEVICE_ATTR_2(temp1_alarm, S_IRUGO, show_temp, NULL, ALARM, 0);
835static SENSOR_DEVICE_ATTR_2(temp1_fault, S_IRUGO, show_temp, NULL, FAULT, 0);
836static SENSOR_DEVICE_ATTR_2(temp1_max, S_IRUGO | S_IWUSR, show_temp, set_temp,
837 MAX, 0);
838static SENSOR_DEVICE_ATTR_2(temp1_min, S_IRUGO | S_IWUSR, show_temp, set_temp,
839 MIN, 0);
840static SENSOR_DEVICE_ATTR_2(temp1_offset, S_IRUGO | S_IWUSR, show_temp,
841 set_temp, OFFSET, 0);
842static SENSOR_DEVICE_ATTR_2(temp1_auto_point1_temp, S_IRUGO | S_IWUSR,
843 show_temp, set_temp, AUTOMIN, 0);
844static SENSOR_DEVICE_ATTR_2(temp1_auto_point2_temp, S_IRUGO | S_IWUSR,
845 show_point2, set_point2, 0, 0);
846static SENSOR_DEVICE_ATTR_2(temp1_crit, S_IRUGO | S_IWUSR, show_temp, set_temp,
847 THERM, 0);
848static SENSOR_DEVICE_ATTR_2(temp1_crit_hyst, S_IRUGO | S_IWUSR, show_temp,
849 set_temp, HYSTERSIS, 0);
850static SENSOR_DEVICE_ATTR_2(temp2_input, S_IRUGO, show_temp, NULL, INPUT, 1);
851static SENSOR_DEVICE_ATTR_2(temp2_alarm, S_IRUGO, show_temp, NULL, ALARM, 1);
852static SENSOR_DEVICE_ATTR_2(temp2_max, S_IRUGO | S_IWUSR, show_temp, set_temp,
853 MAX, 1);
854static SENSOR_DEVICE_ATTR_2(temp2_min, S_IRUGO | S_IWUSR, show_temp, set_temp,
855 MIN, 1);
856static SENSOR_DEVICE_ATTR_2(temp2_offset, S_IRUGO | S_IWUSR, show_temp,
857 set_temp, OFFSET, 1);
858static SENSOR_DEVICE_ATTR_2(temp2_auto_point1_temp, S_IRUGO | S_IWUSR,
859 show_temp, set_temp, AUTOMIN, 1);
860static SENSOR_DEVICE_ATTR_2(temp2_auto_point2_temp, S_IRUGO | S_IWUSR,
861 show_point2, set_point2, 0, 1);
862static SENSOR_DEVICE_ATTR_2(temp2_crit, S_IRUGO | S_IWUSR, show_temp, set_temp,
863 THERM, 1);
864static SENSOR_DEVICE_ATTR_2(temp2_crit_hyst, S_IRUGO | S_IWUSR, show_temp,
865 set_temp, HYSTERSIS, 1);
866static SENSOR_DEVICE_ATTR_2(temp3_input, S_IRUGO, show_temp, NULL, INPUT, 2);
867static SENSOR_DEVICE_ATTR_2(temp3_alarm, S_IRUGO, show_temp, NULL, ALARM, 2);
868static SENSOR_DEVICE_ATTR_2(temp3_fault, S_IRUGO, show_temp, NULL, FAULT, 2);
869static SENSOR_DEVICE_ATTR_2(temp3_max, S_IRUGO | S_IWUSR, show_temp, set_temp,
870 MAX, 2);
871static SENSOR_DEVICE_ATTR_2(temp3_min, S_IRUGO | S_IWUSR, show_temp, set_temp,
872 MIN, 2);
873static SENSOR_DEVICE_ATTR_2(temp3_offset, S_IRUGO | S_IWUSR, show_temp,
874 set_temp, OFFSET, 2);
875static SENSOR_DEVICE_ATTR_2(temp3_auto_point1_temp, S_IRUGO | S_IWUSR,
876 show_temp, set_temp, AUTOMIN, 2);
877static SENSOR_DEVICE_ATTR_2(temp3_auto_point2_temp, S_IRUGO | S_IWUSR,
878 show_point2, set_point2, 0, 2);
879static SENSOR_DEVICE_ATTR_2(temp3_crit, S_IRUGO | S_IWUSR, show_temp, set_temp,
880 THERM, 2);
881static SENSOR_DEVICE_ATTR_2(temp3_crit_hyst, S_IRUGO | S_IWUSR, show_temp,
882 set_temp, HYSTERSIS, 2);
883static SENSOR_DEVICE_ATTR_2(fan1_input, S_IRUGO, show_tach, NULL, INPUT, 0);
884static SENSOR_DEVICE_ATTR_2(fan1_min, S_IRUGO | S_IWUSR, show_tach, set_tach,
885 MIN, 0);
886static SENSOR_DEVICE_ATTR_2(fan1_alarm, S_IRUGO, show_tach, NULL, ALARM, 0);
887static SENSOR_DEVICE_ATTR_2(fan2_input, S_IRUGO, show_tach, NULL, INPUT, 1);
888static SENSOR_DEVICE_ATTR_2(fan2_min, S_IRUGO | S_IWUSR, show_tach, set_tach,
889 MIN, 1);
890static SENSOR_DEVICE_ATTR_2(fan2_alarm, S_IRUGO, show_tach, NULL, ALARM, 1);
891static SENSOR_DEVICE_ATTR_2(fan3_input, S_IRUGO, show_tach, NULL, INPUT, 2);
892static SENSOR_DEVICE_ATTR_2(fan3_min, S_IRUGO | S_IWUSR, show_tach, set_tach,
893 MIN, 2);
894static SENSOR_DEVICE_ATTR_2(fan3_alarm, S_IRUGO, show_tach, NULL, ALARM, 2);
895static SENSOR_DEVICE_ATTR_2(fan4_input, S_IRUGO, show_tach, NULL, INPUT, 3);
896static SENSOR_DEVICE_ATTR_2(fan4_min, S_IRUGO | S_IWUSR, show_tach, set_tach,
897 MIN, 3);
898static SENSOR_DEVICE_ATTR_2(fan4_alarm, S_IRUGO, show_tach, NULL, ALARM, 3);
899static SENSOR_DEVICE_ATTR_2(pwm1, S_IRUGO | S_IWUSR, show_pwm, set_pwm, INPUT,
900 0);
901static SENSOR_DEVICE_ATTR_2(pwm1_freq, S_IRUGO | S_IWUSR, show_pwmfreq,
902 set_pwmfreq, INPUT, 0);
903static SENSOR_DEVICE_ATTR_2(pwm1_enable, S_IRUGO | S_IWUSR, show_pwmctrl,
904 set_pwmctrl, INPUT, 0);
Jean Delvare84d2a312009-11-16 12:45:40 +0100905static SENSOR_DEVICE_ATTR_2(pwm1_auto_channels_temp, S_IRUGO | S_IWUSR,
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100906 show_pwmchan, set_pwmchan, INPUT, 0);
907static SENSOR_DEVICE_ATTR_2(pwm1_auto_point1_pwm, S_IRUGO | S_IWUSR, show_pwm,
908 set_pwm, MIN, 0);
909static SENSOR_DEVICE_ATTR_2(pwm1_auto_point2_pwm, S_IRUGO | S_IWUSR, show_pwm,
910 set_pwm, MAX, 0);
911static SENSOR_DEVICE_ATTR_2(pwm2, S_IRUGO | S_IWUSR, show_pwm, set_pwm, INPUT,
912 1);
913static SENSOR_DEVICE_ATTR_2(pwm2_freq, S_IRUGO | S_IWUSR, show_pwmfreq,
914 set_pwmfreq, INPUT, 1);
915static SENSOR_DEVICE_ATTR_2(pwm2_enable, S_IRUGO | S_IWUSR, show_pwmctrl,
916 set_pwmctrl, INPUT, 1);
Jean Delvare84d2a312009-11-16 12:45:40 +0100917static SENSOR_DEVICE_ATTR_2(pwm2_auto_channels_temp, S_IRUGO | S_IWUSR,
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100918 show_pwmchan, set_pwmchan, INPUT, 1);
919static SENSOR_DEVICE_ATTR_2(pwm2_auto_point1_pwm, S_IRUGO | S_IWUSR, show_pwm,
920 set_pwm, MIN, 1);
921static SENSOR_DEVICE_ATTR_2(pwm2_auto_point2_pwm, S_IRUGO | S_IWUSR, show_pwm,
922 set_pwm, MAX, 1);
923static SENSOR_DEVICE_ATTR_2(pwm3, S_IRUGO | S_IWUSR, show_pwm, set_pwm, INPUT,
924 2);
925static SENSOR_DEVICE_ATTR_2(pwm3_freq, S_IRUGO | S_IWUSR, show_pwmfreq,
926 set_pwmfreq, INPUT, 2);
927static SENSOR_DEVICE_ATTR_2(pwm3_enable, S_IRUGO | S_IWUSR, show_pwmctrl,
928 set_pwmctrl, INPUT, 2);
Jean Delvare84d2a312009-11-16 12:45:40 +0100929static SENSOR_DEVICE_ATTR_2(pwm3_auto_channels_temp, S_IRUGO | S_IWUSR,
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100930 show_pwmchan, set_pwmchan, INPUT, 2);
931static SENSOR_DEVICE_ATTR_2(pwm3_auto_point1_pwm, S_IRUGO | S_IWUSR, show_pwm,
932 set_pwm, MIN, 2);
933static SENSOR_DEVICE_ATTR_2(pwm3_auto_point2_pwm, S_IRUGO | S_IWUSR, show_pwm,
934 set_pwm, MAX, 2);
935
Jean Delvaref99318b2009-12-09 20:36:03 +0100936/* Non-standard name, might need revisiting */
937static DEVICE_ATTR(pwm_use_point2_pwm_at_crit, S_IWUSR | S_IRUGO,
938 show_pwm_at_crit, set_pwm_at_crit);
939
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100940static struct attribute *adt7475_attrs[] = {
941 &sensor_dev_attr_in1_input.dev_attr.attr,
942 &sensor_dev_attr_in1_max.dev_attr.attr,
943 &sensor_dev_attr_in1_min.dev_attr.attr,
944 &sensor_dev_attr_in1_alarm.dev_attr.attr,
945 &sensor_dev_attr_in2_input.dev_attr.attr,
946 &sensor_dev_attr_in2_max.dev_attr.attr,
947 &sensor_dev_attr_in2_min.dev_attr.attr,
948 &sensor_dev_attr_in2_alarm.dev_attr.attr,
949 &sensor_dev_attr_temp1_input.dev_attr.attr,
950 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
951 &sensor_dev_attr_temp1_fault.dev_attr.attr,
952 &sensor_dev_attr_temp1_max.dev_attr.attr,
953 &sensor_dev_attr_temp1_min.dev_attr.attr,
954 &sensor_dev_attr_temp1_offset.dev_attr.attr,
955 &sensor_dev_attr_temp1_auto_point1_temp.dev_attr.attr,
956 &sensor_dev_attr_temp1_auto_point2_temp.dev_attr.attr,
957 &sensor_dev_attr_temp1_crit.dev_attr.attr,
958 &sensor_dev_attr_temp1_crit_hyst.dev_attr.attr,
959 &sensor_dev_attr_temp2_input.dev_attr.attr,
960 &sensor_dev_attr_temp2_alarm.dev_attr.attr,
961 &sensor_dev_attr_temp2_max.dev_attr.attr,
962 &sensor_dev_attr_temp2_min.dev_attr.attr,
963 &sensor_dev_attr_temp2_offset.dev_attr.attr,
964 &sensor_dev_attr_temp2_auto_point1_temp.dev_attr.attr,
965 &sensor_dev_attr_temp2_auto_point2_temp.dev_attr.attr,
966 &sensor_dev_attr_temp2_crit.dev_attr.attr,
967 &sensor_dev_attr_temp2_crit_hyst.dev_attr.attr,
968 &sensor_dev_attr_temp3_input.dev_attr.attr,
969 &sensor_dev_attr_temp3_fault.dev_attr.attr,
970 &sensor_dev_attr_temp3_alarm.dev_attr.attr,
971 &sensor_dev_attr_temp3_max.dev_attr.attr,
972 &sensor_dev_attr_temp3_min.dev_attr.attr,
973 &sensor_dev_attr_temp3_offset.dev_attr.attr,
974 &sensor_dev_attr_temp3_auto_point1_temp.dev_attr.attr,
975 &sensor_dev_attr_temp3_auto_point2_temp.dev_attr.attr,
976 &sensor_dev_attr_temp3_crit.dev_attr.attr,
977 &sensor_dev_attr_temp3_crit_hyst.dev_attr.attr,
978 &sensor_dev_attr_fan1_input.dev_attr.attr,
979 &sensor_dev_attr_fan1_min.dev_attr.attr,
980 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
981 &sensor_dev_attr_fan2_input.dev_attr.attr,
982 &sensor_dev_attr_fan2_min.dev_attr.attr,
983 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
984 &sensor_dev_attr_fan3_input.dev_attr.attr,
985 &sensor_dev_attr_fan3_min.dev_attr.attr,
986 &sensor_dev_attr_fan3_alarm.dev_attr.attr,
987 &sensor_dev_attr_fan4_input.dev_attr.attr,
988 &sensor_dev_attr_fan4_min.dev_attr.attr,
989 &sensor_dev_attr_fan4_alarm.dev_attr.attr,
990 &sensor_dev_attr_pwm1.dev_attr.attr,
991 &sensor_dev_attr_pwm1_freq.dev_attr.attr,
992 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
Jean Delvare84d2a312009-11-16 12:45:40 +0100993 &sensor_dev_attr_pwm1_auto_channels_temp.dev_attr.attr,
Jordan Crouse1c301fc2009-01-15 22:27:47 +0100994 &sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr,
995 &sensor_dev_attr_pwm1_auto_point2_pwm.dev_attr.attr,
996 &sensor_dev_attr_pwm2.dev_attr.attr,
997 &sensor_dev_attr_pwm2_freq.dev_attr.attr,
998 &sensor_dev_attr_pwm2_enable.dev_attr.attr,
Jean Delvare84d2a312009-11-16 12:45:40 +0100999 &sensor_dev_attr_pwm2_auto_channels_temp.dev_attr.attr,
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001000 &sensor_dev_attr_pwm2_auto_point1_pwm.dev_attr.attr,
1001 &sensor_dev_attr_pwm2_auto_point2_pwm.dev_attr.attr,
1002 &sensor_dev_attr_pwm3.dev_attr.attr,
1003 &sensor_dev_attr_pwm3_freq.dev_attr.attr,
1004 &sensor_dev_attr_pwm3_enable.dev_attr.attr,
Jean Delvare84d2a312009-11-16 12:45:40 +01001005 &sensor_dev_attr_pwm3_auto_channels_temp.dev_attr.attr,
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001006 &sensor_dev_attr_pwm3_auto_point1_pwm.dev_attr.attr,
1007 &sensor_dev_attr_pwm3_auto_point2_pwm.dev_attr.attr,
Jean Delvaref99318b2009-12-09 20:36:03 +01001008 &dev_attr_pwm_use_point2_pwm_at_crit.attr,
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001009 NULL,
1010};
1011
Jean Delvare54ecb9e2009-12-09 20:36:03 +01001012static struct attribute_group adt7475_attr_group = { .attrs = adt7475_attrs };
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001013
1014static int adt7475_detect(struct i2c_client *client, int kind,
1015 struct i2c_board_info *info)
1016{
1017 struct i2c_adapter *adapter = client->adapter;
Jean Delvared656b6f2009-12-09 20:36:04 +01001018 int vendid, devid, devid2;
Jean Delvareb180d052009-12-09 20:36:02 +01001019 const char *name;
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001020
1021 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
1022 return -ENODEV;
1023
Jean Delvareb180d052009-12-09 20:36:02 +01001024 vendid = adt7475_read(REG_VENDID);
Jean Delvared656b6f2009-12-09 20:36:04 +01001025 devid2 = adt7475_read(REG_DEVID2);
1026 if (vendid != 0x41 || /* Analog Devices */
1027 (devid2 & 0xf8) != 0x68)
1028 return -ENODEV;
Jean Delvareb180d052009-12-09 20:36:02 +01001029
Jean Delvared656b6f2009-12-09 20:36:04 +01001030 devid = adt7475_read(REG_DEVID);
1031 if (devid == 0x73)
Jean Delvareb180d052009-12-09 20:36:02 +01001032 name = "adt7473";
Jean Delvared656b6f2009-12-09 20:36:04 +01001033 else if (devid == 0x75 && client->addr == 0x2e)
Jean Delvareb180d052009-12-09 20:36:02 +01001034 name = "adt7475";
1035 else {
1036 dev_dbg(&adapter->dev,
1037 "Couldn't detect an ADT7473 or ADT7475 part at "
1038 "0x%02x\n", (unsigned int)client->addr);
Jean Delvare52df6442009-12-09 20:35:57 +01001039 return -ENODEV;
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001040 }
1041
Jean Delvareb180d052009-12-09 20:36:02 +01001042 strlcpy(info->type, name, I2C_NAME_SIZE);
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001043
1044 return 0;
1045}
1046
1047static int adt7475_probe(struct i2c_client *client,
1048 const struct i2c_device_id *id)
1049{
1050 struct adt7475_data *data;
1051 int i, ret = 0;
1052
1053 data = kzalloc(sizeof(*data), GFP_KERNEL);
1054 if (data == NULL)
1055 return -ENOMEM;
1056
1057 mutex_init(&data->lock);
1058 i2c_set_clientdata(client, data);
1059
Jean Delvarecffb9dd2009-12-09 20:36:03 +01001060 /* Initialize device-specific values */
1061 switch (id->driver_data) {
1062 default:
1063 data->has_voltage = 0x06; /* in1, in2 */
1064 }
1065
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001066 /* Call adt7475_read_pwm for all pwm's as this will reprogram any
1067 pwm's which are disabled to manual mode with 0% duty cycle */
1068 for (i = 0; i < ADT7475_PWM_COUNT; i++)
1069 adt7475_read_pwm(client, i);
1070
1071 ret = sysfs_create_group(&client->dev.kobj, &adt7475_attr_group);
1072 if (ret)
1073 goto efree;
1074
1075 data->hwmon_dev = hwmon_device_register(&client->dev);
1076 if (IS_ERR(data->hwmon_dev)) {
1077 ret = PTR_ERR(data->hwmon_dev);
1078 goto eremove;
1079 }
1080
1081 return 0;
1082
1083eremove:
1084 sysfs_remove_group(&client->dev.kobj, &adt7475_attr_group);
1085efree:
1086 kfree(data);
1087 return ret;
1088}
1089
1090static int adt7475_remove(struct i2c_client *client)
1091{
1092 struct adt7475_data *data = i2c_get_clientdata(client);
1093
1094 hwmon_device_unregister(data->hwmon_dev);
1095 sysfs_remove_group(&client->dev.kobj, &adt7475_attr_group);
1096 kfree(data);
1097
1098 return 0;
1099}
1100
1101static struct i2c_driver adt7475_driver = {
1102 .class = I2C_CLASS_HWMON,
1103 .driver = {
1104 .name = "adt7475",
1105 },
1106 .probe = adt7475_probe,
1107 .remove = adt7475_remove,
1108 .id_table = adt7475_id,
1109 .detect = adt7475_detect,
1110 .address_data = &addr_data,
1111};
1112
1113static void adt7475_read_hystersis(struct i2c_client *client)
1114{
1115 struct adt7475_data *data = i2c_get_clientdata(client);
1116
1117 data->temp[HYSTERSIS][0] = (u16) adt7475_read(REG_REMOTE1_HYSTERSIS);
1118 data->temp[HYSTERSIS][1] = data->temp[HYSTERSIS][0];
1119 data->temp[HYSTERSIS][2] = (u16) adt7475_read(REG_REMOTE2_HYSTERSIS);
1120}
1121
1122static void adt7475_read_pwm(struct i2c_client *client, int index)
1123{
1124 struct adt7475_data *data = i2c_get_clientdata(client);
1125 unsigned int v;
1126
1127 data->pwm[CONTROL][index] = adt7475_read(PWM_CONFIG_REG(index));
1128
1129 /* Figure out the internal value for pwmctrl and pwmchan
1130 based on the current settings */
1131 v = (data->pwm[CONTROL][index] >> 5) & 7;
1132
1133 if (v == 3)
1134 data->pwmctl[index] = 0;
1135 else if (v == 7)
1136 data->pwmctl[index] = 1;
1137 else if (v == 4) {
1138 /* The fan is disabled - we don't want to
1139 support that, so change to manual mode and
1140 set the duty cycle to 0 instead
1141 */
1142 data->pwm[INPUT][index] = 0;
1143 data->pwm[CONTROL][index] &= ~0xE0;
1144 data->pwm[CONTROL][index] |= (7 << 5);
1145
1146 i2c_smbus_write_byte_data(client, PWM_CONFIG_REG(index),
1147 data->pwm[INPUT][index]);
1148
1149 i2c_smbus_write_byte_data(client, PWM_CONFIG_REG(index),
1150 data->pwm[CONTROL][index]);
1151
1152 data->pwmctl[index] = 1;
1153 } else {
1154 data->pwmctl[index] = 2;
1155
1156 switch (v) {
1157 case 0:
1158 data->pwmchan[index] = 1;
1159 break;
1160 case 1:
1161 data->pwmchan[index] = 2;
1162 break;
1163 case 2:
1164 data->pwmchan[index] = 4;
1165 break;
1166 case 5:
1167 data->pwmchan[index] = 6;
1168 break;
1169 case 6:
1170 data->pwmchan[index] = 7;
1171 break;
1172 }
1173 }
1174}
1175
1176static struct adt7475_data *adt7475_update_device(struct device *dev)
1177{
1178 struct i2c_client *client = to_i2c_client(dev);
1179 struct adt7475_data *data = i2c_get_clientdata(client);
1180 u8 ext;
1181 int i;
1182
1183 mutex_lock(&data->lock);
1184
1185 /* Measurement values update every 2 seconds */
1186 if (time_after(jiffies, data->measure_updated + HZ * 2) ||
1187 !data->valid) {
1188 data->alarms = adt7475_read(REG_STATUS2) << 8;
1189 data->alarms |= adt7475_read(REG_STATUS1);
1190
1191 ext = adt7475_read(REG_EXTEND1);
Jean Delvarecffb9dd2009-12-09 20:36:03 +01001192 for (i = 0; i < ADT7475_VOLTAGE_COUNT; i++) {
1193 if (!(data->has_voltage & (1 << i)))
1194 continue;
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001195 data->voltage[INPUT][i] =
1196 (adt7475_read(VOLTAGE_REG(i)) << 2) |
Jean Delvarecffb9dd2009-12-09 20:36:03 +01001197 ((ext >> (i * 2)) & 3);
1198 }
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001199
1200 ext = adt7475_read(REG_EXTEND2);
1201 for (i = 0; i < ADT7475_TEMP_COUNT; i++)
1202 data->temp[INPUT][i] =
1203 (adt7475_read(TEMP_REG(i)) << 2) |
1204 ((ext >> ((i + 1) * 2)) & 3);
1205
1206 for (i = 0; i < ADT7475_TACH_COUNT; i++)
1207 data->tach[INPUT][i] =
1208 adt7475_read_word(client, TACH_REG(i));
1209
1210 /* Updated by hw when in auto mode */
1211 for (i = 0; i < ADT7475_PWM_COUNT; i++)
1212 data->pwm[INPUT][i] = adt7475_read(PWM_REG(i));
1213
1214 data->measure_updated = jiffies;
1215 }
1216
1217 /* Limits and settings, should never change update every 60 seconds */
Jean Delvare56e35ee2009-11-16 12:45:40 +01001218 if (time_after(jiffies, data->limits_updated + HZ * 60) ||
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001219 !data->valid) {
Jean Delvaref99318b2009-12-09 20:36:03 +01001220 data->config4 = adt7475_read(REG_CONFIG4);
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001221 data->config5 = adt7475_read(REG_CONFIG5);
1222
1223 for (i = 0; i < ADT7475_VOLTAGE_COUNT; i++) {
Jean Delvarecffb9dd2009-12-09 20:36:03 +01001224 if (!(data->has_voltage & (1 << i)))
1225 continue;
Jordan Crouse1c301fc2009-01-15 22:27:47 +01001226 /* Adjust values so they match the input precision */
1227 data->voltage[MIN][i] =
1228 adt7475_read(VOLTAGE_MIN_REG(i)) << 2;
1229 data->voltage[MAX][i] =
1230 adt7475_read(VOLTAGE_MAX_REG(i)) << 2;
1231 }
1232
1233 for (i = 0; i < ADT7475_TEMP_COUNT; i++) {
1234 /* Adjust values so they match the input precision */
1235 data->temp[MIN][i] =
1236 adt7475_read(TEMP_MIN_REG(i)) << 2;
1237 data->temp[MAX][i] =
1238 adt7475_read(TEMP_MAX_REG(i)) << 2;
1239 data->temp[AUTOMIN][i] =
1240 adt7475_read(TEMP_TMIN_REG(i)) << 2;
1241 data->temp[THERM][i] =
1242 adt7475_read(TEMP_THERM_REG(i)) << 2;
1243 data->temp[OFFSET][i] =
1244 adt7475_read(TEMP_OFFSET_REG(i));
1245 }
1246 adt7475_read_hystersis(client);
1247
1248 for (i = 0; i < ADT7475_TACH_COUNT; i++)
1249 data->tach[MIN][i] =
1250 adt7475_read_word(client, TACH_MIN_REG(i));
1251
1252 for (i = 0; i < ADT7475_PWM_COUNT; i++) {
1253 data->pwm[MAX][i] = adt7475_read(PWM_MAX_REG(i));
1254 data->pwm[MIN][i] = adt7475_read(PWM_MIN_REG(i));
1255 /* Set the channel and control information */
1256 adt7475_read_pwm(client, i);
1257 }
1258
1259 data->range[0] = adt7475_read(TEMP_TRANGE_REG(0));
1260 data->range[1] = adt7475_read(TEMP_TRANGE_REG(1));
1261 data->range[2] = adt7475_read(TEMP_TRANGE_REG(2));
1262
1263 data->limits_updated = jiffies;
1264 data->valid = 1;
1265 }
1266
1267 mutex_unlock(&data->lock);
1268
1269 return data;
1270}
1271
1272static int __init sensors_adt7475_init(void)
1273{
1274 return i2c_add_driver(&adt7475_driver);
1275}
1276
1277static void __exit sensors_adt7475_exit(void)
1278{
1279 i2c_del_driver(&adt7475_driver);
1280}
1281
1282MODULE_AUTHOR("Advanced Micro Devices, Inc");
1283MODULE_DESCRIPTION("adt7475 driver");
1284MODULE_LICENSE("GPL");
1285
1286module_init(sensors_adt7475_init);
1287module_exit(sensors_adt7475_exit);