1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Support for mt9m114 Camera Sensor.
4  *
5  * Copyright (c) 2010 Intel Corporation. All Rights Reserved.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License version
9  * 2 as published by the Free Software Foundation.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  *
17  */
18 
19 #include <linux/module.h>
20 #include <linux/types.h>
21 #include <linux/kernel.h>
22 #include <linux/mm.h>
23 #include <linux/string.h>
24 #include <linux/errno.h>
25 #include <linux/init.h>
26 #include <linux/kmod.h>
27 #include <linux/device.h>
28 #include <linux/fs.h>
29 #include <linux/slab.h>
30 #include <linux/delay.h>
31 #include <linux/i2c.h>
32 #include <linux/acpi.h>
33 #include "../include/linux/atomisp_gmin_platform.h"
34 #include <media/v4l2-device.h>
35 
36 #include "mt9m114.h"
37 
38 #define to_mt9m114_sensor(sd) container_of(sd, struct mt9m114_device, sd)
39 
40 /*
41  * TODO: use debug parameter to actually define when debug messages should
42  * be printed.
43  */
44 static int debug;
45 static int aaalock;
46 module_param(debug, int, 0644);
47 MODULE_PARM_DESC(debug, "Debug level (0-1)");
48 
49 static int mt9m114_t_vflip(struct v4l2_subdev *sd, int value);
50 static int mt9m114_t_hflip(struct v4l2_subdev *sd, int value);
51 static int mt9m114_wait_state(struct i2c_client *client, int timeout);
52 
53 static int
54 mt9m114_read_reg(struct i2c_client *client, u16 data_length, u32 reg, u32 *val)
55 {
56 	int err;
57 	struct i2c_msg msg[2];
58 	unsigned char data[4];
59 
60 	if (!client->adapter) {
61 		v4l2_err(client, "%s error, no client->adapter\n", __func__);
62 		return -ENODEV;
63 	}
64 
65 	if (data_length != MISENSOR_8BIT && data_length != MISENSOR_16BIT
66 	    && data_length != MISENSOR_32BIT) {
67 		v4l2_err(client, "%s error, invalid data length\n", __func__);
68 		return -EINVAL;
69 	}
70 
71 	msg[0].addr = client->addr;
72 	msg[0].flags = 0;
73 	msg[0].len = MSG_LEN_OFFSET;
74 	msg[0].buf = data;
75 
76 	/* high byte goes out first */
77 	data[0] = (u16)(reg >> 8);
78 	data[1] = (u16)(reg & 0xff);
79 
80 	msg[1].addr = client->addr;
81 	msg[1].len = data_length;
82 	msg[1].flags = I2C_M_RD;
83 	msg[1].buf = data;
84 
85 	err = i2c_transfer(client->adapter, msg, 2);
86 
87 	if (err >= 0) {
88 		*val = 0;
89 		/* high byte comes first */
90 		if (data_length == MISENSOR_8BIT)
91 			*val = data[0];
92 		else if (data_length == MISENSOR_16BIT)
93 			*val = data[1] + (data[0] << 8);
94 		else
95 			*val = data[3] + (data[2] << 8) +
96 			       (data[1] << 16) + (data[0] << 24);
97 
98 		return 0;
99 	}
100 
101 	dev_err(&client->dev, "read from offset 0x%x error %d", reg, err);
102 	return err;
103 }
104 
105 static int
106 mt9m114_write_reg(struct i2c_client *client, u16 data_length, u16 reg, u32 val)
107 {
108 	int num_msg;
109 	struct i2c_msg msg;
110 	unsigned char data[6] = {0};
111 	__be16 *wreg;
112 	int retry = 0;
113 
114 	if (!client->adapter) {
115 		v4l2_err(client, "%s error, no client->adapter\n", __func__);
116 		return -ENODEV;
117 	}
118 
119 	if (data_length != MISENSOR_8BIT && data_length != MISENSOR_16BIT
120 	    && data_length != MISENSOR_32BIT) {
121 		v4l2_err(client, "%s error, invalid data_length\n", __func__);
122 		return -EINVAL;
123 	}
124 
125 	memset(&msg, 0, sizeof(msg));
126 
127 again:
128 	msg.addr = client->addr;
129 	msg.flags = 0;
130 	msg.len = 2 + data_length;
131 	msg.buf = data;
132 
133 	/* high byte goes out first */
134 	wreg = (void *)data;
135 	*wreg = cpu_to_be16(reg);
136 
137 	if (data_length == MISENSOR_8BIT) {
138 		data[2] = (u8)(val);
139 	} else if (data_length == MISENSOR_16BIT) {
140 		u16 *wdata = (void *)&data[2];
141 
142 		*wdata = be16_to_cpu(*(__be16 *)&data[2]);
143 	} else {
144 		/* MISENSOR_32BIT */
145 		u32 *wdata = (void *)&data[2];
146 
147 		*wdata = be32_to_cpu(*(__be32 *)&data[2]);
148 	}
149 
150 	num_msg = i2c_transfer(client->adapter, &msg, 1);
151 
152 	/*
153 	 * HACK: Need some delay here for Rev 2 sensors otherwise some
154 	 * registers do not seem to load correctly.
155 	 */
156 	mdelay(1);
157 
158 	if (num_msg >= 0)
159 		return 0;
160 
161 	dev_err(&client->dev, "write error: wrote 0x%x to offset 0x%x error %d",
162 		val, reg, num_msg);
163 	if (retry <= I2C_RETRY_COUNT) {
164 		dev_dbg(&client->dev, "retrying... %d", retry);
165 		retry++;
166 		msleep(20);
167 		goto again;
168 	}
169 
170 	return num_msg;
171 }
172 
173 /**
174  * misensor_rmw_reg - Read/Modify/Write a value to a register in the sensor
175  * device
176  * @client: i2c driver client structure
177  * @data_length: 8/16/32-bits length
178  * @reg: register address
179  * @mask: masked out bits
180  * @set: bits set
181  *
182  * Read/modify/write a value to a register in the  sensor device.
183  * Returns zero if successful, or non-zero otherwise.
184  */
185 static int
186 misensor_rmw_reg(struct i2c_client *client, u16 data_length, u16 reg,
187 		 u32 mask, u32 set)
188 {
189 	int err;
190 	u32 val;
191 
192 	/* Exit when no mask */
193 	if (mask == 0)
194 		return 0;
195 
196 	/* @mask must not exceed data length */
197 	switch (data_length) {
198 	case MISENSOR_8BIT:
199 		if (mask & ~0xff)
200 			return -EINVAL;
201 		break;
202 	case MISENSOR_16BIT:
203 		if (mask & ~0xffff)
204 			return -EINVAL;
205 		break;
206 	case MISENSOR_32BIT:
207 		break;
208 	default:
209 		/* Wrong @data_length */
210 		return -EINVAL;
211 	}
212 
213 	err = mt9m114_read_reg(client, data_length, reg, &val);
214 	if (err) {
215 		v4l2_err(client, "%s error exit, read failed\n", __func__);
216 		return -EINVAL;
217 	}
218 
219 	val &= ~mask;
220 
221 	/*
222 	 * Perform the OR function if the @set exists.
223 	 * Shift @set value to target bit location. @set should set only
224 	 * bits included in @mask.
225 	 *
226 	 * REVISIT: This function expects @set to be non-shifted. Its shift
227 	 * value is then defined to be equal to mask's LSB position.
228 	 * How about to inform values in their right offset position and avoid
229 	 * this unneeded shift operation?
230 	 */
231 	set <<= ffs(mask) - 1;
232 	val |= set & mask;
233 
234 	err = mt9m114_write_reg(client, data_length, reg, val);
235 	if (err) {
236 		v4l2_err(client, "%s error exit, write failed\n", __func__);
237 		return -EINVAL;
238 	}
239 
240 	return 0;
241 }
242 
243 static int __mt9m114_flush_reg_array(struct i2c_client *client,
244 				     struct mt9m114_write_ctrl *ctrl)
245 {
246 	struct i2c_msg msg;
247 	const int num_msg = 1;
248 	int ret;
249 	int retry = 0;
250 	__be16 *data16 = (void *)&ctrl->buffer.addr;
251 
252 	if (ctrl->index == 0)
253 		return 0;
254 
255 again:
256 	msg.addr = client->addr;
257 	msg.flags = 0;
258 	msg.len = 2 + ctrl->index;
259 	*data16 = cpu_to_be16(ctrl->buffer.addr);
260 	msg.buf = (u8 *)&ctrl->buffer;
261 
262 	ret = i2c_transfer(client->adapter, &msg, num_msg);
263 	if (ret != num_msg) {
264 		if (++retry <= I2C_RETRY_COUNT) {
265 			dev_dbg(&client->dev, "retrying... %d\n", retry);
266 			msleep(20);
267 			goto again;
268 		}
269 		dev_err(&client->dev, "%s: i2c transfer error\n", __func__);
270 		return -EIO;
271 	}
272 
273 	ctrl->index = 0;
274 
275 	/*
276 	 * REVISIT: Previously we had a delay after writing data to sensor.
277 	 * But it was removed as our tests have shown it is not necessary
278 	 * anymore.
279 	 */
280 
281 	return 0;
282 }
283 
284 static int __mt9m114_buf_reg_array(struct i2c_client *client,
285 				   struct mt9m114_write_ctrl *ctrl,
286 				   const struct misensor_reg *next)
287 {
288 	__be16 *data16;
289 	__be32 *data32;
290 	int err;
291 
292 	/* Insufficient buffer? Let's flush and get more free space. */
293 	if (ctrl->index + next->length >= MT9M114_MAX_WRITE_BUF_SIZE) {
294 		err = __mt9m114_flush_reg_array(client, ctrl);
295 		if (err)
296 			return err;
297 	}
298 
299 	switch (next->length) {
300 	case MISENSOR_8BIT:
301 		ctrl->buffer.data[ctrl->index] = (u8)next->val;
302 		break;
303 	case MISENSOR_16BIT:
304 		data16 = (__be16 *)&ctrl->buffer.data[ctrl->index];
305 		*data16 = cpu_to_be16((u16)next->val);
306 		break;
307 	case MISENSOR_32BIT:
308 		data32 = (__be32 *)&ctrl->buffer.data[ctrl->index];
309 		*data32 = cpu_to_be32(next->val);
310 		break;
311 	default:
312 		return -EINVAL;
313 	}
314 
315 	/* When first item is added, we need to store its starting address */
316 	if (ctrl->index == 0)
317 		ctrl->buffer.addr = next->reg;
318 
319 	ctrl->index += next->length;
320 
321 	return 0;
322 }
323 
324 static int
325 __mt9m114_write_reg_is_consecutive(struct i2c_client *client,
326 				   struct mt9m114_write_ctrl *ctrl,
327 				   const struct misensor_reg *next)
328 {
329 	if (ctrl->index == 0)
330 		return 1;
331 
332 	return ctrl->buffer.addr + ctrl->index == next->reg;
333 }
334 
335 /*
336  * mt9m114_write_reg_array - Initializes a list of mt9m114 registers
337  * @client: i2c driver client structure
338  * @reglist: list of registers to be written
339  * @poll: completion polling requirement
340  * This function initializes a list of registers. When consecutive addresses
341  * are found in a row on the list, this function creates a buffer and sends
342  * consecutive data in a single i2c_transfer().
343  *
344  * __mt9m114_flush_reg_array, __mt9m114_buf_reg_array() and
345  * __mt9m114_write_reg_is_consecutive() are internal functions to
346  * mt9m114_write_reg_array() and should be not used anywhere else.
347  *
348  */
349 static int mt9m114_write_reg_array(struct i2c_client *client,
350 				   const struct misensor_reg *reglist,
351 				   int poll)
352 {
353 	const struct misensor_reg *next = reglist;
354 	struct mt9m114_write_ctrl ctrl;
355 	int err;
356 
357 	if (poll == PRE_POLLING) {
358 		err = mt9m114_wait_state(client, MT9M114_WAIT_STAT_TIMEOUT);
359 		if (err)
360 			return err;
361 	}
362 
363 	ctrl.index = 0;
364 	for (; next->length != MISENSOR_TOK_TERM; next++) {
365 		switch (next->length & MISENSOR_TOK_MASK) {
366 		case MISENSOR_TOK_DELAY:
367 			err = __mt9m114_flush_reg_array(client, &ctrl);
368 			if (err)
369 				return err;
370 			msleep(next->val);
371 			break;
372 		case MISENSOR_TOK_RMW:
373 			err = __mt9m114_flush_reg_array(client, &ctrl);
374 			err |= misensor_rmw_reg(client,
375 						next->length &
376 						~MISENSOR_TOK_RMW,
377 						next->reg, next->val,
378 						next->val2);
379 			if (err) {
380 				dev_err(&client->dev, "%s read err. aborted\n",
381 					__func__);
382 				return -EINVAL;
383 			}
384 			break;
385 		default:
386 			/*
387 			 * If next address is not consecutive, data needs to be
388 			 * flushed before proceed.
389 			 */
390 			if (!__mt9m114_write_reg_is_consecutive(client, &ctrl,
391 								next)) {
392 				err = __mt9m114_flush_reg_array(client, &ctrl);
393 				if (err)
394 					return err;
395 			}
396 			err = __mt9m114_buf_reg_array(client, &ctrl, next);
397 			if (err) {
398 				v4l2_err(client, "%s: write error, aborted\n",
399 					 __func__);
400 				return err;
401 			}
402 			break;
403 		}
404 	}
405 
406 	err = __mt9m114_flush_reg_array(client, &ctrl);
407 	if (err)
408 		return err;
409 
410 	if (poll == POST_POLLING)
411 		return mt9m114_wait_state(client, MT9M114_WAIT_STAT_TIMEOUT);
412 
413 	return 0;
414 }
415 
416 static int mt9m114_wait_state(struct i2c_client *client, int timeout)
417 {
418 	int ret;
419 	unsigned int val;
420 
421 	while (timeout-- > 0) {
422 		ret = mt9m114_read_reg(client, MISENSOR_16BIT, 0x0080, &val);
423 		if (ret)
424 			return ret;
425 		if ((val & 0x2) == 0)
426 			return 0;
427 		msleep(20);
428 	}
429 
430 	return -EINVAL;
431 }
432 
433 static int mt9m114_set_suspend(struct v4l2_subdev *sd)
434 {
435 	struct i2c_client *client = v4l2_get_subdevdata(sd);
436 
437 	return mt9m114_write_reg_array(client,
438 				       mt9m114_standby_reg, POST_POLLING);
439 }
440 
441 static int mt9m114_init_common(struct v4l2_subdev *sd)
442 {
443 	struct i2c_client *client = v4l2_get_subdevdata(sd);
444 
445 	return mt9m114_write_reg_array(client, mt9m114_common, PRE_POLLING);
446 }
447 
448 static int power_ctrl(struct v4l2_subdev *sd, bool flag)
449 {
450 	int ret;
451 	struct mt9m114_device *dev = to_mt9m114_sensor(sd);
452 
453 	if (!dev || !dev->platform_data)
454 		return -ENODEV;
455 
456 	if (flag) {
457 		ret = dev->platform_data->v2p8_ctrl(sd, 1);
458 		if (ret == 0) {
459 			ret = dev->platform_data->v1p8_ctrl(sd, 1);
460 			if (ret)
461 				ret = dev->platform_data->v2p8_ctrl(sd, 0);
462 		}
463 	} else {
464 		ret = dev->platform_data->v2p8_ctrl(sd, 0);
465 		ret = dev->platform_data->v1p8_ctrl(sd, 0);
466 	}
467 	return ret;
468 }
469 
470 static int gpio_ctrl(struct v4l2_subdev *sd, bool flag)
471 {
472 	int ret;
473 	struct mt9m114_device *dev = to_mt9m114_sensor(sd);
474 
475 	if (!dev || !dev->platform_data)
476 		return -ENODEV;
477 
478 	/*
479 	 * Note: current modules wire only one GPIO signal (RESET#),
480 	 * but the schematic wires up two to the connector.  BIOS
481 	 * versions have been unfortunately inconsistent with which
482 	 * ACPI index RESET# is on, so hit both
483 	 */
484 
485 	if (flag) {
486 		ret = dev->platform_data->gpio0_ctrl(sd, 0);
487 		ret = dev->platform_data->gpio1_ctrl(sd, 0);
488 		msleep(60);
489 		ret |= dev->platform_data->gpio0_ctrl(sd, 1);
490 		ret |= dev->platform_data->gpio1_ctrl(sd, 1);
491 	} else {
492 		ret = dev->platform_data->gpio0_ctrl(sd, 0);
493 		ret = dev->platform_data->gpio1_ctrl(sd, 0);
494 	}
495 	return ret;
496 }
497 
498 static int power_up(struct v4l2_subdev *sd)
499 {
500 	struct mt9m114_device *dev = to_mt9m114_sensor(sd);
501 	struct i2c_client *client = v4l2_get_subdevdata(sd);
502 	int ret;
503 
504 	if (!dev->platform_data) {
505 		dev_err(&client->dev, "no camera_sensor_platform_data");
506 		return -ENODEV;
507 	}
508 
509 	/* power control */
510 	ret = power_ctrl(sd, 1);
511 	if (ret)
512 		goto fail_power;
513 
514 	/* flis clock control */
515 	ret = dev->platform_data->flisclk_ctrl(sd, 1);
516 	if (ret)
517 		goto fail_clk;
518 
519 	/* gpio ctrl */
520 	ret = gpio_ctrl(sd, 1);
521 	if (ret)
522 		dev_err(&client->dev, "gpio failed 1\n");
523 	/*
524 	 * according to DS, 44ms is needed between power up and first i2c
525 	 * commend
526 	 */
527 	msleep(50);
528 
529 	return 0;
530 
531 fail_clk:
532 	dev->platform_data->flisclk_ctrl(sd, 0);
533 fail_power:
534 	power_ctrl(sd, 0);
535 	dev_err(&client->dev, "sensor power-up failed\n");
536 
537 	return ret;
538 }
539 
540 static int power_down(struct v4l2_subdev *sd)
541 {
542 	struct mt9m114_device *dev = to_mt9m114_sensor(sd);
543 	struct i2c_client *client = v4l2_get_subdevdata(sd);
544 	int ret;
545 
546 	if (!dev->platform_data) {
547 		dev_err(&client->dev, "no camera_sensor_platform_data");
548 		return -ENODEV;
549 	}
550 
551 	ret = dev->platform_data->flisclk_ctrl(sd, 0);
552 	if (ret)
553 		dev_err(&client->dev, "flisclk failed\n");
554 
555 	/* gpio ctrl */
556 	ret = gpio_ctrl(sd, 0);
557 	if (ret)
558 		dev_err(&client->dev, "gpio failed 1\n");
559 
560 	/* power control */
561 	ret = power_ctrl(sd, 0);
562 	if (ret)
563 		dev_err(&client->dev, "vprog failed.\n");
564 
565 	/* according to DS, 20ms is needed after power down */
566 	msleep(20);
567 
568 	return ret;
569 }
570 
571 static int mt9m114_s_power(struct v4l2_subdev *sd, int power)
572 {
573 	if (power == 0)
574 		return power_down(sd);
575 
576 	if (power_up(sd))
577 		return -EINVAL;
578 
579 	return mt9m114_init_common(sd);
580 }
581 
582 static int mt9m114_res2size(struct v4l2_subdev *sd, int *h_size, int *v_size)
583 {
584 	struct mt9m114_device *dev = to_mt9m114_sensor(sd);
585 	unsigned short hsize;
586 	unsigned short vsize;
587 
588 	switch (dev->res) {
589 	case MT9M114_RES_736P:
590 		hsize = MT9M114_RES_736P_SIZE_H;
591 		vsize = MT9M114_RES_736P_SIZE_V;
592 		break;
593 	case MT9M114_RES_864P:
594 		hsize = MT9M114_RES_864P_SIZE_H;
595 		vsize = MT9M114_RES_864P_SIZE_V;
596 		break;
597 	case MT9M114_RES_960P:
598 		hsize = MT9M114_RES_960P_SIZE_H;
599 		vsize = MT9M114_RES_960P_SIZE_V;
600 		break;
601 	default:
602 		v4l2_err(sd, "%s: Resolution 0x%08x unknown\n", __func__,
603 			 dev->res);
604 		return -EINVAL;
605 	}
606 
607 	if (h_size)
608 		*h_size = hsize;
609 	if (v_size)
610 		*v_size = vsize;
611 
612 	return 0;
613 }
614 
615 static int mt9m114_get_intg_factor(struct i2c_client *client,
616 				   struct camera_mipi_info *info,
617 				   const struct mt9m114_res_struct *res)
618 {
619 	struct atomisp_sensor_mode_data *buf = &info->data;
620 	u32 reg_val;
621 	int ret;
622 
623 	if (!info)
624 		return -EINVAL;
625 
626 	ret =  mt9m114_read_reg(client, MISENSOR_32BIT,
627 				REG_PIXEL_CLK, &reg_val);
628 	if (ret)
629 		return ret;
630 	buf->vt_pix_clk_freq_mhz = reg_val;
631 
632 	/* get integration time */
633 	buf->coarse_integration_time_min = MT9M114_COARSE_INTG_TIME_MIN;
634 	buf->coarse_integration_time_max_margin =
635 	    MT9M114_COARSE_INTG_TIME_MAX_MARGIN;
636 
637 	buf->fine_integration_time_min = MT9M114_FINE_INTG_TIME_MIN;
638 	buf->fine_integration_time_max_margin =
639 	    MT9M114_FINE_INTG_TIME_MAX_MARGIN;
640 
641 	buf->fine_integration_time_def = MT9M114_FINE_INTG_TIME_MIN;
642 
643 	buf->frame_length_lines = res->lines_per_frame;
644 	buf->line_length_pck = res->pixels_per_line;
645 	buf->read_mode = res->bin_mode;
646 
647 	/* get the cropping and output resolution to ISP for this mode. */
648 	ret =  mt9m114_read_reg(client, MISENSOR_16BIT,
649 				REG_H_START, &reg_val);
650 	if (ret)
651 		return ret;
652 	buf->crop_horizontal_start = reg_val;
653 
654 	ret =  mt9m114_read_reg(client, MISENSOR_16BIT,
655 				REG_V_START, &reg_val);
656 	if (ret)
657 		return ret;
658 	buf->crop_vertical_start = reg_val;
659 
660 	ret = mt9m114_read_reg(client, MISENSOR_16BIT,
661 			       REG_H_END, &reg_val);
662 	if (ret)
663 		return ret;
664 	buf->crop_horizontal_end = reg_val;
665 
666 	ret = mt9m114_read_reg(client, MISENSOR_16BIT,
667 			       REG_V_END, &reg_val);
668 	if (ret)
669 		return ret;
670 	buf->crop_vertical_end = reg_val;
671 
672 	ret = mt9m114_read_reg(client, MISENSOR_16BIT,
673 			       REG_WIDTH, &reg_val);
674 	if (ret)
675 		return ret;
676 	buf->output_width = reg_val;
677 
678 	ret = mt9m114_read_reg(client, MISENSOR_16BIT,
679 			       REG_HEIGHT, &reg_val);
680 	if (ret)
681 		return ret;
682 	buf->output_height = reg_val;
683 
684 	ret = mt9m114_read_reg(client, MISENSOR_16BIT,
685 			       REG_TIMING_HTS, &reg_val);
686 	if (ret)
687 		return ret;
688 	buf->line_length_pck = reg_val;
689 
690 	ret = mt9m114_read_reg(client, MISENSOR_16BIT,
691 			       REG_TIMING_VTS, &reg_val);
692 	if (ret)
693 		return ret;
694 	buf->frame_length_lines = reg_val;
695 
696 	buf->binning_factor_x = res->bin_factor_x ?
697 				res->bin_factor_x : 1;
698 	buf->binning_factor_y = res->bin_factor_y ?
699 				res->bin_factor_y : 1;
700 	return 0;
701 }
702 
703 static int mt9m114_get_fmt(struct v4l2_subdev *sd,
704 			   struct v4l2_subdev_state *sd_state,
705 			   struct v4l2_subdev_format *format)
706 {
707 	struct v4l2_mbus_framefmt *fmt = &format->format;
708 	int width, height;
709 	int ret;
710 
711 	if (format->pad)
712 		return -EINVAL;
713 	fmt->code = MEDIA_BUS_FMT_SGRBG10_1X10;
714 
715 	ret = mt9m114_res2size(sd, &width, &height);
716 	if (ret)
717 		return ret;
718 	fmt->width = width;
719 	fmt->height = height;
720 
721 	return 0;
722 }
723 
724 static int mt9m114_set_fmt(struct v4l2_subdev *sd,
725 			   struct v4l2_subdev_state *sd_state,
726 			   struct v4l2_subdev_format *format)
727 {
728 	struct v4l2_mbus_framefmt *fmt = &format->format;
729 	struct i2c_client *c = v4l2_get_subdevdata(sd);
730 	struct mt9m114_device *dev = to_mt9m114_sensor(sd);
731 	struct mt9m114_res_struct *res;
732 	u32 width = fmt->width;
733 	u32 height = fmt->height;
734 	struct camera_mipi_info *mt9m114_info = NULL;
735 
736 	int ret;
737 
738 	if (format->pad)
739 		return -EINVAL;
740 	dev->streamon = 0;
741 	dev->first_exp = MT9M114_DEFAULT_FIRST_EXP;
742 
743 	mt9m114_info = v4l2_get_subdev_hostdata(sd);
744 	if (!mt9m114_info)
745 		return -EINVAL;
746 
747 	res = v4l2_find_nearest_size(mt9m114_res,
748 				     ARRAY_SIZE(mt9m114_res), width,
749 				     height, fmt->width, fmt->height);
750 	if (!res)
751 		res = &mt9m114_res[N_RES - 1];
752 
753 	fmt->width = res->width;
754 	fmt->height = res->height;
755 
756 	if (format->which == V4L2_SUBDEV_FORMAT_TRY) {
757 		sd_state->pads->try_fmt = *fmt;
758 		return 0;
759 	}
760 
761 	switch (res->res) {
762 	case MT9M114_RES_736P:
763 		ret = mt9m114_write_reg_array(c, mt9m114_736P_init, NO_POLLING);
764 		ret += misensor_rmw_reg(c, MISENSOR_16BIT, MISENSOR_READ_MODE,
765 					MISENSOR_R_MODE_MASK, MISENSOR_NORMAL_SET);
766 		break;
767 	case MT9M114_RES_864P:
768 		ret = mt9m114_write_reg_array(c, mt9m114_864P_init, NO_POLLING);
769 		ret += misensor_rmw_reg(c, MISENSOR_16BIT, MISENSOR_READ_MODE,
770 					MISENSOR_R_MODE_MASK, MISENSOR_NORMAL_SET);
771 		break;
772 	case MT9M114_RES_960P:
773 		ret = mt9m114_write_reg_array(c, mt9m114_976P_init, NO_POLLING);
774 		/* set sensor read_mode to Normal */
775 		ret += misensor_rmw_reg(c, MISENSOR_16BIT, MISENSOR_READ_MODE,
776 					MISENSOR_R_MODE_MASK, MISENSOR_NORMAL_SET);
777 		break;
778 	default:
779 		v4l2_err(sd, "set resolution: %d failed!\n", res->res);
780 		return -EINVAL;
781 	}
782 
783 	if (ret)
784 		return -EINVAL;
785 
786 	ret = mt9m114_write_reg_array(c, mt9m114_chgstat_reg, POST_POLLING);
787 	if (ret < 0)
788 		return ret;
789 
790 	if (mt9m114_set_suspend(sd))
791 		return -EINVAL;
792 
793 	if (dev->res != res->res) {
794 		int index;
795 
796 		/* Switch to different size */
797 		if (width <= 640) {
798 			dev->nctx = 0x00; /* Set for context A */
799 		} else {
800 			/*
801 			 * Context B is used for resolutions larger than 640x480
802 			 * Using YUV for Context B.
803 			 */
804 			dev->nctx = 0x01; /* set for context B */
805 		}
806 
807 		/*
808 		 * Marked current sensor res as being "used"
809 		 *
810 		 * REVISIT: We don't need to use an "used" field on each mode
811 		 * list entry to know which mode is selected. If this
812 		 * information is really necessary, how about to use a single
813 		 * variable on sensor dev struct?
814 		 */
815 		for (index = 0; index < N_RES; index++) {
816 			if ((width == mt9m114_res[index].width) &&
817 			    (height == mt9m114_res[index].height)) {
818 				mt9m114_res[index].used = true;
819 				continue;
820 			}
821 			mt9m114_res[index].used = false;
822 		}
823 	}
824 	ret = mt9m114_get_intg_factor(c, mt9m114_info,
825 				      &mt9m114_res[res->res]);
826 	if (ret) {
827 		dev_err(&c->dev, "failed to get integration_factor\n");
828 		return -EINVAL;
829 	}
830 	/*
831 	 * mt9m114 - we don't poll for context switch
832 	 * because it does not happen with streaming disabled.
833 	 */
834 	dev->res = res->res;
835 
836 	fmt->width = width;
837 	fmt->height = height;
838 	fmt->code = MEDIA_BUS_FMT_SGRBG10_1X10;
839 	return 0;
840 }
841 
842 /* TODO: Update to SOC functions, remove exposure and gain */
843 static int mt9m114_g_focal(struct v4l2_subdev *sd, s32 *val)
844 {
845 	*val = (MT9M114_FOCAL_LENGTH_NUM << 16) | MT9M114_FOCAL_LENGTH_DEM;
846 	return 0;
847 }
848 
849 static int mt9m114_g_fnumber(struct v4l2_subdev *sd, s32 *val)
850 {
851 	/* const f number for mt9m114 */
852 	*val = (MT9M114_F_NUMBER_DEFAULT_NUM << 16) | MT9M114_F_NUMBER_DEM;
853 	return 0;
854 }
855 
856 static int mt9m114_g_fnumber_range(struct v4l2_subdev *sd, s32 *val)
857 {
858 	*val = (MT9M114_F_NUMBER_DEFAULT_NUM << 24) |
859 	       (MT9M114_F_NUMBER_DEM << 16) |
860 	       (MT9M114_F_NUMBER_DEFAULT_NUM << 8) | MT9M114_F_NUMBER_DEM;
861 	return 0;
862 }
863 
864 /* Horizontal flip the image. */
865 static int mt9m114_g_hflip(struct v4l2_subdev *sd, s32 *val)
866 {
867 	struct i2c_client *c = v4l2_get_subdevdata(sd);
868 	int ret;
869 	u32 data;
870 
871 	ret = mt9m114_read_reg(c, MISENSOR_16BIT,
872 			       (u32)MISENSOR_READ_MODE, &data);
873 	if (ret)
874 		return ret;
875 	*val = !!(data & MISENSOR_HFLIP_MASK);
876 
877 	return 0;
878 }
879 
880 static int mt9m114_g_vflip(struct v4l2_subdev *sd, s32 *val)
881 {
882 	struct i2c_client *c = v4l2_get_subdevdata(sd);
883 	int ret;
884 	u32 data;
885 
886 	ret = mt9m114_read_reg(c, MISENSOR_16BIT,
887 			       (u32)MISENSOR_READ_MODE, &data);
888 	if (ret)
889 		return ret;
890 	*val = !!(data & MISENSOR_VFLIP_MASK);
891 
892 	return 0;
893 }
894 
895 static long mt9m114_s_exposure(struct v4l2_subdev *sd,
896 			       struct atomisp_exposure *exposure)
897 {
898 	struct i2c_client *client = v4l2_get_subdevdata(sd);
899 	struct mt9m114_device *dev = to_mt9m114_sensor(sd);
900 	int ret = 0;
901 	unsigned int coarse_integration = 0;
902 	unsigned int f_lines = 0;
903 	unsigned int frame_len_lines = 0; /* ExposureTime.FrameLengthLines; */
904 	unsigned int analog_gain, digital_gain;
905 	u32 analog_gain_to_write = 0;
906 
907 	dev_dbg(&client->dev, "%s(0x%X 0x%X 0x%X)\n", __func__,
908 		exposure->integration_time[0], exposure->gain[0],
909 		exposure->gain[1]);
910 
911 	coarse_integration = exposure->integration_time[0];
912 	/*
913 	 * fine_integration = ExposureTime.FineIntegrationTime;
914 	 * frame_len_lines = ExposureTime.FrameLengthLines;
915 	 */
916 	f_lines = mt9m114_res[dev->res].lines_per_frame;
917 	analog_gain = exposure->gain[0];
918 	digital_gain = exposure->gain[1];
919 	if (!dev->streamon) {
920 		/*Save the first exposure values while stream is off*/
921 		dev->first_exp = coarse_integration;
922 		dev->first_gain = analog_gain;
923 		dev->first_diggain = digital_gain;
924 	}
925 	/* digital_gain = 0x400 * (((u16) digital_gain) >> 8) +		*/
926 	/* ((unsigned int)(0x400 * (((u16) digital_gain) & 0xFF)) >>8); */
927 
928 	/* set frame length */
929 	if (f_lines < coarse_integration + 6)
930 		f_lines = coarse_integration + 6;
931 	if (f_lines < frame_len_lines)
932 		f_lines = frame_len_lines;
933 	ret = mt9m114_write_reg(client, MISENSOR_16BIT, 0x300A, f_lines);
934 	if (ret) {
935 		v4l2_err(client, "%s: fail to set f_lines\n", __func__);
936 		return -EINVAL;
937 	}
938 
939 	/* set coarse integration */
940 	/*
941 	 * 3A provide real exposure time.
942 	 * should not translate to any value here.
943 	 */
944 	ret = mt9m114_write_reg(client, MISENSOR_16BIT,
945 				REG_EXPO_COARSE, (u16)(coarse_integration));
946 	if (ret) {
947 		v4l2_err(client, "%s: fail to set exposure time\n", __func__);
948 		return -EINVAL;
949 	}
950 
951 	/*
952 	 * set analog/digital gain
953 	switch(analog_gain)
954 	{
955 	case 0:
956 	  analog_gain_to_write = 0x0;
957 	  break;
958 	case 1:
959 	  analog_gain_to_write = 0x20;
960 	  break;
961 	case 2:
962 	  analog_gain_to_write = 0x60;
963 	  break;
964 	case 4:
965 	  analog_gain_to_write = 0xA0;
966 	  break;
967 	case 8:
968 	  analog_gain_to_write = 0xE0;
969 	  break;
970 	default:
971 	  analog_gain_to_write = 0x20;
972 	  break;
973 	}
974 	*/
975 	if (digital_gain >= 16 || digital_gain <= 1)
976 		digital_gain = 1;
977 	/*
978 	 * analog_gain_to_write = (u16)((digital_gain << 12)
979 	 *				| analog_gain_to_write);
980 	 */
981 	analog_gain_to_write = (u16)((digital_gain << 12) | (u16)analog_gain);
982 	ret = mt9m114_write_reg(client, MISENSOR_16BIT,
983 				REG_GAIN, analog_gain_to_write);
984 	if (ret) {
985 		v4l2_err(client, "%s: fail to set analog_gain_to_write\n",
986 			 __func__);
987 		return -EINVAL;
988 	}
989 
990 	return ret;
991 }
992 
993 static long mt9m114_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg)
994 {
995 	switch (cmd) {
996 	case ATOMISP_IOC_S_EXPOSURE:
997 		return mt9m114_s_exposure(sd, arg);
998 	default:
999 		return -EINVAL;
1000 	}
1001 
1002 	return 0;
1003 }
1004 
1005 /*
1006  * This returns the exposure time being used. This should only be used
1007  * for filling in EXIF data, not for actual image processing.
1008  */
1009 static int mt9m114_g_exposure(struct v4l2_subdev *sd, s32 *value)
1010 {
1011 	struct i2c_client *client = v4l2_get_subdevdata(sd);
1012 	u32 coarse;
1013 	int ret;
1014 
1015 	/* the fine integration time is currently not calculated */
1016 	ret = mt9m114_read_reg(client, MISENSOR_16BIT,
1017 			       REG_EXPO_COARSE, &coarse);
1018 	if (ret)
1019 		return ret;
1020 
1021 	*value = coarse;
1022 	return 0;
1023 }
1024 
1025 /*
1026  * This function will return the sensor supported max exposure zone number.
1027  * the sensor which supports max exposure zone number is 1.
1028  */
1029 static int mt9m114_g_exposure_zone_num(struct v4l2_subdev *sd, s32 *val)
1030 {
1031 	*val = 1;
1032 
1033 	return 0;
1034 }
1035 
1036 /*
1037  * set exposure metering, average/center_weighted/spot/matrix.
1038  */
1039 static int mt9m114_s_exposure_metering(struct v4l2_subdev *sd, s32 val)
1040 {
1041 	struct i2c_client *client = v4l2_get_subdevdata(sd);
1042 	int ret;
1043 
1044 	switch (val) {
1045 	case V4L2_EXPOSURE_METERING_SPOT:
1046 		ret = mt9m114_write_reg_array(client, mt9m114_exp_average,
1047 					      NO_POLLING);
1048 		if (ret) {
1049 			dev_err(&client->dev, "write exp_average reg err.\n");
1050 			return ret;
1051 		}
1052 		break;
1053 	case V4L2_EXPOSURE_METERING_CENTER_WEIGHTED:
1054 	default:
1055 		ret = mt9m114_write_reg_array(client, mt9m114_exp_center,
1056 					      NO_POLLING);
1057 		if (ret) {
1058 			dev_err(&client->dev, "write exp_default reg err");
1059 			return ret;
1060 		}
1061 	}
1062 
1063 	return 0;
1064 }
1065 
1066 /*
1067  * This function is for touch exposure feature.
1068  */
1069 static int mt9m114_s_exposure_selection(struct v4l2_subdev *sd,
1070 					struct v4l2_subdev_state *sd_state,
1071 					struct v4l2_subdev_selection *sel)
1072 {
1073 	struct i2c_client *client = v4l2_get_subdevdata(sd);
1074 	struct misensor_reg exp_reg;
1075 	int width, height;
1076 	int grid_width, grid_height;
1077 	int grid_left, grid_top, grid_right, grid_bottom;
1078 	int win_left, win_top, win_right, win_bottom;
1079 	int i, j;
1080 	int ret;
1081 
1082 	if (sel->which != V4L2_SUBDEV_FORMAT_TRY &&
1083 	    sel->which != V4L2_SUBDEV_FORMAT_ACTIVE)
1084 		return -EINVAL;
1085 
1086 	grid_left = sel->r.left;
1087 	grid_top = sel->r.top;
1088 	grid_right = sel->r.left + sel->r.width - 1;
1089 	grid_bottom = sel->r.top + sel->r.height - 1;
1090 
1091 	ret = mt9m114_res2size(sd, &width, &height);
1092 	if (ret)
1093 		return ret;
1094 
1095 	grid_width = width / 5;
1096 	grid_height = height / 5;
1097 
1098 	if (grid_width && grid_height) {
1099 		win_left = grid_left / grid_width;
1100 		win_top = grid_top / grid_height;
1101 		win_right = grid_right / grid_width;
1102 		win_bottom = grid_bottom / grid_height;
1103 	} else {
1104 		dev_err(&client->dev, "Incorrect exp grid.\n");
1105 		return -EINVAL;
1106 	}
1107 
1108 	win_left   = clamp_t(int, win_left, 0, 4);
1109 	win_top    = clamp_t(int, win_top, 0, 4);
1110 	win_right  = clamp_t(int, win_right, 0, 4);
1111 	win_bottom = clamp_t(int, win_bottom, 0, 4);
1112 
1113 	ret = mt9m114_write_reg_array(client, mt9m114_exp_average, NO_POLLING);
1114 	if (ret) {
1115 		dev_err(&client->dev, "write exp_average reg err.\n");
1116 		return ret;
1117 	}
1118 
1119 	for (i = win_top; i <= win_bottom; i++) {
1120 		for (j = win_left; j <= win_right; j++) {
1121 			exp_reg = mt9m114_exp_win[i][j];
1122 
1123 			ret = mt9m114_write_reg(client, exp_reg.length,
1124 						exp_reg.reg, exp_reg.val);
1125 			if (ret) {
1126 				dev_err(&client->dev, "write exp_reg err.\n");
1127 				return ret;
1128 			}
1129 		}
1130 	}
1131 
1132 	return 0;
1133 }
1134 
1135 static int mt9m114_g_bin_factor_x(struct v4l2_subdev *sd, s32 *val)
1136 {
1137 	struct mt9m114_device *dev = to_mt9m114_sensor(sd);
1138 
1139 	*val = mt9m114_res[dev->res].bin_factor_x;
1140 
1141 	return 0;
1142 }
1143 
1144 static int mt9m114_g_bin_factor_y(struct v4l2_subdev *sd, s32 *val)
1145 {
1146 	struct mt9m114_device *dev = to_mt9m114_sensor(sd);
1147 
1148 	*val = mt9m114_res[dev->res].bin_factor_y;
1149 
1150 	return 0;
1151 }
1152 
1153 static int mt9m114_s_ev(struct v4l2_subdev *sd, s32 val)
1154 {
1155 	struct i2c_client *c = v4l2_get_subdevdata(sd);
1156 	s32 luma = 0x37;
1157 	int err;
1158 
1159 	/*
1160 	 * EV value only support -2 to 2
1161 	 * 0: 0x37, 1:0x47, 2:0x57, -1:0x27, -2:0x17
1162 	 */
1163 	if (val < -2 || val > 2)
1164 		return -EINVAL;
1165 	luma += 0x10 * val;
1166 	dev_dbg(&c->dev, "%s val:%d luma:0x%x\n", __func__, val, luma);
1167 	err = mt9m114_write_reg(c, MISENSOR_16BIT, 0x098E, 0xC87A);
1168 	if (err) {
1169 		dev_err(&c->dev, "%s logic addr access error\n", __func__);
1170 		return err;
1171 	}
1172 	err = mt9m114_write_reg(c, MISENSOR_8BIT, 0xC87A, (u32)luma);
1173 	if (err) {
1174 		dev_err(&c->dev, "%s write target_average_luma failed\n",
1175 			__func__);
1176 		return err;
1177 	}
1178 	udelay(10);
1179 
1180 	return 0;
1181 }
1182 
1183 static int mt9m114_g_ev(struct v4l2_subdev *sd, s32 *val)
1184 {
1185 	struct i2c_client *c = v4l2_get_subdevdata(sd);
1186 	int err;
1187 	u32 luma;
1188 
1189 	err = mt9m114_write_reg(c, MISENSOR_16BIT, 0x098E, 0xC87A);
1190 	if (err) {
1191 		dev_err(&c->dev, "%s logic addr access error\n", __func__);
1192 		return err;
1193 	}
1194 	err = mt9m114_read_reg(c, MISENSOR_8BIT, 0xC87A, &luma);
1195 	if (err) {
1196 		dev_err(&c->dev, "%s read target_average_luma failed\n",
1197 			__func__);
1198 		return err;
1199 	}
1200 	luma -= 0x17;
1201 	luma /= 0x10;
1202 	*val = (s32)luma - 2;
1203 	dev_dbg(&c->dev, "%s val:%d\n", __func__, *val);
1204 
1205 	return 0;
1206 }
1207 
1208 /*
1209  * Fake interface
1210  * mt9m114 now can not support 3a_lock
1211  */
1212 static int mt9m114_s_3a_lock(struct v4l2_subdev *sd, s32 val)
1213 {
1214 	aaalock = val;
1215 	return 0;
1216 }
1217 
1218 static int mt9m114_g_3a_lock(struct v4l2_subdev *sd, s32 *val)
1219 {
1220 	if (aaalock)
1221 		return V4L2_LOCK_EXPOSURE | V4L2_LOCK_WHITE_BALANCE
1222 		       | V4L2_LOCK_FOCUS;
1223 	return 0;
1224 }
1225 
1226 static int mt9m114_s_ctrl(struct v4l2_ctrl *ctrl)
1227 {
1228 	struct mt9m114_device *dev =
1229 	    container_of(ctrl->handler, struct mt9m114_device, ctrl_handler);
1230 	struct i2c_client *client = v4l2_get_subdevdata(&dev->sd);
1231 	int ret = 0;
1232 
1233 	switch (ctrl->id) {
1234 	case V4L2_CID_VFLIP:
1235 		dev_dbg(&client->dev, "%s: CID_VFLIP:%d.\n",
1236 			__func__, ctrl->val);
1237 		ret = mt9m114_t_vflip(&dev->sd, ctrl->val);
1238 		break;
1239 	case V4L2_CID_HFLIP:
1240 		dev_dbg(&client->dev, "%s: CID_HFLIP:%d.\n",
1241 			__func__, ctrl->val);
1242 		ret = mt9m114_t_hflip(&dev->sd, ctrl->val);
1243 		break;
1244 	case V4L2_CID_EXPOSURE_METERING:
1245 		ret = mt9m114_s_exposure_metering(&dev->sd, ctrl->val);
1246 		break;
1247 	case V4L2_CID_EXPOSURE:
1248 		ret = mt9m114_s_ev(&dev->sd, ctrl->val);
1249 		break;
1250 	case V4L2_CID_3A_LOCK:
1251 		ret = mt9m114_s_3a_lock(&dev->sd, ctrl->val);
1252 		break;
1253 	default:
1254 		ret = -EINVAL;
1255 	}
1256 	return ret;
1257 }
1258 
1259 static int mt9m114_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
1260 {
1261 	struct mt9m114_device *dev =
1262 	    container_of(ctrl->handler, struct mt9m114_device, ctrl_handler);
1263 	int ret = 0;
1264 
1265 	switch (ctrl->id) {
1266 	case V4L2_CID_VFLIP:
1267 		ret = mt9m114_g_vflip(&dev->sd, &ctrl->val);
1268 		break;
1269 	case V4L2_CID_HFLIP:
1270 		ret = mt9m114_g_hflip(&dev->sd, &ctrl->val);
1271 		break;
1272 	case V4L2_CID_FOCAL_ABSOLUTE:
1273 		ret = mt9m114_g_focal(&dev->sd, &ctrl->val);
1274 		break;
1275 	case V4L2_CID_FNUMBER_ABSOLUTE:
1276 		ret = mt9m114_g_fnumber(&dev->sd, &ctrl->val);
1277 		break;
1278 	case V4L2_CID_FNUMBER_RANGE:
1279 		ret = mt9m114_g_fnumber_range(&dev->sd, &ctrl->val);
1280 		break;
1281 	case V4L2_CID_EXPOSURE_ABSOLUTE:
1282 		ret = mt9m114_g_exposure(&dev->sd, &ctrl->val);
1283 		break;
1284 	case V4L2_CID_EXPOSURE_ZONE_NUM:
1285 		ret = mt9m114_g_exposure_zone_num(&dev->sd, &ctrl->val);
1286 		break;
1287 	case V4L2_CID_BIN_FACTOR_HORZ:
1288 		ret = mt9m114_g_bin_factor_x(&dev->sd, &ctrl->val);
1289 		break;
1290 	case V4L2_CID_BIN_FACTOR_VERT:
1291 		ret = mt9m114_g_bin_factor_y(&dev->sd, &ctrl->val);
1292 		break;
1293 	case V4L2_CID_EXPOSURE:
1294 		ret = mt9m114_g_ev(&dev->sd, &ctrl->val);
1295 		break;
1296 	case V4L2_CID_3A_LOCK:
1297 		ret = mt9m114_g_3a_lock(&dev->sd, &ctrl->val);
1298 		break;
1299 	default:
1300 		ret = -EINVAL;
1301 	}
1302 
1303 	return ret;
1304 }
1305 
1306 static const struct v4l2_ctrl_ops ctrl_ops = {
1307 	.s_ctrl = mt9m114_s_ctrl,
1308 	.g_volatile_ctrl = mt9m114_g_volatile_ctrl
1309 };
1310 
1311 static struct v4l2_ctrl_config mt9m114_controls[] = {
1312 	{
1313 		.ops = &ctrl_ops,
1314 		.id = V4L2_CID_VFLIP,
1315 		.name = "Image v-Flip",
1316 		.type = V4L2_CTRL_TYPE_INTEGER,
1317 		.min = 0,
1318 		.max = 1,
1319 		.step = 1,
1320 		.def = 0,
1321 	},
1322 	{
1323 		.ops = &ctrl_ops,
1324 		.id = V4L2_CID_HFLIP,
1325 		.name = "Image h-Flip",
1326 		.type = V4L2_CTRL_TYPE_INTEGER,
1327 		.min = 0,
1328 		.max = 1,
1329 		.step = 1,
1330 		.def = 0,
1331 	},
1332 	{
1333 		.ops = &ctrl_ops,
1334 		.id = V4L2_CID_FOCAL_ABSOLUTE,
1335 		.name = "focal length",
1336 		.type = V4L2_CTRL_TYPE_INTEGER,
1337 		.min = MT9M114_FOCAL_LENGTH_DEFAULT,
1338 		.max = MT9M114_FOCAL_LENGTH_DEFAULT,
1339 		.step = 1,
1340 		.def = MT9M114_FOCAL_LENGTH_DEFAULT,
1341 		.flags = 0,
1342 	},
1343 	{
1344 		.ops = &ctrl_ops,
1345 		.id = V4L2_CID_FNUMBER_ABSOLUTE,
1346 		.name = "f-number",
1347 		.type = V4L2_CTRL_TYPE_INTEGER,
1348 		.min = MT9M114_F_NUMBER_DEFAULT,
1349 		.max = MT9M114_F_NUMBER_DEFAULT,
1350 		.step = 1,
1351 		.def = MT9M114_F_NUMBER_DEFAULT,
1352 		.flags = 0,
1353 	},
1354 	{
1355 		.ops = &ctrl_ops,
1356 		.id = V4L2_CID_FNUMBER_RANGE,
1357 		.name = "f-number range",
1358 		.type = V4L2_CTRL_TYPE_INTEGER,
1359 		.min = MT9M114_F_NUMBER_RANGE,
1360 		.max = MT9M114_F_NUMBER_RANGE,
1361 		.step = 1,
1362 		.def = MT9M114_F_NUMBER_RANGE,
1363 		.flags = 0,
1364 	},
1365 	{
1366 		.ops = &ctrl_ops,
1367 		.id = V4L2_CID_EXPOSURE_ABSOLUTE,
1368 		.name = "exposure",
1369 		.type = V4L2_CTRL_TYPE_INTEGER,
1370 		.min = 0,
1371 		.max = 0xffff,
1372 		.step = 1,
1373 		.def = 0,
1374 		.flags = 0,
1375 	},
1376 	{
1377 		.ops = &ctrl_ops,
1378 		.id = V4L2_CID_EXPOSURE_ZONE_NUM,
1379 		.name = "one-time exposure zone number",
1380 		.type = V4L2_CTRL_TYPE_INTEGER,
1381 		.min = 0,
1382 		.max = 0xffff,
1383 		.step = 1,
1384 		.def = 0,
1385 		.flags = 0,
1386 	},
1387 	{
1388 		.ops = &ctrl_ops,
1389 		.id = V4L2_CID_EXPOSURE_METERING,
1390 		.name = "metering",
1391 		.type = V4L2_CTRL_TYPE_MENU,
1392 		.min = 0,
1393 		.max = 3,
1394 		.step = 0,
1395 		.def = 1,
1396 		.flags = 0,
1397 	},
1398 	{
1399 		.ops = &ctrl_ops,
1400 		.id = V4L2_CID_BIN_FACTOR_HORZ,
1401 		.name = "horizontal binning factor",
1402 		.type = V4L2_CTRL_TYPE_INTEGER,
1403 		.min = 0,
1404 		.max = MT9M114_BIN_FACTOR_MAX,
1405 		.step = 1,
1406 		.def = 0,
1407 		.flags = 0,
1408 	},
1409 	{
1410 		.ops = &ctrl_ops,
1411 		.id = V4L2_CID_BIN_FACTOR_VERT,
1412 		.name = "vertical binning factor",
1413 		.type = V4L2_CTRL_TYPE_INTEGER,
1414 		.min = 0,
1415 		.max = MT9M114_BIN_FACTOR_MAX,
1416 		.step = 1,
1417 		.def = 0,
1418 		.flags = 0,
1419 	},
1420 	{
1421 		.ops = &ctrl_ops,
1422 		.id = V4L2_CID_EXPOSURE,
1423 		.name = "exposure biasx",
1424 		.type = V4L2_CTRL_TYPE_INTEGER,
1425 		.min = -2,
1426 		.max = 2,
1427 		.step = 1,
1428 		.def = 0,
1429 		.flags = 0,
1430 	},
1431 	{
1432 		.ops = &ctrl_ops,
1433 		.id = V4L2_CID_3A_LOCK,
1434 		.name = "3a lock",
1435 		.type = V4L2_CTRL_TYPE_BITMASK,
1436 		.min = 0,
1437 		.max = V4L2_LOCK_EXPOSURE | V4L2_LOCK_WHITE_BALANCE | V4L2_LOCK_FOCUS,
1438 		.step = 1,
1439 		.def = 0,
1440 		.flags = 0,
1441 	},
1442 };
1443 
1444 static int mt9m114_detect(struct mt9m114_device *dev, struct i2c_client *client)
1445 {
1446 	struct i2c_adapter *adapter = client->adapter;
1447 	u32 model;
1448 	int ret;
1449 
1450 	if (!i2c_check_functionality(adapter, I2C_FUNC_I2C)) {
1451 		dev_err(&client->dev, "%s: i2c error", __func__);
1452 		return -ENODEV;
1453 	}
1454 	ret = mt9m114_read_reg(client, MISENSOR_16BIT, MT9M114_PID, &model);
1455 	if (ret)
1456 		return ret;
1457 	dev->real_model_id = model;
1458 
1459 	if (model != MT9M114_MOD_ID) {
1460 		dev_err(&client->dev, "%s: failed: client->addr = %x\n",
1461 			__func__, client->addr);
1462 		return -ENODEV;
1463 	}
1464 
1465 	return 0;
1466 }
1467 
1468 static int
1469 mt9m114_s_config(struct v4l2_subdev *sd, int irq, void *platform_data)
1470 {
1471 	struct mt9m114_device *dev = to_mt9m114_sensor(sd);
1472 	struct i2c_client *client = v4l2_get_subdevdata(sd);
1473 	int ret;
1474 
1475 	if (!platform_data)
1476 		return -ENODEV;
1477 
1478 	dev->platform_data =
1479 	    (struct camera_sensor_platform_data *)platform_data;
1480 
1481 	ret = power_up(sd);
1482 	if (ret) {
1483 		v4l2_err(client, "mt9m114 power-up err");
1484 		return ret;
1485 	}
1486 
1487 	/* config & detect sensor */
1488 	ret = mt9m114_detect(dev, client);
1489 	if (ret) {
1490 		v4l2_err(client, "mt9m114_detect err s_config.\n");
1491 		goto fail_detect;
1492 	}
1493 
1494 	ret = dev->platform_data->csi_cfg(sd, 1);
1495 	if (ret)
1496 		goto fail_csi_cfg;
1497 
1498 	ret = mt9m114_set_suspend(sd);
1499 	if (ret) {
1500 		v4l2_err(client, "mt9m114 suspend err");
1501 		return ret;
1502 	}
1503 
1504 	ret = power_down(sd);
1505 	if (ret) {
1506 		v4l2_err(client, "mt9m114 power down err");
1507 		return ret;
1508 	}
1509 
1510 	return ret;
1511 
1512 fail_csi_cfg:
1513 	dev->platform_data->csi_cfg(sd, 0);
1514 fail_detect:
1515 	power_down(sd);
1516 	dev_err(&client->dev, "sensor power-gating failed\n");
1517 	return ret;
1518 }
1519 
1520 /* Horizontal flip the image. */
1521 static int mt9m114_t_hflip(struct v4l2_subdev *sd, int value)
1522 {
1523 	struct i2c_client *c = v4l2_get_subdevdata(sd);
1524 	struct mt9m114_device *dev = to_mt9m114_sensor(sd);
1525 	int err;
1526 	/* set for direct mode */
1527 	err = mt9m114_write_reg(c, MISENSOR_16BIT, 0x098E, 0xC850);
1528 	if (value) {
1529 		/* enable H flip ctx A */
1530 		err += misensor_rmw_reg(c, MISENSOR_8BIT, 0xC850, 0x01, 0x01);
1531 		err += misensor_rmw_reg(c, MISENSOR_8BIT, 0xC851, 0x01, 0x01);
1532 		/* ctx B */
1533 		err += misensor_rmw_reg(c, MISENSOR_8BIT, 0xC888, 0x01, 0x01);
1534 		err += misensor_rmw_reg(c, MISENSOR_8BIT, 0xC889, 0x01, 0x01);
1535 
1536 		err += misensor_rmw_reg(c, MISENSOR_16BIT, MISENSOR_READ_MODE,
1537 					MISENSOR_HFLIP_MASK, MISENSOR_FLIP_EN);
1538 
1539 		dev->bpat = MT9M114_BPAT_GRGRBGBG;
1540 	} else {
1541 		/* disable H flip ctx A */
1542 		err += misensor_rmw_reg(c, MISENSOR_8BIT, 0xC850, 0x01, 0x00);
1543 		err += misensor_rmw_reg(c, MISENSOR_8BIT, 0xC851, 0x01, 0x00);
1544 		/* ctx B */
1545 		err += misensor_rmw_reg(c, MISENSOR_8BIT, 0xC888, 0x01, 0x00);
1546 		err += misensor_rmw_reg(c, MISENSOR_8BIT, 0xC889, 0x01, 0x00);
1547 
1548 		err += misensor_rmw_reg(c, MISENSOR_16BIT, MISENSOR_READ_MODE,
1549 					MISENSOR_HFLIP_MASK, MISENSOR_FLIP_DIS);
1550 
1551 		dev->bpat = MT9M114_BPAT_BGBGGRGR;
1552 	}
1553 
1554 	err += mt9m114_write_reg(c, MISENSOR_8BIT, 0x8404, 0x06);
1555 	udelay(10);
1556 
1557 	return !!err;
1558 }
1559 
1560 /* Vertically flip the image */
1561 static int mt9m114_t_vflip(struct v4l2_subdev *sd, int value)
1562 {
1563 	struct i2c_client *c = v4l2_get_subdevdata(sd);
1564 	int err;
1565 	/* set for direct mode */
1566 	err = mt9m114_write_reg(c, MISENSOR_16BIT, 0x098E, 0xC850);
1567 	if (value >= 1) {
1568 		/* enable H flip - ctx A */
1569 		err += misensor_rmw_reg(c, MISENSOR_8BIT, 0xC850, 0x02, 0x01);
1570 		err += misensor_rmw_reg(c, MISENSOR_8BIT, 0xC851, 0x02, 0x01);
1571 		/* ctx B */
1572 		err += misensor_rmw_reg(c, MISENSOR_8BIT, 0xC888, 0x02, 0x01);
1573 		err += misensor_rmw_reg(c, MISENSOR_8BIT, 0xC889, 0x02, 0x01);
1574 
1575 		err += misensor_rmw_reg(c, MISENSOR_16BIT, MISENSOR_READ_MODE,
1576 					MISENSOR_VFLIP_MASK, MISENSOR_FLIP_EN);
1577 	} else {
1578 		/* disable H flip - ctx A */
1579 		err += misensor_rmw_reg(c, MISENSOR_8BIT, 0xC850, 0x02, 0x00);
1580 		err += misensor_rmw_reg(c, MISENSOR_8BIT, 0xC851, 0x02, 0x00);
1581 		/* ctx B */
1582 		err += misensor_rmw_reg(c, MISENSOR_8BIT, 0xC888, 0x02, 0x00);
1583 		err += misensor_rmw_reg(c, MISENSOR_8BIT, 0xC889, 0x02, 0x00);
1584 
1585 		err += misensor_rmw_reg(c, MISENSOR_16BIT, MISENSOR_READ_MODE,
1586 					MISENSOR_VFLIP_MASK, MISENSOR_FLIP_DIS);
1587 	}
1588 
1589 	err += mt9m114_write_reg(c, MISENSOR_8BIT, 0x8404, 0x06);
1590 	udelay(10);
1591 
1592 	return !!err;
1593 }
1594 
1595 static int mt9m114_g_frame_interval(struct v4l2_subdev *sd,
1596 				    struct v4l2_subdev_frame_interval *interval)
1597 {
1598 	struct mt9m114_device *dev = to_mt9m114_sensor(sd);
1599 
1600 	interval->interval.numerator = 1;
1601 	interval->interval.denominator = mt9m114_res[dev->res].fps;
1602 
1603 	return 0;
1604 }
1605 
1606 static int mt9m114_s_stream(struct v4l2_subdev *sd, int enable)
1607 {
1608 	int ret;
1609 	struct i2c_client *c = v4l2_get_subdevdata(sd);
1610 	struct mt9m114_device *dev = to_mt9m114_sensor(sd);
1611 	struct atomisp_exposure exposure;
1612 
1613 	if (enable) {
1614 		ret = mt9m114_write_reg_array(c, mt9m114_chgstat_reg,
1615 					      POST_POLLING);
1616 		if (ret < 0)
1617 			return ret;
1618 
1619 		if (dev->first_exp > MT9M114_MAX_FIRST_EXP) {
1620 			exposure.integration_time[0] = dev->first_exp;
1621 			exposure.gain[0] = dev->first_gain;
1622 			exposure.gain[1] = dev->first_diggain;
1623 			mt9m114_s_exposure(sd, &exposure);
1624 		}
1625 		dev->streamon = 1;
1626 
1627 	} else {
1628 		dev->streamon = 0;
1629 		ret = mt9m114_set_suspend(sd);
1630 	}
1631 
1632 	return ret;
1633 }
1634 
1635 static int mt9m114_enum_mbus_code(struct v4l2_subdev *sd,
1636 				  struct v4l2_subdev_state *sd_state,
1637 				  struct v4l2_subdev_mbus_code_enum *code)
1638 {
1639 	if (code->index)
1640 		return -EINVAL;
1641 	code->code = MEDIA_BUS_FMT_SGRBG10_1X10;
1642 
1643 	return 0;
1644 }
1645 
1646 static int mt9m114_enum_frame_size(struct v4l2_subdev *sd,
1647 				   struct v4l2_subdev_state *sd_state,
1648 				   struct v4l2_subdev_frame_size_enum *fse)
1649 {
1650 	unsigned int index = fse->index;
1651 
1652 	if (index >= N_RES)
1653 		return -EINVAL;
1654 
1655 	fse->min_width = mt9m114_res[index].width;
1656 	fse->min_height = mt9m114_res[index].height;
1657 	fse->max_width = mt9m114_res[index].width;
1658 	fse->max_height = mt9m114_res[index].height;
1659 
1660 	return 0;
1661 }
1662 
1663 static int mt9m114_g_skip_frames(struct v4l2_subdev *sd, u32 *frames)
1664 {
1665 	int index;
1666 	struct mt9m114_device *snr = to_mt9m114_sensor(sd);
1667 
1668 	if (!frames)
1669 		return -EINVAL;
1670 
1671 	for (index = 0; index < N_RES; index++) {
1672 		if (mt9m114_res[index].res == snr->res)
1673 			break;
1674 	}
1675 
1676 	if (index >= N_RES)
1677 		return -EINVAL;
1678 
1679 	*frames = mt9m114_res[index].skip_frames;
1680 
1681 	return 0;
1682 }
1683 
1684 static const struct v4l2_subdev_video_ops mt9m114_video_ops = {
1685 	.s_stream = mt9m114_s_stream,
1686 	.g_frame_interval = mt9m114_g_frame_interval,
1687 };
1688 
1689 static const struct v4l2_subdev_sensor_ops mt9m114_sensor_ops = {
1690 	.g_skip_frames	= mt9m114_g_skip_frames,
1691 };
1692 
1693 static const struct v4l2_subdev_core_ops mt9m114_core_ops = {
1694 	.s_power = mt9m114_s_power,
1695 	.ioctl = mt9m114_ioctl,
1696 };
1697 
1698 /* REVISIT: Do we need pad operations? */
1699 static const struct v4l2_subdev_pad_ops mt9m114_pad_ops = {
1700 	.enum_mbus_code = mt9m114_enum_mbus_code,
1701 	.enum_frame_size = mt9m114_enum_frame_size,
1702 	.get_fmt = mt9m114_get_fmt,
1703 	.set_fmt = mt9m114_set_fmt,
1704 	.set_selection = mt9m114_s_exposure_selection,
1705 };
1706 
1707 static const struct v4l2_subdev_ops mt9m114_ops = {
1708 	.core = &mt9m114_core_ops,
1709 	.video = &mt9m114_video_ops,
1710 	.pad = &mt9m114_pad_ops,
1711 	.sensor = &mt9m114_sensor_ops,
1712 };
1713 
1714 static int mt9m114_remove(struct i2c_client *client)
1715 {
1716 	struct mt9m114_device *dev;
1717 	struct v4l2_subdev *sd = i2c_get_clientdata(client);
1718 
1719 	dev = container_of(sd, struct mt9m114_device, sd);
1720 	dev->platform_data->csi_cfg(sd, 0);
1721 	v4l2_device_unregister_subdev(sd);
1722 	media_entity_cleanup(&dev->sd.entity);
1723 	v4l2_ctrl_handler_free(&dev->ctrl_handler);
1724 	kfree(dev);
1725 	return 0;
1726 }
1727 
1728 static int mt9m114_probe(struct i2c_client *client)
1729 {
1730 	struct mt9m114_device *dev;
1731 	int ret = 0;
1732 	unsigned int i;
1733 	void *pdata;
1734 
1735 	/* Setup sensor configuration structure */
1736 	dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1737 	if (!dev)
1738 		return -ENOMEM;
1739 
1740 	v4l2_i2c_subdev_init(&dev->sd, client, &mt9m114_ops);
1741 	pdata = gmin_camera_platform_data(&dev->sd,
1742 					  ATOMISP_INPUT_FORMAT_RAW_10,
1743 					  atomisp_bayer_order_grbg);
1744 	if (pdata)
1745 		ret = mt9m114_s_config(&dev->sd, client->irq, pdata);
1746 	if (!pdata || ret) {
1747 		v4l2_device_unregister_subdev(&dev->sd);
1748 		kfree(dev);
1749 		return ret;
1750 	}
1751 
1752 	ret = atomisp_register_i2c_module(&dev->sd, pdata, RAW_CAMERA);
1753 	if (ret) {
1754 		v4l2_device_unregister_subdev(&dev->sd);
1755 		kfree(dev);
1756 		/* Coverity CID 298095 - return on error */
1757 		return ret;
1758 	}
1759 
1760 	/* TODO add format code here */
1761 	dev->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
1762 	dev->pad.flags = MEDIA_PAD_FL_SOURCE;
1763 	dev->format.code = MEDIA_BUS_FMT_SGRBG10_1X10;
1764 	dev->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
1765 
1766 	ret =
1767 	    v4l2_ctrl_handler_init(&dev->ctrl_handler,
1768 				   ARRAY_SIZE(mt9m114_controls));
1769 	if (ret) {
1770 		mt9m114_remove(client);
1771 		return ret;
1772 	}
1773 
1774 	for (i = 0; i < ARRAY_SIZE(mt9m114_controls); i++)
1775 		v4l2_ctrl_new_custom(&dev->ctrl_handler, &mt9m114_controls[i],
1776 				     NULL);
1777 
1778 	if (dev->ctrl_handler.error) {
1779 		mt9m114_remove(client);
1780 		return dev->ctrl_handler.error;
1781 	}
1782 
1783 	/* Use same lock for controls as for everything else. */
1784 	dev->ctrl_handler.lock = &dev->input_lock;
1785 	dev->sd.ctrl_handler = &dev->ctrl_handler;
1786 
1787 	/* REVISIT: Do we need media controller? */
1788 	ret = media_entity_pads_init(&dev->sd.entity, 1, &dev->pad);
1789 	if (ret) {
1790 		mt9m114_remove(client);
1791 		return ret;
1792 	}
1793 	return 0;
1794 }
1795 
1796 static const struct acpi_device_id mt9m114_acpi_match[] = {
1797 	{ "INT33F0" },
1798 	{ "CRMT1040" },
1799 	{},
1800 };
1801 MODULE_DEVICE_TABLE(acpi, mt9m114_acpi_match);
1802 
1803 static struct i2c_driver mt9m114_driver = {
1804 	.driver = {
1805 		.name = "mt9m114",
1806 		.acpi_match_table = mt9m114_acpi_match,
1807 	},
1808 	.probe_new = mt9m114_probe,
1809 	.remove = mt9m114_remove,
1810 };
1811 module_i2c_driver(mt9m114_driver);
1812 
1813 MODULE_AUTHOR("Shuguang Gong <Shuguang.gong@intel.com>");
1814 MODULE_LICENSE("GPL");
1815