blob: 27ad7fb0657215769fc1074f391ca083f9493fa0 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Guenter Roeck21d2a8f2012-01-14 12:45:47 -08002 * 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 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070021
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
Guenter Roeck21d2a8f2012-01-14 12:45:47 -080073/*
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 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
80/* Each client has this additional data */
81struct adm1021_data {
Tony Jones1beeffe2007-08-20 13:46:20 -070082 struct device *hwmon_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 enum chips type;
84
Ingo Molnar9a61bf62006-01-18 23:19:26 +010085 struct mutex update_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 char valid; /* !=0 if following fields are valid */
Michael Abbott905ffdc2009-09-21 17:04:47 -070087 char low_power; /* !=0 if device in low power mode */
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 unsigned long last_updated; /* In jiffies */
89
Michael Abbottf2668892009-09-21 17:04:46 -070090 int temp_max[2]; /* Register values */
91 int temp_min[2];
92 int temp[2];
Krzysztof Helta8d6646e2007-09-09 19:51:14 +020093 u8 alarms;
94 /* Special values for ADM1023 only */
Krzysztof Helta8d6646e2007-09-09 19:51:14 +020095 u8 remote_temp_offset;
96 u8 remote_temp_offset_prec;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097};
98
Jean Delvare65817ed2008-07-16 19:30:08 +020099static int adm1021_probe(struct i2c_client *client,
100 const struct i2c_device_id *id);
Jean Delvare310ec792009-12-14 21:17:23 +0100101static int adm1021_detect(struct i2c_client *client,
Jean Delvare65817ed2008-07-16 19:30:08 +0200102 struct i2c_board_info *info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103static void adm1021_init_client(struct i2c_client *client);
Jean Delvare65817ed2008-07-16 19:30:08 +0200104static int adm1021_remove(struct i2c_client *client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105static struct adm1021_data *adm1021_update_device(struct device *dev);
106
107/* (amalysh) read only mode, otherwise any limit's writing confuse BIOS */
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030108static bool read_only;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
110
Jean Delvare65817ed2008-07-16 19:30:08 +0200111static 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};
122MODULE_DEVICE_TABLE(i2c, adm1021_id);
123
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124/* This is the driver that will be inserted */
125static struct i2c_driver adm1021_driver = {
Jean Delvare65817ed2008-07-16 19:30:08 +0200126 .class = I2C_CLASS_HWMON,
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100127 .driver = {
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100128 .name = "adm1021",
129 },
Jean Delvare65817ed2008-07-16 19:30:08 +0200130 .probe = adm1021_probe,
131 .remove = adm1021_remove,
132 .id_table = adm1021_id,
133 .detect = adm1021_detect,
Jean Delvarec3813d62009-12-14 21:17:25 +0100134 .address_list = normal_i2c,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135};
136
Krzysztof Helta8d6646e2007-09-09 19:51:14 +0200137static 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
Michael Abbottf2668892009-09-21 17:04:46 -0700143 return sprintf(buf, "%d\n", data->temp[index]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144}
Krzysztof Helta8d6646e2007-09-09 19:51:14 +0200145
146static 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
Michael Abbottf2668892009-09-21 17:04:46 -0700152 return sprintf(buf, "%d\n", data->temp_max[index]);
Krzysztof Helta8d6646e2007-09-09 19:51:14 +0200153}
154
155static 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
Michael Abbottf2668892009-09-21 17:04:46 -0700161 return sprintf(buf, "%d\n", data->temp_min[index]);
Krzysztof Helta8d6646e2007-09-09 19:51:14 +0200162}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
Krzysztof Helt739ba242007-09-09 19:16:03 +0200164static 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
Krzysztof Heltc83c41e2007-07-19 08:00:17 +0200172static 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
Krzysztof Helta8d6646e2007-09-09 19:51:14 +0200180static 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);
Guenter Roeck21d2a8f2012-01-14 12:45:47 -0800187 long temp;
Axel Lin906a2fc2014-07-03 22:45:45 +0800188 int reg_val, err;
Guenter Roeck21d2a8f2012-01-14 12:45:47 -0800189
190 err = kstrtol(buf, 10, &temp);
191 if (err)
192 return err;
193 temp /= 1000;
Krzysztof Helta8d6646e2007-09-09 19:51:14 +0200194
195 mutex_lock(&data->update_lock);
Axel Lin906a2fc2014-07-03 22:45:45 +0800196 reg_val = clamp_val(temp, -128, 127);
197 data->temp_max[index] = reg_val * 1000;
Krzysztof Helta8d6646e2007-09-09 19:51:14 +0200198 if (!read_only)
199 i2c_smbus_write_byte_data(client, ADM1021_REG_TOS_W(index),
Axel Lin906a2fc2014-07-03 22:45:45 +0800200 reg_val);
Krzysztof Helta8d6646e2007-09-09 19:51:14 +0200201 mutex_unlock(&data->update_lock);
202
203 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
Krzysztof Helta8d6646e2007-09-09 19:51:14 +0200206static ssize_t set_temp_min(struct device *dev,
207 struct device_attribute *devattr,
208 const char *buf, size_t count)
209{
210 int index = to_sensor_dev_attr(devattr)->index;
211 struct i2c_client *client = to_i2c_client(dev);
212 struct adm1021_data *data = i2c_get_clientdata(client);
Guenter Roeck21d2a8f2012-01-14 12:45:47 -0800213 long temp;
Axel Lin906a2fc2014-07-03 22:45:45 +0800214 int reg_val, err;
Guenter Roeck21d2a8f2012-01-14 12:45:47 -0800215
216 err = kstrtol(buf, 10, &temp);
217 if (err)
218 return err;
219 temp /= 1000;
Krzysztof Helta8d6646e2007-09-09 19:51:14 +0200220
221 mutex_lock(&data->update_lock);
Axel Lin906a2fc2014-07-03 22:45:45 +0800222 reg_val = clamp_val(temp, -128, 127);
223 data->temp_min[index] = reg_val * 1000;
Krzysztof Helta8d6646e2007-09-09 19:51:14 +0200224 if (!read_only)
225 i2c_smbus_write_byte_data(client, ADM1021_REG_THYST_W(index),
Axel Lin906a2fc2014-07-03 22:45:45 +0800226 reg_val);
Krzysztof Helta8d6646e2007-09-09 19:51:14 +0200227 mutex_unlock(&data->update_lock);
228
229 return count;
230}
231
Michael Abbott905ffdc2009-09-21 17:04:47 -0700232static ssize_t show_low_power(struct device *dev,
233 struct device_attribute *devattr, char *buf)
234{
235 struct adm1021_data *data = adm1021_update_device(dev);
236 return sprintf(buf, "%d\n", data->low_power);
237}
238
239static ssize_t set_low_power(struct device *dev,
240 struct device_attribute *devattr,
241 const char *buf, size_t count)
242{
243 struct i2c_client *client = to_i2c_client(dev);
244 struct adm1021_data *data = i2c_get_clientdata(client);
Guenter Roeck21d2a8f2012-01-14 12:45:47 -0800245 char low_power;
246 unsigned long val;
247 int err;
248
249 err = kstrtoul(buf, 10, &val);
250 if (err)
251 return err;
252 low_power = val != 0;
Michael Abbott905ffdc2009-09-21 17:04:47 -0700253
254 mutex_lock(&data->update_lock);
255 if (low_power != data->low_power) {
256 int config = i2c_smbus_read_byte_data(
257 client, ADM1021_REG_CONFIG_R);
258 data->low_power = low_power;
259 i2c_smbus_write_byte_data(client, ADM1021_REG_CONFIG_W,
260 (config & 0xBF) | (low_power << 6));
261 }
262 mutex_unlock(&data->update_lock);
263
264 return count;
265}
266
267
Krzysztof Helta8d6646e2007-09-09 19:51:14 +0200268static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0);
269static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp_max,
270 set_temp_max, 0);
271static SENSOR_DEVICE_ATTR(temp1_min, S_IWUSR | S_IRUGO, show_temp_min,
272 set_temp_min, 0);
273static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp, NULL, 1);
274static SENSOR_DEVICE_ATTR(temp2_max, S_IWUSR | S_IRUGO, show_temp_max,
275 set_temp_max, 1);
276static SENSOR_DEVICE_ATTR(temp2_min, S_IWUSR | S_IRUGO, show_temp_min,
277 set_temp_min, 1);
Krzysztof Helt739ba242007-09-09 19:16:03 +0200278static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, show_alarm, NULL, 6);
279static SENSOR_DEVICE_ATTR(temp1_min_alarm, S_IRUGO, show_alarm, NULL, 5);
280static SENSOR_DEVICE_ATTR(temp2_max_alarm, S_IRUGO, show_alarm, NULL, 4);
281static SENSOR_DEVICE_ATTR(temp2_min_alarm, S_IRUGO, show_alarm, NULL, 3);
282static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO, show_alarm, NULL, 2);
283
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
Michael Abbott905ffdc2009-09-21 17:04:47 -0700285static DEVICE_ATTR(low_power, S_IWUSR | S_IRUGO, show_low_power, set_low_power);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286
Mark M. Hoffman681c6f72006-09-24 21:15:35 +0200287static struct attribute *adm1021_attributes[] = {
Krzysztof Helta8d6646e2007-09-09 19:51:14 +0200288 &sensor_dev_attr_temp1_max.dev_attr.attr,
289 &sensor_dev_attr_temp1_min.dev_attr.attr,
290 &sensor_dev_attr_temp1_input.dev_attr.attr,
291 &sensor_dev_attr_temp2_max.dev_attr.attr,
292 &sensor_dev_attr_temp2_min.dev_attr.attr,
293 &sensor_dev_attr_temp2_input.dev_attr.attr,
Krzysztof Helt739ba242007-09-09 19:16:03 +0200294 &sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
295 &sensor_dev_attr_temp1_min_alarm.dev_attr.attr,
296 &sensor_dev_attr_temp2_max_alarm.dev_attr.attr,
297 &sensor_dev_attr_temp2_min_alarm.dev_attr.attr,
298 &sensor_dev_attr_temp2_fault.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +0200299 &dev_attr_alarms.attr,
Michael Abbott905ffdc2009-09-21 17:04:47 -0700300 &dev_attr_low_power.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +0200301 NULL
302};
303
304static const struct attribute_group adm1021_group = {
305 .attrs = adm1021_attributes,
306};
307
Jean Delvare65817ed2008-07-16 19:30:08 +0200308/* Return 0 if detection is successful, -ENODEV otherwise */
Jean Delvare310ec792009-12-14 21:17:23 +0100309static int adm1021_detect(struct i2c_client *client,
Jean Delvare65817ed2008-07-16 19:30:08 +0200310 struct i2c_board_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311{
Jean Delvare65817ed2008-07-16 19:30:08 +0200312 struct i2c_adapter *adapter = client->adapter;
Jean Delvare8007ea352009-12-09 20:35:51 +0100313 const char *type_name;
314 int conv_rate, status, config, man_id, dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315
Krzysztof Heltc83c41e2007-07-19 08:00:17 +0200316 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
Guenter Roeckb55f3752013-01-10 10:01:24 -0800317 pr_debug("detect failed, smbus byte data not supported!\n");
Jean Delvare65817ed2008-07-16 19:30:08 +0200318 return -ENODEV;
Krzysztof Heltc83c41e2007-07-19 08:00:17 +0200319 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320
Krzysztof Heltc83c41e2007-07-19 08:00:17 +0200321 status = i2c_smbus_read_byte_data(client, ADM1021_REG_STATUS);
322 conv_rate = i2c_smbus_read_byte_data(client,
323 ADM1021_REG_CONV_RATE_R);
324 config = i2c_smbus_read_byte_data(client, ADM1021_REG_CONFIG_R);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
Jean Delvare8007ea352009-12-09 20:35:51 +0100326 /* Check unused bits */
327 if ((status & 0x03) || (config & 0x3F) || (conv_rate & 0xF8)) {
Guenter Roeckb55f3752013-01-10 10:01:24 -0800328 pr_debug("detect failed, chip not detected!\n");
Jean Delvare8007ea352009-12-09 20:35:51 +0100329 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 }
331
332 /* Determine the chip type. */
Jean Delvare8007ea352009-12-09 20:35:51 +0100333 man_id = i2c_smbus_read_byte_data(client, ADM1021_REG_MAN_ID);
334 dev_id = i2c_smbus_read_byte_data(client, ADM1021_REG_DEV_ID);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335
Guenter Roeck591bfcf2013-06-05 14:09:30 -0700336 if (man_id < 0 || dev_id < 0)
337 return -ENODEV;
338
Jean Delvare8007ea352009-12-09 20:35:51 +0100339 if (man_id == 0x4d && dev_id == 0x01)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 type_name = "max1617a";
Jean Delvare8007ea352009-12-09 20:35:51 +0100341 else if (man_id == 0x41) {
342 if ((dev_id & 0xF0) == 0x30)
343 type_name = "adm1023";
Guenter Roeck591bfcf2013-06-05 14:09:30 -0700344 else if ((dev_id & 0xF0) == 0x00)
Jean Delvare8007ea352009-12-09 20:35:51 +0100345 type_name = "adm1021";
Guenter Roeck591bfcf2013-06-05 14:09:30 -0700346 else
347 return -ENODEV;
Jean Delvare8007ea352009-12-09 20:35:51 +0100348 } else if (man_id == 0x49)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 type_name = "thmc10";
Jean Delvare8007ea352009-12-09 20:35:51 +0100350 else if (man_id == 0x23)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 type_name = "gl523sm";
Jean Delvare8007ea352009-12-09 20:35:51 +0100352 else if (man_id == 0x54)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 type_name = "mc1066";
Guenter Roeck591bfcf2013-06-05 14:09:30 -0700354 else {
355 int lte, rte, lhi, rhi, llo, rlo;
356
357 /* extra checks for LM84 and MAX1617 to avoid misdetections */
358
359 llo = i2c_smbus_read_byte_data(client, ADM1021_REG_THYST_R(0));
360 rlo = i2c_smbus_read_byte_data(client, ADM1021_REG_THYST_R(1));
361
362 /* fail if any of the additional register reads failed */
363 if (llo < 0 || rlo < 0)
364 return -ENODEV;
365
366 lte = i2c_smbus_read_byte_data(client, ADM1021_REG_TEMP(0));
367 rte = i2c_smbus_read_byte_data(client, ADM1021_REG_TEMP(1));
368 lhi = i2c_smbus_read_byte_data(client, ADM1021_REG_TOS_R(0));
369 rhi = i2c_smbus_read_byte_data(client, ADM1021_REG_TOS_R(1));
370
371 /*
372 * Fail for negative temperatures and negative high limits.
373 * This check also catches read errors on the tested registers.
374 */
375 if ((s8)lte < 0 || (s8)rte < 0 || (s8)lhi < 0 || (s8)rhi < 0)
376 return -ENODEV;
377
378 /* fail if all registers hold the same value */
379 if (lte == rte && lte == lhi && lte == rhi && lte == llo
380 && lte == rlo)
381 return -ENODEV;
382
383 /*
384 * LM84 Mfr ID is in a different place,
385 * and it has more unused bits.
386 */
387 if (conv_rate == 0x00
388 && (config & 0x7F) == 0x00
389 && (status & 0xAB) == 0x00) {
390 type_name = "lm84";
391 } else {
392 /* fail if low limits are larger than high limits */
393 if ((s8)llo > lhi || (s8)rlo > rhi)
394 return -ENODEV;
395 type_name = "max1617";
396 }
397 }
Jean Delvare8007ea352009-12-09 20:35:51 +0100398
Guenter Roeckb55f3752013-01-10 10:01:24 -0800399 pr_debug("Detected chip %s at adapter %d, address 0x%02x.\n",
Jean Delvare65817ed2008-07-16 19:30:08 +0200400 type_name, i2c_adapter_id(adapter), client->addr);
401 strlcpy(info->type, type_name, I2C_NAME_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402
Jean Delvare65817ed2008-07-16 19:30:08 +0200403 return 0;
404}
405
406static int adm1021_probe(struct i2c_client *client,
407 const struct i2c_device_id *id)
408{
409 struct adm1021_data *data;
410 int err;
411
Guenter Roeck06ce97d2012-06-02 09:57:58 -0700412 data = devm_kzalloc(&client->dev, sizeof(struct adm1021_data),
413 GFP_KERNEL);
Guenter Roeckb55f3752013-01-10 10:01:24 -0800414 if (!data)
Guenter Roeck06ce97d2012-06-02 09:57:58 -0700415 return -ENOMEM;
Jean Delvare65817ed2008-07-16 19:30:08 +0200416
417 i2c_set_clientdata(client, data);
418 data->type = id->driver_data;
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100419 mutex_init(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 /* Initialize the ADM1021 chip */
Jean Delvare65817ed2008-07-16 19:30:08 +0200422 if (data->type != lm84 && !read_only)
Krzysztof Heltc83c41e2007-07-19 08:00:17 +0200423 adm1021_init_client(client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424
425 /* Register sysfs hooks */
Guenter Roeck21d2a8f2012-01-14 12:45:47 -0800426 err = sysfs_create_group(&client->dev.kobj, &adm1021_group);
427 if (err)
Guenter Roeck06ce97d2012-06-02 09:57:58 -0700428 return err;
Mark M. Hoffman681c6f72006-09-24 21:15:35 +0200429
Tony Jones1beeffe2007-08-20 13:46:20 -0700430 data->hwmon_dev = hwmon_device_register(&client->dev);
431 if (IS_ERR(data->hwmon_dev)) {
432 err = PTR_ERR(data->hwmon_dev);
Guenter Roeck06ce97d2012-06-02 09:57:58 -0700433 goto error;
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400434 }
435
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 return 0;
437
Guenter Roeck06ce97d2012-06-02 09:57:58 -0700438error:
Krzysztof Heltc83c41e2007-07-19 08:00:17 +0200439 sysfs_remove_group(&client->dev.kobj, &adm1021_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 return err;
441}
442
443static void adm1021_init_client(struct i2c_client *client)
444{
445 /* Enable ADC and disable suspend mode */
Krzysztof Heltc83c41e2007-07-19 08:00:17 +0200446 i2c_smbus_write_byte_data(client, ADM1021_REG_CONFIG_W,
447 i2c_smbus_read_byte_data(client, ADM1021_REG_CONFIG_R) & 0xBF);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 /* Set Conversion rate to 1/sec (this can be tinkered with) */
Krzysztof Heltc83c41e2007-07-19 08:00:17 +0200449 i2c_smbus_write_byte_data(client, ADM1021_REG_CONV_RATE_W, 0x04);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450}
451
Jean Delvare65817ed2008-07-16 19:30:08 +0200452static int adm1021_remove(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453{
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400454 struct adm1021_data *data = i2c_get_clientdata(client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455
Tony Jones1beeffe2007-08-20 13:46:20 -0700456 hwmon_device_unregister(data->hwmon_dev);
Mark M. Hoffman681c6f72006-09-24 21:15:35 +0200457 sysfs_remove_group(&client->dev.kobj, &adm1021_group);
Mark M. Hoffman943b0832005-07-15 21:39:18 -0400458
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 return 0;
460}
461
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462static struct adm1021_data *adm1021_update_device(struct device *dev)
463{
464 struct i2c_client *client = to_i2c_client(dev);
465 struct adm1021_data *data = i2c_get_clientdata(client);
466
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100467 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468
469 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
470 || !data->valid) {
Krzysztof Helta8d6646e2007-09-09 19:51:14 +0200471 int i;
472
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 dev_dbg(&client->dev, "Starting adm1021 update\n");
474
Krzysztof Helta8d6646e2007-09-09 19:51:14 +0200475 for (i = 0; i < 2; i++) {
Michael Abbottf2668892009-09-21 17:04:46 -0700476 data->temp[i] = 1000 *
477 (s8) i2c_smbus_read_byte_data(
478 client, ADM1021_REG_TEMP(i));
479 data->temp_max[i] = 1000 *
480 (s8) i2c_smbus_read_byte_data(
481 client, ADM1021_REG_TOS_R(i));
482 data->temp_min[i] = 1000 *
483 (s8) i2c_smbus_read_byte_data(
484 client, ADM1021_REG_THYST_R(i));
Krzysztof Helta8d6646e2007-09-09 19:51:14 +0200485 }
Krzysztof Heltc83c41e2007-07-19 08:00:17 +0200486 data->alarms = i2c_smbus_read_byte_data(client,
487 ADM1021_REG_STATUS) & 0x7c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 if (data->type == adm1023) {
Guenter Roeck21d2a8f2012-01-14 12:45:47 -0800489 /*
490 * The ADM1023 provides 3 extra bits of precision for
491 * the remote sensor in extra registers.
492 */
Michael Abbottf2668892009-09-21 17:04:46 -0700493 data->temp[1] += 125 * (i2c_smbus_read_byte_data(
494 client, ADM1023_REG_REM_TEMP_PREC) >> 5);
495 data->temp_max[1] += 125 * (i2c_smbus_read_byte_data(
496 client, ADM1023_REG_REM_TOS_PREC) >> 5);
497 data->temp_min[1] += 125 * (i2c_smbus_read_byte_data(
498 client, ADM1023_REG_REM_THYST_PREC) >> 5);
Krzysztof Heltc83c41e2007-07-19 08:00:17 +0200499 data->remote_temp_offset =
500 i2c_smbus_read_byte_data(client,
501 ADM1023_REG_REM_OFFSET);
502 data->remote_temp_offset_prec =
503 i2c_smbus_read_byte_data(client,
504 ADM1023_REG_REM_OFFSET_PREC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 }
506 data->last_updated = jiffies;
507 data->valid = 1;
508 }
509
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100510 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511
512 return data;
513}
514
Axel Linf0967ee2012-01-20 15:38:18 +0800515module_i2c_driver(adm1021_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516
Guenter Roeck21d2a8f2012-01-14 12:45:47 -0800517MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl> and "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 "Philip Edelbrock <phil@netroedge.com>");
519MODULE_DESCRIPTION("adm1021 driver");
520MODULE_LICENSE("GPL");
521
522module_param(read_only, bool, 0);
523MODULE_PARM_DESC(read_only, "Don't set any values, read only mode");