Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | adm1021.c - Part of lm_sensors, Linux kernel modules for hardware | |
c83c41e4 | 3 | monitoring |
1da177e4 LT |
4 | Copyright (c) 1998, 1999 Frodo Looijaard <frodol@dds.nl> and |
5 | Philip Edelbrock <phil@netroedge.com> | |
6 | ||
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. | |
11 | ||
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. | |
16 | ||
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. | |
20 | */ | |
21 | ||
1da177e4 LT |
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> | |
943b0830 MH |
27 | #include <linux/hwmon.h> |
28 | #include <linux/err.h> | |
9a61bf63 | 29 | #include <linux/mutex.h> |
1da177e4 LT |
30 | |
31 | ||
32 | /* Addresses to scan */ | |
33 | static unsigned short normal_i2c[] = { 0x18, 0x19, 0x1a, | |
34 | 0x29, 0x2a, 0x2b, | |
c83c41e4 | 35 | 0x4c, 0x4d, 0x4e, |
1da177e4 | 36 | I2C_CLIENT_END }; |
1da177e4 LT |
37 | |
38 | /* Insmod parameters */ | |
c83c41e4 KH |
39 | I2C_CLIENT_INSMOD_8(adm1021, adm1023, max1617, max1617a, thmc10, lm84, gl523sm, |
40 | mc1066); | |
1da177e4 LT |
41 | |
42 | /* adm1021 constants specified below */ | |
43 | ||
44 | /* The adm1021 registers */ | |
45 | /* Read-only */ | |
46 | #define ADM1021_REG_TEMP 0x00 | |
47 | #define ADM1021_REG_REMOTE_TEMP 0x01 | |
48 | #define ADM1021_REG_STATUS 0x02 | |
c83c41e4 KH |
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 | |
1da177e4 LT |
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 */ | |
c83c41e4 KH |
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 | |
1da177e4 LT |
64 | /* limits */ |
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 | |
73 | /* write-only */ | |
74 | #define ADM1021_REG_ONESHOT 0x0F | |
75 | ||
76 | ||
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*/ | |
c83c41e4 | 82 | #define TEMP_TO_REG(val) SENSORS_LIMIT((val) / 1000, -128, 127) |
1da177e4 LT |
83 | |
84 | /* Initial values */ | |
85 | ||
c83c41e4 KH |
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 | |
1da177e4 LT |
89 | clearing it. Weird, ey? --Phil */ |
90 | ||
91 | /* Each client has this additional data */ | |
92 | struct adm1021_data { | |
93 | struct i2c_client client; | |
1beeffe4 | 94 | struct device *hwmon_dev; |
1da177e4 LT |
95 | enum chips type; |
96 | ||
9a61bf63 | 97 | struct mutex update_lock; |
1da177e4 LT |
98 | char valid; /* !=0 if following fields are valid */ |
99 | unsigned long last_updated; /* In jiffies */ | |
100 | ||
c83c41e4 KH |
101 | s8 temp_max; /* Register values */ |
102 | s8 temp_hyst; | |
103 | s8 temp_input; | |
104 | s8 remote_temp_max; | |
105 | s8 remote_temp_hyst; | |
106 | s8 remote_temp_input; | |
1da177e4 | 107 | u8 alarms; |
1da177e4 LT |
108 | /* Special values for ADM1023 only */ |
109 | u8 remote_temp_prec; | |
110 | u8 remote_temp_os_prec; | |
111 | u8 remote_temp_hyst_prec; | |
112 | u8 remote_temp_offset; | |
113 | u8 remote_temp_offset_prec; | |
114 | }; | |
115 | ||
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); | |
1da177e4 LT |
120 | static struct adm1021_data *adm1021_update_device(struct device *dev); |
121 | ||
122 | /* (amalysh) read only mode, otherwise any limit's writing confuse BIOS */ | |
02002963 | 123 | static int read_only; |
1da177e4 LT |
124 | |
125 | ||
126 | /* This is the driver that will be inserted */ | |
127 | static struct i2c_driver adm1021_driver = { | |
cdaf7934 | 128 | .driver = { |
cdaf7934 LR |
129 | .name = "adm1021", |
130 | }, | |
1da177e4 | 131 | .id = I2C_DRIVERID_ADM1021, |
1da177e4 LT |
132 | .attach_adapter = adm1021_attach_adapter, |
133 | .detach_client = adm1021_detach_client, | |
134 | }; | |
135 | ||
c83c41e4 KH |
136 | #define show(value) \ |
137 | static ssize_t show_##value(struct device *dev, \ | |
138 | struct device_attribute *attr, char *buf) \ | |
1da177e4 LT |
139 | { \ |
140 | struct adm1021_data *data = adm1021_update_device(dev); \ | |
c83c41e4 | 141 | return sprintf(buf, "%d\n", 1000 * data->value); \ |
1da177e4 LT |
142 | } |
143 | show(temp_max); | |
144 | show(temp_hyst); | |
145 | show(temp_input); | |
146 | show(remote_temp_max); | |
147 | show(remote_temp_hyst); | |
148 | show(remote_temp_input); | |
149 | ||
c83c41e4 KH |
150 | static ssize_t show_alarms(struct device *dev, |
151 | struct device_attribute *attr, | |
152 | char *buf) | |
153 | { | |
154 | struct adm1021_data *data = adm1021_update_device(dev); | |
155 | return sprintf(buf, "%u\n", data->alarms); | |
1da177e4 | 156 | } |
c83c41e4 KH |
157 | |
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) \ | |
162 | { \ | |
163 | struct i2c_client *client = to_i2c_client(dev); \ | |
164 | struct adm1021_data *data = i2c_get_clientdata(client); \ | |
5bfedac0 | 165 | long temp = simple_strtol(buf, NULL, 10); \ |
c83c41e4 KH |
166 | \ |
167 | mutex_lock(&data->update_lock); \ | |
168 | data->value = TEMP_TO_REG(temp); \ | |
169 | if (!read_only) \ | |
170 | i2c_smbus_write_byte_data(client, reg, data->value); \ | |
171 | mutex_unlock(&data->update_lock); \ | |
172 | return count; \ | |
1da177e4 LT |
173 | } |
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); | |
178 | ||
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); | |
1da177e4 LT |
186 | |
187 | ||
188 | static int adm1021_attach_adapter(struct i2c_adapter *adapter) | |
189 | { | |
190 | if (!(adapter->class & I2C_CLASS_HWMON)) | |
191 | return 0; | |
2ed2dc3c | 192 | return i2c_probe(adapter, &addr_data, adm1021_detect); |
1da177e4 LT |
193 | } |
194 | ||
681c6f7a MH |
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, | |
203 | NULL | |
204 | }; | |
205 | ||
206 | static const struct attribute_group adm1021_group = { | |
207 | .attrs = adm1021_attributes, | |
208 | }; | |
209 | ||
1da177e4 LT |
210 | static int adm1021_detect(struct i2c_adapter *adapter, int address, int kind) |
211 | { | |
212 | int i; | |
c83c41e4 | 213 | struct i2c_client *client; |
1da177e4 LT |
214 | struct adm1021_data *data; |
215 | int err = 0; | |
216 | const char *type_name = ""; | |
c83c41e4 | 217 | int conv_rate, status, config; |
1da177e4 | 218 | |
c83c41e4 KH |
219 | if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) { |
220 | pr_debug("adm1021: detect failed, " | |
221 | "smbus byte data not supported!\n"); | |
1da177e4 | 222 | goto error0; |
c83c41e4 | 223 | } |
1da177e4 LT |
224 | |
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. | |
c83c41e4 | 227 | But it allows us to access adm1021 register values. */ |
1da177e4 | 228 | |
ba9c2e8d | 229 | if (!(data = kzalloc(sizeof(struct adm1021_data), GFP_KERNEL))) { |
c83c41e4 | 230 | pr_debug("adm1021: detect failed, kzalloc failed!\n"); |
1da177e4 LT |
231 | err = -ENOMEM; |
232 | goto error0; | |
233 | } | |
1da177e4 | 234 | |
c83c41e4 KH |
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); | |
1da177e4 LT |
244 | |
245 | /* Now, we do the remaining detection. */ | |
246 | if (kind < 0) { | |
c83c41e4 KH |
247 | if ((status & 0x03) != 0x00 || (config & 0x3F) != 0x00 |
248 | || (conv_rate & 0xF8) != 0x00) { | |
249 | pr_debug("adm1021: detect failed, " | |
250 | "chip not detected!\n"); | |
1da177e4 LT |
251 | err = -ENODEV; |
252 | goto error1; | |
253 | } | |
254 | } | |
255 | ||
256 | /* Determine the chip type. */ | |
257 | if (kind <= 0) { | |
c83c41e4 | 258 | i = i2c_smbus_read_byte_data(client, ADM1021_REG_MAN_ID); |
1da177e4 | 259 | if (i == 0x41) |
c83c41e4 KH |
260 | if ((i2c_smbus_read_byte_data(client, |
261 | ADM1021_REG_DEV_ID) & 0xF0) == 0x30) | |
1da177e4 LT |
262 | kind = adm1023; |
263 | else | |
264 | kind = adm1021; | |
265 | else if (i == 0x49) | |
266 | kind = thmc10; | |
267 | else if (i == 0x23) | |
268 | kind = gl523sm; | |
269 | else if ((i == 0x4d) && | |
c83c41e4 KH |
270 | (i2c_smbus_read_byte_data(client, |
271 | ADM1021_REG_DEV_ID) == 0x01)) | |
1da177e4 LT |
272 | kind = max1617a; |
273 | else if (i == 0x54) | |
274 | kind = mc1066; | |
275 | /* LM84 Mfr ID in a different place, and it has more unused bits */ | |
c83c41e4 KH |
276 | else if (conv_rate == 0x00 |
277 | && (kind == 0 /* skip extra detection */ | |
278 | || ((config & 0x7F) == 0x00 | |
279 | && (status & 0xAB) == 0x00))) | |
1da177e4 LT |
280 | kind = lm84; |
281 | else | |
282 | kind = max1617; | |
283 | } | |
284 | ||
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) { | |
296 | type_name = "lm84"; | |
297 | } else if (kind == gl523sm) { | |
298 | type_name = "gl523sm"; | |
299 | } else if (kind == mc1066) { | |
300 | type_name = "mc1066"; | |
301 | } | |
c83c41e4 KH |
302 | pr_debug("adm1021: Detected chip %s at adapter %d, address 0x%02x.\n", |
303 | type_name, i2c_adapter_id(adapter), address); | |
1da177e4 | 304 | |
c83c41e4 KH |
305 | /* Fill in the remaining client fields */ |
306 | strlcpy(client->name, type_name, I2C_NAME_SIZE); | |
1da177e4 | 307 | data->type = kind; |
9a61bf63 | 308 | mutex_init(&data->update_lock); |
1da177e4 LT |
309 | |
310 | /* Tell the I2C layer a new client has arrived */ | |
c83c41e4 | 311 | if ((err = i2c_attach_client(client))) |
1da177e4 LT |
312 | goto error1; |
313 | ||
314 | /* Initialize the ADM1021 chip */ | |
c83c41e4 KH |
315 | if (kind != lm84 && !read_only) |
316 | adm1021_init_client(client); | |
1da177e4 LT |
317 | |
318 | /* Register sysfs hooks */ | |
c83c41e4 | 319 | if ((err = sysfs_create_group(&client->dev.kobj, &adm1021_group))) |
681c6f7a MH |
320 | goto error2; |
321 | ||
1beeffe4 TJ |
322 | data->hwmon_dev = hwmon_device_register(&client->dev); |
323 | if (IS_ERR(data->hwmon_dev)) { | |
324 | err = PTR_ERR(data->hwmon_dev); | |
681c6f7a | 325 | goto error3; |
943b0830 MH |
326 | } |
327 | ||
1da177e4 LT |
328 | return 0; |
329 | ||
681c6f7a | 330 | error3: |
c83c41e4 | 331 | sysfs_remove_group(&client->dev.kobj, &adm1021_group); |
943b0830 | 332 | error2: |
c83c41e4 | 333 | i2c_detach_client(client); |
1da177e4 LT |
334 | error1: |
335 | kfree(data); | |
336 | error0: | |
337 | return err; | |
338 | } | |
339 | ||
340 | static void adm1021_init_client(struct i2c_client *client) | |
341 | { | |
342 | /* Enable ADC and disable suspend mode */ | |
c83c41e4 KH |
343 | i2c_smbus_write_byte_data(client, ADM1021_REG_CONFIG_W, |
344 | i2c_smbus_read_byte_data(client, ADM1021_REG_CONFIG_R) & 0xBF); | |
1da177e4 | 345 | /* Set Conversion rate to 1/sec (this can be tinkered with) */ |
c83c41e4 | 346 | i2c_smbus_write_byte_data(client, ADM1021_REG_CONV_RATE_W, 0x04); |
1da177e4 LT |
347 | } |
348 | ||
349 | static int adm1021_detach_client(struct i2c_client *client) | |
350 | { | |
943b0830 | 351 | struct adm1021_data *data = i2c_get_clientdata(client); |
1da177e4 LT |
352 | int err; |
353 | ||
1beeffe4 | 354 | hwmon_device_unregister(data->hwmon_dev); |
681c6f7a | 355 | sysfs_remove_group(&client->dev.kobj, &adm1021_group); |
943b0830 | 356 | |
7bef5594 | 357 | if ((err = i2c_detach_client(client))) |
1da177e4 | 358 | return err; |
1da177e4 | 359 | |
943b0830 | 360 | kfree(data); |
1da177e4 LT |
361 | return 0; |
362 | } | |
363 | ||
1da177e4 LT |
364 | static struct adm1021_data *adm1021_update_device(struct device *dev) |
365 | { | |
366 | struct i2c_client *client = to_i2c_client(dev); | |
367 | struct adm1021_data *data = i2c_get_clientdata(client); | |
368 | ||
9a61bf63 | 369 | mutex_lock(&data->update_lock); |
1da177e4 LT |
370 | |
371 | if (time_after(jiffies, data->last_updated + HZ + HZ / 2) | |
372 | || !data->valid) { | |
373 | dev_dbg(&client->dev, "Starting adm1021 update\n"); | |
374 | ||
c83c41e4 KH |
375 | data->temp_input = i2c_smbus_read_byte_data(client, |
376 | ADM1021_REG_TEMP); | |
377 | data->temp_max = i2c_smbus_read_byte_data(client, | |
378 | ADM1021_REG_TOS_R); | |
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; | |
1da177e4 | 389 | if (data->type == adm1023) { |
c83c41e4 KH |
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); | |
1da177e4 LT |
405 | } |
406 | data->last_updated = jiffies; | |
407 | data->valid = 1; | |
408 | } | |
409 | ||
9a61bf63 | 410 | mutex_unlock(&data->update_lock); |
1da177e4 LT |
411 | |
412 | return data; | |
413 | } | |
414 | ||
415 | static int __init sensors_adm1021_init(void) | |
416 | { | |
417 | return i2c_add_driver(&adm1021_driver); | |
418 | } | |
419 | ||
420 | static void __exit sensors_adm1021_exit(void) | |
421 | { | |
422 | i2c_del_driver(&adm1021_driver); | |
423 | } | |
424 | ||
425 | MODULE_AUTHOR ("Frodo Looijaard <frodol@dds.nl> and " | |
426 | "Philip Edelbrock <phil@netroedge.com>"); | |
427 | MODULE_DESCRIPTION("adm1021 driver"); | |
428 | MODULE_LICENSE("GPL"); | |
429 | ||
430 | module_param(read_only, bool, 0); | |
431 | MODULE_PARM_DESC(read_only, "Don't set any values, read only mode"); | |
432 | ||
433 | module_init(sensors_adm1021_init) | |
434 | module_exit(sensors_adm1021_exit) |