Skip to content

video: drivers misc fixes #87366

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
May 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions drivers/video/Kconfig.mcux_mipi_csi2rx
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,10 @@ config VIDEO_MCUX_MIPI_CSI2RX
default y
depends on DT_HAS_NXP_MIPI_CSI2RX_ENABLED
select VIDEO_MCUX_CSI

config VIDEO_MCUX_MIPI_CSI2RX_INIT_PRIORITY
int "NXP MCUX CSI-2 Rx init priority"
default 61
depends on VIDEO_MCUX_MIPI_CSI2RX
help
Initialization priority for the MIPI CSI-2 Rx device.
1 change: 0 additions & 1 deletion drivers/video/gc2145.c
Original file line number Diff line number Diff line change
Expand Up @@ -1183,7 +1183,6 @@ static int gc2145_init(const struct device *dev)
fmt.pixelformat = VIDEO_PIX_FMT_RGB565;
fmt.width = RESOLUTION_QVGA_W;
fmt.height = RESOLUTION_QVGA_H;
fmt.pitch = RESOLUTION_QVGA_W * 2;

ret = gc2145_set_fmt(dev, &fmt);
if (ret) {
Expand Down
56 changes: 24 additions & 32 deletions drivers/video/mt9m114.c
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ static struct mt9m114_reg mt9m114_1280_720[] = {
{MT9M114_CAM_STAT_AE_INITIAL_WINDOW_YEND, 2, 0x008F}, /* 143 */
{/* NULL terminated */}};

static struct mt9m114_resolution_config resolutionConfigs[] = {
static struct mt9m114_resolution_config resolution_configs[] = {
{.width = 480, .height = 272, .params = mt9m114_480_272},
{.width = 640, .height = 480, .params = mt9m114_640_480},
{.width = 1280, .height = 720, .params = mt9m114_1280_720},
Expand Down Expand Up @@ -432,10 +432,10 @@ static int mt9m114_set_fmt(const struct device *dev, struct video_format *fmt)
}

/* Set output resolution */
for (i = 0; i < ARRAY_SIZE(resolutionConfigs); i++) {
if (fmt->width == resolutionConfigs[i].width &&
fmt->height == resolutionConfigs[i].height) {
ret = mt9m114_write_all(dev, resolutionConfigs[i].params);
for (i = 0; i < ARRAY_SIZE(resolution_configs); i++) {
if (fmt->width == resolution_configs[i].width &&
fmt->height == resolution_configs[i].height) {
ret = mt9m114_write_all(dev, resolution_configs[i].params);
if (ret) {
LOG_ERR("Unable to set resolution");
return ret;
Expand Down Expand Up @@ -526,10 +526,16 @@ static int mt9m114_init_controls(const struct device *dev)

static int mt9m114_init(const struct device *dev)
{
const struct mt9m114_config *cfg = dev->config;
struct video_format fmt;
uint16_t val;
int ret;

if (!device_is_ready(cfg->i2c.bus)) {
LOG_ERR("Bus device is not ready");
return -ENODEV;
}

/* no power control, wait for camera ready */
k_sleep(K_MSEC(100));

Expand Down Expand Up @@ -558,7 +564,6 @@ static int mt9m114_init(const struct device *dev)
fmt.pixelformat = VIDEO_PIX_FMT_RGB565;
fmt.width = 480;
fmt.height = 272;
fmt.pitch = fmt.width * 2;

ret = mt9m114_set_fmt(dev, &fmt);
if (ret) {
Expand All @@ -573,29 +578,16 @@ static int mt9m114_init(const struct device *dev)
return mt9m114_init_controls(dev);
}

#if 1 /* Unique Instance */

static const struct mt9m114_config mt9m114_cfg_0 = {
.i2c = I2C_DT_SPEC_INST_GET(0),
};

static struct mt9m114_data mt9m114_data_0;

static int mt9m114_init_0(const struct device *dev)
{
const struct mt9m114_config *cfg = dev->config;

if (!device_is_ready(cfg->i2c.bus)) {
LOG_ERR("Bus device is not ready");
return -ENODEV;
}

return mt9m114_init(dev);
}

DEVICE_DT_INST_DEFINE(0, &mt9m114_init_0, NULL, &mt9m114_data_0, &mt9m114_cfg_0, POST_KERNEL,
CONFIG_VIDEO_INIT_PRIORITY, &mt9m114_driver_api);

VIDEO_DEVICE_DEFINE(mt9m114, DEVICE_DT_INST_GET(0), NULL);

#endif
#define MT9M114_INIT(n) \
static struct mt9m114_data mt9m114_data_##n; \
\
static const struct mt9m114_config mt9m114_cfg_##n = { \
.i2c = I2C_DT_SPEC_INST_GET(n), \
}; \
\
DEVICE_DT_INST_DEFINE(n, &mt9m114_init, NULL, &mt9m114_data_##n, &mt9m114_cfg_##n, \
POST_KERNEL, CONFIG_VIDEO_INIT_PRIORITY, &mt9m114_driver_api); \
\
VIDEO_DEVICE_DEFINE(mt9m114_##n, DEVICE_DT_INST_GET(n), NULL);

DT_INST_FOREACH_STATUS_OKAY(MT9M114_INIT)
1 change: 0 additions & 1 deletion drivers/video/ov2640.c
Original file line number Diff line number Diff line change
Expand Up @@ -1032,7 +1032,6 @@ static int ov2640_init(const struct device *dev)
fmt.pixelformat = VIDEO_PIX_FMT_RGB565;
fmt.width = SVGA_HSIZE;
fmt.height = SVGA_VSIZE;
fmt.pitch = SVGA_HSIZE * 2;
ret = ov2640_set_fmt(dev, &fmt);
if (ret) {
LOG_ERR("Unable to configure default format");
Expand Down
29 changes: 19 additions & 10 deletions drivers/video/ov5640.c
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,6 @@ struct ov5640_ctrls {
struct ov5640_data {
struct ov5640_ctrls ctrls;
struct video_format fmt;
uint64_t cur_pixrate;
uint16_t cur_frmrate;
const struct ov5640_mode_config *cur_mode;
};
Expand Down Expand Up @@ -811,10 +810,9 @@ static int ov5640_set_frmival(const struct device *dev, struct video_frmival *fr
}

drv_data->cur_frmrate = best_match;
drv_data->cur_pixrate = drv_data->cur_mode->mipi_frmrate_config[ind].pixelrate;

/* Update pixerate control */
drv_data->ctrls.pixel_rate.val = drv_data->cur_pixrate;
drv_data->ctrls.pixel_rate.val64 = drv_data->cur_mode->mipi_frmrate_config[ind].pixelrate;

frmival->numerator = 1;
frmival->denominator = best_match;
Expand Down Expand Up @@ -994,9 +992,13 @@ static int ov5640_set_ctrl_hue(const struct device *dev, int value)
sign = 0x02;
}

struct ov5640_reg hue_params[] = {{SDE_CTRL8_REG, sign},
{SDE_CTRL1_REG, abs(cos_coef)},
{SDE_CTRL2_REG, abs(sin_coef)}};
struct ov5640_reg hue_params[] = {{SDE_CTRL1_REG, abs(cos_coef) & 0xFF},
{SDE_CTRL2_REG, abs(sin_coef) & 0xFF}};

ret = ov5640_modify_reg(&cfg->i2c, SDE_CTRL8_REG, 0x7F, sign);
if (ret < 0) {
return ret;
}

return ov5640_write_multi_regs(&cfg->i2c, hue_params, ARRAY_SIZE(hue_params));
}
Expand All @@ -1019,15 +1021,18 @@ static int ov5640_set_ctrl_brightness(const struct device *dev, int value)
{
const struct ov5640_config *cfg = dev->config;

struct ov5640_reg brightness_params[] = {{SDE_CTRL8_REG, value >= 0 ? 0x01 : 0x09},
{SDE_CTRL7_REG, abs(value) & 0xff}};
int ret = ov5640_modify_reg(&cfg->i2c, SDE_CTRL0_REG, BIT(2), BIT(2));

if (ret) {
return ret;
}

return ov5640_write_multi_regs(&cfg->i2c, brightness_params, ARRAY_SIZE(brightness_params));
ret = ov5640_modify_reg(&cfg->i2c, SDE_CTRL8_REG, BIT(3), value >= 0 ? 0 : BIT(3));
if (ret < 0) {
return ret;
}

return ov5640_write_reg(&cfg->i2c, SDE_CTRL7_REG, (abs(value) << 4) & 0xf0);
}

static int ov5640_set_ctrl_contrast(const struct device *dev, int value)
Expand All @@ -1040,6 +1045,11 @@ static int ov5640_set_ctrl_contrast(const struct device *dev, int value)
return ret;
}

ret = ov5640_modify_reg(&cfg->i2c, SDE_CTRL6_REG, BIT(2), value >= 0 ? 0 : BIT(2));
if (ret < 0) {
return ret;
}

return ov5640_write_reg(&cfg->i2c, SDE_CTRL6_REG, value & 0xff);
}

Expand Down Expand Up @@ -1423,7 +1433,6 @@ static int ov5640_init(const struct device *dev)
fmt.width = 1280;
fmt.height = 720;
}
fmt.pitch = fmt.width * 2;
ret = ov5640_set_fmt(dev, &fmt);
if (ret) {
LOG_ERR("Unable to configure default format");
Expand Down
1 change: 0 additions & 1 deletion drivers/video/ov7670.c
Original file line number Diff line number Diff line change
Expand Up @@ -554,7 +554,6 @@ static int ov7670_init(const struct device *dev)
fmt.pixelformat = VIDEO_PIX_FMT_YUYV;
fmt.width = 640;
fmt.height = 480;
fmt.pitch = fmt.width * 2;
ret = ov7670_set_fmt(dev, &fmt);
if (ret < 0) {
return ret;
Expand Down
1 change: 0 additions & 1 deletion drivers/video/ov7725.c
Original file line number Diff line number Diff line change
Expand Up @@ -592,7 +592,6 @@ static int ov7725_init(const struct device *dev)
fmt.pixelformat = VIDEO_PIX_FMT_RGB565;
fmt.width = 640;
fmt.height = 480;
fmt.pitch = 640 * 2;
ret = ov7725_set_fmt(dev, &fmt);
if (ret) {
LOG_ERR("Unable to configure default format");
Expand Down
1 change: 0 additions & 1 deletion drivers/video/video_emul_imager.c
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,6 @@ int emul_imager_init(const struct device *dev)
fmt.pixelformat = fmts[0].pixelformat;
fmt.width = fmts[0].width_min;
fmt.height = fmts[0].height_min;
fmt.pitch = fmt.width * 2;

ret = emul_imager_set_fmt(dev, &fmt);
if (ret < 0) {
Expand Down
5 changes: 5 additions & 0 deletions drivers/video/video_emul_rx.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,9 @@ static int emul_rx_set_fmt(const struct device *const dev, struct video_format *
}

/* Cache the format selected locally to use it for getting the size of the buffer */
fmt->pitch = fmt->width * video_bits_per_pixel(fmt->pixelformat) / BITS_PER_BYTE;
Copy link
Collaborator

@josuah josuah May 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a small semantic change and I proposed a migration-guide entry here to avoid getting in the way of this PR:

This is not a breaking change, and a migration guide entry is only a hint for how to simplify applications after 4.2.

data->fmt = *fmt;

return 0;
}

Expand Down Expand Up @@ -216,6 +218,9 @@ int emul_rx_init(const struct device *dev)
return ret;
}

data->fmt.pitch =
data->fmt.width * video_bits_per_pixel(data->fmt.pixelformat) / BITS_PER_BYTE;

k_fifo_init(&data->fifo_in);
k_fifo_init(&data->fifo_out);
k_work_init(&data->work, &emul_rx_worker);
Expand Down
12 changes: 11 additions & 1 deletion drivers/video/video_esp32_dvp.c
Original file line number Diff line number Diff line change
Expand Up @@ -269,21 +269,31 @@ static int video_esp32_get_fmt(const struct device *dev, struct video_format *fm
return ret;
}

fmt->pitch = fmt->width * video_bits_per_pixel(fmt->pixelformat) / BITS_PER_BYTE;

return 0;
}

static int video_esp32_set_fmt(const struct device *dev, struct video_format *fmt)
{
const struct video_esp32_config *cfg = dev->config;
struct video_esp32_data *data = dev->data;
int ret;

if (fmt == NULL) {
return -EINVAL;
}

ret = video_set_format(cfg->source_dev, fmt);
if (ret < 0) {
return ret;
}

fmt->pitch = fmt->width * video_bits_per_pixel(fmt->pixelformat) / BITS_PER_BYTE;

data->video_format = *fmt;

return video_set_format(cfg->source_dev, fmt);
return 0;
}

static int video_esp32_enqueue(const struct device *dev, struct video_buffer *vbuf)
Expand Down
15 changes: 6 additions & 9 deletions drivers/video/video_mcux_csi.c
Original file line number Diff line number Diff line change
Expand Up @@ -133,16 +133,11 @@ static int video_mcux_csi_set_fmt(const struct device *dev, struct video_format
{
const struct video_mcux_csi_config *config = dev->config;
struct video_mcux_csi_data *data = dev->data;
unsigned int bpp = video_bits_per_pixel(fmt->pixelformat) / BITS_PER_BYTE;
status_t ret;
struct video_format format = *fmt;

if (bpp == 0) {
return -EINVAL;
}

data->csi_config.bytesPerPixel = bpp;
data->csi_config.linePitch_Bytes = fmt->pitch;
data->csi_config.bytesPerPixel = video_bits_per_pixel(fmt->pixelformat) / BITS_PER_BYTE;
data->csi_config.linePitch_Bytes = fmt->width * data->csi_config.bytesPerPixel;
#if defined(CONFIG_VIDEO_MCUX_MIPI_CSI2RX)
if (fmt->pixelformat != VIDEO_PIX_FMT_XRGB32 && fmt->pixelformat != VIDEO_PIX_FMT_XYUV32) {
return -ENOTSUP;
Expand Down Expand Up @@ -172,6 +167,8 @@ static int video_mcux_csi_set_fmt(const struct device *dev, struct video_format
return -EIO;
}

fmt->pitch = data->csi_config.linePitch_Bytes;

return 0;
}

Expand All @@ -187,8 +184,8 @@ static int video_mcux_csi_get_fmt(const struct device *dev, struct video_format
#if defined(CONFIG_VIDEO_MCUX_MIPI_CSI2RX)
video_pix_fmt_convert(fmt, true);
#endif
/* align CSI with source fmt */
return video_mcux_csi_set_fmt(dev, fmt);

return 0;
}

return -EIO;
Expand Down
24 changes: 14 additions & 10 deletions drivers/video/video_mcux_mipi_csi2rx.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ struct mipi_csi2rx_data {
};

struct mipi_csi2rx_tHsSettleEscClk_config {
uint64_t pixel_rate;
uint32_t pixel_rate;
uint8_t tHsSettle_EscClk;
};

Expand Down Expand Up @@ -193,29 +193,32 @@ static int mipi_csi2rx_get_frmival(const struct device *dev, struct video_frmiva
return video_get_frmival(config->sensor_dev, frmival);
}

static uint64_t mipi_csi2rx_cal_frame_size(const struct video_format *fmt)
static uint32_t mipi_csi2rx_cal_frame_size(const struct video_format *fmt)
{
return fmt->height * fmt->width * video_bits_per_pixel(fmt->pixelformat);
}

static uint64_t mipi_csi2rx_estimate_pixel_rate(const struct video_frmival *cur_fmival,
static uint32_t mipi_csi2rx_estimate_pixel_rate(const struct video_frmival *cur_fmival,
const struct video_frmival *fie_frmival,
const struct video_format *cur_format,
const struct video_format *fie_format,
uint64_t cur_pixel_rate, uint8_t laneNum)
uint32_t cur_pixel_rate, uint8_t laneNum)
{
return mipi_csi2rx_cal_frame_size(cur_format) * fie_frmival->denominator *
cur_fmival->numerator * cur_pixel_rate /
(mipi_csi2rx_cal_frame_size(fie_format) * fie_frmival->numerator *
cur_fmival->denominator);
uint64_t numerator = mipi_csi2rx_cal_frame_size(cur_format) * fie_frmival->denominator *
cur_fmival->numerator * cur_pixel_rate;

uint64_t denominator = mipi_csi2rx_cal_frame_size(fie_format) * fie_frmival->numerator *
cur_fmival->denominator;

return numerator / denominator;
}

static int mipi_csi2rx_enum_frmival(const struct device *dev, struct video_frmival_enum *fie)
{
const struct mipi_csi2rx_config *config = dev->config;
struct mipi_csi2rx_data *drv_data = dev->data;
int ret;
uint64_t est_pixel_rate;
uint32_t est_pixel_rate;
struct video_frmival cur_frmival;
struct video_format cur_fmt;
struct video_control sensor_rate = {.id = VIDEO_CID_PIXEL_RATE, .val64 = -1};
Expand Down Expand Up @@ -327,7 +330,8 @@ static int mipi_csi2rx_init(const struct device *dev)
}; \
\
DEVICE_DT_INST_DEFINE(n, &mipi_csi2rx_init, NULL, &mipi_csi2rx_data_##n, \
&mipi_csi2rx_config_##n, POST_KERNEL, CONFIG_VIDEO_INIT_PRIORITY, \
&mipi_csi2rx_config_##n, POST_KERNEL, \
CONFIG_VIDEO_MCUX_MIPI_CSI2RX_INIT_PRIORITY, \
&mipi_csi2rx_driver_api); \
\
VIDEO_DEVICE_DEFINE(mipi_csi2rx_##n, DEVICE_DT_INST_GET(n), SOURCE_DEV(n));
Expand Down
Loading