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 i2c_client
*client
;
85 const struct attribute_group
*groups
[3];
87 struct mutex update_lock
;
88 char valid
; /* !=0 if following fields are valid */
89 char low_power
; /* !=0 if device in low power mode */
90 unsigned long last_updated
; /* In jiffies */
92 int temp_max
[2]; /* Register values */
96 /* Special values for ADM1023 only */
97 u8 remote_temp_offset
;
98 u8 remote_temp_offset_prec
;
101 /* (amalysh) read only mode, otherwise any limit's writing confuse BIOS */
102 static bool read_only
;
104 static struct adm1021_data
*adm1021_update_device(struct device
*dev
)
106 struct adm1021_data
*data
= dev_get_drvdata(dev
);
107 struct i2c_client
*client
= data
->client
;
109 mutex_lock(&data
->update_lock
);
111 if (time_after(jiffies
, data
->last_updated
+ HZ
+ HZ
/ 2)
115 dev_dbg(dev
, "Starting adm1021 update\n");
117 for (i
= 0; i
< 2; i
++) {
118 data
->temp
[i
] = 1000 *
119 (s8
) i2c_smbus_read_byte_data(
120 client
, ADM1021_REG_TEMP(i
));
121 data
->temp_max
[i
] = 1000 *
122 (s8
) i2c_smbus_read_byte_data(
123 client
, ADM1021_REG_TOS_R(i
));
124 if (data
->type
!= lm84
) {
125 data
->temp_min
[i
] = 1000 *
126 (s8
) i2c_smbus_read_byte_data(client
,
127 ADM1021_REG_THYST_R(i
));
130 data
->alarms
= i2c_smbus_read_byte_data(client
,
131 ADM1021_REG_STATUS
) & 0x7c;
132 if (data
->type
== adm1023
) {
134 * The ADM1023 provides 3 extra bits of precision for
135 * the remote sensor in extra registers.
137 data
->temp
[1] += 125 * (i2c_smbus_read_byte_data(
138 client
, ADM1023_REG_REM_TEMP_PREC
) >> 5);
139 data
->temp_max
[1] += 125 * (i2c_smbus_read_byte_data(
140 client
, ADM1023_REG_REM_TOS_PREC
) >> 5);
141 data
->temp_min
[1] += 125 * (i2c_smbus_read_byte_data(
142 client
, ADM1023_REG_REM_THYST_PREC
) >> 5);
143 data
->remote_temp_offset
=
144 i2c_smbus_read_byte_data(client
,
145 ADM1023_REG_REM_OFFSET
);
146 data
->remote_temp_offset_prec
=
147 i2c_smbus_read_byte_data(client
,
148 ADM1023_REG_REM_OFFSET_PREC
);
150 data
->last_updated
= jiffies
;
154 mutex_unlock(&data
->update_lock
);
159 static ssize_t
temp_show(struct device
*dev
, struct device_attribute
*devattr
,
162 int index
= to_sensor_dev_attr(devattr
)->index
;
163 struct adm1021_data
*data
= adm1021_update_device(dev
);
165 return sprintf(buf
, "%d\n", data
->temp
[index
]);
168 static ssize_t
temp_max_show(struct device
*dev
,
169 struct device_attribute
*devattr
, char *buf
)
171 int index
= to_sensor_dev_attr(devattr
)->index
;
172 struct adm1021_data
*data
= adm1021_update_device(dev
);
174 return sprintf(buf
, "%d\n", data
->temp_max
[index
]);
177 static ssize_t
temp_min_show(struct device
*dev
,
178 struct device_attribute
*devattr
, char *buf
)
180 int index
= to_sensor_dev_attr(devattr
)->index
;
181 struct adm1021_data
*data
= adm1021_update_device(dev
);
183 return sprintf(buf
, "%d\n", data
->temp_min
[index
]);
186 static ssize_t
alarm_show(struct device
*dev
, struct device_attribute
*attr
,
189 int index
= to_sensor_dev_attr(attr
)->index
;
190 struct adm1021_data
*data
= adm1021_update_device(dev
);
191 return sprintf(buf
, "%u\n", (data
->alarms
>> index
) & 1);
194 static ssize_t
alarms_show(struct device
*dev
,
195 struct device_attribute
*attr
,
198 struct adm1021_data
*data
= adm1021_update_device(dev
);
199 return sprintf(buf
, "%u\n", data
->alarms
);
202 static ssize_t
temp_max_store(struct device
*dev
,
203 struct device_attribute
*devattr
,
204 const char *buf
, size_t count
)
206 int index
= to_sensor_dev_attr(devattr
)->index
;
207 struct adm1021_data
*data
= dev_get_drvdata(dev
);
208 struct i2c_client
*client
= data
->client
;
212 err
= kstrtol(buf
, 10, &temp
);
217 mutex_lock(&data
->update_lock
);
218 reg_val
= clamp_val(temp
, -128, 127);
219 data
->temp_max
[index
] = reg_val
* 1000;
221 i2c_smbus_write_byte_data(client
, ADM1021_REG_TOS_W(index
),
223 mutex_unlock(&data
->update_lock
);
228 static ssize_t
temp_min_store(struct device
*dev
,
229 struct device_attribute
*devattr
,
230 const char *buf
, size_t count
)
232 int index
= to_sensor_dev_attr(devattr
)->index
;
233 struct adm1021_data
*data
= dev_get_drvdata(dev
);
234 struct i2c_client
*client
= data
->client
;
238 err
= kstrtol(buf
, 10, &temp
);
243 mutex_lock(&data
->update_lock
);
244 reg_val
= clamp_val(temp
, -128, 127);
245 data
->temp_min
[index
] = reg_val
* 1000;
247 i2c_smbus_write_byte_data(client
, ADM1021_REG_THYST_W(index
),
249 mutex_unlock(&data
->update_lock
);
254 static ssize_t
low_power_show(struct device
*dev
,
255 struct device_attribute
*devattr
, char *buf
)
257 struct adm1021_data
*data
= adm1021_update_device(dev
);
258 return sprintf(buf
, "%d\n", data
->low_power
);
261 static ssize_t
low_power_store(struct device
*dev
,
262 struct device_attribute
*devattr
,
263 const char *buf
, size_t count
)
265 struct adm1021_data
*data
= dev_get_drvdata(dev
);
266 struct i2c_client
*client
= data
->client
;
271 err
= kstrtoul(buf
, 10, &val
);
274 low_power
= val
!= 0;
276 mutex_lock(&data
->update_lock
);
277 if (low_power
!= data
->low_power
) {
278 int config
= i2c_smbus_read_byte_data(
279 client
, ADM1021_REG_CONFIG_R
);
280 data
->low_power
= low_power
;
281 i2c_smbus_write_byte_data(client
, ADM1021_REG_CONFIG_W
,
282 (config
& 0xBF) | (low_power
<< 6));
284 mutex_unlock(&data
->update_lock
);
290 static SENSOR_DEVICE_ATTR_RO(temp1_input
, temp
, 0);
291 static SENSOR_DEVICE_ATTR_RW(temp1_max
, temp_max
, 0);
292 static SENSOR_DEVICE_ATTR_RW(temp1_min
, temp_min
, 0);
293 static SENSOR_DEVICE_ATTR_RO(temp2_input
, temp
, 1);
294 static SENSOR_DEVICE_ATTR_RW(temp2_max
, temp_max
, 1);
295 static SENSOR_DEVICE_ATTR_RW(temp2_min
, temp_min
, 1);
296 static SENSOR_DEVICE_ATTR_RO(temp1_max_alarm
, alarm
, 6);
297 static SENSOR_DEVICE_ATTR_RO(temp1_min_alarm
, alarm
, 5);
298 static SENSOR_DEVICE_ATTR_RO(temp2_max_alarm
, alarm
, 4);
299 static SENSOR_DEVICE_ATTR_RO(temp2_min_alarm
, alarm
, 3);
300 static SENSOR_DEVICE_ATTR_RO(temp2_fault
, alarm
, 2);
302 static DEVICE_ATTR_RO(alarms
);
303 static DEVICE_ATTR_RW(low_power
);
305 static struct attribute
*adm1021_attributes
[] = {
306 &sensor_dev_attr_temp1_max
.dev_attr
.attr
,
307 &sensor_dev_attr_temp1_input
.dev_attr
.attr
,
308 &sensor_dev_attr_temp2_max
.dev_attr
.attr
,
309 &sensor_dev_attr_temp2_input
.dev_attr
.attr
,
310 &sensor_dev_attr_temp1_max_alarm
.dev_attr
.attr
,
311 &sensor_dev_attr_temp2_max_alarm
.dev_attr
.attr
,
312 &sensor_dev_attr_temp2_fault
.dev_attr
.attr
,
313 &dev_attr_alarms
.attr
,
314 &dev_attr_low_power
.attr
,
318 static const struct attribute_group adm1021_group
= {
319 .attrs
= adm1021_attributes
,
322 static struct attribute
*adm1021_min_attributes
[] = {
323 &sensor_dev_attr_temp1_min
.dev_attr
.attr
,
324 &sensor_dev_attr_temp2_min
.dev_attr
.attr
,
325 &sensor_dev_attr_temp1_min_alarm
.dev_attr
.attr
,
326 &sensor_dev_attr_temp2_min_alarm
.dev_attr
.attr
,
330 static const struct attribute_group adm1021_min_group
= {
331 .attrs
= adm1021_min_attributes
,
334 /* Return 0 if detection is successful, -ENODEV otherwise */
335 static int adm1021_detect(struct i2c_client
*client
,
336 struct i2c_board_info
*info
)
338 struct i2c_adapter
*adapter
= client
->adapter
;
339 const char *type_name
;
340 int conv_rate
, status
, config
, man_id
, dev_id
;
342 if (!i2c_check_functionality(adapter
, I2C_FUNC_SMBUS_BYTE_DATA
)) {
343 pr_debug("detect failed, smbus byte data not supported!\n");
347 status
= i2c_smbus_read_byte_data(client
, ADM1021_REG_STATUS
);
348 conv_rate
= i2c_smbus_read_byte_data(client
,
349 ADM1021_REG_CONV_RATE_R
);
350 config
= i2c_smbus_read_byte_data(client
, ADM1021_REG_CONFIG_R
);
352 /* Check unused bits */
353 if ((status
& 0x03) || (config
& 0x3F) || (conv_rate
& 0xF8)) {
354 pr_debug("detect failed, chip not detected!\n");
358 /* Determine the chip type. */
359 man_id
= i2c_smbus_read_byte_data(client
, ADM1021_REG_MAN_ID
);
360 dev_id
= i2c_smbus_read_byte_data(client
, ADM1021_REG_DEV_ID
);
362 if (man_id
< 0 || dev_id
< 0)
365 if (man_id
== 0x4d && dev_id
== 0x01)
366 type_name
= "max1617a";
367 else if (man_id
== 0x41) {
368 if ((dev_id
& 0xF0) == 0x30)
369 type_name
= "adm1023";
370 else if ((dev_id
& 0xF0) == 0x00)
371 type_name
= "adm1021";
374 } else if (man_id
== 0x49)
375 type_name
= "thmc10";
376 else if (man_id
== 0x23)
377 type_name
= "gl523sm";
378 else if (man_id
== 0x54)
379 type_name
= "mc1066";
381 int lte
, rte
, lhi
, rhi
, llo
, rlo
;
383 /* extra checks for LM84 and MAX1617 to avoid misdetections */
385 llo
= i2c_smbus_read_byte_data(client
, ADM1021_REG_THYST_R(0));
386 rlo
= i2c_smbus_read_byte_data(client
, ADM1021_REG_THYST_R(1));
388 /* fail if any of the additional register reads failed */
389 if (llo
< 0 || rlo
< 0)
392 lte
= i2c_smbus_read_byte_data(client
, ADM1021_REG_TEMP(0));
393 rte
= i2c_smbus_read_byte_data(client
, ADM1021_REG_TEMP(1));
394 lhi
= i2c_smbus_read_byte_data(client
, ADM1021_REG_TOS_R(0));
395 rhi
= i2c_smbus_read_byte_data(client
, ADM1021_REG_TOS_R(1));
398 * Fail for negative temperatures and negative high limits.
399 * This check also catches read errors on the tested registers.
401 if ((s8
)lte
< 0 || (s8
)rte
< 0 || (s8
)lhi
< 0 || (s8
)rhi
< 0)
404 /* fail if all registers hold the same value */
405 if (lte
== rte
&& lte
== lhi
&& lte
== rhi
&& lte
== llo
410 * LM84 Mfr ID is in a different place,
411 * and it has more unused bits.
413 if (conv_rate
== 0x00
414 && (config
& 0x7F) == 0x00
415 && (status
& 0xAB) == 0x00) {
418 /* fail if low limits are larger than high limits */
419 if ((s8
)llo
> lhi
|| (s8
)rlo
> rhi
)
421 type_name
= "max1617";
425 pr_debug("Detected chip %s at adapter %d, address 0x%02x.\n",
426 type_name
, i2c_adapter_id(adapter
), client
->addr
);
427 strlcpy(info
->type
, type_name
, I2C_NAME_SIZE
);
432 static void adm1021_init_client(struct i2c_client
*client
)
434 /* Enable ADC and disable suspend mode */
435 i2c_smbus_write_byte_data(client
, ADM1021_REG_CONFIG_W
,
436 i2c_smbus_read_byte_data(client
, ADM1021_REG_CONFIG_R
) & 0xBF);
437 /* Set Conversion rate to 1/sec (this can be tinkered with) */
438 i2c_smbus_write_byte_data(client
, ADM1021_REG_CONV_RATE_W
, 0x04);
441 static int adm1021_probe(struct i2c_client
*client
,
442 const struct i2c_device_id
*id
)
444 struct device
*dev
= &client
->dev
;
445 struct adm1021_data
*data
;
446 struct device
*hwmon_dev
;
448 data
= devm_kzalloc(dev
, sizeof(struct adm1021_data
), GFP_KERNEL
);
452 data
->client
= client
;
453 data
->type
= id
->driver_data
;
454 mutex_init(&data
->update_lock
);
456 /* Initialize the ADM1021 chip */
457 if (data
->type
!= lm84
&& !read_only
)
458 adm1021_init_client(client
);
460 data
->groups
[0] = &adm1021_group
;
461 if (data
->type
!= lm84
)
462 data
->groups
[1] = &adm1021_min_group
;
464 hwmon_dev
= devm_hwmon_device_register_with_groups(dev
, client
->name
,
467 return PTR_ERR_OR_ZERO(hwmon_dev
);
470 static const struct i2c_device_id adm1021_id
[] = {
471 { "adm1021", adm1021
},
472 { "adm1023", adm1023
},
473 { "max1617", max1617
},
474 { "max1617a", max1617a
},
475 { "thmc10", thmc10
},
477 { "gl523sm", gl523sm
},
478 { "mc1066", mc1066
},
481 MODULE_DEVICE_TABLE(i2c
, adm1021_id
);
483 static struct i2c_driver adm1021_driver
= {
484 .class = I2C_CLASS_HWMON
,
488 .probe
= adm1021_probe
,
489 .id_table
= adm1021_id
,
490 .detect
= adm1021_detect
,
491 .address_list
= normal_i2c
,
494 module_i2c_driver(adm1021_driver
);
496 MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl> and "
497 "Philip Edelbrock <phil@netroedge.com>");
498 MODULE_DESCRIPTION("adm1021 driver");
499 MODULE_LICENSE("GPL");
501 module_param(read_only
, bool, 0);
502 MODULE_PARM_DESC(read_only
, "Don't set any values, read only mode");