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