1f8de593dSPetr Cvek // SPDX-License-Identifier: GPL-2.0
257b0ad9eSPetr Cvek /*
357b0ad9eSPetr Cvek * OmniVision OV96xx Camera Driver
457b0ad9eSPetr Cvek *
557b0ad9eSPetr Cvek * Copyright (C) 2009 Marek Vasut <marek.vasut@gmail.com>
657b0ad9eSPetr Cvek *
757b0ad9eSPetr Cvek * Based on ov772x camera driver:
857b0ad9eSPetr Cvek *
957b0ad9eSPetr Cvek * Copyright (C) 2008 Renesas Solutions Corp.
1057b0ad9eSPetr Cvek * Kuninori Morimoto <morimoto.kuninori@renesas.com>
1157b0ad9eSPetr Cvek *
1257b0ad9eSPetr Cvek * Based on ov7670 and soc_camera_platform driver,
139f7e55d2SPetr Cvek * transition from soc_camera to pxa_camera based on mt9m111
1457b0ad9eSPetr Cvek *
1557b0ad9eSPetr Cvek * Copyright 2006-7 Jonathan Corbet <corbet@lwn.net>
1657b0ad9eSPetr Cvek * Copyright (C) 2008 Magnus Damm
1757b0ad9eSPetr Cvek * Copyright (C) 2008, Guennadi Liakhovetski <kernel@pengutronix.de>
1857b0ad9eSPetr Cvek */
1957b0ad9eSPetr Cvek
208de14b3aSEzequiel Garcia #include <linux/clk.h>
2157b0ad9eSPetr Cvek #include <linux/init.h>
2257b0ad9eSPetr Cvek #include <linux/module.h>
2357b0ad9eSPetr Cvek #include <linux/i2c.h>
2457b0ad9eSPetr Cvek #include <linux/slab.h>
2557b0ad9eSPetr Cvek #include <linux/delay.h>
2657b0ad9eSPetr Cvek #include <linux/v4l2-mediabus.h>
2757b0ad9eSPetr Cvek #include <linux/videodev2.h>
2857b0ad9eSPetr Cvek
299f7e55d2SPetr Cvek #include <media/v4l2-async.h>
3057b0ad9eSPetr Cvek #include <media/v4l2-common.h>
3157b0ad9eSPetr Cvek #include <media/v4l2-ctrls.h>
329f7e55d2SPetr Cvek #include <media/v4l2-device.h>
339f7e55d2SPetr Cvek #include <media/v4l2-event.h>
349f7e55d2SPetr Cvek
359f7e55d2SPetr Cvek #include <linux/gpio/consumer.h>
3657b0ad9eSPetr Cvek
3757b0ad9eSPetr Cvek #include "ov9640.h"
3857b0ad9eSPetr Cvek
3957b0ad9eSPetr Cvek #define to_ov9640_sensor(sd) container_of(sd, struct ov9640_priv, subdev)
4057b0ad9eSPetr Cvek
4157b0ad9eSPetr Cvek /* default register setup */
4257b0ad9eSPetr Cvek static const struct ov9640_reg ov9640_regs_dflt[] = {
4357b0ad9eSPetr Cvek { OV9640_COM5, OV9640_COM5_SYSCLK | OV9640_COM5_LONGEXP },
4457b0ad9eSPetr Cvek { OV9640_COM6, OV9640_COM6_OPT_BLC | OV9640_COM6_ADBLC_BIAS |
4557b0ad9eSPetr Cvek OV9640_COM6_FMT_RST | OV9640_COM6_ADBLC_OPTEN },
4657b0ad9eSPetr Cvek { OV9640_PSHFT, OV9640_PSHFT_VAL(0x01) },
4757b0ad9eSPetr Cvek { OV9640_ACOM, OV9640_ACOM_2X_ANALOG | OV9640_ACOM_RSVD },
4857b0ad9eSPetr Cvek { OV9640_TSLB, OV9640_TSLB_YUYV_UYVY },
4957b0ad9eSPetr Cvek { OV9640_COM16, OV9640_COM16_RB_AVG },
5057b0ad9eSPetr Cvek
5157b0ad9eSPetr Cvek /* Gamma curve P */
5257b0ad9eSPetr Cvek { 0x6c, 0x40 }, { 0x6d, 0x30 }, { 0x6e, 0x4b }, { 0x6f, 0x60 },
5357b0ad9eSPetr Cvek { 0x70, 0x70 }, { 0x71, 0x70 }, { 0x72, 0x70 }, { 0x73, 0x70 },
5457b0ad9eSPetr Cvek { 0x74, 0x60 }, { 0x75, 0x60 }, { 0x76, 0x50 }, { 0x77, 0x48 },
5557b0ad9eSPetr Cvek { 0x78, 0x3a }, { 0x79, 0x2e }, { 0x7a, 0x28 }, { 0x7b, 0x22 },
5657b0ad9eSPetr Cvek
5757b0ad9eSPetr Cvek /* Gamma curve T */
5857b0ad9eSPetr Cvek { 0x7c, 0x04 }, { 0x7d, 0x07 }, { 0x7e, 0x10 }, { 0x7f, 0x28 },
5957b0ad9eSPetr Cvek { 0x80, 0x36 }, { 0x81, 0x44 }, { 0x82, 0x52 }, { 0x83, 0x60 },
6057b0ad9eSPetr Cvek { 0x84, 0x6c }, { 0x85, 0x78 }, { 0x86, 0x8c }, { 0x87, 0x9e },
6157b0ad9eSPetr Cvek { 0x88, 0xbb }, { 0x89, 0xd2 }, { 0x8a, 0xe6 },
6257b0ad9eSPetr Cvek };
6357b0ad9eSPetr Cvek
6457b0ad9eSPetr Cvek /* Configurations
6557b0ad9eSPetr Cvek * NOTE: for YUV, alter the following registers:
6657b0ad9eSPetr Cvek * COM12 |= OV9640_COM12_YUV_AVG
6757b0ad9eSPetr Cvek *
6857b0ad9eSPetr Cvek * for RGB, alter the following registers:
6957b0ad9eSPetr Cvek * COM7 |= OV9640_COM7_RGB
7057b0ad9eSPetr Cvek * COM13 |= OV9640_COM13_RGB_AVG
7157b0ad9eSPetr Cvek * COM15 |= proper RGB color encoding mode
7257b0ad9eSPetr Cvek */
7357b0ad9eSPetr Cvek static const struct ov9640_reg ov9640_regs_qqcif[] = {
7457b0ad9eSPetr Cvek { OV9640_CLKRC, OV9640_CLKRC_DPLL_EN | OV9640_CLKRC_DIV(0x0f) },
7557b0ad9eSPetr Cvek { OV9640_COM1, OV9640_COM1_QQFMT | OV9640_COM1_HREF_2SKIP },
7657b0ad9eSPetr Cvek { OV9640_COM4, OV9640_COM4_QQ_VP | OV9640_COM4_RSVD },
7757b0ad9eSPetr Cvek { OV9640_COM7, OV9640_COM7_QCIF },
7857b0ad9eSPetr Cvek { OV9640_COM12, OV9640_COM12_RSVD },
7957b0ad9eSPetr Cvek { OV9640_COM13, OV9640_COM13_GAMMA_RAW | OV9640_COM13_MATRIX_EN },
8057b0ad9eSPetr Cvek { OV9640_COM15, OV9640_COM15_OR_10F0 },
8157b0ad9eSPetr Cvek };
8257b0ad9eSPetr Cvek
8357b0ad9eSPetr Cvek static const struct ov9640_reg ov9640_regs_qqvga[] = {
8457b0ad9eSPetr Cvek { OV9640_CLKRC, OV9640_CLKRC_DPLL_EN | OV9640_CLKRC_DIV(0x07) },
8557b0ad9eSPetr Cvek { OV9640_COM1, OV9640_COM1_QQFMT | OV9640_COM1_HREF_2SKIP },
8657b0ad9eSPetr Cvek { OV9640_COM4, OV9640_COM4_QQ_VP | OV9640_COM4_RSVD },
8757b0ad9eSPetr Cvek { OV9640_COM7, OV9640_COM7_QVGA },
8857b0ad9eSPetr Cvek { OV9640_COM12, OV9640_COM12_RSVD },
8957b0ad9eSPetr Cvek { OV9640_COM13, OV9640_COM13_GAMMA_RAW | OV9640_COM13_MATRIX_EN },
9057b0ad9eSPetr Cvek { OV9640_COM15, OV9640_COM15_OR_10F0 },
9157b0ad9eSPetr Cvek };
9257b0ad9eSPetr Cvek
9357b0ad9eSPetr Cvek static const struct ov9640_reg ov9640_regs_qcif[] = {
9457b0ad9eSPetr Cvek { OV9640_CLKRC, OV9640_CLKRC_DPLL_EN | OV9640_CLKRC_DIV(0x07) },
9557b0ad9eSPetr Cvek { OV9640_COM4, OV9640_COM4_QQ_VP | OV9640_COM4_RSVD },
9657b0ad9eSPetr Cvek { OV9640_COM7, OV9640_COM7_QCIF },
9757b0ad9eSPetr Cvek { OV9640_COM12, OV9640_COM12_RSVD },
9857b0ad9eSPetr Cvek { OV9640_COM13, OV9640_COM13_GAMMA_RAW | OV9640_COM13_MATRIX_EN },
9957b0ad9eSPetr Cvek { OV9640_COM15, OV9640_COM15_OR_10F0 },
10057b0ad9eSPetr Cvek };
10157b0ad9eSPetr Cvek
10257b0ad9eSPetr Cvek static const struct ov9640_reg ov9640_regs_qvga[] = {
10357b0ad9eSPetr Cvek { OV9640_CLKRC, OV9640_CLKRC_DPLL_EN | OV9640_CLKRC_DIV(0x03) },
10457b0ad9eSPetr Cvek { OV9640_COM4, OV9640_COM4_QQ_VP | OV9640_COM4_RSVD },
10557b0ad9eSPetr Cvek { OV9640_COM7, OV9640_COM7_QVGA },
10657b0ad9eSPetr Cvek { OV9640_COM12, OV9640_COM12_RSVD },
10757b0ad9eSPetr Cvek { OV9640_COM13, OV9640_COM13_GAMMA_RAW | OV9640_COM13_MATRIX_EN },
10857b0ad9eSPetr Cvek { OV9640_COM15, OV9640_COM15_OR_10F0 },
10957b0ad9eSPetr Cvek };
11057b0ad9eSPetr Cvek
11157b0ad9eSPetr Cvek static const struct ov9640_reg ov9640_regs_cif[] = {
11257b0ad9eSPetr Cvek { OV9640_CLKRC, OV9640_CLKRC_DPLL_EN | OV9640_CLKRC_DIV(0x03) },
11357b0ad9eSPetr Cvek { OV9640_COM3, OV9640_COM3_VP },
11457b0ad9eSPetr Cvek { OV9640_COM7, OV9640_COM7_CIF },
11557b0ad9eSPetr Cvek { OV9640_COM12, OV9640_COM12_RSVD },
11657b0ad9eSPetr Cvek { OV9640_COM13, OV9640_COM13_GAMMA_RAW | OV9640_COM13_MATRIX_EN },
11757b0ad9eSPetr Cvek { OV9640_COM15, OV9640_COM15_OR_10F0 },
11857b0ad9eSPetr Cvek };
11957b0ad9eSPetr Cvek
12057b0ad9eSPetr Cvek static const struct ov9640_reg ov9640_regs_vga[] = {
12157b0ad9eSPetr Cvek { OV9640_CLKRC, OV9640_CLKRC_DPLL_EN | OV9640_CLKRC_DIV(0x01) },
12257b0ad9eSPetr Cvek { OV9640_COM3, OV9640_COM3_VP },
12357b0ad9eSPetr Cvek { OV9640_COM7, OV9640_COM7_VGA },
12457b0ad9eSPetr Cvek { OV9640_COM12, OV9640_COM12_RSVD },
12557b0ad9eSPetr Cvek { OV9640_COM13, OV9640_COM13_GAMMA_RAW | OV9640_COM13_MATRIX_EN },
12657b0ad9eSPetr Cvek { OV9640_COM15, OV9640_COM15_OR_10F0 },
12757b0ad9eSPetr Cvek };
12857b0ad9eSPetr Cvek
12957b0ad9eSPetr Cvek static const struct ov9640_reg ov9640_regs_sxga[] = {
13057b0ad9eSPetr Cvek { OV9640_CLKRC, OV9640_CLKRC_DPLL_EN | OV9640_CLKRC_DIV(0x01) },
13157b0ad9eSPetr Cvek { OV9640_COM3, OV9640_COM3_VP },
13257b0ad9eSPetr Cvek { OV9640_COM7, 0 },
13357b0ad9eSPetr Cvek { OV9640_COM12, OV9640_COM12_RSVD },
13457b0ad9eSPetr Cvek { OV9640_COM13, OV9640_COM13_GAMMA_RAW | OV9640_COM13_MATRIX_EN },
13557b0ad9eSPetr Cvek { OV9640_COM15, OV9640_COM15_OR_10F0 },
13657b0ad9eSPetr Cvek };
13757b0ad9eSPetr Cvek
13857b0ad9eSPetr Cvek static const struct ov9640_reg ov9640_regs_yuv[] = {
13957b0ad9eSPetr Cvek { OV9640_MTX1, 0x58 },
14057b0ad9eSPetr Cvek { OV9640_MTX2, 0x48 },
14157b0ad9eSPetr Cvek { OV9640_MTX3, 0x10 },
14257b0ad9eSPetr Cvek { OV9640_MTX4, 0x28 },
14357b0ad9eSPetr Cvek { OV9640_MTX5, 0x48 },
14457b0ad9eSPetr Cvek { OV9640_MTX6, 0x70 },
14557b0ad9eSPetr Cvek { OV9640_MTX7, 0x40 },
14657b0ad9eSPetr Cvek { OV9640_MTX8, 0x40 },
14757b0ad9eSPetr Cvek { OV9640_MTX9, 0x40 },
14857b0ad9eSPetr Cvek { OV9640_MTXS, 0x0f },
14957b0ad9eSPetr Cvek };
15057b0ad9eSPetr Cvek
15157b0ad9eSPetr Cvek static const struct ov9640_reg ov9640_regs_rgb[] = {
15257b0ad9eSPetr Cvek { OV9640_MTX1, 0x71 },
15357b0ad9eSPetr Cvek { OV9640_MTX2, 0x3e },
15457b0ad9eSPetr Cvek { OV9640_MTX3, 0x0c },
15557b0ad9eSPetr Cvek { OV9640_MTX4, 0x33 },
15657b0ad9eSPetr Cvek { OV9640_MTX5, 0x72 },
15757b0ad9eSPetr Cvek { OV9640_MTX6, 0x00 },
15857b0ad9eSPetr Cvek { OV9640_MTX7, 0x2b },
15957b0ad9eSPetr Cvek { OV9640_MTX8, 0x66 },
16057b0ad9eSPetr Cvek { OV9640_MTX9, 0xd2 },
16157b0ad9eSPetr Cvek { OV9640_MTXS, 0x65 },
16257b0ad9eSPetr Cvek };
16357b0ad9eSPetr Cvek
16495531e46SPetr Cvek static const u32 ov9640_codes[] = {
16557b0ad9eSPetr Cvek MEDIA_BUS_FMT_UYVY8_2X8,
16657b0ad9eSPetr Cvek MEDIA_BUS_FMT_RGB555_2X8_PADHI_LE,
16757b0ad9eSPetr Cvek MEDIA_BUS_FMT_RGB565_2X8_LE,
16857b0ad9eSPetr Cvek };
16957b0ad9eSPetr Cvek
17057b0ad9eSPetr Cvek /* read a register */
ov9640_reg_read(struct i2c_client * client,u8 reg,u8 * val)17157b0ad9eSPetr Cvek static int ov9640_reg_read(struct i2c_client *client, u8 reg, u8 *val)
17257b0ad9eSPetr Cvek {
17357b0ad9eSPetr Cvek int ret;
17457b0ad9eSPetr Cvek u8 data = reg;
17557b0ad9eSPetr Cvek struct i2c_msg msg = {
17657b0ad9eSPetr Cvek .addr = client->addr,
17757b0ad9eSPetr Cvek .flags = 0,
17857b0ad9eSPetr Cvek .len = 1,
17957b0ad9eSPetr Cvek .buf = &data,
18057b0ad9eSPetr Cvek };
18157b0ad9eSPetr Cvek
18257b0ad9eSPetr Cvek ret = i2c_transfer(client->adapter, &msg, 1);
18357b0ad9eSPetr Cvek if (ret < 0)
18457b0ad9eSPetr Cvek goto err;
18557b0ad9eSPetr Cvek
18657b0ad9eSPetr Cvek msg.flags = I2C_M_RD;
18757b0ad9eSPetr Cvek ret = i2c_transfer(client->adapter, &msg, 1);
18857b0ad9eSPetr Cvek if (ret < 0)
18957b0ad9eSPetr Cvek goto err;
19057b0ad9eSPetr Cvek
19157b0ad9eSPetr Cvek *val = data;
19257b0ad9eSPetr Cvek return 0;
19357b0ad9eSPetr Cvek
19457b0ad9eSPetr Cvek err:
19557b0ad9eSPetr Cvek dev_err(&client->dev, "Failed reading register 0x%02x!\n", reg);
19657b0ad9eSPetr Cvek return ret;
19757b0ad9eSPetr Cvek }
19857b0ad9eSPetr Cvek
19957b0ad9eSPetr Cvek /* write a register */
ov9640_reg_write(struct i2c_client * client,u8 reg,u8 val)20057b0ad9eSPetr Cvek static int ov9640_reg_write(struct i2c_client *client, u8 reg, u8 val)
20157b0ad9eSPetr Cvek {
20257b0ad9eSPetr Cvek int ret;
20357b0ad9eSPetr Cvek u8 _val;
20457b0ad9eSPetr Cvek unsigned char data[2] = { reg, val };
20557b0ad9eSPetr Cvek struct i2c_msg msg = {
20657b0ad9eSPetr Cvek .addr = client->addr,
20757b0ad9eSPetr Cvek .flags = 0,
20857b0ad9eSPetr Cvek .len = 2,
20957b0ad9eSPetr Cvek .buf = data,
21057b0ad9eSPetr Cvek };
21157b0ad9eSPetr Cvek
21257b0ad9eSPetr Cvek ret = i2c_transfer(client->adapter, &msg, 1);
21357b0ad9eSPetr Cvek if (ret < 0) {
21457b0ad9eSPetr Cvek dev_err(&client->dev, "Failed writing register 0x%02x!\n", reg);
21557b0ad9eSPetr Cvek return ret;
21657b0ad9eSPetr Cvek }
21757b0ad9eSPetr Cvek
21857b0ad9eSPetr Cvek /* we have to read the register back ... no idea why, maybe HW bug */
21957b0ad9eSPetr Cvek ret = ov9640_reg_read(client, reg, &_val);
22057b0ad9eSPetr Cvek if (ret)
22157b0ad9eSPetr Cvek dev_err(&client->dev,
22257b0ad9eSPetr Cvek "Failed reading back register 0x%02x!\n", reg);
22357b0ad9eSPetr Cvek
22457b0ad9eSPetr Cvek return 0;
22557b0ad9eSPetr Cvek }
22657b0ad9eSPetr Cvek
22757b0ad9eSPetr Cvek
22857b0ad9eSPetr Cvek /* Read a register, alter its bits, write it back */
ov9640_reg_rmw(struct i2c_client * client,u8 reg,u8 set,u8 unset)22957b0ad9eSPetr Cvek static int ov9640_reg_rmw(struct i2c_client *client, u8 reg, u8 set, u8 unset)
23057b0ad9eSPetr Cvek {
23157b0ad9eSPetr Cvek u8 val;
23257b0ad9eSPetr Cvek int ret;
23357b0ad9eSPetr Cvek
23457b0ad9eSPetr Cvek ret = ov9640_reg_read(client, reg, &val);
23557b0ad9eSPetr Cvek if (ret) {
23657b0ad9eSPetr Cvek dev_err(&client->dev,
23757b0ad9eSPetr Cvek "[Read]-Modify-Write of register %02x failed!\n", reg);
23857b0ad9eSPetr Cvek return ret;
23957b0ad9eSPetr Cvek }
24057b0ad9eSPetr Cvek
24157b0ad9eSPetr Cvek val |= set;
24257b0ad9eSPetr Cvek val &= ~unset;
24357b0ad9eSPetr Cvek
24457b0ad9eSPetr Cvek ret = ov9640_reg_write(client, reg, val);
24557b0ad9eSPetr Cvek if (ret)
24657b0ad9eSPetr Cvek dev_err(&client->dev,
24757b0ad9eSPetr Cvek "Read-Modify-[Write] of register %02x failed!\n", reg);
24857b0ad9eSPetr Cvek
24957b0ad9eSPetr Cvek return ret;
25057b0ad9eSPetr Cvek }
25157b0ad9eSPetr Cvek
25257b0ad9eSPetr Cvek /* Soft reset the camera. This has nothing to do with the RESET pin! */
ov9640_reset(struct i2c_client * client)25357b0ad9eSPetr Cvek static int ov9640_reset(struct i2c_client *client)
25457b0ad9eSPetr Cvek {
25557b0ad9eSPetr Cvek int ret;
25657b0ad9eSPetr Cvek
25757b0ad9eSPetr Cvek ret = ov9640_reg_write(client, OV9640_COM7, OV9640_COM7_SCCB_RESET);
25857b0ad9eSPetr Cvek if (ret)
25957b0ad9eSPetr Cvek dev_err(&client->dev,
26057b0ad9eSPetr Cvek "An error occurred while entering soft reset!\n");
26157b0ad9eSPetr Cvek
26257b0ad9eSPetr Cvek return ret;
26357b0ad9eSPetr Cvek }
26457b0ad9eSPetr Cvek
26557b0ad9eSPetr Cvek /* Start/Stop streaming from the device */
ov9640_s_stream(struct v4l2_subdev * sd,int enable)26657b0ad9eSPetr Cvek static int ov9640_s_stream(struct v4l2_subdev *sd, int enable)
26757b0ad9eSPetr Cvek {
26857b0ad9eSPetr Cvek return 0;
26957b0ad9eSPetr Cvek }
27057b0ad9eSPetr Cvek
27157b0ad9eSPetr Cvek /* Set status of additional camera capabilities */
ov9640_s_ctrl(struct v4l2_ctrl * ctrl)27257b0ad9eSPetr Cvek static int ov9640_s_ctrl(struct v4l2_ctrl *ctrl)
27357b0ad9eSPetr Cvek {
27416aaf112SSakari Ailus struct ov9640_priv *priv = container_of(ctrl->handler,
27516aaf112SSakari Ailus struct ov9640_priv, hdl);
27657b0ad9eSPetr Cvek struct i2c_client *client = v4l2_get_subdevdata(&priv->subdev);
27757b0ad9eSPetr Cvek
27857b0ad9eSPetr Cvek switch (ctrl->id) {
27957b0ad9eSPetr Cvek case V4L2_CID_VFLIP:
28057b0ad9eSPetr Cvek if (ctrl->val)
28157b0ad9eSPetr Cvek return ov9640_reg_rmw(client, OV9640_MVFP,
28257b0ad9eSPetr Cvek OV9640_MVFP_V, 0);
28357b0ad9eSPetr Cvek return ov9640_reg_rmw(client, OV9640_MVFP, 0, OV9640_MVFP_V);
28457b0ad9eSPetr Cvek case V4L2_CID_HFLIP:
28557b0ad9eSPetr Cvek if (ctrl->val)
28657b0ad9eSPetr Cvek return ov9640_reg_rmw(client, OV9640_MVFP,
28757b0ad9eSPetr Cvek OV9640_MVFP_H, 0);
28857b0ad9eSPetr Cvek return ov9640_reg_rmw(client, OV9640_MVFP, 0, OV9640_MVFP_H);
28957b0ad9eSPetr Cvek }
290856b0b8fSPetr Cvek
29157b0ad9eSPetr Cvek return -EINVAL;
29257b0ad9eSPetr Cvek }
29357b0ad9eSPetr Cvek
29457b0ad9eSPetr Cvek #ifdef CONFIG_VIDEO_ADV_DEBUG
ov9640_get_register(struct v4l2_subdev * sd,struct v4l2_dbg_register * reg)29557b0ad9eSPetr Cvek static int ov9640_get_register(struct v4l2_subdev *sd,
29657b0ad9eSPetr Cvek struct v4l2_dbg_register *reg)
29757b0ad9eSPetr Cvek {
29857b0ad9eSPetr Cvek struct i2c_client *client = v4l2_get_subdevdata(sd);
29957b0ad9eSPetr Cvek int ret;
30057b0ad9eSPetr Cvek u8 val;
30157b0ad9eSPetr Cvek
30257b0ad9eSPetr Cvek if (reg->reg & ~0xff)
30357b0ad9eSPetr Cvek return -EINVAL;
30457b0ad9eSPetr Cvek
30557b0ad9eSPetr Cvek reg->size = 1;
30657b0ad9eSPetr Cvek
30757b0ad9eSPetr Cvek ret = ov9640_reg_read(client, reg->reg, &val);
30857b0ad9eSPetr Cvek if (ret)
30957b0ad9eSPetr Cvek return ret;
31057b0ad9eSPetr Cvek
31157b0ad9eSPetr Cvek reg->val = (__u64)val;
31257b0ad9eSPetr Cvek
31357b0ad9eSPetr Cvek return 0;
31457b0ad9eSPetr Cvek }
31557b0ad9eSPetr Cvek
ov9640_set_register(struct v4l2_subdev * sd,const struct v4l2_dbg_register * reg)31657b0ad9eSPetr Cvek static int ov9640_set_register(struct v4l2_subdev *sd,
31757b0ad9eSPetr Cvek const struct v4l2_dbg_register *reg)
31857b0ad9eSPetr Cvek {
31957b0ad9eSPetr Cvek struct i2c_client *client = v4l2_get_subdevdata(sd);
32057b0ad9eSPetr Cvek
32157b0ad9eSPetr Cvek if (reg->reg & ~0xff || reg->val & ~0xff)
32257b0ad9eSPetr Cvek return -EINVAL;
32357b0ad9eSPetr Cvek
32457b0ad9eSPetr Cvek return ov9640_reg_write(client, reg->reg, reg->val);
32557b0ad9eSPetr Cvek }
32657b0ad9eSPetr Cvek #endif
32757b0ad9eSPetr Cvek
ov9640_s_power(struct v4l2_subdev * sd,int on)32857b0ad9eSPetr Cvek static int ov9640_s_power(struct v4l2_subdev *sd, int on)
32957b0ad9eSPetr Cvek {
33057b0ad9eSPetr Cvek struct ov9640_priv *priv = to_ov9640_sensor(sd);
3319f7e55d2SPetr Cvek int ret = 0;
33257b0ad9eSPetr Cvek
3339f7e55d2SPetr Cvek if (on) {
3349f7e55d2SPetr Cvek gpiod_set_value(priv->gpio_power, 1);
3359f7e55d2SPetr Cvek usleep_range(1000, 2000);
3368de14b3aSEzequiel Garcia ret = clk_prepare_enable(priv->clk);
3379f7e55d2SPetr Cvek usleep_range(1000, 2000);
3389f7e55d2SPetr Cvek gpiod_set_value(priv->gpio_reset, 0);
3399f7e55d2SPetr Cvek } else {
3409f7e55d2SPetr Cvek gpiod_set_value(priv->gpio_reset, 1);
3419f7e55d2SPetr Cvek usleep_range(1000, 2000);
3428de14b3aSEzequiel Garcia clk_disable_unprepare(priv->clk);
3439f7e55d2SPetr Cvek usleep_range(1000, 2000);
3449f7e55d2SPetr Cvek gpiod_set_value(priv->gpio_power, 0);
3459f7e55d2SPetr Cvek }
346856b0b8fSPetr Cvek
3479f7e55d2SPetr Cvek return ret;
34857b0ad9eSPetr Cvek }
34957b0ad9eSPetr Cvek
35057b0ad9eSPetr Cvek /* select nearest higher resolution for capture */
ov9640_res_roundup(u32 * width,u32 * height)35157b0ad9eSPetr Cvek static void ov9640_res_roundup(u32 *width, u32 *height)
35257b0ad9eSPetr Cvek {
35374d01576SPetr Cvek unsigned int i;
35457b0ad9eSPetr Cvek enum { QQCIF, QQVGA, QCIF, QVGA, CIF, VGA, SXGA };
35574d01576SPetr Cvek static const u32 res_x[] = { 88, 160, 176, 320, 352, 640, 1280 };
35674d01576SPetr Cvek static const u32 res_y[] = { 72, 120, 144, 240, 288, 480, 960 };
35757b0ad9eSPetr Cvek
35857b0ad9eSPetr Cvek for (i = 0; i < ARRAY_SIZE(res_x); i++) {
35957b0ad9eSPetr Cvek if (res_x[i] >= *width && res_y[i] >= *height) {
36057b0ad9eSPetr Cvek *width = res_x[i];
36157b0ad9eSPetr Cvek *height = res_y[i];
36257b0ad9eSPetr Cvek return;
36357b0ad9eSPetr Cvek }
36457b0ad9eSPetr Cvek }
36557b0ad9eSPetr Cvek
36657b0ad9eSPetr Cvek *width = res_x[SXGA];
36757b0ad9eSPetr Cvek *height = res_y[SXGA];
36857b0ad9eSPetr Cvek }
36957b0ad9eSPetr Cvek
37057b0ad9eSPetr Cvek /* Prepare necessary register changes depending on color encoding */
ov9640_alter_regs(u32 code,struct ov9640_reg_alt * alt)37157b0ad9eSPetr Cvek static void ov9640_alter_regs(u32 code,
37257b0ad9eSPetr Cvek struct ov9640_reg_alt *alt)
37357b0ad9eSPetr Cvek {
37457b0ad9eSPetr Cvek switch (code) {
37557b0ad9eSPetr Cvek default:
37657b0ad9eSPetr Cvek case MEDIA_BUS_FMT_UYVY8_2X8:
37757b0ad9eSPetr Cvek alt->com12 = OV9640_COM12_YUV_AVG;
37857b0ad9eSPetr Cvek alt->com13 = OV9640_COM13_Y_DELAY_EN |
37957b0ad9eSPetr Cvek OV9640_COM13_YUV_DLY(0x01);
38057b0ad9eSPetr Cvek break;
38157b0ad9eSPetr Cvek case MEDIA_BUS_FMT_RGB555_2X8_PADHI_LE:
38257b0ad9eSPetr Cvek alt->com7 = OV9640_COM7_RGB;
38357b0ad9eSPetr Cvek alt->com13 = OV9640_COM13_RGB_AVG;
38457b0ad9eSPetr Cvek alt->com15 = OV9640_COM15_RGB_555;
38557b0ad9eSPetr Cvek break;
38657b0ad9eSPetr Cvek case MEDIA_BUS_FMT_RGB565_2X8_LE:
38757b0ad9eSPetr Cvek alt->com7 = OV9640_COM7_RGB;
38857b0ad9eSPetr Cvek alt->com13 = OV9640_COM13_RGB_AVG;
38957b0ad9eSPetr Cvek alt->com15 = OV9640_COM15_RGB_565;
39057b0ad9eSPetr Cvek break;
39157b0ad9eSPetr Cvek }
39257b0ad9eSPetr Cvek }
39357b0ad9eSPetr Cvek
39457b0ad9eSPetr Cvek /* Setup registers according to resolution and color encoding */
ov9640_write_regs(struct i2c_client * client,u32 width,u32 code,struct ov9640_reg_alt * alts)39557b0ad9eSPetr Cvek static int ov9640_write_regs(struct i2c_client *client, u32 width,
39657b0ad9eSPetr Cvek u32 code, struct ov9640_reg_alt *alts)
39757b0ad9eSPetr Cvek {
39857b0ad9eSPetr Cvek const struct ov9640_reg *ov9640_regs, *matrix_regs;
39974d01576SPetr Cvek unsigned int ov9640_regs_len, matrix_regs_len;
40074d01576SPetr Cvek unsigned int i;
40174d01576SPetr Cvek int ret;
40257b0ad9eSPetr Cvek u8 val;
40357b0ad9eSPetr Cvek
40457b0ad9eSPetr Cvek /* select register configuration for given resolution */
40557b0ad9eSPetr Cvek switch (width) {
40657b0ad9eSPetr Cvek case W_QQCIF:
40757b0ad9eSPetr Cvek ov9640_regs = ov9640_regs_qqcif;
40857b0ad9eSPetr Cvek ov9640_regs_len = ARRAY_SIZE(ov9640_regs_qqcif);
40957b0ad9eSPetr Cvek break;
41057b0ad9eSPetr Cvek case W_QQVGA:
41157b0ad9eSPetr Cvek ov9640_regs = ov9640_regs_qqvga;
41257b0ad9eSPetr Cvek ov9640_regs_len = ARRAY_SIZE(ov9640_regs_qqvga);
41357b0ad9eSPetr Cvek break;
41457b0ad9eSPetr Cvek case W_QCIF:
41557b0ad9eSPetr Cvek ov9640_regs = ov9640_regs_qcif;
41657b0ad9eSPetr Cvek ov9640_regs_len = ARRAY_SIZE(ov9640_regs_qcif);
41757b0ad9eSPetr Cvek break;
41857b0ad9eSPetr Cvek case W_QVGA:
41957b0ad9eSPetr Cvek ov9640_regs = ov9640_regs_qvga;
42057b0ad9eSPetr Cvek ov9640_regs_len = ARRAY_SIZE(ov9640_regs_qvga);
42157b0ad9eSPetr Cvek break;
42257b0ad9eSPetr Cvek case W_CIF:
42357b0ad9eSPetr Cvek ov9640_regs = ov9640_regs_cif;
42457b0ad9eSPetr Cvek ov9640_regs_len = ARRAY_SIZE(ov9640_regs_cif);
42557b0ad9eSPetr Cvek break;
42657b0ad9eSPetr Cvek case W_VGA:
42757b0ad9eSPetr Cvek ov9640_regs = ov9640_regs_vga;
42857b0ad9eSPetr Cvek ov9640_regs_len = ARRAY_SIZE(ov9640_regs_vga);
42957b0ad9eSPetr Cvek break;
43057b0ad9eSPetr Cvek case W_SXGA:
43157b0ad9eSPetr Cvek ov9640_regs = ov9640_regs_sxga;
43257b0ad9eSPetr Cvek ov9640_regs_len = ARRAY_SIZE(ov9640_regs_sxga);
43357b0ad9eSPetr Cvek break;
43457b0ad9eSPetr Cvek default:
43557b0ad9eSPetr Cvek dev_err(&client->dev, "Failed to select resolution!\n");
43657b0ad9eSPetr Cvek return -EINVAL;
43757b0ad9eSPetr Cvek }
43857b0ad9eSPetr Cvek
43957b0ad9eSPetr Cvek /* select color matrix configuration for given color encoding */
44057b0ad9eSPetr Cvek if (code == MEDIA_BUS_FMT_UYVY8_2X8) {
44157b0ad9eSPetr Cvek matrix_regs = ov9640_regs_yuv;
44257b0ad9eSPetr Cvek matrix_regs_len = ARRAY_SIZE(ov9640_regs_yuv);
44357b0ad9eSPetr Cvek } else {
44457b0ad9eSPetr Cvek matrix_regs = ov9640_regs_rgb;
44557b0ad9eSPetr Cvek matrix_regs_len = ARRAY_SIZE(ov9640_regs_rgb);
44657b0ad9eSPetr Cvek }
44757b0ad9eSPetr Cvek
44857b0ad9eSPetr Cvek /* write register settings into the module */
44957b0ad9eSPetr Cvek for (i = 0; i < ov9640_regs_len; i++) {
45057b0ad9eSPetr Cvek val = ov9640_regs[i].val;
45157b0ad9eSPetr Cvek
45257b0ad9eSPetr Cvek switch (ov9640_regs[i].reg) {
45357b0ad9eSPetr Cvek case OV9640_COM7:
45457b0ad9eSPetr Cvek val |= alts->com7;
45557b0ad9eSPetr Cvek break;
45657b0ad9eSPetr Cvek case OV9640_COM12:
45757b0ad9eSPetr Cvek val |= alts->com12;
45857b0ad9eSPetr Cvek break;
45957b0ad9eSPetr Cvek case OV9640_COM13:
46057b0ad9eSPetr Cvek val |= alts->com13;
46157b0ad9eSPetr Cvek break;
46257b0ad9eSPetr Cvek case OV9640_COM15:
46357b0ad9eSPetr Cvek val |= alts->com15;
46457b0ad9eSPetr Cvek break;
46557b0ad9eSPetr Cvek }
46657b0ad9eSPetr Cvek
46757b0ad9eSPetr Cvek ret = ov9640_reg_write(client, ov9640_regs[i].reg, val);
46857b0ad9eSPetr Cvek if (ret)
46957b0ad9eSPetr Cvek return ret;
47057b0ad9eSPetr Cvek }
47157b0ad9eSPetr Cvek
47257b0ad9eSPetr Cvek /* write color matrix configuration into the module */
47357b0ad9eSPetr Cvek for (i = 0; i < matrix_regs_len; i++) {
47457b0ad9eSPetr Cvek ret = ov9640_reg_write(client, matrix_regs[i].reg,
47557b0ad9eSPetr Cvek matrix_regs[i].val);
47657b0ad9eSPetr Cvek if (ret)
47757b0ad9eSPetr Cvek return ret;
47857b0ad9eSPetr Cvek }
47957b0ad9eSPetr Cvek
48057b0ad9eSPetr Cvek return 0;
48157b0ad9eSPetr Cvek }
48257b0ad9eSPetr Cvek
48357b0ad9eSPetr Cvek /* program default register values */
ov9640_prog_dflt(struct i2c_client * client)48457b0ad9eSPetr Cvek static int ov9640_prog_dflt(struct i2c_client *client)
48557b0ad9eSPetr Cvek {
48674d01576SPetr Cvek unsigned int i;
48774d01576SPetr Cvek int ret;
48857b0ad9eSPetr Cvek
48957b0ad9eSPetr Cvek for (i = 0; i < ARRAY_SIZE(ov9640_regs_dflt); i++) {
49057b0ad9eSPetr Cvek ret = ov9640_reg_write(client, ov9640_regs_dflt[i].reg,
49157b0ad9eSPetr Cvek ov9640_regs_dflt[i].val);
49257b0ad9eSPetr Cvek if (ret)
49357b0ad9eSPetr Cvek return ret;
49457b0ad9eSPetr Cvek }
49557b0ad9eSPetr Cvek
49657b0ad9eSPetr Cvek /* wait for the changes to actually happen, 140ms are not enough yet */
4979f7e55d2SPetr Cvek msleep(150);
49857b0ad9eSPetr Cvek
49957b0ad9eSPetr Cvek return 0;
50057b0ad9eSPetr Cvek }
50157b0ad9eSPetr Cvek
50257b0ad9eSPetr Cvek /* set the format we will capture in */
ov9640_s_fmt(struct v4l2_subdev * sd,struct v4l2_mbus_framefmt * mf)50357b0ad9eSPetr Cvek static int ov9640_s_fmt(struct v4l2_subdev *sd,
50457b0ad9eSPetr Cvek struct v4l2_mbus_framefmt *mf)
50557b0ad9eSPetr Cvek {
50657b0ad9eSPetr Cvek struct i2c_client *client = v4l2_get_subdevdata(sd);
50757b0ad9eSPetr Cvek struct ov9640_reg_alt alts = {0};
50857b0ad9eSPetr Cvek int ret;
50957b0ad9eSPetr Cvek
51057b0ad9eSPetr Cvek ov9640_alter_regs(mf->code, &alts);
51157b0ad9eSPetr Cvek
51257b0ad9eSPetr Cvek ov9640_reset(client);
51357b0ad9eSPetr Cvek
51457b0ad9eSPetr Cvek ret = ov9640_prog_dflt(client);
51557b0ad9eSPetr Cvek if (ret)
51657b0ad9eSPetr Cvek return ret;
51757b0ad9eSPetr Cvek
51857b0ad9eSPetr Cvek return ov9640_write_regs(client, mf->width, mf->code, &alts);
51957b0ad9eSPetr Cvek }
52057b0ad9eSPetr Cvek
ov9640_set_fmt(struct v4l2_subdev * sd,struct v4l2_subdev_state * sd_state,struct v4l2_subdev_format * format)52157b0ad9eSPetr Cvek static int ov9640_set_fmt(struct v4l2_subdev *sd,
5220d346d2aSTomi Valkeinen struct v4l2_subdev_state *sd_state,
52357b0ad9eSPetr Cvek struct v4l2_subdev_format *format)
52457b0ad9eSPetr Cvek {
52557b0ad9eSPetr Cvek struct v4l2_mbus_framefmt *mf = &format->format;
52657b0ad9eSPetr Cvek
52757b0ad9eSPetr Cvek if (format->pad)
52857b0ad9eSPetr Cvek return -EINVAL;
52957b0ad9eSPetr Cvek
53057b0ad9eSPetr Cvek ov9640_res_roundup(&mf->width, &mf->height);
53157b0ad9eSPetr Cvek
53257b0ad9eSPetr Cvek mf->field = V4L2_FIELD_NONE;
53357b0ad9eSPetr Cvek
53457b0ad9eSPetr Cvek switch (mf->code) {
53557b0ad9eSPetr Cvek case MEDIA_BUS_FMT_RGB555_2X8_PADHI_LE:
53657b0ad9eSPetr Cvek case MEDIA_BUS_FMT_RGB565_2X8_LE:
53757b0ad9eSPetr Cvek mf->colorspace = V4L2_COLORSPACE_SRGB;
53857b0ad9eSPetr Cvek break;
53957b0ad9eSPetr Cvek default:
54057b0ad9eSPetr Cvek mf->code = MEDIA_BUS_FMT_UYVY8_2X8;
5411771e9fbSGustavo A. R. Silva fallthrough;
54257b0ad9eSPetr Cvek case MEDIA_BUS_FMT_UYVY8_2X8:
54357b0ad9eSPetr Cvek mf->colorspace = V4L2_COLORSPACE_JPEG;
54457b0ad9eSPetr Cvek break;
54557b0ad9eSPetr Cvek }
54657b0ad9eSPetr Cvek
54757b0ad9eSPetr Cvek if (format->which == V4L2_SUBDEV_FORMAT_ACTIVE)
54857b0ad9eSPetr Cvek return ov9640_s_fmt(sd, mf);
54957b0ad9eSPetr Cvek
5500d346d2aSTomi Valkeinen sd_state->pads->try_fmt = *mf;
551856b0b8fSPetr Cvek
55257b0ad9eSPetr Cvek return 0;
55357b0ad9eSPetr Cvek }
55457b0ad9eSPetr Cvek
ov9640_enum_mbus_code(struct v4l2_subdev * sd,struct v4l2_subdev_state * sd_state,struct v4l2_subdev_mbus_code_enum * code)55557b0ad9eSPetr Cvek static int ov9640_enum_mbus_code(struct v4l2_subdev *sd,
5560d346d2aSTomi Valkeinen struct v4l2_subdev_state *sd_state,
55757b0ad9eSPetr Cvek struct v4l2_subdev_mbus_code_enum *code)
55857b0ad9eSPetr Cvek {
55957b0ad9eSPetr Cvek if (code->pad || code->index >= ARRAY_SIZE(ov9640_codes))
56057b0ad9eSPetr Cvek return -EINVAL;
56157b0ad9eSPetr Cvek
56257b0ad9eSPetr Cvek code->code = ov9640_codes[code->index];
563856b0b8fSPetr Cvek
56457b0ad9eSPetr Cvek return 0;
56557b0ad9eSPetr Cvek }
56657b0ad9eSPetr Cvek
ov9640_get_selection(struct v4l2_subdev * sd,struct v4l2_subdev_state * sd_state,struct v4l2_subdev_selection * sel)56757b0ad9eSPetr Cvek static int ov9640_get_selection(struct v4l2_subdev *sd,
5680d346d2aSTomi Valkeinen struct v4l2_subdev_state *sd_state,
56957b0ad9eSPetr Cvek struct v4l2_subdev_selection *sel)
57057b0ad9eSPetr Cvek {
57157b0ad9eSPetr Cvek if (sel->which != V4L2_SUBDEV_FORMAT_ACTIVE)
57257b0ad9eSPetr Cvek return -EINVAL;
57357b0ad9eSPetr Cvek
57457b0ad9eSPetr Cvek sel->r.left = 0;
57557b0ad9eSPetr Cvek sel->r.top = 0;
57657b0ad9eSPetr Cvek switch (sel->target) {
57757b0ad9eSPetr Cvek case V4L2_SEL_TGT_CROP_BOUNDS:
57857b0ad9eSPetr Cvek case V4L2_SEL_TGT_CROP:
57957b0ad9eSPetr Cvek sel->r.width = W_SXGA;
58057b0ad9eSPetr Cvek sel->r.height = H_SXGA;
58157b0ad9eSPetr Cvek return 0;
58257b0ad9eSPetr Cvek default:
58357b0ad9eSPetr Cvek return -EINVAL;
58457b0ad9eSPetr Cvek }
58557b0ad9eSPetr Cvek }
58657b0ad9eSPetr Cvek
ov9640_video_probe(struct i2c_client * client)58757b0ad9eSPetr Cvek static int ov9640_video_probe(struct i2c_client *client)
58857b0ad9eSPetr Cvek {
58957b0ad9eSPetr Cvek struct v4l2_subdev *sd = i2c_get_clientdata(client);
59057b0ad9eSPetr Cvek struct ov9640_priv *priv = to_ov9640_sensor(sd);
59157b0ad9eSPetr Cvek u8 pid, ver, midh, midl;
59257b0ad9eSPetr Cvek const char *devname;
59357b0ad9eSPetr Cvek int ret;
59457b0ad9eSPetr Cvek
59557b0ad9eSPetr Cvek ret = ov9640_s_power(&priv->subdev, 1);
59657b0ad9eSPetr Cvek if (ret < 0)
59757b0ad9eSPetr Cvek return ret;
59857b0ad9eSPetr Cvek
59957b0ad9eSPetr Cvek /*
60057b0ad9eSPetr Cvek * check and show product ID and manufacturer ID
60157b0ad9eSPetr Cvek */
60257b0ad9eSPetr Cvek
60357b0ad9eSPetr Cvek ret = ov9640_reg_read(client, OV9640_PID, &pid);
60457b0ad9eSPetr Cvek if (!ret)
60557b0ad9eSPetr Cvek ret = ov9640_reg_read(client, OV9640_VER, &ver);
60657b0ad9eSPetr Cvek if (!ret)
60757b0ad9eSPetr Cvek ret = ov9640_reg_read(client, OV9640_MIDH, &midh);
60857b0ad9eSPetr Cvek if (!ret)
60957b0ad9eSPetr Cvek ret = ov9640_reg_read(client, OV9640_MIDL, &midl);
61057b0ad9eSPetr Cvek if (ret)
61157b0ad9eSPetr Cvek goto done;
61257b0ad9eSPetr Cvek
61357b0ad9eSPetr Cvek switch (VERSION(pid, ver)) {
61457b0ad9eSPetr Cvek case OV9640_V2:
61557b0ad9eSPetr Cvek devname = "ov9640";
61657b0ad9eSPetr Cvek priv->revision = 2;
61757b0ad9eSPetr Cvek break;
61857b0ad9eSPetr Cvek case OV9640_V3:
61957b0ad9eSPetr Cvek devname = "ov9640";
62057b0ad9eSPetr Cvek priv->revision = 3;
62157b0ad9eSPetr Cvek break;
62257b0ad9eSPetr Cvek default:
62357b0ad9eSPetr Cvek dev_err(&client->dev, "Product ID error %x:%x\n", pid, ver);
62457b0ad9eSPetr Cvek ret = -ENODEV;
62557b0ad9eSPetr Cvek goto done;
62657b0ad9eSPetr Cvek }
62757b0ad9eSPetr Cvek
62857b0ad9eSPetr Cvek dev_info(&client->dev, "%s Product ID %0x:%0x Manufacturer ID %x:%x\n",
62957b0ad9eSPetr Cvek devname, pid, ver, midh, midl);
63057b0ad9eSPetr Cvek
63157b0ad9eSPetr Cvek ret = v4l2_ctrl_handler_setup(&priv->hdl);
63257b0ad9eSPetr Cvek
63357b0ad9eSPetr Cvek done:
63457b0ad9eSPetr Cvek ov9640_s_power(&priv->subdev, 0);
63557b0ad9eSPetr Cvek return ret;
63657b0ad9eSPetr Cvek }
63757b0ad9eSPetr Cvek
63857b0ad9eSPetr Cvek static const struct v4l2_ctrl_ops ov9640_ctrl_ops = {
63957b0ad9eSPetr Cvek .s_ctrl = ov9640_s_ctrl,
64057b0ad9eSPetr Cvek };
64157b0ad9eSPetr Cvek
64257b0ad9eSPetr Cvek static const struct v4l2_subdev_core_ops ov9640_core_ops = {
64357b0ad9eSPetr Cvek #ifdef CONFIG_VIDEO_ADV_DEBUG
64457b0ad9eSPetr Cvek .g_register = ov9640_get_register,
64557b0ad9eSPetr Cvek .s_register = ov9640_set_register,
64657b0ad9eSPetr Cvek #endif
64757b0ad9eSPetr Cvek .s_power = ov9640_s_power,
64857b0ad9eSPetr Cvek };
64957b0ad9eSPetr Cvek
65057b0ad9eSPetr Cvek /* Request bus settings on camera side */
ov9640_get_mbus_config(struct v4l2_subdev * sd,unsigned int pad,struct v4l2_mbus_config * cfg)6510c3da525SJacopo Mondi static int ov9640_get_mbus_config(struct v4l2_subdev *sd,
6520c3da525SJacopo Mondi unsigned int pad,
65357b0ad9eSPetr Cvek struct v4l2_mbus_config *cfg)
65457b0ad9eSPetr Cvek {
65557b0ad9eSPetr Cvek cfg->type = V4L2_MBUS_PARALLEL;
6566a7bdd89SLaurent Pinchart cfg->bus.parallel.flags = V4L2_MBUS_PCLK_SAMPLE_RISING |
6576a7bdd89SLaurent Pinchart V4L2_MBUS_MASTER |
6586a7bdd89SLaurent Pinchart V4L2_MBUS_VSYNC_ACTIVE_HIGH |
6596a7bdd89SLaurent Pinchart V4L2_MBUS_HSYNC_ACTIVE_HIGH |
6606a7bdd89SLaurent Pinchart V4L2_MBUS_DATA_ACTIVE_HIGH;
66157b0ad9eSPetr Cvek
66257b0ad9eSPetr Cvek return 0;
66357b0ad9eSPetr Cvek }
66457b0ad9eSPetr Cvek
66557b0ad9eSPetr Cvek static const struct v4l2_subdev_video_ops ov9640_video_ops = {
66657b0ad9eSPetr Cvek .s_stream = ov9640_s_stream,
66757b0ad9eSPetr Cvek };
66857b0ad9eSPetr Cvek
66957b0ad9eSPetr Cvek static const struct v4l2_subdev_pad_ops ov9640_pad_ops = {
67057b0ad9eSPetr Cvek .enum_mbus_code = ov9640_enum_mbus_code,
67157b0ad9eSPetr Cvek .get_selection = ov9640_get_selection,
67257b0ad9eSPetr Cvek .set_fmt = ov9640_set_fmt,
6730c3da525SJacopo Mondi .get_mbus_config = ov9640_get_mbus_config,
67457b0ad9eSPetr Cvek };
67557b0ad9eSPetr Cvek
67657b0ad9eSPetr Cvek static const struct v4l2_subdev_ops ov9640_subdev_ops = {
67757b0ad9eSPetr Cvek .core = &ov9640_core_ops,
67857b0ad9eSPetr Cvek .video = &ov9640_video_ops,
67957b0ad9eSPetr Cvek .pad = &ov9640_pad_ops,
68057b0ad9eSPetr Cvek };
68157b0ad9eSPetr Cvek
68257b0ad9eSPetr Cvek /*
68357b0ad9eSPetr Cvek * i2c_driver function
68457b0ad9eSPetr Cvek */
ov9640_probe(struct i2c_client * client)6855555116eSUwe Kleine-König static int ov9640_probe(struct i2c_client *client)
68657b0ad9eSPetr Cvek {
68757b0ad9eSPetr Cvek struct ov9640_priv *priv;
68857b0ad9eSPetr Cvek int ret;
68957b0ad9eSPetr Cvek
69016aaf112SSakari Ailus priv = devm_kzalloc(&client->dev, sizeof(*priv), GFP_KERNEL);
69157b0ad9eSPetr Cvek if (!priv)
69257b0ad9eSPetr Cvek return -ENOMEM;
69357b0ad9eSPetr Cvek
6949f7e55d2SPetr Cvek priv->gpio_power = devm_gpiod_get(&client->dev, "Camera power",
6959f7e55d2SPetr Cvek GPIOD_OUT_LOW);
696e14b77c3SSakari Ailus if (IS_ERR(priv->gpio_power)) {
6979f7e55d2SPetr Cvek ret = PTR_ERR(priv->gpio_power);
6989f7e55d2SPetr Cvek return ret;
6999f7e55d2SPetr Cvek }
7009f7e55d2SPetr Cvek
7019f7e55d2SPetr Cvek priv->gpio_reset = devm_gpiod_get(&client->dev, "Camera reset",
7029f7e55d2SPetr Cvek GPIOD_OUT_HIGH);
703e14b77c3SSakari Ailus if (IS_ERR(priv->gpio_reset)) {
7049f7e55d2SPetr Cvek ret = PTR_ERR(priv->gpio_reset);
7059f7e55d2SPetr Cvek return ret;
7069f7e55d2SPetr Cvek }
7079f7e55d2SPetr Cvek
70857b0ad9eSPetr Cvek v4l2_i2c_subdev_init(&priv->subdev, client, &ov9640_subdev_ops);
70957b0ad9eSPetr Cvek
71057b0ad9eSPetr Cvek v4l2_ctrl_handler_init(&priv->hdl, 2);
71157b0ad9eSPetr Cvek v4l2_ctrl_new_std(&priv->hdl, &ov9640_ctrl_ops,
71257b0ad9eSPetr Cvek V4L2_CID_VFLIP, 0, 1, 1, 0);
71357b0ad9eSPetr Cvek v4l2_ctrl_new_std(&priv->hdl, &ov9640_ctrl_ops,
71457b0ad9eSPetr Cvek V4L2_CID_HFLIP, 0, 1, 1, 0);
715ffe305d2SPetr Cvek
716ffe305d2SPetr Cvek if (priv->hdl.error) {
717ffe305d2SPetr Cvek ret = priv->hdl.error;
718ffe305d2SPetr Cvek goto ectrlinit;
719ffe305d2SPetr Cvek }
720ffe305d2SPetr Cvek
72157b0ad9eSPetr Cvek priv->subdev.ctrl_handler = &priv->hdl;
72257b0ad9eSPetr Cvek
7238de14b3aSEzequiel Garcia priv->clk = devm_clk_get(&client->dev, "mclk");
72457b0ad9eSPetr Cvek if (IS_ERR(priv->clk)) {
72557b0ad9eSPetr Cvek ret = PTR_ERR(priv->clk);
726ffe305d2SPetr Cvek goto ectrlinit;
72757b0ad9eSPetr Cvek }
72857b0ad9eSPetr Cvek
72957b0ad9eSPetr Cvek ret = ov9640_video_probe(client);
7309f7e55d2SPetr Cvek if (ret)
7318de14b3aSEzequiel Garcia goto ectrlinit;
7329f7e55d2SPetr Cvek
7339f7e55d2SPetr Cvek priv->subdev.dev = &client->dev;
7349f7e55d2SPetr Cvek ret = v4l2_async_register_subdev(&priv->subdev);
7359f7e55d2SPetr Cvek if (ret)
7368de14b3aSEzequiel Garcia goto ectrlinit;
7379f7e55d2SPetr Cvek
7389f7e55d2SPetr Cvek return 0;
7399f7e55d2SPetr Cvek
740ffe305d2SPetr Cvek ectrlinit:
74157b0ad9eSPetr Cvek v4l2_ctrl_handler_free(&priv->hdl);
742856b0b8fSPetr Cvek
74357b0ad9eSPetr Cvek return ret;
74457b0ad9eSPetr Cvek }
74557b0ad9eSPetr Cvek
ov9640_remove(struct i2c_client * client)746ed5c2f5fSUwe Kleine-König static void ov9640_remove(struct i2c_client *client)
74757b0ad9eSPetr Cvek {
74857b0ad9eSPetr Cvek struct v4l2_subdev *sd = i2c_get_clientdata(client);
74957b0ad9eSPetr Cvek struct ov9640_priv *priv = to_ov9640_sensor(sd);
75057b0ad9eSPetr Cvek
7519f7e55d2SPetr Cvek v4l2_async_unregister_subdev(&priv->subdev);
75257b0ad9eSPetr Cvek v4l2_ctrl_handler_free(&priv->hdl);
75357b0ad9eSPetr Cvek }
75457b0ad9eSPetr Cvek
75557b0ad9eSPetr Cvek static const struct i2c_device_id ov9640_id[] = {
75657b0ad9eSPetr Cvek { "ov9640", 0 },
75757b0ad9eSPetr Cvek { }
75857b0ad9eSPetr Cvek };
75957b0ad9eSPetr Cvek MODULE_DEVICE_TABLE(i2c, ov9640_id);
76057b0ad9eSPetr Cvek
76157b0ad9eSPetr Cvek static struct i2c_driver ov9640_i2c_driver = {
76257b0ad9eSPetr Cvek .driver = {
76357b0ad9eSPetr Cvek .name = "ov9640",
76457b0ad9eSPetr Cvek },
765*aaeb31c0SUwe Kleine-König .probe = ov9640_probe,
76657b0ad9eSPetr Cvek .remove = ov9640_remove,
76757b0ad9eSPetr Cvek .id_table = ov9640_id,
76857b0ad9eSPetr Cvek };
76957b0ad9eSPetr Cvek
77057b0ad9eSPetr Cvek module_i2c_driver(ov9640_i2c_driver);
77157b0ad9eSPetr Cvek
772e1db811eSHans Verkuil MODULE_DESCRIPTION("OmniVision OV96xx CMOS Image Sensor driver");
77357b0ad9eSPetr Cvek MODULE_AUTHOR("Marek Vasut <marek.vasut@gmail.com>");
77457b0ad9eSPetr Cvek MODULE_LICENSE("GPL v2");
775