summaryrefstree
path: root/kernel/drivers/irqchip/irq-madera.c
blob: 0decb2f737faf1430f0f0784277f504d357efff0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
/*
 * Interrupt support for Cirrus Logic Madera codecs
 *
 * Copyright 2016 Cirrus Logic
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 */

#include <linux/module.h>
#include <linux/gpio.h>
#include <linux/interrupt.h>
#include <linux/irq.h>
#include <linux/irqdomain.h>
#include <linux/pm_runtime.h>
#include <linux/regmap.h>
#include <linux/slab.h>
#include <linux/of.h>
#include <linux/of_device.h>
#include <linux/of_gpio.h>
#include <linux/of_irq.h>
#include <linux/irqchip/irq-madera.h>
#include <linux/irqchip/irq-madera-pdata.h>
#include <linux/mfd/madera/core.h>
#include <linux/mfd/madera/pdata.h>
#include <linux/mfd/madera/registers.h>

#include "irq-madera.h"

struct madera_irq_priv {
	struct device *dev;
	int irq;
	unsigned int irq_flags;
	int irq_gpio;
	struct regmap_irq_chip_data *irq_data;
	struct irq_domain *domain;
	struct madera *madera;
};

int madera_request_irq(struct madera *madera, int irq, const char *name,
			irq_handler_t handler, void *data)
{
	struct madera_irq_priv *priv;

	if (irq < 0)
		return irq;

	if (!madera->irq_dev)
		return -EINVAL;

	priv = dev_get_drvdata(madera->irq_dev);

	return request_threaded_irq(regmap_irq_get_virq(priv->irq_data, irq),
				    NULL, handler, IRQF_ONESHOT, name, data);

}
EXPORT_SYMBOL_GPL(madera_request_irq);

void madera_free_irq(struct madera *madera, int irq, void *data)
{
	struct madera_irq_priv *priv;

	if (irq < 0)
		return;

	if (!madera->irq_dev)
		return;

	priv = dev_get_drvdata(madera->irq_dev);

	free_irq(regmap_irq_get_virq(priv->irq_data, irq), data);
}
EXPORT_SYMBOL_GPL(madera_free_irq);

int madera_set_irq_wake(struct madera *madera, int irq, int on)
{
	struct madera_irq_priv *priv;

	if (irq < 0)
		return irq;

	if (!madera->irq_dev)
		return -EINVAL;

	priv = dev_get_drvdata(madera->irq_dev);

	return irq_set_irq_wake(regmap_irq_get_virq(priv->irq_data, irq), on);
}
EXPORT_SYMBOL_GPL(madera_set_irq_wake);

static irqreturn_t madera_irq_thread(int irq, void *data)
{
	struct madera_irq_priv *priv = data;
	bool poll;
	int ret;

	dev_dbg(priv->dev, "irq_thread handler\n");

	/* The codec can generate IRQs while it is in low-power mode so
	 * we must do a runtime get before dispatching the IRQ
	 */
	ret = pm_runtime_get_sync(priv->madera->dev);
	if (ret < 0) {
		dev_err(priv->dev, "Failed to resume device: %d\n", ret);
		return IRQ_NONE;
	}

	do {
		poll = false;

		handle_nested_irq(irq_find_mapping(priv->domain, 0));

		/*
		 * Poll the IRQ pin status to see if we're really done
		 * if the interrupt controller can't do it for us.
		 */
		if (!gpio_is_valid(priv->irq_gpio)) {
			break;
		} else if (priv->irq_flags & IRQF_TRIGGER_RISING &&
			   gpio_get_value_cansleep(priv->irq_gpio)) {
			poll = true;
		} else if (priv->irq_flags & IRQF_TRIGGER_FALLING &&
			   !gpio_get_value_cansleep(priv->irq_gpio)) {
			poll = true;
		}
	} while (poll);

	pm_runtime_mark_last_busy(priv->madera->dev);
	pm_runtime_put_autosuspend(priv->madera->dev);

	return IRQ_HANDLED;
}

static void madera_irq_dummy(struct irq_data *data)
{
}

static int madera_irq_set_wake(struct irq_data *data, unsigned int on)
{
	struct madera_irq_priv *priv = irq_data_get_irq_chip_data(data);

	return irq_set_irq_wake(priv->irq, on);
}

static struct irq_chip madera_irq_chip = {
	.name		= "madera",
	.irq_disable	= madera_irq_dummy,
	.irq_enable	= madera_irq_dummy,
	.irq_ack	= madera_irq_dummy,
	.irq_mask	= madera_irq_dummy,
	.irq_unmask	= madera_irq_dummy,
	.irq_set_wake	= madera_irq_set_wake,
};

static struct lock_class_key madera_irq_lock_class;

static int madera_irq_map(struct irq_domain *h, unsigned int virq,
			  irq_hw_number_t hw)
{
	struct madera_irq_priv *priv = h->host_data;

	irq_set_chip_data(virq, priv);
	irq_set_lockdep_class(virq, &madera_irq_lock_class);
	irq_set_chip_and_handler(virq, &madera_irq_chip, handle_simple_irq);
	irq_set_nested_thread(virq, true);

	irq_set_noprobe(virq);

	return 0;
}

static const struct irq_domain_ops madera_domain_ops = {
	.map	= madera_irq_map,
	.xlate	= irq_domain_xlate_twocell,
};

#ifdef CONFIG_PM_SLEEP
static int madera_suspend_noirq(struct device *dev)
{
	struct madera_irq_priv *priv = dev_get_drvdata(dev);

	dev_info(priv->dev, "No IRQ suspend, reenabling IRQ\n");

	enable_irq(priv->irq);

	return 0;
}

static int madera_suspend(struct device *dev)
{
	struct madera_irq_priv *priv = dev_get_drvdata(dev);

	dev_info(priv->dev, "Suspend, disabling IRQ\n");

	disable_irq(priv->irq);

	return 0;
}

static int madera_resume_noirq(struct device *dev)
{
	struct madera_irq_priv *priv = dev_get_drvdata(dev);

	dev_info(priv->dev, "No IRQ resume, disabling IRQ\n");

	disable_irq(priv->irq);

	return 0;
}

static int madera_resume(struct device *dev)
{
	struct madera_irq_priv *priv = dev_get_drvdata(dev);

	dev_info(priv->dev, "Resume, reenabling IRQ\n");

	enable_irq(priv->irq);

	return 0;
}
#endif

static const struct dev_pm_ops madera_irq_pm_ops = {
	SET_SYSTEM_SLEEP_PM_OPS(madera_suspend, madera_resume)
	SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(madera_suspend_noirq,
				      madera_resume_noirq)
};

static int madera_irq_of_get(struct madera_irq_priv *priv)
{
	struct madera *madera = priv->madera;
	struct device_node *np = priv->madera->dev->of_node;
	u32 value;
	int ret;

	ret = of_property_read_u32(np, "cirrus,irq-flags", &value);
	if (ret == 0)
		madera->pdata.irqchip.irq_flags = value;

	madera->pdata.irqchip.irq_gpio = of_get_named_gpio(np,
							   "cirrus,irq-gpios",
							   0);

	return 0;
}

static int madera_irq_probe(struct platform_device *pdev)
{
	struct madera *madera = dev_get_drvdata(pdev->dev.parent);
	struct madera_irq_priv *priv;
	const struct regmap_irq_chip *irq = NULL;
	struct irq_data *irq_data;
	unsigned int virq;
	int flags = IRQF_ONESHOT;
	int ret;

	dev_dbg(&pdev->dev, "probe\n");

	priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
	if (!priv)
		return -ENOMEM;

	priv->dev = &pdev->dev;
	priv->madera = madera;

	switch (madera->type) {
	case CS47L15:
		if (IS_ENABLED(CONFIG_MADERA_IRQ_CS47L15))
			irq = &cs47l15_irq;
		break;
	case CS47L35:
		if (IS_ENABLED(CONFIG_MADERA_IRQ_CS47L35))
			irq = &cs47l35_irq;
		break;
	case CS47L85:
	case WM1840:
		if (IS_ENABLED(CONFIG_MADERA_IRQ_CS47L85))
			irq = &cs47l85_irq;
		break;
	case CS47L90:
	case CS47L91:
		if (IS_ENABLED(CONFIG_MADERA_IRQ_CS47L90))
			irq = &cs47l90_irq;
		break;
	case CS47L92:
	case CS47L93:
		if (IS_ENABLED(CONFIG_MADERA_IRQ_CS47L92))
			irq = &cs47l92_irq;
		break;
	default:
		dev_err(madera->dev, "Unsupported Madera device type %d\n",
			madera->type);
		return -EINVAL;
	}

	if (!irq) {
		dev_err(madera->dev, "No support for %s\n",
			madera_name_from_type(madera->type));
		return -EINVAL;
	}

	/* pdata uses 0 to mean undefined, convert to an invalid GPIO number */
	if (madera->pdata.irqchip.irq_gpio == 0)
		madera->pdata.irqchip.irq_gpio = -EINVAL;

	priv->irq = madera->irq;

	if (IS_ENABLED(CONFIG_OF)) {
		if (!dev_get_platdata(priv->dev)) {
			ret = madera_irq_of_get(priv);
			if (ret < 0)
				return ret;
		}
	}

	priv->irq_flags = madera->pdata.irqchip.irq_flags;
	priv->irq_gpio = madera->pdata.irqchip.irq_gpio;

	/* Read the flags from the interrupt controller if not specified */
	if (!priv->irq_flags) {
		irq_data = irq_get_irq_data(priv->irq);
		if (!irq_data) {
			dev_err(priv->dev, "Invalid IRQ: %d\n", priv->irq);
			return -EINVAL;
		}

		priv->irq_flags = irqd_get_trigger_type(irq_data);
		switch (priv->irq_flags) {
		case IRQF_TRIGGER_LOW:
		case IRQF_TRIGGER_HIGH:
		case IRQF_TRIGGER_RISING:
		case IRQF_TRIGGER_FALLING:
			break;

		case IRQ_TYPE_NONE:
		default:
			/* Device default */
			priv->irq_flags = IRQF_TRIGGER_LOW;
			break;
		}
	}

	flags |= priv->irq_flags;

	if (flags & (IRQF_TRIGGER_HIGH | IRQF_TRIGGER_RISING)) {
		ret = regmap_update_bits(madera->regmap, MADERA_IRQ1_CTRL,
					 MADERA_IRQ_POL_MASK, 0);
		if (ret) {
			dev_err(priv->dev,
				"Couldn't set IRQ polarity: %d\n", ret);
			return ret;
		}
	}


	if (flags & (IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING)) {
		/* A GPIO is recommended to emulate edge trigger */
		if (gpio_is_valid(priv->irq_gpio)) {
			if (gpio_to_irq(priv->irq_gpio) != priv->irq) {
				dev_warn(priv->dev,
					 "IRQ %d is not GPIO %d (%d)\n",
					 priv->irq, priv->irq_gpio,
					 gpio_to_irq(priv->irq_gpio));

				priv->irq = gpio_to_irq(priv->irq_gpio);
			}

			ret = devm_gpio_request_one(priv->dev, priv->irq_gpio,
						    GPIOF_IN, "madera IRQ");
			if (ret) {
				dev_err(priv->dev,
					"Failed to request IRQ GPIO %d: %d\n",
					priv->irq_gpio, ret);
				return ret;
			}
		}

		dev_dbg(priv->dev, "edge-trigger mode irq_gpio=%d\n",
			priv->irq_gpio);
	} else {
		dev_dbg(priv->dev, "level-trigger mode\n");
	}

	priv->domain = irq_domain_add_linear(NULL, 1, &madera_domain_ops, priv);

	virq = irq_create_mapping(priv->domain, 0);
	if (!virq) {
		dev_err(priv->dev, "Failed to map IRQs\n");
		ret = -EINVAL;
		goto err_domain;
	}

	ret = regmap_add_irq_chip(madera->regmap, virq, IRQF_ONESHOT, 0, irq,
				  &priv->irq_data);
	if (ret) {
		dev_err(priv->dev, "add_irq_chip failed: %d\n", ret);
		goto err_mapping;
	}

	ret = request_threaded_irq(priv->irq, NULL, madera_irq_thread,
				   flags, "madera", priv);
	if (ret) {
		dev_err(priv->dev,
			"Failed to request threaded irq %d: %d\n",
			priv->irq, ret);
		goto err_chip;
	}

	platform_set_drvdata(pdev, priv);
	madera->irq_dev = priv->dev;

	return 0;

err_chip:
	regmap_del_irq_chip(virq, priv->irq_data);
err_mapping:
	irq_dispose_mapping(virq);
err_domain:
	irq_domain_remove(priv->domain);

	return ret;
}

static int madera_irq_remove(struct platform_device *pdev)
{
	struct madera_irq_priv *priv = platform_get_drvdata(pdev);
	unsigned int virq;

	priv->madera->irq_dev = NULL;

	virq = irq_find_mapping(priv->domain, 0);
	regmap_del_irq_chip(virq, priv->irq_data);
	irq_dispose_mapping(virq);

	irq_domain_remove(priv->domain);

	free_irq(priv->irq, priv);

	return 0;
}

static struct platform_driver madera_irq_driver = {
	.probe = madera_irq_probe,
	.remove = madera_irq_remove,
	.driver = {
		.name	= "madera-irq",
		.pm = &madera_irq_pm_ops,
	}
};

module_platform_driver(madera_irq_driver);

MODULE_DESCRIPTION("Madera IRQ driver");
MODULE_AUTHOR("Richard Fitzgerald <rf@opensource.wolfsonmicro.com>");
MODULE_LICENSE("GPL v2");