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(s) container_of(s, 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 
mt9m114_res2size(struct v4l2_subdev * sd,int * h_size,int * v_size)582 static int mt9m114_res2size(struct v4l2_subdev *sd, int *h_size, int *v_size)
583 {
584 	struct mt9m114_device *dev = to_mt9m114_sensor(sd);
585 	unsigned short hsize;
586 	unsigned short vsize;
587 
588 	switch (dev->res) {
589 	case MT9M114_RES_736P:
590 		hsize = MT9M114_RES_736P_SIZE_H;
591 		vsize = MT9M114_RES_736P_SIZE_V;
592 		break;
593 	case MT9M114_RES_864P:
594 		hsize = MT9M114_RES_864P_SIZE_H;
595 		vsize = MT9M114_RES_864P_SIZE_V;
596 		break;
597 	case MT9M114_RES_960P:
598 		hsize = MT9M114_RES_960P_SIZE_H;
599 		vsize = MT9M114_RES_960P_SIZE_V;
600 		break;
601 	default:
602 		v4l2_err(sd, "%s: Resolution 0x%08x unknown\n", __func__,
603 			 dev->res);
604 		return -EINVAL;
605 	}
606 
607 	if (h_size)
608 		*h_size = hsize;
609 	if (v_size)
610 		*v_size = vsize;
611 
612 	return 0;
613 }
614 
mt9m114_get_fmt(struct v4l2_subdev * sd,struct v4l2_subdev_state * sd_state,struct v4l2_subdev_format * format)615 static int mt9m114_get_fmt(struct v4l2_subdev *sd,
616 			   struct v4l2_subdev_state *sd_state,
617 			   struct v4l2_subdev_format *format)
618 {
619 	struct v4l2_mbus_framefmt *fmt = &format->format;
620 	int width, height;
621 	int ret;
622 
623 	if (format->pad)
624 		return -EINVAL;
625 	fmt->code = MEDIA_BUS_FMT_SGRBG10_1X10;
626 
627 	ret = mt9m114_res2size(sd, &width, &height);
628 	if (ret)
629 		return ret;
630 	fmt->width = width;
631 	fmt->height = height;
632 
633 	return 0;
634 }
635 
mt9m114_set_fmt(struct v4l2_subdev * sd,struct v4l2_subdev_state * sd_state,struct v4l2_subdev_format * format)636 static int mt9m114_set_fmt(struct v4l2_subdev *sd,
637 			   struct v4l2_subdev_state *sd_state,
638 			   struct v4l2_subdev_format *format)
639 {
640 	struct v4l2_mbus_framefmt *fmt = &format->format;
641 	struct i2c_client *c = v4l2_get_subdevdata(sd);
642 	struct mt9m114_device *dev = to_mt9m114_sensor(sd);
643 	struct mt9m114_res_struct *res;
644 	u32 width = fmt->width;
645 	u32 height = fmt->height;
646 	struct camera_mipi_info *mt9m114_info = NULL;
647 
648 	int ret;
649 
650 	if (format->pad)
651 		return -EINVAL;
652 	dev->streamon = 0;
653 	dev->first_exp = MT9M114_DEFAULT_FIRST_EXP;
654 
655 	mt9m114_info = v4l2_get_subdev_hostdata(sd);
656 	if (!mt9m114_info)
657 		return -EINVAL;
658 
659 	res = v4l2_find_nearest_size(mt9m114_res,
660 				     ARRAY_SIZE(mt9m114_res), width,
661 				     height, fmt->width, fmt->height);
662 	if (!res)
663 		res = &mt9m114_res[N_RES - 1];
664 
665 	fmt->width = res->width;
666 	fmt->height = res->height;
667 
668 	if (format->which == V4L2_SUBDEV_FORMAT_TRY) {
669 		*v4l2_subdev_state_get_format(sd_state, 0) = *fmt;
670 		return 0;
671 	}
672 
673 	switch (res->res) {
674 	case MT9M114_RES_736P:
675 		ret = mt9m114_write_reg_array(c, mt9m114_736P_init, NO_POLLING);
676 		ret += misensor_rmw_reg(c, MISENSOR_16BIT, MISENSOR_READ_MODE,
677 					MISENSOR_R_MODE_MASK, MISENSOR_NORMAL_SET);
678 		break;
679 	case MT9M114_RES_864P:
680 		ret = mt9m114_write_reg_array(c, mt9m114_864P_init, NO_POLLING);
681 		ret += misensor_rmw_reg(c, MISENSOR_16BIT, MISENSOR_READ_MODE,
682 					MISENSOR_R_MODE_MASK, MISENSOR_NORMAL_SET);
683 		break;
684 	case MT9M114_RES_960P:
685 		ret = mt9m114_write_reg_array(c, mt9m114_976P_init, NO_POLLING);
686 		/* set sensor read_mode to Normal */
687 		ret += misensor_rmw_reg(c, MISENSOR_16BIT, MISENSOR_READ_MODE,
688 					MISENSOR_R_MODE_MASK, MISENSOR_NORMAL_SET);
689 		break;
690 	default:
691 		v4l2_err(sd, "set resolution: %d failed!\n", res->res);
692 		return -EINVAL;
693 	}
694 
695 	if (ret)
696 		return -EINVAL;
697 
698 	ret = mt9m114_write_reg_array(c, mt9m114_chgstat_reg, POST_POLLING);
699 	if (ret < 0)
700 		return ret;
701 
702 	if (mt9m114_set_suspend(sd))
703 		return -EINVAL;
704 
705 	if (dev->res != res->res) {
706 		int index;
707 
708 		/* Switch to different size */
709 		if (width <= 640) {
710 			dev->nctx = 0x00; /* Set for context A */
711 		} else {
712 			/*
713 			 * Context B is used for resolutions larger than 640x480
714 			 * Using YUV for Context B.
715 			 */
716 			dev->nctx = 0x01; /* set for context B */
717 		}
718 
719 		/*
720 		 * Marked current sensor res as being "used"
721 		 *
722 		 * REVISIT: We don't need to use an "used" field on each mode
723 		 * list entry to know which mode is selected. If this
724 		 * information is really necessary, how about to use a single
725 		 * variable on sensor dev struct?
726 		 */
727 		for (index = 0; index < N_RES; index++) {
728 			if ((width == mt9m114_res[index].width) &&
729 			    (height == mt9m114_res[index].height)) {
730 				mt9m114_res[index].used = true;
731 				continue;
732 			}
733 			mt9m114_res[index].used = false;
734 		}
735 	}
736 	/*
737 	 * mt9m114 - we don't poll for context switch
738 	 * because it does not happen with streaming disabled.
739 	 */
740 	dev->res = res->res;
741 
742 	fmt->width = width;
743 	fmt->height = height;
744 	fmt->code = MEDIA_BUS_FMT_SGRBG10_1X10;
745 	return 0;
746 }
747 
748 /* Horizontal flip the image. */
mt9m114_g_hflip(struct v4l2_subdev * sd,s32 * val)749 static int mt9m114_g_hflip(struct v4l2_subdev *sd, s32 *val)
750 {
751 	struct i2c_client *c = v4l2_get_subdevdata(sd);
752 	int ret;
753 	u32 data;
754 
755 	ret = mt9m114_read_reg(c, MISENSOR_16BIT,
756 			       (u32)MISENSOR_READ_MODE, &data);
757 	if (ret)
758 		return ret;
759 	*val = !!(data & MISENSOR_HFLIP_MASK);
760 
761 	return 0;
762 }
763 
mt9m114_g_vflip(struct v4l2_subdev * sd,s32 * val)764 static int mt9m114_g_vflip(struct v4l2_subdev *sd, s32 *val)
765 {
766 	struct i2c_client *c = v4l2_get_subdevdata(sd);
767 	int ret;
768 	u32 data;
769 
770 	ret = mt9m114_read_reg(c, MISENSOR_16BIT,
771 			       (u32)MISENSOR_READ_MODE, &data);
772 	if (ret)
773 		return ret;
774 	*val = !!(data & MISENSOR_VFLIP_MASK);
775 
776 	return 0;
777 }
778 
mt9m114_s_exposure(struct v4l2_subdev * sd,struct atomisp_exposure * exposure)779 static long mt9m114_s_exposure(struct v4l2_subdev *sd,
780 			       struct atomisp_exposure *exposure)
781 {
782 	struct i2c_client *client = v4l2_get_subdevdata(sd);
783 	struct mt9m114_device *dev = to_mt9m114_sensor(sd);
784 	int ret = 0;
785 	unsigned int coarse_integration = 0;
786 	unsigned int f_lines = 0;
787 	unsigned int frame_len_lines = 0; /* ExposureTime.FrameLengthLines; */
788 	unsigned int analog_gain, digital_gain;
789 	u32 analog_gain_to_write = 0;
790 
791 	dev_dbg(&client->dev, "%s(0x%X 0x%X 0x%X)\n", __func__,
792 		exposure->integration_time[0], exposure->gain[0],
793 		exposure->gain[1]);
794 
795 	coarse_integration = exposure->integration_time[0];
796 	/*
797 	 * fine_integration = ExposureTime.FineIntegrationTime;
798 	 * frame_len_lines = ExposureTime.FrameLengthLines;
799 	 */
800 	f_lines = mt9m114_res[dev->res].lines_per_frame;
801 	analog_gain = exposure->gain[0];
802 	digital_gain = exposure->gain[1];
803 	if (!dev->streamon) {
804 		/*Save the first exposure values while stream is off*/
805 		dev->first_exp = coarse_integration;
806 		dev->first_gain = analog_gain;
807 		dev->first_diggain = digital_gain;
808 	}
809 	/* digital_gain = 0x400 * (((u16) digital_gain) >> 8) +		*/
810 	/* ((unsigned int)(0x400 * (((u16) digital_gain) & 0xFF)) >>8); */
811 
812 	/* set frame length */
813 	if (f_lines < coarse_integration + 6)
814 		f_lines = coarse_integration + 6;
815 	if (f_lines < frame_len_lines)
816 		f_lines = frame_len_lines;
817 	ret = mt9m114_write_reg(client, MISENSOR_16BIT, 0x300A, f_lines);
818 	if (ret) {
819 		v4l2_err(client, "%s: fail to set f_lines\n", __func__);
820 		return -EINVAL;
821 	}
822 
823 	/* set coarse integration */
824 	/*
825 	 * 3A provide real exposure time.
826 	 * should not translate to any value here.
827 	 */
828 	ret = mt9m114_write_reg(client, MISENSOR_16BIT,
829 				REG_EXPO_COARSE, (u16)(coarse_integration));
830 	if (ret) {
831 		v4l2_err(client, "%s: fail to set exposure time\n", __func__);
832 		return -EINVAL;
833 	}
834 
835 	/*
836 	 * set analog/digital gain
837 	switch(analog_gain)
838 	{
839 	case 0:
840 	  analog_gain_to_write = 0x0;
841 	  break;
842 	case 1:
843 	  analog_gain_to_write = 0x20;
844 	  break;
845 	case 2:
846 	  analog_gain_to_write = 0x60;
847 	  break;
848 	case 4:
849 	  analog_gain_to_write = 0xA0;
850 	  break;
851 	case 8:
852 	  analog_gain_to_write = 0xE0;
853 	  break;
854 	default:
855 	  analog_gain_to_write = 0x20;
856 	  break;
857 	}
858 	*/
859 	if (digital_gain >= 16 || digital_gain <= 1)
860 		digital_gain = 1;
861 	/*
862 	 * analog_gain_to_write = (u16)((digital_gain << 12)
863 	 *				| analog_gain_to_write);
864 	 */
865 	analog_gain_to_write = (u16)((digital_gain << 12) | (u16)analog_gain);
866 	ret = mt9m114_write_reg(client, MISENSOR_16BIT,
867 				REG_GAIN, analog_gain_to_write);
868 	if (ret) {
869 		v4l2_err(client, "%s: fail to set analog_gain_to_write\n",
870 			 __func__);
871 		return -EINVAL;
872 	}
873 
874 	return ret;
875 }
876 
mt9m114_ioctl(struct v4l2_subdev * sd,unsigned int cmd,void * arg)877 static long mt9m114_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg)
878 {
879 	switch (cmd) {
880 	case ATOMISP_IOC_S_EXPOSURE:
881 		return mt9m114_s_exposure(sd, arg);
882 	default:
883 		return -EINVAL;
884 	}
885 
886 	return 0;
887 }
888 
889 /*
890  * This returns the exposure time being used. This should only be used
891  * for filling in EXIF data, not for actual image processing.
892  */
mt9m114_g_exposure(struct v4l2_subdev * sd,s32 * value)893 static int mt9m114_g_exposure(struct v4l2_subdev *sd, s32 *value)
894 {
895 	struct i2c_client *client = v4l2_get_subdevdata(sd);
896 	u32 coarse;
897 	int ret;
898 
899 	/* the fine integration time is currently not calculated */
900 	ret = mt9m114_read_reg(client, MISENSOR_16BIT,
901 			       REG_EXPO_COARSE, &coarse);
902 	if (ret)
903 		return ret;
904 
905 	*value = coarse;
906 	return 0;
907 }
908 
909 /*
910  * This function will return the sensor supported max exposure zone number.
911  * the sensor which supports max exposure zone number is 1.
912  */
mt9m114_g_exposure_zone_num(struct v4l2_subdev * sd,s32 * val)913 static int mt9m114_g_exposure_zone_num(struct v4l2_subdev *sd, s32 *val)
914 {
915 	*val = 1;
916 
917 	return 0;
918 }
919 
920 /*
921  * set exposure metering, average/center_weighted/spot/matrix.
922  */
mt9m114_s_exposure_metering(struct v4l2_subdev * sd,s32 val)923 static int mt9m114_s_exposure_metering(struct v4l2_subdev *sd, s32 val)
924 {
925 	struct i2c_client *client = v4l2_get_subdevdata(sd);
926 	int ret;
927 
928 	switch (val) {
929 	case V4L2_EXPOSURE_METERING_SPOT:
930 		ret = mt9m114_write_reg_array(client, mt9m114_exp_average,
931 					      NO_POLLING);
932 		if (ret) {
933 			dev_err(&client->dev, "write exp_average reg err.\n");
934 			return ret;
935 		}
936 		break;
937 	case V4L2_EXPOSURE_METERING_CENTER_WEIGHTED:
938 	default:
939 		ret = mt9m114_write_reg_array(client, mt9m114_exp_center,
940 					      NO_POLLING);
941 		if (ret) {
942 			dev_err(&client->dev, "write exp_default reg err");
943 			return ret;
944 		}
945 	}
946 
947 	return 0;
948 }
949 
950 /*
951  * This function is for touch exposure feature.
952  */
mt9m114_s_exposure_selection(struct v4l2_subdev * sd,struct v4l2_subdev_state * sd_state,struct v4l2_subdev_selection * sel)953 static int mt9m114_s_exposure_selection(struct v4l2_subdev *sd,
954 					struct v4l2_subdev_state *sd_state,
955 					struct v4l2_subdev_selection *sel)
956 {
957 	struct i2c_client *client = v4l2_get_subdevdata(sd);
958 	struct misensor_reg exp_reg;
959 	int width, height;
960 	int grid_width, grid_height;
961 	int grid_left, grid_top, grid_right, grid_bottom;
962 	int win_left, win_top, win_right, win_bottom;
963 	int i, j;
964 	int ret;
965 
966 	if (sel->which != V4L2_SUBDEV_FORMAT_TRY &&
967 	    sel->which != V4L2_SUBDEV_FORMAT_ACTIVE)
968 		return -EINVAL;
969 
970 	grid_left = sel->r.left;
971 	grid_top = sel->r.top;
972 	grid_right = sel->r.left + sel->r.width - 1;
973 	grid_bottom = sel->r.top + sel->r.height - 1;
974 
975 	ret = mt9m114_res2size(sd, &width, &height);
976 	if (ret)
977 		return ret;
978 
979 	grid_width = width / 5;
980 	grid_height = height / 5;
981 
982 	if (grid_width && grid_height) {
983 		win_left = grid_left / grid_width;
984 		win_top = grid_top / grid_height;
985 		win_right = grid_right / grid_width;
986 		win_bottom = grid_bottom / grid_height;
987 	} else {
988 		dev_err(&client->dev, "Incorrect exp grid.\n");
989 		return -EINVAL;
990 	}
991 
992 	win_left   = clamp_t(int, win_left, 0, 4);
993 	win_top    = clamp_t(int, win_top, 0, 4);
994 	win_right  = clamp_t(int, win_right, 0, 4);
995 	win_bottom = clamp_t(int, win_bottom, 0, 4);
996 
997 	ret = mt9m114_write_reg_array(client, mt9m114_exp_average, NO_POLLING);
998 	if (ret) {
999 		dev_err(&client->dev, "write exp_average reg err.\n");
1000 		return ret;
1001 	}
1002 
1003 	for (i = win_top; i <= win_bottom; i++) {
1004 		for (j = win_left; j <= win_right; j++) {
1005 			exp_reg = mt9m114_exp_win[i][j];
1006 
1007 			ret = mt9m114_write_reg(client, exp_reg.length,
1008 						exp_reg.reg, exp_reg.val);
1009 			if (ret) {
1010 				dev_err(&client->dev, "write exp_reg err.\n");
1011 				return ret;
1012 			}
1013 		}
1014 	}
1015 
1016 	return 0;
1017 }
1018 
mt9m114_s_ev(struct v4l2_subdev * sd,s32 val)1019 static int mt9m114_s_ev(struct v4l2_subdev *sd, s32 val)
1020 {
1021 	struct i2c_client *c = v4l2_get_subdevdata(sd);
1022 	s32 luma = 0x37;
1023 	int err;
1024 
1025 	/*
1026 	 * EV value only support -2 to 2
1027 	 * 0: 0x37, 1:0x47, 2:0x57, -1:0x27, -2:0x17
1028 	 */
1029 	if (val < -2 || val > 2)
1030 		return -EINVAL;
1031 	luma += 0x10 * val;
1032 	dev_dbg(&c->dev, "%s val:%d luma:0x%x\n", __func__, val, luma);
1033 	err = mt9m114_write_reg(c, MISENSOR_16BIT, 0x098E, 0xC87A);
1034 	if (err) {
1035 		dev_err(&c->dev, "%s logic addr access error\n", __func__);
1036 		return err;
1037 	}
1038 	err = mt9m114_write_reg(c, MISENSOR_8BIT, 0xC87A, (u32)luma);
1039 	if (err) {
1040 		dev_err(&c->dev, "%s write target_average_luma failed\n",
1041 			__func__);
1042 		return err;
1043 	}
1044 	udelay(10);
1045 
1046 	return 0;
1047 }
1048 
mt9m114_g_ev(struct v4l2_subdev * sd,s32 * val)1049 static int mt9m114_g_ev(struct v4l2_subdev *sd, s32 *val)
1050 {
1051 	struct i2c_client *c = v4l2_get_subdevdata(sd);
1052 	int err;
1053 	u32 luma;
1054 
1055 	err = mt9m114_write_reg(c, MISENSOR_16BIT, 0x098E, 0xC87A);
1056 	if (err) {
1057 		dev_err(&c->dev, "%s logic addr access error\n", __func__);
1058 		return err;
1059 	}
1060 	err = mt9m114_read_reg(c, MISENSOR_8BIT, 0xC87A, &luma);
1061 	if (err) {
1062 		dev_err(&c->dev, "%s read target_average_luma failed\n",
1063 			__func__);
1064 		return err;
1065 	}
1066 	luma -= 0x17;
1067 	luma /= 0x10;
1068 	*val = (s32)luma - 2;
1069 	dev_dbg(&c->dev, "%s val:%d\n", __func__, *val);
1070 
1071 	return 0;
1072 }
1073 
1074 /*
1075  * Fake interface
1076  * mt9m114 now can not support 3a_lock
1077  */
mt9m114_s_3a_lock(struct v4l2_subdev * sd,s32 val)1078 static int mt9m114_s_3a_lock(struct v4l2_subdev *sd, s32 val)
1079 {
1080 	aaalock = val;
1081 	return 0;
1082 }
1083 
mt9m114_g_3a_lock(struct v4l2_subdev * sd,s32 * val)1084 static int mt9m114_g_3a_lock(struct v4l2_subdev *sd, s32 *val)
1085 {
1086 	if (aaalock)
1087 		return V4L2_LOCK_EXPOSURE | V4L2_LOCK_WHITE_BALANCE
1088 		       | V4L2_LOCK_FOCUS;
1089 	return 0;
1090 }
1091 
mt9m114_s_ctrl(struct v4l2_ctrl * ctrl)1092 static int mt9m114_s_ctrl(struct v4l2_ctrl *ctrl)
1093 {
1094 	struct mt9m114_device *dev =
1095 	    container_of(ctrl->handler, struct mt9m114_device, ctrl_handler);
1096 	struct i2c_client *client = v4l2_get_subdevdata(&dev->sd);
1097 	int ret = 0;
1098 
1099 	switch (ctrl->id) {
1100 	case V4L2_CID_VFLIP:
1101 		dev_dbg(&client->dev, "%s: CID_VFLIP:%d.\n",
1102 			__func__, ctrl->val);
1103 		ret = mt9m114_t_vflip(&dev->sd, ctrl->val);
1104 		break;
1105 	case V4L2_CID_HFLIP:
1106 		dev_dbg(&client->dev, "%s: CID_HFLIP:%d.\n",
1107 			__func__, ctrl->val);
1108 		ret = mt9m114_t_hflip(&dev->sd, ctrl->val);
1109 		break;
1110 	case V4L2_CID_EXPOSURE_METERING:
1111 		ret = mt9m114_s_exposure_metering(&dev->sd, ctrl->val);
1112 		break;
1113 	case V4L2_CID_EXPOSURE:
1114 		ret = mt9m114_s_ev(&dev->sd, ctrl->val);
1115 		break;
1116 	case V4L2_CID_3A_LOCK:
1117 		ret = mt9m114_s_3a_lock(&dev->sd, ctrl->val);
1118 		break;
1119 	default:
1120 		ret = -EINVAL;
1121 	}
1122 	return ret;
1123 }
1124 
mt9m114_g_volatile_ctrl(struct v4l2_ctrl * ctrl)1125 static int mt9m114_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
1126 {
1127 	struct mt9m114_device *dev =
1128 	    container_of(ctrl->handler, struct mt9m114_device, ctrl_handler);
1129 	int ret = 0;
1130 
1131 	switch (ctrl->id) {
1132 	case V4L2_CID_VFLIP:
1133 		ret = mt9m114_g_vflip(&dev->sd, &ctrl->val);
1134 		break;
1135 	case V4L2_CID_HFLIP:
1136 		ret = mt9m114_g_hflip(&dev->sd, &ctrl->val);
1137 		break;
1138 	case V4L2_CID_EXPOSURE_ABSOLUTE:
1139 		ret = mt9m114_g_exposure(&dev->sd, &ctrl->val);
1140 		break;
1141 	case V4L2_CID_EXPOSURE_ZONE_NUM:
1142 		ret = mt9m114_g_exposure_zone_num(&dev->sd, &ctrl->val);
1143 		break;
1144 	case V4L2_CID_EXPOSURE:
1145 		ret = mt9m114_g_ev(&dev->sd, &ctrl->val);
1146 		break;
1147 	case V4L2_CID_3A_LOCK:
1148 		ret = mt9m114_g_3a_lock(&dev->sd, &ctrl->val);
1149 		break;
1150 	default:
1151 		ret = -EINVAL;
1152 	}
1153 
1154 	return ret;
1155 }
1156 
1157 static const struct v4l2_ctrl_ops ctrl_ops = {
1158 	.s_ctrl = mt9m114_s_ctrl,
1159 	.g_volatile_ctrl = mt9m114_g_volatile_ctrl
1160 };
1161 
1162 static struct v4l2_ctrl_config mt9m114_controls[] = {
1163 	{
1164 		.ops = &ctrl_ops,
1165 		.id = V4L2_CID_VFLIP,
1166 		.name = "Image v-Flip",
1167 		.type = V4L2_CTRL_TYPE_INTEGER,
1168 		.min = 0,
1169 		.max = 1,
1170 		.step = 1,
1171 		.def = 0,
1172 	},
1173 	{
1174 		.ops = &ctrl_ops,
1175 		.id = V4L2_CID_HFLIP,
1176 		.name = "Image h-Flip",
1177 		.type = V4L2_CTRL_TYPE_INTEGER,
1178 		.min = 0,
1179 		.max = 1,
1180 		.step = 1,
1181 		.def = 0,
1182 	},
1183 	{
1184 		.ops = &ctrl_ops,
1185 		.id = V4L2_CID_EXPOSURE_ABSOLUTE,
1186 		.name = "exposure",
1187 		.type = V4L2_CTRL_TYPE_INTEGER,
1188 		.min = 0,
1189 		.max = 0xffff,
1190 		.step = 1,
1191 		.def = 0,
1192 		.flags = 0,
1193 	},
1194 	{
1195 		.ops = &ctrl_ops,
1196 		.id = V4L2_CID_EXPOSURE_ZONE_NUM,
1197 		.name = "one-time exposure zone number",
1198 		.type = V4L2_CTRL_TYPE_INTEGER,
1199 		.min = 0,
1200 		.max = 0xffff,
1201 		.step = 1,
1202 		.def = 0,
1203 		.flags = 0,
1204 	},
1205 	{
1206 		.ops = &ctrl_ops,
1207 		.id = V4L2_CID_EXPOSURE_METERING,
1208 		.name = "metering",
1209 		.type = V4L2_CTRL_TYPE_MENU,
1210 		.min = 0,
1211 		.max = 3,
1212 		.step = 0,
1213 		.def = 1,
1214 		.flags = 0,
1215 	},
1216 	{
1217 		.ops = &ctrl_ops,
1218 		.id = V4L2_CID_EXPOSURE,
1219 		.name = "exposure biasx",
1220 		.type = V4L2_CTRL_TYPE_INTEGER,
1221 		.min = -2,
1222 		.max = 2,
1223 		.step = 1,
1224 		.def = 0,
1225 		.flags = 0,
1226 	},
1227 	{
1228 		.ops = &ctrl_ops,
1229 		.id = V4L2_CID_3A_LOCK,
1230 		.name = "3a lock",
1231 		.type = V4L2_CTRL_TYPE_BITMASK,
1232 		.min = 0,
1233 		.max = V4L2_LOCK_EXPOSURE | V4L2_LOCK_WHITE_BALANCE | V4L2_LOCK_FOCUS,
1234 		.step = 1,
1235 		.def = 0,
1236 		.flags = 0,
1237 	},
1238 };
1239 
mt9m114_detect(struct mt9m114_device * dev,struct i2c_client * client)1240 static int mt9m114_detect(struct mt9m114_device *dev, struct i2c_client *client)
1241 {
1242 	struct i2c_adapter *adapter = client->adapter;
1243 	u32 model;
1244 	int ret;
1245 
1246 	if (!i2c_check_functionality(adapter, I2C_FUNC_I2C)) {
1247 		dev_err(&client->dev, "%s: i2c error", __func__);
1248 		return -ENODEV;
1249 	}
1250 	ret = mt9m114_read_reg(client, MISENSOR_16BIT, MT9M114_PID, &model);
1251 	if (ret)
1252 		return ret;
1253 	dev->real_model_id = model;
1254 
1255 	if (model != MT9M114_MOD_ID) {
1256 		dev_err(&client->dev, "%s: failed: client->addr = %x\n",
1257 			__func__, client->addr);
1258 		return -ENODEV;
1259 	}
1260 
1261 	return 0;
1262 }
1263 
1264 static int
mt9m114_s_config(struct v4l2_subdev * sd,int irq,void * platform_data)1265 mt9m114_s_config(struct v4l2_subdev *sd, int irq, void *platform_data)
1266 {
1267 	struct mt9m114_device *dev = to_mt9m114_sensor(sd);
1268 	struct i2c_client *client = v4l2_get_subdevdata(sd);
1269 	int ret;
1270 
1271 	if (!platform_data)
1272 		return -ENODEV;
1273 
1274 	dev->platform_data =
1275 	    (struct camera_sensor_platform_data *)platform_data;
1276 
1277 	ret = power_up(sd);
1278 	if (ret) {
1279 		v4l2_err(client, "mt9m114 power-up err");
1280 		return ret;
1281 	}
1282 
1283 	/* config & detect sensor */
1284 	ret = mt9m114_detect(dev, client);
1285 	if (ret) {
1286 		v4l2_err(client, "mt9m114_detect err s_config.\n");
1287 		goto fail_detect;
1288 	}
1289 
1290 	ret = dev->platform_data->csi_cfg(sd, 1);
1291 	if (ret)
1292 		goto fail_csi_cfg;
1293 
1294 	ret = mt9m114_set_suspend(sd);
1295 	if (ret) {
1296 		v4l2_err(client, "mt9m114 suspend err");
1297 		return ret;
1298 	}
1299 
1300 	ret = power_down(sd);
1301 	if (ret) {
1302 		v4l2_err(client, "mt9m114 power down err");
1303 		return ret;
1304 	}
1305 
1306 	return ret;
1307 
1308 fail_csi_cfg:
1309 	dev->platform_data->csi_cfg(sd, 0);
1310 fail_detect:
1311 	power_down(sd);
1312 	dev_err(&client->dev, "sensor power-gating failed\n");
1313 	return ret;
1314 }
1315 
1316 /* Horizontal flip the image. */
mt9m114_t_hflip(struct v4l2_subdev * sd,int value)1317 static int mt9m114_t_hflip(struct v4l2_subdev *sd, int value)
1318 {
1319 	struct i2c_client *c = v4l2_get_subdevdata(sd);
1320 	struct mt9m114_device *dev = to_mt9m114_sensor(sd);
1321 	int err;
1322 	/* set for direct mode */
1323 	err = mt9m114_write_reg(c, MISENSOR_16BIT, 0x098E, 0xC850);
1324 	if (value) {
1325 		/* enable H flip ctx A */
1326 		err += misensor_rmw_reg(c, MISENSOR_8BIT, 0xC850, 0x01, 0x01);
1327 		err += misensor_rmw_reg(c, MISENSOR_8BIT, 0xC851, 0x01, 0x01);
1328 		/* ctx B */
1329 		err += misensor_rmw_reg(c, MISENSOR_8BIT, 0xC888, 0x01, 0x01);
1330 		err += misensor_rmw_reg(c, MISENSOR_8BIT, 0xC889, 0x01, 0x01);
1331 
1332 		err += misensor_rmw_reg(c, MISENSOR_16BIT, MISENSOR_READ_MODE,
1333 					MISENSOR_HFLIP_MASK, MISENSOR_FLIP_EN);
1334 
1335 		dev->bpat = MT9M114_BPAT_GRGRBGBG;
1336 	} else {
1337 		/* disable H flip ctx A */
1338 		err += misensor_rmw_reg(c, MISENSOR_8BIT, 0xC850, 0x01, 0x00);
1339 		err += misensor_rmw_reg(c, MISENSOR_8BIT, 0xC851, 0x01, 0x00);
1340 		/* ctx B */
1341 		err += misensor_rmw_reg(c, MISENSOR_8BIT, 0xC888, 0x01, 0x00);
1342 		err += misensor_rmw_reg(c, MISENSOR_8BIT, 0xC889, 0x01, 0x00);
1343 
1344 		err += misensor_rmw_reg(c, MISENSOR_16BIT, MISENSOR_READ_MODE,
1345 					MISENSOR_HFLIP_MASK, MISENSOR_FLIP_DIS);
1346 
1347 		dev->bpat = MT9M114_BPAT_BGBGGRGR;
1348 	}
1349 
1350 	err += mt9m114_write_reg(c, MISENSOR_8BIT, 0x8404, 0x06);
1351 	udelay(10);
1352 
1353 	return !!err;
1354 }
1355 
1356 /* Vertically flip the image */
mt9m114_t_vflip(struct v4l2_subdev * sd,int value)1357 static int mt9m114_t_vflip(struct v4l2_subdev *sd, int value)
1358 {
1359 	struct i2c_client *c = v4l2_get_subdevdata(sd);
1360 	int err;
1361 	/* set for direct mode */
1362 	err = mt9m114_write_reg(c, MISENSOR_16BIT, 0x098E, 0xC850);
1363 	if (value >= 1) {
1364 		/* enable H flip - ctx A */
1365 		err += misensor_rmw_reg(c, MISENSOR_8BIT, 0xC850, 0x02, 0x01);
1366 		err += misensor_rmw_reg(c, MISENSOR_8BIT, 0xC851, 0x02, 0x01);
1367 		/* ctx B */
1368 		err += misensor_rmw_reg(c, MISENSOR_8BIT, 0xC888, 0x02, 0x01);
1369 		err += misensor_rmw_reg(c, MISENSOR_8BIT, 0xC889, 0x02, 0x01);
1370 
1371 		err += misensor_rmw_reg(c, MISENSOR_16BIT, MISENSOR_READ_MODE,
1372 					MISENSOR_VFLIP_MASK, MISENSOR_FLIP_EN);
1373 	} else {
1374 		/* disable H flip - ctx A */
1375 		err += misensor_rmw_reg(c, MISENSOR_8BIT, 0xC850, 0x02, 0x00);
1376 		err += misensor_rmw_reg(c, MISENSOR_8BIT, 0xC851, 0x02, 0x00);
1377 		/* ctx B */
1378 		err += misensor_rmw_reg(c, MISENSOR_8BIT, 0xC888, 0x02, 0x00);
1379 		err += misensor_rmw_reg(c, MISENSOR_8BIT, 0xC889, 0x02, 0x00);
1380 
1381 		err += misensor_rmw_reg(c, MISENSOR_16BIT, MISENSOR_READ_MODE,
1382 					MISENSOR_VFLIP_MASK, MISENSOR_FLIP_DIS);
1383 	}
1384 
1385 	err += mt9m114_write_reg(c, MISENSOR_8BIT, 0x8404, 0x06);
1386 	udelay(10);
1387 
1388 	return !!err;
1389 }
1390 
mt9m114_get_frame_interval(struct v4l2_subdev * sd,struct v4l2_subdev_state * sd_state,struct v4l2_subdev_frame_interval * interval)1391 static int mt9m114_get_frame_interval(struct v4l2_subdev *sd,
1392 				      struct v4l2_subdev_state *sd_state,
1393 				      struct v4l2_subdev_frame_interval *interval)
1394 {
1395 	struct mt9m114_device *dev = to_mt9m114_sensor(sd);
1396 
1397 	/*
1398 	 * FIXME: Implement support for V4L2_SUBDEV_FORMAT_TRY, using the V4L2
1399 	 * subdev active state API.
1400 	 */
1401 	if (interval->which != V4L2_SUBDEV_FORMAT_ACTIVE)
1402 		return -EINVAL;
1403 
1404 	interval->interval.numerator = 1;
1405 	interval->interval.denominator = mt9m114_res[dev->res].fps;
1406 
1407 	return 0;
1408 }
1409 
mt9m114_s_stream(struct v4l2_subdev * sd,int enable)1410 static int mt9m114_s_stream(struct v4l2_subdev *sd, int enable)
1411 {
1412 	int ret;
1413 	struct i2c_client *c = v4l2_get_subdevdata(sd);
1414 	struct mt9m114_device *dev = to_mt9m114_sensor(sd);
1415 	struct atomisp_exposure exposure;
1416 
1417 	if (enable) {
1418 		ret = mt9m114_write_reg_array(c, mt9m114_chgstat_reg,
1419 					      POST_POLLING);
1420 		if (ret < 0)
1421 			return ret;
1422 
1423 		if (dev->first_exp > MT9M114_MAX_FIRST_EXP) {
1424 			exposure.integration_time[0] = dev->first_exp;
1425 			exposure.gain[0] = dev->first_gain;
1426 			exposure.gain[1] = dev->first_diggain;
1427 			mt9m114_s_exposure(sd, &exposure);
1428 		}
1429 		dev->streamon = 1;
1430 
1431 	} else {
1432 		dev->streamon = 0;
1433 		ret = mt9m114_set_suspend(sd);
1434 	}
1435 
1436 	return ret;
1437 }
1438 
mt9m114_enum_mbus_code(struct v4l2_subdev * sd,struct v4l2_subdev_state * sd_state,struct v4l2_subdev_mbus_code_enum * code)1439 static int mt9m114_enum_mbus_code(struct v4l2_subdev *sd,
1440 				  struct v4l2_subdev_state *sd_state,
1441 				  struct v4l2_subdev_mbus_code_enum *code)
1442 {
1443 	if (code->index)
1444 		return -EINVAL;
1445 	code->code = MEDIA_BUS_FMT_SGRBG10_1X10;
1446 
1447 	return 0;
1448 }
1449 
mt9m114_enum_frame_size(struct v4l2_subdev * sd,struct v4l2_subdev_state * sd_state,struct v4l2_subdev_frame_size_enum * fse)1450 static int mt9m114_enum_frame_size(struct v4l2_subdev *sd,
1451 				   struct v4l2_subdev_state *sd_state,
1452 				   struct v4l2_subdev_frame_size_enum *fse)
1453 {
1454 	unsigned int index = fse->index;
1455 
1456 	if (index >= N_RES)
1457 		return -EINVAL;
1458 
1459 	fse->min_width = mt9m114_res[index].width;
1460 	fse->min_height = mt9m114_res[index].height;
1461 	fse->max_width = mt9m114_res[index].width;
1462 	fse->max_height = mt9m114_res[index].height;
1463 
1464 	return 0;
1465 }
1466 
mt9m114_g_skip_frames(struct v4l2_subdev * sd,u32 * frames)1467 static int mt9m114_g_skip_frames(struct v4l2_subdev *sd, u32 *frames)
1468 {
1469 	int index;
1470 	struct mt9m114_device *snr = to_mt9m114_sensor(sd);
1471 
1472 	if (!frames)
1473 		return -EINVAL;
1474 
1475 	for (index = 0; index < N_RES; index++) {
1476 		if (mt9m114_res[index].res == snr->res)
1477 			break;
1478 	}
1479 
1480 	if (index >= N_RES)
1481 		return -EINVAL;
1482 
1483 	*frames = mt9m114_res[index].skip_frames;
1484 
1485 	return 0;
1486 }
1487 
1488 static const struct v4l2_subdev_video_ops mt9m114_video_ops = {
1489 	.s_stream = mt9m114_s_stream,
1490 };
1491 
1492 static const struct v4l2_subdev_sensor_ops mt9m114_sensor_ops = {
1493 	.g_skip_frames	= mt9m114_g_skip_frames,
1494 };
1495 
1496 static const struct v4l2_subdev_core_ops mt9m114_core_ops = {
1497 	.s_power = mt9m114_s_power,
1498 	.ioctl = mt9m114_ioctl,
1499 };
1500 
1501 /* REVISIT: Do we need pad operations? */
1502 static const struct v4l2_subdev_pad_ops mt9m114_pad_ops = {
1503 	.enum_mbus_code = mt9m114_enum_mbus_code,
1504 	.enum_frame_size = mt9m114_enum_frame_size,
1505 	.get_fmt = mt9m114_get_fmt,
1506 	.set_fmt = mt9m114_set_fmt,
1507 	.set_selection = mt9m114_s_exposure_selection,
1508 	.get_frame_interval = mt9m114_get_frame_interval,
1509 };
1510 
1511 static const struct v4l2_subdev_ops mt9m114_ops = {
1512 	.core = &mt9m114_core_ops,
1513 	.video = &mt9m114_video_ops,
1514 	.pad = &mt9m114_pad_ops,
1515 	.sensor = &mt9m114_sensor_ops,
1516 };
1517 
mt9m114_remove(struct i2c_client * client)1518 static void mt9m114_remove(struct i2c_client *client)
1519 {
1520 	struct mt9m114_device *dev;
1521 	struct v4l2_subdev *sd = i2c_get_clientdata(client);
1522 
1523 	dev = container_of(sd, struct mt9m114_device, sd);
1524 	dev->platform_data->csi_cfg(sd, 0);
1525 	v4l2_device_unregister_subdev(sd);
1526 	media_entity_cleanup(&dev->sd.entity);
1527 	v4l2_ctrl_handler_free(&dev->ctrl_handler);
1528 	kfree(dev);
1529 }
1530 
mt9m114_probe(struct i2c_client * client)1531 static int mt9m114_probe(struct i2c_client *client)
1532 {
1533 	struct mt9m114_device *dev;
1534 	int ret = 0;
1535 	unsigned int i;
1536 	void *pdata;
1537 
1538 	/* Setup sensor configuration structure */
1539 	dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1540 	if (!dev)
1541 		return -ENOMEM;
1542 
1543 	v4l2_i2c_subdev_init(&dev->sd, client, &mt9m114_ops);
1544 	pdata = gmin_camera_platform_data(&dev->sd,
1545 					  ATOMISP_INPUT_FORMAT_RAW_10,
1546 					  atomisp_bayer_order_grbg);
1547 	if (pdata)
1548 		ret = mt9m114_s_config(&dev->sd, client->irq, pdata);
1549 	if (!pdata || ret) {
1550 		v4l2_device_unregister_subdev(&dev->sd);
1551 		kfree(dev);
1552 		return ret;
1553 	}
1554 
1555 	ret = atomisp_register_i2c_module(&dev->sd, pdata);
1556 	if (ret) {
1557 		v4l2_device_unregister_subdev(&dev->sd);
1558 		kfree(dev);
1559 		/* Coverity CID 298095 - return on error */
1560 		return ret;
1561 	}
1562 
1563 	/* TODO add format code here */
1564 	dev->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
1565 	dev->pad.flags = MEDIA_PAD_FL_SOURCE;
1566 	dev->format.code = MEDIA_BUS_FMT_SGRBG10_1X10;
1567 	dev->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
1568 
1569 	ret =
1570 	    v4l2_ctrl_handler_init(&dev->ctrl_handler,
1571 				   ARRAY_SIZE(mt9m114_controls));
1572 	if (ret) {
1573 		mt9m114_remove(client);
1574 		return ret;
1575 	}
1576 
1577 	for (i = 0; i < ARRAY_SIZE(mt9m114_controls); i++)
1578 		v4l2_ctrl_new_custom(&dev->ctrl_handler, &mt9m114_controls[i],
1579 				     NULL);
1580 
1581 	if (dev->ctrl_handler.error) {
1582 		mt9m114_remove(client);
1583 		return dev->ctrl_handler.error;
1584 	}
1585 
1586 	/* Use same lock for controls as for everything else. */
1587 	dev->ctrl_handler.lock = &dev->input_lock;
1588 	dev->sd.ctrl_handler = &dev->ctrl_handler;
1589 
1590 	/* REVISIT: Do we need media controller? */
1591 	ret = media_entity_pads_init(&dev->sd.entity, 1, &dev->pad);
1592 	if (ret) {
1593 		mt9m114_remove(client);
1594 		return ret;
1595 	}
1596 	return 0;
1597 }
1598 
1599 static const struct acpi_device_id mt9m114_acpi_match[] = {
1600 	{ "INT33F0" },
1601 	{ "CRMT1040" },
1602 	{},
1603 };
1604 MODULE_DEVICE_TABLE(acpi, mt9m114_acpi_match);
1605 
1606 static struct i2c_driver mt9m114_driver = {
1607 	.driver = {
1608 		.name = "mt9m114",
1609 		.acpi_match_table = mt9m114_acpi_match,
1610 	},
1611 	.probe = mt9m114_probe,
1612 	.remove = mt9m114_remove,
1613 };
1614 module_i2c_driver(mt9m114_driver);
1615 
1616 MODULE_AUTHOR("Shuguang Gong <Shuguang.gong@intel.com>");
1617 MODULE_LICENSE("GPL");
1618