xref: /linux/drivers/irqchip/irq-qcom-mpm.c (revision 8ad032cc)
1a6199bb5SShawn Guo // SPDX-License-Identifier: GPL-2.0-only
2a6199bb5SShawn Guo /*
3a6199bb5SShawn Guo  * Copyright (c) 2021, Linaro Limited
4a6199bb5SShawn Guo  * Copyright (c) 2010-2020, The Linux Foundation. All rights reserved.
5a6199bb5SShawn Guo  */
6a6199bb5SShawn Guo 
7a6199bb5SShawn Guo #include <linux/delay.h>
8a6199bb5SShawn Guo #include <linux/err.h>
9a6199bb5SShawn Guo #include <linux/init.h>
10a6199bb5SShawn Guo #include <linux/interrupt.h>
11a6199bb5SShawn Guo #include <linux/io.h>
12a6199bb5SShawn Guo #include <linux/irqchip.h>
13a6199bb5SShawn Guo #include <linux/irqdomain.h>
14a6199bb5SShawn Guo #include <linux/mailbox_client.h>
15a6199bb5SShawn Guo #include <linux/module.h>
16a6199bb5SShawn Guo #include <linux/of.h>
17221b110dSKonrad Dybcio #include <linux/of_address.h>
18ee076750SRob Herring #include <linux/of_platform.h>
19a6199bb5SShawn Guo #include <linux/platform_device.h>
20a6199bb5SShawn Guo #include <linux/pm_domain.h>
21a6199bb5SShawn Guo #include <linux/slab.h>
22a6199bb5SShawn Guo #include <linux/soc/qcom/irq.h>
23a6199bb5SShawn Guo #include <linux/spinlock.h>
24a6199bb5SShawn Guo 
25a6199bb5SShawn Guo /*
26a6199bb5SShawn Guo  * This is the driver for Qualcomm MPM (MSM Power Manager) interrupt controller,
27a6199bb5SShawn Guo  * which is commonly found on Qualcomm SoCs built on the RPM architecture.
28a6199bb5SShawn Guo  * Sitting in always-on domain, MPM monitors the wakeup interrupts when SoC is
29a6199bb5SShawn Guo  * asleep, and wakes up the AP when one of those interrupts occurs.  This driver
30a6199bb5SShawn Guo  * doesn't directly access physical MPM registers though.  Instead, the access
31a6199bb5SShawn Guo  * is bridged via a piece of internal memory (SRAM) that is accessible to both
32a6199bb5SShawn Guo  * AP and RPM.  This piece of memory is called 'vMPM' in the driver.
33a6199bb5SShawn Guo  *
34a6199bb5SShawn Guo  * When SoC is awake, the vMPM is owned by AP and the register setup by this
35a6199bb5SShawn Guo  * driver all happens on vMPM.  When AP is about to get power collapsed, the
36a6199bb5SShawn Guo  * driver sends a mailbox notification to RPM, which will take over the vMPM
37a6199bb5SShawn Guo  * ownership and dump vMPM into physical MPM registers.  On wakeup, AP is woken
38a6199bb5SShawn Guo  * up by a MPM pin/interrupt, and RPM will copy STATUS registers into vMPM.
39a6199bb5SShawn Guo  * Then AP start owning vMPM again.
40a6199bb5SShawn Guo  *
41a6199bb5SShawn Guo  * vMPM register map:
42a6199bb5SShawn Guo  *
43a6199bb5SShawn Guo  *    31                              0
44a6199bb5SShawn Guo  *    +--------------------------------+
45a6199bb5SShawn Guo  *    |            TIMER0              | 0x00
46a6199bb5SShawn Guo  *    +--------------------------------+
47a6199bb5SShawn Guo  *    |            TIMER1              | 0x04
48a6199bb5SShawn Guo  *    +--------------------------------+
49a6199bb5SShawn Guo  *    |            ENABLE0             | 0x08
50a6199bb5SShawn Guo  *    +--------------------------------+
51a6199bb5SShawn Guo  *    |              ...               | ...
52a6199bb5SShawn Guo  *    +--------------------------------+
53a6199bb5SShawn Guo  *    |            ENABLEn             |
54a6199bb5SShawn Guo  *    +--------------------------------+
55a6199bb5SShawn Guo  *    |          FALLING_EDGE0         |
56a6199bb5SShawn Guo  *    +--------------------------------+
57a6199bb5SShawn Guo  *    |              ...               |
58a6199bb5SShawn Guo  *    +--------------------------------+
59a6199bb5SShawn Guo  *    |            STATUSn             |
60a6199bb5SShawn Guo  *    +--------------------------------+
61a6199bb5SShawn Guo  *
62a6199bb5SShawn Guo  *    n = DIV_ROUND_UP(pin_cnt, 32)
63a6199bb5SShawn Guo  *
64a6199bb5SShawn Guo  */
65a6199bb5SShawn Guo 
66a6199bb5SShawn Guo #define MPM_REG_ENABLE		0
67a6199bb5SShawn Guo #define MPM_REG_FALLING_EDGE	1
68a6199bb5SShawn Guo #define MPM_REG_RISING_EDGE	2
69a6199bb5SShawn Guo #define MPM_REG_POLARITY	3
70a6199bb5SShawn Guo #define MPM_REG_STATUS		4
71a6199bb5SShawn Guo 
72a6199bb5SShawn Guo /* MPM pin map to GIC hwirq */
73a6199bb5SShawn Guo struct mpm_gic_map {
74a6199bb5SShawn Guo 	int pin;
75a6199bb5SShawn Guo 	irq_hw_number_t hwirq;
76a6199bb5SShawn Guo };
77a6199bb5SShawn Guo 
78a6199bb5SShawn Guo struct qcom_mpm_priv {
79a6199bb5SShawn Guo 	void __iomem *base;
80a6199bb5SShawn Guo 	raw_spinlock_t lock;
81a6199bb5SShawn Guo 	struct mbox_client mbox_client;
82a6199bb5SShawn Guo 	struct mbox_chan *mbox_chan;
83a6199bb5SShawn Guo 	struct mpm_gic_map *maps;
84a6199bb5SShawn Guo 	unsigned int map_cnt;
85a6199bb5SShawn Guo 	unsigned int reg_stride;
86a6199bb5SShawn Guo 	struct irq_domain *domain;
87a6199bb5SShawn Guo 	struct generic_pm_domain genpd;
88a6199bb5SShawn Guo };
89a6199bb5SShawn Guo 
qcom_mpm_read(struct qcom_mpm_priv * priv,unsigned int reg,unsigned int index)90a6199bb5SShawn Guo static u32 qcom_mpm_read(struct qcom_mpm_priv *priv, unsigned int reg,
91a6199bb5SShawn Guo 			 unsigned int index)
92a6199bb5SShawn Guo {
93a6199bb5SShawn Guo 	unsigned int offset = (reg * priv->reg_stride + index + 2) * 4;
94a6199bb5SShawn Guo 
95a6199bb5SShawn Guo 	return readl_relaxed(priv->base + offset);
96a6199bb5SShawn Guo }
97a6199bb5SShawn Guo 
qcom_mpm_write(struct qcom_mpm_priv * priv,unsigned int reg,unsigned int index,u32 val)98a6199bb5SShawn Guo static void qcom_mpm_write(struct qcom_mpm_priv *priv, unsigned int reg,
99a6199bb5SShawn Guo 			   unsigned int index, u32 val)
100a6199bb5SShawn Guo {
101a6199bb5SShawn Guo 	unsigned int offset = (reg * priv->reg_stride + index + 2) * 4;
102a6199bb5SShawn Guo 
103a6199bb5SShawn Guo 	writel_relaxed(val, priv->base + offset);
104a6199bb5SShawn Guo 
105a6199bb5SShawn Guo 	/* Ensure the write is completed */
106a6199bb5SShawn Guo 	wmb();
107a6199bb5SShawn Guo }
108a6199bb5SShawn Guo 
qcom_mpm_enable_irq(struct irq_data * d,bool en)109a6199bb5SShawn Guo static void qcom_mpm_enable_irq(struct irq_data *d, bool en)
110a6199bb5SShawn Guo {
111a6199bb5SShawn Guo 	struct qcom_mpm_priv *priv = d->chip_data;
112a6199bb5SShawn Guo 	int pin = d->hwirq;
113a6199bb5SShawn Guo 	unsigned int index = pin / 32;
114a6199bb5SShawn Guo 	unsigned int shift = pin % 32;
115a6199bb5SShawn Guo 	unsigned long flags, val;
116a6199bb5SShawn Guo 
117a6199bb5SShawn Guo 	raw_spin_lock_irqsave(&priv->lock, flags);
118a6199bb5SShawn Guo 
119a6199bb5SShawn Guo 	val = qcom_mpm_read(priv, MPM_REG_ENABLE, index);
120a6199bb5SShawn Guo 	__assign_bit(shift, &val, en);
121a6199bb5SShawn Guo 	qcom_mpm_write(priv, MPM_REG_ENABLE, index, val);
122a6199bb5SShawn Guo 
123a6199bb5SShawn Guo 	raw_spin_unlock_irqrestore(&priv->lock, flags);
124a6199bb5SShawn Guo }
125a6199bb5SShawn Guo 
qcom_mpm_mask(struct irq_data * d)126a6199bb5SShawn Guo static void qcom_mpm_mask(struct irq_data *d)
127a6199bb5SShawn Guo {
128a6199bb5SShawn Guo 	qcom_mpm_enable_irq(d, false);
129a6199bb5SShawn Guo 
130a6199bb5SShawn Guo 	if (d->parent_data)
131a6199bb5SShawn Guo 		irq_chip_mask_parent(d);
132a6199bb5SShawn Guo }
133a6199bb5SShawn Guo 
qcom_mpm_unmask(struct irq_data * d)134a6199bb5SShawn Guo static void qcom_mpm_unmask(struct irq_data *d)
135a6199bb5SShawn Guo {
136a6199bb5SShawn Guo 	qcom_mpm_enable_irq(d, true);
137a6199bb5SShawn Guo 
138a6199bb5SShawn Guo 	if (d->parent_data)
139a6199bb5SShawn Guo 		irq_chip_unmask_parent(d);
140a6199bb5SShawn Guo }
141a6199bb5SShawn Guo 
mpm_set_type(struct qcom_mpm_priv * priv,bool set,unsigned int reg,unsigned int index,unsigned int shift)142a6199bb5SShawn Guo static void mpm_set_type(struct qcom_mpm_priv *priv, bool set, unsigned int reg,
143a6199bb5SShawn Guo 			 unsigned int index, unsigned int shift)
144a6199bb5SShawn Guo {
145a6199bb5SShawn Guo 	unsigned long flags, val;
146a6199bb5SShawn Guo 
147a6199bb5SShawn Guo 	raw_spin_lock_irqsave(&priv->lock, flags);
148a6199bb5SShawn Guo 
149a6199bb5SShawn Guo 	val = qcom_mpm_read(priv, reg, index);
150a6199bb5SShawn Guo 	__assign_bit(shift, &val, set);
151a6199bb5SShawn Guo 	qcom_mpm_write(priv, reg, index, val);
152a6199bb5SShawn Guo 
153a6199bb5SShawn Guo 	raw_spin_unlock_irqrestore(&priv->lock, flags);
154a6199bb5SShawn Guo }
155a6199bb5SShawn Guo 
qcom_mpm_set_type(struct irq_data * d,unsigned int type)156a6199bb5SShawn Guo static int qcom_mpm_set_type(struct irq_data *d, unsigned int type)
157a6199bb5SShawn Guo {
158a6199bb5SShawn Guo 	struct qcom_mpm_priv *priv = d->chip_data;
159a6199bb5SShawn Guo 	int pin = d->hwirq;
160a6199bb5SShawn Guo 	unsigned int index = pin / 32;
161a6199bb5SShawn Guo 	unsigned int shift = pin % 32;
162a6199bb5SShawn Guo 
163a6199bb5SShawn Guo 	if (type & IRQ_TYPE_EDGE_RISING)
164a6199bb5SShawn Guo 		mpm_set_type(priv, true, MPM_REG_RISING_EDGE, index, shift);
165a6199bb5SShawn Guo 	else
166a6199bb5SShawn Guo 		mpm_set_type(priv, false, MPM_REG_RISING_EDGE, index, shift);
167a6199bb5SShawn Guo 
168a6199bb5SShawn Guo 	if (type & IRQ_TYPE_EDGE_FALLING)
169a6199bb5SShawn Guo 		mpm_set_type(priv, true, MPM_REG_FALLING_EDGE, index, shift);
170a6199bb5SShawn Guo 	else
171a6199bb5SShawn Guo 		mpm_set_type(priv, false, MPM_REG_FALLING_EDGE, index, shift);
172a6199bb5SShawn Guo 
173a6199bb5SShawn Guo 	if (type & IRQ_TYPE_LEVEL_HIGH)
174a6199bb5SShawn Guo 		mpm_set_type(priv, true, MPM_REG_POLARITY, index, shift);
175a6199bb5SShawn Guo 	else
176a6199bb5SShawn Guo 		mpm_set_type(priv, false, MPM_REG_POLARITY, index, shift);
177a6199bb5SShawn Guo 
178a6199bb5SShawn Guo 	if (!d->parent_data)
179a6199bb5SShawn Guo 		return 0;
180a6199bb5SShawn Guo 
181a6199bb5SShawn Guo 	if (type & IRQ_TYPE_EDGE_BOTH)
182a6199bb5SShawn Guo 		type = IRQ_TYPE_EDGE_RISING;
183a6199bb5SShawn Guo 
184a6199bb5SShawn Guo 	if (type & IRQ_TYPE_LEVEL_MASK)
185a6199bb5SShawn Guo 		type = IRQ_TYPE_LEVEL_HIGH;
186a6199bb5SShawn Guo 
187a6199bb5SShawn Guo 	return irq_chip_set_type_parent(d, type);
188a6199bb5SShawn Guo }
189a6199bb5SShawn Guo 
190a6199bb5SShawn Guo static struct irq_chip qcom_mpm_chip = {
191a6199bb5SShawn Guo 	.name			= "mpm",
192a6199bb5SShawn Guo 	.irq_eoi		= irq_chip_eoi_parent,
193a6199bb5SShawn Guo 	.irq_mask		= qcom_mpm_mask,
194a6199bb5SShawn Guo 	.irq_unmask		= qcom_mpm_unmask,
195a6199bb5SShawn Guo 	.irq_retrigger		= irq_chip_retrigger_hierarchy,
196a6199bb5SShawn Guo 	.irq_set_type		= qcom_mpm_set_type,
197a6199bb5SShawn Guo 	.irq_set_affinity	= irq_chip_set_affinity_parent,
198a6199bb5SShawn Guo 	.flags			= IRQCHIP_MASK_ON_SUSPEND |
199a6199bb5SShawn Guo 				  IRQCHIP_SKIP_SET_WAKE,
200a6199bb5SShawn Guo };
201a6199bb5SShawn Guo 
get_mpm_gic_map(struct qcom_mpm_priv * priv,int pin)202a6199bb5SShawn Guo static struct mpm_gic_map *get_mpm_gic_map(struct qcom_mpm_priv *priv, int pin)
203a6199bb5SShawn Guo {
204a6199bb5SShawn Guo 	struct mpm_gic_map *maps = priv->maps;
205a6199bb5SShawn Guo 	int i;
206a6199bb5SShawn Guo 
207a6199bb5SShawn Guo 	for (i = 0; i < priv->map_cnt; i++) {
208a6199bb5SShawn Guo 		if (maps[i].pin == pin)
209a6199bb5SShawn Guo 			return &maps[i];
210a6199bb5SShawn Guo 	}
211a6199bb5SShawn Guo 
212a6199bb5SShawn Guo 	return NULL;
213a6199bb5SShawn Guo }
214a6199bb5SShawn Guo 
qcom_mpm_alloc(struct irq_domain * domain,unsigned int virq,unsigned int nr_irqs,void * data)215a6199bb5SShawn Guo static int qcom_mpm_alloc(struct irq_domain *domain, unsigned int virq,
216a6199bb5SShawn Guo 			  unsigned int nr_irqs, void *data)
217a6199bb5SShawn Guo {
218a6199bb5SShawn Guo 	struct qcom_mpm_priv *priv = domain->host_data;
219a6199bb5SShawn Guo 	struct irq_fwspec *fwspec = data;
220a6199bb5SShawn Guo 	struct irq_fwspec parent_fwspec;
221a6199bb5SShawn Guo 	struct mpm_gic_map *map;
222a6199bb5SShawn Guo 	irq_hw_number_t pin;
223a6199bb5SShawn Guo 	unsigned int type;
224a6199bb5SShawn Guo 	int  ret;
225a6199bb5SShawn Guo 
226a6199bb5SShawn Guo 	ret = irq_domain_translate_twocell(domain, fwspec, &pin, &type);
227a6199bb5SShawn Guo 	if (ret)
228a6199bb5SShawn Guo 		return ret;
229a6199bb5SShawn Guo 
230a6199bb5SShawn Guo 	ret = irq_domain_set_hwirq_and_chip(domain, virq, pin,
231a6199bb5SShawn Guo 					    &qcom_mpm_chip, priv);
232a6199bb5SShawn Guo 	if (ret)
233a6199bb5SShawn Guo 		return ret;
234a6199bb5SShawn Guo 
235a6199bb5SShawn Guo 	map = get_mpm_gic_map(priv, pin);
236a6199bb5SShawn Guo 	if (map == NULL)
237a6199bb5SShawn Guo 		return irq_domain_disconnect_hierarchy(domain->parent, virq);
238a6199bb5SShawn Guo 
239a6199bb5SShawn Guo 	if (type & IRQ_TYPE_EDGE_BOTH)
240a6199bb5SShawn Guo 		type = IRQ_TYPE_EDGE_RISING;
241a6199bb5SShawn Guo 
242a6199bb5SShawn Guo 	if (type & IRQ_TYPE_LEVEL_MASK)
243a6199bb5SShawn Guo 		type = IRQ_TYPE_LEVEL_HIGH;
244a6199bb5SShawn Guo 
245a6199bb5SShawn Guo 	parent_fwspec.fwnode = domain->parent->fwnode;
246a6199bb5SShawn Guo 	parent_fwspec.param_count = 3;
247a6199bb5SShawn Guo 	parent_fwspec.param[0] = 0;
248a6199bb5SShawn Guo 	parent_fwspec.param[1] = map->hwirq;
249a6199bb5SShawn Guo 	parent_fwspec.param[2] = type;
250a6199bb5SShawn Guo 
251a6199bb5SShawn Guo 	return irq_domain_alloc_irqs_parent(domain, virq, nr_irqs,
252a6199bb5SShawn Guo 					    &parent_fwspec);
253a6199bb5SShawn Guo }
254a6199bb5SShawn Guo 
255a6199bb5SShawn Guo static const struct irq_domain_ops qcom_mpm_ops = {
256a6199bb5SShawn Guo 	.alloc		= qcom_mpm_alloc,
257a6199bb5SShawn Guo 	.free		= irq_domain_free_irqs_common,
258a6199bb5SShawn Guo 	.translate	= irq_domain_translate_twocell,
259a6199bb5SShawn Guo };
260a6199bb5SShawn Guo 
261a6199bb5SShawn Guo /* Triggered by RPM when system resumes from deep sleep */
qcom_mpm_handler(int irq,void * dev_id)262a6199bb5SShawn Guo static irqreturn_t qcom_mpm_handler(int irq, void *dev_id)
263a6199bb5SShawn Guo {
264a6199bb5SShawn Guo 	struct qcom_mpm_priv *priv = dev_id;
265a6199bb5SShawn Guo 	unsigned long enable, pending;
266a6199bb5SShawn Guo 	irqreturn_t ret = IRQ_NONE;
267a6199bb5SShawn Guo 	unsigned long flags;
268a6199bb5SShawn Guo 	int i, j;
269a6199bb5SShawn Guo 
270a6199bb5SShawn Guo 	for (i = 0; i < priv->reg_stride; i++) {
271a6199bb5SShawn Guo 		raw_spin_lock_irqsave(&priv->lock, flags);
272a6199bb5SShawn Guo 		enable = qcom_mpm_read(priv, MPM_REG_ENABLE, i);
273a6199bb5SShawn Guo 		pending = qcom_mpm_read(priv, MPM_REG_STATUS, i);
274a6199bb5SShawn Guo 		pending &= enable;
275a6199bb5SShawn Guo 		raw_spin_unlock_irqrestore(&priv->lock, flags);
276a6199bb5SShawn Guo 
277a6199bb5SShawn Guo 		for_each_set_bit(j, &pending, 32) {
278a6199bb5SShawn Guo 			unsigned int pin = 32 * i + j;
279a6199bb5SShawn Guo 			struct irq_desc *desc = irq_resolve_mapping(priv->domain, pin);
280a6199bb5SShawn Guo 			struct irq_data *d = &desc->irq_data;
281a6199bb5SShawn Guo 
282a6199bb5SShawn Guo 			if (!irqd_is_level_type(d))
283a6199bb5SShawn Guo 				irq_set_irqchip_state(d->irq,
284a6199bb5SShawn Guo 						IRQCHIP_STATE_PENDING, true);
285a6199bb5SShawn Guo 			ret = IRQ_HANDLED;
286a6199bb5SShawn Guo 		}
287a6199bb5SShawn Guo 	}
288a6199bb5SShawn Guo 
289a6199bb5SShawn Guo 	return ret;
290a6199bb5SShawn Guo }
291a6199bb5SShawn Guo 
mpm_pd_power_off(struct generic_pm_domain * genpd)292a6199bb5SShawn Guo static int mpm_pd_power_off(struct generic_pm_domain *genpd)
293a6199bb5SShawn Guo {
294a6199bb5SShawn Guo 	struct qcom_mpm_priv *priv = container_of(genpd, struct qcom_mpm_priv,
295a6199bb5SShawn Guo 						  genpd);
296a6199bb5SShawn Guo 	int i, ret;
297a6199bb5SShawn Guo 
298a6199bb5SShawn Guo 	for (i = 0; i < priv->reg_stride; i++)
299a6199bb5SShawn Guo 		qcom_mpm_write(priv, MPM_REG_STATUS, i, 0);
300a6199bb5SShawn Guo 
301a6199bb5SShawn Guo 	/* Notify RPM to write vMPM into HW */
302a6199bb5SShawn Guo 	ret = mbox_send_message(priv->mbox_chan, NULL);
303a6199bb5SShawn Guo 	if (ret < 0)
304a6199bb5SShawn Guo 		return ret;
305a6199bb5SShawn Guo 
306a6199bb5SShawn Guo 	return 0;
307a6199bb5SShawn Guo }
308a6199bb5SShawn Guo 
gic_hwirq_is_mapped(struct mpm_gic_map * maps,int cnt,u32 hwirq)309a6199bb5SShawn Guo static bool gic_hwirq_is_mapped(struct mpm_gic_map *maps, int cnt, u32 hwirq)
310a6199bb5SShawn Guo {
311a6199bb5SShawn Guo 	int i;
312a6199bb5SShawn Guo 
313a6199bb5SShawn Guo 	for (i = 0; i < cnt; i++)
314a6199bb5SShawn Guo 		if (maps[i].hwirq == hwirq)
315a6199bb5SShawn Guo 			return true;
316a6199bb5SShawn Guo 
317a6199bb5SShawn Guo 	return false;
318a6199bb5SShawn Guo }
319a6199bb5SShawn Guo 
qcom_mpm_init(struct device_node * np,struct device_node * parent)320a6199bb5SShawn Guo static int qcom_mpm_init(struct device_node *np, struct device_node *parent)
321a6199bb5SShawn Guo {
322a6199bb5SShawn Guo 	struct platform_device *pdev = of_find_device_by_node(np);
323a6199bb5SShawn Guo 	struct device *dev = &pdev->dev;
324a6199bb5SShawn Guo 	struct irq_domain *parent_domain;
325a6199bb5SShawn Guo 	struct generic_pm_domain *genpd;
326221b110dSKonrad Dybcio 	struct device_node *msgram_np;
327a6199bb5SShawn Guo 	struct qcom_mpm_priv *priv;
328a6199bb5SShawn Guo 	unsigned int pin_cnt;
329221b110dSKonrad Dybcio 	struct resource res;
330a6199bb5SShawn Guo 	int i, irq;
331a6199bb5SShawn Guo 	int ret;
332a6199bb5SShawn Guo 
333a6199bb5SShawn Guo 	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
334a6199bb5SShawn Guo 	if (!priv)
335a6199bb5SShawn Guo 		return -ENOMEM;
336a6199bb5SShawn Guo 
337a6199bb5SShawn Guo 	ret = of_property_read_u32(np, "qcom,mpm-pin-count", &pin_cnt);
338a6199bb5SShawn Guo 	if (ret) {
339a6199bb5SShawn Guo 		dev_err(dev, "failed to read qcom,mpm-pin-count: %d\n", ret);
340a6199bb5SShawn Guo 		return ret;
341a6199bb5SShawn Guo 	}
342a6199bb5SShawn Guo 
343a6199bb5SShawn Guo 	priv->reg_stride = DIV_ROUND_UP(pin_cnt, 32);
344a6199bb5SShawn Guo 
345a6199bb5SShawn Guo 	ret = of_property_count_u32_elems(np, "qcom,mpm-pin-map");
346a6199bb5SShawn Guo 	if (ret < 0) {
347a6199bb5SShawn Guo 		dev_err(dev, "failed to read qcom,mpm-pin-map: %d\n", ret);
348a6199bb5SShawn Guo 		return ret;
349a6199bb5SShawn Guo 	}
350a6199bb5SShawn Guo 
351a6199bb5SShawn Guo 	if (ret % 2) {
352a6199bb5SShawn Guo 		dev_err(dev, "invalid qcom,mpm-pin-map\n");
353a6199bb5SShawn Guo 		return -EINVAL;
354a6199bb5SShawn Guo 	}
355a6199bb5SShawn Guo 
356a6199bb5SShawn Guo 	priv->map_cnt = ret / 2;
357a6199bb5SShawn Guo 	priv->maps = devm_kcalloc(dev, priv->map_cnt, sizeof(*priv->maps),
358a6199bb5SShawn Guo 				  GFP_KERNEL);
359a6199bb5SShawn Guo 	if (!priv->maps)
360a6199bb5SShawn Guo 		return -ENOMEM;
361a6199bb5SShawn Guo 
362a6199bb5SShawn Guo 	for (i = 0; i < priv->map_cnt; i++) {
363a6199bb5SShawn Guo 		u32 pin, hwirq;
364a6199bb5SShawn Guo 
365a6199bb5SShawn Guo 		of_property_read_u32_index(np, "qcom,mpm-pin-map", i * 2, &pin);
366a6199bb5SShawn Guo 		of_property_read_u32_index(np, "qcom,mpm-pin-map", i * 2 + 1, &hwirq);
367a6199bb5SShawn Guo 
368a6199bb5SShawn Guo 		if (gic_hwirq_is_mapped(priv->maps, i, hwirq)) {
369a6199bb5SShawn Guo 			dev_warn(dev, "failed to map pin %d as GIC hwirq %d is already mapped\n",
370a6199bb5SShawn Guo 				 pin, hwirq);
371a6199bb5SShawn Guo 			continue;
372a6199bb5SShawn Guo 		}
373a6199bb5SShawn Guo 
374a6199bb5SShawn Guo 		priv->maps[i].pin = pin;
375a6199bb5SShawn Guo 		priv->maps[i].hwirq = hwirq;
376a6199bb5SShawn Guo 	}
377a6199bb5SShawn Guo 
378a6199bb5SShawn Guo 	raw_spin_lock_init(&priv->lock);
379a6199bb5SShawn Guo 
380221b110dSKonrad Dybcio 	/* If we have a handle to an RPM message ram partition, use it. */
381221b110dSKonrad Dybcio 	msgram_np = of_parse_phandle(np, "qcom,rpm-msg-ram", 0);
382221b110dSKonrad Dybcio 	if (msgram_np) {
383221b110dSKonrad Dybcio 		ret = of_address_to_resource(msgram_np, 0, &res);
384221b110dSKonrad Dybcio 		if (ret) {
385221b110dSKonrad Dybcio 			of_node_put(msgram_np);
386221b110dSKonrad Dybcio 			return ret;
387221b110dSKonrad Dybcio 		}
388221b110dSKonrad Dybcio 
389221b110dSKonrad Dybcio 		/* Don't use devm_ioremap_resource, as we're accessing a shared region. */
390221b110dSKonrad Dybcio 		priv->base = devm_ioremap(dev, res.start, resource_size(&res));
391221b110dSKonrad Dybcio 		of_node_put(msgram_np);
392*8ad032ccSDan Carpenter 		if (!priv->base)
393*8ad032ccSDan Carpenter 			return -ENOMEM;
394221b110dSKonrad Dybcio 	} else {
395221b110dSKonrad Dybcio 		/* Otherwise, fall back to simple MMIO. */
396a6199bb5SShawn Guo 		priv->base = devm_platform_ioremap_resource(pdev, 0);
39776ff614aSYang Yingliang 		if (IS_ERR(priv->base))
398a6199bb5SShawn Guo 			return PTR_ERR(priv->base);
399221b110dSKonrad Dybcio 	}
400a6199bb5SShawn Guo 
401a6199bb5SShawn Guo 	for (i = 0; i < priv->reg_stride; i++) {
402a6199bb5SShawn Guo 		qcom_mpm_write(priv, MPM_REG_ENABLE, i, 0);
403a6199bb5SShawn Guo 		qcom_mpm_write(priv, MPM_REG_FALLING_EDGE, i, 0);
404a6199bb5SShawn Guo 		qcom_mpm_write(priv, MPM_REG_RISING_EDGE, i, 0);
405a6199bb5SShawn Guo 		qcom_mpm_write(priv, MPM_REG_POLARITY, i, 0);
406a6199bb5SShawn Guo 		qcom_mpm_write(priv, MPM_REG_STATUS, i, 0);
407a6199bb5SShawn Guo 	}
408a6199bb5SShawn Guo 
409a6199bb5SShawn Guo 	irq = platform_get_irq(pdev, 0);
410a6199bb5SShawn Guo 	if (irq < 0)
411a6199bb5SShawn Guo 		return irq;
412a6199bb5SShawn Guo 
413a6199bb5SShawn Guo 	genpd = &priv->genpd;
414a6199bb5SShawn Guo 	genpd->flags = GENPD_FLAG_IRQ_SAFE;
415a6199bb5SShawn Guo 	genpd->power_off = mpm_pd_power_off;
416a6199bb5SShawn Guo 
417a6199bb5SShawn Guo 	genpd->name = devm_kasprintf(dev, GFP_KERNEL, "%s", dev_name(dev));
418a6199bb5SShawn Guo 	if (!genpd->name)
419a6199bb5SShawn Guo 		return -ENOMEM;
420a6199bb5SShawn Guo 
421a6199bb5SShawn Guo 	ret = pm_genpd_init(genpd, NULL, false);
422a6199bb5SShawn Guo 	if (ret) {
423a6199bb5SShawn Guo 		dev_err(dev, "failed to init genpd: %d\n", ret);
424a6199bb5SShawn Guo 		return ret;
425a6199bb5SShawn Guo 	}
426a6199bb5SShawn Guo 
427a6199bb5SShawn Guo 	ret = of_genpd_add_provider_simple(np, genpd);
428a6199bb5SShawn Guo 	if (ret) {
429a6199bb5SShawn Guo 		dev_err(dev, "failed to add genpd provider: %d\n", ret);
430a6199bb5SShawn Guo 		goto remove_genpd;
431a6199bb5SShawn Guo 	}
432a6199bb5SShawn Guo 
433a6199bb5SShawn Guo 	priv->mbox_client.dev = dev;
434a6199bb5SShawn Guo 	priv->mbox_chan = mbox_request_channel(&priv->mbox_client, 0);
435a6199bb5SShawn Guo 	if (IS_ERR(priv->mbox_chan)) {
436a6199bb5SShawn Guo 		ret = PTR_ERR(priv->mbox_chan);
437a6199bb5SShawn Guo 		dev_err(dev, "failed to acquire IPC channel: %d\n", ret);
438a6199bb5SShawn Guo 		return ret;
439a6199bb5SShawn Guo 	}
440a6199bb5SShawn Guo 
441a6199bb5SShawn Guo 	parent_domain = irq_find_host(parent);
442a6199bb5SShawn Guo 	if (!parent_domain) {
443a6199bb5SShawn Guo 		dev_err(dev, "failed to find MPM parent domain\n");
444a6199bb5SShawn Guo 		ret = -ENXIO;
445a6199bb5SShawn Guo 		goto free_mbox;
446a6199bb5SShawn Guo 	}
447a6199bb5SShawn Guo 
448a6199bb5SShawn Guo 	priv->domain = irq_domain_create_hierarchy(parent_domain,
449a6199bb5SShawn Guo 				IRQ_DOMAIN_FLAG_QCOM_MPM_WAKEUP, pin_cnt,
450a6199bb5SShawn Guo 				of_node_to_fwnode(np), &qcom_mpm_ops, priv);
451a6199bb5SShawn Guo 	if (!priv->domain) {
452a6199bb5SShawn Guo 		dev_err(dev, "failed to create MPM domain\n");
453a6199bb5SShawn Guo 		ret = -ENOMEM;
454a6199bb5SShawn Guo 		goto free_mbox;
455a6199bb5SShawn Guo 	}
456a6199bb5SShawn Guo 
457a6199bb5SShawn Guo 	irq_domain_update_bus_token(priv->domain, DOMAIN_BUS_WAKEUP);
458a6199bb5SShawn Guo 
459a6199bb5SShawn Guo 	ret = devm_request_irq(dev, irq, qcom_mpm_handler, IRQF_NO_SUSPEND,
460a6199bb5SShawn Guo 			       "qcom_mpm", priv);
461a6199bb5SShawn Guo 	if (ret) {
462a6199bb5SShawn Guo 		dev_err(dev, "failed to request irq: %d\n", ret);
463a6199bb5SShawn Guo 		goto remove_domain;
464a6199bb5SShawn Guo 	}
465a6199bb5SShawn Guo 
466a6199bb5SShawn Guo 	return 0;
467a6199bb5SShawn Guo 
468a6199bb5SShawn Guo remove_domain:
469a6199bb5SShawn Guo 	irq_domain_remove(priv->domain);
470a6199bb5SShawn Guo free_mbox:
471a6199bb5SShawn Guo 	mbox_free_channel(priv->mbox_chan);
472a6199bb5SShawn Guo remove_genpd:
473a6199bb5SShawn Guo 	pm_genpd_remove(genpd);
474a6199bb5SShawn Guo 	return ret;
475a6199bb5SShawn Guo }
476a6199bb5SShawn Guo 
477a6199bb5SShawn Guo IRQCHIP_PLATFORM_DRIVER_BEGIN(qcom_mpm)
478a6199bb5SShawn Guo IRQCHIP_MATCH("qcom,mpm", qcom_mpm_init)
479a6199bb5SShawn Guo IRQCHIP_PLATFORM_DRIVER_END(qcom_mpm)
480a6199bb5SShawn Guo MODULE_DESCRIPTION("Qualcomm Technologies, Inc. MSM Power Manager");
481a6199bb5SShawn Guo MODULE_LICENSE("GPL v2");
482