18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * Driver for MT9P031 CMOS Image Sensor from Aptina 48c2ecf20Sopenharmony_ci * 58c2ecf20Sopenharmony_ci * Copyright (C) 2011, Laurent Pinchart <laurent.pinchart@ideasonboard.com> 68c2ecf20Sopenharmony_ci * Copyright (C) 2011, Javier Martin <javier.martin@vista-silicon.com> 78c2ecf20Sopenharmony_ci * Copyright (C) 2011, Guennadi Liakhovetski <g.liakhovetski@gmx.de> 88c2ecf20Sopenharmony_ci * 98c2ecf20Sopenharmony_ci * Based on the MT9V032 driver and Bastian Hecht's code. 108c2ecf20Sopenharmony_ci */ 118c2ecf20Sopenharmony_ci 128c2ecf20Sopenharmony_ci#include <linux/clk.h> 138c2ecf20Sopenharmony_ci#include <linux/delay.h> 148c2ecf20Sopenharmony_ci#include <linux/device.h> 158c2ecf20Sopenharmony_ci#include <linux/gpio/consumer.h> 168c2ecf20Sopenharmony_ci#include <linux/i2c.h> 178c2ecf20Sopenharmony_ci#include <linux/log2.h> 188c2ecf20Sopenharmony_ci#include <linux/module.h> 198c2ecf20Sopenharmony_ci#include <linux/of.h> 208c2ecf20Sopenharmony_ci#include <linux/of_graph.h> 218c2ecf20Sopenharmony_ci#include <linux/pm.h> 228c2ecf20Sopenharmony_ci#include <linux/regulator/consumer.h> 238c2ecf20Sopenharmony_ci#include <linux/slab.h> 248c2ecf20Sopenharmony_ci#include <linux/videodev2.h> 258c2ecf20Sopenharmony_ci 268c2ecf20Sopenharmony_ci#include <media/i2c/mt9p031.h> 278c2ecf20Sopenharmony_ci#include <media/v4l2-async.h> 288c2ecf20Sopenharmony_ci#include <media/v4l2-ctrls.h> 298c2ecf20Sopenharmony_ci#include <media/v4l2-device.h> 308c2ecf20Sopenharmony_ci#include <media/v4l2-subdev.h> 318c2ecf20Sopenharmony_ci 328c2ecf20Sopenharmony_ci#include "aptina-pll.h" 338c2ecf20Sopenharmony_ci 348c2ecf20Sopenharmony_ci#define MT9P031_PIXEL_ARRAY_WIDTH 2752 358c2ecf20Sopenharmony_ci#define MT9P031_PIXEL_ARRAY_HEIGHT 2004 368c2ecf20Sopenharmony_ci 378c2ecf20Sopenharmony_ci#define MT9P031_CHIP_VERSION 0x00 388c2ecf20Sopenharmony_ci#define MT9P031_CHIP_VERSION_VALUE 0x1801 398c2ecf20Sopenharmony_ci#define MT9P031_ROW_START 0x01 408c2ecf20Sopenharmony_ci#define MT9P031_ROW_START_MIN 0 418c2ecf20Sopenharmony_ci#define MT9P031_ROW_START_MAX 2004 428c2ecf20Sopenharmony_ci#define MT9P031_ROW_START_DEF 54 438c2ecf20Sopenharmony_ci#define MT9P031_COLUMN_START 0x02 448c2ecf20Sopenharmony_ci#define MT9P031_COLUMN_START_MIN 0 458c2ecf20Sopenharmony_ci#define MT9P031_COLUMN_START_MAX 2750 468c2ecf20Sopenharmony_ci#define MT9P031_COLUMN_START_DEF 16 478c2ecf20Sopenharmony_ci#define MT9P031_WINDOW_HEIGHT 0x03 488c2ecf20Sopenharmony_ci#define MT9P031_WINDOW_HEIGHT_MIN 2 498c2ecf20Sopenharmony_ci#define MT9P031_WINDOW_HEIGHT_MAX 2006 508c2ecf20Sopenharmony_ci#define MT9P031_WINDOW_HEIGHT_DEF 1944 518c2ecf20Sopenharmony_ci#define MT9P031_WINDOW_WIDTH 0x04 528c2ecf20Sopenharmony_ci#define MT9P031_WINDOW_WIDTH_MIN 2 538c2ecf20Sopenharmony_ci#define MT9P031_WINDOW_WIDTH_MAX 2752 548c2ecf20Sopenharmony_ci#define MT9P031_WINDOW_WIDTH_DEF 2592 558c2ecf20Sopenharmony_ci#define MT9P031_HORIZONTAL_BLANK 0x05 568c2ecf20Sopenharmony_ci#define MT9P031_HORIZONTAL_BLANK_MIN 0 578c2ecf20Sopenharmony_ci#define MT9P031_HORIZONTAL_BLANK_MAX 4095 588c2ecf20Sopenharmony_ci#define MT9P031_VERTICAL_BLANK 0x06 598c2ecf20Sopenharmony_ci#define MT9P031_VERTICAL_BLANK_MIN 1 608c2ecf20Sopenharmony_ci#define MT9P031_VERTICAL_BLANK_MAX 4096 618c2ecf20Sopenharmony_ci#define MT9P031_VERTICAL_BLANK_DEF 26 628c2ecf20Sopenharmony_ci#define MT9P031_OUTPUT_CONTROL 0x07 638c2ecf20Sopenharmony_ci#define MT9P031_OUTPUT_CONTROL_CEN 2 648c2ecf20Sopenharmony_ci#define MT9P031_OUTPUT_CONTROL_SYN 1 658c2ecf20Sopenharmony_ci#define MT9P031_OUTPUT_CONTROL_DEF 0x1f82 668c2ecf20Sopenharmony_ci#define MT9P031_SHUTTER_WIDTH_UPPER 0x08 678c2ecf20Sopenharmony_ci#define MT9P031_SHUTTER_WIDTH_LOWER 0x09 688c2ecf20Sopenharmony_ci#define MT9P031_SHUTTER_WIDTH_MIN 1 698c2ecf20Sopenharmony_ci#define MT9P031_SHUTTER_WIDTH_MAX 1048575 708c2ecf20Sopenharmony_ci#define MT9P031_SHUTTER_WIDTH_DEF 1943 718c2ecf20Sopenharmony_ci#define MT9P031_PLL_CONTROL 0x10 728c2ecf20Sopenharmony_ci#define MT9P031_PLL_CONTROL_PWROFF 0x0050 738c2ecf20Sopenharmony_ci#define MT9P031_PLL_CONTROL_PWRON 0x0051 748c2ecf20Sopenharmony_ci#define MT9P031_PLL_CONTROL_USEPLL 0x0052 758c2ecf20Sopenharmony_ci#define MT9P031_PLL_CONFIG_1 0x11 768c2ecf20Sopenharmony_ci#define MT9P031_PLL_CONFIG_2 0x12 778c2ecf20Sopenharmony_ci#define MT9P031_PIXEL_CLOCK_CONTROL 0x0a 788c2ecf20Sopenharmony_ci#define MT9P031_PIXEL_CLOCK_INVERT (1 << 15) 798c2ecf20Sopenharmony_ci#define MT9P031_PIXEL_CLOCK_SHIFT(n) ((n) << 8) 808c2ecf20Sopenharmony_ci#define MT9P031_PIXEL_CLOCK_DIVIDE(n) ((n) << 0) 818c2ecf20Sopenharmony_ci#define MT9P031_RESTART 0x0b 828c2ecf20Sopenharmony_ci#define MT9P031_FRAME_PAUSE_RESTART (1 << 1) 838c2ecf20Sopenharmony_ci#define MT9P031_FRAME_RESTART (1 << 0) 848c2ecf20Sopenharmony_ci#define MT9P031_SHUTTER_DELAY 0x0c 858c2ecf20Sopenharmony_ci#define MT9P031_RST 0x0d 868c2ecf20Sopenharmony_ci#define MT9P031_RST_ENABLE 1 878c2ecf20Sopenharmony_ci#define MT9P031_RST_DISABLE 0 888c2ecf20Sopenharmony_ci#define MT9P031_READ_MODE_1 0x1e 898c2ecf20Sopenharmony_ci#define MT9P031_READ_MODE_2 0x20 908c2ecf20Sopenharmony_ci#define MT9P031_READ_MODE_2_ROW_MIR (1 << 15) 918c2ecf20Sopenharmony_ci#define MT9P031_READ_MODE_2_COL_MIR (1 << 14) 928c2ecf20Sopenharmony_ci#define MT9P031_READ_MODE_2_ROW_BLC (1 << 6) 938c2ecf20Sopenharmony_ci#define MT9P031_ROW_ADDRESS_MODE 0x22 948c2ecf20Sopenharmony_ci#define MT9P031_COLUMN_ADDRESS_MODE 0x23 958c2ecf20Sopenharmony_ci#define MT9P031_GLOBAL_GAIN 0x35 968c2ecf20Sopenharmony_ci#define MT9P031_GLOBAL_GAIN_MIN 8 978c2ecf20Sopenharmony_ci#define MT9P031_GLOBAL_GAIN_MAX 1024 988c2ecf20Sopenharmony_ci#define MT9P031_GLOBAL_GAIN_DEF 8 998c2ecf20Sopenharmony_ci#define MT9P031_GLOBAL_GAIN_MULT (1 << 6) 1008c2ecf20Sopenharmony_ci#define MT9P031_ROW_BLACK_TARGET 0x49 1018c2ecf20Sopenharmony_ci#define MT9P031_ROW_BLACK_DEF_OFFSET 0x4b 1028c2ecf20Sopenharmony_ci#define MT9P031_GREEN1_OFFSET 0x60 1038c2ecf20Sopenharmony_ci#define MT9P031_GREEN2_OFFSET 0x61 1048c2ecf20Sopenharmony_ci#define MT9P031_BLACK_LEVEL_CALIBRATION 0x62 1058c2ecf20Sopenharmony_ci#define MT9P031_BLC_MANUAL_BLC (1 << 0) 1068c2ecf20Sopenharmony_ci#define MT9P031_RED_OFFSET 0x63 1078c2ecf20Sopenharmony_ci#define MT9P031_BLUE_OFFSET 0x64 1088c2ecf20Sopenharmony_ci#define MT9P031_TEST_PATTERN 0xa0 1098c2ecf20Sopenharmony_ci#define MT9P031_TEST_PATTERN_SHIFT 3 1108c2ecf20Sopenharmony_ci#define MT9P031_TEST_PATTERN_ENABLE (1 << 0) 1118c2ecf20Sopenharmony_ci#define MT9P031_TEST_PATTERN_DISABLE (0 << 0) 1128c2ecf20Sopenharmony_ci#define MT9P031_TEST_PATTERN_GREEN 0xa1 1138c2ecf20Sopenharmony_ci#define MT9P031_TEST_PATTERN_RED 0xa2 1148c2ecf20Sopenharmony_ci#define MT9P031_TEST_PATTERN_BLUE 0xa3 1158c2ecf20Sopenharmony_ci 1168c2ecf20Sopenharmony_cienum mt9p031_model { 1178c2ecf20Sopenharmony_ci MT9P031_MODEL_COLOR, 1188c2ecf20Sopenharmony_ci MT9P031_MODEL_MONOCHROME, 1198c2ecf20Sopenharmony_ci}; 1208c2ecf20Sopenharmony_ci 1218c2ecf20Sopenharmony_cistruct mt9p031 { 1228c2ecf20Sopenharmony_ci struct v4l2_subdev subdev; 1238c2ecf20Sopenharmony_ci struct media_pad pad; 1248c2ecf20Sopenharmony_ci struct v4l2_rect crop; /* Sensor window */ 1258c2ecf20Sopenharmony_ci struct v4l2_mbus_framefmt format; 1268c2ecf20Sopenharmony_ci struct mt9p031_platform_data *pdata; 1278c2ecf20Sopenharmony_ci struct mutex power_lock; /* lock to protect power_count */ 1288c2ecf20Sopenharmony_ci int power_count; 1298c2ecf20Sopenharmony_ci 1308c2ecf20Sopenharmony_ci struct clk *clk; 1318c2ecf20Sopenharmony_ci struct regulator_bulk_data regulators[3]; 1328c2ecf20Sopenharmony_ci 1338c2ecf20Sopenharmony_ci enum mt9p031_model model; 1348c2ecf20Sopenharmony_ci struct aptina_pll pll; 1358c2ecf20Sopenharmony_ci unsigned int clk_div; 1368c2ecf20Sopenharmony_ci bool use_pll; 1378c2ecf20Sopenharmony_ci struct gpio_desc *reset; 1388c2ecf20Sopenharmony_ci 1398c2ecf20Sopenharmony_ci struct v4l2_ctrl_handler ctrls; 1408c2ecf20Sopenharmony_ci struct v4l2_ctrl *blc_auto; 1418c2ecf20Sopenharmony_ci struct v4l2_ctrl *blc_offset; 1428c2ecf20Sopenharmony_ci 1438c2ecf20Sopenharmony_ci /* Registers cache */ 1448c2ecf20Sopenharmony_ci u16 output_control; 1458c2ecf20Sopenharmony_ci u16 mode2; 1468c2ecf20Sopenharmony_ci}; 1478c2ecf20Sopenharmony_ci 1488c2ecf20Sopenharmony_cistatic struct mt9p031 *to_mt9p031(struct v4l2_subdev *sd) 1498c2ecf20Sopenharmony_ci{ 1508c2ecf20Sopenharmony_ci return container_of(sd, struct mt9p031, subdev); 1518c2ecf20Sopenharmony_ci} 1528c2ecf20Sopenharmony_ci 1538c2ecf20Sopenharmony_cistatic int mt9p031_read(struct i2c_client *client, u8 reg) 1548c2ecf20Sopenharmony_ci{ 1558c2ecf20Sopenharmony_ci return i2c_smbus_read_word_swapped(client, reg); 1568c2ecf20Sopenharmony_ci} 1578c2ecf20Sopenharmony_ci 1588c2ecf20Sopenharmony_cistatic int mt9p031_write(struct i2c_client *client, u8 reg, u16 data) 1598c2ecf20Sopenharmony_ci{ 1608c2ecf20Sopenharmony_ci return i2c_smbus_write_word_swapped(client, reg, data); 1618c2ecf20Sopenharmony_ci} 1628c2ecf20Sopenharmony_ci 1638c2ecf20Sopenharmony_cistatic int mt9p031_set_output_control(struct mt9p031 *mt9p031, u16 clear, 1648c2ecf20Sopenharmony_ci u16 set) 1658c2ecf20Sopenharmony_ci{ 1668c2ecf20Sopenharmony_ci struct i2c_client *client = v4l2_get_subdevdata(&mt9p031->subdev); 1678c2ecf20Sopenharmony_ci u16 value = (mt9p031->output_control & ~clear) | set; 1688c2ecf20Sopenharmony_ci int ret; 1698c2ecf20Sopenharmony_ci 1708c2ecf20Sopenharmony_ci ret = mt9p031_write(client, MT9P031_OUTPUT_CONTROL, value); 1718c2ecf20Sopenharmony_ci if (ret < 0) 1728c2ecf20Sopenharmony_ci return ret; 1738c2ecf20Sopenharmony_ci 1748c2ecf20Sopenharmony_ci mt9p031->output_control = value; 1758c2ecf20Sopenharmony_ci return 0; 1768c2ecf20Sopenharmony_ci} 1778c2ecf20Sopenharmony_ci 1788c2ecf20Sopenharmony_cistatic int mt9p031_set_mode2(struct mt9p031 *mt9p031, u16 clear, u16 set) 1798c2ecf20Sopenharmony_ci{ 1808c2ecf20Sopenharmony_ci struct i2c_client *client = v4l2_get_subdevdata(&mt9p031->subdev); 1818c2ecf20Sopenharmony_ci u16 value = (mt9p031->mode2 & ~clear) | set; 1828c2ecf20Sopenharmony_ci int ret; 1838c2ecf20Sopenharmony_ci 1848c2ecf20Sopenharmony_ci ret = mt9p031_write(client, MT9P031_READ_MODE_2, value); 1858c2ecf20Sopenharmony_ci if (ret < 0) 1868c2ecf20Sopenharmony_ci return ret; 1878c2ecf20Sopenharmony_ci 1888c2ecf20Sopenharmony_ci mt9p031->mode2 = value; 1898c2ecf20Sopenharmony_ci return 0; 1908c2ecf20Sopenharmony_ci} 1918c2ecf20Sopenharmony_ci 1928c2ecf20Sopenharmony_cistatic int mt9p031_reset(struct mt9p031 *mt9p031) 1938c2ecf20Sopenharmony_ci{ 1948c2ecf20Sopenharmony_ci struct i2c_client *client = v4l2_get_subdevdata(&mt9p031->subdev); 1958c2ecf20Sopenharmony_ci int ret; 1968c2ecf20Sopenharmony_ci 1978c2ecf20Sopenharmony_ci /* Disable chip output, synchronous option update */ 1988c2ecf20Sopenharmony_ci ret = mt9p031_write(client, MT9P031_RST, MT9P031_RST_ENABLE); 1998c2ecf20Sopenharmony_ci if (ret < 0) 2008c2ecf20Sopenharmony_ci return ret; 2018c2ecf20Sopenharmony_ci ret = mt9p031_write(client, MT9P031_RST, MT9P031_RST_DISABLE); 2028c2ecf20Sopenharmony_ci if (ret < 0) 2038c2ecf20Sopenharmony_ci return ret; 2048c2ecf20Sopenharmony_ci 2058c2ecf20Sopenharmony_ci ret = mt9p031_write(client, MT9P031_PIXEL_CLOCK_CONTROL, 2068c2ecf20Sopenharmony_ci MT9P031_PIXEL_CLOCK_DIVIDE(mt9p031->clk_div)); 2078c2ecf20Sopenharmony_ci if (ret < 0) 2088c2ecf20Sopenharmony_ci return ret; 2098c2ecf20Sopenharmony_ci 2108c2ecf20Sopenharmony_ci return mt9p031_set_output_control(mt9p031, MT9P031_OUTPUT_CONTROL_CEN, 2118c2ecf20Sopenharmony_ci 0); 2128c2ecf20Sopenharmony_ci} 2138c2ecf20Sopenharmony_ci 2148c2ecf20Sopenharmony_cistatic int mt9p031_clk_setup(struct mt9p031 *mt9p031) 2158c2ecf20Sopenharmony_ci{ 2168c2ecf20Sopenharmony_ci static const struct aptina_pll_limits limits = { 2178c2ecf20Sopenharmony_ci .ext_clock_min = 6000000, 2188c2ecf20Sopenharmony_ci .ext_clock_max = 27000000, 2198c2ecf20Sopenharmony_ci .int_clock_min = 2000000, 2208c2ecf20Sopenharmony_ci .int_clock_max = 13500000, 2218c2ecf20Sopenharmony_ci .out_clock_min = 180000000, 2228c2ecf20Sopenharmony_ci .out_clock_max = 360000000, 2238c2ecf20Sopenharmony_ci .pix_clock_max = 96000000, 2248c2ecf20Sopenharmony_ci .n_min = 1, 2258c2ecf20Sopenharmony_ci .n_max = 64, 2268c2ecf20Sopenharmony_ci .m_min = 16, 2278c2ecf20Sopenharmony_ci .m_max = 255, 2288c2ecf20Sopenharmony_ci .p1_min = 1, 2298c2ecf20Sopenharmony_ci .p1_max = 128, 2308c2ecf20Sopenharmony_ci }; 2318c2ecf20Sopenharmony_ci 2328c2ecf20Sopenharmony_ci struct i2c_client *client = v4l2_get_subdevdata(&mt9p031->subdev); 2338c2ecf20Sopenharmony_ci struct mt9p031_platform_data *pdata = mt9p031->pdata; 2348c2ecf20Sopenharmony_ci int ret; 2358c2ecf20Sopenharmony_ci 2368c2ecf20Sopenharmony_ci mt9p031->clk = devm_clk_get(&client->dev, NULL); 2378c2ecf20Sopenharmony_ci if (IS_ERR(mt9p031->clk)) 2388c2ecf20Sopenharmony_ci return PTR_ERR(mt9p031->clk); 2398c2ecf20Sopenharmony_ci 2408c2ecf20Sopenharmony_ci ret = clk_set_rate(mt9p031->clk, pdata->ext_freq); 2418c2ecf20Sopenharmony_ci if (ret < 0) 2428c2ecf20Sopenharmony_ci return ret; 2438c2ecf20Sopenharmony_ci 2448c2ecf20Sopenharmony_ci /* If the external clock frequency is out of bounds for the PLL use the 2458c2ecf20Sopenharmony_ci * pixel clock divider only and disable the PLL. 2468c2ecf20Sopenharmony_ci */ 2478c2ecf20Sopenharmony_ci if (pdata->ext_freq > limits.ext_clock_max) { 2488c2ecf20Sopenharmony_ci unsigned int div; 2498c2ecf20Sopenharmony_ci 2508c2ecf20Sopenharmony_ci div = DIV_ROUND_UP(pdata->ext_freq, pdata->target_freq); 2518c2ecf20Sopenharmony_ci div = roundup_pow_of_two(div) / 2; 2528c2ecf20Sopenharmony_ci 2538c2ecf20Sopenharmony_ci mt9p031->clk_div = min_t(unsigned int, div, 64); 2548c2ecf20Sopenharmony_ci mt9p031->use_pll = false; 2558c2ecf20Sopenharmony_ci 2568c2ecf20Sopenharmony_ci return 0; 2578c2ecf20Sopenharmony_ci } 2588c2ecf20Sopenharmony_ci 2598c2ecf20Sopenharmony_ci mt9p031->pll.ext_clock = pdata->ext_freq; 2608c2ecf20Sopenharmony_ci mt9p031->pll.pix_clock = pdata->target_freq; 2618c2ecf20Sopenharmony_ci mt9p031->use_pll = true; 2628c2ecf20Sopenharmony_ci 2638c2ecf20Sopenharmony_ci return aptina_pll_calculate(&client->dev, &limits, &mt9p031->pll); 2648c2ecf20Sopenharmony_ci} 2658c2ecf20Sopenharmony_ci 2668c2ecf20Sopenharmony_cistatic int mt9p031_pll_enable(struct mt9p031 *mt9p031) 2678c2ecf20Sopenharmony_ci{ 2688c2ecf20Sopenharmony_ci struct i2c_client *client = v4l2_get_subdevdata(&mt9p031->subdev); 2698c2ecf20Sopenharmony_ci int ret; 2708c2ecf20Sopenharmony_ci 2718c2ecf20Sopenharmony_ci if (!mt9p031->use_pll) 2728c2ecf20Sopenharmony_ci return 0; 2738c2ecf20Sopenharmony_ci 2748c2ecf20Sopenharmony_ci ret = mt9p031_write(client, MT9P031_PLL_CONTROL, 2758c2ecf20Sopenharmony_ci MT9P031_PLL_CONTROL_PWRON); 2768c2ecf20Sopenharmony_ci if (ret < 0) 2778c2ecf20Sopenharmony_ci return ret; 2788c2ecf20Sopenharmony_ci 2798c2ecf20Sopenharmony_ci ret = mt9p031_write(client, MT9P031_PLL_CONFIG_1, 2808c2ecf20Sopenharmony_ci (mt9p031->pll.m << 8) | (mt9p031->pll.n - 1)); 2818c2ecf20Sopenharmony_ci if (ret < 0) 2828c2ecf20Sopenharmony_ci return ret; 2838c2ecf20Sopenharmony_ci 2848c2ecf20Sopenharmony_ci ret = mt9p031_write(client, MT9P031_PLL_CONFIG_2, mt9p031->pll.p1 - 1); 2858c2ecf20Sopenharmony_ci if (ret < 0) 2868c2ecf20Sopenharmony_ci return ret; 2878c2ecf20Sopenharmony_ci 2888c2ecf20Sopenharmony_ci usleep_range(1000, 2000); 2898c2ecf20Sopenharmony_ci ret = mt9p031_write(client, MT9P031_PLL_CONTROL, 2908c2ecf20Sopenharmony_ci MT9P031_PLL_CONTROL_PWRON | 2918c2ecf20Sopenharmony_ci MT9P031_PLL_CONTROL_USEPLL); 2928c2ecf20Sopenharmony_ci return ret; 2938c2ecf20Sopenharmony_ci} 2948c2ecf20Sopenharmony_ci 2958c2ecf20Sopenharmony_cistatic inline int mt9p031_pll_disable(struct mt9p031 *mt9p031) 2968c2ecf20Sopenharmony_ci{ 2978c2ecf20Sopenharmony_ci struct i2c_client *client = v4l2_get_subdevdata(&mt9p031->subdev); 2988c2ecf20Sopenharmony_ci 2998c2ecf20Sopenharmony_ci if (!mt9p031->use_pll) 3008c2ecf20Sopenharmony_ci return 0; 3018c2ecf20Sopenharmony_ci 3028c2ecf20Sopenharmony_ci return mt9p031_write(client, MT9P031_PLL_CONTROL, 3038c2ecf20Sopenharmony_ci MT9P031_PLL_CONTROL_PWROFF); 3048c2ecf20Sopenharmony_ci} 3058c2ecf20Sopenharmony_ci 3068c2ecf20Sopenharmony_cistatic int mt9p031_power_on(struct mt9p031 *mt9p031) 3078c2ecf20Sopenharmony_ci{ 3088c2ecf20Sopenharmony_ci int ret; 3098c2ecf20Sopenharmony_ci 3108c2ecf20Sopenharmony_ci /* Ensure RESET_BAR is active */ 3118c2ecf20Sopenharmony_ci if (mt9p031->reset) { 3128c2ecf20Sopenharmony_ci gpiod_set_value(mt9p031->reset, 1); 3138c2ecf20Sopenharmony_ci usleep_range(1000, 2000); 3148c2ecf20Sopenharmony_ci } 3158c2ecf20Sopenharmony_ci 3168c2ecf20Sopenharmony_ci /* Bring up the supplies */ 3178c2ecf20Sopenharmony_ci ret = regulator_bulk_enable(ARRAY_SIZE(mt9p031->regulators), 3188c2ecf20Sopenharmony_ci mt9p031->regulators); 3198c2ecf20Sopenharmony_ci if (ret < 0) 3208c2ecf20Sopenharmony_ci return ret; 3218c2ecf20Sopenharmony_ci 3228c2ecf20Sopenharmony_ci /* Enable clock */ 3238c2ecf20Sopenharmony_ci if (mt9p031->clk) { 3248c2ecf20Sopenharmony_ci ret = clk_prepare_enable(mt9p031->clk); 3258c2ecf20Sopenharmony_ci if (ret) { 3268c2ecf20Sopenharmony_ci regulator_bulk_disable(ARRAY_SIZE(mt9p031->regulators), 3278c2ecf20Sopenharmony_ci mt9p031->regulators); 3288c2ecf20Sopenharmony_ci return ret; 3298c2ecf20Sopenharmony_ci } 3308c2ecf20Sopenharmony_ci } 3318c2ecf20Sopenharmony_ci 3328c2ecf20Sopenharmony_ci /* Now RESET_BAR must be high */ 3338c2ecf20Sopenharmony_ci if (mt9p031->reset) { 3348c2ecf20Sopenharmony_ci gpiod_set_value(mt9p031->reset, 0); 3358c2ecf20Sopenharmony_ci usleep_range(1000, 2000); 3368c2ecf20Sopenharmony_ci } 3378c2ecf20Sopenharmony_ci 3388c2ecf20Sopenharmony_ci return 0; 3398c2ecf20Sopenharmony_ci} 3408c2ecf20Sopenharmony_ci 3418c2ecf20Sopenharmony_cistatic void mt9p031_power_off(struct mt9p031 *mt9p031) 3428c2ecf20Sopenharmony_ci{ 3438c2ecf20Sopenharmony_ci if (mt9p031->reset) { 3448c2ecf20Sopenharmony_ci gpiod_set_value(mt9p031->reset, 1); 3458c2ecf20Sopenharmony_ci usleep_range(1000, 2000); 3468c2ecf20Sopenharmony_ci } 3478c2ecf20Sopenharmony_ci 3488c2ecf20Sopenharmony_ci regulator_bulk_disable(ARRAY_SIZE(mt9p031->regulators), 3498c2ecf20Sopenharmony_ci mt9p031->regulators); 3508c2ecf20Sopenharmony_ci 3518c2ecf20Sopenharmony_ci if (mt9p031->clk) 3528c2ecf20Sopenharmony_ci clk_disable_unprepare(mt9p031->clk); 3538c2ecf20Sopenharmony_ci} 3548c2ecf20Sopenharmony_ci 3558c2ecf20Sopenharmony_cistatic int __mt9p031_set_power(struct mt9p031 *mt9p031, bool on) 3568c2ecf20Sopenharmony_ci{ 3578c2ecf20Sopenharmony_ci struct i2c_client *client = v4l2_get_subdevdata(&mt9p031->subdev); 3588c2ecf20Sopenharmony_ci int ret; 3598c2ecf20Sopenharmony_ci 3608c2ecf20Sopenharmony_ci if (!on) { 3618c2ecf20Sopenharmony_ci mt9p031_power_off(mt9p031); 3628c2ecf20Sopenharmony_ci return 0; 3638c2ecf20Sopenharmony_ci } 3648c2ecf20Sopenharmony_ci 3658c2ecf20Sopenharmony_ci ret = mt9p031_power_on(mt9p031); 3668c2ecf20Sopenharmony_ci if (ret < 0) 3678c2ecf20Sopenharmony_ci return ret; 3688c2ecf20Sopenharmony_ci 3698c2ecf20Sopenharmony_ci ret = mt9p031_reset(mt9p031); 3708c2ecf20Sopenharmony_ci if (ret < 0) { 3718c2ecf20Sopenharmony_ci dev_err(&client->dev, "Failed to reset the camera\n"); 3728c2ecf20Sopenharmony_ci return ret; 3738c2ecf20Sopenharmony_ci } 3748c2ecf20Sopenharmony_ci 3758c2ecf20Sopenharmony_ci return v4l2_ctrl_handler_setup(&mt9p031->ctrls); 3768c2ecf20Sopenharmony_ci} 3778c2ecf20Sopenharmony_ci 3788c2ecf20Sopenharmony_ci/* ----------------------------------------------------------------------------- 3798c2ecf20Sopenharmony_ci * V4L2 subdev video operations 3808c2ecf20Sopenharmony_ci */ 3818c2ecf20Sopenharmony_ci 3828c2ecf20Sopenharmony_cistatic int mt9p031_set_params(struct mt9p031 *mt9p031) 3838c2ecf20Sopenharmony_ci{ 3848c2ecf20Sopenharmony_ci struct i2c_client *client = v4l2_get_subdevdata(&mt9p031->subdev); 3858c2ecf20Sopenharmony_ci struct v4l2_mbus_framefmt *format = &mt9p031->format; 3868c2ecf20Sopenharmony_ci const struct v4l2_rect *crop = &mt9p031->crop; 3878c2ecf20Sopenharmony_ci unsigned int hblank; 3888c2ecf20Sopenharmony_ci unsigned int vblank; 3898c2ecf20Sopenharmony_ci unsigned int xskip; 3908c2ecf20Sopenharmony_ci unsigned int yskip; 3918c2ecf20Sopenharmony_ci unsigned int xbin; 3928c2ecf20Sopenharmony_ci unsigned int ybin; 3938c2ecf20Sopenharmony_ci int ret; 3948c2ecf20Sopenharmony_ci 3958c2ecf20Sopenharmony_ci /* Windows position and size. 3968c2ecf20Sopenharmony_ci * 3978c2ecf20Sopenharmony_ci * TODO: Make sure the start coordinates and window size match the 3988c2ecf20Sopenharmony_ci * skipping, binning and mirroring (see description of registers 2 and 4 3998c2ecf20Sopenharmony_ci * in table 13, and Binning section on page 41). 4008c2ecf20Sopenharmony_ci */ 4018c2ecf20Sopenharmony_ci ret = mt9p031_write(client, MT9P031_COLUMN_START, crop->left); 4028c2ecf20Sopenharmony_ci if (ret < 0) 4038c2ecf20Sopenharmony_ci return ret; 4048c2ecf20Sopenharmony_ci ret = mt9p031_write(client, MT9P031_ROW_START, crop->top); 4058c2ecf20Sopenharmony_ci if (ret < 0) 4068c2ecf20Sopenharmony_ci return ret; 4078c2ecf20Sopenharmony_ci ret = mt9p031_write(client, MT9P031_WINDOW_WIDTH, crop->width - 1); 4088c2ecf20Sopenharmony_ci if (ret < 0) 4098c2ecf20Sopenharmony_ci return ret; 4108c2ecf20Sopenharmony_ci ret = mt9p031_write(client, MT9P031_WINDOW_HEIGHT, crop->height - 1); 4118c2ecf20Sopenharmony_ci if (ret < 0) 4128c2ecf20Sopenharmony_ci return ret; 4138c2ecf20Sopenharmony_ci 4148c2ecf20Sopenharmony_ci /* Row and column binning and skipping. Use the maximum binning value 4158c2ecf20Sopenharmony_ci * compatible with the skipping settings. 4168c2ecf20Sopenharmony_ci */ 4178c2ecf20Sopenharmony_ci xskip = DIV_ROUND_CLOSEST(crop->width, format->width); 4188c2ecf20Sopenharmony_ci yskip = DIV_ROUND_CLOSEST(crop->height, format->height); 4198c2ecf20Sopenharmony_ci xbin = 1 << (ffs(xskip) - 1); 4208c2ecf20Sopenharmony_ci ybin = 1 << (ffs(yskip) - 1); 4218c2ecf20Sopenharmony_ci 4228c2ecf20Sopenharmony_ci ret = mt9p031_write(client, MT9P031_COLUMN_ADDRESS_MODE, 4238c2ecf20Sopenharmony_ci ((xbin - 1) << 4) | (xskip - 1)); 4248c2ecf20Sopenharmony_ci if (ret < 0) 4258c2ecf20Sopenharmony_ci return ret; 4268c2ecf20Sopenharmony_ci ret = mt9p031_write(client, MT9P031_ROW_ADDRESS_MODE, 4278c2ecf20Sopenharmony_ci ((ybin - 1) << 4) | (yskip - 1)); 4288c2ecf20Sopenharmony_ci if (ret < 0) 4298c2ecf20Sopenharmony_ci return ret; 4308c2ecf20Sopenharmony_ci 4318c2ecf20Sopenharmony_ci /* Blanking - use minimum value for horizontal blanking and default 4328c2ecf20Sopenharmony_ci * value for vertical blanking. 4338c2ecf20Sopenharmony_ci */ 4348c2ecf20Sopenharmony_ci hblank = 346 * ybin + 64 + (80 >> min_t(unsigned int, xbin, 3)); 4358c2ecf20Sopenharmony_ci vblank = MT9P031_VERTICAL_BLANK_DEF; 4368c2ecf20Sopenharmony_ci 4378c2ecf20Sopenharmony_ci ret = mt9p031_write(client, MT9P031_HORIZONTAL_BLANK, hblank - 1); 4388c2ecf20Sopenharmony_ci if (ret < 0) 4398c2ecf20Sopenharmony_ci return ret; 4408c2ecf20Sopenharmony_ci ret = mt9p031_write(client, MT9P031_VERTICAL_BLANK, vblank - 1); 4418c2ecf20Sopenharmony_ci if (ret < 0) 4428c2ecf20Sopenharmony_ci return ret; 4438c2ecf20Sopenharmony_ci 4448c2ecf20Sopenharmony_ci return ret; 4458c2ecf20Sopenharmony_ci} 4468c2ecf20Sopenharmony_ci 4478c2ecf20Sopenharmony_cistatic int mt9p031_s_stream(struct v4l2_subdev *subdev, int enable) 4488c2ecf20Sopenharmony_ci{ 4498c2ecf20Sopenharmony_ci struct mt9p031 *mt9p031 = to_mt9p031(subdev); 4508c2ecf20Sopenharmony_ci struct i2c_client *client = v4l2_get_subdevdata(subdev); 4518c2ecf20Sopenharmony_ci int val; 4528c2ecf20Sopenharmony_ci int ret; 4538c2ecf20Sopenharmony_ci 4548c2ecf20Sopenharmony_ci if (!enable) { 4558c2ecf20Sopenharmony_ci /* enable pause restart */ 4568c2ecf20Sopenharmony_ci val = MT9P031_FRAME_PAUSE_RESTART; 4578c2ecf20Sopenharmony_ci ret = mt9p031_write(client, MT9P031_RESTART, val); 4588c2ecf20Sopenharmony_ci if (ret < 0) 4598c2ecf20Sopenharmony_ci return ret; 4608c2ecf20Sopenharmony_ci 4618c2ecf20Sopenharmony_ci /* enable restart + keep pause restart set */ 4628c2ecf20Sopenharmony_ci val |= MT9P031_FRAME_RESTART; 4638c2ecf20Sopenharmony_ci ret = mt9p031_write(client, MT9P031_RESTART, val); 4648c2ecf20Sopenharmony_ci if (ret < 0) 4658c2ecf20Sopenharmony_ci return ret; 4668c2ecf20Sopenharmony_ci 4678c2ecf20Sopenharmony_ci /* Stop sensor readout */ 4688c2ecf20Sopenharmony_ci ret = mt9p031_set_output_control(mt9p031, 4698c2ecf20Sopenharmony_ci MT9P031_OUTPUT_CONTROL_CEN, 0); 4708c2ecf20Sopenharmony_ci if (ret < 0) 4718c2ecf20Sopenharmony_ci return ret; 4728c2ecf20Sopenharmony_ci 4738c2ecf20Sopenharmony_ci return mt9p031_pll_disable(mt9p031); 4748c2ecf20Sopenharmony_ci } 4758c2ecf20Sopenharmony_ci 4768c2ecf20Sopenharmony_ci ret = mt9p031_set_params(mt9p031); 4778c2ecf20Sopenharmony_ci if (ret < 0) 4788c2ecf20Sopenharmony_ci return ret; 4798c2ecf20Sopenharmony_ci 4808c2ecf20Sopenharmony_ci /* Switch to master "normal" mode */ 4818c2ecf20Sopenharmony_ci ret = mt9p031_set_output_control(mt9p031, 0, 4828c2ecf20Sopenharmony_ci MT9P031_OUTPUT_CONTROL_CEN); 4838c2ecf20Sopenharmony_ci if (ret < 0) 4848c2ecf20Sopenharmony_ci return ret; 4858c2ecf20Sopenharmony_ci 4868c2ecf20Sopenharmony_ci /* 4878c2ecf20Sopenharmony_ci * - clear pause restart 4888c2ecf20Sopenharmony_ci * - don't clear restart as clearing restart manually can cause 4898c2ecf20Sopenharmony_ci * undefined behavior 4908c2ecf20Sopenharmony_ci */ 4918c2ecf20Sopenharmony_ci val = MT9P031_FRAME_RESTART; 4928c2ecf20Sopenharmony_ci ret = mt9p031_write(client, MT9P031_RESTART, val); 4938c2ecf20Sopenharmony_ci if (ret < 0) 4948c2ecf20Sopenharmony_ci return ret; 4958c2ecf20Sopenharmony_ci 4968c2ecf20Sopenharmony_ci return mt9p031_pll_enable(mt9p031); 4978c2ecf20Sopenharmony_ci} 4988c2ecf20Sopenharmony_ci 4998c2ecf20Sopenharmony_cistatic int mt9p031_enum_mbus_code(struct v4l2_subdev *subdev, 5008c2ecf20Sopenharmony_ci struct v4l2_subdev_pad_config *cfg, 5018c2ecf20Sopenharmony_ci struct v4l2_subdev_mbus_code_enum *code) 5028c2ecf20Sopenharmony_ci{ 5038c2ecf20Sopenharmony_ci struct mt9p031 *mt9p031 = to_mt9p031(subdev); 5048c2ecf20Sopenharmony_ci 5058c2ecf20Sopenharmony_ci if (code->pad || code->index) 5068c2ecf20Sopenharmony_ci return -EINVAL; 5078c2ecf20Sopenharmony_ci 5088c2ecf20Sopenharmony_ci code->code = mt9p031->format.code; 5098c2ecf20Sopenharmony_ci return 0; 5108c2ecf20Sopenharmony_ci} 5118c2ecf20Sopenharmony_ci 5128c2ecf20Sopenharmony_cistatic int mt9p031_enum_frame_size(struct v4l2_subdev *subdev, 5138c2ecf20Sopenharmony_ci struct v4l2_subdev_pad_config *cfg, 5148c2ecf20Sopenharmony_ci struct v4l2_subdev_frame_size_enum *fse) 5158c2ecf20Sopenharmony_ci{ 5168c2ecf20Sopenharmony_ci struct mt9p031 *mt9p031 = to_mt9p031(subdev); 5178c2ecf20Sopenharmony_ci 5188c2ecf20Sopenharmony_ci if (fse->index >= 8 || fse->code != mt9p031->format.code) 5198c2ecf20Sopenharmony_ci return -EINVAL; 5208c2ecf20Sopenharmony_ci 5218c2ecf20Sopenharmony_ci fse->min_width = MT9P031_WINDOW_WIDTH_DEF 5228c2ecf20Sopenharmony_ci / min_t(unsigned int, 7, fse->index + 1); 5238c2ecf20Sopenharmony_ci fse->max_width = fse->min_width; 5248c2ecf20Sopenharmony_ci fse->min_height = MT9P031_WINDOW_HEIGHT_DEF / (fse->index + 1); 5258c2ecf20Sopenharmony_ci fse->max_height = fse->min_height; 5268c2ecf20Sopenharmony_ci 5278c2ecf20Sopenharmony_ci return 0; 5288c2ecf20Sopenharmony_ci} 5298c2ecf20Sopenharmony_ci 5308c2ecf20Sopenharmony_cistatic struct v4l2_mbus_framefmt * 5318c2ecf20Sopenharmony_ci__mt9p031_get_pad_format(struct mt9p031 *mt9p031, struct v4l2_subdev_pad_config *cfg, 5328c2ecf20Sopenharmony_ci unsigned int pad, u32 which) 5338c2ecf20Sopenharmony_ci{ 5348c2ecf20Sopenharmony_ci switch (which) { 5358c2ecf20Sopenharmony_ci case V4L2_SUBDEV_FORMAT_TRY: 5368c2ecf20Sopenharmony_ci return v4l2_subdev_get_try_format(&mt9p031->subdev, cfg, pad); 5378c2ecf20Sopenharmony_ci case V4L2_SUBDEV_FORMAT_ACTIVE: 5388c2ecf20Sopenharmony_ci return &mt9p031->format; 5398c2ecf20Sopenharmony_ci default: 5408c2ecf20Sopenharmony_ci return NULL; 5418c2ecf20Sopenharmony_ci } 5428c2ecf20Sopenharmony_ci} 5438c2ecf20Sopenharmony_ci 5448c2ecf20Sopenharmony_cistatic struct v4l2_rect * 5458c2ecf20Sopenharmony_ci__mt9p031_get_pad_crop(struct mt9p031 *mt9p031, struct v4l2_subdev_pad_config *cfg, 5468c2ecf20Sopenharmony_ci unsigned int pad, u32 which) 5478c2ecf20Sopenharmony_ci{ 5488c2ecf20Sopenharmony_ci switch (which) { 5498c2ecf20Sopenharmony_ci case V4L2_SUBDEV_FORMAT_TRY: 5508c2ecf20Sopenharmony_ci return v4l2_subdev_get_try_crop(&mt9p031->subdev, cfg, pad); 5518c2ecf20Sopenharmony_ci case V4L2_SUBDEV_FORMAT_ACTIVE: 5528c2ecf20Sopenharmony_ci return &mt9p031->crop; 5538c2ecf20Sopenharmony_ci default: 5548c2ecf20Sopenharmony_ci return NULL; 5558c2ecf20Sopenharmony_ci } 5568c2ecf20Sopenharmony_ci} 5578c2ecf20Sopenharmony_ci 5588c2ecf20Sopenharmony_cistatic int mt9p031_get_format(struct v4l2_subdev *subdev, 5598c2ecf20Sopenharmony_ci struct v4l2_subdev_pad_config *cfg, 5608c2ecf20Sopenharmony_ci struct v4l2_subdev_format *fmt) 5618c2ecf20Sopenharmony_ci{ 5628c2ecf20Sopenharmony_ci struct mt9p031 *mt9p031 = to_mt9p031(subdev); 5638c2ecf20Sopenharmony_ci 5648c2ecf20Sopenharmony_ci fmt->format = *__mt9p031_get_pad_format(mt9p031, cfg, fmt->pad, 5658c2ecf20Sopenharmony_ci fmt->which); 5668c2ecf20Sopenharmony_ci return 0; 5678c2ecf20Sopenharmony_ci} 5688c2ecf20Sopenharmony_ci 5698c2ecf20Sopenharmony_cistatic int mt9p031_set_format(struct v4l2_subdev *subdev, 5708c2ecf20Sopenharmony_ci struct v4l2_subdev_pad_config *cfg, 5718c2ecf20Sopenharmony_ci struct v4l2_subdev_format *format) 5728c2ecf20Sopenharmony_ci{ 5738c2ecf20Sopenharmony_ci struct mt9p031 *mt9p031 = to_mt9p031(subdev); 5748c2ecf20Sopenharmony_ci struct v4l2_mbus_framefmt *__format; 5758c2ecf20Sopenharmony_ci struct v4l2_rect *__crop; 5768c2ecf20Sopenharmony_ci unsigned int width; 5778c2ecf20Sopenharmony_ci unsigned int height; 5788c2ecf20Sopenharmony_ci unsigned int hratio; 5798c2ecf20Sopenharmony_ci unsigned int vratio; 5808c2ecf20Sopenharmony_ci 5818c2ecf20Sopenharmony_ci __crop = __mt9p031_get_pad_crop(mt9p031, cfg, format->pad, 5828c2ecf20Sopenharmony_ci format->which); 5838c2ecf20Sopenharmony_ci 5848c2ecf20Sopenharmony_ci /* Clamp the width and height to avoid dividing by zero. */ 5858c2ecf20Sopenharmony_ci width = clamp_t(unsigned int, ALIGN(format->format.width, 2), 5868c2ecf20Sopenharmony_ci max_t(unsigned int, __crop->width / 7, 5878c2ecf20Sopenharmony_ci MT9P031_WINDOW_WIDTH_MIN), 5888c2ecf20Sopenharmony_ci __crop->width); 5898c2ecf20Sopenharmony_ci height = clamp_t(unsigned int, ALIGN(format->format.height, 2), 5908c2ecf20Sopenharmony_ci max_t(unsigned int, __crop->height / 8, 5918c2ecf20Sopenharmony_ci MT9P031_WINDOW_HEIGHT_MIN), 5928c2ecf20Sopenharmony_ci __crop->height); 5938c2ecf20Sopenharmony_ci 5948c2ecf20Sopenharmony_ci hratio = DIV_ROUND_CLOSEST(__crop->width, width); 5958c2ecf20Sopenharmony_ci vratio = DIV_ROUND_CLOSEST(__crop->height, height); 5968c2ecf20Sopenharmony_ci 5978c2ecf20Sopenharmony_ci __format = __mt9p031_get_pad_format(mt9p031, cfg, format->pad, 5988c2ecf20Sopenharmony_ci format->which); 5998c2ecf20Sopenharmony_ci __format->width = __crop->width / hratio; 6008c2ecf20Sopenharmony_ci __format->height = __crop->height / vratio; 6018c2ecf20Sopenharmony_ci 6028c2ecf20Sopenharmony_ci format->format = *__format; 6038c2ecf20Sopenharmony_ci 6048c2ecf20Sopenharmony_ci return 0; 6058c2ecf20Sopenharmony_ci} 6068c2ecf20Sopenharmony_ci 6078c2ecf20Sopenharmony_cistatic int mt9p031_get_selection(struct v4l2_subdev *subdev, 6088c2ecf20Sopenharmony_ci struct v4l2_subdev_pad_config *cfg, 6098c2ecf20Sopenharmony_ci struct v4l2_subdev_selection *sel) 6108c2ecf20Sopenharmony_ci{ 6118c2ecf20Sopenharmony_ci struct mt9p031 *mt9p031 = to_mt9p031(subdev); 6128c2ecf20Sopenharmony_ci 6138c2ecf20Sopenharmony_ci if (sel->target != V4L2_SEL_TGT_CROP) 6148c2ecf20Sopenharmony_ci return -EINVAL; 6158c2ecf20Sopenharmony_ci 6168c2ecf20Sopenharmony_ci sel->r = *__mt9p031_get_pad_crop(mt9p031, cfg, sel->pad, sel->which); 6178c2ecf20Sopenharmony_ci return 0; 6188c2ecf20Sopenharmony_ci} 6198c2ecf20Sopenharmony_ci 6208c2ecf20Sopenharmony_cistatic int mt9p031_set_selection(struct v4l2_subdev *subdev, 6218c2ecf20Sopenharmony_ci struct v4l2_subdev_pad_config *cfg, 6228c2ecf20Sopenharmony_ci struct v4l2_subdev_selection *sel) 6238c2ecf20Sopenharmony_ci{ 6248c2ecf20Sopenharmony_ci struct mt9p031 *mt9p031 = to_mt9p031(subdev); 6258c2ecf20Sopenharmony_ci struct v4l2_mbus_framefmt *__format; 6268c2ecf20Sopenharmony_ci struct v4l2_rect *__crop; 6278c2ecf20Sopenharmony_ci struct v4l2_rect rect; 6288c2ecf20Sopenharmony_ci 6298c2ecf20Sopenharmony_ci if (sel->target != V4L2_SEL_TGT_CROP) 6308c2ecf20Sopenharmony_ci return -EINVAL; 6318c2ecf20Sopenharmony_ci 6328c2ecf20Sopenharmony_ci /* Clamp the crop rectangle boundaries and align them to a multiple of 2 6338c2ecf20Sopenharmony_ci * pixels to ensure a GRBG Bayer pattern. 6348c2ecf20Sopenharmony_ci */ 6358c2ecf20Sopenharmony_ci rect.left = clamp(ALIGN(sel->r.left, 2), MT9P031_COLUMN_START_MIN, 6368c2ecf20Sopenharmony_ci MT9P031_COLUMN_START_MAX); 6378c2ecf20Sopenharmony_ci rect.top = clamp(ALIGN(sel->r.top, 2), MT9P031_ROW_START_MIN, 6388c2ecf20Sopenharmony_ci MT9P031_ROW_START_MAX); 6398c2ecf20Sopenharmony_ci rect.width = clamp_t(unsigned int, ALIGN(sel->r.width, 2), 6408c2ecf20Sopenharmony_ci MT9P031_WINDOW_WIDTH_MIN, 6418c2ecf20Sopenharmony_ci MT9P031_WINDOW_WIDTH_MAX); 6428c2ecf20Sopenharmony_ci rect.height = clamp_t(unsigned int, ALIGN(sel->r.height, 2), 6438c2ecf20Sopenharmony_ci MT9P031_WINDOW_HEIGHT_MIN, 6448c2ecf20Sopenharmony_ci MT9P031_WINDOW_HEIGHT_MAX); 6458c2ecf20Sopenharmony_ci 6468c2ecf20Sopenharmony_ci rect.width = min_t(unsigned int, rect.width, 6478c2ecf20Sopenharmony_ci MT9P031_PIXEL_ARRAY_WIDTH - rect.left); 6488c2ecf20Sopenharmony_ci rect.height = min_t(unsigned int, rect.height, 6498c2ecf20Sopenharmony_ci MT9P031_PIXEL_ARRAY_HEIGHT - rect.top); 6508c2ecf20Sopenharmony_ci 6518c2ecf20Sopenharmony_ci __crop = __mt9p031_get_pad_crop(mt9p031, cfg, sel->pad, sel->which); 6528c2ecf20Sopenharmony_ci 6538c2ecf20Sopenharmony_ci if (rect.width != __crop->width || rect.height != __crop->height) { 6548c2ecf20Sopenharmony_ci /* Reset the output image size if the crop rectangle size has 6558c2ecf20Sopenharmony_ci * been modified. 6568c2ecf20Sopenharmony_ci */ 6578c2ecf20Sopenharmony_ci __format = __mt9p031_get_pad_format(mt9p031, cfg, sel->pad, 6588c2ecf20Sopenharmony_ci sel->which); 6598c2ecf20Sopenharmony_ci __format->width = rect.width; 6608c2ecf20Sopenharmony_ci __format->height = rect.height; 6618c2ecf20Sopenharmony_ci } 6628c2ecf20Sopenharmony_ci 6638c2ecf20Sopenharmony_ci *__crop = rect; 6648c2ecf20Sopenharmony_ci sel->r = rect; 6658c2ecf20Sopenharmony_ci 6668c2ecf20Sopenharmony_ci return 0; 6678c2ecf20Sopenharmony_ci} 6688c2ecf20Sopenharmony_ci 6698c2ecf20Sopenharmony_ci/* ----------------------------------------------------------------------------- 6708c2ecf20Sopenharmony_ci * V4L2 subdev control operations 6718c2ecf20Sopenharmony_ci */ 6728c2ecf20Sopenharmony_ci 6738c2ecf20Sopenharmony_ci#define V4L2_CID_BLC_AUTO (V4L2_CID_USER_BASE | 0x1002) 6748c2ecf20Sopenharmony_ci#define V4L2_CID_BLC_TARGET_LEVEL (V4L2_CID_USER_BASE | 0x1003) 6758c2ecf20Sopenharmony_ci#define V4L2_CID_BLC_ANALOG_OFFSET (V4L2_CID_USER_BASE | 0x1004) 6768c2ecf20Sopenharmony_ci#define V4L2_CID_BLC_DIGITAL_OFFSET (V4L2_CID_USER_BASE | 0x1005) 6778c2ecf20Sopenharmony_ci 6788c2ecf20Sopenharmony_cistatic int mt9p031_restore_blc(struct mt9p031 *mt9p031) 6798c2ecf20Sopenharmony_ci{ 6808c2ecf20Sopenharmony_ci struct i2c_client *client = v4l2_get_subdevdata(&mt9p031->subdev); 6818c2ecf20Sopenharmony_ci int ret; 6828c2ecf20Sopenharmony_ci 6838c2ecf20Sopenharmony_ci if (mt9p031->blc_auto->cur.val != 0) { 6848c2ecf20Sopenharmony_ci ret = mt9p031_set_mode2(mt9p031, 0, 6858c2ecf20Sopenharmony_ci MT9P031_READ_MODE_2_ROW_BLC); 6868c2ecf20Sopenharmony_ci if (ret < 0) 6878c2ecf20Sopenharmony_ci return ret; 6888c2ecf20Sopenharmony_ci } 6898c2ecf20Sopenharmony_ci 6908c2ecf20Sopenharmony_ci if (mt9p031->blc_offset->cur.val != 0) { 6918c2ecf20Sopenharmony_ci ret = mt9p031_write(client, MT9P031_ROW_BLACK_TARGET, 6928c2ecf20Sopenharmony_ci mt9p031->blc_offset->cur.val); 6938c2ecf20Sopenharmony_ci if (ret < 0) 6948c2ecf20Sopenharmony_ci return ret; 6958c2ecf20Sopenharmony_ci } 6968c2ecf20Sopenharmony_ci 6978c2ecf20Sopenharmony_ci return 0; 6988c2ecf20Sopenharmony_ci} 6998c2ecf20Sopenharmony_ci 7008c2ecf20Sopenharmony_cistatic int mt9p031_s_ctrl(struct v4l2_ctrl *ctrl) 7018c2ecf20Sopenharmony_ci{ 7028c2ecf20Sopenharmony_ci struct mt9p031 *mt9p031 = 7038c2ecf20Sopenharmony_ci container_of(ctrl->handler, struct mt9p031, ctrls); 7048c2ecf20Sopenharmony_ci struct i2c_client *client = v4l2_get_subdevdata(&mt9p031->subdev); 7058c2ecf20Sopenharmony_ci u16 data; 7068c2ecf20Sopenharmony_ci int ret; 7078c2ecf20Sopenharmony_ci 7088c2ecf20Sopenharmony_ci if (ctrl->flags & V4L2_CTRL_FLAG_INACTIVE) 7098c2ecf20Sopenharmony_ci return 0; 7108c2ecf20Sopenharmony_ci 7118c2ecf20Sopenharmony_ci switch (ctrl->id) { 7128c2ecf20Sopenharmony_ci case V4L2_CID_EXPOSURE: 7138c2ecf20Sopenharmony_ci ret = mt9p031_write(client, MT9P031_SHUTTER_WIDTH_UPPER, 7148c2ecf20Sopenharmony_ci (ctrl->val >> 16) & 0xffff); 7158c2ecf20Sopenharmony_ci if (ret < 0) 7168c2ecf20Sopenharmony_ci return ret; 7178c2ecf20Sopenharmony_ci 7188c2ecf20Sopenharmony_ci return mt9p031_write(client, MT9P031_SHUTTER_WIDTH_LOWER, 7198c2ecf20Sopenharmony_ci ctrl->val & 0xffff); 7208c2ecf20Sopenharmony_ci 7218c2ecf20Sopenharmony_ci case V4L2_CID_GAIN: 7228c2ecf20Sopenharmony_ci /* Gain is controlled by 2 analog stages and a digital stage. 7238c2ecf20Sopenharmony_ci * Valid values for the 3 stages are 7248c2ecf20Sopenharmony_ci * 7258c2ecf20Sopenharmony_ci * Stage Min Max Step 7268c2ecf20Sopenharmony_ci * ------------------------------------------ 7278c2ecf20Sopenharmony_ci * First analog stage x1 x2 1 7288c2ecf20Sopenharmony_ci * Second analog stage x1 x4 0.125 7298c2ecf20Sopenharmony_ci * Digital stage x1 x16 0.125 7308c2ecf20Sopenharmony_ci * 7318c2ecf20Sopenharmony_ci * To minimize noise, the gain stages should be used in the 7328c2ecf20Sopenharmony_ci * second analog stage, first analog stage, digital stage order. 7338c2ecf20Sopenharmony_ci * Gain from a previous stage should be pushed to its maximum 7348c2ecf20Sopenharmony_ci * value before the next stage is used. 7358c2ecf20Sopenharmony_ci */ 7368c2ecf20Sopenharmony_ci if (ctrl->val <= 32) { 7378c2ecf20Sopenharmony_ci data = ctrl->val; 7388c2ecf20Sopenharmony_ci } else if (ctrl->val <= 64) { 7398c2ecf20Sopenharmony_ci ctrl->val &= ~1; 7408c2ecf20Sopenharmony_ci data = (1 << 6) | (ctrl->val >> 1); 7418c2ecf20Sopenharmony_ci } else { 7428c2ecf20Sopenharmony_ci ctrl->val &= ~7; 7438c2ecf20Sopenharmony_ci data = ((ctrl->val - 64) << 5) | (1 << 6) | 32; 7448c2ecf20Sopenharmony_ci } 7458c2ecf20Sopenharmony_ci 7468c2ecf20Sopenharmony_ci return mt9p031_write(client, MT9P031_GLOBAL_GAIN, data); 7478c2ecf20Sopenharmony_ci 7488c2ecf20Sopenharmony_ci case V4L2_CID_HFLIP: 7498c2ecf20Sopenharmony_ci if (ctrl->val) 7508c2ecf20Sopenharmony_ci return mt9p031_set_mode2(mt9p031, 7518c2ecf20Sopenharmony_ci 0, MT9P031_READ_MODE_2_COL_MIR); 7528c2ecf20Sopenharmony_ci else 7538c2ecf20Sopenharmony_ci return mt9p031_set_mode2(mt9p031, 7548c2ecf20Sopenharmony_ci MT9P031_READ_MODE_2_COL_MIR, 0); 7558c2ecf20Sopenharmony_ci 7568c2ecf20Sopenharmony_ci case V4L2_CID_VFLIP: 7578c2ecf20Sopenharmony_ci if (ctrl->val) 7588c2ecf20Sopenharmony_ci return mt9p031_set_mode2(mt9p031, 7598c2ecf20Sopenharmony_ci 0, MT9P031_READ_MODE_2_ROW_MIR); 7608c2ecf20Sopenharmony_ci else 7618c2ecf20Sopenharmony_ci return mt9p031_set_mode2(mt9p031, 7628c2ecf20Sopenharmony_ci MT9P031_READ_MODE_2_ROW_MIR, 0); 7638c2ecf20Sopenharmony_ci 7648c2ecf20Sopenharmony_ci case V4L2_CID_TEST_PATTERN: 7658c2ecf20Sopenharmony_ci /* The digital side of the Black Level Calibration function must 7668c2ecf20Sopenharmony_ci * be disabled when generating a test pattern to avoid artifacts 7678c2ecf20Sopenharmony_ci * in the image. Activate (deactivate) the BLC-related controls 7688c2ecf20Sopenharmony_ci * when the test pattern is enabled (disabled). 7698c2ecf20Sopenharmony_ci */ 7708c2ecf20Sopenharmony_ci v4l2_ctrl_activate(mt9p031->blc_auto, ctrl->val == 0); 7718c2ecf20Sopenharmony_ci v4l2_ctrl_activate(mt9p031->blc_offset, ctrl->val == 0); 7728c2ecf20Sopenharmony_ci 7738c2ecf20Sopenharmony_ci if (!ctrl->val) { 7748c2ecf20Sopenharmony_ci /* Restore the BLC settings. */ 7758c2ecf20Sopenharmony_ci ret = mt9p031_restore_blc(mt9p031); 7768c2ecf20Sopenharmony_ci if (ret < 0) 7778c2ecf20Sopenharmony_ci return ret; 7788c2ecf20Sopenharmony_ci 7798c2ecf20Sopenharmony_ci return mt9p031_write(client, MT9P031_TEST_PATTERN, 7808c2ecf20Sopenharmony_ci MT9P031_TEST_PATTERN_DISABLE); 7818c2ecf20Sopenharmony_ci } 7828c2ecf20Sopenharmony_ci 7838c2ecf20Sopenharmony_ci ret = mt9p031_write(client, MT9P031_TEST_PATTERN_GREEN, 0x05a0); 7848c2ecf20Sopenharmony_ci if (ret < 0) 7858c2ecf20Sopenharmony_ci return ret; 7868c2ecf20Sopenharmony_ci ret = mt9p031_write(client, MT9P031_TEST_PATTERN_RED, 0x0a50); 7878c2ecf20Sopenharmony_ci if (ret < 0) 7888c2ecf20Sopenharmony_ci return ret; 7898c2ecf20Sopenharmony_ci ret = mt9p031_write(client, MT9P031_TEST_PATTERN_BLUE, 0x0aa0); 7908c2ecf20Sopenharmony_ci if (ret < 0) 7918c2ecf20Sopenharmony_ci return ret; 7928c2ecf20Sopenharmony_ci 7938c2ecf20Sopenharmony_ci /* Disable digital BLC when generating a test pattern. */ 7948c2ecf20Sopenharmony_ci ret = mt9p031_set_mode2(mt9p031, MT9P031_READ_MODE_2_ROW_BLC, 7958c2ecf20Sopenharmony_ci 0); 7968c2ecf20Sopenharmony_ci if (ret < 0) 7978c2ecf20Sopenharmony_ci return ret; 7988c2ecf20Sopenharmony_ci 7998c2ecf20Sopenharmony_ci ret = mt9p031_write(client, MT9P031_ROW_BLACK_DEF_OFFSET, 0); 8008c2ecf20Sopenharmony_ci if (ret < 0) 8018c2ecf20Sopenharmony_ci return ret; 8028c2ecf20Sopenharmony_ci 8038c2ecf20Sopenharmony_ci return mt9p031_write(client, MT9P031_TEST_PATTERN, 8048c2ecf20Sopenharmony_ci ((ctrl->val - 1) << MT9P031_TEST_PATTERN_SHIFT) 8058c2ecf20Sopenharmony_ci | MT9P031_TEST_PATTERN_ENABLE); 8068c2ecf20Sopenharmony_ci 8078c2ecf20Sopenharmony_ci case V4L2_CID_BLC_AUTO: 8088c2ecf20Sopenharmony_ci ret = mt9p031_set_mode2(mt9p031, 8098c2ecf20Sopenharmony_ci ctrl->val ? 0 : MT9P031_READ_MODE_2_ROW_BLC, 8108c2ecf20Sopenharmony_ci ctrl->val ? MT9P031_READ_MODE_2_ROW_BLC : 0); 8118c2ecf20Sopenharmony_ci if (ret < 0) 8128c2ecf20Sopenharmony_ci return ret; 8138c2ecf20Sopenharmony_ci 8148c2ecf20Sopenharmony_ci return mt9p031_write(client, MT9P031_BLACK_LEVEL_CALIBRATION, 8158c2ecf20Sopenharmony_ci ctrl->val ? 0 : MT9P031_BLC_MANUAL_BLC); 8168c2ecf20Sopenharmony_ci 8178c2ecf20Sopenharmony_ci case V4L2_CID_BLC_TARGET_LEVEL: 8188c2ecf20Sopenharmony_ci return mt9p031_write(client, MT9P031_ROW_BLACK_TARGET, 8198c2ecf20Sopenharmony_ci ctrl->val); 8208c2ecf20Sopenharmony_ci 8218c2ecf20Sopenharmony_ci case V4L2_CID_BLC_ANALOG_OFFSET: 8228c2ecf20Sopenharmony_ci data = ctrl->val & ((1 << 9) - 1); 8238c2ecf20Sopenharmony_ci 8248c2ecf20Sopenharmony_ci ret = mt9p031_write(client, MT9P031_GREEN1_OFFSET, data); 8258c2ecf20Sopenharmony_ci if (ret < 0) 8268c2ecf20Sopenharmony_ci return ret; 8278c2ecf20Sopenharmony_ci ret = mt9p031_write(client, MT9P031_GREEN2_OFFSET, data); 8288c2ecf20Sopenharmony_ci if (ret < 0) 8298c2ecf20Sopenharmony_ci return ret; 8308c2ecf20Sopenharmony_ci ret = mt9p031_write(client, MT9P031_RED_OFFSET, data); 8318c2ecf20Sopenharmony_ci if (ret < 0) 8328c2ecf20Sopenharmony_ci return ret; 8338c2ecf20Sopenharmony_ci return mt9p031_write(client, MT9P031_BLUE_OFFSET, data); 8348c2ecf20Sopenharmony_ci 8358c2ecf20Sopenharmony_ci case V4L2_CID_BLC_DIGITAL_OFFSET: 8368c2ecf20Sopenharmony_ci return mt9p031_write(client, MT9P031_ROW_BLACK_DEF_OFFSET, 8378c2ecf20Sopenharmony_ci ctrl->val & ((1 << 12) - 1)); 8388c2ecf20Sopenharmony_ci } 8398c2ecf20Sopenharmony_ci 8408c2ecf20Sopenharmony_ci return 0; 8418c2ecf20Sopenharmony_ci} 8428c2ecf20Sopenharmony_ci 8438c2ecf20Sopenharmony_cistatic const struct v4l2_ctrl_ops mt9p031_ctrl_ops = { 8448c2ecf20Sopenharmony_ci .s_ctrl = mt9p031_s_ctrl, 8458c2ecf20Sopenharmony_ci}; 8468c2ecf20Sopenharmony_ci 8478c2ecf20Sopenharmony_cistatic const char * const mt9p031_test_pattern_menu[] = { 8488c2ecf20Sopenharmony_ci "Disabled", 8498c2ecf20Sopenharmony_ci "Color Field", 8508c2ecf20Sopenharmony_ci "Horizontal Gradient", 8518c2ecf20Sopenharmony_ci "Vertical Gradient", 8528c2ecf20Sopenharmony_ci "Diagonal Gradient", 8538c2ecf20Sopenharmony_ci "Classic Test Pattern", 8548c2ecf20Sopenharmony_ci "Walking 1s", 8558c2ecf20Sopenharmony_ci "Monochrome Horizontal Bars", 8568c2ecf20Sopenharmony_ci "Monochrome Vertical Bars", 8578c2ecf20Sopenharmony_ci "Vertical Color Bars", 8588c2ecf20Sopenharmony_ci}; 8598c2ecf20Sopenharmony_ci 8608c2ecf20Sopenharmony_cistatic const struct v4l2_ctrl_config mt9p031_ctrls[] = { 8618c2ecf20Sopenharmony_ci { 8628c2ecf20Sopenharmony_ci .ops = &mt9p031_ctrl_ops, 8638c2ecf20Sopenharmony_ci .id = V4L2_CID_BLC_AUTO, 8648c2ecf20Sopenharmony_ci .type = V4L2_CTRL_TYPE_BOOLEAN, 8658c2ecf20Sopenharmony_ci .name = "BLC, Auto", 8668c2ecf20Sopenharmony_ci .min = 0, 8678c2ecf20Sopenharmony_ci .max = 1, 8688c2ecf20Sopenharmony_ci .step = 1, 8698c2ecf20Sopenharmony_ci .def = 1, 8708c2ecf20Sopenharmony_ci .flags = 0, 8718c2ecf20Sopenharmony_ci }, { 8728c2ecf20Sopenharmony_ci .ops = &mt9p031_ctrl_ops, 8738c2ecf20Sopenharmony_ci .id = V4L2_CID_BLC_TARGET_LEVEL, 8748c2ecf20Sopenharmony_ci .type = V4L2_CTRL_TYPE_INTEGER, 8758c2ecf20Sopenharmony_ci .name = "BLC Target Level", 8768c2ecf20Sopenharmony_ci .min = 0, 8778c2ecf20Sopenharmony_ci .max = 4095, 8788c2ecf20Sopenharmony_ci .step = 1, 8798c2ecf20Sopenharmony_ci .def = 168, 8808c2ecf20Sopenharmony_ci .flags = 0, 8818c2ecf20Sopenharmony_ci }, { 8828c2ecf20Sopenharmony_ci .ops = &mt9p031_ctrl_ops, 8838c2ecf20Sopenharmony_ci .id = V4L2_CID_BLC_ANALOG_OFFSET, 8848c2ecf20Sopenharmony_ci .type = V4L2_CTRL_TYPE_INTEGER, 8858c2ecf20Sopenharmony_ci .name = "BLC Analog Offset", 8868c2ecf20Sopenharmony_ci .min = -255, 8878c2ecf20Sopenharmony_ci .max = 255, 8888c2ecf20Sopenharmony_ci .step = 1, 8898c2ecf20Sopenharmony_ci .def = 32, 8908c2ecf20Sopenharmony_ci .flags = 0, 8918c2ecf20Sopenharmony_ci }, { 8928c2ecf20Sopenharmony_ci .ops = &mt9p031_ctrl_ops, 8938c2ecf20Sopenharmony_ci .id = V4L2_CID_BLC_DIGITAL_OFFSET, 8948c2ecf20Sopenharmony_ci .type = V4L2_CTRL_TYPE_INTEGER, 8958c2ecf20Sopenharmony_ci .name = "BLC Digital Offset", 8968c2ecf20Sopenharmony_ci .min = -2048, 8978c2ecf20Sopenharmony_ci .max = 2047, 8988c2ecf20Sopenharmony_ci .step = 1, 8998c2ecf20Sopenharmony_ci .def = 40, 9008c2ecf20Sopenharmony_ci .flags = 0, 9018c2ecf20Sopenharmony_ci } 9028c2ecf20Sopenharmony_ci}; 9038c2ecf20Sopenharmony_ci 9048c2ecf20Sopenharmony_ci/* ----------------------------------------------------------------------------- 9058c2ecf20Sopenharmony_ci * V4L2 subdev core operations 9068c2ecf20Sopenharmony_ci */ 9078c2ecf20Sopenharmony_ci 9088c2ecf20Sopenharmony_cistatic int mt9p031_set_power(struct v4l2_subdev *subdev, int on) 9098c2ecf20Sopenharmony_ci{ 9108c2ecf20Sopenharmony_ci struct mt9p031 *mt9p031 = to_mt9p031(subdev); 9118c2ecf20Sopenharmony_ci int ret = 0; 9128c2ecf20Sopenharmony_ci 9138c2ecf20Sopenharmony_ci mutex_lock(&mt9p031->power_lock); 9148c2ecf20Sopenharmony_ci 9158c2ecf20Sopenharmony_ci /* If the power count is modified from 0 to != 0 or from != 0 to 0, 9168c2ecf20Sopenharmony_ci * update the power state. 9178c2ecf20Sopenharmony_ci */ 9188c2ecf20Sopenharmony_ci if (mt9p031->power_count == !on) { 9198c2ecf20Sopenharmony_ci ret = __mt9p031_set_power(mt9p031, !!on); 9208c2ecf20Sopenharmony_ci if (ret < 0) 9218c2ecf20Sopenharmony_ci goto out; 9228c2ecf20Sopenharmony_ci } 9238c2ecf20Sopenharmony_ci 9248c2ecf20Sopenharmony_ci /* Update the power count. */ 9258c2ecf20Sopenharmony_ci mt9p031->power_count += on ? 1 : -1; 9268c2ecf20Sopenharmony_ci WARN_ON(mt9p031->power_count < 0); 9278c2ecf20Sopenharmony_ci 9288c2ecf20Sopenharmony_ciout: 9298c2ecf20Sopenharmony_ci mutex_unlock(&mt9p031->power_lock); 9308c2ecf20Sopenharmony_ci return ret; 9318c2ecf20Sopenharmony_ci} 9328c2ecf20Sopenharmony_ci 9338c2ecf20Sopenharmony_ci/* ----------------------------------------------------------------------------- 9348c2ecf20Sopenharmony_ci * V4L2 subdev internal operations 9358c2ecf20Sopenharmony_ci */ 9368c2ecf20Sopenharmony_ci 9378c2ecf20Sopenharmony_cistatic int mt9p031_registered(struct v4l2_subdev *subdev) 9388c2ecf20Sopenharmony_ci{ 9398c2ecf20Sopenharmony_ci struct i2c_client *client = v4l2_get_subdevdata(subdev); 9408c2ecf20Sopenharmony_ci struct mt9p031 *mt9p031 = to_mt9p031(subdev); 9418c2ecf20Sopenharmony_ci s32 data; 9428c2ecf20Sopenharmony_ci int ret; 9438c2ecf20Sopenharmony_ci 9448c2ecf20Sopenharmony_ci ret = mt9p031_power_on(mt9p031); 9458c2ecf20Sopenharmony_ci if (ret < 0) { 9468c2ecf20Sopenharmony_ci dev_err(&client->dev, "MT9P031 power up failed\n"); 9478c2ecf20Sopenharmony_ci return ret; 9488c2ecf20Sopenharmony_ci } 9498c2ecf20Sopenharmony_ci 9508c2ecf20Sopenharmony_ci /* Read out the chip version register */ 9518c2ecf20Sopenharmony_ci data = mt9p031_read(client, MT9P031_CHIP_VERSION); 9528c2ecf20Sopenharmony_ci mt9p031_power_off(mt9p031); 9538c2ecf20Sopenharmony_ci 9548c2ecf20Sopenharmony_ci if (data != MT9P031_CHIP_VERSION_VALUE) { 9558c2ecf20Sopenharmony_ci dev_err(&client->dev, "MT9P031 not detected, wrong version " 9568c2ecf20Sopenharmony_ci "0x%04x\n", data); 9578c2ecf20Sopenharmony_ci return -ENODEV; 9588c2ecf20Sopenharmony_ci } 9598c2ecf20Sopenharmony_ci 9608c2ecf20Sopenharmony_ci dev_info(&client->dev, "MT9P031 detected at address 0x%02x\n", 9618c2ecf20Sopenharmony_ci client->addr); 9628c2ecf20Sopenharmony_ci 9638c2ecf20Sopenharmony_ci return 0; 9648c2ecf20Sopenharmony_ci} 9658c2ecf20Sopenharmony_ci 9668c2ecf20Sopenharmony_cistatic int mt9p031_open(struct v4l2_subdev *subdev, struct v4l2_subdev_fh *fh) 9678c2ecf20Sopenharmony_ci{ 9688c2ecf20Sopenharmony_ci struct mt9p031 *mt9p031 = to_mt9p031(subdev); 9698c2ecf20Sopenharmony_ci struct v4l2_mbus_framefmt *format; 9708c2ecf20Sopenharmony_ci struct v4l2_rect *crop; 9718c2ecf20Sopenharmony_ci 9728c2ecf20Sopenharmony_ci crop = v4l2_subdev_get_try_crop(subdev, fh->pad, 0); 9738c2ecf20Sopenharmony_ci crop->left = MT9P031_COLUMN_START_DEF; 9748c2ecf20Sopenharmony_ci crop->top = MT9P031_ROW_START_DEF; 9758c2ecf20Sopenharmony_ci crop->width = MT9P031_WINDOW_WIDTH_DEF; 9768c2ecf20Sopenharmony_ci crop->height = MT9P031_WINDOW_HEIGHT_DEF; 9778c2ecf20Sopenharmony_ci 9788c2ecf20Sopenharmony_ci format = v4l2_subdev_get_try_format(subdev, fh->pad, 0); 9798c2ecf20Sopenharmony_ci 9808c2ecf20Sopenharmony_ci if (mt9p031->model == MT9P031_MODEL_MONOCHROME) 9818c2ecf20Sopenharmony_ci format->code = MEDIA_BUS_FMT_Y12_1X12; 9828c2ecf20Sopenharmony_ci else 9838c2ecf20Sopenharmony_ci format->code = MEDIA_BUS_FMT_SGRBG12_1X12; 9848c2ecf20Sopenharmony_ci 9858c2ecf20Sopenharmony_ci format->width = MT9P031_WINDOW_WIDTH_DEF; 9868c2ecf20Sopenharmony_ci format->height = MT9P031_WINDOW_HEIGHT_DEF; 9878c2ecf20Sopenharmony_ci format->field = V4L2_FIELD_NONE; 9888c2ecf20Sopenharmony_ci format->colorspace = V4L2_COLORSPACE_SRGB; 9898c2ecf20Sopenharmony_ci 9908c2ecf20Sopenharmony_ci return mt9p031_set_power(subdev, 1); 9918c2ecf20Sopenharmony_ci} 9928c2ecf20Sopenharmony_ci 9938c2ecf20Sopenharmony_cistatic int mt9p031_close(struct v4l2_subdev *subdev, struct v4l2_subdev_fh *fh) 9948c2ecf20Sopenharmony_ci{ 9958c2ecf20Sopenharmony_ci return mt9p031_set_power(subdev, 0); 9968c2ecf20Sopenharmony_ci} 9978c2ecf20Sopenharmony_ci 9988c2ecf20Sopenharmony_cistatic const struct v4l2_subdev_core_ops mt9p031_subdev_core_ops = { 9998c2ecf20Sopenharmony_ci .s_power = mt9p031_set_power, 10008c2ecf20Sopenharmony_ci}; 10018c2ecf20Sopenharmony_ci 10028c2ecf20Sopenharmony_cistatic const struct v4l2_subdev_video_ops mt9p031_subdev_video_ops = { 10038c2ecf20Sopenharmony_ci .s_stream = mt9p031_s_stream, 10048c2ecf20Sopenharmony_ci}; 10058c2ecf20Sopenharmony_ci 10068c2ecf20Sopenharmony_cistatic const struct v4l2_subdev_pad_ops mt9p031_subdev_pad_ops = { 10078c2ecf20Sopenharmony_ci .enum_mbus_code = mt9p031_enum_mbus_code, 10088c2ecf20Sopenharmony_ci .enum_frame_size = mt9p031_enum_frame_size, 10098c2ecf20Sopenharmony_ci .get_fmt = mt9p031_get_format, 10108c2ecf20Sopenharmony_ci .set_fmt = mt9p031_set_format, 10118c2ecf20Sopenharmony_ci .get_selection = mt9p031_get_selection, 10128c2ecf20Sopenharmony_ci .set_selection = mt9p031_set_selection, 10138c2ecf20Sopenharmony_ci}; 10148c2ecf20Sopenharmony_ci 10158c2ecf20Sopenharmony_cistatic const struct v4l2_subdev_ops mt9p031_subdev_ops = { 10168c2ecf20Sopenharmony_ci .core = &mt9p031_subdev_core_ops, 10178c2ecf20Sopenharmony_ci .video = &mt9p031_subdev_video_ops, 10188c2ecf20Sopenharmony_ci .pad = &mt9p031_subdev_pad_ops, 10198c2ecf20Sopenharmony_ci}; 10208c2ecf20Sopenharmony_ci 10218c2ecf20Sopenharmony_cistatic const struct v4l2_subdev_internal_ops mt9p031_subdev_internal_ops = { 10228c2ecf20Sopenharmony_ci .registered = mt9p031_registered, 10238c2ecf20Sopenharmony_ci .open = mt9p031_open, 10248c2ecf20Sopenharmony_ci .close = mt9p031_close, 10258c2ecf20Sopenharmony_ci}; 10268c2ecf20Sopenharmony_ci 10278c2ecf20Sopenharmony_ci/* ----------------------------------------------------------------------------- 10288c2ecf20Sopenharmony_ci * Driver initialization and probing 10298c2ecf20Sopenharmony_ci */ 10308c2ecf20Sopenharmony_ci 10318c2ecf20Sopenharmony_cistatic struct mt9p031_platform_data * 10328c2ecf20Sopenharmony_cimt9p031_get_pdata(struct i2c_client *client) 10338c2ecf20Sopenharmony_ci{ 10348c2ecf20Sopenharmony_ci struct mt9p031_platform_data *pdata; 10358c2ecf20Sopenharmony_ci struct device_node *np; 10368c2ecf20Sopenharmony_ci 10378c2ecf20Sopenharmony_ci if (!IS_ENABLED(CONFIG_OF) || !client->dev.of_node) 10388c2ecf20Sopenharmony_ci return client->dev.platform_data; 10398c2ecf20Sopenharmony_ci 10408c2ecf20Sopenharmony_ci np = of_graph_get_next_endpoint(client->dev.of_node, NULL); 10418c2ecf20Sopenharmony_ci if (!np) 10428c2ecf20Sopenharmony_ci return NULL; 10438c2ecf20Sopenharmony_ci 10448c2ecf20Sopenharmony_ci pdata = devm_kzalloc(&client->dev, sizeof(*pdata), GFP_KERNEL); 10458c2ecf20Sopenharmony_ci if (!pdata) 10468c2ecf20Sopenharmony_ci goto done; 10478c2ecf20Sopenharmony_ci 10488c2ecf20Sopenharmony_ci of_property_read_u32(np, "input-clock-frequency", &pdata->ext_freq); 10498c2ecf20Sopenharmony_ci of_property_read_u32(np, "pixel-clock-frequency", &pdata->target_freq); 10508c2ecf20Sopenharmony_ci 10518c2ecf20Sopenharmony_cidone: 10528c2ecf20Sopenharmony_ci of_node_put(np); 10538c2ecf20Sopenharmony_ci return pdata; 10548c2ecf20Sopenharmony_ci} 10558c2ecf20Sopenharmony_ci 10568c2ecf20Sopenharmony_cistatic int mt9p031_probe(struct i2c_client *client, 10578c2ecf20Sopenharmony_ci const struct i2c_device_id *did) 10588c2ecf20Sopenharmony_ci{ 10598c2ecf20Sopenharmony_ci struct mt9p031_platform_data *pdata = mt9p031_get_pdata(client); 10608c2ecf20Sopenharmony_ci struct i2c_adapter *adapter = client->adapter; 10618c2ecf20Sopenharmony_ci struct mt9p031 *mt9p031; 10628c2ecf20Sopenharmony_ci unsigned int i; 10638c2ecf20Sopenharmony_ci int ret; 10648c2ecf20Sopenharmony_ci 10658c2ecf20Sopenharmony_ci if (pdata == NULL) { 10668c2ecf20Sopenharmony_ci dev_err(&client->dev, "No platform data\n"); 10678c2ecf20Sopenharmony_ci return -EINVAL; 10688c2ecf20Sopenharmony_ci } 10698c2ecf20Sopenharmony_ci 10708c2ecf20Sopenharmony_ci if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WORD_DATA)) { 10718c2ecf20Sopenharmony_ci dev_warn(&client->dev, 10728c2ecf20Sopenharmony_ci "I2C-Adapter doesn't support I2C_FUNC_SMBUS_WORD\n"); 10738c2ecf20Sopenharmony_ci return -EIO; 10748c2ecf20Sopenharmony_ci } 10758c2ecf20Sopenharmony_ci 10768c2ecf20Sopenharmony_ci mt9p031 = devm_kzalloc(&client->dev, sizeof(*mt9p031), GFP_KERNEL); 10778c2ecf20Sopenharmony_ci if (mt9p031 == NULL) 10788c2ecf20Sopenharmony_ci return -ENOMEM; 10798c2ecf20Sopenharmony_ci 10808c2ecf20Sopenharmony_ci mt9p031->pdata = pdata; 10818c2ecf20Sopenharmony_ci mt9p031->output_control = MT9P031_OUTPUT_CONTROL_DEF; 10828c2ecf20Sopenharmony_ci mt9p031->mode2 = MT9P031_READ_MODE_2_ROW_BLC; 10838c2ecf20Sopenharmony_ci mt9p031->model = did->driver_data; 10848c2ecf20Sopenharmony_ci 10858c2ecf20Sopenharmony_ci mt9p031->regulators[0].supply = "vdd"; 10868c2ecf20Sopenharmony_ci mt9p031->regulators[1].supply = "vdd_io"; 10878c2ecf20Sopenharmony_ci mt9p031->regulators[2].supply = "vaa"; 10888c2ecf20Sopenharmony_ci 10898c2ecf20Sopenharmony_ci ret = devm_regulator_bulk_get(&client->dev, 3, mt9p031->regulators); 10908c2ecf20Sopenharmony_ci if (ret < 0) { 10918c2ecf20Sopenharmony_ci dev_err(&client->dev, "Unable to get regulators\n"); 10928c2ecf20Sopenharmony_ci return ret; 10938c2ecf20Sopenharmony_ci } 10948c2ecf20Sopenharmony_ci 10958c2ecf20Sopenharmony_ci mutex_init(&mt9p031->power_lock); 10968c2ecf20Sopenharmony_ci 10978c2ecf20Sopenharmony_ci v4l2_ctrl_handler_init(&mt9p031->ctrls, ARRAY_SIZE(mt9p031_ctrls) + 6); 10988c2ecf20Sopenharmony_ci 10998c2ecf20Sopenharmony_ci v4l2_ctrl_new_std(&mt9p031->ctrls, &mt9p031_ctrl_ops, 11008c2ecf20Sopenharmony_ci V4L2_CID_EXPOSURE, MT9P031_SHUTTER_WIDTH_MIN, 11018c2ecf20Sopenharmony_ci MT9P031_SHUTTER_WIDTH_MAX, 1, 11028c2ecf20Sopenharmony_ci MT9P031_SHUTTER_WIDTH_DEF); 11038c2ecf20Sopenharmony_ci v4l2_ctrl_new_std(&mt9p031->ctrls, &mt9p031_ctrl_ops, 11048c2ecf20Sopenharmony_ci V4L2_CID_GAIN, MT9P031_GLOBAL_GAIN_MIN, 11058c2ecf20Sopenharmony_ci MT9P031_GLOBAL_GAIN_MAX, 1, MT9P031_GLOBAL_GAIN_DEF); 11068c2ecf20Sopenharmony_ci v4l2_ctrl_new_std(&mt9p031->ctrls, &mt9p031_ctrl_ops, 11078c2ecf20Sopenharmony_ci V4L2_CID_HFLIP, 0, 1, 1, 0); 11088c2ecf20Sopenharmony_ci v4l2_ctrl_new_std(&mt9p031->ctrls, &mt9p031_ctrl_ops, 11098c2ecf20Sopenharmony_ci V4L2_CID_VFLIP, 0, 1, 1, 0); 11108c2ecf20Sopenharmony_ci v4l2_ctrl_new_std(&mt9p031->ctrls, &mt9p031_ctrl_ops, 11118c2ecf20Sopenharmony_ci V4L2_CID_PIXEL_RATE, pdata->target_freq, 11128c2ecf20Sopenharmony_ci pdata->target_freq, 1, pdata->target_freq); 11138c2ecf20Sopenharmony_ci v4l2_ctrl_new_std_menu_items(&mt9p031->ctrls, &mt9p031_ctrl_ops, 11148c2ecf20Sopenharmony_ci V4L2_CID_TEST_PATTERN, 11158c2ecf20Sopenharmony_ci ARRAY_SIZE(mt9p031_test_pattern_menu) - 1, 0, 11168c2ecf20Sopenharmony_ci 0, mt9p031_test_pattern_menu); 11178c2ecf20Sopenharmony_ci 11188c2ecf20Sopenharmony_ci for (i = 0; i < ARRAY_SIZE(mt9p031_ctrls); ++i) 11198c2ecf20Sopenharmony_ci v4l2_ctrl_new_custom(&mt9p031->ctrls, &mt9p031_ctrls[i], NULL); 11208c2ecf20Sopenharmony_ci 11218c2ecf20Sopenharmony_ci mt9p031->subdev.ctrl_handler = &mt9p031->ctrls; 11228c2ecf20Sopenharmony_ci 11238c2ecf20Sopenharmony_ci if (mt9p031->ctrls.error) { 11248c2ecf20Sopenharmony_ci printk(KERN_INFO "%s: control initialization error %d\n", 11258c2ecf20Sopenharmony_ci __func__, mt9p031->ctrls.error); 11268c2ecf20Sopenharmony_ci ret = mt9p031->ctrls.error; 11278c2ecf20Sopenharmony_ci goto done; 11288c2ecf20Sopenharmony_ci } 11298c2ecf20Sopenharmony_ci 11308c2ecf20Sopenharmony_ci mt9p031->blc_auto = v4l2_ctrl_find(&mt9p031->ctrls, V4L2_CID_BLC_AUTO); 11318c2ecf20Sopenharmony_ci mt9p031->blc_offset = v4l2_ctrl_find(&mt9p031->ctrls, 11328c2ecf20Sopenharmony_ci V4L2_CID_BLC_DIGITAL_OFFSET); 11338c2ecf20Sopenharmony_ci 11348c2ecf20Sopenharmony_ci v4l2_i2c_subdev_init(&mt9p031->subdev, client, &mt9p031_subdev_ops); 11358c2ecf20Sopenharmony_ci mt9p031->subdev.internal_ops = &mt9p031_subdev_internal_ops; 11368c2ecf20Sopenharmony_ci 11378c2ecf20Sopenharmony_ci mt9p031->subdev.entity.function = MEDIA_ENT_F_CAM_SENSOR; 11388c2ecf20Sopenharmony_ci mt9p031->pad.flags = MEDIA_PAD_FL_SOURCE; 11398c2ecf20Sopenharmony_ci ret = media_entity_pads_init(&mt9p031->subdev.entity, 1, &mt9p031->pad); 11408c2ecf20Sopenharmony_ci if (ret < 0) 11418c2ecf20Sopenharmony_ci goto done; 11428c2ecf20Sopenharmony_ci 11438c2ecf20Sopenharmony_ci mt9p031->subdev.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE; 11448c2ecf20Sopenharmony_ci 11458c2ecf20Sopenharmony_ci mt9p031->crop.width = MT9P031_WINDOW_WIDTH_DEF; 11468c2ecf20Sopenharmony_ci mt9p031->crop.height = MT9P031_WINDOW_HEIGHT_DEF; 11478c2ecf20Sopenharmony_ci mt9p031->crop.left = MT9P031_COLUMN_START_DEF; 11488c2ecf20Sopenharmony_ci mt9p031->crop.top = MT9P031_ROW_START_DEF; 11498c2ecf20Sopenharmony_ci 11508c2ecf20Sopenharmony_ci if (mt9p031->model == MT9P031_MODEL_MONOCHROME) 11518c2ecf20Sopenharmony_ci mt9p031->format.code = MEDIA_BUS_FMT_Y12_1X12; 11528c2ecf20Sopenharmony_ci else 11538c2ecf20Sopenharmony_ci mt9p031->format.code = MEDIA_BUS_FMT_SGRBG12_1X12; 11548c2ecf20Sopenharmony_ci 11558c2ecf20Sopenharmony_ci mt9p031->format.width = MT9P031_WINDOW_WIDTH_DEF; 11568c2ecf20Sopenharmony_ci mt9p031->format.height = MT9P031_WINDOW_HEIGHT_DEF; 11578c2ecf20Sopenharmony_ci mt9p031->format.field = V4L2_FIELD_NONE; 11588c2ecf20Sopenharmony_ci mt9p031->format.colorspace = V4L2_COLORSPACE_SRGB; 11598c2ecf20Sopenharmony_ci 11608c2ecf20Sopenharmony_ci mt9p031->reset = devm_gpiod_get_optional(&client->dev, "reset", 11618c2ecf20Sopenharmony_ci GPIOD_OUT_HIGH); 11628c2ecf20Sopenharmony_ci 11638c2ecf20Sopenharmony_ci ret = mt9p031_clk_setup(mt9p031); 11648c2ecf20Sopenharmony_ci if (ret) 11658c2ecf20Sopenharmony_ci goto done; 11668c2ecf20Sopenharmony_ci 11678c2ecf20Sopenharmony_ci ret = v4l2_async_register_subdev(&mt9p031->subdev); 11688c2ecf20Sopenharmony_ci 11698c2ecf20Sopenharmony_cidone: 11708c2ecf20Sopenharmony_ci if (ret < 0) { 11718c2ecf20Sopenharmony_ci v4l2_ctrl_handler_free(&mt9p031->ctrls); 11728c2ecf20Sopenharmony_ci media_entity_cleanup(&mt9p031->subdev.entity); 11738c2ecf20Sopenharmony_ci mutex_destroy(&mt9p031->power_lock); 11748c2ecf20Sopenharmony_ci } 11758c2ecf20Sopenharmony_ci 11768c2ecf20Sopenharmony_ci return ret; 11778c2ecf20Sopenharmony_ci} 11788c2ecf20Sopenharmony_ci 11798c2ecf20Sopenharmony_cistatic int mt9p031_remove(struct i2c_client *client) 11808c2ecf20Sopenharmony_ci{ 11818c2ecf20Sopenharmony_ci struct v4l2_subdev *subdev = i2c_get_clientdata(client); 11828c2ecf20Sopenharmony_ci struct mt9p031 *mt9p031 = to_mt9p031(subdev); 11838c2ecf20Sopenharmony_ci 11848c2ecf20Sopenharmony_ci v4l2_ctrl_handler_free(&mt9p031->ctrls); 11858c2ecf20Sopenharmony_ci v4l2_async_unregister_subdev(subdev); 11868c2ecf20Sopenharmony_ci media_entity_cleanup(&subdev->entity); 11878c2ecf20Sopenharmony_ci mutex_destroy(&mt9p031->power_lock); 11888c2ecf20Sopenharmony_ci 11898c2ecf20Sopenharmony_ci return 0; 11908c2ecf20Sopenharmony_ci} 11918c2ecf20Sopenharmony_ci 11928c2ecf20Sopenharmony_cistatic const struct i2c_device_id mt9p031_id[] = { 11938c2ecf20Sopenharmony_ci { "mt9p031", MT9P031_MODEL_COLOR }, 11948c2ecf20Sopenharmony_ci { "mt9p031m", MT9P031_MODEL_MONOCHROME }, 11958c2ecf20Sopenharmony_ci { } 11968c2ecf20Sopenharmony_ci}; 11978c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(i2c, mt9p031_id); 11988c2ecf20Sopenharmony_ci 11998c2ecf20Sopenharmony_ci#if IS_ENABLED(CONFIG_OF) 12008c2ecf20Sopenharmony_cistatic const struct of_device_id mt9p031_of_match[] = { 12018c2ecf20Sopenharmony_ci { .compatible = "aptina,mt9p031", }, 12028c2ecf20Sopenharmony_ci { .compatible = "aptina,mt9p031m", }, 12038c2ecf20Sopenharmony_ci { /* sentinel */ }, 12048c2ecf20Sopenharmony_ci}; 12058c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(of, mt9p031_of_match); 12068c2ecf20Sopenharmony_ci#endif 12078c2ecf20Sopenharmony_ci 12088c2ecf20Sopenharmony_cistatic struct i2c_driver mt9p031_i2c_driver = { 12098c2ecf20Sopenharmony_ci .driver = { 12108c2ecf20Sopenharmony_ci .of_match_table = of_match_ptr(mt9p031_of_match), 12118c2ecf20Sopenharmony_ci .name = "mt9p031", 12128c2ecf20Sopenharmony_ci }, 12138c2ecf20Sopenharmony_ci .probe = mt9p031_probe, 12148c2ecf20Sopenharmony_ci .remove = mt9p031_remove, 12158c2ecf20Sopenharmony_ci .id_table = mt9p031_id, 12168c2ecf20Sopenharmony_ci}; 12178c2ecf20Sopenharmony_ci 12188c2ecf20Sopenharmony_cimodule_i2c_driver(mt9p031_i2c_driver); 12198c2ecf20Sopenharmony_ci 12208c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Aptina MT9P031 Camera driver"); 12218c2ecf20Sopenharmony_ciMODULE_AUTHOR("Bastian Hecht <hechtb@gmail.com>"); 12228c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL v2"); 1223