Merge tag 'pmdomain-v6.6-rc1-2' of git://git.kernel.org/pub/scm/linux/kernel/git...
[platform/kernel/linux-rpi.git] / drivers / hwmon / lm93.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * lm93.c - Part of lm_sensors, Linux kernel modules for hardware monitoring
4  *
5  * Author/Maintainer: Mark M. Hoffman <mhoffman@lightlink.com>
6  *      Copyright (c) 2004 Utilitek Systems, Inc.
7  *
8  * derived in part from lm78.c:
9  *      Copyright (c) 1998, 1999  Frodo Looijaard <frodol@dds.nl>
10  *
11  * derived in part from lm85.c:
12  *      Copyright (c) 2002, 2003 Philip Pokorny <ppokorny@penguincomputing.com>
13  *      Copyright (c) 2003       Margit Schubert-While <margitsw@t-online.de>
14  *
15  * derived in part from w83l785ts.c:
16  *      Copyright (c) 2003-2004 Jean Delvare <jdelvare@suse.de>
17  *
18  * Ported to Linux 2.6 by Eric J. Bowersox <ericb@aspsys.com>
19  *      Copyright (c) 2005 Aspen Systems, Inc.
20  *
21  * Adapted to 2.6.20 by Carsten Emde <cbe@osadl.org>
22  *      Copyright (c) 2006 Carsten Emde, Open Source Automation Development Lab
23  *
24  * Modified for mainline integration by Hans J. Koch <hjk@hansjkoch.de>
25  *      Copyright (c) 2007 Hans J. Koch, Linutronix GmbH
26  */
27
28 #include <linux/module.h>
29 #include <linux/init.h>
30 #include <linux/slab.h>
31 #include <linux/i2c.h>
32 #include <linux/hwmon.h>
33 #include <linux/hwmon-sysfs.h>
34 #include <linux/hwmon-vid.h>
35 #include <linux/err.h>
36 #include <linux/delay.h>
37 #include <linux/jiffies.h>
38
39 /* LM93 REGISTER ADDRESSES */
40
41 /* miscellaneous */
42 #define LM93_REG_MFR_ID                 0x3e
43 #define LM93_REG_VER                    0x3f
44 #define LM93_REG_STATUS_CONTROL         0xe2
45 #define LM93_REG_CONFIG                 0xe3
46 #define LM93_REG_SLEEP_CONTROL          0xe4
47
48 /* alarm values start here */
49 #define LM93_REG_HOST_ERROR_1           0x48
50
51 /* voltage inputs: in1-in16 (nr => 0-15) */
52 #define LM93_REG_IN(nr)                 (0x56 + (nr))
53 #define LM93_REG_IN_MIN(nr)             (0x90 + (nr) * 2)
54 #define LM93_REG_IN_MAX(nr)             (0x91 + (nr) * 2)
55
56 /* temperature inputs: temp1-temp4 (nr => 0-3) */
57 #define LM93_REG_TEMP(nr)               (0x50 + (nr))
58 #define LM93_REG_TEMP_MIN(nr)           (0x78 + (nr) * 2)
59 #define LM93_REG_TEMP_MAX(nr)           (0x79 + (nr) * 2)
60
61 /* temp[1-4]_auto_boost (nr => 0-3) */
62 #define LM93_REG_BOOST(nr)              (0x80 + (nr))
63
64 /* #PROCHOT inputs: prochot1-prochot2 (nr => 0-1) */
65 #define LM93_REG_PROCHOT_CUR(nr)        (0x67 + (nr) * 2)
66 #define LM93_REG_PROCHOT_AVG(nr)        (0x68 + (nr) * 2)
67 #define LM93_REG_PROCHOT_MAX(nr)        (0xb0 + (nr))
68
69 /* fan tach inputs: fan1-fan4 (nr => 0-3) */
70 #define LM93_REG_FAN(nr)                (0x6e + (nr) * 2)
71 #define LM93_REG_FAN_MIN(nr)            (0xb4 + (nr) * 2)
72
73 /* pwm outputs: pwm1-pwm2 (nr => 0-1, reg => 0-3) */
74 #define LM93_REG_PWM_CTL(nr, reg)       (0xc8 + (reg) + (nr) * 4)
75 #define LM93_PWM_CTL1   0x0
76 #define LM93_PWM_CTL2   0x1
77 #define LM93_PWM_CTL3   0x2
78 #define LM93_PWM_CTL4   0x3
79
80 /* GPIO input state */
81 #define LM93_REG_GPI                    0x6b
82
83 /* vid inputs: vid1-vid2 (nr => 0-1) */
84 #define LM93_REG_VID(nr)                (0x6c + (nr))
85
86 /* vccp1 & vccp2: VID relative inputs (nr => 0-1) */
87 #define LM93_REG_VCCP_LIMIT_OFF(nr)     (0xb2 + (nr))
88
89 /* temp[1-4]_auto_boost_hyst */
90 #define LM93_REG_BOOST_HYST_12          0xc0
91 #define LM93_REG_BOOST_HYST_34          0xc1
92 #define LM93_REG_BOOST_HYST(nr)         (0xc0 + (nr)/2)
93
94 /* temp[1-4]_auto_pwm_[min|hyst] */
95 #define LM93_REG_PWM_MIN_HYST_12        0xc3
96 #define LM93_REG_PWM_MIN_HYST_34        0xc4
97 #define LM93_REG_PWM_MIN_HYST(nr)       (0xc3 + (nr)/2)
98
99 /* prochot_override & prochot_interval */
100 #define LM93_REG_PROCHOT_OVERRIDE       0xc6
101 #define LM93_REG_PROCHOT_INTERVAL       0xc7
102
103 /* temp[1-4]_auto_base (nr => 0-3) */
104 #define LM93_REG_TEMP_BASE(nr)          (0xd0 + (nr))
105
106 /* temp[1-4]_auto_offsets (step => 0-11) */
107 #define LM93_REG_TEMP_OFFSET(step)      (0xd4 + (step))
108
109 /* #PROCHOT & #VRDHOT PWM ramp control */
110 #define LM93_REG_PWM_RAMP_CTL           0xbf
111
112 /* miscellaneous */
113 #define LM93_REG_SFC1           0xbc
114 #define LM93_REG_SFC2           0xbd
115 #define LM93_REG_GPI_VID_CTL    0xbe
116 #define LM93_REG_SF_TACH_TO_PWM 0xe0
117
118 /* error masks */
119 #define LM93_REG_GPI_ERR_MASK   0xec
120 #define LM93_REG_MISC_ERR_MASK  0xed
121
122 /* LM93 REGISTER VALUES */
123 #define LM93_MFR_ID             0x73
124 #define LM93_MFR_ID_PROTOTYPE   0x72
125
126 /* LM94 REGISTER VALUES */
127 #define LM94_MFR_ID_2           0x7a
128 #define LM94_MFR_ID             0x79
129 #define LM94_MFR_ID_PROTOTYPE   0x78
130
131 /* SMBus capabilities */
132 #define LM93_SMBUS_FUNC_FULL (I2C_FUNC_SMBUS_BYTE_DATA | \
133                 I2C_FUNC_SMBUS_WORD_DATA | I2C_FUNC_SMBUS_BLOCK_DATA)
134 #define LM93_SMBUS_FUNC_MIN  (I2C_FUNC_SMBUS_BYTE_DATA | \
135                 I2C_FUNC_SMBUS_WORD_DATA)
136
137 /* Addresses to scan */
138 static const unsigned short normal_i2c[] = { 0x2c, 0x2d, 0x2e, I2C_CLIENT_END };
139
140 /* Insmod parameters */
141
142 static bool disable_block;
143 module_param(disable_block, bool, 0);
144 MODULE_PARM_DESC(disable_block,
145         "Set to non-zero to disable SMBus block data transactions.");
146
147 static bool init;
148 module_param(init, bool, 0);
149 MODULE_PARM_DESC(init, "Set to non-zero to force chip initialization.");
150
151 static int vccp_limit_type[2] = {0, 0};
152 module_param_array(vccp_limit_type, int, NULL, 0);
153 MODULE_PARM_DESC(vccp_limit_type, "Configures in7 and in8 limit modes.");
154
155 static int vid_agtl;
156 module_param(vid_agtl, int, 0);
157 MODULE_PARM_DESC(vid_agtl, "Configures VID pin input thresholds.");
158
159 /* Driver data */
160 static struct i2c_driver lm93_driver;
161
162 /* LM93 BLOCK READ COMMANDS */
163 static const struct { u8 cmd; u8 len; } lm93_block_read_cmds[12] = {
164         { 0xf2,  8 },
165         { 0xf3,  8 },
166         { 0xf4,  6 },
167         { 0xf5, 16 },
168         { 0xf6,  4 },
169         { 0xf7,  8 },
170         { 0xf8, 12 },
171         { 0xf9, 32 },
172         { 0xfa,  8 },
173         { 0xfb,  8 },
174         { 0xfc, 16 },
175         { 0xfd,  9 },
176 };
177
178 /*
179  * ALARMS: SYSCTL format described further below
180  * REG: 64 bits in 8 registers, as immediately below
181  */
182 struct block1_t {
183         u8 host_status_1;
184         u8 host_status_2;
185         u8 host_status_3;
186         u8 host_status_4;
187         u8 p1_prochot_status;
188         u8 p2_prochot_status;
189         u8 gpi_status;
190         u8 fan_status;
191 };
192
193 /*
194  * Client-specific data
195  */
196 struct lm93_data {
197         struct i2c_client *client;
198
199         struct mutex update_lock;
200         unsigned long last_updated;     /* In jiffies */
201
202         /* client update function */
203         void (*update)(struct lm93_data *, struct i2c_client *);
204
205         bool valid; /* true if following fields are valid */
206
207         /* register values, arranged by block read groups */
208         struct block1_t block1;
209
210         /*
211          * temp1 - temp4: unfiltered readings
212          * temp1 - temp2: filtered readings
213          */
214         u8 block2[6];
215
216         /* vin1 - vin16: readings */
217         u8 block3[16];
218
219         /* prochot1 - prochot2: readings */
220         struct {
221                 u8 cur;
222                 u8 avg;
223         } block4[2];
224
225         /* fan counts 1-4 => 14-bits, LE, *left* justified */
226         u16 block5[4];
227
228         /* block6 has a lot of data we don't need */
229         struct {
230                 u8 min;
231                 u8 max;
232         } temp_lim[4];
233
234         /* vin1 - vin16: low and high limits */
235         struct {
236                 u8 min;
237                 u8 max;
238         } block7[16];
239
240         /* fan count limits 1-4 => same format as block5 */
241         u16 block8[4];
242
243         /* pwm control registers (2 pwms, 4 regs) */
244         u8 block9[2][4];
245
246         /* auto/pwm base temp and offset temp registers */
247         struct {
248                 u8 base[4];
249                 u8 offset[12];
250         } block10;
251
252         /* master config register */
253         u8 config;
254
255         /* VID1 & VID2 => register format, 6-bits, right justified */
256         u8 vid[2];
257
258         /* prochot1 - prochot2: limits */
259         u8 prochot_max[2];
260
261         /* vccp1 & vccp2 (in7 & in8): VID relative limits (register format) */
262         u8 vccp_limits[2];
263
264         /* GPIO input state (register format, i.e. inverted) */
265         u8 gpi;
266
267         /* #PROCHOT override (register format) */
268         u8 prochot_override;
269
270         /* #PROCHOT intervals (register format) */
271         u8 prochot_interval;
272
273         /* Fan Boost Temperatures (register format) */
274         u8 boost[4];
275
276         /* Fan Boost Hysteresis (register format) */
277         u8 boost_hyst[2];
278
279         /* Temperature Zone Min. PWM & Hysteresis (register format) */
280         u8 auto_pwm_min_hyst[2];
281
282         /* #PROCHOT & #VRDHOT PWM Ramp Control */
283         u8 pwm_ramp_ctl;
284
285         /* miscellaneous setup regs */
286         u8 sfc1;
287         u8 sfc2;
288         u8 sf_tach_to_pwm;
289
290         /*
291          * The two PWM CTL2  registers can read something other than what was
292          * last written for the OVR_DC field (duty cycle override).  So, we
293          * save the user-commanded value here.
294          */
295         u8 pwm_override[2];
296 };
297
298 /*
299  * VID: mV
300  * REG: 6-bits, right justified, *always* using Intel VRM/VRD 10
301  */
302 static int LM93_VID_FROM_REG(u8 reg)
303 {
304         return vid_from_reg((reg & 0x3f), 100);
305 }
306
307 /* min, max, and nominal register values, per channel (u8) */
308 static const u8 lm93_vin_reg_min[16] = {
309         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
310         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xae,
311 };
312 static const u8 lm93_vin_reg_max[16] = {
313         0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
314         0xff, 0xfa, 0xff, 0xff, 0xff, 0xff, 0xff, 0xd1,
315 };
316 /*
317  * Values from the datasheet. They're here for documentation only.
318  * static const u8 lm93_vin_reg_nom[16] = {
319  * 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0,
320  * 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0x40, 0xc0,
321  * };
322  */
323
324 /* min, max, and nominal voltage readings, per channel (mV)*/
325 static const unsigned long lm93_vin_val_min[16] = {
326         0, 0, 0, 0, 0, 0, 0, 0,
327         0, 0, 0, 0, 0, 0, 0, 3000,
328 };
329
330 static const unsigned long lm93_vin_val_max[16] = {
331         1236, 1236, 1236, 1600, 2000, 2000, 1600, 1600,
332         4400, 6500, 3333, 2625, 1312, 1312, 1236, 3600,
333 };
334 /*
335  * Values from the datasheet. They're here for documentation only.
336  * static const unsigned long lm93_vin_val_nom[16] = {
337  * 927,  927,  927, 1200, 1500, 1500, 1200, 1200,
338  * 3300, 5000, 2500, 1969,  984,  984,  309, 3300,
339  * };
340  */
341
342 static unsigned LM93_IN_FROM_REG(int nr, u8 reg)
343 {
344         const long uv_max = lm93_vin_val_max[nr] * 1000;
345         const long uv_min = lm93_vin_val_min[nr] * 1000;
346
347         const long slope = (uv_max - uv_min) /
348                 (lm93_vin_reg_max[nr] - lm93_vin_reg_min[nr]);
349         const long intercept = uv_min - slope * lm93_vin_reg_min[nr];
350
351         return (slope * reg + intercept + 500) / 1000;
352 }
353
354 /*
355  * IN: mV, limits determined by channel nr
356  * REG: scaling determined by channel nr
357  */
358 static u8 LM93_IN_TO_REG(int nr, unsigned val)
359 {
360         /* range limit */
361         const long mv = clamp_val(val,
362                                   lm93_vin_val_min[nr], lm93_vin_val_max[nr]);
363
364         /* try not to lose too much precision here */
365         const long uv = mv * 1000;
366         const long uv_max = lm93_vin_val_max[nr] * 1000;
367         const long uv_min = lm93_vin_val_min[nr] * 1000;
368
369         /* convert */
370         const long slope = (uv_max - uv_min) /
371                 (lm93_vin_reg_max[nr] - lm93_vin_reg_min[nr]);
372         const long intercept = uv_min - slope * lm93_vin_reg_min[nr];
373
374         u8 result = ((uv - intercept + (slope/2)) / slope);
375         result = clamp_val(result,
376                            lm93_vin_reg_min[nr], lm93_vin_reg_max[nr]);
377         return result;
378 }
379
380 /* vid in mV, upper == 0 indicates low limit, otherwise upper limit */
381 static unsigned LM93_IN_REL_FROM_REG(u8 reg, int upper, int vid)
382 {
383         const long uv_offset = upper ? (((reg >> 4 & 0x0f) + 1) * 12500) :
384                                 (((reg >> 0 & 0x0f) + 1) * -25000);
385         const long uv_vid = vid * 1000;
386         return (uv_vid + uv_offset + 5000) / 10000;
387 }
388
389 #define LM93_IN_MIN_FROM_REG(reg, vid)  LM93_IN_REL_FROM_REG((reg), 0, (vid))
390 #define LM93_IN_MAX_FROM_REG(reg, vid)  LM93_IN_REL_FROM_REG((reg), 1, (vid))
391
392 /*
393  * vid in mV , upper == 0 indicates low limit, otherwise upper limit
394  * upper also determines which nibble of the register is returned
395  * (the other nibble will be 0x0)
396  */
397 static u8 LM93_IN_REL_TO_REG(unsigned val, int upper, int vid)
398 {
399         long uv_offset = vid * 1000 - val * 10000;
400         if (upper) {
401                 uv_offset = clamp_val(uv_offset, 12500, 200000);
402                 return (u8)((uv_offset /  12500 - 1) << 4);
403         } else {
404                 uv_offset = clamp_val(uv_offset, -400000, -25000);
405                 return (u8)((uv_offset / -25000 - 1) << 0);
406         }
407 }
408
409 /*
410  * TEMP: 1/1000 degrees C (-128C to +127C)
411  * REG: 1C/bit, two's complement
412  */
413 static int LM93_TEMP_FROM_REG(u8 reg)
414 {
415         return (s8)reg * 1000;
416 }
417
418 #define LM93_TEMP_MIN (-128000)
419 #define LM93_TEMP_MAX (127000)
420
421 /*
422  * TEMP: 1/1000 degrees C (-128C to +127C)
423  * REG: 1C/bit, two's complement
424  */
425 static u8 LM93_TEMP_TO_REG(long temp)
426 {
427         int ntemp = clamp_val(temp, LM93_TEMP_MIN, LM93_TEMP_MAX);
428         ntemp += (ntemp < 0 ? -500 : 500);
429         return (u8)(ntemp / 1000);
430 }
431
432 /* Determine 4-bit temperature offset resolution */
433 static int LM93_TEMP_OFFSET_MODE_FROM_REG(u8 sfc2, int nr)
434 {
435         /* mode: 0 => 1C/bit, nonzero => 0.5C/bit */
436         return sfc2 & (nr < 2 ? 0x10 : 0x20);
437 }
438
439 /*
440  * This function is common to all 4-bit temperature offsets
441  * reg is 4 bits right justified
442  * mode 0 => 1C/bit, mode !0 => 0.5C/bit
443  */
444 static int LM93_TEMP_OFFSET_FROM_REG(u8 reg, int mode)
445 {
446         return (reg & 0x0f) * (mode ? 5 : 10);
447 }
448
449 #define LM93_TEMP_OFFSET_MIN  (0)
450 #define LM93_TEMP_OFFSET_MAX0 (150)
451 #define LM93_TEMP_OFFSET_MAX1 (75)
452
453 /*
454  * This function is common to all 4-bit temperature offsets
455  * returns 4 bits right justified
456  * mode 0 => 1C/bit, mode !0 => 0.5C/bit
457  */
458 static u8 LM93_TEMP_OFFSET_TO_REG(int off, int mode)
459 {
460         int factor = mode ? 5 : 10;
461
462         off = clamp_val(off, LM93_TEMP_OFFSET_MIN,
463                 mode ? LM93_TEMP_OFFSET_MAX1 : LM93_TEMP_OFFSET_MAX0);
464         return (u8)((off + factor/2) / factor);
465 }
466
467 /* 0 <= nr <= 3 */
468 static int LM93_TEMP_AUTO_OFFSET_FROM_REG(u8 reg, int nr, int mode)
469 {
470         /* temp1-temp2 (nr=0,1) use lower nibble */
471         if (nr < 2)
472                 return LM93_TEMP_OFFSET_FROM_REG(reg & 0x0f, mode);
473
474         /* temp3-temp4 (nr=2,3) use upper nibble */
475         else
476                 return LM93_TEMP_OFFSET_FROM_REG(reg >> 4 & 0x0f, mode);
477 }
478
479 /*
480  * TEMP: 1/10 degrees C (0C to +15C (mode 0) or +7.5C (mode non-zero))
481  * REG: 1.0C/bit (mode 0) or 0.5C/bit (mode non-zero)
482  * 0 <= nr <= 3
483  */
484 static u8 LM93_TEMP_AUTO_OFFSET_TO_REG(u8 old, int off, int nr, int mode)
485 {
486         u8 new = LM93_TEMP_OFFSET_TO_REG(off, mode);
487
488         /* temp1-temp2 (nr=0,1) use lower nibble */
489         if (nr < 2)
490                 return (old & 0xf0) | (new & 0x0f);
491
492         /* temp3-temp4 (nr=2,3) use upper nibble */
493         else
494                 return (new << 4 & 0xf0) | (old & 0x0f);
495 }
496
497 static int LM93_AUTO_BOOST_HYST_FROM_REGS(struct lm93_data *data, int nr,
498                 int mode)
499 {
500         u8 reg;
501
502         switch (nr) {
503         case 0:
504                 reg = data->boost_hyst[0] & 0x0f;
505                 break;
506         case 1:
507                 reg = data->boost_hyst[0] >> 4 & 0x0f;
508                 break;
509         case 2:
510                 reg = data->boost_hyst[1] & 0x0f;
511                 break;
512         case 3:
513         default:
514                 reg = data->boost_hyst[1] >> 4 & 0x0f;
515                 break;
516         }
517
518         return LM93_TEMP_FROM_REG(data->boost[nr]) -
519                         LM93_TEMP_OFFSET_FROM_REG(reg, mode);
520 }
521
522 static u8 LM93_AUTO_BOOST_HYST_TO_REG(struct lm93_data *data, long hyst,
523                 int nr, int mode)
524 {
525         u8 reg = LM93_TEMP_OFFSET_TO_REG(
526                         (LM93_TEMP_FROM_REG(data->boost[nr]) - hyst), mode);
527
528         switch (nr) {
529         case 0:
530                 reg = (data->boost_hyst[0] & 0xf0) | (reg & 0x0f);
531                 break;
532         case 1:
533                 reg = (reg << 4 & 0xf0) | (data->boost_hyst[0] & 0x0f);
534                 break;
535         case 2:
536                 reg = (data->boost_hyst[1] & 0xf0) | (reg & 0x0f);
537                 break;
538         case 3:
539         default:
540                 reg = (reg << 4 & 0xf0) | (data->boost_hyst[1] & 0x0f);
541                 break;
542         }
543
544         return reg;
545 }
546
547 /*
548  * PWM: 0-255 per sensors documentation
549  * REG: 0-13 as mapped below... right justified
550  */
551 enum pwm_freq { LM93_PWM_MAP_HI_FREQ, LM93_PWM_MAP_LO_FREQ };
552
553 static int lm93_pwm_map[2][16] = {
554         {
555                 0x00, /*   0.00% */ 0x40, /*  25.00% */
556                 0x50, /*  31.25% */ 0x60, /*  37.50% */
557                 0x70, /*  43.75% */ 0x80, /*  50.00% */
558                 0x90, /*  56.25% */ 0xa0, /*  62.50% */
559                 0xb0, /*  68.75% */ 0xc0, /*  75.00% */
560                 0xd0, /*  81.25% */ 0xe0, /*  87.50% */
561                 0xf0, /*  93.75% */ 0xff, /* 100.00% */
562                 0xff, 0xff, /* 14, 15 are reserved and should never occur */
563         },
564         {
565                 0x00, /*   0.00% */ 0x40, /*  25.00% */
566                 0x49, /*  28.57% */ 0x52, /*  32.14% */
567                 0x5b, /*  35.71% */ 0x64, /*  39.29% */
568                 0x6d, /*  42.86% */ 0x76, /*  46.43% */
569                 0x80, /*  50.00% */ 0x89, /*  53.57% */
570                 0x92, /*  57.14% */ 0xb6, /*  71.43% */
571                 0xdb, /*  85.71% */ 0xff, /* 100.00% */
572                 0xff, 0xff, /* 14, 15 are reserved and should never occur */
573         },
574 };
575
576 static int LM93_PWM_FROM_REG(u8 reg, enum pwm_freq freq)
577 {
578         return lm93_pwm_map[freq][reg & 0x0f];
579 }
580
581 /* round up to nearest match */
582 static u8 LM93_PWM_TO_REG(int pwm, enum pwm_freq freq)
583 {
584         int i;
585         for (i = 0; i < 13; i++)
586                 if (pwm <= lm93_pwm_map[freq][i])
587                         break;
588
589         /* can fall through with i==13 */
590         return (u8)i;
591 }
592
593 static int LM93_FAN_FROM_REG(u16 regs)
594 {
595         const u16 count = le16_to_cpu(regs) >> 2;
596         return count == 0 ? -1 : count == 0x3fff ? 0 : 1350000 / count;
597 }
598
599 /*
600  * RPM: (82.5 to 1350000)
601  * REG: 14-bits, LE, *left* justified
602  */
603 static u16 LM93_FAN_TO_REG(long rpm)
604 {
605         u16 count, regs;
606
607         if (rpm == 0) {
608                 count = 0x3fff;
609         } else {
610                 rpm = clamp_val(rpm, 1, 1000000);
611                 count = clamp_val((1350000 + rpm) / rpm, 1, 0x3ffe);
612         }
613
614         regs = count << 2;
615         return cpu_to_le16(regs);
616 }
617
618 /*
619  * PWM FREQ: HZ
620  * REG: 0-7 as mapped below
621  */
622 static int lm93_pwm_freq_map[8] = {
623         22500, 96, 84, 72, 60, 48, 36, 12
624 };
625
626 static int LM93_PWM_FREQ_FROM_REG(u8 reg)
627 {
628         return lm93_pwm_freq_map[reg & 0x07];
629 }
630
631 /* round up to nearest match */
632 static u8 LM93_PWM_FREQ_TO_REG(int freq)
633 {
634         int i;
635         for (i = 7; i > 0; i--)
636                 if (freq <= lm93_pwm_freq_map[i])
637                         break;
638
639         /* can fall through with i==0 */
640         return (u8)i;
641 }
642
643 /*
644  * TIME: 1/100 seconds
645  * REG: 0-7 as mapped below
646  */
647 static int lm93_spinup_time_map[8] = {
648         0, 10, 25, 40, 70, 100, 200, 400,
649 };
650
651 static int LM93_SPINUP_TIME_FROM_REG(u8 reg)
652 {
653         return lm93_spinup_time_map[reg >> 5 & 0x07];
654 }
655
656 /* round up to nearest match */
657 static u8 LM93_SPINUP_TIME_TO_REG(int time)
658 {
659         int i;
660         for (i = 0; i < 7; i++)
661                 if (time <= lm93_spinup_time_map[i])
662                         break;
663
664         /* can fall through with i==8 */
665         return (u8)i;
666 }
667
668 #define LM93_RAMP_MIN 0
669 #define LM93_RAMP_MAX 75
670
671 static int LM93_RAMP_FROM_REG(u8 reg)
672 {
673         return (reg & 0x0f) * 5;
674 }
675
676 /*
677  * RAMP: 1/100 seconds
678  * REG: 50mS/bit 4-bits right justified
679  */
680 static u8 LM93_RAMP_TO_REG(int ramp)
681 {
682         ramp = clamp_val(ramp, LM93_RAMP_MIN, LM93_RAMP_MAX);
683         return (u8)((ramp + 2) / 5);
684 }
685
686 /*
687  * PROCHOT: 0-255, 0 => 0%, 255 => > 96.6%
688  * REG: (same)
689  */
690 static u8 LM93_PROCHOT_TO_REG(long prochot)
691 {
692         prochot = clamp_val(prochot, 0, 255);
693         return (u8)prochot;
694 }
695
696 /*
697  * PROCHOT-INTERVAL: 73 - 37200 (1/100 seconds)
698  * REG: 0-9 as mapped below
699  */
700 static int lm93_interval_map[10] = {
701         73, 146, 290, 580, 1170, 2330, 4660, 9320, 18600, 37200,
702 };
703
704 static int LM93_INTERVAL_FROM_REG(u8 reg)
705 {
706         return lm93_interval_map[reg & 0x0f];
707 }
708
709 /* round up to nearest match */
710 static u8 LM93_INTERVAL_TO_REG(long interval)
711 {
712         int i;
713         for (i = 0; i < 9; i++)
714                 if (interval <= lm93_interval_map[i])
715                         break;
716
717         /* can fall through with i==9 */
718         return (u8)i;
719 }
720
721 /*
722  * GPIO: 0-255, GPIO0 is LSB
723  * REG: inverted
724  */
725 static unsigned LM93_GPI_FROM_REG(u8 reg)
726 {
727         return ~reg & 0xff;
728 }
729
730 /*
731  * alarm bitmask definitions
732  * The LM93 has nearly 64 bits of error status... I've pared that down to
733  * what I think is a useful subset in order to fit it into 32 bits.
734  *
735  * Especially note that the #VRD_HOT alarms are missing because we provide
736  * that information as values in another sysfs file.
737  *
738  * If libsensors is extended to support 64 bit values, this could be revisited.
739  */
740 #define LM93_ALARM_IN1          0x00000001
741 #define LM93_ALARM_IN2          0x00000002
742 #define LM93_ALARM_IN3          0x00000004
743 #define LM93_ALARM_IN4          0x00000008
744 #define LM93_ALARM_IN5          0x00000010
745 #define LM93_ALARM_IN6          0x00000020
746 #define LM93_ALARM_IN7          0x00000040
747 #define LM93_ALARM_IN8          0x00000080
748 #define LM93_ALARM_IN9          0x00000100
749 #define LM93_ALARM_IN10         0x00000200
750 #define LM93_ALARM_IN11         0x00000400
751 #define LM93_ALARM_IN12         0x00000800
752 #define LM93_ALARM_IN13         0x00001000
753 #define LM93_ALARM_IN14         0x00002000
754 #define LM93_ALARM_IN15         0x00004000
755 #define LM93_ALARM_IN16         0x00008000
756 #define LM93_ALARM_FAN1         0x00010000
757 #define LM93_ALARM_FAN2         0x00020000
758 #define LM93_ALARM_FAN3         0x00040000
759 #define LM93_ALARM_FAN4         0x00080000
760 #define LM93_ALARM_PH1_ERR      0x00100000
761 #define LM93_ALARM_PH2_ERR      0x00200000
762 #define LM93_ALARM_SCSI1_ERR    0x00400000
763 #define LM93_ALARM_SCSI2_ERR    0x00800000
764 #define LM93_ALARM_DVDDP1_ERR   0x01000000
765 #define LM93_ALARM_DVDDP2_ERR   0x02000000
766 #define LM93_ALARM_D1_ERR       0x04000000
767 #define LM93_ALARM_D2_ERR       0x08000000
768 #define LM93_ALARM_TEMP1        0x10000000
769 #define LM93_ALARM_TEMP2        0x20000000
770 #define LM93_ALARM_TEMP3        0x40000000
771
772 static unsigned LM93_ALARMS_FROM_REG(struct block1_t b1)
773 {
774         unsigned result;
775         result  = b1.host_status_2 & 0x3f;
776
777         if (vccp_limit_type[0])
778                 result |= (b1.host_status_4 & 0x10) << 2;
779         else
780                 result |= b1.host_status_2 & 0x40;
781
782         if (vccp_limit_type[1])
783                 result |= (b1.host_status_4 & 0x20) << 2;
784         else
785                 result |= b1.host_status_2 & 0x80;
786
787         result |= b1.host_status_3 << 8;
788         result |= (b1.fan_status & 0x0f) << 16;
789         result |= (b1.p1_prochot_status & 0x80) << 13;
790         result |= (b1.p2_prochot_status & 0x80) << 14;
791         result |= (b1.host_status_4 & 0xfc) << 20;
792         result |= (b1.host_status_1 & 0x07) << 28;
793         return result;
794 }
795
796 #define MAX_RETRIES 5
797
798 static u8 lm93_read_byte(struct i2c_client *client, u8 reg)
799 {
800         int value, i;
801
802         /* retry in case of read errors */
803         for (i = 1; i <= MAX_RETRIES; i++) {
804                 value = i2c_smbus_read_byte_data(client, reg);
805                 if (value >= 0) {
806                         return value;
807                 } else {
808                         dev_warn(&client->dev,
809                                  "lm93: read byte data failed, address 0x%02x.\n",
810                                  reg);
811                         mdelay(i + 3);
812                 }
813
814         }
815
816         /* <TODO> what to return in case of error? */
817         dev_err(&client->dev, "lm93: All read byte retries failed!!\n");
818         return 0;
819 }
820
821 static int lm93_write_byte(struct i2c_client *client, u8 reg, u8 value)
822 {
823         int result;
824
825         /* <TODO> how to handle write errors? */
826         result = i2c_smbus_write_byte_data(client, reg, value);
827
828         if (result < 0)
829                 dev_warn(&client->dev,
830                          "lm93: write byte data failed, 0x%02x at address 0x%02x.\n",
831                          value, reg);
832
833         return result;
834 }
835
836 static u16 lm93_read_word(struct i2c_client *client, u8 reg)
837 {
838         int value, i;
839
840         /* retry in case of read errors */
841         for (i = 1; i <= MAX_RETRIES; i++) {
842                 value = i2c_smbus_read_word_data(client, reg);
843                 if (value >= 0) {
844                         return value;
845                 } else {
846                         dev_warn(&client->dev,
847                                  "lm93: read word data failed, address 0x%02x.\n",
848                                  reg);
849                         mdelay(i + 3);
850                 }
851
852         }
853
854         /* <TODO> what to return in case of error? */
855         dev_err(&client->dev, "lm93: All read word retries failed!!\n");
856         return 0;
857 }
858
859 static int lm93_write_word(struct i2c_client *client, u8 reg, u16 value)
860 {
861         int result;
862
863         /* <TODO> how to handle write errors? */
864         result = i2c_smbus_write_word_data(client, reg, value);
865
866         if (result < 0)
867                 dev_warn(&client->dev,
868                          "lm93: write word data failed, 0x%04x at address 0x%02x.\n",
869                          value, reg);
870
871         return result;
872 }
873
874 static u8 lm93_block_buffer[I2C_SMBUS_BLOCK_MAX];
875
876 /*
877  * read block data into values, retry if not expected length
878  * fbn => index to lm93_block_read_cmds table
879  * (Fixed Block Number - section 14.5.2 of LM93 datasheet)
880  */
881 static void lm93_read_block(struct i2c_client *client, u8 fbn, u8 *values)
882 {
883         int i, result = 0;
884
885         for (i = 1; i <= MAX_RETRIES; i++) {
886                 result = i2c_smbus_read_block_data(client,
887                         lm93_block_read_cmds[fbn].cmd, lm93_block_buffer);
888
889                 if (result == lm93_block_read_cmds[fbn].len) {
890                         break;
891                 } else {
892                         dev_warn(&client->dev,
893                                  "lm93: block read data failed, command 0x%02x.\n",
894                                  lm93_block_read_cmds[fbn].cmd);
895                         mdelay(i + 3);
896                 }
897         }
898
899         if (result == lm93_block_read_cmds[fbn].len) {
900                 memcpy(values, lm93_block_buffer,
901                        lm93_block_read_cmds[fbn].len);
902         } else {
903                 /* <TODO> what to do in case of error? */
904         }
905 }
906
907 static struct lm93_data *lm93_update_device(struct device *dev)
908 {
909         struct lm93_data *data = dev_get_drvdata(dev);
910         struct i2c_client *client = data->client;
911         const unsigned long interval = HZ + (HZ / 2);
912
913         mutex_lock(&data->update_lock);
914
915         if (time_after(jiffies, data->last_updated + interval) ||
916                 !data->valid) {
917
918                 data->update(data, client);
919                 data->last_updated = jiffies;
920                 data->valid = true;
921         }
922
923         mutex_unlock(&data->update_lock);
924         return data;
925 }
926
927 /* update routine for data that has no corresponding SMBus block command */
928 static void lm93_update_client_common(struct lm93_data *data,
929                                       struct i2c_client *client)
930 {
931         int i;
932         u8 *ptr;
933
934         /* temp1 - temp4: limits */
935         for (i = 0; i < 4; i++) {
936                 data->temp_lim[i].min =
937                         lm93_read_byte(client, LM93_REG_TEMP_MIN(i));
938                 data->temp_lim[i].max =
939                         lm93_read_byte(client, LM93_REG_TEMP_MAX(i));
940         }
941
942         /* config register */
943         data->config = lm93_read_byte(client, LM93_REG_CONFIG);
944
945         /* vid1 - vid2: values */
946         for (i = 0; i < 2; i++)
947                 data->vid[i] = lm93_read_byte(client, LM93_REG_VID(i));
948
949         /* prochot1 - prochot2: limits */
950         for (i = 0; i < 2; i++)
951                 data->prochot_max[i] = lm93_read_byte(client,
952                                 LM93_REG_PROCHOT_MAX(i));
953
954         /* vccp1 - vccp2: VID relative limits */
955         for (i = 0; i < 2; i++)
956                 data->vccp_limits[i] = lm93_read_byte(client,
957                                 LM93_REG_VCCP_LIMIT_OFF(i));
958
959         /* GPIO input state */
960         data->gpi = lm93_read_byte(client, LM93_REG_GPI);
961
962         /* #PROCHOT override state */
963         data->prochot_override = lm93_read_byte(client,
964                         LM93_REG_PROCHOT_OVERRIDE);
965
966         /* #PROCHOT intervals */
967         data->prochot_interval = lm93_read_byte(client,
968                         LM93_REG_PROCHOT_INTERVAL);
969
970         /* Fan Boost Temperature registers */
971         for (i = 0; i < 4; i++)
972                 data->boost[i] = lm93_read_byte(client, LM93_REG_BOOST(i));
973
974         /* Fan Boost Temperature Hyst. registers */
975         data->boost_hyst[0] = lm93_read_byte(client, LM93_REG_BOOST_HYST_12);
976         data->boost_hyst[1] = lm93_read_byte(client, LM93_REG_BOOST_HYST_34);
977
978         /* Temperature Zone Min. PWM & Hysteresis registers */
979         data->auto_pwm_min_hyst[0] =
980                         lm93_read_byte(client, LM93_REG_PWM_MIN_HYST_12);
981         data->auto_pwm_min_hyst[1] =
982                         lm93_read_byte(client, LM93_REG_PWM_MIN_HYST_34);
983
984         /* #PROCHOT & #VRDHOT PWM Ramp Control register */
985         data->pwm_ramp_ctl = lm93_read_byte(client, LM93_REG_PWM_RAMP_CTL);
986
987         /* misc setup registers */
988         data->sfc1 = lm93_read_byte(client, LM93_REG_SFC1);
989         data->sfc2 = lm93_read_byte(client, LM93_REG_SFC2);
990         data->sf_tach_to_pwm = lm93_read_byte(client,
991                         LM93_REG_SF_TACH_TO_PWM);
992
993         /* write back alarm values to clear */
994         for (i = 0, ptr = (u8 *)(&data->block1); i < 8; i++)
995                 lm93_write_byte(client, LM93_REG_HOST_ERROR_1 + i, *(ptr + i));
996 }
997
998 /* update routine which uses SMBus block data commands */
999 static void lm93_update_client_full(struct lm93_data *data,
1000                                     struct i2c_client *client)
1001 {
1002         dev_dbg(&client->dev, "starting device update (block data enabled)\n");
1003
1004         /* in1 - in16: values & limits */
1005         lm93_read_block(client, 3, (u8 *)(data->block3));
1006         lm93_read_block(client, 7, (u8 *)(data->block7));
1007
1008         /* temp1 - temp4: values */
1009         lm93_read_block(client, 2, (u8 *)(data->block2));
1010
1011         /* prochot1 - prochot2: values */
1012         lm93_read_block(client, 4, (u8 *)(data->block4));
1013
1014         /* fan1 - fan4: values & limits */
1015         lm93_read_block(client, 5, (u8 *)(data->block5));
1016         lm93_read_block(client, 8, (u8 *)(data->block8));
1017
1018         /* pmw control registers */
1019         lm93_read_block(client, 9, (u8 *)(data->block9));
1020
1021         /* alarm values */
1022         lm93_read_block(client, 1, (u8 *)(&data->block1));
1023
1024         /* auto/pwm registers */
1025         lm93_read_block(client, 10, (u8 *)(&data->block10));
1026
1027         lm93_update_client_common(data, client);
1028 }
1029
1030 /* update routine which uses SMBus byte/word data commands only */
1031 static void lm93_update_client_min(struct lm93_data *data,
1032                                    struct i2c_client *client)
1033 {
1034         int i, j;
1035         u8 *ptr;
1036
1037         dev_dbg(&client->dev, "starting device update (block data disabled)\n");
1038
1039         /* in1 - in16: values & limits */
1040         for (i = 0; i < 16; i++) {
1041                 data->block3[i] =
1042                         lm93_read_byte(client, LM93_REG_IN(i));
1043                 data->block7[i].min =
1044                         lm93_read_byte(client, LM93_REG_IN_MIN(i));
1045                 data->block7[i].max =
1046                         lm93_read_byte(client, LM93_REG_IN_MAX(i));
1047         }
1048
1049         /* temp1 - temp4: values */
1050         for (i = 0; i < 4; i++) {
1051                 data->block2[i] =
1052                         lm93_read_byte(client, LM93_REG_TEMP(i));
1053         }
1054
1055         /* prochot1 - prochot2: values */
1056         for (i = 0; i < 2; i++) {
1057                 data->block4[i].cur =
1058                         lm93_read_byte(client, LM93_REG_PROCHOT_CUR(i));
1059                 data->block4[i].avg =
1060                         lm93_read_byte(client, LM93_REG_PROCHOT_AVG(i));
1061         }
1062
1063         /* fan1 - fan4: values & limits */
1064         for (i = 0; i < 4; i++) {
1065                 data->block5[i] =
1066                         lm93_read_word(client, LM93_REG_FAN(i));
1067                 data->block8[i] =
1068                         lm93_read_word(client, LM93_REG_FAN_MIN(i));
1069         }
1070
1071         /* pwm control registers */
1072         for (i = 0; i < 2; i++) {
1073                 for (j = 0; j < 4; j++) {
1074                         data->block9[i][j] =
1075                                 lm93_read_byte(client, LM93_REG_PWM_CTL(i, j));
1076                 }
1077         }
1078
1079         /* alarm values */
1080         for (i = 0, ptr = (u8 *)(&data->block1); i < 8; i++) {
1081                 *(ptr + i) =
1082                         lm93_read_byte(client, LM93_REG_HOST_ERROR_1 + i);
1083         }
1084
1085         /* auto/pwm (base temp) registers */
1086         for (i = 0; i < 4; i++) {
1087                 data->block10.base[i] =
1088                         lm93_read_byte(client, LM93_REG_TEMP_BASE(i));
1089         }
1090
1091         /* auto/pwm (offset temp) registers */
1092         for (i = 0; i < 12; i++) {
1093                 data->block10.offset[i] =
1094                         lm93_read_byte(client, LM93_REG_TEMP_OFFSET(i));
1095         }
1096
1097         lm93_update_client_common(data, client);
1098 }
1099
1100 /* following are the sysfs callback functions */
1101 static ssize_t in_show(struct device *dev, struct device_attribute *attr,
1102                        char *buf)
1103 {
1104         int nr = (to_sensor_dev_attr(attr))->index;
1105
1106         struct lm93_data *data = lm93_update_device(dev);
1107         return sprintf(buf, "%d\n", LM93_IN_FROM_REG(nr, data->block3[nr]));
1108 }
1109
1110 static SENSOR_DEVICE_ATTR_RO(in1_input, in, 0);
1111 static SENSOR_DEVICE_ATTR_RO(in2_input, in, 1);
1112 static SENSOR_DEVICE_ATTR_RO(in3_input, in, 2);
1113 static SENSOR_DEVICE_ATTR_RO(in4_input, in, 3);
1114 static SENSOR_DEVICE_ATTR_RO(in5_input, in, 4);
1115 static SENSOR_DEVICE_ATTR_RO(in6_input, in, 5);
1116 static SENSOR_DEVICE_ATTR_RO(in7_input, in, 6);
1117 static SENSOR_DEVICE_ATTR_RO(in8_input, in, 7);
1118 static SENSOR_DEVICE_ATTR_RO(in9_input, in, 8);
1119 static SENSOR_DEVICE_ATTR_RO(in10_input, in, 9);
1120 static SENSOR_DEVICE_ATTR_RO(in11_input, in, 10);
1121 static SENSOR_DEVICE_ATTR_RO(in12_input, in, 11);
1122 static SENSOR_DEVICE_ATTR_RO(in13_input, in, 12);
1123 static SENSOR_DEVICE_ATTR_RO(in14_input, in, 13);
1124 static SENSOR_DEVICE_ATTR_RO(in15_input, in, 14);
1125 static SENSOR_DEVICE_ATTR_RO(in16_input, in, 15);
1126
1127 static ssize_t in_min_show(struct device *dev, struct device_attribute *attr,
1128                            char *buf)
1129 {
1130         int nr = (to_sensor_dev_attr(attr))->index;
1131         struct lm93_data *data = lm93_update_device(dev);
1132         int vccp = nr - 6;
1133         long rc, vid;
1134
1135         if ((nr == 6 || nr == 7) && vccp_limit_type[vccp]) {
1136                 vid = LM93_VID_FROM_REG(data->vid[vccp]);
1137                 rc = LM93_IN_MIN_FROM_REG(data->vccp_limits[vccp], vid);
1138         } else {
1139                 rc = LM93_IN_FROM_REG(nr, data->block7[nr].min);
1140         }
1141         return sprintf(buf, "%ld\n", rc);
1142 }
1143
1144 static ssize_t in_min_store(struct device *dev, struct device_attribute *attr,
1145                             const char *buf, size_t count)
1146 {
1147         int nr = (to_sensor_dev_attr(attr))->index;
1148         struct lm93_data *data = dev_get_drvdata(dev);
1149         struct i2c_client *client = data->client;
1150         int vccp = nr - 6;
1151         long vid;
1152         unsigned long val;
1153         int err;
1154
1155         err = kstrtoul(buf, 10, &val);
1156         if (err)
1157                 return err;
1158
1159         mutex_lock(&data->update_lock);
1160         if ((nr == 6 || nr == 7) && vccp_limit_type[vccp]) {
1161                 vid = LM93_VID_FROM_REG(data->vid[vccp]);
1162                 data->vccp_limits[vccp] = (data->vccp_limits[vccp] & 0xf0) |
1163                                 LM93_IN_REL_TO_REG(val, 0, vid);
1164                 lm93_write_byte(client, LM93_REG_VCCP_LIMIT_OFF(vccp),
1165                                 data->vccp_limits[vccp]);
1166         } else {
1167                 data->block7[nr].min = LM93_IN_TO_REG(nr, val);
1168                 lm93_write_byte(client, LM93_REG_IN_MIN(nr),
1169                                 data->block7[nr].min);
1170         }
1171         mutex_unlock(&data->update_lock);
1172         return count;
1173 }
1174
1175 static SENSOR_DEVICE_ATTR_RW(in1_min, in_min, 0);
1176 static SENSOR_DEVICE_ATTR_RW(in2_min, in_min, 1);
1177 static SENSOR_DEVICE_ATTR_RW(in3_min, in_min, 2);
1178 static SENSOR_DEVICE_ATTR_RW(in4_min, in_min, 3);
1179 static SENSOR_DEVICE_ATTR_RW(in5_min, in_min, 4);
1180 static SENSOR_DEVICE_ATTR_RW(in6_min, in_min, 5);
1181 static SENSOR_DEVICE_ATTR_RW(in7_min, in_min, 6);
1182 static SENSOR_DEVICE_ATTR_RW(in8_min, in_min, 7);
1183 static SENSOR_DEVICE_ATTR_RW(in9_min, in_min, 8);
1184 static SENSOR_DEVICE_ATTR_RW(in10_min, in_min, 9);
1185 static SENSOR_DEVICE_ATTR_RW(in11_min, in_min, 10);
1186 static SENSOR_DEVICE_ATTR_RW(in12_min, in_min, 11);
1187 static SENSOR_DEVICE_ATTR_RW(in13_min, in_min, 12);
1188 static SENSOR_DEVICE_ATTR_RW(in14_min, in_min, 13);
1189 static SENSOR_DEVICE_ATTR_RW(in15_min, in_min, 14);
1190 static SENSOR_DEVICE_ATTR_RW(in16_min, in_min, 15);
1191
1192 static ssize_t in_max_show(struct device *dev, struct device_attribute *attr,
1193                            char *buf)
1194 {
1195         int nr = (to_sensor_dev_attr(attr))->index;
1196         struct lm93_data *data = lm93_update_device(dev);
1197         int vccp = nr - 6;
1198         long rc, vid;
1199
1200         if ((nr == 6 || nr == 7) && vccp_limit_type[vccp]) {
1201                 vid = LM93_VID_FROM_REG(data->vid[vccp]);
1202                 rc = LM93_IN_MAX_FROM_REG(data->vccp_limits[vccp], vid);
1203         } else {
1204                 rc = LM93_IN_FROM_REG(nr, data->block7[nr].max);
1205         }
1206         return sprintf(buf, "%ld\n", rc);
1207 }
1208
1209 static ssize_t in_max_store(struct device *dev, struct device_attribute *attr,
1210                             const char *buf, size_t count)
1211 {
1212         int nr = (to_sensor_dev_attr(attr))->index;
1213         struct lm93_data *data = dev_get_drvdata(dev);
1214         struct i2c_client *client = data->client;
1215         int vccp = nr - 6;
1216         long vid;
1217         unsigned long val;
1218         int err;
1219
1220         err = kstrtoul(buf, 10, &val);
1221         if (err)
1222                 return err;
1223
1224         mutex_lock(&data->update_lock);
1225         if ((nr == 6 || nr == 7) && vccp_limit_type[vccp]) {
1226                 vid = LM93_VID_FROM_REG(data->vid[vccp]);
1227                 data->vccp_limits[vccp] = (data->vccp_limits[vccp] & 0x0f) |
1228                                 LM93_IN_REL_TO_REG(val, 1, vid);
1229                 lm93_write_byte(client, LM93_REG_VCCP_LIMIT_OFF(vccp),
1230                                 data->vccp_limits[vccp]);
1231         } else {
1232                 data->block7[nr].max = LM93_IN_TO_REG(nr, val);
1233                 lm93_write_byte(client, LM93_REG_IN_MAX(nr),
1234                                 data->block7[nr].max);
1235         }
1236         mutex_unlock(&data->update_lock);
1237         return count;
1238 }
1239
1240 static SENSOR_DEVICE_ATTR_RW(in1_max, in_max, 0);
1241 static SENSOR_DEVICE_ATTR_RW(in2_max, in_max, 1);
1242 static SENSOR_DEVICE_ATTR_RW(in3_max, in_max, 2);
1243 static SENSOR_DEVICE_ATTR_RW(in4_max, in_max, 3);
1244 static SENSOR_DEVICE_ATTR_RW(in5_max, in_max, 4);
1245 static SENSOR_DEVICE_ATTR_RW(in6_max, in_max, 5);
1246 static SENSOR_DEVICE_ATTR_RW(in7_max, in_max, 6);
1247 static SENSOR_DEVICE_ATTR_RW(in8_max, in_max, 7);
1248 static SENSOR_DEVICE_ATTR_RW(in9_max, in_max, 8);
1249 static SENSOR_DEVICE_ATTR_RW(in10_max, in_max, 9);
1250 static SENSOR_DEVICE_ATTR_RW(in11_max, in_max, 10);
1251 static SENSOR_DEVICE_ATTR_RW(in12_max, in_max, 11);
1252 static SENSOR_DEVICE_ATTR_RW(in13_max, in_max, 12);
1253 static SENSOR_DEVICE_ATTR_RW(in14_max, in_max, 13);
1254 static SENSOR_DEVICE_ATTR_RW(in15_max, in_max, 14);
1255 static SENSOR_DEVICE_ATTR_RW(in16_max, in_max, 15);
1256
1257 static ssize_t temp_show(struct device *dev, struct device_attribute *attr,
1258                          char *buf)
1259 {
1260         int nr = (to_sensor_dev_attr(attr))->index;
1261         struct lm93_data *data = lm93_update_device(dev);
1262         return sprintf(buf, "%d\n", LM93_TEMP_FROM_REG(data->block2[nr]));
1263 }
1264
1265 static SENSOR_DEVICE_ATTR_RO(temp1_input, temp, 0);
1266 static SENSOR_DEVICE_ATTR_RO(temp2_input, temp, 1);
1267 static SENSOR_DEVICE_ATTR_RO(temp3_input, temp, 2);
1268
1269 static ssize_t temp_min_show(struct device *dev,
1270                              struct device_attribute *attr, char *buf)
1271 {
1272         int nr = (to_sensor_dev_attr(attr))->index;
1273         struct lm93_data *data = lm93_update_device(dev);
1274         return sprintf(buf, "%d\n", LM93_TEMP_FROM_REG(data->temp_lim[nr].min));
1275 }
1276
1277 static ssize_t temp_min_store(struct device *dev,
1278                               struct device_attribute *attr, const char *buf,
1279                               size_t count)
1280 {
1281         int nr = (to_sensor_dev_attr(attr))->index;
1282         struct lm93_data *data = dev_get_drvdata(dev);
1283         struct i2c_client *client = data->client;
1284         long val;
1285         int err;
1286
1287         err = kstrtol(buf, 10, &val);
1288         if (err)
1289                 return err;
1290
1291         mutex_lock(&data->update_lock);
1292         data->temp_lim[nr].min = LM93_TEMP_TO_REG(val);
1293         lm93_write_byte(client, LM93_REG_TEMP_MIN(nr), data->temp_lim[nr].min);
1294         mutex_unlock(&data->update_lock);
1295         return count;
1296 }
1297
1298 static SENSOR_DEVICE_ATTR_RW(temp1_min, temp_min, 0);
1299 static SENSOR_DEVICE_ATTR_RW(temp2_min, temp_min, 1);
1300 static SENSOR_DEVICE_ATTR_RW(temp3_min, temp_min, 2);
1301
1302 static ssize_t temp_max_show(struct device *dev,
1303                              struct device_attribute *attr, char *buf)
1304 {
1305         int nr = (to_sensor_dev_attr(attr))->index;
1306         struct lm93_data *data = lm93_update_device(dev);
1307         return sprintf(buf, "%d\n", LM93_TEMP_FROM_REG(data->temp_lim[nr].max));
1308 }
1309
1310 static ssize_t temp_max_store(struct device *dev,
1311                               struct device_attribute *attr, const char *buf,
1312                               size_t count)
1313 {
1314         int nr = (to_sensor_dev_attr(attr))->index;
1315         struct lm93_data *data = dev_get_drvdata(dev);
1316         struct i2c_client *client = data->client;
1317         long val;
1318         int err;
1319
1320         err = kstrtol(buf, 10, &val);
1321         if (err)
1322                 return err;
1323
1324         mutex_lock(&data->update_lock);
1325         data->temp_lim[nr].max = LM93_TEMP_TO_REG(val);
1326         lm93_write_byte(client, LM93_REG_TEMP_MAX(nr), data->temp_lim[nr].max);
1327         mutex_unlock(&data->update_lock);
1328         return count;
1329 }
1330
1331 static SENSOR_DEVICE_ATTR_RW(temp1_max, temp_max, 0);
1332 static SENSOR_DEVICE_ATTR_RW(temp2_max, temp_max, 1);
1333 static SENSOR_DEVICE_ATTR_RW(temp3_max, temp_max, 2);
1334
1335 static ssize_t temp_auto_base_show(struct device *dev,
1336                                    struct device_attribute *attr, char *buf)
1337 {
1338         int nr = (to_sensor_dev_attr(attr))->index;
1339         struct lm93_data *data = lm93_update_device(dev);
1340         return sprintf(buf, "%d\n", LM93_TEMP_FROM_REG(data->block10.base[nr]));
1341 }
1342
1343 static ssize_t temp_auto_base_store(struct device *dev,
1344                                     struct device_attribute *attr,
1345                                     const char *buf, size_t count)
1346 {
1347         int nr = (to_sensor_dev_attr(attr))->index;
1348         struct lm93_data *data = dev_get_drvdata(dev);
1349         struct i2c_client *client = data->client;
1350         long val;
1351         int err;
1352
1353         err = kstrtol(buf, 10, &val);
1354         if (err)
1355                 return err;
1356
1357         mutex_lock(&data->update_lock);
1358         data->block10.base[nr] = LM93_TEMP_TO_REG(val);
1359         lm93_write_byte(client, LM93_REG_TEMP_BASE(nr), data->block10.base[nr]);
1360         mutex_unlock(&data->update_lock);
1361         return count;
1362 }
1363
1364 static SENSOR_DEVICE_ATTR_RW(temp1_auto_base, temp_auto_base, 0);
1365 static SENSOR_DEVICE_ATTR_RW(temp2_auto_base, temp_auto_base, 1);
1366 static SENSOR_DEVICE_ATTR_RW(temp3_auto_base, temp_auto_base, 2);
1367
1368 static ssize_t temp_auto_boost_show(struct device *dev,
1369                                     struct device_attribute *attr, char *buf)
1370 {
1371         int nr = (to_sensor_dev_attr(attr))->index;
1372         struct lm93_data *data = lm93_update_device(dev);
1373         return sprintf(buf, "%d\n", LM93_TEMP_FROM_REG(data->boost[nr]));
1374 }
1375
1376 static ssize_t temp_auto_boost_store(struct device *dev,
1377                                      struct device_attribute *attr,
1378                                      const char *buf, size_t count)
1379 {
1380         int nr = (to_sensor_dev_attr(attr))->index;
1381         struct lm93_data *data = dev_get_drvdata(dev);
1382         struct i2c_client *client = data->client;
1383         long val;
1384         int err;
1385
1386         err = kstrtol(buf, 10, &val);
1387         if (err)
1388                 return err;
1389
1390         mutex_lock(&data->update_lock);
1391         data->boost[nr] = LM93_TEMP_TO_REG(val);
1392         lm93_write_byte(client, LM93_REG_BOOST(nr), data->boost[nr]);
1393         mutex_unlock(&data->update_lock);
1394         return count;
1395 }
1396
1397 static SENSOR_DEVICE_ATTR_RW(temp1_auto_boost, temp_auto_boost, 0);
1398 static SENSOR_DEVICE_ATTR_RW(temp2_auto_boost, temp_auto_boost, 1);
1399 static SENSOR_DEVICE_ATTR_RW(temp3_auto_boost, temp_auto_boost, 2);
1400
1401 static ssize_t temp_auto_boost_hyst_show(struct device *dev,
1402                                          struct device_attribute *attr,
1403                                          char *buf)
1404 {
1405         int nr = (to_sensor_dev_attr(attr))->index;
1406         struct lm93_data *data = lm93_update_device(dev);
1407         int mode = LM93_TEMP_OFFSET_MODE_FROM_REG(data->sfc2, nr);
1408         return sprintf(buf, "%d\n",
1409                        LM93_AUTO_BOOST_HYST_FROM_REGS(data, nr, mode));
1410 }
1411
1412 static ssize_t temp_auto_boost_hyst_store(struct device *dev,
1413                                           struct device_attribute *attr,
1414                                           const char *buf, size_t count)
1415 {
1416         int nr = (to_sensor_dev_attr(attr))->index;
1417         struct lm93_data *data = dev_get_drvdata(dev);
1418         struct i2c_client *client = data->client;
1419         unsigned long val;
1420         int err;
1421
1422         err = kstrtoul(buf, 10, &val);
1423         if (err)
1424                 return err;
1425
1426         mutex_lock(&data->update_lock);
1427         /* force 0.5C/bit mode */
1428         data->sfc2 = lm93_read_byte(client, LM93_REG_SFC2);
1429         data->sfc2 |= ((nr < 2) ? 0x10 : 0x20);
1430         lm93_write_byte(client, LM93_REG_SFC2, data->sfc2);
1431         data->boost_hyst[nr/2] = LM93_AUTO_BOOST_HYST_TO_REG(data, val, nr, 1);
1432         lm93_write_byte(client, LM93_REG_BOOST_HYST(nr),
1433                         data->boost_hyst[nr/2]);
1434         mutex_unlock(&data->update_lock);
1435         return count;
1436 }
1437
1438 static SENSOR_DEVICE_ATTR_RW(temp1_auto_boost_hyst, temp_auto_boost_hyst, 0);
1439 static SENSOR_DEVICE_ATTR_RW(temp2_auto_boost_hyst, temp_auto_boost_hyst, 1);
1440 static SENSOR_DEVICE_ATTR_RW(temp3_auto_boost_hyst, temp_auto_boost_hyst, 2);
1441
1442 static ssize_t temp_auto_offset_show(struct device *dev,
1443                                      struct device_attribute *attr, char *buf)
1444 {
1445         struct sensor_device_attribute_2 *s_attr = to_sensor_dev_attr_2(attr);
1446         int nr = s_attr->index;
1447         int ofs = s_attr->nr;
1448         struct lm93_data *data = lm93_update_device(dev);
1449         int mode = LM93_TEMP_OFFSET_MODE_FROM_REG(data->sfc2, nr);
1450         return sprintf(buf, "%d\n",
1451                LM93_TEMP_AUTO_OFFSET_FROM_REG(data->block10.offset[ofs],
1452                                               nr, mode));
1453 }
1454
1455 static ssize_t temp_auto_offset_store(struct device *dev,
1456                                       struct device_attribute *attr,
1457                                       const char *buf, size_t count)
1458 {
1459         struct sensor_device_attribute_2 *s_attr = to_sensor_dev_attr_2(attr);
1460         int nr = s_attr->index;
1461         int ofs = s_attr->nr;
1462         struct lm93_data *data = dev_get_drvdata(dev);
1463         struct i2c_client *client = data->client;
1464         unsigned long val;
1465         int err;
1466
1467         err = kstrtoul(buf, 10, &val);
1468         if (err)
1469                 return err;
1470
1471         mutex_lock(&data->update_lock);
1472         /* force 0.5C/bit mode */
1473         data->sfc2 = lm93_read_byte(client, LM93_REG_SFC2);
1474         data->sfc2 |= ((nr < 2) ? 0x10 : 0x20);
1475         lm93_write_byte(client, LM93_REG_SFC2, data->sfc2);
1476         data->block10.offset[ofs] = LM93_TEMP_AUTO_OFFSET_TO_REG(
1477                         data->block10.offset[ofs], val, nr, 1);
1478         lm93_write_byte(client, LM93_REG_TEMP_OFFSET(ofs),
1479                         data->block10.offset[ofs]);
1480         mutex_unlock(&data->update_lock);
1481         return count;
1482 }
1483
1484 static SENSOR_DEVICE_ATTR_2_RW(temp1_auto_offset1, temp_auto_offset, 0, 0);
1485 static SENSOR_DEVICE_ATTR_2_RW(temp1_auto_offset2, temp_auto_offset, 1, 0);
1486 static SENSOR_DEVICE_ATTR_2_RW(temp1_auto_offset3, temp_auto_offset, 2, 0);
1487 static SENSOR_DEVICE_ATTR_2_RW(temp1_auto_offset4, temp_auto_offset, 3, 0);
1488 static SENSOR_DEVICE_ATTR_2_RW(temp1_auto_offset5, temp_auto_offset, 4, 0);
1489 static SENSOR_DEVICE_ATTR_2_RW(temp1_auto_offset6, temp_auto_offset, 5, 0);
1490 static SENSOR_DEVICE_ATTR_2_RW(temp1_auto_offset7, temp_auto_offset, 6, 0);
1491 static SENSOR_DEVICE_ATTR_2_RW(temp1_auto_offset8, temp_auto_offset, 7, 0);
1492 static SENSOR_DEVICE_ATTR_2_RW(temp1_auto_offset9, temp_auto_offset, 8, 0);
1493 static SENSOR_DEVICE_ATTR_2_RW(temp1_auto_offset10, temp_auto_offset, 9, 0);
1494 static SENSOR_DEVICE_ATTR_2_RW(temp1_auto_offset11, temp_auto_offset, 10, 0);
1495 static SENSOR_DEVICE_ATTR_2_RW(temp1_auto_offset12, temp_auto_offset, 11, 0);
1496 static SENSOR_DEVICE_ATTR_2_RW(temp2_auto_offset1, temp_auto_offset, 0, 1);
1497 static SENSOR_DEVICE_ATTR_2_RW(temp2_auto_offset2, temp_auto_offset, 1, 1);
1498 static SENSOR_DEVICE_ATTR_2_RW(temp2_auto_offset3, temp_auto_offset, 2, 1);
1499 static SENSOR_DEVICE_ATTR_2_RW(temp2_auto_offset4, temp_auto_offset, 3, 1);
1500 static SENSOR_DEVICE_ATTR_2_RW(temp2_auto_offset5, temp_auto_offset, 4, 1);
1501 static SENSOR_DEVICE_ATTR_2_RW(temp2_auto_offset6, temp_auto_offset, 5, 1);
1502 static SENSOR_DEVICE_ATTR_2_RW(temp2_auto_offset7, temp_auto_offset, 6, 1);
1503 static SENSOR_DEVICE_ATTR_2_RW(temp2_auto_offset8, temp_auto_offset, 7, 1);
1504 static SENSOR_DEVICE_ATTR_2_RW(temp2_auto_offset9, temp_auto_offset, 8, 1);
1505 static SENSOR_DEVICE_ATTR_2_RW(temp2_auto_offset10, temp_auto_offset, 9, 1);
1506 static SENSOR_DEVICE_ATTR_2_RW(temp2_auto_offset11, temp_auto_offset, 10, 1);
1507 static SENSOR_DEVICE_ATTR_2_RW(temp2_auto_offset12, temp_auto_offset, 11, 1);
1508 static SENSOR_DEVICE_ATTR_2_RW(temp3_auto_offset1, temp_auto_offset, 0, 2);
1509 static SENSOR_DEVICE_ATTR_2_RW(temp3_auto_offset2, temp_auto_offset, 1, 2);
1510 static SENSOR_DEVICE_ATTR_2_RW(temp3_auto_offset3, temp_auto_offset, 2, 2);
1511 static SENSOR_DEVICE_ATTR_2_RW(temp3_auto_offset4, temp_auto_offset, 3, 2);
1512 static SENSOR_DEVICE_ATTR_2_RW(temp3_auto_offset5, temp_auto_offset, 4, 2);
1513 static SENSOR_DEVICE_ATTR_2_RW(temp3_auto_offset6, temp_auto_offset, 5, 2);
1514 static SENSOR_DEVICE_ATTR_2_RW(temp3_auto_offset7, temp_auto_offset, 6, 2);
1515 static SENSOR_DEVICE_ATTR_2_RW(temp3_auto_offset8, temp_auto_offset, 7, 2);
1516 static SENSOR_DEVICE_ATTR_2_RW(temp3_auto_offset9, temp_auto_offset, 8, 2);
1517 static SENSOR_DEVICE_ATTR_2_RW(temp3_auto_offset10, temp_auto_offset, 9, 2);
1518 static SENSOR_DEVICE_ATTR_2_RW(temp3_auto_offset11, temp_auto_offset, 10, 2);
1519 static SENSOR_DEVICE_ATTR_2_RW(temp3_auto_offset12, temp_auto_offset, 11, 2);
1520
1521 static ssize_t temp_auto_pwm_min_show(struct device *dev,
1522                                       struct device_attribute *attr,
1523                                       char *buf)
1524 {
1525         int nr = (to_sensor_dev_attr(attr))->index;
1526         u8 reg, ctl4;
1527         struct lm93_data *data = lm93_update_device(dev);
1528         reg = data->auto_pwm_min_hyst[nr/2] >> 4 & 0x0f;
1529         ctl4 = data->block9[nr][LM93_PWM_CTL4];
1530         return sprintf(buf, "%d\n", LM93_PWM_FROM_REG(reg, (ctl4 & 0x07) ?
1531                                 LM93_PWM_MAP_LO_FREQ : LM93_PWM_MAP_HI_FREQ));
1532 }
1533
1534 static ssize_t temp_auto_pwm_min_store(struct device *dev,
1535                                        struct device_attribute *attr,
1536                                        const char *buf, size_t count)
1537 {
1538         int nr = (to_sensor_dev_attr(attr))->index;
1539         struct lm93_data *data = dev_get_drvdata(dev);
1540         struct i2c_client *client = data->client;
1541         u8 reg, ctl4;
1542         unsigned long val;
1543         int err;
1544
1545         err = kstrtoul(buf, 10, &val);
1546         if (err)
1547                 return err;
1548
1549         mutex_lock(&data->update_lock);
1550         reg = lm93_read_byte(client, LM93_REG_PWM_MIN_HYST(nr));
1551         ctl4 = lm93_read_byte(client, LM93_REG_PWM_CTL(nr, LM93_PWM_CTL4));
1552         reg = (reg & 0x0f) |
1553                 LM93_PWM_TO_REG(val, (ctl4 & 0x07) ?
1554                                 LM93_PWM_MAP_LO_FREQ :
1555                                 LM93_PWM_MAP_HI_FREQ) << 4;
1556         data->auto_pwm_min_hyst[nr/2] = reg;
1557         lm93_write_byte(client, LM93_REG_PWM_MIN_HYST(nr), reg);
1558         mutex_unlock(&data->update_lock);
1559         return count;
1560 }
1561
1562 static SENSOR_DEVICE_ATTR_RW(temp1_auto_pwm_min, temp_auto_pwm_min, 0);
1563 static SENSOR_DEVICE_ATTR_RW(temp2_auto_pwm_min, temp_auto_pwm_min, 1);
1564 static SENSOR_DEVICE_ATTR_RW(temp3_auto_pwm_min, temp_auto_pwm_min, 2);
1565
1566 static ssize_t temp_auto_offset_hyst_show(struct device *dev,
1567                                           struct device_attribute *attr,
1568                                           char *buf)
1569 {
1570         int nr = (to_sensor_dev_attr(attr))->index;
1571         struct lm93_data *data = lm93_update_device(dev);
1572         int mode = LM93_TEMP_OFFSET_MODE_FROM_REG(data->sfc2, nr);
1573         return sprintf(buf, "%d\n", LM93_TEMP_OFFSET_FROM_REG(
1574                                         data->auto_pwm_min_hyst[nr / 2], mode));
1575 }
1576
1577 static ssize_t temp_auto_offset_hyst_store(struct device *dev,
1578                                            struct device_attribute *attr,
1579                                            const char *buf, size_t count)
1580 {
1581         int nr = (to_sensor_dev_attr(attr))->index;
1582         struct lm93_data *data = dev_get_drvdata(dev);
1583         struct i2c_client *client = data->client;
1584         u8 reg;
1585         unsigned long val;
1586         int err;
1587
1588         err = kstrtoul(buf, 10, &val);
1589         if (err)
1590                 return err;
1591
1592         mutex_lock(&data->update_lock);
1593         /* force 0.5C/bit mode */
1594         data->sfc2 = lm93_read_byte(client, LM93_REG_SFC2);
1595         data->sfc2 |= ((nr < 2) ? 0x10 : 0x20);
1596         lm93_write_byte(client, LM93_REG_SFC2, data->sfc2);
1597         reg = data->auto_pwm_min_hyst[nr/2];
1598         reg = (reg & 0xf0) | (LM93_TEMP_OFFSET_TO_REG(val, 1) & 0x0f);
1599         data->auto_pwm_min_hyst[nr/2] = reg;
1600         lm93_write_byte(client, LM93_REG_PWM_MIN_HYST(nr), reg);
1601         mutex_unlock(&data->update_lock);
1602         return count;
1603 }
1604
1605 static SENSOR_DEVICE_ATTR_RW(temp1_auto_offset_hyst, temp_auto_offset_hyst, 0);
1606 static SENSOR_DEVICE_ATTR_RW(temp2_auto_offset_hyst, temp_auto_offset_hyst, 1);
1607 static SENSOR_DEVICE_ATTR_RW(temp3_auto_offset_hyst, temp_auto_offset_hyst, 2);
1608
1609 static ssize_t fan_input_show(struct device *dev,
1610                               struct device_attribute *attr, char *buf)
1611 {
1612         struct sensor_device_attribute *s_attr = to_sensor_dev_attr(attr);
1613         int nr = s_attr->index;
1614         struct lm93_data *data = lm93_update_device(dev);
1615
1616         return sprintf(buf, "%d\n", LM93_FAN_FROM_REG(data->block5[nr]));
1617 }
1618
1619 static SENSOR_DEVICE_ATTR_RO(fan1_input, fan_input, 0);
1620 static SENSOR_DEVICE_ATTR_RO(fan2_input, fan_input, 1);
1621 static SENSOR_DEVICE_ATTR_RO(fan3_input, fan_input, 2);
1622 static SENSOR_DEVICE_ATTR_RO(fan4_input, fan_input, 3);
1623
1624 static ssize_t fan_min_show(struct device *dev, struct device_attribute *attr,
1625                             char *buf)
1626 {
1627         int nr = (to_sensor_dev_attr(attr))->index;
1628         struct lm93_data *data = lm93_update_device(dev);
1629
1630         return sprintf(buf, "%d\n", LM93_FAN_FROM_REG(data->block8[nr]));
1631 }
1632
1633 static ssize_t fan_min_store(struct device *dev,
1634                              struct device_attribute *attr, const char *buf,
1635                              size_t count)
1636 {
1637         int nr = (to_sensor_dev_attr(attr))->index;
1638         struct lm93_data *data = dev_get_drvdata(dev);
1639         struct i2c_client *client = data->client;
1640         unsigned long val;
1641         int err;
1642
1643         err = kstrtoul(buf, 10, &val);
1644         if (err)
1645                 return err;
1646
1647         mutex_lock(&data->update_lock);
1648         data->block8[nr] = LM93_FAN_TO_REG(val);
1649         lm93_write_word(client, LM93_REG_FAN_MIN(nr), data->block8[nr]);
1650         mutex_unlock(&data->update_lock);
1651         return count;
1652 }
1653
1654 static SENSOR_DEVICE_ATTR_RW(fan1_min, fan_min, 0);
1655 static SENSOR_DEVICE_ATTR_RW(fan2_min, fan_min, 1);
1656 static SENSOR_DEVICE_ATTR_RW(fan3_min, fan_min, 2);
1657 static SENSOR_DEVICE_ATTR_RW(fan4_min, fan_min, 3);
1658
1659 /*
1660  * some tedious bit-twiddling here to deal with the register format:
1661  *
1662  *      data->sf_tach_to_pwm: (tach to pwm mapping bits)
1663  *
1664  *              bit |  7  |  6  |  5  |  4  |  3  |  2  |  1  |  0
1665  *                   T4:P2 T4:P1 T3:P2 T3:P1 T2:P2 T2:P1 T1:P2 T1:P1
1666  *
1667  *      data->sfc2: (enable bits)
1668  *
1669  *              bit |  3  |  2  |  1  |  0
1670  *                     T4    T3    T2    T1
1671  */
1672
1673 static ssize_t fan_smart_tach_show(struct device *dev,
1674                                    struct device_attribute *attr, char *buf)
1675 {
1676         int nr = (to_sensor_dev_attr(attr))->index;
1677         struct lm93_data *data = lm93_update_device(dev);
1678         long rc = 0;
1679         int mapping;
1680
1681         /* extract the relevant mapping */
1682         mapping = (data->sf_tach_to_pwm >> (nr * 2)) & 0x03;
1683
1684         /* if there's a mapping and it's enabled */
1685         if (mapping && ((data->sfc2 >> nr) & 0x01))
1686                 rc = mapping;
1687         return sprintf(buf, "%ld\n", rc);
1688 }
1689
1690 /*
1691  * helper function - must grab data->update_lock before calling
1692  * fan is 0-3, indicating fan1-fan4
1693  */
1694 static void lm93_write_fan_smart_tach(struct i2c_client *client,
1695         struct lm93_data *data, int fan, long value)
1696 {
1697         /* insert the new mapping and write it out */
1698         data->sf_tach_to_pwm = lm93_read_byte(client, LM93_REG_SF_TACH_TO_PWM);
1699         data->sf_tach_to_pwm &= ~(0x3 << fan * 2);
1700         data->sf_tach_to_pwm |= value << fan * 2;
1701         lm93_write_byte(client, LM93_REG_SF_TACH_TO_PWM, data->sf_tach_to_pwm);
1702
1703         /* insert the enable bit and write it out */
1704         data->sfc2 = lm93_read_byte(client, LM93_REG_SFC2);
1705         if (value)
1706                 data->sfc2 |= 1 << fan;
1707         else
1708                 data->sfc2 &= ~(1 << fan);
1709         lm93_write_byte(client, LM93_REG_SFC2, data->sfc2);
1710 }
1711
1712 static ssize_t fan_smart_tach_store(struct device *dev,
1713                                     struct device_attribute *attr,
1714                                     const char *buf, size_t count)
1715 {
1716         int nr = (to_sensor_dev_attr(attr))->index;
1717         struct lm93_data *data = dev_get_drvdata(dev);
1718         struct i2c_client *client = data->client;
1719         unsigned long val;
1720         int err;
1721
1722         err = kstrtoul(buf, 10, &val);
1723         if (err)
1724                 return err;
1725
1726         mutex_lock(&data->update_lock);
1727         /* sanity test, ignore the write otherwise */
1728         if (val <= 2) {
1729                 /* can't enable if pwm freq is 22.5KHz */
1730                 if (val) {
1731                         u8 ctl4 = lm93_read_byte(client,
1732                                 LM93_REG_PWM_CTL(val - 1, LM93_PWM_CTL4));
1733                         if ((ctl4 & 0x07) == 0)
1734                                 val = 0;
1735                 }
1736                 lm93_write_fan_smart_tach(client, data, nr, val);
1737         }
1738         mutex_unlock(&data->update_lock);
1739         return count;
1740 }
1741
1742 static SENSOR_DEVICE_ATTR_RW(fan1_smart_tach, fan_smart_tach, 0);
1743 static SENSOR_DEVICE_ATTR_RW(fan2_smart_tach, fan_smart_tach, 1);
1744 static SENSOR_DEVICE_ATTR_RW(fan3_smart_tach, fan_smart_tach, 2);
1745 static SENSOR_DEVICE_ATTR_RW(fan4_smart_tach, fan_smart_tach, 3);
1746
1747 static ssize_t pwm_show(struct device *dev, struct device_attribute *attr,
1748                         char *buf)
1749 {
1750         int nr = (to_sensor_dev_attr(attr))->index;
1751         struct lm93_data *data = lm93_update_device(dev);
1752         u8 ctl2, ctl4;
1753         long rc;
1754
1755         ctl2 = data->block9[nr][LM93_PWM_CTL2];
1756         ctl4 = data->block9[nr][LM93_PWM_CTL4];
1757         if (ctl2 & 0x01) /* show user commanded value if enabled */
1758                 rc = data->pwm_override[nr];
1759         else /* show present h/w value if manual pwm disabled */
1760                 rc = LM93_PWM_FROM_REG(ctl2 >> 4, (ctl4 & 0x07) ?
1761                         LM93_PWM_MAP_LO_FREQ : LM93_PWM_MAP_HI_FREQ);
1762         return sprintf(buf, "%ld\n", rc);
1763 }
1764
1765 static ssize_t pwm_store(struct device *dev, struct device_attribute *attr,
1766                          const char *buf, size_t count)
1767 {
1768         int nr = (to_sensor_dev_attr(attr))->index;
1769         struct lm93_data *data = dev_get_drvdata(dev);
1770         struct i2c_client *client = data->client;
1771         u8 ctl2, ctl4;
1772         unsigned long val;
1773         int err;
1774
1775         err = kstrtoul(buf, 10, &val);
1776         if (err)
1777                 return err;
1778
1779         mutex_lock(&data->update_lock);
1780         ctl2 = lm93_read_byte(client, LM93_REG_PWM_CTL(nr, LM93_PWM_CTL2));
1781         ctl4 = lm93_read_byte(client, LM93_REG_PWM_CTL(nr, LM93_PWM_CTL4));
1782         ctl2 = (ctl2 & 0x0f) | LM93_PWM_TO_REG(val, (ctl4 & 0x07) ?
1783                         LM93_PWM_MAP_LO_FREQ : LM93_PWM_MAP_HI_FREQ) << 4;
1784         /* save user commanded value */
1785         data->pwm_override[nr] = LM93_PWM_FROM_REG(ctl2 >> 4,
1786                         (ctl4 & 0x07) ?  LM93_PWM_MAP_LO_FREQ :
1787                         LM93_PWM_MAP_HI_FREQ);
1788         lm93_write_byte(client, LM93_REG_PWM_CTL(nr, LM93_PWM_CTL2), ctl2);
1789         mutex_unlock(&data->update_lock);
1790         return count;
1791 }
1792
1793 static SENSOR_DEVICE_ATTR_RW(pwm1, pwm, 0);
1794 static SENSOR_DEVICE_ATTR_RW(pwm2, pwm, 1);
1795
1796 static ssize_t pwm_enable_show(struct device *dev,
1797                                struct device_attribute *attr, char *buf)
1798 {
1799         int nr = (to_sensor_dev_attr(attr))->index;
1800         struct lm93_data *data = lm93_update_device(dev);
1801         u8 ctl2;
1802         long rc;
1803
1804         ctl2 = data->block9[nr][LM93_PWM_CTL2];
1805         if (ctl2 & 0x01) /* manual override enabled ? */
1806                 rc = ((ctl2 & 0xF0) == 0xF0) ? 0 : 1;
1807         else
1808                 rc = 2;
1809         return sprintf(buf, "%ld\n", rc);
1810 }
1811
1812 static ssize_t pwm_enable_store(struct device *dev,
1813                                 struct device_attribute *attr,
1814                                 const char *buf, size_t count)
1815 {
1816         int nr = (to_sensor_dev_attr(attr))->index;
1817         struct lm93_data *data = dev_get_drvdata(dev);
1818         struct i2c_client *client = data->client;
1819         u8 ctl2;
1820         unsigned long val;
1821         int err;
1822
1823         err = kstrtoul(buf, 10, &val);
1824         if (err)
1825                 return err;
1826
1827         mutex_lock(&data->update_lock);
1828         ctl2 = lm93_read_byte(client, LM93_REG_PWM_CTL(nr, LM93_PWM_CTL2));
1829
1830         switch (val) {
1831         case 0:
1832                 ctl2 |= 0xF1; /* enable manual override, set PWM to max */
1833                 break;
1834         case 1:
1835                 ctl2 |= 0x01; /* enable manual override */
1836                 break;
1837         case 2:
1838                 ctl2 &= ~0x01; /* disable manual override */
1839                 break;
1840         default:
1841                 mutex_unlock(&data->update_lock);
1842                 return -EINVAL;
1843         }
1844
1845         lm93_write_byte(client, LM93_REG_PWM_CTL(nr, LM93_PWM_CTL2), ctl2);
1846         mutex_unlock(&data->update_lock);
1847         return count;
1848 }
1849
1850 static SENSOR_DEVICE_ATTR_RW(pwm1_enable, pwm_enable, 0);
1851 static SENSOR_DEVICE_ATTR_RW(pwm2_enable, pwm_enable, 1);
1852
1853 static ssize_t pwm_freq_show(struct device *dev,
1854                              struct device_attribute *attr, char *buf)
1855 {
1856         int nr = (to_sensor_dev_attr(attr))->index;
1857         struct lm93_data *data = lm93_update_device(dev);
1858         u8 ctl4;
1859
1860         ctl4 = data->block9[nr][LM93_PWM_CTL4];
1861         return sprintf(buf, "%d\n", LM93_PWM_FREQ_FROM_REG(ctl4));
1862 }
1863
1864 /*
1865  * helper function - must grab data->update_lock before calling
1866  * pwm is 0-1, indicating pwm1-pwm2
1867  * this disables smart tach for all tach channels bound to the given pwm
1868  */
1869 static void lm93_disable_fan_smart_tach(struct i2c_client *client,
1870         struct lm93_data *data, int pwm)
1871 {
1872         int mapping = lm93_read_byte(client, LM93_REG_SF_TACH_TO_PWM);
1873         int mask;
1874
1875         /* collapse the mapping into a mask of enable bits */
1876         mapping = (mapping >> pwm) & 0x55;
1877         mask = mapping & 0x01;
1878         mask |= (mapping & 0x04) >> 1;
1879         mask |= (mapping & 0x10) >> 2;
1880         mask |= (mapping & 0x40) >> 3;
1881
1882         /* disable smart tach according to the mask */
1883         data->sfc2 = lm93_read_byte(client, LM93_REG_SFC2);
1884         data->sfc2 &= ~mask;
1885         lm93_write_byte(client, LM93_REG_SFC2, data->sfc2);
1886 }
1887
1888 static ssize_t pwm_freq_store(struct device *dev,
1889                               struct device_attribute *attr, const char *buf,
1890                               size_t count)
1891 {
1892         int nr = (to_sensor_dev_attr(attr))->index;
1893         struct lm93_data *data = dev_get_drvdata(dev);
1894         struct i2c_client *client = data->client;
1895         u8 ctl4;
1896         unsigned long val;
1897         int err;
1898
1899         err = kstrtoul(buf, 10, &val);
1900         if (err)
1901                 return err;
1902
1903         mutex_lock(&data->update_lock);
1904         ctl4 = lm93_read_byte(client, LM93_REG_PWM_CTL(nr, LM93_PWM_CTL4));
1905         ctl4 = (ctl4 & 0xf8) | LM93_PWM_FREQ_TO_REG(val);
1906         data->block9[nr][LM93_PWM_CTL4] = ctl4;
1907         /* ctl4 == 0 -> 22.5KHz -> disable smart tach */
1908         if (!ctl4)
1909                 lm93_disable_fan_smart_tach(client, data, nr);
1910         lm93_write_byte(client, LM93_REG_PWM_CTL(nr, LM93_PWM_CTL4), ctl4);
1911         mutex_unlock(&data->update_lock);
1912         return count;
1913 }
1914
1915 static SENSOR_DEVICE_ATTR_RW(pwm1_freq, pwm_freq, 0);
1916 static SENSOR_DEVICE_ATTR_RW(pwm2_freq, pwm_freq, 1);
1917
1918 static ssize_t pwm_auto_channels_show(struct device *dev,
1919                                       struct device_attribute *attr,
1920                                       char *buf)
1921 {
1922         int nr = (to_sensor_dev_attr(attr))->index;
1923         struct lm93_data *data = lm93_update_device(dev);
1924         return sprintf(buf, "%d\n", data->block9[nr][LM93_PWM_CTL1]);
1925 }
1926
1927 static ssize_t pwm_auto_channels_store(struct device *dev,
1928                                        struct device_attribute *attr,
1929                                        const char *buf, size_t count)
1930 {
1931         int nr = (to_sensor_dev_attr(attr))->index;
1932         struct lm93_data *data = dev_get_drvdata(dev);
1933         struct i2c_client *client = data->client;
1934         unsigned long val;
1935         int err;
1936
1937         err = kstrtoul(buf, 10, &val);
1938         if (err)
1939                 return err;
1940
1941         mutex_lock(&data->update_lock);
1942         data->block9[nr][LM93_PWM_CTL1] = clamp_val(val, 0, 255);
1943         lm93_write_byte(client, LM93_REG_PWM_CTL(nr, LM93_PWM_CTL1),
1944                                 data->block9[nr][LM93_PWM_CTL1]);
1945         mutex_unlock(&data->update_lock);
1946         return count;
1947 }
1948
1949 static SENSOR_DEVICE_ATTR_RW(pwm1_auto_channels, pwm_auto_channels, 0);
1950 static SENSOR_DEVICE_ATTR_RW(pwm2_auto_channels, pwm_auto_channels, 1);
1951
1952 static ssize_t pwm_auto_spinup_min_show(struct device *dev,
1953                                         struct device_attribute *attr,
1954                                         char *buf)
1955 {
1956         int nr = (to_sensor_dev_attr(attr))->index;
1957         struct lm93_data *data = lm93_update_device(dev);
1958         u8 ctl3, ctl4;
1959
1960         ctl3 = data->block9[nr][LM93_PWM_CTL3];
1961         ctl4 = data->block9[nr][LM93_PWM_CTL4];
1962         return sprintf(buf, "%d\n",
1963                        LM93_PWM_FROM_REG(ctl3 & 0x0f, (ctl4 & 0x07) ?
1964                         LM93_PWM_MAP_LO_FREQ : LM93_PWM_MAP_HI_FREQ));
1965 }
1966
1967 static ssize_t pwm_auto_spinup_min_store(struct device *dev,
1968                                          struct device_attribute *attr,
1969                                          const char *buf, size_t count)
1970 {
1971         int nr = (to_sensor_dev_attr(attr))->index;
1972         struct lm93_data *data = dev_get_drvdata(dev);
1973         struct i2c_client *client = data->client;
1974         u8 ctl3, ctl4;
1975         unsigned long val;
1976         int err;
1977
1978         err = kstrtoul(buf, 10, &val);
1979         if (err)
1980                 return err;
1981
1982         mutex_lock(&data->update_lock);
1983         ctl3 = lm93_read_byte(client, LM93_REG_PWM_CTL(nr, LM93_PWM_CTL3));
1984         ctl4 = lm93_read_byte(client, LM93_REG_PWM_CTL(nr, LM93_PWM_CTL4));
1985         ctl3 = (ctl3 & 0xf0) | LM93_PWM_TO_REG(val, (ctl4 & 0x07) ?
1986                         LM93_PWM_MAP_LO_FREQ :
1987                         LM93_PWM_MAP_HI_FREQ);
1988         data->block9[nr][LM93_PWM_CTL3] = ctl3;
1989         lm93_write_byte(client, LM93_REG_PWM_CTL(nr, LM93_PWM_CTL3), ctl3);
1990         mutex_unlock(&data->update_lock);
1991         return count;
1992 }
1993
1994 static SENSOR_DEVICE_ATTR_RW(pwm1_auto_spinup_min, pwm_auto_spinup_min, 0);
1995 static SENSOR_DEVICE_ATTR_RW(pwm2_auto_spinup_min, pwm_auto_spinup_min, 1);
1996
1997 static ssize_t pwm_auto_spinup_time_show(struct device *dev,
1998                                          struct device_attribute *attr,
1999                                          char *buf)
2000 {
2001         int nr = (to_sensor_dev_attr(attr))->index;
2002         struct lm93_data *data = lm93_update_device(dev);
2003         return sprintf(buf, "%d\n", LM93_SPINUP_TIME_FROM_REG(
2004                                 data->block9[nr][LM93_PWM_CTL3]));
2005 }
2006
2007 static ssize_t pwm_auto_spinup_time_store(struct device *dev,
2008                                           struct device_attribute *attr,
2009                                           const char *buf, size_t count)
2010 {
2011         int nr = (to_sensor_dev_attr(attr))->index;
2012         struct lm93_data *data = dev_get_drvdata(dev);
2013         struct i2c_client *client = data->client;
2014         u8 ctl3;
2015         unsigned long val;
2016         int err;
2017
2018         err = kstrtoul(buf, 10, &val);
2019         if (err)
2020                 return err;
2021
2022         mutex_lock(&data->update_lock);
2023         ctl3 = lm93_read_byte(client, LM93_REG_PWM_CTL(nr, LM93_PWM_CTL3));
2024         ctl3 = (ctl3 & 0x1f) | (LM93_SPINUP_TIME_TO_REG(val) << 5 & 0xe0);
2025         data->block9[nr][LM93_PWM_CTL3] = ctl3;
2026         lm93_write_byte(client, LM93_REG_PWM_CTL(nr, LM93_PWM_CTL3), ctl3);
2027         mutex_unlock(&data->update_lock);
2028         return count;
2029 }
2030
2031 static SENSOR_DEVICE_ATTR_RW(pwm1_auto_spinup_time, pwm_auto_spinup_time, 0);
2032 static SENSOR_DEVICE_ATTR_RW(pwm2_auto_spinup_time, pwm_auto_spinup_time, 1);
2033
2034 static ssize_t pwm_auto_prochot_ramp_show(struct device *dev,
2035                                 struct device_attribute *attr, char *buf)
2036 {
2037         struct lm93_data *data = lm93_update_device(dev);
2038         return sprintf(buf, "%d\n",
2039                        LM93_RAMP_FROM_REG(data->pwm_ramp_ctl >> 4 & 0x0f));
2040 }
2041
2042 static ssize_t pwm_auto_prochot_ramp_store(struct device *dev,
2043                                                 struct device_attribute *attr,
2044                                                 const char *buf, size_t count)
2045 {
2046         struct lm93_data *data = dev_get_drvdata(dev);
2047         struct i2c_client *client = data->client;
2048         u8 ramp;
2049         unsigned long val;
2050         int err;
2051
2052         err = kstrtoul(buf, 10, &val);
2053         if (err)
2054                 return err;
2055
2056         mutex_lock(&data->update_lock);
2057         ramp = lm93_read_byte(client, LM93_REG_PWM_RAMP_CTL);
2058         ramp = (ramp & 0x0f) | (LM93_RAMP_TO_REG(val) << 4 & 0xf0);
2059         lm93_write_byte(client, LM93_REG_PWM_RAMP_CTL, ramp);
2060         mutex_unlock(&data->update_lock);
2061         return count;
2062 }
2063
2064 static DEVICE_ATTR_RW(pwm_auto_prochot_ramp);
2065
2066 static ssize_t pwm_auto_vrdhot_ramp_show(struct device *dev,
2067                                 struct device_attribute *attr, char *buf)
2068 {
2069         struct lm93_data *data = lm93_update_device(dev);
2070         return sprintf(buf, "%d\n",
2071                        LM93_RAMP_FROM_REG(data->pwm_ramp_ctl & 0x0f));
2072 }
2073
2074 static ssize_t pwm_auto_vrdhot_ramp_store(struct device *dev,
2075                                                 struct device_attribute *attr,
2076                                                 const char *buf, size_t count)
2077 {
2078         struct lm93_data *data = dev_get_drvdata(dev);
2079         struct i2c_client *client = data->client;
2080         u8 ramp;
2081         unsigned long val;
2082         int err;
2083
2084         err = kstrtoul(buf, 10, &val);
2085         if (err)
2086                 return err;
2087
2088         mutex_lock(&data->update_lock);
2089         ramp = lm93_read_byte(client, LM93_REG_PWM_RAMP_CTL);
2090         ramp = (ramp & 0xf0) | (LM93_RAMP_TO_REG(val) & 0x0f);
2091         lm93_write_byte(client, LM93_REG_PWM_RAMP_CTL, ramp);
2092         mutex_unlock(&data->update_lock);
2093         return 0;
2094 }
2095
2096 static DEVICE_ATTR_RW(pwm_auto_vrdhot_ramp);
2097
2098 static ssize_t vid_show(struct device *dev, struct device_attribute *attr,
2099                         char *buf)
2100 {
2101         int nr = (to_sensor_dev_attr(attr))->index;
2102         struct lm93_data *data = lm93_update_device(dev);
2103         return sprintf(buf, "%d\n", LM93_VID_FROM_REG(data->vid[nr]));
2104 }
2105
2106 static SENSOR_DEVICE_ATTR_RO(cpu0_vid, vid, 0);
2107 static SENSOR_DEVICE_ATTR_RO(cpu1_vid, vid, 1);
2108
2109 static ssize_t prochot_show(struct device *dev, struct device_attribute *attr,
2110                             char *buf)
2111 {
2112         int nr = (to_sensor_dev_attr(attr))->index;
2113         struct lm93_data *data = lm93_update_device(dev);
2114         return sprintf(buf, "%d\n", data->block4[nr].cur);
2115 }
2116
2117 static SENSOR_DEVICE_ATTR_RO(prochot1, prochot, 0);
2118 static SENSOR_DEVICE_ATTR_RO(prochot2, prochot, 1);
2119
2120 static ssize_t prochot_avg_show(struct device *dev,
2121                                 struct device_attribute *attr, char *buf)
2122 {
2123         int nr = (to_sensor_dev_attr(attr))->index;
2124         struct lm93_data *data = lm93_update_device(dev);
2125         return sprintf(buf, "%d\n", data->block4[nr].avg);
2126 }
2127
2128 static SENSOR_DEVICE_ATTR_RO(prochot1_avg, prochot_avg, 0);
2129 static SENSOR_DEVICE_ATTR_RO(prochot2_avg, prochot_avg, 1);
2130
2131 static ssize_t prochot_max_show(struct device *dev,
2132                                 struct device_attribute *attr, char *buf)
2133 {
2134         int nr = (to_sensor_dev_attr(attr))->index;
2135         struct lm93_data *data = lm93_update_device(dev);
2136         return sprintf(buf, "%d\n", data->prochot_max[nr]);
2137 }
2138
2139 static ssize_t prochot_max_store(struct device *dev,
2140                                  struct device_attribute *attr,
2141                                  const char *buf, size_t count)
2142 {
2143         int nr = (to_sensor_dev_attr(attr))->index;
2144         struct lm93_data *data = dev_get_drvdata(dev);
2145         struct i2c_client *client = data->client;
2146         unsigned long val;
2147         int err;
2148
2149         err = kstrtoul(buf, 10, &val);
2150         if (err)
2151                 return err;
2152
2153         mutex_lock(&data->update_lock);
2154         data->prochot_max[nr] = LM93_PROCHOT_TO_REG(val);
2155         lm93_write_byte(client, LM93_REG_PROCHOT_MAX(nr),
2156                         data->prochot_max[nr]);
2157         mutex_unlock(&data->update_lock);
2158         return count;
2159 }
2160
2161 static SENSOR_DEVICE_ATTR_RW(prochot1_max, prochot_max, 0);
2162 static SENSOR_DEVICE_ATTR_RW(prochot2_max, prochot_max, 1);
2163
2164 static const u8 prochot_override_mask[] = { 0x80, 0x40 };
2165
2166 static ssize_t prochot_override_show(struct device *dev,
2167                                      struct device_attribute *attr, char *buf)
2168 {
2169         int nr = (to_sensor_dev_attr(attr))->index;
2170         struct lm93_data *data = lm93_update_device(dev);
2171         return sprintf(buf, "%d\n",
2172                 (data->prochot_override & prochot_override_mask[nr]) ? 1 : 0);
2173 }
2174
2175 static ssize_t prochot_override_store(struct device *dev,
2176                                       struct device_attribute *attr,
2177                                       const char *buf, size_t count)
2178 {
2179         int nr = (to_sensor_dev_attr(attr))->index;
2180         struct lm93_data *data = dev_get_drvdata(dev);
2181         struct i2c_client *client = data->client;
2182         unsigned long val;
2183         int err;
2184
2185         err = kstrtoul(buf, 10, &val);
2186         if (err)
2187                 return err;
2188
2189         mutex_lock(&data->update_lock);
2190         if (val)
2191                 data->prochot_override |= prochot_override_mask[nr];
2192         else
2193                 data->prochot_override &= (~prochot_override_mask[nr]);
2194         lm93_write_byte(client, LM93_REG_PROCHOT_OVERRIDE,
2195                         data->prochot_override);
2196         mutex_unlock(&data->update_lock);
2197         return count;
2198 }
2199
2200 static SENSOR_DEVICE_ATTR_RW(prochot1_override, prochot_override, 0);
2201 static SENSOR_DEVICE_ATTR_RW(prochot2_override, prochot_override, 1);
2202
2203 static ssize_t prochot_interval_show(struct device *dev,
2204                                      struct device_attribute *attr, char *buf)
2205 {
2206         int nr = (to_sensor_dev_attr(attr))->index;
2207         struct lm93_data *data = lm93_update_device(dev);
2208         u8 tmp;
2209         if (nr == 1)
2210                 tmp = (data->prochot_interval & 0xf0) >> 4;
2211         else
2212                 tmp = data->prochot_interval & 0x0f;
2213         return sprintf(buf, "%d\n", LM93_INTERVAL_FROM_REG(tmp));
2214 }
2215
2216 static ssize_t prochot_interval_store(struct device *dev,
2217                                       struct device_attribute *attr,
2218                                       const char *buf, size_t count)
2219 {
2220         int nr = (to_sensor_dev_attr(attr))->index;
2221         struct lm93_data *data = dev_get_drvdata(dev);
2222         struct i2c_client *client = data->client;
2223         u8 tmp;
2224         unsigned long val;
2225         int err;
2226
2227         err = kstrtoul(buf, 10, &val);
2228         if (err)
2229                 return err;
2230
2231         mutex_lock(&data->update_lock);
2232         tmp = lm93_read_byte(client, LM93_REG_PROCHOT_INTERVAL);
2233         if (nr == 1)
2234                 tmp = (tmp & 0x0f) | (LM93_INTERVAL_TO_REG(val) << 4);
2235         else
2236                 tmp = (tmp & 0xf0) | LM93_INTERVAL_TO_REG(val);
2237         data->prochot_interval = tmp;
2238         lm93_write_byte(client, LM93_REG_PROCHOT_INTERVAL, tmp);
2239         mutex_unlock(&data->update_lock);
2240         return count;
2241 }
2242
2243 static SENSOR_DEVICE_ATTR_RW(prochot1_interval, prochot_interval, 0);
2244 static SENSOR_DEVICE_ATTR_RW(prochot2_interval, prochot_interval, 1);
2245
2246 static ssize_t prochot_override_duty_cycle_show(struct device *dev,
2247                                                 struct device_attribute *attr,
2248                                                 char *buf)
2249 {
2250         struct lm93_data *data = lm93_update_device(dev);
2251         return sprintf(buf, "%d\n", data->prochot_override & 0x0f);
2252 }
2253
2254 static ssize_t prochot_override_duty_cycle_store(struct device *dev,
2255                                                 struct device_attribute *attr,
2256                                                 const char *buf, size_t count)
2257 {
2258         struct lm93_data *data = dev_get_drvdata(dev);
2259         struct i2c_client *client = data->client;
2260         unsigned long val;
2261         int err;
2262
2263         err = kstrtoul(buf, 10, &val);
2264         if (err)
2265                 return err;
2266
2267         mutex_lock(&data->update_lock);
2268         data->prochot_override = (data->prochot_override & 0xf0) |
2269                                         clamp_val(val, 0, 15);
2270         lm93_write_byte(client, LM93_REG_PROCHOT_OVERRIDE,
2271                         data->prochot_override);
2272         mutex_unlock(&data->update_lock);
2273         return count;
2274 }
2275
2276 static DEVICE_ATTR_RW(prochot_override_duty_cycle);
2277
2278 static ssize_t prochot_short_show(struct device *dev,
2279                                 struct device_attribute *attr, char *buf)
2280 {
2281         struct lm93_data *data = lm93_update_device(dev);
2282         return sprintf(buf, "%d\n", (data->config & 0x10) ? 1 : 0);
2283 }
2284
2285 static ssize_t prochot_short_store(struct device *dev,
2286                                         struct device_attribute *attr,
2287                                         const char *buf, size_t count)
2288 {
2289         struct lm93_data *data = dev_get_drvdata(dev);
2290         struct i2c_client *client = data->client;
2291         unsigned long val;
2292         int err;
2293
2294         err = kstrtoul(buf, 10, &val);
2295         if (err)
2296                 return err;
2297
2298         mutex_lock(&data->update_lock);
2299         if (val)
2300                 data->config |= 0x10;
2301         else
2302                 data->config &= ~0x10;
2303         lm93_write_byte(client, LM93_REG_CONFIG, data->config);
2304         mutex_unlock(&data->update_lock);
2305         return count;
2306 }
2307
2308 static DEVICE_ATTR_RW(prochot_short);
2309
2310 static ssize_t vrdhot_show(struct device *dev, struct device_attribute *attr,
2311                            char *buf)
2312 {
2313         int nr = (to_sensor_dev_attr(attr))->index;
2314         struct lm93_data *data = lm93_update_device(dev);
2315         return sprintf(buf, "%d\n",
2316                        data->block1.host_status_1 & (1 << (nr + 4)) ? 1 : 0);
2317 }
2318
2319 static SENSOR_DEVICE_ATTR_RO(vrdhot1, vrdhot, 0);
2320 static SENSOR_DEVICE_ATTR_RO(vrdhot2, vrdhot, 1);
2321
2322 static ssize_t gpio_show(struct device *dev, struct device_attribute *attr,
2323                                 char *buf)
2324 {
2325         struct lm93_data *data = lm93_update_device(dev);
2326         return sprintf(buf, "%d\n", LM93_GPI_FROM_REG(data->gpi));
2327 }
2328
2329 static DEVICE_ATTR_RO(gpio);
2330
2331 static ssize_t alarms_show(struct device *dev, struct device_attribute *attr,
2332                                 char *buf)
2333 {
2334         struct lm93_data *data = lm93_update_device(dev);
2335         return sprintf(buf, "%d\n", LM93_ALARMS_FROM_REG(data->block1));
2336 }
2337
2338 static DEVICE_ATTR_RO(alarms);
2339
2340 static struct attribute *lm93_attrs[] = {
2341         &sensor_dev_attr_in1_input.dev_attr.attr,
2342         &sensor_dev_attr_in2_input.dev_attr.attr,
2343         &sensor_dev_attr_in3_input.dev_attr.attr,
2344         &sensor_dev_attr_in4_input.dev_attr.attr,
2345         &sensor_dev_attr_in5_input.dev_attr.attr,
2346         &sensor_dev_attr_in6_input.dev_attr.attr,
2347         &sensor_dev_attr_in7_input.dev_attr.attr,
2348         &sensor_dev_attr_in8_input.dev_attr.attr,
2349         &sensor_dev_attr_in9_input.dev_attr.attr,
2350         &sensor_dev_attr_in10_input.dev_attr.attr,
2351         &sensor_dev_attr_in11_input.dev_attr.attr,
2352         &sensor_dev_attr_in12_input.dev_attr.attr,
2353         &sensor_dev_attr_in13_input.dev_attr.attr,
2354         &sensor_dev_attr_in14_input.dev_attr.attr,
2355         &sensor_dev_attr_in15_input.dev_attr.attr,
2356         &sensor_dev_attr_in16_input.dev_attr.attr,
2357         &sensor_dev_attr_in1_min.dev_attr.attr,
2358         &sensor_dev_attr_in2_min.dev_attr.attr,
2359         &sensor_dev_attr_in3_min.dev_attr.attr,
2360         &sensor_dev_attr_in4_min.dev_attr.attr,
2361         &sensor_dev_attr_in5_min.dev_attr.attr,
2362         &sensor_dev_attr_in6_min.dev_attr.attr,
2363         &sensor_dev_attr_in7_min.dev_attr.attr,
2364         &sensor_dev_attr_in8_min.dev_attr.attr,
2365         &sensor_dev_attr_in9_min.dev_attr.attr,
2366         &sensor_dev_attr_in10_min.dev_attr.attr,
2367         &sensor_dev_attr_in11_min.dev_attr.attr,
2368         &sensor_dev_attr_in12_min.dev_attr.attr,
2369         &sensor_dev_attr_in13_min.dev_attr.attr,
2370         &sensor_dev_attr_in14_min.dev_attr.attr,
2371         &sensor_dev_attr_in15_min.dev_attr.attr,
2372         &sensor_dev_attr_in16_min.dev_attr.attr,
2373         &sensor_dev_attr_in1_max.dev_attr.attr,
2374         &sensor_dev_attr_in2_max.dev_attr.attr,
2375         &sensor_dev_attr_in3_max.dev_attr.attr,
2376         &sensor_dev_attr_in4_max.dev_attr.attr,
2377         &sensor_dev_attr_in5_max.dev_attr.attr,
2378         &sensor_dev_attr_in6_max.dev_attr.attr,
2379         &sensor_dev_attr_in7_max.dev_attr.attr,
2380         &sensor_dev_attr_in8_max.dev_attr.attr,
2381         &sensor_dev_attr_in9_max.dev_attr.attr,
2382         &sensor_dev_attr_in10_max.dev_attr.attr,
2383         &sensor_dev_attr_in11_max.dev_attr.attr,
2384         &sensor_dev_attr_in12_max.dev_attr.attr,
2385         &sensor_dev_attr_in13_max.dev_attr.attr,
2386         &sensor_dev_attr_in14_max.dev_attr.attr,
2387         &sensor_dev_attr_in15_max.dev_attr.attr,
2388         &sensor_dev_attr_in16_max.dev_attr.attr,
2389         &sensor_dev_attr_temp1_input.dev_attr.attr,
2390         &sensor_dev_attr_temp2_input.dev_attr.attr,
2391         &sensor_dev_attr_temp3_input.dev_attr.attr,
2392         &sensor_dev_attr_temp1_min.dev_attr.attr,
2393         &sensor_dev_attr_temp2_min.dev_attr.attr,
2394         &sensor_dev_attr_temp3_min.dev_attr.attr,
2395         &sensor_dev_attr_temp1_max.dev_attr.attr,
2396         &sensor_dev_attr_temp2_max.dev_attr.attr,
2397         &sensor_dev_attr_temp3_max.dev_attr.attr,
2398         &sensor_dev_attr_temp1_auto_base.dev_attr.attr,
2399         &sensor_dev_attr_temp2_auto_base.dev_attr.attr,
2400         &sensor_dev_attr_temp3_auto_base.dev_attr.attr,
2401         &sensor_dev_attr_temp1_auto_boost.dev_attr.attr,
2402         &sensor_dev_attr_temp2_auto_boost.dev_attr.attr,
2403         &sensor_dev_attr_temp3_auto_boost.dev_attr.attr,
2404         &sensor_dev_attr_temp1_auto_boost_hyst.dev_attr.attr,
2405         &sensor_dev_attr_temp2_auto_boost_hyst.dev_attr.attr,
2406         &sensor_dev_attr_temp3_auto_boost_hyst.dev_attr.attr,
2407         &sensor_dev_attr_temp1_auto_offset1.dev_attr.attr,
2408         &sensor_dev_attr_temp1_auto_offset2.dev_attr.attr,
2409         &sensor_dev_attr_temp1_auto_offset3.dev_attr.attr,
2410         &sensor_dev_attr_temp1_auto_offset4.dev_attr.attr,
2411         &sensor_dev_attr_temp1_auto_offset5.dev_attr.attr,
2412         &sensor_dev_attr_temp1_auto_offset6.dev_attr.attr,
2413         &sensor_dev_attr_temp1_auto_offset7.dev_attr.attr,
2414         &sensor_dev_attr_temp1_auto_offset8.dev_attr.attr,
2415         &sensor_dev_attr_temp1_auto_offset9.dev_attr.attr,
2416         &sensor_dev_attr_temp1_auto_offset10.dev_attr.attr,
2417         &sensor_dev_attr_temp1_auto_offset11.dev_attr.attr,
2418         &sensor_dev_attr_temp1_auto_offset12.dev_attr.attr,
2419         &sensor_dev_attr_temp2_auto_offset1.dev_attr.attr,
2420         &sensor_dev_attr_temp2_auto_offset2.dev_attr.attr,
2421         &sensor_dev_attr_temp2_auto_offset3.dev_attr.attr,
2422         &sensor_dev_attr_temp2_auto_offset4.dev_attr.attr,
2423         &sensor_dev_attr_temp2_auto_offset5.dev_attr.attr,
2424         &sensor_dev_attr_temp2_auto_offset6.dev_attr.attr,
2425         &sensor_dev_attr_temp2_auto_offset7.dev_attr.attr,
2426         &sensor_dev_attr_temp2_auto_offset8.dev_attr.attr,
2427         &sensor_dev_attr_temp2_auto_offset9.dev_attr.attr,
2428         &sensor_dev_attr_temp2_auto_offset10.dev_attr.attr,
2429         &sensor_dev_attr_temp2_auto_offset11.dev_attr.attr,
2430         &sensor_dev_attr_temp2_auto_offset12.dev_attr.attr,
2431         &sensor_dev_attr_temp3_auto_offset1.dev_attr.attr,
2432         &sensor_dev_attr_temp3_auto_offset2.dev_attr.attr,
2433         &sensor_dev_attr_temp3_auto_offset3.dev_attr.attr,
2434         &sensor_dev_attr_temp3_auto_offset4.dev_attr.attr,
2435         &sensor_dev_attr_temp3_auto_offset5.dev_attr.attr,
2436         &sensor_dev_attr_temp3_auto_offset6.dev_attr.attr,
2437         &sensor_dev_attr_temp3_auto_offset7.dev_attr.attr,
2438         &sensor_dev_attr_temp3_auto_offset8.dev_attr.attr,
2439         &sensor_dev_attr_temp3_auto_offset9.dev_attr.attr,
2440         &sensor_dev_attr_temp3_auto_offset10.dev_attr.attr,
2441         &sensor_dev_attr_temp3_auto_offset11.dev_attr.attr,
2442         &sensor_dev_attr_temp3_auto_offset12.dev_attr.attr,
2443         &sensor_dev_attr_temp1_auto_pwm_min.dev_attr.attr,
2444         &sensor_dev_attr_temp2_auto_pwm_min.dev_attr.attr,
2445         &sensor_dev_attr_temp3_auto_pwm_min.dev_attr.attr,
2446         &sensor_dev_attr_temp1_auto_offset_hyst.dev_attr.attr,
2447         &sensor_dev_attr_temp2_auto_offset_hyst.dev_attr.attr,
2448         &sensor_dev_attr_temp3_auto_offset_hyst.dev_attr.attr,
2449         &sensor_dev_attr_fan1_input.dev_attr.attr,
2450         &sensor_dev_attr_fan2_input.dev_attr.attr,
2451         &sensor_dev_attr_fan3_input.dev_attr.attr,
2452         &sensor_dev_attr_fan4_input.dev_attr.attr,
2453         &sensor_dev_attr_fan1_min.dev_attr.attr,
2454         &sensor_dev_attr_fan2_min.dev_attr.attr,
2455         &sensor_dev_attr_fan3_min.dev_attr.attr,
2456         &sensor_dev_attr_fan4_min.dev_attr.attr,
2457         &sensor_dev_attr_fan1_smart_tach.dev_attr.attr,
2458         &sensor_dev_attr_fan2_smart_tach.dev_attr.attr,
2459         &sensor_dev_attr_fan3_smart_tach.dev_attr.attr,
2460         &sensor_dev_attr_fan4_smart_tach.dev_attr.attr,
2461         &sensor_dev_attr_pwm1.dev_attr.attr,
2462         &sensor_dev_attr_pwm2.dev_attr.attr,
2463         &sensor_dev_attr_pwm1_enable.dev_attr.attr,
2464         &sensor_dev_attr_pwm2_enable.dev_attr.attr,
2465         &sensor_dev_attr_pwm1_freq.dev_attr.attr,
2466         &sensor_dev_attr_pwm2_freq.dev_attr.attr,
2467         &sensor_dev_attr_pwm1_auto_channels.dev_attr.attr,
2468         &sensor_dev_attr_pwm2_auto_channels.dev_attr.attr,
2469         &sensor_dev_attr_pwm1_auto_spinup_min.dev_attr.attr,
2470         &sensor_dev_attr_pwm2_auto_spinup_min.dev_attr.attr,
2471         &sensor_dev_attr_pwm1_auto_spinup_time.dev_attr.attr,
2472         &sensor_dev_attr_pwm2_auto_spinup_time.dev_attr.attr,
2473         &dev_attr_pwm_auto_prochot_ramp.attr,
2474         &dev_attr_pwm_auto_vrdhot_ramp.attr,
2475         &sensor_dev_attr_cpu0_vid.dev_attr.attr,
2476         &sensor_dev_attr_cpu1_vid.dev_attr.attr,
2477         &sensor_dev_attr_prochot1.dev_attr.attr,
2478         &sensor_dev_attr_prochot2.dev_attr.attr,
2479         &sensor_dev_attr_prochot1_avg.dev_attr.attr,
2480         &sensor_dev_attr_prochot2_avg.dev_attr.attr,
2481         &sensor_dev_attr_prochot1_max.dev_attr.attr,
2482         &sensor_dev_attr_prochot2_max.dev_attr.attr,
2483         &sensor_dev_attr_prochot1_override.dev_attr.attr,
2484         &sensor_dev_attr_prochot2_override.dev_attr.attr,
2485         &sensor_dev_attr_prochot1_interval.dev_attr.attr,
2486         &sensor_dev_attr_prochot2_interval.dev_attr.attr,
2487         &dev_attr_prochot_override_duty_cycle.attr,
2488         &dev_attr_prochot_short.attr,
2489         &sensor_dev_attr_vrdhot1.dev_attr.attr,
2490         &sensor_dev_attr_vrdhot2.dev_attr.attr,
2491         &dev_attr_gpio.attr,
2492         &dev_attr_alarms.attr,
2493         NULL
2494 };
2495
2496 ATTRIBUTE_GROUPS(lm93);
2497
2498 static void lm93_init_client(struct i2c_client *client)
2499 {
2500         int i;
2501         u8 reg;
2502
2503         /* configure VID pin input thresholds */
2504         reg = lm93_read_byte(client, LM93_REG_GPI_VID_CTL);
2505         lm93_write_byte(client, LM93_REG_GPI_VID_CTL,
2506                         reg | (vid_agtl ? 0x03 : 0x00));
2507
2508         if (init) {
2509                 /* enable #ALERT pin */
2510                 reg = lm93_read_byte(client, LM93_REG_CONFIG);
2511                 lm93_write_byte(client, LM93_REG_CONFIG, reg | 0x08);
2512
2513                 /* enable ASF mode for BMC status registers */
2514                 reg = lm93_read_byte(client, LM93_REG_STATUS_CONTROL);
2515                 lm93_write_byte(client, LM93_REG_STATUS_CONTROL, reg | 0x02);
2516
2517                 /* set sleep state to S0 */
2518                 lm93_write_byte(client, LM93_REG_SLEEP_CONTROL, 0);
2519
2520                 /* unmask #VRDHOT and dynamic VCCP (if nec) error events */
2521                 reg = lm93_read_byte(client, LM93_REG_MISC_ERR_MASK);
2522                 reg &= ~0x03;
2523                 reg &= ~(vccp_limit_type[0] ? 0x10 : 0);
2524                 reg &= ~(vccp_limit_type[1] ? 0x20 : 0);
2525                 lm93_write_byte(client, LM93_REG_MISC_ERR_MASK, reg);
2526         }
2527
2528         /* start monitoring */
2529         reg = lm93_read_byte(client, LM93_REG_CONFIG);
2530         lm93_write_byte(client, LM93_REG_CONFIG, reg | 0x01);
2531
2532         /* spin until ready */
2533         for (i = 0; i < 20; i++) {
2534                 msleep(10);
2535                 if ((lm93_read_byte(client, LM93_REG_CONFIG) & 0x80) == 0x80)
2536                         return;
2537         }
2538
2539         dev_warn(&client->dev,
2540                  "timed out waiting for sensor chip to signal ready!\n");
2541 }
2542
2543 /* Return 0 if detection is successful, -ENODEV otherwise */
2544 static int lm93_detect(struct i2c_client *client, struct i2c_board_info *info)
2545 {
2546         struct i2c_adapter *adapter = client->adapter;
2547         int mfr, ver;
2548         const char *name;
2549
2550         if (!i2c_check_functionality(adapter, LM93_SMBUS_FUNC_MIN))
2551                 return -ENODEV;
2552
2553         /* detection */
2554         mfr = lm93_read_byte(client, LM93_REG_MFR_ID);
2555         if (mfr != 0x01) {
2556                 dev_dbg(&adapter->dev,
2557                         "detect failed, bad manufacturer id 0x%02x!\n", mfr);
2558                 return -ENODEV;
2559         }
2560
2561         ver = lm93_read_byte(client, LM93_REG_VER);
2562         switch (ver) {
2563         case LM93_MFR_ID:
2564         case LM93_MFR_ID_PROTOTYPE:
2565                 name = "lm93";
2566                 break;
2567         case LM94_MFR_ID_2:
2568         case LM94_MFR_ID:
2569         case LM94_MFR_ID_PROTOTYPE:
2570                 name = "lm94";
2571                 break;
2572         default:
2573                 dev_dbg(&adapter->dev,
2574                         "detect failed, bad version id 0x%02x!\n", ver);
2575                 return -ENODEV;
2576         }
2577
2578         strscpy(info->type, name, I2C_NAME_SIZE);
2579         dev_dbg(&adapter->dev, "loading %s at %d, 0x%02x\n",
2580                 client->name, i2c_adapter_id(client->adapter),
2581                 client->addr);
2582
2583         return 0;
2584 }
2585
2586 static int lm93_probe(struct i2c_client *client)
2587 {
2588         struct device *dev = &client->dev;
2589         struct lm93_data *data;
2590         struct device *hwmon_dev;
2591         int func;
2592         void (*update)(struct lm93_data *, struct i2c_client *);
2593
2594         /* choose update routine based on bus capabilities */
2595         func = i2c_get_functionality(client->adapter);
2596         if (((LM93_SMBUS_FUNC_FULL & func) == LM93_SMBUS_FUNC_FULL) &&
2597                         (!disable_block)) {
2598                 dev_dbg(dev, "using SMBus block data transactions\n");
2599                 update = lm93_update_client_full;
2600         } else if ((LM93_SMBUS_FUNC_MIN & func) == LM93_SMBUS_FUNC_MIN) {
2601                 dev_dbg(dev, "disabled SMBus block data transactions\n");
2602                 update = lm93_update_client_min;
2603         } else {
2604                 dev_dbg(dev, "detect failed, smbus byte and/or word data not supported!\n");
2605                 return -ENODEV;
2606         }
2607
2608         data = devm_kzalloc(dev, sizeof(struct lm93_data), GFP_KERNEL);
2609         if (!data)
2610                 return -ENOMEM;
2611
2612         /* housekeeping */
2613         data->client = client;
2614         data->update = update;
2615         mutex_init(&data->update_lock);
2616
2617         /* initialize the chip */
2618         lm93_init_client(client);
2619
2620         hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
2621                                                            data,
2622                                                            lm93_groups);
2623         return PTR_ERR_OR_ZERO(hwmon_dev);
2624 }
2625
2626 static const struct i2c_device_id lm93_id[] = {
2627         { "lm93", 0 },
2628         { "lm94", 0 },
2629         { }
2630 };
2631 MODULE_DEVICE_TABLE(i2c, lm93_id);
2632
2633 static struct i2c_driver lm93_driver = {
2634         .class          = I2C_CLASS_HWMON,
2635         .driver = {
2636                 .name   = "lm93",
2637         },
2638         .probe          = lm93_probe,
2639         .id_table       = lm93_id,
2640         .detect         = lm93_detect,
2641         .address_list   = normal_i2c,
2642 };
2643
2644 module_i2c_driver(lm93_driver);
2645
2646 MODULE_AUTHOR("Mark M. Hoffman <mhoffman@lightlink.com>, "
2647                 "Hans J. Koch <hjk@hansjkoch.de>");
2648 MODULE_DESCRIPTION("LM93 driver");
2649 MODULE_LICENSE("GPL");