1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) 2017 IBM Corp.
6 #include <linux/delay.h>
7 #include <linux/kernel.h>
8 #include <linux/module.h>
9 #include <linux/init.h>
10 #include <linux/err.h>
11 #include <linux/i2c.h>
16 MFR_FAN_CONFIG
= 0xf1,
19 #define MAX31785 0x3030
20 #define MAX31785A 0x3040
21 #define MAX31785B 0x3061
23 #define MFR_FAN_CONFIG_DUAL_TACH BIT(12)
25 #define MAX31785_NR_PAGES 23
26 #define MAX31785_NR_FAN_PAGES 6
27 #define MAX31785_WAIT_DELAY_US 250
29 struct max31785_data
{
30 ktime_t access
; /* Chip access time */
31 struct pmbus_driver_info info
;
34 #define to_max31785_data(x) container_of(x, struct max31785_data, info)
37 * MAX31785 Driver Workaround
39 * The MAX31785 fan controller occasionally exhibits communication issues.
40 * These issues are not indicated by the device itself, except for occasional
41 * NACK responses during master transactions. No error bits are set in STATUS_BYTE.
43 * To address this, we introduce a delay of 250us between consecutive accesses
44 * to the fan controller. This delay helps mitigate communication problems by
45 * allowing sufficient time between accesses.
47 static inline void max31785_wait(const struct max31785_data
*data
)
49 s64 delta
= ktime_us_delta(ktime_get(), data
->access
);
51 if (delta
< MAX31785_WAIT_DELAY_US
)
52 usleep_range(MAX31785_WAIT_DELAY_US
- delta
,
53 MAX31785_WAIT_DELAY_US
);
56 static int max31785_i2c_write_byte_data(struct i2c_client
*client
,
57 struct max31785_data
*driver_data
,
58 int command
, u16 data
)
62 max31785_wait(driver_data
);
63 rc
= i2c_smbus_write_byte_data(client
, command
, data
);
64 driver_data
->access
= ktime_get();
68 static int max31785_i2c_read_word_data(struct i2c_client
*client
,
69 struct max31785_data
*driver_data
,
74 max31785_wait(driver_data
);
75 rc
= i2c_smbus_read_word_data(client
, command
);
76 driver_data
->access
= ktime_get();
80 static int _max31785_read_byte_data(struct i2c_client
*client
,
81 struct max31785_data
*driver_data
,
82 int page
, int command
)
86 max31785_wait(driver_data
);
87 rc
= pmbus_read_byte_data(client
, page
, command
);
88 driver_data
->access
= ktime_get();
92 static int _max31785_write_byte_data(struct i2c_client
*client
,
93 struct max31785_data
*driver_data
,
94 int page
, int command
, u16 data
)
98 max31785_wait(driver_data
);
99 rc
= pmbus_write_byte_data(client
, page
, command
, data
);
100 driver_data
->access
= ktime_get();
104 static int _max31785_read_word_data(struct i2c_client
*client
,
105 struct max31785_data
*driver_data
,
106 int page
, int phase
, int command
)
110 max31785_wait(driver_data
);
111 rc
= pmbus_read_word_data(client
, page
, phase
, command
);
112 driver_data
->access
= ktime_get();
116 static int _max31785_write_word_data(struct i2c_client
*client
,
117 struct max31785_data
*driver_data
,
118 int page
, int command
, u16 data
)
122 max31785_wait(driver_data
);
123 rc
= pmbus_write_word_data(client
, page
, command
, data
);
124 driver_data
->access
= ktime_get();
128 static int max31785_read_byte_data(struct i2c_client
*client
, int page
, int reg
)
130 const struct pmbus_driver_info
*info
= pmbus_get_driver_info(client
);
131 struct max31785_data
*driver_data
= to_max31785_data(info
);
134 case PMBUS_VOUT_MODE
:
136 case PMBUS_FAN_CONFIG_12
:
137 return _max31785_read_byte_data(client
, driver_data
,
138 page
- MAX31785_NR_PAGES
,
145 static int max31785_write_byte(struct i2c_client
*client
, int page
, u8 value
)
147 if (page
< MAX31785_NR_PAGES
)
153 static int max31785_read_long_data(struct i2c_client
*client
, int page
,
156 unsigned char cmdbuf
[1];
157 unsigned char rspbuf
[4];
160 struct i2c_msg msg
[2] = {
162 .addr
= client
->addr
,
164 .len
= sizeof(cmdbuf
),
168 .addr
= client
->addr
,
170 .len
= sizeof(rspbuf
),
177 rc
= pmbus_set_page(client
, page
, 0xff);
181 rc
= i2c_transfer(client
->adapter
, msg
, ARRAY_SIZE(msg
));
185 *data
= (rspbuf
[0] << (0 * 8)) | (rspbuf
[1] << (1 * 8)) |
186 (rspbuf
[2] << (2 * 8)) | (rspbuf
[3] << (3 * 8));
191 static int max31785_get_pwm(struct i2c_client
*client
, int page
)
195 rv
= pmbus_get_fan_rate_device(client
, page
, 0, percent
);
198 else if (rv
>= 0x8000)
200 else if (rv
>= 0x2711)
206 static int max31785_get_pwm_mode(struct i2c_client
*client
,
207 struct max31785_data
*driver_data
, int page
)
212 config
= _max31785_read_byte_data(client
, driver_data
, page
,
213 PMBUS_FAN_CONFIG_12
);
217 command
= _max31785_read_word_data(client
, driver_data
, page
, 0xff,
218 PMBUS_FAN_COMMAND_1
);
222 if (config
& PB_FAN_1_RPM
)
223 return (command
>= 0x8000) ? 3 : 2;
225 if (command
>= 0x8000)
227 else if (command
>= 0x2711)
233 static int max31785_read_word_data(struct i2c_client
*client
, int page
,
236 const struct pmbus_driver_info
*info
= pmbus_get_driver_info(client
);
237 struct max31785_data
*driver_data
= to_max31785_data(info
);
242 case PMBUS_READ_FAN_SPEED_1
:
243 if (page
< MAX31785_NR_PAGES
)
246 rv
= max31785_read_long_data(client
, page
- MAX31785_NR_PAGES
,
251 rv
= (val
>> 16) & 0xffff;
253 case PMBUS_FAN_COMMAND_1
:
255 * PMBUS_FAN_COMMAND_x is probed to judge whether or not to
256 * expose fan control registers.
258 * Don't expose fan_target attribute for virtual pages.
260 rv
= (page
>= MAX31785_NR_PAGES
) ? -ENOTSUPP
: -ENODATA
;
262 case PMBUS_VIRT_PWM_1
:
263 rv
= max31785_get_pwm(client
, page
);
265 case PMBUS_VIRT_PWM_ENABLE_1
:
266 rv
= max31785_get_pwm_mode(client
, driver_data
, page
);
276 static inline u32
max31785_scale_pwm(u32 sensor_val
)
279 * The datasheet describes the accepted value range for manual PWM as
280 * [0, 0x2710], while the hwmon pwmX sysfs interface accepts values in
281 * [0, 255]. The MAX31785 uses DIRECT mode to scale the FAN_COMMAND
282 * registers and in PWM mode the coefficients are m=1, b=0, R=2. The
283 * important observation here is that 0x2710 == 10000 == 100 * 100.
285 * R=2 (== 10^2 == 100) accounts for scaling the value provided at the
286 * sysfs interface into the required hardware resolution, but it does
287 * not yet yield a value that we can write to the device (this initial
288 * scaling is handled by pmbus_data2reg()). Multiplying by 100 below
289 * translates the parameter value into the percentage units required by
290 * PMBus, and then we scale back by 255 as required by the hwmon pwmX
291 * interface to yield the percentage value at the appropriate
292 * resolution for hardware.
294 return (sensor_val
* 100) / 255;
297 static int max31785_update_fan(struct i2c_client
*client
,
298 struct max31785_data
*driver_data
, int page
,
299 u8 config
, u8 mask
, u16 command
)
304 from
= _max31785_read_byte_data(client
, driver_data
, page
,
305 PMBUS_FAN_CONFIG_12
);
309 to
= (from
& ~mask
) | (config
& mask
);
312 rv
= _max31785_write_byte_data(client
, driver_data
, page
,
313 PMBUS_FAN_CONFIG_12
, to
);
318 rv
= _max31785_write_word_data(client
, driver_data
, page
,
319 PMBUS_FAN_COMMAND_1
, command
);
324 static int max31785_pwm_enable(struct i2c_client
*client
,
325 struct max31785_data
*driver_data
, int page
,
336 rate
= pmbus_get_fan_rate_cached(client
, page
, 0, percent
);
339 rate
= max31785_scale_pwm(rate
);
342 config
= PB_FAN_1_RPM
;
343 rate
= pmbus_get_fan_rate_cached(client
, page
, 0, rpm
);
354 return max31785_update_fan(client
, driver_data
, page
, config
,
358 static int max31785_write_word_data(struct i2c_client
*client
, int page
,
361 const struct pmbus_driver_info
*info
= pmbus_get_driver_info(client
);
362 struct max31785_data
*driver_data
= to_max31785_data(info
);
365 case PMBUS_VIRT_PWM_1
:
366 return max31785_update_fan(client
, driver_data
, page
, 0,
368 max31785_scale_pwm(word
));
369 case PMBUS_VIRT_PWM_ENABLE_1
:
370 return max31785_pwm_enable(client
, driver_data
, page
, word
);
378 #define MAX31785_FAN_FUNCS \
379 (PMBUS_HAVE_FAN12 | PMBUS_HAVE_STATUS_FAN12 | PMBUS_HAVE_PWM12)
381 #define MAX31785_TEMP_FUNCS \
382 (PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP)
384 #define MAX31785_VOUT_FUNCS \
385 (PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT)
387 static const struct pmbus_driver_info max31785_info
= {
388 .pages
= MAX31785_NR_PAGES
,
390 .write_word_data
= max31785_write_word_data
,
391 .read_byte_data
= max31785_read_byte_data
,
392 .read_word_data
= max31785_read_word_data
,
393 .write_byte
= max31785_write_byte
,
396 .format
[PSC_FAN
] = direct
,
401 .format
[PSC_PWM
] = direct
,
405 .func
[0] = MAX31785_FAN_FUNCS
,
406 .func
[1] = MAX31785_FAN_FUNCS
,
407 .func
[2] = MAX31785_FAN_FUNCS
,
408 .func
[3] = MAX31785_FAN_FUNCS
,
409 .func
[4] = MAX31785_FAN_FUNCS
,
410 .func
[5] = MAX31785_FAN_FUNCS
,
412 .format
[PSC_TEMPERATURE
] = direct
,
413 .m
[PSC_TEMPERATURE
] = 1,
414 .b
[PSC_TEMPERATURE
] = 0,
415 .R
[PSC_TEMPERATURE
] = 2,
416 .func
[6] = MAX31785_TEMP_FUNCS
,
417 .func
[7] = MAX31785_TEMP_FUNCS
,
418 .func
[8] = MAX31785_TEMP_FUNCS
,
419 .func
[9] = MAX31785_TEMP_FUNCS
,
420 .func
[10] = MAX31785_TEMP_FUNCS
,
421 .func
[11] = MAX31785_TEMP_FUNCS
,
422 .func
[12] = MAX31785_TEMP_FUNCS
,
423 .func
[13] = MAX31785_TEMP_FUNCS
,
424 .func
[14] = MAX31785_TEMP_FUNCS
,
425 .func
[15] = MAX31785_TEMP_FUNCS
,
426 .func
[16] = MAX31785_TEMP_FUNCS
,
428 .format
[PSC_VOLTAGE_OUT
] = direct
,
429 .m
[PSC_VOLTAGE_OUT
] = 1,
430 .b
[PSC_VOLTAGE_OUT
] = 0,
431 .R
[PSC_VOLTAGE_OUT
] = 0,
432 .func
[17] = MAX31785_VOUT_FUNCS
,
433 .func
[18] = MAX31785_VOUT_FUNCS
,
434 .func
[19] = MAX31785_VOUT_FUNCS
,
435 .func
[20] = MAX31785_VOUT_FUNCS
,
436 .func
[21] = MAX31785_VOUT_FUNCS
,
437 .func
[22] = MAX31785_VOUT_FUNCS
,
440 static int max31785_configure_dual_tach(struct i2c_client
*client
,
441 struct pmbus_driver_info
*info
)
445 struct max31785_data
*driver_data
= to_max31785_data(info
);
447 for (i
= 0; i
< MAX31785_NR_FAN_PAGES
; i
++) {
448 ret
= max31785_i2c_write_byte_data(client
, driver_data
,
453 ret
= max31785_i2c_read_word_data(client
, driver_data
,
458 if (ret
& MFR_FAN_CONFIG_DUAL_TACH
) {
459 int virtual = MAX31785_NR_PAGES
+ i
;
461 info
->pages
= virtual + 1;
462 info
->func
[virtual] |= PMBUS_HAVE_FAN12
;
463 info
->func
[virtual] |= PMBUS_PAGE_VIRTUAL
;
470 static int max31785_probe(struct i2c_client
*client
)
472 struct device
*dev
= &client
->dev
;
473 struct pmbus_driver_info
*info
;
474 struct max31785_data
*driver_data
;
475 bool dual_tach
= false;
478 if (!i2c_check_functionality(client
->adapter
,
479 I2C_FUNC_SMBUS_BYTE_DATA
|
480 I2C_FUNC_SMBUS_WORD_DATA
))
483 driver_data
= devm_kzalloc(dev
, sizeof(struct max31785_data
), GFP_KERNEL
);
487 info
= &driver_data
->info
;
488 driver_data
->access
= ktime_get();
489 *info
= max31785_info
;
491 ret
= max31785_i2c_write_byte_data(client
, driver_data
,
496 ret
= i2c_smbus_read_word_data(client
, MFR_REVISION
);
500 if (ret
== MAX31785A
|| ret
== MAX31785B
) {
502 } else if (ret
== MAX31785
) {
503 if (!strcmp("max31785a", client
->name
) ||
504 !strcmp("max31785b", client
->name
))
505 dev_warn(dev
, "Expected max31785a/b, found max31785: cannot provide secondary tachometer readings\n");
507 dev_err(dev
, "Unrecognized MAX31785 revision: %x\n", ret
);
512 ret
= max31785_configure_dual_tach(client
, info
);
517 return pmbus_do_probe(client
, info
);
520 static const struct i2c_device_id max31785_id
[] = {
527 MODULE_DEVICE_TABLE(i2c
, max31785_id
);
529 static const struct of_device_id max31785_of_match
[] = {
530 { .compatible
= "maxim,max31785" },
531 { .compatible
= "maxim,max31785a" },
532 { .compatible
= "maxim,max31785b" },
536 MODULE_DEVICE_TABLE(of
, max31785_of_match
);
538 static struct i2c_driver max31785_driver
= {
541 .of_match_table
= max31785_of_match
,
543 .probe
= max31785_probe
,
544 .id_table
= max31785_id
,
547 module_i2c_driver(max31785_driver
);
549 MODULE_AUTHOR("Andrew Jeffery <andrew@aj.id.au>");
550 MODULE_DESCRIPTION("PMBus driver for the Maxim MAX31785");
551 MODULE_LICENSE("GPL");
552 MODULE_IMPORT_NS("PMBUS");