Linux 4.19.133
[linux/fpc-iii.git] / drivers / hwmon / pmbus / max31785.c
blobc9dc8799b5e147b0a84990355bb6df8a12b67324
1 /*
2 * Copyright (C) 2017 IBM Corp.
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 */
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"
17 enum max31785_regs {
18 MFR_REVISION = 0x9b,
19 MFR_FAN_CONFIG = 0xf1,
22 #define MAX31785 0x3030
23 #define MAX31785A 0x3040
25 #define MFR_FAN_CONFIG_DUAL_TACH BIT(12)
27 #define MAX31785_NR_PAGES 23
28 #define MAX31785_NR_FAN_PAGES 6
30 static int max31785_read_byte_data(struct i2c_client *client, int page,
31 int reg)
33 if (page < MAX31785_NR_PAGES)
34 return -ENODATA;
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);
44 return -ENODATA;
47 static int max31785_write_byte(struct i2c_client *client, int page, u8 value)
49 if (page < MAX31785_NR_PAGES)
50 return -ENODATA;
52 return -ENOTSUPP;
55 static int max31785_read_long_data(struct i2c_client *client, int page,
56 int reg, u32 *data)
58 unsigned char cmdbuf[1];
59 unsigned char rspbuf[4];
60 int rc;
62 struct i2c_msg msg[2] = {
64 .addr = client->addr,
65 .flags = 0,
66 .len = sizeof(cmdbuf),
67 .buf = cmdbuf,
70 .addr = client->addr,
71 .flags = I2C_M_RD,
72 .len = sizeof(rspbuf),
73 .buf = rspbuf,
77 cmdbuf[0] = reg;
79 rc = pmbus_set_page(client, page);
80 if (rc < 0)
81 return rc;
83 rc = i2c_transfer(client->adapter, msg, ARRAY_SIZE(msg));
84 if (rc < 0)
85 return rc;
87 *data = (rspbuf[0] << (0 * 8)) | (rspbuf[1] << (1 * 8)) |
88 (rspbuf[2] << (2 * 8)) | (rspbuf[3] << (3 * 8));
90 return rc;
93 static int max31785_get_pwm(struct i2c_client *client, int page)
95 int rv;
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;
105 return rv;
108 static int max31785_get_pwm_mode(struct i2c_client *client, int page)
110 int config;
111 int command;
113 config = pmbus_read_byte_data(client, page, PMBUS_FAN_CONFIG_12);
114 if (config < 0)
115 return config;
117 command = pmbus_read_word_data(client, page, PMBUS_FAN_COMMAND_1);
118 if (command < 0)
119 return command;
121 if (config & PB_FAN_1_RPM)
122 return (command >= 0x8000) ? 3 : 2;
124 if (command >= 0x8000)
125 return 3;
126 else if (command >= 0x2711)
127 return 0;
129 return 1;
132 static int max31785_read_word_data(struct i2c_client *client, int page,
133 int reg)
135 u32 val;
136 int rv;
138 switch (reg) {
139 case PMBUS_READ_FAN_SPEED_1:
140 if (page < MAX31785_NR_PAGES)
141 return -ENODATA;
143 rv = max31785_read_long_data(client, page - MAX31785_NR_PAGES,
144 reg, &val);
145 if (rv < 0)
146 return rv;
148 rv = (val >> 16) & 0xffff;
149 break;
150 case PMBUS_FAN_COMMAND_1:
152 * PMBUS_FAN_COMMAND_x is probed to judge whether or not to
153 * expose fan control registers.
155 * Don't expose fan_target attribute for virtual pages.
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;
170 return rv;
173 static inline u32 max31785_scale_pwm(u32 sensor_val)
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.
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.
191 return (sensor_val * 100) / 255;
194 static int max31785_pwm_enable(struct i2c_client *client, int page,
195 u16 word)
197 int config = 0;
198 int rate;
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;
223 return pmbus_update_fan(client, page, 0, config, PB_FAN_1_RPM, rate);
226 static int max31785_write_word_data(struct i2c_client *client, int page,
227 int reg, u16 word)
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;
239 return -ENODATA;
242 #define MAX31785_FAN_FUNCS \
243 (PMBUS_HAVE_FAN12 | PMBUS_HAVE_STATUS_FAN12 | PMBUS_HAVE_PWM12)
245 #define MAX31785_TEMP_FUNCS \
246 (PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP)
248 #define MAX31785_VOUT_FUNCS \
249 (PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT)
251 #define MAX37185_NUM_FAN_PAGES 6
253 static const struct pmbus_driver_info max31785_info = {
254 .pages = MAX31785_NR_PAGES,
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,
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,
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,
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,
306 static int max31785_configure_dual_tach(struct i2c_client *client,
307 struct pmbus_driver_info *info)
309 int ret;
310 int i;
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;
317 ret = i2c_smbus_read_word_data(client, MFR_FAN_CONFIG);
318 if (ret < 0)
319 return ret;
321 if (ret & MFR_FAN_CONFIG_DUAL_TACH) {
322 int virtual = MAX31785_NR_PAGES + i;
324 info->pages = virtual + 1;
325 info->func[virtual] |= PMBUS_HAVE_FAN12;
326 info->func[virtual] |= PMBUS_PAGE_VIRTUAL;
330 return 0;
333 static int max31785_probe(struct i2c_client *client,
334 const struct i2c_device_id *id)
336 struct device *dev = &client->dev;
337 struct pmbus_driver_info *info;
338 bool dual_tach = false;
339 s64 ret;
341 if (!i2c_check_functionality(client->adapter,
342 I2C_FUNC_SMBUS_BYTE_DATA |
343 I2C_FUNC_SMBUS_WORD_DATA))
344 return -ENODEV;
346 info = devm_kzalloc(dev, sizeof(struct pmbus_driver_info), GFP_KERNEL);
347 if (!info)
348 return -ENOMEM;
350 *info = max31785_info;
352 ret = i2c_smbus_write_byte_data(client, PMBUS_PAGE, 255);
353 if (ret < 0)
354 return ret;
356 ret = i2c_smbus_read_word_data(client, MFR_REVISION);
357 if (ret < 0)
358 return ret;
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;
369 if (dual_tach) {
370 ret = max31785_configure_dual_tach(client, info);
371 if (ret < 0)
372 return ret;
375 return pmbus_do_probe(client, id, info);
378 static const struct i2c_device_id max31785_id[] = {
379 { "max31785", 0 },
380 { "max31785a", 0 },
381 { },
384 MODULE_DEVICE_TABLE(i2c, max31785_id);
386 static const struct of_device_id max31785_of_match[] = {
387 { .compatible = "maxim,max31785" },
388 { .compatible = "maxim,max31785a" },
389 { },
392 MODULE_DEVICE_TABLE(of, max31785_of_match);
394 static struct i2c_driver max31785_driver = {
395 .driver = {
396 .name = "max31785",
397 .of_match_table = max31785_of_match,
399 .probe = max31785_probe,
400 .remove = pmbus_do_remove,
401 .id_table = max31785_id,
404 module_i2c_driver(max31785_driver);
406 MODULE_AUTHOR("Andrew Jeffery <andrew@aj.id.au>");
407 MODULE_DESCRIPTION("PMBus driver for the Maxim MAX31785");
408 MODULE_LICENSE("GPL");