patch-2_6_7-vs1_9_1_12
[linux-2.6.git] / drivers / i2c / chips / lm83.c
1 /*
2  * lm83.c - Part of lm_sensors, Linux kernel modules for hardware
3  *          monitoring
4  * Copyright (C) 2003  Jean Delvare <khali@linux-fr.org>
5  *
6  * Heavily inspired from the lm78, lm75 and adm1021 drivers. The LM83 is
7  * a sensor chip made by National Semiconductor. It reports up to four
8  * temperatures (its own plus up to three external ones) with a 1 deg
9  * resolution and a 3-4 deg accuracy. Complete datasheet can be obtained
10  * from National's website at:
11  *   http://www.national.com/pf/LM/LM83.html
12  * Since the datasheet omits to give the chip stepping code, I give it
13  * here: 0x03 (at register 0xff).
14  *
15  * This program is free software; you can redistribute it and/or modify
16  * it under the terms of the GNU General Public License as published by
17  * the Free Software Foundation; either version 2 of the License, or
18  * (at your option) any later version.
19  *
20  * This program is distributed in the hope that it will be useful,
21  * but WITHOUT ANY WARRANTY; without even the implied warranty of
22  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23  * GNU General Public License for more details.
24  *
25  * You should have received a copy of the GNU General Public License
26  * along with this program; if not, write to the Free Software
27  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28  */
29
30 #include <linux/config.h>
31 #include <linux/module.h>
32 #include <linux/init.h>
33 #include <linux/slab.h>
34 #include <linux/i2c.h>
35 #include <linux/i2c-sensor.h>
36
37 /*
38  * Addresses to scan
39  * Address is selected using 2 three-level pins, resulting in 9 possible
40  * addresses.
41  */
42
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 static unsigned int normal_isa[] = { I2C_CLIENT_ISA_END };
47 static unsigned int normal_isa_range[] = { I2C_CLIENT_ISA_END };
48
49 /*
50  * Insmod parameters
51  */
52
53 SENSORS_INSMOD_1(lm83);
54
55 /*
56  * The LM83 registers
57  * Manufacturer ID is 0x01 for National Semiconductor.
58  */
59
60 #define LM83_REG_R_MAN_ID               0xFE
61 #define LM83_REG_R_CHIP_ID              0xFF
62 #define LM83_REG_R_CONFIG               0x03
63 #define LM83_REG_W_CONFIG               0x09
64 #define LM83_REG_R_STATUS1              0x02
65 #define LM83_REG_R_STATUS2              0x35
66 #define LM83_REG_R_LOCAL_TEMP           0x00
67 #define LM83_REG_R_LOCAL_HIGH           0x05
68 #define LM83_REG_W_LOCAL_HIGH           0x0B
69 #define LM83_REG_R_REMOTE1_TEMP         0x30
70 #define LM83_REG_R_REMOTE1_HIGH         0x38
71 #define LM83_REG_W_REMOTE1_HIGH         0x50
72 #define LM83_REG_R_REMOTE2_TEMP         0x01
73 #define LM83_REG_R_REMOTE2_HIGH         0x07
74 #define LM83_REG_W_REMOTE2_HIGH         0x0D
75 #define LM83_REG_R_REMOTE3_TEMP         0x31
76 #define LM83_REG_R_REMOTE3_HIGH         0x3A
77 #define LM83_REG_W_REMOTE3_HIGH         0x52
78 #define LM83_REG_R_TCRIT                0x42
79 #define LM83_REG_W_TCRIT                0x5A
80
81 /*
82  * Conversions and various macros
83  * The LM83 uses signed 8-bit values.
84  */
85
86 #define TEMP_FROM_REG(val)      ((val > 127 ? val-256 : val) * 1000)
87 #define TEMP_TO_REG(val)        ((val < 0 ? val+256 : val) / 1000)
88
89 static const u8 LM83_REG_R_TEMP[] = {
90         LM83_REG_R_LOCAL_TEMP,
91         LM83_REG_R_REMOTE1_TEMP,
92         LM83_REG_R_REMOTE2_TEMP,
93         LM83_REG_R_REMOTE3_TEMP
94 };
95
96 static const u8 LM83_REG_R_HIGH[] = {
97         LM83_REG_R_LOCAL_HIGH,
98         LM83_REG_R_REMOTE1_HIGH,
99         LM83_REG_R_REMOTE2_HIGH,
100         LM83_REG_R_REMOTE3_HIGH
101 };
102
103 static const u8 LM83_REG_W_HIGH[] = {
104         LM83_REG_W_LOCAL_HIGH,
105         LM83_REG_W_REMOTE1_HIGH,
106         LM83_REG_W_REMOTE2_HIGH,
107         LM83_REG_W_REMOTE3_HIGH
108 };
109
110 /*
111  * Functions declaration
112  */
113
114 static int lm83_attach_adapter(struct i2c_adapter *adapter);
115 static int lm83_detect(struct i2c_adapter *adapter, int address, int kind);
116 static int lm83_detach_client(struct i2c_client *client);
117 static struct lm83_data *lm83_update_device(struct device *dev);
118
119 /*
120  * Driver data (common to all clients)
121  */
122  
123 static struct i2c_driver lm83_driver = {
124         .owner          = THIS_MODULE,
125         .name           = "lm83",
126         .id             = I2C_DRIVERID_LM83,
127         .flags          = I2C_DF_NOTIFY,
128         .attach_adapter = lm83_attach_adapter,
129         .detach_client  = lm83_detach_client,
130 };
131
132 /*
133  * Client data (each client gets its own)
134  */
135
136 struct lm83_data {
137         struct i2c_client client;
138         struct semaphore update_lock;
139         char valid; /* zero until following fields are valid */
140         unsigned long last_updated; /* in jiffies */
141
142         /* registers values */
143         u8 temp_input[4];
144         u8 temp_high[4];
145         u8 temp_crit;
146         u16 alarms; /* bitvector, combined */
147 };
148
149 /*
150  * Internal variables
151  */
152
153 static int lm83_id = 0;
154
155 /*
156  * Sysfs stuff
157  */
158
159 #define show_temp(suffix, value) \
160 static ssize_t show_temp_##suffix(struct device *dev, char *buf) \
161 { \
162         struct lm83_data *data = lm83_update_device(dev); \
163         return sprintf(buf, "%d\n", TEMP_FROM_REG(data->value)); \
164 }
165 show_temp(input1, temp_input[0]);
166 show_temp(input2, temp_input[1]);
167 show_temp(input3, temp_input[2]);
168 show_temp(input4, temp_input[3]);
169 show_temp(high1, temp_high[0]);
170 show_temp(high2, temp_high[1]);
171 show_temp(high3, temp_high[2]);
172 show_temp(high4, temp_high[3]);
173 show_temp(crit, temp_crit);
174
175 #define set_temp(suffix, value, reg) \
176 static ssize_t set_temp_##suffix(struct device *dev, const char *buf, \
177         size_t count) \
178 { \
179         struct i2c_client *client = to_i2c_client(dev); \
180         struct lm83_data *data = i2c_get_clientdata(client); \
181         data->value = TEMP_TO_REG(simple_strtoul(buf, NULL, 10)); \
182         i2c_smbus_write_byte_data(client, reg, data->value); \
183         return count; \
184 }
185 set_temp(high1, temp_high[0], LM83_REG_W_LOCAL_HIGH);
186 set_temp(high2, temp_high[1], LM83_REG_W_REMOTE1_HIGH);
187 set_temp(high3, temp_high[2], LM83_REG_W_REMOTE2_HIGH);
188 set_temp(high4, temp_high[3], LM83_REG_W_REMOTE3_HIGH);
189 set_temp(crit, temp_crit, LM83_REG_W_TCRIT);
190
191 static ssize_t show_alarms(struct device *dev, char *buf)
192 {
193         struct lm83_data *data = lm83_update_device(dev);
194         return sprintf(buf, "%d\n", data->alarms);
195 }
196
197 static DEVICE_ATTR(temp1_input, S_IRUGO, show_temp_input1, NULL);
198 static DEVICE_ATTR(temp2_input, S_IRUGO, show_temp_input2, NULL);
199 static DEVICE_ATTR(temp3_input, S_IRUGO, show_temp_input3, NULL);
200 static DEVICE_ATTR(temp4_input, S_IRUGO, show_temp_input4, NULL);
201 static DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp_high1,
202     set_temp_high1);
203 static DEVICE_ATTR(temp2_max, S_IWUSR | S_IRUGO, show_temp_high2,
204     set_temp_high2);
205 static DEVICE_ATTR(temp3_max, S_IWUSR | S_IRUGO, show_temp_high3,
206     set_temp_high3);
207 static DEVICE_ATTR(temp4_max, S_IWUSR | S_IRUGO, show_temp_high4,
208     set_temp_high4);
209 static DEVICE_ATTR(temp_crit, S_IWUSR | S_IRUGO, show_temp_crit,
210     set_temp_crit);
211 static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
212
213 /*
214  * Real code
215  */
216
217 static int lm83_attach_adapter(struct i2c_adapter *adapter)
218 {
219         if (!(adapter->class & I2C_CLASS_HWMON))
220                 return 0;
221         return i2c_detect(adapter, &addr_data, lm83_detect);
222 }
223
224 /*
225  * The following function does more than just detection. If detection
226  * succeeds, it also registers the new chip.
227  */
228 static int lm83_detect(struct i2c_adapter *adapter, int address, int kind)
229 {
230         struct i2c_client *new_client;
231         struct lm83_data *data;
232         int err = 0;
233         const char *name = "";
234
235         if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
236                 goto exit;
237
238         if (!(data = kmalloc(sizeof(struct lm83_data), GFP_KERNEL))) {
239                 err = -ENOMEM;
240                 goto exit;
241         }
242         memset(data, 0, sizeof(struct lm83_data));
243
244         /* The common I2C client data is placed right after the
245          * LM83-specific data. */
246         new_client = &data->client;
247         i2c_set_clientdata(new_client, data);
248         new_client->addr = address;
249         new_client->adapter = adapter;
250         new_client->driver = &lm83_driver;
251         new_client->flags = 0;
252
253         /* Now we do the detection and identification. A negative kind
254          * means that the driver was loaded with no force parameter
255          * (default), so we must both detect and identify the chip
256          * (actually there is only one possible kind of chip for now, LM83).
257          * A zero kind means that the driver was loaded with the force
258          * parameter, the detection step shall be skipped. A positive kind
259          * means that the driver was loaded with the force parameter and a
260          * given kind of chip is requested, so both the detection and the
261          * identification steps are skipped. */
262         if (kind < 0) { /* detection */
263                 if (((i2c_smbus_read_byte_data(new_client, LM83_REG_R_STATUS1)
264                     & 0xA8) != 0x00) ||
265                     ((i2c_smbus_read_byte_data(new_client, LM83_REG_R_STATUS2)
266                     & 0x48) != 0x00) ||
267                     ((i2c_smbus_read_byte_data(new_client, LM83_REG_R_CONFIG)
268                     & 0x41) != 0x00)) {
269                         dev_dbg(&adapter->dev,
270                             "LM83 detection failed at 0x%02x.\n", address);
271                         goto exit_free;
272                 }
273         }
274
275         if (kind <= 0) { /* identification */
276                 u8 man_id, chip_id;
277
278                 man_id = i2c_smbus_read_byte_data(new_client,
279                     LM83_REG_R_MAN_ID);
280                 chip_id = i2c_smbus_read_byte_data(new_client,
281                     LM83_REG_R_CHIP_ID);
282
283                 if (man_id == 0x01) { /* National Semiconductor */
284                         if (chip_id == 0x03) {
285                                 kind = lm83;
286                         }
287                 }
288
289                 if (kind <= 0) { /* identification failed */
290                         dev_info(&adapter->dev,
291                             "Unsupported chip (man_id=0x%02X, "
292                             "chip_id=0x%02X).\n", man_id, chip_id);
293                         goto exit_free;
294                 }
295         }
296
297         if (kind == lm83) {
298                 name = "lm83";
299         }
300
301         /* We can fill in the remaining client fields */
302         strlcpy(new_client->name, name, I2C_NAME_SIZE);
303         new_client->id = lm83_id++;
304         data->valid = 0;
305         init_MUTEX(&data->update_lock);
306
307         /* Tell the I2C layer a new client has arrived */
308         if ((err = i2c_attach_client(new_client)))
309                 goto exit_free;
310
311         /*
312          * Initialize the LM83 chip
313          * (Nothing to do for this one.)
314          */
315
316         /* Register sysfs hooks */
317         device_create_file(&new_client->dev, &dev_attr_temp1_input);
318         device_create_file(&new_client->dev, &dev_attr_temp2_input);
319         device_create_file(&new_client->dev, &dev_attr_temp3_input);
320         device_create_file(&new_client->dev, &dev_attr_temp4_input);
321         device_create_file(&new_client->dev, &dev_attr_temp1_max);
322         device_create_file(&new_client->dev, &dev_attr_temp2_max);
323         device_create_file(&new_client->dev, &dev_attr_temp3_max);
324         device_create_file(&new_client->dev, &dev_attr_temp4_max);
325         device_create_file(&new_client->dev, &dev_attr_temp_crit);
326         device_create_file(&new_client->dev, &dev_attr_alarms);
327
328         return 0;
329
330 exit_free:
331         kfree(data);
332 exit:
333         return err;
334 }
335
336 static int lm83_detach_client(struct i2c_client *client)
337 {
338         int err;
339
340         if ((err = i2c_detach_client(client))) {
341                 dev_err(&client->dev,
342                     "Client deregistration failed, client not detached.\n");
343                 return err;
344         }
345
346         kfree(i2c_get_clientdata(client));
347         return 0;
348 }
349
350 static struct lm83_data *lm83_update_device(struct device *dev)
351 {
352         struct i2c_client *client = to_i2c_client(dev);
353         struct lm83_data *data = i2c_get_clientdata(client);
354
355         down(&data->update_lock);
356
357         if ((jiffies - data->last_updated > HZ * 2) ||
358             (jiffies < data->last_updated) ||
359             !data->valid) {
360                 int nr;
361
362                 dev_dbg(&client->dev, "Updating lm83 data.\n");
363                 for (nr = 0; nr < 4 ; nr++) {
364                         data->temp_input[nr] =
365                             i2c_smbus_read_byte_data(client,
366                             LM83_REG_R_TEMP[nr]);
367                         data->temp_high[nr] =
368                             i2c_smbus_read_byte_data(client,
369                             LM83_REG_R_HIGH[nr]);
370                 }
371                 data->temp_crit =
372                     i2c_smbus_read_byte_data(client, LM83_REG_R_TCRIT);
373                 data->alarms =
374                     i2c_smbus_read_byte_data(client, LM83_REG_R_STATUS1)
375                     + (i2c_smbus_read_byte_data(client, LM83_REG_R_STATUS2)
376                     << 8);
377
378                 data->last_updated = jiffies;
379                 data->valid = 1;
380         }
381
382         up(&data->update_lock);
383
384         return data;
385 }
386
387 static int __init sensors_lm83_init(void)
388 {
389         return i2c_add_driver(&lm83_driver);
390 }
391
392 static void __exit sensors_lm83_exit(void)
393 {
394         i2c_del_driver(&lm83_driver);
395 }
396
397 MODULE_AUTHOR("Jean Delvare <khali@linux-fr.org>");
398 MODULE_DESCRIPTION("LM83 driver");
399 MODULE_LICENSE("GPL");
400
401 module_init(sensors_lm83_init);
402 module_exit(sensors_lm83_exit);