xref: /openbmc/linux/drivers/hwmon/pmbus/adm1266.c (revision 724ba675)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * ADM1266 - Cascadable Super Sequencer with Margin
4  * Control and Fault Recording
5  *
6  * Copyright 2020 Analog Devices Inc.
7  */
8 
9 #include <linux/bitfield.h>
10 #include <linux/crc8.h>
11 #include <linux/debugfs.h>
12 #include <linux/gpio/driver.h>
13 #include <linux/i2c.h>
14 #include <linux/i2c-smbus.h>
15 #include <linux/init.h>
16 #include <linux/kernel.h>
17 #include <linux/module.h>
18 #include <linux/nvmem-consumer.h>
19 #include <linux/nvmem-provider.h>
20 #include "pmbus.h"
21 #include <linux/slab.h>
22 #include <linux/timekeeping.h>
23 
24 #define ADM1266_BLACKBOX_CONFIG	0xD3
25 #define ADM1266_PDIO_CONFIG	0xD4
26 #define ADM1266_READ_STATE	0xD9
27 #define ADM1266_READ_BLACKBOX	0xDE
28 #define ADM1266_SET_RTC		0xDF
29 #define ADM1266_GPIO_CONFIG	0xE1
30 #define ADM1266_BLACKBOX_INFO	0xE6
31 #define ADM1266_PDIO_STATUS	0xE9
32 #define ADM1266_GPIO_STATUS	0xEA
33 
34 /* ADM1266 GPIO defines */
35 #define ADM1266_GPIO_NR			9
36 #define ADM1266_GPIO_FUNCTIONS(x)	FIELD_GET(BIT(0), x)
37 #define ADM1266_GPIO_INPUT_EN(x)	FIELD_GET(BIT(2), x)
38 #define ADM1266_GPIO_OUTPUT_EN(x)	FIELD_GET(BIT(3), x)
39 #define ADM1266_GPIO_OPEN_DRAIN(x)	FIELD_GET(BIT(4), x)
40 
41 /* ADM1266 PDIO defines */
42 #define ADM1266_PDIO_NR			16
43 #define ADM1266_PDIO_PIN_CFG(x)		FIELD_GET(GENMASK(15, 13), x)
44 #define ADM1266_PDIO_GLITCH_FILT(x)	FIELD_GET(GENMASK(12, 9), x)
45 #define ADM1266_PDIO_OUT_CFG(x)		FIELD_GET(GENMASK(2, 0), x)
46 
47 #define ADM1266_BLACKBOX_OFFSET		0
48 #define ADM1266_BLACKBOX_SIZE		64
49 
50 #define ADM1266_PMBUS_BLOCK_MAX		255
51 
52 struct adm1266_data {
53 	struct pmbus_driver_info info;
54 	struct gpio_chip gc;
55 	const char *gpio_names[ADM1266_GPIO_NR + ADM1266_PDIO_NR];
56 	struct i2c_client *client;
57 	struct dentry *debugfs_dir;
58 	struct nvmem_config nvmem_config;
59 	struct nvmem_device *nvmem;
60 	u8 *dev_mem;
61 	struct mutex buf_mutex;
62 	u8 write_buf[ADM1266_PMBUS_BLOCK_MAX + 1] ____cacheline_aligned;
63 	u8 read_buf[ADM1266_PMBUS_BLOCK_MAX + 1] ____cacheline_aligned;
64 };
65 
66 static const struct nvmem_cell_info adm1266_nvmem_cells[] = {
67 	{
68 		.name           = "blackbox",
69 		.offset         = ADM1266_BLACKBOX_OFFSET,
70 		.bytes          = 2048,
71 	},
72 };
73 
74 DECLARE_CRC8_TABLE(pmbus_crc_table);
75 
76 /*
77  * Different from Block Read as it sends data and waits for the slave to
78  * return a value dependent on that data. The protocol is simply a Write Block
79  * followed by a Read Block without the Read-Block command field and the
80  * Write-Block STOP bit.
81  */
82 static int adm1266_pmbus_block_xfer(struct adm1266_data *data, u8 cmd, u8 w_len, u8 *data_w,
83 				    u8 *data_r)
84 {
85 	struct i2c_client *client = data->client;
86 	struct i2c_msg msgs[2] = {
87 		{
88 			.addr = client->addr,
89 			.flags = I2C_M_DMA_SAFE,
90 			.buf = data->write_buf,
91 			.len = w_len + 2,
92 		},
93 		{
94 			.addr = client->addr,
95 			.flags = I2C_M_RD | I2C_M_DMA_SAFE,
96 			.buf = data->read_buf,
97 			.len = ADM1266_PMBUS_BLOCK_MAX + 2,
98 		}
99 	};
100 	u8 addr;
101 	u8 crc;
102 	int ret;
103 
104 	mutex_lock(&data->buf_mutex);
105 
106 	msgs[0].buf[0] = cmd;
107 	msgs[0].buf[1] = w_len;
108 	memcpy(&msgs[0].buf[2], data_w, w_len);
109 
110 	ret = i2c_transfer(client->adapter, msgs, 2);
111 	if (ret != 2) {
112 		if (ret >= 0)
113 			ret = -EPROTO;
114 
115 		mutex_unlock(&data->buf_mutex);
116 
117 		return ret;
118 	}
119 
120 	if (client->flags & I2C_CLIENT_PEC) {
121 		addr = i2c_8bit_addr_from_msg(&msgs[0]);
122 		crc = crc8(pmbus_crc_table, &addr, 1, 0);
123 		crc = crc8(pmbus_crc_table, msgs[0].buf,  msgs[0].len, crc);
124 
125 		addr = i2c_8bit_addr_from_msg(&msgs[1]);
126 		crc = crc8(pmbus_crc_table, &addr, 1, crc);
127 		crc = crc8(pmbus_crc_table, msgs[1].buf,  msgs[1].buf[0] + 1, crc);
128 
129 		if (crc != msgs[1].buf[msgs[1].buf[0] + 1]) {
130 			mutex_unlock(&data->buf_mutex);
131 			return -EBADMSG;
132 		}
133 	}
134 
135 	memcpy(data_r, &msgs[1].buf[1], msgs[1].buf[0]);
136 
137 	ret = msgs[1].buf[0];
138 	mutex_unlock(&data->buf_mutex);
139 
140 	return ret;
141 }
142 
143 static const unsigned int adm1266_gpio_mapping[ADM1266_GPIO_NR][2] = {
144 	{1, 0},
145 	{2, 1},
146 	{3, 2},
147 	{4, 8},
148 	{5, 9},
149 	{6, 10},
150 	{7, 11},
151 	{8, 6},
152 	{9, 7},
153 };
154 
155 static const char *adm1266_names[ADM1266_GPIO_NR + ADM1266_PDIO_NR] = {
156 	"GPIO1", "GPIO2", "GPIO3", "GPIO4", "GPIO5", "GPIO6", "GPIO7", "GPIO8",
157 	"GPIO9", "PDIO1", "PDIO2", "PDIO3", "PDIO4", "PDIO5", "PDIO6",
158 	"PDIO7", "PDIO8", "PDIO9", "PDIO10", "PDIO11", "PDIO12", "PDIO13",
159 	"PDIO14", "PDIO15", "PDIO16",
160 };
161 
162 static int adm1266_gpio_get(struct gpio_chip *chip, unsigned int offset)
163 {
164 	struct adm1266_data *data = gpiochip_get_data(chip);
165 	u8 read_buf[I2C_SMBUS_BLOCK_MAX + 1];
166 	unsigned long pins_status;
167 	unsigned int pmbus_cmd;
168 	int ret;
169 
170 	if (offset < ADM1266_GPIO_NR)
171 		pmbus_cmd = ADM1266_GPIO_STATUS;
172 	else
173 		pmbus_cmd = ADM1266_PDIO_STATUS;
174 
175 	ret = i2c_smbus_read_block_data(data->client, pmbus_cmd, read_buf);
176 	if (ret < 0)
177 		return ret;
178 
179 	pins_status = read_buf[0] + (read_buf[1] << 8);
180 	if (offset < ADM1266_GPIO_NR)
181 		return test_bit(adm1266_gpio_mapping[offset][1], &pins_status);
182 
183 	return test_bit(offset - ADM1266_GPIO_NR, &pins_status);
184 }
185 
186 static int adm1266_gpio_get_multiple(struct gpio_chip *chip, unsigned long *mask,
187 				     unsigned long *bits)
188 {
189 	struct adm1266_data *data = gpiochip_get_data(chip);
190 	u8 read_buf[ADM1266_PMBUS_BLOCK_MAX + 1];
191 	unsigned long status;
192 	unsigned int gpio_nr;
193 	int ret;
194 
195 	ret = i2c_smbus_read_block_data(data->client, ADM1266_GPIO_STATUS, read_buf);
196 	if (ret < 0)
197 		return ret;
198 
199 	status = read_buf[0] + (read_buf[1] << 8);
200 
201 	*bits = 0;
202 	for_each_set_bit(gpio_nr, mask, ADM1266_GPIO_NR) {
203 		if (test_bit(adm1266_gpio_mapping[gpio_nr][1], &status))
204 			set_bit(gpio_nr, bits);
205 	}
206 
207 	ret = i2c_smbus_read_block_data(data->client, ADM1266_PDIO_STATUS, read_buf);
208 	if (ret < 0)
209 		return ret;
210 
211 	status = read_buf[0] + (read_buf[1] << 8);
212 
213 	*bits = 0;
214 	for_each_set_bit_from(gpio_nr, mask, ADM1266_GPIO_NR + ADM1266_PDIO_STATUS) {
215 		if (test_bit(gpio_nr - ADM1266_GPIO_NR, &status))
216 			set_bit(gpio_nr, bits);
217 	}
218 
219 	return 0;
220 }
221 
222 static void adm1266_gpio_dbg_show(struct seq_file *s, struct gpio_chip *chip)
223 {
224 	struct adm1266_data *data = gpiochip_get_data(chip);
225 	u8 read_buf[ADM1266_PMBUS_BLOCK_MAX + 1];
226 	unsigned long gpio_config;
227 	unsigned long pdio_config;
228 	unsigned long pin_cfg;
229 	u8 write_cmd;
230 	int ret;
231 	int i;
232 
233 	for (i = 0; i < ADM1266_GPIO_NR; i++) {
234 		write_cmd = adm1266_gpio_mapping[i][1];
235 		ret = adm1266_pmbus_block_xfer(data, ADM1266_GPIO_CONFIG, 1, &write_cmd, read_buf);
236 		if (ret != 2)
237 			return;
238 
239 		gpio_config = read_buf[0];
240 		seq_puts(s, adm1266_names[i]);
241 
242 		seq_puts(s, " ( ");
243 		if (!ADM1266_GPIO_FUNCTIONS(gpio_config)) {
244 			seq_puts(s, "high-Z )\n");
245 			continue;
246 		}
247 		if (ADM1266_GPIO_INPUT_EN(gpio_config))
248 			seq_puts(s, "input ");
249 		if (ADM1266_GPIO_OUTPUT_EN(gpio_config))
250 			seq_puts(s, "output ");
251 		if (ADM1266_GPIO_OPEN_DRAIN(gpio_config))
252 			seq_puts(s, "open-drain )\n");
253 		else
254 			seq_puts(s, "push-pull )\n");
255 	}
256 
257 	write_cmd = 0xFF;
258 	ret = adm1266_pmbus_block_xfer(data, ADM1266_PDIO_CONFIG, 1, &write_cmd, read_buf);
259 	if (ret != 32)
260 		return;
261 
262 	for (i = 0; i < ADM1266_PDIO_NR; i++) {
263 		seq_puts(s, adm1266_names[ADM1266_GPIO_NR + i]);
264 
265 		pdio_config = read_buf[2 * i];
266 		pdio_config += (read_buf[2 * i + 1] << 8);
267 		pin_cfg = ADM1266_PDIO_PIN_CFG(pdio_config);
268 
269 		seq_puts(s, " ( ");
270 		if (!pin_cfg || pin_cfg > 5) {
271 			seq_puts(s, "high-Z )\n");
272 			continue;
273 		}
274 
275 		if (pin_cfg & BIT(0))
276 			seq_puts(s, "output ");
277 
278 		if (pin_cfg & BIT(1))
279 			seq_puts(s, "input ");
280 
281 		seq_puts(s, ")\n");
282 	}
283 }
284 
285 static int adm1266_config_gpio(struct adm1266_data *data)
286 {
287 	const char *name = dev_name(&data->client->dev);
288 	char *gpio_name;
289 	int ret;
290 	int i;
291 
292 	for (i = 0; i < ARRAY_SIZE(data->gpio_names); i++) {
293 		gpio_name = devm_kasprintf(&data->client->dev, GFP_KERNEL, "adm1266-%x-%s",
294 					   data->client->addr, adm1266_names[i]);
295 		if (!gpio_name)
296 			return -ENOMEM;
297 
298 		data->gpio_names[i] = gpio_name;
299 	}
300 
301 	data->gc.label = name;
302 	data->gc.parent = &data->client->dev;
303 	data->gc.owner = THIS_MODULE;
304 	data->gc.can_sleep = true;
305 	data->gc.base = -1;
306 	data->gc.names = data->gpio_names;
307 	data->gc.ngpio = ARRAY_SIZE(data->gpio_names);
308 	data->gc.get = adm1266_gpio_get;
309 	data->gc.get_multiple = adm1266_gpio_get_multiple;
310 	data->gc.dbg_show = adm1266_gpio_dbg_show;
311 
312 	ret = devm_gpiochip_add_data(&data->client->dev, &data->gc, data);
313 	if (ret)
314 		dev_err(&data->client->dev, "GPIO registering failed (%d)\n", ret);
315 
316 	return ret;
317 }
318 
319 static int adm1266_state_read(struct seq_file *s, void *pdata)
320 {
321 	struct device *dev = s->private;
322 	struct i2c_client *client = to_i2c_client(dev);
323 	int ret;
324 
325 	ret = i2c_smbus_read_word_data(client, ADM1266_READ_STATE);
326 	if (ret < 0)
327 		return ret;
328 
329 	seq_printf(s, "%d\n", ret);
330 
331 	return 0;
332 }
333 
334 static void adm1266_init_debugfs(struct adm1266_data *data)
335 {
336 	struct dentry *root;
337 
338 	root = pmbus_get_debugfs_dir(data->client);
339 	if (!root)
340 		return;
341 
342 	data->debugfs_dir = debugfs_create_dir(data->client->name, root);
343 	if (!data->debugfs_dir)
344 		return;
345 
346 	debugfs_create_devm_seqfile(&data->client->dev, "sequencer_state", data->debugfs_dir,
347 				    adm1266_state_read);
348 }
349 
350 static int adm1266_nvmem_read_blackbox(struct adm1266_data *data, u8 *read_buff)
351 {
352 	int record_count;
353 	char index;
354 	u8 buf[5];
355 	int ret;
356 
357 	ret = i2c_smbus_read_block_data(data->client, ADM1266_BLACKBOX_INFO, buf);
358 	if (ret < 0)
359 		return ret;
360 
361 	if (ret != 4)
362 		return -EIO;
363 
364 	record_count = buf[3];
365 
366 	for (index = 0; index < record_count; index++) {
367 		ret = adm1266_pmbus_block_xfer(data, ADM1266_READ_BLACKBOX, 1, &index, read_buff);
368 		if (ret < 0)
369 			return ret;
370 
371 		if (ret != ADM1266_BLACKBOX_SIZE)
372 			return -EIO;
373 
374 		read_buff += ADM1266_BLACKBOX_SIZE;
375 	}
376 
377 	return 0;
378 }
379 
380 static int adm1266_nvmem_read(void *priv, unsigned int offset, void *val, size_t bytes)
381 {
382 	struct adm1266_data *data = priv;
383 	int ret;
384 
385 	if (offset + bytes > data->nvmem_config.size)
386 		return -EINVAL;
387 
388 	if (offset == 0) {
389 		memset(data->dev_mem, 0, data->nvmem_config.size);
390 
391 		ret = adm1266_nvmem_read_blackbox(data, data->dev_mem);
392 		if (ret) {
393 			dev_err(&data->client->dev, "Could not read blackbox!");
394 			return ret;
395 		}
396 	}
397 
398 	memcpy(val, data->dev_mem + offset, bytes);
399 
400 	return 0;
401 }
402 
403 static int adm1266_config_nvmem(struct adm1266_data *data)
404 {
405 	data->nvmem_config.name = dev_name(&data->client->dev);
406 	data->nvmem_config.dev = &data->client->dev;
407 	data->nvmem_config.root_only = true;
408 	data->nvmem_config.read_only = true;
409 	data->nvmem_config.owner = THIS_MODULE;
410 	data->nvmem_config.reg_read = adm1266_nvmem_read;
411 	data->nvmem_config.cells = adm1266_nvmem_cells;
412 	data->nvmem_config.ncells = ARRAY_SIZE(adm1266_nvmem_cells);
413 	data->nvmem_config.priv = data;
414 	data->nvmem_config.stride = 1;
415 	data->nvmem_config.word_size = 1;
416 	data->nvmem_config.size = adm1266_nvmem_cells[0].bytes;
417 
418 	data->dev_mem = devm_kzalloc(&data->client->dev, data->nvmem_config.size, GFP_KERNEL);
419 	if (!data->dev_mem)
420 		return -ENOMEM;
421 
422 	data->nvmem = devm_nvmem_register(&data->client->dev, &data->nvmem_config);
423 	if (IS_ERR(data->nvmem)) {
424 		dev_err(&data->client->dev, "Could not register nvmem!");
425 		return PTR_ERR(data->nvmem);
426 	}
427 
428 	return 0;
429 }
430 
431 static int adm1266_set_rtc(struct adm1266_data *data)
432 {
433 	time64_t kt;
434 	char write_buf[6];
435 	int i;
436 
437 	kt = ktime_get_seconds();
438 
439 	memset(write_buf, 0, sizeof(write_buf));
440 
441 	for (i = 0; i < 4; i++)
442 		write_buf[2 + i] = (kt >> (i * 8)) & 0xFF;
443 
444 	return i2c_smbus_write_block_data(data->client, ADM1266_SET_RTC, sizeof(write_buf),
445 					  write_buf);
446 }
447 
448 static int adm1266_probe(struct i2c_client *client)
449 {
450 	struct adm1266_data *data;
451 	int ret;
452 	int i;
453 
454 	data = devm_kzalloc(&client->dev, sizeof(struct adm1266_data), GFP_KERNEL);
455 	if (!data)
456 		return -ENOMEM;
457 
458 	data->client = client;
459 	data->info.pages = 17;
460 	data->info.format[PSC_VOLTAGE_OUT] = linear;
461 	for (i = 0; i < data->info.pages; i++)
462 		data->info.func[i] = PMBUS_HAVE_VOUT | PMBUS_HAVE_STATUS_VOUT;
463 
464 	crc8_populate_msb(pmbus_crc_table, 0x7);
465 	mutex_init(&data->buf_mutex);
466 
467 	ret = adm1266_config_gpio(data);
468 	if (ret < 0)
469 		return ret;
470 
471 	ret = adm1266_set_rtc(data);
472 	if (ret < 0)
473 		return ret;
474 
475 	ret = adm1266_config_nvmem(data);
476 	if (ret < 0)
477 		return ret;
478 
479 	ret = pmbus_do_probe(client, &data->info);
480 	if (ret)
481 		return ret;
482 
483 	adm1266_init_debugfs(data);
484 
485 	return 0;
486 }
487 
488 static const struct of_device_id adm1266_of_match[] = {
489 	{ .compatible = "adi,adm1266" },
490 	{ }
491 };
492 MODULE_DEVICE_TABLE(of, adm1266_of_match);
493 
494 static const struct i2c_device_id adm1266_id[] = {
495 	{ "adm1266", 0 },
496 	{ }
497 };
498 MODULE_DEVICE_TABLE(i2c, adm1266_id);
499 
500 static struct i2c_driver adm1266_driver = {
501 	.driver = {
502 		   .name = "adm1266",
503 		   .of_match_table = adm1266_of_match,
504 		  },
505 	.probe_new = adm1266_probe,
506 	.id_table = adm1266_id,
507 };
508 
509 module_i2c_driver(adm1266_driver);
510 
511 MODULE_AUTHOR("Alexandru Tachici <alexandru.tachici@analog.com>");
512 MODULE_DESCRIPTION("PMBus driver for Analog Devices ADM1266");
513 MODULE_LICENSE("GPL v2");
514 MODULE_IMPORT_NS(PMBUS);
515