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
};
38 adm1021
, adm1023
, max1617
, max1617a
, thmc10
, lm84
, gl523sm
, mc1066
};
40 /* adm1021 constants specified below */
42 /* The adm1021 registers */
45 #define ADM1021_REG_TEMP(nr) (nr)
46 #define ADM1021_REG_STATUS 0x02
47 /* 0x41 = AD, 0x49 = TI, 0x4D = Maxim, 0x23 = Genesys , 0x54 = Onsemi */
48 #define ADM1021_REG_MAN_ID 0xFE
49 /* ADM1021 = 0x0X, ADM1023 = 0x3X */
50 #define ADM1021_REG_DEV_ID 0xFF
51 /* These use different addresses for reading/writing */
52 #define ADM1021_REG_CONFIG_R 0x03
53 #define ADM1021_REG_CONFIG_W 0x09
54 #define ADM1021_REG_CONV_RATE_R 0x04
55 #define ADM1021_REG_CONV_RATE_W 0x0A
56 /* These are for the ADM1023's additional precision on the remote temp sensor */
57 #define ADM1023_REG_REM_TEMP_PREC 0x10
58 #define ADM1023_REG_REM_OFFSET 0x11
59 #define ADM1023_REG_REM_OFFSET_PREC 0x12
60 #define ADM1023_REG_REM_TOS_PREC 0x13
61 #define ADM1023_REG_REM_THYST_PREC 0x14
64 #define ADM1021_REG_TOS_R(nr) (0x05 + 2 * (nr))
65 #define ADM1021_REG_TOS_W(nr) (0x0B + 2 * (nr))
66 #define ADM1021_REG_THYST_R(nr) (0x06 + 2 * (nr))
67 #define ADM1021_REG_THYST_W(nr) (0x0C + 2 * (nr))
69 #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
80 /* Each client has this additional data */
82 struct device
*hwmon_dev
;
85 struct mutex update_lock
;
86 char valid
; /* !=0 if following fields are valid */
87 char low_power
; /* !=0 if device in low power mode */
88 unsigned long last_updated
; /* In jiffies */
90 int temp_max
[2]; /* Register values */
94 /* Special values for ADM1023 only */
95 u8 remote_temp_offset
;
96 u8 remote_temp_offset_prec
;
99 static int adm1021_probe(struct i2c_client
*client
,
100 const struct i2c_device_id
*id
);
101 static int adm1021_detect(struct i2c_client
*client
,
102 struct i2c_board_info
*info
);
103 static void adm1021_init_client(struct i2c_client
*client
);
104 static int adm1021_remove(struct i2c_client
*client
);
105 static struct adm1021_data
*adm1021_update_device(struct device
*dev
);
107 /* (amalysh) read only mode, otherwise any limit's writing confuse BIOS */
108 static bool read_only
;
111 static const struct i2c_device_id adm1021_id
[] = {
112 { "adm1021", adm1021
},
113 { "adm1023", adm1023
},
114 { "max1617", max1617
},
115 { "max1617a", max1617a
},
116 { "thmc10", thmc10
},
118 { "gl523sm", gl523sm
},
119 { "mc1066", mc1066
},
122 MODULE_DEVICE_TABLE(i2c
, adm1021_id
);
124 /* This is the driver that will be inserted */
125 static struct i2c_driver adm1021_driver
= {
126 .class = I2C_CLASS_HWMON
,
130 .probe
= adm1021_probe
,
131 .remove
= adm1021_remove
,
132 .id_table
= adm1021_id
,
133 .detect
= adm1021_detect
,
134 .address_list
= normal_i2c
,
137 static ssize_t
show_temp(struct device
*dev
,
138 struct device_attribute
*devattr
, char *buf
)
140 int index
= to_sensor_dev_attr(devattr
)->index
;
141 struct adm1021_data
*data
= adm1021_update_device(dev
);
143 return sprintf(buf
, "%d\n", data
->temp
[index
]);
146 static ssize_t
show_temp_max(struct device
*dev
,
147 struct device_attribute
*devattr
, char *buf
)
149 int index
= to_sensor_dev_attr(devattr
)->index
;
150 struct adm1021_data
*data
= adm1021_update_device(dev
);
152 return sprintf(buf
, "%d\n", data
->temp_max
[index
]);
155 static ssize_t
show_temp_min(struct device
*dev
,
156 struct device_attribute
*devattr
, char *buf
)
158 int index
= to_sensor_dev_attr(devattr
)->index
;
159 struct adm1021_data
*data
= adm1021_update_device(dev
);
161 return sprintf(buf
, "%d\n", data
->temp_min
[index
]);
164 static ssize_t
show_alarm(struct device
*dev
, struct device_attribute
*attr
,
167 int index
= to_sensor_dev_attr(attr
)->index
;
168 struct adm1021_data
*data
= adm1021_update_device(dev
);
169 return sprintf(buf
, "%u\n", (data
->alarms
>> index
) & 1);
172 static ssize_t
show_alarms(struct device
*dev
,
173 struct device_attribute
*attr
,
176 struct adm1021_data
*data
= adm1021_update_device(dev
);
177 return sprintf(buf
, "%u\n", data
->alarms
);
180 static ssize_t
set_temp_max(struct device
*dev
,
181 struct device_attribute
*devattr
,
182 const char *buf
, size_t count
)
184 int index
= to_sensor_dev_attr(devattr
)->index
;
185 struct i2c_client
*client
= to_i2c_client(dev
);
186 struct adm1021_data
*data
= i2c_get_clientdata(client
);
190 err
= kstrtol(buf
, 10, &temp
);
195 mutex_lock(&data
->update_lock
);
196 reg_val
= clamp_val(temp
, -128, 127);
197 data
->temp_max
[index
] = reg_val
* 1000;
199 i2c_smbus_write_byte_data(client
, ADM1021_REG_TOS_W(index
),
201 mutex_unlock(&data
->update_lock
);
206 static ssize_t
set_temp_min(struct device
*dev
,
207 struct device_attribute
*devattr
,
208 const char *buf
, size_t count
)
210 int index
= to_sensor_dev_attr(devattr
)->index
;
211 struct i2c_client
*client
= to_i2c_client(dev
);
212 struct adm1021_data
*data
= i2c_get_clientdata(client
);
216 err
= kstrtol(buf
, 10, &temp
);
221 mutex_lock(&data
->update_lock
);
222 reg_val
= clamp_val(temp
, -128, 127);
223 data
->temp_min
[index
] = reg_val
* 1000;
225 i2c_smbus_write_byte_data(client
, ADM1021_REG_THYST_W(index
),
227 mutex_unlock(&data
->update_lock
);
232 static ssize_t
show_low_power(struct device
*dev
,
233 struct device_attribute
*devattr
, char *buf
)
235 struct adm1021_data
*data
= adm1021_update_device(dev
);
236 return sprintf(buf
, "%d\n", data
->low_power
);
239 static ssize_t
set_low_power(struct device
*dev
,
240 struct device_attribute
*devattr
,
241 const char *buf
, size_t count
)
243 struct i2c_client
*client
= to_i2c_client(dev
);
244 struct adm1021_data
*data
= i2c_get_clientdata(client
);
249 err
= kstrtoul(buf
, 10, &val
);
252 low_power
= val
!= 0;
254 mutex_lock(&data
->update_lock
);
255 if (low_power
!= data
->low_power
) {
256 int config
= i2c_smbus_read_byte_data(
257 client
, ADM1021_REG_CONFIG_R
);
258 data
->low_power
= low_power
;
259 i2c_smbus_write_byte_data(client
, ADM1021_REG_CONFIG_W
,
260 (config
& 0xBF) | (low_power
<< 6));
262 mutex_unlock(&data
->update_lock
);
268 static SENSOR_DEVICE_ATTR(temp1_input
, S_IRUGO
, show_temp
, NULL
, 0);
269 static SENSOR_DEVICE_ATTR(temp1_max
, S_IWUSR
| S_IRUGO
, show_temp_max
,
271 static SENSOR_DEVICE_ATTR(temp1_min
, S_IWUSR
| S_IRUGO
, show_temp_min
,
273 static SENSOR_DEVICE_ATTR(temp2_input
, S_IRUGO
, show_temp
, NULL
, 1);
274 static SENSOR_DEVICE_ATTR(temp2_max
, S_IWUSR
| S_IRUGO
, show_temp_max
,
276 static SENSOR_DEVICE_ATTR(temp2_min
, S_IWUSR
| S_IRUGO
, show_temp_min
,
278 static SENSOR_DEVICE_ATTR(temp1_max_alarm
, S_IRUGO
, show_alarm
, NULL
, 6);
279 static SENSOR_DEVICE_ATTR(temp1_min_alarm
, S_IRUGO
, show_alarm
, NULL
, 5);
280 static SENSOR_DEVICE_ATTR(temp2_max_alarm
, S_IRUGO
, show_alarm
, NULL
, 4);
281 static SENSOR_DEVICE_ATTR(temp2_min_alarm
, S_IRUGO
, show_alarm
, NULL
, 3);
282 static SENSOR_DEVICE_ATTR(temp2_fault
, S_IRUGO
, show_alarm
, NULL
, 2);
284 static DEVICE_ATTR(alarms
, S_IRUGO
, show_alarms
, NULL
);
285 static DEVICE_ATTR(low_power
, S_IWUSR
| S_IRUGO
, show_low_power
, set_low_power
);
287 static struct attribute
*adm1021_attributes
[] = {
288 &sensor_dev_attr_temp1_max
.dev_attr
.attr
,
289 &sensor_dev_attr_temp1_input
.dev_attr
.attr
,
290 &sensor_dev_attr_temp2_max
.dev_attr
.attr
,
291 &sensor_dev_attr_temp2_input
.dev_attr
.attr
,
292 &sensor_dev_attr_temp1_max_alarm
.dev_attr
.attr
,
293 &sensor_dev_attr_temp2_max_alarm
.dev_attr
.attr
,
294 &sensor_dev_attr_temp2_fault
.dev_attr
.attr
,
295 &dev_attr_alarms
.attr
,
296 &dev_attr_low_power
.attr
,
300 static const struct attribute_group adm1021_group
= {
301 .attrs
= adm1021_attributes
,
304 static struct attribute
*adm1021_min_attributes
[] = {
305 &sensor_dev_attr_temp1_min
.dev_attr
.attr
,
306 &sensor_dev_attr_temp2_min
.dev_attr
.attr
,
307 &sensor_dev_attr_temp1_min_alarm
.dev_attr
.attr
,
308 &sensor_dev_attr_temp2_min_alarm
.dev_attr
.attr
,
312 static const struct attribute_group adm1021_min_group
= {
313 .attrs
= adm1021_min_attributes
,
316 /* Return 0 if detection is successful, -ENODEV otherwise */
317 static int adm1021_detect(struct i2c_client
*client
,
318 struct i2c_board_info
*info
)
320 struct i2c_adapter
*adapter
= client
->adapter
;
321 const char *type_name
;
322 int conv_rate
, status
, config
, man_id
, dev_id
;
324 if (!i2c_check_functionality(adapter
, I2C_FUNC_SMBUS_BYTE_DATA
)) {
325 pr_debug("detect failed, smbus byte data not supported!\n");
329 status
= i2c_smbus_read_byte_data(client
, ADM1021_REG_STATUS
);
330 conv_rate
= i2c_smbus_read_byte_data(client
,
331 ADM1021_REG_CONV_RATE_R
);
332 config
= i2c_smbus_read_byte_data(client
, ADM1021_REG_CONFIG_R
);
334 /* Check unused bits */
335 if ((status
& 0x03) || (config
& 0x3F) || (conv_rate
& 0xF8)) {
336 pr_debug("detect failed, chip not detected!\n");
340 /* Determine the chip type. */
341 man_id
= i2c_smbus_read_byte_data(client
, ADM1021_REG_MAN_ID
);
342 dev_id
= i2c_smbus_read_byte_data(client
, ADM1021_REG_DEV_ID
);
344 if (man_id
< 0 || dev_id
< 0)
347 if (man_id
== 0x4d && dev_id
== 0x01)
348 type_name
= "max1617a";
349 else if (man_id
== 0x41) {
350 if ((dev_id
& 0xF0) == 0x30)
351 type_name
= "adm1023";
352 else if ((dev_id
& 0xF0) == 0x00)
353 type_name
= "adm1021";
356 } else if (man_id
== 0x49)
357 type_name
= "thmc10";
358 else if (man_id
== 0x23)
359 type_name
= "gl523sm";
360 else if (man_id
== 0x54)
361 type_name
= "mc1066";
363 int lte
, rte
, lhi
, rhi
, llo
, rlo
;
365 /* extra checks for LM84 and MAX1617 to avoid misdetections */
367 llo
= i2c_smbus_read_byte_data(client
, ADM1021_REG_THYST_R(0));
368 rlo
= i2c_smbus_read_byte_data(client
, ADM1021_REG_THYST_R(1));
370 /* fail if any of the additional register reads failed */
371 if (llo
< 0 || rlo
< 0)
374 lte
= i2c_smbus_read_byte_data(client
, ADM1021_REG_TEMP(0));
375 rte
= i2c_smbus_read_byte_data(client
, ADM1021_REG_TEMP(1));
376 lhi
= i2c_smbus_read_byte_data(client
, ADM1021_REG_TOS_R(0));
377 rhi
= i2c_smbus_read_byte_data(client
, ADM1021_REG_TOS_R(1));
380 * Fail for negative temperatures and negative high limits.
381 * This check also catches read errors on the tested registers.
383 if ((s8
)lte
< 0 || (s8
)rte
< 0 || (s8
)lhi
< 0 || (s8
)rhi
< 0)
386 /* fail if all registers hold the same value */
387 if (lte
== rte
&& lte
== lhi
&& lte
== rhi
&& lte
== llo
392 * LM84 Mfr ID is in a different place,
393 * and it has more unused bits.
395 if (conv_rate
== 0x00
396 && (config
& 0x7F) == 0x00
397 && (status
& 0xAB) == 0x00) {
400 /* fail if low limits are larger than high limits */
401 if ((s8
)llo
> lhi
|| (s8
)rlo
> rhi
)
403 type_name
= "max1617";
407 pr_debug("Detected chip %s at adapter %d, address 0x%02x.\n",
408 type_name
, i2c_adapter_id(adapter
), client
->addr
);
409 strlcpy(info
->type
, type_name
, I2C_NAME_SIZE
);
414 static int adm1021_probe(struct i2c_client
*client
,
415 const struct i2c_device_id
*id
)
417 struct adm1021_data
*data
;
420 data
= devm_kzalloc(&client
->dev
, sizeof(struct adm1021_data
),
425 i2c_set_clientdata(client
, data
);
426 data
->type
= id
->driver_data
;
427 mutex_init(&data
->update_lock
);
429 /* Initialize the ADM1021 chip */
430 if (data
->type
!= lm84
&& !read_only
)
431 adm1021_init_client(client
);
433 /* Register sysfs hooks */
434 err
= sysfs_create_group(&client
->dev
.kobj
, &adm1021_group
);
438 if (data
->type
!= lm84
) {
439 err
= sysfs_create_group(&client
->dev
.kobj
, &adm1021_min_group
);
444 data
->hwmon_dev
= hwmon_device_register(&client
->dev
);
445 if (IS_ERR(data
->hwmon_dev
)) {
446 err
= PTR_ERR(data
->hwmon_dev
);
453 sysfs_remove_group(&client
->dev
.kobj
, &adm1021_min_group
);
454 sysfs_remove_group(&client
->dev
.kobj
, &adm1021_group
);
458 static void adm1021_init_client(struct i2c_client
*client
)
460 /* Enable ADC and disable suspend mode */
461 i2c_smbus_write_byte_data(client
, ADM1021_REG_CONFIG_W
,
462 i2c_smbus_read_byte_data(client
, ADM1021_REG_CONFIG_R
) & 0xBF);
463 /* Set Conversion rate to 1/sec (this can be tinkered with) */
464 i2c_smbus_write_byte_data(client
, ADM1021_REG_CONV_RATE_W
, 0x04);
467 static int adm1021_remove(struct i2c_client
*client
)
469 struct adm1021_data
*data
= i2c_get_clientdata(client
);
471 hwmon_device_unregister(data
->hwmon_dev
);
472 sysfs_remove_group(&client
->dev
.kobj
, &adm1021_min_group
);
473 sysfs_remove_group(&client
->dev
.kobj
, &adm1021_group
);
478 static struct adm1021_data
*adm1021_update_device(struct device
*dev
)
480 struct i2c_client
*client
= to_i2c_client(dev
);
481 struct adm1021_data
*data
= i2c_get_clientdata(client
);
483 mutex_lock(&data
->update_lock
);
485 if (time_after(jiffies
, data
->last_updated
+ HZ
+ HZ
/ 2)
489 dev_dbg(&client
->dev
, "Starting adm1021 update\n");
491 for (i
= 0; i
< 2; i
++) {
492 data
->temp
[i
] = 1000 *
493 (s8
) i2c_smbus_read_byte_data(
494 client
, ADM1021_REG_TEMP(i
));
495 data
->temp_max
[i
] = 1000 *
496 (s8
) i2c_smbus_read_byte_data(
497 client
, ADM1021_REG_TOS_R(i
));
498 if (data
->type
!= lm84
) {
499 data
->temp_min
[i
] = 1000 *
500 (s8
) i2c_smbus_read_byte_data(client
,
501 ADM1021_REG_THYST_R(i
));
504 data
->alarms
= i2c_smbus_read_byte_data(client
,
505 ADM1021_REG_STATUS
) & 0x7c;
506 if (data
->type
== adm1023
) {
508 * The ADM1023 provides 3 extra bits of precision for
509 * the remote sensor in extra registers.
511 data
->temp
[1] += 125 * (i2c_smbus_read_byte_data(
512 client
, ADM1023_REG_REM_TEMP_PREC
) >> 5);
513 data
->temp_max
[1] += 125 * (i2c_smbus_read_byte_data(
514 client
, ADM1023_REG_REM_TOS_PREC
) >> 5);
515 data
->temp_min
[1] += 125 * (i2c_smbus_read_byte_data(
516 client
, ADM1023_REG_REM_THYST_PREC
) >> 5);
517 data
->remote_temp_offset
=
518 i2c_smbus_read_byte_data(client
,
519 ADM1023_REG_REM_OFFSET
);
520 data
->remote_temp_offset_prec
=
521 i2c_smbus_read_byte_data(client
,
522 ADM1023_REG_REM_OFFSET_PREC
);
524 data
->last_updated
= jiffies
;
528 mutex_unlock(&data
->update_lock
);
533 module_i2c_driver(adm1021_driver
);
535 MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl> and "
536 "Philip Edelbrock <phil@netroedge.com>");
537 MODULE_DESCRIPTION("adm1021 driver");
538 MODULE_LICENSE("GPL");
540 module_param(read_only
, bool, 0);
541 MODULE_PARM_DESC(read_only
, "Don't set any values, read only mode");