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.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22 #include <linux/module.h>
23 #include <linux/types.h>
24 #include <linux/kernel.h>
26 #include <linux/string.h>
27 #include <linux/errno.h>
28 #include <linux/init.h>
29 #include <linux/kmod.h>
30 #include <linux/device.h>
31 #include <linux/delay.h>
32 #include <linux/slab.h>
33 #include <linux/i2c.h>
34 #include <linux/gpio.h>
35 #include <linux/moduleparam.h>
36 #include <media/v4l2-device.h>
37 #include <linux/atomisp_gmin_platform.h>
38 #include <linux/acpi.h>
43 /* i2c read/write stuff */
44 static int gc2235_read_reg(struct i2c_client
*client
,
45 u16 data_length
, u16 reg
, u16
*val
)
48 struct i2c_msg msg
[2];
49 unsigned char data
[6];
51 if (!client
->adapter
) {
52 dev_err(&client
->dev
, "%s error, no client->adapter\n",
57 if (data_length
!= GC2235_8BIT
) {
58 dev_err(&client
->dev
, "%s error, invalid data length\n",
63 memset(msg
, 0 , sizeof(msg
));
65 msg
[0].addr
= client
->addr
;
70 /* high byte goes out first */
71 data
[0] = (u8
)(reg
& 0xff);
73 msg
[1].addr
= client
->addr
;
74 msg
[1].len
= data_length
;
75 msg
[1].flags
= I2C_M_RD
;
78 err
= i2c_transfer(client
->adapter
, msg
, 2);
83 "read from offset 0x%x error %d", reg
, err
);
88 /* high byte comes first */
89 if (data_length
== GC2235_8BIT
)
95 static int gc2235_i2c_write(struct i2c_client
*client
, u16 len
, u8
*data
)
98 const int num_msg
= 1;
101 msg
.addr
= client
->addr
;
105 ret
= i2c_transfer(client
->adapter
, &msg
, 1);
107 return ret
== num_msg
? 0 : -EIO
;
110 static int gc2235_write_reg(struct i2c_client
*client
, u16 data_length
,
114 unsigned char data
[4] = {0};
115 const u16 len
= data_length
+ sizeof(u8
); /* 16-bit address + data */
117 if (data_length
!= GC2235_8BIT
) {
118 dev_err(&client
->dev
,
119 "%s error, invalid data_length\n", __func__
);
123 /* high byte goes out first */
127 ret
= gc2235_i2c_write(client
, len
, data
);
129 dev_err(&client
->dev
,
130 "write error: wrote 0x%x to offset 0x%x error %d",
136 static int __gc2235_flush_reg_array(struct i2c_client
*client
,
137 struct gc2235_write_ctrl
*ctrl
)
141 if (ctrl
->index
== 0)
144 size
= sizeof(u8
) + ctrl
->index
; /* 8-bit address + data */
147 return gc2235_i2c_write(client
, size
, (u8
*)&ctrl
->buffer
);
150 static int __gc2235_buf_reg_array(struct i2c_client
*client
,
151 struct gc2235_write_ctrl
*ctrl
,
152 const struct gc2235_reg
*next
)
156 if (next
->type
!= GC2235_8BIT
)
160 ctrl
->buffer
.data
[ctrl
->index
] = (u8
)next
->val
;
162 /* When first item is added, we need to store its starting address */
163 if (ctrl
->index
== 0)
164 ctrl
->buffer
.addr
= next
->reg
;
169 * Buffer cannot guarantee free space for u32? Better flush it to avoid
170 * possible lack of memory for next item.
172 if (ctrl
->index
+ sizeof(u8
) >= GC2235_MAX_WRITE_BUF_SIZE
)
173 return __gc2235_flush_reg_array(client
, ctrl
);
177 static int __gc2235_write_reg_is_consecutive(struct i2c_client
*client
,
178 struct gc2235_write_ctrl
*ctrl
,
179 const struct gc2235_reg
*next
)
181 if (ctrl
->index
== 0)
184 return ctrl
->buffer
.addr
+ ctrl
->index
== next
->reg
;
186 static int gc2235_write_reg_array(struct i2c_client
*client
,
187 const struct gc2235_reg
*reglist
)
189 const struct gc2235_reg
*next
= reglist
;
190 struct gc2235_write_ctrl ctrl
;
194 for (; next
->type
!= GC2235_TOK_TERM
; next
++) {
195 switch (next
->type
& GC2235_TOK_MASK
) {
196 case GC2235_TOK_DELAY
:
197 err
= __gc2235_flush_reg_array(client
, &ctrl
);
204 * If next address is not consecutive, data needs to be
205 * flushed before proceed.
207 if (!__gc2235_write_reg_is_consecutive(client
, &ctrl
,
209 err
= __gc2235_flush_reg_array(client
, &ctrl
);
213 err
= __gc2235_buf_reg_array(client
, &ctrl
, next
);
215 dev_err(&client
->dev
, "%s: write error, aborted\n",
223 return __gc2235_flush_reg_array(client
, &ctrl
);
226 static int gc2235_g_focal(struct v4l2_subdev
*sd
, s32
*val
)
228 *val
= (GC2235_FOCAL_LENGTH_NUM
<< 16) | GC2235_FOCAL_LENGTH_DEM
;
232 static int gc2235_g_fnumber(struct v4l2_subdev
*sd
, s32
*val
)
234 /*const f number for imx*/
235 *val
= (GC2235_F_NUMBER_DEFAULT_NUM
<< 16) | GC2235_F_NUMBER_DEM
;
239 static int gc2235_g_fnumber_range(struct v4l2_subdev
*sd
, s32
*val
)
241 *val
= (GC2235_F_NUMBER_DEFAULT_NUM
<< 24) |
242 (GC2235_F_NUMBER_DEM
<< 16) |
243 (GC2235_F_NUMBER_DEFAULT_NUM
<< 8) | GC2235_F_NUMBER_DEM
;
248 static int gc2235_get_intg_factor(struct i2c_client
*client
,
249 struct camera_mipi_info
*info
,
250 const struct gc2235_resolution
*res
)
252 struct v4l2_subdev
*sd
= i2c_get_clientdata(client
);
253 struct gc2235_device
*dev
= to_gc2235_sensor(sd
);
254 struct atomisp_sensor_mode_data
*buf
= &info
->data
;
255 u16 reg_val
, reg_val_h
, dummy
;
261 /* pixel clock calculattion */
262 buf
->vt_pix_clk_freq_mhz
= dev
->vt_pix_clk_freq_mhz
= 30000000;
264 /* get integration time */
265 buf
->coarse_integration_time_min
= GC2235_COARSE_INTG_TIME_MIN
;
266 buf
->coarse_integration_time_max_margin
=
267 GC2235_COARSE_INTG_TIME_MAX_MARGIN
;
269 buf
->fine_integration_time_min
= GC2235_FINE_INTG_TIME_MIN
;
270 buf
->fine_integration_time_max_margin
=
271 GC2235_FINE_INTG_TIME_MAX_MARGIN
;
273 buf
->fine_integration_time_def
= GC2235_FINE_INTG_TIME_MIN
;
274 buf
->frame_length_lines
= res
->lines_per_frame
;
275 buf
->line_length_pck
= res
->pixels_per_line
;
276 buf
->read_mode
= res
->bin_mode
;
278 /* get the cropping and output resolution to ISP for this mode. */
279 ret
= gc2235_read_reg(client
, GC2235_8BIT
,
280 GC2235_H_CROP_START_H
, ®_val_h
);
281 ret
= gc2235_read_reg(client
, GC2235_8BIT
,
282 GC2235_H_CROP_START_L
, ®_val
);
286 buf
->crop_horizontal_start
= ((u16
)reg_val_h
<< 8) | (u16
)reg_val
;
288 ret
= gc2235_read_reg(client
, GC2235_8BIT
,
289 GC2235_V_CROP_START_H
, ®_val_h
);
290 ret
= gc2235_read_reg(client
, GC2235_8BIT
,
291 GC2235_V_CROP_START_L
, ®_val
);
295 buf
->crop_vertical_start
= ((u16
)reg_val_h
<< 8) | (u16
)reg_val
;
297 ret
= gc2235_read_reg(client
, GC2235_8BIT
,
298 GC2235_H_OUTSIZE_H
, ®_val_h
);
299 ret
= gc2235_read_reg(client
, GC2235_8BIT
,
300 GC2235_H_OUTSIZE_L
, ®_val
);
303 buf
->output_width
= ((u16
)reg_val_h
<< 8) | (u16
)reg_val
;
305 ret
= gc2235_read_reg(client
, GC2235_8BIT
,
306 GC2235_V_OUTSIZE_H
, ®_val_h
);
307 ret
= gc2235_read_reg(client
, GC2235_8BIT
,
308 GC2235_V_OUTSIZE_L
, ®_val
);
311 buf
->output_height
= ((u16
)reg_val_h
<< 8) | (u16
)reg_val
;
313 buf
->crop_horizontal_end
= buf
->crop_horizontal_start
+
314 buf
->output_width
- 1;
315 buf
->crop_vertical_end
= buf
->crop_vertical_start
+
316 buf
->output_height
- 1;
318 ret
= gc2235_read_reg(client
, GC2235_8BIT
,
319 GC2235_HB_H
, ®_val_h
);
320 ret
= gc2235_read_reg(client
, GC2235_8BIT
,
321 GC2235_HB_L
, ®_val
);
325 dummy
= ((u16
)reg_val_h
<< 8) | (u16
)reg_val
;
327 ret
= gc2235_read_reg(client
, GC2235_8BIT
,
328 GC2235_SH_DELAY_H
, ®_val_h
);
329 ret
= gc2235_read_reg(client
, GC2235_8BIT
,
330 GC2235_SH_DELAY_L
, ®_val
);
333 buf
->line_length_pck
= buf
->output_width
+ 16 + dummy
+
334 (((u16
)reg_val_h
<< 8) | (u16
)reg_val
) + 4;
336 ret
= gc2235_read_reg(client
, GC2235_8BIT
,
337 GC2235_VB_H
, ®_val_h
);
338 ret
= gc2235_read_reg(client
, GC2235_8BIT
,
339 GC2235_VB_L
, ®_val
);
344 buf
->frame_length_lines
= buf
->output_height
+ 32 +
345 (((u16
)reg_val_h
<< 8) | (u16
)reg_val
);
347 buf
->binning_factor_x
= res
->bin_factor_x
?
348 res
->bin_factor_x
: 1;
349 buf
->binning_factor_y
= res
->bin_factor_y
?
350 res
->bin_factor_y
: 1;
354 static long __gc2235_set_exposure(struct v4l2_subdev
*sd
, int coarse_itg
,
355 int gain
, int digitgain
)
358 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
359 u16 coarse_integration
= (u16
)coarse_itg
;
361 u16 expo_coarse_h
, expo_coarse_l
, gain_val
= 0xF0, gain_val2
= 0xF0;
362 expo_coarse_h
= coarse_integration
>>8;
363 expo_coarse_l
= coarse_integration
& 0xff;
365 ret
= gc2235_write_reg(client
, GC2235_8BIT
,
366 GC2235_EXPOSURE_H
, expo_coarse_h
);
367 ret
= gc2235_write_reg(client
, GC2235_8BIT
,
368 GC2235_EXPOSURE_L
, expo_coarse_l
);
373 } else if (gain
< 256) {
377 gain_val2
= 64 * gain
/ 256;
381 ret
= gc2235_write_reg(client
, GC2235_8BIT
,
382 GC2235_GLOBAL_GAIN
, (u8
)gain_val
);
383 ret
= gc2235_write_reg(client
, GC2235_8BIT
,
384 GC2235_PRE_GAIN
, (u8
)gain_val2
);
390 static int gc2235_set_exposure(struct v4l2_subdev
*sd
, int exposure
,
391 int gain
, int digitgain
)
393 struct gc2235_device
*dev
= to_gc2235_sensor(sd
);
396 mutex_lock(&dev
->input_lock
);
397 ret
= __gc2235_set_exposure(sd
, exposure
, gain
, digitgain
);
398 mutex_unlock(&dev
->input_lock
);
403 static long gc2235_s_exposure(struct v4l2_subdev
*sd
,
404 struct atomisp_exposure
*exposure
)
406 int exp
= exposure
->integration_time
[0];
407 int gain
= exposure
->gain
[0];
408 int digitgain
= exposure
->gain
[1];
410 /* we should not accept the invalid value below. */
412 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
413 v4l2_err(client
, "%s: invalid value\n", __func__
);
417 return gc2235_set_exposure(sd
, exp
, gain
, digitgain
);
419 static long gc2235_ioctl(struct v4l2_subdev
*sd
, unsigned int cmd
, void *arg
)
422 case ATOMISP_IOC_S_EXPOSURE
:
423 return gc2235_s_exposure(sd
, arg
);
429 /* This returns the exposure time being used. This should only be used
430 for filling in EXIF data, not for actual image processing. */
431 static int gc2235_q_exposure(struct v4l2_subdev
*sd
, s32
*value
)
433 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
438 ret
= gc2235_read_reg(client
, GC2235_8BIT
,
444 ret
= gc2235_read_reg(client
, GC2235_8BIT
,
450 reg_v
+= reg_v2
<< 8;
457 static int gc2235_g_volatile_ctrl(struct v4l2_ctrl
*ctrl
)
459 struct gc2235_device
*dev
=
460 container_of(ctrl
->handler
, struct gc2235_device
, ctrl_handler
);
464 case V4L2_CID_EXPOSURE_ABSOLUTE
:
465 ret
= gc2235_q_exposure(&dev
->sd
, &ctrl
->val
);
467 case V4L2_CID_FOCAL_ABSOLUTE
:
468 ret
= gc2235_g_focal(&dev
->sd
, &ctrl
->val
);
470 case V4L2_CID_FNUMBER_ABSOLUTE
:
471 ret
= gc2235_g_fnumber(&dev
->sd
, &ctrl
->val
);
473 case V4L2_CID_FNUMBER_RANGE
:
474 ret
= gc2235_g_fnumber_range(&dev
->sd
, &ctrl
->val
);
483 static const struct v4l2_ctrl_ops ctrl_ops
= {
484 .g_volatile_ctrl
= gc2235_g_volatile_ctrl
487 struct v4l2_ctrl_config gc2235_controls
[] = {
490 .id
= V4L2_CID_EXPOSURE_ABSOLUTE
,
491 .type
= V4L2_CTRL_TYPE_INTEGER
,
501 .id
= V4L2_CID_FOCAL_ABSOLUTE
,
502 .type
= V4L2_CTRL_TYPE_INTEGER
,
503 .name
= "focal length",
504 .min
= GC2235_FOCAL_LENGTH_DEFAULT
,
505 .max
= GC2235_FOCAL_LENGTH_DEFAULT
,
507 .def
= GC2235_FOCAL_LENGTH_DEFAULT
,
512 .id
= V4L2_CID_FNUMBER_ABSOLUTE
,
513 .type
= V4L2_CTRL_TYPE_INTEGER
,
515 .min
= GC2235_F_NUMBER_DEFAULT
,
516 .max
= GC2235_F_NUMBER_DEFAULT
,
518 .def
= GC2235_F_NUMBER_DEFAULT
,
523 .id
= V4L2_CID_FNUMBER_RANGE
,
524 .type
= V4L2_CTRL_TYPE_INTEGER
,
525 .name
= "f-number range",
526 .min
= GC2235_F_NUMBER_RANGE
,
527 .max
= GC2235_F_NUMBER_RANGE
,
529 .def
= GC2235_F_NUMBER_RANGE
,
534 static int __gc2235_init(struct v4l2_subdev
*sd
)
536 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
538 /* restore settings */
539 gc2235_res
= gc2235_res_preview
;
540 N_RES
= N_RES_PREVIEW
;
542 return gc2235_write_reg_array(client
, gc2235_init_settings
);
546 static int gc2235_init(struct v4l2_subdev
*sd
)
549 ret
= __gc2235_init(sd
);
554 static int power_ctrl(struct v4l2_subdev
*sd
, bool flag
)
557 struct gc2235_device
*dev
= to_gc2235_sensor(sd
);
559 if (!dev
|| !dev
->platform_data
)
562 /* Non-gmin platforms use the legacy callback */
563 if (dev
->platform_data
->power_ctrl
)
564 return dev
->platform_data
->power_ctrl(sd
, flag
);
567 ret
= dev
->platform_data
->v1p8_ctrl(sd
, 1);
568 usleep_range(60, 90);
570 ret
|= dev
->platform_data
->v2p8_ctrl(sd
, 1);
572 ret
= dev
->platform_data
->v1p8_ctrl(sd
, 0);
573 ret
|= dev
->platform_data
->v2p8_ctrl(sd
, 0);
578 static int gpio_ctrl(struct v4l2_subdev
*sd
, bool flag
)
580 struct gc2235_device
*dev
= to_gc2235_sensor(sd
);
583 if (!dev
|| !dev
->platform_data
)
586 /* Non-gmin platforms use the legacy callback */
587 if (dev
->platform_data
->gpio_ctrl
)
588 return dev
->platform_data
->gpio_ctrl(sd
, flag
);
590 ret
|= dev
->platform_data
->gpio1_ctrl(sd
, !flag
);
591 usleep_range(60, 90);
592 ret
= dev
->platform_data
->gpio0_ctrl(sd
, flag
);
597 static int power_up(struct v4l2_subdev
*sd
)
599 struct gc2235_device
*dev
= to_gc2235_sensor(sd
);
600 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
603 if (NULL
== dev
->platform_data
) {
604 dev_err(&client
->dev
,
605 "no camera_sensor_platform_data");
609 ret
= power_ctrl(sd
, 1);
613 /* according to DS, at least 5ms is needed between DOVDD and PWDN */
614 usleep_range(5000, 6000);
616 ret
= dev
->platform_data
->flisclk_ctrl(sd
, 1);
619 usleep_range(5000, 6000);
622 ret
= gpio_ctrl(sd
, 1);
624 ret
= gpio_ctrl(sd
, 1);
636 dev_err(&client
->dev
, "sensor power-up failed\n");
641 static int power_down(struct v4l2_subdev
*sd
)
643 struct gc2235_device
*dev
= to_gc2235_sensor(sd
);
644 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
647 if (NULL
== dev
->platform_data
) {
648 dev_err(&client
->dev
,
649 "no camera_sensor_platform_data");
653 ret
= gpio_ctrl(sd
, 0);
655 ret
= gpio_ctrl(sd
, 0);
657 dev_err(&client
->dev
, "gpio failed 2\n");
660 ret
= dev
->platform_data
->flisclk_ctrl(sd
, 0);
662 dev_err(&client
->dev
, "flisclk failed\n");
665 ret
= power_ctrl(sd
, 0);
667 dev_err(&client
->dev
, "vprog failed.\n");
672 static int gc2235_s_power(struct v4l2_subdev
*sd
, int on
)
677 ret
= power_down(sd
);
681 ret
= gc2235_init(sd
);
688 * distance - calculate the distance
693 * Get the gap between resolution and w/h.
694 * res->width/height smaller than w/h wouldn't be considered.
695 * Returns the value of gap or -1 if fail.
697 #define LARGEST_ALLOWED_RATIO_MISMATCH 800
698 static int distance(struct gc2235_resolution
*res
, u32 w
, u32 h
)
700 unsigned int w_ratio
= ((res
->width
<< 13)/w
);
701 unsigned int h_ratio
;
706 h_ratio
= ((res
->height
<< 13) / h
);
709 match
= abs(((w_ratio
<< 13) / h_ratio
) - ((int)8192));
711 if ((w_ratio
< (int)8192) || (h_ratio
< (int)8192) ||
712 (match
> LARGEST_ALLOWED_RATIO_MISMATCH
))
715 return w_ratio
+ h_ratio
;
718 /* Return the nearest higher resolution index */
719 static int nearest_resolution_index(int w
, int h
)
724 int min_dist
= INT_MAX
;
725 struct gc2235_resolution
*tmp_res
= NULL
;
727 for (i
= 0; i
< N_RES
; i
++) {
728 tmp_res
= &gc2235_res
[i
];
729 dist
= distance(tmp_res
, w
, h
);
732 if (dist
< min_dist
) {
741 static int get_resolution_index(int w
, int h
)
745 for (i
= 0; i
< N_RES
; i
++) {
746 if (w
!= gc2235_res
[i
].width
)
748 if (h
!= gc2235_res
[i
].height
)
757 static int startup(struct v4l2_subdev
*sd
)
759 struct gc2235_device
*dev
= to_gc2235_sensor(sd
);
760 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
763 /* force gc2235 to do a reset in res change, otherwise it
764 * can not output normal after switching res. and it is not
765 * necessary for first time run up after power on, for the sack
770 gc2235_write_reg_array(client
, gc2235_init_settings
);
773 ret
= gc2235_write_reg_array(client
, gc2235_res
[dev
->fmt_idx
].regs
);
775 dev_err(&client
->dev
, "gc2235 write register err.\n");
783 static int gc2235_set_fmt(struct v4l2_subdev
*sd
,
784 struct v4l2_subdev_pad_config
*cfg
,
785 struct v4l2_subdev_format
*format
)
788 struct v4l2_mbus_framefmt
*fmt
= &format
->format
;
789 struct gc2235_device
*dev
= to_gc2235_sensor(sd
);
790 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
791 struct camera_mipi_info
*gc2235_info
= NULL
;
795 gc2235_info
= v4l2_get_subdev_hostdata(sd
);
796 if (gc2235_info
== NULL
)
802 mutex_lock(&dev
->input_lock
);
803 idx
= nearest_resolution_index(fmt
->width
, fmt
->height
);
805 /* return the largest resolution */
806 fmt
->width
= gc2235_res
[N_RES
- 1].width
;
807 fmt
->height
= gc2235_res
[N_RES
- 1].height
;
809 fmt
->width
= gc2235_res
[idx
].width
;
810 fmt
->height
= gc2235_res
[idx
].height
;
812 fmt
->code
= MEDIA_BUS_FMT_SGRBG10_1X10
;
813 if (format
->which
== V4L2_SUBDEV_FORMAT_TRY
) {
815 mutex_unlock(&dev
->input_lock
);
819 dev
->fmt_idx
= get_resolution_index(fmt
->width
, fmt
->height
);
820 if (dev
->fmt_idx
== -1) {
821 dev_err(&client
->dev
, "get resolution fail\n");
822 mutex_unlock(&dev
->input_lock
);
828 dev_err(&client
->dev
, "gc2235 startup err\n");
832 ret
= gc2235_get_intg_factor(client
, gc2235_info
,
833 &gc2235_res
[dev
->fmt_idx
]);
835 dev_err(&client
->dev
, "failed to get integration_factor\n");
838 mutex_unlock(&dev
->input_lock
);
842 static int gc2235_get_fmt(struct v4l2_subdev
*sd
,
843 struct v4l2_subdev_pad_config
*cfg
,
844 struct v4l2_subdev_format
*format
)
846 struct v4l2_mbus_framefmt
*fmt
= &format
->format
;
847 struct gc2235_device
*dev
= to_gc2235_sensor(sd
);
855 fmt
->width
= gc2235_res
[dev
->fmt_idx
].width
;
856 fmt
->height
= gc2235_res
[dev
->fmt_idx
].height
;
857 fmt
->code
= MEDIA_BUS_FMT_SGRBG10_1X10
;
862 static int gc2235_detect(struct i2c_client
*client
)
864 struct i2c_adapter
*adapter
= client
->adapter
;
869 if (!i2c_check_functionality(adapter
, I2C_FUNC_I2C
))
872 ret
= gc2235_read_reg(client
, GC2235_8BIT
,
873 GC2235_SENSOR_ID_H
, &high
);
875 dev_err(&client
->dev
, "sensor_id_high = 0x%x\n", high
);
878 ret
= gc2235_read_reg(client
, GC2235_8BIT
,
879 GC2235_SENSOR_ID_L
, &low
);
880 id
= ((((u16
) high
) << 8) | (u16
) low
);
882 if (id
!= GC2235_ID
) {
883 dev_err(&client
->dev
, "sensor ID error, 0x%x\n", id
);
887 dev_info(&client
->dev
, "detect gc2235 success\n");
891 static int gc2235_s_stream(struct v4l2_subdev
*sd
, int enable
)
893 struct gc2235_device
*dev
= to_gc2235_sensor(sd
);
894 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
896 mutex_lock(&dev
->input_lock
);
899 ret
= gc2235_write_reg_array(client
, gc2235_stream_on
);
901 ret
= gc2235_write_reg_array(client
, gc2235_stream_off
);
903 mutex_unlock(&dev
->input_lock
);
908 static int gc2235_s_config(struct v4l2_subdev
*sd
,
909 int irq
, void *platform_data
)
911 struct gc2235_device
*dev
= to_gc2235_sensor(sd
);
912 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
915 if (platform_data
== NULL
)
919 (struct camera_sensor_platform_data
*)platform_data
;
921 mutex_lock(&dev
->input_lock
);
922 if (dev
->platform_data
->platform_init
) {
923 ret
= dev
->platform_data
->platform_init(client
);
925 dev_err(&client
->dev
, "platform init err\n");
926 goto platform_init_failed
;
929 /* power off the module, then power on it in future
930 * as first power on by board may not fulfill the
931 * power on sequqence needed by the module
933 ret
= power_down(sd
);
935 dev_err(&client
->dev
, "gc2235 power-off err.\n");
941 dev_err(&client
->dev
, "gc2235 power-up err.\n");
945 ret
= dev
->platform_data
->csi_cfg(sd
, 1);
949 /* config & detect sensor */
950 ret
= gc2235_detect(client
);
952 dev_err(&client
->dev
, "gc2235_detect err s_config.\n");
956 /* turn off sensor, after probed */
957 ret
= power_down(sd
);
959 dev_err(&client
->dev
, "gc2235 power-off err.\n");
962 mutex_unlock(&dev
->input_lock
);
967 dev
->platform_data
->csi_cfg(sd
, 0);
970 dev_err(&client
->dev
, "sensor power-gating failed\n");
972 if (dev
->platform_data
->platform_deinit
)
973 dev
->platform_data
->platform_deinit();
974 platform_init_failed
:
975 mutex_unlock(&dev
->input_lock
);
979 static int gc2235_g_parm(struct v4l2_subdev
*sd
,
980 struct v4l2_streamparm
*param
)
982 struct gc2235_device
*dev
= to_gc2235_sensor(sd
);
983 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
988 if (param
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
) {
989 dev_err(&client
->dev
, "unsupported buffer type.\n");
993 memset(param
, 0, sizeof(*param
));
994 param
->type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
996 if (dev
->fmt_idx
>= 0 && dev
->fmt_idx
< N_RES
) {
997 param
->parm
.capture
.capability
= V4L2_CAP_TIMEPERFRAME
;
998 param
->parm
.capture
.timeperframe
.numerator
= 1;
999 param
->parm
.capture
.capturemode
= dev
->run_mode
;
1000 param
->parm
.capture
.timeperframe
.denominator
=
1001 gc2235_res
[dev
->fmt_idx
].fps
;
1006 static int gc2235_s_parm(struct v4l2_subdev
*sd
,
1007 struct v4l2_streamparm
*param
)
1009 struct gc2235_device
*dev
= to_gc2235_sensor(sd
);
1010 dev
->run_mode
= param
->parm
.capture
.capturemode
;
1012 mutex_lock(&dev
->input_lock
);
1013 switch (dev
->run_mode
) {
1015 gc2235_res
= gc2235_res_video
;
1016 N_RES
= N_RES_VIDEO
;
1018 case CI_MODE_STILL_CAPTURE
:
1019 gc2235_res
= gc2235_res_still
;
1020 N_RES
= N_RES_STILL
;
1023 gc2235_res
= gc2235_res_preview
;
1024 N_RES
= N_RES_PREVIEW
;
1026 mutex_unlock(&dev
->input_lock
);
1030 static int gc2235_g_frame_interval(struct v4l2_subdev
*sd
,
1031 struct v4l2_subdev_frame_interval
*interval
)
1033 struct gc2235_device
*dev
= to_gc2235_sensor(sd
);
1035 interval
->interval
.numerator
= 1;
1036 interval
->interval
.denominator
= gc2235_res
[dev
->fmt_idx
].fps
;
1041 static int gc2235_enum_mbus_code(struct v4l2_subdev
*sd
,
1042 struct v4l2_subdev_pad_config
*cfg
,
1043 struct v4l2_subdev_mbus_code_enum
*code
)
1045 if (code
->index
>= MAX_FMTS
)
1048 code
->code
= MEDIA_BUS_FMT_SBGGR10_1X10
;
1052 static int gc2235_enum_frame_size(struct v4l2_subdev
*sd
,
1053 struct v4l2_subdev_pad_config
*cfg
,
1054 struct v4l2_subdev_frame_size_enum
*fse
)
1056 int index
= fse
->index
;
1061 fse
->min_width
= gc2235_res
[index
].width
;
1062 fse
->min_height
= gc2235_res
[index
].height
;
1063 fse
->max_width
= gc2235_res
[index
].width
;
1064 fse
->max_height
= gc2235_res
[index
].height
;
1070 static int gc2235_g_skip_frames(struct v4l2_subdev
*sd
, u32
*frames
)
1072 struct gc2235_device
*dev
= to_gc2235_sensor(sd
);
1074 mutex_lock(&dev
->input_lock
);
1075 *frames
= gc2235_res
[dev
->fmt_idx
].skip_frames
;
1076 mutex_unlock(&dev
->input_lock
);
1081 static const struct v4l2_subdev_sensor_ops gc2235_sensor_ops
= {
1082 .g_skip_frames
= gc2235_g_skip_frames
,
1085 static const struct v4l2_subdev_video_ops gc2235_video_ops
= {
1086 .s_stream
= gc2235_s_stream
,
1087 .g_parm
= gc2235_g_parm
,
1088 .s_parm
= gc2235_s_parm
,
1089 .g_frame_interval
= gc2235_g_frame_interval
,
1092 static const struct v4l2_subdev_core_ops gc2235_core_ops
= {
1093 .s_power
= gc2235_s_power
,
1094 .ioctl
= gc2235_ioctl
,
1097 static const struct v4l2_subdev_pad_ops gc2235_pad_ops
= {
1098 .enum_mbus_code
= gc2235_enum_mbus_code
,
1099 .enum_frame_size
= gc2235_enum_frame_size
,
1100 .get_fmt
= gc2235_get_fmt
,
1101 .set_fmt
= gc2235_set_fmt
,
1104 static const struct v4l2_subdev_ops gc2235_ops
= {
1105 .core
= &gc2235_core_ops
,
1106 .video
= &gc2235_video_ops
,
1107 .pad
= &gc2235_pad_ops
,
1108 .sensor
= &gc2235_sensor_ops
,
1111 static int gc2235_remove(struct i2c_client
*client
)
1113 struct v4l2_subdev
*sd
= i2c_get_clientdata(client
);
1114 struct gc2235_device
*dev
= to_gc2235_sensor(sd
);
1115 dev_dbg(&client
->dev
, "gc2235_remove...\n");
1117 if (dev
->platform_data
->platform_deinit
)
1118 dev
->platform_data
->platform_deinit();
1120 dev
->platform_data
->csi_cfg(sd
, 0);
1122 v4l2_device_unregister_subdev(sd
);
1123 media_entity_cleanup(&dev
->sd
.entity
);
1124 v4l2_ctrl_handler_free(&dev
->ctrl_handler
);
1130 static int gc2235_probe(struct i2c_client
*client
,
1131 const struct i2c_device_id
*id
)
1133 struct gc2235_device
*dev
;
1138 dev
= kzalloc(sizeof(*dev
), GFP_KERNEL
);
1140 dev_err(&client
->dev
, "out of memory\n");
1144 mutex_init(&dev
->input_lock
);
1147 v4l2_i2c_subdev_init(&(dev
->sd
), client
, &gc2235_ops
);
1149 gcpdev
= client
->dev
.platform_data
;
1150 if (ACPI_COMPANION(&client
->dev
))
1151 gcpdev
= gmin_camera_platform_data(&dev
->sd
,
1152 ATOMISP_INPUT_FORMAT_RAW_10
,
1153 atomisp_bayer_order_grbg
);
1155 ret
= gc2235_s_config(&dev
->sd
, client
->irq
, gcpdev
);
1159 dev
->sd
.flags
|= V4L2_SUBDEV_FL_HAS_DEVNODE
;
1160 dev
->pad
.flags
= MEDIA_PAD_FL_SOURCE
;
1161 dev
->format
.code
= MEDIA_BUS_FMT_SBGGR10_1X10
;
1162 dev
->sd
.entity
.function
= MEDIA_ENT_F_CAM_SENSOR
;
1164 v4l2_ctrl_handler_init(&dev
->ctrl_handler
,
1165 ARRAY_SIZE(gc2235_controls
));
1167 gc2235_remove(client
);
1171 for (i
= 0; i
< ARRAY_SIZE(gc2235_controls
); i
++)
1172 v4l2_ctrl_new_custom(&dev
->ctrl_handler
, &gc2235_controls
[i
],
1175 if (dev
->ctrl_handler
.error
) {
1176 gc2235_remove(client
);
1177 return dev
->ctrl_handler
.error
;
1180 /* Use same lock for controls as for everything else. */
1181 dev
->ctrl_handler
.lock
= &dev
->input_lock
;
1182 dev
->sd
.ctrl_handler
= &dev
->ctrl_handler
;
1184 ret
= media_entity_pads_init(&dev
->sd
.entity
, 1, &dev
->pad
);
1186 gc2235_remove(client
);
1188 if (ACPI_HANDLE(&client
->dev
))
1189 ret
= atomisp_register_i2c_module(&dev
->sd
, gcpdev
, RAW_CAMERA
);
1193 v4l2_device_unregister_subdev(&dev
->sd
);
1199 static struct acpi_device_id gc2235_acpi_match
[] = {
1204 MODULE_DEVICE_TABLE(acpi
, gc2235_acpi_match
);
1205 MODULE_DEVICE_TABLE(i2c
, gc2235_id
);
1206 static struct i2c_driver gc2235_driver
= {
1208 .owner
= THIS_MODULE
,
1209 .name
= GC2235_NAME
,
1210 .acpi_match_table
= ACPI_PTR(gc2235_acpi_match
),
1212 .probe
= gc2235_probe
,
1213 .remove
= gc2235_remove
,
1214 .id_table
= gc2235_id
,
1217 static int init_gc2235(void)
1219 return i2c_add_driver(&gc2235_driver
);
1222 static void exit_gc2235(void)
1225 i2c_del_driver(&gc2235_driver
);
1228 module_init(init_gc2235
);
1229 module_exit(exit_gc2235
);
1231 MODULE_AUTHOR("Shuguang Gong <Shuguang.Gong@intel.com>");
1232 MODULE_DESCRIPTION("A low-level driver for GC2235 sensors");
1233 MODULE_LICENSE("GPL");