summaryrefstree
path: root/kernel/drivers/mfd/s2mps20_irq.c
blob: 167a35885cf98c2773ea1112245a9ce09fc04bb6 (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
/*
 * s2mps20-irq.c - Interrupt controller support for S2MPS20
 *
 * Copyright (C) 2016 Samsung Electronics Co.Ltd
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * 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.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
*/

#include <linux/err.h>
#include <linux/irq.h>
#include <linux/interrupt.h>
#include <linux/gpio.h>
#include <linux/mfd/samsung/s2mps20.h>
#include <linux/mfd/samsung/s2mps20-regulator.h>

static const u8 s2mps20_mask_reg[] = {
	/* TODO: Need to check other INTMASK */
	[S2MPS20_PMIC_INT1] = S2MPS20_PMIC_REG_INT1M,
	[S2MPS20_PMIC_INT2] = S2MPS20_PMIC_REG_INT2M,
};

static struct i2c_client *get_i2c(struct s2mps20_dev *s2mps20,
				enum s2mps20_irq_source src)
{
	switch (src) {
	case S2MPS20_PMIC_INT1 ... S2MPS20_PMIC_INT2:
		return s2mps20->pmic;
	default:
		return ERR_PTR(-EINVAL);
	}
}

struct s2mps20_irq_data {
	int mask;
	enum s2mps20_irq_source group;
};

#define DECLARE_IRQ(idx, _group, _mask)		\
	[(idx)] = { .group = (_group), .mask = (_mask) }

static const struct s2mps20_irq_data s2mps20_irqs[] = {
	DECLARE_IRQ(S2MPS20_PMIC_IRQ_PWRONF_INT1,		S2MPS20_PMIC_INT1, 1 << 0),
	DECLARE_IRQ(S2MPS20_PMIC_IRQ_PWRONR_INT1,		S2MPS20_PMIC_INT1, 1 << 1),
	DECLARE_IRQ(S2MPS20_PMIC_IRQ_OC0_INT1,			S2MPS20_PMIC_INT1, 1 << 2),
	DECLARE_IRQ(S2MPS20_PMIC_IRQ_OC1_INT1,			S2MPS20_PMIC_INT1, 1 << 3),
	DECLARE_IRQ(S2MPS20_PMIC_IRQ_OC2_INT1,			S2MPS20_PMIC_INT1, 1 << 4),
	DECLARE_IRQ(S2MPS20_PMIC_IRQ_WTSR_INT1,			S2MPS20_PMIC_INT1, 1 << 5),
	DECLARE_IRQ(S2MPS20_PMIC_IRQ_ADCDONE_INT1,		S2MPS20_PMIC_INT1, 1 << 6),
	DECLARE_IRQ(S2MPS20_PMIC_IRQ_WRSTB_INT1,		S2MPS20_PMIC_INT1, 1 << 7),

	DECLARE_IRQ(S2MPS20_PMIC_IRQ_INT120C_INT2,		S2MPS20_PMIC_INT2, 1 << 0),
	DECLARE_IRQ(S2MPS20_PMIC_IRQ_INT140C_INT2,		S2MPS20_PMIC_INT2, 1 << 1),
	DECLARE_IRQ(S2MPS20_PMIC_IRQ_TSD_INT2,			S2MPS20_PMIC_INT2, 1 << 2),
	DECLARE_IRQ(S2MPS20_PMIC_IRQ_OVP_INT2,			S2MPS20_PMIC_INT2, 1 << 3),
	DECLARE_IRQ(S2MPS20_PMIC_IRQ_OCP_B1_INT2,		S2MPS20_PMIC_INT2, 1 << 4),
	DECLARE_IRQ(S2MPS20_PMIC_IRQ_OCP_B2_INT2,		S2MPS20_PMIC_INT2, 1 << 5),
	DECLARE_IRQ(S2MPS20_PMIC_IRQ_OCP_B3_INT2,		S2MPS20_PMIC_INT2, 1 << 6),
};

static void s2mps20_irq_lock(struct irq_data *data)
{
	struct s2mps20_dev *s2mps20 = irq_get_chip_data(data->irq);

	mutex_lock(&s2mps20->irqlock);
}

static void s2mps20_irq_sync_unlock(struct irq_data *data)
{
	struct s2mps20_dev *s2mps20 = irq_get_chip_data(data->irq);
	int i;

	for (i = 0; i < S2MPS20_IRQ_GROUP_NR; i++) {
		u8 mask_reg = s2mps20_mask_reg[i];
		struct i2c_client *i2c = get_i2c(s2mps20, i);

		if (mask_reg == S2MPS20_REG_INVALID ||
				IS_ERR_OR_NULL(i2c))
			continue;
		s2mps20->irq_masks_cache[i] = s2mps20->irq_masks_cur[i];

		s2mps20_write_reg(i2c, s2mps20_mask_reg[i],
				s2mps20->irq_masks_cur[i]);
	}

	mutex_unlock(&s2mps20->irqlock);
}

static const inline struct s2mps20_irq_data *
irq_to_s2mps20_irq(struct s2mps20_dev *s2mps20, int irq)
{
	return &s2mps20_irqs[irq - s2mps20->irq_base];
}

static void s2mps20_irq_mask(struct irq_data *data)
{
	struct s2mps20_dev *s2mps20 = irq_get_chip_data(data->irq);
	const struct s2mps20_irq_data *irq_data =
	    irq_to_s2mps20_irq(s2mps20, data->irq);

	if (irq_data->group >= S2MPS20_IRQ_GROUP_NR)
		return;

	s2mps20->irq_masks_cur[irq_data->group] |= irq_data->mask;
}

static void s2mps20_irq_unmask(struct irq_data *data)
{
	struct s2mps20_dev *s2mps20 = irq_get_chip_data(data->irq);
	const struct s2mps20_irq_data *irq_data =
	    irq_to_s2mps20_irq(s2mps20, data->irq);

	if (irq_data->group >= S2MPS20_IRQ_GROUP_NR)
		return;

	s2mps20->irq_masks_cur[irq_data->group] &= ~irq_data->mask;
}

static struct irq_chip s2mps20_irq_chip = {
	.name			= MFD_DEV_NAME,
	.irq_bus_lock		= s2mps20_irq_lock,
	.irq_bus_sync_unlock	= s2mps20_irq_sync_unlock,
	.irq_mask		= s2mps20_irq_mask,
	.irq_unmask		= s2mps20_irq_unmask,
};

static irqreturn_t s2mps20_irq_thread(int irq, void *data)
{
	struct s2mps20_dev *s2mps20 = data;
	u8 irq_reg[S2MPS20_IRQ_GROUP_NR] = {0};
	u8 irq_src;
	int i, ret;

	pr_debug("%s: irq gpio pre-state(0x%02x)\n", __func__,
				gpio_get_value(s2mps20->irq_gpio));

	ret = s2mps20_read_reg(s2mps20->i2c,
					S2MPS20_PMIC_REG_INTSRC, &irq_src);
	if (ret) {
		pr_err("%s:%s Failed to read interrupt source: %d\n",
			MFD_DEV_NAME, __func__, ret);
		return IRQ_NONE;
	}

	pr_info("%s: interrupt source(0x%02x)\n", __func__, irq_src);

	if (irq_src & S2MPS20_IRQSRC_PMIC) {

		/* W/A : internal clock on */
		/* enable interrupt registers read & clear */
		if (s2mps20->pmic_rev < 1)
			s2mps20_update_reg(s2mps20->i2c, S2MPS20_PMIC_REG_OTP_TEST,
				1 << MOSCEN_SHIFT, MOSCEN_MASK);

		/* PMIC_INT */
		ret = s2mps20_bulk_read(s2mps20->pmic, S2MPS20_PMIC_REG_INT1,
				S2MPS20_NUM_IRQ_PMIC_REGS, &irq_reg[S2MPS20_PMIC_INT1]);
		if (ret) {
			pr_err("%s:%s Failed to read pmic interrupt: %d\n",
				MFD_DEV_NAME, __func__, ret);
			return IRQ_NONE;
		}

		/* internal clock off */
		if (s2mps20->pmic_rev < 1)
			s2mps20_update_reg(s2mps20->i2c, S2MPS20_PMIC_REG_OTP_TEST,
				0 << MOSCEN_SHIFT, MOSCEN_MASK);


		pr_info("%s: pmic interrupt(0x%02x, 0x%02x)\n",
			 __func__, irq_reg[S2MPS20_PMIC_INT1], irq_reg[S2MPS20_PMIC_INT2]);
	}

	/* Apply masking */
	for (i = 0; i < S2MPS20_IRQ_GROUP_NR; i++)
		irq_reg[i] &= ~s2mps20->irq_masks_cur[i];

	/* Report */
	for (i = 0; i < S2MPS20_IRQ_NR; i++) {
		if (irq_reg[s2mps20_irqs[i].group] & s2mps20_irqs[i].mask)
			handle_nested_irq(s2mps20->irq_base + i);
	}

	return IRQ_HANDLED;
}

int s2mps20_irq_init(struct s2mps20_dev *s2mps20)
{
	int i;
	int ret;
	u8 i2c_data;
	int cur_irq;

	if (!s2mps20->irq_gpio) {
		dev_warn(s2mps20->dev, "No interrupt specified.\n");
		s2mps20->irq_base = 0;
		return 0;
	}

	if (!s2mps20->irq_base) {
		dev_err(s2mps20->dev, "No interrupt base specified.\n");
		return 0;
	}

	mutex_init(&s2mps20->irqlock);

	s2mps20->irq = gpio_to_irq(s2mps20->irq_gpio);
	pr_info("%s:%s irq=%d, irq->gpio=%d\n", MFD_DEV_NAME, __func__,
			s2mps20->irq, s2mps20->irq_gpio);

	ret = gpio_request(s2mps20->irq_gpio, "if_pmic_irq");
	if (ret) {
		dev_err(s2mps20->dev, "%s: failed requesting gpio %d\n",
			__func__, s2mps20->irq_gpio);
		return ret;
	}
	gpio_direction_input(s2mps20->irq_gpio);
	gpio_free(s2mps20->irq_gpio);

	/* Mask individual interrupt sources */
	for (i = 0; i < S2MPS20_IRQ_GROUP_NR; i++) {
		struct i2c_client *i2c;

		s2mps20->irq_masks_cur[i] = 0xff;
		s2mps20->irq_masks_cache[i] = 0xff;

		i2c = get_i2c(s2mps20, i);

		if (IS_ERR_OR_NULL(i2c))
			continue;
		if (s2mps20_mask_reg[i] == S2MPS20_REG_INVALID)
			continue;

		s2mps20_write_reg(i2c, s2mps20_mask_reg[i], 0xff);
	}

	/* Register with genirq */
	for (i = 0; i < S2MPS20_IRQ_NR; i++) {
		cur_irq = i + s2mps20->irq_base;
		irq_set_chip_data(cur_irq, s2mps20);
		irq_set_chip_and_handler(cur_irq, &s2mps20_irq_chip,
					 handle_level_irq);
		irq_set_nested_thread(cur_irq, 1);
#ifdef CONFIG_ARM
		set_irq_flags(cur_irq, IRQF_VALID);
#else
		irq_set_noprobe(cur_irq);
#endif
	}

	s2mps20_write_reg(s2mps20->i2c, S2MPS20_PMIC_REG_INTSRC_MASK, 0xff);
	/* Unmask s2mps20 interrupt */
	ret = s2mps20_read_reg(s2mps20->i2c, S2MPS20_PMIC_REG_INTSRC_MASK,
			  &i2c_data);
	if (ret) {
		pr_err("%s:%s fail to read intsrc mask reg\n",
					 MFD_DEV_NAME, __func__);
		return ret;
	}

	
	/* mask all PMIC interrupts in case of PMIC EVT0 */
	if (s2mps20->pmic_rev >= 1) {
		i2c_data &= ~(S2MPS20_IRQSRC_PMIC);	/* Unmask pmic interrupt */
		s2mps20_write_reg(s2mps20->i2c, S2MPS20_PMIC_REG_INTSRC_MASK, i2c_data);
	}

	pr_info("%s:%s s2mps20_PMIC_REG_INTSRC_MASK=0x%02x\n",
			MFD_DEV_NAME, __func__, i2c_data);

	ret = request_threaded_irq(s2mps20->irq, NULL, s2mps20_irq_thread,
				   IRQF_TRIGGER_LOW | IRQF_ONESHOT,
				   "s2mps20-irq", s2mps20);

	if (ret) {
		dev_err(s2mps20->dev, "Failed to request IRQ %d: %d\n",
			s2mps20->irq, ret);
		return ret;
	}

	return 0;
}

void s2mps20_irq_exit(struct s2mps20_dev *s2mps20)
{
	if (s2mps20->irq)
		free_irq(s2mps20->irq, s2mps20);
}