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 | 27 | #include <linux/hwmon.h> |
a8d6646e | 28 | #include <linux/hwmon-sysfs.h> |
943b0830 | 29 | #include <linux/err.h> |
9a61bf63 | 30 | #include <linux/mutex.h> |
1da177e4 LT |
31 | |
32 | ||
33 | /* Addresses to scan */ | |
34 | static unsigned short normal_i2c[] = { 0x18, 0x19, 0x1a, | |
35 | 0x29, 0x2a, 0x2b, | |
c83c41e4 | 36 | 0x4c, 0x4d, 0x4e, |
1da177e4 | 37 | I2C_CLIENT_END }; |
1da177e4 LT |
38 | |
39 | /* Insmod parameters */ | |
c83c41e4 KH |
40 | I2C_CLIENT_INSMOD_8(adm1021, adm1023, max1617, max1617a, thmc10, lm84, gl523sm, |
41 | mc1066); | |
1da177e4 LT |
42 | |
43 | /* adm1021 constants specified below */ | |
44 | ||
45 | /* The adm1021 registers */ | |
46 | /* Read-only */ | |
a8d6646e KH |
47 | /* For nr in 0-1 */ |
48 | #define ADM1021_REG_TEMP(nr) (nr) | |
1da177e4 | 49 | #define ADM1021_REG_STATUS 0x02 |
c83c41e4 KH |
50 | /* 0x41 = AD, 0x49 = TI, 0x4D = Maxim, 0x23 = Genesys , 0x54 = Onsemi */ |
51 | #define ADM1021_REG_MAN_ID 0xFE | |
52 | /* ADM1021 = 0x0X, ADM1023 = 0x3X */ | |
53 | #define ADM1021_REG_DEV_ID 0xFF | |
1da177e4 LT |
54 | /* These use different addresses for reading/writing */ |
55 | #define ADM1021_REG_CONFIG_R 0x03 | |
56 | #define ADM1021_REG_CONFIG_W 0x09 | |
57 | #define ADM1021_REG_CONV_RATE_R 0x04 | |
58 | #define ADM1021_REG_CONV_RATE_W 0x0A | |
59 | /* These are for the ADM1023's additional precision on the remote temp sensor */ | |
c83c41e4 KH |
60 | #define ADM1023_REG_REM_TEMP_PREC 0x10 |
61 | #define ADM1023_REG_REM_OFFSET 0x11 | |
62 | #define ADM1023_REG_REM_OFFSET_PREC 0x12 | |
63 | #define ADM1023_REG_REM_TOS_PREC 0x13 | |
64 | #define ADM1023_REG_REM_THYST_PREC 0x14 | |
1da177e4 | 65 | /* limits */ |
a8d6646e KH |
66 | /* For nr in 0-1 */ |
67 | #define ADM1021_REG_TOS_R(nr) (0x05 + 2 * (nr)) | |
68 | #define ADM1021_REG_TOS_W(nr) (0x0B + 2 * (nr)) | |
69 | #define ADM1021_REG_THYST_R(nr) (0x06 + 2 * (nr)) | |
70 | #define ADM1021_REG_THYST_W(nr) (0x0C + 2 * (nr)) | |
1da177e4 LT |
71 | /* write-only */ |
72 | #define ADM1021_REG_ONESHOT 0x0F | |
73 | ||
1da177e4 LT |
74 | /* Initial values */ |
75 | ||
c83c41e4 KH |
76 | /* Note: Even though I left the low and high limits named os and hyst, |
77 | they don't quite work like a thermostat the way the LM75 does. I.e., | |
78 | a lower temp than THYST actually triggers an alarm instead of | |
1da177e4 LT |
79 | clearing it. Weird, ey? --Phil */ |
80 | ||
81 | /* Each client has this additional data */ | |
82 | struct adm1021_data { | |
83 | struct i2c_client client; | |
1beeffe4 | 84 | struct device *hwmon_dev; |
1da177e4 LT |
85 | enum chips type; |
86 | ||
9a61bf63 | 87 | struct mutex update_lock; |
1da177e4 LT |
88 | char valid; /* !=0 if following fields are valid */ |
89 | unsigned long last_updated; /* In jiffies */ | |
90 | ||
a8d6646e KH |
91 | s8 temp_max[2]; /* Register values */ |
92 | s8 temp_min[2]; | |
93 | s8 temp[2]; | |
94 | u8 alarms; | |
95 | /* Special values for ADM1023 only */ | |
96 | u8 remote_temp_prec; | |
97 | u8 remote_temp_os_prec; | |
98 | u8 remote_temp_hyst_prec; | |
99 | u8 remote_temp_offset; | |
100 | u8 remote_temp_offset_prec; | |
1da177e4 LT |
101 | }; |
102 | ||
103 | static int adm1021_attach_adapter(struct i2c_adapter *adapter); | |
104 | static int adm1021_detect(struct i2c_adapter *adapter, int address, int kind); | |
105 | static void adm1021_init_client(struct i2c_client *client); | |
106 | static int adm1021_detach_client(struct i2c_client *client); | |
1da177e4 LT |
107 | static struct adm1021_data *adm1021_update_device(struct device *dev); |
108 | ||
109 | /* (amalysh) read only mode, otherwise any limit's writing confuse BIOS */ | |
02002963 | 110 | static int read_only; |
1da177e4 LT |
111 | |
112 | ||
113 | /* This is the driver that will be inserted */ | |
114 | static struct i2c_driver adm1021_driver = { | |
cdaf7934 | 115 | .driver = { |
cdaf7934 LR |
116 | .name = "adm1021", |
117 | }, | |
1da177e4 | 118 | .id = I2C_DRIVERID_ADM1021, |
1da177e4 LT |
119 | .attach_adapter = adm1021_attach_adapter, |
120 | .detach_client = adm1021_detach_client, | |
121 | }; | |
122 | ||
a8d6646e KH |
123 | static ssize_t show_temp(struct device *dev, |
124 | struct device_attribute *devattr, char *buf) | |
125 | { | |
126 | int index = to_sensor_dev_attr(devattr)->index; | |
127 | struct adm1021_data *data = adm1021_update_device(dev); | |
128 | ||
129 | return sprintf(buf, "%d\n", 1000 * data->temp[index]); | |
130 | } | |
131 | ||
132 | static ssize_t show_temp_max(struct device *dev, | |
133 | struct device_attribute *devattr, char *buf) | |
134 | { | |
135 | int index = to_sensor_dev_attr(devattr)->index; | |
136 | struct adm1021_data *data = adm1021_update_device(dev); | |
137 | ||
138 | return sprintf(buf, "%d\n", 1000 * data->temp_max[index]); | |
139 | } | |
140 | ||
141 | static ssize_t show_temp_min(struct device *dev, | |
142 | struct device_attribute *devattr, char *buf) | |
143 | { | |
144 | int index = to_sensor_dev_attr(devattr)->index; | |
145 | struct adm1021_data *data = adm1021_update_device(dev); | |
146 | ||
147 | return sprintf(buf, "%d\n", 1000 * data->temp_min[index]); | |
1da177e4 | 148 | } |
1da177e4 | 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 | 157 | |
a8d6646e KH |
158 | static ssize_t set_temp_max(struct device *dev, |
159 | struct device_attribute *devattr, | |
160 | const char *buf, size_t count) | |
161 | { | |
162 | int index = to_sensor_dev_attr(devattr)->index; | |
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) / 1000; | |
166 | ||
167 | mutex_lock(&data->update_lock); | |
168 | data->temp_max[index] = SENSORS_LIMIT(temp, -128, 127); | |
169 | if (!read_only) | |
170 | i2c_smbus_write_byte_data(client, ADM1021_REG_TOS_W(index), | |
171 | data->temp_max[index]); | |
172 | mutex_unlock(&data->update_lock); | |
173 | ||
174 | return count; | |
175 | } | |
176 | ||
177 | static ssize_t set_temp_min(struct device *dev, | |
178 | struct device_attribute *devattr, | |
179 | const char *buf, size_t count) | |
180 | { | |
181 | int index = to_sensor_dev_attr(devattr)->index; | |
182 | struct i2c_client *client = to_i2c_client(dev); | |
183 | struct adm1021_data *data = i2c_get_clientdata(client); | |
184 | long temp = simple_strtol(buf, NULL, 10) / 1000; | |
185 | ||
186 | mutex_lock(&data->update_lock); | |
187 | data->temp_min[index] = SENSORS_LIMIT(temp, -128, 127); | |
188 | if (!read_only) | |
189 | i2c_smbus_write_byte_data(client, ADM1021_REG_THYST_W(index), | |
190 | data->temp_min[index]); | |
191 | mutex_unlock(&data->update_lock); | |
192 | ||
193 | return count; | |
1da177e4 | 194 | } |
1da177e4 | 195 | |
a8d6646e KH |
196 | static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0); |
197 | static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp_max, | |
198 | set_temp_max, 0); | |
199 | static SENSOR_DEVICE_ATTR(temp1_min, S_IWUSR | S_IRUGO, show_temp_min, | |
200 | set_temp_min, 0); | |
201 | static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp, NULL, 1); | |
202 | static SENSOR_DEVICE_ATTR(temp2_max, S_IWUSR | S_IRUGO, show_temp_max, | |
203 | set_temp_max, 1); | |
204 | static SENSOR_DEVICE_ATTR(temp2_min, S_IWUSR | S_IRUGO, show_temp_min, | |
205 | set_temp_min, 1); | |
206 | static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL); | |
1da177e4 LT |
207 | |
208 | static int adm1021_attach_adapter(struct i2c_adapter *adapter) | |
209 | { | |
210 | if (!(adapter->class & I2C_CLASS_HWMON)) | |
211 | return 0; | |
2ed2dc3c | 212 | return i2c_probe(adapter, &addr_data, adm1021_detect); |
1da177e4 LT |
213 | } |
214 | ||
681c6f7a | 215 | static struct attribute *adm1021_attributes[] = { |
a8d6646e KH |
216 | &sensor_dev_attr_temp1_max.dev_attr.attr, |
217 | &sensor_dev_attr_temp1_min.dev_attr.attr, | |
218 | &sensor_dev_attr_temp1_input.dev_attr.attr, | |
219 | &sensor_dev_attr_temp2_max.dev_attr.attr, | |
220 | &sensor_dev_attr_temp2_min.dev_attr.attr, | |
221 | &sensor_dev_attr_temp2_input.dev_attr.attr, | |
681c6f7a MH |
222 | &dev_attr_alarms.attr, |
223 | NULL | |
224 | }; | |
225 | ||
226 | static const struct attribute_group adm1021_group = { | |
227 | .attrs = adm1021_attributes, | |
228 | }; | |
229 | ||
1da177e4 LT |
230 | static int adm1021_detect(struct i2c_adapter *adapter, int address, int kind) |
231 | { | |
232 | int i; | |
c83c41e4 | 233 | struct i2c_client *client; |
1da177e4 LT |
234 | struct adm1021_data *data; |
235 | int err = 0; | |
236 | const char *type_name = ""; | |
c83c41e4 | 237 | int conv_rate, status, config; |
1da177e4 | 238 | |
c83c41e4 KH |
239 | if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) { |
240 | pr_debug("adm1021: detect failed, " | |
241 | "smbus byte data not supported!\n"); | |
1da177e4 | 242 | goto error0; |
c83c41e4 | 243 | } |
1da177e4 LT |
244 | |
245 | /* OK. For now, we presume we have a valid client. We now create the | |
246 | client structure, even though we cannot fill it completely yet. | |
c83c41e4 | 247 | But it allows us to access adm1021 register values. */ |
1da177e4 | 248 | |
ba9c2e8d | 249 | if (!(data = kzalloc(sizeof(struct adm1021_data), GFP_KERNEL))) { |
c83c41e4 | 250 | pr_debug("adm1021: detect failed, kzalloc failed!\n"); |
1da177e4 LT |
251 | err = -ENOMEM; |
252 | goto error0; | |
253 | } | |
1da177e4 | 254 | |
c83c41e4 KH |
255 | client = &data->client; |
256 | i2c_set_clientdata(client, data); | |
257 | client->addr = address; | |
258 | client->adapter = adapter; | |
259 | client->driver = &adm1021_driver; | |
260 | status = i2c_smbus_read_byte_data(client, ADM1021_REG_STATUS); | |
261 | conv_rate = i2c_smbus_read_byte_data(client, | |
262 | ADM1021_REG_CONV_RATE_R); | |
263 | config = i2c_smbus_read_byte_data(client, ADM1021_REG_CONFIG_R); | |
1da177e4 LT |
264 | |
265 | /* Now, we do the remaining detection. */ | |
266 | if (kind < 0) { | |
c83c41e4 KH |
267 | if ((status & 0x03) != 0x00 || (config & 0x3F) != 0x00 |
268 | || (conv_rate & 0xF8) != 0x00) { | |
269 | pr_debug("adm1021: detect failed, " | |
270 | "chip not detected!\n"); | |
1da177e4 LT |
271 | err = -ENODEV; |
272 | goto error1; | |
273 | } | |
274 | } | |
275 | ||
276 | /* Determine the chip type. */ | |
277 | if (kind <= 0) { | |
c83c41e4 | 278 | i = i2c_smbus_read_byte_data(client, ADM1021_REG_MAN_ID); |
1da177e4 | 279 | if (i == 0x41) |
c83c41e4 KH |
280 | if ((i2c_smbus_read_byte_data(client, |
281 | ADM1021_REG_DEV_ID) & 0xF0) == 0x30) | |
1da177e4 LT |
282 | kind = adm1023; |
283 | else | |
284 | kind = adm1021; | |
285 | else if (i == 0x49) | |
286 | kind = thmc10; | |
287 | else if (i == 0x23) | |
288 | kind = gl523sm; | |
289 | else if ((i == 0x4d) && | |
c83c41e4 KH |
290 | (i2c_smbus_read_byte_data(client, |
291 | ADM1021_REG_DEV_ID) == 0x01)) | |
1da177e4 LT |
292 | kind = max1617a; |
293 | else if (i == 0x54) | |
294 | kind = mc1066; | |
295 | /* LM84 Mfr ID in a different place, and it has more unused bits */ | |
c83c41e4 KH |
296 | else if (conv_rate == 0x00 |
297 | && (kind == 0 /* skip extra detection */ | |
298 | || ((config & 0x7F) == 0x00 | |
299 | && (status & 0xAB) == 0x00))) | |
1da177e4 LT |
300 | kind = lm84; |
301 | else | |
302 | kind = max1617; | |
303 | } | |
304 | ||
305 | if (kind == max1617) { | |
306 | type_name = "max1617"; | |
307 | } else if (kind == max1617a) { | |
308 | type_name = "max1617a"; | |
309 | } else if (kind == adm1021) { | |
310 | type_name = "adm1021"; | |
311 | } else if (kind == adm1023) { | |
312 | type_name = "adm1023"; | |
313 | } else if (kind == thmc10) { | |
314 | type_name = "thmc10"; | |
315 | } else if (kind == lm84) { | |
316 | type_name = "lm84"; | |
317 | } else if (kind == gl523sm) { | |
318 | type_name = "gl523sm"; | |
319 | } else if (kind == mc1066) { | |
320 | type_name = "mc1066"; | |
321 | } | |
c83c41e4 KH |
322 | pr_debug("adm1021: Detected chip %s at adapter %d, address 0x%02x.\n", |
323 | type_name, i2c_adapter_id(adapter), address); | |
1da177e4 | 324 | |
c83c41e4 KH |
325 | /* Fill in the remaining client fields */ |
326 | strlcpy(client->name, type_name, I2C_NAME_SIZE); | |
1da177e4 | 327 | data->type = kind; |
9a61bf63 | 328 | mutex_init(&data->update_lock); |
1da177e4 LT |
329 | |
330 | /* Tell the I2C layer a new client has arrived */ | |
c83c41e4 | 331 | if ((err = i2c_attach_client(client))) |
1da177e4 LT |
332 | goto error1; |
333 | ||
334 | /* Initialize the ADM1021 chip */ | |
c83c41e4 KH |
335 | if (kind != lm84 && !read_only) |
336 | adm1021_init_client(client); | |
1da177e4 LT |
337 | |
338 | /* Register sysfs hooks */ | |
c83c41e4 | 339 | if ((err = sysfs_create_group(&client->dev.kobj, &adm1021_group))) |
681c6f7a MH |
340 | goto error2; |
341 | ||
1beeffe4 TJ |
342 | data->hwmon_dev = hwmon_device_register(&client->dev); |
343 | if (IS_ERR(data->hwmon_dev)) { | |
344 | err = PTR_ERR(data->hwmon_dev); | |
681c6f7a | 345 | goto error3; |
943b0830 MH |
346 | } |
347 | ||
1da177e4 LT |
348 | return 0; |
349 | ||
681c6f7a | 350 | error3: |
c83c41e4 | 351 | sysfs_remove_group(&client->dev.kobj, &adm1021_group); |
943b0830 | 352 | error2: |
c83c41e4 | 353 | i2c_detach_client(client); |
1da177e4 LT |
354 | error1: |
355 | kfree(data); | |
356 | error0: | |
357 | return err; | |
358 | } | |
359 | ||
360 | static void adm1021_init_client(struct i2c_client *client) | |
361 | { | |
362 | /* Enable ADC and disable suspend mode */ | |
c83c41e4 KH |
363 | i2c_smbus_write_byte_data(client, ADM1021_REG_CONFIG_W, |
364 | i2c_smbus_read_byte_data(client, ADM1021_REG_CONFIG_R) & 0xBF); | |
1da177e4 | 365 | /* Set Conversion rate to 1/sec (this can be tinkered with) */ |
c83c41e4 | 366 | i2c_smbus_write_byte_data(client, ADM1021_REG_CONV_RATE_W, 0x04); |
1da177e4 LT |
367 | } |
368 | ||
369 | static int adm1021_detach_client(struct i2c_client *client) | |
370 | { | |
943b0830 | 371 | struct adm1021_data *data = i2c_get_clientdata(client); |
1da177e4 LT |
372 | int err; |
373 | ||
1beeffe4 | 374 | hwmon_device_unregister(data->hwmon_dev); |
681c6f7a | 375 | sysfs_remove_group(&client->dev.kobj, &adm1021_group); |
943b0830 | 376 | |
7bef5594 | 377 | if ((err = i2c_detach_client(client))) |
1da177e4 | 378 | return err; |
1da177e4 | 379 | |
943b0830 | 380 | kfree(data); |
1da177e4 LT |
381 | return 0; |
382 | } | |
383 | ||
1da177e4 LT |
384 | static struct adm1021_data *adm1021_update_device(struct device *dev) |
385 | { | |
386 | struct i2c_client *client = to_i2c_client(dev); | |
387 | struct adm1021_data *data = i2c_get_clientdata(client); | |
388 | ||
9a61bf63 | 389 | mutex_lock(&data->update_lock); |
1da177e4 LT |
390 | |
391 | if (time_after(jiffies, data->last_updated + HZ + HZ / 2) | |
392 | || !data->valid) { | |
a8d6646e KH |
393 | int i; |
394 | ||
1da177e4 LT |
395 | dev_dbg(&client->dev, "Starting adm1021 update\n"); |
396 | ||
a8d6646e KH |
397 | for (i = 0; i < 2; i++) { |
398 | data->temp[i] = i2c_smbus_read_byte_data(client, | |
399 | ADM1021_REG_TEMP(i)); | |
400 | data->temp_max[i] = i2c_smbus_read_byte_data(client, | |
401 | ADM1021_REG_TOS_R(i)); | |
402 | data->temp_min[i] = i2c_smbus_read_byte_data(client, | |
403 | ADM1021_REG_THYST_R(i)); | |
404 | } | |
c83c41e4 KH |
405 | data->alarms = i2c_smbus_read_byte_data(client, |
406 | ADM1021_REG_STATUS) & 0x7c; | |
1da177e4 | 407 | if (data->type == adm1023) { |
c83c41e4 KH |
408 | data->remote_temp_prec = |
409 | i2c_smbus_read_byte_data(client, | |
410 | ADM1023_REG_REM_TEMP_PREC); | |
411 | data->remote_temp_os_prec = | |
412 | i2c_smbus_read_byte_data(client, | |
413 | ADM1023_REG_REM_TOS_PREC); | |
414 | data->remote_temp_hyst_prec = | |
415 | i2c_smbus_read_byte_data(client, | |
416 | ADM1023_REG_REM_THYST_PREC); | |
417 | data->remote_temp_offset = | |
418 | i2c_smbus_read_byte_data(client, | |
419 | ADM1023_REG_REM_OFFSET); | |
420 | data->remote_temp_offset_prec = | |
421 | i2c_smbus_read_byte_data(client, | |
422 | ADM1023_REG_REM_OFFSET_PREC); | |
1da177e4 LT |
423 | } |
424 | data->last_updated = jiffies; | |
425 | data->valid = 1; | |
426 | } | |
427 | ||
9a61bf63 | 428 | mutex_unlock(&data->update_lock); |
1da177e4 LT |
429 | |
430 | return data; | |
431 | } | |
432 | ||
433 | static int __init sensors_adm1021_init(void) | |
434 | { | |
435 | return i2c_add_driver(&adm1021_driver); | |
436 | } | |
437 | ||
438 | static void __exit sensors_adm1021_exit(void) | |
439 | { | |
440 | i2c_del_driver(&adm1021_driver); | |
441 | } | |
442 | ||
443 | MODULE_AUTHOR ("Frodo Looijaard <frodol@dds.nl> and " | |
444 | "Philip Edelbrock <phil@netroedge.com>"); | |
445 | MODULE_DESCRIPTION("adm1021 driver"); | |
446 | MODULE_LICENSE("GPL"); | |
447 | ||
448 | module_param(read_only, bool, 0); | |
449 | MODULE_PARM_DESC(read_only, "Don't set any values, read only mode"); | |
450 | ||
451 | module_init(sensors_adm1021_init) | |
452 | module_exit(sensors_adm1021_exit) |