xref: /openbmc/linux/drivers/hwmon/pmbus/max31785.c (revision a529fa1b)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (C) 2017 IBM Corp.
4  */
5 
6 #include <linux/kernel.h>
7 #include <linux/module.h>
8 #include <linux/init.h>
9 #include <linux/err.h>
10 #include <linux/i2c.h>
11 #include "pmbus.h"
12 
13 enum max31785_regs {
14 	MFR_REVISION		= 0x9b,
15 	MFR_FAULT_RESPONSE	= 0xd9,
16 	MFR_TEMP_SENSOR_CONFIG	= 0xf0,
17 	MFR_FAN_CONFIG		= 0xf1,
18 	MFR_FAN_FAULT_LIMIT	= 0xf5,
19 };
20 
21 #define MAX31785			0x3030
22 #define MAX31785A			0x3040
23 #define MAX31785B			0x3061
24 
25 #define MFR_FAN_CONFIG_DUAL_TACH	BIT(12)
26 #define MFR_FAN_CONFIG_TSFO		BIT(9)
27 #define MFR_FAN_CONFIG_TACHO		BIT(8)
28 #define MFR_FAN_CONFIG_HEALTH		BIT(4)
29 #define MFR_FAN_CONFIG_ROTOR_HI_LO	BIT(3)
30 #define MFR_FAN_CONFIG_ROTOR		BIT(2)
31 
32 #define MFR_FAULT_RESPONSE_MONITOR	BIT(0)
33 
34 #define MAX31785_NR_PAGES		23
35 #define MAX31785_NR_FAN_PAGES		6
36 
37 /*
38  * MAX31785 dragons ahead
39  *
40  * We see weird issues where some transfers fail. There doesn't appear to be
41  * any pattern to the problem, so below we wrap all the read/write calls with a
42  * retry. The device provides no indication of this besides NACK'ing master
43  * Txs; no bits are set in STATUS_BYTE to suggest anything has gone wrong.
44  */
45 
46 #define max31785_retry(_func, ...) ({					\
47 	/* All relevant functions return int, sue me */			\
48 	int _ret = _func(__VA_ARGS__);					\
49 	if (_ret == -EIO)						\
50 		_ret = _func(__VA_ARGS__);				\
51 	_ret;								\
52 })
53 
max31785_i2c_smbus_read_byte_data(struct i2c_client * client,int command)54 static int max31785_i2c_smbus_read_byte_data(struct i2c_client *client,
55 					      int command)
56 {
57 	return max31785_retry(i2c_smbus_read_byte_data, client, command);
58 }
59 
60 
max31785_i2c_smbus_write_byte_data(struct i2c_client * client,int command,u16 data)61 static int max31785_i2c_smbus_write_byte_data(struct i2c_client *client,
62 					      int command, u16 data)
63 {
64 	return max31785_retry(i2c_smbus_write_byte_data, client, command, data);
65 }
66 
max31785_i2c_smbus_read_word_data(struct i2c_client * client,int command)67 static int max31785_i2c_smbus_read_word_data(struct i2c_client *client,
68 					     int command)
69 {
70 	return max31785_retry(i2c_smbus_read_word_data, client, command);
71 }
72 
max31785_i2c_smbus_write_word_data(struct i2c_client * client,int command,u16 data)73 static int max31785_i2c_smbus_write_word_data(struct i2c_client *client,
74 					      int command, u16 data)
75 {
76 	return max31785_retry(i2c_smbus_write_word_data, client, command, data);
77 }
78 
max31785_pmbus_write_byte(struct i2c_client * client,int page,u8 value)79 static int max31785_pmbus_write_byte(struct i2c_client *client, int page,
80 				     u8 value)
81 {
82 	return max31785_retry(pmbus_write_byte, client, page, value);
83 }
84 
max31785_pmbus_read_byte_data(struct i2c_client * client,int page,int command)85 static int max31785_pmbus_read_byte_data(struct i2c_client *client, int page,
86 					  int command)
87 {
88 	return max31785_retry(pmbus_read_byte_data, client, page, command);
89 }
90 
max31785_pmbus_write_byte_data(struct i2c_client * client,int page,int command,u16 data)91 static int max31785_pmbus_write_byte_data(struct i2c_client *client, int page,
92 					  int command, u16 data)
93 {
94 	return max31785_retry(pmbus_write_byte_data, client, page, command,
95 			      data);
96 }
97 
max31785_pmbus_read_word_data(struct i2c_client * client,int page,int phase,int command)98 static int max31785_pmbus_read_word_data(struct i2c_client *client, int page,
99 					 int phase, int command)
100 {
101 	return max31785_retry(pmbus_read_word_data, client, page, phase, command);
102 }
103 
max31785_pmbus_write_word_data(struct i2c_client * client,int page,int command,u16 data)104 static int max31785_pmbus_write_word_data(struct i2c_client *client, int page,
105 					  int command, u16 data)
106 {
107 	return max31785_retry(pmbus_write_word_data, client, page, command,
108 			      data);
109 }
110 
max31785_read_byte_data(struct i2c_client * client,int page,int reg)111 static int max31785_read_byte_data(struct i2c_client *client, int page,
112 				   int reg)
113 {
114 	switch (reg) {
115 	case PMBUS_VOUT_MODE:
116 		if (page >= MAX31785_NR_PAGES)
117 			return -ENOTSUPP;
118 		break;
119 	case PMBUS_FAN_CONFIG_12:
120 		if (page >= MAX31785_NR_PAGES)
121 			return max31785_pmbus_read_byte_data(client,
122 					page - MAX31785_NR_PAGES,
123 					reg);
124 		break;
125 	}
126 
127 	return max31785_pmbus_read_byte_data(client, page, reg);
128 }
129 
max31785_write_byte(struct i2c_client * client,int page,u8 value)130 static int max31785_write_byte(struct i2c_client *client, int page, u8 value)
131 {
132 	if (page >= MAX31785_NR_PAGES)
133 		return -ENOTSUPP;
134 
135 	return max31785_pmbus_write_byte(client, page, value);
136 }
137 
max31785_read_long_data(struct i2c_client * client,int page,int reg,u32 * data)138 static int max31785_read_long_data(struct i2c_client *client, int page,
139 				   int reg, u32 *data)
140 {
141 	unsigned char cmdbuf[1];
142 	unsigned char rspbuf[4];
143 	int rc;
144 
145 	struct i2c_msg msg[2] = {
146 		{
147 			.addr = client->addr,
148 			.flags = 0,
149 			.len = sizeof(cmdbuf),
150 			.buf = cmdbuf,
151 		},
152 		{
153 			.addr = client->addr,
154 			.flags = I2C_M_RD,
155 			.len = sizeof(rspbuf),
156 			.buf = rspbuf,
157 		},
158 	};
159 
160 	cmdbuf[0] = reg;
161 
162 	rc = pmbus_set_page(client, page, 0xff);
163 	if (rc < 0)
164 		return rc;
165 
166 	rc = i2c_transfer(client->adapter, msg, ARRAY_SIZE(msg));
167 	if (rc < 0)
168 		return rc;
169 
170 	*data = (rspbuf[0] << (0 * 8)) | (rspbuf[1] << (1 * 8)) |
171 		(rspbuf[2] << (2 * 8)) | (rspbuf[3] << (3 * 8));
172 
173 	return rc;
174 }
175 
max31785_get_pwm(struct i2c_client * client,int page)176 static int max31785_get_pwm(struct i2c_client *client, int page)
177 {
178 	int rv;
179 
180 	rv = pmbus_get_fan_rate_device(client, page, 0, percent);
181 	if (rv < 0)
182 		return rv;
183 	else if (rv >= 0x8000)
184 		return 0;
185 	else if (rv >= 0x2711)
186 		return 0x2710;
187 
188 	return rv;
189 }
190 
max31785_get_pwm_mode(struct i2c_client * client,int page)191 static int max31785_get_pwm_mode(struct i2c_client *client, int page)
192 {
193 	int config;
194 	int command;
195 
196 	config = max31785_pmbus_read_byte_data(client, page,
197 					       PMBUS_FAN_CONFIG_12);
198 	if (config < 0)
199 		return config;
200 
201 	command = max31785_pmbus_read_word_data(client, page, 0xff,
202 						PMBUS_FAN_COMMAND_1);
203 	if (command < 0)
204 		return command;
205 
206 	if (config & PB_FAN_1_RPM)
207 		return (command >= 0x8000) ? 3 : 2;
208 
209 	if (command >= 0x8000)
210 		return 3;
211 	else if (command >= 0x2711)
212 		return 0;
213 
214 	return 1;
215 }
216 
max31785_read_word_data(struct i2c_client * client,int page,int phase,int reg)217 static int max31785_read_word_data(struct i2c_client *client, int page,
218 				   int phase, int reg)
219 {
220 	u32 val;
221 	int rv;
222 
223 	switch (reg) {
224 	case PMBUS_READ_FAN_SPEED_1:
225 		if (page < MAX31785_NR_PAGES)
226 			return max31785_pmbus_read_word_data(client, page, 0xff, reg);
227 
228 		rv = max31785_read_long_data(client, page - MAX31785_NR_PAGES,
229 					     reg, &val);
230 		if (rv < 0)
231 			return rv;
232 
233 		return (val >> 16) & 0xffff;
234 	case PMBUS_FAN_COMMAND_1:
235 		/*
236 		 * PMBUS_FAN_COMMAND_x is probed to judge whether or not to
237 		 * expose fan control registers.
238 		 *
239 		 * Don't expose fan_target attribute for virtual pages.
240 		 */
241 		if (page >= MAX31785_NR_PAGES)
242 			return -ENOTSUPP;
243 		break;
244 	case PMBUS_VIRT_FAN_TARGET_1:
245 		if (page >= MAX31785_NR_PAGES)
246 			return -ENOTSUPP;
247 
248 		return -ENODATA;
249 	case PMBUS_VIRT_PWM_1:
250 		return max31785_get_pwm(client, page);
251 	case PMBUS_VIRT_PWM_ENABLE_1:
252 		return max31785_get_pwm_mode(client, page);
253 	default:
254 		if (page >= MAX31785_NR_PAGES)
255 			return -ENXIO;
256 		break;
257 	}
258 
259 	if (reg >= PMBUS_VIRT_BASE)
260 		return -ENXIO;
261 
262 	return max31785_pmbus_read_word_data(client, page, 0xff, reg);
263 }
264 
max31785_scale_pwm(u32 sensor_val)265 static inline u32 max31785_scale_pwm(u32 sensor_val)
266 {
267 	/*
268 	 * The datasheet describes the accepted value range for manual PWM as
269 	 * [0, 0x2710], while the hwmon pwmX sysfs interface accepts values in
270 	 * [0, 255]. The MAX31785 uses DIRECT mode to scale the FAN_COMMAND
271 	 * registers and in PWM mode the coefficients are m=1, b=0, R=2. The
272 	 * important observation here is that 0x2710 == 10000 == 100 * 100.
273 	 *
274 	 * R=2 (== 10^2 == 100) accounts for scaling the value provided at the
275 	 * sysfs interface into the required hardware resolution, but it does
276 	 * not yet yield a value that we can write to the device (this initial
277 	 * scaling is handled by pmbus_data2reg()). Multiplying by 100 below
278 	 * translates the parameter value into the percentage units required by
279 	 * PMBus, and then we scale back by 255 as required by the hwmon pwmX
280 	 * interface to yield the percentage value at the appropriate
281 	 * resolution for hardware.
282 	 */
283 	return (sensor_val * 100) / 255;
284 }
285 
max31785_update_fan(struct i2c_client * client,int page,u8 config,u8 mask,u16 command)286 static int max31785_update_fan(struct i2c_client *client, int page,
287 			       u8 config, u8 mask, u16 command)
288 {
289 	int from, rv;
290 	u8 to;
291 
292 	from = max31785_pmbus_read_byte_data(client, page, PMBUS_FAN_CONFIG_12);
293 	if (from < 0)
294 		return from;
295 
296 	to = (from & ~mask) | (config & mask);
297 
298 	if (to != from) {
299 		rv = max31785_pmbus_write_byte_data(client, page,
300 						    PMBUS_FAN_CONFIG_12, to);
301 		if (rv < 0)
302 			return rv;
303 	}
304 
305 	rv = max31785_pmbus_write_word_data(client, page, PMBUS_FAN_COMMAND_1,
306 					    command);
307 
308 	return rv;
309 }
310 
max31785_pwm_enable(struct i2c_client * client,int page,u16 word)311 static int max31785_pwm_enable(struct i2c_client *client, int page,
312 				    u16 word)
313 {
314 	int config = 0;
315 	int rate;
316 
317 	switch (word) {
318 	case 0:
319 		rate = 0x7fff;
320 		break;
321 	case 1:
322 		rate = pmbus_get_fan_rate_cached(client, page, 0, percent);
323 		if (rate < 0)
324 			return rate;
325 		rate = max31785_scale_pwm(rate);
326 		break;
327 	case 2:
328 		config = PB_FAN_1_RPM;
329 		rate = pmbus_get_fan_rate_cached(client, page, 0, rpm);
330 		if (rate < 0)
331 			return rate;
332 		break;
333 	case 3:
334 		rate = 0xffff;
335 		break;
336 	default:
337 		return -EINVAL;
338 	}
339 
340 	return max31785_update_fan(client, page, config, PB_FAN_1_RPM, rate);
341 }
342 
max31785_write_word_data(struct i2c_client * client,int page,int reg,u16 word)343 static int max31785_write_word_data(struct i2c_client *client, int page,
344 				    int reg, u16 word)
345 {
346 	switch (reg) {
347 	case PMBUS_VIRT_FAN_TARGET_1:
348 		return max31785_update_fan(client, page, PB_FAN_1_RPM,
349 					   PB_FAN_1_RPM, word);
350 	case PMBUS_VIRT_PWM_1:
351 		return max31785_update_fan(client, page, 0, PB_FAN_1_RPM,
352 					max31785_scale_pwm(word));
353 	case PMBUS_VIRT_PWM_ENABLE_1:
354 		return max31785_pwm_enable(client, page, word);
355 	default:
356 		break;
357 	}
358 
359 	if (reg < PMBUS_VIRT_BASE)
360 		return max31785_pmbus_write_word_data(client, page, reg, word);
361 
362 	return -ENXIO;
363 }
364 
365 /*
366  * Returns negative error codes if an unrecoverable problem is detected, 0 if a
367  * recoverable problem is detected, or a positive value on success.
368  */
max31785_of_fan_config(struct i2c_client * client,struct pmbus_driver_info * info,struct device_node * child)369 static int max31785_of_fan_config(struct i2c_client *client,
370 				  struct pmbus_driver_info *info,
371 				  struct device_node *child)
372 {
373 	int mfr_cfg = 0, mfr_fault_resp = 0, pb_cfg;
374 	struct device *dev = &client->dev;
375 	char *lock_polarity = NULL;
376 	const char *sval;
377 	u32 page;
378 	u32 uval;
379 	int ret;
380 
381 	if (!of_device_is_compatible(child, "pmbus-fan"))
382 		return 0;
383 
384 	ret = of_property_read_u32(child, "reg", &page);
385 	if (ret < 0) {
386 		dev_err(&client->dev, "Missing valid reg property\n");
387 		return ret;
388 	}
389 
390 	if (!(info->func[page] & PMBUS_HAVE_FAN12)) {
391 		dev_err(dev, "Page %d does not have fan capabilities\n", page);
392 		return -ENXIO;
393 	}
394 
395 	ret = max31785_i2c_smbus_write_byte_data(client, PMBUS_PAGE, page);
396 	if (ret < 0)
397 		return ret;
398 
399 	pb_cfg = max31785_i2c_smbus_read_byte_data(client, PMBUS_FAN_CONFIG_12);
400 	if (pb_cfg < 0)
401 		return pb_cfg;
402 
403 	if (of_property_read_bool(child->parent, "use-stored-presence")) {
404 		if (!(pb_cfg & PB_FAN_1_INSTALLED))
405 			dev_info(dev, "Fan %d is configured but not installed\n",
406 				 page);
407 	} else {
408 		pb_cfg |= PB_FAN_1_INSTALLED;
409 	}
410 
411 	ret = of_property_read_string(child, "maxim,fan-rotor-input", &sval);
412 	if (ret < 0) {
413 		dev_err(dev, "Missing valid maxim,fan-rotor-input property for fan %d\n",
414 				page);
415 		return ret;
416 	}
417 
418 	if (strcmp("tach", sval) && strcmp("lock", sval)) {
419 		dev_err(dev, "maxim,fan-rotor-input has invalid value for fan %d: %s\n",
420 				page, sval);
421 		return -EINVAL;
422 	} else if (!strcmp("lock", sval)) {
423 		mfr_cfg |= MFR_FAN_CONFIG_ROTOR;
424 
425 		ret = max31785_i2c_smbus_write_word_data(client,
426 							 MFR_FAN_FAULT_LIMIT,
427 							 1);
428 		if (ret < 0)
429 			return ret;
430 
431 		ret = of_property_read_string(child, "maxim,fan-lock-polarity",
432 					      &sval);
433 		if (ret < 0) {
434 			dev_err(dev, "Missing valid maxim,fan-lock-polarity property for fan %d\n",
435 					page);
436 			return ret;
437 		}
438 
439 		if (strcmp("low", sval) && strcmp("high", sval)) {
440 			dev_err(dev, "maxim,fan-lock-polarity has invalid value for fan %d: %s\n",
441 					page, lock_polarity);
442 			return -EINVAL;
443 		} else if (!strcmp("high", sval))
444 			mfr_cfg |= MFR_FAN_CONFIG_ROTOR_HI_LO;
445 	}
446 
447 	if (!of_property_read_string(child, "fan-mode", &sval)) {
448 		if (!strcmp("rpm", sval))
449 			pb_cfg |= PB_FAN_1_RPM;
450 		else if (!strcmp("pwm", sval))
451 			pb_cfg &= ~PB_FAN_1_RPM;
452 		else {
453 			dev_err(dev, "fan-mode has invalid value for fan %d: %s\n",
454 					page, sval);
455 			return -EINVAL;
456 		}
457 	}
458 
459 	ret = of_property_read_u32(child, "tach-pulses", &uval);
460 	if (ret < 0) {
461 		pb_cfg &= ~PB_FAN_1_PULSE_MASK;
462 	} else if (uval && (uval - 1) < 4) {
463 		pb_cfg = ((pb_cfg & ~PB_FAN_1_PULSE_MASK) | ((uval - 1) << 4));
464 	} else {
465 		dev_err(dev, "tach-pulses has invalid value for fan %d: %u\n",
466 				page, uval);
467 		return -EINVAL;
468 	}
469 
470 	if (of_property_read_bool(child, "maxim,fan-health"))
471 		mfr_cfg |= MFR_FAN_CONFIG_HEALTH;
472 
473 	if (of_property_read_bool(child, "maxim,fan-no-watchdog") ||
474 		of_property_read_bool(child, "maxim,tmp-no-fault-ramp"))
475 		mfr_cfg |= MFR_FAN_CONFIG_TSFO;
476 
477 	if (of_property_read_bool(child, "maxim,fan-dual-tach"))
478 		mfr_cfg |= MFR_FAN_CONFIG_DUAL_TACH;
479 
480 	if (of_property_read_bool(child, "maxim,fan-no-fault-ramp"))
481 		mfr_cfg |= MFR_FAN_CONFIG_TACHO;
482 
483 	if (!of_property_read_u32(child, "maxim,fan-startup", &uval)) {
484 		uval /= 2;
485 		if (uval < 5) {
486 			mfr_cfg |= uval;
487 		} else {
488 			dev_err(dev, "maxim,fan-startup has invalid value for fan %d: %u\n",
489 					page, uval);
490 			return -EINVAL;
491 		}
492 	}
493 
494 	if (!of_property_read_u32(child, "maxim,fan-ramp", &uval)) {
495 		if (uval < 8) {
496 			mfr_cfg |= uval << 5;
497 		} else {
498 			dev_err(dev, "maxim,fan-ramp has invalid value for fan %d: %u\n",
499 					page, uval);
500 			return -EINVAL;
501 		}
502 	}
503 
504 	if (!of_property_read_u32(child, "maxim,tmp-hysteresis", &uval)) {
505 		uval /= 2;
506 		uval -= 1;
507 		if (uval < 4) {
508 			mfr_cfg |= uval << 10;
509 		} else {
510 			dev_err(dev, "maxim,tmp-hysteresis has invalid value for fan %d, %u\n",
511 					page, uval);
512 			return -EINVAL;
513 		}
514 	}
515 
516 	if (!of_property_read_u32(child, "maxim,fan-pwm-freq", &uval)) {
517 		u16 val;
518 
519 		if (uval == 30) {
520 			val = 0;
521 		} else if (uval == 50) {
522 			val = 1;
523 		} else if (uval == 100) {
524 			val = 2;
525 		} else if (uval == 150) {
526 			val = 3;
527 		} else if (uval == 25000) {
528 			val = 7;
529 		} else {
530 			dev_err(dev, "maxim,fan-pwm-freq has invalid value for fan %d: %u\n",
531 					page, uval);
532 			return -EINVAL;
533 		}
534 
535 		mfr_cfg |= val << 13;
536 	}
537 
538 	if (of_property_read_bool(child, "maxim,fan-fault-pin-mon"))
539 		mfr_fault_resp |= MFR_FAULT_RESPONSE_MONITOR;
540 
541 	ret = max31785_i2c_smbus_write_byte_data(client, PMBUS_FAN_CONFIG_12,
542 					pb_cfg & ~PB_FAN_1_INSTALLED);
543 	if (ret < 0)
544 		return ret;
545 
546 	ret = max31785_i2c_smbus_write_word_data(client, MFR_FAN_CONFIG,
547 						 mfr_cfg);
548 	if (ret < 0)
549 		return ret;
550 
551 	ret = max31785_i2c_smbus_write_byte_data(client, MFR_FAULT_RESPONSE,
552 						 mfr_fault_resp);
553 	if (ret < 0)
554 		return ret;
555 
556 	ret = max31785_i2c_smbus_write_byte_data(client, PMBUS_FAN_CONFIG_12,
557 						 pb_cfg);
558 	if (ret < 0)
559 		return ret;
560 
561 	/*
562 	 * Fans are on pages 0 - 5. If the page property of a fan node is
563 	 * greater than 5 we will have errored in checks above out above.
564 	 * Therefore we don't need to cope with values up to 31, and the int
565 	 * return type is enough.
566 	 *
567 	 * The bit mask return value is used to populate a bitfield of fans
568 	 * who are both configured in the devicetree _and_ reported as
569 	 * installed by the hardware. Any fans that are not configured in the
570 	 * devicetree but are reported as installed by the hardware will have
571 	 * their hardware configuration updated to unset the installed bit.
572 	 */
573 	return BIT(page);
574 }
575 
max31785_of_tmp_config(struct i2c_client * client,struct pmbus_driver_info * info,struct device_node * child)576 static int max31785_of_tmp_config(struct i2c_client *client,
577 				  struct pmbus_driver_info *info,
578 				  struct device_node *child)
579 {
580 	struct device *dev = &client->dev;
581 	struct device_node *np;
582 	u16 mfr_tmp_cfg = 0;
583 	u32 page;
584 	u32 uval;
585 	int ret;
586 	int i;
587 
588 	if (!of_device_is_compatible(child, "pmbus-temperature"))
589 		return 0;
590 
591 	ret = of_property_read_u32(child, "reg", &page);
592 	if (ret < 0) {
593 		dev_err(&client->dev, "Missing valid reg property\n");
594 		return ret;
595 	}
596 
597 	if (!(info->func[page] & PMBUS_HAVE_TEMP)) {
598 		dev_err(dev, "Page %d does not have temp capabilities\n", page);
599 		return -ENXIO;
600 	}
601 
602 	ret = max31785_i2c_smbus_write_byte_data(client, PMBUS_PAGE, page);
603 	if (ret < 0)
604 		return ret;
605 
606 	if (!of_property_read_u32(child, "maxim,tmp-offset", &uval)) {
607 		if (uval < 32)
608 			mfr_tmp_cfg |= uval << 10;
609 	}
610 
611 	i = 0;
612 	while ((np = of_parse_phandle(child, "maxim,tmp-fans", i))) {
613 		if (of_property_read_u32(np, "reg", &uval)) {
614 			dev_err(&client->dev, "Failed to read fan reg property for phandle index %d\n",
615 					i);
616 		} else {
617 			if (uval < 6)
618 				mfr_tmp_cfg |= BIT(uval);
619 			else
620 				dev_warn(&client->dev, "Invalid fan page: %d\n",
621 						uval);
622 		}
623 		i++;
624 	}
625 
626 	ret = max31785_i2c_smbus_write_word_data(client, MFR_TEMP_SENSOR_CONFIG,
627 					mfr_tmp_cfg);
628 	if (ret < 0)
629 		return ret;
630 
631 	return 0;
632 }
633 
634 #define MAX31785_FAN_FUNCS \
635 	(PMBUS_HAVE_FAN12 | PMBUS_HAVE_STATUS_FAN12 | PMBUS_HAVE_PWM12)
636 
637 #define MAX31785_TEMP_FUNCS \
638 	(PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP)
639 
640 #define MAX31785_VOUT_FUNCS \
641 	(PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT)
642 
643 static const struct pmbus_driver_info max31785_info = {
644 	.pages = MAX31785_NR_PAGES,
645 
646 	.write_word_data = max31785_write_word_data,
647 	.read_byte_data = max31785_read_byte_data,
648 	.read_word_data = max31785_read_word_data,
649 	.write_byte = max31785_write_byte,
650 
651 	/* RPM */
652 	.format[PSC_FAN] = direct,
653 	.m[PSC_FAN] = 1,
654 	.b[PSC_FAN] = 0,
655 	.R[PSC_FAN] = 0,
656 	/* PWM */
657 	.format[PSC_PWM] = direct,
658 	.m[PSC_PWM] = 1,
659 	.b[PSC_PWM] = 0,
660 	.R[PSC_PWM] = 2,
661 	.func[0] = MAX31785_FAN_FUNCS,
662 	.func[1] = MAX31785_FAN_FUNCS,
663 	.func[2] = MAX31785_FAN_FUNCS,
664 	.func[3] = MAX31785_FAN_FUNCS,
665 	.func[4] = MAX31785_FAN_FUNCS,
666 	.func[5] = MAX31785_FAN_FUNCS,
667 
668 	.format[PSC_TEMPERATURE] = direct,
669 	.m[PSC_TEMPERATURE] = 1,
670 	.b[PSC_TEMPERATURE] = 0,
671 	.R[PSC_TEMPERATURE] = 2,
672 	.func[6]  = MAX31785_TEMP_FUNCS,
673 	.func[7]  = MAX31785_TEMP_FUNCS,
674 	.func[8]  = MAX31785_TEMP_FUNCS,
675 	.func[9]  = MAX31785_TEMP_FUNCS,
676 	.func[10] = MAX31785_TEMP_FUNCS,
677 	.func[11] = MAX31785_TEMP_FUNCS,
678 	.func[12] = MAX31785_TEMP_FUNCS,
679 	.func[13] = MAX31785_TEMP_FUNCS,
680 	.func[14] = MAX31785_TEMP_FUNCS,
681 	.func[15] = MAX31785_TEMP_FUNCS,
682 	.func[16] = MAX31785_TEMP_FUNCS,
683 
684 	.format[PSC_VOLTAGE_OUT] = direct,
685 	.m[PSC_VOLTAGE_OUT] = 1,
686 	.b[PSC_VOLTAGE_OUT] = 0,
687 	.R[PSC_VOLTAGE_OUT] = 0,
688 	.func[17] = MAX31785_VOUT_FUNCS,
689 	.func[18] = MAX31785_VOUT_FUNCS,
690 	.func[19] = MAX31785_VOUT_FUNCS,
691 	.func[20] = MAX31785_VOUT_FUNCS,
692 	.func[21] = MAX31785_VOUT_FUNCS,
693 	.func[22] = MAX31785_VOUT_FUNCS,
694 };
695 
max31785_configure_dual_tach(struct i2c_client * client,struct pmbus_driver_info * info)696 static int max31785_configure_dual_tach(struct i2c_client *client,
697 					struct pmbus_driver_info *info)
698 {
699 	int ret;
700 	int i;
701 
702 	for (i = 0; i < MAX31785_NR_FAN_PAGES; i++) {
703 		ret = max31785_i2c_smbus_write_byte_data(client, PMBUS_PAGE, i);
704 		if (ret < 0)
705 			return ret;
706 
707 		ret = max31785_i2c_smbus_read_word_data(client, MFR_FAN_CONFIG);
708 		if (ret < 0)
709 			return ret;
710 
711 		if (ret & MFR_FAN_CONFIG_DUAL_TACH) {
712 			int virtual = MAX31785_NR_PAGES + i;
713 
714 			info->pages = virtual + 1;
715 			info->func[virtual] |= PMBUS_HAVE_FAN12;
716 			info->func[virtual] |= PMBUS_PAGE_VIRTUAL;
717 		}
718 	}
719 
720 	return 0;
721 }
722 
max31785_probe(struct i2c_client * client)723 static int max31785_probe(struct i2c_client *client)
724 {
725 	struct device *dev = &client->dev;
726 	struct device_node *child;
727 	struct pmbus_driver_info *info;
728 	bool dual_tach = false;
729 	int ret;
730 	u32 fans;
731 	int i;
732 
733 	if (!i2c_check_functionality(client->adapter,
734 				     I2C_FUNC_SMBUS_BYTE_DATA |
735 				     I2C_FUNC_SMBUS_WORD_DATA))
736 		return -ENODEV;
737 
738 	info = devm_kzalloc(dev, sizeof(struct pmbus_driver_info), GFP_KERNEL);
739 	if (!info)
740 		return -ENOMEM;
741 
742 	*info = max31785_info;
743 
744 	ret = max31785_i2c_smbus_write_byte_data(client, PMBUS_PAGE, 255);
745 	if (ret < 0)
746 		return ret;
747 
748 	ret = i2c_smbus_read_word_data(client, MFR_REVISION);
749 	if (ret < 0)
750 		return ret;
751 
752 	if (ret == MAX31785A || ret == MAX31785B) {
753 		dual_tach = true;
754 	} else if (ret == MAX31785) {
755 		if (!strcmp("max31785a", client->name) ||
756 		    !strcmp("max31785b", client->name))
757 			dev_warn(dev, "Expected max31785a/b, found max31785: cannot provide secondary tachometer readings\n");
758 	} else {
759 		dev_err(dev, "Unrecognized MAX31785 revision: %x\n", ret);
760 		return -ENODEV;
761 	}
762 
763 	fans = 0;
764 	for_each_child_of_node(dev->of_node, child) {
765 		ret = max31785_of_fan_config(client, info, child);
766 		if (ret < 0) {
767 			of_node_put(child);
768 			return ret;
769 		}
770 
771 		if (ret)
772 			fans |= ret;
773 
774 		ret = max31785_of_tmp_config(client, info, child);
775 		if (ret < 0) {
776 			of_node_put(child);
777 			return ret;
778 		}
779 	}
780 
781 	for (i = 0; i < MAX31785_NR_PAGES; i++) {
782 		bool have_fan = !!(info->func[i] & PMBUS_HAVE_FAN12);
783 		bool fan_configured = !!(fans & BIT(i));
784 
785 		if (!have_fan || fan_configured)
786 			continue;
787 
788 		ret = max31785_i2c_smbus_write_byte_data(client, PMBUS_PAGE,
789 							 i);
790 		if (ret < 0)
791 			return ret;
792 
793 		ret = max31785_i2c_smbus_read_byte_data(client,
794 							PMBUS_FAN_CONFIG_12);
795 		if (ret < 0)
796 			return ret;
797 
798 		ret &= ~PB_FAN_1_INSTALLED;
799 		ret = max31785_i2c_smbus_write_word_data(client,
800 							 PMBUS_FAN_CONFIG_12,
801 							 ret);
802 		if (ret < 0)
803 			return ret;
804 	}
805 
806 	if (dual_tach) {
807 		ret = max31785_configure_dual_tach(client, info);
808 		if (ret < 0)
809 			return ret;
810 	}
811 
812 	return pmbus_do_probe(client, info);
813 }
814 
815 static const struct i2c_device_id max31785_id[] = {
816 	{ "max31785", 0 },
817 	{ "max31785a", 0 },
818 	{ "max31785b", 0 },
819 	{ },
820 };
821 
822 MODULE_DEVICE_TABLE(i2c, max31785_id);
823 
824 static const struct of_device_id max31785_of_match[] = {
825 	{ .compatible = "maxim,max31785" },
826 	{ .compatible = "maxim,max31785a" },
827 	{ .compatible = "maxim,max31785b" },
828 	{ },
829 };
830 
831 MODULE_DEVICE_TABLE(of, max31785_of_match);
832 
833 static struct i2c_driver max31785_driver = {
834 	.driver = {
835 		.name = "max31785",
836 		.of_match_table = max31785_of_match,
837 	},
838 	.probe = max31785_probe,
839 	.id_table = max31785_id,
840 };
841 
842 module_i2c_driver(max31785_driver);
843 
844 MODULE_AUTHOR("Andrew Jeffery <andrew@aj.id.au>");
845 MODULE_DESCRIPTION("PMBus driver for the Maxim MAX31785");
846 MODULE_LICENSE("GPL");
847 MODULE_IMPORT_NS(PMBUS);
848