1 // SPDX-License-Identifier: GPL-2.0
3 * Support for GalaxyCore GC0310 VGA camera sensor.
5 * Copyright (c) 2013 Intel Corporation. All Rights Reserved.
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License version
9 * 2 as published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
18 #include <linux/module.h>
19 #include <linux/types.h>
20 #include <linux/kernel.h>
22 #include <linux/string.h>
23 #include <linux/errno.h>
24 #include <linux/init.h>
25 #include <linux/kmod.h>
26 #include <linux/device.h>
27 #include <linux/delay.h>
28 #include <linux/slab.h>
29 #include <linux/i2c.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);
244 static int gc0310_g_focal(struct v4l2_subdev *sd, s32 *val)
246 *val = (GC0310_FOCAL_LENGTH_NUM << 16) | GC0310_FOCAL_LENGTH_DEM;
250 static int gc0310_g_fnumber(struct v4l2_subdev *sd, s32 *val)
252 /*const f number for imx*/
253 *val = (GC0310_F_NUMBER_DEFAULT_NUM << 16) | GC0310_F_NUMBER_DEM;
257 static int gc0310_g_fnumber_range(struct v4l2_subdev *sd, s32 *val)
259 *val = (GC0310_F_NUMBER_DEFAULT_NUM << 24) |
260 (GC0310_F_NUMBER_DEM << 16) |
261 (GC0310_F_NUMBER_DEFAULT_NUM << 8) | GC0310_F_NUMBER_DEM;
265 static int gc0310_g_bin_factor_x(struct v4l2_subdev *sd, s32 *val)
267 struct gc0310_device *dev = to_gc0310_sensor(sd);
269 *val = dev->res->bin_factor_x;
274 static int gc0310_g_bin_factor_y(struct v4l2_subdev *sd, s32 *val)
276 struct gc0310_device *dev = to_gc0310_sensor(sd);
278 *val = dev->res->bin_factor_y;
283 static int gc0310_get_intg_factor(struct i2c_client *client,
284 struct camera_mipi_info *info,
285 const struct gc0310_resolution *res)
287 struct v4l2_subdev *sd = i2c_get_clientdata(client);
288 struct gc0310_device *dev = to_gc0310_sensor(sd);
289 struct atomisp_sensor_mode_data *buf = &info->data;
293 unsigned int hori_blanking;
294 unsigned int vert_blanking;
295 unsigned int sh_delay;
300 /* pixel clock calculattion */
301 dev->vt_pix_clk_freq_mhz = 14400000; // 16.8MHz
302 buf->vt_pix_clk_freq_mhz = dev->vt_pix_clk_freq_mhz;
303 dev_dbg(&client->dev, "vt_pix_clk_freq_mhz=%d\n", buf->vt_pix_clk_freq_mhz);
305 /* get integration time */
306 buf->coarse_integration_time_min = GC0310_COARSE_INTG_TIME_MIN;
307 buf->coarse_integration_time_max_margin =
308 GC0310_COARSE_INTG_TIME_MAX_MARGIN;
310 buf->fine_integration_time_min = GC0310_FINE_INTG_TIME_MIN;
311 buf->fine_integration_time_max_margin =
312 GC0310_FINE_INTG_TIME_MAX_MARGIN;
314 buf->fine_integration_time_def = GC0310_FINE_INTG_TIME_MIN;
315 buf->read_mode = res->bin_mode;
317 /* get the cropping and output resolution to ISP for this mode. */
318 /* Getting crop_horizontal_start */
319 ret = gc0310_read_reg(client, GC0310_8BIT,
320 GC0310_H_CROP_START_H, ®_val);
323 val = (reg_val & 0xFF) << 8;
324 ret = gc0310_read_reg(client, GC0310_8BIT,
325 GC0310_H_CROP_START_L, ®_val);
328 buf->crop_horizontal_start = val | (reg_val & 0xFF);
329 dev_dbg(&client->dev, "crop_horizontal_start=%d\n", buf->crop_horizontal_start);
331 /* Getting crop_vertical_start */
332 ret = gc0310_read_reg(client, GC0310_8BIT,
333 GC0310_V_CROP_START_H, ®_val);
336 val = (reg_val & 0xFF) << 8;
337 ret = gc0310_read_reg(client, GC0310_8BIT,
338 GC0310_V_CROP_START_L, ®_val);
341 buf->crop_vertical_start = val | (reg_val & 0xFF);
342 dev_dbg(&client->dev, "crop_vertical_start=%d\n", buf->crop_vertical_start);
344 /* Getting output_width */
345 ret = gc0310_read_reg(client, GC0310_8BIT,
346 GC0310_H_OUTSIZE_H, ®_val);
349 val = (reg_val & 0xFF) << 8;
350 ret = gc0310_read_reg(client, GC0310_8BIT,
351 GC0310_H_OUTSIZE_L, ®_val);
354 buf->output_width = val | (reg_val & 0xFF);
355 dev_dbg(&client->dev, "output_width=%d\n", buf->output_width);
357 /* Getting output_height */
358 ret = gc0310_read_reg(client, GC0310_8BIT,
359 GC0310_V_OUTSIZE_H, ®_val);
362 val = (reg_val & 0xFF) << 8;
363 ret = gc0310_read_reg(client, GC0310_8BIT,
364 GC0310_V_OUTSIZE_L, ®_val);
367 buf->output_height = val | (reg_val & 0xFF);
368 dev_dbg(&client->dev, "output_height=%d\n", buf->output_height);
370 buf->crop_horizontal_end = buf->crop_horizontal_start + buf->output_width - 1;
371 buf->crop_vertical_end = buf->crop_vertical_start + buf->output_height - 1;
372 dev_dbg(&client->dev, "crop_horizontal_end=%d\n", buf->crop_horizontal_end);
373 dev_dbg(&client->dev, "crop_vertical_end=%d\n", buf->crop_vertical_end);
375 /* Getting line_length_pck */
376 ret = gc0310_read_reg(client, GC0310_8BIT,
377 GC0310_H_BLANKING_H, ®_val);
380 val = (reg_val & 0xFF) << 8;
381 ret = gc0310_read_reg(client, GC0310_8BIT,
382 GC0310_H_BLANKING_L, ®_val);
385 hori_blanking = val | (reg_val & 0xFF);
386 ret = gc0310_read_reg(client, GC0310_8BIT,
387 GC0310_SH_DELAY, ®_val);
391 buf->line_length_pck = buf->output_width + hori_blanking + sh_delay + 4;
392 dev_dbg(&client->dev, "hori_blanking=%d sh_delay=%d line_length_pck=%d\n", hori_blanking,
393 sh_delay, buf->line_length_pck);
395 /* Getting frame_length_lines */
396 ret = gc0310_read_reg(client, GC0310_8BIT,
397 GC0310_V_BLANKING_H, ®_val);
400 val = (reg_val & 0xFF) << 8;
401 ret = gc0310_read_reg(client, GC0310_8BIT,
402 GC0310_V_BLANKING_L, ®_val);
405 vert_blanking = val | (reg_val & 0xFF);
406 buf->frame_length_lines = buf->output_height + vert_blanking;
407 dev_dbg(&client->dev, "vert_blanking=%d frame_length_lines=%d\n", vert_blanking,
408 buf->frame_length_lines);
410 buf->binning_factor_x = res->bin_factor_x ?
411 res->bin_factor_x : 1;
412 buf->binning_factor_y = res->bin_factor_y ?
413 res->bin_factor_y : 1;
417 static int gc0310_set_gain(struct v4l2_subdev *sd, int gain)
420 struct i2c_client *client = v4l2_get_subdevdata(sd);
429 if (gain >= 0x20 && gain < 0x40) {
430 again = 0x0; /* sqrt(2) */
433 again = 0x2; /* 2 * sqrt(2) */
437 dev_dbg(&client->dev, "gain=0x%x again=0x%x dgain=0x%x\n", gain, again, dgain);
439 /* set analog gain */
440 ret = gc0310_write_reg(client, GC0310_8BIT,
441 GC0310_AGC_ADJ, again);
445 /* set digital gain */
446 ret = gc0310_write_reg(client, GC0310_8BIT,
447 GC0310_DGC_ADJ, dgain);
454 static int __gc0310_set_exposure(struct v4l2_subdev *sd, int coarse_itg,
455 int gain, int digitgain)
458 struct i2c_client *client = v4l2_get_subdevdata(sd);
461 dev_dbg(&client->dev, "coarse_itg=%d gain=%d digitgain=%d\n", coarse_itg, gain, digitgain);
464 ret = gc0310_write_reg(client, GC0310_8BIT,
465 GC0310_AEC_PK_EXPO_L,
470 ret = gc0310_write_reg(client, GC0310_8BIT,
471 GC0310_AEC_PK_EXPO_H,
472 (coarse_itg >> 8) & 0x0f);
476 ret = gc0310_set_gain(sd, gain);
483 static int gc0310_set_exposure(struct v4l2_subdev *sd, int exposure,
484 int gain, int digitgain)
486 struct gc0310_device *dev = to_gc0310_sensor(sd);
489 mutex_lock(&dev->input_lock);
490 ret = __gc0310_set_exposure(sd, exposure, gain, digitgain);
491 mutex_unlock(&dev->input_lock);
496 static long gc0310_s_exposure(struct v4l2_subdev *sd,
497 struct atomisp_exposure *exposure)
499 int exp = exposure->integration_time[0];
500 int gain = exposure->gain[0];
501 int digitgain = exposure->gain[1];
503 /* we should not accept the invalid value below. */
505 struct i2c_client *client = v4l2_get_subdevdata(sd);
507 v4l2_err(client, "%s: invalid value\n", __func__);
511 return gc0310_set_exposure(sd, exp, gain, digitgain);
515 static int gc0310_v_flip(struct v4l2_subdev *sd, s32 value)
521 static int gc0310_h_flip(struct v4l2_subdev *sd, s32 value)
526 static long gc0310_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg)
529 case ATOMISP_IOC_S_EXPOSURE:
530 return gc0310_s_exposure(sd, arg);
537 /* This returns the exposure time being used. This should only be used
538 * for filling in EXIF data, not for actual image processing.
540 static int gc0310_q_exposure(struct v4l2_subdev *sd, s32 *value)
542 struct i2c_client *client = v4l2_get_subdevdata(sd);
547 ret = gc0310_read_reg(client, GC0310_8BIT,
548 GC0310_AEC_PK_EXPO_L,
554 ret = gc0310_read_reg(client, GC0310_8BIT,
555 GC0310_AEC_PK_EXPO_H,
560 *value = *value + (reg_v << 8);
565 static int gc0310_s_ctrl(struct v4l2_ctrl *ctrl)
567 struct gc0310_device *dev =
568 container_of(ctrl->handler, struct gc0310_device, ctrl_handler);
569 struct i2c_client *client = v4l2_get_subdevdata(&dev->sd);
574 dev_dbg(&client->dev, "%s: CID_VFLIP:%d.\n",
575 __func__, ctrl->val);
576 ret = gc0310_v_flip(&dev->sd, ctrl->val);
579 dev_dbg(&client->dev, "%s: CID_HFLIP:%d.\n",
580 __func__, ctrl->val);
581 ret = gc0310_h_flip(&dev->sd, ctrl->val);
589 static int gc0310_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
591 struct gc0310_device *dev =
592 container_of(ctrl->handler, struct gc0310_device, ctrl_handler);
596 case V4L2_CID_EXPOSURE_ABSOLUTE:
597 ret = gc0310_q_exposure(&dev->sd, &ctrl->val);
599 case V4L2_CID_FOCAL_ABSOLUTE:
600 ret = gc0310_g_focal(&dev->sd, &ctrl->val);
602 case V4L2_CID_FNUMBER_ABSOLUTE:
603 ret = gc0310_g_fnumber(&dev->sd, &ctrl->val);
605 case V4L2_CID_FNUMBER_RANGE:
606 ret = gc0310_g_fnumber_range(&dev->sd, &ctrl->val);
608 case V4L2_CID_BIN_FACTOR_HORZ:
609 ret = gc0310_g_bin_factor_x(&dev->sd, &ctrl->val);
611 case V4L2_CID_BIN_FACTOR_VERT:
612 ret = gc0310_g_bin_factor_y(&dev->sd, &ctrl->val);
621 static const struct v4l2_ctrl_ops ctrl_ops = {
622 .s_ctrl = gc0310_s_ctrl,
623 .g_volatile_ctrl = gc0310_g_volatile_ctrl
626 static const struct v4l2_ctrl_config gc0310_controls[] = {
629 .id = V4L2_CID_EXPOSURE_ABSOLUTE,
630 .type = V4L2_CTRL_TYPE_INTEGER,
640 .id = V4L2_CID_VFLIP,
641 .type = V4L2_CTRL_TYPE_BOOLEAN,
650 .id = V4L2_CID_HFLIP,
651 .type = V4L2_CTRL_TYPE_BOOLEAN,
660 .id = V4L2_CID_FOCAL_ABSOLUTE,
661 .type = V4L2_CTRL_TYPE_INTEGER,
662 .name = "focal length",
663 .min = GC0310_FOCAL_LENGTH_DEFAULT,
664 .max = GC0310_FOCAL_LENGTH_DEFAULT,
666 .def = GC0310_FOCAL_LENGTH_DEFAULT,
671 .id = V4L2_CID_FNUMBER_ABSOLUTE,
672 .type = V4L2_CTRL_TYPE_INTEGER,
674 .min = GC0310_F_NUMBER_DEFAULT,
675 .max = GC0310_F_NUMBER_DEFAULT,
677 .def = GC0310_F_NUMBER_DEFAULT,
682 .id = V4L2_CID_FNUMBER_RANGE,
683 .type = V4L2_CTRL_TYPE_INTEGER,
684 .name = "f-number range",
685 .min = GC0310_F_NUMBER_RANGE,
686 .max = GC0310_F_NUMBER_RANGE,
688 .def = GC0310_F_NUMBER_RANGE,
693 .id = V4L2_CID_BIN_FACTOR_HORZ,
694 .type = V4L2_CTRL_TYPE_INTEGER,
695 .name = "horizontal binning factor",
697 .max = GC0310_BIN_FACTOR_MAX,
704 .id = V4L2_CID_BIN_FACTOR_VERT,
705 .type = V4L2_CTRL_TYPE_INTEGER,
706 .name = "vertical binning factor",
708 .max = GC0310_BIN_FACTOR_MAX,
715 static int gc0310_init(struct v4l2_subdev *sd)
718 struct i2c_client *client = v4l2_get_subdevdata(sd);
719 struct gc0310_device *dev = to_gc0310_sensor(sd);
721 mutex_lock(&dev->input_lock);
723 /* set initial registers */
724 ret = gc0310_write_reg_array(client, gc0310_reset_register);
726 /* restore settings */
727 gc0310_res = gc0310_res_preview;
728 N_RES = N_RES_PREVIEW;
730 mutex_unlock(&dev->input_lock);
735 static int power_ctrl(struct v4l2_subdev *sd, bool flag)
738 struct gc0310_device *dev = to_gc0310_sensor(sd);
740 if (!dev || !dev->platform_data)
744 /* The upstream module driver (written to Crystal
745 * Cove) had this logic to pulse the rails low first.
746 * This appears to break things on the MRD7 with the
749 * ret = dev->platform_data->v1p8_ctrl(sd, 0);
750 * ret |= dev->platform_data->v2p8_ctrl(sd, 0);
753 ret |= dev->platform_data->v1p8_ctrl(sd, 1);
754 ret |= dev->platform_data->v2p8_ctrl(sd, 1);
755 usleep_range(10000, 15000);
759 ret |= dev->platform_data->v1p8_ctrl(sd, 0);
760 ret |= dev->platform_data->v2p8_ctrl(sd, 0);
765 static int gpio_ctrl(struct v4l2_subdev *sd, bool flag)
768 struct gc0310_device *dev = to_gc0310_sensor(sd);
770 if (!dev || !dev->platform_data)
773 /* GPIO0 == "reset" (active low), GPIO1 == "power down" */
775 /* Pulse reset, then release power down */
776 ret = dev->platform_data->gpio0_ctrl(sd, 0);
777 usleep_range(5000, 10000);
778 ret |= dev->platform_data->gpio0_ctrl(sd, 1);
779 usleep_range(10000, 15000);
780 ret |= dev->platform_data->gpio1_ctrl(sd, 0);
781 usleep_range(10000, 15000);
783 ret = dev->platform_data->gpio1_ctrl(sd, 1);
784 ret |= dev->platform_data->gpio0_ctrl(sd, 0);
789 static int power_down(struct v4l2_subdev *sd);
791 static int power_up(struct v4l2_subdev *sd)
793 struct gc0310_device *dev = to_gc0310_sensor(sd);
794 struct i2c_client *client = v4l2_get_subdevdata(sd);
797 if (!dev->platform_data) {
798 dev_err(&client->dev,
799 "no camera_sensor_platform_data");
804 ret = power_ctrl(sd, 1);
808 /* flis clock control */
809 ret = dev->platform_data->flisclk_ctrl(sd, 1);
814 ret = gpio_ctrl(sd, 1);
816 ret = gpio_ctrl(sd, 1);
826 dev->platform_data->flisclk_ctrl(sd, 0);
830 dev_err(&client->dev, "sensor power-up failed\n");
835 static int power_down(struct v4l2_subdev *sd)
837 struct gc0310_device *dev = to_gc0310_sensor(sd);
838 struct i2c_client *client = v4l2_get_subdevdata(sd);
841 if (!dev->platform_data) {
842 dev_err(&client->dev,
843 "no camera_sensor_platform_data");
848 ret = gpio_ctrl(sd, 0);
850 ret = gpio_ctrl(sd, 0);
852 dev_err(&client->dev, "gpio failed 2\n");
855 ret = dev->platform_data->flisclk_ctrl(sd, 0);
857 dev_err(&client->dev, "flisclk failed\n");
860 ret = power_ctrl(sd, 0);
862 dev_err(&client->dev, "vprog failed.\n");
867 static int gc0310_s_power(struct v4l2_subdev *sd, int on)
872 return power_down(sd);
878 return gc0310_init(sd);
881 /* TODO: remove it. */
882 static int startup(struct v4l2_subdev *sd)
884 struct gc0310_device *dev = to_gc0310_sensor(sd);
885 struct i2c_client *client = v4l2_get_subdevdata(sd);
888 ret = gc0310_write_reg_array(client, dev->res->regs);
890 dev_err(&client->dev, "gc0310 write register err.\n");
897 static int gc0310_set_fmt(struct v4l2_subdev *sd,
898 struct v4l2_subdev_state *sd_state,
899 struct v4l2_subdev_format *format)
901 struct v4l2_mbus_framefmt *fmt = &format->format;
902 struct gc0310_device *dev = to_gc0310_sensor(sd);
903 struct i2c_client *client = v4l2_get_subdevdata(sd);
904 struct camera_mipi_info *gc0310_info = NULL;
905 struct gc0310_resolution *res;
914 gc0310_info = v4l2_get_subdev_hostdata(sd);
918 mutex_lock(&dev->input_lock);
920 res = v4l2_find_nearest_size(gc0310_res_preview,
921 ARRAY_SIZE(gc0310_res_preview), width,
922 height, fmt->width, fmt->height);
924 res = &gc0310_res_preview[N_RES - 1];
926 fmt->width = res->width;
927 fmt->height = res->height;
930 fmt->code = MEDIA_BUS_FMT_SGRBG8_1X8;
932 if (format->which == V4L2_SUBDEV_FORMAT_TRY) {
933 sd_state->pads->try_fmt = *fmt;
934 mutex_unlock(&dev->input_lock);
938 dev_dbg(&client->dev, "%s: before gc0310_write_reg_array %s\n",
939 __func__, dev->res->desc);
942 dev_err(&client->dev, "gc0310 startup err\n");
946 ret = gc0310_get_intg_factor(client, gc0310_info, dev->res);
948 dev_err(&client->dev, "failed to get integration_factor\n");
953 mutex_unlock(&dev->input_lock);
957 static int gc0310_get_fmt(struct v4l2_subdev *sd,
958 struct v4l2_subdev_state *sd_state,
959 struct v4l2_subdev_format *format)
961 struct v4l2_mbus_framefmt *fmt = &format->format;
962 struct gc0310_device *dev = to_gc0310_sensor(sd);
970 fmt->width = dev->res->width;
971 fmt->height = dev->res->height;
972 fmt->code = MEDIA_BUS_FMT_SGRBG8_1X8;
977 static int gc0310_detect(struct i2c_client *client)
979 struct i2c_adapter *adapter = client->adapter;
984 if (!i2c_check_functionality(adapter, I2C_FUNC_I2C))
987 ret = gc0310_read_reg(client, GC0310_8BIT,
988 GC0310_SC_CMMN_CHIP_ID_H, &high);
990 dev_err(&client->dev, "read sensor_id_high failed\n");
993 ret = gc0310_read_reg(client, GC0310_8BIT,
994 GC0310_SC_CMMN_CHIP_ID_L, &low);
996 dev_err(&client->dev, "read sensor_id_low failed\n");
999 id = ((((u16)high) << 8) | (u16)low);
1000 dev_dbg(&client->dev, "sensor ID = 0x%x\n", id);
1002 if (id != GC0310_ID) {
1003 dev_err(&client->dev, "sensor ID error, read id = 0x%x, target id = 0x%x\n", id,
1008 dev_dbg(&client->dev, "detect gc0310 success\n");
1013 static int gc0310_s_stream(struct v4l2_subdev *sd, int enable)
1015 struct gc0310_device *dev = to_gc0310_sensor(sd);
1016 struct i2c_client *client = v4l2_get_subdevdata(sd);
1019 dev_dbg(&client->dev, "%s S enable=%d\n", __func__, enable);
1020 mutex_lock(&dev->input_lock);
1023 /* enable per frame MIPI and sensor ctrl reset */
1024 ret = gc0310_write_reg(client, GC0310_8BIT,
1027 mutex_unlock(&dev->input_lock);
1032 ret = gc0310_write_reg(client, GC0310_8BIT,
1033 GC0310_RESET_RELATED, GC0310_REGISTER_PAGE_3);
1035 mutex_unlock(&dev->input_lock);
1039 ret = gc0310_write_reg(client, GC0310_8BIT, GC0310_SW_STREAM,
1040 enable ? GC0310_START_STREAMING :
1041 GC0310_STOP_STREAMING);
1043 mutex_unlock(&dev->input_lock);
1047 ret = gc0310_write_reg(client, GC0310_8BIT,
1048 GC0310_RESET_RELATED, GC0310_REGISTER_PAGE_0);
1050 mutex_unlock(&dev->input_lock);
1054 mutex_unlock(&dev->input_lock);
1058 static int gc0310_s_config(struct v4l2_subdev *sd,
1059 int irq, void *platform_data)
1061 struct gc0310_device *dev = to_gc0310_sensor(sd);
1062 struct i2c_client *client = v4l2_get_subdevdata(sd);
1068 dev->platform_data =
1069 (struct camera_sensor_platform_data *)platform_data;
1071 mutex_lock(&dev->input_lock);
1072 /* power off the module, then power on it in future
1073 * as first power on by board may not fulfill the
1074 * power on sequqence needed by the module
1076 ret = power_down(sd);
1078 dev_err(&client->dev, "gc0310 power-off err.\n");
1079 goto fail_power_off;
1084 dev_err(&client->dev, "gc0310 power-up err.\n");
1088 ret = dev->platform_data->csi_cfg(sd, 1);
1092 /* config & detect sensor */
1093 ret = gc0310_detect(client);
1095 dev_err(&client->dev, "gc0310_detect err s_config.\n");
1099 /* turn off sensor, after probed */
1100 ret = power_down(sd);
1102 dev_err(&client->dev, "gc0310 power-off err.\n");
1105 mutex_unlock(&dev->input_lock);
1110 dev->platform_data->csi_cfg(sd, 0);
1113 dev_err(&client->dev, "sensor power-gating failed\n");
1115 mutex_unlock(&dev->input_lock);
1119 static int gc0310_g_frame_interval(struct v4l2_subdev *sd,
1120 struct v4l2_subdev_frame_interval *interval)
1122 struct gc0310_device *dev = to_gc0310_sensor(sd);
1124 interval->interval.numerator = 1;
1125 interval->interval.denominator = dev->res->fps;
1130 static int gc0310_enum_mbus_code(struct v4l2_subdev *sd,
1131 struct v4l2_subdev_state *sd_state,
1132 struct v4l2_subdev_mbus_code_enum *code)
1134 if (code->index >= MAX_FMTS)
1137 code->code = MEDIA_BUS_FMT_SGRBG8_1X8;
1141 static int gc0310_enum_frame_size(struct v4l2_subdev *sd,
1142 struct v4l2_subdev_state *sd_state,
1143 struct v4l2_subdev_frame_size_enum *fse)
1145 int index = fse->index;
1150 fse->min_width = gc0310_res[index].width;
1151 fse->min_height = gc0310_res[index].height;
1152 fse->max_width = gc0310_res[index].width;
1153 fse->max_height = gc0310_res[index].height;
1158 static int gc0310_g_skip_frames(struct v4l2_subdev *sd, u32 *frames)
1160 struct gc0310_device *dev = to_gc0310_sensor(sd);
1162 mutex_lock(&dev->input_lock);
1163 *frames = dev->res->skip_frames;
1164 mutex_unlock(&dev->input_lock);
1169 static const struct v4l2_subdev_sensor_ops gc0310_sensor_ops = {
1170 .g_skip_frames = gc0310_g_skip_frames,
1173 static const struct v4l2_subdev_video_ops gc0310_video_ops = {
1174 .s_stream = gc0310_s_stream,
1175 .g_frame_interval = gc0310_g_frame_interval,
1178 static const struct v4l2_subdev_core_ops gc0310_core_ops = {
1179 .s_power = gc0310_s_power,
1180 .ioctl = gc0310_ioctl,
1183 static const struct v4l2_subdev_pad_ops gc0310_pad_ops = {
1184 .enum_mbus_code = gc0310_enum_mbus_code,
1185 .enum_frame_size = gc0310_enum_frame_size,
1186 .get_fmt = gc0310_get_fmt,
1187 .set_fmt = gc0310_set_fmt,
1190 static const struct v4l2_subdev_ops gc0310_ops = {
1191 .core = &gc0310_core_ops,
1192 .video = &gc0310_video_ops,
1193 .pad = &gc0310_pad_ops,
1194 .sensor = &gc0310_sensor_ops,
1197 static int gc0310_remove(struct i2c_client *client)
1199 struct v4l2_subdev *sd = i2c_get_clientdata(client);
1200 struct gc0310_device *dev = to_gc0310_sensor(sd);
1202 dev_dbg(&client->dev, "gc0310_remove...\n");
1204 dev->platform_data->csi_cfg(sd, 0);
1206 v4l2_device_unregister_subdev(sd);
1207 media_entity_cleanup(&dev->sd.entity);
1208 v4l2_ctrl_handler_free(&dev->ctrl_handler);
1214 static int gc0310_probe(struct i2c_client *client)
1216 struct gc0310_device *dev;
1221 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1225 mutex_init(&dev->input_lock);
1227 dev->res = &gc0310_res_preview[0];
1228 v4l2_i2c_subdev_init(&dev->sd, client, &gc0310_ops);
1230 pdata = gmin_camera_platform_data(&dev->sd,
1231 ATOMISP_INPUT_FORMAT_RAW_8,
1232 atomisp_bayer_order_grbg);
1238 ret = gc0310_s_config(&dev->sd, client->irq, pdata);
1242 ret = atomisp_register_i2c_module(&dev->sd, pdata, RAW_CAMERA);
1246 dev->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
1247 dev->pad.flags = MEDIA_PAD_FL_SOURCE;
1248 dev->format.code = MEDIA_BUS_FMT_SGRBG8_1X8;
1249 dev->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
1251 v4l2_ctrl_handler_init(&dev->ctrl_handler,
1252 ARRAY_SIZE(gc0310_controls));
1254 gc0310_remove(client);
1258 for (i = 0; i < ARRAY_SIZE(gc0310_controls); i++)
1259 v4l2_ctrl_new_custom(&dev->ctrl_handler, &gc0310_controls[i],
1262 if (dev->ctrl_handler.error) {
1263 gc0310_remove(client);
1264 return dev->ctrl_handler.error;
1267 /* Use same lock for controls as for everything else. */
1268 dev->ctrl_handler.lock = &dev->input_lock;
1269 dev->sd.ctrl_handler = &dev->ctrl_handler;
1271 ret = media_entity_pads_init(&dev->sd.entity, 1, &dev->pad);
1273 gc0310_remove(client);
1277 v4l2_device_unregister_subdev(&dev->sd);
1282 static const struct acpi_device_id gc0310_acpi_match[] = {
1287 MODULE_DEVICE_TABLE(acpi, gc0310_acpi_match);
1289 static struct i2c_driver gc0310_driver = {
1292 .acpi_match_table = gc0310_acpi_match,
1294 .probe_new = gc0310_probe,
1295 .remove = gc0310_remove,
1297 module_i2c_driver(gc0310_driver);
1299 MODULE_AUTHOR("Lai, Angie <angie.lai@intel.com>");
1300 MODULE_DESCRIPTION("A low-level driver for GalaxyCore GC0310 sensors");
1301 MODULE_LICENSE("GPL");