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