aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/hwmon/adt7473.c
diff options
context:
space:
mode:
authorDarrick J. Wong <djwong@us.ibm.com>2008-02-18 16:33:23 -0500
committerMark M. Hoffman <mhoffman@lightlink.com>2008-02-18 21:51:29 -0500
commit57df46d6d9ccd6ced95f169020f79ae637423087 (patch)
tree7ca14cba0e78cc8f35094e7cc9a8cbd7a715af19 /drivers/hwmon/adt7473.c
parentae770152c801f10a91e5e86597a39b5f9ccf2d0d (diff)
hwmon: New driver for Analog Devices ADT7473 sensor chip
This driver reports voltage, temperature and fan sensor readings on an ADT7473 chip. Signed-off-by: Darrick J. Wong <djwong@us.ibm.com> Signed-off-by: Mark M. Hoffman <mhoffman@lightlink.com>
Diffstat (limited to 'drivers/hwmon/adt7473.c')
-rw-r--r--drivers/hwmon/adt7473.c1157
1 files changed, 1157 insertions, 0 deletions
diff --git a/drivers/hwmon/adt7473.c b/drivers/hwmon/adt7473.c
new file mode 100644
index 000000000000..a3daeefff472
--- /dev/null
+++ b/drivers/hwmon/adt7473.c
@@ -0,0 +1,1157 @@
1/*
2 * A hwmon driver for the Analog Devices ADT7473
3 * Copyright (C) 2007 IBM
4 *
5 * Author: Darrick J. Wong <djwong@us.ibm.com>
6 *
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>
29#include <linux/delay.h>
30#include <linux/log2.h>
31
32/* Addresses to scan */
33static unsigned short normal_i2c[] = { 0x2C, 0x2D, 0x2E, I2C_CLIENT_END };
34
35/* Insmod parameters */
36I2C_CLIENT_INSMOD_1(adt7473);
37
38/* ADT7473 registers */
39#define ADT7473_REG_BASE_ADDR 0x20
40
41#define ADT7473_REG_VOLT_BASE_ADDR 0x21
42#define ADT7473_REG_VOLT_MAX_ADDR 0x22
43#define ADT7473_REG_VOLT_MIN_BASE_ADDR 0x46
44#define ADT7473_REG_VOLT_MIN_MAX_ADDR 0x49
45
46#define ADT7473_REG_TEMP_BASE_ADDR 0x25
47#define ADT7473_REG_TEMP_MAX_ADDR 0x27
48#define ADT7473_REG_TEMP_LIMITS_BASE_ADDR 0x4E
49#define ADT7473_REG_TEMP_LIMITS_MAX_ADDR 0x53
50#define ADT7473_REG_TEMP_TMIN_BASE_ADDR 0x67
51#define ADT7473_REG_TEMP_TMIN_MAX_ADDR 0x69
52#define ADT7473_REG_TEMP_TMAX_BASE_ADDR 0x6A
53#define ADT7473_REG_TEMP_TMAX_MAX_ADDR 0x6C
54
55#define ADT7473_REG_FAN_BASE_ADDR 0x28
56#define ADT7473_REG_FAN_MAX_ADDR 0x2F
57#define ADT7473_REG_FAN_MIN_BASE_ADDR 0x54
58#define ADT7473_REG_FAN_MIN_MAX_ADDR 0x5B
59
60#define ADT7473_REG_PWM_BASE_ADDR 0x30
61#define ADT7473_REG_PWM_MAX_ADDR 0x32
62#define ADT7473_REG_PWM_MIN_BASE_ADDR 0x64
63#define ADT7473_REG_PWM_MIN_MAX_ADDR 0x66
64#define ADT7473_REG_PWM_MAX_BASE_ADDR 0x38
65#define ADT7473_REG_PWM_MAX_MAX_ADDR 0x3A
66#define ADT7473_REG_PWM_BHVR_BASE_ADDR 0x5C
67#define ADT7473_REG_PWM_BHVR_MAX_ADDR 0x5E
68#define ADT7473_PWM_BHVR_MASK 0xE0
69#define ADT7473_PWM_BHVR_SHIFT 5
70
71#define ADT7473_REG_CFG1 0x40
72#define ADT7473_CFG1_START 0x01
73#define ADT7473_CFG1_READY 0x04
74#define ADT7473_REG_CFG2 0x73
75#define ADT7473_REG_CFG3 0x78
76#define ADT7473_REG_CFG4 0x7D
77#define ADT7473_CFG4_MAX_DUTY_AT_OVT 0x08
78#define ADT7473_REG_CFG5 0x7C
79#define ADT7473_CFG5_TEMP_TWOS 0x01
80#define ADT7473_CFG5_TEMP_OFFSET 0x02
81
82#define ADT7473_REG_DEVICE 0x3D
83#define ADT7473_VENDOR 0x41
84#define ADT7473_REG_VENDOR 0x3E
85#define ADT7473_DEVICE 0x73
86#define ADT7473_REG_REVISION 0x3F
87#define ADT7473_REV_68 0x68
88#define ADT7473_REV_69 0x69
89
90#define ADT7473_REG_ALARM1 0x41
91#define ADT7473_VCCP_ALARM 0x02
92#define ADT7473_VCC_ALARM 0x04
93#define ADT7473_R1T_ALARM 0x10
94#define ADT7473_LT_ALARM 0x20
95#define ADT7473_R2T_ALARM 0x40
96#define ADT7473_OOL 0x80
97#define ADT7473_REG_ALARM2 0x42
98#define ADT7473_OVT_ALARM 0x02
99#define ADT7473_FAN1_ALARM 0x04
100#define ADT7473_FAN2_ALARM 0x08
101#define ADT7473_FAN3_ALARM 0x10
102#define ADT7473_FAN4_ALARM 0x20
103#define ADT7473_R1T_SHORT 0x40
104#define ADT7473_R2T_SHORT 0x80
105#define ADT7473_REG_MAX_ADDR 0x80
106
107#define ALARM2(x) ((x) << 8)
108
109#define ADT7473_VOLT_COUNT 2
110#define ADT7473_REG_VOLT(x) (ADT7473_REG_VOLT_BASE_ADDR + (x))
111#define ADT7473_REG_VOLT_MIN(x) (ADT7473_REG_VOLT_MIN_BASE_ADDR + ((x) * 2))
112#define ADT7473_REG_VOLT_MAX(x) (ADT7473_REG_VOLT_MIN_BASE_ADDR + \
113 ((x) * 2) + 1)
114
115#define ADT7473_TEMP_COUNT 3
116#define ADT7473_REG_TEMP(x) (ADT7473_REG_TEMP_BASE_ADDR + (x))
117#define ADT7473_REG_TEMP_MIN(x) (ADT7473_REG_TEMP_LIMITS_BASE_ADDR + ((x) * 2))
118#define ADT7473_REG_TEMP_MAX(x) (ADT7473_REG_TEMP_LIMITS_BASE_ADDR + \
119 ((x) * 2) + 1)
120#define ADT7473_REG_TEMP_TMIN(x) (ADT7473_REG_TEMP_TMIN_BASE_ADDR + (x))
121#define ADT7473_REG_TEMP_TMAX(x) (ADT7473_REG_TEMP_TMAX_BASE_ADDR + (x))
122
123#define ADT7473_FAN_COUNT 4
124#define ADT7473_REG_FAN(x) (ADT7473_REG_FAN_BASE_ADDR + ((x) * 2))
125#define ADT7473_REG_FAN_MIN(x) (ADT7473_REG_FAN_MIN_BASE_ADDR + ((x) * 2))
126
127#define ADT7473_PWM_COUNT 3
128#define ADT7473_REG_PWM(x) (ADT7473_REG_PWM_BASE_ADDR + (x))
129#define ADT7473_REG_PWM_MAX(x) (ADT7473_REG_PWM_MAX_BASE_ADDR + (x))
130#define ADT7473_REG_PWM_MIN(x) (ADT7473_REG_PWM_MIN_BASE_ADDR + (x))
131#define ADT7473_REG_PWM_BHVR(x) (ADT7473_REG_PWM_BHVR_BASE_ADDR + (x))
132
133/* How often do we reread sensors values? (In jiffies) */
134#define SENSOR_REFRESH_INTERVAL (2 * HZ)
135
136/* How often do we reread sensor limit values? (In jiffies) */
137#define LIMIT_REFRESH_INTERVAL (60 * HZ)
138
139/* datasheet says to divide this number by the fan reading to get fan rpm */
140#define FAN_PERIOD_TO_RPM(x) ((90000 * 60) / (x))
141#define FAN_RPM_TO_PERIOD FAN_PERIOD_TO_RPM
142#define FAN_PERIOD_INVALID 65535
143#define FAN_DATA_VALID(x) ((x) && (x) != FAN_PERIOD_INVALID)
144
145struct adt7473_data {
146 struct i2c_client client;
147 struct device *hwmon_dev;
148 struct attribute_group attrs;
149 struct mutex lock;
150 char sensors_valid;
151 char limits_valid;
152 unsigned long sensors_last_updated; /* In jiffies */
153 unsigned long limits_last_updated; /* In jiffies */
154
155 u8 volt[ADT7473_VOLT_COUNT];
156 s8 volt_min[ADT7473_VOLT_COUNT];
157 s8 volt_max[ADT7473_VOLT_COUNT];
158
159 s8 temp[ADT7473_TEMP_COUNT];
160 s8 temp_min[ADT7473_TEMP_COUNT];
161 s8 temp_max[ADT7473_TEMP_COUNT];
162 s8 temp_tmin[ADT7473_TEMP_COUNT];
163 /* This is called the !THERM limit in the datasheet */
164 s8 temp_tmax[ADT7473_TEMP_COUNT];
165
166 u16 fan[ADT7473_FAN_COUNT];
167 u16 fan_min[ADT7473_FAN_COUNT];
168
169 u8 pwm[ADT7473_PWM_COUNT];
170 u8 pwm_max[ADT7473_PWM_COUNT];
171 u8 pwm_min[ADT7473_PWM_COUNT];
172 u8 pwm_behavior[ADT7473_PWM_COUNT];
173
174 u8 temp_twos_complement;
175 u8 temp_offset;
176
177 u16 alarm;
178 u8 max_duty_at_overheat;
179};
180
181static int adt7473_attach_adapter(struct i2c_adapter *adapter);
182static int adt7473_detect(struct i2c_adapter *adapter, int address, int kind);
183static int adt7473_detach_client(struct i2c_client *client);
184
185static struct i2c_driver adt7473_driver = {
186 .driver = {
187 .name = "adt7473",
188 },
189 .attach_adapter = adt7473_attach_adapter,
190 .detach_client = adt7473_detach_client,
191};
192
193/*
194 * 16-bit registers on the ADT7473 are low-byte first. The data sheet says
195 * that the low byte must be read before the high byte.
196 */
197static inline int adt7473_read_word_data(struct i2c_client *client, u8 reg)
198{
199 u16 foo;
200 foo = i2c_smbus_read_byte_data(client, reg);
201 foo |= ((u16)i2c_smbus_read_byte_data(client, reg + 1) << 8);
202 return foo;
203}
204
205static inline int adt7473_write_word_data(struct i2c_client *client, u8 reg,
206 u16 value)
207{
208 return i2c_smbus_write_byte_data(client, reg, value & 0xFF)
209 && i2c_smbus_write_byte_data(client, reg + 1, value >> 8);
210}
211
212static void adt7473_init_client(struct i2c_client *client)
213{
214 int reg = i2c_smbus_read_byte_data(client, ADT7473_REG_CFG1);
215
216 if (!(reg & ADT7473_CFG1_READY)) {
217 dev_err(&client->dev, "Chip not ready.\n");
218 } else {
219 /* start monitoring */
220 i2c_smbus_write_byte_data(client, ADT7473_REG_CFG1,
221 reg | ADT7473_CFG1_START);
222 }
223}
224
225static struct adt7473_data *adt7473_update_device(struct device *dev)
226{
227 struct i2c_client *client = to_i2c_client(dev);
228 struct adt7473_data *data = i2c_get_clientdata(client);
229 unsigned long local_jiffies = jiffies;
230 u8 cfg;
231 int i;
232
233 mutex_lock(&data->lock);
234 if (time_before(local_jiffies, data->sensors_last_updated +
235 SENSOR_REFRESH_INTERVAL)
236 && data->sensors_valid)
237 goto no_sensor_update;
238
239 for (i = 0; i < ADT7473_VOLT_COUNT; i++)
240 data->volt[i] = i2c_smbus_read_byte_data(client,
241 ADT7473_REG_VOLT(i));
242
243 /* Determine temperature encoding */
244 cfg = i2c_smbus_read_byte_data(client, ADT7473_REG_CFG5);
245 data->temp_twos_complement = (cfg & ADT7473_CFG5_TEMP_TWOS);
246
247 /*
248 * What does this do? it implies a variable temperature sensor
249 * offset, but the datasheet doesn't say anything about this bit
250 * and other parts of the datasheet imply that "offset64" mode
251 * means that you shift temp values by -64 if the above bit was set.
252 */
253 data->temp_offset = (cfg & ADT7473_CFG5_TEMP_OFFSET);
254
255 for (i = 0; i < ADT7473_TEMP_COUNT; i++)
256 data->temp[i] = i2c_smbus_read_byte_data(client,
257 ADT7473_REG_TEMP(i));
258
259 for (i = 0; i < ADT7473_FAN_COUNT; i++)
260 data->fan[i] = adt7473_read_word_data(client,
261 ADT7473_REG_FAN(i));
262
263 for (i = 0; i < ADT7473_PWM_COUNT; i++)
264 data->pwm[i] = i2c_smbus_read_byte_data(client,
265 ADT7473_REG_PWM(i));
266
267 data->alarm = i2c_smbus_read_byte_data(client, ADT7473_REG_ALARM1);
268 if (data->alarm & ADT7473_OOL)
269 data->alarm |= ALARM2(i2c_smbus_read_byte_data(client,
270 ADT7473_REG_ALARM2));
271
272 data->sensors_last_updated = local_jiffies;
273 data->sensors_valid = 1;
274
275no_sensor_update:
276 if (time_before(local_jiffies, data->limits_last_updated +
277 LIMIT_REFRESH_INTERVAL)
278 && data->limits_valid)
279 goto out;
280
281 for (i = 0; i < ADT7473_VOLT_COUNT; i++) {
282 data->volt_min[i] = i2c_smbus_read_byte_data(client,
283 ADT7473_REG_VOLT_MIN(i));
284 data->volt_max[i] = i2c_smbus_read_byte_data(client,
285 ADT7473_REG_VOLT_MAX(i));
286 }
287
288 for (i = 0; i < ADT7473_TEMP_COUNT; i++) {
289 data->temp_min[i] = i2c_smbus_read_byte_data(client,
290 ADT7473_REG_TEMP_MIN(i));
291 data->temp_max[i] = i2c_smbus_read_byte_data(client,
292 ADT7473_REG_TEMP_MAX(i));
293 data->temp_tmin[i] = i2c_smbus_read_byte_data(client,
294 ADT7473_REG_TEMP_TMIN(i));
295 data->temp_tmax[i] = i2c_smbus_read_byte_data(client,
296 ADT7473_REG_TEMP_TMAX(i));
297 }
298
299 for (i = 0; i < ADT7473_FAN_COUNT; i++)
300 data->fan_min[i] = adt7473_read_word_data(client,
301 ADT7473_REG_FAN_MIN(i));
302
303 for (i = 0; i < ADT7473_PWM_COUNT; i++) {
304 data->pwm_max[i] = i2c_smbus_read_byte_data(client,
305 ADT7473_REG_PWM_MAX(i));
306 data->pwm_min[i] = i2c_smbus_read_byte_data(client,
307 ADT7473_REG_PWM_MIN(i));
308 data->pwm_behavior[i] = i2c_smbus_read_byte_data(client,
309 ADT7473_REG_PWM_BHVR(i));
310 }
311
312 data->limits_last_updated = local_jiffies;
313 data->limits_valid = 1;
314
315out:
316 mutex_unlock(&data->lock);
317 return data;
318}
319
320/*
321 * On this chip, voltages are given as a count of steps between a minimum
322 * and maximum voltage, not a direct voltage.
323 */
324static const int volt_convert_table[][2] = {
325 {2997, 3},
326 {4395, 4},
327};
328
329static int decode_volt(int volt_index, u8 raw)
330{
331 int cmax = volt_convert_table[volt_index][0];
332 int cmin = volt_convert_table[volt_index][1];
333 return ((raw * (cmax - cmin)) / 255) + cmin;
334}
335
336static u8 encode_volt(int volt_index, int cooked)
337{
338 int cmax = volt_convert_table[volt_index][0];
339 int cmin = volt_convert_table[volt_index][1];
340 u8 x;
341
342 if (cooked > cmax)
343 cooked = cmax;
344 else if (cooked < cmin)
345 cooked = cmin;
346
347 x = ((cooked - cmin) * 255) / (cmax - cmin);
348
349 return x;
350}
351
352static ssize_t show_volt_min(struct device *dev,
353 struct device_attribute *devattr,
354 char *buf)
355{
356 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
357 struct adt7473_data *data = adt7473_update_device(dev);
358 return sprintf(buf, "%d\n",
359 decode_volt(attr->index, data->volt_min[attr->index]));
360}
361
362static ssize_t set_volt_min(struct device *dev,
363 struct device_attribute *devattr,
364 const char *buf,
365 size_t count)
366{
367 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
368 struct i2c_client *client = to_i2c_client(dev);
369 struct adt7473_data *data = i2c_get_clientdata(client);
370 int volt = encode_volt(attr->index, simple_strtol(buf, NULL, 10));
371
372 mutex_lock(&data->lock);
373 data->volt_min[attr->index] = volt;
374 i2c_smbus_write_byte_data(client, ADT7473_REG_VOLT_MIN(attr->index),
375 volt);
376 mutex_unlock(&data->lock);
377
378 return count;
379}
380
381static ssize_t show_volt_max(struct device *dev,
382 struct device_attribute *devattr,
383 char *buf)
384{
385 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
386 struct adt7473_data *data = adt7473_update_device(dev);
387 return sprintf(buf, "%d\n",
388 decode_volt(attr->index, data->volt_max[attr->index]));
389}
390
391static ssize_t set_volt_max(struct device *dev,
392 struct device_attribute *devattr,
393 const char *buf,
394 size_t count)
395{
396 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
397 struct i2c_client *client = to_i2c_client(dev);
398 struct adt7473_data *data = i2c_get_clientdata(client);
399 int volt = encode_volt(attr->index, simple_strtol(buf, NULL, 10));
400
401 mutex_lock(&data->lock);
402 data->volt_max[attr->index] = volt;
403 i2c_smbus_write_byte_data(client, ADT7473_REG_VOLT_MAX(attr->index),
404 volt);
405 mutex_unlock(&data->lock);
406
407 return count;
408}
409
410static ssize_t show_volt(struct device *dev, struct device_attribute *devattr,
411 char *buf)
412{
413 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
414 struct adt7473_data *data = adt7473_update_device(dev);
415
416 return sprintf(buf, "%d\n",
417 decode_volt(attr->index, data->volt[attr->index]));
418}
419
420/*
421 * This chip can report temperature data either as a two's complement
422 * number in the range -128 to 127, or as an unsigned number that must
423 * be offset by 64.
424 */
425static int decode_temp(struct adt7473_data *data, u8 raw)
426{
427 if (data->temp_twos_complement)
428 return (s8)raw;
429 return raw - 64;
430}
431
432static u8 encode_temp(struct adt7473_data *data, int cooked)
433{
434 if (data->temp_twos_complement)
435 return (cooked & 0xFF);
436 return cooked + 64;
437}
438
439static ssize_t show_temp_min(struct device *dev,
440 struct device_attribute *devattr,
441 char *buf)
442{
443 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
444 struct adt7473_data *data = adt7473_update_device(dev);
445 return sprintf(buf, "%d\n",
446 1000 * decode_temp(data, data->temp_min[attr->index]));
447}
448
449static ssize_t set_temp_min(struct device *dev,
450 struct device_attribute *devattr,
451 const char *buf,
452 size_t count)
453{
454 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
455 struct i2c_client *client = to_i2c_client(dev);
456 struct adt7473_data *data = i2c_get_clientdata(client);
457 int temp = simple_strtol(buf, NULL, 10) / 1000;
458 temp = encode_temp(data, temp);
459
460 mutex_lock(&data->lock);
461 data->temp_min[attr->index] = temp;
462 i2c_smbus_write_byte_data(client, ADT7473_REG_TEMP_MIN(attr->index),
463 temp);
464 mutex_unlock(&data->lock);
465
466 return count;
467}
468
469static ssize_t show_temp_max(struct device *dev,
470 struct device_attribute *devattr,
471 char *buf)
472{
473 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
474 struct adt7473_data *data = adt7473_update_device(dev);
475 return sprintf(buf, "%d\n",
476 1000 * decode_temp(data, data->temp_max[attr->index]));
477}
478
479static ssize_t set_temp_max(struct device *dev,
480 struct device_attribute *devattr,
481 const char *buf,
482 size_t count)
483{
484 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
485 struct i2c_client *client = to_i2c_client(dev);
486 struct adt7473_data *data = i2c_get_clientdata(client);
487 int temp = simple_strtol(buf, NULL, 10) / 1000;
488 temp = encode_temp(data, temp);
489
490 mutex_lock(&data->lock);
491 data->temp_max[attr->index] = temp;
492 i2c_smbus_write_byte_data(client, ADT7473_REG_TEMP_MAX(attr->index),
493 temp);
494 mutex_unlock(&data->lock);
495
496 return count;
497}
498
499static ssize_t show_temp(struct device *dev, struct device_attribute *devattr,
500 char *buf)
501{
502 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
503 struct adt7473_data *data = adt7473_update_device(dev);
504 return sprintf(buf, "%d\n",
505 1000 * decode_temp(data, data->temp[attr->index]));
506}
507
508static ssize_t show_fan_min(struct device *dev,
509 struct device_attribute *devattr,
510 char *buf)
511{
512 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
513 struct adt7473_data *data = adt7473_update_device(dev);
514
515 if (FAN_DATA_VALID(data->fan_min[attr->index]))
516 return sprintf(buf, "%d\n",
517 FAN_PERIOD_TO_RPM(data->fan_min[attr->index]));
518 else
519 return sprintf(buf, "0\n");
520}
521
522static ssize_t set_fan_min(struct device *dev,
523 struct device_attribute *devattr,
524 const char *buf, size_t count)
525{
526 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
527 struct i2c_client *client = to_i2c_client(dev);
528 struct adt7473_data *data = i2c_get_clientdata(client);
529 int temp = simple_strtol(buf, NULL, 10);
530
531 if (!temp)
532 return -EINVAL;
533 temp = FAN_RPM_TO_PERIOD(temp);
534
535 mutex_lock(&data->lock);
536 data->fan_min[attr->index] = temp;
537 adt7473_write_word_data(client, ADT7473_REG_FAN_MIN(attr->index), temp);
538 mutex_unlock(&data->lock);
539
540 return count;
541}
542
543static ssize_t show_fan(struct device *dev, struct device_attribute *devattr,
544 char *buf)
545{
546 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
547 struct adt7473_data *data = adt7473_update_device(dev);
548
549 if (FAN_DATA_VALID(data->fan[attr->index]))
550 return sprintf(buf, "%d\n",
551 FAN_PERIOD_TO_RPM(data->fan[attr->index]));
552 else
553 return sprintf(buf, "0\n");
554}
555
556static ssize_t show_max_duty_at_crit(struct device *dev,
557 struct device_attribute *devattr,
558 char *buf)
559{
560 struct adt7473_data *data = adt7473_update_device(dev);
561 return sprintf(buf, "%d\n", data->max_duty_at_overheat);
562}
563
564static ssize_t set_max_duty_at_crit(struct device *dev,
565 struct device_attribute *devattr,
566 const char *buf,
567 size_t count)
568{
569 u8 reg;
570 struct i2c_client *client = to_i2c_client(dev);
571 struct adt7473_data *data = i2c_get_clientdata(client);
572 int temp = simple_strtol(buf, NULL, 10);
573 temp = temp && 0xFF;
574
575 mutex_lock(&data->lock);
576 data->max_duty_at_overheat = temp;
577 reg = i2c_smbus_read_byte_data(client, ADT7473_REG_CFG4);
578 if (temp)
579 reg |= ADT7473_CFG4_MAX_DUTY_AT_OVT;
580 else
581 reg &= ~ADT7473_CFG4_MAX_DUTY_AT_OVT;
582 i2c_smbus_write_byte_data(client, ADT7473_REG_CFG4, reg);
583 mutex_unlock(&data->lock);
584
585 return count;
586}
587
588static ssize_t show_pwm(struct device *dev, struct device_attribute *devattr,
589 char *buf)
590{
591 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
592 struct adt7473_data *data = adt7473_update_device(dev);
593 return sprintf(buf, "%d\n", data->pwm[attr->index]);
594}
595
596static ssize_t set_pwm(struct device *dev, struct device_attribute *devattr,
597 const char *buf, size_t count)
598{
599 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
600 struct i2c_client *client = to_i2c_client(dev);
601 struct adt7473_data *data = i2c_get_clientdata(client);
602 int temp = simple_strtol(buf, NULL, 10);
603
604 mutex_lock(&data->lock);
605 data->pwm[attr->index] = temp;
606 i2c_smbus_write_byte_data(client, ADT7473_REG_PWM(attr->index), temp);
607 mutex_unlock(&data->lock);
608
609 return count;
610}
611
612static ssize_t show_pwm_max(struct device *dev,
613 struct device_attribute *devattr,
614 char *buf)
615{
616 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
617 struct adt7473_data *data = adt7473_update_device(dev);
618 return sprintf(buf, "%d\n", data->pwm_max[attr->index]);
619}
620
621static ssize_t set_pwm_max(struct device *dev,
622 struct device_attribute *devattr,
623 const char *buf,
624 size_t count)
625{
626 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
627 struct i2c_client *client = to_i2c_client(dev);
628 struct adt7473_data *data = i2c_get_clientdata(client);
629 int temp = simple_strtol(buf, NULL, 10);
630
631 mutex_lock(&data->lock);
632 data->pwm_max[attr->index] = temp;
633 i2c_smbus_write_byte_data(client, ADT7473_REG_PWM_MAX(attr->index),
634 temp);
635 mutex_unlock(&data->lock);
636
637 return count;
638}
639
640static ssize_t show_pwm_min(struct device *dev,
641 struct device_attribute *devattr,
642 char *buf)
643{
644 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
645 struct adt7473_data *data = adt7473_update_device(dev);
646 return sprintf(buf, "%d\n", data->pwm_min[attr->index]);
647}
648
649static ssize_t set_pwm_min(struct device *dev,
650 struct device_attribute *devattr,
651 const char *buf,
652 size_t count)
653{
654 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
655 struct i2c_client *client = to_i2c_client(dev);
656 struct adt7473_data *data = i2c_get_clientdata(client);
657 int temp = simple_strtol(buf, NULL, 10);
658
659 mutex_lock(&data->lock);
660 data->pwm_min[attr->index] = temp;
661 i2c_smbus_write_byte_data(client, ADT7473_REG_PWM_MIN(attr->index),
662 temp);
663 mutex_unlock(&data->lock);
664
665 return count;
666}
667
668static ssize_t show_temp_tmax(struct device *dev,
669 struct device_attribute *devattr,
670 char *buf)
671{
672 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
673 struct adt7473_data *data = adt7473_update_device(dev);
674 return sprintf(buf, "%d\n",
675 1000 * decode_temp(data, data->temp_tmax[attr->index]));
676}
677
678static ssize_t set_temp_tmax(struct device *dev,
679 struct device_attribute *devattr,
680 const char *buf,
681 size_t count)
682{
683 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
684 struct i2c_client *client = to_i2c_client(dev);
685 struct adt7473_data *data = i2c_get_clientdata(client);
686 int temp = simple_strtol(buf, NULL, 10) / 1000;
687 temp = encode_temp(data, temp);
688
689 mutex_lock(&data->lock);
690 data->temp_tmax[attr->index] = temp;
691 i2c_smbus_write_byte_data(client, ADT7473_REG_TEMP_TMAX(attr->index),
692 temp);
693 mutex_unlock(&data->lock);
694
695 return count;
696}
697
698static ssize_t show_temp_tmin(struct device *dev,
699 struct device_attribute *devattr,
700 char *buf)
701{
702 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
703 struct adt7473_data *data = adt7473_update_device(dev);
704 return sprintf(buf, "%d\n",
705 1000 * decode_temp(data, data->temp_tmin[attr->index]));
706}
707
708static ssize_t set_temp_tmin(struct device *dev,
709 struct device_attribute *devattr,
710 const char *buf,
711 size_t count)
712{
713 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
714 struct i2c_client *client = to_i2c_client(dev);
715 struct adt7473_data *data = i2c_get_clientdata(client);
716 int temp = simple_strtol(buf, NULL, 10) / 1000;
717 temp = encode_temp(data, temp);
718
719 mutex_lock(&data->lock);
720 data->temp_tmin[attr->index] = temp;
721 i2c_smbus_write_byte_data(client, ADT7473_REG_TEMP_TMIN(attr->index),
722 temp);
723 mutex_unlock(&data->lock);
724
725 return count;
726}
727
728static ssize_t show_pwm_enable(struct device *dev,
729 struct device_attribute *devattr,
730 char *buf)
731{
732 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
733 struct adt7473_data *data = adt7473_update_device(dev);
734
735 switch (data->pwm_behavior[attr->index] >> ADT7473_PWM_BHVR_SHIFT) {
736 case 3:
737 return sprintf(buf, "0\n");
738 case 7:
739 return sprintf(buf, "1\n");
740 default:
741 return sprintf(buf, "2\n");
742 }
743}
744
745static ssize_t set_pwm_enable(struct device *dev,
746 struct device_attribute *devattr,
747 const char *buf,
748 size_t count)
749{
750 u8 reg;
751 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
752 struct i2c_client *client = to_i2c_client(dev);
753 struct adt7473_data *data = i2c_get_clientdata(client);
754 int temp = simple_strtol(buf, NULL, 10);
755
756 switch (temp) {
757 case 0:
758 temp = 3;
759 break;
760 case 1:
761 temp = 7;
762 break;
763 case 2:
764 /* Enter automatic mode with fans off */
765 temp = 4;
766 break;
767 default:
768 return -EINVAL;
769 }
770
771 mutex_lock(&data->lock);
772 reg = i2c_smbus_read_byte_data(client,
773 ADT7473_REG_PWM_BHVR(attr->index));
774 reg = (temp << ADT7473_PWM_BHVR_SHIFT) |
775 (reg & ~ADT7473_PWM_BHVR_MASK);
776 i2c_smbus_write_byte_data(client, ADT7473_REG_PWM_BHVR(attr->index),
777 reg);
778 data->pwm_behavior[attr->index] = reg;
779 mutex_unlock(&data->lock);
780
781 return count;
782}
783
784static ssize_t show_pwm_auto_temp(struct device *dev,
785 struct device_attribute *devattr,
786 char *buf)
787{
788 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
789 struct adt7473_data *data = adt7473_update_device(dev);
790 int bhvr = data->pwm_behavior[attr->index] >> ADT7473_PWM_BHVR_SHIFT;
791
792 switch (bhvr) {
793 case 3:
794 case 4:
795 case 7:
796 return sprintf(buf, "0\n");
797 case 0:
798 case 1:
799 case 5:
800 case 6:
801 return sprintf(buf, "%d\n", bhvr + 1);
802 case 2:
803 return sprintf(buf, "4\n");
804 }
805 /* shouldn't ever get here */
806 BUG();
807}
808
809static ssize_t set_pwm_auto_temp(struct device *dev,
810 struct device_attribute *devattr,
811 const char *buf,
812 size_t count)
813{
814 u8 reg;
815 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
816 struct i2c_client *client = to_i2c_client(dev);
817 struct adt7473_data *data = i2c_get_clientdata(client);
818 int temp = simple_strtol(buf, NULL, 10);
819
820 switch (temp) {
821 case 1:
822 case 2:
823 case 6:
824 case 7:
825 temp--;
826 break;
827 case 0:
828 temp = 4;
829 break;
830 default:
831 return -EINVAL;
832 }
833
834 mutex_lock(&data->lock);
835 reg = i2c_smbus_read_byte_data(client,
836 ADT7473_REG_PWM_BHVR(attr->index));
837 reg = (temp << ADT7473_PWM_BHVR_SHIFT) |
838 (reg & ~ADT7473_PWM_BHVR_MASK);
839 i2c_smbus_write_byte_data(client, ADT7473_REG_PWM_BHVR(attr->index),
840 reg);
841 data->pwm_behavior[attr->index] = reg;
842 mutex_unlock(&data->lock);
843
844 return count;
845}
846
847static ssize_t show_alarm(struct device *dev,
848 struct device_attribute *devattr,
849 char *buf)
850{
851 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
852 struct adt7473_data *data = adt7473_update_device(dev);
853
854 if (data->alarm & attr->index)
855 return sprintf(buf, "1\n");
856 else
857 return sprintf(buf, "0\n");
858}
859
860
861static SENSOR_DEVICE_ATTR(in1_max, S_IWUSR | S_IRUGO, show_volt_max,
862 set_volt_max, 0);
863static SENSOR_DEVICE_ATTR(in2_max, S_IWUSR | S_IRUGO, show_volt_max,
864 set_volt_max, 1);
865
866static SENSOR_DEVICE_ATTR(in1_min, S_IWUSR | S_IRUGO, show_volt_min,
867 set_volt_min, 0);
868static SENSOR_DEVICE_ATTR(in2_min, S_IWUSR | S_IRUGO, show_volt_min,
869 set_volt_min, 1);
870
871static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, show_volt, NULL, 0);
872static SENSOR_DEVICE_ATTR(in2_input, S_IRUGO, show_volt, NULL, 1);
873
874static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL,
875 ADT7473_VCCP_ALARM);
876static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL,
877 ADT7473_VCC_ALARM);
878
879static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp_max,
880 set_temp_max, 0);
881static SENSOR_DEVICE_ATTR(temp2_max, S_IWUSR | S_IRUGO, show_temp_max,
882 set_temp_max, 1);
883static SENSOR_DEVICE_ATTR(temp3_max, S_IWUSR | S_IRUGO, show_temp_max,
884 set_temp_max, 2);
885
886static SENSOR_DEVICE_ATTR(temp1_min, S_IWUSR | S_IRUGO, show_temp_min,
887 set_temp_min, 0);
888static SENSOR_DEVICE_ATTR(temp2_min, S_IWUSR | S_IRUGO, show_temp_min,
889 set_temp_min, 1);
890static SENSOR_DEVICE_ATTR(temp3_min, S_IWUSR | S_IRUGO, show_temp_min,
891 set_temp_min, 2);
892
893static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0);
894static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp, NULL, 1);
895static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO, show_temp, NULL, 2);
896
897static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL,
898 ADT7473_R1T_ALARM | ALARM2(ADT7473_R1T_SHORT));
899static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL,
900 ADT7473_LT_ALARM);
901static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL,
902 ADT7473_R2T_ALARM | ALARM2(ADT7473_R2T_SHORT));
903
904static SENSOR_DEVICE_ATTR(fan1_min, S_IWUSR | S_IRUGO, show_fan_min,
905 set_fan_min, 0);
906static SENSOR_DEVICE_ATTR(fan2_min, S_IWUSR | S_IRUGO, show_fan_min,
907 set_fan_min, 1);
908static SENSOR_DEVICE_ATTR(fan3_min, S_IWUSR | S_IRUGO, show_fan_min,
909 set_fan_min, 2);
910static SENSOR_DEVICE_ATTR(fan4_min, S_IWUSR | S_IRUGO, show_fan_min,
911 set_fan_min, 3);
912
913static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, show_fan, NULL, 0);
914static SENSOR_DEVICE_ATTR(fan2_input, S_IRUGO, show_fan, NULL, 1);
915static SENSOR_DEVICE_ATTR(fan3_input, S_IRUGO, show_fan, NULL, 2);
916static SENSOR_DEVICE_ATTR(fan4_input, S_IRUGO, show_fan, NULL, 3);
917
918static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL,
919 ALARM2(ADT7473_FAN1_ALARM));
920static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL,
921 ALARM2(ADT7473_FAN2_ALARM));
922static SENSOR_DEVICE_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL,
923 ALARM2(ADT7473_FAN3_ALARM));
924static SENSOR_DEVICE_ATTR(fan4_alarm, S_IRUGO, show_alarm, NULL,
925 ALARM2(ADT7473_FAN4_ALARM));
926
927static SENSOR_DEVICE_ATTR(pwm_use_point2_pwm_at_crit, S_IWUSR | S_IRUGO,
928 show_max_duty_at_crit, set_max_duty_at_crit, 0);
929
930static SENSOR_DEVICE_ATTR(pwm1, S_IWUSR | S_IRUGO, show_pwm, set_pwm, 0);
931static SENSOR_DEVICE_ATTR(pwm2, S_IWUSR | S_IRUGO, show_pwm, set_pwm, 1);
932static SENSOR_DEVICE_ATTR(pwm3, S_IWUSR | S_IRUGO, show_pwm, set_pwm, 2);
933
934static SENSOR_DEVICE_ATTR(pwm1_auto_point1_pwm, S_IWUSR | S_IRUGO,
935 show_pwm_min, set_pwm_min, 0);
936static SENSOR_DEVICE_ATTR(pwm2_auto_point1_pwm, S_IWUSR | S_IRUGO,
937 show_pwm_min, set_pwm_min, 1);
938static SENSOR_DEVICE_ATTR(pwm3_auto_point1_pwm, S_IWUSR | S_IRUGO,
939 show_pwm_min, set_pwm_min, 2);
940
941static SENSOR_DEVICE_ATTR(pwm1_auto_point2_pwm, S_IWUSR | S_IRUGO,
942 show_pwm_max, set_pwm_max, 0);
943static SENSOR_DEVICE_ATTR(pwm2_auto_point2_pwm, S_IWUSR | S_IRUGO,
944 show_pwm_max, set_pwm_max, 1);
945static SENSOR_DEVICE_ATTR(pwm3_auto_point2_pwm, S_IWUSR | S_IRUGO,
946 show_pwm_max, set_pwm_max, 2);
947
948static SENSOR_DEVICE_ATTR(temp1_auto_point1_temp, S_IWUSR | S_IRUGO,
949 show_temp_tmin, set_temp_tmin, 0);
950static SENSOR_DEVICE_ATTR(temp2_auto_point1_temp, S_IWUSR | S_IRUGO,
951 show_temp_tmin, set_temp_tmin, 1);
952static SENSOR_DEVICE_ATTR(temp3_auto_point1_temp, S_IWUSR | S_IRUGO,
953 show_temp_tmin, set_temp_tmin, 2);
954
955static SENSOR_DEVICE_ATTR(temp1_auto_point2_temp, S_IWUSR | S_IRUGO,
956 show_temp_tmax, set_temp_tmax, 0);
957static SENSOR_DEVICE_ATTR(temp2_auto_point2_temp, S_IWUSR | S_IRUGO,
958 show_temp_tmax, set_temp_tmax, 1);
959static SENSOR_DEVICE_ATTR(temp3_auto_point2_temp, S_IWUSR | S_IRUGO,
960 show_temp_tmax, set_temp_tmax, 2);
961
962static SENSOR_DEVICE_ATTR(pwm1_enable, S_IWUSR | S_IRUGO, show_pwm_enable,
963 set_pwm_enable, 0);
964static SENSOR_DEVICE_ATTR(pwm2_enable, S_IWUSR | S_IRUGO, show_pwm_enable,
965 set_pwm_enable, 1);
966static SENSOR_DEVICE_ATTR(pwm3_enable, S_IWUSR | S_IRUGO, show_pwm_enable,
967 set_pwm_enable, 2);
968
969static SENSOR_DEVICE_ATTR(pwm1_auto_channels_temp, S_IWUSR | S_IRUGO,
970 show_pwm_auto_temp, set_pwm_auto_temp, 0);
971static SENSOR_DEVICE_ATTR(pwm2_auto_channels_temp, S_IWUSR | S_IRUGO,
972 show_pwm_auto_temp, set_pwm_auto_temp, 1);
973static SENSOR_DEVICE_ATTR(pwm3_auto_channels_temp, S_IWUSR | S_IRUGO,
974 show_pwm_auto_temp, set_pwm_auto_temp, 2);
975
976static struct attribute *adt7473_attr[] =
977{
978 &sensor_dev_attr_in1_max.dev_attr.attr,
979 &sensor_dev_attr_in2_max.dev_attr.attr,
980 &sensor_dev_attr_in1_min.dev_attr.attr,
981 &sensor_dev_attr_in2_min.dev_attr.attr,
982 &sensor_dev_attr_in1_input.dev_attr.attr,
983 &sensor_dev_attr_in2_input.dev_attr.attr,
984 &sensor_dev_attr_in1_alarm.dev_attr.attr,
985 &sensor_dev_attr_in2_alarm.dev_attr.attr,
986
987 &sensor_dev_attr_temp1_max.dev_attr.attr,
988 &sensor_dev_attr_temp2_max.dev_attr.attr,
989 &sensor_dev_attr_temp3_max.dev_attr.attr,
990 &sensor_dev_attr_temp1_min.dev_attr.attr,
991 &sensor_dev_attr_temp2_min.dev_attr.attr,
992 &sensor_dev_attr_temp3_min.dev_attr.attr,
993 &sensor_dev_attr_temp1_input.dev_attr.attr,
994 &sensor_dev_attr_temp2_input.dev_attr.attr,
995 &sensor_dev_attr_temp3_input.dev_attr.attr,
996 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
997 &sensor_dev_attr_temp2_alarm.dev_attr.attr,
998 &sensor_dev_attr_temp3_alarm.dev_attr.attr,
999 &sensor_dev_attr_temp1_auto_point1_temp.dev_attr.attr,
1000 &sensor_dev_attr_temp2_auto_point1_temp.dev_attr.attr,
1001 &sensor_dev_attr_temp3_auto_point1_temp.dev_attr.attr,
1002 &sensor_dev_attr_temp1_auto_point2_temp.dev_attr.attr,
1003 &sensor_dev_attr_temp2_auto_point2_temp.dev_attr.attr,
1004 &sensor_dev_attr_temp3_auto_point2_temp.dev_attr.attr,
1005
1006 &sensor_dev_attr_fan1_min.dev_attr.attr,
1007 &sensor_dev_attr_fan2_min.dev_attr.attr,
1008 &sensor_dev_attr_fan3_min.dev_attr.attr,
1009 &sensor_dev_attr_fan4_min.dev_attr.attr,
1010 &sensor_dev_attr_fan1_input.dev_attr.attr,
1011 &sensor_dev_attr_fan2_input.dev_attr.attr,
1012 &sensor_dev_attr_fan3_input.dev_attr.attr,
1013 &sensor_dev_attr_fan4_input.dev_attr.attr,
1014 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
1015 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
1016 &sensor_dev_attr_fan3_alarm.dev_attr.attr,
1017 &sensor_dev_attr_fan4_alarm.dev_attr.attr,
1018
1019 &sensor_dev_attr_pwm_use_point2_pwm_at_crit.dev_attr.attr,
1020
1021 &sensor_dev_attr_pwm1.dev_attr.attr,
1022 &sensor_dev_attr_pwm2.dev_attr.attr,
1023 &sensor_dev_attr_pwm3.dev_attr.attr,
1024 &sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr,
1025 &sensor_dev_attr_pwm2_auto_point1_pwm.dev_attr.attr,
1026 &sensor_dev_attr_pwm3_auto_point1_pwm.dev_attr.attr,
1027 &sensor_dev_attr_pwm1_auto_point2_pwm.dev_attr.attr,
1028 &sensor_dev_attr_pwm2_auto_point2_pwm.dev_attr.attr,
1029 &sensor_dev_attr_pwm3_auto_point2_pwm.dev_attr.attr,
1030
1031 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
1032 &sensor_dev_attr_pwm2_enable.dev_attr.attr,
1033 &sensor_dev_attr_pwm3_enable.dev_attr.attr,
1034 &sensor_dev_attr_pwm1_auto_channels_temp.dev_attr.attr,
1035 &sensor_dev_attr_pwm2_auto_channels_temp.dev_attr.attr,
1036 &sensor_dev_attr_pwm3_auto_channels_temp.dev_attr.attr,
1037
1038 NULL
1039};
1040
1041static int adt7473_attach_adapter(struct i2c_adapter *adapter)
1042{
1043 if (!(adapter->class & I2C_CLASS_HWMON))
1044 return 0;
1045 return i2c_probe(adapter, &addr_data, adt7473_detect);
1046}
1047
1048static int adt7473_detect(struct i2c_adapter *adapter, int address, int kind)
1049{
1050 struct i2c_client *client;
1051 struct adt7473_data *data;
1052 int err = 0;
1053
1054 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
1055 goto exit;
1056
1057 data = kzalloc(sizeof(struct adt7473_data), GFP_KERNEL);
1058 if (!data) {
1059 err = -ENOMEM;
1060 goto exit;
1061 }
1062
1063 client = &data->client;
1064 client->addr = address;
1065 client->adapter = adapter;
1066 client->driver = &adt7473_driver;
1067
1068 i2c_set_clientdata(client, data);
1069
1070 mutex_init(&data->lock);
1071
1072 if (kind <= 0) {
1073 int vendor, device, revision;
1074
1075 vendor = i2c_smbus_read_byte_data(client, ADT7473_REG_VENDOR);
1076 if (vendor != ADT7473_VENDOR) {
1077 err = -ENODEV;
1078 goto exit_free;
1079 }
1080
1081 device = i2c_smbus_read_byte_data(client, ADT7473_REG_DEVICE);
1082 if (device != ADT7473_DEVICE) {
1083 err = -ENODEV;
1084 goto exit_free;
1085 }
1086
1087 revision = i2c_smbus_read_byte_data(client,
1088 ADT7473_REG_REVISION);
1089 if (revision != ADT7473_REV_68 && revision != ADT7473_REV_69) {
1090 err = -ENODEV;
1091 goto exit_free;
1092 }
1093 } else
1094 dev_dbg(&adapter->dev, "detection forced\n");
1095
1096 strlcpy(client->name, "adt7473", I2C_NAME_SIZE);
1097
1098 err = i2c_attach_client(client);
1099 if (err)
1100 goto exit_free;
1101
1102 dev_info(&client->dev, "%s chip found\n", client->name);
1103
1104 /* Initialize the ADT7473 chip */
1105 adt7473_init_client(client);
1106
1107 /* Register sysfs hooks */
1108 data->attrs.attrs = adt7473_attr;
1109 err = sysfs_create_group(&client->dev.kobj, &data->attrs);
1110 if (err)
1111 goto exit_detach;
1112
1113 data->hwmon_dev = hwmon_device_register(&client->dev);
1114 if (IS_ERR(data->hwmon_dev)) {
1115 err = PTR_ERR(data->hwmon_dev);
1116 goto exit_remove;
1117 }
1118
1119 return 0;
1120
1121exit_remove:
1122 sysfs_remove_group(&client->dev.kobj, &data->attrs);
1123exit_detach:
1124 i2c_detach_client(client);
1125exit_free:
1126 kfree(data);
1127exit:
1128 return err;
1129}
1130
1131static int adt7473_detach_client(struct i2c_client *client)
1132{
1133 struct adt7473_data *data = i2c_get_clientdata(client);
1134
1135 hwmon_device_unregister(data->hwmon_dev);
1136 sysfs_remove_group(&client->dev.kobj, &data->attrs);
1137 i2c_detach_client(client);
1138 kfree(data);
1139 return 0;
1140}
1141
1142static int __init adt7473_init(void)
1143{
1144 return i2c_add_driver(&adt7473_driver);
1145}
1146
1147static void __exit adt7473_exit(void)
1148{
1149 i2c_del_driver(&adt7473_driver);
1150}
1151
1152MODULE_AUTHOR("Darrick J. Wong <djwong@us.ibm.com>");
1153MODULE_DESCRIPTION("ADT7473 driver");
1154MODULE_LICENSE("GPL");
1155
1156module_init(adt7473_init);
1157module_exit(adt7473_exit);