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