xref: /openbmc/linux/drivers/hwmon/pmbus/max31785.c (revision 63705da3)
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_FAN_CONFIG		= 0xf1,
16 };
17 
18 #define MAX31785			0x3030
19 #define MAX31785A			0x3040
20 #define MAX31785B			0x3061
21 
22 #define MFR_FAN_CONFIG_DUAL_TACH	BIT(12)
23 
24 #define MAX31785_NR_PAGES		23
25 #define MAX31785_NR_FAN_PAGES		6
26 
27 static int max31785_read_byte_data(struct i2c_client *client, int page,
28 				   int reg)
29 {
30 	if (page < MAX31785_NR_PAGES)
31 		return -ENODATA;
32 
33 	switch (reg) {
34 	case PMBUS_VOUT_MODE:
35 		return -ENOTSUPP;
36 	case PMBUS_FAN_CONFIG_12:
37 		return pmbus_read_byte_data(client, page - MAX31785_NR_PAGES,
38 					    reg);
39 	}
40 
41 	return -ENODATA;
42 }
43 
44 static int max31785_write_byte(struct i2c_client *client, int page, u8 value)
45 {
46 	if (page < MAX31785_NR_PAGES)
47 		return -ENODATA;
48 
49 	return -ENOTSUPP;
50 }
51 
52 static int max31785_read_long_data(struct i2c_client *client, int page,
53 				   int reg, u32 *data)
54 {
55 	unsigned char cmdbuf[1];
56 	unsigned char rspbuf[4];
57 	int rc;
58 
59 	struct i2c_msg msg[2] = {
60 		{
61 			.addr = client->addr,
62 			.flags = 0,
63 			.len = sizeof(cmdbuf),
64 			.buf = cmdbuf,
65 		},
66 		{
67 			.addr = client->addr,
68 			.flags = I2C_M_RD,
69 			.len = sizeof(rspbuf),
70 			.buf = rspbuf,
71 		},
72 	};
73 
74 	cmdbuf[0] = reg;
75 
76 	rc = pmbus_set_page(client, page, 0xff);
77 	if (rc < 0)
78 		return rc;
79 
80 	rc = i2c_transfer(client->adapter, msg, ARRAY_SIZE(msg));
81 	if (rc < 0)
82 		return rc;
83 
84 	*data = (rspbuf[0] << (0 * 8)) | (rspbuf[1] << (1 * 8)) |
85 		(rspbuf[2] << (2 * 8)) | (rspbuf[3] << (3 * 8));
86 
87 	return rc;
88 }
89 
90 static int max31785_get_pwm(struct i2c_client *client, int page)
91 {
92 	int rv;
93 
94 	rv = pmbus_get_fan_rate_device(client, page, 0, percent);
95 	if (rv < 0)
96 		return rv;
97 	else if (rv >= 0x8000)
98 		return 0;
99 	else if (rv >= 0x2711)
100 		return 0x2710;
101 
102 	return rv;
103 }
104 
105 static int max31785_get_pwm_mode(struct i2c_client *client, int page)
106 {
107 	int config;
108 	int command;
109 
110 	config = pmbus_read_byte_data(client, page, PMBUS_FAN_CONFIG_12);
111 	if (config < 0)
112 		return config;
113 
114 	command = pmbus_read_word_data(client, page, 0xff, PMBUS_FAN_COMMAND_1);
115 	if (command < 0)
116 		return command;
117 
118 	if (config & PB_FAN_1_RPM)
119 		return (command >= 0x8000) ? 3 : 2;
120 
121 	if (command >= 0x8000)
122 		return 3;
123 	else if (command >= 0x2711)
124 		return 0;
125 
126 	return 1;
127 }
128 
129 static int max31785_read_word_data(struct i2c_client *client, int page,
130 				   int phase, int reg)
131 {
132 	u32 val;
133 	int rv;
134 
135 	switch (reg) {
136 	case PMBUS_READ_FAN_SPEED_1:
137 		if (page < MAX31785_NR_PAGES)
138 			return -ENODATA;
139 
140 		rv = max31785_read_long_data(client, page - MAX31785_NR_PAGES,
141 					     reg, &val);
142 		if (rv < 0)
143 			return rv;
144 
145 		rv = (val >> 16) & 0xffff;
146 		break;
147 	case PMBUS_FAN_COMMAND_1:
148 		/*
149 		 * PMBUS_FAN_COMMAND_x is probed to judge whether or not to
150 		 * expose fan control registers.
151 		 *
152 		 * Don't expose fan_target attribute for virtual pages.
153 		 */
154 		rv = (page >= MAX31785_NR_PAGES) ? -ENOTSUPP : -ENODATA;
155 		break;
156 	case PMBUS_VIRT_PWM_1:
157 		rv = max31785_get_pwm(client, page);
158 		break;
159 	case PMBUS_VIRT_PWM_ENABLE_1:
160 		rv = max31785_get_pwm_mode(client, page);
161 		break;
162 	default:
163 		rv = -ENODATA;
164 		break;
165 	}
166 
167 	return rv;
168 }
169 
170 static inline u32 max31785_scale_pwm(u32 sensor_val)
171 {
172 	/*
173 	 * The datasheet describes the accepted value range for manual PWM as
174 	 * [0, 0x2710], while the hwmon pwmX sysfs interface accepts values in
175 	 * [0, 255]. The MAX31785 uses DIRECT mode to scale the FAN_COMMAND
176 	 * registers and in PWM mode the coefficients are m=1, b=0, R=2. The
177 	 * important observation here is that 0x2710 == 10000 == 100 * 100.
178 	 *
179 	 * R=2 (== 10^2 == 100) accounts for scaling the value provided at the
180 	 * sysfs interface into the required hardware resolution, but it does
181 	 * not yet yield a value that we can write to the device (this initial
182 	 * scaling is handled by pmbus_data2reg()). Multiplying by 100 below
183 	 * translates the parameter value into the percentage units required by
184 	 * PMBus, and then we scale back by 255 as required by the hwmon pwmX
185 	 * interface to yield the percentage value at the appropriate
186 	 * resolution for hardware.
187 	 */
188 	return (sensor_val * 100) / 255;
189 }
190 
191 static int max31785_pwm_enable(struct i2c_client *client, int page,
192 				    u16 word)
193 {
194 	int config = 0;
195 	int rate;
196 
197 	switch (word) {
198 	case 0:
199 		rate = 0x7fff;
200 		break;
201 	case 1:
202 		rate = pmbus_get_fan_rate_cached(client, page, 0, percent);
203 		if (rate < 0)
204 			return rate;
205 		rate = max31785_scale_pwm(rate);
206 		break;
207 	case 2:
208 		config = PB_FAN_1_RPM;
209 		rate = pmbus_get_fan_rate_cached(client, page, 0, rpm);
210 		if (rate < 0)
211 			return rate;
212 		break;
213 	case 3:
214 		rate = 0xffff;
215 		break;
216 	default:
217 		return -EINVAL;
218 	}
219 
220 	return pmbus_update_fan(client, page, 0, config, PB_FAN_1_RPM, rate);
221 }
222 
223 static int max31785_write_word_data(struct i2c_client *client, int page,
224 				    int reg, u16 word)
225 {
226 	switch (reg) {
227 	case PMBUS_VIRT_PWM_1:
228 		return pmbus_update_fan(client, page, 0, 0, PB_FAN_1_RPM,
229 					max31785_scale_pwm(word));
230 	case PMBUS_VIRT_PWM_ENABLE_1:
231 		return max31785_pwm_enable(client, page, word);
232 	default:
233 		break;
234 	}
235 
236 	return -ENODATA;
237 }
238 
239 #define MAX31785_FAN_FUNCS \
240 	(PMBUS_HAVE_FAN12 | PMBUS_HAVE_STATUS_FAN12 | PMBUS_HAVE_PWM12)
241 
242 #define MAX31785_TEMP_FUNCS \
243 	(PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP)
244 
245 #define MAX31785_VOUT_FUNCS \
246 	(PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT)
247 
248 static const struct pmbus_driver_info max31785_info = {
249 	.pages = MAX31785_NR_PAGES,
250 
251 	.write_word_data = max31785_write_word_data,
252 	.read_byte_data = max31785_read_byte_data,
253 	.read_word_data = max31785_read_word_data,
254 	.write_byte = max31785_write_byte,
255 
256 	/* RPM */
257 	.format[PSC_FAN] = direct,
258 	.m[PSC_FAN] = 1,
259 	.b[PSC_FAN] = 0,
260 	.R[PSC_FAN] = 0,
261 	/* PWM */
262 	.format[PSC_PWM] = direct,
263 	.m[PSC_PWM] = 1,
264 	.b[PSC_PWM] = 0,
265 	.R[PSC_PWM] = 2,
266 	.func[0] = MAX31785_FAN_FUNCS,
267 	.func[1] = MAX31785_FAN_FUNCS,
268 	.func[2] = MAX31785_FAN_FUNCS,
269 	.func[3] = MAX31785_FAN_FUNCS,
270 	.func[4] = MAX31785_FAN_FUNCS,
271 	.func[5] = MAX31785_FAN_FUNCS,
272 
273 	.format[PSC_TEMPERATURE] = direct,
274 	.m[PSC_TEMPERATURE] = 1,
275 	.b[PSC_TEMPERATURE] = 0,
276 	.R[PSC_TEMPERATURE] = 2,
277 	.func[6]  = MAX31785_TEMP_FUNCS,
278 	.func[7]  = MAX31785_TEMP_FUNCS,
279 	.func[8]  = MAX31785_TEMP_FUNCS,
280 	.func[9]  = MAX31785_TEMP_FUNCS,
281 	.func[10] = MAX31785_TEMP_FUNCS,
282 	.func[11] = MAX31785_TEMP_FUNCS,
283 	.func[12] = MAX31785_TEMP_FUNCS,
284 	.func[13] = MAX31785_TEMP_FUNCS,
285 	.func[14] = MAX31785_TEMP_FUNCS,
286 	.func[15] = MAX31785_TEMP_FUNCS,
287 	.func[16] = MAX31785_TEMP_FUNCS,
288 
289 	.format[PSC_VOLTAGE_OUT] = direct,
290 	.m[PSC_VOLTAGE_OUT] = 1,
291 	.b[PSC_VOLTAGE_OUT] = 0,
292 	.R[PSC_VOLTAGE_OUT] = 0,
293 	.func[17] = MAX31785_VOUT_FUNCS,
294 	.func[18] = MAX31785_VOUT_FUNCS,
295 	.func[19] = MAX31785_VOUT_FUNCS,
296 	.func[20] = MAX31785_VOUT_FUNCS,
297 	.func[21] = MAX31785_VOUT_FUNCS,
298 	.func[22] = MAX31785_VOUT_FUNCS,
299 };
300 
301 static int max31785_configure_dual_tach(struct i2c_client *client,
302 					struct pmbus_driver_info *info)
303 {
304 	int ret;
305 	int i;
306 
307 	for (i = 0; i < MAX31785_NR_FAN_PAGES; i++) {
308 		ret = i2c_smbus_write_byte_data(client, PMBUS_PAGE, i);
309 		if (ret < 0)
310 			return ret;
311 
312 		ret = i2c_smbus_read_word_data(client, MFR_FAN_CONFIG);
313 		if (ret < 0)
314 			return ret;
315 
316 		if (ret & MFR_FAN_CONFIG_DUAL_TACH) {
317 			int virtual = MAX31785_NR_PAGES + i;
318 
319 			info->pages = virtual + 1;
320 			info->func[virtual] |= PMBUS_HAVE_FAN12;
321 			info->func[virtual] |= PMBUS_PAGE_VIRTUAL;
322 		}
323 	}
324 
325 	return 0;
326 }
327 
328 static int max31785_probe(struct i2c_client *client)
329 {
330 	struct device *dev = &client->dev;
331 	struct pmbus_driver_info *info;
332 	bool dual_tach = false;
333 	int ret;
334 
335 	if (!i2c_check_functionality(client->adapter,
336 				     I2C_FUNC_SMBUS_BYTE_DATA |
337 				     I2C_FUNC_SMBUS_WORD_DATA))
338 		return -ENODEV;
339 
340 	info = devm_kzalloc(dev, sizeof(struct pmbus_driver_info), GFP_KERNEL);
341 	if (!info)
342 		return -ENOMEM;
343 
344 	*info = max31785_info;
345 
346 	ret = i2c_smbus_write_byte_data(client, PMBUS_PAGE, 255);
347 	if (ret < 0)
348 		return ret;
349 
350 	ret = i2c_smbus_read_word_data(client, MFR_REVISION);
351 	if (ret < 0)
352 		return ret;
353 
354 	if (ret == MAX31785A || ret == MAX31785B) {
355 		dual_tach = true;
356 	} else if (ret == MAX31785) {
357 		if (!strcmp("max31785a", client->name) ||
358 		    !strcmp("max31785b", client->name))
359 			dev_warn(dev, "Expected max31785a/b, found max31785: cannot provide secondary tachometer readings\n");
360 	} else {
361 		dev_err(dev, "Unrecognized MAX31785 revision: %x\n", ret);
362 		return -ENODEV;
363 	}
364 
365 	if (dual_tach) {
366 		ret = max31785_configure_dual_tach(client, info);
367 		if (ret < 0)
368 			return ret;
369 	}
370 
371 	return pmbus_do_probe(client, info);
372 }
373 
374 static const struct i2c_device_id max31785_id[] = {
375 	{ "max31785", 0 },
376 	{ "max31785a", 0 },
377 	{ "max31785b", 0 },
378 	{ },
379 };
380 
381 MODULE_DEVICE_TABLE(i2c, max31785_id);
382 
383 static const struct of_device_id max31785_of_match[] = {
384 	{ .compatible = "maxim,max31785" },
385 	{ .compatible = "maxim,max31785a" },
386 	{ .compatible = "maxim,max31785b" },
387 	{ },
388 };
389 
390 MODULE_DEVICE_TABLE(of, max31785_of_match);
391 
392 static struct i2c_driver max31785_driver = {
393 	.driver = {
394 		.name = "max31785",
395 		.of_match_table = max31785_of_match,
396 	},
397 	.probe_new = max31785_probe,
398 	.id_table = max31785_id,
399 };
400 
401 module_i2c_driver(max31785_driver);
402 
403 MODULE_AUTHOR("Andrew Jeffery <andrew@aj.id.au>");
404 MODULE_DESCRIPTION("PMBus driver for the Maxim MAX31785");
405 MODULE_LICENSE("GPL");
406 MODULE_IMPORT_NS(PMBUS);
407