root/lm-sensors/trunk/kernel/chips/lm90.c @ 4348

Revision 4348, 27.3 KB (checked in by khali, 8 years ago)

Spelling fix: explicitly.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1/*
2 * lm90.c - Part of lm_sensors, Linux kernel modules for hardware
3 *          monitoring
4 * Copyright (C) 2003-2005  Jean Delvare <khali@linux-fr.org>
5 *
6 * Based on the lm83 driver. The LM90 is a sensor chip made by National
7 * Semiconductor. It reports up to two temperatures (its own plus up to
8 * one external one) with a 0.125 deg resolution (1 deg for local
9 * temperature) and a 3-4 deg accuracy. Complete datasheet can be
10 * obtained from National's website at:
11 *   http://www.national.com/pf/LM/LM90.html
12 *
13 * This driver also supports the LM89 and LM99, two other sensor chips
14 * made by National Semiconductor. Both have an increased remote
15 * temperature measurement accuracy (1 degree), and the LM99
16 * additionally shifts remote temperatures (measured and limits) by 16
17 * degrees, which allows for higher temperatures measurement. The
18 * driver doesn't handle it since it can be done easily in user-space.
19 * Complete datasheets can be obtained from National's website at:
20 *   http://www.national.com/pf/LM/LM89.html
21 *   http://www.national.com/pf/LM/LM99.html
22 * Note that there is no way to differentiate between both chips.
23 *
24 * This driver also supports the LM86, another sensor chip made by
25 * National Semiconductor. It is exactly similar to the LM90 except it
26 * has a higher accuracy.
27 * Complete datasheet can be obtained from National's website at:
28 *   http://www.national.com/pf/LM/LM86.html
29 *
30 * This driver also supports the ADM1032, a sensor chip made by Analog
31 * Devices. That chip is similar to the LM90, with a few differences
32 * that are not handled by this driver. Complete datasheet can be
33 * obtained from Analog's website at:
34 *   http://products.analog.com/products/info.asp?product=ADM1032
35 * Among others, it has a higher accuracy than the LM90, much like the
36 * LM86 does.
37 *
38 * This driver also supports the MAX6657, MAX6658 and MAX6659 sensor
39 * chips made by Maxim. These chips are similar to the LM86. Complete
40 * datasheet can be obtained at Maxim's website at:
41 *   http://www.maxim-ic.com/quick_view2.cfm/qv_pk/2578
42 * Note that there is no easy way to differentiate between the three
43 * variants. The extra address and features of the MAX6659 are not
44 * supported by this driver.
45 *
46 * This driver also supports the ADT7461 chip from Analog Devices but
47 * only in its "compatability mode". If an ADT7461 chip is found but
48 * is configured in non-compatible mode (where its temperature
49 * register values are decoded differently) it is ignored by this
50 * driver. Complete datasheet can be obtained from Analog's website
51 * at:
52 *   http://products.analog.com/products/info.asp?product=ADT7461
53 *
54 * Since the LM90 was the first chipset supported by this driver, most
55 * comments will refer to this chipset, but are actually general and
56 * concern all supported chipsets, unless mentioned otherwise.
57 *
58 * This program is free software; you can redistribute it and/or modify
59 * it under the terms of the GNU General Public License as published by
60 * the Free Software Foundation; either version 2 of the License, or
61 * (at your option) any later version.
62 *
63 * This program is distributed in the hope that it will be useful,
64 * but WITHOUT ANY WARRANTY; without even the implied warranty of
65 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
66 * GNU General Public License for more details.
67 *
68 * You should have received a copy of the GNU General Public License
69 * along with this program; if not, write to the Free Software
70 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
71 */
72
73#include <linux/module.h>
74#include <linux/slab.h>
75#include <linux/i2c.h>
76#include <linux/i2c-proc.h>
77#include <linux/init.h>
78#include "version.h"
79
80#ifndef I2C_DRIVERID_LM90
81#define I2C_DRIVERID_LM90       1042
82#endif
83
84/*
85 * Addresses to scan
86 * Address is fully defined internally and cannot be changed except for
87 * MAX6659.
88 * LM86, LM89, LM90, LM99, ADM1032, ADM1032-1, ADT7461, MAX6657 and MAX6658
89 * have address 0x4c.
90 * ADM1032-2, ADT7461-2, LM89-1, and LM99-1 have address 0x4d.
91 * MAX6659 can have address 0x4c, 0x4d or 0x4e (unsupported).
92 */
93
94static unsigned short normal_i2c[] = { 0x4c, 0x4d, SENSORS_I2C_END };
95static unsigned short normal_i2c_range[] = { SENSORS_I2C_END };
96static unsigned int normal_isa[] = { SENSORS_ISA_END };
97static unsigned int normal_isa_range[] = { SENSORS_ISA_END };
98
99/*
100 * Insmod parameters
101 */
102
103SENSORS_INSMOD_6(lm90, adm1032, lm99, lm86, max6657, adt7461);
104
105/*
106 * The LM90 registers
107 */
108
109#define LM90_REG_R_MAN_ID        0xFE
110#define LM90_REG_R_CHIP_ID       0xFF
111#define LM90_REG_R_CONFIG1       0x03
112#define LM90_REG_W_CONFIG1       0x09
113#define LM90_REG_R_CONFIG2       0xBF
114#define LM90_REG_W_CONFIG2       0xBF
115#define LM90_REG_R_CONVRATE      0x04
116#define LM90_REG_W_CONVRATE      0x0A
117#define LM90_REG_R_STATUS        0x02
118#define LM90_REG_R_LOCAL_TEMP    0x00
119#define LM90_REG_R_LOCAL_HIGH    0x05
120#define LM90_REG_W_LOCAL_HIGH    0x0B
121#define LM90_REG_R_LOCAL_LOW     0x06
122#define LM90_REG_W_LOCAL_LOW     0x0C
123#define LM90_REG_R_LOCAL_CRIT    0x20
124#define LM90_REG_W_LOCAL_CRIT    0x20
125#define LM90_REG_R_REMOTE_TEMPH  0x01
126#define LM90_REG_R_REMOTE_TEMPL  0x10
127#define LM90_REG_R_REMOTE_OFFSH  0x11
128#define LM90_REG_W_REMOTE_OFFSH  0x11
129#define LM90_REG_R_REMOTE_OFFSL  0x12
130#define LM90_REG_W_REMOTE_OFFSL  0x12
131#define LM90_REG_R_REMOTE_HIGHH  0x07
132#define LM90_REG_W_REMOTE_HIGHH  0x0D
133#define LM90_REG_R_REMOTE_HIGHL  0x13
134#define LM90_REG_W_REMOTE_HIGHL  0x13
135#define LM90_REG_R_REMOTE_LOWH   0x08
136#define LM90_REG_W_REMOTE_LOWH   0x0E
137#define LM90_REG_R_REMOTE_LOWL   0x14
138#define LM90_REG_W_REMOTE_LOWL   0x14
139#define LM90_REG_R_REMOTE_CRIT   0x19
140#define LM90_REG_W_REMOTE_CRIT   0x19
141#define LM90_REG_R_TCRIT_HYST    0x21
142#define LM90_REG_W_TCRIT_HYST    0x21
143
144/*
145 * Conversions and various macros
146 * For local temperatures and limits, critical limits and the hysteresis
147 * value, the LM90 uses signed 8-bit values with LSB = 1 degree Celsius.
148 * For remote temperatures and limits, it uses signed 11-bit values with
149 * LSB = 0.125 degree Celsius, left-justified in 16-bit registers.
150 */
151
152#define TEMP1_FROM_REG(val)     (val)
153#define TEMP1_TO_REG(val)       ((val) <= -128 ? -128 : \
154                                 (val) >= 127 ? 127 : (val))
155#define TEMP2_FROM_REG(val)     ((val) / 32 * 125 / 100)
156#define TEMP2_TO_REG(val)       ((val) <= -1280 ? 0x8000 : \
157                                 (val) >= 1270 ? 0x7FE0 : \
158                                 ((val) * 100 / 125 * 32))
159#define HYST_TO_REG(val)        ((val) <= 0 ? 0 : \
160                                 (val) >= 31 ? 31 : (val))
161
162/*
163 * ADT7461 is almost identical to LM90 except that attempts to write
164 * values that are outside the range 0 < temp < 127 are treated as
165 * the boundary value.
166 */
167
168#define TEMP1_TO_REG_ADT7461(val) ((val) <= 0 ? 0 : \
169                                 (val) >= 127 ? 127 : (val))
170#define TEMP2_TO_REG_ADT7461(val) ((val) <= 0 ? 0 : \
171                                 (val) >= 1277 ? 0x7FC0 : \
172                                 ((val) * 100 / 250 * 64))
173
174/*
175 * Functions declaration
176 */
177
178static int lm90_attach_adapter(struct i2c_adapter *adapter);
179static int lm90_detect(struct i2c_adapter *adapter, int address,
180        unsigned short flags, int kind);
181static void lm90_init_client(struct i2c_client *client);
182static int lm90_detach_client(struct i2c_client *client);
183static void lm90_local_temp(struct i2c_client *client, int operation,
184        int ctl_name, int *nrels_mag, long *results);
185static void lm90_remote_temp(struct i2c_client *client, int operation,
186        int ctl_name, int *nrels_mag, long *results);
187static void lm90_local_tcrit(struct i2c_client *client, int operation,
188        int ctl_name, int *nrels_mag, long *results);
189static void lm90_remote_tcrit(struct i2c_client *client, int operation,
190        int ctl_name, int *nrels_mag, long *results);
191static void lm90_local_hyst(struct i2c_client *client, int operation,
192        int ctl_name, int *nrels_mag, long *results);
193static void lm90_remote_hyst(struct i2c_client *client, int operation,
194        int ctl_name, int *nrels_mag, long *results);
195static void lm90_alarms(struct i2c_client *client, int operation,
196        int ctl_name, int *nrels_mag, long *results);
197static void adm1032_pec(struct i2c_client *client, int operation,
198        int ctl_name, int *nrels_mag, long *results);
199
200/*
201 * Driver data (common to all clients)
202 */
203
204static struct i2c_driver lm90_driver = {
205        .name           = "LM90/ADM1032 sensor driver",
206        .id             = I2C_DRIVERID_LM90,
207        .flags          = I2C_DF_NOTIFY,
208        .attach_adapter = lm90_attach_adapter,
209        .detach_client  = lm90_detach_client
210};
211
212/*
213 * Client data (each client gets its own)
214 */
215
216struct lm90_data {
217        struct i2c_client client;
218        int sysctl_id;
219
220        struct semaphore update_lock;
221        char valid; /* zero until following fields are valid */
222        unsigned long last_updated; /* in jiffies */
223        int kind;
224
225        /* registers values */
226        s8 local_temp, local_high, local_low;
227        s16 remote_temp, remote_high, remote_low; /* combined */
228        s8 local_crit, remote_crit;
229        u8 hyst; /* linked to two sysctl files (hyst1 RW, hyst2 RO) */
230        u8 alarms; /* bitvector */
231};
232
233/*
234 * Proc entries
235 * These files are created for each detected LM90.
236 */
237
238/* -- SENSORS SYSCTL START -- */
239
240#define LM90_SYSCTL_LOCAL_TEMP    1200
241#define LM90_SYSCTL_REMOTE_TEMP   1201
242#define LM90_SYSCTL_LOCAL_TCRIT   1204
243#define LM90_SYSCTL_REMOTE_TCRIT  1205
244#define LM90_SYSCTL_LOCAL_HYST    1207
245#define LM90_SYSCTL_REMOTE_HYST   1208
246#define LM90_SYSCTL_ALARMS        1210
247#define LM90_SYSCTL_PEC           1214
248
249#define LM90_ALARM_LOCAL_HIGH     0x40
250#define LM90_ALARM_LOCAL_LOW      0x20
251#define LM90_ALARM_LOCAL_CRIT     0x01
252#define LM90_ALARM_REMOTE_HIGH    0x10
253#define LM90_ALARM_REMOTE_LOW     0x08
254#define LM90_ALARM_REMOTE_CRIT    0x02
255#define LM90_ALARM_REMOTE_OPEN    0x04
256
257/* -- SENSORS SYSCTL END -- */
258
259
260static ctl_table lm90_dir_table_template[] =
261{
262        {LM90_SYSCTL_LOCAL_TEMP, "temp1", NULL, 0, 0644, NULL,
263         &i2c_proc_real, &i2c_sysctl_real, NULL, &lm90_local_temp},
264        {LM90_SYSCTL_REMOTE_TEMP, "temp2", NULL, 0, 0644, NULL,
265         &i2c_proc_real, &i2c_sysctl_real, NULL, &lm90_remote_temp},
266        {LM90_SYSCTL_LOCAL_TCRIT, "tcrit1", NULL, 0, 0644, NULL,
267         &i2c_proc_real, &i2c_sysctl_real, NULL, &lm90_local_tcrit},
268        {LM90_SYSCTL_REMOTE_TCRIT, "tcrit2", NULL, 0, 0644, NULL,
269         &i2c_proc_real, &i2c_sysctl_real, NULL, &lm90_remote_tcrit},
270        {LM90_SYSCTL_LOCAL_HYST, "hyst1", NULL, 0, 0644, NULL,
271         &i2c_proc_real, &i2c_sysctl_real, NULL, &lm90_local_hyst},
272        {LM90_SYSCTL_REMOTE_HYST, "hyst2", NULL, 0, 0444, NULL,
273         &i2c_proc_real, &i2c_sysctl_real, NULL, &lm90_remote_hyst},
274        {LM90_SYSCTL_ALARMS, "alarms", NULL, 0, 0444, NULL,
275         &i2c_proc_real, &i2c_sysctl_real, NULL, &lm90_alarms},
276        {0}
277};
278
279static ctl_table adm1032_dir_table_template[] =
280{
281        {LM90_SYSCTL_LOCAL_TEMP, "temp1", NULL, 0, 0644, NULL,
282         &i2c_proc_real, &i2c_sysctl_real, NULL, &lm90_local_temp},
283        {LM90_SYSCTL_REMOTE_TEMP, "temp2", NULL, 0, 0644, NULL,
284         &i2c_proc_real, &i2c_sysctl_real, NULL, &lm90_remote_temp},
285        {LM90_SYSCTL_LOCAL_TCRIT, "tcrit1", NULL, 0, 0644, NULL,
286         &i2c_proc_real, &i2c_sysctl_real, NULL, &lm90_local_tcrit},
287        {LM90_SYSCTL_REMOTE_TCRIT, "tcrit2", NULL, 0, 0644, NULL,
288         &i2c_proc_real, &i2c_sysctl_real, NULL, &lm90_remote_tcrit},
289        {LM90_SYSCTL_LOCAL_HYST, "hyst1", NULL, 0, 0644, NULL,
290         &i2c_proc_real, &i2c_sysctl_real, NULL, &lm90_local_hyst},
291        {LM90_SYSCTL_REMOTE_HYST, "hyst2", NULL, 0, 0444, NULL,
292         &i2c_proc_real, &i2c_sysctl_real, NULL, &lm90_remote_hyst},
293        {LM90_SYSCTL_ALARMS, "alarms", NULL, 0, 0444, NULL,
294         &i2c_proc_real, &i2c_sysctl_real, NULL, &lm90_alarms},
295        {LM90_SYSCTL_PEC, "pec", NULL, 0, 0644, NULL,
296         &i2c_proc_real, &i2c_sysctl_real, NULL, &adm1032_pec},
297        {0}
298};
299
300/*
301 * Real code
302 */
303
304/* The ADM1032 supports PEC but not on write byte transactions, so we need
305   to explicitly ask for a transaction without PEC. */
306static inline s32 adm1032_write_byte(struct i2c_client *client, u8 value)
307{
308        return i2c_smbus_xfer(client->adapter, client->addr,
309                              client->flags & ~I2C_CLIENT_PEC,
310                              I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL);
311}
312
313/* It is assumed that client->update_lock is held (unless we are in
314   detection or initialization steps). This matters when PEC is enabled,
315   because we don't want the address pointer to change between the write
316   byte and the read byte transactions. */
317static int lm90_read_reg(struct i2c_client* client, u8 reg, u8 *value)
318{
319        int err;
320
321        if (client->flags & I2C_CLIENT_PEC) {
322                err = adm1032_write_byte(client, reg);
323                if (err >= 0)
324                        err = i2c_smbus_read_byte(client);
325        } else
326                err = i2c_smbus_read_byte_data(client, reg);
327
328        if (err < 0) {
329                printk(KERN_WARNING "lm90: Register 0x%02x read failed (%d)\n",
330                       reg, err);
331                return err;
332        }
333        *value = err;
334
335        return 0;
336}
337
338static int lm90_attach_adapter(struct i2c_adapter *adapter)
339{
340        return i2c_detect(adapter, &addr_data, lm90_detect);
341}
342
343/*
344 * The following function does more than just detection. If detection
345 * succeeds, it also registers the new chip.
346 */
347static int lm90_detect(struct i2c_adapter *adapter, int address,
348        unsigned short flags, int kind)
349{
350        struct i2c_client *new_client;
351        struct lm90_data *data;
352        int err = 0;
353        const char *type_name = "";
354        const char *client_name = "";
355
356        if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
357#ifdef DEBUG
358                printk(KERN_DEBUG "lm90: adapter doesn't support byte mode, "
359                       "skipping\n");
360#endif
361                return 0;
362        }
363
364        if (!(data = kmalloc(sizeof(struct lm90_data), GFP_KERNEL))) {
365                printk(KERN_ERR "lm90: Out of memory in lm90_detect\n");
366                return -ENOMEM;
367        }
368
369        /*
370         * The common I2C client data is placed right before the
371         * LM90-specific data. The LM90-specific data is pointed to by the
372         * data field from the I2C client data.
373         */
374
375        new_client = &data->client;
376        new_client->addr = address;
377        new_client->data = data;
378        new_client->adapter = adapter;
379        new_client->driver = &lm90_driver;
380        new_client->flags = 0;
381
382        /*
383         * Now we do the remaining detection. A negative kind means that
384         * the driver was loaded with no force parameter (default), so we
385         * must both detect and identify the chip. A zero kind means that
386         * the driver was loaded with the force parameter, the detection
387         * step shall be skipped. A positive kind means that the driver
388         * was loaded with the force parameter and a given kind of chip is
389         * requested, so both the detection and the identification steps
390         * are skipped.
391         */
392
393        /* Default to an LM90 if forced */
394        if (kind == 0)
395                kind = lm90;
396
397        if (kind < 0) { /* detection and identification */
398                u8 man_id, chip_id, reg_config1, reg_convrate;
399
400                if (lm90_read_reg(new_client, LM90_REG_R_MAN_ID,
401                                  &man_id) < 0
402                 || lm90_read_reg(new_client, LM90_REG_R_CHIP_ID,
403                                  &chip_id) < 0
404                 || lm90_read_reg(new_client, LM90_REG_R_CONFIG1,
405                                  &reg_config1) < 0
406                 || lm90_read_reg(new_client, LM90_REG_R_CONVRATE,
407                                  &reg_convrate) < 0)
408                        goto exit_free;
409
410                if (man_id == 0x01) { /* National Semiconductor */
411                        u8 reg_config2;
412
413                        if (lm90_read_reg(new_client, LM90_REG_R_CONFIG2,
414                                          &reg_config2) < 0)
415                                goto exit_free;
416
417                        if ((reg_config1 & 0x2A) == 0x00
418                         && (reg_config2 & 0xF8) == 0x00
419                         && reg_convrate <= 0x09) {
420                                if (address == 0x4C
421                                 && (chip_id & 0xF0) == 0x20) /* LM90 */
422                                        kind = lm90;
423                                else if ((chip_id & 0xF0) == 0x30) /* LM89/LM99 */
424                                        kind = lm99;
425                                else if (address == 0x4C
426                                 && (chip_id & 0xF0) == 0x10) /* LM86 */
427                                        kind = lm99;
428                        }
429                } else
430                if (man_id == 0x41) { /* Analog Devices */
431                        if ((chip_id & 0xF0) == 0x40 /* ADM1032 */
432                         && (reg_config1 & 0x3F) == 0x00
433                         && reg_convrate <= 0x0A)
434                                kind = adm1032;
435                        else
436                        if (chip_id == 0x51 /* ADT7461 */
437                         && (reg_config1 & 0x1F) == 0x00 /* check compat mode */
438                         && reg_convrate <= 0x0A)
439                                kind = adt7461;
440                } else
441                if (man_id == 0x4D) { /* Maxim */
442                        /*
443                         * The Maxim variants do NOT have a chip_id register.
444                         * Reading from that address will return the last read
445                         * value, which in our case is those of the man_id
446                         * register. Likewise, the config1 register seems to
447                         * lack a low nibble, so the value will be those of the
448                         * previous read, so in our case those of the man_id
449                         * register.
450                         */
451                        if (chip_id == man_id
452                         && (reg_config1 & 0x1F) == (man_id & 0x0F)
453                         && reg_convrate <= 0x09)
454                                kind = max6657;
455                }
456        }
457
458        if (kind <= 0) { /* identification failed */
459                printk(KERN_INFO "lm90: Unsupported chip\n");
460                goto exit_free;
461        }
462
463        if (kind == lm90) {
464                type_name = "lm90";
465                client_name = "LM90 chip";
466        } else if (kind == adm1032) {
467                type_name = "adm1032";
468                client_name = "ADM1032 chip";
469                /* The ADM1032 supports PEC, but only if combined
470                   transactions are not used. */
471                if (i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE)) {
472                        new_client->flags |= I2C_CLIENT_PEC;
473                        printk(KERN_DEBUG "lm90: Enabling PEC for ADM1032\n");
474                }
475        } else if (kind == lm99) {
476                type_name = "lm99";
477                client_name = "LM99 chip";
478        } else if (kind == lm86) {
479                type_name = "lm86";
480                client_name = "LM86 chip";
481        } else if (kind == max6657) {
482                type_name = "max6657";
483                client_name = "MAX6657 chip";
484        } else if (kind == adt7461) {
485                type_name = "adt7461";
486                client_name = "ADT7561 chip";
487        } else {
488                printk(KERN_ERR "lm90: Unknown kind %d\n", kind);
489                goto exit_free;
490        }
491
492        /*
493         * OK, we got a valid chip so we can fill in the remaining client
494         * fields.
495         */
496
497        strcpy(new_client->name, client_name);
498        data->valid = 0;
499        data->kind = kind;
500        init_MUTEX(&data->update_lock);
501
502        /*
503         * Tell the I2C layer a new client has arrived.
504         */
505
506        if ((err = i2c_attach_client(new_client))) {
507                printk(KERN_ERR "lm90: Failed to attach client (%d)\n", err);
508                goto exit_free;
509        }
510
511        /*
512         * Register a new directory entry.
513         */
514
515        if ((err = i2c_register_entry(new_client, type_name,
516             (new_client->flags & I2C_CLIENT_PEC) ?
517             adm1032_dir_table_template :
518             lm90_dir_table_template, THIS_MODULE)) < 0) {
519                printk(KERN_ERR "lm90: Failed to register directory entry "
520                       "(%d)\n", err);
521                goto exit_detach;
522        }
523        data->sysctl_id = err;
524
525        /*
526         * Initialize the LM90 chip.
527         */
528
529        lm90_init_client(new_client);
530        return 0;
531
532exit_detach:
533        i2c_detach_client(new_client);
534exit_free:
535        kfree(data);
536        return err;
537}
538
539static void lm90_init_client(struct i2c_client *client)
540{
541        u8 config;
542
543        /*
544         * Start the conversions.
545         */
546
547        i2c_smbus_write_byte_data(client, LM90_REG_W_CONVRATE,
548                5); /* 2 Hz */
549        if (lm90_read_reg(client, LM90_REG_R_CONFIG1, &config) < 0) {
550                printk(KERN_ERR "lm90: Initialization failed!\n");
551                return;
552        }
553        if (config & 0x40)
554                i2c_smbus_write_byte_data(client, LM90_REG_W_CONFIG1,
555                        config & 0xBF); /* run */
556}
557
558
559static int lm90_detach_client(struct i2c_client *client)
560{
561        int err;
562
563        i2c_deregister_entry(((struct lm90_data *) (client->data))->sysctl_id);
564        if ((err = i2c_detach_client(client))) {
565                printk(KERN_ERR "lm90: Client deregistration failed, client "
566                       "not detached (%d)\n", err);
567                return err;
568        }
569
570        kfree(client->data);
571        return 0;
572}
573
574static void lm90_update_client(struct i2c_client *client)
575{
576        struct lm90_data *data = client->data;
577
578        down(&data->update_lock);
579
580        if ((jiffies - data->last_updated > HZ * 2) ||
581            (jiffies < data->last_updated) || !data->valid) {
582                u8 oldh, newh, l;
583#ifdef DEBUG
584                printk(KERN_DEBUG "lm90: Updating register values\n");
585#endif
586
587                lm90_read_reg(client, LM90_REG_R_LOCAL_TEMP,
588                              &data->local_temp);
589                lm90_read_reg(client, LM90_REG_R_LOCAL_HIGH,
590                              &data->local_high);
591                lm90_read_reg(client, LM90_REG_R_LOCAL_LOW,
592                              &data->local_low);
593                lm90_read_reg(client, LM90_REG_R_LOCAL_CRIT,
594                              &data->local_crit);
595                lm90_read_reg(client, LM90_REG_R_REMOTE_CRIT,
596                              &data->remote_crit);
597                lm90_read_reg(client, LM90_REG_R_TCRIT_HYST,
598                              &data->hyst);
599
600                /*
601                 * There is a trick here. We have to read two registers to
602                 * have the remote sensor temperature, but we have to beware
603                 * a conversion could occur inbetween the readings. The
604                 * datasheet says we should either use the one-shot
605                 * conversion register, which we don't want to do (disables
606                 * hardware monitoring) or monitor the busy bit, which is
607                 * impossible (we can't read the values and monitor that bit
608                 * at the exact same time). So the solution used here is to
609                 * read the high byte once, then the low byte, then the high
610                 * byte again. If the new high byte matches the old one,
611                 * then we have a valid reading. Else we have to read the low
612                 * byte again, and now we believe we have a correct reading.
613                 */
614
615                if (lm90_read_reg(client, LM90_REG_R_REMOTE_TEMPH, &oldh) == 0
616                 && lm90_read_reg(client, LM90_REG_R_REMOTE_TEMPL, &l) == 0
617                 && lm90_read_reg(client, LM90_REG_R_REMOTE_TEMPH, &newh) == 0
618                 && (newh == oldh
619                  || lm90_read_reg(client, LM90_REG_R_REMOTE_TEMPL, &l) == 0))
620                        data->remote_temp = (newh << 8) | l;
621
622                if (lm90_read_reg(client, LM90_REG_R_REMOTE_LOWH, &newh) == 0
623                 && lm90_read_reg(client, LM90_REG_R_REMOTE_LOWL, &l) == 0)
624                        data->remote_low = (newh << 8) | l;
625                if (lm90_read_reg(client, LM90_REG_R_REMOTE_HIGHH, &newh) == 0
626                 && lm90_read_reg(client, LM90_REG_R_REMOTE_HIGHL, &l) == 0)
627                        data->remote_high = (newh << 8) | l;
628                lm90_read_reg(client, LM90_REG_R_STATUS, &data->alarms);
629
630                data->last_updated = jiffies;
631                data->valid = 1;
632        }
633
634        up(&data->update_lock);
635}
636
637static void lm90_local_temp(struct i2c_client *client, int operation,
638        int ctl_name, int *nrels_mag, long *results)
639{
640        struct lm90_data *data = client->data;
641
642        if (operation == SENSORS_PROC_REAL_INFO)
643                *nrels_mag = 0; /* magnitude */
644        else if (operation == SENSORS_PROC_REAL_READ) {
645                lm90_update_client(client);
646                results[0] = TEMP1_FROM_REG(data->local_high);
647                results[1] = TEMP1_FROM_REG(data->local_low);
648                results[2] = TEMP1_FROM_REG(data->local_temp);
649                *nrels_mag = 3;
650        } else if (operation == SENSORS_PROC_REAL_WRITE) {
651                if (*nrels_mag >= 1) {
652                        if (data->kind == adt7461)
653                                data->local_high = TEMP1_TO_REG_ADT7461(results[0]);
654                        else
655                                data->local_high = TEMP1_TO_REG(results[0]);
656                        i2c_smbus_write_byte_data(client, LM90_REG_W_LOCAL_HIGH,
657                                data->local_high);
658                }
659                if (*nrels_mag >= 2) {
660                        if (data->kind == adt7461)
661                                data->local_low = TEMP1_TO_REG_ADT7461(results[1]);
662                        else
663                                data->local_low = TEMP1_TO_REG(results[1]);
664                        i2c_smbus_write_byte_data(client, LM90_REG_W_LOCAL_LOW,
665                                data->local_low);
666                }
667        }
668}
669
670static void lm90_remote_temp(struct i2c_client *client, int operation,
671        int ctl_name, int *nrels_mag, long *results)
672{
673        struct lm90_data *data = client->data;
674
675        if (operation == SENSORS_PROC_REAL_INFO)
676                *nrels_mag = 1; /* magnitude */
677        else if (operation == SENSORS_PROC_REAL_READ) {
678                lm90_update_client(client);
679                results[0] = TEMP2_FROM_REG(data->remote_high);
680                results[1] = TEMP2_FROM_REG(data->remote_low);
681                results[2] = TEMP2_FROM_REG(data->remote_temp);
682                *nrels_mag = 3;
683        } else if (operation == SENSORS_PROC_REAL_WRITE) {
684                if (*nrels_mag >= 1) {
685                        if (data->kind == adt7461)
686                                data->remote_high = TEMP2_TO_REG_ADT7461(results[0]);
687                        else
688                                data->remote_high = TEMP2_TO_REG(results[0]);
689                        i2c_smbus_write_byte_data(client, LM90_REG_W_REMOTE_HIGHH,
690                                data->remote_high >> 8);
691                        i2c_smbus_write_byte_data(client, LM90_REG_W_REMOTE_HIGHL,
692                                data->remote_high & 0xFF);
693                }
694                if (*nrels_mag >= 2) {
695                        if (data->kind == adt7461)
696                                data->remote_low = TEMP2_TO_REG_ADT7461(results[1]);
697                        else
698                                data->remote_low = TEMP2_TO_REG(results[1]);
699                        i2c_smbus_write_byte_data(client, LM90_REG_W_REMOTE_LOWH,
700                                data->remote_low >> 8);
701                        i2c_smbus_write_byte_data(client, LM90_REG_W_REMOTE_LOWL,
702                                data->remote_low & 0xFF);
703                }
704        }
705}
706
707static void lm90_local_tcrit(struct i2c_client *client, int operation,
708        int ctl_name, int *nrels_mag, long *results)
709{
710        struct lm90_data *data = client->data;
711
712        if (operation == SENSORS_PROC_REAL_INFO)
713                *nrels_mag = 0; /* magnitude */
714        else if (operation == SENSORS_PROC_REAL_READ) {
715                lm90_update_client(client);
716                results[0] = TEMP1_FROM_REG(data->local_crit);
717                *nrels_mag = 1;
718        } else if (operation == SENSORS_PROC_REAL_WRITE) {
719                if (*nrels_mag >= 1) {
720                        if (data->kind == adt7461)
721                                data->local_crit = TEMP1_TO_REG_ADT7461(results[0]);
722                        else
723                                data->local_crit = TEMP1_TO_REG(results[0]);
724                        i2c_smbus_write_byte_data(client, LM90_REG_W_LOCAL_CRIT,
725                                data->local_crit);
726                }
727        }
728}
729
730static void lm90_remote_tcrit(struct i2c_client *client, int operation,
731        int ctl_name, int *nrels_mag, long *results)
732{
733        struct lm90_data *data = client->data;
734
735        if (operation == SENSORS_PROC_REAL_INFO)
736                *nrels_mag = 0; /* magnitude */
737        else if (operation == SENSORS_PROC_REAL_READ) {
738                lm90_update_client(client);
739                results[0] = TEMP1_FROM_REG(data->remote_crit);
740                *nrels_mag = 1;
741        } else if (operation == SENSORS_PROC_REAL_WRITE) {
742                if (*nrels_mag >= 1) {
743                        if (data->kind == adt7461)
744                                data->remote_crit = TEMP1_TO_REG_ADT7461(results[0]);
745                        else
746                                data->remote_crit = TEMP1_TO_REG(results[0]);
747                        i2c_smbus_write_byte_data(client, LM90_REG_W_REMOTE_CRIT,
748                                data->remote_crit);
749                }
750        }
751}
752
753/*
754 * One quick note about hysteresis. Internally, the hysteresis value
755 * is held in a single register by the LM90, as a relative value.
756 * This relative value applies to both the local critical temperature
757 * and the remote critical temperature. Since all temperatures exported
758 * through procfs have to be absolute, we have to do some conversions.
759 * The solution retained here is to export two absolute values, one for
760 * each critical temperature. In order not to confuse the users too
761 * much, only one file is writable. Would we fail to do so, users
762 * would probably attempt to write to both files, as if they were
763 * independant, and since they aren't, they wouldn't understand why
764 * setting one affects the other one (and would probably claim there's
765 * a bug in the driver).
766 */
767
768static void lm90_local_hyst(struct i2c_client *client, int operation,
769        int ctl_name, int *nrels_mag, long *results)
770{
771        struct lm90_data *data = client->data;
772
773        if (operation == SENSORS_PROC_REAL_INFO)
774                *nrels_mag = 0; /* magnitude */
775        else if (operation == SENSORS_PROC_REAL_READ) {
776                lm90_update_client(client);
777                results[0] = TEMP1_FROM_REG(data->local_crit) -
778                        TEMP1_FROM_REG(data->hyst);
779                *nrels_mag = 1;
780        } else if (operation == SENSORS_PROC_REAL_WRITE) {
781                if (*nrels_mag >= 1) {
782                        data->hyst = HYST_TO_REG(data->local_crit - results[0]);
783                        i2c_smbus_write_byte_data(client, LM90_REG_W_TCRIT_HYST,
784                                data->hyst);
785                }
786        }
787}
788
789static void lm90_remote_hyst(struct i2c_client *client, int operation,
790        int ctl_name, int *nrels_mag, long *results)
791{
792        struct lm90_data *data = client->data;
793
794        if (operation == SENSORS_PROC_REAL_INFO)
795                *nrels_mag = 0; /* magnitude */
796        else if (operation == SENSORS_PROC_REAL_READ) {
797                lm90_update_client(client);
798                results[0] = TEMP1_FROM_REG(data->remote_crit) -
799                        TEMP1_FROM_REG(data->hyst);
800                *nrels_mag = 1;
801        }
802}
803
804static void lm90_alarms(struct i2c_client *client, int operation,
805        int ctl_name, int *nrels_mag, long *results)
806{
807        struct lm90_data *data = client->data;
808
809        if (operation == SENSORS_PROC_REAL_INFO)
810                *nrels_mag = 0; /* magnitude */
811        else if (operation == SENSORS_PROC_REAL_READ) {
812                lm90_update_client(client);
813                results[0] = data->alarms;
814                *nrels_mag = 1;
815        }
816}
817
818/* pec used for ADM1032 only */
819static void adm1032_pec(struct i2c_client *client, int operation,
820        int ctl_name, int *nrels_mag, long *results)
821{
822        if (operation == SENSORS_PROC_REAL_INFO)
823                *nrels_mag = 0; /* magnitude */
824        else if (operation == SENSORS_PROC_REAL_READ) {
825                results[0] = !!(client->flags & I2C_CLIENT_PEC);
826                *nrels_mag = 1;
827        } else if (operation == SENSORS_PROC_REAL_WRITE) {
828                if (*nrels_mag >= 1) {
829                        switch (results[0]) {
830                        case 0:
831                                client->flags &= ~I2C_CLIENT_PEC;
832                                break;
833                        case 1:
834                                client->flags |= I2C_CLIENT_PEC;
835                                break;
836                        }
837                }
838        }
839}
840
841static int __init sm_lm90_init(void)
842{
843        printk(KERN_INFO "lm90 driver version %s (%s)\n", LM_VERSION,
844               LM_DATE);
845        return i2c_add_driver(&lm90_driver);
846}
847
848static void __exit sm_lm90_exit(void)
849{
850        i2c_del_driver(&lm90_driver);
851}
852
853MODULE_AUTHOR("Jean Delvare <khali@linux-fr.org>");
854MODULE_DESCRIPTION("LM90/ADM1032 sensor driver");
855MODULE_LICENSE("GPL");
856
857module_init(sm_lm90_init);
858module_exit(sm_lm90_exit);
Note: See TracBrowser for help on using the browser.