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