Merge git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[wrt350n-kernel.git] / drivers / hwmon / adm1026.c
blobd6aa620c4819983559a71f84bd0005fac43cd733
1 /*
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>
7 Chip details at:
9 <http://www.analog.com/UploadedFiles/Data_Sheets/779263102ADM1026_a.pdf>
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.
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.
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.
26 #include <linux/module.h>
27 #include <linux/init.h>
28 #include <linux/slab.h>
29 #include <linux/jiffies.h>
30 #include <linux/i2c.h>
31 #include <linux/hwmon.h>
32 #include <linux/hwmon-sysfs.h>
33 #include <linux/hwmon-vid.h>
34 #include <linux/err.h>
35 #include <linux/mutex.h>
37 /* Addresses to scan */
38 <<<<<<< HEAD:drivers/hwmon/adm1026.c
39 static unsigned short normal_i2c[] = { 0x2c, 0x2d, 0x2e, I2C_CLIENT_END };
40 =======
41 static const unsigned short normal_i2c[] = { 0x2c, 0x2d, 0x2e, I2C_CLIENT_END };
42 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/hwmon/adm1026.c
44 /* Insmod parameters */
45 I2C_CLIENT_INSMOD_1(adm1026);
47 static int gpio_input[17] = { -1, -1, -1, -1, -1, -1, -1, -1, -1,
48 -1, -1, -1, -1, -1, -1, -1, -1 };
49 static int gpio_output[17] = { -1, -1, -1, -1, -1, -1, -1, -1, -1,
50 -1, -1, -1, -1, -1, -1, -1, -1 };
51 static int gpio_inverted[17] = { -1, -1, -1, -1, -1, -1, -1, -1, -1,
52 -1, -1, -1, -1, -1, -1, -1, -1 };
53 static int gpio_normal[17] = { -1, -1, -1, -1, -1, -1, -1, -1, -1,
54 -1, -1, -1, -1, -1, -1, -1, -1 };
55 static int gpio_fan[8] = { -1, -1, -1, -1, -1, -1, -1, -1 };
56 module_param_array(gpio_input, int, NULL, 0);
57 MODULE_PARM_DESC(gpio_input, "List of GPIO pins (0-16) to program as inputs");
58 module_param_array(gpio_output, int, NULL, 0);
59 MODULE_PARM_DESC(gpio_output, "List of GPIO pins (0-16) to program as "
60 "outputs");
61 module_param_array(gpio_inverted, int, NULL, 0);
62 MODULE_PARM_DESC(gpio_inverted, "List of GPIO pins (0-16) to program as "
63 "inverted");
64 module_param_array(gpio_normal, int, NULL, 0);
65 MODULE_PARM_DESC(gpio_normal, "List of GPIO pins (0-16) to program as "
66 "normal/non-inverted");
67 module_param_array(gpio_fan, int, NULL, 0);
68 MODULE_PARM_DESC(gpio_fan, "List of GPIO pins (0-7) to program as fan tachs");
70 /* Many ADM1026 constants specified below */
72 /* The ADM1026 registers */
73 #define ADM1026_REG_CONFIG1 0x00
74 #define CFG1_MONITOR 0x01
75 #define CFG1_INT_ENABLE 0x02
76 #define CFG1_INT_CLEAR 0x04
77 #define CFG1_AIN8_9 0x08
78 #define CFG1_THERM_HOT 0x10
79 #define CFG1_DAC_AFC 0x20
80 #define CFG1_PWM_AFC 0x40
81 #define CFG1_RESET 0x80
83 #define ADM1026_REG_CONFIG2 0x01
84 /* CONFIG2 controls FAN0/GPIO0 through FAN7/GPIO7 */
86 #define ADM1026_REG_CONFIG3 0x07
87 #define CFG3_GPIO16_ENABLE 0x01
88 #define CFG3_CI_CLEAR 0x02
89 #define CFG3_VREF_250 0x04
90 #define CFG3_GPIO16_DIR 0x40
91 #define CFG3_GPIO16_POL 0x80
93 #define ADM1026_REG_E2CONFIG 0x13
94 #define E2CFG_READ 0x01
95 #define E2CFG_WRITE 0x02
96 #define E2CFG_ERASE 0x04
97 #define E2CFG_ROM 0x08
98 #define E2CFG_CLK_EXT 0x80
100 /* There are 10 general analog inputs and 7 dedicated inputs
101 * They are:
102 * 0 - 9 = AIN0 - AIN9
103 * 10 = Vbat
104 * 11 = 3.3V Standby
105 * 12 = 3.3V Main
106 * 13 = +5V
107 * 14 = Vccp (CPU core voltage)
108 * 15 = +12V
109 * 16 = -12V
111 static u16 ADM1026_REG_IN[] = {
112 0x30, 0x31, 0x32, 0x33, 0x34, 0x35,
113 0x36, 0x37, 0x27, 0x29, 0x26, 0x2a,
114 0x2b, 0x2c, 0x2d, 0x2e, 0x2f
116 static u16 ADM1026_REG_IN_MIN[] = {
117 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d,
118 0x5e, 0x5f, 0x6d, 0x49, 0x6b, 0x4a,
119 0x4b, 0x4c, 0x4d, 0x4e, 0x4f
121 static u16 ADM1026_REG_IN_MAX[] = {
122 0x50, 0x51, 0x52, 0x53, 0x54, 0x55,
123 0x56, 0x57, 0x6c, 0x41, 0x6a, 0x42,
124 0x43, 0x44, 0x45, 0x46, 0x47
127 /* Temperatures are:
128 * 0 - Internal
129 * 1 - External 1
130 * 2 - External 2
132 static u16 ADM1026_REG_TEMP[] = { 0x1f, 0x28, 0x29 };
133 static u16 ADM1026_REG_TEMP_MIN[] = { 0x69, 0x48, 0x49 };
134 static u16 ADM1026_REG_TEMP_MAX[] = { 0x68, 0x40, 0x41 };
135 static u16 ADM1026_REG_TEMP_TMIN[] = { 0x10, 0x11, 0x12 };
136 static u16 ADM1026_REG_TEMP_THERM[] = { 0x0d, 0x0e, 0x0f };
137 static u16 ADM1026_REG_TEMP_OFFSET[] = { 0x1e, 0x6e, 0x6f };
139 #define ADM1026_REG_FAN(nr) (0x38 + (nr))
140 #define ADM1026_REG_FAN_MIN(nr) (0x60 + (nr))
141 #define ADM1026_REG_FAN_DIV_0_3 0x02
142 #define ADM1026_REG_FAN_DIV_4_7 0x03
144 #define ADM1026_REG_DAC 0x04
145 #define ADM1026_REG_PWM 0x05
147 #define ADM1026_REG_GPIO_CFG_0_3 0x08
148 #define ADM1026_REG_GPIO_CFG_4_7 0x09
149 #define ADM1026_REG_GPIO_CFG_8_11 0x0a
150 #define ADM1026_REG_GPIO_CFG_12_15 0x0b
151 /* CFG_16 in REG_CFG3 */
152 #define ADM1026_REG_GPIO_STATUS_0_7 0x24
153 #define ADM1026_REG_GPIO_STATUS_8_15 0x25
154 /* STATUS_16 in REG_STATUS4 */
155 #define ADM1026_REG_GPIO_MASK_0_7 0x1c
156 #define ADM1026_REG_GPIO_MASK_8_15 0x1d
157 /* MASK_16 in REG_MASK4 */
159 #define ADM1026_REG_COMPANY 0x16
160 #define ADM1026_REG_VERSTEP 0x17
161 /* These are the recognized values for the above regs */
162 #define ADM1026_COMPANY_ANALOG_DEV 0x41
163 #define ADM1026_VERSTEP_GENERIC 0x40
164 #define ADM1026_VERSTEP_ADM1026 0x44
166 #define ADM1026_REG_MASK1 0x18
167 #define ADM1026_REG_MASK2 0x19
168 #define ADM1026_REG_MASK3 0x1a
169 #define ADM1026_REG_MASK4 0x1b
171 #define ADM1026_REG_STATUS1 0x20
172 #define ADM1026_REG_STATUS2 0x21
173 #define ADM1026_REG_STATUS3 0x22
174 #define ADM1026_REG_STATUS4 0x23
176 #define ADM1026_FAN_ACTIVATION_TEMP_HYST -6
177 #define ADM1026_FAN_CONTROL_TEMP_RANGE 20
178 #define ADM1026_PWM_MAX 255
180 /* Conversions. Rounding and limit checking is only done on the TO_REG
181 * variants. Note that you should be a bit careful with which arguments
182 * these macros are called: arguments may be evaluated more than once.
185 /* IN are scaled acording to built-in resistors. These are the
186 * voltages corresponding to 3/4 of full scale (192 or 0xc0)
187 * NOTE: The -12V input needs an additional factor to account
188 * for the Vref pullup resistor.
189 * NEG12_OFFSET = SCALE * Vref / V-192 - Vref
190 * = 13875 * 2.50 / 1.875 - 2500
191 * = 16000
193 * The values in this table are based on Table II, page 15 of the
194 * datasheet.
196 static int adm1026_scaling[] = { /* .001 Volts */
197 2250, 2250, 2250, 2250, 2250, 2250,
198 1875, 1875, 1875, 1875, 3000, 3330,
199 3330, 4995, 2250, 12000, 13875
201 #define NEG12_OFFSET 16000
202 #define SCALE(val, from, to) (((val)*(to) + ((from)/2))/(from))
203 #define INS_TO_REG(n, val) (SENSORS_LIMIT(SCALE(val, adm1026_scaling[n], 192),\
204 0, 255))
205 #define INS_FROM_REG(n, val) (SCALE(val, 192, adm1026_scaling[n]))
207 /* FAN speed is measured using 22.5kHz clock and counts for 2 pulses
208 * and we assume a 2 pulse-per-rev fan tach signal
209 * 22500 kHz * 60 (sec/min) * 2 (pulse) / 2 (pulse/rev) == 1350000
211 #define FAN_TO_REG(val, div) ((val) <= 0 ? 0xff : \
212 SENSORS_LIMIT(1350000/((val)*(div)), 1, 254))
213 #define FAN_FROM_REG(val, div) ((val) == 0 ? -1:(val) == 0xff ? 0 : \
214 1350000/((val)*(div)))
215 #define DIV_FROM_REG(val) (1<<(val))
216 #define DIV_TO_REG(val) ((val) >= 8 ? 3 : (val) >= 4 ? 2 : (val) >= 2 ? 1 : 0)
218 /* Temperature is reported in 1 degC increments */
219 #define TEMP_TO_REG(val) (SENSORS_LIMIT(((val)+((val)<0 ? -500 : 500))/1000,\
220 -127, 127))
221 #define TEMP_FROM_REG(val) ((val) * 1000)
222 #define OFFSET_TO_REG(val) (SENSORS_LIMIT(((val)+((val)<0 ? -500 : 500))/1000,\
223 -127, 127))
224 #define OFFSET_FROM_REG(val) ((val) * 1000)
226 #define PWM_TO_REG(val) (SENSORS_LIMIT(val, 0, 255))
227 #define PWM_FROM_REG(val) (val)
229 #define PWM_MIN_TO_REG(val) ((val) & 0xf0)
230 #define PWM_MIN_FROM_REG(val) (((val) & 0xf0) + ((val) >> 4))
232 /* Analog output is a voltage, and scaled to millivolts. The datasheet
233 * indicates that the DAC could be used to drive the fans, but in our
234 * example board (Arima HDAMA) it isn't connected to the fans at all.
236 #define DAC_TO_REG(val) (SENSORS_LIMIT(((((val)*255)+500)/2500), 0, 255))
237 #define DAC_FROM_REG(val) (((val)*2500)/255)
239 /* Chip sampling rates
241 * Some sensors are not updated more frequently than once per second
242 * so it doesn't make sense to read them more often than that.
243 * We cache the results and return the saved data if the driver
244 * is called again before a second has elapsed.
246 * Also, there is significant configuration data for this chip
247 * So, we keep the config data up to date in the cache
248 * when it is written and only sample it once every 5 *minutes*
250 #define ADM1026_DATA_INTERVAL (1 * HZ)
251 #define ADM1026_CONFIG_INTERVAL (5 * 60 * HZ)
253 /* We allow for multiple chips in a single system.
255 * For each registered ADM1026, we need to keep state information
256 * at client->data. The adm1026_data structure is dynamically
257 * allocated, when a new client structure is allocated. */
259 struct pwm_data {
260 u8 pwm;
261 u8 enable;
262 u8 auto_pwm_min;
265 struct adm1026_data {
266 struct i2c_client client;
267 struct device *hwmon_dev;
269 struct mutex update_lock;
270 int valid; /* !=0 if following fields are valid */
271 unsigned long last_reading; /* In jiffies */
272 unsigned long last_config; /* In jiffies */
274 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 */
289 u8 analog_out; /* Register value (DAC) */
290 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 */
300 static int adm1026_attach_adapter(struct i2c_adapter *adapter);
301 static int adm1026_detect(struct i2c_adapter *adapter, int address,
302 int kind);
303 static int adm1026_detach_client(struct i2c_client *client);
304 static int adm1026_read_value(struct i2c_client *client, u8 reg);
305 static int adm1026_write_value(struct i2c_client *client, u8 reg, int value);
306 static void adm1026_print_gpio(struct i2c_client *client);
307 static void adm1026_fixup_gpio(struct i2c_client *client);
308 static struct adm1026_data *adm1026_update_device(struct device *dev);
309 static void adm1026_init_client(struct i2c_client *client);
312 static struct i2c_driver adm1026_driver = {
313 .driver = {
314 .name = "adm1026",
316 .attach_adapter = adm1026_attach_adapter,
317 .detach_client = adm1026_detach_client,
320 static int adm1026_attach_adapter(struct i2c_adapter *adapter)
322 if (!(adapter->class & I2C_CLASS_HWMON)) {
323 return 0;
325 return i2c_probe(adapter, &addr_data, adm1026_detect);
328 static int adm1026_read_value(struct i2c_client *client, u8 reg)
330 int res;
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;
339 return res;
342 static int adm1026_write_value(struct i2c_client *client, u8 reg, int value)
344 int res;
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;
353 return res;
356 static void adm1026_init_client(struct i2c_client *client)
358 int value, i;
359 struct adm1026_data *data = i2c_get_clientdata(client);
361 dev_dbg(&client->dev, "Initializing device\n");
362 /* 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);
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");
374 if (data->config1 & CFG1_INT_ENABLE) {
375 dev_dbg(&client->dev, "SMBALERT interrupts are "
376 "enabled.\n");
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");
385 if (data->config1 & CFG1_THERM_HOT) {
386 dev_dbg(&client->dev, "Automatic THERM, PWM, "
387 "and temp limits enabled.\n");
390 if (data->config3 & CFG3_GPIO16_ENABLE) {
391 dev_dbg(&client->dev, "GPIO16 enabled. THERM "
392 "pin disabled.\n");
393 } else {
394 dev_dbg(&client->dev, "THERM pin enabled. "
395 "GPIO16 disabled.\n");
397 if (data->config3 & CFG3_VREF_250) {
398 dev_dbg(&client->dev, "Vref is 2.50 Volts.\n");
399 } else {
400 dev_dbg(&client->dev, "Vref is 1.82 Volts.\n");
402 /* Read and pick apart the existing GPIO configuration */
403 value = 0;
404 for (i = 0;i <= 15;++i) {
405 if ((i & 0x03) == 0) {
406 value = adm1026_read_value(client,
407 ADM1026_REG_GPIO_CFG_0_3 + i/4);
409 data->gpio_config[i] = value & 0x03;
410 value >>= 2;
412 data->gpio_config[16] = (data->config3 >> 6) & 0x03;
414 /* ... and then print it */
415 adm1026_print_gpio(client);
417 /* If the user asks us to reprogram the GPIO config, then
418 * do it now.
420 if (gpio_input[0] != -1 || gpio_output[0] != -1
421 || gpio_inverted[0] != -1 || gpio_normal[0] != -1
422 || gpio_fan[0] != -1) {
423 adm1026_fixup_gpio(client);
426 /* WE INTENTIONALLY make no changes to the limits,
427 * offsets, pwms, fans and zones. If they were
428 * configured, we don't want to mess with them.
429 * If they weren't, the default is 100% PWM, no
430 * control and will suffice until 'sensors -s'
431 * can be run by the user. We DO set the default
432 * value for pwm1.auto_pwm_min to its maximum
433 * so that enabling automatic pwm fan control
434 * without first setting a value for pwm1.auto_pwm_min
435 * will not result in potentially dangerous fan speed decrease.
437 data->pwm1.auto_pwm_min=255;
438 /* Start monitoring */
439 value = adm1026_read_value(client, ADM1026_REG_CONFIG1);
440 /* Set MONITOR, clear interrupt acknowledge and s/w reset */
441 value = (value | CFG1_MONITOR) & (~CFG1_INT_CLEAR & ~CFG1_RESET);
442 dev_dbg(&client->dev, "Setting CONFIG to: 0x%02x\n", value);
443 data->config1 = value;
444 adm1026_write_value(client, ADM1026_REG_CONFIG1, value);
446 /* initialize fan_div[] to hardware defaults */
447 value = adm1026_read_value(client, ADM1026_REG_FAN_DIV_0_3) |
448 (adm1026_read_value(client, ADM1026_REG_FAN_DIV_4_7) << 8);
449 for (i = 0;i <= 7;++i) {
450 data->fan_div[i] = DIV_FROM_REG(value & 0x03);
451 value >>= 2;
455 static void adm1026_print_gpio(struct i2c_client *client)
457 struct adm1026_data *data = i2c_get_clientdata(client);
458 int i;
460 dev_dbg(&client->dev, "GPIO config is:");
461 for (i = 0;i <= 7;++i) {
462 if (data->config2 & (1 << i)) {
463 dev_dbg(&client->dev, "\t%sGP%s%d\n",
464 data->gpio_config[i] & 0x02 ? "" : "!",
465 data->gpio_config[i] & 0x01 ? "OUT" : "IN",
467 } else {
468 dev_dbg(&client->dev, "\tFAN%d\n", i);
471 for (i = 8;i <= 15;++i) {
472 dev_dbg(&client->dev, "\t%sGP%s%d\n",
473 data->gpio_config[i] & 0x02 ? "" : "!",
474 data->gpio_config[i] & 0x01 ? "OUT" : "IN",
477 if (data->config3 & CFG3_GPIO16_ENABLE) {
478 dev_dbg(&client->dev, "\t%sGP%s16\n",
479 data->gpio_config[16] & 0x02 ? "" : "!",
480 data->gpio_config[16] & 0x01 ? "OUT" : "IN");
481 } else {
482 /* GPIO16 is THERM */
483 dev_dbg(&client->dev, "\tTHERM\n");
487 static void adm1026_fixup_gpio(struct i2c_client *client)
489 struct adm1026_data *data = i2c_get_clientdata(client);
490 int i;
491 int value;
493 /* Make the changes requested. */
494 /* We may need to unlock/stop monitoring or soft-reset the
495 * chip before we can make changes. This hasn't been
496 * tested much. FIXME
499 /* Make outputs */
500 for (i = 0;i <= 16;++i) {
501 if (gpio_output[i] >= 0 && gpio_output[i] <= 16) {
502 data->gpio_config[gpio_output[i]] |= 0x01;
504 /* if GPIO0-7 is output, it isn't a FAN tach */
505 if (gpio_output[i] >= 0 && gpio_output[i] <= 7) {
506 data->config2 |= 1 << gpio_output[i];
510 /* Input overrides output */
511 for (i = 0;i <= 16;++i) {
512 if (gpio_input[i] >= 0 && gpio_input[i] <= 16) {
513 data->gpio_config[gpio_input[i]] &= ~ 0x01;
515 /* if GPIO0-7 is input, it isn't a FAN tach */
516 if (gpio_input[i] >= 0 && gpio_input[i] <= 7) {
517 data->config2 |= 1 << gpio_input[i];
521 /* Inverted */
522 for (i = 0;i <= 16;++i) {
523 if (gpio_inverted[i] >= 0 && gpio_inverted[i] <= 16) {
524 data->gpio_config[gpio_inverted[i]] &= ~ 0x02;
528 /* Normal overrides inverted */
529 for (i = 0;i <= 16;++i) {
530 if (gpio_normal[i] >= 0 && gpio_normal[i] <= 16) {
531 data->gpio_config[gpio_normal[i]] |= 0x02;
535 /* Fan overrides input and output */
536 for (i = 0;i <= 7;++i) {
537 if (gpio_fan[i] >= 0 && gpio_fan[i] <= 7) {
538 data->config2 &= ~(1 << gpio_fan[i]);
542 /* Write new configs to registers */
543 adm1026_write_value(client, ADM1026_REG_CONFIG2, data->config2);
544 data->config3 = (data->config3 & 0x3f)
545 | ((data->gpio_config[16] & 0x03) << 6);
546 adm1026_write_value(client, ADM1026_REG_CONFIG3, data->config3);
547 for (i = 15, value = 0;i >= 0;--i) {
548 value <<= 2;
549 value |= data->gpio_config[i] & 0x03;
550 if ((i & 0x03) == 0) {
551 adm1026_write_value(client,
552 ADM1026_REG_GPIO_CFG_0_3 + i/4,
553 value);
554 value = 0;
558 /* Print the new config */
559 adm1026_print_gpio(client);
563 static struct adm1026_data *adm1026_update_device(struct device *dev)
565 struct i2c_client *client = to_i2c_client(dev);
566 struct adm1026_data *data = i2c_get_clientdata(client);
567 int i;
568 long value, alarms, gpio;
570 mutex_lock(&data->update_lock);
571 if (!data->valid
572 || time_after(jiffies, data->last_reading + ADM1026_DATA_INTERVAL)) {
573 /* Things that change quickly */
574 dev_dbg(&client->dev, "Reading sensor values\n");
575 for (i = 0;i <= 16;++i) {
576 data->in[i] =
577 adm1026_read_value(client, ADM1026_REG_IN[i]);
580 for (i = 0;i <= 7;++i) {
581 data->fan[i] =
582 adm1026_read_value(client, ADM1026_REG_FAN(i));
585 for (i = 0;i <= 2;++i) {
586 /* NOTE: temp[] is s8 and we assume 2's complement
587 * "conversion" in the assignment */
588 data->temp[i] =
589 adm1026_read_value(client, ADM1026_REG_TEMP[i]);
592 data->pwm1.pwm = adm1026_read_value(client,
593 ADM1026_REG_PWM);
594 data->analog_out = adm1026_read_value(client,
595 ADM1026_REG_DAC);
596 /* GPIO16 is MSbit of alarms, move it to gpio */
597 alarms = adm1026_read_value(client, ADM1026_REG_STATUS4);
598 gpio = alarms & 0x80 ? 0x0100 : 0; /* GPIO16 */
599 alarms &= 0x7f;
600 alarms <<= 8;
601 alarms |= adm1026_read_value(client, ADM1026_REG_STATUS3);
602 alarms <<= 8;
603 alarms |= adm1026_read_value(client, ADM1026_REG_STATUS2);
604 alarms <<= 8;
605 alarms |= adm1026_read_value(client, ADM1026_REG_STATUS1);
606 data->alarms = alarms;
608 /* Read the GPIO values */
609 gpio |= adm1026_read_value(client,
610 ADM1026_REG_GPIO_STATUS_8_15);
611 gpio <<= 8;
612 gpio |= adm1026_read_value(client,
613 ADM1026_REG_GPIO_STATUS_0_7);
614 data->gpio = gpio;
616 data->last_reading = jiffies;
617 }; /* last_reading */
619 if (!data->valid ||
620 time_after(jiffies, data->last_config + ADM1026_CONFIG_INTERVAL)) {
621 /* Things that don't change often */
622 dev_dbg(&client->dev, "Reading config values\n");
623 for (i = 0;i <= 16;++i) {
624 data->in_min[i] = adm1026_read_value(client,
625 ADM1026_REG_IN_MIN[i]);
626 data->in_max[i] = adm1026_read_value(client,
627 ADM1026_REG_IN_MAX[i]);
630 value = adm1026_read_value(client, ADM1026_REG_FAN_DIV_0_3)
631 | (adm1026_read_value(client, ADM1026_REG_FAN_DIV_4_7)
632 << 8);
633 for (i = 0;i <= 7;++i) {
634 data->fan_min[i] = adm1026_read_value(client,
635 ADM1026_REG_FAN_MIN(i));
636 data->fan_div[i] = DIV_FROM_REG(value & 0x03);
637 value >>= 2;
640 for (i = 0; i <= 2; ++i) {
641 /* NOTE: temp_xxx[] are s8 and we assume 2's
642 * complement "conversion" in the assignment
644 data->temp_min[i] = adm1026_read_value(client,
645 ADM1026_REG_TEMP_MIN[i]);
646 data->temp_max[i] = adm1026_read_value(client,
647 ADM1026_REG_TEMP_MAX[i]);
648 data->temp_tmin[i] = adm1026_read_value(client,
649 ADM1026_REG_TEMP_TMIN[i]);
650 data->temp_crit[i] = adm1026_read_value(client,
651 ADM1026_REG_TEMP_THERM[i]);
652 data->temp_offset[i] = adm1026_read_value(client,
653 ADM1026_REG_TEMP_OFFSET[i]);
656 /* Read the STATUS/alarm masks */
657 alarms = adm1026_read_value(client, ADM1026_REG_MASK4);
658 gpio = alarms & 0x80 ? 0x0100 : 0; /* GPIO16 */
659 alarms = (alarms & 0x7f) << 8;
660 alarms |= adm1026_read_value(client, ADM1026_REG_MASK3);
661 alarms <<= 8;
662 alarms |= adm1026_read_value(client, ADM1026_REG_MASK2);
663 alarms <<= 8;
664 alarms |= adm1026_read_value(client, ADM1026_REG_MASK1);
665 data->alarm_mask = alarms;
667 /* Read the GPIO values */
668 gpio |= adm1026_read_value(client,
669 ADM1026_REG_GPIO_MASK_8_15);
670 gpio <<= 8;
671 gpio |= adm1026_read_value(client, ADM1026_REG_GPIO_MASK_0_7);
672 data->gpio_mask = gpio;
674 /* Read various values from CONFIG1 */
675 data->config1 = adm1026_read_value(client,
676 ADM1026_REG_CONFIG1);
677 if (data->config1 & CFG1_PWM_AFC) {
678 data->pwm1.enable = 2;
679 data->pwm1.auto_pwm_min =
680 PWM_MIN_FROM_REG(data->pwm1.pwm);
682 /* Read the GPIO config */
683 data->config2 = adm1026_read_value(client,
684 ADM1026_REG_CONFIG2);
685 data->config3 = adm1026_read_value(client,
686 ADM1026_REG_CONFIG3);
687 data->gpio_config[16] = (data->config3 >> 6) & 0x03;
689 value = 0;
690 for (i = 0;i <= 15;++i) {
691 if ((i & 0x03) == 0) {
692 value = adm1026_read_value(client,
693 ADM1026_REG_GPIO_CFG_0_3 + i/4);
695 data->gpio_config[i] = value & 0x03;
696 value >>= 2;
699 data->last_config = jiffies;
700 }; /* last_config */
702 dev_dbg(&client->dev, "Setting VID from GPIO11-15.\n");
703 data->vid = (data->gpio >> 11) & 0x1f;
704 data->valid = 1;
705 mutex_unlock(&data->update_lock);
706 return data;
709 static ssize_t show_in(struct device *dev, struct device_attribute *attr,
710 char *buf)
712 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
713 int nr = sensor_attr->index;
714 struct adm1026_data *data = adm1026_update_device(dev);
715 return sprintf(buf, "%d\n", INS_FROM_REG(nr, data->in[nr]));
717 static ssize_t show_in_min(struct device *dev, struct device_attribute *attr,
718 char *buf)
720 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
721 int nr = sensor_attr->index;
722 struct adm1026_data *data = adm1026_update_device(dev);
723 return sprintf(buf, "%d\n", INS_FROM_REG(nr, data->in_min[nr]));
725 static ssize_t set_in_min(struct device *dev, struct device_attribute *attr,
726 const char *buf, size_t count)
728 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
729 int nr = sensor_attr->index;
730 struct i2c_client *client = to_i2c_client(dev);
731 struct adm1026_data *data = i2c_get_clientdata(client);
732 int val = simple_strtol(buf, NULL, 10);
734 mutex_lock(&data->update_lock);
735 data->in_min[nr] = INS_TO_REG(nr, val);
736 adm1026_write_value(client, ADM1026_REG_IN_MIN[nr], data->in_min[nr]);
737 mutex_unlock(&data->update_lock);
738 return count;
740 static ssize_t show_in_max(struct device *dev, struct device_attribute *attr,
741 char *buf)
743 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
744 int nr = sensor_attr->index;
745 struct adm1026_data *data = adm1026_update_device(dev);
746 return sprintf(buf, "%d\n", INS_FROM_REG(nr, data->in_max[nr]));
748 static ssize_t set_in_max(struct device *dev, struct device_attribute *attr,
749 const char *buf, size_t count)
751 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
752 int nr = sensor_attr->index;
753 struct i2c_client *client = to_i2c_client(dev);
754 struct adm1026_data *data = i2c_get_clientdata(client);
755 int val = simple_strtol(buf, NULL, 10);
757 mutex_lock(&data->update_lock);
758 data->in_max[nr] = INS_TO_REG(nr, val);
759 adm1026_write_value(client, ADM1026_REG_IN_MAX[nr], data->in_max[nr]);
760 mutex_unlock(&data->update_lock);
761 return count;
764 #define in_reg(offset) \
765 static SENSOR_DEVICE_ATTR(in##offset##_input, S_IRUGO, show_in, \
766 NULL, offset); \
767 static SENSOR_DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \
768 show_in_min, set_in_min, offset); \
769 static SENSOR_DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \
770 show_in_max, set_in_max, offset);
773 in_reg(0);
774 in_reg(1);
775 in_reg(2);
776 in_reg(3);
777 in_reg(4);
778 in_reg(5);
779 in_reg(6);
780 in_reg(7);
781 in_reg(8);
782 in_reg(9);
783 in_reg(10);
784 in_reg(11);
785 in_reg(12);
786 in_reg(13);
787 in_reg(14);
788 in_reg(15);
790 static ssize_t show_in16(struct device *dev, struct device_attribute *attr, char *buf)
792 struct adm1026_data *data = adm1026_update_device(dev);
793 return sprintf(buf, "%d\n", INS_FROM_REG(16, data->in[16]) -
794 NEG12_OFFSET);
796 static ssize_t show_in16_min(struct device *dev, struct device_attribute *attr, char *buf)
798 struct adm1026_data *data = adm1026_update_device(dev);
799 return sprintf(buf, "%d\n", INS_FROM_REG(16, data->in_min[16])
800 - NEG12_OFFSET);
802 static ssize_t set_in16_min(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
804 struct i2c_client *client = to_i2c_client(dev);
805 struct adm1026_data *data = i2c_get_clientdata(client);
806 int val = simple_strtol(buf, NULL, 10);
808 mutex_lock(&data->update_lock);
809 data->in_min[16] = INS_TO_REG(16, val + NEG12_OFFSET);
810 adm1026_write_value(client, ADM1026_REG_IN_MIN[16], data->in_min[16]);
811 mutex_unlock(&data->update_lock);
812 return count;
814 static ssize_t show_in16_max(struct device *dev, struct device_attribute *attr, char *buf)
816 struct adm1026_data *data = adm1026_update_device(dev);
817 return sprintf(buf, "%d\n", INS_FROM_REG(16, data->in_max[16])
818 - NEG12_OFFSET);
820 static ssize_t set_in16_max(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
822 struct i2c_client *client = to_i2c_client(dev);
823 struct adm1026_data *data = i2c_get_clientdata(client);
824 int val = simple_strtol(buf, NULL, 10);
826 mutex_lock(&data->update_lock);
827 data->in_max[16] = INS_TO_REG(16, val+NEG12_OFFSET);
828 adm1026_write_value(client, ADM1026_REG_IN_MAX[16], data->in_max[16]);
829 mutex_unlock(&data->update_lock);
830 return count;
833 static SENSOR_DEVICE_ATTR(in16_input, S_IRUGO, show_in16, NULL, 16);
834 static SENSOR_DEVICE_ATTR(in16_min, S_IRUGO | S_IWUSR, show_in16_min, set_in16_min, 16);
835 static SENSOR_DEVICE_ATTR(in16_max, S_IRUGO | S_IWUSR, show_in16_max, set_in16_max, 16);
840 /* Now add fan read/write functions */
842 static ssize_t show_fan(struct device *dev, struct device_attribute *attr,
843 char *buf)
845 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
846 int nr = sensor_attr->index;
847 struct adm1026_data *data = adm1026_update_device(dev);
848 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[nr],
849 data->fan_div[nr]));
851 static ssize_t show_fan_min(struct device *dev, struct device_attribute *attr,
852 char *buf)
854 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
855 int nr = sensor_attr->index;
856 struct adm1026_data *data = adm1026_update_device(dev);
857 return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_min[nr],
858 data->fan_div[nr]));
860 static ssize_t set_fan_min(struct device *dev, struct device_attribute *attr,
861 const char *buf, size_t count)
863 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
864 int nr = sensor_attr->index;
865 struct i2c_client *client = to_i2c_client(dev);
866 struct adm1026_data *data = i2c_get_clientdata(client);
867 int val = simple_strtol(buf, NULL, 10);
869 mutex_lock(&data->update_lock);
870 data->fan_min[nr] = FAN_TO_REG(val, data->fan_div[nr]);
871 adm1026_write_value(client, ADM1026_REG_FAN_MIN(nr),
872 data->fan_min[nr]);
873 mutex_unlock(&data->update_lock);
874 return count;
877 #define fan_offset(offset) \
878 static SENSOR_DEVICE_ATTR(fan##offset##_input, S_IRUGO, show_fan, NULL, \
879 offset - 1); \
880 static SENSOR_DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR, \
881 show_fan_min, set_fan_min, offset - 1);
883 fan_offset(1);
884 fan_offset(2);
885 fan_offset(3);
886 fan_offset(4);
887 fan_offset(5);
888 fan_offset(6);
889 fan_offset(7);
890 fan_offset(8);
892 /* Adjust fan_min to account for new fan divisor */
893 static void fixup_fan_min(struct device *dev, int fan, int old_div)
895 struct i2c_client *client = to_i2c_client(dev);
896 struct adm1026_data *data = i2c_get_clientdata(client);
897 int new_min;
898 int new_div = data->fan_div[fan];
900 /* 0 and 0xff are special. Don't adjust them */
901 if (data->fan_min[fan] == 0 || data->fan_min[fan] == 0xff) {
902 return;
905 new_min = data->fan_min[fan] * old_div / new_div;
906 new_min = SENSORS_LIMIT(new_min, 1, 254);
907 data->fan_min[fan] = new_min;
908 adm1026_write_value(client, ADM1026_REG_FAN_MIN(fan), new_min);
911 /* Now add fan_div read/write functions */
912 static ssize_t show_fan_div(struct device *dev, struct device_attribute *attr,
913 char *buf)
915 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
916 int nr = sensor_attr->index;
917 struct adm1026_data *data = adm1026_update_device(dev);
918 return sprintf(buf, "%d\n", data->fan_div[nr]);
920 static ssize_t set_fan_div(struct device *dev, struct device_attribute *attr,
921 const char *buf, size_t count)
923 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
924 int nr = sensor_attr->index;
925 struct i2c_client *client = to_i2c_client(dev);
926 struct adm1026_data *data = i2c_get_clientdata(client);
927 int val, orig_div, new_div, shift;
929 val = simple_strtol(buf, NULL, 10);
930 new_div = DIV_TO_REG(val);
931 if (new_div == 0) {
932 return -EINVAL;
934 mutex_lock(&data->update_lock);
935 orig_div = data->fan_div[nr];
936 data->fan_div[nr] = DIV_FROM_REG(new_div);
938 if (nr < 4) { /* 0 <= nr < 4 */
939 shift = 2 * nr;
940 adm1026_write_value(client, ADM1026_REG_FAN_DIV_0_3,
941 ((DIV_TO_REG(orig_div) & (~(0x03 << shift))) |
942 (new_div << shift)));
943 } else { /* 3 < nr < 8 */
944 shift = 2 * (nr - 4);
945 adm1026_write_value(client, ADM1026_REG_FAN_DIV_4_7,
946 ((DIV_TO_REG(orig_div) & (~(0x03 << (2 * shift)))) |
947 (new_div << shift)));
950 if (data->fan_div[nr] != orig_div) {
951 fixup_fan_min(dev, nr, orig_div);
953 mutex_unlock(&data->update_lock);
954 return count;
957 #define fan_offset_div(offset) \
958 static SENSOR_DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR, \
959 show_fan_div, set_fan_div, offset - 1);
961 fan_offset_div(1);
962 fan_offset_div(2);
963 fan_offset_div(3);
964 fan_offset_div(4);
965 fan_offset_div(5);
966 fan_offset_div(6);
967 fan_offset_div(7);
968 fan_offset_div(8);
970 /* Temps */
971 static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
972 char *buf)
974 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
975 int nr = sensor_attr->index;
976 struct adm1026_data *data = adm1026_update_device(dev);
977 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[nr]));
979 static ssize_t show_temp_min(struct device *dev, struct device_attribute *attr,
980 char *buf)
982 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
983 int nr = sensor_attr->index;
984 struct adm1026_data *data = adm1026_update_device(dev);
985 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_min[nr]));
987 static ssize_t set_temp_min(struct device *dev, struct device_attribute *attr,
988 const char *buf, size_t count)
990 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
991 int nr = sensor_attr->index;
992 struct i2c_client *client = to_i2c_client(dev);
993 struct adm1026_data *data = i2c_get_clientdata(client);
994 int val = simple_strtol(buf, NULL, 10);
996 mutex_lock(&data->update_lock);
997 data->temp_min[nr] = TEMP_TO_REG(val);
998 adm1026_write_value(client, ADM1026_REG_TEMP_MIN[nr],
999 data->temp_min[nr]);
1000 mutex_unlock(&data->update_lock);
1001 return count;
1003 static ssize_t show_temp_max(struct device *dev, struct device_attribute *attr,
1004 char *buf)
1006 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1007 int nr = sensor_attr->index;
1008 struct adm1026_data *data = adm1026_update_device(dev);
1009 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_max[nr]));
1011 static ssize_t set_temp_max(struct device *dev, struct device_attribute *attr,
1012 const char *buf, size_t count)
1014 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1015 int nr = sensor_attr->index;
1016 struct i2c_client *client = to_i2c_client(dev);
1017 struct adm1026_data *data = i2c_get_clientdata(client);
1018 int val = simple_strtol(buf, NULL, 10);
1020 mutex_lock(&data->update_lock);
1021 data->temp_max[nr] = TEMP_TO_REG(val);
1022 adm1026_write_value(client, ADM1026_REG_TEMP_MAX[nr],
1023 data->temp_max[nr]);
1024 mutex_unlock(&data->update_lock);
1025 return count;
1028 #define temp_reg(offset) \
1029 static SENSOR_DEVICE_ATTR(temp##offset##_input, S_IRUGO, show_temp, \
1030 NULL, offset - 1); \
1031 static SENSOR_DEVICE_ATTR(temp##offset##_min, S_IRUGO | S_IWUSR, \
1032 show_temp_min, set_temp_min, offset - 1); \
1033 static SENSOR_DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR, \
1034 show_temp_max, set_temp_max, offset - 1);
1037 temp_reg(1);
1038 temp_reg(2);
1039 temp_reg(3);
1041 static ssize_t show_temp_offset(struct device *dev,
1042 struct device_attribute *attr, char *buf)
1044 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1045 int nr = sensor_attr->index;
1046 struct adm1026_data *data = adm1026_update_device(dev);
1047 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_offset[nr]));
1049 static ssize_t set_temp_offset(struct device *dev,
1050 struct device_attribute *attr, const char *buf,
1051 size_t count)
1053 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1054 int nr = sensor_attr->index;
1055 struct i2c_client *client = to_i2c_client(dev);
1056 struct adm1026_data *data = i2c_get_clientdata(client);
1057 int val = simple_strtol(buf, NULL, 10);
1059 mutex_lock(&data->update_lock);
1060 data->temp_offset[nr] = TEMP_TO_REG(val);
1061 adm1026_write_value(client, ADM1026_REG_TEMP_OFFSET[nr],
1062 data->temp_offset[nr]);
1063 mutex_unlock(&data->update_lock);
1064 return count;
1067 #define temp_offset_reg(offset) \
1068 static SENSOR_DEVICE_ATTR(temp##offset##_offset, S_IRUGO | S_IWUSR, \
1069 show_temp_offset, set_temp_offset, offset - 1);
1071 temp_offset_reg(1);
1072 temp_offset_reg(2);
1073 temp_offset_reg(3);
1075 static ssize_t show_temp_auto_point1_temp_hyst(struct device *dev,
1076 struct device_attribute *attr, char *buf)
1078 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1079 int nr = sensor_attr->index;
1080 struct adm1026_data *data = adm1026_update_device(dev);
1081 return sprintf(buf, "%d\n", TEMP_FROM_REG(
1082 ADM1026_FAN_ACTIVATION_TEMP_HYST + data->temp_tmin[nr]));
1084 static ssize_t show_temp_auto_point2_temp(struct device *dev,
1085 struct device_attribute *attr, char *buf)
1087 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1088 int nr = sensor_attr->index;
1089 struct adm1026_data *data = adm1026_update_device(dev);
1090 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_tmin[nr] +
1091 ADM1026_FAN_CONTROL_TEMP_RANGE));
1093 static ssize_t show_temp_auto_point1_temp(struct device *dev,
1094 struct device_attribute *attr, char *buf)
1096 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1097 int nr = sensor_attr->index;
1098 struct adm1026_data *data = adm1026_update_device(dev);
1099 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_tmin[nr]));
1101 static ssize_t set_temp_auto_point1_temp(struct device *dev,
1102 struct device_attribute *attr, const char *buf, size_t count)
1104 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1105 int nr = sensor_attr->index;
1106 struct i2c_client *client = to_i2c_client(dev);
1107 struct adm1026_data *data = i2c_get_clientdata(client);
1108 int val = simple_strtol(buf, NULL, 10);
1110 mutex_lock(&data->update_lock);
1111 data->temp_tmin[nr] = TEMP_TO_REG(val);
1112 adm1026_write_value(client, ADM1026_REG_TEMP_TMIN[nr],
1113 data->temp_tmin[nr]);
1114 mutex_unlock(&data->update_lock);
1115 return count;
1118 #define temp_auto_point(offset) \
1119 static SENSOR_DEVICE_ATTR(temp##offset##_auto_point1_temp, \
1120 S_IRUGO | S_IWUSR, show_temp_auto_point1_temp, \
1121 set_temp_auto_point1_temp, offset - 1); \
1122 static SENSOR_DEVICE_ATTR(temp##offset##_auto_point1_temp_hyst, S_IRUGO,\
1123 show_temp_auto_point1_temp_hyst, NULL, offset - 1); \
1124 static SENSOR_DEVICE_ATTR(temp##offset##_auto_point2_temp, S_IRUGO, \
1125 show_temp_auto_point2_temp, NULL, offset - 1);
1127 temp_auto_point(1);
1128 temp_auto_point(2);
1129 temp_auto_point(3);
1131 static ssize_t show_temp_crit_enable(struct device *dev,
1132 struct device_attribute *attr, char *buf)
1134 struct adm1026_data *data = adm1026_update_device(dev);
1135 return sprintf(buf, "%d\n", (data->config1 & CFG1_THERM_HOT) >> 4);
1137 static ssize_t set_temp_crit_enable(struct device *dev,
1138 struct device_attribute *attr, const char *buf, size_t count)
1140 struct i2c_client *client = to_i2c_client(dev);
1141 struct adm1026_data *data = i2c_get_clientdata(client);
1142 int val = simple_strtol(buf, NULL, 10);
1144 if ((val == 1) || (val==0)) {
1145 mutex_lock(&data->update_lock);
1146 data->config1 = (data->config1 & ~CFG1_THERM_HOT) | (val << 4);
1147 adm1026_write_value(client, ADM1026_REG_CONFIG1,
1148 data->config1);
1149 mutex_unlock(&data->update_lock);
1151 return count;
1154 #define temp_crit_enable(offset) \
1155 static DEVICE_ATTR(temp##offset##_crit_enable, S_IRUGO | S_IWUSR, \
1156 show_temp_crit_enable, set_temp_crit_enable);
1158 temp_crit_enable(1);
1159 temp_crit_enable(2);
1160 temp_crit_enable(3);
1162 static ssize_t show_temp_crit(struct device *dev,
1163 struct device_attribute *attr, char *buf)
1165 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1166 int nr = sensor_attr->index;
1167 struct adm1026_data *data = adm1026_update_device(dev);
1168 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_crit[nr]));
1170 static ssize_t set_temp_crit(struct device *dev, struct device_attribute *attr,
1171 const char *buf, size_t count)
1173 struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1174 int nr = sensor_attr->index;
1175 struct i2c_client *client = to_i2c_client(dev);
1176 struct adm1026_data *data = i2c_get_clientdata(client);
1177 int val = simple_strtol(buf, NULL, 10);
1179 mutex_lock(&data->update_lock);
1180 data->temp_crit[nr] = TEMP_TO_REG(val);
1181 adm1026_write_value(client, ADM1026_REG_TEMP_THERM[nr],
1182 data->temp_crit[nr]);
1183 mutex_unlock(&data->update_lock);
1184 return count;
1187 #define temp_crit_reg(offset) \
1188 static SENSOR_DEVICE_ATTR(temp##offset##_crit, S_IRUGO | S_IWUSR, \
1189 show_temp_crit, set_temp_crit, offset - 1);
1191 temp_crit_reg(1);
1192 temp_crit_reg(2);
1193 temp_crit_reg(3);
1195 static ssize_t show_analog_out_reg(struct device *dev, struct device_attribute *attr, char *buf)
1197 struct adm1026_data *data = adm1026_update_device(dev);
1198 return sprintf(buf, "%d\n", DAC_FROM_REG(data->analog_out));
1200 static ssize_t set_analog_out_reg(struct device *dev, struct device_attribute *attr, const char *buf,
1201 size_t count)
1203 struct i2c_client *client = to_i2c_client(dev);
1204 struct adm1026_data *data = i2c_get_clientdata(client);
1205 int val = simple_strtol(buf, NULL, 10);
1207 mutex_lock(&data->update_lock);
1208 data->analog_out = DAC_TO_REG(val);
1209 adm1026_write_value(client, ADM1026_REG_DAC, data->analog_out);
1210 mutex_unlock(&data->update_lock);
1211 return count;
1214 static DEVICE_ATTR(analog_out, S_IRUGO | S_IWUSR, show_analog_out_reg,
1215 set_analog_out_reg);
1217 static ssize_t show_vid_reg(struct device *dev, struct device_attribute *attr, char *buf)
1219 struct adm1026_data *data = adm1026_update_device(dev);
1220 return sprintf(buf, "%d\n", vid_from_reg(data->vid & 0x3f, data->vrm));
1222 static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid_reg, NULL);
1224 static ssize_t show_vrm_reg(struct device *dev, struct device_attribute *attr, char *buf)
1226 struct adm1026_data *data = dev_get_drvdata(dev);
1227 return sprintf(buf, "%d\n", data->vrm);
1229 static ssize_t store_vrm_reg(struct device *dev, struct device_attribute *attr, const char *buf,
1230 size_t count)
1232 struct adm1026_data *data = dev_get_drvdata(dev);
1234 data->vrm = simple_strtol(buf, NULL, 10);
1235 return count;
1238 static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm_reg, store_vrm_reg);
1240 static ssize_t show_alarms_reg(struct device *dev, struct device_attribute *attr, char *buf)
1242 struct adm1026_data *data = adm1026_update_device(dev);
1243 return sprintf(buf, "%ld\n", data->alarms);
1246 static DEVICE_ATTR(alarms, S_IRUGO, show_alarms_reg, NULL);
1248 static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
1249 char *buf)
1251 struct adm1026_data *data = adm1026_update_device(dev);
1252 int bitnr = to_sensor_dev_attr(attr)->index;
1253 return sprintf(buf, "%ld\n", (data->alarms >> bitnr) & 1);
1256 static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 0);
1257 static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, 1);
1258 static SENSOR_DEVICE_ATTR(in9_alarm, S_IRUGO, show_alarm, NULL, 1);
1259 static SENSOR_DEVICE_ATTR(in11_alarm, S_IRUGO, show_alarm, NULL, 2);
1260 static SENSOR_DEVICE_ATTR(in12_alarm, S_IRUGO, show_alarm, NULL, 3);
1261 static SENSOR_DEVICE_ATTR(in13_alarm, S_IRUGO, show_alarm, NULL, 4);
1262 static SENSOR_DEVICE_ATTR(in14_alarm, S_IRUGO, show_alarm, NULL, 5);
1263 static SENSOR_DEVICE_ATTR(in15_alarm, S_IRUGO, show_alarm, NULL, 6);
1264 static SENSOR_DEVICE_ATTR(in16_alarm, S_IRUGO, show_alarm, NULL, 7);
1265 static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 8);
1266 static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 9);
1267 static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 10);
1268 static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 11);
1269 static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 12);
1270 static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 13);
1271 static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 14);
1272 static SENSOR_DEVICE_ATTR(in7_alarm, S_IRUGO, show_alarm, NULL, 15);
1273 static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 16);
1274 static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 17);
1275 static SENSOR_DEVICE_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL, 18);
1276 static SENSOR_DEVICE_ATTR(fan4_alarm, S_IRUGO, show_alarm, NULL, 19);
1277 static SENSOR_DEVICE_ATTR(fan5_alarm, S_IRUGO, show_alarm, NULL, 20);
1278 static SENSOR_DEVICE_ATTR(fan6_alarm, S_IRUGO, show_alarm, NULL, 21);
1279 static SENSOR_DEVICE_ATTR(fan7_alarm, S_IRUGO, show_alarm, NULL, 22);
1280 static SENSOR_DEVICE_ATTR(fan8_alarm, S_IRUGO, show_alarm, NULL, 23);
1281 static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 24);
1282 static SENSOR_DEVICE_ATTR(in10_alarm, S_IRUGO, show_alarm, NULL, 25);
1283 static SENSOR_DEVICE_ATTR(in8_alarm, S_IRUGO, show_alarm, NULL, 26);
1285 static ssize_t show_alarm_mask(struct device *dev, struct device_attribute *attr, char *buf)
1287 struct adm1026_data *data = adm1026_update_device(dev);
1288 return sprintf(buf, "%ld\n", data->alarm_mask);
1290 static ssize_t set_alarm_mask(struct device *dev, struct device_attribute *attr, const char *buf,
1291 size_t count)
1293 struct i2c_client *client = to_i2c_client(dev);
1294 struct adm1026_data *data = i2c_get_clientdata(client);
1295 int val = simple_strtol(buf, NULL, 10);
1296 unsigned long mask;
1298 mutex_lock(&data->update_lock);
1299 data->alarm_mask = val & 0x7fffffff;
1300 mask = data->alarm_mask
1301 | (data->gpio_mask & 0x10000 ? 0x80000000 : 0);
1302 adm1026_write_value(client, ADM1026_REG_MASK1,
1303 mask & 0xff);
1304 mask >>= 8;
1305 adm1026_write_value(client, ADM1026_REG_MASK2,
1306 mask & 0xff);
1307 mask >>= 8;
1308 adm1026_write_value(client, ADM1026_REG_MASK3,
1309 mask & 0xff);
1310 mask >>= 8;
1311 adm1026_write_value(client, ADM1026_REG_MASK4,
1312 mask & 0xff);
1313 mutex_unlock(&data->update_lock);
1314 return count;
1317 static DEVICE_ATTR(alarm_mask, S_IRUGO | S_IWUSR, show_alarm_mask,
1318 set_alarm_mask);
1321 static ssize_t show_gpio(struct device *dev, struct device_attribute *attr, char *buf)
1323 struct adm1026_data *data = adm1026_update_device(dev);
1324 return sprintf(buf, "%ld\n", data->gpio);
1326 static ssize_t set_gpio(struct device *dev, struct device_attribute *attr, const char *buf,
1327 size_t count)
1329 struct i2c_client *client = to_i2c_client(dev);
1330 struct adm1026_data *data = i2c_get_clientdata(client);
1331 int val = simple_strtol(buf, NULL, 10);
1332 long gpio;
1334 mutex_lock(&data->update_lock);
1335 data->gpio = val & 0x1ffff;
1336 gpio = data->gpio;
1337 adm1026_write_value(client, ADM1026_REG_GPIO_STATUS_0_7, gpio & 0xff);
1338 gpio >>= 8;
1339 adm1026_write_value(client, ADM1026_REG_GPIO_STATUS_8_15, gpio & 0xff);
1340 gpio = ((gpio >> 1) & 0x80) | (data->alarms >> 24 & 0x7f);
1341 adm1026_write_value(client, ADM1026_REG_STATUS4, gpio & 0xff);
1342 mutex_unlock(&data->update_lock);
1343 return count;
1346 static DEVICE_ATTR(gpio, S_IRUGO | S_IWUSR, show_gpio, set_gpio);
1349 static ssize_t show_gpio_mask(struct device *dev, struct device_attribute *attr, char *buf)
1351 struct adm1026_data *data = adm1026_update_device(dev);
1352 return sprintf(buf, "%ld\n", data->gpio_mask);
1354 static ssize_t set_gpio_mask(struct device *dev, struct device_attribute *attr, const char *buf,
1355 size_t count)
1357 struct i2c_client *client = to_i2c_client(dev);
1358 struct adm1026_data *data = i2c_get_clientdata(client);
1359 int val = simple_strtol(buf, NULL, 10);
1360 long mask;
1362 mutex_lock(&data->update_lock);
1363 data->gpio_mask = val & 0x1ffff;
1364 mask = data->gpio_mask;
1365 adm1026_write_value(client, ADM1026_REG_GPIO_MASK_0_7, mask & 0xff);
1366 mask >>= 8;
1367 adm1026_write_value(client, ADM1026_REG_GPIO_MASK_8_15, mask & 0xff);
1368 mask = ((mask >> 1) & 0x80) | (data->alarm_mask >> 24 & 0x7f);
1369 adm1026_write_value(client, ADM1026_REG_MASK1, mask & 0xff);
1370 mutex_unlock(&data->update_lock);
1371 return count;
1374 static DEVICE_ATTR(gpio_mask, S_IRUGO | S_IWUSR, show_gpio_mask, set_gpio_mask);
1376 static ssize_t show_pwm_reg(struct device *dev, struct device_attribute *attr, char *buf)
1378 struct adm1026_data *data = adm1026_update_device(dev);
1379 return sprintf(buf, "%d\n", PWM_FROM_REG(data->pwm1.pwm));
1381 static ssize_t set_pwm_reg(struct device *dev, struct device_attribute *attr, const char *buf,
1382 size_t count)
1384 struct i2c_client *client = to_i2c_client(dev);
1385 struct adm1026_data *data = i2c_get_clientdata(client);
1387 if (data->pwm1.enable == 1) {
1388 int val = simple_strtol(buf, NULL, 10);
1390 mutex_lock(&data->update_lock);
1391 data->pwm1.pwm = PWM_TO_REG(val);
1392 adm1026_write_value(client, ADM1026_REG_PWM, data->pwm1.pwm);
1393 mutex_unlock(&data->update_lock);
1395 return count;
1397 static ssize_t show_auto_pwm_min(struct device *dev, struct device_attribute *attr, char *buf)
1399 struct adm1026_data *data = adm1026_update_device(dev);
1400 return sprintf(buf, "%d\n", data->pwm1.auto_pwm_min);
1402 static ssize_t set_auto_pwm_min(struct device *dev, struct device_attribute *attr, const char *buf,
1403 size_t count)
1405 struct i2c_client *client = to_i2c_client(dev);
1406 struct adm1026_data *data = i2c_get_clientdata(client);
1407 int val = simple_strtol(buf, NULL, 10);
1409 mutex_lock(&data->update_lock);
1410 data->pwm1.auto_pwm_min = SENSORS_LIMIT(val, 0, 255);
1411 if (data->pwm1.enable == 2) { /* apply immediately */
1412 data->pwm1.pwm = PWM_TO_REG((data->pwm1.pwm & 0x0f) |
1413 PWM_MIN_TO_REG(data->pwm1.auto_pwm_min));
1414 adm1026_write_value(client, ADM1026_REG_PWM, data->pwm1.pwm);
1416 mutex_unlock(&data->update_lock);
1417 return count;
1419 static ssize_t show_auto_pwm_max(struct device *dev, struct device_attribute *attr, char *buf)
1421 return sprintf(buf, "%d\n", ADM1026_PWM_MAX);
1423 static ssize_t show_pwm_enable(struct device *dev, struct device_attribute *attr, char *buf)
1425 struct adm1026_data *data = adm1026_update_device(dev);
1426 return sprintf(buf, "%d\n", data->pwm1.enable);
1428 static ssize_t set_pwm_enable(struct device *dev, struct device_attribute *attr, const char *buf,
1429 size_t count)
1431 struct i2c_client *client = to_i2c_client(dev);
1432 struct adm1026_data *data = i2c_get_clientdata(client);
1433 int val = simple_strtol(buf, NULL, 10);
1434 int old_enable;
1436 if ((val >= 0) && (val < 3)) {
1437 mutex_lock(&data->update_lock);
1438 old_enable = data->pwm1.enable;
1439 data->pwm1.enable = val;
1440 data->config1 = (data->config1 & ~CFG1_PWM_AFC)
1441 | ((val == 2) ? CFG1_PWM_AFC : 0);
1442 adm1026_write_value(client, ADM1026_REG_CONFIG1,
1443 data->config1);
1444 if (val == 2) { /* apply pwm1_auto_pwm_min to pwm1 */
1445 data->pwm1.pwm = PWM_TO_REG((data->pwm1.pwm & 0x0f) |
1446 PWM_MIN_TO_REG(data->pwm1.auto_pwm_min));
1447 adm1026_write_value(client, ADM1026_REG_PWM,
1448 data->pwm1.pwm);
1449 } else if (!((old_enable == 1) && (val == 1))) {
1450 /* set pwm to safe value */
1451 data->pwm1.pwm = 255;
1452 adm1026_write_value(client, ADM1026_REG_PWM,
1453 data->pwm1.pwm);
1455 mutex_unlock(&data->update_lock);
1457 return count;
1460 /* enable PWM fan control */
1461 static DEVICE_ATTR(pwm1, S_IRUGO | S_IWUSR, show_pwm_reg, set_pwm_reg);
1462 static DEVICE_ATTR(pwm2, S_IRUGO | S_IWUSR, show_pwm_reg, set_pwm_reg);
1463 static DEVICE_ATTR(pwm3, S_IRUGO | S_IWUSR, show_pwm_reg, set_pwm_reg);
1464 static DEVICE_ATTR(pwm1_enable, S_IRUGO | S_IWUSR, show_pwm_enable,
1465 set_pwm_enable);
1466 static DEVICE_ATTR(pwm2_enable, S_IRUGO | S_IWUSR, show_pwm_enable,
1467 set_pwm_enable);
1468 static DEVICE_ATTR(pwm3_enable, S_IRUGO | S_IWUSR, show_pwm_enable,
1469 set_pwm_enable);
1470 static DEVICE_ATTR(temp1_auto_point1_pwm, S_IRUGO | S_IWUSR,
1471 show_auto_pwm_min, set_auto_pwm_min);
1472 static DEVICE_ATTR(temp2_auto_point1_pwm, S_IRUGO | S_IWUSR,
1473 show_auto_pwm_min, set_auto_pwm_min);
1474 static DEVICE_ATTR(temp3_auto_point1_pwm, S_IRUGO | S_IWUSR,
1475 show_auto_pwm_min, set_auto_pwm_min);
1477 static DEVICE_ATTR(temp1_auto_point2_pwm, S_IRUGO, show_auto_pwm_max, NULL);
1478 static DEVICE_ATTR(temp2_auto_point2_pwm, S_IRUGO, show_auto_pwm_max, NULL);
1479 static DEVICE_ATTR(temp3_auto_point2_pwm, S_IRUGO, show_auto_pwm_max, NULL);
1481 static struct attribute *adm1026_attributes[] = {
1482 &sensor_dev_attr_in0_input.dev_attr.attr,
1483 &sensor_dev_attr_in0_max.dev_attr.attr,
1484 &sensor_dev_attr_in0_min.dev_attr.attr,
1485 &sensor_dev_attr_in0_alarm.dev_attr.attr,
1486 &sensor_dev_attr_in1_input.dev_attr.attr,
1487 &sensor_dev_attr_in1_max.dev_attr.attr,
1488 &sensor_dev_attr_in1_min.dev_attr.attr,
1489 &sensor_dev_attr_in1_alarm.dev_attr.attr,
1490 &sensor_dev_attr_in2_input.dev_attr.attr,
1491 &sensor_dev_attr_in2_max.dev_attr.attr,
1492 &sensor_dev_attr_in2_min.dev_attr.attr,
1493 &sensor_dev_attr_in2_alarm.dev_attr.attr,
1494 &sensor_dev_attr_in3_input.dev_attr.attr,
1495 &sensor_dev_attr_in3_max.dev_attr.attr,
1496 &sensor_dev_attr_in3_min.dev_attr.attr,
1497 &sensor_dev_attr_in3_alarm.dev_attr.attr,
1498 &sensor_dev_attr_in4_input.dev_attr.attr,
1499 &sensor_dev_attr_in4_max.dev_attr.attr,
1500 &sensor_dev_attr_in4_min.dev_attr.attr,
1501 &sensor_dev_attr_in4_alarm.dev_attr.attr,
1502 &sensor_dev_attr_in5_input.dev_attr.attr,
1503 &sensor_dev_attr_in5_max.dev_attr.attr,
1504 &sensor_dev_attr_in5_min.dev_attr.attr,
1505 &sensor_dev_attr_in5_alarm.dev_attr.attr,
1506 &sensor_dev_attr_in6_input.dev_attr.attr,
1507 &sensor_dev_attr_in6_max.dev_attr.attr,
1508 &sensor_dev_attr_in6_min.dev_attr.attr,
1509 &sensor_dev_attr_in6_alarm.dev_attr.attr,
1510 &sensor_dev_attr_in7_input.dev_attr.attr,
1511 &sensor_dev_attr_in7_max.dev_attr.attr,
1512 &sensor_dev_attr_in7_min.dev_attr.attr,
1513 &sensor_dev_attr_in7_alarm.dev_attr.attr,
1514 &sensor_dev_attr_in10_input.dev_attr.attr,
1515 &sensor_dev_attr_in10_max.dev_attr.attr,
1516 &sensor_dev_attr_in10_min.dev_attr.attr,
1517 &sensor_dev_attr_in10_alarm.dev_attr.attr,
1518 &sensor_dev_attr_in11_input.dev_attr.attr,
1519 &sensor_dev_attr_in11_max.dev_attr.attr,
1520 &sensor_dev_attr_in11_min.dev_attr.attr,
1521 &sensor_dev_attr_in11_alarm.dev_attr.attr,
1522 &sensor_dev_attr_in12_input.dev_attr.attr,
1523 &sensor_dev_attr_in12_max.dev_attr.attr,
1524 &sensor_dev_attr_in12_min.dev_attr.attr,
1525 &sensor_dev_attr_in12_alarm.dev_attr.attr,
1526 &sensor_dev_attr_in13_input.dev_attr.attr,
1527 &sensor_dev_attr_in13_max.dev_attr.attr,
1528 &sensor_dev_attr_in13_min.dev_attr.attr,
1529 &sensor_dev_attr_in13_alarm.dev_attr.attr,
1530 &sensor_dev_attr_in14_input.dev_attr.attr,
1531 &sensor_dev_attr_in14_max.dev_attr.attr,
1532 &sensor_dev_attr_in14_min.dev_attr.attr,
1533 &sensor_dev_attr_in14_alarm.dev_attr.attr,
1534 &sensor_dev_attr_in15_input.dev_attr.attr,
1535 &sensor_dev_attr_in15_max.dev_attr.attr,
1536 &sensor_dev_attr_in15_min.dev_attr.attr,
1537 &sensor_dev_attr_in15_alarm.dev_attr.attr,
1538 &sensor_dev_attr_in16_input.dev_attr.attr,
1539 &sensor_dev_attr_in16_max.dev_attr.attr,
1540 &sensor_dev_attr_in16_min.dev_attr.attr,
1541 &sensor_dev_attr_in16_alarm.dev_attr.attr,
1542 &sensor_dev_attr_fan1_input.dev_attr.attr,
1543 &sensor_dev_attr_fan1_div.dev_attr.attr,
1544 &sensor_dev_attr_fan1_min.dev_attr.attr,
1545 &sensor_dev_attr_fan1_alarm.dev_attr.attr,
1546 &sensor_dev_attr_fan2_input.dev_attr.attr,
1547 &sensor_dev_attr_fan2_div.dev_attr.attr,
1548 &sensor_dev_attr_fan2_min.dev_attr.attr,
1549 &sensor_dev_attr_fan2_alarm.dev_attr.attr,
1550 &sensor_dev_attr_fan3_input.dev_attr.attr,
1551 &sensor_dev_attr_fan3_div.dev_attr.attr,
1552 &sensor_dev_attr_fan3_min.dev_attr.attr,
1553 &sensor_dev_attr_fan3_alarm.dev_attr.attr,
1554 &sensor_dev_attr_fan4_input.dev_attr.attr,
1555 &sensor_dev_attr_fan4_div.dev_attr.attr,
1556 &sensor_dev_attr_fan4_min.dev_attr.attr,
1557 &sensor_dev_attr_fan4_alarm.dev_attr.attr,
1558 &sensor_dev_attr_fan5_input.dev_attr.attr,
1559 &sensor_dev_attr_fan5_div.dev_attr.attr,
1560 &sensor_dev_attr_fan5_min.dev_attr.attr,
1561 &sensor_dev_attr_fan5_alarm.dev_attr.attr,
1562 &sensor_dev_attr_fan6_input.dev_attr.attr,
1563 &sensor_dev_attr_fan6_div.dev_attr.attr,
1564 &sensor_dev_attr_fan6_min.dev_attr.attr,
1565 &sensor_dev_attr_fan6_alarm.dev_attr.attr,
1566 &sensor_dev_attr_fan7_input.dev_attr.attr,
1567 &sensor_dev_attr_fan7_div.dev_attr.attr,
1568 &sensor_dev_attr_fan7_min.dev_attr.attr,
1569 &sensor_dev_attr_fan7_alarm.dev_attr.attr,
1570 &sensor_dev_attr_fan8_input.dev_attr.attr,
1571 &sensor_dev_attr_fan8_div.dev_attr.attr,
1572 &sensor_dev_attr_fan8_min.dev_attr.attr,
1573 &sensor_dev_attr_fan8_alarm.dev_attr.attr,
1574 &sensor_dev_attr_temp1_input.dev_attr.attr,
1575 &sensor_dev_attr_temp1_max.dev_attr.attr,
1576 &sensor_dev_attr_temp1_min.dev_attr.attr,
1577 &sensor_dev_attr_temp1_alarm.dev_attr.attr,
1578 &sensor_dev_attr_temp2_input.dev_attr.attr,
1579 &sensor_dev_attr_temp2_max.dev_attr.attr,
1580 &sensor_dev_attr_temp2_min.dev_attr.attr,
1581 &sensor_dev_attr_temp2_alarm.dev_attr.attr,
1582 &sensor_dev_attr_temp1_offset.dev_attr.attr,
1583 &sensor_dev_attr_temp2_offset.dev_attr.attr,
1584 &sensor_dev_attr_temp1_auto_point1_temp.dev_attr.attr,
1585 &sensor_dev_attr_temp2_auto_point1_temp.dev_attr.attr,
1586 &sensor_dev_attr_temp1_auto_point1_temp_hyst.dev_attr.attr,
1587 &sensor_dev_attr_temp2_auto_point1_temp_hyst.dev_attr.attr,
1588 &sensor_dev_attr_temp1_auto_point2_temp.dev_attr.attr,
1589 &sensor_dev_attr_temp2_auto_point2_temp.dev_attr.attr,
1590 &sensor_dev_attr_temp1_crit.dev_attr.attr,
1591 &sensor_dev_attr_temp2_crit.dev_attr.attr,
1592 &dev_attr_temp1_crit_enable.attr,
1593 &dev_attr_temp2_crit_enable.attr,
1594 &dev_attr_cpu0_vid.attr,
1595 &dev_attr_vrm.attr,
1596 &dev_attr_alarms.attr,
1597 &dev_attr_alarm_mask.attr,
1598 &dev_attr_gpio.attr,
1599 &dev_attr_gpio_mask.attr,
1600 &dev_attr_pwm1.attr,
1601 &dev_attr_pwm2.attr,
1602 &dev_attr_pwm3.attr,
1603 &dev_attr_pwm1_enable.attr,
1604 &dev_attr_pwm2_enable.attr,
1605 &dev_attr_pwm3_enable.attr,
1606 &dev_attr_temp1_auto_point1_pwm.attr,
1607 &dev_attr_temp2_auto_point1_pwm.attr,
1608 &dev_attr_temp1_auto_point2_pwm.attr,
1609 &dev_attr_temp2_auto_point2_pwm.attr,
1610 &dev_attr_analog_out.attr,
1611 NULL
1614 static const struct attribute_group adm1026_group = {
1615 .attrs = adm1026_attributes,
1618 static struct attribute *adm1026_attributes_temp3[] = {
1619 &sensor_dev_attr_temp3_input.dev_attr.attr,
1620 &sensor_dev_attr_temp3_max.dev_attr.attr,
1621 &sensor_dev_attr_temp3_min.dev_attr.attr,
1622 &sensor_dev_attr_temp3_alarm.dev_attr.attr,
1623 &sensor_dev_attr_temp3_offset.dev_attr.attr,
1624 &sensor_dev_attr_temp3_auto_point1_temp.dev_attr.attr,
1625 &sensor_dev_attr_temp3_auto_point1_temp_hyst.dev_attr.attr,
1626 &sensor_dev_attr_temp3_auto_point2_temp.dev_attr.attr,
1627 &sensor_dev_attr_temp3_crit.dev_attr.attr,
1628 &dev_attr_temp3_crit_enable.attr,
1629 &dev_attr_temp3_auto_point1_pwm.attr,
1630 &dev_attr_temp3_auto_point2_pwm.attr,
1631 <<<<<<< HEAD:drivers/hwmon/adm1026.c
1632 =======
1633 NULL
1634 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/hwmon/adm1026.c
1637 static const struct attribute_group adm1026_group_temp3 = {
1638 .attrs = adm1026_attributes_temp3,
1641 static struct attribute *adm1026_attributes_in8_9[] = {
1642 &sensor_dev_attr_in8_input.dev_attr.attr,
1643 &sensor_dev_attr_in8_max.dev_attr.attr,
1644 &sensor_dev_attr_in8_min.dev_attr.attr,
1645 &sensor_dev_attr_in8_alarm.dev_attr.attr,
1646 &sensor_dev_attr_in9_input.dev_attr.attr,
1647 &sensor_dev_attr_in9_max.dev_attr.attr,
1648 &sensor_dev_attr_in9_min.dev_attr.attr,
1649 &sensor_dev_attr_in9_alarm.dev_attr.attr,
1650 <<<<<<< HEAD:drivers/hwmon/adm1026.c
1651 =======
1652 NULL
1653 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/hwmon/adm1026.c
1656 static const struct attribute_group adm1026_group_in8_9 = {
1657 .attrs = adm1026_attributes_in8_9,
1660 static int adm1026_detect(struct i2c_adapter *adapter, int address,
1661 int kind)
1663 int company, verstep;
1664 struct i2c_client *client;
1665 struct adm1026_data *data;
1666 int err = 0;
1667 const char *type_name = "";
1669 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
1670 /* We need to be able to do byte I/O */
1671 goto exit;
1674 /* OK. For now, we presume we have a valid client. We now create the
1675 client structure, even though we cannot fill it completely yet.
1676 But it allows us to access adm1026_{read,write}_value. */
1678 if (!(data = kzalloc(sizeof(struct adm1026_data), GFP_KERNEL))) {
1679 err = -ENOMEM;
1680 goto exit;
1683 client = &data->client;
1684 i2c_set_clientdata(client, data);
1685 client->addr = address;
1686 client->adapter = adapter;
1687 client->driver = &adm1026_driver;
1689 /* Now, we do the remaining detection. */
1691 company = adm1026_read_value(client, ADM1026_REG_COMPANY);
1692 verstep = adm1026_read_value(client, ADM1026_REG_VERSTEP);
1694 dev_dbg(&client->dev, "Detecting device at %d,0x%02x with"
1695 " COMPANY: 0x%02x and VERSTEP: 0x%02x\n",
1696 i2c_adapter_id(client->adapter), client->addr,
1697 company, verstep);
1699 /* If auto-detecting, Determine the chip type. */
1700 if (kind <= 0) {
1701 dev_dbg(&client->dev, "Autodetecting device at %d,0x%02x "
1702 "...\n", i2c_adapter_id(adapter), address);
1703 if (company == ADM1026_COMPANY_ANALOG_DEV
1704 && verstep == ADM1026_VERSTEP_ADM1026) {
1705 kind = adm1026;
1706 } else if (company == ADM1026_COMPANY_ANALOG_DEV
1707 && (verstep & 0xf0) == ADM1026_VERSTEP_GENERIC) {
1708 dev_err(&adapter->dev, ": Unrecognized stepping "
1709 "0x%02x. Defaulting to ADM1026.\n", verstep);
1710 kind = adm1026;
1711 } else if ((verstep & 0xf0) == ADM1026_VERSTEP_GENERIC) {
1712 dev_err(&adapter->dev, ": Found version/stepping "
1713 "0x%02x. Assuming generic ADM1026.\n",
1714 verstep);
1715 kind = any_chip;
1716 } else {
1717 dev_dbg(&client->dev, ": Autodetection "
1718 "failed\n");
1719 /* Not an ADM1026 ... */
1720 if (kind == 0) { /* User used force=x,y */
1721 dev_err(&adapter->dev, "Generic ADM1026 not "
1722 "found at %d,0x%02x. Try "
1723 "force_adm1026.\n",
1724 i2c_adapter_id(adapter), address);
1726 goto exitfree;
1730 /* Fill in the chip specific driver values */
1731 switch (kind) {
1732 case any_chip :
1733 type_name = "adm1026";
1734 break;
1735 case adm1026 :
1736 type_name = "adm1026";
1737 break;
1738 default :
1739 dev_err(&adapter->dev, ": Internal error, invalid "
1740 "kind (%d)!\n", kind);
1741 err = -EFAULT;
1742 goto exitfree;
1744 strlcpy(client->name, type_name, I2C_NAME_SIZE);
1746 /* Fill in the remaining client fields */
1747 mutex_init(&data->update_lock);
1749 /* Tell the I2C layer a new client has arrived */
1750 if ((err = i2c_attach_client(client)))
1751 goto exitfree;
1753 /* Set the VRM version */
1754 data->vrm = vid_which_vrm();
1756 /* Initialize the ADM1026 chip */
1757 adm1026_init_client(client);
1759 /* Register sysfs hooks */
1760 if ((err = sysfs_create_group(&client->dev.kobj, &adm1026_group)))
1761 goto exitdetach;
1762 if (data->config1 & CFG1_AIN8_9)
1763 err = sysfs_create_group(&client->dev.kobj,
1764 &adm1026_group_in8_9);
1765 else
1766 err = sysfs_create_group(&client->dev.kobj,
1767 &adm1026_group_temp3);
1768 if (err)
1769 goto exitremove;
1771 data->hwmon_dev = hwmon_device_register(&client->dev);
1772 if (IS_ERR(data->hwmon_dev)) {
1773 err = PTR_ERR(data->hwmon_dev);
1774 goto exitremove;
1777 return 0;
1779 /* Error out and cleanup code */
1780 exitremove:
1781 sysfs_remove_group(&client->dev.kobj, &adm1026_group);
1782 if (data->config1 & CFG1_AIN8_9)
1783 sysfs_remove_group(&client->dev.kobj, &adm1026_group_in8_9);
1784 else
1785 sysfs_remove_group(&client->dev.kobj, &adm1026_group_temp3);
1786 exitdetach:
1787 i2c_detach_client(client);
1788 exitfree:
1789 kfree(data);
1790 exit:
1791 return err;
1794 static int adm1026_detach_client(struct i2c_client *client)
1796 struct adm1026_data *data = i2c_get_clientdata(client);
1797 hwmon_device_unregister(data->hwmon_dev);
1798 sysfs_remove_group(&client->dev.kobj, &adm1026_group);
1799 if (data->config1 & CFG1_AIN8_9)
1800 sysfs_remove_group(&client->dev.kobj, &adm1026_group_in8_9);
1801 else
1802 sysfs_remove_group(&client->dev.kobj, &adm1026_group_temp3);
1803 i2c_detach_client(client);
1804 kfree(data);
1805 return 0;
1808 static int __init sm_adm1026_init(void)
1810 return i2c_add_driver(&adm1026_driver);
1813 static void __exit sm_adm1026_exit(void)
1815 i2c_del_driver(&adm1026_driver);
1818 MODULE_LICENSE("GPL");
1819 MODULE_AUTHOR("Philip Pokorny <ppokorny@penguincomputing.com>, "
1820 "Justin Thiessen <jthiessen@penguincomputing.com>");
1821 MODULE_DESCRIPTION("ADM1026 driver");
1823 module_init(sm_adm1026_init);
1824 module_exit(sm_adm1026_exit);