2 adm1021.c - Part of lm_sensors, Linux kernel modules for hardware
4 Copyright (c) 1998, 1999 Frodo Looijaard <frodol@dds.nl> and
5 Philip Edelbrock <phil@netroedge.com>
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #include <linux/module.h>
23 #include <linux/init.h>
24 #include <linux/slab.h>
25 #include <linux/jiffies.h>
26 #include <linux/i2c.h>
27 #include <linux/hwmon.h>
28 #include <linux/hwmon-sysfs.h>
29 #include <linux/err.h>
30 #include <linux/mutex.h>
33 /* Addresses to scan */
34 static const unsigned short normal_i2c
[] = {
35 0x18, 0x19, 0x1a, 0x29, 0x2a, 0x2b, 0x4c, 0x4d, 0x4e, I2C_CLIENT_END
};
37 /* Insmod parameters */
38 I2C_CLIENT_INSMOD_8(adm1021
, adm1023
, max1617
, max1617a
, thmc10
, lm84
, gl523sm
,
41 /* adm1021 constants specified below */
43 /* The adm1021 registers */
46 #define ADM1021_REG_TEMP(nr) (nr)
47 #define ADM1021_REG_STATUS 0x02
48 /* 0x41 = AD, 0x49 = TI, 0x4D = Maxim, 0x23 = Genesys , 0x54 = Onsemi */
49 #define ADM1021_REG_MAN_ID 0xFE
50 /* ADM1021 = 0x0X, ADM1023 = 0x3X */
51 #define ADM1021_REG_DEV_ID 0xFF
52 /* These use different addresses for reading/writing */
53 #define ADM1021_REG_CONFIG_R 0x03
54 #define ADM1021_REG_CONFIG_W 0x09
55 #define ADM1021_REG_CONV_RATE_R 0x04
56 #define ADM1021_REG_CONV_RATE_W 0x0A
57 /* These are for the ADM1023's additional precision on the remote temp sensor */
58 #define ADM1023_REG_REM_TEMP_PREC 0x10
59 #define ADM1023_REG_REM_OFFSET 0x11
60 #define ADM1023_REG_REM_OFFSET_PREC 0x12
61 #define ADM1023_REG_REM_TOS_PREC 0x13
62 #define ADM1023_REG_REM_THYST_PREC 0x14
65 #define ADM1021_REG_TOS_R(nr) (0x05 + 2 * (nr))
66 #define ADM1021_REG_TOS_W(nr) (0x0B + 2 * (nr))
67 #define ADM1021_REG_THYST_R(nr) (0x06 + 2 * (nr))
68 #define ADM1021_REG_THYST_W(nr) (0x0C + 2 * (nr))
70 #define ADM1021_REG_ONESHOT 0x0F
74 /* Note: Even though I left the low and high limits named os and hyst,
75 they don't quite work like a thermostat the way the LM75 does. I.e.,
76 a lower temp than THYST actually triggers an alarm instead of
77 clearing it. Weird, ey? --Phil */
79 /* Each client has this additional data */
81 struct device
*hwmon_dev
;
84 struct mutex update_lock
;
85 char valid
; /* !=0 if following fields are valid */
86 char low_power
; /* !=0 if device in low power mode */
87 unsigned long last_updated
; /* In jiffies */
89 int temp_max
[2]; /* Register values */
93 /* Special values for ADM1023 only */
94 u8 remote_temp_offset
;
95 u8 remote_temp_offset_prec
;
98 static int adm1021_probe(struct i2c_client
*client
,
99 const struct i2c_device_id
*id
);
100 static int adm1021_detect(struct i2c_client
*client
, int kind
,
101 struct i2c_board_info
*info
);
102 static void adm1021_init_client(struct i2c_client
*client
);
103 static int adm1021_remove(struct i2c_client
*client
);
104 static struct adm1021_data
*adm1021_update_device(struct device
*dev
);
106 /* (amalysh) read only mode, otherwise any limit's writing confuse BIOS */
107 static int read_only
;
110 static const struct i2c_device_id adm1021_id
[] = {
111 { "adm1021", adm1021
},
112 { "adm1023", adm1023
},
113 { "max1617", max1617
},
114 { "max1617a", max1617a
},
115 { "thmc10", thmc10
},
117 { "gl523sm", gl523sm
},
118 { "mc1066", mc1066
},
121 MODULE_DEVICE_TABLE(i2c
, adm1021_id
);
123 /* This is the driver that will be inserted */
124 static struct i2c_driver adm1021_driver
= {
125 .class = I2C_CLASS_HWMON
,
129 .probe
= adm1021_probe
,
130 .remove
= adm1021_remove
,
131 .id_table
= adm1021_id
,
132 .detect
= adm1021_detect
,
133 .address_data
= &addr_data
,
136 static ssize_t
show_temp(struct device
*dev
,
137 struct device_attribute
*devattr
, char *buf
)
139 int index
= to_sensor_dev_attr(devattr
)->index
;
140 struct adm1021_data
*data
= adm1021_update_device(dev
);
142 return sprintf(buf
, "%d\n", data
->temp
[index
]);
145 static ssize_t
show_temp_max(struct device
*dev
,
146 struct device_attribute
*devattr
, char *buf
)
148 int index
= to_sensor_dev_attr(devattr
)->index
;
149 struct adm1021_data
*data
= adm1021_update_device(dev
);
151 return sprintf(buf
, "%d\n", data
->temp_max
[index
]);
154 static ssize_t
show_temp_min(struct device
*dev
,
155 struct device_attribute
*devattr
, char *buf
)
157 int index
= to_sensor_dev_attr(devattr
)->index
;
158 struct adm1021_data
*data
= adm1021_update_device(dev
);
160 return sprintf(buf
, "%d\n", data
->temp_min
[index
]);
163 static ssize_t
show_alarm(struct device
*dev
, struct device_attribute
*attr
,
166 int index
= to_sensor_dev_attr(attr
)->index
;
167 struct adm1021_data
*data
= adm1021_update_device(dev
);
168 return sprintf(buf
, "%u\n", (data
->alarms
>> index
) & 1);
171 static ssize_t
show_alarms(struct device
*dev
,
172 struct device_attribute
*attr
,
175 struct adm1021_data
*data
= adm1021_update_device(dev
);
176 return sprintf(buf
, "%u\n", data
->alarms
);
179 static ssize_t
set_temp_max(struct device
*dev
,
180 struct device_attribute
*devattr
,
181 const char *buf
, size_t count
)
183 int index
= to_sensor_dev_attr(devattr
)->index
;
184 struct i2c_client
*client
= to_i2c_client(dev
);
185 struct adm1021_data
*data
= i2c_get_clientdata(client
);
186 long temp
= simple_strtol(buf
, NULL
, 10) / 1000;
188 mutex_lock(&data
->update_lock
);
189 data
->temp_max
[index
] = SENSORS_LIMIT(temp
, -128, 127);
191 i2c_smbus_write_byte_data(client
, ADM1021_REG_TOS_W(index
),
192 data
->temp_max
[index
]);
193 mutex_unlock(&data
->update_lock
);
198 static ssize_t
set_temp_min(struct device
*dev
,
199 struct device_attribute
*devattr
,
200 const char *buf
, size_t count
)
202 int index
= to_sensor_dev_attr(devattr
)->index
;
203 struct i2c_client
*client
= to_i2c_client(dev
);
204 struct adm1021_data
*data
= i2c_get_clientdata(client
);
205 long temp
= simple_strtol(buf
, NULL
, 10) / 1000;
207 mutex_lock(&data
->update_lock
);
208 data
->temp_min
[index
] = SENSORS_LIMIT(temp
, -128, 127);
210 i2c_smbus_write_byte_data(client
, ADM1021_REG_THYST_W(index
),
211 data
->temp_min
[index
]);
212 mutex_unlock(&data
->update_lock
);
217 static ssize_t
show_low_power(struct device
*dev
,
218 struct device_attribute
*devattr
, char *buf
)
220 struct adm1021_data
*data
= adm1021_update_device(dev
);
221 return sprintf(buf
, "%d\n", data
->low_power
);
224 static ssize_t
set_low_power(struct device
*dev
,
225 struct device_attribute
*devattr
,
226 const char *buf
, size_t count
)
228 struct i2c_client
*client
= to_i2c_client(dev
);
229 struct adm1021_data
*data
= i2c_get_clientdata(client
);
230 int low_power
= simple_strtol(buf
, NULL
, 10) != 0;
232 mutex_lock(&data
->update_lock
);
233 if (low_power
!= data
->low_power
) {
234 int config
= i2c_smbus_read_byte_data(
235 client
, ADM1021_REG_CONFIG_R
);
236 data
->low_power
= low_power
;
237 i2c_smbus_write_byte_data(client
, ADM1021_REG_CONFIG_W
,
238 (config
& 0xBF) | (low_power
<< 6));
240 mutex_unlock(&data
->update_lock
);
246 static SENSOR_DEVICE_ATTR(temp1_input
, S_IRUGO
, show_temp
, NULL
, 0);
247 static SENSOR_DEVICE_ATTR(temp1_max
, S_IWUSR
| S_IRUGO
, show_temp_max
,
249 static SENSOR_DEVICE_ATTR(temp1_min
, S_IWUSR
| S_IRUGO
, show_temp_min
,
251 static SENSOR_DEVICE_ATTR(temp2_input
, S_IRUGO
, show_temp
, NULL
, 1);
252 static SENSOR_DEVICE_ATTR(temp2_max
, S_IWUSR
| S_IRUGO
, show_temp_max
,
254 static SENSOR_DEVICE_ATTR(temp2_min
, S_IWUSR
| S_IRUGO
, show_temp_min
,
256 static SENSOR_DEVICE_ATTR(temp1_max_alarm
, S_IRUGO
, show_alarm
, NULL
, 6);
257 static SENSOR_DEVICE_ATTR(temp1_min_alarm
, S_IRUGO
, show_alarm
, NULL
, 5);
258 static SENSOR_DEVICE_ATTR(temp2_max_alarm
, S_IRUGO
, show_alarm
, NULL
, 4);
259 static SENSOR_DEVICE_ATTR(temp2_min_alarm
, S_IRUGO
, show_alarm
, NULL
, 3);
260 static SENSOR_DEVICE_ATTR(temp2_fault
, S_IRUGO
, show_alarm
, NULL
, 2);
262 static DEVICE_ATTR(alarms
, S_IRUGO
, show_alarms
, NULL
);
263 static DEVICE_ATTR(low_power
, S_IWUSR
| S_IRUGO
, show_low_power
, set_low_power
);
265 static struct attribute
*adm1021_attributes
[] = {
266 &sensor_dev_attr_temp1_max
.dev_attr
.attr
,
267 &sensor_dev_attr_temp1_min
.dev_attr
.attr
,
268 &sensor_dev_attr_temp1_input
.dev_attr
.attr
,
269 &sensor_dev_attr_temp2_max
.dev_attr
.attr
,
270 &sensor_dev_attr_temp2_min
.dev_attr
.attr
,
271 &sensor_dev_attr_temp2_input
.dev_attr
.attr
,
272 &sensor_dev_attr_temp1_max_alarm
.dev_attr
.attr
,
273 &sensor_dev_attr_temp1_min_alarm
.dev_attr
.attr
,
274 &sensor_dev_attr_temp2_max_alarm
.dev_attr
.attr
,
275 &sensor_dev_attr_temp2_min_alarm
.dev_attr
.attr
,
276 &sensor_dev_attr_temp2_fault
.dev_attr
.attr
,
277 &dev_attr_alarms
.attr
,
278 &dev_attr_low_power
.attr
,
282 static const struct attribute_group adm1021_group
= {
283 .attrs
= adm1021_attributes
,
286 /* Return 0 if detection is successful, -ENODEV otherwise */
287 static int adm1021_detect(struct i2c_client
*client
, int kind
,
288 struct i2c_board_info
*info
)
290 struct i2c_adapter
*adapter
= client
->adapter
;
291 const char *type_name
;
292 int conv_rate
, status
, config
, man_id
, dev_id
;
294 if (!i2c_check_functionality(adapter
, I2C_FUNC_SMBUS_BYTE_DATA
)) {
295 pr_debug("adm1021: detect failed, "
296 "smbus byte data not supported!\n");
300 status
= i2c_smbus_read_byte_data(client
, ADM1021_REG_STATUS
);
301 conv_rate
= i2c_smbus_read_byte_data(client
,
302 ADM1021_REG_CONV_RATE_R
);
303 config
= i2c_smbus_read_byte_data(client
, ADM1021_REG_CONFIG_R
);
305 /* Check unused bits */
306 if ((status
& 0x03) || (config
& 0x3F) || (conv_rate
& 0xF8)) {
307 pr_debug("adm1021: detect failed, chip not detected!\n");
311 /* Determine the chip type. */
312 man_id
= i2c_smbus_read_byte_data(client
, ADM1021_REG_MAN_ID
);
313 dev_id
= i2c_smbus_read_byte_data(client
, ADM1021_REG_DEV_ID
);
315 if (man_id
== 0x4d && dev_id
== 0x01)
316 type_name
= "max1617a";
317 else if (man_id
== 0x41) {
318 if ((dev_id
& 0xF0) == 0x30)
319 type_name
= "adm1023";
321 type_name
= "adm1021";
322 } else if (man_id
== 0x49)
323 type_name
= "thmc10";
324 else if (man_id
== 0x23)
325 type_name
= "gl523sm";
326 else if (man_id
== 0x54)
327 type_name
= "mc1066";
328 /* LM84 Mfr ID in a different place, and it has more unused bits */
329 else if (conv_rate
== 0x00
330 && (config
& 0x7F) == 0x00
331 && (status
& 0xAB) == 0x00)
334 type_name
= "max1617";
336 pr_debug("adm1021: Detected chip %s at adapter %d, address 0x%02x.\n",
337 type_name
, i2c_adapter_id(adapter
), client
->addr
);
338 strlcpy(info
->type
, type_name
, I2C_NAME_SIZE
);
343 static int adm1021_probe(struct i2c_client
*client
,
344 const struct i2c_device_id
*id
)
346 struct adm1021_data
*data
;
349 data
= kzalloc(sizeof(struct adm1021_data
), GFP_KERNEL
);
351 pr_debug("adm1021: detect failed, kzalloc failed!\n");
356 i2c_set_clientdata(client
, data
);
357 data
->type
= id
->driver_data
;
358 mutex_init(&data
->update_lock
);
360 /* Initialize the ADM1021 chip */
361 if (data
->type
!= lm84
&& !read_only
)
362 adm1021_init_client(client
);
364 /* Register sysfs hooks */
365 if ((err
= sysfs_create_group(&client
->dev
.kobj
, &adm1021_group
)))
368 data
->hwmon_dev
= hwmon_device_register(&client
->dev
);
369 if (IS_ERR(data
->hwmon_dev
)) {
370 err
= PTR_ERR(data
->hwmon_dev
);
377 sysfs_remove_group(&client
->dev
.kobj
, &adm1021_group
);
384 static void adm1021_init_client(struct i2c_client
*client
)
386 /* Enable ADC and disable suspend mode */
387 i2c_smbus_write_byte_data(client
, ADM1021_REG_CONFIG_W
,
388 i2c_smbus_read_byte_data(client
, ADM1021_REG_CONFIG_R
) & 0xBF);
389 /* Set Conversion rate to 1/sec (this can be tinkered with) */
390 i2c_smbus_write_byte_data(client
, ADM1021_REG_CONV_RATE_W
, 0x04);
393 static int adm1021_remove(struct i2c_client
*client
)
395 struct adm1021_data
*data
= i2c_get_clientdata(client
);
397 hwmon_device_unregister(data
->hwmon_dev
);
398 sysfs_remove_group(&client
->dev
.kobj
, &adm1021_group
);
404 static struct adm1021_data
*adm1021_update_device(struct device
*dev
)
406 struct i2c_client
*client
= to_i2c_client(dev
);
407 struct adm1021_data
*data
= i2c_get_clientdata(client
);
409 mutex_lock(&data
->update_lock
);
411 if (time_after(jiffies
, data
->last_updated
+ HZ
+ HZ
/ 2)
415 dev_dbg(&client
->dev
, "Starting adm1021 update\n");
417 for (i
= 0; i
< 2; i
++) {
418 data
->temp
[i
] = 1000 *
419 (s8
) i2c_smbus_read_byte_data(
420 client
, ADM1021_REG_TEMP(i
));
421 data
->temp_max
[i
] = 1000 *
422 (s8
) i2c_smbus_read_byte_data(
423 client
, ADM1021_REG_TOS_R(i
));
424 data
->temp_min
[i
] = 1000 *
425 (s8
) i2c_smbus_read_byte_data(
426 client
, ADM1021_REG_THYST_R(i
));
428 data
->alarms
= i2c_smbus_read_byte_data(client
,
429 ADM1021_REG_STATUS
) & 0x7c;
430 if (data
->type
== adm1023
) {
431 /* The ADM1023 provides 3 extra bits of precision for
432 * the remote sensor in extra registers. */
433 data
->temp
[1] += 125 * (i2c_smbus_read_byte_data(
434 client
, ADM1023_REG_REM_TEMP_PREC
) >> 5);
435 data
->temp_max
[1] += 125 * (i2c_smbus_read_byte_data(
436 client
, ADM1023_REG_REM_TOS_PREC
) >> 5);
437 data
->temp_min
[1] += 125 * (i2c_smbus_read_byte_data(
438 client
, ADM1023_REG_REM_THYST_PREC
) >> 5);
439 data
->remote_temp_offset
=
440 i2c_smbus_read_byte_data(client
,
441 ADM1023_REG_REM_OFFSET
);
442 data
->remote_temp_offset_prec
=
443 i2c_smbus_read_byte_data(client
,
444 ADM1023_REG_REM_OFFSET_PREC
);
446 data
->last_updated
= jiffies
;
450 mutex_unlock(&data
->update_lock
);
455 static int __init
sensors_adm1021_init(void)
457 return i2c_add_driver(&adm1021_driver
);
460 static void __exit
sensors_adm1021_exit(void)
462 i2c_del_driver(&adm1021_driver
);
465 MODULE_AUTHOR ("Frodo Looijaard <frodol@dds.nl> and "
466 "Philip Edelbrock <phil@netroedge.com>");
467 MODULE_DESCRIPTION("adm1021 driver");
468 MODULE_LICENSE("GPL");
470 module_param(read_only
, bool, 0);
471 MODULE_PARM_DESC(read_only
, "Don't set any values, read only mode");
473 module_init(sensors_adm1021_init
)
474 module_exit(sensors_adm1021_exit
)