blob: dc67bf954b2164573b920374ed497703a1a481b8 [file] [log] [blame]
Thomas Gleixner74ba9202019-05-20 09:19:02 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02002/*
Guenter Roeck2804a4cf2012-01-14 21:25:54 -08003 * lm93.c - Part of lm_sensors, Linux kernel modules for hardware monitoring
4 *
5 * Author/Maintainer: Mark M. Hoffman <mhoffman@lightlink.com>
6 * Copyright (c) 2004 Utilitek Systems, Inc.
7 *
8 * derived in part from lm78.c:
9 * Copyright (c) 1998, 1999 Frodo Looijaard <frodol@dds.nl>
10 *
11 * derived in part from lm85.c:
12 * Copyright (c) 2002, 2003 Philip Pokorny <ppokorny@penguincomputing.com>
13 * Copyright (c) 2003 Margit Schubert-While <margitsw@t-online.de>
14 *
15 * derived in part from w83l785ts.c:
Jean Delvare7c81c60f2014-01-29 20:40:08 +010016 * Copyright (c) 2003-2004 Jean Delvare <jdelvare@suse.de>
Guenter Roeck2804a4cf2012-01-14 21:25:54 -080017 *
18 * Ported to Linux 2.6 by Eric J. Bowersox <ericb@aspsys.com>
19 * Copyright (c) 2005 Aspen Systems, Inc.
20 *
21 * Adapted to 2.6.20 by Carsten Emde <cbe@osadl.org>
22 * Copyright (c) 2006 Carsten Emde, Open Source Automation Development Lab
23 *
24 * Modified for mainline integration by Hans J. Koch <hjk@hansjkoch.de>
25 * Copyright (c) 2007 Hans J. Koch, Linutronix GmbH
Guenter Roeck2804a4cf2012-01-14 21:25:54 -080026 */
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +020027
28#include <linux/module.h>
29#include <linux/init.h>
30#include <linux/slab.h>
31#include <linux/i2c.h>
32#include <linux/hwmon.h>
33#include <linux/hwmon-sysfs.h>
34#include <linux/hwmon-vid.h>
35#include <linux/err.h>
36#include <linux/delay.h>
Jean Delvaredcd8f392012-10-10 15:25:56 +020037#include <linux/jiffies.h>
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +020038
39/* LM93 REGISTER ADDRESSES */
40
41/* miscellaneous */
42#define LM93_REG_MFR_ID 0x3e
43#define LM93_REG_VER 0x3f
44#define LM93_REG_STATUS_CONTROL 0xe2
45#define LM93_REG_CONFIG 0xe3
46#define LM93_REG_SLEEP_CONTROL 0xe4
47
48/* alarm values start here */
49#define LM93_REG_HOST_ERROR_1 0x48
50
51/* voltage inputs: in1-in16 (nr => 0-15) */
52#define LM93_REG_IN(nr) (0x56 + (nr))
53#define LM93_REG_IN_MIN(nr) (0x90 + (nr) * 2)
54#define LM93_REG_IN_MAX(nr) (0x91 + (nr) * 2)
55
56/* temperature inputs: temp1-temp4 (nr => 0-3) */
57#define LM93_REG_TEMP(nr) (0x50 + (nr))
58#define LM93_REG_TEMP_MIN(nr) (0x78 + (nr) * 2)
59#define LM93_REG_TEMP_MAX(nr) (0x79 + (nr) * 2)
60
61/* temp[1-4]_auto_boost (nr => 0-3) */
62#define LM93_REG_BOOST(nr) (0x80 + (nr))
63
64/* #PROCHOT inputs: prochot1-prochot2 (nr => 0-1) */
65#define LM93_REG_PROCHOT_CUR(nr) (0x67 + (nr) * 2)
66#define LM93_REG_PROCHOT_AVG(nr) (0x68 + (nr) * 2)
67#define LM93_REG_PROCHOT_MAX(nr) (0xb0 + (nr))
68
69/* fan tach inputs: fan1-fan4 (nr => 0-3) */
70#define LM93_REG_FAN(nr) (0x6e + (nr) * 2)
71#define LM93_REG_FAN_MIN(nr) (0xb4 + (nr) * 2)
72
73/* pwm outputs: pwm1-pwm2 (nr => 0-1, reg => 0-3) */
Guenter Roeck2804a4cf2012-01-14 21:25:54 -080074#define LM93_REG_PWM_CTL(nr, reg) (0xc8 + (reg) + (nr) * 4)
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +020075#define LM93_PWM_CTL1 0x0
76#define LM93_PWM_CTL2 0x1
77#define LM93_PWM_CTL3 0x2
78#define LM93_PWM_CTL4 0x3
79
80/* GPIO input state */
81#define LM93_REG_GPI 0x6b
82
83/* vid inputs: vid1-vid2 (nr => 0-1) */
84#define LM93_REG_VID(nr) (0x6c + (nr))
85
86/* vccp1 & vccp2: VID relative inputs (nr => 0-1) */
87#define LM93_REG_VCCP_LIMIT_OFF(nr) (0xb2 + (nr))
88
89/* temp[1-4]_auto_boost_hyst */
90#define LM93_REG_BOOST_HYST_12 0xc0
91#define LM93_REG_BOOST_HYST_34 0xc1
92#define LM93_REG_BOOST_HYST(nr) (0xc0 + (nr)/2)
93
94/* temp[1-4]_auto_pwm_[min|hyst] */
95#define LM93_REG_PWM_MIN_HYST_12 0xc3
96#define LM93_REG_PWM_MIN_HYST_34 0xc4
97#define LM93_REG_PWM_MIN_HYST(nr) (0xc3 + (nr)/2)
98
99/* prochot_override & prochot_interval */
100#define LM93_REG_PROCHOT_OVERRIDE 0xc6
101#define LM93_REG_PROCHOT_INTERVAL 0xc7
102
103/* temp[1-4]_auto_base (nr => 0-3) */
104#define LM93_REG_TEMP_BASE(nr) (0xd0 + (nr))
105
106/* temp[1-4]_auto_offsets (step => 0-11) */
107#define LM93_REG_TEMP_OFFSET(step) (0xd4 + (step))
108
109/* #PROCHOT & #VRDHOT PWM ramp control */
110#define LM93_REG_PWM_RAMP_CTL 0xbf
111
112/* miscellaneous */
113#define LM93_REG_SFC1 0xbc
114#define LM93_REG_SFC2 0xbd
115#define LM93_REG_GPI_VID_CTL 0xbe
116#define LM93_REG_SF_TACH_TO_PWM 0xe0
117
118/* error masks */
119#define LM93_REG_GPI_ERR_MASK 0xec
120#define LM93_REG_MISC_ERR_MASK 0xed
121
122/* LM93 REGISTER VALUES */
123#define LM93_MFR_ID 0x73
124#define LM93_MFR_ID_PROTOTYPE 0x72
125
Guenter Roeckc7bf71c2011-01-17 12:48:20 -0800126/* LM94 REGISTER VALUES */
127#define LM94_MFR_ID_2 0x7a
128#define LM94_MFR_ID 0x79
129#define LM94_MFR_ID_PROTOTYPE 0x78
130
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +0200131/* SMBus capabilities */
132#define LM93_SMBUS_FUNC_FULL (I2C_FUNC_SMBUS_BYTE_DATA | \
133 I2C_FUNC_SMBUS_WORD_DATA | I2C_FUNC_SMBUS_BLOCK_DATA)
134#define LM93_SMBUS_FUNC_MIN (I2C_FUNC_SMBUS_BYTE_DATA | \
135 I2C_FUNC_SMBUS_WORD_DATA)
136
137/* Addresses to scan */
Mark M. Hoffman25e9c862008-02-17 22:28:03 -0500138static const unsigned short normal_i2c[] = { 0x2c, 0x2d, 0x2e, I2C_CLIENT_END };
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +0200139
140/* Insmod parameters */
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +0200141
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030142static bool disable_block;
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +0200143module_param(disable_block, bool, 0);
144MODULE_PARM_DESC(disable_block,
145 "Set to non-zero to disable SMBus block data transactions.");
146
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030147static bool init;
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +0200148module_param(init, bool, 0);
149MODULE_PARM_DESC(init, "Set to non-zero to force chip initialization.");
150
Guenter Roeck2804a4cf2012-01-14 21:25:54 -0800151static int vccp_limit_type[2] = {0, 0};
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +0200152module_param_array(vccp_limit_type, int, NULL, 0);
153MODULE_PARM_DESC(vccp_limit_type, "Configures in7 and in8 limit modes.");
154
155static int vid_agtl;
156module_param(vid_agtl, int, 0);
157MODULE_PARM_DESC(vid_agtl, "Configures VID pin input thresholds.");
158
159/* Driver data */
160static struct i2c_driver lm93_driver;
161
162/* LM93 BLOCK READ COMMANDS */
163static const struct { u8 cmd; u8 len; } lm93_block_read_cmds[12] = {
164 { 0xf2, 8 },
165 { 0xf3, 8 },
166 { 0xf4, 6 },
167 { 0xf5, 16 },
168 { 0xf6, 4 },
169 { 0xf7, 8 },
170 { 0xf8, 12 },
171 { 0xf9, 32 },
172 { 0xfa, 8 },
173 { 0xfb, 8 },
174 { 0xfc, 16 },
175 { 0xfd, 9 },
176};
177
Guenter Roeck2804a4cf2012-01-14 21:25:54 -0800178/*
179 * ALARMS: SYSCTL format described further below
180 * REG: 64 bits in 8 registers, as immediately below
181 */
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +0200182struct block1_t {
183 u8 host_status_1;
184 u8 host_status_2;
185 u8 host_status_3;
186 u8 host_status_4;
187 u8 p1_prochot_status;
188 u8 p2_prochot_status;
189 u8 gpi_status;
190 u8 fan_status;
191};
192
193/*
194 * Client-specific data
195 */
196struct lm93_data {
Axel Lin6579d582014-07-02 20:36:50 +0800197 struct i2c_client *client;
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +0200198
199 struct mutex update_lock;
200 unsigned long last_updated; /* In jiffies */
201
202 /* client update function */
203 void (*update)(struct lm93_data *, struct i2c_client *);
204
Paul Fertser952a11c2021-09-24 22:52:02 +0300205 bool valid; /* true if following fields are valid */
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +0200206
207 /* register values, arranged by block read groups */
208 struct block1_t block1;
209
Guenter Roeck2804a4cf2012-01-14 21:25:54 -0800210 /*
211 * temp1 - temp4: unfiltered readings
212 * temp1 - temp2: filtered readings
213 */
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +0200214 u8 block2[6];
215
216 /* vin1 - vin16: readings */
217 u8 block3[16];
218
219 /* prochot1 - prochot2: readings */
220 struct {
221 u8 cur;
222 u8 avg;
223 } block4[2];
224
225 /* fan counts 1-4 => 14-bits, LE, *left* justified */
226 u16 block5[4];
227
228 /* block6 has a lot of data we don't need */
229 struct {
230 u8 min;
231 u8 max;
Hans-Jürgen Kochf08a3482007-07-23 09:36:57 +0200232 } temp_lim[4];
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +0200233
234 /* vin1 - vin16: low and high limits */
235 struct {
236 u8 min;
237 u8 max;
238 } block7[16];
239
240 /* fan count limits 1-4 => same format as block5 */
241 u16 block8[4];
242
243 /* pwm control registers (2 pwms, 4 regs) */
244 u8 block9[2][4];
245
246 /* auto/pwm base temp and offset temp registers */
247 struct {
248 u8 base[4];
249 u8 offset[12];
250 } block10;
251
252 /* master config register */
253 u8 config;
254
255 /* VID1 & VID2 => register format, 6-bits, right justified */
256 u8 vid[2];
257
258 /* prochot1 - prochot2: limits */
259 u8 prochot_max[2];
260
261 /* vccp1 & vccp2 (in7 & in8): VID relative limits (register format) */
262 u8 vccp_limits[2];
263
264 /* GPIO input state (register format, i.e. inverted) */
265 u8 gpi;
266
267 /* #PROCHOT override (register format) */
268 u8 prochot_override;
269
270 /* #PROCHOT intervals (register format) */
271 u8 prochot_interval;
272
273 /* Fan Boost Temperatures (register format) */
274 u8 boost[4];
275
276 /* Fan Boost Hysteresis (register format) */
277 u8 boost_hyst[2];
278
279 /* Temperature Zone Min. PWM & Hysteresis (register format) */
280 u8 auto_pwm_min_hyst[2];
281
282 /* #PROCHOT & #VRDHOT PWM Ramp Control */
283 u8 pwm_ramp_ctl;
284
285 /* miscellaneous setup regs */
286 u8 sfc1;
287 u8 sfc2;
288 u8 sf_tach_to_pwm;
289
Guenter Roeck2804a4cf2012-01-14 21:25:54 -0800290 /*
291 * The two PWM CTL2 registers can read something other than what was
292 * last written for the OVR_DC field (duty cycle override). So, we
293 * save the user-commanded value here.
294 */
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +0200295 u8 pwm_override[2];
296};
297
Guenter Roeck2804a4cf2012-01-14 21:25:54 -0800298/*
299 * VID: mV
300 * REG: 6-bits, right justified, *always* using Intel VRM/VRD 10
301 */
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +0200302static int LM93_VID_FROM_REG(u8 reg)
303{
304 return vid_from_reg((reg & 0x3f), 100);
305}
306
307/* min, max, and nominal register values, per channel (u8) */
308static const u8 lm93_vin_reg_min[16] = {
309 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
310 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xae,
311};
312static const u8 lm93_vin_reg_max[16] = {
313 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
314 0xff, 0xfa, 0xff, 0xff, 0xff, 0xff, 0xff, 0xd1,
315};
Guenter Roeck2804a4cf2012-01-14 21:25:54 -0800316/*
317 * Values from the datasheet. They're here for documentation only.
318 * static const u8 lm93_vin_reg_nom[16] = {
319 * 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0,
320 * 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0x40, 0xc0,
321 * };
322 */
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +0200323
324/* min, max, and nominal voltage readings, per channel (mV)*/
325static const unsigned long lm93_vin_val_min[16] = {
326 0, 0, 0, 0, 0, 0, 0, 0,
327 0, 0, 0, 0, 0, 0, 0, 3000,
328};
329
330static const unsigned long lm93_vin_val_max[16] = {
331 1236, 1236, 1236, 1600, 2000, 2000, 1600, 1600,
332 4400, 6500, 3333, 2625, 1312, 1312, 1236, 3600,
333};
Guenter Roeck2804a4cf2012-01-14 21:25:54 -0800334/*
335 * Values from the datasheet. They're here for documentation only.
336 * static const unsigned long lm93_vin_val_nom[16] = {
337 * 927, 927, 927, 1200, 1500, 1500, 1200, 1200,
338 * 3300, 5000, 2500, 1969, 984, 984, 309, 3300,
339 * };
340 */
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +0200341
342static unsigned LM93_IN_FROM_REG(int nr, u8 reg)
343{
Guenter Roeck088ce2a2013-03-13 16:40:39 -0700344 const long uv_max = lm93_vin_val_max[nr] * 1000;
345 const long uv_min = lm93_vin_val_min[nr] * 1000;
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +0200346
Guenter Roeck088ce2a2013-03-13 16:40:39 -0700347 const long slope = (uv_max - uv_min) /
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +0200348 (lm93_vin_reg_max[nr] - lm93_vin_reg_min[nr]);
Guenter Roeck088ce2a2013-03-13 16:40:39 -0700349 const long intercept = uv_min - slope * lm93_vin_reg_min[nr];
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +0200350
351 return (slope * reg + intercept + 500) / 1000;
352}
353
Guenter Roeck2804a4cf2012-01-14 21:25:54 -0800354/*
355 * IN: mV, limits determined by channel nr
356 * REG: scaling determined by channel nr
357 */
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +0200358static u8 LM93_IN_TO_REG(int nr, unsigned val)
359{
360 /* range limit */
Guenter Roeck088ce2a2013-03-13 16:40:39 -0700361 const long mv = clamp_val(val,
Guenter Roeck2a844c12013-01-09 08:09:34 -0800362 lm93_vin_val_min[nr], lm93_vin_val_max[nr]);
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +0200363
364 /* try not to lose too much precision here */
Guenter Roeck088ce2a2013-03-13 16:40:39 -0700365 const long uv = mv * 1000;
366 const long uv_max = lm93_vin_val_max[nr] * 1000;
367 const long uv_min = lm93_vin_val_min[nr] * 1000;
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +0200368
369 /* convert */
Guenter Roeck088ce2a2013-03-13 16:40:39 -0700370 const long slope = (uv_max - uv_min) /
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +0200371 (lm93_vin_reg_max[nr] - lm93_vin_reg_min[nr]);
Guenter Roeck088ce2a2013-03-13 16:40:39 -0700372 const long intercept = uv_min - slope * lm93_vin_reg_min[nr];
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +0200373
Guenter Roeck088ce2a2013-03-13 16:40:39 -0700374 u8 result = ((uv - intercept + (slope/2)) / slope);
Guenter Roeck2a844c12013-01-09 08:09:34 -0800375 result = clamp_val(result,
376 lm93_vin_reg_min[nr], lm93_vin_reg_max[nr]);
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +0200377 return result;
378}
379
380/* vid in mV, upper == 0 indicates low limit, otherwise upper limit */
381static unsigned LM93_IN_REL_FROM_REG(u8 reg, int upper, int vid)
382{
Guenter Roeck088ce2a2013-03-13 16:40:39 -0700383 const long uv_offset = upper ? (((reg >> 4 & 0x0f) + 1) * 12500) :
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +0200384 (((reg >> 0 & 0x0f) + 1) * -25000);
Guenter Roeck088ce2a2013-03-13 16:40:39 -0700385 const long uv_vid = vid * 1000;
386 return (uv_vid + uv_offset + 5000) / 10000;
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +0200387}
388
Guenter Roeck2804a4cf2012-01-14 21:25:54 -0800389#define LM93_IN_MIN_FROM_REG(reg, vid) LM93_IN_REL_FROM_REG((reg), 0, (vid))
390#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 +0200391
Guenter Roeck2804a4cf2012-01-14 21:25:54 -0800392/*
393 * vid in mV , upper == 0 indicates low limit, otherwise upper limit
394 * upper also determines which nibble of the register is returned
395 * (the other nibble will be 0x0)
396 */
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +0200397static u8 LM93_IN_REL_TO_REG(unsigned val, int upper, int vid)
398{
Guenter Roeck088ce2a2013-03-13 16:40:39 -0700399 long uv_offset = vid * 1000 - val * 10000;
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +0200400 if (upper) {
Guenter Roeck088ce2a2013-03-13 16:40:39 -0700401 uv_offset = clamp_val(uv_offset, 12500, 200000);
402 return (u8)((uv_offset / 12500 - 1) << 4);
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +0200403 } else {
Guenter Roeck088ce2a2013-03-13 16:40:39 -0700404 uv_offset = clamp_val(uv_offset, -400000, -25000);
405 return (u8)((uv_offset / -25000 - 1) << 0);
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +0200406 }
407}
408
Guenter Roeck2804a4cf2012-01-14 21:25:54 -0800409/*
410 * TEMP: 1/1000 degrees C (-128C to +127C)
411 * REG: 1C/bit, two's complement
412 */
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +0200413static int LM93_TEMP_FROM_REG(u8 reg)
414{
415 return (s8)reg * 1000;
416}
417
418#define LM93_TEMP_MIN (-128000)
Guenter Roeck2804a4cf2012-01-14 21:25:54 -0800419#define LM93_TEMP_MAX (127000)
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +0200420
Guenter Roeck2804a4cf2012-01-14 21:25:54 -0800421/*
422 * TEMP: 1/1000 degrees C (-128C to +127C)
423 * REG: 1C/bit, two's complement
424 */
Christian Hohnstaedt5bfedac2007-08-16 11:40:10 +0200425static u8 LM93_TEMP_TO_REG(long temp)
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +0200426{
Guenter Roeck2a844c12013-01-09 08:09:34 -0800427 int ntemp = clamp_val(temp, LM93_TEMP_MIN, LM93_TEMP_MAX);
Guenter Roeck2804a4cf2012-01-14 21:25:54 -0800428 ntemp += (ntemp < 0 ? -500 : 500);
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +0200429 return (u8)(ntemp / 1000);
430}
431
432/* Determine 4-bit temperature offset resolution */
433static int LM93_TEMP_OFFSET_MODE_FROM_REG(u8 sfc2, int nr)
434{
435 /* mode: 0 => 1C/bit, nonzero => 0.5C/bit */
436 return sfc2 & (nr < 2 ? 0x10 : 0x20);
437}
438
Guenter Roeck2804a4cf2012-01-14 21:25:54 -0800439/*
440 * This function is common to all 4-bit temperature offsets
441 * reg is 4 bits right justified
442 * mode 0 => 1C/bit, mode !0 => 0.5C/bit
443 */
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +0200444static int LM93_TEMP_OFFSET_FROM_REG(u8 reg, int mode)
445{
446 return (reg & 0x0f) * (mode ? 5 : 10);
447}
448
Guenter Roeck2804a4cf2012-01-14 21:25:54 -0800449#define LM93_TEMP_OFFSET_MIN (0)
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +0200450#define LM93_TEMP_OFFSET_MAX0 (150)
Guenter Roeck2804a4cf2012-01-14 21:25:54 -0800451#define LM93_TEMP_OFFSET_MAX1 (75)
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +0200452
Guenter Roeck2804a4cf2012-01-14 21:25:54 -0800453/*
454 * This function is common to all 4-bit temperature offsets
455 * returns 4 bits right justified
456 * mode 0 => 1C/bit, mode !0 => 0.5C/bit
457 */
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +0200458static u8 LM93_TEMP_OFFSET_TO_REG(int off, int mode)
459{
460 int factor = mode ? 5 : 10;
461
Guenter Roeck2a844c12013-01-09 08:09:34 -0800462 off = clamp_val(off, LM93_TEMP_OFFSET_MIN,
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +0200463 mode ? LM93_TEMP_OFFSET_MAX1 : LM93_TEMP_OFFSET_MAX0);
464 return (u8)((off + factor/2) / factor);
465}
466
467/* 0 <= nr <= 3 */
468static int LM93_TEMP_AUTO_OFFSET_FROM_REG(u8 reg, int nr, int mode)
469{
470 /* temp1-temp2 (nr=0,1) use lower nibble */
471 if (nr < 2)
472 return LM93_TEMP_OFFSET_FROM_REG(reg & 0x0f, mode);
473
474 /* temp3-temp4 (nr=2,3) use upper nibble */
475 else
476 return LM93_TEMP_OFFSET_FROM_REG(reg >> 4 & 0x0f, mode);
477}
478
Guenter Roeck2804a4cf2012-01-14 21:25:54 -0800479/*
480 * TEMP: 1/10 degrees C (0C to +15C (mode 0) or +7.5C (mode non-zero))
481 * REG: 1.0C/bit (mode 0) or 0.5C/bit (mode non-zero)
482 * 0 <= nr <= 3
483 */
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +0200484static u8 LM93_TEMP_AUTO_OFFSET_TO_REG(u8 old, int off, int nr, int mode)
485{
486 u8 new = LM93_TEMP_OFFSET_TO_REG(off, mode);
487
488 /* temp1-temp2 (nr=0,1) use lower nibble */
489 if (nr < 2)
490 return (old & 0xf0) | (new & 0x0f);
491
492 /* temp3-temp4 (nr=2,3) use upper nibble */
493 else
494 return (new << 4 & 0xf0) | (old & 0x0f);
495}
496
497static int LM93_AUTO_BOOST_HYST_FROM_REGS(struct lm93_data *data, int nr,
498 int mode)
499{
500 u8 reg;
501
502 switch (nr) {
503 case 0:
504 reg = data->boost_hyst[0] & 0x0f;
505 break;
506 case 1:
507 reg = data->boost_hyst[0] >> 4 & 0x0f;
508 break;
509 case 2:
510 reg = data->boost_hyst[1] & 0x0f;
511 break;
512 case 3:
513 default:
514 reg = data->boost_hyst[1] >> 4 & 0x0f;
515 break;
516 }
517
518 return LM93_TEMP_FROM_REG(data->boost[nr]) -
519 LM93_TEMP_OFFSET_FROM_REG(reg, mode);
520}
521
522static u8 LM93_AUTO_BOOST_HYST_TO_REG(struct lm93_data *data, long hyst,
523 int nr, int mode)
524{
525 u8 reg = LM93_TEMP_OFFSET_TO_REG(
526 (LM93_TEMP_FROM_REG(data->boost[nr]) - hyst), mode);
527
528 switch (nr) {
529 case 0:
530 reg = (data->boost_hyst[0] & 0xf0) | (reg & 0x0f);
531 break;
532 case 1:
533 reg = (reg << 4 & 0xf0) | (data->boost_hyst[0] & 0x0f);
534 break;
535 case 2:
536 reg = (data->boost_hyst[1] & 0xf0) | (reg & 0x0f);
537 break;
538 case 3:
539 default:
540 reg = (reg << 4 & 0xf0) | (data->boost_hyst[1] & 0x0f);
541 break;
542 }
543
544 return reg;
545}
546
Guenter Roeck2804a4cf2012-01-14 21:25:54 -0800547/*
548 * PWM: 0-255 per sensors documentation
549 * REG: 0-13 as mapped below... right justified
550 */
551enum pwm_freq { LM93_PWM_MAP_HI_FREQ, LM93_PWM_MAP_LO_FREQ };
552
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +0200553static int lm93_pwm_map[2][16] = {
554 {
555 0x00, /* 0.00% */ 0x40, /* 25.00% */
556 0x50, /* 31.25% */ 0x60, /* 37.50% */
557 0x70, /* 43.75% */ 0x80, /* 50.00% */
558 0x90, /* 56.25% */ 0xa0, /* 62.50% */
559 0xb0, /* 68.75% */ 0xc0, /* 75.00% */
560 0xd0, /* 81.25% */ 0xe0, /* 87.50% */
561 0xf0, /* 93.75% */ 0xff, /* 100.00% */
562 0xff, 0xff, /* 14, 15 are reserved and should never occur */
563 },
564 {
565 0x00, /* 0.00% */ 0x40, /* 25.00% */
566 0x49, /* 28.57% */ 0x52, /* 32.14% */
567 0x5b, /* 35.71% */ 0x64, /* 39.29% */
568 0x6d, /* 42.86% */ 0x76, /* 46.43% */
569 0x80, /* 50.00% */ 0x89, /* 53.57% */
570 0x92, /* 57.14% */ 0xb6, /* 71.43% */
571 0xdb, /* 85.71% */ 0xff, /* 100.00% */
572 0xff, 0xff, /* 14, 15 are reserved and should never occur */
573 },
574};
575
Guenter Roeck2804a4cf2012-01-14 21:25:54 -0800576static int LM93_PWM_FROM_REG(u8 reg, enum pwm_freq freq)
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +0200577{
578 return lm93_pwm_map[freq][reg & 0x0f];
579}
580
581/* round up to nearest match */
Guenter Roeck2804a4cf2012-01-14 21:25:54 -0800582static u8 LM93_PWM_TO_REG(int pwm, enum pwm_freq freq)
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +0200583{
584 int i;
585 for (i = 0; i < 13; i++)
586 if (pwm <= lm93_pwm_map[freq][i])
587 break;
588
589 /* can fall through with i==13 */
590 return (u8)i;
591}
592
593static int LM93_FAN_FROM_REG(u16 regs)
594{
595 const u16 count = le16_to_cpu(regs) >> 2;
Guenter Roeck2804a4cf2012-01-14 21:25:54 -0800596 return count == 0 ? -1 : count == 0x3fff ? 0 : 1350000 / count;
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +0200597}
598
599/*
600 * RPM: (82.5 to 1350000)
601 * REG: 14-bits, LE, *left* justified
602 */
603static u16 LM93_FAN_TO_REG(long rpm)
604{
605 u16 count, regs;
606
607 if (rpm == 0) {
608 count = 0x3fff;
609 } else {
Guenter Roeck2a844c12013-01-09 08:09:34 -0800610 rpm = clamp_val(rpm, 1, 1000000);
611 count = clamp_val((1350000 + rpm) / rpm, 1, 0x3ffe);
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +0200612 }
613
614 regs = count << 2;
615 return cpu_to_le16(regs);
616}
617
Guenter Roeck2804a4cf2012-01-14 21:25:54 -0800618/*
619 * PWM FREQ: HZ
620 * REG: 0-7 as mapped below
621 */
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +0200622static int lm93_pwm_freq_map[8] = {
623 22500, 96, 84, 72, 60, 48, 36, 12
624};
625
626static int LM93_PWM_FREQ_FROM_REG(u8 reg)
627{
628 return lm93_pwm_freq_map[reg & 0x07];
629}
630
631/* round up to nearest match */
632static u8 LM93_PWM_FREQ_TO_REG(int freq)
633{
634 int i;
635 for (i = 7; i > 0; i--)
636 if (freq <= lm93_pwm_freq_map[i])
637 break;
638
639 /* can fall through with i==0 */
640 return (u8)i;
641}
642
Guenter Roeck2804a4cf2012-01-14 21:25:54 -0800643/*
644 * TIME: 1/100 seconds
645 * REG: 0-7 as mapped below
646 */
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +0200647static int lm93_spinup_time_map[8] = {
648 0, 10, 25, 40, 70, 100, 200, 400,
649};
650
651static int LM93_SPINUP_TIME_FROM_REG(u8 reg)
652{
653 return lm93_spinup_time_map[reg >> 5 & 0x07];
654}
655
656/* round up to nearest match */
657static u8 LM93_SPINUP_TIME_TO_REG(int time)
658{
659 int i;
660 for (i = 0; i < 7; i++)
661 if (time <= lm93_spinup_time_map[i])
662 break;
663
664 /* can fall through with i==8 */
665 return (u8)i;
666}
667
668#define LM93_RAMP_MIN 0
669#define LM93_RAMP_MAX 75
670
671static int LM93_RAMP_FROM_REG(u8 reg)
672{
673 return (reg & 0x0f) * 5;
674}
675
Guenter Roeck2804a4cf2012-01-14 21:25:54 -0800676/*
677 * RAMP: 1/100 seconds
678 * REG: 50mS/bit 4-bits right justified
679 */
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +0200680static u8 LM93_RAMP_TO_REG(int ramp)
681{
Guenter Roeck2a844c12013-01-09 08:09:34 -0800682 ramp = clamp_val(ramp, LM93_RAMP_MIN, LM93_RAMP_MAX);
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +0200683 return (u8)((ramp + 2) / 5);
684}
685
Guenter Roeck2804a4cf2012-01-14 21:25:54 -0800686/*
687 * PROCHOT: 0-255, 0 => 0%, 255 => > 96.6%
688 * REG: (same)
689 */
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +0200690static u8 LM93_PROCHOT_TO_REG(long prochot)
691{
Guenter Roeck2a844c12013-01-09 08:09:34 -0800692 prochot = clamp_val(prochot, 0, 255);
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +0200693 return (u8)prochot;
694}
695
Guenter Roeck2804a4cf2012-01-14 21:25:54 -0800696/*
697 * PROCHOT-INTERVAL: 73 - 37200 (1/100 seconds)
698 * REG: 0-9 as mapped below
699 */
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +0200700static int lm93_interval_map[10] = {
701 73, 146, 290, 580, 1170, 2330, 4660, 9320, 18600, 37200,
702};
703
704static int LM93_INTERVAL_FROM_REG(u8 reg)
705{
706 return lm93_interval_map[reg & 0x0f];
707}
708
709/* round up to nearest match */
710static u8 LM93_INTERVAL_TO_REG(long interval)
711{
712 int i;
713 for (i = 0; i < 9; i++)
714 if (interval <= lm93_interval_map[i])
715 break;
716
717 /* can fall through with i==9 */
718 return (u8)i;
719}
720
Guenter Roeck2804a4cf2012-01-14 21:25:54 -0800721/*
722 * GPIO: 0-255, GPIO0 is LSB
723 * REG: inverted
724 */
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +0200725static unsigned LM93_GPI_FROM_REG(u8 reg)
726{
727 return ~reg & 0xff;
728}
729
Guenter Roeck2804a4cf2012-01-14 21:25:54 -0800730/*
731 * alarm bitmask definitions
732 * The LM93 has nearly 64 bits of error status... I've pared that down to
733 * what I think is a useful subset in order to fit it into 32 bits.
734 *
735 * Especially note that the #VRD_HOT alarms are missing because we provide
736 * that information as values in another sysfs file.
737 *
738 * If libsensors is extended to support 64 bit values, this could be revisited.
739 */
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +0200740#define LM93_ALARM_IN1 0x00000001
741#define LM93_ALARM_IN2 0x00000002
742#define LM93_ALARM_IN3 0x00000004
743#define LM93_ALARM_IN4 0x00000008
744#define LM93_ALARM_IN5 0x00000010
745#define LM93_ALARM_IN6 0x00000020
746#define LM93_ALARM_IN7 0x00000040
747#define LM93_ALARM_IN8 0x00000080
748#define LM93_ALARM_IN9 0x00000100
749#define LM93_ALARM_IN10 0x00000200
750#define LM93_ALARM_IN11 0x00000400
751#define LM93_ALARM_IN12 0x00000800
752#define LM93_ALARM_IN13 0x00001000
753#define LM93_ALARM_IN14 0x00002000
754#define LM93_ALARM_IN15 0x00004000
755#define LM93_ALARM_IN16 0x00008000
756#define LM93_ALARM_FAN1 0x00010000
757#define LM93_ALARM_FAN2 0x00020000
758#define LM93_ALARM_FAN3 0x00040000
759#define LM93_ALARM_FAN4 0x00080000
760#define LM93_ALARM_PH1_ERR 0x00100000
761#define LM93_ALARM_PH2_ERR 0x00200000
762#define LM93_ALARM_SCSI1_ERR 0x00400000
763#define LM93_ALARM_SCSI2_ERR 0x00800000
764#define LM93_ALARM_DVDDP1_ERR 0x01000000
765#define LM93_ALARM_DVDDP2_ERR 0x02000000
766#define LM93_ALARM_D1_ERR 0x04000000
767#define LM93_ALARM_D2_ERR 0x08000000
768#define LM93_ALARM_TEMP1 0x10000000
769#define LM93_ALARM_TEMP2 0x20000000
770#define LM93_ALARM_TEMP3 0x40000000
771
772static unsigned LM93_ALARMS_FROM_REG(struct block1_t b1)
773{
774 unsigned result;
775 result = b1.host_status_2 & 0x3f;
776
777 if (vccp_limit_type[0])
778 result |= (b1.host_status_4 & 0x10) << 2;
779 else
780 result |= b1.host_status_2 & 0x40;
781
782 if (vccp_limit_type[1])
783 result |= (b1.host_status_4 & 0x20) << 2;
784 else
785 result |= b1.host_status_2 & 0x80;
786
787 result |= b1.host_status_3 << 8;
788 result |= (b1.fan_status & 0x0f) << 16;
789 result |= (b1.p1_prochot_status & 0x80) << 13;
790 result |= (b1.p2_prochot_status & 0x80) << 14;
791 result |= (b1.host_status_4 & 0xfc) << 20;
792 result |= (b1.host_status_1 & 0x07) << 28;
793 return result;
794}
795
796#define MAX_RETRIES 5
797
798static u8 lm93_read_byte(struct i2c_client *client, u8 reg)
799{
800 int value, i;
801
802 /* retry in case of read errors */
Guenter Roeck2804a4cf2012-01-14 21:25:54 -0800803 for (i = 1; i <= MAX_RETRIES; i++) {
804 value = i2c_smbus_read_byte_data(client, reg);
805 if (value >= 0) {
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +0200806 return value;
807 } else {
Guenter Roeckb55f3752013-01-10 10:01:24 -0800808 dev_warn(&client->dev,
809 "lm93: read byte data failed, address 0x%02x.\n",
810 reg);
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +0200811 mdelay(i + 3);
812 }
813
814 }
815
816 /* <TODO> what to return in case of error? */
Guenter Roeck2804a4cf2012-01-14 21:25:54 -0800817 dev_err(&client->dev, "lm93: All read byte retries failed!!\n");
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +0200818 return 0;
819}
820
821static int lm93_write_byte(struct i2c_client *client, u8 reg, u8 value)
822{
823 int result;
824
825 /* <TODO> how to handle write errors? */
826 result = i2c_smbus_write_byte_data(client, reg, value);
827
828 if (result < 0)
Guenter Roeckb55f3752013-01-10 10:01:24 -0800829 dev_warn(&client->dev,
830 "lm93: write byte data failed, 0x%02x at address 0x%02x.\n",
831 value, reg);
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +0200832
833 return result;
834}
835
836static u16 lm93_read_word(struct i2c_client *client, u8 reg)
837{
838 int value, i;
839
840 /* retry in case of read errors */
Guenter Roeck2804a4cf2012-01-14 21:25:54 -0800841 for (i = 1; i <= MAX_RETRIES; i++) {
842 value = i2c_smbus_read_word_data(client, reg);
843 if (value >= 0) {
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +0200844 return value;
845 } else {
Guenter Roeckb55f3752013-01-10 10:01:24 -0800846 dev_warn(&client->dev,
847 "lm93: read word data failed, address 0x%02x.\n",
848 reg);
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +0200849 mdelay(i + 3);
850 }
851
852 }
853
854 /* <TODO> what to return in case of error? */
Guenter Roeck2804a4cf2012-01-14 21:25:54 -0800855 dev_err(&client->dev, "lm93: All read word retries failed!!\n");
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +0200856 return 0;
857}
858
859static int lm93_write_word(struct i2c_client *client, u8 reg, u16 value)
860{
861 int result;
862
863 /* <TODO> how to handle write errors? */
864 result = i2c_smbus_write_word_data(client, reg, value);
865
866 if (result < 0)
Guenter Roeckb55f3752013-01-10 10:01:24 -0800867 dev_warn(&client->dev,
868 "lm93: write word data failed, 0x%04x at address 0x%02x.\n",
869 value, reg);
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +0200870
871 return result;
872}
873
874static u8 lm93_block_buffer[I2C_SMBUS_BLOCK_MAX];
875
876/*
Guenter Roeck2804a4cf2012-01-14 21:25:54 -0800877 * read block data into values, retry if not expected length
878 * fbn => index to lm93_block_read_cmds table
879 * (Fixed Block Number - section 14.5.2 of LM93 datasheet)
880 */
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +0200881static void lm93_read_block(struct i2c_client *client, u8 fbn, u8 *values)
882{
Guenter Roeck2804a4cf2012-01-14 21:25:54 -0800883 int i, result = 0;
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +0200884
885 for (i = 1; i <= MAX_RETRIES; i++) {
886 result = i2c_smbus_read_block_data(client,
887 lm93_block_read_cmds[fbn].cmd, lm93_block_buffer);
888
889 if (result == lm93_block_read_cmds[fbn].len) {
890 break;
891 } else {
Guenter Roeckb55f3752013-01-10 10:01:24 -0800892 dev_warn(&client->dev,
893 "lm93: block read data failed, command 0x%02x.\n",
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +0200894 lm93_block_read_cmds[fbn].cmd);
895 mdelay(i + 3);
896 }
897 }
898
899 if (result == lm93_block_read_cmds[fbn].len) {
Guenter Roeck2804a4cf2012-01-14 21:25:54 -0800900 memcpy(values, lm93_block_buffer,
901 lm93_block_read_cmds[fbn].len);
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +0200902 } else {
903 /* <TODO> what to do in case of error? */
904 }
905}
906
907static struct lm93_data *lm93_update_device(struct device *dev)
908{
Axel Lin6579d582014-07-02 20:36:50 +0800909 struct lm93_data *data = dev_get_drvdata(dev);
910 struct i2c_client *client = data->client;
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +0200911 const unsigned long interval = HZ + (HZ / 2);
912
913 mutex_lock(&data->update_lock);
914
915 if (time_after(jiffies, data->last_updated + interval) ||
916 !data->valid) {
917
918 data->update(data, client);
919 data->last_updated = jiffies;
Paul Fertser952a11c2021-09-24 22:52:02 +0300920 data->valid = true;
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +0200921 }
922
923 mutex_unlock(&data->update_lock);
924 return data;
925}
926
927/* update routine for data that has no corresponding SMBus block command */
928static void lm93_update_client_common(struct lm93_data *data,
929 struct i2c_client *client)
930{
931 int i;
932 u8 *ptr;
933
934 /* temp1 - temp4: limits */
935 for (i = 0; i < 4; i++) {
936 data->temp_lim[i].min =
937 lm93_read_byte(client, LM93_REG_TEMP_MIN(i));
938 data->temp_lim[i].max =
939 lm93_read_byte(client, LM93_REG_TEMP_MAX(i));
940 }
941
942 /* config register */
943 data->config = lm93_read_byte(client, LM93_REG_CONFIG);
944
945 /* vid1 - vid2: values */
946 for (i = 0; i < 2; i++)
947 data->vid[i] = lm93_read_byte(client, LM93_REG_VID(i));
948
949 /* prochot1 - prochot2: limits */
950 for (i = 0; i < 2; i++)
951 data->prochot_max[i] = lm93_read_byte(client,
952 LM93_REG_PROCHOT_MAX(i));
953
954 /* vccp1 - vccp2: VID relative limits */
955 for (i = 0; i < 2; i++)
956 data->vccp_limits[i] = lm93_read_byte(client,
957 LM93_REG_VCCP_LIMIT_OFF(i));
958
959 /* GPIO input state */
960 data->gpi = lm93_read_byte(client, LM93_REG_GPI);
961
962 /* #PROCHOT override state */
963 data->prochot_override = lm93_read_byte(client,
964 LM93_REG_PROCHOT_OVERRIDE);
965
966 /* #PROCHOT intervals */
967 data->prochot_interval = lm93_read_byte(client,
968 LM93_REG_PROCHOT_INTERVAL);
969
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200970 /* Fan Boost Temperature registers */
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +0200971 for (i = 0; i < 4; i++)
972 data->boost[i] = lm93_read_byte(client, LM93_REG_BOOST(i));
973
974 /* Fan Boost Temperature Hyst. registers */
975 data->boost_hyst[0] = lm93_read_byte(client, LM93_REG_BOOST_HYST_12);
976 data->boost_hyst[1] = lm93_read_byte(client, LM93_REG_BOOST_HYST_34);
977
978 /* Temperature Zone Min. PWM & Hysteresis registers */
979 data->auto_pwm_min_hyst[0] =
980 lm93_read_byte(client, LM93_REG_PWM_MIN_HYST_12);
981 data->auto_pwm_min_hyst[1] =
982 lm93_read_byte(client, LM93_REG_PWM_MIN_HYST_34);
983
984 /* #PROCHOT & #VRDHOT PWM Ramp Control register */
985 data->pwm_ramp_ctl = lm93_read_byte(client, LM93_REG_PWM_RAMP_CTL);
986
987 /* misc setup registers */
988 data->sfc1 = lm93_read_byte(client, LM93_REG_SFC1);
989 data->sfc2 = lm93_read_byte(client, LM93_REG_SFC2);
990 data->sf_tach_to_pwm = lm93_read_byte(client,
991 LM93_REG_SF_TACH_TO_PWM);
992
993 /* write back alarm values to clear */
994 for (i = 0, ptr = (u8 *)(&data->block1); i < 8; i++)
995 lm93_write_byte(client, LM93_REG_HOST_ERROR_1 + i, *(ptr + i));
996}
997
998/* update routine which uses SMBus block data commands */
999static void lm93_update_client_full(struct lm93_data *data,
1000 struct i2c_client *client)
1001{
Guenter Roeck2804a4cf2012-01-14 21:25:54 -08001002 dev_dbg(&client->dev, "starting device update (block data enabled)\n");
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02001003
1004 /* in1 - in16: values & limits */
1005 lm93_read_block(client, 3, (u8 *)(data->block3));
1006 lm93_read_block(client, 7, (u8 *)(data->block7));
1007
1008 /* temp1 - temp4: values */
1009 lm93_read_block(client, 2, (u8 *)(data->block2));
1010
1011 /* prochot1 - prochot2: values */
1012 lm93_read_block(client, 4, (u8 *)(data->block4));
1013
1014 /* fan1 - fan4: values & limits */
1015 lm93_read_block(client, 5, (u8 *)(data->block5));
1016 lm93_read_block(client, 8, (u8 *)(data->block8));
1017
1018 /* pmw control registers */
1019 lm93_read_block(client, 9, (u8 *)(data->block9));
1020
1021 /* alarm values */
1022 lm93_read_block(client, 1, (u8 *)(&data->block1));
1023
1024 /* auto/pwm registers */
1025 lm93_read_block(client, 10, (u8 *)(&data->block10));
1026
1027 lm93_update_client_common(data, client);
1028}
1029
1030/* update routine which uses SMBus byte/word data commands only */
1031static void lm93_update_client_min(struct lm93_data *data,
1032 struct i2c_client *client)
1033{
Guenter Roeck2804a4cf2012-01-14 21:25:54 -08001034 int i, j;
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02001035 u8 *ptr;
1036
Guenter Roeck2804a4cf2012-01-14 21:25:54 -08001037 dev_dbg(&client->dev, "starting device update (block data disabled)\n");
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02001038
1039 /* in1 - in16: values & limits */
1040 for (i = 0; i < 16; i++) {
1041 data->block3[i] =
1042 lm93_read_byte(client, LM93_REG_IN(i));
1043 data->block7[i].min =
1044 lm93_read_byte(client, LM93_REG_IN_MIN(i));
1045 data->block7[i].max =
1046 lm93_read_byte(client, LM93_REG_IN_MAX(i));
1047 }
1048
1049 /* temp1 - temp4: values */
1050 for (i = 0; i < 4; i++) {
1051 data->block2[i] =
1052 lm93_read_byte(client, LM93_REG_TEMP(i));
1053 }
1054
1055 /* prochot1 - prochot2: values */
1056 for (i = 0; i < 2; i++) {
1057 data->block4[i].cur =
1058 lm93_read_byte(client, LM93_REG_PROCHOT_CUR(i));
1059 data->block4[i].avg =
1060 lm93_read_byte(client, LM93_REG_PROCHOT_AVG(i));
1061 }
1062
1063 /* fan1 - fan4: values & limits */
1064 for (i = 0; i < 4; i++) {
1065 data->block5[i] =
1066 lm93_read_word(client, LM93_REG_FAN(i));
1067 data->block8[i] =
1068 lm93_read_word(client, LM93_REG_FAN_MIN(i));
1069 }
1070
1071 /* pwm control registers */
1072 for (i = 0; i < 2; i++) {
1073 for (j = 0; j < 4; j++) {
1074 data->block9[i][j] =
Guenter Roeck2804a4cf2012-01-14 21:25:54 -08001075 lm93_read_byte(client, LM93_REG_PWM_CTL(i, j));
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02001076 }
1077 }
1078
1079 /* alarm values */
1080 for (i = 0, ptr = (u8 *)(&data->block1); i < 8; i++) {
1081 *(ptr + i) =
1082 lm93_read_byte(client, LM93_REG_HOST_ERROR_1 + i);
1083 }
1084
1085 /* auto/pwm (base temp) registers */
1086 for (i = 0; i < 4; i++) {
1087 data->block10.base[i] =
1088 lm93_read_byte(client, LM93_REG_TEMP_BASE(i));
1089 }
1090
1091 /* auto/pwm (offset temp) registers */
1092 for (i = 0; i < 12; i++) {
1093 data->block10.offset[i] =
1094 lm93_read_byte(client, LM93_REG_TEMP_OFFSET(i));
1095 }
1096
1097 lm93_update_client_common(data, client);
1098}
1099
1100/* following are the sysfs callback functions */
Guenter Roeck7f9d0582018-12-10 14:02:13 -08001101static ssize_t in_show(struct device *dev, struct device_attribute *attr,
1102 char *buf)
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02001103{
1104 int nr = (to_sensor_dev_attr(attr))->index;
1105
1106 struct lm93_data *data = lm93_update_device(dev);
1107 return sprintf(buf, "%d\n", LM93_IN_FROM_REG(nr, data->block3[nr]));
1108}
1109
Guenter Roeck7f9d0582018-12-10 14:02:13 -08001110static SENSOR_DEVICE_ATTR_RO(in1_input, in, 0);
1111static SENSOR_DEVICE_ATTR_RO(in2_input, in, 1);
1112static SENSOR_DEVICE_ATTR_RO(in3_input, in, 2);
1113static SENSOR_DEVICE_ATTR_RO(in4_input, in, 3);
1114static SENSOR_DEVICE_ATTR_RO(in5_input, in, 4);
1115static SENSOR_DEVICE_ATTR_RO(in6_input, in, 5);
1116static SENSOR_DEVICE_ATTR_RO(in7_input, in, 6);
1117static SENSOR_DEVICE_ATTR_RO(in8_input, in, 7);
1118static SENSOR_DEVICE_ATTR_RO(in9_input, in, 8);
1119static SENSOR_DEVICE_ATTR_RO(in10_input, in, 9);
1120static SENSOR_DEVICE_ATTR_RO(in11_input, in, 10);
1121static SENSOR_DEVICE_ATTR_RO(in12_input, in, 11);
1122static SENSOR_DEVICE_ATTR_RO(in13_input, in, 12);
1123static SENSOR_DEVICE_ATTR_RO(in14_input, in, 13);
1124static SENSOR_DEVICE_ATTR_RO(in15_input, in, 14);
1125static SENSOR_DEVICE_ATTR_RO(in16_input, in, 15);
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02001126
Guenter Roeck7f9d0582018-12-10 14:02:13 -08001127static ssize_t in_min_show(struct device *dev, struct device_attribute *attr,
1128 char *buf)
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02001129{
1130 int nr = (to_sensor_dev_attr(attr))->index;
1131 struct lm93_data *data = lm93_update_device(dev);
1132 int vccp = nr - 6;
1133 long rc, vid;
1134
Guenter Roeck2804a4cf2012-01-14 21:25:54 -08001135 if ((nr == 6 || nr == 7) && vccp_limit_type[vccp]) {
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02001136 vid = LM93_VID_FROM_REG(data->vid[vccp]);
1137 rc = LM93_IN_MIN_FROM_REG(data->vccp_limits[vccp], vid);
Guenter Roeck2804a4cf2012-01-14 21:25:54 -08001138 } else {
1139 rc = LM93_IN_FROM_REG(nr, data->block7[nr].min);
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02001140 }
Guenter Roeck2804a4cf2012-01-14 21:25:54 -08001141 return sprintf(buf, "%ld\n", rc);
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02001142}
1143
Guenter Roeck7f9d0582018-12-10 14:02:13 -08001144static ssize_t in_min_store(struct device *dev, struct device_attribute *attr,
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02001145 const char *buf, size_t count)
1146{
1147 int nr = (to_sensor_dev_attr(attr))->index;
Axel Lin6579d582014-07-02 20:36:50 +08001148 struct lm93_data *data = dev_get_drvdata(dev);
1149 struct i2c_client *client = data->client;
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02001150 int vccp = nr - 6;
1151 long vid;
Guenter Roeck2804a4cf2012-01-14 21:25:54 -08001152 unsigned long val;
1153 int err;
1154
1155 err = kstrtoul(buf, 10, &val);
1156 if (err)
1157 return err;
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02001158
1159 mutex_lock(&data->update_lock);
Guenter Roeck2804a4cf2012-01-14 21:25:54 -08001160 if ((nr == 6 || nr == 7) && vccp_limit_type[vccp]) {
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02001161 vid = LM93_VID_FROM_REG(data->vid[vccp]);
1162 data->vccp_limits[vccp] = (data->vccp_limits[vccp] & 0xf0) |
1163 LM93_IN_REL_TO_REG(val, 0, vid);
1164 lm93_write_byte(client, LM93_REG_VCCP_LIMIT_OFF(vccp),
1165 data->vccp_limits[vccp]);
Guenter Roeck2804a4cf2012-01-14 21:25:54 -08001166 } else {
1167 data->block7[nr].min = LM93_IN_TO_REG(nr, val);
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02001168 lm93_write_byte(client, LM93_REG_IN_MIN(nr),
1169 data->block7[nr].min);
1170 }
1171 mutex_unlock(&data->update_lock);
1172 return count;
1173}
1174
Guenter Roeck7f9d0582018-12-10 14:02:13 -08001175static SENSOR_DEVICE_ATTR_RW(in1_min, in_min, 0);
1176static SENSOR_DEVICE_ATTR_RW(in2_min, in_min, 1);
1177static SENSOR_DEVICE_ATTR_RW(in3_min, in_min, 2);
1178static SENSOR_DEVICE_ATTR_RW(in4_min, in_min, 3);
1179static SENSOR_DEVICE_ATTR_RW(in5_min, in_min, 4);
1180static SENSOR_DEVICE_ATTR_RW(in6_min, in_min, 5);
1181static SENSOR_DEVICE_ATTR_RW(in7_min, in_min, 6);
1182static SENSOR_DEVICE_ATTR_RW(in8_min, in_min, 7);
1183static SENSOR_DEVICE_ATTR_RW(in9_min, in_min, 8);
1184static SENSOR_DEVICE_ATTR_RW(in10_min, in_min, 9);
1185static SENSOR_DEVICE_ATTR_RW(in11_min, in_min, 10);
1186static SENSOR_DEVICE_ATTR_RW(in12_min, in_min, 11);
1187static SENSOR_DEVICE_ATTR_RW(in13_min, in_min, 12);
1188static SENSOR_DEVICE_ATTR_RW(in14_min, in_min, 13);
1189static SENSOR_DEVICE_ATTR_RW(in15_min, in_min, 14);
1190static SENSOR_DEVICE_ATTR_RW(in16_min, in_min, 15);
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02001191
Guenter Roeck7f9d0582018-12-10 14:02:13 -08001192static ssize_t in_max_show(struct device *dev, struct device_attribute *attr,
1193 char *buf)
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02001194{
1195 int nr = (to_sensor_dev_attr(attr))->index;
1196 struct lm93_data *data = lm93_update_device(dev);
1197 int vccp = nr - 6;
1198 long rc, vid;
1199
Guenter Roeck2804a4cf2012-01-14 21:25:54 -08001200 if ((nr == 6 || nr == 7) && vccp_limit_type[vccp]) {
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02001201 vid = LM93_VID_FROM_REG(data->vid[vccp]);
Guenter Roeck2804a4cf2012-01-14 21:25:54 -08001202 rc = LM93_IN_MAX_FROM_REG(data->vccp_limits[vccp], vid);
1203 } else {
1204 rc = LM93_IN_FROM_REG(nr, data->block7[nr].max);
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02001205 }
Guenter Roeck2804a4cf2012-01-14 21:25:54 -08001206 return sprintf(buf, "%ld\n", rc);
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02001207}
1208
Guenter Roeck7f9d0582018-12-10 14:02:13 -08001209static ssize_t in_max_store(struct device *dev, struct device_attribute *attr,
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02001210 const char *buf, size_t count)
1211{
1212 int nr = (to_sensor_dev_attr(attr))->index;
Axel Lin6579d582014-07-02 20:36:50 +08001213 struct lm93_data *data = dev_get_drvdata(dev);
1214 struct i2c_client *client = data->client;
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02001215 int vccp = nr - 6;
1216 long vid;
Guenter Roeck2804a4cf2012-01-14 21:25:54 -08001217 unsigned long val;
1218 int err;
1219
1220 err = kstrtoul(buf, 10, &val);
1221 if (err)
1222 return err;
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02001223
1224 mutex_lock(&data->update_lock);
Guenter Roeck2804a4cf2012-01-14 21:25:54 -08001225 if ((nr == 6 || nr == 7) && vccp_limit_type[vccp]) {
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02001226 vid = LM93_VID_FROM_REG(data->vid[vccp]);
1227 data->vccp_limits[vccp] = (data->vccp_limits[vccp] & 0x0f) |
1228 LM93_IN_REL_TO_REG(val, 1, vid);
1229 lm93_write_byte(client, LM93_REG_VCCP_LIMIT_OFF(vccp),
1230 data->vccp_limits[vccp]);
Guenter Roeck2804a4cf2012-01-14 21:25:54 -08001231 } else {
1232 data->block7[nr].max = LM93_IN_TO_REG(nr, val);
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02001233 lm93_write_byte(client, LM93_REG_IN_MAX(nr),
1234 data->block7[nr].max);
1235 }
1236 mutex_unlock(&data->update_lock);
1237 return count;
1238}
1239
Guenter Roeck7f9d0582018-12-10 14:02:13 -08001240static SENSOR_DEVICE_ATTR_RW(in1_max, in_max, 0);
1241static SENSOR_DEVICE_ATTR_RW(in2_max, in_max, 1);
1242static SENSOR_DEVICE_ATTR_RW(in3_max, in_max, 2);
1243static SENSOR_DEVICE_ATTR_RW(in4_max, in_max, 3);
1244static SENSOR_DEVICE_ATTR_RW(in5_max, in_max, 4);
1245static SENSOR_DEVICE_ATTR_RW(in6_max, in_max, 5);
1246static SENSOR_DEVICE_ATTR_RW(in7_max, in_max, 6);
1247static SENSOR_DEVICE_ATTR_RW(in8_max, in_max, 7);
1248static SENSOR_DEVICE_ATTR_RW(in9_max, in_max, 8);
1249static SENSOR_DEVICE_ATTR_RW(in10_max, in_max, 9);
1250static SENSOR_DEVICE_ATTR_RW(in11_max, in_max, 10);
1251static SENSOR_DEVICE_ATTR_RW(in12_max, in_max, 11);
1252static SENSOR_DEVICE_ATTR_RW(in13_max, in_max, 12);
1253static SENSOR_DEVICE_ATTR_RW(in14_max, in_max, 13);
1254static SENSOR_DEVICE_ATTR_RW(in15_max, in_max, 14);
1255static SENSOR_DEVICE_ATTR_RW(in16_max, in_max, 15);
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02001256
Guenter Roeck7f9d0582018-12-10 14:02:13 -08001257static ssize_t temp_show(struct device *dev, struct device_attribute *attr,
1258 char *buf)
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02001259{
1260 int nr = (to_sensor_dev_attr(attr))->index;
1261 struct lm93_data *data = lm93_update_device(dev);
Guenter Roeck2804a4cf2012-01-14 21:25:54 -08001262 return sprintf(buf, "%d\n", LM93_TEMP_FROM_REG(data->block2[nr]));
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02001263}
1264
Guenter Roeck7f9d0582018-12-10 14:02:13 -08001265static SENSOR_DEVICE_ATTR_RO(temp1_input, temp, 0);
1266static SENSOR_DEVICE_ATTR_RO(temp2_input, temp, 1);
1267static SENSOR_DEVICE_ATTR_RO(temp3_input, temp, 2);
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02001268
Guenter Roeck7f9d0582018-12-10 14:02:13 -08001269static ssize_t temp_min_show(struct device *dev,
1270 struct device_attribute *attr, char *buf)
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02001271{
1272 int nr = (to_sensor_dev_attr(attr))->index;
1273 struct lm93_data *data = lm93_update_device(dev);
Guenter Roeck2804a4cf2012-01-14 21:25:54 -08001274 return sprintf(buf, "%d\n", LM93_TEMP_FROM_REG(data->temp_lim[nr].min));
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02001275}
1276
Guenter Roeck7f9d0582018-12-10 14:02:13 -08001277static ssize_t temp_min_store(struct device *dev,
1278 struct device_attribute *attr, const char *buf,
1279 size_t count)
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02001280{
1281 int nr = (to_sensor_dev_attr(attr))->index;
Axel Lin6579d582014-07-02 20:36:50 +08001282 struct lm93_data *data = dev_get_drvdata(dev);
1283 struct i2c_client *client = data->client;
Guenter Roeck2804a4cf2012-01-14 21:25:54 -08001284 long val;
1285 int err;
1286
1287 err = kstrtol(buf, 10, &val);
1288 if (err)
1289 return err;
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02001290
1291 mutex_lock(&data->update_lock);
1292 data->temp_lim[nr].min = LM93_TEMP_TO_REG(val);
1293 lm93_write_byte(client, LM93_REG_TEMP_MIN(nr), data->temp_lim[nr].min);
1294 mutex_unlock(&data->update_lock);
1295 return count;
1296}
1297
Guenter Roeck7f9d0582018-12-10 14:02:13 -08001298static SENSOR_DEVICE_ATTR_RW(temp1_min, temp_min, 0);
1299static SENSOR_DEVICE_ATTR_RW(temp2_min, temp_min, 1);
1300static SENSOR_DEVICE_ATTR_RW(temp3_min, temp_min, 2);
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02001301
Guenter Roeck7f9d0582018-12-10 14:02:13 -08001302static ssize_t temp_max_show(struct device *dev,
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02001303 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->temp_lim[nr].max));
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02001308}
1309
Guenter Roeck7f9d0582018-12-10 14:02:13 -08001310static ssize_t temp_max_store(struct device *dev,
1311 struct device_attribute *attr, const char *buf,
1312 size_t count)
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02001313{
1314 int nr = (to_sensor_dev_attr(attr))->index;
Axel Lin6579d582014-07-02 20:36:50 +08001315 struct lm93_data *data = dev_get_drvdata(dev);
1316 struct i2c_client *client = data->client;
Guenter Roeck2804a4cf2012-01-14 21:25:54 -08001317 long val;
1318 int err;
1319
1320 err = kstrtol(buf, 10, &val);
1321 if (err)
1322 return err;
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02001323
1324 mutex_lock(&data->update_lock);
1325 data->temp_lim[nr].max = LM93_TEMP_TO_REG(val);
1326 lm93_write_byte(client, LM93_REG_TEMP_MAX(nr), data->temp_lim[nr].max);
1327 mutex_unlock(&data->update_lock);
1328 return count;
1329}
1330
Guenter Roeck7f9d0582018-12-10 14:02:13 -08001331static SENSOR_DEVICE_ATTR_RW(temp1_max, temp_max, 0);
1332static SENSOR_DEVICE_ATTR_RW(temp2_max, temp_max, 1);
1333static SENSOR_DEVICE_ATTR_RW(temp3_max, temp_max, 2);
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02001334
Guenter Roeck7f9d0582018-12-10 14:02:13 -08001335static ssize_t temp_auto_base_show(struct device *dev,
1336 struct device_attribute *attr, char *buf)
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02001337{
1338 int nr = (to_sensor_dev_attr(attr))->index;
1339 struct lm93_data *data = lm93_update_device(dev);
Guenter Roeck2804a4cf2012-01-14 21:25:54 -08001340 return sprintf(buf, "%d\n", LM93_TEMP_FROM_REG(data->block10.base[nr]));
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02001341}
1342
Guenter Roeck7f9d0582018-12-10 14:02:13 -08001343static ssize_t temp_auto_base_store(struct device *dev,
1344 struct device_attribute *attr,
1345 const char *buf, size_t count)
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02001346{
1347 int nr = (to_sensor_dev_attr(attr))->index;
Axel Lin6579d582014-07-02 20:36:50 +08001348 struct lm93_data *data = dev_get_drvdata(dev);
1349 struct i2c_client *client = data->client;
Guenter Roeck2804a4cf2012-01-14 21:25:54 -08001350 long val;
1351 int err;
1352
1353 err = kstrtol(buf, 10, &val);
1354 if (err)
1355 return err;
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02001356
1357 mutex_lock(&data->update_lock);
1358 data->block10.base[nr] = LM93_TEMP_TO_REG(val);
1359 lm93_write_byte(client, LM93_REG_TEMP_BASE(nr), data->block10.base[nr]);
1360 mutex_unlock(&data->update_lock);
1361 return count;
1362}
1363
Guenter Roeck7f9d0582018-12-10 14:02:13 -08001364static SENSOR_DEVICE_ATTR_RW(temp1_auto_base, temp_auto_base, 0);
1365static SENSOR_DEVICE_ATTR_RW(temp2_auto_base, temp_auto_base, 1);
1366static SENSOR_DEVICE_ATTR_RW(temp3_auto_base, temp_auto_base, 2);
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02001367
Guenter Roeck7f9d0582018-12-10 14:02:13 -08001368static ssize_t temp_auto_boost_show(struct device *dev,
Guenter Roeck2804a4cf2012-01-14 21:25:54 -08001369 struct device_attribute *attr, char *buf)
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02001370{
1371 int nr = (to_sensor_dev_attr(attr))->index;
1372 struct lm93_data *data = lm93_update_device(dev);
Guenter Roeck2804a4cf2012-01-14 21:25:54 -08001373 return sprintf(buf, "%d\n", LM93_TEMP_FROM_REG(data->boost[nr]));
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02001374}
1375
Guenter Roeck7f9d0582018-12-10 14:02:13 -08001376static ssize_t temp_auto_boost_store(struct device *dev,
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02001377 struct device_attribute *attr,
1378 const char *buf, size_t count)
1379{
1380 int nr = (to_sensor_dev_attr(attr))->index;
Axel Lin6579d582014-07-02 20:36:50 +08001381 struct lm93_data *data = dev_get_drvdata(dev);
1382 struct i2c_client *client = data->client;
Guenter Roeck2804a4cf2012-01-14 21:25:54 -08001383 long val;
1384 int err;
1385
1386 err = kstrtol(buf, 10, &val);
1387 if (err)
1388 return err;
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02001389
1390 mutex_lock(&data->update_lock);
1391 data->boost[nr] = LM93_TEMP_TO_REG(val);
1392 lm93_write_byte(client, LM93_REG_BOOST(nr), data->boost[nr]);
1393 mutex_unlock(&data->update_lock);
1394 return count;
1395}
1396
Guenter Roeck7f9d0582018-12-10 14:02:13 -08001397static SENSOR_DEVICE_ATTR_RW(temp1_auto_boost, temp_auto_boost, 0);
1398static SENSOR_DEVICE_ATTR_RW(temp2_auto_boost, temp_auto_boost, 1);
1399static SENSOR_DEVICE_ATTR_RW(temp3_auto_boost, temp_auto_boost, 2);
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02001400
Guenter Roeck7f9d0582018-12-10 14:02:13 -08001401static ssize_t temp_auto_boost_hyst_show(struct device *dev,
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02001402 struct device_attribute *attr,
1403 char *buf)
1404{
1405 int nr = (to_sensor_dev_attr(attr))->index;
1406 struct lm93_data *data = lm93_update_device(dev);
1407 int mode = LM93_TEMP_OFFSET_MODE_FROM_REG(data->sfc2, nr);
Guenter Roeck2804a4cf2012-01-14 21:25:54 -08001408 return sprintf(buf, "%d\n",
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02001409 LM93_AUTO_BOOST_HYST_FROM_REGS(data, nr, mode));
1410}
1411
Guenter Roeck7f9d0582018-12-10 14:02:13 -08001412static ssize_t temp_auto_boost_hyst_store(struct device *dev,
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02001413 struct device_attribute *attr,
1414 const char *buf, size_t count)
1415{
1416 int nr = (to_sensor_dev_attr(attr))->index;
Axel Lin6579d582014-07-02 20:36:50 +08001417 struct lm93_data *data = dev_get_drvdata(dev);
1418 struct i2c_client *client = data->client;
Guenter Roeck2804a4cf2012-01-14 21:25:54 -08001419 unsigned long val;
1420 int err;
1421
1422 err = kstrtoul(buf, 10, &val);
1423 if (err)
1424 return err;
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02001425
1426 mutex_lock(&data->update_lock);
1427 /* force 0.5C/bit mode */
1428 data->sfc2 = lm93_read_byte(client, LM93_REG_SFC2);
1429 data->sfc2 |= ((nr < 2) ? 0x10 : 0x20);
1430 lm93_write_byte(client, LM93_REG_SFC2, data->sfc2);
1431 data->boost_hyst[nr/2] = LM93_AUTO_BOOST_HYST_TO_REG(data, val, nr, 1);
1432 lm93_write_byte(client, LM93_REG_BOOST_HYST(nr),
1433 data->boost_hyst[nr/2]);
1434 mutex_unlock(&data->update_lock);
1435 return count;
1436}
1437
Guenter Roeck7f9d0582018-12-10 14:02:13 -08001438static SENSOR_DEVICE_ATTR_RW(temp1_auto_boost_hyst, temp_auto_boost_hyst, 0);
1439static SENSOR_DEVICE_ATTR_RW(temp2_auto_boost_hyst, temp_auto_boost_hyst, 1);
1440static SENSOR_DEVICE_ATTR_RW(temp3_auto_boost_hyst, temp_auto_boost_hyst, 2);
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02001441
Guenter Roeck7f9d0582018-12-10 14:02:13 -08001442static ssize_t temp_auto_offset_show(struct device *dev,
1443 struct device_attribute *attr, char *buf)
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02001444{
1445 struct sensor_device_attribute_2 *s_attr = to_sensor_dev_attr_2(attr);
1446 int nr = s_attr->index;
1447 int ofs = s_attr->nr;
1448 struct lm93_data *data = lm93_update_device(dev);
1449 int mode = LM93_TEMP_OFFSET_MODE_FROM_REG(data->sfc2, nr);
Guenter Roeck2804a4cf2012-01-14 21:25:54 -08001450 return sprintf(buf, "%d\n",
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02001451 LM93_TEMP_AUTO_OFFSET_FROM_REG(data->block10.offset[ofs],
Guenter Roeck2804a4cf2012-01-14 21:25:54 -08001452 nr, mode));
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02001453}
1454
Guenter Roeck7f9d0582018-12-10 14:02:13 -08001455static ssize_t temp_auto_offset_store(struct device *dev,
1456 struct device_attribute *attr,
1457 const char *buf, size_t count)
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02001458{
1459 struct sensor_device_attribute_2 *s_attr = to_sensor_dev_attr_2(attr);
1460 int nr = s_attr->index;
1461 int ofs = s_attr->nr;
Axel Lin6579d582014-07-02 20:36:50 +08001462 struct lm93_data *data = dev_get_drvdata(dev);
1463 struct i2c_client *client = data->client;
Guenter Roeck2804a4cf2012-01-14 21:25:54 -08001464 unsigned long val;
1465 int err;
1466
1467 err = kstrtoul(buf, 10, &val);
1468 if (err)
1469 return err;
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02001470
1471 mutex_lock(&data->update_lock);
1472 /* force 0.5C/bit mode */
1473 data->sfc2 = lm93_read_byte(client, LM93_REG_SFC2);
1474 data->sfc2 |= ((nr < 2) ? 0x10 : 0x20);
1475 lm93_write_byte(client, LM93_REG_SFC2, data->sfc2);
1476 data->block10.offset[ofs] = LM93_TEMP_AUTO_OFFSET_TO_REG(
1477 data->block10.offset[ofs], val, nr, 1);
1478 lm93_write_byte(client, LM93_REG_TEMP_OFFSET(ofs),
1479 data->block10.offset[ofs]);
1480 mutex_unlock(&data->update_lock);
1481 return count;
1482}
1483
Guenter Roeck7f9d0582018-12-10 14:02:13 -08001484static SENSOR_DEVICE_ATTR_2_RW(temp1_auto_offset1, temp_auto_offset, 0, 0);
1485static SENSOR_DEVICE_ATTR_2_RW(temp1_auto_offset2, temp_auto_offset, 1, 0);
1486static SENSOR_DEVICE_ATTR_2_RW(temp1_auto_offset3, temp_auto_offset, 2, 0);
1487static SENSOR_DEVICE_ATTR_2_RW(temp1_auto_offset4, temp_auto_offset, 3, 0);
1488static SENSOR_DEVICE_ATTR_2_RW(temp1_auto_offset5, temp_auto_offset, 4, 0);
1489static SENSOR_DEVICE_ATTR_2_RW(temp1_auto_offset6, temp_auto_offset, 5, 0);
1490static SENSOR_DEVICE_ATTR_2_RW(temp1_auto_offset7, temp_auto_offset, 6, 0);
1491static SENSOR_DEVICE_ATTR_2_RW(temp1_auto_offset8, temp_auto_offset, 7, 0);
1492static SENSOR_DEVICE_ATTR_2_RW(temp1_auto_offset9, temp_auto_offset, 8, 0);
1493static SENSOR_DEVICE_ATTR_2_RW(temp1_auto_offset10, temp_auto_offset, 9, 0);
1494static SENSOR_DEVICE_ATTR_2_RW(temp1_auto_offset11, temp_auto_offset, 10, 0);
1495static SENSOR_DEVICE_ATTR_2_RW(temp1_auto_offset12, temp_auto_offset, 11, 0);
1496static SENSOR_DEVICE_ATTR_2_RW(temp2_auto_offset1, temp_auto_offset, 0, 1);
1497static SENSOR_DEVICE_ATTR_2_RW(temp2_auto_offset2, temp_auto_offset, 1, 1);
1498static SENSOR_DEVICE_ATTR_2_RW(temp2_auto_offset3, temp_auto_offset, 2, 1);
1499static SENSOR_DEVICE_ATTR_2_RW(temp2_auto_offset4, temp_auto_offset, 3, 1);
1500static SENSOR_DEVICE_ATTR_2_RW(temp2_auto_offset5, temp_auto_offset, 4, 1);
1501static SENSOR_DEVICE_ATTR_2_RW(temp2_auto_offset6, temp_auto_offset, 5, 1);
1502static SENSOR_DEVICE_ATTR_2_RW(temp2_auto_offset7, temp_auto_offset, 6, 1);
1503static SENSOR_DEVICE_ATTR_2_RW(temp2_auto_offset8, temp_auto_offset, 7, 1);
1504static SENSOR_DEVICE_ATTR_2_RW(temp2_auto_offset9, temp_auto_offset, 8, 1);
1505static SENSOR_DEVICE_ATTR_2_RW(temp2_auto_offset10, temp_auto_offset, 9, 1);
1506static SENSOR_DEVICE_ATTR_2_RW(temp2_auto_offset11, temp_auto_offset, 10, 1);
1507static SENSOR_DEVICE_ATTR_2_RW(temp2_auto_offset12, temp_auto_offset, 11, 1);
1508static SENSOR_DEVICE_ATTR_2_RW(temp3_auto_offset1, temp_auto_offset, 0, 2);
1509static SENSOR_DEVICE_ATTR_2_RW(temp3_auto_offset2, temp_auto_offset, 1, 2);
1510static SENSOR_DEVICE_ATTR_2_RW(temp3_auto_offset3, temp_auto_offset, 2, 2);
1511static SENSOR_DEVICE_ATTR_2_RW(temp3_auto_offset4, temp_auto_offset, 3, 2);
1512static SENSOR_DEVICE_ATTR_2_RW(temp3_auto_offset5, temp_auto_offset, 4, 2);
1513static SENSOR_DEVICE_ATTR_2_RW(temp3_auto_offset6, temp_auto_offset, 5, 2);
1514static SENSOR_DEVICE_ATTR_2_RW(temp3_auto_offset7, temp_auto_offset, 6, 2);
1515static SENSOR_DEVICE_ATTR_2_RW(temp3_auto_offset8, temp_auto_offset, 7, 2);
1516static SENSOR_DEVICE_ATTR_2_RW(temp3_auto_offset9, temp_auto_offset, 8, 2);
1517static SENSOR_DEVICE_ATTR_2_RW(temp3_auto_offset10, temp_auto_offset, 9, 2);
1518static SENSOR_DEVICE_ATTR_2_RW(temp3_auto_offset11, temp_auto_offset, 10, 2);
1519static SENSOR_DEVICE_ATTR_2_RW(temp3_auto_offset12, temp_auto_offset, 11, 2);
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02001520
Guenter Roeck7f9d0582018-12-10 14:02:13 -08001521static ssize_t temp_auto_pwm_min_show(struct device *dev,
1522 struct device_attribute *attr,
1523 char *buf)
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02001524{
1525 int nr = (to_sensor_dev_attr(attr))->index;
1526 u8 reg, ctl4;
1527 struct lm93_data *data = lm93_update_device(dev);
1528 reg = data->auto_pwm_min_hyst[nr/2] >> 4 & 0x0f;
1529 ctl4 = data->block9[nr][LM93_PWM_CTL4];
Guenter Roeck2804a4cf2012-01-14 21:25:54 -08001530 return sprintf(buf, "%d\n", LM93_PWM_FROM_REG(reg, (ctl4 & 0x07) ?
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02001531 LM93_PWM_MAP_LO_FREQ : LM93_PWM_MAP_HI_FREQ));
1532}
1533
Guenter Roeck7f9d0582018-12-10 14:02:13 -08001534static ssize_t temp_auto_pwm_min_store(struct device *dev,
1535 struct device_attribute *attr,
1536 const char *buf, size_t count)
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02001537{
1538 int nr = (to_sensor_dev_attr(attr))->index;
Axel Lin6579d582014-07-02 20:36:50 +08001539 struct lm93_data *data = dev_get_drvdata(dev);
1540 struct i2c_client *client = data->client;
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02001541 u8 reg, ctl4;
Guenter Roeck2804a4cf2012-01-14 21:25:54 -08001542 unsigned long val;
1543 int err;
1544
1545 err = kstrtoul(buf, 10, &val);
1546 if (err)
1547 return err;
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02001548
1549 mutex_lock(&data->update_lock);
1550 reg = lm93_read_byte(client, LM93_REG_PWM_MIN_HYST(nr));
Guenter Roeck2804a4cf2012-01-14 21:25:54 -08001551 ctl4 = lm93_read_byte(client, LM93_REG_PWM_CTL(nr, LM93_PWM_CTL4));
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02001552 reg = (reg & 0x0f) |
1553 LM93_PWM_TO_REG(val, (ctl4 & 0x07) ?
1554 LM93_PWM_MAP_LO_FREQ :
1555 LM93_PWM_MAP_HI_FREQ) << 4;
1556 data->auto_pwm_min_hyst[nr/2] = reg;
1557 lm93_write_byte(client, LM93_REG_PWM_MIN_HYST(nr), reg);
1558 mutex_unlock(&data->update_lock);
1559 return count;
1560}
1561
Guenter Roeck7f9d0582018-12-10 14:02:13 -08001562static SENSOR_DEVICE_ATTR_RW(temp1_auto_pwm_min, temp_auto_pwm_min, 0);
1563static SENSOR_DEVICE_ATTR_RW(temp2_auto_pwm_min, temp_auto_pwm_min, 1);
1564static SENSOR_DEVICE_ATTR_RW(temp3_auto_pwm_min, temp_auto_pwm_min, 2);
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02001565
Guenter Roeck7f9d0582018-12-10 14:02:13 -08001566static ssize_t temp_auto_offset_hyst_show(struct device *dev,
1567 struct device_attribute *attr,
1568 char *buf)
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02001569{
1570 int nr = (to_sensor_dev_attr(attr))->index;
1571 struct lm93_data *data = lm93_update_device(dev);
1572 int mode = LM93_TEMP_OFFSET_MODE_FROM_REG(data->sfc2, nr);
Guenter Roeck2804a4cf2012-01-14 21:25:54 -08001573 return sprintf(buf, "%d\n", LM93_TEMP_OFFSET_FROM_REG(
1574 data->auto_pwm_min_hyst[nr / 2], mode));
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02001575}
1576
Guenter Roeck7f9d0582018-12-10 14:02:13 -08001577static ssize_t temp_auto_offset_hyst_store(struct device *dev,
1578 struct device_attribute *attr,
1579 const char *buf, size_t count)
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02001580{
1581 int nr = (to_sensor_dev_attr(attr))->index;
Axel Lin6579d582014-07-02 20:36:50 +08001582 struct lm93_data *data = dev_get_drvdata(dev);
1583 struct i2c_client *client = data->client;
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02001584 u8 reg;
Guenter Roeck2804a4cf2012-01-14 21:25:54 -08001585 unsigned long val;
1586 int err;
1587
1588 err = kstrtoul(buf, 10, &val);
1589 if (err)
1590 return err;
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02001591
1592 mutex_lock(&data->update_lock);
1593 /* force 0.5C/bit mode */
1594 data->sfc2 = lm93_read_byte(client, LM93_REG_SFC2);
1595 data->sfc2 |= ((nr < 2) ? 0x10 : 0x20);
1596 lm93_write_byte(client, LM93_REG_SFC2, data->sfc2);
1597 reg = data->auto_pwm_min_hyst[nr/2];
1598 reg = (reg & 0xf0) | (LM93_TEMP_OFFSET_TO_REG(val, 1) & 0x0f);
1599 data->auto_pwm_min_hyst[nr/2] = reg;
1600 lm93_write_byte(client, LM93_REG_PWM_MIN_HYST(nr), reg);
1601 mutex_unlock(&data->update_lock);
1602 return count;
1603}
1604
Guenter Roeck7f9d0582018-12-10 14:02:13 -08001605static SENSOR_DEVICE_ATTR_RW(temp1_auto_offset_hyst, temp_auto_offset_hyst, 0);
1606static SENSOR_DEVICE_ATTR_RW(temp2_auto_offset_hyst, temp_auto_offset_hyst, 1);
1607static SENSOR_DEVICE_ATTR_RW(temp3_auto_offset_hyst, temp_auto_offset_hyst, 2);
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02001608
Guenter Roeck7f9d0582018-12-10 14:02:13 -08001609static ssize_t fan_input_show(struct device *dev,
1610 struct device_attribute *attr, char *buf)
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02001611{
1612 struct sensor_device_attribute *s_attr = to_sensor_dev_attr(attr);
1613 int nr = s_attr->index;
1614 struct lm93_data *data = lm93_update_device(dev);
1615
Guenter Roeck2804a4cf2012-01-14 21:25:54 -08001616 return sprintf(buf, "%d\n", LM93_FAN_FROM_REG(data->block5[nr]));
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02001617}
1618
Guenter Roeck7f9d0582018-12-10 14:02:13 -08001619static SENSOR_DEVICE_ATTR_RO(fan1_input, fan_input, 0);
1620static SENSOR_DEVICE_ATTR_RO(fan2_input, fan_input, 1);
1621static SENSOR_DEVICE_ATTR_RO(fan3_input, fan_input, 2);
1622static SENSOR_DEVICE_ATTR_RO(fan4_input, fan_input, 3);
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02001623
Guenter Roeck7f9d0582018-12-10 14:02:13 -08001624static ssize_t fan_min_show(struct device *dev, struct device_attribute *attr,
1625 char *buf)
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02001626{
1627 int nr = (to_sensor_dev_attr(attr))->index;
1628 struct lm93_data *data = lm93_update_device(dev);
1629
Guenter Roeck2804a4cf2012-01-14 21:25:54 -08001630 return sprintf(buf, "%d\n", LM93_FAN_FROM_REG(data->block8[nr]));
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02001631}
1632
Guenter Roeck7f9d0582018-12-10 14:02:13 -08001633static ssize_t fan_min_store(struct device *dev,
1634 struct device_attribute *attr, const char *buf,
1635 size_t count)
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02001636{
1637 int nr = (to_sensor_dev_attr(attr))->index;
Axel Lin6579d582014-07-02 20:36:50 +08001638 struct lm93_data *data = dev_get_drvdata(dev);
1639 struct i2c_client *client = data->client;
Guenter Roeck2804a4cf2012-01-14 21:25:54 -08001640 unsigned long val;
1641 int err;
1642
1643 err = kstrtoul(buf, 10, &val);
1644 if (err)
1645 return err;
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02001646
1647 mutex_lock(&data->update_lock);
1648 data->block8[nr] = LM93_FAN_TO_REG(val);
Guenter Roeck2804a4cf2012-01-14 21:25:54 -08001649 lm93_write_word(client, LM93_REG_FAN_MIN(nr), data->block8[nr]);
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02001650 mutex_unlock(&data->update_lock);
1651 return count;
1652}
1653
Guenter Roeck7f9d0582018-12-10 14:02:13 -08001654static SENSOR_DEVICE_ATTR_RW(fan1_min, fan_min, 0);
1655static SENSOR_DEVICE_ATTR_RW(fan2_min, fan_min, 1);
1656static SENSOR_DEVICE_ATTR_RW(fan3_min, fan_min, 2);
1657static SENSOR_DEVICE_ATTR_RW(fan4_min, fan_min, 3);
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02001658
Guenter Roeck2804a4cf2012-01-14 21:25:54 -08001659/*
1660 * some tedious bit-twiddling here to deal with the register format:
1661 *
1662 * data->sf_tach_to_pwm: (tach to pwm mapping bits)
1663 *
1664 * bit | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0
1665 * T4:P2 T4:P1 T3:P2 T3:P1 T2:P2 T2:P1 T1:P2 T1:P1
1666 *
1667 * data->sfc2: (enable bits)
1668 *
1669 * bit | 3 | 2 | 1 | 0
1670 * T4 T3 T2 T1
1671 */
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02001672
Guenter Roeck7f9d0582018-12-10 14:02:13 -08001673static ssize_t fan_smart_tach_show(struct device *dev,
1674 struct device_attribute *attr, char *buf)
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02001675{
1676 int nr = (to_sensor_dev_attr(attr))->index;
1677 struct lm93_data *data = lm93_update_device(dev);
1678 long rc = 0;
1679 int mapping;
1680
1681 /* extract the relevant mapping */
1682 mapping = (data->sf_tach_to_pwm >> (nr * 2)) & 0x03;
1683
1684 /* if there's a mapping and it's enabled */
1685 if (mapping && ((data->sfc2 >> nr) & 0x01))
1686 rc = mapping;
Guenter Roeck2804a4cf2012-01-14 21:25:54 -08001687 return sprintf(buf, "%ld\n", rc);
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02001688}
1689
Guenter Roeck2804a4cf2012-01-14 21:25:54 -08001690/*
1691 * helper function - must grab data->update_lock before calling
1692 * fan is 0-3, indicating fan1-fan4
1693 */
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02001694static void lm93_write_fan_smart_tach(struct i2c_client *client,
1695 struct lm93_data *data, int fan, long value)
1696{
1697 /* insert the new mapping and write it out */
1698 data->sf_tach_to_pwm = lm93_read_byte(client, LM93_REG_SF_TACH_TO_PWM);
1699 data->sf_tach_to_pwm &= ~(0x3 << fan * 2);
1700 data->sf_tach_to_pwm |= value << fan * 2;
1701 lm93_write_byte(client, LM93_REG_SF_TACH_TO_PWM, data->sf_tach_to_pwm);
1702
1703 /* insert the enable bit and write it out */
1704 data->sfc2 = lm93_read_byte(client, LM93_REG_SFC2);
1705 if (value)
1706 data->sfc2 |= 1 << fan;
1707 else
1708 data->sfc2 &= ~(1 << fan);
1709 lm93_write_byte(client, LM93_REG_SFC2, data->sfc2);
1710}
1711
Guenter Roeck7f9d0582018-12-10 14:02:13 -08001712static ssize_t fan_smart_tach_store(struct device *dev,
1713 struct device_attribute *attr,
1714 const char *buf, size_t count)
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02001715{
1716 int nr = (to_sensor_dev_attr(attr))->index;
Axel Lin6579d582014-07-02 20:36:50 +08001717 struct lm93_data *data = dev_get_drvdata(dev);
1718 struct i2c_client *client = data->client;
Guenter Roeck2804a4cf2012-01-14 21:25:54 -08001719 unsigned long val;
1720 int err;
1721
1722 err = kstrtoul(buf, 10, &val);
1723 if (err)
1724 return err;
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02001725
1726 mutex_lock(&data->update_lock);
1727 /* sanity test, ignore the write otherwise */
Guenter Roeck3be81022012-09-19 11:21:51 -07001728 if (val <= 2) {
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02001729 /* can't enable if pwm freq is 22.5KHz */
1730 if (val) {
1731 u8 ctl4 = lm93_read_byte(client,
Guenter Roeck2804a4cf2012-01-14 21:25:54 -08001732 LM93_REG_PWM_CTL(val - 1, LM93_PWM_CTL4));
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02001733 if ((ctl4 & 0x07) == 0)
1734 val = 0;
1735 }
1736 lm93_write_fan_smart_tach(client, data, nr, val);
1737 }
1738 mutex_unlock(&data->update_lock);
1739 return count;
1740}
1741
Guenter Roeck7f9d0582018-12-10 14:02:13 -08001742static SENSOR_DEVICE_ATTR_RW(fan1_smart_tach, fan_smart_tach, 0);
1743static SENSOR_DEVICE_ATTR_RW(fan2_smart_tach, fan_smart_tach, 1);
1744static SENSOR_DEVICE_ATTR_RW(fan3_smart_tach, fan_smart_tach, 2);
1745static SENSOR_DEVICE_ATTR_RW(fan4_smart_tach, fan_smart_tach, 3);
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02001746
Guenter Roeck7f9d0582018-12-10 14:02:13 -08001747static ssize_t pwm_show(struct device *dev, struct device_attribute *attr,
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02001748 char *buf)
1749{
1750 int nr = (to_sensor_dev_attr(attr))->index;
1751 struct lm93_data *data = lm93_update_device(dev);
1752 u8 ctl2, ctl4;
1753 long rc;
1754
1755 ctl2 = data->block9[nr][LM93_PWM_CTL2];
1756 ctl4 = data->block9[nr][LM93_PWM_CTL4];
1757 if (ctl2 & 0x01) /* show user commanded value if enabled */
1758 rc = data->pwm_override[nr];
1759 else /* show present h/w value if manual pwm disabled */
1760 rc = LM93_PWM_FROM_REG(ctl2 >> 4, (ctl4 & 0x07) ?
1761 LM93_PWM_MAP_LO_FREQ : LM93_PWM_MAP_HI_FREQ);
Guenter Roeck2804a4cf2012-01-14 21:25:54 -08001762 return sprintf(buf, "%ld\n", rc);
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02001763}
1764
Guenter Roeck7f9d0582018-12-10 14:02:13 -08001765static ssize_t pwm_store(struct device *dev, struct device_attribute *attr,
1766 const char *buf, size_t count)
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02001767{
1768 int nr = (to_sensor_dev_attr(attr))->index;
Axel Lin6579d582014-07-02 20:36:50 +08001769 struct lm93_data *data = dev_get_drvdata(dev);
1770 struct i2c_client *client = data->client;
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02001771 u8 ctl2, ctl4;
Guenter Roeck2804a4cf2012-01-14 21:25:54 -08001772 unsigned long val;
1773 int err;
1774
1775 err = kstrtoul(buf, 10, &val);
1776 if (err)
1777 return err;
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02001778
1779 mutex_lock(&data->update_lock);
Guenter Roeck2804a4cf2012-01-14 21:25:54 -08001780 ctl2 = lm93_read_byte(client, LM93_REG_PWM_CTL(nr, LM93_PWM_CTL2));
1781 ctl4 = lm93_read_byte(client, LM93_REG_PWM_CTL(nr, LM93_PWM_CTL4));
1782 ctl2 = (ctl2 & 0x0f) | LM93_PWM_TO_REG(val, (ctl4 & 0x07) ?
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02001783 LM93_PWM_MAP_LO_FREQ : LM93_PWM_MAP_HI_FREQ) << 4;
1784 /* save user commanded value */
1785 data->pwm_override[nr] = LM93_PWM_FROM_REG(ctl2 >> 4,
1786 (ctl4 & 0x07) ? LM93_PWM_MAP_LO_FREQ :
1787 LM93_PWM_MAP_HI_FREQ);
Guenter Roeck2804a4cf2012-01-14 21:25:54 -08001788 lm93_write_byte(client, LM93_REG_PWM_CTL(nr, LM93_PWM_CTL2), ctl2);
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02001789 mutex_unlock(&data->update_lock);
1790 return count;
1791}
1792
Guenter Roeck7f9d0582018-12-10 14:02:13 -08001793static SENSOR_DEVICE_ATTR_RW(pwm1, pwm, 0);
1794static SENSOR_DEVICE_ATTR_RW(pwm2, pwm, 1);
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02001795
Guenter Roeck7f9d0582018-12-10 14:02:13 -08001796static ssize_t pwm_enable_show(struct device *dev,
1797 struct device_attribute *attr, char *buf)
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02001798{
1799 int nr = (to_sensor_dev_attr(attr))->index;
1800 struct lm93_data *data = lm93_update_device(dev);
1801 u8 ctl2;
1802 long rc;
1803
1804 ctl2 = data->block9[nr][LM93_PWM_CTL2];
1805 if (ctl2 & 0x01) /* manual override enabled ? */
1806 rc = ((ctl2 & 0xF0) == 0xF0) ? 0 : 1;
1807 else
1808 rc = 2;
Guenter Roeck2804a4cf2012-01-14 21:25:54 -08001809 return sprintf(buf, "%ld\n", rc);
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02001810}
1811
Guenter Roeck7f9d0582018-12-10 14:02:13 -08001812static ssize_t pwm_enable_store(struct device *dev,
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02001813 struct device_attribute *attr,
1814 const char *buf, size_t count)
1815{
1816 int nr = (to_sensor_dev_attr(attr))->index;
Axel Lin6579d582014-07-02 20:36:50 +08001817 struct lm93_data *data = dev_get_drvdata(dev);
1818 struct i2c_client *client = data->client;
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02001819 u8 ctl2;
Guenter Roeck2804a4cf2012-01-14 21:25:54 -08001820 unsigned long val;
1821 int err;
1822
1823 err = kstrtoul(buf, 10, &val);
1824 if (err)
1825 return err;
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02001826
1827 mutex_lock(&data->update_lock);
Guenter Roeck2804a4cf2012-01-14 21:25:54 -08001828 ctl2 = lm93_read_byte(client, LM93_REG_PWM_CTL(nr, LM93_PWM_CTL2));
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02001829
1830 switch (val) {
1831 case 0:
1832 ctl2 |= 0xF1; /* enable manual override, set PWM to max */
1833 break;
Guenter Roeck2804a4cf2012-01-14 21:25:54 -08001834 case 1:
1835 ctl2 |= 0x01; /* enable manual override */
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02001836 break;
Guenter Roeck2804a4cf2012-01-14 21:25:54 -08001837 case 2:
1838 ctl2 &= ~0x01; /* disable manual override */
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02001839 break;
1840 default:
1841 mutex_unlock(&data->update_lock);
1842 return -EINVAL;
1843 }
1844
Guenter Roeck2804a4cf2012-01-14 21:25:54 -08001845 lm93_write_byte(client, LM93_REG_PWM_CTL(nr, LM93_PWM_CTL2), ctl2);
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02001846 mutex_unlock(&data->update_lock);
1847 return count;
1848}
1849
Guenter Roeck7f9d0582018-12-10 14:02:13 -08001850static SENSOR_DEVICE_ATTR_RW(pwm1_enable, pwm_enable, 0);
1851static SENSOR_DEVICE_ATTR_RW(pwm2_enable, pwm_enable, 1);
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02001852
Guenter Roeck7f9d0582018-12-10 14:02:13 -08001853static ssize_t pwm_freq_show(struct device *dev,
1854 struct device_attribute *attr, char *buf)
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02001855{
1856 int nr = (to_sensor_dev_attr(attr))->index;
1857 struct lm93_data *data = lm93_update_device(dev);
1858 u8 ctl4;
1859
1860 ctl4 = data->block9[nr][LM93_PWM_CTL4];
Guenter Roeck2804a4cf2012-01-14 21:25:54 -08001861 return sprintf(buf, "%d\n", LM93_PWM_FREQ_FROM_REG(ctl4));
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02001862}
1863
Guenter Roeck2804a4cf2012-01-14 21:25:54 -08001864/*
1865 * helper function - must grab data->update_lock before calling
1866 * pwm is 0-1, indicating pwm1-pwm2
1867 * this disables smart tach for all tach channels bound to the given pwm
1868 */
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02001869static void lm93_disable_fan_smart_tach(struct i2c_client *client,
1870 struct lm93_data *data, int pwm)
1871{
1872 int mapping = lm93_read_byte(client, LM93_REG_SF_TACH_TO_PWM);
1873 int mask;
1874
1875 /* collapse the mapping into a mask of enable bits */
1876 mapping = (mapping >> pwm) & 0x55;
1877 mask = mapping & 0x01;
1878 mask |= (mapping & 0x04) >> 1;
1879 mask |= (mapping & 0x10) >> 2;
1880 mask |= (mapping & 0x40) >> 3;
1881
1882 /* disable smart tach according to the mask */
1883 data->sfc2 = lm93_read_byte(client, LM93_REG_SFC2);
1884 data->sfc2 &= ~mask;
1885 lm93_write_byte(client, LM93_REG_SFC2, data->sfc2);
1886}
1887
Guenter Roeck7f9d0582018-12-10 14:02:13 -08001888static ssize_t pwm_freq_store(struct device *dev,
1889 struct device_attribute *attr, const char *buf,
1890 size_t count)
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02001891{
1892 int nr = (to_sensor_dev_attr(attr))->index;
Axel Lin6579d582014-07-02 20:36:50 +08001893 struct lm93_data *data = dev_get_drvdata(dev);
1894 struct i2c_client *client = data->client;
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02001895 u8 ctl4;
Guenter Roeck2804a4cf2012-01-14 21:25:54 -08001896 unsigned long val;
1897 int err;
1898
1899 err = kstrtoul(buf, 10, &val);
1900 if (err)
1901 return err;
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02001902
1903 mutex_lock(&data->update_lock);
Guenter Roeck2804a4cf2012-01-14 21:25:54 -08001904 ctl4 = lm93_read_byte(client, LM93_REG_PWM_CTL(nr, LM93_PWM_CTL4));
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02001905 ctl4 = (ctl4 & 0xf8) | LM93_PWM_FREQ_TO_REG(val);
1906 data->block9[nr][LM93_PWM_CTL4] = ctl4;
1907 /* ctl4 == 0 -> 22.5KHz -> disable smart tach */
1908 if (!ctl4)
1909 lm93_disable_fan_smart_tach(client, data, nr);
Guenter Roeck2804a4cf2012-01-14 21:25:54 -08001910 lm93_write_byte(client, LM93_REG_PWM_CTL(nr, LM93_PWM_CTL4), ctl4);
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02001911 mutex_unlock(&data->update_lock);
1912 return count;
1913}
1914
Guenter Roeck7f9d0582018-12-10 14:02:13 -08001915static SENSOR_DEVICE_ATTR_RW(pwm1_freq, pwm_freq, 0);
1916static SENSOR_DEVICE_ATTR_RW(pwm2_freq, pwm_freq, 1);
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02001917
Guenter Roeck7f9d0582018-12-10 14:02:13 -08001918static ssize_t pwm_auto_channels_show(struct device *dev,
1919 struct device_attribute *attr,
1920 char *buf)
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02001921{
1922 int nr = (to_sensor_dev_attr(attr))->index;
1923 struct lm93_data *data = lm93_update_device(dev);
Guenter Roeck2804a4cf2012-01-14 21:25:54 -08001924 return sprintf(buf, "%d\n", data->block9[nr][LM93_PWM_CTL1]);
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02001925}
1926
Guenter Roeck7f9d0582018-12-10 14:02:13 -08001927static ssize_t pwm_auto_channels_store(struct device *dev,
1928 struct device_attribute *attr,
1929 const char *buf, size_t count)
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02001930{
1931 int nr = (to_sensor_dev_attr(attr))->index;
Axel Lin6579d582014-07-02 20:36:50 +08001932 struct lm93_data *data = dev_get_drvdata(dev);
1933 struct i2c_client *client = data->client;
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 Roeck2a844c12013-01-09 08:09:34 -08001942 data->block9[nr][LM93_PWM_CTL1] = clamp_val(val, 0, 255);
Guenter Roeck2804a4cf2012-01-14 21:25:54 -08001943 lm93_write_byte(client, LM93_REG_PWM_CTL(nr, LM93_PWM_CTL1),
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02001944 data->block9[nr][LM93_PWM_CTL1]);
1945 mutex_unlock(&data->update_lock);
1946 return count;
1947}
1948
Guenter Roeck7f9d0582018-12-10 14:02:13 -08001949static SENSOR_DEVICE_ATTR_RW(pwm1_auto_channels, pwm_auto_channels, 0);
1950static SENSOR_DEVICE_ATTR_RW(pwm2_auto_channels, pwm_auto_channels, 1);
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02001951
Guenter Roeck7f9d0582018-12-10 14:02:13 -08001952static ssize_t pwm_auto_spinup_min_show(struct device *dev,
1953 struct device_attribute *attr,
1954 char *buf)
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02001955{
1956 int nr = (to_sensor_dev_attr(attr))->index;
1957 struct lm93_data *data = lm93_update_device(dev);
1958 u8 ctl3, ctl4;
1959
1960 ctl3 = data->block9[nr][LM93_PWM_CTL3];
1961 ctl4 = data->block9[nr][LM93_PWM_CTL4];
Guenter Roeck2804a4cf2012-01-14 21:25:54 -08001962 return sprintf(buf, "%d\n",
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02001963 LM93_PWM_FROM_REG(ctl3 & 0x0f, (ctl4 & 0x07) ?
1964 LM93_PWM_MAP_LO_FREQ : LM93_PWM_MAP_HI_FREQ));
1965}
1966
Guenter Roeck7f9d0582018-12-10 14:02:13 -08001967static ssize_t pwm_auto_spinup_min_store(struct device *dev,
1968 struct device_attribute *attr,
1969 const char *buf, size_t count)
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02001970{
1971 int nr = (to_sensor_dev_attr(attr))->index;
Axel Lin6579d582014-07-02 20:36:50 +08001972 struct lm93_data *data = dev_get_drvdata(dev);
1973 struct i2c_client *client = data->client;
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02001974 u8 ctl3, ctl4;
Guenter Roeck2804a4cf2012-01-14 21:25:54 -08001975 unsigned long val;
1976 int err;
1977
1978 err = kstrtoul(buf, 10, &val);
1979 if (err)
1980 return err;
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02001981
1982 mutex_lock(&data->update_lock);
Guenter Roeck2804a4cf2012-01-14 21:25:54 -08001983 ctl3 = lm93_read_byte(client, LM93_REG_PWM_CTL(nr, LM93_PWM_CTL3));
1984 ctl4 = lm93_read_byte(client, LM93_REG_PWM_CTL(nr, LM93_PWM_CTL4));
1985 ctl3 = (ctl3 & 0xf0) | LM93_PWM_TO_REG(val, (ctl4 & 0x07) ?
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02001986 LM93_PWM_MAP_LO_FREQ :
1987 LM93_PWM_MAP_HI_FREQ);
1988 data->block9[nr][LM93_PWM_CTL3] = ctl3;
Guenter Roeck2804a4cf2012-01-14 21:25:54 -08001989 lm93_write_byte(client, LM93_REG_PWM_CTL(nr, LM93_PWM_CTL3), ctl3);
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02001990 mutex_unlock(&data->update_lock);
1991 return count;
1992}
1993
Guenter Roeck7f9d0582018-12-10 14:02:13 -08001994static SENSOR_DEVICE_ATTR_RW(pwm1_auto_spinup_min, pwm_auto_spinup_min, 0);
1995static SENSOR_DEVICE_ATTR_RW(pwm2_auto_spinup_min, pwm_auto_spinup_min, 1);
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02001996
Guenter Roeck7f9d0582018-12-10 14:02:13 -08001997static ssize_t pwm_auto_spinup_time_show(struct device *dev,
1998 struct device_attribute *attr,
1999 char *buf)
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02002000{
2001 int nr = (to_sensor_dev_attr(attr))->index;
2002 struct lm93_data *data = lm93_update_device(dev);
Guenter Roeck2804a4cf2012-01-14 21:25:54 -08002003 return sprintf(buf, "%d\n", LM93_SPINUP_TIME_FROM_REG(
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02002004 data->block9[nr][LM93_PWM_CTL3]));
2005}
2006
Guenter Roeck7f9d0582018-12-10 14:02:13 -08002007static ssize_t pwm_auto_spinup_time_store(struct device *dev,
2008 struct device_attribute *attr,
2009 const char *buf, size_t count)
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02002010{
2011 int nr = (to_sensor_dev_attr(attr))->index;
Axel Lin6579d582014-07-02 20:36:50 +08002012 struct lm93_data *data = dev_get_drvdata(dev);
2013 struct i2c_client *client = data->client;
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02002014 u8 ctl3;
Guenter Roeck2804a4cf2012-01-14 21:25:54 -08002015 unsigned long val;
2016 int err;
2017
2018 err = kstrtoul(buf, 10, &val);
2019 if (err)
2020 return err;
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02002021
2022 mutex_lock(&data->update_lock);
Guenter Roeck2804a4cf2012-01-14 21:25:54 -08002023 ctl3 = lm93_read_byte(client, LM93_REG_PWM_CTL(nr, LM93_PWM_CTL3));
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02002024 ctl3 = (ctl3 & 0x1f) | (LM93_SPINUP_TIME_TO_REG(val) << 5 & 0xe0);
2025 data->block9[nr][LM93_PWM_CTL3] = ctl3;
Guenter Roeck2804a4cf2012-01-14 21:25:54 -08002026 lm93_write_byte(client, LM93_REG_PWM_CTL(nr, LM93_PWM_CTL3), ctl3);
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02002027 mutex_unlock(&data->update_lock);
2028 return count;
2029}
2030
Guenter Roeck7f9d0582018-12-10 14:02:13 -08002031static SENSOR_DEVICE_ATTR_RW(pwm1_auto_spinup_time, pwm_auto_spinup_time, 0);
2032static SENSOR_DEVICE_ATTR_RW(pwm2_auto_spinup_time, pwm_auto_spinup_time, 1);
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02002033
Julia Lawallce847a32016-12-22 13:04:55 +01002034static ssize_t pwm_auto_prochot_ramp_show(struct device *dev,
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02002035 struct device_attribute *attr, char *buf)
2036{
2037 struct lm93_data *data = lm93_update_device(dev);
Guenter Roeck2804a4cf2012-01-14 21:25:54 -08002038 return sprintf(buf, "%d\n",
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02002039 LM93_RAMP_FROM_REG(data->pwm_ramp_ctl >> 4 & 0x0f));
2040}
2041
Julia Lawallce847a32016-12-22 13:04:55 +01002042static ssize_t pwm_auto_prochot_ramp_store(struct device *dev,
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02002043 struct device_attribute *attr,
2044 const char *buf, size_t count)
2045{
Axel Lin6579d582014-07-02 20:36:50 +08002046 struct lm93_data *data = dev_get_drvdata(dev);
2047 struct i2c_client *client = data->client;
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02002048 u8 ramp;
Guenter Roeck2804a4cf2012-01-14 21:25:54 -08002049 unsigned long val;
2050 int err;
2051
2052 err = kstrtoul(buf, 10, &val);
2053 if (err)
2054 return err;
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02002055
2056 mutex_lock(&data->update_lock);
2057 ramp = lm93_read_byte(client, LM93_REG_PWM_RAMP_CTL);
2058 ramp = (ramp & 0x0f) | (LM93_RAMP_TO_REG(val) << 4 & 0xf0);
2059 lm93_write_byte(client, LM93_REG_PWM_RAMP_CTL, ramp);
2060 mutex_unlock(&data->update_lock);
2061 return count;
2062}
2063
Julia Lawallce847a32016-12-22 13:04:55 +01002064static DEVICE_ATTR_RW(pwm_auto_prochot_ramp);
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02002065
Julia Lawallce847a32016-12-22 13:04:55 +01002066static ssize_t pwm_auto_vrdhot_ramp_show(struct device *dev,
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02002067 struct device_attribute *attr, char *buf)
2068{
2069 struct lm93_data *data = lm93_update_device(dev);
Guenter Roeck2804a4cf2012-01-14 21:25:54 -08002070 return sprintf(buf, "%d\n",
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02002071 LM93_RAMP_FROM_REG(data->pwm_ramp_ctl & 0x0f));
2072}
2073
Julia Lawallce847a32016-12-22 13:04:55 +01002074static ssize_t pwm_auto_vrdhot_ramp_store(struct device *dev,
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02002075 struct device_attribute *attr,
2076 const char *buf, size_t count)
2077{
Axel Lin6579d582014-07-02 20:36:50 +08002078 struct lm93_data *data = dev_get_drvdata(dev);
2079 struct i2c_client *client = data->client;
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02002080 u8 ramp;
Guenter Roeck2804a4cf2012-01-14 21:25:54 -08002081 unsigned long val;
2082 int err;
2083
2084 err = kstrtoul(buf, 10, &val);
2085 if (err)
2086 return err;
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02002087
2088 mutex_lock(&data->update_lock);
2089 ramp = lm93_read_byte(client, LM93_REG_PWM_RAMP_CTL);
2090 ramp = (ramp & 0xf0) | (LM93_RAMP_TO_REG(val) & 0x0f);
2091 lm93_write_byte(client, LM93_REG_PWM_RAMP_CTL, ramp);
2092 mutex_unlock(&data->update_lock);
2093 return 0;
2094}
2095
Julia Lawallce847a32016-12-22 13:04:55 +01002096static DEVICE_ATTR_RW(pwm_auto_vrdhot_ramp);
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02002097
Guenter Roeck7f9d0582018-12-10 14:02:13 -08002098static ssize_t vid_show(struct device *dev, struct device_attribute *attr,
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02002099 char *buf)
2100{
2101 int nr = (to_sensor_dev_attr(attr))->index;
2102 struct lm93_data *data = lm93_update_device(dev);
Guenter Roeck2804a4cf2012-01-14 21:25:54 -08002103 return sprintf(buf, "%d\n", LM93_VID_FROM_REG(data->vid[nr]));
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02002104}
2105
Guenter Roeck7f9d0582018-12-10 14:02:13 -08002106static SENSOR_DEVICE_ATTR_RO(cpu0_vid, vid, 0);
2107static SENSOR_DEVICE_ATTR_RO(cpu1_vid, vid, 1);
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02002108
Guenter Roeck7f9d0582018-12-10 14:02:13 -08002109static ssize_t prochot_show(struct device *dev, struct device_attribute *attr,
2110 char *buf)
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02002111{
2112 int nr = (to_sensor_dev_attr(attr))->index;
2113 struct lm93_data *data = lm93_update_device(dev);
Guenter Roeck2804a4cf2012-01-14 21:25:54 -08002114 return sprintf(buf, "%d\n", data->block4[nr].cur);
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02002115}
2116
Guenter Roeck7f9d0582018-12-10 14:02:13 -08002117static SENSOR_DEVICE_ATTR_RO(prochot1, prochot, 0);
2118static SENSOR_DEVICE_ATTR_RO(prochot2, prochot, 1);
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02002119
Guenter Roeck7f9d0582018-12-10 14:02:13 -08002120static ssize_t prochot_avg_show(struct device *dev,
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02002121 struct device_attribute *attr, char *buf)
2122{
2123 int nr = (to_sensor_dev_attr(attr))->index;
2124 struct lm93_data *data = lm93_update_device(dev);
Guenter Roeck2804a4cf2012-01-14 21:25:54 -08002125 return sprintf(buf, "%d\n", data->block4[nr].avg);
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02002126}
2127
Guenter Roeck7f9d0582018-12-10 14:02:13 -08002128static SENSOR_DEVICE_ATTR_RO(prochot1_avg, prochot_avg, 0);
2129static SENSOR_DEVICE_ATTR_RO(prochot2_avg, prochot_avg, 1);
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02002130
Guenter Roeck7f9d0582018-12-10 14:02:13 -08002131static ssize_t prochot_max_show(struct device *dev,
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02002132 struct device_attribute *attr, char *buf)
2133{
2134 int nr = (to_sensor_dev_attr(attr))->index;
2135 struct lm93_data *data = lm93_update_device(dev);
Guenter Roeck2804a4cf2012-01-14 21:25:54 -08002136 return sprintf(buf, "%d\n", data->prochot_max[nr]);
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02002137}
2138
Guenter Roeck7f9d0582018-12-10 14:02:13 -08002139static ssize_t prochot_max_store(struct device *dev,
2140 struct device_attribute *attr,
2141 const char *buf, size_t count)
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02002142{
2143 int nr = (to_sensor_dev_attr(attr))->index;
Axel Lin6579d582014-07-02 20:36:50 +08002144 struct lm93_data *data = dev_get_drvdata(dev);
2145 struct i2c_client *client = data->client;
Guenter Roeck2804a4cf2012-01-14 21:25:54 -08002146 unsigned long val;
2147 int err;
2148
2149 err = kstrtoul(buf, 10, &val);
2150 if (err)
2151 return err;
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02002152
2153 mutex_lock(&data->update_lock);
2154 data->prochot_max[nr] = LM93_PROCHOT_TO_REG(val);
2155 lm93_write_byte(client, LM93_REG_PROCHOT_MAX(nr),
2156 data->prochot_max[nr]);
2157 mutex_unlock(&data->update_lock);
2158 return count;
2159}
2160
Guenter Roeck7f9d0582018-12-10 14:02:13 -08002161static SENSOR_DEVICE_ATTR_RW(prochot1_max, prochot_max, 0);
2162static SENSOR_DEVICE_ATTR_RW(prochot2_max, prochot_max, 1);
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02002163
2164static const u8 prochot_override_mask[] = { 0x80, 0x40 };
2165
Guenter Roeck7f9d0582018-12-10 14:02:13 -08002166static ssize_t prochot_override_show(struct device *dev,
2167 struct device_attribute *attr, char *buf)
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02002168{
2169 int nr = (to_sensor_dev_attr(attr))->index;
2170 struct lm93_data *data = lm93_update_device(dev);
Guenter Roeck2804a4cf2012-01-14 21:25:54 -08002171 return sprintf(buf, "%d\n",
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02002172 (data->prochot_override & prochot_override_mask[nr]) ? 1 : 0);
2173}
2174
Guenter Roeck7f9d0582018-12-10 14:02:13 -08002175static ssize_t prochot_override_store(struct device *dev,
2176 struct device_attribute *attr,
2177 const char *buf, size_t count)
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02002178{
2179 int nr = (to_sensor_dev_attr(attr))->index;
Axel Lin6579d582014-07-02 20:36:50 +08002180 struct lm93_data *data = dev_get_drvdata(dev);
2181 struct i2c_client *client = data->client;
Guenter Roeck2804a4cf2012-01-14 21:25:54 -08002182 unsigned long val;
2183 int err;
2184
2185 err = kstrtoul(buf, 10, &val);
2186 if (err)
2187 return err;
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02002188
2189 mutex_lock(&data->update_lock);
2190 if (val)
2191 data->prochot_override |= prochot_override_mask[nr];
2192 else
2193 data->prochot_override &= (~prochot_override_mask[nr]);
2194 lm93_write_byte(client, LM93_REG_PROCHOT_OVERRIDE,
2195 data->prochot_override);
2196 mutex_unlock(&data->update_lock);
2197 return count;
2198}
2199
Guenter Roeck7f9d0582018-12-10 14:02:13 -08002200static SENSOR_DEVICE_ATTR_RW(prochot1_override, prochot_override, 0);
2201static SENSOR_DEVICE_ATTR_RW(prochot2_override, prochot_override, 1);
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02002202
Guenter Roeck7f9d0582018-12-10 14:02:13 -08002203static ssize_t prochot_interval_show(struct device *dev,
2204 struct device_attribute *attr, char *buf)
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02002205{
2206 int nr = (to_sensor_dev_attr(attr))->index;
2207 struct lm93_data *data = lm93_update_device(dev);
2208 u8 tmp;
Guenter Roeck2804a4cf2012-01-14 21:25:54 -08002209 if (nr == 1)
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02002210 tmp = (data->prochot_interval & 0xf0) >> 4;
2211 else
2212 tmp = data->prochot_interval & 0x0f;
Guenter Roeck2804a4cf2012-01-14 21:25:54 -08002213 return sprintf(buf, "%d\n", LM93_INTERVAL_FROM_REG(tmp));
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02002214}
2215
Guenter Roeck7f9d0582018-12-10 14:02:13 -08002216static ssize_t prochot_interval_store(struct device *dev,
2217 struct device_attribute *attr,
2218 const char *buf, size_t count)
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02002219{
2220 int nr = (to_sensor_dev_attr(attr))->index;
Axel Lin6579d582014-07-02 20:36:50 +08002221 struct lm93_data *data = dev_get_drvdata(dev);
2222 struct i2c_client *client = data->client;
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02002223 u8 tmp;
Guenter Roeck2804a4cf2012-01-14 21:25:54 -08002224 unsigned long val;
2225 int err;
2226
2227 err = kstrtoul(buf, 10, &val);
2228 if (err)
2229 return err;
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02002230
2231 mutex_lock(&data->update_lock);
2232 tmp = lm93_read_byte(client, LM93_REG_PROCHOT_INTERVAL);
Guenter Roeck2804a4cf2012-01-14 21:25:54 -08002233 if (nr == 1)
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02002234 tmp = (tmp & 0x0f) | (LM93_INTERVAL_TO_REG(val) << 4);
2235 else
2236 tmp = (tmp & 0xf0) | LM93_INTERVAL_TO_REG(val);
2237 data->prochot_interval = tmp;
2238 lm93_write_byte(client, LM93_REG_PROCHOT_INTERVAL, tmp);
2239 mutex_unlock(&data->update_lock);
2240 return count;
2241}
2242
Guenter Roeck7f9d0582018-12-10 14:02:13 -08002243static SENSOR_DEVICE_ATTR_RW(prochot1_interval, prochot_interval, 0);
2244static SENSOR_DEVICE_ATTR_RW(prochot2_interval, prochot_interval, 1);
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02002245
Julia Lawallce847a32016-12-22 13:04:55 +01002246static ssize_t prochot_override_duty_cycle_show(struct device *dev,
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02002247 struct device_attribute *attr,
2248 char *buf)
2249{
2250 struct lm93_data *data = lm93_update_device(dev);
Guenter Roeck2804a4cf2012-01-14 21:25:54 -08002251 return sprintf(buf, "%d\n", data->prochot_override & 0x0f);
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02002252}
2253
Julia Lawallce847a32016-12-22 13:04:55 +01002254static ssize_t prochot_override_duty_cycle_store(struct device *dev,
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02002255 struct device_attribute *attr,
2256 const char *buf, size_t count)
2257{
Axel Lin6579d582014-07-02 20:36:50 +08002258 struct lm93_data *data = dev_get_drvdata(dev);
2259 struct i2c_client *client = data->client;
Guenter Roeck2804a4cf2012-01-14 21:25:54 -08002260 unsigned long val;
2261 int err;
2262
2263 err = kstrtoul(buf, 10, &val);
2264 if (err)
2265 return err;
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02002266
2267 mutex_lock(&data->update_lock);
2268 data->prochot_override = (data->prochot_override & 0xf0) |
Guenter Roeck2a844c12013-01-09 08:09:34 -08002269 clamp_val(val, 0, 15);
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02002270 lm93_write_byte(client, LM93_REG_PROCHOT_OVERRIDE,
2271 data->prochot_override);
2272 mutex_unlock(&data->update_lock);
2273 return count;
2274}
2275
Julia Lawallce847a32016-12-22 13:04:55 +01002276static DEVICE_ATTR_RW(prochot_override_duty_cycle);
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02002277
Julia Lawallce847a32016-12-22 13:04:55 +01002278static ssize_t prochot_short_show(struct device *dev,
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02002279 struct device_attribute *attr, char *buf)
2280{
2281 struct lm93_data *data = lm93_update_device(dev);
Guenter Roeck2804a4cf2012-01-14 21:25:54 -08002282 return sprintf(buf, "%d\n", (data->config & 0x10) ? 1 : 0);
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02002283}
2284
Julia Lawallce847a32016-12-22 13:04:55 +01002285static ssize_t prochot_short_store(struct device *dev,
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02002286 struct device_attribute *attr,
2287 const char *buf, size_t count)
2288{
Axel Lin6579d582014-07-02 20:36:50 +08002289 struct lm93_data *data = dev_get_drvdata(dev);
2290 struct i2c_client *client = data->client;
Guenter Roeck2804a4cf2012-01-14 21:25:54 -08002291 unsigned long val;
2292 int err;
2293
2294 err = kstrtoul(buf, 10, &val);
2295 if (err)
2296 return err;
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02002297
2298 mutex_lock(&data->update_lock);
2299 if (val)
2300 data->config |= 0x10;
2301 else
2302 data->config &= ~0x10;
2303 lm93_write_byte(client, LM93_REG_CONFIG, data->config);
2304 mutex_unlock(&data->update_lock);
2305 return count;
2306}
2307
Julia Lawallce847a32016-12-22 13:04:55 +01002308static DEVICE_ATTR_RW(prochot_short);
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02002309
Guenter Roeck7f9d0582018-12-10 14:02:13 -08002310static ssize_t vrdhot_show(struct device *dev, struct device_attribute *attr,
2311 char *buf)
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02002312{
2313 int nr = (to_sensor_dev_attr(attr))->index;
2314 struct lm93_data *data = lm93_update_device(dev);
Guenter Roeck2804a4cf2012-01-14 21:25:54 -08002315 return sprintf(buf, "%d\n",
2316 data->block1.host_status_1 & (1 << (nr + 4)) ? 1 : 0);
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02002317}
2318
Guenter Roeck7f9d0582018-12-10 14:02:13 -08002319static SENSOR_DEVICE_ATTR_RO(vrdhot1, vrdhot, 0);
2320static SENSOR_DEVICE_ATTR_RO(vrdhot2, vrdhot, 1);
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02002321
Julia Lawallce847a32016-12-22 13:04:55 +01002322static ssize_t gpio_show(struct device *dev, struct device_attribute *attr,
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02002323 char *buf)
2324{
2325 struct lm93_data *data = lm93_update_device(dev);
Guenter Roeck2804a4cf2012-01-14 21:25:54 -08002326 return sprintf(buf, "%d\n", LM93_GPI_FROM_REG(data->gpi));
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02002327}
2328
Julia Lawallce847a32016-12-22 13:04:55 +01002329static DEVICE_ATTR_RO(gpio);
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02002330
Julia Lawallce847a32016-12-22 13:04:55 +01002331static ssize_t alarms_show(struct device *dev, struct device_attribute *attr,
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02002332 char *buf)
2333{
2334 struct lm93_data *data = lm93_update_device(dev);
Guenter Roeck2804a4cf2012-01-14 21:25:54 -08002335 return sprintf(buf, "%d\n", LM93_ALARMS_FROM_REG(data->block1));
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02002336}
2337
Julia Lawallce847a32016-12-22 13:04:55 +01002338static DEVICE_ATTR_RO(alarms);
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02002339
2340static struct attribute *lm93_attrs[] = {
2341 &sensor_dev_attr_in1_input.dev_attr.attr,
2342 &sensor_dev_attr_in2_input.dev_attr.attr,
2343 &sensor_dev_attr_in3_input.dev_attr.attr,
2344 &sensor_dev_attr_in4_input.dev_attr.attr,
2345 &sensor_dev_attr_in5_input.dev_attr.attr,
2346 &sensor_dev_attr_in6_input.dev_attr.attr,
2347 &sensor_dev_attr_in7_input.dev_attr.attr,
2348 &sensor_dev_attr_in8_input.dev_attr.attr,
2349 &sensor_dev_attr_in9_input.dev_attr.attr,
2350 &sensor_dev_attr_in10_input.dev_attr.attr,
2351 &sensor_dev_attr_in11_input.dev_attr.attr,
2352 &sensor_dev_attr_in12_input.dev_attr.attr,
2353 &sensor_dev_attr_in13_input.dev_attr.attr,
2354 &sensor_dev_attr_in14_input.dev_attr.attr,
2355 &sensor_dev_attr_in15_input.dev_attr.attr,
2356 &sensor_dev_attr_in16_input.dev_attr.attr,
2357 &sensor_dev_attr_in1_min.dev_attr.attr,
2358 &sensor_dev_attr_in2_min.dev_attr.attr,
2359 &sensor_dev_attr_in3_min.dev_attr.attr,
2360 &sensor_dev_attr_in4_min.dev_attr.attr,
2361 &sensor_dev_attr_in5_min.dev_attr.attr,
2362 &sensor_dev_attr_in6_min.dev_attr.attr,
2363 &sensor_dev_attr_in7_min.dev_attr.attr,
2364 &sensor_dev_attr_in8_min.dev_attr.attr,
2365 &sensor_dev_attr_in9_min.dev_attr.attr,
2366 &sensor_dev_attr_in10_min.dev_attr.attr,
2367 &sensor_dev_attr_in11_min.dev_attr.attr,
2368 &sensor_dev_attr_in12_min.dev_attr.attr,
2369 &sensor_dev_attr_in13_min.dev_attr.attr,
2370 &sensor_dev_attr_in14_min.dev_attr.attr,
2371 &sensor_dev_attr_in15_min.dev_attr.attr,
2372 &sensor_dev_attr_in16_min.dev_attr.attr,
2373 &sensor_dev_attr_in1_max.dev_attr.attr,
2374 &sensor_dev_attr_in2_max.dev_attr.attr,
2375 &sensor_dev_attr_in3_max.dev_attr.attr,
2376 &sensor_dev_attr_in4_max.dev_attr.attr,
2377 &sensor_dev_attr_in5_max.dev_attr.attr,
2378 &sensor_dev_attr_in6_max.dev_attr.attr,
2379 &sensor_dev_attr_in7_max.dev_attr.attr,
2380 &sensor_dev_attr_in8_max.dev_attr.attr,
2381 &sensor_dev_attr_in9_max.dev_attr.attr,
2382 &sensor_dev_attr_in10_max.dev_attr.attr,
2383 &sensor_dev_attr_in11_max.dev_attr.attr,
2384 &sensor_dev_attr_in12_max.dev_attr.attr,
2385 &sensor_dev_attr_in13_max.dev_attr.attr,
2386 &sensor_dev_attr_in14_max.dev_attr.attr,
2387 &sensor_dev_attr_in15_max.dev_attr.attr,
2388 &sensor_dev_attr_in16_max.dev_attr.attr,
2389 &sensor_dev_attr_temp1_input.dev_attr.attr,
2390 &sensor_dev_attr_temp2_input.dev_attr.attr,
2391 &sensor_dev_attr_temp3_input.dev_attr.attr,
2392 &sensor_dev_attr_temp1_min.dev_attr.attr,
2393 &sensor_dev_attr_temp2_min.dev_attr.attr,
2394 &sensor_dev_attr_temp3_min.dev_attr.attr,
2395 &sensor_dev_attr_temp1_max.dev_attr.attr,
2396 &sensor_dev_attr_temp2_max.dev_attr.attr,
2397 &sensor_dev_attr_temp3_max.dev_attr.attr,
2398 &sensor_dev_attr_temp1_auto_base.dev_attr.attr,
2399 &sensor_dev_attr_temp2_auto_base.dev_attr.attr,
2400 &sensor_dev_attr_temp3_auto_base.dev_attr.attr,
2401 &sensor_dev_attr_temp1_auto_boost.dev_attr.attr,
2402 &sensor_dev_attr_temp2_auto_boost.dev_attr.attr,
2403 &sensor_dev_attr_temp3_auto_boost.dev_attr.attr,
2404 &sensor_dev_attr_temp1_auto_boost_hyst.dev_attr.attr,
2405 &sensor_dev_attr_temp2_auto_boost_hyst.dev_attr.attr,
2406 &sensor_dev_attr_temp3_auto_boost_hyst.dev_attr.attr,
2407 &sensor_dev_attr_temp1_auto_offset1.dev_attr.attr,
2408 &sensor_dev_attr_temp1_auto_offset2.dev_attr.attr,
2409 &sensor_dev_attr_temp1_auto_offset3.dev_attr.attr,
2410 &sensor_dev_attr_temp1_auto_offset4.dev_attr.attr,
2411 &sensor_dev_attr_temp1_auto_offset5.dev_attr.attr,
2412 &sensor_dev_attr_temp1_auto_offset6.dev_attr.attr,
2413 &sensor_dev_attr_temp1_auto_offset7.dev_attr.attr,
2414 &sensor_dev_attr_temp1_auto_offset8.dev_attr.attr,
2415 &sensor_dev_attr_temp1_auto_offset9.dev_attr.attr,
2416 &sensor_dev_attr_temp1_auto_offset10.dev_attr.attr,
2417 &sensor_dev_attr_temp1_auto_offset11.dev_attr.attr,
2418 &sensor_dev_attr_temp1_auto_offset12.dev_attr.attr,
2419 &sensor_dev_attr_temp2_auto_offset1.dev_attr.attr,
2420 &sensor_dev_attr_temp2_auto_offset2.dev_attr.attr,
2421 &sensor_dev_attr_temp2_auto_offset3.dev_attr.attr,
2422 &sensor_dev_attr_temp2_auto_offset4.dev_attr.attr,
2423 &sensor_dev_attr_temp2_auto_offset5.dev_attr.attr,
2424 &sensor_dev_attr_temp2_auto_offset6.dev_attr.attr,
2425 &sensor_dev_attr_temp2_auto_offset7.dev_attr.attr,
2426 &sensor_dev_attr_temp2_auto_offset8.dev_attr.attr,
2427 &sensor_dev_attr_temp2_auto_offset9.dev_attr.attr,
2428 &sensor_dev_attr_temp2_auto_offset10.dev_attr.attr,
2429 &sensor_dev_attr_temp2_auto_offset11.dev_attr.attr,
2430 &sensor_dev_attr_temp2_auto_offset12.dev_attr.attr,
2431 &sensor_dev_attr_temp3_auto_offset1.dev_attr.attr,
2432 &sensor_dev_attr_temp3_auto_offset2.dev_attr.attr,
2433 &sensor_dev_attr_temp3_auto_offset3.dev_attr.attr,
2434 &sensor_dev_attr_temp3_auto_offset4.dev_attr.attr,
2435 &sensor_dev_attr_temp3_auto_offset5.dev_attr.attr,
2436 &sensor_dev_attr_temp3_auto_offset6.dev_attr.attr,
2437 &sensor_dev_attr_temp3_auto_offset7.dev_attr.attr,
2438 &sensor_dev_attr_temp3_auto_offset8.dev_attr.attr,
2439 &sensor_dev_attr_temp3_auto_offset9.dev_attr.attr,
2440 &sensor_dev_attr_temp3_auto_offset10.dev_attr.attr,
2441 &sensor_dev_attr_temp3_auto_offset11.dev_attr.attr,
2442 &sensor_dev_attr_temp3_auto_offset12.dev_attr.attr,
2443 &sensor_dev_attr_temp1_auto_pwm_min.dev_attr.attr,
2444 &sensor_dev_attr_temp2_auto_pwm_min.dev_attr.attr,
2445 &sensor_dev_attr_temp3_auto_pwm_min.dev_attr.attr,
2446 &sensor_dev_attr_temp1_auto_offset_hyst.dev_attr.attr,
2447 &sensor_dev_attr_temp2_auto_offset_hyst.dev_attr.attr,
2448 &sensor_dev_attr_temp3_auto_offset_hyst.dev_attr.attr,
2449 &sensor_dev_attr_fan1_input.dev_attr.attr,
2450 &sensor_dev_attr_fan2_input.dev_attr.attr,
2451 &sensor_dev_attr_fan3_input.dev_attr.attr,
2452 &sensor_dev_attr_fan4_input.dev_attr.attr,
2453 &sensor_dev_attr_fan1_min.dev_attr.attr,
2454 &sensor_dev_attr_fan2_min.dev_attr.attr,
2455 &sensor_dev_attr_fan3_min.dev_attr.attr,
2456 &sensor_dev_attr_fan4_min.dev_attr.attr,
2457 &sensor_dev_attr_fan1_smart_tach.dev_attr.attr,
2458 &sensor_dev_attr_fan2_smart_tach.dev_attr.attr,
2459 &sensor_dev_attr_fan3_smart_tach.dev_attr.attr,
2460 &sensor_dev_attr_fan4_smart_tach.dev_attr.attr,
2461 &sensor_dev_attr_pwm1.dev_attr.attr,
2462 &sensor_dev_attr_pwm2.dev_attr.attr,
2463 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
2464 &sensor_dev_attr_pwm2_enable.dev_attr.attr,
2465 &sensor_dev_attr_pwm1_freq.dev_attr.attr,
2466 &sensor_dev_attr_pwm2_freq.dev_attr.attr,
2467 &sensor_dev_attr_pwm1_auto_channels.dev_attr.attr,
2468 &sensor_dev_attr_pwm2_auto_channels.dev_attr.attr,
2469 &sensor_dev_attr_pwm1_auto_spinup_min.dev_attr.attr,
2470 &sensor_dev_attr_pwm2_auto_spinup_min.dev_attr.attr,
2471 &sensor_dev_attr_pwm1_auto_spinup_time.dev_attr.attr,
2472 &sensor_dev_attr_pwm2_auto_spinup_time.dev_attr.attr,
2473 &dev_attr_pwm_auto_prochot_ramp.attr,
2474 &dev_attr_pwm_auto_vrdhot_ramp.attr,
Jean Delvaree7f62302007-09-04 14:03:43 +02002475 &sensor_dev_attr_cpu0_vid.dev_attr.attr,
2476 &sensor_dev_attr_cpu1_vid.dev_attr.attr,
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02002477 &sensor_dev_attr_prochot1.dev_attr.attr,
2478 &sensor_dev_attr_prochot2.dev_attr.attr,
2479 &sensor_dev_attr_prochot1_avg.dev_attr.attr,
2480 &sensor_dev_attr_prochot2_avg.dev_attr.attr,
2481 &sensor_dev_attr_prochot1_max.dev_attr.attr,
2482 &sensor_dev_attr_prochot2_max.dev_attr.attr,
2483 &sensor_dev_attr_prochot1_override.dev_attr.attr,
2484 &sensor_dev_attr_prochot2_override.dev_attr.attr,
2485 &sensor_dev_attr_prochot1_interval.dev_attr.attr,
2486 &sensor_dev_attr_prochot2_interval.dev_attr.attr,
2487 &dev_attr_prochot_override_duty_cycle.attr,
2488 &dev_attr_prochot_short.attr,
2489 &sensor_dev_attr_vrdhot1.dev_attr.attr,
2490 &sensor_dev_attr_vrdhot2.dev_attr.attr,
2491 &dev_attr_gpio.attr,
2492 &dev_attr_alarms.attr,
2493 NULL
2494};
2495
Axel Lin6579d582014-07-02 20:36:50 +08002496ATTRIBUTE_GROUPS(lm93);
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02002497
2498static void lm93_init_client(struct i2c_client *client)
2499{
2500 int i;
2501 u8 reg;
2502
2503 /* configure VID pin input thresholds */
2504 reg = lm93_read_byte(client, LM93_REG_GPI_VID_CTL);
2505 lm93_write_byte(client, LM93_REG_GPI_VID_CTL,
2506 reg | (vid_agtl ? 0x03 : 0x00));
2507
2508 if (init) {
2509 /* enable #ALERT pin */
2510 reg = lm93_read_byte(client, LM93_REG_CONFIG);
2511 lm93_write_byte(client, LM93_REG_CONFIG, reg | 0x08);
2512
2513 /* enable ASF mode for BMC status registers */
2514 reg = lm93_read_byte(client, LM93_REG_STATUS_CONTROL);
2515 lm93_write_byte(client, LM93_REG_STATUS_CONTROL, reg | 0x02);
2516
2517 /* set sleep state to S0 */
2518 lm93_write_byte(client, LM93_REG_SLEEP_CONTROL, 0);
2519
2520 /* unmask #VRDHOT and dynamic VCCP (if nec) error events */
2521 reg = lm93_read_byte(client, LM93_REG_MISC_ERR_MASK);
2522 reg &= ~0x03;
2523 reg &= ~(vccp_limit_type[0] ? 0x10 : 0);
2524 reg &= ~(vccp_limit_type[1] ? 0x20 : 0);
2525 lm93_write_byte(client, LM93_REG_MISC_ERR_MASK, reg);
2526 }
2527
2528 /* start monitoring */
2529 reg = lm93_read_byte(client, LM93_REG_CONFIG);
2530 lm93_write_byte(client, LM93_REG_CONFIG, reg | 0x01);
2531
2532 /* spin until ready */
Guenter Roeck2804a4cf2012-01-14 21:25:54 -08002533 for (i = 0; i < 20; i++) {
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02002534 msleep(10);
2535 if ((lm93_read_byte(client, LM93_REG_CONFIG) & 0x80) == 0x80)
2536 return;
2537 }
2538
Guenter Roeckb55f3752013-01-10 10:01:24 -08002539 dev_warn(&client->dev,
2540 "timed out waiting for sensor chip to signal ready!\n");
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02002541}
2542
Jean Delvare70b72402008-07-16 19:30:15 +02002543/* Return 0 if detection is successful, -ENODEV otherwise */
Jean Delvare310ec792009-12-14 21:17:23 +01002544static int lm93_detect(struct i2c_client *client, struct i2c_board_info *info)
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02002545{
Jean Delvare70b72402008-07-16 19:30:15 +02002546 struct i2c_adapter *adapter = client->adapter;
Jean Delvare52df6442009-12-09 20:35:57 +01002547 int mfr, ver;
Guenter Roeckc7bf71c2011-01-17 12:48:20 -08002548 const char *name;
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02002549
Jean Delvare70b72402008-07-16 19:30:15 +02002550 if (!i2c_check_functionality(adapter, LM93_SMBUS_FUNC_MIN))
2551 return -ENODEV;
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02002552
2553 /* detection */
Jean Delvare52df6442009-12-09 20:35:57 +01002554 mfr = lm93_read_byte(client, LM93_REG_MFR_ID);
2555 if (mfr != 0x01) {
2556 dev_dbg(&adapter->dev,
2557 "detect failed, bad manufacturer id 0x%02x!\n", mfr);
2558 return -ENODEV;
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02002559 }
2560
Jean Delvare52df6442009-12-09 20:35:57 +01002561 ver = lm93_read_byte(client, LM93_REG_VER);
Guenter Roeckc7bf71c2011-01-17 12:48:20 -08002562 switch (ver) {
2563 case LM93_MFR_ID:
2564 case LM93_MFR_ID_PROTOTYPE:
2565 name = "lm93";
2566 break;
2567 case LM94_MFR_ID_2:
2568 case LM94_MFR_ID:
2569 case LM94_MFR_ID_PROTOTYPE:
2570 name = "lm94";
2571 break;
2572 default:
Jean Delvare52df6442009-12-09 20:35:57 +01002573 dev_dbg(&adapter->dev,
2574 "detect failed, bad version id 0x%02x!\n", ver);
2575 return -ENODEV;
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02002576 }
2577
Guenter Roeckc7bf71c2011-01-17 12:48:20 -08002578 strlcpy(info->type, name, I2C_NAME_SIZE);
Guenter Roeck2804a4cf2012-01-14 21:25:54 -08002579 dev_dbg(&adapter->dev, "loading %s at %d, 0x%02x\n",
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02002580 client->name, i2c_adapter_id(client->adapter),
2581 client->addr);
2582
Jean Delvare70b72402008-07-16 19:30:15 +02002583 return 0;
2584}
2585
Stephen Kitt67487032020-08-13 18:02:22 +02002586static int lm93_probe(struct i2c_client *client)
Jean Delvare70b72402008-07-16 19:30:15 +02002587{
Axel Lin6579d582014-07-02 20:36:50 +08002588 struct device *dev = &client->dev;
Jean Delvare70b72402008-07-16 19:30:15 +02002589 struct lm93_data *data;
Axel Lin6579d582014-07-02 20:36:50 +08002590 struct device *hwmon_dev;
2591 int func;
Jean Delvare70b72402008-07-16 19:30:15 +02002592 void (*update)(struct lm93_data *, struct i2c_client *);
2593
2594 /* choose update routine based on bus capabilities */
2595 func = i2c_get_functionality(client->adapter);
2596 if (((LM93_SMBUS_FUNC_FULL & func) == LM93_SMBUS_FUNC_FULL) &&
2597 (!disable_block)) {
Axel Lin6579d582014-07-02 20:36:50 +08002598 dev_dbg(dev, "using SMBus block data transactions\n");
Jean Delvare70b72402008-07-16 19:30:15 +02002599 update = lm93_update_client_full;
2600 } else if ((LM93_SMBUS_FUNC_MIN & func) == LM93_SMBUS_FUNC_MIN) {
Axel Lin6579d582014-07-02 20:36:50 +08002601 dev_dbg(dev, "disabled SMBus block data transactions\n");
Jean Delvare70b72402008-07-16 19:30:15 +02002602 update = lm93_update_client_min;
2603 } else {
Axel Lin6579d582014-07-02 20:36:50 +08002604 dev_dbg(dev, "detect failed, smbus byte and/or word data not supported!\n");
Guenter Roeck4a8ad252012-06-02 09:58:11 -07002605 return -ENODEV;
Jean Delvare70b72402008-07-16 19:30:15 +02002606 }
2607
Axel Lin6579d582014-07-02 20:36:50 +08002608 data = devm_kzalloc(dev, sizeof(struct lm93_data), GFP_KERNEL);
Jingoo Han567817d2014-04-29 17:09:19 +09002609 if (!data)
Guenter Roeck4a8ad252012-06-02 09:58:11 -07002610 return -ENOMEM;
Jean Delvare70b72402008-07-16 19:30:15 +02002611
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02002612 /* housekeeping */
Axel Lin6579d582014-07-02 20:36:50 +08002613 data->client = client;
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02002614 data->update = update;
2615 mutex_init(&data->update_lock);
2616
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02002617 /* initialize the chip */
2618 lm93_init_client(client);
2619
Axel Lin6579d582014-07-02 20:36:50 +08002620 hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
2621 data,
2622 lm93_groups);
2623 return PTR_ERR_OR_ZERO(hwmon_dev);
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02002624}
2625
Jean Delvare70b72402008-07-16 19:30:15 +02002626static const struct i2c_device_id lm93_id[] = {
Jean Delvare1f86df42009-12-14 21:17:26 +01002627 { "lm93", 0 },
Guenter Roeckc7bf71c2011-01-17 12:48:20 -08002628 { "lm94", 0 },
Jean Delvare70b72402008-07-16 19:30:15 +02002629 { }
2630};
2631MODULE_DEVICE_TABLE(i2c, lm93_id);
2632
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02002633static struct i2c_driver lm93_driver = {
Jean Delvare70b72402008-07-16 19:30:15 +02002634 .class = I2C_CLASS_HWMON,
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02002635 .driver = {
2636 .name = "lm93",
2637 },
Stephen Kitt67487032020-08-13 18:02:22 +02002638 .probe_new = lm93_probe,
Jean Delvare70b72402008-07-16 19:30:15 +02002639 .id_table = lm93_id,
2640 .detect = lm93_detect,
Jean Delvarec3813d62009-12-14 21:17:25 +01002641 .address_list = normal_i2c,
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02002642};
2643
Axel Linf0967ee2012-01-20 15:38:18 +08002644module_i2c_driver(lm93_driver);
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02002645
2646MODULE_AUTHOR("Mark M. Hoffman <mhoffman@lightlink.com>, "
Hans J. Koch2aa25c22010-11-15 21:38:56 +01002647 "Hans J. Koch <hjk@hansjkoch.de>");
Hans-Jürgen Koche46957e2007-07-05 17:58:29 +02002648MODULE_DESCRIPTION("LM93 driver");
2649MODULE_LICENSE("GPL");