1818#include <zephyr/drivers/i2c_emul.h>
1919#include <zephyr/logging/log.h>
2020
21+ #include "video_common.h"
22+
2123LOG_MODULE_REGISTER (video_emul_imager , CONFIG_VIDEO_LOG_LEVEL );
2224
23- #define EMUL_IMAGER_REG_SENSOR_ID 0x00
25+ #define EMUL_IMAGER_REG8 (addr ) ((addr) | VIDEO_REG_ADDR8_DATA8)
26+
27+ #define EMUL_IMAGER_REG_SENSOR_ID EMUL_IMAGER_REG8(0x00)
2428#define EMUL_IMAGER_SENSOR_ID 0x99
25- #define EMUL_IMAGER_REG_CTRL 0x01
26- #define EMUL_IMAGER_REG_INIT1 0x02
27- #define EMUL_IMAGER_REG_INIT2 0x03
28- #define EMUL_IMAGER_REG_TIMING1 0x04
29- #define EMUL_IMAGER_REG_TIMING2 0x05
30- #define EMUL_IMAGER_REG_TIMING3 0x06
31- #define EMUL_IMAGER_REG_CUSTOM 0x07
32- #define EMUL_IMAGER_REG_FORMAT 0x0a
33- #define EMUL_IMAGER_PATTERN_OFF 0x00
34- #define EMUL_IMAGER_PATTERN_BARS1 0x01
35- #define EMUL_IMAGER_PATTERN_BARS2 0x02
29+ #define EMUL_IMAGER_REG_CTRL EMUL_IMAGER_REG8( 0x01)
30+ #define EMUL_IMAGER_REG_INIT1 EMUL_IMAGER_REG8( 0x02)
31+ #define EMUL_IMAGER_REG_INIT2 EMUL_IMAGER_REG8( 0x03)
32+ #define EMUL_IMAGER_REG_TIMING1 EMUL_IMAGER_REG8( 0x04)
33+ #define EMUL_IMAGER_REG_TIMING2 EMUL_IMAGER_REG8( 0x05)
34+ #define EMUL_IMAGER_REG_TIMING3 EMUL_IMAGER_REG8( 0x06)
35+ #define EMUL_IMAGER_REG_CUSTOM EMUL_IMAGER_REG8( 0x07)
36+ #define EMUL_IMAGER_REG_FORMAT EMUL_IMAGER_REG8( 0x0a)
37+ #define EMUL_IMAGER_PATTERN_OFF EMUL_IMAGER_REG8( 0x00)
38+ #define EMUL_IMAGER_PATTERN_BARS1 EMUL_IMAGER_REG8( 0x01)
39+ #define EMUL_IMAGER_PATTERN_BARS2 EMUL_IMAGER_REG8( 0x02)
3640
3741/* Custom control that is just an I2C write for example and test purpose */
3842#define EMUL_IMAGER_CID_CUSTOM (VIDEO_CID_PRIVATE_BASE + 0x01)
@@ -42,18 +46,13 @@ enum emul_imager_fmt_id {
4246 YUYV_320x240 ,
4347};
4448
45- struct emul_imager_reg {
46- uint16_t addr ;
47- uint8_t value ;
48- };
49-
5049struct emul_imager_mode {
5150 uint8_t fps ;
5251 /* List of registers lists to configure the various properties of the sensor.
5352 * This permits to deduplicate the list of registers in case some lare sections
5453 * are repeated across modes, such as the resolution for different FPS.
5554 */
56- const struct emul_imager_reg * regs [3 ];
55+ const struct video_reg * regs [3 ];
5756 /* More fields can be added according to the needs of the sensor driver */
5857};
5958
@@ -71,39 +70,39 @@ struct emul_imager_data {
7170};
7271
7372/* All the I2C registers sent on various scenario */
74- static const struct emul_imager_reg emul_imager_init_regs [] = {
73+ static const struct video_reg emul_imager_init_regs [] = {
7574 {EMUL_IMAGER_REG_CTRL , 0x00 },
7675 {EMUL_IMAGER_REG_INIT1 , 0x10 },
7776 {EMUL_IMAGER_REG_INIT2 , 0x00 },
7877 /* Undocumented registers from the vendor */
79- {0x80 , 0x01 },
80- {0x84 , 0x01 },
81- {0x85 , 0x20 },
82- {0x89 , 0x7f },
78+ {EMUL_IMAGER_REG8 ( 0x80 ) , 0x01 },
79+ {EMUL_IMAGER_REG8 ( 0x84 ) , 0x01 },
80+ {EMUL_IMAGER_REG8 ( 0x85 ) , 0x20 },
81+ {EMUL_IMAGER_REG8 ( 0x89 ) , 0x7f },
8382 {0 },
8483};
85- static const struct emul_imager_reg emul_imager_rgb565 [] = {
84+ static const struct video_reg emul_imager_rgb565 [] = {
8685 {EMUL_IMAGER_REG_FORMAT , 0x01 },
8786 {0 },
8887};
89- static const struct emul_imager_reg emul_imager_yuyv [] = {
88+ static const struct video_reg emul_imager_yuyv [] = {
9089 {EMUL_IMAGER_REG_FORMAT , 0x02 },
9190 {0 },
9291};
93- static const struct emul_imager_reg emul_imager_320x240 [] = {
92+ static const struct video_reg emul_imager_320x240 [] = {
9493 {EMUL_IMAGER_REG_TIMING1 , 0x32 },
9594 {EMUL_IMAGER_REG_TIMING2 , 0x24 },
9695 {0 },
9796};
98- static const struct emul_imager_reg emul_imager_15fps [] = {
97+ static const struct video_reg emul_imager_15fps [] = {
9998 {EMUL_IMAGER_REG_TIMING3 , 15 },
10099 {0 },
101100};
102- static const struct emul_imager_reg emul_imager_30fps [] = {
101+ static const struct video_reg emul_imager_30fps [] = {
103102 {EMUL_IMAGER_REG_TIMING3 , 30 },
104103 {0 },
105104};
106- static const struct emul_imager_reg emul_imager_60fps [] = {
105+ static const struct video_reg emul_imager_60fps [] = {
107106 {EMUL_IMAGER_REG_TIMING3 , 60 },
108107 {0 },
109108};
@@ -147,71 +146,40 @@ static const struct video_format_cap fmts[] = {
147146 {0 },
148147};
149148
150- /* Emulated I2C register interface, to replace with actual I2C calls for real hardware */
151- static int emul_imager_read_reg (const struct device * const dev , uint8_t reg_addr , uint8_t * value )
152- {
153- const struct emul_imager_config * cfg = dev -> config ;
154-
155- return i2c_write_read_dt (& cfg -> i2c , & reg_addr , 1 , value , 1 );
156- }
157-
158- /* Helper to read a full integer directly from a register */
159- static int emul_imager_read_int (const struct device * const dev , uint8_t reg_addr , int * value )
160- {
161- uint8_t val8 = 0 ;
162- int ret ;
163-
164- ret = emul_imager_read_reg (dev , reg_addr , & val8 );
165- * value = val8 ;
166- return ret ;
167- }
168-
169- /* Some sensors will need reg8 or reg16 variants. */
170- static int emul_imager_write_reg (const struct device * const dev , uint8_t reg_addr , uint8_t value )
149+ static int emul_imager_set_ctrl (const struct device * dev , unsigned int cid , void * value )
171150{
172151 const struct emul_imager_config * cfg = dev -> config ;
173- uint8_t buf_w [] = {reg_addr , value };
174-
175- return i2c_write_dt (& cfg -> i2c , buf_w , 2 );
176- }
177152
178- static int emul_imager_write_multi (const struct device * const dev ,
179- const struct emul_imager_reg * regs )
180- {
181- int ret ;
182-
183- for (int i = 0 ; regs [i ].addr != 0 ; i ++ ) {
184- ret = emul_imager_write_reg (dev , regs [i ].addr , regs [i ].value );
185- if (ret < 0 ) {
186- return ret ;
187- }
188- }
189- return 0 ;
190- }
191-
192- static int emul_imager_set_ctrl (const struct device * dev , unsigned int cid , void * value )
193- {
194153 switch (cid ) {
195154 case EMUL_IMAGER_CID_CUSTOM :
196- return emul_imager_write_reg ( dev , EMUL_IMAGER_REG_CUSTOM , (int )value );
155+ return video_write_cci_reg ( & cfg -> i2c , EMUL_IMAGER_REG_CUSTOM , (int )value );
197156 default :
198157 return - ENOTSUP ;
199158 }
200159}
201160
202161static int emul_imager_get_ctrl (const struct device * dev , unsigned int cid , void * value )
203162{
163+ const struct emul_imager_config * cfg = dev -> config ;
164+ uint32_t reg = 0 ;
165+ int ret ;
166+
204167 switch (cid ) {
205168 case EMUL_IMAGER_CID_CUSTOM :
206- return emul_imager_read_int (dev , EMUL_IMAGER_REG_CUSTOM , value );
169+ ret = video_read_cci_reg (& cfg -> i2c , EMUL_IMAGER_REG_CUSTOM , & reg );
170+ break ;
207171 default :
208172 return - ENOTSUP ;
209173 }
174+
175+ * (uint32_t * )value = reg ;
176+ return ret ;
210177}
211178
212179/* Customize this function according to your "struct emul_imager_mode". */
213180static int emul_imager_set_mode (const struct device * dev , const struct emul_imager_mode * mode )
214181{
182+ const struct emul_imager_config * cfg = dev -> config ;
215183 struct emul_imager_data * data = dev -> data ;
216184 int ret ;
217185
@@ -223,7 +191,7 @@ static int emul_imager_set_mode(const struct device *dev, const struct emul_imag
223191
224192 /* Apply all the configuration registers for that mode */
225193 for (int i = 0 ; i < 2 ; i ++ ) {
226- ret = emul_imager_write_multi ( dev , mode -> regs [i ]);
194+ ret = video_write_cci_multi ( & cfg -> i2c , mode -> regs [i ]);
227195 if (ret < 0 ) {
228196 goto err ;
229197 }
@@ -359,7 +327,9 @@ static int emul_imager_get_caps(const struct device *dev, enum video_endpoint_id
359327
360328static int emul_imager_set_stream (const struct device * dev , bool enable )
361329{
362- return emul_imager_write_reg (dev , EMUL_IMAGER_REG_CTRL , enable ? 1 : 0 );
330+ const struct emul_imager_config * cfg = dev -> config ;
331+
332+ return video_write_cci_reg (& cfg -> i2c , EMUL_IMAGER_REG_CTRL , enable ? 1 : 0 );
363333}
364334
365335static DEVICE_API (video , emul_imager_driver_api ) = {
@@ -378,21 +348,21 @@ int emul_imager_init(const struct device *dev)
378348{
379349 const struct emul_imager_config * cfg = dev -> config ;
380350 struct video_format fmt ;
381- uint8_t sensor_id ;
351+ uint32_t sensor_id ;
382352 int ret ;
383353
384354 if (!i2c_is_ready_dt (& cfg -> i2c )) {
385355 LOG_ERR ("Bus %s is not ready" , cfg -> i2c .bus -> name );
386356 return - ENODEV ;
387357 }
388358
389- ret = emul_imager_read_reg ( dev , EMUL_IMAGER_REG_SENSOR_ID , & sensor_id );
359+ ret = video_read_cci_reg ( & cfg -> i2c , EMUL_IMAGER_REG_SENSOR_ID , & sensor_id );
390360 if (ret < 0 || sensor_id != EMUL_IMAGER_SENSOR_ID ) {
391361 LOG_ERR ("Failed to get a correct sensor ID 0x%x" , sensor_id );
392362 return ret ;
393363 }
394364
395- ret = emul_imager_write_multi ( dev , emul_imager_init_regs );
365+ ret = video_write_cci_multi ( & cfg -> i2c , emul_imager_init_regs );
396366 if (ret < 0 ) {
397367 LOG_ERR ("Could not set initial registers" );
398368 return ret ;
@@ -431,7 +401,7 @@ static int emul_imager_transfer_i2c(const struct emul *target, struct i2c_msg ms
431401 int addr )
432402{
433403 static uint8_t fake_regs [UINT8_MAX ] = {
434- [EMUL_IMAGER_REG_SENSOR_ID ] = EMUL_IMAGER_SENSOR_ID ,
404+ [EMUL_IMAGER_REG_SENSOR_ID & VIDEO_REG_ADDR_MASK ] = EMUL_IMAGER_SENSOR_ID ,
435405 };
436406
437407 if (num_msgs == 0 ) {
0 commit comments