xref: /openbmc/linux/drivers/hwmon/adt7410.c (revision 983b97be)
1983b97beSHartmut Knaack /*
2983b97beSHartmut Knaack  * adt7410.c - Part of lm_sensors, Linux kernel modules for hardware
3983b97beSHartmut Knaack  *	 monitoring
4983b97beSHartmut Knaack  * This driver handles the ADT7410 and compatible digital temperature sensors.
5983b97beSHartmut Knaack  * Hartmut Knaack <knaack.h@gmx.de> 2012-07-22
6983b97beSHartmut Knaack  * based on lm75.c by Frodo Looijaard <frodol@dds.nl>
7983b97beSHartmut Knaack  * and adt7410.c from iio-staging by Sonic Zhang <sonic.zhang@analog.com>
8983b97beSHartmut Knaack  *
9983b97beSHartmut Knaack  * This program is free software; you can redistribute it and/or modify
10983b97beSHartmut Knaack  * it under the terms of the GNU General Public License as published by
11983b97beSHartmut Knaack  * the Free Software Foundation; either version 2 of the License, or
12983b97beSHartmut Knaack  * (at your option) any later version.
13983b97beSHartmut Knaack  *
14983b97beSHartmut Knaack  * This program is distributed in the hope that it will be useful,
15983b97beSHartmut Knaack  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16983b97beSHartmut Knaack  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17983b97beSHartmut Knaack  * GNU General Public License for more details.
18983b97beSHartmut Knaack  *
19983b97beSHartmut Knaack  * You should have received a copy of the GNU General Public License
20983b97beSHartmut Knaack  * along with this program; if not, write to the Free Software
21983b97beSHartmut Knaack  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22983b97beSHartmut Knaack  */
23983b97beSHartmut Knaack 
24983b97beSHartmut Knaack #include <linux/module.h>
25983b97beSHartmut Knaack #include <linux/init.h>
26983b97beSHartmut Knaack #include <linux/slab.h>
27983b97beSHartmut Knaack #include <linux/jiffies.h>
28983b97beSHartmut Knaack #include <linux/i2c.h>
29983b97beSHartmut Knaack #include <linux/hwmon.h>
30983b97beSHartmut Knaack #include <linux/hwmon-sysfs.h>
31983b97beSHartmut Knaack #include <linux/err.h>
32983b97beSHartmut Knaack #include <linux/mutex.h>
33983b97beSHartmut Knaack #include <linux/delay.h>
34983b97beSHartmut Knaack 
35983b97beSHartmut Knaack /*
36983b97beSHartmut Knaack  * ADT7410 registers definition
37983b97beSHartmut Knaack  */
38983b97beSHartmut Knaack 
39983b97beSHartmut Knaack #define ADT7410_TEMPERATURE		0
40983b97beSHartmut Knaack #define ADT7410_STATUS			2
41983b97beSHartmut Knaack #define ADT7410_CONFIG			3
42983b97beSHartmut Knaack #define ADT7410_T_ALARM_HIGH		4
43983b97beSHartmut Knaack #define ADT7410_T_ALARM_LOW		6
44983b97beSHartmut Knaack #define ADT7410_T_CRIT			8
45983b97beSHartmut Knaack #define ADT7410_T_HYST			0xA
46983b97beSHartmut Knaack 
47983b97beSHartmut Knaack /*
48983b97beSHartmut Knaack  * ADT7410 status
49983b97beSHartmut Knaack  */
50983b97beSHartmut Knaack #define ADT7410_STAT_T_LOW		(1 << 4)
51983b97beSHartmut Knaack #define ADT7410_STAT_T_HIGH		(1 << 5)
52983b97beSHartmut Knaack #define ADT7410_STAT_T_CRIT		(1 << 6)
53983b97beSHartmut Knaack #define ADT7410_STAT_NOT_RDY		(1 << 7)
54983b97beSHartmut Knaack 
55983b97beSHartmut Knaack /*
56983b97beSHartmut Knaack  * ADT7410 config
57983b97beSHartmut Knaack  */
58983b97beSHartmut Knaack #define ADT7410_FAULT_QUEUE_MASK	(1 << 0 | 1 << 1)
59983b97beSHartmut Knaack #define ADT7410_CT_POLARITY		(1 << 2)
60983b97beSHartmut Knaack #define ADT7410_INT_POLARITY		(1 << 3)
61983b97beSHartmut Knaack #define ADT7410_EVENT_MODE		(1 << 4)
62983b97beSHartmut Knaack #define ADT7410_MODE_MASK		(1 << 5 | 1 << 6)
63983b97beSHartmut Knaack #define ADT7410_FULL			(0 << 5 | 0 << 6)
64983b97beSHartmut Knaack #define ADT7410_PD			(1 << 5 | 1 << 6)
65983b97beSHartmut Knaack #define ADT7410_RESOLUTION		(1 << 7)
66983b97beSHartmut Knaack 
67983b97beSHartmut Knaack /*
68983b97beSHartmut Knaack  * ADT7410 masks
69983b97beSHartmut Knaack  */
70983b97beSHartmut Knaack #define ADT7410_T13_VALUE_MASK			0xFFF8
71983b97beSHartmut Knaack #define ADT7410_T_HYST_MASK			0xF
72983b97beSHartmut Knaack 
73983b97beSHartmut Knaack /* straight from the datasheet */
74983b97beSHartmut Knaack #define ADT7410_TEMP_MIN (-55000)
75983b97beSHartmut Knaack #define ADT7410_TEMP_MAX 150000
76983b97beSHartmut Knaack 
77983b97beSHartmut Knaack enum adt7410_type {		/* keep sorted in alphabetical order */
78983b97beSHartmut Knaack 	adt7410,
79983b97beSHartmut Knaack };
80983b97beSHartmut Knaack 
81983b97beSHartmut Knaack /* Addresses scanned */
82983b97beSHartmut Knaack static const unsigned short normal_i2c[] = { 0x48, 0x49, 0x4a, 0x4b,
83983b97beSHartmut Knaack 					I2C_CLIENT_END };
84983b97beSHartmut Knaack 
85983b97beSHartmut Knaack static const u8 ADT7410_REG_TEMP[4] = {
86983b97beSHartmut Knaack 	ADT7410_TEMPERATURE,		/* input */
87983b97beSHartmut Knaack 	ADT7410_T_ALARM_HIGH,		/* high */
88983b97beSHartmut Knaack 	ADT7410_T_ALARM_LOW,		/* low */
89983b97beSHartmut Knaack 	ADT7410_T_CRIT,			/* critical */
90983b97beSHartmut Knaack };
91983b97beSHartmut Knaack 
92983b97beSHartmut Knaack /* Each client has this additional data */
93983b97beSHartmut Knaack struct adt7410_data {
94983b97beSHartmut Knaack 	struct device		*hwmon_dev;
95983b97beSHartmut Knaack 	struct mutex		update_lock;
96983b97beSHartmut Knaack 	u8			config;
97983b97beSHartmut Knaack 	u8			oldconfig;
98983b97beSHartmut Knaack 	bool			valid;		/* true if registers valid */
99983b97beSHartmut Knaack 	unsigned long		last_updated;	/* In jiffies */
100983b97beSHartmut Knaack 	s16			temp[4];	/* Register values,
101983b97beSHartmut Knaack 						   0 = input
102983b97beSHartmut Knaack 						   1 = high
103983b97beSHartmut Knaack 						   2 = low
104983b97beSHartmut Knaack 						   3 = critical */
105983b97beSHartmut Knaack 	u8			hyst;		/* hysteresis offset */
106983b97beSHartmut Knaack };
107983b97beSHartmut Knaack 
108983b97beSHartmut Knaack /*
109983b97beSHartmut Knaack  * adt7410 register access by I2C
110983b97beSHartmut Knaack  */
111983b97beSHartmut Knaack static int adt7410_temp_ready(struct i2c_client *client)
112983b97beSHartmut Knaack {
113983b97beSHartmut Knaack 	int i, status;
114983b97beSHartmut Knaack 
115983b97beSHartmut Knaack 	for (i = 0; i < 6; i++) {
116983b97beSHartmut Knaack 		status = i2c_smbus_read_byte_data(client, ADT7410_STATUS);
117983b97beSHartmut Knaack 		if (status < 0)
118983b97beSHartmut Knaack 			return status;
119983b97beSHartmut Knaack 		if (!(status & ADT7410_STAT_NOT_RDY))
120983b97beSHartmut Knaack 			return 0;
121983b97beSHartmut Knaack 		msleep(60);
122983b97beSHartmut Knaack 	}
123983b97beSHartmut Knaack 	return -ETIMEDOUT;
124983b97beSHartmut Knaack }
125983b97beSHartmut Knaack 
126983b97beSHartmut Knaack static struct adt7410_data *adt7410_update_device(struct device *dev)
127983b97beSHartmut Knaack {
128983b97beSHartmut Knaack 	struct i2c_client *client = to_i2c_client(dev);
129983b97beSHartmut Knaack 	struct adt7410_data *data = i2c_get_clientdata(client);
130983b97beSHartmut Knaack 	struct adt7410_data *ret = data;
131983b97beSHartmut Knaack 	mutex_lock(&data->update_lock);
132983b97beSHartmut Knaack 
133983b97beSHartmut Knaack 	if (time_after(jiffies, data->last_updated + HZ + HZ / 2)
134983b97beSHartmut Knaack 	    || !data->valid) {
135983b97beSHartmut Knaack 		int i, status;
136983b97beSHartmut Knaack 
137983b97beSHartmut Knaack 		dev_dbg(&client->dev, "Starting update\n");
138983b97beSHartmut Knaack 
139983b97beSHartmut Knaack 		status = adt7410_temp_ready(client); /* check for new value */
140983b97beSHartmut Knaack 		if (unlikely(status)) {
141983b97beSHartmut Knaack 			ret = ERR_PTR(status);
142983b97beSHartmut Knaack 			goto abort;
143983b97beSHartmut Knaack 		}
144983b97beSHartmut Knaack 		for (i = 0; i < ARRAY_SIZE(data->temp); i++) {
145983b97beSHartmut Knaack 			status = i2c_smbus_read_word_swapped(client,
146983b97beSHartmut Knaack 							ADT7410_REG_TEMP[i]);
147983b97beSHartmut Knaack 			if (unlikely(status < 0)) {
148983b97beSHartmut Knaack 				dev_dbg(dev,
149983b97beSHartmut Knaack 					"Failed to read value: reg %d, error %d\n",
150983b97beSHartmut Knaack 					ADT7410_REG_TEMP[i], status);
151983b97beSHartmut Knaack 				ret = ERR_PTR(status);
152983b97beSHartmut Knaack 				goto abort;
153983b97beSHartmut Knaack 			}
154983b97beSHartmut Knaack 			data->temp[i] = status;
155983b97beSHartmut Knaack 		}
156983b97beSHartmut Knaack 		status = i2c_smbus_read_byte_data(client, ADT7410_T_HYST);
157983b97beSHartmut Knaack 		if (unlikely(status < 0)) {
158983b97beSHartmut Knaack 			dev_dbg(dev,
159983b97beSHartmut Knaack 				"Failed to read value: reg %d, error %d\n",
160983b97beSHartmut Knaack 				ADT7410_T_HYST, status);
161983b97beSHartmut Knaack 			ret = ERR_PTR(status);
162983b97beSHartmut Knaack 			goto abort;
163983b97beSHartmut Knaack 		}
164983b97beSHartmut Knaack 		data->hyst = status;
165983b97beSHartmut Knaack 		data->last_updated = jiffies;
166983b97beSHartmut Knaack 		data->valid = true;
167983b97beSHartmut Knaack 	}
168983b97beSHartmut Knaack 
169983b97beSHartmut Knaack abort:
170983b97beSHartmut Knaack 	mutex_unlock(&data->update_lock);
171983b97beSHartmut Knaack 	return ret;
172983b97beSHartmut Knaack }
173983b97beSHartmut Knaack 
174983b97beSHartmut Knaack static s16 ADT7410_TEMP_TO_REG(long temp)
175983b97beSHartmut Knaack {
176983b97beSHartmut Knaack 	return DIV_ROUND_CLOSEST(SENSORS_LIMIT(temp, ADT7410_TEMP_MIN,
177983b97beSHartmut Knaack 					       ADT7410_TEMP_MAX) * 128, 1000);
178983b97beSHartmut Knaack }
179983b97beSHartmut Knaack 
180983b97beSHartmut Knaack static int ADT7410_REG_TO_TEMP(struct adt7410_data *data, s16 reg)
181983b97beSHartmut Knaack {
182983b97beSHartmut Knaack 	/* in 13 bit mode, bits 0-2 are status flags - mask them out */
183983b97beSHartmut Knaack 	if (!(data->config & ADT7410_RESOLUTION))
184983b97beSHartmut Knaack 		reg &= ADT7410_T13_VALUE_MASK;
185983b97beSHartmut Knaack 	/*
186983b97beSHartmut Knaack 	 * temperature is stored in twos complement format, in steps of
187983b97beSHartmut Knaack 	 * 1/128°C
188983b97beSHartmut Knaack 	 */
189983b97beSHartmut Knaack 	return DIV_ROUND_CLOSEST(reg * 1000, 128);
190983b97beSHartmut Knaack }
191983b97beSHartmut Knaack 
192983b97beSHartmut Knaack /*-----------------------------------------------------------------------*/
193983b97beSHartmut Knaack 
194983b97beSHartmut Knaack /* sysfs attributes for hwmon */
195983b97beSHartmut Knaack 
196983b97beSHartmut Knaack static ssize_t adt7410_show_temp(struct device *dev,
197983b97beSHartmut Knaack 				 struct device_attribute *da, char *buf)
198983b97beSHartmut Knaack {
199983b97beSHartmut Knaack 	struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
200983b97beSHartmut Knaack 	struct adt7410_data *data = adt7410_update_device(dev);
201983b97beSHartmut Knaack 
202983b97beSHartmut Knaack 	if (IS_ERR(data))
203983b97beSHartmut Knaack 		return PTR_ERR(data);
204983b97beSHartmut Knaack 
205983b97beSHartmut Knaack 	return sprintf(buf, "%d\n", ADT7410_REG_TO_TEMP(data,
206983b97beSHartmut Knaack 		       data->temp[attr->index]));
207983b97beSHartmut Knaack }
208983b97beSHartmut Knaack 
209983b97beSHartmut Knaack static ssize_t adt7410_set_temp(struct device *dev,
210983b97beSHartmut Knaack 				struct device_attribute *da,
211983b97beSHartmut Knaack 				const char *buf, size_t count)
212983b97beSHartmut Knaack {
213983b97beSHartmut Knaack 	struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
214983b97beSHartmut Knaack 	struct i2c_client *client = to_i2c_client(dev);
215983b97beSHartmut Knaack 	struct adt7410_data *data = i2c_get_clientdata(client);
216983b97beSHartmut Knaack 	int nr = attr->index;
217983b97beSHartmut Knaack 	long temp;
218983b97beSHartmut Knaack 	int ret;
219983b97beSHartmut Knaack 
220983b97beSHartmut Knaack 	ret = kstrtol(buf, 10, &temp);
221983b97beSHartmut Knaack 	if (ret)
222983b97beSHartmut Knaack 		return ret;
223983b97beSHartmut Knaack 
224983b97beSHartmut Knaack 	mutex_lock(&data->update_lock);
225983b97beSHartmut Knaack 	data->temp[nr] = ADT7410_TEMP_TO_REG(temp);
226983b97beSHartmut Knaack 	ret = i2c_smbus_write_word_swapped(client, ADT7410_REG_TEMP[nr],
227983b97beSHartmut Knaack 					   data->temp[nr]);
228983b97beSHartmut Knaack 	if (ret)
229983b97beSHartmut Knaack 		count = ret;
230983b97beSHartmut Knaack 	mutex_unlock(&data->update_lock);
231983b97beSHartmut Knaack 	return count;
232983b97beSHartmut Knaack }
233983b97beSHartmut Knaack 
234983b97beSHartmut Knaack static ssize_t adt7410_show_t_hyst(struct device *dev,
235983b97beSHartmut Knaack 				   struct device_attribute *da,
236983b97beSHartmut Knaack 				   char *buf)
237983b97beSHartmut Knaack {
238983b97beSHartmut Knaack 	struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
239983b97beSHartmut Knaack 	struct adt7410_data *data = adt7410_update_device(dev);
240983b97beSHartmut Knaack 	int nr = attr->index;
241983b97beSHartmut Knaack 	int hyst = (data->hyst & ADT7410_T_HYST_MASK) * 1000;
242983b97beSHartmut Knaack 
243983b97beSHartmut Knaack 	/*
244983b97beSHartmut Knaack 	 * hysteresis is stored as a 4 bit offset in the device, convert it
245983b97beSHartmut Knaack 	 * to an absolute value
246983b97beSHartmut Knaack 	 */
247983b97beSHartmut Knaack 	if (nr == 2)	/* min has positive offset, others have negative */
248983b97beSHartmut Knaack 		hyst = -hyst;
249983b97beSHartmut Knaack 	return sprintf(buf, "%d\n",
250983b97beSHartmut Knaack 		       ADT7410_REG_TO_TEMP(data, data->temp[nr]) - hyst);
251983b97beSHartmut Knaack }
252983b97beSHartmut Knaack 
253983b97beSHartmut Knaack static ssize_t adt7410_set_t_hyst(struct device *dev,
254983b97beSHartmut Knaack 				  struct device_attribute *da,
255983b97beSHartmut Knaack 				  const char *buf, size_t count)
256983b97beSHartmut Knaack {
257983b97beSHartmut Knaack 	struct i2c_client *client = to_i2c_client(dev);
258983b97beSHartmut Knaack 	struct adt7410_data *data = i2c_get_clientdata(client);
259983b97beSHartmut Knaack 	int limit, ret;
260983b97beSHartmut Knaack 	long hyst;
261983b97beSHartmut Knaack 
262983b97beSHartmut Knaack 	ret = kstrtol(buf, 10, &hyst);
263983b97beSHartmut Knaack 	if (ret)
264983b97beSHartmut Knaack 		return ret;
265983b97beSHartmut Knaack 	/* convert absolute hysteresis value to a 4 bit delta value */
266983b97beSHartmut Knaack 	limit = ADT7410_REG_TO_TEMP(data, data->temp[1]);
267983b97beSHartmut Knaack 	hyst = SENSORS_LIMIT(hyst, ADT7410_TEMP_MIN, ADT7410_TEMP_MAX);
268983b97beSHartmut Knaack 	data->hyst = SENSORS_LIMIT(DIV_ROUND_CLOSEST(limit - hyst, 1000),
269983b97beSHartmut Knaack 				   0, ADT7410_T_HYST_MASK);
270983b97beSHartmut Knaack 	ret = i2c_smbus_write_byte_data(client, ADT7410_T_HYST, data->hyst);
271983b97beSHartmut Knaack 	if (ret)
272983b97beSHartmut Knaack 		return ret;
273983b97beSHartmut Knaack 
274983b97beSHartmut Knaack 	return count;
275983b97beSHartmut Knaack }
276983b97beSHartmut Knaack 
277983b97beSHartmut Knaack static ssize_t adt7410_show_alarm(struct device *dev,
278983b97beSHartmut Knaack 				  struct device_attribute *da,
279983b97beSHartmut Knaack 				  char *buf)
280983b97beSHartmut Knaack {
281983b97beSHartmut Knaack 	struct i2c_client *client = to_i2c_client(dev);
282983b97beSHartmut Knaack 	struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
283983b97beSHartmut Knaack 	int ret;
284983b97beSHartmut Knaack 
285983b97beSHartmut Knaack 	ret = i2c_smbus_read_byte_data(client, ADT7410_STATUS);
286983b97beSHartmut Knaack 	if (ret < 0)
287983b97beSHartmut Knaack 		return ret;
288983b97beSHartmut Knaack 
289983b97beSHartmut Knaack 	return sprintf(buf, "%d\n", !!(ret & attr->index));
290983b97beSHartmut Knaack }
291983b97beSHartmut Knaack 
292983b97beSHartmut Knaack static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, adt7410_show_temp, NULL, 0);
293983b97beSHartmut Knaack static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO,
294983b97beSHartmut Knaack 			  adt7410_show_temp, adt7410_set_temp, 1);
295983b97beSHartmut Knaack static SENSOR_DEVICE_ATTR(temp1_min, S_IWUSR | S_IRUGO,
296983b97beSHartmut Knaack 			  adt7410_show_temp, adt7410_set_temp, 2);
297983b97beSHartmut Knaack static SENSOR_DEVICE_ATTR(temp1_crit, S_IWUSR | S_IRUGO,
298983b97beSHartmut Knaack 			  adt7410_show_temp, adt7410_set_temp, 3);
299983b97beSHartmut Knaack static SENSOR_DEVICE_ATTR(temp1_max_hyst, S_IWUSR | S_IRUGO,
300983b97beSHartmut Knaack 			  adt7410_show_t_hyst, adt7410_set_t_hyst, 1);
301983b97beSHartmut Knaack static SENSOR_DEVICE_ATTR(temp1_min_hyst, S_IRUGO,
302983b97beSHartmut Knaack 			  adt7410_show_t_hyst, NULL, 2);
303983b97beSHartmut Knaack static SENSOR_DEVICE_ATTR(temp1_crit_hyst, S_IRUGO,
304983b97beSHartmut Knaack 			  adt7410_show_t_hyst, NULL, 3);
305983b97beSHartmut Knaack static SENSOR_DEVICE_ATTR(temp1_min_alarm, S_IRUGO, adt7410_show_alarm,
306983b97beSHartmut Knaack 			  NULL, ADT7410_STAT_T_LOW);
307983b97beSHartmut Knaack static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, adt7410_show_alarm,
308983b97beSHartmut Knaack 			  NULL, ADT7410_STAT_T_HIGH);
309983b97beSHartmut Knaack static SENSOR_DEVICE_ATTR(temp1_crit_alarm, S_IRUGO, adt7410_show_alarm,
310983b97beSHartmut Knaack 			  NULL, ADT7410_STAT_T_CRIT);
311983b97beSHartmut Knaack 
312983b97beSHartmut Knaack static struct attribute *adt7410_attributes[] = {
313983b97beSHartmut Knaack 	&sensor_dev_attr_temp1_input.dev_attr.attr,
314983b97beSHartmut Knaack 	&sensor_dev_attr_temp1_max.dev_attr.attr,
315983b97beSHartmut Knaack 	&sensor_dev_attr_temp1_min.dev_attr.attr,
316983b97beSHartmut Knaack 	&sensor_dev_attr_temp1_crit.dev_attr.attr,
317983b97beSHartmut Knaack 	&sensor_dev_attr_temp1_max_hyst.dev_attr.attr,
318983b97beSHartmut Knaack 	&sensor_dev_attr_temp1_min_hyst.dev_attr.attr,
319983b97beSHartmut Knaack 	&sensor_dev_attr_temp1_crit_hyst.dev_attr.attr,
320983b97beSHartmut Knaack 	&sensor_dev_attr_temp1_min_alarm.dev_attr.attr,
321983b97beSHartmut Knaack 	&sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
322983b97beSHartmut Knaack 	&sensor_dev_attr_temp1_crit_alarm.dev_attr.attr,
323983b97beSHartmut Knaack 	NULL
324983b97beSHartmut Knaack };
325983b97beSHartmut Knaack 
326983b97beSHartmut Knaack static const struct attribute_group adt7410_group = {
327983b97beSHartmut Knaack 	.attrs = adt7410_attributes,
328983b97beSHartmut Knaack };
329983b97beSHartmut Knaack 
330983b97beSHartmut Knaack /*-----------------------------------------------------------------------*/
331983b97beSHartmut Knaack 
332983b97beSHartmut Knaack /* device probe and removal */
333983b97beSHartmut Knaack 
334983b97beSHartmut Knaack static int adt7410_probe(struct i2c_client *client,
335983b97beSHartmut Knaack 			 const struct i2c_device_id *id)
336983b97beSHartmut Knaack {
337983b97beSHartmut Knaack 	struct adt7410_data *data;
338983b97beSHartmut Knaack 	int ret;
339983b97beSHartmut Knaack 
340983b97beSHartmut Knaack 	if (!i2c_check_functionality(client->adapter,
341983b97beSHartmut Knaack 			I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA))
342983b97beSHartmut Knaack 		return -ENODEV;
343983b97beSHartmut Knaack 
344983b97beSHartmut Knaack 	data = devm_kzalloc(&client->dev, sizeof(struct adt7410_data),
345983b97beSHartmut Knaack 			    GFP_KERNEL);
346983b97beSHartmut Knaack 	if (!data)
347983b97beSHartmut Knaack 		return -ENOMEM;
348983b97beSHartmut Knaack 
349983b97beSHartmut Knaack 	i2c_set_clientdata(client, data);
350983b97beSHartmut Knaack 	mutex_init(&data->update_lock);
351983b97beSHartmut Knaack 
352983b97beSHartmut Knaack 	/* configure as specified */
353983b97beSHartmut Knaack 	ret = i2c_smbus_read_byte_data(client, ADT7410_CONFIG);
354983b97beSHartmut Knaack 	if (ret < 0) {
355983b97beSHartmut Knaack 		dev_dbg(&client->dev, "Can't read config? %d\n", ret);
356983b97beSHartmut Knaack 		return ret;
357983b97beSHartmut Knaack 	}
358983b97beSHartmut Knaack 	data->oldconfig = ret;
359983b97beSHartmut Knaack 	/*
360983b97beSHartmut Knaack 	 * Set to 16 bit resolution, continous conversion and comparator mode.
361983b97beSHartmut Knaack 	 */
362983b97beSHartmut Knaack 	data->config = ret | ADT7410_FULL | ADT7410_RESOLUTION |
363983b97beSHartmut Knaack 			ADT7410_EVENT_MODE;
364983b97beSHartmut Knaack 	if (data->config != data->oldconfig) {
365983b97beSHartmut Knaack 		ret = i2c_smbus_write_byte_data(client, ADT7410_CONFIG,
366983b97beSHartmut Knaack 						data->config);
367983b97beSHartmut Knaack 		if (ret)
368983b97beSHartmut Knaack 			return ret;
369983b97beSHartmut Knaack 	}
370983b97beSHartmut Knaack 	dev_dbg(&client->dev, "Config %02x\n", data->config);
371983b97beSHartmut Knaack 
372983b97beSHartmut Knaack 	/* Register sysfs hooks */
373983b97beSHartmut Knaack 	ret = sysfs_create_group(&client->dev.kobj, &adt7410_group);
374983b97beSHartmut Knaack 	if (ret)
375983b97beSHartmut Knaack 		goto exit_restore;
376983b97beSHartmut Knaack 
377983b97beSHartmut Knaack 	data->hwmon_dev = hwmon_device_register(&client->dev);
378983b97beSHartmut Knaack 	if (IS_ERR(data->hwmon_dev)) {
379983b97beSHartmut Knaack 		ret = PTR_ERR(data->hwmon_dev);
380983b97beSHartmut Knaack 		goto exit_remove;
381983b97beSHartmut Knaack 	}
382983b97beSHartmut Knaack 
383983b97beSHartmut Knaack 	dev_info(&client->dev, "sensor '%s'\n", client->name);
384983b97beSHartmut Knaack 
385983b97beSHartmut Knaack 	return 0;
386983b97beSHartmut Knaack 
387983b97beSHartmut Knaack exit_remove:
388983b97beSHartmut Knaack 	sysfs_remove_group(&client->dev.kobj, &adt7410_group);
389983b97beSHartmut Knaack exit_restore:
390983b97beSHartmut Knaack 	i2c_smbus_write_byte_data(client, ADT7410_CONFIG, data->oldconfig);
391983b97beSHartmut Knaack 	return ret;
392983b97beSHartmut Knaack }
393983b97beSHartmut Knaack 
394983b97beSHartmut Knaack static int adt7410_remove(struct i2c_client *client)
395983b97beSHartmut Knaack {
396983b97beSHartmut Knaack 	struct adt7410_data *data = i2c_get_clientdata(client);
397983b97beSHartmut Knaack 
398983b97beSHartmut Knaack 	hwmon_device_unregister(data->hwmon_dev);
399983b97beSHartmut Knaack 	sysfs_remove_group(&client->dev.kobj, &adt7410_group);
400983b97beSHartmut Knaack 	if (data->oldconfig != data->config)
401983b97beSHartmut Knaack 		i2c_smbus_write_byte_data(client, ADT7410_CONFIG,
402983b97beSHartmut Knaack 					  data->oldconfig);
403983b97beSHartmut Knaack 	return 0;
404983b97beSHartmut Knaack }
405983b97beSHartmut Knaack 
406983b97beSHartmut Knaack static const struct i2c_device_id adt7410_ids[] = {
407983b97beSHartmut Knaack 	{ "adt7410", adt7410, },
408983b97beSHartmut Knaack 	{ /* LIST END */ }
409983b97beSHartmut Knaack };
410983b97beSHartmut Knaack MODULE_DEVICE_TABLE(i2c, adt7410_ids);
411983b97beSHartmut Knaack 
412983b97beSHartmut Knaack #ifdef CONFIG_PM
413983b97beSHartmut Knaack static int adt7410_suspend(struct device *dev)
414983b97beSHartmut Knaack {
415983b97beSHartmut Knaack 	int ret;
416983b97beSHartmut Knaack 	struct i2c_client *client = to_i2c_client(dev);
417983b97beSHartmut Knaack 	struct adt7410_data *data = i2c_get_clientdata(client);
418983b97beSHartmut Knaack 
419983b97beSHartmut Knaack 	ret = i2c_smbus_write_byte_data(client, ADT7410_CONFIG,
420983b97beSHartmut Knaack 					data->config | ADT7410_PD);
421983b97beSHartmut Knaack 	return ret;
422983b97beSHartmut Knaack }
423983b97beSHartmut Knaack 
424983b97beSHartmut Knaack static int adt7410_resume(struct device *dev)
425983b97beSHartmut Knaack {
426983b97beSHartmut Knaack 	int ret;
427983b97beSHartmut Knaack 	struct i2c_client *client = to_i2c_client(dev);
428983b97beSHartmut Knaack 	struct adt7410_data *data = i2c_get_clientdata(client);
429983b97beSHartmut Knaack 
430983b97beSHartmut Knaack 	ret = i2c_smbus_write_byte_data(client, ADT7410_CONFIG, data->config);
431983b97beSHartmut Knaack 	return ret;
432983b97beSHartmut Knaack }
433983b97beSHartmut Knaack 
434983b97beSHartmut Knaack static const struct dev_pm_ops adt7410_dev_pm_ops = {
435983b97beSHartmut Knaack 	.suspend	= adt7410_suspend,
436983b97beSHartmut Knaack 	.resume		= adt7410_resume,
437983b97beSHartmut Knaack };
438983b97beSHartmut Knaack #define ADT7410_DEV_PM_OPS (&adt7410_dev_pm_ops)
439983b97beSHartmut Knaack #else
440983b97beSHartmut Knaack #define ADT7410_DEV_PM_OPS NULL
441983b97beSHartmut Knaack #endif /* CONFIG_PM */
442983b97beSHartmut Knaack 
443983b97beSHartmut Knaack static struct i2c_driver adt7410_driver = {
444983b97beSHartmut Knaack 	.class		= I2C_CLASS_HWMON,
445983b97beSHartmut Knaack 	.driver = {
446983b97beSHartmut Knaack 		.name	= "adt7410",
447983b97beSHartmut Knaack 		.pm	= ADT7410_DEV_PM_OPS,
448983b97beSHartmut Knaack 	},
449983b97beSHartmut Knaack 	.probe		= adt7410_probe,
450983b97beSHartmut Knaack 	.remove		= adt7410_remove,
451983b97beSHartmut Knaack 	.id_table	= adt7410_ids,
452983b97beSHartmut Knaack 	.address_list	= normal_i2c,
453983b97beSHartmut Knaack };
454983b97beSHartmut Knaack 
455983b97beSHartmut Knaack module_i2c_driver(adt7410_driver);
456983b97beSHartmut Knaack 
457983b97beSHartmut Knaack MODULE_AUTHOR("Hartmut Knaack");
458983b97beSHartmut Knaack MODULE_DESCRIPTION("ADT7410 driver");
459983b97beSHartmut Knaack MODULE_LICENSE("GPL");
460