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