xref: /linux/drivers/hwmon/max6639.c (revision 2a844c148e1f714ebf42cb96e1b172ce394c36c9)
1a5b79d62Sstigge@antcom.de /*
2a5b79d62Sstigge@antcom.de  * max6639.c - Support for Maxim MAX6639
3a5b79d62Sstigge@antcom.de  *
4a5b79d62Sstigge@antcom.de  * 2-Channel Temperature Monitor with Dual PWM Fan-Speed Controller
5a5b79d62Sstigge@antcom.de  *
6a5b79d62Sstigge@antcom.de  * Copyright (C) 2010, 2011 Roland Stigge <stigge@antcom.de>
7a5b79d62Sstigge@antcom.de  *
8a5b79d62Sstigge@antcom.de  * based on the initial MAX6639 support from semptian.net
9a5b79d62Sstigge@antcom.de  * by He Changqing <hechangqing@semptian.com>
10a5b79d62Sstigge@antcom.de  *
11a5b79d62Sstigge@antcom.de  * This program is free software; you can redistribute it and/or modify
12a5b79d62Sstigge@antcom.de  * it under the terms of the GNU General Public License as published by
13a5b79d62Sstigge@antcom.de  * the Free Software Foundation; either version 2 of the License, or
14a5b79d62Sstigge@antcom.de  * (at your option) any later version.
15a5b79d62Sstigge@antcom.de  *
16a5b79d62Sstigge@antcom.de  * This program is distributed in the hope that it will be useful,
17a5b79d62Sstigge@antcom.de  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18a5b79d62Sstigge@antcom.de  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19a5b79d62Sstigge@antcom.de  * GNU General Public License for more details.
20a5b79d62Sstigge@antcom.de  *
21a5b79d62Sstigge@antcom.de  * You should have received a copy of the GNU General Public License
22a5b79d62Sstigge@antcom.de  * along with this program; if not, write to the Free Software
23a5b79d62Sstigge@antcom.de  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24a5b79d62Sstigge@antcom.de  */
25a5b79d62Sstigge@antcom.de 
26a5b79d62Sstigge@antcom.de #include <linux/module.h>
27a5b79d62Sstigge@antcom.de #include <linux/init.h>
28a5b79d62Sstigge@antcom.de #include <linux/slab.h>
29a5b79d62Sstigge@antcom.de #include <linux/jiffies.h>
30a5b79d62Sstigge@antcom.de #include <linux/i2c.h>
31a5b79d62Sstigge@antcom.de #include <linux/hwmon.h>
32a5b79d62Sstigge@antcom.de #include <linux/hwmon-sysfs.h>
33a5b79d62Sstigge@antcom.de #include <linux/err.h>
34a5b79d62Sstigge@antcom.de #include <linux/mutex.h>
35a5b79d62Sstigge@antcom.de #include <linux/i2c/max6639.h>
36a5b79d62Sstigge@antcom.de 
37a5b79d62Sstigge@antcom.de /* Addresses to scan */
38a5b79d62Sstigge@antcom.de static unsigned short normal_i2c[] = { 0x2c, 0x2e, 0x2f, I2C_CLIENT_END };
39a5b79d62Sstigge@antcom.de 
40a5b79d62Sstigge@antcom.de /* The MAX6639 registers, valid channel numbers: 0, 1 */
41a5b79d62Sstigge@antcom.de #define MAX6639_REG_TEMP(ch)			(0x00 + (ch))
42a5b79d62Sstigge@antcom.de #define MAX6639_REG_STATUS			0x02
43a5b79d62Sstigge@antcom.de #define MAX6639_REG_OUTPUT_MASK			0x03
44a5b79d62Sstigge@antcom.de #define MAX6639_REG_GCONFIG			0x04
45a5b79d62Sstigge@antcom.de #define MAX6639_REG_TEMP_EXT(ch)		(0x05 + (ch))
46a5b79d62Sstigge@antcom.de #define MAX6639_REG_ALERT_LIMIT(ch)		(0x08 + (ch))
47a5b79d62Sstigge@antcom.de #define MAX6639_REG_OT_LIMIT(ch)		(0x0A + (ch))
48a5b79d62Sstigge@antcom.de #define MAX6639_REG_THERM_LIMIT(ch)		(0x0C + (ch))
49a5b79d62Sstigge@antcom.de #define MAX6639_REG_FAN_CONFIG1(ch)		(0x10 + (ch) * 4)
50a5b79d62Sstigge@antcom.de #define MAX6639_REG_FAN_CONFIG2a(ch)		(0x11 + (ch) * 4)
51a5b79d62Sstigge@antcom.de #define MAX6639_REG_FAN_CONFIG2b(ch)		(0x12 + (ch) * 4)
52a5b79d62Sstigge@antcom.de #define MAX6639_REG_FAN_CONFIG3(ch)		(0x13 + (ch) * 4)
53a5b79d62Sstigge@antcom.de #define MAX6639_REG_FAN_CNT(ch)			(0x20 + (ch))
54a5b79d62Sstigge@antcom.de #define MAX6639_REG_TARGET_CNT(ch)		(0x22 + (ch))
55a5b79d62Sstigge@antcom.de #define MAX6639_REG_FAN_PPR(ch)			(0x24 + (ch))
56a5b79d62Sstigge@antcom.de #define MAX6639_REG_TARGTDUTY(ch)		(0x26 + (ch))
57a5b79d62Sstigge@antcom.de #define MAX6639_REG_FAN_START_TEMP(ch)		(0x28 + (ch))
58a5b79d62Sstigge@antcom.de #define MAX6639_REG_DEVID			0x3D
59a5b79d62Sstigge@antcom.de #define MAX6639_REG_MANUID			0x3E
60a5b79d62Sstigge@antcom.de #define MAX6639_REG_DEVREV			0x3F
61a5b79d62Sstigge@antcom.de 
62a5b79d62Sstigge@antcom.de /* Register bits */
63a5b79d62Sstigge@antcom.de #define MAX6639_GCONFIG_STANDBY			0x80
64a5b79d62Sstigge@antcom.de #define MAX6639_GCONFIG_POR			0x40
65a5b79d62Sstigge@antcom.de #define MAX6639_GCONFIG_DISABLE_TIMEOUT		0x20
66a5b79d62Sstigge@antcom.de #define MAX6639_GCONFIG_CH2_LOCAL		0x10
67177f3b92Sstigge@antcom.de #define MAX6639_GCONFIG_PWM_FREQ_HI		0x08
68a5b79d62Sstigge@antcom.de 
69a5b79d62Sstigge@antcom.de #define MAX6639_FAN_CONFIG1_PWM			0x80
70a5b79d62Sstigge@antcom.de 
71177f3b92Sstigge@antcom.de #define MAX6639_FAN_CONFIG3_THERM_FULL_SPEED	0x40
72177f3b92Sstigge@antcom.de 
73a5b79d62Sstigge@antcom.de static const int rpm_ranges[] = { 2000, 4000, 8000, 16000 };
74a5b79d62Sstigge@antcom.de 
75b63d97a3SChris D Schimp #define FAN_FROM_REG(val, rpm_range)	((val) == 0 || (val) == 255 ? \
76b63d97a3SChris D Schimp 				0 : (rpm_ranges[rpm_range] * 30) / (val))
77*2a844c14SGuenter Roeck #define TEMP_LIMIT_TO_REG(val)	clamp_val((val) / 1000, 0, 255)
78a5b79d62Sstigge@antcom.de 
79a5b79d62Sstigge@antcom.de /*
80a5b79d62Sstigge@antcom.de  * Client data (each client gets its own)
81a5b79d62Sstigge@antcom.de  */
82a5b79d62Sstigge@antcom.de struct max6639_data {
83a5b79d62Sstigge@antcom.de 	struct device *hwmon_dev;
84a5b79d62Sstigge@antcom.de 	struct mutex update_lock;
85a5b79d62Sstigge@antcom.de 	char valid;		/* !=0 if following fields are valid */
86a5b79d62Sstigge@antcom.de 	unsigned long last_updated;	/* In jiffies */
87a5b79d62Sstigge@antcom.de 
88a5b79d62Sstigge@antcom.de 	/* Register values sampled regularly */
89a5b79d62Sstigge@antcom.de 	u16 temp[2];		/* Temperature, in 1/8 C, 0..255 C */
90a5b79d62Sstigge@antcom.de 	bool temp_fault[2];	/* Detected temperature diode failure */
91a5b79d62Sstigge@antcom.de 	u8 fan[2];		/* Register value: TACH count for fans >=30 */
92a5b79d62Sstigge@antcom.de 	u8 status;		/* Detected channel alarms and fan failures */
93a5b79d62Sstigge@antcom.de 
94a5b79d62Sstigge@antcom.de 	/* Register values only written to */
95a5b79d62Sstigge@antcom.de 	u8 pwm[2];		/* Register value: Duty cycle 0..120 */
96a5b79d62Sstigge@antcom.de 	u8 temp_therm[2];	/* THERM Temperature, 0..255 C (->_max) */
97a5b79d62Sstigge@antcom.de 	u8 temp_alert[2];	/* ALERT Temperature, 0..255 C (->_crit) */
98a5b79d62Sstigge@antcom.de 	u8 temp_ot[2];		/* OT Temperature, 0..255 C (->_emergency) */
99a5b79d62Sstigge@antcom.de 
100a5b79d62Sstigge@antcom.de 	/* Register values initialized only once */
101a5b79d62Sstigge@antcom.de 	u8 ppr;			/* Pulses per rotation 0..3 for 1..4 ppr */
102a5b79d62Sstigge@antcom.de 	u8 rpm_range;		/* Index in above rpm_ranges table */
103a5b79d62Sstigge@antcom.de };
104a5b79d62Sstigge@antcom.de 
105a5b79d62Sstigge@antcom.de static struct max6639_data *max6639_update_device(struct device *dev)
106a5b79d62Sstigge@antcom.de {
107a5b79d62Sstigge@antcom.de 	struct i2c_client *client = to_i2c_client(dev);
108a5b79d62Sstigge@antcom.de 	struct max6639_data *data = i2c_get_clientdata(client);
109a5b79d62Sstigge@antcom.de 	struct max6639_data *ret = data;
110a5b79d62Sstigge@antcom.de 	int i;
111a5b79d62Sstigge@antcom.de 	int status_reg;
112a5b79d62Sstigge@antcom.de 
113a5b79d62Sstigge@antcom.de 	mutex_lock(&data->update_lock);
114a5b79d62Sstigge@antcom.de 
115a5b79d62Sstigge@antcom.de 	if (time_after(jiffies, data->last_updated + 2 * HZ) || !data->valid) {
116a5b79d62Sstigge@antcom.de 		int res;
117a5b79d62Sstigge@antcom.de 
118a5b79d62Sstigge@antcom.de 		dev_dbg(&client->dev, "Starting max6639 update\n");
119a5b79d62Sstigge@antcom.de 
120a5b79d62Sstigge@antcom.de 		status_reg = i2c_smbus_read_byte_data(client,
121a5b79d62Sstigge@antcom.de 						      MAX6639_REG_STATUS);
122a5b79d62Sstigge@antcom.de 		if (status_reg < 0) {
123a5b79d62Sstigge@antcom.de 			ret = ERR_PTR(status_reg);
124a5b79d62Sstigge@antcom.de 			goto abort;
125a5b79d62Sstigge@antcom.de 		}
126a5b79d62Sstigge@antcom.de 
127a5b79d62Sstigge@antcom.de 		data->status = status_reg;
128a5b79d62Sstigge@antcom.de 
129a5b79d62Sstigge@antcom.de 		for (i = 0; i < 2; i++) {
130a5b79d62Sstigge@antcom.de 			res = i2c_smbus_read_byte_data(client,
131a5b79d62Sstigge@antcom.de 					MAX6639_REG_FAN_CNT(i));
132a5b79d62Sstigge@antcom.de 			if (res < 0) {
133a5b79d62Sstigge@antcom.de 				ret = ERR_PTR(res);
134a5b79d62Sstigge@antcom.de 				goto abort;
135a5b79d62Sstigge@antcom.de 			}
136a5b79d62Sstigge@antcom.de 			data->fan[i] = res;
137a5b79d62Sstigge@antcom.de 
138a5b79d62Sstigge@antcom.de 			res = i2c_smbus_read_byte_data(client,
139a5b79d62Sstigge@antcom.de 					MAX6639_REG_TEMP_EXT(i));
140a5b79d62Sstigge@antcom.de 			if (res < 0) {
141a5b79d62Sstigge@antcom.de 				ret = ERR_PTR(res);
142a5b79d62Sstigge@antcom.de 				goto abort;
143a5b79d62Sstigge@antcom.de 			}
144a5b79d62Sstigge@antcom.de 			data->temp[i] = res >> 5;
145a5b79d62Sstigge@antcom.de 			data->temp_fault[i] = res & 0x01;
146a5b79d62Sstigge@antcom.de 
147a5b79d62Sstigge@antcom.de 			res = i2c_smbus_read_byte_data(client,
148a5b79d62Sstigge@antcom.de 					MAX6639_REG_TEMP(i));
149a5b79d62Sstigge@antcom.de 			if (res < 0) {
150a5b79d62Sstigge@antcom.de 				ret = ERR_PTR(res);
151a5b79d62Sstigge@antcom.de 				goto abort;
152a5b79d62Sstigge@antcom.de 			}
153a5b79d62Sstigge@antcom.de 			data->temp[i] |= res << 3;
154a5b79d62Sstigge@antcom.de 		}
155a5b79d62Sstigge@antcom.de 
156a5b79d62Sstigge@antcom.de 		data->last_updated = jiffies;
157a5b79d62Sstigge@antcom.de 		data->valid = 1;
158a5b79d62Sstigge@antcom.de 	}
159a5b79d62Sstigge@antcom.de abort:
160a5b79d62Sstigge@antcom.de 	mutex_unlock(&data->update_lock);
161a5b79d62Sstigge@antcom.de 
162a5b79d62Sstigge@antcom.de 	return ret;
163a5b79d62Sstigge@antcom.de }
164a5b79d62Sstigge@antcom.de 
165a5b79d62Sstigge@antcom.de static ssize_t show_temp_input(struct device *dev,
166a5b79d62Sstigge@antcom.de 			       struct device_attribute *dev_attr, char *buf)
167a5b79d62Sstigge@antcom.de {
168a5b79d62Sstigge@antcom.de 	long temp;
169a5b79d62Sstigge@antcom.de 	struct max6639_data *data = max6639_update_device(dev);
170a5b79d62Sstigge@antcom.de 	struct sensor_device_attribute *attr = to_sensor_dev_attr(dev_attr);
171a5b79d62Sstigge@antcom.de 
172a5b79d62Sstigge@antcom.de 	if (IS_ERR(data))
173a5b79d62Sstigge@antcom.de 		return PTR_ERR(data);
174a5b79d62Sstigge@antcom.de 
175a5b79d62Sstigge@antcom.de 	temp = data->temp[attr->index] * 125;
176a5b79d62Sstigge@antcom.de 	return sprintf(buf, "%ld\n", temp);
177a5b79d62Sstigge@antcom.de }
178a5b79d62Sstigge@antcom.de 
179a5b79d62Sstigge@antcom.de static ssize_t show_temp_fault(struct device *dev,
180a5b79d62Sstigge@antcom.de 			       struct device_attribute *dev_attr, char *buf)
181a5b79d62Sstigge@antcom.de {
182a5b79d62Sstigge@antcom.de 	struct max6639_data *data = max6639_update_device(dev);
183a5b79d62Sstigge@antcom.de 	struct sensor_device_attribute *attr = to_sensor_dev_attr(dev_attr);
184a5b79d62Sstigge@antcom.de 
185a5b79d62Sstigge@antcom.de 	if (IS_ERR(data))
186a5b79d62Sstigge@antcom.de 		return PTR_ERR(data);
187a5b79d62Sstigge@antcom.de 
188a5b79d62Sstigge@antcom.de 	return sprintf(buf, "%d\n", data->temp_fault[attr->index]);
189a5b79d62Sstigge@antcom.de }
190a5b79d62Sstigge@antcom.de 
191a5b79d62Sstigge@antcom.de static ssize_t show_temp_max(struct device *dev,
192a5b79d62Sstigge@antcom.de 			     struct device_attribute *dev_attr, char *buf)
193a5b79d62Sstigge@antcom.de {
194a5b79d62Sstigge@antcom.de 	struct i2c_client *client = to_i2c_client(dev);
195a5b79d62Sstigge@antcom.de 	struct max6639_data *data = i2c_get_clientdata(client);
196a5b79d62Sstigge@antcom.de 	struct sensor_device_attribute *attr = to_sensor_dev_attr(dev_attr);
197a5b79d62Sstigge@antcom.de 
198a5b79d62Sstigge@antcom.de 	return sprintf(buf, "%d\n", (data->temp_therm[attr->index] * 1000));
199a5b79d62Sstigge@antcom.de }
200a5b79d62Sstigge@antcom.de 
201a5b79d62Sstigge@antcom.de static ssize_t set_temp_max(struct device *dev,
202a5b79d62Sstigge@antcom.de 			    struct device_attribute *dev_attr,
203a5b79d62Sstigge@antcom.de 			    const char *buf, size_t count)
204a5b79d62Sstigge@antcom.de {
205a5b79d62Sstigge@antcom.de 	struct i2c_client *client = to_i2c_client(dev);
206a5b79d62Sstigge@antcom.de 	struct max6639_data *data = i2c_get_clientdata(client);
207a5b79d62Sstigge@antcom.de 	struct sensor_device_attribute *attr = to_sensor_dev_attr(dev_attr);
208a5b79d62Sstigge@antcom.de 	unsigned long val;
209a5b79d62Sstigge@antcom.de 	int res;
210a5b79d62Sstigge@antcom.de 
211179c4fdbSFrans Meulenbroeks 	res = kstrtoul(buf, 10, &val);
212a5b79d62Sstigge@antcom.de 	if (res)
213a5b79d62Sstigge@antcom.de 		return res;
214a5b79d62Sstigge@antcom.de 
215a5b79d62Sstigge@antcom.de 	mutex_lock(&data->update_lock);
216a5b79d62Sstigge@antcom.de 	data->temp_therm[attr->index] = TEMP_LIMIT_TO_REG(val);
217a5b79d62Sstigge@antcom.de 	i2c_smbus_write_byte_data(client,
218a5b79d62Sstigge@antcom.de 				  MAX6639_REG_THERM_LIMIT(attr->index),
219a5b79d62Sstigge@antcom.de 				  data->temp_therm[attr->index]);
220a5b79d62Sstigge@antcom.de 	mutex_unlock(&data->update_lock);
221a5b79d62Sstigge@antcom.de 	return count;
222a5b79d62Sstigge@antcom.de }
223a5b79d62Sstigge@antcom.de 
224a5b79d62Sstigge@antcom.de static ssize_t show_temp_crit(struct device *dev,
225a5b79d62Sstigge@antcom.de 			      struct device_attribute *dev_attr, char *buf)
226a5b79d62Sstigge@antcom.de {
227a5b79d62Sstigge@antcom.de 	struct i2c_client *client = to_i2c_client(dev);
228a5b79d62Sstigge@antcom.de 	struct max6639_data *data = i2c_get_clientdata(client);
229a5b79d62Sstigge@antcom.de 	struct sensor_device_attribute *attr = to_sensor_dev_attr(dev_attr);
230a5b79d62Sstigge@antcom.de 
231a5b79d62Sstigge@antcom.de 	return sprintf(buf, "%d\n", (data->temp_alert[attr->index] * 1000));
232a5b79d62Sstigge@antcom.de }
233a5b79d62Sstigge@antcom.de 
234a5b79d62Sstigge@antcom.de static ssize_t set_temp_crit(struct device *dev,
235a5b79d62Sstigge@antcom.de 			     struct device_attribute *dev_attr,
236a5b79d62Sstigge@antcom.de 			     const char *buf, size_t count)
237a5b79d62Sstigge@antcom.de {
238a5b79d62Sstigge@antcom.de 	struct i2c_client *client = to_i2c_client(dev);
239a5b79d62Sstigge@antcom.de 	struct max6639_data *data = i2c_get_clientdata(client);
240a5b79d62Sstigge@antcom.de 	struct sensor_device_attribute *attr = to_sensor_dev_attr(dev_attr);
241a5b79d62Sstigge@antcom.de 	unsigned long val;
242a5b79d62Sstigge@antcom.de 	int res;
243a5b79d62Sstigge@antcom.de 
244179c4fdbSFrans Meulenbroeks 	res = kstrtoul(buf, 10, &val);
245a5b79d62Sstigge@antcom.de 	if (res)
246a5b79d62Sstigge@antcom.de 		return res;
247a5b79d62Sstigge@antcom.de 
248a5b79d62Sstigge@antcom.de 	mutex_lock(&data->update_lock);
249a5b79d62Sstigge@antcom.de 	data->temp_alert[attr->index] = TEMP_LIMIT_TO_REG(val);
250a5b79d62Sstigge@antcom.de 	i2c_smbus_write_byte_data(client,
251a5b79d62Sstigge@antcom.de 				  MAX6639_REG_ALERT_LIMIT(attr->index),
252a5b79d62Sstigge@antcom.de 				  data->temp_alert[attr->index]);
253a5b79d62Sstigge@antcom.de 	mutex_unlock(&data->update_lock);
254a5b79d62Sstigge@antcom.de 	return count;
255a5b79d62Sstigge@antcom.de }
256a5b79d62Sstigge@antcom.de 
257a5b79d62Sstigge@antcom.de static ssize_t show_temp_emergency(struct device *dev,
258a5b79d62Sstigge@antcom.de 				   struct device_attribute *dev_attr,
259a5b79d62Sstigge@antcom.de 				   char *buf)
260a5b79d62Sstigge@antcom.de {
261a5b79d62Sstigge@antcom.de 	struct i2c_client *client = to_i2c_client(dev);
262a5b79d62Sstigge@antcom.de 	struct max6639_data *data = i2c_get_clientdata(client);
263a5b79d62Sstigge@antcom.de 	struct sensor_device_attribute *attr = to_sensor_dev_attr(dev_attr);
264a5b79d62Sstigge@antcom.de 
265a5b79d62Sstigge@antcom.de 	return sprintf(buf, "%d\n", (data->temp_ot[attr->index] * 1000));
266a5b79d62Sstigge@antcom.de }
267a5b79d62Sstigge@antcom.de 
268a5b79d62Sstigge@antcom.de static ssize_t set_temp_emergency(struct device *dev,
269a5b79d62Sstigge@antcom.de 				  struct device_attribute *dev_attr,
270a5b79d62Sstigge@antcom.de 				  const char *buf, size_t count)
271a5b79d62Sstigge@antcom.de {
272a5b79d62Sstigge@antcom.de 	struct i2c_client *client = to_i2c_client(dev);
273a5b79d62Sstigge@antcom.de 	struct max6639_data *data = i2c_get_clientdata(client);
274a5b79d62Sstigge@antcom.de 	struct sensor_device_attribute *attr = to_sensor_dev_attr(dev_attr);
275a5b79d62Sstigge@antcom.de 	unsigned long val;
276a5b79d62Sstigge@antcom.de 	int res;
277a5b79d62Sstigge@antcom.de 
278179c4fdbSFrans Meulenbroeks 	res = kstrtoul(buf, 10, &val);
279a5b79d62Sstigge@antcom.de 	if (res)
280a5b79d62Sstigge@antcom.de 		return res;
281a5b79d62Sstigge@antcom.de 
282a5b79d62Sstigge@antcom.de 	mutex_lock(&data->update_lock);
283a5b79d62Sstigge@antcom.de 	data->temp_ot[attr->index] = TEMP_LIMIT_TO_REG(val);
284a5b79d62Sstigge@antcom.de 	i2c_smbus_write_byte_data(client,
285a5b79d62Sstigge@antcom.de 				  MAX6639_REG_OT_LIMIT(attr->index),
286a5b79d62Sstigge@antcom.de 				  data->temp_ot[attr->index]);
287a5b79d62Sstigge@antcom.de 	mutex_unlock(&data->update_lock);
288a5b79d62Sstigge@antcom.de 	return count;
289a5b79d62Sstigge@antcom.de }
290a5b79d62Sstigge@antcom.de 
291a5b79d62Sstigge@antcom.de static ssize_t show_pwm(struct device *dev,
292a5b79d62Sstigge@antcom.de 			struct device_attribute *dev_attr, char *buf)
293a5b79d62Sstigge@antcom.de {
294a5b79d62Sstigge@antcom.de 	struct i2c_client *client = to_i2c_client(dev);
295a5b79d62Sstigge@antcom.de 	struct max6639_data *data = i2c_get_clientdata(client);
296a5b79d62Sstigge@antcom.de 	struct sensor_device_attribute *attr = to_sensor_dev_attr(dev_attr);
297a5b79d62Sstigge@antcom.de 
298a5b79d62Sstigge@antcom.de 	return sprintf(buf, "%d\n", data->pwm[attr->index] * 255 / 120);
299a5b79d62Sstigge@antcom.de }
300a5b79d62Sstigge@antcom.de 
301a5b79d62Sstigge@antcom.de static ssize_t set_pwm(struct device *dev,
302a5b79d62Sstigge@antcom.de 		       struct device_attribute *dev_attr,
303a5b79d62Sstigge@antcom.de 		       const char *buf, size_t count)
304a5b79d62Sstigge@antcom.de {
305a5b79d62Sstigge@antcom.de 	struct i2c_client *client = to_i2c_client(dev);
306a5b79d62Sstigge@antcom.de 	struct max6639_data *data = i2c_get_clientdata(client);
307a5b79d62Sstigge@antcom.de 	struct sensor_device_attribute *attr = to_sensor_dev_attr(dev_attr);
308a5b79d62Sstigge@antcom.de 	unsigned long val;
309a5b79d62Sstigge@antcom.de 	int res;
310a5b79d62Sstigge@antcom.de 
311179c4fdbSFrans Meulenbroeks 	res = kstrtoul(buf, 10, &val);
312a5b79d62Sstigge@antcom.de 	if (res)
313a5b79d62Sstigge@antcom.de 		return res;
314a5b79d62Sstigge@antcom.de 
315*2a844c14SGuenter Roeck 	val = clamp_val(val, 0, 255);
316a5b79d62Sstigge@antcom.de 
317a5b79d62Sstigge@antcom.de 	mutex_lock(&data->update_lock);
318a5b79d62Sstigge@antcom.de 	data->pwm[attr->index] = (u8)(val * 120 / 255);
319a5b79d62Sstigge@antcom.de 	i2c_smbus_write_byte_data(client,
320a5b79d62Sstigge@antcom.de 				  MAX6639_REG_TARGTDUTY(attr->index),
321a5b79d62Sstigge@antcom.de 				  data->pwm[attr->index]);
322a5b79d62Sstigge@antcom.de 	mutex_unlock(&data->update_lock);
323a5b79d62Sstigge@antcom.de 	return count;
324a5b79d62Sstigge@antcom.de }
325a5b79d62Sstigge@antcom.de 
326a5b79d62Sstigge@antcom.de static ssize_t show_fan_input(struct device *dev,
327a5b79d62Sstigge@antcom.de 			      struct device_attribute *dev_attr, char *buf)
328a5b79d62Sstigge@antcom.de {
329a5b79d62Sstigge@antcom.de 	struct max6639_data *data = max6639_update_device(dev);
330a5b79d62Sstigge@antcom.de 	struct sensor_device_attribute *attr = to_sensor_dev_attr(dev_attr);
331a5b79d62Sstigge@antcom.de 
332a5b79d62Sstigge@antcom.de 	if (IS_ERR(data))
333a5b79d62Sstigge@antcom.de 		return PTR_ERR(data);
334a5b79d62Sstigge@antcom.de 
335a5b79d62Sstigge@antcom.de 	return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[attr->index],
336b63d97a3SChris D Schimp 		       data->rpm_range));
337a5b79d62Sstigge@antcom.de }
338a5b79d62Sstigge@antcom.de 
339a5b79d62Sstigge@antcom.de static ssize_t show_alarm(struct device *dev,
340a5b79d62Sstigge@antcom.de 			  struct device_attribute *dev_attr, char *buf)
341a5b79d62Sstigge@antcom.de {
342a5b79d62Sstigge@antcom.de 	struct max6639_data *data = max6639_update_device(dev);
343a5b79d62Sstigge@antcom.de 	struct sensor_device_attribute *attr = to_sensor_dev_attr(dev_attr);
344a5b79d62Sstigge@antcom.de 
345a5b79d62Sstigge@antcom.de 	if (IS_ERR(data))
346a5b79d62Sstigge@antcom.de 		return PTR_ERR(data);
347a5b79d62Sstigge@antcom.de 
348a5b79d62Sstigge@antcom.de 	return sprintf(buf, "%d\n", !!(data->status & (1 << attr->index)));
349a5b79d62Sstigge@antcom.de }
350a5b79d62Sstigge@antcom.de 
351a5b79d62Sstigge@antcom.de static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp_input, NULL, 0);
352a5b79d62Sstigge@antcom.de static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp_input, NULL, 1);
353a5b79d62Sstigge@antcom.de static SENSOR_DEVICE_ATTR(temp1_fault, S_IRUGO, show_temp_fault, NULL, 0);
354a5b79d62Sstigge@antcom.de static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO, show_temp_fault, NULL, 1);
355a5b79d62Sstigge@antcom.de static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp_max,
356a5b79d62Sstigge@antcom.de 		set_temp_max, 0);
357a5b79d62Sstigge@antcom.de static SENSOR_DEVICE_ATTR(temp2_max, S_IWUSR | S_IRUGO, show_temp_max,
358a5b79d62Sstigge@antcom.de 		set_temp_max, 1);
359a5b79d62Sstigge@antcom.de static SENSOR_DEVICE_ATTR(temp1_crit, S_IWUSR | S_IRUGO, show_temp_crit,
360a5b79d62Sstigge@antcom.de 		set_temp_crit, 0);
361a5b79d62Sstigge@antcom.de static SENSOR_DEVICE_ATTR(temp2_crit, S_IWUSR | S_IRUGO, show_temp_crit,
362a5b79d62Sstigge@antcom.de 		set_temp_crit, 1);
363a5b79d62Sstigge@antcom.de static SENSOR_DEVICE_ATTR(temp1_emergency, S_IWUSR | S_IRUGO,
364a5b79d62Sstigge@antcom.de 		show_temp_emergency, set_temp_emergency, 0);
365a5b79d62Sstigge@antcom.de static SENSOR_DEVICE_ATTR(temp2_emergency, S_IWUSR | S_IRUGO,
366a5b79d62Sstigge@antcom.de 		show_temp_emergency, set_temp_emergency, 1);
367a5b79d62Sstigge@antcom.de static SENSOR_DEVICE_ATTR(pwm1, S_IWUSR | S_IRUGO, show_pwm, set_pwm, 0);
368a5b79d62Sstigge@antcom.de static SENSOR_DEVICE_ATTR(pwm2, S_IWUSR | S_IRUGO, show_pwm, set_pwm, 1);
369a5b79d62Sstigge@antcom.de static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, show_fan_input, NULL, 0);
370a5b79d62Sstigge@antcom.de static SENSOR_DEVICE_ATTR(fan2_input, S_IRUGO, show_fan_input, NULL, 1);
371a5b79d62Sstigge@antcom.de static SENSOR_DEVICE_ATTR(fan1_fault, S_IRUGO, show_alarm, NULL, 1);
372a5b79d62Sstigge@antcom.de static SENSOR_DEVICE_ATTR(fan2_fault, S_IRUGO, show_alarm, NULL, 0);
373a5b79d62Sstigge@antcom.de static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, show_alarm, NULL, 3);
374a5b79d62Sstigge@antcom.de static SENSOR_DEVICE_ATTR(temp2_max_alarm, S_IRUGO, show_alarm, NULL, 2);
375a5b79d62Sstigge@antcom.de static SENSOR_DEVICE_ATTR(temp1_crit_alarm, S_IRUGO, show_alarm, NULL, 7);
376a5b79d62Sstigge@antcom.de static SENSOR_DEVICE_ATTR(temp2_crit_alarm, S_IRUGO, show_alarm, NULL, 6);
377a5b79d62Sstigge@antcom.de static SENSOR_DEVICE_ATTR(temp1_emergency_alarm, S_IRUGO, show_alarm, NULL, 5);
378a5b79d62Sstigge@antcom.de static SENSOR_DEVICE_ATTR(temp2_emergency_alarm, S_IRUGO, show_alarm, NULL, 4);
379a5b79d62Sstigge@antcom.de 
380a5b79d62Sstigge@antcom.de 
381a5b79d62Sstigge@antcom.de static struct attribute *max6639_attributes[] = {
382a5b79d62Sstigge@antcom.de 	&sensor_dev_attr_temp1_input.dev_attr.attr,
383a5b79d62Sstigge@antcom.de 	&sensor_dev_attr_temp2_input.dev_attr.attr,
384a5b79d62Sstigge@antcom.de 	&sensor_dev_attr_temp1_fault.dev_attr.attr,
385a5b79d62Sstigge@antcom.de 	&sensor_dev_attr_temp2_fault.dev_attr.attr,
386a5b79d62Sstigge@antcom.de 	&sensor_dev_attr_temp1_max.dev_attr.attr,
387a5b79d62Sstigge@antcom.de 	&sensor_dev_attr_temp2_max.dev_attr.attr,
388a5b79d62Sstigge@antcom.de 	&sensor_dev_attr_temp1_crit.dev_attr.attr,
389a5b79d62Sstigge@antcom.de 	&sensor_dev_attr_temp2_crit.dev_attr.attr,
390a5b79d62Sstigge@antcom.de 	&sensor_dev_attr_temp1_emergency.dev_attr.attr,
391a5b79d62Sstigge@antcom.de 	&sensor_dev_attr_temp2_emergency.dev_attr.attr,
392a5b79d62Sstigge@antcom.de 	&sensor_dev_attr_pwm1.dev_attr.attr,
393a5b79d62Sstigge@antcom.de 	&sensor_dev_attr_pwm2.dev_attr.attr,
394a5b79d62Sstigge@antcom.de 	&sensor_dev_attr_fan1_input.dev_attr.attr,
395a5b79d62Sstigge@antcom.de 	&sensor_dev_attr_fan2_input.dev_attr.attr,
396a5b79d62Sstigge@antcom.de 	&sensor_dev_attr_fan1_fault.dev_attr.attr,
397a5b79d62Sstigge@antcom.de 	&sensor_dev_attr_fan2_fault.dev_attr.attr,
398a5b79d62Sstigge@antcom.de 	&sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
399a5b79d62Sstigge@antcom.de 	&sensor_dev_attr_temp2_max_alarm.dev_attr.attr,
400a5b79d62Sstigge@antcom.de 	&sensor_dev_attr_temp1_crit_alarm.dev_attr.attr,
401a5b79d62Sstigge@antcom.de 	&sensor_dev_attr_temp2_crit_alarm.dev_attr.attr,
402a5b79d62Sstigge@antcom.de 	&sensor_dev_attr_temp1_emergency_alarm.dev_attr.attr,
403a5b79d62Sstigge@antcom.de 	&sensor_dev_attr_temp2_emergency_alarm.dev_attr.attr,
404a5b79d62Sstigge@antcom.de 	NULL
405a5b79d62Sstigge@antcom.de };
406a5b79d62Sstigge@antcom.de 
407a5b79d62Sstigge@antcom.de static const struct attribute_group max6639_group = {
408a5b79d62Sstigge@antcom.de 	.attrs = max6639_attributes,
409a5b79d62Sstigge@antcom.de };
410a5b79d62Sstigge@antcom.de 
411a5b79d62Sstigge@antcom.de /*
412a5b79d62Sstigge@antcom.de  *  returns respective index in rpm_ranges table
413a5b79d62Sstigge@antcom.de  *  1 by default on invalid range
414a5b79d62Sstigge@antcom.de  */
415a5b79d62Sstigge@antcom.de static int rpm_range_to_reg(int range)
416a5b79d62Sstigge@antcom.de {
417a5b79d62Sstigge@antcom.de 	int i;
418a5b79d62Sstigge@antcom.de 
419a5b79d62Sstigge@antcom.de 	for (i = 0; i < ARRAY_SIZE(rpm_ranges); i++) {
420a5b79d62Sstigge@antcom.de 		if (rpm_ranges[i] == range)
421a5b79d62Sstigge@antcom.de 			return i;
422a5b79d62Sstigge@antcom.de 	}
423a5b79d62Sstigge@antcom.de 
424a5b79d62Sstigge@antcom.de 	return 1; /* default: 4000 RPM */
425a5b79d62Sstigge@antcom.de }
426a5b79d62Sstigge@antcom.de 
427a5b79d62Sstigge@antcom.de static int max6639_init_client(struct i2c_client *client)
428a5b79d62Sstigge@antcom.de {
429a5b79d62Sstigge@antcom.de 	struct max6639_data *data = i2c_get_clientdata(client);
430a5b79d62Sstigge@antcom.de 	struct max6639_platform_data *max6639_info =
431a5b79d62Sstigge@antcom.de 		client->dev.platform_data;
4322f2da1acSChris D Schimp 	int i;
433a5b79d62Sstigge@antcom.de 	int rpm_range = 1; /* default: 4000 RPM */
4342f2da1acSChris D Schimp 	int err;
435a5b79d62Sstigge@antcom.de 
436177f3b92Sstigge@antcom.de 	/* Reset chip to default values, see below for GCONFIG setup */
437a5b79d62Sstigge@antcom.de 	err = i2c_smbus_write_byte_data(client, MAX6639_REG_GCONFIG,
438a5b79d62Sstigge@antcom.de 				  MAX6639_GCONFIG_POR);
439a5b79d62Sstigge@antcom.de 	if (err)
440a5b79d62Sstigge@antcom.de 		goto exit;
441a5b79d62Sstigge@antcom.de 
442a5b79d62Sstigge@antcom.de 	/* Fans pulse per revolution is 2 by default */
443a5b79d62Sstigge@antcom.de 	if (max6639_info && max6639_info->ppr > 0 &&
444a5b79d62Sstigge@antcom.de 			max6639_info->ppr < 5)
445a5b79d62Sstigge@antcom.de 		data->ppr = max6639_info->ppr;
446a5b79d62Sstigge@antcom.de 	else
447a5b79d62Sstigge@antcom.de 		data->ppr = 2;
448a5b79d62Sstigge@antcom.de 	data->ppr -= 1;
449a5b79d62Sstigge@antcom.de 
450a5b79d62Sstigge@antcom.de 	if (max6639_info)
451a5b79d62Sstigge@antcom.de 		rpm_range = rpm_range_to_reg(max6639_info->rpm_range);
452a5b79d62Sstigge@antcom.de 	data->rpm_range = rpm_range;
453a5b79d62Sstigge@antcom.de 
454a5b79d62Sstigge@antcom.de 	for (i = 0; i < 2; i++) {
455a5b79d62Sstigge@antcom.de 
4562f2da1acSChris D Schimp 		/* Set Fan pulse per revolution */
4572f2da1acSChris D Schimp 		err = i2c_smbus_write_byte_data(client,
4582f2da1acSChris D Schimp 				MAX6639_REG_FAN_PPR(i),
4592f2da1acSChris D Schimp 				data->ppr << 6);
4602f2da1acSChris D Schimp 		if (err)
4612f2da1acSChris D Schimp 			goto exit;
4622f2da1acSChris D Schimp 
463a5b79d62Sstigge@antcom.de 		/* Fans config PWM, RPM */
464a5b79d62Sstigge@antcom.de 		err = i2c_smbus_write_byte_data(client,
465a5b79d62Sstigge@antcom.de 			MAX6639_REG_FAN_CONFIG1(i),
466a5b79d62Sstigge@antcom.de 			MAX6639_FAN_CONFIG1_PWM | rpm_range);
467a5b79d62Sstigge@antcom.de 		if (err)
468a5b79d62Sstigge@antcom.de 			goto exit;
469a5b79d62Sstigge@antcom.de 
470a5b79d62Sstigge@antcom.de 		/* Fans PWM polarity high by default */
471a5b79d62Sstigge@antcom.de 		if (max6639_info && max6639_info->pwm_polarity == 0)
472a5b79d62Sstigge@antcom.de 			err = i2c_smbus_write_byte_data(client,
473a5b79d62Sstigge@antcom.de 				MAX6639_REG_FAN_CONFIG2a(i), 0x00);
474a5b79d62Sstigge@antcom.de 		else
475a5b79d62Sstigge@antcom.de 			err = i2c_smbus_write_byte_data(client,
476a5b79d62Sstigge@antcom.de 				MAX6639_REG_FAN_CONFIG2a(i), 0x02);
477a5b79d62Sstigge@antcom.de 		if (err)
478a5b79d62Sstigge@antcom.de 			goto exit;
479a5b79d62Sstigge@antcom.de 
480177f3b92Sstigge@antcom.de 		/*
481177f3b92Sstigge@antcom.de 		 * /THERM full speed enable,
482177f3b92Sstigge@antcom.de 		 * PWM frequency 25kHz, see also GCONFIG below
483177f3b92Sstigge@antcom.de 		 */
484177f3b92Sstigge@antcom.de 		err = i2c_smbus_write_byte_data(client,
485177f3b92Sstigge@antcom.de 			MAX6639_REG_FAN_CONFIG3(i),
486177f3b92Sstigge@antcom.de 			MAX6639_FAN_CONFIG3_THERM_FULL_SPEED | 0x03);
487177f3b92Sstigge@antcom.de 		if (err)
488177f3b92Sstigge@antcom.de 			goto exit;
489177f3b92Sstigge@antcom.de 
490a5b79d62Sstigge@antcom.de 		/* Max. temp. 80C/90C/100C */
491a5b79d62Sstigge@antcom.de 		data->temp_therm[i] = 80;
492a5b79d62Sstigge@antcom.de 		data->temp_alert[i] = 90;
493a5b79d62Sstigge@antcom.de 		data->temp_ot[i] = 100;
494a5b79d62Sstigge@antcom.de 		err = i2c_smbus_write_byte_data(client,
495a5b79d62Sstigge@antcom.de 				MAX6639_REG_THERM_LIMIT(i),
496a5b79d62Sstigge@antcom.de 				data->temp_therm[i]);
497a5b79d62Sstigge@antcom.de 		if (err)
498a5b79d62Sstigge@antcom.de 			goto exit;
499a5b79d62Sstigge@antcom.de 		err = i2c_smbus_write_byte_data(client,
500a5b79d62Sstigge@antcom.de 				MAX6639_REG_ALERT_LIMIT(i),
501a5b79d62Sstigge@antcom.de 				data->temp_alert[i]);
502a5b79d62Sstigge@antcom.de 		if (err)
503a5b79d62Sstigge@antcom.de 			goto exit;
504a5b79d62Sstigge@antcom.de 		err = i2c_smbus_write_byte_data(client,
505a5b79d62Sstigge@antcom.de 				MAX6639_REG_OT_LIMIT(i), data->temp_ot[i]);
506a5b79d62Sstigge@antcom.de 		if (err)
507a5b79d62Sstigge@antcom.de 			goto exit;
508a5b79d62Sstigge@antcom.de 
509a5b79d62Sstigge@antcom.de 		/* PWM 120/120 (i.e. 100%) */
510a5b79d62Sstigge@antcom.de 		data->pwm[i] = 120;
511a5b79d62Sstigge@antcom.de 		err = i2c_smbus_write_byte_data(client,
512a5b79d62Sstigge@antcom.de 				MAX6639_REG_TARGTDUTY(i), data->pwm[i]);
513a5b79d62Sstigge@antcom.de 		if (err)
514a5b79d62Sstigge@antcom.de 			goto exit;
515a5b79d62Sstigge@antcom.de 	}
516a5b79d62Sstigge@antcom.de 	/* Start monitoring */
517a5b79d62Sstigge@antcom.de 	err = i2c_smbus_write_byte_data(client, MAX6639_REG_GCONFIG,
518177f3b92Sstigge@antcom.de 		MAX6639_GCONFIG_DISABLE_TIMEOUT | MAX6639_GCONFIG_CH2_LOCAL |
519177f3b92Sstigge@antcom.de 		MAX6639_GCONFIG_PWM_FREQ_HI);
520a5b79d62Sstigge@antcom.de exit:
521a5b79d62Sstigge@antcom.de 	return err;
522a5b79d62Sstigge@antcom.de }
523a5b79d62Sstigge@antcom.de 
524a5b79d62Sstigge@antcom.de /* Return 0 if detection is successful, -ENODEV otherwise */
525a5b79d62Sstigge@antcom.de static int max6639_detect(struct i2c_client *client,
526a5b79d62Sstigge@antcom.de 			  struct i2c_board_info *info)
527a5b79d62Sstigge@antcom.de {
528a5b79d62Sstigge@antcom.de 	struct i2c_adapter *adapter = client->adapter;
529a5b79d62Sstigge@antcom.de 	int dev_id, manu_id;
530a5b79d62Sstigge@antcom.de 
531a5b79d62Sstigge@antcom.de 	if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
532a5b79d62Sstigge@antcom.de 		return -ENODEV;
533a5b79d62Sstigge@antcom.de 
534a5b79d62Sstigge@antcom.de 	/* Actual detection via device and manufacturer ID */
535a5b79d62Sstigge@antcom.de 	dev_id = i2c_smbus_read_byte_data(client, MAX6639_REG_DEVID);
536a5b79d62Sstigge@antcom.de 	manu_id = i2c_smbus_read_byte_data(client, MAX6639_REG_MANUID);
537a5b79d62Sstigge@antcom.de 	if (dev_id != 0x58 || manu_id != 0x4D)
538a5b79d62Sstigge@antcom.de 		return -ENODEV;
539a5b79d62Sstigge@antcom.de 
540a5b79d62Sstigge@antcom.de 	strlcpy(info->type, "max6639", I2C_NAME_SIZE);
541a5b79d62Sstigge@antcom.de 
542a5b79d62Sstigge@antcom.de 	return 0;
543a5b79d62Sstigge@antcom.de }
544a5b79d62Sstigge@antcom.de 
545a5b79d62Sstigge@antcom.de static int max6639_probe(struct i2c_client *client,
546a5b79d62Sstigge@antcom.de 			 const struct i2c_device_id *id)
547a5b79d62Sstigge@antcom.de {
548a5b79d62Sstigge@antcom.de 	struct max6639_data *data;
549a5b79d62Sstigge@antcom.de 	int err;
550a5b79d62Sstigge@antcom.de 
551b07405fbSGuenter Roeck 	data = devm_kzalloc(&client->dev, sizeof(struct max6639_data),
552b07405fbSGuenter Roeck 			    GFP_KERNEL);
553b07405fbSGuenter Roeck 	if (!data)
554b07405fbSGuenter Roeck 		return -ENOMEM;
555a5b79d62Sstigge@antcom.de 
556a5b79d62Sstigge@antcom.de 	i2c_set_clientdata(client, data);
557a5b79d62Sstigge@antcom.de 	mutex_init(&data->update_lock);
558a5b79d62Sstigge@antcom.de 
559a5b79d62Sstigge@antcom.de 	/* Initialize the max6639 chip */
560a5b79d62Sstigge@antcom.de 	err = max6639_init_client(client);
561a5b79d62Sstigge@antcom.de 	if (err < 0)
562b07405fbSGuenter Roeck 		return err;
563a5b79d62Sstigge@antcom.de 
564a5b79d62Sstigge@antcom.de 	/* Register sysfs hooks */
565a5b79d62Sstigge@antcom.de 	err = sysfs_create_group(&client->dev.kobj, &max6639_group);
566a5b79d62Sstigge@antcom.de 	if (err)
567b07405fbSGuenter Roeck 		return err;
568a5b79d62Sstigge@antcom.de 
569a5b79d62Sstigge@antcom.de 	data->hwmon_dev = hwmon_device_register(&client->dev);
570a5b79d62Sstigge@antcom.de 	if (IS_ERR(data->hwmon_dev)) {
571a5b79d62Sstigge@antcom.de 		err = PTR_ERR(data->hwmon_dev);
572a5b79d62Sstigge@antcom.de 		goto error_remove;
573a5b79d62Sstigge@antcom.de 	}
574a5b79d62Sstigge@antcom.de 
575a5b79d62Sstigge@antcom.de 	dev_info(&client->dev, "temperature sensor and fan control found\n");
576a5b79d62Sstigge@antcom.de 
577a5b79d62Sstigge@antcom.de 	return 0;
578a5b79d62Sstigge@antcom.de 
579a5b79d62Sstigge@antcom.de error_remove:
580a5b79d62Sstigge@antcom.de 	sysfs_remove_group(&client->dev.kobj, &max6639_group);
581a5b79d62Sstigge@antcom.de 	return err;
582a5b79d62Sstigge@antcom.de }
583a5b79d62Sstigge@antcom.de 
584a5b79d62Sstigge@antcom.de static int max6639_remove(struct i2c_client *client)
585a5b79d62Sstigge@antcom.de {
586a5b79d62Sstigge@antcom.de 	struct max6639_data *data = i2c_get_clientdata(client);
587a5b79d62Sstigge@antcom.de 
588a5b79d62Sstigge@antcom.de 	hwmon_device_unregister(data->hwmon_dev);
589a5b79d62Sstigge@antcom.de 	sysfs_remove_group(&client->dev.kobj, &max6639_group);
590a5b79d62Sstigge@antcom.de 
591a5b79d62Sstigge@antcom.de 	return 0;
592a5b79d62Sstigge@antcom.de }
593a5b79d62Sstigge@antcom.de 
59452f30f77SMark Brown #ifdef CONFIG_PM_SLEEP
59552f30f77SMark Brown static int max6639_suspend(struct device *dev)
596a5b79d62Sstigge@antcom.de {
59752f30f77SMark Brown 	struct i2c_client *client = to_i2c_client(dev);
598a5b79d62Sstigge@antcom.de 	int data = i2c_smbus_read_byte_data(client, MAX6639_REG_GCONFIG);
599a5b79d62Sstigge@antcom.de 	if (data < 0)
600a5b79d62Sstigge@antcom.de 		return data;
601a5b79d62Sstigge@antcom.de 
602a5b79d62Sstigge@antcom.de 	return i2c_smbus_write_byte_data(client,
603a5b79d62Sstigge@antcom.de 			MAX6639_REG_GCONFIG, data | MAX6639_GCONFIG_STANDBY);
604a5b79d62Sstigge@antcom.de }
605a5b79d62Sstigge@antcom.de 
60652f30f77SMark Brown static int max6639_resume(struct device *dev)
607a5b79d62Sstigge@antcom.de {
60852f30f77SMark Brown 	struct i2c_client *client = to_i2c_client(dev);
609a5b79d62Sstigge@antcom.de 	int data = i2c_smbus_read_byte_data(client, MAX6639_REG_GCONFIG);
610a5b79d62Sstigge@antcom.de 	if (data < 0)
611a5b79d62Sstigge@antcom.de 		return data;
612a5b79d62Sstigge@antcom.de 
613a5b79d62Sstigge@antcom.de 	return i2c_smbus_write_byte_data(client,
614a5b79d62Sstigge@antcom.de 			MAX6639_REG_GCONFIG, data & ~MAX6639_GCONFIG_STANDBY);
615a5b79d62Sstigge@antcom.de }
61652f30f77SMark Brown #endif /* CONFIG_PM_SLEEP */
617a5b79d62Sstigge@antcom.de 
618a5b79d62Sstigge@antcom.de static const struct i2c_device_id max6639_id[] = {
619a5b79d62Sstigge@antcom.de 	{"max6639", 0},
620a5b79d62Sstigge@antcom.de 	{ }
621a5b79d62Sstigge@antcom.de };
622a5b79d62Sstigge@antcom.de 
623a5b79d62Sstigge@antcom.de MODULE_DEVICE_TABLE(i2c, max6639_id);
624a5b79d62Sstigge@antcom.de 
62552f30f77SMark Brown static const struct dev_pm_ops max6639_pm_ops = {
62652f30f77SMark Brown 	SET_SYSTEM_SLEEP_PM_OPS(max6639_suspend, max6639_resume)
62752f30f77SMark Brown };
62852f30f77SMark Brown 
629a5b79d62Sstigge@antcom.de static struct i2c_driver max6639_driver = {
630a5b79d62Sstigge@antcom.de 	.class = I2C_CLASS_HWMON,
631a5b79d62Sstigge@antcom.de 	.driver = {
632a5b79d62Sstigge@antcom.de 		   .name = "max6639",
63352f30f77SMark Brown 		   .pm = &max6639_pm_ops,
634a5b79d62Sstigge@antcom.de 		   },
635a5b79d62Sstigge@antcom.de 	.probe = max6639_probe,
636a5b79d62Sstigge@antcom.de 	.remove = max6639_remove,
637a5b79d62Sstigge@antcom.de 	.id_table = max6639_id,
638a5b79d62Sstigge@antcom.de 	.detect = max6639_detect,
639a5b79d62Sstigge@antcom.de 	.address_list = normal_i2c,
640a5b79d62Sstigge@antcom.de };
641a5b79d62Sstigge@antcom.de 
642f0967eeaSAxel Lin module_i2c_driver(max6639_driver);
643a5b79d62Sstigge@antcom.de 
644a5b79d62Sstigge@antcom.de MODULE_AUTHOR("Roland Stigge <stigge@antcom.de>");
645a5b79d62Sstigge@antcom.de MODULE_DESCRIPTION("max6639 driver");
646a5b79d62Sstigge@antcom.de MODULE_LICENSE("GPL");
647