blob: ca8430f925643725733014cf47f98f782b017aba [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Guenter Roeck86aa3e22012-01-14 12:51:15 -08002 * adm1026.c - Part of lm_sensors, Linux kernel modules for hardware
3 * monitoring
4 * Copyright (C) 2002, 2003 Philip Pokorny <ppokorny@penguincomputing.com>
5 * Copyright (C) 2004 Justin Thiessen <jthiessen@penguincomputing.com>
6 *
7 * Chip details at:
8 *
9 * <http://www.onsemi.com/PowerSolutions/product.do?id=ADM1026>
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/module.h>
27#include <linux/init.h>
28#include <linux/slab.h>
29#include <linux/jiffies.h>
30#include <linux/i2c.h>
Mark M. Hoffman943b0832005-07-15 21:39:18 -040031#include <linux/hwmon.h>
Jean Delvare303760b2005-07-31 21:52:01 +020032#include <linux/hwmon-sysfs.h>
33#include <linux/hwmon-vid.h>
Mark M. Hoffman943b0832005-07-15 21:39:18 -040034#include <linux/err.h>
Ingo Molnar9a61bf62006-01-18 23:19:26 +010035#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
37/* Addresses to scan */
Mark M. Hoffman25e9c862008-02-17 22:28:03 -050038static const unsigned short normal_i2c[] = { 0x2c, 0x2d, 0x2e, I2C_CLIENT_END };
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
Jean Delvarecb01a232007-11-29 23:46:42 +010040static int gpio_input[17] = { -1, -1, -1, -1, -1, -1, -1, -1, -1,
41 -1, -1, -1, -1, -1, -1, -1, -1 };
Linus Torvalds1da177e2005-04-16 15:20:36 -070042static int gpio_output[17] = { -1, -1, -1, -1, -1, -1, -1, -1, -1,
43 -1, -1, -1, -1, -1, -1, -1, -1 };
44static int gpio_inverted[17] = { -1, -1, -1, -1, -1, -1, -1, -1, -1,
45 -1, -1, -1, -1, -1, -1, -1, -1 };
46static int gpio_normal[17] = { -1, -1, -1, -1, -1, -1, -1, -1, -1,
47 -1, -1, -1, -1, -1, -1, -1, -1 };
48static int gpio_fan[8] = { -1, -1, -1, -1, -1, -1, -1, -1 };
Jean Delvarecb01a232007-11-29 23:46:42 +010049module_param_array(gpio_input, int, NULL, 0);
50MODULE_PARM_DESC(gpio_input, "List of GPIO pins (0-16) to program as inputs");
51module_param_array(gpio_output, int, NULL, 0);
Guenter Roeckb55f3752013-01-10 10:01:24 -080052MODULE_PARM_DESC(gpio_output,
53 "List of GPIO pins (0-16) to program as outputs");
Jean Delvarecb01a232007-11-29 23:46:42 +010054module_param_array(gpio_inverted, int, NULL, 0);
Guenter Roeckb55f3752013-01-10 10:01:24 -080055MODULE_PARM_DESC(gpio_inverted,
56 "List of GPIO pins (0-16) to program as inverted");
Jean Delvarecb01a232007-11-29 23:46:42 +010057module_param_array(gpio_normal, int, NULL, 0);
Guenter Roeckb55f3752013-01-10 10:01:24 -080058MODULE_PARM_DESC(gpio_normal,
59 "List of GPIO pins (0-16) to program as normal/non-inverted");
Jean Delvarecb01a232007-11-29 23:46:42 +010060module_param_array(gpio_fan, int, NULL, 0);
61MODULE_PARM_DESC(gpio_fan, "List of GPIO pins (0-7) to program as fan tachs");
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
63/* Many ADM1026 constants specified below */
64
65/* The ADM1026 registers */
Jean Delvarecb01a232007-11-29 23:46:42 +010066#define ADM1026_REG_CONFIG1 0x00
67#define CFG1_MONITOR 0x01
68#define CFG1_INT_ENABLE 0x02
69#define CFG1_INT_CLEAR 0x04
70#define CFG1_AIN8_9 0x08
71#define CFG1_THERM_HOT 0x10
72#define CFG1_DAC_AFC 0x20
73#define CFG1_PWM_AFC 0x40
74#define CFG1_RESET 0x80
75
76#define ADM1026_REG_CONFIG2 0x01
Linus Torvalds1da177e2005-04-16 15:20:36 -070077/* CONFIG2 controls FAN0/GPIO0 through FAN7/GPIO7 */
Jean Delvarecb01a232007-11-29 23:46:42 +010078
79#define ADM1026_REG_CONFIG3 0x07
80#define CFG3_GPIO16_ENABLE 0x01
81#define CFG3_CI_CLEAR 0x02
82#define CFG3_VREF_250 0x04
83#define CFG3_GPIO16_DIR 0x40
84#define CFG3_GPIO16_POL 0x80
85
86#define ADM1026_REG_E2CONFIG 0x13
87#define E2CFG_READ 0x01
88#define E2CFG_WRITE 0x02
89#define E2CFG_ERASE 0x04
90#define E2CFG_ROM 0x08
91#define E2CFG_CLK_EXT 0x80
Linus Torvalds1da177e2005-04-16 15:20:36 -070092
Guenter Roeck86aa3e22012-01-14 12:51:15 -080093/*
94 * There are 10 general analog inputs and 7 dedicated inputs
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 * They are:
96 * 0 - 9 = AIN0 - AIN9
97 * 10 = Vbat
98 * 11 = 3.3V Standby
99 * 12 = 3.3V Main
100 * 13 = +5V
101 * 14 = Vccp (CPU core voltage)
102 * 15 = +12V
103 * 16 = -12V
104 */
105static u16 ADM1026_REG_IN[] = {
106 0x30, 0x31, 0x32, 0x33, 0x34, 0x35,
107 0x36, 0x37, 0x27, 0x29, 0x26, 0x2a,
108 0x2b, 0x2c, 0x2d, 0x2e, 0x2f
109 };
110static u16 ADM1026_REG_IN_MIN[] = {
111 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d,
112 0x5e, 0x5f, 0x6d, 0x49, 0x6b, 0x4a,
113 0x4b, 0x4c, 0x4d, 0x4e, 0x4f
114 };
115static u16 ADM1026_REG_IN_MAX[] = {
116 0x50, 0x51, 0x52, 0x53, 0x54, 0x55,
117 0x56, 0x57, 0x6c, 0x41, 0x6a, 0x42,
118 0x43, 0x44, 0x45, 0x46, 0x47
119 };
120
Guenter Roeck86aa3e22012-01-14 12:51:15 -0800121/*
122 * Temperatures are:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 * 0 - Internal
124 * 1 - External 1
125 * 2 - External 2
126 */
127static u16 ADM1026_REG_TEMP[] = { 0x1f, 0x28, 0x29 };
128static u16 ADM1026_REG_TEMP_MIN[] = { 0x69, 0x48, 0x49 };
129static u16 ADM1026_REG_TEMP_MAX[] = { 0x68, 0x40, 0x41 };
130static u16 ADM1026_REG_TEMP_TMIN[] = { 0x10, 0x11, 0x12 };
131static u16 ADM1026_REG_TEMP_THERM[] = { 0x0d, 0x0e, 0x0f };
132static u16 ADM1026_REG_TEMP_OFFSET[] = { 0x1e, 0x6e, 0x6f };
133
Jean Delvarecb01a232007-11-29 23:46:42 +0100134#define ADM1026_REG_FAN(nr) (0x38 + (nr))
135#define ADM1026_REG_FAN_MIN(nr) (0x60 + (nr))
136#define ADM1026_REG_FAN_DIV_0_3 0x02
137#define ADM1026_REG_FAN_DIV_4_7 0x03
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
Jean Delvarecb01a232007-11-29 23:46:42 +0100139#define ADM1026_REG_DAC 0x04
140#define ADM1026_REG_PWM 0x05
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
Jean Delvarecb01a232007-11-29 23:46:42 +0100142#define ADM1026_REG_GPIO_CFG_0_3 0x08
143#define ADM1026_REG_GPIO_CFG_4_7 0x09
144#define ADM1026_REG_GPIO_CFG_8_11 0x0a
145#define ADM1026_REG_GPIO_CFG_12_15 0x0b
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146/* CFG_16 in REG_CFG3 */
Jean Delvarecb01a232007-11-29 23:46:42 +0100147#define ADM1026_REG_GPIO_STATUS_0_7 0x24
148#define ADM1026_REG_GPIO_STATUS_8_15 0x25
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149/* STATUS_16 in REG_STATUS4 */
Jean Delvarecb01a232007-11-29 23:46:42 +0100150#define ADM1026_REG_GPIO_MASK_0_7 0x1c
151#define ADM1026_REG_GPIO_MASK_8_15 0x1d
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152/* MASK_16 in REG_MASK4 */
153
Jean Delvarecb01a232007-11-29 23:46:42 +0100154#define ADM1026_REG_COMPANY 0x16
155#define ADM1026_REG_VERSTEP 0x17
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156/* These are the recognized values for the above regs */
Jean Delvarecb01a232007-11-29 23:46:42 +0100157#define ADM1026_COMPANY_ANALOG_DEV 0x41
158#define ADM1026_VERSTEP_GENERIC 0x40
159#define ADM1026_VERSTEP_ADM1026 0x44
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
Jean Delvarecb01a232007-11-29 23:46:42 +0100161#define ADM1026_REG_MASK1 0x18
162#define ADM1026_REG_MASK2 0x19
163#define ADM1026_REG_MASK3 0x1a
164#define ADM1026_REG_MASK4 0x1b
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165
Jean Delvarecb01a232007-11-29 23:46:42 +0100166#define ADM1026_REG_STATUS1 0x20
167#define ADM1026_REG_STATUS2 0x21
168#define ADM1026_REG_STATUS3 0x22
169#define ADM1026_REG_STATUS4 0x23
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
171#define ADM1026_FAN_ACTIVATION_TEMP_HYST -6
Jean Delvarecb01a232007-11-29 23:46:42 +0100172#define ADM1026_FAN_CONTROL_TEMP_RANGE 20
173#define ADM1026_PWM_MAX 255
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174
Guenter Roeck86aa3e22012-01-14 12:51:15 -0800175/*
176 * Conversions. Rounding and limit checking is only done on the TO_REG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 * variants. Note that you should be a bit careful with which arguments
178 * these macros are called: arguments may be evaluated more than once.
179 */
180
Guenter Roeck86aa3e22012-01-14 12:51:15 -0800181/*
182 * IN are scaled according to built-in resistors. These are the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 * voltages corresponding to 3/4 of full scale (192 or 0xc0)
184 * NOTE: The -12V input needs an additional factor to account
185 * for the Vref pullup resistor.
186 * NEG12_OFFSET = SCALE * Vref / V-192 - Vref
187 * = 13875 * 2.50 / 1.875 - 2500
188 * = 16000
189 *
190 * The values in this table are based on Table II, page 15 of the
191 * datasheet.
192 */
Jean Delvarecb01a232007-11-29 23:46:42 +0100193static int adm1026_scaling[] = { /* .001 Volts */
194 2250, 2250, 2250, 2250, 2250, 2250,
195 1875, 1875, 1875, 1875, 3000, 3330,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 3330, 4995, 2250, 12000, 13875
197 };
198#define NEG12_OFFSET 16000
Jean Delvarecb01a232007-11-29 23:46:42 +0100199#define SCALE(val, from, to) (((val)*(to) + ((from)/2))/(from))
Guenter Roeck2a844c12013-01-09 08:09:34 -0800200#define INS_TO_REG(n, val) (clamp_val(SCALE(val, adm1026_scaling[n], 192),\
Jean Delvarecb01a232007-11-29 23:46:42 +0100201 0, 255))
202#define INS_FROM_REG(n, val) (SCALE(val, 192, adm1026_scaling[n]))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
Guenter Roeck86aa3e22012-01-14 12:51:15 -0800204/*
205 * FAN speed is measured using 22.5kHz clock and counts for 2 pulses
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 * and we assume a 2 pulse-per-rev fan tach signal
207 * 22500 kHz * 60 (sec/min) * 2 (pulse) / 2 (pulse/rev) == 1350000
208 */
Jean Delvarecb01a232007-11-29 23:46:42 +0100209#define FAN_TO_REG(val, div) ((val) <= 0 ? 0xff : \
Guenter Roeck2a844c12013-01-09 08:09:34 -0800210 clamp_val(1350000 / ((val) * (div)), \
Guenter Roeck86aa3e22012-01-14 12:51:15 -0800211 1, 254))
212#define FAN_FROM_REG(val, div) ((val) == 0 ? -1 : (val) == 0xff ? 0 : \
213 1350000 / ((val) * (div)))
214#define DIV_FROM_REG(val) (1 << (val))
Jean Delvarecb01a232007-11-29 23:46:42 +0100215#define DIV_TO_REG(val) ((val) >= 8 ? 3 : (val) >= 4 ? 2 : (val) >= 2 ? 1 : 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216
217/* Temperature is reported in 1 degC increments */
Guenter Roeck2a844c12013-01-09 08:09:34 -0800218#define TEMP_TO_REG(val) (clamp_val(((val) + ((val) < 0 ? -500 : 500)) \
Guenter Roeck86aa3e22012-01-14 12:51:15 -0800219 / 1000, -127, 127))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220#define TEMP_FROM_REG(val) ((val) * 1000)
Guenter Roeck2a844c12013-01-09 08:09:34 -0800221#define OFFSET_TO_REG(val) (clamp_val(((val) + ((val) < 0 ? -500 : 500)) \
Guenter Roeck86aa3e22012-01-14 12:51:15 -0800222 / 1000, -127, 127))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223#define OFFSET_FROM_REG(val) ((val) * 1000)
224
Guenter Roeck2a844c12013-01-09 08:09:34 -0800225#define PWM_TO_REG(val) (clamp_val(val, 0, 255))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226#define PWM_FROM_REG(val) (val)
227
228#define PWM_MIN_TO_REG(val) ((val) & 0xf0)
229#define PWM_MIN_FROM_REG(val) (((val) & 0xf0) + ((val) >> 4))
230
Guenter Roeck86aa3e22012-01-14 12:51:15 -0800231/*
232 * Analog output is a voltage, and scaled to millivolts. The datasheet
Jean Delvarecb01a232007-11-29 23:46:42 +0100233 * indicates that the DAC could be used to drive the fans, but in our
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 * example board (Arima HDAMA) it isn't connected to the fans at all.
235 */
Guenter Roeck2a844c12013-01-09 08:09:34 -0800236#define DAC_TO_REG(val) (clamp_val(((((val) * 255) + 500) / 2500), 0, 255))
Guenter Roeck86aa3e22012-01-14 12:51:15 -0800237#define DAC_FROM_REG(val) (((val) * 2500) / 255)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238
Guenter Roeck86aa3e22012-01-14 12:51:15 -0800239/*
240 * Chip sampling rates
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 *
242 * Some sensors are not updated more frequently than once per second
243 * so it doesn't make sense to read them more often than that.
244 * We cache the results and return the saved data if the driver
245 * is called again before a second has elapsed.
246 *
247 * Also, there is significant configuration data for this chip
248 * So, we keep the config data up to date in the cache
249 * when it is written and only sample it once every 5 *minutes*
250 */
Jean Delvarecb01a232007-11-29 23:46:42 +0100251#define ADM1026_DATA_INTERVAL (1 * HZ)
252#define ADM1026_CONFIG_INTERVAL (5 * 60 * HZ)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253
Guenter Roeck86aa3e22012-01-14 12:51:15 -0800254/*
255 * We allow for multiple chips in a single system.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 *
257 * For each registered ADM1026, we need to keep state information
258 * at client->data. The adm1026_data structure is dynamically
Guenter Roeck86aa3e22012-01-14 12:51:15 -0800259 * allocated, when a new client structure is allocated.
260 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261
262struct pwm_data {
263 u8 pwm;
264 u8 enable;
265 u8 auto_pwm_min;
266};
267
268struct adm1026_data {
Axel Linb591d3c2014-07-03 21:53:01 +0800269 struct i2c_client *client;
270 const struct attribute_group *groups[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100272 struct mutex update_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 int valid; /* !=0 if following fields are valid */
274 unsigned long last_reading; /* In jiffies */
275 unsigned long last_config; /* In jiffies */
276
Jean Delvarecb01a232007-11-29 23:46:42 +0100277 u8 in[17]; /* Register value */
278 u8 in_max[17]; /* Register value */
279 u8 in_min[17]; /* Register value */
280 s8 temp[3]; /* Register value */
281 s8 temp_min[3]; /* Register value */
282 s8 temp_max[3]; /* Register value */
283 s8 temp_tmin[3]; /* Register value */
284 s8 temp_crit[3]; /* Register value */
285 s8 temp_offset[3]; /* Register value */
286 u8 fan[8]; /* Register value */
287 u8 fan_min[8]; /* Register value */
288 u8 fan_div[8]; /* Decoded value */
289 struct pwm_data pwm1; /* Pwm control values */
Jean Delvarecb01a232007-11-29 23:46:42 +0100290 u8 vrm; /* VRM version */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 u8 analog_out; /* Register value (DAC) */
Jean Delvarecb01a232007-11-29 23:46:42 +0100292 long alarms; /* Register encoding, combined */
293 long alarm_mask; /* Register encoding, combined */
294 long gpio; /* Register encoding, combined */
295 long gpio_mask; /* Register encoding, combined */
296 u8 gpio_config[17]; /* Decoded value */
297 u8 config1; /* Register value */
298 u8 config2; /* Register value */
299 u8 config3; /* Register value */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300};
301
Ben Dooksc49efce2005-10-26 21:07:25 +0200302static int adm1026_read_value(struct i2c_client *client, u8 reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303{
304 int res;
305
306 if (reg < 0x80) {
307 /* "RAM" locations */
308 res = i2c_smbus_read_byte_data(client, reg) & 0xff;
309 } else {
310 /* EEPROM, do nothing */
311 res = 0;
312 }
313 return res;
314}
315
Ben Dooksc49efce2005-10-26 21:07:25 +0200316static int adm1026_write_value(struct i2c_client *client, u8 reg, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317{
318 int res;
319
320 if (reg < 0x80) {
321 /* "RAM" locations */
322 res = i2c_smbus_write_byte_data(client, reg, value);
323 } else {
324 /* EEPROM, do nothing */
325 res = 0;
326 }
327 return res;
328}
329
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330static struct adm1026_data *adm1026_update_device(struct device *dev)
331{
Axel Linb591d3c2014-07-03 21:53:01 +0800332 struct adm1026_data *data = dev_get_drvdata(dev);
333 struct i2c_client *client = data->client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 int i;
335 long value, alarms, gpio;
336
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100337 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 if (!data->valid
Guenter Roeck86aa3e22012-01-14 12:51:15 -0800339 || time_after(jiffies,
340 data->last_reading + ADM1026_DATA_INTERVAL)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 /* Things that change quickly */
Jean Delvarecb01a232007-11-29 23:46:42 +0100342 dev_dbg(&client->dev, "Reading sensor values\n");
Guenter Roeck86aa3e22012-01-14 12:51:15 -0800343 for (i = 0; i <= 16; ++i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 data->in[i] =
345 adm1026_read_value(client, ADM1026_REG_IN[i]);
346 }
347
Guenter Roeck86aa3e22012-01-14 12:51:15 -0800348 for (i = 0; i <= 7; ++i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 data->fan[i] =
350 adm1026_read_value(client, ADM1026_REG_FAN(i));
351 }
352
Guenter Roeck86aa3e22012-01-14 12:51:15 -0800353 for (i = 0; i <= 2; ++i) {
354 /*
355 * NOTE: temp[] is s8 and we assume 2's complement
356 * "conversion" in the assignment
357 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 data->temp[i] =
359 adm1026_read_value(client, ADM1026_REG_TEMP[i]);
360 }
361
Jean Delvarecb01a232007-11-29 23:46:42 +0100362 data->pwm1.pwm = adm1026_read_value(client,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 ADM1026_REG_PWM);
Jean Delvarecb01a232007-11-29 23:46:42 +0100364 data->analog_out = adm1026_read_value(client,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 ADM1026_REG_DAC);
366 /* GPIO16 is MSbit of alarms, move it to gpio */
367 alarms = adm1026_read_value(client, ADM1026_REG_STATUS4);
Jean Delvarecb01a232007-11-29 23:46:42 +0100368 gpio = alarms & 0x80 ? 0x0100 : 0; /* GPIO16 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 alarms &= 0x7f;
370 alarms <<= 8;
371 alarms |= adm1026_read_value(client, ADM1026_REG_STATUS3);
372 alarms <<= 8;
373 alarms |= adm1026_read_value(client, ADM1026_REG_STATUS2);
374 alarms <<= 8;
375 alarms |= adm1026_read_value(client, ADM1026_REG_STATUS1);
376 data->alarms = alarms;
377
378 /* Read the GPIO values */
Jean Delvarecb01a232007-11-29 23:46:42 +0100379 gpio |= adm1026_read_value(client,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 ADM1026_REG_GPIO_STATUS_8_15);
381 gpio <<= 8;
Jean Delvarecb01a232007-11-29 23:46:42 +0100382 gpio |= adm1026_read_value(client,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 ADM1026_REG_GPIO_STATUS_0_7);
384 data->gpio = gpio;
385
386 data->last_reading = jiffies;
Guenter Roecka0393712013-03-29 13:54:50 -0700387 } /* last_reading */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388
389 if (!data->valid ||
390 time_after(jiffies, data->last_config + ADM1026_CONFIG_INTERVAL)) {
391 /* Things that don't change often */
392 dev_dbg(&client->dev, "Reading config values\n");
Guenter Roeck86aa3e22012-01-14 12:51:15 -0800393 for (i = 0; i <= 16; ++i) {
Jean Delvarecb01a232007-11-29 23:46:42 +0100394 data->in_min[i] = adm1026_read_value(client,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 ADM1026_REG_IN_MIN[i]);
Jean Delvarecb01a232007-11-29 23:46:42 +0100396 data->in_max[i] = adm1026_read_value(client,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 ADM1026_REG_IN_MAX[i]);
398 }
399
400 value = adm1026_read_value(client, ADM1026_REG_FAN_DIV_0_3)
401 | (adm1026_read_value(client, ADM1026_REG_FAN_DIV_4_7)
402 << 8);
Guenter Roeck86aa3e22012-01-14 12:51:15 -0800403 for (i = 0; i <= 7; ++i) {
Jean Delvarecb01a232007-11-29 23:46:42 +0100404 data->fan_min[i] = adm1026_read_value(client,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 ADM1026_REG_FAN_MIN(i));
406 data->fan_div[i] = DIV_FROM_REG(value & 0x03);
407 value >>= 2;
408 }
409
410 for (i = 0; i <= 2; ++i) {
Guenter Roeck86aa3e22012-01-14 12:51:15 -0800411 /*
412 * NOTE: temp_xxx[] are s8 and we assume 2's
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 * complement "conversion" in the assignment
414 */
Jean Delvarecb01a232007-11-29 23:46:42 +0100415 data->temp_min[i] = adm1026_read_value(client,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 ADM1026_REG_TEMP_MIN[i]);
Jean Delvarecb01a232007-11-29 23:46:42 +0100417 data->temp_max[i] = adm1026_read_value(client,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 ADM1026_REG_TEMP_MAX[i]);
Jean Delvarecb01a232007-11-29 23:46:42 +0100419 data->temp_tmin[i] = adm1026_read_value(client,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 ADM1026_REG_TEMP_TMIN[i]);
Jean Delvarecb01a232007-11-29 23:46:42 +0100421 data->temp_crit[i] = adm1026_read_value(client,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 ADM1026_REG_TEMP_THERM[i]);
Jean Delvarecb01a232007-11-29 23:46:42 +0100423 data->temp_offset[i] = adm1026_read_value(client,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 ADM1026_REG_TEMP_OFFSET[i]);
425 }
426
427 /* Read the STATUS/alarm masks */
Jean Delvarecb01a232007-11-29 23:46:42 +0100428 alarms = adm1026_read_value(client, ADM1026_REG_MASK4);
429 gpio = alarms & 0x80 ? 0x0100 : 0; /* GPIO16 */
430 alarms = (alarms & 0x7f) << 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 alarms |= adm1026_read_value(client, ADM1026_REG_MASK3);
432 alarms <<= 8;
433 alarms |= adm1026_read_value(client, ADM1026_REG_MASK2);
434 alarms <<= 8;
435 alarms |= adm1026_read_value(client, ADM1026_REG_MASK1);
436 data->alarm_mask = alarms;
437
438 /* Read the GPIO values */
Jean Delvarecb01a232007-11-29 23:46:42 +0100439 gpio |= adm1026_read_value(client,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 ADM1026_REG_GPIO_MASK_8_15);
441 gpio <<= 8;
442 gpio |= adm1026_read_value(client, ADM1026_REG_GPIO_MASK_0_7);
443 data->gpio_mask = gpio;
444
445 /* Read various values from CONFIG1 */
Jean Delvarecb01a232007-11-29 23:46:42 +0100446 data->config1 = adm1026_read_value(client,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 ADM1026_REG_CONFIG1);
448 if (data->config1 & CFG1_PWM_AFC) {
449 data->pwm1.enable = 2;
Jean Delvarecb01a232007-11-29 23:46:42 +0100450 data->pwm1.auto_pwm_min =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 PWM_MIN_FROM_REG(data->pwm1.pwm);
452 }
453 /* Read the GPIO config */
Jean Delvarecb01a232007-11-29 23:46:42 +0100454 data->config2 = adm1026_read_value(client,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 ADM1026_REG_CONFIG2);
Jean Delvarecb01a232007-11-29 23:46:42 +0100456 data->config3 = adm1026_read_value(client,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 ADM1026_REG_CONFIG3);
458 data->gpio_config[16] = (data->config3 >> 6) & 0x03;
459
460 value = 0;
Guenter Roeck86aa3e22012-01-14 12:51:15 -0800461 for (i = 0; i <= 15; ++i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 if ((i & 0x03) == 0) {
463 value = adm1026_read_value(client,
464 ADM1026_REG_GPIO_CFG_0_3 + i/4);
465 }
466 data->gpio_config[i] = value & 0x03;
467 value >>= 2;
468 }
469
470 data->last_config = jiffies;
Guenter Roecka0393712013-03-29 13:54:50 -0700471 } /* last_config */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 data->valid = 1;
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100474 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 return data;
476}
477
Yani Ioannou050480f2005-06-05 10:51:46 +0200478static ssize_t show_in(struct device *dev, struct device_attribute *attr,
479 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480{
Yani Ioannou050480f2005-06-05 10:51:46 +0200481 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
482 int nr = sensor_attr->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 struct adm1026_data *data = adm1026_update_device(dev);
Jean Delvarecb01a232007-11-29 23:46:42 +0100484 return sprintf(buf, "%d\n", INS_FROM_REG(nr, data->in[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485}
Yani Ioannou050480f2005-06-05 10:51:46 +0200486static ssize_t show_in_min(struct device *dev, struct device_attribute *attr,
487 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488{
Yani Ioannou050480f2005-06-05 10:51:46 +0200489 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
490 int nr = sensor_attr->index;
Jean Delvarecb01a232007-11-29 23:46:42 +0100491 struct adm1026_data *data = adm1026_update_device(dev);
492 return sprintf(buf, "%d\n", INS_FROM_REG(nr, data->in_min[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493}
Yani Ioannou050480f2005-06-05 10:51:46 +0200494static ssize_t set_in_min(struct device *dev, struct device_attribute *attr,
495 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496{
Yani Ioannou050480f2005-06-05 10:51:46 +0200497 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
498 int nr = sensor_attr->index;
Axel Linb591d3c2014-07-03 21:53:01 +0800499 struct adm1026_data *data = dev_get_drvdata(dev);
500 struct i2c_client *client = data->client;
Guenter Roeck86aa3e22012-01-14 12:51:15 -0800501 long val;
502 int err;
503
504 err = kstrtol(buf, 10, &val);
505 if (err)
506 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100508 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 data->in_min[nr] = INS_TO_REG(nr, val);
510 adm1026_write_value(client, ADM1026_REG_IN_MIN[nr], data->in_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100511 mutex_unlock(&data->update_lock);
Jean Delvarecb01a232007-11-29 23:46:42 +0100512 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513}
Yani Ioannou050480f2005-06-05 10:51:46 +0200514static ssize_t show_in_max(struct device *dev, struct device_attribute *attr,
515 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516{
Yani Ioannou050480f2005-06-05 10:51:46 +0200517 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
518 int nr = sensor_attr->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 struct adm1026_data *data = adm1026_update_device(dev);
Jean Delvarecb01a232007-11-29 23:46:42 +0100520 return sprintf(buf, "%d\n", INS_FROM_REG(nr, data->in_max[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521}
Yani Ioannou050480f2005-06-05 10:51:46 +0200522static ssize_t set_in_max(struct device *dev, struct device_attribute *attr,
523 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524{
Yani Ioannou050480f2005-06-05 10:51:46 +0200525 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
526 int nr = sensor_attr->index;
Axel Linb591d3c2014-07-03 21:53:01 +0800527 struct adm1026_data *data = dev_get_drvdata(dev);
528 struct i2c_client *client = data->client;
Guenter Roeck86aa3e22012-01-14 12:51:15 -0800529 long val;
530 int err;
531
532 err = kstrtol(buf, 10, &val);
533 if (err)
534 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100536 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 data->in_max[nr] = INS_TO_REG(nr, val);
538 adm1026_write_value(client, ADM1026_REG_IN_MAX[nr], data->in_max[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100539 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 return count;
541}
542
Yani Ioannou050480f2005-06-05 10:51:46 +0200543#define in_reg(offset) \
544static SENSOR_DEVICE_ATTR(in##offset##_input, S_IRUGO, show_in, \
545 NULL, offset); \
546static SENSOR_DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \
547 show_in_min, set_in_min, offset); \
548static SENSOR_DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \
549 show_in_max, set_in_max, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550
551
552in_reg(0);
553in_reg(1);
554in_reg(2);
555in_reg(3);
556in_reg(4);
557in_reg(5);
558in_reg(6);
559in_reg(7);
560in_reg(8);
561in_reg(9);
562in_reg(10);
563in_reg(11);
564in_reg(12);
565in_reg(13);
566in_reg(14);
567in_reg(15);
568
Guenter Roeck86aa3e22012-01-14 12:51:15 -0800569static ssize_t show_in16(struct device *dev, struct device_attribute *attr,
570 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571{
572 struct adm1026_data *data = adm1026_update_device(dev);
Jean Delvarecb01a232007-11-29 23:46:42 +0100573 return sprintf(buf, "%d\n", INS_FROM_REG(16, data->in[16]) -
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 NEG12_OFFSET);
575}
Guenter Roeck86aa3e22012-01-14 12:51:15 -0800576static ssize_t show_in16_min(struct device *dev, struct device_attribute *attr,
577 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578{
Jean Delvarecb01a232007-11-29 23:46:42 +0100579 struct adm1026_data *data = adm1026_update_device(dev);
580 return sprintf(buf, "%d\n", INS_FROM_REG(16, data->in_min[16])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 - NEG12_OFFSET);
582}
Guenter Roeck86aa3e22012-01-14 12:51:15 -0800583static ssize_t set_in16_min(struct device *dev, struct device_attribute *attr,
584 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585{
Axel Linb591d3c2014-07-03 21:53:01 +0800586 struct adm1026_data *data = dev_get_drvdata(dev);
587 struct i2c_client *client = data->client;
Guenter Roeck86aa3e22012-01-14 12:51:15 -0800588 long val;
589 int err;
590
591 err = kstrtol(buf, 10, &val);
592 if (err)
593 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100595 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 data->in_min[16] = INS_TO_REG(16, val + NEG12_OFFSET);
597 adm1026_write_value(client, ADM1026_REG_IN_MIN[16], data->in_min[16]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100598 mutex_unlock(&data->update_lock);
Jean Delvarecb01a232007-11-29 23:46:42 +0100599 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600}
Guenter Roeck86aa3e22012-01-14 12:51:15 -0800601static ssize_t show_in16_max(struct device *dev, struct device_attribute *attr,
602 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603{
604 struct adm1026_data *data = adm1026_update_device(dev);
Jean Delvarecb01a232007-11-29 23:46:42 +0100605 return sprintf(buf, "%d\n", INS_FROM_REG(16, data->in_max[16])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 - NEG12_OFFSET);
607}
Guenter Roeck86aa3e22012-01-14 12:51:15 -0800608static ssize_t set_in16_max(struct device *dev, struct device_attribute *attr,
609 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610{
Axel Linb591d3c2014-07-03 21:53:01 +0800611 struct adm1026_data *data = dev_get_drvdata(dev);
612 struct i2c_client *client = data->client;
Guenter Roeck86aa3e22012-01-14 12:51:15 -0800613 long val;
614 int err;
615
616 err = kstrtol(buf, 10, &val);
617 if (err)
618 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100620 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 data->in_max[16] = INS_TO_REG(16, val+NEG12_OFFSET);
622 adm1026_write_value(client, ADM1026_REG_IN_MAX[16], data->in_max[16]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100623 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 return count;
625}
626
Yani Ioannou050480f2005-06-05 10:51:46 +0200627static SENSOR_DEVICE_ATTR(in16_input, S_IRUGO, show_in16, NULL, 16);
Guenter Roeck86aa3e22012-01-14 12:51:15 -0800628static SENSOR_DEVICE_ATTR(in16_min, S_IRUGO | S_IWUSR, show_in16_min,
629 set_in16_min, 16);
630static SENSOR_DEVICE_ATTR(in16_max, S_IRUGO | S_IWUSR, show_in16_max,
631 set_in16_max, 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632
633
634/* Now add fan read/write functions */
635
Yani Ioannou050480f2005-06-05 10:51:46 +0200636static ssize_t show_fan(struct device *dev, struct device_attribute *attr,
637 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638{
Yani Ioannou050480f2005-06-05 10:51:46 +0200639 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
640 int nr = sensor_attr->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 struct adm1026_data *data = adm1026_update_device(dev);
Jean Delvarecb01a232007-11-29 23:46:42 +0100642 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[nr],
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 data->fan_div[nr]));
644}
Yani Ioannou050480f2005-06-05 10:51:46 +0200645static ssize_t show_fan_min(struct device *dev, struct device_attribute *attr,
646 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647{
Yani Ioannou050480f2005-06-05 10:51:46 +0200648 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
649 int nr = sensor_attr->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 struct adm1026_data *data = adm1026_update_device(dev);
Jean Delvarecb01a232007-11-29 23:46:42 +0100651 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_min[nr],
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 data->fan_div[nr]));
653}
Yani Ioannou050480f2005-06-05 10:51:46 +0200654static ssize_t set_fan_min(struct device *dev, struct device_attribute *attr,
655 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656{
Yani Ioannou050480f2005-06-05 10:51:46 +0200657 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
658 int nr = sensor_attr->index;
Axel Linb591d3c2014-07-03 21:53:01 +0800659 struct adm1026_data *data = dev_get_drvdata(dev);
660 struct i2c_client *client = data->client;
Guenter Roeck86aa3e22012-01-14 12:51:15 -0800661 long val;
662 int err;
663
664 err = kstrtol(buf, 10, &val);
665 if (err)
666 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100668 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 data->fan_min[nr] = FAN_TO_REG(val, data->fan_div[nr]);
670 adm1026_write_value(client, ADM1026_REG_FAN_MIN(nr),
671 data->fan_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100672 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 return count;
674}
675
Jean Delvarecb01a232007-11-29 23:46:42 +0100676#define fan_offset(offset) \
677static SENSOR_DEVICE_ATTR(fan##offset##_input, S_IRUGO, show_fan, NULL, \
678 offset - 1); \
679static SENSOR_DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
Yani Ioannou050480f2005-06-05 10:51:46 +0200680 show_fan_min, set_fan_min, offset - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681
682fan_offset(1);
683fan_offset(2);
684fan_offset(3);
685fan_offset(4);
686fan_offset(5);
687fan_offset(6);
688fan_offset(7);
689fan_offset(8);
690
691/* Adjust fan_min to account for new fan divisor */
692static void fixup_fan_min(struct device *dev, int fan, int old_div)
693{
Axel Linb591d3c2014-07-03 21:53:01 +0800694 struct adm1026_data *data = dev_get_drvdata(dev);
695 struct i2c_client *client = data->client;
Jean Delvarecb01a232007-11-29 23:46:42 +0100696 int new_min;
697 int new_div = data->fan_div[fan];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698
699 /* 0 and 0xff are special. Don't adjust them */
Guenter Roeck86aa3e22012-01-14 12:51:15 -0800700 if (data->fan_min[fan] == 0 || data->fan_min[fan] == 0xff)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702
703 new_min = data->fan_min[fan] * old_div / new_div;
Guenter Roeck2a844c12013-01-09 08:09:34 -0800704 new_min = clamp_val(new_min, 1, 254);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 data->fan_min[fan] = new_min;
706 adm1026_write_value(client, ADM1026_REG_FAN_MIN(fan), new_min);
707}
708
709/* Now add fan_div read/write functions */
Yani Ioannou050480f2005-06-05 10:51:46 +0200710static ssize_t show_fan_div(struct device *dev, struct device_attribute *attr,
711 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712{
Yani Ioannou050480f2005-06-05 10:51:46 +0200713 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
714 int nr = sensor_attr->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 struct adm1026_data *data = adm1026_update_device(dev);
Jean Delvarecb01a232007-11-29 23:46:42 +0100716 return sprintf(buf, "%d\n", data->fan_div[nr]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717}
Yani Ioannou050480f2005-06-05 10:51:46 +0200718static ssize_t set_fan_div(struct device *dev, struct device_attribute *attr,
719 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720{
Yani Ioannou050480f2005-06-05 10:51:46 +0200721 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
722 int nr = sensor_attr->index;
Axel Linb591d3c2014-07-03 21:53:01 +0800723 struct adm1026_data *data = dev_get_drvdata(dev);
724 struct i2c_client *client = data->client;
Guenter Roeck86aa3e22012-01-14 12:51:15 -0800725 long val;
726 int orig_div, new_div;
727 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728
Guenter Roeck86aa3e22012-01-14 12:51:15 -0800729 err = kstrtol(buf, 10, &val);
730 if (err)
731 return err;
732
Jean Delvarecb01a232007-11-29 23:46:42 +0100733 new_div = DIV_TO_REG(val);
Gabriele Gorla8b0f1842010-12-08 16:27:22 +0100734
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100735 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 orig_div = data->fan_div[nr];
737 data->fan_div[nr] = DIV_FROM_REG(new_div);
738
739 if (nr < 4) { /* 0 <= nr < 4 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 adm1026_write_value(client, ADM1026_REG_FAN_DIV_0_3,
Gabriele Gorla52bc9802010-12-08 16:27:22 +0100741 (DIV_TO_REG(data->fan_div[0]) << 0) |
742 (DIV_TO_REG(data->fan_div[1]) << 2) |
743 (DIV_TO_REG(data->fan_div[2]) << 4) |
744 (DIV_TO_REG(data->fan_div[3]) << 6));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 } else { /* 3 < nr < 8 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 adm1026_write_value(client, ADM1026_REG_FAN_DIV_4_7,
Gabriele Gorla52bc9802010-12-08 16:27:22 +0100747 (DIV_TO_REG(data->fan_div[4]) << 0) |
748 (DIV_TO_REG(data->fan_div[5]) << 2) |
749 (DIV_TO_REG(data->fan_div[6]) << 4) |
750 (DIV_TO_REG(data->fan_div[7]) << 6));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 }
752
Guenter Roeck86aa3e22012-01-14 12:51:15 -0800753 if (data->fan_div[nr] != orig_div)
Jean Delvarecb01a232007-11-29 23:46:42 +0100754 fixup_fan_min(dev, nr, orig_div);
Guenter Roeck86aa3e22012-01-14 12:51:15 -0800755
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100756 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 return count;
758}
759
Jean Delvarecb01a232007-11-29 23:46:42 +0100760#define fan_offset_div(offset) \
761static SENSOR_DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR, \
Yani Ioannou050480f2005-06-05 10:51:46 +0200762 show_fan_div, set_fan_div, offset - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763
764fan_offset_div(1);
765fan_offset_div(2);
766fan_offset_div(3);
767fan_offset_div(4);
768fan_offset_div(5);
769fan_offset_div(6);
770fan_offset_div(7);
771fan_offset_div(8);
772
773/* Temps */
Yani Ioannou050480f2005-06-05 10:51:46 +0200774static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
775 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776{
Yani Ioannou050480f2005-06-05 10:51:46 +0200777 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
778 int nr = sensor_attr->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 struct adm1026_data *data = adm1026_update_device(dev);
Jean Delvarecb01a232007-11-29 23:46:42 +0100780 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781}
Yani Ioannou050480f2005-06-05 10:51:46 +0200782static ssize_t show_temp_min(struct device *dev, struct device_attribute *attr,
783 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784{
Yani Ioannou050480f2005-06-05 10:51:46 +0200785 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
786 int nr = sensor_attr->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 struct adm1026_data *data = adm1026_update_device(dev);
Jean Delvarecb01a232007-11-29 23:46:42 +0100788 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_min[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789}
Yani Ioannou050480f2005-06-05 10:51:46 +0200790static ssize_t set_temp_min(struct device *dev, struct device_attribute *attr,
791 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792{
Yani Ioannou050480f2005-06-05 10:51:46 +0200793 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
794 int nr = sensor_attr->index;
Axel Linb591d3c2014-07-03 21:53:01 +0800795 struct adm1026_data *data = dev_get_drvdata(dev);
796 struct i2c_client *client = data->client;
Guenter Roeck86aa3e22012-01-14 12:51:15 -0800797 long val;
798 int err;
799
800 err = kstrtol(buf, 10, &val);
801 if (err)
802 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100804 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 data->temp_min[nr] = TEMP_TO_REG(val);
806 adm1026_write_value(client, ADM1026_REG_TEMP_MIN[nr],
807 data->temp_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100808 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 return count;
810}
Yani Ioannou050480f2005-06-05 10:51:46 +0200811static ssize_t show_temp_max(struct device *dev, struct device_attribute *attr,
812 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813{
Yani Ioannou050480f2005-06-05 10:51:46 +0200814 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
815 int nr = sensor_attr->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 struct adm1026_data *data = adm1026_update_device(dev);
Jean Delvarecb01a232007-11-29 23:46:42 +0100817 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_max[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818}
Yani Ioannou050480f2005-06-05 10:51:46 +0200819static ssize_t set_temp_max(struct device *dev, struct device_attribute *attr,
820 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821{
Yani Ioannou050480f2005-06-05 10:51:46 +0200822 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
823 int nr = sensor_attr->index;
Axel Linb591d3c2014-07-03 21:53:01 +0800824 struct adm1026_data *data = dev_get_drvdata(dev);
825 struct i2c_client *client = data->client;
Guenter Roeck86aa3e22012-01-14 12:51:15 -0800826 long val;
827 int err;
828
829 err = kstrtol(buf, 10, &val);
830 if (err)
831 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100833 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 data->temp_max[nr] = TEMP_TO_REG(val);
835 adm1026_write_value(client, ADM1026_REG_TEMP_MAX[nr],
836 data->temp_max[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100837 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 return count;
839}
Yani Ioannou050480f2005-06-05 10:51:46 +0200840
841#define temp_reg(offset) \
Jean Delvarecb01a232007-11-29 23:46:42 +0100842static SENSOR_DEVICE_ATTR(temp##offset##_input, S_IRUGO, show_temp, \
Yani Ioannou050480f2005-06-05 10:51:46 +0200843 NULL, offset - 1); \
844static SENSOR_DEVICE_ATTR(temp##offset##_min, S_IRUGO | S_IWUSR, \
845 show_temp_min, set_temp_min, offset - 1); \
846static SENSOR_DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR, \
847 show_temp_max, set_temp_max, offset - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848
849
850temp_reg(1);
851temp_reg(2);
852temp_reg(3);
853
Yani Ioannou050480f2005-06-05 10:51:46 +0200854static ssize_t show_temp_offset(struct device *dev,
855 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856{
Yani Ioannou050480f2005-06-05 10:51:46 +0200857 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
858 int nr = sensor_attr->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 struct adm1026_data *data = adm1026_update_device(dev);
Jean Delvarecb01a232007-11-29 23:46:42 +0100860 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_offset[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861}
Yani Ioannou050480f2005-06-05 10:51:46 +0200862static ssize_t set_temp_offset(struct device *dev,
863 struct device_attribute *attr, const char *buf,
864 size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865{
Yani Ioannou050480f2005-06-05 10:51:46 +0200866 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
867 int nr = sensor_attr->index;
Axel Linb591d3c2014-07-03 21:53:01 +0800868 struct adm1026_data *data = dev_get_drvdata(dev);
869 struct i2c_client *client = data->client;
Guenter Roeck86aa3e22012-01-14 12:51:15 -0800870 long val;
871 int err;
872
873 err = kstrtol(buf, 10, &val);
874 if (err)
875 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100877 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 data->temp_offset[nr] = TEMP_TO_REG(val);
879 adm1026_write_value(client, ADM1026_REG_TEMP_OFFSET[nr],
880 data->temp_offset[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100881 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 return count;
883}
884
Guenter Roeck86aa3e22012-01-14 12:51:15 -0800885#define temp_offset_reg(offset) \
886static SENSOR_DEVICE_ATTR(temp##offset##_offset, S_IRUGO | S_IWUSR, \
Yani Ioannou050480f2005-06-05 10:51:46 +0200887 show_temp_offset, set_temp_offset, offset - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888
889temp_offset_reg(1);
890temp_offset_reg(2);
891temp_offset_reg(3);
892
Yani Ioannou050480f2005-06-05 10:51:46 +0200893static ssize_t show_temp_auto_point1_temp_hyst(struct device *dev,
894 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895{
Yani Ioannou050480f2005-06-05 10:51:46 +0200896 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
897 int nr = sensor_attr->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 struct adm1026_data *data = adm1026_update_device(dev);
Jean Delvarecb01a232007-11-29 23:46:42 +0100899 return sprintf(buf, "%d\n", TEMP_FROM_REG(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 ADM1026_FAN_ACTIVATION_TEMP_HYST + data->temp_tmin[nr]));
901}
Yani Ioannou050480f2005-06-05 10:51:46 +0200902static ssize_t show_temp_auto_point2_temp(struct device *dev,
903 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904{
Yani Ioannou050480f2005-06-05 10:51:46 +0200905 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
906 int nr = sensor_attr->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 struct adm1026_data *data = adm1026_update_device(dev);
Jean Delvarecb01a232007-11-29 23:46:42 +0100908 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_tmin[nr] +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 ADM1026_FAN_CONTROL_TEMP_RANGE));
910}
Yani Ioannou050480f2005-06-05 10:51:46 +0200911static ssize_t show_temp_auto_point1_temp(struct device *dev,
912 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913{
Yani Ioannou050480f2005-06-05 10:51:46 +0200914 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
915 int nr = sensor_attr->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 struct adm1026_data *data = adm1026_update_device(dev);
Jean Delvarecb01a232007-11-29 23:46:42 +0100917 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_tmin[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918}
Yani Ioannou050480f2005-06-05 10:51:46 +0200919static ssize_t set_temp_auto_point1_temp(struct device *dev,
920 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921{
Yani Ioannou050480f2005-06-05 10:51:46 +0200922 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
923 int nr = sensor_attr->index;
Axel Linb591d3c2014-07-03 21:53:01 +0800924 struct adm1026_data *data = dev_get_drvdata(dev);
925 struct i2c_client *client = data->client;
Guenter Roeck86aa3e22012-01-14 12:51:15 -0800926 long val;
927 int err;
928
929 err = kstrtol(buf, 10, &val);
930 if (err)
931 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100933 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 data->temp_tmin[nr] = TEMP_TO_REG(val);
935 adm1026_write_value(client, ADM1026_REG_TEMP_TMIN[nr],
936 data->temp_tmin[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100937 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 return count;
939}
940
Jean Delvarecb01a232007-11-29 23:46:42 +0100941#define temp_auto_point(offset) \
942static SENSOR_DEVICE_ATTR(temp##offset##_auto_point1_temp, \
943 S_IRUGO | S_IWUSR, show_temp_auto_point1_temp, \
944 set_temp_auto_point1_temp, offset - 1); \
945static SENSOR_DEVICE_ATTR(temp##offset##_auto_point1_temp_hyst, S_IRUGO,\
946 show_temp_auto_point1_temp_hyst, NULL, offset - 1); \
947static SENSOR_DEVICE_ATTR(temp##offset##_auto_point2_temp, S_IRUGO, \
Yani Ioannou050480f2005-06-05 10:51:46 +0200948 show_temp_auto_point2_temp, NULL, offset - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949
950temp_auto_point(1);
951temp_auto_point(2);
952temp_auto_point(3);
953
Yani Ioannou050480f2005-06-05 10:51:46 +0200954static ssize_t show_temp_crit_enable(struct device *dev,
955 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956{
957 struct adm1026_data *data = adm1026_update_device(dev);
Jean Delvarecb01a232007-11-29 23:46:42 +0100958 return sprintf(buf, "%d\n", (data->config1 & CFG1_THERM_HOT) >> 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959}
Yani Ioannou050480f2005-06-05 10:51:46 +0200960static ssize_t set_temp_crit_enable(struct device *dev,
961 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962{
Axel Linb591d3c2014-07-03 21:53:01 +0800963 struct adm1026_data *data = dev_get_drvdata(dev);
964 struct i2c_client *client = data->client;
Guenter Roeck86aa3e22012-01-14 12:51:15 -0800965 unsigned long val;
966 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967
Guenter Roeck86aa3e22012-01-14 12:51:15 -0800968 err = kstrtoul(buf, 10, &val);
969 if (err)
970 return err;
971
972 if (val > 1)
973 return -EINVAL;
974
975 mutex_lock(&data->update_lock);
976 data->config1 = (data->config1 & ~CFG1_THERM_HOT) | (val << 4);
977 adm1026_write_value(client, ADM1026_REG_CONFIG1, data->config1);
978 mutex_unlock(&data->update_lock);
979
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 return count;
981}
982
Yani Ioannou050480f2005-06-05 10:51:46 +0200983#define temp_crit_enable(offset) \
984static DEVICE_ATTR(temp##offset##_crit_enable, S_IRUGO | S_IWUSR, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 show_temp_crit_enable, set_temp_crit_enable);
986
Yani Ioannou050480f2005-06-05 10:51:46 +0200987temp_crit_enable(1);
988temp_crit_enable(2);
989temp_crit_enable(3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990
Yani Ioannou050480f2005-06-05 10:51:46 +0200991static ssize_t show_temp_crit(struct device *dev,
992 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993{
Yani Ioannou050480f2005-06-05 10:51:46 +0200994 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
995 int nr = sensor_attr->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 struct adm1026_data *data = adm1026_update_device(dev);
Jean Delvarecb01a232007-11-29 23:46:42 +0100997 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_crit[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998}
Yani Ioannou050480f2005-06-05 10:51:46 +0200999static ssize_t set_temp_crit(struct device *dev, struct device_attribute *attr,
1000 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001{
Yani Ioannou050480f2005-06-05 10:51:46 +02001002 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1003 int nr = sensor_attr->index;
Axel Linb591d3c2014-07-03 21:53:01 +08001004 struct adm1026_data *data = dev_get_drvdata(dev);
1005 struct i2c_client *client = data->client;
Guenter Roeck86aa3e22012-01-14 12:51:15 -08001006 long val;
1007 int err;
1008
1009 err = kstrtol(buf, 10, &val);
1010 if (err)
1011 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001013 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 data->temp_crit[nr] = TEMP_TO_REG(val);
1015 adm1026_write_value(client, ADM1026_REG_TEMP_THERM[nr],
1016 data->temp_crit[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001017 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 return count;
1019}
1020
Yani Ioannou050480f2005-06-05 10:51:46 +02001021#define temp_crit_reg(offset) \
1022static SENSOR_DEVICE_ATTR(temp##offset##_crit, S_IRUGO | S_IWUSR, \
1023 show_temp_crit, set_temp_crit, offset - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024
1025temp_crit_reg(1);
1026temp_crit_reg(2);
1027temp_crit_reg(3);
1028
Guenter Roeck86aa3e22012-01-14 12:51:15 -08001029static ssize_t show_analog_out_reg(struct device *dev,
1030 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031{
1032 struct adm1026_data *data = adm1026_update_device(dev);
Jean Delvarecb01a232007-11-29 23:46:42 +01001033 return sprintf(buf, "%d\n", DAC_FROM_REG(data->analog_out));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034}
Guenter Roeck86aa3e22012-01-14 12:51:15 -08001035static ssize_t set_analog_out_reg(struct device *dev,
1036 struct device_attribute *attr,
1037 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038{
Axel Linb591d3c2014-07-03 21:53:01 +08001039 struct adm1026_data *data = dev_get_drvdata(dev);
1040 struct i2c_client *client = data->client;
Guenter Roeck86aa3e22012-01-14 12:51:15 -08001041 long val;
1042 int err;
1043
1044 err = kstrtol(buf, 10, &val);
1045 if (err)
1046 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001048 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 data->analog_out = DAC_TO_REG(val);
1050 adm1026_write_value(client, ADM1026_REG_DAC, data->analog_out);
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001051 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 return count;
1053}
1054
Jean Delvarecb01a232007-11-29 23:46:42 +01001055static DEVICE_ATTR(analog_out, S_IRUGO | S_IWUSR, show_analog_out_reg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 set_analog_out_reg);
1057
Guenter Roeck86aa3e22012-01-14 12:51:15 -08001058static ssize_t show_vid_reg(struct device *dev, struct device_attribute *attr,
1059 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060{
1061 struct adm1026_data *data = adm1026_update_device(dev);
Jean Delvarea0cf3542008-10-17 17:51:20 +02001062 int vid = (data->gpio >> 11) & 0x1f;
1063
1064 dev_dbg(dev, "Setting VID from GPIO11-15.\n");
1065 return sprintf(buf, "%d\n", vid_from_reg(vid, data->vrm));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066}
Guenter Roeck86aa3e22012-01-14 12:51:15 -08001067
Grant Coady937df8d2005-05-12 11:59:29 +10001068static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid_reg, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069
Guenter Roeck86aa3e22012-01-14 12:51:15 -08001070static ssize_t show_vrm_reg(struct device *dev, struct device_attribute *attr,
1071 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072{
Jean Delvare90d66192007-10-08 18:24:35 +02001073 struct adm1026_data *data = dev_get_drvdata(dev);
Jean Delvarecb01a232007-11-29 23:46:42 +01001074 return sprintf(buf, "%d\n", data->vrm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075}
Guenter Roeck86aa3e22012-01-14 12:51:15 -08001076
1077static ssize_t store_vrm_reg(struct device *dev, struct device_attribute *attr,
1078 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079{
Jean Delvaref67fdab2007-12-01 11:24:17 +01001080 struct adm1026_data *data = dev_get_drvdata(dev);
Guenter Roeck86aa3e22012-01-14 12:51:15 -08001081 unsigned long val;
1082 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083
Guenter Roeck86aa3e22012-01-14 12:51:15 -08001084 err = kstrtoul(buf, 10, &val);
1085 if (err)
1086 return err;
1087
1088 data->vrm = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 return count;
1090}
1091
1092static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm_reg, store_vrm_reg);
1093
Guenter Roeck86aa3e22012-01-14 12:51:15 -08001094static ssize_t show_alarms_reg(struct device *dev,
1095 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096{
1097 struct adm1026_data *data = adm1026_update_device(dev);
Jean Delvaref67fdab2007-12-01 11:24:17 +01001098 return sprintf(buf, "%ld\n", data->alarms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099}
1100
1101static DEVICE_ATTR(alarms, S_IRUGO, show_alarms_reg, NULL);
1102
Jean Delvarea9273cb2007-11-29 23:45:22 +01001103static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
1104 char *buf)
1105{
1106 struct adm1026_data *data = adm1026_update_device(dev);
1107 int bitnr = to_sensor_dev_attr(attr)->index;
1108 return sprintf(buf, "%ld\n", (data->alarms >> bitnr) & 1);
1109}
1110
1111static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 0);
1112static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, 1);
1113static SENSOR_DEVICE_ATTR(in9_alarm, S_IRUGO, show_alarm, NULL, 1);
1114static SENSOR_DEVICE_ATTR(in11_alarm, S_IRUGO, show_alarm, NULL, 2);
1115static SENSOR_DEVICE_ATTR(in12_alarm, S_IRUGO, show_alarm, NULL, 3);
1116static SENSOR_DEVICE_ATTR(in13_alarm, S_IRUGO, show_alarm, NULL, 4);
1117static SENSOR_DEVICE_ATTR(in14_alarm, S_IRUGO, show_alarm, NULL, 5);
1118static SENSOR_DEVICE_ATTR(in15_alarm, S_IRUGO, show_alarm, NULL, 6);
1119static SENSOR_DEVICE_ATTR(in16_alarm, S_IRUGO, show_alarm, NULL, 7);
1120static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 8);
1121static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 9);
1122static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 10);
1123static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 11);
1124static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 12);
1125static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 13);
1126static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 14);
1127static SENSOR_DEVICE_ATTR(in7_alarm, S_IRUGO, show_alarm, NULL, 15);
1128static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 16);
1129static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 17);
1130static SENSOR_DEVICE_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL, 18);
1131static SENSOR_DEVICE_ATTR(fan4_alarm, S_IRUGO, show_alarm, NULL, 19);
1132static SENSOR_DEVICE_ATTR(fan5_alarm, S_IRUGO, show_alarm, NULL, 20);
1133static SENSOR_DEVICE_ATTR(fan6_alarm, S_IRUGO, show_alarm, NULL, 21);
1134static SENSOR_DEVICE_ATTR(fan7_alarm, S_IRUGO, show_alarm, NULL, 22);
1135static SENSOR_DEVICE_ATTR(fan8_alarm, S_IRUGO, show_alarm, NULL, 23);
1136static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 24);
1137static SENSOR_DEVICE_ATTR(in10_alarm, S_IRUGO, show_alarm, NULL, 25);
1138static SENSOR_DEVICE_ATTR(in8_alarm, S_IRUGO, show_alarm, NULL, 26);
1139
Guenter Roeck86aa3e22012-01-14 12:51:15 -08001140static ssize_t show_alarm_mask(struct device *dev,
1141 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142{
1143 struct adm1026_data *data = adm1026_update_device(dev);
Jean Delvarecb01a232007-11-29 23:46:42 +01001144 return sprintf(buf, "%ld\n", data->alarm_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145}
Guenter Roeck86aa3e22012-01-14 12:51:15 -08001146static ssize_t set_alarm_mask(struct device *dev, struct device_attribute *attr,
1147 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148{
Axel Linb591d3c2014-07-03 21:53:01 +08001149 struct adm1026_data *data = dev_get_drvdata(dev);
1150 struct i2c_client *client = data->client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 unsigned long mask;
Guenter Roeck86aa3e22012-01-14 12:51:15 -08001152 long val;
1153 int err;
1154
1155 err = kstrtol(buf, 10, &val);
1156 if (err)
1157 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001159 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 data->alarm_mask = val & 0x7fffffff;
1161 mask = data->alarm_mask
1162 | (data->gpio_mask & 0x10000 ? 0x80000000 : 0);
1163 adm1026_write_value(client, ADM1026_REG_MASK1,
1164 mask & 0xff);
1165 mask >>= 8;
1166 adm1026_write_value(client, ADM1026_REG_MASK2,
1167 mask & 0xff);
1168 mask >>= 8;
1169 adm1026_write_value(client, ADM1026_REG_MASK3,
1170 mask & 0xff);
1171 mask >>= 8;
1172 adm1026_write_value(client, ADM1026_REG_MASK4,
1173 mask & 0xff);
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001174 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 return count;
1176}
1177
1178static DEVICE_ATTR(alarm_mask, S_IRUGO | S_IWUSR, show_alarm_mask,
1179 set_alarm_mask);
1180
1181
Guenter Roeck86aa3e22012-01-14 12:51:15 -08001182static ssize_t show_gpio(struct device *dev, struct device_attribute *attr,
1183 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184{
1185 struct adm1026_data *data = adm1026_update_device(dev);
Jean Delvarecb01a232007-11-29 23:46:42 +01001186 return sprintf(buf, "%ld\n", data->gpio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187}
Guenter Roeck86aa3e22012-01-14 12:51:15 -08001188static ssize_t set_gpio(struct device *dev, struct device_attribute *attr,
1189 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190{
Axel Linb591d3c2014-07-03 21:53:01 +08001191 struct adm1026_data *data = dev_get_drvdata(dev);
1192 struct i2c_client *client = data->client;
Jean Delvarecb01a232007-11-29 23:46:42 +01001193 long gpio;
Guenter Roeck86aa3e22012-01-14 12:51:15 -08001194 long val;
1195 int err;
1196
1197 err = kstrtol(buf, 10, &val);
1198 if (err)
1199 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001201 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 data->gpio = val & 0x1ffff;
1203 gpio = data->gpio;
Jean Delvarecb01a232007-11-29 23:46:42 +01001204 adm1026_write_value(client, ADM1026_REG_GPIO_STATUS_0_7, gpio & 0xff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 gpio >>= 8;
Jean Delvarecb01a232007-11-29 23:46:42 +01001206 adm1026_write_value(client, ADM1026_REG_GPIO_STATUS_8_15, gpio & 0xff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 gpio = ((gpio >> 1) & 0x80) | (data->alarms >> 24 & 0x7f);
Jean Delvarecb01a232007-11-29 23:46:42 +01001208 adm1026_write_value(client, ADM1026_REG_STATUS4, gpio & 0xff);
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001209 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 return count;
1211}
1212
1213static DEVICE_ATTR(gpio, S_IRUGO | S_IWUSR, show_gpio, set_gpio);
1214
Guenter Roeck86aa3e22012-01-14 12:51:15 -08001215static ssize_t show_gpio_mask(struct device *dev, struct device_attribute *attr,
1216 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217{
1218 struct adm1026_data *data = adm1026_update_device(dev);
Jean Delvarecb01a232007-11-29 23:46:42 +01001219 return sprintf(buf, "%ld\n", data->gpio_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220}
Guenter Roeck86aa3e22012-01-14 12:51:15 -08001221static ssize_t set_gpio_mask(struct device *dev, struct device_attribute *attr,
1222 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223{
Axel Linb591d3c2014-07-03 21:53:01 +08001224 struct adm1026_data *data = dev_get_drvdata(dev);
1225 struct i2c_client *client = data->client;
Jean Delvarecb01a232007-11-29 23:46:42 +01001226 long mask;
Guenter Roeck86aa3e22012-01-14 12:51:15 -08001227 long val;
1228 int err;
1229
1230 err = kstrtol(buf, 10, &val);
1231 if (err)
1232 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001234 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 data->gpio_mask = val & 0x1ffff;
1236 mask = data->gpio_mask;
Jean Delvarecb01a232007-11-29 23:46:42 +01001237 adm1026_write_value(client, ADM1026_REG_GPIO_MASK_0_7, mask & 0xff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 mask >>= 8;
Jean Delvarecb01a232007-11-29 23:46:42 +01001239 adm1026_write_value(client, ADM1026_REG_GPIO_MASK_8_15, mask & 0xff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 mask = ((mask >> 1) & 0x80) | (data->alarm_mask >> 24 & 0x7f);
Jean Delvarecb01a232007-11-29 23:46:42 +01001241 adm1026_write_value(client, ADM1026_REG_MASK1, mask & 0xff);
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001242 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 return count;
1244}
1245
1246static DEVICE_ATTR(gpio_mask, S_IRUGO | S_IWUSR, show_gpio_mask, set_gpio_mask);
1247
Guenter Roeck86aa3e22012-01-14 12:51:15 -08001248static ssize_t show_pwm_reg(struct device *dev, struct device_attribute *attr,
1249 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250{
1251 struct adm1026_data *data = adm1026_update_device(dev);
Jean Delvarecb01a232007-11-29 23:46:42 +01001252 return sprintf(buf, "%d\n", PWM_FROM_REG(data->pwm1.pwm));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253}
Guenter Roeck86aa3e22012-01-14 12:51:15 -08001254
1255static ssize_t set_pwm_reg(struct device *dev, struct device_attribute *attr,
1256 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257{
Axel Linb591d3c2014-07-03 21:53:01 +08001258 struct adm1026_data *data = dev_get_drvdata(dev);
1259 struct i2c_client *client = data->client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260
1261 if (data->pwm1.enable == 1) {
Guenter Roeck86aa3e22012-01-14 12:51:15 -08001262 long val;
1263 int err;
1264
1265 err = kstrtol(buf, 10, &val);
1266 if (err)
1267 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001269 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 data->pwm1.pwm = PWM_TO_REG(val);
1271 adm1026_write_value(client, ADM1026_REG_PWM, data->pwm1.pwm);
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001272 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 }
1274 return count;
1275}
Guenter Roeck86aa3e22012-01-14 12:51:15 -08001276
1277static ssize_t show_auto_pwm_min(struct device *dev,
1278 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279{
1280 struct adm1026_data *data = adm1026_update_device(dev);
Jean Delvarecb01a232007-11-29 23:46:42 +01001281 return sprintf(buf, "%d\n", data->pwm1.auto_pwm_min);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282}
Guenter Roeck86aa3e22012-01-14 12:51:15 -08001283
1284static ssize_t set_auto_pwm_min(struct device *dev,
1285 struct device_attribute *attr, const char *buf,
1286 size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287{
Axel Linb591d3c2014-07-03 21:53:01 +08001288 struct adm1026_data *data = dev_get_drvdata(dev);
1289 struct i2c_client *client = data->client;
Guenter Roeck86aa3e22012-01-14 12:51:15 -08001290 unsigned long val;
1291 int err;
1292
1293 err = kstrtoul(buf, 10, &val);
1294 if (err)
1295 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001297 mutex_lock(&data->update_lock);
Guenter Roeck2a844c12013-01-09 08:09:34 -08001298 data->pwm1.auto_pwm_min = clamp_val(val, 0, 255);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 if (data->pwm1.enable == 2) { /* apply immediately */
1300 data->pwm1.pwm = PWM_TO_REG((data->pwm1.pwm & 0x0f) |
Jean Delvarecb01a232007-11-29 23:46:42 +01001301 PWM_MIN_TO_REG(data->pwm1.auto_pwm_min));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 adm1026_write_value(client, ADM1026_REG_PWM, data->pwm1.pwm);
1303 }
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001304 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 return count;
1306}
Guenter Roeck86aa3e22012-01-14 12:51:15 -08001307
1308static ssize_t show_auto_pwm_max(struct device *dev,
1309 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310{
Jean Delvarecb01a232007-11-29 23:46:42 +01001311 return sprintf(buf, "%d\n", ADM1026_PWM_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312}
Guenter Roeck86aa3e22012-01-14 12:51:15 -08001313
1314static ssize_t show_pwm_enable(struct device *dev,
1315 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316{
1317 struct adm1026_data *data = adm1026_update_device(dev);
Jean Delvarecb01a232007-11-29 23:46:42 +01001318 return sprintf(buf, "%d\n", data->pwm1.enable);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319}
Guenter Roeck86aa3e22012-01-14 12:51:15 -08001320
1321static ssize_t set_pwm_enable(struct device *dev, struct device_attribute *attr,
1322 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323{
Axel Linb591d3c2014-07-03 21:53:01 +08001324 struct adm1026_data *data = dev_get_drvdata(dev);
1325 struct i2c_client *client = data->client;
Jean Delvarecb01a232007-11-29 23:46:42 +01001326 int old_enable;
Guenter Roeck86aa3e22012-01-14 12:51:15 -08001327 unsigned long val;
1328 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329
Guenter Roeck86aa3e22012-01-14 12:51:15 -08001330 err = kstrtoul(buf, 10, &val);
1331 if (err)
1332 return err;
1333
1334 if (val >= 3)
1335 return -EINVAL;
1336
1337 mutex_lock(&data->update_lock);
1338 old_enable = data->pwm1.enable;
1339 data->pwm1.enable = val;
1340 data->config1 = (data->config1 & ~CFG1_PWM_AFC)
1341 | ((val == 2) ? CFG1_PWM_AFC : 0);
1342 adm1026_write_value(client, ADM1026_REG_CONFIG1, data->config1);
1343 if (val == 2) { /* apply pwm1_auto_pwm_min to pwm1 */
1344 data->pwm1.pwm = PWM_TO_REG((data->pwm1.pwm & 0x0f) |
1345 PWM_MIN_TO_REG(data->pwm1.auto_pwm_min));
1346 adm1026_write_value(client, ADM1026_REG_PWM, data->pwm1.pwm);
1347 } else if (!((old_enable == 1) && (val == 1))) {
1348 /* set pwm to safe value */
1349 data->pwm1.pwm = 255;
1350 adm1026_write_value(client, ADM1026_REG_PWM, data->pwm1.pwm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 }
Guenter Roeck86aa3e22012-01-14 12:51:15 -08001352 mutex_unlock(&data->update_lock);
1353
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 return count;
1355}
1356
1357/* enable PWM fan control */
Jean Delvarecb01a232007-11-29 23:46:42 +01001358static DEVICE_ATTR(pwm1, S_IRUGO | S_IWUSR, show_pwm_reg, set_pwm_reg);
1359static DEVICE_ATTR(pwm2, S_IRUGO | S_IWUSR, show_pwm_reg, set_pwm_reg);
1360static DEVICE_ATTR(pwm3, S_IRUGO | S_IWUSR, show_pwm_reg, set_pwm_reg);
1361static DEVICE_ATTR(pwm1_enable, S_IRUGO | S_IWUSR, show_pwm_enable,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 set_pwm_enable);
Jean Delvarecb01a232007-11-29 23:46:42 +01001363static DEVICE_ATTR(pwm2_enable, S_IRUGO | S_IWUSR, show_pwm_enable,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 set_pwm_enable);
Jean Delvarecb01a232007-11-29 23:46:42 +01001365static DEVICE_ATTR(pwm3_enable, S_IRUGO | S_IWUSR, show_pwm_enable,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 set_pwm_enable);
Jean Delvarecb01a232007-11-29 23:46:42 +01001367static DEVICE_ATTR(temp1_auto_point1_pwm, S_IRUGO | S_IWUSR,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 show_auto_pwm_min, set_auto_pwm_min);
Jean Delvarecb01a232007-11-29 23:46:42 +01001369static DEVICE_ATTR(temp2_auto_point1_pwm, S_IRUGO | S_IWUSR,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 show_auto_pwm_min, set_auto_pwm_min);
Jean Delvarecb01a232007-11-29 23:46:42 +01001371static DEVICE_ATTR(temp3_auto_point1_pwm, S_IRUGO | S_IWUSR,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 show_auto_pwm_min, set_auto_pwm_min);
1373
1374static DEVICE_ATTR(temp1_auto_point2_pwm, S_IRUGO, show_auto_pwm_max, NULL);
1375static DEVICE_ATTR(temp2_auto_point2_pwm, S_IRUGO, show_auto_pwm_max, NULL);
1376static DEVICE_ATTR(temp3_auto_point2_pwm, S_IRUGO, show_auto_pwm_max, NULL);
1377
Mark M. Hoffman681c6f72006-09-24 21:15:35 +02001378static struct attribute *adm1026_attributes[] = {
1379 &sensor_dev_attr_in0_input.dev_attr.attr,
1380 &sensor_dev_attr_in0_max.dev_attr.attr,
1381 &sensor_dev_attr_in0_min.dev_attr.attr,
Jean Delvarea9273cb2007-11-29 23:45:22 +01001382 &sensor_dev_attr_in0_alarm.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +02001383 &sensor_dev_attr_in1_input.dev_attr.attr,
1384 &sensor_dev_attr_in1_max.dev_attr.attr,
1385 &sensor_dev_attr_in1_min.dev_attr.attr,
Jean Delvarea9273cb2007-11-29 23:45:22 +01001386 &sensor_dev_attr_in1_alarm.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +02001387 &sensor_dev_attr_in2_input.dev_attr.attr,
1388 &sensor_dev_attr_in2_max.dev_attr.attr,
1389 &sensor_dev_attr_in2_min.dev_attr.attr,
Jean Delvarea9273cb2007-11-29 23:45:22 +01001390 &sensor_dev_attr_in2_alarm.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +02001391 &sensor_dev_attr_in3_input.dev_attr.attr,
1392 &sensor_dev_attr_in3_max.dev_attr.attr,
1393 &sensor_dev_attr_in3_min.dev_attr.attr,
Jean Delvarea9273cb2007-11-29 23:45:22 +01001394 &sensor_dev_attr_in3_alarm.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +02001395 &sensor_dev_attr_in4_input.dev_attr.attr,
1396 &sensor_dev_attr_in4_max.dev_attr.attr,
1397 &sensor_dev_attr_in4_min.dev_attr.attr,
Jean Delvarea9273cb2007-11-29 23:45:22 +01001398 &sensor_dev_attr_in4_alarm.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +02001399 &sensor_dev_attr_in5_input.dev_attr.attr,
1400 &sensor_dev_attr_in5_max.dev_attr.attr,
1401 &sensor_dev_attr_in5_min.dev_attr.attr,
Jean Delvarea9273cb2007-11-29 23:45:22 +01001402 &sensor_dev_attr_in5_alarm.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +02001403 &sensor_dev_attr_in6_input.dev_attr.attr,
1404 &sensor_dev_attr_in6_max.dev_attr.attr,
1405 &sensor_dev_attr_in6_min.dev_attr.attr,
Jean Delvarea9273cb2007-11-29 23:45:22 +01001406 &sensor_dev_attr_in6_alarm.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +02001407 &sensor_dev_attr_in7_input.dev_attr.attr,
1408 &sensor_dev_attr_in7_max.dev_attr.attr,
1409 &sensor_dev_attr_in7_min.dev_attr.attr,
Jean Delvarea9273cb2007-11-29 23:45:22 +01001410 &sensor_dev_attr_in7_alarm.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +02001411 &sensor_dev_attr_in10_input.dev_attr.attr,
1412 &sensor_dev_attr_in10_max.dev_attr.attr,
1413 &sensor_dev_attr_in10_min.dev_attr.attr,
Jean Delvarea9273cb2007-11-29 23:45:22 +01001414 &sensor_dev_attr_in10_alarm.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +02001415 &sensor_dev_attr_in11_input.dev_attr.attr,
1416 &sensor_dev_attr_in11_max.dev_attr.attr,
1417 &sensor_dev_attr_in11_min.dev_attr.attr,
Jean Delvarea9273cb2007-11-29 23:45:22 +01001418 &sensor_dev_attr_in11_alarm.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +02001419 &sensor_dev_attr_in12_input.dev_attr.attr,
1420 &sensor_dev_attr_in12_max.dev_attr.attr,
1421 &sensor_dev_attr_in12_min.dev_attr.attr,
Jean Delvarea9273cb2007-11-29 23:45:22 +01001422 &sensor_dev_attr_in12_alarm.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +02001423 &sensor_dev_attr_in13_input.dev_attr.attr,
1424 &sensor_dev_attr_in13_max.dev_attr.attr,
1425 &sensor_dev_attr_in13_min.dev_attr.attr,
Jean Delvarea9273cb2007-11-29 23:45:22 +01001426 &sensor_dev_attr_in13_alarm.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +02001427 &sensor_dev_attr_in14_input.dev_attr.attr,
1428 &sensor_dev_attr_in14_max.dev_attr.attr,
1429 &sensor_dev_attr_in14_min.dev_attr.attr,
Jean Delvarea9273cb2007-11-29 23:45:22 +01001430 &sensor_dev_attr_in14_alarm.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +02001431 &sensor_dev_attr_in15_input.dev_attr.attr,
1432 &sensor_dev_attr_in15_max.dev_attr.attr,
1433 &sensor_dev_attr_in15_min.dev_attr.attr,
Jean Delvarea9273cb2007-11-29 23:45:22 +01001434 &sensor_dev_attr_in15_alarm.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +02001435 &sensor_dev_attr_in16_input.dev_attr.attr,
1436 &sensor_dev_attr_in16_max.dev_attr.attr,
1437 &sensor_dev_attr_in16_min.dev_attr.attr,
Jean Delvarea9273cb2007-11-29 23:45:22 +01001438 &sensor_dev_attr_in16_alarm.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +02001439 &sensor_dev_attr_fan1_input.dev_attr.attr,
1440 &sensor_dev_attr_fan1_div.dev_attr.attr,
1441 &sensor_dev_attr_fan1_min.dev_attr.attr,
Jean Delvarea9273cb2007-11-29 23:45:22 +01001442 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +02001443 &sensor_dev_attr_fan2_input.dev_attr.attr,
1444 &sensor_dev_attr_fan2_div.dev_attr.attr,
1445 &sensor_dev_attr_fan2_min.dev_attr.attr,
Jean Delvarea9273cb2007-11-29 23:45:22 +01001446 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +02001447 &sensor_dev_attr_fan3_input.dev_attr.attr,
1448 &sensor_dev_attr_fan3_div.dev_attr.attr,
1449 &sensor_dev_attr_fan3_min.dev_attr.attr,
Jean Delvarea9273cb2007-11-29 23:45:22 +01001450 &sensor_dev_attr_fan3_alarm.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +02001451 &sensor_dev_attr_fan4_input.dev_attr.attr,
1452 &sensor_dev_attr_fan4_div.dev_attr.attr,
1453 &sensor_dev_attr_fan4_min.dev_attr.attr,
Jean Delvarea9273cb2007-11-29 23:45:22 +01001454 &sensor_dev_attr_fan4_alarm.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +02001455 &sensor_dev_attr_fan5_input.dev_attr.attr,
1456 &sensor_dev_attr_fan5_div.dev_attr.attr,
1457 &sensor_dev_attr_fan5_min.dev_attr.attr,
Jean Delvarea9273cb2007-11-29 23:45:22 +01001458 &sensor_dev_attr_fan5_alarm.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +02001459 &sensor_dev_attr_fan6_input.dev_attr.attr,
1460 &sensor_dev_attr_fan6_div.dev_attr.attr,
1461 &sensor_dev_attr_fan6_min.dev_attr.attr,
Jean Delvarea9273cb2007-11-29 23:45:22 +01001462 &sensor_dev_attr_fan6_alarm.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +02001463 &sensor_dev_attr_fan7_input.dev_attr.attr,
1464 &sensor_dev_attr_fan7_div.dev_attr.attr,
1465 &sensor_dev_attr_fan7_min.dev_attr.attr,
Jean Delvarea9273cb2007-11-29 23:45:22 +01001466 &sensor_dev_attr_fan7_alarm.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +02001467 &sensor_dev_attr_fan8_input.dev_attr.attr,
1468 &sensor_dev_attr_fan8_div.dev_attr.attr,
1469 &sensor_dev_attr_fan8_min.dev_attr.attr,
Jean Delvarea9273cb2007-11-29 23:45:22 +01001470 &sensor_dev_attr_fan8_alarm.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +02001471 &sensor_dev_attr_temp1_input.dev_attr.attr,
1472 &sensor_dev_attr_temp1_max.dev_attr.attr,
1473 &sensor_dev_attr_temp1_min.dev_attr.attr,
Jean Delvarea9273cb2007-11-29 23:45:22 +01001474 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +02001475 &sensor_dev_attr_temp2_input.dev_attr.attr,
1476 &sensor_dev_attr_temp2_max.dev_attr.attr,
1477 &sensor_dev_attr_temp2_min.dev_attr.attr,
Jean Delvarea9273cb2007-11-29 23:45:22 +01001478 &sensor_dev_attr_temp2_alarm.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +02001479 &sensor_dev_attr_temp1_offset.dev_attr.attr,
1480 &sensor_dev_attr_temp2_offset.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +02001481 &sensor_dev_attr_temp1_auto_point1_temp.dev_attr.attr,
1482 &sensor_dev_attr_temp2_auto_point1_temp.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +02001483 &sensor_dev_attr_temp1_auto_point1_temp_hyst.dev_attr.attr,
1484 &sensor_dev_attr_temp2_auto_point1_temp_hyst.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +02001485 &sensor_dev_attr_temp1_auto_point2_temp.dev_attr.attr,
1486 &sensor_dev_attr_temp2_auto_point2_temp.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +02001487 &sensor_dev_attr_temp1_crit.dev_attr.attr,
1488 &sensor_dev_attr_temp2_crit.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +02001489 &dev_attr_temp1_crit_enable.attr,
1490 &dev_attr_temp2_crit_enable.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +02001491 &dev_attr_cpu0_vid.attr,
1492 &dev_attr_vrm.attr,
1493 &dev_attr_alarms.attr,
1494 &dev_attr_alarm_mask.attr,
1495 &dev_attr_gpio.attr,
1496 &dev_attr_gpio_mask.attr,
1497 &dev_attr_pwm1.attr,
1498 &dev_attr_pwm2.attr,
1499 &dev_attr_pwm3.attr,
1500 &dev_attr_pwm1_enable.attr,
1501 &dev_attr_pwm2_enable.attr,
1502 &dev_attr_pwm3_enable.attr,
1503 &dev_attr_temp1_auto_point1_pwm.attr,
1504 &dev_attr_temp2_auto_point1_pwm.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +02001505 &dev_attr_temp1_auto_point2_pwm.attr,
1506 &dev_attr_temp2_auto_point2_pwm.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +02001507 &dev_attr_analog_out.attr,
1508 NULL
1509};
1510
1511static const struct attribute_group adm1026_group = {
1512 .attrs = adm1026_attributes,
1513};
1514
Jean Delvare5b34dbc2007-11-29 23:47:54 +01001515static struct attribute *adm1026_attributes_temp3[] = {
1516 &sensor_dev_attr_temp3_input.dev_attr.attr,
1517 &sensor_dev_attr_temp3_max.dev_attr.attr,
1518 &sensor_dev_attr_temp3_min.dev_attr.attr,
1519 &sensor_dev_attr_temp3_alarm.dev_attr.attr,
1520 &sensor_dev_attr_temp3_offset.dev_attr.attr,
1521 &sensor_dev_attr_temp3_auto_point1_temp.dev_attr.attr,
1522 &sensor_dev_attr_temp3_auto_point1_temp_hyst.dev_attr.attr,
1523 &sensor_dev_attr_temp3_auto_point2_temp.dev_attr.attr,
1524 &sensor_dev_attr_temp3_crit.dev_attr.attr,
1525 &dev_attr_temp3_crit_enable.attr,
1526 &dev_attr_temp3_auto_point1_pwm.attr,
1527 &dev_attr_temp3_auto_point2_pwm.attr,
Jean Delvare1d5f2c12008-02-10 19:01:15 +01001528 NULL
Jean Delvare5b34dbc2007-11-29 23:47:54 +01001529};
1530
1531static const struct attribute_group adm1026_group_temp3 = {
1532 .attrs = adm1026_attributes_temp3,
1533};
1534
1535static struct attribute *adm1026_attributes_in8_9[] = {
1536 &sensor_dev_attr_in8_input.dev_attr.attr,
1537 &sensor_dev_attr_in8_max.dev_attr.attr,
1538 &sensor_dev_attr_in8_min.dev_attr.attr,
1539 &sensor_dev_attr_in8_alarm.dev_attr.attr,
1540 &sensor_dev_attr_in9_input.dev_attr.attr,
1541 &sensor_dev_attr_in9_max.dev_attr.attr,
1542 &sensor_dev_attr_in9_min.dev_attr.attr,
1543 &sensor_dev_attr_in9_alarm.dev_attr.attr,
Jean Delvare1d5f2c12008-02-10 19:01:15 +01001544 NULL
Jean Delvare5b34dbc2007-11-29 23:47:54 +01001545};
1546
1547static const struct attribute_group adm1026_group_in8_9 = {
1548 .attrs = adm1026_attributes_in8_9,
1549};
1550
Jean Delvare57f7eb02008-07-16 19:30:08 +02001551/* Return 0 if detection is successful, -ENODEV otherwise */
Jean Delvare310ec792009-12-14 21:17:23 +01001552static int adm1026_detect(struct i2c_client *client,
Jean Delvare57f7eb02008-07-16 19:30:08 +02001553 struct i2c_board_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554{
Jean Delvare57f7eb02008-07-16 19:30:08 +02001555 struct i2c_adapter *adapter = client->adapter;
1556 int address = client->addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 int company, verstep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558
1559 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
1560 /* We need to be able to do byte I/O */
Jean Delvare57f7eb02008-07-16 19:30:08 +02001561 return -ENODEV;
Guenter Roecka0393712013-03-29 13:54:50 -07001562 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 /* Now, we do the remaining detection. */
1565
Jean Delvaref67fdab2007-12-01 11:24:17 +01001566 company = adm1026_read_value(client, ADM1026_REG_COMPANY);
1567 verstep = adm1026_read_value(client, ADM1026_REG_VERSTEP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568
Guenter Roeckb55f3752013-01-10 10:01:24 -08001569 dev_dbg(&adapter->dev,
1570 "Detecting device at %d,0x%02x with COMPANY: 0x%02x and VERSTEP: 0x%02x\n",
Jean Delvaref67fdab2007-12-01 11:24:17 +01001571 i2c_adapter_id(client->adapter), client->addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 company, verstep);
1573
Jean Delvare52df6442009-12-09 20:35:57 +01001574 /* Determine the chip type. */
1575 dev_dbg(&adapter->dev, "Autodetecting device at %d,0x%02x...\n",
1576 i2c_adapter_id(adapter), address);
1577 if (company == ADM1026_COMPANY_ANALOG_DEV
1578 && verstep == ADM1026_VERSTEP_ADM1026) {
1579 /* Analog Devices ADM1026 */
1580 } else if (company == ADM1026_COMPANY_ANALOG_DEV
1581 && (verstep & 0xf0) == ADM1026_VERSTEP_GENERIC) {
Guenter Roeckb55f3752013-01-10 10:01:24 -08001582 dev_err(&adapter->dev,
1583 "Unrecognized stepping 0x%02x. Defaulting to ADM1026.\n",
1584 verstep);
Jean Delvare52df6442009-12-09 20:35:57 +01001585 } else if ((verstep & 0xf0) == ADM1026_VERSTEP_GENERIC) {
Guenter Roeckb55f3752013-01-10 10:01:24 -08001586 dev_err(&adapter->dev,
1587 "Found version/stepping 0x%02x. Assuming generic ADM1026.\n",
Jean Delvare52df6442009-12-09 20:35:57 +01001588 verstep);
1589 } else {
1590 dev_dbg(&adapter->dev, "Autodetection failed\n");
1591 /* Not an ADM1026... */
1592 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 }
Jean Delvare52df6442009-12-09 20:35:57 +01001594
Jean Delvare57f7eb02008-07-16 19:30:08 +02001595 strlcpy(info->type, "adm1026", I2C_NAME_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596
Jean Delvare57f7eb02008-07-16 19:30:08 +02001597 return 0;
1598}
1599
Axel Lin97df5562014-07-03 21:51:43 +08001600static void adm1026_print_gpio(struct i2c_client *client)
1601{
1602 struct adm1026_data *data = i2c_get_clientdata(client);
1603 int i;
1604
1605 dev_dbg(&client->dev, "GPIO config is:\n");
1606 for (i = 0; i <= 7; ++i) {
1607 if (data->config2 & (1 << i)) {
1608 dev_dbg(&client->dev, "\t%sGP%s%d\n",
1609 data->gpio_config[i] & 0x02 ? "" : "!",
1610 data->gpio_config[i] & 0x01 ? "OUT" : "IN",
1611 i);
1612 } else {
1613 dev_dbg(&client->dev, "\tFAN%d\n", i);
1614 }
1615 }
1616 for (i = 8; i <= 15; ++i) {
1617 dev_dbg(&client->dev, "\t%sGP%s%d\n",
1618 data->gpio_config[i] & 0x02 ? "" : "!",
1619 data->gpio_config[i] & 0x01 ? "OUT" : "IN",
1620 i);
1621 }
1622 if (data->config3 & CFG3_GPIO16_ENABLE) {
1623 dev_dbg(&client->dev, "\t%sGP%s16\n",
1624 data->gpio_config[16] & 0x02 ? "" : "!",
1625 data->gpio_config[16] & 0x01 ? "OUT" : "IN");
1626 } else {
1627 /* GPIO16 is THERM */
1628 dev_dbg(&client->dev, "\tTHERM\n");
1629 }
1630}
1631
1632static void adm1026_fixup_gpio(struct i2c_client *client)
1633{
1634 struct adm1026_data *data = i2c_get_clientdata(client);
1635 int i;
1636 int value;
1637
1638 /* Make the changes requested. */
1639 /*
1640 * We may need to unlock/stop monitoring or soft-reset the
1641 * chip before we can make changes. This hasn't been
1642 * tested much. FIXME
1643 */
1644
1645 /* Make outputs */
1646 for (i = 0; i <= 16; ++i) {
1647 if (gpio_output[i] >= 0 && gpio_output[i] <= 16)
1648 data->gpio_config[gpio_output[i]] |= 0x01;
1649 /* if GPIO0-7 is output, it isn't a FAN tach */
1650 if (gpio_output[i] >= 0 && gpio_output[i] <= 7)
1651 data->config2 |= 1 << gpio_output[i];
1652 }
1653
1654 /* Input overrides output */
1655 for (i = 0; i <= 16; ++i) {
1656 if (gpio_input[i] >= 0 && gpio_input[i] <= 16)
1657 data->gpio_config[gpio_input[i]] &= ~0x01;
1658 /* if GPIO0-7 is input, it isn't a FAN tach */
1659 if (gpio_input[i] >= 0 && gpio_input[i] <= 7)
1660 data->config2 |= 1 << gpio_input[i];
1661 }
1662
1663 /* Inverted */
1664 for (i = 0; i <= 16; ++i) {
1665 if (gpio_inverted[i] >= 0 && gpio_inverted[i] <= 16)
1666 data->gpio_config[gpio_inverted[i]] &= ~0x02;
1667 }
1668
1669 /* Normal overrides inverted */
1670 for (i = 0; i <= 16; ++i) {
1671 if (gpio_normal[i] >= 0 && gpio_normal[i] <= 16)
1672 data->gpio_config[gpio_normal[i]] |= 0x02;
1673 }
1674
1675 /* Fan overrides input and output */
1676 for (i = 0; i <= 7; ++i) {
1677 if (gpio_fan[i] >= 0 && gpio_fan[i] <= 7)
1678 data->config2 &= ~(1 << gpio_fan[i]);
1679 }
1680
1681 /* Write new configs to registers */
1682 adm1026_write_value(client, ADM1026_REG_CONFIG2, data->config2);
1683 data->config3 = (data->config3 & 0x3f)
1684 | ((data->gpio_config[16] & 0x03) << 6);
1685 adm1026_write_value(client, ADM1026_REG_CONFIG3, data->config3);
1686 for (i = 15, value = 0; i >= 0; --i) {
1687 value <<= 2;
1688 value |= data->gpio_config[i] & 0x03;
1689 if ((i & 0x03) == 0) {
1690 adm1026_write_value(client,
1691 ADM1026_REG_GPIO_CFG_0_3 + i/4,
1692 value);
1693 value = 0;
1694 }
1695 }
1696
1697 /* Print the new config */
1698 adm1026_print_gpio(client);
1699}
1700
1701static void adm1026_init_client(struct i2c_client *client)
1702{
1703 int value, i;
1704 struct adm1026_data *data = i2c_get_clientdata(client);
1705
1706 dev_dbg(&client->dev, "Initializing device\n");
1707 /* Read chip config */
1708 data->config1 = adm1026_read_value(client, ADM1026_REG_CONFIG1);
1709 data->config2 = adm1026_read_value(client, ADM1026_REG_CONFIG2);
1710 data->config3 = adm1026_read_value(client, ADM1026_REG_CONFIG3);
1711
1712 /* Inform user of chip config */
1713 dev_dbg(&client->dev, "ADM1026_REG_CONFIG1 is: 0x%02x\n",
1714 data->config1);
1715 if ((data->config1 & CFG1_MONITOR) == 0) {
1716 dev_dbg(&client->dev,
1717 "Monitoring not currently enabled.\n");
1718 }
1719 if (data->config1 & CFG1_INT_ENABLE) {
1720 dev_dbg(&client->dev,
1721 "SMBALERT interrupts are enabled.\n");
1722 }
1723 if (data->config1 & CFG1_AIN8_9) {
1724 dev_dbg(&client->dev,
1725 "in8 and in9 enabled. temp3 disabled.\n");
1726 } else {
1727 dev_dbg(&client->dev,
1728 "temp3 enabled. in8 and in9 disabled.\n");
1729 }
1730 if (data->config1 & CFG1_THERM_HOT) {
1731 dev_dbg(&client->dev,
1732 "Automatic THERM, PWM, and temp limits enabled.\n");
1733 }
1734
1735 if (data->config3 & CFG3_GPIO16_ENABLE) {
1736 dev_dbg(&client->dev,
1737 "GPIO16 enabled. THERM pin disabled.\n");
1738 } else {
1739 dev_dbg(&client->dev,
1740 "THERM pin enabled. GPIO16 disabled.\n");
1741 }
1742 if (data->config3 & CFG3_VREF_250)
1743 dev_dbg(&client->dev, "Vref is 2.50 Volts.\n");
1744 else
1745 dev_dbg(&client->dev, "Vref is 1.82 Volts.\n");
1746 /* Read and pick apart the existing GPIO configuration */
1747 value = 0;
1748 for (i = 0; i <= 15; ++i) {
1749 if ((i & 0x03) == 0) {
1750 value = adm1026_read_value(client,
1751 ADM1026_REG_GPIO_CFG_0_3 + i / 4);
1752 }
1753 data->gpio_config[i] = value & 0x03;
1754 value >>= 2;
1755 }
1756 data->gpio_config[16] = (data->config3 >> 6) & 0x03;
1757
1758 /* ... and then print it */
1759 adm1026_print_gpio(client);
1760
1761 /*
1762 * If the user asks us to reprogram the GPIO config, then
1763 * do it now.
1764 */
1765 if (gpio_input[0] != -1 || gpio_output[0] != -1
1766 || gpio_inverted[0] != -1 || gpio_normal[0] != -1
1767 || gpio_fan[0] != -1) {
1768 adm1026_fixup_gpio(client);
1769 }
1770
1771 /*
1772 * WE INTENTIONALLY make no changes to the limits,
1773 * offsets, pwms, fans and zones. If they were
1774 * configured, we don't want to mess with them.
1775 * If they weren't, the default is 100% PWM, no
1776 * control and will suffice until 'sensors -s'
1777 * can be run by the user. We DO set the default
1778 * value for pwm1.auto_pwm_min to its maximum
1779 * so that enabling automatic pwm fan control
1780 * without first setting a value for pwm1.auto_pwm_min
1781 * will not result in potentially dangerous fan speed decrease.
1782 */
1783 data->pwm1.auto_pwm_min = 255;
1784 /* Start monitoring */
1785 value = adm1026_read_value(client, ADM1026_REG_CONFIG1);
1786 /* Set MONITOR, clear interrupt acknowledge and s/w reset */
1787 value = (value | CFG1_MONITOR) & (~CFG1_INT_CLEAR & ~CFG1_RESET);
1788 dev_dbg(&client->dev, "Setting CONFIG to: 0x%02x\n", value);
1789 data->config1 = value;
1790 adm1026_write_value(client, ADM1026_REG_CONFIG1, value);
1791
1792 /* initialize fan_div[] to hardware defaults */
1793 value = adm1026_read_value(client, ADM1026_REG_FAN_DIV_0_3) |
1794 (adm1026_read_value(client, ADM1026_REG_FAN_DIV_4_7) << 8);
1795 for (i = 0; i <= 7; ++i) {
1796 data->fan_div[i] = DIV_FROM_REG(value & 0x03);
1797 value >>= 2;
1798 }
1799}
1800
Jean Delvare57f7eb02008-07-16 19:30:08 +02001801static int adm1026_probe(struct i2c_client *client,
1802 const struct i2c_device_id *id)
1803{
Axel Linb591d3c2014-07-03 21:53:01 +08001804 struct device *dev = &client->dev;
1805 struct device *hwmon_dev;
Jean Delvare57f7eb02008-07-16 19:30:08 +02001806 struct adm1026_data *data;
Jean Delvare57f7eb02008-07-16 19:30:08 +02001807
Axel Linb591d3c2014-07-03 21:53:01 +08001808 data = devm_kzalloc(dev, sizeof(struct adm1026_data), GFP_KERNEL);
Guenter Roeck3421e212012-06-02 09:57:59 -07001809 if (!data)
1810 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811
Jean Delvare57f7eb02008-07-16 19:30:08 +02001812 i2c_set_clientdata(client, data);
Axel Linb591d3c2014-07-03 21:53:01 +08001813 data->client = client;
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001814 mutex_init(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 /* Set the VRM version */
Jean Delvare303760b2005-07-31 21:52:01 +02001817 data->vrm = vid_which_vrm();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818
1819 /* Initialize the ADM1026 chip */
Jean Delvaref67fdab2007-12-01 11:24:17 +01001820 adm1026_init_client(client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821
Axel Linb591d3c2014-07-03 21:53:01 +08001822 /* sysfs hooks */
1823 data->groups[0] = &adm1026_group;
Jean Delvare5b34dbc2007-11-29 23:47:54 +01001824 if (data->config1 & CFG1_AIN8_9)
Axel Linb591d3c2014-07-03 21:53:01 +08001825 data->groups[1] = &adm1026_group_in8_9;
Jean Delvare5b34dbc2007-11-29 23:47:54 +01001826 else
Axel Linb591d3c2014-07-03 21:53:01 +08001827 data->groups[1] = &adm1026_group_temp3;
Mark M. Hoffman681c6f72006-09-24 21:15:35 +02001828
Axel Linb591d3c2014-07-03 21:53:01 +08001829 hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
1830 data, data->groups);
1831 return PTR_ERR_OR_ZERO(hwmon_dev);
Mark M. Hoffman681c6f72006-09-24 21:15:35 +02001832}
1833
Axel Lin97df5562014-07-03 21:51:43 +08001834static const struct i2c_device_id adm1026_id[] = {
1835 { "adm1026", 0 },
1836 { }
1837};
1838MODULE_DEVICE_TABLE(i2c, adm1026_id);
1839
1840static struct i2c_driver adm1026_driver = {
1841 .class = I2C_CLASS_HWMON,
1842 .driver = {
1843 .name = "adm1026",
1844 },
1845 .probe = adm1026_probe,
Axel Lin97df5562014-07-03 21:51:43 +08001846 .id_table = adm1026_id,
1847 .detect = adm1026_detect,
1848 .address_list = normal_i2c,
1849};
1850
Axel Linf0967ee2012-01-20 15:38:18 +08001851module_i2c_driver(adm1026_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852
1853MODULE_LICENSE("GPL");
1854MODULE_AUTHOR("Philip Pokorny <ppokorny@penguincomputing.com>, "
Jean Delvarecb01a232007-11-29 23:46:42 +01001855 "Justin Thiessen <jthiessen@penguincomputing.com>");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856MODULE_DESCRIPTION("ADM1026 driver");