1 /*
2  * drivers/input/sensor/sunxi_gpadc.h
3  *
4  * Copyright (C) 2016 Allwinner.
5  * fuzhaoke <fuzhaoke@allwinnertech.com>
6  *
7  * SUNXI GPADC Controller Driver Header
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License as
11  * published by the Free Software Foundation; either version 2 of
12  * the License, or (at your option) any later version.
13  */
14 
15 #ifndef HAL_GPADC_H
16 #define HAL_GPADC_H
17 
18 #include "hal_clk.h"
19 #include "sunxi_hal_common.h"
20 #include <hal_log.h>
21 #include <interrupt.h>
22 #include <gpadc/platform_gpadc.h>
23 #include <gpadc/common_gpadc.h>
24 
25 #ifdef __cplusplus
26 extern "C" {
27 #endif
28 
29 #define CONFIG_DRIVERS_GPADC_DEBUG
30 #ifdef CONFIG_DRIVERS_GPADC_DEBUG
31 #define GPADC_INFO(fmt, arg...) hal_log_info(fmt, ##arg)
32 #else
33 #define GPADC_INFO(fmt, arg...) do {}while(0)
34 #endif
35 
36 #define GPADC_ERR(fmt, arg...) hal_log_err(fmt, ##arg)
37 
38 enum
39 {
40     GPADC_DOWN,
41     GPADC_UP
42 };
43 
44 typedef enum
45 {
46     GP_CH_0 = 0,
47     GP_CH_1,
48     GP_CH_2,
49     GP_CH_3,
50     GP_CH_4,
51     GP_CH_5,
52     GP_CH_6,
53     GP_CH_7,
54     GP_CH_MAX
55 } hal_gpadc_channel_t;
56 
57 typedef enum
58 {
59     GPADC_IRQ_ERROR = -4,
60     GPADC_CHANNEL_ERROR = -3,
61     GPADC_CLK_ERROR = -2,
62     GPADC_ERROR = -1,
63     GPADC_OK = 0,
64 } hal_gpadc_status_t;
65 
66 typedef enum gp_select_mode
67 {
68     GP_SINGLE_MODE = 0,
69     GP_SINGLE_CYCLE_MODE,
70     GP_CONTINUOUS_MODE,
71     GP_BURST_MODE,
72 } hal_gpadc_mode_t;
73 
74 typedef int (*gpadc_callback_t)(uint32_t data_type, uint32_t data);
75 
76 typedef struct
77 {
78     uint32_t reg_base;
79     uint32_t channel_num;
80     uint32_t irq_num;
81     uint32_t sample_rate;
82 #if defined(CONFIG_SOC_SUN20IW1)
83     hal_clk_id_t bus_clk;
84     hal_clk_id_t rst_clk;
85     hal_clk_t mbus_clk;
86 #else
87     hal_clk_id_t mclk;
88     hal_clk_id_t pclk;
89 #endif
90     hal_gpadc_mode_t mode;
91     gpadc_callback_t callback[CHANNEL_MAX_NUM];
92 } hal_gpadc_t;
93 
94 int hal_gpadc_init(void);
95 hal_gpadc_status_t hal_gpadc_deinit(void);
96 hal_gpadc_status_t hal_gpadc_channel_init(hal_gpadc_channel_t channal);
97 hal_gpadc_status_t hal_gpadc_channel_exit(hal_gpadc_channel_t channal);
98 hal_gpadc_status_t hal_gpadc_register_callback(hal_gpadc_channel_t channal,
99         gpadc_callback_t user_callback);
100 
101 #ifdef __cplusplus
102 }
103 #endif
104 
105 #endif
106