xref: /linux/drivers/hwmon/max6639.c (revision 1975d167869ef03102771d6267582623d6e07058)
174ba9207SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2a5b79d62Sstigge@antcom.de /*
3a5b79d62Sstigge@antcom.de  * max6639.c - Support for Maxim MAX6639
4a5b79d62Sstigge@antcom.de  *
5a5b79d62Sstigge@antcom.de  * 2-Channel Temperature Monitor with Dual PWM Fan-Speed Controller
6a5b79d62Sstigge@antcom.de  *
7a5b79d62Sstigge@antcom.de  * Copyright (C) 2010, 2011 Roland Stigge <stigge@antcom.de>
8a5b79d62Sstigge@antcom.de  *
9a5b79d62Sstigge@antcom.de  * based on the initial MAX6639 support from semptian.net
10a5b79d62Sstigge@antcom.de  * by He Changqing <hechangqing@semptian.com>
11a5b79d62Sstigge@antcom.de  */
12a5b79d62Sstigge@antcom.de 
13a5b79d62Sstigge@antcom.de #include <linux/module.h>
14a5b79d62Sstigge@antcom.de #include <linux/init.h>
15a5b79d62Sstigge@antcom.de #include <linux/slab.h>
16a5b79d62Sstigge@antcom.de #include <linux/jiffies.h>
17a5b79d62Sstigge@antcom.de #include <linux/i2c.h>
18a5b79d62Sstigge@antcom.de #include <linux/hwmon.h>
19a5b79d62Sstigge@antcom.de #include <linux/hwmon-sysfs.h>
20a5b79d62Sstigge@antcom.de #include <linux/err.h>
21a5b79d62Sstigge@antcom.de #include <linux/mutex.h>
220c9fe161SWolfram Sang #include <linux/platform_data/max6639.h>
23a5b79d62Sstigge@antcom.de 
24a5b79d62Sstigge@antcom.de /* Addresses to scan */
257edc8cc1SAxel Lin static const unsigned short normal_i2c[] = { 0x2c, 0x2e, 0x2f, I2C_CLIENT_END };
26a5b79d62Sstigge@antcom.de 
27a5b79d62Sstigge@antcom.de /* The MAX6639 registers, valid channel numbers: 0, 1 */
28a5b79d62Sstigge@antcom.de #define MAX6639_REG_TEMP(ch)			(0x00 + (ch))
29a5b79d62Sstigge@antcom.de #define MAX6639_REG_STATUS			0x02
30a5b79d62Sstigge@antcom.de #define MAX6639_REG_OUTPUT_MASK			0x03
31a5b79d62Sstigge@antcom.de #define MAX6639_REG_GCONFIG			0x04
32a5b79d62Sstigge@antcom.de #define MAX6639_REG_TEMP_EXT(ch)		(0x05 + (ch))
33a5b79d62Sstigge@antcom.de #define MAX6639_REG_ALERT_LIMIT(ch)		(0x08 + (ch))
34a5b79d62Sstigge@antcom.de #define MAX6639_REG_OT_LIMIT(ch)		(0x0A + (ch))
35a5b79d62Sstigge@antcom.de #define MAX6639_REG_THERM_LIMIT(ch)		(0x0C + (ch))
36a5b79d62Sstigge@antcom.de #define MAX6639_REG_FAN_CONFIG1(ch)		(0x10 + (ch) * 4)
37a5b79d62Sstigge@antcom.de #define MAX6639_REG_FAN_CONFIG2a(ch)		(0x11 + (ch) * 4)
38a5b79d62Sstigge@antcom.de #define MAX6639_REG_FAN_CONFIG2b(ch)		(0x12 + (ch) * 4)
39a5b79d62Sstigge@antcom.de #define MAX6639_REG_FAN_CONFIG3(ch)		(0x13 + (ch) * 4)
40a5b79d62Sstigge@antcom.de #define MAX6639_REG_FAN_CNT(ch)			(0x20 + (ch))
41a5b79d62Sstigge@antcom.de #define MAX6639_REG_TARGET_CNT(ch)		(0x22 + (ch))
42a5b79d62Sstigge@antcom.de #define MAX6639_REG_FAN_PPR(ch)			(0x24 + (ch))
43a5b79d62Sstigge@antcom.de #define MAX6639_REG_TARGTDUTY(ch)		(0x26 + (ch))
44a5b79d62Sstigge@antcom.de #define MAX6639_REG_FAN_START_TEMP(ch)		(0x28 + (ch))
45a5b79d62Sstigge@antcom.de #define MAX6639_REG_DEVID			0x3D
46a5b79d62Sstigge@antcom.de #define MAX6639_REG_MANUID			0x3E
47a5b79d62Sstigge@antcom.de #define MAX6639_REG_DEVREV			0x3F
48a5b79d62Sstigge@antcom.de 
49a5b79d62Sstigge@antcom.de /* Register bits */
50a5b79d62Sstigge@antcom.de #define MAX6639_GCONFIG_STANDBY			0x80
51a5b79d62Sstigge@antcom.de #define MAX6639_GCONFIG_POR			0x40
52a5b79d62Sstigge@antcom.de #define MAX6639_GCONFIG_DISABLE_TIMEOUT		0x20
53a5b79d62Sstigge@antcom.de #define MAX6639_GCONFIG_CH2_LOCAL		0x10
54177f3b92Sstigge@antcom.de #define MAX6639_GCONFIG_PWM_FREQ_HI		0x08
55a5b79d62Sstigge@antcom.de 
56a5b79d62Sstigge@antcom.de #define MAX6639_FAN_CONFIG1_PWM			0x80
57a5b79d62Sstigge@antcom.de 
58177f3b92Sstigge@antcom.de #define MAX6639_FAN_CONFIG3_THERM_FULL_SPEED	0x40
59177f3b92Sstigge@antcom.de 
60a5b79d62Sstigge@antcom.de static const int rpm_ranges[] = { 2000, 4000, 8000, 16000 };
61a5b79d62Sstigge@antcom.de 
62b63d97a3SChris D Schimp #define FAN_FROM_REG(val, rpm_range)	((val) == 0 || (val) == 255 ? \
63b63d97a3SChris D Schimp 				0 : (rpm_ranges[rpm_range] * 30) / (val))
642a844c14SGuenter Roeck #define TEMP_LIMIT_TO_REG(val)	clamp_val((val) / 1000, 0, 255)
65a5b79d62Sstigge@antcom.de 
66a5b79d62Sstigge@antcom.de /*
67a5b79d62Sstigge@antcom.de  * Client data (each client gets its own)
68a5b79d62Sstigge@antcom.de  */
69a5b79d62Sstigge@antcom.de struct max6639_data {
707981c584SGuenter Roeck 	struct i2c_client *client;
71a5b79d62Sstigge@antcom.de 	struct mutex update_lock;
72952a11caSPaul Fertser 	bool valid;		/* true if following fields are valid */
73a5b79d62Sstigge@antcom.de 	unsigned long last_updated;	/* In jiffies */
74a5b79d62Sstigge@antcom.de 
75a5b79d62Sstigge@antcom.de 	/* Register values sampled regularly */
76a5b79d62Sstigge@antcom.de 	u16 temp[2];		/* Temperature, in 1/8 C, 0..255 C */
77a5b79d62Sstigge@antcom.de 	bool temp_fault[2];	/* Detected temperature diode failure */
78a5b79d62Sstigge@antcom.de 	u8 fan[2];		/* Register value: TACH count for fans >=30 */
79a5b79d62Sstigge@antcom.de 	u8 status;		/* Detected channel alarms and fan failures */
80a5b79d62Sstigge@antcom.de 
81a5b79d62Sstigge@antcom.de 	/* Register values only written to */
82a5b79d62Sstigge@antcom.de 	u8 pwm[2];		/* Register value: Duty cycle 0..120 */
83a5b79d62Sstigge@antcom.de 	u8 temp_therm[2];	/* THERM Temperature, 0..255 C (->_max) */
84a5b79d62Sstigge@antcom.de 	u8 temp_alert[2];	/* ALERT Temperature, 0..255 C (->_crit) */
85a5b79d62Sstigge@antcom.de 	u8 temp_ot[2];		/* OT Temperature, 0..255 C (->_emergency) */
86a5b79d62Sstigge@antcom.de 
87a5b79d62Sstigge@antcom.de 	/* Register values initialized only once */
88a5b79d62Sstigge@antcom.de 	u8 ppr;			/* Pulses per rotation 0..3 for 1..4 ppr */
89a5b79d62Sstigge@antcom.de 	u8 rpm_range;		/* Index in above rpm_ranges table */
904e2271eaSMarcello Sylvester Bauer 
914e2271eaSMarcello Sylvester Bauer 	/* Optional regulator for FAN supply */
924e2271eaSMarcello Sylvester Bauer 	struct regulator *reg;
93a5b79d62Sstigge@antcom.de };
94a5b79d62Sstigge@antcom.de 
95a5b79d62Sstigge@antcom.de static struct max6639_data *max6639_update_device(struct device *dev)
96a5b79d62Sstigge@antcom.de {
977981c584SGuenter Roeck 	struct max6639_data *data = dev_get_drvdata(dev);
987981c584SGuenter Roeck 	struct i2c_client *client = data->client;
99a5b79d62Sstigge@antcom.de 	struct max6639_data *ret = data;
100a5b79d62Sstigge@antcom.de 	int i;
101a5b79d62Sstigge@antcom.de 	int status_reg;
102a5b79d62Sstigge@antcom.de 
103a5b79d62Sstigge@antcom.de 	mutex_lock(&data->update_lock);
104a5b79d62Sstigge@antcom.de 
105a5b79d62Sstigge@antcom.de 	if (time_after(jiffies, data->last_updated + 2 * HZ) || !data->valid) {
106a5b79d62Sstigge@antcom.de 		int res;
107a5b79d62Sstigge@antcom.de 
108a5b79d62Sstigge@antcom.de 		dev_dbg(&client->dev, "Starting max6639 update\n");
109a5b79d62Sstigge@antcom.de 
110a5b79d62Sstigge@antcom.de 		status_reg = i2c_smbus_read_byte_data(client,
111a5b79d62Sstigge@antcom.de 						      MAX6639_REG_STATUS);
112a5b79d62Sstigge@antcom.de 		if (status_reg < 0) {
113a5b79d62Sstigge@antcom.de 			ret = ERR_PTR(status_reg);
114a5b79d62Sstigge@antcom.de 			goto abort;
115a5b79d62Sstigge@antcom.de 		}
116a5b79d62Sstigge@antcom.de 
117a5b79d62Sstigge@antcom.de 		data->status = status_reg;
118a5b79d62Sstigge@antcom.de 
119a5b79d62Sstigge@antcom.de 		for (i = 0; i < 2; i++) {
120a5b79d62Sstigge@antcom.de 			res = i2c_smbus_read_byte_data(client,
121a5b79d62Sstigge@antcom.de 					MAX6639_REG_FAN_CNT(i));
122a5b79d62Sstigge@antcom.de 			if (res < 0) {
123a5b79d62Sstigge@antcom.de 				ret = ERR_PTR(res);
124a5b79d62Sstigge@antcom.de 				goto abort;
125a5b79d62Sstigge@antcom.de 			}
126a5b79d62Sstigge@antcom.de 			data->fan[i] = res;
127a5b79d62Sstigge@antcom.de 
128a5b79d62Sstigge@antcom.de 			res = i2c_smbus_read_byte_data(client,
129a5b79d62Sstigge@antcom.de 					MAX6639_REG_TEMP_EXT(i));
130a5b79d62Sstigge@antcom.de 			if (res < 0) {
131a5b79d62Sstigge@antcom.de 				ret = ERR_PTR(res);
132a5b79d62Sstigge@antcom.de 				goto abort;
133a5b79d62Sstigge@antcom.de 			}
134a5b79d62Sstigge@antcom.de 			data->temp[i] = res >> 5;
135a5b79d62Sstigge@antcom.de 			data->temp_fault[i] = res & 0x01;
136a5b79d62Sstigge@antcom.de 
137a5b79d62Sstigge@antcom.de 			res = i2c_smbus_read_byte_data(client,
138a5b79d62Sstigge@antcom.de 					MAX6639_REG_TEMP(i));
139a5b79d62Sstigge@antcom.de 			if (res < 0) {
140a5b79d62Sstigge@antcom.de 				ret = ERR_PTR(res);
141a5b79d62Sstigge@antcom.de 				goto abort;
142a5b79d62Sstigge@antcom.de 			}
143a5b79d62Sstigge@antcom.de 			data->temp[i] |= res << 3;
144a5b79d62Sstigge@antcom.de 		}
145a5b79d62Sstigge@antcom.de 
146a5b79d62Sstigge@antcom.de 		data->last_updated = jiffies;
147952a11caSPaul Fertser 		data->valid = true;
148a5b79d62Sstigge@antcom.de 	}
149a5b79d62Sstigge@antcom.de abort:
150a5b79d62Sstigge@antcom.de 	mutex_unlock(&data->update_lock);
151a5b79d62Sstigge@antcom.de 
152a5b79d62Sstigge@antcom.de 	return ret;
153a5b79d62Sstigge@antcom.de }
154a5b79d62Sstigge@antcom.de 
1550a0ab22aSGuenter Roeck static ssize_t temp_input_show(struct device *dev,
156a5b79d62Sstigge@antcom.de 			       struct device_attribute *dev_attr, char *buf)
157a5b79d62Sstigge@antcom.de {
158a5b79d62Sstigge@antcom.de 	long temp;
159a5b79d62Sstigge@antcom.de 	struct max6639_data *data = max6639_update_device(dev);
160a5b79d62Sstigge@antcom.de 	struct sensor_device_attribute *attr = to_sensor_dev_attr(dev_attr);
161a5b79d62Sstigge@antcom.de 
162a5b79d62Sstigge@antcom.de 	if (IS_ERR(data))
163a5b79d62Sstigge@antcom.de 		return PTR_ERR(data);
164a5b79d62Sstigge@antcom.de 
165a5b79d62Sstigge@antcom.de 	temp = data->temp[attr->index] * 125;
166a5b79d62Sstigge@antcom.de 	return sprintf(buf, "%ld\n", temp);
167a5b79d62Sstigge@antcom.de }
168a5b79d62Sstigge@antcom.de 
1690a0ab22aSGuenter Roeck static ssize_t temp_fault_show(struct device *dev,
170a5b79d62Sstigge@antcom.de 			       struct device_attribute *dev_attr, char *buf)
171a5b79d62Sstigge@antcom.de {
172a5b79d62Sstigge@antcom.de 	struct max6639_data *data = max6639_update_device(dev);
173a5b79d62Sstigge@antcom.de 	struct sensor_device_attribute *attr = to_sensor_dev_attr(dev_attr);
174a5b79d62Sstigge@antcom.de 
175a5b79d62Sstigge@antcom.de 	if (IS_ERR(data))
176a5b79d62Sstigge@antcom.de 		return PTR_ERR(data);
177a5b79d62Sstigge@antcom.de 
178a5b79d62Sstigge@antcom.de 	return sprintf(buf, "%d\n", data->temp_fault[attr->index]);
179a5b79d62Sstigge@antcom.de }
180a5b79d62Sstigge@antcom.de 
1810a0ab22aSGuenter Roeck static ssize_t temp_max_show(struct device *dev,
182a5b79d62Sstigge@antcom.de 			     struct device_attribute *dev_attr, char *buf)
183a5b79d62Sstigge@antcom.de {
184a5b79d62Sstigge@antcom.de 	struct sensor_device_attribute *attr = to_sensor_dev_attr(dev_attr);
1857981c584SGuenter Roeck 	struct max6639_data *data = dev_get_drvdata(dev);
186a5b79d62Sstigge@antcom.de 
187a5b79d62Sstigge@antcom.de 	return sprintf(buf, "%d\n", (data->temp_therm[attr->index] * 1000));
188a5b79d62Sstigge@antcom.de }
189a5b79d62Sstigge@antcom.de 
1900a0ab22aSGuenter Roeck static ssize_t temp_max_store(struct device *dev,
191a5b79d62Sstigge@antcom.de 			      struct device_attribute *dev_attr,
192a5b79d62Sstigge@antcom.de 			      const char *buf, size_t count)
193a5b79d62Sstigge@antcom.de {
194a5b79d62Sstigge@antcom.de 	struct sensor_device_attribute *attr = to_sensor_dev_attr(dev_attr);
1957981c584SGuenter Roeck 	struct max6639_data *data = dev_get_drvdata(dev);
1967981c584SGuenter Roeck 	struct i2c_client *client = data->client;
197a5b79d62Sstigge@antcom.de 	unsigned long val;
198a5b79d62Sstigge@antcom.de 	int res;
199a5b79d62Sstigge@antcom.de 
200179c4fdbSFrans Meulenbroeks 	res = kstrtoul(buf, 10, &val);
201a5b79d62Sstigge@antcom.de 	if (res)
202a5b79d62Sstigge@antcom.de 		return res;
203a5b79d62Sstigge@antcom.de 
204a5b79d62Sstigge@antcom.de 	mutex_lock(&data->update_lock);
205a5b79d62Sstigge@antcom.de 	data->temp_therm[attr->index] = TEMP_LIMIT_TO_REG(val);
206a5b79d62Sstigge@antcom.de 	i2c_smbus_write_byte_data(client,
207a5b79d62Sstigge@antcom.de 				  MAX6639_REG_THERM_LIMIT(attr->index),
208a5b79d62Sstigge@antcom.de 				  data->temp_therm[attr->index]);
209a5b79d62Sstigge@antcom.de 	mutex_unlock(&data->update_lock);
210a5b79d62Sstigge@antcom.de 	return count;
211a5b79d62Sstigge@antcom.de }
212a5b79d62Sstigge@antcom.de 
2130a0ab22aSGuenter Roeck static ssize_t temp_crit_show(struct device *dev,
214a5b79d62Sstigge@antcom.de 			      struct device_attribute *dev_attr, char *buf)
215a5b79d62Sstigge@antcom.de {
216a5b79d62Sstigge@antcom.de 	struct sensor_device_attribute *attr = to_sensor_dev_attr(dev_attr);
2177981c584SGuenter Roeck 	struct max6639_data *data = dev_get_drvdata(dev);
218a5b79d62Sstigge@antcom.de 
219a5b79d62Sstigge@antcom.de 	return sprintf(buf, "%d\n", (data->temp_alert[attr->index] * 1000));
220a5b79d62Sstigge@antcom.de }
221a5b79d62Sstigge@antcom.de 
2220a0ab22aSGuenter Roeck static ssize_t temp_crit_store(struct device *dev,
223a5b79d62Sstigge@antcom.de 			       struct device_attribute *dev_attr,
224a5b79d62Sstigge@antcom.de 			       const char *buf, size_t count)
225a5b79d62Sstigge@antcom.de {
226a5b79d62Sstigge@antcom.de 	struct sensor_device_attribute *attr = to_sensor_dev_attr(dev_attr);
2277981c584SGuenter Roeck 	struct max6639_data *data = dev_get_drvdata(dev);
2287981c584SGuenter Roeck 	struct i2c_client *client = data->client;
229a5b79d62Sstigge@antcom.de 	unsigned long val;
230a5b79d62Sstigge@antcom.de 	int res;
231a5b79d62Sstigge@antcom.de 
232179c4fdbSFrans Meulenbroeks 	res = kstrtoul(buf, 10, &val);
233a5b79d62Sstigge@antcom.de 	if (res)
234a5b79d62Sstigge@antcom.de 		return res;
235a5b79d62Sstigge@antcom.de 
236a5b79d62Sstigge@antcom.de 	mutex_lock(&data->update_lock);
237a5b79d62Sstigge@antcom.de 	data->temp_alert[attr->index] = TEMP_LIMIT_TO_REG(val);
238a5b79d62Sstigge@antcom.de 	i2c_smbus_write_byte_data(client,
239a5b79d62Sstigge@antcom.de 				  MAX6639_REG_ALERT_LIMIT(attr->index),
240a5b79d62Sstigge@antcom.de 				  data->temp_alert[attr->index]);
241a5b79d62Sstigge@antcom.de 	mutex_unlock(&data->update_lock);
242a5b79d62Sstigge@antcom.de 	return count;
243a5b79d62Sstigge@antcom.de }
244a5b79d62Sstigge@antcom.de 
2450a0ab22aSGuenter Roeck static ssize_t temp_emergency_show(struct device *dev,
246a5b79d62Sstigge@antcom.de 				   struct device_attribute *dev_attr,
247a5b79d62Sstigge@antcom.de 				   char *buf)
248a5b79d62Sstigge@antcom.de {
249a5b79d62Sstigge@antcom.de 	struct sensor_device_attribute *attr = to_sensor_dev_attr(dev_attr);
2507981c584SGuenter Roeck 	struct max6639_data *data = dev_get_drvdata(dev);
251a5b79d62Sstigge@antcom.de 
252a5b79d62Sstigge@antcom.de 	return sprintf(buf, "%d\n", (data->temp_ot[attr->index] * 1000));
253a5b79d62Sstigge@antcom.de }
254a5b79d62Sstigge@antcom.de 
2550a0ab22aSGuenter Roeck static ssize_t temp_emergency_store(struct device *dev,
256a5b79d62Sstigge@antcom.de 				    struct device_attribute *dev_attr,
257a5b79d62Sstigge@antcom.de 				    const char *buf, size_t count)
258a5b79d62Sstigge@antcom.de {
259a5b79d62Sstigge@antcom.de 	struct sensor_device_attribute *attr = to_sensor_dev_attr(dev_attr);
2607981c584SGuenter Roeck 	struct max6639_data *data = dev_get_drvdata(dev);
2617981c584SGuenter Roeck 	struct i2c_client *client = data->client;
262a5b79d62Sstigge@antcom.de 	unsigned long val;
263a5b79d62Sstigge@antcom.de 	int res;
264a5b79d62Sstigge@antcom.de 
265179c4fdbSFrans Meulenbroeks 	res = kstrtoul(buf, 10, &val);
266a5b79d62Sstigge@antcom.de 	if (res)
267a5b79d62Sstigge@antcom.de 		return res;
268a5b79d62Sstigge@antcom.de 
269a5b79d62Sstigge@antcom.de 	mutex_lock(&data->update_lock);
270a5b79d62Sstigge@antcom.de 	data->temp_ot[attr->index] = TEMP_LIMIT_TO_REG(val);
271a5b79d62Sstigge@antcom.de 	i2c_smbus_write_byte_data(client,
272a5b79d62Sstigge@antcom.de 				  MAX6639_REG_OT_LIMIT(attr->index),
273a5b79d62Sstigge@antcom.de 				  data->temp_ot[attr->index]);
274a5b79d62Sstigge@antcom.de 	mutex_unlock(&data->update_lock);
275a5b79d62Sstigge@antcom.de 	return count;
276a5b79d62Sstigge@antcom.de }
277a5b79d62Sstigge@antcom.de 
2780a0ab22aSGuenter Roeck static ssize_t pwm_show(struct device *dev, struct device_attribute *dev_attr,
2790a0ab22aSGuenter Roeck 			char *buf)
280a5b79d62Sstigge@antcom.de {
281a5b79d62Sstigge@antcom.de 	struct sensor_device_attribute *attr = to_sensor_dev_attr(dev_attr);
2827981c584SGuenter Roeck 	struct max6639_data *data = dev_get_drvdata(dev);
283a5b79d62Sstigge@antcom.de 
284a5b79d62Sstigge@antcom.de 	return sprintf(buf, "%d\n", data->pwm[attr->index] * 255 / 120);
285a5b79d62Sstigge@antcom.de }
286a5b79d62Sstigge@antcom.de 
2870a0ab22aSGuenter Roeck static ssize_t pwm_store(struct device *dev,
2880a0ab22aSGuenter Roeck 			 struct device_attribute *dev_attr, const char *buf,
2890a0ab22aSGuenter Roeck 			 size_t count)
290a5b79d62Sstigge@antcom.de {
291a5b79d62Sstigge@antcom.de 	struct sensor_device_attribute *attr = to_sensor_dev_attr(dev_attr);
2927981c584SGuenter Roeck 	struct max6639_data *data = dev_get_drvdata(dev);
2937981c584SGuenter Roeck 	struct i2c_client *client = data->client;
294a5b79d62Sstigge@antcom.de 	unsigned long val;
295a5b79d62Sstigge@antcom.de 	int res;
296a5b79d62Sstigge@antcom.de 
297179c4fdbSFrans Meulenbroeks 	res = kstrtoul(buf, 10, &val);
298a5b79d62Sstigge@antcom.de 	if (res)
299a5b79d62Sstigge@antcom.de 		return res;
300a5b79d62Sstigge@antcom.de 
3012a844c14SGuenter Roeck 	val = clamp_val(val, 0, 255);
302a5b79d62Sstigge@antcom.de 
303a5b79d62Sstigge@antcom.de 	mutex_lock(&data->update_lock);
304a5b79d62Sstigge@antcom.de 	data->pwm[attr->index] = (u8)(val * 120 / 255);
305a5b79d62Sstigge@antcom.de 	i2c_smbus_write_byte_data(client,
306a5b79d62Sstigge@antcom.de 				  MAX6639_REG_TARGTDUTY(attr->index),
307a5b79d62Sstigge@antcom.de 				  data->pwm[attr->index]);
308a5b79d62Sstigge@antcom.de 	mutex_unlock(&data->update_lock);
309a5b79d62Sstigge@antcom.de 	return count;
310a5b79d62Sstigge@antcom.de }
311a5b79d62Sstigge@antcom.de 
3120a0ab22aSGuenter Roeck static ssize_t fan_input_show(struct device *dev,
313a5b79d62Sstigge@antcom.de 			      struct device_attribute *dev_attr, char *buf)
314a5b79d62Sstigge@antcom.de {
315a5b79d62Sstigge@antcom.de 	struct max6639_data *data = max6639_update_device(dev);
316a5b79d62Sstigge@antcom.de 	struct sensor_device_attribute *attr = to_sensor_dev_attr(dev_attr);
317a5b79d62Sstigge@antcom.de 
318a5b79d62Sstigge@antcom.de 	if (IS_ERR(data))
319a5b79d62Sstigge@antcom.de 		return PTR_ERR(data);
320a5b79d62Sstigge@antcom.de 
321a5b79d62Sstigge@antcom.de 	return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[attr->index],
322b63d97a3SChris D Schimp 		       data->rpm_range));
323a5b79d62Sstigge@antcom.de }
324a5b79d62Sstigge@antcom.de 
3250a0ab22aSGuenter Roeck static ssize_t alarm_show(struct device *dev,
326a5b79d62Sstigge@antcom.de 			  struct device_attribute *dev_attr, char *buf)
327a5b79d62Sstigge@antcom.de {
328a5b79d62Sstigge@antcom.de 	struct max6639_data *data = max6639_update_device(dev);
329a5b79d62Sstigge@antcom.de 	struct sensor_device_attribute *attr = to_sensor_dev_attr(dev_attr);
330a5b79d62Sstigge@antcom.de 
331a5b79d62Sstigge@antcom.de 	if (IS_ERR(data))
332a5b79d62Sstigge@antcom.de 		return PTR_ERR(data);
333a5b79d62Sstigge@antcom.de 
334a5b79d62Sstigge@antcom.de 	return sprintf(buf, "%d\n", !!(data->status & (1 << attr->index)));
335a5b79d62Sstigge@antcom.de }
336a5b79d62Sstigge@antcom.de 
3370a0ab22aSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(temp1_input, temp_input, 0);
3380a0ab22aSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(temp2_input, temp_input, 1);
3390a0ab22aSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(temp1_fault, temp_fault, 0);
3400a0ab22aSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(temp2_fault, temp_fault, 1);
3410a0ab22aSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(temp1_max, temp_max, 0);
3420a0ab22aSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(temp2_max, temp_max, 1);
3430a0ab22aSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(temp1_crit, temp_crit, 0);
3440a0ab22aSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(temp2_crit, temp_crit, 1);
3450a0ab22aSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(temp1_emergency, temp_emergency, 0);
3460a0ab22aSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(temp2_emergency, temp_emergency, 1);
3470a0ab22aSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(pwm1, pwm, 0);
3480a0ab22aSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(pwm2, pwm, 1);
3490a0ab22aSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(fan1_input, fan_input, 0);
3500a0ab22aSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(fan2_input, fan_input, 1);
3510a0ab22aSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(fan1_fault, alarm, 1);
3520a0ab22aSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(fan2_fault, alarm, 0);
3530a0ab22aSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(temp1_max_alarm, alarm, 3);
3540a0ab22aSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(temp2_max_alarm, alarm, 2);
3550a0ab22aSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(temp1_crit_alarm, alarm, 7);
3560a0ab22aSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(temp2_crit_alarm, alarm, 6);
3570a0ab22aSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(temp1_emergency_alarm, alarm, 5);
3580a0ab22aSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(temp2_emergency_alarm, alarm, 4);
359a5b79d62Sstigge@antcom.de 
360a5b79d62Sstigge@antcom.de 
3617981c584SGuenter Roeck static struct attribute *max6639_attrs[] = {
362a5b79d62Sstigge@antcom.de 	&sensor_dev_attr_temp1_input.dev_attr.attr,
363a5b79d62Sstigge@antcom.de 	&sensor_dev_attr_temp2_input.dev_attr.attr,
364a5b79d62Sstigge@antcom.de 	&sensor_dev_attr_temp1_fault.dev_attr.attr,
365a5b79d62Sstigge@antcom.de 	&sensor_dev_attr_temp2_fault.dev_attr.attr,
366a5b79d62Sstigge@antcom.de 	&sensor_dev_attr_temp1_max.dev_attr.attr,
367a5b79d62Sstigge@antcom.de 	&sensor_dev_attr_temp2_max.dev_attr.attr,
368a5b79d62Sstigge@antcom.de 	&sensor_dev_attr_temp1_crit.dev_attr.attr,
369a5b79d62Sstigge@antcom.de 	&sensor_dev_attr_temp2_crit.dev_attr.attr,
370a5b79d62Sstigge@antcom.de 	&sensor_dev_attr_temp1_emergency.dev_attr.attr,
371a5b79d62Sstigge@antcom.de 	&sensor_dev_attr_temp2_emergency.dev_attr.attr,
372a5b79d62Sstigge@antcom.de 	&sensor_dev_attr_pwm1.dev_attr.attr,
373a5b79d62Sstigge@antcom.de 	&sensor_dev_attr_pwm2.dev_attr.attr,
374a5b79d62Sstigge@antcom.de 	&sensor_dev_attr_fan1_input.dev_attr.attr,
375a5b79d62Sstigge@antcom.de 	&sensor_dev_attr_fan2_input.dev_attr.attr,
376a5b79d62Sstigge@antcom.de 	&sensor_dev_attr_fan1_fault.dev_attr.attr,
377a5b79d62Sstigge@antcom.de 	&sensor_dev_attr_fan2_fault.dev_attr.attr,
378a5b79d62Sstigge@antcom.de 	&sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
379a5b79d62Sstigge@antcom.de 	&sensor_dev_attr_temp2_max_alarm.dev_attr.attr,
380a5b79d62Sstigge@antcom.de 	&sensor_dev_attr_temp1_crit_alarm.dev_attr.attr,
381a5b79d62Sstigge@antcom.de 	&sensor_dev_attr_temp2_crit_alarm.dev_attr.attr,
382a5b79d62Sstigge@antcom.de 	&sensor_dev_attr_temp1_emergency_alarm.dev_attr.attr,
383a5b79d62Sstigge@antcom.de 	&sensor_dev_attr_temp2_emergency_alarm.dev_attr.attr,
384a5b79d62Sstigge@antcom.de 	NULL
385a5b79d62Sstigge@antcom.de };
3867981c584SGuenter Roeck ATTRIBUTE_GROUPS(max6639);
387a5b79d62Sstigge@antcom.de 
388a5b79d62Sstigge@antcom.de /*
389a5b79d62Sstigge@antcom.de  *  returns respective index in rpm_ranges table
390a5b79d62Sstigge@antcom.de  *  1 by default on invalid range
391a5b79d62Sstigge@antcom.de  */
392a5b79d62Sstigge@antcom.de static int rpm_range_to_reg(int range)
393a5b79d62Sstigge@antcom.de {
394a5b79d62Sstigge@antcom.de 	int i;
395a5b79d62Sstigge@antcom.de 
396a5b79d62Sstigge@antcom.de 	for (i = 0; i < ARRAY_SIZE(rpm_ranges); i++) {
397a5b79d62Sstigge@antcom.de 		if (rpm_ranges[i] == range)
398a5b79d62Sstigge@antcom.de 			return i;
399a5b79d62Sstigge@antcom.de 	}
400a5b79d62Sstigge@antcom.de 
401a5b79d62Sstigge@antcom.de 	return 1; /* default: 4000 RPM */
402a5b79d62Sstigge@antcom.de }
403a5b79d62Sstigge@antcom.de 
4047981c584SGuenter Roeck static int max6639_init_client(struct i2c_client *client,
4057981c584SGuenter Roeck 			       struct max6639_data *data)
406a5b79d62Sstigge@antcom.de {
407a5b79d62Sstigge@antcom.de 	struct max6639_platform_data *max6639_info =
408a8b3a3a5SJingoo Han 		dev_get_platdata(&client->dev);
4092f2da1acSChris D Schimp 	int i;
410a5b79d62Sstigge@antcom.de 	int rpm_range = 1; /* default: 4000 RPM */
4112f2da1acSChris D Schimp 	int err;
412a5b79d62Sstigge@antcom.de 
413177f3b92Sstigge@antcom.de 	/* Reset chip to default values, see below for GCONFIG setup */
414a5b79d62Sstigge@antcom.de 	err = i2c_smbus_write_byte_data(client, MAX6639_REG_GCONFIG,
415a5b79d62Sstigge@antcom.de 				  MAX6639_GCONFIG_POR);
416a5b79d62Sstigge@antcom.de 	if (err)
417a5b79d62Sstigge@antcom.de 		goto exit;
418a5b79d62Sstigge@antcom.de 
419a5b79d62Sstigge@antcom.de 	/* Fans pulse per revolution is 2 by default */
420a5b79d62Sstigge@antcom.de 	if (max6639_info && max6639_info->ppr > 0 &&
421a5b79d62Sstigge@antcom.de 			max6639_info->ppr < 5)
422a5b79d62Sstigge@antcom.de 		data->ppr = max6639_info->ppr;
423a5b79d62Sstigge@antcom.de 	else
424a5b79d62Sstigge@antcom.de 		data->ppr = 2;
425a5b79d62Sstigge@antcom.de 	data->ppr -= 1;
426a5b79d62Sstigge@antcom.de 
427a5b79d62Sstigge@antcom.de 	if (max6639_info)
428a5b79d62Sstigge@antcom.de 		rpm_range = rpm_range_to_reg(max6639_info->rpm_range);
429a5b79d62Sstigge@antcom.de 	data->rpm_range = rpm_range;
430a5b79d62Sstigge@antcom.de 
431a5b79d62Sstigge@antcom.de 	for (i = 0; i < 2; i++) {
432a5b79d62Sstigge@antcom.de 
4332f2da1acSChris D Schimp 		/* Set Fan pulse per revolution */
4342f2da1acSChris D Schimp 		err = i2c_smbus_write_byte_data(client,
4352f2da1acSChris D Schimp 				MAX6639_REG_FAN_PPR(i),
4362f2da1acSChris D Schimp 				data->ppr << 6);
4372f2da1acSChris D Schimp 		if (err)
4382f2da1acSChris D Schimp 			goto exit;
4392f2da1acSChris D Schimp 
440a5b79d62Sstigge@antcom.de 		/* Fans config PWM, RPM */
441a5b79d62Sstigge@antcom.de 		err = i2c_smbus_write_byte_data(client,
442a5b79d62Sstigge@antcom.de 			MAX6639_REG_FAN_CONFIG1(i),
443a5b79d62Sstigge@antcom.de 			MAX6639_FAN_CONFIG1_PWM | rpm_range);
444a5b79d62Sstigge@antcom.de 		if (err)
445a5b79d62Sstigge@antcom.de 			goto exit;
446a5b79d62Sstigge@antcom.de 
447a5b79d62Sstigge@antcom.de 		/* Fans PWM polarity high by default */
448a5b79d62Sstigge@antcom.de 		if (max6639_info && max6639_info->pwm_polarity == 0)
449a5b79d62Sstigge@antcom.de 			err = i2c_smbus_write_byte_data(client,
450a5b79d62Sstigge@antcom.de 				MAX6639_REG_FAN_CONFIG2a(i), 0x00);
451a5b79d62Sstigge@antcom.de 		else
452a5b79d62Sstigge@antcom.de 			err = i2c_smbus_write_byte_data(client,
453a5b79d62Sstigge@antcom.de 				MAX6639_REG_FAN_CONFIG2a(i), 0x02);
454a5b79d62Sstigge@antcom.de 		if (err)
455a5b79d62Sstigge@antcom.de 			goto exit;
456a5b79d62Sstigge@antcom.de 
457177f3b92Sstigge@antcom.de 		/*
458177f3b92Sstigge@antcom.de 		 * /THERM full speed enable,
459177f3b92Sstigge@antcom.de 		 * PWM frequency 25kHz, see also GCONFIG below
460177f3b92Sstigge@antcom.de 		 */
461177f3b92Sstigge@antcom.de 		err = i2c_smbus_write_byte_data(client,
462177f3b92Sstigge@antcom.de 			MAX6639_REG_FAN_CONFIG3(i),
463177f3b92Sstigge@antcom.de 			MAX6639_FAN_CONFIG3_THERM_FULL_SPEED | 0x03);
464177f3b92Sstigge@antcom.de 		if (err)
465177f3b92Sstigge@antcom.de 			goto exit;
466177f3b92Sstigge@antcom.de 
467a5b79d62Sstigge@antcom.de 		/* Max. temp. 80C/90C/100C */
468a5b79d62Sstigge@antcom.de 		data->temp_therm[i] = 80;
469a5b79d62Sstigge@antcom.de 		data->temp_alert[i] = 90;
470a5b79d62Sstigge@antcom.de 		data->temp_ot[i] = 100;
471a5b79d62Sstigge@antcom.de 		err = i2c_smbus_write_byte_data(client,
472a5b79d62Sstigge@antcom.de 				MAX6639_REG_THERM_LIMIT(i),
473a5b79d62Sstigge@antcom.de 				data->temp_therm[i]);
474a5b79d62Sstigge@antcom.de 		if (err)
475a5b79d62Sstigge@antcom.de 			goto exit;
476a5b79d62Sstigge@antcom.de 		err = i2c_smbus_write_byte_data(client,
477a5b79d62Sstigge@antcom.de 				MAX6639_REG_ALERT_LIMIT(i),
478a5b79d62Sstigge@antcom.de 				data->temp_alert[i]);
479a5b79d62Sstigge@antcom.de 		if (err)
480a5b79d62Sstigge@antcom.de 			goto exit;
481a5b79d62Sstigge@antcom.de 		err = i2c_smbus_write_byte_data(client,
482a5b79d62Sstigge@antcom.de 				MAX6639_REG_OT_LIMIT(i), data->temp_ot[i]);
483a5b79d62Sstigge@antcom.de 		if (err)
484a5b79d62Sstigge@antcom.de 			goto exit;
485a5b79d62Sstigge@antcom.de 
486a5b79d62Sstigge@antcom.de 		/* PWM 120/120 (i.e. 100%) */
487a5b79d62Sstigge@antcom.de 		data->pwm[i] = 120;
488a5b79d62Sstigge@antcom.de 		err = i2c_smbus_write_byte_data(client,
489a5b79d62Sstigge@antcom.de 				MAX6639_REG_TARGTDUTY(i), data->pwm[i]);
490a5b79d62Sstigge@antcom.de 		if (err)
491a5b79d62Sstigge@antcom.de 			goto exit;
492a5b79d62Sstigge@antcom.de 	}
493a5b79d62Sstigge@antcom.de 	/* Start monitoring */
494a5b79d62Sstigge@antcom.de 	err = i2c_smbus_write_byte_data(client, MAX6639_REG_GCONFIG,
495177f3b92Sstigge@antcom.de 		MAX6639_GCONFIG_DISABLE_TIMEOUT | MAX6639_GCONFIG_CH2_LOCAL |
496177f3b92Sstigge@antcom.de 		MAX6639_GCONFIG_PWM_FREQ_HI);
497a5b79d62Sstigge@antcom.de exit:
498a5b79d62Sstigge@antcom.de 	return err;
499a5b79d62Sstigge@antcom.de }
500a5b79d62Sstigge@antcom.de 
501a5b79d62Sstigge@antcom.de /* Return 0 if detection is successful, -ENODEV otherwise */
502a5b79d62Sstigge@antcom.de static int max6639_detect(struct i2c_client *client,
503a5b79d62Sstigge@antcom.de 			  struct i2c_board_info *info)
504a5b79d62Sstigge@antcom.de {
505a5b79d62Sstigge@antcom.de 	struct i2c_adapter *adapter = client->adapter;
506a5b79d62Sstigge@antcom.de 	int dev_id, manu_id;
507a5b79d62Sstigge@antcom.de 
508a5b79d62Sstigge@antcom.de 	if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
509a5b79d62Sstigge@antcom.de 		return -ENODEV;
510a5b79d62Sstigge@antcom.de 
511a5b79d62Sstigge@antcom.de 	/* Actual detection via device and manufacturer ID */
512a5b79d62Sstigge@antcom.de 	dev_id = i2c_smbus_read_byte_data(client, MAX6639_REG_DEVID);
513a5b79d62Sstigge@antcom.de 	manu_id = i2c_smbus_read_byte_data(client, MAX6639_REG_MANUID);
514a5b79d62Sstigge@antcom.de 	if (dev_id != 0x58 || manu_id != 0x4D)
515a5b79d62Sstigge@antcom.de 		return -ENODEV;
516a5b79d62Sstigge@antcom.de 
517f2f394dbSWolfram Sang 	strscpy(info->type, "max6639", I2C_NAME_SIZE);
518a5b79d62Sstigge@antcom.de 
519a5b79d62Sstigge@antcom.de 	return 0;
520a5b79d62Sstigge@antcom.de }
521a5b79d62Sstigge@antcom.de 
5224e2271eaSMarcello Sylvester Bauer static void max6639_regulator_disable(void *data)
5234e2271eaSMarcello Sylvester Bauer {
5244e2271eaSMarcello Sylvester Bauer 	regulator_disable(data);
5254e2271eaSMarcello Sylvester Bauer }
5264e2271eaSMarcello Sylvester Bauer 
52767487038SStephen Kitt static int max6639_probe(struct i2c_client *client)
528a5b79d62Sstigge@antcom.de {
529c1ea0a04SGuenter Roeck 	struct device *dev = &client->dev;
530a5b79d62Sstigge@antcom.de 	struct max6639_data *data;
5317981c584SGuenter Roeck 	struct device *hwmon_dev;
532a5b79d62Sstigge@antcom.de 	int err;
533a5b79d62Sstigge@antcom.de 
534c1ea0a04SGuenter Roeck 	data = devm_kzalloc(dev, sizeof(struct max6639_data), GFP_KERNEL);
535b07405fbSGuenter Roeck 	if (!data)
536b07405fbSGuenter Roeck 		return -ENOMEM;
537a5b79d62Sstigge@antcom.de 
5387981c584SGuenter Roeck 	data->client = client;
5394e2271eaSMarcello Sylvester Bauer 
5404e2271eaSMarcello Sylvester Bauer 	data->reg = devm_regulator_get_optional(dev, "fan");
5414e2271eaSMarcello Sylvester Bauer 	if (IS_ERR(data->reg)) {
5424e2271eaSMarcello Sylvester Bauer 		if (PTR_ERR(data->reg) != -ENODEV)
5434e2271eaSMarcello Sylvester Bauer 			return PTR_ERR(data->reg);
5444e2271eaSMarcello Sylvester Bauer 
5454e2271eaSMarcello Sylvester Bauer 		data->reg = NULL;
5464e2271eaSMarcello Sylvester Bauer 	} else {
5474e2271eaSMarcello Sylvester Bauer 		/* Spin up fans */
5484e2271eaSMarcello Sylvester Bauer 		err = regulator_enable(data->reg);
5494e2271eaSMarcello Sylvester Bauer 		if (err) {
5504e2271eaSMarcello Sylvester Bauer 			dev_err(dev, "Failed to enable fan supply: %d\n", err);
5514e2271eaSMarcello Sylvester Bauer 			return err;
5524e2271eaSMarcello Sylvester Bauer 		}
5534e2271eaSMarcello Sylvester Bauer 		err = devm_add_action_or_reset(dev, max6639_regulator_disable,
5544e2271eaSMarcello Sylvester Bauer 					       data->reg);
5554e2271eaSMarcello Sylvester Bauer 		if (err) {
5564e2271eaSMarcello Sylvester Bauer 			dev_err(dev, "Failed to register action: %d\n", err);
5574e2271eaSMarcello Sylvester Bauer 			return err;
5584e2271eaSMarcello Sylvester Bauer 		}
5594e2271eaSMarcello Sylvester Bauer 	}
5604e2271eaSMarcello Sylvester Bauer 
561a5b79d62Sstigge@antcom.de 	mutex_init(&data->update_lock);
562a5b79d62Sstigge@antcom.de 
563a5b79d62Sstigge@antcom.de 	/* Initialize the max6639 chip */
5647981c584SGuenter Roeck 	err = max6639_init_client(client, data);
565a5b79d62Sstigge@antcom.de 	if (err < 0)
566b07405fbSGuenter Roeck 		return err;
567a5b79d62Sstigge@antcom.de 
5687981c584SGuenter Roeck 	hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
5697981c584SGuenter Roeck 							   data,
5707981c584SGuenter Roeck 							   max6639_groups);
5717981c584SGuenter Roeck 	return PTR_ERR_OR_ZERO(hwmon_dev);
572a5b79d62Sstigge@antcom.de }
573a5b79d62Sstigge@antcom.de 
57452f30f77SMark Brown static int max6639_suspend(struct device *dev)
575a5b79d62Sstigge@antcom.de {
57652f30f77SMark Brown 	struct i2c_client *client = to_i2c_client(dev);
5774e2271eaSMarcello Sylvester Bauer 	struct max6639_data *data = dev_get_drvdata(dev);
5784e2271eaSMarcello Sylvester Bauer 	int ret = i2c_smbus_read_byte_data(client, MAX6639_REG_GCONFIG);
5794e2271eaSMarcello Sylvester Bauer 
5804e2271eaSMarcello Sylvester Bauer 	if (ret < 0)
5814e2271eaSMarcello Sylvester Bauer 		return ret;
5824e2271eaSMarcello Sylvester Bauer 
5834e2271eaSMarcello Sylvester Bauer 	if (data->reg)
5844e2271eaSMarcello Sylvester Bauer 		regulator_disable(data->reg);
585a5b79d62Sstigge@antcom.de 
586a5b79d62Sstigge@antcom.de 	return i2c_smbus_write_byte_data(client,
5874e2271eaSMarcello Sylvester Bauer 			MAX6639_REG_GCONFIG, ret | MAX6639_GCONFIG_STANDBY);
588a5b79d62Sstigge@antcom.de }
589a5b79d62Sstigge@antcom.de 
59052f30f77SMark Brown static int max6639_resume(struct device *dev)
591a5b79d62Sstigge@antcom.de {
59252f30f77SMark Brown 	struct i2c_client *client = to_i2c_client(dev);
5934e2271eaSMarcello Sylvester Bauer 	struct max6639_data *data = dev_get_drvdata(dev);
5944e2271eaSMarcello Sylvester Bauer 	int ret;
5954e2271eaSMarcello Sylvester Bauer 
5964e2271eaSMarcello Sylvester Bauer 	if (data->reg) {
5974e2271eaSMarcello Sylvester Bauer 		ret = regulator_enable(data->reg);
5984e2271eaSMarcello Sylvester Bauer 		if (ret) {
5994e2271eaSMarcello Sylvester Bauer 			dev_err(dev, "Failed to enable fan supply: %d\n", ret);
6004e2271eaSMarcello Sylvester Bauer 			return ret;
6014e2271eaSMarcello Sylvester Bauer 		}
6024e2271eaSMarcello Sylvester Bauer 	}
6034e2271eaSMarcello Sylvester Bauer 
6044e2271eaSMarcello Sylvester Bauer 	ret = i2c_smbus_read_byte_data(client, MAX6639_REG_GCONFIG);
6054e2271eaSMarcello Sylvester Bauer 	if (ret < 0)
6064e2271eaSMarcello Sylvester Bauer 		return ret;
607a5b79d62Sstigge@antcom.de 
608a5b79d62Sstigge@antcom.de 	return i2c_smbus_write_byte_data(client,
6094e2271eaSMarcello Sylvester Bauer 			MAX6639_REG_GCONFIG, ret & ~MAX6639_GCONFIG_STANDBY);
610a5b79d62Sstigge@antcom.de }
611a5b79d62Sstigge@antcom.de 
612a5b79d62Sstigge@antcom.de static const struct i2c_device_id max6639_id[] = {
613a5b79d62Sstigge@antcom.de 	{"max6639", 0},
614a5b79d62Sstigge@antcom.de 	{ }
615a5b79d62Sstigge@antcom.de };
616a5b79d62Sstigge@antcom.de 
617a5b79d62Sstigge@antcom.de MODULE_DEVICE_TABLE(i2c, max6639_id);
618a5b79d62Sstigge@antcom.de 
61977563092SJonathan Cameron static DEFINE_SIMPLE_DEV_PM_OPS(max6639_pm_ops, max6639_suspend, max6639_resume);
62052f30f77SMark Brown 
621a5b79d62Sstigge@antcom.de static struct i2c_driver max6639_driver = {
622a5b79d62Sstigge@antcom.de 	.class = I2C_CLASS_HWMON,
623a5b79d62Sstigge@antcom.de 	.driver = {
624a5b79d62Sstigge@antcom.de 		   .name = "max6639",
62577563092SJonathan Cameron 		   .pm = pm_sleep_ptr(&max6639_pm_ops),
626a5b79d62Sstigge@antcom.de 		   },
627*1975d167SUwe Kleine-König 	.probe = max6639_probe,
628a5b79d62Sstigge@antcom.de 	.id_table = max6639_id,
629a5b79d62Sstigge@antcom.de 	.detect = max6639_detect,
630a5b79d62Sstigge@antcom.de 	.address_list = normal_i2c,
631a5b79d62Sstigge@antcom.de };
632a5b79d62Sstigge@antcom.de 
633f0967eeaSAxel Lin module_i2c_driver(max6639_driver);
634a5b79d62Sstigge@antcom.de 
635a5b79d62Sstigge@antcom.de MODULE_AUTHOR("Roland Stigge <stigge@antcom.de>");
636a5b79d62Sstigge@antcom.de MODULE_DESCRIPTION("max6639 driver");
637a5b79d62Sstigge@antcom.de MODULE_LICENSE("GPL");
638