1 // SPDX-License-Identifier: GPL-2.0+
3 * Hardware monitoring driver for Renesas Digital Multiphase Voltage Regulators
5 * Copyright (c) 2017 Google Inc
6 * Copyright (c) 2020 Renesas Electronics America
10 #include <linux/err.h>
11 #include <linux/hwmon-sysfs.h>
12 #include <linux/i2c.h>
13 #include <linux/init.h>
14 #include <linux/kernel.h>
15 #include <linux/module.h>
17 #include <linux/string.h>
18 #include <linux/sysfs.h>
22 #define ISL68137_VOUT_AVS 0x30
23 #define RAA_DMPVR2_READ_VMON 0xc8
24 #define MAX_CHANNELS 4
72 raa_dmpvr2_2rail_nontc
,
77 struct isl68137_channel
{
78 u32 vout_voltage_divider
[2];
81 struct isl68137_data
{
82 struct pmbus_driver_info info
;
83 struct isl68137_channel channel
[MAX_CHANNELS
];
86 #define to_isl68137_data(x) container_of(x, struct isl68137_data, info)
88 static const struct i2c_device_id raa_dmpvr_id
[];
90 static ssize_t
isl68137_avs_enable_show_page(struct i2c_client
*client
,
94 int val
= pmbus_read_byte_data(client
, page
, PMBUS_OPERATION
);
96 return sprintf(buf
, "%d\n",
97 (val
& ISL68137_VOUT_AVS
) == ISL68137_VOUT_AVS
? 1 : 0);
100 static ssize_t
isl68137_avs_enable_store_page(struct i2c_client
*client
,
102 const char *buf
, size_t count
)
107 rc
= kstrtobool(buf
, &result
);
111 op_val
= result
? ISL68137_VOUT_AVS
: 0;
114 * Writes to VOUT setpoint over AVSBus will persist after the VRM is
115 * switched to PMBus control. Switching back to AVSBus control
116 * restores this persisted setpoint rather than re-initializing to
117 * PMBus VOUT_COMMAND. Writing VOUT_COMMAND first over PMBus before
118 * enabling AVS control is the workaround.
120 if (op_val
== ISL68137_VOUT_AVS
) {
121 rc
= pmbus_read_word_data(client
, page
, 0xff,
126 rc
= pmbus_write_word_data(client
, page
, PMBUS_VOUT_COMMAND
,
132 rc
= pmbus_update_byte_data(client
, page
, PMBUS_OPERATION
,
133 ISL68137_VOUT_AVS
, op_val
);
135 return (rc
< 0) ? rc
: count
;
138 static ssize_t
isl68137_avs_enable_show(struct device
*dev
,
139 struct device_attribute
*devattr
,
142 struct i2c_client
*client
= to_i2c_client(dev
->parent
);
143 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(devattr
);
145 return isl68137_avs_enable_show_page(client
, attr
->index
, buf
);
148 static ssize_t
isl68137_avs_enable_store(struct device
*dev
,
149 struct device_attribute
*devattr
,
150 const char *buf
, size_t count
)
152 struct i2c_client
*client
= to_i2c_client(dev
->parent
);
153 struct sensor_device_attribute
*attr
= to_sensor_dev_attr(devattr
);
155 return isl68137_avs_enable_store_page(client
, attr
->index
, buf
, count
);
158 static SENSOR_DEVICE_ATTR_RW(avs0_enable
, isl68137_avs_enable
, 0);
159 static SENSOR_DEVICE_ATTR_RW(avs1_enable
, isl68137_avs_enable
, 1);
161 static struct attribute
*enable_attrs
[] = {
162 &sensor_dev_attr_avs0_enable
.dev_attr
.attr
,
163 &sensor_dev_attr_avs1_enable
.dev_attr
.attr
,
167 static const struct attribute_group enable_group
= {
168 .attrs
= enable_attrs
,
171 static const struct attribute_group
*isl68137_attribute_groups
[] = {
176 static int raa_dmpvr2_read_word_data(struct i2c_client
*client
, int page
,
179 const struct pmbus_driver_info
*info
= pmbus_get_driver_info(client
);
180 const struct isl68137_data
*data
= to_isl68137_data(info
);
185 case PMBUS_VIRT_READ_VMON
:
186 ret
= pmbus_read_word_data(client
, page
, phase
,
187 RAA_DMPVR2_READ_VMON
);
189 case PMBUS_READ_POUT
:
190 case PMBUS_READ_VOUT
:
192 * In cases where a voltage divider is attached to the target
193 * rail between Vout and the Vsense pin, both Vout and Pout
194 * should be scaled by the voltage divider scaling factor.
195 * I.e. Vout = Vsense * Rtotal / Rout
197 ret
= pmbus_read_word_data(client
, page
, phase
, reg
);
199 temp
= DIV_U64_ROUND_CLOSEST((u64
)ret
*
200 data
->channel
[page
].vout_voltage_divider
[1],
201 data
->channel
[page
].vout_voltage_divider
[0]);
202 ret
= clamp_val(temp
, 0, 0xffff);
213 static int raa_dmpvr2_write_word_data(struct i2c_client
*client
, int page
,
216 const struct pmbus_driver_info
*info
= pmbus_get_driver_info(client
);
217 const struct isl68137_data
*data
= to_isl68137_data(info
);
223 case PMBUS_VOUT_MARGIN_HIGH
:
224 case PMBUS_VOUT_MARGIN_LOW
:
225 case PMBUS_VOUT_OV_FAULT_LIMIT
:
226 case PMBUS_VOUT_UV_FAULT_LIMIT
:
227 case PMBUS_VOUT_COMMAND
:
229 * In cases where a voltage divider is attached to the target
230 * rail between Vout and the Vsense pin, Vout related PMBus
231 * commands should be scaled based on the expected voltage
233 * I.e. Vsense = Vout * Rout / Rtotal
235 temp
= DIV_U64_ROUND_CLOSEST((u64
)word
*
236 data
->channel
[page
].vout_voltage_divider
[0],
237 data
->channel
[page
].vout_voltage_divider
[1]);
238 ret
= clamp_val(temp
, 0, 0xffff);
247 static struct pmbus_driver_info raa_dmpvr_info
= {
249 .format
[PSC_VOLTAGE_IN
] = direct
,
250 .format
[PSC_VOLTAGE_OUT
] = direct
,
251 .format
[PSC_CURRENT_IN
] = direct
,
252 .format
[PSC_CURRENT_OUT
] = direct
,
253 .format
[PSC_POWER
] = direct
,
254 .format
[PSC_TEMPERATURE
] = direct
,
255 .m
[PSC_VOLTAGE_IN
] = 1,
256 .b
[PSC_VOLTAGE_IN
] = 0,
257 .R
[PSC_VOLTAGE_IN
] = 2,
258 .m
[PSC_VOLTAGE_OUT
] = 1,
259 .b
[PSC_VOLTAGE_OUT
] = 0,
260 .R
[PSC_VOLTAGE_OUT
] = 3,
261 .m
[PSC_CURRENT_IN
] = 1,
262 .b
[PSC_CURRENT_IN
] = 0,
263 .R
[PSC_CURRENT_IN
] = 2,
264 .m
[PSC_CURRENT_OUT
] = 1,
265 .b
[PSC_CURRENT_OUT
] = 0,
266 .R
[PSC_CURRENT_OUT
] = 1,
270 .m
[PSC_TEMPERATURE
] = 1,
271 .b
[PSC_TEMPERATURE
] = 0,
272 .R
[PSC_TEMPERATURE
] = 0,
273 .func
[0] = PMBUS_HAVE_VIN
| PMBUS_HAVE_IIN
| PMBUS_HAVE_PIN
274 | PMBUS_HAVE_STATUS_INPUT
| PMBUS_HAVE_TEMP
| PMBUS_HAVE_TEMP2
275 | PMBUS_HAVE_TEMP3
| PMBUS_HAVE_STATUS_TEMP
276 | PMBUS_HAVE_VOUT
| PMBUS_HAVE_STATUS_VOUT
277 | PMBUS_HAVE_IOUT
| PMBUS_HAVE_STATUS_IOUT
| PMBUS_HAVE_POUT
279 .func
[1] = PMBUS_HAVE_IIN
| PMBUS_HAVE_PIN
| PMBUS_HAVE_STATUS_INPUT
280 | PMBUS_HAVE_TEMP
| PMBUS_HAVE_TEMP3
| PMBUS_HAVE_STATUS_TEMP
281 | PMBUS_HAVE_VOUT
| PMBUS_HAVE_STATUS_VOUT
| PMBUS_HAVE_IOUT
282 | PMBUS_HAVE_STATUS_IOUT
| PMBUS_HAVE_POUT
,
283 .func
[2] = PMBUS_HAVE_IIN
| PMBUS_HAVE_PIN
| PMBUS_HAVE_STATUS_INPUT
284 | PMBUS_HAVE_TEMP
| PMBUS_HAVE_TEMP3
| PMBUS_HAVE_STATUS_TEMP
285 | PMBUS_HAVE_VOUT
| PMBUS_HAVE_STATUS_VOUT
| PMBUS_HAVE_IOUT
286 | PMBUS_HAVE_STATUS_IOUT
| PMBUS_HAVE_POUT
,
289 static int isl68137_probe_child_from_dt(struct device
*dev
,
290 struct device_node
*child
,
291 struct isl68137_data
*data
)
293 u32 channel
, rout
, rtotal
;
296 err
= of_property_read_u32(child
, "reg", &channel
);
298 dev_err(dev
, "missing reg property of %pOFn\n", child
);
301 if (channel
>= data
->info
.pages
) {
302 dev_err(dev
, "invalid reg %d of %pOFn\n", channel
, child
);
306 err
= of_property_read_u32_array(child
, "vout-voltage-divider",
307 data
->channel
[channel
].vout_voltage_divider
,
308 ARRAY_SIZE(data
->channel
[channel
].vout_voltage_divider
));
309 if (err
&& err
!= -EINVAL
) {
311 "malformed vout-voltage-divider value for channel %d\n",
316 rout
= data
->channel
[channel
].vout_voltage_divider
[0];
317 rtotal
= data
->channel
[channel
].vout_voltage_divider
[1];
320 "Voltage divider output resistance must be greater than 0\n");
325 "Voltage divider total resistance is less than output resistance\n");
332 static int isl68137_probe_from_dt(struct device
*dev
,
333 struct isl68137_data
*data
)
335 const struct device_node
*np
= dev
->of_node
;
336 struct device_node
*child
;
339 for_each_child_of_node(np
, child
) {
340 if (strcmp(child
->name
, "channel"))
343 err
= isl68137_probe_child_from_dt(dev
, child
, data
);
351 static int isl68137_probe(struct i2c_client
*client
)
353 struct device
*dev
= &client
->dev
;
354 struct pmbus_driver_info
*info
;
355 struct isl68137_data
*data
;
358 data
= devm_kzalloc(dev
, sizeof(*data
), GFP_KERNEL
);
363 * Initialize all voltage dividers to Rout=1 and Rtotal=1 to simplify
364 * logic in PMBus word read/write functions
366 for (i
= 0; i
< MAX_CHANNELS
; i
++)
367 memset(data
->channel
[i
].vout_voltage_divider
,
369 sizeof(data
->channel
[i
].vout_voltage_divider
));
371 memcpy(&data
->info
, &raa_dmpvr_info
, sizeof(data
->info
));
374 switch (i2c_match_id(raa_dmpvr_id
, client
)->driver_data
) {
375 case raa_dmpvr1_2rail
:
377 info
->R
[PSC_VOLTAGE_IN
] = 3;
378 info
->func
[0] &= ~PMBUS_HAVE_VMON
;
379 info
->func
[1] = PMBUS_HAVE_VOUT
| PMBUS_HAVE_STATUS_VOUT
380 | PMBUS_HAVE_IOUT
| PMBUS_HAVE_STATUS_IOUT
382 info
->read_word_data
= raa_dmpvr2_read_word_data
;
383 info
->write_word_data
= raa_dmpvr2_write_word_data
;
384 info
->groups
= isl68137_attribute_groups
;
386 case raa_dmpvr2_1rail
:
388 info
->read_word_data
= raa_dmpvr2_read_word_data
;
389 info
->write_word_data
= raa_dmpvr2_write_word_data
;
391 case raa_dmpvr2_2rail_nontc
:
392 info
->func
[0] &= ~PMBUS_HAVE_TEMP3
;
393 info
->func
[1] &= ~PMBUS_HAVE_TEMP3
;
395 case raa_dmpvr2_2rail
:
397 info
->read_word_data
= raa_dmpvr2_read_word_data
;
398 info
->write_word_data
= raa_dmpvr2_write_word_data
;
400 case raa_dmpvr2_3rail
:
401 info
->read_word_data
= raa_dmpvr2_read_word_data
;
402 info
->write_word_data
= raa_dmpvr2_write_word_data
;
406 info
->R
[PSC_VOLTAGE_IN
] = 1;
407 info
->m
[PSC_VOLTAGE_OUT
] = 2;
408 info
->R
[PSC_VOLTAGE_OUT
] = 2;
409 info
->m
[PSC_CURRENT_IN
] = 2;
410 info
->m
[PSC_POWER
] = 2;
411 info
->R
[PSC_POWER
] = -1;
412 info
->read_word_data
= raa_dmpvr2_read_word_data
;
413 info
->write_word_data
= raa_dmpvr2_write_word_data
;
419 err
= isl68137_probe_from_dt(dev
, data
);
423 return pmbus_do_probe(client
, info
);
426 static const struct i2c_device_id raa_dmpvr_id
[] = {
427 {"isl68137", raa_dmpvr1_2rail
},
428 {"isl68220", raa_dmpvr2_2rail
},
429 {"isl68221", raa_dmpvr2_3rail
},
430 {"isl68222", raa_dmpvr2_2rail
},
431 {"isl68223", raa_dmpvr2_2rail
},
432 {"isl68224", raa_dmpvr2_3rail
},
433 {"isl68225", raa_dmpvr2_2rail
},
434 {"isl68226", raa_dmpvr2_3rail
},
435 {"isl68227", raa_dmpvr2_1rail
},
436 {"isl68229", raa_dmpvr2_3rail
},
437 {"isl68233", raa_dmpvr2_2rail
},
438 {"isl68239", raa_dmpvr2_3rail
},
440 {"isl69222", raa_dmpvr2_2rail
},
441 {"isl69223", raa_dmpvr2_3rail
},
442 {"isl69224", raa_dmpvr2_2rail
},
443 {"isl69225", raa_dmpvr2_2rail
},
444 {"isl69227", raa_dmpvr2_3rail
},
445 {"isl69228", raa_dmpvr2_3rail
},
446 {"isl69234", raa_dmpvr2_2rail
},
447 {"isl69236", raa_dmpvr2_2rail
},
448 {"isl69239", raa_dmpvr2_3rail
},
449 {"isl69242", raa_dmpvr2_2rail
},
450 {"isl69243", raa_dmpvr2_1rail
},
451 {"isl69247", raa_dmpvr2_2rail
},
452 {"isl69248", raa_dmpvr2_2rail
},
453 {"isl69254", raa_dmpvr2_2rail
},
454 {"isl69255", raa_dmpvr2_2rail
},
455 {"isl69256", raa_dmpvr2_2rail
},
456 {"isl69259", raa_dmpvr2_2rail
},
457 {"isl69260", raa_dmpvr2_2rail
},
458 {"isl69268", raa_dmpvr2_2rail
},
459 {"isl69269", raa_dmpvr2_3rail
},
460 {"isl69298", raa_dmpvr2_2rail
},
462 {"raa228000", raa_dmpvr2_hv
},
463 {"raa228004", raa_dmpvr2_hv
},
464 {"raa228006", raa_dmpvr2_hv
},
465 {"raa228228", raa_dmpvr2_2rail_nontc
},
466 {"raa229001", raa_dmpvr2_2rail
},
467 {"raa229004", raa_dmpvr2_2rail
},
471 MODULE_DEVICE_TABLE(i2c
, raa_dmpvr_id
);
473 static const struct of_device_id isl68137_of_match
[] = {
474 { .compatible
= "isil,isl68137", .data
= (void *)raa_dmpvr1_2rail
},
475 { .compatible
= "renesas,isl68220", .data
= (void *)raa_dmpvr2_2rail
},
476 { .compatible
= "renesas,isl68221", .data
= (void *)raa_dmpvr2_3rail
},
477 { .compatible
= "renesas,isl68222", .data
= (void *)raa_dmpvr2_2rail
},
478 { .compatible
= "renesas,isl68223", .data
= (void *)raa_dmpvr2_2rail
},
479 { .compatible
= "renesas,isl68224", .data
= (void *)raa_dmpvr2_3rail
},
480 { .compatible
= "renesas,isl68225", .data
= (void *)raa_dmpvr2_2rail
},
481 { .compatible
= "renesas,isl68226", .data
= (void *)raa_dmpvr2_3rail
},
482 { .compatible
= "renesas,isl68227", .data
= (void *)raa_dmpvr2_1rail
},
483 { .compatible
= "renesas,isl68229", .data
= (void *)raa_dmpvr2_3rail
},
484 { .compatible
= "renesas,isl68233", .data
= (void *)raa_dmpvr2_2rail
},
485 { .compatible
= "renesas,isl68239", .data
= (void *)raa_dmpvr2_3rail
},
487 { .compatible
= "renesas,isl69222", .data
= (void *)raa_dmpvr2_2rail
},
488 { .compatible
= "renesas,isl69223", .data
= (void *)raa_dmpvr2_3rail
},
489 { .compatible
= "renesas,isl69224", .data
= (void *)raa_dmpvr2_2rail
},
490 { .compatible
= "renesas,isl69225", .data
= (void *)raa_dmpvr2_2rail
},
491 { .compatible
= "renesas,isl69227", .data
= (void *)raa_dmpvr2_3rail
},
492 { .compatible
= "renesas,isl69228", .data
= (void *)raa_dmpvr2_3rail
},
493 { .compatible
= "renesas,isl69234", .data
= (void *)raa_dmpvr2_2rail
},
494 { .compatible
= "renesas,isl69236", .data
= (void *)raa_dmpvr2_2rail
},
495 { .compatible
= "renesas,isl69239", .data
= (void *)raa_dmpvr2_3rail
},
496 { .compatible
= "renesas,isl69242", .data
= (void *)raa_dmpvr2_2rail
},
497 { .compatible
= "renesas,isl69243", .data
= (void *)raa_dmpvr2_1rail
},
498 { .compatible
= "renesas,isl69247", .data
= (void *)raa_dmpvr2_2rail
},
499 { .compatible
= "renesas,isl69248", .data
= (void *)raa_dmpvr2_2rail
},
500 { .compatible
= "renesas,isl69254", .data
= (void *)raa_dmpvr2_2rail
},
501 { .compatible
= "renesas,isl69255", .data
= (void *)raa_dmpvr2_2rail
},
502 { .compatible
= "renesas,isl69256", .data
= (void *)raa_dmpvr2_2rail
},
503 { .compatible
= "renesas,isl69259", .data
= (void *)raa_dmpvr2_2rail
},
504 { .compatible
= "isil,isl69260", .data
= (void *)raa_dmpvr2_2rail
},
505 { .compatible
= "renesas,isl69268", .data
= (void *)raa_dmpvr2_2rail
},
506 { .compatible
= "isil,isl69269", .data
= (void *)raa_dmpvr2_3rail
},
507 { .compatible
= "renesas,isl69298", .data
= (void *)raa_dmpvr2_2rail
},
509 { .compatible
= "renesas,raa228000", .data
= (void *)raa_dmpvr2_hv
},
510 { .compatible
= "renesas,raa228004", .data
= (void *)raa_dmpvr2_hv
},
511 { .compatible
= "renesas,raa228006", .data
= (void *)raa_dmpvr2_hv
},
512 { .compatible
= "renesas,raa228228", .data
= (void *)raa_dmpvr2_2rail_nontc
},
513 { .compatible
= "renesas,raa229001", .data
= (void *)raa_dmpvr2_2rail
},
514 { .compatible
= "renesas,raa229004", .data
= (void *)raa_dmpvr2_2rail
},
518 MODULE_DEVICE_TABLE(of
, isl68137_of_match
);
520 /* This is the driver that will be inserted */
521 static struct i2c_driver isl68137_driver
= {
524 .of_match_table
= isl68137_of_match
,
526 .probe
= isl68137_probe
,
527 .id_table
= raa_dmpvr_id
,
530 module_i2c_driver(isl68137_driver
);
532 MODULE_AUTHOR("Maxim Sloyko <maxims@google.com>");
533 MODULE_DESCRIPTION("PMBus driver for Renesas digital multiphase voltage regulators");
534 MODULE_LICENSE("GPL");
535 MODULE_IMPORT_NS(PMBUS
);