blob: c3c2865a89671d34ab87211267e97789190014d3 [file] [log] [blame]
Grant Coady40b5cda2005-04-30 21:41:29 +10001/*
2 * adm9240.c Part of lm_sensors, Linux kernel modules for hardware
Guenter Roeckc387e4e2012-01-14 13:12:38 -08003 * monitoring
Grant Coady40b5cda2005-04-30 21:41:29 +10004 *
5 * Copyright (C) 1999 Frodo Looijaard <frodol@dds.nl>
6 * Philip Edelbrock <phil@netroedge.com>
7 * Copyright (C) 2003 Michiel Rook <michiel@grendelproject.nl>
Grant Coady2ca7b962006-10-08 21:57:41 +02008 * Copyright (C) 2005 Grant Coady <gcoady.lk@gmail.com> with valuable
Guenter Roeckc387e4e2012-01-14 13:12:38 -08009 * guidance from Jean Delvare
Grant Coady40b5cda2005-04-30 21:41:29 +100010 *
11 * Driver supports Analog Devices ADM9240
12 * Dallas Semiconductor DS1780
13 * National Semiconductor LM81
14 *
15 * ADM9240 is the reference, DS1780 and LM81 are register compatibles
16 *
17 * Voltage Six inputs are scaled by chip, VID also reported
18 * Temperature Chip temperature to 0.5'C, maximum and max_hysteris
19 * Fans 2 fans, low speed alarm, automatic fan clock divider
20 * Alarms 16-bit map of active alarms
21 * Analog Out 0..1250 mV output
22 *
Jean Delvare0de2b242011-01-12 21:55:09 +010023 * Chassis Intrusion: clear CI latch with 'echo 0 > intrusion0_alarm'
Grant Coady40b5cda2005-04-30 21:41:29 +100024 *
25 * Test hardware: Intel SE440BX-2 desktop motherboard --Grant
26 *
27 * LM81 extended temp reading not implemented
28 *
29 * This program is free software; you can redistribute it and/or modify
30 * it under the terms of the GNU General Public License as published by
31 * the Free Software Foundation; either version 2 of the License, or
32 * (at your option) any later version.
33 *
34 * This program is distributed in the hope that it will be useful,
35 * but WITHOUT ANY WARRANTY; without even the implied warranty of
36 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
37 * GNU General Public License for more details.
38 *
39 * You should have received a copy of the GNU General Public License
40 * along with this program; if not, write to the Free Software
41 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
42 */
43
44#include <linux/init.h>
45#include <linux/module.h>
46#include <linux/slab.h>
47#include <linux/i2c.h>
Grant Coadyc7461a62005-09-17 05:32:57 +100048#include <linux/hwmon-sysfs.h>
Mark M. Hoffman943b0832005-07-15 21:39:18 -040049#include <linux/hwmon.h>
Jean Delvare303760b2005-07-31 21:52:01 +020050#include <linux/hwmon-vid.h>
Mark M. Hoffman943b0832005-07-15 21:39:18 -040051#include <linux/err.h>
Ingo Molnar9a61bf62006-01-18 23:19:26 +010052#include <linux/mutex.h>
Grant Coady40b5cda2005-04-30 21:41:29 +100053
54/* Addresses to scan */
Mark M. Hoffman25e9c862008-02-17 22:28:03 -050055static const unsigned short normal_i2c[] = { 0x2c, 0x2d, 0x2e, 0x2f,
Grant Coady40b5cda2005-04-30 21:41:29 +100056 I2C_CLIENT_END };
57
Jean Delvaree5e9f442009-12-14 21:17:27 +010058enum chips { adm9240, ds1780, lm81 };
Grant Coady40b5cda2005-04-30 21:41:29 +100059
60/* ADM9240 registers */
61#define ADM9240_REG_MAN_ID 0x3e
62#define ADM9240_REG_DIE_REV 0x3f
63#define ADM9240_REG_CONFIG 0x40
64
65#define ADM9240_REG_IN(nr) (0x20 + (nr)) /* 0..5 */
66#define ADM9240_REG_IN_MAX(nr) (0x2b + (nr) * 2)
67#define ADM9240_REG_IN_MIN(nr) (0x2c + (nr) * 2)
68#define ADM9240_REG_FAN(nr) (0x28 + (nr)) /* 0..1 */
69#define ADM9240_REG_FAN_MIN(nr) (0x3b + (nr))
70#define ADM9240_REG_INT(nr) (0x41 + (nr))
71#define ADM9240_REG_INT_MASK(nr) (0x43 + (nr))
72#define ADM9240_REG_TEMP 0x27
Grant Coadyc7461a62005-09-17 05:32:57 +100073#define ADM9240_REG_TEMP_MAX(nr) (0x39 + (nr)) /* 0, 1 = high, hyst */
Grant Coady40b5cda2005-04-30 21:41:29 +100074#define ADM9240_REG_ANALOG_OUT 0x19
75#define ADM9240_REG_CHASSIS_CLEAR 0x46
76#define ADM9240_REG_VID_FAN_DIV 0x47
77#define ADM9240_REG_I2C_ADDR 0x48
78#define ADM9240_REG_VID4 0x49
79#define ADM9240_REG_TEMP_CONF 0x4b
80
81/* generalised scaling with integer rounding */
82static inline int SCALE(long val, int mul, int div)
83{
84 if (val < 0)
85 return (val * mul - div / 2) / div;
86 else
87 return (val * mul + div / 2) / div;
88}
89
90/* adm9240 internally scales voltage measurements */
91static const u16 nom_mv[] = { 2500, 2700, 3300, 5000, 12000, 2700 };
92
93static inline unsigned int IN_FROM_REG(u8 reg, int n)
94{
95 return SCALE(reg, nom_mv[n], 192);
96}
97
98static inline u8 IN_TO_REG(unsigned long val, int n)
99{
100 return SENSORS_LIMIT(SCALE(val, 192, nom_mv[n]), 0, 255);
101}
102
103/* temperature range: -40..125, 127 disables temperature alarm */
104static inline s8 TEMP_TO_REG(long val)
105{
106 return SENSORS_LIMIT(SCALE(val, 1, 1000), -40, 127);
107}
108
109/* two fans, each with low fan speed limit */
110static inline unsigned int FAN_FROM_REG(u8 reg, u8 div)
111{
112 if (!reg) /* error */
113 return -1;
114
115 if (reg == 255)
116 return 0;
117
118 return SCALE(1350000, 1, reg * div);
119}
120
121/* analog out 0..1250mV */
122static inline u8 AOUT_TO_REG(unsigned long val)
123{
124 return SENSORS_LIMIT(SCALE(val, 255, 1250), 0, 255);
125}
126
127static inline unsigned int AOUT_FROM_REG(u8 reg)
128{
129 return SCALE(reg, 1250, 255);
130}
131
Jean Delvare7fae8282008-07-16 19:30:09 +0200132static int adm9240_probe(struct i2c_client *client,
133 const struct i2c_device_id *id);
Jean Delvare310ec792009-12-14 21:17:23 +0100134static int adm9240_detect(struct i2c_client *client,
Jean Delvare7fae8282008-07-16 19:30:09 +0200135 struct i2c_board_info *info);
Grant Coady40b5cda2005-04-30 21:41:29 +1000136static void adm9240_init_client(struct i2c_client *client);
Jean Delvare7fae8282008-07-16 19:30:09 +0200137static int adm9240_remove(struct i2c_client *client);
Grant Coady40b5cda2005-04-30 21:41:29 +1000138static struct adm9240_data *adm9240_update_device(struct device *dev);
139
140/* driver data */
Jean Delvare7fae8282008-07-16 19:30:09 +0200141static const struct i2c_device_id adm9240_id[] = {
142 { "adm9240", adm9240 },
143 { "ds1780", ds1780 },
144 { "lm81", lm81 },
145 { }
146};
147MODULE_DEVICE_TABLE(i2c, adm9240_id);
148
Grant Coady40b5cda2005-04-30 21:41:29 +1000149static struct i2c_driver adm9240_driver = {
Jean Delvare7fae8282008-07-16 19:30:09 +0200150 .class = I2C_CLASS_HWMON,
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100151 .driver = {
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100152 .name = "adm9240",
153 },
Jean Delvare7fae8282008-07-16 19:30:09 +0200154 .probe = adm9240_probe,
155 .remove = adm9240_remove,
156 .id_table = adm9240_id,
157 .detect = adm9240_detect,
Jean Delvarec3813d62009-12-14 21:17:25 +0100158 .address_list = normal_i2c,
Grant Coady40b5cda2005-04-30 21:41:29 +1000159};
160
161/* per client data */
162struct adm9240_data {
Tony Jones1beeffe2007-08-20 13:46:20 -0700163 struct device *hwmon_dev;
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100164 struct mutex update_lock;
Grant Coady40b5cda2005-04-30 21:41:29 +1000165 char valid;
166 unsigned long last_updated_measure;
167 unsigned long last_updated_config;
168
169 u8 in[6]; /* ro in0_input */
170 u8 in_max[6]; /* rw in0_max */
171 u8 in_min[6]; /* rw in0_min */
172 u8 fan[2]; /* ro fan1_input */
173 u8 fan_min[2]; /* rw fan1_min */
174 u8 fan_div[2]; /* rw fan1_div, read-only accessor */
175 s16 temp; /* ro temp1_input, 9-bit sign-extended */
Grant Coadyc7461a62005-09-17 05:32:57 +1000176 s8 temp_max[2]; /* rw 0 -> temp_max, 1 -> temp_max_hyst */
Grant Coady40b5cda2005-04-30 21:41:29 +1000177 u16 alarms; /* ro alarms */
Grant Coady8e8f9282005-05-13 20:26:10 +1000178 u8 aout; /* rw aout_output */
Grant Coady40b5cda2005-04-30 21:41:29 +1000179 u8 vid; /* ro vid */
180 u8 vrm; /* -- vrm set on startup, no accessor */
181};
182
Grant Coady40b5cda2005-04-30 21:41:29 +1000183/*** sysfs accessors ***/
184
185/* temperature */
Grant Coadyc7461a62005-09-17 05:32:57 +1000186static ssize_t show_temp(struct device *dev, struct device_attribute *dummy,
187 char *buf)
188{
189 struct adm9240_data *data = adm9240_update_device(dev);
190 return sprintf(buf, "%d\n", data->temp * 500); /* 9-bit value */
Grant Coady40b5cda2005-04-30 21:41:29 +1000191}
192
Grant Coadyc7461a62005-09-17 05:32:57 +1000193static ssize_t show_max(struct device *dev, struct device_attribute *devattr,
194 char *buf)
195{
196 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
197 struct adm9240_data *data = adm9240_update_device(dev);
198 return sprintf(buf, "%d\n", data->temp_max[attr->index] * 1000);
199}
Grant Coady40b5cda2005-04-30 21:41:29 +1000200
Grant Coadyc7461a62005-09-17 05:32:57 +1000201static ssize_t set_max(struct device *dev, struct device_attribute *devattr,
202 const char *buf, size_t count)
203{
204 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
205 struct i2c_client *client = to_i2c_client(dev);
206 struct adm9240_data *data = i2c_get_clientdata(client);
Guenter Roeckc387e4e2012-01-14 13:12:38 -0800207 long val;
208 int err;
209
210 err = kstrtol(buf, 10, &val);
211 if (err)
212 return err;
Grant Coadyc7461a62005-09-17 05:32:57 +1000213
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100214 mutex_lock(&data->update_lock);
Grant Coadyc7461a62005-09-17 05:32:57 +1000215 data->temp_max[attr->index] = TEMP_TO_REG(val);
216 i2c_smbus_write_byte_data(client, ADM9240_REG_TEMP_MAX(attr->index),
217 data->temp_max[attr->index]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100218 mutex_unlock(&data->update_lock);
Grant Coadyc7461a62005-09-17 05:32:57 +1000219 return count;
220}
221
Grant Coady40b5cda2005-04-30 21:41:29 +1000222static DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL);
Grant Coadyc7461a62005-09-17 05:32:57 +1000223static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO,
224 show_max, set_max, 0);
225static SENSOR_DEVICE_ATTR(temp1_max_hyst, S_IWUSR | S_IRUGO,
226 show_max, set_max, 1);
Grant Coady40b5cda2005-04-30 21:41:29 +1000227
228/* voltage */
Grant Coadyc7461a62005-09-17 05:32:57 +1000229static ssize_t show_in(struct device *dev, struct device_attribute *devattr,
230 char *buf)
Grant Coady40b5cda2005-04-30 21:41:29 +1000231{
Grant Coadyc7461a62005-09-17 05:32:57 +1000232 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
Grant Coady40b5cda2005-04-30 21:41:29 +1000233 struct adm9240_data *data = adm9240_update_device(dev);
Grant Coadyc7461a62005-09-17 05:32:57 +1000234 return sprintf(buf, "%d\n", IN_FROM_REG(data->in[attr->index],
235 attr->index));
Grant Coady40b5cda2005-04-30 21:41:29 +1000236}
237
Jean Delvaree415e482005-09-25 16:14:18 +0200238static ssize_t show_in_min(struct device *dev,
239 struct device_attribute *devattr, char *buf)
Grant Coady40b5cda2005-04-30 21:41:29 +1000240{
Grant Coadyc7461a62005-09-17 05:32:57 +1000241 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
Grant Coady40b5cda2005-04-30 21:41:29 +1000242 struct adm9240_data *data = adm9240_update_device(dev);
Grant Coadyc7461a62005-09-17 05:32:57 +1000243 return sprintf(buf, "%d\n", IN_FROM_REG(data->in_min[attr->index],
244 attr->index));
Grant Coady40b5cda2005-04-30 21:41:29 +1000245}
246
Jean Delvaree415e482005-09-25 16:14:18 +0200247static ssize_t show_in_max(struct device *dev,
248 struct device_attribute *devattr, char *buf)
Grant Coady40b5cda2005-04-30 21:41:29 +1000249{
Grant Coadyc7461a62005-09-17 05:32:57 +1000250 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
Grant Coady40b5cda2005-04-30 21:41:29 +1000251 struct adm9240_data *data = adm9240_update_device(dev);
Grant Coadyc7461a62005-09-17 05:32:57 +1000252 return sprintf(buf, "%d\n", IN_FROM_REG(data->in_max[attr->index],
253 attr->index));
Grant Coady40b5cda2005-04-30 21:41:29 +1000254}
255
Jean Delvaree415e482005-09-25 16:14:18 +0200256static ssize_t set_in_min(struct device *dev,
257 struct device_attribute *devattr,
Grant Coadyc7461a62005-09-17 05:32:57 +1000258 const char *buf, size_t count)
Grant Coady40b5cda2005-04-30 21:41:29 +1000259{
Grant Coadyc7461a62005-09-17 05:32:57 +1000260 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
Grant Coady40b5cda2005-04-30 21:41:29 +1000261 struct i2c_client *client = to_i2c_client(dev);
262 struct adm9240_data *data = i2c_get_clientdata(client);
Guenter Roeckc387e4e2012-01-14 13:12:38 -0800263 unsigned long val;
264 int err;
265
266 err = kstrtoul(buf, 10, &val);
267 if (err)
268 return err;
Grant Coady40b5cda2005-04-30 21:41:29 +1000269
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100270 mutex_lock(&data->update_lock);
Grant Coadyc7461a62005-09-17 05:32:57 +1000271 data->in_min[attr->index] = IN_TO_REG(val, attr->index);
272 i2c_smbus_write_byte_data(client, ADM9240_REG_IN_MIN(attr->index),
273 data->in_min[attr->index]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100274 mutex_unlock(&data->update_lock);
Grant Coady40b5cda2005-04-30 21:41:29 +1000275 return count;
276}
277
Jean Delvaree415e482005-09-25 16:14:18 +0200278static ssize_t set_in_max(struct device *dev,
279 struct device_attribute *devattr,
Grant Coadyc7461a62005-09-17 05:32:57 +1000280 const char *buf, size_t count)
Grant Coady40b5cda2005-04-30 21:41:29 +1000281{
Grant Coadyc7461a62005-09-17 05:32:57 +1000282 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
Grant Coady40b5cda2005-04-30 21:41:29 +1000283 struct i2c_client *client = to_i2c_client(dev);
284 struct adm9240_data *data = i2c_get_clientdata(client);
Guenter Roeckc387e4e2012-01-14 13:12:38 -0800285 unsigned long val;
286 int err;
287
288 err = kstrtoul(buf, 10, &val);
289 if (err)
290 return err;
Grant Coady40b5cda2005-04-30 21:41:29 +1000291
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100292 mutex_lock(&data->update_lock);
Grant Coadyc7461a62005-09-17 05:32:57 +1000293 data->in_max[attr->index] = IN_TO_REG(val, attr->index);
294 i2c_smbus_write_byte_data(client, ADM9240_REG_IN_MAX(attr->index),
295 data->in_max[attr->index]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100296 mutex_unlock(&data->update_lock);
Grant Coady40b5cda2005-04-30 21:41:29 +1000297 return count;
298}
299
Grant Coadyc7461a62005-09-17 05:32:57 +1000300#define vin(nr) \
Guenter Roeckc387e4e2012-01-14 13:12:38 -0800301static SENSOR_DEVICE_ATTR(in##nr##_input, S_IRUGO, \
Grant Coadyc7461a62005-09-17 05:32:57 +1000302 show_in, NULL, nr); \
303static SENSOR_DEVICE_ATTR(in##nr##_min, S_IRUGO | S_IWUSR, \
304 show_in_min, set_in_min, nr); \
305static SENSOR_DEVICE_ATTR(in##nr##_max, S_IRUGO | S_IWUSR, \
306 show_in_max, set_in_max, nr);
Grant Coady40b5cda2005-04-30 21:41:29 +1000307
Grant Coadyc7461a62005-09-17 05:32:57 +1000308vin(0);
309vin(1);
310vin(2);
311vin(3);
312vin(4);
313vin(5);
Grant Coady40b5cda2005-04-30 21:41:29 +1000314
315/* fans */
Grant Coadyc7461a62005-09-17 05:32:57 +1000316static ssize_t show_fan(struct device *dev,
317 struct device_attribute *devattr, char *buf)
Grant Coady40b5cda2005-04-30 21:41:29 +1000318{
Grant Coadyc7461a62005-09-17 05:32:57 +1000319 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
Grant Coady40b5cda2005-04-30 21:41:29 +1000320 struct adm9240_data *data = adm9240_update_device(dev);
Grant Coadyc7461a62005-09-17 05:32:57 +1000321 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[attr->index],
322 1 << data->fan_div[attr->index]));
Grant Coady40b5cda2005-04-30 21:41:29 +1000323}
324
Grant Coadyc7461a62005-09-17 05:32:57 +1000325static ssize_t show_fan_min(struct device *dev,
326 struct device_attribute *devattr, char *buf)
Grant Coady40b5cda2005-04-30 21:41:29 +1000327{
Grant Coadyc7461a62005-09-17 05:32:57 +1000328 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
Grant Coady40b5cda2005-04-30 21:41:29 +1000329 struct adm9240_data *data = adm9240_update_device(dev);
Grant Coadyc7461a62005-09-17 05:32:57 +1000330 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_min[attr->index],
331 1 << data->fan_div[attr->index]));
Grant Coady40b5cda2005-04-30 21:41:29 +1000332}
333
Grant Coadyc7461a62005-09-17 05:32:57 +1000334static ssize_t show_fan_div(struct device *dev,
335 struct device_attribute *devattr, char *buf)
Grant Coady40b5cda2005-04-30 21:41:29 +1000336{
Grant Coadyc7461a62005-09-17 05:32:57 +1000337 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
Grant Coady40b5cda2005-04-30 21:41:29 +1000338 struct adm9240_data *data = adm9240_update_device(dev);
Grant Coadyc7461a62005-09-17 05:32:57 +1000339 return sprintf(buf, "%d\n", 1 << data->fan_div[attr->index]);
Grant Coady40b5cda2005-04-30 21:41:29 +1000340}
341
342/* write new fan div, callers must hold data->update_lock */
343static void adm9240_write_fan_div(struct i2c_client *client, int nr,
344 u8 fan_div)
345{
346 u8 reg, old, shift = (nr + 2) * 2;
347
Grant Coady205cf132005-09-17 05:32:55 +1000348 reg = i2c_smbus_read_byte_data(client, ADM9240_REG_VID_FAN_DIV);
Grant Coady40b5cda2005-04-30 21:41:29 +1000349 old = (reg >> shift) & 3;
350 reg &= ~(3 << shift);
351 reg |= (fan_div << shift);
Grant Coady205cf132005-09-17 05:32:55 +1000352 i2c_smbus_write_byte_data(client, ADM9240_REG_VID_FAN_DIV, reg);
Grant Coady40b5cda2005-04-30 21:41:29 +1000353 dev_dbg(&client->dev, "fan%d clock divider changed from %u "
354 "to %u\n", nr + 1, 1 << old, 1 << fan_div);
355}
356
Jean Delvaree415e482005-09-25 16:14:18 +0200357/*
Grant Coady40b5cda2005-04-30 21:41:29 +1000358 * set fan speed low limit:
359 *
360 * - value is zero: disable fan speed low limit alarm
361 *
362 * - value is below fan speed measurement range: enable fan speed low
363 * limit alarm to be asserted while fan speed too slow to measure
364 *
365 * - otherwise: select fan clock divider to suit fan speed low limit,
366 * measurement code may adjust registers to ensure fan speed reading
367 */
Grant Coadyc7461a62005-09-17 05:32:57 +1000368static ssize_t set_fan_min(struct device *dev,
369 struct device_attribute *devattr,
370 const char *buf, size_t count)
Grant Coady40b5cda2005-04-30 21:41:29 +1000371{
Grant Coadyc7461a62005-09-17 05:32:57 +1000372 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
Grant Coady40b5cda2005-04-30 21:41:29 +1000373 struct i2c_client *client = to_i2c_client(dev);
374 struct adm9240_data *data = i2c_get_clientdata(client);
Grant Coadyc7461a62005-09-17 05:32:57 +1000375 int nr = attr->index;
Grant Coady40b5cda2005-04-30 21:41:29 +1000376 u8 new_div;
Guenter Roeckc387e4e2012-01-14 13:12:38 -0800377 unsigned long val;
378 int err;
379
380 err = kstrtoul(buf, 10, &val);
381 if (err)
382 return err;
Grant Coady40b5cda2005-04-30 21:41:29 +1000383
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100384 mutex_lock(&data->update_lock);
Grant Coady40b5cda2005-04-30 21:41:29 +1000385
386 if (!val) {
387 data->fan_min[nr] = 255;
388 new_div = data->fan_div[nr];
389
390 dev_dbg(&client->dev, "fan%u low limit set disabled\n",
391 nr + 1);
392
393 } else if (val < 1350000 / (8 * 254)) {
394 new_div = 3;
395 data->fan_min[nr] = 254;
396
397 dev_dbg(&client->dev, "fan%u low limit set minimum %u\n",
398 nr + 1, FAN_FROM_REG(254, 1 << new_div));
399
400 } else {
401 unsigned int new_min = 1350000 / val;
402
403 new_div = 0;
404 while (new_min > 192 && new_div < 3) {
405 new_div++;
406 new_min /= 2;
407 }
408 if (!new_min) /* keep > 0 */
409 new_min++;
410
411 data->fan_min[nr] = new_min;
412
413 dev_dbg(&client->dev, "fan%u low limit set fan speed %u\n",
414 nr + 1, FAN_FROM_REG(new_min, 1 << new_div));
415 }
416
417 if (new_div != data->fan_div[nr]) {
418 data->fan_div[nr] = new_div;
419 adm9240_write_fan_div(client, nr, new_div);
420 }
Grant Coady205cf132005-09-17 05:32:55 +1000421 i2c_smbus_write_byte_data(client, ADM9240_REG_FAN_MIN(nr),
Grant Coady40b5cda2005-04-30 21:41:29 +1000422 data->fan_min[nr]);
423
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100424 mutex_unlock(&data->update_lock);
Grant Coady40b5cda2005-04-30 21:41:29 +1000425 return count;
426}
427
Grant Coadyc7461a62005-09-17 05:32:57 +1000428#define fan(nr) \
429static SENSOR_DEVICE_ATTR(fan##nr##_input, S_IRUGO, \
430 show_fan, NULL, nr - 1); \
431static SENSOR_DEVICE_ATTR(fan##nr##_div, S_IRUGO, \
432 show_fan_div, NULL, nr - 1); \
433static SENSOR_DEVICE_ATTR(fan##nr##_min, S_IRUGO | S_IWUSR, \
434 show_fan_min, set_fan_min, nr - 1);
Grant Coady40b5cda2005-04-30 21:41:29 +1000435
Grant Coadyc7461a62005-09-17 05:32:57 +1000436fan(1);
437fan(2);
Grant Coady40b5cda2005-04-30 21:41:29 +1000438
439/* alarms */
Jean Delvaree415e482005-09-25 16:14:18 +0200440static ssize_t show_alarms(struct device *dev,
441 struct device_attribute *attr, char *buf)
Grant Coady40b5cda2005-04-30 21:41:29 +1000442{
443 struct adm9240_data *data = adm9240_update_device(dev);
444 return sprintf(buf, "%u\n", data->alarms);
445}
446static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
447
Jean Delvare360f9452008-01-06 15:49:19 +0100448static ssize_t show_alarm(struct device *dev,
449 struct device_attribute *attr, char *buf)
450{
451 int bitnr = to_sensor_dev_attr(attr)->index;
452 struct adm9240_data *data = adm9240_update_device(dev);
453 return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1);
454}
455static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 0);
456static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 1);
457static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 2);
458static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 3);
459static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 8);
460static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 9);
461static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 4);
462static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 6);
463static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 7);
464
Grant Coady40b5cda2005-04-30 21:41:29 +1000465/* vid */
Jean Delvaree415e482005-09-25 16:14:18 +0200466static ssize_t show_vid(struct device *dev,
467 struct device_attribute *attr, char *buf)
Grant Coady40b5cda2005-04-30 21:41:29 +1000468{
469 struct adm9240_data *data = adm9240_update_device(dev);
470 return sprintf(buf, "%d\n", vid_from_reg(data->vid, data->vrm));
471}
472static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid, NULL);
473
474/* analog output */
Jean Delvaree415e482005-09-25 16:14:18 +0200475static ssize_t show_aout(struct device *dev,
476 struct device_attribute *attr, char *buf)
Grant Coady40b5cda2005-04-30 21:41:29 +1000477{
478 struct adm9240_data *data = adm9240_update_device(dev);
479 return sprintf(buf, "%d\n", AOUT_FROM_REG(data->aout));
480}
481
Jean Delvaree415e482005-09-25 16:14:18 +0200482static ssize_t set_aout(struct device *dev,
483 struct device_attribute *attr,
484 const char *buf, size_t count)
Grant Coady40b5cda2005-04-30 21:41:29 +1000485{
486 struct i2c_client *client = to_i2c_client(dev);
487 struct adm9240_data *data = i2c_get_clientdata(client);
Guenter Roeckc387e4e2012-01-14 13:12:38 -0800488 long val;
489 int err;
490
491 err = kstrtol(buf, 10, &val);
492 if (err)
493 return err;
Grant Coady40b5cda2005-04-30 21:41:29 +1000494
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100495 mutex_lock(&data->update_lock);
Grant Coady40b5cda2005-04-30 21:41:29 +1000496 data->aout = AOUT_TO_REG(val);
Grant Coady205cf132005-09-17 05:32:55 +1000497 i2c_smbus_write_byte_data(client, ADM9240_REG_ANALOG_OUT, data->aout);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100498 mutex_unlock(&data->update_lock);
Grant Coady40b5cda2005-04-30 21:41:29 +1000499 return count;
500}
501static DEVICE_ATTR(aout_output, S_IRUGO | S_IWUSR, show_aout, set_aout);
502
503/* chassis_clear */
Jean Delvare0de2b242011-01-12 21:55:09 +0100504static ssize_t chassis_clear_legacy(struct device *dev,
Jean Delvaree415e482005-09-25 16:14:18 +0200505 struct device_attribute *attr,
506 const char *buf, size_t count)
Grant Coady40b5cda2005-04-30 21:41:29 +1000507{
508 struct i2c_client *client = to_i2c_client(dev);
Guenter Roeckc387e4e2012-01-14 13:12:38 -0800509 long val;
510 int err;
511
512 err = kstrtol(buf, 10, &val);
513 if (err)
514 return err;
Grant Coady40b5cda2005-04-30 21:41:29 +1000515
Jean Delvare0de2b242011-01-12 21:55:09 +0100516 dev_warn(dev, "Attribute chassis_clear is deprecated, "
517 "use intrusion0_alarm instead\n");
518
Grant Coady40b5cda2005-04-30 21:41:29 +1000519 if (val == 1) {
Grant Coady205cf132005-09-17 05:32:55 +1000520 i2c_smbus_write_byte_data(client,
521 ADM9240_REG_CHASSIS_CLEAR, 0x80);
Grant Coady40b5cda2005-04-30 21:41:29 +1000522 dev_dbg(&client->dev, "chassis intrusion latch cleared\n");
523 }
524 return count;
525}
Jean Delvare0de2b242011-01-12 21:55:09 +0100526static DEVICE_ATTR(chassis_clear, S_IWUSR, NULL, chassis_clear_legacy);
527
528static ssize_t chassis_clear(struct device *dev,
529 struct device_attribute *attr,
530 const char *buf, size_t count)
531{
532 struct i2c_client *client = to_i2c_client(dev);
533 struct adm9240_data *data = i2c_get_clientdata(client);
534 unsigned long val;
535
Frans Meulenbroeks179c4fd2012-01-04 20:58:52 +0100536 if (kstrtoul(buf, 10, &val) || val != 0)
Jean Delvare0de2b242011-01-12 21:55:09 +0100537 return -EINVAL;
538
539 mutex_lock(&data->update_lock);
540 i2c_smbus_write_byte_data(client, ADM9240_REG_CHASSIS_CLEAR, 0x80);
541 data->valid = 0; /* Force cache refresh */
542 mutex_unlock(&data->update_lock);
543 dev_dbg(&client->dev, "chassis intrusion latch cleared\n");
544
545 return count;
546}
547static SENSOR_DEVICE_ATTR(intrusion0_alarm, S_IRUGO | S_IWUSR, show_alarm,
548 chassis_clear, 12);
Grant Coady40b5cda2005-04-30 21:41:29 +1000549
Mark M. Hoffman681c6f72006-09-24 21:15:35 +0200550static struct attribute *adm9240_attributes[] = {
551 &sensor_dev_attr_in0_input.dev_attr.attr,
552 &sensor_dev_attr_in0_min.dev_attr.attr,
553 &sensor_dev_attr_in0_max.dev_attr.attr,
Jean Delvare360f9452008-01-06 15:49:19 +0100554 &sensor_dev_attr_in0_alarm.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +0200555 &sensor_dev_attr_in1_input.dev_attr.attr,
556 &sensor_dev_attr_in1_min.dev_attr.attr,
557 &sensor_dev_attr_in1_max.dev_attr.attr,
Jean Delvare360f9452008-01-06 15:49:19 +0100558 &sensor_dev_attr_in1_alarm.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +0200559 &sensor_dev_attr_in2_input.dev_attr.attr,
560 &sensor_dev_attr_in2_min.dev_attr.attr,
561 &sensor_dev_attr_in2_max.dev_attr.attr,
Jean Delvare360f9452008-01-06 15:49:19 +0100562 &sensor_dev_attr_in2_alarm.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +0200563 &sensor_dev_attr_in3_input.dev_attr.attr,
564 &sensor_dev_attr_in3_min.dev_attr.attr,
565 &sensor_dev_attr_in3_max.dev_attr.attr,
Jean Delvare360f9452008-01-06 15:49:19 +0100566 &sensor_dev_attr_in3_alarm.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +0200567 &sensor_dev_attr_in4_input.dev_attr.attr,
568 &sensor_dev_attr_in4_min.dev_attr.attr,
569 &sensor_dev_attr_in4_max.dev_attr.attr,
Jean Delvare360f9452008-01-06 15:49:19 +0100570 &sensor_dev_attr_in4_alarm.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +0200571 &sensor_dev_attr_in5_input.dev_attr.attr,
572 &sensor_dev_attr_in5_min.dev_attr.attr,
573 &sensor_dev_attr_in5_max.dev_attr.attr,
Jean Delvare360f9452008-01-06 15:49:19 +0100574 &sensor_dev_attr_in5_alarm.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +0200575 &dev_attr_temp1_input.attr,
576 &sensor_dev_attr_temp1_max.dev_attr.attr,
577 &sensor_dev_attr_temp1_max_hyst.dev_attr.attr,
Jean Delvare360f9452008-01-06 15:49:19 +0100578 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +0200579 &sensor_dev_attr_fan1_input.dev_attr.attr,
580 &sensor_dev_attr_fan1_div.dev_attr.attr,
581 &sensor_dev_attr_fan1_min.dev_attr.attr,
Jean Delvare360f9452008-01-06 15:49:19 +0100582 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +0200583 &sensor_dev_attr_fan2_input.dev_attr.attr,
584 &sensor_dev_attr_fan2_div.dev_attr.attr,
585 &sensor_dev_attr_fan2_min.dev_attr.attr,
Jean Delvare360f9452008-01-06 15:49:19 +0100586 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +0200587 &dev_attr_alarms.attr,
588 &dev_attr_aout_output.attr,
589 &dev_attr_chassis_clear.attr,
Jean Delvare0de2b242011-01-12 21:55:09 +0100590 &sensor_dev_attr_intrusion0_alarm.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +0200591 &dev_attr_cpu0_vid.attr,
592 NULL
593};
594
595static const struct attribute_group adm9240_group = {
596 .attrs = adm9240_attributes,
597};
598
Grant Coady40b5cda2005-04-30 21:41:29 +1000599
600/*** sensor chip detect and driver install ***/
601
Jean Delvare7fae8282008-07-16 19:30:09 +0200602/* Return 0 if detection is successful, -ENODEV otherwise */
Jean Delvare310ec792009-12-14 21:17:23 +0100603static int adm9240_detect(struct i2c_client *new_client,
Jean Delvare7fae8282008-07-16 19:30:09 +0200604 struct i2c_board_info *info)
Grant Coady40b5cda2005-04-30 21:41:29 +1000605{
Jean Delvare7fae8282008-07-16 19:30:09 +0200606 struct i2c_adapter *adapter = new_client->adapter;
Grant Coady40b5cda2005-04-30 21:41:29 +1000607 const char *name = "";
Jean Delvare7fae8282008-07-16 19:30:09 +0200608 int address = new_client->addr;
Grant Coady40b5cda2005-04-30 21:41:29 +1000609 u8 man_id, die_rev;
610
611 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
Jean Delvare7fae8282008-07-16 19:30:09 +0200612 return -ENODEV;
Grant Coady40b5cda2005-04-30 21:41:29 +1000613
Jean Delvare52df6442009-12-09 20:35:57 +0100614 /* verify chip: reg address should match i2c address */
615 if (i2c_smbus_read_byte_data(new_client, ADM9240_REG_I2C_ADDR)
616 != address) {
617 dev_err(&adapter->dev, "detect fail: address match, 0x%02x\n",
618 address);
619 return -ENODEV;
Grant Coady40b5cda2005-04-30 21:41:29 +1000620 }
621
Jean Delvare52df6442009-12-09 20:35:57 +0100622 /* check known chip manufacturer */
623 man_id = i2c_smbus_read_byte_data(new_client, ADM9240_REG_MAN_ID);
624 if (man_id == 0x23) {
Grant Coady40b5cda2005-04-30 21:41:29 +1000625 name = "adm9240";
Jean Delvare52df6442009-12-09 20:35:57 +0100626 } else if (man_id == 0xda) {
Grant Coady40b5cda2005-04-30 21:41:29 +1000627 name = "ds1780";
Jean Delvare52df6442009-12-09 20:35:57 +0100628 } else if (man_id == 0x01) {
Grant Coady40b5cda2005-04-30 21:41:29 +1000629 name = "lm81";
Jean Delvare52df6442009-12-09 20:35:57 +0100630 } else {
631 dev_err(&adapter->dev, "detect fail: unknown manuf, 0x%02x\n",
632 man_id);
633 return -ENODEV;
Grant Coady40b5cda2005-04-30 21:41:29 +1000634 }
Jean Delvare52df6442009-12-09 20:35:57 +0100635
636 /* successful detect, print chip info */
637 die_rev = i2c_smbus_read_byte_data(new_client, ADM9240_REG_DIE_REV);
638 dev_info(&adapter->dev, "found %s revision %u\n",
639 man_id == 0x23 ? "ADM9240" :
640 man_id == 0xda ? "DS1780" : "LM81", die_rev);
641
Jean Delvare7fae8282008-07-16 19:30:09 +0200642 strlcpy(info->type, name, I2C_NAME_SIZE);
Grant Coady40b5cda2005-04-30 21:41:29 +1000643
Jean Delvare7fae8282008-07-16 19:30:09 +0200644 return 0;
645}
646
647static int adm9240_probe(struct i2c_client *new_client,
648 const struct i2c_device_id *id)
649{
650 struct adm9240_data *data;
651 int err;
652
653 data = kzalloc(sizeof(*data), GFP_KERNEL);
654 if (!data) {
655 err = -ENOMEM;
656 goto exit;
657 }
658
659 i2c_set_clientdata(new_client, data);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100660 mutex_init(&data->update_lock);
Grant Coady40b5cda2005-04-30 21:41:29 +1000661
Grant Coady40b5cda2005-04-30 21:41:29 +1000662 adm9240_init_client(new_client);
663
664 /* populate sysfs filesystem */
Guenter Roeckc387e4e2012-01-14 13:12:38 -0800665 err = sysfs_create_group(&new_client->dev.kobj, &adm9240_group);
666 if (err)
Jean Delvare7fae8282008-07-16 19:30:09 +0200667 goto exit_free;
Mark M. Hoffman681c6f72006-09-24 21:15:35 +0200668
Tony Jones1beeffe2007-08-20 13:46:20 -0700669 data->hwmon_dev = hwmon_device_register(&new_client->dev);
670 if (IS_ERR(data->hwmon_dev)) {
671 err = PTR_ERR(data->hwmon_dev);
Mark M. Hoffman681c6f72006-09-24 21:15:35 +0200672 goto exit_remove;
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400673 }
674
Grant Coady40b5cda2005-04-30 21:41:29 +1000675 return 0;
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400676
Mark M. Hoffman681c6f72006-09-24 21:15:35 +0200677exit_remove:
678 sysfs_remove_group(&new_client->dev.kobj, &adm9240_group);
Grant Coady40b5cda2005-04-30 21:41:29 +1000679exit_free:
Alexey Dobriyan1f57ff82005-08-26 01:49:14 +0400680 kfree(data);
Grant Coady40b5cda2005-04-30 21:41:29 +1000681exit:
682 return err;
683}
684
Jean Delvare7fae8282008-07-16 19:30:09 +0200685static int adm9240_remove(struct i2c_client *client)
Grant Coady40b5cda2005-04-30 21:41:29 +1000686{
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400687 struct adm9240_data *data = i2c_get_clientdata(client);
Grant Coady40b5cda2005-04-30 21:41:29 +1000688
Tony Jones1beeffe2007-08-20 13:46:20 -0700689 hwmon_device_unregister(data->hwmon_dev);
Mark M. Hoffman681c6f72006-09-24 21:15:35 +0200690 sysfs_remove_group(&client->dev.kobj, &adm9240_group);
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400691
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400692 kfree(data);
Grant Coady40b5cda2005-04-30 21:41:29 +1000693 return 0;
694}
695
696static void adm9240_init_client(struct i2c_client *client)
697{
698 struct adm9240_data *data = i2c_get_clientdata(client);
Grant Coady205cf132005-09-17 05:32:55 +1000699 u8 conf = i2c_smbus_read_byte_data(client, ADM9240_REG_CONFIG);
700 u8 mode = i2c_smbus_read_byte_data(client, ADM9240_REG_TEMP_CONF) & 3;
Grant Coady40b5cda2005-04-30 21:41:29 +1000701
Jean Delvare303760b2005-07-31 21:52:01 +0200702 data->vrm = vid_which_vrm(); /* need this to report vid as mV */
Grant Coady40b5cda2005-04-30 21:41:29 +1000703
Grant Coady8e8f9282005-05-13 20:26:10 +1000704 dev_info(&client->dev, "Using VRM: %d.%d\n", data->vrm / 10,
705 data->vrm % 10);
706
Grant Coady40b5cda2005-04-30 21:41:29 +1000707 if (conf & 1) { /* measurement cycle running: report state */
708
709 dev_info(&client->dev, "status: config 0x%02x mode %u\n",
710 conf, mode);
711
712 } else { /* cold start: open limits before starting chip */
713 int i;
714
Guenter Roeckc387e4e2012-01-14 13:12:38 -0800715 for (i = 0; i < 6; i++) {
Grant Coady205cf132005-09-17 05:32:55 +1000716 i2c_smbus_write_byte_data(client,
Grant Coady40b5cda2005-04-30 21:41:29 +1000717 ADM9240_REG_IN_MIN(i), 0);
Grant Coady205cf132005-09-17 05:32:55 +1000718 i2c_smbus_write_byte_data(client,
Grant Coady40b5cda2005-04-30 21:41:29 +1000719 ADM9240_REG_IN_MAX(i), 255);
720 }
Grant Coady205cf132005-09-17 05:32:55 +1000721 i2c_smbus_write_byte_data(client,
722 ADM9240_REG_FAN_MIN(0), 255);
723 i2c_smbus_write_byte_data(client,
724 ADM9240_REG_FAN_MIN(1), 255);
725 i2c_smbus_write_byte_data(client,
Grant Coadyc7461a62005-09-17 05:32:57 +1000726 ADM9240_REG_TEMP_MAX(0), 127);
Grant Coady205cf132005-09-17 05:32:55 +1000727 i2c_smbus_write_byte_data(client,
Grant Coadyc7461a62005-09-17 05:32:57 +1000728 ADM9240_REG_TEMP_MAX(1), 127);
Grant Coady40b5cda2005-04-30 21:41:29 +1000729
730 /* start measurement cycle */
Grant Coady205cf132005-09-17 05:32:55 +1000731 i2c_smbus_write_byte_data(client, ADM9240_REG_CONFIG, 1);
Grant Coady40b5cda2005-04-30 21:41:29 +1000732
733 dev_info(&client->dev, "cold start: config was 0x%02x "
734 "mode %u\n", conf, mode);
735 }
736}
737
738static struct adm9240_data *adm9240_update_device(struct device *dev)
739{
740 struct i2c_client *client = to_i2c_client(dev);
741 struct adm9240_data *data = i2c_get_clientdata(client);
742 int i;
743
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100744 mutex_lock(&data->update_lock);
Grant Coady40b5cda2005-04-30 21:41:29 +1000745
746 /* minimum measurement cycle: 1.75 seconds */
747 if (time_after(jiffies, data->last_updated_measure + (HZ * 7 / 4))
748 || !data->valid) {
749
Guenter Roeckc387e4e2012-01-14 13:12:38 -0800750 for (i = 0; i < 6; i++) { /* read voltages */
Grant Coady205cf132005-09-17 05:32:55 +1000751 data->in[i] = i2c_smbus_read_byte_data(client,
Grant Coady40b5cda2005-04-30 21:41:29 +1000752 ADM9240_REG_IN(i));
753 }
Grant Coady205cf132005-09-17 05:32:55 +1000754 data->alarms = i2c_smbus_read_byte_data(client,
Grant Coady40b5cda2005-04-30 21:41:29 +1000755 ADM9240_REG_INT(0)) |
Grant Coady205cf132005-09-17 05:32:55 +1000756 i2c_smbus_read_byte_data(client,
Grant Coady40b5cda2005-04-30 21:41:29 +1000757 ADM9240_REG_INT(1)) << 8;
758
Guenter Roeckc387e4e2012-01-14 13:12:38 -0800759 /*
760 * read temperature: assume temperature changes less than
Grant Coady40b5cda2005-04-30 21:41:29 +1000761 * 0.5'C per two measurement cycles thus ignore possible
Guenter Roeckc387e4e2012-01-14 13:12:38 -0800762 * but unlikely aliasing error on lsb reading. --Grant
763 */
Grant Coady205cf132005-09-17 05:32:55 +1000764 data->temp = ((i2c_smbus_read_byte_data(client,
Grant Coady40b5cda2005-04-30 21:41:29 +1000765 ADM9240_REG_TEMP) << 8) |
Grant Coady205cf132005-09-17 05:32:55 +1000766 i2c_smbus_read_byte_data(client,
Grant Coady40b5cda2005-04-30 21:41:29 +1000767 ADM9240_REG_TEMP_CONF)) / 128;
768
Guenter Roeckc387e4e2012-01-14 13:12:38 -0800769 for (i = 0; i < 2; i++) { /* read fans */
Grant Coady205cf132005-09-17 05:32:55 +1000770 data->fan[i] = i2c_smbus_read_byte_data(client,
Grant Coady40b5cda2005-04-30 21:41:29 +1000771 ADM9240_REG_FAN(i));
772
773 /* adjust fan clock divider on overflow */
774 if (data->valid && data->fan[i] == 255 &&
775 data->fan_div[i] < 3) {
776
777 adm9240_write_fan_div(client, i,
778 ++data->fan_div[i]);
779
780 /* adjust fan_min if active, but not to 0 */
781 if (data->fan_min[i] < 255 &&
782 data->fan_min[i] >= 2)
783 data->fan_min[i] /= 2;
784 }
785 }
786 data->last_updated_measure = jiffies;
787 }
788
789 /* minimum config reading cycle: 300 seconds */
790 if (time_after(jiffies, data->last_updated_config + (HZ * 300))
791 || !data->valid) {
792
Guenter Roeckc387e4e2012-01-14 13:12:38 -0800793 for (i = 0; i < 6; i++) {
Grant Coady205cf132005-09-17 05:32:55 +1000794 data->in_min[i] = i2c_smbus_read_byte_data(client,
Grant Coady40b5cda2005-04-30 21:41:29 +1000795 ADM9240_REG_IN_MIN(i));
Grant Coady205cf132005-09-17 05:32:55 +1000796 data->in_max[i] = i2c_smbus_read_byte_data(client,
Grant Coady40b5cda2005-04-30 21:41:29 +1000797 ADM9240_REG_IN_MAX(i));
798 }
Guenter Roeckc387e4e2012-01-14 13:12:38 -0800799 for (i = 0; i < 2; i++) {
Grant Coady205cf132005-09-17 05:32:55 +1000800 data->fan_min[i] = i2c_smbus_read_byte_data(client,
Grant Coady40b5cda2005-04-30 21:41:29 +1000801 ADM9240_REG_FAN_MIN(i));
802 }
Grant Coadyc7461a62005-09-17 05:32:57 +1000803 data->temp_max[0] = i2c_smbus_read_byte_data(client,
804 ADM9240_REG_TEMP_MAX(0));
805 data->temp_max[1] = i2c_smbus_read_byte_data(client,
806 ADM9240_REG_TEMP_MAX(1));
Grant Coady40b5cda2005-04-30 21:41:29 +1000807
808 /* read fan divs and 5-bit VID */
Grant Coady205cf132005-09-17 05:32:55 +1000809 i = i2c_smbus_read_byte_data(client, ADM9240_REG_VID_FAN_DIV);
Grant Coady40b5cda2005-04-30 21:41:29 +1000810 data->fan_div[0] = (i >> 4) & 3;
811 data->fan_div[1] = (i >> 6) & 3;
812 data->vid = i & 0x0f;
Grant Coady205cf132005-09-17 05:32:55 +1000813 data->vid |= (i2c_smbus_read_byte_data(client,
Grant Coady40b5cda2005-04-30 21:41:29 +1000814 ADM9240_REG_VID4) & 1) << 4;
815 /* read analog out */
Grant Coady205cf132005-09-17 05:32:55 +1000816 data->aout = i2c_smbus_read_byte_data(client,
Grant Coady40b5cda2005-04-30 21:41:29 +1000817 ADM9240_REG_ANALOG_OUT);
818
819 data->last_updated_config = jiffies;
820 data->valid = 1;
821 }
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100822 mutex_unlock(&data->update_lock);
Grant Coady40b5cda2005-04-30 21:41:29 +1000823 return data;
824}
825
Axel Linf0967ee2012-01-20 15:38:18 +0800826module_i2c_driver(adm9240_driver);
Grant Coady40b5cda2005-04-30 21:41:29 +1000827
828MODULE_AUTHOR("Michiel Rook <michiel@grendelproject.nl>, "
Grant Coady2ca7b962006-10-08 21:57:41 +0200829 "Grant Coady <gcoady.lk@gmail.com> and others");
Grant Coady40b5cda2005-04-30 21:41:29 +1000830MODULE_DESCRIPTION("ADM9240/DS1780/LM81 driver");
831MODULE_LICENSE("GPL");