1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * tps65910.c -- TI TPS6591x chip family multi-function driver
5 * Copyright 2010 Texas Instruments Inc.
7 * Author: Graeme Gregory <gg@slimlogic.co.uk>
8 * Author: Jorge Eduardo Candelaria <jedu@slimlogic.co.uk>
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>
22 #include <linux/of_device.h>
24 static const struct resource rtc_resources
[] = {
26 .start
= TPS65910_IRQ_RTC_ALARM
,
27 .end
= TPS65910_IRQ_RTC_ALARM
,
28 .flags
= IORESOURCE_IRQ
,
32 static const struct mfd_cell tps65910s
[] = {
34 .name
= "tps65910-gpio",
37 .name
= "tps65910-pmic",
40 .name
= "tps65910-rtc",
41 .num_resources
= ARRAY_SIZE(rtc_resources
),
42 .resources
= &rtc_resources
[0],
45 .name
= "tps65910-power",
50 static const struct regmap_irq tps65911_irqs
[] = {
52 [TPS65911_IRQ_PWRHOLD_F
] = {
53 .mask
= INT_MSK_PWRHOLD_F_IT_MSK_MASK
,
56 [TPS65911_IRQ_VBAT_VMHI
] = {
57 .mask
= INT_MSK_VMBHI_IT_MSK_MASK
,
60 [TPS65911_IRQ_PWRON
] = {
61 .mask
= INT_MSK_PWRON_IT_MSK_MASK
,
64 [TPS65911_IRQ_PWRON_LP
] = {
65 .mask
= INT_MSK_PWRON_LP_IT_MSK_MASK
,
68 [TPS65911_IRQ_PWRHOLD_R
] = {
69 .mask
= INT_MSK_PWRHOLD_R_IT_MSK_MASK
,
72 [TPS65911_IRQ_HOTDIE
] = {
73 .mask
= INT_MSK_HOTDIE_IT_MSK_MASK
,
76 [TPS65911_IRQ_RTC_ALARM
] = {
77 .mask
= INT_MSK_RTC_ALARM_IT_MSK_MASK
,
80 [TPS65911_IRQ_RTC_PERIOD
] = {
81 .mask
= INT_MSK_RTC_PERIOD_IT_MSK_MASK
,
86 [TPS65911_IRQ_GPIO0_R
] = {
87 .mask
= INT_MSK2_GPIO0_R_IT_MSK_MASK
,
90 [TPS65911_IRQ_GPIO0_F
] = {
91 .mask
= INT_MSK2_GPIO0_F_IT_MSK_MASK
,
94 [TPS65911_IRQ_GPIO1_R
] = {
95 .mask
= INT_MSK2_GPIO1_R_IT_MSK_MASK
,
98 [TPS65911_IRQ_GPIO1_F
] = {
99 .mask
= INT_MSK2_GPIO1_F_IT_MSK_MASK
,
102 [TPS65911_IRQ_GPIO2_R
] = {
103 .mask
= INT_MSK2_GPIO2_R_IT_MSK_MASK
,
106 [TPS65911_IRQ_GPIO2_F
] = {
107 .mask
= INT_MSK2_GPIO2_F_IT_MSK_MASK
,
110 [TPS65911_IRQ_GPIO3_R
] = {
111 .mask
= INT_MSK2_GPIO3_R_IT_MSK_MASK
,
114 [TPS65911_IRQ_GPIO3_F
] = {
115 .mask
= INT_MSK2_GPIO3_F_IT_MSK_MASK
,
120 [TPS65911_IRQ_GPIO4_R
] = {
121 .mask
= INT_MSK3_GPIO4_R_IT_MSK_MASK
,
124 [TPS65911_IRQ_GPIO4_F
] = {
125 .mask
= INT_MSK3_GPIO4_F_IT_MSK_MASK
,
128 [TPS65911_IRQ_GPIO5_R
] = {
129 .mask
= INT_MSK3_GPIO5_R_IT_MSK_MASK
,
132 [TPS65911_IRQ_GPIO5_F
] = {
133 .mask
= INT_MSK3_GPIO5_F_IT_MSK_MASK
,
136 [TPS65911_IRQ_WTCHDG
] = {
137 .mask
= INT_MSK3_WTCHDG_IT_MSK_MASK
,
140 [TPS65911_IRQ_VMBCH2_H
] = {
141 .mask
= INT_MSK3_VMBCH2_H_IT_MSK_MASK
,
144 [TPS65911_IRQ_VMBCH2_L
] = {
145 .mask
= INT_MSK3_VMBCH2_L_IT_MSK_MASK
,
148 [TPS65911_IRQ_PWRDN
] = {
149 .mask
= INT_MSK3_PWRDN_IT_MSK_MASK
,
154 static const struct regmap_irq tps65910_irqs
[] = {
156 [TPS65910_IRQ_VBAT_VMBDCH
] = {
157 .mask
= TPS65910_INT_MSK_VMBDCH_IT_MSK_MASK
,
160 [TPS65910_IRQ_VBAT_VMHI
] = {
161 .mask
= TPS65910_INT_MSK_VMBHI_IT_MSK_MASK
,
164 [TPS65910_IRQ_PWRON
] = {
165 .mask
= TPS65910_INT_MSK_PWRON_IT_MSK_MASK
,
168 [TPS65910_IRQ_PWRON_LP
] = {
169 .mask
= TPS65910_INT_MSK_PWRON_LP_IT_MSK_MASK
,
172 [TPS65910_IRQ_PWRHOLD
] = {
173 .mask
= TPS65910_INT_MSK_PWRHOLD_IT_MSK_MASK
,
176 [TPS65910_IRQ_HOTDIE
] = {
177 .mask
= TPS65910_INT_MSK_HOTDIE_IT_MSK_MASK
,
180 [TPS65910_IRQ_RTC_ALARM
] = {
181 .mask
= TPS65910_INT_MSK_RTC_ALARM_IT_MSK_MASK
,
184 [TPS65910_IRQ_RTC_PERIOD
] = {
185 .mask
= TPS65910_INT_MSK_RTC_PERIOD_IT_MSK_MASK
,
190 [TPS65910_IRQ_GPIO_R
] = {
191 .mask
= TPS65910_INT_MSK2_GPIO0_F_IT_MSK_MASK
,
194 [TPS65910_IRQ_GPIO_F
] = {
195 .mask
= TPS65910_INT_MSK2_GPIO0_R_IT_MSK_MASK
,
200 static struct regmap_irq_chip tps65911_irq_chip
= {
202 .irqs
= tps65911_irqs
,
203 .num_irqs
= ARRAY_SIZE(tps65911_irqs
),
206 .status_base
= TPS65910_INT_STS
,
207 .mask_base
= TPS65910_INT_MSK
,
208 .ack_base
= TPS65910_INT_STS
,
211 static struct regmap_irq_chip tps65910_irq_chip
= {
213 .irqs
= tps65910_irqs
,
214 .num_irqs
= ARRAY_SIZE(tps65910_irqs
),
217 .status_base
= TPS65910_INT_STS
,
218 .mask_base
= TPS65910_INT_MSK
,
219 .ack_base
= TPS65910_INT_STS
,
222 static int tps65910_irq_init(struct tps65910
*tps65910
, int irq
,
223 struct tps65910_platform_data
*pdata
)
226 static struct regmap_irq_chip
*tps6591x_irqs_chip
;
229 dev_warn(tps65910
->dev
, "No interrupt support, no core IRQ\n");
234 dev_warn(tps65910
->dev
, "No interrupt support, no pdata\n");
238 switch (tps65910_chip_id(tps65910
)) {
240 tps6591x_irqs_chip
= &tps65910_irq_chip
;
243 tps6591x_irqs_chip
= &tps65911_irq_chip
;
247 tps65910
->chip_irq
= irq
;
248 ret
= devm_regmap_add_irq_chip(tps65910
->dev
, tps65910
->regmap
,
250 IRQF_ONESHOT
, pdata
->irq_base
,
251 tps6591x_irqs_chip
, &tps65910
->irq_data
);
253 dev_warn(tps65910
->dev
, "Failed to add irq_chip %d\n", ret
);
254 tps65910
->chip_irq
= 0;
259 static bool is_volatile_reg(struct device
*dev
, unsigned int reg
)
261 struct tps65910
*tps65910
= dev_get_drvdata(dev
);
264 * Caching all regulator registers.
265 * All regualator register address range is same for
266 * TPS65910 and TPS65911
268 if ((reg
>= TPS65910_VIO
) && (reg
<= TPS65910_VDAC
)) {
269 /* Check for non-existing register */
270 if (tps65910_chip_id(tps65910
) == TPS65910
)
271 if ((reg
== TPS65911_VDDCTRL_OP
) ||
272 (reg
== TPS65911_VDDCTRL_SR
))
279 static const struct regmap_config tps65910_regmap_config
= {
282 .volatile_reg
= is_volatile_reg
,
283 .max_register
= TPS65910_MAX_REGISTER
- 1,
284 .cache_type
= REGCACHE_RBTREE
,
287 static int tps65910_ck32k_init(struct tps65910
*tps65910
,
288 struct tps65910_board
*pmic_pdata
)
292 if (!pmic_pdata
->en_ck32k_xtal
)
295 ret
= regmap_clear_bits(tps65910
->regmap
, TPS65910_DEVCTRL
,
296 DEVCTRL_CK32K_CTRL_MASK
);
298 dev_err(tps65910
->dev
, "clear ck32k_ctrl failed: %d\n", ret
);
305 static int tps65910_sleepinit(struct tps65910
*tps65910
,
306 struct tps65910_board
*pmic_pdata
)
311 if (!pmic_pdata
->en_dev_slp
)
316 /* enabling SLEEP device state */
317 ret
= regmap_set_bits(tps65910
->regmap
, TPS65910_DEVCTRL
,
318 DEVCTRL_DEV_SLP_MASK
);
320 dev_err(dev
, "set dev_slp failed: %d\n", ret
);
324 if (pmic_pdata
->slp_keepon
.therm_keepon
) {
325 ret
= regmap_set_bits(tps65910
->regmap
,
326 TPS65910_SLEEP_KEEP_RES_ON
,
327 SLEEP_KEEP_RES_ON_THERM_KEEPON_MASK
);
329 dev_err(dev
, "set therm_keepon failed: %d\n", ret
);
330 goto disable_dev_slp
;
334 if (pmic_pdata
->slp_keepon
.clkout32k_keepon
) {
335 ret
= regmap_set_bits(tps65910
->regmap
,
336 TPS65910_SLEEP_KEEP_RES_ON
,
337 SLEEP_KEEP_RES_ON_CLKOUT32K_KEEPON_MASK
);
339 dev_err(dev
, "set clkout32k_keepon failed: %d\n", ret
);
340 goto disable_dev_slp
;
344 if (pmic_pdata
->slp_keepon
.i2chs_keepon
) {
345 ret
= regmap_set_bits(tps65910
->regmap
,
346 TPS65910_SLEEP_KEEP_RES_ON
,
347 SLEEP_KEEP_RES_ON_I2CHS_KEEPON_MASK
);
349 dev_err(dev
, "set i2chs_keepon failed: %d\n", ret
);
350 goto disable_dev_slp
;
357 regmap_clear_bits(tps65910
->regmap
, TPS65910_DEVCTRL
,
358 DEVCTRL_DEV_SLP_MASK
);
365 static const struct of_device_id tps65910_of_match
[] = {
366 { .compatible
= "ti,tps65910", .data
= (void *)TPS65910
},
367 { .compatible
= "ti,tps65911", .data
= (void *)TPS65911
},
371 static struct tps65910_board
*tps65910_parse_dt(struct i2c_client
*client
,
372 unsigned long *chip_id
)
374 struct device_node
*np
= client
->dev
.of_node
;
375 struct tps65910_board
*board_info
;
377 const struct of_device_id
*match
;
380 match
= of_match_device(tps65910_of_match
, &client
->dev
);
382 dev_err(&client
->dev
, "Failed to find matching dt id\n");
386 *chip_id
= (unsigned long)match
->data
;
388 board_info
= devm_kzalloc(&client
->dev
, sizeof(*board_info
),
393 ret
= of_property_read_u32(np
, "ti,vmbch-threshold", &prop
);
395 board_info
->vmbch_threshold
= prop
;
397 ret
= of_property_read_u32(np
, "ti,vmbch2-threshold", &prop
);
399 board_info
->vmbch2_threshold
= prop
;
401 prop
= of_property_read_bool(np
, "ti,en-ck32k-xtal");
402 board_info
->en_ck32k_xtal
= prop
;
404 prop
= of_property_read_bool(np
, "ti,sleep-enable");
405 board_info
->en_dev_slp
= prop
;
407 prop
= of_property_read_bool(np
, "ti,sleep-keep-therm");
408 board_info
->slp_keepon
.therm_keepon
= prop
;
410 prop
= of_property_read_bool(np
, "ti,sleep-keep-ck32k");
411 board_info
->slp_keepon
.clkout32k_keepon
= prop
;
413 prop
= of_property_read_bool(np
, "ti,sleep-keep-hsclk");
414 board_info
->slp_keepon
.i2chs_keepon
= prop
;
416 board_info
->irq
= client
->irq
;
417 board_info
->irq_base
= -1;
418 board_info
->pm_off
= of_property_read_bool(np
,
419 "ti,system-power-controller");
425 struct tps65910_board
*tps65910_parse_dt(struct i2c_client
*client
,
426 unsigned long *chip_id
)
432 static struct i2c_client
*tps65910_i2c_client
;
433 static void tps65910_power_off(void)
435 struct tps65910
*tps65910
;
437 tps65910
= dev_get_drvdata(&tps65910_i2c_client
->dev
);
440 * The PWR_OFF bit needs to be set separately, before transitioning
441 * to the OFF state. It enables the "sequential" power-off mode on
442 * TPS65911, it's a NO-OP on TPS65910.
444 if (regmap_set_bits(tps65910
->regmap
, TPS65910_DEVCTRL
,
445 DEVCTRL_PWR_OFF_MASK
) < 0)
448 regmap_update_bits(tps65910
->regmap
, TPS65910_DEVCTRL
,
449 DEVCTRL_DEV_OFF_MASK
| DEVCTRL_DEV_ON_MASK
,
450 DEVCTRL_DEV_OFF_MASK
);
453 static int tps65910_i2c_probe(struct i2c_client
*i2c
,
454 const struct i2c_device_id
*id
)
456 struct tps65910
*tps65910
;
457 struct tps65910_board
*pmic_plat_data
;
458 struct tps65910_board
*of_pmic_plat_data
= NULL
;
459 struct tps65910_platform_data
*init_data
;
460 unsigned long chip_id
= id
->driver_data
;
463 pmic_plat_data
= dev_get_platdata(&i2c
->dev
);
465 if (!pmic_plat_data
&& i2c
->dev
.of_node
) {
466 pmic_plat_data
= tps65910_parse_dt(i2c
, &chip_id
);
467 of_pmic_plat_data
= pmic_plat_data
;
473 init_data
= devm_kzalloc(&i2c
->dev
, sizeof(*init_data
), GFP_KERNEL
);
474 if (init_data
== NULL
)
477 tps65910
= devm_kzalloc(&i2c
->dev
, sizeof(*tps65910
), GFP_KERNEL
);
478 if (tps65910
== NULL
)
481 tps65910
->of_plat_data
= of_pmic_plat_data
;
482 i2c_set_clientdata(i2c
, tps65910
);
483 tps65910
->dev
= &i2c
->dev
;
484 tps65910
->i2c_client
= i2c
;
485 tps65910
->id
= chip_id
;
487 /* Work around silicon erratum SWCZ010: the tps65910 may miss the
488 * first I2C transfer. So issue a dummy transfer before the first
491 i2c_master_send(i2c
, "", 1);
492 tps65910
->regmap
= devm_regmap_init_i2c(i2c
, &tps65910_regmap_config
);
493 if (IS_ERR(tps65910
->regmap
)) {
494 ret
= PTR_ERR(tps65910
->regmap
);
495 dev_err(&i2c
->dev
, "regmap initialization failed: %d\n", ret
);
499 init_data
->irq
= pmic_plat_data
->irq
;
500 init_data
->irq_base
= pmic_plat_data
->irq_base
;
502 tps65910_irq_init(tps65910
, init_data
->irq
, init_data
);
503 tps65910_ck32k_init(tps65910
, pmic_plat_data
);
504 tps65910_sleepinit(tps65910
, pmic_plat_data
);
506 if (pmic_plat_data
->pm_off
&& !pm_power_off
) {
507 tps65910_i2c_client
= i2c
;
508 pm_power_off
= tps65910_power_off
;
511 ret
= devm_mfd_add_devices(tps65910
->dev
, -1,
512 tps65910s
, ARRAY_SIZE(tps65910s
),
514 regmap_irq_get_domain(tps65910
->irq_data
));
516 dev_err(&i2c
->dev
, "mfd_add_devices failed: %d\n", ret
);
523 static const struct i2c_device_id tps65910_i2c_id
[] = {
524 { "tps65910", TPS65910
},
525 { "tps65911", TPS65911
},
529 static struct i2c_driver tps65910_i2c_driver
= {
532 .of_match_table
= of_match_ptr(tps65910_of_match
),
534 .probe
= tps65910_i2c_probe
,
535 .id_table
= tps65910_i2c_id
,
538 static int __init
tps65910_i2c_init(void)
540 return i2c_add_driver(&tps65910_i2c_driver
);
542 /* init early so consumer devices can complete system boot */
543 subsys_initcall(tps65910_i2c_init
);