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