1 | // SPDX-License-Identifier: GPL-2.0+ |
2 | /* |
3 | * Hardware monitoring driver for Vicor PLI1209BC Digital Supervisor |
4 | * |
5 | * Copyright (c) 2022 9elements GmbH |
6 | */ |
7 | |
8 | #include <linux/delay.h> |
9 | #include <linux/i2c.h> |
10 | #include <linux/module.h> |
11 | #include <linux/pmbus.h> |
12 | #include <linux/regulator/driver.h> |
13 | #include "pmbus.h" |
14 | |
15 | /* |
16 | * The capability command is only supported at page 0. Probing the device while |
17 | * the page register is set to 1 will falsely enable PEC support. Disable |
18 | * capability probing accordingly, since the PLI1209BC does not have any |
19 | * additional capabilities. |
20 | */ |
21 | static struct pmbus_platform_data pli1209bc_plat_data = { |
22 | .flags = PMBUS_NO_CAPABILITY, |
23 | }; |
24 | |
25 | static int pli1209bc_read_word_data(struct i2c_client *client, int page, |
26 | int phase, int reg) |
27 | { |
28 | int data; |
29 | |
30 | switch (reg) { |
31 | /* PMBUS_READ_POUT uses a direct format with R=0 */ |
32 | case PMBUS_READ_POUT: |
33 | data = pmbus_read_word_data(client, page, phase, reg); |
34 | if (data < 0) |
35 | return data; |
36 | data = sign_extend32(value: data, index: 15) * 10; |
37 | return clamp_val(data, -32768, 32767) & 0xffff; |
38 | /* |
39 | * PMBUS_READ_VOUT and PMBUS_READ_TEMPERATURE_1 return invalid data |
40 | * when the BCM is turned off. Since it is not possible to return |
41 | * ENODATA error, return zero instead. |
42 | */ |
43 | case PMBUS_READ_VOUT: |
44 | case PMBUS_READ_TEMPERATURE_1: |
45 | data = pmbus_read_word_data(client, page, phase, |
46 | reg: PMBUS_STATUS_WORD); |
47 | if (data < 0) |
48 | return data; |
49 | if (data & PB_STATUS_POWER_GOOD_N) |
50 | return 0; |
51 | return pmbus_read_word_data(client, page, phase, reg); |
52 | default: |
53 | return -ENODATA; |
54 | } |
55 | } |
56 | |
57 | static int pli1209bc_write_byte(struct i2c_client *client, int page, u8 reg) |
58 | { |
59 | int ret; |
60 | |
61 | switch (reg) { |
62 | case PMBUS_CLEAR_FAULTS: |
63 | ret = pmbus_write_byte(client, page, value: reg); |
64 | /* |
65 | * PLI1209 takes 230 usec to execute the CLEAR_FAULTS command. |
66 | * During that time it's busy and NACKs all requests on the |
67 | * SMBUS interface. It also NACKs reads on PMBUS_STATUS_BYTE |
68 | * making it impossible to poll the BUSY flag. |
69 | * |
70 | * Just wait for not BUSY unconditionally. |
71 | */ |
72 | usleep_range(min: 250, max: 300); |
73 | break; |
74 | default: |
75 | ret = -ENODATA; |
76 | break; |
77 | } |
78 | return ret; |
79 | } |
80 | |
81 | #if IS_ENABLED(CONFIG_SENSORS_PLI1209BC_REGULATOR) |
82 | static const struct regulator_desc pli1209bc_reg_desc = { |
83 | .name = "vout2" , |
84 | .id = 1, |
85 | .of_match = of_match_ptr("vout2" ), |
86 | .regulators_node = of_match_ptr("regulators" ), |
87 | .ops = &pmbus_regulator_ops, |
88 | .type = REGULATOR_VOLTAGE, |
89 | .owner = THIS_MODULE, |
90 | }; |
91 | #endif |
92 | |
93 | static struct pmbus_driver_info pli1209bc_info = { |
94 | .pages = 2, |
95 | .format[PSC_VOLTAGE_IN] = direct, |
96 | .format[PSC_VOLTAGE_OUT] = direct, |
97 | .format[PSC_CURRENT_IN] = direct, |
98 | .format[PSC_CURRENT_OUT] = direct, |
99 | .format[PSC_POWER] = direct, |
100 | .format[PSC_TEMPERATURE] = direct, |
101 | .m[PSC_VOLTAGE_IN] = 1, |
102 | .b[PSC_VOLTAGE_IN] = 0, |
103 | .R[PSC_VOLTAGE_IN] = 1, |
104 | .m[PSC_VOLTAGE_OUT] = 1, |
105 | .b[PSC_VOLTAGE_OUT] = 0, |
106 | .R[PSC_VOLTAGE_OUT] = 1, |
107 | .m[PSC_CURRENT_IN] = 1, |
108 | .b[PSC_CURRENT_IN] = 0, |
109 | .R[PSC_CURRENT_IN] = 3, |
110 | .m[PSC_CURRENT_OUT] = 1, |
111 | .b[PSC_CURRENT_OUT] = 0, |
112 | .R[PSC_CURRENT_OUT] = 2, |
113 | .m[PSC_POWER] = 1, |
114 | .b[PSC_POWER] = 0, |
115 | .R[PSC_POWER] = 1, |
116 | .m[PSC_TEMPERATURE] = 1, |
117 | .b[PSC_TEMPERATURE] = 0, |
118 | .R[PSC_TEMPERATURE] = 0, |
119 | /* |
120 | * Page 0 sums up all attributes except voltage readings. |
121 | * The pli1209 digital supervisor only contains a single BCM, making |
122 | * page 0 redundant. |
123 | */ |
124 | .func[1] = PMBUS_HAVE_VIN | PMBUS_HAVE_VOUT |
125 | | PMBUS_HAVE_IIN | PMBUS_HAVE_IOUT |
126 | | PMBUS_HAVE_PIN | PMBUS_HAVE_POUT |
127 | | PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP |
128 | | PMBUS_HAVE_STATUS_IOUT | PMBUS_HAVE_STATUS_INPUT, |
129 | .read_word_data = pli1209bc_read_word_data, |
130 | .write_byte = pli1209bc_write_byte, |
131 | #if IS_ENABLED(CONFIG_SENSORS_PLI1209BC_REGULATOR) |
132 | .num_regulators = 1, |
133 | .reg_desc = &pli1209bc_reg_desc, |
134 | #endif |
135 | }; |
136 | |
137 | static int pli1209bc_probe(struct i2c_client *client) |
138 | { |
139 | client->dev.platform_data = &pli1209bc_plat_data; |
140 | return pmbus_do_probe(client, info: &pli1209bc_info); |
141 | } |
142 | |
143 | static const struct i2c_device_id pli1209bc_id[] = { |
144 | {"pli1209bc" , 0}, |
145 | {} |
146 | }; |
147 | |
148 | MODULE_DEVICE_TABLE(i2c, pli1209bc_id); |
149 | |
150 | #ifdef CONFIG_OF |
151 | static const struct of_device_id pli1209bc_of_match[] = { |
152 | { .compatible = "vicor,pli1209bc" }, |
153 | { }, |
154 | }; |
155 | MODULE_DEVICE_TABLE(of, pli1209bc_of_match); |
156 | #endif |
157 | |
158 | static struct i2c_driver pli1209bc_driver = { |
159 | .driver = { |
160 | .name = "pli1209bc" , |
161 | .of_match_table = of_match_ptr(pli1209bc_of_match), |
162 | }, |
163 | .probe = pli1209bc_probe, |
164 | .id_table = pli1209bc_id, |
165 | }; |
166 | |
167 | module_i2c_driver(pli1209bc_driver); |
168 | |
169 | MODULE_AUTHOR("Marcello Sylvester Bauer <sylv@sylv.io>" ); |
170 | MODULE_DESCRIPTION("PMBus driver for Vicor PLI1209BC" ); |
171 | MODULE_LICENSE("GPL" ); |
172 | MODULE_IMPORT_NS(PMBUS); |
173 | |