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.
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>
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
,
33 if (page
< MAX31785_NR_PAGES
)
39 case PMBUS_FAN_CONFIG_12
:
40 return pmbus_read_byte_data(client
, page
- MAX31785_NR_PAGES
,
47 static int max31785_write_byte(struct i2c_client
*client
, int page
, u8 value
)
49 if (page
< MAX31785_NR_PAGES
)
55 static int max31785_read_long_data(struct i2c_client
*client
, int page
,
58 unsigned char cmdbuf
[1];
59 unsigned char rspbuf
[4];
62 struct i2c_msg msg
[2] = {
66 .len
= sizeof(cmdbuf
),
72 .len
= sizeof(rspbuf
),
79 rc
= pmbus_set_page(client
, page
);
83 rc
= i2c_transfer(client
->adapter
, msg
, ARRAY_SIZE(msg
));
87 *data
= (rspbuf
[0] << (0 * 8)) | (rspbuf
[1] << (1 * 8)) |
88 (rspbuf
[2] << (2 * 8)) | (rspbuf
[3] << (3 * 8));
93 static int max31785_get_pwm(struct i2c_client
*client
, int page
)
97 rv
= pmbus_get_fan_rate_device(client
, page
, 0, percent
);
100 else if (rv
>= 0x8000)
102 else if (rv
>= 0x2711)
108 static int max31785_get_pwm_mode(struct i2c_client
*client
, int page
)
113 config
= pmbus_read_byte_data(client
, page
, PMBUS_FAN_CONFIG_12
);
117 command
= pmbus_read_word_data(client
, page
, PMBUS_FAN_COMMAND_1
);
121 if (config
& PB_FAN_1_RPM
)
122 return (command
>= 0x8000) ? 3 : 2;
124 if (command
>= 0x8000)
126 else if (command
>= 0x2711)
132 static int max31785_read_word_data(struct i2c_client
*client
, int page
,
139 case PMBUS_READ_FAN_SPEED_1
:
140 if (page
< MAX31785_NR_PAGES
)
143 rv
= max31785_read_long_data(client
, page
- MAX31785_NR_PAGES
,
148 rv
= (val
>> 16) & 0xffff;
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
;
159 case PMBUS_VIRT_PWM_1
:
160 rv
= max31785_get_pwm(client
, page
);
162 case PMBUS_VIRT_PWM_ENABLE_1
:
163 rv
= max31785_get_pwm_mode(client
, page
);
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
,
205 rate
= pmbus_get_fan_rate_cached(client
, page
, 0, percent
);
208 rate
= max31785_scale_pwm(rate
);
211 config
= PB_FAN_1_RPM
;
212 rate
= pmbus_get_fan_rate_cached(client
, page
, 0, rpm
);
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
,
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
);
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
,
262 .format
[PSC_FAN
] = direct
,
267 .format
[PSC_PWM
] = direct
,
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
)
312 for (i
= 0; i
< MAX31785_NR_FAN_PAGES
; i
++) {
313 ret
= i2c_smbus_write_byte_data(client
, PMBUS_PAGE
, i
);
317 ret
= i2c_smbus_read_word_data(client
, MFR_FAN_CONFIG
);
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
;
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;
341 if (!i2c_check_functionality(client
->adapter
,
342 I2C_FUNC_SMBUS_BYTE_DATA
|
343 I2C_FUNC_SMBUS_WORD_DATA
))
346 info
= devm_kzalloc(dev
, sizeof(struct pmbus_driver_info
), GFP_KERNEL
);
350 *info
= max31785_info
;
352 ret
= i2c_smbus_write_byte_data(client
, PMBUS_PAGE
, 255);
356 ret
= i2c_smbus_read_word_data(client
, MFR_REVISION
);
360 if (ret
== MAX31785A
) {
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");
370 ret
= max31785_configure_dual_tach(client
, info
);
375 return pmbus_do_probe(client
, id
, info
);
378 static const struct i2c_device_id max31785_id
[] = {
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" },
392 MODULE_DEVICE_TABLE(of
, max31785_of_match
);
394 static struct i2c_driver max31785_driver
= {
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");