11da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/*
21da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * adm1025.c
31da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
41da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Copyright (C) 2000       Chen-Yuan Wu <gwu@esoft.com>
57c81c60f3789a082e141d7a013392af5f78db16aJean Delvare * Copyright (C) 2003-2009  Jean Delvare <jdelvare@suse.de>
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:
152b22de5117d6dec44b62c305cb456e5b9a913c95Guenter Roeck *   http://www.onsemi.com/PowerSolutions/product.do?id=ADM1025
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>
53943b0830cebe4711354945ed3cb44e84152aaca0Mark M. Hoffman#include <linux/hwmon.h>
54d8543e7f30fe9306c8d5a16086bf75f71ab79acfJean Delvare#include <linux/hwmon-sysfs.h>
55303760b44a7a142cb9f4c9df4609fb63bbda98dbJean Delvare#include <linux/hwmon-vid.h>
56943b0830cebe4711354945ed3cb44e84152aaca0Mark M. Hoffman#include <linux/err.h>
579a61bf6300533d3b64d7ff29adfec00e596de67dIngo Molnar#include <linux/mutex.h>
581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/*
601da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Addresses to scan
611da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * ADM1025 and ADM1025A have three possible addresses: 0x2c, 0x2d and 0x2e.
621da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * NE1619 has two possible addresses: 0x2c and 0x2d.
631da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */
641da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
6525e9c86d5a6d82ea45eb680fc66bf73ac5e50dffMark M. Hoffmanstatic const unsigned short normal_i2c[] = { 0x2c, 0x2d, 0x2e, I2C_CLIENT_END };
661da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
67e5e9f44c246fbafe723e579e9fe887677beb40e4Jean Delvareenum chips { adm1025, ne1619 };
681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/*
701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * The ADM1025 registers
711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */
721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define ADM1025_REG_MAN_ID		0x3E
744e9527998f8673b1e3bb6f9645b9700e7973581eJean Delvare#define ADM1025_REG_CHIP_ID		0x3F
751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define ADM1025_REG_CONFIG		0x40
761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define ADM1025_REG_STATUS1		0x41
771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define ADM1025_REG_STATUS2		0x42
781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define ADM1025_REG_IN(nr)		(0x20 + (nr))
791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define ADM1025_REG_IN_MAX(nr)		(0x2B + (nr) * 2)
801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define ADM1025_REG_IN_MIN(nr)		(0x2C + (nr) * 2)
811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define ADM1025_REG_TEMP(nr)		(0x26 + (nr))
821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define ADM1025_REG_TEMP_HIGH(nr)	(0x37 + (nr) * 2)
831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define ADM1025_REG_TEMP_LOW(nr)	(0x38 + (nr) * 2)
841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define ADM1025_REG_VID			0x47
851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define ADM1025_REG_VID4		0x49
861da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
871da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/*
881da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Conversions and various macros
891da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * The ADM1025 uses signed 8-bit values for temperatures.
901da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */
911da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
924e9527998f8673b1e3bb6f9645b9700e7973581eJean Delvarestatic const int in_scale[6] = { 2500, 2250, 3300, 5000, 12000, 3300 };
931da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
942b22de5117d6dec44b62c305cb456e5b9a913c95Guenter Roeck#define IN_FROM_REG(reg, scale)	(((reg) * (scale) + 96) / 192)
952b22de5117d6dec44b62c305cb456e5b9a913c95Guenter Roeck#define IN_TO_REG(val, scale)	((val) <= 0 ? 0 : \
961da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				 (val) * 192 >= (scale) * 255 ? 255 : \
972b22de5117d6dec44b62c305cb456e5b9a913c95Guenter Roeck				 ((val) * 192 + (scale) / 2) / (scale))
981da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
991da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define TEMP_FROM_REG(reg)	((reg) * 1000)
1001da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define TEMP_TO_REG(val)	((val) <= -127500 ? -128 : \
1011da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				 (val) >= 126500 ? 127 : \
1022b22de5117d6dec44b62c305cb456e5b9a913c95Guenter Roeck				 (((val) < 0 ? (val) - 500 : \
1032b22de5117d6dec44b62c305cb456e5b9a913c95Guenter Roeck				   (val) + 500) / 1000))
1041da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1051da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/*
1061da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Client data (each client gets its own)
1071da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */
1081da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1091da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstruct adm1025_data {
110aab3f57a11723c75477f43d7a510c148f2269d16Axel Lin	struct i2c_client *client;
111aab3f57a11723c75477f43d7a510c148f2269d16Axel Lin	const struct attribute_group *groups[3];
1129a61bf6300533d3b64d7ff29adfec00e596de67dIngo Molnar	struct mutex update_lock;
1131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	char valid; /* zero until following fields are valid */
1141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	unsigned long last_updated; /* in jiffies */
1151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	u8 in[6];		/* register value */
1171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	u8 in_max[6];		/* register value */
1181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	u8 in_min[6];		/* register value */
1191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	s8 temp[2];		/* register value */
1201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	s8 temp_min[2];		/* register value */
1211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	s8 temp_max[2];		/* register value */
1221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	u16 alarms;		/* register values, combined */
1231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	u8 vid;			/* register values, combined */
1241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	u8 vrm;
1251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds};
1261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
127851a7afa82c9cbbfa2f85151872a29db0a3f6cbfAxel Linstatic struct adm1025_data *adm1025_update_device(struct device *dev)
128851a7afa82c9cbbfa2f85151872a29db0a3f6cbfAxel Lin{
129aab3f57a11723c75477f43d7a510c148f2269d16Axel Lin	struct adm1025_data *data = dev_get_drvdata(dev);
130aab3f57a11723c75477f43d7a510c148f2269d16Axel Lin	struct i2c_client *client = data->client;
131851a7afa82c9cbbfa2f85151872a29db0a3f6cbfAxel Lin
132851a7afa82c9cbbfa2f85151872a29db0a3f6cbfAxel Lin	mutex_lock(&data->update_lock);
133851a7afa82c9cbbfa2f85151872a29db0a3f6cbfAxel Lin
134851a7afa82c9cbbfa2f85151872a29db0a3f6cbfAxel Lin	if (time_after(jiffies, data->last_updated + HZ * 2) || !data->valid) {
135851a7afa82c9cbbfa2f85151872a29db0a3f6cbfAxel Lin		int i;
136851a7afa82c9cbbfa2f85151872a29db0a3f6cbfAxel Lin
137851a7afa82c9cbbfa2f85151872a29db0a3f6cbfAxel Lin		dev_dbg(&client->dev, "Updating data.\n");
138851a7afa82c9cbbfa2f85151872a29db0a3f6cbfAxel Lin		for (i = 0; i < 6; i++) {
139851a7afa82c9cbbfa2f85151872a29db0a3f6cbfAxel Lin			data->in[i] = i2c_smbus_read_byte_data(client,
140851a7afa82c9cbbfa2f85151872a29db0a3f6cbfAxel Lin				      ADM1025_REG_IN(i));
141851a7afa82c9cbbfa2f85151872a29db0a3f6cbfAxel Lin			data->in_min[i] = i2c_smbus_read_byte_data(client,
142851a7afa82c9cbbfa2f85151872a29db0a3f6cbfAxel Lin					  ADM1025_REG_IN_MIN(i));
143851a7afa82c9cbbfa2f85151872a29db0a3f6cbfAxel Lin			data->in_max[i] = i2c_smbus_read_byte_data(client,
144851a7afa82c9cbbfa2f85151872a29db0a3f6cbfAxel Lin					  ADM1025_REG_IN_MAX(i));
145851a7afa82c9cbbfa2f85151872a29db0a3f6cbfAxel Lin		}
146851a7afa82c9cbbfa2f85151872a29db0a3f6cbfAxel Lin		for (i = 0; i < 2; i++) {
147851a7afa82c9cbbfa2f85151872a29db0a3f6cbfAxel Lin			data->temp[i] = i2c_smbus_read_byte_data(client,
148851a7afa82c9cbbfa2f85151872a29db0a3f6cbfAxel Lin					ADM1025_REG_TEMP(i));
149851a7afa82c9cbbfa2f85151872a29db0a3f6cbfAxel Lin			data->temp_min[i] = i2c_smbus_read_byte_data(client,
150851a7afa82c9cbbfa2f85151872a29db0a3f6cbfAxel Lin					    ADM1025_REG_TEMP_LOW(i));
151851a7afa82c9cbbfa2f85151872a29db0a3f6cbfAxel Lin			data->temp_max[i] = i2c_smbus_read_byte_data(client,
152851a7afa82c9cbbfa2f85151872a29db0a3f6cbfAxel Lin					    ADM1025_REG_TEMP_HIGH(i));
153851a7afa82c9cbbfa2f85151872a29db0a3f6cbfAxel Lin		}
154851a7afa82c9cbbfa2f85151872a29db0a3f6cbfAxel Lin		data->alarms = i2c_smbus_read_byte_data(client,
155851a7afa82c9cbbfa2f85151872a29db0a3f6cbfAxel Lin			       ADM1025_REG_STATUS1)
156851a7afa82c9cbbfa2f85151872a29db0a3f6cbfAxel Lin			     | (i2c_smbus_read_byte_data(client,
157851a7afa82c9cbbfa2f85151872a29db0a3f6cbfAxel Lin				ADM1025_REG_STATUS2) << 8);
158851a7afa82c9cbbfa2f85151872a29db0a3f6cbfAxel Lin		data->vid = (i2c_smbus_read_byte_data(client,
159851a7afa82c9cbbfa2f85151872a29db0a3f6cbfAxel Lin			     ADM1025_REG_VID) & 0x0f)
160851a7afa82c9cbbfa2f85151872a29db0a3f6cbfAxel Lin			  | ((i2c_smbus_read_byte_data(client,
161851a7afa82c9cbbfa2f85151872a29db0a3f6cbfAxel Lin			      ADM1025_REG_VID4) & 0x01) << 4);
162851a7afa82c9cbbfa2f85151872a29db0a3f6cbfAxel Lin
163851a7afa82c9cbbfa2f85151872a29db0a3f6cbfAxel Lin		data->last_updated = jiffies;
164851a7afa82c9cbbfa2f85151872a29db0a3f6cbfAxel Lin		data->valid = 1;
165851a7afa82c9cbbfa2f85151872a29db0a3f6cbfAxel Lin	}
166851a7afa82c9cbbfa2f85151872a29db0a3f6cbfAxel Lin
167851a7afa82c9cbbfa2f85151872a29db0a3f6cbfAxel Lin	mutex_unlock(&data->update_lock);
168851a7afa82c9cbbfa2f85151872a29db0a3f6cbfAxel Lin
169851a7afa82c9cbbfa2f85151872a29db0a3f6cbfAxel Lin	return data;
170851a7afa82c9cbbfa2f85151872a29db0a3f6cbfAxel Lin}
171851a7afa82c9cbbfa2f85151872a29db0a3f6cbfAxel Lin
1721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/*
1731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Sysfs stuff
1741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */
1751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
176d8543e7f30fe9306c8d5a16086bf75f71ab79acfJean Delvarestatic ssize_t
177d8543e7f30fe9306c8d5a16086bf75f71ab79acfJean Delvareshow_in(struct device *dev, struct device_attribute *attr, char *buf)
178d8543e7f30fe9306c8d5a16086bf75f71ab79acfJean Delvare{
179d8543e7f30fe9306c8d5a16086bf75f71ab79acfJean Delvare	int index = to_sensor_dev_attr(attr)->index;
180d8543e7f30fe9306c8d5a16086bf75f71ab79acfJean Delvare	struct adm1025_data *data = adm1025_update_device(dev);
181d8543e7f30fe9306c8d5a16086bf75f71ab79acfJean Delvare	return sprintf(buf, "%u\n", IN_FROM_REG(data->in[index],
182d8543e7f30fe9306c8d5a16086bf75f71ab79acfJean Delvare		       in_scale[index]));
183d8543e7f30fe9306c8d5a16086bf75f71ab79acfJean Delvare}
184d8543e7f30fe9306c8d5a16086bf75f71ab79acfJean Delvare
185d8543e7f30fe9306c8d5a16086bf75f71ab79acfJean Delvarestatic ssize_t
186d8543e7f30fe9306c8d5a16086bf75f71ab79acfJean Delvareshow_in_min(struct device *dev, struct device_attribute *attr, char *buf)
187d8543e7f30fe9306c8d5a16086bf75f71ab79acfJean Delvare{
188d8543e7f30fe9306c8d5a16086bf75f71ab79acfJean Delvare	int index = to_sensor_dev_attr(attr)->index;
189d8543e7f30fe9306c8d5a16086bf75f71ab79acfJean Delvare	struct adm1025_data *data = adm1025_update_device(dev);
190d8543e7f30fe9306c8d5a16086bf75f71ab79acfJean Delvare	return sprintf(buf, "%u\n", IN_FROM_REG(data->in_min[index],
191d8543e7f30fe9306c8d5a16086bf75f71ab79acfJean Delvare		       in_scale[index]));
192d8543e7f30fe9306c8d5a16086bf75f71ab79acfJean Delvare}
193d8543e7f30fe9306c8d5a16086bf75f71ab79acfJean Delvare
194d8543e7f30fe9306c8d5a16086bf75f71ab79acfJean Delvarestatic ssize_t
195d8543e7f30fe9306c8d5a16086bf75f71ab79acfJean Delvareshow_in_max(struct device *dev, struct device_attribute *attr, char *buf)
196d8543e7f30fe9306c8d5a16086bf75f71ab79acfJean Delvare{
197d8543e7f30fe9306c8d5a16086bf75f71ab79acfJean Delvare	int index = to_sensor_dev_attr(attr)->index;
198d8543e7f30fe9306c8d5a16086bf75f71ab79acfJean Delvare	struct adm1025_data *data = adm1025_update_device(dev);
199d8543e7f30fe9306c8d5a16086bf75f71ab79acfJean Delvare	return sprintf(buf, "%u\n", IN_FROM_REG(data->in_max[index],
200d8543e7f30fe9306c8d5a16086bf75f71ab79acfJean Delvare		       in_scale[index]));
201d8543e7f30fe9306c8d5a16086bf75f71ab79acfJean Delvare}
202d8543e7f30fe9306c8d5a16086bf75f71ab79acfJean Delvare
203d8543e7f30fe9306c8d5a16086bf75f71ab79acfJean Delvarestatic ssize_t
204d8543e7f30fe9306c8d5a16086bf75f71ab79acfJean Delvareshow_temp(struct device *dev, struct device_attribute *attr, char *buf)
205d8543e7f30fe9306c8d5a16086bf75f71ab79acfJean Delvare{
206d8543e7f30fe9306c8d5a16086bf75f71ab79acfJean Delvare	int index = to_sensor_dev_attr(attr)->index;
207d8543e7f30fe9306c8d5a16086bf75f71ab79acfJean Delvare	struct adm1025_data *data = adm1025_update_device(dev);
208d8543e7f30fe9306c8d5a16086bf75f71ab79acfJean Delvare	return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[index]));
209d8543e7f30fe9306c8d5a16086bf75f71ab79acfJean Delvare}
210d8543e7f30fe9306c8d5a16086bf75f71ab79acfJean Delvare
211d8543e7f30fe9306c8d5a16086bf75f71ab79acfJean Delvarestatic ssize_t
212d8543e7f30fe9306c8d5a16086bf75f71ab79acfJean Delvareshow_temp_min(struct device *dev, struct device_attribute *attr, char *buf)
213d8543e7f30fe9306c8d5a16086bf75f71ab79acfJean Delvare{
214d8543e7f30fe9306c8d5a16086bf75f71ab79acfJean Delvare	int index = to_sensor_dev_attr(attr)->index;
215d8543e7f30fe9306c8d5a16086bf75f71ab79acfJean Delvare	struct adm1025_data *data = adm1025_update_device(dev);
216d8543e7f30fe9306c8d5a16086bf75f71ab79acfJean Delvare	return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_min[index]));
217d8543e7f30fe9306c8d5a16086bf75f71ab79acfJean Delvare}
218d8543e7f30fe9306c8d5a16086bf75f71ab79acfJean Delvare
219d8543e7f30fe9306c8d5a16086bf75f71ab79acfJean Delvarestatic ssize_t
220d8543e7f30fe9306c8d5a16086bf75f71ab79acfJean Delvareshow_temp_max(struct device *dev, struct device_attribute *attr, char *buf)
221d8543e7f30fe9306c8d5a16086bf75f71ab79acfJean Delvare{
222d8543e7f30fe9306c8d5a16086bf75f71ab79acfJean Delvare	int index = to_sensor_dev_attr(attr)->index;
223d8543e7f30fe9306c8d5a16086bf75f71ab79acfJean Delvare	struct adm1025_data *data = adm1025_update_device(dev);
224d8543e7f30fe9306c8d5a16086bf75f71ab79acfJean Delvare	return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_max[index]));
225d8543e7f30fe9306c8d5a16086bf75f71ab79acfJean Delvare}
226d8543e7f30fe9306c8d5a16086bf75f71ab79acfJean Delvare
227d8543e7f30fe9306c8d5a16086bf75f71ab79acfJean Delvarestatic ssize_t set_in_min(struct device *dev, struct device_attribute *attr,
228d8543e7f30fe9306c8d5a16086bf75f71ab79acfJean Delvare			  const char *buf, size_t count)
229d8543e7f30fe9306c8d5a16086bf75f71ab79acfJean Delvare{
230d8543e7f30fe9306c8d5a16086bf75f71ab79acfJean Delvare	int index = to_sensor_dev_attr(attr)->index;
231aab3f57a11723c75477f43d7a510c148f2269d16Axel Lin	struct adm1025_data *data = dev_get_drvdata(dev);
232aab3f57a11723c75477f43d7a510c148f2269d16Axel Lin	struct i2c_client *client = data->client;
2332b22de5117d6dec44b62c305cb456e5b9a913c95Guenter Roeck	long val;
2342b22de5117d6dec44b62c305cb456e5b9a913c95Guenter Roeck	int err;
2352b22de5117d6dec44b62c305cb456e5b9a913c95Guenter Roeck
2362b22de5117d6dec44b62c305cb456e5b9a913c95Guenter Roeck	err = kstrtol(buf, 10, &val);
2372b22de5117d6dec44b62c305cb456e5b9a913c95Guenter Roeck	if (err)
2382b22de5117d6dec44b62c305cb456e5b9a913c95Guenter Roeck		return err;
239d8543e7f30fe9306c8d5a16086bf75f71ab79acfJean Delvare
240d8543e7f30fe9306c8d5a16086bf75f71ab79acfJean Delvare	mutex_lock(&data->update_lock);
241d8543e7f30fe9306c8d5a16086bf75f71ab79acfJean Delvare	data->in_min[index] = IN_TO_REG(val, in_scale[index]);
242d8543e7f30fe9306c8d5a16086bf75f71ab79acfJean Delvare	i2c_smbus_write_byte_data(client, ADM1025_REG_IN_MIN(index),
243d8543e7f30fe9306c8d5a16086bf75f71ab79acfJean Delvare				  data->in_min[index]);
244d8543e7f30fe9306c8d5a16086bf75f71ab79acfJean Delvare	mutex_unlock(&data->update_lock);
245d8543e7f30fe9306c8d5a16086bf75f71ab79acfJean Delvare	return count;
246d8543e7f30fe9306c8d5a16086bf75f71ab79acfJean Delvare}
247d8543e7f30fe9306c8d5a16086bf75f71ab79acfJean Delvare
248d8543e7f30fe9306c8d5a16086bf75f71ab79acfJean Delvarestatic ssize_t set_in_max(struct device *dev, struct device_attribute *attr,
249d8543e7f30fe9306c8d5a16086bf75f71ab79acfJean Delvare			  const char *buf, size_t count)
250d8543e7f30fe9306c8d5a16086bf75f71ab79acfJean Delvare{
251d8543e7f30fe9306c8d5a16086bf75f71ab79acfJean Delvare	int index = to_sensor_dev_attr(attr)->index;
252aab3f57a11723c75477f43d7a510c148f2269d16Axel Lin	struct adm1025_data *data = dev_get_drvdata(dev);
253aab3f57a11723c75477f43d7a510c148f2269d16Axel Lin	struct i2c_client *client = data->client;
2542b22de5117d6dec44b62c305cb456e5b9a913c95Guenter Roeck	long val;
2552b22de5117d6dec44b62c305cb456e5b9a913c95Guenter Roeck	int err;
2562b22de5117d6dec44b62c305cb456e5b9a913c95Guenter Roeck
2572b22de5117d6dec44b62c305cb456e5b9a913c95Guenter Roeck	err = kstrtol(buf, 10, &val);
2582b22de5117d6dec44b62c305cb456e5b9a913c95Guenter Roeck	if (err)
2592b22de5117d6dec44b62c305cb456e5b9a913c95Guenter Roeck		return err;
260d8543e7f30fe9306c8d5a16086bf75f71ab79acfJean Delvare
261d8543e7f30fe9306c8d5a16086bf75f71ab79acfJean Delvare	mutex_lock(&data->update_lock);
262d8543e7f30fe9306c8d5a16086bf75f71ab79acfJean Delvare	data->in_max[index] = IN_TO_REG(val, in_scale[index]);
263d8543e7f30fe9306c8d5a16086bf75f71ab79acfJean Delvare	i2c_smbus_write_byte_data(client, ADM1025_REG_IN_MAX(index),
264d8543e7f30fe9306c8d5a16086bf75f71ab79acfJean Delvare				  data->in_max[index]);
265d8543e7f30fe9306c8d5a16086bf75f71ab79acfJean Delvare	mutex_unlock(&data->update_lock);
266d8543e7f30fe9306c8d5a16086bf75f71ab79acfJean Delvare	return count;
267d8543e7f30fe9306c8d5a16086bf75f71ab79acfJean Delvare}
2681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define set_in(offset) \
270d8543e7f30fe9306c8d5a16086bf75f71ab79acfJean Delvarestatic SENSOR_DEVICE_ATTR(in##offset##_input, S_IRUGO, \
271d8543e7f30fe9306c8d5a16086bf75f71ab79acfJean Delvare	show_in, NULL, offset); \
272d8543e7f30fe9306c8d5a16086bf75f71ab79acfJean Delvarestatic SENSOR_DEVICE_ATTR(in##offset##_min, S_IWUSR | S_IRUGO, \
273d8543e7f30fe9306c8d5a16086bf75f71ab79acfJean Delvare	show_in_min, set_in_min, offset); \
274d8543e7f30fe9306c8d5a16086bf75f71ab79acfJean Delvarestatic SENSOR_DEVICE_ATTR(in##offset##_max, S_IWUSR | S_IRUGO, \
275d8543e7f30fe9306c8d5a16086bf75f71ab79acfJean Delvare	show_in_max, set_in_max, offset)
2761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsset_in(0);
2771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsset_in(1);
2781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsset_in(2);
2791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsset_in(3);
2801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsset_in(4);
2811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsset_in(5);
2821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
283d8543e7f30fe9306c8d5a16086bf75f71ab79acfJean Delvarestatic ssize_t set_temp_min(struct device *dev, struct device_attribute *attr,
284d8543e7f30fe9306c8d5a16086bf75f71ab79acfJean Delvare			    const char *buf, size_t count)
285d8543e7f30fe9306c8d5a16086bf75f71ab79acfJean Delvare{
286d8543e7f30fe9306c8d5a16086bf75f71ab79acfJean Delvare	int index = to_sensor_dev_attr(attr)->index;
287aab3f57a11723c75477f43d7a510c148f2269d16Axel Lin	struct adm1025_data *data = dev_get_drvdata(dev);
288aab3f57a11723c75477f43d7a510c148f2269d16Axel Lin	struct i2c_client *client = data->client;
2892b22de5117d6dec44b62c305cb456e5b9a913c95Guenter Roeck	long val;
2902b22de5117d6dec44b62c305cb456e5b9a913c95Guenter Roeck	int err;
2912b22de5117d6dec44b62c305cb456e5b9a913c95Guenter Roeck
2922b22de5117d6dec44b62c305cb456e5b9a913c95Guenter Roeck	err = kstrtol(buf, 10, &val);
2932b22de5117d6dec44b62c305cb456e5b9a913c95Guenter Roeck	if (err)
2942b22de5117d6dec44b62c305cb456e5b9a913c95Guenter Roeck		return err;
295d8543e7f30fe9306c8d5a16086bf75f71ab79acfJean Delvare
296d8543e7f30fe9306c8d5a16086bf75f71ab79acfJean Delvare	mutex_lock(&data->update_lock);
297d8543e7f30fe9306c8d5a16086bf75f71ab79acfJean Delvare	data->temp_min[index] = TEMP_TO_REG(val);
298d8543e7f30fe9306c8d5a16086bf75f71ab79acfJean Delvare	i2c_smbus_write_byte_data(client, ADM1025_REG_TEMP_LOW(index),
299d8543e7f30fe9306c8d5a16086bf75f71ab79acfJean Delvare				  data->temp_min[index]);
300d8543e7f30fe9306c8d5a16086bf75f71ab79acfJean Delvare	mutex_unlock(&data->update_lock);
301d8543e7f30fe9306c8d5a16086bf75f71ab79acfJean Delvare	return count;
302d8543e7f30fe9306c8d5a16086bf75f71ab79acfJean Delvare}
303d8543e7f30fe9306c8d5a16086bf75f71ab79acfJean Delvare
304d8543e7f30fe9306c8d5a16086bf75f71ab79acfJean Delvarestatic ssize_t set_temp_max(struct device *dev, struct device_attribute *attr,
305d8543e7f30fe9306c8d5a16086bf75f71ab79acfJean Delvare	const char *buf, size_t count)
306d8543e7f30fe9306c8d5a16086bf75f71ab79acfJean Delvare{
307d8543e7f30fe9306c8d5a16086bf75f71ab79acfJean Delvare	int index = to_sensor_dev_attr(attr)->index;
308aab3f57a11723c75477f43d7a510c148f2269d16Axel Lin	struct adm1025_data *data = dev_get_drvdata(dev);
309aab3f57a11723c75477f43d7a510c148f2269d16Axel Lin	struct i2c_client *client = data->client;
3102b22de5117d6dec44b62c305cb456e5b9a913c95Guenter Roeck	long val;
3112b22de5117d6dec44b62c305cb456e5b9a913c95Guenter Roeck	int err;
3122b22de5117d6dec44b62c305cb456e5b9a913c95Guenter Roeck
3132b22de5117d6dec44b62c305cb456e5b9a913c95Guenter Roeck	err = kstrtol(buf, 10, &val);
3142b22de5117d6dec44b62c305cb456e5b9a913c95Guenter Roeck	if (err)
3152b22de5117d6dec44b62c305cb456e5b9a913c95Guenter Roeck		return err;
316d8543e7f30fe9306c8d5a16086bf75f71ab79acfJean Delvare
317d8543e7f30fe9306c8d5a16086bf75f71ab79acfJean Delvare	mutex_lock(&data->update_lock);
318d8543e7f30fe9306c8d5a16086bf75f71ab79acfJean Delvare	data->temp_max[index] = TEMP_TO_REG(val);
319d8543e7f30fe9306c8d5a16086bf75f71ab79acfJean Delvare	i2c_smbus_write_byte_data(client, ADM1025_REG_TEMP_HIGH(index),
320d8543e7f30fe9306c8d5a16086bf75f71ab79acfJean Delvare				  data->temp_max[index]);
321d8543e7f30fe9306c8d5a16086bf75f71ab79acfJean Delvare	mutex_unlock(&data->update_lock);
322d8543e7f30fe9306c8d5a16086bf75f71ab79acfJean Delvare	return count;
323d8543e7f30fe9306c8d5a16086bf75f71ab79acfJean Delvare}
324d8543e7f30fe9306c8d5a16086bf75f71ab79acfJean Delvare
3251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define set_temp(offset) \
326d8543e7f30fe9306c8d5a16086bf75f71ab79acfJean Delvarestatic SENSOR_DEVICE_ATTR(temp##offset##_input, S_IRUGO, \
327d8543e7f30fe9306c8d5a16086bf75f71ab79acfJean Delvare	show_temp, NULL, offset - 1); \
328d8543e7f30fe9306c8d5a16086bf75f71ab79acfJean Delvarestatic SENSOR_DEVICE_ATTR(temp##offset##_min, S_IWUSR | S_IRUGO, \
329d8543e7f30fe9306c8d5a16086bf75f71ab79acfJean Delvare	show_temp_min, set_temp_min, offset - 1); \
330d8543e7f30fe9306c8d5a16086bf75f71ab79acfJean Delvarestatic SENSOR_DEVICE_ATTR(temp##offset##_max, S_IWUSR | S_IRUGO, \
331d8543e7f30fe9306c8d5a16086bf75f71ab79acfJean Delvare	show_temp_max, set_temp_max, offset - 1)
3321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsset_temp(1);
3331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsset_temp(2);
3341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3354e9527998f8673b1e3bb6f9645b9700e7973581eJean Delvarestatic ssize_t
3364e9527998f8673b1e3bb6f9645b9700e7973581eJean Delvareshow_alarms(struct device *dev, struct device_attribute *attr, char *buf)
3371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
3381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct adm1025_data *data = adm1025_update_device(dev);
3391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return sprintf(buf, "%u\n", data->alarms);
3401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
3411da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
3421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
343bb081300326335594c273107fcd4e5bdad765ae7Jean Delvarestatic ssize_t
344bb081300326335594c273107fcd4e5bdad765ae7Jean Delvareshow_alarm(struct device *dev, struct device_attribute *attr, char *buf)
345bb081300326335594c273107fcd4e5bdad765ae7Jean Delvare{
346bb081300326335594c273107fcd4e5bdad765ae7Jean Delvare	int bitnr = to_sensor_dev_attr(attr)->index;
347bb081300326335594c273107fcd4e5bdad765ae7Jean Delvare	struct adm1025_data *data = adm1025_update_device(dev);
348bb081300326335594c273107fcd4e5bdad765ae7Jean Delvare	return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1);
349bb081300326335594c273107fcd4e5bdad765ae7Jean Delvare}
350bb081300326335594c273107fcd4e5bdad765ae7Jean Delvarestatic SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 0);
351bb081300326335594c273107fcd4e5bdad765ae7Jean Delvarestatic SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 1);
352bb081300326335594c273107fcd4e5bdad765ae7Jean Delvarestatic SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 2);
353bb081300326335594c273107fcd4e5bdad765ae7Jean Delvarestatic SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 3);
354bb081300326335594c273107fcd4e5bdad765ae7Jean Delvarestatic SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 8);
355bb081300326335594c273107fcd4e5bdad765ae7Jean Delvarestatic SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 9);
356bb081300326335594c273107fcd4e5bdad765ae7Jean Delvarestatic SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 5);
357bb081300326335594c273107fcd4e5bdad765ae7Jean Delvarestatic SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 4);
358bb081300326335594c273107fcd4e5bdad765ae7Jean Delvarestatic SENSOR_DEVICE_ATTR(temp1_fault, S_IRUGO, show_alarm, NULL, 14);
359bb081300326335594c273107fcd4e5bdad765ae7Jean Delvare
3604e9527998f8673b1e3bb6f9645b9700e7973581eJean Delvarestatic ssize_t
3614e9527998f8673b1e3bb6f9645b9700e7973581eJean Delvareshow_vid(struct device *dev, struct device_attribute *attr, char *buf)
3621da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
3631da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct adm1025_data *data = adm1025_update_device(dev);
3641da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return sprintf(buf, "%u\n", vid_from_reg(data->vid, data->vrm));
3651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
366937df8df907ce63b0f7e19adf6e3cdef1687fac3Grant Coadystatic DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid, NULL);
3671da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3684e9527998f8673b1e3bb6f9645b9700e7973581eJean Delvarestatic ssize_t
3694e9527998f8673b1e3bb6f9645b9700e7973581eJean Delvareshow_vrm(struct device *dev, struct device_attribute *attr, char *buf)
3701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
37190d6619a916062cb75a176aacb318d108758b4a5Jean Delvare	struct adm1025_data *data = dev_get_drvdata(dev);
3721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return sprintf(buf, "%u\n", data->vrm);
3731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
3744e9527998f8673b1e3bb6f9645b9700e7973581eJean Delvarestatic ssize_t set_vrm(struct device *dev, struct device_attribute *attr,
3754e9527998f8673b1e3bb6f9645b9700e7973581eJean Delvare		       const char *buf, size_t count)
3761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
3778f74efe81d122c071410fd74f42879ef81439fa4Jean Delvare	struct adm1025_data *data = dev_get_drvdata(dev);
3782b22de5117d6dec44b62c305cb456e5b9a913c95Guenter Roeck	unsigned long val;
3792b22de5117d6dec44b62c305cb456e5b9a913c95Guenter Roeck	int err;
3802b22de5117d6dec44b62c305cb456e5b9a913c95Guenter Roeck
3812b22de5117d6dec44b62c305cb456e5b9a913c95Guenter Roeck	err = kstrtoul(buf, 10, &val);
3822b22de5117d6dec44b62c305cb456e5b9a913c95Guenter Roeck	if (err)
3832b22de5117d6dec44b62c305cb456e5b9a913c95Guenter Roeck		return err;
3842b22de5117d6dec44b62c305cb456e5b9a913c95Guenter Roeck
3859c8ae7287a51d0fdc2e3547068afccfc4880e902Axel Lin	if (val > 255)
3869c8ae7287a51d0fdc2e3547068afccfc4880e902Axel Lin		return -EINVAL;
3879c8ae7287a51d0fdc2e3547068afccfc4880e902Axel Lin
3882b22de5117d6dec44b62c305cb456e5b9a913c95Guenter Roeck	data->vrm = val;
3891da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return count;
3901da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
3911da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm, set_vrm);
3921da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
3931da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/*
3941da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Real code
3951da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */
3961da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
397681c6f7a6702f208d48b501c8829dbc03a2ca238Mark M. Hoffmanstatic struct attribute *adm1025_attributes[] = {
398d8543e7f30fe9306c8d5a16086bf75f71ab79acfJean Delvare	&sensor_dev_attr_in0_input.dev_attr.attr,
399d8543e7f30fe9306c8d5a16086bf75f71ab79acfJean Delvare	&sensor_dev_attr_in1_input.dev_attr.attr,
400d8543e7f30fe9306c8d5a16086bf75f71ab79acfJean Delvare	&sensor_dev_attr_in2_input.dev_attr.attr,
401d8543e7f30fe9306c8d5a16086bf75f71ab79acfJean Delvare	&sensor_dev_attr_in3_input.dev_attr.attr,
402d8543e7f30fe9306c8d5a16086bf75f71ab79acfJean Delvare	&sensor_dev_attr_in5_input.dev_attr.attr,
403d8543e7f30fe9306c8d5a16086bf75f71ab79acfJean Delvare	&sensor_dev_attr_in0_min.dev_attr.attr,
404d8543e7f30fe9306c8d5a16086bf75f71ab79acfJean Delvare	&sensor_dev_attr_in1_min.dev_attr.attr,
405d8543e7f30fe9306c8d5a16086bf75f71ab79acfJean Delvare	&sensor_dev_attr_in2_min.dev_attr.attr,
406d8543e7f30fe9306c8d5a16086bf75f71ab79acfJean Delvare	&sensor_dev_attr_in3_min.dev_attr.attr,
407d8543e7f30fe9306c8d5a16086bf75f71ab79acfJean Delvare	&sensor_dev_attr_in5_min.dev_attr.attr,
408d8543e7f30fe9306c8d5a16086bf75f71ab79acfJean Delvare	&sensor_dev_attr_in0_max.dev_attr.attr,
409d8543e7f30fe9306c8d5a16086bf75f71ab79acfJean Delvare	&sensor_dev_attr_in1_max.dev_attr.attr,
410d8543e7f30fe9306c8d5a16086bf75f71ab79acfJean Delvare	&sensor_dev_attr_in2_max.dev_attr.attr,
411d8543e7f30fe9306c8d5a16086bf75f71ab79acfJean Delvare	&sensor_dev_attr_in3_max.dev_attr.attr,
412d8543e7f30fe9306c8d5a16086bf75f71ab79acfJean Delvare	&sensor_dev_attr_in5_max.dev_attr.attr,
413bb081300326335594c273107fcd4e5bdad765ae7Jean Delvare	&sensor_dev_attr_in0_alarm.dev_attr.attr,
414bb081300326335594c273107fcd4e5bdad765ae7Jean Delvare	&sensor_dev_attr_in1_alarm.dev_attr.attr,
415bb081300326335594c273107fcd4e5bdad765ae7Jean Delvare	&sensor_dev_attr_in2_alarm.dev_attr.attr,
416bb081300326335594c273107fcd4e5bdad765ae7Jean Delvare	&sensor_dev_attr_in3_alarm.dev_attr.attr,
417bb081300326335594c273107fcd4e5bdad765ae7Jean Delvare	&sensor_dev_attr_in5_alarm.dev_attr.attr,
418d8543e7f30fe9306c8d5a16086bf75f71ab79acfJean Delvare	&sensor_dev_attr_temp1_input.dev_attr.attr,
419d8543e7f30fe9306c8d5a16086bf75f71ab79acfJean Delvare	&sensor_dev_attr_temp2_input.dev_attr.attr,
420d8543e7f30fe9306c8d5a16086bf75f71ab79acfJean Delvare	&sensor_dev_attr_temp1_min.dev_attr.attr,
421d8543e7f30fe9306c8d5a16086bf75f71ab79acfJean Delvare	&sensor_dev_attr_temp2_min.dev_attr.attr,
422d8543e7f30fe9306c8d5a16086bf75f71ab79acfJean Delvare	&sensor_dev_attr_temp1_max.dev_attr.attr,
423d8543e7f30fe9306c8d5a16086bf75f71ab79acfJean Delvare	&sensor_dev_attr_temp2_max.dev_attr.attr,
424bb081300326335594c273107fcd4e5bdad765ae7Jean Delvare	&sensor_dev_attr_temp1_alarm.dev_attr.attr,
425bb081300326335594c273107fcd4e5bdad765ae7Jean Delvare	&sensor_dev_attr_temp2_alarm.dev_attr.attr,
426bb081300326335594c273107fcd4e5bdad765ae7Jean Delvare	&sensor_dev_attr_temp1_fault.dev_attr.attr,
427681c6f7a6702f208d48b501c8829dbc03a2ca238Mark M. Hoffman	&dev_attr_alarms.attr,
428681c6f7a6702f208d48b501c8829dbc03a2ca238Mark M. Hoffman	&dev_attr_cpu0_vid.attr,
429681c6f7a6702f208d48b501c8829dbc03a2ca238Mark M. Hoffman	&dev_attr_vrm.attr,
430681c6f7a6702f208d48b501c8829dbc03a2ca238Mark M. Hoffman	NULL
431681c6f7a6702f208d48b501c8829dbc03a2ca238Mark M. Hoffman};
432681c6f7a6702f208d48b501c8829dbc03a2ca238Mark M. Hoffman
433681c6f7a6702f208d48b501c8829dbc03a2ca238Mark M. Hoffmanstatic const struct attribute_group adm1025_group = {
434681c6f7a6702f208d48b501c8829dbc03a2ca238Mark M. Hoffman	.attrs = adm1025_attributes,
435681c6f7a6702f208d48b501c8829dbc03a2ca238Mark M. Hoffman};
436681c6f7a6702f208d48b501c8829dbc03a2ca238Mark M. Hoffman
4374e9527998f8673b1e3bb6f9645b9700e7973581eJean Delvarestatic struct attribute *adm1025_attributes_in4[] = {
438d8543e7f30fe9306c8d5a16086bf75f71ab79acfJean Delvare	&sensor_dev_attr_in4_input.dev_attr.attr,
439d8543e7f30fe9306c8d5a16086bf75f71ab79acfJean Delvare	&sensor_dev_attr_in4_min.dev_attr.attr,
440d8543e7f30fe9306c8d5a16086bf75f71ab79acfJean Delvare	&sensor_dev_attr_in4_max.dev_attr.attr,
441bb081300326335594c273107fcd4e5bdad765ae7Jean Delvare	&sensor_dev_attr_in4_alarm.dev_attr.attr,
442681c6f7a6702f208d48b501c8829dbc03a2ca238Mark M. Hoffman	NULL
443681c6f7a6702f208d48b501c8829dbc03a2ca238Mark M. Hoffman};
444681c6f7a6702f208d48b501c8829dbc03a2ca238Mark M. Hoffman
4454e9527998f8673b1e3bb6f9645b9700e7973581eJean Delvarestatic const struct attribute_group adm1025_group_in4 = {
4464e9527998f8673b1e3bb6f9645b9700e7973581eJean Delvare	.attrs = adm1025_attributes_in4,
447681c6f7a6702f208d48b501c8829dbc03a2ca238Mark M. Hoffman};
448681c6f7a6702f208d48b501c8829dbc03a2ca238Mark M. Hoffman
4497dbafe021ba360bf25674a7e290d3e4a5c953981Jean Delvare/* Return 0 if detection is successful, -ENODEV otherwise */
450310ec79210d754afe51e2e4a983e846b60179abdJean Delvarestatic int adm1025_detect(struct i2c_client *client,
4517dbafe021ba360bf25674a7e290d3e4a5c953981Jean Delvare			  struct i2c_board_info *info)
4521da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
4537dbafe021ba360bf25674a7e290d3e4a5c953981Jean Delvare	struct i2c_adapter *adapter = client->adapter;
4549560672369098b021688421f45476ce0c8bc451bJean Delvare	const char *name;
4559560672369098b021688421f45476ce0c8bc451bJean Delvare	u8 man_id, chip_id;
4561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
4587dbafe021ba360bf25674a7e290d3e4a5c953981Jean Delvare		return -ENODEV;
4591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4609560672369098b021688421f45476ce0c8bc451bJean Delvare	/* Check for unused bits */
4619560672369098b021688421f45476ce0c8bc451bJean Delvare	if ((i2c_smbus_read_byte_data(client, ADM1025_REG_CONFIG) & 0x80)
4629560672369098b021688421f45476ce0c8bc451bJean Delvare	 || (i2c_smbus_read_byte_data(client, ADM1025_REG_STATUS1) & 0xC0)
4639560672369098b021688421f45476ce0c8bc451bJean Delvare	 || (i2c_smbus_read_byte_data(client, ADM1025_REG_STATUS2) & 0xBC)) {
4649560672369098b021688421f45476ce0c8bc451bJean Delvare		dev_dbg(&adapter->dev, "ADM1025 detection failed at 0x%02x\n",
4659560672369098b021688421f45476ce0c8bc451bJean Delvare			client->addr);
4669560672369098b021688421f45476ce0c8bc451bJean Delvare		return -ENODEV;
4671da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
4681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4699560672369098b021688421f45476ce0c8bc451bJean Delvare	/* Identification */
4709560672369098b021688421f45476ce0c8bc451bJean Delvare	chip_id = i2c_smbus_read_byte_data(client, ADM1025_REG_CHIP_ID);
4719560672369098b021688421f45476ce0c8bc451bJean Delvare	if ((chip_id & 0xF0) != 0x20)
4729560672369098b021688421f45476ce0c8bc451bJean Delvare		return -ENODEV;
4731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4749560672369098b021688421f45476ce0c8bc451bJean Delvare	man_id = i2c_smbus_read_byte_data(client, ADM1025_REG_MAN_ID);
4759560672369098b021688421f45476ce0c8bc451bJean Delvare	if (man_id == 0x41)
4761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		name = "adm1025";
4779560672369098b021688421f45476ce0c8bc451bJean Delvare	else if (man_id == 0xA1 && client->addr != 0x2E)
4781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		name = "ne1619";
4799560672369098b021688421f45476ce0c8bc451bJean Delvare	else
4809560672369098b021688421f45476ce0c8bc451bJean Delvare		return -ENODEV;
4819560672369098b021688421f45476ce0c8bc451bJean Delvare
4827dbafe021ba360bf25674a7e290d3e4a5c953981Jean Delvare	strlcpy(info->type, name, I2C_NAME_SIZE);
4831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4847dbafe021ba360bf25674a7e290d3e4a5c953981Jean Delvare	return 0;
4857dbafe021ba360bf25674a7e290d3e4a5c953981Jean Delvare}
4861da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
487851a7afa82c9cbbfa2f85151872a29db0a3f6cbfAxel Linstatic void adm1025_init_client(struct i2c_client *client)
488851a7afa82c9cbbfa2f85151872a29db0a3f6cbfAxel Lin{
489851a7afa82c9cbbfa2f85151872a29db0a3f6cbfAxel Lin	u8 reg;
490851a7afa82c9cbbfa2f85151872a29db0a3f6cbfAxel Lin	struct adm1025_data *data = i2c_get_clientdata(client);
491851a7afa82c9cbbfa2f85151872a29db0a3f6cbfAxel Lin	int i;
492851a7afa82c9cbbfa2f85151872a29db0a3f6cbfAxel Lin
493851a7afa82c9cbbfa2f85151872a29db0a3f6cbfAxel Lin	data->vrm = vid_which_vrm();
494851a7afa82c9cbbfa2f85151872a29db0a3f6cbfAxel Lin
495851a7afa82c9cbbfa2f85151872a29db0a3f6cbfAxel Lin	/*
496851a7afa82c9cbbfa2f85151872a29db0a3f6cbfAxel Lin	 * Set high limits
497851a7afa82c9cbbfa2f85151872a29db0a3f6cbfAxel Lin	 * Usually we avoid setting limits on driver init, but it happens
498851a7afa82c9cbbfa2f85151872a29db0a3f6cbfAxel Lin	 * that the ADM1025 comes with stupid default limits (all registers
499851a7afa82c9cbbfa2f85151872a29db0a3f6cbfAxel Lin	 * set to 0). In case the chip has not gone through any limit
500851a7afa82c9cbbfa2f85151872a29db0a3f6cbfAxel Lin	 * setting yet, we better set the high limits to the max so that
501851a7afa82c9cbbfa2f85151872a29db0a3f6cbfAxel Lin	 * no alarm triggers.
502851a7afa82c9cbbfa2f85151872a29db0a3f6cbfAxel Lin	 */
503851a7afa82c9cbbfa2f85151872a29db0a3f6cbfAxel Lin	for (i = 0; i < 6; i++) {
504851a7afa82c9cbbfa2f85151872a29db0a3f6cbfAxel Lin		reg = i2c_smbus_read_byte_data(client,
505851a7afa82c9cbbfa2f85151872a29db0a3f6cbfAxel Lin					       ADM1025_REG_IN_MAX(i));
506851a7afa82c9cbbfa2f85151872a29db0a3f6cbfAxel Lin		if (reg == 0)
507851a7afa82c9cbbfa2f85151872a29db0a3f6cbfAxel Lin			i2c_smbus_write_byte_data(client,
508851a7afa82c9cbbfa2f85151872a29db0a3f6cbfAxel Lin						  ADM1025_REG_IN_MAX(i),
509851a7afa82c9cbbfa2f85151872a29db0a3f6cbfAxel Lin						  0xFF);
510851a7afa82c9cbbfa2f85151872a29db0a3f6cbfAxel Lin	}
511851a7afa82c9cbbfa2f85151872a29db0a3f6cbfAxel Lin	for (i = 0; i < 2; i++) {
512851a7afa82c9cbbfa2f85151872a29db0a3f6cbfAxel Lin		reg = i2c_smbus_read_byte_data(client,
513851a7afa82c9cbbfa2f85151872a29db0a3f6cbfAxel Lin					       ADM1025_REG_TEMP_HIGH(i));
514851a7afa82c9cbbfa2f85151872a29db0a3f6cbfAxel Lin		if (reg == 0)
515851a7afa82c9cbbfa2f85151872a29db0a3f6cbfAxel Lin			i2c_smbus_write_byte_data(client,
516851a7afa82c9cbbfa2f85151872a29db0a3f6cbfAxel Lin						  ADM1025_REG_TEMP_HIGH(i),
517851a7afa82c9cbbfa2f85151872a29db0a3f6cbfAxel Lin						  0x7F);
518851a7afa82c9cbbfa2f85151872a29db0a3f6cbfAxel Lin	}
519851a7afa82c9cbbfa2f85151872a29db0a3f6cbfAxel Lin
520851a7afa82c9cbbfa2f85151872a29db0a3f6cbfAxel Lin	/*
521851a7afa82c9cbbfa2f85151872a29db0a3f6cbfAxel Lin	 * Start the conversions
522851a7afa82c9cbbfa2f85151872a29db0a3f6cbfAxel Lin	 */
523851a7afa82c9cbbfa2f85151872a29db0a3f6cbfAxel Lin	reg = i2c_smbus_read_byte_data(client, ADM1025_REG_CONFIG);
524851a7afa82c9cbbfa2f85151872a29db0a3f6cbfAxel Lin	if (!(reg & 0x01))
525851a7afa82c9cbbfa2f85151872a29db0a3f6cbfAxel Lin		i2c_smbus_write_byte_data(client, ADM1025_REG_CONFIG,
526851a7afa82c9cbbfa2f85151872a29db0a3f6cbfAxel Lin					  (reg&0x7E)|0x01);
527851a7afa82c9cbbfa2f85151872a29db0a3f6cbfAxel Lin}
528851a7afa82c9cbbfa2f85151872a29db0a3f6cbfAxel Lin
5297dbafe021ba360bf25674a7e290d3e4a5c953981Jean Delvarestatic int adm1025_probe(struct i2c_client *client,
5307dbafe021ba360bf25674a7e290d3e4a5c953981Jean Delvare			 const struct i2c_device_id *id)
5317dbafe021ba360bf25674a7e290d3e4a5c953981Jean Delvare{
532aab3f57a11723c75477f43d7a510c148f2269d16Axel Lin	struct device *dev = &client->dev;
533aab3f57a11723c75477f43d7a510c148f2269d16Axel Lin	struct device *hwmon_dev;
5347dbafe021ba360bf25674a7e290d3e4a5c953981Jean Delvare	struct adm1025_data *data;
5357dbafe021ba360bf25674a7e290d3e4a5c953981Jean Delvare	u8 config;
5367dbafe021ba360bf25674a7e290d3e4a5c953981Jean Delvare
537aab3f57a11723c75477f43d7a510c148f2269d16Axel Lin	data = devm_kzalloc(dev, sizeof(struct adm1025_data), GFP_KERNEL);
5386702c9981e92aeb5cbfc192d23c11ef0f26b6157Guenter Roeck	if (!data)
5396702c9981e92aeb5cbfc192d23c11ef0f26b6157Guenter Roeck		return -ENOMEM;
5407dbafe021ba360bf25674a7e290d3e4a5c953981Jean Delvare
5417dbafe021ba360bf25674a7e290d3e4a5c953981Jean Delvare	i2c_set_clientdata(client, data);
542aab3f57a11723c75477f43d7a510c148f2269d16Axel Lin	data->client = client;
5437dbafe021ba360bf25674a7e290d3e4a5c953981Jean Delvare	mutex_init(&data->update_lock);
5441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
5451da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* Initialize the ADM1025 chip */
5464e9527998f8673b1e3bb6f9645b9700e7973581eJean Delvare	adm1025_init_client(client);
5471da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
548aab3f57a11723c75477f43d7a510c148f2269d16Axel Lin	/* sysfs hooks */
549aab3f57a11723c75477f43d7a510c148f2269d16Axel Lin	data->groups[0] = &adm1025_group;
5501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* Pin 11 is either in4 (+12V) or VID4 */
5517dbafe021ba360bf25674a7e290d3e4a5c953981Jean Delvare	config = i2c_smbus_read_byte_data(client, ADM1025_REG_CONFIG);
552aab3f57a11723c75477f43d7a510c148f2269d16Axel Lin	if (!(config & 0x20))
553aab3f57a11723c75477f43d7a510c148f2269d16Axel Lin		data->groups[1] = &adm1025_group_in4;
5541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
555aab3f57a11723c75477f43d7a510c148f2269d16Axel Lin	hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
556aab3f57a11723c75477f43d7a510c148f2269d16Axel Lin							   data, data->groups);
557aab3f57a11723c75477f43d7a510c148f2269d16Axel Lin	return PTR_ERR_OR_ZERO(hwmon_dev);
5581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
5591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
560851a7afa82c9cbbfa2f85151872a29db0a3f6cbfAxel Linstatic const struct i2c_device_id adm1025_id[] = {
561851a7afa82c9cbbfa2f85151872a29db0a3f6cbfAxel Lin	{ "adm1025", adm1025 },
562851a7afa82c9cbbfa2f85151872a29db0a3f6cbfAxel Lin	{ "ne1619", ne1619 },
563851a7afa82c9cbbfa2f85151872a29db0a3f6cbfAxel Lin	{ }
564851a7afa82c9cbbfa2f85151872a29db0a3f6cbfAxel Lin};
565851a7afa82c9cbbfa2f85151872a29db0a3f6cbfAxel LinMODULE_DEVICE_TABLE(i2c, adm1025_id);
5661da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
567851a7afa82c9cbbfa2f85151872a29db0a3f6cbfAxel Linstatic struct i2c_driver adm1025_driver = {
568851a7afa82c9cbbfa2f85151872a29db0a3f6cbfAxel Lin	.class		= I2C_CLASS_HWMON,
569851a7afa82c9cbbfa2f85151872a29db0a3f6cbfAxel Lin	.driver = {
570851a7afa82c9cbbfa2f85151872a29db0a3f6cbfAxel Lin		.name	= "adm1025",
571851a7afa82c9cbbfa2f85151872a29db0a3f6cbfAxel Lin	},
572851a7afa82c9cbbfa2f85151872a29db0a3f6cbfAxel Lin	.probe		= adm1025_probe,
573851a7afa82c9cbbfa2f85151872a29db0a3f6cbfAxel Lin	.id_table	= adm1025_id,
574851a7afa82c9cbbfa2f85151872a29db0a3f6cbfAxel Lin	.detect		= adm1025_detect,
575851a7afa82c9cbbfa2f85151872a29db0a3f6cbfAxel Lin	.address_list	= normal_i2c,
576851a7afa82c9cbbfa2f85151872a29db0a3f6cbfAxel Lin};
5771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
578f0967eea80ec2a19a4fe1ad27e3ff1b22c79a3c7Axel Linmodule_i2c_driver(adm1025_driver);
5791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
5807c81c60f3789a082e141d7a013392af5f78db16aJean DelvareMODULE_AUTHOR("Jean Delvare <jdelvare@suse.de>");
5811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus TorvaldsMODULE_DESCRIPTION("ADM1025 driver");
5821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus TorvaldsMODULE_LICENSE("GPL");
583