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