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