blob: 1ad0a885c5a5795034759dfc93c9c13015d211d5 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 adm1021.c - Part of lm_sensors, Linux kernel modules for hardware
Krzysztof Heltc83c41e2007-07-19 08:00:17 +02003 monitoring
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 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
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/module.h>
23#include <linux/init.h>
24#include <linux/slab.h>
25#include <linux/jiffies.h>
26#include <linux/i2c.h>
Mark M. Hoffman943b0832005-07-15 21:39:18 -040027#include <linux/hwmon.h>
Krzysztof Helta8d6646e2007-09-09 19:51:14 +020028#include <linux/hwmon-sysfs.h>
Mark M. Hoffman943b0832005-07-15 21:39:18 -040029#include <linux/err.h>
Ingo Molnar9a61bf62006-01-18 23:19:26 +010030#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
32
33/* Addresses to scan */
Mark M. Hoffman25e9c862008-02-17 22:28:03 -050034static const unsigned short normal_i2c[] = {
35 0x18, 0x19, 0x1a, 0x29, 0x2a, 0x2b, 0x4c, 0x4d, 0x4e, I2C_CLIENT_END };
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
Jean Delvaree5e9f442009-12-14 21:17:27 +010037enum chips {
38 adm1021, adm1023, max1617, max1617a, thmc10, lm84, gl523sm, mc1066 };
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
40/* adm1021 constants specified below */
41
42/* The adm1021 registers */
43/* Read-only */
Krzysztof Helta8d6646e2007-09-09 19:51:14 +020044/* For nr in 0-1 */
45#define ADM1021_REG_TEMP(nr) (nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#define ADM1021_REG_STATUS 0x02
Krzysztof Heltc83c41e2007-07-19 08:00:17 +020047/* 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
Linus Torvalds1da177e2005-04-16 15:20:36 -070051/* 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 */
Krzysztof Heltc83c41e2007-07-19 08:00:17 +020057#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
Linus Torvalds1da177e2005-04-16 15:20:36 -070062/* limits */
Krzysztof Helta8d6646e2007-09-09 19:51:14 +020063/* 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))
Linus Torvalds1da177e2005-04-16 15:20:36 -070068/* write-only */
69#define ADM1021_REG_ONESHOT 0x0F
70
Linus Torvalds1da177e2005-04-16 15:20:36 -070071/* Initial values */
72
Krzysztof Heltc83c41e2007-07-19 08:00:17 +020073/* Note: Even though I left the low and high limits named os and hyst,
74they don't quite work like a thermostat the way the LM75 does. I.e.,
75a lower temp than THYST actually triggers an alarm instead of
Linus Torvalds1da177e2005-04-16 15:20:36 -070076clearing it. Weird, ey? --Phil */
77
78/* Each client has this additional data */
79struct adm1021_data {
Tony Jones1beeffe2007-08-20 13:46:20 -070080 struct device *hwmon_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 enum chips type;
82
Ingo Molnar9a61bf62006-01-18 23:19:26 +010083 struct mutex update_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 char valid; /* !=0 if following fields are valid */
Michael Abbott905ffdc2009-09-21 17:04:47 -070085 char low_power; /* !=0 if device in low power mode */
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 unsigned long last_updated; /* In jiffies */
87
Michael Abbottf2668892009-09-21 17:04:46 -070088 int temp_max[2]; /* Register values */
89 int temp_min[2];
90 int temp[2];
Krzysztof Helta8d6646e2007-09-09 19:51:14 +020091 u8 alarms;
92 /* Special values for ADM1023 only */
Krzysztof Helta8d6646e2007-09-09 19:51:14 +020093 u8 remote_temp_offset;
94 u8 remote_temp_offset_prec;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095};
96
Jean Delvare65817ed2008-07-16 19:30:08 +020097static int adm1021_probe(struct i2c_client *client,
98 const struct i2c_device_id *id);
Jean Delvare310ec792009-12-14 21:17:23 +010099static int adm1021_detect(struct i2c_client *client,
Jean Delvare65817ed2008-07-16 19:30:08 +0200100 struct i2c_board_info *info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101static void adm1021_init_client(struct i2c_client *client);
Jean Delvare65817ed2008-07-16 19:30:08 +0200102static int adm1021_remove(struct i2c_client *client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103static struct adm1021_data *adm1021_update_device(struct device *dev);
104
105/* (amalysh) read only mode, otherwise any limit's writing confuse BIOS */
Jean Delvare02002962005-09-25 16:26:44 +0200106static int read_only;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
108
Jean Delvare65817ed2008-07-16 19:30:08 +0200109static const struct i2c_device_id adm1021_id[] = {
110 { "adm1021", adm1021 },
111 { "adm1023", adm1023 },
112 { "max1617", max1617 },
113 { "max1617a", max1617a },
114 { "thmc10", thmc10 },
115 { "lm84", lm84 },
116 { "gl523sm", gl523sm },
117 { "mc1066", mc1066 },
118 { }
119};
120MODULE_DEVICE_TABLE(i2c, adm1021_id);
121
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122/* This is the driver that will be inserted */
123static struct i2c_driver adm1021_driver = {
Jean Delvare65817ed2008-07-16 19:30:08 +0200124 .class = I2C_CLASS_HWMON,
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100125 .driver = {
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100126 .name = "adm1021",
127 },
Jean Delvare65817ed2008-07-16 19:30:08 +0200128 .probe = adm1021_probe,
129 .remove = adm1021_remove,
130 .id_table = adm1021_id,
131 .detect = adm1021_detect,
Jean Delvarec3813d62009-12-14 21:17:25 +0100132 .address_list = normal_i2c,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133};
134
Krzysztof Helta8d6646e2007-09-09 19:51:14 +0200135static ssize_t show_temp(struct device *dev,
136 struct device_attribute *devattr, char *buf)
137{
138 int index = to_sensor_dev_attr(devattr)->index;
139 struct adm1021_data *data = adm1021_update_device(dev);
140
Michael Abbottf2668892009-09-21 17:04:46 -0700141 return sprintf(buf, "%d\n", data->temp[index]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142}
Krzysztof Helta8d6646e2007-09-09 19:51:14 +0200143
144static ssize_t show_temp_max(struct device *dev,
145 struct device_attribute *devattr, char *buf)
146{
147 int index = to_sensor_dev_attr(devattr)->index;
148 struct adm1021_data *data = adm1021_update_device(dev);
149
Michael Abbottf2668892009-09-21 17:04:46 -0700150 return sprintf(buf, "%d\n", data->temp_max[index]);
Krzysztof Helta8d6646e2007-09-09 19:51:14 +0200151}
152
153static ssize_t show_temp_min(struct device *dev,
154 struct device_attribute *devattr, char *buf)
155{
156 int index = to_sensor_dev_attr(devattr)->index;
157 struct adm1021_data *data = adm1021_update_device(dev);
158
Michael Abbottf2668892009-09-21 17:04:46 -0700159 return sprintf(buf, "%d\n", data->temp_min[index]);
Krzysztof Helta8d6646e2007-09-09 19:51:14 +0200160}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
Krzysztof Helt739ba242007-09-09 19:16:03 +0200162static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
163 char *buf)
164{
165 int index = to_sensor_dev_attr(attr)->index;
166 struct adm1021_data *data = adm1021_update_device(dev);
167 return sprintf(buf, "%u\n", (data->alarms >> index) & 1);
168}
169
Krzysztof Heltc83c41e2007-07-19 08:00:17 +0200170static ssize_t show_alarms(struct device *dev,
171 struct device_attribute *attr,
172 char *buf)
173{
174 struct adm1021_data *data = adm1021_update_device(dev);
175 return sprintf(buf, "%u\n", data->alarms);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177
Krzysztof Helta8d6646e2007-09-09 19:51:14 +0200178static ssize_t set_temp_max(struct device *dev,
179 struct device_attribute *devattr,
180 const char *buf, size_t count)
181{
182 int index = to_sensor_dev_attr(devattr)->index;
183 struct i2c_client *client = to_i2c_client(dev);
184 struct adm1021_data *data = i2c_get_clientdata(client);
185 long temp = simple_strtol(buf, NULL, 10) / 1000;
186
187 mutex_lock(&data->update_lock);
188 data->temp_max[index] = SENSORS_LIMIT(temp, -128, 127);
189 if (!read_only)
190 i2c_smbus_write_byte_data(client, ADM1021_REG_TOS_W(index),
191 data->temp_max[index]);
192 mutex_unlock(&data->update_lock);
193
194 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
Krzysztof Helta8d6646e2007-09-09 19:51:14 +0200197static ssize_t set_temp_min(struct device *dev,
198 struct device_attribute *devattr,
199 const char *buf, size_t count)
200{
201 int index = to_sensor_dev_attr(devattr)->index;
202 struct i2c_client *client = to_i2c_client(dev);
203 struct adm1021_data *data = i2c_get_clientdata(client);
204 long temp = simple_strtol(buf, NULL, 10) / 1000;
205
206 mutex_lock(&data->update_lock);
207 data->temp_min[index] = SENSORS_LIMIT(temp, -128, 127);
208 if (!read_only)
209 i2c_smbus_write_byte_data(client, ADM1021_REG_THYST_W(index),
210 data->temp_min[index]);
211 mutex_unlock(&data->update_lock);
212
213 return count;
214}
215
Michael Abbott905ffdc2009-09-21 17:04:47 -0700216static ssize_t show_low_power(struct device *dev,
217 struct device_attribute *devattr, char *buf)
218{
219 struct adm1021_data *data = adm1021_update_device(dev);
220 return sprintf(buf, "%d\n", data->low_power);
221}
222
223static ssize_t set_low_power(struct device *dev,
224 struct device_attribute *devattr,
225 const char *buf, size_t count)
226{
227 struct i2c_client *client = to_i2c_client(dev);
228 struct adm1021_data *data = i2c_get_clientdata(client);
229 int low_power = simple_strtol(buf, NULL, 10) != 0;
230
231 mutex_lock(&data->update_lock);
232 if (low_power != data->low_power) {
233 int config = i2c_smbus_read_byte_data(
234 client, ADM1021_REG_CONFIG_R);
235 data->low_power = low_power;
236 i2c_smbus_write_byte_data(client, ADM1021_REG_CONFIG_W,
237 (config & 0xBF) | (low_power << 6));
238 }
239 mutex_unlock(&data->update_lock);
240
241 return count;
242}
243
244
Krzysztof Helta8d6646e2007-09-09 19:51:14 +0200245static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0);
246static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp_max,
247 set_temp_max, 0);
248static SENSOR_DEVICE_ATTR(temp1_min, S_IWUSR | S_IRUGO, show_temp_min,
249 set_temp_min, 0);
250static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp, NULL, 1);
251static SENSOR_DEVICE_ATTR(temp2_max, S_IWUSR | S_IRUGO, show_temp_max,
252 set_temp_max, 1);
253static SENSOR_DEVICE_ATTR(temp2_min, S_IWUSR | S_IRUGO, show_temp_min,
254 set_temp_min, 1);
Krzysztof Helt739ba242007-09-09 19:16:03 +0200255static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, show_alarm, NULL, 6);
256static SENSOR_DEVICE_ATTR(temp1_min_alarm, S_IRUGO, show_alarm, NULL, 5);
257static SENSOR_DEVICE_ATTR(temp2_max_alarm, S_IRUGO, show_alarm, NULL, 4);
258static SENSOR_DEVICE_ATTR(temp2_min_alarm, S_IRUGO, show_alarm, NULL, 3);
259static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO, show_alarm, NULL, 2);
260
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
Michael Abbott905ffdc2009-09-21 17:04:47 -0700262static DEVICE_ATTR(low_power, S_IWUSR | S_IRUGO, show_low_power, set_low_power);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
Mark M. Hoffman681c6f72006-09-24 21:15:35 +0200264static struct attribute *adm1021_attributes[] = {
Krzysztof Helta8d6646e2007-09-09 19:51:14 +0200265 &sensor_dev_attr_temp1_max.dev_attr.attr,
266 &sensor_dev_attr_temp1_min.dev_attr.attr,
267 &sensor_dev_attr_temp1_input.dev_attr.attr,
268 &sensor_dev_attr_temp2_max.dev_attr.attr,
269 &sensor_dev_attr_temp2_min.dev_attr.attr,
270 &sensor_dev_attr_temp2_input.dev_attr.attr,
Krzysztof Helt739ba242007-09-09 19:16:03 +0200271 &sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
272 &sensor_dev_attr_temp1_min_alarm.dev_attr.attr,
273 &sensor_dev_attr_temp2_max_alarm.dev_attr.attr,
274 &sensor_dev_attr_temp2_min_alarm.dev_attr.attr,
275 &sensor_dev_attr_temp2_fault.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +0200276 &dev_attr_alarms.attr,
Michael Abbott905ffdc2009-09-21 17:04:47 -0700277 &dev_attr_low_power.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +0200278 NULL
279};
280
281static const struct attribute_group adm1021_group = {
282 .attrs = adm1021_attributes,
283};
284
Jean Delvare65817ed2008-07-16 19:30:08 +0200285/* Return 0 if detection is successful, -ENODEV otherwise */
Jean Delvare310ec792009-12-14 21:17:23 +0100286static int adm1021_detect(struct i2c_client *client,
Jean Delvare65817ed2008-07-16 19:30:08 +0200287 struct i2c_board_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288{
Jean Delvare65817ed2008-07-16 19:30:08 +0200289 struct i2c_adapter *adapter = client->adapter;
Jean Delvare8007ea32009-12-09 20:35:51 +0100290 const char *type_name;
291 int conv_rate, status, config, man_id, dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292
Krzysztof Heltc83c41e2007-07-19 08:00:17 +0200293 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
294 pr_debug("adm1021: detect failed, "
295 "smbus byte data not supported!\n");
Jean Delvare65817ed2008-07-16 19:30:08 +0200296 return -ENODEV;
Krzysztof Heltc83c41e2007-07-19 08:00:17 +0200297 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298
Krzysztof Heltc83c41e2007-07-19 08:00:17 +0200299 status = i2c_smbus_read_byte_data(client, ADM1021_REG_STATUS);
300 conv_rate = i2c_smbus_read_byte_data(client,
301 ADM1021_REG_CONV_RATE_R);
302 config = i2c_smbus_read_byte_data(client, ADM1021_REG_CONFIG_R);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303
Jean Delvare8007ea32009-12-09 20:35:51 +0100304 /* Check unused bits */
305 if ((status & 0x03) || (config & 0x3F) || (conv_rate & 0xF8)) {
306 pr_debug("adm1021: detect failed, chip not detected!\n");
307 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 }
309
310 /* Determine the chip type. */
Jean Delvare8007ea32009-12-09 20:35:51 +0100311 man_id = i2c_smbus_read_byte_data(client, ADM1021_REG_MAN_ID);
312 dev_id = i2c_smbus_read_byte_data(client, ADM1021_REG_DEV_ID);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
Jean Delvare8007ea32009-12-09 20:35:51 +0100314 if (man_id == 0x4d && dev_id == 0x01)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 type_name = "max1617a";
Jean Delvare8007ea32009-12-09 20:35:51 +0100316 else if (man_id == 0x41) {
317 if ((dev_id & 0xF0) == 0x30)
318 type_name = "adm1023";
319 else
320 type_name = "adm1021";
321 } else if (man_id == 0x49)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 type_name = "thmc10";
Jean Delvare8007ea32009-12-09 20:35:51 +0100323 else if (man_id == 0x23)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 type_name = "gl523sm";
Jean Delvare8007ea32009-12-09 20:35:51 +0100325 else if (man_id == 0x54)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 type_name = "mc1066";
Jean Delvare8007ea32009-12-09 20:35:51 +0100327 /* LM84 Mfr ID in a different place, and it has more unused bits */
328 else if (conv_rate == 0x00
329 && (config & 0x7F) == 0x00
330 && (status & 0xAB) == 0x00)
331 type_name = "lm84";
332 else
333 type_name = "max1617";
334
Krzysztof Heltc83c41e2007-07-19 08:00:17 +0200335 pr_debug("adm1021: Detected chip %s at adapter %d, address 0x%02x.\n",
Jean Delvare65817ed2008-07-16 19:30:08 +0200336 type_name, i2c_adapter_id(adapter), client->addr);
337 strlcpy(info->type, type_name, I2C_NAME_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338
Jean Delvare65817ed2008-07-16 19:30:08 +0200339 return 0;
340}
341
342static int adm1021_probe(struct i2c_client *client,
343 const struct i2c_device_id *id)
344{
345 struct adm1021_data *data;
346 int err;
347
348 data = kzalloc(sizeof(struct adm1021_data), GFP_KERNEL);
349 if (!data) {
350 pr_debug("adm1021: detect failed, kzalloc failed!\n");
351 err = -ENOMEM;
352 goto error0;
353 }
354
355 i2c_set_clientdata(client, data);
356 data->type = id->driver_data;
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100357 mutex_init(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 /* Initialize the ADM1021 chip */
Jean Delvare65817ed2008-07-16 19:30:08 +0200360 if (data->type != lm84 && !read_only)
Krzysztof Heltc83c41e2007-07-19 08:00:17 +0200361 adm1021_init_client(client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362
363 /* Register sysfs hooks */
Krzysztof Heltc83c41e2007-07-19 08:00:17 +0200364 if ((err = sysfs_create_group(&client->dev.kobj, &adm1021_group)))
Jean Delvare65817ed2008-07-16 19:30:08 +0200365 goto error1;
Mark M. Hoffman681c6f72006-09-24 21:15:35 +0200366
Tony Jones1beeffe2007-08-20 13:46:20 -0700367 data->hwmon_dev = hwmon_device_register(&client->dev);
368 if (IS_ERR(data->hwmon_dev)) {
369 err = PTR_ERR(data->hwmon_dev);
Mark M. Hoffman681c6f72006-09-24 21:15:35 +0200370 goto error3;
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400371 }
372
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 return 0;
374
Mark M. Hoffman681c6f72006-09-24 21:15:35 +0200375error3:
Krzysztof Heltc83c41e2007-07-19 08:00:17 +0200376 sysfs_remove_group(&client->dev.kobj, &adm1021_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377error1:
378 kfree(data);
379error0:
380 return err;
381}
382
383static void adm1021_init_client(struct i2c_client *client)
384{
385 /* Enable ADC and disable suspend mode */
Krzysztof Heltc83c41e2007-07-19 08:00:17 +0200386 i2c_smbus_write_byte_data(client, ADM1021_REG_CONFIG_W,
387 i2c_smbus_read_byte_data(client, ADM1021_REG_CONFIG_R) & 0xBF);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 /* Set Conversion rate to 1/sec (this can be tinkered with) */
Krzysztof Heltc83c41e2007-07-19 08:00:17 +0200389 i2c_smbus_write_byte_data(client, ADM1021_REG_CONV_RATE_W, 0x04);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390}
391
Jean Delvare65817ed2008-07-16 19:30:08 +0200392static int adm1021_remove(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393{
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400394 struct adm1021_data *data = i2c_get_clientdata(client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395
Tony Jones1beeffe2007-08-20 13:46:20 -0700396 hwmon_device_unregister(data->hwmon_dev);
Mark M. Hoffman681c6f72006-09-24 21:15:35 +0200397 sysfs_remove_group(&client->dev.kobj, &adm1021_group);
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400398
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400399 kfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 return 0;
401}
402
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403static struct adm1021_data *adm1021_update_device(struct device *dev)
404{
405 struct i2c_client *client = to_i2c_client(dev);
406 struct adm1021_data *data = i2c_get_clientdata(client);
407
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100408 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409
410 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
411 || !data->valid) {
Krzysztof Helta8d6646e2007-09-09 19:51:14 +0200412 int i;
413
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 dev_dbg(&client->dev, "Starting adm1021 update\n");
415
Krzysztof Helta8d6646e2007-09-09 19:51:14 +0200416 for (i = 0; i < 2; i++) {
Michael Abbottf2668892009-09-21 17:04:46 -0700417 data->temp[i] = 1000 *
418 (s8) i2c_smbus_read_byte_data(
419 client, ADM1021_REG_TEMP(i));
420 data->temp_max[i] = 1000 *
421 (s8) i2c_smbus_read_byte_data(
422 client, ADM1021_REG_TOS_R(i));
423 data->temp_min[i] = 1000 *
424 (s8) i2c_smbus_read_byte_data(
425 client, ADM1021_REG_THYST_R(i));
Krzysztof Helta8d6646e2007-09-09 19:51:14 +0200426 }
Krzysztof Heltc83c41e2007-07-19 08:00:17 +0200427 data->alarms = i2c_smbus_read_byte_data(client,
428 ADM1021_REG_STATUS) & 0x7c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 if (data->type == adm1023) {
Michael Abbottf2668892009-09-21 17:04:46 -0700430 /* The ADM1023 provides 3 extra bits of precision for
431 * the remote sensor in extra registers. */
432 data->temp[1] += 125 * (i2c_smbus_read_byte_data(
433 client, ADM1023_REG_REM_TEMP_PREC) >> 5);
434 data->temp_max[1] += 125 * (i2c_smbus_read_byte_data(
435 client, ADM1023_REG_REM_TOS_PREC) >> 5);
436 data->temp_min[1] += 125 * (i2c_smbus_read_byte_data(
437 client, ADM1023_REG_REM_THYST_PREC) >> 5);
Krzysztof Heltc83c41e2007-07-19 08:00:17 +0200438 data->remote_temp_offset =
439 i2c_smbus_read_byte_data(client,
440 ADM1023_REG_REM_OFFSET);
441 data->remote_temp_offset_prec =
442 i2c_smbus_read_byte_data(client,
443 ADM1023_REG_REM_OFFSET_PREC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 }
445 data->last_updated = jiffies;
446 data->valid = 1;
447 }
448
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100449 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450
451 return data;
452}
453
454static int __init sensors_adm1021_init(void)
455{
456 return i2c_add_driver(&adm1021_driver);
457}
458
459static void __exit sensors_adm1021_exit(void)
460{
461 i2c_del_driver(&adm1021_driver);
462}
463
464MODULE_AUTHOR ("Frodo Looijaard <frodol@dds.nl> and "
465 "Philip Edelbrock <phil@netroedge.com>");
466MODULE_DESCRIPTION("adm1021 driver");
467MODULE_LICENSE("GPL");
468
469module_param(read_only, bool, 0);
470MODULE_PARM_DESC(read_only, "Don't set any values, read only mode");
471
472module_init(sensors_adm1021_init)
473module_exit(sensors_adm1021_exit)