157b0ad9eSPetr Cvek /* 257b0ad9eSPetr Cvek * OmniVision OV96xx Camera Driver 357b0ad9eSPetr Cvek * 457b0ad9eSPetr Cvek * Copyright (C) 2009 Marek Vasut <marek.vasut@gmail.com> 557b0ad9eSPetr Cvek * 657b0ad9eSPetr Cvek * Based on ov772x camera driver: 757b0ad9eSPetr Cvek * 857b0ad9eSPetr Cvek * Copyright (C) 2008 Renesas Solutions Corp. 957b0ad9eSPetr Cvek * Kuninori Morimoto <morimoto.kuninori@renesas.com> 1057b0ad9eSPetr Cvek * 1157b0ad9eSPetr Cvek * Based on ov7670 and soc_camera_platform driver, 12*9f7e55d2SPetr Cvek * transition from soc_camera to pxa_camera based on mt9m111 1357b0ad9eSPetr Cvek * 1457b0ad9eSPetr Cvek * Copyright 2006-7 Jonathan Corbet <corbet@lwn.net> 1557b0ad9eSPetr Cvek * Copyright (C) 2008 Magnus Damm 1657b0ad9eSPetr Cvek * Copyright (C) 2008, Guennadi Liakhovetski <kernel@pengutronix.de> 1757b0ad9eSPetr Cvek * 1857b0ad9eSPetr Cvek * This program is free software; you can redistribute it and/or modify 1957b0ad9eSPetr Cvek * it under the terms of the GNU General Public License version 2 as 2057b0ad9eSPetr Cvek * published by the Free Software Foundation. 2157b0ad9eSPetr Cvek */ 2257b0ad9eSPetr Cvek 2357b0ad9eSPetr Cvek #include <linux/init.h> 2457b0ad9eSPetr Cvek #include <linux/module.h> 2557b0ad9eSPetr Cvek #include <linux/i2c.h> 2657b0ad9eSPetr Cvek #include <linux/slab.h> 2757b0ad9eSPetr Cvek #include <linux/delay.h> 2857b0ad9eSPetr Cvek #include <linux/v4l2-mediabus.h> 2957b0ad9eSPetr Cvek #include <linux/videodev2.h> 3057b0ad9eSPetr Cvek 31*9f7e55d2SPetr Cvek #include <media/v4l2-async.h> 3257b0ad9eSPetr Cvek #include <media/v4l2-clk.h> 3357b0ad9eSPetr Cvek #include <media/v4l2-common.h> 3457b0ad9eSPetr Cvek #include <media/v4l2-ctrls.h> 35*9f7e55d2SPetr Cvek #include <media/v4l2-device.h> 36*9f7e55d2SPetr Cvek #include <media/v4l2-event.h> 37*9f7e55d2SPetr Cvek 38*9f7e55d2SPetr Cvek #include <linux/gpio/consumer.h> 3957b0ad9eSPetr Cvek 4057b0ad9eSPetr Cvek #include "ov9640.h" 4157b0ad9eSPetr Cvek 4257b0ad9eSPetr Cvek #define to_ov9640_sensor(sd) container_of(sd, struct ov9640_priv, subdev) 4357b0ad9eSPetr Cvek 4457b0ad9eSPetr Cvek /* default register setup */ 4557b0ad9eSPetr Cvek static const struct ov9640_reg ov9640_regs_dflt[] = { 4657b0ad9eSPetr Cvek { OV9640_COM5, OV9640_COM5_SYSCLK | OV9640_COM5_LONGEXP }, 4757b0ad9eSPetr Cvek { OV9640_COM6, OV9640_COM6_OPT_BLC | OV9640_COM6_ADBLC_BIAS | 4857b0ad9eSPetr Cvek OV9640_COM6_FMT_RST | OV9640_COM6_ADBLC_OPTEN }, 4957b0ad9eSPetr Cvek { OV9640_PSHFT, OV9640_PSHFT_VAL(0x01) }, 5057b0ad9eSPetr Cvek { OV9640_ACOM, OV9640_ACOM_2X_ANALOG | OV9640_ACOM_RSVD }, 5157b0ad9eSPetr Cvek { OV9640_TSLB, OV9640_TSLB_YUYV_UYVY }, 5257b0ad9eSPetr Cvek { OV9640_COM16, OV9640_COM16_RB_AVG }, 5357b0ad9eSPetr Cvek 5457b0ad9eSPetr Cvek /* Gamma curve P */ 5557b0ad9eSPetr Cvek { 0x6c, 0x40 }, { 0x6d, 0x30 }, { 0x6e, 0x4b }, { 0x6f, 0x60 }, 5657b0ad9eSPetr Cvek { 0x70, 0x70 }, { 0x71, 0x70 }, { 0x72, 0x70 }, { 0x73, 0x70 }, 5757b0ad9eSPetr Cvek { 0x74, 0x60 }, { 0x75, 0x60 }, { 0x76, 0x50 }, { 0x77, 0x48 }, 5857b0ad9eSPetr Cvek { 0x78, 0x3a }, { 0x79, 0x2e }, { 0x7a, 0x28 }, { 0x7b, 0x22 }, 5957b0ad9eSPetr Cvek 6057b0ad9eSPetr Cvek /* Gamma curve T */ 6157b0ad9eSPetr Cvek { 0x7c, 0x04 }, { 0x7d, 0x07 }, { 0x7e, 0x10 }, { 0x7f, 0x28 }, 6257b0ad9eSPetr Cvek { 0x80, 0x36 }, { 0x81, 0x44 }, { 0x82, 0x52 }, { 0x83, 0x60 }, 6357b0ad9eSPetr Cvek { 0x84, 0x6c }, { 0x85, 0x78 }, { 0x86, 0x8c }, { 0x87, 0x9e }, 6457b0ad9eSPetr Cvek { 0x88, 0xbb }, { 0x89, 0xd2 }, { 0x8a, 0xe6 }, 6557b0ad9eSPetr Cvek }; 6657b0ad9eSPetr Cvek 6757b0ad9eSPetr Cvek /* Configurations 6857b0ad9eSPetr Cvek * NOTE: for YUV, alter the following registers: 6957b0ad9eSPetr Cvek * COM12 |= OV9640_COM12_YUV_AVG 7057b0ad9eSPetr Cvek * 7157b0ad9eSPetr Cvek * for RGB, alter the following registers: 7257b0ad9eSPetr Cvek * COM7 |= OV9640_COM7_RGB 7357b0ad9eSPetr Cvek * COM13 |= OV9640_COM13_RGB_AVG 7457b0ad9eSPetr Cvek * COM15 |= proper RGB color encoding mode 7557b0ad9eSPetr Cvek */ 7657b0ad9eSPetr Cvek static const struct ov9640_reg ov9640_regs_qqcif[] = { 7757b0ad9eSPetr Cvek { OV9640_CLKRC, OV9640_CLKRC_DPLL_EN | OV9640_CLKRC_DIV(0x0f) }, 7857b0ad9eSPetr Cvek { OV9640_COM1, OV9640_COM1_QQFMT | OV9640_COM1_HREF_2SKIP }, 7957b0ad9eSPetr Cvek { OV9640_COM4, OV9640_COM4_QQ_VP | OV9640_COM4_RSVD }, 8057b0ad9eSPetr Cvek { OV9640_COM7, OV9640_COM7_QCIF }, 8157b0ad9eSPetr Cvek { OV9640_COM12, OV9640_COM12_RSVD }, 8257b0ad9eSPetr Cvek { OV9640_COM13, OV9640_COM13_GAMMA_RAW | OV9640_COM13_MATRIX_EN }, 8357b0ad9eSPetr Cvek { OV9640_COM15, OV9640_COM15_OR_10F0 }, 8457b0ad9eSPetr Cvek }; 8557b0ad9eSPetr Cvek 8657b0ad9eSPetr Cvek static const struct ov9640_reg ov9640_regs_qqvga[] = { 8757b0ad9eSPetr Cvek { OV9640_CLKRC, OV9640_CLKRC_DPLL_EN | OV9640_CLKRC_DIV(0x07) }, 8857b0ad9eSPetr Cvek { OV9640_COM1, OV9640_COM1_QQFMT | OV9640_COM1_HREF_2SKIP }, 8957b0ad9eSPetr Cvek { OV9640_COM4, OV9640_COM4_QQ_VP | OV9640_COM4_RSVD }, 9057b0ad9eSPetr Cvek { OV9640_COM7, OV9640_COM7_QVGA }, 9157b0ad9eSPetr Cvek { OV9640_COM12, OV9640_COM12_RSVD }, 9257b0ad9eSPetr Cvek { OV9640_COM13, OV9640_COM13_GAMMA_RAW | OV9640_COM13_MATRIX_EN }, 9357b0ad9eSPetr Cvek { OV9640_COM15, OV9640_COM15_OR_10F0 }, 9457b0ad9eSPetr Cvek }; 9557b0ad9eSPetr Cvek 9657b0ad9eSPetr Cvek static const struct ov9640_reg ov9640_regs_qcif[] = { 9757b0ad9eSPetr Cvek { OV9640_CLKRC, OV9640_CLKRC_DPLL_EN | OV9640_CLKRC_DIV(0x07) }, 9857b0ad9eSPetr Cvek { OV9640_COM4, OV9640_COM4_QQ_VP | OV9640_COM4_RSVD }, 9957b0ad9eSPetr Cvek { OV9640_COM7, OV9640_COM7_QCIF }, 10057b0ad9eSPetr Cvek { OV9640_COM12, OV9640_COM12_RSVD }, 10157b0ad9eSPetr Cvek { OV9640_COM13, OV9640_COM13_GAMMA_RAW | OV9640_COM13_MATRIX_EN }, 10257b0ad9eSPetr Cvek { OV9640_COM15, OV9640_COM15_OR_10F0 }, 10357b0ad9eSPetr Cvek }; 10457b0ad9eSPetr Cvek 10557b0ad9eSPetr Cvek static const struct ov9640_reg ov9640_regs_qvga[] = { 10657b0ad9eSPetr Cvek { OV9640_CLKRC, OV9640_CLKRC_DPLL_EN | OV9640_CLKRC_DIV(0x03) }, 10757b0ad9eSPetr Cvek { OV9640_COM4, OV9640_COM4_QQ_VP | OV9640_COM4_RSVD }, 10857b0ad9eSPetr Cvek { OV9640_COM7, OV9640_COM7_QVGA }, 10957b0ad9eSPetr Cvek { OV9640_COM12, OV9640_COM12_RSVD }, 11057b0ad9eSPetr Cvek { OV9640_COM13, OV9640_COM13_GAMMA_RAW | OV9640_COM13_MATRIX_EN }, 11157b0ad9eSPetr Cvek { OV9640_COM15, OV9640_COM15_OR_10F0 }, 11257b0ad9eSPetr Cvek }; 11357b0ad9eSPetr Cvek 11457b0ad9eSPetr Cvek static const struct ov9640_reg ov9640_regs_cif[] = { 11557b0ad9eSPetr Cvek { OV9640_CLKRC, OV9640_CLKRC_DPLL_EN | OV9640_CLKRC_DIV(0x03) }, 11657b0ad9eSPetr Cvek { OV9640_COM3, OV9640_COM3_VP }, 11757b0ad9eSPetr Cvek { OV9640_COM7, OV9640_COM7_CIF }, 11857b0ad9eSPetr Cvek { OV9640_COM12, OV9640_COM12_RSVD }, 11957b0ad9eSPetr Cvek { OV9640_COM13, OV9640_COM13_GAMMA_RAW | OV9640_COM13_MATRIX_EN }, 12057b0ad9eSPetr Cvek { OV9640_COM15, OV9640_COM15_OR_10F0 }, 12157b0ad9eSPetr Cvek }; 12257b0ad9eSPetr Cvek 12357b0ad9eSPetr Cvek static const struct ov9640_reg ov9640_regs_vga[] = { 12457b0ad9eSPetr Cvek { OV9640_CLKRC, OV9640_CLKRC_DPLL_EN | OV9640_CLKRC_DIV(0x01) }, 12557b0ad9eSPetr Cvek { OV9640_COM3, OV9640_COM3_VP }, 12657b0ad9eSPetr Cvek { OV9640_COM7, OV9640_COM7_VGA }, 12757b0ad9eSPetr Cvek { OV9640_COM12, OV9640_COM12_RSVD }, 12857b0ad9eSPetr Cvek { OV9640_COM13, OV9640_COM13_GAMMA_RAW | OV9640_COM13_MATRIX_EN }, 12957b0ad9eSPetr Cvek { OV9640_COM15, OV9640_COM15_OR_10F0 }, 13057b0ad9eSPetr Cvek }; 13157b0ad9eSPetr Cvek 13257b0ad9eSPetr Cvek static const struct ov9640_reg ov9640_regs_sxga[] = { 13357b0ad9eSPetr Cvek { OV9640_CLKRC, OV9640_CLKRC_DPLL_EN | OV9640_CLKRC_DIV(0x01) }, 13457b0ad9eSPetr Cvek { OV9640_COM3, OV9640_COM3_VP }, 13557b0ad9eSPetr Cvek { OV9640_COM7, 0 }, 13657b0ad9eSPetr Cvek { OV9640_COM12, OV9640_COM12_RSVD }, 13757b0ad9eSPetr Cvek { OV9640_COM13, OV9640_COM13_GAMMA_RAW | OV9640_COM13_MATRIX_EN }, 13857b0ad9eSPetr Cvek { OV9640_COM15, OV9640_COM15_OR_10F0 }, 13957b0ad9eSPetr Cvek }; 14057b0ad9eSPetr Cvek 14157b0ad9eSPetr Cvek static const struct ov9640_reg ov9640_regs_yuv[] = { 14257b0ad9eSPetr Cvek { OV9640_MTX1, 0x58 }, 14357b0ad9eSPetr Cvek { OV9640_MTX2, 0x48 }, 14457b0ad9eSPetr Cvek { OV9640_MTX3, 0x10 }, 14557b0ad9eSPetr Cvek { OV9640_MTX4, 0x28 }, 14657b0ad9eSPetr Cvek { OV9640_MTX5, 0x48 }, 14757b0ad9eSPetr Cvek { OV9640_MTX6, 0x70 }, 14857b0ad9eSPetr Cvek { OV9640_MTX7, 0x40 }, 14957b0ad9eSPetr Cvek { OV9640_MTX8, 0x40 }, 15057b0ad9eSPetr Cvek { OV9640_MTX9, 0x40 }, 15157b0ad9eSPetr Cvek { OV9640_MTXS, 0x0f }, 15257b0ad9eSPetr Cvek }; 15357b0ad9eSPetr Cvek 15457b0ad9eSPetr Cvek static const struct ov9640_reg ov9640_regs_rgb[] = { 15557b0ad9eSPetr Cvek { OV9640_MTX1, 0x71 }, 15657b0ad9eSPetr Cvek { OV9640_MTX2, 0x3e }, 15757b0ad9eSPetr Cvek { OV9640_MTX3, 0x0c }, 15857b0ad9eSPetr Cvek { OV9640_MTX4, 0x33 }, 15957b0ad9eSPetr Cvek { OV9640_MTX5, 0x72 }, 16057b0ad9eSPetr Cvek { OV9640_MTX6, 0x00 }, 16157b0ad9eSPetr Cvek { OV9640_MTX7, 0x2b }, 16257b0ad9eSPetr Cvek { OV9640_MTX8, 0x66 }, 16357b0ad9eSPetr Cvek { OV9640_MTX9, 0xd2 }, 16457b0ad9eSPetr Cvek { OV9640_MTXS, 0x65 }, 16557b0ad9eSPetr Cvek }; 16657b0ad9eSPetr Cvek 16757b0ad9eSPetr Cvek static u32 ov9640_codes[] = { 16857b0ad9eSPetr Cvek MEDIA_BUS_FMT_UYVY8_2X8, 16957b0ad9eSPetr Cvek MEDIA_BUS_FMT_RGB555_2X8_PADHI_LE, 17057b0ad9eSPetr Cvek MEDIA_BUS_FMT_RGB565_2X8_LE, 17157b0ad9eSPetr Cvek }; 17257b0ad9eSPetr Cvek 17357b0ad9eSPetr Cvek /* read a register */ 17457b0ad9eSPetr Cvek static int ov9640_reg_read(struct i2c_client *client, u8 reg, u8 *val) 17557b0ad9eSPetr Cvek { 17657b0ad9eSPetr Cvek int ret; 17757b0ad9eSPetr Cvek u8 data = reg; 17857b0ad9eSPetr Cvek struct i2c_msg msg = { 17957b0ad9eSPetr Cvek .addr = client->addr, 18057b0ad9eSPetr Cvek .flags = 0, 18157b0ad9eSPetr Cvek .len = 1, 18257b0ad9eSPetr Cvek .buf = &data, 18357b0ad9eSPetr Cvek }; 18457b0ad9eSPetr Cvek 18557b0ad9eSPetr Cvek ret = i2c_transfer(client->adapter, &msg, 1); 18657b0ad9eSPetr Cvek if (ret < 0) 18757b0ad9eSPetr Cvek goto err; 18857b0ad9eSPetr Cvek 18957b0ad9eSPetr Cvek msg.flags = I2C_M_RD; 19057b0ad9eSPetr Cvek ret = i2c_transfer(client->adapter, &msg, 1); 19157b0ad9eSPetr Cvek if (ret < 0) 19257b0ad9eSPetr Cvek goto err; 19357b0ad9eSPetr Cvek 19457b0ad9eSPetr Cvek *val = data; 19557b0ad9eSPetr Cvek return 0; 19657b0ad9eSPetr Cvek 19757b0ad9eSPetr Cvek err: 19857b0ad9eSPetr Cvek dev_err(&client->dev, "Failed reading register 0x%02x!\n", reg); 19957b0ad9eSPetr Cvek return ret; 20057b0ad9eSPetr Cvek } 20157b0ad9eSPetr Cvek 20257b0ad9eSPetr Cvek /* write a register */ 20357b0ad9eSPetr Cvek static int ov9640_reg_write(struct i2c_client *client, u8 reg, u8 val) 20457b0ad9eSPetr Cvek { 20557b0ad9eSPetr Cvek int ret; 20657b0ad9eSPetr Cvek u8 _val; 20757b0ad9eSPetr Cvek unsigned char data[2] = { reg, val }; 20857b0ad9eSPetr Cvek struct i2c_msg msg = { 20957b0ad9eSPetr Cvek .addr = client->addr, 21057b0ad9eSPetr Cvek .flags = 0, 21157b0ad9eSPetr Cvek .len = 2, 21257b0ad9eSPetr Cvek .buf = data, 21357b0ad9eSPetr Cvek }; 21457b0ad9eSPetr Cvek 21557b0ad9eSPetr Cvek ret = i2c_transfer(client->adapter, &msg, 1); 21657b0ad9eSPetr Cvek if (ret < 0) { 21757b0ad9eSPetr Cvek dev_err(&client->dev, "Failed writing register 0x%02x!\n", reg); 21857b0ad9eSPetr Cvek return ret; 21957b0ad9eSPetr Cvek } 22057b0ad9eSPetr Cvek 22157b0ad9eSPetr Cvek /* we have to read the register back ... no idea why, maybe HW bug */ 22257b0ad9eSPetr Cvek ret = ov9640_reg_read(client, reg, &_val); 22357b0ad9eSPetr Cvek if (ret) 22457b0ad9eSPetr Cvek dev_err(&client->dev, 22557b0ad9eSPetr Cvek "Failed reading back register 0x%02x!\n", reg); 22657b0ad9eSPetr Cvek 22757b0ad9eSPetr Cvek return 0; 22857b0ad9eSPetr Cvek } 22957b0ad9eSPetr Cvek 23057b0ad9eSPetr Cvek 23157b0ad9eSPetr Cvek /* Read a register, alter its bits, write it back */ 23257b0ad9eSPetr Cvek static int ov9640_reg_rmw(struct i2c_client *client, u8 reg, u8 set, u8 unset) 23357b0ad9eSPetr Cvek { 23457b0ad9eSPetr Cvek u8 val; 23557b0ad9eSPetr Cvek int ret; 23657b0ad9eSPetr Cvek 23757b0ad9eSPetr Cvek ret = ov9640_reg_read(client, reg, &val); 23857b0ad9eSPetr Cvek if (ret) { 23957b0ad9eSPetr Cvek dev_err(&client->dev, 24057b0ad9eSPetr Cvek "[Read]-Modify-Write of register %02x failed!\n", reg); 24157b0ad9eSPetr Cvek return ret; 24257b0ad9eSPetr Cvek } 24357b0ad9eSPetr Cvek 24457b0ad9eSPetr Cvek val |= set; 24557b0ad9eSPetr Cvek val &= ~unset; 24657b0ad9eSPetr Cvek 24757b0ad9eSPetr Cvek ret = ov9640_reg_write(client, reg, val); 24857b0ad9eSPetr Cvek if (ret) 24957b0ad9eSPetr Cvek dev_err(&client->dev, 25057b0ad9eSPetr Cvek "Read-Modify-[Write] of register %02x failed!\n", reg); 25157b0ad9eSPetr Cvek 25257b0ad9eSPetr Cvek return ret; 25357b0ad9eSPetr Cvek } 25457b0ad9eSPetr Cvek 25557b0ad9eSPetr Cvek /* Soft reset the camera. This has nothing to do with the RESET pin! */ 25657b0ad9eSPetr Cvek static int ov9640_reset(struct i2c_client *client) 25757b0ad9eSPetr Cvek { 25857b0ad9eSPetr Cvek int ret; 25957b0ad9eSPetr Cvek 26057b0ad9eSPetr Cvek ret = ov9640_reg_write(client, OV9640_COM7, OV9640_COM7_SCCB_RESET); 26157b0ad9eSPetr Cvek if (ret) 26257b0ad9eSPetr Cvek dev_err(&client->dev, 26357b0ad9eSPetr Cvek "An error occurred while entering soft reset!\n"); 26457b0ad9eSPetr Cvek 26557b0ad9eSPetr Cvek return ret; 26657b0ad9eSPetr Cvek } 26757b0ad9eSPetr Cvek 26857b0ad9eSPetr Cvek /* Start/Stop streaming from the device */ 26957b0ad9eSPetr Cvek static int ov9640_s_stream(struct v4l2_subdev *sd, int enable) 27057b0ad9eSPetr Cvek { 27157b0ad9eSPetr Cvek return 0; 27257b0ad9eSPetr Cvek } 27357b0ad9eSPetr Cvek 27457b0ad9eSPetr Cvek /* Set status of additional camera capabilities */ 27557b0ad9eSPetr Cvek static int ov9640_s_ctrl(struct v4l2_ctrl *ctrl) 27657b0ad9eSPetr Cvek { 27757b0ad9eSPetr Cvek struct ov9640_priv *priv = container_of(ctrl->handler, struct ov9640_priv, hdl); 27857b0ad9eSPetr Cvek struct i2c_client *client = v4l2_get_subdevdata(&priv->subdev); 27957b0ad9eSPetr Cvek 28057b0ad9eSPetr Cvek switch (ctrl->id) { 28157b0ad9eSPetr Cvek case V4L2_CID_VFLIP: 28257b0ad9eSPetr Cvek if (ctrl->val) 28357b0ad9eSPetr Cvek return ov9640_reg_rmw(client, OV9640_MVFP, 28457b0ad9eSPetr Cvek OV9640_MVFP_V, 0); 28557b0ad9eSPetr Cvek return ov9640_reg_rmw(client, OV9640_MVFP, 0, OV9640_MVFP_V); 28657b0ad9eSPetr Cvek case V4L2_CID_HFLIP: 28757b0ad9eSPetr Cvek if (ctrl->val) 28857b0ad9eSPetr Cvek return ov9640_reg_rmw(client, OV9640_MVFP, 28957b0ad9eSPetr Cvek OV9640_MVFP_H, 0); 29057b0ad9eSPetr Cvek return ov9640_reg_rmw(client, OV9640_MVFP, 0, OV9640_MVFP_H); 29157b0ad9eSPetr Cvek } 29257b0ad9eSPetr Cvek return -EINVAL; 29357b0ad9eSPetr Cvek } 29457b0ad9eSPetr Cvek 29557b0ad9eSPetr Cvek #ifdef CONFIG_VIDEO_ADV_DEBUG 29657b0ad9eSPetr Cvek static int ov9640_get_register(struct v4l2_subdev *sd, 29757b0ad9eSPetr Cvek struct v4l2_dbg_register *reg) 29857b0ad9eSPetr Cvek { 29957b0ad9eSPetr Cvek struct i2c_client *client = v4l2_get_subdevdata(sd); 30057b0ad9eSPetr Cvek int ret; 30157b0ad9eSPetr Cvek u8 val; 30257b0ad9eSPetr Cvek 30357b0ad9eSPetr Cvek if (reg->reg & ~0xff) 30457b0ad9eSPetr Cvek return -EINVAL; 30557b0ad9eSPetr Cvek 30657b0ad9eSPetr Cvek reg->size = 1; 30757b0ad9eSPetr Cvek 30857b0ad9eSPetr Cvek ret = ov9640_reg_read(client, reg->reg, &val); 30957b0ad9eSPetr Cvek if (ret) 31057b0ad9eSPetr Cvek return ret; 31157b0ad9eSPetr Cvek 31257b0ad9eSPetr Cvek reg->val = (__u64)val; 31357b0ad9eSPetr Cvek 31457b0ad9eSPetr Cvek return 0; 31557b0ad9eSPetr Cvek } 31657b0ad9eSPetr Cvek 31757b0ad9eSPetr Cvek static int ov9640_set_register(struct v4l2_subdev *sd, 31857b0ad9eSPetr Cvek const struct v4l2_dbg_register *reg) 31957b0ad9eSPetr Cvek { 32057b0ad9eSPetr Cvek struct i2c_client *client = v4l2_get_subdevdata(sd); 32157b0ad9eSPetr Cvek 32257b0ad9eSPetr Cvek if (reg->reg & ~0xff || reg->val & ~0xff) 32357b0ad9eSPetr Cvek return -EINVAL; 32457b0ad9eSPetr Cvek 32557b0ad9eSPetr Cvek return ov9640_reg_write(client, reg->reg, reg->val); 32657b0ad9eSPetr Cvek } 32757b0ad9eSPetr Cvek #endif 32857b0ad9eSPetr Cvek 32957b0ad9eSPetr Cvek static int ov9640_s_power(struct v4l2_subdev *sd, int on) 33057b0ad9eSPetr Cvek { 33157b0ad9eSPetr Cvek struct ov9640_priv *priv = to_ov9640_sensor(sd); 332*9f7e55d2SPetr Cvek int ret = 0; 33357b0ad9eSPetr Cvek 334*9f7e55d2SPetr Cvek if (on) { 335*9f7e55d2SPetr Cvek gpiod_set_value(priv->gpio_power, 1); 336*9f7e55d2SPetr Cvek usleep_range(1000, 2000); 337*9f7e55d2SPetr Cvek ret = v4l2_clk_enable(priv->clk); 338*9f7e55d2SPetr Cvek usleep_range(1000, 2000); 339*9f7e55d2SPetr Cvek gpiod_set_value(priv->gpio_reset, 0); 340*9f7e55d2SPetr Cvek } else { 341*9f7e55d2SPetr Cvek gpiod_set_value(priv->gpio_reset, 1); 342*9f7e55d2SPetr Cvek usleep_range(1000, 2000); 343*9f7e55d2SPetr Cvek v4l2_clk_disable(priv->clk); 344*9f7e55d2SPetr Cvek usleep_range(1000, 2000); 345*9f7e55d2SPetr Cvek gpiod_set_value(priv->gpio_power, 0); 346*9f7e55d2SPetr Cvek } 347*9f7e55d2SPetr Cvek return ret; 34857b0ad9eSPetr Cvek } 34957b0ad9eSPetr Cvek 35057b0ad9eSPetr Cvek /* select nearest higher resolution for capture */ 35157b0ad9eSPetr Cvek static void ov9640_res_roundup(u32 *width, u32 *height) 35257b0ad9eSPetr Cvek { 35357b0ad9eSPetr Cvek int i; 35457b0ad9eSPetr Cvek enum { QQCIF, QQVGA, QCIF, QVGA, CIF, VGA, SXGA }; 35557b0ad9eSPetr Cvek static const int res_x[] = { 88, 160, 176, 320, 352, 640, 1280 }; 35657b0ad9eSPetr Cvek static const int 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 */ 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 */ 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; 39957b0ad9eSPetr Cvek int ov9640_regs_len, matrix_regs_len; 40057b0ad9eSPetr Cvek int i, ret; 40157b0ad9eSPetr Cvek u8 val; 40257b0ad9eSPetr Cvek 40357b0ad9eSPetr Cvek /* select register configuration for given resolution */ 40457b0ad9eSPetr Cvek switch (width) { 40557b0ad9eSPetr Cvek case W_QQCIF: 40657b0ad9eSPetr Cvek ov9640_regs = ov9640_regs_qqcif; 40757b0ad9eSPetr Cvek ov9640_regs_len = ARRAY_SIZE(ov9640_regs_qqcif); 40857b0ad9eSPetr Cvek break; 40957b0ad9eSPetr Cvek case W_QQVGA: 41057b0ad9eSPetr Cvek ov9640_regs = ov9640_regs_qqvga; 41157b0ad9eSPetr Cvek ov9640_regs_len = ARRAY_SIZE(ov9640_regs_qqvga); 41257b0ad9eSPetr Cvek break; 41357b0ad9eSPetr Cvek case W_QCIF: 41457b0ad9eSPetr Cvek ov9640_regs = ov9640_regs_qcif; 41557b0ad9eSPetr Cvek ov9640_regs_len = ARRAY_SIZE(ov9640_regs_qcif); 41657b0ad9eSPetr Cvek break; 41757b0ad9eSPetr Cvek case W_QVGA: 41857b0ad9eSPetr Cvek ov9640_regs = ov9640_regs_qvga; 41957b0ad9eSPetr Cvek ov9640_regs_len = ARRAY_SIZE(ov9640_regs_qvga); 42057b0ad9eSPetr Cvek break; 42157b0ad9eSPetr Cvek case W_CIF: 42257b0ad9eSPetr Cvek ov9640_regs = ov9640_regs_cif; 42357b0ad9eSPetr Cvek ov9640_regs_len = ARRAY_SIZE(ov9640_regs_cif); 42457b0ad9eSPetr Cvek break; 42557b0ad9eSPetr Cvek case W_VGA: 42657b0ad9eSPetr Cvek ov9640_regs = ov9640_regs_vga; 42757b0ad9eSPetr Cvek ov9640_regs_len = ARRAY_SIZE(ov9640_regs_vga); 42857b0ad9eSPetr Cvek break; 42957b0ad9eSPetr Cvek case W_SXGA: 43057b0ad9eSPetr Cvek ov9640_regs = ov9640_regs_sxga; 43157b0ad9eSPetr Cvek ov9640_regs_len = ARRAY_SIZE(ov9640_regs_sxga); 43257b0ad9eSPetr Cvek break; 43357b0ad9eSPetr Cvek default: 43457b0ad9eSPetr Cvek dev_err(&client->dev, "Failed to select resolution!\n"); 43557b0ad9eSPetr Cvek return -EINVAL; 43657b0ad9eSPetr Cvek } 43757b0ad9eSPetr Cvek 43857b0ad9eSPetr Cvek /* select color matrix configuration for given color encoding */ 43957b0ad9eSPetr Cvek if (code == MEDIA_BUS_FMT_UYVY8_2X8) { 44057b0ad9eSPetr Cvek matrix_regs = ov9640_regs_yuv; 44157b0ad9eSPetr Cvek matrix_regs_len = ARRAY_SIZE(ov9640_regs_yuv); 44257b0ad9eSPetr Cvek } else { 44357b0ad9eSPetr Cvek matrix_regs = ov9640_regs_rgb; 44457b0ad9eSPetr Cvek matrix_regs_len = ARRAY_SIZE(ov9640_regs_rgb); 44557b0ad9eSPetr Cvek } 44657b0ad9eSPetr Cvek 44757b0ad9eSPetr Cvek /* write register settings into the module */ 44857b0ad9eSPetr Cvek for (i = 0; i < ov9640_regs_len; i++) { 44957b0ad9eSPetr Cvek val = ov9640_regs[i].val; 45057b0ad9eSPetr Cvek 45157b0ad9eSPetr Cvek switch (ov9640_regs[i].reg) { 45257b0ad9eSPetr Cvek case OV9640_COM7: 45357b0ad9eSPetr Cvek val |= alts->com7; 45457b0ad9eSPetr Cvek break; 45557b0ad9eSPetr Cvek case OV9640_COM12: 45657b0ad9eSPetr Cvek val |= alts->com12; 45757b0ad9eSPetr Cvek break; 45857b0ad9eSPetr Cvek case OV9640_COM13: 45957b0ad9eSPetr Cvek val |= alts->com13; 46057b0ad9eSPetr Cvek break; 46157b0ad9eSPetr Cvek case OV9640_COM15: 46257b0ad9eSPetr Cvek val |= alts->com15; 46357b0ad9eSPetr Cvek break; 46457b0ad9eSPetr Cvek } 46557b0ad9eSPetr Cvek 46657b0ad9eSPetr Cvek ret = ov9640_reg_write(client, ov9640_regs[i].reg, val); 46757b0ad9eSPetr Cvek if (ret) 46857b0ad9eSPetr Cvek return ret; 46957b0ad9eSPetr Cvek } 47057b0ad9eSPetr Cvek 47157b0ad9eSPetr Cvek /* write color matrix configuration into the module */ 47257b0ad9eSPetr Cvek for (i = 0; i < matrix_regs_len; i++) { 47357b0ad9eSPetr Cvek ret = ov9640_reg_write(client, matrix_regs[i].reg, 47457b0ad9eSPetr Cvek matrix_regs[i].val); 47557b0ad9eSPetr Cvek if (ret) 47657b0ad9eSPetr Cvek return ret; 47757b0ad9eSPetr Cvek } 47857b0ad9eSPetr Cvek 47957b0ad9eSPetr Cvek return 0; 48057b0ad9eSPetr Cvek } 48157b0ad9eSPetr Cvek 48257b0ad9eSPetr Cvek /* program default register values */ 48357b0ad9eSPetr Cvek static int ov9640_prog_dflt(struct i2c_client *client) 48457b0ad9eSPetr Cvek { 48557b0ad9eSPetr Cvek int i, ret; 48657b0ad9eSPetr Cvek 48757b0ad9eSPetr Cvek for (i = 0; i < ARRAY_SIZE(ov9640_regs_dflt); i++) { 48857b0ad9eSPetr Cvek ret = ov9640_reg_write(client, ov9640_regs_dflt[i].reg, 48957b0ad9eSPetr Cvek ov9640_regs_dflt[i].val); 49057b0ad9eSPetr Cvek if (ret) 49157b0ad9eSPetr Cvek return ret; 49257b0ad9eSPetr Cvek } 49357b0ad9eSPetr Cvek 49457b0ad9eSPetr Cvek /* wait for the changes to actually happen, 140ms are not enough yet */ 495*9f7e55d2SPetr Cvek msleep(150); 49657b0ad9eSPetr Cvek 49757b0ad9eSPetr Cvek return 0; 49857b0ad9eSPetr Cvek } 49957b0ad9eSPetr Cvek 50057b0ad9eSPetr Cvek /* set the format we will capture in */ 50157b0ad9eSPetr Cvek static int ov9640_s_fmt(struct v4l2_subdev *sd, 50257b0ad9eSPetr Cvek struct v4l2_mbus_framefmt *mf) 50357b0ad9eSPetr Cvek { 50457b0ad9eSPetr Cvek struct i2c_client *client = v4l2_get_subdevdata(sd); 50557b0ad9eSPetr Cvek struct ov9640_reg_alt alts = {0}; 50657b0ad9eSPetr Cvek int ret; 50757b0ad9eSPetr Cvek 50857b0ad9eSPetr Cvek ov9640_alter_regs(mf->code, &alts); 50957b0ad9eSPetr Cvek 51057b0ad9eSPetr Cvek ov9640_reset(client); 51157b0ad9eSPetr Cvek 51257b0ad9eSPetr Cvek ret = ov9640_prog_dflt(client); 51357b0ad9eSPetr Cvek if (ret) 51457b0ad9eSPetr Cvek return ret; 51557b0ad9eSPetr Cvek 51657b0ad9eSPetr Cvek return ov9640_write_regs(client, mf->width, mf->code, &alts); 51757b0ad9eSPetr Cvek } 51857b0ad9eSPetr Cvek 51957b0ad9eSPetr Cvek static int ov9640_set_fmt(struct v4l2_subdev *sd, 52057b0ad9eSPetr Cvek struct v4l2_subdev_pad_config *cfg, 52157b0ad9eSPetr Cvek struct v4l2_subdev_format *format) 52257b0ad9eSPetr Cvek { 52357b0ad9eSPetr Cvek struct v4l2_mbus_framefmt *mf = &format->format; 52457b0ad9eSPetr Cvek 52557b0ad9eSPetr Cvek if (format->pad) 52657b0ad9eSPetr Cvek return -EINVAL; 52757b0ad9eSPetr Cvek 52857b0ad9eSPetr Cvek ov9640_res_roundup(&mf->width, &mf->height); 52957b0ad9eSPetr Cvek 53057b0ad9eSPetr Cvek mf->field = V4L2_FIELD_NONE; 53157b0ad9eSPetr Cvek 53257b0ad9eSPetr Cvek switch (mf->code) { 53357b0ad9eSPetr Cvek case MEDIA_BUS_FMT_RGB555_2X8_PADHI_LE: 53457b0ad9eSPetr Cvek case MEDIA_BUS_FMT_RGB565_2X8_LE: 53557b0ad9eSPetr Cvek mf->colorspace = V4L2_COLORSPACE_SRGB; 53657b0ad9eSPetr Cvek break; 53757b0ad9eSPetr Cvek default: 53857b0ad9eSPetr Cvek mf->code = MEDIA_BUS_FMT_UYVY8_2X8; 53957b0ad9eSPetr Cvek /* fall through */ 54057b0ad9eSPetr Cvek case MEDIA_BUS_FMT_UYVY8_2X8: 54157b0ad9eSPetr Cvek mf->colorspace = V4L2_COLORSPACE_JPEG; 54257b0ad9eSPetr Cvek break; 54357b0ad9eSPetr Cvek } 54457b0ad9eSPetr Cvek 54557b0ad9eSPetr Cvek if (format->which == V4L2_SUBDEV_FORMAT_ACTIVE) 54657b0ad9eSPetr Cvek return ov9640_s_fmt(sd, mf); 54757b0ad9eSPetr Cvek 54857b0ad9eSPetr Cvek cfg->try_fmt = *mf; 54957b0ad9eSPetr Cvek return 0; 55057b0ad9eSPetr Cvek } 55157b0ad9eSPetr Cvek 55257b0ad9eSPetr Cvek static int ov9640_enum_mbus_code(struct v4l2_subdev *sd, 55357b0ad9eSPetr Cvek struct v4l2_subdev_pad_config *cfg, 55457b0ad9eSPetr Cvek struct v4l2_subdev_mbus_code_enum *code) 55557b0ad9eSPetr Cvek { 55657b0ad9eSPetr Cvek if (code->pad || code->index >= ARRAY_SIZE(ov9640_codes)) 55757b0ad9eSPetr Cvek return -EINVAL; 55857b0ad9eSPetr Cvek 55957b0ad9eSPetr Cvek code->code = ov9640_codes[code->index]; 56057b0ad9eSPetr Cvek return 0; 56157b0ad9eSPetr Cvek } 56257b0ad9eSPetr Cvek 56357b0ad9eSPetr Cvek static int ov9640_get_selection(struct v4l2_subdev *sd, 56457b0ad9eSPetr Cvek struct v4l2_subdev_pad_config *cfg, 56557b0ad9eSPetr Cvek struct v4l2_subdev_selection *sel) 56657b0ad9eSPetr Cvek { 56757b0ad9eSPetr Cvek if (sel->which != V4L2_SUBDEV_FORMAT_ACTIVE) 56857b0ad9eSPetr Cvek return -EINVAL; 56957b0ad9eSPetr Cvek 57057b0ad9eSPetr Cvek sel->r.left = 0; 57157b0ad9eSPetr Cvek sel->r.top = 0; 57257b0ad9eSPetr Cvek switch (sel->target) { 57357b0ad9eSPetr Cvek case V4L2_SEL_TGT_CROP_BOUNDS: 57457b0ad9eSPetr Cvek case V4L2_SEL_TGT_CROP: 57557b0ad9eSPetr Cvek sel->r.width = W_SXGA; 57657b0ad9eSPetr Cvek sel->r.height = H_SXGA; 57757b0ad9eSPetr Cvek return 0; 57857b0ad9eSPetr Cvek default: 57957b0ad9eSPetr Cvek return -EINVAL; 58057b0ad9eSPetr Cvek } 58157b0ad9eSPetr Cvek } 58257b0ad9eSPetr Cvek 58357b0ad9eSPetr Cvek static int ov9640_video_probe(struct i2c_client *client) 58457b0ad9eSPetr Cvek { 58557b0ad9eSPetr Cvek struct v4l2_subdev *sd = i2c_get_clientdata(client); 58657b0ad9eSPetr Cvek struct ov9640_priv *priv = to_ov9640_sensor(sd); 58757b0ad9eSPetr Cvek u8 pid, ver, midh, midl; 58857b0ad9eSPetr Cvek const char *devname; 58957b0ad9eSPetr Cvek int ret; 59057b0ad9eSPetr Cvek 59157b0ad9eSPetr Cvek ret = ov9640_s_power(&priv->subdev, 1); 59257b0ad9eSPetr Cvek if (ret < 0) 59357b0ad9eSPetr Cvek return ret; 59457b0ad9eSPetr Cvek 59557b0ad9eSPetr Cvek /* 59657b0ad9eSPetr Cvek * check and show product ID and manufacturer ID 59757b0ad9eSPetr Cvek */ 59857b0ad9eSPetr Cvek 59957b0ad9eSPetr Cvek ret = ov9640_reg_read(client, OV9640_PID, &pid); 60057b0ad9eSPetr Cvek if (!ret) 60157b0ad9eSPetr Cvek ret = ov9640_reg_read(client, OV9640_VER, &ver); 60257b0ad9eSPetr Cvek if (!ret) 60357b0ad9eSPetr Cvek ret = ov9640_reg_read(client, OV9640_MIDH, &midh); 60457b0ad9eSPetr Cvek if (!ret) 60557b0ad9eSPetr Cvek ret = ov9640_reg_read(client, OV9640_MIDL, &midl); 60657b0ad9eSPetr Cvek if (ret) 60757b0ad9eSPetr Cvek goto done; 60857b0ad9eSPetr Cvek 60957b0ad9eSPetr Cvek switch (VERSION(pid, ver)) { 61057b0ad9eSPetr Cvek case OV9640_V2: 61157b0ad9eSPetr Cvek devname = "ov9640"; 61257b0ad9eSPetr Cvek priv->revision = 2; 61357b0ad9eSPetr Cvek break; 61457b0ad9eSPetr Cvek case OV9640_V3: 61557b0ad9eSPetr Cvek devname = "ov9640"; 61657b0ad9eSPetr Cvek priv->revision = 3; 61757b0ad9eSPetr Cvek break; 61857b0ad9eSPetr Cvek default: 61957b0ad9eSPetr Cvek dev_err(&client->dev, "Product ID error %x:%x\n", pid, ver); 62057b0ad9eSPetr Cvek ret = -ENODEV; 62157b0ad9eSPetr Cvek goto done; 62257b0ad9eSPetr Cvek } 62357b0ad9eSPetr Cvek 62457b0ad9eSPetr Cvek dev_info(&client->dev, "%s Product ID %0x:%0x Manufacturer ID %x:%x\n", 62557b0ad9eSPetr Cvek devname, pid, ver, midh, midl); 62657b0ad9eSPetr Cvek 62757b0ad9eSPetr Cvek ret = v4l2_ctrl_handler_setup(&priv->hdl); 62857b0ad9eSPetr Cvek 62957b0ad9eSPetr Cvek done: 63057b0ad9eSPetr Cvek ov9640_s_power(&priv->subdev, 0); 63157b0ad9eSPetr Cvek return ret; 63257b0ad9eSPetr Cvek } 63357b0ad9eSPetr Cvek 63457b0ad9eSPetr Cvek static const struct v4l2_ctrl_ops ov9640_ctrl_ops = { 63557b0ad9eSPetr Cvek .s_ctrl = ov9640_s_ctrl, 63657b0ad9eSPetr Cvek }; 63757b0ad9eSPetr Cvek 63857b0ad9eSPetr Cvek static const struct v4l2_subdev_core_ops ov9640_core_ops = { 63957b0ad9eSPetr Cvek #ifdef CONFIG_VIDEO_ADV_DEBUG 64057b0ad9eSPetr Cvek .g_register = ov9640_get_register, 64157b0ad9eSPetr Cvek .s_register = ov9640_set_register, 64257b0ad9eSPetr Cvek #endif 64357b0ad9eSPetr Cvek .s_power = ov9640_s_power, 64457b0ad9eSPetr Cvek }; 64557b0ad9eSPetr Cvek 64657b0ad9eSPetr Cvek /* Request bus settings on camera side */ 64757b0ad9eSPetr Cvek static int ov9640_g_mbus_config(struct v4l2_subdev *sd, 64857b0ad9eSPetr Cvek struct v4l2_mbus_config *cfg) 64957b0ad9eSPetr Cvek { 65057b0ad9eSPetr Cvek cfg->flags = V4L2_MBUS_PCLK_SAMPLE_RISING | V4L2_MBUS_MASTER | 65157b0ad9eSPetr Cvek V4L2_MBUS_VSYNC_ACTIVE_HIGH | V4L2_MBUS_HSYNC_ACTIVE_HIGH | 65257b0ad9eSPetr Cvek V4L2_MBUS_DATA_ACTIVE_HIGH; 65357b0ad9eSPetr Cvek cfg->type = V4L2_MBUS_PARALLEL; 65457b0ad9eSPetr Cvek 65557b0ad9eSPetr Cvek return 0; 65657b0ad9eSPetr Cvek } 65757b0ad9eSPetr Cvek 65857b0ad9eSPetr Cvek static const struct v4l2_subdev_video_ops ov9640_video_ops = { 65957b0ad9eSPetr Cvek .s_stream = ov9640_s_stream, 66057b0ad9eSPetr Cvek .g_mbus_config = ov9640_g_mbus_config, 66157b0ad9eSPetr Cvek }; 66257b0ad9eSPetr Cvek 66357b0ad9eSPetr Cvek static const struct v4l2_subdev_pad_ops ov9640_pad_ops = { 66457b0ad9eSPetr Cvek .enum_mbus_code = ov9640_enum_mbus_code, 66557b0ad9eSPetr Cvek .get_selection = ov9640_get_selection, 66657b0ad9eSPetr Cvek .set_fmt = ov9640_set_fmt, 66757b0ad9eSPetr Cvek }; 66857b0ad9eSPetr Cvek 66957b0ad9eSPetr Cvek static const struct v4l2_subdev_ops ov9640_subdev_ops = { 67057b0ad9eSPetr Cvek .core = &ov9640_core_ops, 67157b0ad9eSPetr Cvek .video = &ov9640_video_ops, 67257b0ad9eSPetr Cvek .pad = &ov9640_pad_ops, 67357b0ad9eSPetr Cvek }; 67457b0ad9eSPetr Cvek 67557b0ad9eSPetr Cvek /* 67657b0ad9eSPetr Cvek * i2c_driver function 67757b0ad9eSPetr Cvek */ 67857b0ad9eSPetr Cvek static int ov9640_probe(struct i2c_client *client, 67957b0ad9eSPetr Cvek const struct i2c_device_id *did) 68057b0ad9eSPetr Cvek { 68157b0ad9eSPetr Cvek struct ov9640_priv *priv; 68257b0ad9eSPetr Cvek int ret; 68357b0ad9eSPetr Cvek 684*9f7e55d2SPetr Cvek priv = devm_kzalloc(&client->dev, sizeof(*priv), 685*9f7e55d2SPetr Cvek GFP_KERNEL); 68657b0ad9eSPetr Cvek if (!priv) 68757b0ad9eSPetr Cvek return -ENOMEM; 68857b0ad9eSPetr Cvek 689*9f7e55d2SPetr Cvek priv->gpio_power = devm_gpiod_get(&client->dev, "Camera power", 690*9f7e55d2SPetr Cvek GPIOD_OUT_LOW); 691*9f7e55d2SPetr Cvek if (IS_ERR_OR_NULL(priv->gpio_power)) { 692*9f7e55d2SPetr Cvek ret = PTR_ERR(priv->gpio_power); 693*9f7e55d2SPetr Cvek return ret; 694*9f7e55d2SPetr Cvek } 695*9f7e55d2SPetr Cvek 696*9f7e55d2SPetr Cvek priv->gpio_reset = devm_gpiod_get(&client->dev, "Camera reset", 697*9f7e55d2SPetr Cvek GPIOD_OUT_HIGH); 698*9f7e55d2SPetr Cvek if (IS_ERR_OR_NULL(priv->gpio_reset)) { 699*9f7e55d2SPetr Cvek ret = PTR_ERR(priv->gpio_reset); 700*9f7e55d2SPetr Cvek return ret; 701*9f7e55d2SPetr Cvek } 702*9f7e55d2SPetr Cvek 70357b0ad9eSPetr Cvek v4l2_i2c_subdev_init(&priv->subdev, client, &ov9640_subdev_ops); 70457b0ad9eSPetr Cvek 70557b0ad9eSPetr Cvek v4l2_ctrl_handler_init(&priv->hdl, 2); 70657b0ad9eSPetr Cvek v4l2_ctrl_new_std(&priv->hdl, &ov9640_ctrl_ops, 70757b0ad9eSPetr Cvek V4L2_CID_VFLIP, 0, 1, 1, 0); 70857b0ad9eSPetr Cvek v4l2_ctrl_new_std(&priv->hdl, &ov9640_ctrl_ops, 70957b0ad9eSPetr Cvek V4L2_CID_HFLIP, 0, 1, 1, 0); 71057b0ad9eSPetr Cvek priv->subdev.ctrl_handler = &priv->hdl; 71157b0ad9eSPetr Cvek if (priv->hdl.error) 71257b0ad9eSPetr Cvek return priv->hdl.error; 71357b0ad9eSPetr Cvek 71457b0ad9eSPetr Cvek priv->clk = v4l2_clk_get(&client->dev, "mclk"); 71557b0ad9eSPetr Cvek if (IS_ERR(priv->clk)) { 71657b0ad9eSPetr Cvek ret = PTR_ERR(priv->clk); 71757b0ad9eSPetr Cvek goto eclkget; 71857b0ad9eSPetr Cvek } 71957b0ad9eSPetr Cvek 72057b0ad9eSPetr Cvek ret = ov9640_video_probe(client); 721*9f7e55d2SPetr Cvek if (ret) 722*9f7e55d2SPetr Cvek goto eprobe; 723*9f7e55d2SPetr Cvek 724*9f7e55d2SPetr Cvek priv->subdev.dev = &client->dev; 725*9f7e55d2SPetr Cvek ret = v4l2_async_register_subdev(&priv->subdev); 726*9f7e55d2SPetr Cvek if (ret) 727*9f7e55d2SPetr Cvek goto eprobe; 728*9f7e55d2SPetr Cvek 729*9f7e55d2SPetr Cvek return 0; 730*9f7e55d2SPetr Cvek 731*9f7e55d2SPetr Cvek eprobe: 73257b0ad9eSPetr Cvek v4l2_clk_put(priv->clk); 73357b0ad9eSPetr Cvek eclkget: 73457b0ad9eSPetr Cvek v4l2_ctrl_handler_free(&priv->hdl); 73557b0ad9eSPetr Cvek return ret; 73657b0ad9eSPetr Cvek } 73757b0ad9eSPetr Cvek 73857b0ad9eSPetr Cvek static int ov9640_remove(struct i2c_client *client) 73957b0ad9eSPetr Cvek { 74057b0ad9eSPetr Cvek struct v4l2_subdev *sd = i2c_get_clientdata(client); 74157b0ad9eSPetr Cvek struct ov9640_priv *priv = to_ov9640_sensor(sd); 74257b0ad9eSPetr Cvek 74357b0ad9eSPetr Cvek v4l2_clk_put(priv->clk); 744*9f7e55d2SPetr Cvek v4l2_async_unregister_subdev(&priv->subdev); 74557b0ad9eSPetr Cvek v4l2_ctrl_handler_free(&priv->hdl); 74657b0ad9eSPetr Cvek return 0; 74757b0ad9eSPetr Cvek } 74857b0ad9eSPetr Cvek 74957b0ad9eSPetr Cvek static const struct i2c_device_id ov9640_id[] = { 75057b0ad9eSPetr Cvek { "ov9640", 0 }, 75157b0ad9eSPetr Cvek { } 75257b0ad9eSPetr Cvek }; 75357b0ad9eSPetr Cvek MODULE_DEVICE_TABLE(i2c, ov9640_id); 75457b0ad9eSPetr Cvek 75557b0ad9eSPetr Cvek static struct i2c_driver ov9640_i2c_driver = { 75657b0ad9eSPetr Cvek .driver = { 75757b0ad9eSPetr Cvek .name = "ov9640", 75857b0ad9eSPetr Cvek }, 75957b0ad9eSPetr Cvek .probe = ov9640_probe, 76057b0ad9eSPetr Cvek .remove = ov9640_remove, 76157b0ad9eSPetr Cvek .id_table = ov9640_id, 76257b0ad9eSPetr Cvek }; 76357b0ad9eSPetr Cvek 76457b0ad9eSPetr Cvek module_i2c_driver(ov9640_i2c_driver); 76557b0ad9eSPetr Cvek 76657b0ad9eSPetr Cvek MODULE_DESCRIPTION("SoC Camera driver for OmniVision OV96xx"); 76757b0ad9eSPetr Cvek MODULE_AUTHOR("Marek Vasut <marek.vasut@gmail.com>"); 76857b0ad9eSPetr Cvek MODULE_LICENSE("GPL v2"); 769