1 // SPDX-License-Identifier: GPL-2.0-or-later
5 * Copyright 2011-2012 Texas Instruments Inc.
7 * Author: Graeme Gregory <gg@slimlogic.co.uk>
10 #include <linux/module.h>
11 #include <linux/moduleparam.h>
12 #include <linux/init.h>
13 #include <linux/slab.h>
14 #include <linux/i2c.h>
15 #include <linux/interrupt.h>
16 #include <linux/irq.h>
17 #include <linux/regmap.h>
18 #include <linux/err.h>
19 #include <linux/mfd/core.h>
20 #include <linux/mfd/palmas.h>
22 #include <linux/of_platform.h>
24 static const struct regmap_config palmas_regmap_config[PALMAS_NUM_CLIENTS] = {
28 .max_register = PALMAS_BASE_TO_REG(PALMAS_PU_PD_OD_BASE,
29 PALMAS_PRIMARY_SECONDARY_PAD3),
34 .max_register = PALMAS_BASE_TO_REG(PALMAS_GPADC_BASE,
35 PALMAS_GPADC_SMPS_VSEL_MONITORING),
40 .max_register = PALMAS_BASE_TO_REG(PALMAS_TRIM_GPADC_BASE,
45 static const struct regmap_irq tps65917_irqs[] = {
47 [TPS65917_RESERVED1] = {
48 .mask = TPS65917_RESERVED,
50 [TPS65917_PWRON_IRQ] = {
51 .mask = TPS65917_INT1_STATUS_PWRON,
53 [TPS65917_LONG_PRESS_KEY_IRQ] = {
54 .mask = TPS65917_INT1_STATUS_LONG_PRESS_KEY,
56 [TPS65917_RESERVED2] = {
57 .mask = TPS65917_RESERVED,
59 [TPS65917_PWRDOWN_IRQ] = {
60 .mask = TPS65917_INT1_STATUS_PWRDOWN,
62 [TPS65917_HOTDIE_IRQ] = {
63 .mask = TPS65917_INT1_STATUS_HOTDIE,
65 [TPS65917_VSYS_MON_IRQ] = {
66 .mask = TPS65917_INT1_STATUS_VSYS_MON,
68 [TPS65917_RESERVED3] = {
69 .mask = TPS65917_RESERVED,
72 [TPS65917_RESERVED4] = {
73 .mask = TPS65917_RESERVED,
76 [TPS65917_OTP_ERROR_IRQ] = {
77 .mask = TPS65917_INT2_STATUS_OTP_ERROR,
80 [TPS65917_WDT_IRQ] = {
81 .mask = TPS65917_INT2_STATUS_WDT,
84 [TPS65917_RESERVED5] = {
85 .mask = TPS65917_RESERVED,
88 [TPS65917_RESET_IN_IRQ] = {
89 .mask = TPS65917_INT2_STATUS_RESET_IN,
92 [TPS65917_FSD_IRQ] = {
93 .mask = TPS65917_INT2_STATUS_FSD,
96 [TPS65917_SHORT_IRQ] = {
97 .mask = TPS65917_INT2_STATUS_SHORT,
100 [TPS65917_RESERVED6] = {
101 .mask = TPS65917_RESERVED,
105 [TPS65917_GPADC_AUTO_0_IRQ] = {
106 .mask = TPS65917_INT3_STATUS_GPADC_AUTO_0,
109 [TPS65917_GPADC_AUTO_1_IRQ] = {
110 .mask = TPS65917_INT3_STATUS_GPADC_AUTO_1,
113 [TPS65917_GPADC_EOC_SW_IRQ] = {
114 .mask = TPS65917_INT3_STATUS_GPADC_EOC_SW,
117 [TPS65917_RESREVED6] = {
118 .mask = TPS65917_RESERVED6,
121 [TPS65917_RESERVED7] = {
122 .mask = TPS65917_RESERVED,
125 [TPS65917_RESERVED8] = {
126 .mask = TPS65917_RESERVED,
129 [TPS65917_RESERVED9] = {
130 .mask = TPS65917_RESERVED,
133 [TPS65917_VBUS_IRQ] = {
134 .mask = TPS65917_INT3_STATUS_VBUS,
138 [TPS65917_GPIO_0_IRQ] = {
139 .mask = TPS65917_INT4_STATUS_GPIO_0,
142 [TPS65917_GPIO_1_IRQ] = {
143 .mask = TPS65917_INT4_STATUS_GPIO_1,
146 [TPS65917_GPIO_2_IRQ] = {
147 .mask = TPS65917_INT4_STATUS_GPIO_2,
150 [TPS65917_GPIO_3_IRQ] = {
151 .mask = TPS65917_INT4_STATUS_GPIO_3,
154 [TPS65917_GPIO_4_IRQ] = {
155 .mask = TPS65917_INT4_STATUS_GPIO_4,
158 [TPS65917_GPIO_5_IRQ] = {
159 .mask = TPS65917_INT4_STATUS_GPIO_5,
162 [TPS65917_GPIO_6_IRQ] = {
163 .mask = TPS65917_INT4_STATUS_GPIO_6,
166 [TPS65917_RESERVED10] = {
167 .mask = TPS65917_RESERVED10,
172 static const struct regmap_irq palmas_irqs[] = {
174 [PALMAS_CHARG_DET_N_VBUS_OVV_IRQ] = {
175 .mask = PALMAS_INT1_STATUS_CHARG_DET_N_VBUS_OVV,
177 [PALMAS_PWRON_IRQ] = {
178 .mask = PALMAS_INT1_STATUS_PWRON,
180 [PALMAS_LONG_PRESS_KEY_IRQ] = {
181 .mask = PALMAS_INT1_STATUS_LONG_PRESS_KEY,
183 [PALMAS_RPWRON_IRQ] = {
184 .mask = PALMAS_INT1_STATUS_RPWRON,
186 [PALMAS_PWRDOWN_IRQ] = {
187 .mask = PALMAS_INT1_STATUS_PWRDOWN,
189 [PALMAS_HOTDIE_IRQ] = {
190 .mask = PALMAS_INT1_STATUS_HOTDIE,
192 [PALMAS_VSYS_MON_IRQ] = {
193 .mask = PALMAS_INT1_STATUS_VSYS_MON,
195 [PALMAS_VBAT_MON_IRQ] = {
196 .mask = PALMAS_INT1_STATUS_VBAT_MON,
199 [PALMAS_RTC_ALARM_IRQ] = {
200 .mask = PALMAS_INT2_STATUS_RTC_ALARM,
203 [PALMAS_RTC_TIMER_IRQ] = {
204 .mask = PALMAS_INT2_STATUS_RTC_TIMER,
208 .mask = PALMAS_INT2_STATUS_WDT,
211 [PALMAS_BATREMOVAL_IRQ] = {
212 .mask = PALMAS_INT2_STATUS_BATREMOVAL,
215 [PALMAS_RESET_IN_IRQ] = {
216 .mask = PALMAS_INT2_STATUS_RESET_IN,
219 [PALMAS_FBI_BB_IRQ] = {
220 .mask = PALMAS_INT2_STATUS_FBI_BB,
223 [PALMAS_SHORT_IRQ] = {
224 .mask = PALMAS_INT2_STATUS_SHORT,
227 [PALMAS_VAC_ACOK_IRQ] = {
228 .mask = PALMAS_INT2_STATUS_VAC_ACOK,
232 [PALMAS_GPADC_AUTO_0_IRQ] = {
233 .mask = PALMAS_INT3_STATUS_GPADC_AUTO_0,
236 [PALMAS_GPADC_AUTO_1_IRQ] = {
237 .mask = PALMAS_INT3_STATUS_GPADC_AUTO_1,
240 [PALMAS_GPADC_EOC_SW_IRQ] = {
241 .mask = PALMAS_INT3_STATUS_GPADC_EOC_SW,
244 [PALMAS_GPADC_EOC_RT_IRQ] = {
245 .mask = PALMAS_INT3_STATUS_GPADC_EOC_RT,
248 [PALMAS_ID_OTG_IRQ] = {
249 .mask = PALMAS_INT3_STATUS_ID_OTG,
253 .mask = PALMAS_INT3_STATUS_ID,
256 [PALMAS_VBUS_OTG_IRQ] = {
257 .mask = PALMAS_INT3_STATUS_VBUS_OTG,
260 [PALMAS_VBUS_IRQ] = {
261 .mask = PALMAS_INT3_STATUS_VBUS,
265 [PALMAS_GPIO_0_IRQ] = {
266 .mask = PALMAS_INT4_STATUS_GPIO_0,
269 [PALMAS_GPIO_1_IRQ] = {
270 .mask = PALMAS_INT4_STATUS_GPIO_1,
273 [PALMAS_GPIO_2_IRQ] = {
274 .mask = PALMAS_INT4_STATUS_GPIO_2,
277 [PALMAS_GPIO_3_IRQ] = {
278 .mask = PALMAS_INT4_STATUS_GPIO_3,
281 [PALMAS_GPIO_4_IRQ] = {
282 .mask = PALMAS_INT4_STATUS_GPIO_4,
285 [PALMAS_GPIO_5_IRQ] = {
286 .mask = PALMAS_INT4_STATUS_GPIO_5,
289 [PALMAS_GPIO_6_IRQ] = {
290 .mask = PALMAS_INT4_STATUS_GPIO_6,
293 [PALMAS_GPIO_7_IRQ] = {
294 .mask = PALMAS_INT4_STATUS_GPIO_7,
299 static struct regmap_irq_chip palmas_irq_chip = {
302 .num_irqs = ARRAY_SIZE(palmas_irqs),
306 .status_base = PALMAS_BASE_TO_REG(PALMAS_INTERRUPT_BASE,
308 .mask_base = PALMAS_BASE_TO_REG(PALMAS_INTERRUPT_BASE,
312 static struct regmap_irq_chip tps65917_irq_chip = {
314 .irqs = tps65917_irqs,
315 .num_irqs = ARRAY_SIZE(tps65917_irqs),
319 .status_base = PALMAS_BASE_TO_REG(PALMAS_INTERRUPT_BASE,
321 .mask_base = PALMAS_BASE_TO_REG(PALMAS_INTERRUPT_BASE,
325 int palmas_ext_control_req_config(struct palmas *palmas,
326 enum palmas_external_requestor_id id, int ext_ctrl, bool enable)
328 struct palmas_pmic_driver_data *pmic_ddata = palmas->pmic_ddata;
329 int preq_mask_bit = 0;
333 if (!(ext_ctrl & PALMAS_EXT_REQ))
336 if (id >= PALMAS_EXTERNAL_REQSTR_ID_MAX)
339 if (ext_ctrl & PALMAS_EXT_CONTROL_NSLEEP) {
340 reg_add = PALMAS_NSLEEP_RES_ASSIGN;
342 } else if (ext_ctrl & PALMAS_EXT_CONTROL_ENABLE1) {
343 reg_add = PALMAS_ENABLE1_RES_ASSIGN;
345 } else if (ext_ctrl & PALMAS_EXT_CONTROL_ENABLE2) {
346 reg_add = PALMAS_ENABLE2_RES_ASSIGN;
350 bit_pos = pmic_ddata->sleep_req_info[id].bit_pos;
351 reg_add += pmic_ddata->sleep_req_info[id].reg_offset;
353 ret = palmas_update_bits(palmas, PALMAS_RESOURCE_BASE,
354 reg_add, BIT(bit_pos), BIT(bit_pos));
356 ret = palmas_update_bits(palmas, PALMAS_RESOURCE_BASE,
357 reg_add, BIT(bit_pos), 0);
359 dev_err(palmas->dev, "Resource reg 0x%02x update failed %d\n",
364 /* Unmask the PREQ */
365 ret = palmas_update_bits(palmas, PALMAS_PMU_CONTROL_BASE,
366 PALMAS_POWER_CTRL, BIT(preq_mask_bit), 0);
368 dev_err(palmas->dev, "POWER_CTRL register update failed %d\n",
374 EXPORT_SYMBOL_GPL(palmas_ext_control_req_config);
376 static int palmas_set_pdata_irq_flag(struct i2c_client *i2c,
377 struct palmas_platform_data *pdata)
379 struct irq_data *irq_data = irq_get_irq_data(i2c->irq);
381 dev_err(&i2c->dev, "Invalid IRQ: %d\n", i2c->irq);
385 pdata->irq_flags = irqd_get_trigger_type(irq_data);
386 dev_info(&i2c->dev, "Irq flag is 0x%08x\n", pdata->irq_flags);
390 static void palmas_dt_to_pdata(struct i2c_client *i2c,
391 struct palmas_platform_data *pdata)
393 struct device_node *node = i2c->dev.of_node;
397 ret = of_property_read_u32(node, "ti,mux-pad1", &prop);
399 pdata->mux_from_pdata = 1;
403 ret = of_property_read_u32(node, "ti,mux-pad2", &prop);
405 pdata->mux_from_pdata = 1;
409 /* The default for this register is all masked */
410 ret = of_property_read_u32(node, "ti,power-ctrl", &prop);
412 pdata->power_ctrl = prop;
414 pdata->power_ctrl = PALMAS_POWER_CTRL_NSLEEP_MASK |
415 PALMAS_POWER_CTRL_ENABLE1_MASK |
416 PALMAS_POWER_CTRL_ENABLE2_MASK;
418 palmas_set_pdata_irq_flag(i2c, pdata);
420 pdata->pm_off = of_property_read_bool(node,
421 "ti,system-power-controller");
424 static struct palmas *palmas_dev;
425 static void palmas_power_off(void)
430 struct device_node *np = palmas_dev->dev->of_node;
432 if (of_property_read_bool(np, "ti,palmas-override-powerhold")) {
433 addr = PALMAS_BASE_TO_REG(PALMAS_PU_PD_OD_BASE,
434 PALMAS_PRIMARY_SECONDARY_PAD2);
435 slave = PALMAS_BASE_TO_SLAVE(PALMAS_PU_PD_OD_BASE);
437 if (of_device_is_compatible(np, "ti,tps65917"))
439 TPS65917_PRIMARY_SECONDARY_PAD2_GPIO_5_MASK;
442 PALMAS_PRIMARY_SECONDARY_PAD2_GPIO_7_MASK;
444 ret = regmap_update_bits(palmas_dev->regmap[slave], addr,
447 dev_err(palmas_dev->dev,
448 "Unable to write PRIMARY_SECONDARY_PAD2 %d\n",
452 slave = PALMAS_BASE_TO_SLAVE(PALMAS_PMU_CONTROL_BASE);
453 addr = PALMAS_BASE_TO_REG(PALMAS_PMU_CONTROL_BASE, PALMAS_DEV_CTRL);
455 ret = regmap_update_bits(
456 palmas_dev->regmap[slave],
458 PALMAS_DEV_CTRL_DEV_ON,
462 pr_err("%s: Unable to write to DEV_CTRL_DEV_ON: %d\n",
466 static unsigned int palmas_features = PALMAS_PMIC_FEATURE_SMPS10_BOOST;
467 static unsigned int tps659038_features;
469 struct palmas_driver_data {
470 unsigned int *features;
471 struct regmap_irq_chip *irq_chip;
474 static struct palmas_driver_data palmas_data = {
475 .features = &palmas_features,
476 .irq_chip = &palmas_irq_chip,
479 static struct palmas_driver_data tps659038_data = {
480 .features = &tps659038_features,
481 .irq_chip = &palmas_irq_chip,
484 static struct palmas_driver_data tps65917_data = {
485 .features = &tps659038_features,
486 .irq_chip = &tps65917_irq_chip,
489 static const struct of_device_id of_palmas_match_tbl[] = {
491 .compatible = "ti,palmas",
492 .data = &palmas_data,
495 .compatible = "ti,tps659038",
496 .data = &tps659038_data,
499 .compatible = "ti,tps65917",
500 .data = &tps65917_data,
504 MODULE_DEVICE_TABLE(of, of_palmas_match_tbl);
506 static int palmas_i2c_probe(struct i2c_client *i2c)
508 struct palmas *palmas;
509 struct palmas_platform_data *pdata;
510 struct palmas_driver_data *driver_data;
511 struct device_node *node = i2c->dev.of_node;
513 unsigned int reg, addr;
516 pdata = dev_get_platdata(&i2c->dev);
518 if (node && !pdata) {
519 pdata = devm_kzalloc(&i2c->dev, sizeof(*pdata), GFP_KERNEL);
524 palmas_dt_to_pdata(i2c, pdata);
530 palmas = devm_kzalloc(&i2c->dev, sizeof(struct palmas), GFP_KERNEL);
534 i2c_set_clientdata(i2c, palmas);
535 palmas->dev = &i2c->dev;
536 palmas->irq = i2c->irq;
538 driver_data = (struct palmas_driver_data *) device_get_match_data(&i2c->dev);
539 palmas->features = *driver_data->features;
541 for (i = 0; i < PALMAS_NUM_CLIENTS; i++) {
543 palmas->i2c_clients[i] = i2c;
545 palmas->i2c_clients[i] =
546 i2c_new_dummy_device(i2c->adapter,
548 if (IS_ERR(palmas->i2c_clients[i])) {
550 "can't attach client %d\n", i);
551 ret = PTR_ERR(palmas->i2c_clients[i]);
554 palmas->i2c_clients[i]->dev.of_node = of_node_get(node);
556 palmas->regmap[i] = devm_regmap_init_i2c(palmas->i2c_clients[i],
557 &palmas_regmap_config[i]);
558 if (IS_ERR(palmas->regmap[i])) {
559 ret = PTR_ERR(palmas->regmap[i]);
561 "Failed to allocate regmap %d, err: %d\n",
568 dev_warn(palmas->dev, "IRQ missing: skipping irq request\n");
572 /* Change interrupt line output polarity */
573 if (pdata->irq_flags & IRQ_TYPE_LEVEL_HIGH)
574 reg = PALMAS_POLARITY_CTRL_INT_POLARITY;
577 ret = palmas_update_bits(palmas, PALMAS_PU_PD_OD_BASE,
578 PALMAS_POLARITY_CTRL, PALMAS_POLARITY_CTRL_INT_POLARITY,
581 dev_err(palmas->dev, "POLARITY_CTRL update failed: %d\n", ret);
585 /* Change IRQ into clear on read mode for efficiency */
586 slave = PALMAS_BASE_TO_SLAVE(PALMAS_INTERRUPT_BASE);
587 addr = PALMAS_BASE_TO_REG(PALMAS_INTERRUPT_BASE, PALMAS_INT_CTRL);
588 reg = PALMAS_INT_CTRL_INT_CLEAR;
590 regmap_write(palmas->regmap[slave], addr, reg);
592 ret = regmap_add_irq_chip(palmas->regmap[slave], palmas->irq,
593 IRQF_ONESHOT | pdata->irq_flags, 0,
594 driver_data->irq_chip, &palmas->irq_data);
599 slave = PALMAS_BASE_TO_SLAVE(PALMAS_PU_PD_OD_BASE);
600 addr = PALMAS_BASE_TO_REG(PALMAS_PU_PD_OD_BASE,
601 PALMAS_PRIMARY_SECONDARY_PAD1);
603 if (pdata->mux_from_pdata) {
605 ret = regmap_write(palmas->regmap[slave], addr, reg);
609 ret = regmap_read(palmas->regmap[slave], addr, ®);
614 if (!(reg & PALMAS_PRIMARY_SECONDARY_PAD1_GPIO_0))
615 palmas->gpio_muxed |= PALMAS_GPIO_0_MUXED;
616 if (!(reg & PALMAS_PRIMARY_SECONDARY_PAD1_GPIO_1_MASK))
617 palmas->gpio_muxed |= PALMAS_GPIO_1_MUXED;
618 else if ((reg & PALMAS_PRIMARY_SECONDARY_PAD1_GPIO_1_MASK) ==
619 (2 << PALMAS_PRIMARY_SECONDARY_PAD1_GPIO_1_SHIFT))
620 palmas->led_muxed |= PALMAS_LED1_MUXED;
621 else if ((reg & PALMAS_PRIMARY_SECONDARY_PAD1_GPIO_1_MASK) ==
622 (3 << PALMAS_PRIMARY_SECONDARY_PAD1_GPIO_1_SHIFT))
623 palmas->pwm_muxed |= PALMAS_PWM1_MUXED;
624 if (!(reg & PALMAS_PRIMARY_SECONDARY_PAD1_GPIO_2_MASK))
625 palmas->gpio_muxed |= PALMAS_GPIO_2_MUXED;
626 else if ((reg & PALMAS_PRIMARY_SECONDARY_PAD1_GPIO_2_MASK) ==
627 (2 << PALMAS_PRIMARY_SECONDARY_PAD1_GPIO_2_SHIFT))
628 palmas->led_muxed |= PALMAS_LED2_MUXED;
629 else if ((reg & PALMAS_PRIMARY_SECONDARY_PAD1_GPIO_2_MASK) ==
630 (3 << PALMAS_PRIMARY_SECONDARY_PAD1_GPIO_2_SHIFT))
631 palmas->pwm_muxed |= PALMAS_PWM2_MUXED;
632 if (!(reg & PALMAS_PRIMARY_SECONDARY_PAD1_GPIO_3))
633 palmas->gpio_muxed |= PALMAS_GPIO_3_MUXED;
635 addr = PALMAS_BASE_TO_REG(PALMAS_PU_PD_OD_BASE,
636 PALMAS_PRIMARY_SECONDARY_PAD2);
638 if (pdata->mux_from_pdata) {
640 ret = regmap_write(palmas->regmap[slave], addr, reg);
644 ret = regmap_read(palmas->regmap[slave], addr, ®);
649 if (!(reg & PALMAS_PRIMARY_SECONDARY_PAD2_GPIO_4))
650 palmas->gpio_muxed |= PALMAS_GPIO_4_MUXED;
651 if (!(reg & PALMAS_PRIMARY_SECONDARY_PAD2_GPIO_5_MASK))
652 palmas->gpio_muxed |= PALMAS_GPIO_5_MUXED;
653 if (!(reg & PALMAS_PRIMARY_SECONDARY_PAD2_GPIO_6))
654 palmas->gpio_muxed |= PALMAS_GPIO_6_MUXED;
655 if (!(reg & PALMAS_PRIMARY_SECONDARY_PAD2_GPIO_7_MASK))
656 palmas->gpio_muxed |= PALMAS_GPIO_7_MUXED;
658 dev_info(palmas->dev, "Muxing GPIO %x, PWM %x, LED %x\n",
659 palmas->gpio_muxed, palmas->pwm_muxed,
662 reg = pdata->power_ctrl;
664 slave = PALMAS_BASE_TO_SLAVE(PALMAS_PMU_CONTROL_BASE);
665 addr = PALMAS_BASE_TO_REG(PALMAS_PMU_CONTROL_BASE, PALMAS_POWER_CTRL);
667 ret = regmap_write(palmas->regmap[slave], addr, reg);
672 * If we are probing with DT do this the DT way and return here
673 * otherwise continue and add devices using mfd helpers.
676 ret = devm_of_platform_populate(&i2c->dev);
679 } else if (pdata->pm_off && !pm_power_off) {
681 pm_power_off = palmas_power_off;
688 regmap_del_irq_chip(palmas->irq, palmas->irq_data);
690 for (i = 1; i < PALMAS_NUM_CLIENTS; i++) {
691 if (palmas->i2c_clients[i])
692 i2c_unregister_device(palmas->i2c_clients[i]);
697 static void palmas_i2c_remove(struct i2c_client *i2c)
699 struct palmas *palmas = i2c_get_clientdata(i2c);
702 regmap_del_irq_chip(palmas->irq, palmas->irq_data);
704 for (i = 1; i < PALMAS_NUM_CLIENTS; i++) {
705 if (palmas->i2c_clients[i])
706 i2c_unregister_device(palmas->i2c_clients[i]);
709 if (palmas == palmas_dev) {
715 static const struct i2c_device_id palmas_i2c_id[] = {
722 MODULE_DEVICE_TABLE(i2c, palmas_i2c_id);
724 static struct i2c_driver palmas_i2c_driver = {
727 .of_match_table = of_palmas_match_tbl,
729 .probe = palmas_i2c_probe,
730 .remove = palmas_i2c_remove,
731 .id_table = palmas_i2c_id,
734 static int __init palmas_i2c_init(void)
736 return i2c_add_driver(&palmas_i2c_driver);
738 /* init early so consumer devices can complete system boot */
739 subsys_initcall(palmas_i2c_init);
741 static void __exit palmas_i2c_exit(void)
743 i2c_del_driver(&palmas_i2c_driver);
745 module_exit(palmas_i2c_exit);
747 MODULE_AUTHOR("Graeme Gregory <gg@slimlogic.co.uk>");
748 MODULE_DESCRIPTION("Palmas chip family multi-function driver");
749 MODULE_LICENSE("GPL");