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