18
18
#include <zephyr/drivers/i2c_emul.h>
19
19
#include <zephyr/logging/log.h>
20
20
21
+ #include "video_common.h"
22
+
21
23
LOG_MODULE_REGISTER (video_emul_imager , CONFIG_VIDEO_LOG_LEVEL );
22
24
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)
24
28
#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)
36
40
37
41
/* Custom control that is just an I2C write for example and test purpose */
38
42
#define EMUL_IMAGER_CID_CUSTOM (VIDEO_CID_PRIVATE_BASE + 0x01)
@@ -42,18 +46,13 @@ enum emul_imager_fmt_id {
42
46
YUYV_320x240 ,
43
47
};
44
48
45
- struct emul_imager_reg {
46
- uint16_t addr ;
47
- uint8_t value ;
48
- };
49
-
50
49
struct emul_imager_mode {
51
50
uint8_t fps ;
52
51
/* List of registers lists to configure the various properties of the sensor.
53
52
* This permits to deduplicate the list of registers in case some lare sections
54
53
* are repeated across modes, such as the resolution for different FPS.
55
54
*/
56
- const struct emul_imager_reg * regs [3 ];
55
+ const struct video_reg * regs [3 ];
57
56
/* More fields can be added according to the needs of the sensor driver */
58
57
};
59
58
@@ -71,39 +70,39 @@ struct emul_imager_data {
71
70
};
72
71
73
72
/* 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 [] = {
75
74
{EMUL_IMAGER_REG_CTRL , 0x00 },
76
75
{EMUL_IMAGER_REG_INIT1 , 0x10 },
77
76
{EMUL_IMAGER_REG_INIT2 , 0x00 },
78
77
/* 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 },
83
82
{0 },
84
83
};
85
- static const struct emul_imager_reg emul_imager_rgb565 [] = {
84
+ static const struct video_reg emul_imager_rgb565 [] = {
86
85
{EMUL_IMAGER_REG_FORMAT , 0x01 },
87
86
{0 },
88
87
};
89
- static const struct emul_imager_reg emul_imager_yuyv [] = {
88
+ static const struct video_reg emul_imager_yuyv [] = {
90
89
{EMUL_IMAGER_REG_FORMAT , 0x02 },
91
90
{0 },
92
91
};
93
- static const struct emul_imager_reg emul_imager_320x240 [] = {
92
+ static const struct video_reg emul_imager_320x240 [] = {
94
93
{EMUL_IMAGER_REG_TIMING1 , 0x32 },
95
94
{EMUL_IMAGER_REG_TIMING2 , 0x24 },
96
95
{0 },
97
96
};
98
- static const struct emul_imager_reg emul_imager_15fps [] = {
97
+ static const struct video_reg emul_imager_15fps [] = {
99
98
{EMUL_IMAGER_REG_TIMING3 , 15 },
100
99
{0 },
101
100
};
102
- static const struct emul_imager_reg emul_imager_30fps [] = {
101
+ static const struct video_reg emul_imager_30fps [] = {
103
102
{EMUL_IMAGER_REG_TIMING3 , 30 },
104
103
{0 },
105
104
};
106
- static const struct emul_imager_reg emul_imager_60fps [] = {
105
+ static const struct video_reg emul_imager_60fps [] = {
107
106
{EMUL_IMAGER_REG_TIMING3 , 60 },
108
107
{0 },
109
108
};
@@ -147,71 +146,40 @@ static const struct video_format_cap fmts[] = {
147
146
{0 },
148
147
};
149
148
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 )
171
150
{
172
151
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
- }
177
152
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
- {
194
153
switch (cid ) {
195
154
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 );
197
156
default :
198
157
return - ENOTSUP ;
199
158
}
200
159
}
201
160
202
161
static int emul_imager_get_ctrl (const struct device * dev , unsigned int cid , void * value )
203
162
{
163
+ const struct emul_imager_config * cfg = dev -> config ;
164
+ uint32_t reg = 0 ;
165
+ int ret ;
166
+
204
167
switch (cid ) {
205
168
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 ;
207
171
default :
208
172
return - ENOTSUP ;
209
173
}
174
+
175
+ * (uint32_t * )value = reg ;
176
+ return ret ;
210
177
}
211
178
212
179
/* Customize this function according to your "struct emul_imager_mode". */
213
180
static int emul_imager_set_mode (const struct device * dev , const struct emul_imager_mode * mode )
214
181
{
182
+ const struct emul_imager_config * cfg = dev -> config ;
215
183
struct emul_imager_data * data = dev -> data ;
216
184
int ret ;
217
185
@@ -223,7 +191,7 @@ static int emul_imager_set_mode(const struct device *dev, const struct emul_imag
223
191
224
192
/* Apply all the configuration registers for that mode */
225
193
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 ]);
227
195
if (ret < 0 ) {
228
196
goto err ;
229
197
}
@@ -359,7 +327,9 @@ static int emul_imager_get_caps(const struct device *dev, enum video_endpoint_id
359
327
360
328
static int emul_imager_set_stream (const struct device * dev , bool enable )
361
329
{
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 );
363
333
}
364
334
365
335
static DEVICE_API (video , emul_imager_driver_api ) = {
@@ -378,21 +348,21 @@ int emul_imager_init(const struct device *dev)
378
348
{
379
349
const struct emul_imager_config * cfg = dev -> config ;
380
350
struct video_format fmt ;
381
- uint8_t sensor_id ;
351
+ uint32_t sensor_id ;
382
352
int ret ;
383
353
384
354
if (!i2c_is_ready_dt (& cfg -> i2c )) {
385
355
LOG_ERR ("Bus %s is not ready" , cfg -> i2c .bus -> name );
386
356
return - ENODEV ;
387
357
}
388
358
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 );
390
360
if (ret < 0 || sensor_id != EMUL_IMAGER_SENSOR_ID ) {
391
361
LOG_ERR ("Failed to get a correct sensor ID 0x%x" , sensor_id );
392
362
return ret ;
393
363
}
394
364
395
- ret = emul_imager_write_multi ( dev , emul_imager_init_regs );
365
+ ret = video_write_cci_multi ( & cfg -> i2c , emul_imager_init_regs );
396
366
if (ret < 0 ) {
397
367
LOG_ERR ("Could not set initial registers" );
398
368
return ret ;
@@ -431,7 +401,7 @@ static int emul_imager_transfer_i2c(const struct emul *target, struct i2c_msg ms
431
401
int addr )
432
402
{
433
403
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 ,
435
405
};
436
406
437
407
if (num_msgs == 0 ) {
0 commit comments