blob: 90a48e272b79783198c1693185442df2e188a931 [file] [log] [blame]
liubin281ac462023-07-19 14:22:54 +08001/**
2 * \file mbtk_adc.c
3 * \brief A Documented file.
4 *
5 * Detailed description
6 * \Author: js.wang <js.wang@mobiletek.cn>
7 * \Version: 1.0.0
8 * \Date: 2022-04-22
9 */
10#include <fcntl.h>
11#include <stdint.h>
12#include <limits.h>
13#include <termios.h>
14#include <stdarg.h>
15#include <dirent.h>
16#include <sys/stat.h>
17#include <sys/statfs.h>
18#include <sys/types.h>
19#include "mbtk_log.h"
20#include "mbtk_type.h"
21#include "mbtk_adc.h"
22
liubinb8a0dd72023-08-04 15:26:48 +080023#define ADC_DEVICE_802 "/sys/devices/soc.0/d4000000.apb/pxa2xx-i2c.2/i2c-2/2-0030/pm802-bat/adc"
liubin6648d642023-09-02 13:43:19 +080024#define ADC_DEVICE_803 "/sys/kernel/debug/adc"
gy.tangb8c12bd2024-07-09 10:40:14 +080025#define ADC_DEVICE_PMIC802 "/sys/devices/platform/asr-adc/pm80x_adc"
wangyouqiangc57b3682023-12-07 18:58:37 +080026#define ADC_DEVICE_AUX "/sys/devices/platform/asr-adc/aux_adc"
liubinb8a0dd72023-08-04 15:26:48 +080027
wangyouqiang6de4f692023-10-26 16:37:28 +080028int mbtk_adc_close(void)
wangyouqiang2b352b22023-10-21 15:18:43 +080029{
wangyouqiang6de4f692023-10-26 16:37:28 +080030 int ret = 0;
31 int fd = 0;
32 char adc = '3';
33 //system("echo 3 > /sys/kernel/debug/adc");
34 if(!access(ADC_DEVICE_803, R_OK))
35 {
b.liuc7ffd092024-01-03 15:23:07 +080036 //LOGI("DEV:%s", ADC_DEVICE_803);
wangyouqiang6de4f692023-10-26 16:37:28 +080037 fd = open(ADC_DEVICE_803, O_RDWR|O_CREAT|O_TRUNC, 0644);
38 }
39 else
40 {
41 LOGE("No found ADC devices.");
42 return -1;
43 }
44
45 if(fd < 0) {
46 LOGE("[%s] file open error\n", __FUNCTION__);
47 return -2;
48 }
49 ret = write(fd, &adc, 1);
50 if (ret < 0) {
51 LOGE("%s: error writing to file!\n", __FUNCTION__);
52 close(fd);
53 return -2;
54 }
55 close(fd);
56 return 0;
wangyouqiang2b352b22023-10-21 15:18:43 +080057}
liubin281ac462023-07-19 14:22:54 +080058
59int mbtk_adc_get(mbtk_adc_enum channle)
60{
61 int ret = 0;
62 int fd = 0;
63 char adc_buf[24] = {0};
64 char *adc_value = NULL;
wangyouqiangc57b3682023-12-07 18:58:37 +080065 char adc =(channle == MBTK_ADC0 ? '0' : (channle == MBTK_ADC1 ? '1' : '2'));
liubin281ac462023-07-19 14:22:54 +080066
wangyouqiang53babb72024-03-01 18:32:28 +080067#if defined(MBTK_PROJECT_L508_X6)
wangyouqiangc57b3682023-12-07 18:58:37 +080068 switch(channle)
69 {
70 case MBTK_ADC0:
71 case MBTK_ADC1:
72 {
73 if(!access(ADC_DEVICE_PMIC802, R_OK))
74 {
75 LOGI("[adc] DEV:%s", ADC_DEVICE_PMIC802);
76 fd = open(ADC_DEVICE_PMIC802, O_RDWR|O_CREAT|O_TRUNC, 0644);
77 }
78 else
79 {
80 LOGE("No found ADC devices.");
81 return -1;
82 }
83 break;
84 }
85 case MBTK_ADC2:
86 {
87 if(!access(ADC_DEVICE_AUX, R_OK))
88 {
89 LOGI("[adc] DEV:%s", ADC_DEVICE_AUX);
90 fd = open(ADC_DEVICE_AUX, O_RDWR|O_CREAT|O_TRUNC, 0644);
91 }
92 else
93 {
94 LOGE("No found ADC devices.");
95 return -1;
96 }
97 break;
98 }
99 default:
100 {
101 LOGE("channle is error.");
102 return -1;
103 }
104 }
wangyouqiang53babb72024-03-01 18:32:28 +0800105#elif defined(MBTK_PROJECT_T108)
106 if(!access(ADC_DEVICE_AUX, R_OK))
107 {
108 LOGI("[adc] DEV:%s", ADC_DEVICE_AUX);
109 fd = open(ADC_DEVICE_AUX, O_RDWR|O_CREAT|O_TRUNC, 0644);
110 }
111 else
112 {
113 LOGE("No found ADC devices.");
114 return -1;
115 }
wangyouqiangc57b3682023-12-07 18:58:37 +0800116#else
wangyouqiang648d0e52024-04-01 10:26:48 +0800117#if 0
liubinb8a0dd72023-08-04 15:26:48 +0800118 if(!access(ADC_DEVICE_802, R_OK)) {
b.liuc7ffd092024-01-03 15:23:07 +0800119 //LOGI("DEV:%s", ADC_DEVICE_802);
liubinb8a0dd72023-08-04 15:26:48 +0800120 fd = open(ADC_DEVICE_802, O_RDWR|O_CREAT|O_TRUNC, 0644);
121 } else {
122 if(!access(ADC_DEVICE_803, R_OK)) {
b.liuc7ffd092024-01-03 15:23:07 +0800123 //LOGI("DEV:%s", ADC_DEVICE_803);
liubinb8a0dd72023-08-04 15:26:48 +0800124 fd = open(ADC_DEVICE_803, O_RDWR|O_CREAT|O_TRUNC, 0644);
125 } else {
126 LOGE("No found ADC devices.");
127 return -1;
128 }
129 }
wangyouqiangc57b3682023-12-07 18:58:37 +0800130#endif
wangyouqiang648d0e52024-04-01 10:26:48 +0800131 if(!access(ADC_DEVICE_PMIC802, R_OK))
132 {
133 //LOGI("DEV:%s", ADC_DEVICE_803);
134 fd = open(ADC_DEVICE_PMIC802, O_RDWR|O_CREAT|O_TRUNC, 0644);
135 }
136 else
137 {
138 LOGE("No found ADC devices.");
139 return -1;
140 }
141#endif
liubinb8a0dd72023-08-04 15:26:48 +0800142
liubin281ac462023-07-19 14:22:54 +0800143 if(fd < 0) {
144 LOGE("[%s] file open error\n", __FUNCTION__);
145 return -2;
146 }
147 ret = write(fd, &adc, 1);
148 if (ret < 0) {
149 LOGE("%s: error writing to file!\n", __FUNCTION__);
150 close(fd);
151 return -2;
152 }
153 ret = read(fd, adc_buf, 24);
154 if (ret < 0) {
155 LOGE("%s: error writing to file!\n", __FUNCTION__);
156 close(fd);
157 return -2;
158 }else{
b.liuc7ffd092024-01-03 15:23:07 +0800159 //LOGI("%s %d adc:%s\n", __FUNCTION__, __LINE__, adc_buf);
liubin281ac462023-07-19 14:22:54 +0800160 adc_value = strstr(adc_buf, "channel");
161 }
162 close(fd);
163 if(adc_value)
wangyouqiang59d8dd22023-10-19 18:03:54 +0800164 {
165 //LOGI("%s adc: %s\n", __FUNCTION__, adc_value);
166 }
liubin281ac462023-07-19 14:22:54 +0800167 else
168 return -2;
169
170 return atoi(&adc_value[9]);
171}