summaryrefslogtreecommitdiff
path: root/bsp/allwinner/libraries/sunxi-hal/hal/source/tpadc/hal_tpadc.c
blob: fa91a8a858f93508f70913b2ca71fd8092e6cee4 (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
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
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
/*
 * ===========================================================================================
 *
 *       Filename:  hal_tpadc.c
 *
 *    Description:  tpadc hal layer code
 *
 *        Version:  Melis3.0
 *         Create:  2020-1-14
 *       Revision:  none
 *       Compiler:  GCC:version 9.2.1 20170904 (release),ARM/embedded-7-branch revision 255204
 *
 *         Author:  [email protected]
 *   Organization:  SWC-BPD
 *  Last Modified:  2021-1-14
 *
 * ===========================================================================================
 */
#include <hal_interrupt.h>
#include <hal_clk.h>
#include <hal_reset.h>
#include "sunxi_hal_tpadc.h"

hal_tpadc_t hal_tpadc;

static void sunxi_flush_fifo(unsigned long reg_base)
{
    uint32_t reg_val;

    reg_val = readl(reg_base + TP_INT_FIFOC);
    reg_val  = SET_BITS(TP_FIFO_FLUSH, TP_FIFO_FLUSH_WIDTH,reg_val, 0x1);

    writel(reg_val, reg_base + TP_INT_FIFOC);
}

static void sunxi_clear_fifo_status(unsigned long reg_base)
{
    uint32_t reg_val;

    reg_val = readl(reg_base + TP_INT_FIFOS);
    reg_val  = SET_BITS(FIFO_OVERRUN_PENDING, FIFO_OVERRUN_PENDIN_WIDTH,
                reg_val, 0x0);
    reg_val  = SET_BITS(FIFO_DATA_PENDING, FIFO_DATA_PENDING_WIDTH,
                reg_val, 0x0);

    writel(reg_val, reg_base + TP_INT_FIFOS);
}

static void sunxi_set_acqiure_time(unsigned long reg_base, uint32_t val)
{
    uint32_t reg_val;

    reg_val = readl(reg_base + TP_CTRL0);
    reg_val  = SET_BITS(TACQ, TACQ_WIDTH, reg_val, val);

    writel(reg_val, reg_base + TP_CTRL0);
}

static void sunxi_set_frequency_divider(unsigned long reg_base, uint32_t val)
{
    uint32_t reg_val;

    reg_val = readl(reg_base + TP_CTRL0);
    reg_val  = SET_BITS(FS_DIV, FS_DIV_WIDTH, reg_val, val);

    writel(reg_val, reg_base + TP_CTRL0);
}

static void sunxi_set_clk_divider(unsigned long reg_base, uint32_t val)
{
    uint32_t reg_val;

    reg_val = readl(reg_base + TP_CTRL0);
    reg_val  = SET_BITS(ADC_CLK_DIVIDER, ADC_CLK_DIVIDER_WIDTH, reg_val, val);

    writel(reg_val, reg_base + TP_CTRL0);
}

static void sunxi_select_delay_mode(unsigned long reg_base, uint32_t val)
{
    uint32_t reg_val;

    reg_val = readl(reg_base + TP_CTRL0);
    reg_val  = SET_BITS(ADC_FIRST_DLY_MODE, ADC_FIRST_DLY_MODE_WIDTH,reg_val, val);

    writel(reg_val, reg_base + TP_CTRL0);
}

static void sunxi_set_dealy_time(unsigned long reg_base, uint32_t val)
{
    uint32_t reg_val;

    reg_val = readl(reg_base + TP_CTRL0);
    reg_val  = SET_BITS(ADC_FIRST_DLY, ADC_FIRST_DLY_WIDTH,reg_val, val);

    writel(reg_val, reg_base + TP_CTRL0);
}

static void sunxi_clk_init(unsigned long reg_base)
{
    sunxi_set_acqiure_time(reg_base, 0x02); //2us
    sunxi_set_frequency_divider(reg_base, 0xb);
    sunxi_set_clk_divider(reg_base, 0x3);
    sunxi_select_delay_mode(reg_base, 0x1);
    sunxi_set_dealy_time(reg_base, 0xf);
}

static uint32_t sunxi_tpadc_adc_ch_select(unsigned long reg_base, tp_channel_id id)
{
    uint32_t reg_val;

    reg_val = readl(reg_base + TP_CTRL1);
    switch (id) {
    case TP_CH_0:
        reg_val |= TP_CH0_SELECT;
        break;
    case TP_CH_1:
        reg_val |= TP_CH1_SELECT;
        break;
    case TP_CH_2:
        reg_val |= TP_CH2_SELECT;
        break;
    case TP_CH_3:
        reg_val |= TP_CH3_SELECT;
        break;
    default:
        TPADC_ERR("%s, invalid channel id!", __func__);
        return TPADC_ERROR;
    }
    writel(reg_val, reg_base + TP_CTRL1);

    return 0;
}

/* 0:TAPADC  1: ADC*/
static void sunxi_tpadc_mode_select(unsigned long reg_base, uint32_t val)
{
    uint32_t reg_val;

    reg_val = readl(reg_base + TP_CTRL1);
    reg_val  = SET_BITS(TP_MODE_SELECT, TP_MODE_SELECT_WIDTH,reg_val, val);

    writel(reg_val, reg_base + TP_CTRL1);
}


static void sunxi_tpadc_enable(unsigned long reg_base, uint32_t val)
{
    uint32_t reg_val;

    reg_val = readl(reg_base + TP_CTRL1);
    reg_val  = SET_BITS(TP_EN, TP_EN_WIDTH, reg_val, val);

    writel(reg_val, reg_base + TP_CTRL1);
}

static void sunxi_set_up_debou_time(unsigned long reg_base, uint32_t val)
{
    uint32_t reg_val;

    reg_val = readl(reg_base + TP_CTRL1);
    reg_val  = SET_BITS(TP_DEBOUNCE, TP_DEBOUNCE_WIDTH, reg_val, val);

    writel(reg_val, reg_base + TP_CTRL1);
}

static void sunxi_set_pressure_thresholed(unsigned long reg_base, uint32_t val)
{
    uint32_t reg_val;

    reg_val = readl(reg_base + TP_CTRL2);
    reg_val  = SET_BITS(PRE_MEA, PRE_MEA_WIDTH, reg_val, val);

    writel(reg_val, reg_base + TP_CTRL2);
}

static void sunxi_pressure_enable(unsigned long reg_base)
{
    uint32_t reg_val;

    reg_val = readl(reg_base + TP_CTRL2);
    reg_val  = SET_BITS(PRE_MEA_EN, PRE_MEA_EN_WIDTH, reg_val, 0x1);

    writel(reg_val, reg_base + TP_CTRL2);
}

/* 0: x_data y_data x_data y_data */
static void sunxi_tp_mode_fifo_select(unsigned long reg_base, uint32_t val)
{
    uint32_t reg_val;

    reg_val = readl(reg_base + TP_CTRL2);
    reg_val  = SET_BITS(TP_FIFO_MODE, TP_FIFO_MODE_WIDTH, reg_val, val);

    writel(reg_val, reg_base + TP_CTRL2);
}

static void sunxi_set_sensitivity(unsigned long reg_base, uint32_t val)
{
    uint32_t reg_val;

    reg_val = readl(reg_base + TP_CTRL2);
    reg_val  = SET_BITS(TP_SENSITIVE, TP_SENSITIVE_WIDTH, reg_val, val);

    writel(reg_val, reg_base + TP_CTRL2);
}

static void sunxi_set_filter_type(unsigned long reg_base, uint32_t val)
{
    uint32_t reg_val;

    reg_val = readl(reg_base + TP_CTRL3);
    reg_val  = SET_BITS(FILTER_TYPE, FILTER_TYPE_WIDTH, reg_val, val);

    writel(reg_val, reg_base + TP_CTRL3);
}
static void sunxi_filter_enable(unsigned long reg_base)
{
    uint32_t reg_val;

    reg_val = readl(reg_base + TP_CTRL3);
    reg_val  = SET_BITS(FILTER_EN, FILTER_EN_WIDTH, reg_val, 0x1);

    writel(reg_val, reg_base + TP_CTRL3);
}

static void sunxi_downirq_enable(unsigned long reg_base)
{
    uint32_t reg_val;

    reg_val = readl(reg_base + TP_INT_FIFOC);
    reg_val  = SET_BITS(TP_DOWN_IRQ_EN, TP_DOWN_IRQ_ENWIDTH, reg_val, 0x1);

    writel(reg_val, reg_base + TP_INT_FIFOC);
}

static void sunxi_upirq_enable(unsigned long reg_base)
{
    uint32_t reg_val;

    reg_val = readl(reg_base + TP_INT_FIFOC);
    reg_val  = SET_BITS(TP_UP_IRQ_EN, TP_UP_IRQ_EN_WIDTH, reg_val, 0x1);

    writel(reg_val, reg_base + TP_INT_FIFOC);
}

static void sunxi_drq_enable(unsigned long reg_base)
{
    uint32_t reg_val;

    reg_val = readl(reg_base + TP_INT_FIFOC);
    reg_val  = SET_BITS(TP_DATA_DRQ_EN, TP_DATA_DRQ_EN_WIDTH, reg_val, 0x1);

    writel(reg_val, reg_base + TP_INT_FIFOC);
}

static void sunxi_set_trig_level(unsigned long reg_base, uint32_t val)
{
    uint32_t reg_val;

    reg_val = readl(reg_base + TP_INT_FIFOC);
    reg_val  = SET_BITS(TP_FIFO_TRIG, TP_FIFO_TRIG_WIDTH, reg_val, val);

    writel(reg_val, reg_base + TP_INT_FIFOC);
}

static void sunxi_xydata_change_enable(unsigned long reg_base, uint32_t val)
{
    uint32_t reg_val;

    reg_val = readl(reg_base + TP_INT_FIFOC);
    reg_val = SET_BITS(TP_DATA_XY_CHANGE, TP_DATA_XY_CHANGE_WIDTH, reg_val, val);

    writel(reg_val, reg_base + TP_INT_FIFOC);
}

static void sunxi_irq_enable(unsigned long reg_base)
{
    uint32_t reg_val;

    reg_val = readl(reg_base + TP_INT_FIFOC);
    reg_val = SET_BITS(TP_DATA_IRQ_EN, TP_DATA_IRQ_EN_WIDTH, reg_val, 0x1);

    writel(reg_val, reg_base + TP_INT_FIFOC);
}

static void sunxi_irq_disable(unsigned long reg_base)
{
    uint32_t reg_val;

    reg_val = readl(reg_base + TP_INT_FIFOC);
    reg_val = SET_BITS(TP_DATA_IRQ_EN, TP_DATA_IRQ_EN_WIDTH, reg_val, 0x0);

    writel(reg_val, reg_base + TP_INT_FIFOC);
}

static void sunxi_overrun_enable(unsigned long reg_base)
{
    uint32_t reg_val;

    reg_val = readl(reg_base + TP_INT_FIFOC);
    reg_val = SET_BITS(TP_FIFO_OVERRUN_IRQ, TP_FIFO_OVERRUN_IRQ_WIDTH, reg_val, 0x1);

    writel(reg_val, reg_base + TP_INT_FIFOC);
}

static u32 sunxi_tpadc_irq_status(unsigned long reg_base)
{
    return readl(reg_base + TP_INT_FIFOS);
}

static void sunxi_tpadc_clear_pending(unsigned long reg_base)
{
    int reg;

    reg = readl(reg_base + TP_INT_FIFOS);
    writel(reg, reg_base + TP_INT_FIFOS);
}

static u32 sunxi_tpadc_ch_select(unsigned long reg_base, tp_channel_id id)
{
    u32 reg_val;

    reg_val = readl(reg_base + TP_CTRL1);
    switch (id) {
    case TP_CH_0:
        reg_val |= TP_CH0_SELECT;
        break;
    case TP_CH_1:
        reg_val |= TP_CH1_SELECT;
        break;
    case TP_CH_2:
        reg_val |= TP_CH2_SELECT;
        break;
    case TP_CH_3:
        reg_val |= TP_CH3_SELECT;
        break;
    default:
        TPADC_ERR("%s, invalid channel id!", __func__);
        return -1;
    }
    writel(reg_val, reg_base + TP_CTRL1);

    return 0;
}

static int sunxi_tpadc_ch_deselect(unsigned long reg_base, tp_channel_id id)
{
    u32 reg_val;

    reg_val = readl(reg_base + TP_CTRL1);
    switch (id) {
    case TP_CH_0:
        reg_val &= ~TP_CH0_SELECT;
        break;
    case TP_CH_1:
        reg_val &= ~TP_CH1_SELECT;
        break;
    case TP_CH_2:
        reg_val &= ~TP_CH2_SELECT;
        break;
    case TP_CH_3:
        reg_val &= ~TP_CH3_SELECT;
        break;
    default:
        TPADC_ERR("%s, invalid channel id!", __func__);
        return -1;
    }
    writel(reg_val, reg_base + TP_CTRL1);

    return 0;
}

static u32 hal_tpadc_data_read(unsigned long reg_base)
{
    hal_tpadc_t *tpadc = &hal_tpadc;

    return readl(reg_base + TP_DATA);
}

static int hal_tpadc_callback(uint32_t data, data_flag_t flag)
{
    TPADC_INFO("tpadc : tpadc mode interrupt, data_flag is %ld", flag);
    return 0;
}

static int hal_tpadc_adc_callback(uint32_t data, tp_channel_id channel)
{
    TPADC_INFO("tpadc : auxiliary adc mode interrupt");
    return 0;
}

static void sunxi_tpadc_setup(hal_tpadc_t *tpadc)
{
    u32 i;

    tpadc->reg_base = (unsigned long)TPADC_BASE;
    tpadc->irq_num = SUNXI_TPADC_IRQ;
    tpadc->rst_clk_id = (hal_reset_id_t)RST_BUS_TPADC;
    tpadc->bus_clk_id = (hal_clk_id_t)CLK_BUS_TPADC;
    tpadc->mod_clk_id = (hal_clk_id_t)CLK_TPADC;

    tpadc->callback = hal_tpadc_callback;
    for(i=0; i<TP_CH_MAX; i++)
        tpadc->adc_callback[i] = hal_tpadc_adc_callback;
}

static hal_tpadc_status_t hal_tpadc_clk_init(hal_tpadc_t *tpadc)
{
    hal_clk_type_t  clk_type = HAL_SUNXI_CCU;
    hal_clk_id_t bus_clk_id = tpadc->bus_clk_id;
    hal_clk_id_t mod_clk_id = tpadc->mod_clk_id;

    hal_reset_type_t reset_type = HAL_SUNXI_RESET;
    hal_reset_id_t  tpadc_reset_id = tpadc->rst_clk_id;
    struct reset_control *reset;

    reset = hal_reset_control_get(reset_type, tpadc_reset_id);
    if (hal_reset_control_deassert(reset))
    {
        TPADC_ERR("tpadc reset deassert  failed!");
        return TPADC_ERROR;
    }
    hal_reset_control_put(reset);

    tpadc->mod_clk = hal_clock_get(clk_type, mod_clk_id);
    if (hal_clock_enable(tpadc->mod_clk))
    {
        TPADC_ERR("tpadc mod clk enable mclk failed!");
        return TPADC_ERROR;
    }

    tpadc->bus_clk = hal_clock_get(clk_type, bus_clk_id);
    if (hal_clock_enable(tpadc->bus_clk))
    {
        TPADC_ERR("tpadc bus clk enable mclk failed!");
        return TPADC_ERROR;
    }

    return TPADC_OK;
}

static hal_tpadc_status_t hal_tpadc_clk_exit(hal_tpadc_t *tpadc)
{
    hal_clock_disable(tpadc->bus_clk);
    hal_clock_put(tpadc->bus_clk);

    hal_clock_disable(tpadc->mod_clk);
    hal_clock_put(tpadc->mod_clk);

    return TPADC_OK;
}

/* the rtpadc tpadc mode interface */
static irqreturn_t tpadc_handler(int irq, void *dev)
{
    hal_tpadc_t *tpadc = (hal_tpadc_t *)dev;
    u32  reg_val;
    u32 x_data, y_data;
    u32 i;

    reg_val = sunxi_tpadc_irq_status(tpadc->reg_base);
    if (reg_val & TP_DATAPEND) {
        x_data = hal_tpadc_data_read(tpadc->reg_base);
        tpadc->callback(x_data, DATA_X);
        y_data = hal_tpadc_data_read(tpadc->reg_base);
        tpadc->callback(y_data, DATA_Y);
    }

    if (reg_val & TP_UPPEND) {
        tpadc->callback(0, DATA_UP);
    }

    if (reg_val & TP_DOWNPEND) {
        TPADC_INFO("touch down ");
    }

    sunxi_tpadc_clear_pending(tpadc->reg_base);

    return TPADC_OK;
}

hal_tpadc_status_t hal_tpadc_register_callback(tpadc_usercallback_t user_callback)
{
    hal_tpadc_t *tpadc = &hal_tpadc;

    if (user_callback == NULL)
        return TPADC_ERROR;
    else
        tpadc->callback = user_callback;

    return TPADC_OK;
}

hal_tpadc_status_t hal_tpadc_init(void)
{
    hal_tpadc_t *tpadc = &hal_tpadc;
    u32 clk_in;

    sunxi_tpadc_setup(tpadc);

    if (hal_tpadc_clk_init(tpadc))
    {
        TPADC_ERR("tpadc init clk error");
        return TPADC_ERROR;
    }

    /* clear status */
    sunxi_flush_fifo(tpadc->reg_base);
    sunxi_clear_fifo_status(tpadc->reg_base);

    sunxi_clk_init(tpadc->reg_base);

    sunxi_tpadc_mode_select(tpadc->reg_base, 0);
    sunxi_tpadc_enable(tpadc->reg_base, 1);
    sunxi_set_up_debou_time(tpadc->reg_base, 0x00);

    sunxi_set_pressure_thresholed(tpadc->reg_base, 0x800fff);
    sunxi_pressure_enable(tpadc->reg_base);
    sunxi_tp_mode_fifo_select(tpadc->reg_base, 0x00);
    sunxi_set_sensitivity(tpadc->reg_base, 0xf);

    sunxi_set_filter_type(tpadc->reg_base, 0x1);
    sunxi_filter_enable(tpadc->reg_base);

    sunxi_downirq_enable(tpadc->reg_base);
    sunxi_upirq_enable(tpadc->reg_base);
    sunxi_drq_enable(tpadc->reg_base);
    sunxi_set_trig_level(tpadc->reg_base, 0x1);
    sunxi_xydata_change_enable(tpadc->reg_base, 0);
    sunxi_irq_enable(tpadc->reg_base);
    sunxi_overrun_enable(tpadc->reg_base);

    sunxi_tpadc_clear_pending(tpadc->reg_base);

    if (request_irq(tpadc->irq_num, tpadc_handler, 0, "tpadc", tpadc))
    {
        TPADC_ERR("tpadc request irq(%d) failed \n", tpadc->irq_num);
    return TPADC_ERROR;
    }

    enable_irq(tpadc->irq_num);

    TPADC_INFO("tpadc init success");

    return TPADC_OK;
}

hal_tpadc_status_t hal_tpadc_exit(void)
{
    hal_tpadc_t *tpadc = &hal_tpadc;

    sunxi_irq_disable(tpadc->reg_base);
    free_irq(tpadc->irq_num, tpadc);
    hal_tpadc_clk_exit(tpadc);

    return TPADC_OK;
}

hal_tpadc_status_t hal_tpadc_resume(void)
{
    hal_tpadc_t *tpadc = &hal_tpadc;

    hal_tpadc_clk_init(tpadc);
    sunxi_tpadc_enable(tpadc->reg_base, 1);

    return TPADC_OK;
}

hal_tpadc_status_t hal_tpadc_suspend(void)
{
    hal_tpadc_t *tpadc = &hal_tpadc;

    sunxi_tpadc_enable(tpadc->reg_base, 0);
    hal_tpadc_clk_exit(tpadc);

    return TPADC_OK;
}

/* the rtpadc adc mode interface*/
static irqreturn_t tpadc_adc_handler(int irq, void *dev)
{
    hal_tpadc_t *tpadc = (hal_tpadc_t *)dev;
    u32 reg_val;
    u32 data;

    reg_val = sunxi_tpadc_irq_status(tpadc->reg_base);
    if (reg_val & TP_DATAPEND) {
        reg_val = readl(tpadc->reg_base + TP_CTRL1);
        if (reg_val & TP_CH0_SELECT)
        {
            data = hal_tpadc_data_read(tpadc->reg_base);
            if (tpadc->adc_callback[0])
                tpadc->adc_callback[0](data, TP_CH0_SELECT);
        }

        if (reg_val & TP_CH1_SELECT)
        {
            data = hal_tpadc_data_read(tpadc->reg_base);
            if (tpadc->adc_callback[1])
                tpadc->adc_callback[1](data, TP_CH1_SELECT);
        }

        if (reg_val & TP_CH2_SELECT)
        {
            data = hal_tpadc_data_read(tpadc->reg_base);
            if (tpadc->adc_callback[2])
                tpadc->adc_callback[2](data, TP_CH2_SELECT);
        }

        if (reg_val & TP_CH3_SELECT)
        {
            data = hal_tpadc_data_read(tpadc->reg_base);
            if (tpadc->adc_callback[3])
                tpadc->adc_callback[3](data, TP_CH3_SELECT);
        }

        if (reg_val & 0x0)
        {
            TPADC_ERR("no channel init, you need init 1~4 channel first");
            return -IRQ_HANDLED;
        }
    }

    sunxi_tpadc_clear_pending(tpadc->reg_base);

    return IRQ_NONE;
}

hal_tpadc_status_t hal_tpadc_adc_register_callback(tp_channel_id channel, tpadc_adc_usercallback_t user_adc_callback)
{
    hal_tpadc_t *tpadc = &hal_tpadc;

    if (user_adc_callback == NULL)
        return TPADC_ERROR;
    else
        tpadc->adc_callback[channel] = user_adc_callback;

    return TPADC_OK;
}

hal_tpadc_status_t hal_tpadc_adc_init(void)
{
    hal_tpadc_t *tpadc = &hal_tpadc;
    u32 clk_in;

    sunxi_tpadc_setup(tpadc);

    if (hal_tpadc_clk_init(tpadc))
    {
        TPADC_ERR("tpadc init clk error");
        return TPADC_ERROR;
    }

    /* clear status */
    sunxi_flush_fifo(tpadc->reg_base);
    sunxi_clear_fifo_status(tpadc->reg_base);

    sunxi_clk_init(tpadc->reg_base);

    sunxi_tpadc_mode_select(tpadc->reg_base, 1);
    sunxi_tpadc_enable(tpadc->reg_base, 1);

    sunxi_set_filter_type(tpadc->reg_base, 0x1);
    sunxi_filter_enable(tpadc->reg_base);

    sunxi_downirq_enable(tpadc->reg_base);
    sunxi_upirq_enable(tpadc->reg_base);
    sunxi_set_trig_level(tpadc->reg_base, 0x3);
    sunxi_irq_enable(tpadc->reg_base);

    sunxi_tpadc_clear_pending(tpadc->reg_base);

    if (request_irq(tpadc->irq_num, tpadc_adc_handler, 0, "tpadc", tpadc))
    {
        TPADC_ERR("tpadc request irq(%d) failed \n", tpadc->irq_num);
    return TPADC_ERROR;
    }

    enable_irq(tpadc->irq_num);

    TPADC_INFO("tpadc init success");

    return TPADC_OK;
}

hal_tpadc_status_t hal_tpadc_adc_channel_init(tp_channel_id channel)
{
    hal_tpadc_t *tpadc = &hal_tpadc;

    if (sunxi_tpadc_adc_ch_select(tpadc->reg_base, channel))
    {
        TPADC_ERR("TPADC adc mode channel select error");
        return -1;
    }

    return 0;
}

hal_tpadc_status_t hal_tpadc_adc_exit()
{
    hal_tpadc_t *tpadc = &hal_tpadc;

    sunxi_irq_disable(tpadc->reg_base);
    free_irq(tpadc->irq_num, tpadc);

    hal_clock_disable(tpadc->bus_clk);
    hal_clock_put(tpadc->bus_clk);

    hal_clock_disable(tpadc->mod_clk);
    hal_clock_put(tpadc->mod_clk);

    return TPADC_OK;
}