2 * Support for GalaxyCore GC0310 VGA camera sensor.
4 * Copyright (c) 2013 Intel Corporation. All Rights Reserved.
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License version
8 * 2 as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
17 #include <linux/module.h>
18 #include <linux/types.h>
19 #include <linux/kernel.h>
21 #include <linux/string.h>
22 #include <linux/errno.h>
23 #include <linux/init.h>
24 #include <linux/kmod.h>
25 #include <linux/device.h>
26 #include <linux/delay.h>
27 #include <linux/slab.h>
28 #include <linux/i2c.h>
29 #include <linux/moduleparam.h>
30 #include <media/v4l2-device.h>
32 #include "../include/linux/atomisp_gmin_platform.h"
36 /* i2c read/write stuff */
37 static int gc0310_read_reg(struct i2c_client
*client
,
38 u16 data_length
, u8 reg
, u8
*val
)
41 struct i2c_msg msg
[2];
42 unsigned char data
[1];
44 if (!client
->adapter
) {
45 dev_err(&client
->dev
, "%s error, no client->adapter\n",
50 if (data_length
!= GC0310_8BIT
) {
51 dev_err(&client
->dev
, "%s error, invalid data length\n",
56 memset(msg
, 0, sizeof(msg
));
58 msg
[0].addr
= client
->addr
;
60 msg
[0].len
= I2C_MSG_LENGTH
;
63 /* high byte goes out first */
64 data
[0] = (u8
)(reg
& 0xff);
66 msg
[1].addr
= client
->addr
;
67 msg
[1].len
= data_length
;
68 msg
[1].flags
= I2C_M_RD
;
71 err
= i2c_transfer(client
->adapter
, msg
, 2);
76 "read from offset 0x%x error %d", reg
, err
);
81 /* high byte comes first */
82 if (data_length
== GC0310_8BIT
)
88 static int gc0310_i2c_write(struct i2c_client
*client
, u16 len
, u8
*data
)
91 const int num_msg
= 1;
94 msg
.addr
= client
->addr
;
98 ret
= i2c_transfer(client
->adapter
, &msg
, 1);
100 return ret
== num_msg
? 0 : -EIO
;
103 static int gc0310_write_reg(struct i2c_client
*client
, u16 data_length
,
107 unsigned char data
[2] = {0};
108 u8
*wreg
= (u8
*)data
;
109 const u16 len
= data_length
+ sizeof(u8
); /* 8-bit address + data */
111 if (data_length
!= GC0310_8BIT
) {
112 dev_err(&client
->dev
,
113 "%s error, invalid data_length\n", __func__
);
117 /* high byte goes out first */
118 *wreg
= (u8
)(reg
& 0xff);
120 if (data_length
== GC0310_8BIT
)
123 ret
= gc0310_i2c_write(client
, len
, data
);
125 dev_err(&client
->dev
,
126 "write error: wrote 0x%x to offset 0x%x error %d",
133 * gc0310_write_reg_array - Initializes a list of GC0310 registers
134 * @client: i2c driver client structure
135 * @reglist: list of registers to be written
137 * This function initializes a list of registers. When consecutive addresses
138 * are found in a row on the list, this function creates a buffer and sends
139 * consecutive data in a single i2c_transfer().
141 * __gc0310_flush_reg_array, __gc0310_buf_reg_array() and
142 * __gc0310_write_reg_is_consecutive() are internal functions to
143 * gc0310_write_reg_array_fast() and should be not used anywhere else.
147 static int __gc0310_flush_reg_array(struct i2c_client
*client
,
148 struct gc0310_write_ctrl
*ctrl
)
152 if (ctrl
->index
== 0)
155 size
= sizeof(u8
) + ctrl
->index
; /* 8-bit address + data */
156 ctrl
->buffer
.addr
= (u8
)(ctrl
->buffer
.addr
);
159 return gc0310_i2c_write(client
, size
, (u8
*)&ctrl
->buffer
);
162 static int __gc0310_buf_reg_array(struct i2c_client
*client
,
163 struct gc0310_write_ctrl
*ctrl
,
164 const struct gc0310_reg
*next
)
168 switch (next
->type
) {
171 ctrl
->buffer
.data
[ctrl
->index
] = (u8
)next
->val
;
177 /* When first item is added, we need to store its starting address */
178 if (ctrl
->index
== 0)
179 ctrl
->buffer
.addr
= next
->reg
;
184 * Buffer cannot guarantee free space for u32? Better flush it to avoid
185 * possible lack of memory for next item.
187 if (ctrl
->index
+ sizeof(u8
) >= GC0310_MAX_WRITE_BUF_SIZE
)
188 return __gc0310_flush_reg_array(client
, ctrl
);
193 static int __gc0310_write_reg_is_consecutive(struct i2c_client
*client
,
194 struct gc0310_write_ctrl
*ctrl
,
195 const struct gc0310_reg
*next
)
197 if (ctrl
->index
== 0)
200 return ctrl
->buffer
.addr
+ ctrl
->index
== next
->reg
;
203 static int gc0310_write_reg_array(struct i2c_client
*client
,
204 const struct gc0310_reg
*reglist
)
206 const struct gc0310_reg
*next
= reglist
;
207 struct gc0310_write_ctrl ctrl
;
211 for (; next
->type
!= GC0310_TOK_TERM
; next
++) {
212 switch (next
->type
& GC0310_TOK_MASK
) {
213 case GC0310_TOK_DELAY
:
214 err
= __gc0310_flush_reg_array(client
, &ctrl
);
221 * If next address is not consecutive, data needs to be
222 * flushed before proceed.
224 if (!__gc0310_write_reg_is_consecutive(client
, &ctrl
,
226 err
= __gc0310_flush_reg_array(client
, &ctrl
);
230 err
= __gc0310_buf_reg_array(client
, &ctrl
, next
);
232 dev_err(&client
->dev
, "%s: write error, aborted\n",
240 return __gc0310_flush_reg_array(client
, &ctrl
);
242 static int gc0310_g_focal(struct v4l2_subdev
*sd
, s32
*val
)
244 *val
= (GC0310_FOCAL_LENGTH_NUM
<< 16) | GC0310_FOCAL_LENGTH_DEM
;
248 static int gc0310_g_fnumber(struct v4l2_subdev
*sd
, s32
*val
)
250 /*const f number for imx*/
251 *val
= (GC0310_F_NUMBER_DEFAULT_NUM
<< 16) | GC0310_F_NUMBER_DEM
;
255 static int gc0310_g_fnumber_range(struct v4l2_subdev
*sd
, s32
*val
)
257 *val
= (GC0310_F_NUMBER_DEFAULT_NUM
<< 24) |
258 (GC0310_F_NUMBER_DEM
<< 16) |
259 (GC0310_F_NUMBER_DEFAULT_NUM
<< 8) | GC0310_F_NUMBER_DEM
;
263 static int gc0310_g_bin_factor_x(struct v4l2_subdev
*sd
, s32
*val
)
265 struct gc0310_device
*dev
= to_gc0310_sensor(sd
);
267 *val
= gc0310_res
[dev
->fmt_idx
].bin_factor_x
;
272 static int gc0310_g_bin_factor_y(struct v4l2_subdev
*sd
, s32
*val
)
274 struct gc0310_device
*dev
= to_gc0310_sensor(sd
);
276 *val
= gc0310_res
[dev
->fmt_idx
].bin_factor_y
;
281 static int gc0310_get_intg_factor(struct i2c_client
*client
,
282 struct camera_mipi_info
*info
,
283 const struct gc0310_resolution
*res
)
285 struct v4l2_subdev
*sd
= i2c_get_clientdata(client
);
286 struct gc0310_device
*dev
= to_gc0310_sensor(sd
);
287 struct atomisp_sensor_mode_data
*buf
= &info
->data
;
291 unsigned int hori_blanking
;
292 unsigned int vert_blanking
;
293 unsigned int sh_delay
;
298 /* pixel clock calculattion */
299 dev
->vt_pix_clk_freq_mhz
= 14400000; // 16.8MHz
300 buf
->vt_pix_clk_freq_mhz
= dev
->vt_pix_clk_freq_mhz
;
301 pr_info("vt_pix_clk_freq_mhz=%d\n", buf
->vt_pix_clk_freq_mhz
);
303 /* get integration time */
304 buf
->coarse_integration_time_min
= GC0310_COARSE_INTG_TIME_MIN
;
305 buf
->coarse_integration_time_max_margin
=
306 GC0310_COARSE_INTG_TIME_MAX_MARGIN
;
308 buf
->fine_integration_time_min
= GC0310_FINE_INTG_TIME_MIN
;
309 buf
->fine_integration_time_max_margin
=
310 GC0310_FINE_INTG_TIME_MAX_MARGIN
;
312 buf
->fine_integration_time_def
= GC0310_FINE_INTG_TIME_MIN
;
313 buf
->read_mode
= res
->bin_mode
;
315 /* get the cropping and output resolution to ISP for this mode. */
316 /* Getting crop_horizontal_start */
317 ret
= gc0310_read_reg(client
, GC0310_8BIT
,
318 GC0310_H_CROP_START_H
, ®_val
);
321 val
= (reg_val
& 0xFF) << 8;
322 ret
= gc0310_read_reg(client
, GC0310_8BIT
,
323 GC0310_H_CROP_START_L
, ®_val
);
326 buf
->crop_horizontal_start
= val
| (reg_val
& 0xFF);
327 pr_info("crop_horizontal_start=%d\n", buf
->crop_horizontal_start
);
329 /* Getting crop_vertical_start */
330 ret
= gc0310_read_reg(client
, GC0310_8BIT
,
331 GC0310_V_CROP_START_H
, ®_val
);
334 val
= (reg_val
& 0xFF) << 8;
335 ret
= gc0310_read_reg(client
, GC0310_8BIT
,
336 GC0310_V_CROP_START_L
, ®_val
);
339 buf
->crop_vertical_start
= val
| (reg_val
& 0xFF);
340 pr_info("crop_vertical_start=%d\n", buf
->crop_vertical_start
);
342 /* Getting output_width */
343 ret
= gc0310_read_reg(client
, GC0310_8BIT
,
344 GC0310_H_OUTSIZE_H
, ®_val
);
347 val
= (reg_val
& 0xFF) << 8;
348 ret
= gc0310_read_reg(client
, GC0310_8BIT
,
349 GC0310_H_OUTSIZE_L
, ®_val
);
352 buf
->output_width
= val
| (reg_val
& 0xFF);
353 pr_info("output_width=%d\n", buf
->output_width
);
355 /* Getting output_height */
356 ret
= gc0310_read_reg(client
, GC0310_8BIT
,
357 GC0310_V_OUTSIZE_H
, ®_val
);
360 val
= (reg_val
& 0xFF) << 8;
361 ret
= gc0310_read_reg(client
, GC0310_8BIT
,
362 GC0310_V_OUTSIZE_L
, ®_val
);
365 buf
->output_height
= val
| (reg_val
& 0xFF);
366 pr_info("output_height=%d\n", buf
->output_height
);
368 buf
->crop_horizontal_end
= buf
->crop_horizontal_start
+ buf
->output_width
- 1;
369 buf
->crop_vertical_end
= buf
->crop_vertical_start
+ buf
->output_height
- 1;
370 pr_info("crop_horizontal_end=%d\n", buf
->crop_horizontal_end
);
371 pr_info("crop_vertical_end=%d\n", buf
->crop_vertical_end
);
373 /* Getting line_length_pck */
374 ret
= gc0310_read_reg(client
, GC0310_8BIT
,
375 GC0310_H_BLANKING_H
, ®_val
);
378 val
= (reg_val
& 0xFF) << 8;
379 ret
= gc0310_read_reg(client
, GC0310_8BIT
,
380 GC0310_H_BLANKING_L
, ®_val
);
383 hori_blanking
= val
| (reg_val
& 0xFF);
384 ret
= gc0310_read_reg(client
, GC0310_8BIT
,
385 GC0310_SH_DELAY
, ®_val
);
389 buf
->line_length_pck
= buf
->output_width
+ hori_blanking
+ sh_delay
+ 4;
390 pr_info("hori_blanking=%d sh_delay=%d line_length_pck=%d\n", hori_blanking
, sh_delay
, buf
->line_length_pck
);
392 /* Getting frame_length_lines */
393 ret
= gc0310_read_reg(client
, GC0310_8BIT
,
394 GC0310_V_BLANKING_H
, ®_val
);
397 val
= (reg_val
& 0xFF) << 8;
398 ret
= gc0310_read_reg(client
, GC0310_8BIT
,
399 GC0310_V_BLANKING_L
, ®_val
);
402 vert_blanking
= val
| (reg_val
& 0xFF);
403 buf
->frame_length_lines
= buf
->output_height
+ vert_blanking
;
404 pr_info("vert_blanking=%d frame_length_lines=%d\n", vert_blanking
, buf
->frame_length_lines
);
406 buf
->binning_factor_x
= res
->bin_factor_x
?
407 res
->bin_factor_x
: 1;
408 buf
->binning_factor_y
= res
->bin_factor_y
?
409 res
->bin_factor_y
: 1;
413 static int gc0310_set_gain(struct v4l2_subdev
*sd
, int gain
)
416 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
425 if (gain
>= 0x20 && gain
< 0x40) {
426 again
= 0x0; /* sqrt(2) */
429 again
= 0x2; /* 2 * sqrt(2) */
433 pr_info("gain=0x%x again=0x%x dgain=0x%x\n", gain
, again
, dgain
);
435 /* set analog gain */
436 ret
= gc0310_write_reg(client
, GC0310_8BIT
,
437 GC0310_AGC_ADJ
, again
);
441 /* set digital gain */
442 ret
= gc0310_write_reg(client
, GC0310_8BIT
,
443 GC0310_DGC_ADJ
, dgain
);
450 static int __gc0310_set_exposure(struct v4l2_subdev
*sd
, int coarse_itg
,
451 int gain
, int digitgain
)
454 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
457 pr_info("coarse_itg=%d gain=%d digitgain=%d\n", coarse_itg
, gain
, digitgain
);
460 ret
= gc0310_write_reg(client
, GC0310_8BIT
,
461 GC0310_AEC_PK_EXPO_L
,
466 ret
= gc0310_write_reg(client
, GC0310_8BIT
,
467 GC0310_AEC_PK_EXPO_H
,
468 (coarse_itg
>> 8) & 0x0f);
472 ret
= gc0310_set_gain(sd
, gain
);
479 static int gc0310_set_exposure(struct v4l2_subdev
*sd
, int exposure
,
480 int gain
, int digitgain
)
482 struct gc0310_device
*dev
= to_gc0310_sensor(sd
);
485 mutex_lock(&dev
->input_lock
);
486 ret
= __gc0310_set_exposure(sd
, exposure
, gain
, digitgain
);
487 mutex_unlock(&dev
->input_lock
);
492 static long gc0310_s_exposure(struct v4l2_subdev
*sd
,
493 struct atomisp_exposure
*exposure
)
495 int exp
= exposure
->integration_time
[0];
496 int gain
= exposure
->gain
[0];
497 int digitgain
= exposure
->gain
[1];
499 /* we should not accept the invalid value below. */
501 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
502 v4l2_err(client
, "%s: invalid value\n", __func__
);
506 return gc0310_set_exposure(sd
, exp
, gain
, digitgain
);
510 static int gc0310_v_flip(struct v4l2_subdev
*sd
, s32 value
)
516 static int gc0310_h_flip(struct v4l2_subdev
*sd
, s32 value
)
521 static long gc0310_ioctl(struct v4l2_subdev
*sd
, unsigned int cmd
, void *arg
)
525 case ATOMISP_IOC_S_EXPOSURE
:
526 return gc0310_s_exposure(sd
, arg
);
533 /* This returns the exposure time being used. This should only be used
534 * for filling in EXIF data, not for actual image processing.
536 static int gc0310_q_exposure(struct v4l2_subdev
*sd
, s32
*value
)
538 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
543 ret
= gc0310_read_reg(client
, GC0310_8BIT
,
544 GC0310_AEC_PK_EXPO_L
,
550 ret
= gc0310_read_reg(client
, GC0310_8BIT
,
551 GC0310_AEC_PK_EXPO_H
,
556 *value
= *value
+ (reg_v
<< 8);
561 static int gc0310_s_ctrl(struct v4l2_ctrl
*ctrl
)
563 struct gc0310_device
*dev
=
564 container_of(ctrl
->handler
, struct gc0310_device
, ctrl_handler
);
565 struct i2c_client
*client
= v4l2_get_subdevdata(&dev
->sd
);
570 dev_dbg(&client
->dev
, "%s: CID_VFLIP:%d.\n",
571 __func__
, ctrl
->val
);
572 ret
= gc0310_v_flip(&dev
->sd
, ctrl
->val
);
575 dev_dbg(&client
->dev
, "%s: CID_HFLIP:%d.\n",
576 __func__
, ctrl
->val
);
577 ret
= gc0310_h_flip(&dev
->sd
, ctrl
->val
);
585 static int gc0310_g_volatile_ctrl(struct v4l2_ctrl
*ctrl
)
587 struct gc0310_device
*dev
=
588 container_of(ctrl
->handler
, struct gc0310_device
, ctrl_handler
);
592 case V4L2_CID_EXPOSURE_ABSOLUTE
:
593 ret
= gc0310_q_exposure(&dev
->sd
, &ctrl
->val
);
595 case V4L2_CID_FOCAL_ABSOLUTE
:
596 ret
= gc0310_g_focal(&dev
->sd
, &ctrl
->val
);
598 case V4L2_CID_FNUMBER_ABSOLUTE
:
599 ret
= gc0310_g_fnumber(&dev
->sd
, &ctrl
->val
);
601 case V4L2_CID_FNUMBER_RANGE
:
602 ret
= gc0310_g_fnumber_range(&dev
->sd
, &ctrl
->val
);
604 case V4L2_CID_BIN_FACTOR_HORZ
:
605 ret
= gc0310_g_bin_factor_x(&dev
->sd
, &ctrl
->val
);
607 case V4L2_CID_BIN_FACTOR_VERT
:
608 ret
= gc0310_g_bin_factor_y(&dev
->sd
, &ctrl
->val
);
617 static const struct v4l2_ctrl_ops ctrl_ops
= {
618 .s_ctrl
= gc0310_s_ctrl
,
619 .g_volatile_ctrl
= gc0310_g_volatile_ctrl
622 struct v4l2_ctrl_config gc0310_controls
[] = {
625 .id
= V4L2_CID_EXPOSURE_ABSOLUTE
,
626 .type
= V4L2_CTRL_TYPE_INTEGER
,
636 .id
= V4L2_CID_VFLIP
,
637 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
646 .id
= V4L2_CID_HFLIP
,
647 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
656 .id
= V4L2_CID_FOCAL_ABSOLUTE
,
657 .type
= V4L2_CTRL_TYPE_INTEGER
,
658 .name
= "focal length",
659 .min
= GC0310_FOCAL_LENGTH_DEFAULT
,
660 .max
= GC0310_FOCAL_LENGTH_DEFAULT
,
662 .def
= GC0310_FOCAL_LENGTH_DEFAULT
,
667 .id
= V4L2_CID_FNUMBER_ABSOLUTE
,
668 .type
= V4L2_CTRL_TYPE_INTEGER
,
670 .min
= GC0310_F_NUMBER_DEFAULT
,
671 .max
= GC0310_F_NUMBER_DEFAULT
,
673 .def
= GC0310_F_NUMBER_DEFAULT
,
678 .id
= V4L2_CID_FNUMBER_RANGE
,
679 .type
= V4L2_CTRL_TYPE_INTEGER
,
680 .name
= "f-number range",
681 .min
= GC0310_F_NUMBER_RANGE
,
682 .max
= GC0310_F_NUMBER_RANGE
,
684 .def
= GC0310_F_NUMBER_RANGE
,
689 .id
= V4L2_CID_BIN_FACTOR_HORZ
,
690 .type
= V4L2_CTRL_TYPE_INTEGER
,
691 .name
= "horizontal binning factor",
693 .max
= GC0310_BIN_FACTOR_MAX
,
700 .id
= V4L2_CID_BIN_FACTOR_VERT
,
701 .type
= V4L2_CTRL_TYPE_INTEGER
,
702 .name
= "vertical binning factor",
704 .max
= GC0310_BIN_FACTOR_MAX
,
711 static int gc0310_init(struct v4l2_subdev
*sd
)
714 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
715 struct gc0310_device
*dev
= to_gc0310_sensor(sd
);
717 pr_info("%s S\n", __func__
);
718 mutex_lock(&dev
->input_lock
);
720 /* set inital registers */
721 ret
= gc0310_write_reg_array(client
, gc0310_reset_register
);
723 /* restore settings */
724 gc0310_res
= gc0310_res_preview
;
725 N_RES
= N_RES_PREVIEW
;
727 mutex_unlock(&dev
->input_lock
);
729 pr_info("%s E\n", __func__
);
733 static int power_ctrl(struct v4l2_subdev
*sd
, bool flag
)
736 struct gc0310_device
*dev
= to_gc0310_sensor(sd
);
737 if (!dev
|| !dev
->platform_data
)
741 /* The upstream module driver (written to Crystal
742 * Cove) had this logic to pulse the rails low first.
743 * This appears to break things on the MRD7 with the
746 * ret = dev->platform_data->v1p8_ctrl(sd, 0);
747 * ret |= dev->platform_data->v2p8_ctrl(sd, 0);
750 ret
|= dev
->platform_data
->v1p8_ctrl(sd
, 1);
751 ret
|= dev
->platform_data
->v2p8_ctrl(sd
, 1);
752 usleep_range(10000, 15000);
756 ret
|= dev
->platform_data
->v1p8_ctrl(sd
, 0);
757 ret
|= dev
->platform_data
->v2p8_ctrl(sd
, 0);
762 static int gpio_ctrl(struct v4l2_subdev
*sd
, bool flag
)
765 struct gc0310_device
*dev
= to_gc0310_sensor(sd
);
767 if (!dev
|| !dev
->platform_data
)
770 /* GPIO0 == "reset" (active low), GPIO1 == "power down" */
772 /* Pulse reset, then release power down */
773 ret
= dev
->platform_data
->gpio0_ctrl(sd
, 0);
774 usleep_range(5000, 10000);
775 ret
|= dev
->platform_data
->gpio0_ctrl(sd
, 1);
776 usleep_range(10000, 15000);
777 ret
|= dev
->platform_data
->gpio1_ctrl(sd
, 0);
778 usleep_range(10000, 15000);
780 ret
= dev
->platform_data
->gpio1_ctrl(sd
, 1);
781 ret
|= dev
->platform_data
->gpio0_ctrl(sd
, 0);
787 static int power_down(struct v4l2_subdev
*sd
);
789 static int power_up(struct v4l2_subdev
*sd
)
791 struct gc0310_device
*dev
= to_gc0310_sensor(sd
);
792 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
795 pr_info("%s S\n", __func__
);
796 if (!dev
->platform_data
) {
797 dev_err(&client
->dev
,
798 "no camera_sensor_platform_data");
803 ret
= power_ctrl(sd
, 1);
807 /* flis clock control */
808 ret
= dev
->platform_data
->flisclk_ctrl(sd
, 1);
813 ret
= gpio_ctrl(sd
, 1);
815 ret
= gpio_ctrl(sd
, 1);
822 pr_info("%s E\n", __func__
);
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
)
871 return power_down(sd
);
875 return gc0310_init(sd
);
881 * distance - calculate the distance
886 * Get the gap between resolution and w/h.
887 * res->width/height smaller than w/h wouldn't be considered.
888 * Returns the value of gap or -1 if fail.
890 #define LARGEST_ALLOWED_RATIO_MISMATCH 800
891 static int distance(struct gc0310_resolution
*res
, u32 w
, u32 h
)
893 unsigned int w_ratio
= (res
->width
<< 13) / w
;
894 unsigned int h_ratio
;
899 h_ratio
= (res
->height
<< 13) / h
;
902 match
= abs(((w_ratio
<< 13) / h_ratio
) - ((int)8192));
904 if ((w_ratio
< (int)8192) || (h_ratio
< (int)8192) ||
905 (match
> LARGEST_ALLOWED_RATIO_MISMATCH
))
908 return w_ratio
+ h_ratio
;
911 /* Return the nearest higher resolution index */
912 static int nearest_resolution_index(int w
, int h
)
917 int min_dist
= INT_MAX
;
918 struct gc0310_resolution
*tmp_res
= NULL
;
920 for (i
= 0; i
< N_RES
; i
++) {
921 tmp_res
= &gc0310_res
[i
];
922 dist
= distance(tmp_res
, w
, h
);
925 if (dist
< min_dist
) {
934 static int get_resolution_index(int w
, int h
)
938 for (i
= 0; i
< N_RES
; i
++) {
939 if (w
!= gc0310_res
[i
].width
)
941 if (h
!= gc0310_res
[i
].height
)
951 /* TODO: remove it. */
952 static int startup(struct v4l2_subdev
*sd
)
954 struct gc0310_device
*dev
= to_gc0310_sensor(sd
);
955 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
958 pr_info("%s S\n", __func__
);
960 ret
= gc0310_write_reg_array(client
, gc0310_res
[dev
->fmt_idx
].regs
);
962 dev_err(&client
->dev
, "gc0310 write register err.\n");
966 pr_info("%s E\n", __func__
);
970 static int gc0310_set_fmt(struct v4l2_subdev
*sd
,
971 struct v4l2_subdev_pad_config
*cfg
,
972 struct v4l2_subdev_format
*format
)
974 struct v4l2_mbus_framefmt
*fmt
= &format
->format
;
975 struct gc0310_device
*dev
= to_gc0310_sensor(sd
);
976 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
977 struct camera_mipi_info
*gc0310_info
= NULL
;
980 pr_info("%s S\n", __func__
);
988 gc0310_info
= v4l2_get_subdev_hostdata(sd
);
992 mutex_lock(&dev
->input_lock
);
994 idx
= nearest_resolution_index(fmt
->width
, fmt
->height
);
996 /* return the largest resolution */
997 fmt
->width
= gc0310_res
[N_RES
- 1].width
;
998 fmt
->height
= gc0310_res
[N_RES
- 1].height
;
1000 fmt
->width
= gc0310_res
[idx
].width
;
1001 fmt
->height
= gc0310_res
[idx
].height
;
1003 fmt
->code
= MEDIA_BUS_FMT_SGRBG8_1X8
;
1005 if (format
->which
== V4L2_SUBDEV_FORMAT_TRY
) {
1006 cfg
->try_fmt
= *fmt
;
1007 mutex_unlock(&dev
->input_lock
);
1011 dev
->fmt_idx
= get_resolution_index(fmt
->width
, fmt
->height
);
1012 if (dev
->fmt_idx
== -1) {
1013 dev_err(&client
->dev
, "get resolution fail\n");
1014 mutex_unlock(&dev
->input_lock
);
1018 printk("%s: before gc0310_write_reg_array %s\n", __FUNCTION__
,
1019 gc0310_res
[dev
->fmt_idx
].desc
);
1022 dev_err(&client
->dev
, "gc0310 startup err\n");
1026 ret
= gc0310_get_intg_factor(client
, gc0310_info
,
1027 &gc0310_res
[dev
->fmt_idx
]);
1029 dev_err(&client
->dev
, "failed to get integration_factor\n");
1033 pr_info("%s E\n", __func__
);
1035 mutex_unlock(&dev
->input_lock
);
1039 static int gc0310_get_fmt(struct v4l2_subdev
*sd
,
1040 struct v4l2_subdev_pad_config
*cfg
,
1041 struct v4l2_subdev_format
*format
)
1043 struct v4l2_mbus_framefmt
*fmt
= &format
->format
;
1044 struct gc0310_device
*dev
= to_gc0310_sensor(sd
);
1052 fmt
->width
= gc0310_res
[dev
->fmt_idx
].width
;
1053 fmt
->height
= gc0310_res
[dev
->fmt_idx
].height
;
1054 fmt
->code
= MEDIA_BUS_FMT_SGRBG8_1X8
;
1059 static int gc0310_detect(struct i2c_client
*client
)
1061 struct i2c_adapter
*adapter
= client
->adapter
;
1066 pr_info("%s S\n", __func__
);
1067 if (!i2c_check_functionality(adapter
, I2C_FUNC_I2C
))
1070 ret
= gc0310_read_reg(client
, GC0310_8BIT
,
1071 GC0310_SC_CMMN_CHIP_ID_H
, &high
);
1073 dev_err(&client
->dev
, "read sensor_id_high failed\n");
1076 ret
= gc0310_read_reg(client
, GC0310_8BIT
,
1077 GC0310_SC_CMMN_CHIP_ID_L
, &low
);
1079 dev_err(&client
->dev
, "read sensor_id_low failed\n");
1082 id
= ((((u16
) high
) << 8) | (u16
) low
);
1083 pr_info("sensor ID = 0x%x\n", id
);
1085 if (id
!= GC0310_ID
) {
1086 dev_err(&client
->dev
, "sensor ID error, read id = 0x%x, target id = 0x%x\n", id
, GC0310_ID
);
1090 dev_dbg(&client
->dev
, "detect gc0310 success\n");
1092 pr_info("%s E\n", __func__
);
1097 static int gc0310_s_stream(struct v4l2_subdev
*sd
, int enable
)
1099 struct gc0310_device
*dev
= to_gc0310_sensor(sd
);
1100 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
1103 pr_info("%s S enable=%d\n", __func__
, enable
);
1104 mutex_lock(&dev
->input_lock
);
1107 /* enable per frame MIPI and sensor ctrl reset */
1108 ret
= gc0310_write_reg(client
, GC0310_8BIT
,
1111 mutex_unlock(&dev
->input_lock
);
1116 ret
= gc0310_write_reg(client
, GC0310_8BIT
,
1117 GC0310_RESET_RELATED
, GC0310_REGISTER_PAGE_3
);
1119 mutex_unlock(&dev
->input_lock
);
1123 ret
= gc0310_write_reg(client
, GC0310_8BIT
, GC0310_SW_STREAM
,
1124 enable
? GC0310_START_STREAMING
:
1125 GC0310_STOP_STREAMING
);
1127 mutex_unlock(&dev
->input_lock
);
1131 ret
= gc0310_write_reg(client
, GC0310_8BIT
,
1132 GC0310_RESET_RELATED
, GC0310_REGISTER_PAGE_0
);
1134 mutex_unlock(&dev
->input_lock
);
1138 mutex_unlock(&dev
->input_lock
);
1139 pr_info("%s E\n", __func__
);
1144 static int gc0310_s_config(struct v4l2_subdev
*sd
,
1145 int irq
, void *platform_data
)
1147 struct gc0310_device
*dev
= to_gc0310_sensor(sd
);
1148 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
1151 pr_info("%s S\n", __func__
);
1155 dev
->platform_data
=
1156 (struct camera_sensor_platform_data
*)platform_data
;
1158 mutex_lock(&dev
->input_lock
);
1159 /* power off the module, then power on it in future
1160 * as first power on by board may not fulfill the
1161 * power on sequqence needed by the module
1163 ret
= power_down(sd
);
1165 dev_err(&client
->dev
, "gc0310 power-off err.\n");
1166 goto fail_power_off
;
1171 dev_err(&client
->dev
, "gc0310 power-up err.\n");
1175 ret
= dev
->platform_data
->csi_cfg(sd
, 1);
1179 /* config & detect sensor */
1180 ret
= gc0310_detect(client
);
1182 dev_err(&client
->dev
, "gc0310_detect err s_config.\n");
1186 /* turn off sensor, after probed */
1187 ret
= power_down(sd
);
1189 dev_err(&client
->dev
, "gc0310 power-off err.\n");
1192 mutex_unlock(&dev
->input_lock
);
1194 pr_info("%s E\n", __func__
);
1198 dev
->platform_data
->csi_cfg(sd
, 0);
1201 dev_err(&client
->dev
, "sensor power-gating failed\n");
1203 mutex_unlock(&dev
->input_lock
);
1207 static int gc0310_g_parm(struct v4l2_subdev
*sd
,
1208 struct v4l2_streamparm
*param
)
1210 struct gc0310_device
*dev
= to_gc0310_sensor(sd
);
1211 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
1216 if (param
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
) {
1217 dev_err(&client
->dev
, "unsupported buffer type.\n");
1221 memset(param
, 0, sizeof(*param
));
1222 param
->type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
1224 if (dev
->fmt_idx
>= 0 && dev
->fmt_idx
< N_RES
) {
1225 param
->parm
.capture
.capability
= V4L2_CAP_TIMEPERFRAME
;
1226 param
->parm
.capture
.timeperframe
.numerator
= 1;
1227 param
->parm
.capture
.capturemode
= dev
->run_mode
;
1228 param
->parm
.capture
.timeperframe
.denominator
=
1229 gc0310_res
[dev
->fmt_idx
].fps
;
1234 static int gc0310_s_parm(struct v4l2_subdev
*sd
,
1235 struct v4l2_streamparm
*param
)
1237 struct gc0310_device
*dev
= to_gc0310_sensor(sd
);
1238 dev
->run_mode
= param
->parm
.capture
.capturemode
;
1240 mutex_lock(&dev
->input_lock
);
1241 switch (dev
->run_mode
) {
1243 gc0310_res
= gc0310_res_video
;
1244 N_RES
= N_RES_VIDEO
;
1246 case CI_MODE_STILL_CAPTURE
:
1247 gc0310_res
= gc0310_res_still
;
1248 N_RES
= N_RES_STILL
;
1251 gc0310_res
= gc0310_res_preview
;
1252 N_RES
= N_RES_PREVIEW
;
1254 mutex_unlock(&dev
->input_lock
);
1258 static int gc0310_g_frame_interval(struct v4l2_subdev
*sd
,
1259 struct v4l2_subdev_frame_interval
*interval
)
1261 struct gc0310_device
*dev
= to_gc0310_sensor(sd
);
1263 interval
->interval
.numerator
= 1;
1264 interval
->interval
.denominator
= gc0310_res
[dev
->fmt_idx
].fps
;
1269 static int gc0310_enum_mbus_code(struct v4l2_subdev
*sd
,
1270 struct v4l2_subdev_pad_config
*cfg
,
1271 struct v4l2_subdev_mbus_code_enum
*code
)
1273 if (code
->index
>= MAX_FMTS
)
1276 code
->code
= MEDIA_BUS_FMT_SGRBG8_1X8
;
1280 static int gc0310_enum_frame_size(struct v4l2_subdev
*sd
,
1281 struct v4l2_subdev_pad_config
*cfg
,
1282 struct v4l2_subdev_frame_size_enum
*fse
)
1284 int index
= fse
->index
;
1289 fse
->min_width
= gc0310_res
[index
].width
;
1290 fse
->min_height
= gc0310_res
[index
].height
;
1291 fse
->max_width
= gc0310_res
[index
].width
;
1292 fse
->max_height
= gc0310_res
[index
].height
;
1299 static int gc0310_g_skip_frames(struct v4l2_subdev
*sd
, u32
*frames
)
1301 struct gc0310_device
*dev
= to_gc0310_sensor(sd
);
1303 mutex_lock(&dev
->input_lock
);
1304 *frames
= gc0310_res
[dev
->fmt_idx
].skip_frames
;
1305 mutex_unlock(&dev
->input_lock
);
1310 static const struct v4l2_subdev_sensor_ops gc0310_sensor_ops
= {
1311 .g_skip_frames
= gc0310_g_skip_frames
,
1314 static const struct v4l2_subdev_video_ops gc0310_video_ops
= {
1315 .s_stream
= gc0310_s_stream
,
1316 .g_parm
= gc0310_g_parm
,
1317 .s_parm
= gc0310_s_parm
,
1318 .g_frame_interval
= gc0310_g_frame_interval
,
1321 static const struct v4l2_subdev_core_ops gc0310_core_ops
= {
1322 .s_power
= gc0310_s_power
,
1323 .ioctl
= gc0310_ioctl
,
1326 static const struct v4l2_subdev_pad_ops gc0310_pad_ops
= {
1327 .enum_mbus_code
= gc0310_enum_mbus_code
,
1328 .enum_frame_size
= gc0310_enum_frame_size
,
1329 .get_fmt
= gc0310_get_fmt
,
1330 .set_fmt
= gc0310_set_fmt
,
1333 static const struct v4l2_subdev_ops gc0310_ops
= {
1334 .core
= &gc0310_core_ops
,
1335 .video
= &gc0310_video_ops
,
1336 .pad
= &gc0310_pad_ops
,
1337 .sensor
= &gc0310_sensor_ops
,
1340 static int gc0310_remove(struct i2c_client
*client
)
1342 struct v4l2_subdev
*sd
= i2c_get_clientdata(client
);
1343 struct gc0310_device
*dev
= to_gc0310_sensor(sd
);
1344 dev_dbg(&client
->dev
, "gc0310_remove...\n");
1346 dev
->platform_data
->csi_cfg(sd
, 0);
1348 v4l2_device_unregister_subdev(sd
);
1349 media_entity_cleanup(&dev
->sd
.entity
);
1350 v4l2_ctrl_handler_free(&dev
->ctrl_handler
);
1356 static int gc0310_probe(struct i2c_client
*client
)
1358 struct gc0310_device
*dev
;
1363 pr_info("%s S\n", __func__
);
1364 dev
= kzalloc(sizeof(*dev
), GFP_KERNEL
);
1368 mutex_init(&dev
->input_lock
);
1371 v4l2_i2c_subdev_init(&(dev
->sd
), client
, &gc0310_ops
);
1373 if (ACPI_COMPANION(&client
->dev
))
1374 pdata
= gmin_camera_platform_data(&dev
->sd
,
1375 ATOMISP_INPUT_FORMAT_RAW_8
,
1376 atomisp_bayer_order_grbg
);
1378 pdata
= client
->dev
.platform_data
;
1385 ret
= gc0310_s_config(&dev
->sd
, client
->irq
, pdata
);
1389 ret
= atomisp_register_i2c_module(&dev
->sd
, pdata
, RAW_CAMERA
);
1393 dev
->sd
.flags
|= V4L2_SUBDEV_FL_HAS_DEVNODE
;
1394 dev
->pad
.flags
= MEDIA_PAD_FL_SOURCE
;
1395 dev
->format
.code
= MEDIA_BUS_FMT_SGRBG8_1X8
;
1396 dev
->sd
.entity
.function
= MEDIA_ENT_F_CAM_SENSOR
;
1398 v4l2_ctrl_handler_init(&dev
->ctrl_handler
,
1399 ARRAY_SIZE(gc0310_controls
));
1401 gc0310_remove(client
);
1405 for (i
= 0; i
< ARRAY_SIZE(gc0310_controls
); i
++)
1406 v4l2_ctrl_new_custom(&dev
->ctrl_handler
, &gc0310_controls
[i
],
1409 if (dev
->ctrl_handler
.error
) {
1410 gc0310_remove(client
);
1411 return dev
->ctrl_handler
.error
;
1414 /* Use same lock for controls as for everything else. */
1415 dev
->ctrl_handler
.lock
= &dev
->input_lock
;
1416 dev
->sd
.ctrl_handler
= &dev
->ctrl_handler
;
1418 ret
= media_entity_pads_init(&dev
->sd
.entity
, 1, &dev
->pad
);
1420 gc0310_remove(client
);
1422 pr_info("%s E\n", __func__
);
1425 v4l2_device_unregister_subdev(&dev
->sd
);
1430 static const struct acpi_device_id gc0310_acpi_match
[] = {
1435 MODULE_DEVICE_TABLE(acpi
, gc0310_acpi_match
);
1437 static struct i2c_driver gc0310_driver
= {
1440 .acpi_match_table
= gc0310_acpi_match
,
1442 .probe_new
= gc0310_probe
,
1443 .remove
= gc0310_remove
,
1445 module_i2c_driver(gc0310_driver
);
1447 MODULE_AUTHOR("Lai, Angie <angie.lai@intel.com>");
1448 MODULE_DESCRIPTION("A low-level driver for GalaxyCore GC0310 sensors");
1449 MODULE_LICENSE("GPL");