xref: /linux/drivers/mfd/tps65910.c (revision bba2c3615bd6cfee7456d1130f2e6b01b3f4e9ba)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * tps65910.c  --  TI TPS6591x chip family multi-function driver
4  *
5  * Copyright 2010 Texas Instruments Inc.
6  *
7  * Author: Graeme Gregory <gg@slimlogic.co.uk>
8  * Author: Jorge Eduardo Candelaria <jedu@slimlogic.co.uk>
9  */
10 
11 #include <linux/init.h>
12 #include <linux/err.h>
13 #include <linux/slab.h>
14 #include <linux/i2c.h>
15 #include <linux/interrupt.h>
16 #include <linux/irq.h>
17 #include <linux/irqdomain.h>
18 #include <linux/mfd/core.h>
19 #include <linux/regmap.h>
20 #include <linux/mfd/tps65910.h>
21 #include <linux/of.h>
22 #include <linux/property.h>
23 
24 /* Dummy I2C transfer length for SWCZ010 workaround */
25 #define TPS65910_DUMMY_XFER_LEN 1
26 
27 static const struct resource rtc_resources[] = {
28 	{
29 		.start  = TPS65910_IRQ_RTC_ALARM,
30 		.end    = TPS65910_IRQ_RTC_ALARM,
31 		.flags  = IORESOURCE_IRQ,
32 	}
33 };
34 
35 static const struct mfd_cell tps65910s[] = {
36 	{
37 		.name = "tps65910-gpio",
38 	},
39 	{
40 		.name = "tps65910-pmic",
41 	},
42 	{
43 		.name = "tps65910-rtc",
44 		.num_resources = ARRAY_SIZE(rtc_resources),
45 		.resources = &rtc_resources[0],
46 	},
47 	{
48 		.name = "tps65910-power",
49 	},
50 };
51 
52 
53 static const struct regmap_irq tps65911_irqs[] = {
54 	/* INT_STS */
55 	[TPS65911_IRQ_PWRHOLD_F] = {
56 		.mask = INT_MSK_PWRHOLD_F_IT_MSK_MASK,
57 		.reg_offset = 0,
58 	},
59 	[TPS65911_IRQ_VBAT_VMHI] = {
60 		.mask = INT_MSK_VMBHI_IT_MSK_MASK,
61 		.reg_offset = 0,
62 	},
63 	[TPS65911_IRQ_PWRON] = {
64 		.mask = INT_MSK_PWRON_IT_MSK_MASK,
65 		.reg_offset = 0,
66 	},
67 	[TPS65911_IRQ_PWRON_LP] = {
68 		.mask = INT_MSK_PWRON_LP_IT_MSK_MASK,
69 		.reg_offset = 0,
70 	},
71 	[TPS65911_IRQ_PWRHOLD_R] = {
72 		.mask = INT_MSK_PWRHOLD_R_IT_MSK_MASK,
73 		.reg_offset = 0,
74 	},
75 	[TPS65911_IRQ_HOTDIE] = {
76 		.mask = INT_MSK_HOTDIE_IT_MSK_MASK,
77 		.reg_offset = 0,
78 	},
79 	[TPS65911_IRQ_RTC_ALARM] = {
80 		.mask = INT_MSK_RTC_ALARM_IT_MSK_MASK,
81 		.reg_offset = 0,
82 	},
83 	[TPS65911_IRQ_RTC_PERIOD] = {
84 		.mask = INT_MSK_RTC_PERIOD_IT_MSK_MASK,
85 		.reg_offset = 0,
86 	},
87 
88 	/* INT_STS2 */
89 	[TPS65911_IRQ_GPIO0_R] = {
90 		.mask = INT_MSK2_GPIO0_R_IT_MSK_MASK,
91 		.reg_offset = 1,
92 	},
93 	[TPS65911_IRQ_GPIO0_F] = {
94 		.mask = INT_MSK2_GPIO0_F_IT_MSK_MASK,
95 		.reg_offset = 1,
96 	},
97 	[TPS65911_IRQ_GPIO1_R] = {
98 		.mask = INT_MSK2_GPIO1_R_IT_MSK_MASK,
99 		.reg_offset = 1,
100 	},
101 	[TPS65911_IRQ_GPIO1_F] = {
102 		.mask = INT_MSK2_GPIO1_F_IT_MSK_MASK,
103 		.reg_offset = 1,
104 	},
105 	[TPS65911_IRQ_GPIO2_R] = {
106 		.mask = INT_MSK2_GPIO2_R_IT_MSK_MASK,
107 		.reg_offset = 1,
108 	},
109 	[TPS65911_IRQ_GPIO2_F] = {
110 		.mask = INT_MSK2_GPIO2_F_IT_MSK_MASK,
111 		.reg_offset = 1,
112 	},
113 	[TPS65911_IRQ_GPIO3_R] = {
114 		.mask = INT_MSK2_GPIO3_R_IT_MSK_MASK,
115 		.reg_offset = 1,
116 	},
117 	[TPS65911_IRQ_GPIO3_F] = {
118 		.mask = INT_MSK2_GPIO3_F_IT_MSK_MASK,
119 		.reg_offset = 1,
120 	},
121 
122 	/* INT_STS2 */
123 	[TPS65911_IRQ_GPIO4_R] = {
124 		.mask = INT_MSK3_GPIO4_R_IT_MSK_MASK,
125 		.reg_offset = 2,
126 	},
127 	[TPS65911_IRQ_GPIO4_F] = {
128 		.mask = INT_MSK3_GPIO4_F_IT_MSK_MASK,
129 		.reg_offset = 2,
130 	},
131 	[TPS65911_IRQ_GPIO5_R] = {
132 		.mask = INT_MSK3_GPIO5_R_IT_MSK_MASK,
133 		.reg_offset = 2,
134 	},
135 	[TPS65911_IRQ_GPIO5_F] = {
136 		.mask = INT_MSK3_GPIO5_F_IT_MSK_MASK,
137 		.reg_offset = 2,
138 	},
139 	[TPS65911_IRQ_WTCHDG] = {
140 		.mask = INT_MSK3_WTCHDG_IT_MSK_MASK,
141 		.reg_offset = 2,
142 	},
143 	[TPS65911_IRQ_VMBCH2_H] = {
144 		.mask = INT_MSK3_VMBCH2_H_IT_MSK_MASK,
145 		.reg_offset = 2,
146 	},
147 	[TPS65911_IRQ_VMBCH2_L] = {
148 		.mask = INT_MSK3_VMBCH2_L_IT_MSK_MASK,
149 		.reg_offset = 2,
150 	},
151 	[TPS65911_IRQ_PWRDN] = {
152 		.mask = INT_MSK3_PWRDN_IT_MSK_MASK,
153 		.reg_offset = 2,
154 	},
155 };
156 
157 static const struct regmap_irq tps65910_irqs[] = {
158 	/* INT_STS */
159 	[TPS65910_IRQ_VBAT_VMBDCH] = {
160 		.mask = TPS65910_INT_MSK_VMBDCH_IT_MSK_MASK,
161 		.reg_offset = 0,
162 	},
163 	[TPS65910_IRQ_VBAT_VMHI] = {
164 		.mask = TPS65910_INT_MSK_VMBHI_IT_MSK_MASK,
165 		.reg_offset = 0,
166 	},
167 	[TPS65910_IRQ_PWRON] = {
168 		.mask = TPS65910_INT_MSK_PWRON_IT_MSK_MASK,
169 		.reg_offset = 0,
170 	},
171 	[TPS65910_IRQ_PWRON_LP] = {
172 		.mask = TPS65910_INT_MSK_PWRON_LP_IT_MSK_MASK,
173 		.reg_offset = 0,
174 	},
175 	[TPS65910_IRQ_PWRHOLD] = {
176 		.mask = TPS65910_INT_MSK_PWRHOLD_IT_MSK_MASK,
177 		.reg_offset = 0,
178 	},
179 	[TPS65910_IRQ_HOTDIE] = {
180 		.mask = TPS65910_INT_MSK_HOTDIE_IT_MSK_MASK,
181 		.reg_offset = 0,
182 	},
183 	[TPS65910_IRQ_RTC_ALARM] = {
184 		.mask = TPS65910_INT_MSK_RTC_ALARM_IT_MSK_MASK,
185 		.reg_offset = 0,
186 	},
187 	[TPS65910_IRQ_RTC_PERIOD] = {
188 		.mask = TPS65910_INT_MSK_RTC_PERIOD_IT_MSK_MASK,
189 		.reg_offset = 0,
190 	},
191 
192 	/* INT_STS2 */
193 	[TPS65910_IRQ_GPIO_R] = {
194 		.mask = TPS65910_INT_MSK2_GPIO0_F_IT_MSK_MASK,
195 		.reg_offset = 1,
196 	},
197 	[TPS65910_IRQ_GPIO_F] = {
198 		.mask = TPS65910_INT_MSK2_GPIO0_R_IT_MSK_MASK,
199 		.reg_offset = 1,
200 	},
201 };
202 
203 static const struct regmap_irq_chip tps65911_irq_chip = {
204 	.name = "tps65910",
205 	.irqs = tps65911_irqs,
206 	.num_irqs = ARRAY_SIZE(tps65911_irqs),
207 	.num_regs = 3,
208 	.irq_reg_stride = 2,
209 	.status_base = TPS65910_INT_STS,
210 	.mask_base = TPS65910_INT_MSK,
211 	.ack_base = TPS65910_INT_STS,
212 };
213 
214 static const struct regmap_irq_chip tps65910_irq_chip = {
215 	.name = "tps65910",
216 	.irqs = tps65910_irqs,
217 	.num_irqs = ARRAY_SIZE(tps65910_irqs),
218 	.num_regs = 2,
219 	.irq_reg_stride = 2,
220 	.status_base = TPS65910_INT_STS,
221 	.mask_base = TPS65910_INT_MSK,
222 	.ack_base = TPS65910_INT_STS,
223 };
224 
225 static int tps65910_irq_init(struct tps65910 *tps65910, int irq,
226 		    struct tps65910_platform_data *pdata)
227 {
228 	int ret;
229 	static const struct regmap_irq_chip *tps6591x_irqs_chip;
230 
231 	if (!irq) {
232 		dev_warn(tps65910->dev, "No interrupt support, no core IRQ\n");
233 		return -EINVAL;
234 	}
235 
236 	if (!pdata) {
237 		dev_warn(tps65910->dev, "No interrupt support, no pdata\n");
238 		return -EINVAL;
239 	}
240 
241 	switch (tps65910_chip_id(tps65910)) {
242 	case TPS65910:
243 		tps6591x_irqs_chip = &tps65910_irq_chip;
244 		break;
245 	case TPS65911:
246 		tps6591x_irqs_chip = &tps65911_irq_chip;
247 		break;
248 	}
249 
250 	tps65910->chip_irq = irq;
251 	ret = devm_regmap_add_irq_chip(tps65910->dev, tps65910->regmap,
252 				       tps65910->chip_irq,
253 				       IRQF_ONESHOT, pdata->irq_base,
254 				       tps6591x_irqs_chip, &tps65910->irq_data);
255 	if (ret < 0) {
256 		dev_warn(tps65910->dev, "Failed to add irq_chip %d\n", ret);
257 		tps65910->chip_irq = 0;
258 	}
259 	return ret;
260 }
261 
262 static bool is_volatile_reg(struct device *dev, unsigned int reg)
263 {
264 	struct tps65910 *tps65910 = dev_get_drvdata(dev);
265 
266 	/*
267 	 * Caching all regulator registers.
268 	 * All regualator register address range is same for
269 	 * TPS65910 and TPS65911
270 	 */
271 	if ((reg >= TPS65910_VIO) && (reg <= TPS65910_VDAC)) {
272 		/* Check for non-existing register */
273 		if (tps65910_chip_id(tps65910) == TPS65910)
274 			if ((reg == TPS65911_VDDCTRL_OP) ||
275 				(reg == TPS65911_VDDCTRL_SR))
276 				return true;
277 		return false;
278 	}
279 	return true;
280 }
281 
282 static const struct regmap_config tps65910_regmap_config = {
283 	.reg_bits = 8,
284 	.val_bits = 8,
285 	.volatile_reg = is_volatile_reg,
286 	.max_register = TPS65910_MAX_REGISTER - 1,
287 	.cache_type = REGCACHE_MAPLE,
288 };
289 
290 static int tps65910_ck32k_init(struct tps65910 *tps65910,
291 					struct tps65910_board *pmic_pdata)
292 {
293 	int ret;
294 
295 	if (!pmic_pdata->en_ck32k_xtal)
296 		return 0;
297 
298 	ret = regmap_clear_bits(tps65910->regmap, TPS65910_DEVCTRL,
299 				DEVCTRL_CK32K_CTRL_MASK);
300 	if (ret < 0) {
301 		dev_err(tps65910->dev, "clear ck32k_ctrl failed: %d\n", ret);
302 		return ret;
303 	}
304 
305 	return 0;
306 }
307 
308 static int tps65910_sleepinit(struct tps65910 *tps65910,
309 		struct tps65910_board *pmic_pdata)
310 {
311 	struct device *dev;
312 	int ret;
313 
314 	if (!pmic_pdata->en_dev_slp)
315 		return 0;
316 
317 	dev = tps65910->dev;
318 
319 	/* enabling SLEEP device state */
320 	ret = regmap_set_bits(tps65910->regmap, TPS65910_DEVCTRL,
321 			      DEVCTRL_DEV_SLP_MASK);
322 	if (ret < 0) {
323 		dev_err(dev, "set dev_slp failed: %d\n", ret);
324 		goto err_sleep_init;
325 	}
326 
327 	if (pmic_pdata->slp_keepon.therm_keepon) {
328 		ret = regmap_set_bits(tps65910->regmap,
329 				      TPS65910_SLEEP_KEEP_RES_ON,
330 				      SLEEP_KEEP_RES_ON_THERM_KEEPON_MASK);
331 		if (ret < 0) {
332 			dev_err(dev, "set therm_keepon failed: %d\n", ret);
333 			goto disable_dev_slp;
334 		}
335 	}
336 
337 	if (pmic_pdata->slp_keepon.clkout32k_keepon) {
338 		ret = regmap_set_bits(tps65910->regmap,
339 				      TPS65910_SLEEP_KEEP_RES_ON,
340 				      SLEEP_KEEP_RES_ON_CLKOUT32K_KEEPON_MASK);
341 		if (ret < 0) {
342 			dev_err(dev, "set clkout32k_keepon failed: %d\n", ret);
343 			goto disable_dev_slp;
344 		}
345 	}
346 
347 	if (pmic_pdata->slp_keepon.i2chs_keepon) {
348 		ret = regmap_set_bits(tps65910->regmap,
349 				      TPS65910_SLEEP_KEEP_RES_ON,
350 				      SLEEP_KEEP_RES_ON_I2CHS_KEEPON_MASK);
351 		if (ret < 0) {
352 			dev_err(dev, "set i2chs_keepon failed: %d\n", ret);
353 			goto disable_dev_slp;
354 		}
355 	}
356 
357 	return 0;
358 
359 disable_dev_slp:
360 	regmap_clear_bits(tps65910->regmap, TPS65910_DEVCTRL,
361 			  DEVCTRL_DEV_SLP_MASK);
362 
363 err_sleep_init:
364 	return ret;
365 }
366 
367 #ifdef CONFIG_OF
368 static const struct of_device_id tps65910_of_match[] = {
369 	{ .compatible = "ti,tps65910", .data = (void *)TPS65910},
370 	{ .compatible = "ti,tps65911", .data = (void *)TPS65911},
371 	{ },
372 };
373 
374 static struct tps65910_board *tps65910_parse_dt(struct i2c_client *client,
375 						unsigned long *chip_id)
376 {
377 	struct device_node *np = client->dev.of_node;
378 	struct tps65910_board *board_info;
379 	unsigned int prop;
380 	int ret;
381 
382 	*chip_id  = (unsigned long)device_get_match_data(&client->dev);
383 
384 	board_info = devm_kzalloc(&client->dev, sizeof(*board_info),
385 			GFP_KERNEL);
386 	if (!board_info)
387 		return NULL;
388 
389 	ret = of_property_read_u32(np, "ti,vmbch-threshold", &prop);
390 	if (!ret)
391 		board_info->vmbch_threshold = prop;
392 
393 	ret = of_property_read_u32(np, "ti,vmbch2-threshold", &prop);
394 	if (!ret)
395 		board_info->vmbch2_threshold = prop;
396 
397 	prop = of_property_read_bool(np, "ti,en-ck32k-xtal");
398 	board_info->en_ck32k_xtal = prop;
399 
400 	prop = of_property_read_bool(np, "ti,sleep-enable");
401 	board_info->en_dev_slp = prop;
402 
403 	prop = of_property_read_bool(np, "ti,sleep-keep-therm");
404 	board_info->slp_keepon.therm_keepon = prop;
405 
406 	prop = of_property_read_bool(np, "ti,sleep-keep-ck32k");
407 	board_info->slp_keepon.clkout32k_keepon = prop;
408 
409 	prop = of_property_read_bool(np, "ti,sleep-keep-hsclk");
410 	board_info->slp_keepon.i2chs_keepon = prop;
411 
412 	board_info->irq = client->irq;
413 	board_info->irq_base = -1;
414 	board_info->pm_off = of_property_read_bool(np,
415 			"ti,system-power-controller");
416 
417 	return board_info;
418 }
419 #else
420 static inline
421 struct tps65910_board *tps65910_parse_dt(struct i2c_client *client,
422 					 unsigned long *chip_id)
423 {
424 	return NULL;
425 }
426 #endif
427 
428 static struct i2c_client *tps65910_i2c_client;
429 static void tps65910_power_off(void)
430 {
431 	struct tps65910 *tps65910;
432 
433 	tps65910 = dev_get_drvdata(&tps65910_i2c_client->dev);
434 
435 	regmap_update_bits(tps65910->regmap, TPS65910_DEVCTRL,
436 			   DEVCTRL_DEV_OFF_MASK | DEVCTRL_DEV_ON_MASK,
437 			   DEVCTRL_DEV_OFF_MASK);
438 }
439 
440 static int tps65910_i2c_probe(struct i2c_client *i2c)
441 {
442 	const struct i2c_device_id *id = i2c_client_get_device_id(i2c);
443 	struct tps65910 *tps65910;
444 	struct tps65910_board *pmic_plat_data;
445 	struct tps65910_board *of_pmic_plat_data = NULL;
446 	struct tps65910_platform_data *init_data;
447 	unsigned long chip_id = id->driver_data;
448 	int ret;
449 
450 	pmic_plat_data = dev_get_platdata(&i2c->dev);
451 
452 	if (!pmic_plat_data && i2c->dev.of_node) {
453 		pmic_plat_data = tps65910_parse_dt(i2c, &chip_id);
454 		of_pmic_plat_data = pmic_plat_data;
455 	}
456 
457 	if (!pmic_plat_data)
458 		return -EINVAL;
459 
460 	init_data = devm_kzalloc(&i2c->dev, sizeof(*init_data), GFP_KERNEL);
461 	if (init_data == NULL)
462 		return -ENOMEM;
463 
464 	tps65910 = devm_kzalloc(&i2c->dev, sizeof(*tps65910), GFP_KERNEL);
465 	if (tps65910 == NULL)
466 		return -ENOMEM;
467 
468 	tps65910->of_plat_data = of_pmic_plat_data;
469 	i2c_set_clientdata(i2c, tps65910);
470 	tps65910->dev = &i2c->dev;
471 	tps65910->i2c_client = i2c;
472 	tps65910->id = chip_id;
473 
474 	/* Work around silicon erratum SWCZ010: the tps65910 may miss the
475 	 * first I2C transfer. So issue a dummy transfer before the first
476 	 * real transfer.
477 	 */
478 	ret = i2c_master_send(i2c, "", TPS65910_DUMMY_XFER_LEN);
479 	if (ret != TPS65910_DUMMY_XFER_LEN) {
480 		int err;
481 
482 		if (ret < 0)
483 			err = ret;
484 		else
485 			err = -EIO;
486 
487 		return dev_err_probe(&i2c->dev, err, "dummy transfer failed\n");
488 	}
489 
490 	tps65910->regmap = devm_regmap_init_i2c(i2c, &tps65910_regmap_config);
491 	if (IS_ERR(tps65910->regmap)) {
492 		ret = PTR_ERR(tps65910->regmap);
493 		dev_err(&i2c->dev, "regmap initialization failed: %d\n", ret);
494 		return ret;
495 	}
496 
497 	init_data->irq = pmic_plat_data->irq;
498 	init_data->irq_base = pmic_plat_data->irq_base;
499 
500 	tps65910_irq_init(tps65910, init_data->irq, init_data);
501 	tps65910_ck32k_init(tps65910, pmic_plat_data);
502 	tps65910_sleepinit(tps65910, pmic_plat_data);
503 
504 	if (pmic_plat_data->pm_off && !pm_power_off) {
505 		/*
506 		 * The PWR_OFF bit needs to be set separately, before
507 		 * transitioning to the OFF state. It enables the "sequential"
508 		 * power-off mode on TPS65911, it's a NO-OP on TPS65910.
509 		 */
510 		ret = regmap_set_bits(tps65910->regmap, TPS65910_DEVCTRL,
511 				      DEVCTRL_PWR_OFF_MASK);
512 		if (ret) {
513 			dev_err(&i2c->dev, "failed to set power-off mode: %d\n",
514 				ret);
515 			return ret;
516 		}
517 
518 		tps65910_i2c_client = i2c;
519 		pm_power_off = tps65910_power_off;
520 	}
521 
522 	ret = devm_mfd_add_devices(tps65910->dev, -1,
523 				   tps65910s, ARRAY_SIZE(tps65910s),
524 				   NULL, 0,
525 				   regmap_irq_get_domain(tps65910->irq_data));
526 	if (ret < 0) {
527 		dev_err(&i2c->dev, "mfd_add_devices failed: %d\n", ret);
528 		return ret;
529 	}
530 
531 	return ret;
532 }
533 
534 static const struct i2c_device_id tps65910_i2c_id[] = {
535        { "tps65910", TPS65910 },
536        { "tps65911", TPS65911 },
537        { }
538 };
539 
540 static struct i2c_driver tps65910_i2c_driver = {
541 	.driver = {
542 		   .name = "tps65910",
543 		   .of_match_table = of_match_ptr(tps65910_of_match),
544 	},
545 	.probe = tps65910_i2c_probe,
546 	.id_table = tps65910_i2c_id,
547 };
548 
549 static int __init tps65910_i2c_init(void)
550 {
551 	return i2c_add_driver(&tps65910_i2c_driver);
552 }
553 /* init early so consumer devices can complete system boot */
554 subsys_initcall(tps65910_i2c_init);
555