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