xref: /linux/drivers/hwmon/max6639.c (revision 74ba9207e1adf1966c57450340534ae9742d00af)
1*74ba9207SThomas 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;
72a5b79d62Sstigge@antcom.de 	char valid;		/* !=0 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 */
90a5b79d62Sstigge@antcom.de };
91a5b79d62Sstigge@antcom.de 
92a5b79d62Sstigge@antcom.de static struct max6639_data *max6639_update_device(struct device *dev)
93a5b79d62Sstigge@antcom.de {
947981c584SGuenter Roeck 	struct max6639_data *data = dev_get_drvdata(dev);
957981c584SGuenter Roeck 	struct i2c_client *client = data->client;
96a5b79d62Sstigge@antcom.de 	struct max6639_data *ret = data;
97a5b79d62Sstigge@antcom.de 	int i;
98a5b79d62Sstigge@antcom.de 	int status_reg;
99a5b79d62Sstigge@antcom.de 
100a5b79d62Sstigge@antcom.de 	mutex_lock(&data->update_lock);
101a5b79d62Sstigge@antcom.de 
102a5b79d62Sstigge@antcom.de 	if (time_after(jiffies, data->last_updated + 2 * HZ) || !data->valid) {
103a5b79d62Sstigge@antcom.de 		int res;
104a5b79d62Sstigge@antcom.de 
105a5b79d62Sstigge@antcom.de 		dev_dbg(&client->dev, "Starting max6639 update\n");
106a5b79d62Sstigge@antcom.de 
107a5b79d62Sstigge@antcom.de 		status_reg = i2c_smbus_read_byte_data(client,
108a5b79d62Sstigge@antcom.de 						      MAX6639_REG_STATUS);
109a5b79d62Sstigge@antcom.de 		if (status_reg < 0) {
110a5b79d62Sstigge@antcom.de 			ret = ERR_PTR(status_reg);
111a5b79d62Sstigge@antcom.de 			goto abort;
112a5b79d62Sstigge@antcom.de 		}
113a5b79d62Sstigge@antcom.de 
114a5b79d62Sstigge@antcom.de 		data->status = status_reg;
115a5b79d62Sstigge@antcom.de 
116a5b79d62Sstigge@antcom.de 		for (i = 0; i < 2; i++) {
117a5b79d62Sstigge@antcom.de 			res = i2c_smbus_read_byte_data(client,
118a5b79d62Sstigge@antcom.de 					MAX6639_REG_FAN_CNT(i));
119a5b79d62Sstigge@antcom.de 			if (res < 0) {
120a5b79d62Sstigge@antcom.de 				ret = ERR_PTR(res);
121a5b79d62Sstigge@antcom.de 				goto abort;
122a5b79d62Sstigge@antcom.de 			}
123a5b79d62Sstigge@antcom.de 			data->fan[i] = res;
124a5b79d62Sstigge@antcom.de 
125a5b79d62Sstigge@antcom.de 			res = i2c_smbus_read_byte_data(client,
126a5b79d62Sstigge@antcom.de 					MAX6639_REG_TEMP_EXT(i));
127a5b79d62Sstigge@antcom.de 			if (res < 0) {
128a5b79d62Sstigge@antcom.de 				ret = ERR_PTR(res);
129a5b79d62Sstigge@antcom.de 				goto abort;
130a5b79d62Sstigge@antcom.de 			}
131a5b79d62Sstigge@antcom.de 			data->temp[i] = res >> 5;
132a5b79d62Sstigge@antcom.de 			data->temp_fault[i] = res & 0x01;
133a5b79d62Sstigge@antcom.de 
134a5b79d62Sstigge@antcom.de 			res = i2c_smbus_read_byte_data(client,
135a5b79d62Sstigge@antcom.de 					MAX6639_REG_TEMP(i));
136a5b79d62Sstigge@antcom.de 			if (res < 0) {
137a5b79d62Sstigge@antcom.de 				ret = ERR_PTR(res);
138a5b79d62Sstigge@antcom.de 				goto abort;
139a5b79d62Sstigge@antcom.de 			}
140a5b79d62Sstigge@antcom.de 			data->temp[i] |= res << 3;
141a5b79d62Sstigge@antcom.de 		}
142a5b79d62Sstigge@antcom.de 
143a5b79d62Sstigge@antcom.de 		data->last_updated = jiffies;
144a5b79d62Sstigge@antcom.de 		data->valid = 1;
145a5b79d62Sstigge@antcom.de 	}
146a5b79d62Sstigge@antcom.de abort:
147a5b79d62Sstigge@antcom.de 	mutex_unlock(&data->update_lock);
148a5b79d62Sstigge@antcom.de 
149a5b79d62Sstigge@antcom.de 	return ret;
150a5b79d62Sstigge@antcom.de }
151a5b79d62Sstigge@antcom.de 
1520a0ab22aSGuenter Roeck static ssize_t temp_input_show(struct device *dev,
153a5b79d62Sstigge@antcom.de 			       struct device_attribute *dev_attr, char *buf)
154a5b79d62Sstigge@antcom.de {
155a5b79d62Sstigge@antcom.de 	long temp;
156a5b79d62Sstigge@antcom.de 	struct max6639_data *data = max6639_update_device(dev);
157a5b79d62Sstigge@antcom.de 	struct sensor_device_attribute *attr = to_sensor_dev_attr(dev_attr);
158a5b79d62Sstigge@antcom.de 
159a5b79d62Sstigge@antcom.de 	if (IS_ERR(data))
160a5b79d62Sstigge@antcom.de 		return PTR_ERR(data);
161a5b79d62Sstigge@antcom.de 
162a5b79d62Sstigge@antcom.de 	temp = data->temp[attr->index] * 125;
163a5b79d62Sstigge@antcom.de 	return sprintf(buf, "%ld\n", temp);
164a5b79d62Sstigge@antcom.de }
165a5b79d62Sstigge@antcom.de 
1660a0ab22aSGuenter Roeck static ssize_t temp_fault_show(struct device *dev,
167a5b79d62Sstigge@antcom.de 			       struct device_attribute *dev_attr, char *buf)
168a5b79d62Sstigge@antcom.de {
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 	return sprintf(buf, "%d\n", data->temp_fault[attr->index]);
176a5b79d62Sstigge@antcom.de }
177a5b79d62Sstigge@antcom.de 
1780a0ab22aSGuenter Roeck static ssize_t temp_max_show(struct device *dev,
179a5b79d62Sstigge@antcom.de 			     struct device_attribute *dev_attr, char *buf)
180a5b79d62Sstigge@antcom.de {
181a5b79d62Sstigge@antcom.de 	struct sensor_device_attribute *attr = to_sensor_dev_attr(dev_attr);
1827981c584SGuenter Roeck 	struct max6639_data *data = dev_get_drvdata(dev);
183a5b79d62Sstigge@antcom.de 
184a5b79d62Sstigge@antcom.de 	return sprintf(buf, "%d\n", (data->temp_therm[attr->index] * 1000));
185a5b79d62Sstigge@antcom.de }
186a5b79d62Sstigge@antcom.de 
1870a0ab22aSGuenter Roeck static ssize_t temp_max_store(struct device *dev,
188a5b79d62Sstigge@antcom.de 			      struct device_attribute *dev_attr,
189a5b79d62Sstigge@antcom.de 			      const char *buf, size_t count)
190a5b79d62Sstigge@antcom.de {
191a5b79d62Sstigge@antcom.de 	struct sensor_device_attribute *attr = to_sensor_dev_attr(dev_attr);
1927981c584SGuenter Roeck 	struct max6639_data *data = dev_get_drvdata(dev);
1937981c584SGuenter Roeck 	struct i2c_client *client = data->client;
194a5b79d62Sstigge@antcom.de 	unsigned long val;
195a5b79d62Sstigge@antcom.de 	int res;
196a5b79d62Sstigge@antcom.de 
197179c4fdbSFrans Meulenbroeks 	res = kstrtoul(buf, 10, &val);
198a5b79d62Sstigge@antcom.de 	if (res)
199a5b79d62Sstigge@antcom.de 		return res;
200a5b79d62Sstigge@antcom.de 
201a5b79d62Sstigge@antcom.de 	mutex_lock(&data->update_lock);
202a5b79d62Sstigge@antcom.de 	data->temp_therm[attr->index] = TEMP_LIMIT_TO_REG(val);
203a5b79d62Sstigge@antcom.de 	i2c_smbus_write_byte_data(client,
204a5b79d62Sstigge@antcom.de 				  MAX6639_REG_THERM_LIMIT(attr->index),
205a5b79d62Sstigge@antcom.de 				  data->temp_therm[attr->index]);
206a5b79d62Sstigge@antcom.de 	mutex_unlock(&data->update_lock);
207a5b79d62Sstigge@antcom.de 	return count;
208a5b79d62Sstigge@antcom.de }
209a5b79d62Sstigge@antcom.de 
2100a0ab22aSGuenter Roeck static ssize_t temp_crit_show(struct device *dev,
211a5b79d62Sstigge@antcom.de 			      struct device_attribute *dev_attr, char *buf)
212a5b79d62Sstigge@antcom.de {
213a5b79d62Sstigge@antcom.de 	struct sensor_device_attribute *attr = to_sensor_dev_attr(dev_attr);
2147981c584SGuenter Roeck 	struct max6639_data *data = dev_get_drvdata(dev);
215a5b79d62Sstigge@antcom.de 
216a5b79d62Sstigge@antcom.de 	return sprintf(buf, "%d\n", (data->temp_alert[attr->index] * 1000));
217a5b79d62Sstigge@antcom.de }
218a5b79d62Sstigge@antcom.de 
2190a0ab22aSGuenter Roeck static ssize_t temp_crit_store(struct device *dev,
220a5b79d62Sstigge@antcom.de 			       struct device_attribute *dev_attr,
221a5b79d62Sstigge@antcom.de 			       const char *buf, size_t count)
222a5b79d62Sstigge@antcom.de {
223a5b79d62Sstigge@antcom.de 	struct sensor_device_attribute *attr = to_sensor_dev_attr(dev_attr);
2247981c584SGuenter Roeck 	struct max6639_data *data = dev_get_drvdata(dev);
2257981c584SGuenter Roeck 	struct i2c_client *client = data->client;
226a5b79d62Sstigge@antcom.de 	unsigned long val;
227a5b79d62Sstigge@antcom.de 	int res;
228a5b79d62Sstigge@antcom.de 
229179c4fdbSFrans Meulenbroeks 	res = kstrtoul(buf, 10, &val);
230a5b79d62Sstigge@antcom.de 	if (res)
231a5b79d62Sstigge@antcom.de 		return res;
232a5b79d62Sstigge@antcom.de 
233a5b79d62Sstigge@antcom.de 	mutex_lock(&data->update_lock);
234a5b79d62Sstigge@antcom.de 	data->temp_alert[attr->index] = TEMP_LIMIT_TO_REG(val);
235a5b79d62Sstigge@antcom.de 	i2c_smbus_write_byte_data(client,
236a5b79d62Sstigge@antcom.de 				  MAX6639_REG_ALERT_LIMIT(attr->index),
237a5b79d62Sstigge@antcom.de 				  data->temp_alert[attr->index]);
238a5b79d62Sstigge@antcom.de 	mutex_unlock(&data->update_lock);
239a5b79d62Sstigge@antcom.de 	return count;
240a5b79d62Sstigge@antcom.de }
241a5b79d62Sstigge@antcom.de 
2420a0ab22aSGuenter Roeck static ssize_t temp_emergency_show(struct device *dev,
243a5b79d62Sstigge@antcom.de 				   struct device_attribute *dev_attr,
244a5b79d62Sstigge@antcom.de 				   char *buf)
245a5b79d62Sstigge@antcom.de {
246a5b79d62Sstigge@antcom.de 	struct sensor_device_attribute *attr = to_sensor_dev_attr(dev_attr);
2477981c584SGuenter Roeck 	struct max6639_data *data = dev_get_drvdata(dev);
248a5b79d62Sstigge@antcom.de 
249a5b79d62Sstigge@antcom.de 	return sprintf(buf, "%d\n", (data->temp_ot[attr->index] * 1000));
250a5b79d62Sstigge@antcom.de }
251a5b79d62Sstigge@antcom.de 
2520a0ab22aSGuenter Roeck static ssize_t temp_emergency_store(struct device *dev,
253a5b79d62Sstigge@antcom.de 				    struct device_attribute *dev_attr,
254a5b79d62Sstigge@antcom.de 				    const char *buf, size_t count)
255a5b79d62Sstigge@antcom.de {
256a5b79d62Sstigge@antcom.de 	struct sensor_device_attribute *attr = to_sensor_dev_attr(dev_attr);
2577981c584SGuenter Roeck 	struct max6639_data *data = dev_get_drvdata(dev);
2587981c584SGuenter Roeck 	struct i2c_client *client = data->client;
259a5b79d62Sstigge@antcom.de 	unsigned long val;
260a5b79d62Sstigge@antcom.de 	int res;
261a5b79d62Sstigge@antcom.de 
262179c4fdbSFrans Meulenbroeks 	res = kstrtoul(buf, 10, &val);
263a5b79d62Sstigge@antcom.de 	if (res)
264a5b79d62Sstigge@antcom.de 		return res;
265a5b79d62Sstigge@antcom.de 
266a5b79d62Sstigge@antcom.de 	mutex_lock(&data->update_lock);
267a5b79d62Sstigge@antcom.de 	data->temp_ot[attr->index] = TEMP_LIMIT_TO_REG(val);
268a5b79d62Sstigge@antcom.de 	i2c_smbus_write_byte_data(client,
269a5b79d62Sstigge@antcom.de 				  MAX6639_REG_OT_LIMIT(attr->index),
270a5b79d62Sstigge@antcom.de 				  data->temp_ot[attr->index]);
271a5b79d62Sstigge@antcom.de 	mutex_unlock(&data->update_lock);
272a5b79d62Sstigge@antcom.de 	return count;
273a5b79d62Sstigge@antcom.de }
274a5b79d62Sstigge@antcom.de 
2750a0ab22aSGuenter Roeck static ssize_t pwm_show(struct device *dev, struct device_attribute *dev_attr,
2760a0ab22aSGuenter Roeck 			char *buf)
277a5b79d62Sstigge@antcom.de {
278a5b79d62Sstigge@antcom.de 	struct sensor_device_attribute *attr = to_sensor_dev_attr(dev_attr);
2797981c584SGuenter Roeck 	struct max6639_data *data = dev_get_drvdata(dev);
280a5b79d62Sstigge@antcom.de 
281a5b79d62Sstigge@antcom.de 	return sprintf(buf, "%d\n", data->pwm[attr->index] * 255 / 120);
282a5b79d62Sstigge@antcom.de }
283a5b79d62Sstigge@antcom.de 
2840a0ab22aSGuenter Roeck static ssize_t pwm_store(struct device *dev,
2850a0ab22aSGuenter Roeck 			 struct device_attribute *dev_attr, const char *buf,
2860a0ab22aSGuenter Roeck 			 size_t count)
287a5b79d62Sstigge@antcom.de {
288a5b79d62Sstigge@antcom.de 	struct sensor_device_attribute *attr = to_sensor_dev_attr(dev_attr);
2897981c584SGuenter Roeck 	struct max6639_data *data = dev_get_drvdata(dev);
2907981c584SGuenter Roeck 	struct i2c_client *client = data->client;
291a5b79d62Sstigge@antcom.de 	unsigned long val;
292a5b79d62Sstigge@antcom.de 	int res;
293a5b79d62Sstigge@antcom.de 
294179c4fdbSFrans Meulenbroeks 	res = kstrtoul(buf, 10, &val);
295a5b79d62Sstigge@antcom.de 	if (res)
296a5b79d62Sstigge@antcom.de 		return res;
297a5b79d62Sstigge@antcom.de 
2982a844c14SGuenter Roeck 	val = clamp_val(val, 0, 255);
299a5b79d62Sstigge@antcom.de 
300a5b79d62Sstigge@antcom.de 	mutex_lock(&data->update_lock);
301a5b79d62Sstigge@antcom.de 	data->pwm[attr->index] = (u8)(val * 120 / 255);
302a5b79d62Sstigge@antcom.de 	i2c_smbus_write_byte_data(client,
303a5b79d62Sstigge@antcom.de 				  MAX6639_REG_TARGTDUTY(attr->index),
304a5b79d62Sstigge@antcom.de 				  data->pwm[attr->index]);
305a5b79d62Sstigge@antcom.de 	mutex_unlock(&data->update_lock);
306a5b79d62Sstigge@antcom.de 	return count;
307a5b79d62Sstigge@antcom.de }
308a5b79d62Sstigge@antcom.de 
3090a0ab22aSGuenter Roeck static ssize_t fan_input_show(struct device *dev,
310a5b79d62Sstigge@antcom.de 			      struct device_attribute *dev_attr, char *buf)
311a5b79d62Sstigge@antcom.de {
312a5b79d62Sstigge@antcom.de 	struct max6639_data *data = max6639_update_device(dev);
313a5b79d62Sstigge@antcom.de 	struct sensor_device_attribute *attr = to_sensor_dev_attr(dev_attr);
314a5b79d62Sstigge@antcom.de 
315a5b79d62Sstigge@antcom.de 	if (IS_ERR(data))
316a5b79d62Sstigge@antcom.de 		return PTR_ERR(data);
317a5b79d62Sstigge@antcom.de 
318a5b79d62Sstigge@antcom.de 	return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[attr->index],
319b63d97a3SChris D Schimp 		       data->rpm_range));
320a5b79d62Sstigge@antcom.de }
321a5b79d62Sstigge@antcom.de 
3220a0ab22aSGuenter Roeck static ssize_t alarm_show(struct device *dev,
323a5b79d62Sstigge@antcom.de 			  struct device_attribute *dev_attr, char *buf)
324a5b79d62Sstigge@antcom.de {
325a5b79d62Sstigge@antcom.de 	struct max6639_data *data = max6639_update_device(dev);
326a5b79d62Sstigge@antcom.de 	struct sensor_device_attribute *attr = to_sensor_dev_attr(dev_attr);
327a5b79d62Sstigge@antcom.de 
328a5b79d62Sstigge@antcom.de 	if (IS_ERR(data))
329a5b79d62Sstigge@antcom.de 		return PTR_ERR(data);
330a5b79d62Sstigge@antcom.de 
331a5b79d62Sstigge@antcom.de 	return sprintf(buf, "%d\n", !!(data->status & (1 << attr->index)));
332a5b79d62Sstigge@antcom.de }
333a5b79d62Sstigge@antcom.de 
3340a0ab22aSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(temp1_input, temp_input, 0);
3350a0ab22aSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(temp2_input, temp_input, 1);
3360a0ab22aSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(temp1_fault, temp_fault, 0);
3370a0ab22aSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(temp2_fault, temp_fault, 1);
3380a0ab22aSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(temp1_max, temp_max, 0);
3390a0ab22aSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(temp2_max, temp_max, 1);
3400a0ab22aSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(temp1_crit, temp_crit, 0);
3410a0ab22aSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(temp2_crit, temp_crit, 1);
3420a0ab22aSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(temp1_emergency, temp_emergency, 0);
3430a0ab22aSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(temp2_emergency, temp_emergency, 1);
3440a0ab22aSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(pwm1, pwm, 0);
3450a0ab22aSGuenter Roeck static SENSOR_DEVICE_ATTR_RW(pwm2, pwm, 1);
3460a0ab22aSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(fan1_input, fan_input, 0);
3470a0ab22aSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(fan2_input, fan_input, 1);
3480a0ab22aSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(fan1_fault, alarm, 1);
3490a0ab22aSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(fan2_fault, alarm, 0);
3500a0ab22aSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(temp1_max_alarm, alarm, 3);
3510a0ab22aSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(temp2_max_alarm, alarm, 2);
3520a0ab22aSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(temp1_crit_alarm, alarm, 7);
3530a0ab22aSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(temp2_crit_alarm, alarm, 6);
3540a0ab22aSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(temp1_emergency_alarm, alarm, 5);
3550a0ab22aSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(temp2_emergency_alarm, alarm, 4);
356a5b79d62Sstigge@antcom.de 
357a5b79d62Sstigge@antcom.de 
3587981c584SGuenter Roeck static struct attribute *max6639_attrs[] = {
359a5b79d62Sstigge@antcom.de 	&sensor_dev_attr_temp1_input.dev_attr.attr,
360a5b79d62Sstigge@antcom.de 	&sensor_dev_attr_temp2_input.dev_attr.attr,
361a5b79d62Sstigge@antcom.de 	&sensor_dev_attr_temp1_fault.dev_attr.attr,
362a5b79d62Sstigge@antcom.de 	&sensor_dev_attr_temp2_fault.dev_attr.attr,
363a5b79d62Sstigge@antcom.de 	&sensor_dev_attr_temp1_max.dev_attr.attr,
364a5b79d62Sstigge@antcom.de 	&sensor_dev_attr_temp2_max.dev_attr.attr,
365a5b79d62Sstigge@antcom.de 	&sensor_dev_attr_temp1_crit.dev_attr.attr,
366a5b79d62Sstigge@antcom.de 	&sensor_dev_attr_temp2_crit.dev_attr.attr,
367a5b79d62Sstigge@antcom.de 	&sensor_dev_attr_temp1_emergency.dev_attr.attr,
368a5b79d62Sstigge@antcom.de 	&sensor_dev_attr_temp2_emergency.dev_attr.attr,
369a5b79d62Sstigge@antcom.de 	&sensor_dev_attr_pwm1.dev_attr.attr,
370a5b79d62Sstigge@antcom.de 	&sensor_dev_attr_pwm2.dev_attr.attr,
371a5b79d62Sstigge@antcom.de 	&sensor_dev_attr_fan1_input.dev_attr.attr,
372a5b79d62Sstigge@antcom.de 	&sensor_dev_attr_fan2_input.dev_attr.attr,
373a5b79d62Sstigge@antcom.de 	&sensor_dev_attr_fan1_fault.dev_attr.attr,
374a5b79d62Sstigge@antcom.de 	&sensor_dev_attr_fan2_fault.dev_attr.attr,
375a5b79d62Sstigge@antcom.de 	&sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
376a5b79d62Sstigge@antcom.de 	&sensor_dev_attr_temp2_max_alarm.dev_attr.attr,
377a5b79d62Sstigge@antcom.de 	&sensor_dev_attr_temp1_crit_alarm.dev_attr.attr,
378a5b79d62Sstigge@antcom.de 	&sensor_dev_attr_temp2_crit_alarm.dev_attr.attr,
379a5b79d62Sstigge@antcom.de 	&sensor_dev_attr_temp1_emergency_alarm.dev_attr.attr,
380a5b79d62Sstigge@antcom.de 	&sensor_dev_attr_temp2_emergency_alarm.dev_attr.attr,
381a5b79d62Sstigge@antcom.de 	NULL
382a5b79d62Sstigge@antcom.de };
3837981c584SGuenter Roeck ATTRIBUTE_GROUPS(max6639);
384a5b79d62Sstigge@antcom.de 
385a5b79d62Sstigge@antcom.de /*
386a5b79d62Sstigge@antcom.de  *  returns respective index in rpm_ranges table
387a5b79d62Sstigge@antcom.de  *  1 by default on invalid range
388a5b79d62Sstigge@antcom.de  */
389a5b79d62Sstigge@antcom.de static int rpm_range_to_reg(int range)
390a5b79d62Sstigge@antcom.de {
391a5b79d62Sstigge@antcom.de 	int i;
392a5b79d62Sstigge@antcom.de 
393a5b79d62Sstigge@antcom.de 	for (i = 0; i < ARRAY_SIZE(rpm_ranges); i++) {
394a5b79d62Sstigge@antcom.de 		if (rpm_ranges[i] == range)
395a5b79d62Sstigge@antcom.de 			return i;
396a5b79d62Sstigge@antcom.de 	}
397a5b79d62Sstigge@antcom.de 
398a5b79d62Sstigge@antcom.de 	return 1; /* default: 4000 RPM */
399a5b79d62Sstigge@antcom.de }
400a5b79d62Sstigge@antcom.de 
4017981c584SGuenter Roeck static int max6639_init_client(struct i2c_client *client,
4027981c584SGuenter Roeck 			       struct max6639_data *data)
403a5b79d62Sstigge@antcom.de {
404a5b79d62Sstigge@antcom.de 	struct max6639_platform_data *max6639_info =
405a8b3a3a5SJingoo Han 		dev_get_platdata(&client->dev);
4062f2da1acSChris D Schimp 	int i;
407a5b79d62Sstigge@antcom.de 	int rpm_range = 1; /* default: 4000 RPM */
4082f2da1acSChris D Schimp 	int err;
409a5b79d62Sstigge@antcom.de 
410177f3b92Sstigge@antcom.de 	/* Reset chip to default values, see below for GCONFIG setup */
411a5b79d62Sstigge@antcom.de 	err = i2c_smbus_write_byte_data(client, MAX6639_REG_GCONFIG,
412a5b79d62Sstigge@antcom.de 				  MAX6639_GCONFIG_POR);
413a5b79d62Sstigge@antcom.de 	if (err)
414a5b79d62Sstigge@antcom.de 		goto exit;
415a5b79d62Sstigge@antcom.de 
416a5b79d62Sstigge@antcom.de 	/* Fans pulse per revolution is 2 by default */
417a5b79d62Sstigge@antcom.de 	if (max6639_info && max6639_info->ppr > 0 &&
418a5b79d62Sstigge@antcom.de 			max6639_info->ppr < 5)
419a5b79d62Sstigge@antcom.de 		data->ppr = max6639_info->ppr;
420a5b79d62Sstigge@antcom.de 	else
421a5b79d62Sstigge@antcom.de 		data->ppr = 2;
422a5b79d62Sstigge@antcom.de 	data->ppr -= 1;
423a5b79d62Sstigge@antcom.de 
424a5b79d62Sstigge@antcom.de 	if (max6639_info)
425a5b79d62Sstigge@antcom.de 		rpm_range = rpm_range_to_reg(max6639_info->rpm_range);
426a5b79d62Sstigge@antcom.de 	data->rpm_range = rpm_range;
427a5b79d62Sstigge@antcom.de 
428a5b79d62Sstigge@antcom.de 	for (i = 0; i < 2; i++) {
429a5b79d62Sstigge@antcom.de 
4302f2da1acSChris D Schimp 		/* Set Fan pulse per revolution */
4312f2da1acSChris D Schimp 		err = i2c_smbus_write_byte_data(client,
4322f2da1acSChris D Schimp 				MAX6639_REG_FAN_PPR(i),
4332f2da1acSChris D Schimp 				data->ppr << 6);
4342f2da1acSChris D Schimp 		if (err)
4352f2da1acSChris D Schimp 			goto exit;
4362f2da1acSChris D Schimp 
437a5b79d62Sstigge@antcom.de 		/* Fans config PWM, RPM */
438a5b79d62Sstigge@antcom.de 		err = i2c_smbus_write_byte_data(client,
439a5b79d62Sstigge@antcom.de 			MAX6639_REG_FAN_CONFIG1(i),
440a5b79d62Sstigge@antcom.de 			MAX6639_FAN_CONFIG1_PWM | rpm_range);
441a5b79d62Sstigge@antcom.de 		if (err)
442a5b79d62Sstigge@antcom.de 			goto exit;
443a5b79d62Sstigge@antcom.de 
444a5b79d62Sstigge@antcom.de 		/* Fans PWM polarity high by default */
445a5b79d62Sstigge@antcom.de 		if (max6639_info && max6639_info->pwm_polarity == 0)
446a5b79d62Sstigge@antcom.de 			err = i2c_smbus_write_byte_data(client,
447a5b79d62Sstigge@antcom.de 				MAX6639_REG_FAN_CONFIG2a(i), 0x00);
448a5b79d62Sstigge@antcom.de 		else
449a5b79d62Sstigge@antcom.de 			err = i2c_smbus_write_byte_data(client,
450a5b79d62Sstigge@antcom.de 				MAX6639_REG_FAN_CONFIG2a(i), 0x02);
451a5b79d62Sstigge@antcom.de 		if (err)
452a5b79d62Sstigge@antcom.de 			goto exit;
453a5b79d62Sstigge@antcom.de 
454177f3b92Sstigge@antcom.de 		/*
455177f3b92Sstigge@antcom.de 		 * /THERM full speed enable,
456177f3b92Sstigge@antcom.de 		 * PWM frequency 25kHz, see also GCONFIG below
457177f3b92Sstigge@antcom.de 		 */
458177f3b92Sstigge@antcom.de 		err = i2c_smbus_write_byte_data(client,
459177f3b92Sstigge@antcom.de 			MAX6639_REG_FAN_CONFIG3(i),
460177f3b92Sstigge@antcom.de 			MAX6639_FAN_CONFIG3_THERM_FULL_SPEED | 0x03);
461177f3b92Sstigge@antcom.de 		if (err)
462177f3b92Sstigge@antcom.de 			goto exit;
463177f3b92Sstigge@antcom.de 
464a5b79d62Sstigge@antcom.de 		/* Max. temp. 80C/90C/100C */
465a5b79d62Sstigge@antcom.de 		data->temp_therm[i] = 80;
466a5b79d62Sstigge@antcom.de 		data->temp_alert[i] = 90;
467a5b79d62Sstigge@antcom.de 		data->temp_ot[i] = 100;
468a5b79d62Sstigge@antcom.de 		err = i2c_smbus_write_byte_data(client,
469a5b79d62Sstigge@antcom.de 				MAX6639_REG_THERM_LIMIT(i),
470a5b79d62Sstigge@antcom.de 				data->temp_therm[i]);
471a5b79d62Sstigge@antcom.de 		if (err)
472a5b79d62Sstigge@antcom.de 			goto exit;
473a5b79d62Sstigge@antcom.de 		err = i2c_smbus_write_byte_data(client,
474a5b79d62Sstigge@antcom.de 				MAX6639_REG_ALERT_LIMIT(i),
475a5b79d62Sstigge@antcom.de 				data->temp_alert[i]);
476a5b79d62Sstigge@antcom.de 		if (err)
477a5b79d62Sstigge@antcom.de 			goto exit;
478a5b79d62Sstigge@antcom.de 		err = i2c_smbus_write_byte_data(client,
479a5b79d62Sstigge@antcom.de 				MAX6639_REG_OT_LIMIT(i), data->temp_ot[i]);
480a5b79d62Sstigge@antcom.de 		if (err)
481a5b79d62Sstigge@antcom.de 			goto exit;
482a5b79d62Sstigge@antcom.de 
483a5b79d62Sstigge@antcom.de 		/* PWM 120/120 (i.e. 100%) */
484a5b79d62Sstigge@antcom.de 		data->pwm[i] = 120;
485a5b79d62Sstigge@antcom.de 		err = i2c_smbus_write_byte_data(client,
486a5b79d62Sstigge@antcom.de 				MAX6639_REG_TARGTDUTY(i), data->pwm[i]);
487a5b79d62Sstigge@antcom.de 		if (err)
488a5b79d62Sstigge@antcom.de 			goto exit;
489a5b79d62Sstigge@antcom.de 	}
490a5b79d62Sstigge@antcom.de 	/* Start monitoring */
491a5b79d62Sstigge@antcom.de 	err = i2c_smbus_write_byte_data(client, MAX6639_REG_GCONFIG,
492177f3b92Sstigge@antcom.de 		MAX6639_GCONFIG_DISABLE_TIMEOUT | MAX6639_GCONFIG_CH2_LOCAL |
493177f3b92Sstigge@antcom.de 		MAX6639_GCONFIG_PWM_FREQ_HI);
494a5b79d62Sstigge@antcom.de exit:
495a5b79d62Sstigge@antcom.de 	return err;
496a5b79d62Sstigge@antcom.de }
497a5b79d62Sstigge@antcom.de 
498a5b79d62Sstigge@antcom.de /* Return 0 if detection is successful, -ENODEV otherwise */
499a5b79d62Sstigge@antcom.de static int max6639_detect(struct i2c_client *client,
500a5b79d62Sstigge@antcom.de 			  struct i2c_board_info *info)
501a5b79d62Sstigge@antcom.de {
502a5b79d62Sstigge@antcom.de 	struct i2c_adapter *adapter = client->adapter;
503a5b79d62Sstigge@antcom.de 	int dev_id, manu_id;
504a5b79d62Sstigge@antcom.de 
505a5b79d62Sstigge@antcom.de 	if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
506a5b79d62Sstigge@antcom.de 		return -ENODEV;
507a5b79d62Sstigge@antcom.de 
508a5b79d62Sstigge@antcom.de 	/* Actual detection via device and manufacturer ID */
509a5b79d62Sstigge@antcom.de 	dev_id = i2c_smbus_read_byte_data(client, MAX6639_REG_DEVID);
510a5b79d62Sstigge@antcom.de 	manu_id = i2c_smbus_read_byte_data(client, MAX6639_REG_MANUID);
511a5b79d62Sstigge@antcom.de 	if (dev_id != 0x58 || manu_id != 0x4D)
512a5b79d62Sstigge@antcom.de 		return -ENODEV;
513a5b79d62Sstigge@antcom.de 
514a5b79d62Sstigge@antcom.de 	strlcpy(info->type, "max6639", I2C_NAME_SIZE);
515a5b79d62Sstigge@antcom.de 
516a5b79d62Sstigge@antcom.de 	return 0;
517a5b79d62Sstigge@antcom.de }
518a5b79d62Sstigge@antcom.de 
519a5b79d62Sstigge@antcom.de static int max6639_probe(struct i2c_client *client,
520a5b79d62Sstigge@antcom.de 			 const struct i2c_device_id *id)
521a5b79d62Sstigge@antcom.de {
522c1ea0a04SGuenter Roeck 	struct device *dev = &client->dev;
523a5b79d62Sstigge@antcom.de 	struct max6639_data *data;
5247981c584SGuenter Roeck 	struct device *hwmon_dev;
525a5b79d62Sstigge@antcom.de 	int err;
526a5b79d62Sstigge@antcom.de 
527c1ea0a04SGuenter Roeck 	data = devm_kzalloc(dev, sizeof(struct max6639_data), GFP_KERNEL);
528b07405fbSGuenter Roeck 	if (!data)
529b07405fbSGuenter Roeck 		return -ENOMEM;
530a5b79d62Sstigge@antcom.de 
5317981c584SGuenter Roeck 	data->client = client;
532a5b79d62Sstigge@antcom.de 	mutex_init(&data->update_lock);
533a5b79d62Sstigge@antcom.de 
534a5b79d62Sstigge@antcom.de 	/* Initialize the max6639 chip */
5357981c584SGuenter Roeck 	err = max6639_init_client(client, data);
536a5b79d62Sstigge@antcom.de 	if (err < 0)
537b07405fbSGuenter Roeck 		return err;
538a5b79d62Sstigge@antcom.de 
5397981c584SGuenter Roeck 	hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
5407981c584SGuenter Roeck 							   data,
5417981c584SGuenter Roeck 							   max6639_groups);
5427981c584SGuenter Roeck 	return PTR_ERR_OR_ZERO(hwmon_dev);
543a5b79d62Sstigge@antcom.de }
544a5b79d62Sstigge@antcom.de 
54552f30f77SMark Brown #ifdef CONFIG_PM_SLEEP
54652f30f77SMark Brown static int max6639_suspend(struct device *dev)
547a5b79d62Sstigge@antcom.de {
54852f30f77SMark Brown 	struct i2c_client *client = to_i2c_client(dev);
549a5b79d62Sstigge@antcom.de 	int data = i2c_smbus_read_byte_data(client, MAX6639_REG_GCONFIG);
550a5b79d62Sstigge@antcom.de 	if (data < 0)
551a5b79d62Sstigge@antcom.de 		return data;
552a5b79d62Sstigge@antcom.de 
553a5b79d62Sstigge@antcom.de 	return i2c_smbus_write_byte_data(client,
554a5b79d62Sstigge@antcom.de 			MAX6639_REG_GCONFIG, data | MAX6639_GCONFIG_STANDBY);
555a5b79d62Sstigge@antcom.de }
556a5b79d62Sstigge@antcom.de 
55752f30f77SMark Brown static int max6639_resume(struct device *dev)
558a5b79d62Sstigge@antcom.de {
55952f30f77SMark Brown 	struct i2c_client *client = to_i2c_client(dev);
560a5b79d62Sstigge@antcom.de 	int data = i2c_smbus_read_byte_data(client, MAX6639_REG_GCONFIG);
561a5b79d62Sstigge@antcom.de 	if (data < 0)
562a5b79d62Sstigge@antcom.de 		return data;
563a5b79d62Sstigge@antcom.de 
564a5b79d62Sstigge@antcom.de 	return i2c_smbus_write_byte_data(client,
565a5b79d62Sstigge@antcom.de 			MAX6639_REG_GCONFIG, data & ~MAX6639_GCONFIG_STANDBY);
566a5b79d62Sstigge@antcom.de }
56752f30f77SMark Brown #endif /* CONFIG_PM_SLEEP */
568a5b79d62Sstigge@antcom.de 
569a5b79d62Sstigge@antcom.de static const struct i2c_device_id max6639_id[] = {
570a5b79d62Sstigge@antcom.de 	{"max6639", 0},
571a5b79d62Sstigge@antcom.de 	{ }
572a5b79d62Sstigge@antcom.de };
573a5b79d62Sstigge@antcom.de 
574a5b79d62Sstigge@antcom.de MODULE_DEVICE_TABLE(i2c, max6639_id);
575a5b79d62Sstigge@antcom.de 
576768821a3SJingoo Han static SIMPLE_DEV_PM_OPS(max6639_pm_ops, max6639_suspend, max6639_resume);
57752f30f77SMark Brown 
578a5b79d62Sstigge@antcom.de static struct i2c_driver max6639_driver = {
579a5b79d62Sstigge@antcom.de 	.class = I2C_CLASS_HWMON,
580a5b79d62Sstigge@antcom.de 	.driver = {
581a5b79d62Sstigge@antcom.de 		   .name = "max6639",
58252f30f77SMark Brown 		   .pm = &max6639_pm_ops,
583a5b79d62Sstigge@antcom.de 		   },
584a5b79d62Sstigge@antcom.de 	.probe = max6639_probe,
585a5b79d62Sstigge@antcom.de 	.id_table = max6639_id,
586a5b79d62Sstigge@antcom.de 	.detect = max6639_detect,
587a5b79d62Sstigge@antcom.de 	.address_list = normal_i2c,
588a5b79d62Sstigge@antcom.de };
589a5b79d62Sstigge@antcom.de 
590f0967eeaSAxel Lin module_i2c_driver(max6639_driver);
591a5b79d62Sstigge@antcom.de 
592a5b79d62Sstigge@antcom.de MODULE_AUTHOR("Roland Stigge <stigge@antcom.de>");
593a5b79d62Sstigge@antcom.de MODULE_DESCRIPTION("max6639 driver");
594a5b79d62Sstigge@antcom.de MODULE_LICENSE("GPL");
595