2 * Support for GalaxyCore GC2235 2M camera sensor.
4 * Copyright (c) 2014 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>
32 #include "../include/linux/atomisp_gmin_platform.h"
33 #include <linux/acpi.h>
38 /* i2c read/write stuff */
39 static int gc2235_read_reg(struct i2c_client *client,
40 u16 data_length, u16 reg, u16 *val)
43 struct i2c_msg msg[2];
44 unsigned char data[6];
46 if (!client->adapter) {
47 dev_err(&client->dev, "%s error, no client->adapter\n",
52 if (data_length != GC2235_8BIT) {
53 dev_err(&client->dev, "%s error, invalid data length\n",
58 memset(msg, 0, sizeof(msg));
60 msg[0].addr = client->addr;
65 /* high byte goes out first */
66 data[0] = (u8)(reg & 0xff);
68 msg[1].addr = client->addr;
69 msg[1].len = data_length;
70 msg[1].flags = I2C_M_RD;
73 err = i2c_transfer(client->adapter, msg, 2);
78 "read from offset 0x%x error %d", reg, err);
83 /* high byte comes first */
84 if (data_length == GC2235_8BIT)
90 static int gc2235_i2c_write(struct i2c_client *client, u16 len, u8 *data)
93 const int num_msg = 1;
96 msg.addr = client->addr;
100 ret = i2c_transfer(client->adapter, &msg, 1);
102 return ret == num_msg ? 0 : -EIO;
105 static int gc2235_write_reg(struct i2c_client *client, u16 data_length,
109 unsigned char data[4] = {0};
110 const u16 len = data_length + sizeof(u8); /* 16-bit address + data */
112 if (data_length != GC2235_8BIT) {
113 dev_err(&client->dev,
114 "%s error, invalid data_length\n", __func__);
118 /* high byte goes out first */
122 ret = gc2235_i2c_write(client, len, data);
124 dev_err(&client->dev,
125 "write error: wrote 0x%x to offset 0x%x error %d",
131 static int __gc2235_flush_reg_array(struct i2c_client *client,
132 struct gc2235_write_ctrl *ctrl)
136 if (ctrl->index == 0)
139 size = sizeof(u8) + ctrl->index; /* 8-bit address + data */
142 return gc2235_i2c_write(client, size, (u8 *)&ctrl->buffer);
145 static int __gc2235_buf_reg_array(struct i2c_client *client,
146 struct gc2235_write_ctrl *ctrl,
147 const struct gc2235_reg *next)
151 if (next->type != GC2235_8BIT)
155 ctrl->buffer.data[ctrl->index] = (u8)next->val;
157 /* When first item is added, we need to store its starting address */
158 if (ctrl->index == 0)
159 ctrl->buffer.addr = next->reg;
164 * Buffer cannot guarantee free space for u32? Better flush it to avoid
165 * possible lack of memory for next item.
167 if (ctrl->index + sizeof(u8) >= GC2235_MAX_WRITE_BUF_SIZE)
168 return __gc2235_flush_reg_array(client, ctrl);
172 static int __gc2235_write_reg_is_consecutive(struct i2c_client *client,
173 struct gc2235_write_ctrl *ctrl,
174 const struct gc2235_reg *next)
176 if (ctrl->index == 0)
179 return ctrl->buffer.addr + ctrl->index == next->reg;
181 static int gc2235_write_reg_array(struct i2c_client *client,
182 const struct gc2235_reg *reglist)
184 const struct gc2235_reg *next = reglist;
185 struct gc2235_write_ctrl ctrl;
189 for (; next->type != GC2235_TOK_TERM; next++) {
190 switch (next->type & GC2235_TOK_MASK) {
191 case GC2235_TOK_DELAY:
192 err = __gc2235_flush_reg_array(client, &ctrl);
199 * If next address is not consecutive, data needs to be
200 * flushed before proceed.
202 if (!__gc2235_write_reg_is_consecutive(client, &ctrl,
204 err = __gc2235_flush_reg_array(client, &ctrl);
208 err = __gc2235_buf_reg_array(client, &ctrl, next);
210 dev_err(&client->dev, "%s: write error, aborted\n",
218 return __gc2235_flush_reg_array(client, &ctrl);
221 static int gc2235_g_focal(struct v4l2_subdev *sd, s32 *val)
223 *val = (GC2235_FOCAL_LENGTH_NUM << 16) | GC2235_FOCAL_LENGTH_DEM;
227 static int gc2235_g_fnumber(struct v4l2_subdev *sd, s32 *val)
229 /*const f number for imx*/
230 *val = (GC2235_F_NUMBER_DEFAULT_NUM << 16) | GC2235_F_NUMBER_DEM;
234 static int gc2235_g_fnumber_range(struct v4l2_subdev *sd, s32 *val)
236 *val = (GC2235_F_NUMBER_DEFAULT_NUM << 24) |
237 (GC2235_F_NUMBER_DEM << 16) |
238 (GC2235_F_NUMBER_DEFAULT_NUM << 8) | GC2235_F_NUMBER_DEM;
243 static int gc2235_get_intg_factor(struct i2c_client *client,
244 struct camera_mipi_info *info,
245 const struct gc2235_resolution *res)
247 struct v4l2_subdev *sd = i2c_get_clientdata(client);
248 struct gc2235_device *dev = to_gc2235_sensor(sd);
249 struct atomisp_sensor_mode_data *buf = &info->data;
250 u16 reg_val, reg_val_h, dummy;
256 /* pixel clock calculattion */
257 buf->vt_pix_clk_freq_mhz = dev->vt_pix_clk_freq_mhz = 30000000;
259 /* get integration time */
260 buf->coarse_integration_time_min = GC2235_COARSE_INTG_TIME_MIN;
261 buf->coarse_integration_time_max_margin =
262 GC2235_COARSE_INTG_TIME_MAX_MARGIN;
264 buf->fine_integration_time_min = GC2235_FINE_INTG_TIME_MIN;
265 buf->fine_integration_time_max_margin =
266 GC2235_FINE_INTG_TIME_MAX_MARGIN;
268 buf->fine_integration_time_def = GC2235_FINE_INTG_TIME_MIN;
269 buf->frame_length_lines = res->lines_per_frame;
270 buf->line_length_pck = res->pixels_per_line;
271 buf->read_mode = res->bin_mode;
273 /* get the cropping and output resolution to ISP for this mode. */
274 ret = gc2235_read_reg(client, GC2235_8BIT,
275 GC2235_H_CROP_START_H, ®_val_h);
276 ret = gc2235_read_reg(client, GC2235_8BIT,
277 GC2235_H_CROP_START_L, ®_val);
281 buf->crop_horizontal_start = (reg_val_h << 8) | reg_val;
283 ret = gc2235_read_reg(client, GC2235_8BIT,
284 GC2235_V_CROP_START_H, ®_val_h);
285 ret = gc2235_read_reg(client, GC2235_8BIT,
286 GC2235_V_CROP_START_L, ®_val);
290 buf->crop_vertical_start = (reg_val_h << 8) | reg_val;
292 ret = gc2235_read_reg(client, GC2235_8BIT,
293 GC2235_H_OUTSIZE_H, ®_val_h);
294 ret = gc2235_read_reg(client, GC2235_8BIT,
295 GC2235_H_OUTSIZE_L, ®_val);
298 buf->output_width = (reg_val_h << 8) | reg_val;
300 ret = gc2235_read_reg(client, GC2235_8BIT,
301 GC2235_V_OUTSIZE_H, ®_val_h);
302 ret = gc2235_read_reg(client, GC2235_8BIT,
303 GC2235_V_OUTSIZE_L, ®_val);
306 buf->output_height = (reg_val_h << 8) | reg_val;
308 buf->crop_horizontal_end = buf->crop_horizontal_start +
309 buf->output_width - 1;
310 buf->crop_vertical_end = buf->crop_vertical_start +
311 buf->output_height - 1;
313 ret = gc2235_read_reg(client, GC2235_8BIT,
314 GC2235_HB_H, ®_val_h);
315 ret = gc2235_read_reg(client, GC2235_8BIT,
316 GC2235_HB_L, ®_val);
320 dummy = (reg_val_h << 8) | reg_val;
322 ret = gc2235_read_reg(client, GC2235_8BIT,
323 GC2235_SH_DELAY_H, ®_val_h);
324 ret = gc2235_read_reg(client, GC2235_8BIT,
325 GC2235_SH_DELAY_L, ®_val);
328 buf->line_length_pck = buf->output_width + 16 + dummy +
329 (((u16)reg_val_h << 8) | (u16)reg_val) + 4;
331 ret = gc2235_read_reg(client, GC2235_8BIT,
332 GC2235_VB_H, ®_val_h);
333 ret = gc2235_read_reg(client, GC2235_8BIT,
334 GC2235_VB_L, ®_val);
339 buf->frame_length_lines = buf->output_height + 32 +
340 (((u16)reg_val_h << 8) | (u16)reg_val);
342 buf->binning_factor_x = res->bin_factor_x ?
343 res->bin_factor_x : 1;
344 buf->binning_factor_y = res->bin_factor_y ?
345 res->bin_factor_y : 1;
349 static long __gc2235_set_exposure(struct v4l2_subdev *sd, int coarse_itg,
350 int gain, int digitgain)
353 struct i2c_client *client = v4l2_get_subdevdata(sd);
354 u16 coarse_integration = (u16)coarse_itg;
356 u16 expo_coarse_h, expo_coarse_l, gain_val = 0xF0, gain_val2 = 0xF0;
357 expo_coarse_h = coarse_integration >> 8;
358 expo_coarse_l = coarse_integration & 0xff;
360 ret = gc2235_write_reg(client, GC2235_8BIT,
361 GC2235_EXPOSURE_H, expo_coarse_h);
362 ret = gc2235_write_reg(client, GC2235_8BIT,
363 GC2235_EXPOSURE_L, expo_coarse_l);
368 } else if (gain < 256) {
372 gain_val2 = 64 * gain / 256;
376 ret = gc2235_write_reg(client, GC2235_8BIT,
377 GC2235_GLOBAL_GAIN, (u8)gain_val);
378 ret = gc2235_write_reg(client, GC2235_8BIT,
379 GC2235_PRE_GAIN, (u8)gain_val2);
385 static int gc2235_set_exposure(struct v4l2_subdev *sd, int exposure,
386 int gain, int digitgain)
388 struct gc2235_device *dev = to_gc2235_sensor(sd);
391 mutex_lock(&dev->input_lock);
392 ret = __gc2235_set_exposure(sd, exposure, gain, digitgain);
393 mutex_unlock(&dev->input_lock);
398 static long gc2235_s_exposure(struct v4l2_subdev *sd,
399 struct atomisp_exposure *exposure)
401 int exp = exposure->integration_time[0];
402 int gain = exposure->gain[0];
403 int digitgain = exposure->gain[1];
405 /* we should not accept the invalid value below. */
407 struct i2c_client *client = v4l2_get_subdevdata(sd);
408 v4l2_err(client, "%s: invalid value\n", __func__);
412 return gc2235_set_exposure(sd, exp, gain, digitgain);
414 static long gc2235_ioctl(struct v4l2_subdev *sd, unsigned int cmd, void *arg)
417 case ATOMISP_IOC_S_EXPOSURE:
418 return gc2235_s_exposure(sd, arg);
424 /* This returns the exposure time being used. This should only be used
425 * for filling in EXIF data, not for actual image processing.
427 static int gc2235_q_exposure(struct v4l2_subdev *sd, s32 *value)
429 struct i2c_client *client = v4l2_get_subdevdata(sd);
434 ret = gc2235_read_reg(client, GC2235_8BIT,
440 ret = gc2235_read_reg(client, GC2235_8BIT,
446 reg_v += reg_v2 << 8;
453 static int gc2235_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
455 struct gc2235_device *dev =
456 container_of(ctrl->handler, struct gc2235_device, ctrl_handler);
460 case V4L2_CID_EXPOSURE_ABSOLUTE:
461 ret = gc2235_q_exposure(&dev->sd, &ctrl->val);
463 case V4L2_CID_FOCAL_ABSOLUTE:
464 ret = gc2235_g_focal(&dev->sd, &ctrl->val);
466 case V4L2_CID_FNUMBER_ABSOLUTE:
467 ret = gc2235_g_fnumber(&dev->sd, &ctrl->val);
469 case V4L2_CID_FNUMBER_RANGE:
470 ret = gc2235_g_fnumber_range(&dev->sd, &ctrl->val);
479 static const struct v4l2_ctrl_ops ctrl_ops = {
480 .g_volatile_ctrl = gc2235_g_volatile_ctrl
483 static struct v4l2_ctrl_config gc2235_controls[] = {
486 .id = V4L2_CID_EXPOSURE_ABSOLUTE,
487 .type = V4L2_CTRL_TYPE_INTEGER,
497 .id = V4L2_CID_FOCAL_ABSOLUTE,
498 .type = V4L2_CTRL_TYPE_INTEGER,
499 .name = "focal length",
500 .min = GC2235_FOCAL_LENGTH_DEFAULT,
501 .max = GC2235_FOCAL_LENGTH_DEFAULT,
503 .def = GC2235_FOCAL_LENGTH_DEFAULT,
508 .id = V4L2_CID_FNUMBER_ABSOLUTE,
509 .type = V4L2_CTRL_TYPE_INTEGER,
511 .min = GC2235_F_NUMBER_DEFAULT,
512 .max = GC2235_F_NUMBER_DEFAULT,
514 .def = GC2235_F_NUMBER_DEFAULT,
519 .id = V4L2_CID_FNUMBER_RANGE,
520 .type = V4L2_CTRL_TYPE_INTEGER,
521 .name = "f-number range",
522 .min = GC2235_F_NUMBER_RANGE,
523 .max = GC2235_F_NUMBER_RANGE,
525 .def = GC2235_F_NUMBER_RANGE,
530 static int __gc2235_init(struct v4l2_subdev *sd)
532 struct i2c_client *client = v4l2_get_subdevdata(sd);
534 /* restore settings */
535 gc2235_res = gc2235_res_preview;
536 N_RES = N_RES_PREVIEW;
538 return gc2235_write_reg_array(client, gc2235_init_settings);
543 static int power_ctrl(struct v4l2_subdev *sd, bool flag)
546 struct gc2235_device *dev = to_gc2235_sensor(sd);
548 if (!dev || !dev->platform_data)
551 /* Non-gmin platforms use the legacy callback */
552 if (dev->platform_data->power_ctrl)
553 return dev->platform_data->power_ctrl(sd, flag);
556 ret = dev->platform_data->v1p8_ctrl(sd, 1);
557 usleep_range(60, 90);
559 ret |= dev->platform_data->v2p8_ctrl(sd, 1);
561 ret = dev->platform_data->v1p8_ctrl(sd, 0);
562 ret |= dev->platform_data->v2p8_ctrl(sd, 0);
567 static int gpio_ctrl(struct v4l2_subdev *sd, bool flag)
569 struct gc2235_device *dev = to_gc2235_sensor(sd);
572 if (!dev || !dev->platform_data)
575 /* Non-gmin platforms use the legacy callback */
576 if (dev->platform_data->gpio_ctrl)
577 return dev->platform_data->gpio_ctrl(sd, flag);
579 ret |= dev->platform_data->gpio1_ctrl(sd, !flag);
580 usleep_range(60, 90);
581 return dev->platform_data->gpio0_ctrl(sd, flag);
584 static int power_up(struct v4l2_subdev *sd)
586 struct gc2235_device *dev = to_gc2235_sensor(sd);
587 struct i2c_client *client = v4l2_get_subdevdata(sd);
590 if (!dev->platform_data) {
591 dev_err(&client->dev,
592 "no camera_sensor_platform_data");
596 ret = power_ctrl(sd, 1);
600 /* according to DS, at least 5ms is needed between DOVDD and PWDN */
601 usleep_range(5000, 6000);
603 ret = dev->platform_data->flisclk_ctrl(sd, 1);
606 usleep_range(5000, 6000);
609 ret = gpio_ctrl(sd, 1);
611 ret = gpio_ctrl(sd, 1);
623 dev_err(&client->dev, "sensor power-up failed\n");
628 static int power_down(struct v4l2_subdev *sd)
630 struct gc2235_device *dev = to_gc2235_sensor(sd);
631 struct i2c_client *client = v4l2_get_subdevdata(sd);
634 if (!dev->platform_data) {
635 dev_err(&client->dev,
636 "no camera_sensor_platform_data");
640 ret = gpio_ctrl(sd, 0);
642 ret = gpio_ctrl(sd, 0);
644 dev_err(&client->dev, "gpio failed 2\n");
647 ret = dev->platform_data->flisclk_ctrl(sd, 0);
649 dev_err(&client->dev, "flisclk failed\n");
652 ret = power_ctrl(sd, 0);
654 dev_err(&client->dev, "vprog failed.\n");
659 static int gc2235_s_power(struct v4l2_subdev *sd, int on)
664 ret = power_down(sd);
668 ret = __gc2235_init(sd);
675 * distance - calculate the distance
680 * Get the gap between resolution and w/h.
681 * res->width/height smaller than w/h wouldn't be considered.
682 * Returns the value of gap or -1 if fail.
684 #define LARGEST_ALLOWED_RATIO_MISMATCH 800
685 static int distance(struct gc2235_resolution *res, u32 w, u32 h)
687 unsigned int w_ratio = (res->width << 13) / w;
688 unsigned int h_ratio;
693 h_ratio = (res->height << 13) / h;
696 match = abs(((w_ratio << 13) / h_ratio) - 8192);
698 if ((w_ratio < 8192) || (h_ratio < 8192) ||
699 (match > LARGEST_ALLOWED_RATIO_MISMATCH))
702 return w_ratio + h_ratio;
705 /* Return the nearest higher resolution index */
706 static int nearest_resolution_index(int w, int h)
711 int min_dist = INT_MAX;
712 struct gc2235_resolution *tmp_res = NULL;
714 for (i = 0; i < N_RES; i++) {
715 tmp_res = &gc2235_res[i];
716 dist = distance(tmp_res, w, h);
719 if (dist < min_dist) {
728 static int get_resolution_index(int w, int h)
732 for (i = 0; i < N_RES; i++) {
733 if (w != gc2235_res[i].width)
735 if (h != gc2235_res[i].height)
744 static int startup(struct v4l2_subdev *sd)
746 struct gc2235_device *dev = to_gc2235_sensor(sd);
747 struct i2c_client *client = v4l2_get_subdevdata(sd);
750 /* force gc2235 to do a reset in res change, otherwise it
751 * can not output normal after switching res. and it is not
752 * necessary for first time run up after power on, for the sack
757 gc2235_write_reg_array(client, gc2235_init_settings);
760 ret = gc2235_write_reg_array(client, gc2235_res[dev->fmt_idx].regs);
762 dev_err(&client->dev, "gc2235 write register err.\n");
770 static int gc2235_set_fmt(struct v4l2_subdev *sd,
771 struct v4l2_subdev_pad_config *cfg,
772 struct v4l2_subdev_format *format)
775 struct v4l2_mbus_framefmt *fmt = &format->format;
776 struct gc2235_device *dev = to_gc2235_sensor(sd);
777 struct i2c_client *client = v4l2_get_subdevdata(sd);
778 struct camera_mipi_info *gc2235_info = NULL;
782 gc2235_info = v4l2_get_subdev_hostdata(sd);
789 mutex_lock(&dev->input_lock);
790 idx = nearest_resolution_index(fmt->width, fmt->height);
792 /* return the largest resolution */
793 fmt->width = gc2235_res[N_RES - 1].width;
794 fmt->height = gc2235_res[N_RES - 1].height;
796 fmt->width = gc2235_res[idx].width;
797 fmt->height = gc2235_res[idx].height;
799 fmt->code = MEDIA_BUS_FMT_SGRBG10_1X10;
800 if (format->which == V4L2_SUBDEV_FORMAT_TRY) {
802 mutex_unlock(&dev->input_lock);
806 dev->fmt_idx = get_resolution_index(fmt->width, fmt->height);
807 if (dev->fmt_idx == -1) {
808 dev_err(&client->dev, "get resolution fail\n");
809 mutex_unlock(&dev->input_lock);
815 dev_err(&client->dev, "gc2235 startup err\n");
819 ret = gc2235_get_intg_factor(client, gc2235_info,
820 &gc2235_res[dev->fmt_idx]);
822 dev_err(&client->dev, "failed to get integration_factor\n");
825 mutex_unlock(&dev->input_lock);
829 static int gc2235_get_fmt(struct v4l2_subdev *sd,
830 struct v4l2_subdev_pad_config *cfg,
831 struct v4l2_subdev_format *format)
833 struct v4l2_mbus_framefmt *fmt = &format->format;
834 struct gc2235_device *dev = to_gc2235_sensor(sd);
842 fmt->width = gc2235_res[dev->fmt_idx].width;
843 fmt->height = gc2235_res[dev->fmt_idx].height;
844 fmt->code = MEDIA_BUS_FMT_SGRBG10_1X10;
849 static int gc2235_detect(struct i2c_client *client)
851 struct i2c_adapter *adapter = client->adapter;
856 if (!i2c_check_functionality(adapter, I2C_FUNC_I2C))
859 ret = gc2235_read_reg(client, GC2235_8BIT,
860 GC2235_SENSOR_ID_H, &high);
862 dev_err(&client->dev, "sensor_id_high = 0x%x\n", high);
865 ret = gc2235_read_reg(client, GC2235_8BIT,
866 GC2235_SENSOR_ID_L, &low);
867 id = ((high << 8) | low);
869 if (id != GC2235_ID) {
870 dev_err(&client->dev, "sensor ID error, 0x%x\n", id);
874 dev_info(&client->dev, "detect gc2235 success\n");
878 static int gc2235_s_stream(struct v4l2_subdev *sd, int enable)
880 struct gc2235_device *dev = to_gc2235_sensor(sd);
881 struct i2c_client *client = v4l2_get_subdevdata(sd);
883 mutex_lock(&dev->input_lock);
886 ret = gc2235_write_reg_array(client, gc2235_stream_on);
888 ret = gc2235_write_reg_array(client, gc2235_stream_off);
890 mutex_unlock(&dev->input_lock);
895 static int gc2235_s_config(struct v4l2_subdev *sd,
896 int irq, void *platform_data)
898 struct gc2235_device *dev = to_gc2235_sensor(sd);
899 struct i2c_client *client = v4l2_get_subdevdata(sd);
906 (struct camera_sensor_platform_data *)platform_data;
908 mutex_lock(&dev->input_lock);
909 if (dev->platform_data->platform_init) {
910 ret = dev->platform_data->platform_init(client);
912 dev_err(&client->dev, "platform init err\n");
913 goto platform_init_failed;
916 /* power off the module, then power on it in future
917 * as first power on by board may not fulfill the
918 * power on sequqence needed by the module
920 ret = power_down(sd);
922 dev_err(&client->dev, "gc2235 power-off err.\n");
928 dev_err(&client->dev, "gc2235 power-up err.\n");
932 ret = dev->platform_data->csi_cfg(sd, 1);
936 /* config & detect sensor */
937 ret = gc2235_detect(client);
939 dev_err(&client->dev, "gc2235_detect err s_config.\n");
943 /* turn off sensor, after probed */
944 ret = power_down(sd);
946 dev_err(&client->dev, "gc2235 power-off err.\n");
949 mutex_unlock(&dev->input_lock);
954 dev->platform_data->csi_cfg(sd, 0);
957 dev_err(&client->dev, "sensor power-gating failed\n");
959 if (dev->platform_data->platform_deinit)
960 dev->platform_data->platform_deinit();
961 platform_init_failed:
962 mutex_unlock(&dev->input_lock);
966 static int gc2235_g_parm(struct v4l2_subdev *sd,
967 struct v4l2_streamparm *param)
969 struct gc2235_device *dev = to_gc2235_sensor(sd);
970 struct i2c_client *client = v4l2_get_subdevdata(sd);
975 if (param->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
976 dev_err(&client->dev, "unsupported buffer type.\n");
980 memset(param, 0, sizeof(*param));
981 param->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
983 if (dev->fmt_idx >= 0 && dev->fmt_idx < N_RES) {
984 param->parm.capture.capability = V4L2_CAP_TIMEPERFRAME;
985 param->parm.capture.timeperframe.numerator = 1;
986 param->parm.capture.capturemode = dev->run_mode;
987 param->parm.capture.timeperframe.denominator =
988 gc2235_res[dev->fmt_idx].fps;
993 static int gc2235_s_parm(struct v4l2_subdev *sd,
994 struct v4l2_streamparm *param)
996 struct gc2235_device *dev = to_gc2235_sensor(sd);
997 dev->run_mode = param->parm.capture.capturemode;
999 mutex_lock(&dev->input_lock);
1000 switch (dev->run_mode) {
1002 gc2235_res = gc2235_res_video;
1003 N_RES = N_RES_VIDEO;
1005 case CI_MODE_STILL_CAPTURE:
1006 gc2235_res = gc2235_res_still;
1007 N_RES = N_RES_STILL;
1010 gc2235_res = gc2235_res_preview;
1011 N_RES = N_RES_PREVIEW;
1013 mutex_unlock(&dev->input_lock);
1017 static int gc2235_g_frame_interval(struct v4l2_subdev *sd,
1018 struct v4l2_subdev_frame_interval *interval)
1020 struct gc2235_device *dev = to_gc2235_sensor(sd);
1022 interval->interval.numerator = 1;
1023 interval->interval.denominator = gc2235_res[dev->fmt_idx].fps;
1028 static int gc2235_enum_mbus_code(struct v4l2_subdev *sd,
1029 struct v4l2_subdev_pad_config *cfg,
1030 struct v4l2_subdev_mbus_code_enum *code)
1032 if (code->index >= MAX_FMTS)
1035 code->code = MEDIA_BUS_FMT_SBGGR10_1X10;
1039 static int gc2235_enum_frame_size(struct v4l2_subdev *sd,
1040 struct v4l2_subdev_pad_config *cfg,
1041 struct v4l2_subdev_frame_size_enum *fse)
1043 int index = fse->index;
1048 fse->min_width = gc2235_res[index].width;
1049 fse->min_height = gc2235_res[index].height;
1050 fse->max_width = gc2235_res[index].width;
1051 fse->max_height = gc2235_res[index].height;
1057 static int gc2235_g_skip_frames(struct v4l2_subdev *sd, u32 *frames)
1059 struct gc2235_device *dev = to_gc2235_sensor(sd);
1061 mutex_lock(&dev->input_lock);
1062 *frames = gc2235_res[dev->fmt_idx].skip_frames;
1063 mutex_unlock(&dev->input_lock);
1068 static const struct v4l2_subdev_sensor_ops gc2235_sensor_ops = {
1069 .g_skip_frames = gc2235_g_skip_frames,
1072 static const struct v4l2_subdev_video_ops gc2235_video_ops = {
1073 .s_stream = gc2235_s_stream,
1074 .g_parm = gc2235_g_parm,
1075 .s_parm = gc2235_s_parm,
1076 .g_frame_interval = gc2235_g_frame_interval,
1079 static const struct v4l2_subdev_core_ops gc2235_core_ops = {
1080 .s_power = gc2235_s_power,
1081 .ioctl = gc2235_ioctl,
1084 static const struct v4l2_subdev_pad_ops gc2235_pad_ops = {
1085 .enum_mbus_code = gc2235_enum_mbus_code,
1086 .enum_frame_size = gc2235_enum_frame_size,
1087 .get_fmt = gc2235_get_fmt,
1088 .set_fmt = gc2235_set_fmt,
1091 static const struct v4l2_subdev_ops gc2235_ops = {
1092 .core = &gc2235_core_ops,
1093 .video = &gc2235_video_ops,
1094 .pad = &gc2235_pad_ops,
1095 .sensor = &gc2235_sensor_ops,
1098 static int gc2235_remove(struct i2c_client *client)
1100 struct v4l2_subdev *sd = i2c_get_clientdata(client);
1101 struct gc2235_device *dev = to_gc2235_sensor(sd);
1102 dev_dbg(&client->dev, "gc2235_remove...\n");
1104 if (dev->platform_data->platform_deinit)
1105 dev->platform_data->platform_deinit();
1107 dev->platform_data->csi_cfg(sd, 0);
1109 v4l2_device_unregister_subdev(sd);
1110 media_entity_cleanup(&dev->sd.entity);
1111 v4l2_ctrl_handler_free(&dev->ctrl_handler);
1117 static int gc2235_probe(struct i2c_client *client,
1118 const struct i2c_device_id *id)
1120 struct gc2235_device *dev;
1125 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1127 dev_err(&client->dev, "out of memory\n");
1131 mutex_init(&dev->input_lock);
1134 v4l2_i2c_subdev_init(&(dev->sd), client, &gc2235_ops);
1136 gcpdev = client->dev.platform_data;
1137 if (ACPI_COMPANION(&client->dev))
1138 gcpdev = gmin_camera_platform_data(&dev->sd,
1139 ATOMISP_INPUT_FORMAT_RAW_10,
1140 atomisp_bayer_order_grbg);
1142 ret = gc2235_s_config(&dev->sd, client->irq, gcpdev);
1146 dev->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
1147 dev->pad.flags = MEDIA_PAD_FL_SOURCE;
1148 dev->format.code = MEDIA_BUS_FMT_SBGGR10_1X10;
1149 dev->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
1151 v4l2_ctrl_handler_init(&dev->ctrl_handler,
1152 ARRAY_SIZE(gc2235_controls));
1154 gc2235_remove(client);
1158 for (i = 0; i < ARRAY_SIZE(gc2235_controls); i++)
1159 v4l2_ctrl_new_custom(&dev->ctrl_handler, &gc2235_controls[i],
1162 if (dev->ctrl_handler.error) {
1163 gc2235_remove(client);
1164 return dev->ctrl_handler.error;
1167 /* Use same lock for controls as for everything else. */
1168 dev->ctrl_handler.lock = &dev->input_lock;
1169 dev->sd.ctrl_handler = &dev->ctrl_handler;
1171 ret = media_entity_pads_init(&dev->sd.entity, 1, &dev->pad);
1173 gc2235_remove(client);
1175 if (ACPI_HANDLE(&client->dev))
1176 ret = atomisp_register_i2c_module(&dev->sd, gcpdev, RAW_CAMERA);
1180 v4l2_device_unregister_subdev(&dev->sd);
1186 static const struct acpi_device_id gc2235_acpi_match[] = {
1191 MODULE_DEVICE_TABLE(acpi, gc2235_acpi_match);
1192 MODULE_DEVICE_TABLE(i2c, gc2235_id);
1193 static struct i2c_driver gc2235_driver = {
1195 .name = GC2235_NAME,
1196 .acpi_match_table = ACPI_PTR(gc2235_acpi_match),
1198 .probe = gc2235_probe,
1199 .remove = gc2235_remove,
1200 .id_table = gc2235_id,
1203 static int init_gc2235(void)
1205 return i2c_add_driver(&gc2235_driver);
1208 static void exit_gc2235(void)
1211 i2c_del_driver(&gc2235_driver);
1214 module_init(init_gc2235);
1215 module_exit(exit_gc2235);
1217 MODULE_AUTHOR("Shuguang Gong <Shuguang.Gong@intel.com>");
1218 MODULE_DESCRIPTION("A low-level driver for GC2235 sensors");
1219 MODULE_LICENSE("GPL");