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