aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/hwmon/adm1021.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/hwmon/adm1021.c')
-rw-r--r--drivers/hwmon/adm1021.c373
1 files changed, 214 insertions, 159 deletions
diff --git a/drivers/hwmon/adm1021.c b/drivers/hwmon/adm1021.c
index c466329b2ef4..ebdc6d7db231 100644
--- a/drivers/hwmon/adm1021.c
+++ b/drivers/hwmon/adm1021.c
@@ -1,6 +1,6 @@
1/* 1/*
2 adm1021.c - Part of lm_sensors, Linux kernel modules for hardware 2 adm1021.c - Part of lm_sensors, Linux kernel modules for hardware
3 monitoring 3 monitoring
4 Copyright (c) 1998, 1999 Frodo Looijaard <frodol@dds.nl> and 4 Copyright (c) 1998, 1999 Frodo Looijaard <frodol@dds.nl> and
5 Philip Edelbrock <phil@netroedge.com> 5 Philip Edelbrock <phil@netroedge.com>
6 6
@@ -25,6 +25,7 @@
25#include <linux/jiffies.h> 25#include <linux/jiffies.h>
26#include <linux/i2c.h> 26#include <linux/i2c.h>
27#include <linux/hwmon.h> 27#include <linux/hwmon.h>
28#include <linux/hwmon-sysfs.h>
28#include <linux/err.h> 29#include <linux/err.h>
29#include <linux/mutex.h> 30#include <linux/mutex.h>
30 31
@@ -32,93 +33,77 @@
32/* Addresses to scan */ 33/* Addresses to scan */
33static unsigned short normal_i2c[] = { 0x18, 0x19, 0x1a, 34static unsigned short normal_i2c[] = { 0x18, 0x19, 0x1a,
34 0x29, 0x2a, 0x2b, 35 0x29, 0x2a, 0x2b,
35 0x4c, 0x4d, 0x4e, 36 0x4c, 0x4d, 0x4e,
36 I2C_CLIENT_END }; 37 I2C_CLIENT_END };
37 38
38/* Insmod parameters */ 39/* Insmod parameters */
39I2C_CLIENT_INSMOD_8(adm1021, adm1023, max1617, max1617a, thmc10, lm84, gl523sm, mc1066); 40I2C_CLIENT_INSMOD_8(adm1021, adm1023, max1617, max1617a, thmc10, lm84, gl523sm,
41 mc1066);
40 42
41/* adm1021 constants specified below */ 43/* adm1021 constants specified below */
42 44
43/* The adm1021 registers */ 45/* The adm1021 registers */
44/* Read-only */ 46/* Read-only */
45#define ADM1021_REG_TEMP 0x00 47/* For nr in 0-1 */
46#define ADM1021_REG_REMOTE_TEMP 0x01 48#define ADM1021_REG_TEMP(nr) (nr)
47#define ADM1021_REG_STATUS 0x02 49#define ADM1021_REG_STATUS 0x02
48#define ADM1021_REG_MAN_ID 0x0FE /* 0x41 = AMD, 0x49 = TI, 0x4D = Maxim, 0x23 = Genesys , 0x54 = Onsemi*/ 50/* 0x41 = AD, 0x49 = TI, 0x4D = Maxim, 0x23 = Genesys , 0x54 = Onsemi */
49#define ADM1021_REG_DEV_ID 0x0FF /* ADM1021 = 0x0X, ADM1023 = 0x3X */ 51#define ADM1021_REG_MAN_ID 0xFE
50#define ADM1021_REG_DIE_CODE 0x0FF /* MAX1617A */ 52/* ADM1021 = 0x0X, ADM1023 = 0x3X */
53#define ADM1021_REG_DEV_ID 0xFF
51/* These use different addresses for reading/writing */ 54/* These use different addresses for reading/writing */
52#define ADM1021_REG_CONFIG_R 0x03 55#define ADM1021_REG_CONFIG_R 0x03
53#define ADM1021_REG_CONFIG_W 0x09 56#define ADM1021_REG_CONFIG_W 0x09
54#define ADM1021_REG_CONV_RATE_R 0x04 57#define ADM1021_REG_CONV_RATE_R 0x04
55#define ADM1021_REG_CONV_RATE_W 0x0A 58#define ADM1021_REG_CONV_RATE_W 0x0A
56/* These are for the ADM1023's additional precision on the remote temp sensor */ 59/* These are for the ADM1023's additional precision on the remote temp sensor */
57#define ADM1021_REG_REM_TEMP_PREC 0x010 60#define ADM1023_REG_REM_TEMP_PREC 0x10
58#define ADM1021_REG_REM_OFFSET 0x011 61#define ADM1023_REG_REM_OFFSET 0x11
59#define ADM1021_REG_REM_OFFSET_PREC 0x012 62#define ADM1023_REG_REM_OFFSET_PREC 0x12
60#define ADM1021_REG_REM_TOS_PREC 0x013 63#define ADM1023_REG_REM_TOS_PREC 0x13
61#define ADM1021_REG_REM_THYST_PREC 0x014 64#define ADM1023_REG_REM_THYST_PREC 0x14
62/* limits */ 65/* limits */
63#define ADM1021_REG_TOS_R 0x05 66/* For nr in 0-1 */
64#define ADM1021_REG_TOS_W 0x0B 67#define ADM1021_REG_TOS_R(nr) (0x05 + 2 * (nr))
65#define ADM1021_REG_REMOTE_TOS_R 0x07 68#define ADM1021_REG_TOS_W(nr) (0x0B + 2 * (nr))
66#define ADM1021_REG_REMOTE_TOS_W 0x0D 69#define ADM1021_REG_THYST_R(nr) (0x06 + 2 * (nr))
67#define ADM1021_REG_THYST_R 0x06 70#define ADM1021_REG_THYST_W(nr) (0x0C + 2 * (nr))
68#define ADM1021_REG_THYST_W 0x0C
69#define ADM1021_REG_REMOTE_THYST_R 0x08
70#define ADM1021_REG_REMOTE_THYST_W 0x0E
71/* write-only */ 71/* write-only */
72#define ADM1021_REG_ONESHOT 0x0F 72#define ADM1021_REG_ONESHOT 0x0F
73 73
74
75/* Conversions. Rounding and limit checking is only done on the TO_REG
76 variants. Note that you should be a bit careful with which arguments
77 these macros are called: arguments may be evaluated more than once.
78 Fixing this is just not worth it. */
79/* Conversions note: 1021 uses normal integer signed-byte format*/
80#define TEMP_FROM_REG(val) (val > 127 ? (val-256)*1000 : val*1000)
81#define TEMP_TO_REG(val) (SENSORS_LIMIT((val < 0 ? (val/1000)+256 : val/1000),0,255))
82
83/* Initial values */ 74/* Initial values */
84 75
85/* Note: Even though I left the low and high limits named os and hyst, 76/* Note: Even though I left the low and high limits named os and hyst,
86they don't quite work like a thermostat the way the LM75 does. I.e., 77they don't quite work like a thermostat the way the LM75 does. I.e.,
87a lower temp than THYST actually triggers an alarm instead of 78a lower temp than THYST actually triggers an alarm instead of
88clearing it. Weird, ey? --Phil */ 79clearing it. Weird, ey? --Phil */
89 80
90/* Each client has this additional data */ 81/* Each client has this additional data */
91struct adm1021_data { 82struct adm1021_data {
92 struct i2c_client client; 83 struct i2c_client client;
93 struct class_device *class_dev; 84 struct device *hwmon_dev;
94 enum chips type; 85 enum chips type;
95 86
96 struct mutex update_lock; 87 struct mutex update_lock;
97 char valid; /* !=0 if following fields are valid */ 88 char valid; /* !=0 if following fields are valid */
98 unsigned long last_updated; /* In jiffies */ 89 unsigned long last_updated; /* In jiffies */
99 90
100 u8 temp_max; /* Register values */ 91 s8 temp_max[2]; /* Register values */
101 u8 temp_hyst; 92 s8 temp_min[2];
102 u8 temp_input; 93 s8 temp[2];
103 u8 remote_temp_max; 94 u8 alarms;
104 u8 remote_temp_hyst; 95 /* Special values for ADM1023 only */
105 u8 remote_temp_input; 96 u8 remote_temp_prec;
106 u8 alarms; 97 u8 remote_temp_os_prec;
107 /* Special values for ADM1023 only */ 98 u8 remote_temp_hyst_prec;
108 u8 remote_temp_prec; 99 u8 remote_temp_offset;
109 u8 remote_temp_os_prec; 100 u8 remote_temp_offset_prec;
110 u8 remote_temp_hyst_prec;
111 u8 remote_temp_offset;
112 u8 remote_temp_offset_prec;
113}; 101};
114 102
115static int adm1021_attach_adapter(struct i2c_adapter *adapter); 103static int adm1021_attach_adapter(struct i2c_adapter *adapter);
116static int adm1021_detect(struct i2c_adapter *adapter, int address, int kind); 104static int adm1021_detect(struct i2c_adapter *adapter, int address, int kind);
117static void adm1021_init_client(struct i2c_client *client); 105static void adm1021_init_client(struct i2c_client *client);
118static int adm1021_detach_client(struct i2c_client *client); 106static int adm1021_detach_client(struct i2c_client *client);
119static int adm1021_read_value(struct i2c_client *client, u8 reg);
120static int adm1021_write_value(struct i2c_client *client, u8 reg,
121 u16 value);
122static struct adm1021_data *adm1021_update_device(struct device *dev); 107static struct adm1021_data *adm1021_update_device(struct device *dev);
123 108
124/* (amalysh) read only mode, otherwise any limit's writing confuse BIOS */ 109/* (amalysh) read only mode, otherwise any limit's writing confuse BIOS */
@@ -135,53 +120,104 @@ static struct i2c_driver adm1021_driver = {
135 .detach_client = adm1021_detach_client, 120 .detach_client = adm1021_detach_client,
136}; 121};
137 122
138#define show(value) \ 123static ssize_t show_temp(struct device *dev,
139static ssize_t show_##value(struct device *dev, struct device_attribute *attr, char *buf) \ 124 struct device_attribute *devattr, char *buf)
140{ \ 125{
141 struct adm1021_data *data = adm1021_update_device(dev); \ 126 int index = to_sensor_dev_attr(devattr)->index;
142 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->value)); \ 127 struct adm1021_data *data = adm1021_update_device(dev);
128
129 return sprintf(buf, "%d\n", 1000 * data->temp[index]);
143} 130}
144show(temp_max); 131
145show(temp_hyst); 132static ssize_t show_temp_max(struct device *dev,
146show(temp_input); 133 struct device_attribute *devattr, char *buf)
147show(remote_temp_max); 134{
148show(remote_temp_hyst); 135 int index = to_sensor_dev_attr(devattr)->index;
149show(remote_temp_input); 136 struct adm1021_data *data = adm1021_update_device(dev);
150 137
151#define show2(value) \ 138 return sprintf(buf, "%d\n", 1000 * data->temp_max[index]);
152static ssize_t show_##value(struct device *dev, struct device_attribute *attr, char *buf) \
153{ \
154 struct adm1021_data *data = adm1021_update_device(dev); \
155 return sprintf(buf, "%d\n", data->value); \
156} 139}
157show2(alarms); 140
158 141static ssize_t show_temp_min(struct device *dev,
159#define set(value, reg) \ 142 struct device_attribute *devattr, char *buf)
160static ssize_t set_##value(struct device *dev, struct device_attribute *attr, const char *buf, size_t count) \ 143{
161{ \ 144 int index = to_sensor_dev_attr(devattr)->index;
162 struct i2c_client *client = to_i2c_client(dev); \ 145 struct adm1021_data *data = adm1021_update_device(dev);
163 struct adm1021_data *data = i2c_get_clientdata(client); \ 146
164 int temp = simple_strtoul(buf, NULL, 10); \ 147 return sprintf(buf, "%d\n", 1000 * data->temp_min[index]);
165 \ 148}
166 mutex_lock(&data->update_lock); \ 149
167 data->value = TEMP_TO_REG(temp); \ 150static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
168 adm1021_write_value(client, reg, data->value); \ 151 char *buf)
169 mutex_unlock(&data->update_lock); \ 152{
170 return count; \ 153 int index = to_sensor_dev_attr(attr)->index;
154 struct adm1021_data *data = adm1021_update_device(dev);
155 return sprintf(buf, "%u\n", (data->alarms >> index) & 1);
156}
157
158static ssize_t show_alarms(struct device *dev,
159 struct device_attribute *attr,
160 char *buf)
161{
162 struct adm1021_data *data = adm1021_update_device(dev);
163 return sprintf(buf, "%u\n", data->alarms);
171} 164}
172set(temp_max, ADM1021_REG_TOS_W);
173set(temp_hyst, ADM1021_REG_THYST_W);
174set(remote_temp_max, ADM1021_REG_REMOTE_TOS_W);
175set(remote_temp_hyst, ADM1021_REG_REMOTE_THYST_W);
176
177static DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp_max, set_temp_max);
178static DEVICE_ATTR(temp1_min, S_IWUSR | S_IRUGO, show_temp_hyst, set_temp_hyst);
179static DEVICE_ATTR(temp1_input, S_IRUGO, show_temp_input, NULL);
180static DEVICE_ATTR(temp2_max, S_IWUSR | S_IRUGO, show_remote_temp_max, set_remote_temp_max);
181static DEVICE_ATTR(temp2_min, S_IWUSR | S_IRUGO, show_remote_temp_hyst, set_remote_temp_hyst);
182static DEVICE_ATTR(temp2_input, S_IRUGO, show_remote_temp_input, NULL);
183static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
184 165
166static ssize_t set_temp_max(struct device *dev,
167 struct device_attribute *devattr,
168 const char *buf, size_t count)
169{
170 int index = to_sensor_dev_attr(devattr)->index;
171 struct i2c_client *client = to_i2c_client(dev);
172 struct adm1021_data *data = i2c_get_clientdata(client);
173 long temp = simple_strtol(buf, NULL, 10) / 1000;
174
175 mutex_lock(&data->update_lock);
176 data->temp_max[index] = SENSORS_LIMIT(temp, -128, 127);
177 if (!read_only)
178 i2c_smbus_write_byte_data(client, ADM1021_REG_TOS_W(index),
179 data->temp_max[index]);
180 mutex_unlock(&data->update_lock);
181
182 return count;
183}
184
185static ssize_t set_temp_min(struct device *dev,
186 struct device_attribute *devattr,
187 const char *buf, size_t count)
188{
189 int index = to_sensor_dev_attr(devattr)->index;
190 struct i2c_client *client = to_i2c_client(dev);
191 struct adm1021_data *data = i2c_get_clientdata(client);
192 long temp = simple_strtol(buf, NULL, 10) / 1000;
193
194 mutex_lock(&data->update_lock);
195 data->temp_min[index] = SENSORS_LIMIT(temp, -128, 127);
196 if (!read_only)
197 i2c_smbus_write_byte_data(client, ADM1021_REG_THYST_W(index),
198 data->temp_min[index]);
199 mutex_unlock(&data->update_lock);
200
201 return count;
202}
203
204static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0);
205static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp_max,
206 set_temp_max, 0);
207static SENSOR_DEVICE_ATTR(temp1_min, S_IWUSR | S_IRUGO, show_temp_min,
208 set_temp_min, 0);
209static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp, NULL, 1);
210static SENSOR_DEVICE_ATTR(temp2_max, S_IWUSR | S_IRUGO, show_temp_max,
211 set_temp_max, 1);
212static SENSOR_DEVICE_ATTR(temp2_min, S_IWUSR | S_IRUGO, show_temp_min,
213 set_temp_min, 1);
214static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, show_alarm, NULL, 6);
215static SENSOR_DEVICE_ATTR(temp1_min_alarm, S_IRUGO, show_alarm, NULL, 5);
216static SENSOR_DEVICE_ATTR(temp2_max_alarm, S_IRUGO, show_alarm, NULL, 4);
217static SENSOR_DEVICE_ATTR(temp2_min_alarm, S_IRUGO, show_alarm, NULL, 3);
218static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO, show_alarm, NULL, 2);
219
220static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
185 221
186static int adm1021_attach_adapter(struct i2c_adapter *adapter) 222static int adm1021_attach_adapter(struct i2c_adapter *adapter)
187{ 223{
@@ -191,12 +227,17 @@ static int adm1021_attach_adapter(struct i2c_adapter *adapter)
191} 227}
192 228
193static struct attribute *adm1021_attributes[] = { 229static struct attribute *adm1021_attributes[] = {
194 &dev_attr_temp1_max.attr, 230 &sensor_dev_attr_temp1_max.dev_attr.attr,
195 &dev_attr_temp1_min.attr, 231 &sensor_dev_attr_temp1_min.dev_attr.attr,
196 &dev_attr_temp1_input.attr, 232 &sensor_dev_attr_temp1_input.dev_attr.attr,
197 &dev_attr_temp2_max.attr, 233 &sensor_dev_attr_temp2_max.dev_attr.attr,
198 &dev_attr_temp2_min.attr, 234 &sensor_dev_attr_temp2_min.dev_attr.attr,
199 &dev_attr_temp2_input.attr, 235 &sensor_dev_attr_temp2_input.dev_attr.attr,
236 &sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
237 &sensor_dev_attr_temp1_min_alarm.dev_attr.attr,
238 &sensor_dev_attr_temp2_max_alarm.dev_attr.attr,
239 &sensor_dev_attr_temp2_min_alarm.dev_attr.attr,
240 &sensor_dev_attr_temp2_fault.dev_attr.attr,
200 &dev_attr_alarms.attr, 241 &dev_attr_alarms.attr,
201 NULL 242 NULL
202}; 243};
@@ -208,35 +249,44 @@ static const struct attribute_group adm1021_group = {
208static int adm1021_detect(struct i2c_adapter *adapter, int address, int kind) 249static int adm1021_detect(struct i2c_adapter *adapter, int address, int kind)
209{ 250{
210 int i; 251 int i;
211 struct i2c_client *new_client; 252 struct i2c_client *client;
212 struct adm1021_data *data; 253 struct adm1021_data *data;
213 int err = 0; 254 int err = 0;
214 const char *type_name = ""; 255 const char *type_name = "";
256 int conv_rate, status, config;
215 257
216 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) 258 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
259 pr_debug("adm1021: detect failed, "
260 "smbus byte data not supported!\n");
217 goto error0; 261 goto error0;
262 }
218 263
219 /* OK. For now, we presume we have a valid client. We now create the 264 /* OK. For now, we presume we have a valid client. We now create the
220 client structure, even though we cannot fill it completely yet. 265 client structure, even though we cannot fill it completely yet.
221 But it allows us to access adm1021_{read,write}_value. */ 266 But it allows us to access adm1021 register values. */
222 267
223 if (!(data = kzalloc(sizeof(struct adm1021_data), GFP_KERNEL))) { 268 if (!(data = kzalloc(sizeof(struct adm1021_data), GFP_KERNEL))) {
269 pr_debug("adm1021: detect failed, kzalloc failed!\n");
224 err = -ENOMEM; 270 err = -ENOMEM;
225 goto error0; 271 goto error0;
226 } 272 }
227 273
228 new_client = &data->client; 274 client = &data->client;
229 i2c_set_clientdata(new_client, data); 275 i2c_set_clientdata(client, data);
230 new_client->addr = address; 276 client->addr = address;
231 new_client->adapter = adapter; 277 client->adapter = adapter;
232 new_client->driver = &adm1021_driver; 278 client->driver = &adm1021_driver;
233 new_client->flags = 0; 279 status = i2c_smbus_read_byte_data(client, ADM1021_REG_STATUS);
280 conv_rate = i2c_smbus_read_byte_data(client,
281 ADM1021_REG_CONV_RATE_R);
282 config = i2c_smbus_read_byte_data(client, ADM1021_REG_CONFIG_R);
234 283
235 /* Now, we do the remaining detection. */ 284 /* Now, we do the remaining detection. */
236 if (kind < 0) { 285 if (kind < 0) {
237 if ((adm1021_read_value(new_client, ADM1021_REG_STATUS) & 0x03) != 0x00 286 if ((status & 0x03) != 0x00 || (config & 0x3F) != 0x00
238 || (adm1021_read_value(new_client, ADM1021_REG_CONFIG_R) & 0x3F) != 0x00 287 || (conv_rate & 0xF8) != 0x00) {
239 || (adm1021_read_value(new_client, ADM1021_REG_CONV_RATE_R) & 0xF8) != 0x00) { 288 pr_debug("adm1021: detect failed, "
289 "chip not detected!\n");
240 err = -ENODEV; 290 err = -ENODEV;
241 goto error1; 291 goto error1;
242 } 292 }
@@ -244,9 +294,10 @@ static int adm1021_detect(struct i2c_adapter *adapter, int address, int kind)
244 294
245 /* Determine the chip type. */ 295 /* Determine the chip type. */
246 if (kind <= 0) { 296 if (kind <= 0) {
247 i = adm1021_read_value(new_client, ADM1021_REG_MAN_ID); 297 i = i2c_smbus_read_byte_data(client, ADM1021_REG_MAN_ID);
248 if (i == 0x41) 298 if (i == 0x41)
249 if ((adm1021_read_value(new_client, ADM1021_REG_DEV_ID) & 0x0F0) == 0x030) 299 if ((i2c_smbus_read_byte_data(client,
300 ADM1021_REG_DEV_ID) & 0xF0) == 0x30)
250 kind = adm1023; 301 kind = adm1023;
251 else 302 else
252 kind = adm1021; 303 kind = adm1021;
@@ -255,15 +306,16 @@ static int adm1021_detect(struct i2c_adapter *adapter, int address, int kind)
255 else if (i == 0x23) 306 else if (i == 0x23)
256 kind = gl523sm; 307 kind = gl523sm;
257 else if ((i == 0x4d) && 308 else if ((i == 0x4d) &&
258 (adm1021_read_value(new_client, ADM1021_REG_DEV_ID) == 0x01)) 309 (i2c_smbus_read_byte_data(client,
310 ADM1021_REG_DEV_ID) == 0x01))
259 kind = max1617a; 311 kind = max1617a;
260 else if (i == 0x54) 312 else if (i == 0x54)
261 kind = mc1066; 313 kind = mc1066;
262 /* LM84 Mfr ID in a different place, and it has more unused bits */ 314 /* LM84 Mfr ID in a different place, and it has more unused bits */
263 else if (adm1021_read_value(new_client, ADM1021_REG_CONV_RATE_R) == 0x00 315 else if (conv_rate == 0x00
264 && (kind == 0 /* skip extra detection */ 316 && (kind == 0 /* skip extra detection */
265 || ((adm1021_read_value(new_client, ADM1021_REG_CONFIG_R) & 0x7F) == 0x00 317 || ((config & 0x7F) == 0x00
266 && (adm1021_read_value(new_client, ADM1021_REG_STATUS) & 0xAB) == 0x00))) 318 && (status & 0xAB) == 0x00)))
267 kind = lm84; 319 kind = lm84;
268 else 320 else
269 kind = max1617; 321 kind = max1617;
@@ -286,37 +338,38 @@ static int adm1021_detect(struct i2c_adapter *adapter, int address, int kind)
286 } else if (kind == mc1066) { 338 } else if (kind == mc1066) {
287 type_name = "mc1066"; 339 type_name = "mc1066";
288 } 340 }
341 pr_debug("adm1021: Detected chip %s at adapter %d, address 0x%02x.\n",
342 type_name, i2c_adapter_id(adapter), address);
289 343
290 /* Fill in the remaining client fields and put it into the global list */ 344 /* Fill in the remaining client fields */
291 strlcpy(new_client->name, type_name, I2C_NAME_SIZE); 345 strlcpy(client->name, type_name, I2C_NAME_SIZE);
292 data->type = kind; 346 data->type = kind;
293 data->valid = 0;
294 mutex_init(&data->update_lock); 347 mutex_init(&data->update_lock);
295 348
296 /* Tell the I2C layer a new client has arrived */ 349 /* Tell the I2C layer a new client has arrived */
297 if ((err = i2c_attach_client(new_client))) 350 if ((err = i2c_attach_client(client)))
298 goto error1; 351 goto error1;
299 352
300 /* Initialize the ADM1021 chip */ 353 /* Initialize the ADM1021 chip */
301 if (kind != lm84) 354 if (kind != lm84 && !read_only)
302 adm1021_init_client(new_client); 355 adm1021_init_client(client);
303 356
304 /* Register sysfs hooks */ 357 /* Register sysfs hooks */
305 if ((err = sysfs_create_group(&new_client->dev.kobj, &adm1021_group))) 358 if ((err = sysfs_create_group(&client->dev.kobj, &adm1021_group)))
306 goto error2; 359 goto error2;
307 360
308 data->class_dev = hwmon_device_register(&new_client->dev); 361 data->hwmon_dev = hwmon_device_register(&client->dev);
309 if (IS_ERR(data->class_dev)) { 362 if (IS_ERR(data->hwmon_dev)) {
310 err = PTR_ERR(data->class_dev); 363 err = PTR_ERR(data->hwmon_dev);
311 goto error3; 364 goto error3;
312 } 365 }
313 366
314 return 0; 367 return 0;
315 368
316error3: 369error3:
317 sysfs_remove_group(&new_client->dev.kobj, &adm1021_group); 370 sysfs_remove_group(&client->dev.kobj, &adm1021_group);
318error2: 371error2:
319 i2c_detach_client(new_client); 372 i2c_detach_client(client);
320error1: 373error1:
321 kfree(data); 374 kfree(data);
322error0: 375error0:
@@ -326,10 +379,10 @@ error0:
326static void adm1021_init_client(struct i2c_client *client) 379static void adm1021_init_client(struct i2c_client *client)
327{ 380{
328 /* Enable ADC and disable suspend mode */ 381 /* Enable ADC and disable suspend mode */
329 adm1021_write_value(client, ADM1021_REG_CONFIG_W, 382 i2c_smbus_write_byte_data(client, ADM1021_REG_CONFIG_W,
330 adm1021_read_value(client, ADM1021_REG_CONFIG_R) & 0xBF); 383 i2c_smbus_read_byte_data(client, ADM1021_REG_CONFIG_R) & 0xBF);
331 /* Set Conversion rate to 1/sec (this can be tinkered with) */ 384 /* Set Conversion rate to 1/sec (this can be tinkered with) */
332 adm1021_write_value(client, ADM1021_REG_CONV_RATE_W, 0x04); 385 i2c_smbus_write_byte_data(client, ADM1021_REG_CONV_RATE_W, 0x04);
333} 386}
334 387
335static int adm1021_detach_client(struct i2c_client *client) 388static int adm1021_detach_client(struct i2c_client *client)
@@ -337,7 +390,7 @@ static int adm1021_detach_client(struct i2c_client *client)
337 struct adm1021_data *data = i2c_get_clientdata(client); 390 struct adm1021_data *data = i2c_get_clientdata(client);
338 int err; 391 int err;
339 392
340 hwmon_device_unregister(data->class_dev); 393 hwmon_device_unregister(data->hwmon_dev);
341 sysfs_remove_group(&client->dev.kobj, &adm1021_group); 394 sysfs_remove_group(&client->dev.kobj, &adm1021_group);
342 395
343 if ((err = i2c_detach_client(client))) 396 if ((err = i2c_detach_client(client)))
@@ -347,19 +400,6 @@ static int adm1021_detach_client(struct i2c_client *client)
347 return 0; 400 return 0;
348} 401}
349 402
350/* All registers are byte-sized */
351static int adm1021_read_value(struct i2c_client *client, u8 reg)
352{
353 return i2c_smbus_read_byte_data(client, reg);
354}
355
356static int adm1021_write_value(struct i2c_client *client, u8 reg, u16 value)
357{
358 if (!read_only)
359 return i2c_smbus_write_byte_data(client, reg, value);
360 return 0;
361}
362
363static struct adm1021_data *adm1021_update_device(struct device *dev) 403static struct adm1021_data *adm1021_update_device(struct device *dev)
364{ 404{
365 struct i2c_client *client = to_i2c_client(dev); 405 struct i2c_client *client = to_i2c_client(dev);
@@ -369,21 +409,36 @@ static struct adm1021_data *adm1021_update_device(struct device *dev)
369 409
370 if (time_after(jiffies, data->last_updated + HZ + HZ / 2) 410 if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
371 || !data->valid) { 411 || !data->valid) {
412 int i;
413
372 dev_dbg(&client->dev, "Starting adm1021 update\n"); 414 dev_dbg(&client->dev, "Starting adm1021 update\n");
373 415
374 data->temp_input = adm1021_read_value(client, ADM1021_REG_TEMP); 416 for (i = 0; i < 2; i++) {
375 data->temp_max = adm1021_read_value(client, ADM1021_REG_TOS_R); 417 data->temp[i] = i2c_smbus_read_byte_data(client,
376 data->temp_hyst = adm1021_read_value(client, ADM1021_REG_THYST_R); 418 ADM1021_REG_TEMP(i));
377 data->remote_temp_input = adm1021_read_value(client, ADM1021_REG_REMOTE_TEMP); 419 data->temp_max[i] = i2c_smbus_read_byte_data(client,
378 data->remote_temp_max = adm1021_read_value(client, ADM1021_REG_REMOTE_TOS_R); 420 ADM1021_REG_TOS_R(i));
379 data->remote_temp_hyst = adm1021_read_value(client, ADM1021_REG_REMOTE_THYST_R); 421 data->temp_min[i] = i2c_smbus_read_byte_data(client,
380 data->alarms = adm1021_read_value(client, ADM1021_REG_STATUS) & 0x7c; 422 ADM1021_REG_THYST_R(i));
423 }
424 data->alarms = i2c_smbus_read_byte_data(client,
425 ADM1021_REG_STATUS) & 0x7c;
381 if (data->type == adm1023) { 426 if (data->type == adm1023) {
382 data->remote_temp_prec = adm1021_read_value(client, ADM1021_REG_REM_TEMP_PREC); 427 data->remote_temp_prec =
383 data->remote_temp_os_prec = adm1021_read_value(client, ADM1021_REG_REM_TOS_PREC); 428 i2c_smbus_read_byte_data(client,
384 data->remote_temp_hyst_prec = adm1021_read_value(client, ADM1021_REG_REM_THYST_PREC); 429 ADM1023_REG_REM_TEMP_PREC);
385 data->remote_temp_offset = adm1021_read_value(client, ADM1021_REG_REM_OFFSET); 430 data->remote_temp_os_prec =
386 data->remote_temp_offset_prec = adm1021_read_value(client, ADM1021_REG_REM_OFFSET_PREC); 431 i2c_smbus_read_byte_data(client,
432 ADM1023_REG_REM_TOS_PREC);
433 data->remote_temp_hyst_prec =
434 i2c_smbus_read_byte_data(client,
435 ADM1023_REG_REM_THYST_PREC);
436 data->remote_temp_offset =
437 i2c_smbus_read_byte_data(client,
438 ADM1023_REG_REM_OFFSET);
439 data->remote_temp_offset_prec =
440 i2c_smbus_read_byte_data(client,
441 ADM1023_REG_REM_OFFSET_PREC);
387 } 442 }
388 data->last_updated = jiffies; 443 data->last_updated = jiffies;
389 data->valid = 1; 444 data->valid = 1;