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/moduleparam.h>
30 #include <media/v4l2-device.h>
31 #include "../include/linux/atomisp_gmin_platform.h"
32 #include <linux/acpi.h>
37 /* i2c read/write stuff */
38 static int gc2235_read_reg(struct i2c_client
*client
,
39 u16 data_length
, u16 reg
, u16
*val
)
42 struct i2c_msg msg
[2];
43 unsigned char data
[6];
45 if (!client
->adapter
) {
46 dev_err(&client
->dev
, "%s error, no client->adapter\n",
51 if (data_length
!= GC2235_8BIT
) {
52 dev_err(&client
->dev
, "%s error, invalid data length\n",
57 memset(msg
, 0, sizeof(msg
));
59 msg
[0].addr
= client
->addr
;
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
== GC2235_8BIT
)
89 static int gc2235_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 gc2235_write_reg(struct i2c_client
*client
, u16 data_length
,
108 unsigned char data
[4] = {0};
109 const u16 len
= data_length
+ sizeof(u8
); /* 16-bit address + data */
111 if (data_length
!= GC2235_8BIT
) {
112 dev_err(&client
->dev
,
113 "%s error, invalid data_length\n", __func__
);
117 /* high byte goes out first */
121 ret
= gc2235_i2c_write(client
, len
, data
);
123 dev_err(&client
->dev
,
124 "write error: wrote 0x%x to offset 0x%x error %d",
130 static int __gc2235_flush_reg_array(struct i2c_client
*client
,
131 struct gc2235_write_ctrl
*ctrl
)
135 if (ctrl
->index
== 0)
138 size
= sizeof(u8
) + ctrl
->index
; /* 8-bit address + data */
141 return gc2235_i2c_write(client
, size
, (u8
*)&ctrl
->buffer
);
144 static int __gc2235_buf_reg_array(struct i2c_client
*client
,
145 struct gc2235_write_ctrl
*ctrl
,
146 const struct gc2235_reg
*next
)
150 if (next
->type
!= GC2235_8BIT
)
154 ctrl
->buffer
.data
[ctrl
->index
] = (u8
)next
->val
;
156 /* When first item is added, we need to store its starting address */
157 if (ctrl
->index
== 0)
158 ctrl
->buffer
.addr
= next
->reg
;
163 * Buffer cannot guarantee free space for u32? Better flush it to avoid
164 * possible lack of memory for next item.
166 if (ctrl
->index
+ sizeof(u8
) >= GC2235_MAX_WRITE_BUF_SIZE
)
167 return __gc2235_flush_reg_array(client
, ctrl
);
171 static int __gc2235_write_reg_is_consecutive(struct i2c_client
*client
,
172 struct gc2235_write_ctrl
*ctrl
,
173 const struct gc2235_reg
*next
)
175 if (ctrl
->index
== 0)
178 return ctrl
->buffer
.addr
+ ctrl
->index
== next
->reg
;
180 static int gc2235_write_reg_array(struct i2c_client
*client
,
181 const struct gc2235_reg
*reglist
)
183 const struct gc2235_reg
*next
= reglist
;
184 struct gc2235_write_ctrl ctrl
;
188 for (; next
->type
!= GC2235_TOK_TERM
; next
++) {
189 switch (next
->type
& GC2235_TOK_MASK
) {
190 case GC2235_TOK_DELAY
:
191 err
= __gc2235_flush_reg_array(client
, &ctrl
);
198 * If next address is not consecutive, data needs to be
199 * flushed before proceed.
201 if (!__gc2235_write_reg_is_consecutive(client
, &ctrl
,
203 err
= __gc2235_flush_reg_array(client
, &ctrl
);
207 err
= __gc2235_buf_reg_array(client
, &ctrl
, next
);
209 dev_err(&client
->dev
, "%s: write error, aborted\n",
217 return __gc2235_flush_reg_array(client
, &ctrl
);
220 static int gc2235_g_focal(struct v4l2_subdev
*sd
, s32
*val
)
222 *val
= (GC2235_FOCAL_LENGTH_NUM
<< 16) | GC2235_FOCAL_LENGTH_DEM
;
226 static int gc2235_g_fnumber(struct v4l2_subdev
*sd
, s32
*val
)
228 /*const f number for imx*/
229 *val
= (GC2235_F_NUMBER_DEFAULT_NUM
<< 16) | GC2235_F_NUMBER_DEM
;
233 static int gc2235_g_fnumber_range(struct v4l2_subdev
*sd
, s32
*val
)
235 *val
= (GC2235_F_NUMBER_DEFAULT_NUM
<< 24) |
236 (GC2235_F_NUMBER_DEM
<< 16) |
237 (GC2235_F_NUMBER_DEFAULT_NUM
<< 8) | GC2235_F_NUMBER_DEM
;
242 static int gc2235_get_intg_factor(struct i2c_client
*client
,
243 struct camera_mipi_info
*info
,
244 const struct gc2235_resolution
*res
)
246 struct v4l2_subdev
*sd
= i2c_get_clientdata(client
);
247 struct gc2235_device
*dev
= to_gc2235_sensor(sd
);
248 struct atomisp_sensor_mode_data
*buf
= &info
->data
;
249 u16 reg_val
, reg_val_h
, dummy
;
255 /* pixel clock calculattion */
256 buf
->vt_pix_clk_freq_mhz
= dev
->vt_pix_clk_freq_mhz
= 30000000;
258 /* get integration time */
259 buf
->coarse_integration_time_min
= GC2235_COARSE_INTG_TIME_MIN
;
260 buf
->coarse_integration_time_max_margin
=
261 GC2235_COARSE_INTG_TIME_MAX_MARGIN
;
263 buf
->fine_integration_time_min
= GC2235_FINE_INTG_TIME_MIN
;
264 buf
->fine_integration_time_max_margin
=
265 GC2235_FINE_INTG_TIME_MAX_MARGIN
;
267 buf
->fine_integration_time_def
= GC2235_FINE_INTG_TIME_MIN
;
268 buf
->frame_length_lines
= res
->lines_per_frame
;
269 buf
->line_length_pck
= res
->pixels_per_line
;
270 buf
->read_mode
= res
->bin_mode
;
272 /* get the cropping and output resolution to ISP for this mode. */
273 ret
= gc2235_read_reg(client
, GC2235_8BIT
,
274 GC2235_H_CROP_START_H
, ®_val_h
);
275 ret
= gc2235_read_reg(client
, GC2235_8BIT
,
276 GC2235_H_CROP_START_L
, ®_val
);
280 buf
->crop_horizontal_start
= (reg_val_h
<< 8) | reg_val
;
282 ret
= gc2235_read_reg(client
, GC2235_8BIT
,
283 GC2235_V_CROP_START_H
, ®_val_h
);
284 ret
= gc2235_read_reg(client
, GC2235_8BIT
,
285 GC2235_V_CROP_START_L
, ®_val
);
289 buf
->crop_vertical_start
= (reg_val_h
<< 8) | reg_val
;
291 ret
= gc2235_read_reg(client
, GC2235_8BIT
,
292 GC2235_H_OUTSIZE_H
, ®_val_h
);
293 ret
= gc2235_read_reg(client
, GC2235_8BIT
,
294 GC2235_H_OUTSIZE_L
, ®_val
);
297 buf
->output_width
= (reg_val_h
<< 8) | reg_val
;
299 ret
= gc2235_read_reg(client
, GC2235_8BIT
,
300 GC2235_V_OUTSIZE_H
, ®_val_h
);
301 ret
= gc2235_read_reg(client
, GC2235_8BIT
,
302 GC2235_V_OUTSIZE_L
, ®_val
);
305 buf
->output_height
= (reg_val_h
<< 8) | reg_val
;
307 buf
->crop_horizontal_end
= buf
->crop_horizontal_start
+
308 buf
->output_width
- 1;
309 buf
->crop_vertical_end
= buf
->crop_vertical_start
+
310 buf
->output_height
- 1;
312 ret
= gc2235_read_reg(client
, GC2235_8BIT
,
313 GC2235_HB_H
, ®_val_h
);
314 ret
= gc2235_read_reg(client
, GC2235_8BIT
,
315 GC2235_HB_L
, ®_val
);
319 dummy
= (reg_val_h
<< 8) | reg_val
;
321 ret
= gc2235_read_reg(client
, GC2235_8BIT
,
322 GC2235_SH_DELAY_H
, ®_val_h
);
323 ret
= gc2235_read_reg(client
, GC2235_8BIT
,
324 GC2235_SH_DELAY_L
, ®_val
);
327 buf
->line_length_pck
= buf
->output_width
+ 16 + dummy
+
328 (((u16
)reg_val_h
<< 8) | (u16
)reg_val
) + 4;
330 ret
= gc2235_read_reg(client
, GC2235_8BIT
,
331 GC2235_VB_H
, ®_val_h
);
332 ret
= gc2235_read_reg(client
, GC2235_8BIT
,
333 GC2235_VB_L
, ®_val
);
338 buf
->frame_length_lines
= buf
->output_height
+ 32 +
339 (((u16
)reg_val_h
<< 8) | (u16
)reg_val
);
341 buf
->binning_factor_x
= res
->bin_factor_x
?
342 res
->bin_factor_x
: 1;
343 buf
->binning_factor_y
= res
->bin_factor_y
?
344 res
->bin_factor_y
: 1;
348 static long __gc2235_set_exposure(struct v4l2_subdev
*sd
, int coarse_itg
,
349 int gain
, int digitgain
)
352 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
353 u16 coarse_integration
= (u16
)coarse_itg
;
355 u16 expo_coarse_h
, expo_coarse_l
, gain_val
= 0xF0, gain_val2
= 0xF0;
356 expo_coarse_h
= coarse_integration
>> 8;
357 expo_coarse_l
= coarse_integration
& 0xff;
359 ret
= gc2235_write_reg(client
, GC2235_8BIT
,
360 GC2235_EXPOSURE_H
, expo_coarse_h
);
361 ret
= gc2235_write_reg(client
, GC2235_8BIT
,
362 GC2235_EXPOSURE_L
, expo_coarse_l
);
367 } else if (gain
< 256) {
371 gain_val2
= 64 * gain
/ 256;
375 ret
= gc2235_write_reg(client
, GC2235_8BIT
,
376 GC2235_GLOBAL_GAIN
, (u8
)gain_val
);
377 ret
= gc2235_write_reg(client
, GC2235_8BIT
,
378 GC2235_PRE_GAIN
, (u8
)gain_val2
);
384 static int gc2235_set_exposure(struct v4l2_subdev
*sd
, int exposure
,
385 int gain
, int digitgain
)
387 struct gc2235_device
*dev
= to_gc2235_sensor(sd
);
390 mutex_lock(&dev
->input_lock
);
391 ret
= __gc2235_set_exposure(sd
, exposure
, gain
, digitgain
);
392 mutex_unlock(&dev
->input_lock
);
397 static long gc2235_s_exposure(struct v4l2_subdev
*sd
,
398 struct atomisp_exposure
*exposure
)
400 int exp
= exposure
->integration_time
[0];
401 int gain
= exposure
->gain
[0];
402 int digitgain
= exposure
->gain
[1];
404 /* we should not accept the invalid value below. */
406 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
407 v4l2_err(client
, "%s: invalid value\n", __func__
);
411 return gc2235_set_exposure(sd
, exp
, gain
, digitgain
);
413 static long gc2235_ioctl(struct v4l2_subdev
*sd
, unsigned int cmd
, void *arg
)
416 case ATOMISP_IOC_S_EXPOSURE
:
417 return gc2235_s_exposure(sd
, arg
);
423 /* This returns the exposure time being used. This should only be used
424 * for filling in EXIF data, not for actual image processing.
426 static int gc2235_q_exposure(struct v4l2_subdev
*sd
, s32
*value
)
428 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
433 ret
= gc2235_read_reg(client
, GC2235_8BIT
,
439 ret
= gc2235_read_reg(client
, GC2235_8BIT
,
445 reg_v
+= reg_v2
<< 8;
452 static int gc2235_g_volatile_ctrl(struct v4l2_ctrl
*ctrl
)
454 struct gc2235_device
*dev
=
455 container_of(ctrl
->handler
, struct gc2235_device
, ctrl_handler
);
459 case V4L2_CID_EXPOSURE_ABSOLUTE
:
460 ret
= gc2235_q_exposure(&dev
->sd
, &ctrl
->val
);
462 case V4L2_CID_FOCAL_ABSOLUTE
:
463 ret
= gc2235_g_focal(&dev
->sd
, &ctrl
->val
);
465 case V4L2_CID_FNUMBER_ABSOLUTE
:
466 ret
= gc2235_g_fnumber(&dev
->sd
, &ctrl
->val
);
468 case V4L2_CID_FNUMBER_RANGE
:
469 ret
= gc2235_g_fnumber_range(&dev
->sd
, &ctrl
->val
);
478 static const struct v4l2_ctrl_ops ctrl_ops
= {
479 .g_volatile_ctrl
= gc2235_g_volatile_ctrl
482 static struct v4l2_ctrl_config gc2235_controls
[] = {
485 .id
= V4L2_CID_EXPOSURE_ABSOLUTE
,
486 .type
= V4L2_CTRL_TYPE_INTEGER
,
496 .id
= V4L2_CID_FOCAL_ABSOLUTE
,
497 .type
= V4L2_CTRL_TYPE_INTEGER
,
498 .name
= "focal length",
499 .min
= GC2235_FOCAL_LENGTH_DEFAULT
,
500 .max
= GC2235_FOCAL_LENGTH_DEFAULT
,
502 .def
= GC2235_FOCAL_LENGTH_DEFAULT
,
507 .id
= V4L2_CID_FNUMBER_ABSOLUTE
,
508 .type
= V4L2_CTRL_TYPE_INTEGER
,
510 .min
= GC2235_F_NUMBER_DEFAULT
,
511 .max
= GC2235_F_NUMBER_DEFAULT
,
513 .def
= GC2235_F_NUMBER_DEFAULT
,
518 .id
= V4L2_CID_FNUMBER_RANGE
,
519 .type
= V4L2_CTRL_TYPE_INTEGER
,
520 .name
= "f-number range",
521 .min
= GC2235_F_NUMBER_RANGE
,
522 .max
= GC2235_F_NUMBER_RANGE
,
524 .def
= GC2235_F_NUMBER_RANGE
,
529 static int __gc2235_init(struct v4l2_subdev
*sd
)
531 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
533 /* restore settings */
534 gc2235_res
= gc2235_res_preview
;
535 N_RES
= N_RES_PREVIEW
;
537 return gc2235_write_reg_array(client
, gc2235_init_settings
);
542 static int power_ctrl(struct v4l2_subdev
*sd
, bool flag
)
545 struct gc2235_device
*dev
= to_gc2235_sensor(sd
);
547 if (!dev
|| !dev
->platform_data
)
550 /* Non-gmin platforms use the legacy callback */
551 if (dev
->platform_data
->power_ctrl
)
552 return dev
->platform_data
->power_ctrl(sd
, flag
);
555 ret
= dev
->platform_data
->v1p8_ctrl(sd
, 1);
556 usleep_range(60, 90);
558 ret
|= dev
->platform_data
->v2p8_ctrl(sd
, 1);
560 ret
= dev
->platform_data
->v1p8_ctrl(sd
, 0);
561 ret
|= dev
->platform_data
->v2p8_ctrl(sd
, 0);
566 static int gpio_ctrl(struct v4l2_subdev
*sd
, bool flag
)
568 struct gc2235_device
*dev
= to_gc2235_sensor(sd
);
571 if (!dev
|| !dev
->platform_data
)
574 /* Non-gmin platforms use the legacy callback */
575 if (dev
->platform_data
->gpio_ctrl
)
576 return dev
->platform_data
->gpio_ctrl(sd
, flag
);
578 ret
|= dev
->platform_data
->gpio1_ctrl(sd
, !flag
);
579 usleep_range(60, 90);
580 return dev
->platform_data
->gpio0_ctrl(sd
, flag
);
583 static int power_up(struct v4l2_subdev
*sd
)
585 struct gc2235_device
*dev
= to_gc2235_sensor(sd
);
586 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
589 if (!dev
->platform_data
) {
590 dev_err(&client
->dev
,
591 "no camera_sensor_platform_data");
595 ret
= power_ctrl(sd
, 1);
599 /* according to DS, at least 5ms is needed between DOVDD and PWDN */
600 usleep_range(5000, 6000);
602 ret
= dev
->platform_data
->flisclk_ctrl(sd
, 1);
605 usleep_range(5000, 6000);
608 ret
= gpio_ctrl(sd
, 1);
610 ret
= gpio_ctrl(sd
, 1);
622 dev_err(&client
->dev
, "sensor power-up failed\n");
627 static int power_down(struct v4l2_subdev
*sd
)
629 struct gc2235_device
*dev
= to_gc2235_sensor(sd
);
630 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
633 if (!dev
->platform_data
) {
634 dev_err(&client
->dev
,
635 "no camera_sensor_platform_data");
639 ret
= gpio_ctrl(sd
, 0);
641 ret
= gpio_ctrl(sd
, 0);
643 dev_err(&client
->dev
, "gpio failed 2\n");
646 ret
= dev
->platform_data
->flisclk_ctrl(sd
, 0);
648 dev_err(&client
->dev
, "flisclk failed\n");
651 ret
= power_ctrl(sd
, 0);
653 dev_err(&client
->dev
, "vprog failed.\n");
658 static int gc2235_s_power(struct v4l2_subdev
*sd
, int on
)
663 ret
= power_down(sd
);
667 ret
= __gc2235_init(sd
);
674 * distance - calculate the distance
679 * Get the gap between resolution and w/h.
680 * res->width/height smaller than w/h wouldn't be considered.
681 * Returns the value of gap or -1 if fail.
683 #define LARGEST_ALLOWED_RATIO_MISMATCH 800
684 static int distance(struct gc2235_resolution
*res
, u32 w
, u32 h
)
686 unsigned int w_ratio
= (res
->width
<< 13) / w
;
687 unsigned int h_ratio
;
692 h_ratio
= (res
->height
<< 13) / h
;
695 match
= abs(((w_ratio
<< 13) / h_ratio
) - 8192);
697 if ((w_ratio
< 8192) || (h_ratio
< 8192) ||
698 (match
> LARGEST_ALLOWED_RATIO_MISMATCH
))
701 return w_ratio
+ h_ratio
;
704 /* Return the nearest higher resolution index */
705 static int nearest_resolution_index(int w
, int h
)
710 int min_dist
= INT_MAX
;
711 struct gc2235_resolution
*tmp_res
= NULL
;
713 for (i
= 0; i
< N_RES
; i
++) {
714 tmp_res
= &gc2235_res
[i
];
715 dist
= distance(tmp_res
, w
, h
);
718 if (dist
< min_dist
) {
727 static int get_resolution_index(int w
, int h
)
731 for (i
= 0; i
< N_RES
; i
++) {
732 if (w
!= gc2235_res
[i
].width
)
734 if (h
!= gc2235_res
[i
].height
)
743 static int startup(struct v4l2_subdev
*sd
)
745 struct gc2235_device
*dev
= to_gc2235_sensor(sd
);
746 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
749 /* force gc2235 to do a reset in res change, otherwise it
750 * can not output normal after switching res. and it is not
751 * necessary for first time run up after power on, for the sack
756 gc2235_write_reg_array(client
, gc2235_init_settings
);
759 ret
= gc2235_write_reg_array(client
, gc2235_res
[dev
->fmt_idx
].regs
);
761 dev_err(&client
->dev
, "gc2235 write register err.\n");
769 static int gc2235_set_fmt(struct v4l2_subdev
*sd
,
770 struct v4l2_subdev_pad_config
*cfg
,
771 struct v4l2_subdev_format
*format
)
774 struct v4l2_mbus_framefmt
*fmt
= &format
->format
;
775 struct gc2235_device
*dev
= to_gc2235_sensor(sd
);
776 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
777 struct camera_mipi_info
*gc2235_info
= NULL
;
781 gc2235_info
= v4l2_get_subdev_hostdata(sd
);
788 mutex_lock(&dev
->input_lock
);
789 idx
= nearest_resolution_index(fmt
->width
, fmt
->height
);
791 /* return the largest resolution */
792 fmt
->width
= gc2235_res
[N_RES
- 1].width
;
793 fmt
->height
= gc2235_res
[N_RES
- 1].height
;
795 fmt
->width
= gc2235_res
[idx
].width
;
796 fmt
->height
= gc2235_res
[idx
].height
;
798 fmt
->code
= MEDIA_BUS_FMT_SGRBG10_1X10
;
799 if (format
->which
== V4L2_SUBDEV_FORMAT_TRY
) {
801 mutex_unlock(&dev
->input_lock
);
805 dev
->fmt_idx
= get_resolution_index(fmt
->width
, fmt
->height
);
806 if (dev
->fmt_idx
== -1) {
807 dev_err(&client
->dev
, "get resolution fail\n");
808 mutex_unlock(&dev
->input_lock
);
814 dev_err(&client
->dev
, "gc2235 startup err\n");
818 ret
= gc2235_get_intg_factor(client
, gc2235_info
,
819 &gc2235_res
[dev
->fmt_idx
]);
821 dev_err(&client
->dev
, "failed to get integration_factor\n");
824 mutex_unlock(&dev
->input_lock
);
828 static int gc2235_get_fmt(struct v4l2_subdev
*sd
,
829 struct v4l2_subdev_pad_config
*cfg
,
830 struct v4l2_subdev_format
*format
)
832 struct v4l2_mbus_framefmt
*fmt
= &format
->format
;
833 struct gc2235_device
*dev
= to_gc2235_sensor(sd
);
841 fmt
->width
= gc2235_res
[dev
->fmt_idx
].width
;
842 fmt
->height
= gc2235_res
[dev
->fmt_idx
].height
;
843 fmt
->code
= MEDIA_BUS_FMT_SGRBG10_1X10
;
848 static int gc2235_detect(struct i2c_client
*client
)
850 struct i2c_adapter
*adapter
= client
->adapter
;
855 if (!i2c_check_functionality(adapter
, I2C_FUNC_I2C
))
858 ret
= gc2235_read_reg(client
, GC2235_8BIT
,
859 GC2235_SENSOR_ID_H
, &high
);
861 dev_err(&client
->dev
, "sensor_id_high = 0x%x\n", high
);
864 ret
= gc2235_read_reg(client
, GC2235_8BIT
,
865 GC2235_SENSOR_ID_L
, &low
);
866 id
= ((high
<< 8) | low
);
868 if (id
!= GC2235_ID
) {
869 dev_err(&client
->dev
, "sensor ID error, 0x%x\n", id
);
873 dev_info(&client
->dev
, "detect gc2235 success\n");
877 static int gc2235_s_stream(struct v4l2_subdev
*sd
, int enable
)
879 struct gc2235_device
*dev
= to_gc2235_sensor(sd
);
880 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
882 mutex_lock(&dev
->input_lock
);
885 ret
= gc2235_write_reg_array(client
, gc2235_stream_on
);
887 ret
= gc2235_write_reg_array(client
, gc2235_stream_off
);
889 mutex_unlock(&dev
->input_lock
);
894 static int gc2235_s_config(struct v4l2_subdev
*sd
,
895 int irq
, void *platform_data
)
897 struct gc2235_device
*dev
= to_gc2235_sensor(sd
);
898 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
905 (struct camera_sensor_platform_data
*)platform_data
;
907 mutex_lock(&dev
->input_lock
);
908 if (dev
->platform_data
->platform_init
) {
909 ret
= dev
->platform_data
->platform_init(client
);
911 dev_err(&client
->dev
, "platform init err\n");
912 goto platform_init_failed
;
915 /* power off the module, then power on it in future
916 * as first power on by board may not fulfill the
917 * power on sequqence needed by the module
919 ret
= power_down(sd
);
921 dev_err(&client
->dev
, "gc2235 power-off err.\n");
927 dev_err(&client
->dev
, "gc2235 power-up err.\n");
931 ret
= dev
->platform_data
->csi_cfg(sd
, 1);
935 /* config & detect sensor */
936 ret
= gc2235_detect(client
);
938 dev_err(&client
->dev
, "gc2235_detect err s_config.\n");
942 /* turn off sensor, after probed */
943 ret
= power_down(sd
);
945 dev_err(&client
->dev
, "gc2235 power-off err.\n");
948 mutex_unlock(&dev
->input_lock
);
953 dev
->platform_data
->csi_cfg(sd
, 0);
956 dev_err(&client
->dev
, "sensor power-gating failed\n");
958 if (dev
->platform_data
->platform_deinit
)
959 dev
->platform_data
->platform_deinit();
960 platform_init_failed
:
961 mutex_unlock(&dev
->input_lock
);
965 static int gc2235_g_parm(struct v4l2_subdev
*sd
,
966 struct v4l2_streamparm
*param
)
968 struct gc2235_device
*dev
= to_gc2235_sensor(sd
);
969 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
974 if (param
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
) {
975 dev_err(&client
->dev
, "unsupported buffer type.\n");
979 memset(param
, 0, sizeof(*param
));
980 param
->type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
982 if (dev
->fmt_idx
>= 0 && dev
->fmt_idx
< N_RES
) {
983 param
->parm
.capture
.capability
= V4L2_CAP_TIMEPERFRAME
;
984 param
->parm
.capture
.timeperframe
.numerator
= 1;
985 param
->parm
.capture
.capturemode
= dev
->run_mode
;
986 param
->parm
.capture
.timeperframe
.denominator
=
987 gc2235_res
[dev
->fmt_idx
].fps
;
992 static int gc2235_s_parm(struct v4l2_subdev
*sd
,
993 struct v4l2_streamparm
*param
)
995 struct gc2235_device
*dev
= to_gc2235_sensor(sd
);
996 dev
->run_mode
= param
->parm
.capture
.capturemode
;
998 mutex_lock(&dev
->input_lock
);
999 switch (dev
->run_mode
) {
1001 gc2235_res
= gc2235_res_video
;
1002 N_RES
= N_RES_VIDEO
;
1004 case CI_MODE_STILL_CAPTURE
:
1005 gc2235_res
= gc2235_res_still
;
1006 N_RES
= N_RES_STILL
;
1009 gc2235_res
= gc2235_res_preview
;
1010 N_RES
= N_RES_PREVIEW
;
1012 mutex_unlock(&dev
->input_lock
);
1016 static int gc2235_g_frame_interval(struct v4l2_subdev
*sd
,
1017 struct v4l2_subdev_frame_interval
*interval
)
1019 struct gc2235_device
*dev
= to_gc2235_sensor(sd
);
1021 interval
->interval
.numerator
= 1;
1022 interval
->interval
.denominator
= gc2235_res
[dev
->fmt_idx
].fps
;
1027 static int gc2235_enum_mbus_code(struct v4l2_subdev
*sd
,
1028 struct v4l2_subdev_pad_config
*cfg
,
1029 struct v4l2_subdev_mbus_code_enum
*code
)
1031 if (code
->index
>= MAX_FMTS
)
1034 code
->code
= MEDIA_BUS_FMT_SBGGR10_1X10
;
1038 static int gc2235_enum_frame_size(struct v4l2_subdev
*sd
,
1039 struct v4l2_subdev_pad_config
*cfg
,
1040 struct v4l2_subdev_frame_size_enum
*fse
)
1042 int index
= fse
->index
;
1047 fse
->min_width
= gc2235_res
[index
].width
;
1048 fse
->min_height
= gc2235_res
[index
].height
;
1049 fse
->max_width
= gc2235_res
[index
].width
;
1050 fse
->max_height
= gc2235_res
[index
].height
;
1056 static int gc2235_g_skip_frames(struct v4l2_subdev
*sd
, u32
*frames
)
1058 struct gc2235_device
*dev
= to_gc2235_sensor(sd
);
1060 mutex_lock(&dev
->input_lock
);
1061 *frames
= gc2235_res
[dev
->fmt_idx
].skip_frames
;
1062 mutex_unlock(&dev
->input_lock
);
1067 static const struct v4l2_subdev_sensor_ops gc2235_sensor_ops
= {
1068 .g_skip_frames
= gc2235_g_skip_frames
,
1071 static const struct v4l2_subdev_video_ops gc2235_video_ops
= {
1072 .s_stream
= gc2235_s_stream
,
1073 .g_parm
= gc2235_g_parm
,
1074 .s_parm
= gc2235_s_parm
,
1075 .g_frame_interval
= gc2235_g_frame_interval
,
1078 static const struct v4l2_subdev_core_ops gc2235_core_ops
= {
1079 .s_power
= gc2235_s_power
,
1080 .ioctl
= gc2235_ioctl
,
1083 static const struct v4l2_subdev_pad_ops gc2235_pad_ops
= {
1084 .enum_mbus_code
= gc2235_enum_mbus_code
,
1085 .enum_frame_size
= gc2235_enum_frame_size
,
1086 .get_fmt
= gc2235_get_fmt
,
1087 .set_fmt
= gc2235_set_fmt
,
1090 static const struct v4l2_subdev_ops gc2235_ops
= {
1091 .core
= &gc2235_core_ops
,
1092 .video
= &gc2235_video_ops
,
1093 .pad
= &gc2235_pad_ops
,
1094 .sensor
= &gc2235_sensor_ops
,
1097 static int gc2235_remove(struct i2c_client
*client
)
1099 struct v4l2_subdev
*sd
= i2c_get_clientdata(client
);
1100 struct gc2235_device
*dev
= to_gc2235_sensor(sd
);
1101 dev_dbg(&client
->dev
, "gc2235_remove...\n");
1103 if (dev
->platform_data
->platform_deinit
)
1104 dev
->platform_data
->platform_deinit();
1106 dev
->platform_data
->csi_cfg(sd
, 0);
1108 v4l2_device_unregister_subdev(sd
);
1109 media_entity_cleanup(&dev
->sd
.entity
);
1110 v4l2_ctrl_handler_free(&dev
->ctrl_handler
);
1116 static int gc2235_probe(struct i2c_client
*client
)
1118 struct gc2235_device
*dev
;
1123 dev
= kzalloc(sizeof(*dev
), GFP_KERNEL
);
1127 mutex_init(&dev
->input_lock
);
1130 v4l2_i2c_subdev_init(&(dev
->sd
), client
, &gc2235_ops
);
1132 gcpdev
= client
->dev
.platform_data
;
1133 if (ACPI_COMPANION(&client
->dev
))
1134 gcpdev
= gmin_camera_platform_data(&dev
->sd
,
1135 ATOMISP_INPUT_FORMAT_RAW_10
,
1136 atomisp_bayer_order_grbg
);
1138 ret
= gc2235_s_config(&dev
->sd
, client
->irq
, gcpdev
);
1142 dev
->sd
.flags
|= V4L2_SUBDEV_FL_HAS_DEVNODE
;
1143 dev
->pad
.flags
= MEDIA_PAD_FL_SOURCE
;
1144 dev
->format
.code
= MEDIA_BUS_FMT_SBGGR10_1X10
;
1145 dev
->sd
.entity
.function
= MEDIA_ENT_F_CAM_SENSOR
;
1147 v4l2_ctrl_handler_init(&dev
->ctrl_handler
,
1148 ARRAY_SIZE(gc2235_controls
));
1150 gc2235_remove(client
);
1154 for (i
= 0; i
< ARRAY_SIZE(gc2235_controls
); i
++)
1155 v4l2_ctrl_new_custom(&dev
->ctrl_handler
, &gc2235_controls
[i
],
1158 if (dev
->ctrl_handler
.error
) {
1159 gc2235_remove(client
);
1160 return dev
->ctrl_handler
.error
;
1163 /* Use same lock for controls as for everything else. */
1164 dev
->ctrl_handler
.lock
= &dev
->input_lock
;
1165 dev
->sd
.ctrl_handler
= &dev
->ctrl_handler
;
1167 ret
= media_entity_pads_init(&dev
->sd
.entity
, 1, &dev
->pad
);
1169 gc2235_remove(client
);
1171 if (ACPI_HANDLE(&client
->dev
))
1172 ret
= atomisp_register_i2c_module(&dev
->sd
, gcpdev
, RAW_CAMERA
);
1176 v4l2_device_unregister_subdev(&dev
->sd
);
1182 static const struct acpi_device_id gc2235_acpi_match
[] = {
1186 MODULE_DEVICE_TABLE(acpi
, gc2235_acpi_match
);
1188 static struct i2c_driver gc2235_driver
= {
1191 .acpi_match_table
= gc2235_acpi_match
,
1193 .probe_new
= gc2235_probe
,
1194 .remove
= gc2235_remove
,
1196 module_i2c_driver(gc2235_driver
);
1198 MODULE_AUTHOR("Shuguang Gong <Shuguang.Gong@intel.com>");
1199 MODULE_DESCRIPTION("A low-level driver for GC2235 sensors");
1200 MODULE_LICENSE("GPL");