File Index
Files and directories
Sign in
FILE

et8ek8_driver.c

File preview with syntax highlighting.

linux-6.19.14/drivers/media/i2c/et8ek8/et8ek8_driver.c C
   1// SPDX-License-Identifier: GPL-2.0-only
   2/*
   3 * et8ek8_driver.c
   4 *
   5 * Copyright (C) 2008 Nokia Corporation
   6 *
   7 * Contact: Sakari Ailus <sakari.ailus@iki.fi>
   8 *          Tuukka Toivonen <tuukkat76@gmail.com>
   9 *          Pavel Machek <pavel@ucw.cz>
  10 *
  11 * Based on code from Toni Leinonen <toni.leinonen@offcode.fi>.
  12 *
  13 * This driver is based on the Micron MT9T012 camera imager driver
  14 * (C) Texas Instruments.
  15 */
  16
  17#include <linux/clk.h>
  18#include <linux/delay.h>
  19#include <linux/gpio/consumer.h>
  20#include <linux/i2c.h>
  21#include <linux/kernel.h>
  22#include <linux/module.h>
  23#include <linux/mutex.h>
  24#include <linux/regulator/consumer.h>
  25#include <linux/slab.h>
  26#include <linux/sort.h>
  27#include <linux/v4l2-mediabus.h>
  28
  29#include <media/media-entity.h>
  30#include <media/v4l2-ctrls.h>
  31#include <media/v4l2-device.h>
  32#include <media/v4l2-subdev.h>
  33
  34#include "et8ek8_reg.h"
  35
  36#define ET8EK8_NAME		"et8ek8"
  37#define ET8EK8_PRIV_MEM_SIZE	128
  38#define ET8EK8_MAX_MSG		8
  39
  40struct et8ek8_sensor {
  41	struct v4l2_subdev subdev;
  42	struct media_pad pad;
  43	struct v4l2_mbus_framefmt format;
  44	struct gpio_desc *reset;
  45	struct regulator *vana;
  46	struct clk *ext_clk;
  47	u32 xclk_freq;
  48
  49	u16 version;
  50
  51	struct v4l2_ctrl_handler ctrl_handler;
  52	struct v4l2_ctrl *exposure;
  53	struct v4l2_ctrl *pixel_rate;
  54	struct et8ek8_reglist *current_reglist;
  55
  56	u8 priv_mem[ET8EK8_PRIV_MEM_SIZE];
  57
  58	struct mutex power_lock;
  59	int power_count;
  60};
  61
  62#define to_et8ek8_sensor(sd)	container_of(sd, struct et8ek8_sensor, subdev)
  63
  64enum et8ek8_versions {
  65	ET8EK8_REV_1 = 0x0001,
  66	ET8EK8_REV_2,
  67};
  68
  69/*
  70 * This table describes what should be written to the sensor register
  71 * for each gain value. The gain(index in the table) is in terms of
  72 * 0.1EV, i.e. 10 indexes in the table give 2 time more gain [0] in
  73 * the *analog gain, [1] in the digital gain
  74 *
  75 * Analog gain [dB] = 20*log10(regvalue/32); 0x20..0x100
  76 */
  77static struct et8ek8_gain {
  78	u16 analog;
  79	u16 digital;
  80} const et8ek8_gain_table[] = {
  81	{ 32,    0},  /* x1 */
  82	{ 34,    0},
  83	{ 37,    0},
  84	{ 39,    0},
  85	{ 42,    0},
  86	{ 45,    0},
  87	{ 49,    0},
  88	{ 52,    0},
  89	{ 56,    0},
  90	{ 60,    0},
  91	{ 64,    0},  /* x2 */
  92	{ 69,    0},
  93	{ 74,    0},
  94	{ 79,    0},
  95	{ 84,    0},
  96	{ 91,    0},
  97	{ 97,    0},
  98	{104,    0},
  99	{111,    0},
 100	{119,    0},
 101	{128,    0},  /* x4 */
 102	{137,    0},
 103	{147,    0},
 104	{158,    0},
 105	{169,    0},
 106	{181,    0},
 107	{194,    0},
 108	{208,    0},
 109	{223,    0},
 110	{239,    0},
 111	{256,    0},  /* x8 */
 112	{256,   73},
 113	{256,  152},
 114	{256,  236},
 115	{256,  327},
 116	{256,  424},
 117	{256,  528},
 118	{256,  639},
 119	{256,  758},
 120	{256,  886},
 121	{256, 1023},  /* x16 */
 122};
 123
 124/* Register definitions */
 125#define REG_REVISION_NUMBER_L	0x1200
 126#define REG_REVISION_NUMBER_H	0x1201
 127
 128#define PRIV_MEM_START_REG	0x0008
 129#define PRIV_MEM_WIN_SIZE	8
 130
 131#define ET8EK8_I2C_DELAY	3	/* msec delay b/w accesses */
 132
 133#define USE_CRC			1
 134
 135/*
 136 * Register access helpers
 137 *
 138 * Read a 8/16/32-bit i2c register.  The value is returned in 'val'.
 139 * Returns zero if successful, or non-zero otherwise.
 140 */
 141static int et8ek8_i2c_read_reg(struct i2c_client *client, u16 data_length,
 142			       u16 reg, u32 *val)
 143{
 144	int r;
 145	struct i2c_msg msg;
 146	unsigned char data[4];
 147
 148	if (!client->adapter)
 149		return -ENODEV;
 150	if (data_length != ET8EK8_REG_8BIT && data_length != ET8EK8_REG_16BIT)
 151		return -EINVAL;
 152
 153	msg.addr = client->addr;
 154	msg.flags = 0;
 155	msg.len = 2;
 156	msg.buf = data;
 157
 158	/* high byte goes out first */
 159	data[0] = (u8) (reg >> 8);
 160	data[1] = (u8) (reg & 0xff);
 161	r = i2c_transfer(client->adapter, &msg, 1);
 162	if (r < 0)
 163		goto err;
 164
 165	msg.len = data_length;
 166	msg.flags = I2C_M_RD;
 167	r = i2c_transfer(client->adapter, &msg, 1);
 168	if (r < 0)
 169		goto err;
 170
 171	*val = 0;
 172	/* high byte comes first */
 173	if (data_length == ET8EK8_REG_8BIT)
 174		*val = data[0];
 175	else
 176		*val = (data[1] << 8) + data[0];
 177
 178	return 0;
 179
 180err:
 181	dev_err(&client->dev, "read from offset 0x%x error %d\n", reg, r);
 182
 183	return r;
 184}
 185
 186static void et8ek8_i2c_create_msg(struct i2c_client *client, u16 len, u16 reg,
 187				  u32 val, struct i2c_msg *msg,
 188				  unsigned char *buf)
 189{
 190	msg->addr = client->addr;
 191	msg->flags = 0; /* Write */
 192	msg->len = 2 + len;
 193	msg->buf = buf;
 194
 195	/* high byte goes out first */
 196	buf[0] = (u8) (reg >> 8);
 197	buf[1] = (u8) (reg & 0xff);
 198
 199	switch (len) {
 200	case ET8EK8_REG_8BIT:
 201		buf[2] = (u8) (val) & 0xff;
 202		break;
 203	case ET8EK8_REG_16BIT:
 204		buf[2] = (u8) (val) & 0xff;
 205		buf[3] = (u8) (val >> 8) & 0xff;
 206		break;
 207	default:
 208		WARN_ONCE(1, ET8EK8_NAME ": %s: invalid message length.\n",
 209			  __func__);
 210	}
 211}
 212
 213/*
 214 * A buffered write method that puts the wanted register write
 215 * commands in smaller number of message lists and passes the lists to
 216 * the i2c framework
 217 */
 218static int et8ek8_i2c_buffered_write_regs(struct i2c_client *client,
 219					  const struct et8ek8_reg *wnext,
 220					  int cnt)
 221{
 222	struct i2c_msg msg[ET8EK8_MAX_MSG];
 223	unsigned char data[ET8EK8_MAX_MSG][6];
 224	int wcnt = 0;
 225	u16 reg, data_length;
 226	u32 val;
 227	int rval;
 228
 229	/* Create new write messages for all writes */
 230	while (wcnt < cnt) {
 231		data_length = wnext->type;
 232		reg = wnext->reg;
 233		val = wnext->val;
 234		wnext++;
 235
 236		et8ek8_i2c_create_msg(client, data_length, reg,
 237				    val, &msg[wcnt], &data[wcnt][0]);
 238
 239		/* Update write count */
 240		wcnt++;
 241
 242		if (wcnt < ET8EK8_MAX_MSG)
 243			continue;
 244
 245		rval = i2c_transfer(client->adapter, msg, wcnt);
 246		if (rval < 0)
 247			return rval;
 248
 249		cnt -= wcnt;
 250		wcnt = 0;
 251	}
 252
 253	rval = i2c_transfer(client->adapter, msg, wcnt);
 254
 255	return rval < 0 ? rval : 0;
 256}
 257
 258/*
 259 * Write a list of registers to i2c device.
 260 *
 261 * The list of registers is terminated by ET8EK8_REG_TERM.
 262 * Returns zero if successful, or non-zero otherwise.
 263 */
 264static int et8ek8_i2c_write_regs(struct i2c_client *client,
 265				 const struct et8ek8_reg *regs)
 266{
 267	int r, cnt = 0;
 268	const struct et8ek8_reg *next;
 269
 270	if (!client->adapter)
 271		return -ENODEV;
 272
 273	if (!regs)
 274		return -EINVAL;
 275
 276	/* Initialize list pointers to the start of the list */
 277	next = regs;
 278
 279	do {
 280		/*
 281		 * We have to go through the list to figure out how
 282		 * many regular writes we have in a row
 283		 */
 284		while (next->type != ET8EK8_REG_TERM &&
 285		       next->type != ET8EK8_REG_DELAY) {
 286			/*
 287			 * Here we check that the actual length fields
 288			 * are valid
 289			 */
 290			if (WARN(next->type != ET8EK8_REG_8BIT &&
 291				 next->type != ET8EK8_REG_16BIT,
 292				 "Invalid type = %d", next->type)) {
 293				return -EINVAL;
 294			}
 295			/*
 296			 * Increment count of successive writes and
 297			 * read pointer
 298			 */
 299			cnt++;
 300			next++;
 301		}
 302
 303		/* Now we start writing ... */
 304		r = et8ek8_i2c_buffered_write_regs(client, regs, cnt);
 305
 306		/* ... and then check that everything was OK */
 307		if (r < 0) {
 308			dev_err(&client->dev, "i2c transfer error!\n");
 309			return r;
 310		}
 311
 312		/*
 313		 * If we ran into a sleep statement when going through
 314		 * the list, this is where we snooze for the required time
 315		 */
 316		if (next->type == ET8EK8_REG_DELAY) {
 317			msleep(next->val);
 318			/*
 319			 * ZZZ ...
 320			 * Update list pointers and cnt and start over ...
 321			 */
 322			next++;
 323			regs = next;
 324			cnt = 0;
 325		}
 326	} while (next->type != ET8EK8_REG_TERM);
 327
 328	return 0;
 329}
 330
 331/*
 332 * Write to a 8/16-bit register.
 333 * Returns zero if successful, or non-zero otherwise.
 334 */
 335static int et8ek8_i2c_write_reg(struct i2c_client *client, u16 data_length,
 336				u16 reg, u32 val)
 337{
 338	int r;
 339	struct i2c_msg msg;
 340	unsigned char data[6];
 341
 342	if (!client->adapter)
 343		return -ENODEV;
 344	if (data_length != ET8EK8_REG_8BIT && data_length != ET8EK8_REG_16BIT)
 345		return -EINVAL;
 346
 347	et8ek8_i2c_create_msg(client, data_length, reg, val, &msg, data);
 348
 349	r = i2c_transfer(client->adapter, &msg, 1);
 350	if (r < 0) {
 351		dev_err(&client->dev,
 352			"wrote 0x%x to offset 0x%x error %d\n", val, reg, r);
 353		return r;
 354	}
 355
 356	return 0;
 357}
 358
 359static struct et8ek8_reglist *et8ek8_reglist_find_type(
 360		struct et8ek8_meta_reglist *meta,
 361		u16 type)
 362{
 363	struct et8ek8_reglist **next = &meta->reglist[0].ptr;
 364
 365	while (*next) {
 366		if ((*next)->type == type)
 367			return *next;
 368
 369		next++;
 370	}
 371
 372	return NULL;
 373}
 374
 375static int et8ek8_i2c_reglist_find_write(struct i2c_client *client,
 376					 struct et8ek8_meta_reglist *meta,
 377					 u16 type)
 378{
 379	struct et8ek8_reglist *reglist;
 380
 381	reglist = et8ek8_reglist_find_type(meta, type);
 382	if (!reglist)
 383		return -EINVAL;
 384
 385	return et8ek8_i2c_write_regs(client, reglist->regs);
 386}
 387
 388static struct et8ek8_reglist **et8ek8_reglist_first(
 389		struct et8ek8_meta_reglist *meta)
 390{
 391	return &meta->reglist[0].ptr;
 392}
 393
 394static void et8ek8_reglist_to_mbus(const struct et8ek8_reglist *reglist,
 395				   struct v4l2_mbus_framefmt *fmt)
 396{
 397	fmt->width = reglist->mode.window_width;
 398	fmt->height = reglist->mode.window_height;
 399	fmt->code = reglist->mode.bus_format;
 400}
 401
 402static struct et8ek8_reglist *et8ek8_reglist_find_mode_fmt(
 403		struct et8ek8_meta_reglist *meta,
 404		struct v4l2_mbus_framefmt *fmt)
 405{
 406	struct et8ek8_reglist **list = et8ek8_reglist_first(meta);
 407	struct et8ek8_reglist *best_match = NULL;
 408	struct et8ek8_reglist *best_other = NULL;
 409	struct v4l2_mbus_framefmt format;
 410	unsigned int max_dist_match = (unsigned int)-1;
 411	unsigned int max_dist_other = (unsigned int)-1;
 412
 413	/*
 414	 * Find the mode with the closest image size. The distance between
 415	 * image sizes is the size in pixels of the non-overlapping regions
 416	 * between the requested size and the frame-specified size.
 417	 *
 418	 * Store both the closest mode that matches the requested format, and
 419	 * the closest mode for all other formats. The best match is returned
 420	 * if found, otherwise the best mode with a non-matching format is
 421	 * returned.
 422	 */
 423	for (; *list; list++) {
 424		unsigned int dist;
 425
 426		if ((*list)->type != ET8EK8_REGLIST_MODE)
 427			continue;
 428
 429		et8ek8_reglist_to_mbus(*list, &format);
 430
 431		dist = min(fmt->width, format.width)
 432		     * min(fmt->height, format.height);
 433		dist = format.width * format.height
 434		     + fmt->width * fmt->height - 2 * dist;
 435
 436
 437		if (fmt->code == format.code) {
 438			if (dist < max_dist_match || !best_match) {
 439				best_match = *list;
 440				max_dist_match = dist;
 441			}
 442		} else {
 443			if (dist < max_dist_other || !best_other) {
 444				best_other = *list;
 445				max_dist_other = dist;
 446			}
 447		}
 448	}
 449
 450	return best_match ? best_match : best_other;
 451}
 452
 453#define TIMEPERFRAME_AVG_FPS(t)						\
 454	(((t).denominator + ((t).numerator >> 1)) / (t).numerator)
 455
 456static struct et8ek8_reglist *et8ek8_reglist_find_mode_ival(
 457		struct et8ek8_meta_reglist *meta,
 458		struct et8ek8_reglist *current_reglist,
 459		struct v4l2_fract *timeperframe)
 460{
 461	int fps = TIMEPERFRAME_AVG_FPS(*timeperframe);
 462	struct et8ek8_reglist **list = et8ek8_reglist_first(meta);
 463	struct et8ek8_mode *current_mode = &current_reglist->mode;
 464
 465	for (; *list; list++) {
 466		struct et8ek8_mode *mode = &(*list)->mode;
 467
 468		if ((*list)->type != ET8EK8_REGLIST_MODE)
 469			continue;
 470
 471		if (mode->window_width != current_mode->window_width ||
 472		    mode->window_height != current_mode->window_height)
 473			continue;
 474
 475		if (TIMEPERFRAME_AVG_FPS(mode->timeperframe) == fps)
 476			return *list;
 477	}
 478
 479	return NULL;
 480}
 481
 482static int et8ek8_reglist_cmp(const void *a, const void *b)
 483{
 484	const struct et8ek8_reglist **list1 = (const struct et8ek8_reglist **)a,
 485		**list2 = (const struct et8ek8_reglist **)b;
 486
 487	/* Put real modes in the beginning. */
 488	if ((*list1)->type == ET8EK8_REGLIST_MODE &&
 489	    (*list2)->type != ET8EK8_REGLIST_MODE)
 490		return -1;
 491	if ((*list1)->type != ET8EK8_REGLIST_MODE &&
 492	    (*list2)->type == ET8EK8_REGLIST_MODE)
 493		return 1;
 494
 495	/* Descending width. */
 496	if ((*list1)->mode.window_width > (*list2)->mode.window_width)
 497		return -1;
 498	if ((*list1)->mode.window_width < (*list2)->mode.window_width)
 499		return 1;
 500
 501	if ((*list1)->mode.window_height > (*list2)->mode.window_height)
 502		return -1;
 503	if ((*list1)->mode.window_height < (*list2)->mode.window_height)
 504		return 1;
 505
 506	return 0;
 507}
 508
 509static int et8ek8_reglist_import(struct i2c_client *client,
 510				 struct et8ek8_meta_reglist *meta)
 511{
 512	int nlists = 0, i;
 513
 514	dev_info(&client->dev, "meta_reglist version %s\n", meta->version);
 515
 516	while (meta->reglist[nlists].ptr)
 517		nlists++;
 518
 519	if (!nlists)
 520		return -EINVAL;
 521
 522	sort(&meta->reglist[0].ptr, nlists, sizeof(meta->reglist[0].ptr),
 523	     et8ek8_reglist_cmp, NULL);
 524
 525	i = nlists;
 526	nlists = 0;
 527
 528	while (i--) {
 529		struct et8ek8_reglist *list;
 530
 531		list = meta->reglist[nlists].ptr;
 532
 533		dev_dbg(&client->dev,
 534		       "%s: type %d\tw %d\th %d\tfmt %x\tival %d/%d\tptr %p\n",
 535		       __func__,
 536		       list->type,
 537		       list->mode.window_width, list->mode.window_height,
 538		       list->mode.bus_format,
 539		       list->mode.timeperframe.numerator,
 540		       list->mode.timeperframe.denominator,
 541		       (void *)meta->reglist[nlists].ptr);
 542
 543		nlists++;
 544	}
 545
 546	return 0;
 547}
 548
 549/* Called to change the V4L2 gain control value. This function
 550 * rounds and clamps the given value and updates the V4L2 control value.
 551 * If power is on, also updates the sensor analog and digital gains.
 552 * gain is in 0.1 EV (exposure value) units.
 553 */
 554static int et8ek8_set_gain(struct et8ek8_sensor *sensor, s32 gain)
 555{
 556	struct i2c_client *client = v4l2_get_subdevdata(&sensor->subdev);
 557	struct et8ek8_gain new;
 558	int r;
 559
 560	new = et8ek8_gain_table[gain];
 561
 562	/* FIXME: optimise I2C writes! */
 563	r = et8ek8_i2c_write_reg(client, ET8EK8_REG_8BIT,
 564				0x124a, new.analog >> 8);
 565	if (r)
 566		return r;
 567	r = et8ek8_i2c_write_reg(client, ET8EK8_REG_8BIT,
 568				0x1249, new.analog & 0xff);
 569	if (r)
 570		return r;
 571
 572	r = et8ek8_i2c_write_reg(client, ET8EK8_REG_8BIT,
 573				0x124d, new.digital >> 8);
 574	if (r)
 575		return r;
 576	r = et8ek8_i2c_write_reg(client, ET8EK8_REG_8BIT,
 577				0x124c, new.digital & 0xff);
 578
 579	return r;
 580}
 581
 582static int et8ek8_set_test_pattern(struct et8ek8_sensor *sensor, s32 mode)
 583{
 584	struct i2c_client *client = v4l2_get_subdevdata(&sensor->subdev);
 585	int cbh_mode, cbv_mode, tp_mode, din_sw, r1420, rval;
 586
 587	/* Values for normal mode */
 588	cbh_mode = 0;
 589	cbv_mode = 0;
 590	tp_mode  = 0;
 591	din_sw   = 0x00;
 592	r1420    = 0xF0;
 593
 594	if (mode) {
 595		/* Test pattern mode */
 596		if (mode < 5) {
 597			cbh_mode = 1;
 598			cbv_mode = 1;
 599			tp_mode  = mode + 3;
 600		} else {
 601			cbh_mode = 0;
 602			cbv_mode = 0;
 603			tp_mode  = mode - 4 + 3;
 604		}
 605
 606		din_sw   = 0x01;
 607		r1420    = 0xE0;
 608	}
 609
 610	rval = et8ek8_i2c_write_reg(client, ET8EK8_REG_8BIT, 0x111B,
 611				    tp_mode << 4);
 612	if (rval)
 613		return rval;
 614
 615	rval = et8ek8_i2c_write_reg(client, ET8EK8_REG_8BIT, 0x1121,
 616				    cbh_mode << 7);
 617	if (rval)
 618		return rval;
 619
 620	rval = et8ek8_i2c_write_reg(client, ET8EK8_REG_8BIT, 0x1124,
 621				    cbv_mode << 7);
 622	if (rval)
 623		return rval;
 624
 625	rval = et8ek8_i2c_write_reg(client, ET8EK8_REG_8BIT, 0x112C, din_sw);
 626	if (rval)
 627		return rval;
 628
 629	return et8ek8_i2c_write_reg(client, ET8EK8_REG_8BIT, 0x1420, r1420);
 630}
 631
 632/* -----------------------------------------------------------------------------
 633 * V4L2 controls
 634 */
 635
 636static int et8ek8_set_ctrl(struct v4l2_ctrl *ctrl)
 637{
 638	struct et8ek8_sensor *sensor =
 639		container_of(ctrl->handler, struct et8ek8_sensor, ctrl_handler);
 640
 641	switch (ctrl->id) {
 642	case V4L2_CID_GAIN:
 643		return et8ek8_set_gain(sensor, ctrl->val);
 644
 645	case V4L2_CID_EXPOSURE:
 646	{
 647		struct i2c_client *client =
 648			v4l2_get_subdevdata(&sensor->subdev);
 649
 650		return et8ek8_i2c_write_reg(client, ET8EK8_REG_16BIT, 0x1243,
 651					    ctrl->val);
 652	}
 653
 654	case V4L2_CID_TEST_PATTERN:
 655		return et8ek8_set_test_pattern(sensor, ctrl->val);
 656
 657	case V4L2_CID_PIXEL_RATE:
 658		return 0;
 659
 660	default:
 661		return -EINVAL;
 662	}
 663}
 664
 665static const struct v4l2_ctrl_ops et8ek8_ctrl_ops = {
 666	.s_ctrl = et8ek8_set_ctrl,
 667};
 668
 669static const char * const et8ek8_test_pattern_menu[] = {
 670	"Normal",
 671	"Vertical colorbar",
 672	"Horizontal colorbar",
 673	"Scale",
 674	"Ramp",
 675	"Small vertical colorbar",
 676	"Small horizontal colorbar",
 677	"Small scale",
 678	"Small ramp",
 679};
 680
 681static int et8ek8_init_controls(struct et8ek8_sensor *sensor)
 682{
 683	s32 max_rows;
 684
 685	v4l2_ctrl_handler_init(&sensor->ctrl_handler, 4);
 686
 687	/* V4L2_CID_GAIN */
 688	v4l2_ctrl_new_std(&sensor->ctrl_handler, &et8ek8_ctrl_ops,
 689			  V4L2_CID_GAIN, 0, ARRAY_SIZE(et8ek8_gain_table) - 1,
 690			  1, 0);
 691
 692	max_rows = sensor->current_reglist->mode.max_exp;
 693	{
 694		u32 min = 1, max = max_rows;
 695
 696		sensor->exposure =
 697			v4l2_ctrl_new_std(&sensor->ctrl_handler,
 698					  &et8ek8_ctrl_ops, V4L2_CID_EXPOSURE,
 699					  min, max, min, max);
 700	}
 701
 702	/* V4L2_CID_PIXEL_RATE */
 703	sensor->pixel_rate =
 704		v4l2_ctrl_new_std(&sensor->ctrl_handler, &et8ek8_ctrl_ops,
 705		V4L2_CID_PIXEL_RATE, 1, INT_MAX, 1, 1);
 706
 707	/* V4L2_CID_TEST_PATTERN */
 708	v4l2_ctrl_new_std_menu_items(&sensor->ctrl_handler,
 709				     &et8ek8_ctrl_ops, V4L2_CID_TEST_PATTERN,
 710				     ARRAY_SIZE(et8ek8_test_pattern_menu) - 1,
 711				     0, 0, et8ek8_test_pattern_menu);
 712
 713	if (sensor->ctrl_handler.error)
 714		return sensor->ctrl_handler.error;
 715
 716	sensor->subdev.ctrl_handler = &sensor->ctrl_handler;
 717
 718	return 0;
 719}
 720
 721static void et8ek8_update_controls(struct et8ek8_sensor *sensor)
 722{
 723	struct v4l2_ctrl *ctrl;
 724	struct et8ek8_mode *mode = &sensor->current_reglist->mode;
 725
 726	u32 min, max, pixel_rate;
 727	static const int S = 8;
 728
 729	ctrl = sensor->exposure;
 730
 731	min = 1;
 732	max = mode->max_exp;
 733
 734	/*
 735	 * Calculate average pixel clock per line. Assume buffers can spread
 736	 * the data over horizontal blanking time. Rounding upwards.
 737	 * Formula taken from stock Nokia N900 kernel.
 738	 */
 739	pixel_rate = ((mode->pixel_clock + (1 << S) - 1) >> S) + mode->width;
 740	pixel_rate = mode->window_width * (pixel_rate - 1) / mode->width;
 741
 742	__v4l2_ctrl_modify_range(ctrl, min, max, min, max);
 743	__v4l2_ctrl_s_ctrl_int64(sensor->pixel_rate, pixel_rate << S);
 744}
 745
 746static int et8ek8_configure(struct et8ek8_sensor *sensor)
 747{
 748	struct v4l2_subdev *subdev = &sensor->subdev;
 749	struct i2c_client *client = v4l2_get_subdevdata(subdev);
 750	int rval;
 751
 752	rval = et8ek8_i2c_write_regs(client, sensor->current_reglist->regs);
 753	if (rval)
 754		goto fail;
 755
 756	/* Controls set while the power to the sensor is turned off are saved
 757	 * but not applied to the hardware. Now that we're about to start
 758	 * streaming apply all the current values to the hardware.
 759	 */
 760	rval = v4l2_ctrl_handler_setup(&sensor->ctrl_handler);
 761	if (rval)
 762		goto fail;
 763
 764	return 0;
 765
 766fail:
 767	dev_err(&client->dev, "sensor configuration failed\n");
 768
 769	return rval;
 770}
 771
 772static int et8ek8_stream_on(struct et8ek8_sensor *sensor)
 773{
 774	struct i2c_client *client = v4l2_get_subdevdata(&sensor->subdev);
 775
 776	return et8ek8_i2c_write_reg(client, ET8EK8_REG_8BIT, 0x1252, 0xb0);
 777}
 778
 779static int et8ek8_stream_off(struct et8ek8_sensor *sensor)
 780{
 781	struct i2c_client *client = v4l2_get_subdevdata(&sensor->subdev);
 782
 783	return et8ek8_i2c_write_reg(client, ET8EK8_REG_8BIT, 0x1252, 0x30);
 784}
 785
 786static int et8ek8_s_stream(struct v4l2_subdev *subdev, int streaming)
 787{
 788	struct et8ek8_sensor *sensor = to_et8ek8_sensor(subdev);
 789	int ret;
 790
 791	if (!streaming)
 792		return et8ek8_stream_off(sensor);
 793
 794	ret = et8ek8_configure(sensor);
 795	if (ret < 0)
 796		return ret;
 797
 798	return et8ek8_stream_on(sensor);
 799}
 800
 801/* --------------------------------------------------------------------------
 802 * V4L2 subdev operations
 803 */
 804
 805static int et8ek8_power_off(struct et8ek8_sensor *sensor)
 806{
 807	gpiod_set_value(sensor->reset, 0);
 808	udelay(1);
 809
 810	clk_disable_unprepare(sensor->ext_clk);
 811
 812	return regulator_disable(sensor->vana);
 813}
 814
 815static int et8ek8_power_on(struct et8ek8_sensor *sensor)
 816{
 817	struct v4l2_subdev *subdev = &sensor->subdev;
 818	struct i2c_client *client = v4l2_get_subdevdata(subdev);
 819	int val, rval;
 820
 821	rval = regulator_enable(sensor->vana);
 822	if (rval) {
 823		dev_err(&client->dev, "failed to enable vana regulator\n");
 824		return rval;
 825	}
 826
 827	rval = clk_prepare_enable(sensor->ext_clk);
 828	if (rval < 0) {
 829		dev_err(&client->dev, "failed to enable extclk\n");
 830		goto out;
 831	}
 832
 833	if (rval)
 834		goto out;
 835
 836	udelay(10); /* I wish this is a good value */
 837
 838	gpiod_set_value(sensor->reset, 1);
 839
 840	msleep(5000 * 1000 / sensor->xclk_freq + 1); /* Wait 5000 cycles */
 841
 842	rval = et8ek8_i2c_reglist_find_write(client, &meta_reglist,
 843					     ET8EK8_REGLIST_POWERON);
 844	if (rval)
 845		goto out;
 846
 847#ifdef USE_CRC
 848	rval = et8ek8_i2c_read_reg(client, ET8EK8_REG_8BIT, 0x1263, &val);
 849	if (rval)
 850		goto out;
 851#if USE_CRC /* TODO get crc setting from DT */
 852	val |= BIT(4);
 853#else
 854	val &= ~BIT(4);
 855#endif
 856	rval = et8ek8_i2c_write_reg(client, ET8EK8_REG_8BIT, 0x1263, val);
 857	if (rval)
 858		goto out;
 859#endif
 860
 861out:
 862	if (rval)
 863		et8ek8_power_off(sensor);
 864
 865	return rval;
 866}
 867
 868/* --------------------------------------------------------------------------
 869 * V4L2 subdev video operations
 870 */
 871#define MAX_FMTS 4
 872static int et8ek8_enum_mbus_code(struct v4l2_subdev *subdev,
 873				 struct v4l2_subdev_state *sd_state,
 874				 struct v4l2_subdev_mbus_code_enum *code)
 875{
 876	struct et8ek8_reglist **list =
 877			et8ek8_reglist_first(&meta_reglist);
 878	u32 pixelformat[MAX_FMTS];
 879	int npixelformat = 0;
 880
 881	if (code->index >= MAX_FMTS)
 882		return -EINVAL;
 883
 884	for (; *list; list++) {
 885		struct et8ek8_mode *mode = &(*list)->mode;
 886		int i;
 887
 888		if ((*list)->type != ET8EK8_REGLIST_MODE)
 889			continue;
 890
 891		for (i = 0; i < npixelformat; i++) {
 892			if (pixelformat[i] == mode->bus_format)
 893				break;
 894		}
 895		if (i != npixelformat)
 896			continue;
 897
 898		if (code->index == npixelformat) {
 899			code->code = mode->bus_format;
 900			return 0;
 901		}
 902
 903		pixelformat[npixelformat] = mode->bus_format;
 904		npixelformat++;
 905	}
 906
 907	return -EINVAL;
 908}
 909
 910static int et8ek8_enum_frame_size(struct v4l2_subdev *subdev,
 911				  struct v4l2_subdev_state *sd_state,
 912				  struct v4l2_subdev_frame_size_enum *fse)
 913{
 914	struct et8ek8_reglist **list =
 915			et8ek8_reglist_first(&meta_reglist);
 916	struct v4l2_mbus_framefmt format;
 917	int cmp_width = INT_MAX;
 918	int cmp_height = INT_MAX;
 919	int index = fse->index;
 920
 921	for (; *list; list++) {
 922		if ((*list)->type != ET8EK8_REGLIST_MODE)
 923			continue;
 924
 925		et8ek8_reglist_to_mbus(*list, &format);
 926		if (fse->code != format.code)
 927			continue;
 928
 929		/* Assume that the modes are grouped by frame size. */
 930		if (format.width == cmp_width && format.height == cmp_height)
 931			continue;
 932
 933		cmp_width = format.width;
 934		cmp_height = format.height;
 935
 936		if (index-- == 0) {
 937			fse->min_width = format.width;
 938			fse->min_height = format.height;
 939			fse->max_width = format.width;
 940			fse->max_height = format.height;
 941			return 0;
 942		}
 943	}
 944
 945	return -EINVAL;
 946}
 947
 948static int et8ek8_enum_frame_ival(struct v4l2_subdev *subdev,
 949				  struct v4l2_subdev_state *sd_state,
 950				  struct v4l2_subdev_frame_interval_enum *fie)
 951{
 952	struct et8ek8_reglist **list =
 953			et8ek8_reglist_first(&meta_reglist);
 954	struct v4l2_mbus_framefmt format;
 955	int index = fie->index;
 956
 957	for (; *list; list++) {
 958		struct et8ek8_mode *mode = &(*list)->mode;
 959
 960		if ((*list)->type != ET8EK8_REGLIST_MODE)
 961			continue;
 962
 963		et8ek8_reglist_to_mbus(*list, &format);
 964		if (fie->code != format.code)
 965			continue;
 966
 967		if (fie->width != format.width || fie->height != format.height)
 968			continue;
 969
 970		if (index-- == 0) {
 971			fie->interval = mode->timeperframe;
 972			return 0;
 973		}
 974	}
 975
 976	return -EINVAL;
 977}
 978
 979static struct v4l2_mbus_framefmt *
 980__et8ek8_get_pad_format(struct et8ek8_sensor *sensor,
 981			struct v4l2_subdev_state *sd_state,
 982			unsigned int pad, enum v4l2_subdev_format_whence which)
 983{
 984	switch (which) {
 985	case V4L2_SUBDEV_FORMAT_TRY:
 986		return v4l2_subdev_state_get_format(sd_state, pad);
 987	case V4L2_SUBDEV_FORMAT_ACTIVE:
 988		return &sensor->format;
 989	default:
 990		return NULL;
 991	}
 992}
 993
 994static int et8ek8_get_pad_format(struct v4l2_subdev *subdev,
 995				 struct v4l2_subdev_state *sd_state,
 996				 struct v4l2_subdev_format *fmt)
 997{
 998	struct et8ek8_sensor *sensor = to_et8ek8_sensor(subdev);
 999	struct v4l2_mbus_framefmt *format;
1000
1001	format = __et8ek8_get_pad_format(sensor, sd_state, fmt->pad,
1002					 fmt->which);
1003	if (!format)
1004		return -EINVAL;
1005
1006	fmt->format = *format;
1007
1008	return 0;
1009}
1010
1011static int et8ek8_set_pad_format(struct v4l2_subdev *subdev,
1012				 struct v4l2_subdev_state *sd_state,
1013				 struct v4l2_subdev_format *fmt)
1014{
1015	struct et8ek8_sensor *sensor = to_et8ek8_sensor(subdev);
1016	struct v4l2_mbus_framefmt *format;
1017	struct et8ek8_reglist *reglist;
1018
1019	format = __et8ek8_get_pad_format(sensor, sd_state, fmt->pad,
1020					 fmt->which);
1021	if (!format)
1022		return -EINVAL;
1023
1024	reglist = et8ek8_reglist_find_mode_fmt(&meta_reglist, &fmt->format);
1025	et8ek8_reglist_to_mbus(reglist, &fmt->format);
1026	*format = fmt->format;
1027
1028	if (fmt->which == V4L2_SUBDEV_FORMAT_ACTIVE) {
1029		sensor->current_reglist = reglist;
1030		et8ek8_update_controls(sensor);
1031	}
1032
1033	return 0;
1034}
1035
1036static int et8ek8_get_frame_interval(struct v4l2_subdev *subdev,
1037				     struct v4l2_subdev_state *sd_state,
1038				     struct v4l2_subdev_frame_interval *fi)
1039{
1040	struct et8ek8_sensor *sensor = to_et8ek8_sensor(subdev);
1041
1042	/*
1043	 * FIXME: Implement support for V4L2_SUBDEV_FORMAT_TRY, using the V4L2
1044	 * subdev active state API.
1045	 */
1046	if (fi->which != V4L2_SUBDEV_FORMAT_ACTIVE)
1047		return -EINVAL;
1048
1049	memset(fi, 0, sizeof(*fi));
1050	fi->interval = sensor->current_reglist->mode.timeperframe;
1051
1052	return 0;
1053}
1054
1055static int et8ek8_set_frame_interval(struct v4l2_subdev *subdev,
1056				     struct v4l2_subdev_state *sd_state,
1057				     struct v4l2_subdev_frame_interval *fi)
1058{
1059	struct et8ek8_sensor *sensor = to_et8ek8_sensor(subdev);
1060	struct et8ek8_reglist *reglist;
1061
1062	/*
1063	 * FIXME: Implement support for V4L2_SUBDEV_FORMAT_TRY, using the V4L2
1064	 * subdev active state API.
1065	 */
1066	if (fi->which != V4L2_SUBDEV_FORMAT_ACTIVE)
1067		return -EINVAL;
1068
1069	reglist = et8ek8_reglist_find_mode_ival(&meta_reglist,
1070						sensor->current_reglist,
1071						&fi->interval);
1072
1073	if (!reglist)
1074		return -EINVAL;
1075
1076	sensor->current_reglist = reglist;
1077	et8ek8_update_controls(sensor);
1078
1079	return 0;
1080}
1081
1082static int et8ek8_g_priv_mem(struct v4l2_subdev *subdev)
1083{
1084	struct et8ek8_sensor *sensor = to_et8ek8_sensor(subdev);
1085	struct i2c_client *client = v4l2_get_subdevdata(subdev);
1086	unsigned int length = ET8EK8_PRIV_MEM_SIZE;
1087	unsigned int offset = 0;
1088	u8 *ptr  = sensor->priv_mem;
1089	int rval = 0;
1090
1091	/* Read the EEPROM window-by-window, each window 8 bytes */
1092	do {
1093		u8 buffer[PRIV_MEM_WIN_SIZE];
1094		struct i2c_msg msg;
1095		int bytes, i;
1096		int ofs;
1097
1098		/* Set the current window */
1099		rval = et8ek8_i2c_write_reg(client, ET8EK8_REG_8BIT, 0x0001,
1100					    0xe0 | (offset >> 3));
1101		if (rval < 0)
1102			return rval;
1103
1104		/* Wait for status bit */
1105		for (i = 0; i < 1000; ++i) {
1106			u32 status;
1107
1108			rval = et8ek8_i2c_read_reg(client, ET8EK8_REG_8BIT,
1109						   0x0003, &status);
1110			if (rval < 0)
1111				return rval;
1112			if (!(status & 0x08))
1113				break;
1114			usleep_range(1000, 2000);
1115		}
1116
1117		if (i == 1000)
1118			return -EIO;
1119
1120		/* Read window, 8 bytes at once, and copy to user space */
1121		ofs = offset & 0x07;	/* Offset within this window */
1122		bytes = length + ofs > 8 ? 8-ofs : length;
1123		msg.addr = client->addr;
1124		msg.flags = 0;
1125		msg.len = 2;
1126		msg.buf = buffer;
1127		ofs += PRIV_MEM_START_REG;
1128		buffer[0] = (u8)(ofs >> 8);
1129		buffer[1] = (u8)(ofs & 0xFF);
1130
1131		rval = i2c_transfer(client->adapter, &msg, 1);
1132		if (rval < 0)
1133			return rval;
1134
1135		mdelay(ET8EK8_I2C_DELAY);
1136		msg.addr = client->addr;
1137		msg.len = bytes;
1138		msg.flags = I2C_M_RD;
1139		msg.buf = buffer;
1140		memset(buffer, 0, sizeof(buffer));
1141
1142		rval = i2c_transfer(client->adapter, &msg, 1);
1143		if (rval < 0)
1144			return rval;
1145
1146		rval = 0;
1147		memcpy(ptr, buffer, bytes);
1148
1149		length -= bytes;
1150		offset += bytes;
1151		ptr += bytes;
1152	} while (length > 0);
1153
1154	return rval;
1155}
1156
1157static int et8ek8_dev_init(struct v4l2_subdev *subdev)
1158{
1159	struct et8ek8_sensor *sensor = to_et8ek8_sensor(subdev);
1160	struct i2c_client *client = v4l2_get_subdevdata(subdev);
1161	int rval, rev_l, rev_h;
1162
1163	rval = et8ek8_power_on(sensor);
1164	if (rval) {
1165		dev_err(&client->dev, "could not power on\n");
1166		return rval;
1167	}
1168
1169	rval = et8ek8_i2c_read_reg(client, ET8EK8_REG_8BIT,
1170				   REG_REVISION_NUMBER_L, &rev_l);
1171	if (!rval)
1172		rval = et8ek8_i2c_read_reg(client, ET8EK8_REG_8BIT,
1173					   REG_REVISION_NUMBER_H, &rev_h);
1174	if (rval) {
1175		dev_err(&client->dev, "no et8ek8 sensor detected\n");
1176		goto out_poweroff;
1177	}
1178
1179	sensor->version = (rev_h << 8) + rev_l;
1180	if (sensor->version != ET8EK8_REV_1 && sensor->version != ET8EK8_REV_2)
1181		dev_info(&client->dev,
1182			 "unknown version 0x%x detected, continuing anyway\n",
1183			 sensor->version);
1184
1185	rval = et8ek8_reglist_import(client, &meta_reglist);
1186	if (rval) {
1187		dev_err(&client->dev,
1188			"invalid register list %s, import failed\n",
1189			ET8EK8_NAME);
1190		goto out_poweroff;
1191	}
1192
1193	sensor->current_reglist = et8ek8_reglist_find_type(&meta_reglist,
1194							   ET8EK8_REGLIST_MODE);
1195	if (!sensor->current_reglist) {
1196		dev_err(&client->dev,
1197			"invalid register list %s, no mode found\n",
1198			ET8EK8_NAME);
1199		rval = -ENODEV;
1200		goto out_poweroff;
1201	}
1202
1203	et8ek8_reglist_to_mbus(sensor->current_reglist, &sensor->format);
1204
1205	rval = et8ek8_i2c_reglist_find_write(client, &meta_reglist,
1206					     ET8EK8_REGLIST_POWERON);
1207	if (rval) {
1208		dev_err(&client->dev,
1209			"invalid register list %s, no POWERON mode found\n",
1210			ET8EK8_NAME);
1211		goto out_poweroff;
1212	}
1213	rval = et8ek8_stream_on(sensor); /* Needed to be able to read EEPROM */
1214	if (rval)
1215		goto out_poweroff;
1216	rval = et8ek8_g_priv_mem(subdev);
1217	if (rval)
1218		dev_warn(&client->dev,
1219			"can not read OTP (EEPROM) memory from sensor\n");
1220	rval = et8ek8_stream_off(sensor);
1221	if (rval)
1222		goto out_poweroff;
1223
1224	rval = et8ek8_power_off(sensor);
1225	if (rval)
1226		goto out_poweroff;
1227
1228	return 0;
1229
1230out_poweroff:
1231	et8ek8_power_off(sensor);
1232
1233	return rval;
1234}
1235
1236/* --------------------------------------------------------------------------
1237 * sysfs attributes
1238 */
1239static ssize_t
1240priv_mem_show(struct device *dev, struct device_attribute *attr, char *buf)
1241{
1242	struct v4l2_subdev *subdev = dev_get_drvdata(dev);
1243	struct et8ek8_sensor *sensor = to_et8ek8_sensor(subdev);
1244
1245#if PAGE_SIZE < ET8EK8_PRIV_MEM_SIZE
1246#error PAGE_SIZE too small!
1247#endif
1248
1249	memcpy(buf, sensor->priv_mem, ET8EK8_PRIV_MEM_SIZE);
1250
1251	return ET8EK8_PRIV_MEM_SIZE;
1252}
1253static DEVICE_ATTR_RO(priv_mem);
1254
1255/* --------------------------------------------------------------------------
1256 * V4L2 subdev core operations
1257 */
1258
1259static int
1260et8ek8_registered(struct v4l2_subdev *subdev)
1261{
1262	struct et8ek8_sensor *sensor = to_et8ek8_sensor(subdev);
1263	struct i2c_client *client = v4l2_get_subdevdata(subdev);
1264	int rval;
1265
1266	dev_dbg(&client->dev, "registered!");
1267
1268	rval = device_create_file(&client->dev, &dev_attr_priv_mem);
1269	if (rval) {
1270		dev_err(&client->dev, "could not register sysfs entry\n");
1271		return rval;
1272	}
1273
1274	rval = et8ek8_dev_init(subdev);
1275	if (rval)
1276		goto err_file;
1277
1278	rval = et8ek8_init_controls(sensor);
1279	if (rval) {
1280		dev_err(&client->dev, "controls initialization failed\n");
1281		goto err_file;
1282	}
1283
1284	__et8ek8_get_pad_format(sensor, NULL, 0, V4L2_SUBDEV_FORMAT_ACTIVE);
1285
1286	return 0;
1287
1288err_file:
1289	device_remove_file(&client->dev, &dev_attr_priv_mem);
1290
1291	return rval;
1292}
1293
1294static int __et8ek8_set_power(struct et8ek8_sensor *sensor, bool on)
1295{
1296	return on ? et8ek8_power_on(sensor) : et8ek8_power_off(sensor);
1297}
1298
1299static int et8ek8_set_power(struct v4l2_subdev *subdev, int on)
1300{
1301	struct et8ek8_sensor *sensor = to_et8ek8_sensor(subdev);
1302	int ret = 0;
1303
1304	mutex_lock(&sensor->power_lock);
1305
1306	/* If the power count is modified from 0 to != 0 or from != 0 to 0,
1307	 * update the power state.
1308	 */
1309	if (sensor->power_count == !on) {
1310		ret = __et8ek8_set_power(sensor, !!on);
1311		if (ret < 0)
1312			goto done;
1313	}
1314
1315	/* Update the power count. */
1316	sensor->power_count += on ? 1 : -1;
1317	WARN_ON(sensor->power_count < 0);
1318
1319done:
1320	mutex_unlock(&sensor->power_lock);
1321
1322	return ret;
1323}
1324
1325static int et8ek8_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
1326{
1327	struct et8ek8_sensor *sensor = to_et8ek8_sensor(sd);
1328	struct v4l2_mbus_framefmt *format;
1329	struct et8ek8_reglist *reglist;
1330
1331	reglist = et8ek8_reglist_find_type(&meta_reglist, ET8EK8_REGLIST_MODE);
1332	format = __et8ek8_get_pad_format(sensor, fh->state, 0,
1333					 V4L2_SUBDEV_FORMAT_TRY);
1334	et8ek8_reglist_to_mbus(reglist, format);
1335
1336	return et8ek8_set_power(sd, true);
1337}
1338
1339static int et8ek8_close(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
1340{
1341	return et8ek8_set_power(sd, false);
1342}
1343
1344static const struct v4l2_subdev_video_ops et8ek8_video_ops = {
1345	.s_stream = et8ek8_s_stream,
1346};
1347
1348static const struct v4l2_subdev_core_ops et8ek8_core_ops = {
1349	.s_power = et8ek8_set_power,
1350};
1351
1352static const struct v4l2_subdev_pad_ops et8ek8_pad_ops = {
1353	.enum_mbus_code = et8ek8_enum_mbus_code,
1354	.enum_frame_size = et8ek8_enum_frame_size,
1355	.enum_frame_interval = et8ek8_enum_frame_ival,
1356	.get_fmt = et8ek8_get_pad_format,
1357	.set_fmt = et8ek8_set_pad_format,
1358	.get_frame_interval = et8ek8_get_frame_interval,
1359	.set_frame_interval = et8ek8_set_frame_interval,
1360};
1361
1362static const struct v4l2_subdev_ops et8ek8_ops = {
1363	.core = &et8ek8_core_ops,
1364	.video = &et8ek8_video_ops,
1365	.pad = &et8ek8_pad_ops,
1366};
1367
1368static const struct v4l2_subdev_internal_ops et8ek8_internal_ops = {
1369	.registered = et8ek8_registered,
1370	.open = et8ek8_open,
1371	.close = et8ek8_close,
1372};
1373
1374/* --------------------------------------------------------------------------
1375 * I2C driver
1376 */
1377static int __maybe_unused et8ek8_suspend(struct device *dev)
1378{
1379	struct v4l2_subdev *subdev = dev_get_drvdata(dev);
1380	struct et8ek8_sensor *sensor = to_et8ek8_sensor(subdev);
1381
1382	if (!sensor->power_count)
1383		return 0;
1384
1385	return __et8ek8_set_power(sensor, false);
1386}
1387
1388static int __maybe_unused et8ek8_resume(struct device *dev)
1389{
1390	struct v4l2_subdev *subdev = dev_get_drvdata(dev);
1391	struct et8ek8_sensor *sensor = to_et8ek8_sensor(subdev);
1392
1393	if (!sensor->power_count)
1394		return 0;
1395
1396	return __et8ek8_set_power(sensor, true);
1397}
1398
1399static int et8ek8_probe(struct i2c_client *client)
1400{
1401	struct et8ek8_sensor *sensor;
1402	struct device *dev = &client->dev;
1403	int ret;
1404
1405	sensor = devm_kzalloc(&client->dev, sizeof(*sensor), GFP_KERNEL);
1406	if (!sensor)
1407		return -ENOMEM;
1408
1409	sensor->reset = devm_gpiod_get(dev, "reset", GPIOD_OUT_LOW);
1410	if (IS_ERR(sensor->reset)) {
1411		dev_dbg(&client->dev, "could not request reset gpio\n");
1412		return PTR_ERR(sensor->reset);
1413	}
1414
1415	sensor->vana = devm_regulator_get(dev, "vana");
1416	if (IS_ERR(sensor->vana)) {
1417		dev_err(&client->dev, "could not get regulator for vana\n");
1418		return PTR_ERR(sensor->vana);
1419	}
1420
1421	sensor->ext_clk = devm_v4l2_sensor_clk_get_legacy(dev, NULL, true,
1422							  9600000);
1423	if (IS_ERR(sensor->ext_clk))
1424		return dev_err_probe(&client->dev, PTR_ERR(sensor->ext_clk),
1425				     "could not get clock\n");
1426
1427	sensor->xclk_freq = clk_get_rate(sensor->ext_clk);
1428
1429	mutex_init(&sensor->power_lock);
1430
1431	v4l2_i2c_subdev_init(&sensor->subdev, client, &et8ek8_ops);
1432	sensor->subdev.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
1433	sensor->subdev.internal_ops = &et8ek8_internal_ops;
1434
1435	sensor->subdev.entity.function = MEDIA_ENT_F_CAM_SENSOR;
1436	sensor->pad.flags = MEDIA_PAD_FL_SOURCE;
1437	ret = media_entity_pads_init(&sensor->subdev.entity, 1, &sensor->pad);
1438	if (ret < 0) {
1439		dev_err(&client->dev, "media entity init failed!\n");
1440		goto err_mutex;
1441	}
1442
1443	ret = v4l2_async_register_subdev_sensor(&sensor->subdev);
1444	if (ret < 0)
1445		goto err_entity;
1446
1447	dev_dbg(dev, "initialized!\n");
1448
1449	return 0;
1450
1451err_entity:
1452	media_entity_cleanup(&sensor->subdev.entity);
1453err_mutex:
1454	mutex_destroy(&sensor->power_lock);
1455	return ret;
1456}
1457
1458static void et8ek8_remove(struct i2c_client *client)
1459{
1460	struct v4l2_subdev *subdev = i2c_get_clientdata(client);
1461	struct et8ek8_sensor *sensor = to_et8ek8_sensor(subdev);
1462
1463	if (sensor->power_count) {
1464		WARN_ON(1);
1465		et8ek8_power_off(sensor);
1466		sensor->power_count = 0;
1467	}
1468
1469	v4l2_device_unregister_subdev(&sensor->subdev);
1470	device_remove_file(&client->dev, &dev_attr_priv_mem);
1471	v4l2_ctrl_handler_free(&sensor->ctrl_handler);
1472	v4l2_async_unregister_subdev(&sensor->subdev);
1473	media_entity_cleanup(&sensor->subdev.entity);
1474	mutex_destroy(&sensor->power_lock);
1475}
1476
1477static const struct of_device_id et8ek8_of_table[] = {
1478	{ .compatible = "toshiba,et8ek8" },
1479	{ },
1480};
1481MODULE_DEVICE_TABLE(of, et8ek8_of_table);
1482
1483static const struct i2c_device_id et8ek8_id_table[] = {
1484	{ ET8EK8_NAME },
1485	{ }
1486};
1487MODULE_DEVICE_TABLE(i2c, et8ek8_id_table);
1488
1489static const struct dev_pm_ops et8ek8_pm_ops = {
1490	SET_SYSTEM_SLEEP_PM_OPS(et8ek8_suspend, et8ek8_resume)
1491};
1492
1493static struct i2c_driver et8ek8_i2c_driver = {
1494	.driver		= {
1495		.name	= ET8EK8_NAME,
1496		.pm	= &et8ek8_pm_ops,
1497		.of_match_table	= et8ek8_of_table,
1498	},
1499	.probe		= et8ek8_probe,
1500	.remove		= et8ek8_remove,
1501	.id_table	= et8ek8_id_table,
1502};
1503
1504module_i2c_driver(et8ek8_i2c_driver);
1505
1506MODULE_AUTHOR("Sakari Ailus <sakari.ailus@iki.fi>, Pavel Machek <pavel@ucw.cz");
1507MODULE_DESCRIPTION("Toshiba ET8EK8 camera sensor driver");
1508MODULE_LICENSE("GPL");
~/ Read-only file preview.