You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
146 lines
3.2 KiB
146 lines
3.2 KiB
/*
|
|
* Copyright (c) Hisilicon Technologies Co., Ltd. 2012-2021. All rights reserved.
|
|
* Description: iapi_lasdc.c
|
|
*/
|
|
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <sys/ioctl.h>
|
|
#include <sys/types.h>
|
|
#include <sys/stat.h>
|
|
#include <fcntl.h>
|
|
#include <unistd.h>
|
|
#include "soc_errno.h"
|
|
#include "soc_log.h"
|
|
#include "drv_ioctl_lsadc.h"
|
|
#include "uapi_lsadc.h"
|
|
|
|
#undef LOG_MODULE_ID
|
|
#define LOG_MODULE_ID SOC_ID_LSADC
|
|
static td_s32 g_lsadc_fd = -1;
|
|
static td_u32 g_lsadc_init_count = 0;
|
|
#define EXT_LSADC_MAX_CHANNEL 16
|
|
|
|
#define INNER_TO_OUTER 0
|
|
#define OUTER_TO_INNER 1
|
|
/* Init */
|
|
td_s32 uapi_lsadc_init(td_void)
|
|
{
|
|
g_lsadc_init_count++;
|
|
|
|
if (g_lsadc_fd < 0) {
|
|
g_lsadc_fd = open("/dev/soc_lsadc", O_RDWR, 0);
|
|
if (g_lsadc_fd < 0) {
|
|
soc_fatal_print_call_fun_err(open, g_lsadc_fd);
|
|
return SOC_ERR_LSADC_FAILED_INIT;
|
|
}
|
|
}
|
|
|
|
return TD_SUCCESS;
|
|
}
|
|
|
|
/* Deinit */
|
|
td_s32 uapi_lsadc_deinit(td_void)
|
|
{
|
|
td_s32 ret = 0;
|
|
|
|
if (g_lsadc_fd < 0) {
|
|
printf("don't init lsadc\n");
|
|
return SOC_ERR_LSADC_FAILED_INIT;
|
|
}
|
|
|
|
g_lsadc_init_count--;
|
|
|
|
if (g_lsadc_init_count == 0) {
|
|
ret = close(g_lsadc_fd);
|
|
if (ret != TD_SUCCESS) {
|
|
soc_fatal_print_call_fun_err(close, ret);
|
|
return SOC_ERR_LSADC_CLOSE_ERR;
|
|
}
|
|
g_lsadc_fd = -1;
|
|
}
|
|
|
|
return TD_SUCCESS;
|
|
}
|
|
|
|
/* Set LSADC Config */
|
|
td_s32 uapi_lsadc_set_config(const uapi_lsadc_config *config)
|
|
{
|
|
td_s32 ret;
|
|
|
|
if (config == TD_NULL) {
|
|
printf("input parameter is invalid\n");
|
|
return SOC_ERR_LSADC_INVALID_PARA;
|
|
}
|
|
|
|
if (g_lsadc_fd < 0) {
|
|
printf("don't init lsadc\n");
|
|
return SOC_ERR_LSADC_FAILED_INIT;
|
|
}
|
|
|
|
ret = ioctl(g_lsadc_fd, CMD_LSADC_SET_CONFIG, config);
|
|
if (ret != TD_SUCCESS) {
|
|
soc_fatal_print_call_fun_err(ioctl, ret);
|
|
return SOC_ERR_LSADC_GET_DATA_ERR;
|
|
}
|
|
|
|
return TD_SUCCESS;
|
|
}
|
|
|
|
/* Read LSADC Config */
|
|
td_s32 uapi_lsadc_get_config(uapi_lsadc_config *config)
|
|
{
|
|
td_s32 ret;
|
|
|
|
if (config == TD_NULL) {
|
|
printf("input parameter is invalid\n");
|
|
return SOC_ERR_LSADC_INVALID_PARA;
|
|
}
|
|
|
|
if (g_lsadc_fd < 0) {
|
|
printf("don't init lsadc\n");
|
|
return SOC_ERR_LSADC_FAILED_INIT;
|
|
}
|
|
|
|
ret = ioctl(g_lsadc_fd, CMD_LSADC_GET_CONFIG, config);
|
|
if (ret != TD_SUCCESS) {
|
|
soc_fatal_print_call_fun_err(ioctl, ret);
|
|
return SOC_ERR_LSADC_GET_DATA_ERR;
|
|
}
|
|
|
|
return TD_SUCCESS;
|
|
}
|
|
|
|
/* Read lsadc value */
|
|
td_s32 uapi_lsadc_get_value(td_u32 channel, td_u32 *value)
|
|
{
|
|
td_s32 ret;
|
|
ext_lsadc_value_param param = {0};
|
|
|
|
if (g_lsadc_fd < 0) {
|
|
printf("don't init lsadc\n");
|
|
return SOC_ERR_LSADC_FAILED_INIT;
|
|
}
|
|
|
|
if (value == NULL) {
|
|
printf("input parameter is null\n");
|
|
return SOC_ERR_LSADC_INVALID_PARA;
|
|
}
|
|
|
|
if (channel >= EXT_LSADC_MAX_CHANNEL) {
|
|
printf("input paramter invalid\n");
|
|
return SOC_ERR_LSADC_INVALID_PARA;
|
|
}
|
|
|
|
param.channel = channel;
|
|
ret = ioctl(g_lsadc_fd, CMD_LSADC_GET_VALUE, ¶m);
|
|
if (ret != TD_SUCCESS) {
|
|
soc_fatal_print_call_fun_err(ioctl, ret);
|
|
return SOC_ERR_LSADC_GET_DATA_ERR;
|
|
}
|
|
|
|
*value = param.channel_val & 0x3FF; /* 0x3FF is 0-9 bit */
|
|
|
|
return TD_SUCCESS;
|
|
}
|