blob: 149ef25252e7658442e4191b9b645a5c088685c7 [file] [log] [blame]
Grant Coady40b5cda2005-04-30 21:41:29 +10001/*
2 * adm9240.c Part of lm_sensors, Linux kernel modules for hardware
3 * monitoring
4 *
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
Grant Coady40b5cda2005-04-30 21:41:29 +10009 * guidance from Jean Delvare
10 *
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 *
23 * Chassis Intrusion: clear CI latch with 'echo 1 > chassis_clear'
24 *
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
Grant Coady40b5cda2005-04-30 21:41:29 +100058/* Insmod parameters */
Jean Delvaref4b50262005-07-31 21:49:03 +020059I2C_CLIENT_INSMOD_3(adm9240, ds1780, lm81);
Grant Coady40b5cda2005-04-30 21:41:29 +100060
61/* ADM9240 registers */
62#define ADM9240_REG_MAN_ID 0x3e
63#define ADM9240_REG_DIE_REV 0x3f
64#define ADM9240_REG_CONFIG 0x40
65
66#define ADM9240_REG_IN(nr) (0x20 + (nr)) /* 0..5 */
67#define ADM9240_REG_IN_MAX(nr) (0x2b + (nr) * 2)
68#define ADM9240_REG_IN_MIN(nr) (0x2c + (nr) * 2)
69#define ADM9240_REG_FAN(nr) (0x28 + (nr)) /* 0..1 */
70#define ADM9240_REG_FAN_MIN(nr) (0x3b + (nr))
71#define ADM9240_REG_INT(nr) (0x41 + (nr))
72#define ADM9240_REG_INT_MASK(nr) (0x43 + (nr))
73#define ADM9240_REG_TEMP 0x27
Grant Coadyc7461a62005-09-17 05:32:57 +100074#define ADM9240_REG_TEMP_MAX(nr) (0x39 + (nr)) /* 0, 1 = high, hyst */
Grant Coady40b5cda2005-04-30 21:41:29 +100075#define ADM9240_REG_ANALOG_OUT 0x19
76#define ADM9240_REG_CHASSIS_CLEAR 0x46
77#define ADM9240_REG_VID_FAN_DIV 0x47
78#define ADM9240_REG_I2C_ADDR 0x48
79#define ADM9240_REG_VID4 0x49
80#define ADM9240_REG_TEMP_CONF 0x4b
81
82/* generalised scaling with integer rounding */
83static inline int SCALE(long val, int mul, int div)
84{
85 if (val < 0)
86 return (val * mul - div / 2) / div;
87 else
88 return (val * mul + div / 2) / div;
89}
90
91/* adm9240 internally scales voltage measurements */
92static const u16 nom_mv[] = { 2500, 2700, 3300, 5000, 12000, 2700 };
93
94static inline unsigned int IN_FROM_REG(u8 reg, int n)
95{
96 return SCALE(reg, nom_mv[n], 192);
97}
98
99static inline u8 IN_TO_REG(unsigned long val, int n)
100{
101 return SENSORS_LIMIT(SCALE(val, 192, nom_mv[n]), 0, 255);
102}
103
104/* temperature range: -40..125, 127 disables temperature alarm */
105static inline s8 TEMP_TO_REG(long val)
106{
107 return SENSORS_LIMIT(SCALE(val, 1, 1000), -40, 127);
108}
109
110/* two fans, each with low fan speed limit */
111static inline unsigned int FAN_FROM_REG(u8 reg, u8 div)
112{
113 if (!reg) /* error */
114 return -1;
115
116 if (reg == 255)
117 return 0;
118
119 return SCALE(1350000, 1, reg * div);
120}
121
122/* analog out 0..1250mV */
123static inline u8 AOUT_TO_REG(unsigned long val)
124{
125 return SENSORS_LIMIT(SCALE(val, 255, 1250), 0, 255);
126}
127
128static inline unsigned int AOUT_FROM_REG(u8 reg)
129{
130 return SCALE(reg, 1250, 255);
131}
132
133static int adm9240_attach_adapter(struct i2c_adapter *adapter);
134static int adm9240_detect(struct i2c_adapter *adapter, int address, int kind);
135static void adm9240_init_client(struct i2c_client *client);
136static int adm9240_detach_client(struct i2c_client *client);
137static struct adm9240_data *adm9240_update_device(struct device *dev);
138
139/* driver data */
140static struct i2c_driver adm9240_driver = {
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100141 .driver = {
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100142 .name = "adm9240",
143 },
Grant Coady40b5cda2005-04-30 21:41:29 +1000144 .attach_adapter = adm9240_attach_adapter,
145 .detach_client = adm9240_detach_client,
146};
147
148/* per client data */
149struct adm9240_data {
150 enum chips type;
151 struct i2c_client client;
Tony Jones1beeffe2007-08-20 13:46:20 -0700152 struct device *hwmon_dev;
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100153 struct mutex update_lock;
Grant Coady40b5cda2005-04-30 21:41:29 +1000154 char valid;
155 unsigned long last_updated_measure;
156 unsigned long last_updated_config;
157
158 u8 in[6]; /* ro in0_input */
159 u8 in_max[6]; /* rw in0_max */
160 u8 in_min[6]; /* rw in0_min */
161 u8 fan[2]; /* ro fan1_input */
162 u8 fan_min[2]; /* rw fan1_min */
163 u8 fan_div[2]; /* rw fan1_div, read-only accessor */
164 s16 temp; /* ro temp1_input, 9-bit sign-extended */
Grant Coadyc7461a62005-09-17 05:32:57 +1000165 s8 temp_max[2]; /* rw 0 -> temp_max, 1 -> temp_max_hyst */
Grant Coady40b5cda2005-04-30 21:41:29 +1000166 u16 alarms; /* ro alarms */
Grant Coady8e8f9282005-05-13 20:26:10 +1000167 u8 aout; /* rw aout_output */
Grant Coady40b5cda2005-04-30 21:41:29 +1000168 u8 vid; /* ro vid */
169 u8 vrm; /* -- vrm set on startup, no accessor */
170};
171
Grant Coady40b5cda2005-04-30 21:41:29 +1000172/*** sysfs accessors ***/
173
174/* temperature */
Grant Coadyc7461a62005-09-17 05:32:57 +1000175static ssize_t show_temp(struct device *dev, struct device_attribute *dummy,
176 char *buf)
177{
178 struct adm9240_data *data = adm9240_update_device(dev);
179 return sprintf(buf, "%d\n", data->temp * 500); /* 9-bit value */
Grant Coady40b5cda2005-04-30 21:41:29 +1000180}
181
Grant Coadyc7461a62005-09-17 05:32:57 +1000182static ssize_t show_max(struct device *dev, struct device_attribute *devattr,
183 char *buf)
184{
185 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
186 struct adm9240_data *data = adm9240_update_device(dev);
187 return sprintf(buf, "%d\n", data->temp_max[attr->index] * 1000);
188}
Grant Coady40b5cda2005-04-30 21:41:29 +1000189
Grant Coadyc7461a62005-09-17 05:32:57 +1000190static ssize_t set_max(struct device *dev, struct device_attribute *devattr,
191 const char *buf, size_t count)
192{
193 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
194 struct i2c_client *client = to_i2c_client(dev);
195 struct adm9240_data *data = i2c_get_clientdata(client);
196 long val = simple_strtol(buf, NULL, 10);
197
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100198 mutex_lock(&data->update_lock);
Grant Coadyc7461a62005-09-17 05:32:57 +1000199 data->temp_max[attr->index] = TEMP_TO_REG(val);
200 i2c_smbus_write_byte_data(client, ADM9240_REG_TEMP_MAX(attr->index),
201 data->temp_max[attr->index]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100202 mutex_unlock(&data->update_lock);
Grant Coadyc7461a62005-09-17 05:32:57 +1000203 return count;
204}
205
Grant Coady40b5cda2005-04-30 21:41:29 +1000206static DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL);
Grant Coadyc7461a62005-09-17 05:32:57 +1000207static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO,
208 show_max, set_max, 0);
209static SENSOR_DEVICE_ATTR(temp1_max_hyst, S_IWUSR | S_IRUGO,
210 show_max, set_max, 1);
Grant Coady40b5cda2005-04-30 21:41:29 +1000211
212/* voltage */
Grant Coadyc7461a62005-09-17 05:32:57 +1000213static ssize_t show_in(struct device *dev, struct device_attribute *devattr,
214 char *buf)
Grant Coady40b5cda2005-04-30 21:41:29 +1000215{
Grant Coadyc7461a62005-09-17 05:32:57 +1000216 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
Grant Coady40b5cda2005-04-30 21:41:29 +1000217 struct adm9240_data *data = adm9240_update_device(dev);
Grant Coadyc7461a62005-09-17 05:32:57 +1000218 return sprintf(buf, "%d\n", IN_FROM_REG(data->in[attr->index],
219 attr->index));
Grant Coady40b5cda2005-04-30 21:41:29 +1000220}
221
Jean Delvaree415e482005-09-25 16:14:18 +0200222static ssize_t show_in_min(struct device *dev,
223 struct device_attribute *devattr, char *buf)
Grant Coady40b5cda2005-04-30 21:41:29 +1000224{
Grant Coadyc7461a62005-09-17 05:32:57 +1000225 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
Grant Coady40b5cda2005-04-30 21:41:29 +1000226 struct adm9240_data *data = adm9240_update_device(dev);
Grant Coadyc7461a62005-09-17 05:32:57 +1000227 return sprintf(buf, "%d\n", IN_FROM_REG(data->in_min[attr->index],
228 attr->index));
Grant Coady40b5cda2005-04-30 21:41:29 +1000229}
230
Jean Delvaree415e482005-09-25 16:14:18 +0200231static ssize_t show_in_max(struct device *dev,
232 struct device_attribute *devattr, char *buf)
Grant Coady40b5cda2005-04-30 21:41:29 +1000233{
Grant Coadyc7461a62005-09-17 05:32:57 +1000234 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
Grant Coady40b5cda2005-04-30 21:41:29 +1000235 struct adm9240_data *data = adm9240_update_device(dev);
Grant Coadyc7461a62005-09-17 05:32:57 +1000236 return sprintf(buf, "%d\n", IN_FROM_REG(data->in_max[attr->index],
237 attr->index));
Grant Coady40b5cda2005-04-30 21:41:29 +1000238}
239
Jean Delvaree415e482005-09-25 16:14:18 +0200240static ssize_t set_in_min(struct device *dev,
241 struct device_attribute *devattr,
Grant Coadyc7461a62005-09-17 05:32:57 +1000242 const char *buf, size_t count)
Grant Coady40b5cda2005-04-30 21:41:29 +1000243{
Grant Coadyc7461a62005-09-17 05:32:57 +1000244 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
Grant Coady40b5cda2005-04-30 21:41:29 +1000245 struct i2c_client *client = to_i2c_client(dev);
246 struct adm9240_data *data = i2c_get_clientdata(client);
247 unsigned long val = simple_strtoul(buf, NULL, 10);
248
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100249 mutex_lock(&data->update_lock);
Grant Coadyc7461a62005-09-17 05:32:57 +1000250 data->in_min[attr->index] = IN_TO_REG(val, attr->index);
251 i2c_smbus_write_byte_data(client, ADM9240_REG_IN_MIN(attr->index),
252 data->in_min[attr->index]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100253 mutex_unlock(&data->update_lock);
Grant Coady40b5cda2005-04-30 21:41:29 +1000254 return count;
255}
256
Jean Delvaree415e482005-09-25 16:14:18 +0200257static ssize_t set_in_max(struct device *dev,
258 struct device_attribute *devattr,
Grant Coadyc7461a62005-09-17 05:32:57 +1000259 const char *buf, size_t count)
Grant Coady40b5cda2005-04-30 21:41:29 +1000260{
Grant Coadyc7461a62005-09-17 05:32:57 +1000261 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
Grant Coady40b5cda2005-04-30 21:41:29 +1000262 struct i2c_client *client = to_i2c_client(dev);
263 struct adm9240_data *data = i2c_get_clientdata(client);
264 unsigned long val = simple_strtoul(buf, NULL, 10);
265
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100266 mutex_lock(&data->update_lock);
Grant Coadyc7461a62005-09-17 05:32:57 +1000267 data->in_max[attr->index] = IN_TO_REG(val, attr->index);
268 i2c_smbus_write_byte_data(client, ADM9240_REG_IN_MAX(attr->index),
269 data->in_max[attr->index]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100270 mutex_unlock(&data->update_lock);
Grant Coady40b5cda2005-04-30 21:41:29 +1000271 return count;
272}
273
Grant Coadyc7461a62005-09-17 05:32:57 +1000274#define vin(nr) \
275static SENSOR_DEVICE_ATTR(in##nr##_input, S_IRUGO, \
276 show_in, NULL, nr); \
277static SENSOR_DEVICE_ATTR(in##nr##_min, S_IRUGO | S_IWUSR, \
278 show_in_min, set_in_min, nr); \
279static SENSOR_DEVICE_ATTR(in##nr##_max, S_IRUGO | S_IWUSR, \
280 show_in_max, set_in_max, nr);
Grant Coady40b5cda2005-04-30 21:41:29 +1000281
Grant Coadyc7461a62005-09-17 05:32:57 +1000282vin(0);
283vin(1);
284vin(2);
285vin(3);
286vin(4);
287vin(5);
Grant Coady40b5cda2005-04-30 21:41:29 +1000288
289/* fans */
Grant Coadyc7461a62005-09-17 05:32:57 +1000290static ssize_t show_fan(struct device *dev,
291 struct device_attribute *devattr, char *buf)
Grant Coady40b5cda2005-04-30 21:41:29 +1000292{
Grant Coadyc7461a62005-09-17 05:32:57 +1000293 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
Grant Coady40b5cda2005-04-30 21:41:29 +1000294 struct adm9240_data *data = adm9240_update_device(dev);
Grant Coadyc7461a62005-09-17 05:32:57 +1000295 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[attr->index],
296 1 << data->fan_div[attr->index]));
Grant Coady40b5cda2005-04-30 21:41:29 +1000297}
298
Grant Coadyc7461a62005-09-17 05:32:57 +1000299static ssize_t show_fan_min(struct device *dev,
300 struct device_attribute *devattr, char *buf)
Grant Coady40b5cda2005-04-30 21:41:29 +1000301{
Grant Coadyc7461a62005-09-17 05:32:57 +1000302 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
Grant Coady40b5cda2005-04-30 21:41:29 +1000303 struct adm9240_data *data = adm9240_update_device(dev);
Grant Coadyc7461a62005-09-17 05:32:57 +1000304 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_min[attr->index],
305 1 << data->fan_div[attr->index]));
Grant Coady40b5cda2005-04-30 21:41:29 +1000306}
307
Grant Coadyc7461a62005-09-17 05:32:57 +1000308static ssize_t show_fan_div(struct device *dev,
309 struct device_attribute *devattr, char *buf)
Grant Coady40b5cda2005-04-30 21:41:29 +1000310{
Grant Coadyc7461a62005-09-17 05:32:57 +1000311 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
Grant Coady40b5cda2005-04-30 21:41:29 +1000312 struct adm9240_data *data = adm9240_update_device(dev);
Grant Coadyc7461a62005-09-17 05:32:57 +1000313 return sprintf(buf, "%d\n", 1 << data->fan_div[attr->index]);
Grant Coady40b5cda2005-04-30 21:41:29 +1000314}
315
316/* write new fan div, callers must hold data->update_lock */
317static void adm9240_write_fan_div(struct i2c_client *client, int nr,
318 u8 fan_div)
319{
320 u8 reg, old, shift = (nr + 2) * 2;
321
Grant Coady205cf132005-09-17 05:32:55 +1000322 reg = i2c_smbus_read_byte_data(client, ADM9240_REG_VID_FAN_DIV);
Grant Coady40b5cda2005-04-30 21:41:29 +1000323 old = (reg >> shift) & 3;
324 reg &= ~(3 << shift);
325 reg |= (fan_div << shift);
Grant Coady205cf132005-09-17 05:32:55 +1000326 i2c_smbus_write_byte_data(client, ADM9240_REG_VID_FAN_DIV, reg);
Grant Coady40b5cda2005-04-30 21:41:29 +1000327 dev_dbg(&client->dev, "fan%d clock divider changed from %u "
328 "to %u\n", nr + 1, 1 << old, 1 << fan_div);
329}
330
Jean Delvaree415e482005-09-25 16:14:18 +0200331/*
Grant Coady40b5cda2005-04-30 21:41:29 +1000332 * set fan speed low limit:
333 *
334 * - value is zero: disable fan speed low limit alarm
335 *
336 * - value is below fan speed measurement range: enable fan speed low
337 * limit alarm to be asserted while fan speed too slow to measure
338 *
339 * - otherwise: select fan clock divider to suit fan speed low limit,
340 * measurement code may adjust registers to ensure fan speed reading
341 */
Grant Coadyc7461a62005-09-17 05:32:57 +1000342static ssize_t set_fan_min(struct device *dev,
343 struct device_attribute *devattr,
344 const char *buf, size_t count)
Grant Coady40b5cda2005-04-30 21:41:29 +1000345{
Grant Coadyc7461a62005-09-17 05:32:57 +1000346 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
Grant Coady40b5cda2005-04-30 21:41:29 +1000347 struct i2c_client *client = to_i2c_client(dev);
348 struct adm9240_data *data = i2c_get_clientdata(client);
349 unsigned long val = simple_strtoul(buf, NULL, 10);
Grant Coadyc7461a62005-09-17 05:32:57 +1000350 int nr = attr->index;
Grant Coady40b5cda2005-04-30 21:41:29 +1000351 u8 new_div;
352
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100353 mutex_lock(&data->update_lock);
Grant Coady40b5cda2005-04-30 21:41:29 +1000354
355 if (!val) {
356 data->fan_min[nr] = 255;
357 new_div = data->fan_div[nr];
358
359 dev_dbg(&client->dev, "fan%u low limit set disabled\n",
360 nr + 1);
361
362 } else if (val < 1350000 / (8 * 254)) {
363 new_div = 3;
364 data->fan_min[nr] = 254;
365
366 dev_dbg(&client->dev, "fan%u low limit set minimum %u\n",
367 nr + 1, FAN_FROM_REG(254, 1 << new_div));
368
369 } else {
370 unsigned int new_min = 1350000 / val;
371
372 new_div = 0;
373 while (new_min > 192 && new_div < 3) {
374 new_div++;
375 new_min /= 2;
376 }
377 if (!new_min) /* keep > 0 */
378 new_min++;
379
380 data->fan_min[nr] = new_min;
381
382 dev_dbg(&client->dev, "fan%u low limit set fan speed %u\n",
383 nr + 1, FAN_FROM_REG(new_min, 1 << new_div));
384 }
385
386 if (new_div != data->fan_div[nr]) {
387 data->fan_div[nr] = new_div;
388 adm9240_write_fan_div(client, nr, new_div);
389 }
Grant Coady205cf132005-09-17 05:32:55 +1000390 i2c_smbus_write_byte_data(client, ADM9240_REG_FAN_MIN(nr),
Grant Coady40b5cda2005-04-30 21:41:29 +1000391 data->fan_min[nr]);
392
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100393 mutex_unlock(&data->update_lock);
Grant Coady40b5cda2005-04-30 21:41:29 +1000394 return count;
395}
396
Grant Coadyc7461a62005-09-17 05:32:57 +1000397#define fan(nr) \
398static SENSOR_DEVICE_ATTR(fan##nr##_input, S_IRUGO, \
399 show_fan, NULL, nr - 1); \
400static SENSOR_DEVICE_ATTR(fan##nr##_div, S_IRUGO, \
401 show_fan_div, NULL, nr - 1); \
402static SENSOR_DEVICE_ATTR(fan##nr##_min, S_IRUGO | S_IWUSR, \
403 show_fan_min, set_fan_min, nr - 1);
Grant Coady40b5cda2005-04-30 21:41:29 +1000404
Grant Coadyc7461a62005-09-17 05:32:57 +1000405fan(1);
406fan(2);
Grant Coady40b5cda2005-04-30 21:41:29 +1000407
408/* alarms */
Jean Delvaree415e482005-09-25 16:14:18 +0200409static ssize_t show_alarms(struct device *dev,
410 struct device_attribute *attr, char *buf)
Grant Coady40b5cda2005-04-30 21:41:29 +1000411{
412 struct adm9240_data *data = adm9240_update_device(dev);
413 return sprintf(buf, "%u\n", data->alarms);
414}
415static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
416
Jean Delvare360f9452008-01-06 15:49:19 +0100417static ssize_t show_alarm(struct device *dev,
418 struct device_attribute *attr, char *buf)
419{
420 int bitnr = to_sensor_dev_attr(attr)->index;
421 struct adm9240_data *data = adm9240_update_device(dev);
422 return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1);
423}
424static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 0);
425static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 1);
426static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 2);
427static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 3);
428static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 8);
429static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 9);
430static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 4);
431static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 6);
432static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 7);
433
Grant Coady40b5cda2005-04-30 21:41:29 +1000434/* vid */
Jean Delvaree415e482005-09-25 16:14:18 +0200435static ssize_t show_vid(struct device *dev,
436 struct device_attribute *attr, char *buf)
Grant Coady40b5cda2005-04-30 21:41:29 +1000437{
438 struct adm9240_data *data = adm9240_update_device(dev);
439 return sprintf(buf, "%d\n", vid_from_reg(data->vid, data->vrm));
440}
441static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid, NULL);
442
443/* analog output */
Jean Delvaree415e482005-09-25 16:14:18 +0200444static ssize_t show_aout(struct device *dev,
445 struct device_attribute *attr, char *buf)
Grant Coady40b5cda2005-04-30 21:41:29 +1000446{
447 struct adm9240_data *data = adm9240_update_device(dev);
448 return sprintf(buf, "%d\n", AOUT_FROM_REG(data->aout));
449}
450
Jean Delvaree415e482005-09-25 16:14:18 +0200451static ssize_t set_aout(struct device *dev,
452 struct device_attribute *attr,
453 const char *buf, size_t count)
Grant Coady40b5cda2005-04-30 21:41:29 +1000454{
455 struct i2c_client *client = to_i2c_client(dev);
456 struct adm9240_data *data = i2c_get_clientdata(client);
457 unsigned long val = simple_strtol(buf, NULL, 10);
458
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100459 mutex_lock(&data->update_lock);
Grant Coady40b5cda2005-04-30 21:41:29 +1000460 data->aout = AOUT_TO_REG(val);
Grant Coady205cf132005-09-17 05:32:55 +1000461 i2c_smbus_write_byte_data(client, ADM9240_REG_ANALOG_OUT, data->aout);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100462 mutex_unlock(&data->update_lock);
Grant Coady40b5cda2005-04-30 21:41:29 +1000463 return count;
464}
465static DEVICE_ATTR(aout_output, S_IRUGO | S_IWUSR, show_aout, set_aout);
466
467/* chassis_clear */
Jean Delvaree415e482005-09-25 16:14:18 +0200468static ssize_t chassis_clear(struct device *dev,
469 struct device_attribute *attr,
470 const char *buf, size_t count)
Grant Coady40b5cda2005-04-30 21:41:29 +1000471{
472 struct i2c_client *client = to_i2c_client(dev);
473 unsigned long val = simple_strtol(buf, NULL, 10);
474
475 if (val == 1) {
Grant Coady205cf132005-09-17 05:32:55 +1000476 i2c_smbus_write_byte_data(client,
477 ADM9240_REG_CHASSIS_CLEAR, 0x80);
Grant Coady40b5cda2005-04-30 21:41:29 +1000478 dev_dbg(&client->dev, "chassis intrusion latch cleared\n");
479 }
480 return count;
481}
482static DEVICE_ATTR(chassis_clear, S_IWUSR, NULL, chassis_clear);
483
Mark M. Hoffman681c6f72006-09-24 21:15:35 +0200484static struct attribute *adm9240_attributes[] = {
485 &sensor_dev_attr_in0_input.dev_attr.attr,
486 &sensor_dev_attr_in0_min.dev_attr.attr,
487 &sensor_dev_attr_in0_max.dev_attr.attr,
Jean Delvare360f9452008-01-06 15:49:19 +0100488 &sensor_dev_attr_in0_alarm.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +0200489 &sensor_dev_attr_in1_input.dev_attr.attr,
490 &sensor_dev_attr_in1_min.dev_attr.attr,
491 &sensor_dev_attr_in1_max.dev_attr.attr,
Jean Delvare360f9452008-01-06 15:49:19 +0100492 &sensor_dev_attr_in1_alarm.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +0200493 &sensor_dev_attr_in2_input.dev_attr.attr,
494 &sensor_dev_attr_in2_min.dev_attr.attr,
495 &sensor_dev_attr_in2_max.dev_attr.attr,
Jean Delvare360f9452008-01-06 15:49:19 +0100496 &sensor_dev_attr_in2_alarm.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +0200497 &sensor_dev_attr_in3_input.dev_attr.attr,
498 &sensor_dev_attr_in3_min.dev_attr.attr,
499 &sensor_dev_attr_in3_max.dev_attr.attr,
Jean Delvare360f9452008-01-06 15:49:19 +0100500 &sensor_dev_attr_in3_alarm.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +0200501 &sensor_dev_attr_in4_input.dev_attr.attr,
502 &sensor_dev_attr_in4_min.dev_attr.attr,
503 &sensor_dev_attr_in4_max.dev_attr.attr,
Jean Delvare360f9452008-01-06 15:49:19 +0100504 &sensor_dev_attr_in4_alarm.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +0200505 &sensor_dev_attr_in5_input.dev_attr.attr,
506 &sensor_dev_attr_in5_min.dev_attr.attr,
507 &sensor_dev_attr_in5_max.dev_attr.attr,
Jean Delvare360f9452008-01-06 15:49:19 +0100508 &sensor_dev_attr_in5_alarm.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +0200509 &dev_attr_temp1_input.attr,
510 &sensor_dev_attr_temp1_max.dev_attr.attr,
511 &sensor_dev_attr_temp1_max_hyst.dev_attr.attr,
Jean Delvare360f9452008-01-06 15:49:19 +0100512 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +0200513 &sensor_dev_attr_fan1_input.dev_attr.attr,
514 &sensor_dev_attr_fan1_div.dev_attr.attr,
515 &sensor_dev_attr_fan1_min.dev_attr.attr,
Jean Delvare360f9452008-01-06 15:49:19 +0100516 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +0200517 &sensor_dev_attr_fan2_input.dev_attr.attr,
518 &sensor_dev_attr_fan2_div.dev_attr.attr,
519 &sensor_dev_attr_fan2_min.dev_attr.attr,
Jean Delvare360f9452008-01-06 15:49:19 +0100520 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +0200521 &dev_attr_alarms.attr,
522 &dev_attr_aout_output.attr,
523 &dev_attr_chassis_clear.attr,
524 &dev_attr_cpu0_vid.attr,
525 NULL
526};
527
528static const struct attribute_group adm9240_group = {
529 .attrs = adm9240_attributes,
530};
531
Grant Coady40b5cda2005-04-30 21:41:29 +1000532
533/*** sensor chip detect and driver install ***/
534
535static int adm9240_detect(struct i2c_adapter *adapter, int address, int kind)
536{
537 struct i2c_client *new_client;
538 struct adm9240_data *data;
539 int err = 0;
540 const char *name = "";
541 u8 man_id, die_rev;
542
543 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
544 goto exit;
545
Grant Coady205cf132005-09-17 05:32:55 +1000546 if (!(data = kzalloc(sizeof(*data), GFP_KERNEL))) {
Grant Coady40b5cda2005-04-30 21:41:29 +1000547 err = -ENOMEM;
548 goto exit;
549 }
Grant Coady40b5cda2005-04-30 21:41:29 +1000550
551 new_client = &data->client;
552 i2c_set_clientdata(new_client, data);
553 new_client->addr = address;
554 new_client->adapter = adapter;
555 new_client->driver = &adm9240_driver;
556 new_client->flags = 0;
557
558 if (kind == 0) {
559 kind = adm9240;
560 }
561
562 if (kind < 0) {
563
564 /* verify chip: reg address should match i2c address */
Grant Coady205cf132005-09-17 05:32:55 +1000565 if (i2c_smbus_read_byte_data(new_client, ADM9240_REG_I2C_ADDR)
Grant Coady40b5cda2005-04-30 21:41:29 +1000566 != address) {
567 dev_err(&adapter->dev, "detect fail: address match, "
568 "0x%02x\n", address);
569 goto exit_free;
570 }
571
572 /* check known chip manufacturer */
Grant Coady205cf132005-09-17 05:32:55 +1000573 man_id = i2c_smbus_read_byte_data(new_client,
574 ADM9240_REG_MAN_ID);
Grant Coady40b5cda2005-04-30 21:41:29 +1000575 if (man_id == 0x23) {
576 kind = adm9240;
577 } else if (man_id == 0xda) {
578 kind = ds1780;
579 } else if (man_id == 0x01) {
580 kind = lm81;
581 } else {
582 dev_err(&adapter->dev, "detect fail: unknown manuf, "
583 "0x%02x\n", man_id);
584 goto exit_free;
585 }
586
587 /* successful detect, print chip info */
Grant Coady205cf132005-09-17 05:32:55 +1000588 die_rev = i2c_smbus_read_byte_data(new_client,
589 ADM9240_REG_DIE_REV);
Grant Coady40b5cda2005-04-30 21:41:29 +1000590 dev_info(&adapter->dev, "found %s revision %u\n",
591 man_id == 0x23 ? "ADM9240" :
592 man_id == 0xda ? "DS1780" : "LM81", die_rev);
593 }
594
595 /* either forced or detected chip kind */
596 if (kind == adm9240) {
597 name = "adm9240";
598 } else if (kind == ds1780) {
599 name = "ds1780";
600 } else if (kind == lm81) {
601 name = "lm81";
602 }
603
604 /* fill in the remaining client fields and attach */
605 strlcpy(new_client->name, name, I2C_NAME_SIZE);
606 data->type = kind;
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100607 mutex_init(&data->update_lock);
Grant Coady40b5cda2005-04-30 21:41:29 +1000608
609 if ((err = i2c_attach_client(new_client)))
610 goto exit_free;
611
612 adm9240_init_client(new_client);
613
614 /* populate sysfs filesystem */
Mark M. Hoffman681c6f72006-09-24 21:15:35 +0200615 if ((err = sysfs_create_group(&new_client->dev.kobj, &adm9240_group)))
616 goto exit_detach;
617
Tony Jones1beeffe2007-08-20 13:46:20 -0700618 data->hwmon_dev = hwmon_device_register(&new_client->dev);
619 if (IS_ERR(data->hwmon_dev)) {
620 err = PTR_ERR(data->hwmon_dev);
Mark M. Hoffman681c6f72006-09-24 21:15:35 +0200621 goto exit_remove;
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400622 }
623
Grant Coady40b5cda2005-04-30 21:41:29 +1000624 return 0;
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400625
Mark M. Hoffman681c6f72006-09-24 21:15:35 +0200626exit_remove:
627 sysfs_remove_group(&new_client->dev.kobj, &adm9240_group);
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400628exit_detach:
629 i2c_detach_client(new_client);
Grant Coady40b5cda2005-04-30 21:41:29 +1000630exit_free:
Alexey Dobriyan1f57ff82005-08-26 01:49:14 +0400631 kfree(data);
Grant Coady40b5cda2005-04-30 21:41:29 +1000632exit:
633 return err;
634}
635
636static int adm9240_attach_adapter(struct i2c_adapter *adapter)
637{
638 if (!(adapter->class & I2C_CLASS_HWMON))
639 return 0;
Jean Delvare2ed2dc32005-07-31 21:42:02 +0200640 return i2c_probe(adapter, &addr_data, adm9240_detect);
Grant Coady40b5cda2005-04-30 21:41:29 +1000641}
642
643static int adm9240_detach_client(struct i2c_client *client)
644{
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400645 struct adm9240_data *data = i2c_get_clientdata(client);
Grant Coady40b5cda2005-04-30 21:41:29 +1000646 int err;
647
Tony Jones1beeffe2007-08-20 13:46:20 -0700648 hwmon_device_unregister(data->hwmon_dev);
Mark M. Hoffman681c6f72006-09-24 21:15:35 +0200649 sysfs_remove_group(&client->dev.kobj, &adm9240_group);
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400650
Jean Delvare7bef5592005-07-27 22:14:49 +0200651 if ((err = i2c_detach_client(client)))
Grant Coady40b5cda2005-04-30 21:41:29 +1000652 return err;
Grant Coady40b5cda2005-04-30 21:41:29 +1000653
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400654 kfree(data);
Grant Coady40b5cda2005-04-30 21:41:29 +1000655 return 0;
656}
657
658static void adm9240_init_client(struct i2c_client *client)
659{
660 struct adm9240_data *data = i2c_get_clientdata(client);
Grant Coady205cf132005-09-17 05:32:55 +1000661 u8 conf = i2c_smbus_read_byte_data(client, ADM9240_REG_CONFIG);
662 u8 mode = i2c_smbus_read_byte_data(client, ADM9240_REG_TEMP_CONF) & 3;
Grant Coady40b5cda2005-04-30 21:41:29 +1000663
Jean Delvare303760b2005-07-31 21:52:01 +0200664 data->vrm = vid_which_vrm(); /* need this to report vid as mV */
Grant Coady40b5cda2005-04-30 21:41:29 +1000665
Grant Coady8e8f9282005-05-13 20:26:10 +1000666 dev_info(&client->dev, "Using VRM: %d.%d\n", data->vrm / 10,
667 data->vrm % 10);
668
Grant Coady40b5cda2005-04-30 21:41:29 +1000669 if (conf & 1) { /* measurement cycle running: report state */
670
671 dev_info(&client->dev, "status: config 0x%02x mode %u\n",
672 conf, mode);
673
674 } else { /* cold start: open limits before starting chip */
675 int i;
676
677 for (i = 0; i < 6; i++)
678 {
Grant Coady205cf132005-09-17 05:32:55 +1000679 i2c_smbus_write_byte_data(client,
Grant Coady40b5cda2005-04-30 21:41:29 +1000680 ADM9240_REG_IN_MIN(i), 0);
Grant Coady205cf132005-09-17 05:32:55 +1000681 i2c_smbus_write_byte_data(client,
Grant Coady40b5cda2005-04-30 21:41:29 +1000682 ADM9240_REG_IN_MAX(i), 255);
683 }
Grant Coady205cf132005-09-17 05:32:55 +1000684 i2c_smbus_write_byte_data(client,
685 ADM9240_REG_FAN_MIN(0), 255);
686 i2c_smbus_write_byte_data(client,
687 ADM9240_REG_FAN_MIN(1), 255);
688 i2c_smbus_write_byte_data(client,
Grant Coadyc7461a62005-09-17 05:32:57 +1000689 ADM9240_REG_TEMP_MAX(0), 127);
Grant Coady205cf132005-09-17 05:32:55 +1000690 i2c_smbus_write_byte_data(client,
Grant Coadyc7461a62005-09-17 05:32:57 +1000691 ADM9240_REG_TEMP_MAX(1), 127);
Grant Coady40b5cda2005-04-30 21:41:29 +1000692
693 /* start measurement cycle */
Grant Coady205cf132005-09-17 05:32:55 +1000694 i2c_smbus_write_byte_data(client, ADM9240_REG_CONFIG, 1);
Grant Coady40b5cda2005-04-30 21:41:29 +1000695
696 dev_info(&client->dev, "cold start: config was 0x%02x "
697 "mode %u\n", conf, mode);
698 }
699}
700
701static struct adm9240_data *adm9240_update_device(struct device *dev)
702{
703 struct i2c_client *client = to_i2c_client(dev);
704 struct adm9240_data *data = i2c_get_clientdata(client);
705 int i;
706
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100707 mutex_lock(&data->update_lock);
Grant Coady40b5cda2005-04-30 21:41:29 +1000708
709 /* minimum measurement cycle: 1.75 seconds */
710 if (time_after(jiffies, data->last_updated_measure + (HZ * 7 / 4))
711 || !data->valid) {
712
713 for (i = 0; i < 6; i++) /* read voltages */
714 {
Grant Coady205cf132005-09-17 05:32:55 +1000715 data->in[i] = i2c_smbus_read_byte_data(client,
Grant Coady40b5cda2005-04-30 21:41:29 +1000716 ADM9240_REG_IN(i));
717 }
Grant Coady205cf132005-09-17 05:32:55 +1000718 data->alarms = i2c_smbus_read_byte_data(client,
Grant Coady40b5cda2005-04-30 21:41:29 +1000719 ADM9240_REG_INT(0)) |
Grant Coady205cf132005-09-17 05:32:55 +1000720 i2c_smbus_read_byte_data(client,
Grant Coady40b5cda2005-04-30 21:41:29 +1000721 ADM9240_REG_INT(1)) << 8;
722
723 /* read temperature: assume temperature changes less than
724 * 0.5'C per two measurement cycles thus ignore possible
725 * but unlikely aliasing error on lsb reading. --Grant */
Grant Coady205cf132005-09-17 05:32:55 +1000726 data->temp = ((i2c_smbus_read_byte_data(client,
Grant Coady40b5cda2005-04-30 21:41:29 +1000727 ADM9240_REG_TEMP) << 8) |
Grant Coady205cf132005-09-17 05:32:55 +1000728 i2c_smbus_read_byte_data(client,
Grant Coady40b5cda2005-04-30 21:41:29 +1000729 ADM9240_REG_TEMP_CONF)) / 128;
730
731 for (i = 0; i < 2; i++) /* read fans */
732 {
Grant Coady205cf132005-09-17 05:32:55 +1000733 data->fan[i] = i2c_smbus_read_byte_data(client,
Grant Coady40b5cda2005-04-30 21:41:29 +1000734 ADM9240_REG_FAN(i));
735
736 /* adjust fan clock divider on overflow */
737 if (data->valid && data->fan[i] == 255 &&
738 data->fan_div[i] < 3) {
739
740 adm9240_write_fan_div(client, i,
741 ++data->fan_div[i]);
742
743 /* adjust fan_min if active, but not to 0 */
744 if (data->fan_min[i] < 255 &&
745 data->fan_min[i] >= 2)
746 data->fan_min[i] /= 2;
747 }
748 }
749 data->last_updated_measure = jiffies;
750 }
751
752 /* minimum config reading cycle: 300 seconds */
753 if (time_after(jiffies, data->last_updated_config + (HZ * 300))
754 || !data->valid) {
755
756 for (i = 0; i < 6; i++)
757 {
Grant Coady205cf132005-09-17 05:32:55 +1000758 data->in_min[i] = i2c_smbus_read_byte_data(client,
Grant Coady40b5cda2005-04-30 21:41:29 +1000759 ADM9240_REG_IN_MIN(i));
Grant Coady205cf132005-09-17 05:32:55 +1000760 data->in_max[i] = i2c_smbus_read_byte_data(client,
Grant Coady40b5cda2005-04-30 21:41:29 +1000761 ADM9240_REG_IN_MAX(i));
762 }
763 for (i = 0; i < 2; i++)
764 {
Grant Coady205cf132005-09-17 05:32:55 +1000765 data->fan_min[i] = i2c_smbus_read_byte_data(client,
Grant Coady40b5cda2005-04-30 21:41:29 +1000766 ADM9240_REG_FAN_MIN(i));
767 }
Grant Coadyc7461a62005-09-17 05:32:57 +1000768 data->temp_max[0] = i2c_smbus_read_byte_data(client,
769 ADM9240_REG_TEMP_MAX(0));
770 data->temp_max[1] = i2c_smbus_read_byte_data(client,
771 ADM9240_REG_TEMP_MAX(1));
Grant Coady40b5cda2005-04-30 21:41:29 +1000772
773 /* read fan divs and 5-bit VID */
Grant Coady205cf132005-09-17 05:32:55 +1000774 i = i2c_smbus_read_byte_data(client, ADM9240_REG_VID_FAN_DIV);
Grant Coady40b5cda2005-04-30 21:41:29 +1000775 data->fan_div[0] = (i >> 4) & 3;
776 data->fan_div[1] = (i >> 6) & 3;
777 data->vid = i & 0x0f;
Grant Coady205cf132005-09-17 05:32:55 +1000778 data->vid |= (i2c_smbus_read_byte_data(client,
Grant Coady40b5cda2005-04-30 21:41:29 +1000779 ADM9240_REG_VID4) & 1) << 4;
780 /* read analog out */
Grant Coady205cf132005-09-17 05:32:55 +1000781 data->aout = i2c_smbus_read_byte_data(client,
Grant Coady40b5cda2005-04-30 21:41:29 +1000782 ADM9240_REG_ANALOG_OUT);
783
784 data->last_updated_config = jiffies;
785 data->valid = 1;
786 }
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100787 mutex_unlock(&data->update_lock);
Grant Coady40b5cda2005-04-30 21:41:29 +1000788 return data;
789}
790
791static int __init sensors_adm9240_init(void)
792{
793 return i2c_add_driver(&adm9240_driver);
794}
795
796static void __exit sensors_adm9240_exit(void)
797{
798 i2c_del_driver(&adm9240_driver);
799}
800
801MODULE_AUTHOR("Michiel Rook <michiel@grendelproject.nl>, "
Grant Coady2ca7b962006-10-08 21:57:41 +0200802 "Grant Coady <gcoady.lk@gmail.com> and others");
Grant Coady40b5cda2005-04-30 21:41:29 +1000803MODULE_DESCRIPTION("ADM9240/DS1780/LM81 driver");
804MODULE_LICENSE("GPL");
805
806module_init(sensors_adm9240_init);
807module_exit(sensors_adm9240_exit);
808