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