File Index
Files and directories
Sign in
FILE

ov2680.c

File preview with syntax highlighting.

linux-6.19.14/drivers/media/i2c/ov2680.c C
   1// SPDX-License-Identifier: GPL-2.0
   2/*
   3 * Omnivision OV2680 CMOS Image Sensor driver
   4 *
   5 * Copyright (C) 2018 Linaro Ltd
   6 *
   7 * Based on OV5640 Sensor Driver
   8 * Copyright (C) 2011-2013 Freescale Semiconductor, Inc. All Rights Reserved.
   9 * Copyright (C) 2014-2017 Mentor Graphics Inc.
  10 *
  11 */
  12
  13#include <linux/clk.h>
  14#include <linux/delay.h>
  15#include <linux/err.h>
  16#include <linux/gpio/consumer.h>
  17#include <linux/i2c.h>
  18#include <linux/init.h>
  19#include <linux/mod_devicetable.h>
  20#include <linux/module.h>
  21#include <linux/pm_runtime.h>
  22#include <linux/regmap.h>
  23#include <linux/regulator/consumer.h>
  24
  25#include <media/v4l2-cci.h>
  26#include <media/v4l2-common.h>
  27#include <media/v4l2-ctrls.h>
  28#include <media/v4l2-fwnode.h>
  29#include <media/v4l2-subdev.h>
  30
  31#define OV2680_CHIP_ID				0x2680
  32
  33#define OV2680_REG_STREAM_CTRL			CCI_REG8(0x0100)
  34#define OV2680_REG_SOFT_RESET			CCI_REG8(0x0103)
  35
  36#define OV2680_REG_CHIP_ID			CCI_REG16(0x300a)
  37#define OV2680_REG_SC_CMMN_SUB_ID		CCI_REG8(0x302a)
  38#define OV2680_REG_PLL_MULTIPLIER		CCI_REG16(0x3081)
  39
  40#define OV2680_REG_EXPOSURE_PK			CCI_REG24(0x3500)
  41#define OV2680_REG_R_MANUAL			CCI_REG8(0x3503)
  42#define OV2680_REG_GAIN_PK			CCI_REG16(0x350a)
  43
  44#define OV2680_REG_SENSOR_CTRL_0A		CCI_REG8(0x370a)
  45
  46#define OV2680_REG_HORIZONTAL_START		CCI_REG16(0x3800)
  47#define OV2680_REG_VERTICAL_START		CCI_REG16(0x3802)
  48#define OV2680_REG_HORIZONTAL_END		CCI_REG16(0x3804)
  49#define OV2680_REG_VERTICAL_END			CCI_REG16(0x3806)
  50#define OV2680_REG_HORIZONTAL_OUTPUT_SIZE	CCI_REG16(0x3808)
  51#define OV2680_REG_VERTICAL_OUTPUT_SIZE		CCI_REG16(0x380a)
  52#define OV2680_REG_TIMING_HTS			CCI_REG16(0x380c)
  53#define OV2680_REG_TIMING_VTS			CCI_REG16(0x380e)
  54#define OV2680_REG_ISP_X_WIN			CCI_REG16(0x3810)
  55#define OV2680_REG_ISP_Y_WIN			CCI_REG16(0x3812)
  56#define OV2680_REG_X_INC			CCI_REG8(0x3814)
  57#define OV2680_REG_Y_INC			CCI_REG8(0x3815)
  58#define OV2680_REG_FORMAT1			CCI_REG8(0x3820)
  59#define OV2680_REG_FORMAT2			CCI_REG8(0x3821)
  60
  61#define OV2680_REG_ISP_CTRL00			CCI_REG8(0x5080)
  62
  63#define OV2680_REG_X_WIN			CCI_REG16(0x5704)
  64#define OV2680_REG_Y_WIN			CCI_REG16(0x5706)
  65
  66#define OV2680_FRAME_RATE			30
  67
  68#define OV2680_NATIVE_WIDTH			1616
  69#define OV2680_NATIVE_HEIGHT			1216
  70#define OV2680_NATIVE_START_LEFT		0
  71#define OV2680_NATIVE_START_TOP			0
  72#define OV2680_ACTIVE_WIDTH			1600
  73#define OV2680_ACTIVE_HEIGHT			1200
  74#define OV2680_ACTIVE_START_LEFT		8
  75#define OV2680_ACTIVE_START_TOP			8
  76#define OV2680_MIN_CROP_WIDTH			2
  77#define OV2680_MIN_CROP_HEIGHT			2
  78#define OV2680_MIN_VBLANK			4
  79#define OV2680_MAX_VBLANK			0xffff
  80
  81/* Fixed pre-div of 1/2 */
  82#define OV2680_PLL_PREDIV0			2
  83
  84/* Pre-div configurable through reg 0x3080, left at its default of 0x02 : 1/2 */
  85#define OV2680_PLL_PREDIV			2
  86
  87/* 66MHz pixel clock: 66MHz / 1704 * 1294 = 30fps */
  88#define OV2680_PIXELS_PER_LINE			1704
  89#define OV2680_LINES_PER_FRAME_30FPS		1294
  90
  91/* Max exposure time is VTS - 8 */
  92#define OV2680_INTEGRATION_TIME_MARGIN		8
  93
  94#define OV2680_DEFAULT_WIDTH			800
  95#define OV2680_DEFAULT_HEIGHT			600
  96
  97/* For enum_frame_size() full-size + binned-/quarter-size */
  98#define OV2680_FRAME_SIZES			2
  99
 100static const char * const ov2680_supply_name[] = {
 101	"DOVDD",
 102	"DVDD",
 103	"AVDD",
 104};
 105
 106#define OV2680_NUM_SUPPLIES ARRAY_SIZE(ov2680_supply_name)
 107
 108enum {
 109	OV2680_19_2_MHZ,
 110	OV2680_24_MHZ,
 111};
 112
 113static const unsigned long ov2680_xvclk_freqs[] = {
 114	[OV2680_19_2_MHZ] = 19200000,
 115	[OV2680_24_MHZ] = 24000000,
 116};
 117
 118static const u8 ov2680_pll_multipliers[] = {
 119	[OV2680_19_2_MHZ] = 69,
 120	[OV2680_24_MHZ] = 55,
 121};
 122
 123struct ov2680_ctrls {
 124	struct v4l2_ctrl_handler handler;
 125	struct v4l2_ctrl *exposure;
 126	struct v4l2_ctrl *gain;
 127	struct v4l2_ctrl *hflip;
 128	struct v4l2_ctrl *vflip;
 129	struct v4l2_ctrl *test_pattern;
 130	struct v4l2_ctrl *link_freq;
 131	struct v4l2_ctrl *pixel_rate;
 132	struct v4l2_ctrl *vblank;
 133	struct v4l2_ctrl *hblank;
 134};
 135
 136struct ov2680_mode {
 137	struct v4l2_rect		crop;
 138	struct v4l2_mbus_framefmt	fmt;
 139	struct v4l2_fract		frame_interval;
 140	bool				binning;
 141	u16				h_start;
 142	u16				v_start;
 143	u16				h_end;
 144	u16				v_end;
 145	u16				h_output_size;
 146	u16				v_output_size;
 147};
 148
 149struct ov2680_dev {
 150	struct device			*dev;
 151	struct regmap			*regmap;
 152	struct v4l2_subdev		sd;
 153
 154	struct media_pad		pad;
 155	struct clk			*xvclk;
 156	u32				xvclk_freq;
 157	u8				pll_mult;
 158	s64				link_freq[1];
 159	u64				pixel_rate;
 160	struct regulator_bulk_data	supplies[OV2680_NUM_SUPPLIES];
 161
 162	struct gpio_desc		*pwdn_gpio;
 163	struct mutex			lock; /* protect members */
 164
 165	bool				is_streaming;
 166
 167	struct ov2680_ctrls		ctrls;
 168	struct ov2680_mode		mode;
 169};
 170
 171static const struct v4l2_rect ov2680_default_crop = {
 172	.left = OV2680_ACTIVE_START_LEFT,
 173	.top = OV2680_ACTIVE_START_TOP,
 174	.width = OV2680_ACTIVE_WIDTH,
 175	.height = OV2680_ACTIVE_HEIGHT,
 176};
 177
 178static const char * const test_pattern_menu[] = {
 179	"Disabled",
 180	"Color Bars",
 181	"Random Data",
 182	"Square",
 183	"Black Image",
 184};
 185
 186static const int ov2680_hv_flip_bayer_order[] = {
 187	MEDIA_BUS_FMT_SBGGR10_1X10,
 188	MEDIA_BUS_FMT_SGRBG10_1X10,
 189	MEDIA_BUS_FMT_SGBRG10_1X10,
 190	MEDIA_BUS_FMT_SRGGB10_1X10,
 191};
 192
 193static const struct reg_sequence ov2680_global_setting[] = {
 194	/* MIPI PHY, 0x10 -> 0x1c enable bp_c_hs_en_lat and bp_d_hs_en_lat */
 195	{0x3016, 0x1c},
 196
 197	/* R MANUAL set exposure and gain to manual (hw does not do auto) */
 198	{0x3503, 0x03},
 199
 200	/* Analog control register tweaks */
 201	{0x3603, 0x39}, /* Reset value 0x99 */
 202	{0x3604, 0x24}, /* Reset value 0x74 */
 203	{0x3621, 0x37}, /* Reset value 0x44 */
 204
 205	/* Sensor control register tweaks */
 206	{0x3701, 0x64}, /* Reset value 0x61 */
 207	{0x3705, 0x3c}, /* Reset value 0x21 */
 208	{0x370c, 0x50}, /* Reset value 0x10 */
 209	{0x370d, 0xc0}, /* Reset value 0x00 */
 210	{0x3718, 0x88}, /* Reset value 0x80 */
 211
 212	/* PSRAM tweaks */
 213	{0x3781, 0x80}, /* Reset value 0x00 */
 214	{0x3784, 0x0c}, /* Reset value 0x00, based on OV2680_R1A_AM10.ovt */
 215	{0x3789, 0x60}, /* Reset value 0x50 */
 216
 217	/* BLC CTRL00 0x01 -> 0x81 set avg_weight to 8 */
 218	{0x4000, 0x81},
 219
 220	/* Set black level compensation range to 0 - 3 (default 0 - 11) */
 221	{0x4008, 0x00},
 222	{0x4009, 0x03},
 223
 224	/* VFIFO R2 0x00 -> 0x02 set Frame reset enable */
 225	{0x4602, 0x02},
 226
 227	/* MIPI ctrl CLK PREPARE MIN change from 0x26 (38) -> 0x36 (54) */
 228	{0x481f, 0x36},
 229
 230	/* MIPI ctrl CLK LPX P MIN change from 0x32 (50) -> 0x36 (54) */
 231	{0x4825, 0x36},
 232
 233	/* R ISP CTRL2 0x20 -> 0x30, set sof_sel bit */
 234	{0x5002, 0x30},
 235
 236	/*
 237	 * Window CONTROL 0x00 -> 0x01, enable manual window control,
 238	 * this is necessary for full size flip and mirror support.
 239	 */
 240	{0x5708, 0x01},
 241
 242	/*
 243	 * DPC CTRL0 0x14 -> 0x3e, set enable_tail, enable_3x3_cluster
 244	 * and enable_general_tail bits based OV2680_R1A_AM10.ovt.
 245	 */
 246	{0x5780, 0x3e},
 247
 248	/* DPC MORE CONNECTION CASE THRE 0x0c (12) -> 0x02 (2) */
 249	{0x5788, 0x02},
 250
 251	/* DPC GAIN LIST1 0x0f (15) -> 0x08 (8) */
 252	{0x578e, 0x08},
 253
 254	/* DPC GAIN LIST2 0x3f (63) -> 0x0c (12) */
 255	{0x578f, 0x0c},
 256
 257	/* DPC THRE RATIO 0x04 (4) -> 0x00 (0) */
 258	{0x5792, 0x00},
 259};
 260
 261static struct ov2680_dev *to_ov2680_dev(struct v4l2_subdev *sd)
 262{
 263	return container_of(sd, struct ov2680_dev, sd);
 264}
 265
 266static inline struct v4l2_subdev *ctrl_to_sd(struct v4l2_ctrl *ctrl)
 267{
 268	return &container_of(ctrl->handler, struct ov2680_dev,
 269			     ctrls.handler)->sd;
 270}
 271
 272static void ov2680_power_up(struct ov2680_dev *sensor)
 273{
 274	if (!sensor->pwdn_gpio)
 275		return;
 276
 277	gpiod_set_value(sensor->pwdn_gpio, 0);
 278	usleep_range(5000, 10000);
 279}
 280
 281static void ov2680_power_down(struct ov2680_dev *sensor)
 282{
 283	if (!sensor->pwdn_gpio)
 284		return;
 285
 286	gpiod_set_value(sensor->pwdn_gpio, 1);
 287	usleep_range(5000, 10000);
 288}
 289
 290static void ov2680_set_bayer_order(struct ov2680_dev *sensor,
 291				   struct v4l2_mbus_framefmt *fmt)
 292{
 293	int hv_flip = 0;
 294
 295	if (sensor->ctrls.vflip && sensor->ctrls.vflip->val)
 296		hv_flip += 1;
 297
 298	if (sensor->ctrls.hflip && sensor->ctrls.hflip->val)
 299		hv_flip += 2;
 300
 301	fmt->code = ov2680_hv_flip_bayer_order[hv_flip];
 302}
 303
 304static struct v4l2_mbus_framefmt *
 305__ov2680_get_pad_format(struct ov2680_dev *sensor,
 306			struct v4l2_subdev_state *state,
 307			unsigned int pad,
 308			enum v4l2_subdev_format_whence which)
 309{
 310	if (which == V4L2_SUBDEV_FORMAT_TRY)
 311		return v4l2_subdev_state_get_format(state, pad);
 312
 313	return &sensor->mode.fmt;
 314}
 315
 316static struct v4l2_rect *
 317__ov2680_get_pad_crop(struct ov2680_dev *sensor,
 318		      struct v4l2_subdev_state *state,
 319		      unsigned int pad,
 320		      enum v4l2_subdev_format_whence which)
 321{
 322	if (which == V4L2_SUBDEV_FORMAT_TRY)
 323		return v4l2_subdev_state_get_crop(state, pad);
 324
 325	return &sensor->mode.crop;
 326}
 327
 328static void ov2680_fill_format(struct ov2680_dev *sensor,
 329			       struct v4l2_mbus_framefmt *fmt,
 330			       unsigned int width, unsigned int height)
 331{
 332	memset(fmt, 0, sizeof(*fmt));
 333	fmt->width = width;
 334	fmt->height = height;
 335	fmt->field = V4L2_FIELD_NONE;
 336	fmt->colorspace = V4L2_COLORSPACE_SRGB;
 337	ov2680_set_bayer_order(sensor, fmt);
 338}
 339
 340static void ov2680_calc_mode(struct ov2680_dev *sensor)
 341{
 342	int width = sensor->mode.fmt.width;
 343	int height = sensor->mode.fmt.height;
 344	int orig_width = width;
 345	int orig_height = height;
 346
 347	if (width  <= (sensor->mode.crop.width / 2) &&
 348	    height <= (sensor->mode.crop.height / 2)) {
 349		sensor->mode.binning = true;
 350		width *= 2;
 351		height *= 2;
 352	} else {
 353		sensor->mode.binning = false;
 354	}
 355
 356	sensor->mode.h_start = (sensor->mode.crop.left +
 357				(sensor->mode.crop.width - width) / 2) & ~1;
 358	sensor->mode.v_start = (sensor->mode.crop.top +
 359				(sensor->mode.crop.height - height) / 2) & ~1;
 360	sensor->mode.h_end =
 361		min(sensor->mode.h_start + width - 1, OV2680_NATIVE_WIDTH - 1);
 362	sensor->mode.v_end =
 363		min(sensor->mode.v_start + height - 1, OV2680_NATIVE_HEIGHT - 1);
 364	sensor->mode.h_output_size = orig_width;
 365	sensor->mode.v_output_size = orig_height;
 366}
 367
 368static int ov2680_set_mode(struct ov2680_dev *sensor)
 369{
 370	u8 sensor_ctrl_0a, inc, fmt1, fmt2;
 371	int ret = 0;
 372
 373	if (sensor->mode.binning) {
 374		sensor_ctrl_0a = 0x23;
 375		inc = 0x31;
 376		fmt1 = 0xc2;
 377		fmt2 = 0x01;
 378	} else {
 379		sensor_ctrl_0a = 0x21;
 380		inc = 0x11;
 381		fmt1 = 0xc0;
 382		fmt2 = 0x00;
 383	}
 384
 385	cci_write(sensor->regmap, OV2680_REG_SENSOR_CTRL_0A,
 386		  sensor_ctrl_0a, &ret);
 387	cci_write(sensor->regmap, OV2680_REG_HORIZONTAL_START,
 388		  sensor->mode.h_start, &ret);
 389	cci_write(sensor->regmap, OV2680_REG_VERTICAL_START,
 390		  sensor->mode.v_start, &ret);
 391	cci_write(sensor->regmap, OV2680_REG_HORIZONTAL_END,
 392		  sensor->mode.h_end, &ret);
 393	cci_write(sensor->regmap, OV2680_REG_VERTICAL_END,
 394		  sensor->mode.v_end, &ret);
 395	cci_write(sensor->regmap, OV2680_REG_HORIZONTAL_OUTPUT_SIZE,
 396		  sensor->mode.h_output_size, &ret);
 397	cci_write(sensor->regmap, OV2680_REG_VERTICAL_OUTPUT_SIZE,
 398		  sensor->mode.v_output_size, &ret);
 399	cci_write(sensor->regmap, OV2680_REG_TIMING_HTS,
 400		  OV2680_PIXELS_PER_LINE, &ret);
 401	/* VTS gets set by the vblank ctrl */
 402	cci_write(sensor->regmap, OV2680_REG_ISP_X_WIN, 0, &ret);
 403	cci_write(sensor->regmap, OV2680_REG_ISP_Y_WIN, 0, &ret);
 404	cci_write(sensor->regmap, OV2680_REG_X_INC, inc, &ret);
 405	cci_write(sensor->regmap, OV2680_REG_Y_INC, inc, &ret);
 406	cci_write(sensor->regmap, OV2680_REG_X_WIN,
 407		  sensor->mode.h_output_size, &ret);
 408	cci_write(sensor->regmap, OV2680_REG_Y_WIN,
 409		  sensor->mode.v_output_size, &ret);
 410	cci_write(sensor->regmap, OV2680_REG_FORMAT1, fmt1, &ret);
 411	cci_write(sensor->regmap, OV2680_REG_FORMAT2, fmt2, &ret);
 412
 413	return ret;
 414}
 415
 416static int ov2680_set_vflip(struct ov2680_dev *sensor, s32 val)
 417{
 418	int ret;
 419
 420	if (sensor->is_streaming)
 421		return -EBUSY;
 422
 423	ret = cci_update_bits(sensor->regmap, OV2680_REG_FORMAT1,
 424			      BIT(2), val ? BIT(2) : 0, NULL);
 425	if (ret < 0)
 426		return ret;
 427
 428	ov2680_set_bayer_order(sensor, &sensor->mode.fmt);
 429	return 0;
 430}
 431
 432static int ov2680_set_hflip(struct ov2680_dev *sensor, s32 val)
 433{
 434	int ret;
 435
 436	if (sensor->is_streaming)
 437		return -EBUSY;
 438
 439	ret = cci_update_bits(sensor->regmap, OV2680_REG_FORMAT2,
 440			      BIT(2), val ? BIT(2) : 0, NULL);
 441	if (ret < 0)
 442		return ret;
 443
 444	ov2680_set_bayer_order(sensor, &sensor->mode.fmt);
 445	return 0;
 446}
 447
 448static int ov2680_test_pattern_set(struct ov2680_dev *sensor, int value)
 449{
 450	int ret = 0;
 451
 452	if (!value)
 453		return cci_update_bits(sensor->regmap, OV2680_REG_ISP_CTRL00,
 454				       BIT(7), 0, NULL);
 455
 456	cci_update_bits(sensor->regmap, OV2680_REG_ISP_CTRL00,
 457			0x03, value - 1, &ret);
 458	cci_update_bits(sensor->regmap, OV2680_REG_ISP_CTRL00,
 459			BIT(7), BIT(7), &ret);
 460
 461	return ret;
 462}
 463
 464static int ov2680_gain_set(struct ov2680_dev *sensor, u32 gain)
 465{
 466	return cci_write(sensor->regmap, OV2680_REG_GAIN_PK, gain, NULL);
 467}
 468
 469static int ov2680_exposure_set(struct ov2680_dev *sensor, u32 exp)
 470{
 471	return cci_write(sensor->regmap, OV2680_REG_EXPOSURE_PK, exp << 4,
 472			 NULL);
 473}
 474
 475static int ov2680_exposure_update_range(struct ov2680_dev *sensor)
 476{
 477	int exp_max = sensor->mode.fmt.height + sensor->ctrls.vblank->val -
 478		      OV2680_INTEGRATION_TIME_MARGIN;
 479
 480	return __v4l2_ctrl_modify_range(sensor->ctrls.exposure, 0, exp_max,
 481					1, exp_max);
 482}
 483
 484static int ov2680_stream_enable(struct ov2680_dev *sensor)
 485{
 486	int ret;
 487
 488	ret = cci_write(sensor->regmap, OV2680_REG_PLL_MULTIPLIER,
 489			sensor->pll_mult, NULL);
 490	if (ret < 0)
 491		return ret;
 492
 493	ret = regmap_multi_reg_write(sensor->regmap,
 494				     ov2680_global_setting,
 495				     ARRAY_SIZE(ov2680_global_setting));
 496	if (ret < 0)
 497		return ret;
 498
 499	ret = ov2680_set_mode(sensor);
 500	if (ret < 0)
 501		return ret;
 502
 503	/* Restore value of all ctrls */
 504	ret = __v4l2_ctrl_handler_setup(&sensor->ctrls.handler);
 505	if (ret < 0)
 506		return ret;
 507
 508	return cci_write(sensor->regmap, OV2680_REG_STREAM_CTRL, 1, NULL);
 509}
 510
 511static int ov2680_stream_disable(struct ov2680_dev *sensor)
 512{
 513	return cci_write(sensor->regmap, OV2680_REG_STREAM_CTRL, 0, NULL);
 514}
 515
 516static int ov2680_power_off(struct ov2680_dev *sensor)
 517{
 518	clk_disable_unprepare(sensor->xvclk);
 519	ov2680_power_down(sensor);
 520	regulator_bulk_disable(OV2680_NUM_SUPPLIES, sensor->supplies);
 521	return 0;
 522}
 523
 524static int ov2680_power_on(struct ov2680_dev *sensor)
 525{
 526	int ret;
 527
 528	ret = regulator_bulk_enable(OV2680_NUM_SUPPLIES, sensor->supplies);
 529	if (ret < 0) {
 530		dev_err(sensor->dev, "failed to enable regulators: %d\n", ret);
 531		return ret;
 532	}
 533
 534	if (!sensor->pwdn_gpio) {
 535		ret = cci_write(sensor->regmap, OV2680_REG_SOFT_RESET, 0x01,
 536				NULL);
 537		if (ret != 0) {
 538			dev_err(sensor->dev, "sensor soft reset failed\n");
 539			goto err_disable_regulators;
 540		}
 541		usleep_range(1000, 2000);
 542	} else {
 543		ov2680_power_down(sensor);
 544		ov2680_power_up(sensor);
 545	}
 546
 547	ret = clk_prepare_enable(sensor->xvclk);
 548	if (ret < 0)
 549		goto err_disable_regulators;
 550
 551	return 0;
 552
 553err_disable_regulators:
 554	regulator_bulk_disable(OV2680_NUM_SUPPLIES, sensor->supplies);
 555	return ret;
 556}
 557
 558static int ov2680_get_frame_interval(struct v4l2_subdev *sd,
 559				     struct v4l2_subdev_state *sd_state,
 560				     struct v4l2_subdev_frame_interval *fi)
 561{
 562	struct ov2680_dev *sensor = to_ov2680_dev(sd);
 563
 564	/*
 565	 * FIXME: Implement support for V4L2_SUBDEV_FORMAT_TRY, using the V4L2
 566	 * subdev active state API.
 567	 */
 568	if (fi->which != V4L2_SUBDEV_FORMAT_ACTIVE)
 569		return -EINVAL;
 570
 571	mutex_lock(&sensor->lock);
 572	fi->interval = sensor->mode.frame_interval;
 573	mutex_unlock(&sensor->lock);
 574
 575	return 0;
 576}
 577
 578static int ov2680_s_stream(struct v4l2_subdev *sd, int enable)
 579{
 580	struct ov2680_dev *sensor = to_ov2680_dev(sd);
 581	int ret = 0;
 582
 583	mutex_lock(&sensor->lock);
 584
 585	if (sensor->is_streaming == !!enable)
 586		goto unlock;
 587
 588	if (enable) {
 589		ret = pm_runtime_resume_and_get(sensor->sd.dev);
 590		if (ret < 0)
 591			goto unlock;
 592
 593		ret = ov2680_stream_enable(sensor);
 594		if (ret < 0) {
 595			pm_runtime_put(sensor->sd.dev);
 596			goto unlock;
 597		}
 598	} else {
 599		ret = ov2680_stream_disable(sensor);
 600		pm_runtime_put(sensor->sd.dev);
 601	}
 602
 603	sensor->is_streaming = !!enable;
 604
 605unlock:
 606	mutex_unlock(&sensor->lock);
 607
 608	return ret;
 609}
 610
 611static int ov2680_enum_mbus_code(struct v4l2_subdev *sd,
 612				 struct v4l2_subdev_state *sd_state,
 613				 struct v4l2_subdev_mbus_code_enum *code)
 614{
 615	struct ov2680_dev *sensor = to_ov2680_dev(sd);
 616
 617	if (code->index != 0)
 618		return -EINVAL;
 619
 620	code->code = sensor->mode.fmt.code;
 621
 622	return 0;
 623}
 624
 625static int ov2680_get_fmt(struct v4l2_subdev *sd,
 626			  struct v4l2_subdev_state *sd_state,
 627			  struct v4l2_subdev_format *format)
 628{
 629	struct ov2680_dev *sensor = to_ov2680_dev(sd);
 630	struct v4l2_mbus_framefmt *fmt;
 631
 632	fmt = __ov2680_get_pad_format(sensor, sd_state, format->pad,
 633				      format->which);
 634
 635	mutex_lock(&sensor->lock);
 636	format->format = *fmt;
 637	mutex_unlock(&sensor->lock);
 638
 639	return 0;
 640}
 641
 642static int ov2680_set_fmt(struct v4l2_subdev *sd,
 643			  struct v4l2_subdev_state *sd_state,
 644			  struct v4l2_subdev_format *format)
 645{
 646	struct ov2680_dev *sensor = to_ov2680_dev(sd);
 647	struct v4l2_mbus_framefmt *try_fmt;
 648	const struct v4l2_rect *crop;
 649	unsigned int width, height;
 650	int def, max, ret = 0;
 651
 652	crop = __ov2680_get_pad_crop(sensor, sd_state, format->pad,
 653				     format->which);
 654
 655	/* Limit set_fmt max size to crop width / height */
 656	width = clamp_val(ALIGN(format->format.width, 2),
 657			  OV2680_MIN_CROP_WIDTH, crop->width);
 658	height = clamp_val(ALIGN(format->format.height, 2),
 659			   OV2680_MIN_CROP_HEIGHT, crop->height);
 660
 661	ov2680_fill_format(sensor, &format->format, width, height);
 662
 663	if (format->which == V4L2_SUBDEV_FORMAT_TRY) {
 664		try_fmt = v4l2_subdev_state_get_format(sd_state, 0);
 665		*try_fmt = format->format;
 666		return 0;
 667	}
 668
 669	mutex_lock(&sensor->lock);
 670
 671	if (sensor->is_streaming) {
 672		ret = -EBUSY;
 673		goto unlock;
 674	}
 675
 676	sensor->mode.fmt = format->format;
 677	ov2680_calc_mode(sensor);
 678
 679	/* vblank range is height dependent adjust and reset to default */
 680	max = OV2680_MAX_VBLANK - height;
 681	def = OV2680_LINES_PER_FRAME_30FPS - height;
 682	ret = __v4l2_ctrl_modify_range(sensor->ctrls.vblank, OV2680_MIN_VBLANK,
 683				       max, 1, def);
 684	if (ret)
 685		goto unlock;
 686
 687	ret = __v4l2_ctrl_s_ctrl(sensor->ctrls.vblank, def);
 688	if (ret)
 689		goto unlock;
 690
 691	/* exposure range depends on vts which may have changed */
 692	ret = ov2680_exposure_update_range(sensor);
 693	if (ret)
 694		goto unlock;
 695
 696	/* adjust hblank value for new width */
 697	def = OV2680_PIXELS_PER_LINE - width;
 698	ret = __v4l2_ctrl_modify_range(sensor->ctrls.hblank, def, def, 1, def);
 699
 700unlock:
 701	mutex_unlock(&sensor->lock);
 702
 703	return ret;
 704}
 705
 706static int ov2680_get_selection(struct v4l2_subdev *sd,
 707				struct v4l2_subdev_state *state,
 708				struct v4l2_subdev_selection *sel)
 709{
 710	struct ov2680_dev *sensor = to_ov2680_dev(sd);
 711
 712	switch (sel->target) {
 713	case V4L2_SEL_TGT_CROP:
 714		mutex_lock(&sensor->lock);
 715		sel->r = *__ov2680_get_pad_crop(sensor, state, sel->pad,
 716						sel->which);
 717		mutex_unlock(&sensor->lock);
 718		break;
 719	case V4L2_SEL_TGT_NATIVE_SIZE:
 720	case V4L2_SEL_TGT_CROP_BOUNDS:
 721		sel->r.top = 0;
 722		sel->r.left = 0;
 723		sel->r.width = OV2680_NATIVE_WIDTH;
 724		sel->r.height = OV2680_NATIVE_HEIGHT;
 725		break;
 726	case V4L2_SEL_TGT_CROP_DEFAULT:
 727		sel->r = ov2680_default_crop;
 728		break;
 729	default:
 730		return -EINVAL;
 731	}
 732
 733	return 0;
 734}
 735
 736static int ov2680_set_selection(struct v4l2_subdev *sd,
 737				struct v4l2_subdev_state *state,
 738				struct v4l2_subdev_selection *sel)
 739{
 740	struct ov2680_dev *sensor = to_ov2680_dev(sd);
 741	struct v4l2_mbus_framefmt *format;
 742	struct v4l2_rect *crop;
 743	struct v4l2_rect rect;
 744
 745	if (sel->target != V4L2_SEL_TGT_CROP)
 746		return -EINVAL;
 747
 748	/*
 749	 * Clamp the boundaries of the crop rectangle to the size of the sensor
 750	 * pixel array. Align to multiples of 2 to ensure Bayer pattern isn't
 751	 * disrupted.
 752	 */
 753	rect.left = clamp_val(ALIGN(sel->r.left, 2),
 754			      OV2680_NATIVE_START_LEFT, OV2680_NATIVE_WIDTH);
 755	rect.top = clamp_val(ALIGN(sel->r.top, 2),
 756			     OV2680_NATIVE_START_TOP, OV2680_NATIVE_HEIGHT);
 757	rect.width = clamp_val(ALIGN(sel->r.width, 2),
 758			       OV2680_MIN_CROP_WIDTH, OV2680_NATIVE_WIDTH);
 759	rect.height = clamp_val(ALIGN(sel->r.height, 2),
 760				OV2680_MIN_CROP_HEIGHT, OV2680_NATIVE_HEIGHT);
 761
 762	/* Make sure the crop rectangle isn't outside the bounds of the array */
 763	rect.width = min_t(unsigned int, rect.width,
 764			   OV2680_NATIVE_WIDTH - rect.left);
 765	rect.height = min_t(unsigned int, rect.height,
 766			    OV2680_NATIVE_HEIGHT - rect.top);
 767
 768	crop = __ov2680_get_pad_crop(sensor, state, sel->pad, sel->which);
 769
 770	mutex_lock(&sensor->lock);
 771	if (rect.width != crop->width || rect.height != crop->height) {
 772		/*
 773		 * Reset the output image size if the crop rectangle size has
 774		 * been modified.
 775		 */
 776		format = __ov2680_get_pad_format(sensor, state, sel->pad,
 777						 sel->which);
 778		format->width = rect.width;
 779		format->height = rect.height;
 780	}
 781
 782	*crop = rect;
 783	mutex_unlock(&sensor->lock);
 784
 785	sel->r = rect;
 786
 787	return 0;
 788}
 789
 790static int ov2680_init_state(struct v4l2_subdev *sd,
 791			     struct v4l2_subdev_state *sd_state)
 792{
 793	struct ov2680_dev *sensor = to_ov2680_dev(sd);
 794
 795	*v4l2_subdev_state_get_crop(sd_state, 0) = ov2680_default_crop;
 796
 797	ov2680_fill_format(sensor, v4l2_subdev_state_get_format(sd_state, 0),
 798			   OV2680_DEFAULT_WIDTH, OV2680_DEFAULT_HEIGHT);
 799	return 0;
 800}
 801
 802static int ov2680_enum_frame_size(struct v4l2_subdev *sd,
 803				  struct v4l2_subdev_state *sd_state,
 804				  struct v4l2_subdev_frame_size_enum *fse)
 805{
 806	struct ov2680_dev *sensor = to_ov2680_dev(sd);
 807	struct v4l2_rect *crop;
 808
 809	if (fse->index >= OV2680_FRAME_SIZES)
 810		return -EINVAL;
 811
 812	crop = __ov2680_get_pad_crop(sensor, sd_state, fse->pad, fse->which);
 813	if (!crop)
 814		return -EINVAL;
 815
 816	fse->min_width = crop->width / (fse->index + 1);
 817	fse->min_height = crop->height / (fse->index + 1);
 818	fse->max_width = fse->min_width;
 819	fse->max_height = fse->min_height;
 820
 821	return 0;
 822}
 823
 824static bool ov2680_valid_frame_size(struct v4l2_subdev *sd,
 825				    struct v4l2_subdev_state *sd_state,
 826				    struct v4l2_subdev_frame_interval_enum *fie)
 827{
 828	struct v4l2_subdev_frame_size_enum fse = {
 829		.pad = fie->pad,
 830		.which = fie->which,
 831	};
 832	int i;
 833
 834	for (i = 0; i < OV2680_FRAME_SIZES; i++) {
 835		fse.index = i;
 836
 837		if (ov2680_enum_frame_size(sd, sd_state, &fse))
 838			return false;
 839
 840		if (fie->width == fse.min_width &&
 841		    fie->height == fse.min_height)
 842			return true;
 843	}
 844
 845	return false;
 846}
 847
 848static int ov2680_enum_frame_interval(struct v4l2_subdev *sd,
 849			      struct v4l2_subdev_state *sd_state,
 850			      struct v4l2_subdev_frame_interval_enum *fie)
 851{
 852	struct ov2680_dev *sensor = to_ov2680_dev(sd);
 853
 854	/* Only 1 framerate */
 855	if (fie->index || !ov2680_valid_frame_size(sd, sd_state, fie))
 856		return -EINVAL;
 857
 858	fie->interval = sensor->mode.frame_interval;
 859
 860	return 0;
 861}
 862
 863static int ov2680_s_ctrl(struct v4l2_ctrl *ctrl)
 864{
 865	struct v4l2_subdev *sd = ctrl_to_sd(ctrl);
 866	struct ov2680_dev *sensor = to_ov2680_dev(sd);
 867	int ret;
 868
 869	/* Update exposure range on vblank changes */
 870	if (ctrl->id == V4L2_CID_VBLANK) {
 871		ret = ov2680_exposure_update_range(sensor);
 872		if (ret)
 873			return ret;
 874	}
 875
 876	/* Only apply changes to the controls if the device is powered up */
 877	if (!pm_runtime_get_if_in_use(sensor->sd.dev)) {
 878		ov2680_set_bayer_order(sensor, &sensor->mode.fmt);
 879		return 0;
 880	}
 881
 882	switch (ctrl->id) {
 883	case V4L2_CID_ANALOGUE_GAIN:
 884		ret = ov2680_gain_set(sensor, ctrl->val);
 885		break;
 886	case V4L2_CID_EXPOSURE:
 887		ret = ov2680_exposure_set(sensor, ctrl->val);
 888		break;
 889	case V4L2_CID_VFLIP:
 890		ret = ov2680_set_vflip(sensor, ctrl->val);
 891		break;
 892	case V4L2_CID_HFLIP:
 893		ret = ov2680_set_hflip(sensor, ctrl->val);
 894		break;
 895	case V4L2_CID_TEST_PATTERN:
 896		ret = ov2680_test_pattern_set(sensor, ctrl->val);
 897		break;
 898	case V4L2_CID_VBLANK:
 899		ret = cci_write(sensor->regmap, OV2680_REG_TIMING_VTS,
 900				sensor->mode.fmt.height + ctrl->val, NULL);
 901		break;
 902	default:
 903		ret = -EINVAL;
 904		break;
 905	}
 906
 907	pm_runtime_put(sensor->sd.dev);
 908	return ret;
 909}
 910
 911static const struct v4l2_ctrl_ops ov2680_ctrl_ops = {
 912	.s_ctrl = ov2680_s_ctrl,
 913};
 914
 915static const struct v4l2_subdev_video_ops ov2680_video_ops = {
 916	.s_stream		= ov2680_s_stream,
 917};
 918
 919static const struct v4l2_subdev_pad_ops ov2680_pad_ops = {
 920	.enum_mbus_code		= ov2680_enum_mbus_code,
 921	.enum_frame_size	= ov2680_enum_frame_size,
 922	.enum_frame_interval	= ov2680_enum_frame_interval,
 923	.get_fmt		= ov2680_get_fmt,
 924	.set_fmt		= ov2680_set_fmt,
 925	.get_selection		= ov2680_get_selection,
 926	.set_selection		= ov2680_set_selection,
 927	.get_frame_interval	= ov2680_get_frame_interval,
 928	.set_frame_interval	= ov2680_get_frame_interval,
 929};
 930
 931static const struct v4l2_subdev_ops ov2680_subdev_ops = {
 932	.video	= &ov2680_video_ops,
 933	.pad	= &ov2680_pad_ops,
 934};
 935
 936static const struct v4l2_subdev_internal_ops ov2680_internal_ops = {
 937	.init_state		= ov2680_init_state,
 938};
 939
 940static int ov2680_mode_init(struct ov2680_dev *sensor)
 941{
 942	/* set initial mode */
 943	sensor->mode.crop = ov2680_default_crop;
 944	ov2680_fill_format(sensor, &sensor->mode.fmt,
 945			   OV2680_DEFAULT_WIDTH, OV2680_DEFAULT_HEIGHT);
 946	ov2680_calc_mode(sensor);
 947
 948	sensor->mode.frame_interval.denominator = OV2680_FRAME_RATE;
 949	sensor->mode.frame_interval.numerator = 1;
 950
 951	return 0;
 952}
 953
 954static int ov2680_v4l2_register(struct ov2680_dev *sensor)
 955{
 956	struct i2c_client *client = to_i2c_client(sensor->dev);
 957	const struct v4l2_ctrl_ops *ops = &ov2680_ctrl_ops;
 958	struct ov2680_ctrls *ctrls = &sensor->ctrls;
 959	struct v4l2_ctrl_handler *hdl = &ctrls->handler;
 960	struct v4l2_fwnode_device_properties props;
 961	int def, max, ret = 0;
 962
 963	v4l2_i2c_subdev_init(&sensor->sd, client, &ov2680_subdev_ops);
 964	sensor->sd.internal_ops = &ov2680_internal_ops;
 965
 966	sensor->sd.flags = V4L2_SUBDEV_FL_HAS_DEVNODE;
 967	sensor->pad.flags = MEDIA_PAD_FL_SOURCE;
 968	sensor->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
 969
 970	ret = media_entity_pads_init(&sensor->sd.entity, 1, &sensor->pad);
 971	if (ret < 0)
 972		return ret;
 973
 974	v4l2_ctrl_handler_init(hdl, 11);
 975
 976	hdl->lock = &sensor->lock;
 977
 978	ctrls->vflip = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_VFLIP, 0, 1, 1, 0);
 979	ctrls->hflip = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_HFLIP, 0, 1, 1, 0);
 980
 981	ctrls->test_pattern = v4l2_ctrl_new_std_menu_items(hdl,
 982					&ov2680_ctrl_ops, V4L2_CID_TEST_PATTERN,
 983					ARRAY_SIZE(test_pattern_menu) - 1,
 984					0, 0, test_pattern_menu);
 985
 986	max = OV2680_LINES_PER_FRAME_30FPS - OV2680_INTEGRATION_TIME_MARGIN;
 987	ctrls->exposure = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_EXPOSURE,
 988					    0, max, 1, max);
 989
 990	ctrls->gain = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_ANALOGUE_GAIN,
 991					0, 1023, 1, 250);
 992
 993	ctrls->link_freq = v4l2_ctrl_new_int_menu(hdl, NULL, V4L2_CID_LINK_FREQ,
 994						  0, 0, sensor->link_freq);
 995	ctrls->pixel_rate = v4l2_ctrl_new_std(hdl, NULL, V4L2_CID_PIXEL_RATE,
 996					      0, sensor->pixel_rate,
 997					      1, sensor->pixel_rate);
 998
 999	max = OV2680_MAX_VBLANK - OV2680_DEFAULT_HEIGHT;
1000	def = OV2680_LINES_PER_FRAME_30FPS - OV2680_DEFAULT_HEIGHT;
1001	ctrls->vblank = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_VBLANK,
1002					  OV2680_MIN_VBLANK, max, 1, def);
1003
1004	def = OV2680_PIXELS_PER_LINE - OV2680_DEFAULT_WIDTH;
1005	ctrls->hblank = v4l2_ctrl_new_std(hdl, ops, V4L2_CID_HBLANK,
1006					  def, def, 1, def);
1007
1008	ret = v4l2_fwnode_device_parse(sensor->dev, &props);
1009	if (ret)
1010		goto cleanup_entity;
1011
1012	v4l2_ctrl_new_fwnode_properties(hdl, ops, &props);
1013
1014	if (hdl->error) {
1015		ret = hdl->error;
1016		goto cleanup_entity;
1017	}
1018
1019	ctrls->vflip->flags |= V4L2_CTRL_FLAG_MODIFY_LAYOUT;
1020	ctrls->hflip->flags |= V4L2_CTRL_FLAG_MODIFY_LAYOUT;
1021	ctrls->link_freq->flags |= V4L2_CTRL_FLAG_READ_ONLY;
1022	ctrls->hblank->flags |= V4L2_CTRL_FLAG_READ_ONLY;
1023
1024	sensor->sd.ctrl_handler = hdl;
1025
1026	ret = v4l2_async_register_subdev(&sensor->sd);
1027	if (ret < 0)
1028		goto cleanup_entity;
1029
1030	return 0;
1031
1032cleanup_entity:
1033	media_entity_cleanup(&sensor->sd.entity);
1034	v4l2_ctrl_handler_free(hdl);
1035
1036	return ret;
1037}
1038
1039static int ov2680_get_regulators(struct ov2680_dev *sensor)
1040{
1041	int i;
1042
1043	for (i = 0; i < OV2680_NUM_SUPPLIES; i++)
1044		sensor->supplies[i].supply = ov2680_supply_name[i];
1045
1046	return devm_regulator_bulk_get(sensor->dev,
1047				       OV2680_NUM_SUPPLIES, sensor->supplies);
1048}
1049
1050static int ov2680_check_id(struct ov2680_dev *sensor)
1051{
1052	u64 chip_id, rev;
1053	int ret = 0;
1054
1055	cci_read(sensor->regmap, OV2680_REG_CHIP_ID, &chip_id, &ret);
1056	cci_read(sensor->regmap, OV2680_REG_SC_CMMN_SUB_ID, &rev, &ret);
1057	if (ret < 0) {
1058		dev_err(sensor->dev, "failed to read chip id\n");
1059		return ret;
1060	}
1061
1062	if (chip_id != OV2680_CHIP_ID) {
1063		dev_err(sensor->dev, "chip id: 0x%04llx does not match expected 0x%04x\n",
1064			chip_id, OV2680_CHIP_ID);
1065		return -ENODEV;
1066	}
1067
1068	dev_info(sensor->dev, "sensor_revision id = 0x%llx, rev= %lld\n",
1069		 chip_id, rev & 0x0f);
1070
1071	return 0;
1072}
1073
1074static int ov2680_parse_dt(struct ov2680_dev *sensor)
1075{
1076	struct v4l2_fwnode_endpoint bus_cfg = {
1077		.bus_type = V4L2_MBUS_CSI2_DPHY,
1078	};
1079	struct device *dev = sensor->dev;
1080	struct fwnode_handle *ep_fwnode;
1081	struct gpio_desc *gpio;
1082	int i, ret;
1083
1084	/*
1085	 * Sometimes the fwnode graph is initialized by the bridge driver.
1086	 * Bridge drivers doing this may also add GPIO mappings, wait for this.
1087	 */
1088	ep_fwnode = fwnode_graph_get_next_endpoint(dev_fwnode(dev), NULL);
1089	if (!ep_fwnode)
1090		return dev_err_probe(dev, -EPROBE_DEFER,
1091				     "waiting for fwnode graph endpoint\n");
1092
1093	ret = v4l2_fwnode_endpoint_alloc_parse(ep_fwnode, &bus_cfg);
1094	fwnode_handle_put(ep_fwnode);
1095	if (ret)
1096		return ret;
1097
1098	/*
1099	 * The pin we want is named XSHUTDN in the datasheet. Linux sensor
1100	 * drivers have standardized on using "powerdown" as con-id name
1101	 * for powerdown or shutdown pins. Older DTB files use "reset",
1102	 * so fallback to that if there is no "powerdown" pin.
1103	 */
1104	gpio = devm_gpiod_get_optional(dev, "powerdown", GPIOD_OUT_HIGH);
1105	if (!gpio)
1106		gpio = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_HIGH);
1107
1108	ret = PTR_ERR_OR_ZERO(gpio);
1109	if (ret < 0) {
1110		dev_dbg(dev, "error while getting reset gpio: %d\n", ret);
1111		goto out_free_bus_cfg;
1112	}
1113
1114	sensor->pwdn_gpio = gpio;
1115
1116	sensor->xvclk = devm_v4l2_sensor_clk_get(dev, "xvclk");
1117	if (IS_ERR(sensor->xvclk)) {
1118		ret = dev_err_probe(dev, PTR_ERR(sensor->xvclk),
1119				    "xvclk clock missing or invalid\n");
1120		goto out_free_bus_cfg;
1121	}
1122
1123	sensor->xvclk_freq = clk_get_rate(sensor->xvclk);
1124
1125	for (i = 0; i < ARRAY_SIZE(ov2680_xvclk_freqs); i++) {
1126		if (sensor->xvclk_freq == ov2680_xvclk_freqs[i])
1127			break;
1128	}
1129
1130	if (i == ARRAY_SIZE(ov2680_xvclk_freqs)) {
1131		ret = dev_err_probe(dev, -EINVAL,
1132				    "unsupported xvclk frequency %d Hz\n",
1133				    sensor->xvclk_freq);
1134		goto out_free_bus_cfg;
1135	}
1136
1137	sensor->pll_mult = ov2680_pll_multipliers[i];
1138
1139	sensor->link_freq[0] = sensor->xvclk_freq / OV2680_PLL_PREDIV0 /
1140			       OV2680_PLL_PREDIV * sensor->pll_mult;
1141
1142	/* CSI-2 is double data rate, bus-format is 10 bpp */
1143	sensor->pixel_rate = sensor->link_freq[0] * 2;
1144	do_div(sensor->pixel_rate, 10);
1145
1146	if (!bus_cfg.nr_of_link_frequencies) {
1147		dev_warn(dev, "Consider passing 'link-frequencies' in DT\n");
1148		goto skip_link_freq_validation;
1149	}
1150
1151	for (i = 0; i < bus_cfg.nr_of_link_frequencies; i++)
1152		if (bus_cfg.link_frequencies[i] == sensor->link_freq[0])
1153			break;
1154
1155	if (bus_cfg.nr_of_link_frequencies == i) {
1156		ret = dev_err_probe(dev, -EINVAL,
1157				    "supported link freq %lld not found\n",
1158				    sensor->link_freq[0]);
1159		goto out_free_bus_cfg;
1160	}
1161
1162skip_link_freq_validation:
1163	ret = 0;
1164out_free_bus_cfg:
1165	v4l2_fwnode_endpoint_free(&bus_cfg);
1166	return ret;
1167}
1168
1169static int ov2680_probe(struct i2c_client *client)
1170{
1171	struct device *dev = &client->dev;
1172	struct ov2680_dev *sensor;
1173	int ret;
1174
1175	sensor = devm_kzalloc(dev, sizeof(*sensor), GFP_KERNEL);
1176	if (!sensor)
1177		return -ENOMEM;
1178
1179	sensor->dev = &client->dev;
1180
1181	sensor->regmap = devm_cci_regmap_init_i2c(client, 16);
1182	if (IS_ERR(sensor->regmap))
1183		return PTR_ERR(sensor->regmap);
1184
1185	ret = ov2680_parse_dt(sensor);
1186	if (ret < 0)
1187		return ret;
1188
1189	ret = ov2680_mode_init(sensor);
1190	if (ret < 0)
1191		return ret;
1192
1193	ret = ov2680_get_regulators(sensor);
1194	if (ret < 0) {
1195		dev_err(dev, "failed to get regulators\n");
1196		return ret;
1197	}
1198
1199	mutex_init(&sensor->lock);
1200
1201	/*
1202	 * Power up and verify the chip now, so that if runtime pm is
1203	 * disabled the chip is left on and streaming will work.
1204	 */
1205	ret = ov2680_power_on(sensor);
1206	if (ret < 0)
1207		goto lock_destroy;
1208
1209	ret = ov2680_check_id(sensor);
1210	if (ret < 0)
1211		goto err_powerdown;
1212
1213	pm_runtime_set_active(&client->dev);
1214	pm_runtime_get_noresume(&client->dev);
1215	pm_runtime_enable(&client->dev);
1216
1217	ret = ov2680_v4l2_register(sensor);
1218	if (ret < 0)
1219		goto err_pm_runtime;
1220
1221	pm_runtime_set_autosuspend_delay(&client->dev, 1000);
1222	pm_runtime_use_autosuspend(&client->dev);
1223	pm_runtime_put_autosuspend(&client->dev);
1224
1225	return 0;
1226
1227err_pm_runtime:
1228	pm_runtime_disable(&client->dev);
1229	pm_runtime_put_noidle(&client->dev);
1230err_powerdown:
1231	ov2680_power_off(sensor);
1232lock_destroy:
1233	dev_err(dev, "ov2680 init fail: %d\n", ret);
1234	mutex_destroy(&sensor->lock);
1235
1236	return ret;
1237}
1238
1239static void ov2680_remove(struct i2c_client *client)
1240{
1241	struct v4l2_subdev *sd = i2c_get_clientdata(client);
1242	struct ov2680_dev *sensor = to_ov2680_dev(sd);
1243
1244	v4l2_async_unregister_subdev(&sensor->sd);
1245	mutex_destroy(&sensor->lock);
1246	media_entity_cleanup(&sensor->sd.entity);
1247	v4l2_ctrl_handler_free(&sensor->ctrls.handler);
1248
1249	/*
1250	 * Disable runtime PM. In case runtime PM is disabled in the kernel,
1251	 * make sure to turn power off manually.
1252	 */
1253	pm_runtime_disable(&client->dev);
1254	if (!pm_runtime_status_suspended(&client->dev))
1255		ov2680_power_off(sensor);
1256	pm_runtime_set_suspended(&client->dev);
1257}
1258
1259static int ov2680_suspend(struct device *dev)
1260{
1261	struct v4l2_subdev *sd = dev_get_drvdata(dev);
1262	struct ov2680_dev *sensor = to_ov2680_dev(sd);
1263
1264	if (sensor->is_streaming)
1265		ov2680_stream_disable(sensor);
1266
1267	return ov2680_power_off(sensor);
1268}
1269
1270static int ov2680_resume(struct device *dev)
1271{
1272	struct v4l2_subdev *sd = dev_get_drvdata(dev);
1273	struct ov2680_dev *sensor = to_ov2680_dev(sd);
1274	int ret;
1275
1276	ret = ov2680_power_on(sensor);
1277	if (ret < 0)
1278		goto stream_disable;
1279
1280	if (sensor->is_streaming) {
1281		ret = ov2680_stream_enable(sensor);
1282		if (ret < 0)
1283			goto stream_disable;
1284	}
1285
1286	return 0;
1287
1288stream_disable:
1289	ov2680_stream_disable(sensor);
1290	sensor->is_streaming = false;
1291
1292	return ret;
1293}
1294
1295static DEFINE_RUNTIME_DEV_PM_OPS(ov2680_pm_ops, ov2680_suspend, ov2680_resume,
1296				 NULL);
1297
1298static const struct of_device_id ov2680_dt_ids[] = {
1299	{ .compatible = "ovti,ov2680" },
1300	{ /* sentinel */ },
1301};
1302MODULE_DEVICE_TABLE(of, ov2680_dt_ids);
1303
1304static const struct acpi_device_id ov2680_acpi_ids[] = {
1305	{ "OVTI2680" },
1306	{ /* sentinel */ }
1307};
1308MODULE_DEVICE_TABLE(acpi, ov2680_acpi_ids);
1309
1310static struct i2c_driver ov2680_i2c_driver = {
1311	.driver = {
1312		.name  = "ov2680",
1313		.pm = pm_sleep_ptr(&ov2680_pm_ops),
1314		.of_match_table	= ov2680_dt_ids,
1315		.acpi_match_table = ov2680_acpi_ids,
1316	},
1317	.probe		= ov2680_probe,
1318	.remove		= ov2680_remove,
1319};
1320module_i2c_driver(ov2680_i2c_driver);
1321
1322MODULE_AUTHOR("Rui Miguel Silva <rui.silva@linaro.org>");
1323MODULE_DESCRIPTION("OV2680 CMOS Image Sensor driver");
1324MODULE_LICENSE("GPL v2");
~/ Read-only file preview.