2 * Support for GalaxyCore GC0310 VGA camera sensor.
4 * Copyright (c) 2013 Intel Corporation. All Rights Reserved.
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.
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.
17 #include <linux/module.h>
18 #include <linux/types.h>
19 #include <linux/kernel.h>
21 #include <linux/string.h>
22 #include <linux/errno.h>
23 #include <linux/init.h>
24 #include <linux/kmod.h>
25 #include <linux/device.h>
26 #include <linux/delay.h>
27 #include <linux/slab.h>
28 #include <linux/i2c.h>
29 #include <linux/gpio.h>
30 #include <linux/moduleparam.h>
31 #include <media/v4l2-device.h>
33 #include "../include/linux/atomisp_gmin_platform.h"
37 /* i2c read/write stuff */
38 static int gc0310_read_reg(struct i2c_client *client,
39 u16 data_length, u8 reg, u8 *val)
42 struct i2c_msg msg[2];
43 unsigned char data[1];
45 if (!client->adapter) {
46 dev_err(&client->dev, "%s error, no client->adapter\n",
51 if (data_length != GC0310_8BIT) {
52 dev_err(&client->dev, "%s error, invalid data length\n",
57 memset(msg, 0, sizeof(msg));
59 msg[0].addr = client->addr;
61 msg[0].len = I2C_MSG_LENGTH;
64 /* high byte goes out first */
65 data[0] = (u8)(reg & 0xff);
67 msg[1].addr = client->addr;
68 msg[1].len = data_length;
69 msg[1].flags = I2C_M_RD;
72 err = i2c_transfer(client->adapter, msg, 2);
77 "read from offset 0x%x error %d", reg, err);
82 /* high byte comes first */
83 if (data_length == GC0310_8BIT)
89 static int gc0310_i2c_write(struct i2c_client *client, u16 len, u8 *data)
92 const int num_msg = 1;
95 msg.addr = client->addr;
99 ret = i2c_transfer(client->adapter, &msg, 1);
101 return ret == num_msg ? 0 : -EIO;
104 static int gc0310_write_reg(struct i2c_client *client, u16 data_length,
108 unsigned char data[2] = {0};
109 u8 *wreg = (u8 *)data;
110 const u16 len = data_length + sizeof(u8); /* 8-bit address + data */
112 if (data_length != GC0310_8BIT) {
113 dev_err(&client->dev,
114 "%s error, invalid data_length\n", __func__);
118 /* high byte goes out first */
119 *wreg = (u8)(reg & 0xff);
121 if (data_length == GC0310_8BIT)
124 ret = gc0310_i2c_write(client, len, data);
126 dev_err(&client->dev,
127 "write error: wrote 0x%x to offset 0x%x error %d",
134 * gc0310_write_reg_array - Initializes a list of GC0310 registers
135 * @client: i2c driver client structure
136 * @reglist: list of registers to be written
138 * This function initializes a list of registers. When consecutive addresses
139 * are found in a row on the list, this function creates a buffer and sends
140 * consecutive data in a single i2c_transfer().
142 * __gc0310_flush_reg_array, __gc0310_buf_reg_array() and
143 * __gc0310_write_reg_is_consecutive() are internal functions to
144 * gc0310_write_reg_array_fast() and should be not used anywhere else.
148 static int __gc0310_flush_reg_array(struct i2c_client *client,
149 struct gc0310_write_ctrl *ctrl)
153 if (ctrl->index == 0)
156 size = sizeof(u8) + ctrl->index; /* 8-bit address + data */
157 ctrl->buffer.addr = (u8)(ctrl->buffer.addr);
160 return gc0310_i2c_write(client, size, (u8 *)&ctrl->buffer);
163 static int __gc0310_buf_reg_array(struct i2c_client *client,
164 struct gc0310_write_ctrl *ctrl,
165 const struct gc0310_reg *next)
169 switch (next->type) {
172 ctrl->buffer.data[ctrl->index] = (u8)next->val;
178 /* When first item is added, we need to store its starting address */
179 if (ctrl->index == 0)
180 ctrl->buffer.addr = next->reg;
185 * Buffer cannot guarantee free space for u32? Better flush it to avoid
186 * possible lack of memory for next item.
188 if (ctrl->index + sizeof(u8) >= GC0310_MAX_WRITE_BUF_SIZE)
189 return __gc0310_flush_reg_array(client, ctrl);
194 static int __gc0310_write_reg_is_consecutive(struct i2c_client *client,
195 struct gc0310_write_ctrl *ctrl,
196 const struct gc0310_reg *next)
198 if (ctrl->index == 0)
201 return ctrl->buffer.addr + ctrl->index == next->reg;
204 static int gc0310_write_reg_array(struct i2c_client *client,
205 const struct gc0310_reg *reglist)
207 const struct gc0310_reg *next = reglist;
208 struct gc0310_write_ctrl ctrl;
212 for (; next->type != GC0310_TOK_TERM; next++) {
213 switch (next->type & GC0310_TOK_MASK) {
214 case GC0310_TOK_DELAY:
215 err = __gc0310_flush_reg_array(client, &ctrl);
222 * If next address is not consecutive, data needs to be
223 * flushed before proceed.
225 if (!__gc0310_write_reg_is_consecutive(client, &ctrl,
227 err = __gc0310_flush_reg_array(client, &ctrl);
231 err = __gc0310_buf_reg_array(client, &ctrl, next);
233 dev_err(&client->dev, "%s: write error, aborted\n",
241 return __gc0310_flush_reg_array(client, &ctrl);
243 static int gc0310_g_focal(struct v4l2_subdev *sd, s32 *val)
245 *val = (GC0310_FOCAL_LENGTH_NUM << 16) | GC0310_FOCAL_LENGTH_DEM;
249 static int gc0310_g_fnumber(struct v4l2_subdev *sd, s32 *val)
251 /*const f number for imx*/
252 *val = (GC0310_F_NUMBER_DEFAULT_NUM << 16) | GC0310_F_NUMBER_DEM;
256 static int gc0310_g_fnumber_range(struct v4l2_subdev *sd, s32 *val)
258 *val = (GC0310_F_NUMBER_DEFAULT_NUM << 24) |
259 (GC0310_F_NUMBER_DEM << 16) |
260 (GC0310_F_NUMBER_DEFAULT_NUM << 8) | GC0310_F_NUMBER_DEM;
264 static int gc0310_g_bin_factor_x(struct v4l2_subdev *sd, s32 *val)
266 struct gc0310_device *dev = to_gc0310_sensor(sd);
268 *val = gc0310_res[dev->fmt_idx].bin_factor_x;
273 static int gc0310_g_bin_factor_y(struct v4l2_subdev *sd, s32 *val)
275 struct gc0310_device *dev = to_gc0310_sensor(sd);
277 *val = gc0310_res[dev->fmt_idx].bin_factor_y;
282 static int gc0310_get_intg_factor(struct i2c_client *client,
283 struct camera_mipi_info *info,
284 const struct gc0310_resolution *res)
286 struct v4l2_subdev *sd = i2c_get_clientdata(client);
287 struct gc0310_device *dev = to_gc0310_sensor(sd);
288 struct atomisp_sensor_mode_data *buf = &info->data;
292 unsigned int hori_blanking;
293 unsigned int vert_blanking;
294 unsigned int sh_delay;
299 /* pixel clock calculattion */
300 dev->vt_pix_clk_freq_mhz = 14400000; // 16.8MHz
301 buf->vt_pix_clk_freq_mhz = dev->vt_pix_clk_freq_mhz;
302 pr_info("vt_pix_clk_freq_mhz=%d\n", buf->vt_pix_clk_freq_mhz);
304 /* get integration time */
305 buf->coarse_integration_time_min = GC0310_COARSE_INTG_TIME_MIN;
306 buf->coarse_integration_time_max_margin =
307 GC0310_COARSE_INTG_TIME_MAX_MARGIN;
309 buf->fine_integration_time_min = GC0310_FINE_INTG_TIME_MIN;
310 buf->fine_integration_time_max_margin =
311 GC0310_FINE_INTG_TIME_MAX_MARGIN;
313 buf->fine_integration_time_def = GC0310_FINE_INTG_TIME_MIN;
314 buf->read_mode = res->bin_mode;
316 /* get the cropping and output resolution to ISP for this mode. */
317 /* Getting crop_horizontal_start */
318 ret = gc0310_read_reg(client, GC0310_8BIT,
319 GC0310_H_CROP_START_H, ®_val);
322 val = (reg_val & 0xFF) << 8;
323 ret = gc0310_read_reg(client, GC0310_8BIT,
324 GC0310_H_CROP_START_L, ®_val);
327 buf->crop_horizontal_start = val | (reg_val & 0xFF);
328 pr_info("crop_horizontal_start=%d\n", buf->crop_horizontal_start);
330 /* Getting crop_vertical_start */
331 ret = gc0310_read_reg(client, GC0310_8BIT,
332 GC0310_V_CROP_START_H, ®_val);
335 val = (reg_val & 0xFF) << 8;
336 ret = gc0310_read_reg(client, GC0310_8BIT,
337 GC0310_V_CROP_START_L, ®_val);
340 buf->crop_vertical_start = val | (reg_val & 0xFF);
341 pr_info("crop_vertical_start=%d\n", buf->crop_vertical_start);
343 /* Getting output_width */
344 ret = gc0310_read_reg(client, GC0310_8BIT,
345 GC0310_H_OUTSIZE_H, ®_val);
348 val = (reg_val & 0xFF) << 8;
349 ret = gc0310_read_reg(client, GC0310_8BIT,
350 GC0310_H_OUTSIZE_L, ®_val);
353 buf->output_width = val | (reg_val & 0xFF);
354 pr_info("output_width=%d\n", buf->output_width);
356 /* Getting output_height */
357 ret = gc0310_read_reg(client, GC0310_8BIT,
358 GC0310_V_OUTSIZE_H, ®_val);
361 val = (reg_val & 0xFF) << 8;
362 ret = gc0310_read_reg(client, GC0310_8BIT,
363 GC0310_V_OUTSIZE_L, ®_val);
366 buf->output_height = val | (reg_val & 0xFF);
367 pr_info("output_height=%d\n", buf->output_height);
369 buf->crop_horizontal_end = buf->crop_horizontal_start + buf->output_width - 1;
370 buf->crop_vertical_end = buf->crop_vertical_start + buf->output_height - 1;
371 pr_info("crop_horizontal_end=%d\n", buf->crop_horizontal_end);
372 pr_info("crop_vertical_end=%d\n", buf->crop_vertical_end);
374 /* Getting line_length_pck */
375 ret = gc0310_read_reg(client, GC0310_8BIT,
376 GC0310_H_BLANKING_H, ®_val);
379 val = (reg_val & 0xFF) << 8;
380 ret = gc0310_read_reg(client, GC0310_8BIT,
381 GC0310_H_BLANKING_L, ®_val);
384 hori_blanking = val | (reg_val & 0xFF);
385 ret = gc0310_read_reg(client, GC0310_8BIT,
386 GC0310_SH_DELAY, ®_val);
390 buf->line_length_pck = buf->output_width + hori_blanking + sh_delay + 4;
391 pr_info("hori_blanking=%d sh_delay=%d line_length_pck=%d\n", hori_blanking, sh_delay, buf->line_length_pck);
393 /* Getting frame_length_lines */
394 ret = gc0310_read_reg(client, GC0310_8BIT,
395 GC0310_V_BLANKING_H, ®_val);
398 val = (reg_val & 0xFF) << 8;
399 ret = gc0310_read_reg(client, GC0310_8BIT,
400 GC0310_V_BLANKING_L, ®_val);
403 vert_blanking = val | (reg_val & 0xFF);
404 buf->frame_length_lines = buf->output_height + vert_blanking;
405 pr_info("vert_blanking=%d frame_length_lines=%d\n", vert_blanking, buf->frame_length_lines);
407 buf->binning_factor_x = res->bin_factor_x ?
408 res->bin_factor_x : 1;
409 buf->binning_factor_y = res->bin_factor_y ?
410 res->bin_factor_y : 1;
414 static int gc0310_set_gain(struct v4l2_subdev *sd, int gain)
417 struct i2c_client *client = v4l2_get_subdevdata(sd);
426 if (gain >= 0x20 && gain < 0x40) {
427 again = 0x0; /* sqrt(2) */
430 again = 0x2; /* 2 * sqrt(2) */
434 pr_info("gain=0x%x again=0x%x dgain=0x%x\n", gain, again, dgain);
436 /* set analog gain */
437 ret = gc0310_write_reg(client, GC0310_8BIT,
438 GC0310_AGC_ADJ, again);
442 /* set digital gain */
443 ret = gc0310_write_reg(client, GC0310_8BIT,
444 GC0310_DGC_ADJ, dgain);
451 static int __gc0310_set_exposure(struct v4l2_subdev *sd, int coarse_itg,
452 int gain, int digitgain)
455 struct i2c_client *client = v4l2_get_subdevdata(sd);
458 pr_info("coarse_itg=%d gain=%d digitgain=%d\n", coarse_itg, gain, digitgain);
461 ret = gc0310_write_reg(client, GC0310_8BIT,
462 GC0310_AEC_PK_EXPO_L,
467 ret = gc0310_write_reg(client, GC0310_8BIT,
468 GC0310_AEC_PK_EXPO_H,
469 (coarse_itg >> 8) & 0x0f);
473 ret = gc0310_set_gain(sd, gain);
480 static int gc0310_set_exposure(struct v4l2_subdev *sd, int exposure,
481 int gain, int digitgain)
483 struct gc0310_device *dev = to_gc0310_sensor(sd);
486 mutex_lock(&dev->input_lock);
487 ret = __gc0310_set_exposure(sd, exposure, gain, digitgain);
488 mutex_unlock(&dev->input_lock);
493 static long gc0310_s_exposure(struct v4l2_subdev *sd,
494 struct atomisp_exposure *exposure)
496 int exp = exposure->integration_time[0];
497 int gain = exposure->gain[0];
498 int digitgain = exposure->gain[1];
500 /* we should not accept the invalid value below. */
502 struct i2c_client *client = v4l2_get_subdevdata(sd);
503 v4l2_err(client, "%s: invalid value\n", __func__);
507 return gc0310_set_exposure(sd, exp, gain, digitgain);
511 static int gc0310_v_flip(struct v4l2_subdev *sd, s32 value)
517 static int gc0310_h_flip(struct v4l2_subdev *sd, s32 value)
522 static long gc0310_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg)
526 case ATOMISP_IOC_S_EXPOSURE:
527 return gc0310_s_exposure(sd, arg);
534 /* This returns the exposure time being used. This should only be used
535 * for filling in EXIF data, not for actual image processing.
537 static int gc0310_q_exposure(struct v4l2_subdev *sd, s32 *value)
539 struct i2c_client *client = v4l2_get_subdevdata(sd);
544 ret = gc0310_read_reg(client, GC0310_8BIT,
545 GC0310_AEC_PK_EXPO_L,
551 ret = gc0310_read_reg(client, GC0310_8BIT,
552 GC0310_AEC_PK_EXPO_H,
557 *value = *value + (reg_v << 8);
562 static int gc0310_s_ctrl(struct v4l2_ctrl *ctrl)
564 struct gc0310_device *dev =
565 container_of(ctrl->handler, struct gc0310_device, ctrl_handler);
566 struct i2c_client *client = v4l2_get_subdevdata(&dev->sd);
571 dev_dbg(&client->dev, "%s: CID_VFLIP:%d.\n",
572 __func__, ctrl->val);
573 ret = gc0310_v_flip(&dev->sd, ctrl->val);
576 dev_dbg(&client->dev, "%s: CID_HFLIP:%d.\n",
577 __func__, ctrl->val);
578 ret = gc0310_h_flip(&dev->sd, ctrl->val);
586 static int gc0310_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
588 struct gc0310_device *dev =
589 container_of(ctrl->handler, struct gc0310_device, ctrl_handler);
593 case V4L2_CID_EXPOSURE_ABSOLUTE:
594 ret = gc0310_q_exposure(&dev->sd, &ctrl->val);
596 case V4L2_CID_FOCAL_ABSOLUTE:
597 ret = gc0310_g_focal(&dev->sd, &ctrl->val);
599 case V4L2_CID_FNUMBER_ABSOLUTE:
600 ret = gc0310_g_fnumber(&dev->sd, &ctrl->val);
602 case V4L2_CID_FNUMBER_RANGE:
603 ret = gc0310_g_fnumber_range(&dev->sd, &ctrl->val);
605 case V4L2_CID_BIN_FACTOR_HORZ:
606 ret = gc0310_g_bin_factor_x(&dev->sd, &ctrl->val);
608 case V4L2_CID_BIN_FACTOR_VERT:
609 ret = gc0310_g_bin_factor_y(&dev->sd, &ctrl->val);
618 static const struct v4l2_ctrl_ops ctrl_ops = {
619 .s_ctrl = gc0310_s_ctrl,
620 .g_volatile_ctrl = gc0310_g_volatile_ctrl
623 struct v4l2_ctrl_config gc0310_controls[] = {
626 .id = V4L2_CID_EXPOSURE_ABSOLUTE,
627 .type = V4L2_CTRL_TYPE_INTEGER,
637 .id = V4L2_CID_VFLIP,
638 .type = V4L2_CTRL_TYPE_BOOLEAN,
647 .id = V4L2_CID_HFLIP,
648 .type = V4L2_CTRL_TYPE_BOOLEAN,
657 .id = V4L2_CID_FOCAL_ABSOLUTE,
658 .type = V4L2_CTRL_TYPE_INTEGER,
659 .name = "focal length",
660 .min = GC0310_FOCAL_LENGTH_DEFAULT,
661 .max = GC0310_FOCAL_LENGTH_DEFAULT,
663 .def = GC0310_FOCAL_LENGTH_DEFAULT,
668 .id = V4L2_CID_FNUMBER_ABSOLUTE,
669 .type = V4L2_CTRL_TYPE_INTEGER,
671 .min = GC0310_F_NUMBER_DEFAULT,
672 .max = GC0310_F_NUMBER_DEFAULT,
674 .def = GC0310_F_NUMBER_DEFAULT,
679 .id = V4L2_CID_FNUMBER_RANGE,
680 .type = V4L2_CTRL_TYPE_INTEGER,
681 .name = "f-number range",
682 .min = GC0310_F_NUMBER_RANGE,
683 .max = GC0310_F_NUMBER_RANGE,
685 .def = GC0310_F_NUMBER_RANGE,
690 .id = V4L2_CID_BIN_FACTOR_HORZ,
691 .type = V4L2_CTRL_TYPE_INTEGER,
692 .name = "horizontal binning factor",
694 .max = GC0310_BIN_FACTOR_MAX,
701 .id = V4L2_CID_BIN_FACTOR_VERT,
702 .type = V4L2_CTRL_TYPE_INTEGER,
703 .name = "vertical binning factor",
705 .max = GC0310_BIN_FACTOR_MAX,
712 static int gc0310_init(struct v4l2_subdev *sd)
715 struct i2c_client *client = v4l2_get_subdevdata(sd);
716 struct gc0310_device *dev = to_gc0310_sensor(sd);
718 pr_info("%s S\n", __func__);
719 mutex_lock(&dev->input_lock);
721 /* set inital registers */
722 ret = gc0310_write_reg_array(client, gc0310_reset_register);
724 /* restore settings */
725 gc0310_res = gc0310_res_preview;
726 N_RES = N_RES_PREVIEW;
728 mutex_unlock(&dev->input_lock);
730 pr_info("%s E\n", __func__);
734 static int power_ctrl(struct v4l2_subdev *sd, bool flag)
737 struct gc0310_device *dev = to_gc0310_sensor(sd);
738 if (!dev || !dev->platform_data)
741 /* Non-gmin platforms use the legacy callback */
742 if (dev->platform_data->power_ctrl)
743 return dev->platform_data->power_ctrl(sd, flag);
746 /* The upstream module driver (written to Crystal
747 * Cove) had this logic to pulse the rails low first.
748 * This appears to break things on the MRD7 with the
751 * ret = dev->platform_data->v1p8_ctrl(sd, 0);
752 * ret |= dev->platform_data->v2p8_ctrl(sd, 0);
755 ret |= dev->platform_data->v1p8_ctrl(sd, 1);
756 ret |= dev->platform_data->v2p8_ctrl(sd, 1);
757 usleep_range(10000, 15000);
761 ret |= dev->platform_data->v1p8_ctrl(sd, 0);
762 ret |= dev->platform_data->v2p8_ctrl(sd, 0);
767 static int gpio_ctrl(struct v4l2_subdev *sd, bool flag)
770 struct gc0310_device *dev = to_gc0310_sensor(sd);
772 if (!dev || !dev->platform_data)
775 /* Non-gmin platforms use the legacy callback */
776 if (dev->platform_data->gpio_ctrl)
777 return dev->platform_data->gpio_ctrl(sd, flag);
779 /* GPIO0 == "reset" (active low), GPIO1 == "power down" */
781 /* Pulse reset, then release power down */
782 ret = dev->platform_data->gpio0_ctrl(sd, 0);
783 usleep_range(5000, 10000);
784 ret |= dev->platform_data->gpio0_ctrl(sd, 1);
785 usleep_range(10000, 15000);
786 ret |= dev->platform_data->gpio1_ctrl(sd, 0);
787 usleep_range(10000, 15000);
789 ret = dev->platform_data->gpio1_ctrl(sd, 1);
790 ret |= dev->platform_data->gpio0_ctrl(sd, 0);
796 static int power_down(struct v4l2_subdev *sd);
798 static int power_up(struct v4l2_subdev *sd)
800 struct gc0310_device *dev = to_gc0310_sensor(sd);
801 struct i2c_client *client = v4l2_get_subdevdata(sd);
804 pr_info("%s S\n", __func__);
805 if (!dev->platform_data) {
806 dev_err(&client->dev,
807 "no camera_sensor_platform_data");
812 ret = power_ctrl(sd, 1);
816 /* flis clock control */
817 ret = dev->platform_data->flisclk_ctrl(sd, 1);
822 ret = gpio_ctrl(sd, 1);
824 ret = gpio_ctrl(sd, 1);
831 pr_info("%s E\n", __func__);
835 dev->platform_data->flisclk_ctrl(sd, 0);
839 dev_err(&client->dev, "sensor power-up failed\n");
844 static int power_down(struct v4l2_subdev *sd)
846 struct gc0310_device *dev = to_gc0310_sensor(sd);
847 struct i2c_client *client = v4l2_get_subdevdata(sd);
850 if (!dev->platform_data) {
851 dev_err(&client->dev,
852 "no camera_sensor_platform_data");
857 ret = gpio_ctrl(sd, 0);
859 ret = gpio_ctrl(sd, 0);
861 dev_err(&client->dev, "gpio failed 2\n");
864 ret = dev->platform_data->flisclk_ctrl(sd, 0);
866 dev_err(&client->dev, "flisclk failed\n");
869 ret = power_ctrl(sd, 0);
871 dev_err(&client->dev, "vprog failed.\n");
876 static int gc0310_s_power(struct v4l2_subdev *sd, int on)
880 return power_down(sd);
884 return gc0310_init(sd);
890 * distance - calculate the distance
895 * Get the gap between resolution and w/h.
896 * res->width/height smaller than w/h wouldn't be considered.
897 * Returns the value of gap or -1 if fail.
899 #define LARGEST_ALLOWED_RATIO_MISMATCH 800
900 static int distance(struct gc0310_resolution *res, u32 w, u32 h)
902 unsigned int w_ratio = (res->width << 13) / w;
903 unsigned int h_ratio;
908 h_ratio = (res->height << 13) / h;
911 match = abs(((w_ratio << 13) / h_ratio) - ((int)8192));
913 if ((w_ratio < (int)8192) || (h_ratio < (int)8192) ||
914 (match > LARGEST_ALLOWED_RATIO_MISMATCH))
917 return w_ratio + h_ratio;
920 /* Return the nearest higher resolution index */
921 static int nearest_resolution_index(int w, int h)
926 int min_dist = INT_MAX;
927 struct gc0310_resolution *tmp_res = NULL;
929 for (i = 0; i < N_RES; i++) {
930 tmp_res = &gc0310_res[i];
931 dist = distance(tmp_res, w, h);
934 if (dist < min_dist) {
943 static int get_resolution_index(int w, int h)
947 for (i = 0; i < N_RES; i++) {
948 if (w != gc0310_res[i].width)
950 if (h != gc0310_res[i].height)
960 /* TODO: remove it. */
961 static int startup(struct v4l2_subdev *sd)
963 struct gc0310_device *dev = to_gc0310_sensor(sd);
964 struct i2c_client *client = v4l2_get_subdevdata(sd);
967 pr_info("%s S\n", __func__);
969 ret = gc0310_write_reg_array(client, gc0310_res[dev->fmt_idx].regs);
971 dev_err(&client->dev, "gc0310 write register err.\n");
975 pr_info("%s E\n", __func__);
979 static int gc0310_set_fmt(struct v4l2_subdev *sd,
980 struct v4l2_subdev_pad_config *cfg,
981 struct v4l2_subdev_format *format)
983 struct v4l2_mbus_framefmt *fmt = &format->format;
984 struct gc0310_device *dev = to_gc0310_sensor(sd);
985 struct i2c_client *client = v4l2_get_subdevdata(sd);
986 struct camera_mipi_info *gc0310_info = NULL;
989 pr_info("%s S\n", __func__);
997 gc0310_info = v4l2_get_subdev_hostdata(sd);
1001 mutex_lock(&dev->input_lock);
1003 idx = nearest_resolution_index(fmt->width, fmt->height);
1005 /* return the largest resolution */
1006 fmt->width = gc0310_res[N_RES - 1].width;
1007 fmt->height = gc0310_res[N_RES - 1].height;
1009 fmt->width = gc0310_res[idx].width;
1010 fmt->height = gc0310_res[idx].height;
1012 fmt->code = MEDIA_BUS_FMT_SGRBG8_1X8;
1014 if (format->which == V4L2_SUBDEV_FORMAT_TRY) {
1015 cfg->try_fmt = *fmt;
1016 mutex_unlock(&dev->input_lock);
1020 dev->fmt_idx = get_resolution_index(fmt->width, fmt->height);
1021 if (dev->fmt_idx == -1) {
1022 dev_err(&client->dev, "get resolution fail\n");
1023 mutex_unlock(&dev->input_lock);
1027 printk("%s: before gc0310_write_reg_array %s\n", __FUNCTION__,
1028 gc0310_res[dev->fmt_idx].desc);
1031 dev_err(&client->dev, "gc0310 startup err\n");
1035 ret = gc0310_get_intg_factor(client, gc0310_info,
1036 &gc0310_res[dev->fmt_idx]);
1038 dev_err(&client->dev, "failed to get integration_factor\n");
1042 pr_info("%s E\n", __func__);
1044 mutex_unlock(&dev->input_lock);
1048 static int gc0310_get_fmt(struct v4l2_subdev *sd,
1049 struct v4l2_subdev_pad_config *cfg,
1050 struct v4l2_subdev_format *format)
1052 struct v4l2_mbus_framefmt *fmt = &format->format;
1053 struct gc0310_device *dev = to_gc0310_sensor(sd);
1061 fmt->width = gc0310_res[dev->fmt_idx].width;
1062 fmt->height = gc0310_res[dev->fmt_idx].height;
1063 fmt->code = MEDIA_BUS_FMT_SGRBG8_1X8;
1068 static int gc0310_detect(struct i2c_client *client)
1070 struct i2c_adapter *adapter = client->adapter;
1075 pr_info("%s S\n", __func__);
1076 if (!i2c_check_functionality(adapter, I2C_FUNC_I2C))
1079 ret = gc0310_read_reg(client, GC0310_8BIT,
1080 GC0310_SC_CMMN_CHIP_ID_H, &high);
1082 dev_err(&client->dev, "read sensor_id_high failed\n");
1085 ret = gc0310_read_reg(client, GC0310_8BIT,
1086 GC0310_SC_CMMN_CHIP_ID_L, &low);
1088 dev_err(&client->dev, "read sensor_id_low failed\n");
1091 id = ((((u16) high) << 8) | (u16) low);
1092 pr_info("sensor ID = 0x%x\n", id);
1094 if (id != GC0310_ID) {
1095 dev_err(&client->dev, "sensor ID error, read id = 0x%x, target id = 0x%x\n", id, GC0310_ID);
1099 dev_dbg(&client->dev, "detect gc0310 success\n");
1101 pr_info("%s E\n", __func__);
1106 static int gc0310_s_stream(struct v4l2_subdev *sd, int enable)
1108 struct gc0310_device *dev = to_gc0310_sensor(sd);
1109 struct i2c_client *client = v4l2_get_subdevdata(sd);
1112 pr_info("%s S enable=%d\n", __func__, enable);
1113 mutex_lock(&dev->input_lock);
1116 /* enable per frame MIPI and sensor ctrl reset */
1117 ret = gc0310_write_reg(client, GC0310_8BIT,
1120 mutex_unlock(&dev->input_lock);
1125 ret = gc0310_write_reg(client, GC0310_8BIT,
1126 GC0310_RESET_RELATED, GC0310_REGISTER_PAGE_3);
1128 mutex_unlock(&dev->input_lock);
1132 ret = gc0310_write_reg(client, GC0310_8BIT, GC0310_SW_STREAM,
1133 enable ? GC0310_START_STREAMING :
1134 GC0310_STOP_STREAMING);
1136 mutex_unlock(&dev->input_lock);
1140 ret = gc0310_write_reg(client, GC0310_8BIT,
1141 GC0310_RESET_RELATED, GC0310_REGISTER_PAGE_0);
1143 mutex_unlock(&dev->input_lock);
1147 mutex_unlock(&dev->input_lock);
1148 pr_info("%s E\n", __func__);
1153 static int gc0310_s_config(struct v4l2_subdev *sd,
1154 int irq, void *platform_data)
1156 struct gc0310_device *dev = to_gc0310_sensor(sd);
1157 struct i2c_client *client = v4l2_get_subdevdata(sd);
1160 pr_info("%s S\n", __func__);
1164 dev->platform_data =
1165 (struct camera_sensor_platform_data *)platform_data;
1167 mutex_lock(&dev->input_lock);
1168 if (dev->platform_data->platform_init) {
1169 ret = dev->platform_data->platform_init(client);
1171 dev_err(&client->dev, "platform init err\n");
1172 goto platform_init_failed;
1175 /* power off the module, then power on it in future
1176 * as first power on by board may not fulfill the
1177 * power on sequqence needed by the module
1179 ret = power_down(sd);
1181 dev_err(&client->dev, "gc0310 power-off err.\n");
1182 goto fail_power_off;
1187 dev_err(&client->dev, "gc0310 power-up err.\n");
1191 ret = dev->platform_data->csi_cfg(sd, 1);
1195 /* config & detect sensor */
1196 ret = gc0310_detect(client);
1198 dev_err(&client->dev, "gc0310_detect err s_config.\n");
1202 /* turn off sensor, after probed */
1203 ret = power_down(sd);
1205 dev_err(&client->dev, "gc0310 power-off err.\n");
1208 mutex_unlock(&dev->input_lock);
1210 pr_info("%s E\n", __func__);
1214 dev->platform_data->csi_cfg(sd, 0);
1217 dev_err(&client->dev, "sensor power-gating failed\n");
1219 if (dev->platform_data->platform_deinit)
1220 dev->platform_data->platform_deinit();
1221 platform_init_failed:
1222 mutex_unlock(&dev->input_lock);
1226 static int gc0310_g_parm(struct v4l2_subdev *sd,
1227 struct v4l2_streamparm *param)
1229 struct gc0310_device *dev = to_gc0310_sensor(sd);
1230 struct i2c_client *client = v4l2_get_subdevdata(sd);
1235 if (param->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1236 dev_err(&client->dev, "unsupported buffer type.\n");
1240 memset(param, 0, sizeof(*param));
1241 param->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1243 if (dev->fmt_idx >= 0 && dev->fmt_idx < N_RES) {
1244 param->parm.capture.capability = V4L2_CAP_TIMEPERFRAME;
1245 param->parm.capture.timeperframe.numerator = 1;
1246 param->parm.capture.capturemode = dev->run_mode;
1247 param->parm.capture.timeperframe.denominator =
1248 gc0310_res[dev->fmt_idx].fps;
1253 static int gc0310_s_parm(struct v4l2_subdev *sd,
1254 struct v4l2_streamparm *param)
1256 struct gc0310_device *dev = to_gc0310_sensor(sd);
1257 dev->run_mode = param->parm.capture.capturemode;
1259 mutex_lock(&dev->input_lock);
1260 switch (dev->run_mode) {
1262 gc0310_res = gc0310_res_video;
1263 N_RES = N_RES_VIDEO;
1265 case CI_MODE_STILL_CAPTURE:
1266 gc0310_res = gc0310_res_still;
1267 N_RES = N_RES_STILL;
1270 gc0310_res = gc0310_res_preview;
1271 N_RES = N_RES_PREVIEW;
1273 mutex_unlock(&dev->input_lock);
1277 static int gc0310_g_frame_interval(struct v4l2_subdev *sd,
1278 struct v4l2_subdev_frame_interval *interval)
1280 struct gc0310_device *dev = to_gc0310_sensor(sd);
1282 interval->interval.numerator = 1;
1283 interval->interval.denominator = gc0310_res[dev->fmt_idx].fps;
1288 static int gc0310_enum_mbus_code(struct v4l2_subdev *sd,
1289 struct v4l2_subdev_pad_config *cfg,
1290 struct v4l2_subdev_mbus_code_enum *code)
1292 if (code->index >= MAX_FMTS)
1295 code->code = MEDIA_BUS_FMT_SGRBG8_1X8;
1299 static int gc0310_enum_frame_size(struct v4l2_subdev *sd,
1300 struct v4l2_subdev_pad_config *cfg,
1301 struct v4l2_subdev_frame_size_enum *fse)
1303 int index = fse->index;
1308 fse->min_width = gc0310_res[index].width;
1309 fse->min_height = gc0310_res[index].height;
1310 fse->max_width = gc0310_res[index].width;
1311 fse->max_height = gc0310_res[index].height;
1318 static int gc0310_g_skip_frames(struct v4l2_subdev *sd, u32 *frames)
1320 struct gc0310_device *dev = to_gc0310_sensor(sd);
1322 mutex_lock(&dev->input_lock);
1323 *frames = gc0310_res[dev->fmt_idx].skip_frames;
1324 mutex_unlock(&dev->input_lock);
1329 static const struct v4l2_subdev_sensor_ops gc0310_sensor_ops = {
1330 .g_skip_frames = gc0310_g_skip_frames,
1333 static const struct v4l2_subdev_video_ops gc0310_video_ops = {
1334 .s_stream = gc0310_s_stream,
1335 .g_parm = gc0310_g_parm,
1336 .s_parm = gc0310_s_parm,
1337 .g_frame_interval = gc0310_g_frame_interval,
1340 static const struct v4l2_subdev_core_ops gc0310_core_ops = {
1341 .s_power = gc0310_s_power,
1342 .ioctl = gc0310_ioctl,
1345 static const struct v4l2_subdev_pad_ops gc0310_pad_ops = {
1346 .enum_mbus_code = gc0310_enum_mbus_code,
1347 .enum_frame_size = gc0310_enum_frame_size,
1348 .get_fmt = gc0310_get_fmt,
1349 .set_fmt = gc0310_set_fmt,
1352 static const struct v4l2_subdev_ops gc0310_ops = {
1353 .core = &gc0310_core_ops,
1354 .video = &gc0310_video_ops,
1355 .pad = &gc0310_pad_ops,
1356 .sensor = &gc0310_sensor_ops,
1359 static int gc0310_remove(struct i2c_client *client)
1361 struct v4l2_subdev *sd = i2c_get_clientdata(client);
1362 struct gc0310_device *dev = to_gc0310_sensor(sd);
1363 dev_dbg(&client->dev, "gc0310_remove...\n");
1365 if (dev->platform_data->platform_deinit)
1366 dev->platform_data->platform_deinit();
1368 dev->platform_data->csi_cfg(sd, 0);
1370 v4l2_device_unregister_subdev(sd);
1371 media_entity_cleanup(&dev->sd.entity);
1372 v4l2_ctrl_handler_free(&dev->ctrl_handler);
1378 static int gc0310_probe(struct i2c_client *client,
1379 const struct i2c_device_id *id)
1381 struct gc0310_device *dev;
1386 pr_info("%s S\n", __func__);
1387 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1389 dev_err(&client->dev, "out of memory\n");
1393 mutex_init(&dev->input_lock);
1396 v4l2_i2c_subdev_init(&(dev->sd), client, &gc0310_ops);
1398 if (ACPI_COMPANION(&client->dev))
1399 pdata = gmin_camera_platform_data(&dev->sd,
1400 ATOMISP_INPUT_FORMAT_RAW_8,
1401 atomisp_bayer_order_grbg);
1403 pdata = client->dev.platform_data;
1410 ret = gc0310_s_config(&dev->sd, client->irq, pdata);
1414 ret = atomisp_register_i2c_module(&dev->sd, pdata, RAW_CAMERA);
1418 dev->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
1419 dev->pad.flags = MEDIA_PAD_FL_SOURCE;
1420 dev->format.code = MEDIA_BUS_FMT_SGRBG8_1X8;
1421 dev->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
1423 v4l2_ctrl_handler_init(&dev->ctrl_handler,
1424 ARRAY_SIZE(gc0310_controls));
1426 gc0310_remove(client);
1430 for (i = 0; i < ARRAY_SIZE(gc0310_controls); i++)
1431 v4l2_ctrl_new_custom(&dev->ctrl_handler, &gc0310_controls[i],
1434 if (dev->ctrl_handler.error) {
1435 gc0310_remove(client);
1436 return dev->ctrl_handler.error;
1439 /* Use same lock for controls as for everything else. */
1440 dev->ctrl_handler.lock = &dev->input_lock;
1441 dev->sd.ctrl_handler = &dev->ctrl_handler;
1443 ret = media_entity_pads_init(&dev->sd.entity, 1, &dev->pad);
1445 gc0310_remove(client);
1447 pr_info("%s E\n", __func__);
1450 v4l2_device_unregister_subdev(&dev->sd);
1455 static const struct acpi_device_id gc0310_acpi_match[] = {
1461 MODULE_DEVICE_TABLE(acpi, gc0310_acpi_match);
1463 MODULE_DEVICE_TABLE(i2c, gc0310_id);
1464 static struct i2c_driver gc0310_driver = {
1466 .name = GC0310_NAME,
1467 .acpi_match_table = ACPI_PTR(gc0310_acpi_match),
1469 .probe = gc0310_probe,
1470 .remove = gc0310_remove,
1471 .id_table = gc0310_id,
1474 static int init_gc0310(void)
1476 return i2c_add_driver(&gc0310_driver);
1479 static void exit_gc0310(void)
1482 i2c_del_driver(&gc0310_driver);
1485 module_init(init_gc0310);
1486 module_exit(exit_gc0310);
1488 MODULE_AUTHOR("Lai, Angie <angie.lai@intel.com>");
1489 MODULE_DESCRIPTION("A low-level driver for GalaxyCore GC0310 sensors");
1490 MODULE_LICENSE("GPL");