blob: 581bcba72ae4f9fd8b42d32ebd5f14749d8e3247 [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"
wangyouqiangc57b3682023-12-07 18:58:37 +080025#define ADC_DEVICE_PMIC802 "/sys/devices/platform/asr-adc/pmic802adc"
26#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
wangyouqiangc57b3682023-12-07 18:58:37 +080067#if (defined(MBTK_PROJECT_L508_X6) || defined(MBTK_PROJECT_T108))
68 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 }
105#else
liubinb8a0dd72023-08-04 15:26:48 +0800106 if(!access(ADC_DEVICE_802, R_OK)) {
b.liuc7ffd092024-01-03 15:23:07 +0800107 //LOGI("DEV:%s", ADC_DEVICE_802);
liubinb8a0dd72023-08-04 15:26:48 +0800108 fd = open(ADC_DEVICE_802, O_RDWR|O_CREAT|O_TRUNC, 0644);
109 } else {
110 if(!access(ADC_DEVICE_803, R_OK)) {
b.liuc7ffd092024-01-03 15:23:07 +0800111 //LOGI("DEV:%s", ADC_DEVICE_803);
liubinb8a0dd72023-08-04 15:26:48 +0800112 fd = open(ADC_DEVICE_803, O_RDWR|O_CREAT|O_TRUNC, 0644);
113 } else {
114 LOGE("No found ADC devices.");
115 return -1;
116 }
117 }
wangyouqiangc57b3682023-12-07 18:58:37 +0800118#endif
liubinb8a0dd72023-08-04 15:26:48 +0800119
liubin281ac462023-07-19 14:22:54 +0800120 if(fd < 0) {
121 LOGE("[%s] file open error\n", __FUNCTION__);
122 return -2;
123 }
124 ret = write(fd, &adc, 1);
125 if (ret < 0) {
126 LOGE("%s: error writing to file!\n", __FUNCTION__);
127 close(fd);
128 return -2;
129 }
130 ret = read(fd, adc_buf, 24);
131 if (ret < 0) {
132 LOGE("%s: error writing to file!\n", __FUNCTION__);
133 close(fd);
134 return -2;
135 }else{
b.liuc7ffd092024-01-03 15:23:07 +0800136 //LOGI("%s %d adc:%s\n", __FUNCTION__, __LINE__, adc_buf);
liubin281ac462023-07-19 14:22:54 +0800137 adc_value = strstr(adc_buf, "channel");
138 }
139 close(fd);
140 if(adc_value)
wangyouqiang59d8dd22023-10-19 18:03:54 +0800141 {
142 //LOGI("%s adc: %s\n", __FUNCTION__, adc_value);
143 }
liubin281ac462023-07-19 14:22:54 +0800144 else
145 return -2;
146
147 return atoi(&adc_value[9]);
148}