xref: /linux/drivers/hwmon/max6639.c (revision 4b132aacb0768ac1e652cf517097ea6f237214b9)
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 #include <linux/regmap.h>
24 #include <linux/util_macros.h>
25 
26 /* Addresses to scan */
27 static const unsigned short normal_i2c[] = { 0x2c, 0x2e, 0x2f, I2C_CLIENT_END };
28 
29 /* The MAX6639 registers, valid channel numbers: 0, 1 */
30 #define MAX6639_REG_TEMP(ch)			(0x00 + (ch))
31 #define MAX6639_REG_STATUS			0x02
32 #define MAX6639_REG_OUTPUT_MASK			0x03
33 #define MAX6639_REG_GCONFIG			0x04
34 #define MAX6639_REG_TEMP_EXT(ch)		(0x05 + (ch))
35 #define MAX6639_REG_ALERT_LIMIT(ch)		(0x08 + (ch))
36 #define MAX6639_REG_OT_LIMIT(ch)		(0x0A + (ch))
37 #define MAX6639_REG_THERM_LIMIT(ch)		(0x0C + (ch))
38 #define MAX6639_REG_FAN_CONFIG1(ch)		(0x10 + (ch) * 4)
39 #define MAX6639_REG_FAN_CONFIG2a(ch)		(0x11 + (ch) * 4)
40 #define MAX6639_REG_FAN_CONFIG2b(ch)		(0x12 + (ch) * 4)
41 #define MAX6639_REG_FAN_CONFIG3(ch)		(0x13 + (ch) * 4)
42 #define MAX6639_REG_FAN_CNT(ch)			(0x20 + (ch))
43 #define MAX6639_REG_TARGET_CNT(ch)		(0x22 + (ch))
44 #define MAX6639_REG_FAN_PPR(ch)			(0x24 + (ch))
45 #define MAX6639_REG_TARGTDUTY(ch)		(0x26 + (ch))
46 #define MAX6639_REG_FAN_START_TEMP(ch)		(0x28 + (ch))
47 #define MAX6639_REG_DEVID			0x3D
48 #define MAX6639_REG_MANUID			0x3E
49 #define MAX6639_REG_DEVREV			0x3F
50 
51 /* Register bits */
52 #define MAX6639_GCONFIG_STANDBY			0x80
53 #define MAX6639_GCONFIG_POR			0x40
54 #define MAX6639_GCONFIG_DISABLE_TIMEOUT		0x20
55 #define MAX6639_GCONFIG_CH2_LOCAL		0x10
56 #define MAX6639_GCONFIG_PWM_FREQ_HI		0x08
57 
58 #define MAX6639_FAN_CONFIG1_PWM			0x80
59 #define MAX6639_FAN_CONFIG3_FREQ_MASK		0x03
60 #define MAX6639_FAN_CONFIG3_THERM_FULL_SPEED	0x40
61 
62 #define MAX6639_NUM_CHANNELS			2
63 
64 static const int rpm_ranges[] = { 2000, 4000, 8000, 16000 };
65 
66 /* Supported PWM frequency */
67 static const unsigned int freq_table[] = { 20, 33, 50, 100, 5000, 8333, 12500,
68 					   25000 };
69 
70 #define FAN_FROM_REG(val, rpm_range)	((val) == 0 || (val) == 255 ? \
71 				0 : (rpm_ranges[rpm_range] * 30) / (val))
72 #define TEMP_LIMIT_TO_REG(val)	clamp_val((val) / 1000, 0, 255)
73 
74 /*
75  * Client data (each client gets its own)
76  */
77 struct max6639_data {
78 	struct regmap *regmap;
79 	struct mutex update_lock;
80 
81 	/* Register values initialized only once */
82 	u8 ppr[MAX6639_NUM_CHANNELS];	/* Pulses per rotation 0..3 for 1..4 ppr */
83 	u8 rpm_range[MAX6639_NUM_CHANNELS]; /* Index in above rpm_ranges table */
84 
85 	/* Optional regulator for FAN supply */
86 	struct regulator *reg;
87 };
88 
89 static int max6639_temp_read_input(struct device *dev, int channel, long *temp)
90 {
91 	struct max6639_data *data = dev_get_drvdata(dev);
92 	unsigned int val;
93 	int res;
94 
95 	/*
96 	 * Lock isn't needed as MAX6639_REG_TEMP wpnt change for at least 250ms after reading
97 	 * MAX6639_REG_TEMP_EXT
98 	 */
99 	res = regmap_read(data->regmap, MAX6639_REG_TEMP_EXT(channel), &val);
100 	if (res < 0)
101 		return res;
102 
103 	*temp = val >> 5;
104 	res = regmap_read(data->regmap, MAX6639_REG_TEMP(channel), &val);
105 	if (res < 0)
106 		return res;
107 
108 	*temp |= val << 3;
109 	*temp *= 125;
110 
111 	return 0;
112 }
113 
114 static int max6639_temp_read_fault(struct device *dev, int channel, long *fault)
115 {
116 	struct max6639_data *data = dev_get_drvdata(dev);
117 	unsigned int val;
118 	int res;
119 
120 	res = regmap_read(data->regmap, MAX6639_REG_TEMP_EXT(channel), &val);
121 	if (res < 0)
122 		return res;
123 
124 	*fault = val & 1;
125 
126 	return 0;
127 }
128 
129 static int max6639_temp_read_max(struct device *dev, int channel, long *max)
130 {
131 	struct max6639_data *data = dev_get_drvdata(dev);
132 	unsigned int val;
133 	int res;
134 
135 	res = regmap_read(data->regmap, MAX6639_REG_THERM_LIMIT(channel), &val);
136 	if (res < 0)
137 		return res;
138 
139 	*max = (long)val * 1000;
140 
141 	return 0;
142 }
143 
144 static int max6639_temp_read_crit(struct device *dev, int channel, long *crit)
145 {
146 	struct max6639_data *data = dev_get_drvdata(dev);
147 	unsigned int val;
148 	int res;
149 
150 	res = regmap_read(data->regmap, MAX6639_REG_ALERT_LIMIT(channel), &val);
151 	if (res < 0)
152 		return res;
153 
154 	*crit = (long)val * 1000;
155 
156 	return 0;
157 }
158 
159 static int max6639_temp_read_emergency(struct device *dev, int channel, long *emerg)
160 {
161 	struct max6639_data *data = dev_get_drvdata(dev);
162 	unsigned int val;
163 	int res;
164 
165 	res = regmap_read(data->regmap, MAX6639_REG_OT_LIMIT(channel), &val);
166 	if (res < 0)
167 		return res;
168 
169 	*emerg = (long)val * 1000;
170 
171 	return 0;
172 }
173 
174 static int max6639_get_status(struct device *dev, unsigned int *status)
175 {
176 	struct max6639_data *data = dev_get_drvdata(dev);
177 	unsigned int val;
178 	int res;
179 
180 	res = regmap_read(data->regmap, MAX6639_REG_STATUS, &val);
181 	if (res < 0)
182 		return res;
183 
184 	*status = val;
185 
186 	return 0;
187 }
188 
189 static int max6639_temp_set_max(struct max6639_data *data, int channel, long val)
190 {
191 	int res;
192 
193 	res = regmap_write(data->regmap, MAX6639_REG_THERM_LIMIT(channel),
194 			   TEMP_LIMIT_TO_REG(val));
195 	return res;
196 }
197 
198 static int max6639_temp_set_crit(struct max6639_data *data, int channel, long val)
199 {
200 	int res;
201 
202 	res = regmap_write(data->regmap, MAX6639_REG_ALERT_LIMIT(channel), TEMP_LIMIT_TO_REG(val));
203 
204 	return res;
205 }
206 
207 static int max6639_temp_set_emergency(struct max6639_data *data, int channel, long val)
208 {
209 	int res;
210 
211 	res = regmap_write(data->regmap, MAX6639_REG_OT_LIMIT(channel), TEMP_LIMIT_TO_REG(val));
212 
213 	return res;
214 }
215 
216 static int max6639_read_fan(struct device *dev, u32 attr, int channel,
217 			    long *fan_val)
218 {
219 	struct max6639_data *data = dev_get_drvdata(dev);
220 	unsigned int val;
221 	int res;
222 
223 	switch (attr) {
224 	case hwmon_fan_input:
225 		res = regmap_read(data->regmap, MAX6639_REG_FAN_CNT(channel), &val);
226 		if (res < 0)
227 			return res;
228 		*fan_val = FAN_FROM_REG(val, data->rpm_range[channel]);
229 		return 0;
230 	case hwmon_fan_fault:
231 		res = max6639_get_status(dev, &val);
232 		if (res < 0)
233 			return res;
234 		*fan_val = !!(val & BIT(1 - channel));
235 		return 0;
236 	case hwmon_fan_pulses:
237 		*fan_val = data->ppr[channel];
238 		return 0;
239 	default:
240 		return -EOPNOTSUPP;
241 	}
242 }
243 
244 static int max6639_set_ppr(struct max6639_data *data, int channel, u8 ppr)
245 {
246 	/* Decrement the PPR value and shift left by 6 to match the register format */
247 	return regmap_write(data->regmap, MAX6639_REG_FAN_PPR(channel), ppr-- << 6);
248 }
249 
250 static int max6639_write_fan(struct device *dev, u32 attr, int channel,
251 			     long val)
252 {
253 	struct max6639_data *data = dev_get_drvdata(dev);
254 	int err;
255 
256 	switch (attr) {
257 	case hwmon_fan_pulses:
258 		if (val <= 0 || val > 4)
259 			return -EINVAL;
260 
261 		mutex_lock(&data->update_lock);
262 		/* Set Fan pulse per revolution */
263 		err = max6639_set_ppr(data, channel, val);
264 		if (err < 0) {
265 			mutex_unlock(&data->update_lock);
266 			return err;
267 		}
268 		data->ppr[channel] = val;
269 
270 		mutex_unlock(&data->update_lock);
271 		return 0;
272 	default:
273 		return -EOPNOTSUPP;
274 	}
275 }
276 
277 static umode_t max6639_fan_is_visible(const void *_data, u32 attr, int channel)
278 {
279 	switch (attr) {
280 	case hwmon_fan_input:
281 	case hwmon_fan_fault:
282 		return 0444;
283 	case hwmon_fan_pulses:
284 		return 0644;
285 	default:
286 		return 0;
287 	}
288 }
289 
290 static int max6639_read_pwm(struct device *dev, u32 attr, int channel,
291 			    long *pwm_val)
292 {
293 	struct max6639_data *data = dev_get_drvdata(dev);
294 	unsigned int val;
295 	int res;
296 	u8 i;
297 
298 	switch (attr) {
299 	case hwmon_pwm_input:
300 		res = regmap_read(data->regmap, MAX6639_REG_TARGTDUTY(channel), &val);
301 		if (res < 0)
302 			return res;
303 		*pwm_val = val * 255 / 120;
304 		return 0;
305 	case hwmon_pwm_freq:
306 		mutex_lock(&data->update_lock);
307 		res = regmap_read(data->regmap, MAX6639_REG_FAN_CONFIG3(channel), &val);
308 		if (res < 0) {
309 			mutex_unlock(&data->update_lock);
310 			return res;
311 		}
312 		i = val & MAX6639_FAN_CONFIG3_FREQ_MASK;
313 
314 		res = regmap_read(data->regmap, MAX6639_REG_GCONFIG, &val);
315 		if (res < 0) {
316 			mutex_unlock(&data->update_lock);
317 			return res;
318 		}
319 
320 		if (val & MAX6639_GCONFIG_PWM_FREQ_HI)
321 			i |= 0x4;
322 		i &= 0x7;
323 		*pwm_val = freq_table[i];
324 
325 		mutex_unlock(&data->update_lock);
326 		return 0;
327 	default:
328 		return -EOPNOTSUPP;
329 	}
330 }
331 
332 static int max6639_write_pwm(struct device *dev, u32 attr, int channel,
333 			     long val)
334 {
335 	struct max6639_data *data = dev_get_drvdata(dev);
336 	int err;
337 	u8 i;
338 
339 	switch (attr) {
340 	case hwmon_pwm_input:
341 		if (val < 0 || val > 255)
342 			return -EINVAL;
343 		err = regmap_write(data->regmap, MAX6639_REG_TARGTDUTY(channel),
344 				   val * 120 / 255);
345 		return err;
346 	case hwmon_pwm_freq:
347 		val = clamp_val(val, 0, 25000);
348 
349 		i = find_closest(val, freq_table, ARRAY_SIZE(freq_table));
350 
351 		mutex_lock(&data->update_lock);
352 		err = regmap_update_bits(data->regmap, MAX6639_REG_FAN_CONFIG3(channel),
353 					 MAX6639_FAN_CONFIG3_FREQ_MASK, i);
354 		if (err < 0) {
355 			mutex_unlock(&data->update_lock);
356 			return err;
357 		}
358 
359 		if (i >> 2)
360 			err = regmap_set_bits(data->regmap, MAX6639_REG_GCONFIG,
361 					      MAX6639_GCONFIG_PWM_FREQ_HI);
362 		else
363 			err = regmap_clear_bits(data->regmap, MAX6639_REG_GCONFIG,
364 						MAX6639_GCONFIG_PWM_FREQ_HI);
365 
366 		mutex_unlock(&data->update_lock);
367 		return err;
368 	default:
369 		return -EOPNOTSUPP;
370 	}
371 }
372 
373 static umode_t max6639_pwm_is_visible(const void *_data, u32 attr, int channel)
374 {
375 	switch (attr) {
376 	case hwmon_pwm_input:
377 	case hwmon_pwm_freq:
378 		return 0644;
379 	default:
380 		return 0;
381 	}
382 }
383 
384 static int max6639_read_temp(struct device *dev, u32 attr, int channel,
385 			     long *val)
386 {
387 	unsigned int status;
388 	int res;
389 
390 	switch (attr) {
391 	case hwmon_temp_input:
392 		res = max6639_temp_read_input(dev, channel, val);
393 		return res;
394 	case hwmon_temp_fault:
395 		res = max6639_temp_read_fault(dev, channel, val);
396 		return res;
397 	case hwmon_temp_max:
398 		res = max6639_temp_read_max(dev, channel, val);
399 		return res;
400 	case hwmon_temp_crit:
401 		res = max6639_temp_read_crit(dev, channel, val);
402 		return res;
403 	case hwmon_temp_emergency:
404 		res = max6639_temp_read_emergency(dev, channel, val);
405 		return res;
406 	case hwmon_temp_max_alarm:
407 		res = max6639_get_status(dev, &status);
408 		if (res < 0)
409 			return res;
410 		*val = !!(status & BIT(3 - channel));
411 		return 0;
412 	case hwmon_temp_crit_alarm:
413 		res = max6639_get_status(dev, &status);
414 		if (res < 0)
415 			return res;
416 		*val = !!(status & BIT(7 - channel));
417 		return 0;
418 	case hwmon_temp_emergency_alarm:
419 		res = max6639_get_status(dev, &status);
420 		if (res < 0)
421 			return res;
422 		*val = !!(status & BIT(5 - channel));
423 		return 0;
424 	default:
425 		return -EOPNOTSUPP;
426 	}
427 }
428 
429 static int max6639_write_temp(struct device *dev, u32 attr, int channel,
430 			      long val)
431 {
432 	struct max6639_data *data = dev_get_drvdata(dev);
433 
434 	switch (attr) {
435 	case hwmon_temp_max:
436 		return max6639_temp_set_max(data, channel, val);
437 	case hwmon_temp_crit:
438 		return max6639_temp_set_crit(data, channel, val);
439 	case hwmon_temp_emergency:
440 		return max6639_temp_set_emergency(data, channel, val);
441 	default:
442 		return -EOPNOTSUPP;
443 	}
444 }
445 
446 static umode_t max6639_temp_is_visible(const void *_data, u32 attr, int channel)
447 {
448 	switch (attr) {
449 	case hwmon_temp_input:
450 	case hwmon_temp_fault:
451 	case hwmon_temp_max_alarm:
452 	case hwmon_temp_crit_alarm:
453 	case hwmon_temp_emergency_alarm:
454 		return 0444;
455 	case hwmon_temp_max:
456 	case hwmon_temp_crit:
457 	case hwmon_temp_emergency:
458 		return 0644;
459 	default:
460 		return 0;
461 	}
462 }
463 
464 static int max6639_read(struct device *dev, enum hwmon_sensor_types type,
465 			u32 attr, int channel, long *val)
466 {
467 	switch (type) {
468 	case hwmon_fan:
469 		return max6639_read_fan(dev, attr, channel, val);
470 	case hwmon_pwm:
471 		return max6639_read_pwm(dev, attr, channel, val);
472 	case hwmon_temp:
473 		return max6639_read_temp(dev, attr, channel, val);
474 	default:
475 		return -EOPNOTSUPP;
476 	}
477 }
478 
479 static int max6639_write(struct device *dev, enum hwmon_sensor_types type,
480 			 u32 attr, int channel, long val)
481 {
482 	switch (type) {
483 	case hwmon_fan:
484 		return max6639_write_fan(dev, attr, channel, val);
485 	case hwmon_pwm:
486 		return max6639_write_pwm(dev, attr, channel, val);
487 	case hwmon_temp:
488 		return max6639_write_temp(dev, attr, channel, val);
489 	default:
490 		return -EOPNOTSUPP;
491 	}
492 }
493 
494 static umode_t max6639_is_visible(const void *data,
495 				  enum hwmon_sensor_types type,
496 				  u32 attr, int channel)
497 {
498 	switch (type) {
499 	case hwmon_fan:
500 		return max6639_fan_is_visible(data, attr, channel);
501 	case hwmon_pwm:
502 		return max6639_pwm_is_visible(data, attr, channel);
503 	case hwmon_temp:
504 		return max6639_temp_is_visible(data, attr, channel);
505 	default:
506 		return 0;
507 	}
508 }
509 
510 static const struct hwmon_channel_info * const max6639_info[] = {
511 	HWMON_CHANNEL_INFO(fan,
512 			   HWMON_F_INPUT | HWMON_F_FAULT | HWMON_F_PULSES,
513 			   HWMON_F_INPUT | HWMON_F_FAULT | HWMON_F_PULSES),
514 	HWMON_CHANNEL_INFO(pwm,
515 			   HWMON_PWM_INPUT | HWMON_PWM_FREQ,
516 			   HWMON_PWM_INPUT | HWMON_PWM_FREQ),
517 	HWMON_CHANNEL_INFO(temp,
518 			   HWMON_T_INPUT | HWMON_T_FAULT | HWMON_T_MAX | HWMON_T_MAX_ALARM |
519 			   HWMON_T_CRIT | HWMON_T_CRIT_ALARM | HWMON_T_EMERGENCY |
520 			   HWMON_T_EMERGENCY_ALARM,
521 			   HWMON_T_INPUT | HWMON_T_FAULT | HWMON_T_MAX | HWMON_T_MAX_ALARM |
522 			   HWMON_T_CRIT | HWMON_T_CRIT_ALARM | HWMON_T_EMERGENCY |
523 			   HWMON_T_EMERGENCY_ALARM),
524 	NULL
525 };
526 
527 static const struct hwmon_ops max6639_hwmon_ops = {
528 	.is_visible = max6639_is_visible,
529 	.read = max6639_read,
530 	.write = max6639_write,
531 };
532 
533 static const struct hwmon_chip_info max6639_chip_info = {
534 	.ops = &max6639_hwmon_ops,
535 	.info = max6639_info,
536 };
537 
538 /*
539  *  returns respective index in rpm_ranges table
540  *  1 by default on invalid range
541  */
542 static int rpm_range_to_reg(int range)
543 {
544 	int i;
545 
546 	for (i = 0; i < ARRAY_SIZE(rpm_ranges); i++) {
547 		if (rpm_ranges[i] == range)
548 			return i;
549 	}
550 
551 	return 1; /* default: 4000 RPM */
552 }
553 
554 static int max6639_init_client(struct i2c_client *client,
555 			       struct max6639_data *data)
556 {
557 	struct max6639_platform_data *max6639_info =
558 		dev_get_platdata(&client->dev);
559 	int i;
560 	int rpm_range = 1; /* default: 4000 RPM */
561 	int err, ppr;
562 
563 	/* Reset chip to default values, see below for GCONFIG setup */
564 	err = regmap_write(data->regmap, MAX6639_REG_GCONFIG, MAX6639_GCONFIG_POR);
565 	if (err)
566 		return err;
567 
568 	/* Fans pulse per revolution is 2 by default */
569 	if (max6639_info && max6639_info->ppr > 0 &&
570 			max6639_info->ppr < 5)
571 		ppr = max6639_info->ppr;
572 	else
573 		ppr = 2;
574 
575 	data->ppr[0] = ppr;
576 	data->ppr[1] = ppr;
577 
578 	if (max6639_info)
579 		rpm_range = rpm_range_to_reg(max6639_info->rpm_range);
580 	data->rpm_range[0] = rpm_range;
581 	data->rpm_range[1] = rpm_range;
582 
583 	for (i = 0; i < MAX6639_NUM_CHANNELS; i++) {
584 		/* Set Fan pulse per revolution */
585 		err = max6639_set_ppr(data, i, data->ppr[i]);
586 		if (err)
587 			return err;
588 
589 		/* Fans config PWM, RPM */
590 		err = regmap_write(data->regmap, MAX6639_REG_FAN_CONFIG1(i),
591 				   MAX6639_FAN_CONFIG1_PWM | data->rpm_range[i]);
592 		if (err)
593 			return err;
594 
595 		/* Fans PWM polarity high by default */
596 		if (max6639_info) {
597 			if (max6639_info->pwm_polarity == 0)
598 				err = regmap_write(data->regmap, MAX6639_REG_FAN_CONFIG2a(i), 0x00);
599 			else
600 				err = regmap_write(data->regmap, MAX6639_REG_FAN_CONFIG2a(i), 0x02);
601 		}
602 		if (err)
603 			return err;
604 
605 		/*
606 		 * /THERM full speed enable,
607 		 * PWM frequency 25kHz, see also GCONFIG below
608 		 */
609 		err = regmap_write(data->regmap, MAX6639_REG_FAN_CONFIG3(i),
610 				   MAX6639_FAN_CONFIG3_THERM_FULL_SPEED | 0x03);
611 		if (err)
612 			return err;
613 
614 		/* Max. temp. 80C/90C/100C */
615 		err = regmap_write(data->regmap, MAX6639_REG_THERM_LIMIT(i), 80);
616 		if (err)
617 			return err;
618 		err = regmap_write(data->regmap, MAX6639_REG_ALERT_LIMIT(i), 90);
619 		if (err)
620 			return err;
621 		err = regmap_write(data->regmap, MAX6639_REG_OT_LIMIT(i), 100);
622 		if (err)
623 			return err;
624 
625 		/* PWM 120/120 (i.e. 100%) */
626 		err = regmap_write(data->regmap, MAX6639_REG_TARGTDUTY(i), 120);
627 		if (err)
628 			return err;
629 	}
630 	/* Start monitoring */
631 	return regmap_write(data->regmap, MAX6639_REG_GCONFIG,
632 			    MAX6639_GCONFIG_DISABLE_TIMEOUT | MAX6639_GCONFIG_CH2_LOCAL |
633 			    MAX6639_GCONFIG_PWM_FREQ_HI);
634 
635 }
636 
637 /* Return 0 if detection is successful, -ENODEV otherwise */
638 static int max6639_detect(struct i2c_client *client,
639 			  struct i2c_board_info *info)
640 {
641 	struct i2c_adapter *adapter = client->adapter;
642 	int dev_id, manu_id;
643 
644 	if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
645 		return -ENODEV;
646 
647 	/* Actual detection via device and manufacturer ID */
648 	dev_id = i2c_smbus_read_byte_data(client, MAX6639_REG_DEVID);
649 	manu_id = i2c_smbus_read_byte_data(client, MAX6639_REG_MANUID);
650 	if (dev_id != 0x58 || manu_id != 0x4D)
651 		return -ENODEV;
652 
653 	strscpy(info->type, "max6639", I2C_NAME_SIZE);
654 
655 	return 0;
656 }
657 
658 static void max6639_regulator_disable(void *data)
659 {
660 	regulator_disable(data);
661 }
662 
663 static bool max6639_regmap_is_volatile(struct device *dev, unsigned int reg)
664 {
665 	switch (reg) {
666 	case MAX6639_REG_TEMP(0):
667 	case MAX6639_REG_TEMP_EXT(0):
668 	case MAX6639_REG_TEMP(1):
669 	case MAX6639_REG_TEMP_EXT(1):
670 	case MAX6639_REG_STATUS:
671 	case MAX6639_REG_FAN_CNT(0):
672 	case MAX6639_REG_FAN_CNT(1):
673 	case MAX6639_REG_TARGTDUTY(0):
674 	case MAX6639_REG_TARGTDUTY(1):
675 		return true;
676 	default:
677 		return false;
678 	}
679 }
680 
681 static const struct regmap_config max6639_regmap_config = {
682 	.reg_bits = 8,
683 	.val_bits = 8,
684 	.max_register = MAX6639_REG_DEVREV,
685 	.cache_type = REGCACHE_MAPLE,
686 	.volatile_reg = max6639_regmap_is_volatile,
687 };
688 
689 static int max6639_probe(struct i2c_client *client)
690 {
691 	struct device *dev = &client->dev;
692 	struct max6639_data *data;
693 	struct device *hwmon_dev;
694 	int err;
695 
696 	data = devm_kzalloc(dev, sizeof(struct max6639_data), GFP_KERNEL);
697 	if (!data)
698 		return -ENOMEM;
699 
700 	data->regmap = devm_regmap_init_i2c(client, &max6639_regmap_config);
701 	if (IS_ERR(data->regmap))
702 		return dev_err_probe(dev,
703 				     PTR_ERR(data->regmap),
704 				     "regmap initialization failed\n");
705 
706 	data->reg = devm_regulator_get_optional(dev, "fan");
707 	if (IS_ERR(data->reg)) {
708 		if (PTR_ERR(data->reg) != -ENODEV)
709 			return PTR_ERR(data->reg);
710 
711 		data->reg = NULL;
712 	} else {
713 		/* Spin up fans */
714 		err = regulator_enable(data->reg);
715 		if (err) {
716 			dev_err(dev, "Failed to enable fan supply: %d\n", err);
717 			return err;
718 		}
719 		err = devm_add_action_or_reset(dev, max6639_regulator_disable,
720 					       data->reg);
721 		if (err) {
722 			dev_err(dev, "Failed to register action: %d\n", err);
723 			return err;
724 		}
725 	}
726 
727 	mutex_init(&data->update_lock);
728 
729 	/* Initialize the max6639 chip */
730 	err = max6639_init_client(client, data);
731 	if (err < 0)
732 		return err;
733 
734 	hwmon_dev = devm_hwmon_device_register_with_info(dev, client->name,
735 							 data, &max6639_chip_info,
736 							 NULL);
737 
738 	return PTR_ERR_OR_ZERO(hwmon_dev);
739 }
740 
741 static int max6639_suspend(struct device *dev)
742 {
743 	struct max6639_data *data = dev_get_drvdata(dev);
744 
745 	if (data->reg)
746 		regulator_disable(data->reg);
747 
748 	return regmap_write_bits(data->regmap, MAX6639_REG_GCONFIG, MAX6639_GCONFIG_STANDBY,
749 				 MAX6639_GCONFIG_STANDBY);
750 }
751 
752 static int max6639_resume(struct device *dev)
753 {
754 	struct max6639_data *data = dev_get_drvdata(dev);
755 	int ret;
756 
757 	if (data->reg) {
758 		ret = regulator_enable(data->reg);
759 		if (ret) {
760 			dev_err(dev, "Failed to enable fan supply: %d\n", ret);
761 			return ret;
762 		}
763 	}
764 
765 	return regmap_write_bits(data->regmap, MAX6639_REG_GCONFIG, MAX6639_GCONFIG_STANDBY,
766 				 ~MAX6639_GCONFIG_STANDBY);
767 }
768 
769 static const struct i2c_device_id max6639_id[] = {
770 	{"max6639"},
771 	{ }
772 };
773 
774 MODULE_DEVICE_TABLE(i2c, max6639_id);
775 
776 static DEFINE_SIMPLE_DEV_PM_OPS(max6639_pm_ops, max6639_suspend, max6639_resume);
777 
778 static const struct of_device_id max6639_of_match[] = {
779 	{ .compatible = "maxim,max6639", },
780 	{ },
781 };
782 
783 static struct i2c_driver max6639_driver = {
784 	.class = I2C_CLASS_HWMON,
785 	.driver = {
786 		   .name = "max6639",
787 		   .pm = pm_sleep_ptr(&max6639_pm_ops),
788 		   .of_match_table = max6639_of_match,
789 		   },
790 	.probe = max6639_probe,
791 	.id_table = max6639_id,
792 	.detect = max6639_detect,
793 	.address_list = normal_i2c,
794 };
795 
796 module_i2c_driver(max6639_driver);
797 
798 MODULE_AUTHOR("Roland Stigge <stigge@antcom.de>");
799 MODULE_DESCRIPTION("max6639 driver");
800 MODULE_LICENSE("GPL");
801