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 	/* Note: current modules wire only one GPIO signal (RESET#),
479 	 * but the schematic wires up two to the connector.  BIOS
480 	 * versions have been unfortunately inconsistent with which
481 	 * ACPI index RESET# is on, so hit both */
482 
483 	if (flag) {
484 		ret = dev->platform_data->gpio0_ctrl(sd, 0);
485 		ret = dev->platform_data->gpio1_ctrl(sd, 0);
486 		msleep(60);
487 		ret |= dev->platform_data->gpio0_ctrl(sd, 1);
488 		ret |= dev->platform_data->gpio1_ctrl(sd, 1);
489 	} else {
490 		ret = dev->platform_data->gpio0_ctrl(sd, 0);
491 		ret = dev->platform_data->gpio1_ctrl(sd, 0);
492 	}
493 	return ret;
494 }
495 
496 static int power_up(struct v4l2_subdev *sd)
497 {
498 	struct mt9m114_device *dev = to_mt9m114_sensor(sd);
499 	struct i2c_client *client = v4l2_get_subdevdata(sd);
500 	int ret;
501 
502 	if (!dev->platform_data) {
503 		dev_err(&client->dev, "no camera_sensor_platform_data");
504 		return -ENODEV;
505 	}
506 
507 	/* power control */
508 	ret = power_ctrl(sd, 1);
509 	if (ret)
510 		goto fail_power;
511 
512 	/* flis clock control */
513 	ret = dev->platform_data->flisclk_ctrl(sd, 1);
514 	if (ret)
515 		goto fail_clk;
516 
517 	/* gpio ctrl */
518 	ret = gpio_ctrl(sd, 1);
519 	if (ret)
520 		dev_err(&client->dev, "gpio failed 1\n");
521 	/*
522 	 * according to DS, 44ms is needed between power up and first i2c
523 	 * commend
524 	 */
525 	msleep(50);
526 
527 	return 0;
528 
529 fail_clk:
530 	dev->platform_data->flisclk_ctrl(sd, 0);
531 fail_power:
532 	power_ctrl(sd, 0);
533 	dev_err(&client->dev, "sensor power-up failed\n");
534 
535 	return ret;
536 }
537 
538 static int power_down(struct v4l2_subdev *sd)
539 {
540 	struct mt9m114_device *dev = to_mt9m114_sensor(sd);
541 	struct i2c_client *client = v4l2_get_subdevdata(sd);
542 	int ret;
543 
544 	if (!dev->platform_data) {
545 		dev_err(&client->dev, "no camera_sensor_platform_data");
546 		return -ENODEV;
547 	}
548 
549 	ret = dev->platform_data->flisclk_ctrl(sd, 0);
550 	if (ret)
551 		dev_err(&client->dev, "flisclk failed\n");
552 
553 	/* gpio ctrl */
554 	ret = gpio_ctrl(sd, 0);
555 	if (ret)
556 		dev_err(&client->dev, "gpio failed 1\n");
557 
558 	/* power control */
559 	ret = power_ctrl(sd, 0);
560 	if (ret)
561 		dev_err(&client->dev, "vprog failed.\n");
562 
563 	/*according to DS, 20ms is needed after power down*/
564 	msleep(20);
565 
566 	return ret;
567 }
568 
569 static int mt9m114_s_power(struct v4l2_subdev *sd, int power)
570 {
571 	if (power == 0)
572 		return power_down(sd);
573 	else {
574 		if (power_up(sd))
575 			return -EINVAL;
576 
577 		return mt9m114_init_common(sd);
578 	}
579 }
580 
581 /*
582  * distance - calculate the distance
583  * @res: resolution
584  * @w: width
585  * @h: height
586  *
587  * Get the gap between resolution and w/h.
588  * res->width/height smaller than w/h wouldn't be considered.
589  * Returns the value of gap or -1 if fail.
590  */
591 #define LARGEST_ALLOWED_RATIO_MISMATCH 600
592 static int distance(struct mt9m114_res_struct const *res, u32 w, u32 h)
593 {
594 	unsigned int w_ratio;
595 	unsigned int h_ratio;
596 	int match;
597 
598 	if (w == 0)
599 		return -1;
600 	w_ratio = (res->width << 13) / w;
601 	if (h == 0)
602 		return -1;
603 	h_ratio = (res->height << 13) / h;
604 	if (h_ratio == 0)
605 		return -1;
606 	match   = abs(((w_ratio << 13) / h_ratio) - 8192);
607 
608 	if ((w_ratio < 8192) || (h_ratio < 8192) ||
609 	    (match > LARGEST_ALLOWED_RATIO_MISMATCH))
610 		return -1;
611 
612 	return w_ratio + h_ratio;
613 }
614 
615 /* Return the nearest higher resolution index */
616 static int nearest_resolution_index(int w, int h)
617 {
618 	int i;
619 	int idx = -1;
620 	int dist;
621 	int min_dist = INT_MAX;
622 	const struct mt9m114_res_struct *tmp_res = NULL;
623 
624 	for (i = 0; i < ARRAY_SIZE(mt9m114_res); i++) {
625 		tmp_res = &mt9m114_res[i];
626 		dist = distance(tmp_res, w, h);
627 		if (dist == -1)
628 			continue;
629 		if (dist < min_dist) {
630 			min_dist = dist;
631 			idx = i;
632 		}
633 	}
634 
635 	return idx;
636 }
637 
638 static int mt9m114_try_res(u32 *w, u32 *h)
639 {
640 	int idx = 0;
641 
642 	if ((*w > MT9M114_RES_960P_SIZE_H)
643 	    || (*h > MT9M114_RES_960P_SIZE_V)) {
644 		*w = MT9M114_RES_960P_SIZE_H;
645 		*h = MT9M114_RES_960P_SIZE_V;
646 	} else {
647 		idx = nearest_resolution_index(*w, *h);
648 
649 		/*
650 		 * nearest_resolution_index() doesn't return smaller
651 		 *  resolutions. If it fails, it means the requested
652 		 *  resolution is higher than wecan support. Fallback
653 		 *  to highest possible resolution in this case.
654 		 */
655 		if (idx == -1)
656 			idx = ARRAY_SIZE(mt9m114_res) - 1;
657 
658 		*w = mt9m114_res[idx].width;
659 		*h = mt9m114_res[idx].height;
660 	}
661 
662 	return 0;
663 }
664 
665 static struct mt9m114_res_struct *mt9m114_to_res(u32 w, u32 h)
666 {
667 	int  index;
668 
669 	for (index = 0; index < N_RES; index++) {
670 		if ((mt9m114_res[index].width == w) &&
671 		    (mt9m114_res[index].height == h))
672 			break;
673 	}
674 
675 	/* No mode found */
676 	if (index >= N_RES)
677 		return NULL;
678 
679 	return &mt9m114_res[index];
680 }
681 
682 static int mt9m114_res2size(struct v4l2_subdev *sd, int *h_size, int *v_size)
683 {
684 	struct mt9m114_device *dev = to_mt9m114_sensor(sd);
685 	unsigned short hsize;
686 	unsigned short vsize;
687 
688 	switch (dev->res) {
689 	case MT9M114_RES_736P:
690 		hsize = MT9M114_RES_736P_SIZE_H;
691 		vsize = MT9M114_RES_736P_SIZE_V;
692 		break;
693 	case MT9M114_RES_864P:
694 		hsize = MT9M114_RES_864P_SIZE_H;
695 		vsize = MT9M114_RES_864P_SIZE_V;
696 		break;
697 	case MT9M114_RES_960P:
698 		hsize = MT9M114_RES_960P_SIZE_H;
699 		vsize = MT9M114_RES_960P_SIZE_V;
700 		break;
701 	default:
702 		v4l2_err(sd, "%s: Resolution 0x%08x unknown\n", __func__,
703 			 dev->res);
704 		return -EINVAL;
705 	}
706 
707 	if (h_size)
708 		*h_size = hsize;
709 	if (v_size)
710 		*v_size = vsize;
711 
712 	return 0;
713 }
714 
715 static int mt9m114_get_intg_factor(struct i2c_client *client,
716 				   struct camera_mipi_info *info,
717 				   const struct mt9m114_res_struct *res)
718 {
719 	struct atomisp_sensor_mode_data *buf = &info->data;
720 	u32 reg_val;
721 	int ret;
722 
723 	if (!info)
724 		return -EINVAL;
725 
726 	ret =  mt9m114_read_reg(client, MISENSOR_32BIT,
727 				REG_PIXEL_CLK, &reg_val);
728 	if (ret)
729 		return ret;
730 	buf->vt_pix_clk_freq_mhz = reg_val;
731 
732 	/* get integration time */
733 	buf->coarse_integration_time_min = MT9M114_COARSE_INTG_TIME_MIN;
734 	buf->coarse_integration_time_max_margin =
735 	    MT9M114_COARSE_INTG_TIME_MAX_MARGIN;
736 
737 	buf->fine_integration_time_min = MT9M114_FINE_INTG_TIME_MIN;
738 	buf->fine_integration_time_max_margin =
739 	    MT9M114_FINE_INTG_TIME_MAX_MARGIN;
740 
741 	buf->fine_integration_time_def = MT9M114_FINE_INTG_TIME_MIN;
742 
743 	buf->frame_length_lines = res->lines_per_frame;
744 	buf->line_length_pck = res->pixels_per_line;
745 	buf->read_mode = res->bin_mode;
746 
747 	/* get the cropping and output resolution to ISP for this mode. */
748 	ret =  mt9m114_read_reg(client, MISENSOR_16BIT,
749 				REG_H_START, &reg_val);
750 	if (ret)
751 		return ret;
752 	buf->crop_horizontal_start = reg_val;
753 
754 	ret =  mt9m114_read_reg(client, MISENSOR_16BIT,
755 				REG_V_START, &reg_val);
756 	if (ret)
757 		return ret;
758 	buf->crop_vertical_start = reg_val;
759 
760 	ret = mt9m114_read_reg(client, MISENSOR_16BIT,
761 			       REG_H_END, &reg_val);
762 	if (ret)
763 		return ret;
764 	buf->crop_horizontal_end = reg_val;
765 
766 	ret = mt9m114_read_reg(client, MISENSOR_16BIT,
767 			       REG_V_END, &reg_val);
768 	if (ret)
769 		return ret;
770 	buf->crop_vertical_end = reg_val;
771 
772 	ret = mt9m114_read_reg(client, MISENSOR_16BIT,
773 			       REG_WIDTH, &reg_val);
774 	if (ret)
775 		return ret;
776 	buf->output_width = reg_val;
777 
778 	ret = mt9m114_read_reg(client, MISENSOR_16BIT,
779 			       REG_HEIGHT, &reg_val);
780 	if (ret)
781 		return ret;
782 	buf->output_height = reg_val;
783 
784 	ret = mt9m114_read_reg(client, MISENSOR_16BIT,
785 			       REG_TIMING_HTS, &reg_val);
786 	if (ret)
787 		return ret;
788 	buf->line_length_pck = reg_val;
789 
790 	ret = mt9m114_read_reg(client, MISENSOR_16BIT,
791 			       REG_TIMING_VTS, &reg_val);
792 	if (ret)
793 		return ret;
794 	buf->frame_length_lines = reg_val;
795 
796 	buf->binning_factor_x = res->bin_factor_x ?
797 				res->bin_factor_x : 1;
798 	buf->binning_factor_y = res->bin_factor_y ?
799 				res->bin_factor_y : 1;
800 	return 0;
801 }
802 
803 static int mt9m114_get_fmt(struct v4l2_subdev *sd,
804 			   struct v4l2_subdev_pad_config *cfg,
805 			   struct v4l2_subdev_format *format)
806 {
807 	struct v4l2_mbus_framefmt *fmt = &format->format;
808 	int width, height;
809 	int ret;
810 
811 	if (format->pad)
812 		return -EINVAL;
813 	fmt->code = MEDIA_BUS_FMT_SGRBG10_1X10;
814 
815 	ret = mt9m114_res2size(sd, &width, &height);
816 	if (ret)
817 		return ret;
818 	fmt->width = width;
819 	fmt->height = height;
820 
821 	return 0;
822 }
823 
824 static int mt9m114_set_fmt(struct v4l2_subdev *sd,
825 			   struct v4l2_subdev_pad_config *cfg,
826 			   struct v4l2_subdev_format *format)
827 {
828 	struct v4l2_mbus_framefmt *fmt = &format->format;
829 	struct i2c_client *c = v4l2_get_subdevdata(sd);
830 	struct mt9m114_device *dev = to_mt9m114_sensor(sd);
831 	struct mt9m114_res_struct *res_index;
832 	u32 width = fmt->width;
833 	u32 height = fmt->height;
834 	struct camera_mipi_info *mt9m114_info = NULL;
835 
836 	int ret;
837 
838 	if (format->pad)
839 		return -EINVAL;
840 	dev->streamon = 0;
841 	dev->first_exp = MT9M114_DEFAULT_FIRST_EXP;
842 
843 	mt9m114_info = v4l2_get_subdev_hostdata(sd);
844 	if (!mt9m114_info)
845 		return -EINVAL;
846 
847 	mt9m114_try_res(&width, &height);
848 	if (format->which == V4L2_SUBDEV_FORMAT_TRY) {
849 		cfg->try_fmt = *fmt;
850 		return 0;
851 	}
852 	res_index = mt9m114_to_res(width, height);
853 
854 	/* Sanity check */
855 	if (unlikely(!res_index)) {
856 		WARN_ON(1);
857 		return -EINVAL;
858 	}
859 
860 	switch (res_index->res) {
861 	case MT9M114_RES_736P:
862 		ret = mt9m114_write_reg_array(c, mt9m114_736P_init, NO_POLLING);
863 		ret += misensor_rmw_reg(c, MISENSOR_16BIT, MISENSOR_READ_MODE,
864 					MISENSOR_R_MODE_MASK, MISENSOR_NORMAL_SET);
865 		break;
866 	case MT9M114_RES_864P:
867 		ret = mt9m114_write_reg_array(c, mt9m114_864P_init, NO_POLLING);
868 		ret += misensor_rmw_reg(c, MISENSOR_16BIT, MISENSOR_READ_MODE,
869 					MISENSOR_R_MODE_MASK, MISENSOR_NORMAL_SET);
870 		break;
871 	case MT9M114_RES_960P:
872 		ret = mt9m114_write_reg_array(c, mt9m114_976P_init, NO_POLLING);
873 		/* set sensor read_mode to Normal */
874 		ret += misensor_rmw_reg(c, MISENSOR_16BIT, MISENSOR_READ_MODE,
875 					MISENSOR_R_MODE_MASK, MISENSOR_NORMAL_SET);
876 		break;
877 	default:
878 		v4l2_err(sd, "set resolution: %d failed!\n", res_index->res);
879 		return -EINVAL;
880 	}
881 
882 	if (ret)
883 		return -EINVAL;
884 
885 	ret = mt9m114_write_reg_array(c, mt9m114_chgstat_reg, POST_POLLING);
886 	if (ret < 0)
887 		return ret;
888 
889 	if (mt9m114_set_suspend(sd))
890 		return -EINVAL;
891 
892 	if (dev->res != res_index->res) {
893 		int index;
894 
895 		/* Switch to different size */
896 		if (width <= 640) {
897 			dev->nctx = 0x00; /* Set for context A */
898 		} else {
899 			/*
900 			 * Context B is used for resolutions larger than 640x480
901 			 * Using YUV for Context B.
902 			 */
903 			dev->nctx = 0x01; /* set for context B */
904 		}
905 
906 		/*
907 		 * Marked current sensor res as being "used"
908 		 *
909 		 * REVISIT: We don't need to use an "used" field on each mode
910 		 * list entry to know which mode is selected. If this
911 		 * information is really necessary, how about to use a single
912 		 * variable on sensor dev struct?
913 		 */
914 		for (index = 0; index < N_RES; index++) {
915 			if ((width == mt9m114_res[index].width) &&
916 			    (height == mt9m114_res[index].height)) {
917 				mt9m114_res[index].used = true;
918 				continue;
919 			}
920 			mt9m114_res[index].used = false;
921 		}
922 	}
923 	ret = mt9m114_get_intg_factor(c, mt9m114_info,
924 				      &mt9m114_res[res_index->res]);
925 	if (ret) {
926 		dev_err(&c->dev, "failed to get integration_factor\n");
927 		return -EINVAL;
928 	}
929 	/*
930 	 * mt9m114 - we don't poll for context switch
931 	 * because it does not happen with streaming disabled.
932 	 */
933 	dev->res = res_index->res;
934 
935 	fmt->width = width;
936 	fmt->height = height;
937 	fmt->code = MEDIA_BUS_FMT_SGRBG10_1X10;
938 	return 0;
939 }
940 
941 /* TODO: Update to SOC functions, remove exposure and gain */
942 static int mt9m114_g_focal(struct v4l2_subdev *sd, s32 *val)
943 {
944 	*val = (MT9M114_FOCAL_LENGTH_NUM << 16) | MT9M114_FOCAL_LENGTH_DEM;
945 	return 0;
946 }
947 
948 static int mt9m114_g_fnumber(struct v4l2_subdev *sd, s32 *val)
949 {
950 	/*const f number for mt9m114*/
951 	*val = (MT9M114_F_NUMBER_DEFAULT_NUM << 16) | MT9M114_F_NUMBER_DEM;
952 	return 0;
953 }
954 
955 static int mt9m114_g_fnumber_range(struct v4l2_subdev *sd, s32 *val)
956 {
957 	*val = (MT9M114_F_NUMBER_DEFAULT_NUM << 24) |
958 	       (MT9M114_F_NUMBER_DEM << 16) |
959 	       (MT9M114_F_NUMBER_DEFAULT_NUM << 8) | MT9M114_F_NUMBER_DEM;
960 	return 0;
961 }
962 
963 /* Horizontal flip the image. */
964 static int mt9m114_g_hflip(struct v4l2_subdev *sd, s32 *val)
965 {
966 	struct i2c_client *c = v4l2_get_subdevdata(sd);
967 	int ret;
968 	u32 data;
969 
970 	ret = mt9m114_read_reg(c, MISENSOR_16BIT,
971 			       (u32)MISENSOR_READ_MODE, &data);
972 	if (ret)
973 		return ret;
974 	*val = !!(data & MISENSOR_HFLIP_MASK);
975 
976 	return 0;
977 }
978 
979 static int mt9m114_g_vflip(struct v4l2_subdev *sd, s32 *val)
980 {
981 	struct i2c_client *c = v4l2_get_subdevdata(sd);
982 	int ret;
983 	u32 data;
984 
985 	ret = mt9m114_read_reg(c, MISENSOR_16BIT,
986 			       (u32)MISENSOR_READ_MODE, &data);
987 	if (ret)
988 		return ret;
989 	*val = !!(data & MISENSOR_VFLIP_MASK);
990 
991 	return 0;
992 }
993 
994 static long mt9m114_s_exposure(struct v4l2_subdev *sd,
995 			       struct atomisp_exposure *exposure)
996 {
997 	struct i2c_client *client = v4l2_get_subdevdata(sd);
998 	struct mt9m114_device *dev = to_mt9m114_sensor(sd);
999 	int ret = 0;
1000 	unsigned int coarse_integration = 0;
1001 	unsigned int FLines = 0;
1002 	unsigned int FrameLengthLines = 0; /* ExposureTime.FrameLengthLines; */
1003 	unsigned int AnalogGain, DigitalGain;
1004 	u32 AnalogGainToWrite = 0;
1005 
1006 	dev_dbg(&client->dev, "%s(0x%X 0x%X 0x%X)\n", __func__,
1007 		exposure->integration_time[0], exposure->gain[0],
1008 		exposure->gain[1]);
1009 
1010 	coarse_integration = exposure->integration_time[0];
1011 	/* fine_integration = ExposureTime.FineIntegrationTime; */
1012 	/* FrameLengthLines = ExposureTime.FrameLengthLines; */
1013 	FLines = mt9m114_res[dev->res].lines_per_frame;
1014 	AnalogGain = exposure->gain[0];
1015 	DigitalGain = exposure->gain[1];
1016 	if (!dev->streamon) {
1017 		/*Save the first exposure values while stream is off*/
1018 		dev->first_exp = coarse_integration;
1019 		dev->first_gain = AnalogGain;
1020 		dev->first_diggain = DigitalGain;
1021 	}
1022 	/* DigitalGain = 0x400 * (((u16) DigitalGain) >> 8) +
1023 	((unsigned int)(0x400 * (((u16) DigitalGain) & 0xFF)) >>8); */
1024 
1025 	/* set frame length */
1026 	if (FLines < coarse_integration + 6)
1027 		FLines = coarse_integration + 6;
1028 	if (FLines < FrameLengthLines)
1029 		FLines = FrameLengthLines;
1030 	ret = mt9m114_write_reg(client, MISENSOR_16BIT, 0x300A, FLines);
1031 	if (ret) {
1032 		v4l2_err(client, "%s: fail to set FLines\n", __func__);
1033 		return -EINVAL;
1034 	}
1035 
1036 	/* set coarse integration */
1037 	/* 3A provide real exposure time.
1038 		should not translate to any value here. */
1039 	ret = mt9m114_write_reg(client, MISENSOR_16BIT,
1040 				REG_EXPO_COARSE, (u16)(coarse_integration));
1041 	if (ret) {
1042 		v4l2_err(client, "%s: fail to set exposure time\n", __func__);
1043 		return -EINVAL;
1044 	}
1045 
1046 	/*
1047 	// set analog/digital gain
1048 	switch(AnalogGain)
1049 	{
1050 	case 0:
1051 	  AnalogGainToWrite = 0x0;
1052 	  break;
1053 	case 1:
1054 	  AnalogGainToWrite = 0x20;
1055 	  break;
1056 	case 2:
1057 	  AnalogGainToWrite = 0x60;
1058 	  break;
1059 	case 4:
1060 	  AnalogGainToWrite = 0xA0;
1061 	  break;
1062 	case 8:
1063 	  AnalogGainToWrite = 0xE0;
1064 	  break;
1065 	default:
1066 	  AnalogGainToWrite = 0x20;
1067 	  break;
1068 	}
1069 	*/
1070 	if (DigitalGain >= 16 || DigitalGain <= 1)
1071 		DigitalGain = 1;
1072 	/* AnalogGainToWrite =
1073 		(u16)((DigitalGain << 12) | AnalogGainToWrite); */
1074 	AnalogGainToWrite = (u16)((DigitalGain << 12) | (u16)AnalogGain);
1075 	ret = mt9m114_write_reg(client, MISENSOR_16BIT,
1076 				REG_GAIN, AnalogGainToWrite);
1077 	if (ret) {
1078 		v4l2_err(client, "%s: fail to set AnalogGainToWrite\n",
1079 			 __func__);
1080 		return -EINVAL;
1081 	}
1082 
1083 	return ret;
1084 }
1085 
1086 static long mt9m114_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg)
1087 {
1088 	switch (cmd) {
1089 	case ATOMISP_IOC_S_EXPOSURE:
1090 		return mt9m114_s_exposure(sd, arg);
1091 	default:
1092 		return -EINVAL;
1093 	}
1094 
1095 	return 0;
1096 }
1097 
1098 /* This returns the exposure time being used. This should only be used
1099    for filling in EXIF data, not for actual image processing. */
1100 static int mt9m114_g_exposure(struct v4l2_subdev *sd, s32 *value)
1101 {
1102 	struct i2c_client *client = v4l2_get_subdevdata(sd);
1103 	u32 coarse;
1104 	int ret;
1105 
1106 	/* the fine integration time is currently not calculated */
1107 	ret = mt9m114_read_reg(client, MISENSOR_16BIT,
1108 			       REG_EXPO_COARSE, &coarse);
1109 	if (ret)
1110 		return ret;
1111 
1112 	*value = coarse;
1113 	return 0;
1114 }
1115 
1116 /*
1117  * This function will return the sensor supported max exposure zone number.
1118  * the sensor which supports max exposure zone number is 1.
1119  */
1120 static int mt9m114_g_exposure_zone_num(struct v4l2_subdev *sd, s32 *val)
1121 {
1122 	*val = 1;
1123 
1124 	return 0;
1125 }
1126 
1127 /*
1128  * set exposure metering, average/center_weighted/spot/matrix.
1129  */
1130 static int mt9m114_s_exposure_metering(struct v4l2_subdev *sd, s32 val)
1131 {
1132 	struct i2c_client *client = v4l2_get_subdevdata(sd);
1133 	int ret;
1134 
1135 	switch (val) {
1136 	case V4L2_EXPOSURE_METERING_SPOT:
1137 		ret = mt9m114_write_reg_array(client, mt9m114_exp_average,
1138 					      NO_POLLING);
1139 		if (ret) {
1140 			dev_err(&client->dev, "write exp_average reg err.\n");
1141 			return ret;
1142 		}
1143 		break;
1144 	case V4L2_EXPOSURE_METERING_CENTER_WEIGHTED:
1145 	default:
1146 		ret = mt9m114_write_reg_array(client, mt9m114_exp_center,
1147 					      NO_POLLING);
1148 		if (ret) {
1149 			dev_err(&client->dev, "write exp_default reg err");
1150 			return ret;
1151 		}
1152 	}
1153 
1154 	return 0;
1155 }
1156 
1157 /*
1158  * This function is for touch exposure feature.
1159  */
1160 static int mt9m114_s_exposure_selection(struct v4l2_subdev *sd,
1161 					struct v4l2_subdev_pad_config *cfg,
1162 					struct v4l2_subdev_selection *sel)
1163 {
1164 	struct i2c_client *client = v4l2_get_subdevdata(sd);
1165 	struct misensor_reg exp_reg;
1166 	int width, height;
1167 	int grid_width, grid_height;
1168 	int grid_left, grid_top, grid_right, grid_bottom;
1169 	int win_left, win_top, win_right, win_bottom;
1170 	int i, j;
1171 	int ret;
1172 
1173 	if (sel->which != V4L2_SUBDEV_FORMAT_TRY &&
1174 	    sel->which != V4L2_SUBDEV_FORMAT_ACTIVE)
1175 		return -EINVAL;
1176 
1177 	grid_left = sel->r.left;
1178 	grid_top = sel->r.top;
1179 	grid_right = sel->r.left + sel->r.width - 1;
1180 	grid_bottom = sel->r.top + sel->r.height - 1;
1181 
1182 	ret = mt9m114_res2size(sd, &width, &height);
1183 	if (ret)
1184 		return ret;
1185 
1186 	grid_width = width / 5;
1187 	grid_height = height / 5;
1188 
1189 	if (grid_width && grid_height) {
1190 		win_left = grid_left / grid_width;
1191 		win_top = grid_top / grid_height;
1192 		win_right = grid_right / grid_width;
1193 		win_bottom = grid_bottom / grid_height;
1194 	} else {
1195 		dev_err(&client->dev, "Incorrect exp grid.\n");
1196 		return -EINVAL;
1197 	}
1198 
1199 	win_left   = clamp_t(int, win_left, 0, 4);
1200 	win_top    = clamp_t(int, win_top, 0, 4);
1201 	win_right  = clamp_t(int, win_right, 0, 4);
1202 	win_bottom = clamp_t(int, win_bottom, 0, 4);
1203 
1204 	ret = mt9m114_write_reg_array(client, mt9m114_exp_average, NO_POLLING);
1205 	if (ret) {
1206 		dev_err(&client->dev, "write exp_average reg err.\n");
1207 		return ret;
1208 	}
1209 
1210 	for (i = win_top; i <= win_bottom; i++) {
1211 		for (j = win_left; j <= win_right; j++) {
1212 			exp_reg = mt9m114_exp_win[i][j];
1213 
1214 			ret = mt9m114_write_reg(client, exp_reg.length,
1215 						exp_reg.reg, exp_reg.val);
1216 			if (ret) {
1217 				dev_err(&client->dev, "write exp_reg err.\n");
1218 				return ret;
1219 			}
1220 		}
1221 	}
1222 
1223 	return 0;
1224 }
1225 
1226 static int mt9m114_g_bin_factor_x(struct v4l2_subdev *sd, s32 *val)
1227 {
1228 	struct mt9m114_device *dev = to_mt9m114_sensor(sd);
1229 
1230 	*val = mt9m114_res[dev->res].bin_factor_x;
1231 
1232 	return 0;
1233 }
1234 
1235 static int mt9m114_g_bin_factor_y(struct v4l2_subdev *sd, s32 *val)
1236 {
1237 	struct mt9m114_device *dev = to_mt9m114_sensor(sd);
1238 
1239 	*val = mt9m114_res[dev->res].bin_factor_y;
1240 
1241 	return 0;
1242 }
1243 
1244 static int mt9m114_s_ev(struct v4l2_subdev *sd, s32 val)
1245 {
1246 	struct i2c_client *c = v4l2_get_subdevdata(sd);
1247 	s32 luma = 0x37;
1248 	int err;
1249 
1250 	/* EV value only support -2 to 2
1251 	 * 0: 0x37, 1:0x47, 2:0x57, -1:0x27, -2:0x17
1252 	 */
1253 	if (val < -2 || val > 2)
1254 		return -EINVAL;
1255 	luma += 0x10 * val;
1256 	dev_dbg(&c->dev, "%s val:%d luma:0x%x\n", __func__, val, luma);
1257 	err = mt9m114_write_reg(c, MISENSOR_16BIT, 0x098E, 0xC87A);
1258 	if (err) {
1259 		dev_err(&c->dev, "%s logic addr access error\n", __func__);
1260 		return err;
1261 	}
1262 	err = mt9m114_write_reg(c, MISENSOR_8BIT, 0xC87A, (u32)luma);
1263 	if (err) {
1264 		dev_err(&c->dev, "%s write target_average_luma failed\n",
1265 			__func__);
1266 		return err;
1267 	}
1268 	udelay(10);
1269 
1270 	return 0;
1271 }
1272 
1273 static int mt9m114_g_ev(struct v4l2_subdev *sd, s32 *val)
1274 {
1275 	struct i2c_client *c = v4l2_get_subdevdata(sd);
1276 	int err;
1277 	u32 luma;
1278 
1279 	err = mt9m114_write_reg(c, MISENSOR_16BIT, 0x098E, 0xC87A);
1280 	if (err) {
1281 		dev_err(&c->dev, "%s logic addr access error\n", __func__);
1282 		return err;
1283 	}
1284 	err = mt9m114_read_reg(c, MISENSOR_8BIT, 0xC87A, &luma);
1285 	if (err) {
1286 		dev_err(&c->dev, "%s read target_average_luma failed\n",
1287 			__func__);
1288 		return err;
1289 	}
1290 	luma -= 0x17;
1291 	luma /= 0x10;
1292 	*val = (s32)luma - 2;
1293 	dev_dbg(&c->dev, "%s val:%d\n", __func__, *val);
1294 
1295 	return 0;
1296 }
1297 
1298 /* Fake interface
1299  * mt9m114 now can not support 3a_lock
1300 */
1301 static int mt9m114_s_3a_lock(struct v4l2_subdev *sd, s32 val)
1302 {
1303 	aaalock = val;
1304 	return 0;
1305 }
1306 
1307 static int mt9m114_g_3a_lock(struct v4l2_subdev *sd, s32 *val)
1308 {
1309 	if (aaalock)
1310 		return V4L2_LOCK_EXPOSURE | V4L2_LOCK_WHITE_BALANCE
1311 		       | V4L2_LOCK_FOCUS;
1312 	return 0;
1313 }
1314 
1315 static int mt9m114_s_ctrl(struct v4l2_ctrl *ctrl)
1316 {
1317 	struct mt9m114_device *dev =
1318 	    container_of(ctrl->handler, struct mt9m114_device, ctrl_handler);
1319 	struct i2c_client *client = v4l2_get_subdevdata(&dev->sd);
1320 	int ret = 0;
1321 
1322 	switch (ctrl->id) {
1323 	case V4L2_CID_VFLIP:
1324 		dev_dbg(&client->dev, "%s: CID_VFLIP:%d.\n",
1325 			__func__, ctrl->val);
1326 		ret = mt9m114_t_vflip(&dev->sd, ctrl->val);
1327 		break;
1328 	case V4L2_CID_HFLIP:
1329 		dev_dbg(&client->dev, "%s: CID_HFLIP:%d.\n",
1330 			__func__, ctrl->val);
1331 		ret = mt9m114_t_hflip(&dev->sd, ctrl->val);
1332 		break;
1333 	case V4L2_CID_EXPOSURE_METERING:
1334 		ret = mt9m114_s_exposure_metering(&dev->sd, ctrl->val);
1335 		break;
1336 	case V4L2_CID_EXPOSURE:
1337 		ret = mt9m114_s_ev(&dev->sd, ctrl->val);
1338 		break;
1339 	case V4L2_CID_3A_LOCK:
1340 		ret = mt9m114_s_3a_lock(&dev->sd, ctrl->val);
1341 		break;
1342 	default:
1343 		ret = -EINVAL;
1344 	}
1345 	return ret;
1346 }
1347 
1348 static int mt9m114_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
1349 {
1350 	struct mt9m114_device *dev =
1351 	    container_of(ctrl->handler, struct mt9m114_device, ctrl_handler);
1352 	int ret = 0;
1353 
1354 	switch (ctrl->id) {
1355 	case V4L2_CID_VFLIP:
1356 		ret = mt9m114_g_vflip(&dev->sd, &ctrl->val);
1357 		break;
1358 	case V4L2_CID_HFLIP:
1359 		ret = mt9m114_g_hflip(&dev->sd, &ctrl->val);
1360 		break;
1361 	case V4L2_CID_FOCAL_ABSOLUTE:
1362 		ret = mt9m114_g_focal(&dev->sd, &ctrl->val);
1363 		break;
1364 	case V4L2_CID_FNUMBER_ABSOLUTE:
1365 		ret = mt9m114_g_fnumber(&dev->sd, &ctrl->val);
1366 		break;
1367 	case V4L2_CID_FNUMBER_RANGE:
1368 		ret = mt9m114_g_fnumber_range(&dev->sd, &ctrl->val);
1369 		break;
1370 	case V4L2_CID_EXPOSURE_ABSOLUTE:
1371 		ret = mt9m114_g_exposure(&dev->sd, &ctrl->val);
1372 		break;
1373 	case V4L2_CID_EXPOSURE_ZONE_NUM:
1374 		ret = mt9m114_g_exposure_zone_num(&dev->sd, &ctrl->val);
1375 		break;
1376 	case V4L2_CID_BIN_FACTOR_HORZ:
1377 		ret = mt9m114_g_bin_factor_x(&dev->sd, &ctrl->val);
1378 		break;
1379 	case V4L2_CID_BIN_FACTOR_VERT:
1380 		ret = mt9m114_g_bin_factor_y(&dev->sd, &ctrl->val);
1381 		break;
1382 	case V4L2_CID_EXPOSURE:
1383 		ret = mt9m114_g_ev(&dev->sd, &ctrl->val);
1384 		break;
1385 	case V4L2_CID_3A_LOCK:
1386 		ret = mt9m114_g_3a_lock(&dev->sd, &ctrl->val);
1387 		break;
1388 	default:
1389 		ret = -EINVAL;
1390 	}
1391 
1392 	return ret;
1393 }
1394 
1395 static const struct v4l2_ctrl_ops ctrl_ops = {
1396 	.s_ctrl = mt9m114_s_ctrl,
1397 	.g_volatile_ctrl = mt9m114_g_volatile_ctrl
1398 };
1399 
1400 static struct v4l2_ctrl_config mt9m114_controls[] = {
1401 	{
1402 		.ops = &ctrl_ops,
1403 		.id = V4L2_CID_VFLIP,
1404 		.name = "Image v-Flip",
1405 		.type = V4L2_CTRL_TYPE_INTEGER,
1406 		.min = 0,
1407 		.max = 1,
1408 		.step = 1,
1409 		.def = 0,
1410 	},
1411 	{
1412 		.ops = &ctrl_ops,
1413 		.id = V4L2_CID_HFLIP,
1414 		.name = "Image h-Flip",
1415 		.type = V4L2_CTRL_TYPE_INTEGER,
1416 		.min = 0,
1417 		.max = 1,
1418 		.step = 1,
1419 		.def = 0,
1420 	},
1421 	{
1422 		.ops = &ctrl_ops,
1423 		.id = V4L2_CID_FOCAL_ABSOLUTE,
1424 		.name = "focal length",
1425 		.type = V4L2_CTRL_TYPE_INTEGER,
1426 		.min = MT9M114_FOCAL_LENGTH_DEFAULT,
1427 		.max = MT9M114_FOCAL_LENGTH_DEFAULT,
1428 		.step = 1,
1429 		.def = MT9M114_FOCAL_LENGTH_DEFAULT,
1430 		.flags = 0,
1431 	},
1432 	{
1433 		.ops = &ctrl_ops,
1434 		.id = V4L2_CID_FNUMBER_ABSOLUTE,
1435 		.name = "f-number",
1436 		.type = V4L2_CTRL_TYPE_INTEGER,
1437 		.min = MT9M114_F_NUMBER_DEFAULT,
1438 		.max = MT9M114_F_NUMBER_DEFAULT,
1439 		.step = 1,
1440 		.def = MT9M114_F_NUMBER_DEFAULT,
1441 		.flags = 0,
1442 	},
1443 	{
1444 		.ops = &ctrl_ops,
1445 		.id = V4L2_CID_FNUMBER_RANGE,
1446 		.name = "f-number range",
1447 		.type = V4L2_CTRL_TYPE_INTEGER,
1448 		.min = MT9M114_F_NUMBER_RANGE,
1449 		.max = MT9M114_F_NUMBER_RANGE,
1450 		.step = 1,
1451 		.def = MT9M114_F_NUMBER_RANGE,
1452 		.flags = 0,
1453 	},
1454 	{
1455 		.ops = &ctrl_ops,
1456 		.id = V4L2_CID_EXPOSURE_ABSOLUTE,
1457 		.name = "exposure",
1458 		.type = V4L2_CTRL_TYPE_INTEGER,
1459 		.min = 0,
1460 		.max = 0xffff,
1461 		.step = 1,
1462 		.def = 0,
1463 		.flags = 0,
1464 	},
1465 	{
1466 		.ops = &ctrl_ops,
1467 		.id = V4L2_CID_EXPOSURE_ZONE_NUM,
1468 		.name = "one-time exposure zone number",
1469 		.type = V4L2_CTRL_TYPE_INTEGER,
1470 		.min = 0,
1471 		.max = 0xffff,
1472 		.step = 1,
1473 		.def = 0,
1474 		.flags = 0,
1475 	},
1476 	{
1477 		.ops = &ctrl_ops,
1478 		.id = V4L2_CID_EXPOSURE_METERING,
1479 		.name = "metering",
1480 		.type = V4L2_CTRL_TYPE_MENU,
1481 		.min = 0,
1482 		.max = 3,
1483 		.step = 0,
1484 		.def = 1,
1485 		.flags = 0,
1486 	},
1487 	{
1488 		.ops = &ctrl_ops,
1489 		.id = V4L2_CID_BIN_FACTOR_HORZ,
1490 		.name = "horizontal binning factor",
1491 		.type = V4L2_CTRL_TYPE_INTEGER,
1492 		.min = 0,
1493 		.max = MT9M114_BIN_FACTOR_MAX,
1494 		.step = 1,
1495 		.def = 0,
1496 		.flags = 0,
1497 	},
1498 	{
1499 		.ops = &ctrl_ops,
1500 		.id = V4L2_CID_BIN_FACTOR_VERT,
1501 		.name = "vertical binning factor",
1502 		.type = V4L2_CTRL_TYPE_INTEGER,
1503 		.min = 0,
1504 		.max = MT9M114_BIN_FACTOR_MAX,
1505 		.step = 1,
1506 		.def = 0,
1507 		.flags = 0,
1508 	},
1509 	{
1510 		.ops = &ctrl_ops,
1511 		.id = V4L2_CID_EXPOSURE,
1512 		.name = "exposure biasx",
1513 		.type = V4L2_CTRL_TYPE_INTEGER,
1514 		.min = -2,
1515 		.max = 2,
1516 		.step = 1,
1517 		.def = 0,
1518 		.flags = 0,
1519 	},
1520 	{
1521 		.ops = &ctrl_ops,
1522 		.id = V4L2_CID_3A_LOCK,
1523 		.name = "3a lock",
1524 		.type = V4L2_CTRL_TYPE_BITMASK,
1525 		.min = 0,
1526 		.max = V4L2_LOCK_EXPOSURE | V4L2_LOCK_WHITE_BALANCE | V4L2_LOCK_FOCUS,
1527 		.step = 1,
1528 		.def = 0,
1529 		.flags = 0,
1530 	},
1531 };
1532 
1533 static int mt9m114_detect(struct mt9m114_device *dev, struct i2c_client *client)
1534 {
1535 	struct i2c_adapter *adapter = client->adapter;
1536 	u32 retvalue;
1537 
1538 	if (!i2c_check_functionality(adapter, I2C_FUNC_I2C)) {
1539 		dev_err(&client->dev, "%s: i2c error", __func__);
1540 		return -ENODEV;
1541 	}
1542 	mt9m114_read_reg(client, MISENSOR_16BIT, (u32)MT9M114_PID, &retvalue);
1543 	dev->real_model_id = retvalue;
1544 
1545 	if (retvalue != MT9M114_MOD_ID) {
1546 		dev_err(&client->dev, "%s: failed: client->addr = %x\n",
1547 			__func__, client->addr);
1548 		return -ENODEV;
1549 	}
1550 
1551 	return 0;
1552 }
1553 
1554 static int
1555 mt9m114_s_config(struct v4l2_subdev *sd, int irq, void *platform_data)
1556 {
1557 	struct mt9m114_device *dev = to_mt9m114_sensor(sd);
1558 	struct i2c_client *client = v4l2_get_subdevdata(sd);
1559 	int ret;
1560 
1561 	if (!platform_data)
1562 		return -ENODEV;
1563 
1564 	dev->platform_data =
1565 	    (struct camera_sensor_platform_data *)platform_data;
1566 
1567 	ret = power_up(sd);
1568 	if (ret) {
1569 		v4l2_err(client, "mt9m114 power-up err");
1570 		return ret;
1571 	}
1572 
1573 	/* config & detect sensor */
1574 	ret = mt9m114_detect(dev, client);
1575 	if (ret) {
1576 		v4l2_err(client, "mt9m114_detect err s_config.\n");
1577 		goto fail_detect;
1578 	}
1579 
1580 	ret = dev->platform_data->csi_cfg(sd, 1);
1581 	if (ret)
1582 		goto fail_csi_cfg;
1583 
1584 	ret = mt9m114_set_suspend(sd);
1585 	if (ret) {
1586 		v4l2_err(client, "mt9m114 suspend err");
1587 		return ret;
1588 	}
1589 
1590 	ret = power_down(sd);
1591 	if (ret) {
1592 		v4l2_err(client, "mt9m114 power down err");
1593 		return ret;
1594 	}
1595 
1596 	return ret;
1597 
1598 fail_csi_cfg:
1599 	dev->platform_data->csi_cfg(sd, 0);
1600 fail_detect:
1601 	power_down(sd);
1602 	dev_err(&client->dev, "sensor power-gating failed\n");
1603 	return ret;
1604 }
1605 
1606 /* Horizontal flip the image. */
1607 static int mt9m114_t_hflip(struct v4l2_subdev *sd, int value)
1608 {
1609 	struct i2c_client *c = v4l2_get_subdevdata(sd);
1610 	struct mt9m114_device *dev = to_mt9m114_sensor(sd);
1611 	int err;
1612 	/* set for direct mode */
1613 	err = mt9m114_write_reg(c, MISENSOR_16BIT, 0x098E, 0xC850);
1614 	if (value) {
1615 		/* enable H flip ctx A */
1616 		err += misensor_rmw_reg(c, MISENSOR_8BIT, 0xC850, 0x01, 0x01);
1617 		err += misensor_rmw_reg(c, MISENSOR_8BIT, 0xC851, 0x01, 0x01);
1618 		/* ctx B */
1619 		err += misensor_rmw_reg(c, MISENSOR_8BIT, 0xC888, 0x01, 0x01);
1620 		err += misensor_rmw_reg(c, MISENSOR_8BIT, 0xC889, 0x01, 0x01);
1621 
1622 		err += misensor_rmw_reg(c, MISENSOR_16BIT, MISENSOR_READ_MODE,
1623 					MISENSOR_HFLIP_MASK, MISENSOR_FLIP_EN);
1624 
1625 		dev->bpat = MT9M114_BPAT_GRGRBGBG;
1626 	} else {
1627 		/* disable H flip ctx A */
1628 		err += misensor_rmw_reg(c, MISENSOR_8BIT, 0xC850, 0x01, 0x00);
1629 		err += misensor_rmw_reg(c, MISENSOR_8BIT, 0xC851, 0x01, 0x00);
1630 		/* ctx B */
1631 		err += misensor_rmw_reg(c, MISENSOR_8BIT, 0xC888, 0x01, 0x00);
1632 		err += misensor_rmw_reg(c, MISENSOR_8BIT, 0xC889, 0x01, 0x00);
1633 
1634 		err += misensor_rmw_reg(c, MISENSOR_16BIT, MISENSOR_READ_MODE,
1635 					MISENSOR_HFLIP_MASK, MISENSOR_FLIP_DIS);
1636 
1637 		dev->bpat = MT9M114_BPAT_BGBGGRGR;
1638 	}
1639 
1640 	err += mt9m114_write_reg(c, MISENSOR_8BIT, 0x8404, 0x06);
1641 	udelay(10);
1642 
1643 	return !!err;
1644 }
1645 
1646 /* Vertically flip the image */
1647 static int mt9m114_t_vflip(struct v4l2_subdev *sd, int value)
1648 {
1649 	struct i2c_client *c = v4l2_get_subdevdata(sd);
1650 	int err;
1651 	/* set for direct mode */
1652 	err = mt9m114_write_reg(c, MISENSOR_16BIT, 0x098E, 0xC850);
1653 	if (value >= 1) {
1654 		/* enable H flip - ctx A */
1655 		err += misensor_rmw_reg(c, MISENSOR_8BIT, 0xC850, 0x02, 0x01);
1656 		err += misensor_rmw_reg(c, MISENSOR_8BIT, 0xC851, 0x02, 0x01);
1657 		/* ctx B */
1658 		err += misensor_rmw_reg(c, MISENSOR_8BIT, 0xC888, 0x02, 0x01);
1659 		err += misensor_rmw_reg(c, MISENSOR_8BIT, 0xC889, 0x02, 0x01);
1660 
1661 		err += misensor_rmw_reg(c, MISENSOR_16BIT, MISENSOR_READ_MODE,
1662 					MISENSOR_VFLIP_MASK, MISENSOR_FLIP_EN);
1663 	} else {
1664 		/* disable H flip - ctx A */
1665 		err += misensor_rmw_reg(c, MISENSOR_8BIT, 0xC850, 0x02, 0x00);
1666 		err += misensor_rmw_reg(c, MISENSOR_8BIT, 0xC851, 0x02, 0x00);
1667 		/* ctx B */
1668 		err += misensor_rmw_reg(c, MISENSOR_8BIT, 0xC888, 0x02, 0x00);
1669 		err += misensor_rmw_reg(c, MISENSOR_8BIT, 0xC889, 0x02, 0x00);
1670 
1671 		err += misensor_rmw_reg(c, MISENSOR_16BIT, MISENSOR_READ_MODE,
1672 					MISENSOR_VFLIP_MASK, MISENSOR_FLIP_DIS);
1673 	}
1674 
1675 	err += mt9m114_write_reg(c, MISENSOR_8BIT, 0x8404, 0x06);
1676 	udelay(10);
1677 
1678 	return !!err;
1679 }
1680 
1681 static int mt9m114_g_frame_interval(struct v4l2_subdev *sd,
1682 				    struct v4l2_subdev_frame_interval *interval)
1683 {
1684 	struct mt9m114_device *dev = to_mt9m114_sensor(sd);
1685 
1686 	interval->interval.numerator = 1;
1687 	interval->interval.denominator = mt9m114_res[dev->res].fps;
1688 
1689 	return 0;
1690 }
1691 
1692 static int mt9m114_s_stream(struct v4l2_subdev *sd, int enable)
1693 {
1694 	int ret;
1695 	struct i2c_client *c = v4l2_get_subdevdata(sd);
1696 	struct mt9m114_device *dev = to_mt9m114_sensor(sd);
1697 	struct atomisp_exposure exposure;
1698 
1699 	if (enable) {
1700 		ret = mt9m114_write_reg_array(c, mt9m114_chgstat_reg,
1701 					      POST_POLLING);
1702 		if (ret < 0)
1703 			return ret;
1704 
1705 		if (dev->first_exp > MT9M114_MAX_FIRST_EXP) {
1706 			exposure.integration_time[0] = dev->first_exp;
1707 			exposure.gain[0] = dev->first_gain;
1708 			exposure.gain[1] = dev->first_diggain;
1709 			mt9m114_s_exposure(sd, &exposure);
1710 		}
1711 		dev->streamon = 1;
1712 
1713 	} else {
1714 		dev->streamon = 0;
1715 		ret = mt9m114_set_suspend(sd);
1716 	}
1717 
1718 	return ret;
1719 }
1720 
1721 static int mt9m114_enum_mbus_code(struct v4l2_subdev *sd,
1722 				  struct v4l2_subdev_pad_config *cfg,
1723 				  struct v4l2_subdev_mbus_code_enum *code)
1724 {
1725 	if (code->index)
1726 		return -EINVAL;
1727 	code->code = MEDIA_BUS_FMT_SGRBG10_1X10;
1728 
1729 	return 0;
1730 }
1731 
1732 static int mt9m114_enum_frame_size(struct v4l2_subdev *sd,
1733 				   struct v4l2_subdev_pad_config *cfg,
1734 				   struct v4l2_subdev_frame_size_enum *fse)
1735 {
1736 	unsigned int index = fse->index;
1737 
1738 	if (index >= N_RES)
1739 		return -EINVAL;
1740 
1741 	fse->min_width = mt9m114_res[index].width;
1742 	fse->min_height = mt9m114_res[index].height;
1743 	fse->max_width = mt9m114_res[index].width;
1744 	fse->max_height = mt9m114_res[index].height;
1745 
1746 	return 0;
1747 }
1748 
1749 static int mt9m114_g_skip_frames(struct v4l2_subdev *sd, u32 *frames)
1750 {
1751 	int index;
1752 	struct mt9m114_device *snr = to_mt9m114_sensor(sd);
1753 
1754 	if (!frames)
1755 		return -EINVAL;
1756 
1757 	for (index = 0; index < N_RES; index++) {
1758 		if (mt9m114_res[index].res == snr->res)
1759 			break;
1760 	}
1761 
1762 	if (index >= N_RES)
1763 		return -EINVAL;
1764 
1765 	*frames = mt9m114_res[index].skip_frames;
1766 
1767 	return 0;
1768 }
1769 
1770 static const struct v4l2_subdev_video_ops mt9m114_video_ops = {
1771 	.s_stream = mt9m114_s_stream,
1772 	.g_frame_interval = mt9m114_g_frame_interval,
1773 };
1774 
1775 static const struct v4l2_subdev_sensor_ops mt9m114_sensor_ops = {
1776 	.g_skip_frames	= mt9m114_g_skip_frames,
1777 };
1778 
1779 static const struct v4l2_subdev_core_ops mt9m114_core_ops = {
1780 	.s_power = mt9m114_s_power,
1781 	.ioctl = mt9m114_ioctl,
1782 };
1783 
1784 /* REVISIT: Do we need pad operations? */
1785 static const struct v4l2_subdev_pad_ops mt9m114_pad_ops = {
1786 	.enum_mbus_code = mt9m114_enum_mbus_code,
1787 	.enum_frame_size = mt9m114_enum_frame_size,
1788 	.get_fmt = mt9m114_get_fmt,
1789 	.set_fmt = mt9m114_set_fmt,
1790 	.set_selection = mt9m114_s_exposure_selection,
1791 };
1792 
1793 static const struct v4l2_subdev_ops mt9m114_ops = {
1794 	.core = &mt9m114_core_ops,
1795 	.video = &mt9m114_video_ops,
1796 	.pad = &mt9m114_pad_ops,
1797 	.sensor = &mt9m114_sensor_ops,
1798 };
1799 
1800 static int mt9m114_remove(struct i2c_client *client)
1801 {
1802 	struct mt9m114_device *dev;
1803 	struct v4l2_subdev *sd = i2c_get_clientdata(client);
1804 
1805 	dev = container_of(sd, struct mt9m114_device, sd);
1806 	dev->platform_data->csi_cfg(sd, 0);
1807 	v4l2_device_unregister_subdev(sd);
1808 	media_entity_cleanup(&dev->sd.entity);
1809 	v4l2_ctrl_handler_free(&dev->ctrl_handler);
1810 	kfree(dev);
1811 	return 0;
1812 }
1813 
1814 static int mt9m114_probe(struct i2c_client *client)
1815 {
1816 	struct mt9m114_device *dev;
1817 	int ret = 0;
1818 	unsigned int i;
1819 	void *pdata;
1820 
1821 	/* Setup sensor configuration structure */
1822 	dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1823 	if (!dev)
1824 		return -ENOMEM;
1825 
1826 	v4l2_i2c_subdev_init(&dev->sd, client, &mt9m114_ops);
1827 	pdata = gmin_camera_platform_data(&dev->sd,
1828 					  ATOMISP_INPUT_FORMAT_RAW_10,
1829 					  atomisp_bayer_order_grbg);
1830 	if (pdata)
1831 		ret = mt9m114_s_config(&dev->sd, client->irq, pdata);
1832 	if (!pdata || ret) {
1833 		v4l2_device_unregister_subdev(&dev->sd);
1834 		kfree(dev);
1835 		return ret;
1836 	}
1837 
1838 	ret = atomisp_register_i2c_module(&dev->sd, pdata, RAW_CAMERA);
1839 	if (ret) {
1840 		v4l2_device_unregister_subdev(&dev->sd);
1841 		kfree(dev);
1842 		/* Coverity CID 298095 - return on error */
1843 		return ret;
1844 	}
1845 
1846 	/*TODO add format code here*/
1847 	dev->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
1848 	dev->pad.flags = MEDIA_PAD_FL_SOURCE;
1849 	dev->format.code = MEDIA_BUS_FMT_SGRBG10_1X10;
1850 	dev->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
1851 
1852 	ret =
1853 	    v4l2_ctrl_handler_init(&dev->ctrl_handler,
1854 				   ARRAY_SIZE(mt9m114_controls));
1855 	if (ret) {
1856 		mt9m114_remove(client);
1857 		return ret;
1858 	}
1859 
1860 	for (i = 0; i < ARRAY_SIZE(mt9m114_controls); i++)
1861 		v4l2_ctrl_new_custom(&dev->ctrl_handler, &mt9m114_controls[i],
1862 				     NULL);
1863 
1864 	if (dev->ctrl_handler.error) {
1865 		mt9m114_remove(client);
1866 		return dev->ctrl_handler.error;
1867 	}
1868 
1869 	/* Use same lock for controls as for everything else. */
1870 	dev->ctrl_handler.lock = &dev->input_lock;
1871 	dev->sd.ctrl_handler = &dev->ctrl_handler;
1872 
1873 	/* REVISIT: Do we need media controller? */
1874 	ret = media_entity_pads_init(&dev->sd.entity, 1, &dev->pad);
1875 	if (ret) {
1876 		mt9m114_remove(client);
1877 		return ret;
1878 	}
1879 	return 0;
1880 }
1881 
1882 static const struct acpi_device_id mt9m114_acpi_match[] = {
1883 	{ "INT33F0" },
1884 	{ "CRMT1040" },
1885 	{},
1886 };
1887 MODULE_DEVICE_TABLE(acpi, mt9m114_acpi_match);
1888 
1889 static struct i2c_driver mt9m114_driver = {
1890 	.driver = {
1891 		.name = "mt9m114",
1892 		.acpi_match_table = mt9m114_acpi_match,
1893 	},
1894 	.probe_new = mt9m114_probe,
1895 	.remove = mt9m114_remove,
1896 };
1897 module_i2c_driver(mt9m114_driver);
1898 
1899 MODULE_AUTHOR("Shuguang Gong <Shuguang.gong@intel.com>");
1900 MODULE_LICENSE("GPL");
1901