xref: /linux/drivers/hwmon/max6639.c (revision 0526b56cbc3c489642bd6a5fe4b718dea7ef0ee8)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * max6639.c - Support for Maxim MAX6639
4  *
5  * 2-Channel Temperature Monitor with Dual PWM Fan-Speed Controller
6  *
7  * Copyright (C) 2010, 2011 Roland Stigge <stigge@antcom.de>
8  *
9  * based on the initial MAX6639 support from semptian.net
10  * by He Changqing <hechangqing@semptian.com>
11  */
12 
13 #include <linux/module.h>
14 #include <linux/init.h>
15 #include <linux/slab.h>
16 #include <linux/jiffies.h>
17 #include <linux/i2c.h>
18 #include <linux/hwmon.h>
19 #include <linux/hwmon-sysfs.h>
20 #include <linux/err.h>
21 #include <linux/mutex.h>
22 #include <linux/platform_data/max6639.h>
23 
24 /* Addresses to scan */
25 static const unsigned short normal_i2c[] = { 0x2c, 0x2e, 0x2f, I2C_CLIENT_END };
26 
27 /* The MAX6639 registers, valid channel numbers: 0, 1 */
28 #define MAX6639_REG_TEMP(ch)			(0x00 + (ch))
29 #define MAX6639_REG_STATUS			0x02
30 #define MAX6639_REG_OUTPUT_MASK			0x03
31 #define MAX6639_REG_GCONFIG			0x04
32 #define MAX6639_REG_TEMP_EXT(ch)		(0x05 + (ch))
33 #define MAX6639_REG_ALERT_LIMIT(ch)		(0x08 + (ch))
34 #define MAX6639_REG_OT_LIMIT(ch)		(0x0A + (ch))
35 #define MAX6639_REG_THERM_LIMIT(ch)		(0x0C + (ch))
36 #define MAX6639_REG_FAN_CONFIG1(ch)		(0x10 + (ch) * 4)
37 #define MAX6639_REG_FAN_CONFIG2a(ch)		(0x11 + (ch) * 4)
38 #define MAX6639_REG_FAN_CONFIG2b(ch)		(0x12 + (ch) * 4)
39 #define MAX6639_REG_FAN_CONFIG3(ch)		(0x13 + (ch) * 4)
40 #define MAX6639_REG_FAN_CNT(ch)			(0x20 + (ch))
41 #define MAX6639_REG_TARGET_CNT(ch)		(0x22 + (ch))
42 #define MAX6639_REG_FAN_PPR(ch)			(0x24 + (ch))
43 #define MAX6639_REG_TARGTDUTY(ch)		(0x26 + (ch))
44 #define MAX6639_REG_FAN_START_TEMP(ch)		(0x28 + (ch))
45 #define MAX6639_REG_DEVID			0x3D
46 #define MAX6639_REG_MANUID			0x3E
47 #define MAX6639_REG_DEVREV			0x3F
48 
49 /* Register bits */
50 #define MAX6639_GCONFIG_STANDBY			0x80
51 #define MAX6639_GCONFIG_POR			0x40
52 #define MAX6639_GCONFIG_DISABLE_TIMEOUT		0x20
53 #define MAX6639_GCONFIG_CH2_LOCAL		0x10
54 #define MAX6639_GCONFIG_PWM_FREQ_HI		0x08
55 
56 #define MAX6639_FAN_CONFIG1_PWM			0x80
57 
58 #define MAX6639_FAN_CONFIG3_THERM_FULL_SPEED	0x40
59 
60 static const int rpm_ranges[] = { 2000, 4000, 8000, 16000 };
61 
62 #define FAN_FROM_REG(val, rpm_range)	((val) == 0 || (val) == 255 ? \
63 				0 : (rpm_ranges[rpm_range] * 30) / (val))
64 #define TEMP_LIMIT_TO_REG(val)	clamp_val((val) / 1000, 0, 255)
65 
66 /*
67  * Client data (each client gets its own)
68  */
69 struct max6639_data {
70 	struct i2c_client *client;
71 	struct mutex update_lock;
72 	bool valid;		/* true if following fields are valid */
73 	unsigned long last_updated;	/* In jiffies */
74 
75 	/* Register values sampled regularly */
76 	u16 temp[2];		/* Temperature, in 1/8 C, 0..255 C */
77 	bool temp_fault[2];	/* Detected temperature diode failure */
78 	u8 fan[2];		/* Register value: TACH count for fans >=30 */
79 	u8 status;		/* Detected channel alarms and fan failures */
80 
81 	/* Register values only written to */
82 	u8 pwm[2];		/* Register value: Duty cycle 0..120 */
83 	u8 temp_therm[2];	/* THERM Temperature, 0..255 C (->_max) */
84 	u8 temp_alert[2];	/* ALERT Temperature, 0..255 C (->_crit) */
85 	u8 temp_ot[2];		/* OT Temperature, 0..255 C (->_emergency) */
86 
87 	/* Register values initialized only once */
88 	u8 ppr;			/* Pulses per rotation 0..3 for 1..4 ppr */
89 	u8 rpm_range;		/* Index in above rpm_ranges table */
90 
91 	/* Optional regulator for FAN supply */
92 	struct regulator *reg;
93 };
94 
95 static struct max6639_data *max6639_update_device(struct device *dev)
96 {
97 	struct max6639_data *data = dev_get_drvdata(dev);
98 	struct i2c_client *client = data->client;
99 	struct max6639_data *ret = data;
100 	int i;
101 	int status_reg;
102 
103 	mutex_lock(&data->update_lock);
104 
105 	if (time_after(jiffies, data->last_updated + 2 * HZ) || !data->valid) {
106 		int res;
107 
108 		dev_dbg(&client->dev, "Starting max6639 update\n");
109 
110 		status_reg = i2c_smbus_read_byte_data(client,
111 						      MAX6639_REG_STATUS);
112 		if (status_reg < 0) {
113 			ret = ERR_PTR(status_reg);
114 			goto abort;
115 		}
116 
117 		data->status = status_reg;
118 
119 		for (i = 0; i < 2; i++) {
120 			res = i2c_smbus_read_byte_data(client,
121 					MAX6639_REG_FAN_CNT(i));
122 			if (res < 0) {
123 				ret = ERR_PTR(res);
124 				goto abort;
125 			}
126 			data->fan[i] = res;
127 
128 			res = i2c_smbus_read_byte_data(client,
129 					MAX6639_REG_TEMP_EXT(i));
130 			if (res < 0) {
131 				ret = ERR_PTR(res);
132 				goto abort;
133 			}
134 			data->temp[i] = res >> 5;
135 			data->temp_fault[i] = res & 0x01;
136 
137 			res = i2c_smbus_read_byte_data(client,
138 					MAX6639_REG_TEMP(i));
139 			if (res < 0) {
140 				ret = ERR_PTR(res);
141 				goto abort;
142 			}
143 			data->temp[i] |= res << 3;
144 		}
145 
146 		data->last_updated = jiffies;
147 		data->valid = true;
148 	}
149 abort:
150 	mutex_unlock(&data->update_lock);
151 
152 	return ret;
153 }
154 
155 static ssize_t temp_input_show(struct device *dev,
156 			       struct device_attribute *dev_attr, char *buf)
157 {
158 	long temp;
159 	struct max6639_data *data = max6639_update_device(dev);
160 	struct sensor_device_attribute *attr = to_sensor_dev_attr(dev_attr);
161 
162 	if (IS_ERR(data))
163 		return PTR_ERR(data);
164 
165 	temp = data->temp[attr->index] * 125;
166 	return sprintf(buf, "%ld\n", temp);
167 }
168 
169 static ssize_t temp_fault_show(struct device *dev,
170 			       struct device_attribute *dev_attr, char *buf)
171 {
172 	struct max6639_data *data = max6639_update_device(dev);
173 	struct sensor_device_attribute *attr = to_sensor_dev_attr(dev_attr);
174 
175 	if (IS_ERR(data))
176 		return PTR_ERR(data);
177 
178 	return sprintf(buf, "%d\n", data->temp_fault[attr->index]);
179 }
180 
181 static ssize_t temp_max_show(struct device *dev,
182 			     struct device_attribute *dev_attr, char *buf)
183 {
184 	struct sensor_device_attribute *attr = to_sensor_dev_attr(dev_attr);
185 	struct max6639_data *data = dev_get_drvdata(dev);
186 
187 	return sprintf(buf, "%d\n", (data->temp_therm[attr->index] * 1000));
188 }
189 
190 static ssize_t temp_max_store(struct device *dev,
191 			      struct device_attribute *dev_attr,
192 			      const char *buf, size_t count)
193 {
194 	struct sensor_device_attribute *attr = to_sensor_dev_attr(dev_attr);
195 	struct max6639_data *data = dev_get_drvdata(dev);
196 	struct i2c_client *client = data->client;
197 	unsigned long val;
198 	int res;
199 
200 	res = kstrtoul(buf, 10, &val);
201 	if (res)
202 		return res;
203 
204 	mutex_lock(&data->update_lock);
205 	data->temp_therm[attr->index] = TEMP_LIMIT_TO_REG(val);
206 	i2c_smbus_write_byte_data(client,
207 				  MAX6639_REG_THERM_LIMIT(attr->index),
208 				  data->temp_therm[attr->index]);
209 	mutex_unlock(&data->update_lock);
210 	return count;
211 }
212 
213 static ssize_t temp_crit_show(struct device *dev,
214 			      struct device_attribute *dev_attr, char *buf)
215 {
216 	struct sensor_device_attribute *attr = to_sensor_dev_attr(dev_attr);
217 	struct max6639_data *data = dev_get_drvdata(dev);
218 
219 	return sprintf(buf, "%d\n", (data->temp_alert[attr->index] * 1000));
220 }
221 
222 static ssize_t temp_crit_store(struct device *dev,
223 			       struct device_attribute *dev_attr,
224 			       const char *buf, size_t count)
225 {
226 	struct sensor_device_attribute *attr = to_sensor_dev_attr(dev_attr);
227 	struct max6639_data *data = dev_get_drvdata(dev);
228 	struct i2c_client *client = data->client;
229 	unsigned long val;
230 	int res;
231 
232 	res = kstrtoul(buf, 10, &val);
233 	if (res)
234 		return res;
235 
236 	mutex_lock(&data->update_lock);
237 	data->temp_alert[attr->index] = TEMP_LIMIT_TO_REG(val);
238 	i2c_smbus_write_byte_data(client,
239 				  MAX6639_REG_ALERT_LIMIT(attr->index),
240 				  data->temp_alert[attr->index]);
241 	mutex_unlock(&data->update_lock);
242 	return count;
243 }
244 
245 static ssize_t temp_emergency_show(struct device *dev,
246 				   struct device_attribute *dev_attr,
247 				   char *buf)
248 {
249 	struct sensor_device_attribute *attr = to_sensor_dev_attr(dev_attr);
250 	struct max6639_data *data = dev_get_drvdata(dev);
251 
252 	return sprintf(buf, "%d\n", (data->temp_ot[attr->index] * 1000));
253 }
254 
255 static ssize_t temp_emergency_store(struct device *dev,
256 				    struct device_attribute *dev_attr,
257 				    const char *buf, size_t count)
258 {
259 	struct sensor_device_attribute *attr = to_sensor_dev_attr(dev_attr);
260 	struct max6639_data *data = dev_get_drvdata(dev);
261 	struct i2c_client *client = data->client;
262 	unsigned long val;
263 	int res;
264 
265 	res = kstrtoul(buf, 10, &val);
266 	if (res)
267 		return res;
268 
269 	mutex_lock(&data->update_lock);
270 	data->temp_ot[attr->index] = TEMP_LIMIT_TO_REG(val);
271 	i2c_smbus_write_byte_data(client,
272 				  MAX6639_REG_OT_LIMIT(attr->index),
273 				  data->temp_ot[attr->index]);
274 	mutex_unlock(&data->update_lock);
275 	return count;
276 }
277 
278 static ssize_t pwm_show(struct device *dev, struct device_attribute *dev_attr,
279 			char *buf)
280 {
281 	struct sensor_device_attribute *attr = to_sensor_dev_attr(dev_attr);
282 	struct max6639_data *data = dev_get_drvdata(dev);
283 
284 	return sprintf(buf, "%d\n", data->pwm[attr->index] * 255 / 120);
285 }
286 
287 static ssize_t pwm_store(struct device *dev,
288 			 struct device_attribute *dev_attr, const char *buf,
289 			 size_t count)
290 {
291 	struct sensor_device_attribute *attr = to_sensor_dev_attr(dev_attr);
292 	struct max6639_data *data = dev_get_drvdata(dev);
293 	struct i2c_client *client = data->client;
294 	unsigned long val;
295 	int res;
296 
297 	res = kstrtoul(buf, 10, &val);
298 	if (res)
299 		return res;
300 
301 	val = clamp_val(val, 0, 255);
302 
303 	mutex_lock(&data->update_lock);
304 	data->pwm[attr->index] = (u8)(val * 120 / 255);
305 	i2c_smbus_write_byte_data(client,
306 				  MAX6639_REG_TARGTDUTY(attr->index),
307 				  data->pwm[attr->index]);
308 	mutex_unlock(&data->update_lock);
309 	return count;
310 }
311 
312 static ssize_t fan_input_show(struct device *dev,
313 			      struct device_attribute *dev_attr, char *buf)
314 {
315 	struct max6639_data *data = max6639_update_device(dev);
316 	struct sensor_device_attribute *attr = to_sensor_dev_attr(dev_attr);
317 
318 	if (IS_ERR(data))
319 		return PTR_ERR(data);
320 
321 	return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[attr->index],
322 		       data->rpm_range));
323 }
324 
325 static ssize_t alarm_show(struct device *dev,
326 			  struct device_attribute *dev_attr, char *buf)
327 {
328 	struct max6639_data *data = max6639_update_device(dev);
329 	struct sensor_device_attribute *attr = to_sensor_dev_attr(dev_attr);
330 
331 	if (IS_ERR(data))
332 		return PTR_ERR(data);
333 
334 	return sprintf(buf, "%d\n", !!(data->status & (1 << attr->index)));
335 }
336 
337 static SENSOR_DEVICE_ATTR_RO(temp1_input, temp_input, 0);
338 static SENSOR_DEVICE_ATTR_RO(temp2_input, temp_input, 1);
339 static SENSOR_DEVICE_ATTR_RO(temp1_fault, temp_fault, 0);
340 static SENSOR_DEVICE_ATTR_RO(temp2_fault, temp_fault, 1);
341 static SENSOR_DEVICE_ATTR_RW(temp1_max, temp_max, 0);
342 static SENSOR_DEVICE_ATTR_RW(temp2_max, temp_max, 1);
343 static SENSOR_DEVICE_ATTR_RW(temp1_crit, temp_crit, 0);
344 static SENSOR_DEVICE_ATTR_RW(temp2_crit, temp_crit, 1);
345 static SENSOR_DEVICE_ATTR_RW(temp1_emergency, temp_emergency, 0);
346 static SENSOR_DEVICE_ATTR_RW(temp2_emergency, temp_emergency, 1);
347 static SENSOR_DEVICE_ATTR_RW(pwm1, pwm, 0);
348 static SENSOR_DEVICE_ATTR_RW(pwm2, pwm, 1);
349 static SENSOR_DEVICE_ATTR_RO(fan1_input, fan_input, 0);
350 static SENSOR_DEVICE_ATTR_RO(fan2_input, fan_input, 1);
351 static SENSOR_DEVICE_ATTR_RO(fan1_fault, alarm, 1);
352 static SENSOR_DEVICE_ATTR_RO(fan2_fault, alarm, 0);
353 static SENSOR_DEVICE_ATTR_RO(temp1_max_alarm, alarm, 3);
354 static SENSOR_DEVICE_ATTR_RO(temp2_max_alarm, alarm, 2);
355 static SENSOR_DEVICE_ATTR_RO(temp1_crit_alarm, alarm, 7);
356 static SENSOR_DEVICE_ATTR_RO(temp2_crit_alarm, alarm, 6);
357 static SENSOR_DEVICE_ATTR_RO(temp1_emergency_alarm, alarm, 5);
358 static SENSOR_DEVICE_ATTR_RO(temp2_emergency_alarm, alarm, 4);
359 
360 
361 static struct attribute *max6639_attrs[] = {
362 	&sensor_dev_attr_temp1_input.dev_attr.attr,
363 	&sensor_dev_attr_temp2_input.dev_attr.attr,
364 	&sensor_dev_attr_temp1_fault.dev_attr.attr,
365 	&sensor_dev_attr_temp2_fault.dev_attr.attr,
366 	&sensor_dev_attr_temp1_max.dev_attr.attr,
367 	&sensor_dev_attr_temp2_max.dev_attr.attr,
368 	&sensor_dev_attr_temp1_crit.dev_attr.attr,
369 	&sensor_dev_attr_temp2_crit.dev_attr.attr,
370 	&sensor_dev_attr_temp1_emergency.dev_attr.attr,
371 	&sensor_dev_attr_temp2_emergency.dev_attr.attr,
372 	&sensor_dev_attr_pwm1.dev_attr.attr,
373 	&sensor_dev_attr_pwm2.dev_attr.attr,
374 	&sensor_dev_attr_fan1_input.dev_attr.attr,
375 	&sensor_dev_attr_fan2_input.dev_attr.attr,
376 	&sensor_dev_attr_fan1_fault.dev_attr.attr,
377 	&sensor_dev_attr_fan2_fault.dev_attr.attr,
378 	&sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
379 	&sensor_dev_attr_temp2_max_alarm.dev_attr.attr,
380 	&sensor_dev_attr_temp1_crit_alarm.dev_attr.attr,
381 	&sensor_dev_attr_temp2_crit_alarm.dev_attr.attr,
382 	&sensor_dev_attr_temp1_emergency_alarm.dev_attr.attr,
383 	&sensor_dev_attr_temp2_emergency_alarm.dev_attr.attr,
384 	NULL
385 };
386 ATTRIBUTE_GROUPS(max6639);
387 
388 /*
389  *  returns respective index in rpm_ranges table
390  *  1 by default on invalid range
391  */
392 static int rpm_range_to_reg(int range)
393 {
394 	int i;
395 
396 	for (i = 0; i < ARRAY_SIZE(rpm_ranges); i++) {
397 		if (rpm_ranges[i] == range)
398 			return i;
399 	}
400 
401 	return 1; /* default: 4000 RPM */
402 }
403 
404 static int max6639_init_client(struct i2c_client *client,
405 			       struct max6639_data *data)
406 {
407 	struct max6639_platform_data *max6639_info =
408 		dev_get_platdata(&client->dev);
409 	int i;
410 	int rpm_range = 1; /* default: 4000 RPM */
411 	int err;
412 
413 	/* Reset chip to default values, see below for GCONFIG setup */
414 	err = i2c_smbus_write_byte_data(client, MAX6639_REG_GCONFIG,
415 				  MAX6639_GCONFIG_POR);
416 	if (err)
417 		goto exit;
418 
419 	/* Fans pulse per revolution is 2 by default */
420 	if (max6639_info && max6639_info->ppr > 0 &&
421 			max6639_info->ppr < 5)
422 		data->ppr = max6639_info->ppr;
423 	else
424 		data->ppr = 2;
425 	data->ppr -= 1;
426 
427 	if (max6639_info)
428 		rpm_range = rpm_range_to_reg(max6639_info->rpm_range);
429 	data->rpm_range = rpm_range;
430 
431 	for (i = 0; i < 2; i++) {
432 
433 		/* Set Fan pulse per revolution */
434 		err = i2c_smbus_write_byte_data(client,
435 				MAX6639_REG_FAN_PPR(i),
436 				data->ppr << 6);
437 		if (err)
438 			goto exit;
439 
440 		/* Fans config PWM, RPM */
441 		err = i2c_smbus_write_byte_data(client,
442 			MAX6639_REG_FAN_CONFIG1(i),
443 			MAX6639_FAN_CONFIG1_PWM | rpm_range);
444 		if (err)
445 			goto exit;
446 
447 		/* Fans PWM polarity high by default */
448 		if (max6639_info && max6639_info->pwm_polarity == 0)
449 			err = i2c_smbus_write_byte_data(client,
450 				MAX6639_REG_FAN_CONFIG2a(i), 0x00);
451 		else
452 			err = i2c_smbus_write_byte_data(client,
453 				MAX6639_REG_FAN_CONFIG2a(i), 0x02);
454 		if (err)
455 			goto exit;
456 
457 		/*
458 		 * /THERM full speed enable,
459 		 * PWM frequency 25kHz, see also GCONFIG below
460 		 */
461 		err = i2c_smbus_write_byte_data(client,
462 			MAX6639_REG_FAN_CONFIG3(i),
463 			MAX6639_FAN_CONFIG3_THERM_FULL_SPEED | 0x03);
464 		if (err)
465 			goto exit;
466 
467 		/* Max. temp. 80C/90C/100C */
468 		data->temp_therm[i] = 80;
469 		data->temp_alert[i] = 90;
470 		data->temp_ot[i] = 100;
471 		err = i2c_smbus_write_byte_data(client,
472 				MAX6639_REG_THERM_LIMIT(i),
473 				data->temp_therm[i]);
474 		if (err)
475 			goto exit;
476 		err = i2c_smbus_write_byte_data(client,
477 				MAX6639_REG_ALERT_LIMIT(i),
478 				data->temp_alert[i]);
479 		if (err)
480 			goto exit;
481 		err = i2c_smbus_write_byte_data(client,
482 				MAX6639_REG_OT_LIMIT(i), data->temp_ot[i]);
483 		if (err)
484 			goto exit;
485 
486 		/* PWM 120/120 (i.e. 100%) */
487 		data->pwm[i] = 120;
488 		err = i2c_smbus_write_byte_data(client,
489 				MAX6639_REG_TARGTDUTY(i), data->pwm[i]);
490 		if (err)
491 			goto exit;
492 	}
493 	/* Start monitoring */
494 	err = i2c_smbus_write_byte_data(client, MAX6639_REG_GCONFIG,
495 		MAX6639_GCONFIG_DISABLE_TIMEOUT | MAX6639_GCONFIG_CH2_LOCAL |
496 		MAX6639_GCONFIG_PWM_FREQ_HI);
497 exit:
498 	return err;
499 }
500 
501 /* Return 0 if detection is successful, -ENODEV otherwise */
502 static int max6639_detect(struct i2c_client *client,
503 			  struct i2c_board_info *info)
504 {
505 	struct i2c_adapter *adapter = client->adapter;
506 	int dev_id, manu_id;
507 
508 	if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
509 		return -ENODEV;
510 
511 	/* Actual detection via device and manufacturer ID */
512 	dev_id = i2c_smbus_read_byte_data(client, MAX6639_REG_DEVID);
513 	manu_id = i2c_smbus_read_byte_data(client, MAX6639_REG_MANUID);
514 	if (dev_id != 0x58 || manu_id != 0x4D)
515 		return -ENODEV;
516 
517 	strscpy(info->type, "max6639", I2C_NAME_SIZE);
518 
519 	return 0;
520 }
521 
522 static void max6639_regulator_disable(void *data)
523 {
524 	regulator_disable(data);
525 }
526 
527 static int max6639_probe(struct i2c_client *client)
528 {
529 	struct device *dev = &client->dev;
530 	struct max6639_data *data;
531 	struct device *hwmon_dev;
532 	int err;
533 
534 	data = devm_kzalloc(dev, sizeof(struct max6639_data), GFP_KERNEL);
535 	if (!data)
536 		return -ENOMEM;
537 
538 	data->client = client;
539 
540 	data->reg = devm_regulator_get_optional(dev, "fan");
541 	if (IS_ERR(data->reg)) {
542 		if (PTR_ERR(data->reg) != -ENODEV)
543 			return PTR_ERR(data->reg);
544 
545 		data->reg = NULL;
546 	} else {
547 		/* Spin up fans */
548 		err = regulator_enable(data->reg);
549 		if (err) {
550 			dev_err(dev, "Failed to enable fan supply: %d\n", err);
551 			return err;
552 		}
553 		err = devm_add_action_or_reset(dev, max6639_regulator_disable,
554 					       data->reg);
555 		if (err) {
556 			dev_err(dev, "Failed to register action: %d\n", err);
557 			return err;
558 		}
559 	}
560 
561 	mutex_init(&data->update_lock);
562 
563 	/* Initialize the max6639 chip */
564 	err = max6639_init_client(client, data);
565 	if (err < 0)
566 		return err;
567 
568 	hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
569 							   data,
570 							   max6639_groups);
571 	return PTR_ERR_OR_ZERO(hwmon_dev);
572 }
573 
574 static int max6639_suspend(struct device *dev)
575 {
576 	struct i2c_client *client = to_i2c_client(dev);
577 	struct max6639_data *data = dev_get_drvdata(dev);
578 	int ret = i2c_smbus_read_byte_data(client, MAX6639_REG_GCONFIG);
579 
580 	if (ret < 0)
581 		return ret;
582 
583 	if (data->reg)
584 		regulator_disable(data->reg);
585 
586 	return i2c_smbus_write_byte_data(client,
587 			MAX6639_REG_GCONFIG, ret | MAX6639_GCONFIG_STANDBY);
588 }
589 
590 static int max6639_resume(struct device *dev)
591 {
592 	struct i2c_client *client = to_i2c_client(dev);
593 	struct max6639_data *data = dev_get_drvdata(dev);
594 	int ret;
595 
596 	if (data->reg) {
597 		ret = regulator_enable(data->reg);
598 		if (ret) {
599 			dev_err(dev, "Failed to enable fan supply: %d\n", ret);
600 			return ret;
601 		}
602 	}
603 
604 	ret = i2c_smbus_read_byte_data(client, MAX6639_REG_GCONFIG);
605 	if (ret < 0)
606 		return ret;
607 
608 	return i2c_smbus_write_byte_data(client,
609 			MAX6639_REG_GCONFIG, ret & ~MAX6639_GCONFIG_STANDBY);
610 }
611 
612 static const struct i2c_device_id max6639_id[] = {
613 	{"max6639", 0},
614 	{ }
615 };
616 
617 MODULE_DEVICE_TABLE(i2c, max6639_id);
618 
619 static DEFINE_SIMPLE_DEV_PM_OPS(max6639_pm_ops, max6639_suspend, max6639_resume);
620 
621 static struct i2c_driver max6639_driver = {
622 	.class = I2C_CLASS_HWMON,
623 	.driver = {
624 		   .name = "max6639",
625 		   .pm = pm_sleep_ptr(&max6639_pm_ops),
626 		   },
627 	.probe_new = max6639_probe,
628 	.id_table = max6639_id,
629 	.detect = max6639_detect,
630 	.address_list = normal_i2c,
631 };
632 
633 module_i2c_driver(max6639_driver);
634 
635 MODULE_AUTHOR("Roland Stigge <stigge@antcom.de>");
636 MODULE_DESCRIPTION("max6639 driver");
637 MODULE_LICENSE("GPL");
638