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