1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) 2017 IBM Corp.
6 #include <linux/kernel.h>
7 #include <linux/module.h>
8 #include <linux/init.h>
10 #include <linux/i2c.h>
15 MFR_FAN_CONFIG
= 0xf1,
18 #define MAX31785 0x3030
19 #define MAX31785A 0x3040
21 #define MFR_FAN_CONFIG_DUAL_TACH BIT(12)
23 #define MAX31785_NR_PAGES 23
24 #define MAX31785_NR_FAN_PAGES 6
26 static int max31785_read_byte_data(struct i2c_client
*client
, int page
,
29 if (page
< MAX31785_NR_PAGES
)
35 case PMBUS_FAN_CONFIG_12
:
36 return pmbus_read_byte_data(client
, page
- MAX31785_NR_PAGES
,
43 static int max31785_write_byte(struct i2c_client
*client
, int page
, u8 value
)
45 if (page
< MAX31785_NR_PAGES
)
51 static int max31785_read_long_data(struct i2c_client
*client
, int page
,
54 unsigned char cmdbuf
[1];
55 unsigned char rspbuf
[4];
58 struct i2c_msg msg
[2] = {
62 .len
= sizeof(cmdbuf
),
68 .len
= sizeof(rspbuf
),
75 rc
= pmbus_set_page(client
, page
, 0xff);
79 rc
= i2c_transfer(client
->adapter
, msg
, ARRAY_SIZE(msg
));
83 *data
= (rspbuf
[0] << (0 * 8)) | (rspbuf
[1] << (1 * 8)) |
84 (rspbuf
[2] << (2 * 8)) | (rspbuf
[3] << (3 * 8));
89 static int max31785_get_pwm(struct i2c_client
*client
, int page
)
93 rv
= pmbus_get_fan_rate_device(client
, page
, 0, percent
);
96 else if (rv
>= 0x8000)
98 else if (rv
>= 0x2711)
104 static int max31785_get_pwm_mode(struct i2c_client
*client
, int page
)
109 config
= pmbus_read_byte_data(client
, page
, PMBUS_FAN_CONFIG_12
);
113 command
= pmbus_read_word_data(client
, page
, 0xff, PMBUS_FAN_COMMAND_1
);
117 if (config
& PB_FAN_1_RPM
)
118 return (command
>= 0x8000) ? 3 : 2;
120 if (command
>= 0x8000)
122 else if (command
>= 0x2711)
128 static int max31785_read_word_data(struct i2c_client
*client
, int page
,
135 case PMBUS_READ_FAN_SPEED_1
:
136 if (page
< MAX31785_NR_PAGES
)
139 rv
= max31785_read_long_data(client
, page
- MAX31785_NR_PAGES
,
144 rv
= (val
>> 16) & 0xffff;
146 case PMBUS_FAN_COMMAND_1
:
148 * PMBUS_FAN_COMMAND_x is probed to judge whether or not to
149 * expose fan control registers.
151 * Don't expose fan_target attribute for virtual pages.
153 rv
= (page
>= MAX31785_NR_PAGES
) ? -ENOTSUPP
: -ENODATA
;
155 case PMBUS_VIRT_PWM_1
:
156 rv
= max31785_get_pwm(client
, page
);
158 case PMBUS_VIRT_PWM_ENABLE_1
:
159 rv
= max31785_get_pwm_mode(client
, page
);
169 static inline u32
max31785_scale_pwm(u32 sensor_val
)
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.
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.
187 return (sensor_val
* 100) / 255;
190 static int max31785_pwm_enable(struct i2c_client
*client
, int page
,
201 rate
= pmbus_get_fan_rate_cached(client
, page
, 0, percent
);
204 rate
= max31785_scale_pwm(rate
);
207 config
= PB_FAN_1_RPM
;
208 rate
= pmbus_get_fan_rate_cached(client
, page
, 0, rpm
);
219 return pmbus_update_fan(client
, page
, 0, config
, PB_FAN_1_RPM
, rate
);
222 static int max31785_write_word_data(struct i2c_client
*client
, int page
,
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
);
238 #define MAX31785_FAN_FUNCS \
239 (PMBUS_HAVE_FAN12 | PMBUS_HAVE_STATUS_FAN12 | PMBUS_HAVE_PWM12)
241 #define MAX31785_TEMP_FUNCS \
242 (PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP)
244 #define MAX31785_VOUT_FUNCS \
245 (PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT)
247 static const struct pmbus_driver_info max31785_info
= {
248 .pages
= MAX31785_NR_PAGES
,
250 .write_word_data
= max31785_write_word_data
,
251 .read_byte_data
= max31785_read_byte_data
,
252 .read_word_data
= max31785_read_word_data
,
253 .write_byte
= max31785_write_byte
,
256 .format
[PSC_FAN
] = direct
,
261 .format
[PSC_PWM
] = direct
,
265 .func
[0] = MAX31785_FAN_FUNCS
,
266 .func
[1] = MAX31785_FAN_FUNCS
,
267 .func
[2] = MAX31785_FAN_FUNCS
,
268 .func
[3] = MAX31785_FAN_FUNCS
,
269 .func
[4] = MAX31785_FAN_FUNCS
,
270 .func
[5] = MAX31785_FAN_FUNCS
,
272 .format
[PSC_TEMPERATURE
] = direct
,
273 .m
[PSC_TEMPERATURE
] = 1,
274 .b
[PSC_TEMPERATURE
] = 0,
275 .R
[PSC_TEMPERATURE
] = 2,
276 .func
[6] = MAX31785_TEMP_FUNCS
,
277 .func
[7] = MAX31785_TEMP_FUNCS
,
278 .func
[8] = MAX31785_TEMP_FUNCS
,
279 .func
[9] = MAX31785_TEMP_FUNCS
,
280 .func
[10] = MAX31785_TEMP_FUNCS
,
281 .func
[11] = MAX31785_TEMP_FUNCS
,
282 .func
[12] = MAX31785_TEMP_FUNCS
,
283 .func
[13] = MAX31785_TEMP_FUNCS
,
284 .func
[14] = MAX31785_TEMP_FUNCS
,
285 .func
[15] = MAX31785_TEMP_FUNCS
,
286 .func
[16] = MAX31785_TEMP_FUNCS
,
288 .format
[PSC_VOLTAGE_OUT
] = direct
,
289 .m
[PSC_VOLTAGE_OUT
] = 1,
290 .b
[PSC_VOLTAGE_OUT
] = 0,
291 .R
[PSC_VOLTAGE_OUT
] = 0,
292 .func
[17] = MAX31785_VOUT_FUNCS
,
293 .func
[18] = MAX31785_VOUT_FUNCS
,
294 .func
[19] = MAX31785_VOUT_FUNCS
,
295 .func
[20] = MAX31785_VOUT_FUNCS
,
296 .func
[21] = MAX31785_VOUT_FUNCS
,
297 .func
[22] = MAX31785_VOUT_FUNCS
,
300 static int max31785_configure_dual_tach(struct i2c_client
*client
,
301 struct pmbus_driver_info
*info
)
306 for (i
= 0; i
< MAX31785_NR_FAN_PAGES
; i
++) {
307 ret
= i2c_smbus_write_byte_data(client
, PMBUS_PAGE
, i
);
311 ret
= i2c_smbus_read_word_data(client
, MFR_FAN_CONFIG
);
315 if (ret
& MFR_FAN_CONFIG_DUAL_TACH
) {
316 int virtual = MAX31785_NR_PAGES
+ i
;
318 info
->pages
= virtual + 1;
319 info
->func
[virtual] |= PMBUS_HAVE_FAN12
;
320 info
->func
[virtual] |= PMBUS_PAGE_VIRTUAL
;
327 static int max31785_probe(struct i2c_client
*client
)
329 struct device
*dev
= &client
->dev
;
330 struct pmbus_driver_info
*info
;
331 bool dual_tach
= false;
334 if (!i2c_check_functionality(client
->adapter
,
335 I2C_FUNC_SMBUS_BYTE_DATA
|
336 I2C_FUNC_SMBUS_WORD_DATA
))
339 info
= devm_kzalloc(dev
, sizeof(struct pmbus_driver_info
), GFP_KERNEL
);
343 *info
= max31785_info
;
345 ret
= i2c_smbus_write_byte_data(client
, PMBUS_PAGE
, 255);
349 ret
= i2c_smbus_read_word_data(client
, MFR_REVISION
);
353 if (ret
== MAX31785A
) {
355 } else if (ret
== MAX31785
) {
356 if (!strcmp("max31785a", client
->name
))
357 dev_warn(dev
, "Expected max3175a, found max31785: cannot provide secondary tachometer readings\n");
363 ret
= max31785_configure_dual_tach(client
, info
);
368 return pmbus_do_probe(client
, info
);
371 static const struct i2c_device_id max31785_id
[] = {
377 MODULE_DEVICE_TABLE(i2c
, max31785_id
);
379 static const struct of_device_id max31785_of_match
[] = {
380 { .compatible
= "maxim,max31785" },
381 { .compatible
= "maxim,max31785a" },
385 MODULE_DEVICE_TABLE(of
, max31785_of_match
);
387 static struct i2c_driver max31785_driver
= {
390 .of_match_table
= max31785_of_match
,
392 .probe_new
= max31785_probe
,
393 .id_table
= max31785_id
,
396 module_i2c_driver(max31785_driver
);
398 MODULE_AUTHOR("Andrew Jeffery <andrew@aj.id.au>");
399 MODULE_DESCRIPTION("PMBus driver for the Maxim MAX31785");
400 MODULE_LICENSE("GPL");