Skip to content

Commit

Permalink
drivers: video: gc2145: Add support for YUV format.
Browse files Browse the repository at this point in the history
Can be used to get a fast grayscale image.

Signed-off-by: Ibrahim Abdalkader <i.abdalkader@gmail.com>
  • Loading branch information
iabdalkader committed Jan 22, 2025
1 parent ddff91f commit 4e82bf1
Showing 1 changed file with 27 additions and 22 deletions.
49 changes: 27 additions & 22 deletions drivers/video/gc2145.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ LOG_MODULE_REGISTER(video_gc2145, CONFIG_VIDEO_LOG_LEVEL);
#define GC2145_REG_AMODE1_DEF 0x14
#define GC2145_REG_OUTPUT_FMT 0x84
#define GC2145_REG_OUTPUT_FMT_RGB565 0x06
#define GC2145_REG_OUTPUT_FMT_YCBYCR 0x02
#define GC2145_REG_SYNC_MODE 0x86
#define GC2145_REG_SYNC_MODE_DEF 0x23
#define GC2145_REG_SYNC_MODE_COL_SWITCH 0x10
Expand Down Expand Up @@ -700,16 +701,22 @@ struct gc2145_data {
}

enum resolutions {
QVGA_RESOLUTION = 0,
VGA_RESOLUTION,
UXGA_RESOLUTION,
QVGA_RGB_RESOLUTION = 0,
VGA_RGB_RESOLUTION,
UXGA_RGB_RESOLUTION,
QVGA_YUV_RESOLUTION,
VGA_YUV_RESOLUTION,
UXGA_YUV_RESOLUTION,
RESOLUTIONS_MAX,
};

static const struct video_format_cap fmts[] = {
[QVGA_RESOLUTION] = GC2145_VIDEO_FORMAT_CAP(320, 240, VIDEO_PIX_FMT_RGB565), /* QVGA */
[VGA_RESOLUTION] = GC2145_VIDEO_FORMAT_CAP(640, 480, VIDEO_PIX_FMT_RGB565), /* VGA */
[UXGA_RESOLUTION] = GC2145_VIDEO_FORMAT_CAP(1600, 1200, VIDEO_PIX_FMT_RGB565), /* UXGA */
[QVGA_RGB_RESOLUTION] = GC2145_VIDEO_FORMAT_CAP(320, 240, VIDEO_PIX_FMT_RGB565), /* QVGA */

Check warning on line 714 in drivers/video/gc2145.c

View workflow job for this annotation

GitHub Actions / Run compliance checks on patch series (PR)

LONG_LINE_COMMENT

drivers/video/gc2145.c:714 line length of 101 exceeds 100 columns
[VGA_RGB_RESOLUTION] = GC2145_VIDEO_FORMAT_CAP(640, 480, VIDEO_PIX_FMT_RGB565), /* VGA */

Check warning on line 715 in drivers/video/gc2145.c

View workflow job for this annotation

GitHub Actions / Run compliance checks on patch series (PR)

LONG_LINE_COMMENT

drivers/video/gc2145.c:715 line length of 101 exceeds 100 columns
[UXGA_RGB_RESOLUTION] = GC2145_VIDEO_FORMAT_CAP(1600, 1200, VIDEO_PIX_FMT_RGB565), /* UXGA */

Check warning on line 716 in drivers/video/gc2145.c

View workflow job for this annotation

GitHub Actions / Run compliance checks on patch series (PR)

LONG_LINE_COMMENT

drivers/video/gc2145.c:716 line length of 101 exceeds 100 columns
[QVGA_YUV_RESOLUTION] = GC2145_VIDEO_FORMAT_CAP(320, 240, VIDEO_PIX_FMT_YUYV), /* QVGA */
[VGA_YUV_RESOLUTION] = GC2145_VIDEO_FORMAT_CAP(640, 480, VIDEO_PIX_FMT_YUYV), /* VGA */
[UXGA_YUV_RESOLUTION] = GC2145_VIDEO_FORMAT_CAP(1600, 1200, VIDEO_PIX_FMT_YUYV), /* UXGA */
[RESOLUTIONS_MAX] = {0},
};

Expand Down Expand Up @@ -893,13 +900,17 @@ static int gc2145_set_output_format(const struct device *dev, int output_format)
return ret;
}

if (output_format != VIDEO_PIX_FMT_RGB565) {
/* Map format to sensor format */
if (output_format == VIDEO_PIX_FMT_RGB565) {
output_format = GC2145_REG_OUTPUT_FMT_RGB565;
} else if (output_format == VIDEO_PIX_FMT_YUYV) {
output_format = GC2145_REG_OUTPUT_FMT_YCBYCR;
} else {
LOG_ERR("Image format not supported");
return -ENOTSUP;
}

/* Disable JPEG compression and set output to RGB565 */
ret = gc2145_write_reg(&cfg->i2c, GC2145_REG_OUTPUT_FMT, GC2145_REG_OUTPUT_FMT_RGB565);
ret = gc2145_write_reg(&cfg->i2c, GC2145_REG_OUTPUT_FMT, output_format);
if (ret < 0) {
return ret;
}
Expand Down Expand Up @@ -933,16 +944,16 @@ static int gc2145_set_resolution(const struct device *dev, enum resolutions res)
h = fmts[res].height_min;

/* Add the subsampling factor depending on resolution */
switch (res) {
case QVGA_RESOLUTION:
switch (w) {
case 320:
c_ratio = 3;
r_ratio = 3;
break;
case VGA_RESOLUTION:
case 640:
c_ratio = 2;
r_ratio = 2;
break;
case UXGA_RESOLUTION:
case 1600:
c_ratio = 1;
r_ratio = 1;
break;
Expand Down Expand Up @@ -1030,12 +1041,6 @@ static int gc2145_set_fmt(const struct device *dev, enum video_endpoint_id ep,
enum resolutions res = RESOLUTIONS_MAX;
int ret;

/* We only support RGB565 formats */
if (fmt->pixelformat != VIDEO_PIX_FMT_RGB565) {
LOG_ERR("gc2145 camera supports only RGB565");
return -ENOTSUP;
}

if (memcmp(&drv_data->fmt, fmt, sizeof(drv_data->fmt)) == 0) {
/* nothing to do */
return 0;
Expand Down Expand Up @@ -1161,9 +1166,9 @@ static int gc2145_init(const struct device *dev)

/* set default/init format QVGA RGB565 */
fmt.pixelformat = VIDEO_PIX_FMT_RGB565;
fmt.width = fmts[QVGA_RESOLUTION].width_min;
fmt.height = fmts[QVGA_RESOLUTION].height_min;
fmt.pitch = fmts[QVGA_RESOLUTION].width_min * 2;
fmt.width = fmts[QVGA_RGB_RESOLUTION].width_min;
fmt.height = fmts[QVGA_RGB_RESOLUTION].height_min;
fmt.pitch = fmts[QVGA_RGB_RESOLUTION].width_min * 2;

ret = gc2145_set_fmt(dev, VIDEO_EP_OUT, &fmt);
if (ret) {
Expand Down

0 comments on commit 4e82bf1

Please sign in to comment.