root/lm-sensors/trunk/kernel/chips/lm93.c @ 2653

Revision 2653, 37.0 KB (checked in by mmh, 9 years ago)

first draft at a driver for LM93

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1/*
2    lm93.c - Part of lm_sensors, Linux kernel modules for hardware monitoring
3
4    Author/Maintainer: Mark M. Hoffman <mhoffman@lightlink.com>
5        Copyright (c) 2004 Utilitek Systems, Inc.
6
7    derived in part from lm78.c:
8        Copyright (c) 1998, 1999  Frodo Looijaard <frodol@dds.nl>
9
10    derived in part from lm85.c:
11        Copyright (c) 2002, 2003 Philip Pokorny <ppokorny@penguincomputing.com>
12        Copyright (c) 2003       Margit Schubert-While <margitsw@t-online.de>
13
14    derived in part from w83l785ts.c:
15        Copyright (c) 2003-2004 Jean Delvare <khali@linux-fr.org>
16
17    This program is free software; you can redistribute it and/or modify
18    it under the terms of the GNU General Public License as published by
19    the Free Software Foundation; either version 2 of the License, or
20    (at your option) any later version.
21
22    This program is distributed in the hope that it will be useful,
23    but WITHOUT ANY WARRANTY; without even the implied warranty of
24    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25    GNU General Public License for more details.
26
27    You should have received a copy of the GNU General Public License
28    along with this program; if not, write to the Free Software
29    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
30*/
31
32#define DEBUG 1
33
34#include <linux/module.h>
35#include <linux/slab.h>
36#include <linux/ioport.h>
37#include <linux/i2c.h>
38#include <linux/i2c-proc.h>
39#include <linux/init.h>
40#include <linux/delay.h>
41#include <asm/io.h>
42#include <asm/bitops.h>
43#include "version.h"
44#include "sensors_vid.h"
45
46#ifndef I2C_DRIVERID_LM93
47#define I2C_DRIVERID_LM93 1049
48#endif
49
50/* I2C addresses to scan */
51static unsigned short normal_i2c[] = { SENSORS_I2C_END };
52static unsigned short normal_i2c_range[] = { 0x2c, 0x2e, SENSORS_I2C_END };
53
54/* ISA addresses to scan (none) */
55static unsigned int normal_isa[] = { SENSORS_ISA_END };
56static unsigned int normal_isa_range[] = { SENSORS_ISA_END };
57
58/* module parameters */
59SENSORS_INSMOD_1(lm93);
60
61static int disable_block /* = 0 */ ;
62MODULE_PARM(disable_block, "i");
63MODULE_PARM_DESC(disable_block,
64        "Set to non-zero to disable SMBus block data transactions.");
65
66static int init = 1;
67MODULE_PARM(init, "i");
68MODULE_PARM_DESC(init, "Set to zero to bypass chip initialization");
69
70static int vccp_limit_type[2] /* = {0,0} */ ;
71MODULE_PARM(vccp_limit_type, "2-2i");
72MODULE_PARM_DESC(vccp_limit_type, "Configures in7 and in8 limit modes");
73
74/* SMBus capabilities */
75#define LM93_SMBUS_FUNC_FULL (I2C_FUNC_SMBUS_BYTE_DATA | \
76                I2C_FUNC_SMBUS_WORD_DATA | I2C_FUNC_SMBUS_BLOCK_DATA)
77#define LM93_SMBUS_FUNC_MIN  (I2C_FUNC_SMBUS_BYTE_DATA | \
78                I2C_FUNC_SMBUS_WORD_DATA)
79
80/* LM93 REGISTER ADDRESSES */
81
82/* miscellaneous */
83#define LM93_REG_MFR_ID                 0x3e
84#define LM93_REG_VER                    0x3f
85#define LM93_REG_STATUS_CONTROL         0xe2
86#define LM93_REG_CONFIG                 0xe3
87#define LM93_REG_SLEEP_CONTROL          0xe4
88
89/* voltage inputs: in1-in16 (nr => 0-15) */
90#define LM93_REG_IN(nr)                 (0x56 + (nr))
91#define LM93_REG_IN_MIN(nr)             (0x90 + (nr) * 2)
92#define LM93_REG_IN_MAX(nr)             (0x91 + (nr) * 2)
93
94/* temperature inputs: temp1-temp3 (nr => 0-2) */
95#define LM93_REG_TEMP(nr)               (0x50 + (nr))
96#define LM93_REG_TEMP_MIN(nr)           (0x78 + (nr) * 2)
97#define LM93_REG_TEMP_MAX(nr)           (0x79 + (nr) * 2)
98
99/* #PROCHOT inputs: prochot1-prochot2 (nr => 0-1) */
100#define LM93_REG_PROCHOT_CUR(nr)        (0x67 + (nr) * 2)
101#define LM93_REG_PROCHOT_AVG(nr)        (0x68 + (nr) * 2)
102#define LM93_REG_PROCHOT_MAX(nr)        (0xb0 + (nr))
103
104/* fan tach inputs: fan1-fan4 (nr => 0-3) */
105#define LM93_REG_FAN(nr)                (0x6e + (nr) * 2)
106#define LM93_REG_FAN_MIN(nr)            (0xb4 + (nr) * 2)
107
108/* pwm outputs: pwm1-pwm2 (nr => 0-1) */
109#define LM93_REG_PWM_CTL1(nr)           (0xc8 + (nr) * 4)
110#define LM93_REG_PWM_CTL2(nr)           (0xc9 + (nr) * 4)
111#define LM93_REG_PWM_CTL3(nr)           (0xca + (nr) * 4)
112#define LM93_REG_PWM_CTL4(nr)           (0xcb + (nr) * 4)
113
114/* vid inputs: vid1-vid2 (nr => 0-1) */
115#define LM93_REG_VID(nr)                (0x6c + (nr))
116
117/* vccp1 & vccp2: VID relative inputs (nr => 0-1) */
118#define LM93_REG_VCCP_LIMIT_OFF(nr)     (0xb2 + (nr))
119
120/* miscellaneous */
121#define LM93_REG_SFC1           0xbc
122#define LM93_REG_SFC2           0xbd
123#define LM93_REG_SF_TACH_TO_PWM 0xe0
124
125/* LM93 REGISTER VALUES */
126#define LM93_MFR_ID             0x73
127#define LM93_MFR_ID_PROTOTYPE   0x72
128
129/* LM93 BLOCK READ COMMANDS */
130static const struct { u8 cmd; u8 len; } lm93_block_read_cmds[12] = {
131        { 0xf2,  8 },
132        { 0xf3,  8 },
133        { 0xf4,  6 },
134        { 0xf5, 16 },
135        { 0xf6,  4 },
136        { 0xf7,  8 },
137        { 0xf8, 12 },
138        { 0xf9, 32 },
139        { 0xfa,  8 },
140        { 0xfb,  8 },
141        { 0xfc, 16 },
142        { 0xfd,  9 },
143};
144
145/* CONVERSIONS */
146
147/* fan tach register to/from tach values */
148static const u8 lm93_tach_reg_to_value[16] =
149        { 0x00, 0x40, 0x50, 0x60, 0x70, 0x80, 0x90, 0xa0,
150          0xb0, 0xc0, 0xd0, 0xe0, 0xf0, 0xff, 0x00, 0x00 };
151
152/* min, max, and nominal voltage readings, per channel (mV)*/
153static const unsigned long lm93_vin_val_min[16] = {
154        0, 0, 0, 0, 0, 0, 0, 0,
155        0, 0, 0, 0, 0, 0, 0, 3000,
156};
157static const unsigned long lm93_vin_val_max[16] = {
158        1236, 1236, 1236, 1600, 2000, 2000, 1600, 1600,
159        4400, 6667, 3333, 2625, 1312, 1312, 1236, 3600,
160};
161/*
162static const unsigned long lm93_vin_val_nom[16] = {
163         927,  927,  927, 1200, 1500, 1500, 1200, 1200,
164        3300, 5000, 2500, 1969,  984,  984,  309, 3300,
165};
166*/
167
168/* min, max, and nominal register values, per channel (u8) */
169static const u8 lm93_vin_reg_min[16] = {
170        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
171        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xae, 
172};
173static const u8 lm93_vin_reg_max[16] = {
174        0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 
175        0xff, 0xfa, 0xff, 0xff, 0xff, 0xff, 0xff, 0xd1, 
176};
177/*
178static const u8 lm93_vin_reg_nom[16] = {
179        0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0,
180        0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0x40, 0xc0,
181};
182*/
183
184/* IN: 1/100 V, limits determined by channel nr
185   REG: scaling determined by channel nr */
186static u8 LM93_IN_TO_REG(int nr, unsigned val)
187{
188        /* range limit */
189        const long mV = SENSORS_LIMIT(val * 10, 
190                lm93_vin_val_min[nr], lm93_vin_val_max[nr]);
191
192        /* try not to lose too much precision here */
193        const long uV = mV * 1000;
194        const long uV_max = lm93_vin_val_max[nr] * 1000;
195        const long uV_min = lm93_vin_val_min[nr] * 1000;
196
197        /* convert */
198        const long slope = (uV_max - uV_min) / 
199                (lm93_vin_reg_max[nr] - lm93_vin_reg_min[nr]);
200        const long intercept = uV_min - slope * lm93_vin_reg_min[nr];
201
202        u8 result = ((uV - intercept + (slope/2)) / slope);
203        result = SENSORS_LIMIT(result, 
204                        lm93_vin_reg_min[nr], lm93_vin_reg_max[nr]);
205        return result;
206}
207
208static unsigned LM93_IN_FROM_REG(int nr, u8 reg)
209{
210        const long uV_max = lm93_vin_val_max[nr] * 1000;
211        const long uV_min = lm93_vin_val_min[nr] * 1000;
212
213        const long slope = (uV_max - uV_min) /
214                (lm93_vin_reg_max[nr] - lm93_vin_reg_min[nr]);
215        const long intercept = uV_min - slope * lm93_vin_reg_min[nr];
216
217        return (slope * reg + intercept + 5000) / 10000;
218}
219
220/* vid in mV , upper == 0 indicates low limit, otherwise upper limit
221   upper also determines which nibble of the register is returned
222   (the other nibble will be 0x0) */
223static u8 LM93_IN_REL_TO_REG(unsigned val, int upper, int vid)
224{
225        long uV_offset = vid * 1000 - val * 10000;
226        if (upper) {
227                uV_offset = SENSORS_LIMIT(uV_offset, 12500, 200000);
228                return (u8)((uV_offset /  12500 - 1) << 4);
229        } else {
230                uV_offset = SENSORS_LIMIT(uV_offset, -400000, -25000);
231                return (u8)((uV_offset / -25000 - 1) << 0);
232        }
233}
234       
235/* vid in mV, upper == 0 indicates low limit, otherwise upper limit */
236static unsigned LM93_IN_REL_FROM_REG(u8 reg, int upper, int vid)
237{
238        const long uV_offset = upper ? (((reg >> 4 & 0x0f) + 1) * 12500) :
239                                (((reg >> 0 & 0x0f) + 1) * -25000);
240        const long uV_vid = vid * 1000;
241        return (uV_vid + uV_offset + 5000) / 10000;
242}
243
244#define LM93_TEMP_MIN (-1280)
245#define LM93_TEMP_MAX ( 1270)
246
247/* TEMP: 1/10 degrees C (-128C to +127C)
248   REG: 1C/bit, two's complement */
249static u8 LM93_TEMP_TO_REG(int temp)
250{
251        int ntemp = SENSORS_LIMIT(temp, LM93_TEMP_MIN, LM93_TEMP_MAX);
252        ntemp += (ntemp<0 ? -5 : 5);
253        return (u8)(ntemp / 10);
254}
255
256static int LM93_TEMP_FROM_REG(u8 reg)
257{
258        return (s8)reg * 10;
259}
260
261/* RPM: (82.5 to 1350000)
262   REG: 14-bits, LE, *left* justified */
263static u16 LM93_FAN_TO_REG(long rpm)
264{
265        u16 count, regs;
266
267        if (rpm == 0) {
268                count = 0x3fff;
269        } else {
270                rpm = SENSORS_LIMIT(rpm, 1, 1000000);
271                count = SENSORS_LIMIT((1350000 + rpm) / rpm, 1, 0x3ffe);
272        }
273
274        /* <TODO> is this byte-order correct? */
275        regs = count << 2;
276        return cpu_to_le16(regs);
277}
278
279static int LM93_FAN_FROM_REG(u16 regs)
280{
281        /* <TODO> is this byte-order correct? */
282        const u16 count = le16_to_cpu(regs) >> 2;
283
284        /* <TODO> do we need further divider of 2 here? */
285        return count==0 ? -1 : count==0x3fff ? 0: 1350000 / count;
286}
287
288/* VID: mV
289   REG: 6-bits, right justified, *always* using Intel VRM/VRD 10 */
290static int LM93_VID_FROM_REG(u8 reg)
291{
292        return vid_from_reg((reg & 0x3f), 100);
293}
294
295/* PROCHOT: 0-255, 0 => 0%, 255 => > 96.6%
296 * REG: (same) */
297static u8 LM93_PROCHOT_TO_REG(long prochot)
298{
299        prochot = SENSORS_LIMIT(prochot, 0, 255);
300        return (u8)prochot;
301}
302
303/* <TODO> add conversion routines here */
304
305/* For each registered client, we need to keep some data in memory. That
306   data is pointed to by client->data. The structure itself is dynamically
307   allocated, at the same time the client itself is allocated. */
308
309struct lm93_data {
310        struct i2c_client client;
311        struct semaphore lock;
312        int sysctl_id;
313        enum chips type;
314
315        struct semaphore update_lock;
316        unsigned long last_updated;     /* In jiffies */
317
318        /* client update function */
319        void (*update)(struct lm93_data *, struct i2c_client *);
320
321        char valid; /* !=0 if following fields are valid */
322
323        /* register values, arranged by block read groups */
324        struct {
325                u8 host_status_1;
326                u8 host_status_2;
327                u8 host_status_3;
328                u8 host_status_4;
329                u8 p1_prochot_status;
330                u8 p2_prochot_status;
331                u8 gpi_status;
332                u8 fan_status;
333        } block1;
334
335        /* temp1 - temp4: unfiltered readings
336           temp1 - temp2: filtered readings */
337        u8 block2[6];
338
339        /* vin1 - vin16: readings */
340        u8 block3[16];
341
342        /* prochot1 - prochot2: readings */
343        struct { u8 cur; u8 avg; } block4[2];
344
345        /* fan counts 1-4 => 14-bits, LE, *left* justified */
346        u16 block5[4];
347
348        /* block6 has a lot of data we don't need */
349        struct { u8 min; u8 max; } temp_lim[3];
350
351        /* vin1 - vin16: low and high limits */
352        struct { u8 min; u8 max; } block7[16];
353
354        /* fan count limits 1-4 => same format as block5 */
355        u16 block8[4];
356
357        /* more register values */
358
359        /* master config register */
360        u8 config;
361
362        /* VID1 & VID2 => register format, 6-bits, right justified */
363        u8 vid[2];
364
365        /* prochot1 - prochot2: limits */
366        u8 prochot_max[2];
367
368        /* vccp1 & vccp2 (in7 & in8): VID relative limits (register format) */
369        u8 vccp_limits[2];
370
371        /* miscellaneous setup regs */
372        u8 sfc1;
373        u8 sfc2;
374        u8 sf_tach_to_pwm;
375
376        /* <TODO> add members here */
377};
378
379
380static int lm93_attach_adapter(struct i2c_adapter *adapter);
381static int lm93_detect(struct i2c_adapter *adapter, int address,
382                       unsigned short flags, int kind);
383static int lm93_detach_client(struct i2c_client *client);
384
385static u8 lm93_read_byte(struct i2c_client *client, u8 register);
386static int lm93_write_byte(struct i2c_client *client, u8 register, u8 value);
387static u16 lm93_read_word(struct i2c_client *client, u8 register);
388static int lm93_write_word(struct i2c_client *client, u8 register, u16 value);
389static void lm93_update_client(struct i2c_client *client);
390static void lm93_update_client_full(struct lm93_data *data,
391                struct i2c_client *client);
392static void lm93_update_client_min (struct lm93_data *data,
393                struct i2c_client *client);
394
395static void lm93_init_client(struct i2c_client *client);
396
397
398static void lm93_in(struct i2c_client *client, int operation, int ctl_name,
399                    int *nrels_mag, long *results);
400static void lm93_temp(struct i2c_client *client, int operation,
401                      int ctl_name, int *nrels_mag, long *results);
402static void lm93_fan(struct i2c_client *client, int operation,
403                     int ctl_name, int *nrels_mag, long *results);
404#if 0
405static void lm93_pwm(struct i2c_client *client, int operation,
406                      int ctl_name, int *nrels_mag, long *results);
407#endif
408static void lm93_fan_smart_tach(struct i2c_client *client, int operation,
409                     int ctl_name, int *nrels_mag, long *results);
410static void lm93_vid(struct i2c_client *client, int operation,
411                     int ctl_name, int *nrels_mag, long *results);
412static void lm93_prochot(struct i2c_client *client, int operation,
413                     int ctl_name, int *nrels_mag, long *results);
414static void lm93_prochot_short(struct i2c_client *client, int operation,
415                     int ctl_name, int *nrels_mag, long *results);
416#if 0
417static void lm93_alarms(struct i2c_client *client, int operation,
418                        int ctl_name, int *nrels_mag, long *results);
419#endif
420
421static struct i2c_driver lm93_driver = {
422        .owner          = THIS_MODULE,
423        .name           = "LM93 sensor driver",
424        .id             = I2C_DRIVERID_LM93,
425        .flags          = I2C_DF_NOTIFY,
426        .attach_adapter = lm93_attach_adapter,
427        .detach_client  = lm93_detach_client,
428};
429
430/* unique ID for each LM93 detected */
431static int lm93_id = 0;
432
433/* -- SENSORS SYSCTL START -- */
434/* volts * 100 */
435#define LM93_SYSCTL_IN1         1001
436#define LM93_SYSCTL_IN2         1002
437#define LM93_SYSCTL_IN3         1003
438#define LM93_SYSCTL_IN4         1004
439#define LM93_SYSCTL_IN5         1005
440#define LM93_SYSCTL_IN6         1006
441#define LM93_SYSCTL_IN7         1007
442#define LM93_SYSCTL_IN8         1008
443#define LM93_SYSCTL_IN9         1009
444#define LM93_SYSCTL_IN10        1010
445#define LM93_SYSCTL_IN11        1011
446#define LM93_SYSCTL_IN12        1012
447#define LM93_SYSCTL_IN13        1013
448#define LM93_SYSCTL_IN14        1014
449#define LM93_SYSCTL_IN15        1015
450#define LM93_SYSCTL_IN16        1016
451
452/* degrees celcius * 10 */
453#define LM93_SYSCTL_TEMP1       1101
454#define LM93_SYSCTL_TEMP2       1102
455#define LM93_SYSCTL_TEMP3       1103
456
457/* rotations/minute */
458#define LM93_SYSCTL_FAN1        1201
459#define LM93_SYSCTL_FAN2        1202
460#define LM93_SYSCTL_FAN3        1203
461#define LM93_SYSCTL_FAN4        1204
462
463/* 1-2 => enable smart tach mode associated with this pwm #, or disable */
464#define LM93_SYSCTL_FAN1_SMART_TACH     1205
465#define LM93_SYSCTL_FAN2_SMART_TACH     1205
466#define LM93_SYSCTL_FAN3_SMART_TACH     1207
467#define LM93_SYSCTL_FAN4_SMART_TACH     1208
468
469/* volts * 1000 */
470#define LM93_SYSCTL_VID1        1301
471#define LM93_SYSCTL_VID2        1302
472
473/* 0 => off, 255 => 100% */
474#define LM93_SYSCTL_PWM1        1401
475#define LM93_SYSCTL_PWM2        1402
476
477/* 0 => 0%, 255 => > 99.6% */
478#define LM93_SYSCTL_PROCHOT1    1501
479#define LM93_SYSCTL_PROCHOT2    1502
480
481/* !0 => enable #PROCHOT logical short */
482#define LM93_SYSCTL_PROCHOT_SHORT 1503
483
484/* bitmask of alarms */
485#define LM93_SYSCTL_ALARMS      2001    /* bitvector */
486
487/*
488   <TODO> alarm bitmask definitions
489
490   This is what would happen if you treated the entire 8 bytes of host
491   error status registers as a single big-endian integer.  Trouble is,
492   the handler i2c_proc_real() only does 32-bit values.  What to do?
493*/
494#define LM93_ALARM_FAN1         0x0000000000000001ull
495#define LM93_ALARM_FAN2         0x0000000000000002ull
496#define LM93_ALARM_FAN3         0x0000000000000004ull
497#define LM93_ALARM_FAN4         0x0000000000000008ull
498#define LM93_ALARM_PH2_ERR      0x0000000000800000ull
499#define LM93_ALARM_PH1_ERR      0x0000000080000000ull
500#define LM93_ALARM_SCSI1_ERR    0x0000000400000000ull
501#define LM93_ALARM_SCSI2_ERR    0x0000000800000000ull
502#define LM93_ALARM_DVDDP1_ERR   0x0000001000000000ull
503#define LM93_ALARM_DVDDP2_ERR   0x0000002000000000ull
504#define LM93_ALARM_D1_ERR       0x0000004000000000ull
505#define LM93_ALARM_D2_ERR       0x0000008000000000ull
506#define LM93_ALARM_IN1          0x0000010000000000ull
507#define LM93_ALARM_IN2          0x0000020000000000ull
508#define LM93_ALARM_IN3          0x0000040000000000ull
509#define LM93_ALARM_IN4          0x0000080000000000ull
510#define LM93_ALARM_IN5          0x0000100000000000ull
511#define LM93_ALARM_IN6          0x0000200000000000ull
512#define LM93_ALARM_IN7          0x0000400000000000ull
513#define LM93_ALARM_IN8          0x0000800000000000ull
514#define LM93_ALARM_IN9          0x0001000000000000ull
515#define LM93_ALARM_IN10         0x0002000000000000ull
516#define LM93_ALARM_IN11         0x0004000000000000ull
517#define LM93_ALARM_IN12         0x0008000000000000ull
518#define LM93_ALARM_IN13         0x0010000000000000ull
519#define LM93_ALARM_IN14         0x0020000000000000ull
520#define LM93_ALARM_IN15         0x0040000000000000ull
521#define LM93_ALARM_IN16         0x0080000000000000ull
522#define LM93_ALARM_TEMP1        0x0100000000000000ull
523#define LM93_ALARM_TEMP2        0x0200000000000000ull
524#define LM93_ALARM_TEMP3        0x0400000000000000ull
525#define LM93_ALARM_VRD1_ERR     0x1000000000000000ull
526#define LM93_ALARM_VRD2_ERR     0x2000000000000000ull
527
528
529/* -- SENSORS SYSCTL END -- */
530
531/* These files are created for each detected LM93. This is just a template;
532   though at first sight, you might think we could use a statically
533   allocated list, we need some way to get back to the parent - which
534   is done through one of the 'extra' fields which are initialized
535   when a new copy is allocated. */
536
537#define LM93_SYSCTL_IN(nr)   {LM93_SYSCTL_IN##nr, "in" #nr, NULL, 0, \
538        0644, NULL, &i2c_proc_real, &i2c_sysctl_real, NULL, &lm93_in}
539#define LM93_SYSCTL_TEMP(nr) {LM93_SYSCTL_TEMP##nr, "temp" #nr, NULL, 0, \
540        0644, NULL, &i2c_proc_real, &i2c_sysctl_real, NULL, &lm93_temp}
541#define LM93_SYSCTL_PWM(nr)  {LM93_SYSCTL_PWM##nr, "pwm" #nr, NULL, 0, \
542        0644, NULL, &i2c_proc_real, &i2c_sysctl_real, NULL, &lm93_pwm}
543#define LM93_SYSCTL_FAN(nr)  {LM93_SYSCTL_FAN##nr, "fan" #nr, NULL, 0, \
544        0644, NULL, &i2c_proc_real, &i2c_sysctl_real, NULL, &lm93_fan}
545#define LM93_SYSCTL_VID(nr)  {LM93_SYSCTL_VID##nr, "vid" #nr, NULL, 0, \
546        0444, NULL, &i2c_proc_real, &i2c_sysctl_real, NULL, &lm93_vid}
547#define LM93_SYSCTL_PROCHOT(nr) {LM93_SYSCTL_PROCHOT##nr, "prochot" #nr, NULL, \
548        0, 0644, NULL, &i2c_proc_real, &i2c_sysctl_real, NULL, &lm93_prochot}
549#define LM93_SYSCTL_FAN_SMART_TACH(nr) {LM93_SYSCTL_FAN##nr##_SMART_TACH, \
550        "fan" #nr "_smart_tach", NULL, 0, 0644, NULL, &i2c_proc_real, \
551        &i2c_sysctl_real, NULL, &lm93_fan_smart_tach}
552static ctl_table lm93_dir_table_template[] = {
553        LM93_SYSCTL_IN(1),
554        LM93_SYSCTL_IN(2),
555        LM93_SYSCTL_IN(3),
556        LM93_SYSCTL_IN(4),
557        LM93_SYSCTL_IN(5),
558        LM93_SYSCTL_IN(6),
559        LM93_SYSCTL_IN(7),
560        LM93_SYSCTL_IN(8),
561        LM93_SYSCTL_IN(9),
562        LM93_SYSCTL_IN(10),
563        LM93_SYSCTL_IN(11),
564        LM93_SYSCTL_IN(12),
565        LM93_SYSCTL_IN(13),
566        LM93_SYSCTL_IN(14),
567        LM93_SYSCTL_IN(15),
568        LM93_SYSCTL_IN(16),
569
570        LM93_SYSCTL_TEMP(1),
571        LM93_SYSCTL_TEMP(2),
572        LM93_SYSCTL_TEMP(3),
573
574        LM93_SYSCTL_FAN(1),
575        LM93_SYSCTL_FAN(2),
576        LM93_SYSCTL_FAN(3),
577        LM93_SYSCTL_FAN(4),
578
579        LM93_SYSCTL_FAN_SMART_TACH(1),
580        LM93_SYSCTL_FAN_SMART_TACH(2),
581        LM93_SYSCTL_FAN_SMART_TACH(3),
582        LM93_SYSCTL_FAN_SMART_TACH(4),
583
584#if 0
585        LM93_SYSCTL_PWM(1),
586        LM93_SYSCTL_PWM(2),
587#endif
588
589        LM93_SYSCTL_VID(1),
590        LM93_SYSCTL_VID(2),
591
592        LM93_SYSCTL_PROCHOT(1),
593        LM93_SYSCTL_PROCHOT(2),
594
595        {LM93_SYSCTL_PROCHOT_SHORT, "prochot_short", NULL, 0, 0644, NULL,
596         &i2c_proc_real, &i2c_sysctl_real, NULL, &lm93_prochot_short},
597#if 0
598        {LM93_SYSCTL_ALARMS, "alarms", NULL, 0, 0444, NULL, &i2c_proc_real,
599         &i2c_sysctl_real, NULL, &lm93_alarms},
600#endif
601
602        {0}
603};
604
605
606/* This function is called when:
607     * lm93_driver is inserted (when this module is loaded), for each
608       available adapter
609     * when a new adapter is inserted (and lm93_driver is still present) */
610static int lm93_attach_adapter(struct i2c_adapter *adapter)
611{
612        return i2c_detect(adapter, &addr_data, lm93_detect);
613}
614
615/* This function is called by i2c_detect */
616int lm93_detect(struct i2c_adapter *adapter, int address,
617                unsigned short flags, int kind)
618{
619        int err, func;
620        struct lm93_data *data;
621        struct i2c_client *client;
622        void (*update)(struct lm93_data *, struct i2c_client *);
623
624        /* lm93 is SMBus only */
625        if (i2c_is_isa_adapter(adapter)) {
626                pr_debug("lm93.o: detect failed, "
627                                "cannot attach to legacy adapter!\n");
628                err = -ENODEV;
629                goto ERROR0;
630        }
631
632        /* choose update routine based on bus capabilities */
633        func = i2c_get_functionality(adapter);
634
635        if ( ((LM93_SMBUS_FUNC_FULL & func) == LM93_SMBUS_FUNC_FULL) &&
636                        (!disable_block) ) {
637                pr_debug("lm93.o: using SMBus block data transactions\n");
638                update = lm93_update_client_full;
639        } else if ((LM93_SMBUS_FUNC_MIN & func) == LM93_SMBUS_FUNC_MIN) {
640                pr_debug("lm93.o: disabled SMBus block data transactions\n");
641                update = lm93_update_client_min;
642        } else {
643                pr_debug("lm93.o: detect failed, "
644                                "smbus byte and/or word data not supported!\n");
645                err = -ENODEV;
646                goto ERROR0;
647        }
648
649        /* OK. For now, we presume we have a valid client. We now create the
650           client structure, even though we cannot fill it completely yet.
651           But it allows us to access lm78_{read,write}_value. */
652
653        if (!(data = kmalloc(sizeof(struct lm93_data), GFP_KERNEL))) {
654                pr_debug("lm93.o: detect failed, kmalloc failed!\n");
655                err = -ENOMEM;
656                goto ERROR0;
657        }
658
659        client = &data->client;
660        client->addr = address;
661        init_MUTEX(&data->lock);
662        client->data = data;
663        client->adapter = adapter;
664        client->driver = &lm93_driver;
665        client->flags = 0;
666
667        /* detection */
668        if (kind < 0) {
669                int mfr = lm93_read_byte(client, LM93_REG_MFR_ID);
670
671                if (mfr != 0x01) {
672                        pr_debug("lm93.o: detect failed, "
673                                "bad manufacturer id 0x%02x!\n", mfr);
674                        err = -ENODEV;
675                        goto ERROR1;
676                }
677        }
678
679        if (kind <= 0) {
680                int ver = lm93_read_byte(client, LM93_REG_VER);
681
682                if ((ver == LM93_MFR_ID) || (ver == LM93_MFR_ID_PROTOTYPE)) {
683                        kind = lm93;
684                } else {
685                        pr_debug("lm93.o: detect failed, "
686                                "bad version id 0x%02x!\n", ver);
687                        if (kind == 0)
688                                pr_debug("lm93.o: "
689                                        "(ignored 'force' parameter)\n");
690                        err = -ENODEV;
691                        goto ERROR1;
692                }
693        }
694
695        /* fill in remaining client fields */
696        strcpy(client->name, "LM93 chip");
697        client->id = lm93_id++;
698        pr_debug("lm93.o: assigning ID %d to %s at %d,0x%02x\n", client->id,
699                client->name, i2c_adapter_id(client->adapter), client->addr);
700
701        /* housekeeping */
702        data->type = kind;
703        data->valid = 0;
704        data->update = update;
705        init_MUTEX(&data->update_lock);
706
707        /* tell the I2C layer a new client has arrived */
708        if ((err = i2c_attach_client(client)))
709                goto ERROR1;
710
711        /* initialize the chip */
712        lm93_init_client(client);
713
714        /* register a new directory entry with module sensors */
715        if ((data->sysctl_id = i2c_register_entry(client, "lm93", 
716                        lm93_dir_table_template)) < 0) {
717                err = data->sysctl_id;
718                goto ERROR2;
719        }
720
721        return 0;
722
723ERROR2:
724        i2c_detach_client(client);
725ERROR1:
726        kfree(data);
727ERROR0:
728        return err;
729}
730
731static int lm93_detach_client(struct i2c_client *client)
732{
733        int err;
734        struct lm93_data *data = client->data;
735
736        i2c_deregister_entry(data->sysctl_id);
737
738        if ((err = i2c_detach_client(client))) {
739                printk (KERN_ERR "lm93.o: Client deregistration failed; "
740                        "client not detached.\n");
741                return err;
742        }
743
744        return 0;
745}
746
747#define MAX_RETRIES 5
748
749static u8 lm93_read_byte(struct i2c_client *client, u8 reg)
750{
751        int value, i;
752
753        /* retry in case of read errors */
754        for (i=1; i<=MAX_RETRIES; i++) {
755                if ((value = i2c_smbus_read_byte_data(client, reg)) >= 0) {
756                        return value;
757                } else {
758                        printk(KERN_WARNING "lm93.o: read byte data failed, "
759                                "address 0x%02x.\n", reg);
760                        mdelay(i);
761                }
762
763        }
764
765        /* <TODO> what to return in case of error? */
766        return 0;
767}
768
769static int lm93_write_byte(struct i2c_client *client, u8 reg, u8 value)
770{
771        int result;
772
773        /* <TODO> how to handle write errors? */
774        result = i2c_smbus_write_byte_data(client, reg, value);
775
776        if (result < 0)
777                printk(KERN_WARNING "lm93.o: write byte data failed, "
778                        "0x%02x at address 0x%02x.\n", value, reg);
779
780        return result;
781}
782
783static u16 lm93_read_word(struct i2c_client *client, u8 reg)
784{
785        int value, i;
786
787        /* retry in case of read errors */
788        for (i=1; i<=MAX_RETRIES; i++) {
789                if ((value = i2c_smbus_read_word_data(client, reg)) >= 0) {
790                        return value;
791                } else {
792                        printk(KERN_WARNING "lm93.o: read word data failed, "
793                                "address 0x%02x.\n", reg);
794                        mdelay(i);
795                }
796
797        }
798
799        /* <TODO> what to return in case of error? */
800        return 0;
801}
802
803static int lm93_write_word(struct i2c_client *client, u8 reg, u16 value)
804{
805        int result;
806
807        /* <TODO> how to handle write errors? */
808        result = i2c_smbus_write_word_data(client, reg, value);
809
810        if (result < 0)
811                printk(KERN_WARNING "lm93.o: write word data failed, "
812                        "0x%04x at address 0x%02x.\n", value, reg);
813
814        return result;
815}
816
817static u8 lm93_block_buffer[I2C_SMBUS_BLOCK_MAX];
818
819/*
820        read block data into values, retry if not expected length
821        fbn => index to lm93_block_read_cmds table
822                (Fixed Block Number - section 14.5.2 of LM93 datasheet)
823*/
824static void lm93_read_block(struct i2c_client *client, u8 fbn, u8 *values)
825{
826        int i, result;
827
828        for (i = 1; i <= MAX_RETRIES; i++) {
829                result = i2c_smbus_read_block_data(client, 
830                        lm93_block_read_cmds[fbn].cmd, lm93_block_buffer);
831
832                if (result != lm93_block_read_cmds[fbn].len) {
833                        printk(KERN_WARNING "lm93.o: block read data failed, "
834                                "command 0x%02x.\n", 
835                                lm93_block_read_cmds[fbn].cmd);
836                        mdelay(1);
837                }
838        }
839
840        if (result == lm93_block_read_cmds[fbn].len) {
841                memcpy(values,lm93_block_buffer,lm93_block_read_cmds[fbn].len);
842        } else {
843                /* <TODO> what to do in case of error? */
844        }
845}
846
847static void lm93_init_client(struct i2c_client *client)
848{
849        int i;
850        u8 reg = lm93_read_byte(client, LM93_REG_CONFIG);
851
852        /* start monitoring */
853        lm93_write_byte(client, LM93_REG_CONFIG, reg | 0x01);
854
855        if (init) {
856                /* enable #ALERT pin */
857                reg = lm93_read_byte(client, LM93_REG_CONFIG);
858                lm93_write_byte(client, LM93_REG_CONFIG, reg | 0x08);
859
860                /* enable ASF mode for BMC status registers */
861                reg = lm93_read_byte(client, LM93_REG_STATUS_CONTROL);
862                lm93_write_byte(client, LM93_REG_STATUS_CONTROL, reg | 0x02);
863
864                /* set sleep state to S0 */
865                lm93_write_byte(client, LM93_REG_SLEEP_CONTROL, 0);
866        }
867               
868        /* spin until ready */
869        for (i=0; i<20; i++) {
870                mdelay(10);
871                if ((lm93_read_byte(client, LM93_REG_CONFIG) & 0x80) == 0x80)   
872                        return;
873        }
874
875        printk(KERN_WARNING "lm93.o: timed out waiting for sensor "
876                        "chip to signal ready!\n");
877}
878
879static void lm93_update_client(struct i2c_client *client)
880{
881        struct lm93_data *data = client->data;
882
883        down(&data->update_lock);
884
885        if (time_after(jiffies - data->last_updated, HZ + HZ / 2) ||
886                time_before(jiffies, data->last_updated) || !data->valid) {
887
888                data->update(data, client);
889                data->last_updated = jiffies;
890                data->valid = 1;
891        }
892
893        up(&data->update_lock);
894}
895
896/* update routine for data that has no corresponding SMBus block data command */
897static void lm93_update_client_common(struct lm93_data *data,
898                struct i2c_client *client)
899{
900        int i;
901
902        /* temp1 - temp3: limits */
903        for (i = 0; i < 3; i++) {
904                data->temp_lim[i].min =
905                        lm93_read_byte(client, LM93_REG_TEMP_MIN(i));
906                data->temp_lim[i].max =
907                        lm93_read_byte(client, LM93_REG_TEMP_MAX(i));
908        }
909
910        /* config register */
911        data->config = lm93_read_byte(client, LM93_REG_CONFIG);
912
913        /* vid1 - vid2: values */
914        for (i = 0; i < 2; i++)
915                data->vid[i] = lm93_read_byte(client, LM93_REG_VID(i));
916
917        /* prochot1 - prochot2: limits */
918        for (i = 0; i < 2; i++)
919                data->prochot_max[i] = lm93_read_byte(client,
920                                LM93_REG_PROCHOT_MAX(i));
921
922        /* vccp1 - vccp2: VID relative limits */
923        for (i = 0; i < 2; i++)
924                data->vccp_limits[i] = lm93_read_byte(client,
925                                LM93_REG_VCCP_LIMIT_OFF(i));
926
927        /* misc setup registers */
928        data->sfc1 = lm93_read_byte(client, LM93_REG_SFC1);
929        data->sfc2 = lm93_read_byte(client, LM93_REG_SFC2);
930        data->sf_tach_to_pwm = lm93_read_byte(client,
931                        LM93_REG_SF_TACH_TO_PWM);
932}
933
934/* update routine which uses SMBus block data commands */
935static void lm93_update_client_full(struct lm93_data *data,
936                struct i2c_client *client)
937{
938        pr_debug("lm93.o: starting device update (block data enabled)\n");
939
940        /* in1 - in16: values & limits */
941        lm93_read_block(client, 3, (u8 *)(data->block3));
942        lm93_read_block(client, 7, (u8 *)(data->block7));
943
944        /* temp1 - temp3: values */
945        lm93_read_block(client, 2, (u8 *)(data->block2));
946
947        /* prochot1 - prochot2: values */
948        lm93_read_block(client, 4, (u8 *)(data->block4));
949
950        /* fan1 - fan4: values & limits */
951        lm93_read_block(client, 5, (u8 *)(data->block5));
952        lm93_read_block(client, 8, (u8 *)(data->block8));
953
954        /* <TODO> add code here */
955
956        lm93_update_client_common(data, client);
957}
958
959/* update routine which uses SMBus byte/word data commands only */
960static void lm93_update_client_min(struct lm93_data *data,
961                struct i2c_client *client)
962{
963        int i;
964
965        pr_debug("lm93.o: starting device update (block data disabled)\n");
966
967        /* in1 - in16: values & limits */
968        for (i = 0; i < 16; i++) {
969                data->block3[i] = 
970                        lm93_read_byte(client, LM93_REG_IN(i));
971                data->block7[i].min =
972                        lm93_read_byte(client, LM93_REG_IN_MIN(i));
973                data->block7[i].max =
974                        lm93_read_byte(client, LM93_REG_IN_MAX(i));
975        }
976
977        /* temp1 - temp3: values */
978        for (i = 0; i < 4; i++) {
979                data->block2[i] =
980                        lm93_read_byte(client, LM93_REG_TEMP(i));
981        }
982
983        /* prochot1 - prochot2: values */
984        for (i = 0; i < 2; i++) {
985                data->block4[i].cur =
986                        lm93_read_byte(client, LM93_REG_PROCHOT_CUR(i));
987                data->block4[i].avg =
988                        lm93_read_byte(client, LM93_REG_PROCHOT_AVG(i));
989        }
990
991        /* fan1 - fan4: values & limits */
992        for (i = 0; i < 4; i++) {
993                data->block5[i] =
994                        lm93_read_word(client, LM93_REG_FAN(i));
995                data->block8[i] =
996                        lm93_read_word(client, LM93_REG_FAN_MIN(i));
997        }
998       
999        /* <TODO> add code here */
1000
1001        lm93_update_client_common(data, client);
1002}
1003
1004/* The next few functions are the call-back functions of the /proc/sys and
1005   sysctl files. Which function is used is defined in the ctl_table in
1006   the extra1 field.
1007   Each function must return the magnitude (power of 10 to divide the date
1008   with) if it is called with operation==SENSORS_PROC_REAL_INFO. It must
1009   put a maximum of *nrels elements in results reflecting the data of this
1010   file, and set *nrels to the number it actually put in it, if operation==
1011   SENSORS_PROC_REAL_READ. Finally, it must get upto *nrels elements from
1012   results and write them to the chip, if operations==SENSORS_PROC_REAL_WRITE.
1013   Note that on SENSORS_PROC_REAL_READ, I do not check whether results is
1014   large enough (by checking the incoming value of *nrels). This is not very
1015   good practice, but as long as you put less than about 5 values in results,
1016   you can assume it is large enough. */
1017
1018static void lm93_in(struct i2c_client *client, int operation, int ctl_name,
1019                int *nrels_mag, long *results)
1020{
1021        struct lm93_data *data = client->data;
1022        int nr = ctl_name - LM93_SYSCTL_IN1; /* 0 <= nr <= 15 */
1023        int vccp = ctl_name - LM93_SYSCTL_IN7; /* 0 <= vccp <= 1 if relevant */
1024
1025        if (operation == SENSORS_PROC_REAL_INFO)
1026                *nrels_mag = 2;
1027        else if (operation == SENSORS_PROC_REAL_READ) {
1028
1029                lm93_update_client(client);
1030
1031                /* for limits, check in7 and in8 for VID relative mode */
1032                if ((ctl_name==LM93_SYSCTL_IN7 || ctl_name==LM93_SYSCTL_IN8) &&
1033                                (vccp_limit_type[vccp])) {
1034                        long vid = LM93_VID_FROM_REG(data->vid[vccp]);
1035                        results[0] = LM93_IN_REL_FROM_REG(
1036                                data->vccp_limits[vccp], 0, vid);
1037                        results[1] = LM93_IN_REL_FROM_REG(
1038                                data->vccp_limits[vccp], 1, vid);
1039
1040                /* otherwise, use absolute limits */
1041                } else {
1042                        results[0] = LM93_IN_FROM_REG(nr,
1043                                        data->block7[nr].min);
1044                        results[1] = LM93_IN_FROM_REG(nr,
1045                                        data->block7[nr].max);
1046                }
1047
1048                results[2] = LM93_IN_FROM_REG(nr, data->block3[nr]);
1049                *nrels_mag = 3;
1050        } else if (operation == SENSORS_PROC_REAL_WRITE) {
1051                down(&data->update_lock);
1052
1053                /* for limits, check in7 and in8 for VID relative mode */
1054                if ((ctl_name==LM93_SYSCTL_IN7 || ctl_name==LM93_SYSCTL_IN8) &&
1055                                (vccp_limit_type[vccp])) {
1056
1057                        long vid = LM93_VID_FROM_REG(data->vid[vccp]);
1058                        if (*nrels_mag >= 2) {
1059                                data->vccp_limits[vccp] =
1060                                        (data->vccp_limits[vccp] & 0x0f) |
1061                                        LM93_IN_REL_TO_REG(results[1], 1, vid);
1062                        }
1063                        if (*nrels_mag >= 1) {
1064                                data->vccp_limits[vccp] = 
1065                                        (data->vccp_limits[vccp] & 0xf0) |
1066                                        LM93_IN_REL_TO_REG(results[0], 0, vid);
1067                                lm93_write_byte(client,
1068                                                LM93_REG_VCCP_LIMIT_OFF(vccp),
1069                                                data->vccp_limits[vccp]);
1070                        }
1071
1072                /* otherwise, use absolute limits */
1073                } else {
1074                        if (*nrels_mag >= 1) {
1075                                data->block7[nr].min = LM93_IN_TO_REG(nr,
1076                                                results[0]);
1077                                lm93_write_byte(client, LM93_REG_IN_MIN(nr),
1078                                                data->block7[nr].min);
1079                        }
1080                        if (*nrels_mag >= 2) {
1081                                data->block7[nr].max = LM93_IN_TO_REG(nr,
1082                                                results[1]);
1083                                lm93_write_byte(client, LM93_REG_IN_MAX(nr),
1084                                                data->block7[nr].max);
1085                        }
1086                }
1087                up(&data->update_lock);
1088        }
1089}
1090
1091void lm93_temp(struct i2c_client *client, int operation, int ctl_name,
1092                int *nrels_mag, long *results)
1093{
1094        struct lm93_data *data = client->data;
1095        int nr = ctl_name - LM93_SYSCTL_TEMP1;
1096
1097        if (0 > nr || nr > 2)
1098                return; /* ERROR */
1099
1100        if (operation == SENSORS_PROC_REAL_INFO)
1101                *nrels_mag = 1;
1102        else if (operation == SENSORS_PROC_REAL_READ) {
1103                lm93_update_client(client);
1104                results[0] = LM93_TEMP_FROM_REG(data->temp_lim[nr].max);
1105                results[1] = LM93_TEMP_FROM_REG(data->temp_lim[nr].min);
1106                results[2] = LM93_TEMP_FROM_REG(data->block2[nr]);
1107                *nrels_mag = 3;
1108        } else if (operation == SENSORS_PROC_REAL_WRITE) {
1109                down(&data->update_lock);
1110                if (*nrels_mag >= 1) {
1111                        data->temp_lim[nr].max = LM93_TEMP_TO_REG(results[0]);
1112                        lm93_write_byte(client, LM93_REG_TEMP_MAX(nr),
1113                                         data->temp_lim[nr].max);
1114                }
1115                if (*nrels_mag >= 2) {
1116                        data->temp_lim[nr].min = LM93_TEMP_TO_REG(results[1]);
1117                        lm93_write_byte(client, LM93_REG_TEMP_MIN(nr),
1118                                         data->temp_lim[nr].min);
1119                }
1120                up(&data->update_lock);
1121        }
1122}
1123
1124void lm93_fan(struct i2c_client *client, int operation, int ctl_name,
1125                int *nrels_mag, long *results)
1126{
1127        struct lm93_data *data = client->data;
1128        int nr = ctl_name - LM93_SYSCTL_FAN1;
1129
1130        if (0 > nr || nr > 3)
1131                return; /* ERROR */
1132
1133        if (operation == SENSORS_PROC_REAL_INFO)
1134                *nrels_mag = 0;
1135        else if (operation == SENSORS_PROC_REAL_READ) {
1136                lm93_update_client(client);
1137                results[0] = LM93_FAN_FROM_REG(data->block8[nr]); /* min */
1138                results[1] = LM93_FAN_FROM_REG(data->block5[nr]); /* val */
1139                *nrels_mag = 2;
1140        } else if (operation == SENSORS_PROC_REAL_WRITE) {
1141                if (*nrels_mag >= 1) {
1142                        down(&data->update_lock);
1143                        data->block8[nr] = LM93_FAN_TO_REG(results[0]);
1144                        lm93_write_word(client, LM93_REG_FAN_MIN(nr),
1145                                        data->block8[nr]);
1146                        up(&data->update_lock);
1147                }
1148        }
1149}
1150
1151void lm93_vid(struct i2c_client *client, int operation, int ctl_name,
1152              int *nrels_mag, long *results)
1153{
1154        struct lm93_data *data = client->data;
1155        int nr = ctl_name - LM93_SYSCTL_VID1;
1156
1157        if (0 > nr || nr > 1)
1158                return; /* ERROR */
1159
1160        if (operation == SENSORS_PROC_REAL_INFO)
1161                *nrels_mag = 2;
1162        else if (operation == SENSORS_PROC_REAL_READ) {
1163                lm93_update_client(client);
1164                results[0] = LM93_VID_FROM_REG(data->vid[nr]);
1165                *nrels_mag = 1;
1166        }
1167}
1168
1169/* some tedious bit-twiddling here to deal with the register format:
1170
1171        data->sf_tach_to_pwm: (tach to pwm mapping bits)
1172
1173                bit |  7  |  6  |  5  |  4  |  3  |  2  |  1  |  0
1174                     T4:P2 T4:P1 T3:P2 T3:P1 T2:P2 T2:P1 T1:P2 T1:P1
1175
1176        data->sfc2: (enable bits)
1177
1178                bit |  3  |  2  |  1  |  0
1179                       T4    T3    T2    T1
1180*/
1181void lm93_fan_smart_tach(struct i2c_client *client, int operation, int ctl_name,
1182        int *nrels_mag, long *results)
1183{
1184        struct lm93_data *data = client->data;
1185        int nr = ctl_name - LM93_SYSCTL_FAN1_SMART_TACH;
1186
1187        if (0 > nr || nr > 3)
1188                return; /* ERROR */
1189
1190        if (operation == SENSORS_PROC_REAL_INFO)
1191                *nrels_mag = 0;
1192        else if (operation == SENSORS_PROC_REAL_READ) {
1193                lm93_update_client(client);
1194
1195                /* extract the relevant mapping */
1196                int mapping = (data->sf_tach_to_pwm >> (nr * 2)) & 0x03;
1197
1198                /* if there's a mapping and it's enabled */
1199                if (mapping && ((data->sfc2 >> nr) & 0x01))
1200                        results[0] = mapping;
1201                else
1202                        results[0] = 0;
1203
1204                *nrels_mag = 1;
1205
1206        } else if (operation == SENSORS_PROC_REAL_WRITE) {
1207                if (*nrels_mag >= 1) {
1208                        down(&data->update_lock);
1209
1210                        /* sanity test, ignore the write otherwise */
1211                        if (0 <= results[0] && results[0] <= 2) {
1212
1213                                /* insert the new mapping and write it out */
1214                                data->sf_tach_to_pwm &= ~0x3 << nr * 2;
1215                                data->sf_tach_to_pwm |= results[0] << nr * 2;
1216                                lm93_write_byte(client, LM93_REG_SF_TACH_TO_PWM,
1217                                        data->sf_tach_to_pwm);
1218
1219                                /* insert the enable bit and write it out */
1220                                data->sfc2 &= ~1 << nr;
1221                                data->sfc2 |= 1 << nr;
1222                                lm93_write_byte(client, LM93_REG_SFC2,
1223                                        data->sfc2);
1224                        }
1225
1226                        up(&data->update_lock);
1227                }
1228        }
1229}
1230
1231void lm93_prochot(struct i2c_client *client, int operation, int ctl_name,
1232                int *nrels_mag, long *results)
1233{
1234        struct lm93_data *data = client->data;
1235        int nr = ctl_name - LM93_SYSCTL_PROCHOT1;
1236
1237        if (0 > nr || nr > 1)
1238                return; /* ERROR */
1239
1240        if (operation == SENSORS_PROC_REAL_INFO)
1241                *nrels_mag = 0;
1242        else if (operation == SENSORS_PROC_REAL_READ) {
1243                lm93_update_client(client);
1244                results[0] = data->prochot_max[nr];
1245                results[1] = data->block4[nr].avg;
1246                results[2] = data->block4[nr].avg;
1247                *nrels_mag = 3;
1248        } else if (operation == SENSORS_PROC_REAL_WRITE) {
1249                down(&data->update_lock);
1250                if (*nrels_mag >= 1) {
1251                        data->prochot_max[nr] = LM93_PROCHOT_TO_REG(results[0]);
1252                        lm93_write_byte(client, LM93_REG_PROCHOT_MAX(nr),
1253                                data->prochot_max[nr]);
1254                }
1255                up(&data->update_lock);
1256        }
1257}
1258
1259void lm93_prochot_short(struct i2c_client *client, int operation, int ctl_name,
1260        int *nrels_mag, long *results)
1261{
1262        struct lm93_data *data = client->data;
1263
1264        if (ctl_name != LM93_SYSCTL_PROCHOT_SHORT)
1265                return; /* ERROR */
1266
1267        if (operation == SENSORS_PROC_REAL_INFO)
1268                *nrels_mag = 0;
1269        else if (operation == SENSORS_PROC_REAL_READ) {
1270                lm93_update_client(client);
1271                results[0] = (data->config & 0x10) ? 1 : 0;
1272                *nrels_mag = 1;
1273        } else if (operation == SENSORS_PROC_REAL_WRITE) {
1274                down(&data->update_lock);
1275                if (*nrels_mag >= 1) {
1276                        if (results[0])
1277                                data->config |= 0x10;
1278                        else
1279                                data->config &= ~0x10;
1280                        lm93_write_byte(client, LM93_REG_CONFIG, data->config);
1281                }
1282                up(&data->update_lock);
1283        }
1284}
1285
1286#if 0
1287void lm78_alarms(struct i2c_client *client, int operation, int ctl_name,
1288                 int *nrels_mag, long *results)
1289{
1290        struct lm78_data *data = client->data;
1291        if (operation == SENSORS_PROC_REAL_INFO)
1292                *nrels_mag = 0;
1293        else if (operation == SENSORS_PROC_REAL_READ) {
1294                lm78_update_client(client);
1295                results[0] = ALARMS_FROM_REG(data->alarms);
1296                *nrels_mag = 1;
1297        }
1298}
1299
1300#endif
1301
1302static int __init lm93_init(void)
1303{
1304        printk(KERN_INFO "lm93.o version %s (%s)\n", LM_VERSION, LM_DATE);
1305        return i2c_add_driver(&lm93_driver);
1306}
1307
1308static void __exit lm93_exit(void)
1309{
1310        i2c_del_driver(&lm93_driver);
1311}
1312
1313MODULE_AUTHOR("Mark M. Hoffman <mhoffman@lightlink.com>");
1314MODULE_DESCRIPTION("LM93 driver");
1315MODULE_LICENSE("GPL");
1316
1317module_init(lm93_init);
1318module_exit(lm93_exit);
Note: See TracBrowser for help on using the browser.