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 /* 189cae9722STodor Tomov */ 199cae9722STodor Tomov 209cae9722STodor Tomov #include <linux/bitops.h> 219cae9722STodor Tomov #include <linux/clk.h> 229cae9722STodor Tomov #include <linux/delay.h> 239cae9722STodor Tomov #include <linux/device.h> 249cae9722STodor Tomov #include <linux/gpio/consumer.h> 259cae9722STodor Tomov #include <linux/i2c.h> 269cae9722STodor Tomov #include <linux/init.h> 279cae9722STodor Tomov #include <linux/module.h> 289cae9722STodor Tomov #include <linux/of.h> 299cae9722STodor Tomov #include <linux/of_graph.h> 309cae9722STodor Tomov #include <linux/regulator/consumer.h> 319cae9722STodor Tomov #include <linux/slab.h> 329cae9722STodor Tomov #include <linux/types.h> 339cae9722STodor Tomov #include <media/v4l2-ctrls.h> 34859969b3SSakari Ailus #include <media/v4l2-fwnode.h> 359cae9722STodor Tomov #include <media/v4l2-subdev.h> 369cae9722STodor Tomov 379cae9722STodor Tomov #define OV5645_SYSTEM_CTRL0 0x3008 389cae9722STodor Tomov #define OV5645_SYSTEM_CTRL0_START 0x02 399cae9722STodor Tomov #define OV5645_SYSTEM_CTRL0_STOP 0x42 409cae9722STodor Tomov #define OV5645_CHIP_ID_HIGH 0x300a 419cae9722STodor Tomov #define OV5645_CHIP_ID_HIGH_BYTE 0x56 429cae9722STodor Tomov #define OV5645_CHIP_ID_LOW 0x300b 439cae9722STodor Tomov #define OV5645_CHIP_ID_LOW_BYTE 0x45 44092e8eb9SEzequiel Garcia #define OV5645_IO_MIPI_CTRL00 0x300e 45092e8eb9SEzequiel Garcia #define OV5645_PAD_OUTPUT00 0x3019 469cae9722STodor Tomov #define OV5645_AWB_MANUAL_CONTROL 0x3406 479cae9722STodor Tomov #define OV5645_AWB_MANUAL_ENABLE BIT(0) 489cae9722STodor Tomov #define OV5645_AEC_PK_MANUAL 0x3503 499cae9722STodor Tomov #define OV5645_AEC_MANUAL_ENABLE BIT(0) 509cae9722STodor Tomov #define OV5645_AGC_MANUAL_ENABLE BIT(1) 519cae9722STodor Tomov #define OV5645_TIMING_TC_REG20 0x3820 529cae9722STodor Tomov #define OV5645_SENSOR_VFLIP BIT(1) 539cae9722STodor Tomov #define OV5645_ISP_VFLIP BIT(2) 549cae9722STodor Tomov #define OV5645_TIMING_TC_REG21 0x3821 559cae9722STodor Tomov #define OV5645_SENSOR_MIRROR BIT(1) 56092e8eb9SEzequiel Garcia #define OV5645_MIPI_CTRL00 0x4800 579cae9722STodor Tomov #define OV5645_PRE_ISP_TEST_SETTING_1 0x503d 589cae9722STodor Tomov #define OV5645_TEST_PATTERN_MASK 0x3 599cae9722STodor Tomov #define OV5645_SET_TEST_PATTERN(x) ((x) & OV5645_TEST_PATTERN_MASK) 609cae9722STodor Tomov #define OV5645_TEST_PATTERN_ENABLE BIT(7) 619cae9722STodor Tomov #define OV5645_SDE_SAT_U 0x5583 629cae9722STodor Tomov #define OV5645_SDE_SAT_V 0x5584 639cae9722STodor Tomov 644962bb38SFabio Estevam /* regulator supplies */ 654962bb38SFabio Estevam static const char * const ov5645_supply_name[] = { 664962bb38SFabio Estevam "vdddo", /* Digital I/O (1.8V) supply */ 674962bb38SFabio Estevam "vdda", /* Analog (2.8V) supply */ 684962bb38SFabio Estevam "vddd", /* Digital Core (1.5V) supply */ 694962bb38SFabio Estevam }; 704962bb38SFabio Estevam 714962bb38SFabio Estevam #define OV5645_NUM_SUPPLIES ARRAY_SIZE(ov5645_supply_name) 724962bb38SFabio Estevam 739cae9722STodor Tomov struct reg_value { 749cae9722STodor Tomov u16 reg; 759cae9722STodor Tomov u8 val; 769cae9722STodor Tomov }; 779cae9722STodor Tomov 789cae9722STodor Tomov struct ov5645_mode_info { 799cae9722STodor Tomov u32 width; 809cae9722STodor Tomov u32 height; 819cae9722STodor Tomov const struct reg_value *data; 829cae9722STodor Tomov u32 data_size; 831c2177fdSTodor Tomov u32 pixel_clock; 84e72e6cbeSTodor Tomov u32 link_freq; 859cae9722STodor Tomov }; 869cae9722STodor Tomov 879cae9722STodor Tomov struct ov5645 { 889cae9722STodor Tomov struct i2c_client *i2c_client; 899cae9722STodor Tomov struct device *dev; 909cae9722STodor Tomov struct v4l2_subdev sd; 919cae9722STodor Tomov struct media_pad pad; 92859969b3SSakari Ailus struct v4l2_fwnode_endpoint ep; 939cae9722STodor Tomov struct v4l2_mbus_framefmt fmt; 949cae9722STodor Tomov struct v4l2_rect crop; 959cae9722STodor Tomov struct clk *xclk; 969cae9722STodor Tomov 974962bb38SFabio Estevam struct regulator_bulk_data supplies[OV5645_NUM_SUPPLIES]; 989cae9722STodor Tomov 999cae9722STodor Tomov const struct ov5645_mode_info *current_mode; 1009cae9722STodor Tomov 1019cae9722STodor Tomov struct v4l2_ctrl_handler ctrls; 1021c2177fdSTodor Tomov struct v4l2_ctrl *pixel_clock; 103e72e6cbeSTodor Tomov struct v4l2_ctrl *link_freq; 1049cae9722STodor Tomov 1059cae9722STodor Tomov /* Cached register values */ 1069cae9722STodor Tomov u8 aec_pk_manual; 1079cae9722STodor Tomov u8 timing_tc_reg20; 1089cae9722STodor Tomov u8 timing_tc_reg21; 1099cae9722STodor Tomov 1109cae9722STodor Tomov struct mutex power_lock; /* lock to protect power state */ 1119cae9722STodor Tomov int power_count; 1129cae9722STodor Tomov 1139cae9722STodor Tomov struct gpio_desc *enable_gpio; 1149cae9722STodor Tomov struct gpio_desc *rst_gpio; 1159cae9722STodor Tomov }; 1169cae9722STodor Tomov 1179cae9722STodor Tomov static inline struct ov5645 *to_ov5645(struct v4l2_subdev *sd) 1189cae9722STodor Tomov { 1199cae9722STodor Tomov return container_of(sd, struct ov5645, sd); 1209cae9722STodor Tomov } 1219cae9722STodor Tomov 1229cae9722STodor Tomov static const struct reg_value ov5645_global_init_setting[] = { 1239cae9722STodor Tomov { 0x3103, 0x11 }, 1249cae9722STodor Tomov { 0x3008, 0x82 }, 1259cae9722STodor Tomov { 0x3008, 0x42 }, 1269cae9722STodor Tomov { 0x3103, 0x03 }, 1279cae9722STodor Tomov { 0x3503, 0x07 }, 1289cae9722STodor Tomov { 0x3002, 0x1c }, 1299cae9722STodor Tomov { 0x3006, 0xc3 }, 1309cae9722STodor Tomov { 0x3017, 0x00 }, 1319cae9722STodor Tomov { 0x3018, 0x00 }, 1329cae9722STodor Tomov { 0x302e, 0x0b }, 1339cae9722STodor Tomov { 0x3037, 0x13 }, 1349cae9722STodor Tomov { 0x3108, 0x01 }, 1359cae9722STodor Tomov { 0x3611, 0x06 }, 1369cae9722STodor Tomov { 0x3500, 0x00 }, 1379cae9722STodor Tomov { 0x3501, 0x01 }, 1389cae9722STodor Tomov { 0x3502, 0x00 }, 1399cae9722STodor Tomov { 0x350a, 0x00 }, 1409cae9722STodor Tomov { 0x350b, 0x3f }, 1419cae9722STodor Tomov { 0x3620, 0x33 }, 1429cae9722STodor Tomov { 0x3621, 0xe0 }, 1439cae9722STodor Tomov { 0x3622, 0x01 }, 1449cae9722STodor Tomov { 0x3630, 0x2e }, 1459cae9722STodor Tomov { 0x3631, 0x00 }, 1469cae9722STodor Tomov { 0x3632, 0x32 }, 1479cae9722STodor Tomov { 0x3633, 0x52 }, 1489cae9722STodor Tomov { 0x3634, 0x70 }, 1499cae9722STodor Tomov { 0x3635, 0x13 }, 1509cae9722STodor Tomov { 0x3636, 0x03 }, 1519cae9722STodor Tomov { 0x3703, 0x5a }, 1529cae9722STodor Tomov { 0x3704, 0xa0 }, 1539cae9722STodor Tomov { 0x3705, 0x1a }, 1549cae9722STodor Tomov { 0x3709, 0x12 }, 1559cae9722STodor Tomov { 0x370b, 0x61 }, 1569cae9722STodor Tomov { 0x370f, 0x10 }, 1579cae9722STodor Tomov { 0x3715, 0x78 }, 1589cae9722STodor Tomov { 0x3717, 0x01 }, 1599cae9722STodor Tomov { 0x371b, 0x20 }, 1609cae9722STodor Tomov { 0x3731, 0x12 }, 1619cae9722STodor Tomov { 0x3901, 0x0a }, 1629cae9722STodor Tomov { 0x3905, 0x02 }, 1639cae9722STodor Tomov { 0x3906, 0x10 }, 1649cae9722STodor Tomov { 0x3719, 0x86 }, 1659cae9722STodor Tomov { 0x3810, 0x00 }, 1669cae9722STodor Tomov { 0x3811, 0x10 }, 1679cae9722STodor Tomov { 0x3812, 0x00 }, 1689cae9722STodor Tomov { 0x3821, 0x01 }, 1699cae9722STodor Tomov { 0x3824, 0x01 }, 1709cae9722STodor Tomov { 0x3826, 0x03 }, 1719cae9722STodor Tomov { 0x3828, 0x08 }, 1729cae9722STodor Tomov { 0x3a19, 0xf8 }, 1739cae9722STodor Tomov { 0x3c01, 0x34 }, 1749cae9722STodor Tomov { 0x3c04, 0x28 }, 1759cae9722STodor Tomov { 0x3c05, 0x98 }, 1769cae9722STodor Tomov { 0x3c07, 0x07 }, 1779cae9722STodor Tomov { 0x3c09, 0xc2 }, 1789cae9722STodor Tomov { 0x3c0a, 0x9c }, 1799cae9722STodor Tomov { 0x3c0b, 0x40 }, 1809cae9722STodor Tomov { 0x3c01, 0x34 }, 1819cae9722STodor Tomov { 0x4001, 0x02 }, 1829cae9722STodor Tomov { 0x4514, 0x00 }, 1839cae9722STodor Tomov { 0x4520, 0xb0 }, 1849cae9722STodor Tomov { 0x460b, 0x37 }, 1859cae9722STodor Tomov { 0x460c, 0x20 }, 1869cae9722STodor Tomov { 0x4818, 0x01 }, 1879cae9722STodor Tomov { 0x481d, 0xf0 }, 1889cae9722STodor Tomov { 0x481f, 0x50 }, 1899cae9722STodor Tomov { 0x4823, 0x70 }, 1909cae9722STodor Tomov { 0x4831, 0x14 }, 1919cae9722STodor Tomov { 0x5000, 0xa7 }, 1929cae9722STodor Tomov { 0x5001, 0x83 }, 1939cae9722STodor Tomov { 0x501d, 0x00 }, 1949cae9722STodor Tomov { 0x501f, 0x00 }, 1959cae9722STodor Tomov { 0x503d, 0x00 }, 1969cae9722STodor Tomov { 0x505c, 0x30 }, 1979cae9722STodor Tomov { 0x5181, 0x59 }, 1989cae9722STodor Tomov { 0x5183, 0x00 }, 1999cae9722STodor Tomov { 0x5191, 0xf0 }, 2009cae9722STodor Tomov { 0x5192, 0x03 }, 2019cae9722STodor Tomov { 0x5684, 0x10 }, 2029cae9722STodor Tomov { 0x5685, 0xa0 }, 2039cae9722STodor Tomov { 0x5686, 0x0c }, 2049cae9722STodor Tomov { 0x5687, 0x78 }, 2059cae9722STodor Tomov { 0x5a00, 0x08 }, 2069cae9722STodor Tomov { 0x5a21, 0x00 }, 2079cae9722STodor Tomov { 0x5a24, 0x00 }, 2089cae9722STodor Tomov { 0x3008, 0x02 }, 2099cae9722STodor Tomov { 0x3503, 0x00 }, 2109cae9722STodor Tomov { 0x5180, 0xff }, 2119cae9722STodor Tomov { 0x5181, 0xf2 }, 2129cae9722STodor Tomov { 0x5182, 0x00 }, 2139cae9722STodor Tomov { 0x5183, 0x14 }, 2149cae9722STodor Tomov { 0x5184, 0x25 }, 2159cae9722STodor Tomov { 0x5185, 0x24 }, 2169cae9722STodor Tomov { 0x5186, 0x09 }, 2179cae9722STodor Tomov { 0x5187, 0x09 }, 2189cae9722STodor Tomov { 0x5188, 0x0a }, 2199cae9722STodor Tomov { 0x5189, 0x75 }, 2209cae9722STodor Tomov { 0x518a, 0x52 }, 2219cae9722STodor Tomov { 0x518b, 0xea }, 2229cae9722STodor Tomov { 0x518c, 0xa8 }, 2239cae9722STodor Tomov { 0x518d, 0x42 }, 2249cae9722STodor Tomov { 0x518e, 0x38 }, 2259cae9722STodor Tomov { 0x518f, 0x56 }, 2269cae9722STodor Tomov { 0x5190, 0x42 }, 2279cae9722STodor Tomov { 0x5191, 0xf8 }, 2289cae9722STodor Tomov { 0x5192, 0x04 }, 2299cae9722STodor Tomov { 0x5193, 0x70 }, 2309cae9722STodor Tomov { 0x5194, 0xf0 }, 2319cae9722STodor Tomov { 0x5195, 0xf0 }, 2329cae9722STodor Tomov { 0x5196, 0x03 }, 2339cae9722STodor Tomov { 0x5197, 0x01 }, 2349cae9722STodor Tomov { 0x5198, 0x04 }, 2359cae9722STodor Tomov { 0x5199, 0x12 }, 2369cae9722STodor Tomov { 0x519a, 0x04 }, 2379cae9722STodor Tomov { 0x519b, 0x00 }, 2389cae9722STodor Tomov { 0x519c, 0x06 }, 2399cae9722STodor Tomov { 0x519d, 0x82 }, 2409cae9722STodor Tomov { 0x519e, 0x38 }, 2419cae9722STodor Tomov { 0x5381, 0x1e }, 2429cae9722STodor Tomov { 0x5382, 0x5b }, 2439cae9722STodor Tomov { 0x5383, 0x08 }, 2449cae9722STodor Tomov { 0x5384, 0x0a }, 2459cae9722STodor Tomov { 0x5385, 0x7e }, 2469cae9722STodor Tomov { 0x5386, 0x88 }, 2479cae9722STodor Tomov { 0x5387, 0x7c }, 2489cae9722STodor Tomov { 0x5388, 0x6c }, 2499cae9722STodor Tomov { 0x5389, 0x10 }, 2509cae9722STodor Tomov { 0x538a, 0x01 }, 2519cae9722STodor Tomov { 0x538b, 0x98 }, 2529cae9722STodor Tomov { 0x5300, 0x08 }, 2539cae9722STodor Tomov { 0x5301, 0x30 }, 2549cae9722STodor Tomov { 0x5302, 0x10 }, 2559cae9722STodor Tomov { 0x5303, 0x00 }, 2569cae9722STodor Tomov { 0x5304, 0x08 }, 2579cae9722STodor Tomov { 0x5305, 0x30 }, 2589cae9722STodor Tomov { 0x5306, 0x08 }, 2599cae9722STodor Tomov { 0x5307, 0x16 }, 2609cae9722STodor Tomov { 0x5309, 0x08 }, 2619cae9722STodor Tomov { 0x530a, 0x30 }, 2629cae9722STodor Tomov { 0x530b, 0x04 }, 2639cae9722STodor Tomov { 0x530c, 0x06 }, 2649cae9722STodor Tomov { 0x5480, 0x01 }, 2659cae9722STodor Tomov { 0x5481, 0x08 }, 2669cae9722STodor Tomov { 0x5482, 0x14 }, 2679cae9722STodor Tomov { 0x5483, 0x28 }, 2689cae9722STodor Tomov { 0x5484, 0x51 }, 2699cae9722STodor Tomov { 0x5485, 0x65 }, 2709cae9722STodor Tomov { 0x5486, 0x71 }, 2719cae9722STodor Tomov { 0x5487, 0x7d }, 2729cae9722STodor Tomov { 0x5488, 0x87 }, 2739cae9722STodor Tomov { 0x5489, 0x91 }, 2749cae9722STodor Tomov { 0x548a, 0x9a }, 2759cae9722STodor Tomov { 0x548b, 0xaa }, 2769cae9722STodor Tomov { 0x548c, 0xb8 }, 2779cae9722STodor Tomov { 0x548d, 0xcd }, 2789cae9722STodor Tomov { 0x548e, 0xdd }, 2799cae9722STodor Tomov { 0x548f, 0xea }, 2809cae9722STodor Tomov { 0x5490, 0x1d }, 2819cae9722STodor Tomov { 0x5580, 0x02 }, 2829cae9722STodor Tomov { 0x5583, 0x40 }, 2839cae9722STodor Tomov { 0x5584, 0x10 }, 2849cae9722STodor Tomov { 0x5589, 0x10 }, 2859cae9722STodor Tomov { 0x558a, 0x00 }, 2869cae9722STodor Tomov { 0x558b, 0xf8 }, 2879cae9722STodor Tomov { 0x5800, 0x3f }, 2889cae9722STodor Tomov { 0x5801, 0x16 }, 2899cae9722STodor Tomov { 0x5802, 0x0e }, 2909cae9722STodor Tomov { 0x5803, 0x0d }, 2919cae9722STodor Tomov { 0x5804, 0x17 }, 2929cae9722STodor Tomov { 0x5805, 0x3f }, 2939cae9722STodor Tomov { 0x5806, 0x0b }, 2949cae9722STodor Tomov { 0x5807, 0x06 }, 2959cae9722STodor Tomov { 0x5808, 0x04 }, 2969cae9722STodor Tomov { 0x5809, 0x04 }, 2979cae9722STodor Tomov { 0x580a, 0x06 }, 2989cae9722STodor Tomov { 0x580b, 0x0b }, 2999cae9722STodor Tomov { 0x580c, 0x09 }, 3009cae9722STodor Tomov { 0x580d, 0x03 }, 3019cae9722STodor Tomov { 0x580e, 0x00 }, 3029cae9722STodor Tomov { 0x580f, 0x00 }, 3039cae9722STodor Tomov { 0x5810, 0x03 }, 3049cae9722STodor Tomov { 0x5811, 0x08 }, 3059cae9722STodor Tomov { 0x5812, 0x0a }, 3069cae9722STodor Tomov { 0x5813, 0x03 }, 3079cae9722STodor Tomov { 0x5814, 0x00 }, 3089cae9722STodor Tomov { 0x5815, 0x00 }, 3099cae9722STodor Tomov { 0x5816, 0x04 }, 3109cae9722STodor Tomov { 0x5817, 0x09 }, 3119cae9722STodor Tomov { 0x5818, 0x0f }, 3129cae9722STodor Tomov { 0x5819, 0x08 }, 3139cae9722STodor Tomov { 0x581a, 0x06 }, 3149cae9722STodor Tomov { 0x581b, 0x06 }, 3159cae9722STodor Tomov { 0x581c, 0x08 }, 3169cae9722STodor Tomov { 0x581d, 0x0c }, 3179cae9722STodor Tomov { 0x581e, 0x3f }, 3189cae9722STodor Tomov { 0x581f, 0x1e }, 3199cae9722STodor Tomov { 0x5820, 0x12 }, 3209cae9722STodor Tomov { 0x5821, 0x13 }, 3219cae9722STodor Tomov { 0x5822, 0x21 }, 3229cae9722STodor Tomov { 0x5823, 0x3f }, 3239cae9722STodor Tomov { 0x5824, 0x68 }, 3249cae9722STodor Tomov { 0x5825, 0x28 }, 3259cae9722STodor Tomov { 0x5826, 0x2c }, 3269cae9722STodor Tomov { 0x5827, 0x28 }, 3279cae9722STodor Tomov { 0x5828, 0x08 }, 3289cae9722STodor Tomov { 0x5829, 0x48 }, 3299cae9722STodor Tomov { 0x582a, 0x64 }, 3309cae9722STodor Tomov { 0x582b, 0x62 }, 3319cae9722STodor Tomov { 0x582c, 0x64 }, 3329cae9722STodor Tomov { 0x582d, 0x28 }, 3339cae9722STodor Tomov { 0x582e, 0x46 }, 3349cae9722STodor Tomov { 0x582f, 0x62 }, 3359cae9722STodor Tomov { 0x5830, 0x60 }, 3369cae9722STodor Tomov { 0x5831, 0x62 }, 3379cae9722STodor Tomov { 0x5832, 0x26 }, 3389cae9722STodor Tomov { 0x5833, 0x48 }, 3399cae9722STodor Tomov { 0x5834, 0x66 }, 3409cae9722STodor Tomov { 0x5835, 0x44 }, 3419cae9722STodor Tomov { 0x5836, 0x64 }, 3429cae9722STodor Tomov { 0x5837, 0x28 }, 3439cae9722STodor Tomov { 0x5838, 0x66 }, 3449cae9722STodor Tomov { 0x5839, 0x48 }, 3459cae9722STodor Tomov { 0x583a, 0x2c }, 3469cae9722STodor Tomov { 0x583b, 0x28 }, 3479cae9722STodor Tomov { 0x583c, 0x26 }, 3489cae9722STodor Tomov { 0x583d, 0xae }, 3499cae9722STodor Tomov { 0x5025, 0x00 }, 3509cae9722STodor Tomov { 0x3a0f, 0x30 }, 3519cae9722STodor Tomov { 0x3a10, 0x28 }, 3529cae9722STodor Tomov { 0x3a1b, 0x30 }, 3539cae9722STodor Tomov { 0x3a1e, 0x26 }, 3549cae9722STodor Tomov { 0x3a11, 0x60 }, 3559cae9722STodor Tomov { 0x3a1f, 0x14 }, 3569cae9722STodor Tomov { 0x0601, 0x02 }, 3579cae9722STodor Tomov { 0x3008, 0x42 }, 358092e8eb9SEzequiel Garcia { 0x3008, 0x02 }, 359092e8eb9SEzequiel Garcia { OV5645_IO_MIPI_CTRL00, 0x40 }, 360092e8eb9SEzequiel Garcia { OV5645_MIPI_CTRL00, 0x24 }, 361092e8eb9SEzequiel Garcia { OV5645_PAD_OUTPUT00, 0x70 } 3629cae9722STodor Tomov }; 3639cae9722STodor Tomov 3649cae9722STodor Tomov static const struct reg_value ov5645_setting_sxga[] = { 3659cae9722STodor Tomov { 0x3612, 0xa9 }, 3669cae9722STodor Tomov { 0x3614, 0x50 }, 3679cae9722STodor Tomov { 0x3618, 0x00 }, 3689cae9722STodor Tomov { 0x3034, 0x18 }, 3699cae9722STodor Tomov { 0x3035, 0x21 }, 3709cae9722STodor Tomov { 0x3036, 0x70 }, 3719cae9722STodor Tomov { 0x3600, 0x09 }, 3729cae9722STodor Tomov { 0x3601, 0x43 }, 3739cae9722STodor Tomov { 0x3708, 0x66 }, 3749cae9722STodor Tomov { 0x370c, 0xc3 }, 3759cae9722STodor Tomov { 0x3800, 0x00 }, 3769cae9722STodor Tomov { 0x3801, 0x00 }, 3779cae9722STodor Tomov { 0x3802, 0x00 }, 3789cae9722STodor Tomov { 0x3803, 0x06 }, 3799cae9722STodor Tomov { 0x3804, 0x0a }, 3809cae9722STodor Tomov { 0x3805, 0x3f }, 3819cae9722STodor Tomov { 0x3806, 0x07 }, 3829cae9722STodor Tomov { 0x3807, 0x9d }, 3839cae9722STodor Tomov { 0x3808, 0x05 }, 3849cae9722STodor Tomov { 0x3809, 0x00 }, 3859cae9722STodor Tomov { 0x380a, 0x03 }, 3869cae9722STodor Tomov { 0x380b, 0xc0 }, 3879cae9722STodor Tomov { 0x380c, 0x07 }, 3889cae9722STodor Tomov { 0x380d, 0x68 }, 3899cae9722STodor Tomov { 0x380e, 0x03 }, 3909cae9722STodor Tomov { 0x380f, 0xd8 }, 3919cae9722STodor Tomov { 0x3813, 0x06 }, 3929cae9722STodor Tomov { 0x3814, 0x31 }, 3939cae9722STodor Tomov { 0x3815, 0x31 }, 3949cae9722STodor Tomov { 0x3820, 0x47 }, 3959cae9722STodor Tomov { 0x3a02, 0x03 }, 3969cae9722STodor Tomov { 0x3a03, 0xd8 }, 3979cae9722STodor Tomov { 0x3a08, 0x01 }, 3989cae9722STodor Tomov { 0x3a09, 0xf8 }, 3999cae9722STodor Tomov { 0x3a0a, 0x01 }, 4009cae9722STodor Tomov { 0x3a0b, 0xa4 }, 4019cae9722STodor Tomov { 0x3a0e, 0x02 }, 4029cae9722STodor Tomov { 0x3a0d, 0x02 }, 4039cae9722STodor Tomov { 0x3a14, 0x03 }, 4049cae9722STodor Tomov { 0x3a15, 0xd8 }, 4059cae9722STodor Tomov { 0x3a18, 0x00 }, 4069cae9722STodor Tomov { 0x4004, 0x02 }, 4079cae9722STodor Tomov { 0x4005, 0x18 }, 4089cae9722STodor Tomov { 0x4300, 0x32 }, 4099cae9722STodor Tomov { 0x4202, 0x00 } 4109cae9722STodor Tomov }; 4119cae9722STodor Tomov 4129cae9722STodor Tomov static const struct reg_value ov5645_setting_1080p[] = { 4139cae9722STodor Tomov { 0x3612, 0xab }, 4149cae9722STodor Tomov { 0x3614, 0x50 }, 4159cae9722STodor Tomov { 0x3618, 0x04 }, 4169cae9722STodor Tomov { 0x3034, 0x18 }, 4179cae9722STodor Tomov { 0x3035, 0x11 }, 4189cae9722STodor Tomov { 0x3036, 0x54 }, 4199cae9722STodor Tomov { 0x3600, 0x08 }, 4209cae9722STodor Tomov { 0x3601, 0x33 }, 4219cae9722STodor Tomov { 0x3708, 0x63 }, 4229cae9722STodor Tomov { 0x370c, 0xc0 }, 4239cae9722STodor Tomov { 0x3800, 0x01 }, 4249cae9722STodor Tomov { 0x3801, 0x50 }, 4259cae9722STodor Tomov { 0x3802, 0x01 }, 4269cae9722STodor Tomov { 0x3803, 0xb2 }, 4279cae9722STodor Tomov { 0x3804, 0x08 }, 4289cae9722STodor Tomov { 0x3805, 0xef }, 4299cae9722STodor Tomov { 0x3806, 0x05 }, 4309cae9722STodor Tomov { 0x3807, 0xf1 }, 4319cae9722STodor Tomov { 0x3808, 0x07 }, 4329cae9722STodor Tomov { 0x3809, 0x80 }, 4339cae9722STodor Tomov { 0x380a, 0x04 }, 4349cae9722STodor Tomov { 0x380b, 0x38 }, 4359cae9722STodor Tomov { 0x380c, 0x09 }, 4369cae9722STodor Tomov { 0x380d, 0xc4 }, 4379cae9722STodor Tomov { 0x380e, 0x04 }, 4389cae9722STodor Tomov { 0x380f, 0x60 }, 4399cae9722STodor Tomov { 0x3813, 0x04 }, 4409cae9722STodor Tomov { 0x3814, 0x11 }, 4419cae9722STodor Tomov { 0x3815, 0x11 }, 4429cae9722STodor Tomov { 0x3820, 0x47 }, 4439cae9722STodor Tomov { 0x4514, 0x88 }, 4449cae9722STodor Tomov { 0x3a02, 0x04 }, 4459cae9722STodor Tomov { 0x3a03, 0x60 }, 4469cae9722STodor Tomov { 0x3a08, 0x01 }, 4479cae9722STodor Tomov { 0x3a09, 0x50 }, 4489cae9722STodor Tomov { 0x3a0a, 0x01 }, 4499cae9722STodor Tomov { 0x3a0b, 0x18 }, 4509cae9722STodor Tomov { 0x3a0e, 0x03 }, 4519cae9722STodor Tomov { 0x3a0d, 0x04 }, 4529cae9722STodor Tomov { 0x3a14, 0x04 }, 4539cae9722STodor Tomov { 0x3a15, 0x60 }, 4549cae9722STodor Tomov { 0x3a18, 0x00 }, 4559cae9722STodor Tomov { 0x4004, 0x06 }, 4569cae9722STodor Tomov { 0x4005, 0x18 }, 4579cae9722STodor Tomov { 0x4300, 0x32 }, 4589cae9722STodor Tomov { 0x4202, 0x00 }, 4599cae9722STodor Tomov { 0x4837, 0x0b } 4609cae9722STodor Tomov }; 4619cae9722STodor Tomov 4629cae9722STodor Tomov static const struct reg_value ov5645_setting_full[] = { 4639cae9722STodor Tomov { 0x3612, 0xab }, 4649cae9722STodor Tomov { 0x3614, 0x50 }, 4659cae9722STodor Tomov { 0x3618, 0x04 }, 4669cae9722STodor Tomov { 0x3034, 0x18 }, 4679cae9722STodor Tomov { 0x3035, 0x11 }, 4689cae9722STodor Tomov { 0x3036, 0x54 }, 4699cae9722STodor Tomov { 0x3600, 0x08 }, 4709cae9722STodor Tomov { 0x3601, 0x33 }, 4719cae9722STodor Tomov { 0x3708, 0x63 }, 4729cae9722STodor Tomov { 0x370c, 0xc0 }, 4739cae9722STodor Tomov { 0x3800, 0x00 }, 4749cae9722STodor Tomov { 0x3801, 0x00 }, 4759cae9722STodor Tomov { 0x3802, 0x00 }, 4769cae9722STodor Tomov { 0x3803, 0x00 }, 4779cae9722STodor Tomov { 0x3804, 0x0a }, 4789cae9722STodor Tomov { 0x3805, 0x3f }, 4799cae9722STodor Tomov { 0x3806, 0x07 }, 4809cae9722STodor Tomov { 0x3807, 0x9f }, 4819cae9722STodor Tomov { 0x3808, 0x0a }, 4829cae9722STodor Tomov { 0x3809, 0x20 }, 4839cae9722STodor Tomov { 0x380a, 0x07 }, 4849cae9722STodor Tomov { 0x380b, 0x98 }, 4859cae9722STodor Tomov { 0x380c, 0x0b }, 4869cae9722STodor Tomov { 0x380d, 0x1c }, 4879cae9722STodor Tomov { 0x380e, 0x07 }, 4889cae9722STodor Tomov { 0x380f, 0xb0 }, 4899cae9722STodor Tomov { 0x3813, 0x06 }, 4909cae9722STodor Tomov { 0x3814, 0x11 }, 4919cae9722STodor Tomov { 0x3815, 0x11 }, 4929cae9722STodor Tomov { 0x3820, 0x47 }, 4939cae9722STodor Tomov { 0x4514, 0x88 }, 4949cae9722STodor Tomov { 0x3a02, 0x07 }, 4959cae9722STodor Tomov { 0x3a03, 0xb0 }, 4969cae9722STodor Tomov { 0x3a08, 0x01 }, 4979cae9722STodor Tomov { 0x3a09, 0x27 }, 4989cae9722STodor Tomov { 0x3a0a, 0x00 }, 4999cae9722STodor Tomov { 0x3a0b, 0xf6 }, 5009cae9722STodor Tomov { 0x3a0e, 0x06 }, 5019cae9722STodor Tomov { 0x3a0d, 0x08 }, 5029cae9722STodor Tomov { 0x3a14, 0x07 }, 5039cae9722STodor Tomov { 0x3a15, 0xb0 }, 5049cae9722STodor Tomov { 0x3a18, 0x01 }, 5059cae9722STodor Tomov { 0x4004, 0x06 }, 5069cae9722STodor Tomov { 0x4005, 0x18 }, 5079cae9722STodor Tomov { 0x4300, 0x32 }, 5089cae9722STodor Tomov { 0x4837, 0x0b }, 5099cae9722STodor Tomov { 0x4202, 0x00 } 5109cae9722STodor Tomov }; 5119cae9722STodor Tomov 512e72e6cbeSTodor Tomov static const s64 link_freq[] = { 5134adb0a04STodor Tomov 224000000, 5144adb0a04STodor Tomov 336000000 515e72e6cbeSTodor Tomov }; 516e72e6cbeSTodor Tomov 5179cae9722STodor Tomov static const struct ov5645_mode_info ov5645_mode_info_data[] = { 5189cae9722STodor Tomov { 5199cae9722STodor Tomov .width = 1280, 5209cae9722STodor Tomov .height = 960, 5219cae9722STodor Tomov .data = ov5645_setting_sxga, 5221c2177fdSTodor Tomov .data_size = ARRAY_SIZE(ov5645_setting_sxga), 5234adb0a04STodor Tomov .pixel_clock = 112000000, 524e72e6cbeSTodor Tomov .link_freq = 0 /* an index in link_freq[] */ 5259cae9722STodor Tomov }, 5269cae9722STodor Tomov { 5279cae9722STodor Tomov .width = 1920, 5289cae9722STodor Tomov .height = 1080, 5299cae9722STodor Tomov .data = ov5645_setting_1080p, 5301c2177fdSTodor Tomov .data_size = ARRAY_SIZE(ov5645_setting_1080p), 5314adb0a04STodor Tomov .pixel_clock = 168000000, 532e72e6cbeSTodor Tomov .link_freq = 1 /* an index in link_freq[] */ 5339cae9722STodor Tomov }, 5349cae9722STodor Tomov { 5359cae9722STodor Tomov .width = 2592, 5369cae9722STodor Tomov .height = 1944, 5379cae9722STodor Tomov .data = ov5645_setting_full, 5381c2177fdSTodor Tomov .data_size = ARRAY_SIZE(ov5645_setting_full), 5394adb0a04STodor Tomov .pixel_clock = 168000000, 540e72e6cbeSTodor Tomov .link_freq = 1 /* an index in link_freq[] */ 5419cae9722STodor Tomov }, 5429cae9722STodor Tomov }; 5439cae9722STodor Tomov 5449cae9722STodor Tomov static int ov5645_write_reg(struct ov5645 *ov5645, u16 reg, u8 val) 5459cae9722STodor Tomov { 5469cae9722STodor Tomov u8 regbuf[3]; 5479cae9722STodor Tomov int ret; 5489cae9722STodor Tomov 5499cae9722STodor Tomov regbuf[0] = reg >> 8; 5509cae9722STodor Tomov regbuf[1] = reg & 0xff; 5519cae9722STodor Tomov regbuf[2] = val; 5529cae9722STodor Tomov 5539cae9722STodor Tomov ret = i2c_master_send(ov5645->i2c_client, regbuf, 3); 55470648780STodor Tomov if (ret < 0) { 5559cae9722STodor Tomov dev_err(ov5645->dev, "%s: write reg error %d: reg=%x, val=%x\n", 5569cae9722STodor Tomov __func__, ret, reg, val); 5579cae9722STodor Tomov return ret; 5589cae9722STodor Tomov } 5599cae9722STodor Tomov 56070648780STodor Tomov return 0; 56170648780STodor Tomov } 56270648780STodor Tomov 5639cae9722STodor Tomov static int ov5645_read_reg(struct ov5645 *ov5645, u16 reg, u8 *val) 5649cae9722STodor Tomov { 5659cae9722STodor Tomov u8 regbuf[2]; 5669cae9722STodor Tomov int ret; 5679cae9722STodor Tomov 5689cae9722STodor Tomov regbuf[0] = reg >> 8; 5699cae9722STodor Tomov regbuf[1] = reg & 0xff; 5709cae9722STodor Tomov 5719cae9722STodor Tomov ret = i2c_master_send(ov5645->i2c_client, regbuf, 2); 5729cae9722STodor Tomov if (ret < 0) { 5739cae9722STodor Tomov dev_err(ov5645->dev, "%s: write reg error %d: reg=%x\n", 5749cae9722STodor Tomov __func__, ret, reg); 5759cae9722STodor Tomov return ret; 5769cae9722STodor Tomov } 5779cae9722STodor Tomov 5789cae9722STodor Tomov ret = i2c_master_recv(ov5645->i2c_client, val, 1); 5799cae9722STodor Tomov if (ret < 0) { 5809cae9722STodor Tomov dev_err(ov5645->dev, "%s: read reg error %d: reg=%x\n", 5819cae9722STodor Tomov __func__, ret, reg); 5829cae9722STodor Tomov return ret; 5839cae9722STodor Tomov } 5849cae9722STodor Tomov 5859cae9722STodor Tomov return 0; 5869cae9722STodor Tomov } 5879cae9722STodor Tomov 5889cae9722STodor Tomov static int ov5645_set_aec_mode(struct ov5645 *ov5645, u32 mode) 5899cae9722STodor Tomov { 5909cae9722STodor Tomov u8 val = ov5645->aec_pk_manual; 5919cae9722STodor Tomov int ret; 5929cae9722STodor Tomov 5939cae9722STodor Tomov if (mode == V4L2_EXPOSURE_AUTO) 5949cae9722STodor Tomov val &= ~OV5645_AEC_MANUAL_ENABLE; 5959cae9722STodor Tomov else /* V4L2_EXPOSURE_MANUAL */ 5969cae9722STodor Tomov val |= OV5645_AEC_MANUAL_ENABLE; 5979cae9722STodor Tomov 5989cae9722STodor Tomov ret = ov5645_write_reg(ov5645, OV5645_AEC_PK_MANUAL, val); 5999cae9722STodor Tomov if (!ret) 6009cae9722STodor Tomov ov5645->aec_pk_manual = val; 6019cae9722STodor Tomov 6029cae9722STodor Tomov return ret; 6039cae9722STodor Tomov } 6049cae9722STodor Tomov 6059cae9722STodor Tomov static int ov5645_set_agc_mode(struct ov5645 *ov5645, u32 enable) 6069cae9722STodor Tomov { 6079cae9722STodor Tomov u8 val = ov5645->aec_pk_manual; 6089cae9722STodor Tomov int ret; 6099cae9722STodor Tomov 6109cae9722STodor Tomov if (enable) 6119cae9722STodor Tomov val &= ~OV5645_AGC_MANUAL_ENABLE; 6129cae9722STodor Tomov else 6139cae9722STodor Tomov val |= OV5645_AGC_MANUAL_ENABLE; 6149cae9722STodor Tomov 6159cae9722STodor Tomov ret = ov5645_write_reg(ov5645, OV5645_AEC_PK_MANUAL, val); 6169cae9722STodor Tomov if (!ret) 6179cae9722STodor Tomov ov5645->aec_pk_manual = val; 6189cae9722STodor Tomov 6199cae9722STodor Tomov return ret; 6209cae9722STodor Tomov } 6219cae9722STodor Tomov 6229cae9722STodor Tomov static int ov5645_set_register_array(struct ov5645 *ov5645, 6239cae9722STodor Tomov const struct reg_value *settings, 6249cae9722STodor Tomov unsigned int num_settings) 6259cae9722STodor Tomov { 6269cae9722STodor Tomov unsigned int i; 6279cae9722STodor Tomov int ret; 6289cae9722STodor Tomov 6299cae9722STodor Tomov for (i = 0; i < num_settings; ++i, ++settings) { 6309cae9722STodor Tomov ret = ov5645_write_reg(ov5645, settings->reg, settings->val); 6319cae9722STodor Tomov if (ret < 0) 6329cae9722STodor Tomov return ret; 6339cae9722STodor Tomov } 6349cae9722STodor Tomov 6359cae9722STodor Tomov return 0; 6369cae9722STodor Tomov } 6379cae9722STodor Tomov 6389cae9722STodor Tomov static int ov5645_set_power_on(struct ov5645 *ov5645) 6399cae9722STodor Tomov { 6409cae9722STodor Tomov int ret; 6419cae9722STodor Tomov 6424962bb38SFabio Estevam ret = regulator_bulk_enable(OV5645_NUM_SUPPLIES, ov5645->supplies); 6434962bb38SFabio Estevam if (ret < 0) 6449cae9722STodor Tomov return ret; 6459cae9722STodor Tomov 6469cae9722STodor Tomov ret = clk_prepare_enable(ov5645->xclk); 6479cae9722STodor Tomov if (ret < 0) { 6489cae9722STodor Tomov dev_err(ov5645->dev, "clk prepare enable failed\n"); 6494962bb38SFabio Estevam regulator_bulk_disable(OV5645_NUM_SUPPLIES, ov5645->supplies); 6509cae9722STodor Tomov return ret; 6519cae9722STodor Tomov } 6529cae9722STodor Tomov 6539cae9722STodor Tomov usleep_range(5000, 15000); 6549cae9722STodor Tomov gpiod_set_value_cansleep(ov5645->enable_gpio, 1); 6559cae9722STodor Tomov 6569cae9722STodor Tomov usleep_range(1000, 2000); 6579cae9722STodor Tomov gpiod_set_value_cansleep(ov5645->rst_gpio, 0); 6589cae9722STodor Tomov 6599cae9722STodor Tomov msleep(20); 6609cae9722STodor Tomov 6619cae9722STodor Tomov return 0; 6629cae9722STodor Tomov } 6639cae9722STodor Tomov 6649cae9722STodor Tomov static void ov5645_set_power_off(struct ov5645 *ov5645) 6659cae9722STodor Tomov { 6669cae9722STodor Tomov gpiod_set_value_cansleep(ov5645->rst_gpio, 1); 6679cae9722STodor Tomov gpiod_set_value_cansleep(ov5645->enable_gpio, 0); 6689cae9722STodor Tomov clk_disable_unprepare(ov5645->xclk); 6694962bb38SFabio Estevam regulator_bulk_disable(OV5645_NUM_SUPPLIES, ov5645->supplies); 6709cae9722STodor Tomov } 6719cae9722STodor Tomov 6729cae9722STodor Tomov static int ov5645_s_power(struct v4l2_subdev *sd, int on) 6739cae9722STodor Tomov { 6749cae9722STodor Tomov struct ov5645 *ov5645 = to_ov5645(sd); 6759cae9722STodor Tomov int ret = 0; 6769cae9722STodor Tomov 6779cae9722STodor Tomov mutex_lock(&ov5645->power_lock); 6789cae9722STodor Tomov 6799cae9722STodor Tomov /* If the power count is modified from 0 to != 0 or from != 0 to 0, 6809cae9722STodor Tomov * update the power state. 6819cae9722STodor Tomov */ 6829cae9722STodor Tomov if (ov5645->power_count == !on) { 6839cae9722STodor Tomov if (on) { 6849cae9722STodor Tomov ret = ov5645_set_power_on(ov5645); 6859cae9722STodor Tomov if (ret < 0) 6869cae9722STodor Tomov goto exit; 6879cae9722STodor Tomov 6889cae9722STodor Tomov ret = ov5645_set_register_array(ov5645, 6899cae9722STodor Tomov ov5645_global_init_setting, 6909cae9722STodor Tomov ARRAY_SIZE(ov5645_global_init_setting)); 6919cae9722STodor Tomov if (ret < 0) { 6929cae9722STodor Tomov dev_err(ov5645->dev, 6939cae9722STodor Tomov "could not set init registers\n"); 6949cae9722STodor Tomov ov5645_set_power_off(ov5645); 6959cae9722STodor Tomov goto exit; 6969cae9722STodor Tomov } 6979cae9722STodor Tomov 698092e8eb9SEzequiel Garcia usleep_range(500, 1000); 6999cae9722STodor Tomov } else { 700092e8eb9SEzequiel Garcia ov5645_write_reg(ov5645, OV5645_IO_MIPI_CTRL00, 0x58); 7019cae9722STodor Tomov ov5645_set_power_off(ov5645); 7029cae9722STodor Tomov } 7039cae9722STodor Tomov } 7049cae9722STodor Tomov 7059cae9722STodor Tomov /* Update the power count. */ 7069cae9722STodor Tomov ov5645->power_count += on ? 1 : -1; 7079cae9722STodor Tomov WARN_ON(ov5645->power_count < 0); 7089cae9722STodor Tomov 7099cae9722STodor Tomov exit: 7109cae9722STodor Tomov mutex_unlock(&ov5645->power_lock); 7119cae9722STodor Tomov 7129cae9722STodor Tomov return ret; 7139cae9722STodor Tomov } 7149cae9722STodor Tomov 7159cae9722STodor Tomov static int ov5645_set_saturation(struct ov5645 *ov5645, s32 value) 7169cae9722STodor Tomov { 7179cae9722STodor Tomov u32 reg_value = (value * 0x10) + 0x40; 7189cae9722STodor Tomov int ret; 7199cae9722STodor Tomov 7209cae9722STodor Tomov ret = ov5645_write_reg(ov5645, OV5645_SDE_SAT_U, reg_value); 7219cae9722STodor Tomov if (ret < 0) 7229cae9722STodor Tomov return ret; 7239cae9722STodor Tomov 7249cae9722STodor Tomov return ov5645_write_reg(ov5645, OV5645_SDE_SAT_V, reg_value); 7259cae9722STodor Tomov } 7269cae9722STodor Tomov 7279cae9722STodor Tomov static int ov5645_set_hflip(struct ov5645 *ov5645, s32 value) 7289cae9722STodor Tomov { 7299cae9722STodor Tomov u8 val = ov5645->timing_tc_reg21; 7309cae9722STodor Tomov int ret; 7319cae9722STodor Tomov 7329cae9722STodor Tomov if (value == 0) 7339cae9722STodor Tomov val &= ~(OV5645_SENSOR_MIRROR); 7349cae9722STodor Tomov else 7359cae9722STodor Tomov val |= (OV5645_SENSOR_MIRROR); 7369cae9722STodor Tomov 7379cae9722STodor Tomov ret = ov5645_write_reg(ov5645, OV5645_TIMING_TC_REG21, val); 7389cae9722STodor Tomov if (!ret) 7399cae9722STodor Tomov ov5645->timing_tc_reg21 = val; 7409cae9722STodor Tomov 7419cae9722STodor Tomov return ret; 7429cae9722STodor Tomov } 7439cae9722STodor Tomov 7449cae9722STodor Tomov static int ov5645_set_vflip(struct ov5645 *ov5645, s32 value) 7459cae9722STodor Tomov { 7469cae9722STodor Tomov u8 val = ov5645->timing_tc_reg20; 7479cae9722STodor Tomov int ret; 7489cae9722STodor Tomov 7499cae9722STodor Tomov if (value == 0) 7509cae9722STodor Tomov val |= (OV5645_SENSOR_VFLIP | OV5645_ISP_VFLIP); 7519cae9722STodor Tomov else 7529cae9722STodor Tomov val &= ~(OV5645_SENSOR_VFLIP | OV5645_ISP_VFLIP); 7539cae9722STodor Tomov 7549cae9722STodor Tomov ret = ov5645_write_reg(ov5645, OV5645_TIMING_TC_REG20, val); 7559cae9722STodor Tomov if (!ret) 7569cae9722STodor Tomov ov5645->timing_tc_reg20 = val; 7579cae9722STodor Tomov 7589cae9722STodor Tomov return ret; 7599cae9722STodor Tomov } 7609cae9722STodor Tomov 7619cae9722STodor Tomov static int ov5645_set_test_pattern(struct ov5645 *ov5645, s32 value) 7629cae9722STodor Tomov { 7639cae9722STodor Tomov u8 val = 0; 7649cae9722STodor Tomov 7659cae9722STodor Tomov if (value) { 7669cae9722STodor Tomov val = OV5645_SET_TEST_PATTERN(value - 1); 7679cae9722STodor Tomov val |= OV5645_TEST_PATTERN_ENABLE; 7689cae9722STodor Tomov } 7699cae9722STodor Tomov 7709cae9722STodor Tomov return ov5645_write_reg(ov5645, OV5645_PRE_ISP_TEST_SETTING_1, val); 7719cae9722STodor Tomov } 7729cae9722STodor Tomov 7739cae9722STodor Tomov static const char * const ov5645_test_pattern_menu[] = { 7749cae9722STodor Tomov "Disabled", 7759cae9722STodor Tomov "Vertical Color Bars", 7769cae9722STodor Tomov "Pseudo-Random Data", 7779cae9722STodor Tomov "Color Square", 7789cae9722STodor Tomov "Black Image", 7799cae9722STodor Tomov }; 7809cae9722STodor Tomov 7819cae9722STodor Tomov static int ov5645_set_awb(struct ov5645 *ov5645, s32 enable_auto) 7829cae9722STodor Tomov { 7839cae9722STodor Tomov u8 val = 0; 7849cae9722STodor Tomov 7859cae9722STodor Tomov if (!enable_auto) 7869cae9722STodor Tomov val = OV5645_AWB_MANUAL_ENABLE; 7879cae9722STodor Tomov 7889cae9722STodor Tomov return ov5645_write_reg(ov5645, OV5645_AWB_MANUAL_CONTROL, val); 7899cae9722STodor Tomov } 7909cae9722STodor Tomov 7919cae9722STodor Tomov static int ov5645_s_ctrl(struct v4l2_ctrl *ctrl) 7929cae9722STodor Tomov { 7939cae9722STodor Tomov struct ov5645 *ov5645 = container_of(ctrl->handler, 7949cae9722STodor Tomov struct ov5645, ctrls); 7959cae9722STodor Tomov int ret; 7969cae9722STodor Tomov 7979cae9722STodor Tomov mutex_lock(&ov5645->power_lock); 7989cae9722STodor Tomov if (!ov5645->power_count) { 7999cae9722STodor Tomov mutex_unlock(&ov5645->power_lock); 8009cae9722STodor Tomov return 0; 8019cae9722STodor Tomov } 8029cae9722STodor Tomov 8039cae9722STodor Tomov switch (ctrl->id) { 8049cae9722STodor Tomov case V4L2_CID_SATURATION: 8059cae9722STodor Tomov ret = ov5645_set_saturation(ov5645, ctrl->val); 8069cae9722STodor Tomov break; 8079cae9722STodor Tomov case V4L2_CID_AUTO_WHITE_BALANCE: 8089cae9722STodor Tomov ret = ov5645_set_awb(ov5645, ctrl->val); 8099cae9722STodor Tomov break; 8109cae9722STodor Tomov case V4L2_CID_AUTOGAIN: 8119cae9722STodor Tomov ret = ov5645_set_agc_mode(ov5645, ctrl->val); 8129cae9722STodor Tomov break; 8139cae9722STodor Tomov case V4L2_CID_EXPOSURE_AUTO: 8149cae9722STodor Tomov ret = ov5645_set_aec_mode(ov5645, ctrl->val); 8159cae9722STodor Tomov break; 8169cae9722STodor Tomov case V4L2_CID_TEST_PATTERN: 8179cae9722STodor Tomov ret = ov5645_set_test_pattern(ov5645, ctrl->val); 8189cae9722STodor Tomov break; 8199cae9722STodor Tomov case V4L2_CID_HFLIP: 8209cae9722STodor Tomov ret = ov5645_set_hflip(ov5645, ctrl->val); 8219cae9722STodor Tomov break; 8229cae9722STodor Tomov case V4L2_CID_VFLIP: 8239cae9722STodor Tomov ret = ov5645_set_vflip(ov5645, ctrl->val); 8249cae9722STodor Tomov break; 8259cae9722STodor Tomov default: 8269cae9722STodor Tomov ret = -EINVAL; 8279cae9722STodor Tomov break; 8289cae9722STodor Tomov } 8299cae9722STodor Tomov 8309cae9722STodor Tomov mutex_unlock(&ov5645->power_lock); 8319cae9722STodor Tomov 8329cae9722STodor Tomov return ret; 8339cae9722STodor Tomov } 8349cae9722STodor Tomov 835d148b85eSJulia Lawall static const struct v4l2_ctrl_ops ov5645_ctrl_ops = { 8369cae9722STodor Tomov .s_ctrl = ov5645_s_ctrl, 8379cae9722STodor Tomov }; 8389cae9722STodor Tomov 8399cae9722STodor Tomov static int ov5645_enum_mbus_code(struct v4l2_subdev *sd, 8400d346d2aSTomi Valkeinen struct v4l2_subdev_state *sd_state, 8419cae9722STodor Tomov struct v4l2_subdev_mbus_code_enum *code) 8429cae9722STodor Tomov { 8439cae9722STodor Tomov if (code->index > 0) 8449cae9722STodor Tomov return -EINVAL; 8459cae9722STodor Tomov 846ba449bb5SJacopo Mondi code->code = MEDIA_BUS_FMT_UYVY8_1X16; 8479cae9722STodor Tomov 8489cae9722STodor Tomov return 0; 8499cae9722STodor Tomov } 8509cae9722STodor Tomov 8519cae9722STodor Tomov static int ov5645_enum_frame_size(struct v4l2_subdev *subdev, 8520d346d2aSTomi Valkeinen struct v4l2_subdev_state *sd_state, 8539cae9722STodor Tomov struct v4l2_subdev_frame_size_enum *fse) 8549cae9722STodor Tomov { 855ba449bb5SJacopo Mondi if (fse->code != MEDIA_BUS_FMT_UYVY8_1X16) 8569cae9722STodor Tomov return -EINVAL; 8579cae9722STodor Tomov 8589cae9722STodor Tomov if (fse->index >= ARRAY_SIZE(ov5645_mode_info_data)) 8599cae9722STodor Tomov return -EINVAL; 8609cae9722STodor Tomov 8619cae9722STodor Tomov fse->min_width = ov5645_mode_info_data[fse->index].width; 8629cae9722STodor Tomov fse->max_width = ov5645_mode_info_data[fse->index].width; 8639cae9722STodor Tomov fse->min_height = ov5645_mode_info_data[fse->index].height; 8649cae9722STodor Tomov fse->max_height = ov5645_mode_info_data[fse->index].height; 8659cae9722STodor Tomov 8669cae9722STodor Tomov return 0; 8679cae9722STodor Tomov } 8689cae9722STodor Tomov 8699cae9722STodor Tomov static struct v4l2_mbus_framefmt * 8709cae9722STodor Tomov __ov5645_get_pad_format(struct ov5645 *ov5645, 8710d346d2aSTomi Valkeinen struct v4l2_subdev_state *sd_state, 8729cae9722STodor Tomov unsigned int pad, 8739cae9722STodor Tomov enum v4l2_subdev_format_whence which) 8749cae9722STodor Tomov { 8759cae9722STodor Tomov switch (which) { 8769cae9722STodor Tomov case V4L2_SUBDEV_FORMAT_TRY: 8770d346d2aSTomi Valkeinen return v4l2_subdev_get_try_format(&ov5645->sd, sd_state, pad); 8789cae9722STodor Tomov case V4L2_SUBDEV_FORMAT_ACTIVE: 8799cae9722STodor Tomov return &ov5645->fmt; 8809cae9722STodor Tomov default: 8819cae9722STodor Tomov return NULL; 8829cae9722STodor Tomov } 8839cae9722STodor Tomov } 8849cae9722STodor Tomov 8859cae9722STodor Tomov static int ov5645_get_format(struct v4l2_subdev *sd, 8860d346d2aSTomi Valkeinen struct v4l2_subdev_state *sd_state, 8879cae9722STodor Tomov struct v4l2_subdev_format *format) 8889cae9722STodor Tomov { 8899cae9722STodor Tomov struct ov5645 *ov5645 = to_ov5645(sd); 8909cae9722STodor Tomov 8910d346d2aSTomi Valkeinen format->format = *__ov5645_get_pad_format(ov5645, sd_state, 8920d346d2aSTomi Valkeinen format->pad, 8939cae9722STodor Tomov format->which); 8949cae9722STodor Tomov return 0; 8959cae9722STodor Tomov } 8969cae9722STodor Tomov 8979cae9722STodor Tomov static struct v4l2_rect * 8980d346d2aSTomi Valkeinen __ov5645_get_pad_crop(struct ov5645 *ov5645, 8990d346d2aSTomi Valkeinen struct v4l2_subdev_state *sd_state, 9009cae9722STodor Tomov unsigned int pad, enum v4l2_subdev_format_whence which) 9019cae9722STodor Tomov { 9029cae9722STodor Tomov switch (which) { 9039cae9722STodor Tomov case V4L2_SUBDEV_FORMAT_TRY: 9040d346d2aSTomi Valkeinen return v4l2_subdev_get_try_crop(&ov5645->sd, sd_state, pad); 9059cae9722STodor Tomov case V4L2_SUBDEV_FORMAT_ACTIVE: 9069cae9722STodor Tomov return &ov5645->crop; 9079cae9722STodor Tomov default: 9089cae9722STodor Tomov return NULL; 9099cae9722STodor Tomov } 9109cae9722STodor Tomov } 9119cae9722STodor Tomov 9129cae9722STodor Tomov static int ov5645_set_format(struct v4l2_subdev *sd, 9130d346d2aSTomi Valkeinen struct v4l2_subdev_state *sd_state, 9149cae9722STodor Tomov struct v4l2_subdev_format *format) 9159cae9722STodor Tomov { 9169cae9722STodor Tomov struct ov5645 *ov5645 = to_ov5645(sd); 9179cae9722STodor Tomov struct v4l2_mbus_framefmt *__format; 9189cae9722STodor Tomov struct v4l2_rect *__crop; 9199cae9722STodor Tomov const struct ov5645_mode_info *new_mode; 9201c2177fdSTodor Tomov int ret; 9219cae9722STodor Tomov 9220d346d2aSTomi Valkeinen __crop = __ov5645_get_pad_crop(ov5645, sd_state, format->pad, 9239cae9722STodor Tomov format->which); 9249cae9722STodor Tomov 9255b81c53cSTodor Tomov new_mode = v4l2_find_nearest_size(ov5645_mode_info_data, 9265b81c53cSTodor Tomov ARRAY_SIZE(ov5645_mode_info_data), 9275b81c53cSTodor Tomov width, height, 9285b81c53cSTodor Tomov format->format.width, format->format.height); 9295b81c53cSTodor Tomov 9309cae9722STodor Tomov __crop->width = new_mode->width; 9319cae9722STodor Tomov __crop->height = new_mode->height; 9329cae9722STodor Tomov 9331c2177fdSTodor Tomov if (format->which == V4L2_SUBDEV_FORMAT_ACTIVE) { 9341c2177fdSTodor Tomov ret = v4l2_ctrl_s_ctrl_int64(ov5645->pixel_clock, 9351c2177fdSTodor Tomov new_mode->pixel_clock); 9361c2177fdSTodor Tomov if (ret < 0) 9371c2177fdSTodor Tomov return ret; 9381c2177fdSTodor Tomov 939e72e6cbeSTodor Tomov ret = v4l2_ctrl_s_ctrl(ov5645->link_freq, 940e72e6cbeSTodor Tomov new_mode->link_freq); 941e72e6cbeSTodor Tomov if (ret < 0) 942e72e6cbeSTodor Tomov return ret; 943e72e6cbeSTodor Tomov 9449cae9722STodor Tomov ov5645->current_mode = new_mode; 9451c2177fdSTodor Tomov } 9469cae9722STodor Tomov 9470d346d2aSTomi Valkeinen __format = __ov5645_get_pad_format(ov5645, sd_state, format->pad, 9489cae9722STodor Tomov format->which); 9499cae9722STodor Tomov __format->width = __crop->width; 9509cae9722STodor Tomov __format->height = __crop->height; 951ba449bb5SJacopo Mondi __format->code = MEDIA_BUS_FMT_UYVY8_1X16; 9529cae9722STodor Tomov __format->field = V4L2_FIELD_NONE; 9539cae9722STodor Tomov __format->colorspace = V4L2_COLORSPACE_SRGB; 9549cae9722STodor Tomov 9559cae9722STodor Tomov format->format = *__format; 9569cae9722STodor Tomov 9579cae9722STodor Tomov return 0; 9589cae9722STodor Tomov } 9599cae9722STodor Tomov 9609cae9722STodor Tomov static int ov5645_entity_init_cfg(struct v4l2_subdev *subdev, 9610d346d2aSTomi Valkeinen struct v4l2_subdev_state *sd_state) 9629cae9722STodor Tomov { 9639cae9722STodor Tomov struct v4l2_subdev_format fmt = { 0 }; 9649cae9722STodor Tomov 9650d346d2aSTomi Valkeinen fmt.which = sd_state ? V4L2_SUBDEV_FORMAT_TRY : V4L2_SUBDEV_FORMAT_ACTIVE; 9669cae9722STodor Tomov fmt.format.width = 1920; 9679cae9722STodor Tomov fmt.format.height = 1080; 9689cae9722STodor Tomov 9690d346d2aSTomi Valkeinen ov5645_set_format(subdev, sd_state, &fmt); 9709cae9722STodor Tomov 9719cae9722STodor Tomov return 0; 9729cae9722STodor Tomov } 9739cae9722STodor Tomov 9749cae9722STodor Tomov static int ov5645_get_selection(struct v4l2_subdev *sd, 9750d346d2aSTomi Valkeinen struct v4l2_subdev_state *sd_state, 9769cae9722STodor Tomov struct v4l2_subdev_selection *sel) 9779cae9722STodor Tomov { 9789cae9722STodor Tomov struct ov5645 *ov5645 = to_ov5645(sd); 9799cae9722STodor Tomov 9809cae9722STodor Tomov if (sel->target != V4L2_SEL_TGT_CROP) 9819cae9722STodor Tomov return -EINVAL; 9829cae9722STodor Tomov 9830d346d2aSTomi Valkeinen sel->r = *__ov5645_get_pad_crop(ov5645, sd_state, sel->pad, 9849cae9722STodor Tomov sel->which); 9859cae9722STodor Tomov return 0; 9869cae9722STodor Tomov } 9879cae9722STodor Tomov 9889cae9722STodor Tomov static int ov5645_s_stream(struct v4l2_subdev *subdev, int enable) 9899cae9722STodor Tomov { 9909cae9722STodor Tomov struct ov5645 *ov5645 = to_ov5645(subdev); 9919cae9722STodor Tomov int ret; 9929cae9722STodor Tomov 9939cae9722STodor Tomov if (enable) { 9949cae9722STodor Tomov ret = ov5645_set_register_array(ov5645, 9959cae9722STodor Tomov ov5645->current_mode->data, 9969cae9722STodor Tomov ov5645->current_mode->data_size); 9979cae9722STodor Tomov if (ret < 0) { 9989cae9722STodor Tomov dev_err(ov5645->dev, "could not set mode %dx%d\n", 9999cae9722STodor Tomov ov5645->current_mode->width, 10009cae9722STodor Tomov ov5645->current_mode->height); 10019cae9722STodor Tomov return ret; 10029cae9722STodor Tomov } 10039cae9722STodor Tomov ret = v4l2_ctrl_handler_setup(&ov5645->ctrls); 10049cae9722STodor Tomov if (ret < 0) { 10059cae9722STodor Tomov dev_err(ov5645->dev, "could not sync v4l2 controls\n"); 10069cae9722STodor Tomov return ret; 10079cae9722STodor Tomov } 1008092e8eb9SEzequiel Garcia 1009092e8eb9SEzequiel Garcia ret = ov5645_write_reg(ov5645, OV5645_IO_MIPI_CTRL00, 0x45); 1010092e8eb9SEzequiel Garcia if (ret < 0) 1011092e8eb9SEzequiel Garcia return ret; 1012092e8eb9SEzequiel Garcia 10139cae9722STodor Tomov ret = ov5645_write_reg(ov5645, OV5645_SYSTEM_CTRL0, 10149cae9722STodor Tomov OV5645_SYSTEM_CTRL0_START); 10159cae9722STodor Tomov if (ret < 0) 10169cae9722STodor Tomov return ret; 10179cae9722STodor Tomov } else { 1018092e8eb9SEzequiel Garcia ret = ov5645_write_reg(ov5645, OV5645_IO_MIPI_CTRL00, 0x40); 1019092e8eb9SEzequiel Garcia if (ret < 0) 1020092e8eb9SEzequiel Garcia return ret; 1021092e8eb9SEzequiel Garcia 10229cae9722STodor Tomov ret = ov5645_write_reg(ov5645, OV5645_SYSTEM_CTRL0, 10239cae9722STodor Tomov OV5645_SYSTEM_CTRL0_STOP); 10249cae9722STodor Tomov if (ret < 0) 10259cae9722STodor Tomov return ret; 10269cae9722STodor Tomov } 10279cae9722STodor Tomov 10289cae9722STodor Tomov return 0; 10299cae9722STodor Tomov } 10309cae9722STodor Tomov 10319cae9722STodor Tomov static const struct v4l2_subdev_core_ops ov5645_core_ops = { 10329cae9722STodor Tomov .s_power = ov5645_s_power, 10339cae9722STodor Tomov }; 10349cae9722STodor Tomov 10359cae9722STodor Tomov static const struct v4l2_subdev_video_ops ov5645_video_ops = { 10369cae9722STodor Tomov .s_stream = ov5645_s_stream, 10379cae9722STodor Tomov }; 10389cae9722STodor Tomov 10399cae9722STodor Tomov static const struct v4l2_subdev_pad_ops ov5645_subdev_pad_ops = { 10409cae9722STodor Tomov .init_cfg = ov5645_entity_init_cfg, 10419cae9722STodor Tomov .enum_mbus_code = ov5645_enum_mbus_code, 10429cae9722STodor Tomov .enum_frame_size = ov5645_enum_frame_size, 10439cae9722STodor Tomov .get_fmt = ov5645_get_format, 10449cae9722STodor Tomov .set_fmt = ov5645_set_format, 10459cae9722STodor Tomov .get_selection = ov5645_get_selection, 10469cae9722STodor Tomov }; 10479cae9722STodor Tomov 10489cae9722STodor Tomov static const struct v4l2_subdev_ops ov5645_subdev_ops = { 10499cae9722STodor Tomov .core = &ov5645_core_ops, 10509cae9722STodor Tomov .video = &ov5645_video_ops, 10519cae9722STodor Tomov .pad = &ov5645_subdev_pad_ops, 10529cae9722STodor Tomov }; 10539cae9722STodor Tomov 1054e6714993SKieran Bingham static int ov5645_probe(struct i2c_client *client) 10559cae9722STodor Tomov { 10569cae9722STodor Tomov struct device *dev = &client->dev; 10579cae9722STodor Tomov struct device_node *endpoint; 10589cae9722STodor Tomov struct ov5645 *ov5645; 10599cae9722STodor Tomov u8 chip_id_high, chip_id_low; 10604962bb38SFabio Estevam unsigned int i; 10619cae9722STodor Tomov u32 xclk_freq; 10629cae9722STodor Tomov int ret; 10639cae9722STodor Tomov 10649cae9722STodor Tomov ov5645 = devm_kzalloc(dev, sizeof(struct ov5645), GFP_KERNEL); 10659cae9722STodor Tomov if (!ov5645) 10669cae9722STodor Tomov return -ENOMEM; 10679cae9722STodor Tomov 10689cae9722STodor Tomov ov5645->i2c_client = client; 10699cae9722STodor Tomov ov5645->dev = dev; 10709cae9722STodor Tomov 10719cae9722STodor Tomov endpoint = of_graph_get_next_endpoint(dev->of_node, NULL); 10729cae9722STodor Tomov if (!endpoint) { 10739cae9722STodor Tomov dev_err(dev, "endpoint node not found\n"); 10749cae9722STodor Tomov return -EINVAL; 10759cae9722STodor Tomov } 10769cae9722STodor Tomov 1077859969b3SSakari Ailus ret = v4l2_fwnode_endpoint_parse(of_fwnode_handle(endpoint), 1078859969b3SSakari Ailus &ov5645->ep); 107906fe9323SAkinobu Mita 108006fe9323SAkinobu Mita of_node_put(endpoint); 108106fe9323SAkinobu Mita 10829cae9722STodor Tomov if (ret < 0) { 10839cae9722STodor Tomov dev_err(dev, "parsing endpoint node failed\n"); 10849cae9722STodor Tomov return ret; 10859cae9722STodor Tomov } 10869cae9722STodor Tomov 10872d95e7edSSakari Ailus if (ov5645->ep.bus_type != V4L2_MBUS_CSI2_DPHY) { 10889cae9722STodor Tomov dev_err(dev, "invalid bus type, must be CSI2\n"); 10899cae9722STodor Tomov return -EINVAL; 10909cae9722STodor Tomov } 10919cae9722STodor Tomov 10929cae9722STodor Tomov /* get system clock (xclk) */ 10939cae9722STodor Tomov ov5645->xclk = devm_clk_get(dev, "xclk"); 10949cae9722STodor Tomov if (IS_ERR(ov5645->xclk)) { 10959cae9722STodor Tomov dev_err(dev, "could not get xclk"); 10969cae9722STodor Tomov return PTR_ERR(ov5645->xclk); 10979cae9722STodor Tomov } 10989cae9722STodor Tomov 10999cae9722STodor Tomov ret = of_property_read_u32(dev->of_node, "clock-frequency", &xclk_freq); 11009cae9722STodor Tomov if (ret) { 11019cae9722STodor Tomov dev_err(dev, "could not get xclk frequency\n"); 11029cae9722STodor Tomov return ret; 11039cae9722STodor Tomov } 11049cae9722STodor Tomov 11054adb0a04STodor Tomov /* external clock must be 24MHz, allow 1% tolerance */ 11064adb0a04STodor Tomov if (xclk_freq < 23760000 || xclk_freq > 24240000) { 11079cae9722STodor Tomov dev_err(dev, "external clock frequency %u is not supported\n", 11089cae9722STodor Tomov xclk_freq); 11099cae9722STodor Tomov return -EINVAL; 11109cae9722STodor Tomov } 11119cae9722STodor Tomov 11129cae9722STodor Tomov ret = clk_set_rate(ov5645->xclk, xclk_freq); 11139cae9722STodor Tomov if (ret) { 11149cae9722STodor Tomov dev_err(dev, "could not set xclk frequency\n"); 11159cae9722STodor Tomov return ret; 11169cae9722STodor Tomov } 11179cae9722STodor Tomov 11184962bb38SFabio Estevam for (i = 0; i < OV5645_NUM_SUPPLIES; i++) 11194962bb38SFabio Estevam ov5645->supplies[i].supply = ov5645_supply_name[i]; 11209cae9722STodor Tomov 11214962bb38SFabio Estevam ret = devm_regulator_bulk_get(dev, OV5645_NUM_SUPPLIES, 11224962bb38SFabio Estevam ov5645->supplies); 11234962bb38SFabio Estevam if (ret < 0) 11244962bb38SFabio Estevam return ret; 11259cae9722STodor Tomov 11269cae9722STodor Tomov ov5645->enable_gpio = devm_gpiod_get(dev, "enable", GPIOD_OUT_HIGH); 11279cae9722STodor Tomov if (IS_ERR(ov5645->enable_gpio)) { 11289cae9722STodor Tomov dev_err(dev, "cannot get enable gpio\n"); 11299cae9722STodor Tomov return PTR_ERR(ov5645->enable_gpio); 11309cae9722STodor Tomov } 11319cae9722STodor Tomov 11329cae9722STodor Tomov ov5645->rst_gpio = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH); 11339cae9722STodor Tomov if (IS_ERR(ov5645->rst_gpio)) { 11349cae9722STodor Tomov dev_err(dev, "cannot get reset gpio\n"); 11359cae9722STodor Tomov return PTR_ERR(ov5645->rst_gpio); 11369cae9722STodor Tomov } 11379cae9722STodor Tomov 11389cae9722STodor Tomov mutex_init(&ov5645->power_lock); 11399cae9722STodor Tomov 1140e72e6cbeSTodor Tomov v4l2_ctrl_handler_init(&ov5645->ctrls, 9); 11419cae9722STodor Tomov v4l2_ctrl_new_std(&ov5645->ctrls, &ov5645_ctrl_ops, 11429cae9722STodor Tomov V4L2_CID_SATURATION, -4, 4, 1, 0); 11439cae9722STodor Tomov v4l2_ctrl_new_std(&ov5645->ctrls, &ov5645_ctrl_ops, 11449cae9722STodor Tomov V4L2_CID_HFLIP, 0, 1, 1, 0); 11459cae9722STodor Tomov v4l2_ctrl_new_std(&ov5645->ctrls, &ov5645_ctrl_ops, 11469cae9722STodor Tomov V4L2_CID_VFLIP, 0, 1, 1, 0); 11479cae9722STodor Tomov v4l2_ctrl_new_std(&ov5645->ctrls, &ov5645_ctrl_ops, 11489cae9722STodor Tomov V4L2_CID_AUTOGAIN, 0, 1, 1, 1); 11499cae9722STodor Tomov v4l2_ctrl_new_std(&ov5645->ctrls, &ov5645_ctrl_ops, 11509cae9722STodor Tomov V4L2_CID_AUTO_WHITE_BALANCE, 0, 1, 1, 1); 11519cae9722STodor Tomov v4l2_ctrl_new_std_menu(&ov5645->ctrls, &ov5645_ctrl_ops, 11529cae9722STodor Tomov V4L2_CID_EXPOSURE_AUTO, V4L2_EXPOSURE_MANUAL, 11539cae9722STodor Tomov 0, V4L2_EXPOSURE_AUTO); 11549cae9722STodor Tomov v4l2_ctrl_new_std_menu_items(&ov5645->ctrls, &ov5645_ctrl_ops, 11559cae9722STodor Tomov V4L2_CID_TEST_PATTERN, 11569cae9722STodor Tomov ARRAY_SIZE(ov5645_test_pattern_menu) - 1, 11579cae9722STodor Tomov 0, 0, ov5645_test_pattern_menu); 11581c2177fdSTodor Tomov ov5645->pixel_clock = v4l2_ctrl_new_std(&ov5645->ctrls, 11591c2177fdSTodor Tomov &ov5645_ctrl_ops, 11601c2177fdSTodor Tomov V4L2_CID_PIXEL_RATE, 11611c2177fdSTodor Tomov 1, INT_MAX, 1, 1); 1162e72e6cbeSTodor Tomov ov5645->link_freq = v4l2_ctrl_new_int_menu(&ov5645->ctrls, 1163e72e6cbeSTodor Tomov &ov5645_ctrl_ops, 1164e72e6cbeSTodor Tomov V4L2_CID_LINK_FREQ, 1165e72e6cbeSTodor Tomov ARRAY_SIZE(link_freq) - 1, 1166e72e6cbeSTodor Tomov 0, link_freq); 1167e72e6cbeSTodor Tomov if (ov5645->link_freq) 1168e72e6cbeSTodor Tomov ov5645->link_freq->flags |= V4L2_CTRL_FLAG_READ_ONLY; 11699cae9722STodor Tomov 11709cae9722STodor Tomov ov5645->sd.ctrl_handler = &ov5645->ctrls; 11719cae9722STodor Tomov 11729cae9722STodor Tomov if (ov5645->ctrls.error) { 11739cae9722STodor Tomov dev_err(dev, "%s: control initialization error %d\n", 11749cae9722STodor Tomov __func__, ov5645->ctrls.error); 11759cae9722STodor Tomov ret = ov5645->ctrls.error; 11769cae9722STodor Tomov goto free_ctrl; 11779cae9722STodor Tomov } 11789cae9722STodor Tomov 11799cae9722STodor Tomov v4l2_i2c_subdev_init(&ov5645->sd, client, &ov5645_subdev_ops); 11809cae9722STodor Tomov ov5645->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE; 11819cae9722STodor Tomov ov5645->pad.flags = MEDIA_PAD_FL_SOURCE; 11829cae9722STodor Tomov ov5645->sd.dev = &client->dev; 1183f7b51859STodor Tomov ov5645->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR; 11849cae9722STodor Tomov 11859cae9722STodor Tomov ret = media_entity_pads_init(&ov5645->sd.entity, 1, &ov5645->pad); 11869cae9722STodor Tomov if (ret < 0) { 11879cae9722STodor Tomov dev_err(dev, "could not register media entity\n"); 11889cae9722STodor Tomov goto free_ctrl; 11899cae9722STodor Tomov } 11909cae9722STodor Tomov 11919cae9722STodor Tomov ret = ov5645_s_power(&ov5645->sd, true); 11929cae9722STodor Tomov if (ret < 0) { 11939cae9722STodor Tomov dev_err(dev, "could not power up OV5645\n"); 11949cae9722STodor Tomov goto free_entity; 11959cae9722STodor Tomov } 11969cae9722STodor Tomov 11979cae9722STodor Tomov ret = ov5645_read_reg(ov5645, OV5645_CHIP_ID_HIGH, &chip_id_high); 11989cae9722STodor Tomov if (ret < 0 || chip_id_high != OV5645_CHIP_ID_HIGH_BYTE) { 11999cae9722STodor Tomov dev_err(dev, "could not read ID high\n"); 12009cae9722STodor Tomov ret = -ENODEV; 12019cae9722STodor Tomov goto power_down; 12029cae9722STodor Tomov } 12039cae9722STodor Tomov ret = ov5645_read_reg(ov5645, OV5645_CHIP_ID_LOW, &chip_id_low); 12049cae9722STodor Tomov if (ret < 0 || chip_id_low != OV5645_CHIP_ID_LOW_BYTE) { 12059cae9722STodor Tomov dev_err(dev, "could not read ID low\n"); 12069cae9722STodor Tomov ret = -ENODEV; 12079cae9722STodor Tomov goto power_down; 12089cae9722STodor Tomov } 12099cae9722STodor Tomov 12109cae9722STodor Tomov dev_info(dev, "OV5645 detected at address 0x%02x\n", client->addr); 12119cae9722STodor Tomov 12129cae9722STodor Tomov ret = ov5645_read_reg(ov5645, OV5645_AEC_PK_MANUAL, 12139cae9722STodor Tomov &ov5645->aec_pk_manual); 12149cae9722STodor Tomov if (ret < 0) { 12159cae9722STodor Tomov dev_err(dev, "could not read AEC/AGC mode\n"); 12169cae9722STodor Tomov ret = -ENODEV; 12179cae9722STodor Tomov goto power_down; 12189cae9722STodor Tomov } 12199cae9722STodor Tomov 12209cae9722STodor Tomov ret = ov5645_read_reg(ov5645, OV5645_TIMING_TC_REG20, 12219cae9722STodor Tomov &ov5645->timing_tc_reg20); 12229cae9722STodor Tomov if (ret < 0) { 12239cae9722STodor Tomov dev_err(dev, "could not read vflip value\n"); 12249cae9722STodor Tomov ret = -ENODEV; 12259cae9722STodor Tomov goto power_down; 12269cae9722STodor Tomov } 12279cae9722STodor Tomov 12289cae9722STodor Tomov ret = ov5645_read_reg(ov5645, OV5645_TIMING_TC_REG21, 12299cae9722STodor Tomov &ov5645->timing_tc_reg21); 12309cae9722STodor Tomov if (ret < 0) { 12319cae9722STodor Tomov dev_err(dev, "could not read hflip value\n"); 12329cae9722STodor Tomov ret = -ENODEV; 12339cae9722STodor Tomov goto power_down; 12349cae9722STodor Tomov } 12359cae9722STodor Tomov 12369cae9722STodor Tomov ov5645_s_power(&ov5645->sd, false); 12379cae9722STodor Tomov 12389cae9722STodor Tomov ret = v4l2_async_register_subdev(&ov5645->sd); 12399cae9722STodor Tomov if (ret < 0) { 12409cae9722STodor Tomov dev_err(dev, "could not register v4l2 device\n"); 12419cae9722STodor Tomov goto free_entity; 12429cae9722STodor Tomov } 12439cae9722STodor Tomov 12449cae9722STodor Tomov ov5645_entity_init_cfg(&ov5645->sd, NULL); 12459cae9722STodor Tomov 12469cae9722STodor Tomov return 0; 12479cae9722STodor Tomov 12489cae9722STodor Tomov power_down: 12499cae9722STodor Tomov ov5645_s_power(&ov5645->sd, false); 12509cae9722STodor Tomov free_entity: 12519cae9722STodor Tomov media_entity_cleanup(&ov5645->sd.entity); 12529cae9722STodor Tomov free_ctrl: 12539cae9722STodor Tomov v4l2_ctrl_handler_free(&ov5645->ctrls); 12549cae9722STodor Tomov mutex_destroy(&ov5645->power_lock); 12559cae9722STodor Tomov 12569cae9722STodor Tomov return ret; 12579cae9722STodor Tomov } 12589cae9722STodor Tomov 12599cae9722STodor Tomov static int ov5645_remove(struct i2c_client *client) 12609cae9722STodor Tomov { 12619cae9722STodor Tomov struct v4l2_subdev *sd = i2c_get_clientdata(client); 12629cae9722STodor Tomov struct ov5645 *ov5645 = to_ov5645(sd); 12639cae9722STodor Tomov 12649cae9722STodor Tomov v4l2_async_unregister_subdev(&ov5645->sd); 12659cae9722STodor Tomov media_entity_cleanup(&ov5645->sd.entity); 12669cae9722STodor Tomov v4l2_ctrl_handler_free(&ov5645->ctrls); 12679cae9722STodor Tomov mutex_destroy(&ov5645->power_lock); 12689cae9722STodor Tomov 12699cae9722STodor Tomov return 0; 12709cae9722STodor Tomov } 12719cae9722STodor Tomov 12729cae9722STodor Tomov static const struct i2c_device_id ov5645_id[] = { 12739cae9722STodor Tomov { "ov5645", 0 }, 12749cae9722STodor Tomov {} 12759cae9722STodor Tomov }; 12769cae9722STodor Tomov MODULE_DEVICE_TABLE(i2c, ov5645_id); 12779cae9722STodor Tomov 12789cae9722STodor Tomov static const struct of_device_id ov5645_of_match[] = { 12799cae9722STodor Tomov { .compatible = "ovti,ov5645" }, 12809cae9722STodor Tomov { /* sentinel */ } 12819cae9722STodor Tomov }; 12829cae9722STodor Tomov MODULE_DEVICE_TABLE(of, ov5645_of_match); 12839cae9722STodor Tomov 12849cae9722STodor Tomov static struct i2c_driver ov5645_i2c_driver = { 12859cae9722STodor Tomov .driver = { 1286*92beb555SMoses Christopher Bollavarapu .of_match_table = ov5645_of_match, 12879cae9722STodor Tomov .name = "ov5645", 12889cae9722STodor Tomov }, 1289e6714993SKieran Bingham .probe_new = 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