1 /*
2 * Copyright (c) 2020-2021 Huawei Device Co., Ltd.
3 *
4 * HDF is dual licensed: you can use it either under the terms of
5 * the GPL, or the BSD license, at your option.
6 * See the LICENSE file in the root of this repository for complete details.
7 */
8
9 #include "sensor_platform_if.h"
10 #include <securec.h>
11 #include "osal_io.h"
12 #include "osal_time.h"
13
14 #define HDF_LOG_TAG khdf_sensor_common_driver
15
16 #define I2C_READ_MSG_NUM 2
17 #define I2C_READ_MSG_ADDR_IDX 0
18 #define I2C_READ_MSG_VALUE_IDX 1
19
20 #define I2C_WRITE_MSG_NUM 1
21 #define I2C_REG_BUF_LEN 4
22 #define I2C_BYTE_MASK 0xFF
23 #define I2C_BYTE_OFFSET 8
24
25 #define SENSOR_STACK_SIZE 0x2000 // 4k buffer
26
ReadSensor(struct SensorBusCfg * busCfg,uint16_t regAddr,uint8_t * data,uint16_t dataLen)27 int32_t ReadSensor(struct SensorBusCfg *busCfg, uint16_t regAddr, uint8_t *data, uint16_t dataLen)
28 {
29 int index = 0;
30 unsigned char regBuf[I2C_REG_BUF_LEN] = {0};
31 struct I2cMsg msg[I2C_READ_MSG_NUM];
32
33 CHECK_NULL_PTR_RETURN_VALUE(busCfg, HDF_FAILURE);
34 CHECK_NULL_PTR_RETURN_VALUE(data, HDF_FAILURE);
35
36 if (busCfg->busType == SENSOR_BUS_I2C) {
37 CHECK_NULL_PTR_RETURN_VALUE(busCfg->i2cCfg.handle, HDF_FAILURE);
38
39 msg[I2C_READ_MSG_ADDR_IDX].addr = busCfg->i2cCfg.devAddr;
40 msg[I2C_READ_MSG_ADDR_IDX].flags = 0;
41 msg[I2C_READ_MSG_ADDR_IDX].len = busCfg->i2cCfg.regWidth;
42 msg[I2C_READ_MSG_ADDR_IDX].buf = regBuf;
43
44 if (busCfg->i2cCfg.regWidth == SENSOR_ADDR_WIDTH_1_BYTE) {
45 regBuf[index++] = regAddr & I2C_BYTE_MASK;
46 } else if (busCfg->i2cCfg.regWidth == SENSOR_ADDR_WIDTH_2_BYTE) {
47 regBuf[index++] = (regAddr >> I2C_BYTE_OFFSET) & I2C_BYTE_MASK;
48 regBuf[index++] = regAddr & I2C_BYTE_MASK;
49 } else {
50 HDF_LOGE("%s: i2c regWidth[%u] failed", __func__, busCfg->i2cCfg.regWidth);
51 return HDF_FAILURE;
52 }
53
54 msg[I2C_READ_MSG_VALUE_IDX].addr = busCfg->i2cCfg.devAddr;
55 msg[I2C_READ_MSG_VALUE_IDX].flags = I2C_FLAG_READ;
56 msg[I2C_READ_MSG_VALUE_IDX].len = dataLen;
57 msg[I2C_READ_MSG_VALUE_IDX].buf = data;
58
59 if (I2cTransfer(busCfg->i2cCfg.handle, msg, I2C_READ_MSG_NUM) != I2C_READ_MSG_NUM) {
60 HDF_LOGE("%s: i2c[%u] read failed", __func__, busCfg->i2cCfg.busNum);
61 return HDF_FAILURE;
62 }
63
64 #if defined(LOSCFG_DRIVERS_HDF_PLATFORM_SPI) || defined(CONFIG_DRIVERS_HDF_PLATFORM_SPI)
65 } else if (busCfg->busType == SENSOR_BUS_SPI) {
66 if (SpiRead(busCfg->spiCfg.handle, data, dataLen) != HDF_SUCCESS) {
67 HDF_LOGE("%s: spi read failed", __func__);
68 return HDF_FAILURE;
69 }
70 #endif
71 }
72
73 return HDF_SUCCESS;
74 }
75
WriteSensor(struct SensorBusCfg * busCfg,uint8_t * writeData,uint16_t dataLen)76 int32_t WriteSensor(struct SensorBusCfg *busCfg, uint8_t *writeData, uint16_t dataLen)
77 {
78 struct I2cMsg msg[I2C_WRITE_MSG_NUM];
79
80 CHECK_NULL_PTR_RETURN_VALUE(busCfg, HDF_FAILURE);
81 CHECK_NULL_PTR_RETURN_VALUE(writeData, HDF_FAILURE);
82
83 if (busCfg->busType == SENSOR_BUS_I2C) {
84 CHECK_NULL_PTR_RETURN_VALUE(busCfg->i2cCfg.handle, HDF_FAILURE);
85
86 msg[0].addr = busCfg->i2cCfg.devAddr;
87 msg[0].flags = 0;
88 msg[0].len = dataLen;
89 msg[0].buf = writeData;
90
91 if (I2cTransfer(busCfg->i2cCfg.handle, msg, I2C_WRITE_MSG_NUM) != I2C_WRITE_MSG_NUM) {
92 HDF_LOGE("%s: i2c[%u] write failed", __func__, busCfg->i2cCfg.busNum);
93 return HDF_FAILURE;
94 }
95
96 #if defined(LOSCFG_DRIVERS_HDF_PLATFORM_SPI) || defined(CONFIG_DRIVERS_HDF_PLATFORM_SPI)
97 } else if (busCfg->busType == SENSOR_BUS_SPI) {
98 if (SpiWrite(busCfg->spiCfg.handle, writeData, dataLen) != HDF_SUCCESS) {
99 HDF_LOGE("%s: spi write failed", __func__);
100 return HDF_FAILURE;
101 }
102 #endif
103 }
104
105 return HDF_SUCCESS;
106 }
107
SetSensorPinMux(uint32_t regAddr,int32_t regSize,uint32_t regValue)108 int32_t SetSensorPinMux(uint32_t regAddr, int32_t regSize, uint32_t regValue)
109 {
110 uint8_t *base = NULL;
111 if (regAddr == 0) {
112 HDF_LOGE("%s: regAddr invalid", __func__);
113 HDF_LOGE("%s: regSize = %d, regValue = %u", __func__, regSize, regValue);
114 return HDF_FAILURE;
115 }
116
117 #if !defined(CONFIG_ARCH_ROCKCHIP)
118 base = OsalIoRemap(regAddr, regSize);
119 if (base == NULL) {
120 HDF_LOGE("%s: ioremap fail", __func__);
121 return HDF_FAILURE;
122 }
123
124 OSAL_WRITEL(regValue, base);
125 OsalIoUnmap((void *)base);
126 #endif
127
128 return HDF_SUCCESS;
129 }
130