blob: 78fadaa431e2d95c093d082e833160c5b105a88b [file] [log] [blame]
Darrick J. Wong57df46d2008-02-18 13:33:23 -08001/*
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 */
Mark M. Hoffman25e9c862008-02-17 22:28:03 -050033static const unsigned short normal_i2c[] = { 0x2C, 0x2D, 0x2E, I2C_CLIENT_END };
Darrick J. Wong57df46d2008-02-18 13:33:23 -080034
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
Darrick J. Wong57df46d2008-02-18 13:33:23 -080042#define ADT7473_REG_VOLT_MIN_BASE_ADDR 0x46
Darrick J. Wong57df46d2008-02-18 13:33:23 -080043
44#define ADT7473_REG_TEMP_BASE_ADDR 0x25
Darrick J. Wong57df46d2008-02-18 13:33:23 -080045#define ADT7473_REG_TEMP_LIMITS_BASE_ADDR 0x4E
Darrick J. Wong57df46d2008-02-18 13:33:23 -080046#define ADT7473_REG_TEMP_TMIN_BASE_ADDR 0x67
Darrick J. Wong57df46d2008-02-18 13:33:23 -080047#define ADT7473_REG_TEMP_TMAX_BASE_ADDR 0x6A
Darrick J. Wong57df46d2008-02-18 13:33:23 -080048
49#define ADT7473_REG_FAN_BASE_ADDR 0x28
Darrick J. Wong57df46d2008-02-18 13:33:23 -080050#define ADT7473_REG_FAN_MIN_BASE_ADDR 0x54
Darrick J. Wong57df46d2008-02-18 13:33:23 -080051
52#define ADT7473_REG_PWM_BASE_ADDR 0x30
Darrick J. Wong57df46d2008-02-18 13:33:23 -080053#define ADT7473_REG_PWM_MIN_BASE_ADDR 0x64
Darrick J. Wong57df46d2008-02-18 13:33:23 -080054#define ADT7473_REG_PWM_MAX_BASE_ADDR 0x38
Darrick J. Wong57df46d2008-02-18 13:33:23 -080055#define ADT7473_REG_PWM_BHVR_BASE_ADDR 0x5C
Darrick J. Wong57df46d2008-02-18 13:33:23 -080056#define ADT7473_PWM_BHVR_MASK 0xE0
57#define ADT7473_PWM_BHVR_SHIFT 5
58
59#define ADT7473_REG_CFG1 0x40
60#define ADT7473_CFG1_START 0x01
61#define ADT7473_CFG1_READY 0x04
62#define ADT7473_REG_CFG2 0x73
63#define ADT7473_REG_CFG3 0x78
64#define ADT7473_REG_CFG4 0x7D
65#define ADT7473_CFG4_MAX_DUTY_AT_OVT 0x08
66#define ADT7473_REG_CFG5 0x7C
67#define ADT7473_CFG5_TEMP_TWOS 0x01
68#define ADT7473_CFG5_TEMP_OFFSET 0x02
69
70#define ADT7473_REG_DEVICE 0x3D
71#define ADT7473_VENDOR 0x41
72#define ADT7473_REG_VENDOR 0x3E
73#define ADT7473_DEVICE 0x73
74#define ADT7473_REG_REVISION 0x3F
75#define ADT7473_REV_68 0x68
76#define ADT7473_REV_69 0x69
77
78#define ADT7473_REG_ALARM1 0x41
79#define ADT7473_VCCP_ALARM 0x02
80#define ADT7473_VCC_ALARM 0x04
81#define ADT7473_R1T_ALARM 0x10
82#define ADT7473_LT_ALARM 0x20
83#define ADT7473_R2T_ALARM 0x40
84#define ADT7473_OOL 0x80
85#define ADT7473_REG_ALARM2 0x42
86#define ADT7473_OVT_ALARM 0x02
87#define ADT7473_FAN1_ALARM 0x04
88#define ADT7473_FAN2_ALARM 0x08
89#define ADT7473_FAN3_ALARM 0x10
90#define ADT7473_FAN4_ALARM 0x20
91#define ADT7473_R1T_SHORT 0x40
92#define ADT7473_R2T_SHORT 0x80
Darrick J. Wong57df46d2008-02-18 13:33:23 -080093
94#define ALARM2(x) ((x) << 8)
95
96#define ADT7473_VOLT_COUNT 2
97#define ADT7473_REG_VOLT(x) (ADT7473_REG_VOLT_BASE_ADDR + (x))
98#define ADT7473_REG_VOLT_MIN(x) (ADT7473_REG_VOLT_MIN_BASE_ADDR + ((x) * 2))
99#define ADT7473_REG_VOLT_MAX(x) (ADT7473_REG_VOLT_MIN_BASE_ADDR + \
100 ((x) * 2) + 1)
101
102#define ADT7473_TEMP_COUNT 3
103#define ADT7473_REG_TEMP(x) (ADT7473_REG_TEMP_BASE_ADDR + (x))
104#define ADT7473_REG_TEMP_MIN(x) (ADT7473_REG_TEMP_LIMITS_BASE_ADDR + ((x) * 2))
105#define ADT7473_REG_TEMP_MAX(x) (ADT7473_REG_TEMP_LIMITS_BASE_ADDR + \
106 ((x) * 2) + 1)
107#define ADT7473_REG_TEMP_TMIN(x) (ADT7473_REG_TEMP_TMIN_BASE_ADDR + (x))
108#define ADT7473_REG_TEMP_TMAX(x) (ADT7473_REG_TEMP_TMAX_BASE_ADDR + (x))
109
110#define ADT7473_FAN_COUNT 4
111#define ADT7473_REG_FAN(x) (ADT7473_REG_FAN_BASE_ADDR + ((x) * 2))
112#define ADT7473_REG_FAN_MIN(x) (ADT7473_REG_FAN_MIN_BASE_ADDR + ((x) * 2))
113
114#define ADT7473_PWM_COUNT 3
115#define ADT7473_REG_PWM(x) (ADT7473_REG_PWM_BASE_ADDR + (x))
116#define ADT7473_REG_PWM_MAX(x) (ADT7473_REG_PWM_MAX_BASE_ADDR + (x))
117#define ADT7473_REG_PWM_MIN(x) (ADT7473_REG_PWM_MIN_BASE_ADDR + (x))
118#define ADT7473_REG_PWM_BHVR(x) (ADT7473_REG_PWM_BHVR_BASE_ADDR + (x))
119
120/* How often do we reread sensors values? (In jiffies) */
121#define SENSOR_REFRESH_INTERVAL (2 * HZ)
122
123/* How often do we reread sensor limit values? (In jiffies) */
124#define LIMIT_REFRESH_INTERVAL (60 * HZ)
125
126/* datasheet says to divide this number by the fan reading to get fan rpm */
127#define FAN_PERIOD_TO_RPM(x) ((90000 * 60) / (x))
128#define FAN_RPM_TO_PERIOD FAN_PERIOD_TO_RPM
129#define FAN_PERIOD_INVALID 65535
130#define FAN_DATA_VALID(x) ((x) && (x) != FAN_PERIOD_INVALID)
131
132struct adt7473_data {
Darrick J. Wong57df46d2008-02-18 13:33:23 -0800133 struct device *hwmon_dev;
134 struct attribute_group attrs;
135 struct mutex lock;
136 char sensors_valid;
137 char limits_valid;
138 unsigned long sensors_last_updated; /* In jiffies */
139 unsigned long limits_last_updated; /* In jiffies */
140
141 u8 volt[ADT7473_VOLT_COUNT];
142 s8 volt_min[ADT7473_VOLT_COUNT];
143 s8 volt_max[ADT7473_VOLT_COUNT];
144
145 s8 temp[ADT7473_TEMP_COUNT];
146 s8 temp_min[ADT7473_TEMP_COUNT];
147 s8 temp_max[ADT7473_TEMP_COUNT];
148 s8 temp_tmin[ADT7473_TEMP_COUNT];
149 /* This is called the !THERM limit in the datasheet */
150 s8 temp_tmax[ADT7473_TEMP_COUNT];
151
152 u16 fan[ADT7473_FAN_COUNT];
153 u16 fan_min[ADT7473_FAN_COUNT];
154
155 u8 pwm[ADT7473_PWM_COUNT];
156 u8 pwm_max[ADT7473_PWM_COUNT];
157 u8 pwm_min[ADT7473_PWM_COUNT];
158 u8 pwm_behavior[ADT7473_PWM_COUNT];
159
160 u8 temp_twos_complement;
161 u8 temp_offset;
162
163 u16 alarm;
164 u8 max_duty_at_overheat;
165};
166
Jean Delvareeea54762008-07-16 19:30:10 +0200167static int adt7473_probe(struct i2c_client *client,
168 const struct i2c_device_id *id);
Jean Delvare310ec792009-12-14 21:17:23 +0100169static int adt7473_detect(struct i2c_client *client,
Jean Delvareeea54762008-07-16 19:30:10 +0200170 struct i2c_board_info *info);
171static int adt7473_remove(struct i2c_client *client);
172
173static const struct i2c_device_id adt7473_id[] = {
174 { "adt7473", adt7473 },
175 { }
176};
Darrick J. Wong57df46d2008-02-18 13:33:23 -0800177
178static struct i2c_driver adt7473_driver = {
Jean Delvareeea54762008-07-16 19:30:10 +0200179 .class = I2C_CLASS_HWMON,
Darrick J. Wong57df46d2008-02-18 13:33:23 -0800180 .driver = {
181 .name = "adt7473",
182 },
Jean Delvareeea54762008-07-16 19:30:10 +0200183 .probe = adt7473_probe,
184 .remove = adt7473_remove,
185 .id_table = adt7473_id,
186 .detect = adt7473_detect,
187 .address_data = &addr_data,
Darrick J. Wong57df46d2008-02-18 13:33:23 -0800188};
189
190/*
191 * 16-bit registers on the ADT7473 are low-byte first. The data sheet says
192 * that the low byte must be read before the high byte.
193 */
194static inline int adt7473_read_word_data(struct i2c_client *client, u8 reg)
195{
196 u16 foo;
197 foo = i2c_smbus_read_byte_data(client, reg);
198 foo |= ((u16)i2c_smbus_read_byte_data(client, reg + 1) << 8);
199 return foo;
200}
201
202static inline int adt7473_write_word_data(struct i2c_client *client, u8 reg,
203 u16 value)
204{
205 return i2c_smbus_write_byte_data(client, reg, value & 0xFF)
206 && i2c_smbus_write_byte_data(client, reg + 1, value >> 8);
207}
208
209static void adt7473_init_client(struct i2c_client *client)
210{
211 int reg = i2c_smbus_read_byte_data(client, ADT7473_REG_CFG1);
212
213 if (!(reg & ADT7473_CFG1_READY)) {
214 dev_err(&client->dev, "Chip not ready.\n");
215 } else {
216 /* start monitoring */
217 i2c_smbus_write_byte_data(client, ADT7473_REG_CFG1,
218 reg | ADT7473_CFG1_START);
219 }
220}
221
222static struct adt7473_data *adt7473_update_device(struct device *dev)
223{
224 struct i2c_client *client = to_i2c_client(dev);
225 struct adt7473_data *data = i2c_get_clientdata(client);
226 unsigned long local_jiffies = jiffies;
227 u8 cfg;
228 int i;
229
230 mutex_lock(&data->lock);
231 if (time_before(local_jiffies, data->sensors_last_updated +
232 SENSOR_REFRESH_INTERVAL)
233 && data->sensors_valid)
234 goto no_sensor_update;
235
236 for (i = 0; i < ADT7473_VOLT_COUNT; i++)
237 data->volt[i] = i2c_smbus_read_byte_data(client,
238 ADT7473_REG_VOLT(i));
239
240 /* Determine temperature encoding */
241 cfg = i2c_smbus_read_byte_data(client, ADT7473_REG_CFG5);
242 data->temp_twos_complement = (cfg & ADT7473_CFG5_TEMP_TWOS);
243
244 /*
245 * What does this do? it implies a variable temperature sensor
246 * offset, but the datasheet doesn't say anything about this bit
247 * and other parts of the datasheet imply that "offset64" mode
248 * means that you shift temp values by -64 if the above bit was set.
249 */
250 data->temp_offset = (cfg & ADT7473_CFG5_TEMP_OFFSET);
251
252 for (i = 0; i < ADT7473_TEMP_COUNT; i++)
253 data->temp[i] = i2c_smbus_read_byte_data(client,
254 ADT7473_REG_TEMP(i));
255
256 for (i = 0; i < ADT7473_FAN_COUNT; i++)
257 data->fan[i] = adt7473_read_word_data(client,
258 ADT7473_REG_FAN(i));
259
260 for (i = 0; i < ADT7473_PWM_COUNT; i++)
261 data->pwm[i] = i2c_smbus_read_byte_data(client,
262 ADT7473_REG_PWM(i));
263
264 data->alarm = i2c_smbus_read_byte_data(client, ADT7473_REG_ALARM1);
265 if (data->alarm & ADT7473_OOL)
266 data->alarm |= ALARM2(i2c_smbus_read_byte_data(client,
267 ADT7473_REG_ALARM2));
268
269 data->sensors_last_updated = local_jiffies;
270 data->sensors_valid = 1;
271
272no_sensor_update:
273 if (time_before(local_jiffies, data->limits_last_updated +
274 LIMIT_REFRESH_INTERVAL)
275 && data->limits_valid)
276 goto out;
277
278 for (i = 0; i < ADT7473_VOLT_COUNT; i++) {
279 data->volt_min[i] = i2c_smbus_read_byte_data(client,
280 ADT7473_REG_VOLT_MIN(i));
281 data->volt_max[i] = i2c_smbus_read_byte_data(client,
282 ADT7473_REG_VOLT_MAX(i));
283 }
284
285 for (i = 0; i < ADT7473_TEMP_COUNT; i++) {
286 data->temp_min[i] = i2c_smbus_read_byte_data(client,
287 ADT7473_REG_TEMP_MIN(i));
288 data->temp_max[i] = i2c_smbus_read_byte_data(client,
289 ADT7473_REG_TEMP_MAX(i));
290 data->temp_tmin[i] = i2c_smbus_read_byte_data(client,
291 ADT7473_REG_TEMP_TMIN(i));
292 data->temp_tmax[i] = i2c_smbus_read_byte_data(client,
293 ADT7473_REG_TEMP_TMAX(i));
294 }
295
296 for (i = 0; i < ADT7473_FAN_COUNT; i++)
297 data->fan_min[i] = adt7473_read_word_data(client,
298 ADT7473_REG_FAN_MIN(i));
299
300 for (i = 0; i < ADT7473_PWM_COUNT; i++) {
301 data->pwm_max[i] = i2c_smbus_read_byte_data(client,
302 ADT7473_REG_PWM_MAX(i));
303 data->pwm_min[i] = i2c_smbus_read_byte_data(client,
304 ADT7473_REG_PWM_MIN(i));
305 data->pwm_behavior[i] = i2c_smbus_read_byte_data(client,
306 ADT7473_REG_PWM_BHVR(i));
307 }
308
Jean Delvareed4ec812008-04-26 16:34:26 +0200309 i = i2c_smbus_read_byte_data(client, ADT7473_REG_CFG4);
310 data->max_duty_at_overheat = !!(i & ADT7473_CFG4_MAX_DUTY_AT_OVT);
311
Darrick J. Wong57df46d2008-02-18 13:33:23 -0800312 data->limits_last_updated = local_jiffies;
313 data->limits_valid = 1;
314
315out:
316 mutex_unlock(&data->lock);
317 return data;
318}
319
320/*
Jean Delvarebe821b72008-10-26 17:04:40 +0100321 * Conversions
Darrick J. Wong57df46d2008-02-18 13:33:23 -0800322 */
Jean Delvarebe821b72008-10-26 17:04:40 +0100323
324/* IN are scaled acording to built-in resistors */
325static const int adt7473_scaling[] = { /* .001 Volts */
326 2250, 3300
Darrick J. Wong57df46d2008-02-18 13:33:23 -0800327};
Jean Delvarebe821b72008-10-26 17:04:40 +0100328#define SCALE(val, from, to) (((val) * (to) + ((from) / 2)) / (from))
Darrick J. Wong57df46d2008-02-18 13:33:23 -0800329
330static int decode_volt(int volt_index, u8 raw)
331{
Jean Delvarebe821b72008-10-26 17:04:40 +0100332 return SCALE(raw, 192, adt7473_scaling[volt_index]);
Darrick J. Wong57df46d2008-02-18 13:33:23 -0800333}
334
335static u8 encode_volt(int volt_index, int cooked)
336{
Jean Delvarebe821b72008-10-26 17:04:40 +0100337 int raw = SCALE(cooked, adt7473_scaling[volt_index], 192);
338 return SENSORS_LIMIT(raw, 0, 255);
Darrick J. Wong57df46d2008-02-18 13:33:23 -0800339}
340
341static ssize_t show_volt_min(struct device *dev,
342 struct device_attribute *devattr,
343 char *buf)
344{
345 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
346 struct adt7473_data *data = adt7473_update_device(dev);
347 return sprintf(buf, "%d\n",
348 decode_volt(attr->index, data->volt_min[attr->index]));
349}
350
351static ssize_t set_volt_min(struct device *dev,
352 struct device_attribute *devattr,
353 const char *buf,
354 size_t count)
355{
356 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
357 struct i2c_client *client = to_i2c_client(dev);
358 struct adt7473_data *data = i2c_get_clientdata(client);
Darrick J. Wong862343c2008-11-12 13:26:58 -0800359 long volt;
360
361 if (strict_strtol(buf, 10, &volt))
362 return -EINVAL;
363
364 volt = encode_volt(attr->index, volt);
Darrick J. Wong57df46d2008-02-18 13:33:23 -0800365
366 mutex_lock(&data->lock);
367 data->volt_min[attr->index] = volt;
368 i2c_smbus_write_byte_data(client, ADT7473_REG_VOLT_MIN(attr->index),
369 volt);
370 mutex_unlock(&data->lock);
371
372 return count;
373}
374
375static ssize_t show_volt_max(struct device *dev,
376 struct device_attribute *devattr,
377 char *buf)
378{
379 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
380 struct adt7473_data *data = adt7473_update_device(dev);
381 return sprintf(buf, "%d\n",
382 decode_volt(attr->index, data->volt_max[attr->index]));
383}
384
385static ssize_t set_volt_max(struct device *dev,
386 struct device_attribute *devattr,
387 const char *buf,
388 size_t count)
389{
390 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
391 struct i2c_client *client = to_i2c_client(dev);
392 struct adt7473_data *data = i2c_get_clientdata(client);
Darrick J. Wong862343c2008-11-12 13:26:58 -0800393 long volt;
394
395 if (strict_strtol(buf, 10, &volt))
396 return -EINVAL;
397
398 volt = encode_volt(attr->index, volt);
Darrick J. Wong57df46d2008-02-18 13:33:23 -0800399
400 mutex_lock(&data->lock);
401 data->volt_max[attr->index] = volt;
402 i2c_smbus_write_byte_data(client, ADT7473_REG_VOLT_MAX(attr->index),
403 volt);
404 mutex_unlock(&data->lock);
405
406 return count;
407}
408
409static ssize_t show_volt(struct device *dev, struct device_attribute *devattr,
410 char *buf)
411{
412 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
413 struct adt7473_data *data = adt7473_update_device(dev);
414
415 return sprintf(buf, "%d\n",
416 decode_volt(attr->index, data->volt[attr->index]));
417}
418
419/*
420 * This chip can report temperature data either as a two's complement
421 * number in the range -128 to 127, or as an unsigned number that must
422 * be offset by 64.
423 */
Mark M. Hoffman18524482008-03-06 08:41:09 -0500424static int decode_temp(u8 twos_complement, u8 raw)
Darrick J. Wong57df46d2008-02-18 13:33:23 -0800425{
Mark M. Hoffman18524482008-03-06 08:41:09 -0500426 return twos_complement ? (s8)raw : raw - 64;
Darrick J. Wong57df46d2008-02-18 13:33:23 -0800427}
428
Mark M. Hoffman18524482008-03-06 08:41:09 -0500429static u8 encode_temp(u8 twos_complement, int cooked)
Darrick J. Wong57df46d2008-02-18 13:33:23 -0800430{
Darrick J. Wong862343c2008-11-12 13:26:58 -0800431 u8 ret = twos_complement ? cooked & 0xFF : cooked + 64;
432 return SENSORS_LIMIT(ret, 0, 255);
Darrick J. Wong57df46d2008-02-18 13:33:23 -0800433}
434
435static ssize_t show_temp_min(struct device *dev,
436 struct device_attribute *devattr,
437 char *buf)
438{
439 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
440 struct adt7473_data *data = adt7473_update_device(dev);
Mark M. Hoffman18524482008-03-06 08:41:09 -0500441 return sprintf(buf, "%d\n", 1000 * decode_temp(
442 data->temp_twos_complement,
443 data->temp_min[attr->index]));
Darrick J. Wong57df46d2008-02-18 13:33:23 -0800444}
445
446static ssize_t set_temp_min(struct device *dev,
447 struct device_attribute *devattr,
448 const char *buf,
449 size_t count)
450{
451 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
452 struct i2c_client *client = to_i2c_client(dev);
453 struct adt7473_data *data = i2c_get_clientdata(client);
Darrick J. Wong862343c2008-11-12 13:26:58 -0800454 long temp;
455
456 if (strict_strtol(buf, 10, &temp))
457 return -EINVAL;
458
Darrick J. Wong8f8c1fb2009-01-06 14:41:31 -0800459 temp = DIV_ROUND_CLOSEST(temp, 1000);
Mark M. Hoffman18524482008-03-06 08:41:09 -0500460 temp = encode_temp(data->temp_twos_complement, temp);
Darrick J. Wong57df46d2008-02-18 13:33:23 -0800461
462 mutex_lock(&data->lock);
463 data->temp_min[attr->index] = temp;
464 i2c_smbus_write_byte_data(client, ADT7473_REG_TEMP_MIN(attr->index),
465 temp);
466 mutex_unlock(&data->lock);
467
468 return count;
469}
470
471static ssize_t show_temp_max(struct device *dev,
472 struct device_attribute *devattr,
473 char *buf)
474{
475 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
476 struct adt7473_data *data = adt7473_update_device(dev);
Mark M. Hoffman18524482008-03-06 08:41:09 -0500477 return sprintf(buf, "%d\n", 1000 * decode_temp(
478 data->temp_twos_complement,
479 data->temp_max[attr->index]));
Darrick J. Wong57df46d2008-02-18 13:33:23 -0800480}
481
482static ssize_t set_temp_max(struct device *dev,
483 struct device_attribute *devattr,
484 const char *buf,
485 size_t count)
486{
487 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
488 struct i2c_client *client = to_i2c_client(dev);
489 struct adt7473_data *data = i2c_get_clientdata(client);
Darrick J. Wong862343c2008-11-12 13:26:58 -0800490 long temp;
491
492 if (strict_strtol(buf, 10, &temp))
493 return -EINVAL;
494
Darrick J. Wong8f8c1fb2009-01-06 14:41:31 -0800495 temp = DIV_ROUND_CLOSEST(temp, 1000);
Mark M. Hoffman18524482008-03-06 08:41:09 -0500496 temp = encode_temp(data->temp_twos_complement, temp);
Darrick J. Wong57df46d2008-02-18 13:33:23 -0800497
498 mutex_lock(&data->lock);
499 data->temp_max[attr->index] = temp;
500 i2c_smbus_write_byte_data(client, ADT7473_REG_TEMP_MAX(attr->index),
501 temp);
502 mutex_unlock(&data->lock);
503
504 return count;
505}
506
507static ssize_t show_temp(struct device *dev, struct device_attribute *devattr,
508 char *buf)
509{
510 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
511 struct adt7473_data *data = adt7473_update_device(dev);
Mark M. Hoffman18524482008-03-06 08:41:09 -0500512 return sprintf(buf, "%d\n", 1000 * decode_temp(
513 data->temp_twos_complement,
514 data->temp[attr->index]));
Darrick J. Wong57df46d2008-02-18 13:33:23 -0800515}
516
517static ssize_t show_fan_min(struct device *dev,
518 struct device_attribute *devattr,
519 char *buf)
520{
521 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
522 struct adt7473_data *data = adt7473_update_device(dev);
523
524 if (FAN_DATA_VALID(data->fan_min[attr->index]))
525 return sprintf(buf, "%d\n",
526 FAN_PERIOD_TO_RPM(data->fan_min[attr->index]));
527 else
528 return sprintf(buf, "0\n");
529}
530
531static ssize_t set_fan_min(struct device *dev,
532 struct device_attribute *devattr,
533 const char *buf, size_t count)
534{
535 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
536 struct i2c_client *client = to_i2c_client(dev);
537 struct adt7473_data *data = i2c_get_clientdata(client);
Darrick J. Wong862343c2008-11-12 13:26:58 -0800538 long temp;
Darrick J. Wong57df46d2008-02-18 13:33:23 -0800539
Darrick J. Wong862343c2008-11-12 13:26:58 -0800540 if (strict_strtol(buf, 10, &temp) || !temp)
Darrick J. Wong57df46d2008-02-18 13:33:23 -0800541 return -EINVAL;
Darrick J. Wong862343c2008-11-12 13:26:58 -0800542
Darrick J. Wong57df46d2008-02-18 13:33:23 -0800543 temp = FAN_RPM_TO_PERIOD(temp);
Darrick J. Wong862343c2008-11-12 13:26:58 -0800544 temp = SENSORS_LIMIT(temp, 1, 65534);
Darrick J. Wong57df46d2008-02-18 13:33:23 -0800545
546 mutex_lock(&data->lock);
547 data->fan_min[attr->index] = temp;
548 adt7473_write_word_data(client, ADT7473_REG_FAN_MIN(attr->index), temp);
549 mutex_unlock(&data->lock);
550
551 return count;
552}
553
554static ssize_t show_fan(struct device *dev, struct device_attribute *devattr,
555 char *buf)
556{
557 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
558 struct adt7473_data *data = adt7473_update_device(dev);
559
560 if (FAN_DATA_VALID(data->fan[attr->index]))
561 return sprintf(buf, "%d\n",
562 FAN_PERIOD_TO_RPM(data->fan[attr->index]));
563 else
564 return sprintf(buf, "0\n");
565}
566
567static ssize_t show_max_duty_at_crit(struct device *dev,
568 struct device_attribute *devattr,
569 char *buf)
570{
571 struct adt7473_data *data = adt7473_update_device(dev);
572 return sprintf(buf, "%d\n", data->max_duty_at_overheat);
573}
574
575static ssize_t set_max_duty_at_crit(struct device *dev,
576 struct device_attribute *devattr,
577 const char *buf,
578 size_t count)
579{
580 u8 reg;
581 struct i2c_client *client = to_i2c_client(dev);
582 struct adt7473_data *data = i2c_get_clientdata(client);
Darrick J. Wong862343c2008-11-12 13:26:58 -0800583 long temp;
584
585 if (strict_strtol(buf, 10, &temp))
586 return -EINVAL;
Darrick J. Wong57df46d2008-02-18 13:33:23 -0800587
588 mutex_lock(&data->lock);
Mark M. Hoffman321c4132008-05-26 15:09:36 -0400589 data->max_duty_at_overheat = !!temp;
Darrick J. Wong57df46d2008-02-18 13:33:23 -0800590 reg = i2c_smbus_read_byte_data(client, ADT7473_REG_CFG4);
591 if (temp)
592 reg |= ADT7473_CFG4_MAX_DUTY_AT_OVT;
593 else
594 reg &= ~ADT7473_CFG4_MAX_DUTY_AT_OVT;
595 i2c_smbus_write_byte_data(client, ADT7473_REG_CFG4, reg);
596 mutex_unlock(&data->lock);
597
598 return count;
599}
600
601static ssize_t show_pwm(struct device *dev, struct device_attribute *devattr,
602 char *buf)
603{
604 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
605 struct adt7473_data *data = adt7473_update_device(dev);
606 return sprintf(buf, "%d\n", data->pwm[attr->index]);
607}
608
609static ssize_t set_pwm(struct device *dev, struct device_attribute *devattr,
610 const char *buf, size_t count)
611{
612 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
613 struct i2c_client *client = to_i2c_client(dev);
614 struct adt7473_data *data = i2c_get_clientdata(client);
Darrick J. Wong862343c2008-11-12 13:26:58 -0800615 long temp;
616
617 if (strict_strtol(buf, 10, &temp))
618 return -EINVAL;
619
620 temp = SENSORS_LIMIT(temp, 0, 255);
Darrick J. Wong57df46d2008-02-18 13:33:23 -0800621
622 mutex_lock(&data->lock);
623 data->pwm[attr->index] = temp;
624 i2c_smbus_write_byte_data(client, ADT7473_REG_PWM(attr->index), temp);
625 mutex_unlock(&data->lock);
626
627 return count;
628}
629
630static ssize_t show_pwm_max(struct device *dev,
631 struct device_attribute *devattr,
632 char *buf)
633{
634 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
635 struct adt7473_data *data = adt7473_update_device(dev);
636 return sprintf(buf, "%d\n", data->pwm_max[attr->index]);
637}
638
639static ssize_t set_pwm_max(struct device *dev,
640 struct device_attribute *devattr,
641 const char *buf,
642 size_t count)
643{
644 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
645 struct i2c_client *client = to_i2c_client(dev);
646 struct adt7473_data *data = i2c_get_clientdata(client);
Darrick J. Wong862343c2008-11-12 13:26:58 -0800647 long temp;
648
649 if (strict_strtol(buf, 10, &temp))
650 return -EINVAL;
651
652 temp = SENSORS_LIMIT(temp, 0, 255);
Darrick J. Wong57df46d2008-02-18 13:33:23 -0800653
654 mutex_lock(&data->lock);
655 data->pwm_max[attr->index] = temp;
656 i2c_smbus_write_byte_data(client, ADT7473_REG_PWM_MAX(attr->index),
657 temp);
658 mutex_unlock(&data->lock);
659
660 return count;
661}
662
663static ssize_t show_pwm_min(struct device *dev,
664 struct device_attribute *devattr,
665 char *buf)
666{
667 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
668 struct adt7473_data *data = adt7473_update_device(dev);
669 return sprintf(buf, "%d\n", data->pwm_min[attr->index]);
670}
671
672static ssize_t set_pwm_min(struct device *dev,
673 struct device_attribute *devattr,
674 const char *buf,
675 size_t count)
676{
677 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
678 struct i2c_client *client = to_i2c_client(dev);
679 struct adt7473_data *data = i2c_get_clientdata(client);
Darrick J. Wong862343c2008-11-12 13:26:58 -0800680 long temp;
681
682 if (strict_strtol(buf, 10, &temp))
683 return -EINVAL;
684
685 temp = SENSORS_LIMIT(temp, 0, 255);
Darrick J. Wong57df46d2008-02-18 13:33:23 -0800686
687 mutex_lock(&data->lock);
688 data->pwm_min[attr->index] = temp;
689 i2c_smbus_write_byte_data(client, ADT7473_REG_PWM_MIN(attr->index),
690 temp);
691 mutex_unlock(&data->lock);
692
693 return count;
694}
695
696static ssize_t show_temp_tmax(struct device *dev,
697 struct device_attribute *devattr,
698 char *buf)
699{
700 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
701 struct adt7473_data *data = adt7473_update_device(dev);
Mark M. Hoffman18524482008-03-06 08:41:09 -0500702 return sprintf(buf, "%d\n", 1000 * decode_temp(
703 data->temp_twos_complement,
704 data->temp_tmax[attr->index]));
Darrick J. Wong57df46d2008-02-18 13:33:23 -0800705}
706
707static ssize_t set_temp_tmax(struct device *dev,
708 struct device_attribute *devattr,
709 const char *buf,
710 size_t count)
711{
712 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
713 struct i2c_client *client = to_i2c_client(dev);
714 struct adt7473_data *data = i2c_get_clientdata(client);
Darrick J. Wong862343c2008-11-12 13:26:58 -0800715 long temp;
716
717 if (strict_strtol(buf, 10, &temp))
718 return -EINVAL;
719
Darrick J. Wong8f8c1fb2009-01-06 14:41:31 -0800720 temp = DIV_ROUND_CLOSEST(temp, 1000);
Mark M. Hoffman18524482008-03-06 08:41:09 -0500721 temp = encode_temp(data->temp_twos_complement, temp);
Darrick J. Wong57df46d2008-02-18 13:33:23 -0800722
723 mutex_lock(&data->lock);
724 data->temp_tmax[attr->index] = temp;
725 i2c_smbus_write_byte_data(client, ADT7473_REG_TEMP_TMAX(attr->index),
726 temp);
727 mutex_unlock(&data->lock);
728
729 return count;
730}
731
732static ssize_t show_temp_tmin(struct device *dev,
733 struct device_attribute *devattr,
734 char *buf)
735{
736 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
737 struct adt7473_data *data = adt7473_update_device(dev);
Mark M. Hoffman18524482008-03-06 08:41:09 -0500738 return sprintf(buf, "%d\n", 1000 * decode_temp(
739 data->temp_twos_complement,
740 data->temp_tmin[attr->index]));
Darrick J. Wong57df46d2008-02-18 13:33:23 -0800741}
742
743static ssize_t set_temp_tmin(struct device *dev,
744 struct device_attribute *devattr,
745 const char *buf,
746 size_t count)
747{
748 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
749 struct i2c_client *client = to_i2c_client(dev);
750 struct adt7473_data *data = i2c_get_clientdata(client);
Darrick J. Wong862343c2008-11-12 13:26:58 -0800751 long temp;
752
753 if (strict_strtol(buf, 10, &temp))
754 return -EINVAL;
755
Darrick J. Wong8f8c1fb2009-01-06 14:41:31 -0800756 temp = DIV_ROUND_CLOSEST(temp, 1000);
Mark M. Hoffman18524482008-03-06 08:41:09 -0500757 temp = encode_temp(data->temp_twos_complement, temp);
Darrick J. Wong57df46d2008-02-18 13:33:23 -0800758
759 mutex_lock(&data->lock);
760 data->temp_tmin[attr->index] = temp;
761 i2c_smbus_write_byte_data(client, ADT7473_REG_TEMP_TMIN(attr->index),
762 temp);
763 mutex_unlock(&data->lock);
764
765 return count;
766}
767
768static ssize_t show_pwm_enable(struct device *dev,
769 struct device_attribute *devattr,
770 char *buf)
771{
772 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
773 struct adt7473_data *data = adt7473_update_device(dev);
774
775 switch (data->pwm_behavior[attr->index] >> ADT7473_PWM_BHVR_SHIFT) {
776 case 3:
777 return sprintf(buf, "0\n");
778 case 7:
779 return sprintf(buf, "1\n");
780 default:
781 return sprintf(buf, "2\n");
782 }
783}
784
785static ssize_t set_pwm_enable(struct device *dev,
786 struct device_attribute *devattr,
787 const char *buf,
788 size_t count)
789{
790 u8 reg;
791 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
792 struct i2c_client *client = to_i2c_client(dev);
793 struct adt7473_data *data = i2c_get_clientdata(client);
Darrick J. Wong862343c2008-11-12 13:26:58 -0800794 long temp;
795
796 if (strict_strtol(buf, 10, &temp))
797 return -EINVAL;
Darrick J. Wong57df46d2008-02-18 13:33:23 -0800798
799 switch (temp) {
800 case 0:
801 temp = 3;
802 break;
803 case 1:
804 temp = 7;
805 break;
806 case 2:
807 /* Enter automatic mode with fans off */
808 temp = 4;
809 break;
810 default:
811 return -EINVAL;
812 }
813
814 mutex_lock(&data->lock);
815 reg = i2c_smbus_read_byte_data(client,
816 ADT7473_REG_PWM_BHVR(attr->index));
817 reg = (temp << ADT7473_PWM_BHVR_SHIFT) |
818 (reg & ~ADT7473_PWM_BHVR_MASK);
819 i2c_smbus_write_byte_data(client, ADT7473_REG_PWM_BHVR(attr->index),
820 reg);
821 data->pwm_behavior[attr->index] = reg;
822 mutex_unlock(&data->lock);
823
824 return count;
825}
826
827static ssize_t show_pwm_auto_temp(struct device *dev,
828 struct device_attribute *devattr,
829 char *buf)
830{
831 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
832 struct adt7473_data *data = adt7473_update_device(dev);
833 int bhvr = data->pwm_behavior[attr->index] >> ADT7473_PWM_BHVR_SHIFT;
834
835 switch (bhvr) {
836 case 3:
837 case 4:
838 case 7:
839 return sprintf(buf, "0\n");
840 case 0:
841 case 1:
842 case 5:
843 case 6:
844 return sprintf(buf, "%d\n", bhvr + 1);
845 case 2:
846 return sprintf(buf, "4\n");
847 }
848 /* shouldn't ever get here */
849 BUG();
850}
851
852static ssize_t set_pwm_auto_temp(struct device *dev,
853 struct device_attribute *devattr,
854 const char *buf,
855 size_t count)
856{
857 u8 reg;
858 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
859 struct i2c_client *client = to_i2c_client(dev);
860 struct adt7473_data *data = i2c_get_clientdata(client);
Darrick J. Wong862343c2008-11-12 13:26:58 -0800861 long temp;
862
863 if (strict_strtol(buf, 10, &temp))
864 return -EINVAL;
Darrick J. Wong57df46d2008-02-18 13:33:23 -0800865
866 switch (temp) {
867 case 1:
868 case 2:
869 case 6:
870 case 7:
871 temp--;
872 break;
873 case 0:
874 temp = 4;
875 break;
876 default:
877 return -EINVAL;
878 }
879
880 mutex_lock(&data->lock);
881 reg = i2c_smbus_read_byte_data(client,
882 ADT7473_REG_PWM_BHVR(attr->index));
883 reg = (temp << ADT7473_PWM_BHVR_SHIFT) |
884 (reg & ~ADT7473_PWM_BHVR_MASK);
885 i2c_smbus_write_byte_data(client, ADT7473_REG_PWM_BHVR(attr->index),
886 reg);
887 data->pwm_behavior[attr->index] = reg;
888 mutex_unlock(&data->lock);
889
890 return count;
891}
892
893static ssize_t show_alarm(struct device *dev,
894 struct device_attribute *devattr,
895 char *buf)
896{
897 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
898 struct adt7473_data *data = adt7473_update_device(dev);
899
900 if (data->alarm & attr->index)
901 return sprintf(buf, "1\n");
902 else
903 return sprintf(buf, "0\n");
904}
905
906
907static SENSOR_DEVICE_ATTR(in1_max, S_IWUSR | S_IRUGO, show_volt_max,
908 set_volt_max, 0);
909static SENSOR_DEVICE_ATTR(in2_max, S_IWUSR | S_IRUGO, show_volt_max,
910 set_volt_max, 1);
911
912static SENSOR_DEVICE_ATTR(in1_min, S_IWUSR | S_IRUGO, show_volt_min,
913 set_volt_min, 0);
914static SENSOR_DEVICE_ATTR(in2_min, S_IWUSR | S_IRUGO, show_volt_min,
915 set_volt_min, 1);
916
917static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, show_volt, NULL, 0);
918static SENSOR_DEVICE_ATTR(in2_input, S_IRUGO, show_volt, NULL, 1);
919
920static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL,
921 ADT7473_VCCP_ALARM);
922static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL,
923 ADT7473_VCC_ALARM);
924
925static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp_max,
926 set_temp_max, 0);
927static SENSOR_DEVICE_ATTR(temp2_max, S_IWUSR | S_IRUGO, show_temp_max,
928 set_temp_max, 1);
929static SENSOR_DEVICE_ATTR(temp3_max, S_IWUSR | S_IRUGO, show_temp_max,
930 set_temp_max, 2);
931
932static SENSOR_DEVICE_ATTR(temp1_min, S_IWUSR | S_IRUGO, show_temp_min,
933 set_temp_min, 0);
934static SENSOR_DEVICE_ATTR(temp2_min, S_IWUSR | S_IRUGO, show_temp_min,
935 set_temp_min, 1);
936static SENSOR_DEVICE_ATTR(temp3_min, S_IWUSR | S_IRUGO, show_temp_min,
937 set_temp_min, 2);
938
939static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0);
940static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp, NULL, 1);
941static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO, show_temp, NULL, 2);
942
943static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL,
944 ADT7473_R1T_ALARM | ALARM2(ADT7473_R1T_SHORT));
945static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL,
946 ADT7473_LT_ALARM);
947static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL,
948 ADT7473_R2T_ALARM | ALARM2(ADT7473_R2T_SHORT));
949
950static SENSOR_DEVICE_ATTR(fan1_min, S_IWUSR | S_IRUGO, show_fan_min,
951 set_fan_min, 0);
952static SENSOR_DEVICE_ATTR(fan2_min, S_IWUSR | S_IRUGO, show_fan_min,
953 set_fan_min, 1);
954static SENSOR_DEVICE_ATTR(fan3_min, S_IWUSR | S_IRUGO, show_fan_min,
955 set_fan_min, 2);
956static SENSOR_DEVICE_ATTR(fan4_min, S_IWUSR | S_IRUGO, show_fan_min,
957 set_fan_min, 3);
958
959static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, show_fan, NULL, 0);
960static SENSOR_DEVICE_ATTR(fan2_input, S_IRUGO, show_fan, NULL, 1);
961static SENSOR_DEVICE_ATTR(fan3_input, S_IRUGO, show_fan, NULL, 2);
962static SENSOR_DEVICE_ATTR(fan4_input, S_IRUGO, show_fan, NULL, 3);
963
964static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL,
965 ALARM2(ADT7473_FAN1_ALARM));
966static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL,
967 ALARM2(ADT7473_FAN2_ALARM));
968static SENSOR_DEVICE_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL,
969 ALARM2(ADT7473_FAN3_ALARM));
970static SENSOR_DEVICE_ATTR(fan4_alarm, S_IRUGO, show_alarm, NULL,
971 ALARM2(ADT7473_FAN4_ALARM));
972
973static SENSOR_DEVICE_ATTR(pwm_use_point2_pwm_at_crit, S_IWUSR | S_IRUGO,
974 show_max_duty_at_crit, set_max_duty_at_crit, 0);
975
976static SENSOR_DEVICE_ATTR(pwm1, S_IWUSR | S_IRUGO, show_pwm, set_pwm, 0);
977static SENSOR_DEVICE_ATTR(pwm2, S_IWUSR | S_IRUGO, show_pwm, set_pwm, 1);
978static SENSOR_DEVICE_ATTR(pwm3, S_IWUSR | S_IRUGO, show_pwm, set_pwm, 2);
979
980static SENSOR_DEVICE_ATTR(pwm1_auto_point1_pwm, S_IWUSR | S_IRUGO,
981 show_pwm_min, set_pwm_min, 0);
982static SENSOR_DEVICE_ATTR(pwm2_auto_point1_pwm, S_IWUSR | S_IRUGO,
983 show_pwm_min, set_pwm_min, 1);
984static SENSOR_DEVICE_ATTR(pwm3_auto_point1_pwm, S_IWUSR | S_IRUGO,
985 show_pwm_min, set_pwm_min, 2);
986
987static SENSOR_DEVICE_ATTR(pwm1_auto_point2_pwm, S_IWUSR | S_IRUGO,
988 show_pwm_max, set_pwm_max, 0);
989static SENSOR_DEVICE_ATTR(pwm2_auto_point2_pwm, S_IWUSR | S_IRUGO,
990 show_pwm_max, set_pwm_max, 1);
991static SENSOR_DEVICE_ATTR(pwm3_auto_point2_pwm, S_IWUSR | S_IRUGO,
992 show_pwm_max, set_pwm_max, 2);
993
994static SENSOR_DEVICE_ATTR(temp1_auto_point1_temp, S_IWUSR | S_IRUGO,
995 show_temp_tmin, set_temp_tmin, 0);
996static SENSOR_DEVICE_ATTR(temp2_auto_point1_temp, S_IWUSR | S_IRUGO,
997 show_temp_tmin, set_temp_tmin, 1);
998static SENSOR_DEVICE_ATTR(temp3_auto_point1_temp, S_IWUSR | S_IRUGO,
999 show_temp_tmin, set_temp_tmin, 2);
1000
1001static SENSOR_DEVICE_ATTR(temp1_auto_point2_temp, S_IWUSR | S_IRUGO,
1002 show_temp_tmax, set_temp_tmax, 0);
1003static SENSOR_DEVICE_ATTR(temp2_auto_point2_temp, S_IWUSR | S_IRUGO,
1004 show_temp_tmax, set_temp_tmax, 1);
1005static SENSOR_DEVICE_ATTR(temp3_auto_point2_temp, S_IWUSR | S_IRUGO,
1006 show_temp_tmax, set_temp_tmax, 2);
1007
1008static SENSOR_DEVICE_ATTR(pwm1_enable, S_IWUSR | S_IRUGO, show_pwm_enable,
1009 set_pwm_enable, 0);
1010static SENSOR_DEVICE_ATTR(pwm2_enable, S_IWUSR | S_IRUGO, show_pwm_enable,
1011 set_pwm_enable, 1);
1012static SENSOR_DEVICE_ATTR(pwm3_enable, S_IWUSR | S_IRUGO, show_pwm_enable,
1013 set_pwm_enable, 2);
1014
1015static SENSOR_DEVICE_ATTR(pwm1_auto_channels_temp, S_IWUSR | S_IRUGO,
1016 show_pwm_auto_temp, set_pwm_auto_temp, 0);
1017static SENSOR_DEVICE_ATTR(pwm2_auto_channels_temp, S_IWUSR | S_IRUGO,
1018 show_pwm_auto_temp, set_pwm_auto_temp, 1);
1019static SENSOR_DEVICE_ATTR(pwm3_auto_channels_temp, S_IWUSR | S_IRUGO,
1020 show_pwm_auto_temp, set_pwm_auto_temp, 2);
1021
1022static struct attribute *adt7473_attr[] =
1023{
1024 &sensor_dev_attr_in1_max.dev_attr.attr,
1025 &sensor_dev_attr_in2_max.dev_attr.attr,
1026 &sensor_dev_attr_in1_min.dev_attr.attr,
1027 &sensor_dev_attr_in2_min.dev_attr.attr,
1028 &sensor_dev_attr_in1_input.dev_attr.attr,
1029 &sensor_dev_attr_in2_input.dev_attr.attr,
1030 &sensor_dev_attr_in1_alarm.dev_attr.attr,
1031 &sensor_dev_attr_in2_alarm.dev_attr.attr,
1032
1033 &sensor_dev_attr_temp1_max.dev_attr.attr,
1034 &sensor_dev_attr_temp2_max.dev_attr.attr,
1035 &sensor_dev_attr_temp3_max.dev_attr.attr,
1036 &sensor_dev_attr_temp1_min.dev_attr.attr,
1037 &sensor_dev_attr_temp2_min.dev_attr.attr,
1038 &sensor_dev_attr_temp3_min.dev_attr.attr,
1039 &sensor_dev_attr_temp1_input.dev_attr.attr,
1040 &sensor_dev_attr_temp2_input.dev_attr.attr,
1041 &sensor_dev_attr_temp3_input.dev_attr.attr,
1042 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
1043 &sensor_dev_attr_temp2_alarm.dev_attr.attr,
1044 &sensor_dev_attr_temp3_alarm.dev_attr.attr,
1045 &sensor_dev_attr_temp1_auto_point1_temp.dev_attr.attr,
1046 &sensor_dev_attr_temp2_auto_point1_temp.dev_attr.attr,
1047 &sensor_dev_attr_temp3_auto_point1_temp.dev_attr.attr,
1048 &sensor_dev_attr_temp1_auto_point2_temp.dev_attr.attr,
1049 &sensor_dev_attr_temp2_auto_point2_temp.dev_attr.attr,
1050 &sensor_dev_attr_temp3_auto_point2_temp.dev_attr.attr,
1051
1052 &sensor_dev_attr_fan1_min.dev_attr.attr,
1053 &sensor_dev_attr_fan2_min.dev_attr.attr,
1054 &sensor_dev_attr_fan3_min.dev_attr.attr,
1055 &sensor_dev_attr_fan4_min.dev_attr.attr,
1056 &sensor_dev_attr_fan1_input.dev_attr.attr,
1057 &sensor_dev_attr_fan2_input.dev_attr.attr,
1058 &sensor_dev_attr_fan3_input.dev_attr.attr,
1059 &sensor_dev_attr_fan4_input.dev_attr.attr,
1060 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
1061 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
1062 &sensor_dev_attr_fan3_alarm.dev_attr.attr,
1063 &sensor_dev_attr_fan4_alarm.dev_attr.attr,
1064
1065 &sensor_dev_attr_pwm_use_point2_pwm_at_crit.dev_attr.attr,
1066
1067 &sensor_dev_attr_pwm1.dev_attr.attr,
1068 &sensor_dev_attr_pwm2.dev_attr.attr,
1069 &sensor_dev_attr_pwm3.dev_attr.attr,
1070 &sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr,
1071 &sensor_dev_attr_pwm2_auto_point1_pwm.dev_attr.attr,
1072 &sensor_dev_attr_pwm3_auto_point1_pwm.dev_attr.attr,
1073 &sensor_dev_attr_pwm1_auto_point2_pwm.dev_attr.attr,
1074 &sensor_dev_attr_pwm2_auto_point2_pwm.dev_attr.attr,
1075 &sensor_dev_attr_pwm3_auto_point2_pwm.dev_attr.attr,
1076
1077 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
1078 &sensor_dev_attr_pwm2_enable.dev_attr.attr,
1079 &sensor_dev_attr_pwm3_enable.dev_attr.attr,
1080 &sensor_dev_attr_pwm1_auto_channels_temp.dev_attr.attr,
1081 &sensor_dev_attr_pwm2_auto_channels_temp.dev_attr.attr,
1082 &sensor_dev_attr_pwm3_auto_channels_temp.dev_attr.attr,
1083
1084 NULL
1085};
1086
Jean Delvareeea54762008-07-16 19:30:10 +02001087/* Return 0 if detection is successful, -ENODEV otherwise */
Jean Delvare310ec792009-12-14 21:17:23 +01001088static int adt7473_detect(struct i2c_client *client,
Jean Delvareeea54762008-07-16 19:30:10 +02001089 struct i2c_board_info *info)
Darrick J. Wong57df46d2008-02-18 13:33:23 -08001090{
Jean Delvareeea54762008-07-16 19:30:10 +02001091 struct i2c_adapter *adapter = client->adapter;
Jean Delvare52df6442009-12-09 20:35:57 +01001092 int vendor, device, revision;
Darrick J. Wong57df46d2008-02-18 13:33:23 -08001093
1094 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
Jean Delvareeea54762008-07-16 19:30:10 +02001095 return -ENODEV;
1096
Jean Delvare52df6442009-12-09 20:35:57 +01001097 vendor = i2c_smbus_read_byte_data(client, ADT7473_REG_VENDOR);
1098 if (vendor != ADT7473_VENDOR)
1099 return -ENODEV;
Jean Delvareeea54762008-07-16 19:30:10 +02001100
Jean Delvare52df6442009-12-09 20:35:57 +01001101 device = i2c_smbus_read_byte_data(client, ADT7473_REG_DEVICE);
1102 if (device != ADT7473_DEVICE)
1103 return -ENODEV;
Jean Delvareeea54762008-07-16 19:30:10 +02001104
Jean Delvare52df6442009-12-09 20:35:57 +01001105 revision = i2c_smbus_read_byte_data(client, ADT7473_REG_REVISION);
1106 if (revision != ADT7473_REV_68 && revision != ADT7473_REV_69)
1107 return -ENODEV;
Jean Delvareeea54762008-07-16 19:30:10 +02001108
1109 strlcpy(info->type, "adt7473", I2C_NAME_SIZE);
1110
1111 return 0;
1112}
1113
1114static int adt7473_probe(struct i2c_client *client,
1115 const struct i2c_device_id *id)
1116{
1117 struct adt7473_data *data;
1118 int err;
Darrick J. Wong57df46d2008-02-18 13:33:23 -08001119
1120 data = kzalloc(sizeof(struct adt7473_data), GFP_KERNEL);
1121 if (!data) {
1122 err = -ENOMEM;
1123 goto exit;
1124 }
1125
Darrick J. Wong57df46d2008-02-18 13:33:23 -08001126 i2c_set_clientdata(client, data);
Darrick J. Wong57df46d2008-02-18 13:33:23 -08001127 mutex_init(&data->lock);
1128
Darrick J. Wong57df46d2008-02-18 13:33:23 -08001129 dev_info(&client->dev, "%s chip found\n", client->name);
1130
1131 /* Initialize the ADT7473 chip */
1132 adt7473_init_client(client);
1133
1134 /* Register sysfs hooks */
1135 data->attrs.attrs = adt7473_attr;
1136 err = sysfs_create_group(&client->dev.kobj, &data->attrs);
1137 if (err)
Jean Delvareeea54762008-07-16 19:30:10 +02001138 goto exit_free;
Darrick J. Wong57df46d2008-02-18 13:33:23 -08001139
1140 data->hwmon_dev = hwmon_device_register(&client->dev);
1141 if (IS_ERR(data->hwmon_dev)) {
1142 err = PTR_ERR(data->hwmon_dev);
1143 goto exit_remove;
1144 }
1145
1146 return 0;
1147
1148exit_remove:
1149 sysfs_remove_group(&client->dev.kobj, &data->attrs);
Darrick J. Wong57df46d2008-02-18 13:33:23 -08001150exit_free:
1151 kfree(data);
1152exit:
1153 return err;
1154}
1155
Jean Delvareeea54762008-07-16 19:30:10 +02001156static int adt7473_remove(struct i2c_client *client)
Darrick J. Wong57df46d2008-02-18 13:33:23 -08001157{
1158 struct adt7473_data *data = i2c_get_clientdata(client);
1159
1160 hwmon_device_unregister(data->hwmon_dev);
1161 sysfs_remove_group(&client->dev.kobj, &data->attrs);
Darrick J. Wong57df46d2008-02-18 13:33:23 -08001162 kfree(data);
1163 return 0;
1164}
1165
1166static int __init adt7473_init(void)
1167{
Jean Delvareb180d052009-12-09 20:36:02 +01001168 pr_notice("The adt7473 driver is deprecated, please use the adt7475 "
1169 "driver instead\n");
Darrick J. Wong57df46d2008-02-18 13:33:23 -08001170 return i2c_add_driver(&adt7473_driver);
1171}
1172
1173static void __exit adt7473_exit(void)
1174{
1175 i2c_del_driver(&adt7473_driver);
1176}
1177
1178MODULE_AUTHOR("Darrick J. Wong <djwong@us.ibm.com>");
1179MODULE_DESCRIPTION("ADT7473 driver");
1180MODULE_LICENSE("GPL");
1181
1182module_init(adt7473_init);
1183module_exit(adt7473_exit);