2 * tps65910.c -- TI TPS6591x
4 * Copyright 2010 Texas Instruments Inc.
6 * Author: Graeme Gregory <gg@slimlogic.co.uk>
7 * Author: Jorge Eduardo Candelaria <jedu@slimlogic.co.uk>
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the
11 * Free Software Foundation; either version 2 of the License, or (at your
12 * option) any later version.
16 #include <linux/module.h>
17 #include <linux/moduleparam.h>
18 #include <linux/init.h>
19 #include <linux/err.h>
20 #include <linux/slab.h>
21 #include <linux/i2c.h>
22 #include <linux/interrupt.h>
23 #include <linux/irq.h>
24 #include <linux/irqdomain.h>
25 #include <linux/mfd/core.h>
26 #include <linux/regmap.h>
27 #include <linux/mfd/tps65910.h>
29 #include <linux/of_device.h>
31 static struct resource rtc_resources
[] = {
33 .start
= TPS65910_IRQ_RTC_ALARM
,
34 .end
= TPS65910_IRQ_RTC_ALARM
,
35 .flags
= IORESOURCE_IRQ
,
39 static const struct mfd_cell tps65910s
[] = {
41 .name
= "tps65910-gpio",
44 .name
= "tps65910-pmic",
47 .name
= "tps65910-rtc",
48 .num_resources
= ARRAY_SIZE(rtc_resources
),
49 .resources
= &rtc_resources
[0],
52 .name
= "tps65910-power",
57 static const struct regmap_irq tps65911_irqs
[] = {
59 [TPS65911_IRQ_PWRHOLD_F
] = {
60 .mask
= INT_MSK_PWRHOLD_F_IT_MSK_MASK
,
63 [TPS65911_IRQ_VBAT_VMHI
] = {
64 .mask
= INT_MSK_VMBHI_IT_MSK_MASK
,
67 [TPS65911_IRQ_PWRON
] = {
68 .mask
= INT_MSK_PWRON_IT_MSK_MASK
,
71 [TPS65911_IRQ_PWRON_LP
] = {
72 .mask
= INT_MSK_PWRON_LP_IT_MSK_MASK
,
75 [TPS65911_IRQ_PWRHOLD_R
] = {
76 .mask
= INT_MSK_PWRHOLD_R_IT_MSK_MASK
,
79 [TPS65911_IRQ_HOTDIE
] = {
80 .mask
= INT_MSK_HOTDIE_IT_MSK_MASK
,
83 [TPS65911_IRQ_RTC_ALARM
] = {
84 .mask
= INT_MSK_RTC_ALARM_IT_MSK_MASK
,
87 [TPS65911_IRQ_RTC_PERIOD
] = {
88 .mask
= INT_MSK_RTC_PERIOD_IT_MSK_MASK
,
93 [TPS65911_IRQ_GPIO0_R
] = {
94 .mask
= INT_MSK2_GPIO0_R_IT_MSK_MASK
,
97 [TPS65911_IRQ_GPIO0_F
] = {
98 .mask
= INT_MSK2_GPIO0_F_IT_MSK_MASK
,
101 [TPS65911_IRQ_GPIO1_R
] = {
102 .mask
= INT_MSK2_GPIO1_R_IT_MSK_MASK
,
105 [TPS65911_IRQ_GPIO1_F
] = {
106 .mask
= INT_MSK2_GPIO1_F_IT_MSK_MASK
,
109 [TPS65911_IRQ_GPIO2_R
] = {
110 .mask
= INT_MSK2_GPIO2_R_IT_MSK_MASK
,
113 [TPS65911_IRQ_GPIO2_F
] = {
114 .mask
= INT_MSK2_GPIO2_F_IT_MSK_MASK
,
117 [TPS65911_IRQ_GPIO3_R
] = {
118 .mask
= INT_MSK2_GPIO3_R_IT_MSK_MASK
,
121 [TPS65911_IRQ_GPIO3_F
] = {
122 .mask
= INT_MSK2_GPIO3_F_IT_MSK_MASK
,
127 [TPS65911_IRQ_GPIO4_R
] = {
128 .mask
= INT_MSK3_GPIO4_R_IT_MSK_MASK
,
131 [TPS65911_IRQ_GPIO4_F
] = {
132 .mask
= INT_MSK3_GPIO4_F_IT_MSK_MASK
,
135 [TPS65911_IRQ_GPIO5_R
] = {
136 .mask
= INT_MSK3_GPIO5_R_IT_MSK_MASK
,
139 [TPS65911_IRQ_GPIO5_F
] = {
140 .mask
= INT_MSK3_GPIO5_F_IT_MSK_MASK
,
143 [TPS65911_IRQ_WTCHDG
] = {
144 .mask
= INT_MSK3_WTCHDG_IT_MSK_MASK
,
147 [TPS65911_IRQ_VMBCH2_H
] = {
148 .mask
= INT_MSK3_VMBCH2_H_IT_MSK_MASK
,
151 [TPS65911_IRQ_VMBCH2_L
] = {
152 .mask
= INT_MSK3_VMBCH2_L_IT_MSK_MASK
,
155 [TPS65911_IRQ_PWRDN
] = {
156 .mask
= INT_MSK3_PWRDN_IT_MSK_MASK
,
161 static const struct regmap_irq tps65910_irqs
[] = {
163 [TPS65910_IRQ_VBAT_VMBDCH
] = {
164 .mask
= TPS65910_INT_MSK_VMBDCH_IT_MSK_MASK
,
167 [TPS65910_IRQ_VBAT_VMHI
] = {
168 .mask
= TPS65910_INT_MSK_VMBHI_IT_MSK_MASK
,
171 [TPS65910_IRQ_PWRON
] = {
172 .mask
= TPS65910_INT_MSK_PWRON_IT_MSK_MASK
,
175 [TPS65910_IRQ_PWRON_LP
] = {
176 .mask
= TPS65910_INT_MSK_PWRON_LP_IT_MSK_MASK
,
179 [TPS65910_IRQ_PWRHOLD
] = {
180 .mask
= TPS65910_INT_MSK_PWRHOLD_IT_MSK_MASK
,
183 [TPS65910_IRQ_HOTDIE
] = {
184 .mask
= TPS65910_INT_MSK_HOTDIE_IT_MSK_MASK
,
187 [TPS65910_IRQ_RTC_ALARM
] = {
188 .mask
= TPS65910_INT_MSK_RTC_ALARM_IT_MSK_MASK
,
191 [TPS65910_IRQ_RTC_PERIOD
] = {
192 .mask
= TPS65910_INT_MSK_RTC_PERIOD_IT_MSK_MASK
,
197 [TPS65910_IRQ_GPIO_R
] = {
198 .mask
= TPS65910_INT_MSK2_GPIO0_F_IT_MSK_MASK
,
201 [TPS65910_IRQ_GPIO_F
] = {
202 .mask
= TPS65910_INT_MSK2_GPIO0_R_IT_MSK_MASK
,
207 static struct regmap_irq_chip tps65911_irq_chip
= {
209 .irqs
= tps65911_irqs
,
210 .num_irqs
= ARRAY_SIZE(tps65911_irqs
),
213 .status_base
= TPS65910_INT_STS
,
214 .mask_base
= TPS65910_INT_MSK
,
215 .ack_base
= TPS65910_INT_STS
,
218 static struct regmap_irq_chip tps65910_irq_chip
= {
220 .irqs
= tps65910_irqs
,
221 .num_irqs
= ARRAY_SIZE(tps65910_irqs
),
224 .status_base
= TPS65910_INT_STS
,
225 .mask_base
= TPS65910_INT_MSK
,
226 .ack_base
= TPS65910_INT_STS
,
229 static int tps65910_irq_init(struct tps65910
*tps65910
, int irq
,
230 struct tps65910_platform_data
*pdata
)
233 static struct regmap_irq_chip
*tps6591x_irqs_chip
;
236 dev_warn(tps65910
->dev
, "No interrupt support, no core IRQ\n");
241 dev_warn(tps65910
->dev
, "No interrupt support, no pdata\n");
245 switch (tps65910_chip_id(tps65910
)) {
247 tps6591x_irqs_chip
= &tps65910_irq_chip
;
250 tps6591x_irqs_chip
= &tps65911_irq_chip
;
254 tps65910
->chip_irq
= irq
;
255 ret
= regmap_add_irq_chip(tps65910
->regmap
, tps65910
->chip_irq
,
256 IRQF_ONESHOT
, pdata
->irq_base
,
257 tps6591x_irqs_chip
, &tps65910
->irq_data
);
259 dev_warn(tps65910
->dev
, "Failed to add irq_chip %d\n", ret
);
260 tps65910
->chip_irq
= 0;
265 static int tps65910_irq_exit(struct tps65910
*tps65910
)
267 if (tps65910
->chip_irq
> 0)
268 regmap_del_irq_chip(tps65910
->chip_irq
, tps65910
->irq_data
);
272 static bool is_volatile_reg(struct device
*dev
, unsigned int reg
)
274 struct tps65910
*tps65910
= dev_get_drvdata(dev
);
277 * Caching all regulator registers.
278 * All regualator register address range is same for
279 * TPS65910 and TPS65911
281 if ((reg
>= TPS65910_VIO
) && (reg
<= TPS65910_VDAC
)) {
282 /* Check for non-existing register */
283 if (tps65910_chip_id(tps65910
) == TPS65910
)
284 if ((reg
== TPS65911_VDDCTRL_OP
) ||
285 (reg
== TPS65911_VDDCTRL_SR
))
292 static const struct regmap_config tps65910_regmap_config
= {
295 .volatile_reg
= is_volatile_reg
,
296 .max_register
= TPS65910_MAX_REGISTER
- 1,
297 .cache_type
= REGCACHE_RBTREE
,
300 static int tps65910_ck32k_init(struct tps65910
*tps65910
,
301 struct tps65910_board
*pmic_pdata
)
305 if (!pmic_pdata
->en_ck32k_xtal
)
308 ret
= tps65910_reg_clear_bits(tps65910
, TPS65910_DEVCTRL
,
309 DEVCTRL_CK32K_CTRL_MASK
);
311 dev_err(tps65910
->dev
, "clear ck32k_ctrl failed: %d\n", ret
);
318 static int tps65910_sleepinit(struct tps65910
*tps65910
,
319 struct tps65910_board
*pmic_pdata
)
321 struct device
*dev
= NULL
;
326 if (!pmic_pdata
->en_dev_slp
)
329 /* enabling SLEEP device state */
330 ret
= tps65910_reg_set_bits(tps65910
, TPS65910_DEVCTRL
,
331 DEVCTRL_DEV_SLP_MASK
);
333 dev_err(dev
, "set dev_slp failed: %d\n", ret
);
337 /* Return if there is no sleep keepon data. */
338 if (!pmic_pdata
->slp_keepon
)
341 if (pmic_pdata
->slp_keepon
->therm_keepon
) {
342 ret
= tps65910_reg_set_bits(tps65910
,
343 TPS65910_SLEEP_KEEP_RES_ON
,
344 SLEEP_KEEP_RES_ON_THERM_KEEPON_MASK
);
346 dev_err(dev
, "set therm_keepon failed: %d\n", ret
);
347 goto disable_dev_slp
;
351 if (pmic_pdata
->slp_keepon
->clkout32k_keepon
) {
352 ret
= tps65910_reg_set_bits(tps65910
,
353 TPS65910_SLEEP_KEEP_RES_ON
,
354 SLEEP_KEEP_RES_ON_CLKOUT32K_KEEPON_MASK
);
356 dev_err(dev
, "set clkout32k_keepon failed: %d\n", ret
);
357 goto disable_dev_slp
;
361 if (pmic_pdata
->slp_keepon
->i2chs_keepon
) {
362 ret
= tps65910_reg_set_bits(tps65910
,
363 TPS65910_SLEEP_KEEP_RES_ON
,
364 SLEEP_KEEP_RES_ON_I2CHS_KEEPON_MASK
);
366 dev_err(dev
, "set i2chs_keepon failed: %d\n", ret
);
367 goto disable_dev_slp
;
374 tps65910_reg_clear_bits(tps65910
, TPS65910_DEVCTRL
,
375 DEVCTRL_DEV_SLP_MASK
);
382 static const struct of_device_id tps65910_of_match
[] = {
383 { .compatible
= "ti,tps65910", .data
= (void *)TPS65910
},
384 { .compatible
= "ti,tps65911", .data
= (void *)TPS65911
},
387 MODULE_DEVICE_TABLE(of
, tps65910_of_match
);
389 static struct tps65910_board
*tps65910_parse_dt(struct i2c_client
*client
,
390 unsigned long *chip_id
)
392 struct device_node
*np
= client
->dev
.of_node
;
393 struct tps65910_board
*board_info
;
395 const struct of_device_id
*match
;
398 match
= of_match_device(tps65910_of_match
, &client
->dev
);
400 dev_err(&client
->dev
, "Failed to find matching dt id\n");
404 *chip_id
= (unsigned long)match
->data
;
406 board_info
= devm_kzalloc(&client
->dev
, sizeof(*board_info
),
409 dev_err(&client
->dev
, "Failed to allocate pdata\n");
413 ret
= of_property_read_u32(np
, "ti,vmbch-threshold", &prop
);
415 board_info
->vmbch_threshold
= prop
;
417 ret
= of_property_read_u32(np
, "ti,vmbch2-threshold", &prop
);
419 board_info
->vmbch2_threshold
= prop
;
421 prop
= of_property_read_bool(np
, "ti,en-ck32k-xtal");
422 board_info
->en_ck32k_xtal
= prop
;
424 board_info
->irq
= client
->irq
;
425 board_info
->irq_base
= -1;
426 board_info
->pm_off
= of_property_read_bool(np
,
427 "ti,system-power-controller");
433 struct tps65910_board
*tps65910_parse_dt(struct i2c_client
*client
,
434 unsigned long *chip_id
)
440 static struct i2c_client
*tps65910_i2c_client
;
441 static void tps65910_power_off(void)
443 struct tps65910
*tps65910
;
445 tps65910
= dev_get_drvdata(&tps65910_i2c_client
->dev
);
447 if (tps65910_reg_set_bits(tps65910
, TPS65910_DEVCTRL
,
448 DEVCTRL_PWR_OFF_MASK
) < 0)
451 tps65910_reg_clear_bits(tps65910
, TPS65910_DEVCTRL
,
452 DEVCTRL_DEV_ON_MASK
);
455 static int tps65910_i2c_probe(struct i2c_client
*i2c
,
456 const struct i2c_device_id
*id
)
458 struct tps65910
*tps65910
;
459 struct tps65910_board
*pmic_plat_data
;
460 struct tps65910_board
*of_pmic_plat_data
= NULL
;
461 struct tps65910_platform_data
*init_data
;
462 unsigned long chip_id
= id
->driver_data
;
465 pmic_plat_data
= dev_get_platdata(&i2c
->dev
);
467 if (!pmic_plat_data
&& i2c
->dev
.of_node
) {
468 pmic_plat_data
= tps65910_parse_dt(i2c
, &chip_id
);
469 of_pmic_plat_data
= pmic_plat_data
;
475 init_data
= devm_kzalloc(&i2c
->dev
, sizeof(*init_data
), GFP_KERNEL
);
476 if (init_data
== NULL
)
479 tps65910
= devm_kzalloc(&i2c
->dev
, sizeof(*tps65910
), GFP_KERNEL
);
480 if (tps65910
== NULL
)
483 tps65910
->of_plat_data
= of_pmic_plat_data
;
484 i2c_set_clientdata(i2c
, tps65910
);
485 tps65910
->dev
= &i2c
->dev
;
486 tps65910
->i2c_client
= i2c
;
487 tps65910
->id
= chip_id
;
489 /* Work around silicon erratum SWCZ010: the tps65910 may miss the
490 * first I2C transfer. So issue a dummy transfer before the first
493 i2c_master_send(i2c
, "", 1);
494 tps65910
->regmap
= devm_regmap_init_i2c(i2c
, &tps65910_regmap_config
);
495 if (IS_ERR(tps65910
->regmap
)) {
496 ret
= PTR_ERR(tps65910
->regmap
);
497 dev_err(&i2c
->dev
, "regmap initialization failed: %d\n", ret
);
501 init_data
->irq
= pmic_plat_data
->irq
;
502 init_data
->irq_base
= pmic_plat_data
->irq_base
;
504 tps65910_irq_init(tps65910
, init_data
->irq
, init_data
);
505 tps65910_ck32k_init(tps65910
, pmic_plat_data
);
506 tps65910_sleepinit(tps65910
, pmic_plat_data
);
508 if (pmic_plat_data
->pm_off
&& !pm_power_off
) {
509 tps65910_i2c_client
= i2c
;
510 pm_power_off
= tps65910_power_off
;
513 ret
= mfd_add_devices(tps65910
->dev
, -1,
514 tps65910s
, ARRAY_SIZE(tps65910s
),
516 regmap_irq_get_domain(tps65910
->irq_data
));
518 dev_err(&i2c
->dev
, "mfd_add_devices failed: %d\n", ret
);
519 tps65910_irq_exit(tps65910
);
526 static int tps65910_i2c_remove(struct i2c_client
*i2c
)
528 struct tps65910
*tps65910
= i2c_get_clientdata(i2c
);
530 tps65910_irq_exit(tps65910
);
531 mfd_remove_devices(tps65910
->dev
);
536 static const struct i2c_device_id tps65910_i2c_id
[] = {
537 { "tps65910", TPS65910
},
538 { "tps65911", TPS65911
},
541 MODULE_DEVICE_TABLE(i2c
, tps65910_i2c_id
);
544 static struct i2c_driver tps65910_i2c_driver
= {
547 .owner
= THIS_MODULE
,
548 .of_match_table
= of_match_ptr(tps65910_of_match
),
550 .probe
= tps65910_i2c_probe
,
551 .remove
= tps65910_i2c_remove
,
552 .id_table
= tps65910_i2c_id
,
555 static int __init
tps65910_i2c_init(void)
557 return i2c_add_driver(&tps65910_i2c_driver
);
559 /* init early so consumer devices can complete system boot */
560 subsys_initcall(tps65910_i2c_init
);
562 static void __exit
tps65910_i2c_exit(void)
564 i2c_del_driver(&tps65910_i2c_driver
);
566 module_exit(tps65910_i2c_exit
);
568 MODULE_AUTHOR("Graeme Gregory <gg@slimlogic.co.uk>");
569 MODULE_AUTHOR("Jorge Eduardo Candelaria <jedu@slimlogic.co.uk>");
570 MODULE_DESCRIPTION("TPS6591x chip family multi-function driver");
571 MODULE_LICENSE("GPL");