xref: /linux/drivers/mfd/tps6594-spi.c (revision df822f2e36cf2c2864d166d90cf002df4d8689e5)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * SPI access driver for the following TI PMICs:
4  *  - LP8764
5  *  - TPS65224
6  *  - TPS652G1
7  *  - TPS6593
8  *  - TPS6594
9  *
10  * Copyright (C) 2023 BayLibre Incorporated - https://www.baylibre.com/
11  */
12 
13 #include <linux/crc8.h>
14 #include <linux/module.h>
15 #include <linux/of_device.h>
16 #include <linux/regmap.h>
17 #include <linux/spi/spi.h>
18 
19 #include <linux/mfd/tps6594.h>
20 
21 #define TPS6594_SPI_PAGE_SHIFT	5
22 #define TPS6594_SPI_READ_BIT	BIT(4)
23 
24 static bool enable_crc;
25 module_param(enable_crc, bool, 0444);
26 MODULE_PARM_DESC(enable_crc, "Enable CRC feature for SPI interface");
27 
28 DECLARE_CRC8_TABLE(tps6594_spi_crc_table);
29 
30 static int tps6594_spi_reg_read(void *context, unsigned int reg, unsigned int *val)
31 {
32 	struct spi_device *spi = context;
33 	struct tps6594 *tps = spi_get_drvdata(spi);
34 	u8 buf[4] = { 0 };
35 	size_t count_rx = 1;
36 	int ret;
37 
38 	buf[0] = reg;
39 	buf[1] = TPS6594_REG_TO_PAGE(reg) << TPS6594_SPI_PAGE_SHIFT | TPS6594_SPI_READ_BIT;
40 
41 	if (tps->use_crc)
42 		count_rx++;
43 
44 	ret = spi_write_then_read(spi, buf, 2, buf + 2, count_rx);
45 	if (ret < 0)
46 		return ret;
47 
48 	if (tps->use_crc && buf[3] != crc8(tps6594_spi_crc_table, buf, 3, CRC8_INIT_VALUE))
49 		return -EIO;
50 
51 	*val = buf[2];
52 
53 	return 0;
54 }
55 
56 static int tps6594_spi_reg_write(void *context, unsigned int reg, unsigned int val)
57 {
58 	struct spi_device *spi = context;
59 	struct tps6594 *tps = spi_get_drvdata(spi);
60 	u8 buf[4] = { 0 };
61 	size_t count = 3;
62 
63 	buf[0] = reg;
64 	buf[1] = TPS6594_REG_TO_PAGE(reg) << TPS6594_SPI_PAGE_SHIFT;
65 	buf[2] = val;
66 
67 	if (tps->use_crc)
68 		buf[3] = crc8(tps6594_spi_crc_table, buf, count++, CRC8_INIT_VALUE);
69 
70 	return spi_write(spi, buf, count);
71 }
72 
73 static struct regmap_config tps6594_spi_regmap_config = {
74 	.reg_bits = 16,
75 	.val_bits = 8,
76 	.max_register = TPS6594_REG_DWD_FAIL_CNT_REG,
77 	.volatile_table = &tps6594_volatile_table,
78 	.reg_read = tps6594_spi_reg_read,
79 	.reg_write = tps6594_spi_reg_write,
80 	.use_single_read = true,
81 	.use_single_write = true,
82 };
83 
84 static const struct of_device_id tps6594_spi_of_match_table[] = {
85 	{ .compatible = "ti,tps6594-q1", .data = (void *)TPS6594, },
86 	{ .compatible = "ti,tps6593-q1", .data = (void *)TPS6593, },
87 	{ .compatible = "ti,lp8764-q1",  .data = (void *)LP8764,  },
88 	{ .compatible = "ti,tps65224-q1", .data = (void *)TPS65224, },
89 	{ .compatible = "ti,tps652g1", .data = (void *)TPS652G1, },
90 	{}
91 };
92 MODULE_DEVICE_TABLE(of, tps6594_spi_of_match_table);
93 
94 static int tps6594_spi_probe(struct spi_device *spi)
95 {
96 	struct device *dev = &spi->dev;
97 	struct tps6594 *tps;
98 	const struct of_device_id *match;
99 
100 	tps = devm_kzalloc(dev, sizeof(*tps), GFP_KERNEL);
101 	if (!tps)
102 		return -ENOMEM;
103 
104 	spi_set_drvdata(spi, tps);
105 
106 	tps->dev = dev;
107 	tps->reg = spi_get_chipselect(spi, 0);
108 	tps->irq = spi->irq;
109 
110 	match = of_match_device(tps6594_spi_of_match_table, dev);
111 	if (!match)
112 		return dev_err_probe(dev, -EINVAL, "Failed to find matching chip ID\n");
113 	tps->chip_id = (unsigned long)match->data;
114 
115 	if (tps->chip_id == TPS65224 || tps->chip_id == TPS652G1)
116 		tps6594_spi_regmap_config.volatile_table = &tps65224_volatile_table;
117 
118 	tps->regmap = devm_regmap_init(dev, NULL, spi, &tps6594_spi_regmap_config);
119 	if (IS_ERR(tps->regmap))
120 		return dev_err_probe(dev, PTR_ERR(tps->regmap), "Failed to init regmap\n");
121 
122 	crc8_populate_msb(tps6594_spi_crc_table, TPS6594_CRC8_POLYNOMIAL);
123 
124 	return tps6594_device_init(tps, enable_crc);
125 }
126 
127 static struct spi_driver tps6594_spi_driver = {
128 	.driver	= {
129 		.name = "tps6594",
130 		.of_match_table = tps6594_spi_of_match_table,
131 	},
132 	.probe = tps6594_spi_probe,
133 };
134 module_spi_driver(tps6594_spi_driver);
135 
136 MODULE_AUTHOR("Julien Panis <jpanis@baylibre.com>");
137 MODULE_DESCRIPTION("SPI Interface Driver for TPS65224, TPS6594/3, and LP8764");
138 MODULE_LICENSE("GPL");
139