1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2021, The Linux Foundation. All rights reserved.
6 #include <linux/bitops.h>
8 #include <linux/interrupt.h>
10 #include <linux/irqdomain.h>
11 #include <linux/module.h>
12 #include <linux/of_device.h>
13 #include <linux/of_platform.h>
14 #include <linux/pinctrl/consumer.h>
15 #include <linux/regmap.h>
16 #include <linux/slab.h>
18 #include <dt-bindings/mfd/qcom-pm8008.h>
20 #define I2C_INTR_STATUS_BASE 0x0550
21 #define INT_RT_STS_OFFSET 0x10
22 #define INT_SET_TYPE_OFFSET 0x11
23 #define INT_POL_HIGH_OFFSET 0x12
24 #define INT_POL_LOW_OFFSET 0x13
25 #define INT_LATCHED_CLR_OFFSET 0x14
26 #define INT_EN_SET_OFFSET 0x15
27 #define INT_EN_CLR_OFFSET 0x16
28 #define INT_LATCHED_STS_OFFSET 0x18
38 #define PM8008_PERIPH_0_BASE 0x900
39 #define PM8008_PERIPH_1_BASE 0x2400
40 #define PM8008_PERIPH_2_BASE 0xC000
41 #define PM8008_PERIPH_3_BASE 0xC100
43 #define PM8008_TEMP_ALARM_ADDR PM8008_PERIPH_1_BASE
44 #define PM8008_GPIO1_ADDR PM8008_PERIPH_2_BASE
45 #define PM8008_GPIO2_ADDR PM8008_PERIPH_3_BASE
47 #define PM8008_STATUS_BASE (PM8008_PERIPH_0_BASE | INT_LATCHED_STS_OFFSET)
48 #define PM8008_MASK_BASE (PM8008_PERIPH_0_BASE | INT_EN_SET_OFFSET)
49 #define PM8008_UNMASK_BASE (PM8008_PERIPH_0_BASE | INT_EN_CLR_OFFSET)
50 #define PM8008_TYPE_BASE (PM8008_PERIPH_0_BASE | INT_SET_TYPE_OFFSET)
51 #define PM8008_ACK_BASE (PM8008_PERIPH_0_BASE | INT_LATCHED_CLR_OFFSET)
52 #define PM8008_POLARITY_HI_BASE (PM8008_PERIPH_0_BASE | INT_POL_HIGH_OFFSET)
53 #define PM8008_POLARITY_LO_BASE (PM8008_PERIPH_0_BASE | INT_POL_LOW_OFFSET)
55 #define PM8008_PERIPH_OFFSET(paddr) (paddr - PM8008_PERIPH_0_BASE)
57 static unsigned int p0_offs[] = {PM8008_PERIPH_OFFSET(PM8008_PERIPH_0_BASE)};
58 static unsigned int p1_offs[] = {PM8008_PERIPH_OFFSET(PM8008_PERIPH_1_BASE)};
59 static unsigned int p2_offs[] = {PM8008_PERIPH_OFFSET(PM8008_PERIPH_2_BASE)};
60 static unsigned int p3_offs[] = {PM8008_PERIPH_OFFSET(PM8008_PERIPH_3_BASE)};
62 static struct regmap_irq_sub_irq_map pm8008_sub_reg_offsets[] = {
63 REGMAP_IRQ_MAIN_REG_OFFSET(p0_offs),
64 REGMAP_IRQ_MAIN_REG_OFFSET(p1_offs),
65 REGMAP_IRQ_MAIN_REG_OFFSET(p2_offs),
66 REGMAP_IRQ_MAIN_REG_OFFSET(p3_offs),
69 static unsigned int pm8008_virt_regs[] = {
70 PM8008_POLARITY_HI_BASE,
71 PM8008_POLARITY_LO_BASE,
80 static struct regmap_irq pm8008_irqs[] = {
81 REGMAP_IRQ_REG(PM8008_IRQ_MISC_UVLO, PM8008_MISC, BIT(0)),
82 REGMAP_IRQ_REG(PM8008_IRQ_MISC_OVLO, PM8008_MISC, BIT(1)),
83 REGMAP_IRQ_REG(PM8008_IRQ_MISC_OTST2, PM8008_MISC, BIT(2)),
84 REGMAP_IRQ_REG(PM8008_IRQ_MISC_OTST3, PM8008_MISC, BIT(3)),
85 REGMAP_IRQ_REG(PM8008_IRQ_MISC_LDO_OCP, PM8008_MISC, BIT(4)),
86 REGMAP_IRQ_REG(PM8008_IRQ_TEMP_ALARM, PM8008_TEMP_ALARM, BIT(0)),
87 REGMAP_IRQ_REG(PM8008_IRQ_GPIO1, PM8008_GPIO1, BIT(0)),
88 REGMAP_IRQ_REG(PM8008_IRQ_GPIO2, PM8008_GPIO2, BIT(0)),
91 static int pm8008_set_type_virt(unsigned int **virt_buf,
92 unsigned int type, unsigned long hwirq,
96 case IRQ_TYPE_EDGE_FALLING:
97 case IRQ_TYPE_LEVEL_LOW:
98 virt_buf[POLARITY_HI_INDEX][reg] &= ~pm8008_irqs[hwirq].mask;
99 virt_buf[POLARITY_LO_INDEX][reg] |= pm8008_irqs[hwirq].mask;
102 case IRQ_TYPE_EDGE_RISING:
103 case IRQ_TYPE_LEVEL_HIGH:
104 virt_buf[POLARITY_HI_INDEX][reg] |= pm8008_irqs[hwirq].mask;
105 virt_buf[POLARITY_LO_INDEX][reg] &= ~pm8008_irqs[hwirq].mask;
108 case IRQ_TYPE_EDGE_BOTH:
109 virt_buf[POLARITY_HI_INDEX][reg] |= pm8008_irqs[hwirq].mask;
110 virt_buf[POLARITY_LO_INDEX][reg] |= pm8008_irqs[hwirq].mask;
120 static struct regmap_irq_chip pm8008_irq_chip = {
121 .name = "pm8008_irq",
122 .main_status = I2C_INTR_STATUS_BASE,
124 .num_virt_regs = PM8008_NUM_VIRT_REGS,
126 .num_irqs = ARRAY_SIZE(pm8008_irqs),
127 .num_regs = PM8008_NUM_PERIPHS,
128 .not_fixed_stride = true,
129 .sub_reg_offsets = pm8008_sub_reg_offsets,
130 .set_type_virt = pm8008_set_type_virt,
131 .status_base = PM8008_STATUS_BASE,
132 .mask_base = PM8008_MASK_BASE,
133 .unmask_base = PM8008_UNMASK_BASE,
134 .type_base = PM8008_TYPE_BASE,
135 .ack_base = PM8008_ACK_BASE,
136 .virt_reg_base = pm8008_virt_regs,
137 .num_type_reg = PM8008_NUM_PERIPHS,
140 static struct regmap_config qcom_mfd_regmap_cfg = {
143 .max_register = 0xFFFF,
146 static int pm8008_init(struct regmap *regmap)
151 * Set TEMP_ALARM peripheral's TYPE so that the regmap-irq framework
152 * reads this as the default value instead of zero, the HW default.
153 * This is required to enable the writing of TYPE registers in
154 * regmap_irq_sync_unlock().
156 rc = regmap_write(regmap, (PM8008_TEMP_ALARM_ADDR | INT_SET_TYPE_OFFSET), BIT(0));
160 /* Do the same for GPIO1 and GPIO2 peripherals */
161 rc = regmap_write(regmap, (PM8008_GPIO1_ADDR | INT_SET_TYPE_OFFSET), BIT(0));
165 rc = regmap_write(regmap, (PM8008_GPIO2_ADDR | INT_SET_TYPE_OFFSET), BIT(0));
170 static int pm8008_probe_irq_peripherals(struct device *dev,
171 struct regmap *regmap,
175 struct regmap_irq_type *type;
176 struct regmap_irq_chip_data *irq_data;
178 rc = pm8008_init(regmap);
180 dev_err(dev, "Init failed: %d\n", rc);
184 for (i = 0; i < ARRAY_SIZE(pm8008_irqs); i++) {
185 type = &pm8008_irqs[i].type;
187 type->type_reg_offset = pm8008_irqs[i].reg_offset;
188 type->type_rising_val = pm8008_irqs[i].mask;
189 type->type_falling_val = pm8008_irqs[i].mask;
190 type->type_level_high_val = 0;
191 type->type_level_low_val = 0;
193 if (type->type_reg_offset == PM8008_MISC)
194 type->types_supported = IRQ_TYPE_EDGE_RISING;
196 type->types_supported = (IRQ_TYPE_EDGE_BOTH |
197 IRQ_TYPE_LEVEL_HIGH | IRQ_TYPE_LEVEL_LOW);
200 rc = devm_regmap_add_irq_chip(dev, regmap, client_irq,
201 IRQF_SHARED, 0, &pm8008_irq_chip, &irq_data);
203 dev_err(dev, "Failed to add IRQ chip: %d\n", rc);
210 static int pm8008_probe(struct i2c_client *client)
214 struct regmap *regmap;
217 regmap = devm_regmap_init_i2c(client, &qcom_mfd_regmap_cfg);
219 return PTR_ERR(regmap);
221 i2c_set_clientdata(client, regmap);
223 if (of_property_read_bool(dev->of_node, "interrupt-controller")) {
224 rc = pm8008_probe_irq_peripherals(dev, regmap, client->irq);
226 dev_err(dev, "Failed to probe irq periphs: %d\n", rc);
229 return devm_of_platform_populate(dev);
232 static const struct of_device_id pm8008_match[] = {
233 { .compatible = "qcom,pm8008", },
237 static struct i2c_driver pm8008_mfd_driver = {
240 .of_match_table = pm8008_match,
242 .probe_new = pm8008_probe,
244 module_i2c_driver(pm8008_mfd_driver);
246 MODULE_LICENSE("GPL v2");
247 MODULE_ALIAS("i2c:qcom-pm8008");