1 /*
2  * Copyright 2017-2018, 2020 NXP
3  * All rights reserved.
4  *
5  *
6  * SPDX-License-Identifier: BSD-3-Clause
7  */
8 
9 #include "fsl_common.h"
10 #include "fsl_video_i2c.h"
11 
12 /*******************************************************************************
13  * Definitions
14  ******************************************************************************/
15 
16 /*******************************************************************************
17  * Code
18  ******************************************************************************/
VIDEO_I2C_WriteReg(uint8_t i2cAddr,video_reg_addr_t addrType,uint32_t reg,video_reg_width_t regWidth,uint32_t value,video_i2c_send_func_t i2cSendFunc)19 status_t VIDEO_I2C_WriteReg(uint8_t i2cAddr,
20                             video_reg_addr_t addrType,
21                             uint32_t reg,
22                             video_reg_width_t regWidth,
23                             uint32_t value,
24                             video_i2c_send_func_t i2cSendFunc)
25 {
26     uint8_t data[4];
27     uint8_t i;
28 
29     i = (uint8_t)regWidth;
30     while (0U != (i--))
31     {
32         data[i] = (uint8_t)value;
33         value >>= 8;
34     }
35 
36     return i2cSendFunc(i2cAddr, reg, addrType, data, regWidth);
37 }
38 
39 /*!
40  * @brief Read the register value.
41  *
42  * @param i2cAddr I2C address.
43  * @param addrType Register address type.
44  * @param reg The register to read.
45  * @param regWidth The width of the register.
46  * @param value The value read out.
47  * @param i2cReceiveFunc The actual I2C receive function.
48  * @return Returns @ref kStatus_Success if success, otherwise returns error code.
49  */
VIDEO_I2C_ReadReg(uint8_t i2cAddr,video_reg_addr_t addrType,uint32_t reg,video_reg_width_t regWidth,void * value,video_i2c_receive_func_t i2cReceiveFunc)50 status_t VIDEO_I2C_ReadReg(uint8_t i2cAddr,
51                            video_reg_addr_t addrType,
52                            uint32_t reg,
53                            video_reg_width_t regWidth,
54                            void *value,
55                            video_i2c_receive_func_t i2cReceiveFunc)
56 {
57     uint8_t data[4] = {0u, 0u, 0u, 0u};
58     uint8_t i       = 0;
59     uint8_t width   = (uint8_t)regWidth;
60     status_t status;
61 
62     status = i2cReceiveFunc(i2cAddr, reg, addrType, data, regWidth);
63 
64     if (kStatus_Success == status)
65     {
66         while (0U != (width--))
67         {
68             ((uint8_t *)value)[i++] = data[width];
69         }
70     }
71 
72     return status;
73 }
74 
VIDEO_I2C_ModifyReg(uint8_t i2cAddr,video_reg_addr_t addrType,uint32_t reg,video_reg_width_t regWidth,uint32_t clrMask,uint32_t value,video_i2c_receive_func_t i2cReceiveFunc,video_i2c_send_func_t i2cSendFunc)75 status_t VIDEO_I2C_ModifyReg(uint8_t i2cAddr,
76                              video_reg_addr_t addrType,
77                              uint32_t reg,
78                              video_reg_width_t regWidth,
79                              uint32_t clrMask,
80                              uint32_t value,
81                              video_i2c_receive_func_t i2cReceiveFunc,
82                              video_i2c_send_func_t i2cSendFunc)
83 {
84     status_t status;
85     uint32_t regVal = 0U;
86 
87     status = VIDEO_I2C_ReadReg(i2cAddr, addrType, reg, regWidth, &regVal, i2cReceiveFunc);
88 
89     if (kStatus_Success != status)
90     {
91         return status;
92     }
93 
94     regVal = (regVal & ~(clrMask)) | (value & clrMask);
95 
96     return VIDEO_I2C_WriteReg(i2cAddr, addrType, reg, regWidth, regVal, i2cSendFunc);
97 }
98