blob: 5ddc22fea4a37e665180a3591a680e0c5f62f0c0 [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>
8 * Copyright (C) 2005 Grant Coady <gcoady@gmail.com> with valuable
9 * 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>
Grant Coady40b5cda2005-04-30 21:41:29 +100052
53/* Addresses to scan */
54static unsigned short normal_i2c[] = { 0x2c, 0x2d, 0x2e, 0x2f,
55 I2C_CLIENT_END };
56
Grant Coady40b5cda2005-04-30 21:41:29 +100057/* Insmod parameters */
Jean Delvaref4b50262005-07-31 21:49:03 +020058I2C_CLIENT_INSMOD_3(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
132static int adm9240_attach_adapter(struct i2c_adapter *adapter);
133static int adm9240_detect(struct i2c_adapter *adapter, int address, int kind);
134static void adm9240_init_client(struct i2c_client *client);
135static int adm9240_detach_client(struct i2c_client *client);
136static struct adm9240_data *adm9240_update_device(struct device *dev);
137
138/* driver data */
139static struct i2c_driver adm9240_driver = {
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100140 .driver = {
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100141 .name = "adm9240",
142 },
Grant Coady40b5cda2005-04-30 21:41:29 +1000143 .id = I2C_DRIVERID_ADM9240,
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;
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400152 struct class_device *class_dev;
Grant Coady40b5cda2005-04-30 21:41:29 +1000153 struct semaphore update_lock;
154 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
198 down(&data->update_lock);
199 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]);
202 up(&data->update_lock);
203 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
249 down(&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]);
Grant Coady40b5cda2005-04-30 21:41:29 +1000253 up(&data->update_lock);
254 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
266 down(&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]);
Grant Coady40b5cda2005-04-30 21:41:29 +1000270 up(&data->update_lock);
271 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
353 down(&data->update_lock);
354
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
393 up(&data->update_lock);
394 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
417/* vid */
Jean Delvaree415e482005-09-25 16:14:18 +0200418static ssize_t show_vid(struct device *dev,
419 struct device_attribute *attr, char *buf)
Grant Coady40b5cda2005-04-30 21:41:29 +1000420{
421 struct adm9240_data *data = adm9240_update_device(dev);
422 return sprintf(buf, "%d\n", vid_from_reg(data->vid, data->vrm));
423}
424static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid, NULL);
425
426/* analog output */
Jean Delvaree415e482005-09-25 16:14:18 +0200427static ssize_t show_aout(struct device *dev,
428 struct device_attribute *attr, char *buf)
Grant Coady40b5cda2005-04-30 21:41:29 +1000429{
430 struct adm9240_data *data = adm9240_update_device(dev);
431 return sprintf(buf, "%d\n", AOUT_FROM_REG(data->aout));
432}
433
Jean Delvaree415e482005-09-25 16:14:18 +0200434static ssize_t set_aout(struct device *dev,
435 struct device_attribute *attr,
436 const char *buf, size_t count)
Grant Coady40b5cda2005-04-30 21:41:29 +1000437{
438 struct i2c_client *client = to_i2c_client(dev);
439 struct adm9240_data *data = i2c_get_clientdata(client);
440 unsigned long val = simple_strtol(buf, NULL, 10);
441
442 down(&data->update_lock);
443 data->aout = AOUT_TO_REG(val);
Grant Coady205cf132005-09-17 05:32:55 +1000444 i2c_smbus_write_byte_data(client, ADM9240_REG_ANALOG_OUT, data->aout);
Grant Coady40b5cda2005-04-30 21:41:29 +1000445 up(&data->update_lock);
446 return count;
447}
448static DEVICE_ATTR(aout_output, S_IRUGO | S_IWUSR, show_aout, set_aout);
449
450/* chassis_clear */
Jean Delvaree415e482005-09-25 16:14:18 +0200451static ssize_t chassis_clear(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 unsigned long val = simple_strtol(buf, NULL, 10);
457
458 if (val == 1) {
Grant Coady205cf132005-09-17 05:32:55 +1000459 i2c_smbus_write_byte_data(client,
460 ADM9240_REG_CHASSIS_CLEAR, 0x80);
Grant Coady40b5cda2005-04-30 21:41:29 +1000461 dev_dbg(&client->dev, "chassis intrusion latch cleared\n");
462 }
463 return count;
464}
465static DEVICE_ATTR(chassis_clear, S_IWUSR, NULL, chassis_clear);
466
467
468/*** sensor chip detect and driver install ***/
469
470static int adm9240_detect(struct i2c_adapter *adapter, int address, int kind)
471{
472 struct i2c_client *new_client;
473 struct adm9240_data *data;
474 int err = 0;
475 const char *name = "";
476 u8 man_id, die_rev;
477
478 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
479 goto exit;
480
Grant Coady205cf132005-09-17 05:32:55 +1000481 if (!(data = kzalloc(sizeof(*data), GFP_KERNEL))) {
Grant Coady40b5cda2005-04-30 21:41:29 +1000482 err = -ENOMEM;
483 goto exit;
484 }
Grant Coady40b5cda2005-04-30 21:41:29 +1000485
486 new_client = &data->client;
487 i2c_set_clientdata(new_client, data);
488 new_client->addr = address;
489 new_client->adapter = adapter;
490 new_client->driver = &adm9240_driver;
491 new_client->flags = 0;
492
493 if (kind == 0) {
494 kind = adm9240;
495 }
496
497 if (kind < 0) {
498
499 /* verify chip: reg address should match i2c address */
Grant Coady205cf132005-09-17 05:32:55 +1000500 if (i2c_smbus_read_byte_data(new_client, ADM9240_REG_I2C_ADDR)
Grant Coady40b5cda2005-04-30 21:41:29 +1000501 != address) {
502 dev_err(&adapter->dev, "detect fail: address match, "
503 "0x%02x\n", address);
504 goto exit_free;
505 }
506
507 /* check known chip manufacturer */
Grant Coady205cf132005-09-17 05:32:55 +1000508 man_id = i2c_smbus_read_byte_data(new_client,
509 ADM9240_REG_MAN_ID);
Grant Coady40b5cda2005-04-30 21:41:29 +1000510 if (man_id == 0x23) {
511 kind = adm9240;
512 } else if (man_id == 0xda) {
513 kind = ds1780;
514 } else if (man_id == 0x01) {
515 kind = lm81;
516 } else {
517 dev_err(&adapter->dev, "detect fail: unknown manuf, "
518 "0x%02x\n", man_id);
519 goto exit_free;
520 }
521
522 /* successful detect, print chip info */
Grant Coady205cf132005-09-17 05:32:55 +1000523 die_rev = i2c_smbus_read_byte_data(new_client,
524 ADM9240_REG_DIE_REV);
Grant Coady40b5cda2005-04-30 21:41:29 +1000525 dev_info(&adapter->dev, "found %s revision %u\n",
526 man_id == 0x23 ? "ADM9240" :
527 man_id == 0xda ? "DS1780" : "LM81", die_rev);
528 }
529
530 /* either forced or detected chip kind */
531 if (kind == adm9240) {
532 name = "adm9240";
533 } else if (kind == ds1780) {
534 name = "ds1780";
535 } else if (kind == lm81) {
536 name = "lm81";
537 }
538
539 /* fill in the remaining client fields and attach */
540 strlcpy(new_client->name, name, I2C_NAME_SIZE);
541 data->type = kind;
542 init_MUTEX(&data->update_lock);
543
544 if ((err = i2c_attach_client(new_client)))
545 goto exit_free;
546
547 adm9240_init_client(new_client);
548
549 /* populate sysfs filesystem */
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400550 data->class_dev = hwmon_device_register(&new_client->dev);
551 if (IS_ERR(data->class_dev)) {
552 err = PTR_ERR(data->class_dev);
553 goto exit_detach;
554 }
555
Grant Coadyc7461a62005-09-17 05:32:57 +1000556 device_create_file(&new_client->dev,
557 &sensor_dev_attr_in0_input.dev_attr);
558 device_create_file(&new_client->dev,
559 &sensor_dev_attr_in0_min.dev_attr);
560 device_create_file(&new_client->dev,
561 &sensor_dev_attr_in0_max.dev_attr);
562 device_create_file(&new_client->dev,
563 &sensor_dev_attr_in1_input.dev_attr);
564 device_create_file(&new_client->dev,
565 &sensor_dev_attr_in1_min.dev_attr);
566 device_create_file(&new_client->dev,
567 &sensor_dev_attr_in1_max.dev_attr);
568 device_create_file(&new_client->dev,
569 &sensor_dev_attr_in2_input.dev_attr);
570 device_create_file(&new_client->dev,
571 &sensor_dev_attr_in2_min.dev_attr);
572 device_create_file(&new_client->dev,
573 &sensor_dev_attr_in2_max.dev_attr);
574 device_create_file(&new_client->dev,
575 &sensor_dev_attr_in3_input.dev_attr);
576 device_create_file(&new_client->dev,
577 &sensor_dev_attr_in3_min.dev_attr);
578 device_create_file(&new_client->dev,
579 &sensor_dev_attr_in3_max.dev_attr);
580 device_create_file(&new_client->dev,
581 &sensor_dev_attr_in4_input.dev_attr);
582 device_create_file(&new_client->dev,
583 &sensor_dev_attr_in4_min.dev_attr);
584 device_create_file(&new_client->dev,
585 &sensor_dev_attr_in4_max.dev_attr);
586 device_create_file(&new_client->dev,
587 &sensor_dev_attr_in5_input.dev_attr);
588 device_create_file(&new_client->dev,
589 &sensor_dev_attr_in5_min.dev_attr);
590 device_create_file(&new_client->dev,
591 &sensor_dev_attr_in5_max.dev_attr);
Grant Coady40b5cda2005-04-30 21:41:29 +1000592 device_create_file(&new_client->dev, &dev_attr_temp1_input);
Grant Coadyc7461a62005-09-17 05:32:57 +1000593 device_create_file(&new_client->dev,
594 &sensor_dev_attr_temp1_max.dev_attr);
595 device_create_file(&new_client->dev,
596 &sensor_dev_attr_temp1_max_hyst.dev_attr);
597 device_create_file(&new_client->dev,
598 &sensor_dev_attr_fan1_input.dev_attr);
599 device_create_file(&new_client->dev,
600 &sensor_dev_attr_fan1_div.dev_attr);
601 device_create_file(&new_client->dev,
602 &sensor_dev_attr_fan1_min.dev_attr);
603 device_create_file(&new_client->dev,
604 &sensor_dev_attr_fan2_input.dev_attr);
605 device_create_file(&new_client->dev,
606 &sensor_dev_attr_fan2_div.dev_attr);
607 device_create_file(&new_client->dev,
608 &sensor_dev_attr_fan2_min.dev_attr);
Grant Coady40b5cda2005-04-30 21:41:29 +1000609 device_create_file(&new_client->dev, &dev_attr_alarms);
610 device_create_file(&new_client->dev, &dev_attr_aout_output);
611 device_create_file(&new_client->dev, &dev_attr_chassis_clear);
612 device_create_file(&new_client->dev, &dev_attr_cpu0_vid);
613
614 return 0;
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400615
616exit_detach:
617 i2c_detach_client(new_client);
Grant Coady40b5cda2005-04-30 21:41:29 +1000618exit_free:
Alexey Dobriyan1f57ff82005-08-26 01:49:14 +0400619 kfree(data);
Grant Coady40b5cda2005-04-30 21:41:29 +1000620exit:
621 return err;
622}
623
624static int adm9240_attach_adapter(struct i2c_adapter *adapter)
625{
626 if (!(adapter->class & I2C_CLASS_HWMON))
627 return 0;
Jean Delvare2ed2dc32005-07-31 21:42:02 +0200628 return i2c_probe(adapter, &addr_data, adm9240_detect);
Grant Coady40b5cda2005-04-30 21:41:29 +1000629}
630
631static int adm9240_detach_client(struct i2c_client *client)
632{
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400633 struct adm9240_data *data = i2c_get_clientdata(client);
Grant Coady40b5cda2005-04-30 21:41:29 +1000634 int err;
635
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400636 hwmon_device_unregister(data->class_dev);
637
Jean Delvare7bef5592005-07-27 22:14:49 +0200638 if ((err = i2c_detach_client(client)))
Grant Coady40b5cda2005-04-30 21:41:29 +1000639 return err;
Grant Coady40b5cda2005-04-30 21:41:29 +1000640
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400641 kfree(data);
Grant Coady40b5cda2005-04-30 21:41:29 +1000642 return 0;
643}
644
645static void adm9240_init_client(struct i2c_client *client)
646{
647 struct adm9240_data *data = i2c_get_clientdata(client);
Grant Coady205cf132005-09-17 05:32:55 +1000648 u8 conf = i2c_smbus_read_byte_data(client, ADM9240_REG_CONFIG);
649 u8 mode = i2c_smbus_read_byte_data(client, ADM9240_REG_TEMP_CONF) & 3;
Grant Coady40b5cda2005-04-30 21:41:29 +1000650
Jean Delvare303760b2005-07-31 21:52:01 +0200651 data->vrm = vid_which_vrm(); /* need this to report vid as mV */
Grant Coady40b5cda2005-04-30 21:41:29 +1000652
Grant Coady8e8f9282005-05-13 20:26:10 +1000653 dev_info(&client->dev, "Using VRM: %d.%d\n", data->vrm / 10,
654 data->vrm % 10);
655
Grant Coady40b5cda2005-04-30 21:41:29 +1000656 if (conf & 1) { /* measurement cycle running: report state */
657
658 dev_info(&client->dev, "status: config 0x%02x mode %u\n",
659 conf, mode);
660
661 } else { /* cold start: open limits before starting chip */
662 int i;
663
664 for (i = 0; i < 6; i++)
665 {
Grant Coady205cf132005-09-17 05:32:55 +1000666 i2c_smbus_write_byte_data(client,
Grant Coady40b5cda2005-04-30 21:41:29 +1000667 ADM9240_REG_IN_MIN(i), 0);
Grant Coady205cf132005-09-17 05:32:55 +1000668 i2c_smbus_write_byte_data(client,
Grant Coady40b5cda2005-04-30 21:41:29 +1000669 ADM9240_REG_IN_MAX(i), 255);
670 }
Grant Coady205cf132005-09-17 05:32:55 +1000671 i2c_smbus_write_byte_data(client,
672 ADM9240_REG_FAN_MIN(0), 255);
673 i2c_smbus_write_byte_data(client,
674 ADM9240_REG_FAN_MIN(1), 255);
675 i2c_smbus_write_byte_data(client,
Grant Coadyc7461a62005-09-17 05:32:57 +1000676 ADM9240_REG_TEMP_MAX(0), 127);
Grant Coady205cf132005-09-17 05:32:55 +1000677 i2c_smbus_write_byte_data(client,
Grant Coadyc7461a62005-09-17 05:32:57 +1000678 ADM9240_REG_TEMP_MAX(1), 127);
Grant Coady40b5cda2005-04-30 21:41:29 +1000679
680 /* start measurement cycle */
Grant Coady205cf132005-09-17 05:32:55 +1000681 i2c_smbus_write_byte_data(client, ADM9240_REG_CONFIG, 1);
Grant Coady40b5cda2005-04-30 21:41:29 +1000682
683 dev_info(&client->dev, "cold start: config was 0x%02x "
684 "mode %u\n", conf, mode);
685 }
686}
687
688static struct adm9240_data *adm9240_update_device(struct device *dev)
689{
690 struct i2c_client *client = to_i2c_client(dev);
691 struct adm9240_data *data = i2c_get_clientdata(client);
692 int i;
693
694 down(&data->update_lock);
695
696 /* minimum measurement cycle: 1.75 seconds */
697 if (time_after(jiffies, data->last_updated_measure + (HZ * 7 / 4))
698 || !data->valid) {
699
700 for (i = 0; i < 6; i++) /* read voltages */
701 {
Grant Coady205cf132005-09-17 05:32:55 +1000702 data->in[i] = i2c_smbus_read_byte_data(client,
Grant Coady40b5cda2005-04-30 21:41:29 +1000703 ADM9240_REG_IN(i));
704 }
Grant Coady205cf132005-09-17 05:32:55 +1000705 data->alarms = i2c_smbus_read_byte_data(client,
Grant Coady40b5cda2005-04-30 21:41:29 +1000706 ADM9240_REG_INT(0)) |
Grant Coady205cf132005-09-17 05:32:55 +1000707 i2c_smbus_read_byte_data(client,
Grant Coady40b5cda2005-04-30 21:41:29 +1000708 ADM9240_REG_INT(1)) << 8;
709
710 /* read temperature: assume temperature changes less than
711 * 0.5'C per two measurement cycles thus ignore possible
712 * but unlikely aliasing error on lsb reading. --Grant */
Grant Coady205cf132005-09-17 05:32:55 +1000713 data->temp = ((i2c_smbus_read_byte_data(client,
Grant Coady40b5cda2005-04-30 21:41:29 +1000714 ADM9240_REG_TEMP) << 8) |
Grant Coady205cf132005-09-17 05:32:55 +1000715 i2c_smbus_read_byte_data(client,
Grant Coady40b5cda2005-04-30 21:41:29 +1000716 ADM9240_REG_TEMP_CONF)) / 128;
717
718 for (i = 0; i < 2; i++) /* read fans */
719 {
Grant Coady205cf132005-09-17 05:32:55 +1000720 data->fan[i] = i2c_smbus_read_byte_data(client,
Grant Coady40b5cda2005-04-30 21:41:29 +1000721 ADM9240_REG_FAN(i));
722
723 /* adjust fan clock divider on overflow */
724 if (data->valid && data->fan[i] == 255 &&
725 data->fan_div[i] < 3) {
726
727 adm9240_write_fan_div(client, i,
728 ++data->fan_div[i]);
729
730 /* adjust fan_min if active, but not to 0 */
731 if (data->fan_min[i] < 255 &&
732 data->fan_min[i] >= 2)
733 data->fan_min[i] /= 2;
734 }
735 }
736 data->last_updated_measure = jiffies;
737 }
738
739 /* minimum config reading cycle: 300 seconds */
740 if (time_after(jiffies, data->last_updated_config + (HZ * 300))
741 || !data->valid) {
742
743 for (i = 0; i < 6; i++)
744 {
Grant Coady205cf132005-09-17 05:32:55 +1000745 data->in_min[i] = i2c_smbus_read_byte_data(client,
Grant Coady40b5cda2005-04-30 21:41:29 +1000746 ADM9240_REG_IN_MIN(i));
Grant Coady205cf132005-09-17 05:32:55 +1000747 data->in_max[i] = i2c_smbus_read_byte_data(client,
Grant Coady40b5cda2005-04-30 21:41:29 +1000748 ADM9240_REG_IN_MAX(i));
749 }
750 for (i = 0; i < 2; i++)
751 {
Grant Coady205cf132005-09-17 05:32:55 +1000752 data->fan_min[i] = i2c_smbus_read_byte_data(client,
Grant Coady40b5cda2005-04-30 21:41:29 +1000753 ADM9240_REG_FAN_MIN(i));
754 }
Grant Coadyc7461a62005-09-17 05:32:57 +1000755 data->temp_max[0] = i2c_smbus_read_byte_data(client,
756 ADM9240_REG_TEMP_MAX(0));
757 data->temp_max[1] = i2c_smbus_read_byte_data(client,
758 ADM9240_REG_TEMP_MAX(1));
Grant Coady40b5cda2005-04-30 21:41:29 +1000759
760 /* read fan divs and 5-bit VID */
Grant Coady205cf132005-09-17 05:32:55 +1000761 i = i2c_smbus_read_byte_data(client, ADM9240_REG_VID_FAN_DIV);
Grant Coady40b5cda2005-04-30 21:41:29 +1000762 data->fan_div[0] = (i >> 4) & 3;
763 data->fan_div[1] = (i >> 6) & 3;
764 data->vid = i & 0x0f;
Grant Coady205cf132005-09-17 05:32:55 +1000765 data->vid |= (i2c_smbus_read_byte_data(client,
Grant Coady40b5cda2005-04-30 21:41:29 +1000766 ADM9240_REG_VID4) & 1) << 4;
767 /* read analog out */
Grant Coady205cf132005-09-17 05:32:55 +1000768 data->aout = i2c_smbus_read_byte_data(client,
Grant Coady40b5cda2005-04-30 21:41:29 +1000769 ADM9240_REG_ANALOG_OUT);
770
771 data->last_updated_config = jiffies;
772 data->valid = 1;
773 }
774 up(&data->update_lock);
775 return data;
776}
777
778static int __init sensors_adm9240_init(void)
779{
780 return i2c_add_driver(&adm9240_driver);
781}
782
783static void __exit sensors_adm9240_exit(void)
784{
785 i2c_del_driver(&adm9240_driver);
786}
787
788MODULE_AUTHOR("Michiel Rook <michiel@grendelproject.nl>, "
789 "Grant Coady <gcoady@gmail.com> and others");
790MODULE_DESCRIPTION("ADM9240/DS1780/LM81 driver");
791MODULE_LICENSE("GPL");
792
793module_init(sensors_adm9240_init);
794module_exit(sensors_adm9240_exit);
795