blob: 7fe2441fc8455f93fc175ddeaa12531f5777cb2b [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
9 <http://www.analog.com/UploadedFiles/Data_Sheets/779263102ADM1026_a.pdf>
10
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 2 of the License, or
14 (at your option) any later version.
15
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24*/
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
40/* Insmod parameters */
Jean Delvaref4b50262005-07-31 21:49:03 +020041I2C_CLIENT_INSMOD_1(adm1026);
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
Jean Delvarecb01a232007-11-29 23:46:42 +010043static int gpio_input[17] = { -1, -1, -1, -1, -1, -1, -1, -1, -1,
44 -1, -1, -1, -1, -1, -1, -1, -1 };
Linus Torvalds1da177e2005-04-16 15:20:36 -070045static int gpio_output[17] = { -1, -1, -1, -1, -1, -1, -1, -1, -1,
46 -1, -1, -1, -1, -1, -1, -1, -1 };
47static int gpio_inverted[17] = { -1, -1, -1, -1, -1, -1, -1, -1, -1,
48 -1, -1, -1, -1, -1, -1, -1, -1 };
49static int gpio_normal[17] = { -1, -1, -1, -1, -1, -1, -1, -1, -1,
50 -1, -1, -1, -1, -1, -1, -1, -1 };
51static int gpio_fan[8] = { -1, -1, -1, -1, -1, -1, -1, -1 };
Jean Delvarecb01a232007-11-29 23:46:42 +010052module_param_array(gpio_input, int, NULL, 0);
53MODULE_PARM_DESC(gpio_input, "List of GPIO pins (0-16) to program as inputs");
54module_param_array(gpio_output, int, NULL, 0);
55MODULE_PARM_DESC(gpio_output, "List of GPIO pins (0-16) to program as "
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 "outputs");
Jean Delvarecb01a232007-11-29 23:46:42 +010057module_param_array(gpio_inverted, int, NULL, 0);
58MODULE_PARM_DESC(gpio_inverted, "List of GPIO pins (0-16) to program as "
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 "inverted");
Jean Delvarecb01a232007-11-29 23:46:42 +010060module_param_array(gpio_normal, int, NULL, 0);
61MODULE_PARM_DESC(gpio_normal, "List of GPIO pins (0-16) to program as "
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 "normal/non-inverted");
Jean Delvarecb01a232007-11-29 23:46:42 +010063module_param_array(gpio_fan, int, NULL, 0);
64MODULE_PARM_DESC(gpio_fan, "List of GPIO pins (0-7) to program as fan tachs");
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
66/* Many ADM1026 constants specified below */
67
68/* The ADM1026 registers */
Jean Delvarecb01a232007-11-29 23:46:42 +010069#define ADM1026_REG_CONFIG1 0x00
70#define CFG1_MONITOR 0x01
71#define CFG1_INT_ENABLE 0x02
72#define CFG1_INT_CLEAR 0x04
73#define CFG1_AIN8_9 0x08
74#define CFG1_THERM_HOT 0x10
75#define CFG1_DAC_AFC 0x20
76#define CFG1_PWM_AFC 0x40
77#define CFG1_RESET 0x80
78
79#define ADM1026_REG_CONFIG2 0x01
Linus Torvalds1da177e2005-04-16 15:20:36 -070080/* CONFIG2 controls FAN0/GPIO0 through FAN7/GPIO7 */
Jean Delvarecb01a232007-11-29 23:46:42 +010081
82#define ADM1026_REG_CONFIG3 0x07
83#define CFG3_GPIO16_ENABLE 0x01
84#define CFG3_CI_CLEAR 0x02
85#define CFG3_VREF_250 0x04
86#define CFG3_GPIO16_DIR 0x40
87#define CFG3_GPIO16_POL 0x80
88
89#define ADM1026_REG_E2CONFIG 0x13
90#define E2CFG_READ 0x01
91#define E2CFG_WRITE 0x02
92#define E2CFG_ERASE 0x04
93#define E2CFG_ROM 0x08
94#define E2CFG_CLK_EXT 0x80
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
96/* There are 10 general analog inputs and 7 dedicated inputs
97 * They are:
98 * 0 - 9 = AIN0 - AIN9
99 * 10 = Vbat
100 * 11 = 3.3V Standby
101 * 12 = 3.3V Main
102 * 13 = +5V
103 * 14 = Vccp (CPU core voltage)
104 * 15 = +12V
105 * 16 = -12V
106 */
107static u16 ADM1026_REG_IN[] = {
108 0x30, 0x31, 0x32, 0x33, 0x34, 0x35,
109 0x36, 0x37, 0x27, 0x29, 0x26, 0x2a,
110 0x2b, 0x2c, 0x2d, 0x2e, 0x2f
111 };
112static u16 ADM1026_REG_IN_MIN[] = {
113 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d,
114 0x5e, 0x5f, 0x6d, 0x49, 0x6b, 0x4a,
115 0x4b, 0x4c, 0x4d, 0x4e, 0x4f
116 };
117static u16 ADM1026_REG_IN_MAX[] = {
118 0x50, 0x51, 0x52, 0x53, 0x54, 0x55,
119 0x56, 0x57, 0x6c, 0x41, 0x6a, 0x42,
120 0x43, 0x44, 0x45, 0x46, 0x47
121 };
122
123/* Temperatures are:
124 * 0 - Internal
125 * 1 - External 1
126 * 2 - External 2
127 */
128static u16 ADM1026_REG_TEMP[] = { 0x1f, 0x28, 0x29 };
129static u16 ADM1026_REG_TEMP_MIN[] = { 0x69, 0x48, 0x49 };
130static u16 ADM1026_REG_TEMP_MAX[] = { 0x68, 0x40, 0x41 };
131static u16 ADM1026_REG_TEMP_TMIN[] = { 0x10, 0x11, 0x12 };
132static u16 ADM1026_REG_TEMP_THERM[] = { 0x0d, 0x0e, 0x0f };
133static u16 ADM1026_REG_TEMP_OFFSET[] = { 0x1e, 0x6e, 0x6f };
134
Jean Delvarecb01a232007-11-29 23:46:42 +0100135#define ADM1026_REG_FAN(nr) (0x38 + (nr))
136#define ADM1026_REG_FAN_MIN(nr) (0x60 + (nr))
137#define ADM1026_REG_FAN_DIV_0_3 0x02
138#define ADM1026_REG_FAN_DIV_4_7 0x03
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
Jean Delvarecb01a232007-11-29 23:46:42 +0100140#define ADM1026_REG_DAC 0x04
141#define ADM1026_REG_PWM 0x05
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
Jean Delvarecb01a232007-11-29 23:46:42 +0100143#define ADM1026_REG_GPIO_CFG_0_3 0x08
144#define ADM1026_REG_GPIO_CFG_4_7 0x09
145#define ADM1026_REG_GPIO_CFG_8_11 0x0a
146#define ADM1026_REG_GPIO_CFG_12_15 0x0b
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147/* CFG_16 in REG_CFG3 */
Jean Delvarecb01a232007-11-29 23:46:42 +0100148#define ADM1026_REG_GPIO_STATUS_0_7 0x24
149#define ADM1026_REG_GPIO_STATUS_8_15 0x25
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150/* STATUS_16 in REG_STATUS4 */
Jean Delvarecb01a232007-11-29 23:46:42 +0100151#define ADM1026_REG_GPIO_MASK_0_7 0x1c
152#define ADM1026_REG_GPIO_MASK_8_15 0x1d
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153/* MASK_16 in REG_MASK4 */
154
Jean Delvarecb01a232007-11-29 23:46:42 +0100155#define ADM1026_REG_COMPANY 0x16
156#define ADM1026_REG_VERSTEP 0x17
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157/* These are the recognized values for the above regs */
Jean Delvarecb01a232007-11-29 23:46:42 +0100158#define ADM1026_COMPANY_ANALOG_DEV 0x41
159#define ADM1026_VERSTEP_GENERIC 0x40
160#define ADM1026_VERSTEP_ADM1026 0x44
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
Jean Delvarecb01a232007-11-29 23:46:42 +0100162#define ADM1026_REG_MASK1 0x18
163#define ADM1026_REG_MASK2 0x19
164#define ADM1026_REG_MASK3 0x1a
165#define ADM1026_REG_MASK4 0x1b
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
Jean Delvarecb01a232007-11-29 23:46:42 +0100167#define ADM1026_REG_STATUS1 0x20
168#define ADM1026_REG_STATUS2 0x21
169#define ADM1026_REG_STATUS3 0x22
170#define ADM1026_REG_STATUS4 0x23
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
172#define ADM1026_FAN_ACTIVATION_TEMP_HYST -6
Jean Delvarecb01a232007-11-29 23:46:42 +0100173#define ADM1026_FAN_CONTROL_TEMP_RANGE 20
174#define ADM1026_PWM_MAX 255
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
Jean Delvarecb01a232007-11-29 23:46:42 +0100176/* Conversions. Rounding and limit checking is only done on the TO_REG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 * variants. Note that you should be a bit careful with which arguments
178 * these macros are called: arguments may be evaluated more than once.
179 */
180
181/* IN are scaled acording to built-in resistors. These are the
182 * voltages corresponding to 3/4 of full scale (192 or 0xc0)
183 * NOTE: The -12V input needs an additional factor to account
184 * for the Vref pullup resistor.
185 * NEG12_OFFSET = SCALE * Vref / V-192 - Vref
186 * = 13875 * 2.50 / 1.875 - 2500
187 * = 16000
188 *
189 * The values in this table are based on Table II, page 15 of the
190 * datasheet.
191 */
Jean Delvarecb01a232007-11-29 23:46:42 +0100192static int adm1026_scaling[] = { /* .001 Volts */
193 2250, 2250, 2250, 2250, 2250, 2250,
194 1875, 1875, 1875, 1875, 3000, 3330,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 3330, 4995, 2250, 12000, 13875
196 };
197#define NEG12_OFFSET 16000
Jean Delvarecb01a232007-11-29 23:46:42 +0100198#define SCALE(val, from, to) (((val)*(to) + ((from)/2))/(from))
199#define INS_TO_REG(n, val) (SENSORS_LIMIT(SCALE(val, adm1026_scaling[n], 192),\
200 0, 255))
201#define INS_FROM_REG(n, val) (SCALE(val, 192, adm1026_scaling[n]))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
203/* FAN speed is measured using 22.5kHz clock and counts for 2 pulses
204 * and we assume a 2 pulse-per-rev fan tach signal
205 * 22500 kHz * 60 (sec/min) * 2 (pulse) / 2 (pulse/rev) == 1350000
206 */
Jean Delvarecb01a232007-11-29 23:46:42 +0100207#define FAN_TO_REG(val, div) ((val) <= 0 ? 0xff : \
208 SENSORS_LIMIT(1350000/((val)*(div)), 1, 254))
209#define FAN_FROM_REG(val, div) ((val) == 0 ? -1:(val) == 0xff ? 0 : \
210 1350000/((val)*(div)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211#define DIV_FROM_REG(val) (1<<(val))
Jean Delvarecb01a232007-11-29 23:46:42 +0100212#define DIV_TO_REG(val) ((val) >= 8 ? 3 : (val) >= 4 ? 2 : (val) >= 2 ? 1 : 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213
214/* Temperature is reported in 1 degC increments */
215#define TEMP_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 TEMP_FROM_REG(val) ((val) * 1000)
218#define OFFSET_TO_REG(val) (SENSORS_LIMIT(((val)+((val)<0 ? -500 : 500))/1000,\
Jean Delvarecb01a232007-11-29 23:46:42 +0100219 -127, 127))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220#define OFFSET_FROM_REG(val) ((val) * 1000)
221
Jean Delvarecb01a232007-11-29 23:46:42 +0100222#define PWM_TO_REG(val) (SENSORS_LIMIT(val, 0, 255))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223#define PWM_FROM_REG(val) (val)
224
225#define PWM_MIN_TO_REG(val) ((val) & 0xf0)
226#define PWM_MIN_FROM_REG(val) (((val) & 0xf0) + ((val) >> 4))
227
Jean Delvarecb01a232007-11-29 23:46:42 +0100228/* Analog output is a voltage, and scaled to millivolts. The datasheet
229 * indicates that the DAC could be used to drive the fans, but in our
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 * example board (Arima HDAMA) it isn't connected to the fans at all.
231 */
Jean Delvarecb01a232007-11-29 23:46:42 +0100232#define DAC_TO_REG(val) (SENSORS_LIMIT(((((val)*255)+500)/2500), 0, 255))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233#define DAC_FROM_REG(val) (((val)*2500)/255)
234
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235/* Chip sampling rates
236 *
237 * Some sensors are not updated more frequently than once per second
238 * so it doesn't make sense to read them more often than that.
239 * We cache the results and return the saved data if the driver
240 * is called again before a second has elapsed.
241 *
242 * Also, there is significant configuration data for this chip
243 * So, we keep the config data up to date in the cache
244 * when it is written and only sample it once every 5 *minutes*
245 */
Jean Delvarecb01a232007-11-29 23:46:42 +0100246#define ADM1026_DATA_INTERVAL (1 * HZ)
247#define ADM1026_CONFIG_INTERVAL (5 * 60 * HZ)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
249/* We allow for multiple chips in a single system.
250 *
251 * For each registered ADM1026, we need to keep state information
252 * at client->data. The adm1026_data structure is dynamically
253 * allocated, when a new client structure is allocated. */
254
255struct pwm_data {
256 u8 pwm;
257 u8 enable;
258 u8 auto_pwm_min;
259};
260
261struct adm1026_data {
Tony Jones1beeffe2007-08-20 13:46:20 -0700262 struct device *hwmon_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100264 struct mutex update_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 int valid; /* !=0 if following fields are valid */
266 unsigned long last_reading; /* In jiffies */
267 unsigned long last_config; /* In jiffies */
268
Jean Delvarecb01a232007-11-29 23:46:42 +0100269 u8 in[17]; /* Register value */
270 u8 in_max[17]; /* Register value */
271 u8 in_min[17]; /* Register value */
272 s8 temp[3]; /* Register value */
273 s8 temp_min[3]; /* Register value */
274 s8 temp_max[3]; /* Register value */
275 s8 temp_tmin[3]; /* Register value */
276 s8 temp_crit[3]; /* Register value */
277 s8 temp_offset[3]; /* Register value */
278 u8 fan[8]; /* Register value */
279 u8 fan_min[8]; /* Register value */
280 u8 fan_div[8]; /* Decoded value */
281 struct pwm_data pwm1; /* Pwm control values */
282 int vid; /* Decoded value */
283 u8 vrm; /* VRM version */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 u8 analog_out; /* Register value (DAC) */
Jean Delvarecb01a232007-11-29 23:46:42 +0100285 long alarms; /* Register encoding, combined */
286 long alarm_mask; /* Register encoding, combined */
287 long gpio; /* Register encoding, combined */
288 long gpio_mask; /* Register encoding, combined */
289 u8 gpio_config[17]; /* Decoded value */
290 u8 config1; /* Register value */
291 u8 config2; /* Register value */
292 u8 config3; /* Register value */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293};
294
Jean Delvare57f7eb02008-07-16 19:30:08 +0200295static int adm1026_probe(struct i2c_client *client,
296 const struct i2c_device_id *id);
297static int adm1026_detect(struct i2c_client *client, int kind,
298 struct i2c_board_info *info);
299static int adm1026_remove(struct i2c_client *client);
Darren Jenkinsf6c27fc2006-02-27 23:14:58 +0100300static int adm1026_read_value(struct i2c_client *client, u8 reg);
301static int adm1026_write_value(struct i2c_client *client, u8 reg, int value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302static void adm1026_print_gpio(struct i2c_client *client);
Jean Delvarecb01a232007-11-29 23:46:42 +0100303static void adm1026_fixup_gpio(struct i2c_client *client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304static struct adm1026_data *adm1026_update_device(struct device *dev);
305static void adm1026_init_client(struct i2c_client *client);
306
307
Jean Delvare57f7eb02008-07-16 19:30:08 +0200308static const struct i2c_device_id adm1026_id[] = {
309 { "adm1026", adm1026 },
310 { }
311};
312MODULE_DEVICE_TABLE(i2c, adm1026_id);
313
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314static struct i2c_driver adm1026_driver = {
Jean Delvare57f7eb02008-07-16 19:30:08 +0200315 .class = I2C_CLASS_HWMON,
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100316 .driver = {
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100317 .name = "adm1026",
318 },
Jean Delvare57f7eb02008-07-16 19:30:08 +0200319 .probe = adm1026_probe,
320 .remove = adm1026_remove,
321 .id_table = adm1026_id,
322 .detect = adm1026_detect,
323 .address_data = &addr_data,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324};
325
Ben Dooksc49efce2005-10-26 21:07:25 +0200326static int adm1026_read_value(struct i2c_client *client, u8 reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327{
328 int res;
329
330 if (reg < 0x80) {
331 /* "RAM" locations */
332 res = i2c_smbus_read_byte_data(client, reg) & 0xff;
333 } else {
334 /* EEPROM, do nothing */
335 res = 0;
336 }
337 return res;
338}
339
Ben Dooksc49efce2005-10-26 21:07:25 +0200340static int adm1026_write_value(struct i2c_client *client, u8 reg, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341{
342 int res;
343
344 if (reg < 0x80) {
345 /* "RAM" locations */
346 res = i2c_smbus_write_byte_data(client, reg, value);
347 } else {
348 /* EEPROM, do nothing */
349 res = 0;
350 }
351 return res;
352}
353
Ben Dooksc49efce2005-10-26 21:07:25 +0200354static void adm1026_init_client(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355{
356 int value, i;
357 struct adm1026_data *data = i2c_get_clientdata(client);
358
Jean Delvarecb01a232007-11-29 23:46:42 +0100359 dev_dbg(&client->dev, "Initializing device\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 /* Read chip config */
361 data->config1 = adm1026_read_value(client, ADM1026_REG_CONFIG1);
362 data->config2 = adm1026_read_value(client, ADM1026_REG_CONFIG2);
363 data->config3 = adm1026_read_value(client, ADM1026_REG_CONFIG3);
364
365 /* Inform user of chip config */
366 dev_dbg(&client->dev, "ADM1026_REG_CONFIG1 is: 0x%02x\n",
367 data->config1);
368 if ((data->config1 & CFG1_MONITOR) == 0) {
369 dev_dbg(&client->dev, "Monitoring not currently "
370 "enabled.\n");
371 }
372 if (data->config1 & CFG1_INT_ENABLE) {
373 dev_dbg(&client->dev, "SMBALERT interrupts are "
374 "enabled.\n");
375 }
376 if (data->config1 & CFG1_AIN8_9) {
377 dev_dbg(&client->dev, "in8 and in9 enabled. "
378 "temp3 disabled.\n");
379 } else {
380 dev_dbg(&client->dev, "temp3 enabled. in8 and "
381 "in9 disabled.\n");
382 }
383 if (data->config1 & CFG1_THERM_HOT) {
384 dev_dbg(&client->dev, "Automatic THERM, PWM, "
385 "and temp limits enabled.\n");
386 }
387
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 if (data->config3 & CFG3_GPIO16_ENABLE) {
Jean Delvare368609c2005-07-29 12:15:07 -0700389 dev_dbg(&client->dev, "GPIO16 enabled. THERM "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 "pin disabled.\n");
391 } else {
392 dev_dbg(&client->dev, "THERM pin enabled. "
393 "GPIO16 disabled.\n");
394 }
395 if (data->config3 & CFG3_VREF_250) {
396 dev_dbg(&client->dev, "Vref is 2.50 Volts.\n");
397 } else {
398 dev_dbg(&client->dev, "Vref is 1.82 Volts.\n");
399 }
400 /* Read and pick apart the existing GPIO configuration */
401 value = 0;
402 for (i = 0;i <= 15;++i) {
403 if ((i & 0x03) == 0) {
404 value = adm1026_read_value(client,
405 ADM1026_REG_GPIO_CFG_0_3 + i/4);
406 }
407 data->gpio_config[i] = value & 0x03;
408 value >>= 2;
409 }
410 data->gpio_config[16] = (data->config3 >> 6) & 0x03;
411
412 /* ... and then print it */
413 adm1026_print_gpio(client);
414
415 /* If the user asks us to reprogram the GPIO config, then
416 * do it now.
417 */
418 if (gpio_input[0] != -1 || gpio_output[0] != -1
419 || gpio_inverted[0] != -1 || gpio_normal[0] != -1
420 || gpio_fan[0] != -1) {
421 adm1026_fixup_gpio(client);
422 }
423
424 /* WE INTENTIONALLY make no changes to the limits,
425 * offsets, pwms, fans and zones. If they were
426 * configured, we don't want to mess with them.
427 * If they weren't, the default is 100% PWM, no
428 * control and will suffice until 'sensors -s'
Jean Delvarecb01a232007-11-29 23:46:42 +0100429 * can be run by the user. We DO set the default
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 * value for pwm1.auto_pwm_min to its maximum
431 * so that enabling automatic pwm fan control
Jean Delvarecb01a232007-11-29 23:46:42 +0100432 * without first setting a value for pwm1.auto_pwm_min
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 * will not result in potentially dangerous fan speed decrease.
434 */
435 data->pwm1.auto_pwm_min=255;
436 /* Start monitoring */
437 value = adm1026_read_value(client, ADM1026_REG_CONFIG1);
438 /* Set MONITOR, clear interrupt acknowledge and s/w reset */
439 value = (value | CFG1_MONITOR) & (~CFG1_INT_CLEAR & ~CFG1_RESET);
440 dev_dbg(&client->dev, "Setting CONFIG to: 0x%02x\n", value);
441 data->config1 = value;
442 adm1026_write_value(client, ADM1026_REG_CONFIG1, value);
443
444 /* initialize fan_div[] to hardware defaults */
445 value = adm1026_read_value(client, ADM1026_REG_FAN_DIV_0_3) |
446 (adm1026_read_value(client, ADM1026_REG_FAN_DIV_4_7) << 8);
447 for (i = 0;i <= 7;++i) {
448 data->fan_div[i] = DIV_FROM_REG(value & 0x03);
449 value >>= 2;
450 }
451}
452
Ben Dooksc49efce2005-10-26 21:07:25 +0200453static void adm1026_print_gpio(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454{
455 struct adm1026_data *data = i2c_get_clientdata(client);
Jean Delvarecb01a232007-11-29 23:46:42 +0100456 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457
458 dev_dbg(&client->dev, "GPIO config is:");
459 for (i = 0;i <= 7;++i) {
460 if (data->config2 & (1 << i)) {
461 dev_dbg(&client->dev, "\t%sGP%s%d\n",
462 data->gpio_config[i] & 0x02 ? "" : "!",
463 data->gpio_config[i] & 0x01 ? "OUT" : "IN",
464 i);
465 } else {
466 dev_dbg(&client->dev, "\tFAN%d\n", i);
467 }
468 }
469 for (i = 8;i <= 15;++i) {
470 dev_dbg(&client->dev, "\t%sGP%s%d\n",
471 data->gpio_config[i] & 0x02 ? "" : "!",
472 data->gpio_config[i] & 0x01 ? "OUT" : "IN",
473 i);
474 }
475 if (data->config3 & CFG3_GPIO16_ENABLE) {
476 dev_dbg(&client->dev, "\t%sGP%s16\n",
477 data->gpio_config[16] & 0x02 ? "" : "!",
478 data->gpio_config[16] & 0x01 ? "OUT" : "IN");
479 } else {
Jean Delvarecb01a232007-11-29 23:46:42 +0100480 /* GPIO16 is THERM */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 dev_dbg(&client->dev, "\tTHERM\n");
482 }
483}
484
Ben Dooksc49efce2005-10-26 21:07:25 +0200485static void adm1026_fixup_gpio(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486{
487 struct adm1026_data *data = i2c_get_clientdata(client);
Jean Delvarecb01a232007-11-29 23:46:42 +0100488 int i;
489 int value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490
491 /* Make the changes requested. */
492 /* We may need to unlock/stop monitoring or soft-reset the
493 * chip before we can make changes. This hasn't been
494 * tested much. FIXME
495 */
496
497 /* Make outputs */
498 for (i = 0;i <= 16;++i) {
499 if (gpio_output[i] >= 0 && gpio_output[i] <= 16) {
500 data->gpio_config[gpio_output[i]] |= 0x01;
501 }
502 /* if GPIO0-7 is output, it isn't a FAN tach */
503 if (gpio_output[i] >= 0 && gpio_output[i] <= 7) {
504 data->config2 |= 1 << gpio_output[i];
505 }
506 }
507
508 /* Input overrides output */
509 for (i = 0;i <= 16;++i) {
510 if (gpio_input[i] >= 0 && gpio_input[i] <= 16) {
511 data->gpio_config[gpio_input[i]] &= ~ 0x01;
512 }
513 /* if GPIO0-7 is input, it isn't a FAN tach */
514 if (gpio_input[i] >= 0 && gpio_input[i] <= 7) {
515 data->config2 |= 1 << gpio_input[i];
516 }
517 }
518
Jean Delvarecb01a232007-11-29 23:46:42 +0100519 /* Inverted */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 for (i = 0;i <= 16;++i) {
521 if (gpio_inverted[i] >= 0 && gpio_inverted[i] <= 16) {
522 data->gpio_config[gpio_inverted[i]] &= ~ 0x02;
523 }
524 }
525
Jean Delvarecb01a232007-11-29 23:46:42 +0100526 /* Normal overrides inverted */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 for (i = 0;i <= 16;++i) {
528 if (gpio_normal[i] >= 0 && gpio_normal[i] <= 16) {
529 data->gpio_config[gpio_normal[i]] |= 0x02;
530 }
531 }
532
533 /* Fan overrides input and output */
534 for (i = 0;i <= 7;++i) {
535 if (gpio_fan[i] >= 0 && gpio_fan[i] <= 7) {
536 data->config2 &= ~(1 << gpio_fan[i]);
537 }
538 }
539
540 /* Write new configs to registers */
541 adm1026_write_value(client, ADM1026_REG_CONFIG2, data->config2);
542 data->config3 = (data->config3 & 0x3f)
543 | ((data->gpio_config[16] & 0x03) << 6);
544 adm1026_write_value(client, ADM1026_REG_CONFIG3, data->config3);
545 for (i = 15, value = 0;i >= 0;--i) {
546 value <<= 2;
547 value |= data->gpio_config[i] & 0x03;
548 if ((i & 0x03) == 0) {
549 adm1026_write_value(client,
550 ADM1026_REG_GPIO_CFG_0_3 + i/4,
551 value);
552 value = 0;
553 }
554 }
555
556 /* Print the new config */
557 adm1026_print_gpio(client);
558}
559
560
561static struct adm1026_data *adm1026_update_device(struct device *dev)
562{
563 struct i2c_client *client = to_i2c_client(dev);
564 struct adm1026_data *data = i2c_get_clientdata(client);
565 int i;
566 long value, alarms, gpio;
567
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100568 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 if (!data->valid
570 || time_after(jiffies, data->last_reading + ADM1026_DATA_INTERVAL)) {
571 /* Things that change quickly */
Jean Delvarecb01a232007-11-29 23:46:42 +0100572 dev_dbg(&client->dev, "Reading sensor values\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 for (i = 0;i <= 16;++i) {
574 data->in[i] =
575 adm1026_read_value(client, ADM1026_REG_IN[i]);
576 }
577
578 for (i = 0;i <= 7;++i) {
579 data->fan[i] =
580 adm1026_read_value(client, ADM1026_REG_FAN(i));
581 }
582
583 for (i = 0;i <= 2;++i) {
584 /* NOTE: temp[] is s8 and we assume 2's complement
Jean Delvarecb01a232007-11-29 23:46:42 +0100585 * "conversion" in the assignment */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 data->temp[i] =
587 adm1026_read_value(client, ADM1026_REG_TEMP[i]);
588 }
589
Jean Delvarecb01a232007-11-29 23:46:42 +0100590 data->pwm1.pwm = adm1026_read_value(client,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 ADM1026_REG_PWM);
Jean Delvarecb01a232007-11-29 23:46:42 +0100592 data->analog_out = adm1026_read_value(client,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 ADM1026_REG_DAC);
594 /* GPIO16 is MSbit of alarms, move it to gpio */
595 alarms = adm1026_read_value(client, ADM1026_REG_STATUS4);
Jean Delvarecb01a232007-11-29 23:46:42 +0100596 gpio = alarms & 0x80 ? 0x0100 : 0; /* GPIO16 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 alarms &= 0x7f;
598 alarms <<= 8;
599 alarms |= adm1026_read_value(client, ADM1026_REG_STATUS3);
600 alarms <<= 8;
601 alarms |= adm1026_read_value(client, ADM1026_REG_STATUS2);
602 alarms <<= 8;
603 alarms |= adm1026_read_value(client, ADM1026_REG_STATUS1);
604 data->alarms = alarms;
605
606 /* Read the GPIO values */
Jean Delvarecb01a232007-11-29 23:46:42 +0100607 gpio |= adm1026_read_value(client,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 ADM1026_REG_GPIO_STATUS_8_15);
609 gpio <<= 8;
Jean Delvarecb01a232007-11-29 23:46:42 +0100610 gpio |= adm1026_read_value(client,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 ADM1026_REG_GPIO_STATUS_0_7);
612 data->gpio = gpio;
613
614 data->last_reading = jiffies;
Jean Delvarecb01a232007-11-29 23:46:42 +0100615 }; /* last_reading */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616
617 if (!data->valid ||
618 time_after(jiffies, data->last_config + ADM1026_CONFIG_INTERVAL)) {
619 /* Things that don't change often */
620 dev_dbg(&client->dev, "Reading config values\n");
621 for (i = 0;i <= 16;++i) {
Jean Delvarecb01a232007-11-29 23:46:42 +0100622 data->in_min[i] = adm1026_read_value(client,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 ADM1026_REG_IN_MIN[i]);
Jean Delvarecb01a232007-11-29 23:46:42 +0100624 data->in_max[i] = adm1026_read_value(client,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 ADM1026_REG_IN_MAX[i]);
626 }
627
628 value = adm1026_read_value(client, ADM1026_REG_FAN_DIV_0_3)
629 | (adm1026_read_value(client, ADM1026_REG_FAN_DIV_4_7)
630 << 8);
631 for (i = 0;i <= 7;++i) {
Jean Delvarecb01a232007-11-29 23:46:42 +0100632 data->fan_min[i] = adm1026_read_value(client,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 ADM1026_REG_FAN_MIN(i));
634 data->fan_div[i] = DIV_FROM_REG(value & 0x03);
635 value >>= 2;
636 }
637
638 for (i = 0; i <= 2; ++i) {
Jean Delvarecb01a232007-11-29 23:46:42 +0100639 /* NOTE: temp_xxx[] are s8 and we assume 2's
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 * complement "conversion" in the assignment
641 */
Jean Delvarecb01a232007-11-29 23:46:42 +0100642 data->temp_min[i] = adm1026_read_value(client,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 ADM1026_REG_TEMP_MIN[i]);
Jean Delvarecb01a232007-11-29 23:46:42 +0100644 data->temp_max[i] = adm1026_read_value(client,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 ADM1026_REG_TEMP_MAX[i]);
Jean Delvarecb01a232007-11-29 23:46:42 +0100646 data->temp_tmin[i] = adm1026_read_value(client,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 ADM1026_REG_TEMP_TMIN[i]);
Jean Delvarecb01a232007-11-29 23:46:42 +0100648 data->temp_crit[i] = adm1026_read_value(client,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 ADM1026_REG_TEMP_THERM[i]);
Jean Delvarecb01a232007-11-29 23:46:42 +0100650 data->temp_offset[i] = adm1026_read_value(client,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 ADM1026_REG_TEMP_OFFSET[i]);
652 }
653
654 /* Read the STATUS/alarm masks */
Jean Delvarecb01a232007-11-29 23:46:42 +0100655 alarms = adm1026_read_value(client, ADM1026_REG_MASK4);
656 gpio = alarms & 0x80 ? 0x0100 : 0; /* GPIO16 */
657 alarms = (alarms & 0x7f) << 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 alarms |= adm1026_read_value(client, ADM1026_REG_MASK3);
659 alarms <<= 8;
660 alarms |= adm1026_read_value(client, ADM1026_REG_MASK2);
661 alarms <<= 8;
662 alarms |= adm1026_read_value(client, ADM1026_REG_MASK1);
663 data->alarm_mask = alarms;
664
665 /* Read the GPIO values */
Jean Delvarecb01a232007-11-29 23:46:42 +0100666 gpio |= adm1026_read_value(client,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 ADM1026_REG_GPIO_MASK_8_15);
668 gpio <<= 8;
669 gpio |= adm1026_read_value(client, ADM1026_REG_GPIO_MASK_0_7);
670 data->gpio_mask = gpio;
671
672 /* Read various values from CONFIG1 */
Jean Delvarecb01a232007-11-29 23:46:42 +0100673 data->config1 = adm1026_read_value(client,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 ADM1026_REG_CONFIG1);
675 if (data->config1 & CFG1_PWM_AFC) {
676 data->pwm1.enable = 2;
Jean Delvarecb01a232007-11-29 23:46:42 +0100677 data->pwm1.auto_pwm_min =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 PWM_MIN_FROM_REG(data->pwm1.pwm);
679 }
680 /* Read the GPIO config */
Jean Delvarecb01a232007-11-29 23:46:42 +0100681 data->config2 = adm1026_read_value(client,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 ADM1026_REG_CONFIG2);
Jean Delvarecb01a232007-11-29 23:46:42 +0100683 data->config3 = adm1026_read_value(client,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 ADM1026_REG_CONFIG3);
685 data->gpio_config[16] = (data->config3 >> 6) & 0x03;
686
687 value = 0;
688 for (i = 0;i <= 15;++i) {
689 if ((i & 0x03) == 0) {
690 value = adm1026_read_value(client,
691 ADM1026_REG_GPIO_CFG_0_3 + i/4);
692 }
693 data->gpio_config[i] = value & 0x03;
694 value >>= 2;
695 }
696
697 data->last_config = jiffies;
Jean Delvarecb01a232007-11-29 23:46:42 +0100698 }; /* last_config */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699
700 dev_dbg(&client->dev, "Setting VID from GPIO11-15.\n");
701 data->vid = (data->gpio >> 11) & 0x1f;
702 data->valid = 1;
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100703 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 return data;
705}
706
Yani Ioannou050480f2005-06-05 10:51:46 +0200707static ssize_t show_in(struct device *dev, struct device_attribute *attr,
708 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709{
Yani Ioannou050480f2005-06-05 10:51:46 +0200710 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
711 int nr = sensor_attr->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 struct adm1026_data *data = adm1026_update_device(dev);
Jean Delvarecb01a232007-11-29 23:46:42 +0100713 return sprintf(buf, "%d\n", INS_FROM_REG(nr, data->in[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714}
Yani Ioannou050480f2005-06-05 10:51:46 +0200715static ssize_t show_in_min(struct device *dev, struct device_attribute *attr,
716 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717{
Yani Ioannou050480f2005-06-05 10:51:46 +0200718 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
719 int nr = sensor_attr->index;
Jean Delvarecb01a232007-11-29 23:46:42 +0100720 struct adm1026_data *data = adm1026_update_device(dev);
721 return sprintf(buf, "%d\n", INS_FROM_REG(nr, data->in_min[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722}
Yani Ioannou050480f2005-06-05 10:51:46 +0200723static ssize_t set_in_min(struct device *dev, struct device_attribute *attr,
724 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725{
Yani Ioannou050480f2005-06-05 10:51:46 +0200726 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
727 int nr = sensor_attr->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 struct i2c_client *client = to_i2c_client(dev);
729 struct adm1026_data *data = i2c_get_clientdata(client);
730 int val = simple_strtol(buf, NULL, 10);
731
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100732 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 data->in_min[nr] = INS_TO_REG(nr, val);
734 adm1026_write_value(client, ADM1026_REG_IN_MIN[nr], data->in_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100735 mutex_unlock(&data->update_lock);
Jean Delvarecb01a232007-11-29 23:46:42 +0100736 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737}
Yani Ioannou050480f2005-06-05 10:51:46 +0200738static ssize_t show_in_max(struct device *dev, struct device_attribute *attr,
739 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740{
Yani Ioannou050480f2005-06-05 10:51:46 +0200741 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
742 int nr = sensor_attr->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 struct adm1026_data *data = adm1026_update_device(dev);
Jean Delvarecb01a232007-11-29 23:46:42 +0100744 return sprintf(buf, "%d\n", INS_FROM_REG(nr, data->in_max[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745}
Yani Ioannou050480f2005-06-05 10:51:46 +0200746static ssize_t set_in_max(struct device *dev, struct device_attribute *attr,
747 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748{
Yani Ioannou050480f2005-06-05 10:51:46 +0200749 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
750 int nr = sensor_attr->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 struct i2c_client *client = to_i2c_client(dev);
752 struct adm1026_data *data = i2c_get_clientdata(client);
753 int val = simple_strtol(buf, NULL, 10);
754
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100755 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 data->in_max[nr] = INS_TO_REG(nr, val);
757 adm1026_write_value(client, ADM1026_REG_IN_MAX[nr], data->in_max[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100758 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 return count;
760}
761
Yani Ioannou050480f2005-06-05 10:51:46 +0200762#define in_reg(offset) \
763static SENSOR_DEVICE_ATTR(in##offset##_input, S_IRUGO, show_in, \
764 NULL, offset); \
765static SENSOR_DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \
766 show_in_min, set_in_min, offset); \
767static SENSOR_DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \
768 show_in_max, set_in_max, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769
770
771in_reg(0);
772in_reg(1);
773in_reg(2);
774in_reg(3);
775in_reg(4);
776in_reg(5);
777in_reg(6);
778in_reg(7);
779in_reg(8);
780in_reg(9);
781in_reg(10);
782in_reg(11);
783in_reg(12);
784in_reg(13);
785in_reg(14);
786in_reg(15);
787
Yani Ioannou74880c02005-05-17 06:41:12 -0400788static ssize_t show_in16(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789{
790 struct adm1026_data *data = adm1026_update_device(dev);
Jean Delvarecb01a232007-11-29 23:46:42 +0100791 return sprintf(buf, "%d\n", INS_FROM_REG(16, data->in[16]) -
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 NEG12_OFFSET);
793}
Yani Ioannou74880c02005-05-17 06:41:12 -0400794static ssize_t show_in16_min(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795{
Jean Delvarecb01a232007-11-29 23:46:42 +0100796 struct adm1026_data *data = adm1026_update_device(dev);
797 return sprintf(buf, "%d\n", INS_FROM_REG(16, data->in_min[16])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 - NEG12_OFFSET);
799}
Yani Ioannou74880c02005-05-17 06:41:12 -0400800static 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 -0700801{
802 struct i2c_client *client = to_i2c_client(dev);
803 struct adm1026_data *data = i2c_get_clientdata(client);
804 int val = simple_strtol(buf, NULL, 10);
805
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100806 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 data->in_min[16] = INS_TO_REG(16, val + NEG12_OFFSET);
808 adm1026_write_value(client, ADM1026_REG_IN_MIN[16], data->in_min[16]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100809 mutex_unlock(&data->update_lock);
Jean Delvarecb01a232007-11-29 23:46:42 +0100810 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811}
Yani Ioannou74880c02005-05-17 06:41:12 -0400812static ssize_t show_in16_max(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813{
814 struct adm1026_data *data = adm1026_update_device(dev);
Jean Delvarecb01a232007-11-29 23:46:42 +0100815 return sprintf(buf, "%d\n", INS_FROM_REG(16, data->in_max[16])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 - NEG12_OFFSET);
817}
Yani Ioannou74880c02005-05-17 06:41:12 -0400818static 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 -0700819{
820 struct i2c_client *client = to_i2c_client(dev);
821 struct adm1026_data *data = i2c_get_clientdata(client);
822 int val = simple_strtol(buf, NULL, 10);
823
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100824 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 data->in_max[16] = INS_TO_REG(16, val+NEG12_OFFSET);
826 adm1026_write_value(client, ADM1026_REG_IN_MAX[16], data->in_max[16]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100827 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 return count;
829}
830
Yani Ioannou050480f2005-06-05 10:51:46 +0200831static SENSOR_DEVICE_ATTR(in16_input, S_IRUGO, show_in16, NULL, 16);
832static SENSOR_DEVICE_ATTR(in16_min, S_IRUGO | S_IWUSR, show_in16_min, set_in16_min, 16);
833static SENSOR_DEVICE_ATTR(in16_max, S_IRUGO | S_IWUSR, show_in16_max, set_in16_max, 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834
835
836
837
838/* Now add fan read/write functions */
839
Yani Ioannou050480f2005-06-05 10:51:46 +0200840static ssize_t show_fan(struct device *dev, struct device_attribute *attr,
841 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842{
Yani Ioannou050480f2005-06-05 10:51:46 +0200843 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
844 int nr = sensor_attr->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 struct adm1026_data *data = adm1026_update_device(dev);
Jean Delvarecb01a232007-11-29 23:46:42 +0100846 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[nr],
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 data->fan_div[nr]));
848}
Yani Ioannou050480f2005-06-05 10:51:46 +0200849static ssize_t show_fan_min(struct device *dev, struct device_attribute *attr,
850 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851{
Yani Ioannou050480f2005-06-05 10:51:46 +0200852 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
853 int nr = sensor_attr->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 struct adm1026_data *data = adm1026_update_device(dev);
Jean Delvarecb01a232007-11-29 23:46:42 +0100855 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_min[nr],
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 data->fan_div[nr]));
857}
Yani Ioannou050480f2005-06-05 10:51:46 +0200858static ssize_t set_fan_min(struct device *dev, struct device_attribute *attr,
859 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860{
Yani Ioannou050480f2005-06-05 10:51:46 +0200861 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
862 int nr = sensor_attr->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 struct i2c_client *client = to_i2c_client(dev);
864 struct adm1026_data *data = i2c_get_clientdata(client);
865 int val = simple_strtol(buf, NULL, 10);
866
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100867 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 data->fan_min[nr] = FAN_TO_REG(val, data->fan_div[nr]);
869 adm1026_write_value(client, ADM1026_REG_FAN_MIN(nr),
870 data->fan_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100871 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 return count;
873}
874
Jean Delvarecb01a232007-11-29 23:46:42 +0100875#define fan_offset(offset) \
876static SENSOR_DEVICE_ATTR(fan##offset##_input, S_IRUGO, show_fan, NULL, \
877 offset - 1); \
878static SENSOR_DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
Yani Ioannou050480f2005-06-05 10:51:46 +0200879 show_fan_min, set_fan_min, offset - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880
881fan_offset(1);
882fan_offset(2);
883fan_offset(3);
884fan_offset(4);
885fan_offset(5);
886fan_offset(6);
887fan_offset(7);
888fan_offset(8);
889
890/* Adjust fan_min to account for new fan divisor */
891static void fixup_fan_min(struct device *dev, int fan, int old_div)
892{
893 struct i2c_client *client = to_i2c_client(dev);
894 struct adm1026_data *data = i2c_get_clientdata(client);
Jean Delvarecb01a232007-11-29 23:46:42 +0100895 int new_min;
896 int new_div = data->fan_div[fan];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897
898 /* 0 and 0xff are special. Don't adjust them */
899 if (data->fan_min[fan] == 0 || data->fan_min[fan] == 0xff) {
900 return;
901 }
902
903 new_min = data->fan_min[fan] * old_div / new_div;
904 new_min = SENSORS_LIMIT(new_min, 1, 254);
905 data->fan_min[fan] = new_min;
906 adm1026_write_value(client, ADM1026_REG_FAN_MIN(fan), new_min);
907}
908
909/* Now add fan_div read/write functions */
Yani Ioannou050480f2005-06-05 10:51:46 +0200910static ssize_t show_fan_div(struct device *dev, struct device_attribute *attr,
911 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912{
Yani Ioannou050480f2005-06-05 10:51:46 +0200913 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
914 int nr = sensor_attr->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 struct adm1026_data *data = adm1026_update_device(dev);
Jean Delvarecb01a232007-11-29 23:46:42 +0100916 return sprintf(buf, "%d\n", data->fan_div[nr]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917}
Yani Ioannou050480f2005-06-05 10:51:46 +0200918static ssize_t set_fan_div(struct device *dev, struct device_attribute *attr,
919 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920{
Yani Ioannou050480f2005-06-05 10:51:46 +0200921 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
922 int nr = sensor_attr->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 struct i2c_client *client = to_i2c_client(dev);
924 struct adm1026_data *data = i2c_get_clientdata(client);
Jean Delvarecb01a232007-11-29 23:46:42 +0100925 int val, orig_div, new_div, shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926
927 val = simple_strtol(buf, NULL, 10);
Jean Delvarecb01a232007-11-29 23:46:42 +0100928 new_div = DIV_TO_REG(val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 if (new_div == 0) {
930 return -EINVAL;
931 }
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100932 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 orig_div = data->fan_div[nr];
934 data->fan_div[nr] = DIV_FROM_REG(new_div);
935
936 if (nr < 4) { /* 0 <= nr < 4 */
937 shift = 2 * nr;
938 adm1026_write_value(client, ADM1026_REG_FAN_DIV_0_3,
939 ((DIV_TO_REG(orig_div) & (~(0x03 << shift))) |
940 (new_div << shift)));
941 } else { /* 3 < nr < 8 */
942 shift = 2 * (nr - 4);
943 adm1026_write_value(client, ADM1026_REG_FAN_DIV_4_7,
944 ((DIV_TO_REG(orig_div) & (~(0x03 << (2 * shift)))) |
945 (new_div << shift)));
946 }
947
948 if (data->fan_div[nr] != orig_div) {
Jean Delvarecb01a232007-11-29 23:46:42 +0100949 fixup_fan_min(dev, nr, orig_div);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 }
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100951 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 return count;
953}
954
Jean Delvarecb01a232007-11-29 23:46:42 +0100955#define fan_offset_div(offset) \
956static SENSOR_DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR, \
Yani Ioannou050480f2005-06-05 10:51:46 +0200957 show_fan_div, set_fan_div, offset - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958
959fan_offset_div(1);
960fan_offset_div(2);
961fan_offset_div(3);
962fan_offset_div(4);
963fan_offset_div(5);
964fan_offset_div(6);
965fan_offset_div(7);
966fan_offset_div(8);
967
968/* Temps */
Yani Ioannou050480f2005-06-05 10:51:46 +0200969static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
970 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971{
Yani Ioannou050480f2005-06-05 10:51:46 +0200972 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
973 int nr = sensor_attr->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 struct adm1026_data *data = adm1026_update_device(dev);
Jean Delvarecb01a232007-11-29 23:46:42 +0100975 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976}
Yani Ioannou050480f2005-06-05 10:51:46 +0200977static ssize_t show_temp_min(struct device *dev, struct device_attribute *attr,
978 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979{
Yani Ioannou050480f2005-06-05 10:51:46 +0200980 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
981 int nr = sensor_attr->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 struct adm1026_data *data = adm1026_update_device(dev);
Jean Delvarecb01a232007-11-29 23:46:42 +0100983 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_min[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984}
Yani Ioannou050480f2005-06-05 10:51:46 +0200985static ssize_t set_temp_min(struct device *dev, struct device_attribute *attr,
986 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987{
Yani Ioannou050480f2005-06-05 10:51:46 +0200988 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
989 int nr = sensor_attr->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 struct i2c_client *client = to_i2c_client(dev);
991 struct adm1026_data *data = i2c_get_clientdata(client);
992 int val = simple_strtol(buf, NULL, 10);
993
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100994 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 data->temp_min[nr] = TEMP_TO_REG(val);
996 adm1026_write_value(client, ADM1026_REG_TEMP_MIN[nr],
997 data->temp_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100998 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 return count;
1000}
Yani Ioannou050480f2005-06-05 10:51:46 +02001001static ssize_t show_temp_max(struct device *dev, struct device_attribute *attr,
1002 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003{
Yani Ioannou050480f2005-06-05 10:51:46 +02001004 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1005 int nr = sensor_attr->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 struct adm1026_data *data = adm1026_update_device(dev);
Jean Delvarecb01a232007-11-29 23:46:42 +01001007 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_max[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008}
Yani Ioannou050480f2005-06-05 10:51:46 +02001009static ssize_t set_temp_max(struct device *dev, struct device_attribute *attr,
1010 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011{
Yani Ioannou050480f2005-06-05 10:51:46 +02001012 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1013 int nr = sensor_attr->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 struct i2c_client *client = to_i2c_client(dev);
1015 struct adm1026_data *data = i2c_get_clientdata(client);
1016 int val = simple_strtol(buf, NULL, 10);
1017
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001018 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 data->temp_max[nr] = TEMP_TO_REG(val);
1020 adm1026_write_value(client, ADM1026_REG_TEMP_MAX[nr],
1021 data->temp_max[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001022 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 return count;
1024}
Yani Ioannou050480f2005-06-05 10:51:46 +02001025
1026#define temp_reg(offset) \
Jean Delvarecb01a232007-11-29 23:46:42 +01001027static SENSOR_DEVICE_ATTR(temp##offset##_input, S_IRUGO, show_temp, \
Yani Ioannou050480f2005-06-05 10:51:46 +02001028 NULL, offset - 1); \
1029static SENSOR_DEVICE_ATTR(temp##offset##_min, S_IRUGO | S_IWUSR, \
1030 show_temp_min, set_temp_min, offset - 1); \
1031static SENSOR_DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR, \
1032 show_temp_max, set_temp_max, offset - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033
1034
1035temp_reg(1);
1036temp_reg(2);
1037temp_reg(3);
1038
Yani Ioannou050480f2005-06-05 10:51:46 +02001039static ssize_t show_temp_offset(struct device *dev,
1040 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041{
Yani Ioannou050480f2005-06-05 10:51:46 +02001042 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1043 int nr = sensor_attr->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 struct adm1026_data *data = adm1026_update_device(dev);
Jean Delvarecb01a232007-11-29 23:46:42 +01001045 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_offset[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046}
Yani Ioannou050480f2005-06-05 10:51:46 +02001047static ssize_t set_temp_offset(struct device *dev,
1048 struct device_attribute *attr, const char *buf,
1049 size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050{
Yani Ioannou050480f2005-06-05 10:51:46 +02001051 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1052 int nr = sensor_attr->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 struct i2c_client *client = to_i2c_client(dev);
1054 struct adm1026_data *data = i2c_get_clientdata(client);
1055 int val = simple_strtol(buf, NULL, 10);
1056
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001057 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 data->temp_offset[nr] = TEMP_TO_REG(val);
1059 adm1026_write_value(client, ADM1026_REG_TEMP_OFFSET[nr],
1060 data->temp_offset[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001061 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 return count;
1063}
1064
Yani Ioannou050480f2005-06-05 10:51:46 +02001065#define temp_offset_reg(offset) \
1066static SENSOR_DEVICE_ATTR(temp##offset##_offset, S_IRUGO | S_IWUSR, \
1067 show_temp_offset, set_temp_offset, offset - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068
1069temp_offset_reg(1);
1070temp_offset_reg(2);
1071temp_offset_reg(3);
1072
Yani Ioannou050480f2005-06-05 10:51:46 +02001073static ssize_t show_temp_auto_point1_temp_hyst(struct device *dev,
1074 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075{
Yani Ioannou050480f2005-06-05 10:51:46 +02001076 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1077 int nr = sensor_attr->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 struct adm1026_data *data = adm1026_update_device(dev);
Jean Delvarecb01a232007-11-29 23:46:42 +01001079 return sprintf(buf, "%d\n", TEMP_FROM_REG(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 ADM1026_FAN_ACTIVATION_TEMP_HYST + data->temp_tmin[nr]));
1081}
Yani Ioannou050480f2005-06-05 10:51:46 +02001082static ssize_t show_temp_auto_point2_temp(struct device *dev,
1083 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084{
Yani Ioannou050480f2005-06-05 10:51:46 +02001085 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1086 int nr = sensor_attr->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 struct adm1026_data *data = adm1026_update_device(dev);
Jean Delvarecb01a232007-11-29 23:46:42 +01001088 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_tmin[nr] +
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 ADM1026_FAN_CONTROL_TEMP_RANGE));
1090}
Yani Ioannou050480f2005-06-05 10:51:46 +02001091static ssize_t show_temp_auto_point1_temp(struct device *dev,
1092 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093{
Yani Ioannou050480f2005-06-05 10:51:46 +02001094 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1095 int nr = sensor_attr->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 struct adm1026_data *data = adm1026_update_device(dev);
Jean Delvarecb01a232007-11-29 23:46:42 +01001097 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_tmin[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098}
Yani Ioannou050480f2005-06-05 10:51:46 +02001099static ssize_t set_temp_auto_point1_temp(struct device *dev,
1100 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101{
Yani Ioannou050480f2005-06-05 10:51:46 +02001102 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1103 int nr = sensor_attr->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 struct i2c_client *client = to_i2c_client(dev);
1105 struct adm1026_data *data = i2c_get_clientdata(client);
1106 int val = simple_strtol(buf, NULL, 10);
1107
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001108 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 data->temp_tmin[nr] = TEMP_TO_REG(val);
1110 adm1026_write_value(client, ADM1026_REG_TEMP_TMIN[nr],
1111 data->temp_tmin[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001112 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 return count;
1114}
1115
Jean Delvarecb01a232007-11-29 23:46:42 +01001116#define temp_auto_point(offset) \
1117static SENSOR_DEVICE_ATTR(temp##offset##_auto_point1_temp, \
1118 S_IRUGO | S_IWUSR, show_temp_auto_point1_temp, \
1119 set_temp_auto_point1_temp, offset - 1); \
1120static SENSOR_DEVICE_ATTR(temp##offset##_auto_point1_temp_hyst, S_IRUGO,\
1121 show_temp_auto_point1_temp_hyst, NULL, offset - 1); \
1122static SENSOR_DEVICE_ATTR(temp##offset##_auto_point2_temp, S_IRUGO, \
Yani Ioannou050480f2005-06-05 10:51:46 +02001123 show_temp_auto_point2_temp, NULL, offset - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124
1125temp_auto_point(1);
1126temp_auto_point(2);
1127temp_auto_point(3);
1128
Yani Ioannou050480f2005-06-05 10:51:46 +02001129static ssize_t show_temp_crit_enable(struct device *dev,
1130 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131{
1132 struct adm1026_data *data = adm1026_update_device(dev);
Jean Delvarecb01a232007-11-29 23:46:42 +01001133 return sprintf(buf, "%d\n", (data->config1 & CFG1_THERM_HOT) >> 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134}
Yani Ioannou050480f2005-06-05 10:51:46 +02001135static ssize_t set_temp_crit_enable(struct device *dev,
1136 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137{
1138 struct i2c_client *client = to_i2c_client(dev);
1139 struct adm1026_data *data = i2c_get_clientdata(client);
1140 int val = simple_strtol(buf, NULL, 10);
1141
1142 if ((val == 1) || (val==0)) {
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001143 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 data->config1 = (data->config1 & ~CFG1_THERM_HOT) | (val << 4);
Jean Delvarecb01a232007-11-29 23:46:42 +01001145 adm1026_write_value(client, ADM1026_REG_CONFIG1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 data->config1);
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001147 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 }
1149 return count;
1150}
1151
Yani Ioannou050480f2005-06-05 10:51:46 +02001152#define temp_crit_enable(offset) \
1153static DEVICE_ATTR(temp##offset##_crit_enable, S_IRUGO | S_IWUSR, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 show_temp_crit_enable, set_temp_crit_enable);
1155
Yani Ioannou050480f2005-06-05 10:51:46 +02001156temp_crit_enable(1);
1157temp_crit_enable(2);
1158temp_crit_enable(3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159
Yani Ioannou050480f2005-06-05 10:51:46 +02001160static ssize_t show_temp_crit(struct device *dev,
1161 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162{
Yani Ioannou050480f2005-06-05 10:51:46 +02001163 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1164 int nr = sensor_attr->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 struct adm1026_data *data = adm1026_update_device(dev);
Jean Delvarecb01a232007-11-29 23:46:42 +01001166 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_crit[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167}
Yani Ioannou050480f2005-06-05 10:51:46 +02001168static ssize_t set_temp_crit(struct device *dev, struct device_attribute *attr,
1169 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170{
Yani Ioannou050480f2005-06-05 10:51:46 +02001171 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1172 int nr = sensor_attr->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 struct i2c_client *client = to_i2c_client(dev);
1174 struct adm1026_data *data = i2c_get_clientdata(client);
1175 int val = simple_strtol(buf, NULL, 10);
1176
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001177 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 data->temp_crit[nr] = TEMP_TO_REG(val);
1179 adm1026_write_value(client, ADM1026_REG_TEMP_THERM[nr],
1180 data->temp_crit[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001181 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 return count;
1183}
1184
Yani Ioannou050480f2005-06-05 10:51:46 +02001185#define temp_crit_reg(offset) \
1186static SENSOR_DEVICE_ATTR(temp##offset##_crit, S_IRUGO | S_IWUSR, \
1187 show_temp_crit, set_temp_crit, offset - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188
1189temp_crit_reg(1);
1190temp_crit_reg(2);
1191temp_crit_reg(3);
1192
Yani Ioannou74880c02005-05-17 06:41:12 -04001193static ssize_t show_analog_out_reg(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194{
1195 struct adm1026_data *data = adm1026_update_device(dev);
Jean Delvarecb01a232007-11-29 23:46:42 +01001196 return sprintf(buf, "%d\n", DAC_FROM_REG(data->analog_out));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197}
Yani Ioannou74880c02005-05-17 06:41:12 -04001198static ssize_t set_analog_out_reg(struct device *dev, struct device_attribute *attr, const char *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 size_t count)
1200{
1201 struct i2c_client *client = to_i2c_client(dev);
1202 struct adm1026_data *data = i2c_get_clientdata(client);
1203 int val = simple_strtol(buf, NULL, 10);
1204
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001205 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 data->analog_out = DAC_TO_REG(val);
1207 adm1026_write_value(client, ADM1026_REG_DAC, data->analog_out);
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001208 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 return count;
1210}
1211
Jean Delvarecb01a232007-11-29 23:46:42 +01001212static DEVICE_ATTR(analog_out, S_IRUGO | S_IWUSR, show_analog_out_reg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 set_analog_out_reg);
1214
Yani Ioannou74880c02005-05-17 06:41:12 -04001215static ssize_t show_vid_reg(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216{
1217 struct adm1026_data *data = adm1026_update_device(dev);
Jean Delvarecb01a232007-11-29 23:46:42 +01001218 return sprintf(buf, "%d\n", vid_from_reg(data->vid & 0x3f, data->vrm));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219}
Grant Coady937df8d2005-05-12 11:59:29 +10001220static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid_reg, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221
Yani Ioannou74880c02005-05-17 06:41:12 -04001222static ssize_t show_vrm_reg(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223{
Jean Delvare90d66192007-10-08 18:24:35 +02001224 struct adm1026_data *data = dev_get_drvdata(dev);
Jean Delvarecb01a232007-11-29 23:46:42 +01001225 return sprintf(buf, "%d\n", data->vrm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226}
Yani Ioannou74880c02005-05-17 06:41:12 -04001227static ssize_t store_vrm_reg(struct device *dev, struct device_attribute *attr, const char *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 size_t count)
1229{
Jean Delvaref67fdab2007-12-01 11:24:17 +01001230 struct adm1026_data *data = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231
1232 data->vrm = simple_strtol(buf, NULL, 10);
1233 return count;
1234}
1235
1236static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm_reg, store_vrm_reg);
1237
Yani Ioannou74880c02005-05-17 06:41:12 -04001238static ssize_t show_alarms_reg(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239{
1240 struct adm1026_data *data = adm1026_update_device(dev);
Jean Delvaref67fdab2007-12-01 11:24:17 +01001241 return sprintf(buf, "%ld\n", data->alarms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242}
1243
1244static DEVICE_ATTR(alarms, S_IRUGO, show_alarms_reg, NULL);
1245
Jean Delvarea9273cb2007-11-29 23:45:22 +01001246static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
1247 char *buf)
1248{
1249 struct adm1026_data *data = adm1026_update_device(dev);
1250 int bitnr = to_sensor_dev_attr(attr)->index;
1251 return sprintf(buf, "%ld\n", (data->alarms >> bitnr) & 1);
1252}
1253
1254static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 0);
1255static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, 1);
1256static SENSOR_DEVICE_ATTR(in9_alarm, S_IRUGO, show_alarm, NULL, 1);
1257static SENSOR_DEVICE_ATTR(in11_alarm, S_IRUGO, show_alarm, NULL, 2);
1258static SENSOR_DEVICE_ATTR(in12_alarm, S_IRUGO, show_alarm, NULL, 3);
1259static SENSOR_DEVICE_ATTR(in13_alarm, S_IRUGO, show_alarm, NULL, 4);
1260static SENSOR_DEVICE_ATTR(in14_alarm, S_IRUGO, show_alarm, NULL, 5);
1261static SENSOR_DEVICE_ATTR(in15_alarm, S_IRUGO, show_alarm, NULL, 6);
1262static SENSOR_DEVICE_ATTR(in16_alarm, S_IRUGO, show_alarm, NULL, 7);
1263static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 8);
1264static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 9);
1265static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 10);
1266static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 11);
1267static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 12);
1268static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 13);
1269static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 14);
1270static SENSOR_DEVICE_ATTR(in7_alarm, S_IRUGO, show_alarm, NULL, 15);
1271static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 16);
1272static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 17);
1273static SENSOR_DEVICE_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL, 18);
1274static SENSOR_DEVICE_ATTR(fan4_alarm, S_IRUGO, show_alarm, NULL, 19);
1275static SENSOR_DEVICE_ATTR(fan5_alarm, S_IRUGO, show_alarm, NULL, 20);
1276static SENSOR_DEVICE_ATTR(fan6_alarm, S_IRUGO, show_alarm, NULL, 21);
1277static SENSOR_DEVICE_ATTR(fan7_alarm, S_IRUGO, show_alarm, NULL, 22);
1278static SENSOR_DEVICE_ATTR(fan8_alarm, S_IRUGO, show_alarm, NULL, 23);
1279static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 24);
1280static SENSOR_DEVICE_ATTR(in10_alarm, S_IRUGO, show_alarm, NULL, 25);
1281static SENSOR_DEVICE_ATTR(in8_alarm, S_IRUGO, show_alarm, NULL, 26);
1282
Yani Ioannou74880c02005-05-17 06:41:12 -04001283static ssize_t show_alarm_mask(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284{
1285 struct adm1026_data *data = adm1026_update_device(dev);
Jean Delvarecb01a232007-11-29 23:46:42 +01001286 return sprintf(buf, "%ld\n", data->alarm_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287}
Yani Ioannou74880c02005-05-17 06:41:12 -04001288static ssize_t set_alarm_mask(struct device *dev, struct device_attribute *attr, const char *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 size_t count)
1290{
1291 struct i2c_client *client = to_i2c_client(dev);
1292 struct adm1026_data *data = i2c_get_clientdata(client);
1293 int val = simple_strtol(buf, NULL, 10);
1294 unsigned long mask;
1295
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001296 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 data->alarm_mask = val & 0x7fffffff;
1298 mask = data->alarm_mask
1299 | (data->gpio_mask & 0x10000 ? 0x80000000 : 0);
1300 adm1026_write_value(client, ADM1026_REG_MASK1,
1301 mask & 0xff);
1302 mask >>= 8;
1303 adm1026_write_value(client, ADM1026_REG_MASK2,
1304 mask & 0xff);
1305 mask >>= 8;
1306 adm1026_write_value(client, ADM1026_REG_MASK3,
1307 mask & 0xff);
1308 mask >>= 8;
1309 adm1026_write_value(client, ADM1026_REG_MASK4,
1310 mask & 0xff);
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001311 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 return count;
1313}
1314
1315static DEVICE_ATTR(alarm_mask, S_IRUGO | S_IWUSR, show_alarm_mask,
1316 set_alarm_mask);
1317
1318
Yani Ioannou74880c02005-05-17 06:41:12 -04001319static ssize_t show_gpio(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320{
1321 struct adm1026_data *data = adm1026_update_device(dev);
Jean Delvarecb01a232007-11-29 23:46:42 +01001322 return sprintf(buf, "%ld\n", data->gpio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323}
Yani Ioannou74880c02005-05-17 06:41:12 -04001324static ssize_t set_gpio(struct device *dev, struct device_attribute *attr, const char *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 size_t count)
1326{
1327 struct i2c_client *client = to_i2c_client(dev);
1328 struct adm1026_data *data = i2c_get_clientdata(client);
1329 int val = simple_strtol(buf, NULL, 10);
Jean Delvarecb01a232007-11-29 23:46:42 +01001330 long gpio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001332 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 data->gpio = val & 0x1ffff;
1334 gpio = data->gpio;
Jean Delvarecb01a232007-11-29 23:46:42 +01001335 adm1026_write_value(client, ADM1026_REG_GPIO_STATUS_0_7, gpio & 0xff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 gpio >>= 8;
Jean Delvarecb01a232007-11-29 23:46:42 +01001337 adm1026_write_value(client, ADM1026_REG_GPIO_STATUS_8_15, gpio & 0xff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 gpio = ((gpio >> 1) & 0x80) | (data->alarms >> 24 & 0x7f);
Jean Delvarecb01a232007-11-29 23:46:42 +01001339 adm1026_write_value(client, ADM1026_REG_STATUS4, gpio & 0xff);
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001340 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 return count;
1342}
1343
1344static DEVICE_ATTR(gpio, S_IRUGO | S_IWUSR, show_gpio, set_gpio);
1345
1346
Yani Ioannou74880c02005-05-17 06:41:12 -04001347static ssize_t show_gpio_mask(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348{
1349 struct adm1026_data *data = adm1026_update_device(dev);
Jean Delvarecb01a232007-11-29 23:46:42 +01001350 return sprintf(buf, "%ld\n", data->gpio_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351}
Yani Ioannou74880c02005-05-17 06:41:12 -04001352static ssize_t set_gpio_mask(struct device *dev, struct device_attribute *attr, const char *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 size_t count)
1354{
1355 struct i2c_client *client = to_i2c_client(dev);
1356 struct adm1026_data *data = i2c_get_clientdata(client);
1357 int val = simple_strtol(buf, NULL, 10);
Jean Delvarecb01a232007-11-29 23:46:42 +01001358 long mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001360 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 data->gpio_mask = val & 0x1ffff;
1362 mask = data->gpio_mask;
Jean Delvarecb01a232007-11-29 23:46:42 +01001363 adm1026_write_value(client, ADM1026_REG_GPIO_MASK_0_7, mask & 0xff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 mask >>= 8;
Jean Delvarecb01a232007-11-29 23:46:42 +01001365 adm1026_write_value(client, ADM1026_REG_GPIO_MASK_8_15, mask & 0xff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 mask = ((mask >> 1) & 0x80) | (data->alarm_mask >> 24 & 0x7f);
Jean Delvarecb01a232007-11-29 23:46:42 +01001367 adm1026_write_value(client, ADM1026_REG_MASK1, mask & 0xff);
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001368 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 return count;
1370}
1371
1372static DEVICE_ATTR(gpio_mask, S_IRUGO | S_IWUSR, show_gpio_mask, set_gpio_mask);
1373
Yani Ioannou74880c02005-05-17 06:41:12 -04001374static ssize_t show_pwm_reg(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375{
1376 struct adm1026_data *data = adm1026_update_device(dev);
Jean Delvarecb01a232007-11-29 23:46:42 +01001377 return sprintf(buf, "%d\n", PWM_FROM_REG(data->pwm1.pwm));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378}
Yani Ioannou74880c02005-05-17 06:41:12 -04001379static ssize_t set_pwm_reg(struct device *dev, struct device_attribute *attr, const char *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 size_t count)
1381{
1382 struct i2c_client *client = to_i2c_client(dev);
1383 struct adm1026_data *data = i2c_get_clientdata(client);
1384
1385 if (data->pwm1.enable == 1) {
1386 int val = simple_strtol(buf, NULL, 10);
1387
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001388 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 data->pwm1.pwm = PWM_TO_REG(val);
1390 adm1026_write_value(client, ADM1026_REG_PWM, data->pwm1.pwm);
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001391 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 }
1393 return count;
1394}
Yani Ioannou74880c02005-05-17 06:41:12 -04001395static ssize_t show_auto_pwm_min(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396{
1397 struct adm1026_data *data = adm1026_update_device(dev);
Jean Delvarecb01a232007-11-29 23:46:42 +01001398 return sprintf(buf, "%d\n", data->pwm1.auto_pwm_min);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399}
Yani Ioannou74880c02005-05-17 06:41:12 -04001400static ssize_t set_auto_pwm_min(struct device *dev, struct device_attribute *attr, const char *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 size_t count)
1402{
1403 struct i2c_client *client = to_i2c_client(dev);
1404 struct adm1026_data *data = i2c_get_clientdata(client);
1405 int val = simple_strtol(buf, NULL, 10);
1406
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001407 mutex_lock(&data->update_lock);
Jean Delvarecb01a232007-11-29 23:46:42 +01001408 data->pwm1.auto_pwm_min = SENSORS_LIMIT(val, 0, 255);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 if (data->pwm1.enable == 2) { /* apply immediately */
1410 data->pwm1.pwm = PWM_TO_REG((data->pwm1.pwm & 0x0f) |
Jean Delvarecb01a232007-11-29 23:46:42 +01001411 PWM_MIN_TO_REG(data->pwm1.auto_pwm_min));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 adm1026_write_value(client, ADM1026_REG_PWM, data->pwm1.pwm);
1413 }
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001414 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 return count;
1416}
Yani Ioannou74880c02005-05-17 06:41:12 -04001417static ssize_t show_auto_pwm_max(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418{
Jean Delvarecb01a232007-11-29 23:46:42 +01001419 return sprintf(buf, "%d\n", ADM1026_PWM_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420}
Yani Ioannou74880c02005-05-17 06:41:12 -04001421static ssize_t show_pwm_enable(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422{
1423 struct adm1026_data *data = adm1026_update_device(dev);
Jean Delvarecb01a232007-11-29 23:46:42 +01001424 return sprintf(buf, "%d\n", data->pwm1.enable);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425}
Yani Ioannou74880c02005-05-17 06:41:12 -04001426static ssize_t set_pwm_enable(struct device *dev, struct device_attribute *attr, const char *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 size_t count)
1428{
1429 struct i2c_client *client = to_i2c_client(dev);
1430 struct adm1026_data *data = i2c_get_clientdata(client);
1431 int val = simple_strtol(buf, NULL, 10);
Jean Delvarecb01a232007-11-29 23:46:42 +01001432 int old_enable;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433
1434 if ((val >= 0) && (val < 3)) {
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001435 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 old_enable = data->pwm1.enable;
1437 data->pwm1.enable = val;
1438 data->config1 = (data->config1 & ~CFG1_PWM_AFC)
1439 | ((val == 2) ? CFG1_PWM_AFC : 0);
1440 adm1026_write_value(client, ADM1026_REG_CONFIG1,
1441 data->config1);
Jean Delvarecb01a232007-11-29 23:46:42 +01001442 if (val == 2) { /* apply pwm1_auto_pwm_min to pwm1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 data->pwm1.pwm = PWM_TO_REG((data->pwm1.pwm & 0x0f) |
Jean Delvarecb01a232007-11-29 23:46:42 +01001444 PWM_MIN_TO_REG(data->pwm1.auto_pwm_min));
1445 adm1026_write_value(client, ADM1026_REG_PWM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 data->pwm1.pwm);
1447 } else if (!((old_enable == 1) && (val == 1))) {
1448 /* set pwm to safe value */
1449 data->pwm1.pwm = 255;
Jean Delvarecb01a232007-11-29 23:46:42 +01001450 adm1026_write_value(client, ADM1026_REG_PWM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 data->pwm1.pwm);
1452 }
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001453 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 }
1455 return count;
1456}
1457
1458/* enable PWM fan control */
Jean Delvarecb01a232007-11-29 23:46:42 +01001459static DEVICE_ATTR(pwm1, S_IRUGO | S_IWUSR, show_pwm_reg, set_pwm_reg);
1460static DEVICE_ATTR(pwm2, S_IRUGO | S_IWUSR, show_pwm_reg, set_pwm_reg);
1461static DEVICE_ATTR(pwm3, S_IRUGO | S_IWUSR, show_pwm_reg, set_pwm_reg);
1462static DEVICE_ATTR(pwm1_enable, S_IRUGO | S_IWUSR, show_pwm_enable,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 set_pwm_enable);
Jean Delvarecb01a232007-11-29 23:46:42 +01001464static DEVICE_ATTR(pwm2_enable, S_IRUGO | S_IWUSR, show_pwm_enable,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 set_pwm_enable);
Jean Delvarecb01a232007-11-29 23:46:42 +01001466static DEVICE_ATTR(pwm3_enable, S_IRUGO | S_IWUSR, show_pwm_enable,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 set_pwm_enable);
Jean Delvarecb01a232007-11-29 23:46:42 +01001468static DEVICE_ATTR(temp1_auto_point1_pwm, S_IRUGO | S_IWUSR,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 show_auto_pwm_min, set_auto_pwm_min);
Jean Delvarecb01a232007-11-29 23:46:42 +01001470static DEVICE_ATTR(temp2_auto_point1_pwm, S_IRUGO | S_IWUSR,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 show_auto_pwm_min, set_auto_pwm_min);
Jean Delvarecb01a232007-11-29 23:46:42 +01001472static DEVICE_ATTR(temp3_auto_point1_pwm, S_IRUGO | S_IWUSR,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 show_auto_pwm_min, set_auto_pwm_min);
1474
1475static DEVICE_ATTR(temp1_auto_point2_pwm, S_IRUGO, show_auto_pwm_max, NULL);
1476static DEVICE_ATTR(temp2_auto_point2_pwm, S_IRUGO, show_auto_pwm_max, NULL);
1477static DEVICE_ATTR(temp3_auto_point2_pwm, S_IRUGO, show_auto_pwm_max, NULL);
1478
Mark M. Hoffman681c6f72006-09-24 21:15:35 +02001479static struct attribute *adm1026_attributes[] = {
1480 &sensor_dev_attr_in0_input.dev_attr.attr,
1481 &sensor_dev_attr_in0_max.dev_attr.attr,
1482 &sensor_dev_attr_in0_min.dev_attr.attr,
Jean Delvarea9273cb2007-11-29 23:45:22 +01001483 &sensor_dev_attr_in0_alarm.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +02001484 &sensor_dev_attr_in1_input.dev_attr.attr,
1485 &sensor_dev_attr_in1_max.dev_attr.attr,
1486 &sensor_dev_attr_in1_min.dev_attr.attr,
Jean Delvarea9273cb2007-11-29 23:45:22 +01001487 &sensor_dev_attr_in1_alarm.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +02001488 &sensor_dev_attr_in2_input.dev_attr.attr,
1489 &sensor_dev_attr_in2_max.dev_attr.attr,
1490 &sensor_dev_attr_in2_min.dev_attr.attr,
Jean Delvarea9273cb2007-11-29 23:45:22 +01001491 &sensor_dev_attr_in2_alarm.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +02001492 &sensor_dev_attr_in3_input.dev_attr.attr,
1493 &sensor_dev_attr_in3_max.dev_attr.attr,
1494 &sensor_dev_attr_in3_min.dev_attr.attr,
Jean Delvarea9273cb2007-11-29 23:45:22 +01001495 &sensor_dev_attr_in3_alarm.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +02001496 &sensor_dev_attr_in4_input.dev_attr.attr,
1497 &sensor_dev_attr_in4_max.dev_attr.attr,
1498 &sensor_dev_attr_in4_min.dev_attr.attr,
Jean Delvarea9273cb2007-11-29 23:45:22 +01001499 &sensor_dev_attr_in4_alarm.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +02001500 &sensor_dev_attr_in5_input.dev_attr.attr,
1501 &sensor_dev_attr_in5_max.dev_attr.attr,
1502 &sensor_dev_attr_in5_min.dev_attr.attr,
Jean Delvarea9273cb2007-11-29 23:45:22 +01001503 &sensor_dev_attr_in5_alarm.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +02001504 &sensor_dev_attr_in6_input.dev_attr.attr,
1505 &sensor_dev_attr_in6_max.dev_attr.attr,
1506 &sensor_dev_attr_in6_min.dev_attr.attr,
Jean Delvarea9273cb2007-11-29 23:45:22 +01001507 &sensor_dev_attr_in6_alarm.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +02001508 &sensor_dev_attr_in7_input.dev_attr.attr,
1509 &sensor_dev_attr_in7_max.dev_attr.attr,
1510 &sensor_dev_attr_in7_min.dev_attr.attr,
Jean Delvarea9273cb2007-11-29 23:45:22 +01001511 &sensor_dev_attr_in7_alarm.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +02001512 &sensor_dev_attr_in10_input.dev_attr.attr,
1513 &sensor_dev_attr_in10_max.dev_attr.attr,
1514 &sensor_dev_attr_in10_min.dev_attr.attr,
Jean Delvarea9273cb2007-11-29 23:45:22 +01001515 &sensor_dev_attr_in10_alarm.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +02001516 &sensor_dev_attr_in11_input.dev_attr.attr,
1517 &sensor_dev_attr_in11_max.dev_attr.attr,
1518 &sensor_dev_attr_in11_min.dev_attr.attr,
Jean Delvarea9273cb2007-11-29 23:45:22 +01001519 &sensor_dev_attr_in11_alarm.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +02001520 &sensor_dev_attr_in12_input.dev_attr.attr,
1521 &sensor_dev_attr_in12_max.dev_attr.attr,
1522 &sensor_dev_attr_in12_min.dev_attr.attr,
Jean Delvarea9273cb2007-11-29 23:45:22 +01001523 &sensor_dev_attr_in12_alarm.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +02001524 &sensor_dev_attr_in13_input.dev_attr.attr,
1525 &sensor_dev_attr_in13_max.dev_attr.attr,
1526 &sensor_dev_attr_in13_min.dev_attr.attr,
Jean Delvarea9273cb2007-11-29 23:45:22 +01001527 &sensor_dev_attr_in13_alarm.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +02001528 &sensor_dev_attr_in14_input.dev_attr.attr,
1529 &sensor_dev_attr_in14_max.dev_attr.attr,
1530 &sensor_dev_attr_in14_min.dev_attr.attr,
Jean Delvarea9273cb2007-11-29 23:45:22 +01001531 &sensor_dev_attr_in14_alarm.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +02001532 &sensor_dev_attr_in15_input.dev_attr.attr,
1533 &sensor_dev_attr_in15_max.dev_attr.attr,
1534 &sensor_dev_attr_in15_min.dev_attr.attr,
Jean Delvarea9273cb2007-11-29 23:45:22 +01001535 &sensor_dev_attr_in15_alarm.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +02001536 &sensor_dev_attr_in16_input.dev_attr.attr,
1537 &sensor_dev_attr_in16_max.dev_attr.attr,
1538 &sensor_dev_attr_in16_min.dev_attr.attr,
Jean Delvarea9273cb2007-11-29 23:45:22 +01001539 &sensor_dev_attr_in16_alarm.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +02001540 &sensor_dev_attr_fan1_input.dev_attr.attr,
1541 &sensor_dev_attr_fan1_div.dev_attr.attr,
1542 &sensor_dev_attr_fan1_min.dev_attr.attr,
Jean Delvarea9273cb2007-11-29 23:45:22 +01001543 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +02001544 &sensor_dev_attr_fan2_input.dev_attr.attr,
1545 &sensor_dev_attr_fan2_div.dev_attr.attr,
1546 &sensor_dev_attr_fan2_min.dev_attr.attr,
Jean Delvarea9273cb2007-11-29 23:45:22 +01001547 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +02001548 &sensor_dev_attr_fan3_input.dev_attr.attr,
1549 &sensor_dev_attr_fan3_div.dev_attr.attr,
1550 &sensor_dev_attr_fan3_min.dev_attr.attr,
Jean Delvarea9273cb2007-11-29 23:45:22 +01001551 &sensor_dev_attr_fan3_alarm.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +02001552 &sensor_dev_attr_fan4_input.dev_attr.attr,
1553 &sensor_dev_attr_fan4_div.dev_attr.attr,
1554 &sensor_dev_attr_fan4_min.dev_attr.attr,
Jean Delvarea9273cb2007-11-29 23:45:22 +01001555 &sensor_dev_attr_fan4_alarm.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +02001556 &sensor_dev_attr_fan5_input.dev_attr.attr,
1557 &sensor_dev_attr_fan5_div.dev_attr.attr,
1558 &sensor_dev_attr_fan5_min.dev_attr.attr,
Jean Delvarea9273cb2007-11-29 23:45:22 +01001559 &sensor_dev_attr_fan5_alarm.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +02001560 &sensor_dev_attr_fan6_input.dev_attr.attr,
1561 &sensor_dev_attr_fan6_div.dev_attr.attr,
1562 &sensor_dev_attr_fan6_min.dev_attr.attr,
Jean Delvarea9273cb2007-11-29 23:45:22 +01001563 &sensor_dev_attr_fan6_alarm.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +02001564 &sensor_dev_attr_fan7_input.dev_attr.attr,
1565 &sensor_dev_attr_fan7_div.dev_attr.attr,
1566 &sensor_dev_attr_fan7_min.dev_attr.attr,
Jean Delvarea9273cb2007-11-29 23:45:22 +01001567 &sensor_dev_attr_fan7_alarm.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +02001568 &sensor_dev_attr_fan8_input.dev_attr.attr,
1569 &sensor_dev_attr_fan8_div.dev_attr.attr,
1570 &sensor_dev_attr_fan8_min.dev_attr.attr,
Jean Delvarea9273cb2007-11-29 23:45:22 +01001571 &sensor_dev_attr_fan8_alarm.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +02001572 &sensor_dev_attr_temp1_input.dev_attr.attr,
1573 &sensor_dev_attr_temp1_max.dev_attr.attr,
1574 &sensor_dev_attr_temp1_min.dev_attr.attr,
Jean Delvarea9273cb2007-11-29 23:45:22 +01001575 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +02001576 &sensor_dev_attr_temp2_input.dev_attr.attr,
1577 &sensor_dev_attr_temp2_max.dev_attr.attr,
1578 &sensor_dev_attr_temp2_min.dev_attr.attr,
Jean Delvarea9273cb2007-11-29 23:45:22 +01001579 &sensor_dev_attr_temp2_alarm.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +02001580 &sensor_dev_attr_temp1_offset.dev_attr.attr,
1581 &sensor_dev_attr_temp2_offset.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +02001582 &sensor_dev_attr_temp1_auto_point1_temp.dev_attr.attr,
1583 &sensor_dev_attr_temp2_auto_point1_temp.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +02001584 &sensor_dev_attr_temp1_auto_point1_temp_hyst.dev_attr.attr,
1585 &sensor_dev_attr_temp2_auto_point1_temp_hyst.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +02001586 &sensor_dev_attr_temp1_auto_point2_temp.dev_attr.attr,
1587 &sensor_dev_attr_temp2_auto_point2_temp.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +02001588 &sensor_dev_attr_temp1_crit.dev_attr.attr,
1589 &sensor_dev_attr_temp2_crit.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +02001590 &dev_attr_temp1_crit_enable.attr,
1591 &dev_attr_temp2_crit_enable.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +02001592 &dev_attr_cpu0_vid.attr,
1593 &dev_attr_vrm.attr,
1594 &dev_attr_alarms.attr,
1595 &dev_attr_alarm_mask.attr,
1596 &dev_attr_gpio.attr,
1597 &dev_attr_gpio_mask.attr,
1598 &dev_attr_pwm1.attr,
1599 &dev_attr_pwm2.attr,
1600 &dev_attr_pwm3.attr,
1601 &dev_attr_pwm1_enable.attr,
1602 &dev_attr_pwm2_enable.attr,
1603 &dev_attr_pwm3_enable.attr,
1604 &dev_attr_temp1_auto_point1_pwm.attr,
1605 &dev_attr_temp2_auto_point1_pwm.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +02001606 &dev_attr_temp1_auto_point2_pwm.attr,
1607 &dev_attr_temp2_auto_point2_pwm.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +02001608 &dev_attr_analog_out.attr,
1609 NULL
1610};
1611
1612static const struct attribute_group adm1026_group = {
1613 .attrs = adm1026_attributes,
1614};
1615
Jean Delvare5b34dbc2007-11-29 23:47:54 +01001616static struct attribute *adm1026_attributes_temp3[] = {
1617 &sensor_dev_attr_temp3_input.dev_attr.attr,
1618 &sensor_dev_attr_temp3_max.dev_attr.attr,
1619 &sensor_dev_attr_temp3_min.dev_attr.attr,
1620 &sensor_dev_attr_temp3_alarm.dev_attr.attr,
1621 &sensor_dev_attr_temp3_offset.dev_attr.attr,
1622 &sensor_dev_attr_temp3_auto_point1_temp.dev_attr.attr,
1623 &sensor_dev_attr_temp3_auto_point1_temp_hyst.dev_attr.attr,
1624 &sensor_dev_attr_temp3_auto_point2_temp.dev_attr.attr,
1625 &sensor_dev_attr_temp3_crit.dev_attr.attr,
1626 &dev_attr_temp3_crit_enable.attr,
1627 &dev_attr_temp3_auto_point1_pwm.attr,
1628 &dev_attr_temp3_auto_point2_pwm.attr,
Jean Delvare1d5f2c12008-02-10 19:01:15 +01001629 NULL
Jean Delvare5b34dbc2007-11-29 23:47:54 +01001630};
1631
1632static const struct attribute_group adm1026_group_temp3 = {
1633 .attrs = adm1026_attributes_temp3,
1634};
1635
1636static struct attribute *adm1026_attributes_in8_9[] = {
1637 &sensor_dev_attr_in8_input.dev_attr.attr,
1638 &sensor_dev_attr_in8_max.dev_attr.attr,
1639 &sensor_dev_attr_in8_min.dev_attr.attr,
1640 &sensor_dev_attr_in8_alarm.dev_attr.attr,
1641 &sensor_dev_attr_in9_input.dev_attr.attr,
1642 &sensor_dev_attr_in9_max.dev_attr.attr,
1643 &sensor_dev_attr_in9_min.dev_attr.attr,
1644 &sensor_dev_attr_in9_alarm.dev_attr.attr,
Jean Delvare1d5f2c12008-02-10 19:01:15 +01001645 NULL
Jean Delvare5b34dbc2007-11-29 23:47:54 +01001646};
1647
1648static const struct attribute_group adm1026_group_in8_9 = {
1649 .attrs = adm1026_attributes_in8_9,
1650};
1651
Jean Delvare57f7eb02008-07-16 19:30:08 +02001652/* Return 0 if detection is successful, -ENODEV otherwise */
1653static int adm1026_detect(struct i2c_client *client, int kind,
1654 struct i2c_board_info *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655{
Jean Delvare57f7eb02008-07-16 19:30:08 +02001656 struct i2c_adapter *adapter = client->adapter;
1657 int address = client->addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 int company, verstep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659
1660 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
1661 /* We need to be able to do byte I/O */
Jean Delvare57f7eb02008-07-16 19:30:08 +02001662 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 };
1664
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 /* Now, we do the remaining detection. */
1666
Jean Delvaref67fdab2007-12-01 11:24:17 +01001667 company = adm1026_read_value(client, ADM1026_REG_COMPANY);
1668 verstep = adm1026_read_value(client, ADM1026_REG_VERSTEP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669
Jean Delvare57f7eb02008-07-16 19:30:08 +02001670 dev_dbg(&adapter->dev, "Detecting device at %d,0x%02x with"
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 " COMPANY: 0x%02x and VERSTEP: 0x%02x\n",
Jean Delvaref67fdab2007-12-01 11:24:17 +01001672 i2c_adapter_id(client->adapter), client->addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 company, verstep);
1674
1675 /* If auto-detecting, Determine the chip type. */
1676 if (kind <= 0) {
Jean Delvare57f7eb02008-07-16 19:30:08 +02001677 dev_dbg(&adapter->dev, "Autodetecting device at %d,0x%02x "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 "...\n", i2c_adapter_id(adapter), address);
1679 if (company == ADM1026_COMPANY_ANALOG_DEV
1680 && verstep == ADM1026_VERSTEP_ADM1026) {
1681 kind = adm1026;
1682 } else if (company == ADM1026_COMPANY_ANALOG_DEV
1683 && (verstep & 0xf0) == ADM1026_VERSTEP_GENERIC) {
1684 dev_err(&adapter->dev, ": Unrecognized stepping "
1685 "0x%02x. Defaulting to ADM1026.\n", verstep);
1686 kind = adm1026;
1687 } else if ((verstep & 0xf0) == ADM1026_VERSTEP_GENERIC) {
1688 dev_err(&adapter->dev, ": Found version/stepping "
1689 "0x%02x. Assuming generic ADM1026.\n",
1690 verstep);
1691 kind = any_chip;
1692 } else {
Jean Delvare57f7eb02008-07-16 19:30:08 +02001693 dev_dbg(&adapter->dev, ": Autodetection "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 "failed\n");
1695 /* Not an ADM1026 ... */
Jean Delvarecb01a232007-11-29 23:46:42 +01001696 if (kind == 0) { /* User used force=x,y */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 dev_err(&adapter->dev, "Generic ADM1026 not "
1698 "found at %d,0x%02x. Try "
1699 "force_adm1026.\n",
1700 i2c_adapter_id(adapter), address);
1701 }
Jean Delvare57f7eb02008-07-16 19:30:08 +02001702 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 }
1704 }
Jean Delvare57f7eb02008-07-16 19:30:08 +02001705 strlcpy(info->type, "adm1026", I2C_NAME_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706
Jean Delvare57f7eb02008-07-16 19:30:08 +02001707 return 0;
1708}
1709
1710static int adm1026_probe(struct i2c_client *client,
1711 const struct i2c_device_id *id)
1712{
1713 struct adm1026_data *data;
1714 int err;
1715
1716 data = kzalloc(sizeof(struct adm1026_data), GFP_KERNEL);
1717 if (!data) {
1718 err = -ENOMEM;
1719 goto exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721
Jean Delvare57f7eb02008-07-16 19:30:08 +02001722 i2c_set_clientdata(client, data);
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001723 mutex_init(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 /* Set the VRM version */
Jean Delvare303760b2005-07-31 21:52:01 +02001726 data->vrm = vid_which_vrm();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727
1728 /* Initialize the ADM1026 chip */
Jean Delvaref67fdab2007-12-01 11:24:17 +01001729 adm1026_init_client(client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730
1731 /* Register sysfs hooks */
Jean Delvaref67fdab2007-12-01 11:24:17 +01001732 if ((err = sysfs_create_group(&client->dev.kobj, &adm1026_group)))
Jean Delvare57f7eb02008-07-16 19:30:08 +02001733 goto exitfree;
Jean Delvare5b34dbc2007-11-29 23:47:54 +01001734 if (data->config1 & CFG1_AIN8_9)
1735 err = sysfs_create_group(&client->dev.kobj,
1736 &adm1026_group_in8_9);
1737 else
1738 err = sysfs_create_group(&client->dev.kobj,
1739 &adm1026_group_temp3);
1740 if (err)
1741 goto exitremove;
Mark M. Hoffman681c6f72006-09-24 21:15:35 +02001742
Jean Delvaref67fdab2007-12-01 11:24:17 +01001743 data->hwmon_dev = hwmon_device_register(&client->dev);
Tony Jones1beeffe2007-08-20 13:46:20 -07001744 if (IS_ERR(data->hwmon_dev)) {
1745 err = PTR_ERR(data->hwmon_dev);
Mark M. Hoffman681c6f72006-09-24 21:15:35 +02001746 goto exitremove;
Mark M. Hoffman943b0832005-07-15 21:39:18 -04001747 }
1748
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 return 0;
1750
1751 /* Error out and cleanup code */
Mark M. Hoffman681c6f72006-09-24 21:15:35 +02001752exitremove:
Jean Delvaref67fdab2007-12-01 11:24:17 +01001753 sysfs_remove_group(&client->dev.kobj, &adm1026_group);
Jean Delvare5b34dbc2007-11-29 23:47:54 +01001754 if (data->config1 & CFG1_AIN8_9)
1755 sysfs_remove_group(&client->dev.kobj, &adm1026_group_in8_9);
1756 else
1757 sysfs_remove_group(&client->dev.kobj, &adm1026_group_temp3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758exitfree:
Alexey Dobriyan1f57ff82005-08-26 01:49:14 +04001759 kfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760exit:
1761 return err;
1762}
Mark M. Hoffman681c6f72006-09-24 21:15:35 +02001763
Jean Delvare57f7eb02008-07-16 19:30:08 +02001764static int adm1026_remove(struct i2c_client *client)
Mark M. Hoffman681c6f72006-09-24 21:15:35 +02001765{
1766 struct adm1026_data *data = i2c_get_clientdata(client);
Tony Jones1beeffe2007-08-20 13:46:20 -07001767 hwmon_device_unregister(data->hwmon_dev);
Mark M. Hoffman681c6f72006-09-24 21:15:35 +02001768 sysfs_remove_group(&client->dev.kobj, &adm1026_group);
Jean Delvare5b34dbc2007-11-29 23:47:54 +01001769 if (data->config1 & CFG1_AIN8_9)
1770 sysfs_remove_group(&client->dev.kobj, &adm1026_group_in8_9);
1771 else
1772 sysfs_remove_group(&client->dev.kobj, &adm1026_group_temp3);
Mark M. Hoffman681c6f72006-09-24 21:15:35 +02001773 kfree(data);
1774 return 0;
1775}
1776
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777static int __init sm_adm1026_init(void)
1778{
1779 return i2c_add_driver(&adm1026_driver);
1780}
1781
Jean Delvarecb01a232007-11-29 23:46:42 +01001782static void __exit sm_adm1026_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783{
1784 i2c_del_driver(&adm1026_driver);
1785}
1786
1787MODULE_LICENSE("GPL");
1788MODULE_AUTHOR("Philip Pokorny <ppokorny@penguincomputing.com>, "
Jean Delvarecb01a232007-11-29 23:46:42 +01001789 "Justin Thiessen <jthiessen@penguincomputing.com>");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790MODULE_DESCRIPTION("ADM1026 driver");
1791
1792module_init(sm_adm1026_init);
1793module_exit(sm_adm1026_exit);