2 * aptina_camera_module.c
4 * Generic galaxycore sensor driver
6 * Copyright (C) 2016 Fuzhou Rockchip Electronics Co., Ltd.
8 * Copyright (C) 2012-2014 Intel Mobile Communications GmbH
10 * Copyright (C) 2008 Texas Instruments.
12 * This file is licensed under the terms of the GNU General Public License
13 * version 2. This program is licensed "as is" without any warranty of any
14 * kind, whether express or implied.
18 #include <linux/delay.h>
19 #include <media/v4l2-subdev.h>
20 #include <media/v4l2-device.h>
21 #include <media/videobuf-core.h>
22 #include <linux/slab.h>
23 #include <linux/gcd.h>
24 #include <linux/i2c.h>
25 #include <media/v4l2-controls_rockchip.h>
26 #include "aptina_camera_module.h"
29 #define I2C_MSG_MAX 300
30 #define I2C_DATA_MAX (I2C_MSG_MAX * 3)
32 /* ======================================================================== */
34 struct aptina_camera_module *to_aptina_camera_module(struct v4l2_subdev *sd)
36 return container_of(sd, struct aptina_camera_module, sd);
39 /* ======================================================================== */
41 void aptina_camera_module_reset(
42 struct aptina_camera_module *cam_mod)
44 pltfrm_camera_module_pr_debug(&cam_mod->sd, "\n");
46 cam_mod->inited = false;
47 cam_mod->active_config = NULL;
48 cam_mod->update_config = true;
49 cam_mod->frm_fmt_valid = false;
50 cam_mod->frm_intrvl_valid = false;
51 cam_mod->exp_config.auto_exp = false;
52 cam_mod->exp_config.auto_gain = false;
53 cam_mod->wb_config.auto_wb = false;
54 cam_mod->hflip = false;
55 cam_mod->vflip = false;
56 cam_mod->auto_adjust_fps = true;
57 cam_mod->rotation = 0;
58 cam_mod->ctrl_updt = 0;
59 cam_mod->state = APTINA_CAMERA_MODULE_POWER_OFF;
60 cam_mod->state_before_suspend = APTINA_CAMERA_MODULE_POWER_OFF;
63 /* ======================================================================== */
65 int aptina_read_i2c_reg(
66 struct v4l2_subdev *sd,
71 struct i2c_client *client = v4l2_get_subdevdata(sd);
73 struct i2c_msg msg[1];
74 unsigned char data[2] = { 0, 0};
76 if (!client->adapter) {
77 pltfrm_camera_module_pr_err(sd, "client->adapter NULL\n");
81 msg->addr = client->addr;
82 msg->flags = I2C_M_WR;
86 /* High byte goes out first */
87 data[0] = (u8)((reg >> 8) & 0xff);
88 data[1] = (u8)(reg & 0xff);
90 ret = i2c_transfer(client->adapter, msg, 1);
93 msg->flags = I2C_M_RD;
94 msg->len = data_length;
95 i2c_transfer(client->adapter, msg, 1);
99 /* High byte comes first */
100 if (data_length == 1)
102 else if (data_length == 2)
103 *val = data[1] + (data[0] << 8);
109 pltfrm_camera_module_pr_err(sd,
110 "i2c read from offset 0x%08x failed with error %d\n", reg, ret);
114 /* ======================================================================== */
116 int aptina_write_i2c_reg(
117 struct v4l2_subdev *sd,
120 struct i2c_client *client = v4l2_get_subdevdata(sd);
122 struct i2c_msg msg[1];
123 unsigned char data[4] = {0, 0, 0, 0};
126 if (!client->adapter) {
127 pltfrm_camera_module_pr_err(sd, "client->adapter NULL\n");
131 for (retries = 0; retries < 5; retries++) {
132 msg->addr = client->addr;
133 msg->flags = I2C_M_WR;
137 /* high byte goes out first */
138 data[0] = (u8)((reg >> 8) & 0xff);
139 data[1] = (u8)(reg & 0xff);
140 data[2] = (u8)((val >> 8) & 0xff);
141 data[3] = (u8)(val & 0xff);
143 ret = i2c_transfer(client->adapter, msg, 1);
144 usleep_range(20, 50);
149 pltfrm_camera_module_pr_debug(sd,
150 "retrying I2C... %d\n", retries);
152 set_current_state(TASK_UNINTERRUPTIBLE);
153 schedule_timeout(msecs_to_jiffies(20));
156 pltfrm_camera_module_pr_err(sd,
157 "i2c write to offset 0x%08x failed with error %d\n", reg, ret);
161 /* ======================================================================== */
163 int aptina_write_reglist(
164 struct v4l2_subdev *sd,
165 const struct pltfrm_camera_module_reg reglist[],
168 struct i2c_client *client = v4l2_get_subdevdata(sd);
170 unsigned int k = 0, j = 0;
174 unsigned int max_entries = len;
176 msg = kmalloc((sizeof(struct i2c_msg) * I2C_MSG_MAX), GFP_KERNEL);
179 data = kmalloc((sizeof(unsigned char) * I2C_DATA_MAX), GFP_KERNEL);
185 for (i = 0; i < max_entries; i++) {
186 switch (reglist[i].flag) {
187 case PLTFRM_CAMERA_MODULE_REG_TYPE_DATA:
188 (msg + j)->addr = client->addr;
189 (msg + j)->flags = I2C_M_WR;
191 (msg + j)->buf = (data + k);
193 data[k + 0] = (u8)((reglist[i].reg >> 8) & 0xFF);
194 data[k + 1] = (u8)(reglist[i].reg & 0xFF);
195 data[k + 2] = (u8)((reglist[i].val >> 8) & 0xFF);
196 data[k + 3] = (u8)(reglist[i].val & 0xFF);
200 if (j == (I2C_MSG_MAX - 1)) {
201 /* Bulk I2C transfer */
202 pltfrm_camera_module_pr_err(sd,
203 "messages transfers 1 0x%p msg %d bytes %d\n",
205 ret = i2c_transfer(client->adapter, msg, j);
207 pltfrm_camera_module_pr_err(sd,
208 "i2c transfer returned with err %d\n",
216 pltfrm_camera_module_pr_debug(sd,
217 "i2c_transfer return %d\n", ret);
220 case PLTFRM_CAMERA_MODULE_REG_TYPE_TIMEOUT:
222 /* Bulk I2C transfer */
223 pltfrm_camera_module_pr_debug(sd,
224 "messages transfers 1 0x%p msg %d bytes %d\n",
226 ret = i2c_transfer(client->adapter, msg, j);
228 pltfrm_camera_module_pr_debug(sd,
229 "i2c transfer returned with err %d\n",
235 pltfrm_camera_module_pr_debug(sd,
236 "i2c_transfer return %d\n", ret);
238 mdelay(reglist[i].val);
243 pltfrm_camera_module_pr_debug(sd, "unknown command\n");
250 if (j != 0) { /*Remaining I2C message*/
251 pltfrm_camera_module_pr_debug(sd,
252 "messages transfers 1 0x%p msg %d bytes %d\n",
254 ret = i2c_transfer(client->adapter, msg, j);
256 pltfrm_camera_module_pr_err(sd,
257 "i2c transfer returned with err %d\n", ret);
262 pltfrm_camera_module_pr_debug(sd,
263 "i2c_transfer return %d\n", ret);
271 static void aptina_camera_module_set_active_config(
272 struct aptina_camera_module *cam_mod,
273 struct aptina_camera_module_config *new_config)
275 pltfrm_camera_module_pr_debug(&cam_mod->sd, "\n");
277 if (IS_ERR_OR_NULL(new_config)) {
278 cam_mod->active_config = new_config;
279 pltfrm_camera_module_pr_debug(&cam_mod->sd,
280 "no active config\n");
282 cam_mod->ctrl_updt &= APTINA_CAMERA_MODULE_CTRL_UPDT_AUTO_EXP |
283 APTINA_CAMERA_MODULE_CTRL_UPDT_AUTO_GAIN |
284 APTINA_CAMERA_MODULE_CTRL_UPDT_AUTO_WB;
285 if (new_config->auto_exp_enabled !=
286 cam_mod->exp_config.auto_exp) {
287 cam_mod->ctrl_updt |=
288 APTINA_CAMERA_MODULE_CTRL_UPDT_AUTO_EXP;
289 cam_mod->exp_config.auto_exp =
290 new_config->auto_exp_enabled;
292 if (new_config->auto_gain_enabled !=
293 cam_mod->exp_config.auto_gain) {
294 cam_mod->ctrl_updt |=
295 APTINA_CAMERA_MODULE_CTRL_UPDT_AUTO_GAIN;
296 cam_mod->exp_config.auto_gain =
297 new_config->auto_gain_enabled;
299 if (new_config->auto_wb_enabled !=
300 cam_mod->wb_config.auto_wb) {
301 cam_mod->ctrl_updt |=
302 APTINA_CAMERA_MODULE_CTRL_UPDT_AUTO_WB;
303 cam_mod->wb_config.auto_wb =
304 new_config->auto_wb_enabled;
306 if (new_config != cam_mod->active_config) {
307 cam_mod->update_config = true;
308 cam_mod->active_config = new_config;
309 pltfrm_camera_module_pr_debug(&cam_mod->sd,
310 "activating config '%s'\n",
316 /* ======================================================================== */
318 static struct aptina_camera_module_config *aptina_camera_module_find_config(
319 struct aptina_camera_module *cam_mod,
320 struct v4l2_mbus_framefmt *fmt,
321 struct v4l2_subdev_frame_interval *frm_intrvl)
325 struct v4l2_subdev_frame_interval norm_interval;
327 pltfrm_camera_module_pr_debug(&cam_mod->sd, "\n");
328 if (!IS_ERR_OR_NULL(fmt))
329 pltfrm_camera_module_pr_debug(&cam_mod->sd,
330 "%dx%d, fmt code 0x%04x\n",
331 fmt->width, fmt->height, fmt->code);
333 if (!IS_ERR_OR_NULL(frm_intrvl))
334 pltfrm_camera_module_pr_debug(&cam_mod->sd,
335 "frame interval %d/%d\n",
336 frm_intrvl->interval.numerator,
337 frm_intrvl->interval.denominator);
339 for (i = 0; i < cam_mod->custom.num_configs; i++) {
340 if (!IS_ERR_OR_NULL(frm_intrvl)) {
341 gcdiv = gcd(cam_mod->custom.configs[i].
342 frm_intrvl.interval.numerator,
343 cam_mod->custom.configs[i].
344 frm_intrvl.interval.denominator);
345 norm_interval.interval.numerator =
346 cam_mod->custom.configs[i].
347 frm_intrvl.interval.numerator / gcdiv;
348 norm_interval.interval.denominator =
349 cam_mod->custom.configs[i].
350 frm_intrvl.interval.denominator / gcdiv;
351 if ((frm_intrvl->interval.numerator !=
352 norm_interval.interval.numerator) ||
353 (frm_intrvl->interval.denominator !=
354 norm_interval.interval.denominator))
357 if (!IS_ERR_OR_NULL(fmt)) {
358 if ((cam_mod->custom.configs[i].frm_fmt.width !=
360 (cam_mod->custom.configs[i].frm_fmt.height !=
362 (cam_mod->custom.configs[i].frm_fmt.code !=
367 pltfrm_camera_module_pr_debug(&cam_mod->sd,
368 "found matching config %s\n",
369 cam_mod->custom.configs[i].name);
370 return &cam_mod->custom.configs[i];
372 pltfrm_camera_module_pr_debug(&cam_mod->sd,
373 "no matching config found\n");
375 return ERR_PTR(-EINVAL);
378 /* ======================================================================== */
380 static int aptina_camera_module_write_config(
381 struct aptina_camera_module *cam_mod)
385 pltfrm_camera_module_pr_debug(&cam_mod->sd, "\n");
387 if (IS_ERR_OR_NULL(cam_mod->active_config)) {
388 pltfrm_camera_module_pr_err(&cam_mod->sd,
389 "no active sensor configuration");
394 ret = aptina_write_reglist(&cam_mod->sd,
395 cam_mod->active_config->reg_table,
396 cam_mod->active_config->reg_table_num_entries);
397 if (IS_ERR_VALUE(ret))
399 ret = pltfrm_camera_module_patch_config(&cam_mod->sd,
401 &cam_mod->frm_intrvl);
402 if (IS_ERR_VALUE(ret))
407 pltfrm_camera_module_pr_err(&cam_mod->sd,
408 "failed with error %d\n", ret);
412 static int aptina_camera_module_attach(
413 struct aptina_camera_module *cam_mod)
416 struct aptina_camera_module_custom_config *custom;
418 custom = &cam_mod->custom;
419 pltfrm_camera_module_pr_debug(&cam_mod->sd, "\n");
420 if (custom->check_camera_id) {
421 aptina_camera_module_s_power(&cam_mod->sd, 1);
422 ret = custom->check_camera_id(cam_mod);
423 aptina_camera_module_s_power(&cam_mod->sd, 0);
431 pltfrm_camera_module_pr_err(&cam_mod->sd,
432 "failed with error %d\n", ret);
433 aptina_camera_module_release(cam_mod);
437 /* ======================================================================== */
439 int aptina_camera_module_try_fmt(struct v4l2_subdev *sd,
440 struct v4l2_mbus_framefmt *fmt)
442 struct aptina_camera_module *cam_mod = to_aptina_camera_module(sd);
444 pltfrm_camera_module_pr_debug(&cam_mod->sd, "%dx%d, fmt code 0x%04x\n",
445 fmt->width, fmt->height, fmt->code);
448 aptina_camera_module_find_config(
449 cam_mod, fmt, NULL))) {
450 pltfrm_camera_module_pr_debug(&cam_mod->sd,
451 "format not supported\n");
454 pltfrm_camera_module_pr_debug(&cam_mod->sd, "format supported\n");
459 /* ======================================================================== */
461 int aptina_camera_module_s_fmt(struct v4l2_subdev *sd,
462 struct v4l2_subdev_pad_config *cfg,
463 struct v4l2_subdev_format *format)
465 struct aptina_camera_module *cam_mod = to_aptina_camera_module(sd);
466 struct v4l2_mbus_framefmt *fmt = &format->format;
469 pltfrm_camera_module_pr_debug(&cam_mod->sd, "%dx%d, fmt code 0x%04x\n",
470 fmt->width, fmt->height, fmt->code);
473 aptina_camera_module_find_config(
474 cam_mod, fmt, NULL))) {
475 pltfrm_camera_module_pr_err(&cam_mod->sd,
476 "format %dx%d, code 0x%04x, not supported\n",
477 fmt->width, fmt->height, fmt->code);
481 cam_mod->frm_fmt_valid = true;
482 cam_mod->frm_fmt = *fmt;
483 if (cam_mod->frm_intrvl_valid) {
484 aptina_camera_module_set_active_config(cam_mod,
485 aptina_camera_module_find_config(cam_mod,
486 fmt, &cam_mod->frm_intrvl));
490 pltfrm_camera_module_pr_err(&cam_mod->sd,
491 "failed with error %d\n", ret);
495 /* ======================================================================== */
497 int aptina_camera_module_g_fmt(struct v4l2_subdev *sd,
498 struct v4l2_subdev_pad_config *cfg,
499 struct v4l2_subdev_format *format)
501 struct aptina_camera_module *cam_mod = to_aptina_camera_module(sd);
502 struct v4l2_mbus_framefmt *fmt = &format->format;
504 pltfrm_camera_module_pr_debug(&cam_mod->sd, "\n");
506 if (cam_mod->active_config) {
507 fmt->code = cam_mod->active_config->frm_fmt.code;
508 fmt->width = cam_mod->active_config->frm_fmt.width;
509 fmt->height = cam_mod->active_config->frm_fmt.height;
513 pltfrm_camera_module_pr_debug(&cam_mod->sd, "no active config\n");
518 /* ======================================================================== */
520 int aptina_camera_module_s_frame_interval(
521 struct v4l2_subdev *sd,
522 struct v4l2_subdev_frame_interval *interval)
524 struct aptina_camera_module *cam_mod = to_aptina_camera_module(sd);
526 struct v4l2_subdev_frame_interval norm_interval;
529 pltfrm_camera_module_pr_debug(&cam_mod->sd, "\n");
530 if ((interval->interval.denominator == 0) ||
531 (interval->interval.numerator == 0)) {
532 pltfrm_camera_module_pr_err(&cam_mod->sd,
533 "invalid frame interval %d/%d\n",
534 interval->interval.numerator,
535 interval->interval.denominator);
540 pltfrm_camera_module_pr_debug(&cam_mod->sd, "%d/%d (%dfps)\n",
541 interval->interval.numerator, interval->interval.denominator,
542 (interval->interval.denominator +
543 (interval->interval.numerator >> 1)) /
544 interval->interval.numerator);
546 /* normalize interval */
547 gcdiv = gcd(interval->interval.numerator,
548 interval->interval.denominator);
549 norm_interval.interval.numerator =
550 interval->interval.numerator / gcdiv;
551 norm_interval.interval.denominator =
552 interval->interval.denominator / gcdiv;
554 if (IS_ERR_OR_NULL(aptina_camera_module_find_config(cam_mod,
555 NULL, &norm_interval))) {
556 pltfrm_camera_module_pr_err(&cam_mod->sd,
557 "frame interval %d/%d not supported\n",
558 interval->interval.numerator,
559 interval->interval.denominator);
563 cam_mod->frm_intrvl_valid = true;
564 cam_mod->frm_intrvl = norm_interval;
565 if (cam_mod->frm_fmt_valid) {
566 aptina_camera_module_set_active_config(cam_mod,
567 aptina_camera_module_find_config(cam_mod,
568 &cam_mod->frm_fmt, interval));
572 pltfrm_camera_module_pr_err(&cam_mod->sd,
573 "failed with error %d\n", ret);
577 /* ======================================================================== */
579 int aptina_camera_module_s_stream(struct v4l2_subdev *sd, int enable)
582 struct aptina_camera_module *cam_mod = to_aptina_camera_module(sd);
584 pltfrm_camera_module_pr_debug(&cam_mod->sd, "%d\n", enable);
587 if (cam_mod->state == APTINA_CAMERA_MODULE_STREAMING)
589 if (IS_ERR_OR_NULL(cam_mod->active_config)) {
590 pltfrm_camera_module_pr_err(&cam_mod->sd,
591 "no active sensor configuration, cannot start streaming\n");
595 if (cam_mod->state != APTINA_CAMERA_MODULE_SW_STANDBY) {
596 pltfrm_camera_module_pr_err(&cam_mod->sd,
597 "sensor is not powered on (in state %d), cannot start streaming\n",
603 if (!IS_ERR_OR_NULL(cam_mod->custom.set_flip))
604 cam_mod->custom.set_flip(cam_mod);
606 if (cam_mod->update_config)
607 ret = aptina_camera_module_write_config(cam_mod);
608 if (IS_ERR_VALUE(ret))
611 ret = cam_mod->custom.start_streaming(cam_mod);
612 if (IS_ERR_VALUE(ret))
614 cam_mod->update_config = false;
615 cam_mod->ctrl_updt = 0;
616 mdelay(cam_mod->custom.power_up_delays_ms[2]);
617 cam_mod->state = APTINA_CAMERA_MODULE_STREAMING;
621 struct isp_supplemental_sensor_mode_data timings;
623 if (cam_mod->state != APTINA_CAMERA_MODULE_STREAMING)
625 ret = cam_mod->custom.stop_streaming(cam_mod);
626 if (IS_ERR_VALUE(ret))
629 cam_mod->state = APTINA_CAMERA_MODULE_SW_STANDBY;
631 ret = aptina_camera_module_ioctl(sd, RK_VIDIOC_SENSOR_MODE_DATA,
633 if (IS_ERR_VALUE(ret))
635 pclk = timings.vt_pix_clk_freq_hz / 1000;
638 wait_ms = (timings.line_length_pck
639 * timings.frame_length_lines) / pclk;
640 /* wait for a frame period to make sure that there is
641 *no pending frame left.
646 cam_mod->state_before_suspend = cam_mod->state;
650 pltfrm_camera_module_pr_err(&cam_mod->sd,
651 "failed with error %d\n", ret);
655 /* ======================================================================== */
657 int aptina_camera_module_s_power(struct v4l2_subdev *sd, int on)
660 struct aptina_camera_module *cam_mod = to_aptina_camera_module(sd);
661 struct v4l2_subdev *af_ctrl;
663 pltfrm_camera_module_pr_debug(&cam_mod->sd, "%d\n", on);
666 if (cam_mod->state == APTINA_CAMERA_MODULE_POWER_OFF) {
667 ret = pltfrm_camera_module_s_power(&cam_mod->sd, 1);
668 if (!IS_ERR_VALUE(ret)) {
669 mdelay(cam_mod->custom.power_up_delays_ms[0]);
671 APTINA_CAMERA_MODULE_HW_STANDBY;
674 if (cam_mod->state == APTINA_CAMERA_MODULE_HW_STANDBY) {
675 ret = pltfrm_camera_module_set_pin_state(&cam_mod->sd,
676 PLTFRM_CAMERA_MODULE_PIN_PWR,
677 PLTFRM_CAMERA_MODULE_PIN_STATE_ACTIVE);
680 ret = pltfrm_camera_module_set_pin_state(
682 PLTFRM_CAMERA_MODULE_PIN_PD,
683 PLTFRM_CAMERA_MODULE_PIN_STATE_ACTIVE);
686 ret = pltfrm_camera_module_set_pin_state(
688 PLTFRM_CAMERA_MODULE_PIN_RESET,
689 PLTFRM_CAMERA_MODULE_PIN_STATE_ACTIVE);
692 ret = pltfrm_camera_module_set_pin_state(
694 PLTFRM_CAMERA_MODULE_PIN_RESET,
695 PLTFRM_CAMERA_MODULE_PIN_STATE_INACTIVE);
698 ret = pltfrm_camera_module_set_pin_state(
700 PLTFRM_CAMERA_MODULE_PIN_RESET,
701 PLTFRM_CAMERA_MODULE_PIN_STATE_ACTIVE);
704 ret = pltfrm_camera_module_set_pin_state(
706 PLTFRM_CAMERA_MODULE_PIN_PD,
707 PLTFRM_CAMERA_MODULE_PIN_STATE_INACTIVE);
709 ret = pltfrm_camera_module_set_pin_state(
711 PLTFRM_CAMERA_MODULE_PIN_PD,
712 PLTFRM_CAMERA_MODULE_PIN_STATE_ACTIVE);
715 if (!IS_ERR_VALUE(ret)) {
716 mdelay(cam_mod->custom.power_up_delays_ms[1]);
718 APTINA_CAMERA_MODULE_SW_STANDBY;
722 cam_mod->custom.init_common) &&
723 cam_mod->custom.init_common(
725 usleep_range(1000, 1500);
728 af_ctrl = pltfrm_camera_module_get_af_ctrl(sd);
729 if (!IS_ERR_OR_NULL(af_ctrl)) {
730 v4l2_subdev_call(af_ctrl,
736 if (cam_mod->state == APTINA_CAMERA_MODULE_STREAMING) {
737 ret = aptina_camera_module_s_stream(sd, 0);
738 if (!IS_ERR_VALUE(ret))
740 APTINA_CAMERA_MODULE_SW_STANDBY;
742 if (cam_mod->state == APTINA_CAMERA_MODULE_SW_STANDBY) {
743 ret = pltfrm_camera_module_set_pin_state(
745 PLTFRM_CAMERA_MODULE_PIN_PD,
746 PLTFRM_CAMERA_MODULE_PIN_STATE_INACTIVE);
748 ret = pltfrm_camera_module_set_pin_state(
750 PLTFRM_CAMERA_MODULE_PIN_RESET,
751 PLTFRM_CAMERA_MODULE_PIN_STATE_INACTIVE);
753 ret = pltfrm_camera_module_set_pin_state(
755 PLTFRM_CAMERA_MODULE_PIN_PWR,
756 PLTFRM_CAMERA_MODULE_PIN_STATE_INACTIVE);
758 if (!IS_ERR_VALUE(ret))
760 APTINA_CAMERA_MODULE_HW_STANDBY;
762 if (cam_mod->state == APTINA_CAMERA_MODULE_HW_STANDBY) {
763 ret = pltfrm_camera_module_s_power(&cam_mod->sd, 0);
764 if (!IS_ERR_VALUE(ret)) {
765 cam_mod->state = APTINA_CAMERA_MODULE_POWER_OFF;
766 aptina_camera_module_reset(cam_mod);
771 cam_mod->state_before_suspend = cam_mod->state;
773 if (IS_ERR_VALUE(ret)) {
774 pltfrm_camera_module_pr_err(&cam_mod->sd,
775 "%s failed, camera left in state %d\n",
776 on ? "on" : "off", cam_mod->state);
779 pltfrm_camera_module_pr_debug(&cam_mod->sd,
780 "camera powered %s\n", on ? "on" : "off");
784 pltfrm_camera_module_pr_err(&cam_mod->sd,
785 "failed with error %d\n", ret);
789 /* ======================================================================== */
791 int aptina_camera_module_g_ctrl(struct v4l2_subdev *sd,
792 struct v4l2_control *ctrl)
794 struct aptina_camera_module *cam_mod = to_aptina_camera_module(sd);
797 pltfrm_camera_module_pr_debug(&cam_mod->sd, " id 0x%x\n", ctrl->id);
799 if (ctrl->id == V4L2_CID_FLASH_LED_MODE) {
800 ctrl->value = cam_mod->exp_config.flash_mode;
801 pltfrm_camera_module_pr_debug(&cam_mod->sd,
802 "V4L2_CID_FLASH_LED_MODE %d\n",
807 if (IS_ERR_OR_NULL(cam_mod->active_config)) {
808 pltfrm_camera_module_pr_err(&cam_mod->sd,
809 "no active configuration\n");
813 if (ctrl->id == RK_V4L2_CID_VBLANKING) {
814 ctrl->value = cam_mod->active_config->v_blanking_time_us;
815 pltfrm_camera_module_pr_debug(&cam_mod->sd,
816 "RK_V4L2_CID_VBLANKING %d\n",
822 *if (ctrl->id == RK_V4L2_CID_IGNORE_MEASUREMENT_CHECK) {
823 * ctrl->value = cam_mod->active_config->ignore_measurement_check;
824 * pltfrm_camera_module_pr_debug(
826 * "CIF_ISP20_CID_IGNORE_MEASUREMENT_CHECK %d\n",
832 if ((cam_mod->state != APTINA_CAMERA_MODULE_SW_STANDBY) &&
833 (cam_mod->state != APTINA_CAMERA_MODULE_STREAMING)) {
834 pltfrm_camera_module_pr_err(&cam_mod->sd,
835 "cannot get controls when camera is off\n");
839 if (ctrl->id == V4L2_CID_FOCUS_ABSOLUTE) {
840 struct v4l2_subdev *af_ctrl;
843 pltfrm_camera_module_get_af_ctrl(sd);
844 if (!IS_ERR_OR_NULL(af_ctrl)) {
845 ret = v4l2_subdev_call(af_ctrl, core, g_ctrl, ctrl);
850 if (!IS_ERR_OR_NULL(cam_mod->custom.g_ctrl)) {
851 ret = cam_mod->custom.g_ctrl(cam_mod, ctrl->id);
852 if (IS_ERR_VALUE(ret))
858 ctrl->value = cam_mod->exp_config.gain;
859 pltfrm_camera_module_pr_debug(&cam_mod->sd,
860 "V4L2_CID_GAIN %d\n",
863 case V4L2_CID_EXPOSURE:
864 ctrl->value = cam_mod->exp_config.exp_time;
865 pltfrm_camera_module_pr_debug(&cam_mod->sd,
866 "V4L2_CID_EXPOSURE %d\n",
869 case V4L2_CID_WHITE_BALANCE_TEMPERATURE:
870 ctrl->value = cam_mod->wb_config.temperature;
871 pltfrm_camera_module_pr_debug(&cam_mod->sd,
872 "V4L2_CID_WHITE_BALANCE_TEMPERATURE %d\n",
875 case V4L2_CID_AUTOGAIN:
876 ctrl->value = cam_mod->exp_config.auto_gain;
877 pltfrm_camera_module_pr_debug(&cam_mod->sd,
878 "V4L2_CID_AUTOGAIN %d\n",
881 case V4L2_CID_EXPOSURE_AUTO:
882 ctrl->value = cam_mod->exp_config.auto_exp;
883 pltfrm_camera_module_pr_debug(&cam_mod->sd,
884 "V4L2_CID_EXPOSURE_AUTO %d\n",
887 case V4L2_CID_AUTO_WHITE_BALANCE:
888 ctrl->value = cam_mod->wb_config.auto_wb;
889 pltfrm_camera_module_pr_debug(&cam_mod->sd,
890 "V4L2_CID_AUTO_WHITE_BALANCE %d\n",
893 case V4L2_CID_FOCUS_ABSOLUTE:
894 ctrl->value = cam_mod->af_config.abs_pos;
895 pltfrm_camera_module_pr_debug(&cam_mod->sd,
896 "V4L2_CID_FOCUS_ABSOLUTE %d\n",
904 pltfrm_camera_module_pr_debug(&cam_mod->sd,
905 "failed, unknown ctrl %d\n", ctrl->id);
912 /* ======================================================================== */
914 static int flash_light_ctrl(
915 struct v4l2_subdev *sd,
916 struct aptina_camera_module *cam_mod,
922 /* ======================================================================== */
924 int aptina_camera_module_s_ext_ctrls(
925 struct v4l2_subdev *sd,
926 struct v4l2_ext_controls *ctrls)
930 struct aptina_camera_module *cam_mod = to_aptina_camera_module(sd);
933 pltfrm_camera_module_pr_debug(&cam_mod->sd, "\n");
935 if (ctrls->count == 0)
938 for (i = 0; i < ctrls->count; i++) {
939 struct v4l2_ext_control *ctrl;
942 ctrl = &ctrls->controls[i];
946 ctrl_updt = APTINA_CAMERA_MODULE_CTRL_UPDT_GAIN;
947 cam_mod->exp_config.gain = ctrl->value;
948 pltfrm_camera_module_pr_debug(&cam_mod->sd,
949 "V4L2_CID_GAIN %d\n",
952 case RK_V4L2_CID_GAIN_PERCENT:
953 ctrl_updt = APTINA_CAMERA_MODULE_CTRL_UPDT_GAIN;
954 cam_mod->exp_config.gain_percent = ctrl->value;
956 case V4L2_CID_FLASH_LED_MODE:
957 ret = flash_light_ctrl(sd, cam_mod, ctrl->value);
959 cam_mod->exp_config.flash_mode = ctrl->value;
960 pltfrm_camera_module_pr_debug(
962 "V4L2_CID_FLASH_LED_MODE %d\n",
966 case V4L2_CID_EXPOSURE:
967 ctrl_updt = APTINA_CAMERA_MODULE_CTRL_UPDT_EXP_TIME;
968 cam_mod->exp_config.exp_time = ctrl->value;
969 pltfrm_camera_module_pr_debug(&cam_mod->sd,
970 "V4L2_CID_EXPOSURE %d\n",
973 case V4L2_CID_WHITE_BALANCE_TEMPERATURE:
975 APTINA_CAMERA_MODULE_CTRL_UPDT_WB_TEMPERATURE;
976 cam_mod->wb_config.temperature = ctrl->value;
977 pltfrm_camera_module_pr_debug(&cam_mod->sd,
978 "V4L2_CID_WHITE_BALANCE_TEMPERATURE %d\n",
981 case V4L2_CID_AUTOGAIN:
982 ctrl_updt = APTINA_CAMERA_MODULE_CTRL_UPDT_AUTO_GAIN;
983 cam_mod->exp_config.auto_gain = ctrl->value;
984 pltfrm_camera_module_pr_debug(&cam_mod->sd,
985 "V4L2_CID_AUTOGAIN %d\n",
988 case V4L2_CID_EXPOSURE_AUTO:
989 ctrl_updt = APTINA_CAMERA_MODULE_CTRL_UPDT_AUTO_EXP;
990 cam_mod->exp_config.auto_exp = ctrl->value;
991 pltfrm_camera_module_pr_debug(&cam_mod->sd,
992 "V4L2_CID_EXPOSURE_AUTO %d\n",
995 case V4L2_CID_AUTO_WHITE_BALANCE:
996 ctrl_updt = APTINA_CAMERA_MODULE_CTRL_UPDT_AUTO_WB;
997 cam_mod->wb_config.auto_wb = ctrl->value;
998 pltfrm_camera_module_pr_debug(&cam_mod->sd,
999 "V4L2_CID_AUTO_WHITE_BALANCE %d\n",
1002 case RK_V4L2_CID_AUTO_FPS:
1003 cam_mod->auto_adjust_fps = ctrl->value;
1004 pltfrm_camera_module_pr_debug(&cam_mod->sd,
1005 "RK_V4L2_CID_AUTO_FPS %d\n",
1008 case V4L2_CID_FOCUS_ABSOLUTE:
1010 struct v4l2_subdev *af_ctrl;
1012 af_ctrl = pltfrm_camera_module_get_af_ctrl(sd);
1013 if (!IS_ERR_OR_NULL(af_ctrl)) {
1014 struct v4l2_control single_ctrl;
1017 V4L2_CID_FOCUS_ABSOLUTE;
1018 single_ctrl.value = ctrl->value;
1019 ret = v4l2_subdev_call(af_ctrl,
1020 core, s_ctrl, &single_ctrl);
1025 APTINA_CAMERA_MODULE_CTRL_UPDT_FOCUS_ABSOLUTE;
1026 cam_mod->af_config.abs_pos = ctrl->value;
1027 pltfrm_camera_module_pr_debug(&cam_mod->sd,
1028 "V4L2_CID_FOCUS_ABSOLUTE %d\n",
1031 case V4L2_CID_HFLIP:
1033 cam_mod->hflip = true;
1035 cam_mod->hflip = false;
1037 case V4L2_CID_VFLIP:
1039 cam_mod->vflip = true;
1041 cam_mod->vflip = false;
1044 pltfrm_camera_module_pr_warn(&cam_mod->sd,
1045 "ignoring unknown ctrl 0x%x\n", ctrl->id);
1049 if (cam_mod->state != APTINA_CAMERA_MODULE_SW_STANDBY &&
1050 cam_mod->state != APTINA_CAMERA_MODULE_STREAMING)
1051 cam_mod->ctrl_updt |= ctrl_updt;
1056 /* if camera module is already streaming, write through */
1058 (cam_mod->state == APTINA_CAMERA_MODULE_STREAMING ||
1059 cam_mod->state == APTINA_CAMERA_MODULE_SW_STANDBY)) {
1060 struct aptina_camera_module_ext_ctrls aptina_ctrls;
1062 aptina_ctrls.ctrls =
1063 (struct aptina_camera_module_ext_ctrl *)
1064 kmalloc(ctrl_cnt * sizeof(struct aptina_camera_module_ext_ctrl),
1067 if (aptina_ctrls.ctrls) {
1068 for (i = 0; i < ctrl_cnt; i++) {
1069 aptina_ctrls.ctrls[i].id =
1070 ctrls->controls[i].id;
1071 aptina_ctrls.ctrls[i].value =
1072 ctrls->controls[i].value;
1075 aptina_ctrls.count = ctrl_cnt;
1077 ret = cam_mod->custom.s_ext_ctrls(cam_mod,
1080 kfree(aptina_ctrls.ctrls);
1085 if (IS_ERR_VALUE(ret))
1086 pltfrm_camera_module_pr_debug(&cam_mod->sd,
1087 "failed with error %d\n", ret);
1093 /* ======================================================================== */
1095 int aptina_camera_module_s_ctrl(
1096 struct v4l2_subdev *sd,
1097 struct v4l2_control *ctrl)
1099 struct aptina_camera_module *cam_mod = to_aptina_camera_module(sd);
1100 struct v4l2_ext_control ext_ctrl[1];
1101 struct v4l2_ext_controls ext_ctrls;
1103 pltfrm_camera_module_pr_debug(&cam_mod->sd,
1104 "0x%x 0x%x\n", ctrl->id, ctrl->value);
1106 ext_ctrl[0].id = ctrl->id;
1107 ext_ctrl[0].value = ctrl->value;
1109 ext_ctrls.count = 1;
1110 ext_ctrls.controls = ext_ctrl;
1112 return aptina_camera_module_s_ext_ctrls(sd, &ext_ctrls);
1115 /* ======================================================================== */
1117 long aptina_camera_module_ioctl(struct v4l2_subdev *sd,
1121 struct aptina_camera_module *cam_mod = to_aptina_camera_module(sd);
1124 pltfrm_camera_module_pr_debug(&cam_mod->sd, "\n");
1126 if (cmd == RK_VIDIOC_SENSOR_MODE_DATA) {
1127 struct aptina_camera_module_timings aptina_timings;
1128 struct isp_supplemental_sensor_mode_data *timings =
1129 (struct isp_supplemental_sensor_mode_data *)arg;
1131 if (cam_mod->custom.g_timings)
1132 ret = cam_mod->custom.g_timings(cam_mod,
1137 if (IS_ERR_VALUE(ret)) {
1138 pltfrm_camera_module_pr_err(&cam_mod->sd,
1139 "failed with error %d\n", ret);
1143 timings->sensor_output_width =
1144 aptina_timings.sensor_output_width;
1145 timings->sensor_output_height =
1146 aptina_timings.sensor_output_height;
1147 timings->crop_horizontal_start =
1148 aptina_timings.crop_horizontal_start;
1149 timings->crop_vertical_start =
1150 aptina_timings.crop_vertical_start;
1151 timings->crop_horizontal_end =
1152 aptina_timings.crop_horizontal_end;
1153 timings->crop_vertical_end =
1154 aptina_timings.crop_vertical_end;
1155 timings->line_length_pck =
1156 aptina_timings.line_length_pck;
1157 timings->frame_length_lines =
1158 aptina_timings.frame_length_lines;
1159 timings->vt_pix_clk_freq_hz =
1160 aptina_timings.vt_pix_clk_freq_hz;
1161 timings->binning_factor_x =
1162 aptina_timings.binning_factor_x;
1163 timings->binning_factor_y =
1164 aptina_timings.binning_factor_y;
1165 timings->coarse_integration_time_max_margin =
1166 aptina_timings.coarse_integration_time_max_margin;
1167 timings->coarse_integration_time_min =
1168 aptina_timings.coarse_integration_time_min;
1169 timings->fine_integration_time_max_margin =
1170 aptina_timings.fine_integration_time_max_margin;
1171 timings->fine_integration_time_min =
1172 aptina_timings.fine_integration_time_min;
1174 if (cam_mod->custom.g_exposure_valid_frame)
1175 timings->exposure_valid_frame =
1176 cam_mod->custom.g_exposure_valid_frame(cam_mod);
1179 *timings->exp_time = cam_mod->exp_config.exp_time;
1180 *timings->gain = cam_mod->exp_config.gain;
1184 } else if (cmd == PLTFRM_CIFCAM_G_ITF_CFG) {
1185 struct pltfrm_cam_itf *itf_cfg = (struct pltfrm_cam_itf *)arg;
1186 struct aptina_camera_module_config *config;
1188 if (cam_mod->custom.num_configs <= 0) {
1189 pltfrm_camera_module_pr_err(&cam_mod->sd,
1190 "cam_mod->custom.num_configs is NULL, Get interface config failed!\n");
1194 if (IS_ERR_OR_NULL(cam_mod->active_config))
1195 config = &cam_mod->custom.configs[0];
1197 config = cam_mod->active_config;
1199 *itf_cfg = config->itf_cfg;
1200 pltfrm_camera_module_ioctl(sd, PLTFRM_CIFCAM_G_ITF_CFG, arg);
1202 } else if (cmd == PLTFRM_CIFCAM_ATTACH) {
1203 aptina_camera_module_init(cam_mod, &cam_mod->custom);
1204 pltfrm_camera_module_ioctl(sd, cmd, arg);
1205 return aptina_camera_module_attach(cam_mod);
1208 ret = pltfrm_camera_module_ioctl(sd, cmd, arg);
1212 /* Reconfigure bayer order according to xml's definition */
1213 static int aptina_camera_module_s_flip(struct aptina_camera_module *cam_mod)
1216 static int flip_mode;
1218 if (!cam_mod->custom.configs->bayer_order_alter_enble) {
1219 aptina_camera_module_pr_info(cam_mod,
1220 "not need to change bayer order!");
1224 mode = pltfrm_camera_module_get_flip_mirror(&cam_mod->sd);
1226 aptina_camera_module_pr_debug(cam_mod,
1227 "%s(%d): flip_mode is %d, mode is %d.\n",
1233 if (mode != flip_mode) {
1234 /* Reconfigure the compatible bayer order */
1237 /* Change all the bayter order of registered configs */
1238 for (i = 0; i < cam_mod->custom.num_configs; i++)
1239 cam_mod->custom.configs[i].frm_fmt.code =
1240 MEDIA_BUS_FMT_SGRBG10_1X10;
1244 for (i = 0; i < cam_mod->custom.num_configs; i++)
1245 cam_mod->custom.configs[i].frm_fmt.code =
1246 MEDIA_BUS_FMT_SRGGB10_1X10;
1250 for (i = 0; i < cam_mod->custom.num_configs; i++)
1251 cam_mod->custom.configs[i].frm_fmt.code =
1252 MEDIA_BUS_FMT_SBGGR10_1X10;
1256 for (i = 0; i < cam_mod->custom.num_configs; i++)
1257 cam_mod->custom.configs[i].frm_fmt.code =
1258 MEDIA_BUS_FMT_SGBRG10_1X10;
1271 int aptina_camera_module_get_flip_mirror(
1272 struct aptina_camera_module *cam_mod)
1274 return pltfrm_camera_module_get_flip_mirror(&cam_mod->sd);
1277 int aptina_camera_module_enum_frameintervals(
1278 struct v4l2_subdev *sd,
1279 struct v4l2_subdev_pad_config *cfg,
1280 struct v4l2_subdev_frame_interval_enum *file)
1282 struct aptina_camera_module *cam_mod = to_aptina_camera_module(sd);
1284 pltfrm_camera_module_pr_debug(&cam_mod->sd, "%d\n", file->index);
1286 if (file->index >= cam_mod->custom.num_configs)
1289 aptina_camera_module_s_flip(cam_mod);
1291 cam_mod->custom.configs[file->index].frm_fmt.code;
1292 file->width = cam_mod->custom.configs[file->index].frm_fmt.width;
1293 file->height = cam_mod->custom.configs[file->index].frm_fmt.height;
1294 file->interval.numerator = cam_mod->custom.
1295 configs[file->index].frm_intrvl.interval.numerator;
1296 file->interval.denominator = cam_mod->custom.
1297 configs[file->index].frm_intrvl.interval.denominator;
1301 /* ======================================================================== */
1303 int aptina_camera_module_write_reglist(
1304 struct aptina_camera_module *cam_mod,
1305 const struct aptina_camera_module_reg reglist[],
1308 return aptina_write_reglist(&cam_mod->sd, reglist, len);
1311 /* ======================================================================== */
1313 int aptina_camera_module_write_reg(
1314 struct aptina_camera_module *cam_mod,
1318 return aptina_write_i2c_reg(&cam_mod->sd, reg, val);
1321 /* ======================================================================== */
1323 int aptina_camera_module_read_reg(
1324 struct aptina_camera_module *cam_mod,
1329 return aptina_read_i2c_reg(&cam_mod->sd,
1330 data_length, reg, val);
1333 /* ======================================================================== */
1335 int aptina_camera_module_read_reg_table(
1336 struct aptina_camera_module *cam_mod,
1342 if (cam_mod->state == APTINA_CAMERA_MODULE_STREAMING)
1343 return aptina_read_i2c_reg(&cam_mod->sd,
1346 if (!IS_ERR_OR_NULL(cam_mod->active_config)) {
1348 i = cam_mod->active_config->reg_table_num_entries - 1;
1351 if (cam_mod->active_config->reg_table[i].reg == reg) {
1352 *val = cam_mod->active_config->reg_table[i].val;
1358 if (cam_mod->state == APTINA_CAMERA_MODULE_SW_STANDBY)
1359 return aptina_read_i2c_reg(&cam_mod->sd,
1365 /* ======================================================================== */
1367 int aptina_camera_module_init(struct aptina_camera_module *cam_mod,
1368 struct aptina_camera_module_custom_config *custom)
1372 pltfrm_camera_module_pr_debug(&cam_mod->sd, "\n");
1374 aptina_camera_module_reset(cam_mod);
1376 if (IS_ERR_OR_NULL(custom->start_streaming) ||
1377 IS_ERR_OR_NULL(custom->stop_streaming) ||
1378 IS_ERR_OR_NULL(custom->s_ctrl) ||
1379 IS_ERR_OR_NULL(custom->g_ctrl)) {
1380 pltfrm_camera_module_pr_err(&cam_mod->sd,
1381 "mandatory callback function is missing\n");
1386 ret = pltfrm_camera_module_init(&cam_mod->sd, &cam_mod->pltfm_data);
1387 if (IS_ERR_VALUE(ret))
1390 ret = pltfrm_camera_module_set_pin_state(&cam_mod->sd,
1391 PLTFRM_CAMERA_MODULE_PIN_PD,
1392 PLTFRM_CAMERA_MODULE_PIN_STATE_INACTIVE);
1393 ret = pltfrm_camera_module_set_pin_state(&cam_mod->sd,
1394 PLTFRM_CAMERA_MODULE_PIN_RESET,
1395 PLTFRM_CAMERA_MODULE_PIN_STATE_ACTIVE);
1396 if (IS_ERR_VALUE(ret)) {
1397 aptina_camera_module_release(cam_mod);
1401 *if (custom->check_camera_id) {
1402 * aptina_camera_module_s_power(&cam_mod->sd, 1);
1403 * ret = (custom->check_camera_id)(cam_mod);
1404 * aptina_camera_module_s_power(&cam_mod->sd, 0);
1407 *if (IS_ERR_VALUE(ret)) {
1408 * aptina_camera_module_release(cam_mod);
1415 pltfrm_camera_module_pr_err(&cam_mod->sd,
1416 "failed with error %d\n", ret);
1420 void aptina_camera_module_release(struct aptina_camera_module *cam_mod)
1422 pltfrm_camera_module_pr_debug(&cam_mod->sd, "\n");
1424 cam_mod->custom.configs = NULL;
1426 pltfrm_camera_module_release(&cam_mod->sd);
1427 v4l2_device_unregister_subdev(&cam_mod->sd);