aboutsummaryrefslogtreecommitdiff
path: root/techpack/audio/asoc/codecs/wcd-irq.c
blob: 48495f553999a6310fbf34449366a09fbe06aa23 (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
/* Copyright (c) 2018, The Linux Foundation. All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 and
 * only version 2 as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/irq.h>
#include <linux/of.h>
#include <linux/of_irq.h>
#include <linux/slab.h>
#include <linux/irqdomain.h>
#include <linux/regmap.h>
#include <asoc/wcd-irq.h>

static int wcd_map_irq(struct wcd_irq_info *irq_info, int irq)
{
	if (!irq_info) {
		pr_err("%s: Null IRQ handle\n", __func__);
		return -EINVAL;
	}
	return regmap_irq_get_virq(irq_info->irq_chip, irq);
}

/**
 * wcd_request_irq: Request a thread handler for the given IRQ
 * @irq_info: pointer to IRQ info structure
 * @irq: irq number
 * @name: name for the IRQ thread
 * @handler: irq handler
 * @data: data pointer
 *
 * Returns 0 on success or error on failure
 */
int wcd_request_irq(struct wcd_irq_info *irq_info, int irq, const char *name,
			irq_handler_t handler, void *data)
{
	if (!irq_info) {
		pr_err("%s: Null IRQ handle\n", __func__);
		return -EINVAL;
	}
	irq = wcd_map_irq(irq_info, irq);
	if (irq < 0)
		return irq;

	return request_threaded_irq(irq, NULL, handler,
				    IRQF_ONESHOT | IRQF_TRIGGER_RISING,
				    name, data);
}
EXPORT_SYMBOL(wcd_request_irq);

/**
 * wcd_free_irq: Free the IRQ resources allocated during request_irq
 * @irq_info: pointer to IRQ info structure
 * @irq: irq number
 * @data: data pointer
 */
void wcd_free_irq(struct wcd_irq_info *irq_info, int irq, void *data)
{
	if (!irq_info) {
		pr_err("%s: Null IRQ handle\n", __func__);
		return;
	}

	irq = wcd_map_irq(irq_info, irq);
	if (irq < 0)
		return;

	free_irq(irq, data);
}
EXPORT_SYMBOL(wcd_free_irq);

/**
 * wcd_enable_irq: Enable the given IRQ
 * @irq_info: pointer to IRQ info structure
 * @irq: irq number
 */
void wcd_enable_irq(struct wcd_irq_info *irq_info, int irq)
{
	if (!irq_info)
		pr_err("%s: Null IRQ handle\n", __func__);
	else
		enable_irq(wcd_map_irq(irq_info, irq));
}
EXPORT_SYMBOL(wcd_enable_irq);

/**
 * wcd_disable_irq: Disable the given IRQ
 * @irq_info: pointer to IRQ info structure
 * @irq: irq number
 */
void wcd_disable_irq(struct wcd_irq_info *irq_info, int irq)
{
	if (!irq_info)
		pr_err("%s: Null IRQ handle\n", __func__);
	else
		disable_irq_nosync(wcd_map_irq(irq_info, irq));
}
EXPORT_SYMBOL(wcd_disable_irq);

static void wcd_irq_chip_disable(struct irq_data *data)
{
}

static void wcd_irq_chip_enable(struct irq_data *data)
{
}

static struct irq_chip wcd_irq_chip = {
	.name = NULL,
	.irq_disable = wcd_irq_chip_disable,
	.irq_enable = wcd_irq_chip_enable,
};

static struct lock_class_key wcd_irq_lock_class;

static int wcd_irq_chip_map(struct irq_domain *irqd, unsigned int virq,
			irq_hw_number_t hw)
{
	irq_set_chip_and_handler(virq, &wcd_irq_chip, handle_simple_irq);
	irq_set_lockdep_class(virq, &wcd_irq_lock_class);
	irq_set_nested_thread(virq, 1);
	irq_set_noprobe(virq);

	return 0;
}

static const struct irq_domain_ops wcd_domain_ops = {
	.map = wcd_irq_chip_map,
};

/**
 * wcd_irq_init: Initializes IRQ module
 * @irq_info: pointer to IRQ info structure
 *
 * Returns 0 on success or error on failure
 */
int wcd_irq_init(struct wcd_irq_info *irq_info, struct irq_domain **virq)
{
	int ret = 0;

	if (!irq_info) {
		pr_err("%s: Null IRQ handle\n", __func__);
		return -EINVAL;
	}

	wcd_irq_chip.name = irq_info->codec_name;

	*virq = irq_domain_add_linear(NULL, 1, &wcd_domain_ops, NULL);
	if (!(*virq)) {
		pr_err("%s: Failed to add IRQ domain\n", __func__);
		return -EINVAL;
	}

	ret = devm_regmap_add_irq_chip(irq_info->dev, irq_info->regmap,
				 irq_create_mapping(*virq, 0),
				 IRQF_ONESHOT, 0, irq_info->wcd_regmap_irq_chip,
				 &irq_info->irq_chip);
	if (ret)
		pr_err("%s: Failed to add IRQs: %d\n",
			__func__, ret);

	return ret;
}
EXPORT_SYMBOL(wcd_irq_init);

/**
 * wcd_irq_exit: Uninitialize regmap IRQ and free IRQ resources
 * @irq_info: pointer to IRQ info structure
 *
 * Returns 0 on success or error on failure
 */
int wcd_irq_exit(struct wcd_irq_info *irq_info, struct irq_domain *virq)
{
	if (!irq_info) {
		pr_err("%s: Null pointer handle\n", __func__);
		return -EINVAL;
	}

	regmap_del_irq_chip(irq_find_mapping(virq, 0), irq_info->irq_chip);

	return 0;
}
EXPORT_SYMBOL(wcd_irq_exit);