blob: 9f44ed7ebb7be99f98494693ad8ea5c97abc63a3 [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 */
38static 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
235/* Typically used with systems using a v9.1 VRM spec ? */
236#define ADM1026_INIT_VRM 91
237
238/* Chip sampling rates
239 *
240 * Some sensors are not updated more frequently than once per second
241 * so it doesn't make sense to read them more often than that.
242 * We cache the results and return the saved data if the driver
243 * is called again before a second has elapsed.
244 *
245 * Also, there is significant configuration data for this chip
246 * So, we keep the config data up to date in the cache
247 * when it is written and only sample it once every 5 *minutes*
248 */
Jean Delvarecb01a232007-11-29 23:46:42 +0100249#define ADM1026_DATA_INTERVAL (1 * HZ)
250#define ADM1026_CONFIG_INTERVAL (5 * 60 * HZ)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
252/* We allow for multiple chips in a single system.
253 *
254 * For each registered ADM1026, we need to keep state information
255 * at client->data. The adm1026_data structure is dynamically
256 * allocated, when a new client structure is allocated. */
257
258struct pwm_data {
259 u8 pwm;
260 u8 enable;
261 u8 auto_pwm_min;
262};
263
264struct adm1026_data {
265 struct i2c_client client;
Tony Jones1beeffe2007-08-20 13:46:20 -0700266 struct device *hwmon_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 enum chips type;
268
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100269 struct mutex update_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 int valid; /* !=0 if following fields are valid */
271 unsigned long last_reading; /* In jiffies */
272 unsigned long last_config; /* In jiffies */
273
Jean Delvarecb01a232007-11-29 23:46:42 +0100274 u8 in[17]; /* Register value */
275 u8 in_max[17]; /* Register value */
276 u8 in_min[17]; /* Register value */
277 s8 temp[3]; /* Register value */
278 s8 temp_min[3]; /* Register value */
279 s8 temp_max[3]; /* Register value */
280 s8 temp_tmin[3]; /* Register value */
281 s8 temp_crit[3]; /* Register value */
282 s8 temp_offset[3]; /* Register value */
283 u8 fan[8]; /* Register value */
284 u8 fan_min[8]; /* Register value */
285 u8 fan_div[8]; /* Decoded value */
286 struct pwm_data pwm1; /* Pwm control values */
287 int vid; /* Decoded value */
288 u8 vrm; /* VRM version */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 u8 analog_out; /* Register value (DAC) */
Jean Delvarecb01a232007-11-29 23:46:42 +0100290 long alarms; /* Register encoding, combined */
291 long alarm_mask; /* Register encoding, combined */
292 long gpio; /* Register encoding, combined */
293 long gpio_mask; /* Register encoding, combined */
294 u8 gpio_config[17]; /* Decoded value */
295 u8 config1; /* Register value */
296 u8 config2; /* Register value */
297 u8 config3; /* Register value */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298};
299
300static int adm1026_attach_adapter(struct i2c_adapter *adapter);
301static int adm1026_detect(struct i2c_adapter *adapter, int address,
302 int kind);
303static int adm1026_detach_client(struct i2c_client *client);
Darren Jenkinsf6c27fc2006-02-27 23:14:58 +0100304static int adm1026_read_value(struct i2c_client *client, u8 reg);
305static int adm1026_write_value(struct i2c_client *client, u8 reg, int value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306static void adm1026_print_gpio(struct i2c_client *client);
Jean Delvarecb01a232007-11-29 23:46:42 +0100307static void adm1026_fixup_gpio(struct i2c_client *client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308static struct adm1026_data *adm1026_update_device(struct device *dev);
309static void adm1026_init_client(struct i2c_client *client);
310
311
312static struct i2c_driver adm1026_driver = {
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100313 .driver = {
Laurent Riffardcdaf7932005-11-26 20:37:41 +0100314 .name = "adm1026",
315 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 .attach_adapter = adm1026_attach_adapter,
Jean Delvarecb01a232007-11-29 23:46:42 +0100317 .detach_client = adm1026_detach_client,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318};
319
Ben Dooksc49efce2005-10-26 21:07:25 +0200320static int adm1026_attach_adapter(struct i2c_adapter *adapter)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321{
322 if (!(adapter->class & I2C_CLASS_HWMON)) {
323 return 0;
324 }
Jean Delvare2ed2dc32005-07-31 21:42:02 +0200325 return i2c_probe(adapter, &addr_data, adm1026_detect);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326}
327
Ben Dooksc49efce2005-10-26 21:07:25 +0200328static int adm1026_read_value(struct i2c_client *client, u8 reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329{
330 int res;
331
332 if (reg < 0x80) {
333 /* "RAM" locations */
334 res = i2c_smbus_read_byte_data(client, reg) & 0xff;
335 } else {
336 /* EEPROM, do nothing */
337 res = 0;
338 }
339 return res;
340}
341
Ben Dooksc49efce2005-10-26 21:07:25 +0200342static int adm1026_write_value(struct i2c_client *client, u8 reg, int value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343{
344 int res;
345
346 if (reg < 0x80) {
347 /* "RAM" locations */
348 res = i2c_smbus_write_byte_data(client, reg, value);
349 } else {
350 /* EEPROM, do nothing */
351 res = 0;
352 }
353 return res;
354}
355
Ben Dooksc49efce2005-10-26 21:07:25 +0200356static void adm1026_init_client(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357{
358 int value, i;
359 struct adm1026_data *data = i2c_get_clientdata(client);
360
Jean Delvarecb01a232007-11-29 23:46:42 +0100361 dev_dbg(&client->dev, "Initializing device\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 /* Read chip config */
363 data->config1 = adm1026_read_value(client, ADM1026_REG_CONFIG1);
364 data->config2 = adm1026_read_value(client, ADM1026_REG_CONFIG2);
365 data->config3 = adm1026_read_value(client, ADM1026_REG_CONFIG3);
366
367 /* Inform user of chip config */
368 dev_dbg(&client->dev, "ADM1026_REG_CONFIG1 is: 0x%02x\n",
369 data->config1);
370 if ((data->config1 & CFG1_MONITOR) == 0) {
371 dev_dbg(&client->dev, "Monitoring not currently "
372 "enabled.\n");
373 }
374 if (data->config1 & CFG1_INT_ENABLE) {
375 dev_dbg(&client->dev, "SMBALERT interrupts are "
376 "enabled.\n");
377 }
378 if (data->config1 & CFG1_AIN8_9) {
379 dev_dbg(&client->dev, "in8 and in9 enabled. "
380 "temp3 disabled.\n");
381 } else {
382 dev_dbg(&client->dev, "temp3 enabled. in8 and "
383 "in9 disabled.\n");
384 }
385 if (data->config1 & CFG1_THERM_HOT) {
386 dev_dbg(&client->dev, "Automatic THERM, PWM, "
387 "and temp limits enabled.\n");
388 }
389
390 value = data->config3;
391 if (data->config3 & CFG3_GPIO16_ENABLE) {
Jean Delvare368609c2005-07-29 12:15:07 -0700392 dev_dbg(&client->dev, "GPIO16 enabled. THERM "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 "pin disabled.\n");
394 } else {
395 dev_dbg(&client->dev, "THERM pin enabled. "
396 "GPIO16 disabled.\n");
397 }
398 if (data->config3 & CFG3_VREF_250) {
399 dev_dbg(&client->dev, "Vref is 2.50 Volts.\n");
400 } else {
401 dev_dbg(&client->dev, "Vref is 1.82 Volts.\n");
402 }
403 /* Read and pick apart the existing GPIO configuration */
404 value = 0;
405 for (i = 0;i <= 15;++i) {
406 if ((i & 0x03) == 0) {
407 value = adm1026_read_value(client,
408 ADM1026_REG_GPIO_CFG_0_3 + i/4);
409 }
410 data->gpio_config[i] = value & 0x03;
411 value >>= 2;
412 }
413 data->gpio_config[16] = (data->config3 >> 6) & 0x03;
414
415 /* ... and then print it */
416 adm1026_print_gpio(client);
417
418 /* If the user asks us to reprogram the GPIO config, then
419 * do it now.
420 */
421 if (gpio_input[0] != -1 || gpio_output[0] != -1
422 || gpio_inverted[0] != -1 || gpio_normal[0] != -1
423 || gpio_fan[0] != -1) {
424 adm1026_fixup_gpio(client);
425 }
426
427 /* WE INTENTIONALLY make no changes to the limits,
428 * offsets, pwms, fans and zones. If they were
429 * configured, we don't want to mess with them.
430 * If they weren't, the default is 100% PWM, no
431 * control and will suffice until 'sensors -s'
Jean Delvarecb01a232007-11-29 23:46:42 +0100432 * can be run by the user. We DO set the default
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 * value for pwm1.auto_pwm_min to its maximum
434 * so that enabling automatic pwm fan control
Jean Delvarecb01a232007-11-29 23:46:42 +0100435 * without first setting a value for pwm1.auto_pwm_min
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 * will not result in potentially dangerous fan speed decrease.
437 */
438 data->pwm1.auto_pwm_min=255;
439 /* Start monitoring */
440 value = adm1026_read_value(client, ADM1026_REG_CONFIG1);
441 /* Set MONITOR, clear interrupt acknowledge and s/w reset */
442 value = (value | CFG1_MONITOR) & (~CFG1_INT_CLEAR & ~CFG1_RESET);
443 dev_dbg(&client->dev, "Setting CONFIG to: 0x%02x\n", value);
444 data->config1 = value;
445 adm1026_write_value(client, ADM1026_REG_CONFIG1, value);
446
447 /* initialize fan_div[] to hardware defaults */
448 value = adm1026_read_value(client, ADM1026_REG_FAN_DIV_0_3) |
449 (adm1026_read_value(client, ADM1026_REG_FAN_DIV_4_7) << 8);
450 for (i = 0;i <= 7;++i) {
451 data->fan_div[i] = DIV_FROM_REG(value & 0x03);
452 value >>= 2;
453 }
454}
455
Ben Dooksc49efce2005-10-26 21:07:25 +0200456static void adm1026_print_gpio(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457{
458 struct adm1026_data *data = i2c_get_clientdata(client);
Jean Delvarecb01a232007-11-29 23:46:42 +0100459 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460
461 dev_dbg(&client->dev, "GPIO config is:");
462 for (i = 0;i <= 7;++i) {
463 if (data->config2 & (1 << i)) {
464 dev_dbg(&client->dev, "\t%sGP%s%d\n",
465 data->gpio_config[i] & 0x02 ? "" : "!",
466 data->gpio_config[i] & 0x01 ? "OUT" : "IN",
467 i);
468 } else {
469 dev_dbg(&client->dev, "\tFAN%d\n", i);
470 }
471 }
472 for (i = 8;i <= 15;++i) {
473 dev_dbg(&client->dev, "\t%sGP%s%d\n",
474 data->gpio_config[i] & 0x02 ? "" : "!",
475 data->gpio_config[i] & 0x01 ? "OUT" : "IN",
476 i);
477 }
478 if (data->config3 & CFG3_GPIO16_ENABLE) {
479 dev_dbg(&client->dev, "\t%sGP%s16\n",
480 data->gpio_config[16] & 0x02 ? "" : "!",
481 data->gpio_config[16] & 0x01 ? "OUT" : "IN");
482 } else {
Jean Delvarecb01a232007-11-29 23:46:42 +0100483 /* GPIO16 is THERM */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 dev_dbg(&client->dev, "\tTHERM\n");
485 }
486}
487
Ben Dooksc49efce2005-10-26 21:07:25 +0200488static void adm1026_fixup_gpio(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489{
490 struct adm1026_data *data = i2c_get_clientdata(client);
Jean Delvarecb01a232007-11-29 23:46:42 +0100491 int i;
492 int value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493
494 /* Make the changes requested. */
495 /* We may need to unlock/stop monitoring or soft-reset the
496 * chip before we can make changes. This hasn't been
497 * tested much. FIXME
498 */
499
500 /* Make outputs */
501 for (i = 0;i <= 16;++i) {
502 if (gpio_output[i] >= 0 && gpio_output[i] <= 16) {
503 data->gpio_config[gpio_output[i]] |= 0x01;
504 }
505 /* if GPIO0-7 is output, it isn't a FAN tach */
506 if (gpio_output[i] >= 0 && gpio_output[i] <= 7) {
507 data->config2 |= 1 << gpio_output[i];
508 }
509 }
510
511 /* Input overrides output */
512 for (i = 0;i <= 16;++i) {
513 if (gpio_input[i] >= 0 && gpio_input[i] <= 16) {
514 data->gpio_config[gpio_input[i]] &= ~ 0x01;
515 }
516 /* if GPIO0-7 is input, it isn't a FAN tach */
517 if (gpio_input[i] >= 0 && gpio_input[i] <= 7) {
518 data->config2 |= 1 << gpio_input[i];
519 }
520 }
521
Jean Delvarecb01a232007-11-29 23:46:42 +0100522 /* Inverted */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 for (i = 0;i <= 16;++i) {
524 if (gpio_inverted[i] >= 0 && gpio_inverted[i] <= 16) {
525 data->gpio_config[gpio_inverted[i]] &= ~ 0x02;
526 }
527 }
528
Jean Delvarecb01a232007-11-29 23:46:42 +0100529 /* Normal overrides inverted */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 for (i = 0;i <= 16;++i) {
531 if (gpio_normal[i] >= 0 && gpio_normal[i] <= 16) {
532 data->gpio_config[gpio_normal[i]] |= 0x02;
533 }
534 }
535
536 /* Fan overrides input and output */
537 for (i = 0;i <= 7;++i) {
538 if (gpio_fan[i] >= 0 && gpio_fan[i] <= 7) {
539 data->config2 &= ~(1 << gpio_fan[i]);
540 }
541 }
542
543 /* Write new configs to registers */
544 adm1026_write_value(client, ADM1026_REG_CONFIG2, data->config2);
545 data->config3 = (data->config3 & 0x3f)
546 | ((data->gpio_config[16] & 0x03) << 6);
547 adm1026_write_value(client, ADM1026_REG_CONFIG3, data->config3);
548 for (i = 15, value = 0;i >= 0;--i) {
549 value <<= 2;
550 value |= data->gpio_config[i] & 0x03;
551 if ((i & 0x03) == 0) {
552 adm1026_write_value(client,
553 ADM1026_REG_GPIO_CFG_0_3 + i/4,
554 value);
555 value = 0;
556 }
557 }
558
559 /* Print the new config */
560 adm1026_print_gpio(client);
561}
562
563
564static struct adm1026_data *adm1026_update_device(struct device *dev)
565{
566 struct i2c_client *client = to_i2c_client(dev);
567 struct adm1026_data *data = i2c_get_clientdata(client);
568 int i;
569 long value, alarms, gpio;
570
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100571 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 if (!data->valid
573 || time_after(jiffies, data->last_reading + ADM1026_DATA_INTERVAL)) {
574 /* Things that change quickly */
Jean Delvarecb01a232007-11-29 23:46:42 +0100575 dev_dbg(&client->dev, "Reading sensor values\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 for (i = 0;i <= 16;++i) {
577 data->in[i] =
578 adm1026_read_value(client, ADM1026_REG_IN[i]);
579 }
580
581 for (i = 0;i <= 7;++i) {
582 data->fan[i] =
583 adm1026_read_value(client, ADM1026_REG_FAN(i));
584 }
585
586 for (i = 0;i <= 2;++i) {
587 /* NOTE: temp[] is s8 and we assume 2's complement
Jean Delvarecb01a232007-11-29 23:46:42 +0100588 * "conversion" in the assignment */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 data->temp[i] =
590 adm1026_read_value(client, ADM1026_REG_TEMP[i]);
591 }
592
Jean Delvarecb01a232007-11-29 23:46:42 +0100593 data->pwm1.pwm = adm1026_read_value(client,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 ADM1026_REG_PWM);
Jean Delvarecb01a232007-11-29 23:46:42 +0100595 data->analog_out = adm1026_read_value(client,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 ADM1026_REG_DAC);
597 /* GPIO16 is MSbit of alarms, move it to gpio */
598 alarms = adm1026_read_value(client, ADM1026_REG_STATUS4);
Jean Delvarecb01a232007-11-29 23:46:42 +0100599 gpio = alarms & 0x80 ? 0x0100 : 0; /* GPIO16 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 alarms &= 0x7f;
601 alarms <<= 8;
602 alarms |= adm1026_read_value(client, ADM1026_REG_STATUS3);
603 alarms <<= 8;
604 alarms |= adm1026_read_value(client, ADM1026_REG_STATUS2);
605 alarms <<= 8;
606 alarms |= adm1026_read_value(client, ADM1026_REG_STATUS1);
607 data->alarms = alarms;
608
609 /* Read the GPIO values */
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_8_15);
612 gpio <<= 8;
Jean Delvarecb01a232007-11-29 23:46:42 +0100613 gpio |= adm1026_read_value(client,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 ADM1026_REG_GPIO_STATUS_0_7);
615 data->gpio = gpio;
616
617 data->last_reading = jiffies;
Jean Delvarecb01a232007-11-29 23:46:42 +0100618 }; /* last_reading */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619
620 if (!data->valid ||
621 time_after(jiffies, data->last_config + ADM1026_CONFIG_INTERVAL)) {
622 /* Things that don't change often */
623 dev_dbg(&client->dev, "Reading config values\n");
624 for (i = 0;i <= 16;++i) {
Jean Delvarecb01a232007-11-29 23:46:42 +0100625 data->in_min[i] = adm1026_read_value(client,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 ADM1026_REG_IN_MIN[i]);
Jean Delvarecb01a232007-11-29 23:46:42 +0100627 data->in_max[i] = adm1026_read_value(client,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 ADM1026_REG_IN_MAX[i]);
629 }
630
631 value = adm1026_read_value(client, ADM1026_REG_FAN_DIV_0_3)
632 | (adm1026_read_value(client, ADM1026_REG_FAN_DIV_4_7)
633 << 8);
634 for (i = 0;i <= 7;++i) {
Jean Delvarecb01a232007-11-29 23:46:42 +0100635 data->fan_min[i] = adm1026_read_value(client,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 ADM1026_REG_FAN_MIN(i));
637 data->fan_div[i] = DIV_FROM_REG(value & 0x03);
638 value >>= 2;
639 }
640
641 for (i = 0; i <= 2; ++i) {
Jean Delvarecb01a232007-11-29 23:46:42 +0100642 /* NOTE: temp_xxx[] are s8 and we assume 2's
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 * complement "conversion" in the assignment
644 */
Jean Delvarecb01a232007-11-29 23:46:42 +0100645 data->temp_min[i] = adm1026_read_value(client,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 ADM1026_REG_TEMP_MIN[i]);
Jean Delvarecb01a232007-11-29 23:46:42 +0100647 data->temp_max[i] = adm1026_read_value(client,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 ADM1026_REG_TEMP_MAX[i]);
Jean Delvarecb01a232007-11-29 23:46:42 +0100649 data->temp_tmin[i] = adm1026_read_value(client,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 ADM1026_REG_TEMP_TMIN[i]);
Jean Delvarecb01a232007-11-29 23:46:42 +0100651 data->temp_crit[i] = adm1026_read_value(client,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 ADM1026_REG_TEMP_THERM[i]);
Jean Delvarecb01a232007-11-29 23:46:42 +0100653 data->temp_offset[i] = adm1026_read_value(client,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 ADM1026_REG_TEMP_OFFSET[i]);
655 }
656
657 /* Read the STATUS/alarm masks */
Jean Delvarecb01a232007-11-29 23:46:42 +0100658 alarms = adm1026_read_value(client, ADM1026_REG_MASK4);
659 gpio = alarms & 0x80 ? 0x0100 : 0; /* GPIO16 */
660 alarms = (alarms & 0x7f) << 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 alarms |= adm1026_read_value(client, ADM1026_REG_MASK3);
662 alarms <<= 8;
663 alarms |= adm1026_read_value(client, ADM1026_REG_MASK2);
664 alarms <<= 8;
665 alarms |= adm1026_read_value(client, ADM1026_REG_MASK1);
666 data->alarm_mask = alarms;
667
668 /* Read the GPIO values */
Jean Delvarecb01a232007-11-29 23:46:42 +0100669 gpio |= adm1026_read_value(client,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 ADM1026_REG_GPIO_MASK_8_15);
671 gpio <<= 8;
672 gpio |= adm1026_read_value(client, ADM1026_REG_GPIO_MASK_0_7);
673 data->gpio_mask = gpio;
674
675 /* Read various values from CONFIG1 */
Jean Delvarecb01a232007-11-29 23:46:42 +0100676 data->config1 = adm1026_read_value(client,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 ADM1026_REG_CONFIG1);
678 if (data->config1 & CFG1_PWM_AFC) {
679 data->pwm1.enable = 2;
Jean Delvarecb01a232007-11-29 23:46:42 +0100680 data->pwm1.auto_pwm_min =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 PWM_MIN_FROM_REG(data->pwm1.pwm);
682 }
683 /* Read the GPIO config */
Jean Delvarecb01a232007-11-29 23:46:42 +0100684 data->config2 = adm1026_read_value(client,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 ADM1026_REG_CONFIG2);
Jean Delvarecb01a232007-11-29 23:46:42 +0100686 data->config3 = adm1026_read_value(client,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 ADM1026_REG_CONFIG3);
688 data->gpio_config[16] = (data->config3 >> 6) & 0x03;
689
690 value = 0;
691 for (i = 0;i <= 15;++i) {
692 if ((i & 0x03) == 0) {
693 value = adm1026_read_value(client,
694 ADM1026_REG_GPIO_CFG_0_3 + i/4);
695 }
696 data->gpio_config[i] = value & 0x03;
697 value >>= 2;
698 }
699
700 data->last_config = jiffies;
Jean Delvarecb01a232007-11-29 23:46:42 +0100701 }; /* last_config */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702
703 dev_dbg(&client->dev, "Setting VID from GPIO11-15.\n");
704 data->vid = (data->gpio >> 11) & 0x1f;
705 data->valid = 1;
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100706 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 return data;
708}
709
Yani Ioannou050480f2005-06-05 10:51:46 +0200710static ssize_t show_in(struct device *dev, struct device_attribute *attr,
711 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712{
Yani Ioannou050480f2005-06-05 10:51:46 +0200713 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
714 int nr = sensor_attr->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 struct adm1026_data *data = adm1026_update_device(dev);
Jean Delvarecb01a232007-11-29 23:46:42 +0100716 return sprintf(buf, "%d\n", INS_FROM_REG(nr, data->in[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717}
Yani Ioannou050480f2005-06-05 10:51:46 +0200718static ssize_t show_in_min(struct device *dev, struct device_attribute *attr,
719 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720{
Yani Ioannou050480f2005-06-05 10:51:46 +0200721 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
722 int nr = sensor_attr->index;
Jean Delvarecb01a232007-11-29 23:46:42 +0100723 struct adm1026_data *data = adm1026_update_device(dev);
724 return sprintf(buf, "%d\n", INS_FROM_REG(nr, data->in_min[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725}
Yani Ioannou050480f2005-06-05 10:51:46 +0200726static ssize_t set_in_min(struct device *dev, struct device_attribute *attr,
727 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728{
Yani Ioannou050480f2005-06-05 10:51:46 +0200729 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
730 int nr = sensor_attr->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 struct i2c_client *client = to_i2c_client(dev);
732 struct adm1026_data *data = i2c_get_clientdata(client);
733 int val = simple_strtol(buf, NULL, 10);
734
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100735 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 data->in_min[nr] = INS_TO_REG(nr, val);
737 adm1026_write_value(client, ADM1026_REG_IN_MIN[nr], data->in_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100738 mutex_unlock(&data->update_lock);
Jean Delvarecb01a232007-11-29 23:46:42 +0100739 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740}
Yani Ioannou050480f2005-06-05 10:51:46 +0200741static ssize_t show_in_max(struct device *dev, struct device_attribute *attr,
742 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743{
Yani Ioannou050480f2005-06-05 10:51:46 +0200744 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
745 int nr = sensor_attr->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 struct adm1026_data *data = adm1026_update_device(dev);
Jean Delvarecb01a232007-11-29 23:46:42 +0100747 return sprintf(buf, "%d\n", INS_FROM_REG(nr, data->in_max[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748}
Yani Ioannou050480f2005-06-05 10:51:46 +0200749static ssize_t set_in_max(struct device *dev, struct device_attribute *attr,
750 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751{
Yani Ioannou050480f2005-06-05 10:51:46 +0200752 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
753 int nr = sensor_attr->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 struct i2c_client *client = to_i2c_client(dev);
755 struct adm1026_data *data = i2c_get_clientdata(client);
756 int val = simple_strtol(buf, NULL, 10);
757
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100758 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 data->in_max[nr] = INS_TO_REG(nr, val);
760 adm1026_write_value(client, ADM1026_REG_IN_MAX[nr], data->in_max[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100761 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 return count;
763}
764
Yani Ioannou050480f2005-06-05 10:51:46 +0200765#define in_reg(offset) \
766static SENSOR_DEVICE_ATTR(in##offset##_input, S_IRUGO, show_in, \
767 NULL, offset); \
768static SENSOR_DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \
769 show_in_min, set_in_min, offset); \
770static SENSOR_DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \
771 show_in_max, set_in_max, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772
773
774in_reg(0);
775in_reg(1);
776in_reg(2);
777in_reg(3);
778in_reg(4);
779in_reg(5);
780in_reg(6);
781in_reg(7);
782in_reg(8);
783in_reg(9);
784in_reg(10);
785in_reg(11);
786in_reg(12);
787in_reg(13);
788in_reg(14);
789in_reg(15);
790
Yani Ioannou74880c02005-05-17 06:41:12 -0400791static ssize_t show_in16(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792{
793 struct adm1026_data *data = adm1026_update_device(dev);
Jean Delvarecb01a232007-11-29 23:46:42 +0100794 return sprintf(buf, "%d\n", INS_FROM_REG(16, data->in[16]) -
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 NEG12_OFFSET);
796}
Yani Ioannou74880c02005-05-17 06:41:12 -0400797static ssize_t show_in16_min(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798{
Jean Delvarecb01a232007-11-29 23:46:42 +0100799 struct adm1026_data *data = adm1026_update_device(dev);
800 return sprintf(buf, "%d\n", INS_FROM_REG(16, data->in_min[16])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 - NEG12_OFFSET);
802}
Yani Ioannou74880c02005-05-17 06:41:12 -0400803static 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 -0700804{
805 struct i2c_client *client = to_i2c_client(dev);
806 struct adm1026_data *data = i2c_get_clientdata(client);
807 int val = simple_strtol(buf, NULL, 10);
808
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100809 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 data->in_min[16] = INS_TO_REG(16, val + NEG12_OFFSET);
811 adm1026_write_value(client, ADM1026_REG_IN_MIN[16], data->in_min[16]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100812 mutex_unlock(&data->update_lock);
Jean Delvarecb01a232007-11-29 23:46:42 +0100813 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814}
Yani Ioannou74880c02005-05-17 06:41:12 -0400815static ssize_t show_in16_max(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816{
817 struct adm1026_data *data = adm1026_update_device(dev);
Jean Delvarecb01a232007-11-29 23:46:42 +0100818 return sprintf(buf, "%d\n", INS_FROM_REG(16, data->in_max[16])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 - NEG12_OFFSET);
820}
Yani Ioannou74880c02005-05-17 06:41:12 -0400821static 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 -0700822{
823 struct i2c_client *client = to_i2c_client(dev);
824 struct adm1026_data *data = i2c_get_clientdata(client);
825 int val = simple_strtol(buf, NULL, 10);
826
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100827 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 data->in_max[16] = INS_TO_REG(16, val+NEG12_OFFSET);
829 adm1026_write_value(client, ADM1026_REG_IN_MAX[16], data->in_max[16]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100830 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 return count;
832}
833
Yani Ioannou050480f2005-06-05 10:51:46 +0200834static SENSOR_DEVICE_ATTR(in16_input, S_IRUGO, show_in16, NULL, 16);
835static SENSOR_DEVICE_ATTR(in16_min, S_IRUGO | S_IWUSR, show_in16_min, set_in16_min, 16);
836static SENSOR_DEVICE_ATTR(in16_max, S_IRUGO | S_IWUSR, show_in16_max, set_in16_max, 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837
838
839
840
841/* Now add fan read/write functions */
842
Yani Ioannou050480f2005-06-05 10:51:46 +0200843static ssize_t show_fan(struct device *dev, struct device_attribute *attr,
844 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845{
Yani Ioannou050480f2005-06-05 10:51:46 +0200846 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
847 int nr = sensor_attr->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 struct adm1026_data *data = adm1026_update_device(dev);
Jean Delvarecb01a232007-11-29 23:46:42 +0100849 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[nr],
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 data->fan_div[nr]));
851}
Yani Ioannou050480f2005-06-05 10:51:46 +0200852static ssize_t show_fan_min(struct device *dev, struct device_attribute *attr,
853 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854{
Yani Ioannou050480f2005-06-05 10:51:46 +0200855 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
856 int nr = sensor_attr->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 struct adm1026_data *data = adm1026_update_device(dev);
Jean Delvarecb01a232007-11-29 23:46:42 +0100858 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_min[nr],
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 data->fan_div[nr]));
860}
Yani Ioannou050480f2005-06-05 10:51:46 +0200861static ssize_t set_fan_min(struct device *dev, struct device_attribute *attr,
862 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863{
Yani Ioannou050480f2005-06-05 10:51:46 +0200864 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
865 int nr = sensor_attr->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 struct i2c_client *client = to_i2c_client(dev);
867 struct adm1026_data *data = i2c_get_clientdata(client);
868 int val = simple_strtol(buf, NULL, 10);
869
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100870 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 data->fan_min[nr] = FAN_TO_REG(val, data->fan_div[nr]);
872 adm1026_write_value(client, ADM1026_REG_FAN_MIN(nr),
873 data->fan_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100874 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 return count;
876}
877
Jean Delvarecb01a232007-11-29 23:46:42 +0100878#define fan_offset(offset) \
879static SENSOR_DEVICE_ATTR(fan##offset##_input, S_IRUGO, show_fan, NULL, \
880 offset - 1); \
881static SENSOR_DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
Yani Ioannou050480f2005-06-05 10:51:46 +0200882 show_fan_min, set_fan_min, offset - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883
884fan_offset(1);
885fan_offset(2);
886fan_offset(3);
887fan_offset(4);
888fan_offset(5);
889fan_offset(6);
890fan_offset(7);
891fan_offset(8);
892
893/* Adjust fan_min to account for new fan divisor */
894static void fixup_fan_min(struct device *dev, int fan, int old_div)
895{
896 struct i2c_client *client = to_i2c_client(dev);
897 struct adm1026_data *data = i2c_get_clientdata(client);
Jean Delvarecb01a232007-11-29 23:46:42 +0100898 int new_min;
899 int new_div = data->fan_div[fan];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900
901 /* 0 and 0xff are special. Don't adjust them */
902 if (data->fan_min[fan] == 0 || data->fan_min[fan] == 0xff) {
903 return;
904 }
905
906 new_min = data->fan_min[fan] * old_div / new_div;
907 new_min = SENSORS_LIMIT(new_min, 1, 254);
908 data->fan_min[fan] = new_min;
909 adm1026_write_value(client, ADM1026_REG_FAN_MIN(fan), new_min);
910}
911
912/* Now add fan_div read/write functions */
Yani Ioannou050480f2005-06-05 10:51:46 +0200913static ssize_t show_fan_div(struct device *dev, struct device_attribute *attr,
914 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915{
Yani Ioannou050480f2005-06-05 10:51:46 +0200916 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
917 int nr = sensor_attr->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 struct adm1026_data *data = adm1026_update_device(dev);
Jean Delvarecb01a232007-11-29 23:46:42 +0100919 return sprintf(buf, "%d\n", data->fan_div[nr]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920}
Yani Ioannou050480f2005-06-05 10:51:46 +0200921static ssize_t set_fan_div(struct device *dev, struct device_attribute *attr,
922 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923{
Yani Ioannou050480f2005-06-05 10:51:46 +0200924 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
925 int nr = sensor_attr->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 struct i2c_client *client = to_i2c_client(dev);
927 struct adm1026_data *data = i2c_get_clientdata(client);
Jean Delvarecb01a232007-11-29 23:46:42 +0100928 int val, orig_div, new_div, shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929
930 val = simple_strtol(buf, NULL, 10);
Jean Delvarecb01a232007-11-29 23:46:42 +0100931 new_div = DIV_TO_REG(val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 if (new_div == 0) {
933 return -EINVAL;
934 }
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100935 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 orig_div = data->fan_div[nr];
937 data->fan_div[nr] = DIV_FROM_REG(new_div);
938
939 if (nr < 4) { /* 0 <= nr < 4 */
940 shift = 2 * nr;
941 adm1026_write_value(client, ADM1026_REG_FAN_DIV_0_3,
942 ((DIV_TO_REG(orig_div) & (~(0x03 << shift))) |
943 (new_div << shift)));
944 } else { /* 3 < nr < 8 */
945 shift = 2 * (nr - 4);
946 adm1026_write_value(client, ADM1026_REG_FAN_DIV_4_7,
947 ((DIV_TO_REG(orig_div) & (~(0x03 << (2 * shift)))) |
948 (new_div << shift)));
949 }
950
951 if (data->fan_div[nr] != orig_div) {
Jean Delvarecb01a232007-11-29 23:46:42 +0100952 fixup_fan_min(dev, nr, orig_div);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 }
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100954 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 return count;
956}
957
Jean Delvarecb01a232007-11-29 23:46:42 +0100958#define fan_offset_div(offset) \
959static SENSOR_DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR, \
Yani Ioannou050480f2005-06-05 10:51:46 +0200960 show_fan_div, set_fan_div, offset - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961
962fan_offset_div(1);
963fan_offset_div(2);
964fan_offset_div(3);
965fan_offset_div(4);
966fan_offset_div(5);
967fan_offset_div(6);
968fan_offset_div(7);
969fan_offset_div(8);
970
971/* Temps */
Yani Ioannou050480f2005-06-05 10:51:46 +0200972static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
973 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974{
Yani Ioannou050480f2005-06-05 10:51:46 +0200975 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
976 int nr = sensor_attr->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 struct adm1026_data *data = adm1026_update_device(dev);
Jean Delvarecb01a232007-11-29 23:46:42 +0100978 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979}
Yani Ioannou050480f2005-06-05 10:51:46 +0200980static ssize_t show_temp_min(struct device *dev, struct device_attribute *attr,
981 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982{
Yani Ioannou050480f2005-06-05 10:51:46 +0200983 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
984 int nr = sensor_attr->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 struct adm1026_data *data = adm1026_update_device(dev);
Jean Delvarecb01a232007-11-29 23:46:42 +0100986 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_min[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987}
Yani Ioannou050480f2005-06-05 10:51:46 +0200988static ssize_t set_temp_min(struct device *dev, struct device_attribute *attr,
989 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990{
Yani Ioannou050480f2005-06-05 10:51:46 +0200991 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
992 int nr = sensor_attr->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 struct i2c_client *client = to_i2c_client(dev);
994 struct adm1026_data *data = i2c_get_clientdata(client);
995 int val = simple_strtol(buf, NULL, 10);
996
Ingo Molnar9a61bf62006-01-18 23:19:26 +0100997 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 data->temp_min[nr] = TEMP_TO_REG(val);
999 adm1026_write_value(client, ADM1026_REG_TEMP_MIN[nr],
1000 data->temp_min[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001001 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 return count;
1003}
Yani Ioannou050480f2005-06-05 10:51:46 +02001004static ssize_t show_temp_max(struct device *dev, struct device_attribute *attr,
1005 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006{
Yani Ioannou050480f2005-06-05 10:51:46 +02001007 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1008 int nr = sensor_attr->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 struct adm1026_data *data = adm1026_update_device(dev);
Jean Delvarecb01a232007-11-29 23:46:42 +01001010 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_max[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011}
Yani Ioannou050480f2005-06-05 10:51:46 +02001012static ssize_t set_temp_max(struct device *dev, struct device_attribute *attr,
1013 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014{
Yani Ioannou050480f2005-06-05 10:51:46 +02001015 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1016 int nr = sensor_attr->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 struct i2c_client *client = to_i2c_client(dev);
1018 struct adm1026_data *data = i2c_get_clientdata(client);
1019 int val = simple_strtol(buf, NULL, 10);
1020
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001021 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 data->temp_max[nr] = TEMP_TO_REG(val);
1023 adm1026_write_value(client, ADM1026_REG_TEMP_MAX[nr],
1024 data->temp_max[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001025 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 return count;
1027}
Yani Ioannou050480f2005-06-05 10:51:46 +02001028
1029#define temp_reg(offset) \
Jean Delvarecb01a232007-11-29 23:46:42 +01001030static SENSOR_DEVICE_ATTR(temp##offset##_input, S_IRUGO, show_temp, \
Yani Ioannou050480f2005-06-05 10:51:46 +02001031 NULL, offset - 1); \
1032static SENSOR_DEVICE_ATTR(temp##offset##_min, S_IRUGO | S_IWUSR, \
1033 show_temp_min, set_temp_min, offset - 1); \
1034static SENSOR_DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR, \
1035 show_temp_max, set_temp_max, offset - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036
1037
1038temp_reg(1);
1039temp_reg(2);
1040temp_reg(3);
1041
Yani Ioannou050480f2005-06-05 10:51:46 +02001042static ssize_t show_temp_offset(struct device *dev,
1043 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044{
Yani Ioannou050480f2005-06-05 10:51:46 +02001045 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1046 int nr = sensor_attr->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 struct adm1026_data *data = adm1026_update_device(dev);
Jean Delvarecb01a232007-11-29 23:46:42 +01001048 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_offset[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049}
Yani Ioannou050480f2005-06-05 10:51:46 +02001050static ssize_t set_temp_offset(struct device *dev,
1051 struct device_attribute *attr, const char *buf,
1052 size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053{
Yani Ioannou050480f2005-06-05 10:51:46 +02001054 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1055 int nr = sensor_attr->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 struct i2c_client *client = to_i2c_client(dev);
1057 struct adm1026_data *data = i2c_get_clientdata(client);
1058 int val = simple_strtol(buf, NULL, 10);
1059
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001060 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 data->temp_offset[nr] = TEMP_TO_REG(val);
1062 adm1026_write_value(client, ADM1026_REG_TEMP_OFFSET[nr],
1063 data->temp_offset[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001064 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 return count;
1066}
1067
Yani Ioannou050480f2005-06-05 10:51:46 +02001068#define temp_offset_reg(offset) \
1069static SENSOR_DEVICE_ATTR(temp##offset##_offset, S_IRUGO | S_IWUSR, \
1070 show_temp_offset, set_temp_offset, offset - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071
1072temp_offset_reg(1);
1073temp_offset_reg(2);
1074temp_offset_reg(3);
1075
Yani Ioannou050480f2005-06-05 10:51:46 +02001076static ssize_t show_temp_auto_point1_temp_hyst(struct device *dev,
1077 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078{
Yani Ioannou050480f2005-06-05 10:51:46 +02001079 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1080 int nr = sensor_attr->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 struct adm1026_data *data = adm1026_update_device(dev);
Jean Delvarecb01a232007-11-29 23:46:42 +01001082 return sprintf(buf, "%d\n", TEMP_FROM_REG(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 ADM1026_FAN_ACTIVATION_TEMP_HYST + data->temp_tmin[nr]));
1084}
Yani Ioannou050480f2005-06-05 10:51:46 +02001085static ssize_t show_temp_auto_point2_temp(struct device *dev,
1086 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087{
Yani Ioannou050480f2005-06-05 10:51:46 +02001088 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1089 int nr = sensor_attr->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 struct adm1026_data *data = adm1026_update_device(dev);
Jean Delvarecb01a232007-11-29 23:46:42 +01001091 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_tmin[nr] +
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 ADM1026_FAN_CONTROL_TEMP_RANGE));
1093}
Yani Ioannou050480f2005-06-05 10:51:46 +02001094static ssize_t show_temp_auto_point1_temp(struct device *dev,
1095 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096{
Yani Ioannou050480f2005-06-05 10:51:46 +02001097 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1098 int nr = sensor_attr->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 struct adm1026_data *data = adm1026_update_device(dev);
Jean Delvarecb01a232007-11-29 23:46:42 +01001100 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_tmin[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101}
Yani Ioannou050480f2005-06-05 10:51:46 +02001102static ssize_t set_temp_auto_point1_temp(struct device *dev,
1103 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104{
Yani Ioannou050480f2005-06-05 10:51:46 +02001105 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1106 int nr = sensor_attr->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 struct i2c_client *client = to_i2c_client(dev);
1108 struct adm1026_data *data = i2c_get_clientdata(client);
1109 int val = simple_strtol(buf, NULL, 10);
1110
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001111 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 data->temp_tmin[nr] = TEMP_TO_REG(val);
1113 adm1026_write_value(client, ADM1026_REG_TEMP_TMIN[nr],
1114 data->temp_tmin[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001115 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 return count;
1117}
1118
Jean Delvarecb01a232007-11-29 23:46:42 +01001119#define temp_auto_point(offset) \
1120static SENSOR_DEVICE_ATTR(temp##offset##_auto_point1_temp, \
1121 S_IRUGO | S_IWUSR, show_temp_auto_point1_temp, \
1122 set_temp_auto_point1_temp, offset - 1); \
1123static SENSOR_DEVICE_ATTR(temp##offset##_auto_point1_temp_hyst, S_IRUGO,\
1124 show_temp_auto_point1_temp_hyst, NULL, offset - 1); \
1125static SENSOR_DEVICE_ATTR(temp##offset##_auto_point2_temp, S_IRUGO, \
Yani Ioannou050480f2005-06-05 10:51:46 +02001126 show_temp_auto_point2_temp, NULL, offset - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127
1128temp_auto_point(1);
1129temp_auto_point(2);
1130temp_auto_point(3);
1131
Yani Ioannou050480f2005-06-05 10:51:46 +02001132static ssize_t show_temp_crit_enable(struct device *dev,
1133 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134{
1135 struct adm1026_data *data = adm1026_update_device(dev);
Jean Delvarecb01a232007-11-29 23:46:42 +01001136 return sprintf(buf, "%d\n", (data->config1 & CFG1_THERM_HOT) >> 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137}
Yani Ioannou050480f2005-06-05 10:51:46 +02001138static ssize_t set_temp_crit_enable(struct device *dev,
1139 struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140{
1141 struct i2c_client *client = to_i2c_client(dev);
1142 struct adm1026_data *data = i2c_get_clientdata(client);
1143 int val = simple_strtol(buf, NULL, 10);
1144
1145 if ((val == 1) || (val==0)) {
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001146 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 data->config1 = (data->config1 & ~CFG1_THERM_HOT) | (val << 4);
Jean Delvarecb01a232007-11-29 23:46:42 +01001148 adm1026_write_value(client, ADM1026_REG_CONFIG1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 data->config1);
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001150 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 }
1152 return count;
1153}
1154
Yani Ioannou050480f2005-06-05 10:51:46 +02001155#define temp_crit_enable(offset) \
1156static DEVICE_ATTR(temp##offset##_crit_enable, S_IRUGO | S_IWUSR, \
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 show_temp_crit_enable, set_temp_crit_enable);
1158
Yani Ioannou050480f2005-06-05 10:51:46 +02001159temp_crit_enable(1);
1160temp_crit_enable(2);
1161temp_crit_enable(3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162
Yani Ioannou050480f2005-06-05 10:51:46 +02001163static ssize_t show_temp_crit(struct device *dev,
1164 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165{
Yani Ioannou050480f2005-06-05 10:51:46 +02001166 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1167 int nr = sensor_attr->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 struct adm1026_data *data = adm1026_update_device(dev);
Jean Delvarecb01a232007-11-29 23:46:42 +01001169 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_crit[nr]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170}
Yani Ioannou050480f2005-06-05 10:51:46 +02001171static ssize_t set_temp_crit(struct device *dev, struct device_attribute *attr,
1172 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173{
Yani Ioannou050480f2005-06-05 10:51:46 +02001174 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1175 int nr = sensor_attr->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 struct i2c_client *client = to_i2c_client(dev);
1177 struct adm1026_data *data = i2c_get_clientdata(client);
1178 int val = simple_strtol(buf, NULL, 10);
1179
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001180 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 data->temp_crit[nr] = TEMP_TO_REG(val);
1182 adm1026_write_value(client, ADM1026_REG_TEMP_THERM[nr],
1183 data->temp_crit[nr]);
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001184 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 return count;
1186}
1187
Yani Ioannou050480f2005-06-05 10:51:46 +02001188#define temp_crit_reg(offset) \
1189static SENSOR_DEVICE_ATTR(temp##offset##_crit, S_IRUGO | S_IWUSR, \
1190 show_temp_crit, set_temp_crit, offset - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191
1192temp_crit_reg(1);
1193temp_crit_reg(2);
1194temp_crit_reg(3);
1195
Yani Ioannou74880c02005-05-17 06:41:12 -04001196static ssize_t show_analog_out_reg(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197{
1198 struct adm1026_data *data = adm1026_update_device(dev);
Jean Delvarecb01a232007-11-29 23:46:42 +01001199 return sprintf(buf, "%d\n", DAC_FROM_REG(data->analog_out));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200}
Yani Ioannou74880c02005-05-17 06:41:12 -04001201static ssize_t set_analog_out_reg(struct device *dev, struct device_attribute *attr, const char *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 size_t count)
1203{
1204 struct i2c_client *client = to_i2c_client(dev);
1205 struct adm1026_data *data = i2c_get_clientdata(client);
1206 int val = simple_strtol(buf, NULL, 10);
1207
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001208 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 data->analog_out = DAC_TO_REG(val);
1210 adm1026_write_value(client, ADM1026_REG_DAC, data->analog_out);
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001211 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 return count;
1213}
1214
Jean Delvarecb01a232007-11-29 23:46:42 +01001215static DEVICE_ATTR(analog_out, S_IRUGO | S_IWUSR, show_analog_out_reg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 set_analog_out_reg);
1217
Yani Ioannou74880c02005-05-17 06:41:12 -04001218static ssize_t show_vid_reg(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219{
1220 struct adm1026_data *data = adm1026_update_device(dev);
Jean Delvarecb01a232007-11-29 23:46:42 +01001221 return sprintf(buf, "%d\n", vid_from_reg(data->vid & 0x3f, data->vrm));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222}
Grant Coady937df8d2005-05-12 11:59:29 +10001223static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid_reg, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224
Yani Ioannou74880c02005-05-17 06:41:12 -04001225static ssize_t show_vrm_reg(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226{
Jean Delvare90d66192007-10-08 18:24:35 +02001227 struct adm1026_data *data = dev_get_drvdata(dev);
Jean Delvarecb01a232007-11-29 23:46:42 +01001228 return sprintf(buf, "%d\n", data->vrm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229}
Yani Ioannou74880c02005-05-17 06:41:12 -04001230static ssize_t store_vrm_reg(struct device *dev, struct device_attribute *attr, const char *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 size_t count)
1232{
1233 struct i2c_client *client = to_i2c_client(dev);
1234 struct adm1026_data *data = i2c_get_clientdata(client);
1235
1236 data->vrm = simple_strtol(buf, NULL, 10);
1237 return count;
1238}
1239
1240static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm_reg, store_vrm_reg);
1241
Yani Ioannou74880c02005-05-17 06:41:12 -04001242static ssize_t show_alarms_reg(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243{
1244 struct adm1026_data *data = adm1026_update_device(dev);
1245 return sprintf(buf, "%ld\n", (long) (data->alarms));
1246}
1247
1248static DEVICE_ATTR(alarms, S_IRUGO, show_alarms_reg, NULL);
1249
Jean Delvarea9273cb2007-11-29 23:45:22 +01001250static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
1251 char *buf)
1252{
1253 struct adm1026_data *data = adm1026_update_device(dev);
1254 int bitnr = to_sensor_dev_attr(attr)->index;
1255 return sprintf(buf, "%ld\n", (data->alarms >> bitnr) & 1);
1256}
1257
1258static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 0);
1259static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, 1);
1260static SENSOR_DEVICE_ATTR(in9_alarm, S_IRUGO, show_alarm, NULL, 1);
1261static SENSOR_DEVICE_ATTR(in11_alarm, S_IRUGO, show_alarm, NULL, 2);
1262static SENSOR_DEVICE_ATTR(in12_alarm, S_IRUGO, show_alarm, NULL, 3);
1263static SENSOR_DEVICE_ATTR(in13_alarm, S_IRUGO, show_alarm, NULL, 4);
1264static SENSOR_DEVICE_ATTR(in14_alarm, S_IRUGO, show_alarm, NULL, 5);
1265static SENSOR_DEVICE_ATTR(in15_alarm, S_IRUGO, show_alarm, NULL, 6);
1266static SENSOR_DEVICE_ATTR(in16_alarm, S_IRUGO, show_alarm, NULL, 7);
1267static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 8);
1268static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 9);
1269static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 10);
1270static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 11);
1271static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 12);
1272static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 13);
1273static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 14);
1274static SENSOR_DEVICE_ATTR(in7_alarm, S_IRUGO, show_alarm, NULL, 15);
1275static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 16);
1276static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 17);
1277static SENSOR_DEVICE_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL, 18);
1278static SENSOR_DEVICE_ATTR(fan4_alarm, S_IRUGO, show_alarm, NULL, 19);
1279static SENSOR_DEVICE_ATTR(fan5_alarm, S_IRUGO, show_alarm, NULL, 20);
1280static SENSOR_DEVICE_ATTR(fan6_alarm, S_IRUGO, show_alarm, NULL, 21);
1281static SENSOR_DEVICE_ATTR(fan7_alarm, S_IRUGO, show_alarm, NULL, 22);
1282static SENSOR_DEVICE_ATTR(fan8_alarm, S_IRUGO, show_alarm, NULL, 23);
1283static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 24);
1284static SENSOR_DEVICE_ATTR(in10_alarm, S_IRUGO, show_alarm, NULL, 25);
1285static SENSOR_DEVICE_ATTR(in8_alarm, S_IRUGO, show_alarm, NULL, 26);
1286
Yani Ioannou74880c02005-05-17 06:41:12 -04001287static ssize_t show_alarm_mask(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288{
1289 struct adm1026_data *data = adm1026_update_device(dev);
Jean Delvarecb01a232007-11-29 23:46:42 +01001290 return sprintf(buf, "%ld\n", data->alarm_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291}
Yani Ioannou74880c02005-05-17 06:41:12 -04001292static ssize_t set_alarm_mask(struct device *dev, struct device_attribute *attr, const char *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 size_t count)
1294{
1295 struct i2c_client *client = to_i2c_client(dev);
1296 struct adm1026_data *data = i2c_get_clientdata(client);
1297 int val = simple_strtol(buf, NULL, 10);
1298 unsigned long mask;
1299
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001300 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 data->alarm_mask = val & 0x7fffffff;
1302 mask = data->alarm_mask
1303 | (data->gpio_mask & 0x10000 ? 0x80000000 : 0);
1304 adm1026_write_value(client, ADM1026_REG_MASK1,
1305 mask & 0xff);
1306 mask >>= 8;
1307 adm1026_write_value(client, ADM1026_REG_MASK2,
1308 mask & 0xff);
1309 mask >>= 8;
1310 adm1026_write_value(client, ADM1026_REG_MASK3,
1311 mask & 0xff);
1312 mask >>= 8;
1313 adm1026_write_value(client, ADM1026_REG_MASK4,
1314 mask & 0xff);
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001315 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 return count;
1317}
1318
1319static DEVICE_ATTR(alarm_mask, S_IRUGO | S_IWUSR, show_alarm_mask,
1320 set_alarm_mask);
1321
1322
Yani Ioannou74880c02005-05-17 06:41:12 -04001323static ssize_t show_gpio(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324{
1325 struct adm1026_data *data = adm1026_update_device(dev);
Jean Delvarecb01a232007-11-29 23:46:42 +01001326 return sprintf(buf, "%ld\n", data->gpio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327}
Yani Ioannou74880c02005-05-17 06:41:12 -04001328static ssize_t set_gpio(struct device *dev, struct device_attribute *attr, const char *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 size_t count)
1330{
1331 struct i2c_client *client = to_i2c_client(dev);
1332 struct adm1026_data *data = i2c_get_clientdata(client);
1333 int val = simple_strtol(buf, NULL, 10);
Jean Delvarecb01a232007-11-29 23:46:42 +01001334 long gpio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001336 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 data->gpio = val & 0x1ffff;
1338 gpio = data->gpio;
Jean Delvarecb01a232007-11-29 23:46:42 +01001339 adm1026_write_value(client, ADM1026_REG_GPIO_STATUS_0_7, gpio & 0xff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 gpio >>= 8;
Jean Delvarecb01a232007-11-29 23:46:42 +01001341 adm1026_write_value(client, ADM1026_REG_GPIO_STATUS_8_15, gpio & 0xff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 gpio = ((gpio >> 1) & 0x80) | (data->alarms >> 24 & 0x7f);
Jean Delvarecb01a232007-11-29 23:46:42 +01001343 adm1026_write_value(client, ADM1026_REG_STATUS4, gpio & 0xff);
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001344 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 return count;
1346}
1347
1348static DEVICE_ATTR(gpio, S_IRUGO | S_IWUSR, show_gpio, set_gpio);
1349
1350
Yani Ioannou74880c02005-05-17 06:41:12 -04001351static ssize_t show_gpio_mask(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352{
1353 struct adm1026_data *data = adm1026_update_device(dev);
Jean Delvarecb01a232007-11-29 23:46:42 +01001354 return sprintf(buf, "%ld\n", data->gpio_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355}
Yani Ioannou74880c02005-05-17 06:41:12 -04001356static ssize_t set_gpio_mask(struct device *dev, struct device_attribute *attr, const char *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 size_t count)
1358{
1359 struct i2c_client *client = to_i2c_client(dev);
1360 struct adm1026_data *data = i2c_get_clientdata(client);
1361 int val = simple_strtol(buf, NULL, 10);
Jean Delvarecb01a232007-11-29 23:46:42 +01001362 long mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001364 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 data->gpio_mask = val & 0x1ffff;
1366 mask = data->gpio_mask;
Jean Delvarecb01a232007-11-29 23:46:42 +01001367 adm1026_write_value(client, ADM1026_REG_GPIO_MASK_0_7, mask & 0xff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 mask >>= 8;
Jean Delvarecb01a232007-11-29 23:46:42 +01001369 adm1026_write_value(client, ADM1026_REG_GPIO_MASK_8_15, mask & 0xff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 mask = ((mask >> 1) & 0x80) | (data->alarm_mask >> 24 & 0x7f);
Jean Delvarecb01a232007-11-29 23:46:42 +01001371 adm1026_write_value(client, ADM1026_REG_MASK1, mask & 0xff);
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001372 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 return count;
1374}
1375
1376static DEVICE_ATTR(gpio_mask, S_IRUGO | S_IWUSR, show_gpio_mask, set_gpio_mask);
1377
Yani Ioannou74880c02005-05-17 06:41:12 -04001378static ssize_t show_pwm_reg(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379{
1380 struct adm1026_data *data = adm1026_update_device(dev);
Jean Delvarecb01a232007-11-29 23:46:42 +01001381 return sprintf(buf, "%d\n", PWM_FROM_REG(data->pwm1.pwm));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382}
Yani Ioannou74880c02005-05-17 06:41:12 -04001383static ssize_t set_pwm_reg(struct device *dev, struct device_attribute *attr, const char *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 size_t count)
1385{
1386 struct i2c_client *client = to_i2c_client(dev);
1387 struct adm1026_data *data = i2c_get_clientdata(client);
1388
1389 if (data->pwm1.enable == 1) {
1390 int val = simple_strtol(buf, NULL, 10);
1391
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001392 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 data->pwm1.pwm = PWM_TO_REG(val);
1394 adm1026_write_value(client, ADM1026_REG_PWM, data->pwm1.pwm);
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001395 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 }
1397 return count;
1398}
Yani Ioannou74880c02005-05-17 06:41:12 -04001399static ssize_t show_auto_pwm_min(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400{
1401 struct adm1026_data *data = adm1026_update_device(dev);
Jean Delvarecb01a232007-11-29 23:46:42 +01001402 return sprintf(buf, "%d\n", data->pwm1.auto_pwm_min);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403}
Yani Ioannou74880c02005-05-17 06:41:12 -04001404static ssize_t set_auto_pwm_min(struct device *dev, struct device_attribute *attr, const char *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 size_t count)
1406{
1407 struct i2c_client *client = to_i2c_client(dev);
1408 struct adm1026_data *data = i2c_get_clientdata(client);
1409 int val = simple_strtol(buf, NULL, 10);
1410
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001411 mutex_lock(&data->update_lock);
Jean Delvarecb01a232007-11-29 23:46:42 +01001412 data->pwm1.auto_pwm_min = SENSORS_LIMIT(val, 0, 255);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 if (data->pwm1.enable == 2) { /* apply immediately */
1414 data->pwm1.pwm = PWM_TO_REG((data->pwm1.pwm & 0x0f) |
Jean Delvarecb01a232007-11-29 23:46:42 +01001415 PWM_MIN_TO_REG(data->pwm1.auto_pwm_min));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 adm1026_write_value(client, ADM1026_REG_PWM, data->pwm1.pwm);
1417 }
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001418 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 return count;
1420}
Yani Ioannou74880c02005-05-17 06:41:12 -04001421static ssize_t show_auto_pwm_max(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422{
Jean Delvarecb01a232007-11-29 23:46:42 +01001423 return sprintf(buf, "%d\n", ADM1026_PWM_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424}
Yani Ioannou74880c02005-05-17 06:41:12 -04001425static ssize_t show_pwm_enable(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426{
1427 struct adm1026_data *data = adm1026_update_device(dev);
Jean Delvarecb01a232007-11-29 23:46:42 +01001428 return sprintf(buf, "%d\n", data->pwm1.enable);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429}
Yani Ioannou74880c02005-05-17 06:41:12 -04001430static ssize_t set_pwm_enable(struct device *dev, struct device_attribute *attr, const char *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 size_t count)
1432{
1433 struct i2c_client *client = to_i2c_client(dev);
1434 struct adm1026_data *data = i2c_get_clientdata(client);
1435 int val = simple_strtol(buf, NULL, 10);
Jean Delvarecb01a232007-11-29 23:46:42 +01001436 int old_enable;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437
1438 if ((val >= 0) && (val < 3)) {
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001439 mutex_lock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 old_enable = data->pwm1.enable;
1441 data->pwm1.enable = val;
1442 data->config1 = (data->config1 & ~CFG1_PWM_AFC)
1443 | ((val == 2) ? CFG1_PWM_AFC : 0);
1444 adm1026_write_value(client, ADM1026_REG_CONFIG1,
1445 data->config1);
Jean Delvarecb01a232007-11-29 23:46:42 +01001446 if (val == 2) { /* apply pwm1_auto_pwm_min to pwm1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 data->pwm1.pwm = PWM_TO_REG((data->pwm1.pwm & 0x0f) |
Jean Delvarecb01a232007-11-29 23:46:42 +01001448 PWM_MIN_TO_REG(data->pwm1.auto_pwm_min));
1449 adm1026_write_value(client, ADM1026_REG_PWM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 data->pwm1.pwm);
1451 } else if (!((old_enable == 1) && (val == 1))) {
1452 /* set pwm to safe value */
1453 data->pwm1.pwm = 255;
Jean Delvarecb01a232007-11-29 23:46:42 +01001454 adm1026_write_value(client, ADM1026_REG_PWM,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 data->pwm1.pwm);
1456 }
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001457 mutex_unlock(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 }
1459 return count;
1460}
1461
1462/* enable PWM fan control */
Jean Delvarecb01a232007-11-29 23:46:42 +01001463static DEVICE_ATTR(pwm1, S_IRUGO | S_IWUSR, show_pwm_reg, set_pwm_reg);
1464static DEVICE_ATTR(pwm2, S_IRUGO | S_IWUSR, show_pwm_reg, set_pwm_reg);
1465static DEVICE_ATTR(pwm3, S_IRUGO | S_IWUSR, show_pwm_reg, set_pwm_reg);
1466static DEVICE_ATTR(pwm1_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(pwm2_enable, S_IRUGO | S_IWUSR, show_pwm_enable,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 set_pwm_enable);
Jean Delvarecb01a232007-11-29 23:46:42 +01001470static DEVICE_ATTR(pwm3_enable, S_IRUGO | S_IWUSR, show_pwm_enable,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 set_pwm_enable);
Jean Delvarecb01a232007-11-29 23:46:42 +01001472static DEVICE_ATTR(temp1_auto_point1_pwm, S_IRUGO | S_IWUSR,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 show_auto_pwm_min, set_auto_pwm_min);
Jean Delvarecb01a232007-11-29 23:46:42 +01001474static DEVICE_ATTR(temp2_auto_point1_pwm, S_IRUGO | S_IWUSR,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 show_auto_pwm_min, set_auto_pwm_min);
Jean Delvarecb01a232007-11-29 23:46:42 +01001476static DEVICE_ATTR(temp3_auto_point1_pwm, S_IRUGO | S_IWUSR,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 show_auto_pwm_min, set_auto_pwm_min);
1478
1479static DEVICE_ATTR(temp1_auto_point2_pwm, S_IRUGO, show_auto_pwm_max, NULL);
1480static DEVICE_ATTR(temp2_auto_point2_pwm, S_IRUGO, show_auto_pwm_max, NULL);
1481static DEVICE_ATTR(temp3_auto_point2_pwm, S_IRUGO, show_auto_pwm_max, NULL);
1482
Mark M. Hoffman681c6f72006-09-24 21:15:35 +02001483static struct attribute *adm1026_attributes[] = {
1484 &sensor_dev_attr_in0_input.dev_attr.attr,
1485 &sensor_dev_attr_in0_max.dev_attr.attr,
1486 &sensor_dev_attr_in0_min.dev_attr.attr,
Jean Delvarea9273cb2007-11-29 23:45:22 +01001487 &sensor_dev_attr_in0_alarm.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +02001488 &sensor_dev_attr_in1_input.dev_attr.attr,
1489 &sensor_dev_attr_in1_max.dev_attr.attr,
1490 &sensor_dev_attr_in1_min.dev_attr.attr,
Jean Delvarea9273cb2007-11-29 23:45:22 +01001491 &sensor_dev_attr_in1_alarm.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +02001492 &sensor_dev_attr_in2_input.dev_attr.attr,
1493 &sensor_dev_attr_in2_max.dev_attr.attr,
1494 &sensor_dev_attr_in2_min.dev_attr.attr,
Jean Delvarea9273cb2007-11-29 23:45:22 +01001495 &sensor_dev_attr_in2_alarm.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +02001496 &sensor_dev_attr_in3_input.dev_attr.attr,
1497 &sensor_dev_attr_in3_max.dev_attr.attr,
1498 &sensor_dev_attr_in3_min.dev_attr.attr,
Jean Delvarea9273cb2007-11-29 23:45:22 +01001499 &sensor_dev_attr_in3_alarm.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +02001500 &sensor_dev_attr_in4_input.dev_attr.attr,
1501 &sensor_dev_attr_in4_max.dev_attr.attr,
1502 &sensor_dev_attr_in4_min.dev_attr.attr,
Jean Delvarea9273cb2007-11-29 23:45:22 +01001503 &sensor_dev_attr_in4_alarm.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +02001504 &sensor_dev_attr_in5_input.dev_attr.attr,
1505 &sensor_dev_attr_in5_max.dev_attr.attr,
1506 &sensor_dev_attr_in5_min.dev_attr.attr,
Jean Delvarea9273cb2007-11-29 23:45:22 +01001507 &sensor_dev_attr_in5_alarm.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +02001508 &sensor_dev_attr_in6_input.dev_attr.attr,
1509 &sensor_dev_attr_in6_max.dev_attr.attr,
1510 &sensor_dev_attr_in6_min.dev_attr.attr,
Jean Delvarea9273cb2007-11-29 23:45:22 +01001511 &sensor_dev_attr_in6_alarm.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +02001512 &sensor_dev_attr_in7_input.dev_attr.attr,
1513 &sensor_dev_attr_in7_max.dev_attr.attr,
1514 &sensor_dev_attr_in7_min.dev_attr.attr,
Jean Delvarea9273cb2007-11-29 23:45:22 +01001515 &sensor_dev_attr_in7_alarm.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +02001516 &sensor_dev_attr_in8_input.dev_attr.attr,
1517 &sensor_dev_attr_in8_max.dev_attr.attr,
1518 &sensor_dev_attr_in8_min.dev_attr.attr,
Jean Delvarea9273cb2007-11-29 23:45:22 +01001519 &sensor_dev_attr_in8_alarm.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +02001520 &sensor_dev_attr_in9_input.dev_attr.attr,
1521 &sensor_dev_attr_in9_max.dev_attr.attr,
1522 &sensor_dev_attr_in9_min.dev_attr.attr,
Jean Delvarea9273cb2007-11-29 23:45:22 +01001523 &sensor_dev_attr_in9_alarm.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +02001524 &sensor_dev_attr_in10_input.dev_attr.attr,
1525 &sensor_dev_attr_in10_max.dev_attr.attr,
1526 &sensor_dev_attr_in10_min.dev_attr.attr,
Jean Delvarea9273cb2007-11-29 23:45:22 +01001527 &sensor_dev_attr_in10_alarm.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +02001528 &sensor_dev_attr_in11_input.dev_attr.attr,
1529 &sensor_dev_attr_in11_max.dev_attr.attr,
1530 &sensor_dev_attr_in11_min.dev_attr.attr,
Jean Delvarea9273cb2007-11-29 23:45:22 +01001531 &sensor_dev_attr_in11_alarm.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +02001532 &sensor_dev_attr_in12_input.dev_attr.attr,
1533 &sensor_dev_attr_in12_max.dev_attr.attr,
1534 &sensor_dev_attr_in12_min.dev_attr.attr,
Jean Delvarea9273cb2007-11-29 23:45:22 +01001535 &sensor_dev_attr_in12_alarm.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +02001536 &sensor_dev_attr_in13_input.dev_attr.attr,
1537 &sensor_dev_attr_in13_max.dev_attr.attr,
1538 &sensor_dev_attr_in13_min.dev_attr.attr,
Jean Delvarea9273cb2007-11-29 23:45:22 +01001539 &sensor_dev_attr_in13_alarm.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +02001540 &sensor_dev_attr_in14_input.dev_attr.attr,
1541 &sensor_dev_attr_in14_max.dev_attr.attr,
1542 &sensor_dev_attr_in14_min.dev_attr.attr,
Jean Delvarea9273cb2007-11-29 23:45:22 +01001543 &sensor_dev_attr_in14_alarm.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +02001544 &sensor_dev_attr_in15_input.dev_attr.attr,
1545 &sensor_dev_attr_in15_max.dev_attr.attr,
1546 &sensor_dev_attr_in15_min.dev_attr.attr,
Jean Delvarea9273cb2007-11-29 23:45:22 +01001547 &sensor_dev_attr_in15_alarm.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +02001548 &sensor_dev_attr_in16_input.dev_attr.attr,
1549 &sensor_dev_attr_in16_max.dev_attr.attr,
1550 &sensor_dev_attr_in16_min.dev_attr.attr,
Jean Delvarea9273cb2007-11-29 23:45:22 +01001551 &sensor_dev_attr_in16_alarm.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +02001552 &sensor_dev_attr_fan1_input.dev_attr.attr,
1553 &sensor_dev_attr_fan1_div.dev_attr.attr,
1554 &sensor_dev_attr_fan1_min.dev_attr.attr,
Jean Delvarea9273cb2007-11-29 23:45:22 +01001555 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +02001556 &sensor_dev_attr_fan2_input.dev_attr.attr,
1557 &sensor_dev_attr_fan2_div.dev_attr.attr,
1558 &sensor_dev_attr_fan2_min.dev_attr.attr,
Jean Delvarea9273cb2007-11-29 23:45:22 +01001559 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +02001560 &sensor_dev_attr_fan3_input.dev_attr.attr,
1561 &sensor_dev_attr_fan3_div.dev_attr.attr,
1562 &sensor_dev_attr_fan3_min.dev_attr.attr,
Jean Delvarea9273cb2007-11-29 23:45:22 +01001563 &sensor_dev_attr_fan3_alarm.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +02001564 &sensor_dev_attr_fan4_input.dev_attr.attr,
1565 &sensor_dev_attr_fan4_div.dev_attr.attr,
1566 &sensor_dev_attr_fan4_min.dev_attr.attr,
Jean Delvarea9273cb2007-11-29 23:45:22 +01001567 &sensor_dev_attr_fan4_alarm.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +02001568 &sensor_dev_attr_fan5_input.dev_attr.attr,
1569 &sensor_dev_attr_fan5_div.dev_attr.attr,
1570 &sensor_dev_attr_fan5_min.dev_attr.attr,
Jean Delvarea9273cb2007-11-29 23:45:22 +01001571 &sensor_dev_attr_fan5_alarm.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +02001572 &sensor_dev_attr_fan6_input.dev_attr.attr,
1573 &sensor_dev_attr_fan6_div.dev_attr.attr,
1574 &sensor_dev_attr_fan6_min.dev_attr.attr,
Jean Delvarea9273cb2007-11-29 23:45:22 +01001575 &sensor_dev_attr_fan6_alarm.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +02001576 &sensor_dev_attr_fan7_input.dev_attr.attr,
1577 &sensor_dev_attr_fan7_div.dev_attr.attr,
1578 &sensor_dev_attr_fan7_min.dev_attr.attr,
Jean Delvarea9273cb2007-11-29 23:45:22 +01001579 &sensor_dev_attr_fan7_alarm.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +02001580 &sensor_dev_attr_fan8_input.dev_attr.attr,
1581 &sensor_dev_attr_fan8_div.dev_attr.attr,
1582 &sensor_dev_attr_fan8_min.dev_attr.attr,
Jean Delvarea9273cb2007-11-29 23:45:22 +01001583 &sensor_dev_attr_fan8_alarm.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +02001584 &sensor_dev_attr_temp1_input.dev_attr.attr,
1585 &sensor_dev_attr_temp1_max.dev_attr.attr,
1586 &sensor_dev_attr_temp1_min.dev_attr.attr,
Jean Delvarea9273cb2007-11-29 23:45:22 +01001587 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +02001588 &sensor_dev_attr_temp2_input.dev_attr.attr,
1589 &sensor_dev_attr_temp2_max.dev_attr.attr,
1590 &sensor_dev_attr_temp2_min.dev_attr.attr,
Jean Delvarea9273cb2007-11-29 23:45:22 +01001591 &sensor_dev_attr_temp2_alarm.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +02001592 &sensor_dev_attr_temp3_input.dev_attr.attr,
1593 &sensor_dev_attr_temp3_max.dev_attr.attr,
1594 &sensor_dev_attr_temp3_min.dev_attr.attr,
Jean Delvarea9273cb2007-11-29 23:45:22 +01001595 &sensor_dev_attr_temp3_alarm.dev_attr.attr,
Mark M. Hoffman681c6f72006-09-24 21:15:35 +02001596 &sensor_dev_attr_temp1_offset.dev_attr.attr,
1597 &sensor_dev_attr_temp2_offset.dev_attr.attr,
1598 &sensor_dev_attr_temp3_offset.dev_attr.attr,
1599 &sensor_dev_attr_temp1_auto_point1_temp.dev_attr.attr,
1600 &sensor_dev_attr_temp2_auto_point1_temp.dev_attr.attr,
1601 &sensor_dev_attr_temp3_auto_point1_temp.dev_attr.attr,
1602 &sensor_dev_attr_temp1_auto_point1_temp_hyst.dev_attr.attr,
1603 &sensor_dev_attr_temp2_auto_point1_temp_hyst.dev_attr.attr,
1604 &sensor_dev_attr_temp3_auto_point1_temp_hyst.dev_attr.attr,
1605 &sensor_dev_attr_temp1_auto_point2_temp.dev_attr.attr,
1606 &sensor_dev_attr_temp2_auto_point2_temp.dev_attr.attr,
1607 &sensor_dev_attr_temp3_auto_point2_temp.dev_attr.attr,
1608 &sensor_dev_attr_temp1_crit.dev_attr.attr,
1609 &sensor_dev_attr_temp2_crit.dev_attr.attr,
1610 &sensor_dev_attr_temp3_crit.dev_attr.attr,
1611 &dev_attr_temp1_crit_enable.attr,
1612 &dev_attr_temp2_crit_enable.attr,
1613 &dev_attr_temp3_crit_enable.attr,
1614 &dev_attr_cpu0_vid.attr,
1615 &dev_attr_vrm.attr,
1616 &dev_attr_alarms.attr,
1617 &dev_attr_alarm_mask.attr,
1618 &dev_attr_gpio.attr,
1619 &dev_attr_gpio_mask.attr,
1620 &dev_attr_pwm1.attr,
1621 &dev_attr_pwm2.attr,
1622 &dev_attr_pwm3.attr,
1623 &dev_attr_pwm1_enable.attr,
1624 &dev_attr_pwm2_enable.attr,
1625 &dev_attr_pwm3_enable.attr,
1626 &dev_attr_temp1_auto_point1_pwm.attr,
1627 &dev_attr_temp2_auto_point1_pwm.attr,
1628 &dev_attr_temp3_auto_point1_pwm.attr,
1629 &dev_attr_temp1_auto_point2_pwm.attr,
1630 &dev_attr_temp2_auto_point2_pwm.attr,
1631 &dev_attr_temp3_auto_point2_pwm.attr,
1632 &dev_attr_analog_out.attr,
1633 NULL
1634};
1635
1636static const struct attribute_group adm1026_group = {
1637 .attrs = adm1026_attributes,
1638};
1639
Ben Dooksc49efce2005-10-26 21:07:25 +02001640static int adm1026_detect(struct i2c_adapter *adapter, int address,
1641 int kind)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642{
1643 int company, verstep;
1644 struct i2c_client *new_client;
1645 struct adm1026_data *data;
1646 int err = 0;
1647 const char *type_name = "";
1648
1649 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
1650 /* We need to be able to do byte I/O */
1651 goto exit;
1652 };
1653
1654 /* OK. For now, we presume we have a valid client. We now create the
1655 client structure, even though we cannot fill it completely yet.
1656 But it allows us to access adm1026_{read,write}_value. */
1657
Deepak Saxenaba9c2e82005-10-17 23:08:32 +02001658 if (!(data = kzalloc(sizeof(struct adm1026_data), GFP_KERNEL))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 err = -ENOMEM;
1660 goto exit;
1661 }
1662
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 new_client = &data->client;
1664 i2c_set_clientdata(new_client, data);
1665 new_client->addr = address;
1666 new_client->adapter = adapter;
1667 new_client->driver = &adm1026_driver;
1668 new_client->flags = 0;
1669
1670 /* Now, we do the remaining detection. */
1671
1672 company = adm1026_read_value(new_client, ADM1026_REG_COMPANY);
1673 verstep = adm1026_read_value(new_client, ADM1026_REG_VERSTEP);
1674
1675 dev_dbg(&new_client->dev, "Detecting device at %d,0x%02x with"
1676 " COMPANY: 0x%02x and VERSTEP: 0x%02x\n",
1677 i2c_adapter_id(new_client->adapter), new_client->addr,
1678 company, verstep);
1679
1680 /* If auto-detecting, Determine the chip type. */
1681 if (kind <= 0) {
1682 dev_dbg(&new_client->dev, "Autodetecting device at %d,0x%02x "
1683 "...\n", i2c_adapter_id(adapter), address);
1684 if (company == ADM1026_COMPANY_ANALOG_DEV
1685 && verstep == ADM1026_VERSTEP_ADM1026) {
1686 kind = adm1026;
1687 } else if (company == ADM1026_COMPANY_ANALOG_DEV
1688 && (verstep & 0xf0) == ADM1026_VERSTEP_GENERIC) {
1689 dev_err(&adapter->dev, ": Unrecognized stepping "
1690 "0x%02x. Defaulting to ADM1026.\n", verstep);
1691 kind = adm1026;
1692 } else if ((verstep & 0xf0) == ADM1026_VERSTEP_GENERIC) {
1693 dev_err(&adapter->dev, ": Found version/stepping "
1694 "0x%02x. Assuming generic ADM1026.\n",
1695 verstep);
1696 kind = any_chip;
1697 } else {
1698 dev_dbg(&new_client->dev, ": Autodetection "
1699 "failed\n");
1700 /* Not an ADM1026 ... */
Jean Delvarecb01a232007-11-29 23:46:42 +01001701 if (kind == 0) { /* User used force=x,y */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 dev_err(&adapter->dev, "Generic ADM1026 not "
1703 "found at %d,0x%02x. Try "
1704 "force_adm1026.\n",
1705 i2c_adapter_id(adapter), address);
1706 }
1707 err = 0;
1708 goto exitfree;
1709 }
1710 }
1711
1712 /* Fill in the chip specific driver values */
1713 switch (kind) {
1714 case any_chip :
1715 type_name = "adm1026";
1716 break;
1717 case adm1026 :
1718 type_name = "adm1026";
1719 break;
1720 default :
1721 dev_err(&adapter->dev, ": Internal error, invalid "
Joe Perches898eb712007-10-18 03:06:30 -07001722 "kind (%d)!\n", kind);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 err = -EFAULT;
1724 goto exitfree;
1725 }
1726 strlcpy(new_client->name, type_name, I2C_NAME_SIZE);
1727
1728 /* Fill in the remaining client fields */
1729 data->type = kind;
1730 data->valid = 0;
Ingo Molnar9a61bf62006-01-18 23:19:26 +01001731 mutex_init(&data->update_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732
1733 /* Tell the I2C layer a new client has arrived */
1734 if ((err = i2c_attach_client(new_client)))
1735 goto exitfree;
1736
1737 /* Set the VRM version */
Jean Delvare303760b2005-07-31 21:52:01 +02001738 data->vrm = vid_which_vrm();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739
1740 /* Initialize the ADM1026 chip */
1741 adm1026_init_client(new_client);
1742
1743 /* Register sysfs hooks */
Mark M. Hoffman681c6f72006-09-24 21:15:35 +02001744 if ((err = sysfs_create_group(&new_client->dev.kobj, &adm1026_group)))
1745 goto exitdetach;
1746
Tony Jones1beeffe2007-08-20 13:46:20 -07001747 data->hwmon_dev = hwmon_device_register(&new_client->dev);
1748 if (IS_ERR(data->hwmon_dev)) {
1749 err = PTR_ERR(data->hwmon_dev);
Mark M. Hoffman681c6f72006-09-24 21:15:35 +02001750 goto exitremove;
Mark M. Hoffman943b0832005-07-15 21:39:18 -04001751 }
1752
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 return 0;
1754
1755 /* Error out and cleanup code */
Mark M. Hoffman681c6f72006-09-24 21:15:35 +02001756exitremove:
1757 sysfs_remove_group(&new_client->dev.kobj, &adm1026_group);
Mark M. Hoffman943b0832005-07-15 21:39:18 -04001758exitdetach:
1759 i2c_detach_client(new_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760exitfree:
Alexey Dobriyan1f57ff82005-08-26 01:49:14 +04001761 kfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762exit:
1763 return err;
1764}
Mark M. Hoffman681c6f72006-09-24 21:15:35 +02001765
1766static int adm1026_detach_client(struct i2c_client *client)
1767{
1768 struct adm1026_data *data = i2c_get_clientdata(client);
Tony Jones1beeffe2007-08-20 13:46:20 -07001769 hwmon_device_unregister(data->hwmon_dev);
Mark M. Hoffman681c6f72006-09-24 21:15:35 +02001770 sysfs_remove_group(&client->dev.kobj, &adm1026_group);
1771 i2c_detach_client(client);
1772 kfree(data);
1773 return 0;
1774}
1775
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776static int __init sm_adm1026_init(void)
1777{
1778 return i2c_add_driver(&adm1026_driver);
1779}
1780
Jean Delvarecb01a232007-11-29 23:46:42 +01001781static void __exit sm_adm1026_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782{
1783 i2c_del_driver(&adm1026_driver);
1784}
1785
1786MODULE_LICENSE("GPL");
1787MODULE_AUTHOR("Philip Pokorny <ppokorny@penguincomputing.com>, "
Jean Delvarecb01a232007-11-29 23:46:42 +01001788 "Justin Thiessen <jthiessen@penguincomputing.com>");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789MODULE_DESCRIPTION("ADM1026 driver");
1790
1791module_init(sm_adm1026_init);
1792module_exit(sm_adm1026_exit);