blob: 77a0a83399b346d530051e1ebb15a1e20fe53952 [file] [log] [blame]
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02001/*
Guenter Roeck2804a4cf2012-01-14 21:25:54 -08002 * 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:
Jean Delvare7c81c602014-01-29 20:40:08 +010015 * Copyright (c) 2003-2004 Jean Delvare <jdelvare@suse.de>
Guenter Roeck2804a4cf2012-01-14 21:25:54 -080016 *
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 */
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +020040
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>
Jean Delvaredcd8f392012-10-10 15:25:56 +020050#include <linux/jiffies.h>
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +020051
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) */
Guenter Roeck2804a4cf2012-01-14 21:25:54 -080087#define LM93_REG_PWM_CTL(nr, reg) (0xc8 + (reg) + (nr) * 4)
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +020088#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
Guenter Roeckc7bf71c2011-01-17 12:48:20 -0800139/* LM94 REGISTER VALUES */
140#define LM94_MFR_ID_2 0x7a
141#define LM94_MFR_ID 0x79
142#define LM94_MFR_ID_PROTOTYPE 0x78
143
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +0200144/* 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 */
Mark M. Hoffman25e9c862008-02-17 22:28:03 -0500151static const unsigned short normal_i2c[] = { 0x2c, 0x2d, 0x2e, I2C_CLIENT_END };
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +0200152
153/* Insmod parameters */
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +0200154
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030155static bool disable_block;
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +0200156module_param(disable_block, bool, 0);
157MODULE_PARM_DESC(disable_block,
158 "Set to non-zero to disable SMBus block data transactions.");
159
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030160static bool init;
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +0200161module_param(init, bool, 0);
162MODULE_PARM_DESC(init, "Set to non-zero to force chip initialization.");
163
Guenter Roeck2804a4cf2012-01-14 21:25:54 -0800164static int vccp_limit_type[2] = {0, 0};
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +0200165module_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
Guenter Roeck2804a4cf2012-01-14 21:25:54 -0800191/*
192 * ALARMS: SYSCTL format described further below
193 * REG: 64 bits in 8 registers, as immediately below
194 */
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +0200195struct 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 {
Axel Lin6579d582014-07-02 20:36:50 +0800210 struct i2c_client *client;
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +0200211
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
Guenter Roeck2804a4cf2012-01-14 21:25:54 -0800223 /*
224 * temp1 - temp4: unfiltered readings
225 * temp1 - temp2: filtered readings
226 */
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +0200227 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;
Hans-Jürgen Kochf08a3482007-07-23 09:36:57 +0200245 } temp_lim[4];
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +0200246
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
Guenter Roeck2804a4cf2012-01-14 21:25:54 -0800303 /*
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 */
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +0200308 u8 pwm_override[2];
309};
310
Guenter Roeck2804a4cf2012-01-14 21:25:54 -0800311/*
312 * VID: mV
313 * REG: 6-bits, right justified, *always* using Intel VRM/VRD 10
314 */
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +0200315static 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};
Guenter Roeck2804a4cf2012-01-14 21:25:54 -0800329/*
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 */
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +0200336
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};
Guenter Roeck2804a4cf2012-01-14 21:25:54 -0800347/*
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 */
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +0200354
355static unsigned LM93_IN_FROM_REG(int nr, u8 reg)
356{
Guenter Roeck088ce2a2013-03-13 16:40:39 -0700357 const long uv_max = lm93_vin_val_max[nr] * 1000;
358 const long uv_min = lm93_vin_val_min[nr] * 1000;
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +0200359
Guenter Roeck088ce2a2013-03-13 16:40:39 -0700360 const long slope = (uv_max - uv_min) /
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +0200361 (lm93_vin_reg_max[nr] - lm93_vin_reg_min[nr]);
Guenter Roeck088ce2a2013-03-13 16:40:39 -0700362 const long intercept = uv_min - slope * lm93_vin_reg_min[nr];
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +0200363
364 return (slope * reg + intercept + 500) / 1000;
365}
366
Guenter Roeck2804a4cf2012-01-14 21:25:54 -0800367/*
368 * IN: mV, limits determined by channel nr
369 * REG: scaling determined by channel nr
370 */
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +0200371static u8 LM93_IN_TO_REG(int nr, unsigned val)
372{
373 /* range limit */
Guenter Roeck088ce2a2013-03-13 16:40:39 -0700374 const long mv = clamp_val(val,
Guenter Roeck2a844c12013-01-09 08:09:34 -0800375 lm93_vin_val_min[nr], lm93_vin_val_max[nr]);
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +0200376
377 /* try not to lose too much precision here */
Guenter Roeck088ce2a2013-03-13 16:40:39 -0700378 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;
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +0200381
382 /* convert */
Guenter Roeck088ce2a2013-03-13 16:40:39 -0700383 const long slope = (uv_max - uv_min) /
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +0200384 (lm93_vin_reg_max[nr] - lm93_vin_reg_min[nr]);
Guenter Roeck088ce2a2013-03-13 16:40:39 -0700385 const long intercept = uv_min - slope * lm93_vin_reg_min[nr];
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +0200386
Guenter Roeck088ce2a2013-03-13 16:40:39 -0700387 u8 result = ((uv - intercept + (slope/2)) / slope);
Guenter Roeck2a844c12013-01-09 08:09:34 -0800388 result = clamp_val(result,
389 lm93_vin_reg_min[nr], lm93_vin_reg_max[nr]);
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +0200390 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{
Guenter Roeck088ce2a2013-03-13 16:40:39 -0700396 const long uv_offset = upper ? (((reg >> 4 & 0x0f) + 1) * 12500) :
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +0200397 (((reg >> 0 & 0x0f) + 1) * -25000);
Guenter Roeck088ce2a2013-03-13 16:40:39 -0700398 const long uv_vid = vid * 1000;
399 return (uv_vid + uv_offset + 5000) / 10000;
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +0200400}
401
Guenter Roeck2804a4cf2012-01-14 21:25:54 -0800402#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))
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +0200404
Guenter Roeck2804a4cf2012-01-14 21:25:54 -0800405/*
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 */
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +0200410static u8 LM93_IN_REL_TO_REG(unsigned val, int upper, int vid)
411{
Guenter Roeck088ce2a2013-03-13 16:40:39 -0700412 long uv_offset = vid * 1000 - val * 10000;
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +0200413 if (upper) {
Guenter Roeck088ce2a2013-03-13 16:40:39 -0700414 uv_offset = clamp_val(uv_offset, 12500, 200000);
415 return (u8)((uv_offset / 12500 - 1) << 4);
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +0200416 } else {
Guenter Roeck088ce2a2013-03-13 16:40:39 -0700417 uv_offset = clamp_val(uv_offset, -400000, -25000);
418 return (u8)((uv_offset / -25000 - 1) << 0);
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +0200419 }
420}
421
Guenter Roeck2804a4cf2012-01-14 21:25:54 -0800422/*
423 * TEMP: 1/1000 degrees C (-128C to +127C)
424 * REG: 1C/bit, two's complement
425 */
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +0200426static int LM93_TEMP_FROM_REG(u8 reg)
427{
428 return (s8)reg * 1000;
429}
430
431#define LM93_TEMP_MIN (-128000)
Guenter Roeck2804a4cf2012-01-14 21:25:54 -0800432#define LM93_TEMP_MAX (127000)
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +0200433
Guenter Roeck2804a4cf2012-01-14 21:25:54 -0800434/*
435 * TEMP: 1/1000 degrees C (-128C to +127C)
436 * REG: 1C/bit, two's complement
437 */
Christian Hohnstaedt5bfedac2007-08-16 11:40:10 +0200438static u8 LM93_TEMP_TO_REG(long temp)
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +0200439{
Guenter Roeck2a844c12013-01-09 08:09:34 -0800440 int ntemp = clamp_val(temp, LM93_TEMP_MIN, LM93_TEMP_MAX);
Guenter Roeck2804a4cf2012-01-14 21:25:54 -0800441 ntemp += (ntemp < 0 ? -500 : 500);
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +0200442 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
Guenter Roeck2804a4cf2012-01-14 21:25:54 -0800452/*
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 */
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +0200457static int LM93_TEMP_OFFSET_FROM_REG(u8 reg, int mode)
458{
459 return (reg & 0x0f) * (mode ? 5 : 10);
460}
461
Guenter Roeck2804a4cf2012-01-14 21:25:54 -0800462#define LM93_TEMP_OFFSET_MIN (0)
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +0200463#define LM93_TEMP_OFFSET_MAX0 (150)
Guenter Roeck2804a4cf2012-01-14 21:25:54 -0800464#define LM93_TEMP_OFFSET_MAX1 (75)
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +0200465
Guenter Roeck2804a4cf2012-01-14 21:25:54 -0800466/*
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 */
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +0200471static u8 LM93_TEMP_OFFSET_TO_REG(int off, int mode)
472{
473 int factor = mode ? 5 : 10;
474
Guenter Roeck2a844c12013-01-09 08:09:34 -0800475 off = clamp_val(off, LM93_TEMP_OFFSET_MIN,
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +0200476 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
Guenter Roeck2804a4cf2012-01-14 21:25:54 -0800492/*
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 */
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +0200497static 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
Guenter Roeck2804a4cf2012-01-14 21:25:54 -0800560/*
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
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +0200566static 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
Guenter Roeck2804a4cf2012-01-14 21:25:54 -0800589static int LM93_PWM_FROM_REG(u8 reg, enum pwm_freq freq)
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +0200590{
591 return lm93_pwm_map[freq][reg & 0x0f];
592}
593
594/* round up to nearest match */
Guenter Roeck2804a4cf2012-01-14 21:25:54 -0800595static u8 LM93_PWM_TO_REG(int pwm, enum pwm_freq freq)
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +0200596{
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;
Guenter Roeck2804a4cf2012-01-14 21:25:54 -0800609 return count == 0 ? -1 : count == 0x3fff ? 0 : 1350000 / count;
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +0200610}
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 {
Guenter Roeck2a844c12013-01-09 08:09:34 -0800623 rpm = clamp_val(rpm, 1, 1000000);
624 count = clamp_val((1350000 + rpm) / rpm, 1, 0x3ffe);
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +0200625 }
626
627 regs = count << 2;
628 return cpu_to_le16(regs);
629}
630
Guenter Roeck2804a4cf2012-01-14 21:25:54 -0800631/*
632 * PWM FREQ: HZ
633 * REG: 0-7 as mapped below
634 */
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +0200635static 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
Guenter Roeck2804a4cf2012-01-14 21:25:54 -0800656/*
657 * TIME: 1/100 seconds
658 * REG: 0-7 as mapped below
659 */
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +0200660static 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
Guenter Roeck2804a4cf2012-01-14 21:25:54 -0800689/*
690 * RAMP: 1/100 seconds
691 * REG: 50mS/bit 4-bits right justified
692 */
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +0200693static u8 LM93_RAMP_TO_REG(int ramp)
694{
Guenter Roeck2a844c12013-01-09 08:09:34 -0800695 ramp = clamp_val(ramp, LM93_RAMP_MIN, LM93_RAMP_MAX);
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +0200696 return (u8)((ramp + 2) / 5);
697}
698
Guenter Roeck2804a4cf2012-01-14 21:25:54 -0800699/*
700 * PROCHOT: 0-255, 0 => 0%, 255 => > 96.6%
701 * REG: (same)
702 */
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +0200703static u8 LM93_PROCHOT_TO_REG(long prochot)
704{
Guenter Roeck2a844c12013-01-09 08:09:34 -0800705 prochot = clamp_val(prochot, 0, 255);
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +0200706 return (u8)prochot;
707}
708
Guenter Roeck2804a4cf2012-01-14 21:25:54 -0800709/*
710 * PROCHOT-INTERVAL: 73 - 37200 (1/100 seconds)
711 * REG: 0-9 as mapped below
712 */
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +0200713static 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
Guenter Roeck2804a4cf2012-01-14 21:25:54 -0800734/*
735 * GPIO: 0-255, GPIO0 is LSB
736 * REG: inverted
737 */
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +0200738static unsigned LM93_GPI_FROM_REG(u8 reg)
739{
740 return ~reg & 0xff;
741}
742
Guenter Roeck2804a4cf2012-01-14 21:25:54 -0800743/*
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 */
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +0200753#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 */
Guenter Roeck2804a4cf2012-01-14 21:25:54 -0800816 for (i = 1; i <= MAX_RETRIES; i++) {
817 value = i2c_smbus_read_byte_data(client, reg);
818 if (value >= 0) {
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +0200819 return value;
820 } else {
Guenter Roeckb55f3752013-01-10 10:01:24 -0800821 dev_warn(&client->dev,
822 "lm93: read byte data failed, address 0x%02x.\n",
823 reg);
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +0200824 mdelay(i + 3);
825 }
826
827 }
828
829 /* <TODO> what to return in case of error? */
Guenter Roeck2804a4cf2012-01-14 21:25:54 -0800830 dev_err(&client->dev, "lm93: All read byte retries failed!!\n");
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +0200831 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)
Guenter Roeckb55f3752013-01-10 10:01:24 -0800842 dev_warn(&client->dev,
843 "lm93: write byte data failed, 0x%02x at address 0x%02x.\n",
844 value, reg);
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +0200845
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 */
Guenter Roeck2804a4cf2012-01-14 21:25:54 -0800854 for (i = 1; i <= MAX_RETRIES; i++) {
855 value = i2c_smbus_read_word_data(client, reg);
856 if (value >= 0) {
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +0200857 return value;
858 } else {
Guenter Roeckb55f3752013-01-10 10:01:24 -0800859 dev_warn(&client->dev,
860 "lm93: read word data failed, address 0x%02x.\n",
861 reg);
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +0200862 mdelay(i + 3);
863 }
864
865 }
866
867 /* <TODO> what to return in case of error? */
Guenter Roeck2804a4cf2012-01-14 21:25:54 -0800868 dev_err(&client->dev, "lm93: All read word retries failed!!\n");
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +0200869 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)
Guenter Roeckb55f3752013-01-10 10:01:24 -0800880 dev_warn(&client->dev,
881 "lm93: write word data failed, 0x%04x at address 0x%02x.\n",
882 value, reg);
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +0200883
884 return result;
885}
886
887static u8 lm93_block_buffer[I2C_SMBUS_BLOCK_MAX];
888
889/*
Guenter Roeck2804a4cf2012-01-14 21:25:54 -0800890 * 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 */
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +0200894static void lm93_read_block(struct i2c_client *client, u8 fbn, u8 *values)
895{
Guenter Roeck2804a4cf2012-01-14 21:25:54 -0800896 int i, result = 0;
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +0200897
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 {
Guenter Roeckb55f3752013-01-10 10:01:24 -0800905 dev_warn(&client->dev,
906 "lm93: block read data failed, command 0x%02x.\n",
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +0200907 lm93_block_read_cmds[fbn].cmd);
908 mdelay(i + 3);
909 }
910 }
911
912 if (result == lm93_block_read_cmds[fbn].len) {
Guenter Roeck2804a4cf2012-01-14 21:25:54 -0800913 memcpy(values, lm93_block_buffer,
914 lm93_block_read_cmds[fbn].len);
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +0200915 } else {
916 /* <TODO> what to do in case of error? */
917 }
918}
919
920static struct lm93_data *lm93_update_device(struct device *dev)
921{
Axel Lin6579d582014-07-02 20:36:50 +0800922 struct lm93_data *data = dev_get_drvdata(dev);
923 struct i2c_client *client = data->client;
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +0200924 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
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200983 /* Fan Boost Temperature registers */
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +0200984 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{
Guenter Roeck2804a4cf2012-01-14 21:25:54 -08001015 dev_dbg(&client->dev, "starting device update (block data enabled)\n");
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02001016
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{
Guenter Roeck2804a4cf2012-01-14 21:25:54 -08001047 int i, j;
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02001048 u8 *ptr;
1049
Guenter Roeck2804a4cf2012-01-14 21:25:54 -08001050 dev_dbg(&client->dev, "starting device update (block data disabled)\n");
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02001051
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] =
Guenter Roeck2804a4cf2012-01-14 21:25:54 -08001088 lm93_read_byte(client, LM93_REG_PWM_CTL(i, j));
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02001089 }
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 show_in(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(in1_input, S_IRUGO, show_in, NULL, 0);
1124static SENSOR_DEVICE_ATTR(in2_input, S_IRUGO, show_in, NULL, 1);
1125static SENSOR_DEVICE_ATTR(in3_input, S_IRUGO, show_in, NULL, 2);
1126static SENSOR_DEVICE_ATTR(in4_input, S_IRUGO, show_in, NULL, 3);
1127static SENSOR_DEVICE_ATTR(in5_input, S_IRUGO, show_in, NULL, 4);
1128static SENSOR_DEVICE_ATTR(in6_input, S_IRUGO, show_in, NULL, 5);
1129static SENSOR_DEVICE_ATTR(in7_input, S_IRUGO, show_in, NULL, 6);
1130static SENSOR_DEVICE_ATTR(in8_input, S_IRUGO, show_in, NULL, 7);
1131static SENSOR_DEVICE_ATTR(in9_input, S_IRUGO, show_in, NULL, 8);
1132static SENSOR_DEVICE_ATTR(in10_input, S_IRUGO, show_in, NULL, 9);
1133static SENSOR_DEVICE_ATTR(in11_input, S_IRUGO, show_in, NULL, 10);
1134static SENSOR_DEVICE_ATTR(in12_input, S_IRUGO, show_in, NULL, 11);
1135static SENSOR_DEVICE_ATTR(in13_input, S_IRUGO, show_in, NULL, 12);
1136static SENSOR_DEVICE_ATTR(in14_input, S_IRUGO, show_in, NULL, 13);
1137static SENSOR_DEVICE_ATTR(in15_input, S_IRUGO, show_in, NULL, 14);
1138static SENSOR_DEVICE_ATTR(in16_input, S_IRUGO, show_in, NULL, 15);
1139
1140static ssize_t show_in_min(struct device *dev,
1141 struct device_attribute *attr, 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
Guenter Roeck2804a4cf2012-01-14 21:25:54 -08001148 if ((nr == 6 || nr == 7) && vccp_limit_type[vccp]) {
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02001149 vid = LM93_VID_FROM_REG(data->vid[vccp]);
1150 rc = LM93_IN_MIN_FROM_REG(data->vccp_limits[vccp], vid);
Guenter Roeck2804a4cf2012-01-14 21:25:54 -08001151 } else {
1152 rc = LM93_IN_FROM_REG(nr, data->block7[nr].min);
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02001153 }
Guenter Roeck2804a4cf2012-01-14 21:25:54 -08001154 return sprintf(buf, "%ld\n", rc);
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02001155}
1156
1157static ssize_t store_in_min(struct device *dev, struct device_attribute *attr,
1158 const char *buf, size_t count)
1159{
1160 int nr = (to_sensor_dev_attr(attr))->index;
Axel Lin6579d582014-07-02 20:36:50 +08001161 struct lm93_data *data = dev_get_drvdata(dev);
1162 struct i2c_client *client = data->client;
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02001163 int vccp = nr - 6;
1164 long vid;
Guenter Roeck2804a4cf2012-01-14 21:25:54 -08001165 unsigned long val;
1166 int err;
1167
1168 err = kstrtoul(buf, 10, &val);
1169 if (err)
1170 return err;
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02001171
1172 mutex_lock(&data->update_lock);
Guenter Roeck2804a4cf2012-01-14 21:25:54 -08001173 if ((nr == 6 || nr == 7) && vccp_limit_type[vccp]) {
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02001174 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]);
Guenter Roeck2804a4cf2012-01-14 21:25:54 -08001179 } else {
1180 data->block7[nr].min = LM93_IN_TO_REG(nr, val);
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02001181 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(in1_min, S_IWUSR | S_IRUGO,
1189 show_in_min, store_in_min, 0);
1190static SENSOR_DEVICE_ATTR(in2_min, S_IWUSR | S_IRUGO,
1191 show_in_min, store_in_min, 1);
1192static SENSOR_DEVICE_ATTR(in3_min, S_IWUSR | S_IRUGO,
1193 show_in_min, store_in_min, 2);
1194static SENSOR_DEVICE_ATTR(in4_min, S_IWUSR | S_IRUGO,
1195 show_in_min, store_in_min, 3);
1196static SENSOR_DEVICE_ATTR(in5_min, S_IWUSR | S_IRUGO,
1197 show_in_min, store_in_min, 4);
1198static SENSOR_DEVICE_ATTR(in6_min, S_IWUSR | S_IRUGO,
1199 show_in_min, store_in_min, 5);
1200static SENSOR_DEVICE_ATTR(in7_min, S_IWUSR | S_IRUGO,
1201 show_in_min, store_in_min, 6);
1202static SENSOR_DEVICE_ATTR(in8_min, S_IWUSR | S_IRUGO,
1203 show_in_min, store_in_min, 7);
1204static SENSOR_DEVICE_ATTR(in9_min, S_IWUSR | S_IRUGO,
1205 show_in_min, store_in_min, 8);
1206static SENSOR_DEVICE_ATTR(in10_min, S_IWUSR | S_IRUGO,
1207 show_in_min, store_in_min, 9);
1208static SENSOR_DEVICE_ATTR(in11_min, S_IWUSR | S_IRUGO,
1209 show_in_min, store_in_min, 10);
1210static SENSOR_DEVICE_ATTR(in12_min, S_IWUSR | S_IRUGO,
1211 show_in_min, store_in_min, 11);
1212static SENSOR_DEVICE_ATTR(in13_min, S_IWUSR | S_IRUGO,
1213 show_in_min, store_in_min, 12);
1214static SENSOR_DEVICE_ATTR(in14_min, S_IWUSR | S_IRUGO,
1215 show_in_min, store_in_min, 13);
1216static SENSOR_DEVICE_ATTR(in15_min, S_IWUSR | S_IRUGO,
1217 show_in_min, store_in_min, 14);
1218static SENSOR_DEVICE_ATTR(in16_min, S_IWUSR | S_IRUGO,
1219 show_in_min, store_in_min, 15);
1220
1221static ssize_t show_in_max(struct device *dev,
1222 struct device_attribute *attr, char *buf)
1223{
1224 int nr = (to_sensor_dev_attr(attr))->index;
1225 struct lm93_data *data = lm93_update_device(dev);
1226 int vccp = nr - 6;
1227 long rc, vid;
1228
Guenter Roeck2804a4cf2012-01-14 21:25:54 -08001229 if ((nr == 6 || nr == 7) && vccp_limit_type[vccp]) {
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02001230 vid = LM93_VID_FROM_REG(data->vid[vccp]);
Guenter Roeck2804a4cf2012-01-14 21:25:54 -08001231 rc = LM93_IN_MAX_FROM_REG(data->vccp_limits[vccp], vid);
1232 } else {
1233 rc = LM93_IN_FROM_REG(nr, data->block7[nr].max);
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02001234 }
Guenter Roeck2804a4cf2012-01-14 21:25:54 -08001235 return sprintf(buf, "%ld\n", rc);
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02001236}
1237
1238static ssize_t store_in_max(struct device *dev, struct device_attribute *attr,
1239 const char *buf, size_t count)
1240{
1241 int nr = (to_sensor_dev_attr(attr))->index;
Axel Lin6579d582014-07-02 20:36:50 +08001242 struct lm93_data *data = dev_get_drvdata(dev);
1243 struct i2c_client *client = data->client;
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02001244 int vccp = nr - 6;
1245 long vid;
Guenter Roeck2804a4cf2012-01-14 21:25:54 -08001246 unsigned long val;
1247 int err;
1248
1249 err = kstrtoul(buf, 10, &val);
1250 if (err)
1251 return err;
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02001252
1253 mutex_lock(&data->update_lock);
Guenter Roeck2804a4cf2012-01-14 21:25:54 -08001254 if ((nr == 6 || nr == 7) && vccp_limit_type[vccp]) {
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02001255 vid = LM93_VID_FROM_REG(data->vid[vccp]);
1256 data->vccp_limits[vccp] = (data->vccp_limits[vccp] & 0x0f) |
1257 LM93_IN_REL_TO_REG(val, 1, vid);
1258 lm93_write_byte(client, LM93_REG_VCCP_LIMIT_OFF(vccp),
1259 data->vccp_limits[vccp]);
Guenter Roeck2804a4cf2012-01-14 21:25:54 -08001260 } else {
1261 data->block7[nr].max = LM93_IN_TO_REG(nr, val);
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02001262 lm93_write_byte(client, LM93_REG_IN_MAX(nr),
1263 data->block7[nr].max);
1264 }
1265 mutex_unlock(&data->update_lock);
1266 return count;
1267}
1268
1269static SENSOR_DEVICE_ATTR(in1_max, S_IWUSR | S_IRUGO,
1270 show_in_max, store_in_max, 0);
1271static SENSOR_DEVICE_ATTR(in2_max, S_IWUSR | S_IRUGO,
1272 show_in_max, store_in_max, 1);
1273static SENSOR_DEVICE_ATTR(in3_max, S_IWUSR | S_IRUGO,
1274 show_in_max, store_in_max, 2);
1275static SENSOR_DEVICE_ATTR(in4_max, S_IWUSR | S_IRUGO,
1276 show_in_max, store_in_max, 3);
1277static SENSOR_DEVICE_ATTR(in5_max, S_IWUSR | S_IRUGO,
1278 show_in_max, store_in_max, 4);
1279static SENSOR_DEVICE_ATTR(in6_max, S_IWUSR | S_IRUGO,
1280 show_in_max, store_in_max, 5);
1281static SENSOR_DEVICE_ATTR(in7_max, S_IWUSR | S_IRUGO,
1282 show_in_max, store_in_max, 6);
1283static SENSOR_DEVICE_ATTR(in8_max, S_IWUSR | S_IRUGO,
1284 show_in_max, store_in_max, 7);
1285static SENSOR_DEVICE_ATTR(in9_max, S_IWUSR | S_IRUGO,
1286 show_in_max, store_in_max, 8);
1287static SENSOR_DEVICE_ATTR(in10_max, S_IWUSR | S_IRUGO,
1288 show_in_max, store_in_max, 9);
1289static SENSOR_DEVICE_ATTR(in11_max, S_IWUSR | S_IRUGO,
1290 show_in_max, store_in_max, 10);
1291static SENSOR_DEVICE_ATTR(in12_max, S_IWUSR | S_IRUGO,
1292 show_in_max, store_in_max, 11);
1293static SENSOR_DEVICE_ATTR(in13_max, S_IWUSR | S_IRUGO,
1294 show_in_max, store_in_max, 12);
1295static SENSOR_DEVICE_ATTR(in14_max, S_IWUSR | S_IRUGO,
1296 show_in_max, store_in_max, 13);
1297static SENSOR_DEVICE_ATTR(in15_max, S_IWUSR | S_IRUGO,
1298 show_in_max, store_in_max, 14);
1299static SENSOR_DEVICE_ATTR(in16_max, S_IWUSR | S_IRUGO,
1300 show_in_max, store_in_max, 15);
1301
1302static ssize_t show_temp(struct device *dev,
1303 struct device_attribute *attr, char *buf)
1304{
1305 int nr = (to_sensor_dev_attr(attr))->index;
1306 struct lm93_data *data = lm93_update_device(dev);
Guenter Roeck2804a4cf2012-01-14 21:25:54 -08001307 return sprintf(buf, "%d\n", LM93_TEMP_FROM_REG(data->block2[nr]));
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02001308}
1309
1310static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0);
1311static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp, NULL, 1);
1312static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO, show_temp, NULL, 2);
1313
1314static ssize_t show_temp_min(struct device *dev,
1315 struct device_attribute *attr, char *buf)
1316{
1317 int nr = (to_sensor_dev_attr(attr))->index;
1318 struct lm93_data *data = lm93_update_device(dev);
Guenter Roeck2804a4cf2012-01-14 21:25:54 -08001319 return sprintf(buf, "%d\n", LM93_TEMP_FROM_REG(data->temp_lim[nr].min));
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02001320}
1321
1322static ssize_t store_temp_min(struct device *dev, struct device_attribute *attr,
1323 const char *buf, size_t count)
1324{
1325 int nr = (to_sensor_dev_attr(attr))->index;
Axel Lin6579d582014-07-02 20:36:50 +08001326 struct lm93_data *data = dev_get_drvdata(dev);
1327 struct i2c_client *client = data->client;
Guenter Roeck2804a4cf2012-01-14 21:25:54 -08001328 long val;
1329 int err;
1330
1331 err = kstrtol(buf, 10, &val);
1332 if (err)
1333 return err;
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02001334
1335 mutex_lock(&data->update_lock);
1336 data->temp_lim[nr].min = LM93_TEMP_TO_REG(val);
1337 lm93_write_byte(client, LM93_REG_TEMP_MIN(nr), data->temp_lim[nr].min);
1338 mutex_unlock(&data->update_lock);
1339 return count;
1340}
1341
1342static SENSOR_DEVICE_ATTR(temp1_min, S_IWUSR | S_IRUGO,
1343 show_temp_min, store_temp_min, 0);
1344static SENSOR_DEVICE_ATTR(temp2_min, S_IWUSR | S_IRUGO,
1345 show_temp_min, store_temp_min, 1);
1346static SENSOR_DEVICE_ATTR(temp3_min, S_IWUSR | S_IRUGO,
1347 show_temp_min, store_temp_min, 2);
1348
1349static ssize_t show_temp_max(struct device *dev,
1350 struct device_attribute *attr, char *buf)
1351{
1352 int nr = (to_sensor_dev_attr(attr))->index;
1353 struct lm93_data *data = lm93_update_device(dev);
Guenter Roeck2804a4cf2012-01-14 21:25:54 -08001354 return sprintf(buf, "%d\n", LM93_TEMP_FROM_REG(data->temp_lim[nr].max));
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02001355}
1356
1357static ssize_t store_temp_max(struct device *dev, struct device_attribute *attr,
1358 const char *buf, size_t count)
1359{
1360 int nr = (to_sensor_dev_attr(attr))->index;
Axel Lin6579d582014-07-02 20:36:50 +08001361 struct lm93_data *data = dev_get_drvdata(dev);
1362 struct i2c_client *client = data->client;
Guenter Roeck2804a4cf2012-01-14 21:25:54 -08001363 long val;
1364 int err;
1365
1366 err = kstrtol(buf, 10, &val);
1367 if (err)
1368 return err;
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02001369
1370 mutex_lock(&data->update_lock);
1371 data->temp_lim[nr].max = LM93_TEMP_TO_REG(val);
1372 lm93_write_byte(client, LM93_REG_TEMP_MAX(nr), data->temp_lim[nr].max);
1373 mutex_unlock(&data->update_lock);
1374 return count;
1375}
1376
1377static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO,
1378 show_temp_max, store_temp_max, 0);
1379static SENSOR_DEVICE_ATTR(temp2_max, S_IWUSR | S_IRUGO,
1380 show_temp_max, store_temp_max, 1);
1381static SENSOR_DEVICE_ATTR(temp3_max, S_IWUSR | S_IRUGO,
1382 show_temp_max, store_temp_max, 2);
1383
1384static ssize_t show_temp_auto_base(struct device *dev,
1385 struct device_attribute *attr, char *buf)
1386{
1387 int nr = (to_sensor_dev_attr(attr))->index;
1388 struct lm93_data *data = lm93_update_device(dev);
Guenter Roeck2804a4cf2012-01-14 21:25:54 -08001389 return sprintf(buf, "%d\n", LM93_TEMP_FROM_REG(data->block10.base[nr]));
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02001390}
1391
1392static ssize_t store_temp_auto_base(struct device *dev,
1393 struct device_attribute *attr,
1394 const char *buf, size_t count)
1395{
1396 int nr = (to_sensor_dev_attr(attr))->index;
Axel Lin6579d582014-07-02 20:36:50 +08001397 struct lm93_data *data = dev_get_drvdata(dev);
1398 struct i2c_client *client = data->client;
Guenter Roeck2804a4cf2012-01-14 21:25:54 -08001399 long val;
1400 int err;
1401
1402 err = kstrtol(buf, 10, &val);
1403 if (err)
1404 return err;
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02001405
1406 mutex_lock(&data->update_lock);
1407 data->block10.base[nr] = LM93_TEMP_TO_REG(val);
1408 lm93_write_byte(client, LM93_REG_TEMP_BASE(nr), data->block10.base[nr]);
1409 mutex_unlock(&data->update_lock);
1410 return count;
1411}
1412
1413static SENSOR_DEVICE_ATTR(temp1_auto_base, S_IWUSR | S_IRUGO,
1414 show_temp_auto_base, store_temp_auto_base, 0);
1415static SENSOR_DEVICE_ATTR(temp2_auto_base, S_IWUSR | S_IRUGO,
1416 show_temp_auto_base, store_temp_auto_base, 1);
1417static SENSOR_DEVICE_ATTR(temp3_auto_base, S_IWUSR | S_IRUGO,
1418 show_temp_auto_base, store_temp_auto_base, 2);
1419
1420static ssize_t show_temp_auto_boost(struct device *dev,
Guenter Roeck2804a4cf2012-01-14 21:25:54 -08001421 struct device_attribute *attr, char *buf)
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02001422{
1423 int nr = (to_sensor_dev_attr(attr))->index;
1424 struct lm93_data *data = lm93_update_device(dev);
Guenter Roeck2804a4cf2012-01-14 21:25:54 -08001425 return sprintf(buf, "%d\n", LM93_TEMP_FROM_REG(data->boost[nr]));
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02001426}
1427
1428static ssize_t store_temp_auto_boost(struct device *dev,
1429 struct device_attribute *attr,
1430 const char *buf, size_t count)
1431{
1432 int nr = (to_sensor_dev_attr(attr))->index;
Axel Lin6579d582014-07-02 20:36:50 +08001433 struct lm93_data *data = dev_get_drvdata(dev);
1434 struct i2c_client *client = data->client;
Guenter Roeck2804a4cf2012-01-14 21:25:54 -08001435 long val;
1436 int err;
1437
1438 err = kstrtol(buf, 10, &val);
1439 if (err)
1440 return err;
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02001441
1442 mutex_lock(&data->update_lock);
1443 data->boost[nr] = LM93_TEMP_TO_REG(val);
1444 lm93_write_byte(client, LM93_REG_BOOST(nr), data->boost[nr]);
1445 mutex_unlock(&data->update_lock);
1446 return count;
1447}
1448
1449static SENSOR_DEVICE_ATTR(temp1_auto_boost, S_IWUSR | S_IRUGO,
1450 show_temp_auto_boost, store_temp_auto_boost, 0);
1451static SENSOR_DEVICE_ATTR(temp2_auto_boost, S_IWUSR | S_IRUGO,
1452 show_temp_auto_boost, store_temp_auto_boost, 1);
1453static SENSOR_DEVICE_ATTR(temp3_auto_boost, S_IWUSR | S_IRUGO,
1454 show_temp_auto_boost, store_temp_auto_boost, 2);
1455
1456static ssize_t show_temp_auto_boost_hyst(struct device *dev,
1457 struct device_attribute *attr,
1458 char *buf)
1459{
1460 int nr = (to_sensor_dev_attr(attr))->index;
1461 struct lm93_data *data = lm93_update_device(dev);
1462 int mode = LM93_TEMP_OFFSET_MODE_FROM_REG(data->sfc2, nr);
Guenter Roeck2804a4cf2012-01-14 21:25:54 -08001463 return sprintf(buf, "%d\n",
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02001464 LM93_AUTO_BOOST_HYST_FROM_REGS(data, nr, mode));
1465}
1466
1467static ssize_t store_temp_auto_boost_hyst(struct device *dev,
1468 struct device_attribute *attr,
1469 const char *buf, size_t count)
1470{
1471 int nr = (to_sensor_dev_attr(attr))->index;
Axel Lin6579d582014-07-02 20:36:50 +08001472 struct lm93_data *data = dev_get_drvdata(dev);
1473 struct i2c_client *client = data->client;
Guenter Roeck2804a4cf2012-01-14 21:25:54 -08001474 unsigned long val;
1475 int err;
1476
1477 err = kstrtoul(buf, 10, &val);
1478 if (err)
1479 return err;
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02001480
1481 mutex_lock(&data->update_lock);
1482 /* force 0.5C/bit mode */
1483 data->sfc2 = lm93_read_byte(client, LM93_REG_SFC2);
1484 data->sfc2 |= ((nr < 2) ? 0x10 : 0x20);
1485 lm93_write_byte(client, LM93_REG_SFC2, data->sfc2);
1486 data->boost_hyst[nr/2] = LM93_AUTO_BOOST_HYST_TO_REG(data, val, nr, 1);
1487 lm93_write_byte(client, LM93_REG_BOOST_HYST(nr),
1488 data->boost_hyst[nr/2]);
1489 mutex_unlock(&data->update_lock);
1490 return count;
1491}
1492
1493static SENSOR_DEVICE_ATTR(temp1_auto_boost_hyst, S_IWUSR | S_IRUGO,
1494 show_temp_auto_boost_hyst,
1495 store_temp_auto_boost_hyst, 0);
1496static SENSOR_DEVICE_ATTR(temp2_auto_boost_hyst, S_IWUSR | S_IRUGO,
1497 show_temp_auto_boost_hyst,
1498 store_temp_auto_boost_hyst, 1);
1499static SENSOR_DEVICE_ATTR(temp3_auto_boost_hyst, S_IWUSR | S_IRUGO,
1500 show_temp_auto_boost_hyst,
1501 store_temp_auto_boost_hyst, 2);
1502
1503static ssize_t show_temp_auto_offset(struct device *dev,
1504 struct device_attribute *attr, char *buf)
1505{
1506 struct sensor_device_attribute_2 *s_attr = to_sensor_dev_attr_2(attr);
1507 int nr = s_attr->index;
1508 int ofs = s_attr->nr;
1509 struct lm93_data *data = lm93_update_device(dev);
1510 int mode = LM93_TEMP_OFFSET_MODE_FROM_REG(data->sfc2, nr);
Guenter Roeck2804a4cf2012-01-14 21:25:54 -08001511 return sprintf(buf, "%d\n",
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02001512 LM93_TEMP_AUTO_OFFSET_FROM_REG(data->block10.offset[ofs],
Guenter Roeck2804a4cf2012-01-14 21:25:54 -08001513 nr, mode));
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02001514}
1515
1516static ssize_t store_temp_auto_offset(struct device *dev,
1517 struct device_attribute *attr,
1518 const char *buf, size_t count)
1519{
1520 struct sensor_device_attribute_2 *s_attr = to_sensor_dev_attr_2(attr);
1521 int nr = s_attr->index;
1522 int ofs = s_attr->nr;
Axel Lin6579d582014-07-02 20:36:50 +08001523 struct lm93_data *data = dev_get_drvdata(dev);
1524 struct i2c_client *client = data->client;
Guenter Roeck2804a4cf2012-01-14 21:25:54 -08001525 unsigned long val;
1526 int err;
1527
1528 err = kstrtoul(buf, 10, &val);
1529 if (err)
1530 return err;
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02001531
1532 mutex_lock(&data->update_lock);
1533 /* force 0.5C/bit mode */
1534 data->sfc2 = lm93_read_byte(client, LM93_REG_SFC2);
1535 data->sfc2 |= ((nr < 2) ? 0x10 : 0x20);
1536 lm93_write_byte(client, LM93_REG_SFC2, data->sfc2);
1537 data->block10.offset[ofs] = LM93_TEMP_AUTO_OFFSET_TO_REG(
1538 data->block10.offset[ofs], val, nr, 1);
1539 lm93_write_byte(client, LM93_REG_TEMP_OFFSET(ofs),
1540 data->block10.offset[ofs]);
1541 mutex_unlock(&data->update_lock);
1542 return count;
1543}
1544
1545static SENSOR_DEVICE_ATTR_2(temp1_auto_offset1, S_IWUSR | S_IRUGO,
1546 show_temp_auto_offset, store_temp_auto_offset, 0, 0);
1547static SENSOR_DEVICE_ATTR_2(temp1_auto_offset2, S_IWUSR | S_IRUGO,
1548 show_temp_auto_offset, store_temp_auto_offset, 1, 0);
1549static SENSOR_DEVICE_ATTR_2(temp1_auto_offset3, S_IWUSR | S_IRUGO,
1550 show_temp_auto_offset, store_temp_auto_offset, 2, 0);
1551static SENSOR_DEVICE_ATTR_2(temp1_auto_offset4, S_IWUSR | S_IRUGO,
1552 show_temp_auto_offset, store_temp_auto_offset, 3, 0);
1553static SENSOR_DEVICE_ATTR_2(temp1_auto_offset5, S_IWUSR | S_IRUGO,
1554 show_temp_auto_offset, store_temp_auto_offset, 4, 0);
1555static SENSOR_DEVICE_ATTR_2(temp1_auto_offset6, S_IWUSR | S_IRUGO,
1556 show_temp_auto_offset, store_temp_auto_offset, 5, 0);
1557static SENSOR_DEVICE_ATTR_2(temp1_auto_offset7, S_IWUSR | S_IRUGO,
1558 show_temp_auto_offset, store_temp_auto_offset, 6, 0);
1559static SENSOR_DEVICE_ATTR_2(temp1_auto_offset8, S_IWUSR | S_IRUGO,
1560 show_temp_auto_offset, store_temp_auto_offset, 7, 0);
1561static SENSOR_DEVICE_ATTR_2(temp1_auto_offset9, S_IWUSR | S_IRUGO,
1562 show_temp_auto_offset, store_temp_auto_offset, 8, 0);
1563static SENSOR_DEVICE_ATTR_2(temp1_auto_offset10, S_IWUSR | S_IRUGO,
1564 show_temp_auto_offset, store_temp_auto_offset, 9, 0);
1565static SENSOR_DEVICE_ATTR_2(temp1_auto_offset11, S_IWUSR | S_IRUGO,
1566 show_temp_auto_offset, store_temp_auto_offset, 10, 0);
1567static SENSOR_DEVICE_ATTR_2(temp1_auto_offset12, S_IWUSR | S_IRUGO,
1568 show_temp_auto_offset, store_temp_auto_offset, 11, 0);
1569static SENSOR_DEVICE_ATTR_2(temp2_auto_offset1, S_IWUSR | S_IRUGO,
1570 show_temp_auto_offset, store_temp_auto_offset, 0, 1);
1571static SENSOR_DEVICE_ATTR_2(temp2_auto_offset2, S_IWUSR | S_IRUGO,
1572 show_temp_auto_offset, store_temp_auto_offset, 1, 1);
1573static SENSOR_DEVICE_ATTR_2(temp2_auto_offset3, S_IWUSR | S_IRUGO,
1574 show_temp_auto_offset, store_temp_auto_offset, 2, 1);
1575static SENSOR_DEVICE_ATTR_2(temp2_auto_offset4, S_IWUSR | S_IRUGO,
1576 show_temp_auto_offset, store_temp_auto_offset, 3, 1);
1577static SENSOR_DEVICE_ATTR_2(temp2_auto_offset5, S_IWUSR | S_IRUGO,
1578 show_temp_auto_offset, store_temp_auto_offset, 4, 1);
1579static SENSOR_DEVICE_ATTR_2(temp2_auto_offset6, S_IWUSR | S_IRUGO,
1580 show_temp_auto_offset, store_temp_auto_offset, 5, 1);
1581static SENSOR_DEVICE_ATTR_2(temp2_auto_offset7, S_IWUSR | S_IRUGO,
1582 show_temp_auto_offset, store_temp_auto_offset, 6, 1);
1583static SENSOR_DEVICE_ATTR_2(temp2_auto_offset8, S_IWUSR | S_IRUGO,
1584 show_temp_auto_offset, store_temp_auto_offset, 7, 1);
1585static SENSOR_DEVICE_ATTR_2(temp2_auto_offset9, S_IWUSR | S_IRUGO,
1586 show_temp_auto_offset, store_temp_auto_offset, 8, 1);
1587static SENSOR_DEVICE_ATTR_2(temp2_auto_offset10, S_IWUSR | S_IRUGO,
1588 show_temp_auto_offset, store_temp_auto_offset, 9, 1);
1589static SENSOR_DEVICE_ATTR_2(temp2_auto_offset11, S_IWUSR | S_IRUGO,
1590 show_temp_auto_offset, store_temp_auto_offset, 10, 1);
1591static SENSOR_DEVICE_ATTR_2(temp2_auto_offset12, S_IWUSR | S_IRUGO,
1592 show_temp_auto_offset, store_temp_auto_offset, 11, 1);
1593static SENSOR_DEVICE_ATTR_2(temp3_auto_offset1, S_IWUSR | S_IRUGO,
1594 show_temp_auto_offset, store_temp_auto_offset, 0, 2);
1595static SENSOR_DEVICE_ATTR_2(temp3_auto_offset2, S_IWUSR | S_IRUGO,
1596 show_temp_auto_offset, store_temp_auto_offset, 1, 2);
1597static SENSOR_DEVICE_ATTR_2(temp3_auto_offset3, S_IWUSR | S_IRUGO,
1598 show_temp_auto_offset, store_temp_auto_offset, 2, 2);
1599static SENSOR_DEVICE_ATTR_2(temp3_auto_offset4, S_IWUSR | S_IRUGO,
1600 show_temp_auto_offset, store_temp_auto_offset, 3, 2);
1601static SENSOR_DEVICE_ATTR_2(temp3_auto_offset5, S_IWUSR | S_IRUGO,
1602 show_temp_auto_offset, store_temp_auto_offset, 4, 2);
1603static SENSOR_DEVICE_ATTR_2(temp3_auto_offset6, S_IWUSR | S_IRUGO,
1604 show_temp_auto_offset, store_temp_auto_offset, 5, 2);
1605static SENSOR_DEVICE_ATTR_2(temp3_auto_offset7, S_IWUSR | S_IRUGO,
1606 show_temp_auto_offset, store_temp_auto_offset, 6, 2);
1607static SENSOR_DEVICE_ATTR_2(temp3_auto_offset8, S_IWUSR | S_IRUGO,
1608 show_temp_auto_offset, store_temp_auto_offset, 7, 2);
1609static SENSOR_DEVICE_ATTR_2(temp3_auto_offset9, S_IWUSR | S_IRUGO,
1610 show_temp_auto_offset, store_temp_auto_offset, 8, 2);
1611static SENSOR_DEVICE_ATTR_2(temp3_auto_offset10, S_IWUSR | S_IRUGO,
1612 show_temp_auto_offset, store_temp_auto_offset, 9, 2);
1613static SENSOR_DEVICE_ATTR_2(temp3_auto_offset11, S_IWUSR | S_IRUGO,
1614 show_temp_auto_offset, store_temp_auto_offset, 10, 2);
1615static SENSOR_DEVICE_ATTR_2(temp3_auto_offset12, S_IWUSR | S_IRUGO,
1616 show_temp_auto_offset, store_temp_auto_offset, 11, 2);
1617
1618static ssize_t show_temp_auto_pwm_min(struct device *dev,
1619 struct device_attribute *attr, char *buf)
1620{
1621 int nr = (to_sensor_dev_attr(attr))->index;
1622 u8 reg, ctl4;
1623 struct lm93_data *data = lm93_update_device(dev);
1624 reg = data->auto_pwm_min_hyst[nr/2] >> 4 & 0x0f;
1625 ctl4 = data->block9[nr][LM93_PWM_CTL4];
Guenter Roeck2804a4cf2012-01-14 21:25:54 -08001626 return sprintf(buf, "%d\n", LM93_PWM_FROM_REG(reg, (ctl4 & 0x07) ?
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02001627 LM93_PWM_MAP_LO_FREQ : LM93_PWM_MAP_HI_FREQ));
1628}
1629
1630static ssize_t store_temp_auto_pwm_min(struct device *dev,
1631 struct device_attribute *attr,
1632 const char *buf, size_t count)
1633{
1634 int nr = (to_sensor_dev_attr(attr))->index;
Axel Lin6579d582014-07-02 20:36:50 +08001635 struct lm93_data *data = dev_get_drvdata(dev);
1636 struct i2c_client *client = data->client;
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02001637 u8 reg, ctl4;
Guenter Roeck2804a4cf2012-01-14 21:25:54 -08001638 unsigned long val;
1639 int err;
1640
1641 err = kstrtoul(buf, 10, &val);
1642 if (err)
1643 return err;
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02001644
1645 mutex_lock(&data->update_lock);
1646 reg = lm93_read_byte(client, LM93_REG_PWM_MIN_HYST(nr));
Guenter Roeck2804a4cf2012-01-14 21:25:54 -08001647 ctl4 = lm93_read_byte(client, LM93_REG_PWM_CTL(nr, LM93_PWM_CTL4));
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02001648 reg = (reg & 0x0f) |
1649 LM93_PWM_TO_REG(val, (ctl4 & 0x07) ?
1650 LM93_PWM_MAP_LO_FREQ :
1651 LM93_PWM_MAP_HI_FREQ) << 4;
1652 data->auto_pwm_min_hyst[nr/2] = reg;
1653 lm93_write_byte(client, LM93_REG_PWM_MIN_HYST(nr), reg);
1654 mutex_unlock(&data->update_lock);
1655 return count;
1656}
1657
1658static SENSOR_DEVICE_ATTR(temp1_auto_pwm_min, S_IWUSR | S_IRUGO,
1659 show_temp_auto_pwm_min,
1660 store_temp_auto_pwm_min, 0);
1661static SENSOR_DEVICE_ATTR(temp2_auto_pwm_min, S_IWUSR | S_IRUGO,
1662 show_temp_auto_pwm_min,
1663 store_temp_auto_pwm_min, 1);
1664static SENSOR_DEVICE_ATTR(temp3_auto_pwm_min, S_IWUSR | S_IRUGO,
1665 show_temp_auto_pwm_min,
1666 store_temp_auto_pwm_min, 2);
1667
1668static ssize_t show_temp_auto_offset_hyst(struct device *dev,
1669 struct device_attribute *attr, char *buf)
1670{
1671 int nr = (to_sensor_dev_attr(attr))->index;
1672 struct lm93_data *data = lm93_update_device(dev);
1673 int mode = LM93_TEMP_OFFSET_MODE_FROM_REG(data->sfc2, nr);
Guenter Roeck2804a4cf2012-01-14 21:25:54 -08001674 return sprintf(buf, "%d\n", LM93_TEMP_OFFSET_FROM_REG(
1675 data->auto_pwm_min_hyst[nr / 2], mode));
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02001676}
1677
1678static ssize_t store_temp_auto_offset_hyst(struct device *dev,
1679 struct device_attribute *attr,
1680 const char *buf, size_t count)
1681{
1682 int nr = (to_sensor_dev_attr(attr))->index;
Axel Lin6579d582014-07-02 20:36:50 +08001683 struct lm93_data *data = dev_get_drvdata(dev);
1684 struct i2c_client *client = data->client;
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02001685 u8 reg;
Guenter Roeck2804a4cf2012-01-14 21:25:54 -08001686 unsigned long val;
1687 int err;
1688
1689 err = kstrtoul(buf, 10, &val);
1690 if (err)
1691 return err;
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02001692
1693 mutex_lock(&data->update_lock);
1694 /* force 0.5C/bit mode */
1695 data->sfc2 = lm93_read_byte(client, LM93_REG_SFC2);
1696 data->sfc2 |= ((nr < 2) ? 0x10 : 0x20);
1697 lm93_write_byte(client, LM93_REG_SFC2, data->sfc2);
1698 reg = data->auto_pwm_min_hyst[nr/2];
1699 reg = (reg & 0xf0) | (LM93_TEMP_OFFSET_TO_REG(val, 1) & 0x0f);
1700 data->auto_pwm_min_hyst[nr/2] = reg;
1701 lm93_write_byte(client, LM93_REG_PWM_MIN_HYST(nr), reg);
1702 mutex_unlock(&data->update_lock);
1703 return count;
1704}
1705
1706static SENSOR_DEVICE_ATTR(temp1_auto_offset_hyst, S_IWUSR | S_IRUGO,
1707 show_temp_auto_offset_hyst,
1708 store_temp_auto_offset_hyst, 0);
1709static SENSOR_DEVICE_ATTR(temp2_auto_offset_hyst, S_IWUSR | S_IRUGO,
1710 show_temp_auto_offset_hyst,
1711 store_temp_auto_offset_hyst, 1);
1712static SENSOR_DEVICE_ATTR(temp3_auto_offset_hyst, S_IWUSR | S_IRUGO,
1713 show_temp_auto_offset_hyst,
1714 store_temp_auto_offset_hyst, 2);
1715
1716static ssize_t show_fan_input(struct device *dev,
1717 struct device_attribute *attr, char *buf)
1718{
1719 struct sensor_device_attribute *s_attr = to_sensor_dev_attr(attr);
1720 int nr = s_attr->index;
1721 struct lm93_data *data = lm93_update_device(dev);
1722
Guenter Roeck2804a4cf2012-01-14 21:25:54 -08001723 return sprintf(buf, "%d\n", LM93_FAN_FROM_REG(data->block5[nr]));
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02001724}
1725
1726static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, show_fan_input, NULL, 0);
1727static SENSOR_DEVICE_ATTR(fan2_input, S_IRUGO, show_fan_input, NULL, 1);
1728static SENSOR_DEVICE_ATTR(fan3_input, S_IRUGO, show_fan_input, NULL, 2);
1729static SENSOR_DEVICE_ATTR(fan4_input, S_IRUGO, show_fan_input, NULL, 3);
1730
1731static ssize_t show_fan_min(struct device *dev,
1732 struct device_attribute *attr, char *buf)
1733{
1734 int nr = (to_sensor_dev_attr(attr))->index;
1735 struct lm93_data *data = lm93_update_device(dev);
1736
Guenter Roeck2804a4cf2012-01-14 21:25:54 -08001737 return sprintf(buf, "%d\n", LM93_FAN_FROM_REG(data->block8[nr]));
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02001738}
1739
1740static ssize_t store_fan_min(struct device *dev, struct device_attribute *attr,
1741 const char *buf, size_t count)
1742{
1743 int nr = (to_sensor_dev_attr(attr))->index;
Axel Lin6579d582014-07-02 20:36:50 +08001744 struct lm93_data *data = dev_get_drvdata(dev);
1745 struct i2c_client *client = data->client;
Guenter Roeck2804a4cf2012-01-14 21:25:54 -08001746 unsigned long val;
1747 int err;
1748
1749 err = kstrtoul(buf, 10, &val);
1750 if (err)
1751 return err;
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02001752
1753 mutex_lock(&data->update_lock);
1754 data->block8[nr] = LM93_FAN_TO_REG(val);
Guenter Roeck2804a4cf2012-01-14 21:25:54 -08001755 lm93_write_word(client, LM93_REG_FAN_MIN(nr), data->block8[nr]);
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02001756 mutex_unlock(&data->update_lock);
1757 return count;
1758}
1759
1760static SENSOR_DEVICE_ATTR(fan1_min, S_IWUSR | S_IRUGO,
1761 show_fan_min, store_fan_min, 0);
1762static SENSOR_DEVICE_ATTR(fan2_min, S_IWUSR | S_IRUGO,
1763 show_fan_min, store_fan_min, 1);
1764static SENSOR_DEVICE_ATTR(fan3_min, S_IWUSR | S_IRUGO,
1765 show_fan_min, store_fan_min, 2);
1766static SENSOR_DEVICE_ATTR(fan4_min, S_IWUSR | S_IRUGO,
1767 show_fan_min, store_fan_min, 3);
1768
Guenter Roeck2804a4cf2012-01-14 21:25:54 -08001769/*
1770 * some tedious bit-twiddling here to deal with the register format:
1771 *
1772 * data->sf_tach_to_pwm: (tach to pwm mapping bits)
1773 *
1774 * bit | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0
1775 * T4:P2 T4:P1 T3:P2 T3:P1 T2:P2 T2:P1 T1:P2 T1:P1
1776 *
1777 * data->sfc2: (enable bits)
1778 *
1779 * bit | 3 | 2 | 1 | 0
1780 * T4 T3 T2 T1
1781 */
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02001782
1783static ssize_t show_fan_smart_tach(struct device *dev,
1784 struct device_attribute *attr, char *buf)
1785{
1786 int nr = (to_sensor_dev_attr(attr))->index;
1787 struct lm93_data *data = lm93_update_device(dev);
1788 long rc = 0;
1789 int mapping;
1790
1791 /* extract the relevant mapping */
1792 mapping = (data->sf_tach_to_pwm >> (nr * 2)) & 0x03;
1793
1794 /* if there's a mapping and it's enabled */
1795 if (mapping && ((data->sfc2 >> nr) & 0x01))
1796 rc = mapping;
Guenter Roeck2804a4cf2012-01-14 21:25:54 -08001797 return sprintf(buf, "%ld\n", rc);
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02001798}
1799
Guenter Roeck2804a4cf2012-01-14 21:25:54 -08001800/*
1801 * helper function - must grab data->update_lock before calling
1802 * fan is 0-3, indicating fan1-fan4
1803 */
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02001804static void lm93_write_fan_smart_tach(struct i2c_client *client,
1805 struct lm93_data *data, int fan, long value)
1806{
1807 /* insert the new mapping and write it out */
1808 data->sf_tach_to_pwm = lm93_read_byte(client, LM93_REG_SF_TACH_TO_PWM);
1809 data->sf_tach_to_pwm &= ~(0x3 << fan * 2);
1810 data->sf_tach_to_pwm |= value << fan * 2;
1811 lm93_write_byte(client, LM93_REG_SF_TACH_TO_PWM, data->sf_tach_to_pwm);
1812
1813 /* insert the enable bit and write it out */
1814 data->sfc2 = lm93_read_byte(client, LM93_REG_SFC2);
1815 if (value)
1816 data->sfc2 |= 1 << fan;
1817 else
1818 data->sfc2 &= ~(1 << fan);
1819 lm93_write_byte(client, LM93_REG_SFC2, data->sfc2);
1820}
1821
1822static ssize_t store_fan_smart_tach(struct device *dev,
1823 struct device_attribute *attr,
1824 const char *buf, size_t count)
1825{
1826 int nr = (to_sensor_dev_attr(attr))->index;
Axel Lin6579d582014-07-02 20:36:50 +08001827 struct lm93_data *data = dev_get_drvdata(dev);
1828 struct i2c_client *client = data->client;
Guenter Roeck2804a4cf2012-01-14 21:25:54 -08001829 unsigned long val;
1830 int err;
1831
1832 err = kstrtoul(buf, 10, &val);
1833 if (err)
1834 return err;
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02001835
1836 mutex_lock(&data->update_lock);
1837 /* sanity test, ignore the write otherwise */
Guenter Roeck3be81022012-09-19 11:21:51 -07001838 if (val <= 2) {
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02001839 /* can't enable if pwm freq is 22.5KHz */
1840 if (val) {
1841 u8 ctl4 = lm93_read_byte(client,
Guenter Roeck2804a4cf2012-01-14 21:25:54 -08001842 LM93_REG_PWM_CTL(val - 1, LM93_PWM_CTL4));
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02001843 if ((ctl4 & 0x07) == 0)
1844 val = 0;
1845 }
1846 lm93_write_fan_smart_tach(client, data, nr, val);
1847 }
1848 mutex_unlock(&data->update_lock);
1849 return count;
1850}
1851
1852static SENSOR_DEVICE_ATTR(fan1_smart_tach, S_IWUSR | S_IRUGO,
1853 show_fan_smart_tach, store_fan_smart_tach, 0);
1854static SENSOR_DEVICE_ATTR(fan2_smart_tach, S_IWUSR | S_IRUGO,
1855 show_fan_smart_tach, store_fan_smart_tach, 1);
1856static SENSOR_DEVICE_ATTR(fan3_smart_tach, S_IWUSR | S_IRUGO,
1857 show_fan_smart_tach, store_fan_smart_tach, 2);
1858static SENSOR_DEVICE_ATTR(fan4_smart_tach, S_IWUSR | S_IRUGO,
1859 show_fan_smart_tach, store_fan_smart_tach, 3);
1860
1861static ssize_t show_pwm(struct device *dev, struct device_attribute *attr,
1862 char *buf)
1863{
1864 int nr = (to_sensor_dev_attr(attr))->index;
1865 struct lm93_data *data = lm93_update_device(dev);
1866 u8 ctl2, ctl4;
1867 long rc;
1868
1869 ctl2 = data->block9[nr][LM93_PWM_CTL2];
1870 ctl4 = data->block9[nr][LM93_PWM_CTL4];
1871 if (ctl2 & 0x01) /* show user commanded value if enabled */
1872 rc = data->pwm_override[nr];
1873 else /* show present h/w value if manual pwm disabled */
1874 rc = LM93_PWM_FROM_REG(ctl2 >> 4, (ctl4 & 0x07) ?
1875 LM93_PWM_MAP_LO_FREQ : LM93_PWM_MAP_HI_FREQ);
Guenter Roeck2804a4cf2012-01-14 21:25:54 -08001876 return sprintf(buf, "%ld\n", rc);
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02001877}
1878
1879static ssize_t store_pwm(struct device *dev, struct device_attribute *attr,
1880 const char *buf, size_t count)
1881{
1882 int nr = (to_sensor_dev_attr(attr))->index;
Axel Lin6579d582014-07-02 20:36:50 +08001883 struct lm93_data *data = dev_get_drvdata(dev);
1884 struct i2c_client *client = data->client;
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02001885 u8 ctl2, ctl4;
Guenter Roeck2804a4cf2012-01-14 21:25:54 -08001886 unsigned long val;
1887 int err;
1888
1889 err = kstrtoul(buf, 10, &val);
1890 if (err)
1891 return err;
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02001892
1893 mutex_lock(&data->update_lock);
Guenter Roeck2804a4cf2012-01-14 21:25:54 -08001894 ctl2 = lm93_read_byte(client, LM93_REG_PWM_CTL(nr, LM93_PWM_CTL2));
1895 ctl4 = lm93_read_byte(client, LM93_REG_PWM_CTL(nr, LM93_PWM_CTL4));
1896 ctl2 = (ctl2 & 0x0f) | LM93_PWM_TO_REG(val, (ctl4 & 0x07) ?
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02001897 LM93_PWM_MAP_LO_FREQ : LM93_PWM_MAP_HI_FREQ) << 4;
1898 /* save user commanded value */
1899 data->pwm_override[nr] = LM93_PWM_FROM_REG(ctl2 >> 4,
1900 (ctl4 & 0x07) ? LM93_PWM_MAP_LO_FREQ :
1901 LM93_PWM_MAP_HI_FREQ);
Guenter Roeck2804a4cf2012-01-14 21:25:54 -08001902 lm93_write_byte(client, LM93_REG_PWM_CTL(nr, LM93_PWM_CTL2), ctl2);
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02001903 mutex_unlock(&data->update_lock);
1904 return count;
1905}
1906
1907static SENSOR_DEVICE_ATTR(pwm1, S_IWUSR | S_IRUGO, show_pwm, store_pwm, 0);
1908static SENSOR_DEVICE_ATTR(pwm2, S_IWUSR | S_IRUGO, show_pwm, store_pwm, 1);
1909
1910static ssize_t show_pwm_enable(struct device *dev,
1911 struct device_attribute *attr, char *buf)
1912{
1913 int nr = (to_sensor_dev_attr(attr))->index;
1914 struct lm93_data *data = lm93_update_device(dev);
1915 u8 ctl2;
1916 long rc;
1917
1918 ctl2 = data->block9[nr][LM93_PWM_CTL2];
1919 if (ctl2 & 0x01) /* manual override enabled ? */
1920 rc = ((ctl2 & 0xF0) == 0xF0) ? 0 : 1;
1921 else
1922 rc = 2;
Guenter Roeck2804a4cf2012-01-14 21:25:54 -08001923 return sprintf(buf, "%ld\n", rc);
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02001924}
1925
1926static ssize_t store_pwm_enable(struct device *dev,
1927 struct device_attribute *attr,
1928 const char *buf, size_t count)
1929{
1930 int nr = (to_sensor_dev_attr(attr))->index;
Axel Lin6579d582014-07-02 20:36:50 +08001931 struct lm93_data *data = dev_get_drvdata(dev);
1932 struct i2c_client *client = data->client;
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02001933 u8 ctl2;
Guenter Roeck2804a4cf2012-01-14 21:25:54 -08001934 unsigned long val;
1935 int err;
1936
1937 err = kstrtoul(buf, 10, &val);
1938 if (err)
1939 return err;
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02001940
1941 mutex_lock(&data->update_lock);
Guenter Roeck2804a4cf2012-01-14 21:25:54 -08001942 ctl2 = lm93_read_byte(client, LM93_REG_PWM_CTL(nr, LM93_PWM_CTL2));
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02001943
1944 switch (val) {
1945 case 0:
1946 ctl2 |= 0xF1; /* enable manual override, set PWM to max */
1947 break;
Guenter Roeck2804a4cf2012-01-14 21:25:54 -08001948 case 1:
1949 ctl2 |= 0x01; /* enable manual override */
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02001950 break;
Guenter Roeck2804a4cf2012-01-14 21:25:54 -08001951 case 2:
1952 ctl2 &= ~0x01; /* disable manual override */
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02001953 break;
1954 default:
1955 mutex_unlock(&data->update_lock);
1956 return -EINVAL;
1957 }
1958
Guenter Roeck2804a4cf2012-01-14 21:25:54 -08001959 lm93_write_byte(client, LM93_REG_PWM_CTL(nr, LM93_PWM_CTL2), ctl2);
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02001960 mutex_unlock(&data->update_lock);
1961 return count;
1962}
1963
1964static SENSOR_DEVICE_ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
1965 show_pwm_enable, store_pwm_enable, 0);
1966static SENSOR_DEVICE_ATTR(pwm2_enable, S_IWUSR | S_IRUGO,
1967 show_pwm_enable, store_pwm_enable, 1);
1968
1969static ssize_t show_pwm_freq(struct device *dev, struct device_attribute *attr,
1970 char *buf)
1971{
1972 int nr = (to_sensor_dev_attr(attr))->index;
1973 struct lm93_data *data = lm93_update_device(dev);
1974 u8 ctl4;
1975
1976 ctl4 = data->block9[nr][LM93_PWM_CTL4];
Guenter Roeck2804a4cf2012-01-14 21:25:54 -08001977 return sprintf(buf, "%d\n", LM93_PWM_FREQ_FROM_REG(ctl4));
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02001978}
1979
Guenter Roeck2804a4cf2012-01-14 21:25:54 -08001980/*
1981 * helper function - must grab data->update_lock before calling
1982 * pwm is 0-1, indicating pwm1-pwm2
1983 * this disables smart tach for all tach channels bound to the given pwm
1984 */
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02001985static void lm93_disable_fan_smart_tach(struct i2c_client *client,
1986 struct lm93_data *data, int pwm)
1987{
1988 int mapping = lm93_read_byte(client, LM93_REG_SF_TACH_TO_PWM);
1989 int mask;
1990
1991 /* collapse the mapping into a mask of enable bits */
1992 mapping = (mapping >> pwm) & 0x55;
1993 mask = mapping & 0x01;
1994 mask |= (mapping & 0x04) >> 1;
1995 mask |= (mapping & 0x10) >> 2;
1996 mask |= (mapping & 0x40) >> 3;
1997
1998 /* disable smart tach according to the mask */
1999 data->sfc2 = lm93_read_byte(client, LM93_REG_SFC2);
2000 data->sfc2 &= ~mask;
2001 lm93_write_byte(client, LM93_REG_SFC2, data->sfc2);
2002}
2003
2004static ssize_t store_pwm_freq(struct device *dev,
2005 struct device_attribute *attr,
2006 const char *buf, size_t count)
2007{
2008 int nr = (to_sensor_dev_attr(attr))->index;
Axel Lin6579d582014-07-02 20:36:50 +08002009 struct lm93_data *data = dev_get_drvdata(dev);
2010 struct i2c_client *client = data->client;
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02002011 u8 ctl4;
Guenter Roeck2804a4cf2012-01-14 21:25:54 -08002012 unsigned long val;
2013 int err;
2014
2015 err = kstrtoul(buf, 10, &val);
2016 if (err)
2017 return err;
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02002018
2019 mutex_lock(&data->update_lock);
Guenter Roeck2804a4cf2012-01-14 21:25:54 -08002020 ctl4 = lm93_read_byte(client, LM93_REG_PWM_CTL(nr, LM93_PWM_CTL4));
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02002021 ctl4 = (ctl4 & 0xf8) | LM93_PWM_FREQ_TO_REG(val);
2022 data->block9[nr][LM93_PWM_CTL4] = ctl4;
2023 /* ctl4 == 0 -> 22.5KHz -> disable smart tach */
2024 if (!ctl4)
2025 lm93_disable_fan_smart_tach(client, data, nr);
Guenter Roeck2804a4cf2012-01-14 21:25:54 -08002026 lm93_write_byte(client, LM93_REG_PWM_CTL(nr, LM93_PWM_CTL4), ctl4);
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02002027 mutex_unlock(&data->update_lock);
2028 return count;
2029}
2030
2031static SENSOR_DEVICE_ATTR(pwm1_freq, S_IWUSR | S_IRUGO,
2032 show_pwm_freq, store_pwm_freq, 0);
2033static SENSOR_DEVICE_ATTR(pwm2_freq, S_IWUSR | S_IRUGO,
2034 show_pwm_freq, store_pwm_freq, 1);
2035
2036static ssize_t show_pwm_auto_channels(struct device *dev,
2037 struct device_attribute *attr, char *buf)
2038{
2039 int nr = (to_sensor_dev_attr(attr))->index;
2040 struct lm93_data *data = lm93_update_device(dev);
Guenter Roeck2804a4cf2012-01-14 21:25:54 -08002041 return sprintf(buf, "%d\n", data->block9[nr][LM93_PWM_CTL1]);
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02002042}
2043
2044static ssize_t store_pwm_auto_channels(struct device *dev,
2045 struct device_attribute *attr,
2046 const char *buf, size_t count)
2047{
2048 int nr = (to_sensor_dev_attr(attr))->index;
Axel Lin6579d582014-07-02 20:36:50 +08002049 struct lm93_data *data = dev_get_drvdata(dev);
2050 struct i2c_client *client = data->client;
Guenter Roeck2804a4cf2012-01-14 21:25:54 -08002051 unsigned long val;
2052 int err;
2053
2054 err = kstrtoul(buf, 10, &val);
2055 if (err)
2056 return err;
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02002057
2058 mutex_lock(&data->update_lock);
Guenter Roeck2a844c12013-01-09 08:09:34 -08002059 data->block9[nr][LM93_PWM_CTL1] = clamp_val(val, 0, 255);
Guenter Roeck2804a4cf2012-01-14 21:25:54 -08002060 lm93_write_byte(client, LM93_REG_PWM_CTL(nr, LM93_PWM_CTL1),
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02002061 data->block9[nr][LM93_PWM_CTL1]);
2062 mutex_unlock(&data->update_lock);
2063 return count;
2064}
2065
2066static SENSOR_DEVICE_ATTR(pwm1_auto_channels, S_IWUSR | S_IRUGO,
2067 show_pwm_auto_channels, store_pwm_auto_channels, 0);
2068static SENSOR_DEVICE_ATTR(pwm2_auto_channels, S_IWUSR | S_IRUGO,
2069 show_pwm_auto_channels, store_pwm_auto_channels, 1);
2070
2071static ssize_t show_pwm_auto_spinup_min(struct device *dev,
Guenter Roeck2804a4cf2012-01-14 21:25:54 -08002072 struct device_attribute *attr, char *buf)
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02002073{
2074 int nr = (to_sensor_dev_attr(attr))->index;
2075 struct lm93_data *data = lm93_update_device(dev);
2076 u8 ctl3, ctl4;
2077
2078 ctl3 = data->block9[nr][LM93_PWM_CTL3];
2079 ctl4 = data->block9[nr][LM93_PWM_CTL4];
Guenter Roeck2804a4cf2012-01-14 21:25:54 -08002080 return sprintf(buf, "%d\n",
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02002081 LM93_PWM_FROM_REG(ctl3 & 0x0f, (ctl4 & 0x07) ?
2082 LM93_PWM_MAP_LO_FREQ : LM93_PWM_MAP_HI_FREQ));
2083}
2084
2085static ssize_t store_pwm_auto_spinup_min(struct device *dev,
2086 struct device_attribute *attr,
2087 const char *buf, size_t count)
2088{
2089 int nr = (to_sensor_dev_attr(attr))->index;
Axel Lin6579d582014-07-02 20:36:50 +08002090 struct lm93_data *data = dev_get_drvdata(dev);
2091 struct i2c_client *client = data->client;
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02002092 u8 ctl3, ctl4;
Guenter Roeck2804a4cf2012-01-14 21:25:54 -08002093 unsigned long val;
2094 int err;
2095
2096 err = kstrtoul(buf, 10, &val);
2097 if (err)
2098 return err;
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02002099
2100 mutex_lock(&data->update_lock);
Guenter Roeck2804a4cf2012-01-14 21:25:54 -08002101 ctl3 = lm93_read_byte(client, LM93_REG_PWM_CTL(nr, LM93_PWM_CTL3));
2102 ctl4 = lm93_read_byte(client, LM93_REG_PWM_CTL(nr, LM93_PWM_CTL4));
2103 ctl3 = (ctl3 & 0xf0) | LM93_PWM_TO_REG(val, (ctl4 & 0x07) ?
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02002104 LM93_PWM_MAP_LO_FREQ :
2105 LM93_PWM_MAP_HI_FREQ);
2106 data->block9[nr][LM93_PWM_CTL3] = ctl3;
Guenter Roeck2804a4cf2012-01-14 21:25:54 -08002107 lm93_write_byte(client, LM93_REG_PWM_CTL(nr, LM93_PWM_CTL3), ctl3);
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02002108 mutex_unlock(&data->update_lock);
2109 return count;
2110}
2111
2112static SENSOR_DEVICE_ATTR(pwm1_auto_spinup_min, S_IWUSR | S_IRUGO,
2113 show_pwm_auto_spinup_min,
2114 store_pwm_auto_spinup_min, 0);
2115static SENSOR_DEVICE_ATTR(pwm2_auto_spinup_min, S_IWUSR | S_IRUGO,
2116 show_pwm_auto_spinup_min,
2117 store_pwm_auto_spinup_min, 1);
2118
2119static ssize_t show_pwm_auto_spinup_time(struct device *dev,
2120 struct device_attribute *attr, char *buf)
2121{
2122 int nr = (to_sensor_dev_attr(attr))->index;
2123 struct lm93_data *data = lm93_update_device(dev);
Guenter Roeck2804a4cf2012-01-14 21:25:54 -08002124 return sprintf(buf, "%d\n", LM93_SPINUP_TIME_FROM_REG(
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02002125 data->block9[nr][LM93_PWM_CTL3]));
2126}
2127
2128static ssize_t store_pwm_auto_spinup_time(struct device *dev,
2129 struct device_attribute *attr,
2130 const char *buf, size_t count)
2131{
2132 int nr = (to_sensor_dev_attr(attr))->index;
Axel Lin6579d582014-07-02 20:36:50 +08002133 struct lm93_data *data = dev_get_drvdata(dev);
2134 struct i2c_client *client = data->client;
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02002135 u8 ctl3;
Guenter Roeck2804a4cf2012-01-14 21:25:54 -08002136 unsigned long val;
2137 int err;
2138
2139 err = kstrtoul(buf, 10, &val);
2140 if (err)
2141 return err;
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02002142
2143 mutex_lock(&data->update_lock);
Guenter Roeck2804a4cf2012-01-14 21:25:54 -08002144 ctl3 = lm93_read_byte(client, LM93_REG_PWM_CTL(nr, LM93_PWM_CTL3));
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02002145 ctl3 = (ctl3 & 0x1f) | (LM93_SPINUP_TIME_TO_REG(val) << 5 & 0xe0);
2146 data->block9[nr][LM93_PWM_CTL3] = ctl3;
Guenter Roeck2804a4cf2012-01-14 21:25:54 -08002147 lm93_write_byte(client, LM93_REG_PWM_CTL(nr, LM93_PWM_CTL3), ctl3);
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02002148 mutex_unlock(&data->update_lock);
2149 return count;
2150}
2151
2152static SENSOR_DEVICE_ATTR(pwm1_auto_spinup_time, S_IWUSR | S_IRUGO,
2153 show_pwm_auto_spinup_time,
2154 store_pwm_auto_spinup_time, 0);
2155static SENSOR_DEVICE_ATTR(pwm2_auto_spinup_time, S_IWUSR | S_IRUGO,
2156 show_pwm_auto_spinup_time,
2157 store_pwm_auto_spinup_time, 1);
2158
Julia Lawallce847a32016-12-22 13:04:55 +01002159static ssize_t pwm_auto_prochot_ramp_show(struct device *dev,
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02002160 struct device_attribute *attr, char *buf)
2161{
2162 struct lm93_data *data = lm93_update_device(dev);
Guenter Roeck2804a4cf2012-01-14 21:25:54 -08002163 return sprintf(buf, "%d\n",
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02002164 LM93_RAMP_FROM_REG(data->pwm_ramp_ctl >> 4 & 0x0f));
2165}
2166
Julia Lawallce847a32016-12-22 13:04:55 +01002167static ssize_t pwm_auto_prochot_ramp_store(struct device *dev,
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02002168 struct device_attribute *attr,
2169 const char *buf, size_t count)
2170{
Axel Lin6579d582014-07-02 20:36:50 +08002171 struct lm93_data *data = dev_get_drvdata(dev);
2172 struct i2c_client *client = data->client;
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02002173 u8 ramp;
Guenter Roeck2804a4cf2012-01-14 21:25:54 -08002174 unsigned long val;
2175 int err;
2176
2177 err = kstrtoul(buf, 10, &val);
2178 if (err)
2179 return err;
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02002180
2181 mutex_lock(&data->update_lock);
2182 ramp = lm93_read_byte(client, LM93_REG_PWM_RAMP_CTL);
2183 ramp = (ramp & 0x0f) | (LM93_RAMP_TO_REG(val) << 4 & 0xf0);
2184 lm93_write_byte(client, LM93_REG_PWM_RAMP_CTL, ramp);
2185 mutex_unlock(&data->update_lock);
2186 return count;
2187}
2188
Julia Lawallce847a32016-12-22 13:04:55 +01002189static DEVICE_ATTR_RW(pwm_auto_prochot_ramp);
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02002190
Julia Lawallce847a32016-12-22 13:04:55 +01002191static ssize_t pwm_auto_vrdhot_ramp_show(struct device *dev,
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02002192 struct device_attribute *attr, char *buf)
2193{
2194 struct lm93_data *data = lm93_update_device(dev);
Guenter Roeck2804a4cf2012-01-14 21:25:54 -08002195 return sprintf(buf, "%d\n",
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02002196 LM93_RAMP_FROM_REG(data->pwm_ramp_ctl & 0x0f));
2197}
2198
Julia Lawallce847a32016-12-22 13:04:55 +01002199static ssize_t pwm_auto_vrdhot_ramp_store(struct device *dev,
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02002200 struct device_attribute *attr,
2201 const char *buf, size_t count)
2202{
Axel Lin6579d582014-07-02 20:36:50 +08002203 struct lm93_data *data = dev_get_drvdata(dev);
2204 struct i2c_client *client = data->client;
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02002205 u8 ramp;
Guenter Roeck2804a4cf2012-01-14 21:25:54 -08002206 unsigned long val;
2207 int err;
2208
2209 err = kstrtoul(buf, 10, &val);
2210 if (err)
2211 return err;
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02002212
2213 mutex_lock(&data->update_lock);
2214 ramp = lm93_read_byte(client, LM93_REG_PWM_RAMP_CTL);
2215 ramp = (ramp & 0xf0) | (LM93_RAMP_TO_REG(val) & 0x0f);
2216 lm93_write_byte(client, LM93_REG_PWM_RAMP_CTL, ramp);
2217 mutex_unlock(&data->update_lock);
2218 return 0;
2219}
2220
Julia Lawallce847a32016-12-22 13:04:55 +01002221static DEVICE_ATTR_RW(pwm_auto_vrdhot_ramp);
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02002222
2223static ssize_t show_vid(struct device *dev, struct device_attribute *attr,
2224 char *buf)
2225{
2226 int nr = (to_sensor_dev_attr(attr))->index;
2227 struct lm93_data *data = lm93_update_device(dev);
Guenter Roeck2804a4cf2012-01-14 21:25:54 -08002228 return sprintf(buf, "%d\n", LM93_VID_FROM_REG(data->vid[nr]));
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02002229}
2230
Jean Delvaree7f62302007-09-04 14:03:43 +02002231static SENSOR_DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid, NULL, 0);
2232static SENSOR_DEVICE_ATTR(cpu1_vid, S_IRUGO, show_vid, NULL, 1);
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02002233
2234static ssize_t show_prochot(struct device *dev, struct device_attribute *attr,
2235 char *buf)
2236{
2237 int nr = (to_sensor_dev_attr(attr))->index;
2238 struct lm93_data *data = lm93_update_device(dev);
Guenter Roeck2804a4cf2012-01-14 21:25:54 -08002239 return sprintf(buf, "%d\n", data->block4[nr].cur);
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02002240}
2241
2242static SENSOR_DEVICE_ATTR(prochot1, S_IRUGO, show_prochot, NULL, 0);
2243static SENSOR_DEVICE_ATTR(prochot2, S_IRUGO, show_prochot, NULL, 1);
2244
2245static ssize_t show_prochot_avg(struct device *dev,
2246 struct device_attribute *attr, char *buf)
2247{
2248 int nr = (to_sensor_dev_attr(attr))->index;
2249 struct lm93_data *data = lm93_update_device(dev);
Guenter Roeck2804a4cf2012-01-14 21:25:54 -08002250 return sprintf(buf, "%d\n", data->block4[nr].avg);
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02002251}
2252
2253static SENSOR_DEVICE_ATTR(prochot1_avg, S_IRUGO, show_prochot_avg, NULL, 0);
2254static SENSOR_DEVICE_ATTR(prochot2_avg, S_IRUGO, show_prochot_avg, NULL, 1);
2255
2256static ssize_t show_prochot_max(struct device *dev,
2257 struct device_attribute *attr, char *buf)
2258{
2259 int nr = (to_sensor_dev_attr(attr))->index;
2260 struct lm93_data *data = lm93_update_device(dev);
Guenter Roeck2804a4cf2012-01-14 21:25:54 -08002261 return sprintf(buf, "%d\n", data->prochot_max[nr]);
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02002262}
2263
2264static ssize_t store_prochot_max(struct device *dev,
2265 struct device_attribute *attr,
2266 const char *buf, size_t count)
2267{
2268 int nr = (to_sensor_dev_attr(attr))->index;
Axel Lin6579d582014-07-02 20:36:50 +08002269 struct lm93_data *data = dev_get_drvdata(dev);
2270 struct i2c_client *client = data->client;
Guenter Roeck2804a4cf2012-01-14 21:25:54 -08002271 unsigned long val;
2272 int err;
2273
2274 err = kstrtoul(buf, 10, &val);
2275 if (err)
2276 return err;
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02002277
2278 mutex_lock(&data->update_lock);
2279 data->prochot_max[nr] = LM93_PROCHOT_TO_REG(val);
2280 lm93_write_byte(client, LM93_REG_PROCHOT_MAX(nr),
2281 data->prochot_max[nr]);
2282 mutex_unlock(&data->update_lock);
2283 return count;
2284}
2285
2286static SENSOR_DEVICE_ATTR(prochot1_max, S_IWUSR | S_IRUGO,
2287 show_prochot_max, store_prochot_max, 0);
2288static SENSOR_DEVICE_ATTR(prochot2_max, S_IWUSR | S_IRUGO,
2289 show_prochot_max, store_prochot_max, 1);
2290
2291static const u8 prochot_override_mask[] = { 0x80, 0x40 };
2292
2293static ssize_t show_prochot_override(struct device *dev,
2294 struct device_attribute *attr, char *buf)
2295{
2296 int nr = (to_sensor_dev_attr(attr))->index;
2297 struct lm93_data *data = lm93_update_device(dev);
Guenter Roeck2804a4cf2012-01-14 21:25:54 -08002298 return sprintf(buf, "%d\n",
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02002299 (data->prochot_override & prochot_override_mask[nr]) ? 1 : 0);
2300}
2301
2302static ssize_t store_prochot_override(struct device *dev,
2303 struct device_attribute *attr,
2304 const char *buf, size_t count)
2305{
2306 int nr = (to_sensor_dev_attr(attr))->index;
Axel Lin6579d582014-07-02 20:36:50 +08002307 struct lm93_data *data = dev_get_drvdata(dev);
2308 struct i2c_client *client = data->client;
Guenter Roeck2804a4cf2012-01-14 21:25:54 -08002309 unsigned long val;
2310 int err;
2311
2312 err = kstrtoul(buf, 10, &val);
2313 if (err)
2314 return err;
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02002315
2316 mutex_lock(&data->update_lock);
2317 if (val)
2318 data->prochot_override |= prochot_override_mask[nr];
2319 else
2320 data->prochot_override &= (~prochot_override_mask[nr]);
2321 lm93_write_byte(client, LM93_REG_PROCHOT_OVERRIDE,
2322 data->prochot_override);
2323 mutex_unlock(&data->update_lock);
2324 return count;
2325}
2326
2327static SENSOR_DEVICE_ATTR(prochot1_override, S_IWUSR | S_IRUGO,
2328 show_prochot_override, store_prochot_override, 0);
2329static SENSOR_DEVICE_ATTR(prochot2_override, S_IWUSR | S_IRUGO,
2330 show_prochot_override, store_prochot_override, 1);
2331
2332static ssize_t show_prochot_interval(struct device *dev,
2333 struct device_attribute *attr, char *buf)
2334{
2335 int nr = (to_sensor_dev_attr(attr))->index;
2336 struct lm93_data *data = lm93_update_device(dev);
2337 u8 tmp;
Guenter Roeck2804a4cf2012-01-14 21:25:54 -08002338 if (nr == 1)
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02002339 tmp = (data->prochot_interval & 0xf0) >> 4;
2340 else
2341 tmp = data->prochot_interval & 0x0f;
Guenter Roeck2804a4cf2012-01-14 21:25:54 -08002342 return sprintf(buf, "%d\n", LM93_INTERVAL_FROM_REG(tmp));
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02002343}
2344
2345static ssize_t store_prochot_interval(struct device *dev,
2346 struct device_attribute *attr,
2347 const char *buf, size_t count)
2348{
2349 int nr = (to_sensor_dev_attr(attr))->index;
Axel Lin6579d582014-07-02 20:36:50 +08002350 struct lm93_data *data = dev_get_drvdata(dev);
2351 struct i2c_client *client = data->client;
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02002352 u8 tmp;
Guenter Roeck2804a4cf2012-01-14 21:25:54 -08002353 unsigned long val;
2354 int err;
2355
2356 err = kstrtoul(buf, 10, &val);
2357 if (err)
2358 return err;
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02002359
2360 mutex_lock(&data->update_lock);
2361 tmp = lm93_read_byte(client, LM93_REG_PROCHOT_INTERVAL);
Guenter Roeck2804a4cf2012-01-14 21:25:54 -08002362 if (nr == 1)
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02002363 tmp = (tmp & 0x0f) | (LM93_INTERVAL_TO_REG(val) << 4);
2364 else
2365 tmp = (tmp & 0xf0) | LM93_INTERVAL_TO_REG(val);
2366 data->prochot_interval = tmp;
2367 lm93_write_byte(client, LM93_REG_PROCHOT_INTERVAL, tmp);
2368 mutex_unlock(&data->update_lock);
2369 return count;
2370}
2371
2372static SENSOR_DEVICE_ATTR(prochot1_interval, S_IWUSR | S_IRUGO,
2373 show_prochot_interval, store_prochot_interval, 0);
2374static SENSOR_DEVICE_ATTR(prochot2_interval, S_IWUSR | S_IRUGO,
2375 show_prochot_interval, store_prochot_interval, 1);
2376
Julia Lawallce847a32016-12-22 13:04:55 +01002377static ssize_t prochot_override_duty_cycle_show(struct device *dev,
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02002378 struct device_attribute *attr,
2379 char *buf)
2380{
2381 struct lm93_data *data = lm93_update_device(dev);
Guenter Roeck2804a4cf2012-01-14 21:25:54 -08002382 return sprintf(buf, "%d\n", data->prochot_override & 0x0f);
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02002383}
2384
Julia Lawallce847a32016-12-22 13:04:55 +01002385static ssize_t prochot_override_duty_cycle_store(struct device *dev,
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02002386 struct device_attribute *attr,
2387 const char *buf, size_t count)
2388{
Axel Lin6579d582014-07-02 20:36:50 +08002389 struct lm93_data *data = dev_get_drvdata(dev);
2390 struct i2c_client *client = data->client;
Guenter Roeck2804a4cf2012-01-14 21:25:54 -08002391 unsigned long val;
2392 int err;
2393
2394 err = kstrtoul(buf, 10, &val);
2395 if (err)
2396 return err;
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02002397
2398 mutex_lock(&data->update_lock);
2399 data->prochot_override = (data->prochot_override & 0xf0) |
Guenter Roeck2a844c12013-01-09 08:09:34 -08002400 clamp_val(val, 0, 15);
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02002401 lm93_write_byte(client, LM93_REG_PROCHOT_OVERRIDE,
2402 data->prochot_override);
2403 mutex_unlock(&data->update_lock);
2404 return count;
2405}
2406
Julia Lawallce847a32016-12-22 13:04:55 +01002407static DEVICE_ATTR_RW(prochot_override_duty_cycle);
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02002408
Julia Lawallce847a32016-12-22 13:04:55 +01002409static ssize_t prochot_short_show(struct device *dev,
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02002410 struct device_attribute *attr, char *buf)
2411{
2412 struct lm93_data *data = lm93_update_device(dev);
Guenter Roeck2804a4cf2012-01-14 21:25:54 -08002413 return sprintf(buf, "%d\n", (data->config & 0x10) ? 1 : 0);
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02002414}
2415
Julia Lawallce847a32016-12-22 13:04:55 +01002416static ssize_t prochot_short_store(struct device *dev,
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02002417 struct device_attribute *attr,
2418 const char *buf, size_t count)
2419{
Axel Lin6579d582014-07-02 20:36:50 +08002420 struct lm93_data *data = dev_get_drvdata(dev);
2421 struct i2c_client *client = data->client;
Guenter Roeck2804a4cf2012-01-14 21:25:54 -08002422 unsigned long val;
2423 int err;
2424
2425 err = kstrtoul(buf, 10, &val);
2426 if (err)
2427 return err;
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02002428
2429 mutex_lock(&data->update_lock);
2430 if (val)
2431 data->config |= 0x10;
2432 else
2433 data->config &= ~0x10;
2434 lm93_write_byte(client, LM93_REG_CONFIG, data->config);
2435 mutex_unlock(&data->update_lock);
2436 return count;
2437}
2438
Julia Lawallce847a32016-12-22 13:04:55 +01002439static DEVICE_ATTR_RW(prochot_short);
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02002440
2441static ssize_t show_vrdhot(struct device *dev, struct device_attribute *attr,
2442 char *buf)
2443{
2444 int nr = (to_sensor_dev_attr(attr))->index;
2445 struct lm93_data *data = lm93_update_device(dev);
Guenter Roeck2804a4cf2012-01-14 21:25:54 -08002446 return sprintf(buf, "%d\n",
2447 data->block1.host_status_1 & (1 << (nr + 4)) ? 1 : 0);
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02002448}
2449
2450static SENSOR_DEVICE_ATTR(vrdhot1, S_IRUGO, show_vrdhot, NULL, 0);
2451static SENSOR_DEVICE_ATTR(vrdhot2, S_IRUGO, show_vrdhot, NULL, 1);
2452
Julia Lawallce847a32016-12-22 13:04:55 +01002453static ssize_t gpio_show(struct device *dev, struct device_attribute *attr,
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02002454 char *buf)
2455{
2456 struct lm93_data *data = lm93_update_device(dev);
Guenter Roeck2804a4cf2012-01-14 21:25:54 -08002457 return sprintf(buf, "%d\n", LM93_GPI_FROM_REG(data->gpi));
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02002458}
2459
Julia Lawallce847a32016-12-22 13:04:55 +01002460static DEVICE_ATTR_RO(gpio);
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02002461
Julia Lawallce847a32016-12-22 13:04:55 +01002462static ssize_t alarms_show(struct device *dev, struct device_attribute *attr,
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02002463 char *buf)
2464{
2465 struct lm93_data *data = lm93_update_device(dev);
Guenter Roeck2804a4cf2012-01-14 21:25:54 -08002466 return sprintf(buf, "%d\n", LM93_ALARMS_FROM_REG(data->block1));
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02002467}
2468
Julia Lawallce847a32016-12-22 13:04:55 +01002469static DEVICE_ATTR_RO(alarms);
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02002470
2471static struct attribute *lm93_attrs[] = {
2472 &sensor_dev_attr_in1_input.dev_attr.attr,
2473 &sensor_dev_attr_in2_input.dev_attr.attr,
2474 &sensor_dev_attr_in3_input.dev_attr.attr,
2475 &sensor_dev_attr_in4_input.dev_attr.attr,
2476 &sensor_dev_attr_in5_input.dev_attr.attr,
2477 &sensor_dev_attr_in6_input.dev_attr.attr,
2478 &sensor_dev_attr_in7_input.dev_attr.attr,
2479 &sensor_dev_attr_in8_input.dev_attr.attr,
2480 &sensor_dev_attr_in9_input.dev_attr.attr,
2481 &sensor_dev_attr_in10_input.dev_attr.attr,
2482 &sensor_dev_attr_in11_input.dev_attr.attr,
2483 &sensor_dev_attr_in12_input.dev_attr.attr,
2484 &sensor_dev_attr_in13_input.dev_attr.attr,
2485 &sensor_dev_attr_in14_input.dev_attr.attr,
2486 &sensor_dev_attr_in15_input.dev_attr.attr,
2487 &sensor_dev_attr_in16_input.dev_attr.attr,
2488 &sensor_dev_attr_in1_min.dev_attr.attr,
2489 &sensor_dev_attr_in2_min.dev_attr.attr,
2490 &sensor_dev_attr_in3_min.dev_attr.attr,
2491 &sensor_dev_attr_in4_min.dev_attr.attr,
2492 &sensor_dev_attr_in5_min.dev_attr.attr,
2493 &sensor_dev_attr_in6_min.dev_attr.attr,
2494 &sensor_dev_attr_in7_min.dev_attr.attr,
2495 &sensor_dev_attr_in8_min.dev_attr.attr,
2496 &sensor_dev_attr_in9_min.dev_attr.attr,
2497 &sensor_dev_attr_in10_min.dev_attr.attr,
2498 &sensor_dev_attr_in11_min.dev_attr.attr,
2499 &sensor_dev_attr_in12_min.dev_attr.attr,
2500 &sensor_dev_attr_in13_min.dev_attr.attr,
2501 &sensor_dev_attr_in14_min.dev_attr.attr,
2502 &sensor_dev_attr_in15_min.dev_attr.attr,
2503 &sensor_dev_attr_in16_min.dev_attr.attr,
2504 &sensor_dev_attr_in1_max.dev_attr.attr,
2505 &sensor_dev_attr_in2_max.dev_attr.attr,
2506 &sensor_dev_attr_in3_max.dev_attr.attr,
2507 &sensor_dev_attr_in4_max.dev_attr.attr,
2508 &sensor_dev_attr_in5_max.dev_attr.attr,
2509 &sensor_dev_attr_in6_max.dev_attr.attr,
2510 &sensor_dev_attr_in7_max.dev_attr.attr,
2511 &sensor_dev_attr_in8_max.dev_attr.attr,
2512 &sensor_dev_attr_in9_max.dev_attr.attr,
2513 &sensor_dev_attr_in10_max.dev_attr.attr,
2514 &sensor_dev_attr_in11_max.dev_attr.attr,
2515 &sensor_dev_attr_in12_max.dev_attr.attr,
2516 &sensor_dev_attr_in13_max.dev_attr.attr,
2517 &sensor_dev_attr_in14_max.dev_attr.attr,
2518 &sensor_dev_attr_in15_max.dev_attr.attr,
2519 &sensor_dev_attr_in16_max.dev_attr.attr,
2520 &sensor_dev_attr_temp1_input.dev_attr.attr,
2521 &sensor_dev_attr_temp2_input.dev_attr.attr,
2522 &sensor_dev_attr_temp3_input.dev_attr.attr,
2523 &sensor_dev_attr_temp1_min.dev_attr.attr,
2524 &sensor_dev_attr_temp2_min.dev_attr.attr,
2525 &sensor_dev_attr_temp3_min.dev_attr.attr,
2526 &sensor_dev_attr_temp1_max.dev_attr.attr,
2527 &sensor_dev_attr_temp2_max.dev_attr.attr,
2528 &sensor_dev_attr_temp3_max.dev_attr.attr,
2529 &sensor_dev_attr_temp1_auto_base.dev_attr.attr,
2530 &sensor_dev_attr_temp2_auto_base.dev_attr.attr,
2531 &sensor_dev_attr_temp3_auto_base.dev_attr.attr,
2532 &sensor_dev_attr_temp1_auto_boost.dev_attr.attr,
2533 &sensor_dev_attr_temp2_auto_boost.dev_attr.attr,
2534 &sensor_dev_attr_temp3_auto_boost.dev_attr.attr,
2535 &sensor_dev_attr_temp1_auto_boost_hyst.dev_attr.attr,
2536 &sensor_dev_attr_temp2_auto_boost_hyst.dev_attr.attr,
2537 &sensor_dev_attr_temp3_auto_boost_hyst.dev_attr.attr,
2538 &sensor_dev_attr_temp1_auto_offset1.dev_attr.attr,
2539 &sensor_dev_attr_temp1_auto_offset2.dev_attr.attr,
2540 &sensor_dev_attr_temp1_auto_offset3.dev_attr.attr,
2541 &sensor_dev_attr_temp1_auto_offset4.dev_attr.attr,
2542 &sensor_dev_attr_temp1_auto_offset5.dev_attr.attr,
2543 &sensor_dev_attr_temp1_auto_offset6.dev_attr.attr,
2544 &sensor_dev_attr_temp1_auto_offset7.dev_attr.attr,
2545 &sensor_dev_attr_temp1_auto_offset8.dev_attr.attr,
2546 &sensor_dev_attr_temp1_auto_offset9.dev_attr.attr,
2547 &sensor_dev_attr_temp1_auto_offset10.dev_attr.attr,
2548 &sensor_dev_attr_temp1_auto_offset11.dev_attr.attr,
2549 &sensor_dev_attr_temp1_auto_offset12.dev_attr.attr,
2550 &sensor_dev_attr_temp2_auto_offset1.dev_attr.attr,
2551 &sensor_dev_attr_temp2_auto_offset2.dev_attr.attr,
2552 &sensor_dev_attr_temp2_auto_offset3.dev_attr.attr,
2553 &sensor_dev_attr_temp2_auto_offset4.dev_attr.attr,
2554 &sensor_dev_attr_temp2_auto_offset5.dev_attr.attr,
2555 &sensor_dev_attr_temp2_auto_offset6.dev_attr.attr,
2556 &sensor_dev_attr_temp2_auto_offset7.dev_attr.attr,
2557 &sensor_dev_attr_temp2_auto_offset8.dev_attr.attr,
2558 &sensor_dev_attr_temp2_auto_offset9.dev_attr.attr,
2559 &sensor_dev_attr_temp2_auto_offset10.dev_attr.attr,
2560 &sensor_dev_attr_temp2_auto_offset11.dev_attr.attr,
2561 &sensor_dev_attr_temp2_auto_offset12.dev_attr.attr,
2562 &sensor_dev_attr_temp3_auto_offset1.dev_attr.attr,
2563 &sensor_dev_attr_temp3_auto_offset2.dev_attr.attr,
2564 &sensor_dev_attr_temp3_auto_offset3.dev_attr.attr,
2565 &sensor_dev_attr_temp3_auto_offset4.dev_attr.attr,
2566 &sensor_dev_attr_temp3_auto_offset5.dev_attr.attr,
2567 &sensor_dev_attr_temp3_auto_offset6.dev_attr.attr,
2568 &sensor_dev_attr_temp3_auto_offset7.dev_attr.attr,
2569 &sensor_dev_attr_temp3_auto_offset8.dev_attr.attr,
2570 &sensor_dev_attr_temp3_auto_offset9.dev_attr.attr,
2571 &sensor_dev_attr_temp3_auto_offset10.dev_attr.attr,
2572 &sensor_dev_attr_temp3_auto_offset11.dev_attr.attr,
2573 &sensor_dev_attr_temp3_auto_offset12.dev_attr.attr,
2574 &sensor_dev_attr_temp1_auto_pwm_min.dev_attr.attr,
2575 &sensor_dev_attr_temp2_auto_pwm_min.dev_attr.attr,
2576 &sensor_dev_attr_temp3_auto_pwm_min.dev_attr.attr,
2577 &sensor_dev_attr_temp1_auto_offset_hyst.dev_attr.attr,
2578 &sensor_dev_attr_temp2_auto_offset_hyst.dev_attr.attr,
2579 &sensor_dev_attr_temp3_auto_offset_hyst.dev_attr.attr,
2580 &sensor_dev_attr_fan1_input.dev_attr.attr,
2581 &sensor_dev_attr_fan2_input.dev_attr.attr,
2582 &sensor_dev_attr_fan3_input.dev_attr.attr,
2583 &sensor_dev_attr_fan4_input.dev_attr.attr,
2584 &sensor_dev_attr_fan1_min.dev_attr.attr,
2585 &sensor_dev_attr_fan2_min.dev_attr.attr,
2586 &sensor_dev_attr_fan3_min.dev_attr.attr,
2587 &sensor_dev_attr_fan4_min.dev_attr.attr,
2588 &sensor_dev_attr_fan1_smart_tach.dev_attr.attr,
2589 &sensor_dev_attr_fan2_smart_tach.dev_attr.attr,
2590 &sensor_dev_attr_fan3_smart_tach.dev_attr.attr,
2591 &sensor_dev_attr_fan4_smart_tach.dev_attr.attr,
2592 &sensor_dev_attr_pwm1.dev_attr.attr,
2593 &sensor_dev_attr_pwm2.dev_attr.attr,
2594 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
2595 &sensor_dev_attr_pwm2_enable.dev_attr.attr,
2596 &sensor_dev_attr_pwm1_freq.dev_attr.attr,
2597 &sensor_dev_attr_pwm2_freq.dev_attr.attr,
2598 &sensor_dev_attr_pwm1_auto_channels.dev_attr.attr,
2599 &sensor_dev_attr_pwm2_auto_channels.dev_attr.attr,
2600 &sensor_dev_attr_pwm1_auto_spinup_min.dev_attr.attr,
2601 &sensor_dev_attr_pwm2_auto_spinup_min.dev_attr.attr,
2602 &sensor_dev_attr_pwm1_auto_spinup_time.dev_attr.attr,
2603 &sensor_dev_attr_pwm2_auto_spinup_time.dev_attr.attr,
2604 &dev_attr_pwm_auto_prochot_ramp.attr,
2605 &dev_attr_pwm_auto_vrdhot_ramp.attr,
Jean Delvaree7f62302007-09-04 14:03:43 +02002606 &sensor_dev_attr_cpu0_vid.dev_attr.attr,
2607 &sensor_dev_attr_cpu1_vid.dev_attr.attr,
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02002608 &sensor_dev_attr_prochot1.dev_attr.attr,
2609 &sensor_dev_attr_prochot2.dev_attr.attr,
2610 &sensor_dev_attr_prochot1_avg.dev_attr.attr,
2611 &sensor_dev_attr_prochot2_avg.dev_attr.attr,
2612 &sensor_dev_attr_prochot1_max.dev_attr.attr,
2613 &sensor_dev_attr_prochot2_max.dev_attr.attr,
2614 &sensor_dev_attr_prochot1_override.dev_attr.attr,
2615 &sensor_dev_attr_prochot2_override.dev_attr.attr,
2616 &sensor_dev_attr_prochot1_interval.dev_attr.attr,
2617 &sensor_dev_attr_prochot2_interval.dev_attr.attr,
2618 &dev_attr_prochot_override_duty_cycle.attr,
2619 &dev_attr_prochot_short.attr,
2620 &sensor_dev_attr_vrdhot1.dev_attr.attr,
2621 &sensor_dev_attr_vrdhot2.dev_attr.attr,
2622 &dev_attr_gpio.attr,
2623 &dev_attr_alarms.attr,
2624 NULL
2625};
2626
Axel Lin6579d582014-07-02 20:36:50 +08002627ATTRIBUTE_GROUPS(lm93);
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02002628
2629static void lm93_init_client(struct i2c_client *client)
2630{
2631 int i;
2632 u8 reg;
2633
2634 /* configure VID pin input thresholds */
2635 reg = lm93_read_byte(client, LM93_REG_GPI_VID_CTL);
2636 lm93_write_byte(client, LM93_REG_GPI_VID_CTL,
2637 reg | (vid_agtl ? 0x03 : 0x00));
2638
2639 if (init) {
2640 /* enable #ALERT pin */
2641 reg = lm93_read_byte(client, LM93_REG_CONFIG);
2642 lm93_write_byte(client, LM93_REG_CONFIG, reg | 0x08);
2643
2644 /* enable ASF mode for BMC status registers */
2645 reg = lm93_read_byte(client, LM93_REG_STATUS_CONTROL);
2646 lm93_write_byte(client, LM93_REG_STATUS_CONTROL, reg | 0x02);
2647
2648 /* set sleep state to S0 */
2649 lm93_write_byte(client, LM93_REG_SLEEP_CONTROL, 0);
2650
2651 /* unmask #VRDHOT and dynamic VCCP (if nec) error events */
2652 reg = lm93_read_byte(client, LM93_REG_MISC_ERR_MASK);
2653 reg &= ~0x03;
2654 reg &= ~(vccp_limit_type[0] ? 0x10 : 0);
2655 reg &= ~(vccp_limit_type[1] ? 0x20 : 0);
2656 lm93_write_byte(client, LM93_REG_MISC_ERR_MASK, reg);
2657 }
2658
2659 /* start monitoring */
2660 reg = lm93_read_byte(client, LM93_REG_CONFIG);
2661 lm93_write_byte(client, LM93_REG_CONFIG, reg | 0x01);
2662
2663 /* spin until ready */
Guenter Roeck2804a4cf2012-01-14 21:25:54 -08002664 for (i = 0; i < 20; i++) {
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02002665 msleep(10);
2666 if ((lm93_read_byte(client, LM93_REG_CONFIG) & 0x80) == 0x80)
2667 return;
2668 }
2669
Guenter Roeckb55f3752013-01-10 10:01:24 -08002670 dev_warn(&client->dev,
2671 "timed out waiting for sensor chip to signal ready!\n");
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02002672}
2673
Jean Delvare70b72402008-07-16 19:30:15 +02002674/* Return 0 if detection is successful, -ENODEV otherwise */
Jean Delvare310ec792009-12-14 21:17:23 +01002675static int lm93_detect(struct i2c_client *client, struct i2c_board_info *info)
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02002676{
Jean Delvare70b72402008-07-16 19:30:15 +02002677 struct i2c_adapter *adapter = client->adapter;
Jean Delvare52df6442009-12-09 20:35:57 +01002678 int mfr, ver;
Guenter Roeckc7bf71c2011-01-17 12:48:20 -08002679 const char *name;
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02002680
Jean Delvare70b72402008-07-16 19:30:15 +02002681 if (!i2c_check_functionality(adapter, LM93_SMBUS_FUNC_MIN))
2682 return -ENODEV;
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02002683
2684 /* detection */
Jean Delvare52df6442009-12-09 20:35:57 +01002685 mfr = lm93_read_byte(client, LM93_REG_MFR_ID);
2686 if (mfr != 0x01) {
2687 dev_dbg(&adapter->dev,
2688 "detect failed, bad manufacturer id 0x%02x!\n", mfr);
2689 return -ENODEV;
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02002690 }
2691
Jean Delvare52df6442009-12-09 20:35:57 +01002692 ver = lm93_read_byte(client, LM93_REG_VER);
Guenter Roeckc7bf71c2011-01-17 12:48:20 -08002693 switch (ver) {
2694 case LM93_MFR_ID:
2695 case LM93_MFR_ID_PROTOTYPE:
2696 name = "lm93";
2697 break;
2698 case LM94_MFR_ID_2:
2699 case LM94_MFR_ID:
2700 case LM94_MFR_ID_PROTOTYPE:
2701 name = "lm94";
2702 break;
2703 default:
Jean Delvare52df6442009-12-09 20:35:57 +01002704 dev_dbg(&adapter->dev,
2705 "detect failed, bad version id 0x%02x!\n", ver);
2706 return -ENODEV;
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02002707 }
2708
Guenter Roeckc7bf71c2011-01-17 12:48:20 -08002709 strlcpy(info->type, name, I2C_NAME_SIZE);
Guenter Roeck2804a4cf2012-01-14 21:25:54 -08002710 dev_dbg(&adapter->dev, "loading %s at %d, 0x%02x\n",
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02002711 client->name, i2c_adapter_id(client->adapter),
2712 client->addr);
2713
Jean Delvare70b72402008-07-16 19:30:15 +02002714 return 0;
2715}
2716
2717static int lm93_probe(struct i2c_client *client,
2718 const struct i2c_device_id *id)
2719{
Axel Lin6579d582014-07-02 20:36:50 +08002720 struct device *dev = &client->dev;
Jean Delvare70b72402008-07-16 19:30:15 +02002721 struct lm93_data *data;
Axel Lin6579d582014-07-02 20:36:50 +08002722 struct device *hwmon_dev;
2723 int func;
Jean Delvare70b72402008-07-16 19:30:15 +02002724 void (*update)(struct lm93_data *, struct i2c_client *);
2725
2726 /* choose update routine based on bus capabilities */
2727 func = i2c_get_functionality(client->adapter);
2728 if (((LM93_SMBUS_FUNC_FULL & func) == LM93_SMBUS_FUNC_FULL) &&
2729 (!disable_block)) {
Axel Lin6579d582014-07-02 20:36:50 +08002730 dev_dbg(dev, "using SMBus block data transactions\n");
Jean Delvare70b72402008-07-16 19:30:15 +02002731 update = lm93_update_client_full;
2732 } else if ((LM93_SMBUS_FUNC_MIN & func) == LM93_SMBUS_FUNC_MIN) {
Axel Lin6579d582014-07-02 20:36:50 +08002733 dev_dbg(dev, "disabled SMBus block data transactions\n");
Jean Delvare70b72402008-07-16 19:30:15 +02002734 update = lm93_update_client_min;
2735 } else {
Axel Lin6579d582014-07-02 20:36:50 +08002736 dev_dbg(dev, "detect failed, smbus byte and/or word data not supported!\n");
Guenter Roeck4a8ad252012-06-02 09:58:11 -07002737 return -ENODEV;
Jean Delvare70b72402008-07-16 19:30:15 +02002738 }
2739
Axel Lin6579d582014-07-02 20:36:50 +08002740 data = devm_kzalloc(dev, sizeof(struct lm93_data), GFP_KERNEL);
Jingoo Han567817d2014-04-29 17:09:19 +09002741 if (!data)
Guenter Roeck4a8ad252012-06-02 09:58:11 -07002742 return -ENOMEM;
Jean Delvare70b72402008-07-16 19:30:15 +02002743
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02002744 /* housekeeping */
Axel Lin6579d582014-07-02 20:36:50 +08002745 data->client = client;
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02002746 data->update = update;
2747 mutex_init(&data->update_lock);
2748
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02002749 /* initialize the chip */
2750 lm93_init_client(client);
2751
Axel Lin6579d582014-07-02 20:36:50 +08002752 hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
2753 data,
2754 lm93_groups);
2755 return PTR_ERR_OR_ZERO(hwmon_dev);
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02002756}
2757
Jean Delvare70b72402008-07-16 19:30:15 +02002758static const struct i2c_device_id lm93_id[] = {
Jean Delvare1f86df42009-12-14 21:17:26 +01002759 { "lm93", 0 },
Guenter Roeckc7bf71c2011-01-17 12:48:20 -08002760 { "lm94", 0 },
Jean Delvare70b72402008-07-16 19:30:15 +02002761 { }
2762};
2763MODULE_DEVICE_TABLE(i2c, lm93_id);
2764
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02002765static struct i2c_driver lm93_driver = {
Jean Delvare70b72402008-07-16 19:30:15 +02002766 .class = I2C_CLASS_HWMON,
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02002767 .driver = {
2768 .name = "lm93",
2769 },
Jean Delvare70b72402008-07-16 19:30:15 +02002770 .probe = lm93_probe,
Jean Delvare70b72402008-07-16 19:30:15 +02002771 .id_table = lm93_id,
2772 .detect = lm93_detect,
Jean Delvarec3813d62009-12-14 21:17:25 +01002773 .address_list = normal_i2c,
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02002774};
2775
Axel Linf0967ee2012-01-20 15:38:18 +08002776module_i2c_driver(lm93_driver);
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02002777
2778MODULE_AUTHOR("Mark M. Hoffman <mhoffman@lightlink.com>, "
Hans J. Koch2aa25c22010-11-15 21:38:56 +01002779 "Hans J. Koch <hjk@hansjkoch.de>");
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02002780MODULE_DESCRIPTION("LM93 driver");
2781MODULE_LICENSE("GPL");