xref: /linux/drivers/hwmon/pmbus/tps53679.c (revision d24e3bf4c5484b29432837269ded253480fa8928)
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 < 0 ? ret : -EIO;
108 
109 	/* Adjust length if null terminator is 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 (ret != 6 || memcmp(buf, "TI\x53\x67\x60\x00", 6)) {
179 		dev_err(&client->dev, "Unexpected device ID: %*ph\n", ret, 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] & BIT(4))
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 	} else {
204 		/*
205 		 * pmbus_set_page() does not update the PAGE register on
206 		 * single-page devices, so select page 0 explicitly in case
207 		 * the boot firmware left the device on another page.
208 		 */
209 		ret = i2c_smbus_write_byte_data(client, PMBUS_PAGE, 0);
210 		if (ret < 0)
211 			return ret;
212 	}
213 	return 0;
214 }
215 
tps53681_read_word_data(struct i2c_client * client,int page,int phase,int reg)216 static int tps53681_read_word_data(struct i2c_client *client, int page,
217 				   int phase, int reg)
218 {
219 	/*
220 	 * For reading the total output current (READ_IOUT) for all phases,
221 	 * the chip datasheet is a bit vague. It says "PHASE must be set to
222 	 * FFh to access all phases simultaneously. PHASE may also be set to
223 	 * 80h readack (!) the total phase current".
224 	 * Experiments show that the command does _not_ report the total
225 	 * current for all phases if the phase is set to 0xff. Instead, it
226 	 * appears to report the current of one of the phases. Override phase
227 	 * parameter with 0x80 when reading the total output current on page 0.
228 	 */
229 	if (reg == PMBUS_READ_IOUT && page == 0 && phase == 0xff)
230 		return pmbus_read_word_data(client, page, 0x80, reg);
231 	return -ENODATA;
232 }
233 
234 static struct pmbus_driver_info tps53679_info = {
235 	.format[PSC_VOLTAGE_IN] = linear,
236 	.format[PSC_VOLTAGE_OUT] = vid,
237 	.format[PSC_TEMPERATURE] = linear,
238 	.format[PSC_CURRENT_OUT] = linear,
239 	.format[PSC_POWER] = linear,
240 	.func[0] = PMBUS_HAVE_VIN | PMBUS_HAVE_IIN | PMBUS_HAVE_PIN |
241 		PMBUS_HAVE_STATUS_INPUT |
242 		PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT |
243 		PMBUS_HAVE_IOUT | PMBUS_HAVE_STATUS_IOUT |
244 		PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP |
245 		PMBUS_HAVE_POUT,
246 	.func[1] = PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT |
247 		PMBUS_HAVE_IOUT | PMBUS_HAVE_STATUS_IOUT |
248 		PMBUS_HAVE_TEMP | PMBUS_HAVE_STATUS_TEMP |
249 		PMBUS_HAVE_POUT,
250 	.pfunc[0] = PMBUS_HAVE_IOUT,
251 	.pfunc[1] = PMBUS_HAVE_IOUT,
252 	.pfunc[2] = PMBUS_HAVE_IOUT,
253 	.pfunc[3] = PMBUS_HAVE_IOUT,
254 	.pfunc[4] = PMBUS_HAVE_IOUT,
255 	.pfunc[5] = PMBUS_HAVE_IOUT,
256 	.pfunc[6] = PMBUS_HAVE_IOUT,
257 };
258 
tps53679_probe(struct i2c_client * client)259 static int tps53679_probe(struct i2c_client *client)
260 {
261 	struct device *dev = &client->dev;
262 	struct pmbus_driver_info *info;
263 	enum chips chip_id;
264 
265 	chip_id = (uintptr_t)i2c_get_match_data(client);
266 
267 	info = devm_kmemdup(dev, &tps53679_info, sizeof(*info), GFP_KERNEL);
268 	if (!info)
269 		return -ENOMEM;
270 
271 	switch (chip_id) {
272 	case tps53647:
273 	case tps53667:
274 		info->pages = TPS53647_PAGE_NUM;
275 		info->identify = tps53679_identify;
276 		break;
277 	case tps53676:
278 		info->identify = tps53676_identify;
279 		break;
280 	case tps53679:
281 	case tps53688:
282 		info->pages = TPS53679_PAGE_NUM;
283 		info->identify = tps53679_identify;
284 		break;
285 	case tps53681:
286 		info->pages = TPS53679_PAGE_NUM;
287 		info->phases[0] = 6;
288 		info->identify = tps53681_identify;
289 		info->read_word_data = tps53681_read_word_data;
290 		break;
291 	case tps53685:
292 		info->pages = TPS53679_PAGE_NUM;
293 		info->identify = tps53685_identify;
294 		break;
295 	default:
296 		return -ENODEV;
297 	}
298 
299 	return pmbus_do_probe(client, info);
300 }
301 
302 static const struct i2c_device_id tps53679_id[] = {
303 	{ .name = "bmr474", .driver_data = tps53676 },
304 	{ .name = "tps53647", .driver_data = tps53647 },
305 	{ .name = "tps53667", .driver_data = tps53667 },
306 	{ .name = "tps53676", .driver_data = tps53676 },
307 	{ .name = "tps53679", .driver_data = tps53679 },
308 	{ .name = "tps53681", .driver_data = tps53681 },
309 	{ .name = "tps53685", .driver_data = tps53685 },
310 	{ .name = "tps53688", .driver_data = tps53688 },
311 	{ }
312 };
313 
314 MODULE_DEVICE_TABLE(i2c, tps53679_id);
315 
316 static const struct of_device_id __maybe_unused tps53679_of_match[] = {
317 	{.compatible = "ti,tps53647", .data = (void *)tps53647},
318 	{.compatible = "ti,tps53667", .data = (void *)tps53667},
319 	{.compatible = "ti,tps53676", .data = (void *)tps53676},
320 	{.compatible = "ti,tps53679", .data = (void *)tps53679},
321 	{.compatible = "ti,tps53681", .data = (void *)tps53681},
322 	{.compatible = "ti,tps53685", .data = (void *)tps53685},
323 	{.compatible = "ti,tps53688", .data = (void *)tps53688},
324 	{}
325 };
326 MODULE_DEVICE_TABLE(of, tps53679_of_match);
327 
328 static struct i2c_driver tps53679_driver = {
329 	.driver = {
330 		.name = "tps53679",
331 		.of_match_table = of_match_ptr(tps53679_of_match),
332 	},
333 	.probe = tps53679_probe,
334 	.id_table = tps53679_id,
335 };
336 
337 module_i2c_driver(tps53679_driver);
338 
339 MODULE_AUTHOR("Vadim Pasternak <vadimp@mellanox.com>");
340 MODULE_DESCRIPTION("PMBus driver for Texas Instruments TPS53679");
341 MODULE_LICENSE("GPL");
342 MODULE_IMPORT_NS("PMBUS");
343