blob: 1fdcc3e703b9c364a39008e6c5c3a711d2483686 [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 {
Guenter Roeckc669ec82014-04-04 18:01:34 +020082 struct i2c_client *client;
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 enum chips type;
84
Guenter Roeckc669ec82014-04-04 18:01:34 +020085 const struct attribute_group *groups[3];
86
Ingo Molnar9a61bf62006-01-18 23:19:26 +010087 struct mutex update_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 char valid; /* !=0 if following fields are valid */
Michael Abbott905ffdc2009-09-21 17:04:47 -070089 char low_power; /* !=0 if device in low power mode */
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 unsigned long last_updated; /* In jiffies */
91
Michael Abbottf2668892009-09-21 17:04:46 -070092 int temp_max[2]; /* Register values */
93 int temp_min[2];
94 int temp[2];
Krzysztof Helta8d6646e2007-09-09 19:51:14 +020095 u8 alarms;
96 /* Special values for ADM1023 only */
Krzysztof Helta8d6646e2007-09-09 19:51:14 +020097 u8 remote_temp_offset;
98 u8 remote_temp_offset_prec;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099};
100
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101/* (amalysh) read only mode, otherwise any limit's writing confuse BIOS */
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030102static bool read_only;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
Axel Lin48995bc2014-07-03 21:45:33 +0800104static struct adm1021_data *adm1021_update_device(struct device *dev)
105{
106 struct adm1021_data *data = dev_get_drvdata(dev);
107 struct i2c_client *client = data->client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
Axel Lin48995bc2014-07-03 21:45:33 +0800109 mutex_lock(&data->update_lock);
Jean Delvare65817ed2008-07-16 19:30:08 +0200110
Axel Lin48995bc2014-07-03 21:45:33 +0800111 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
112 || !data->valid) {
113 int i;
114
115 dev_dbg(dev, "Starting adm1021 update\n");
116
117 for (i = 0; i < 2; i++) {
118 data->temp[i] = 1000 *
119 (s8) i2c_smbus_read_byte_data(
120 client, ADM1021_REG_TEMP(i));
121 data->temp_max[i] = 1000 *
122 (s8) i2c_smbus_read_byte_data(
123 client, ADM1021_REG_TOS_R(i));
124 if (data->type != lm84) {
125 data->temp_min[i] = 1000 *
126 (s8) i2c_smbus_read_byte_data(client,
127 ADM1021_REG_THYST_R(i));
128 }
129 }
130 data->alarms = i2c_smbus_read_byte_data(client,
131 ADM1021_REG_STATUS) & 0x7c;
132 if (data->type == adm1023) {
133 /*
134 * The ADM1023 provides 3 extra bits of precision for
135 * the remote sensor in extra registers.
136 */
137 data->temp[1] += 125 * (i2c_smbus_read_byte_data(
138 client, ADM1023_REG_REM_TEMP_PREC) >> 5);
139 data->temp_max[1] += 125 * (i2c_smbus_read_byte_data(
140 client, ADM1023_REG_REM_TOS_PREC) >> 5);
141 data->temp_min[1] += 125 * (i2c_smbus_read_byte_data(
142 client, ADM1023_REG_REM_THYST_PREC) >> 5);
143 data->remote_temp_offset =
144 i2c_smbus_read_byte_data(client,
145 ADM1023_REG_REM_OFFSET);
146 data->remote_temp_offset_prec =
147 i2c_smbus_read_byte_data(client,
148 ADM1023_REG_REM_OFFSET_PREC);
149 }
150 data->last_updated = jiffies;
151 data->valid = 1;
152 }
153
154 mutex_unlock(&data->update_lock);
155
156 return data;
157}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
Krzysztof Helta8d6646e2007-09-09 19:51:14 +0200159static ssize_t show_temp(struct device *dev,
160 struct device_attribute *devattr, char *buf)
161{
162 int index = to_sensor_dev_attr(devattr)->index;
163 struct adm1021_data *data = adm1021_update_device(dev);
164
Michael Abbottf2668892009-09-21 17:04:46 -0700165 return sprintf(buf, "%d\n", data->temp[index]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166}
Krzysztof Helta8d6646e2007-09-09 19:51:14 +0200167
168static ssize_t show_temp_max(struct device *dev,
169 struct device_attribute *devattr, char *buf)
170{
171 int index = to_sensor_dev_attr(devattr)->index;
172 struct adm1021_data *data = adm1021_update_device(dev);
173
Michael Abbottf2668892009-09-21 17:04:46 -0700174 return sprintf(buf, "%d\n", data->temp_max[index]);
Krzysztof Helta8d6646e2007-09-09 19:51:14 +0200175}
176
177static ssize_t show_temp_min(struct device *dev,
178 struct device_attribute *devattr, char *buf)
179{
180 int index = to_sensor_dev_attr(devattr)->index;
181 struct adm1021_data *data = adm1021_update_device(dev);
182
Michael Abbottf2668892009-09-21 17:04:46 -0700183 return sprintf(buf, "%d\n", data->temp_min[index]);
Krzysztof Helta8d6646e2007-09-09 19:51:14 +0200184}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185
Krzysztof Helt739ba242007-09-09 19:16:03 +0200186static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
187 char *buf)
188{
189 int index = to_sensor_dev_attr(attr)->index;
190 struct adm1021_data *data = adm1021_update_device(dev);
191 return sprintf(buf, "%u\n", (data->alarms >> index) & 1);
192}
193
Krzysztof Heltc83c41e2007-07-19 08:00:17 +0200194static ssize_t show_alarms(struct device *dev,
195 struct device_attribute *attr,
196 char *buf)
197{
198 struct adm1021_data *data = adm1021_update_device(dev);
199 return sprintf(buf, "%u\n", data->alarms);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
Krzysztof Helta8d6646e2007-09-09 19:51:14 +0200202static ssize_t set_temp_max(struct device *dev,
203 struct device_attribute *devattr,
204 const char *buf, size_t count)
205{
206 int index = to_sensor_dev_attr(devattr)->index;
Guenter Roeckc669ec82014-04-04 18:01:34 +0200207 struct adm1021_data *data = dev_get_drvdata(dev);
208 struct i2c_client *client = data->client;
Guenter Roeck21d2a8f2012-01-14 12:45:47 -0800209 long temp;
Axel Linc0240442014-07-03 22:45:45 +0800210 int reg_val, err;
Guenter Roeck21d2a8f2012-01-14 12:45:47 -0800211
212 err = kstrtol(buf, 10, &temp);
213 if (err)
214 return err;
215 temp /= 1000;
Krzysztof Helta8d6646e2007-09-09 19:51:14 +0200216
217 mutex_lock(&data->update_lock);
Axel Linc0240442014-07-03 22:45:45 +0800218 reg_val = clamp_val(temp, -128, 127);
219 data->temp_max[index] = reg_val * 1000;
Krzysztof Helta8d6646e2007-09-09 19:51:14 +0200220 if (!read_only)
221 i2c_smbus_write_byte_data(client, ADM1021_REG_TOS_W(index),
Axel Linc0240442014-07-03 22:45:45 +0800222 reg_val);
Krzysztof Helta8d6646e2007-09-09 19:51:14 +0200223 mutex_unlock(&data->update_lock);
224
225 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227
Krzysztof Helta8d6646e2007-09-09 19:51:14 +0200228static ssize_t set_temp_min(struct device *dev,
229 struct device_attribute *devattr,
230 const char *buf, size_t count)
231{
232 int index = to_sensor_dev_attr(devattr)->index;
Guenter Roeckc669ec82014-04-04 18:01:34 +0200233 struct adm1021_data *data = dev_get_drvdata(dev);
234 struct i2c_client *client = data->client;
Guenter Roeck21d2a8f2012-01-14 12:45:47 -0800235 long temp;
Axel Linc0240442014-07-03 22:45:45 +0800236 int reg_val, err;
Guenter Roeck21d2a8f2012-01-14 12:45:47 -0800237
238 err = kstrtol(buf, 10, &temp);
239 if (err)
240 return err;
241 temp /= 1000;
Krzysztof Helta8d6646e2007-09-09 19:51:14 +0200242
243 mutex_lock(&data->update_lock);
Axel Linc0240442014-07-03 22:45:45 +0800244 reg_val = clamp_val(temp, -128, 127);
245 data->temp_min[index] = reg_val * 1000;
Krzysztof Helta8d6646e2007-09-09 19:51:14 +0200246 if (!read_only)
247 i2c_smbus_write_byte_data(client, ADM1021_REG_THYST_W(index),
Axel Linc0240442014-07-03 22:45:45 +0800248 reg_val);
Krzysztof Helta8d6646e2007-09-09 19:51:14 +0200249 mutex_unlock(&data->update_lock);
250
251 return count;
252}
253
Michael Abbott905ffdc2009-09-21 17:04:47 -0700254static ssize_t show_low_power(struct device *dev,
255 struct device_attribute *devattr, char *buf)
256{
257 struct adm1021_data *data = adm1021_update_device(dev);
258 return sprintf(buf, "%d\n", data->low_power);
259}
260
261static ssize_t set_low_power(struct device *dev,
262 struct device_attribute *devattr,
263 const char *buf, size_t count)
264{
Guenter Roeckc669ec82014-04-04 18:01:34 +0200265 struct adm1021_data *data = dev_get_drvdata(dev);
266 struct i2c_client *client = data->client;
Guenter Roeck21d2a8f2012-01-14 12:45:47 -0800267 char low_power;
268 unsigned long val;
269 int err;
270
271 err = kstrtoul(buf, 10, &val);
272 if (err)
273 return err;
274 low_power = val != 0;
Michael Abbott905ffdc2009-09-21 17:04:47 -0700275
276 mutex_lock(&data->update_lock);
277 if (low_power != data->low_power) {
278 int config = i2c_smbus_read_byte_data(
279 client, ADM1021_REG_CONFIG_R);
280 data->low_power = low_power;
281 i2c_smbus_write_byte_data(client, ADM1021_REG_CONFIG_W,
282 (config & 0xBF) | (low_power << 6));
283 }
284 mutex_unlock(&data->update_lock);
285
286 return count;
287}
288
289
Krzysztof Helta8d6646e2007-09-09 19:51:14 +0200290static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0);
291static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp_max,
292 set_temp_max, 0);
293static SENSOR_DEVICE_ATTR(temp1_min, S_IWUSR | S_IRUGO, show_temp_min,
294 set_temp_min, 0);
295static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp, NULL, 1);
296static SENSOR_DEVICE_ATTR(temp2_max, S_IWUSR | S_IRUGO, show_temp_max,
297 set_temp_max, 1);
298static SENSOR_DEVICE_ATTR(temp2_min, S_IWUSR | S_IRUGO, show_temp_min,
299 set_temp_min, 1);
Krzysztof Helt739ba242007-09-09 19:16:03 +0200300static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, show_alarm, NULL, 6);
301static SENSOR_DEVICE_ATTR(temp1_min_alarm, S_IRUGO, show_alarm, NULL, 5);
302static SENSOR_DEVICE_ATTR(temp2_max_alarm, S_IRUGO, show_alarm, NULL, 4);
303static SENSOR_DEVICE_ATTR(temp2_min_alarm, S_IRUGO, show_alarm, NULL, 3);
304static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO, show_alarm, NULL, 2);
305
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
Michael Abbott905ffdc2009-09-21 17:04:47 -0700307static DEVICE_ATTR(low_power, S_IWUSR | S_IRUGO, show_low_power, set_low_power);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
Mark M. Hoffman681c6f72006-09-24 21:15:35 +0200309static struct attribute *adm1021_attributes[] = {
Krzysztof Helta8d6646e2007-09-09 19:51:14 +0200310 &sensor_dev_attr_temp1_max.dev_attr.attr,
Krzysztof Helta8d6646e2007-09-09 19:51:14 +0200311 &sensor_dev_attr_temp1_input.dev_attr.attr,
312 &sensor_dev_attr_temp2_max.dev_attr.attr,
Krzysztof Helta8d6646e2007-09-09 19:51:14 +0200313 &sensor_dev_attr_temp2_input.dev_attr.attr,
Krzysztof Helt739ba242007-09-09 19:16:03 +0200314 &sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
Krzysztof Helt739ba242007-09-09 19:16:03 +0200315 &sensor_dev_attr_temp2_max_alarm.dev_attr.attr,
Krzysztof Helt739ba242007-09-09 19:16:03 +0200316 &sensor_dev_attr_temp2_fault.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +0200317 &dev_attr_alarms.attr,
Michael Abbott905ffdc2009-09-21 17:04:47 -0700318 &dev_attr_low_power.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +0200319 NULL
320};
321
322static const struct attribute_group adm1021_group = {
323 .attrs = adm1021_attributes,
324};
325
Guenter Roeck68146a52013-06-06 10:30:10 -0700326static struct attribute *adm1021_min_attributes[] = {
327 &sensor_dev_attr_temp1_min.dev_attr.attr,
328 &sensor_dev_attr_temp2_min.dev_attr.attr,
329 &sensor_dev_attr_temp1_min_alarm.dev_attr.attr,
330 &sensor_dev_attr_temp2_min_alarm.dev_attr.attr,
331 NULL
332};
333
334static const struct attribute_group adm1021_min_group = {
335 .attrs = adm1021_min_attributes,
336};
337
Jean Delvare65817ed2008-07-16 19:30:08 +0200338/* Return 0 if detection is successful, -ENODEV otherwise */
Jean Delvare310ec792009-12-14 21:17:23 +0100339static int adm1021_detect(struct i2c_client *client,
Jean Delvare65817ed2008-07-16 19:30:08 +0200340 struct i2c_board_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341{
Jean Delvare65817ed2008-07-16 19:30:08 +0200342 struct i2c_adapter *adapter = client->adapter;
Jean Delvare8007ea32009-12-09 20:35:51 +0100343 const char *type_name;
344 int conv_rate, status, config, man_id, dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345
Krzysztof Heltc83c41e2007-07-19 08:00:17 +0200346 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
Guenter Roeckb55f3752013-01-10 10:01:24 -0800347 pr_debug("detect failed, smbus byte data not supported!\n");
Jean Delvare65817ed2008-07-16 19:30:08 +0200348 return -ENODEV;
Krzysztof Heltc83c41e2007-07-19 08:00:17 +0200349 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350
Krzysztof Heltc83c41e2007-07-19 08:00:17 +0200351 status = i2c_smbus_read_byte_data(client, ADM1021_REG_STATUS);
352 conv_rate = i2c_smbus_read_byte_data(client,
353 ADM1021_REG_CONV_RATE_R);
354 config = i2c_smbus_read_byte_data(client, ADM1021_REG_CONFIG_R);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355
Jean Delvare8007ea32009-12-09 20:35:51 +0100356 /* Check unused bits */
357 if ((status & 0x03) || (config & 0x3F) || (conv_rate & 0xF8)) {
Guenter Roeckb55f3752013-01-10 10:01:24 -0800358 pr_debug("detect failed, chip not detected!\n");
Jean Delvare8007ea32009-12-09 20:35:51 +0100359 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 }
361
362 /* Determine the chip type. */
Jean Delvare8007ea32009-12-09 20:35:51 +0100363 man_id = i2c_smbus_read_byte_data(client, ADM1021_REG_MAN_ID);
364 dev_id = i2c_smbus_read_byte_data(client, ADM1021_REG_DEV_ID);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365
Guenter Roeck591bfcf2013-06-05 14:09:30 -0700366 if (man_id < 0 || dev_id < 0)
367 return -ENODEV;
368
Jean Delvare8007ea32009-12-09 20:35:51 +0100369 if (man_id == 0x4d && dev_id == 0x01)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 type_name = "max1617a";
Jean Delvare8007ea32009-12-09 20:35:51 +0100371 else if (man_id == 0x41) {
372 if ((dev_id & 0xF0) == 0x30)
373 type_name = "adm1023";
Guenter Roeck591bfcf2013-06-05 14:09:30 -0700374 else if ((dev_id & 0xF0) == 0x00)
Jean Delvare8007ea32009-12-09 20:35:51 +0100375 type_name = "adm1021";
Guenter Roeck591bfcf2013-06-05 14:09:30 -0700376 else
377 return -ENODEV;
Jean Delvare8007ea32009-12-09 20:35:51 +0100378 } else if (man_id == 0x49)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 type_name = "thmc10";
Jean Delvare8007ea32009-12-09 20:35:51 +0100380 else if (man_id == 0x23)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 type_name = "gl523sm";
Jean Delvare8007ea32009-12-09 20:35:51 +0100382 else if (man_id == 0x54)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 type_name = "mc1066";
Guenter Roeck591bfcf2013-06-05 14:09:30 -0700384 else {
385 int lte, rte, lhi, rhi, llo, rlo;
386
387 /* extra checks for LM84 and MAX1617 to avoid misdetections */
388
389 llo = i2c_smbus_read_byte_data(client, ADM1021_REG_THYST_R(0));
390 rlo = i2c_smbus_read_byte_data(client, ADM1021_REG_THYST_R(1));
391
392 /* fail if any of the additional register reads failed */
393 if (llo < 0 || rlo < 0)
394 return -ENODEV;
395
396 lte = i2c_smbus_read_byte_data(client, ADM1021_REG_TEMP(0));
397 rte = i2c_smbus_read_byte_data(client, ADM1021_REG_TEMP(1));
398 lhi = i2c_smbus_read_byte_data(client, ADM1021_REG_TOS_R(0));
399 rhi = i2c_smbus_read_byte_data(client, ADM1021_REG_TOS_R(1));
400
401 /*
402 * Fail for negative temperatures and negative high limits.
403 * This check also catches read errors on the tested registers.
404 */
405 if ((s8)lte < 0 || (s8)rte < 0 || (s8)lhi < 0 || (s8)rhi < 0)
406 return -ENODEV;
407
408 /* fail if all registers hold the same value */
409 if (lte == rte && lte == lhi && lte == rhi && lte == llo
410 && lte == rlo)
411 return -ENODEV;
412
413 /*
414 * LM84 Mfr ID is in a different place,
415 * and it has more unused bits.
416 */
417 if (conv_rate == 0x00
418 && (config & 0x7F) == 0x00
419 && (status & 0xAB) == 0x00) {
420 type_name = "lm84";
421 } else {
422 /* fail if low limits are larger than high limits */
423 if ((s8)llo > lhi || (s8)rlo > rhi)
424 return -ENODEV;
425 type_name = "max1617";
426 }
427 }
Jean Delvare8007ea32009-12-09 20:35:51 +0100428
Guenter Roeckb55f3752013-01-10 10:01:24 -0800429 pr_debug("Detected chip %s at adapter %d, address 0x%02x.\n",
Jean Delvare65817ed2008-07-16 19:30:08 +0200430 type_name, i2c_adapter_id(adapter), client->addr);
431 strlcpy(info->type, type_name, I2C_NAME_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432
Jean Delvare65817ed2008-07-16 19:30:08 +0200433 return 0;
434}
435
Axel Lin48995bc2014-07-03 21:45:33 +0800436static void adm1021_init_client(struct i2c_client *client)
437{
438 /* Enable ADC and disable suspend mode */
439 i2c_smbus_write_byte_data(client, ADM1021_REG_CONFIG_W,
440 i2c_smbus_read_byte_data(client, ADM1021_REG_CONFIG_R) & 0xBF);
441 /* Set Conversion rate to 1/sec (this can be tinkered with) */
442 i2c_smbus_write_byte_data(client, ADM1021_REG_CONV_RATE_W, 0x04);
443}
444
Jean Delvare65817ed2008-07-16 19:30:08 +0200445static int adm1021_probe(struct i2c_client *client,
446 const struct i2c_device_id *id)
447{
Guenter Roeckc669ec82014-04-04 18:01:34 +0200448 struct device *dev = &client->dev;
Jean Delvare65817ed2008-07-16 19:30:08 +0200449 struct adm1021_data *data;
Guenter Roeckc669ec82014-04-04 18:01:34 +0200450 struct device *hwmon_dev;
Jean Delvare65817ed2008-07-16 19:30:08 +0200451
Guenter Roeckc669ec82014-04-04 18:01:34 +0200452 data = devm_kzalloc(dev, sizeof(struct adm1021_data), GFP_KERNEL);
Guenter Roeckb55f3752013-01-10 10:01:24 -0800453 if (!data)
Guenter Roeck06ce97d2012-06-02 09:57:58 -0700454 return -ENOMEM;
Jean Delvare65817ed2008-07-16 19:30:08 +0200455
Guenter Roeckc669ec82014-04-04 18:01:34 +0200456 data->client = client;
Jean Delvare65817ed2008-07-16 19:30:08 +0200457 data->type = id->driver_data;
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100458 mutex_init(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 /* Initialize the ADM1021 chip */
Jean Delvare65817ed2008-07-16 19:30:08 +0200461 if (data->type != lm84 && !read_only)
Krzysztof Heltc83c41e2007-07-19 08:00:17 +0200462 adm1021_init_client(client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463
Guenter Roeckc669ec82014-04-04 18:01:34 +0200464 data->groups[0] = &adm1021_group;
465 if (data->type != lm84)
466 data->groups[1] = &adm1021_min_group;
Mark M. Hoffman681c6f72006-09-24 21:15:35 +0200467
Guenter Roeckc669ec82014-04-04 18:01:34 +0200468 hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
469 data, data->groups);
Guenter Roeck68146a52013-06-06 10:30:10 -0700470
Guenter Roeckc669ec82014-04-04 18:01:34 +0200471 return PTR_ERR_OR_ZERO(hwmon_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472}
473
Axel Lin48995bc2014-07-03 21:45:33 +0800474static const struct i2c_device_id adm1021_id[] = {
475 { "adm1021", adm1021 },
476 { "adm1023", adm1023 },
477 { "max1617", max1617 },
478 { "max1617a", max1617a },
479 { "thmc10", thmc10 },
480 { "lm84", lm84 },
481 { "gl523sm", gl523sm },
482 { "mc1066", mc1066 },
483 { }
484};
485MODULE_DEVICE_TABLE(i2c, adm1021_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486
Axel Lin48995bc2014-07-03 21:45:33 +0800487static struct i2c_driver adm1021_driver = {
488 .class = I2C_CLASS_HWMON,
489 .driver = {
490 .name = "adm1021",
491 },
492 .probe = adm1021_probe,
493 .id_table = adm1021_id,
494 .detect = adm1021_detect,
495 .address_list = normal_i2c,
496};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497
Axel Linf0967ee2012-01-20 15:38:18 +0800498module_i2c_driver(adm1021_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499
Guenter Roeck21d2a8f2012-01-14 12:45:47 -0800500MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl> and "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 "Philip Edelbrock <phil@netroedge.com>");
502MODULE_DESCRIPTION("adm1021 driver");
503MODULE_LICENSE("GPL");
504
505module_param(read_only, bool, 0);
506MODULE_PARM_DESC(read_only, "Don't set any values, read only mode");