Linux Audio

Check our new training course

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