1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Hardware monitoring driver for Texas Instruments TPS53679
4 *
5 * Copyright (c) 2017 Mellanox Technologies. All rights reserved.
6 * Copyright (c) 2017 Vadim Pasternak <vadimp@mellanox.com>
7 */
8
9 #include <linux/bits.h>
10 #include <linux/err.h>
11 #include <linux/i2c.h>
12 #include <linux/init.h>
13 #include <linux/kernel.h>
14 #include <linux/module.h>
15 #include <linux/of.h>
16 #include "pmbus.h"
17
18 enum chips {
19 tps53647, tps53667, tps53676, tps53679, tps53681, tps53685, tps53688
20 };
21
22 #define TPS53647_PAGE_NUM 1
23
24 #define TPS53676_USER_DATA_03 0xb3
25 #define TPS53676_MAX_PHASES 7
26
27 #define TPS53679_PROT_VR12_5MV 0x01 /* VR12.0 mode, 5-mV DAC */
28 #define TPS53679_PROT_VR12_5_10MV 0x02 /* VR12.5 mode, 10-mV DAC */
29 #define TPS53679_PROT_VR13_10MV 0x04 /* VR13.0 mode, 10-mV DAC */
30 #define TPS53679_PROT_IMVP8_5MV 0x05 /* IMVP8 mode, 5-mV DAC */
31 #define TPS53679_PROT_VR13_5MV 0x07 /* VR13.0 mode, 5-mV DAC */
32 #define TPS53679_PAGE_NUM 2
33
34 #define TPS53681_DEVICE_ID "\x81"
35 #define TPS53685_DEVICE_ID "TIShP"
36
37 #define TPS53681_PMBUS_REVISION 0x33
38
39 #define TPS53681_MFR_SPECIFIC_20 0xe4 /* Number of phases, per page */
40
41 static const struct i2c_device_id tps53679_id[];
42
tps53679_identify_mode(struct i2c_client * client,struct pmbus_driver_info * info)43 static int tps53679_identify_mode(struct i2c_client *client,
44 struct pmbus_driver_info *info)
45 {
46 u8 vout_params;
47 int i, ret;
48
49 for (i = 0; i < info->pages; i++) {
50 /* Read the register with VOUT scaling value.*/
51 ret = pmbus_read_byte_data(client, i, PMBUS_VOUT_MODE);
52 if (ret < 0)
53 return ret;
54
55 vout_params = ret & GENMASK(4, 0);
56
57 switch (vout_params) {
58 case TPS53679_PROT_VR13_10MV:
59 case TPS53679_PROT_VR12_5_10MV:
60 info->vrm_version[i] = vr13;
61 break;
62 case TPS53679_PROT_VR13_5MV:
63 case TPS53679_PROT_VR12_5MV:
64 case TPS53679_PROT_IMVP8_5MV:
65 info->vrm_version[i] = vr12;
66 break;
67 default:
68 return -EINVAL;
69 }
70 }
71
72 return 0;
73 }
74
tps53679_identify_phases(struct i2c_client * client,struct pmbus_driver_info * info)75 static int tps53679_identify_phases(struct i2c_client *client,
76 struct pmbus_driver_info *info)
77 {
78 int ret;
79
80 /* On TPS53681, only channel A provides per-phase output current */
81 ret = pmbus_read_byte_data(client, 0, TPS53681_MFR_SPECIFIC_20);
82 if (ret < 0)
83 return ret;
84 info->phases[0] = (ret & 0x07) + 1;
85
86 return 0;
87 }
88
tps53679_identify_chip(struct i2c_client * client,u8 revision,char * id)89 static int tps53679_identify_chip(struct i2c_client *client,
90 u8 revision, char *id)
91 {
92 u8 buf[I2C_SMBUS_BLOCK_MAX];
93 int ret;
94 int buf_len;
95 int id_len;
96
97 ret = pmbus_read_byte_data(client, 0, PMBUS_REVISION);
98 if (ret < 0)
99 return ret;
100 if (ret != revision) {
101 dev_err(&client->dev, "Unexpected PMBus revision 0x%x\n", ret);
102 return -ENODEV;
103 }
104
105 ret = i2c_smbus_read_block_data(client, PMBUS_IC_DEVICE_ID, buf);
106 if (ret < 0)
107 return ret;
108
109 /* Adjust length if null terminator if present */
110 buf_len = (buf[ret - 1] != '\x00' ? ret : ret - 1);
111
112 id_len = strlen(id);
113
114 if (buf_len != id_len || strncmp(id, buf, id_len)) {
115 dev_err(&client->dev, "Unexpected device ID: %*ph\n", ret, buf);
116 return -ENODEV;
117 }
118 return 0;
119 }
120
121 /*
122 * Common identification function for chips with multi-phase support.
123 * Since those chips have special configuration registers, we want to have
124 * some level of reassurance that we are really talking with the chip
125 * being probed. Check PMBus revision and chip ID.
126 */
tps53679_identify_multiphase(struct i2c_client * client,struct pmbus_driver_info * info,int pmbus_rev,char * device_id)127 static int tps53679_identify_multiphase(struct i2c_client *client,
128 struct pmbus_driver_info *info,
129 int pmbus_rev, char *device_id)
130 {
131 int ret;
132
133 ret = tps53679_identify_chip(client, pmbus_rev, device_id);
134 if (ret < 0)
135 return ret;
136
137 ret = tps53679_identify_mode(client, info);
138 if (ret < 0)
139 return ret;
140
141 return tps53679_identify_phases(client, info);
142 }
143
tps53679_identify(struct i2c_client * client,struct pmbus_driver_info * info)144 static int tps53679_identify(struct i2c_client *client,
145 struct pmbus_driver_info *info)
146 {
147 return tps53679_identify_mode(client, info);
148 }
149
tps53685_identify(struct i2c_client * client,struct pmbus_driver_info * info)150 static int tps53685_identify(struct i2c_client *client,
151 struct pmbus_driver_info *info)
152 {
153 info->func[1] |= PMBUS_HAVE_VIN | PMBUS_HAVE_IIN | PMBUS_HAVE_PIN |
154 PMBUS_HAVE_STATUS_INPUT;
155 info->format[PSC_VOLTAGE_OUT] = linear;
156 return tps53679_identify_chip(client, TPS53681_PMBUS_REVISION,
157 TPS53685_DEVICE_ID);
158 }
159
tps53681_identify(struct i2c_client * client,struct pmbus_driver_info * info)160 static int tps53681_identify(struct i2c_client *client,
161 struct pmbus_driver_info *info)
162 {
163 return tps53679_identify_multiphase(client, info,
164 TPS53681_PMBUS_REVISION,
165 TPS53681_DEVICE_ID);
166 }
167
tps53676_identify(struct i2c_client * client,struct pmbus_driver_info * info)168 static int tps53676_identify(struct i2c_client *client,
169 struct pmbus_driver_info *info)
170 {
171 u8 buf[I2C_SMBUS_BLOCK_MAX];
172 int phases_a = 0, phases_b = 0;
173 int i, ret;
174
175 ret = i2c_smbus_read_block_data(client, PMBUS_IC_DEVICE_ID, buf);
176 if (ret < 0)
177 return ret;
178 if (strncmp("TI\x53\x67\x60", buf, 5)) {
179 dev_err(&client->dev, "Unexpected device ID: %s\n", buf);
180 return -ENODEV;
181 }
182
183 ret = i2c_smbus_read_block_data(client, TPS53676_USER_DATA_03, buf);
184 if (ret < 0)
185 return ret;
186 if (ret != 24)
187 return -EIO;
188 for (i = 0; i < 2 * TPS53676_MAX_PHASES; i += 2) {
189 if (buf[i + 1] & 0x80) {
190 if (buf[i] & 0x08)
191 phases_b++;
192 else
193 phases_a++;
194 }
195 }
196
197 info->format[PSC_VOLTAGE_OUT] = linear;
198 info->pages = 1;
199 info->phases[0] = phases_a;
200 if (phases_b > 0) {
201 info->pages = 2;
202 info->phases[1] = phases_b;
203 }
204 return 0;
205 }
206
tps53681_read_word_data(struct i2c_client * client,int page,int phase,int reg)207 static int tps53681_read_word_data(struct i2c_client *client, int page,
208 int phase, int reg)
209 {
210 /*
211 * For reading the total output current (READ_IOUT) for all phases,
212 * the chip datasheet is a bit vague. It says "PHASE must be set to
213 * FFh to access all phases simultaneously. PHASE may also be set to
214 * 80h readack (!) the total phase current".
215 * Experiments show that the command does _not_ report the total
216 * current for all phases if the phase is set to 0xff. Instead, it
217 * appears to report the current of one of the phases. Override phase
218 * parameter with 0x80 when reading the total output current on page 0.
219 */
220 if (reg == PMBUS_READ_IOUT && page == 0 && phase == 0xff)
221 return pmbus_read_word_data(client, page, 0x80, reg);
222 return -ENODATA;
223 }
224
225 static struct pmbus_driver_info tps53679_info = {
226 .format[PSC_VOLTAGE_IN] = linear,
227 .format[PSC_VOLTAGE_OUT] = vid,
228 .format[PSC_TEMPERATURE] = linear,
229 .format[PSC_CURRENT_OUT] = linear,
230 .format[PSC_POWER] = linear,
231 .func[0] = PMBUS_HAVE_VIN | PMBUS_HAVE_IIN | PMBUS_HAVE_PIN |
232 PMBUS_HAVE_STATUS_INPUT |
233 PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT |
234 PMBUS_HAVE_IOUT | PMBUS_HAVE_STATUS_IOUT |
235 PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP |
236 PMBUS_HAVE_POUT,
237 .func[1] = PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT |
238 PMBUS_HAVE_IOUT | PMBUS_HAVE_STATUS_IOUT |
239 PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP |
240 PMBUS_HAVE_POUT,
241 .pfunc[0] = PMBUS_HAVE_IOUT,
242 .pfunc[1] = PMBUS_HAVE_IOUT,
243 .pfunc[2] = PMBUS_HAVE_IOUT,
244 .pfunc[3] = PMBUS_HAVE_IOUT,
245 .pfunc[4] = PMBUS_HAVE_IOUT,
246 .pfunc[5] = PMBUS_HAVE_IOUT,
247 .pfunc[6] = PMBUS_HAVE_IOUT,
248 };
249
tps53679_probe(struct i2c_client * client)250 static int tps53679_probe(struct i2c_client *client)
251 {
252 struct device *dev = &client->dev;
253 struct pmbus_driver_info *info;
254 enum chips chip_id;
255
256 if (dev->of_node)
257 chip_id = (uintptr_t)of_device_get_match_data(dev);
258 else
259 chip_id = i2c_match_id(tps53679_id, client)->driver_data;
260
261 info = devm_kmemdup(dev, &tps53679_info, sizeof(*info), GFP_KERNEL);
262 if (!info)
263 return -ENOMEM;
264
265 switch (chip_id) {
266 case tps53647:
267 case tps53667:
268 info->pages = TPS53647_PAGE_NUM;
269 info->identify = tps53679_identify;
270 break;
271 case tps53676:
272 info->identify = tps53676_identify;
273 break;
274 case tps53679:
275 case tps53688:
276 info->pages = TPS53679_PAGE_NUM;
277 info->identify = tps53679_identify;
278 break;
279 case tps53681:
280 info->pages = TPS53679_PAGE_NUM;
281 info->phases[0] = 6;
282 info->identify = tps53681_identify;
283 info->read_word_data = tps53681_read_word_data;
284 break;
285 case tps53685:
286 info->pages = TPS53679_PAGE_NUM;
287 info->identify = tps53685_identify;
288 break;
289 default:
290 return -ENODEV;
291 }
292
293 return pmbus_do_probe(client, info);
294 }
295
296 static const struct i2c_device_id tps53679_id[] = {
297 {"bmr474", tps53676},
298 {"tps53647", tps53647},
299 {"tps53667", tps53667},
300 {"tps53676", tps53676},
301 {"tps53679", tps53679},
302 {"tps53681", tps53681},
303 {"tps53685", tps53685},
304 {"tps53688", tps53688},
305 {}
306 };
307
308 MODULE_DEVICE_TABLE(i2c, tps53679_id);
309
310 static const struct of_device_id __maybe_unused tps53679_of_match[] = {
311 {.compatible = "ti,tps53647", .data = (void *)tps53647},
312 {.compatible = "ti,tps53667", .data = (void *)tps53667},
313 {.compatible = "ti,tps53676", .data = (void *)tps53676},
314 {.compatible = "ti,tps53679", .data = (void *)tps53679},
315 {.compatible = "ti,tps53681", .data = (void *)tps53681},
316 {.compatible = "ti,tps53685", .data = (void *)tps53685},
317 {.compatible = "ti,tps53688", .data = (void *)tps53688},
318 {}
319 };
320 MODULE_DEVICE_TABLE(of, tps53679_of_match);
321
322 static struct i2c_driver tps53679_driver = {
323 .driver = {
324 .name = "tps53679",
325 .of_match_table = of_match_ptr(tps53679_of_match),
326 },
327 .probe = tps53679_probe,
328 .id_table = tps53679_id,
329 };
330
331 module_i2c_driver(tps53679_driver);
332
333 MODULE_AUTHOR("Vadim Pasternak <vadimp@mellanox.com>");
334 MODULE_DESCRIPTION("PMBus driver for Texas Instruments TPS53679");
335 MODULE_LICENSE("GPL");
336 MODULE_IMPORT_NS("PMBUS");
337