blob: 6f257ce1ae079f89147ee655121ad9c30efb8200 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 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
Justin P. Mattock631dd1a2010-10-18 11:03:14 +02009 <http://www.onsemi.com/PowerSolutions/product.do?id=ADM1026>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010
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*/
25
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);
52MODULE_PARM_DESC(gpio_output, "List of GPIO pins (0-16) to program as "
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 "outputs");
Jean Delvarecb01a232007-11-29 23:46:42 +010054module_param_array(gpio_inverted, int, NULL, 0);
55MODULE_PARM_DESC(gpio_inverted, "List of GPIO pins (0-16) to program as "
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 "inverted");
Jean Delvarecb01a232007-11-29 23:46:42 +010057module_param_array(gpio_normal, int, NULL, 0);
58MODULE_PARM_DESC(gpio_normal, "List of GPIO pins (0-16) to program as "
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 "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
93/* There are 10 general analog inputs and 7 dedicated inputs
94 * They are:
95 * 0 - 9 = AIN0 - AIN9
96 * 10 = Vbat
97 * 11 = 3.3V Standby
98 * 12 = 3.3V Main
99 * 13 = +5V
100 * 14 = Vccp (CPU core voltage)
101 * 15 = +12V
102 * 16 = -12V
103 */
104static u16 ADM1026_REG_IN[] = {
105 0x30, 0x31, 0x32, 0x33, 0x34, 0x35,
106 0x36, 0x37, 0x27, 0x29, 0x26, 0x2a,
107 0x2b, 0x2c, 0x2d, 0x2e, 0x2f
108 };
109static u16 ADM1026_REG_IN_MIN[] = {
110 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d,
111 0x5e, 0x5f, 0x6d, 0x49, 0x6b, 0x4a,
112 0x4b, 0x4c, 0x4d, 0x4e, 0x4f
113 };
114static u16 ADM1026_REG_IN_MAX[] = {
115 0x50, 0x51, 0x52, 0x53, 0x54, 0x55,
116 0x56, 0x57, 0x6c, 0x41, 0x6a, 0x42,
117 0x43, 0x44, 0x45, 0x46, 0x47
118 };
119
120/* Temperatures are:
121 * 0 - Internal
122 * 1 - External 1
123 * 2 - External 2
124 */
125static u16 ADM1026_REG_TEMP[] = { 0x1f, 0x28, 0x29 };
126static u16 ADM1026_REG_TEMP_MIN[] = { 0x69, 0x48, 0x49 };
127static u16 ADM1026_REG_TEMP_MAX[] = { 0x68, 0x40, 0x41 };
128static u16 ADM1026_REG_TEMP_TMIN[] = { 0x10, 0x11, 0x12 };
129static u16 ADM1026_REG_TEMP_THERM[] = { 0x0d, 0x0e, 0x0f };
130static u16 ADM1026_REG_TEMP_OFFSET[] = { 0x1e, 0x6e, 0x6f };
131
Jean Delvarecb01a232007-11-29 23:46:42 +0100132#define ADM1026_REG_FAN(nr) (0x38 + (nr))
133#define ADM1026_REG_FAN_MIN(nr) (0x60 + (nr))
134#define ADM1026_REG_FAN_DIV_0_3 0x02
135#define ADM1026_REG_FAN_DIV_4_7 0x03
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
Jean Delvarecb01a232007-11-29 23:46:42 +0100137#define ADM1026_REG_DAC 0x04
138#define ADM1026_REG_PWM 0x05
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
Jean Delvarecb01a232007-11-29 23:46:42 +0100140#define ADM1026_REG_GPIO_CFG_0_3 0x08
141#define ADM1026_REG_GPIO_CFG_4_7 0x09
142#define ADM1026_REG_GPIO_CFG_8_11 0x0a
143#define ADM1026_REG_GPIO_CFG_12_15 0x0b
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144/* CFG_16 in REG_CFG3 */
Jean Delvarecb01a232007-11-29 23:46:42 +0100145#define ADM1026_REG_GPIO_STATUS_0_7 0x24
146#define ADM1026_REG_GPIO_STATUS_8_15 0x25
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147/* STATUS_16 in REG_STATUS4 */
Jean Delvarecb01a232007-11-29 23:46:42 +0100148#define ADM1026_REG_GPIO_MASK_0_7 0x1c
149#define ADM1026_REG_GPIO_MASK_8_15 0x1d
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150/* MASK_16 in REG_MASK4 */
151
Jean Delvarecb01a232007-11-29 23:46:42 +0100152#define ADM1026_REG_COMPANY 0x16
153#define ADM1026_REG_VERSTEP 0x17
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154/* These are the recognized values for the above regs */
Jean Delvarecb01a232007-11-29 23:46:42 +0100155#define ADM1026_COMPANY_ANALOG_DEV 0x41
156#define ADM1026_VERSTEP_GENERIC 0x40
157#define ADM1026_VERSTEP_ADM1026 0x44
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
Jean Delvarecb01a232007-11-29 23:46:42 +0100159#define ADM1026_REG_MASK1 0x18
160#define ADM1026_REG_MASK2 0x19
161#define ADM1026_REG_MASK3 0x1a
162#define ADM1026_REG_MASK4 0x1b
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
Jean Delvarecb01a232007-11-29 23:46:42 +0100164#define ADM1026_REG_STATUS1 0x20
165#define ADM1026_REG_STATUS2 0x21
166#define ADM1026_REG_STATUS3 0x22
167#define ADM1026_REG_STATUS4 0x23
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168
169#define ADM1026_FAN_ACTIVATION_TEMP_HYST -6
Jean Delvarecb01a232007-11-29 23:46:42 +0100170#define ADM1026_FAN_CONTROL_TEMP_RANGE 20
171#define ADM1026_PWM_MAX 255
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
Jean Delvarecb01a232007-11-29 23:46:42 +0100173/* Conversions. Rounding and limit checking is only done on the TO_REG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 * variants. Note that you should be a bit careful with which arguments
175 * these macros are called: arguments may be evaluated more than once.
176 */
177
178/* IN are scaled acording to built-in resistors. These are the
179 * voltages corresponding to 3/4 of full scale (192 or 0xc0)
180 * NOTE: The -12V input needs an additional factor to account
181 * for the Vref pullup resistor.
182 * NEG12_OFFSET = SCALE * Vref / V-192 - Vref
183 * = 13875 * 2.50 / 1.875 - 2500
184 * = 16000
185 *
186 * The values in this table are based on Table II, page 15 of the
187 * datasheet.
188 */
Jean Delvarecb01a232007-11-29 23:46:42 +0100189static int adm1026_scaling[] = { /* .001 Volts */
190 2250, 2250, 2250, 2250, 2250, 2250,
191 1875, 1875, 1875, 1875, 3000, 3330,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 3330, 4995, 2250, 12000, 13875
193 };
194#define NEG12_OFFSET 16000
Jean Delvarecb01a232007-11-29 23:46:42 +0100195#define SCALE(val, from, to) (((val)*(to) + ((from)/2))/(from))
196#define INS_TO_REG(n, val) (SENSORS_LIMIT(SCALE(val, adm1026_scaling[n], 192),\
197 0, 255))
198#define INS_FROM_REG(n, val) (SCALE(val, 192, adm1026_scaling[n]))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
200/* FAN speed is measured using 22.5kHz clock and counts for 2 pulses
201 * and we assume a 2 pulse-per-rev fan tach signal
202 * 22500 kHz * 60 (sec/min) * 2 (pulse) / 2 (pulse/rev) == 1350000
203 */
Jean Delvarecb01a232007-11-29 23:46:42 +0100204#define FAN_TO_REG(val, div) ((val) <= 0 ? 0xff : \
205 SENSORS_LIMIT(1350000/((val)*(div)), 1, 254))
206#define FAN_FROM_REG(val, div) ((val) == 0 ? -1:(val) == 0xff ? 0 : \
207 1350000/((val)*(div)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208#define DIV_FROM_REG(val) (1<<(val))
Jean Delvarecb01a232007-11-29 23:46:42 +0100209#define DIV_TO_REG(val) ((val) >= 8 ? 3 : (val) >= 4 ? 2 : (val) >= 2 ? 1 : 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
211/* Temperature is reported in 1 degC increments */
212#define TEMP_TO_REG(val) (SENSORS_LIMIT(((val)+((val)<0 ? -500 : 500))/1000,\
Jean Delvarecb01a232007-11-29 23:46:42 +0100213 -127, 127))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214#define TEMP_FROM_REG(val) ((val) * 1000)
215#define OFFSET_TO_REG(val) (SENSORS_LIMIT(((val)+((val)<0 ? -500 : 500))/1000,\
Jean Delvarecb01a232007-11-29 23:46:42 +0100216 -127, 127))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217#define OFFSET_FROM_REG(val) ((val) * 1000)
218
Jean Delvarecb01a232007-11-29 23:46:42 +0100219#define PWM_TO_REG(val) (SENSORS_LIMIT(val, 0, 255))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220#define PWM_FROM_REG(val) (val)
221
222#define PWM_MIN_TO_REG(val) ((val) & 0xf0)
223#define PWM_MIN_FROM_REG(val) (((val) & 0xf0) + ((val) >> 4))
224
Jean Delvarecb01a232007-11-29 23:46:42 +0100225/* Analog output is a voltage, and scaled to millivolts. The datasheet
226 * indicates that the DAC could be used to drive the fans, but in our
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 * example board (Arima HDAMA) it isn't connected to the fans at all.
228 */
Jean Delvarecb01a232007-11-29 23:46:42 +0100229#define DAC_TO_REG(val) (SENSORS_LIMIT(((((val)*255)+500)/2500), 0, 255))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230#define DAC_FROM_REG(val) (((val)*2500)/255)
231
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232/* Chip sampling rates
233 *
234 * Some sensors are not updated more frequently than once per second
235 * so it doesn't make sense to read them more often than that.
236 * We cache the results and return the saved data if the driver
237 * is called again before a second has elapsed.
238 *
239 * Also, there is significant configuration data for this chip
240 * So, we keep the config data up to date in the cache
241 * when it is written and only sample it once every 5 *minutes*
242 */
Jean Delvarecb01a232007-11-29 23:46:42 +0100243#define ADM1026_DATA_INTERVAL (1 * HZ)
244#define ADM1026_CONFIG_INTERVAL (5 * 60 * HZ)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245
246/* We allow for multiple chips in a single system.
247 *
248 * For each registered ADM1026, we need to keep state information
249 * at client->data. The adm1026_data structure is dynamically
250 * allocated, when a new client structure is allocated. */
251
252struct pwm_data {
253 u8 pwm;
254 u8 enable;
255 u8 auto_pwm_min;
256};
257
258struct adm1026_data {
Tony Jones1beeffe2007-08-20 13:46:20 -0700259 struct device *hwmon_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100261 struct mutex update_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 int valid; /* !=0 if following fields are valid */
263 unsigned long last_reading; /* In jiffies */
264 unsigned long last_config; /* In jiffies */
265
Jean Delvarecb01a232007-11-29 23:46:42 +0100266 u8 in[17]; /* Register value */
267 u8 in_max[17]; /* Register value */
268 u8 in_min[17]; /* Register value */
269 s8 temp[3]; /* Register value */
270 s8 temp_min[3]; /* Register value */
271 s8 temp_max[3]; /* Register value */
272 s8 temp_tmin[3]; /* Register value */
273 s8 temp_crit[3]; /* Register value */
274 s8 temp_offset[3]; /* Register value */
275 u8 fan[8]; /* Register value */
276 u8 fan_min[8]; /* Register value */
277 u8 fan_div[8]; /* Decoded value */
278 struct pwm_data pwm1; /* Pwm control values */
Jean Delvarecb01a232007-11-29 23:46:42 +0100279 u8 vrm; /* VRM version */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 u8 analog_out; /* Register value (DAC) */
Jean Delvarecb01a232007-11-29 23:46:42 +0100281 long alarms; /* Register encoding, combined */
282 long alarm_mask; /* Register encoding, combined */
283 long gpio; /* Register encoding, combined */
284 long gpio_mask; /* Register encoding, combined */
285 u8 gpio_config[17]; /* Decoded value */
286 u8 config1; /* Register value */
287 u8 config2; /* Register value */
288 u8 config3; /* Register value */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289};
290
Jean Delvare57f7eb02008-07-16 19:30:08 +0200291static int adm1026_probe(struct i2c_client *client,
292 const struct i2c_device_id *id);
Jean Delvare310ec792009-12-14 21:17:23 +0100293static int adm1026_detect(struct i2c_client *client,
Jean Delvare57f7eb02008-07-16 19:30:08 +0200294 struct i2c_board_info *info);
295static int adm1026_remove(struct i2c_client *client);
Darren Jenkinsf6c27fc2006-02-27 23:14:58 +0100296static int adm1026_read_value(struct i2c_client *client, u8 reg);
297static int adm1026_write_value(struct i2c_client *client, u8 reg, int value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298static void adm1026_print_gpio(struct i2c_client *client);
Jean Delvarecb01a232007-11-29 23:46:42 +0100299static void adm1026_fixup_gpio(struct i2c_client *client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300static struct adm1026_data *adm1026_update_device(struct device *dev);
301static void adm1026_init_client(struct i2c_client *client);
302
303
Jean Delvare57f7eb02008-07-16 19:30:08 +0200304static const struct i2c_device_id adm1026_id[] = {
Jean Delvare1f86df42009-12-14 21:17:26 +0100305 { "adm1026", 0 },
Jean Delvare57f7eb02008-07-16 19:30:08 +0200306 { }
307};
308MODULE_DEVICE_TABLE(i2c, adm1026_id);
309
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310static struct i2c_driver adm1026_driver = {
Jean Delvare57f7eb02008-07-16 19:30:08 +0200311 .class = I2C_CLASS_HWMON,
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100312 .driver = {
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100313 .name = "adm1026",
314 },
Jean Delvare57f7eb02008-07-16 19:30:08 +0200315 .probe = adm1026_probe,
316 .remove = adm1026_remove,
317 .id_table = adm1026_id,
318 .detect = adm1026_detect,
Jean Delvarec3813d62009-12-14 21:17:25 +0100319 .address_list = normal_i2c,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320};
321
Ben Dooksc49efce2005-10-26 21:07:25 +0200322static int adm1026_read_value(struct i2c_client *client, u8 reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323{
324 int res;
325
326 if (reg < 0x80) {
327 /* "RAM" locations */
328 res = i2c_smbus_read_byte_data(client, reg) & 0xff;
329 } else {
330 /* EEPROM, do nothing */
331 res = 0;
332 }
333 return res;
334}
335
Ben Dooksc49efce2005-10-26 21:07:25 +0200336static int adm1026_write_value(struct i2c_client *client, u8 reg, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337{
338 int res;
339
340 if (reg < 0x80) {
341 /* "RAM" locations */
342 res = i2c_smbus_write_byte_data(client, reg, value);
343 } else {
344 /* EEPROM, do nothing */
345 res = 0;
346 }
347 return res;
348}
349
Ben Dooksc49efce2005-10-26 21:07:25 +0200350static void adm1026_init_client(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351{
352 int value, i;
353 struct adm1026_data *data = i2c_get_clientdata(client);
354
Jean Delvarecb01a232007-11-29 23:46:42 +0100355 dev_dbg(&client->dev, "Initializing device\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 /* Read chip config */
357 data->config1 = adm1026_read_value(client, ADM1026_REG_CONFIG1);
358 data->config2 = adm1026_read_value(client, ADM1026_REG_CONFIG2);
359 data->config3 = adm1026_read_value(client, ADM1026_REG_CONFIG3);
360
361 /* Inform user of chip config */
362 dev_dbg(&client->dev, "ADM1026_REG_CONFIG1 is: 0x%02x\n",
363 data->config1);
364 if ((data->config1 & CFG1_MONITOR) == 0) {
365 dev_dbg(&client->dev, "Monitoring not currently "
366 "enabled.\n");
367 }
368 if (data->config1 & CFG1_INT_ENABLE) {
369 dev_dbg(&client->dev, "SMBALERT interrupts are "
370 "enabled.\n");
371 }
372 if (data->config1 & CFG1_AIN8_9) {
373 dev_dbg(&client->dev, "in8 and in9 enabled. "
374 "temp3 disabled.\n");
375 } else {
376 dev_dbg(&client->dev, "temp3 enabled. in8 and "
377 "in9 disabled.\n");
378 }
379 if (data->config1 & CFG1_THERM_HOT) {
380 dev_dbg(&client->dev, "Automatic THERM, PWM, "
381 "and temp limits enabled.\n");
382 }
383
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 if (data->config3 & CFG3_GPIO16_ENABLE) {
Jean Delvare368609c2005-07-29 12:15:07 -0700385 dev_dbg(&client->dev, "GPIO16 enabled. THERM "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 "pin disabled.\n");
387 } else {
388 dev_dbg(&client->dev, "THERM pin enabled. "
389 "GPIO16 disabled.\n");
390 }
391 if (data->config3 & CFG3_VREF_250) {
392 dev_dbg(&client->dev, "Vref is 2.50 Volts.\n");
393 } else {
394 dev_dbg(&client->dev, "Vref is 1.82 Volts.\n");
395 }
396 /* Read and pick apart the existing GPIO configuration */
397 value = 0;
398 for (i = 0;i <= 15;++i) {
399 if ((i & 0x03) == 0) {
400 value = adm1026_read_value(client,
401 ADM1026_REG_GPIO_CFG_0_3 + i/4);
402 }
403 data->gpio_config[i] = value & 0x03;
404 value >>= 2;
405 }
406 data->gpio_config[16] = (data->config3 >> 6) & 0x03;
407
408 /* ... and then print it */
409 adm1026_print_gpio(client);
410
411 /* If the user asks us to reprogram the GPIO config, then
412 * do it now.
413 */
414 if (gpio_input[0] != -1 || gpio_output[0] != -1
415 || gpio_inverted[0] != -1 || gpio_normal[0] != -1
416 || gpio_fan[0] != -1) {
417 adm1026_fixup_gpio(client);
418 }
419
420 /* WE INTENTIONALLY make no changes to the limits,
421 * offsets, pwms, fans and zones. If they were
422 * configured, we don't want to mess with them.
423 * If they weren't, the default is 100% PWM, no
424 * control and will suffice until 'sensors -s'
Jean Delvarecb01a232007-11-29 23:46:42 +0100425 * can be run by the user. We DO set the default
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 * value for pwm1.auto_pwm_min to its maximum
427 * so that enabling automatic pwm fan control
Jean Delvarecb01a232007-11-29 23:46:42 +0100428 * without first setting a value for pwm1.auto_pwm_min
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 * will not result in potentially dangerous fan speed decrease.
430 */
431 data->pwm1.auto_pwm_min=255;
432 /* Start monitoring */
433 value = adm1026_read_value(client, ADM1026_REG_CONFIG1);
434 /* Set MONITOR, clear interrupt acknowledge and s/w reset */
435 value = (value | CFG1_MONITOR) & (~CFG1_INT_CLEAR & ~CFG1_RESET);
436 dev_dbg(&client->dev, "Setting CONFIG to: 0x%02x\n", value);
437 data->config1 = value;
438 adm1026_write_value(client, ADM1026_REG_CONFIG1, value);
439
440 /* initialize fan_div[] to hardware defaults */
441 value = adm1026_read_value(client, ADM1026_REG_FAN_DIV_0_3) |
442 (adm1026_read_value(client, ADM1026_REG_FAN_DIV_4_7) << 8);
443 for (i = 0;i <= 7;++i) {
444 data->fan_div[i] = DIV_FROM_REG(value & 0x03);
445 value >>= 2;
446 }
447}
448
Ben Dooksc49efce2005-10-26 21:07:25 +0200449static void adm1026_print_gpio(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450{
451 struct adm1026_data *data = i2c_get_clientdata(client);
Jean Delvarecb01a232007-11-29 23:46:42 +0100452 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453
Jean Delvare885fe4b2008-10-17 17:51:20 +0200454 dev_dbg(&client->dev, "GPIO config is:\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 for (i = 0;i <= 7;++i) {
456 if (data->config2 & (1 << i)) {
457 dev_dbg(&client->dev, "\t%sGP%s%d\n",
458 data->gpio_config[i] & 0x02 ? "" : "!",
459 data->gpio_config[i] & 0x01 ? "OUT" : "IN",
460 i);
461 } else {
462 dev_dbg(&client->dev, "\tFAN%d\n", i);
463 }
464 }
465 for (i = 8;i <= 15;++i) {
466 dev_dbg(&client->dev, "\t%sGP%s%d\n",
467 data->gpio_config[i] & 0x02 ? "" : "!",
468 data->gpio_config[i] & 0x01 ? "OUT" : "IN",
469 i);
470 }
471 if (data->config3 & CFG3_GPIO16_ENABLE) {
472 dev_dbg(&client->dev, "\t%sGP%s16\n",
473 data->gpio_config[16] & 0x02 ? "" : "!",
474 data->gpio_config[16] & 0x01 ? "OUT" : "IN");
475 } else {
Jean Delvarecb01a232007-11-29 23:46:42 +0100476 /* GPIO16 is THERM */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 dev_dbg(&client->dev, "\tTHERM\n");
478 }
479}
480
Ben Dooksc49efce2005-10-26 21:07:25 +0200481static void adm1026_fixup_gpio(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482{
483 struct adm1026_data *data = i2c_get_clientdata(client);
Jean Delvarecb01a232007-11-29 23:46:42 +0100484 int i;
485 int value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486
487 /* Make the changes requested. */
488 /* We may need to unlock/stop monitoring or soft-reset the
489 * chip before we can make changes. This hasn't been
490 * tested much. FIXME
491 */
492
493 /* Make outputs */
494 for (i = 0;i <= 16;++i) {
495 if (gpio_output[i] >= 0 && gpio_output[i] <= 16) {
496 data->gpio_config[gpio_output[i]] |= 0x01;
497 }
498 /* if GPIO0-7 is output, it isn't a FAN tach */
499 if (gpio_output[i] >= 0 && gpio_output[i] <= 7) {
500 data->config2 |= 1 << gpio_output[i];
501 }
502 }
503
504 /* Input overrides output */
505 for (i = 0;i <= 16;++i) {
506 if (gpio_input[i] >= 0 && gpio_input[i] <= 16) {
507 data->gpio_config[gpio_input[i]] &= ~ 0x01;
508 }
509 /* if GPIO0-7 is input, it isn't a FAN tach */
510 if (gpio_input[i] >= 0 && gpio_input[i] <= 7) {
511 data->config2 |= 1 << gpio_input[i];
512 }
513 }
514
Jean Delvarecb01a232007-11-29 23:46:42 +0100515 /* Inverted */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 for (i = 0;i <= 16;++i) {
517 if (gpio_inverted[i] >= 0 && gpio_inverted[i] <= 16) {
518 data->gpio_config[gpio_inverted[i]] &= ~ 0x02;
519 }
520 }
521
Jean Delvarecb01a232007-11-29 23:46:42 +0100522 /* Normal overrides inverted */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 for (i = 0;i <= 16;++i) {
524 if (gpio_normal[i] >= 0 && gpio_normal[i] <= 16) {
525 data->gpio_config[gpio_normal[i]] |= 0x02;
526 }
527 }
528
529 /* Fan overrides input and output */
530 for (i = 0;i <= 7;++i) {
531 if (gpio_fan[i] >= 0 && gpio_fan[i] <= 7) {
532 data->config2 &= ~(1 << gpio_fan[i]);
533 }
534 }
535
536 /* Write new configs to registers */
537 adm1026_write_value(client, ADM1026_REG_CONFIG2, data->config2);
538 data->config3 = (data->config3 & 0x3f)
539 | ((data->gpio_config[16] & 0x03) << 6);
540 adm1026_write_value(client, ADM1026_REG_CONFIG3, data->config3);
541 for (i = 15, value = 0;i >= 0;--i) {
542 value <<= 2;
543 value |= data->gpio_config[i] & 0x03;
544 if ((i & 0x03) == 0) {
545 adm1026_write_value(client,
546 ADM1026_REG_GPIO_CFG_0_3 + i/4,
547 value);
548 value = 0;
549 }
550 }
551
552 /* Print the new config */
553 adm1026_print_gpio(client);
554}
555
556
557static struct adm1026_data *adm1026_update_device(struct device *dev)
558{
559 struct i2c_client *client = to_i2c_client(dev);
560 struct adm1026_data *data = i2c_get_clientdata(client);
561 int i;
562 long value, alarms, gpio;
563
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100564 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 if (!data->valid
566 || time_after(jiffies, data->last_reading + ADM1026_DATA_INTERVAL)) {
567 /* Things that change quickly */
Jean Delvarecb01a232007-11-29 23:46:42 +0100568 dev_dbg(&client->dev, "Reading sensor values\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 for (i = 0;i <= 16;++i) {
570 data->in[i] =
571 adm1026_read_value(client, ADM1026_REG_IN[i]);
572 }
573
574 for (i = 0;i <= 7;++i) {
575 data->fan[i] =
576 adm1026_read_value(client, ADM1026_REG_FAN(i));
577 }
578
579 for (i = 0;i <= 2;++i) {
580 /* NOTE: temp[] is s8 and we assume 2's complement
Jean Delvarecb01a232007-11-29 23:46:42 +0100581 * "conversion" in the assignment */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 data->temp[i] =
583 adm1026_read_value(client, ADM1026_REG_TEMP[i]);
584 }
585
Jean Delvarecb01a232007-11-29 23:46:42 +0100586 data->pwm1.pwm = adm1026_read_value(client,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 ADM1026_REG_PWM);
Jean Delvarecb01a232007-11-29 23:46:42 +0100588 data->analog_out = adm1026_read_value(client,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 ADM1026_REG_DAC);
590 /* GPIO16 is MSbit of alarms, move it to gpio */
591 alarms = adm1026_read_value(client, ADM1026_REG_STATUS4);
Jean Delvarecb01a232007-11-29 23:46:42 +0100592 gpio = alarms & 0x80 ? 0x0100 : 0; /* GPIO16 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 alarms &= 0x7f;
594 alarms <<= 8;
595 alarms |= adm1026_read_value(client, ADM1026_REG_STATUS3);
596 alarms <<= 8;
597 alarms |= adm1026_read_value(client, ADM1026_REG_STATUS2);
598 alarms <<= 8;
599 alarms |= adm1026_read_value(client, ADM1026_REG_STATUS1);
600 data->alarms = alarms;
601
602 /* Read the GPIO values */
Jean Delvarecb01a232007-11-29 23:46:42 +0100603 gpio |= adm1026_read_value(client,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 ADM1026_REG_GPIO_STATUS_8_15);
605 gpio <<= 8;
Jean Delvarecb01a232007-11-29 23:46:42 +0100606 gpio |= adm1026_read_value(client,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 ADM1026_REG_GPIO_STATUS_0_7);
608 data->gpio = gpio;
609
610 data->last_reading = jiffies;
Jean Delvarecb01a232007-11-29 23:46:42 +0100611 }; /* last_reading */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612
613 if (!data->valid ||
614 time_after(jiffies, data->last_config + ADM1026_CONFIG_INTERVAL)) {
615 /* Things that don't change often */
616 dev_dbg(&client->dev, "Reading config values\n");
617 for (i = 0;i <= 16;++i) {
Jean Delvarecb01a232007-11-29 23:46:42 +0100618 data->in_min[i] = adm1026_read_value(client,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 ADM1026_REG_IN_MIN[i]);
Jean Delvarecb01a232007-11-29 23:46:42 +0100620 data->in_max[i] = adm1026_read_value(client,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 ADM1026_REG_IN_MAX[i]);
622 }
623
624 value = adm1026_read_value(client, ADM1026_REG_FAN_DIV_0_3)
625 | (adm1026_read_value(client, ADM1026_REG_FAN_DIV_4_7)
626 << 8);
627 for (i = 0;i <= 7;++i) {
Jean Delvarecb01a232007-11-29 23:46:42 +0100628 data->fan_min[i] = adm1026_read_value(client,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 ADM1026_REG_FAN_MIN(i));
630 data->fan_div[i] = DIV_FROM_REG(value & 0x03);
631 value >>= 2;
632 }
633
634 for (i = 0; i <= 2; ++i) {
Jean Delvarecb01a232007-11-29 23:46:42 +0100635 /* NOTE: temp_xxx[] are s8 and we assume 2's
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 * complement "conversion" in the assignment
637 */
Jean Delvarecb01a232007-11-29 23:46:42 +0100638 data->temp_min[i] = adm1026_read_value(client,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 ADM1026_REG_TEMP_MIN[i]);
Jean Delvarecb01a232007-11-29 23:46:42 +0100640 data->temp_max[i] = adm1026_read_value(client,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 ADM1026_REG_TEMP_MAX[i]);
Jean Delvarecb01a232007-11-29 23:46:42 +0100642 data->temp_tmin[i] = adm1026_read_value(client,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 ADM1026_REG_TEMP_TMIN[i]);
Jean Delvarecb01a232007-11-29 23:46:42 +0100644 data->temp_crit[i] = adm1026_read_value(client,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 ADM1026_REG_TEMP_THERM[i]);
Jean Delvarecb01a232007-11-29 23:46:42 +0100646 data->temp_offset[i] = adm1026_read_value(client,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 ADM1026_REG_TEMP_OFFSET[i]);
648 }
649
650 /* Read the STATUS/alarm masks */
Jean Delvarecb01a232007-11-29 23:46:42 +0100651 alarms = adm1026_read_value(client, ADM1026_REG_MASK4);
652 gpio = alarms & 0x80 ? 0x0100 : 0; /* GPIO16 */
653 alarms = (alarms & 0x7f) << 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 alarms |= adm1026_read_value(client, ADM1026_REG_MASK3);
655 alarms <<= 8;
656 alarms |= adm1026_read_value(client, ADM1026_REG_MASK2);
657 alarms <<= 8;
658 alarms |= adm1026_read_value(client, ADM1026_REG_MASK1);
659 data->alarm_mask = alarms;
660
661 /* Read the GPIO values */
Jean Delvarecb01a232007-11-29 23:46:42 +0100662 gpio |= adm1026_read_value(client,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 ADM1026_REG_GPIO_MASK_8_15);
664 gpio <<= 8;
665 gpio |= adm1026_read_value(client, ADM1026_REG_GPIO_MASK_0_7);
666 data->gpio_mask = gpio;
667
668 /* Read various values from CONFIG1 */
Jean Delvarecb01a232007-11-29 23:46:42 +0100669 data->config1 = adm1026_read_value(client,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 ADM1026_REG_CONFIG1);
671 if (data->config1 & CFG1_PWM_AFC) {
672 data->pwm1.enable = 2;
Jean Delvarecb01a232007-11-29 23:46:42 +0100673 data->pwm1.auto_pwm_min =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 PWM_MIN_FROM_REG(data->pwm1.pwm);
675 }
676 /* Read the GPIO config */
Jean Delvarecb01a232007-11-29 23:46:42 +0100677 data->config2 = adm1026_read_value(client,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 ADM1026_REG_CONFIG2);
Jean Delvarecb01a232007-11-29 23:46:42 +0100679 data->config3 = adm1026_read_value(client,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 ADM1026_REG_CONFIG3);
681 data->gpio_config[16] = (data->config3 >> 6) & 0x03;
682
683 value = 0;
684 for (i = 0;i <= 15;++i) {
685 if ((i & 0x03) == 0) {
686 value = adm1026_read_value(client,
687 ADM1026_REG_GPIO_CFG_0_3 + i/4);
688 }
689 data->gpio_config[i] = value & 0x03;
690 value >>= 2;
691 }
692
693 data->last_config = jiffies;
Jean Delvarecb01a232007-11-29 23:46:42 +0100694 }; /* last_config */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 data->valid = 1;
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100697 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 return data;
699}
700
Yani Ioannou050480f2005-06-05 10:51:46 +0200701static ssize_t show_in(struct device *dev, struct device_attribute *attr,
702 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703{
Yani Ioannou050480f2005-06-05 10:51:46 +0200704 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
705 int nr = sensor_attr->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 struct adm1026_data *data = adm1026_update_device(dev);
Jean Delvarecb01a232007-11-29 23:46:42 +0100707 return sprintf(buf, "%d\n", INS_FROM_REG(nr, data->in[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708}
Yani Ioannou050480f2005-06-05 10:51:46 +0200709static ssize_t show_in_min(struct device *dev, struct device_attribute *attr,
710 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711{
Yani Ioannou050480f2005-06-05 10:51:46 +0200712 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
713 int nr = sensor_attr->index;
Jean Delvarecb01a232007-11-29 23:46:42 +0100714 struct adm1026_data *data = adm1026_update_device(dev);
715 return sprintf(buf, "%d\n", INS_FROM_REG(nr, data->in_min[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716}
Yani Ioannou050480f2005-06-05 10:51:46 +0200717static ssize_t set_in_min(struct device *dev, struct device_attribute *attr,
718 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719{
Yani Ioannou050480f2005-06-05 10:51:46 +0200720 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
721 int nr = sensor_attr->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 struct i2c_client *client = to_i2c_client(dev);
723 struct adm1026_data *data = i2c_get_clientdata(client);
724 int val = simple_strtol(buf, NULL, 10);
725
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100726 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 data->in_min[nr] = INS_TO_REG(nr, val);
728 adm1026_write_value(client, ADM1026_REG_IN_MIN[nr], data->in_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100729 mutex_unlock(&data->update_lock);
Jean Delvarecb01a232007-11-29 23:46:42 +0100730 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731}
Yani Ioannou050480f2005-06-05 10:51:46 +0200732static ssize_t show_in_max(struct device *dev, struct device_attribute *attr,
733 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734{
Yani Ioannou050480f2005-06-05 10:51:46 +0200735 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
736 int nr = sensor_attr->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 struct adm1026_data *data = adm1026_update_device(dev);
Jean Delvarecb01a232007-11-29 23:46:42 +0100738 return sprintf(buf, "%d\n", INS_FROM_REG(nr, data->in_max[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739}
Yani Ioannou050480f2005-06-05 10:51:46 +0200740static ssize_t set_in_max(struct device *dev, struct device_attribute *attr,
741 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742{
Yani Ioannou050480f2005-06-05 10:51:46 +0200743 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
744 int nr = sensor_attr->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 struct i2c_client *client = to_i2c_client(dev);
746 struct adm1026_data *data = i2c_get_clientdata(client);
747 int val = simple_strtol(buf, NULL, 10);
748
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100749 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 data->in_max[nr] = INS_TO_REG(nr, val);
751 adm1026_write_value(client, ADM1026_REG_IN_MAX[nr], data->in_max[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100752 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 return count;
754}
755
Yani Ioannou050480f2005-06-05 10:51:46 +0200756#define in_reg(offset) \
757static SENSOR_DEVICE_ATTR(in##offset##_input, S_IRUGO, show_in, \
758 NULL, offset); \
759static SENSOR_DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \
760 show_in_min, set_in_min, offset); \
761static SENSOR_DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \
762 show_in_max, set_in_max, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763
764
765in_reg(0);
766in_reg(1);
767in_reg(2);
768in_reg(3);
769in_reg(4);
770in_reg(5);
771in_reg(6);
772in_reg(7);
773in_reg(8);
774in_reg(9);
775in_reg(10);
776in_reg(11);
777in_reg(12);
778in_reg(13);
779in_reg(14);
780in_reg(15);
781
Yani Ioannou74880c02005-05-17 06:41:12 -0400782static ssize_t show_in16(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783{
784 struct adm1026_data *data = adm1026_update_device(dev);
Jean Delvarecb01a232007-11-29 23:46:42 +0100785 return sprintf(buf, "%d\n", INS_FROM_REG(16, data->in[16]) -
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 NEG12_OFFSET);
787}
Yani Ioannou74880c02005-05-17 06:41:12 -0400788static ssize_t show_in16_min(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789{
Jean Delvarecb01a232007-11-29 23:46:42 +0100790 struct adm1026_data *data = adm1026_update_device(dev);
791 return sprintf(buf, "%d\n", INS_FROM_REG(16, data->in_min[16])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 - NEG12_OFFSET);
793}
Yani Ioannou74880c02005-05-17 06:41:12 -0400794static ssize_t set_in16_min(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795{
796 struct i2c_client *client = to_i2c_client(dev);
797 struct adm1026_data *data = i2c_get_clientdata(client);
798 int val = simple_strtol(buf, NULL, 10);
799
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100800 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 data->in_min[16] = INS_TO_REG(16, val + NEG12_OFFSET);
802 adm1026_write_value(client, ADM1026_REG_IN_MIN[16], data->in_min[16]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100803 mutex_unlock(&data->update_lock);
Jean Delvarecb01a232007-11-29 23:46:42 +0100804 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805}
Yani Ioannou74880c02005-05-17 06:41:12 -0400806static ssize_t show_in16_max(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807{
808 struct adm1026_data *data = adm1026_update_device(dev);
Jean Delvarecb01a232007-11-29 23:46:42 +0100809 return sprintf(buf, "%d\n", INS_FROM_REG(16, data->in_max[16])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 - NEG12_OFFSET);
811}
Yani Ioannou74880c02005-05-17 06:41:12 -0400812static ssize_t set_in16_max(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813{
814 struct i2c_client *client = to_i2c_client(dev);
815 struct adm1026_data *data = i2c_get_clientdata(client);
816 int val = simple_strtol(buf, NULL, 10);
817
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100818 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 data->in_max[16] = INS_TO_REG(16, val+NEG12_OFFSET);
820 adm1026_write_value(client, ADM1026_REG_IN_MAX[16], data->in_max[16]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100821 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 return count;
823}
824
Yani Ioannou050480f2005-06-05 10:51:46 +0200825static SENSOR_DEVICE_ATTR(in16_input, S_IRUGO, show_in16, NULL, 16);
826static SENSOR_DEVICE_ATTR(in16_min, S_IRUGO | S_IWUSR, show_in16_min, set_in16_min, 16);
827static SENSOR_DEVICE_ATTR(in16_max, S_IRUGO | S_IWUSR, show_in16_max, set_in16_max, 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828
829
830
831
832/* Now add fan read/write functions */
833
Yani Ioannou050480f2005-06-05 10:51:46 +0200834static ssize_t show_fan(struct device *dev, struct device_attribute *attr,
835 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836{
Yani Ioannou050480f2005-06-05 10:51:46 +0200837 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
838 int nr = sensor_attr->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 struct adm1026_data *data = adm1026_update_device(dev);
Jean Delvarecb01a232007-11-29 23:46:42 +0100840 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[nr],
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 data->fan_div[nr]));
842}
Yani Ioannou050480f2005-06-05 10:51:46 +0200843static ssize_t show_fan_min(struct device *dev, struct device_attribute *attr,
844 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845{
Yani Ioannou050480f2005-06-05 10:51:46 +0200846 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
847 int nr = sensor_attr->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 struct adm1026_data *data = adm1026_update_device(dev);
Jean Delvarecb01a232007-11-29 23:46:42 +0100849 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_min[nr],
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 data->fan_div[nr]));
851}
Yani Ioannou050480f2005-06-05 10:51:46 +0200852static ssize_t set_fan_min(struct device *dev, struct device_attribute *attr,
853 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854{
Yani Ioannou050480f2005-06-05 10:51:46 +0200855 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
856 int nr = sensor_attr->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 struct i2c_client *client = to_i2c_client(dev);
858 struct adm1026_data *data = i2c_get_clientdata(client);
859 int val = simple_strtol(buf, NULL, 10);
860
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100861 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 data->fan_min[nr] = FAN_TO_REG(val, data->fan_div[nr]);
863 adm1026_write_value(client, ADM1026_REG_FAN_MIN(nr),
864 data->fan_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100865 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 return count;
867}
868
Jean Delvarecb01a232007-11-29 23:46:42 +0100869#define fan_offset(offset) \
870static SENSOR_DEVICE_ATTR(fan##offset##_input, S_IRUGO, show_fan, NULL, \
871 offset - 1); \
872static SENSOR_DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
Yani Ioannou050480f2005-06-05 10:51:46 +0200873 show_fan_min, set_fan_min, offset - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874
875fan_offset(1);
876fan_offset(2);
877fan_offset(3);
878fan_offset(4);
879fan_offset(5);
880fan_offset(6);
881fan_offset(7);
882fan_offset(8);
883
884/* Adjust fan_min to account for new fan divisor */
885static void fixup_fan_min(struct device *dev, int fan, int old_div)
886{
887 struct i2c_client *client = to_i2c_client(dev);
888 struct adm1026_data *data = i2c_get_clientdata(client);
Jean Delvarecb01a232007-11-29 23:46:42 +0100889 int new_min;
890 int new_div = data->fan_div[fan];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891
892 /* 0 and 0xff are special. Don't adjust them */
893 if (data->fan_min[fan] == 0 || data->fan_min[fan] == 0xff) {
894 return;
895 }
896
897 new_min = data->fan_min[fan] * old_div / new_div;
898 new_min = SENSORS_LIMIT(new_min, 1, 254);
899 data->fan_min[fan] = new_min;
900 adm1026_write_value(client, ADM1026_REG_FAN_MIN(fan), new_min);
901}
902
903/* Now add fan_div read/write functions */
Yani Ioannou050480f2005-06-05 10:51:46 +0200904static ssize_t show_fan_div(struct device *dev, struct device_attribute *attr,
905 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906{
Yani Ioannou050480f2005-06-05 10:51:46 +0200907 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
908 int nr = sensor_attr->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 struct adm1026_data *data = adm1026_update_device(dev);
Jean Delvarecb01a232007-11-29 23:46:42 +0100910 return sprintf(buf, "%d\n", data->fan_div[nr]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911}
Yani Ioannou050480f2005-06-05 10:51:46 +0200912static ssize_t set_fan_div(struct device *dev, struct device_attribute *attr,
913 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914{
Yani Ioannou050480f2005-06-05 10:51:46 +0200915 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
916 int nr = sensor_attr->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 struct i2c_client *client = to_i2c_client(dev);
918 struct adm1026_data *data = i2c_get_clientdata(client);
Gabriele Gorla52bc9802010-12-08 16:27:22 +0100919 int val, orig_div, new_div;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920
921 val = simple_strtol(buf, NULL, 10);
Jean Delvarecb01a232007-11-29 23:46:42 +0100922 new_div = DIV_TO_REG(val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 if (new_div == 0) {
924 return -EINVAL;
925 }
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100926 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 orig_div = data->fan_div[nr];
928 data->fan_div[nr] = DIV_FROM_REG(new_div);
929
930 if (nr < 4) { /* 0 <= nr < 4 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 adm1026_write_value(client, ADM1026_REG_FAN_DIV_0_3,
Gabriele Gorla52bc9802010-12-08 16:27:22 +0100932 (DIV_TO_REG(data->fan_div[0]) << 0) |
933 (DIV_TO_REG(data->fan_div[1]) << 2) |
934 (DIV_TO_REG(data->fan_div[2]) << 4) |
935 (DIV_TO_REG(data->fan_div[3]) << 6));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 } else { /* 3 < nr < 8 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 adm1026_write_value(client, ADM1026_REG_FAN_DIV_4_7,
Gabriele Gorla52bc9802010-12-08 16:27:22 +0100938 (DIV_TO_REG(data->fan_div[4]) << 0) |
939 (DIV_TO_REG(data->fan_div[5]) << 2) |
940 (DIV_TO_REG(data->fan_div[6]) << 4) |
941 (DIV_TO_REG(data->fan_div[7]) << 6));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 }
943
944 if (data->fan_div[nr] != orig_div) {
Jean Delvarecb01a232007-11-29 23:46:42 +0100945 fixup_fan_min(dev, nr, orig_div);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 }
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100947 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 return count;
949}
950
Jean Delvarecb01a232007-11-29 23:46:42 +0100951#define fan_offset_div(offset) \
952static SENSOR_DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR, \
Yani Ioannou050480f2005-06-05 10:51:46 +0200953 show_fan_div, set_fan_div, offset - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954
955fan_offset_div(1);
956fan_offset_div(2);
957fan_offset_div(3);
958fan_offset_div(4);
959fan_offset_div(5);
960fan_offset_div(6);
961fan_offset_div(7);
962fan_offset_div(8);
963
964/* Temps */
Yani Ioannou050480f2005-06-05 10:51:46 +0200965static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
966 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967{
Yani Ioannou050480f2005-06-05 10:51:46 +0200968 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
969 int nr = sensor_attr->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 struct adm1026_data *data = adm1026_update_device(dev);
Jean Delvarecb01a232007-11-29 23:46:42 +0100971 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972}
Yani Ioannou050480f2005-06-05 10:51:46 +0200973static ssize_t show_temp_min(struct device *dev, struct device_attribute *attr,
974 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975{
Yani Ioannou050480f2005-06-05 10:51:46 +0200976 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
977 int nr = sensor_attr->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 struct adm1026_data *data = adm1026_update_device(dev);
Jean Delvarecb01a232007-11-29 23:46:42 +0100979 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_min[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980}
Yani Ioannou050480f2005-06-05 10:51:46 +0200981static ssize_t set_temp_min(struct device *dev, struct device_attribute *attr,
982 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983{
Yani Ioannou050480f2005-06-05 10:51:46 +0200984 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
985 int nr = sensor_attr->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 struct i2c_client *client = to_i2c_client(dev);
987 struct adm1026_data *data = i2c_get_clientdata(client);
988 int val = simple_strtol(buf, NULL, 10);
989
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100990 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 data->temp_min[nr] = TEMP_TO_REG(val);
992 adm1026_write_value(client, ADM1026_REG_TEMP_MIN[nr],
993 data->temp_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100994 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 return count;
996}
Yani Ioannou050480f2005-06-05 10:51:46 +0200997static ssize_t show_temp_max(struct device *dev, struct device_attribute *attr,
998 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999{
Yani Ioannou050480f2005-06-05 10:51:46 +02001000 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1001 int nr = sensor_attr->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 struct adm1026_data *data = adm1026_update_device(dev);
Jean Delvarecb01a232007-11-29 23:46:42 +01001003 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_max[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004}
Yani Ioannou050480f2005-06-05 10:51:46 +02001005static ssize_t set_temp_max(struct device *dev, struct device_attribute *attr,
1006 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007{
Yani Ioannou050480f2005-06-05 10:51:46 +02001008 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1009 int nr = sensor_attr->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 struct i2c_client *client = to_i2c_client(dev);
1011 struct adm1026_data *data = i2c_get_clientdata(client);
1012 int val = simple_strtol(buf, NULL, 10);
1013
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001014 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 data->temp_max[nr] = TEMP_TO_REG(val);
1016 adm1026_write_value(client, ADM1026_REG_TEMP_MAX[nr],
1017 data->temp_max[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001018 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 return count;
1020}
Yani Ioannou050480f2005-06-05 10:51:46 +02001021
1022#define temp_reg(offset) \
Jean Delvarecb01a232007-11-29 23:46:42 +01001023static SENSOR_DEVICE_ATTR(temp##offset##_input, S_IRUGO, show_temp, \
Yani Ioannou050480f2005-06-05 10:51:46 +02001024 NULL, offset - 1); \
1025static SENSOR_DEVICE_ATTR(temp##offset##_min, S_IRUGO | S_IWUSR, \
1026 show_temp_min, set_temp_min, offset - 1); \
1027static SENSOR_DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR, \
1028 show_temp_max, set_temp_max, offset - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029
1030
1031temp_reg(1);
1032temp_reg(2);
1033temp_reg(3);
1034
Yani Ioannou050480f2005-06-05 10:51:46 +02001035static ssize_t show_temp_offset(struct device *dev,
1036 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037{
Yani Ioannou050480f2005-06-05 10:51:46 +02001038 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1039 int nr = sensor_attr->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 struct adm1026_data *data = adm1026_update_device(dev);
Jean Delvarecb01a232007-11-29 23:46:42 +01001041 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_offset[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042}
Yani Ioannou050480f2005-06-05 10:51:46 +02001043static ssize_t set_temp_offset(struct device *dev,
1044 struct device_attribute *attr, const char *buf,
1045 size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046{
Yani Ioannou050480f2005-06-05 10:51:46 +02001047 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1048 int nr = sensor_attr->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 struct i2c_client *client = to_i2c_client(dev);
1050 struct adm1026_data *data = i2c_get_clientdata(client);
1051 int val = simple_strtol(buf, NULL, 10);
1052
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001053 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 data->temp_offset[nr] = TEMP_TO_REG(val);
1055 adm1026_write_value(client, ADM1026_REG_TEMP_OFFSET[nr],
1056 data->temp_offset[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001057 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 return count;
1059}
1060
Yani Ioannou050480f2005-06-05 10:51:46 +02001061#define temp_offset_reg(offset) \
1062static SENSOR_DEVICE_ATTR(temp##offset##_offset, S_IRUGO | S_IWUSR, \
1063 show_temp_offset, set_temp_offset, offset - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064
1065temp_offset_reg(1);
1066temp_offset_reg(2);
1067temp_offset_reg(3);
1068
Yani Ioannou050480f2005-06-05 10:51:46 +02001069static ssize_t show_temp_auto_point1_temp_hyst(struct device *dev,
1070 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071{
Yani Ioannou050480f2005-06-05 10:51:46 +02001072 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1073 int nr = sensor_attr->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 struct adm1026_data *data = adm1026_update_device(dev);
Jean Delvarecb01a232007-11-29 23:46:42 +01001075 return sprintf(buf, "%d\n", TEMP_FROM_REG(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 ADM1026_FAN_ACTIVATION_TEMP_HYST + data->temp_tmin[nr]));
1077}
Yani Ioannou050480f2005-06-05 10:51:46 +02001078static ssize_t show_temp_auto_point2_temp(struct device *dev,
1079 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080{
Yani Ioannou050480f2005-06-05 10:51:46 +02001081 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1082 int nr = sensor_attr->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 struct adm1026_data *data = adm1026_update_device(dev);
Jean Delvarecb01a232007-11-29 23:46:42 +01001084 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_tmin[nr] +
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 ADM1026_FAN_CONTROL_TEMP_RANGE));
1086}
Yani Ioannou050480f2005-06-05 10:51:46 +02001087static ssize_t show_temp_auto_point1_temp(struct device *dev,
1088 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089{
Yani Ioannou050480f2005-06-05 10:51:46 +02001090 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1091 int nr = sensor_attr->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 struct adm1026_data *data = adm1026_update_device(dev);
Jean Delvarecb01a232007-11-29 23:46:42 +01001093 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_tmin[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094}
Yani Ioannou050480f2005-06-05 10:51:46 +02001095static ssize_t set_temp_auto_point1_temp(struct device *dev,
1096 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097{
Yani Ioannou050480f2005-06-05 10:51:46 +02001098 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1099 int nr = sensor_attr->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 struct i2c_client *client = to_i2c_client(dev);
1101 struct adm1026_data *data = i2c_get_clientdata(client);
1102 int val = simple_strtol(buf, NULL, 10);
1103
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001104 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 data->temp_tmin[nr] = TEMP_TO_REG(val);
1106 adm1026_write_value(client, ADM1026_REG_TEMP_TMIN[nr],
1107 data->temp_tmin[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001108 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 return count;
1110}
1111
Jean Delvarecb01a232007-11-29 23:46:42 +01001112#define temp_auto_point(offset) \
1113static SENSOR_DEVICE_ATTR(temp##offset##_auto_point1_temp, \
1114 S_IRUGO | S_IWUSR, show_temp_auto_point1_temp, \
1115 set_temp_auto_point1_temp, offset - 1); \
1116static SENSOR_DEVICE_ATTR(temp##offset##_auto_point1_temp_hyst, S_IRUGO,\
1117 show_temp_auto_point1_temp_hyst, NULL, offset - 1); \
1118static SENSOR_DEVICE_ATTR(temp##offset##_auto_point2_temp, S_IRUGO, \
Yani Ioannou050480f2005-06-05 10:51:46 +02001119 show_temp_auto_point2_temp, NULL, offset - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120
1121temp_auto_point(1);
1122temp_auto_point(2);
1123temp_auto_point(3);
1124
Yani Ioannou050480f2005-06-05 10:51:46 +02001125static ssize_t show_temp_crit_enable(struct device *dev,
1126 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127{
1128 struct adm1026_data *data = adm1026_update_device(dev);
Jean Delvarecb01a232007-11-29 23:46:42 +01001129 return sprintf(buf, "%d\n", (data->config1 & CFG1_THERM_HOT) >> 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130}
Yani Ioannou050480f2005-06-05 10:51:46 +02001131static ssize_t set_temp_crit_enable(struct device *dev,
1132 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133{
1134 struct i2c_client *client = to_i2c_client(dev);
1135 struct adm1026_data *data = i2c_get_clientdata(client);
1136 int val = simple_strtol(buf, NULL, 10);
1137
1138 if ((val == 1) || (val==0)) {
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001139 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 data->config1 = (data->config1 & ~CFG1_THERM_HOT) | (val << 4);
Jean Delvarecb01a232007-11-29 23:46:42 +01001141 adm1026_write_value(client, ADM1026_REG_CONFIG1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 data->config1);
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001143 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 }
1145 return count;
1146}
1147
Yani Ioannou050480f2005-06-05 10:51:46 +02001148#define temp_crit_enable(offset) \
1149static DEVICE_ATTR(temp##offset##_crit_enable, S_IRUGO | S_IWUSR, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 show_temp_crit_enable, set_temp_crit_enable);
1151
Yani Ioannou050480f2005-06-05 10:51:46 +02001152temp_crit_enable(1);
1153temp_crit_enable(2);
1154temp_crit_enable(3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155
Yani Ioannou050480f2005-06-05 10:51:46 +02001156static ssize_t show_temp_crit(struct device *dev,
1157 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158{
Yani Ioannou050480f2005-06-05 10:51:46 +02001159 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1160 int nr = sensor_attr->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 struct adm1026_data *data = adm1026_update_device(dev);
Jean Delvarecb01a232007-11-29 23:46:42 +01001162 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_crit[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163}
Yani Ioannou050480f2005-06-05 10:51:46 +02001164static ssize_t set_temp_crit(struct device *dev, struct device_attribute *attr,
1165 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166{
Yani Ioannou050480f2005-06-05 10:51:46 +02001167 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1168 int nr = sensor_attr->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 struct i2c_client *client = to_i2c_client(dev);
1170 struct adm1026_data *data = i2c_get_clientdata(client);
1171 int val = simple_strtol(buf, NULL, 10);
1172
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001173 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 data->temp_crit[nr] = TEMP_TO_REG(val);
1175 adm1026_write_value(client, ADM1026_REG_TEMP_THERM[nr],
1176 data->temp_crit[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001177 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 return count;
1179}
1180
Yani Ioannou050480f2005-06-05 10:51:46 +02001181#define temp_crit_reg(offset) \
1182static SENSOR_DEVICE_ATTR(temp##offset##_crit, S_IRUGO | S_IWUSR, \
1183 show_temp_crit, set_temp_crit, offset - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184
1185temp_crit_reg(1);
1186temp_crit_reg(2);
1187temp_crit_reg(3);
1188
Yani Ioannou74880c02005-05-17 06:41:12 -04001189static ssize_t show_analog_out_reg(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190{
1191 struct adm1026_data *data = adm1026_update_device(dev);
Jean Delvarecb01a232007-11-29 23:46:42 +01001192 return sprintf(buf, "%d\n", DAC_FROM_REG(data->analog_out));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193}
Yani Ioannou74880c02005-05-17 06:41:12 -04001194static ssize_t set_analog_out_reg(struct device *dev, struct device_attribute *attr, const char *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 size_t count)
1196{
1197 struct i2c_client *client = to_i2c_client(dev);
1198 struct adm1026_data *data = i2c_get_clientdata(client);
1199 int val = simple_strtol(buf, NULL, 10);
1200
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001201 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 data->analog_out = DAC_TO_REG(val);
1203 adm1026_write_value(client, ADM1026_REG_DAC, data->analog_out);
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001204 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 return count;
1206}
1207
Jean Delvarecb01a232007-11-29 23:46:42 +01001208static DEVICE_ATTR(analog_out, S_IRUGO | S_IWUSR, show_analog_out_reg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 set_analog_out_reg);
1210
Yani Ioannou74880c02005-05-17 06:41:12 -04001211static ssize_t show_vid_reg(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212{
1213 struct adm1026_data *data = adm1026_update_device(dev);
Jean Delvarea0cf3542008-10-17 17:51:20 +02001214 int vid = (data->gpio >> 11) & 0x1f;
1215
1216 dev_dbg(dev, "Setting VID from GPIO11-15.\n");
1217 return sprintf(buf, "%d\n", vid_from_reg(vid, data->vrm));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218}
Grant Coady937df8d2005-05-12 11:59:29 +10001219static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid_reg, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220
Yani Ioannou74880c02005-05-17 06:41:12 -04001221static ssize_t show_vrm_reg(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222{
Jean Delvare90d66192007-10-08 18:24:35 +02001223 struct adm1026_data *data = dev_get_drvdata(dev);
Jean Delvarecb01a232007-11-29 23:46:42 +01001224 return sprintf(buf, "%d\n", data->vrm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225}
Yani Ioannou74880c02005-05-17 06:41:12 -04001226static ssize_t store_vrm_reg(struct device *dev, struct device_attribute *attr, const char *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 size_t count)
1228{
Jean Delvaref67fdab2007-12-01 11:24:17 +01001229 struct adm1026_data *data = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230
1231 data->vrm = simple_strtol(buf, NULL, 10);
1232 return count;
1233}
1234
1235static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm_reg, store_vrm_reg);
1236
Yani Ioannou74880c02005-05-17 06:41:12 -04001237static ssize_t show_alarms_reg(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238{
1239 struct adm1026_data *data = adm1026_update_device(dev);
Jean Delvaref67fdab2007-12-01 11:24:17 +01001240 return sprintf(buf, "%ld\n", data->alarms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241}
1242
1243static DEVICE_ATTR(alarms, S_IRUGO, show_alarms_reg, NULL);
1244
Jean Delvarea9273cb2007-11-29 23:45:22 +01001245static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
1246 char *buf)
1247{
1248 struct adm1026_data *data = adm1026_update_device(dev);
1249 int bitnr = to_sensor_dev_attr(attr)->index;
1250 return sprintf(buf, "%ld\n", (data->alarms >> bitnr) & 1);
1251}
1252
1253static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 0);
1254static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, 1);
1255static SENSOR_DEVICE_ATTR(in9_alarm, S_IRUGO, show_alarm, NULL, 1);
1256static SENSOR_DEVICE_ATTR(in11_alarm, S_IRUGO, show_alarm, NULL, 2);
1257static SENSOR_DEVICE_ATTR(in12_alarm, S_IRUGO, show_alarm, NULL, 3);
1258static SENSOR_DEVICE_ATTR(in13_alarm, S_IRUGO, show_alarm, NULL, 4);
1259static SENSOR_DEVICE_ATTR(in14_alarm, S_IRUGO, show_alarm, NULL, 5);
1260static SENSOR_DEVICE_ATTR(in15_alarm, S_IRUGO, show_alarm, NULL, 6);
1261static SENSOR_DEVICE_ATTR(in16_alarm, S_IRUGO, show_alarm, NULL, 7);
1262static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 8);
1263static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 9);
1264static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 10);
1265static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 11);
1266static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 12);
1267static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 13);
1268static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 14);
1269static SENSOR_DEVICE_ATTR(in7_alarm, S_IRUGO, show_alarm, NULL, 15);
1270static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 16);
1271static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 17);
1272static SENSOR_DEVICE_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL, 18);
1273static SENSOR_DEVICE_ATTR(fan4_alarm, S_IRUGO, show_alarm, NULL, 19);
1274static SENSOR_DEVICE_ATTR(fan5_alarm, S_IRUGO, show_alarm, NULL, 20);
1275static SENSOR_DEVICE_ATTR(fan6_alarm, S_IRUGO, show_alarm, NULL, 21);
1276static SENSOR_DEVICE_ATTR(fan7_alarm, S_IRUGO, show_alarm, NULL, 22);
1277static SENSOR_DEVICE_ATTR(fan8_alarm, S_IRUGO, show_alarm, NULL, 23);
1278static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 24);
1279static SENSOR_DEVICE_ATTR(in10_alarm, S_IRUGO, show_alarm, NULL, 25);
1280static SENSOR_DEVICE_ATTR(in8_alarm, S_IRUGO, show_alarm, NULL, 26);
1281
Yani Ioannou74880c02005-05-17 06:41:12 -04001282static ssize_t show_alarm_mask(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283{
1284 struct adm1026_data *data = adm1026_update_device(dev);
Jean Delvarecb01a232007-11-29 23:46:42 +01001285 return sprintf(buf, "%ld\n", data->alarm_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286}
Yani Ioannou74880c02005-05-17 06:41:12 -04001287static ssize_t set_alarm_mask(struct device *dev, struct device_attribute *attr, const char *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 size_t count)
1289{
1290 struct i2c_client *client = to_i2c_client(dev);
1291 struct adm1026_data *data = i2c_get_clientdata(client);
1292 int val = simple_strtol(buf, NULL, 10);
1293 unsigned long mask;
1294
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001295 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 data->alarm_mask = val & 0x7fffffff;
1297 mask = data->alarm_mask
1298 | (data->gpio_mask & 0x10000 ? 0x80000000 : 0);
1299 adm1026_write_value(client, ADM1026_REG_MASK1,
1300 mask & 0xff);
1301 mask >>= 8;
1302 adm1026_write_value(client, ADM1026_REG_MASK2,
1303 mask & 0xff);
1304 mask >>= 8;
1305 adm1026_write_value(client, ADM1026_REG_MASK3,
1306 mask & 0xff);
1307 mask >>= 8;
1308 adm1026_write_value(client, ADM1026_REG_MASK4,
1309 mask & 0xff);
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001310 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 return count;
1312}
1313
1314static DEVICE_ATTR(alarm_mask, S_IRUGO | S_IWUSR, show_alarm_mask,
1315 set_alarm_mask);
1316
1317
Yani Ioannou74880c02005-05-17 06:41:12 -04001318static ssize_t show_gpio(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319{
1320 struct adm1026_data *data = adm1026_update_device(dev);
Jean Delvarecb01a232007-11-29 23:46:42 +01001321 return sprintf(buf, "%ld\n", data->gpio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322}
Yani Ioannou74880c02005-05-17 06:41:12 -04001323static ssize_t set_gpio(struct device *dev, struct device_attribute *attr, const char *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 size_t count)
1325{
1326 struct i2c_client *client = to_i2c_client(dev);
1327 struct adm1026_data *data = i2c_get_clientdata(client);
1328 int val = simple_strtol(buf, NULL, 10);
Jean Delvarecb01a232007-11-29 23:46:42 +01001329 long gpio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001331 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 data->gpio = val & 0x1ffff;
1333 gpio = data->gpio;
Jean Delvarecb01a232007-11-29 23:46:42 +01001334 adm1026_write_value(client, ADM1026_REG_GPIO_STATUS_0_7, gpio & 0xff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 gpio >>= 8;
Jean Delvarecb01a232007-11-29 23:46:42 +01001336 adm1026_write_value(client, ADM1026_REG_GPIO_STATUS_8_15, gpio & 0xff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 gpio = ((gpio >> 1) & 0x80) | (data->alarms >> 24 & 0x7f);
Jean Delvarecb01a232007-11-29 23:46:42 +01001338 adm1026_write_value(client, ADM1026_REG_STATUS4, gpio & 0xff);
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001339 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 return count;
1341}
1342
1343static DEVICE_ATTR(gpio, S_IRUGO | S_IWUSR, show_gpio, set_gpio);
1344
1345
Yani Ioannou74880c02005-05-17 06:41:12 -04001346static ssize_t show_gpio_mask(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347{
1348 struct adm1026_data *data = adm1026_update_device(dev);
Jean Delvarecb01a232007-11-29 23:46:42 +01001349 return sprintf(buf, "%ld\n", data->gpio_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350}
Yani Ioannou74880c02005-05-17 06:41:12 -04001351static ssize_t set_gpio_mask(struct device *dev, struct device_attribute *attr, const char *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 size_t count)
1353{
1354 struct i2c_client *client = to_i2c_client(dev);
1355 struct adm1026_data *data = i2c_get_clientdata(client);
1356 int val = simple_strtol(buf, NULL, 10);
Jean Delvarecb01a232007-11-29 23:46:42 +01001357 long mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001359 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 data->gpio_mask = val & 0x1ffff;
1361 mask = data->gpio_mask;
Jean Delvarecb01a232007-11-29 23:46:42 +01001362 adm1026_write_value(client, ADM1026_REG_GPIO_MASK_0_7, mask & 0xff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 mask >>= 8;
Jean Delvarecb01a232007-11-29 23:46:42 +01001364 adm1026_write_value(client, ADM1026_REG_GPIO_MASK_8_15, mask & 0xff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 mask = ((mask >> 1) & 0x80) | (data->alarm_mask >> 24 & 0x7f);
Jean Delvarecb01a232007-11-29 23:46:42 +01001366 adm1026_write_value(client, ADM1026_REG_MASK1, mask & 0xff);
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001367 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 return count;
1369}
1370
1371static DEVICE_ATTR(gpio_mask, S_IRUGO | S_IWUSR, show_gpio_mask, set_gpio_mask);
1372
Yani Ioannou74880c02005-05-17 06:41:12 -04001373static ssize_t show_pwm_reg(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374{
1375 struct adm1026_data *data = adm1026_update_device(dev);
Jean Delvarecb01a232007-11-29 23:46:42 +01001376 return sprintf(buf, "%d\n", PWM_FROM_REG(data->pwm1.pwm));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377}
Yani Ioannou74880c02005-05-17 06:41:12 -04001378static ssize_t set_pwm_reg(struct device *dev, struct device_attribute *attr, const char *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 size_t count)
1380{
1381 struct i2c_client *client = to_i2c_client(dev);
1382 struct adm1026_data *data = i2c_get_clientdata(client);
1383
1384 if (data->pwm1.enable == 1) {
1385 int val = simple_strtol(buf, NULL, 10);
1386
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001387 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 data->pwm1.pwm = PWM_TO_REG(val);
1389 adm1026_write_value(client, ADM1026_REG_PWM, data->pwm1.pwm);
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001390 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 }
1392 return count;
1393}
Yani Ioannou74880c02005-05-17 06:41:12 -04001394static ssize_t show_auto_pwm_min(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395{
1396 struct adm1026_data *data = adm1026_update_device(dev);
Jean Delvarecb01a232007-11-29 23:46:42 +01001397 return sprintf(buf, "%d\n", data->pwm1.auto_pwm_min);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398}
Yani Ioannou74880c02005-05-17 06:41:12 -04001399static ssize_t set_auto_pwm_min(struct device *dev, struct device_attribute *attr, const char *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 size_t count)
1401{
1402 struct i2c_client *client = to_i2c_client(dev);
1403 struct adm1026_data *data = i2c_get_clientdata(client);
1404 int val = simple_strtol(buf, NULL, 10);
1405
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001406 mutex_lock(&data->update_lock);
Jean Delvarecb01a232007-11-29 23:46:42 +01001407 data->pwm1.auto_pwm_min = SENSORS_LIMIT(val, 0, 255);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 if (data->pwm1.enable == 2) { /* apply immediately */
1409 data->pwm1.pwm = PWM_TO_REG((data->pwm1.pwm & 0x0f) |
Jean Delvarecb01a232007-11-29 23:46:42 +01001410 PWM_MIN_TO_REG(data->pwm1.auto_pwm_min));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 adm1026_write_value(client, ADM1026_REG_PWM, data->pwm1.pwm);
1412 }
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001413 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 return count;
1415}
Yani Ioannou74880c02005-05-17 06:41:12 -04001416static ssize_t show_auto_pwm_max(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417{
Jean Delvarecb01a232007-11-29 23:46:42 +01001418 return sprintf(buf, "%d\n", ADM1026_PWM_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419}
Yani Ioannou74880c02005-05-17 06:41:12 -04001420static ssize_t show_pwm_enable(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421{
1422 struct adm1026_data *data = adm1026_update_device(dev);
Jean Delvarecb01a232007-11-29 23:46:42 +01001423 return sprintf(buf, "%d\n", data->pwm1.enable);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424}
Yani Ioannou74880c02005-05-17 06:41:12 -04001425static ssize_t set_pwm_enable(struct device *dev, struct device_attribute *attr, const char *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 size_t count)
1427{
1428 struct i2c_client *client = to_i2c_client(dev);
1429 struct adm1026_data *data = i2c_get_clientdata(client);
1430 int val = simple_strtol(buf, NULL, 10);
Jean Delvarecb01a232007-11-29 23:46:42 +01001431 int old_enable;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432
1433 if ((val >= 0) && (val < 3)) {
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001434 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 old_enable = data->pwm1.enable;
1436 data->pwm1.enable = val;
1437 data->config1 = (data->config1 & ~CFG1_PWM_AFC)
1438 | ((val == 2) ? CFG1_PWM_AFC : 0);
1439 adm1026_write_value(client, ADM1026_REG_CONFIG1,
1440 data->config1);
Jean Delvarecb01a232007-11-29 23:46:42 +01001441 if (val == 2) { /* apply pwm1_auto_pwm_min to pwm1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 data->pwm1.pwm = PWM_TO_REG((data->pwm1.pwm & 0x0f) |
Jean Delvarecb01a232007-11-29 23:46:42 +01001443 PWM_MIN_TO_REG(data->pwm1.auto_pwm_min));
1444 adm1026_write_value(client, ADM1026_REG_PWM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 data->pwm1.pwm);
1446 } else if (!((old_enable == 1) && (val == 1))) {
1447 /* set pwm to safe value */
1448 data->pwm1.pwm = 255;
Jean Delvarecb01a232007-11-29 23:46:42 +01001449 adm1026_write_value(client, ADM1026_REG_PWM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 data->pwm1.pwm);
1451 }
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001452 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 }
1454 return count;
1455}
1456
1457/* enable PWM fan control */
Jean Delvarecb01a232007-11-29 23:46:42 +01001458static DEVICE_ATTR(pwm1, S_IRUGO | S_IWUSR, show_pwm_reg, set_pwm_reg);
1459static DEVICE_ATTR(pwm2, S_IRUGO | S_IWUSR, show_pwm_reg, set_pwm_reg);
1460static DEVICE_ATTR(pwm3, S_IRUGO | S_IWUSR, show_pwm_reg, set_pwm_reg);
1461static DEVICE_ATTR(pwm1_enable, S_IRUGO | S_IWUSR, show_pwm_enable,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 set_pwm_enable);
Jean Delvarecb01a232007-11-29 23:46:42 +01001463static DEVICE_ATTR(pwm2_enable, S_IRUGO | S_IWUSR, show_pwm_enable,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 set_pwm_enable);
Jean Delvarecb01a232007-11-29 23:46:42 +01001465static DEVICE_ATTR(pwm3_enable, S_IRUGO | S_IWUSR, show_pwm_enable,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 set_pwm_enable);
Jean Delvarecb01a232007-11-29 23:46:42 +01001467static DEVICE_ATTR(temp1_auto_point1_pwm, S_IRUGO | S_IWUSR,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 show_auto_pwm_min, set_auto_pwm_min);
Jean Delvarecb01a232007-11-29 23:46:42 +01001469static DEVICE_ATTR(temp2_auto_point1_pwm, S_IRUGO | S_IWUSR,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 show_auto_pwm_min, set_auto_pwm_min);
Jean Delvarecb01a232007-11-29 23:46:42 +01001471static DEVICE_ATTR(temp3_auto_point1_pwm, S_IRUGO | S_IWUSR,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 show_auto_pwm_min, set_auto_pwm_min);
1473
1474static DEVICE_ATTR(temp1_auto_point2_pwm, S_IRUGO, show_auto_pwm_max, NULL);
1475static DEVICE_ATTR(temp2_auto_point2_pwm, S_IRUGO, show_auto_pwm_max, NULL);
1476static DEVICE_ATTR(temp3_auto_point2_pwm, S_IRUGO, show_auto_pwm_max, NULL);
1477
Mark M. Hoffman681c6f72006-09-24 21:15:35 +02001478static struct attribute *adm1026_attributes[] = {
1479 &sensor_dev_attr_in0_input.dev_attr.attr,
1480 &sensor_dev_attr_in0_max.dev_attr.attr,
1481 &sensor_dev_attr_in0_min.dev_attr.attr,
Jean Delvarea9273cb2007-11-29 23:45:22 +01001482 &sensor_dev_attr_in0_alarm.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +02001483 &sensor_dev_attr_in1_input.dev_attr.attr,
1484 &sensor_dev_attr_in1_max.dev_attr.attr,
1485 &sensor_dev_attr_in1_min.dev_attr.attr,
Jean Delvarea9273cb2007-11-29 23:45:22 +01001486 &sensor_dev_attr_in1_alarm.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +02001487 &sensor_dev_attr_in2_input.dev_attr.attr,
1488 &sensor_dev_attr_in2_max.dev_attr.attr,
1489 &sensor_dev_attr_in2_min.dev_attr.attr,
Jean Delvarea9273cb2007-11-29 23:45:22 +01001490 &sensor_dev_attr_in2_alarm.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +02001491 &sensor_dev_attr_in3_input.dev_attr.attr,
1492 &sensor_dev_attr_in3_max.dev_attr.attr,
1493 &sensor_dev_attr_in3_min.dev_attr.attr,
Jean Delvarea9273cb2007-11-29 23:45:22 +01001494 &sensor_dev_attr_in3_alarm.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +02001495 &sensor_dev_attr_in4_input.dev_attr.attr,
1496 &sensor_dev_attr_in4_max.dev_attr.attr,
1497 &sensor_dev_attr_in4_min.dev_attr.attr,
Jean Delvarea9273cb2007-11-29 23:45:22 +01001498 &sensor_dev_attr_in4_alarm.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +02001499 &sensor_dev_attr_in5_input.dev_attr.attr,
1500 &sensor_dev_attr_in5_max.dev_attr.attr,
1501 &sensor_dev_attr_in5_min.dev_attr.attr,
Jean Delvarea9273cb2007-11-29 23:45:22 +01001502 &sensor_dev_attr_in5_alarm.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +02001503 &sensor_dev_attr_in6_input.dev_attr.attr,
1504 &sensor_dev_attr_in6_max.dev_attr.attr,
1505 &sensor_dev_attr_in6_min.dev_attr.attr,
Jean Delvarea9273cb2007-11-29 23:45:22 +01001506 &sensor_dev_attr_in6_alarm.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +02001507 &sensor_dev_attr_in7_input.dev_attr.attr,
1508 &sensor_dev_attr_in7_max.dev_attr.attr,
1509 &sensor_dev_attr_in7_min.dev_attr.attr,
Jean Delvarea9273cb2007-11-29 23:45:22 +01001510 &sensor_dev_attr_in7_alarm.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +02001511 &sensor_dev_attr_in10_input.dev_attr.attr,
1512 &sensor_dev_attr_in10_max.dev_attr.attr,
1513 &sensor_dev_attr_in10_min.dev_attr.attr,
Jean Delvarea9273cb2007-11-29 23:45:22 +01001514 &sensor_dev_attr_in10_alarm.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +02001515 &sensor_dev_attr_in11_input.dev_attr.attr,
1516 &sensor_dev_attr_in11_max.dev_attr.attr,
1517 &sensor_dev_attr_in11_min.dev_attr.attr,
Jean Delvarea9273cb2007-11-29 23:45:22 +01001518 &sensor_dev_attr_in11_alarm.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +02001519 &sensor_dev_attr_in12_input.dev_attr.attr,
1520 &sensor_dev_attr_in12_max.dev_attr.attr,
1521 &sensor_dev_attr_in12_min.dev_attr.attr,
Jean Delvarea9273cb2007-11-29 23:45:22 +01001522 &sensor_dev_attr_in12_alarm.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +02001523 &sensor_dev_attr_in13_input.dev_attr.attr,
1524 &sensor_dev_attr_in13_max.dev_attr.attr,
1525 &sensor_dev_attr_in13_min.dev_attr.attr,
Jean Delvarea9273cb2007-11-29 23:45:22 +01001526 &sensor_dev_attr_in13_alarm.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +02001527 &sensor_dev_attr_in14_input.dev_attr.attr,
1528 &sensor_dev_attr_in14_max.dev_attr.attr,
1529 &sensor_dev_attr_in14_min.dev_attr.attr,
Jean Delvarea9273cb2007-11-29 23:45:22 +01001530 &sensor_dev_attr_in14_alarm.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +02001531 &sensor_dev_attr_in15_input.dev_attr.attr,
1532 &sensor_dev_attr_in15_max.dev_attr.attr,
1533 &sensor_dev_attr_in15_min.dev_attr.attr,
Jean Delvarea9273cb2007-11-29 23:45:22 +01001534 &sensor_dev_attr_in15_alarm.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +02001535 &sensor_dev_attr_in16_input.dev_attr.attr,
1536 &sensor_dev_attr_in16_max.dev_attr.attr,
1537 &sensor_dev_attr_in16_min.dev_attr.attr,
Jean Delvarea9273cb2007-11-29 23:45:22 +01001538 &sensor_dev_attr_in16_alarm.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +02001539 &sensor_dev_attr_fan1_input.dev_attr.attr,
1540 &sensor_dev_attr_fan1_div.dev_attr.attr,
1541 &sensor_dev_attr_fan1_min.dev_attr.attr,
Jean Delvarea9273cb2007-11-29 23:45:22 +01001542 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +02001543 &sensor_dev_attr_fan2_input.dev_attr.attr,
1544 &sensor_dev_attr_fan2_div.dev_attr.attr,
1545 &sensor_dev_attr_fan2_min.dev_attr.attr,
Jean Delvarea9273cb2007-11-29 23:45:22 +01001546 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +02001547 &sensor_dev_attr_fan3_input.dev_attr.attr,
1548 &sensor_dev_attr_fan3_div.dev_attr.attr,
1549 &sensor_dev_attr_fan3_min.dev_attr.attr,
Jean Delvarea9273cb2007-11-29 23:45:22 +01001550 &sensor_dev_attr_fan3_alarm.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +02001551 &sensor_dev_attr_fan4_input.dev_attr.attr,
1552 &sensor_dev_attr_fan4_div.dev_attr.attr,
1553 &sensor_dev_attr_fan4_min.dev_attr.attr,
Jean Delvarea9273cb2007-11-29 23:45:22 +01001554 &sensor_dev_attr_fan4_alarm.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +02001555 &sensor_dev_attr_fan5_input.dev_attr.attr,
1556 &sensor_dev_attr_fan5_div.dev_attr.attr,
1557 &sensor_dev_attr_fan5_min.dev_attr.attr,
Jean Delvarea9273cb2007-11-29 23:45:22 +01001558 &sensor_dev_attr_fan5_alarm.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +02001559 &sensor_dev_attr_fan6_input.dev_attr.attr,
1560 &sensor_dev_attr_fan6_div.dev_attr.attr,
1561 &sensor_dev_attr_fan6_min.dev_attr.attr,
Jean Delvarea9273cb2007-11-29 23:45:22 +01001562 &sensor_dev_attr_fan6_alarm.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +02001563 &sensor_dev_attr_fan7_input.dev_attr.attr,
1564 &sensor_dev_attr_fan7_div.dev_attr.attr,
1565 &sensor_dev_attr_fan7_min.dev_attr.attr,
Jean Delvarea9273cb2007-11-29 23:45:22 +01001566 &sensor_dev_attr_fan7_alarm.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +02001567 &sensor_dev_attr_fan8_input.dev_attr.attr,
1568 &sensor_dev_attr_fan8_div.dev_attr.attr,
1569 &sensor_dev_attr_fan8_min.dev_attr.attr,
Jean Delvarea9273cb2007-11-29 23:45:22 +01001570 &sensor_dev_attr_fan8_alarm.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +02001571 &sensor_dev_attr_temp1_input.dev_attr.attr,
1572 &sensor_dev_attr_temp1_max.dev_attr.attr,
1573 &sensor_dev_attr_temp1_min.dev_attr.attr,
Jean Delvarea9273cb2007-11-29 23:45:22 +01001574 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +02001575 &sensor_dev_attr_temp2_input.dev_attr.attr,
1576 &sensor_dev_attr_temp2_max.dev_attr.attr,
1577 &sensor_dev_attr_temp2_min.dev_attr.attr,
Jean Delvarea9273cb2007-11-29 23:45:22 +01001578 &sensor_dev_attr_temp2_alarm.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +02001579 &sensor_dev_attr_temp1_offset.dev_attr.attr,
1580 &sensor_dev_attr_temp2_offset.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +02001581 &sensor_dev_attr_temp1_auto_point1_temp.dev_attr.attr,
1582 &sensor_dev_attr_temp2_auto_point1_temp.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +02001583 &sensor_dev_attr_temp1_auto_point1_temp_hyst.dev_attr.attr,
1584 &sensor_dev_attr_temp2_auto_point1_temp_hyst.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +02001585 &sensor_dev_attr_temp1_auto_point2_temp.dev_attr.attr,
1586 &sensor_dev_attr_temp2_auto_point2_temp.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +02001587 &sensor_dev_attr_temp1_crit.dev_attr.attr,
1588 &sensor_dev_attr_temp2_crit.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +02001589 &dev_attr_temp1_crit_enable.attr,
1590 &dev_attr_temp2_crit_enable.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +02001591 &dev_attr_cpu0_vid.attr,
1592 &dev_attr_vrm.attr,
1593 &dev_attr_alarms.attr,
1594 &dev_attr_alarm_mask.attr,
1595 &dev_attr_gpio.attr,
1596 &dev_attr_gpio_mask.attr,
1597 &dev_attr_pwm1.attr,
1598 &dev_attr_pwm2.attr,
1599 &dev_attr_pwm3.attr,
1600 &dev_attr_pwm1_enable.attr,
1601 &dev_attr_pwm2_enable.attr,
1602 &dev_attr_pwm3_enable.attr,
1603 &dev_attr_temp1_auto_point1_pwm.attr,
1604 &dev_attr_temp2_auto_point1_pwm.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +02001605 &dev_attr_temp1_auto_point2_pwm.attr,
1606 &dev_attr_temp2_auto_point2_pwm.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +02001607 &dev_attr_analog_out.attr,
1608 NULL
1609};
1610
1611static const struct attribute_group adm1026_group = {
1612 .attrs = adm1026_attributes,
1613};
1614
Jean Delvare5b34dbc2007-11-29 23:47:54 +01001615static struct attribute *adm1026_attributes_temp3[] = {
1616 &sensor_dev_attr_temp3_input.dev_attr.attr,
1617 &sensor_dev_attr_temp3_max.dev_attr.attr,
1618 &sensor_dev_attr_temp3_min.dev_attr.attr,
1619 &sensor_dev_attr_temp3_alarm.dev_attr.attr,
1620 &sensor_dev_attr_temp3_offset.dev_attr.attr,
1621 &sensor_dev_attr_temp3_auto_point1_temp.dev_attr.attr,
1622 &sensor_dev_attr_temp3_auto_point1_temp_hyst.dev_attr.attr,
1623 &sensor_dev_attr_temp3_auto_point2_temp.dev_attr.attr,
1624 &sensor_dev_attr_temp3_crit.dev_attr.attr,
1625 &dev_attr_temp3_crit_enable.attr,
1626 &dev_attr_temp3_auto_point1_pwm.attr,
1627 &dev_attr_temp3_auto_point2_pwm.attr,
Jean Delvare1d5f2c12008-02-10 19:01:15 +01001628 NULL
Jean Delvare5b34dbc2007-11-29 23:47:54 +01001629};
1630
1631static const struct attribute_group adm1026_group_temp3 = {
1632 .attrs = adm1026_attributes_temp3,
1633};
1634
1635static struct attribute *adm1026_attributes_in8_9[] = {
1636 &sensor_dev_attr_in8_input.dev_attr.attr,
1637 &sensor_dev_attr_in8_max.dev_attr.attr,
1638 &sensor_dev_attr_in8_min.dev_attr.attr,
1639 &sensor_dev_attr_in8_alarm.dev_attr.attr,
1640 &sensor_dev_attr_in9_input.dev_attr.attr,
1641 &sensor_dev_attr_in9_max.dev_attr.attr,
1642 &sensor_dev_attr_in9_min.dev_attr.attr,
1643 &sensor_dev_attr_in9_alarm.dev_attr.attr,
Jean Delvare1d5f2c12008-02-10 19:01:15 +01001644 NULL
Jean Delvare5b34dbc2007-11-29 23:47:54 +01001645};
1646
1647static const struct attribute_group adm1026_group_in8_9 = {
1648 .attrs = adm1026_attributes_in8_9,
1649};
1650
Jean Delvare57f7eb02008-07-16 19:30:08 +02001651/* Return 0 if detection is successful, -ENODEV otherwise */
Jean Delvare310ec792009-12-14 21:17:23 +01001652static int adm1026_detect(struct i2c_client *client,
Jean Delvare57f7eb02008-07-16 19:30:08 +02001653 struct i2c_board_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654{
Jean Delvare57f7eb02008-07-16 19:30:08 +02001655 struct i2c_adapter *adapter = client->adapter;
1656 int address = client->addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 int company, verstep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658
1659 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
1660 /* We need to be able to do byte I/O */
Jean Delvare57f7eb02008-07-16 19:30:08 +02001661 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 };
1663
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 /* Now, we do the remaining detection. */
1665
Jean Delvaref67fdab2007-12-01 11:24:17 +01001666 company = adm1026_read_value(client, ADM1026_REG_COMPANY);
1667 verstep = adm1026_read_value(client, ADM1026_REG_VERSTEP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668
Jean Delvare57f7eb02008-07-16 19:30:08 +02001669 dev_dbg(&adapter->dev, "Detecting device at %d,0x%02x with"
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 " COMPANY: 0x%02x and VERSTEP: 0x%02x\n",
Jean Delvaref67fdab2007-12-01 11:24:17 +01001671 i2c_adapter_id(client->adapter), client->addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 company, verstep);
1673
Jean Delvare52df6442009-12-09 20:35:57 +01001674 /* Determine the chip type. */
1675 dev_dbg(&adapter->dev, "Autodetecting device at %d,0x%02x...\n",
1676 i2c_adapter_id(adapter), address);
1677 if (company == ADM1026_COMPANY_ANALOG_DEV
1678 && verstep == ADM1026_VERSTEP_ADM1026) {
1679 /* Analog Devices ADM1026 */
1680 } else if (company == ADM1026_COMPANY_ANALOG_DEV
1681 && (verstep & 0xf0) == ADM1026_VERSTEP_GENERIC) {
1682 dev_err(&adapter->dev, "Unrecognized stepping "
1683 "0x%02x. Defaulting to ADM1026.\n", verstep);
1684 } else if ((verstep & 0xf0) == ADM1026_VERSTEP_GENERIC) {
1685 dev_err(&adapter->dev, "Found version/stepping "
1686 "0x%02x. Assuming generic ADM1026.\n",
1687 verstep);
1688 } else {
1689 dev_dbg(&adapter->dev, "Autodetection failed\n");
1690 /* Not an ADM1026... */
1691 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 }
Jean Delvare52df6442009-12-09 20:35:57 +01001693
Jean Delvare57f7eb02008-07-16 19:30:08 +02001694 strlcpy(info->type, "adm1026", I2C_NAME_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695
Jean Delvare57f7eb02008-07-16 19:30:08 +02001696 return 0;
1697}
1698
1699static int adm1026_probe(struct i2c_client *client,
1700 const struct i2c_device_id *id)
1701{
1702 struct adm1026_data *data;
1703 int err;
1704
1705 data = kzalloc(sizeof(struct adm1026_data), GFP_KERNEL);
1706 if (!data) {
1707 err = -ENOMEM;
1708 goto exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710
Jean Delvare57f7eb02008-07-16 19:30:08 +02001711 i2c_set_clientdata(client, data);
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001712 mutex_init(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 /* Set the VRM version */
Jean Delvare303760b2005-07-31 21:52:01 +02001715 data->vrm = vid_which_vrm();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716
1717 /* Initialize the ADM1026 chip */
Jean Delvaref67fdab2007-12-01 11:24:17 +01001718 adm1026_init_client(client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719
1720 /* Register sysfs hooks */
Jean Delvaref67fdab2007-12-01 11:24:17 +01001721 if ((err = sysfs_create_group(&client->dev.kobj, &adm1026_group)))
Jean Delvare57f7eb02008-07-16 19:30:08 +02001722 goto exitfree;
Jean Delvare5b34dbc2007-11-29 23:47:54 +01001723 if (data->config1 & CFG1_AIN8_9)
1724 err = sysfs_create_group(&client->dev.kobj,
1725 &adm1026_group_in8_9);
1726 else
1727 err = sysfs_create_group(&client->dev.kobj,
1728 &adm1026_group_temp3);
1729 if (err)
1730 goto exitremove;
Mark M. Hoffman681c6f72006-09-24 21:15:35 +02001731
Jean Delvaref67fdab2007-12-01 11:24:17 +01001732 data->hwmon_dev = hwmon_device_register(&client->dev);
Tony Jones1beeffe2007-08-20 13:46:20 -07001733 if (IS_ERR(data->hwmon_dev)) {
1734 err = PTR_ERR(data->hwmon_dev);
Mark M. Hoffman681c6f72006-09-24 21:15:35 +02001735 goto exitremove;
Mark M. Hoffman943b0832005-07-15 21:39:18 -04001736 }
1737
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 return 0;
1739
1740 /* Error out and cleanup code */
Mark M. Hoffman681c6f72006-09-24 21:15:35 +02001741exitremove:
Jean Delvaref67fdab2007-12-01 11:24:17 +01001742 sysfs_remove_group(&client->dev.kobj, &adm1026_group);
Jean Delvare5b34dbc2007-11-29 23:47:54 +01001743 if (data->config1 & CFG1_AIN8_9)
1744 sysfs_remove_group(&client->dev.kobj, &adm1026_group_in8_9);
1745 else
1746 sysfs_remove_group(&client->dev.kobj, &adm1026_group_temp3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747exitfree:
Alexey Dobriyan1f57ff82005-08-26 01:49:14 +04001748 kfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749exit:
1750 return err;
1751}
Mark M. Hoffman681c6f72006-09-24 21:15:35 +02001752
Jean Delvare57f7eb02008-07-16 19:30:08 +02001753static int adm1026_remove(struct i2c_client *client)
Mark M. Hoffman681c6f72006-09-24 21:15:35 +02001754{
1755 struct adm1026_data *data = i2c_get_clientdata(client);
Tony Jones1beeffe2007-08-20 13:46:20 -07001756 hwmon_device_unregister(data->hwmon_dev);
Mark M. Hoffman681c6f72006-09-24 21:15:35 +02001757 sysfs_remove_group(&client->dev.kobj, &adm1026_group);
Jean Delvare5b34dbc2007-11-29 23:47:54 +01001758 if (data->config1 & CFG1_AIN8_9)
1759 sysfs_remove_group(&client->dev.kobj, &adm1026_group_in8_9);
1760 else
1761 sysfs_remove_group(&client->dev.kobj, &adm1026_group_temp3);
Mark M. Hoffman681c6f72006-09-24 21:15:35 +02001762 kfree(data);
1763 return 0;
1764}
1765
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766static int __init sm_adm1026_init(void)
1767{
1768 return i2c_add_driver(&adm1026_driver);
1769}
1770
Jean Delvarecb01a232007-11-29 23:46:42 +01001771static void __exit sm_adm1026_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772{
1773 i2c_del_driver(&adm1026_driver);
1774}
1775
1776MODULE_LICENSE("GPL");
1777MODULE_AUTHOR("Philip Pokorny <ppokorny@penguincomputing.com>, "
Jean Delvarecb01a232007-11-29 23:46:42 +01001778 "Justin Thiessen <jthiessen@penguincomputing.com>");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779MODULE_DESCRIPTION("ADM1026 driver");
1780
1781module_init(sm_adm1026_init);
1782module_exit(sm_adm1026_exit);