root/lm-sensors/trunk/kernel/chips/lm83.c @ 2192

Revision 2192, 12.9 KB (checked in by khali, 9 years ago)

Misc cleanups inspired from Linux 2.6.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
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/module.h>
31#include <linux/slab.h>
32#include <linux/i2c.h>
33#include <linux/i2c-proc.h>
34#include <linux/init.h>
35#include "version.h"
36
37/*
38 * Addresses to scan
39 * Address is selected using 2 three-level pins, resulting in 9 possible
40 * addresses.
41 */
42
43static unsigned short normal_i2c[] = { SENSORS_I2C_END };
44static unsigned short normal_i2c_range[] = { 0x18, 0x1a, 0x29, 0x2b,
45        0x4c, 0x4e, SENSORS_I2C_END };
46static unsigned int normal_isa[] = { SENSORS_ISA_END };
47static unsigned int normal_isa_range[] = { SENSORS_ISA_END };
48
49/*
50 * Insmod parameters
51 */
52
53SENSORS_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)
87#define TEMP_TO_REG(val)    (val < 0 ? val+256 : val)
88
89static 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
96static 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
103static 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
114static int lm83_attach_adapter(struct i2c_adapter *adapter);
115static int lm83_detect(struct i2c_adapter *adapter, int address, unsigned
116        short flags, int kind);
117static int lm83_detach_client(struct i2c_client *client);
118static void lm83_update_client(struct i2c_client *client);
119static void lm83_temp(struct i2c_client *client, int operation, int
120        ctl_name, int *nrels_mag, long *results);
121static void lm83_tcrit(struct i2c_client *client, int operation, int
122        ctl_name, int *nrels_mag, long *results);
123static void lm83_alarms(struct i2c_client *client, int operation, int
124        ctl_name, int *nrels_mag, long *results);
125
126/*
127 * Driver data (common to all clients)
128 */
129 
130static struct i2c_driver lm83_driver = {
131        .owner          = THIS_MODULE,
132        .name           = "LM83 sensor driver",
133        .id             = I2C_DRIVERID_LM83,
134        .flags          = I2C_DF_NOTIFY,
135        .attach_adapter = lm83_attach_adapter,
136        .detach_client  = lm83_detach_client,
137};
138
139/*
140 * Client data (each client gets its own)
141 */
142
143struct lm83_data
144{
145        int sysctl_id;
146
147        struct semaphore update_lock;
148        char valid; /* zero until following fields are valid */
149        unsigned long last_updated; /* in jiffies */
150
151        /* registers values */
152        u8 temp[4], temp_high[4], tcrit;
153        u16 alarms; /* bitvector, combined */
154};
155
156/*
157 * Proc entries
158 * These files are created for each detected LM83.
159 */
160
161/* -- SENSORS SYSCTL START -- */
162
163#define LM83_SYSCTL_LOCAL_TEMP    1200
164#define LM83_SYSCTL_REMOTE1_TEMP  1201
165#define LM83_SYSCTL_REMOTE2_TEMP  1202
166#define LM83_SYSCTL_REMOTE3_TEMP  1203
167#define LM83_SYSCTL_TCRIT         1208
168#define LM83_SYSCTL_ALARMS        1210
169
170#define LM83_ALARM_LOCAL_HIGH     0x0040
171#define LM83_ALARM_LOCAL_CRIT     0x0001
172#define LM83_ALARM_REMOTE1_HIGH   0x8000
173#define LM83_ALARM_REMOTE1_CRIT   0x0100
174#define LM83_ALARM_REMOTE1_OPEN   0x2000
175#define LM83_ALARM_REMOTE2_HIGH   0x0010
176#define LM83_ALARM_REMOTE2_CRIT   0x0002
177#define LM83_ALARM_REMOTE2_OPEN   0x0004
178#define LM83_ALARM_REMOTE3_HIGH   0x1000
179#define LM83_ALARM_REMOTE3_CRIT   0x0200
180#define LM83_ALARM_REMOTE3_OPEN   0x0400
181
182/* -- SENSORS SYSCTL END -- */
183
184
185static ctl_table lm83_dir_table_template[] =
186{
187        {LM83_SYSCTL_LOCAL_TEMP, "temp1", NULL, 0, 0644, NULL,
188         &i2c_proc_real, &i2c_sysctl_real, NULL, &lm83_temp},
189        {LM83_SYSCTL_REMOTE1_TEMP, "temp2", NULL, 0, 0644, NULL,
190         &i2c_proc_real, &i2c_sysctl_real, NULL, &lm83_temp},
191        {LM83_SYSCTL_REMOTE2_TEMP, "temp3", NULL, 0, 0644, NULL,
192         &i2c_proc_real, &i2c_sysctl_real, NULL, &lm83_temp},
193        {LM83_SYSCTL_REMOTE3_TEMP, "temp4", NULL, 0, 0644, NULL,
194         &i2c_proc_real, &i2c_sysctl_real, NULL, &lm83_temp},
195        {LM83_SYSCTL_TCRIT, "tcrit", NULL, 0, 0644, NULL,
196         &i2c_proc_real, &i2c_sysctl_real, NULL, &lm83_tcrit},
197        {LM83_SYSCTL_ALARMS, "alarms", NULL, 0, 0444, NULL,
198         &i2c_proc_real, &i2c_sysctl_real, NULL, &lm83_alarms},
199        {0}
200};
201
202/*
203 * Internal variables
204 */
205
206static int lm83_id = 0;
207
208/*
209 * Real code
210 */
211
212static int lm83_attach_adapter(struct i2c_adapter *adapter)
213{
214        return i2c_detect(adapter, &addr_data, lm83_detect);
215}
216
217/*
218 * The following function does more than just detection. If detection
219 * succeeds, it also registers the new chip.
220 */
221static int lm83_detect(struct i2c_adapter *adapter, int address, unsigned
222        short flags, int kind)
223{
224        struct i2c_client *new_client;
225        struct lm83_data *data;
226        int err = 0;
227        const char *type_name = "";
228        const char *client_name = "";
229
230#ifdef DEBUG
231        if (i2c_is_isa_adapter(adapter))
232        {
233                printk("lm83.o: Called for an ISA bus adapter, aborting.\n");
234                return 0;
235        }
236#endif
237
238        if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
239        {
240#ifdef DEBUG
241                printk("lm83.o: I2C bus doesn't support byte read mode, "
242                       "skipping.\n");
243#endif
244                return 0;
245        }
246
247        if (!(new_client = kmalloc(sizeof(struct i2c_client) + sizeof(struct
248                lm83_data), GFP_KERNEL)))
249        {
250                printk("lm83.o: Out of memory in lm83_detect (new_client).\n");
251                return -ENOMEM;
252        }
253
254        /*
255         * The LM83-specific data is placed right after the common I2C
256         * client data, and is pointed to by the data field from the I2C
257         * client data.
258         */
259
260        new_client->addr = address;
261        new_client->data = data = (struct lm83_data *) (new_client + 1);
262        new_client->adapter = adapter;
263        new_client->driver = &lm83_driver;
264        new_client->flags = 0;
265
266        /*
267         * Now we do the remaining detection. A negative kind means that
268         * the driver was loaded with no force parameter (default), so we
269         * must both detect and identify the chip (actually there is only
270         * one possible kind of chip for now, LM83). A zero kind means that
271         * the driver was loaded with the force parameter, the detection
272         * step shall be skipped. A positive kind means that the driver
273         * was loaded with the force parameter and a given kind of chip is
274         * requested, so both the detection and the identification steps
275         * are skipped.
276         */
277
278        if (kind < 0) /* detection */
279        {
280                if (((i2c_smbus_read_byte_data(new_client, LM83_REG_R_STATUS1)
281                      & 0xA8) != 0x00)
282                ||  ((i2c_smbus_read_byte_data(new_client, LM83_REG_R_STATUS2)
283                      & 0x48) != 0x00)
284                ||  ((i2c_smbus_read_byte_data(new_client, LM83_REG_R_CONFIG)
285                      & 0x41) != 0x00))
286                {
287#ifdef DEBUG
288                        printk(KERN_DEBUG "lm83.o: Detection failed at 0x%02x.\n",
289                                address);
290#endif
291                        goto ERROR1;
292                }
293        }
294
295        if (kind <= 0) /* identification */
296        {
297                u8 man_id, chip_id;
298
299                man_id = i2c_smbus_read_byte_data(new_client, LM83_REG_R_MAN_ID);
300                chip_id = i2c_smbus_read_byte_data(new_client, LM83_REG_R_CHIP_ID);
301                if (man_id == 0x01) /* National Semiconductor */
302                {
303                        if (chip_id == 0x03)
304                                kind = lm83;
305                }
306        }
307
308        if (kind <= 0) /* identification failed */
309        {
310                printk("lm83.o: Unsupported chip.\n");
311                goto ERROR1;
312        }
313
314        if (kind == lm83)
315        {
316                type_name = "lm83";
317                client_name = "LM83 chip";
318        }
319        else
320        {
321                printk("lm83.o: Unknown kind %d.\n", kind);
322                goto ERROR1;
323        }
324       
325        /*
326         * OK, we got a valid chip so we can fill in the remaining client
327         * fields.
328         */
329
330        strcpy(new_client->name, client_name);
331        new_client->id = lm83_id++;
332        data->valid = 0;
333        init_MUTEX(&data->update_lock);
334
335        /*
336         * Tell the I2C layer a new client has arrived.
337         */
338
339        if ((err = i2c_attach_client(new_client)))
340        {
341#ifdef DEBUG
342                printk("lm83.o: Failed attaching client.\n");
343#endif
344                goto ERROR1;
345        }
346
347        /*
348         * Register a new directory entry.
349         */
350
351        if ((err = i2c_register_entry(new_client, type_name,
352             lm83_dir_table_template)) < 0)
353        {
354#ifdef DEBUG
355                printk("lm83.o: Failed registering directory entry.\n");
356#endif
357                goto ERROR2;
358        }
359        data->sysctl_id = err;
360
361        /*
362         * Initialize the LM83 chip
363         * (Nothing to do for this one.)
364         */
365
366        return 0;
367
368        ERROR2:
369        i2c_detach_client(new_client);
370        ERROR1:
371        kfree(new_client);
372        return err;
373}
374
375static int lm83_detach_client(struct i2c_client *client)
376{
377        int err;
378
379        i2c_deregister_entry(((struct lm83_data *) (client->data))->sysctl_id);
380        if ((err = i2c_detach_client(client)))
381        {
382                printk("lm83.o: Client deregistration failed, client not "
383                       "detached.\n");
384                return err;
385        }
386
387        kfree(client);
388        return 0;
389}
390
391static void lm83_update_client(struct i2c_client *client)
392{
393        struct lm83_data *data = client->data;
394
395        down(&data->update_lock);
396
397        if ((jiffies - data->last_updated > HZ * 2) ||
398            (jiffies < data->last_updated) || !data->valid)
399        {
400                int nr;
401#ifdef DEBUG
402                printk("lm83.o: Updating LM83 data.\n");
403#endif
404                for (nr = 0; nr < 4 ; nr++)
405                {
406                        data->temp[nr] =
407                                i2c_smbus_read_byte_data(client, LM83_REG_R_TEMP[nr]);
408                        data->temp_high[nr] =
409                                i2c_smbus_read_byte_data(client, LM83_REG_R_HIGH[nr]);
410                }
411                data->tcrit = i2c_smbus_read_byte_data(client, LM83_REG_R_TCRIT);
412                data->alarms =
413                        i2c_smbus_read_byte_data(client, LM83_REG_R_STATUS1) +
414                        (i2c_smbus_read_byte_data(client, LM83_REG_R_STATUS2) << 8);
415
416                data->last_updated = jiffies;
417                data->valid = 1;
418        }
419
420        up(&data->update_lock);
421}
422
423static void lm83_temp(struct i2c_client *client, int operation, int
424        ctl_name, int *nrels_mag, long *results)
425{
426        struct lm83_data *data = client->data;
427        int nr = ctl_name - LM83_SYSCTL_LOCAL_TEMP;
428
429        if (operation == SENSORS_PROC_REAL_INFO)
430                *nrels_mag = 0; /* magnitude */
431        else if (operation == SENSORS_PROC_REAL_READ)
432        {
433                lm83_update_client(client);
434                results[0] = TEMP_FROM_REG(data->temp_high[nr]);
435                results[1] = TEMP_FROM_REG(data->temp[nr]);
436                *nrels_mag = 2;
437        }
438        else if (operation == SENSORS_PROC_REAL_WRITE)
439        {
440                if (*nrels_mag >= 1)
441                {
442                        data->temp_high[nr] = TEMP_TO_REG(results[0]);
443                        i2c_smbus_write_byte_data(client, LM83_REG_W_HIGH[nr],
444                                            data->temp_high[nr]);
445                }
446        }
447}
448
449static void lm83_tcrit(struct i2c_client *client, int operation, int
450        ctl_name, int *nrels_mag, long *results)
451{
452        struct lm83_data *data = client->data;
453
454        if (operation == SENSORS_PROC_REAL_INFO)
455                *nrels_mag = 0; /* magnitude */
456        else if (operation == SENSORS_PROC_REAL_READ)
457        {
458                lm83_update_client(client);
459                results[0] = TEMP_FROM_REG(data->tcrit);
460                *nrels_mag = 1;
461        }
462        else if (operation == SENSORS_PROC_REAL_WRITE)
463        {
464                if (*nrels_mag >= 1)
465                {
466                        data->tcrit = TEMP_TO_REG(results[0]);
467                        i2c_smbus_write_byte_data(client, LM83_REG_W_TCRIT,
468                                data->tcrit);
469                }
470        }
471}
472
473static void lm83_alarms(struct i2c_client *client, int operation, int
474        ctl_name, int *nrels_mag, long *results)
475{
476        struct lm83_data *data = client->data;
477
478        if (operation == SENSORS_PROC_REAL_INFO)
479                *nrels_mag = 0; /* magnitude */
480        else if (operation == SENSORS_PROC_REAL_READ)
481        {
482                lm83_update_client(client);
483                results[0] = data->alarms;
484                *nrels_mag = 1;
485        }
486}
487
488static int __init sm_lm83_init(void)
489{
490        printk(KERN_INFO "lm83.o version %s (%s)\n", LM_VERSION, LM_DATE);
491        return i2c_add_driver(&lm83_driver);
492}
493
494static void __exit sm_lm83_exit(void)
495{
496        i2c_del_driver(&lm83_driver);
497}
498
499MODULE_AUTHOR("Jean Delvare <khali@linux-fr.org>");
500MODULE_DESCRIPTION("LM83 sensor driver");
501MODULE_LICENSE("GPL");
502
503module_init(sm_lm83_init);
504module_exit(sm_lm83_exit);
Note: See TracBrowser for help on using the browser.