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
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
|
/* drivers/mfd/s2mu003_irq.c
* S2MU003 Multifunction Device Driver
* Charger / Buck / LDOs / FlashLED
*
* Copyright (C) 2013 Richtek Technology Corp.
* Author: Patrick Chang <patrick_chang@richtek.com>
*
* 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; either version 2
* of the License, or (at your option) any later version.
*/
#include <linux/mfd/samsung/s2mu003.h>
#include <linux/mfd/samsung/s2mu003_irq.h>
#include <linux/gpio.h>
#include <linux/device.h>
#define ALIAS_NAME "s2mu003_mfd_irq"
#define S2MU003_CHG_IRQ 0x60
#define S2MU003_LED_IRQ 0x66
#define S2MU003_PMIC_IRQ 0x68
#define S2MU003_OFF_EVENT 0x6B
#define S2MU003_CHG_IRQ_CTRL 0x63
#define S2MU003_CHG_IRQ1_CTRL S2MU003_CHG_IRQ_CTRL
#define S2MU003_CHG_IRQ2_CTRL (S2MU003_CHG_IRQ_CTRL + 1)
#define S2MU003_CHG_IRQ3_CTRL (S2MU003_CHG_IRQ_CTRL + 2)
#define S2MU003_LED_IRQ_CTRL 0x67
#define S2MU003_PMIC_IRQ_CTRL 0x69
static const char *s2mu003_irq_names[] = {
[S2MU003_EOC_IRQ] = S2MU003_EOC_IRQ_NAME,
[S2MU003_CINIR_IRQ] = S2MU003_CINIR_IRQ_NAME,
[S2MU003_BATP_IRQ] = S2MU003_BATP_IRQ_NAME,
[S2MU003_BATLV_IRQ] = S2MU003_BATLV_IRQ_NAME,
[S2MU003_TOPOFF_IRQ] = S2MU003_TOPOFF_IRQ_NAME,
[S2MU003_CINOVP_IRQ] = S2MU003_CINOVP_IRQ_NAME,
[S2MU003_CHGTSD_IRQ] = S2MU003_CHGTSD_IRQ_NAME,
[S2MU003_CHGVINVR_IRQ] = S2MU003_CHGVINVR_IRQ_NAME,
[S2MU003_CHGTR_IRQ] = S2MU003_CHGTR_IRQ_NAME,
[S2MU003_TMROUT_IRQ] = S2MU003_TMROUT_IRQ_NAME,
[S2MU003_RECHG_IRQ] = S2MU003_RECHG_IRQ_NAME,
[S2MU003_CHGTERM_IRQ] = S2MU003_CHGTERM_IRQ_NAME,
[S2MU003_BATOVP_IRQ] = S2MU003_BATOVP_IRQ_NAME,
[S2MU003_CHGVIN_IRQ] = S2MU003_CHGVIN_IRQ_NAME,
[S2MU003_CIN2VAT_IRQ] = S2MU003_CIN2VAT_IRQ_NAME,
[S2MU003_CHGSTS_IRQ] = S2MU003_CHGSTS_IRQ_NAME,
[S2MU003_OTGILIM_IRQ] = S2MU003_OTGILIM_IRQ_NAME,
[S2MU003_BSTINLV_IRQ] = S2MU003_BSTINLV_IRQ_NAME,
[S2MU003_BSTILIM_IRQ] = S2MU003_BSTILIM_IRQ_NAME,
[S2MU003_VMIDOVP_IRQ] = S2MU003_VMIDOVP_IRQ_NAME,
[S2MU003_LBPROT_IRQ] = S2MU003_LBPROT_IRQ_NAME,
[S2MU003_OPEN_CH2_IRQ] = S2MU003_OPEN_CH2_IRQ_NAME,
[S2MU003_OPEN_CH1_IRQ] = S2MU003_OPEN_CH1_IRQ_NAME,
[S2MU003_SHORT_CH2_IRQ] = S2MU003_SHORT_CH2_IRQ_NAME,
[S2MU003_SHORT_CH1_IRQ] = S2MU003_SHORT_CH1_IRQ_NAME,
[S2MU003_WDT_IRQ] = S2MU003_WDT_IRQ_NAME,
[S2MU003_TSD_IRQ] = S2MU003_TSD_IRQ_NAME,
[S2MU003_CPWRLV_IRQ] = S2MU003_CPWRLV_IRQ_NAME,
};
const char *s2mu003_get_irq_name_by_index(int index)
{
return s2mu003_irq_names[index];
}
EXPORT_SYMBOL(s2mu003_get_irq_name_by_index);
enum S2MU003_IRQ_OFFSET {
S2MU003_CHG_IRQ_OFFSET = 0,
S2MU003_CHG_IRQ1_OFFSET = 0,
S2MU003_CHG_IRQ2_OFFSET,
S2MU003_CHG_IRQ3_OFFSET,
S2MU003_LED_IRQ_OFFSET,
S2MU003_PMIC_IRQ_OFFSET,
};
struct s2mu003_irq_data {
int mask;
int offset;
};
static const u8 s2mu003_mask_reg[] = {
S2MU003_CHG_IRQ1_CTRL,
S2MU003_CHG_IRQ2_CTRL,
S2MU003_CHG_IRQ3_CTRL,
S2MU003_LED_IRQ_CTRL,
S2MU003_PMIC_IRQ_CTRL,
};
static const struct s2mu003_irq_data s2mu003_irqs[] = {
[S2MU003_EOC_IRQ] = {
.offset = 0,
.mask = 1 << 1,
},
[S2MU003_CINIR_IRQ] = {
.offset = 0,
.mask = 1 << 2,
},
[S2MU003_BATP_IRQ] = {
.offset = 0,
.mask = 1 << 3,
},
[S2MU003_BATLV_IRQ] = {
.offset = 0,
.mask = 1 << 4,
},
[S2MU003_TOPOFF_IRQ] = {
.offset = 0,
.mask = 1 << 5,
},
[S2MU003_CINOVP_IRQ] = {
.offset = 0,
.mask = 1 << 6,
},
[S2MU003_CHGTSD_IRQ] = {
.offset = 0,
.mask = 1 << 7,
},
[S2MU003_CHGVINVR_IRQ] = {
.offset = 1,
.mask = 1 << 0,
},
[S2MU003_CHGTR_IRQ] = {
.offset = 1,
.mask = 1 << 1,
},
[S2MU003_TMROUT_IRQ] = {
.offset = 1,
.mask = 1 << 2,
},
[S2MU003_RECHG_IRQ] = {
.offset = 1,
.mask = 1 << 3,
},
[S2MU003_CHGTERM_IRQ] = {
.offset = 1,
.mask = 1 << 4,
},
[S2MU003_BATOVP_IRQ] = {
.offset = 1,
.mask = 1 << 5,
},
[S2MU003_CHGVIN_IRQ] = {
.offset = 1,
.mask = 1 << 6,
},
[S2MU003_CIN2VAT_IRQ] = {
.offset = 1,
.mask = 1 << 7,
},
[S2MU003_CHGSTS_IRQ] = {
.offset = 2,
.mask = 1 << 1,
},
[S2MU003_OTGILIM_IRQ] = {
.offset = 2,
.mask = 1 << 4,
},
[S2MU003_BSTINLV_IRQ] = {
.offset = 2,
.mask = 1 << 5,
},
[S2MU003_BSTILIM_IRQ] = {
.offset = 2,
.mask = 1 << 6,
},
[S2MU003_VMIDOVP_IRQ] = {
.offset = 2,
.mask = 1 << 7,
},
[S2MU003_LBPROT_IRQ] = {
.offset = 0,
.mask = 1 << 2,
},
[S2MU003_OPEN_CH2_IRQ] = {
.offset = 0,
.mask = 1 << 4,
},
[S2MU003_OPEN_CH1_IRQ] = {
.offset = 0,
.mask = 1 << 5,
},
[S2MU003_SHORT_CH2_IRQ] = {
.offset = 0,
.mask = 1 << 6,
},
[S2MU003_SHORT_CH1_IRQ] = {
.offset = 0,
.mask = 1 << 7,
},
[S2MU003_WDT_IRQ] = {
.offset = 0,
.mask = 1 << 0,
},
[S2MU003_TSD_IRQ] = {
.offset = 0,
.mask = 1 << 6,
},
[S2MU003_CPWRLV_IRQ] = {
.offset = 0,
.mask = 1 << 7,
},
};
static void s2mu003_irq_lock(struct irq_data *data)
{
s2mu003_mfd_chip_t *chip = irq_get_chip_data(data->irq);
mutex_lock(&chip->irq_lock);
}
static void s2mu003_irq_sync_unlock(struct irq_data *data)
{
s2mu003_mfd_chip_t *chip = irq_get_chip_data(data->irq);
s2mu003_block_write_device(chip->i2c_client,
S2MU003_CHG_IRQ_CTRL,
S2MU003_CHG_IRQ_REGS_NR,
chip->irq_masks_cache +
S2MU003_CHG_IRQ_OFFSET);
s2mu003_block_write_device(chip->i2c_client,
S2MU003_LED_IRQ_CTRL,
S2MU003_LED_IRQ_REGS_NR,
chip->irq_masks_cache +
S2MU003_LED_IRQ_OFFSET);
s2mu003_block_write_device(chip->i2c_client,
S2MU003_PMIC_IRQ_CTRL,
S2MU003_PMIC_IRQ_REGS_NR,
chip->irq_masks_cache +
S2MU003_PMIC_IRQ_OFFSET);
mutex_unlock(&chip->irq_lock);
}
static const inline struct s2mu003_irq_data *
irq_to_s2mu003_irq(s2mu003_mfd_chip_t *chip, int irq)
{
return &s2mu003_irqs[irq - chip->irq_base];
}
static void s2mu003_irq_mask(struct irq_data *data)
{
s2mu003_mfd_chip_t *chip = irq_get_chip_data(data->irq);
const struct s2mu003_irq_data *irq_data = irq_to_s2mu003_irq(chip,
data->irq);
chip->irq_masks_cache[irq_data->offset] |= irq_data->mask;
}
static void s2mu003_irq_unmask(struct irq_data *data)
{
s2mu003_mfd_chip_t *chip = irq_get_chip_data(data->irq);
const struct s2mu003_irq_data *irq_data = irq_to_s2mu003_irq(chip,
data->irq);
chip->irq_masks_cache[irq_data->offset] &= ~irq_data->mask;
}
static struct irq_chip s2mu003_irq_chip = {
.name = "s2mu003",
.irq_bus_lock = s2mu003_irq_lock,
.irq_bus_sync_unlock = s2mu003_irq_sync_unlock,
.irq_mask = s2mu003_irq_mask,
.irq_unmask = s2mu003_irq_unmask,
};
s2mu003_irq_status_t *s2mu003_get_irq_status(s2mu003_mfd_chip_t *mfd_chip,
s2mu003_irq_status_sel_t sel)
{
int index;
switch (sel) {
case S2MU003_PREV_STATUS:
index = mfd_chip->irq_status_index^0x01;
break;
case S2MU003_NOW_STATUS:
default:
index = mfd_chip->irq_status_index;
}
return &mfd_chip->irq_status[index];
}
EXPORT_SYMBOL(s2mu003_get_irq_status);
static int s2mu003_read_irq_status(s2mu003_mfd_chip_t *chip)
{
int ret;
struct i2c_client *iic = chip->i2c_client;
s2mu003_irq_status_t *now_irq_status;
now_irq_status = s2mu003_get_irq_status(chip, S2MU003_NOW_STATUS);
ret = s2mu003_block_read_device(iic, S2MU003_CHG_IRQ,
sizeof(now_irq_status->chg_irq_status),
now_irq_status->chg_irq_status);
if (ret < 0) {
dev_err(chip->dev,
"Failed on reading CHG irq status\n");
return ret;
}
printk("charger irq = 0x%x 0x%x 0x%x\n", (int)now_irq_status->chg_irq_status[0],
(int)now_irq_status->chg_irq_status[1],
now_irq_status->chg_irq_status[2]);
ret = s2mu003_block_read_device(iic, S2MU003_LED_IRQ,
sizeof(now_irq_status->fled_irq_status),
now_irq_status->fled_irq_status);
if (ret < 0) {
dev_err(chip->dev,
"Failed on reading FlashLED irq status\n");
return ret;
}
printk("fled irq = 0x%x\n", (int)now_irq_status->fled_irq_status[0]);
ret = s2mu003_block_read_device(iic, S2MU003_PMIC_IRQ,
sizeof(now_irq_status->pmic_irq_status),
now_irq_status->pmic_irq_status);
if (ret < 0) {
dev_err(chip->dev,
"Failed on reading PMIC irq status\n");
return ret;
}
printk("regulator irq = 0x%x\n", (int)now_irq_status->pmic_irq_status[0]);
return 0;
}
static irqreturn_t s2mu003_irq_handler(int irq, void *data)
{
int ret;
int i;
s2mu003_mfd_chip_t *chip = data;
s2mu003_irq_status_t *status;
printk("S2MU003 IRQ triggered\n");
wake_lock_timeout(&chip->irq_wake_lock, msecs_to_jiffies(500));
ret = s2mu003_reg_read(chip->i2c_client, 0x64);
printk("S2MU003 IRQ 0x64 : %d\n", ret);
ret = s2mu003_read_irq_status(chip);
if (ret < 0) {
pr_err("%s :Error : can't read irq status (%d)\n",
__func__, ret);
return IRQ_HANDLED;
}
status = s2mu003_get_irq_status(chip, S2MU003_NOW_STATUS);
for (i = 0; i < S2MU003_IRQ_REGS_NR; i++)
status->regs[i] &= ~chip->irq_masks_cache[i];
for (i = 0; i < S2MU003_IRQS_NR; i++) {
if (status->regs[s2mu003_irqs[i].offset] & s2mu003_irqs[i].mask) {
pr_info("%s : Trigger IRQ %s, irq : %d \n",
__func__, s2mu003_get_irq_name_by_index(i),
chip->irq_base + i);
handle_nested_irq(chip->irq_base + i);
}
}
/* exchange irq index */
chip->irq_status_index ^= 0x01;
return IRQ_HANDLED;
}
static int s2mu003_irq_ctrl_regs[] = {
S2MU003_CHG_IRQ1_CTRL,
S2MU003_CHG_IRQ2_CTRL,
S2MU003_CHG_IRQ3_CTRL,
S2MU003_LED_IRQ_CTRL,
S2MU003_PMIC_IRQ_CTRL,
};
static uint8_t s2mu003_irqs_ctrl_mask_all_val[] = {
0xFF,
0xFF,
0xFF,
0xFF,
0xFF,
};
static int s2mu003_mask_all_irqs(struct i2c_client *iic)
{
int rc;
int i;
s2mu003_mfd_chip_t *chip = i2c_get_clientdata(iic);
for (i = 0; i < ARRAY_SIZE(s2mu003_irq_ctrl_regs); i++) {
rc = s2mu003_reg_write(iic, s2mu003_irq_ctrl_regs[i],
s2mu003_irqs_ctrl_mask_all_val[i]);
chip->irq_masks_cache[i] = s2mu003_irqs_ctrl_mask_all_val[i];
if (rc < 0) {
pr_info("Error : can't write reg[0x%x] = 0x%x\n",
s2mu003_irq_ctrl_regs[i],
s2mu003_irqs_ctrl_mask_all_val[i]);
return rc;
}
}
return 0;
}
static int s2mu003_irq_init_read(s2mu003_mfd_chip_t *chip)
{
int ret;
ret = s2mu003_read_irq_status(chip);
chip->irq_status_index ^= 0x01;
return ret;
}
int s2mu003_init_irq(s2mu003_mfd_chip_t *chip)
{
int i, ret, curr_irq;
ret = s2mu003_mask_all_irqs(chip->i2c_client);
if (ret < 0) {
pr_err("%s : Can't mask all irqs(%d)\n", __func__, ret);
goto err_mask_all_irqs;
}
s2mu003_reg_write(chip->i2c_client, 0x64, 0xC3);
s2mu003_irq_init_read(chip);
mutex_init(&chip->irq_lock);
/* Register with genirq */
for (i = 0; i < S2MU003_IRQS_NR; i++) {
curr_irq = i + chip->irq_base;
irq_set_chip_data(curr_irq, chip);
irq_set_chip_and_handler(curr_irq, &s2mu003_irq_chip,
handle_simple_irq);
irq_set_nested_thread(curr_irq, 1);
#ifdef CONFIG_ARM
set_irq_flags(curr_irq, IRQF_VALID);
#else
irq_set_noprobe(curr_irq);
#endif
}
ret = gpio_request(chip->pdata->irq_gpio, "s2mu003_mfd_irq");
if (ret < 0) {
pr_err("%s : Request GPIO %d failed\n",
__func__, (int)chip->pdata->irq_gpio);
goto err_gpio_request;
}
ret = gpio_direction_input(chip->pdata->irq_gpio);
if (ret < 0) {
pr_err("Set GPIO direction to input : failed\n");
goto err_set_gpio_input;
}
chip->irq = gpio_to_irq(chip->pdata->irq_gpio);
ret = request_threaded_irq(chip->irq, NULL, s2mu003_irq_handler,
/* IRQF_TRIGGER_FALLING */
IRQF_TRIGGER_FALLING | IRQF_ONESHOT | IRQF_NO_SUSPEND,
"s2mu003", chip);
if (ret < 0) {
pr_err("%s : Failed : request IRQ (%d)\n", __func__, ret);
goto err_request_irq;
}
ret = enable_irq_wake(chip->irq);
if (ret < 0) {
pr_info("%s : enable_irq_wake(%d) failed for (%d)\n",
__func__, chip->irq, ret);
}
return ret;
err_request_irq:
err_set_gpio_input:
gpio_free(chip->pdata->irq_gpio);
err_gpio_request:
for (curr_irq = chip->irq_base;
curr_irq < chip->irq_base + S2MU003_IRQS_NR;
curr_irq++) {
#ifdef CONFIG_ARM
set_irq_flags(curr_irq, 0);
#endif
irq_set_chip_and_handler(curr_irq, NULL, NULL);
irq_set_chip_data(curr_irq, NULL);
}
mutex_destroy(&chip->irq_lock);
err_mask_all_irqs:
return ret;
}
int s2mu003_exit_irq(s2mu003_mfd_chip_t *chip)
{
int curr_irq;
for (curr_irq = chip->irq_base;
curr_irq < chip->irq_base + S2MU003_IRQS_NR;
curr_irq++) {
#ifdef CONFIG_ARM
set_irq_flags(curr_irq, 0);
#endif
irq_set_chip_and_handler(curr_irq, NULL, NULL);
irq_set_chip_data(curr_irq, NULL);
}
if (chip->irq)
free_irq(chip->irq, chip);
mutex_destroy(&chip->irq_lock);
return 0;
}
|