• Home
  • History
  • Annotate
  • Line#
  • Navigate
  • Raw
  • Download
  • only in /netgear-R7000-V1.0.7.12_1.2.5/components/opensource/linux/linux-2.6.36/drivers/hwmon/
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>
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
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>
36
37/* Addresses to scan */
38static const unsigned short normal_i2c[] = { 0x2c, 0x2d, 0x2e, I2C_CLIENT_END };
39
40static int gpio_input[17] = { -1, -1, -1, -1, -1, -1, -1, -1, -1,
41				-1, -1, -1, -1, -1, -1, -1, -1 };
42static int gpio_output[17] = { -1, -1, -1, -1, -1, -1, -1, -1, -1,
43				-1, -1, -1, -1, -1, -1, -1, -1 };
44static int gpio_inverted[17] = { -1, -1, -1, -1, -1, -1, -1, -1, -1,
45				-1, -1, -1, -1, -1, -1, -1, -1 };
46static int gpio_normal[17] = { -1, -1, -1, -1, -1, -1, -1, -1, -1,
47				-1, -1, -1, -1, -1, -1, -1, -1 };
48static int gpio_fan[8] = { -1, -1, -1, -1, -1, -1, -1, -1 };
49module_param_array(gpio_input, int, NULL, 0);
50MODULE_PARM_DESC(gpio_input, "List of GPIO pins (0-16) to program as inputs");
51module_param_array(gpio_output, int, NULL, 0);
52MODULE_PARM_DESC(gpio_output, "List of GPIO pins (0-16) to program as "
53	"outputs");
54module_param_array(gpio_inverted, int, NULL, 0);
55MODULE_PARM_DESC(gpio_inverted, "List of GPIO pins (0-16) to program as "
56	"inverted");
57module_param_array(gpio_normal, int, NULL, 0);
58MODULE_PARM_DESC(gpio_normal, "List of GPIO pins (0-16) to program as "
59	"normal/non-inverted");
60module_param_array(gpio_fan, int, NULL, 0);
61MODULE_PARM_DESC(gpio_fan, "List of GPIO pins (0-7) to program as fan tachs");
62
63/* Many ADM1026 constants specified below */
64
65/* The ADM1026 registers */
66#define ADM1026_REG_CONFIG1	0x00
67#define CFG1_MONITOR		0x01
68#define CFG1_INT_ENABLE		0x02
69#define CFG1_INT_CLEAR		0x04
70#define CFG1_AIN8_9		0x08
71#define CFG1_THERM_HOT		0x10
72#define CFG1_DAC_AFC		0x20
73#define CFG1_PWM_AFC		0x40
74#define CFG1_RESET		0x80
75
76#define ADM1026_REG_CONFIG2	0x01
77/* CONFIG2 controls FAN0/GPIO0 through FAN7/GPIO7 */
78
79#define ADM1026_REG_CONFIG3	0x07
80#define CFG3_GPIO16_ENABLE	0x01
81#define CFG3_CI_CLEAR		0x02
82#define CFG3_VREF_250		0x04
83#define CFG3_GPIO16_DIR		0x40
84#define CFG3_GPIO16_POL		0x80
85
86#define ADM1026_REG_E2CONFIG	0x13
87#define E2CFG_READ		0x01
88#define E2CFG_WRITE		0x02
89#define E2CFG_ERASE		0x04
90#define E2CFG_ROM		0x08
91#define E2CFG_CLK_EXT		0x80
92
93/* There are 10 general analog inputs and 7 dedicated inputs
94 * They are:
95 *    0 - 9  =  AIN0 - AIN9
96 *       10  =  Vbat
97 *       11  =  3.3V Standby
98 *       12  =  3.3V Main
99 *       13  =  +5V
100 *       14  =  Vccp (CPU core voltage)
101 *       15  =  +12V
102 *       16  =  -12V
103 */
104static u16 ADM1026_REG_IN[] = {
105		0x30, 0x31, 0x32, 0x33, 0x34, 0x35,
106		0x36, 0x37, 0x27, 0x29, 0x26, 0x2a,
107		0x2b, 0x2c, 0x2d, 0x2e, 0x2f
108	};
109static u16 ADM1026_REG_IN_MIN[] = {
110		0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d,
111		0x5e, 0x5f, 0x6d, 0x49, 0x6b, 0x4a,
112		0x4b, 0x4c, 0x4d, 0x4e, 0x4f
113	};
114static u16 ADM1026_REG_IN_MAX[] = {
115		0x50, 0x51, 0x52, 0x53, 0x54, 0x55,
116		0x56, 0x57, 0x6c, 0x41, 0x6a, 0x42,
117		0x43, 0x44, 0x45, 0x46, 0x47
118	};
119
120/* Temperatures are:
121 *    0 - Internal
122 *    1 - External 1
123 *    2 - External 2
124 */
125static u16 ADM1026_REG_TEMP[] = { 0x1f, 0x28, 0x29 };
126static u16 ADM1026_REG_TEMP_MIN[] = { 0x69, 0x48, 0x49 };
127static u16 ADM1026_REG_TEMP_MAX[] = { 0x68, 0x40, 0x41 };
128static u16 ADM1026_REG_TEMP_TMIN[] = { 0x10, 0x11, 0x12 };
129static u16 ADM1026_REG_TEMP_THERM[] = { 0x0d, 0x0e, 0x0f };
130static u16 ADM1026_REG_TEMP_OFFSET[] = { 0x1e, 0x6e, 0x6f };
131
132#define ADM1026_REG_FAN(nr)		(0x38 + (nr))
133#define ADM1026_REG_FAN_MIN(nr)		(0x60 + (nr))
134#define ADM1026_REG_FAN_DIV_0_3		0x02
135#define ADM1026_REG_FAN_DIV_4_7		0x03
136
137#define ADM1026_REG_DAC			0x04
138#define ADM1026_REG_PWM			0x05
139
140#define ADM1026_REG_GPIO_CFG_0_3	0x08
141#define ADM1026_REG_GPIO_CFG_4_7	0x09
142#define ADM1026_REG_GPIO_CFG_8_11	0x0a
143#define ADM1026_REG_GPIO_CFG_12_15	0x0b
144/* CFG_16 in REG_CFG3 */
145#define ADM1026_REG_GPIO_STATUS_0_7	0x24
146#define ADM1026_REG_GPIO_STATUS_8_15	0x25
147/* STATUS_16 in REG_STATUS4 */
148#define ADM1026_REG_GPIO_MASK_0_7	0x1c
149#define ADM1026_REG_GPIO_MASK_8_15	0x1d
150/* MASK_16 in REG_MASK4 */
151
152#define ADM1026_REG_COMPANY		0x16
153#define ADM1026_REG_VERSTEP		0x17
154/* These are the recognized values for the above regs */
155#define ADM1026_COMPANY_ANALOG_DEV	0x41
156#define ADM1026_VERSTEP_GENERIC		0x40
157#define ADM1026_VERSTEP_ADM1026		0x44
158
159#define ADM1026_REG_MASK1		0x18
160#define ADM1026_REG_MASK2		0x19
161#define ADM1026_REG_MASK3		0x1a
162#define ADM1026_REG_MASK4		0x1b
163
164#define ADM1026_REG_STATUS1		0x20
165#define ADM1026_REG_STATUS2		0x21
166#define ADM1026_REG_STATUS3		0x22
167#define ADM1026_REG_STATUS4		0x23
168
169#define ADM1026_FAN_ACTIVATION_TEMP_HYST -6
170#define ADM1026_FAN_CONTROL_TEMP_RANGE	20
171#define ADM1026_PWM_MAX			255
172
173/* Conversions. Rounding and limit checking is only done on the TO_REG
174 * variants. Note that you should be a bit careful with which arguments
175 * these macros are called: arguments may be evaluated more than once.
176 */
177
178/* IN are scaled acording to built-in resistors.  These are the
179 *   voltages corresponding to 3/4 of full scale (192 or 0xc0)
180 *   NOTE: The -12V input needs an additional factor to account
181 *      for the Vref pullup resistor.
182 *      NEG12_OFFSET = SCALE * Vref / V-192 - Vref
183 *                   = 13875 * 2.50 / 1.875 - 2500
184 *                   = 16000
185 *
186 * The values in this table are based on Table II, page 15 of the
187 *    datasheet.
188 */
189static int adm1026_scaling[] = { /* .001 Volts */
190		2250, 2250, 2250, 2250, 2250, 2250,
191		1875, 1875, 1875, 1875, 3000, 3330,
192		3330, 4995, 2250, 12000, 13875
193	};
194#define NEG12_OFFSET  16000
195#define SCALE(val, from, to) (((val)*(to) + ((from)/2))/(from))
196#define INS_TO_REG(n, val)  (SENSORS_LIMIT(SCALE(val, adm1026_scaling[n], 192),\
197	0, 255))
198#define INS_FROM_REG(n, val) (SCALE(val, 192, adm1026_scaling[n]))
199
200/* FAN speed is measured using 22.5kHz clock and counts for 2 pulses
201 *   and we assume a 2 pulse-per-rev fan tach signal
202 *      22500 kHz * 60 (sec/min) * 2 (pulse) / 2 (pulse/rev) == 1350000
203 */
204#define FAN_TO_REG(val, div)  ((val) <= 0 ? 0xff : \
205				SENSORS_LIMIT(1350000/((val)*(div)), 1, 254))
206#define FAN_FROM_REG(val, div) ((val) == 0 ? -1:(val) == 0xff ? 0 : \
207				1350000/((val)*(div)))
208#define DIV_FROM_REG(val) (1<<(val))
209#define DIV_TO_REG(val) ((val) >= 8 ? 3 : (val) >= 4 ? 2 : (val) >= 2 ? 1 : 0)
210
211/* Temperature is reported in 1 degC increments */
212#define TEMP_TO_REG(val) (SENSORS_LIMIT(((val)+((val)<0 ? -500 : 500))/1000,\
213	-127, 127))
214#define TEMP_FROM_REG(val) ((val) * 1000)
215#define OFFSET_TO_REG(val) (SENSORS_LIMIT(((val)+((val)<0 ? -500 : 500))/1000,\
216	-127, 127))
217#define OFFSET_FROM_REG(val) ((val) * 1000)
218
219#define PWM_TO_REG(val) (SENSORS_LIMIT(val, 0, 255))
220#define PWM_FROM_REG(val) (val)
221
222#define PWM_MIN_TO_REG(val) ((val) & 0xf0)
223#define PWM_MIN_FROM_REG(val) (((val) & 0xf0) + ((val) >> 4))
224
225/* Analog output is a voltage, and scaled to millivolts.  The datasheet
226 *   indicates that the DAC could be used to drive the fans, but in our
227 *   example board (Arima HDAMA) it isn't connected to the fans at all.
228 */
229#define DAC_TO_REG(val) (SENSORS_LIMIT(((((val)*255)+500)/2500), 0, 255))
230#define DAC_FROM_REG(val) (((val)*2500)/255)
231
232/* Chip sampling rates
233 *
234 * Some sensors are not updated more frequently than once per second
235 *    so it doesn't make sense to read them more often than that.
236 *    We cache the results and return the saved data if the driver
237 *    is called again before a second has elapsed.
238 *
239 * Also, there is significant configuration data for this chip
240 *    So, we keep the config data up to date in the cache
241 *    when it is written and only sample it once every 5 *minutes*
242 */
243#define ADM1026_DATA_INTERVAL		(1 * HZ)
244#define ADM1026_CONFIG_INTERVAL		(5 * 60 * HZ)
245
246/* We allow for multiple chips in a single system.
247 *
248 * For each registered ADM1026, we need to keep state information
249 * at client->data. The adm1026_data structure is dynamically
250 * allocated, when a new client structure is allocated. */
251
252struct pwm_data {
253	u8 pwm;
254	u8 enable;
255	u8 auto_pwm_min;
256};
257
258struct adm1026_data {
259	struct device *hwmon_dev;
260
261	struct mutex update_lock;
262	int valid;		/* !=0 if following fields are valid */
263	unsigned long last_reading;	/* In jiffies */
264	unsigned long last_config;	/* In jiffies */
265
266	u8 in[17];		/* Register value */
267	u8 in_max[17];		/* Register value */
268	u8 in_min[17];		/* Register value */
269	s8 temp[3];		/* Register value */
270	s8 temp_min[3];		/* Register value */
271	s8 temp_max[3];		/* Register value */
272	s8 temp_tmin[3];	/* Register value */
273	s8 temp_crit[3];	/* Register value */
274	s8 temp_offset[3];	/* Register value */
275	u8 fan[8];		/* Register value */
276	u8 fan_min[8];		/* Register value */
277	u8 fan_div[8];		/* Decoded value */
278	struct pwm_data pwm1;	/* Pwm control values */
279	u8 vrm;			/* VRM version */
280	u8 analog_out;		/* Register value (DAC) */
281	long alarms;		/* Register encoding, combined */
282	long alarm_mask;	/* Register encoding, combined */
283	long gpio;		/* Register encoding, combined */
284	long gpio_mask;		/* Register encoding, combined */
285	u8 gpio_config[17];	/* Decoded value */
286	u8 config1;		/* Register value */
287	u8 config2;		/* Register value */
288	u8 config3;		/* Register value */
289};
290
291static int adm1026_probe(struct i2c_client *client,
292			 const struct i2c_device_id *id);
293static int adm1026_detect(struct i2c_client *client,
294			  struct i2c_board_info *info);
295static int adm1026_remove(struct i2c_client *client);
296static int adm1026_read_value(struct i2c_client *client, u8 reg);
297static int adm1026_write_value(struct i2c_client *client, u8 reg, int value);
298static void adm1026_print_gpio(struct i2c_client *client);
299static void adm1026_fixup_gpio(struct i2c_client *client);
300static struct adm1026_data *adm1026_update_device(struct device *dev);
301static void adm1026_init_client(struct i2c_client *client);
302
303
304static const struct i2c_device_id adm1026_id[] = {
305	{ "adm1026", 0 },
306	{ }
307};
308MODULE_DEVICE_TABLE(i2c, adm1026_id);
309
310static struct i2c_driver adm1026_driver = {
311	.class		= I2C_CLASS_HWMON,
312	.driver = {
313		.name	= "adm1026",
314	},
315	.probe		= adm1026_probe,
316	.remove		= adm1026_remove,
317	.id_table	= adm1026_id,
318	.detect		= adm1026_detect,
319	.address_list	= normal_i2c,
320};
321
322static int adm1026_read_value(struct i2c_client *client, u8 reg)
323{
324	int res;
325
326	if (reg < 0x80) {
327		/* "RAM" locations */
328		res = i2c_smbus_read_byte_data(client, reg) & 0xff;
329	} else {
330		/* EEPROM, do nothing */
331		res = 0;
332	}
333	return res;
334}
335
336static int adm1026_write_value(struct i2c_client *client, u8 reg, int value)
337{
338	int res;
339
340	if (reg < 0x80) {
341		/* "RAM" locations */
342		res = i2c_smbus_write_byte_data(client, reg, value);
343	} else {
344		/* EEPROM, do nothing */
345		res = 0;
346	}
347	return res;
348}
349
350static void adm1026_init_client(struct i2c_client *client)
351{
352	int value, i;
353	struct adm1026_data *data = i2c_get_clientdata(client);
354
355	dev_dbg(&client->dev, "Initializing device\n");
356	/* Read chip config */
357	data->config1 = adm1026_read_value(client, ADM1026_REG_CONFIG1);
358	data->config2 = adm1026_read_value(client, ADM1026_REG_CONFIG2);
359	data->config3 = adm1026_read_value(client, ADM1026_REG_CONFIG3);
360
361	/* Inform user of chip config */
362	dev_dbg(&client->dev, "ADM1026_REG_CONFIG1 is: 0x%02x\n",
363		data->config1);
364	if ((data->config1 & CFG1_MONITOR) == 0) {
365		dev_dbg(&client->dev, "Monitoring not currently "
366			"enabled.\n");
367	}
368	if (data->config1 & CFG1_INT_ENABLE) {
369		dev_dbg(&client->dev, "SMBALERT interrupts are "
370			"enabled.\n");
371	}
372	if (data->config1 & CFG1_AIN8_9) {
373		dev_dbg(&client->dev, "in8 and in9 enabled. "
374			"temp3 disabled.\n");
375	} else {
376		dev_dbg(&client->dev, "temp3 enabled.  in8 and "
377			"in9 disabled.\n");
378	}
379	if (data->config1 & CFG1_THERM_HOT) {
380		dev_dbg(&client->dev, "Automatic THERM, PWM, "
381			"and temp limits enabled.\n");
382	}
383
384	if (data->config3 & CFG3_GPIO16_ENABLE) {
385		dev_dbg(&client->dev, "GPIO16 enabled.  THERM "
386			"pin disabled.\n");
387	} else {
388		dev_dbg(&client->dev, "THERM pin enabled.  "
389			"GPIO16 disabled.\n");
390	}
391	if (data->config3 & CFG3_VREF_250) {
392		dev_dbg(&client->dev, "Vref is 2.50 Volts.\n");
393	} else {
394		dev_dbg(&client->dev, "Vref is 1.82 Volts.\n");
395	}
396	/* Read and pick apart the existing GPIO configuration */
397	value = 0;
398	for (i = 0;i <= 15;++i) {
399		if ((i & 0x03) == 0) {
400			value = adm1026_read_value(client,
401					ADM1026_REG_GPIO_CFG_0_3 + i/4);
402		}
403		data->gpio_config[i] = value & 0x03;
404		value >>= 2;
405	}
406	data->gpio_config[16] = (data->config3 >> 6) & 0x03;
407
408	/* ... and then print it */
409	adm1026_print_gpio(client);
410
411	/* If the user asks us to reprogram the GPIO config, then
412	 * do it now.
413	 */
414	if (gpio_input[0] != -1 || gpio_output[0] != -1
415		|| gpio_inverted[0] != -1 || gpio_normal[0] != -1
416		|| gpio_fan[0] != -1) {
417		adm1026_fixup_gpio(client);
418	}
419
420	/* WE INTENTIONALLY make no changes to the limits,
421	 *   offsets, pwms, fans and zones.  If they were
422	 *   configured, we don't want to mess with them.
423	 *   If they weren't, the default is 100% PWM, no
424	 *   control and will suffice until 'sensors -s'
425	 *   can be run by the user.  We DO set the default
426	 *   value for pwm1.auto_pwm_min to its maximum
427	 *   so that enabling automatic pwm fan control
428	 *   without first setting a value for pwm1.auto_pwm_min
429	 *   will not result in potentially dangerous fan speed decrease.
430	 */
431	data->pwm1.auto_pwm_min=255;
432	/* Start monitoring */
433	value = adm1026_read_value(client, ADM1026_REG_CONFIG1);
434	/* Set MONITOR, clear interrupt acknowledge and s/w reset */
435	value = (value | CFG1_MONITOR) & (~CFG1_INT_CLEAR & ~CFG1_RESET);
436	dev_dbg(&client->dev, "Setting CONFIG to: 0x%02x\n", value);
437	data->config1 = value;
438	adm1026_write_value(client, ADM1026_REG_CONFIG1, value);
439
440	/* initialize fan_div[] to hardware defaults */
441	value = adm1026_read_value(client, ADM1026_REG_FAN_DIV_0_3) |
442		(adm1026_read_value(client, ADM1026_REG_FAN_DIV_4_7) << 8);
443	for (i = 0;i <= 7;++i) {
444		data->fan_div[i] = DIV_FROM_REG(value & 0x03);
445		value >>= 2;
446	}
447}
448
449static void adm1026_print_gpio(struct i2c_client *client)
450{
451	struct adm1026_data *data = i2c_get_clientdata(client);
452	int i;
453
454	dev_dbg(&client->dev, "GPIO config is:\n");
455	for (i = 0;i <= 7;++i) {
456		if (data->config2 & (1 << i)) {
457			dev_dbg(&client->dev, "\t%sGP%s%d\n",
458				data->gpio_config[i] & 0x02 ? "" : "!",
459				data->gpio_config[i] & 0x01 ? "OUT" : "IN",
460				i);
461		} else {
462			dev_dbg(&client->dev, "\tFAN%d\n", i);
463		}
464	}
465	for (i = 8;i <= 15;++i) {
466		dev_dbg(&client->dev, "\t%sGP%s%d\n",
467			data->gpio_config[i] & 0x02 ? "" : "!",
468			data->gpio_config[i] & 0x01 ? "OUT" : "IN",
469			i);
470	}
471	if (data->config3 & CFG3_GPIO16_ENABLE) {
472		dev_dbg(&client->dev, "\t%sGP%s16\n",
473			data->gpio_config[16] & 0x02 ? "" : "!",
474			data->gpio_config[16] & 0x01 ? "OUT" : "IN");
475	} else {
476		/* GPIO16 is THERM */
477		dev_dbg(&client->dev, "\tTHERM\n");
478	}
479}
480
481static void adm1026_fixup_gpio(struct i2c_client *client)
482{
483	struct adm1026_data *data = i2c_get_clientdata(client);
484	int i;
485	int value;
486
487	/* Make the changes requested. */
488
489	/* Make outputs */
490	for (i = 0;i <= 16;++i) {
491		if (gpio_output[i] >= 0 && gpio_output[i] <= 16) {
492			data->gpio_config[gpio_output[i]] |= 0x01;
493		}
494		/* if GPIO0-7 is output, it isn't a FAN tach */
495		if (gpio_output[i] >= 0 && gpio_output[i] <= 7) {
496			data->config2 |= 1 << gpio_output[i];
497		}
498	}
499
500	/* Input overrides output */
501	for (i = 0;i <= 16;++i) {
502		if (gpio_input[i] >= 0 && gpio_input[i] <= 16) {
503			data->gpio_config[gpio_input[i]] &= ~ 0x01;
504		}
505		/* if GPIO0-7 is input, it isn't a FAN tach */
506		if (gpio_input[i] >= 0 && gpio_input[i] <= 7) {
507			data->config2 |= 1 << gpio_input[i];
508		}
509	}
510
511	/* Inverted */
512	for (i = 0;i <= 16;++i) {
513		if (gpio_inverted[i] >= 0 && gpio_inverted[i] <= 16) {
514			data->gpio_config[gpio_inverted[i]] &= ~ 0x02;
515		}
516	}
517
518	/* Normal overrides inverted */
519	for (i = 0;i <= 16;++i) {
520		if (gpio_normal[i] >= 0 && gpio_normal[i] <= 16) {
521			data->gpio_config[gpio_normal[i]] |= 0x02;
522		}
523	}
524
525	/* Fan overrides input and output */
526	for (i = 0;i <= 7;++i) {
527		if (gpio_fan[i] >= 0 && gpio_fan[i] <= 7) {
528			data->config2 &= ~(1 << gpio_fan[i]);
529		}
530	}
531
532	/* Write new configs to registers */
533	adm1026_write_value(client, ADM1026_REG_CONFIG2, data->config2);
534	data->config3 = (data->config3 & 0x3f)
535			| ((data->gpio_config[16] & 0x03) << 6);
536	adm1026_write_value(client, ADM1026_REG_CONFIG3, data->config3);
537	for (i = 15, value = 0;i >= 0;--i) {
538		value <<= 2;
539		value |= data->gpio_config[i] & 0x03;
540		if ((i & 0x03) == 0) {
541			adm1026_write_value(client,
542					ADM1026_REG_GPIO_CFG_0_3 + i/4,
543					value);
544			value = 0;
545		}
546	}
547
548	/* Print the new config */
549	adm1026_print_gpio(client);
550}
551
552
553static struct adm1026_data *adm1026_update_device(struct device *dev)
554{
555	struct i2c_client *client = to_i2c_client(dev);
556	struct adm1026_data *data = i2c_get_clientdata(client);
557	int i;
558	long value, alarms, gpio;
559
560	mutex_lock(&data->update_lock);
561	if (!data->valid
562	    || time_after(jiffies, data->last_reading + ADM1026_DATA_INTERVAL)) {
563		/* Things that change quickly */
564		dev_dbg(&client->dev, "Reading sensor values\n");
565		for (i = 0;i <= 16;++i) {
566			data->in[i] =
567			    adm1026_read_value(client, ADM1026_REG_IN[i]);
568		}
569
570		for (i = 0;i <= 7;++i) {
571			data->fan[i] =
572			    adm1026_read_value(client, ADM1026_REG_FAN(i));
573		}
574
575		for (i = 0;i <= 2;++i) {
576			/* NOTE: temp[] is s8 and we assume 2's complement
577			 *   "conversion" in the assignment */
578			data->temp[i] =
579			    adm1026_read_value(client, ADM1026_REG_TEMP[i]);
580		}
581
582		data->pwm1.pwm = adm1026_read_value(client,
583			ADM1026_REG_PWM);
584		data->analog_out = adm1026_read_value(client,
585			ADM1026_REG_DAC);
586		/* GPIO16 is MSbit of alarms, move it to gpio */
587		alarms = adm1026_read_value(client, ADM1026_REG_STATUS4);
588		gpio = alarms & 0x80 ? 0x0100 : 0; /* GPIO16 */
589		alarms &= 0x7f;
590		alarms <<= 8;
591		alarms |= adm1026_read_value(client, ADM1026_REG_STATUS3);
592		alarms <<= 8;
593		alarms |= adm1026_read_value(client, ADM1026_REG_STATUS2);
594		alarms <<= 8;
595		alarms |= adm1026_read_value(client, ADM1026_REG_STATUS1);
596		data->alarms = alarms;
597
598		/* Read the GPIO values */
599		gpio |= adm1026_read_value(client,
600			ADM1026_REG_GPIO_STATUS_8_15);
601		gpio <<= 8;
602		gpio |= adm1026_read_value(client,
603			ADM1026_REG_GPIO_STATUS_0_7);
604		data->gpio = gpio;
605
606		data->last_reading = jiffies;
607	}; /* last_reading */
608
609	if (!data->valid ||
610	    time_after(jiffies, data->last_config + ADM1026_CONFIG_INTERVAL)) {
611		/* Things that don't change often */
612		dev_dbg(&client->dev, "Reading config values\n");
613		for (i = 0;i <= 16;++i) {
614			data->in_min[i] = adm1026_read_value(client,
615				ADM1026_REG_IN_MIN[i]);
616			data->in_max[i] = adm1026_read_value(client,
617				ADM1026_REG_IN_MAX[i]);
618		}
619
620		value = adm1026_read_value(client, ADM1026_REG_FAN_DIV_0_3)
621			| (adm1026_read_value(client, ADM1026_REG_FAN_DIV_4_7)
622			<< 8);
623		for (i = 0;i <= 7;++i) {
624			data->fan_min[i] = adm1026_read_value(client,
625				ADM1026_REG_FAN_MIN(i));
626			data->fan_div[i] = DIV_FROM_REG(value & 0x03);
627			value >>= 2;
628		}
629
630		for (i = 0; i <= 2; ++i) {
631			/* NOTE: temp_xxx[] are s8 and we assume 2's
632			 *    complement "conversion" in the assignment
633			 */
634			data->temp_min[i] = adm1026_read_value(client,
635				ADM1026_REG_TEMP_MIN[i]);
636			data->temp_max[i] = adm1026_read_value(client,
637				ADM1026_REG_TEMP_MAX[i]);
638			data->temp_tmin[i] = adm1026_read_value(client,
639				ADM1026_REG_TEMP_TMIN[i]);
640			data->temp_crit[i] = adm1026_read_value(client,
641				ADM1026_REG_TEMP_THERM[i]);
642			data->temp_offset[i] = adm1026_read_value(client,
643				ADM1026_REG_TEMP_OFFSET[i]);
644		}
645
646		/* Read the STATUS/alarm masks */
647		alarms = adm1026_read_value(client, ADM1026_REG_MASK4);
648		gpio = alarms & 0x80 ? 0x0100 : 0; /* GPIO16 */
649		alarms = (alarms & 0x7f) << 8;
650		alarms |= adm1026_read_value(client, ADM1026_REG_MASK3);
651		alarms <<= 8;
652		alarms |= adm1026_read_value(client, ADM1026_REG_MASK2);
653		alarms <<= 8;
654		alarms |= adm1026_read_value(client, ADM1026_REG_MASK1);
655		data->alarm_mask = alarms;
656
657		/* Read the GPIO values */
658		gpio |= adm1026_read_value(client,
659			ADM1026_REG_GPIO_MASK_8_15);
660		gpio <<= 8;
661		gpio |= adm1026_read_value(client, ADM1026_REG_GPIO_MASK_0_7);
662		data->gpio_mask = gpio;
663
664		/* Read various values from CONFIG1 */
665		data->config1 = adm1026_read_value(client,
666			ADM1026_REG_CONFIG1);
667		if (data->config1 & CFG1_PWM_AFC) {
668			data->pwm1.enable = 2;
669			data->pwm1.auto_pwm_min =
670				PWM_MIN_FROM_REG(data->pwm1.pwm);
671		}
672		/* Read the GPIO config */
673		data->config2 = adm1026_read_value(client,
674			ADM1026_REG_CONFIG2);
675		data->config3 = adm1026_read_value(client,
676			ADM1026_REG_CONFIG3);
677		data->gpio_config[16] = (data->config3 >> 6) & 0x03;
678
679		value = 0;
680		for (i = 0;i <= 15;++i) {
681			if ((i & 0x03) == 0) {
682				value = adm1026_read_value(client,
683					    ADM1026_REG_GPIO_CFG_0_3 + i/4);
684			}
685			data->gpio_config[i] = value & 0x03;
686			value >>= 2;
687		}
688
689		data->last_config = jiffies;
690	}; /* last_config */
691
692	data->valid = 1;
693	mutex_unlock(&data->update_lock);
694	return data;
695}
696
697static ssize_t show_in(struct device *dev, struct device_attribute *attr,
698		char *buf)
699{
700	struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
701	int nr = sensor_attr->index;
702	struct adm1026_data *data = adm1026_update_device(dev);
703	return sprintf(buf, "%d\n", INS_FROM_REG(nr, data->in[nr]));
704}
705static ssize_t show_in_min(struct device *dev, struct device_attribute *attr,
706		char *buf)
707{
708	struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
709	int nr = sensor_attr->index;
710	struct adm1026_data *data = adm1026_update_device(dev);
711	return sprintf(buf, "%d\n", INS_FROM_REG(nr, data->in_min[nr]));
712}
713static ssize_t set_in_min(struct device *dev, struct device_attribute *attr,
714		const char *buf, size_t count)
715{
716	struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
717	int nr = sensor_attr->index;
718	struct i2c_client *client = to_i2c_client(dev);
719	struct adm1026_data *data = i2c_get_clientdata(client);
720	int val = simple_strtol(buf, NULL, 10);
721
722	mutex_lock(&data->update_lock);
723	data->in_min[nr] = INS_TO_REG(nr, val);
724	adm1026_write_value(client, ADM1026_REG_IN_MIN[nr], data->in_min[nr]);
725	mutex_unlock(&data->update_lock);
726	return count;
727}
728static ssize_t show_in_max(struct device *dev, struct device_attribute *attr,
729		char *buf)
730{
731	struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
732	int nr = sensor_attr->index;
733	struct adm1026_data *data = adm1026_update_device(dev);
734	return sprintf(buf, "%d\n", INS_FROM_REG(nr, data->in_max[nr]));
735}
736static ssize_t set_in_max(struct device *dev, struct device_attribute *attr,
737		const char *buf, size_t count)
738{
739	struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
740	int nr = sensor_attr->index;
741	struct i2c_client *client = to_i2c_client(dev);
742	struct adm1026_data *data = i2c_get_clientdata(client);
743	int val = simple_strtol(buf, NULL, 10);
744
745	mutex_lock(&data->update_lock);
746	data->in_max[nr] = INS_TO_REG(nr, val);
747	adm1026_write_value(client, ADM1026_REG_IN_MAX[nr], data->in_max[nr]);
748	mutex_unlock(&data->update_lock);
749	return count;
750}
751
752#define in_reg(offset)						\
753static SENSOR_DEVICE_ATTR(in##offset##_input, S_IRUGO, show_in,	\
754		NULL, offset);					\
755static SENSOR_DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR,	\
756		show_in_min, set_in_min, offset);		\
757static SENSOR_DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR,	\
758		show_in_max, set_in_max, offset);
759
760
761in_reg(0);
762in_reg(1);
763in_reg(2);
764in_reg(3);
765in_reg(4);
766in_reg(5);
767in_reg(6);
768in_reg(7);
769in_reg(8);
770in_reg(9);
771in_reg(10);
772in_reg(11);
773in_reg(12);
774in_reg(13);
775in_reg(14);
776in_reg(15);
777
778static ssize_t show_in16(struct device *dev, struct device_attribute *attr, char *buf)
779{
780	struct adm1026_data *data = adm1026_update_device(dev);
781	return sprintf(buf, "%d\n", INS_FROM_REG(16, data->in[16]) -
782		NEG12_OFFSET);
783}
784static ssize_t show_in16_min(struct device *dev, struct device_attribute *attr, char *buf)
785{
786	struct adm1026_data *data = adm1026_update_device(dev);
787	return sprintf(buf, "%d\n", INS_FROM_REG(16, data->in_min[16])
788		- NEG12_OFFSET);
789}
790static ssize_t set_in16_min(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
791{
792	struct i2c_client *client = to_i2c_client(dev);
793	struct adm1026_data *data = i2c_get_clientdata(client);
794	int val = simple_strtol(buf, NULL, 10);
795
796	mutex_lock(&data->update_lock);
797	data->in_min[16] = INS_TO_REG(16, val + NEG12_OFFSET);
798	adm1026_write_value(client, ADM1026_REG_IN_MIN[16], data->in_min[16]);
799	mutex_unlock(&data->update_lock);
800	return count;
801}
802static ssize_t show_in16_max(struct device *dev, struct device_attribute *attr, char *buf)
803{
804	struct adm1026_data *data = adm1026_update_device(dev);
805	return sprintf(buf, "%d\n", INS_FROM_REG(16, data->in_max[16])
806			- NEG12_OFFSET);
807}
808static ssize_t set_in16_max(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
809{
810	struct i2c_client *client = to_i2c_client(dev);
811	struct adm1026_data *data = i2c_get_clientdata(client);
812	int val = simple_strtol(buf, NULL, 10);
813
814	mutex_lock(&data->update_lock);
815	data->in_max[16] = INS_TO_REG(16, val+NEG12_OFFSET);
816	adm1026_write_value(client, ADM1026_REG_IN_MAX[16], data->in_max[16]);
817	mutex_unlock(&data->update_lock);
818	return count;
819}
820
821static SENSOR_DEVICE_ATTR(in16_input, S_IRUGO, show_in16, NULL, 16);
822static SENSOR_DEVICE_ATTR(in16_min, S_IRUGO | S_IWUSR, show_in16_min, set_in16_min, 16);
823static SENSOR_DEVICE_ATTR(in16_max, S_IRUGO | S_IWUSR, show_in16_max, set_in16_max, 16);
824
825
826
827
828/* Now add fan read/write functions */
829
830static ssize_t show_fan(struct device *dev, struct device_attribute *attr,
831		char *buf)
832{
833	struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
834	int nr = sensor_attr->index;
835	struct adm1026_data *data = adm1026_update_device(dev);
836	return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[nr],
837		data->fan_div[nr]));
838}
839static ssize_t show_fan_min(struct device *dev, struct device_attribute *attr,
840		char *buf)
841{
842	struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
843	int nr = sensor_attr->index;
844	struct adm1026_data *data = adm1026_update_device(dev);
845	return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan_min[nr],
846		data->fan_div[nr]));
847}
848static ssize_t set_fan_min(struct device *dev, struct device_attribute *attr,
849		const char *buf, size_t count)
850{
851	struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
852	int nr = sensor_attr->index;
853	struct i2c_client *client = to_i2c_client(dev);
854	struct adm1026_data *data = i2c_get_clientdata(client);
855	int val = simple_strtol(buf, NULL, 10);
856
857	mutex_lock(&data->update_lock);
858	data->fan_min[nr] = FAN_TO_REG(val, data->fan_div[nr]);
859	adm1026_write_value(client, ADM1026_REG_FAN_MIN(nr),
860		data->fan_min[nr]);
861	mutex_unlock(&data->update_lock);
862	return count;
863}
864
865#define fan_offset(offset)						\
866static SENSOR_DEVICE_ATTR(fan##offset##_input, S_IRUGO, show_fan, NULL,	\
867		offset - 1);						\
868static SENSOR_DEVICE_ATTR(fan##offset##_min, S_IRUGO | S_IWUSR,		\
869		show_fan_min, set_fan_min, offset - 1);
870
871fan_offset(1);
872fan_offset(2);
873fan_offset(3);
874fan_offset(4);
875fan_offset(5);
876fan_offset(6);
877fan_offset(7);
878fan_offset(8);
879
880/* Adjust fan_min to account for new fan divisor */
881static void fixup_fan_min(struct device *dev, int fan, int old_div)
882{
883	struct i2c_client *client = to_i2c_client(dev);
884	struct adm1026_data *data = i2c_get_clientdata(client);
885	int new_min;
886	int new_div = data->fan_div[fan];
887
888	/* 0 and 0xff are special.  Don't adjust them */
889	if (data->fan_min[fan] == 0 || data->fan_min[fan] == 0xff) {
890		return;
891	}
892
893	new_min = data->fan_min[fan] * old_div / new_div;
894	new_min = SENSORS_LIMIT(new_min, 1, 254);
895	data->fan_min[fan] = new_min;
896	adm1026_write_value(client, ADM1026_REG_FAN_MIN(fan), new_min);
897}
898
899/* Now add fan_div read/write functions */
900static ssize_t show_fan_div(struct device *dev, struct device_attribute *attr,
901		char *buf)
902{
903	struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
904	int nr = sensor_attr->index;
905	struct adm1026_data *data = adm1026_update_device(dev);
906	return sprintf(buf, "%d\n", data->fan_div[nr]);
907}
908static ssize_t set_fan_div(struct device *dev, struct device_attribute *attr,
909		const char *buf, size_t count)
910{
911	struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
912	int nr = sensor_attr->index;
913	struct i2c_client *client = to_i2c_client(dev);
914	struct adm1026_data *data = i2c_get_clientdata(client);
915	int val, orig_div, new_div;
916
917	val = simple_strtol(buf, NULL, 10);
918	new_div = DIV_TO_REG(val);
919
920	mutex_lock(&data->update_lock);
921	orig_div = data->fan_div[nr];
922	data->fan_div[nr] = DIV_FROM_REG(new_div);
923
924	if (nr < 4) { /* 0 <= nr < 4 */
925		adm1026_write_value(client, ADM1026_REG_FAN_DIV_0_3,
926				    (DIV_TO_REG(data->fan_div[0]) << 0) |
927				    (DIV_TO_REG(data->fan_div[1]) << 2) |
928				    (DIV_TO_REG(data->fan_div[2]) << 4) |
929				    (DIV_TO_REG(data->fan_div[3]) << 6));
930	} else { /* 3 < nr < 8 */
931		adm1026_write_value(client, ADM1026_REG_FAN_DIV_4_7,
932				    (DIV_TO_REG(data->fan_div[4]) << 0) |
933				    (DIV_TO_REG(data->fan_div[5]) << 2) |
934				    (DIV_TO_REG(data->fan_div[6]) << 4) |
935				    (DIV_TO_REG(data->fan_div[7]) << 6));
936	}
937
938	if (data->fan_div[nr] != orig_div) {
939		fixup_fan_min(dev, nr, orig_div);
940	}
941	mutex_unlock(&data->update_lock);
942	return count;
943}
944
945#define fan_offset_div(offset)						\
946static SENSOR_DEVICE_ATTR(fan##offset##_div, S_IRUGO | S_IWUSR,		\
947		show_fan_div, set_fan_div, offset - 1);
948
949fan_offset_div(1);
950fan_offset_div(2);
951fan_offset_div(3);
952fan_offset_div(4);
953fan_offset_div(5);
954fan_offset_div(6);
955fan_offset_div(7);
956fan_offset_div(8);
957
958/* Temps */
959static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
960		char *buf)
961{
962	struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
963	int nr = sensor_attr->index;
964	struct adm1026_data *data = adm1026_update_device(dev);
965	return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[nr]));
966}
967static ssize_t show_temp_min(struct device *dev, struct device_attribute *attr,
968		char *buf)
969{
970	struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
971	int nr = sensor_attr->index;
972	struct adm1026_data *data = adm1026_update_device(dev);
973	return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_min[nr]));
974}
975static ssize_t set_temp_min(struct device *dev, struct device_attribute *attr,
976		const char *buf, size_t count)
977{
978	struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
979	int nr = sensor_attr->index;
980	struct i2c_client *client = to_i2c_client(dev);
981	struct adm1026_data *data = i2c_get_clientdata(client);
982	int val = simple_strtol(buf, NULL, 10);
983
984	mutex_lock(&data->update_lock);
985	data->temp_min[nr] = TEMP_TO_REG(val);
986	adm1026_write_value(client, ADM1026_REG_TEMP_MIN[nr],
987		data->temp_min[nr]);
988	mutex_unlock(&data->update_lock);
989	return count;
990}
991static ssize_t show_temp_max(struct device *dev, struct device_attribute *attr,
992		char *buf)
993{
994	struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
995	int nr = sensor_attr->index;
996	struct adm1026_data *data = adm1026_update_device(dev);
997	return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_max[nr]));
998}
999static ssize_t set_temp_max(struct device *dev, struct device_attribute *attr,
1000		const char *buf, size_t count)
1001{
1002	struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1003	int nr = sensor_attr->index;
1004	struct i2c_client *client = to_i2c_client(dev);
1005	struct adm1026_data *data = i2c_get_clientdata(client);
1006	int val = simple_strtol(buf, NULL, 10);
1007
1008	mutex_lock(&data->update_lock);
1009	data->temp_max[nr] = TEMP_TO_REG(val);
1010	adm1026_write_value(client, ADM1026_REG_TEMP_MAX[nr],
1011		data->temp_max[nr]);
1012	mutex_unlock(&data->update_lock);
1013	return count;
1014}
1015
1016#define temp_reg(offset)						\
1017static SENSOR_DEVICE_ATTR(temp##offset##_input, S_IRUGO, show_temp,	\
1018		NULL, offset - 1);					\
1019static SENSOR_DEVICE_ATTR(temp##offset##_min, S_IRUGO | S_IWUSR,	\
1020		show_temp_min, set_temp_min, offset - 1);		\
1021static SENSOR_DEVICE_ATTR(temp##offset##_max, S_IRUGO | S_IWUSR,	\
1022		show_temp_max, set_temp_max, offset - 1);
1023
1024
1025temp_reg(1);
1026temp_reg(2);
1027temp_reg(3);
1028
1029static ssize_t show_temp_offset(struct device *dev,
1030		struct device_attribute *attr, char *buf)
1031{
1032	struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1033	int nr = sensor_attr->index;
1034	struct adm1026_data *data = adm1026_update_device(dev);
1035	return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_offset[nr]));
1036}
1037static ssize_t set_temp_offset(struct device *dev,
1038		struct device_attribute *attr, const char *buf,
1039		size_t count)
1040{
1041	struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1042	int nr = sensor_attr->index;
1043	struct i2c_client *client = to_i2c_client(dev);
1044	struct adm1026_data *data = i2c_get_clientdata(client);
1045	int val = simple_strtol(buf, NULL, 10);
1046
1047	mutex_lock(&data->update_lock);
1048	data->temp_offset[nr] = TEMP_TO_REG(val);
1049	adm1026_write_value(client, ADM1026_REG_TEMP_OFFSET[nr],
1050		data->temp_offset[nr]);
1051	mutex_unlock(&data->update_lock);
1052	return count;
1053}
1054
1055#define temp_offset_reg(offset)							\
1056static SENSOR_DEVICE_ATTR(temp##offset##_offset, S_IRUGO | S_IWUSR,		\
1057		show_temp_offset, set_temp_offset, offset - 1);
1058
1059temp_offset_reg(1);
1060temp_offset_reg(2);
1061temp_offset_reg(3);
1062
1063static ssize_t show_temp_auto_point1_temp_hyst(struct device *dev,
1064		struct device_attribute *attr, char *buf)
1065{
1066	struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1067	int nr = sensor_attr->index;
1068	struct adm1026_data *data = adm1026_update_device(dev);
1069	return sprintf(buf, "%d\n", TEMP_FROM_REG(
1070		ADM1026_FAN_ACTIVATION_TEMP_HYST + data->temp_tmin[nr]));
1071}
1072static ssize_t show_temp_auto_point2_temp(struct device *dev,
1073		struct device_attribute *attr, char *buf)
1074{
1075	struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1076	int nr = sensor_attr->index;
1077	struct adm1026_data *data = adm1026_update_device(dev);
1078	return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_tmin[nr] +
1079		ADM1026_FAN_CONTROL_TEMP_RANGE));
1080}
1081static ssize_t show_temp_auto_point1_temp(struct device *dev,
1082		struct device_attribute *attr, char *buf)
1083{
1084	struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1085	int nr = sensor_attr->index;
1086	struct adm1026_data *data = adm1026_update_device(dev);
1087	return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_tmin[nr]));
1088}
1089static ssize_t set_temp_auto_point1_temp(struct device *dev,
1090		struct device_attribute *attr, const char *buf, size_t count)
1091{
1092	struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1093	int nr = sensor_attr->index;
1094	struct i2c_client *client = to_i2c_client(dev);
1095	struct adm1026_data *data = i2c_get_clientdata(client);
1096	int val = simple_strtol(buf, NULL, 10);
1097
1098	mutex_lock(&data->update_lock);
1099	data->temp_tmin[nr] = TEMP_TO_REG(val);
1100	adm1026_write_value(client, ADM1026_REG_TEMP_TMIN[nr],
1101		data->temp_tmin[nr]);
1102	mutex_unlock(&data->update_lock);
1103	return count;
1104}
1105
1106#define temp_auto_point(offset)						\
1107static SENSOR_DEVICE_ATTR(temp##offset##_auto_point1_temp,		\
1108		S_IRUGO | S_IWUSR, show_temp_auto_point1_temp,		\
1109		set_temp_auto_point1_temp, offset - 1);			\
1110static SENSOR_DEVICE_ATTR(temp##offset##_auto_point1_temp_hyst, S_IRUGO,\
1111		show_temp_auto_point1_temp_hyst, NULL, offset - 1);	\
1112static SENSOR_DEVICE_ATTR(temp##offset##_auto_point2_temp, S_IRUGO,	\
1113		show_temp_auto_point2_temp, NULL, offset - 1);
1114
1115temp_auto_point(1);
1116temp_auto_point(2);
1117temp_auto_point(3);
1118
1119static ssize_t show_temp_crit_enable(struct device *dev,
1120		struct device_attribute *attr, char *buf)
1121{
1122	struct adm1026_data *data = adm1026_update_device(dev);
1123	return sprintf(buf, "%d\n", (data->config1 & CFG1_THERM_HOT) >> 4);
1124}
1125static ssize_t set_temp_crit_enable(struct device *dev,
1126		struct device_attribute *attr, const char *buf, size_t count)
1127{
1128	struct i2c_client *client = to_i2c_client(dev);
1129	struct adm1026_data *data = i2c_get_clientdata(client);
1130	int val = simple_strtol(buf, NULL, 10);
1131
1132	if ((val == 1) || (val==0)) {
1133		mutex_lock(&data->update_lock);
1134		data->config1 = (data->config1 & ~CFG1_THERM_HOT) | (val << 4);
1135		adm1026_write_value(client, ADM1026_REG_CONFIG1,
1136			data->config1);
1137		mutex_unlock(&data->update_lock);
1138	}
1139	return count;
1140}
1141
1142#define temp_crit_enable(offset)				\
1143static DEVICE_ATTR(temp##offset##_crit_enable, S_IRUGO | S_IWUSR, \
1144	show_temp_crit_enable, set_temp_crit_enable);
1145
1146temp_crit_enable(1);
1147temp_crit_enable(2);
1148temp_crit_enable(3);
1149
1150static ssize_t show_temp_crit(struct device *dev,
1151		struct device_attribute *attr, char *buf)
1152{
1153	struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1154	int nr = sensor_attr->index;
1155	struct adm1026_data *data = adm1026_update_device(dev);
1156	return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_crit[nr]));
1157}
1158static ssize_t set_temp_crit(struct device *dev, struct device_attribute *attr,
1159		const char *buf, size_t count)
1160{
1161	struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr);
1162	int nr = sensor_attr->index;
1163	struct i2c_client *client = to_i2c_client(dev);
1164	struct adm1026_data *data = i2c_get_clientdata(client);
1165	int val = simple_strtol(buf, NULL, 10);
1166
1167	mutex_lock(&data->update_lock);
1168	data->temp_crit[nr] = TEMP_TO_REG(val);
1169	adm1026_write_value(client, ADM1026_REG_TEMP_THERM[nr],
1170		data->temp_crit[nr]);
1171	mutex_unlock(&data->update_lock);
1172	return count;
1173}
1174
1175#define temp_crit_reg(offset)						\
1176static SENSOR_DEVICE_ATTR(temp##offset##_crit, S_IRUGO | S_IWUSR,	\
1177		show_temp_crit, set_temp_crit, offset - 1);
1178
1179temp_crit_reg(1);
1180temp_crit_reg(2);
1181temp_crit_reg(3);
1182
1183static ssize_t show_analog_out_reg(struct device *dev, struct device_attribute *attr, char *buf)
1184{
1185	struct adm1026_data *data = adm1026_update_device(dev);
1186	return sprintf(buf, "%d\n", DAC_FROM_REG(data->analog_out));
1187}
1188static ssize_t set_analog_out_reg(struct device *dev, struct device_attribute *attr, const char *buf,
1189		size_t count)
1190{
1191	struct i2c_client *client = to_i2c_client(dev);
1192	struct adm1026_data *data = i2c_get_clientdata(client);
1193	int val = simple_strtol(buf, NULL, 10);
1194
1195	mutex_lock(&data->update_lock);
1196	data->analog_out = DAC_TO_REG(val);
1197	adm1026_write_value(client, ADM1026_REG_DAC, data->analog_out);
1198	mutex_unlock(&data->update_lock);
1199	return count;
1200}
1201
1202static DEVICE_ATTR(analog_out, S_IRUGO | S_IWUSR, show_analog_out_reg,
1203	set_analog_out_reg);
1204
1205static ssize_t show_vid_reg(struct device *dev, struct device_attribute *attr, char *buf)
1206{
1207	struct adm1026_data *data = adm1026_update_device(dev);
1208	int vid = (data->gpio >> 11) & 0x1f;
1209
1210	dev_dbg(dev, "Setting VID from GPIO11-15.\n");
1211	return sprintf(buf, "%d\n", vid_from_reg(vid, data->vrm));
1212}
1213static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid_reg, NULL);
1214
1215static ssize_t show_vrm_reg(struct device *dev, struct device_attribute *attr, char *buf)
1216{
1217	struct adm1026_data *data = dev_get_drvdata(dev);
1218	return sprintf(buf, "%d\n", data->vrm);
1219}
1220static ssize_t store_vrm_reg(struct device *dev, struct device_attribute *attr, const char *buf,
1221		size_t count)
1222{
1223	struct adm1026_data *data = dev_get_drvdata(dev);
1224
1225	data->vrm = simple_strtol(buf, NULL, 10);
1226	return count;
1227}
1228
1229static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm_reg, store_vrm_reg);
1230
1231static ssize_t show_alarms_reg(struct device *dev, struct device_attribute *attr, char *buf)
1232{
1233	struct adm1026_data *data = adm1026_update_device(dev);
1234	return sprintf(buf, "%ld\n", data->alarms);
1235}
1236
1237static DEVICE_ATTR(alarms, S_IRUGO, show_alarms_reg, NULL);
1238
1239static ssize_t show_alarm(struct device *dev, struct device_attribute *attr,
1240			  char *buf)
1241{
1242	struct adm1026_data *data = adm1026_update_device(dev);
1243	int bitnr = to_sensor_dev_attr(attr)->index;
1244	return sprintf(buf, "%ld\n", (data->alarms >> bitnr) & 1);
1245}
1246
1247static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 0);
1248static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, 1);
1249static SENSOR_DEVICE_ATTR(in9_alarm, S_IRUGO, show_alarm, NULL, 1);
1250static SENSOR_DEVICE_ATTR(in11_alarm, S_IRUGO, show_alarm, NULL, 2);
1251static SENSOR_DEVICE_ATTR(in12_alarm, S_IRUGO, show_alarm, NULL, 3);
1252static SENSOR_DEVICE_ATTR(in13_alarm, S_IRUGO, show_alarm, NULL, 4);
1253static SENSOR_DEVICE_ATTR(in14_alarm, S_IRUGO, show_alarm, NULL, 5);
1254static SENSOR_DEVICE_ATTR(in15_alarm, S_IRUGO, show_alarm, NULL, 6);
1255static SENSOR_DEVICE_ATTR(in16_alarm, S_IRUGO, show_alarm, NULL, 7);
1256static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 8);
1257static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 9);
1258static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 10);
1259static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 11);
1260static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 12);
1261static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 13);
1262static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 14);
1263static SENSOR_DEVICE_ATTR(in7_alarm, S_IRUGO, show_alarm, NULL, 15);
1264static SENSOR_DEVICE_ATTR(fan1_alarm, S_IRUGO, show_alarm, NULL, 16);
1265static SENSOR_DEVICE_ATTR(fan2_alarm, S_IRUGO, show_alarm, NULL, 17);
1266static SENSOR_DEVICE_ATTR(fan3_alarm, S_IRUGO, show_alarm, NULL, 18);
1267static SENSOR_DEVICE_ATTR(fan4_alarm, S_IRUGO, show_alarm, NULL, 19);
1268static SENSOR_DEVICE_ATTR(fan5_alarm, S_IRUGO, show_alarm, NULL, 20);
1269static SENSOR_DEVICE_ATTR(fan6_alarm, S_IRUGO, show_alarm, NULL, 21);
1270static SENSOR_DEVICE_ATTR(fan7_alarm, S_IRUGO, show_alarm, NULL, 22);
1271static SENSOR_DEVICE_ATTR(fan8_alarm, S_IRUGO, show_alarm, NULL, 23);
1272static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 24);
1273static SENSOR_DEVICE_ATTR(in10_alarm, S_IRUGO, show_alarm, NULL, 25);
1274static SENSOR_DEVICE_ATTR(in8_alarm, S_IRUGO, show_alarm, NULL, 26);
1275
1276static ssize_t show_alarm_mask(struct device *dev, struct device_attribute *attr, char *buf)
1277{
1278	struct adm1026_data *data = adm1026_update_device(dev);
1279	return sprintf(buf, "%ld\n", data->alarm_mask);
1280}
1281static ssize_t set_alarm_mask(struct device *dev, struct device_attribute *attr, const char *buf,
1282		size_t count)
1283{
1284	struct i2c_client *client = to_i2c_client(dev);
1285	struct adm1026_data *data = i2c_get_clientdata(client);
1286	int val = simple_strtol(buf, NULL, 10);
1287	unsigned long mask;
1288
1289	mutex_lock(&data->update_lock);
1290	data->alarm_mask = val & 0x7fffffff;
1291	mask = data->alarm_mask
1292		| (data->gpio_mask & 0x10000 ? 0x80000000 : 0);
1293	adm1026_write_value(client, ADM1026_REG_MASK1,
1294		mask & 0xff);
1295	mask >>= 8;
1296	adm1026_write_value(client, ADM1026_REG_MASK2,
1297		mask & 0xff);
1298	mask >>= 8;
1299	adm1026_write_value(client, ADM1026_REG_MASK3,
1300		mask & 0xff);
1301	mask >>= 8;
1302	adm1026_write_value(client, ADM1026_REG_MASK4,
1303		mask & 0xff);
1304	mutex_unlock(&data->update_lock);
1305	return count;
1306}
1307
1308static DEVICE_ATTR(alarm_mask, S_IRUGO | S_IWUSR, show_alarm_mask,
1309	set_alarm_mask);
1310
1311
1312static ssize_t show_gpio(struct device *dev, struct device_attribute *attr, char *buf)
1313{
1314	struct adm1026_data *data = adm1026_update_device(dev);
1315	return sprintf(buf, "%ld\n", data->gpio);
1316}
1317static ssize_t set_gpio(struct device *dev, struct device_attribute *attr, const char *buf,
1318		size_t count)
1319{
1320	struct i2c_client *client = to_i2c_client(dev);
1321	struct adm1026_data *data = i2c_get_clientdata(client);
1322	int val = simple_strtol(buf, NULL, 10);
1323	long gpio;
1324
1325	mutex_lock(&data->update_lock);
1326	data->gpio = val & 0x1ffff;
1327	gpio = data->gpio;
1328	adm1026_write_value(client, ADM1026_REG_GPIO_STATUS_0_7, gpio & 0xff);
1329	gpio >>= 8;
1330	adm1026_write_value(client, ADM1026_REG_GPIO_STATUS_8_15, gpio & 0xff);
1331	gpio = ((gpio >> 1) & 0x80) | (data->alarms >> 24 & 0x7f);
1332	adm1026_write_value(client, ADM1026_REG_STATUS4, gpio & 0xff);
1333	mutex_unlock(&data->update_lock);
1334	return count;
1335}
1336
1337static DEVICE_ATTR(gpio, S_IRUGO | S_IWUSR, show_gpio, set_gpio);
1338
1339
1340static ssize_t show_gpio_mask(struct device *dev, struct device_attribute *attr, char *buf)
1341{
1342	struct adm1026_data *data = adm1026_update_device(dev);
1343	return sprintf(buf, "%ld\n", data->gpio_mask);
1344}
1345static ssize_t set_gpio_mask(struct device *dev, struct device_attribute *attr, const char *buf,
1346		size_t count)
1347{
1348	struct i2c_client *client = to_i2c_client(dev);
1349	struct adm1026_data *data = i2c_get_clientdata(client);
1350	int val = simple_strtol(buf, NULL, 10);
1351	long mask;
1352
1353	mutex_lock(&data->update_lock);
1354	data->gpio_mask = val & 0x1ffff;
1355	mask = data->gpio_mask;
1356	adm1026_write_value(client, ADM1026_REG_GPIO_MASK_0_7, mask & 0xff);
1357	mask >>= 8;
1358	adm1026_write_value(client, ADM1026_REG_GPIO_MASK_8_15, mask & 0xff);
1359	mask = ((mask >> 1) & 0x80) | (data->alarm_mask >> 24 & 0x7f);
1360	adm1026_write_value(client, ADM1026_REG_MASK1, mask & 0xff);
1361	mutex_unlock(&data->update_lock);
1362	return count;
1363}
1364
1365static DEVICE_ATTR(gpio_mask, S_IRUGO | S_IWUSR, show_gpio_mask, set_gpio_mask);
1366
1367static ssize_t show_pwm_reg(struct device *dev, struct device_attribute *attr, char *buf)
1368{
1369	struct adm1026_data *data = adm1026_update_device(dev);
1370	return sprintf(buf, "%d\n", PWM_FROM_REG(data->pwm1.pwm));
1371}
1372static ssize_t set_pwm_reg(struct device *dev, struct device_attribute *attr, const char *buf,
1373		size_t count)
1374{
1375	struct i2c_client *client = to_i2c_client(dev);
1376	struct adm1026_data *data = i2c_get_clientdata(client);
1377
1378	if (data->pwm1.enable == 1) {
1379		int val = simple_strtol(buf, NULL, 10);
1380
1381		mutex_lock(&data->update_lock);
1382		data->pwm1.pwm = PWM_TO_REG(val);
1383		adm1026_write_value(client, ADM1026_REG_PWM, data->pwm1.pwm);
1384		mutex_unlock(&data->update_lock);
1385	}
1386	return count;
1387}
1388static ssize_t show_auto_pwm_min(struct device *dev, struct device_attribute *attr, char *buf)
1389{
1390	struct adm1026_data *data = adm1026_update_device(dev);
1391	return sprintf(buf, "%d\n", data->pwm1.auto_pwm_min);
1392}
1393static ssize_t set_auto_pwm_min(struct device *dev, struct device_attribute *attr, const char *buf,
1394		size_t count)
1395{
1396	struct i2c_client *client = to_i2c_client(dev);
1397	struct adm1026_data *data = i2c_get_clientdata(client);
1398	int val = simple_strtol(buf, NULL, 10);
1399
1400	mutex_lock(&data->update_lock);
1401	data->pwm1.auto_pwm_min = SENSORS_LIMIT(val, 0, 255);
1402	if (data->pwm1.enable == 2) { /* apply immediately */
1403		data->pwm1.pwm = PWM_TO_REG((data->pwm1.pwm & 0x0f) |
1404			PWM_MIN_TO_REG(data->pwm1.auto_pwm_min));
1405		adm1026_write_value(client, ADM1026_REG_PWM, data->pwm1.pwm);
1406	}
1407	mutex_unlock(&data->update_lock);
1408	return count;
1409}
1410static ssize_t show_auto_pwm_max(struct device *dev, struct device_attribute *attr, char *buf)
1411{
1412	return sprintf(buf, "%d\n", ADM1026_PWM_MAX);
1413}
1414static ssize_t show_pwm_enable(struct device *dev, struct device_attribute *attr, char *buf)
1415{
1416	struct adm1026_data *data = adm1026_update_device(dev);
1417	return sprintf(buf, "%d\n", data->pwm1.enable);
1418}
1419static ssize_t set_pwm_enable(struct device *dev, struct device_attribute *attr, const char *buf,
1420		size_t count)
1421{
1422	struct i2c_client *client = to_i2c_client(dev);
1423	struct adm1026_data *data = i2c_get_clientdata(client);
1424	int val = simple_strtol(buf, NULL, 10);
1425	int old_enable;
1426
1427	if ((val >= 0) && (val < 3)) {
1428		mutex_lock(&data->update_lock);
1429		old_enable = data->pwm1.enable;
1430		data->pwm1.enable = val;
1431		data->config1 = (data->config1 & ~CFG1_PWM_AFC)
1432				| ((val == 2) ? CFG1_PWM_AFC : 0);
1433		adm1026_write_value(client, ADM1026_REG_CONFIG1,
1434			data->config1);
1435		if (val == 2) { /* apply pwm1_auto_pwm_min to pwm1 */
1436			data->pwm1.pwm = PWM_TO_REG((data->pwm1.pwm & 0x0f) |
1437				PWM_MIN_TO_REG(data->pwm1.auto_pwm_min));
1438			adm1026_write_value(client, ADM1026_REG_PWM,
1439				data->pwm1.pwm);
1440		} else if (!((old_enable == 1) && (val == 1))) {
1441			/* set pwm to safe value */
1442			data->pwm1.pwm = 255;
1443			adm1026_write_value(client, ADM1026_REG_PWM,
1444				data->pwm1.pwm);
1445		}
1446		mutex_unlock(&data->update_lock);
1447	}
1448	return count;
1449}
1450
1451/* enable PWM fan control */
1452static DEVICE_ATTR(pwm1, S_IRUGO | S_IWUSR, show_pwm_reg, set_pwm_reg);
1453static DEVICE_ATTR(pwm2, S_IRUGO | S_IWUSR, show_pwm_reg, set_pwm_reg);
1454static DEVICE_ATTR(pwm3, S_IRUGO | S_IWUSR, show_pwm_reg, set_pwm_reg);
1455static DEVICE_ATTR(pwm1_enable, S_IRUGO | S_IWUSR, show_pwm_enable,
1456	set_pwm_enable);
1457static DEVICE_ATTR(pwm2_enable, S_IRUGO | S_IWUSR, show_pwm_enable,
1458	set_pwm_enable);
1459static DEVICE_ATTR(pwm3_enable, S_IRUGO | S_IWUSR, show_pwm_enable,
1460	set_pwm_enable);
1461static DEVICE_ATTR(temp1_auto_point1_pwm, S_IRUGO | S_IWUSR,
1462	show_auto_pwm_min, set_auto_pwm_min);
1463static DEVICE_ATTR(temp2_auto_point1_pwm, S_IRUGO | S_IWUSR,
1464	show_auto_pwm_min, set_auto_pwm_min);
1465static DEVICE_ATTR(temp3_auto_point1_pwm, S_IRUGO | S_IWUSR,
1466	show_auto_pwm_min, set_auto_pwm_min);
1467
1468static DEVICE_ATTR(temp1_auto_point2_pwm, S_IRUGO, show_auto_pwm_max, NULL);
1469static DEVICE_ATTR(temp2_auto_point2_pwm, S_IRUGO, show_auto_pwm_max, NULL);
1470static DEVICE_ATTR(temp3_auto_point2_pwm, S_IRUGO, show_auto_pwm_max, NULL);
1471
1472static struct attribute *adm1026_attributes[] = {
1473	&sensor_dev_attr_in0_input.dev_attr.attr,
1474	&sensor_dev_attr_in0_max.dev_attr.attr,
1475	&sensor_dev_attr_in0_min.dev_attr.attr,
1476	&sensor_dev_attr_in0_alarm.dev_attr.attr,
1477	&sensor_dev_attr_in1_input.dev_attr.attr,
1478	&sensor_dev_attr_in1_max.dev_attr.attr,
1479	&sensor_dev_attr_in1_min.dev_attr.attr,
1480	&sensor_dev_attr_in1_alarm.dev_attr.attr,
1481	&sensor_dev_attr_in2_input.dev_attr.attr,
1482	&sensor_dev_attr_in2_max.dev_attr.attr,
1483	&sensor_dev_attr_in2_min.dev_attr.attr,
1484	&sensor_dev_attr_in2_alarm.dev_attr.attr,
1485	&sensor_dev_attr_in3_input.dev_attr.attr,
1486	&sensor_dev_attr_in3_max.dev_attr.attr,
1487	&sensor_dev_attr_in3_min.dev_attr.attr,
1488	&sensor_dev_attr_in3_alarm.dev_attr.attr,
1489	&sensor_dev_attr_in4_input.dev_attr.attr,
1490	&sensor_dev_attr_in4_max.dev_attr.attr,
1491	&sensor_dev_attr_in4_min.dev_attr.attr,
1492	&sensor_dev_attr_in4_alarm.dev_attr.attr,
1493	&sensor_dev_attr_in5_input.dev_attr.attr,
1494	&sensor_dev_attr_in5_max.dev_attr.attr,
1495	&sensor_dev_attr_in5_min.dev_attr.attr,
1496	&sensor_dev_attr_in5_alarm.dev_attr.attr,
1497	&sensor_dev_attr_in6_input.dev_attr.attr,
1498	&sensor_dev_attr_in6_max.dev_attr.attr,
1499	&sensor_dev_attr_in6_min.dev_attr.attr,
1500	&sensor_dev_attr_in6_alarm.dev_attr.attr,
1501	&sensor_dev_attr_in7_input.dev_attr.attr,
1502	&sensor_dev_attr_in7_max.dev_attr.attr,
1503	&sensor_dev_attr_in7_min.dev_attr.attr,
1504	&sensor_dev_attr_in7_alarm.dev_attr.attr,
1505	&sensor_dev_attr_in10_input.dev_attr.attr,
1506	&sensor_dev_attr_in10_max.dev_attr.attr,
1507	&sensor_dev_attr_in10_min.dev_attr.attr,
1508	&sensor_dev_attr_in10_alarm.dev_attr.attr,
1509	&sensor_dev_attr_in11_input.dev_attr.attr,
1510	&sensor_dev_attr_in11_max.dev_attr.attr,
1511	&sensor_dev_attr_in11_min.dev_attr.attr,
1512	&sensor_dev_attr_in11_alarm.dev_attr.attr,
1513	&sensor_dev_attr_in12_input.dev_attr.attr,
1514	&sensor_dev_attr_in12_max.dev_attr.attr,
1515	&sensor_dev_attr_in12_min.dev_attr.attr,
1516	&sensor_dev_attr_in12_alarm.dev_attr.attr,
1517	&sensor_dev_attr_in13_input.dev_attr.attr,
1518	&sensor_dev_attr_in13_max.dev_attr.attr,
1519	&sensor_dev_attr_in13_min.dev_attr.attr,
1520	&sensor_dev_attr_in13_alarm.dev_attr.attr,
1521	&sensor_dev_attr_in14_input.dev_attr.attr,
1522	&sensor_dev_attr_in14_max.dev_attr.attr,
1523	&sensor_dev_attr_in14_min.dev_attr.attr,
1524	&sensor_dev_attr_in14_alarm.dev_attr.attr,
1525	&sensor_dev_attr_in15_input.dev_attr.attr,
1526	&sensor_dev_attr_in15_max.dev_attr.attr,
1527	&sensor_dev_attr_in15_min.dev_attr.attr,
1528	&sensor_dev_attr_in15_alarm.dev_attr.attr,
1529	&sensor_dev_attr_in16_input.dev_attr.attr,
1530	&sensor_dev_attr_in16_max.dev_attr.attr,
1531	&sensor_dev_attr_in16_min.dev_attr.attr,
1532	&sensor_dev_attr_in16_alarm.dev_attr.attr,
1533	&sensor_dev_attr_fan1_input.dev_attr.attr,
1534	&sensor_dev_attr_fan1_div.dev_attr.attr,
1535	&sensor_dev_attr_fan1_min.dev_attr.attr,
1536	&sensor_dev_attr_fan1_alarm.dev_attr.attr,
1537	&sensor_dev_attr_fan2_input.dev_attr.attr,
1538	&sensor_dev_attr_fan2_div.dev_attr.attr,
1539	&sensor_dev_attr_fan2_min.dev_attr.attr,
1540	&sensor_dev_attr_fan2_alarm.dev_attr.attr,
1541	&sensor_dev_attr_fan3_input.dev_attr.attr,
1542	&sensor_dev_attr_fan3_div.dev_attr.attr,
1543	&sensor_dev_attr_fan3_min.dev_attr.attr,
1544	&sensor_dev_attr_fan3_alarm.dev_attr.attr,
1545	&sensor_dev_attr_fan4_input.dev_attr.attr,
1546	&sensor_dev_attr_fan4_div.dev_attr.attr,
1547	&sensor_dev_attr_fan4_min.dev_attr.attr,
1548	&sensor_dev_attr_fan4_alarm.dev_attr.attr,
1549	&sensor_dev_attr_fan5_input.dev_attr.attr,
1550	&sensor_dev_attr_fan5_div.dev_attr.attr,
1551	&sensor_dev_attr_fan5_min.dev_attr.attr,
1552	&sensor_dev_attr_fan5_alarm.dev_attr.attr,
1553	&sensor_dev_attr_fan6_input.dev_attr.attr,
1554	&sensor_dev_attr_fan6_div.dev_attr.attr,
1555	&sensor_dev_attr_fan6_min.dev_attr.attr,
1556	&sensor_dev_attr_fan6_alarm.dev_attr.attr,
1557	&sensor_dev_attr_fan7_input.dev_attr.attr,
1558	&sensor_dev_attr_fan7_div.dev_attr.attr,
1559	&sensor_dev_attr_fan7_min.dev_attr.attr,
1560	&sensor_dev_attr_fan7_alarm.dev_attr.attr,
1561	&sensor_dev_attr_fan8_input.dev_attr.attr,
1562	&sensor_dev_attr_fan8_div.dev_attr.attr,
1563	&sensor_dev_attr_fan8_min.dev_attr.attr,
1564	&sensor_dev_attr_fan8_alarm.dev_attr.attr,
1565	&sensor_dev_attr_temp1_input.dev_attr.attr,
1566	&sensor_dev_attr_temp1_max.dev_attr.attr,
1567	&sensor_dev_attr_temp1_min.dev_attr.attr,
1568	&sensor_dev_attr_temp1_alarm.dev_attr.attr,
1569	&sensor_dev_attr_temp2_input.dev_attr.attr,
1570	&sensor_dev_attr_temp2_max.dev_attr.attr,
1571	&sensor_dev_attr_temp2_min.dev_attr.attr,
1572	&sensor_dev_attr_temp2_alarm.dev_attr.attr,
1573	&sensor_dev_attr_temp1_offset.dev_attr.attr,
1574	&sensor_dev_attr_temp2_offset.dev_attr.attr,
1575	&sensor_dev_attr_temp1_auto_point1_temp.dev_attr.attr,
1576	&sensor_dev_attr_temp2_auto_point1_temp.dev_attr.attr,
1577	&sensor_dev_attr_temp1_auto_point1_temp_hyst.dev_attr.attr,
1578	&sensor_dev_attr_temp2_auto_point1_temp_hyst.dev_attr.attr,
1579	&sensor_dev_attr_temp1_auto_point2_temp.dev_attr.attr,
1580	&sensor_dev_attr_temp2_auto_point2_temp.dev_attr.attr,
1581	&sensor_dev_attr_temp1_crit.dev_attr.attr,
1582	&sensor_dev_attr_temp2_crit.dev_attr.attr,
1583	&dev_attr_temp1_crit_enable.attr,
1584	&dev_attr_temp2_crit_enable.attr,
1585	&dev_attr_cpu0_vid.attr,
1586	&dev_attr_vrm.attr,
1587	&dev_attr_alarms.attr,
1588	&dev_attr_alarm_mask.attr,
1589	&dev_attr_gpio.attr,
1590	&dev_attr_gpio_mask.attr,
1591	&dev_attr_pwm1.attr,
1592	&dev_attr_pwm2.attr,
1593	&dev_attr_pwm3.attr,
1594	&dev_attr_pwm1_enable.attr,
1595	&dev_attr_pwm2_enable.attr,
1596	&dev_attr_pwm3_enable.attr,
1597	&dev_attr_temp1_auto_point1_pwm.attr,
1598	&dev_attr_temp2_auto_point1_pwm.attr,
1599	&dev_attr_temp1_auto_point2_pwm.attr,
1600	&dev_attr_temp2_auto_point2_pwm.attr,
1601	&dev_attr_analog_out.attr,
1602	NULL
1603};
1604
1605static const struct attribute_group adm1026_group = {
1606	.attrs = adm1026_attributes,
1607};
1608
1609static struct attribute *adm1026_attributes_temp3[] = {
1610	&sensor_dev_attr_temp3_input.dev_attr.attr,
1611	&sensor_dev_attr_temp3_max.dev_attr.attr,
1612	&sensor_dev_attr_temp3_min.dev_attr.attr,
1613	&sensor_dev_attr_temp3_alarm.dev_attr.attr,
1614	&sensor_dev_attr_temp3_offset.dev_attr.attr,
1615	&sensor_dev_attr_temp3_auto_point1_temp.dev_attr.attr,
1616	&sensor_dev_attr_temp3_auto_point1_temp_hyst.dev_attr.attr,
1617	&sensor_dev_attr_temp3_auto_point2_temp.dev_attr.attr,
1618	&sensor_dev_attr_temp3_crit.dev_attr.attr,
1619	&dev_attr_temp3_crit_enable.attr,
1620	&dev_attr_temp3_auto_point1_pwm.attr,
1621	&dev_attr_temp3_auto_point2_pwm.attr,
1622	NULL
1623};
1624
1625static const struct attribute_group adm1026_group_temp3 = {
1626	.attrs = adm1026_attributes_temp3,
1627};
1628
1629static struct attribute *adm1026_attributes_in8_9[] = {
1630	&sensor_dev_attr_in8_input.dev_attr.attr,
1631	&sensor_dev_attr_in8_max.dev_attr.attr,
1632	&sensor_dev_attr_in8_min.dev_attr.attr,
1633	&sensor_dev_attr_in8_alarm.dev_attr.attr,
1634	&sensor_dev_attr_in9_input.dev_attr.attr,
1635	&sensor_dev_attr_in9_max.dev_attr.attr,
1636	&sensor_dev_attr_in9_min.dev_attr.attr,
1637	&sensor_dev_attr_in9_alarm.dev_attr.attr,
1638	NULL
1639};
1640
1641static const struct attribute_group adm1026_group_in8_9 = {
1642	.attrs = adm1026_attributes_in8_9,
1643};
1644
1645/* Return 0 if detection is successful, -ENODEV otherwise */
1646static int adm1026_detect(struct i2c_client *client,
1647			  struct i2c_board_info *info)
1648{
1649	struct i2c_adapter *adapter = client->adapter;
1650	int address = client->addr;
1651	int company, verstep;
1652
1653	if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
1654		/* We need to be able to do byte I/O */
1655		return -ENODEV;
1656	};
1657
1658	/* Now, we do the remaining detection. */
1659
1660	company = adm1026_read_value(client, ADM1026_REG_COMPANY);
1661	verstep = adm1026_read_value(client, ADM1026_REG_VERSTEP);
1662
1663	dev_dbg(&adapter->dev, "Detecting device at %d,0x%02x with"
1664		" COMPANY: 0x%02x and VERSTEP: 0x%02x\n",
1665		i2c_adapter_id(client->adapter), client->addr,
1666		company, verstep);
1667
1668	/* Determine the chip type. */
1669	dev_dbg(&adapter->dev, "Autodetecting device at %d,0x%02x...\n",
1670		i2c_adapter_id(adapter), address);
1671	if (company == ADM1026_COMPANY_ANALOG_DEV
1672	    && verstep == ADM1026_VERSTEP_ADM1026) {
1673		/* Analog Devices ADM1026 */
1674	} else if (company == ADM1026_COMPANY_ANALOG_DEV
1675		&& (verstep & 0xf0) == ADM1026_VERSTEP_GENERIC) {
1676		dev_err(&adapter->dev, "Unrecognized stepping "
1677			"0x%02x. Defaulting to ADM1026.\n", verstep);
1678	} else if ((verstep & 0xf0) == ADM1026_VERSTEP_GENERIC) {
1679		dev_err(&adapter->dev, "Found version/stepping "
1680			"0x%02x. Assuming generic ADM1026.\n",
1681			verstep);
1682	} else {
1683		dev_dbg(&adapter->dev, "Autodetection failed\n");
1684		/* Not an ADM1026... */
1685		return -ENODEV;
1686	}
1687
1688	strlcpy(info->type, "adm1026", I2C_NAME_SIZE);
1689
1690	return 0;
1691}
1692
1693static int adm1026_probe(struct i2c_client *client,
1694			 const struct i2c_device_id *id)
1695{
1696	struct adm1026_data *data;
1697	int err;
1698
1699	data = kzalloc(sizeof(struct adm1026_data), GFP_KERNEL);
1700	if (!data) {
1701		err = -ENOMEM;
1702		goto exit;
1703	}
1704
1705	i2c_set_clientdata(client, data);
1706	mutex_init(&data->update_lock);
1707
1708	/* Set the VRM version */
1709	data->vrm = vid_which_vrm();
1710
1711	/* Initialize the ADM1026 chip */
1712	adm1026_init_client(client);
1713
1714	/* Register sysfs hooks */
1715	if ((err = sysfs_create_group(&client->dev.kobj, &adm1026_group)))
1716		goto exitfree;
1717	if (data->config1 & CFG1_AIN8_9)
1718		err = sysfs_create_group(&client->dev.kobj,
1719					 &adm1026_group_in8_9);
1720	else
1721		err = sysfs_create_group(&client->dev.kobj,
1722					 &adm1026_group_temp3);
1723	if (err)
1724		goto exitremove;
1725
1726	data->hwmon_dev = hwmon_device_register(&client->dev);
1727	if (IS_ERR(data->hwmon_dev)) {
1728		err = PTR_ERR(data->hwmon_dev);
1729		goto exitremove;
1730	}
1731
1732	return 0;
1733
1734	/* Error out and cleanup code */
1735exitremove:
1736	sysfs_remove_group(&client->dev.kobj, &adm1026_group);
1737	if (data->config1 & CFG1_AIN8_9)
1738		sysfs_remove_group(&client->dev.kobj, &adm1026_group_in8_9);
1739	else
1740		sysfs_remove_group(&client->dev.kobj, &adm1026_group_temp3);
1741exitfree:
1742	kfree(data);
1743exit:
1744	return err;
1745}
1746
1747static int adm1026_remove(struct i2c_client *client)
1748{
1749	struct adm1026_data *data = i2c_get_clientdata(client);
1750	hwmon_device_unregister(data->hwmon_dev);
1751	sysfs_remove_group(&client->dev.kobj, &adm1026_group);
1752	if (data->config1 & CFG1_AIN8_9)
1753		sysfs_remove_group(&client->dev.kobj, &adm1026_group_in8_9);
1754	else
1755		sysfs_remove_group(&client->dev.kobj, &adm1026_group_temp3);
1756	kfree(data);
1757	return 0;
1758}
1759
1760static int __init sm_adm1026_init(void)
1761{
1762	return i2c_add_driver(&adm1026_driver);
1763}
1764
1765static void __exit sm_adm1026_exit(void)
1766{
1767	i2c_del_driver(&adm1026_driver);
1768}
1769
1770MODULE_LICENSE("GPL");
1771MODULE_AUTHOR("Philip Pokorny <ppokorny@penguincomputing.com>, "
1772	      "Justin Thiessen <jthiessen@penguincomputing.com>");
1773MODULE_DESCRIPTION("ADM1026 driver");
1774
1775module_init(sm_adm1026_init);
1776module_exit(sm_adm1026_exit);
1777