xref: /openbmc/linux/drivers/mfd/sprd-sc27xx-spi.c (revision 55fd7e02)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2017 Spreadtrum Communications Inc.
4  */
5 
6 #include <linux/interrupt.h>
7 #include <linux/kernel.h>
8 #include <linux/module.h>
9 #include <linux/mfd/core.h>
10 #include <linux/of_device.h>
11 #include <linux/regmap.h>
12 #include <linux/spi/spi.h>
13 #include <uapi/linux/usb/charger.h>
14 
15 #define SPRD_PMIC_INT_MASK_STATUS	0x0
16 #define SPRD_PMIC_INT_RAW_STATUS	0x4
17 #define SPRD_PMIC_INT_EN		0x8
18 
19 #define SPRD_SC2731_IRQ_BASE		0x140
20 #define SPRD_SC2731_IRQ_NUMS		16
21 #define SPRD_SC2731_CHG_DET		0xedc
22 
23 /* PMIC charger detection definition */
24 #define SPRD_PMIC_CHG_DET_DELAY_US	200000
25 #define SPRD_PMIC_CHG_DET_TIMEOUT	2000000
26 #define SPRD_PMIC_CHG_DET_DONE		BIT(11)
27 #define SPRD_PMIC_SDP_TYPE		BIT(7)
28 #define SPRD_PMIC_DCP_TYPE		BIT(6)
29 #define SPRD_PMIC_CDP_TYPE		BIT(5)
30 #define SPRD_PMIC_CHG_TYPE_MASK		GENMASK(7, 5)
31 
32 struct sprd_pmic {
33 	struct regmap *regmap;
34 	struct device *dev;
35 	struct regmap_irq *irqs;
36 	struct regmap_irq_chip irq_chip;
37 	struct regmap_irq_chip_data *irq_data;
38 	const struct sprd_pmic_data *pdata;
39 	int irq;
40 };
41 
42 struct sprd_pmic_data {
43 	u32 irq_base;
44 	u32 num_irqs;
45 	u32 charger_det;
46 };
47 
48 /*
49  * Since different PMICs of SC27xx series can have different interrupt
50  * base address and irq number, we should save irq number and irq base
51  * in the device data structure.
52  */
53 static const struct sprd_pmic_data sc2731_data = {
54 	.irq_base = SPRD_SC2731_IRQ_BASE,
55 	.num_irqs = SPRD_SC2731_IRQ_NUMS,
56 	.charger_det = SPRD_SC2731_CHG_DET,
57 };
58 
59 enum usb_charger_type sprd_pmic_detect_charger_type(struct device *dev)
60 {
61 	struct spi_device *spi = to_spi_device(dev);
62 	struct sprd_pmic *ddata = spi_get_drvdata(spi);
63 	const struct sprd_pmic_data *pdata = ddata->pdata;
64 	enum usb_charger_type type;
65 	u32 val;
66 	int ret;
67 
68 	ret = regmap_read_poll_timeout(ddata->regmap, pdata->charger_det, val,
69 				       (val & SPRD_PMIC_CHG_DET_DONE),
70 				       SPRD_PMIC_CHG_DET_DELAY_US,
71 				       SPRD_PMIC_CHG_DET_TIMEOUT);
72 	if (ret) {
73 		dev_err(&spi->dev, "failed to detect charger type\n");
74 		return UNKNOWN_TYPE;
75 	}
76 
77 	switch (val & SPRD_PMIC_CHG_TYPE_MASK) {
78 	case SPRD_PMIC_CDP_TYPE:
79 		type = CDP_TYPE;
80 		break;
81 	case SPRD_PMIC_DCP_TYPE:
82 		type = DCP_TYPE;
83 		break;
84 	case SPRD_PMIC_SDP_TYPE:
85 		type = SDP_TYPE;
86 		break;
87 	default:
88 		type = UNKNOWN_TYPE;
89 		break;
90 	}
91 
92 	return type;
93 }
94 EXPORT_SYMBOL_GPL(sprd_pmic_detect_charger_type);
95 
96 static const struct mfd_cell sprd_pmic_devs[] = {
97 	{
98 		.name = "sc27xx-wdt",
99 		.of_compatible = "sprd,sc2731-wdt",
100 	}, {
101 		.name = "sc27xx-rtc",
102 		.of_compatible = "sprd,sc2731-rtc",
103 	}, {
104 		.name = "sc27xx-charger",
105 		.of_compatible = "sprd,sc2731-charger",
106 	}, {
107 		.name = "sc27xx-chg-timer",
108 		.of_compatible = "sprd,sc2731-chg-timer",
109 	}, {
110 		.name = "sc27xx-fast-chg",
111 		.of_compatible = "sprd,sc2731-fast-chg",
112 	}, {
113 		.name = "sc27xx-chg-wdt",
114 		.of_compatible = "sprd,sc2731-chg-wdt",
115 	}, {
116 		.name = "sc27xx-typec",
117 		.of_compatible = "sprd,sc2731-typec",
118 	}, {
119 		.name = "sc27xx-flash",
120 		.of_compatible = "sprd,sc2731-flash",
121 	}, {
122 		.name = "sc27xx-eic",
123 		.of_compatible = "sprd,sc2731-eic",
124 	}, {
125 		.name = "sc27xx-efuse",
126 		.of_compatible = "sprd,sc2731-efuse",
127 	}, {
128 		.name = "sc27xx-thermal",
129 		.of_compatible = "sprd,sc2731-thermal",
130 	}, {
131 		.name = "sc27xx-adc",
132 		.of_compatible = "sprd,sc2731-adc",
133 	}, {
134 		.name = "sc27xx-audio-codec",
135 		.of_compatible = "sprd,sc2731-audio-codec",
136 	}, {
137 		.name = "sc27xx-regulator",
138 		.of_compatible = "sprd,sc2731-regulator",
139 	}, {
140 		.name = "sc27xx-vibrator",
141 		.of_compatible = "sprd,sc2731-vibrator",
142 	}, {
143 		.name = "sc27xx-keypad-led",
144 		.of_compatible = "sprd,sc2731-keypad-led",
145 	}, {
146 		.name = "sc27xx-bltc",
147 		.of_compatible = "sprd,sc2731-bltc",
148 	}, {
149 		.name = "sc27xx-fgu",
150 		.of_compatible = "sprd,sc2731-fgu",
151 	}, {
152 		.name = "sc27xx-7sreset",
153 		.of_compatible = "sprd,sc2731-7sreset",
154 	}, {
155 		.name = "sc27xx-poweroff",
156 		.of_compatible = "sprd,sc2731-poweroff",
157 	}, {
158 		.name = "sc27xx-syscon",
159 		.of_compatible = "sprd,sc2731-syscon",
160 	},
161 };
162 
163 static int sprd_pmic_spi_write(void *context, const void *data, size_t count)
164 {
165 	struct device *dev = context;
166 	struct spi_device *spi = to_spi_device(dev);
167 
168 	return spi_write(spi, data, count);
169 }
170 
171 static int sprd_pmic_spi_read(void *context,
172 			      const void *reg, size_t reg_size,
173 			      void *val, size_t val_size)
174 {
175 	struct device *dev = context;
176 	struct spi_device *spi = to_spi_device(dev);
177 	u32 rx_buf[2] = { 0 };
178 	int ret;
179 
180 	/* Now we only support one PMIC register to read every time. */
181 	if (reg_size != sizeof(u32) || val_size != sizeof(u32))
182 		return -EINVAL;
183 
184 	/* Copy address to read from into first element of SPI buffer. */
185 	memcpy(rx_buf, reg, sizeof(u32));
186 	ret = spi_read(spi, rx_buf, 1);
187 	if (ret < 0)
188 		return ret;
189 
190 	memcpy(val, rx_buf, val_size);
191 	return 0;
192 }
193 
194 static struct regmap_bus sprd_pmic_regmap = {
195 	.write = sprd_pmic_spi_write,
196 	.read = sprd_pmic_spi_read,
197 	.reg_format_endian_default = REGMAP_ENDIAN_NATIVE,
198 	.val_format_endian_default = REGMAP_ENDIAN_NATIVE,
199 };
200 
201 static const struct regmap_config sprd_pmic_config = {
202 	.reg_bits = 32,
203 	.val_bits = 32,
204 	.reg_stride = 4,
205 	.max_register = 0xffff,
206 };
207 
208 static int sprd_pmic_probe(struct spi_device *spi)
209 {
210 	struct sprd_pmic *ddata;
211 	const struct sprd_pmic_data *pdata;
212 	int ret, i;
213 
214 	pdata = of_device_get_match_data(&spi->dev);
215 	if (!pdata) {
216 		dev_err(&spi->dev, "No matching driver data found\n");
217 		return -EINVAL;
218 	}
219 
220 	ddata = devm_kzalloc(&spi->dev, sizeof(*ddata), GFP_KERNEL);
221 	if (!ddata)
222 		return -ENOMEM;
223 
224 	ddata->regmap = devm_regmap_init(&spi->dev, &sprd_pmic_regmap,
225 					 &spi->dev, &sprd_pmic_config);
226 	if (IS_ERR(ddata->regmap)) {
227 		ret = PTR_ERR(ddata->regmap);
228 		dev_err(&spi->dev, "Failed to allocate register map %d\n", ret);
229 		return ret;
230 	}
231 
232 	spi_set_drvdata(spi, ddata);
233 	ddata->dev = &spi->dev;
234 	ddata->irq = spi->irq;
235 	ddata->pdata = pdata;
236 
237 	ddata->irq_chip.name = dev_name(&spi->dev);
238 	ddata->irq_chip.status_base =
239 		pdata->irq_base + SPRD_PMIC_INT_MASK_STATUS;
240 	ddata->irq_chip.mask_base = pdata->irq_base + SPRD_PMIC_INT_EN;
241 	ddata->irq_chip.ack_base = 0;
242 	ddata->irq_chip.num_regs = 1;
243 	ddata->irq_chip.num_irqs = pdata->num_irqs;
244 	ddata->irq_chip.mask_invert = true;
245 
246 	ddata->irqs = devm_kcalloc(&spi->dev,
247 				   pdata->num_irqs, sizeof(struct regmap_irq),
248 				   GFP_KERNEL);
249 	if (!ddata->irqs)
250 		return -ENOMEM;
251 
252 	ddata->irq_chip.irqs = ddata->irqs;
253 	for (i = 0; i < pdata->num_irqs; i++) {
254 		ddata->irqs[i].reg_offset = i / pdata->num_irqs;
255 		ddata->irqs[i].mask = BIT(i % pdata->num_irqs);
256 	}
257 
258 	ret = devm_regmap_add_irq_chip(&spi->dev, ddata->regmap, ddata->irq,
259 				       IRQF_ONESHOT | IRQF_NO_SUSPEND, 0,
260 				       &ddata->irq_chip, &ddata->irq_data);
261 	if (ret) {
262 		dev_err(&spi->dev, "Failed to add PMIC irq chip %d\n", ret);
263 		return ret;
264 	}
265 
266 	ret = devm_mfd_add_devices(&spi->dev, PLATFORM_DEVID_AUTO,
267 				   sprd_pmic_devs, ARRAY_SIZE(sprd_pmic_devs),
268 				   NULL, 0,
269 				   regmap_irq_get_domain(ddata->irq_data));
270 	if (ret) {
271 		dev_err(&spi->dev, "Failed to register device %d\n", ret);
272 		return ret;
273 	}
274 
275 	return 0;
276 }
277 
278 static const struct of_device_id sprd_pmic_match[] = {
279 	{ .compatible = "sprd,sc2731", .data = &sc2731_data },
280 	{},
281 };
282 MODULE_DEVICE_TABLE(of, sprd_pmic_match);
283 
284 static struct spi_driver sprd_pmic_driver = {
285 	.driver = {
286 		.name = "sc27xx-pmic",
287 		.of_match_table = sprd_pmic_match,
288 	},
289 	.probe = sprd_pmic_probe,
290 };
291 
292 static int __init sprd_pmic_init(void)
293 {
294 	return spi_register_driver(&sprd_pmic_driver);
295 }
296 subsys_initcall(sprd_pmic_init);
297 
298 static void __exit sprd_pmic_exit(void)
299 {
300 	spi_unregister_driver(&sprd_pmic_driver);
301 }
302 module_exit(sprd_pmic_exit);
303 
304 MODULE_LICENSE("GPL v2");
305 MODULE_DESCRIPTION("Spreadtrum SC27xx PMICs driver");
306 MODULE_AUTHOR("Baolin Wang <baolin.wang@spreadtrum.com>");
307