Commit | Line | Data |
---|---|---|
1da177e4 | 1 | /* |
21d2a8f1 GR |
2 | * adm1021.c - Part of lm_sensors, Linux kernel modules for hardware |
3 | * monitoring | |
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 | */ | |
1da177e4 | 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 */ | |
25e9c86d MH |
34 | static const unsigned short normal_i2c[] = { |
35 | 0x18, 0x19, 0x1a, 0x29, 0x2a, 0x2b, 0x4c, 0x4d, 0x4e, I2C_CLIENT_END }; | |
1da177e4 | 36 | |
e5e9f44c JD |
37 | enum chips { |
38 | adm1021, adm1023, max1617, max1617a, thmc10, lm84, gl523sm, mc1066 }; | |
1da177e4 LT |
39 | |
40 | /* adm1021 constants specified below */ | |
41 | ||
42 | /* The adm1021 registers */ | |
43 | /* Read-only */ | |
a8d6646e KH |
44 | /* For nr in 0-1 */ |
45 | #define ADM1021_REG_TEMP(nr) (nr) | |
1da177e4 | 46 | #define ADM1021_REG_STATUS 0x02 |
c83c41e4 KH |
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 | |
1da177e4 LT |
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 */ | |
c83c41e4 KH |
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 | |
1da177e4 | 62 | /* limits */ |
a8d6646e KH |
63 | /* For nr in 0-1 */ |
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)) | |
1da177e4 LT |
68 | /* write-only */ |
69 | #define ADM1021_REG_ONESHOT 0x0F | |
70 | ||
1da177e4 LT |
71 | /* Initial values */ |
72 | ||
21d2a8f1 GR |
73 | /* |
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 | |
78 | */ | |
1da177e4 LT |
79 | |
80 | /* Each client has this additional data */ | |
81 | struct adm1021_data { | |
1beeffe4 | 82 | struct device *hwmon_dev; |
1da177e4 LT |
83 | enum chips type; |
84 | ||
9a61bf63 | 85 | struct mutex update_lock; |
1da177e4 | 86 | char valid; /* !=0 if following fields are valid */ |
905ffdc3 | 87 | char low_power; /* !=0 if device in low power mode */ |
1da177e4 LT |
88 | unsigned long last_updated; /* In jiffies */ |
89 | ||
f2668895 MA |
90 | int temp_max[2]; /* Register values */ |
91 | int temp_min[2]; | |
92 | int temp[2]; | |
a8d6646e KH |
93 | u8 alarms; |
94 | /* Special values for ADM1023 only */ | |
a8d6646e KH |
95 | u8 remote_temp_offset; |
96 | u8 remote_temp_offset_prec; | |
1da177e4 LT |
97 | }; |
98 | ||
65817ed8 JD |
99 | static int adm1021_probe(struct i2c_client *client, |
100 | const struct i2c_device_id *id); | |
310ec792 | 101 | static int adm1021_detect(struct i2c_client *client, |
65817ed8 | 102 | struct i2c_board_info *info); |
1da177e4 | 103 | static void adm1021_init_client(struct i2c_client *client); |
65817ed8 | 104 | static int adm1021_remove(struct i2c_client *client); |
1da177e4 LT |
105 | static struct adm1021_data *adm1021_update_device(struct device *dev); |
106 | ||
107 | /* (amalysh) read only mode, otherwise any limit's writing confuse BIOS */ | |
90ab5ee9 | 108 | static bool read_only; |
1da177e4 LT |
109 | |
110 | ||
65817ed8 JD |
111 | static const struct i2c_device_id adm1021_id[] = { |
112 | { "adm1021", adm1021 }, | |
113 | { "adm1023", adm1023 }, | |
114 | { "max1617", max1617 }, | |
115 | { "max1617a", max1617a }, | |
116 | { "thmc10", thmc10 }, | |
117 | { "lm84", lm84 }, | |
118 | { "gl523sm", gl523sm }, | |
119 | { "mc1066", mc1066 }, | |
120 | { } | |
121 | }; | |
122 | MODULE_DEVICE_TABLE(i2c, adm1021_id); | |
123 | ||
1da177e4 LT |
124 | /* This is the driver that will be inserted */ |
125 | static struct i2c_driver adm1021_driver = { | |
65817ed8 | 126 | .class = I2C_CLASS_HWMON, |
cdaf7934 | 127 | .driver = { |
cdaf7934 LR |
128 | .name = "adm1021", |
129 | }, | |
65817ed8 JD |
130 | .probe = adm1021_probe, |
131 | .remove = adm1021_remove, | |
132 | .id_table = adm1021_id, | |
133 | .detect = adm1021_detect, | |
c3813d6a | 134 | .address_list = normal_i2c, |
1da177e4 LT |
135 | }; |
136 | ||
a8d6646e KH |
137 | static ssize_t show_temp(struct device *dev, |
138 | struct device_attribute *devattr, char *buf) | |
139 | { | |
140 | int index = to_sensor_dev_attr(devattr)->index; | |
141 | struct adm1021_data *data = adm1021_update_device(dev); | |
142 | ||
f2668895 | 143 | return sprintf(buf, "%d\n", data->temp[index]); |
a8d6646e KH |
144 | } |
145 | ||
146 | static ssize_t show_temp_max(struct device *dev, | |
147 | struct device_attribute *devattr, char *buf) | |
148 | { | |
149 | int index = to_sensor_dev_attr(devattr)->index; | |
150 | struct adm1021_data *data = adm1021_update_device(dev); | |
151 | ||
f2668895 | 152 | return sprintf(buf, "%d\n", data->temp_max[index]); |
a8d6646e KH |
153 | } |
154 | ||
155 | static ssize_t show_temp_min(struct device *dev, | |
156 | struct device_attribute *devattr, char *buf) | |
157 | { | |
158 | int index = to_sensor_dev_attr(devattr)->index; | |
159 | struct adm1021_data *data = adm1021_update_device(dev); | |
160 | ||
f2668895 | 161 | return sprintf(buf, "%d\n", data->temp_min[index]); |
1da177e4 | 162 | } |
1da177e4 | 163 | |
739ba248 KH |
164 | static ssize_t show_alarm(struct device *dev, struct device_attribute *attr, |
165 | char *buf) | |
166 | { | |
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); | |
170 | } | |
171 | ||
c83c41e4 KH |
172 | static ssize_t show_alarms(struct device *dev, |
173 | struct device_attribute *attr, | |
174 | char *buf) | |
175 | { | |
176 | struct adm1021_data *data = adm1021_update_device(dev); | |
177 | return sprintf(buf, "%u\n", data->alarms); | |
1da177e4 | 178 | } |
c83c41e4 | 179 | |
a8d6646e KH |
180 | static ssize_t set_temp_max(struct device *dev, |
181 | struct device_attribute *devattr, | |
182 | const char *buf, size_t count) | |
183 | { | |
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); | |
21d2a8f1 GR |
187 | long temp; |
188 | int err; | |
189 | ||
190 | err = kstrtol(buf, 10, &temp); | |
191 | if (err) | |
192 | return err; | |
193 | temp /= 1000; | |
a8d6646e KH |
194 | |
195 | mutex_lock(&data->update_lock); | |
2a844c14 | 196 | data->temp_max[index] = clamp_val(temp, -128, 127); |
a8d6646e KH |
197 | if (!read_only) |
198 | i2c_smbus_write_byte_data(client, ADM1021_REG_TOS_W(index), | |
199 | data->temp_max[index]); | |
200 | mutex_unlock(&data->update_lock); | |
201 | ||
202 | return count; | |
203 | } | |
204 | ||
205 | static ssize_t set_temp_min(struct device *dev, | |
206 | struct device_attribute *devattr, | |
207 | const char *buf, size_t count) | |
208 | { | |
209 | int index = to_sensor_dev_attr(devattr)->index; | |
210 | struct i2c_client *client = to_i2c_client(dev); | |
211 | struct adm1021_data *data = i2c_get_clientdata(client); | |
21d2a8f1 GR |
212 | long temp; |
213 | int err; | |
214 | ||
215 | err = kstrtol(buf, 10, &temp); | |
216 | if (err) | |
217 | return err; | |
218 | temp /= 1000; | |
a8d6646e KH |
219 | |
220 | mutex_lock(&data->update_lock); | |
2a844c14 | 221 | data->temp_min[index] = clamp_val(temp, -128, 127); |
a8d6646e KH |
222 | if (!read_only) |
223 | i2c_smbus_write_byte_data(client, ADM1021_REG_THYST_W(index), | |
224 | data->temp_min[index]); | |
225 | mutex_unlock(&data->update_lock); | |
226 | ||
227 | return count; | |
1da177e4 | 228 | } |
1da177e4 | 229 | |
905ffdc3 MA |
230 | static ssize_t show_low_power(struct device *dev, |
231 | struct device_attribute *devattr, char *buf) | |
232 | { | |
233 | struct adm1021_data *data = adm1021_update_device(dev); | |
234 | return sprintf(buf, "%d\n", data->low_power); | |
235 | } | |
236 | ||
237 | static ssize_t set_low_power(struct device *dev, | |
238 | struct device_attribute *devattr, | |
239 | const char *buf, size_t count) | |
240 | { | |
241 | struct i2c_client *client = to_i2c_client(dev); | |
242 | struct adm1021_data *data = i2c_get_clientdata(client); | |
21d2a8f1 GR |
243 | char low_power; |
244 | unsigned long val; | |
245 | int err; | |
246 | ||
247 | err = kstrtoul(buf, 10, &val); | |
248 | if (err) | |
249 | return err; | |
250 | low_power = val != 0; | |
905ffdc3 MA |
251 | |
252 | mutex_lock(&data->update_lock); | |
253 | if (low_power != data->low_power) { | |
254 | int config = i2c_smbus_read_byte_data( | |
255 | client, ADM1021_REG_CONFIG_R); | |
256 | data->low_power = low_power; | |
257 | i2c_smbus_write_byte_data(client, ADM1021_REG_CONFIG_W, | |
258 | (config & 0xBF) | (low_power << 6)); | |
259 | } | |
260 | mutex_unlock(&data->update_lock); | |
261 | ||
262 | return count; | |
263 | } | |
264 | ||
265 | ||
a8d6646e KH |
266 | static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0); |
267 | static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp_max, | |
268 | set_temp_max, 0); | |
269 | static SENSOR_DEVICE_ATTR(temp1_min, S_IWUSR | S_IRUGO, show_temp_min, | |
270 | set_temp_min, 0); | |
271 | static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp, NULL, 1); | |
272 | static SENSOR_DEVICE_ATTR(temp2_max, S_IWUSR | S_IRUGO, show_temp_max, | |
273 | set_temp_max, 1); | |
274 | static SENSOR_DEVICE_ATTR(temp2_min, S_IWUSR | S_IRUGO, show_temp_min, | |
275 | set_temp_min, 1); | |
739ba248 KH |
276 | static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, show_alarm, NULL, 6); |
277 | static SENSOR_DEVICE_ATTR(temp1_min_alarm, S_IRUGO, show_alarm, NULL, 5); | |
278 | static SENSOR_DEVICE_ATTR(temp2_max_alarm, S_IRUGO, show_alarm, NULL, 4); | |
279 | static SENSOR_DEVICE_ATTR(temp2_min_alarm, S_IRUGO, show_alarm, NULL, 3); | |
280 | static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO, show_alarm, NULL, 2); | |
281 | ||
a8d6646e | 282 | static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL); |
905ffdc3 | 283 | static DEVICE_ATTR(low_power, S_IWUSR | S_IRUGO, show_low_power, set_low_power); |
1da177e4 | 284 | |
681c6f7a | 285 | static struct attribute *adm1021_attributes[] = { |
a8d6646e | 286 | &sensor_dev_attr_temp1_max.dev_attr.attr, |
a8d6646e KH |
287 | &sensor_dev_attr_temp1_input.dev_attr.attr, |
288 | &sensor_dev_attr_temp2_max.dev_attr.attr, | |
a8d6646e | 289 | &sensor_dev_attr_temp2_input.dev_attr.attr, |
739ba248 | 290 | &sensor_dev_attr_temp1_max_alarm.dev_attr.attr, |
739ba248 | 291 | &sensor_dev_attr_temp2_max_alarm.dev_attr.attr, |
739ba248 | 292 | &sensor_dev_attr_temp2_fault.dev_attr.attr, |
681c6f7a | 293 | &dev_attr_alarms.attr, |
905ffdc3 | 294 | &dev_attr_low_power.attr, |
681c6f7a MH |
295 | NULL |
296 | }; | |
297 | ||
298 | static const struct attribute_group adm1021_group = { | |
299 | .attrs = adm1021_attributes, | |
300 | }; | |
301 | ||
68146a57 GR |
302 | static struct attribute *adm1021_min_attributes[] = { |
303 | &sensor_dev_attr_temp1_min.dev_attr.attr, | |
304 | &sensor_dev_attr_temp2_min.dev_attr.attr, | |
305 | &sensor_dev_attr_temp1_min_alarm.dev_attr.attr, | |
306 | &sensor_dev_attr_temp2_min_alarm.dev_attr.attr, | |
307 | NULL | |
308 | }; | |
309 | ||
310 | static const struct attribute_group adm1021_min_group = { | |
311 | .attrs = adm1021_min_attributes, | |
312 | }; | |
313 | ||
65817ed8 | 314 | /* Return 0 if detection is successful, -ENODEV otherwise */ |
310ec792 | 315 | static int adm1021_detect(struct i2c_client *client, |
65817ed8 | 316 | struct i2c_board_info *info) |
1da177e4 | 317 | { |
65817ed8 | 318 | struct i2c_adapter *adapter = client->adapter; |
8007ea35 JD |
319 | const char *type_name; |
320 | int conv_rate, status, config, man_id, dev_id; | |
1da177e4 | 321 | |
c83c41e4 | 322 | if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) { |
b55f3757 | 323 | pr_debug("detect failed, smbus byte data not supported!\n"); |
65817ed8 | 324 | return -ENODEV; |
1da177e4 | 325 | } |
1da177e4 | 326 | |
c83c41e4 KH |
327 | status = i2c_smbus_read_byte_data(client, ADM1021_REG_STATUS); |
328 | conv_rate = i2c_smbus_read_byte_data(client, | |
329 | ADM1021_REG_CONV_RATE_R); | |
330 | config = i2c_smbus_read_byte_data(client, ADM1021_REG_CONFIG_R); | |
1da177e4 | 331 | |
8007ea35 JD |
332 | /* Check unused bits */ |
333 | if ((status & 0x03) || (config & 0x3F) || (conv_rate & 0xF8)) { | |
b55f3757 | 334 | pr_debug("detect failed, chip not detected!\n"); |
8007ea35 | 335 | return -ENODEV; |
1da177e4 LT |
336 | } |
337 | ||
338 | /* Determine the chip type. */ | |
8007ea35 JD |
339 | man_id = i2c_smbus_read_byte_data(client, ADM1021_REG_MAN_ID); |
340 | dev_id = i2c_smbus_read_byte_data(client, ADM1021_REG_DEV_ID); | |
1da177e4 | 341 | |
591bfcfc GR |
342 | if (man_id < 0 || dev_id < 0) |
343 | return -ENODEV; | |
344 | ||
8007ea35 | 345 | if (man_id == 0x4d && dev_id == 0x01) |
1da177e4 | 346 | type_name = "max1617a"; |
8007ea35 JD |
347 | else if (man_id == 0x41) { |
348 | if ((dev_id & 0xF0) == 0x30) | |
349 | type_name = "adm1023"; | |
591bfcfc | 350 | else if ((dev_id & 0xF0) == 0x00) |
8007ea35 | 351 | type_name = "adm1021"; |
591bfcfc GR |
352 | else |
353 | return -ENODEV; | |
8007ea35 | 354 | } else if (man_id == 0x49) |
1da177e4 | 355 | type_name = "thmc10"; |
8007ea35 | 356 | else if (man_id == 0x23) |
1da177e4 | 357 | type_name = "gl523sm"; |
8007ea35 | 358 | else if (man_id == 0x54) |
1da177e4 | 359 | type_name = "mc1066"; |
591bfcfc GR |
360 | else { |
361 | int lte, rte, lhi, rhi, llo, rlo; | |
362 | ||
363 | /* extra checks for LM84 and MAX1617 to avoid misdetections */ | |
364 | ||
365 | llo = i2c_smbus_read_byte_data(client, ADM1021_REG_THYST_R(0)); | |
366 | rlo = i2c_smbus_read_byte_data(client, ADM1021_REG_THYST_R(1)); | |
367 | ||
368 | /* fail if any of the additional register reads failed */ | |
369 | if (llo < 0 || rlo < 0) | |
370 | return -ENODEV; | |
371 | ||
372 | lte = i2c_smbus_read_byte_data(client, ADM1021_REG_TEMP(0)); | |
373 | rte = i2c_smbus_read_byte_data(client, ADM1021_REG_TEMP(1)); | |
374 | lhi = i2c_smbus_read_byte_data(client, ADM1021_REG_TOS_R(0)); | |
375 | rhi = i2c_smbus_read_byte_data(client, ADM1021_REG_TOS_R(1)); | |
376 | ||
377 | /* | |
378 | * Fail for negative temperatures and negative high limits. | |
379 | * This check also catches read errors on the tested registers. | |
380 | */ | |
381 | if ((s8)lte < 0 || (s8)rte < 0 || (s8)lhi < 0 || (s8)rhi < 0) | |
382 | return -ENODEV; | |
383 | ||
384 | /* fail if all registers hold the same value */ | |
385 | if (lte == rte && lte == lhi && lte == rhi && lte == llo | |
386 | && lte == rlo) | |
387 | return -ENODEV; | |
388 | ||
389 | /* | |
390 | * LM84 Mfr ID is in a different place, | |
391 | * and it has more unused bits. | |
392 | */ | |
393 | if (conv_rate == 0x00 | |
394 | && (config & 0x7F) == 0x00 | |
395 | && (status & 0xAB) == 0x00) { | |
396 | type_name = "lm84"; | |
397 | } else { | |
398 | /* fail if low limits are larger than high limits */ | |
399 | if ((s8)llo > lhi || (s8)rlo > rhi) | |
400 | return -ENODEV; | |
401 | type_name = "max1617"; | |
402 | } | |
403 | } | |
8007ea35 | 404 | |
b55f3757 | 405 | pr_debug("Detected chip %s at adapter %d, address 0x%02x.\n", |
65817ed8 JD |
406 | type_name, i2c_adapter_id(adapter), client->addr); |
407 | strlcpy(info->type, type_name, I2C_NAME_SIZE); | |
1da177e4 | 408 | |
65817ed8 JD |
409 | return 0; |
410 | } | |
1da177e4 | 411 | |
65817ed8 JD |
412 | static int adm1021_probe(struct i2c_client *client, |
413 | const struct i2c_device_id *id) | |
414 | { | |
415 | struct adm1021_data *data; | |
416 | int err; | |
417 | ||
06ce97d6 GR |
418 | data = devm_kzalloc(&client->dev, sizeof(struct adm1021_data), |
419 | GFP_KERNEL); | |
b55f3757 | 420 | if (!data) |
06ce97d6 | 421 | return -ENOMEM; |
65817ed8 JD |
422 | |
423 | i2c_set_clientdata(client, data); | |
424 | data->type = id->driver_data; | |
425 | mutex_init(&data->update_lock); | |
1da177e4 LT |
426 | |
427 | /* Initialize the ADM1021 chip */ | |
65817ed8 | 428 | if (data->type != lm84 && !read_only) |
c83c41e4 | 429 | adm1021_init_client(client); |
1da177e4 LT |
430 | |
431 | /* Register sysfs hooks */ | |
21d2a8f1 GR |
432 | err = sysfs_create_group(&client->dev.kobj, &adm1021_group); |
433 | if (err) | |
06ce97d6 | 434 | return err; |
681c6f7a | 435 | |
68146a57 GR |
436 | if (data->type != lm84) { |
437 | err = sysfs_create_group(&client->dev.kobj, &adm1021_min_group); | |
438 | if (err) | |
439 | goto error; | |
440 | } | |
441 | ||
1beeffe4 TJ |
442 | data->hwmon_dev = hwmon_device_register(&client->dev); |
443 | if (IS_ERR(data->hwmon_dev)) { | |
444 | err = PTR_ERR(data->hwmon_dev); | |
06ce97d6 | 445 | goto error; |
943b0830 MH |
446 | } |
447 | ||
1da177e4 LT |
448 | return 0; |
449 | ||
06ce97d6 | 450 | error: |
68146a57 | 451 | sysfs_remove_group(&client->dev.kobj, &adm1021_min_group); |
c83c41e4 | 452 | sysfs_remove_group(&client->dev.kobj, &adm1021_group); |
1da177e4 LT |
453 | return err; |
454 | } | |
455 | ||
456 | static void adm1021_init_client(struct i2c_client *client) | |
457 | { | |
458 | /* Enable ADC and disable suspend mode */ | |
c83c41e4 KH |
459 | i2c_smbus_write_byte_data(client, ADM1021_REG_CONFIG_W, |
460 | i2c_smbus_read_byte_data(client, ADM1021_REG_CONFIG_R) & 0xBF); | |
1da177e4 | 461 | /* Set Conversion rate to 1/sec (this can be tinkered with) */ |
c83c41e4 | 462 | i2c_smbus_write_byte_data(client, ADM1021_REG_CONV_RATE_W, 0x04); |
1da177e4 LT |
463 | } |
464 | ||
65817ed8 | 465 | static int adm1021_remove(struct i2c_client *client) |
1da177e4 | 466 | { |
943b0830 | 467 | struct adm1021_data *data = i2c_get_clientdata(client); |
1da177e4 | 468 | |
1beeffe4 | 469 | hwmon_device_unregister(data->hwmon_dev); |
68146a57 | 470 | sysfs_remove_group(&client->dev.kobj, &adm1021_min_group); |
681c6f7a | 471 | sysfs_remove_group(&client->dev.kobj, &adm1021_group); |
943b0830 | 472 | |
1da177e4 LT |
473 | return 0; |
474 | } | |
475 | ||
1da177e4 LT |
476 | static struct adm1021_data *adm1021_update_device(struct device *dev) |
477 | { | |
478 | struct i2c_client *client = to_i2c_client(dev); | |
479 | struct adm1021_data *data = i2c_get_clientdata(client); | |
480 | ||
9a61bf63 | 481 | mutex_lock(&data->update_lock); |
1da177e4 LT |
482 | |
483 | if (time_after(jiffies, data->last_updated + HZ + HZ / 2) | |
484 | || !data->valid) { | |
a8d6646e KH |
485 | int i; |
486 | ||
1da177e4 LT |
487 | dev_dbg(&client->dev, "Starting adm1021 update\n"); |
488 | ||
a8d6646e | 489 | for (i = 0; i < 2; i++) { |
f2668895 MA |
490 | data->temp[i] = 1000 * |
491 | (s8) i2c_smbus_read_byte_data( | |
492 | client, ADM1021_REG_TEMP(i)); | |
493 | data->temp_max[i] = 1000 * | |
494 | (s8) i2c_smbus_read_byte_data( | |
495 | client, ADM1021_REG_TOS_R(i)); | |
68146a57 GR |
496 | if (data->type != lm84) { |
497 | data->temp_min[i] = 1000 * | |
498 | (s8) i2c_smbus_read_byte_data(client, | |
499 | ADM1021_REG_THYST_R(i)); | |
500 | } | |
a8d6646e | 501 | } |
c83c41e4 KH |
502 | data->alarms = i2c_smbus_read_byte_data(client, |
503 | ADM1021_REG_STATUS) & 0x7c; | |
1da177e4 | 504 | if (data->type == adm1023) { |
21d2a8f1 GR |
505 | /* |
506 | * The ADM1023 provides 3 extra bits of precision for | |
507 | * the remote sensor in extra registers. | |
508 | */ | |
f2668895 MA |
509 | data->temp[1] += 125 * (i2c_smbus_read_byte_data( |
510 | client, ADM1023_REG_REM_TEMP_PREC) >> 5); | |
511 | data->temp_max[1] += 125 * (i2c_smbus_read_byte_data( | |
512 | client, ADM1023_REG_REM_TOS_PREC) >> 5); | |
513 | data->temp_min[1] += 125 * (i2c_smbus_read_byte_data( | |
514 | client, ADM1023_REG_REM_THYST_PREC) >> 5); | |
c83c41e4 KH |
515 | data->remote_temp_offset = |
516 | i2c_smbus_read_byte_data(client, | |
517 | ADM1023_REG_REM_OFFSET); | |
518 | data->remote_temp_offset_prec = | |
519 | i2c_smbus_read_byte_data(client, | |
520 | ADM1023_REG_REM_OFFSET_PREC); | |
1da177e4 LT |
521 | } |
522 | data->last_updated = jiffies; | |
523 | data->valid = 1; | |
524 | } | |
525 | ||
9a61bf63 | 526 | mutex_unlock(&data->update_lock); |
1da177e4 LT |
527 | |
528 | return data; | |
529 | } | |
530 | ||
f0967eea | 531 | module_i2c_driver(adm1021_driver); |
1da177e4 | 532 | |
21d2a8f1 | 533 | MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl> and " |
1da177e4 LT |
534 | "Philip Edelbrock <phil@netroedge.com>"); |
535 | MODULE_DESCRIPTION("adm1021 driver"); | |
536 | MODULE_LICENSE("GPL"); | |
537 | ||
538 | module_param(read_only, bool, 0); | |
539 | MODULE_PARM_DESC(read_only, "Don't set any values, read only mode"); |