patch-2_6_7-vs1_9_1_12
[linux-2.6.git] / drivers / i2c / chips / adm1021.c
1 /*
2     adm1021.c - Part of lm_sensors, Linux kernel modules for hardware
3              monitoring
4     Copyright (c) 1998, 1999  Frodo Looijaard <frodol@dds.nl> and
5     Philip Edelbrock <phil@netroedge.com>
6
7     This program is free software; you can redistribute it and/or modify
8     it under the terms of the GNU General Public License as published by
9     the Free Software Foundation; either version 2 of the License, or
10     (at your option) any later version.
11
12     This program is distributed in the hope that it will be useful,
13     but WITHOUT ANY WARRANTY; without even the implied warranty of
14     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15     GNU General Public License for more details.
16
17     You should have received a copy of the GNU General Public License
18     along with this program; if not, write to the Free Software
19     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22 #include <linux/config.h>
23 #include <linux/module.h>
24 #include <linux/init.h>
25 #include <linux/slab.h>
26 #include <linux/i2c.h>
27 #include <linux/i2c-sensor.h>
28
29
30 /* Registers */
31 #define ADM1021_SYSCTL_TEMP             1200
32 #define ADM1021_SYSCTL_REMOTE_TEMP      1201
33 #define ADM1021_SYSCTL_DIE_CODE         1202
34 #define ADM1021_SYSCTL_ALARMS           1203
35
36 #define ADM1021_ALARM_TEMP_HIGH         0x40
37 #define ADM1021_ALARM_TEMP_LOW          0x20
38 #define ADM1021_ALARM_RTEMP_HIGH        0x10
39 #define ADM1021_ALARM_RTEMP_LOW         0x08
40 #define ADM1021_ALARM_RTEMP_NA          0x04
41
42 /* Addresses to scan */
43 static unsigned short normal_i2c[] = { I2C_CLIENT_END };
44 static unsigned short normal_i2c_range[] = { 0x18, 0x1a, 0x29, 0x2b,
45         0x4c, 0x4e, I2C_CLIENT_END
46 };
47 static unsigned int normal_isa[] = { I2C_CLIENT_ISA_END };
48 static unsigned int normal_isa_range[] = { I2C_CLIENT_ISA_END };
49
50 /* Insmod parameters */
51 SENSORS_INSMOD_8(adm1021, adm1023, max1617, max1617a, thmc10, lm84, gl523sm, mc1066);
52
53 /* adm1021 constants specified below */
54
55 /* The adm1021 registers */
56 /* Read-only */
57 #define ADM1021_REG_TEMP                0x00
58 #define ADM1021_REG_REMOTE_TEMP         0x01
59 #define ADM1021_REG_STATUS              0x02
60 #define ADM1021_REG_MAN_ID              0x0FE   /* 0x41 = AMD, 0x49 = TI, 0x4D = Maxim, 0x23 = Genesys , 0x54 = Onsemi*/
61 #define ADM1021_REG_DEV_ID              0x0FF   /* ADM1021 = 0x0X, ADM1023 = 0x3X */
62 #define ADM1021_REG_DIE_CODE            0x0FF   /* MAX1617A */
63 /* These use different addresses for reading/writing */
64 #define ADM1021_REG_CONFIG_R            0x03
65 #define ADM1021_REG_CONFIG_W            0x09
66 #define ADM1021_REG_CONV_RATE_R         0x04
67 #define ADM1021_REG_CONV_RATE_W         0x0A
68 /* These are for the ADM1023's additional precision on the remote temp sensor */
69 #define ADM1021_REG_REM_TEMP_PREC       0x010
70 #define ADM1021_REG_REM_OFFSET          0x011
71 #define ADM1021_REG_REM_OFFSET_PREC     0x012
72 #define ADM1021_REG_REM_TOS_PREC        0x013
73 #define ADM1021_REG_REM_THYST_PREC      0x014
74 /* limits */
75 #define ADM1021_REG_TOS_R               0x05
76 #define ADM1021_REG_TOS_W               0x0B
77 #define ADM1021_REG_REMOTE_TOS_R        0x07
78 #define ADM1021_REG_REMOTE_TOS_W        0x0D
79 #define ADM1021_REG_THYST_R             0x06
80 #define ADM1021_REG_THYST_W             0x0C
81 #define ADM1021_REG_REMOTE_THYST_R      0x08
82 #define ADM1021_REG_REMOTE_THYST_W      0x0E
83 /* write-only */
84 #define ADM1021_REG_ONESHOT             0x0F
85
86
87 /* Conversions. Rounding and limit checking is only done on the TO_REG
88    variants. Note that you should be a bit careful with which arguments
89    these macros are called: arguments may be evaluated more than once.
90    Fixing this is just not worth it. */
91 /* Conversions  note: 1021 uses normal integer signed-byte format*/
92 #define TEMP_FROM_REG(val)      (val > 127 ? (val-256)*1000 : val*1000)
93 #define TEMP_TO_REG(val)        (SENSORS_LIMIT((val < 0 ? (val/1000)+256 : val/1000),0,255))
94
95 /* Initial values */
96
97 /* Note: Even though I left the low and high limits named os and hyst, 
98 they don't quite work like a thermostat the way the LM75 does.  I.e., 
99 a lower temp than THYST actually triggers an alarm instead of 
100 clearing it.  Weird, ey?   --Phil  */
101
102 /* Each client has this additional data */
103 struct adm1021_data {
104         struct i2c_client client;
105         enum chips type;
106
107         struct semaphore update_lock;
108         char valid;             /* !=0 if following fields are valid */
109         unsigned long last_updated;     /* In jiffies */
110
111         u8      temp_max;       /* Register values */
112         u8      temp_hyst;
113         u8      temp_input;
114         u8      remote_temp_max;
115         u8      remote_temp_hyst;
116         u8      remote_temp_input;
117         u8      alarms;
118         /* special values for ADM1021 only */
119         u8      die_code;
120         /* Special values for ADM1023 only */
121         u8      remote_temp_prec;
122         u8      remote_temp_os_prec;
123         u8      remote_temp_hyst_prec;
124         u8      remote_temp_offset;
125         u8      remote_temp_offset_prec;
126 };
127
128 static int adm1021_attach_adapter(struct i2c_adapter *adapter);
129 static int adm1021_detect(struct i2c_adapter *adapter, int address, int kind);
130 static void adm1021_init_client(struct i2c_client *client);
131 static int adm1021_detach_client(struct i2c_client *client);
132 static int adm1021_read_value(struct i2c_client *client, u8 reg);
133 static int adm1021_write_value(struct i2c_client *client, u8 reg,
134                                u16 value);
135 static struct adm1021_data *adm1021_update_device(struct device *dev);
136
137 /* (amalysh) read only mode, otherwise any limit's writing confuse BIOS */
138 static int read_only = 0;
139
140
141 /* This is the driver that will be inserted */
142 static struct i2c_driver adm1021_driver = {
143         .owner          = THIS_MODULE,
144         .name           = "adm1021",
145         .id             = I2C_DRIVERID_ADM1021,
146         .flags          = I2C_DF_NOTIFY,
147         .attach_adapter = adm1021_attach_adapter,
148         .detach_client  = adm1021_detach_client,
149 };
150
151 static int adm1021_id = 0;
152
153 #define show(value)     \
154 static ssize_t show_##value(struct device *dev, char *buf)              \
155 {                                                                       \
156         struct adm1021_data *data = adm1021_update_device(dev);         \
157         return sprintf(buf, "%d\n", TEMP_FROM_REG(data->value));        \
158 }
159 show(temp_max);
160 show(temp_hyst);
161 show(temp_input);
162 show(remote_temp_max);
163 show(remote_temp_hyst);
164 show(remote_temp_input);
165
166 #define show2(value)    \
167 static ssize_t show_##value(struct device *dev, char *buf)              \
168 {                                                                       \
169         struct adm1021_data *data = adm1021_update_device(dev);         \
170         return sprintf(buf, "%d\n", data->value);                       \
171 }
172 show2(alarms);
173 show2(die_code);
174
175 #define set(value, reg) \
176 static ssize_t set_##value(struct device *dev, const char *buf, size_t count)   \
177 {                                                               \
178         struct i2c_client *client = to_i2c_client(dev);         \
179         struct adm1021_data *data = i2c_get_clientdata(client); \
180         int temp = simple_strtoul(buf, NULL, 10);               \
181                                                                 \
182         data->value = TEMP_TO_REG(temp);                        \
183         adm1021_write_value(client, reg, data->value);          \
184         return count;                                           \
185 }
186 set(temp_max, ADM1021_REG_TOS_W);
187 set(temp_hyst, ADM1021_REG_THYST_W);
188 set(remote_temp_max, ADM1021_REG_REMOTE_TOS_W);
189 set(remote_temp_hyst, ADM1021_REG_REMOTE_THYST_W);
190
191 static DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp_max, set_temp_max);
192 static DEVICE_ATTR(temp1_min, S_IWUSR | S_IRUGO, show_temp_hyst, set_temp_hyst);
193 static DEVICE_ATTR(temp1_input, S_IRUGO, show_temp_input, NULL);
194 static DEVICE_ATTR(temp2_max, S_IWUSR | S_IRUGO, show_remote_temp_max, set_remote_temp_max);
195 static DEVICE_ATTR(temp2_min, S_IWUSR | S_IRUGO, show_remote_temp_hyst, set_remote_temp_hyst);
196 static DEVICE_ATTR(temp2_input, S_IRUGO, show_remote_temp_input, NULL);
197 static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
198 static DEVICE_ATTR(die_code, S_IRUGO, show_die_code, NULL);
199
200
201 static int adm1021_attach_adapter(struct i2c_adapter *adapter)
202 {
203         if (!(adapter->class & I2C_CLASS_HWMON))
204                 return 0;
205         return i2c_detect(adapter, &addr_data, adm1021_detect);
206 }
207
208 static int adm1021_detect(struct i2c_adapter *adapter, int address, int kind)
209 {
210         int i;
211         struct i2c_client *new_client;
212         struct adm1021_data *data;
213         int err = 0;
214         const char *type_name = "";
215
216         /* Make sure we aren't probing the ISA bus!! This is just a safety check
217            at this moment; i2c_detect really won't call us. */
218 #ifdef DEBUG
219         if (i2c_is_isa_adapter(adapter)) {
220                 dev_dbg(&adapter->dev, "adm1021_detect called for an ISA bus adapter?!?\n");
221                 return 0;
222         }
223 #endif
224
225         if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
226                 goto error0;
227
228         /* OK. For now, we presume we have a valid client. We now create the
229            client structure, even though we cannot fill it completely yet.
230            But it allows us to access adm1021_{read,write}_value. */
231
232         if (!(data = kmalloc(sizeof(struct adm1021_data), GFP_KERNEL))) {
233                 err = -ENOMEM;
234                 goto error0;
235         }
236         memset(data, 0, sizeof(struct adm1021_data));
237
238         new_client = &data->client;
239         i2c_set_clientdata(new_client, data);
240         new_client->addr = address;
241         new_client->adapter = adapter;
242         new_client->driver = &adm1021_driver;
243         new_client->flags = 0;
244
245         /* Now, we do the remaining detection. */
246         if (kind < 0) {
247                 if ((adm1021_read_value(new_client, ADM1021_REG_STATUS) & 0x03) != 0x00
248                  || (adm1021_read_value(new_client, ADM1021_REG_CONFIG_R) & 0x3F) != 0x00
249                  || (adm1021_read_value(new_client, ADM1021_REG_CONV_RATE_R) & 0xF8) != 0x00) {
250                         err = -ENODEV;
251                         goto error1;
252                 }
253         }
254
255         /* Determine the chip type. */
256         if (kind <= 0) {
257                 i = adm1021_read_value(new_client, ADM1021_REG_MAN_ID);
258                 if (i == 0x41)
259                         if ((adm1021_read_value(new_client, ADM1021_REG_DEV_ID) & 0x0F0) == 0x030)
260                                 kind = adm1023;
261                         else
262                                 kind = adm1021;
263                 else if (i == 0x49)
264                         kind = thmc10;
265                 else if (i == 0x23)
266                         kind = gl523sm;
267                 else if ((i == 0x4d) &&
268                          (adm1021_read_value(new_client, ADM1021_REG_DEV_ID) == 0x01))
269                         kind = max1617a;
270                 else if (i == 0x54)
271                         kind = mc1066;
272                 /* LM84 Mfr ID in a different place, and it has more unused bits */
273                 else if (adm1021_read_value(new_client, ADM1021_REG_CONV_RATE_R) == 0x00
274                       && (kind == 0 /* skip extra detection */
275                        || ((adm1021_read_value(new_client, ADM1021_REG_CONFIG_R) & 0x7F) == 0x00
276                         && (adm1021_read_value(new_client, ADM1021_REG_STATUS) & 0xAB) == 0x00)))
277                         kind = lm84;
278                 else
279                         kind = max1617;
280         }
281
282         if (kind == max1617) {
283                 type_name = "max1617";
284         } else if (kind == max1617a) {
285                 type_name = "max1617a";
286         } else if (kind == adm1021) {
287                 type_name = "adm1021";
288         } else if (kind == adm1023) {
289                 type_name = "adm1023";
290         } else if (kind == thmc10) {
291                 type_name = "thmc10";
292         } else if (kind == lm84) {
293                 type_name = "lm84";
294         } else if (kind == gl523sm) {
295                 type_name = "gl523sm";
296         } else if (kind == mc1066) {
297                 type_name = "mc1066";
298         }
299
300         /* Fill in the remaining client fields and put it into the global list */
301         strlcpy(new_client->name, type_name, I2C_NAME_SIZE);
302         data->type = kind;
303
304         new_client->id = adm1021_id++;
305         data->valid = 0;
306         init_MUTEX(&data->update_lock);
307
308         /* Tell the I2C layer a new client has arrived */
309         if ((err = i2c_attach_client(new_client)))
310                 goto error1;
311
312         /* Initialize the ADM1021 chip */
313         if (kind != lm84)
314                 adm1021_init_client(new_client);
315
316         /* Register sysfs hooks */
317         device_create_file(&new_client->dev, &dev_attr_temp1_max);
318         device_create_file(&new_client->dev, &dev_attr_temp1_min);
319         device_create_file(&new_client->dev, &dev_attr_temp1_input);
320         device_create_file(&new_client->dev, &dev_attr_temp2_max);
321         device_create_file(&new_client->dev, &dev_attr_temp2_min);
322         device_create_file(&new_client->dev, &dev_attr_temp2_input);
323         device_create_file(&new_client->dev, &dev_attr_alarms);
324         if (data->type == adm1021)
325                 device_create_file(&new_client->dev, &dev_attr_die_code);
326
327         return 0;
328
329 error1:
330         kfree(data);
331 error0:
332         return err;
333 }
334
335 static void adm1021_init_client(struct i2c_client *client)
336 {
337         /* Enable ADC and disable suspend mode */
338         adm1021_write_value(client, ADM1021_REG_CONFIG_W,
339                 adm1021_read_value(client, ADM1021_REG_CONFIG_R) & 0xBF);
340         /* Set Conversion rate to 1/sec (this can be tinkered with) */
341         adm1021_write_value(client, ADM1021_REG_CONV_RATE_W, 0x04);
342 }
343
344 static int adm1021_detach_client(struct i2c_client *client)
345 {
346         int err;
347
348         if ((err = i2c_detach_client(client))) {
349                 dev_err(&client->dev, "Client deregistration failed, client not detached.\n");
350                 return err;
351         }
352
353         kfree(i2c_get_clientdata(client));
354         return 0;
355 }
356
357 /* All registers are byte-sized */
358 static int adm1021_read_value(struct i2c_client *client, u8 reg)
359 {
360         return i2c_smbus_read_byte_data(client, reg);
361 }
362
363 static int adm1021_write_value(struct i2c_client *client, u8 reg, u16 value)
364 {
365         if (!read_only)
366                 return i2c_smbus_write_byte_data(client, reg, value);
367         return 0;
368 }
369
370 static struct adm1021_data *adm1021_update_device(struct device *dev)
371 {
372         struct i2c_client *client = to_i2c_client(dev);
373         struct adm1021_data *data = i2c_get_clientdata(client);
374
375         down(&data->update_lock);
376
377         if ((jiffies - data->last_updated > HZ + HZ / 2) ||
378             (jiffies < data->last_updated) || !data->valid) {
379                 dev_dbg(&client->dev, "Starting adm1021 update\n");
380
381                 data->temp_input = adm1021_read_value(client, ADM1021_REG_TEMP);
382                 data->temp_max = adm1021_read_value(client, ADM1021_REG_TOS_R);
383                 data->temp_hyst = adm1021_read_value(client, ADM1021_REG_THYST_R);
384                 data->remote_temp_input = adm1021_read_value(client, ADM1021_REG_REMOTE_TEMP);
385                 data->remote_temp_max = adm1021_read_value(client, ADM1021_REG_REMOTE_TOS_R);
386                 data->remote_temp_hyst = adm1021_read_value(client, ADM1021_REG_REMOTE_THYST_R);
387                 data->alarms = adm1021_read_value(client, ADM1021_REG_STATUS) & 0xec;
388                 if (data->type == adm1021)
389                         data->die_code = adm1021_read_value(client, ADM1021_REG_DIE_CODE);
390                 if (data->type == adm1023) {
391                         data->remote_temp_prec = adm1021_read_value(client, ADM1021_REG_REM_TEMP_PREC);
392                         data->remote_temp_os_prec = adm1021_read_value(client, ADM1021_REG_REM_TOS_PREC);
393                         data->remote_temp_hyst_prec = adm1021_read_value(client, ADM1021_REG_REM_THYST_PREC);
394                         data->remote_temp_offset = adm1021_read_value(client, ADM1021_REG_REM_OFFSET);
395                         data->remote_temp_offset_prec = adm1021_read_value(client, ADM1021_REG_REM_OFFSET_PREC);
396                 }
397                 data->last_updated = jiffies;
398                 data->valid = 1;
399         }
400
401         up(&data->update_lock);
402
403         return data;
404 }
405
406 static int __init sensors_adm1021_init(void)
407 {
408         return i2c_add_driver(&adm1021_driver);
409 }
410
411 static void __exit sensors_adm1021_exit(void)
412 {
413         i2c_del_driver(&adm1021_driver);
414 }
415
416 MODULE_AUTHOR ("Frodo Looijaard <frodol@dds.nl> and "
417                 "Philip Edelbrock <phil@netroedge.com>");
418 MODULE_DESCRIPTION("adm1021 driver");
419 MODULE_LICENSE("GPL");
420
421 MODULE_PARM(read_only, "i");
422 MODULE_PARM_DESC(read_only, "Don't set any values, read only mode");
423
424 module_init(sensors_adm1021_init)
425 module_exit(sensors_adm1021_exit)