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