[go: nahoru, domu]

adm1025.c revision 937df8df907ce63b0f7e19adf6e3cdef1687fac3
11da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/*
21da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * adm1025.c
31da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
41da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Copyright (C) 2000       Chen-Yuan Wu <gwu@esoft.com>
51da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Copyright (C) 2003-2004  Jean Delvare <khali@linux-fr.org>
61da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
71da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * The ADM1025 is a sensor chip made by Analog Devices. It reports up to 6
81da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * voltages (including its own power source) and up to two temperatures
91da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * (its own plus up to one external one). Voltages are scaled internally
101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * (which is not the common way) with ratios such that the nominal value
111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * of each voltage correspond to a register value of 192 (which means a
121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * resolution of about 0.5% of the nominal value). Temperature values are
131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * reported with a 1 deg resolution and a 3 deg accuracy. Complete
141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * datasheet can be obtained from Analog's website at:
151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *   http://www.analog.com/Analog_Root/productPage/productHome/0,2121,ADM1025,00.html
161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * This driver also supports the ADM1025A, which differs from the ADM1025
181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * only in that it has "open-drain VID inputs while the ADM1025 has
191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * on-chip 100k pull-ups on the VID inputs". It doesn't make any
201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * difference for us.
211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * This driver also supports the NE1619, a sensor chip made by Philips.
231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * That chip is similar to the ADM1025A, with a few differences. The only
241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * difference that matters to us is that the NE1619 has only two possible
251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * addresses while the ADM1025A has a third one. Complete datasheet can be
261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * obtained from Philips's website at:
271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *   http://www.semiconductors.philips.com/pip/NE1619DS.html
281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Since the ADM1025 was the first chipset supported by this driver, most
301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * comments will refer to this chipset, but are actually general and
311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * concern all supported chipsets, unless mentioned otherwise.
321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * This program is free software; you can redistribute it and/or modify
341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * it under the terms of the GNU General Public License as published by
351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * the Free Software Foundation; either version 2 of the License, or
361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * (at your option) any later version.
371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * This program is distributed in the hope that it will be useful,
391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * but WITHOUT ANY WARRANTY; without even the implied warranty of
401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
411da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * GNU General Public License for more details.
421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
431da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * You should have received a copy of the GNU General Public License
441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * along with this program; if not, write to the Free Software
451da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */
471da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <linux/module.h>
491da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <linux/init.h>
501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <linux/slab.h>
511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <linux/jiffies.h>
521da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <linux/i2c.h>
531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <linux/i2c-sensor.h>
541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <linux/i2c-vid.h>
551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/*
571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Addresses to scan
581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * ADM1025 and ADM1025A have three possible addresses: 0x2c, 0x2d and 0x2e.
591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * NE1619 has two possible addresses: 0x2c and 0x2d.
601da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */
611da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
621da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic unsigned short normal_i2c[] = { 0x2c, 0x2d, 0x2e, I2C_CLIENT_END };
631da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic unsigned int normal_isa[] = { I2C_CLIENT_ISA_END };
641da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/*
661da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Insmod parameters
671da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */
681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus TorvaldsSENSORS_INSMOD_2(adm1025, ne1619);
701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/*
721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * The ADM1025 registers
731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */
741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define ADM1025_REG_MAN_ID		0x3E
761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define ADM1025_REG_CHIP_ID 		0x3F
771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define ADM1025_REG_CONFIG		0x40
781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define ADM1025_REG_STATUS1		0x41
791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define ADM1025_REG_STATUS2		0x42
801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define ADM1025_REG_IN(nr)		(0x20 + (nr))
811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define ADM1025_REG_IN_MAX(nr)		(0x2B + (nr) * 2)
821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define ADM1025_REG_IN_MIN(nr)		(0x2C + (nr) * 2)
831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define ADM1025_REG_TEMP(nr)		(0x26 + (nr))
841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define ADM1025_REG_TEMP_HIGH(nr)	(0x37 + (nr) * 2)
851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define ADM1025_REG_TEMP_LOW(nr)	(0x38 + (nr) * 2)
861da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define ADM1025_REG_VID			0x47
871da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define ADM1025_REG_VID4		0x49
881da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
891da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/*
901da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Conversions and various macros
911da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * The ADM1025 uses signed 8-bit values for temperatures.
921da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */
931da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
941da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic int in_scale[6] = { 2500, 2250, 3300, 5000, 12000, 3300 };
951da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
961da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define IN_FROM_REG(reg,scale)	(((reg) * (scale) + 96) / 192)
971da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define IN_TO_REG(val,scale)	((val) <= 0 ? 0 : \
981da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				 (val) * 192 >= (scale) * 255 ? 255 : \
991da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				 ((val) * 192 + (scale)/2) / (scale))
1001da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1011da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define TEMP_FROM_REG(reg)	((reg) * 1000)
1021da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define TEMP_TO_REG(val)	((val) <= -127500 ? -128 : \
1031da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				 (val) >= 126500 ? 127 : \
1041da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				 (((val) < 0 ? (val)-500 : (val)+500) / 1000))
1051da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1061da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/*
1071da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Functions declaration
1081da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */
1091da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic int adm1025_attach_adapter(struct i2c_adapter *adapter);
1111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic int adm1025_detect(struct i2c_adapter *adapter, int address, int kind);
1121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic void adm1025_init_client(struct i2c_client *client);
1131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic int adm1025_detach_client(struct i2c_client *client);
1141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic struct adm1025_data *adm1025_update_device(struct device *dev);
1151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/*
1171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Driver data (common to all clients)
1181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */
1191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic struct i2c_driver adm1025_driver = {
1211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.owner		= THIS_MODULE,
1221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.name		= "adm1025",
1231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.id		= I2C_DRIVERID_ADM1025,
1241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.flags		= I2C_DF_NOTIFY,
1251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.attach_adapter	= adm1025_attach_adapter,
1261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.detach_client	= adm1025_detach_client,
1271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds};
1281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/*
1301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Client data (each client gets its own)
1311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */
1321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstruct adm1025_data {
1341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct i2c_client client;
1351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct semaphore update_lock;
1361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	char valid; /* zero until following fields are valid */
1371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	unsigned long last_updated; /* in jiffies */
1381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	u8 in[6];		/* register value */
1401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	u8 in_max[6];		/* register value */
1411da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	u8 in_min[6];		/* register value */
1421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	s8 temp[2];		/* register value */
1431da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	s8 temp_min[2];		/* register value */
1441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	s8 temp_max[2];		/* register value */
1451da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	u16 alarms;		/* register values, combined */
1461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	u8 vid;			/* register values, combined */
1471da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	u8 vrm;
1481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds};
1491da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/*
1511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Sysfs stuff
1521da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */
1531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define show_in(offset) \
15574880c063b06efd103c924abfe19d9d8fa4864c4Yani Ioannoustatic ssize_t show_in##offset(struct device *dev, struct device_attribute *attr, char *buf) \
1561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{ \
1571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct adm1025_data *data = adm1025_update_device(dev); \
1581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return sprintf(buf, "%u\n", IN_FROM_REG(data->in[offset], \
1591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		       in_scale[offset])); \
1601da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds} \
16174880c063b06efd103c924abfe19d9d8fa4864c4Yani Ioannoustatic ssize_t show_in##offset##_min(struct device *dev, struct device_attribute *attr, char *buf) \
1621da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{ \
1631da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct adm1025_data *data = adm1025_update_device(dev); \
1641da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return sprintf(buf, "%u\n", IN_FROM_REG(data->in_min[offset], \
1651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		       in_scale[offset])); \
1661da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds} \
16774880c063b06efd103c924abfe19d9d8fa4864c4Yani Ioannoustatic ssize_t show_in##offset##_max(struct device *dev, struct device_attribute *attr, char *buf) \
1681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{ \
1691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct adm1025_data *data = adm1025_update_device(dev); \
1701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return sprintf(buf, "%u\n", IN_FROM_REG(data->in_max[offset], \
1711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		       in_scale[offset])); \
1721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds} \
1731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic DEVICE_ATTR(in##offset##_input, S_IRUGO, show_in##offset, NULL);
1741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsshow_in(0);
1751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsshow_in(1);
1761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsshow_in(2);
1771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsshow_in(3);
1781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsshow_in(4);
1791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsshow_in(5);
1801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define show_temp(offset) \
18274880c063b06efd103c924abfe19d9d8fa4864c4Yani Ioannoustatic ssize_t show_temp##offset(struct device *dev, struct device_attribute *attr, char *buf) \
1831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{ \
1841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct adm1025_data *data = adm1025_update_device(dev); \
1851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[offset-1])); \
1861da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds} \
18774880c063b06efd103c924abfe19d9d8fa4864c4Yani Ioannoustatic ssize_t show_temp##offset##_min(struct device *dev, struct device_attribute *attr, char *buf) \
1881da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{ \
1891da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct adm1025_data *data = adm1025_update_device(dev); \
1901da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_min[offset-1])); \
1911da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds} \
19274880c063b06efd103c924abfe19d9d8fa4864c4Yani Ioannoustatic ssize_t show_temp##offset##_max(struct device *dev, struct device_attribute *attr, char *buf) \
1931da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{ \
1941da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct adm1025_data *data = adm1025_update_device(dev); \
1951da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_max[offset-1])); \
1961da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}\
1971da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic DEVICE_ATTR(temp##offset##_input, S_IRUGO, show_temp##offset, NULL);
1981da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsshow_temp(1);
1991da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsshow_temp(2);
2001da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2011da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define set_in(offset) \
20274880c063b06efd103c924abfe19d9d8fa4864c4Yani Ioannoustatic ssize_t set_in##offset##_min(struct device *dev, struct device_attribute *attr, const char *buf, \
2031da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	size_t count) \
2041da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{ \
2051da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct i2c_client *client = to_i2c_client(dev); \
2061da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct adm1025_data *data = i2c_get_clientdata(client); \
2071da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	long val = simple_strtol(buf, NULL, 10); \
2081da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds \
2091da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	down(&data->update_lock); \
2101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	data->in_min[offset] = IN_TO_REG(val, in_scale[offset]); \
2111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i2c_smbus_write_byte_data(client, ADM1025_REG_IN_MIN(offset), \
2121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				  data->in_min[offset]); \
2131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	up(&data->update_lock); \
2141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return count; \
2151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds} \
21674880c063b06efd103c924abfe19d9d8fa4864c4Yani Ioannoustatic ssize_t set_in##offset##_max(struct device *dev, struct device_attribute *attr, const char *buf, \
2171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	size_t count) \
2181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{ \
2191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct i2c_client *client = to_i2c_client(dev); \
2201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct adm1025_data *data = i2c_get_clientdata(client); \
2211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	long val = simple_strtol(buf, NULL, 10); \
2221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds \
2231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	down(&data->update_lock); \
2241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	data->in_max[offset] = IN_TO_REG(val, in_scale[offset]); \
2251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i2c_smbus_write_byte_data(client, ADM1025_REG_IN_MAX(offset), \
2261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				  data->in_max[offset]); \
2271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	up(&data->update_lock); \
2281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return count; \
2291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds} \
2301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic DEVICE_ATTR(in##offset##_min, S_IWUSR | S_IRUGO, \
2311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	show_in##offset##_min, set_in##offset##_min); \
2321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic DEVICE_ATTR(in##offset##_max, S_IWUSR | S_IRUGO, \
2331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	show_in##offset##_max, set_in##offset##_max);
2341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsset_in(0);
2351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsset_in(1);
2361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsset_in(2);
2371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsset_in(3);
2381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsset_in(4);
2391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsset_in(5);
2401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2411da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define set_temp(offset) \
24274880c063b06efd103c924abfe19d9d8fa4864c4Yani Ioannoustatic ssize_t set_temp##offset##_min(struct device *dev, struct device_attribute *attr, const char *buf, \
2431da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	size_t count) \
2441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{ \
2451da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct i2c_client *client = to_i2c_client(dev); \
2461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct adm1025_data *data = i2c_get_clientdata(client); \
2471da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	long val = simple_strtol(buf, NULL, 10); \
2481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds \
2491da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	down(&data->update_lock); \
2501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	data->temp_min[offset-1] = TEMP_TO_REG(val); \
2511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i2c_smbus_write_byte_data(client, ADM1025_REG_TEMP_LOW(offset-1), \
2521da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				  data->temp_min[offset-1]); \
2531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	up(&data->update_lock); \
2541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return count; \
2551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds} \
25674880c063b06efd103c924abfe19d9d8fa4864c4Yani Ioannoustatic ssize_t set_temp##offset##_max(struct device *dev, struct device_attribute *attr, const char *buf, \
2571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	size_t count) \
2581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{ \
2591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct i2c_client *client = to_i2c_client(dev); \
2601da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct adm1025_data *data = i2c_get_clientdata(client); \
2611da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	long val = simple_strtol(buf, NULL, 10); \
2621da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds \
2631da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	down(&data->update_lock); \
2641da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	data->temp_max[offset-1] = TEMP_TO_REG(val); \
2651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i2c_smbus_write_byte_data(client, ADM1025_REG_TEMP_HIGH(offset-1), \
2661da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				  data->temp_max[offset-1]); \
2671da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	up(&data->update_lock); \
2681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return count; \
2691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds} \
2701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic DEVICE_ATTR(temp##offset##_min, S_IWUSR | S_IRUGO, \
2711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	show_temp##offset##_min, set_temp##offset##_min); \
2721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic DEVICE_ATTR(temp##offset##_max, S_IWUSR | S_IRUGO, \
2731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	show_temp##offset##_max, set_temp##offset##_max);
2741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsset_temp(1);
2751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsset_temp(2);
2761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
27774880c063b06efd103c924abfe19d9d8fa4864c4Yani Ioannoustatic ssize_t show_alarms(struct device *dev, struct device_attribute *attr, char *buf)
2781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
2791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct adm1025_data *data = adm1025_update_device(dev);
2801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return sprintf(buf, "%u\n", data->alarms);
2811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
2821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
2831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
28474880c063b06efd103c924abfe19d9d8fa4864c4Yani Ioannoustatic ssize_t show_vid(struct device *dev, struct device_attribute *attr, char *buf)
2851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
2861da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct adm1025_data *data = adm1025_update_device(dev);
2871da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return sprintf(buf, "%u\n", vid_from_reg(data->vid, data->vrm));
2881da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
289937df8df907ce63b0f7e19adf6e3cdef1687fac3Grant Coady/* in1_ref is deprecated in favour of cpu0_vid, remove after 2005-11-11 */
2901da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic DEVICE_ATTR(in1_ref, S_IRUGO, show_vid, NULL);
291937df8df907ce63b0f7e19adf6e3cdef1687fac3Grant Coadystatic DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid, NULL);
2921da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
29374880c063b06efd103c924abfe19d9d8fa4864c4Yani Ioannoustatic ssize_t show_vrm(struct device *dev, struct device_attribute *attr, char *buf)
2941da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
2951da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct adm1025_data *data = adm1025_update_device(dev);
2961da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return sprintf(buf, "%u\n", data->vrm);
2971da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
29874880c063b06efd103c924abfe19d9d8fa4864c4Yani Ioannoustatic ssize_t set_vrm(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
2991da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
3001da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct i2c_client *client = to_i2c_client(dev);
3011da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct adm1025_data *data = i2c_get_clientdata(client);
3021da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	data->vrm = simple_strtoul(buf, NULL, 10);
3031da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return count;
3041da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
3051da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm, set_vrm);
3061da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3071da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/*
3081da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Real code
3091da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */
3101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic int adm1025_attach_adapter(struct i2c_adapter *adapter)
3121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
3131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (!(adapter->class & I2C_CLASS_HWMON))
3141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return 0;
3151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return i2c_detect(adapter, &addr_data, adm1025_detect);
3161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
3171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/*
3191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * The following function does more than just detection. If detection
3201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * succeeds, it also registers the new chip.
3211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */
3221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic int adm1025_detect(struct i2c_adapter *adapter, int address, int kind)
3231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
3241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct i2c_client *new_client;
3251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct adm1025_data *data;
3261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int err = 0;
3271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	const char *name = "";
3281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	u8 config;
3291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
3311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		goto exit;
3321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (!(data = kmalloc(sizeof(struct adm1025_data), GFP_KERNEL))) {
3341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		err = -ENOMEM;
3351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		goto exit;
3361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
3371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	memset(data, 0, sizeof(struct adm1025_data));
3381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* The common I2C client data is placed right before the
3401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	   ADM1025-specific data. */
3411da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	new_client = &data->client;
3421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i2c_set_clientdata(new_client, data);
3431da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	new_client->addr = address;
3441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	new_client->adapter = adapter;
3451da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	new_client->driver = &adm1025_driver;
3461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	new_client->flags = 0;
3471da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/*
3491da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * Now we do the remaining detection. A negative kind means that
3501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * the driver was loaded with no force parameter (default), so we
3511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * must both detect and identify the chip. A zero kind means that
3521da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * the driver was loaded with the force parameter, the detection
3531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * step shall be skipped. A positive kind means that the driver
3541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * was loaded with the force parameter and a given kind of chip is
3551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * requested, so both the detection and the identification steps
3561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * are skipped.
3571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 */
3581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	config = i2c_smbus_read_byte_data(new_client, ADM1025_REG_CONFIG);
3591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (kind < 0) { /* detection */
3601da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		if ((config & 0x80) != 0x00
3611da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		 || (i2c_smbus_read_byte_data(new_client,
3621da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		     ADM1025_REG_STATUS1) & 0xC0) != 0x00
3631da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		 || (i2c_smbus_read_byte_data(new_client,
3641da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		     ADM1025_REG_STATUS2) & 0xBC) != 0x00) {
3651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			dev_dbg(&adapter->dev,
3661da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				"ADM1025 detection failed at 0x%02x.\n",
3671da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				address);
3681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			goto exit_free;
3691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		}
3701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
3711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (kind <= 0) { /* identification */
3731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		u8 man_id, chip_id;
3741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		man_id = i2c_smbus_read_byte_data(new_client,
3761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			 ADM1025_REG_MAN_ID);
3771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		chip_id = i2c_smbus_read_byte_data(new_client,
3781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			  ADM1025_REG_CHIP_ID);
3791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		if (man_id == 0x41) { /* Analog Devices */
3811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			if ((chip_id & 0xF0) == 0x20) { /* ADM1025/ADM1025A */
3821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				kind = adm1025;
3831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			}
3841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		} else
3851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		if (man_id == 0xA1) { /* Philips */
3861da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			if (address != 0x2E
3871da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			 && (chip_id & 0xF0) == 0x20) { /* NE1619 */
3881da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				kind = ne1619;
3891da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			}
3901da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		}
3911da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3921da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		if (kind <= 0) { /* identification failed */
3931da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			dev_info(&adapter->dev,
3941da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			    "Unsupported chip (man_id=0x%02X, "
3951da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			    "chip_id=0x%02X).\n", man_id, chip_id);
3961da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			goto exit_free;
3971da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		}
3981da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
3991da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4001da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (kind == adm1025) {
4011da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		name = "adm1025";
4021da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	} else if (kind == ne1619) {
4031da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		name = "ne1619";
4041da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
4051da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4061da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* We can fill in the remaining client fields */
4071da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	strlcpy(new_client->name, name, I2C_NAME_SIZE);
4081da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	data->valid = 0;
4091da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	init_MUTEX(&data->update_lock);
4101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* Tell the I2C layer a new client has arrived */
4121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if ((err = i2c_attach_client(new_client)))
4131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		goto exit_free;
4141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* Initialize the ADM1025 chip */
4161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	adm1025_init_client(new_client);
4171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* Register sysfs hooks */
4191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	device_create_file(&new_client->dev, &dev_attr_in0_input);
4201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	device_create_file(&new_client->dev, &dev_attr_in1_input);
4211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	device_create_file(&new_client->dev, &dev_attr_in2_input);
4221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	device_create_file(&new_client->dev, &dev_attr_in3_input);
4231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	device_create_file(&new_client->dev, &dev_attr_in5_input);
4241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	device_create_file(&new_client->dev, &dev_attr_in0_min);
4251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	device_create_file(&new_client->dev, &dev_attr_in1_min);
4261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	device_create_file(&new_client->dev, &dev_attr_in2_min);
4271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	device_create_file(&new_client->dev, &dev_attr_in3_min);
4281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	device_create_file(&new_client->dev, &dev_attr_in5_min);
4291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	device_create_file(&new_client->dev, &dev_attr_in0_max);
4301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	device_create_file(&new_client->dev, &dev_attr_in1_max);
4311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	device_create_file(&new_client->dev, &dev_attr_in2_max);
4321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	device_create_file(&new_client->dev, &dev_attr_in3_max);
4331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	device_create_file(&new_client->dev, &dev_attr_in5_max);
4341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	device_create_file(&new_client->dev, &dev_attr_temp1_input);
4351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	device_create_file(&new_client->dev, &dev_attr_temp2_input);
4361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	device_create_file(&new_client->dev, &dev_attr_temp1_min);
4371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	device_create_file(&new_client->dev, &dev_attr_temp2_min);
4381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	device_create_file(&new_client->dev, &dev_attr_temp1_max);
4391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	device_create_file(&new_client->dev, &dev_attr_temp2_max);
4401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	device_create_file(&new_client->dev, &dev_attr_alarms);
441937df8df907ce63b0f7e19adf6e3cdef1687fac3Grant Coady	/* in1_ref is deprecated, remove after 2005-11-11 */
4421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	device_create_file(&new_client->dev, &dev_attr_in1_ref);
443937df8df907ce63b0f7e19adf6e3cdef1687fac3Grant Coady	device_create_file(&new_client->dev, &dev_attr_cpu0_vid);
4441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	device_create_file(&new_client->dev, &dev_attr_vrm);
4451da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* Pin 11 is either in4 (+12V) or VID4 */
4471da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (!(config & 0x20)) {
4481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		device_create_file(&new_client->dev, &dev_attr_in4_input);
4491da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		device_create_file(&new_client->dev, &dev_attr_in4_min);
4501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		device_create_file(&new_client->dev, &dev_attr_in4_max);
4511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
4521da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return 0;
4541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsexit_free:
4561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	kfree(data);
4571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsexit:
4581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return err;
4591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
4601da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4611da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic void adm1025_init_client(struct i2c_client *client)
4621da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
4631da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	u8 reg;
4641da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct adm1025_data *data = i2c_get_clientdata(client);
4651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int i;
4661da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4671da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	data->vrm = i2c_which_vrm();
4681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/*
4701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * Set high limits
4711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * Usually we avoid setting limits on driver init, but it happens
4721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * that the ADM1025 comes with stupid default limits (all registers
4731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * set to 0). In case the chip has not gone through any limit
4741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * setting yet, we better set the high limits to the max so that
4751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * no alarm triggers.
4761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 */
4771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	for (i=0; i<6; i++) {
4781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		reg = i2c_smbus_read_byte_data(client,
4791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds					       ADM1025_REG_IN_MAX(i));
4801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		if (reg == 0)
4811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			i2c_smbus_write_byte_data(client,
4821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds						  ADM1025_REG_IN_MAX(i),
4831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds						  0xFF);
4841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
4851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	for (i=0; i<2; i++) {
4861da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		reg = i2c_smbus_read_byte_data(client,
4871da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds					       ADM1025_REG_TEMP_HIGH(i));
4881da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		if (reg == 0)
4891da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			i2c_smbus_write_byte_data(client,
4901da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds						  ADM1025_REG_TEMP_HIGH(i),
4911da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds						  0x7F);
4921da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
4931da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4941da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/*
4951da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * Start the conversions
4961da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 */
4971da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	reg = i2c_smbus_read_byte_data(client, ADM1025_REG_CONFIG);
4981da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (!(reg & 0x01))
4991da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		i2c_smbus_write_byte_data(client, ADM1025_REG_CONFIG,
5001da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds					  (reg&0x7E)|0x01);
5011da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
5021da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
5031da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic int adm1025_detach_client(struct i2c_client *client)
5041da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
5051da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int err;
5061da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
5071da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if ((err = i2c_detach_client(client))) {
5081da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		dev_err(&client->dev, "Client deregistration failed, "
5091da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			"client not detached.\n");
5101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return err;
5111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
5121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
5131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	kfree(i2c_get_clientdata(client));
5141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return 0;
5151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
5161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
5171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic struct adm1025_data *adm1025_update_device(struct device *dev)
5181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
5191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct i2c_client *client = to_i2c_client(dev);
5201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct adm1025_data *data = i2c_get_clientdata(client);
5211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
5221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	down(&data->update_lock);
5231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
5241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (time_after(jiffies, data->last_updated + HZ * 2) || !data->valid) {
5251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		int i;
5261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
5271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		dev_dbg(&client->dev, "Updating data.\n");
5281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		for (i=0; i<6; i++) {
5291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			data->in[i] = i2c_smbus_read_byte_data(client,
5301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				      ADM1025_REG_IN(i));
5311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			data->in_min[i] = i2c_smbus_read_byte_data(client,
5321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds					  ADM1025_REG_IN_MIN(i));
5331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			data->in_max[i] = i2c_smbus_read_byte_data(client,
5341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds					  ADM1025_REG_IN_MAX(i));
5351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		}
5361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		for (i=0; i<2; i++) {
5371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			data->temp[i] = i2c_smbus_read_byte_data(client,
5381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds					ADM1025_REG_TEMP(i));
5391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			data->temp_min[i] = i2c_smbus_read_byte_data(client,
5401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds					    ADM1025_REG_TEMP_LOW(i));
5411da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			data->temp_max[i] = i2c_smbus_read_byte_data(client,
5421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds					    ADM1025_REG_TEMP_HIGH(i));
5431da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		}
5441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		data->alarms = i2c_smbus_read_byte_data(client,
5451da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			       ADM1025_REG_STATUS1)
5461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			     | (i2c_smbus_read_byte_data(client,
5471da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				ADM1025_REG_STATUS2) << 8);
5481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		data->vid = (i2c_smbus_read_byte_data(client,
5491da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			     ADM1025_REG_VID) & 0x0f)
5501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			  | ((i2c_smbus_read_byte_data(client,
5511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			      ADM1025_REG_VID4) & 0x01) << 4);
5521da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
5531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		data->last_updated = jiffies;
5541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		data->valid = 1;
5551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
5561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
5571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	up(&data->update_lock);
5581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
5591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return data;
5601da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
5611da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
5621da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic int __init sensors_adm1025_init(void)
5631da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
5641da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return i2c_add_driver(&adm1025_driver);
5651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
5661da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
5671da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic void __exit sensors_adm1025_exit(void)
5681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
5691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	i2c_del_driver(&adm1025_driver);
5701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
5711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
5721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus TorvaldsMODULE_AUTHOR("Jean Delvare <khali@linux-fr.org>");
5731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus TorvaldsMODULE_DESCRIPTION("ADM1025 driver");
5741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus TorvaldsMODULE_LICENSE("GPL");
5751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
5761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsmodule_init(sensors_adm1025_init);
5771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsmodule_exit(sensors_adm1025_exit);
578