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/err.h>
29 #include <linux/mutex.h>
32 /* Addresses to scan */
33 static unsigned short normal_i2c
[] = { 0x18, 0x19, 0x1a,
38 /* Insmod parameters */
39 I2C_CLIENT_INSMOD_8(adm1021
, adm1023
, max1617
, max1617a
, thmc10
, lm84
, gl523sm
,
42 /* adm1021 constants specified below */
44 /* The adm1021 registers */
46 #define ADM1021_REG_TEMP 0x00
47 #define ADM1021_REG_REMOTE_TEMP 0x01
48 #define ADM1021_REG_STATUS 0x02
49 /* 0x41 = AD, 0x49 = TI, 0x4D = Maxim, 0x23 = Genesys , 0x54 = Onsemi */
50 #define ADM1021_REG_MAN_ID 0xFE
51 /* ADM1021 = 0x0X, ADM1023 = 0x3X */
52 #define ADM1021_REG_DEV_ID 0xFF
53 /* These use different addresses for reading/writing */
54 #define ADM1021_REG_CONFIG_R 0x03
55 #define ADM1021_REG_CONFIG_W 0x09
56 #define ADM1021_REG_CONV_RATE_R 0x04
57 #define ADM1021_REG_CONV_RATE_W 0x0A
58 /* These are for the ADM1023's additional precision on the remote temp sensor */
59 #define ADM1023_REG_REM_TEMP_PREC 0x10
60 #define ADM1023_REG_REM_OFFSET 0x11
61 #define ADM1023_REG_REM_OFFSET_PREC 0x12
62 #define ADM1023_REG_REM_TOS_PREC 0x13
63 #define ADM1023_REG_REM_THYST_PREC 0x14
65 #define ADM1021_REG_TOS_R 0x05
66 #define ADM1021_REG_TOS_W 0x0B
67 #define ADM1021_REG_REMOTE_TOS_R 0x07
68 #define ADM1021_REG_REMOTE_TOS_W 0x0D
69 #define ADM1021_REG_THYST_R 0x06
70 #define ADM1021_REG_THYST_W 0x0C
71 #define ADM1021_REG_REMOTE_THYST_R 0x08
72 #define ADM1021_REG_REMOTE_THYST_W 0x0E
74 #define ADM1021_REG_ONESHOT 0x0F
77 /* Conversions. Rounding and limit checking is only done on the TO_REG
78 variants. Note that you should be a bit careful with which arguments
79 these macros are called: arguments may be evaluated more than once.
80 Fixing this is just not worth it. */
81 /* Conversions note: 1021 uses normal integer signed-byte format*/
82 #define TEMP_TO_REG(val) SENSORS_LIMIT((val) / 1000, -128, 127)
86 /* Note: Even though I left the low and high limits named os and hyst,
87 they don't quite work like a thermostat the way the LM75 does. I.e.,
88 a lower temp than THYST actually triggers an alarm instead of
89 clearing it. Weird, ey? --Phil */
91 /* Each client has this additional data */
93 struct i2c_client client
;
94 struct device
*hwmon_dev
;
97 struct mutex update_lock
;
98 char valid
; /* !=0 if following fields are valid */
99 unsigned long last_updated
; /* In jiffies */
101 s8 temp_max
; /* Register values */
106 s8 remote_temp_input
;
108 /* Special values for ADM1023 only */
110 u8 remote_temp_os_prec
;
111 u8 remote_temp_hyst_prec
;
112 u8 remote_temp_offset
;
113 u8 remote_temp_offset_prec
;
116 static int adm1021_attach_adapter(struct i2c_adapter
*adapter
);
117 static int adm1021_detect(struct i2c_adapter
*adapter
, int address
, int kind
);
118 static void adm1021_init_client(struct i2c_client
*client
);
119 static int adm1021_detach_client(struct i2c_client
*client
);
120 static struct adm1021_data
*adm1021_update_device(struct device
*dev
);
122 /* (amalysh) read only mode, otherwise any limit's writing confuse BIOS */
123 static int read_only
;
126 /* This is the driver that will be inserted */
127 static struct i2c_driver adm1021_driver
= {
131 .id
= I2C_DRIVERID_ADM1021
,
132 .attach_adapter
= adm1021_attach_adapter
,
133 .detach_client
= adm1021_detach_client
,
136 #define show(value) \
137 static ssize_t show_##value(struct device *dev, \
138 struct device_attribute *attr, char *buf) \
140 struct adm1021_data *data = adm1021_update_device(dev); \
141 return sprintf(buf, "%d\n", 1000 * data->value); \
146 show(remote_temp_max
);
147 show(remote_temp_hyst
);
148 show(remote_temp_input
);
150 static ssize_t
show_alarms(struct device
*dev
,
151 struct device_attribute
*attr
,
154 struct adm1021_data
*data
= adm1021_update_device(dev
);
155 return sprintf(buf
, "%u\n", data
->alarms
);
158 #define set(value, reg) \
159 static ssize_t set_##value(struct device *dev, \
160 struct device_attribute *attr, \
161 const char *buf, size_t count) \
163 struct i2c_client *client = to_i2c_client(dev); \
164 struct adm1021_data *data = i2c_get_clientdata(client); \
165 long temp = simple_strtol(buf, NULL, 10); \
167 mutex_lock(&data->update_lock); \
168 data->value = TEMP_TO_REG(temp); \
170 i2c_smbus_write_byte_data(client, reg, data->value); \
171 mutex_unlock(&data->update_lock); \
174 set(temp_max
, ADM1021_REG_TOS_W
);
175 set(temp_hyst
, ADM1021_REG_THYST_W
);
176 set(remote_temp_max
, ADM1021_REG_REMOTE_TOS_W
);
177 set(remote_temp_hyst
, ADM1021_REG_REMOTE_THYST_W
);
179 static DEVICE_ATTR(temp1_max
, S_IWUSR
| S_IRUGO
, show_temp_max
, set_temp_max
);
180 static DEVICE_ATTR(temp1_min
, S_IWUSR
| S_IRUGO
, show_temp_hyst
, set_temp_hyst
);
181 static DEVICE_ATTR(temp1_input
, S_IRUGO
, show_temp_input
, NULL
);
182 static DEVICE_ATTR(temp2_max
, S_IWUSR
| S_IRUGO
, show_remote_temp_max
, set_remote_temp_max
);
183 static DEVICE_ATTR(temp2_min
, S_IWUSR
| S_IRUGO
, show_remote_temp_hyst
, set_remote_temp_hyst
);
184 static DEVICE_ATTR(temp2_input
, S_IRUGO
, show_remote_temp_input
, NULL
);
185 static DEVICE_ATTR(alarms
, S_IRUGO
, show_alarms
, NULL
);
188 static int adm1021_attach_adapter(struct i2c_adapter
*adapter
)
190 if (!(adapter
->class & I2C_CLASS_HWMON
))
192 return i2c_probe(adapter
, &addr_data
, adm1021_detect
);
195 static struct attribute
*adm1021_attributes
[] = {
196 &dev_attr_temp1_max
.attr
,
197 &dev_attr_temp1_min
.attr
,
198 &dev_attr_temp1_input
.attr
,
199 &dev_attr_temp2_max
.attr
,
200 &dev_attr_temp2_min
.attr
,
201 &dev_attr_temp2_input
.attr
,
202 &dev_attr_alarms
.attr
,
206 static const struct attribute_group adm1021_group
= {
207 .attrs
= adm1021_attributes
,
210 static int adm1021_detect(struct i2c_adapter
*adapter
, int address
, int kind
)
213 struct i2c_client
*client
;
214 struct adm1021_data
*data
;
216 const char *type_name
= "";
217 int conv_rate
, status
, config
;
219 if (!i2c_check_functionality(adapter
, I2C_FUNC_SMBUS_BYTE_DATA
)) {
220 pr_debug("adm1021: detect failed, "
221 "smbus byte data not supported!\n");
225 /* OK. For now, we presume we have a valid client. We now create the
226 client structure, even though we cannot fill it completely yet.
227 But it allows us to access adm1021 register values. */
229 if (!(data
= kzalloc(sizeof(struct adm1021_data
), GFP_KERNEL
))) {
230 pr_debug("adm1021: detect failed, kzalloc failed!\n");
235 client
= &data
->client
;
236 i2c_set_clientdata(client
, data
);
237 client
->addr
= address
;
238 client
->adapter
= adapter
;
239 client
->driver
= &adm1021_driver
;
240 status
= i2c_smbus_read_byte_data(client
, ADM1021_REG_STATUS
);
241 conv_rate
= i2c_smbus_read_byte_data(client
,
242 ADM1021_REG_CONV_RATE_R
);
243 config
= i2c_smbus_read_byte_data(client
, ADM1021_REG_CONFIG_R
);
245 /* Now, we do the remaining detection. */
247 if ((status
& 0x03) != 0x00 || (config
& 0x3F) != 0x00
248 || (conv_rate
& 0xF8) != 0x00) {
249 pr_debug("adm1021: detect failed, "
250 "chip not detected!\n");
256 /* Determine the chip type. */
258 i
= i2c_smbus_read_byte_data(client
, ADM1021_REG_MAN_ID
);
260 if ((i2c_smbus_read_byte_data(client
,
261 ADM1021_REG_DEV_ID
) & 0xF0) == 0x30)
269 else if ((i
== 0x4d) &&
270 (i2c_smbus_read_byte_data(client
,
271 ADM1021_REG_DEV_ID
) == 0x01))
275 /* LM84 Mfr ID in a different place, and it has more unused bits */
276 else if (conv_rate
== 0x00
277 && (kind
== 0 /* skip extra detection */
278 || ((config
& 0x7F) == 0x00
279 && (status
& 0xAB) == 0x00)))
285 if (kind
== max1617
) {
286 type_name
= "max1617";
287 } else if (kind
== max1617a
) {
288 type_name
= "max1617a";
289 } else if (kind
== adm1021
) {
290 type_name
= "adm1021";
291 } else if (kind
== adm1023
) {
292 type_name
= "adm1023";
293 } else if (kind
== thmc10
) {
294 type_name
= "thmc10";
295 } else if (kind
== lm84
) {
297 } else if (kind
== gl523sm
) {
298 type_name
= "gl523sm";
299 } else if (kind
== mc1066
) {
300 type_name
= "mc1066";
302 pr_debug("adm1021: Detected chip %s at adapter %d, address 0x%02x.\n",
303 type_name
, i2c_adapter_id(adapter
), address
);
305 /* Fill in the remaining client fields */
306 strlcpy(client
->name
, type_name
, I2C_NAME_SIZE
);
308 mutex_init(&data
->update_lock
);
310 /* Tell the I2C layer a new client has arrived */
311 if ((err
= i2c_attach_client(client
)))
314 /* Initialize the ADM1021 chip */
315 if (kind
!= lm84
&& !read_only
)
316 adm1021_init_client(client
);
318 /* Register sysfs hooks */
319 if ((err
= sysfs_create_group(&client
->dev
.kobj
, &adm1021_group
)))
322 data
->hwmon_dev
= hwmon_device_register(&client
->dev
);
323 if (IS_ERR(data
->hwmon_dev
)) {
324 err
= PTR_ERR(data
->hwmon_dev
);
331 sysfs_remove_group(&client
->dev
.kobj
, &adm1021_group
);
333 i2c_detach_client(client
);
340 static void adm1021_init_client(struct i2c_client
*client
)
342 /* Enable ADC and disable suspend mode */
343 i2c_smbus_write_byte_data(client
, ADM1021_REG_CONFIG_W
,
344 i2c_smbus_read_byte_data(client
, ADM1021_REG_CONFIG_R
) & 0xBF);
345 /* Set Conversion rate to 1/sec (this can be tinkered with) */
346 i2c_smbus_write_byte_data(client
, ADM1021_REG_CONV_RATE_W
, 0x04);
349 static int adm1021_detach_client(struct i2c_client
*client
)
351 struct adm1021_data
*data
= i2c_get_clientdata(client
);
354 hwmon_device_unregister(data
->hwmon_dev
);
355 sysfs_remove_group(&client
->dev
.kobj
, &adm1021_group
);
357 if ((err
= i2c_detach_client(client
)))
364 static struct adm1021_data
*adm1021_update_device(struct device
*dev
)
366 struct i2c_client
*client
= to_i2c_client(dev
);
367 struct adm1021_data
*data
= i2c_get_clientdata(client
);
369 mutex_lock(&data
->update_lock
);
371 if (time_after(jiffies
, data
->last_updated
+ HZ
+ HZ
/ 2)
373 dev_dbg(&client
->dev
, "Starting adm1021 update\n");
375 data
->temp_input
= i2c_smbus_read_byte_data(client
,
377 data
->temp_max
= i2c_smbus_read_byte_data(client
,
379 data
->temp_hyst
= i2c_smbus_read_byte_data(client
,
380 ADM1021_REG_THYST_R
);
381 data
->remote_temp_input
= i2c_smbus_read_byte_data(client
,
382 ADM1021_REG_REMOTE_TEMP
);
383 data
->remote_temp_max
= i2c_smbus_read_byte_data(client
,
384 ADM1021_REG_REMOTE_TOS_R
);
385 data
->remote_temp_hyst
= i2c_smbus_read_byte_data(client
,
386 ADM1021_REG_REMOTE_THYST_R
);
387 data
->alarms
= i2c_smbus_read_byte_data(client
,
388 ADM1021_REG_STATUS
) & 0x7c;
389 if (data
->type
== adm1023
) {
390 data
->remote_temp_prec
=
391 i2c_smbus_read_byte_data(client
,
392 ADM1023_REG_REM_TEMP_PREC
);
393 data
->remote_temp_os_prec
=
394 i2c_smbus_read_byte_data(client
,
395 ADM1023_REG_REM_TOS_PREC
);
396 data
->remote_temp_hyst_prec
=
397 i2c_smbus_read_byte_data(client
,
398 ADM1023_REG_REM_THYST_PREC
);
399 data
->remote_temp_offset
=
400 i2c_smbus_read_byte_data(client
,
401 ADM1023_REG_REM_OFFSET
);
402 data
->remote_temp_offset_prec
=
403 i2c_smbus_read_byte_data(client
,
404 ADM1023_REG_REM_OFFSET_PREC
);
406 data
->last_updated
= jiffies
;
410 mutex_unlock(&data
->update_lock
);
415 static int __init
sensors_adm1021_init(void)
417 return i2c_add_driver(&adm1021_driver
);
420 static void __exit
sensors_adm1021_exit(void)
422 i2c_del_driver(&adm1021_driver
);
425 MODULE_AUTHOR ("Frodo Looijaard <frodol@dds.nl> and "
426 "Philip Edelbrock <phil@netroedge.com>");
427 MODULE_DESCRIPTION("adm1021 driver");
428 MODULE_LICENSE("GPL");
430 module_param(read_only
, bool, 0);
431 MODULE_PARM_DESC(read_only
, "Don't set any values, read only mode");
433 module_init(sensors_adm1021_init
)
434 module_exit(sensors_adm1021_exit
)