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