xref: /linux/drivers/hwmon/max6650.c (revision c9d468ba1bf222e61f886b46748696cd940e43a4)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * max6650.c - Part of lm_sensors, Linux kernel modules for hardware
4  *             monitoring.
5  *
6  * (C) 2007 by Hans J. Koch <hjk@hansjkoch.de>
7  *
8  * based on code written by John Morris <john.morris@spirentcom.com>
9  * Copyright (c) 2003 Spirent Communications
10  * and Claus Gindhart <claus.gindhart@kontron.com>
11  *
12  * This module has only been tested with the MAX6650 chip. It should
13  * also work with the MAX6651. It does not distinguish max6650 and max6651
14  * chips.
15  *
16  * The datasheet was last seen at:
17  *
18  *        http://pdfserv.maxim-ic.com/en/ds/MAX6650-MAX6651.pdf
19  */
20 
21 #include <linux/module.h>
22 #include <linux/init.h>
23 #include <linux/slab.h>
24 #include <linux/jiffies.h>
25 #include <linux/i2c.h>
26 #include <linux/hwmon.h>
27 #include <linux/hwmon-sysfs.h>
28 #include <linux/err.h>
29 #include <linux/of.h>
30 #include <linux/sysfs.h>
31 #include <linux/thermal.h>
32 
33 /*
34  * Insmod parameters
35  */
36 
37 /* fan_voltage: 5=5V fan, 12=12V fan, 0=don't change */
38 static int fan_voltage;
39 /* prescaler: Possible values are 1, 2, 4, 8, 16 or 0 for don't change */
40 static int prescaler;
41 /* clock: The clock frequency of the chip (max6651 can be clocked externally) */
42 static int clock = 254000;
43 
44 module_param(fan_voltage, int, 0444);
45 module_param(prescaler, int, 0444);
46 module_param(clock, int, 0444);
47 
48 /*
49  * MAX 6650/6651 registers
50  */
51 
52 #define MAX6650_REG_SPEED	0x00
53 #define MAX6650_REG_CONFIG	0x02
54 #define MAX6650_REG_GPIO_DEF	0x04
55 #define MAX6650_REG_DAC		0x06
56 #define MAX6650_REG_ALARM_EN	0x08
57 #define MAX6650_REG_ALARM	0x0A
58 #define MAX6650_REG_TACH0	0x0C
59 #define MAX6650_REG_TACH1	0x0E
60 #define MAX6650_REG_TACH2	0x10
61 #define MAX6650_REG_TACH3	0x12
62 #define MAX6650_REG_GPIO_STAT	0x14
63 #define MAX6650_REG_COUNT	0x16
64 
65 /*
66  * Config register bits
67  */
68 
69 #define MAX6650_CFG_V12			0x08
70 #define MAX6650_CFG_PRESCALER_MASK	0x07
71 #define MAX6650_CFG_PRESCALER_2		0x01
72 #define MAX6650_CFG_PRESCALER_4		0x02
73 #define MAX6650_CFG_PRESCALER_8		0x03
74 #define MAX6650_CFG_PRESCALER_16	0x04
75 #define MAX6650_CFG_MODE_MASK		0x30
76 #define MAX6650_CFG_MODE_ON		0x00
77 #define MAX6650_CFG_MODE_OFF		0x10
78 #define MAX6650_CFG_MODE_CLOSED_LOOP	0x20
79 #define MAX6650_CFG_MODE_OPEN_LOOP	0x30
80 #define MAX6650_COUNT_MASK		0x03
81 
82 /*
83  * Alarm status register bits
84  */
85 
86 #define MAX6650_ALRM_MAX	0x01
87 #define MAX6650_ALRM_MIN	0x02
88 #define MAX6650_ALRM_TACH	0x04
89 #define MAX6650_ALRM_GPIO1	0x08
90 #define MAX6650_ALRM_GPIO2	0x10
91 
92 /* Minimum and maximum values of the FAN-RPM */
93 #define FAN_RPM_MIN 240
94 #define FAN_RPM_MAX 30000
95 
96 #define DIV_FROM_REG(reg)	(1 << ((reg) & 7))
97 #define DAC_LIMIT(v12)		((v12) ? 180 : 76)
98 
99 /*
100  * Client data (each client gets its own)
101  */
102 
103 struct max6650_data {
104 	struct i2c_client *client;
105 	struct mutex update_lock; /* protect alarm register updates */
106 	int nr_fans;
107 	bool valid; /* false until following fields are valid */
108 	unsigned long last_updated; /* in jiffies */
109 
110 	/* register values */
111 	u8 speed;
112 	u8 config;
113 	u8 tach[4];
114 	u8 count;
115 	u8 dac;
116 	u8 alarm;
117 	u8 alarm_en;
118 	unsigned long cooling_dev_state;
119 };
120 
121 static const u8 tach_reg[] = {
122 	MAX6650_REG_TACH0,
123 	MAX6650_REG_TACH1,
124 	MAX6650_REG_TACH2,
125 	MAX6650_REG_TACH3,
126 };
127 
128 static const struct of_device_id __maybe_unused max6650_dt_match[] = {
129 	{
130 		.compatible = "maxim,max6650",
131 		.data = (void *)1
132 	},
133 	{
134 		.compatible = "maxim,max6651",
135 		.data = (void *)4
136 	},
137 	{ },
138 };
139 MODULE_DEVICE_TABLE(of, max6650_dt_match);
140 
141 static int dac_to_pwm(int dac, bool v12)
142 {
143 	/*
144 	 * Useful range for dac is 0-180 for 12V fans and 0-76 for 5V fans.
145 	 * Lower DAC values mean higher speeds.
146 	 */
147 	return clamp_val(255 - (255 * dac) / DAC_LIMIT(v12), 0, 255);
148 }
149 
150 static u8 pwm_to_dac(unsigned int pwm, bool v12)
151 {
152 	int limit = DAC_LIMIT(v12);
153 
154 	return limit - (limit * pwm) / 255;
155 }
156 
157 static struct max6650_data *max6650_update_device(struct device *dev)
158 {
159 	struct max6650_data *data = dev_get_drvdata(dev);
160 	struct i2c_client *client = data->client;
161 	int reg, err = 0;
162 	int i;
163 
164 	mutex_lock(&data->update_lock);
165 
166 	if (time_after(jiffies, data->last_updated + HZ) || !data->valid) {
167 		for (i = 0; i < data->nr_fans; i++) {
168 			reg = i2c_smbus_read_byte_data(client, tach_reg[i]);
169 			if (reg < 0) {
170 				err = reg;
171 				goto error;
172 			}
173 			data->tach[i] = reg;
174 		}
175 
176 		/*
177 		 * Alarms are cleared on read in case the condition that
178 		 * caused the alarm is removed. Keep the value latched here
179 		 * for providing the register through different alarm files.
180 		 */
181 		reg = i2c_smbus_read_byte_data(client, MAX6650_REG_ALARM);
182 		if (reg < 0) {
183 			err = reg;
184 			goto error;
185 		}
186 		data->alarm |= reg;
187 		data->last_updated = jiffies;
188 		data->valid = true;
189 	}
190 
191 error:
192 	mutex_unlock(&data->update_lock);
193 	if (err)
194 		data = ERR_PTR(err);
195 	return data;
196 }
197 
198 /*
199  * Change the operating mode of the chip (if needed).
200  * mode is one of the MAX6650_CFG_MODE_* values.
201  */
202 static int max6650_set_operating_mode(struct max6650_data *data, u8 mode)
203 {
204 	int result;
205 	u8 config = data->config;
206 
207 	if (mode == (config & MAX6650_CFG_MODE_MASK))
208 		return 0;
209 
210 	config = (config & ~MAX6650_CFG_MODE_MASK) | mode;
211 
212 	result = i2c_smbus_write_byte_data(data->client, MAX6650_REG_CONFIG,
213 					   config);
214 	if (result < 0)
215 		return result;
216 
217 	data->config = config;
218 
219 	return 0;
220 }
221 
222 /*
223  * Set the fan speed to the specified RPM (or read back the RPM setting).
224  * This works in closed loop mode only. Use pwm1 for open loop speed setting.
225  *
226  * The MAX6650/1 will automatically control fan speed when in closed loop
227  * mode.
228  *
229  * Assumptions:
230  *
231  * 1) The MAX6650/1 internal 254kHz clock frequency is set correctly. Use
232  *    the clock module parameter if you need to fine tune this.
233  *
234  * 2) The prescaler (low three bits of the config register) has already
235  *    been set to an appropriate value. Use the prescaler module parameter
236  *    if your BIOS doesn't initialize the chip properly.
237  *
238  * The relevant equations are given on pages 21 and 22 of the datasheet.
239  *
240  * From the datasheet, the relevant equation when in regulation is:
241  *
242  *    [fCLK / (128 x (KTACH + 1))] = 2 x FanSpeed / KSCALE
243  *
244  * where:
245  *
246  *    fCLK is the oscillator frequency (either the 254kHz internal
247  *         oscillator or the externally applied clock)
248  *
249  *    KTACH is the value in the speed register
250  *
251  *    FanSpeed is the speed of the fan in rps
252  *
253  *    KSCALE is the prescaler value (1, 2, 4, 8, or 16)
254  *
255  * When reading, we need to solve for FanSpeed. When writing, we need to
256  * solve for KTACH.
257  *
258  * Note: this tachometer is completely separate from the tachometers
259  * used to measure the fan speeds. Only one fan's speed (fan1) is
260  * controlled.
261  */
262 
263 static int max6650_set_target(struct max6650_data *data, unsigned long rpm)
264 {
265 	int kscale, ktach;
266 
267 	if (rpm == 0)
268 		return max6650_set_operating_mode(data, MAX6650_CFG_MODE_OFF);
269 
270 	rpm = clamp_val(rpm, FAN_RPM_MIN, FAN_RPM_MAX);
271 
272 	/*
273 	 * Divide the required speed by 60 to get from rpm to rps, then
274 	 * use the datasheet equation:
275 	 *
276 	 *     KTACH = [(fCLK x KSCALE) / (256 x FanSpeed)] - 1
277 	 */
278 
279 	kscale = DIV_FROM_REG(data->config);
280 	ktach = ((clock * kscale) / (256 * rpm / 60)) - 1;
281 	if (ktach < 0)
282 		ktach = 0;
283 	if (ktach > 255)
284 		ktach = 255;
285 	data->speed = ktach;
286 
287 	return i2c_smbus_write_byte_data(data->client, MAX6650_REG_SPEED,
288 					 data->speed);
289 }
290 
291 /*
292  * Get gpio alarm status:
293  * Possible values:
294  * 0 = no alarm
295  * 1 = alarm
296  */
297 
298 static ssize_t alarm_show(struct device *dev,
299 			  struct device_attribute *devattr, char *buf)
300 {
301 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
302 	struct max6650_data *data = max6650_update_device(dev);
303 	bool alarm;
304 
305 	if (IS_ERR(data))
306 		return PTR_ERR(data);
307 
308 	alarm = data->alarm & attr->index;
309 	if (alarm) {
310 		mutex_lock(&data->update_lock);
311 		data->alarm &= ~attr->index;
312 		data->valid = false;
313 		mutex_unlock(&data->update_lock);
314 	}
315 
316 	return sysfs_emit(buf, "%d\n", alarm);
317 }
318 
319 static SENSOR_DEVICE_ATTR_RO(gpio1_alarm, alarm, MAX6650_ALRM_GPIO1);
320 static SENSOR_DEVICE_ATTR_RO(gpio2_alarm, alarm, MAX6650_ALRM_GPIO2);
321 
322 static umode_t max6650_attrs_visible(struct kobject *kobj, struct attribute *a,
323 				     int n)
324 {
325 	struct device *dev = kobj_to_dev(kobj);
326 	struct max6650_data *data = dev_get_drvdata(dev);
327 	struct device_attribute *devattr;
328 
329 	/*
330 	 * Hide the alarms that have not been enabled by the firmware
331 	 */
332 
333 	devattr = container_of(a, struct device_attribute, attr);
334 	if (devattr == &sensor_dev_attr_gpio1_alarm.dev_attr ||
335 	    devattr == &sensor_dev_attr_gpio2_alarm.dev_attr) {
336 		if (!(data->alarm_en & to_sensor_dev_attr(devattr)->index))
337 			return 0;
338 	}
339 
340 	return a->mode;
341 }
342 
343 static struct attribute *max6650_attrs[] = {
344 	&sensor_dev_attr_gpio1_alarm.dev_attr.attr,
345 	&sensor_dev_attr_gpio2_alarm.dev_attr.attr,
346 	NULL
347 };
348 
349 static const struct attribute_group max6650_group = {
350 	.attrs = max6650_attrs,
351 	.is_visible = max6650_attrs_visible,
352 };
353 
354 static const struct attribute_group *max6650_groups[] = {
355 	&max6650_group,
356 	NULL
357 };
358 
359 static int max6650_init_client(struct max6650_data *data,
360 			       struct i2c_client *client)
361 {
362 	struct device *dev = &client->dev;
363 	int reg;
364 	int err;
365 	u32 voltage;
366 	u32 prescale;
367 	u32 target_rpm;
368 
369 	if (of_property_read_u32(dev->of_node, "maxim,fan-microvolt",
370 				 &voltage))
371 		voltage = fan_voltage;
372 	else
373 		voltage /= 1000000; /* Microvolts to volts */
374 	if (of_property_read_u32(dev->of_node, "maxim,fan-prescale",
375 				 &prescale))
376 		prescale = prescaler;
377 
378 	reg = i2c_smbus_read_byte_data(client, MAX6650_REG_CONFIG);
379 	if (reg < 0) {
380 		dev_err(dev, "Error reading config register, aborting.\n");
381 		return reg;
382 	}
383 
384 	switch (voltage) {
385 	case 0:
386 		break;
387 	case 5:
388 		reg &= ~MAX6650_CFG_V12;
389 		break;
390 	case 12:
391 		reg |= MAX6650_CFG_V12;
392 		break;
393 	default:
394 		dev_err(dev, "illegal value for fan_voltage (%d)\n", voltage);
395 	}
396 
397 	switch (prescale) {
398 	case 0:
399 		break;
400 	case 1:
401 		reg &= ~MAX6650_CFG_PRESCALER_MASK;
402 		break;
403 	case 2:
404 		reg = (reg & ~MAX6650_CFG_PRESCALER_MASK)
405 			 | MAX6650_CFG_PRESCALER_2;
406 		break;
407 	case  4:
408 		reg = (reg & ~MAX6650_CFG_PRESCALER_MASK)
409 			 | MAX6650_CFG_PRESCALER_4;
410 		break;
411 	case  8:
412 		reg = (reg & ~MAX6650_CFG_PRESCALER_MASK)
413 			 | MAX6650_CFG_PRESCALER_8;
414 		break;
415 	case 16:
416 		reg = (reg & ~MAX6650_CFG_PRESCALER_MASK)
417 			 | MAX6650_CFG_PRESCALER_16;
418 		break;
419 	default:
420 		dev_err(dev, "illegal value for prescaler (%d)\n", prescale);
421 	}
422 
423 	dev_info(dev, "Fan voltage: %dV, prescaler: %d.\n",
424 		 (reg & MAX6650_CFG_V12) ? 12 : 5,
425 		 1 << (reg & MAX6650_CFG_PRESCALER_MASK));
426 
427 	err = i2c_smbus_write_byte_data(client, MAX6650_REG_CONFIG, reg);
428 	if (err) {
429 		dev_err(dev, "Config write error, aborting.\n");
430 		return err;
431 	}
432 	data->config = reg;
433 
434 	reg = i2c_smbus_read_byte_data(client, MAX6650_REG_SPEED);
435 	if (reg < 0) {
436 		dev_err(dev, "Failed to read speed register, aborting.\n");
437 		return reg;
438 	}
439 	data->speed = reg;
440 
441 	reg = i2c_smbus_read_byte_data(client, MAX6650_REG_DAC);
442 	if (reg < 0) {
443 		dev_err(dev, "Failed to read DAC register, aborting.\n");
444 		return reg;
445 	}
446 	data->dac = reg;
447 
448 	reg = i2c_smbus_read_byte_data(client, MAX6650_REG_COUNT);
449 	if (reg < 0) {
450 		dev_err(dev, "Failed to read count register, aborting.\n");
451 		return reg;
452 	}
453 	data->count = reg;
454 
455 	reg = i2c_smbus_read_byte_data(client, MAX6650_REG_ALARM_EN);
456 	if (reg < 0) {
457 		dev_err(dev, "Failed to read alarm configuration, aborting.\n");
458 		return reg;
459 	}
460 	data->alarm_en = reg;
461 
462 	if (!of_property_read_u32(client->dev.of_node, "maxim,fan-target-rpm",
463 				  &target_rpm)) {
464 		max6650_set_target(data, target_rpm);
465 		max6650_set_operating_mode(data, MAX6650_CFG_MODE_CLOSED_LOOP);
466 	}
467 
468 	return 0;
469 }
470 
471 static int max6650_get_max_state(struct thermal_cooling_device *cdev,
472 				 unsigned long *state)
473 {
474 	*state = 255;
475 
476 	return 0;
477 }
478 
479 static int max6650_get_cur_state(struct thermal_cooling_device *cdev,
480 				 unsigned long *state)
481 {
482 	struct max6650_data *data = cdev->devdata;
483 
484 	*state = data->cooling_dev_state;
485 
486 	return 0;
487 }
488 
489 static int max6650_set_cur_state(struct thermal_cooling_device *cdev,
490 				 unsigned long state)
491 {
492 	struct max6650_data *data = cdev->devdata;
493 	struct i2c_client *client = data->client;
494 	int err;
495 
496 	state = clamp_val(state, 0, 255);
497 
498 	mutex_lock(&data->update_lock);
499 
500 	data->dac = pwm_to_dac(state, data->config & MAX6650_CFG_V12);
501 	err = i2c_smbus_write_byte_data(client, MAX6650_REG_DAC, data->dac);
502 	if (!err) {
503 		max6650_set_operating_mode(data, state ?
504 					   MAX6650_CFG_MODE_OPEN_LOOP :
505 					   MAX6650_CFG_MODE_OFF);
506 		data->cooling_dev_state = state;
507 	}
508 
509 	mutex_unlock(&data->update_lock);
510 
511 	return err;
512 }
513 
514 static const struct thermal_cooling_device_ops max6650_cooling_ops = {
515 	.get_max_state = max6650_get_max_state,
516 	.get_cur_state = max6650_get_cur_state,
517 	.set_cur_state = max6650_set_cur_state,
518 };
519 
520 static int max6650_read(struct device *dev, enum hwmon_sensor_types type,
521 			u32 attr, int channel, long *val)
522 {
523 	struct max6650_data *data = max6650_update_device(dev);
524 	int mode;
525 
526 	if (IS_ERR(data))
527 		return PTR_ERR(data);
528 
529 	switch (type) {
530 	case hwmon_pwm:
531 		switch (attr) {
532 		case hwmon_pwm_input:
533 			*val = dac_to_pwm(data->dac,
534 					  data->config & MAX6650_CFG_V12);
535 			break;
536 		case hwmon_pwm_enable:
537 			/*
538 			 * Possible values:
539 			 * 0 = Fan always on
540 			 * 1 = Open loop, Voltage is set according to speed,
541 			 *     not regulated.
542 			 * 2 = Closed loop, RPM for all fans regulated by fan1
543 			 *     tachometer
544 			 * 3 = Fan off
545 			 */
546 			mode = (data->config & MAX6650_CFG_MODE_MASK) >> 4;
547 			*val = (4 - mode) & 3; /* {0 1 2 3} -> {0 3 2 1} */
548 			break;
549 		default:
550 			return -EOPNOTSUPP;
551 		}
552 		break;
553 	case hwmon_fan:
554 		switch (attr) {
555 		case hwmon_fan_input:
556 			/*
557 			 * Calculation details:
558 			 *
559 			 * Each tachometer counts over an interval given by the
560 			 * "count" register (0.25, 0.5, 1 or 2 seconds).
561 			 * The driver assumes that the fans produce two pulses
562 			 * per revolution (this seems to be the most common).
563 			 */
564 			*val = DIV_ROUND_CLOSEST(data->tach[channel] * 120,
565 						 DIV_FROM_REG(data->count));
566 			break;
567 		case hwmon_fan_div:
568 			*val = DIV_FROM_REG(data->count);
569 			break;
570 		case hwmon_fan_target:
571 			/*
572 			 * Use the datasheet equation:
573 			 *    FanSpeed = KSCALE x fCLK / [256 x (KTACH + 1)]
574 			 * then multiply by 60 to give rpm.
575 			 */
576 			*val = 60 * DIV_FROM_REG(data->config) * clock /
577 				(256 * (data->speed + 1));
578 			break;
579 		case hwmon_fan_min_alarm:
580 			*val = !!(data->alarm & MAX6650_ALRM_MIN);
581 			data->alarm &= ~MAX6650_ALRM_MIN;
582 			data->valid = false;
583 			break;
584 		case hwmon_fan_max_alarm:
585 			*val = !!(data->alarm & MAX6650_ALRM_MAX);
586 			data->alarm &= ~MAX6650_ALRM_MAX;
587 			data->valid = false;
588 			break;
589 		case hwmon_fan_fault:
590 			*val = !!(data->alarm & MAX6650_ALRM_TACH);
591 			data->alarm &= ~MAX6650_ALRM_TACH;
592 			data->valid = false;
593 			break;
594 		default:
595 			return -EOPNOTSUPP;
596 		}
597 		break;
598 	default:
599 		return -EOPNOTSUPP;
600 	}
601 	return 0;
602 }
603 
604 static const u8 max6650_pwm_modes[] = {
605 	MAX6650_CFG_MODE_ON,
606 	MAX6650_CFG_MODE_OPEN_LOOP,
607 	MAX6650_CFG_MODE_CLOSED_LOOP,
608 	MAX6650_CFG_MODE_OFF,
609 };
610 
611 static int max6650_write(struct device *dev, enum hwmon_sensor_types type,
612 			 u32 attr, int channel, long val)
613 {
614 	struct max6650_data *data = dev_get_drvdata(dev);
615 	int ret = 0;
616 	u8 reg;
617 
618 	mutex_lock(&data->update_lock);
619 
620 	switch (type) {
621 	case hwmon_pwm:
622 		switch (attr) {
623 		case hwmon_pwm_input:
624 			reg = pwm_to_dac(clamp_val(val, 0, 255),
625 					 data->config & MAX6650_CFG_V12);
626 			ret = i2c_smbus_write_byte_data(data->client,
627 							MAX6650_REG_DAC, reg);
628 			if (ret)
629 				break;
630 			data->dac = reg;
631 			break;
632 		case hwmon_pwm_enable:
633 			if (val < 0 || val >= ARRAY_SIZE(max6650_pwm_modes)) {
634 				ret = -EINVAL;
635 				break;
636 			}
637 			ret = max6650_set_operating_mode(data,
638 						max6650_pwm_modes[val]);
639 			break;
640 		default:
641 			ret = -EOPNOTSUPP;
642 			break;
643 		}
644 		break;
645 	case hwmon_fan:
646 		switch (attr) {
647 		case hwmon_fan_div:
648 			switch (val) {
649 			case 1:
650 				reg = 0;
651 				break;
652 			case 2:
653 				reg = 1;
654 				break;
655 			case 4:
656 				reg = 2;
657 				break;
658 			case 8:
659 				reg = 3;
660 				break;
661 			default:
662 				ret = -EINVAL;
663 				goto error;
664 			}
665 			ret = i2c_smbus_write_byte_data(data->client,
666 							MAX6650_REG_COUNT, reg);
667 			if (ret)
668 				break;
669 			data->count = reg;
670 			break;
671 		case hwmon_fan_target:
672 			if (val < 0) {
673 				ret = -EINVAL;
674 				break;
675 			}
676 			ret = max6650_set_target(data, val);
677 			break;
678 		default:
679 			ret = -EOPNOTSUPP;
680 			break;
681 		}
682 		break;
683 	default:
684 		ret = -EOPNOTSUPP;
685 		break;
686 	}
687 
688 error:
689 	mutex_unlock(&data->update_lock);
690 	return ret;
691 }
692 
693 static umode_t max6650_is_visible(const void *_data,
694 				  enum hwmon_sensor_types type, u32 attr,
695 				  int channel)
696 {
697 	const struct max6650_data *data = _data;
698 
699 	if (channel && (channel >= data->nr_fans || type != hwmon_fan))
700 		return 0;
701 
702 	switch (type) {
703 	case hwmon_fan:
704 		switch (attr) {
705 		case hwmon_fan_input:
706 			return 0444;
707 		case hwmon_fan_target:
708 		case hwmon_fan_div:
709 			return 0644;
710 		case hwmon_fan_min_alarm:
711 			if (data->alarm_en & MAX6650_ALRM_MIN)
712 				return 0444;
713 			break;
714 		case hwmon_fan_max_alarm:
715 			if (data->alarm_en & MAX6650_ALRM_MAX)
716 				return 0444;
717 			break;
718 		case hwmon_fan_fault:
719 			if (data->alarm_en & MAX6650_ALRM_TACH)
720 				return 0444;
721 			break;
722 		default:
723 			break;
724 		}
725 		break;
726 	case hwmon_pwm:
727 		switch (attr) {
728 		case hwmon_pwm_input:
729 		case hwmon_pwm_enable:
730 			return 0644;
731 		default:
732 			break;
733 		}
734 		break;
735 	default:
736 		break;
737 	}
738 	return 0;
739 }
740 
741 static const struct hwmon_channel_info * const max6650_info[] = {
742 	HWMON_CHANNEL_INFO(fan, HWMON_F_INPUT | HWMON_F_TARGET | HWMON_F_DIV |
743 			   HWMON_F_MIN_ALARM | HWMON_F_MAX_ALARM |
744 			   HWMON_F_FAULT,
745 			   HWMON_F_INPUT, HWMON_F_INPUT, HWMON_F_INPUT),
746 	HWMON_CHANNEL_INFO(pwm, HWMON_PWM_INPUT | HWMON_PWM_ENABLE),
747 	NULL
748 };
749 
750 static const struct hwmon_ops max6650_hwmon_ops = {
751 	.read = max6650_read,
752 	.write = max6650_write,
753 	.is_visible = max6650_is_visible,
754 };
755 
756 static const struct hwmon_chip_info max6650_chip_info = {
757 	.ops = &max6650_hwmon_ops,
758 	.info = max6650_info,
759 };
760 
761 static const struct i2c_device_id max6650_id[];
762 
763 static int max6650_probe(struct i2c_client *client)
764 {
765 	struct thermal_cooling_device *cooling_dev;
766 	struct device *dev = &client->dev;
767 	struct max6650_data *data;
768 	struct device *hwmon_dev;
769 	int err;
770 
771 	data = devm_kzalloc(dev, sizeof(struct max6650_data), GFP_KERNEL);
772 	if (!data)
773 		return -ENOMEM;
774 
775 	data->client = client;
776 	i2c_set_clientdata(client, data);
777 	mutex_init(&data->update_lock);
778 
779 	data->nr_fans = (uintptr_t)i2c_get_match_data(client);
780 
781 	/*
782 	 * Initialize the max6650 chip
783 	 */
784 	err = max6650_init_client(data, client);
785 	if (err)
786 		return err;
787 
788 	hwmon_dev = devm_hwmon_device_register_with_info(dev,
789 							 client->name, data,
790 							 &max6650_chip_info,
791 							 max6650_groups);
792 	err = PTR_ERR_OR_ZERO(hwmon_dev);
793 	if (err)
794 		return err;
795 
796 	if (IS_ENABLED(CONFIG_THERMAL)) {
797 		cooling_dev = devm_thermal_of_cooling_device_register(dev,
798 						dev->of_node, client->name,
799 						data, &max6650_cooling_ops);
800 		if (IS_ERR(cooling_dev)) {
801 			dev_warn(dev, "thermal cooling device register failed: %ld\n",
802 				 PTR_ERR(cooling_dev));
803 		}
804 	}
805 
806 	return 0;
807 }
808 
809 static const struct i2c_device_id max6650_id[] = {
810 	{ "max6650", 1 },
811 	{ "max6651", 4 },
812 	{ }
813 };
814 MODULE_DEVICE_TABLE(i2c, max6650_id);
815 
816 static struct i2c_driver max6650_driver = {
817 	.driver = {
818 		.name	= "max6650",
819 		.of_match_table = of_match_ptr(max6650_dt_match),
820 	},
821 	.probe		= max6650_probe,
822 	.id_table	= max6650_id,
823 };
824 
825 module_i2c_driver(max6650_driver);
826 
827 MODULE_AUTHOR("Hans J. Koch");
828 MODULE_DESCRIPTION("MAX6650 sensor driver");
829 MODULE_LICENSE("GPL");
830