1c942fddfSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
29cae9722STodor Tomov /*
39cae9722STodor Tomov * Driver for the OV5645 camera sensor.
49cae9722STodor Tomov *
59cae9722STodor Tomov * Copyright (c) 2011-2015, The Linux Foundation. All rights reserved.
69cae9722STodor Tomov * Copyright (C) 2015 By Tech Design S.L. All Rights Reserved.
79cae9722STodor Tomov * Copyright (C) 2012-2013 Freescale Semiconductor, Inc. All Rights Reserved.
89cae9722STodor Tomov *
99cae9722STodor Tomov * Based on:
109cae9722STodor Tomov * - the OV5645 driver from QC msm-3.10 kernel on codeaurora.org:
119cae9722STodor Tomov * https://us.codeaurora.org/cgit/quic/la/kernel/msm-3.10/tree/drivers/
129cae9722STodor Tomov * media/platform/msm/camera_v2/sensor/ov5645.c?h=LA.BR.1.2.4_rb1.41
139cae9722STodor Tomov * - the OV5640 driver posted on linux-media:
149cae9722STodor Tomov * https://www.mail-archive.com/linux-media%40vger.kernel.org/msg92671.html
159cae9722STodor Tomov */
169cae9722STodor Tomov
179cae9722STodor Tomov #include <linux/bitops.h>
189cae9722STodor Tomov #include <linux/clk.h>
199cae9722STodor Tomov #include <linux/delay.h>
209cae9722STodor Tomov #include <linux/device.h>
219cae9722STodor Tomov #include <linux/gpio/consumer.h>
229cae9722STodor Tomov #include <linux/i2c.h>
239cae9722STodor Tomov #include <linux/init.h>
249cae9722STodor Tomov #include <linux/module.h>
259cae9722STodor Tomov #include <linux/of.h>
269cae9722STodor Tomov #include <linux/of_graph.h>
27b5045039SLad Prabhakar #include <linux/pm_runtime.h>
289cae9722STodor Tomov #include <linux/regulator/consumer.h>
299cae9722STodor Tomov #include <linux/slab.h>
309cae9722STodor Tomov #include <linux/types.h>
319cae9722STodor Tomov #include <media/v4l2-ctrls.h>
32859969b3SSakari Ailus #include <media/v4l2-fwnode.h>
339cae9722STodor Tomov #include <media/v4l2-subdev.h>
349cae9722STodor Tomov
359cae9722STodor Tomov #define OV5645_SYSTEM_CTRL0 0x3008
369cae9722STodor Tomov #define OV5645_SYSTEM_CTRL0_START 0x02
379cae9722STodor Tomov #define OV5645_SYSTEM_CTRL0_STOP 0x42
389cae9722STodor Tomov #define OV5645_CHIP_ID_HIGH 0x300a
399cae9722STodor Tomov #define OV5645_CHIP_ID_HIGH_BYTE 0x56
409cae9722STodor Tomov #define OV5645_CHIP_ID_LOW 0x300b
419cae9722STodor Tomov #define OV5645_CHIP_ID_LOW_BYTE 0x45
42092e8eb9SEzequiel Garcia #define OV5645_IO_MIPI_CTRL00 0x300e
43092e8eb9SEzequiel Garcia #define OV5645_PAD_OUTPUT00 0x3019
449cae9722STodor Tomov #define OV5645_AWB_MANUAL_CONTROL 0x3406
459cae9722STodor Tomov #define OV5645_AWB_MANUAL_ENABLE BIT(0)
469cae9722STodor Tomov #define OV5645_AEC_PK_MANUAL 0x3503
479cae9722STodor Tomov #define OV5645_AEC_MANUAL_ENABLE BIT(0)
489cae9722STodor Tomov #define OV5645_AGC_MANUAL_ENABLE BIT(1)
499cae9722STodor Tomov #define OV5645_TIMING_TC_REG20 0x3820
509cae9722STodor Tomov #define OV5645_SENSOR_VFLIP BIT(1)
519cae9722STodor Tomov #define OV5645_ISP_VFLIP BIT(2)
529cae9722STodor Tomov #define OV5645_TIMING_TC_REG21 0x3821
539cae9722STodor Tomov #define OV5645_SENSOR_MIRROR BIT(1)
54092e8eb9SEzequiel Garcia #define OV5645_MIPI_CTRL00 0x4800
559cae9722STodor Tomov #define OV5645_PRE_ISP_TEST_SETTING_1 0x503d
569cae9722STodor Tomov #define OV5645_TEST_PATTERN_MASK 0x3
579cae9722STodor Tomov #define OV5645_SET_TEST_PATTERN(x) ((x) & OV5645_TEST_PATTERN_MASK)
589cae9722STodor Tomov #define OV5645_TEST_PATTERN_ENABLE BIT(7)
599cae9722STodor Tomov #define OV5645_SDE_SAT_U 0x5583
609cae9722STodor Tomov #define OV5645_SDE_SAT_V 0x5584
619cae9722STodor Tomov
624962bb38SFabio Estevam /* regulator supplies */
634962bb38SFabio Estevam static const char * const ov5645_supply_name[] = {
644962bb38SFabio Estevam "vdddo", /* Digital I/O (1.8V) supply */
654962bb38SFabio Estevam "vdda", /* Analog (2.8V) supply */
664962bb38SFabio Estevam "vddd", /* Digital Core (1.5V) supply */
674962bb38SFabio Estevam };
684962bb38SFabio Estevam
694962bb38SFabio Estevam #define OV5645_NUM_SUPPLIES ARRAY_SIZE(ov5645_supply_name)
704962bb38SFabio Estevam
719cae9722STodor Tomov struct reg_value {
729cae9722STodor Tomov u16 reg;
739cae9722STodor Tomov u8 val;
749cae9722STodor Tomov };
759cae9722STodor Tomov
769cae9722STodor Tomov struct ov5645_mode_info {
779cae9722STodor Tomov u32 width;
789cae9722STodor Tomov u32 height;
799cae9722STodor Tomov const struct reg_value *data;
809cae9722STodor Tomov u32 data_size;
811c2177fdSTodor Tomov u32 pixel_clock;
82e72e6cbeSTodor Tomov u32 link_freq;
839cae9722STodor Tomov };
849cae9722STodor Tomov
859cae9722STodor Tomov struct ov5645 {
869cae9722STodor Tomov struct i2c_client *i2c_client;
879cae9722STodor Tomov struct device *dev;
889cae9722STodor Tomov struct v4l2_subdev sd;
899cae9722STodor Tomov struct media_pad pad;
90859969b3SSakari Ailus struct v4l2_fwnode_endpoint ep;
919cae9722STodor Tomov struct v4l2_mbus_framefmt fmt;
929cae9722STodor Tomov struct v4l2_rect crop;
939cae9722STodor Tomov struct clk *xclk;
949cae9722STodor Tomov
954962bb38SFabio Estevam struct regulator_bulk_data supplies[OV5645_NUM_SUPPLIES];
969cae9722STodor Tomov
979cae9722STodor Tomov const struct ov5645_mode_info *current_mode;
989cae9722STodor Tomov
999cae9722STodor Tomov struct v4l2_ctrl_handler ctrls;
1001c2177fdSTodor Tomov struct v4l2_ctrl *pixel_clock;
101e72e6cbeSTodor Tomov struct v4l2_ctrl *link_freq;
1029cae9722STodor Tomov
1039cae9722STodor Tomov /* Cached register values */
1049cae9722STodor Tomov u8 aec_pk_manual;
1059cae9722STodor Tomov u8 timing_tc_reg20;
1069cae9722STodor Tomov u8 timing_tc_reg21;
1079cae9722STodor Tomov
1089cae9722STodor Tomov struct mutex power_lock; /* lock to protect power state */
1099cae9722STodor Tomov
1109cae9722STodor Tomov struct gpio_desc *enable_gpio;
1119cae9722STodor Tomov struct gpio_desc *rst_gpio;
1129cae9722STodor Tomov };
1139cae9722STodor Tomov
to_ov5645(struct v4l2_subdev * sd)1149cae9722STodor Tomov static inline struct ov5645 *to_ov5645(struct v4l2_subdev *sd)
1159cae9722STodor Tomov {
1169cae9722STodor Tomov return container_of(sd, struct ov5645, sd);
1179cae9722STodor Tomov }
1189cae9722STodor Tomov
1199cae9722STodor Tomov static const struct reg_value ov5645_global_init_setting[] = {
1209cae9722STodor Tomov { 0x3103, 0x11 },
1219cae9722STodor Tomov { 0x3008, 0x82 },
1229cae9722STodor Tomov { 0x3008, 0x42 },
1239cae9722STodor Tomov { 0x3103, 0x03 },
1249cae9722STodor Tomov { 0x3503, 0x07 },
1259cae9722STodor Tomov { 0x3002, 0x1c },
1269cae9722STodor Tomov { 0x3006, 0xc3 },
1279cae9722STodor Tomov { 0x3017, 0x00 },
1289cae9722STodor Tomov { 0x3018, 0x00 },
1299cae9722STodor Tomov { 0x302e, 0x0b },
1309cae9722STodor Tomov { 0x3037, 0x13 },
1319cae9722STodor Tomov { 0x3108, 0x01 },
1329cae9722STodor Tomov { 0x3611, 0x06 },
1339cae9722STodor Tomov { 0x3500, 0x00 },
1349cae9722STodor Tomov { 0x3501, 0x01 },
1359cae9722STodor Tomov { 0x3502, 0x00 },
1369cae9722STodor Tomov { 0x350a, 0x00 },
1379cae9722STodor Tomov { 0x350b, 0x3f },
1389cae9722STodor Tomov { 0x3620, 0x33 },
1399cae9722STodor Tomov { 0x3621, 0xe0 },
1409cae9722STodor Tomov { 0x3622, 0x01 },
1419cae9722STodor Tomov { 0x3630, 0x2e },
1429cae9722STodor Tomov { 0x3631, 0x00 },
1439cae9722STodor Tomov { 0x3632, 0x32 },
1449cae9722STodor Tomov { 0x3633, 0x52 },
1459cae9722STodor Tomov { 0x3634, 0x70 },
1469cae9722STodor Tomov { 0x3635, 0x13 },
1479cae9722STodor Tomov { 0x3636, 0x03 },
1489cae9722STodor Tomov { 0x3703, 0x5a },
1499cae9722STodor Tomov { 0x3704, 0xa0 },
1509cae9722STodor Tomov { 0x3705, 0x1a },
1519cae9722STodor Tomov { 0x3709, 0x12 },
1529cae9722STodor Tomov { 0x370b, 0x61 },
1539cae9722STodor Tomov { 0x370f, 0x10 },
1549cae9722STodor Tomov { 0x3715, 0x78 },
1559cae9722STodor Tomov { 0x3717, 0x01 },
1569cae9722STodor Tomov { 0x371b, 0x20 },
1579cae9722STodor Tomov { 0x3731, 0x12 },
1589cae9722STodor Tomov { 0x3901, 0x0a },
1599cae9722STodor Tomov { 0x3905, 0x02 },
1609cae9722STodor Tomov { 0x3906, 0x10 },
1619cae9722STodor Tomov { 0x3719, 0x86 },
1629cae9722STodor Tomov { 0x3810, 0x00 },
1639cae9722STodor Tomov { 0x3811, 0x10 },
1649cae9722STodor Tomov { 0x3812, 0x00 },
1659cae9722STodor Tomov { 0x3821, 0x01 },
1669cae9722STodor Tomov { 0x3824, 0x01 },
1679cae9722STodor Tomov { 0x3826, 0x03 },
1689cae9722STodor Tomov { 0x3828, 0x08 },
1699cae9722STodor Tomov { 0x3a19, 0xf8 },
1709cae9722STodor Tomov { 0x3c01, 0x34 },
1719cae9722STodor Tomov { 0x3c04, 0x28 },
1729cae9722STodor Tomov { 0x3c05, 0x98 },
1739cae9722STodor Tomov { 0x3c07, 0x07 },
1749cae9722STodor Tomov { 0x3c09, 0xc2 },
1759cae9722STodor Tomov { 0x3c0a, 0x9c },
1769cae9722STodor Tomov { 0x3c0b, 0x40 },
1779cae9722STodor Tomov { 0x3c01, 0x34 },
1789cae9722STodor Tomov { 0x4001, 0x02 },
1799cae9722STodor Tomov { 0x4514, 0x00 },
1809cae9722STodor Tomov { 0x4520, 0xb0 },
1819cae9722STodor Tomov { 0x460b, 0x37 },
1829cae9722STodor Tomov { 0x460c, 0x20 },
1839cae9722STodor Tomov { 0x4818, 0x01 },
1849cae9722STodor Tomov { 0x481d, 0xf0 },
1859cae9722STodor Tomov { 0x481f, 0x50 },
1869cae9722STodor Tomov { 0x4823, 0x70 },
1879cae9722STodor Tomov { 0x4831, 0x14 },
1889cae9722STodor Tomov { 0x5000, 0xa7 },
1899cae9722STodor Tomov { 0x5001, 0x83 },
1909cae9722STodor Tomov { 0x501d, 0x00 },
1919cae9722STodor Tomov { 0x501f, 0x00 },
1929cae9722STodor Tomov { 0x503d, 0x00 },
1939cae9722STodor Tomov { 0x505c, 0x30 },
1949cae9722STodor Tomov { 0x5181, 0x59 },
1959cae9722STodor Tomov { 0x5183, 0x00 },
1969cae9722STodor Tomov { 0x5191, 0xf0 },
1979cae9722STodor Tomov { 0x5192, 0x03 },
1989cae9722STodor Tomov { 0x5684, 0x10 },
1999cae9722STodor Tomov { 0x5685, 0xa0 },
2009cae9722STodor Tomov { 0x5686, 0x0c },
2019cae9722STodor Tomov { 0x5687, 0x78 },
2029cae9722STodor Tomov { 0x5a00, 0x08 },
2039cae9722STodor Tomov { 0x5a21, 0x00 },
2049cae9722STodor Tomov { 0x5a24, 0x00 },
2059cae9722STodor Tomov { 0x3008, 0x02 },
2069cae9722STodor Tomov { 0x3503, 0x00 },
2079cae9722STodor Tomov { 0x5180, 0xff },
2089cae9722STodor Tomov { 0x5181, 0xf2 },
2099cae9722STodor Tomov { 0x5182, 0x00 },
2109cae9722STodor Tomov { 0x5183, 0x14 },
2119cae9722STodor Tomov { 0x5184, 0x25 },
2129cae9722STodor Tomov { 0x5185, 0x24 },
2139cae9722STodor Tomov { 0x5186, 0x09 },
2149cae9722STodor Tomov { 0x5187, 0x09 },
2159cae9722STodor Tomov { 0x5188, 0x0a },
2169cae9722STodor Tomov { 0x5189, 0x75 },
2179cae9722STodor Tomov { 0x518a, 0x52 },
2189cae9722STodor Tomov { 0x518b, 0xea },
2199cae9722STodor Tomov { 0x518c, 0xa8 },
2209cae9722STodor Tomov { 0x518d, 0x42 },
2219cae9722STodor Tomov { 0x518e, 0x38 },
2229cae9722STodor Tomov { 0x518f, 0x56 },
2239cae9722STodor Tomov { 0x5190, 0x42 },
2249cae9722STodor Tomov { 0x5191, 0xf8 },
2259cae9722STodor Tomov { 0x5192, 0x04 },
2269cae9722STodor Tomov { 0x5193, 0x70 },
2279cae9722STodor Tomov { 0x5194, 0xf0 },
2289cae9722STodor Tomov { 0x5195, 0xf0 },
2299cae9722STodor Tomov { 0x5196, 0x03 },
2309cae9722STodor Tomov { 0x5197, 0x01 },
2319cae9722STodor Tomov { 0x5198, 0x04 },
2329cae9722STodor Tomov { 0x5199, 0x12 },
2339cae9722STodor Tomov { 0x519a, 0x04 },
2349cae9722STodor Tomov { 0x519b, 0x00 },
2359cae9722STodor Tomov { 0x519c, 0x06 },
2369cae9722STodor Tomov { 0x519d, 0x82 },
2379cae9722STodor Tomov { 0x519e, 0x38 },
2389cae9722STodor Tomov { 0x5381, 0x1e },
2399cae9722STodor Tomov { 0x5382, 0x5b },
2409cae9722STodor Tomov { 0x5383, 0x08 },
2419cae9722STodor Tomov { 0x5384, 0x0a },
2429cae9722STodor Tomov { 0x5385, 0x7e },
2439cae9722STodor Tomov { 0x5386, 0x88 },
2449cae9722STodor Tomov { 0x5387, 0x7c },
2459cae9722STodor Tomov { 0x5388, 0x6c },
2469cae9722STodor Tomov { 0x5389, 0x10 },
2479cae9722STodor Tomov { 0x538a, 0x01 },
2489cae9722STodor Tomov { 0x538b, 0x98 },
2499cae9722STodor Tomov { 0x5300, 0x08 },
2509cae9722STodor Tomov { 0x5301, 0x30 },
2519cae9722STodor Tomov { 0x5302, 0x10 },
2529cae9722STodor Tomov { 0x5303, 0x00 },
2539cae9722STodor Tomov { 0x5304, 0x08 },
2549cae9722STodor Tomov { 0x5305, 0x30 },
2559cae9722STodor Tomov { 0x5306, 0x08 },
2569cae9722STodor Tomov { 0x5307, 0x16 },
2579cae9722STodor Tomov { 0x5309, 0x08 },
2589cae9722STodor Tomov { 0x530a, 0x30 },
2599cae9722STodor Tomov { 0x530b, 0x04 },
2609cae9722STodor Tomov { 0x530c, 0x06 },
2619cae9722STodor Tomov { 0x5480, 0x01 },
2629cae9722STodor Tomov { 0x5481, 0x08 },
2639cae9722STodor Tomov { 0x5482, 0x14 },
2649cae9722STodor Tomov { 0x5483, 0x28 },
2659cae9722STodor Tomov { 0x5484, 0x51 },
2669cae9722STodor Tomov { 0x5485, 0x65 },
2679cae9722STodor Tomov { 0x5486, 0x71 },
2689cae9722STodor Tomov { 0x5487, 0x7d },
2699cae9722STodor Tomov { 0x5488, 0x87 },
2709cae9722STodor Tomov { 0x5489, 0x91 },
2719cae9722STodor Tomov { 0x548a, 0x9a },
2729cae9722STodor Tomov { 0x548b, 0xaa },
2739cae9722STodor Tomov { 0x548c, 0xb8 },
2749cae9722STodor Tomov { 0x548d, 0xcd },
2759cae9722STodor Tomov { 0x548e, 0xdd },
2769cae9722STodor Tomov { 0x548f, 0xea },
2779cae9722STodor Tomov { 0x5490, 0x1d },
2789cae9722STodor Tomov { 0x5580, 0x02 },
2799cae9722STodor Tomov { 0x5583, 0x40 },
2809cae9722STodor Tomov { 0x5584, 0x10 },
2819cae9722STodor Tomov { 0x5589, 0x10 },
2829cae9722STodor Tomov { 0x558a, 0x00 },
2839cae9722STodor Tomov { 0x558b, 0xf8 },
2849cae9722STodor Tomov { 0x5800, 0x3f },
2859cae9722STodor Tomov { 0x5801, 0x16 },
2869cae9722STodor Tomov { 0x5802, 0x0e },
2879cae9722STodor Tomov { 0x5803, 0x0d },
2889cae9722STodor Tomov { 0x5804, 0x17 },
2899cae9722STodor Tomov { 0x5805, 0x3f },
2909cae9722STodor Tomov { 0x5806, 0x0b },
2919cae9722STodor Tomov { 0x5807, 0x06 },
2929cae9722STodor Tomov { 0x5808, 0x04 },
2939cae9722STodor Tomov { 0x5809, 0x04 },
2949cae9722STodor Tomov { 0x580a, 0x06 },
2959cae9722STodor Tomov { 0x580b, 0x0b },
2969cae9722STodor Tomov { 0x580c, 0x09 },
2979cae9722STodor Tomov { 0x580d, 0x03 },
2989cae9722STodor Tomov { 0x580e, 0x00 },
2999cae9722STodor Tomov { 0x580f, 0x00 },
3009cae9722STodor Tomov { 0x5810, 0x03 },
3019cae9722STodor Tomov { 0x5811, 0x08 },
3029cae9722STodor Tomov { 0x5812, 0x0a },
3039cae9722STodor Tomov { 0x5813, 0x03 },
3049cae9722STodor Tomov { 0x5814, 0x00 },
3059cae9722STodor Tomov { 0x5815, 0x00 },
3069cae9722STodor Tomov { 0x5816, 0x04 },
3079cae9722STodor Tomov { 0x5817, 0x09 },
3089cae9722STodor Tomov { 0x5818, 0x0f },
3099cae9722STodor Tomov { 0x5819, 0x08 },
3109cae9722STodor Tomov { 0x581a, 0x06 },
3119cae9722STodor Tomov { 0x581b, 0x06 },
3129cae9722STodor Tomov { 0x581c, 0x08 },
3139cae9722STodor Tomov { 0x581d, 0x0c },
3149cae9722STodor Tomov { 0x581e, 0x3f },
3159cae9722STodor Tomov { 0x581f, 0x1e },
3169cae9722STodor Tomov { 0x5820, 0x12 },
3179cae9722STodor Tomov { 0x5821, 0x13 },
3189cae9722STodor Tomov { 0x5822, 0x21 },
3199cae9722STodor Tomov { 0x5823, 0x3f },
3209cae9722STodor Tomov { 0x5824, 0x68 },
3219cae9722STodor Tomov { 0x5825, 0x28 },
3229cae9722STodor Tomov { 0x5826, 0x2c },
3239cae9722STodor Tomov { 0x5827, 0x28 },
3249cae9722STodor Tomov { 0x5828, 0x08 },
3259cae9722STodor Tomov { 0x5829, 0x48 },
3269cae9722STodor Tomov { 0x582a, 0x64 },
3279cae9722STodor Tomov { 0x582b, 0x62 },
3289cae9722STodor Tomov { 0x582c, 0x64 },
3299cae9722STodor Tomov { 0x582d, 0x28 },
3309cae9722STodor Tomov { 0x582e, 0x46 },
3319cae9722STodor Tomov { 0x582f, 0x62 },
3329cae9722STodor Tomov { 0x5830, 0x60 },
3339cae9722STodor Tomov { 0x5831, 0x62 },
3349cae9722STodor Tomov { 0x5832, 0x26 },
3359cae9722STodor Tomov { 0x5833, 0x48 },
3369cae9722STodor Tomov { 0x5834, 0x66 },
3379cae9722STodor Tomov { 0x5835, 0x44 },
3389cae9722STodor Tomov { 0x5836, 0x64 },
3399cae9722STodor Tomov { 0x5837, 0x28 },
3409cae9722STodor Tomov { 0x5838, 0x66 },
3419cae9722STodor Tomov { 0x5839, 0x48 },
3429cae9722STodor Tomov { 0x583a, 0x2c },
3439cae9722STodor Tomov { 0x583b, 0x28 },
3449cae9722STodor Tomov { 0x583c, 0x26 },
3459cae9722STodor Tomov { 0x583d, 0xae },
3469cae9722STodor Tomov { 0x5025, 0x00 },
3479cae9722STodor Tomov { 0x3a0f, 0x30 },
3489cae9722STodor Tomov { 0x3a10, 0x28 },
3499cae9722STodor Tomov { 0x3a1b, 0x30 },
3509cae9722STodor Tomov { 0x3a1e, 0x26 },
3519cae9722STodor Tomov { 0x3a11, 0x60 },
3529cae9722STodor Tomov { 0x3a1f, 0x14 },
3539cae9722STodor Tomov { 0x0601, 0x02 },
3549cae9722STodor Tomov { 0x3008, 0x42 },
355092e8eb9SEzequiel Garcia { 0x3008, 0x02 },
356092e8eb9SEzequiel Garcia { OV5645_IO_MIPI_CTRL00, 0x40 },
357092e8eb9SEzequiel Garcia { OV5645_MIPI_CTRL00, 0x24 },
358092e8eb9SEzequiel Garcia { OV5645_PAD_OUTPUT00, 0x70 }
3599cae9722STodor Tomov };
3609cae9722STodor Tomov
3619cae9722STodor Tomov static const struct reg_value ov5645_setting_sxga[] = {
3629cae9722STodor Tomov { 0x3612, 0xa9 },
3639cae9722STodor Tomov { 0x3614, 0x50 },
3649cae9722STodor Tomov { 0x3618, 0x00 },
3659cae9722STodor Tomov { 0x3034, 0x18 },
3669cae9722STodor Tomov { 0x3035, 0x21 },
3679cae9722STodor Tomov { 0x3036, 0x70 },
3689cae9722STodor Tomov { 0x3600, 0x09 },
3699cae9722STodor Tomov { 0x3601, 0x43 },
3709cae9722STodor Tomov { 0x3708, 0x66 },
3719cae9722STodor Tomov { 0x370c, 0xc3 },
3729cae9722STodor Tomov { 0x3800, 0x00 },
3739cae9722STodor Tomov { 0x3801, 0x00 },
3749cae9722STodor Tomov { 0x3802, 0x00 },
3759cae9722STodor Tomov { 0x3803, 0x06 },
3769cae9722STodor Tomov { 0x3804, 0x0a },
3779cae9722STodor Tomov { 0x3805, 0x3f },
3789cae9722STodor Tomov { 0x3806, 0x07 },
3799cae9722STodor Tomov { 0x3807, 0x9d },
3809cae9722STodor Tomov { 0x3808, 0x05 },
3819cae9722STodor Tomov { 0x3809, 0x00 },
3829cae9722STodor Tomov { 0x380a, 0x03 },
3839cae9722STodor Tomov { 0x380b, 0xc0 },
3849cae9722STodor Tomov { 0x380c, 0x07 },
3859cae9722STodor Tomov { 0x380d, 0x68 },
3869cae9722STodor Tomov { 0x380e, 0x03 },
3879cae9722STodor Tomov { 0x380f, 0xd8 },
3889cae9722STodor Tomov { 0x3813, 0x06 },
3899cae9722STodor Tomov { 0x3814, 0x31 },
3909cae9722STodor Tomov { 0x3815, 0x31 },
3919cae9722STodor Tomov { 0x3820, 0x47 },
3929cae9722STodor Tomov { 0x3a02, 0x03 },
3939cae9722STodor Tomov { 0x3a03, 0xd8 },
3949cae9722STodor Tomov { 0x3a08, 0x01 },
3959cae9722STodor Tomov { 0x3a09, 0xf8 },
3969cae9722STodor Tomov { 0x3a0a, 0x01 },
3979cae9722STodor Tomov { 0x3a0b, 0xa4 },
3989cae9722STodor Tomov { 0x3a0e, 0x02 },
3999cae9722STodor Tomov { 0x3a0d, 0x02 },
4009cae9722STodor Tomov { 0x3a14, 0x03 },
4019cae9722STodor Tomov { 0x3a15, 0xd8 },
4029cae9722STodor Tomov { 0x3a18, 0x00 },
4039cae9722STodor Tomov { 0x4004, 0x02 },
4049cae9722STodor Tomov { 0x4005, 0x18 },
4059cae9722STodor Tomov { 0x4300, 0x32 },
4069cae9722STodor Tomov { 0x4202, 0x00 }
4079cae9722STodor Tomov };
4089cae9722STodor Tomov
4099cae9722STodor Tomov static const struct reg_value ov5645_setting_1080p[] = {
4109cae9722STodor Tomov { 0x3612, 0xab },
4119cae9722STodor Tomov { 0x3614, 0x50 },
4129cae9722STodor Tomov { 0x3618, 0x04 },
4139cae9722STodor Tomov { 0x3034, 0x18 },
4149cae9722STodor Tomov { 0x3035, 0x11 },
4159cae9722STodor Tomov { 0x3036, 0x54 },
4169cae9722STodor Tomov { 0x3600, 0x08 },
4179cae9722STodor Tomov { 0x3601, 0x33 },
4189cae9722STodor Tomov { 0x3708, 0x63 },
4199cae9722STodor Tomov { 0x370c, 0xc0 },
4209cae9722STodor Tomov { 0x3800, 0x01 },
4219cae9722STodor Tomov { 0x3801, 0x50 },
4229cae9722STodor Tomov { 0x3802, 0x01 },
4239cae9722STodor Tomov { 0x3803, 0xb2 },
4249cae9722STodor Tomov { 0x3804, 0x08 },
4259cae9722STodor Tomov { 0x3805, 0xef },
4269cae9722STodor Tomov { 0x3806, 0x05 },
4279cae9722STodor Tomov { 0x3807, 0xf1 },
4289cae9722STodor Tomov { 0x3808, 0x07 },
4299cae9722STodor Tomov { 0x3809, 0x80 },
4309cae9722STodor Tomov { 0x380a, 0x04 },
4319cae9722STodor Tomov { 0x380b, 0x38 },
4329cae9722STodor Tomov { 0x380c, 0x09 },
4339cae9722STodor Tomov { 0x380d, 0xc4 },
4349cae9722STodor Tomov { 0x380e, 0x04 },
4359cae9722STodor Tomov { 0x380f, 0x60 },
4369cae9722STodor Tomov { 0x3813, 0x04 },
4379cae9722STodor Tomov { 0x3814, 0x11 },
4389cae9722STodor Tomov { 0x3815, 0x11 },
4399cae9722STodor Tomov { 0x3820, 0x47 },
4409cae9722STodor Tomov { 0x4514, 0x88 },
4419cae9722STodor Tomov { 0x3a02, 0x04 },
4429cae9722STodor Tomov { 0x3a03, 0x60 },
4439cae9722STodor Tomov { 0x3a08, 0x01 },
4449cae9722STodor Tomov { 0x3a09, 0x50 },
4459cae9722STodor Tomov { 0x3a0a, 0x01 },
4469cae9722STodor Tomov { 0x3a0b, 0x18 },
4479cae9722STodor Tomov { 0x3a0e, 0x03 },
4489cae9722STodor Tomov { 0x3a0d, 0x04 },
4499cae9722STodor Tomov { 0x3a14, 0x04 },
4509cae9722STodor Tomov { 0x3a15, 0x60 },
4519cae9722STodor Tomov { 0x3a18, 0x00 },
4529cae9722STodor Tomov { 0x4004, 0x06 },
4539cae9722STodor Tomov { 0x4005, 0x18 },
4549cae9722STodor Tomov { 0x4300, 0x32 },
4559cae9722STodor Tomov { 0x4202, 0x00 },
4569cae9722STodor Tomov { 0x4837, 0x0b }
4579cae9722STodor Tomov };
4589cae9722STodor Tomov
4599cae9722STodor Tomov static const struct reg_value ov5645_setting_full[] = {
4609cae9722STodor Tomov { 0x3612, 0xab },
4619cae9722STodor Tomov { 0x3614, 0x50 },
4629cae9722STodor Tomov { 0x3618, 0x04 },
4639cae9722STodor Tomov { 0x3034, 0x18 },
4649cae9722STodor Tomov { 0x3035, 0x11 },
4659cae9722STodor Tomov { 0x3036, 0x54 },
4669cae9722STodor Tomov { 0x3600, 0x08 },
4679cae9722STodor Tomov { 0x3601, 0x33 },
4689cae9722STodor Tomov { 0x3708, 0x63 },
4699cae9722STodor Tomov { 0x370c, 0xc0 },
4709cae9722STodor Tomov { 0x3800, 0x00 },
4719cae9722STodor Tomov { 0x3801, 0x00 },
4729cae9722STodor Tomov { 0x3802, 0x00 },
4739cae9722STodor Tomov { 0x3803, 0x00 },
4749cae9722STodor Tomov { 0x3804, 0x0a },
4759cae9722STodor Tomov { 0x3805, 0x3f },
4769cae9722STodor Tomov { 0x3806, 0x07 },
4779cae9722STodor Tomov { 0x3807, 0x9f },
4789cae9722STodor Tomov { 0x3808, 0x0a },
4799cae9722STodor Tomov { 0x3809, 0x20 },
4809cae9722STodor Tomov { 0x380a, 0x07 },
4819cae9722STodor Tomov { 0x380b, 0x98 },
4829cae9722STodor Tomov { 0x380c, 0x0b },
4839cae9722STodor Tomov { 0x380d, 0x1c },
4849cae9722STodor Tomov { 0x380e, 0x07 },
4859cae9722STodor Tomov { 0x380f, 0xb0 },
4869cae9722STodor Tomov { 0x3813, 0x06 },
4879cae9722STodor Tomov { 0x3814, 0x11 },
4889cae9722STodor Tomov { 0x3815, 0x11 },
4899cae9722STodor Tomov { 0x3820, 0x47 },
4909cae9722STodor Tomov { 0x4514, 0x88 },
4919cae9722STodor Tomov { 0x3a02, 0x07 },
4929cae9722STodor Tomov { 0x3a03, 0xb0 },
4939cae9722STodor Tomov { 0x3a08, 0x01 },
4949cae9722STodor Tomov { 0x3a09, 0x27 },
4959cae9722STodor Tomov { 0x3a0a, 0x00 },
4969cae9722STodor Tomov { 0x3a0b, 0xf6 },
4979cae9722STodor Tomov { 0x3a0e, 0x06 },
4989cae9722STodor Tomov { 0x3a0d, 0x08 },
4999cae9722STodor Tomov { 0x3a14, 0x07 },
5009cae9722STodor Tomov { 0x3a15, 0xb0 },
5019cae9722STodor Tomov { 0x3a18, 0x01 },
5029cae9722STodor Tomov { 0x4004, 0x06 },
5039cae9722STodor Tomov { 0x4005, 0x18 },
5049cae9722STodor Tomov { 0x4300, 0x32 },
5059cae9722STodor Tomov { 0x4837, 0x0b },
5069cae9722STodor Tomov { 0x4202, 0x00 }
5079cae9722STodor Tomov };
5089cae9722STodor Tomov
509e72e6cbeSTodor Tomov static const s64 link_freq[] = {
5104adb0a04STodor Tomov 224000000,
5114adb0a04STodor Tomov 336000000
512e72e6cbeSTodor Tomov };
513e72e6cbeSTodor Tomov
5149cae9722STodor Tomov static const struct ov5645_mode_info ov5645_mode_info_data[] = {
5159cae9722STodor Tomov {
5169cae9722STodor Tomov .width = 1280,
5179cae9722STodor Tomov .height = 960,
5189cae9722STodor Tomov .data = ov5645_setting_sxga,
5191c2177fdSTodor Tomov .data_size = ARRAY_SIZE(ov5645_setting_sxga),
5204adb0a04STodor Tomov .pixel_clock = 112000000,
521e72e6cbeSTodor Tomov .link_freq = 0 /* an index in link_freq[] */
5229cae9722STodor Tomov },
5239cae9722STodor Tomov {
5249cae9722STodor Tomov .width = 1920,
5259cae9722STodor Tomov .height = 1080,
5269cae9722STodor Tomov .data = ov5645_setting_1080p,
5271c2177fdSTodor Tomov .data_size = ARRAY_SIZE(ov5645_setting_1080p),
5284adb0a04STodor Tomov .pixel_clock = 168000000,
529e72e6cbeSTodor Tomov .link_freq = 1 /* an index in link_freq[] */
5309cae9722STodor Tomov },
5319cae9722STodor Tomov {
5329cae9722STodor Tomov .width = 2592,
5339cae9722STodor Tomov .height = 1944,
5349cae9722STodor Tomov .data = ov5645_setting_full,
5351c2177fdSTodor Tomov .data_size = ARRAY_SIZE(ov5645_setting_full),
5364adb0a04STodor Tomov .pixel_clock = 168000000,
537e72e6cbeSTodor Tomov .link_freq = 1 /* an index in link_freq[] */
5389cae9722STodor Tomov },
5399cae9722STodor Tomov };
5409cae9722STodor Tomov
ov5645_write_reg(struct ov5645 * ov5645,u16 reg,u8 val)5419cae9722STodor Tomov static int ov5645_write_reg(struct ov5645 *ov5645, u16 reg, u8 val)
5429cae9722STodor Tomov {
5439cae9722STodor Tomov u8 regbuf[3];
5449cae9722STodor Tomov int ret;
5459cae9722STodor Tomov
5469cae9722STodor Tomov regbuf[0] = reg >> 8;
5479cae9722STodor Tomov regbuf[1] = reg & 0xff;
5489cae9722STodor Tomov regbuf[2] = val;
5499cae9722STodor Tomov
5509cae9722STodor Tomov ret = i2c_master_send(ov5645->i2c_client, regbuf, 3);
55170648780STodor Tomov if (ret < 0) {
5529cae9722STodor Tomov dev_err(ov5645->dev, "%s: write reg error %d: reg=%x, val=%x\n",
5539cae9722STodor Tomov __func__, ret, reg, val);
5549cae9722STodor Tomov return ret;
5559cae9722STodor Tomov }
5569cae9722STodor Tomov
55770648780STodor Tomov return 0;
55870648780STodor Tomov }
55970648780STodor Tomov
ov5645_read_reg(struct ov5645 * ov5645,u16 reg,u8 * val)5609cae9722STodor Tomov static int ov5645_read_reg(struct ov5645 *ov5645, u16 reg, u8 *val)
5619cae9722STodor Tomov {
5629cae9722STodor Tomov u8 regbuf[2];
5639cae9722STodor Tomov int ret;
5649cae9722STodor Tomov
5659cae9722STodor Tomov regbuf[0] = reg >> 8;
5669cae9722STodor Tomov regbuf[1] = reg & 0xff;
5679cae9722STodor Tomov
5689cae9722STodor Tomov ret = i2c_master_send(ov5645->i2c_client, regbuf, 2);
5699cae9722STodor Tomov if (ret < 0) {
5709cae9722STodor Tomov dev_err(ov5645->dev, "%s: write reg error %d: reg=%x\n",
5719cae9722STodor Tomov __func__, ret, reg);
5729cae9722STodor Tomov return ret;
5739cae9722STodor Tomov }
5749cae9722STodor Tomov
5759cae9722STodor Tomov ret = i2c_master_recv(ov5645->i2c_client, val, 1);
5769cae9722STodor Tomov if (ret < 0) {
5779cae9722STodor Tomov dev_err(ov5645->dev, "%s: read reg error %d: reg=%x\n",
5789cae9722STodor Tomov __func__, ret, reg);
5799cae9722STodor Tomov return ret;
5809cae9722STodor Tomov }
5819cae9722STodor Tomov
5829cae9722STodor Tomov return 0;
5839cae9722STodor Tomov }
5849cae9722STodor Tomov
ov5645_set_aec_mode(struct ov5645 * ov5645,u32 mode)5859cae9722STodor Tomov static int ov5645_set_aec_mode(struct ov5645 *ov5645, u32 mode)
5869cae9722STodor Tomov {
5879cae9722STodor Tomov u8 val = ov5645->aec_pk_manual;
5889cae9722STodor Tomov int ret;
5899cae9722STodor Tomov
5909cae9722STodor Tomov if (mode == V4L2_EXPOSURE_AUTO)
5919cae9722STodor Tomov val &= ~OV5645_AEC_MANUAL_ENABLE;
5929cae9722STodor Tomov else /* V4L2_EXPOSURE_MANUAL */
5939cae9722STodor Tomov val |= OV5645_AEC_MANUAL_ENABLE;
5949cae9722STodor Tomov
5959cae9722STodor Tomov ret = ov5645_write_reg(ov5645, OV5645_AEC_PK_MANUAL, val);
5969cae9722STodor Tomov if (!ret)
5979cae9722STodor Tomov ov5645->aec_pk_manual = val;
5989cae9722STodor Tomov
5999cae9722STodor Tomov return ret;
6009cae9722STodor Tomov }
6019cae9722STodor Tomov
ov5645_set_agc_mode(struct ov5645 * ov5645,u32 enable)6029cae9722STodor Tomov static int ov5645_set_agc_mode(struct ov5645 *ov5645, u32 enable)
6039cae9722STodor Tomov {
6049cae9722STodor Tomov u8 val = ov5645->aec_pk_manual;
6059cae9722STodor Tomov int ret;
6069cae9722STodor Tomov
6079cae9722STodor Tomov if (enable)
6089cae9722STodor Tomov val &= ~OV5645_AGC_MANUAL_ENABLE;
6099cae9722STodor Tomov else
6109cae9722STodor Tomov val |= OV5645_AGC_MANUAL_ENABLE;
6119cae9722STodor Tomov
6129cae9722STodor Tomov ret = ov5645_write_reg(ov5645, OV5645_AEC_PK_MANUAL, val);
6139cae9722STodor Tomov if (!ret)
6149cae9722STodor Tomov ov5645->aec_pk_manual = val;
6159cae9722STodor Tomov
6169cae9722STodor Tomov return ret;
6179cae9722STodor Tomov }
6189cae9722STodor Tomov
ov5645_set_register_array(struct ov5645 * ov5645,const struct reg_value * settings,unsigned int num_settings)6199cae9722STodor Tomov static int ov5645_set_register_array(struct ov5645 *ov5645,
6209cae9722STodor Tomov const struct reg_value *settings,
6219cae9722STodor Tomov unsigned int num_settings)
6229cae9722STodor Tomov {
6239cae9722STodor Tomov unsigned int i;
6249cae9722STodor Tomov int ret;
6259cae9722STodor Tomov
6269cae9722STodor Tomov for (i = 0; i < num_settings; ++i, ++settings) {
6279cae9722STodor Tomov ret = ov5645_write_reg(ov5645, settings->reg, settings->val);
6289cae9722STodor Tomov if (ret < 0)
6299cae9722STodor Tomov return ret;
6309cae9722STodor Tomov }
6319cae9722STodor Tomov
6329cae9722STodor Tomov return 0;
6339cae9722STodor Tomov }
6349cae9722STodor Tomov
ov5645_set_power_off(struct device * dev)635b5045039SLad Prabhakar static int ov5645_set_power_off(struct device *dev)
6369cae9722STodor Tomov {
637b5045039SLad Prabhakar struct v4l2_subdev *sd = dev_get_drvdata(dev);
638b5045039SLad Prabhakar struct ov5645 *ov5645 = to_ov5645(sd);
639b5045039SLad Prabhakar
640b5045039SLad Prabhakar ov5645_write_reg(ov5645, OV5645_IO_MIPI_CTRL00, 0x58);
641b5045039SLad Prabhakar gpiod_set_value_cansleep(ov5645->rst_gpio, 1);
642b5045039SLad Prabhakar gpiod_set_value_cansleep(ov5645->enable_gpio, 0);
643b5045039SLad Prabhakar clk_disable_unprepare(ov5645->xclk);
644b5045039SLad Prabhakar regulator_bulk_disable(OV5645_NUM_SUPPLIES, ov5645->supplies);
645b5045039SLad Prabhakar
646b5045039SLad Prabhakar return 0;
647b5045039SLad Prabhakar }
648b5045039SLad Prabhakar
ov5645_set_power_on(struct device * dev)649b5045039SLad Prabhakar static int ov5645_set_power_on(struct device *dev)
650b5045039SLad Prabhakar {
651b5045039SLad Prabhakar struct v4l2_subdev *sd = dev_get_drvdata(dev);
652b5045039SLad Prabhakar struct ov5645 *ov5645 = to_ov5645(sd);
6539cae9722STodor Tomov int ret;
6549cae9722STodor Tomov
6554962bb38SFabio Estevam ret = regulator_bulk_enable(OV5645_NUM_SUPPLIES, ov5645->supplies);
6564962bb38SFabio Estevam if (ret < 0)
6579cae9722STodor Tomov return ret;
6589cae9722STodor Tomov
6599cae9722STodor Tomov ret = clk_prepare_enable(ov5645->xclk);
6609cae9722STodor Tomov if (ret < 0) {
6619cae9722STodor Tomov dev_err(ov5645->dev, "clk prepare enable failed\n");
6624962bb38SFabio Estevam regulator_bulk_disable(OV5645_NUM_SUPPLIES, ov5645->supplies);
6639cae9722STodor Tomov return ret;
6649cae9722STodor Tomov }
6659cae9722STodor Tomov
6669cae9722STodor Tomov usleep_range(5000, 15000);
6679cae9722STodor Tomov gpiod_set_value_cansleep(ov5645->enable_gpio, 1);
6689cae9722STodor Tomov
6699cae9722STodor Tomov usleep_range(1000, 2000);
6709cae9722STodor Tomov gpiod_set_value_cansleep(ov5645->rst_gpio, 0);
6719cae9722STodor Tomov
6729cae9722STodor Tomov msleep(20);
6739cae9722STodor Tomov
674b5045039SLad Prabhakar ret = ov5645_set_register_array(ov5645, ov5645_global_init_setting,
6759cae9722STodor Tomov ARRAY_SIZE(ov5645_global_init_setting));
6769cae9722STodor Tomov if (ret < 0) {
677b5045039SLad Prabhakar dev_err(ov5645->dev, "could not set init registers\n");
6789cae9722STodor Tomov goto exit;
6799cae9722STodor Tomov }
6809cae9722STodor Tomov
681092e8eb9SEzequiel Garcia usleep_range(500, 1000);
6829cae9722STodor Tomov
683b5045039SLad Prabhakar return 0;
6849cae9722STodor Tomov
6859cae9722STodor Tomov exit:
686b5045039SLad Prabhakar ov5645_set_power_off(dev);
6879cae9722STodor Tomov return ret;
6889cae9722STodor Tomov }
6899cae9722STodor Tomov
ov5645_set_saturation(struct ov5645 * ov5645,s32 value)6909cae9722STodor Tomov static int ov5645_set_saturation(struct ov5645 *ov5645, s32 value)
6919cae9722STodor Tomov {
6929cae9722STodor Tomov u32 reg_value = (value * 0x10) + 0x40;
6939cae9722STodor Tomov int ret;
6949cae9722STodor Tomov
6959cae9722STodor Tomov ret = ov5645_write_reg(ov5645, OV5645_SDE_SAT_U, reg_value);
6969cae9722STodor Tomov if (ret < 0)
6979cae9722STodor Tomov return ret;
6989cae9722STodor Tomov
6999cae9722STodor Tomov return ov5645_write_reg(ov5645, OV5645_SDE_SAT_V, reg_value);
7009cae9722STodor Tomov }
7019cae9722STodor Tomov
ov5645_set_hflip(struct ov5645 * ov5645,s32 value)7029cae9722STodor Tomov static int ov5645_set_hflip(struct ov5645 *ov5645, s32 value)
7039cae9722STodor Tomov {
7049cae9722STodor Tomov u8 val = ov5645->timing_tc_reg21;
7059cae9722STodor Tomov int ret;
7069cae9722STodor Tomov
7079cae9722STodor Tomov if (value == 0)
7089cae9722STodor Tomov val &= ~(OV5645_SENSOR_MIRROR);
7099cae9722STodor Tomov else
7109cae9722STodor Tomov val |= (OV5645_SENSOR_MIRROR);
7119cae9722STodor Tomov
7129cae9722STodor Tomov ret = ov5645_write_reg(ov5645, OV5645_TIMING_TC_REG21, val);
7139cae9722STodor Tomov if (!ret)
7149cae9722STodor Tomov ov5645->timing_tc_reg21 = val;
7159cae9722STodor Tomov
7169cae9722STodor Tomov return ret;
7179cae9722STodor Tomov }
7189cae9722STodor Tomov
ov5645_set_vflip(struct ov5645 * ov5645,s32 value)7199cae9722STodor Tomov static int ov5645_set_vflip(struct ov5645 *ov5645, s32 value)
7209cae9722STodor Tomov {
7219cae9722STodor Tomov u8 val = ov5645->timing_tc_reg20;
7229cae9722STodor Tomov int ret;
7239cae9722STodor Tomov
7249cae9722STodor Tomov if (value == 0)
7259cae9722STodor Tomov val |= (OV5645_SENSOR_VFLIP | OV5645_ISP_VFLIP);
7269cae9722STodor Tomov else
7279cae9722STodor Tomov val &= ~(OV5645_SENSOR_VFLIP | OV5645_ISP_VFLIP);
7289cae9722STodor Tomov
7299cae9722STodor Tomov ret = ov5645_write_reg(ov5645, OV5645_TIMING_TC_REG20, val);
7309cae9722STodor Tomov if (!ret)
7319cae9722STodor Tomov ov5645->timing_tc_reg20 = val;
7329cae9722STodor Tomov
7339cae9722STodor Tomov return ret;
7349cae9722STodor Tomov }
7359cae9722STodor Tomov
ov5645_set_test_pattern(struct ov5645 * ov5645,s32 value)7369cae9722STodor Tomov static int ov5645_set_test_pattern(struct ov5645 *ov5645, s32 value)
7379cae9722STodor Tomov {
7389cae9722STodor Tomov u8 val = 0;
7399cae9722STodor Tomov
7409cae9722STodor Tomov if (value) {
7419cae9722STodor Tomov val = OV5645_SET_TEST_PATTERN(value - 1);
7429cae9722STodor Tomov val |= OV5645_TEST_PATTERN_ENABLE;
7439cae9722STodor Tomov }
7449cae9722STodor Tomov
7459cae9722STodor Tomov return ov5645_write_reg(ov5645, OV5645_PRE_ISP_TEST_SETTING_1, val);
7469cae9722STodor Tomov }
7479cae9722STodor Tomov
7489cae9722STodor Tomov static const char * const ov5645_test_pattern_menu[] = {
7499cae9722STodor Tomov "Disabled",
7509cae9722STodor Tomov "Vertical Color Bars",
7519cae9722STodor Tomov "Pseudo-Random Data",
7529cae9722STodor Tomov "Color Square",
7539cae9722STodor Tomov "Black Image",
7549cae9722STodor Tomov };
7559cae9722STodor Tomov
ov5645_set_awb(struct ov5645 * ov5645,s32 enable_auto)7569cae9722STodor Tomov static int ov5645_set_awb(struct ov5645 *ov5645, s32 enable_auto)
7579cae9722STodor Tomov {
7589cae9722STodor Tomov u8 val = 0;
7599cae9722STodor Tomov
7609cae9722STodor Tomov if (!enable_auto)
7619cae9722STodor Tomov val = OV5645_AWB_MANUAL_ENABLE;
7629cae9722STodor Tomov
7639cae9722STodor Tomov return ov5645_write_reg(ov5645, OV5645_AWB_MANUAL_CONTROL, val);
7649cae9722STodor Tomov }
7659cae9722STodor Tomov
ov5645_s_ctrl(struct v4l2_ctrl * ctrl)7669cae9722STodor Tomov static int ov5645_s_ctrl(struct v4l2_ctrl *ctrl)
7679cae9722STodor Tomov {
7689cae9722STodor Tomov struct ov5645 *ov5645 = container_of(ctrl->handler,
7699cae9722STodor Tomov struct ov5645, ctrls);
7709cae9722STodor Tomov int ret;
7719cae9722STodor Tomov
7729cae9722STodor Tomov mutex_lock(&ov5645->power_lock);
773b5045039SLad Prabhakar if (!pm_runtime_get_if_in_use(ov5645->dev)) {
7749cae9722STodor Tomov mutex_unlock(&ov5645->power_lock);
7759cae9722STodor Tomov return 0;
7769cae9722STodor Tomov }
7779cae9722STodor Tomov
7789cae9722STodor Tomov switch (ctrl->id) {
7799cae9722STodor Tomov case V4L2_CID_SATURATION:
7809cae9722STodor Tomov ret = ov5645_set_saturation(ov5645, ctrl->val);
7819cae9722STodor Tomov break;
7829cae9722STodor Tomov case V4L2_CID_AUTO_WHITE_BALANCE:
7839cae9722STodor Tomov ret = ov5645_set_awb(ov5645, ctrl->val);
7849cae9722STodor Tomov break;
7859cae9722STodor Tomov case V4L2_CID_AUTOGAIN:
7869cae9722STodor Tomov ret = ov5645_set_agc_mode(ov5645, ctrl->val);
7879cae9722STodor Tomov break;
7889cae9722STodor Tomov case V4L2_CID_EXPOSURE_AUTO:
7899cae9722STodor Tomov ret = ov5645_set_aec_mode(ov5645, ctrl->val);
7909cae9722STodor Tomov break;
7919cae9722STodor Tomov case V4L2_CID_TEST_PATTERN:
7929cae9722STodor Tomov ret = ov5645_set_test_pattern(ov5645, ctrl->val);
7939cae9722STodor Tomov break;
7949cae9722STodor Tomov case V4L2_CID_HFLIP:
7959cae9722STodor Tomov ret = ov5645_set_hflip(ov5645, ctrl->val);
7969cae9722STodor Tomov break;
7979cae9722STodor Tomov case V4L2_CID_VFLIP:
7989cae9722STodor Tomov ret = ov5645_set_vflip(ov5645, ctrl->val);
7999cae9722STodor Tomov break;
8009cae9722STodor Tomov default:
8019cae9722STodor Tomov ret = -EINVAL;
8029cae9722STodor Tomov break;
8039cae9722STodor Tomov }
8049cae9722STodor Tomov
805b5045039SLad Prabhakar pm_runtime_mark_last_busy(ov5645->dev);
806b5045039SLad Prabhakar pm_runtime_put_autosuspend(ov5645->dev);
8079cae9722STodor Tomov mutex_unlock(&ov5645->power_lock);
8089cae9722STodor Tomov
8099cae9722STodor Tomov return ret;
8109cae9722STodor Tomov }
8119cae9722STodor Tomov
812d148b85eSJulia Lawall static const struct v4l2_ctrl_ops ov5645_ctrl_ops = {
8139cae9722STodor Tomov .s_ctrl = ov5645_s_ctrl,
8149cae9722STodor Tomov };
8159cae9722STodor Tomov
ov5645_enum_mbus_code(struct v4l2_subdev * sd,struct v4l2_subdev_state * sd_state,struct v4l2_subdev_mbus_code_enum * code)8169cae9722STodor Tomov static int ov5645_enum_mbus_code(struct v4l2_subdev *sd,
8170d346d2aSTomi Valkeinen struct v4l2_subdev_state *sd_state,
8189cae9722STodor Tomov struct v4l2_subdev_mbus_code_enum *code)
8199cae9722STodor Tomov {
8209cae9722STodor Tomov if (code->index > 0)
8219cae9722STodor Tomov return -EINVAL;
8229cae9722STodor Tomov
823ba449bb5SJacopo Mondi code->code = MEDIA_BUS_FMT_UYVY8_1X16;
8249cae9722STodor Tomov
8259cae9722STodor Tomov return 0;
8269cae9722STodor Tomov }
8279cae9722STodor Tomov
ov5645_enum_frame_size(struct v4l2_subdev * subdev,struct v4l2_subdev_state * sd_state,struct v4l2_subdev_frame_size_enum * fse)8289cae9722STodor Tomov static int ov5645_enum_frame_size(struct v4l2_subdev *subdev,
8290d346d2aSTomi Valkeinen struct v4l2_subdev_state *sd_state,
8309cae9722STodor Tomov struct v4l2_subdev_frame_size_enum *fse)
8319cae9722STodor Tomov {
832ba449bb5SJacopo Mondi if (fse->code != MEDIA_BUS_FMT_UYVY8_1X16)
8339cae9722STodor Tomov return -EINVAL;
8349cae9722STodor Tomov
8359cae9722STodor Tomov if (fse->index >= ARRAY_SIZE(ov5645_mode_info_data))
8369cae9722STodor Tomov return -EINVAL;
8379cae9722STodor Tomov
8389cae9722STodor Tomov fse->min_width = ov5645_mode_info_data[fse->index].width;
8399cae9722STodor Tomov fse->max_width = ov5645_mode_info_data[fse->index].width;
8409cae9722STodor Tomov fse->min_height = ov5645_mode_info_data[fse->index].height;
8419cae9722STodor Tomov fse->max_height = ov5645_mode_info_data[fse->index].height;
8429cae9722STodor Tomov
8439cae9722STodor Tomov return 0;
8449cae9722STodor Tomov }
8459cae9722STodor Tomov
8469cae9722STodor Tomov static struct v4l2_mbus_framefmt *
__ov5645_get_pad_format(struct ov5645 * ov5645,struct v4l2_subdev_state * sd_state,unsigned int pad,enum v4l2_subdev_format_whence which)8479cae9722STodor Tomov __ov5645_get_pad_format(struct ov5645 *ov5645,
8480d346d2aSTomi Valkeinen struct v4l2_subdev_state *sd_state,
8499cae9722STodor Tomov unsigned int pad,
8509cae9722STodor Tomov enum v4l2_subdev_format_whence which)
8519cae9722STodor Tomov {
8529cae9722STodor Tomov switch (which) {
8539cae9722STodor Tomov case V4L2_SUBDEV_FORMAT_TRY:
8540d346d2aSTomi Valkeinen return v4l2_subdev_get_try_format(&ov5645->sd, sd_state, pad);
8559cae9722STodor Tomov case V4L2_SUBDEV_FORMAT_ACTIVE:
8569cae9722STodor Tomov return &ov5645->fmt;
8579cae9722STodor Tomov default:
8589cae9722STodor Tomov return NULL;
8599cae9722STodor Tomov }
8609cae9722STodor Tomov }
8619cae9722STodor Tomov
ov5645_get_format(struct v4l2_subdev * sd,struct v4l2_subdev_state * sd_state,struct v4l2_subdev_format * format)8629cae9722STodor Tomov static int ov5645_get_format(struct v4l2_subdev *sd,
8630d346d2aSTomi Valkeinen struct v4l2_subdev_state *sd_state,
8649cae9722STodor Tomov struct v4l2_subdev_format *format)
8659cae9722STodor Tomov {
8669cae9722STodor Tomov struct ov5645 *ov5645 = to_ov5645(sd);
8679cae9722STodor Tomov
8680d346d2aSTomi Valkeinen format->format = *__ov5645_get_pad_format(ov5645, sd_state,
8690d346d2aSTomi Valkeinen format->pad,
8709cae9722STodor Tomov format->which);
8719cae9722STodor Tomov return 0;
8729cae9722STodor Tomov }
8739cae9722STodor Tomov
8749cae9722STodor Tomov static struct v4l2_rect *
__ov5645_get_pad_crop(struct ov5645 * ov5645,struct v4l2_subdev_state * sd_state,unsigned int pad,enum v4l2_subdev_format_whence which)8750d346d2aSTomi Valkeinen __ov5645_get_pad_crop(struct ov5645 *ov5645,
8760d346d2aSTomi Valkeinen struct v4l2_subdev_state *sd_state,
8779cae9722STodor Tomov unsigned int pad, enum v4l2_subdev_format_whence which)
8789cae9722STodor Tomov {
8799cae9722STodor Tomov switch (which) {
8809cae9722STodor Tomov case V4L2_SUBDEV_FORMAT_TRY:
8810d346d2aSTomi Valkeinen return v4l2_subdev_get_try_crop(&ov5645->sd, sd_state, pad);
8829cae9722STodor Tomov case V4L2_SUBDEV_FORMAT_ACTIVE:
8839cae9722STodor Tomov return &ov5645->crop;
8849cae9722STodor Tomov default:
8859cae9722STodor Tomov return NULL;
8869cae9722STodor Tomov }
8879cae9722STodor Tomov }
8889cae9722STodor Tomov
ov5645_set_format(struct v4l2_subdev * sd,struct v4l2_subdev_state * sd_state,struct v4l2_subdev_format * format)8899cae9722STodor Tomov static int ov5645_set_format(struct v4l2_subdev *sd,
8900d346d2aSTomi Valkeinen struct v4l2_subdev_state *sd_state,
8919cae9722STodor Tomov struct v4l2_subdev_format *format)
8929cae9722STodor Tomov {
8939cae9722STodor Tomov struct ov5645 *ov5645 = to_ov5645(sd);
8949cae9722STodor Tomov struct v4l2_mbus_framefmt *__format;
8959cae9722STodor Tomov struct v4l2_rect *__crop;
8969cae9722STodor Tomov const struct ov5645_mode_info *new_mode;
8971c2177fdSTodor Tomov int ret;
8989cae9722STodor Tomov
8990d346d2aSTomi Valkeinen __crop = __ov5645_get_pad_crop(ov5645, sd_state, format->pad,
9009cae9722STodor Tomov format->which);
9019cae9722STodor Tomov
9025b81c53cSTodor Tomov new_mode = v4l2_find_nearest_size(ov5645_mode_info_data,
9035b81c53cSTodor Tomov ARRAY_SIZE(ov5645_mode_info_data),
9045b81c53cSTodor Tomov width, height,
9055b81c53cSTodor Tomov format->format.width, format->format.height);
9065b81c53cSTodor Tomov
9079cae9722STodor Tomov __crop->width = new_mode->width;
9089cae9722STodor Tomov __crop->height = new_mode->height;
9099cae9722STodor Tomov
9101c2177fdSTodor Tomov if (format->which == V4L2_SUBDEV_FORMAT_ACTIVE) {
9111c2177fdSTodor Tomov ret = v4l2_ctrl_s_ctrl_int64(ov5645->pixel_clock,
9121c2177fdSTodor Tomov new_mode->pixel_clock);
9131c2177fdSTodor Tomov if (ret < 0)
9141c2177fdSTodor Tomov return ret;
9151c2177fdSTodor Tomov
916e72e6cbeSTodor Tomov ret = v4l2_ctrl_s_ctrl(ov5645->link_freq,
917e72e6cbeSTodor Tomov new_mode->link_freq);
918e72e6cbeSTodor Tomov if (ret < 0)
919e72e6cbeSTodor Tomov return ret;
920e72e6cbeSTodor Tomov
9219cae9722STodor Tomov ov5645->current_mode = new_mode;
9221c2177fdSTodor Tomov }
9239cae9722STodor Tomov
9240d346d2aSTomi Valkeinen __format = __ov5645_get_pad_format(ov5645, sd_state, format->pad,
9259cae9722STodor Tomov format->which);
9269cae9722STodor Tomov __format->width = __crop->width;
9279cae9722STodor Tomov __format->height = __crop->height;
928ba449bb5SJacopo Mondi __format->code = MEDIA_BUS_FMT_UYVY8_1X16;
9299cae9722STodor Tomov __format->field = V4L2_FIELD_NONE;
9309cae9722STodor Tomov __format->colorspace = V4L2_COLORSPACE_SRGB;
9319cae9722STodor Tomov
9329cae9722STodor Tomov format->format = *__format;
9339cae9722STodor Tomov
9349cae9722STodor Tomov return 0;
9359cae9722STodor Tomov }
9369cae9722STodor Tomov
ov5645_entity_init_cfg(struct v4l2_subdev * subdev,struct v4l2_subdev_state * sd_state)9379cae9722STodor Tomov static int ov5645_entity_init_cfg(struct v4l2_subdev *subdev,
9380d346d2aSTomi Valkeinen struct v4l2_subdev_state *sd_state)
9399cae9722STodor Tomov {
9409cae9722STodor Tomov struct v4l2_subdev_format fmt = { 0 };
9419cae9722STodor Tomov
9420d346d2aSTomi Valkeinen fmt.which = sd_state ? V4L2_SUBDEV_FORMAT_TRY : V4L2_SUBDEV_FORMAT_ACTIVE;
9439cae9722STodor Tomov fmt.format.width = 1920;
9449cae9722STodor Tomov fmt.format.height = 1080;
9459cae9722STodor Tomov
9460d346d2aSTomi Valkeinen ov5645_set_format(subdev, sd_state, &fmt);
9479cae9722STodor Tomov
9489cae9722STodor Tomov return 0;
9499cae9722STodor Tomov }
9509cae9722STodor Tomov
ov5645_get_selection(struct v4l2_subdev * sd,struct v4l2_subdev_state * sd_state,struct v4l2_subdev_selection * sel)9519cae9722STodor Tomov static int ov5645_get_selection(struct v4l2_subdev *sd,
9520d346d2aSTomi Valkeinen struct v4l2_subdev_state *sd_state,
9539cae9722STodor Tomov struct v4l2_subdev_selection *sel)
9549cae9722STodor Tomov {
9559cae9722STodor Tomov struct ov5645 *ov5645 = to_ov5645(sd);
9569cae9722STodor Tomov
9579cae9722STodor Tomov if (sel->target != V4L2_SEL_TGT_CROP)
9589cae9722STodor Tomov return -EINVAL;
9599cae9722STodor Tomov
9600d346d2aSTomi Valkeinen sel->r = *__ov5645_get_pad_crop(ov5645, sd_state, sel->pad,
9619cae9722STodor Tomov sel->which);
9629cae9722STodor Tomov return 0;
9639cae9722STodor Tomov }
9649cae9722STodor Tomov
ov5645_s_stream(struct v4l2_subdev * subdev,int enable)9659cae9722STodor Tomov static int ov5645_s_stream(struct v4l2_subdev *subdev, int enable)
9669cae9722STodor Tomov {
9679cae9722STodor Tomov struct ov5645 *ov5645 = to_ov5645(subdev);
9689cae9722STodor Tomov int ret;
9699cae9722STodor Tomov
9709cae9722STodor Tomov if (enable) {
971b5045039SLad Prabhakar ret = pm_runtime_resume_and_get(ov5645->dev);
972b5045039SLad Prabhakar if (ret < 0)
973b5045039SLad Prabhakar return ret;
974b5045039SLad Prabhakar
9759cae9722STodor Tomov ret = ov5645_set_register_array(ov5645,
9769cae9722STodor Tomov ov5645->current_mode->data,
9779cae9722STodor Tomov ov5645->current_mode->data_size);
9789cae9722STodor Tomov if (ret < 0) {
9799cae9722STodor Tomov dev_err(ov5645->dev, "could not set mode %dx%d\n",
9809cae9722STodor Tomov ov5645->current_mode->width,
9819cae9722STodor Tomov ov5645->current_mode->height);
982b5045039SLad Prabhakar goto err_rpm_put;
9839cae9722STodor Tomov }
9849cae9722STodor Tomov ret = v4l2_ctrl_handler_setup(&ov5645->ctrls);
9859cae9722STodor Tomov if (ret < 0) {
9869cae9722STodor Tomov dev_err(ov5645->dev, "could not sync v4l2 controls\n");
987b5045039SLad Prabhakar goto err_rpm_put;
9889cae9722STodor Tomov }
989092e8eb9SEzequiel Garcia
990092e8eb9SEzequiel Garcia ret = ov5645_write_reg(ov5645, OV5645_IO_MIPI_CTRL00, 0x45);
991092e8eb9SEzequiel Garcia if (ret < 0)
992b5045039SLad Prabhakar goto err_rpm_put;
993092e8eb9SEzequiel Garcia
9949cae9722STodor Tomov ret = ov5645_write_reg(ov5645, OV5645_SYSTEM_CTRL0,
9959cae9722STodor Tomov OV5645_SYSTEM_CTRL0_START);
9969cae9722STodor Tomov if (ret < 0)
997b5045039SLad Prabhakar goto err_rpm_put;
9989cae9722STodor Tomov } else {
999092e8eb9SEzequiel Garcia ret = ov5645_write_reg(ov5645, OV5645_IO_MIPI_CTRL00, 0x40);
1000092e8eb9SEzequiel Garcia if (ret < 0)
1001d781dce6SLad Prabhakar goto stream_off_rpm_put;
1002092e8eb9SEzequiel Garcia
10039cae9722STodor Tomov ret = ov5645_write_reg(ov5645, OV5645_SYSTEM_CTRL0,
10049cae9722STodor Tomov OV5645_SYSTEM_CTRL0_STOP);
1005b5045039SLad Prabhakar
1006d781dce6SLad Prabhakar goto stream_off_rpm_put;
10079cae9722STodor Tomov }
10089cae9722STodor Tomov
10099cae9722STodor Tomov return 0;
10109cae9722STodor Tomov
1011b5045039SLad Prabhakar err_rpm_put:
1012b5045039SLad Prabhakar pm_runtime_put_sync(ov5645->dev);
1013b5045039SLad Prabhakar return ret;
1014d781dce6SLad Prabhakar
1015d781dce6SLad Prabhakar stream_off_rpm_put:
1016d781dce6SLad Prabhakar pm_runtime_mark_last_busy(ov5645->dev);
1017d781dce6SLad Prabhakar pm_runtime_put_autosuspend(ov5645->dev);
1018d781dce6SLad Prabhakar return ret;
1019b5045039SLad Prabhakar }
10209cae9722STodor Tomov
10219cae9722STodor Tomov static const struct v4l2_subdev_video_ops ov5645_video_ops = {
10229cae9722STodor Tomov .s_stream = ov5645_s_stream,
10239cae9722STodor Tomov };
10249cae9722STodor Tomov
10259cae9722STodor Tomov static const struct v4l2_subdev_pad_ops ov5645_subdev_pad_ops = {
10269cae9722STodor Tomov .init_cfg = ov5645_entity_init_cfg,
10279cae9722STodor Tomov .enum_mbus_code = ov5645_enum_mbus_code,
10289cae9722STodor Tomov .enum_frame_size = ov5645_enum_frame_size,
10299cae9722STodor Tomov .get_fmt = ov5645_get_format,
10309cae9722STodor Tomov .set_fmt = ov5645_set_format,
10319cae9722STodor Tomov .get_selection = ov5645_get_selection,
10329cae9722STodor Tomov };
10339cae9722STodor Tomov
10349cae9722STodor Tomov static const struct v4l2_subdev_ops ov5645_subdev_ops = {
10359cae9722STodor Tomov .video = &ov5645_video_ops,
10369cae9722STodor Tomov .pad = &ov5645_subdev_pad_ops,
10379cae9722STodor Tomov };
10389cae9722STodor Tomov
ov5645_probe(struct i2c_client * client)1039e6714993SKieran Bingham static int ov5645_probe(struct i2c_client *client)
10409cae9722STodor Tomov {
10419cae9722STodor Tomov struct device *dev = &client->dev;
10429cae9722STodor Tomov struct device_node *endpoint;
10439cae9722STodor Tomov struct ov5645 *ov5645;
10449cae9722STodor Tomov u8 chip_id_high, chip_id_low;
10454962bb38SFabio Estevam unsigned int i;
10469cae9722STodor Tomov u32 xclk_freq;
10479cae9722STodor Tomov int ret;
10489cae9722STodor Tomov
10499cae9722STodor Tomov ov5645 = devm_kzalloc(dev, sizeof(struct ov5645), GFP_KERNEL);
10509cae9722STodor Tomov if (!ov5645)
10519cae9722STodor Tomov return -ENOMEM;
10529cae9722STodor Tomov
10539cae9722STodor Tomov ov5645->i2c_client = client;
10549cae9722STodor Tomov ov5645->dev = dev;
10559cae9722STodor Tomov
10569cae9722STodor Tomov endpoint = of_graph_get_next_endpoint(dev->of_node, NULL);
10579cae9722STodor Tomov if (!endpoint) {
10589cae9722STodor Tomov dev_err(dev, "endpoint node not found\n");
10599cae9722STodor Tomov return -EINVAL;
10609cae9722STodor Tomov }
10619cae9722STodor Tomov
1062859969b3SSakari Ailus ret = v4l2_fwnode_endpoint_parse(of_fwnode_handle(endpoint),
1063859969b3SSakari Ailus &ov5645->ep);
106406fe9323SAkinobu Mita
106506fe9323SAkinobu Mita of_node_put(endpoint);
106606fe9323SAkinobu Mita
10679cae9722STodor Tomov if (ret < 0) {
10689cae9722STodor Tomov dev_err(dev, "parsing endpoint node failed\n");
10699cae9722STodor Tomov return ret;
10709cae9722STodor Tomov }
10719cae9722STodor Tomov
10722d95e7edSSakari Ailus if (ov5645->ep.bus_type != V4L2_MBUS_CSI2_DPHY) {
10739cae9722STodor Tomov dev_err(dev, "invalid bus type, must be CSI2\n");
10749cae9722STodor Tomov return -EINVAL;
10759cae9722STodor Tomov }
10769cae9722STodor Tomov
10779cae9722STodor Tomov /* get system clock (xclk) */
10789084e2c8SLad Prabhakar ov5645->xclk = devm_clk_get(dev, NULL);
10799cae9722STodor Tomov if (IS_ERR(ov5645->xclk)) {
10809cae9722STodor Tomov dev_err(dev, "could not get xclk");
10819cae9722STodor Tomov return PTR_ERR(ov5645->xclk);
10829cae9722STodor Tomov }
10839cae9722STodor Tomov
10849cae9722STodor Tomov ret = of_property_read_u32(dev->of_node, "clock-frequency", &xclk_freq);
10859cae9722STodor Tomov if (ret) {
10869cae9722STodor Tomov dev_err(dev, "could not get xclk frequency\n");
10879cae9722STodor Tomov return ret;
10889cae9722STodor Tomov }
10899cae9722STodor Tomov
10904adb0a04STodor Tomov /* external clock must be 24MHz, allow 1% tolerance */
10914adb0a04STodor Tomov if (xclk_freq < 23760000 || xclk_freq > 24240000) {
10929cae9722STodor Tomov dev_err(dev, "external clock frequency %u is not supported\n",
10939cae9722STodor Tomov xclk_freq);
10949cae9722STodor Tomov return -EINVAL;
10959cae9722STodor Tomov }
10969cae9722STodor Tomov
10979cae9722STodor Tomov ret = clk_set_rate(ov5645->xclk, xclk_freq);
10989cae9722STodor Tomov if (ret) {
10999cae9722STodor Tomov dev_err(dev, "could not set xclk frequency\n");
11009cae9722STodor Tomov return ret;
11019cae9722STodor Tomov }
11029cae9722STodor Tomov
11034962bb38SFabio Estevam for (i = 0; i < OV5645_NUM_SUPPLIES; i++)
11044962bb38SFabio Estevam ov5645->supplies[i].supply = ov5645_supply_name[i];
11059cae9722STodor Tomov
11064962bb38SFabio Estevam ret = devm_regulator_bulk_get(dev, OV5645_NUM_SUPPLIES,
11074962bb38SFabio Estevam ov5645->supplies);
11084962bb38SFabio Estevam if (ret < 0)
11094962bb38SFabio Estevam return ret;
11109cae9722STodor Tomov
11119cae9722STodor Tomov ov5645->enable_gpio = devm_gpiod_get(dev, "enable", GPIOD_OUT_HIGH);
11129cae9722STodor Tomov if (IS_ERR(ov5645->enable_gpio)) {
11139cae9722STodor Tomov dev_err(dev, "cannot get enable gpio\n");
11149cae9722STodor Tomov return PTR_ERR(ov5645->enable_gpio);
11159cae9722STodor Tomov }
11169cae9722STodor Tomov
11179cae9722STodor Tomov ov5645->rst_gpio = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH);
11189cae9722STodor Tomov if (IS_ERR(ov5645->rst_gpio)) {
11199cae9722STodor Tomov dev_err(dev, "cannot get reset gpio\n");
11209cae9722STodor Tomov return PTR_ERR(ov5645->rst_gpio);
11219cae9722STodor Tomov }
11229cae9722STodor Tomov
11239cae9722STodor Tomov mutex_init(&ov5645->power_lock);
11249cae9722STodor Tomov
1125e72e6cbeSTodor Tomov v4l2_ctrl_handler_init(&ov5645->ctrls, 9);
11269cae9722STodor Tomov v4l2_ctrl_new_std(&ov5645->ctrls, &ov5645_ctrl_ops,
11279cae9722STodor Tomov V4L2_CID_SATURATION, -4, 4, 1, 0);
11289cae9722STodor Tomov v4l2_ctrl_new_std(&ov5645->ctrls, &ov5645_ctrl_ops,
11299cae9722STodor Tomov V4L2_CID_HFLIP, 0, 1, 1, 0);
11309cae9722STodor Tomov v4l2_ctrl_new_std(&ov5645->ctrls, &ov5645_ctrl_ops,
11319cae9722STodor Tomov V4L2_CID_VFLIP, 0, 1, 1, 0);
11329cae9722STodor Tomov v4l2_ctrl_new_std(&ov5645->ctrls, &ov5645_ctrl_ops,
11339cae9722STodor Tomov V4L2_CID_AUTOGAIN, 0, 1, 1, 1);
11349cae9722STodor Tomov v4l2_ctrl_new_std(&ov5645->ctrls, &ov5645_ctrl_ops,
11359cae9722STodor Tomov V4L2_CID_AUTO_WHITE_BALANCE, 0, 1, 1, 1);
11369cae9722STodor Tomov v4l2_ctrl_new_std_menu(&ov5645->ctrls, &ov5645_ctrl_ops,
11379cae9722STodor Tomov V4L2_CID_EXPOSURE_AUTO, V4L2_EXPOSURE_MANUAL,
11389cae9722STodor Tomov 0, V4L2_EXPOSURE_AUTO);
11399cae9722STodor Tomov v4l2_ctrl_new_std_menu_items(&ov5645->ctrls, &ov5645_ctrl_ops,
11409cae9722STodor Tomov V4L2_CID_TEST_PATTERN,
11419cae9722STodor Tomov ARRAY_SIZE(ov5645_test_pattern_menu) - 1,
11429cae9722STodor Tomov 0, 0, ov5645_test_pattern_menu);
11431c2177fdSTodor Tomov ov5645->pixel_clock = v4l2_ctrl_new_std(&ov5645->ctrls,
11441c2177fdSTodor Tomov &ov5645_ctrl_ops,
11451c2177fdSTodor Tomov V4L2_CID_PIXEL_RATE,
11461c2177fdSTodor Tomov 1, INT_MAX, 1, 1);
1147e72e6cbeSTodor Tomov ov5645->link_freq = v4l2_ctrl_new_int_menu(&ov5645->ctrls,
1148e72e6cbeSTodor Tomov &ov5645_ctrl_ops,
1149e72e6cbeSTodor Tomov V4L2_CID_LINK_FREQ,
1150e72e6cbeSTodor Tomov ARRAY_SIZE(link_freq) - 1,
1151e72e6cbeSTodor Tomov 0, link_freq);
1152e72e6cbeSTodor Tomov if (ov5645->link_freq)
1153e72e6cbeSTodor Tomov ov5645->link_freq->flags |= V4L2_CTRL_FLAG_READ_ONLY;
11549cae9722STodor Tomov
11559cae9722STodor Tomov ov5645->sd.ctrl_handler = &ov5645->ctrls;
11569cae9722STodor Tomov
11579cae9722STodor Tomov if (ov5645->ctrls.error) {
11589cae9722STodor Tomov dev_err(dev, "%s: control initialization error %d\n",
11599cae9722STodor Tomov __func__, ov5645->ctrls.error);
11609cae9722STodor Tomov ret = ov5645->ctrls.error;
11619cae9722STodor Tomov goto free_ctrl;
11629cae9722STodor Tomov }
11639cae9722STodor Tomov
11649cae9722STodor Tomov v4l2_i2c_subdev_init(&ov5645->sd, client, &ov5645_subdev_ops);
11659cae9722STodor Tomov ov5645->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
11669cae9722STodor Tomov ov5645->pad.flags = MEDIA_PAD_FL_SOURCE;
11679cae9722STodor Tomov ov5645->sd.dev = &client->dev;
1168f7b51859STodor Tomov ov5645->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
11699cae9722STodor Tomov
11709cae9722STodor Tomov ret = media_entity_pads_init(&ov5645->sd.entity, 1, &ov5645->pad);
11719cae9722STodor Tomov if (ret < 0) {
11729cae9722STodor Tomov dev_err(dev, "could not register media entity\n");
11739cae9722STodor Tomov goto free_ctrl;
11749cae9722STodor Tomov }
11759cae9722STodor Tomov
1176b5045039SLad Prabhakar ret = ov5645_set_power_on(dev);
1177b5045039SLad Prabhakar if (ret)
11789cae9722STodor Tomov goto free_entity;
11799cae9722STodor Tomov
11809cae9722STodor Tomov ret = ov5645_read_reg(ov5645, OV5645_CHIP_ID_HIGH, &chip_id_high);
11819cae9722STodor Tomov if (ret < 0 || chip_id_high != OV5645_CHIP_ID_HIGH_BYTE) {
11829cae9722STodor Tomov dev_err(dev, "could not read ID high\n");
11839cae9722STodor Tomov ret = -ENODEV;
11849cae9722STodor Tomov goto power_down;
11859cae9722STodor Tomov }
11869cae9722STodor Tomov ret = ov5645_read_reg(ov5645, OV5645_CHIP_ID_LOW, &chip_id_low);
11879cae9722STodor Tomov if (ret < 0 || chip_id_low != OV5645_CHIP_ID_LOW_BYTE) {
11889cae9722STodor Tomov dev_err(dev, "could not read ID low\n");
11899cae9722STodor Tomov ret = -ENODEV;
11909cae9722STodor Tomov goto power_down;
11919cae9722STodor Tomov }
11929cae9722STodor Tomov
11939cae9722STodor Tomov dev_info(dev, "OV5645 detected at address 0x%02x\n", client->addr);
11949cae9722STodor Tomov
11959cae9722STodor Tomov ret = ov5645_read_reg(ov5645, OV5645_AEC_PK_MANUAL,
11969cae9722STodor Tomov &ov5645->aec_pk_manual);
11979cae9722STodor Tomov if (ret < 0) {
11989cae9722STodor Tomov dev_err(dev, "could not read AEC/AGC mode\n");
11999cae9722STodor Tomov ret = -ENODEV;
12009cae9722STodor Tomov goto power_down;
12019cae9722STodor Tomov }
12029cae9722STodor Tomov
12039cae9722STodor Tomov ret = ov5645_read_reg(ov5645, OV5645_TIMING_TC_REG20,
12049cae9722STodor Tomov &ov5645->timing_tc_reg20);
12059cae9722STodor Tomov if (ret < 0) {
12069cae9722STodor Tomov dev_err(dev, "could not read vflip value\n");
12079cae9722STodor Tomov ret = -ENODEV;
12089cae9722STodor Tomov goto power_down;
12099cae9722STodor Tomov }
12109cae9722STodor Tomov
12119cae9722STodor Tomov ret = ov5645_read_reg(ov5645, OV5645_TIMING_TC_REG21,
12129cae9722STodor Tomov &ov5645->timing_tc_reg21);
12139cae9722STodor Tomov if (ret < 0) {
12149cae9722STodor Tomov dev_err(dev, "could not read hflip value\n");
12159cae9722STodor Tomov ret = -ENODEV;
12169cae9722STodor Tomov goto power_down;
12179cae9722STodor Tomov }
12189cae9722STodor Tomov
1219b5045039SLad Prabhakar pm_runtime_set_active(dev);
1220b5045039SLad Prabhakar pm_runtime_get_noresume(dev);
1221b5045039SLad Prabhakar pm_runtime_enable(dev);
12229cae9722STodor Tomov
122387fab0ebSLad Prabhakar ov5645_entity_init_cfg(&ov5645->sd, NULL);
122487fab0ebSLad Prabhakar
12259cae9722STodor Tomov ret = v4l2_async_register_subdev(&ov5645->sd);
12269cae9722STodor Tomov if (ret < 0) {
12279cae9722STodor Tomov dev_err(dev, "could not register v4l2 device\n");
1228b5045039SLad Prabhakar goto err_pm_runtime;
12299cae9722STodor Tomov }
12309cae9722STodor Tomov
1231b5045039SLad Prabhakar pm_runtime_set_autosuspend_delay(dev, 1000);
1232b5045039SLad Prabhakar pm_runtime_use_autosuspend(dev);
1233b5045039SLad Prabhakar pm_runtime_mark_last_busy(dev);
1234b5045039SLad Prabhakar pm_runtime_put_autosuspend(dev);
1235b5045039SLad Prabhakar
12369cae9722STodor Tomov return 0;
12379cae9722STodor Tomov
1238b5045039SLad Prabhakar err_pm_runtime:
1239b5045039SLad Prabhakar pm_runtime_disable(dev);
1240b5045039SLad Prabhakar pm_runtime_put_noidle(dev);
12419cae9722STodor Tomov power_down:
1242b5045039SLad Prabhakar ov5645_set_power_off(dev);
12439cae9722STodor Tomov free_entity:
12449cae9722STodor Tomov media_entity_cleanup(&ov5645->sd.entity);
12459cae9722STodor Tomov free_ctrl:
12469cae9722STodor Tomov v4l2_ctrl_handler_free(&ov5645->ctrls);
12479cae9722STodor Tomov mutex_destroy(&ov5645->power_lock);
12489cae9722STodor Tomov
12499cae9722STodor Tomov return ret;
12509cae9722STodor Tomov }
12519cae9722STodor Tomov
ov5645_remove(struct i2c_client * client)1252ed5c2f5fSUwe Kleine-König static void ov5645_remove(struct i2c_client *client)
12539cae9722STodor Tomov {
12549cae9722STodor Tomov struct v4l2_subdev *sd = i2c_get_clientdata(client);
12559cae9722STodor Tomov struct ov5645 *ov5645 = to_ov5645(sd);
12569cae9722STodor Tomov
12579cae9722STodor Tomov v4l2_async_unregister_subdev(&ov5645->sd);
12589cae9722STodor Tomov media_entity_cleanup(&ov5645->sd.entity);
12599cae9722STodor Tomov v4l2_ctrl_handler_free(&ov5645->ctrls);
1260b5045039SLad Prabhakar pm_runtime_disable(ov5645->dev);
1261b5045039SLad Prabhakar if (!pm_runtime_status_suspended(ov5645->dev))
1262b5045039SLad Prabhakar ov5645_set_power_off(ov5645->dev);
1263b5045039SLad Prabhakar pm_runtime_set_suspended(ov5645->dev);
12649cae9722STodor Tomov mutex_destroy(&ov5645->power_lock);
12659cae9722STodor Tomov }
12669cae9722STodor Tomov
12679cae9722STodor Tomov static const struct i2c_device_id ov5645_id[] = {
12689cae9722STodor Tomov { "ov5645", 0 },
12699cae9722STodor Tomov {}
12709cae9722STodor Tomov };
12719cae9722STodor Tomov MODULE_DEVICE_TABLE(i2c, ov5645_id);
12729cae9722STodor Tomov
12739cae9722STodor Tomov static const struct of_device_id ov5645_of_match[] = {
12749cae9722STodor Tomov { .compatible = "ovti,ov5645" },
12759cae9722STodor Tomov { /* sentinel */ }
12769cae9722STodor Tomov };
12779cae9722STodor Tomov MODULE_DEVICE_TABLE(of, ov5645_of_match);
12789cae9722STodor Tomov
1279b5045039SLad Prabhakar static const struct dev_pm_ops ov5645_pm_ops = {
1280b5045039SLad Prabhakar SET_RUNTIME_PM_OPS(ov5645_set_power_off, ov5645_set_power_on, NULL)
1281b5045039SLad Prabhakar };
1282b5045039SLad Prabhakar
12839cae9722STodor Tomov static struct i2c_driver ov5645_i2c_driver = {
12849cae9722STodor Tomov .driver = {
128592beb555SMoses Christopher Bollavarapu .of_match_table = ov5645_of_match,
12869cae9722STodor Tomov .name = "ov5645",
1287b5045039SLad Prabhakar .pm = &ov5645_pm_ops,
12889cae9722STodor Tomov },
1289*aaeb31c0SUwe Kleine-König .probe = ov5645_probe,
12909cae9722STodor Tomov .remove = ov5645_remove,
12919cae9722STodor Tomov .id_table = ov5645_id,
12929cae9722STodor Tomov };
12939cae9722STodor Tomov
12949cae9722STodor Tomov module_i2c_driver(ov5645_i2c_driver);
12959cae9722STodor Tomov
12969cae9722STodor Tomov MODULE_DESCRIPTION("Omnivision OV5645 Camera Driver");
12979cae9722STodor Tomov MODULE_AUTHOR("Todor Tomov <todor.tomov@linaro.org>");
12989cae9722STodor Tomov MODULE_LICENSE("GPL v2");
1299