blob: e43f09a07cd025ad032e88029cf5ec6beb37d55c [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 Roeckf1b9baa2016-12-03 11:08:07 -0800200#define INS_TO_REG(n, val) \
201 SCALE(clamp_val(val, 0, 255 * adm1026_scaling[n] / 192), \
202 adm1026_scaling[n], 192)
Jean Delvarecb01a232007-11-29 23:46:42 +0100203#define INS_FROM_REG(n, val) (SCALE(val, 192, adm1026_scaling[n]))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
Guenter Roeck86aa3e22012-01-14 12:51:15 -0800205/*
206 * FAN speed is measured using 22.5kHz clock and counts for 2 pulses
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 * and we assume a 2 pulse-per-rev fan tach signal
208 * 22500 kHz * 60 (sec/min) * 2 (pulse) / 2 (pulse/rev) == 1350000
209 */
Jean Delvarecb01a232007-11-29 23:46:42 +0100210#define FAN_TO_REG(val, div) ((val) <= 0 ? 0xff : \
Guenter Roeck2a844c12013-01-09 08:09:34 -0800211 clamp_val(1350000 / ((val) * (div)), \
Guenter Roeck86aa3e22012-01-14 12:51:15 -0800212 1, 254))
213#define FAN_FROM_REG(val, div) ((val) == 0 ? -1 : (val) == 0xff ? 0 : \
214 1350000 / ((val) * (div)))
215#define DIV_FROM_REG(val) (1 << (val))
Jean Delvarecb01a232007-11-29 23:46:42 +0100216#define DIV_TO_REG(val) ((val) >= 8 ? 3 : (val) >= 4 ? 2 : (val) >= 2 ? 1 : 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217
218/* Temperature is reported in 1 degC increments */
Guenter Roeckf1b9baa2016-12-03 11:08:07 -0800219#define TEMP_TO_REG(val) DIV_ROUND_CLOSEST(clamp_val(val, -128000, 127000), \
220 1000)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221#define TEMP_FROM_REG(val) ((val) * 1000)
Guenter Roeckf1b9baa2016-12-03 11:08:07 -0800222#define OFFSET_TO_REG(val) DIV_ROUND_CLOSEST(clamp_val(val, -128000, 127000), \
223 1000)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224#define OFFSET_FROM_REG(val) ((val) * 1000)
225
Guenter Roeck2a844c12013-01-09 08:09:34 -0800226#define PWM_TO_REG(val) (clamp_val(val, 0, 255))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227#define PWM_FROM_REG(val) (val)
228
229#define PWM_MIN_TO_REG(val) ((val) & 0xf0)
230#define PWM_MIN_FROM_REG(val) (((val) & 0xf0) + ((val) >> 4))
231
Guenter Roeck86aa3e22012-01-14 12:51:15 -0800232/*
233 * Analog output is a voltage, and scaled to millivolts. The datasheet
Jean Delvarecb01a232007-11-29 23:46:42 +0100234 * indicates that the DAC could be used to drive the fans, but in our
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 * example board (Arima HDAMA) it isn't connected to the fans at all.
236 */
Guenter Roeckf1b9baa2016-12-03 11:08:07 -0800237#define DAC_TO_REG(val) DIV_ROUND_CLOSEST(clamp_val(val, 0, 2500) * 255, \
238 2500)
Guenter Roeck86aa3e22012-01-14 12:51:15 -0800239#define DAC_FROM_REG(val) (((val) * 2500) / 255)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
Guenter Roeck86aa3e22012-01-14 12:51:15 -0800241/*
242 * Chip sampling rates
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 *
244 * Some sensors are not updated more frequently than once per second
245 * so it doesn't make sense to read them more often than that.
246 * We cache the results and return the saved data if the driver
247 * is called again before a second has elapsed.
248 *
249 * Also, there is significant configuration data for this chip
250 * So, we keep the config data up to date in the cache
251 * when it is written and only sample it once every 5 *minutes*
252 */
Jean Delvarecb01a232007-11-29 23:46:42 +0100253#define ADM1026_DATA_INTERVAL (1 * HZ)
254#define ADM1026_CONFIG_INTERVAL (5 * 60 * HZ)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
Guenter Roeck86aa3e22012-01-14 12:51:15 -0800256/*
257 * We allow for multiple chips in a single system.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 *
259 * For each registered ADM1026, we need to keep state information
260 * at client->data. The adm1026_data structure is dynamically
Guenter Roeck86aa3e22012-01-14 12:51:15 -0800261 * allocated, when a new client structure is allocated.
262 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
264struct pwm_data {
265 u8 pwm;
266 u8 enable;
267 u8 auto_pwm_min;
268};
269
270struct adm1026_data {
Axel Linb591d3c2014-07-03 21:53:01 +0800271 struct i2c_client *client;
272 const struct attribute_group *groups[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100274 struct mutex update_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 int valid; /* !=0 if following fields are valid */
276 unsigned long last_reading; /* In jiffies */
277 unsigned long last_config; /* In jiffies */
278
Jean Delvarecb01a232007-11-29 23:46:42 +0100279 u8 in[17]; /* Register value */
280 u8 in_max[17]; /* Register value */
281 u8 in_min[17]; /* Register value */
282 s8 temp[3]; /* Register value */
283 s8 temp_min[3]; /* Register value */
284 s8 temp_max[3]; /* Register value */
285 s8 temp_tmin[3]; /* Register value */
286 s8 temp_crit[3]; /* Register value */
287 s8 temp_offset[3]; /* Register value */
288 u8 fan[8]; /* Register value */
289 u8 fan_min[8]; /* Register value */
290 u8 fan_div[8]; /* Decoded value */
291 struct pwm_data pwm1; /* Pwm control values */
Jean Delvarecb01a232007-11-29 23:46:42 +0100292 u8 vrm; /* VRM version */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 u8 analog_out; /* Register value (DAC) */
Jean Delvarecb01a232007-11-29 23:46:42 +0100294 long alarms; /* Register encoding, combined */
295 long alarm_mask; /* Register encoding, combined */
296 long gpio; /* Register encoding, combined */
297 long gpio_mask; /* Register encoding, combined */
298 u8 gpio_config[17]; /* Decoded value */
299 u8 config1; /* Register value */
300 u8 config2; /* Register value */
301 u8 config3; /* Register value */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302};
303
Ben Dooksc49efce2005-10-26 21:07:25 +0200304static int adm1026_read_value(struct i2c_client *client, u8 reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305{
306 int res;
307
308 if (reg < 0x80) {
309 /* "RAM" locations */
310 res = i2c_smbus_read_byte_data(client, reg) & 0xff;
311 } else {
312 /* EEPROM, do nothing */
313 res = 0;
314 }
315 return res;
316}
317
Ben Dooksc49efce2005-10-26 21:07:25 +0200318static int adm1026_write_value(struct i2c_client *client, u8 reg, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319{
320 int res;
321
322 if (reg < 0x80) {
323 /* "RAM" locations */
324 res = i2c_smbus_write_byte_data(client, reg, value);
325 } else {
326 /* EEPROM, do nothing */
327 res = 0;
328 }
329 return res;
330}
331
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332static struct adm1026_data *adm1026_update_device(struct device *dev)
333{
Axel Linb591d3c2014-07-03 21:53:01 +0800334 struct adm1026_data *data = dev_get_drvdata(dev);
335 struct i2c_client *client = data->client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 int i;
337 long value, alarms, gpio;
338
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100339 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 if (!data->valid
Guenter Roeck86aa3e22012-01-14 12:51:15 -0800341 || time_after(jiffies,
342 data->last_reading + ADM1026_DATA_INTERVAL)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 /* Things that change quickly */
Jean Delvarecb01a232007-11-29 23:46:42 +0100344 dev_dbg(&client->dev, "Reading sensor values\n");
Guenter Roeck86aa3e22012-01-14 12:51:15 -0800345 for (i = 0; i <= 16; ++i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 data->in[i] =
347 adm1026_read_value(client, ADM1026_REG_IN[i]);
348 }
349
Guenter Roeck86aa3e22012-01-14 12:51:15 -0800350 for (i = 0; i <= 7; ++i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 data->fan[i] =
352 adm1026_read_value(client, ADM1026_REG_FAN(i));
353 }
354
Guenter Roeck86aa3e22012-01-14 12:51:15 -0800355 for (i = 0; i <= 2; ++i) {
356 /*
357 * NOTE: temp[] is s8 and we assume 2's complement
358 * "conversion" in the assignment
359 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 data->temp[i] =
361 adm1026_read_value(client, ADM1026_REG_TEMP[i]);
362 }
363
Jean Delvarecb01a232007-11-29 23:46:42 +0100364 data->pwm1.pwm = adm1026_read_value(client,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 ADM1026_REG_PWM);
Jean Delvarecb01a232007-11-29 23:46:42 +0100366 data->analog_out = adm1026_read_value(client,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 ADM1026_REG_DAC);
368 /* GPIO16 is MSbit of alarms, move it to gpio */
369 alarms = adm1026_read_value(client, ADM1026_REG_STATUS4);
Jean Delvarecb01a232007-11-29 23:46:42 +0100370 gpio = alarms & 0x80 ? 0x0100 : 0; /* GPIO16 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 alarms &= 0x7f;
372 alarms <<= 8;
373 alarms |= adm1026_read_value(client, ADM1026_REG_STATUS3);
374 alarms <<= 8;
375 alarms |= adm1026_read_value(client, ADM1026_REG_STATUS2);
376 alarms <<= 8;
377 alarms |= adm1026_read_value(client, ADM1026_REG_STATUS1);
378 data->alarms = alarms;
379
380 /* Read the GPIO values */
Jean Delvarecb01a232007-11-29 23:46:42 +0100381 gpio |= adm1026_read_value(client,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 ADM1026_REG_GPIO_STATUS_8_15);
383 gpio <<= 8;
Jean Delvarecb01a232007-11-29 23:46:42 +0100384 gpio |= adm1026_read_value(client,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 ADM1026_REG_GPIO_STATUS_0_7);
386 data->gpio = gpio;
387
388 data->last_reading = jiffies;
Guenter Roecka0393712013-03-29 13:54:50 -0700389 } /* last_reading */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390
391 if (!data->valid ||
392 time_after(jiffies, data->last_config + ADM1026_CONFIG_INTERVAL)) {
393 /* Things that don't change often */
394 dev_dbg(&client->dev, "Reading config values\n");
Guenter Roeck86aa3e22012-01-14 12:51:15 -0800395 for (i = 0; i <= 16; ++i) {
Jean Delvarecb01a232007-11-29 23:46:42 +0100396 data->in_min[i] = adm1026_read_value(client,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 ADM1026_REG_IN_MIN[i]);
Jean Delvarecb01a232007-11-29 23:46:42 +0100398 data->in_max[i] = adm1026_read_value(client,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 ADM1026_REG_IN_MAX[i]);
400 }
401
402 value = adm1026_read_value(client, ADM1026_REG_FAN_DIV_0_3)
403 | (adm1026_read_value(client, ADM1026_REG_FAN_DIV_4_7)
404 << 8);
Guenter Roeck86aa3e22012-01-14 12:51:15 -0800405 for (i = 0; i <= 7; ++i) {
Jean Delvarecb01a232007-11-29 23:46:42 +0100406 data->fan_min[i] = adm1026_read_value(client,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 ADM1026_REG_FAN_MIN(i));
408 data->fan_div[i] = DIV_FROM_REG(value & 0x03);
409 value >>= 2;
410 }
411
412 for (i = 0; i <= 2; ++i) {
Guenter Roeck86aa3e22012-01-14 12:51:15 -0800413 /*
414 * NOTE: temp_xxx[] are s8 and we assume 2's
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 * complement "conversion" in the assignment
416 */
Jean Delvarecb01a232007-11-29 23:46:42 +0100417 data->temp_min[i] = adm1026_read_value(client,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 ADM1026_REG_TEMP_MIN[i]);
Jean Delvarecb01a232007-11-29 23:46:42 +0100419 data->temp_max[i] = adm1026_read_value(client,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 ADM1026_REG_TEMP_MAX[i]);
Jean Delvarecb01a232007-11-29 23:46:42 +0100421 data->temp_tmin[i] = adm1026_read_value(client,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 ADM1026_REG_TEMP_TMIN[i]);
Jean Delvarecb01a232007-11-29 23:46:42 +0100423 data->temp_crit[i] = adm1026_read_value(client,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 ADM1026_REG_TEMP_THERM[i]);
Jean Delvarecb01a232007-11-29 23:46:42 +0100425 data->temp_offset[i] = adm1026_read_value(client,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 ADM1026_REG_TEMP_OFFSET[i]);
427 }
428
429 /* Read the STATUS/alarm masks */
Jean Delvarecb01a232007-11-29 23:46:42 +0100430 alarms = adm1026_read_value(client, ADM1026_REG_MASK4);
431 gpio = alarms & 0x80 ? 0x0100 : 0; /* GPIO16 */
432 alarms = (alarms & 0x7f) << 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 alarms |= adm1026_read_value(client, ADM1026_REG_MASK3);
434 alarms <<= 8;
435 alarms |= adm1026_read_value(client, ADM1026_REG_MASK2);
436 alarms <<= 8;
437 alarms |= adm1026_read_value(client, ADM1026_REG_MASK1);
438 data->alarm_mask = alarms;
439
440 /* Read the GPIO values */
Jean Delvarecb01a232007-11-29 23:46:42 +0100441 gpio |= adm1026_read_value(client,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 ADM1026_REG_GPIO_MASK_8_15);
443 gpio <<= 8;
444 gpio |= adm1026_read_value(client, ADM1026_REG_GPIO_MASK_0_7);
445 data->gpio_mask = gpio;
446
447 /* Read various values from CONFIG1 */
Jean Delvarecb01a232007-11-29 23:46:42 +0100448 data->config1 = adm1026_read_value(client,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 ADM1026_REG_CONFIG1);
450 if (data->config1 & CFG1_PWM_AFC) {
451 data->pwm1.enable = 2;
Jean Delvarecb01a232007-11-29 23:46:42 +0100452 data->pwm1.auto_pwm_min =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 PWM_MIN_FROM_REG(data->pwm1.pwm);
454 }
455 /* Read the GPIO config */
Jean Delvarecb01a232007-11-29 23:46:42 +0100456 data->config2 = adm1026_read_value(client,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 ADM1026_REG_CONFIG2);
Jean Delvarecb01a232007-11-29 23:46:42 +0100458 data->config3 = adm1026_read_value(client,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 ADM1026_REG_CONFIG3);
460 data->gpio_config[16] = (data->config3 >> 6) & 0x03;
461
462 value = 0;
Guenter Roeck86aa3e22012-01-14 12:51:15 -0800463 for (i = 0; i <= 15; ++i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 if ((i & 0x03) == 0) {
465 value = adm1026_read_value(client,
466 ADM1026_REG_GPIO_CFG_0_3 + i/4);
467 }
468 data->gpio_config[i] = value & 0x03;
469 value >>= 2;
470 }
471
472 data->last_config = jiffies;
Guenter Roecka0393712013-03-29 13:54:50 -0700473 } /* last_config */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 data->valid = 1;
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100476 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 return data;
478}
479
Yani Ioannou050480f2005-06-05 10:51:46 +0200480static ssize_t show_in(struct device *dev, struct device_attribute *attr,
481 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482{
Yani Ioannou050480f2005-06-05 10:51:46 +0200483 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
484 int nr = sensor_attr->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 struct adm1026_data *data = adm1026_update_device(dev);
Jean Delvarecb01a232007-11-29 23:46:42 +0100486 return sprintf(buf, "%d\n", INS_FROM_REG(nr, data->in[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487}
Yani Ioannou050480f2005-06-05 10:51:46 +0200488static ssize_t show_in_min(struct device *dev, struct device_attribute *attr,
489 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490{
Yani Ioannou050480f2005-06-05 10:51:46 +0200491 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
492 int nr = sensor_attr->index;
Jean Delvarecb01a232007-11-29 23:46:42 +0100493 struct adm1026_data *data = adm1026_update_device(dev);
494 return sprintf(buf, "%d\n", INS_FROM_REG(nr, data->in_min[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495}
Yani Ioannou050480f2005-06-05 10:51:46 +0200496static ssize_t set_in_min(struct device *dev, struct device_attribute *attr,
497 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498{
Yani Ioannou050480f2005-06-05 10:51:46 +0200499 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
500 int nr = sensor_attr->index;
Axel Linb591d3c2014-07-03 21:53:01 +0800501 struct adm1026_data *data = dev_get_drvdata(dev);
502 struct i2c_client *client = data->client;
Guenter Roeck86aa3e22012-01-14 12:51:15 -0800503 long val;
504 int err;
505
506 err = kstrtol(buf, 10, &val);
507 if (err)
508 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100510 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 data->in_min[nr] = INS_TO_REG(nr, val);
512 adm1026_write_value(client, ADM1026_REG_IN_MIN[nr], data->in_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100513 mutex_unlock(&data->update_lock);
Jean Delvarecb01a232007-11-29 23:46:42 +0100514 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515}
Yani Ioannou050480f2005-06-05 10:51:46 +0200516static ssize_t show_in_max(struct device *dev, struct device_attribute *attr,
517 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518{
Yani Ioannou050480f2005-06-05 10:51:46 +0200519 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
520 int nr = sensor_attr->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 struct adm1026_data *data = adm1026_update_device(dev);
Jean Delvarecb01a232007-11-29 23:46:42 +0100522 return sprintf(buf, "%d\n", INS_FROM_REG(nr, data->in_max[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523}
Yani Ioannou050480f2005-06-05 10:51:46 +0200524static ssize_t set_in_max(struct device *dev, struct device_attribute *attr,
525 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526{
Yani Ioannou050480f2005-06-05 10:51:46 +0200527 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
528 int nr = sensor_attr->index;
Axel Linb591d3c2014-07-03 21:53:01 +0800529 struct adm1026_data *data = dev_get_drvdata(dev);
530 struct i2c_client *client = data->client;
Guenter Roeck86aa3e22012-01-14 12:51:15 -0800531 long val;
532 int err;
533
534 err = kstrtol(buf, 10, &val);
535 if (err)
536 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100538 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 data->in_max[nr] = INS_TO_REG(nr, val);
540 adm1026_write_value(client, ADM1026_REG_IN_MAX[nr], data->in_max[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100541 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 return count;
543}
544
Yani Ioannou050480f2005-06-05 10:51:46 +0200545#define in_reg(offset) \
546static SENSOR_DEVICE_ATTR(in##offset##_input, S_IRUGO, show_in, \
547 NULL, offset); \
548static SENSOR_DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \
549 show_in_min, set_in_min, offset); \
550static SENSOR_DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \
551 show_in_max, set_in_max, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552
553
554in_reg(0);
555in_reg(1);
556in_reg(2);
557in_reg(3);
558in_reg(4);
559in_reg(5);
560in_reg(6);
561in_reg(7);
562in_reg(8);
563in_reg(9);
564in_reg(10);
565in_reg(11);
566in_reg(12);
567in_reg(13);
568in_reg(14);
569in_reg(15);
570
Guenter Roeck86aa3e22012-01-14 12:51:15 -0800571static ssize_t show_in16(struct device *dev, struct device_attribute *attr,
572 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573{
574 struct adm1026_data *data = adm1026_update_device(dev);
Jean Delvarecb01a232007-11-29 23:46:42 +0100575 return sprintf(buf, "%d\n", INS_FROM_REG(16, data->in[16]) -
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 NEG12_OFFSET);
577}
Guenter Roeck86aa3e22012-01-14 12:51:15 -0800578static ssize_t show_in16_min(struct device *dev, struct device_attribute *attr,
579 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580{
Jean Delvarecb01a232007-11-29 23:46:42 +0100581 struct adm1026_data *data = adm1026_update_device(dev);
582 return sprintf(buf, "%d\n", INS_FROM_REG(16, data->in_min[16])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 - NEG12_OFFSET);
584}
Guenter Roeck86aa3e22012-01-14 12:51:15 -0800585static ssize_t set_in16_min(struct device *dev, struct device_attribute *attr,
586 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587{
Axel Linb591d3c2014-07-03 21:53:01 +0800588 struct adm1026_data *data = dev_get_drvdata(dev);
589 struct i2c_client *client = data->client;
Guenter Roeck86aa3e22012-01-14 12:51:15 -0800590 long val;
591 int err;
592
593 err = kstrtol(buf, 10, &val);
594 if (err)
595 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100597 mutex_lock(&data->update_lock);
Guenter Roeckf1b9baa2016-12-03 11:08:07 -0800598 data->in_min[16] = INS_TO_REG(16,
599 clamp_val(val, INT_MIN,
600 INT_MAX - NEG12_OFFSET) +
601 NEG12_OFFSET);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 adm1026_write_value(client, ADM1026_REG_IN_MIN[16], data->in_min[16]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100603 mutex_unlock(&data->update_lock);
Jean Delvarecb01a232007-11-29 23:46:42 +0100604 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605}
Guenter Roeck86aa3e22012-01-14 12:51:15 -0800606static ssize_t show_in16_max(struct device *dev, struct device_attribute *attr,
607 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608{
609 struct adm1026_data *data = adm1026_update_device(dev);
Jean Delvarecb01a232007-11-29 23:46:42 +0100610 return sprintf(buf, "%d\n", INS_FROM_REG(16, data->in_max[16])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 - NEG12_OFFSET);
612}
Guenter Roeck86aa3e22012-01-14 12:51:15 -0800613static ssize_t set_in16_max(struct device *dev, struct device_attribute *attr,
614 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615{
Axel Linb591d3c2014-07-03 21:53:01 +0800616 struct adm1026_data *data = dev_get_drvdata(dev);
617 struct i2c_client *client = data->client;
Guenter Roeck86aa3e22012-01-14 12:51:15 -0800618 long val;
619 int err;
620
621 err = kstrtol(buf, 10, &val);
622 if (err)
623 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100625 mutex_lock(&data->update_lock);
Guenter Roeckf1b9baa2016-12-03 11:08:07 -0800626 data->in_max[16] = INS_TO_REG(16,
627 clamp_val(val, INT_MIN,
628 INT_MAX - NEG12_OFFSET) +
629 NEG12_OFFSET);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 adm1026_write_value(client, ADM1026_REG_IN_MAX[16], data->in_max[16]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100631 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 return count;
633}
634
Yani Ioannou050480f2005-06-05 10:51:46 +0200635static SENSOR_DEVICE_ATTR(in16_input, S_IRUGO, show_in16, NULL, 16);
Guenter Roeck86aa3e22012-01-14 12:51:15 -0800636static SENSOR_DEVICE_ATTR(in16_min, S_IRUGO | S_IWUSR, show_in16_min,
637 set_in16_min, 16);
638static SENSOR_DEVICE_ATTR(in16_max, S_IRUGO | S_IWUSR, show_in16_max,
639 set_in16_max, 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640
641
642/* Now add fan read/write functions */
643
Yani Ioannou050480f2005-06-05 10:51:46 +0200644static ssize_t show_fan(struct device *dev, struct device_attribute *attr,
645 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646{
Yani Ioannou050480f2005-06-05 10:51:46 +0200647 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
648 int nr = sensor_attr->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 struct adm1026_data *data = adm1026_update_device(dev);
Jean Delvarecb01a232007-11-29 23:46:42 +0100650 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[nr],
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 data->fan_div[nr]));
652}
Yani Ioannou050480f2005-06-05 10:51:46 +0200653static ssize_t show_fan_min(struct device *dev, struct device_attribute *attr,
654 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655{
Yani Ioannou050480f2005-06-05 10:51:46 +0200656 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
657 int nr = sensor_attr->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 struct adm1026_data *data = adm1026_update_device(dev);
Jean Delvarecb01a232007-11-29 23:46:42 +0100659 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_min[nr],
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 data->fan_div[nr]));
661}
Yani Ioannou050480f2005-06-05 10:51:46 +0200662static ssize_t set_fan_min(struct device *dev, struct device_attribute *attr,
663 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664{
Yani Ioannou050480f2005-06-05 10:51:46 +0200665 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
666 int nr = sensor_attr->index;
Axel Linb591d3c2014-07-03 21:53:01 +0800667 struct adm1026_data *data = dev_get_drvdata(dev);
668 struct i2c_client *client = data->client;
Guenter Roeck86aa3e22012-01-14 12:51:15 -0800669 long val;
670 int err;
671
672 err = kstrtol(buf, 10, &val);
673 if (err)
674 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100676 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 data->fan_min[nr] = FAN_TO_REG(val, data->fan_div[nr]);
678 adm1026_write_value(client, ADM1026_REG_FAN_MIN(nr),
679 data->fan_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100680 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 return count;
682}
683
Jean Delvarecb01a232007-11-29 23:46:42 +0100684#define fan_offset(offset) \
685static SENSOR_DEVICE_ATTR(fan##offset##_input, S_IRUGO, show_fan, NULL, \
686 offset - 1); \
687static SENSOR_DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
Yani Ioannou050480f2005-06-05 10:51:46 +0200688 show_fan_min, set_fan_min, offset - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689
690fan_offset(1);
691fan_offset(2);
692fan_offset(3);
693fan_offset(4);
694fan_offset(5);
695fan_offset(6);
696fan_offset(7);
697fan_offset(8);
698
699/* Adjust fan_min to account for new fan divisor */
700static void fixup_fan_min(struct device *dev, int fan, int old_div)
701{
Axel Linb591d3c2014-07-03 21:53:01 +0800702 struct adm1026_data *data = dev_get_drvdata(dev);
703 struct i2c_client *client = data->client;
Jean Delvarecb01a232007-11-29 23:46:42 +0100704 int new_min;
705 int new_div = data->fan_div[fan];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706
707 /* 0 and 0xff are special. Don't adjust them */
Guenter Roeck86aa3e22012-01-14 12:51:15 -0800708 if (data->fan_min[fan] == 0 || data->fan_min[fan] == 0xff)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710
711 new_min = data->fan_min[fan] * old_div / new_div;
Guenter Roeck2a844c12013-01-09 08:09:34 -0800712 new_min = clamp_val(new_min, 1, 254);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 data->fan_min[fan] = new_min;
714 adm1026_write_value(client, ADM1026_REG_FAN_MIN(fan), new_min);
715}
716
717/* Now add fan_div read/write functions */
Yani Ioannou050480f2005-06-05 10:51:46 +0200718static ssize_t show_fan_div(struct device *dev, struct device_attribute *attr,
719 char *buf)
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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 struct adm1026_data *data = adm1026_update_device(dev);
Jean Delvarecb01a232007-11-29 23:46:42 +0100724 return sprintf(buf, "%d\n", data->fan_div[nr]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725}
Yani Ioannou050480f2005-06-05 10:51:46 +0200726static ssize_t set_fan_div(struct device *dev, struct device_attribute *attr,
727 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728{
Yani Ioannou050480f2005-06-05 10:51:46 +0200729 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
730 int nr = sensor_attr->index;
Axel Linb591d3c2014-07-03 21:53:01 +0800731 struct adm1026_data *data = dev_get_drvdata(dev);
732 struct i2c_client *client = data->client;
Guenter Roeck86aa3e22012-01-14 12:51:15 -0800733 long val;
734 int orig_div, new_div;
735 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736
Guenter Roeck86aa3e22012-01-14 12:51:15 -0800737 err = kstrtol(buf, 10, &val);
738 if (err)
739 return err;
740
Jean Delvarecb01a232007-11-29 23:46:42 +0100741 new_div = DIV_TO_REG(val);
Gabriele Gorla8b0f1842010-12-08 16:27:22 +0100742
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100743 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 orig_div = data->fan_div[nr];
745 data->fan_div[nr] = DIV_FROM_REG(new_div);
746
747 if (nr < 4) { /* 0 <= nr < 4 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 adm1026_write_value(client, ADM1026_REG_FAN_DIV_0_3,
Gabriele Gorla52bc9802010-12-08 16:27:22 +0100749 (DIV_TO_REG(data->fan_div[0]) << 0) |
750 (DIV_TO_REG(data->fan_div[1]) << 2) |
751 (DIV_TO_REG(data->fan_div[2]) << 4) |
752 (DIV_TO_REG(data->fan_div[3]) << 6));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 } else { /* 3 < nr < 8 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 adm1026_write_value(client, ADM1026_REG_FAN_DIV_4_7,
Gabriele Gorla52bc9802010-12-08 16:27:22 +0100755 (DIV_TO_REG(data->fan_div[4]) << 0) |
756 (DIV_TO_REG(data->fan_div[5]) << 2) |
757 (DIV_TO_REG(data->fan_div[6]) << 4) |
758 (DIV_TO_REG(data->fan_div[7]) << 6));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 }
760
Guenter Roeck86aa3e22012-01-14 12:51:15 -0800761 if (data->fan_div[nr] != orig_div)
Jean Delvarecb01a232007-11-29 23:46:42 +0100762 fixup_fan_min(dev, nr, orig_div);
Guenter Roeck86aa3e22012-01-14 12:51:15 -0800763
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100764 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 return count;
766}
767
Jean Delvarecb01a232007-11-29 23:46:42 +0100768#define fan_offset_div(offset) \
769static SENSOR_DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR, \
Yani Ioannou050480f2005-06-05 10:51:46 +0200770 show_fan_div, set_fan_div, offset - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771
772fan_offset_div(1);
773fan_offset_div(2);
774fan_offset_div(3);
775fan_offset_div(4);
776fan_offset_div(5);
777fan_offset_div(6);
778fan_offset_div(7);
779fan_offset_div(8);
780
781/* Temps */
Yani Ioannou050480f2005-06-05 10:51:46 +0200782static ssize_t show_temp(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[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789}
Yani Ioannou050480f2005-06-05 10:51:46 +0200790static ssize_t show_temp_min(struct device *dev, struct device_attribute *attr,
791 char *buf)
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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 struct adm1026_data *data = adm1026_update_device(dev);
Jean Delvarecb01a232007-11-29 23:46:42 +0100796 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_min[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797}
Yani Ioannou050480f2005-06-05 10:51:46 +0200798static ssize_t set_temp_min(struct device *dev, struct device_attribute *attr,
799 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800{
Yani Ioannou050480f2005-06-05 10:51:46 +0200801 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
802 int nr = sensor_attr->index;
Axel Linb591d3c2014-07-03 21:53:01 +0800803 struct adm1026_data *data = dev_get_drvdata(dev);
804 struct i2c_client *client = data->client;
Guenter Roeck86aa3e22012-01-14 12:51:15 -0800805 long val;
806 int err;
807
808 err = kstrtol(buf, 10, &val);
809 if (err)
810 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100812 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 data->temp_min[nr] = TEMP_TO_REG(val);
814 adm1026_write_value(client, ADM1026_REG_TEMP_MIN[nr],
815 data->temp_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100816 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 return count;
818}
Yani Ioannou050480f2005-06-05 10:51:46 +0200819static ssize_t show_temp_max(struct device *dev, struct device_attribute *attr,
820 char *buf)
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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 struct adm1026_data *data = adm1026_update_device(dev);
Jean Delvarecb01a232007-11-29 23:46:42 +0100825 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_max[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826}
Yani Ioannou050480f2005-06-05 10:51:46 +0200827static ssize_t set_temp_max(struct device *dev, struct device_attribute *attr,
828 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829{
Yani Ioannou050480f2005-06-05 10:51:46 +0200830 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
831 int nr = sensor_attr->index;
Axel Linb591d3c2014-07-03 21:53:01 +0800832 struct adm1026_data *data = dev_get_drvdata(dev);
833 struct i2c_client *client = data->client;
Guenter Roeck86aa3e22012-01-14 12:51:15 -0800834 long val;
835 int err;
836
837 err = kstrtol(buf, 10, &val);
838 if (err)
839 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100841 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 data->temp_max[nr] = TEMP_TO_REG(val);
843 adm1026_write_value(client, ADM1026_REG_TEMP_MAX[nr],
844 data->temp_max[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100845 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 return count;
847}
Yani Ioannou050480f2005-06-05 10:51:46 +0200848
849#define temp_reg(offset) \
Jean Delvarecb01a232007-11-29 23:46:42 +0100850static SENSOR_DEVICE_ATTR(temp##offset##_input, S_IRUGO, show_temp, \
Yani Ioannou050480f2005-06-05 10:51:46 +0200851 NULL, offset - 1); \
852static SENSOR_DEVICE_ATTR(temp##offset##_min, S_IRUGO | S_IWUSR, \
853 show_temp_min, set_temp_min, offset - 1); \
854static SENSOR_DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR, \
855 show_temp_max, set_temp_max, offset - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856
857
858temp_reg(1);
859temp_reg(2);
860temp_reg(3);
861
Yani Ioannou050480f2005-06-05 10:51:46 +0200862static ssize_t show_temp_offset(struct device *dev,
863 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864{
Yani Ioannou050480f2005-06-05 10:51:46 +0200865 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
866 int nr = sensor_attr->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 struct adm1026_data *data = adm1026_update_device(dev);
Jean Delvarecb01a232007-11-29 23:46:42 +0100868 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_offset[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869}
Yani Ioannou050480f2005-06-05 10:51:46 +0200870static ssize_t set_temp_offset(struct device *dev,
871 struct device_attribute *attr, const char *buf,
872 size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873{
Yani Ioannou050480f2005-06-05 10:51:46 +0200874 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
875 int nr = sensor_attr->index;
Axel Linb591d3c2014-07-03 21:53:01 +0800876 struct adm1026_data *data = dev_get_drvdata(dev);
877 struct i2c_client *client = data->client;
Guenter Roeck86aa3e22012-01-14 12:51:15 -0800878 long val;
879 int err;
880
881 err = kstrtol(buf, 10, &val);
882 if (err)
883 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100885 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 data->temp_offset[nr] = TEMP_TO_REG(val);
887 adm1026_write_value(client, ADM1026_REG_TEMP_OFFSET[nr],
888 data->temp_offset[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100889 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 return count;
891}
892
Guenter Roeck86aa3e22012-01-14 12:51:15 -0800893#define temp_offset_reg(offset) \
894static SENSOR_DEVICE_ATTR(temp##offset##_offset, S_IRUGO | S_IWUSR, \
Yani Ioannou050480f2005-06-05 10:51:46 +0200895 show_temp_offset, set_temp_offset, offset - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896
897temp_offset_reg(1);
898temp_offset_reg(2);
899temp_offset_reg(3);
900
Yani Ioannou050480f2005-06-05 10:51:46 +0200901static ssize_t show_temp_auto_point1_temp_hyst(struct device *dev,
902 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903{
Yani Ioannou050480f2005-06-05 10:51:46 +0200904 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
905 int nr = sensor_attr->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 struct adm1026_data *data = adm1026_update_device(dev);
Jean Delvarecb01a232007-11-29 23:46:42 +0100907 return sprintf(buf, "%d\n", TEMP_FROM_REG(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 ADM1026_FAN_ACTIVATION_TEMP_HYST + data->temp_tmin[nr]));
909}
Yani Ioannou050480f2005-06-05 10:51:46 +0200910static ssize_t show_temp_auto_point2_temp(struct device *dev,
911 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912{
Yani Ioannou050480f2005-06-05 10:51:46 +0200913 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
914 int nr = sensor_attr->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 struct adm1026_data *data = adm1026_update_device(dev);
Jean Delvarecb01a232007-11-29 23:46:42 +0100916 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_tmin[nr] +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 ADM1026_FAN_CONTROL_TEMP_RANGE));
918}
Yani Ioannou050480f2005-06-05 10:51:46 +0200919static ssize_t show_temp_auto_point1_temp(struct device *dev,
920 struct device_attribute *attr, char *buf)
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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 struct adm1026_data *data = adm1026_update_device(dev);
Jean Delvarecb01a232007-11-29 23:46:42 +0100925 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_tmin[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926}
Yani Ioannou050480f2005-06-05 10:51:46 +0200927static ssize_t set_temp_auto_point1_temp(struct device *dev,
928 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929{
Yani Ioannou050480f2005-06-05 10:51:46 +0200930 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
931 int nr = sensor_attr->index;
Axel Linb591d3c2014-07-03 21:53:01 +0800932 struct adm1026_data *data = dev_get_drvdata(dev);
933 struct i2c_client *client = data->client;
Guenter Roeck86aa3e22012-01-14 12:51:15 -0800934 long val;
935 int err;
936
937 err = kstrtol(buf, 10, &val);
938 if (err)
939 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100941 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 data->temp_tmin[nr] = TEMP_TO_REG(val);
943 adm1026_write_value(client, ADM1026_REG_TEMP_TMIN[nr],
944 data->temp_tmin[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100945 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 return count;
947}
948
Jean Delvarecb01a232007-11-29 23:46:42 +0100949#define temp_auto_point(offset) \
950static SENSOR_DEVICE_ATTR(temp##offset##_auto_point1_temp, \
951 S_IRUGO | S_IWUSR, show_temp_auto_point1_temp, \
952 set_temp_auto_point1_temp, offset - 1); \
953static SENSOR_DEVICE_ATTR(temp##offset##_auto_point1_temp_hyst, S_IRUGO,\
954 show_temp_auto_point1_temp_hyst, NULL, offset - 1); \
955static SENSOR_DEVICE_ATTR(temp##offset##_auto_point2_temp, S_IRUGO, \
Yani Ioannou050480f2005-06-05 10:51:46 +0200956 show_temp_auto_point2_temp, NULL, offset - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957
958temp_auto_point(1);
959temp_auto_point(2);
960temp_auto_point(3);
961
Yani Ioannou050480f2005-06-05 10:51:46 +0200962static ssize_t show_temp_crit_enable(struct device *dev,
963 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964{
965 struct adm1026_data *data = adm1026_update_device(dev);
Jean Delvarecb01a232007-11-29 23:46:42 +0100966 return sprintf(buf, "%d\n", (data->config1 & CFG1_THERM_HOT) >> 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967}
Yani Ioannou050480f2005-06-05 10:51:46 +0200968static ssize_t set_temp_crit_enable(struct device *dev,
969 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970{
Axel Linb591d3c2014-07-03 21:53:01 +0800971 struct adm1026_data *data = dev_get_drvdata(dev);
972 struct i2c_client *client = data->client;
Guenter Roeck86aa3e22012-01-14 12:51:15 -0800973 unsigned long val;
974 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975
Guenter Roeck86aa3e22012-01-14 12:51:15 -0800976 err = kstrtoul(buf, 10, &val);
977 if (err)
978 return err;
979
980 if (val > 1)
981 return -EINVAL;
982
983 mutex_lock(&data->update_lock);
984 data->config1 = (data->config1 & ~CFG1_THERM_HOT) | (val << 4);
985 adm1026_write_value(client, ADM1026_REG_CONFIG1, data->config1);
986 mutex_unlock(&data->update_lock);
987
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 return count;
989}
990
Yani Ioannou050480f2005-06-05 10:51:46 +0200991#define temp_crit_enable(offset) \
992static DEVICE_ATTR(temp##offset##_crit_enable, S_IRUGO | S_IWUSR, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 show_temp_crit_enable, set_temp_crit_enable);
994
Yani Ioannou050480f2005-06-05 10:51:46 +0200995temp_crit_enable(1);
996temp_crit_enable(2);
997temp_crit_enable(3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998
Yani Ioannou050480f2005-06-05 10:51:46 +0200999static ssize_t show_temp_crit(struct device *dev,
1000 struct device_attribute *attr, char *buf)
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;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 struct adm1026_data *data = adm1026_update_device(dev);
Jean Delvarecb01a232007-11-29 23:46:42 +01001005 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_crit[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006}
Yani Ioannou050480f2005-06-05 10:51:46 +02001007static ssize_t set_temp_crit(struct device *dev, struct device_attribute *attr,
1008 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009{
Yani Ioannou050480f2005-06-05 10:51:46 +02001010 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1011 int nr = sensor_attr->index;
Axel Linb591d3c2014-07-03 21:53:01 +08001012 struct adm1026_data *data = dev_get_drvdata(dev);
1013 struct i2c_client *client = data->client;
Guenter Roeck86aa3e22012-01-14 12:51:15 -08001014 long val;
1015 int err;
1016
1017 err = kstrtol(buf, 10, &val);
1018 if (err)
1019 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001021 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 data->temp_crit[nr] = TEMP_TO_REG(val);
1023 adm1026_write_value(client, ADM1026_REG_TEMP_THERM[nr],
1024 data->temp_crit[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001025 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 return count;
1027}
1028
Yani Ioannou050480f2005-06-05 10:51:46 +02001029#define temp_crit_reg(offset) \
1030static SENSOR_DEVICE_ATTR(temp##offset##_crit, S_IRUGO | S_IWUSR, \
1031 show_temp_crit, set_temp_crit, offset - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032
1033temp_crit_reg(1);
1034temp_crit_reg(2);
1035temp_crit_reg(3);
1036
Julia Lawall6d3c2132016-12-22 13:04:31 +01001037static ssize_t analog_out_show(struct device *dev,
1038 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039{
1040 struct adm1026_data *data = adm1026_update_device(dev);
Jean Delvarecb01a232007-11-29 23:46:42 +01001041 return sprintf(buf, "%d\n", DAC_FROM_REG(data->analog_out));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042}
Julia Lawall6d3c2132016-12-22 13:04:31 +01001043static ssize_t analog_out_store(struct device *dev,
1044 struct device_attribute *attr,
1045 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046{
Axel Linb591d3c2014-07-03 21:53:01 +08001047 struct adm1026_data *data = dev_get_drvdata(dev);
1048 struct i2c_client *client = data->client;
Guenter Roeck86aa3e22012-01-14 12:51:15 -08001049 long val;
1050 int err;
1051
1052 err = kstrtol(buf, 10, &val);
1053 if (err)
1054 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001056 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 data->analog_out = DAC_TO_REG(val);
1058 adm1026_write_value(client, ADM1026_REG_DAC, data->analog_out);
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001059 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 return count;
1061}
1062
Julia Lawall6d3c2132016-12-22 13:04:31 +01001063static DEVICE_ATTR_RW(analog_out);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064
Julia Lawall6d3c2132016-12-22 13:04:31 +01001065static ssize_t cpu0_vid_show(struct device *dev,
1066 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067{
1068 struct adm1026_data *data = adm1026_update_device(dev);
Jean Delvarea0cf3542008-10-17 17:51:20 +02001069 int vid = (data->gpio >> 11) & 0x1f;
1070
1071 dev_dbg(dev, "Setting VID from GPIO11-15.\n");
1072 return sprintf(buf, "%d\n", vid_from_reg(vid, data->vrm));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073}
Guenter Roeck86aa3e22012-01-14 12:51:15 -08001074
Julia Lawall6d3c2132016-12-22 13:04:31 +01001075static DEVICE_ATTR_RO(cpu0_vid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076
Julia Lawall6d3c2132016-12-22 13:04:31 +01001077static ssize_t vrm_show(struct device *dev, struct device_attribute *attr,
1078 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079{
Jean Delvare90d66192007-10-08 18:24:35 +02001080 struct adm1026_data *data = dev_get_drvdata(dev);
Jean Delvarecb01a232007-11-29 23:46:42 +01001081 return sprintf(buf, "%d\n", data->vrm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082}
Guenter Roeck86aa3e22012-01-14 12:51:15 -08001083
Julia Lawall6d3c2132016-12-22 13:04:31 +01001084static ssize_t vrm_store(struct device *dev, struct device_attribute *attr,
1085 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086{
Jean Delvaref67fdab2007-12-01 11:24:17 +01001087 struct adm1026_data *data = dev_get_drvdata(dev);
Guenter Roeck86aa3e22012-01-14 12:51:15 -08001088 unsigned long val;
1089 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090
Guenter Roeck86aa3e22012-01-14 12:51:15 -08001091 err = kstrtoul(buf, 10, &val);
1092 if (err)
1093 return err;
1094
Axel Lina4461642014-08-06 08:20:45 +08001095 if (val > 255)
1096 return -EINVAL;
1097
Guenter Roeck86aa3e22012-01-14 12:51:15 -08001098 data->vrm = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 return count;
1100}
1101
Julia Lawall6d3c2132016-12-22 13:04:31 +01001102static DEVICE_ATTR_RW(vrm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103
Julia Lawall6d3c2132016-12-22 13:04:31 +01001104static ssize_t alarms_show(struct device *dev, struct device_attribute *attr,
1105 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106{
1107 struct adm1026_data *data = adm1026_update_device(dev);
Jean Delvaref67fdab2007-12-01 11:24:17 +01001108 return sprintf(buf, "%ld\n", data->alarms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109}
1110
Julia Lawall6d3c2132016-12-22 13:04:31 +01001111static DEVICE_ATTR_RO(alarms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112
Jean Delvarea9273cb2007-11-29 23:45:22 +01001113static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
1114 char *buf)
1115{
1116 struct adm1026_data *data = adm1026_update_device(dev);
1117 int bitnr = to_sensor_dev_attr(attr)->index;
1118 return sprintf(buf, "%ld\n", (data->alarms >> bitnr) & 1);
1119}
1120
1121static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 0);
1122static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, 1);
1123static SENSOR_DEVICE_ATTR(in9_alarm, S_IRUGO, show_alarm, NULL, 1);
1124static SENSOR_DEVICE_ATTR(in11_alarm, S_IRUGO, show_alarm, NULL, 2);
1125static SENSOR_DEVICE_ATTR(in12_alarm, S_IRUGO, show_alarm, NULL, 3);
1126static SENSOR_DEVICE_ATTR(in13_alarm, S_IRUGO, show_alarm, NULL, 4);
1127static SENSOR_DEVICE_ATTR(in14_alarm, S_IRUGO, show_alarm, NULL, 5);
1128static SENSOR_DEVICE_ATTR(in15_alarm, S_IRUGO, show_alarm, NULL, 6);
1129static SENSOR_DEVICE_ATTR(in16_alarm, S_IRUGO, show_alarm, NULL, 7);
1130static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 8);
1131static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 9);
1132static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 10);
1133static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 11);
1134static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 12);
1135static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 13);
1136static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 14);
1137static SENSOR_DEVICE_ATTR(in7_alarm, S_IRUGO, show_alarm, NULL, 15);
1138static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 16);
1139static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 17);
1140static SENSOR_DEVICE_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL, 18);
1141static SENSOR_DEVICE_ATTR(fan4_alarm, S_IRUGO, show_alarm, NULL, 19);
1142static SENSOR_DEVICE_ATTR(fan5_alarm, S_IRUGO, show_alarm, NULL, 20);
1143static SENSOR_DEVICE_ATTR(fan6_alarm, S_IRUGO, show_alarm, NULL, 21);
1144static SENSOR_DEVICE_ATTR(fan7_alarm, S_IRUGO, show_alarm, NULL, 22);
1145static SENSOR_DEVICE_ATTR(fan8_alarm, S_IRUGO, show_alarm, NULL, 23);
1146static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 24);
1147static SENSOR_DEVICE_ATTR(in10_alarm, S_IRUGO, show_alarm, NULL, 25);
1148static SENSOR_DEVICE_ATTR(in8_alarm, S_IRUGO, show_alarm, NULL, 26);
1149
Julia Lawall6d3c2132016-12-22 13:04:31 +01001150static ssize_t alarm_mask_show(struct device *dev,
Guenter Roeck86aa3e22012-01-14 12:51:15 -08001151 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152{
1153 struct adm1026_data *data = adm1026_update_device(dev);
Jean Delvarecb01a232007-11-29 23:46:42 +01001154 return sprintf(buf, "%ld\n", data->alarm_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155}
Julia Lawall6d3c2132016-12-22 13:04:31 +01001156static ssize_t alarm_mask_store(struct device *dev,
1157 struct device_attribute *attr,
1158 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159{
Axel Linb591d3c2014-07-03 21:53:01 +08001160 struct adm1026_data *data = dev_get_drvdata(dev);
1161 struct i2c_client *client = data->client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 unsigned long mask;
Guenter Roeck86aa3e22012-01-14 12:51:15 -08001163 long val;
1164 int err;
1165
1166 err = kstrtol(buf, 10, &val);
1167 if (err)
1168 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001170 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 data->alarm_mask = val & 0x7fffffff;
1172 mask = data->alarm_mask
1173 | (data->gpio_mask & 0x10000 ? 0x80000000 : 0);
1174 adm1026_write_value(client, ADM1026_REG_MASK1,
1175 mask & 0xff);
1176 mask >>= 8;
1177 adm1026_write_value(client, ADM1026_REG_MASK2,
1178 mask & 0xff);
1179 mask >>= 8;
1180 adm1026_write_value(client, ADM1026_REG_MASK3,
1181 mask & 0xff);
1182 mask >>= 8;
1183 adm1026_write_value(client, ADM1026_REG_MASK4,
1184 mask & 0xff);
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001185 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 return count;
1187}
1188
Julia Lawall6d3c2132016-12-22 13:04:31 +01001189static DEVICE_ATTR_RW(alarm_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190
1191
Julia Lawall6d3c2132016-12-22 13:04:31 +01001192static ssize_t gpio_show(struct device *dev, struct device_attribute *attr,
Guenter Roeck86aa3e22012-01-14 12:51:15 -08001193 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194{
1195 struct adm1026_data *data = adm1026_update_device(dev);
Jean Delvarecb01a232007-11-29 23:46:42 +01001196 return sprintf(buf, "%ld\n", data->gpio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197}
Julia Lawall6d3c2132016-12-22 13:04:31 +01001198static ssize_t gpio_store(struct device *dev, struct device_attribute *attr,
1199 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200{
Axel Linb591d3c2014-07-03 21:53:01 +08001201 struct adm1026_data *data = dev_get_drvdata(dev);
1202 struct i2c_client *client = data->client;
Jean Delvarecb01a232007-11-29 23:46:42 +01001203 long gpio;
Guenter Roeck86aa3e22012-01-14 12:51:15 -08001204 long val;
1205 int err;
1206
1207 err = kstrtol(buf, 10, &val);
1208 if (err)
1209 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001211 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 data->gpio = val & 0x1ffff;
1213 gpio = data->gpio;
Jean Delvarecb01a232007-11-29 23:46:42 +01001214 adm1026_write_value(client, ADM1026_REG_GPIO_STATUS_0_7, gpio & 0xff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 gpio >>= 8;
Jean Delvarecb01a232007-11-29 23:46:42 +01001216 adm1026_write_value(client, ADM1026_REG_GPIO_STATUS_8_15, gpio & 0xff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 gpio = ((gpio >> 1) & 0x80) | (data->alarms >> 24 & 0x7f);
Jean Delvarecb01a232007-11-29 23:46:42 +01001218 adm1026_write_value(client, ADM1026_REG_STATUS4, gpio & 0xff);
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001219 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 return count;
1221}
1222
Julia Lawall6d3c2132016-12-22 13:04:31 +01001223static DEVICE_ATTR_RW(gpio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224
Julia Lawall6d3c2132016-12-22 13:04:31 +01001225static ssize_t gpio_mask_show(struct device *dev,
1226 struct device_attribute *attr,
Guenter Roeck86aa3e22012-01-14 12:51:15 -08001227 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228{
1229 struct adm1026_data *data = adm1026_update_device(dev);
Jean Delvarecb01a232007-11-29 23:46:42 +01001230 return sprintf(buf, "%ld\n", data->gpio_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231}
Julia Lawall6d3c2132016-12-22 13:04:31 +01001232static ssize_t gpio_mask_store(struct device *dev,
1233 struct device_attribute *attr, const char *buf,
1234 size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235{
Axel Linb591d3c2014-07-03 21:53:01 +08001236 struct adm1026_data *data = dev_get_drvdata(dev);
1237 struct i2c_client *client = data->client;
Jean Delvarecb01a232007-11-29 23:46:42 +01001238 long mask;
Guenter Roeck86aa3e22012-01-14 12:51:15 -08001239 long val;
1240 int err;
1241
1242 err = kstrtol(buf, 10, &val);
1243 if (err)
1244 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001246 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 data->gpio_mask = val & 0x1ffff;
1248 mask = data->gpio_mask;
Jean Delvarecb01a232007-11-29 23:46:42 +01001249 adm1026_write_value(client, ADM1026_REG_GPIO_MASK_0_7, mask & 0xff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 mask >>= 8;
Jean Delvarecb01a232007-11-29 23:46:42 +01001251 adm1026_write_value(client, ADM1026_REG_GPIO_MASK_8_15, mask & 0xff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 mask = ((mask >> 1) & 0x80) | (data->alarm_mask >> 24 & 0x7f);
Jean Delvarecb01a232007-11-29 23:46:42 +01001253 adm1026_write_value(client, ADM1026_REG_MASK1, mask & 0xff);
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001254 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 return count;
1256}
1257
Julia Lawall6d3c2132016-12-22 13:04:31 +01001258static DEVICE_ATTR_RW(gpio_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259
Julia Lawall6d3c2132016-12-22 13:04:31 +01001260static ssize_t pwm1_show(struct device *dev, struct device_attribute *attr,
1261 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262{
1263 struct adm1026_data *data = adm1026_update_device(dev);
Jean Delvarecb01a232007-11-29 23:46:42 +01001264 return sprintf(buf, "%d\n", PWM_FROM_REG(data->pwm1.pwm));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265}
Guenter Roeck86aa3e22012-01-14 12:51:15 -08001266
Julia Lawall6d3c2132016-12-22 13:04:31 +01001267static ssize_t pwm1_store(struct device *dev, struct device_attribute *attr,
1268 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269{
Axel Linb591d3c2014-07-03 21:53:01 +08001270 struct adm1026_data *data = dev_get_drvdata(dev);
1271 struct i2c_client *client = data->client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272
1273 if (data->pwm1.enable == 1) {
Guenter Roeck86aa3e22012-01-14 12:51:15 -08001274 long val;
1275 int err;
1276
1277 err = kstrtol(buf, 10, &val);
1278 if (err)
1279 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001281 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 data->pwm1.pwm = PWM_TO_REG(val);
1283 adm1026_write_value(client, ADM1026_REG_PWM, data->pwm1.pwm);
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001284 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 }
1286 return count;
1287}
Guenter Roeck86aa3e22012-01-14 12:51:15 -08001288
Julia Lawall6d3c2132016-12-22 13:04:31 +01001289static ssize_t temp1_auto_point1_pwm_show(struct device *dev,
1290 struct device_attribute *attr,
1291 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292{
1293 struct adm1026_data *data = adm1026_update_device(dev);
Jean Delvarecb01a232007-11-29 23:46:42 +01001294 return sprintf(buf, "%d\n", data->pwm1.auto_pwm_min);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295}
Guenter Roeck86aa3e22012-01-14 12:51:15 -08001296
Julia Lawall6d3c2132016-12-22 13:04:31 +01001297static ssize_t temp1_auto_point1_pwm_store(struct device *dev,
1298 struct device_attribute *attr,
1299 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300{
Axel Linb591d3c2014-07-03 21:53:01 +08001301 struct adm1026_data *data = dev_get_drvdata(dev);
1302 struct i2c_client *client = data->client;
Guenter Roeck86aa3e22012-01-14 12:51:15 -08001303 unsigned long val;
1304 int err;
1305
1306 err = kstrtoul(buf, 10, &val);
1307 if (err)
1308 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001310 mutex_lock(&data->update_lock);
Guenter Roeck2a844c12013-01-09 08:09:34 -08001311 data->pwm1.auto_pwm_min = clamp_val(val, 0, 255);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 if (data->pwm1.enable == 2) { /* apply immediately */
1313 data->pwm1.pwm = PWM_TO_REG((data->pwm1.pwm & 0x0f) |
Jean Delvarecb01a232007-11-29 23:46:42 +01001314 PWM_MIN_TO_REG(data->pwm1.auto_pwm_min));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 adm1026_write_value(client, ADM1026_REG_PWM, data->pwm1.pwm);
1316 }
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001317 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 return count;
1319}
Guenter Roeck86aa3e22012-01-14 12:51:15 -08001320
Julia Lawall6d3c2132016-12-22 13:04:31 +01001321static ssize_t temp1_auto_point2_pwm_show(struct device *dev,
1322 struct device_attribute *attr,
1323 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324{
Jean Delvarecb01a232007-11-29 23:46:42 +01001325 return sprintf(buf, "%d\n", ADM1026_PWM_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326}
Guenter Roeck86aa3e22012-01-14 12:51:15 -08001327
Julia Lawall6d3c2132016-12-22 13:04:31 +01001328static ssize_t pwm1_enable_show(struct device *dev,
1329 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330{
1331 struct adm1026_data *data = adm1026_update_device(dev);
Jean Delvarecb01a232007-11-29 23:46:42 +01001332 return sprintf(buf, "%d\n", data->pwm1.enable);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333}
Guenter Roeck86aa3e22012-01-14 12:51:15 -08001334
Julia Lawall6d3c2132016-12-22 13:04:31 +01001335static ssize_t pwm1_enable_store(struct device *dev,
1336 struct device_attribute *attr,
1337 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338{
Axel Linb591d3c2014-07-03 21:53:01 +08001339 struct adm1026_data *data = dev_get_drvdata(dev);
1340 struct i2c_client *client = data->client;
Jean Delvarecb01a232007-11-29 23:46:42 +01001341 int old_enable;
Guenter Roeck86aa3e22012-01-14 12:51:15 -08001342 unsigned long val;
1343 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344
Guenter Roeck86aa3e22012-01-14 12:51:15 -08001345 err = kstrtoul(buf, 10, &val);
1346 if (err)
1347 return err;
1348
1349 if (val >= 3)
1350 return -EINVAL;
1351
1352 mutex_lock(&data->update_lock);
1353 old_enable = data->pwm1.enable;
1354 data->pwm1.enable = val;
1355 data->config1 = (data->config1 & ~CFG1_PWM_AFC)
1356 | ((val == 2) ? CFG1_PWM_AFC : 0);
1357 adm1026_write_value(client, ADM1026_REG_CONFIG1, data->config1);
1358 if (val == 2) { /* apply pwm1_auto_pwm_min to pwm1 */
1359 data->pwm1.pwm = PWM_TO_REG((data->pwm1.pwm & 0x0f) |
1360 PWM_MIN_TO_REG(data->pwm1.auto_pwm_min));
1361 adm1026_write_value(client, ADM1026_REG_PWM, data->pwm1.pwm);
1362 } else if (!((old_enable == 1) && (val == 1))) {
1363 /* set pwm to safe value */
1364 data->pwm1.pwm = 255;
1365 adm1026_write_value(client, ADM1026_REG_PWM, data->pwm1.pwm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 }
Guenter Roeck86aa3e22012-01-14 12:51:15 -08001367 mutex_unlock(&data->update_lock);
1368
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 return count;
1370}
1371
1372/* enable PWM fan control */
Julia Lawall6d3c2132016-12-22 13:04:31 +01001373static DEVICE_ATTR_RW(pwm1);
1374static DEVICE_ATTR(pwm2, S_IRUGO | S_IWUSR, pwm1_show, pwm1_store);
1375static DEVICE_ATTR(pwm3, S_IRUGO | S_IWUSR, pwm1_show, pwm1_store);
1376static DEVICE_ATTR_RW(pwm1_enable);
1377static DEVICE_ATTR(pwm2_enable, S_IRUGO | S_IWUSR, pwm1_enable_show,
1378 pwm1_enable_store);
1379static DEVICE_ATTR(pwm3_enable, S_IRUGO | S_IWUSR, pwm1_enable_show,
1380 pwm1_enable_store);
1381static DEVICE_ATTR_RW(temp1_auto_point1_pwm);
Jean Delvarecb01a232007-11-29 23:46:42 +01001382static DEVICE_ATTR(temp2_auto_point1_pwm, S_IRUGO | S_IWUSR,
Julia Lawall6d3c2132016-12-22 13:04:31 +01001383 temp1_auto_point1_pwm_show, temp1_auto_point1_pwm_store);
Jean Delvarecb01a232007-11-29 23:46:42 +01001384static DEVICE_ATTR(temp3_auto_point1_pwm, S_IRUGO | S_IWUSR,
Julia Lawall6d3c2132016-12-22 13:04:31 +01001385 temp1_auto_point1_pwm_show, temp1_auto_point1_pwm_store);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386
Julia Lawall6d3c2132016-12-22 13:04:31 +01001387static DEVICE_ATTR_RO(temp1_auto_point2_pwm);
1388static DEVICE_ATTR(temp2_auto_point2_pwm, S_IRUGO, temp1_auto_point2_pwm_show,
1389 NULL);
1390static DEVICE_ATTR(temp3_auto_point2_pwm, S_IRUGO, temp1_auto_point2_pwm_show,
1391 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392
Mark M. Hoffman681c6f72006-09-24 21:15:35 +02001393static struct attribute *adm1026_attributes[] = {
1394 &sensor_dev_attr_in0_input.dev_attr.attr,
1395 &sensor_dev_attr_in0_max.dev_attr.attr,
1396 &sensor_dev_attr_in0_min.dev_attr.attr,
Jean Delvarea9273cb2007-11-29 23:45:22 +01001397 &sensor_dev_attr_in0_alarm.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +02001398 &sensor_dev_attr_in1_input.dev_attr.attr,
1399 &sensor_dev_attr_in1_max.dev_attr.attr,
1400 &sensor_dev_attr_in1_min.dev_attr.attr,
Jean Delvarea9273cb2007-11-29 23:45:22 +01001401 &sensor_dev_attr_in1_alarm.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +02001402 &sensor_dev_attr_in2_input.dev_attr.attr,
1403 &sensor_dev_attr_in2_max.dev_attr.attr,
1404 &sensor_dev_attr_in2_min.dev_attr.attr,
Jean Delvarea9273cb2007-11-29 23:45:22 +01001405 &sensor_dev_attr_in2_alarm.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +02001406 &sensor_dev_attr_in3_input.dev_attr.attr,
1407 &sensor_dev_attr_in3_max.dev_attr.attr,
1408 &sensor_dev_attr_in3_min.dev_attr.attr,
Jean Delvarea9273cb2007-11-29 23:45:22 +01001409 &sensor_dev_attr_in3_alarm.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +02001410 &sensor_dev_attr_in4_input.dev_attr.attr,
1411 &sensor_dev_attr_in4_max.dev_attr.attr,
1412 &sensor_dev_attr_in4_min.dev_attr.attr,
Jean Delvarea9273cb2007-11-29 23:45:22 +01001413 &sensor_dev_attr_in4_alarm.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +02001414 &sensor_dev_attr_in5_input.dev_attr.attr,
1415 &sensor_dev_attr_in5_max.dev_attr.attr,
1416 &sensor_dev_attr_in5_min.dev_attr.attr,
Jean Delvarea9273cb2007-11-29 23:45:22 +01001417 &sensor_dev_attr_in5_alarm.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +02001418 &sensor_dev_attr_in6_input.dev_attr.attr,
1419 &sensor_dev_attr_in6_max.dev_attr.attr,
1420 &sensor_dev_attr_in6_min.dev_attr.attr,
Jean Delvarea9273cb2007-11-29 23:45:22 +01001421 &sensor_dev_attr_in6_alarm.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +02001422 &sensor_dev_attr_in7_input.dev_attr.attr,
1423 &sensor_dev_attr_in7_max.dev_attr.attr,
1424 &sensor_dev_attr_in7_min.dev_attr.attr,
Jean Delvarea9273cb2007-11-29 23:45:22 +01001425 &sensor_dev_attr_in7_alarm.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +02001426 &sensor_dev_attr_in10_input.dev_attr.attr,
1427 &sensor_dev_attr_in10_max.dev_attr.attr,
1428 &sensor_dev_attr_in10_min.dev_attr.attr,
Jean Delvarea9273cb2007-11-29 23:45:22 +01001429 &sensor_dev_attr_in10_alarm.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +02001430 &sensor_dev_attr_in11_input.dev_attr.attr,
1431 &sensor_dev_attr_in11_max.dev_attr.attr,
1432 &sensor_dev_attr_in11_min.dev_attr.attr,
Jean Delvarea9273cb2007-11-29 23:45:22 +01001433 &sensor_dev_attr_in11_alarm.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +02001434 &sensor_dev_attr_in12_input.dev_attr.attr,
1435 &sensor_dev_attr_in12_max.dev_attr.attr,
1436 &sensor_dev_attr_in12_min.dev_attr.attr,
Jean Delvarea9273cb2007-11-29 23:45:22 +01001437 &sensor_dev_attr_in12_alarm.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +02001438 &sensor_dev_attr_in13_input.dev_attr.attr,
1439 &sensor_dev_attr_in13_max.dev_attr.attr,
1440 &sensor_dev_attr_in13_min.dev_attr.attr,
Jean Delvarea9273cb2007-11-29 23:45:22 +01001441 &sensor_dev_attr_in13_alarm.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +02001442 &sensor_dev_attr_in14_input.dev_attr.attr,
1443 &sensor_dev_attr_in14_max.dev_attr.attr,
1444 &sensor_dev_attr_in14_min.dev_attr.attr,
Jean Delvarea9273cb2007-11-29 23:45:22 +01001445 &sensor_dev_attr_in14_alarm.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +02001446 &sensor_dev_attr_in15_input.dev_attr.attr,
1447 &sensor_dev_attr_in15_max.dev_attr.attr,
1448 &sensor_dev_attr_in15_min.dev_attr.attr,
Jean Delvarea9273cb2007-11-29 23:45:22 +01001449 &sensor_dev_attr_in15_alarm.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +02001450 &sensor_dev_attr_in16_input.dev_attr.attr,
1451 &sensor_dev_attr_in16_max.dev_attr.attr,
1452 &sensor_dev_attr_in16_min.dev_attr.attr,
Jean Delvarea9273cb2007-11-29 23:45:22 +01001453 &sensor_dev_attr_in16_alarm.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +02001454 &sensor_dev_attr_fan1_input.dev_attr.attr,
1455 &sensor_dev_attr_fan1_div.dev_attr.attr,
1456 &sensor_dev_attr_fan1_min.dev_attr.attr,
Jean Delvarea9273cb2007-11-29 23:45:22 +01001457 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +02001458 &sensor_dev_attr_fan2_input.dev_attr.attr,
1459 &sensor_dev_attr_fan2_div.dev_attr.attr,
1460 &sensor_dev_attr_fan2_min.dev_attr.attr,
Jean Delvarea9273cb2007-11-29 23:45:22 +01001461 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +02001462 &sensor_dev_attr_fan3_input.dev_attr.attr,
1463 &sensor_dev_attr_fan3_div.dev_attr.attr,
1464 &sensor_dev_attr_fan3_min.dev_attr.attr,
Jean Delvarea9273cb2007-11-29 23:45:22 +01001465 &sensor_dev_attr_fan3_alarm.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +02001466 &sensor_dev_attr_fan4_input.dev_attr.attr,
1467 &sensor_dev_attr_fan4_div.dev_attr.attr,
1468 &sensor_dev_attr_fan4_min.dev_attr.attr,
Jean Delvarea9273cb2007-11-29 23:45:22 +01001469 &sensor_dev_attr_fan4_alarm.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +02001470 &sensor_dev_attr_fan5_input.dev_attr.attr,
1471 &sensor_dev_attr_fan5_div.dev_attr.attr,
1472 &sensor_dev_attr_fan5_min.dev_attr.attr,
Jean Delvarea9273cb2007-11-29 23:45:22 +01001473 &sensor_dev_attr_fan5_alarm.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +02001474 &sensor_dev_attr_fan6_input.dev_attr.attr,
1475 &sensor_dev_attr_fan6_div.dev_attr.attr,
1476 &sensor_dev_attr_fan6_min.dev_attr.attr,
Jean Delvarea9273cb2007-11-29 23:45:22 +01001477 &sensor_dev_attr_fan6_alarm.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +02001478 &sensor_dev_attr_fan7_input.dev_attr.attr,
1479 &sensor_dev_attr_fan7_div.dev_attr.attr,
1480 &sensor_dev_attr_fan7_min.dev_attr.attr,
Jean Delvarea9273cb2007-11-29 23:45:22 +01001481 &sensor_dev_attr_fan7_alarm.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +02001482 &sensor_dev_attr_fan8_input.dev_attr.attr,
1483 &sensor_dev_attr_fan8_div.dev_attr.attr,
1484 &sensor_dev_attr_fan8_min.dev_attr.attr,
Jean Delvarea9273cb2007-11-29 23:45:22 +01001485 &sensor_dev_attr_fan8_alarm.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +02001486 &sensor_dev_attr_temp1_input.dev_attr.attr,
1487 &sensor_dev_attr_temp1_max.dev_attr.attr,
1488 &sensor_dev_attr_temp1_min.dev_attr.attr,
Jean Delvarea9273cb2007-11-29 23:45:22 +01001489 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +02001490 &sensor_dev_attr_temp2_input.dev_attr.attr,
1491 &sensor_dev_attr_temp2_max.dev_attr.attr,
1492 &sensor_dev_attr_temp2_min.dev_attr.attr,
Jean Delvarea9273cb2007-11-29 23:45:22 +01001493 &sensor_dev_attr_temp2_alarm.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +02001494 &sensor_dev_attr_temp1_offset.dev_attr.attr,
1495 &sensor_dev_attr_temp2_offset.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +02001496 &sensor_dev_attr_temp1_auto_point1_temp.dev_attr.attr,
1497 &sensor_dev_attr_temp2_auto_point1_temp.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +02001498 &sensor_dev_attr_temp1_auto_point1_temp_hyst.dev_attr.attr,
1499 &sensor_dev_attr_temp2_auto_point1_temp_hyst.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +02001500 &sensor_dev_attr_temp1_auto_point2_temp.dev_attr.attr,
1501 &sensor_dev_attr_temp2_auto_point2_temp.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +02001502 &sensor_dev_attr_temp1_crit.dev_attr.attr,
1503 &sensor_dev_attr_temp2_crit.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +02001504 &dev_attr_temp1_crit_enable.attr,
1505 &dev_attr_temp2_crit_enable.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +02001506 &dev_attr_cpu0_vid.attr,
1507 &dev_attr_vrm.attr,
1508 &dev_attr_alarms.attr,
1509 &dev_attr_alarm_mask.attr,
1510 &dev_attr_gpio.attr,
1511 &dev_attr_gpio_mask.attr,
1512 &dev_attr_pwm1.attr,
1513 &dev_attr_pwm2.attr,
1514 &dev_attr_pwm3.attr,
1515 &dev_attr_pwm1_enable.attr,
1516 &dev_attr_pwm2_enable.attr,
1517 &dev_attr_pwm3_enable.attr,
1518 &dev_attr_temp1_auto_point1_pwm.attr,
1519 &dev_attr_temp2_auto_point1_pwm.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +02001520 &dev_attr_temp1_auto_point2_pwm.attr,
1521 &dev_attr_temp2_auto_point2_pwm.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +02001522 &dev_attr_analog_out.attr,
1523 NULL
1524};
1525
1526static const struct attribute_group adm1026_group = {
1527 .attrs = adm1026_attributes,
1528};
1529
Jean Delvare5b34dbc2007-11-29 23:47:54 +01001530static struct attribute *adm1026_attributes_temp3[] = {
1531 &sensor_dev_attr_temp3_input.dev_attr.attr,
1532 &sensor_dev_attr_temp3_max.dev_attr.attr,
1533 &sensor_dev_attr_temp3_min.dev_attr.attr,
1534 &sensor_dev_attr_temp3_alarm.dev_attr.attr,
1535 &sensor_dev_attr_temp3_offset.dev_attr.attr,
1536 &sensor_dev_attr_temp3_auto_point1_temp.dev_attr.attr,
1537 &sensor_dev_attr_temp3_auto_point1_temp_hyst.dev_attr.attr,
1538 &sensor_dev_attr_temp3_auto_point2_temp.dev_attr.attr,
1539 &sensor_dev_attr_temp3_crit.dev_attr.attr,
1540 &dev_attr_temp3_crit_enable.attr,
1541 &dev_attr_temp3_auto_point1_pwm.attr,
1542 &dev_attr_temp3_auto_point2_pwm.attr,
Jean Delvare1d5f2c12008-02-10 19:01:15 +01001543 NULL
Jean Delvare5b34dbc2007-11-29 23:47:54 +01001544};
1545
1546static const struct attribute_group adm1026_group_temp3 = {
1547 .attrs = adm1026_attributes_temp3,
1548};
1549
1550static struct attribute *adm1026_attributes_in8_9[] = {
1551 &sensor_dev_attr_in8_input.dev_attr.attr,
1552 &sensor_dev_attr_in8_max.dev_attr.attr,
1553 &sensor_dev_attr_in8_min.dev_attr.attr,
1554 &sensor_dev_attr_in8_alarm.dev_attr.attr,
1555 &sensor_dev_attr_in9_input.dev_attr.attr,
1556 &sensor_dev_attr_in9_max.dev_attr.attr,
1557 &sensor_dev_attr_in9_min.dev_attr.attr,
1558 &sensor_dev_attr_in9_alarm.dev_attr.attr,
Jean Delvare1d5f2c12008-02-10 19:01:15 +01001559 NULL
Jean Delvare5b34dbc2007-11-29 23:47:54 +01001560};
1561
1562static const struct attribute_group adm1026_group_in8_9 = {
1563 .attrs = adm1026_attributes_in8_9,
1564};
1565
Jean Delvare57f7eb02008-07-16 19:30:08 +02001566/* Return 0 if detection is successful, -ENODEV otherwise */
Jean Delvare310ec792009-12-14 21:17:23 +01001567static int adm1026_detect(struct i2c_client *client,
Jean Delvare57f7eb02008-07-16 19:30:08 +02001568 struct i2c_board_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569{
Jean Delvare57f7eb02008-07-16 19:30:08 +02001570 struct i2c_adapter *adapter = client->adapter;
1571 int address = client->addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 int company, verstep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573
1574 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
1575 /* We need to be able to do byte I/O */
Jean Delvare57f7eb02008-07-16 19:30:08 +02001576 return -ENODEV;
Guenter Roecka0393712013-03-29 13:54:50 -07001577 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 /* Now, we do the remaining detection. */
1580
Jean Delvaref67fdab2007-12-01 11:24:17 +01001581 company = adm1026_read_value(client, ADM1026_REG_COMPANY);
1582 verstep = adm1026_read_value(client, ADM1026_REG_VERSTEP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583
Guenter Roeckb55f3752013-01-10 10:01:24 -08001584 dev_dbg(&adapter->dev,
1585 "Detecting device at %d,0x%02x with COMPANY: 0x%02x and VERSTEP: 0x%02x\n",
Jean Delvaref67fdab2007-12-01 11:24:17 +01001586 i2c_adapter_id(client->adapter), client->addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 company, verstep);
1588
Jean Delvare52df6442009-12-09 20:35:57 +01001589 /* Determine the chip type. */
1590 dev_dbg(&adapter->dev, "Autodetecting device at %d,0x%02x...\n",
1591 i2c_adapter_id(adapter), address);
1592 if (company == ADM1026_COMPANY_ANALOG_DEV
1593 && verstep == ADM1026_VERSTEP_ADM1026) {
1594 /* Analog Devices ADM1026 */
1595 } else if (company == ADM1026_COMPANY_ANALOG_DEV
1596 && (verstep & 0xf0) == ADM1026_VERSTEP_GENERIC) {
Guenter Roeckb55f3752013-01-10 10:01:24 -08001597 dev_err(&adapter->dev,
1598 "Unrecognized stepping 0x%02x. Defaulting to ADM1026.\n",
1599 verstep);
Jean Delvare52df6442009-12-09 20:35:57 +01001600 } else if ((verstep & 0xf0) == ADM1026_VERSTEP_GENERIC) {
Guenter Roeckb55f3752013-01-10 10:01:24 -08001601 dev_err(&adapter->dev,
1602 "Found version/stepping 0x%02x. Assuming generic ADM1026.\n",
Jean Delvare52df6442009-12-09 20:35:57 +01001603 verstep);
1604 } else {
1605 dev_dbg(&adapter->dev, "Autodetection failed\n");
1606 /* Not an ADM1026... */
1607 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 }
Jean Delvare52df6442009-12-09 20:35:57 +01001609
Jean Delvare57f7eb02008-07-16 19:30:08 +02001610 strlcpy(info->type, "adm1026", I2C_NAME_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611
Jean Delvare57f7eb02008-07-16 19:30:08 +02001612 return 0;
1613}
1614
Axel Lin97df5562014-07-03 21:51:43 +08001615static void adm1026_print_gpio(struct i2c_client *client)
1616{
1617 struct adm1026_data *data = i2c_get_clientdata(client);
1618 int i;
1619
1620 dev_dbg(&client->dev, "GPIO config is:\n");
1621 for (i = 0; i <= 7; ++i) {
1622 if (data->config2 & (1 << i)) {
1623 dev_dbg(&client->dev, "\t%sGP%s%d\n",
1624 data->gpio_config[i] & 0x02 ? "" : "!",
1625 data->gpio_config[i] & 0x01 ? "OUT" : "IN",
1626 i);
1627 } else {
1628 dev_dbg(&client->dev, "\tFAN%d\n", i);
1629 }
1630 }
1631 for (i = 8; i <= 15; ++i) {
1632 dev_dbg(&client->dev, "\t%sGP%s%d\n",
1633 data->gpio_config[i] & 0x02 ? "" : "!",
1634 data->gpio_config[i] & 0x01 ? "OUT" : "IN",
1635 i);
1636 }
1637 if (data->config3 & CFG3_GPIO16_ENABLE) {
1638 dev_dbg(&client->dev, "\t%sGP%s16\n",
1639 data->gpio_config[16] & 0x02 ? "" : "!",
1640 data->gpio_config[16] & 0x01 ? "OUT" : "IN");
1641 } else {
1642 /* GPIO16 is THERM */
1643 dev_dbg(&client->dev, "\tTHERM\n");
1644 }
1645}
1646
1647static void adm1026_fixup_gpio(struct i2c_client *client)
1648{
1649 struct adm1026_data *data = i2c_get_clientdata(client);
1650 int i;
1651 int value;
1652
1653 /* Make the changes requested. */
1654 /*
1655 * We may need to unlock/stop monitoring or soft-reset the
1656 * chip before we can make changes. This hasn't been
1657 * tested much. FIXME
1658 */
1659
1660 /* Make outputs */
1661 for (i = 0; i <= 16; ++i) {
1662 if (gpio_output[i] >= 0 && gpio_output[i] <= 16)
1663 data->gpio_config[gpio_output[i]] |= 0x01;
1664 /* if GPIO0-7 is output, it isn't a FAN tach */
1665 if (gpio_output[i] >= 0 && gpio_output[i] <= 7)
1666 data->config2 |= 1 << gpio_output[i];
1667 }
1668
1669 /* Input overrides output */
1670 for (i = 0; i <= 16; ++i) {
1671 if (gpio_input[i] >= 0 && gpio_input[i] <= 16)
1672 data->gpio_config[gpio_input[i]] &= ~0x01;
1673 /* if GPIO0-7 is input, it isn't a FAN tach */
1674 if (gpio_input[i] >= 0 && gpio_input[i] <= 7)
1675 data->config2 |= 1 << gpio_input[i];
1676 }
1677
1678 /* Inverted */
1679 for (i = 0; i <= 16; ++i) {
1680 if (gpio_inverted[i] >= 0 && gpio_inverted[i] <= 16)
1681 data->gpio_config[gpio_inverted[i]] &= ~0x02;
1682 }
1683
1684 /* Normal overrides inverted */
1685 for (i = 0; i <= 16; ++i) {
1686 if (gpio_normal[i] >= 0 && gpio_normal[i] <= 16)
1687 data->gpio_config[gpio_normal[i]] |= 0x02;
1688 }
1689
1690 /* Fan overrides input and output */
1691 for (i = 0; i <= 7; ++i) {
1692 if (gpio_fan[i] >= 0 && gpio_fan[i] <= 7)
1693 data->config2 &= ~(1 << gpio_fan[i]);
1694 }
1695
1696 /* Write new configs to registers */
1697 adm1026_write_value(client, ADM1026_REG_CONFIG2, data->config2);
1698 data->config3 = (data->config3 & 0x3f)
1699 | ((data->gpio_config[16] & 0x03) << 6);
1700 adm1026_write_value(client, ADM1026_REG_CONFIG3, data->config3);
1701 for (i = 15, value = 0; i >= 0; --i) {
1702 value <<= 2;
1703 value |= data->gpio_config[i] & 0x03;
1704 if ((i & 0x03) == 0) {
1705 adm1026_write_value(client,
1706 ADM1026_REG_GPIO_CFG_0_3 + i/4,
1707 value);
1708 value = 0;
1709 }
1710 }
1711
1712 /* Print the new config */
1713 adm1026_print_gpio(client);
1714}
1715
1716static void adm1026_init_client(struct i2c_client *client)
1717{
1718 int value, i;
1719 struct adm1026_data *data = i2c_get_clientdata(client);
1720
1721 dev_dbg(&client->dev, "Initializing device\n");
1722 /* Read chip config */
1723 data->config1 = adm1026_read_value(client, ADM1026_REG_CONFIG1);
1724 data->config2 = adm1026_read_value(client, ADM1026_REG_CONFIG2);
1725 data->config3 = adm1026_read_value(client, ADM1026_REG_CONFIG3);
1726
1727 /* Inform user of chip config */
1728 dev_dbg(&client->dev, "ADM1026_REG_CONFIG1 is: 0x%02x\n",
1729 data->config1);
1730 if ((data->config1 & CFG1_MONITOR) == 0) {
1731 dev_dbg(&client->dev,
1732 "Monitoring not currently enabled.\n");
1733 }
1734 if (data->config1 & CFG1_INT_ENABLE) {
1735 dev_dbg(&client->dev,
1736 "SMBALERT interrupts are enabled.\n");
1737 }
1738 if (data->config1 & CFG1_AIN8_9) {
1739 dev_dbg(&client->dev,
1740 "in8 and in9 enabled. temp3 disabled.\n");
1741 } else {
1742 dev_dbg(&client->dev,
1743 "temp3 enabled. in8 and in9 disabled.\n");
1744 }
1745 if (data->config1 & CFG1_THERM_HOT) {
1746 dev_dbg(&client->dev,
1747 "Automatic THERM, PWM, and temp limits enabled.\n");
1748 }
1749
1750 if (data->config3 & CFG3_GPIO16_ENABLE) {
1751 dev_dbg(&client->dev,
1752 "GPIO16 enabled. THERM pin disabled.\n");
1753 } else {
1754 dev_dbg(&client->dev,
1755 "THERM pin enabled. GPIO16 disabled.\n");
1756 }
1757 if (data->config3 & CFG3_VREF_250)
1758 dev_dbg(&client->dev, "Vref is 2.50 Volts.\n");
1759 else
1760 dev_dbg(&client->dev, "Vref is 1.82 Volts.\n");
1761 /* Read and pick apart the existing GPIO configuration */
1762 value = 0;
1763 for (i = 0; i <= 15; ++i) {
1764 if ((i & 0x03) == 0) {
1765 value = adm1026_read_value(client,
1766 ADM1026_REG_GPIO_CFG_0_3 + i / 4);
1767 }
1768 data->gpio_config[i] = value & 0x03;
1769 value >>= 2;
1770 }
1771 data->gpio_config[16] = (data->config3 >> 6) & 0x03;
1772
1773 /* ... and then print it */
1774 adm1026_print_gpio(client);
1775
1776 /*
1777 * If the user asks us to reprogram the GPIO config, then
1778 * do it now.
1779 */
1780 if (gpio_input[0] != -1 || gpio_output[0] != -1
1781 || gpio_inverted[0] != -1 || gpio_normal[0] != -1
1782 || gpio_fan[0] != -1) {
1783 adm1026_fixup_gpio(client);
1784 }
1785
1786 /*
1787 * WE INTENTIONALLY make no changes to the limits,
1788 * offsets, pwms, fans and zones. If they were
1789 * configured, we don't want to mess with them.
1790 * If they weren't, the default is 100% PWM, no
1791 * control and will suffice until 'sensors -s'
1792 * can be run by the user. We DO set the default
1793 * value for pwm1.auto_pwm_min to its maximum
1794 * so that enabling automatic pwm fan control
1795 * without first setting a value for pwm1.auto_pwm_min
1796 * will not result in potentially dangerous fan speed decrease.
1797 */
1798 data->pwm1.auto_pwm_min = 255;
1799 /* Start monitoring */
1800 value = adm1026_read_value(client, ADM1026_REG_CONFIG1);
1801 /* Set MONITOR, clear interrupt acknowledge and s/w reset */
1802 value = (value | CFG1_MONITOR) & (~CFG1_INT_CLEAR & ~CFG1_RESET);
1803 dev_dbg(&client->dev, "Setting CONFIG to: 0x%02x\n", value);
1804 data->config1 = value;
1805 adm1026_write_value(client, ADM1026_REG_CONFIG1, value);
1806
1807 /* initialize fan_div[] to hardware defaults */
1808 value = adm1026_read_value(client, ADM1026_REG_FAN_DIV_0_3) |
1809 (adm1026_read_value(client, ADM1026_REG_FAN_DIV_4_7) << 8);
1810 for (i = 0; i <= 7; ++i) {
1811 data->fan_div[i] = DIV_FROM_REG(value & 0x03);
1812 value >>= 2;
1813 }
1814}
1815
Jean Delvare57f7eb02008-07-16 19:30:08 +02001816static int adm1026_probe(struct i2c_client *client,
1817 const struct i2c_device_id *id)
1818{
Axel Linb591d3c2014-07-03 21:53:01 +08001819 struct device *dev = &client->dev;
1820 struct device *hwmon_dev;
Jean Delvare57f7eb02008-07-16 19:30:08 +02001821 struct adm1026_data *data;
Jean Delvare57f7eb02008-07-16 19:30:08 +02001822
Axel Linb591d3c2014-07-03 21:53:01 +08001823 data = devm_kzalloc(dev, sizeof(struct adm1026_data), GFP_KERNEL);
Guenter Roeck3421e212012-06-02 09:57:59 -07001824 if (!data)
1825 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826
Jean Delvare57f7eb02008-07-16 19:30:08 +02001827 i2c_set_clientdata(client, data);
Axel Linb591d3c2014-07-03 21:53:01 +08001828 data->client = client;
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001829 mutex_init(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 /* Set the VRM version */
Jean Delvare303760b2005-07-31 21:52:01 +02001832 data->vrm = vid_which_vrm();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833
1834 /* Initialize the ADM1026 chip */
Jean Delvaref67fdab2007-12-01 11:24:17 +01001835 adm1026_init_client(client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836
Axel Linb591d3c2014-07-03 21:53:01 +08001837 /* sysfs hooks */
1838 data->groups[0] = &adm1026_group;
Jean Delvare5b34dbc2007-11-29 23:47:54 +01001839 if (data->config1 & CFG1_AIN8_9)
Axel Linb591d3c2014-07-03 21:53:01 +08001840 data->groups[1] = &adm1026_group_in8_9;
Jean Delvare5b34dbc2007-11-29 23:47:54 +01001841 else
Axel Linb591d3c2014-07-03 21:53:01 +08001842 data->groups[1] = &adm1026_group_temp3;
Mark M. Hoffman681c6f72006-09-24 21:15:35 +02001843
Axel Linb591d3c2014-07-03 21:53:01 +08001844 hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
1845 data, data->groups);
1846 return PTR_ERR_OR_ZERO(hwmon_dev);
Mark M. Hoffman681c6f72006-09-24 21:15:35 +02001847}
1848
Axel Lin97df5562014-07-03 21:51:43 +08001849static const struct i2c_device_id adm1026_id[] = {
1850 { "adm1026", 0 },
1851 { }
1852};
1853MODULE_DEVICE_TABLE(i2c, adm1026_id);
1854
1855static struct i2c_driver adm1026_driver = {
1856 .class = I2C_CLASS_HWMON,
1857 .driver = {
1858 .name = "adm1026",
1859 },
1860 .probe = adm1026_probe,
Axel Lin97df5562014-07-03 21:51:43 +08001861 .id_table = adm1026_id,
1862 .detect = adm1026_detect,
1863 .address_list = normal_i2c,
1864};
1865
Axel Linf0967ee2012-01-20 15:38:18 +08001866module_i2c_driver(adm1026_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867
1868MODULE_LICENSE("GPL");
1869MODULE_AUTHOR("Philip Pokorny <ppokorny@penguincomputing.com>, "
Jean Delvarecb01a232007-11-29 23:46:42 +01001870 "Justin Thiessen <jthiessen@penguincomputing.com>");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871MODULE_DESCRIPTION("ADM1026 driver");