1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2018 Spreadtrum Communications Inc.
4 * Copyright (C) 2018 Linaro Ltd.
7 #include <linux/gpio/driver.h>
8 #include <linux/interrupt.h>
9 #include <linux/kernel.h>
10 #include <linux/module.h>
12 #include <linux/platform_device.h>
13 #include <linux/regmap.h>
15 /* EIC registers definition */
16 #define SPRD_PMIC_EIC_DATA 0x0
17 #define SPRD_PMIC_EIC_DMSK 0x4
18 #define SPRD_PMIC_EIC_IEV 0x14
19 #define SPRD_PMIC_EIC_IE 0x18
20 #define SPRD_PMIC_EIC_RIS 0x1c
21 #define SPRD_PMIC_EIC_MIS 0x20
22 #define SPRD_PMIC_EIC_IC 0x24
23 #define SPRD_PMIC_EIC_TRIG 0x28
24 #define SPRD_PMIC_EIC_CTRL0 0x40
27 * The PMIC EIC controller only has one bank, and each bank now can contain
30 #define SPRD_PMIC_EIC_PER_BANK_NR 16
31 #define SPRD_PMIC_EIC_NR SPRD_PMIC_EIC_PER_BANK_NR
32 #define SPRD_PMIC_EIC_DATA_MASK GENMASK(15, 0)
33 #define SPRD_PMIC_EIC_BIT(x) ((x) & (SPRD_PMIC_EIC_PER_BANK_NR - 1))
34 #define SPRD_PMIC_EIC_DBNC_MASK GENMASK(11, 0)
37 * These registers are modified under the irq bus lock and cached to avoid
38 * unnecessary writes in bus_sync_unlock.
48 * struct sprd_pmic_eic - PMIC EIC controller
49 * @chip: the gpio_chip structure.
50 * @map: the regmap from the parent device.
51 * @offset: the EIC controller's offset address of the PMIC.
52 * @reg: the array to cache the EIC registers.
53 * @buslock: for bus lock/sync and unlock.
54 * @irq: the interrupt number of the PMIC EIC conteroller.
56 struct sprd_pmic_eic
{
57 struct gpio_chip chip
;
60 u8 reg
[CACHE_NR_REGS
];
65 static void sprd_pmic_eic_update(struct gpio_chip
*chip
, unsigned int offset
,
66 u16 reg
, unsigned int val
)
68 struct sprd_pmic_eic
*pmic_eic
= gpiochip_get_data(chip
);
69 u32 shift
= SPRD_PMIC_EIC_BIT(offset
);
71 regmap_update_bits(pmic_eic
->map
, pmic_eic
->offset
+ reg
,
72 BIT(shift
), val
<< shift
);
75 static int sprd_pmic_eic_read(struct gpio_chip
*chip
, unsigned int offset
,
78 struct sprd_pmic_eic
*pmic_eic
= gpiochip_get_data(chip
);
82 ret
= regmap_read(pmic_eic
->map
, pmic_eic
->offset
+ reg
, &value
);
86 return !!(value
& BIT(SPRD_PMIC_EIC_BIT(offset
)));
89 static int sprd_pmic_eic_request(struct gpio_chip
*chip
, unsigned int offset
)
91 sprd_pmic_eic_update(chip
, offset
, SPRD_PMIC_EIC_DMSK
, 1);
95 static void sprd_pmic_eic_free(struct gpio_chip
*chip
, unsigned int offset
)
97 sprd_pmic_eic_update(chip
, offset
, SPRD_PMIC_EIC_DMSK
, 0);
100 static int sprd_pmic_eic_get(struct gpio_chip
*chip
, unsigned int offset
)
102 return sprd_pmic_eic_read(chip
, offset
, SPRD_PMIC_EIC_DATA
);
105 static int sprd_pmic_eic_direction_input(struct gpio_chip
*chip
,
108 /* EICs are always input, nothing need to do here. */
112 static void sprd_pmic_eic_set(struct gpio_chip
*chip
, unsigned int offset
,
115 /* EICs are always input, nothing need to do here. */
118 static int sprd_pmic_eic_set_debounce(struct gpio_chip
*chip
,
120 unsigned int debounce
)
122 struct sprd_pmic_eic
*pmic_eic
= gpiochip_get_data(chip
);
126 reg
= SPRD_PMIC_EIC_CTRL0
+ SPRD_PMIC_EIC_BIT(offset
) * 0x4;
127 ret
= regmap_read(pmic_eic
->map
, pmic_eic
->offset
+ reg
, &value
);
131 value
&= ~SPRD_PMIC_EIC_DBNC_MASK
;
132 value
|= (debounce
/ 1000) & SPRD_PMIC_EIC_DBNC_MASK
;
133 return regmap_write(pmic_eic
->map
, pmic_eic
->offset
+ reg
, value
);
136 static int sprd_pmic_eic_set_config(struct gpio_chip
*chip
, unsigned int offset
,
137 unsigned long config
)
139 unsigned long param
= pinconf_to_config_param(config
);
140 u32 arg
= pinconf_to_config_argument(config
);
142 if (param
== PIN_CONFIG_INPUT_DEBOUNCE
)
143 return sprd_pmic_eic_set_debounce(chip
, offset
, arg
);
148 static void sprd_pmic_eic_irq_mask(struct irq_data
*data
)
150 struct gpio_chip
*chip
= irq_data_get_irq_chip_data(data
);
151 struct sprd_pmic_eic
*pmic_eic
= gpiochip_get_data(chip
);
152 u32 offset
= irqd_to_hwirq(data
);
154 pmic_eic
->reg
[REG_IE
] &= ~BIT(offset
);
155 pmic_eic
->reg
[REG_TRIG
] &= ~BIT(offset
);
157 gpiochip_disable_irq(chip
, offset
);
160 static void sprd_pmic_eic_irq_unmask(struct irq_data
*data
)
162 struct gpio_chip
*chip
= irq_data_get_irq_chip_data(data
);
163 struct sprd_pmic_eic
*pmic_eic
= gpiochip_get_data(chip
);
164 u32 offset
= irqd_to_hwirq(data
);
166 gpiochip_enable_irq(chip
, offset
);
168 pmic_eic
->reg
[REG_IE
] |= BIT(offset
);
169 pmic_eic
->reg
[REG_TRIG
] |= BIT(offset
);
172 static int sprd_pmic_eic_irq_set_type(struct irq_data
*data
,
173 unsigned int flow_type
)
175 struct gpio_chip
*chip
= irq_data_get_irq_chip_data(data
);
176 struct sprd_pmic_eic
*pmic_eic
= gpiochip_get_data(chip
);
177 u32 offset
= irqd_to_hwirq(data
);
180 case IRQ_TYPE_LEVEL_HIGH
:
181 pmic_eic
->reg
[REG_IEV
] |= BIT(offset
);
183 case IRQ_TYPE_LEVEL_LOW
:
184 pmic_eic
->reg
[REG_IEV
] &= ~BIT(offset
);
186 case IRQ_TYPE_EDGE_RISING
:
187 case IRQ_TYPE_EDGE_FALLING
:
188 case IRQ_TYPE_EDGE_BOTH
:
190 * Will set the trigger level according to current EIC level
191 * in irq_bus_sync_unlock() interface, so here nothing to do.
201 static void sprd_pmic_eic_bus_lock(struct irq_data
*data
)
203 struct gpio_chip
*chip
= irq_data_get_irq_chip_data(data
);
204 struct sprd_pmic_eic
*pmic_eic
= gpiochip_get_data(chip
);
206 mutex_lock(&pmic_eic
->buslock
);
209 static void sprd_pmic_eic_bus_sync_unlock(struct irq_data
*data
)
211 struct gpio_chip
*chip
= irq_data_get_irq_chip_data(data
);
212 struct sprd_pmic_eic
*pmic_eic
= gpiochip_get_data(chip
);
213 u32 trigger
= irqd_get_trigger_type(data
);
214 u32 offset
= irqd_to_hwirq(data
);
218 if (trigger
& IRQ_TYPE_EDGE_BOTH
) {
219 state
= sprd_pmic_eic_get(chip
, offset
);
221 sprd_pmic_eic_update(chip
, offset
, SPRD_PMIC_EIC_IEV
, 0);
223 sprd_pmic_eic_update(chip
, offset
, SPRD_PMIC_EIC_IEV
, 1);
225 sprd_pmic_eic_update(chip
, offset
, SPRD_PMIC_EIC_IEV
,
226 !!(pmic_eic
->reg
[REG_IEV
] & BIT(offset
)));
230 sprd_pmic_eic_update(chip
, offset
, SPRD_PMIC_EIC_IE
,
231 !!(pmic_eic
->reg
[REG_IE
] & BIT(offset
)));
232 /* Generate trigger start pulse for debounce EIC */
233 sprd_pmic_eic_update(chip
, offset
, SPRD_PMIC_EIC_TRIG
,
234 !!(pmic_eic
->reg
[REG_TRIG
] & BIT(offset
)));
236 mutex_unlock(&pmic_eic
->buslock
);
239 static void sprd_pmic_eic_toggle_trigger(struct gpio_chip
*chip
,
240 unsigned int irq
, unsigned int offset
)
242 u32 trigger
= irq_get_trigger_type(irq
);
243 int state
, post_state
;
245 if (!(trigger
& IRQ_TYPE_EDGE_BOTH
))
248 state
= sprd_pmic_eic_get(chip
, offset
);
251 sprd_pmic_eic_update(chip
, offset
, SPRD_PMIC_EIC_IEV
, 0);
253 sprd_pmic_eic_update(chip
, offset
, SPRD_PMIC_EIC_IEV
, 1);
255 post_state
= sprd_pmic_eic_get(chip
, offset
);
256 if (state
!= post_state
) {
257 dev_warn(chip
->parent
, "PMIC EIC level was changed.\n");
263 sprd_pmic_eic_update(chip
, offset
, SPRD_PMIC_EIC_IE
, 1);
264 /* Generate trigger start pulse for debounce EIC */
265 sprd_pmic_eic_update(chip
, offset
, SPRD_PMIC_EIC_TRIG
, 1);
268 static irqreturn_t
sprd_pmic_eic_irq_handler(int irq
, void *data
)
270 struct sprd_pmic_eic
*pmic_eic
= data
;
271 struct gpio_chip
*chip
= &pmic_eic
->chip
;
272 unsigned long status
;
276 ret
= regmap_read(pmic_eic
->map
, pmic_eic
->offset
+ SPRD_PMIC_EIC_MIS
,
279 return IRQ_RETVAL(ret
);
281 status
= val
& SPRD_PMIC_EIC_DATA_MASK
;
283 for_each_set_bit(n
, &status
, chip
->ngpio
) {
284 /* Clear the interrupt */
285 sprd_pmic_eic_update(chip
, n
, SPRD_PMIC_EIC_IC
, 1);
287 girq
= irq_find_mapping(chip
->irq
.domain
, n
);
288 handle_nested_irq(girq
);
291 * The PMIC EIC can only support level trigger, so we can
292 * toggle the level trigger to emulate the edge trigger.
294 sprd_pmic_eic_toggle_trigger(chip
, girq
, n
);
300 static const struct irq_chip pmic_eic_irq_chip
= {
301 .name
= "sprd-pmic-eic",
302 .irq_mask
= sprd_pmic_eic_irq_mask
,
303 .irq_unmask
= sprd_pmic_eic_irq_unmask
,
304 .irq_set_type
= sprd_pmic_eic_irq_set_type
,
305 .irq_bus_lock
= sprd_pmic_eic_bus_lock
,
306 .irq_bus_sync_unlock
= sprd_pmic_eic_bus_sync_unlock
,
307 .flags
= IRQCHIP_SKIP_SET_WAKE
| IRQCHIP_IMMUTABLE
,
308 GPIOCHIP_IRQ_RESOURCE_HELPERS
,
311 static int sprd_pmic_eic_probe(struct platform_device
*pdev
)
313 struct gpio_irq_chip
*irq
;
314 struct sprd_pmic_eic
*pmic_eic
;
317 pmic_eic
= devm_kzalloc(&pdev
->dev
, sizeof(*pmic_eic
), GFP_KERNEL
);
321 mutex_init(&pmic_eic
->buslock
);
323 pmic_eic
->irq
= platform_get_irq(pdev
, 0);
324 if (pmic_eic
->irq
< 0)
325 return pmic_eic
->irq
;
327 pmic_eic
->map
= dev_get_regmap(pdev
->dev
.parent
, NULL
);
331 ret
= of_property_read_u32(pdev
->dev
.of_node
, "reg", &pmic_eic
->offset
);
333 dev_err(&pdev
->dev
, "Failed to get PMIC EIC base address.\n");
337 ret
= devm_request_threaded_irq(&pdev
->dev
, pmic_eic
->irq
, NULL
,
338 sprd_pmic_eic_irq_handler
,
339 IRQF_ONESHOT
| IRQF_NO_SUSPEND
,
340 dev_name(&pdev
->dev
), pmic_eic
);
342 dev_err(&pdev
->dev
, "Failed to request PMIC EIC IRQ.\n");
346 pmic_eic
->chip
.label
= dev_name(&pdev
->dev
);
347 pmic_eic
->chip
.ngpio
= SPRD_PMIC_EIC_NR
;
348 pmic_eic
->chip
.base
= -1;
349 pmic_eic
->chip
.parent
= &pdev
->dev
;
350 pmic_eic
->chip
.direction_input
= sprd_pmic_eic_direction_input
;
351 pmic_eic
->chip
.request
= sprd_pmic_eic_request
;
352 pmic_eic
->chip
.free
= sprd_pmic_eic_free
;
353 pmic_eic
->chip
.set_config
= sprd_pmic_eic_set_config
;
354 pmic_eic
->chip
.set
= sprd_pmic_eic_set
;
355 pmic_eic
->chip
.get
= sprd_pmic_eic_get
;
356 pmic_eic
->chip
.can_sleep
= true;
358 irq
= &pmic_eic
->chip
.irq
;
359 gpio_irq_chip_set_chip(irq
, &pmic_eic_irq_chip
);
360 irq
->threaded
= true;
362 ret
= devm_gpiochip_add_data(&pdev
->dev
, &pmic_eic
->chip
, pmic_eic
);
364 dev_err(&pdev
->dev
, "Could not register gpiochip %d.\n", ret
);
371 static const struct of_device_id sprd_pmic_eic_of_match
[] = {
372 { .compatible
= "sprd,sc2731-eic", },
373 { /* end of list */ }
375 MODULE_DEVICE_TABLE(of
, sprd_pmic_eic_of_match
);
377 static struct platform_driver sprd_pmic_eic_driver
= {
378 .probe
= sprd_pmic_eic_probe
,
380 .name
= "sprd-pmic-eic",
381 .of_match_table
= sprd_pmic_eic_of_match
,
385 module_platform_driver(sprd_pmic_eic_driver
);
387 MODULE_DESCRIPTION("Spreadtrum PMIC EIC driver");
388 MODULE_LICENSE("GPL v2");