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