2 * Driver for RJ54N1CB0C CMOS Image Sensor from Sharp
4 * Copyright (C) 2009, Guennadi Liakhovetski <g.liakhovetski@gmx.de>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
11 #include <linux/delay.h>
12 #include <linux/i2c.h>
13 #include <linux/slab.h>
14 #include <linux/v4l2-mediabus.h>
15 #include <linux/videodev2.h>
17 #include <media/rj54n1cb0c.h>
18 #include <media/soc_camera.h>
19 #include <media/v4l2-subdev.h>
20 #include <media/v4l2-chip-ident.h>
21 #include <media/v4l2-ctrls.h>
23 #define RJ54N1_DEV_CODE 0x0400
24 #define RJ54N1_DEV_CODE2 0x0401
25 #define RJ54N1_OUT_SEL 0x0403
26 #define RJ54N1_XY_OUTPUT_SIZE_S_H 0x0404
27 #define RJ54N1_X_OUTPUT_SIZE_S_L 0x0405
28 #define RJ54N1_Y_OUTPUT_SIZE_S_L 0x0406
29 #define RJ54N1_XY_OUTPUT_SIZE_P_H 0x0407
30 #define RJ54N1_X_OUTPUT_SIZE_P_L 0x0408
31 #define RJ54N1_Y_OUTPUT_SIZE_P_L 0x0409
32 #define RJ54N1_LINE_LENGTH_PCK_S_H 0x040a
33 #define RJ54N1_LINE_LENGTH_PCK_S_L 0x040b
34 #define RJ54N1_LINE_LENGTH_PCK_P_H 0x040c
35 #define RJ54N1_LINE_LENGTH_PCK_P_L 0x040d
36 #define RJ54N1_RESIZE_N 0x040e
37 #define RJ54N1_RESIZE_N_STEP 0x040f
38 #define RJ54N1_RESIZE_STEP 0x0410
39 #define RJ54N1_RESIZE_HOLD_H 0x0411
40 #define RJ54N1_RESIZE_HOLD_L 0x0412
41 #define RJ54N1_H_OBEN_OFS 0x0413
42 #define RJ54N1_V_OBEN_OFS 0x0414
43 #define RJ54N1_RESIZE_CONTROL 0x0415
44 #define RJ54N1_STILL_CONTROL 0x0417
45 #define RJ54N1_INC_USE_SEL_H 0x0425
46 #define RJ54N1_INC_USE_SEL_L 0x0426
47 #define RJ54N1_MIRROR_STILL_MODE 0x0427
48 #define RJ54N1_INIT_START 0x0428
49 #define RJ54N1_SCALE_1_2_LEV 0x0429
50 #define RJ54N1_SCALE_4_LEV 0x042a
51 #define RJ54N1_Y_GAIN 0x04d8
52 #define RJ54N1_APT_GAIN_UP 0x04fa
53 #define RJ54N1_RA_SEL_UL 0x0530
54 #define RJ54N1_BYTE_SWAP 0x0531
55 #define RJ54N1_OUT_SIGPO 0x053b
56 #define RJ54N1_WB_SEL_WEIGHT_I 0x054e
57 #define RJ54N1_BIT8_WB 0x0569
58 #define RJ54N1_HCAPS_WB 0x056a
59 #define RJ54N1_VCAPS_WB 0x056b
60 #define RJ54N1_HCAPE_WB 0x056c
61 #define RJ54N1_VCAPE_WB 0x056d
62 #define RJ54N1_EXPOSURE_CONTROL 0x058c
63 #define RJ54N1_FRAME_LENGTH_S_H 0x0595
64 #define RJ54N1_FRAME_LENGTH_S_L 0x0596
65 #define RJ54N1_FRAME_LENGTH_P_H 0x0597
66 #define RJ54N1_FRAME_LENGTH_P_L 0x0598
67 #define RJ54N1_PEAK_H 0x05b7
68 #define RJ54N1_PEAK_50 0x05b8
69 #define RJ54N1_PEAK_60 0x05b9
70 #define RJ54N1_PEAK_DIFF 0x05ba
71 #define RJ54N1_IOC 0x05ef
72 #define RJ54N1_TG_BYPASS 0x0700
73 #define RJ54N1_PLL_L 0x0701
74 #define RJ54N1_PLL_N 0x0702
75 #define RJ54N1_PLL_EN 0x0704
76 #define RJ54N1_RATIO_TG 0x0706
77 #define RJ54N1_RATIO_T 0x0707
78 #define RJ54N1_RATIO_R 0x0708
79 #define RJ54N1_RAMP_TGCLK_EN 0x0709
80 #define RJ54N1_OCLK_DSP 0x0710
81 #define RJ54N1_RATIO_OP 0x0711
82 #define RJ54N1_RATIO_O 0x0712
83 #define RJ54N1_OCLK_SEL_EN 0x0713
84 #define RJ54N1_CLK_RST 0x0717
85 #define RJ54N1_RESET_STANDBY 0x0718
86 #define RJ54N1_FWFLG 0x07fe
88 #define E_EXCLK (1 << 7)
89 #define SOFT_STDBY (1 << 4)
90 #define SEN_RSTX (1 << 2)
91 #define TG_RSTX (1 << 1)
92 #define DSP_RSTX (1 << 0)
94 #define RESIZE_HOLD_SEL (1 << 2)
95 #define RESIZE_GO (1 << 1)
98 * When cropping, the camera automatically centers the cropped region, there
99 * doesn't seem to be a way to specify an explicit location of the rectangle.
101 #define RJ54N1_COLUMN_SKIP 0
102 #define RJ54N1_ROW_SKIP 0
103 #define RJ54N1_MAX_WIDTH 1600
104 #define RJ54N1_MAX_HEIGHT 1200
109 /* I2C addresses: 0x50, 0x51, 0x60, 0x61 */
111 /* RJ54N1CB0C has only one fixed colorspace per pixelcode */
112 struct rj54n1_datafmt
{
113 enum v4l2_mbus_pixelcode code
;
114 enum v4l2_colorspace colorspace
;
117 /* Find a data format by a pixel code in an array */
118 static const struct rj54n1_datafmt
*rj54n1_find_datafmt(
119 enum v4l2_mbus_pixelcode code
, const struct rj54n1_datafmt
*fmt
,
123 for (i
= 0; i
< n
; i
++)
124 if (fmt
[i
].code
== code
)
130 static const struct rj54n1_datafmt rj54n1_colour_fmts
[] = {
131 {V4L2_MBUS_FMT_YUYV8_2X8
, V4L2_COLORSPACE_JPEG
},
132 {V4L2_MBUS_FMT_YVYU8_2X8
, V4L2_COLORSPACE_JPEG
},
133 {V4L2_MBUS_FMT_RGB565_2X8_LE
, V4L2_COLORSPACE_SRGB
},
134 {V4L2_MBUS_FMT_RGB565_2X8_BE
, V4L2_COLORSPACE_SRGB
},
135 {V4L2_MBUS_FMT_SBGGR10_2X8_PADHI_LE
, V4L2_COLORSPACE_SRGB
},
136 {V4L2_MBUS_FMT_SBGGR10_2X8_PADLO_LE
, V4L2_COLORSPACE_SRGB
},
137 {V4L2_MBUS_FMT_SBGGR10_2X8_PADHI_BE
, V4L2_COLORSPACE_SRGB
},
138 {V4L2_MBUS_FMT_SBGGR10_2X8_PADLO_BE
, V4L2_COLORSPACE_SRGB
},
139 {V4L2_MBUS_FMT_SBGGR10_1X10
, V4L2_COLORSPACE_SRGB
},
142 struct rj54n1_clock_div
{
143 u8 ratio_tg
; /* can be 0 or an odd number */
151 struct v4l2_subdev subdev
;
152 struct v4l2_ctrl_handler hdl
;
153 struct rj54n1_clock_div clk_div
;
154 const struct rj54n1_datafmt
*fmt
;
155 struct v4l2_rect rect
; /* Sensor window */
156 unsigned int tgclk_mhz
;
158 unsigned short width
; /* Output window */
159 unsigned short height
;
160 unsigned short resize
; /* Sensor * 1024 / resize = Output */
161 unsigned short scale
;
165 struct rj54n1_reg_val
{
170 static const struct rj54n1_reg_val bank_4
[] = {
191 static const struct rj54n1_reg_val bank_5
[] = {
212 static const struct rj54n1_reg_val bank_7
[] = {
220 static const struct rj54n1_reg_val bank_8
[] = {
408 static const struct rj54n1_reg_val bank_10
[] = {
412 /* Clock dividers - these are default register values, divider = register + 1 */
413 static const struct rj54n1_clock_div clk_div
= {
414 .ratio_tg
= 3 /* default: 5 */,
415 .ratio_t
= 4 /* default: 1 */,
416 .ratio_r
= 4 /* default: 0 */,
417 .ratio_op
= 1 /* default: 5 */,
418 .ratio_o
= 9 /* default: 0 */,
421 static struct rj54n1
*to_rj54n1(const struct i2c_client
*client
)
423 return container_of(i2c_get_clientdata(client
), struct rj54n1
, subdev
);
426 static int reg_read(struct i2c_client
*client
, const u16 reg
)
428 struct rj54n1
*rj54n1
= to_rj54n1(client
);
432 if (rj54n1
->bank
!= reg
>> 8) {
433 dev_dbg(&client
->dev
, "[0x%x] = 0x%x\n", 0xff, reg
>> 8);
434 ret
= i2c_smbus_write_byte_data(client
, 0xff, reg
>> 8);
437 rj54n1
->bank
= reg
>> 8;
439 return i2c_smbus_read_byte_data(client
, reg
& 0xff);
442 static int reg_write(struct i2c_client
*client
, const u16 reg
,
445 struct rj54n1
*rj54n1
= to_rj54n1(client
);
449 if (rj54n1
->bank
!= reg
>> 8) {
450 dev_dbg(&client
->dev
, "[0x%x] = 0x%x\n", 0xff, reg
>> 8);
451 ret
= i2c_smbus_write_byte_data(client
, 0xff, reg
>> 8);
454 rj54n1
->bank
= reg
>> 8;
456 dev_dbg(&client
->dev
, "[0x%x] = 0x%x\n", reg
& 0xff, data
);
457 return i2c_smbus_write_byte_data(client
, reg
& 0xff, data
);
460 static int reg_set(struct i2c_client
*client
, const u16 reg
,
461 const u8 data
, const u8 mask
)
465 ret
= reg_read(client
, reg
);
468 return reg_write(client
, reg
, (ret
& ~mask
) | (data
& mask
));
471 static int reg_write_multiple(struct i2c_client
*client
,
472 const struct rj54n1_reg_val
*rv
, const int n
)
476 for (i
= 0; i
< n
; i
++) {
477 ret
= reg_write(client
, rv
->reg
, rv
->val
);
486 static int rj54n1_enum_fmt(struct v4l2_subdev
*sd
, unsigned int index
,
487 enum v4l2_mbus_pixelcode
*code
)
489 if (index
>= ARRAY_SIZE(rj54n1_colour_fmts
))
492 *code
= rj54n1_colour_fmts
[index
].code
;
496 static int rj54n1_s_stream(struct v4l2_subdev
*sd
, int enable
)
498 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
500 /* Switch between preview and still shot modes */
501 return reg_set(client
, RJ54N1_STILL_CONTROL
, (!enable
) << 7, 0x80);
504 static int rj54n1_set_rect(struct i2c_client
*client
,
505 u16 reg_x
, u16 reg_y
, u16 reg_xy
,
506 u32 width
, u32 height
)
510 ret
= reg_write(client
, reg_xy
,
511 ((width
>> 4) & 0x70) |
512 ((height
>> 8) & 7));
515 ret
= reg_write(client
, reg_x
, width
& 0xff);
517 ret
= reg_write(client
, reg_y
, height
& 0xff);
523 * Some commands, specifically certain initialisation sequences, require
524 * a commit operation.
526 static int rj54n1_commit(struct i2c_client
*client
)
528 int ret
= reg_write(client
, RJ54N1_INIT_START
, 1);
531 ret
= reg_write(client
, RJ54N1_INIT_START
, 0);
535 static int rj54n1_sensor_scale(struct v4l2_subdev
*sd
, s32
*in_w
, s32
*in_h
,
536 s32
*out_w
, s32
*out_h
);
538 static int rj54n1_s_crop(struct v4l2_subdev
*sd
, struct v4l2_crop
*a
)
540 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
541 struct rj54n1
*rj54n1
= to_rj54n1(client
);
542 struct v4l2_rect
*rect
= &a
->c
;
543 int dummy
= 0, output_w
, output_h
,
544 input_w
= rect
->width
, input_h
= rect
->height
;
547 /* arbitrary minimum width and height, edges unimportant */
548 soc_camera_limit_side(&dummy
, &input_w
,
549 RJ54N1_COLUMN_SKIP
, 8, RJ54N1_MAX_WIDTH
);
551 soc_camera_limit_side(&dummy
, &input_h
,
552 RJ54N1_ROW_SKIP
, 8, RJ54N1_MAX_HEIGHT
);
554 output_w
= (input_w
* 1024 + rj54n1
->resize
/ 2) / rj54n1
->resize
;
555 output_h
= (input_h
* 1024 + rj54n1
->resize
/ 2) / rj54n1
->resize
;
557 dev_dbg(&client
->dev
, "Scaling for %dx%d : %u = %dx%d\n",
558 input_w
, input_h
, rj54n1
->resize
, output_w
, output_h
);
560 ret
= rj54n1_sensor_scale(sd
, &input_w
, &input_h
, &output_w
, &output_h
);
564 rj54n1
->width
= output_w
;
565 rj54n1
->height
= output_h
;
566 rj54n1
->resize
= ret
;
567 rj54n1
->rect
.width
= input_w
;
568 rj54n1
->rect
.height
= input_h
;
573 static int rj54n1_g_crop(struct v4l2_subdev
*sd
, struct v4l2_crop
*a
)
575 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
576 struct rj54n1
*rj54n1
= to_rj54n1(client
);
579 a
->type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
584 static int rj54n1_cropcap(struct v4l2_subdev
*sd
, struct v4l2_cropcap
*a
)
586 a
->bounds
.left
= RJ54N1_COLUMN_SKIP
;
587 a
->bounds
.top
= RJ54N1_ROW_SKIP
;
588 a
->bounds
.width
= RJ54N1_MAX_WIDTH
;
589 a
->bounds
.height
= RJ54N1_MAX_HEIGHT
;
590 a
->defrect
= a
->bounds
;
591 a
->type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
592 a
->pixelaspect
.numerator
= 1;
593 a
->pixelaspect
.denominator
= 1;
598 static int rj54n1_g_fmt(struct v4l2_subdev
*sd
,
599 struct v4l2_mbus_framefmt
*mf
)
601 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
602 struct rj54n1
*rj54n1
= to_rj54n1(client
);
604 mf
->code
= rj54n1
->fmt
->code
;
605 mf
->colorspace
= rj54n1
->fmt
->colorspace
;
606 mf
->field
= V4L2_FIELD_NONE
;
607 mf
->width
= rj54n1
->width
;
608 mf
->height
= rj54n1
->height
;
614 * The actual geometry configuration routine. It scales the input window into
615 * the output one, updates the window sizes and returns an error or the resize
616 * coefficient on success. Note: we only use the "Fixed Scaling" on this camera.
618 static int rj54n1_sensor_scale(struct v4l2_subdev
*sd
, s32
*in_w
, s32
*in_h
,
619 s32
*out_w
, s32
*out_h
)
621 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
622 struct rj54n1
*rj54n1
= to_rj54n1(client
);
623 unsigned int skip
, resize
, input_w
= *in_w
, input_h
= *in_h
,
624 output_w
= *out_w
, output_h
= *out_h
;
625 u16 inc_sel
, wb_bit8
, wb_left
, wb_right
, wb_top
, wb_bottom
;
626 unsigned int peak
, peak_50
, peak_60
;
630 * We have a problem with crops, where the window is larger than 512x384
631 * and output window is larger than a half of the input one. In this
632 * case we have to either reduce the input window to equal or below
633 * 512x384 or the output window to equal or below 1/2 of the input.
635 if (output_w
> max(512U, input_w
/ 2)) {
636 if (2 * output_w
> RJ54N1_MAX_WIDTH
) {
637 input_w
= RJ54N1_MAX_WIDTH
;
638 output_w
= RJ54N1_MAX_WIDTH
/ 2;
640 input_w
= output_w
* 2;
643 dev_dbg(&client
->dev
, "Adjusted output width: in %u, out %u\n",
647 if (output_h
> max(384U, input_h
/ 2)) {
648 if (2 * output_h
> RJ54N1_MAX_HEIGHT
) {
649 input_h
= RJ54N1_MAX_HEIGHT
;
650 output_h
= RJ54N1_MAX_HEIGHT
/ 2;
652 input_h
= output_h
* 2;
655 dev_dbg(&client
->dev
, "Adjusted output height: in %u, out %u\n",
659 /* Idea: use the read mode for snapshots, handle separate geometries */
660 ret
= rj54n1_set_rect(client
, RJ54N1_X_OUTPUT_SIZE_S_L
,
661 RJ54N1_Y_OUTPUT_SIZE_S_L
,
662 RJ54N1_XY_OUTPUT_SIZE_S_H
, output_w
, output_h
);
664 ret
= rj54n1_set_rect(client
, RJ54N1_X_OUTPUT_SIZE_P_L
,
665 RJ54N1_Y_OUTPUT_SIZE_P_L
,
666 RJ54N1_XY_OUTPUT_SIZE_P_H
, output_w
, output_h
);
671 if (output_w
> input_w
&& output_h
> input_h
) {
677 unsigned int resize_x
, resize_y
;
678 resize_x
= (input_w
* 1024 + output_w
/ 2) / output_w
;
679 resize_y
= (input_h
* 1024 + output_h
/ 2) / output_h
;
681 /* We want max(resize_x, resize_y), check if it still fits */
682 if (resize_x
> resize_y
&&
683 (output_h
* resize_x
+ 512) / 1024 > RJ54N1_MAX_HEIGHT
)
684 resize
= (RJ54N1_MAX_HEIGHT
* 1024 + output_h
/ 2) /
686 else if (resize_y
> resize_x
&&
687 (output_w
* resize_y
+ 512) / 1024 > RJ54N1_MAX_WIDTH
)
688 resize
= (RJ54N1_MAX_WIDTH
* 1024 + output_w
/ 2) /
691 resize
= max(resize_x
, resize_y
);
693 /* Prohibited value ranges */
704 case 16320 ... 16384:
710 ret
= reg_write(client
, RJ54N1_RESIZE_HOLD_L
, resize
& 0xff);
712 ret
= reg_write(client
, RJ54N1_RESIZE_HOLD_H
, resize
>> 8);
718 * Configure a skipping bitmask. The sensor will select a skipping value
719 * among set bits automatically. This is very unclear in the datasheet
720 * too. I was told, in this register one enables all skipping values,
721 * that are required for a specific resize, and the camera selects
722 * automatically, which ones to use. But it is unclear how to identify,
723 * which cropping values are needed. Secondly, why don't we just set all
724 * bits and let the camera choose? Would it increase processing time and
725 * reduce the framerate? Using 0xfffc for INC_USE_SEL doesn't seem to
726 * improve the image quality or stability for larger frames (see comment
727 * above), but I didn't check the framerate.
729 skip
= min(resize
/ 1024, 15U);
735 else if (resize
& 1023 && skip
< 15)
736 inc_sel
|= 1 << (skip
+ 1);
738 ret
= reg_write(client
, RJ54N1_INC_USE_SEL_L
, inc_sel
& 0xfc);
740 ret
= reg_write(client
, RJ54N1_INC_USE_SEL_H
, inc_sel
>> 8);
742 if (!rj54n1
->auto_wb
) {
743 /* Auto white balance window */
744 wb_left
= output_w
/ 16;
745 wb_right
= (3 * output_w
/ 4 - 3) / 4;
746 wb_top
= output_h
/ 16;
747 wb_bottom
= (3 * output_h
/ 4 - 3) / 4;
748 wb_bit8
= ((wb_left
>> 2) & 0x40) | ((wb_top
>> 4) & 0x10) |
749 ((wb_right
>> 6) & 4) | ((wb_bottom
>> 8) & 1);
752 ret
= reg_write(client
, RJ54N1_BIT8_WB
, wb_bit8
);
754 ret
= reg_write(client
, RJ54N1_HCAPS_WB
, wb_left
);
756 ret
= reg_write(client
, RJ54N1_VCAPS_WB
, wb_top
);
758 ret
= reg_write(client
, RJ54N1_HCAPE_WB
, wb_right
);
760 ret
= reg_write(client
, RJ54N1_VCAPE_WB
, wb_bottom
);
764 peak
= 12 * RJ54N1_MAX_WIDTH
* (1 << 14) * resize
/ rj54n1
->tgclk_mhz
/
770 ret
= reg_write(client
, RJ54N1_PEAK_H
,
771 ((peak_50
>> 4) & 0xf0) | (peak_60
>> 8));
773 ret
= reg_write(client
, RJ54N1_PEAK_50
, peak_50
);
775 ret
= reg_write(client
, RJ54N1_PEAK_60
, peak_60
);
777 ret
= reg_write(client
, RJ54N1_PEAK_DIFF
, peak
/ 150);
781 ret
= reg_write(client
, RJ54N1_RESIZE_CONTROL
,
782 RESIZE_HOLD_SEL
| RESIZE_GO
| 1);
787 /* Constant taken from manufacturer's example */
790 ret
= reg_write(client
, RJ54N1_RESIZE_CONTROL
, RESIZE_HOLD_SEL
| 1);
794 *in_w
= (output_w
* resize
+ 512) / 1024;
795 *in_h
= (output_h
* resize
+ 512) / 1024;
799 dev_dbg(&client
->dev
, "Scaled for %dx%d : %u = %ux%u, skip %u\n",
800 *in_w
, *in_h
, resize
, output_w
, output_h
, skip
);
805 static int rj54n1_set_clock(struct i2c_client
*client
)
807 struct rj54n1
*rj54n1
= to_rj54n1(client
);
810 /* Enable external clock */
811 ret
= reg_write(client
, RJ54N1_RESET_STANDBY
, E_EXCLK
| SOFT_STDBY
);
812 /* Leave stand-by. Note: use this when implementing suspend / resume */
814 ret
= reg_write(client
, RJ54N1_RESET_STANDBY
, E_EXCLK
);
817 ret
= reg_write(client
, RJ54N1_PLL_L
, PLL_L
);
819 ret
= reg_write(client
, RJ54N1_PLL_N
, PLL_N
);
823 ret
= reg_write(client
, RJ54N1_RATIO_TG
,
824 rj54n1
->clk_div
.ratio_tg
);
826 ret
= reg_write(client
, RJ54N1_RATIO_T
,
827 rj54n1
->clk_div
.ratio_t
);
829 ret
= reg_write(client
, RJ54N1_RATIO_R
,
830 rj54n1
->clk_div
.ratio_r
);
832 /* Enable TGCLK & RAMP */
834 ret
= reg_write(client
, RJ54N1_RAMP_TGCLK_EN
, 3);
836 /* Disable clock output */
838 ret
= reg_write(client
, RJ54N1_OCLK_DSP
, 0);
842 ret
= reg_write(client
, RJ54N1_RATIO_OP
,
843 rj54n1
->clk_div
.ratio_op
);
845 ret
= reg_write(client
, RJ54N1_RATIO_O
,
846 rj54n1
->clk_div
.ratio_o
);
850 ret
= reg_write(client
, RJ54N1_OCLK_SEL_EN
, 1);
852 /* Use PLL for Timing Generator, write 2 to reserved bits */
854 ret
= reg_write(client
, RJ54N1_TG_BYPASS
, 2);
856 /* Take sensor out of reset */
858 ret
= reg_write(client
, RJ54N1_RESET_STANDBY
,
862 ret
= reg_write(client
, RJ54N1_PLL_EN
, 1);
864 /* Wait for PLL to stabilise */
867 /* Enable clock to frequency divider */
869 ret
= reg_write(client
, RJ54N1_CLK_RST
, 1);
872 ret
= reg_read(client
, RJ54N1_CLK_RST
);
874 dev_err(&client
->dev
,
875 "Resetting RJ54N1CB0C clock failed: %d!\n", ret
);
880 ret
= reg_set(client
, RJ54N1_OCLK_DSP
, 1, 1);
884 ret
= reg_write(client
, RJ54N1_OCLK_SEL_EN
, 1);
889 static int rj54n1_reg_init(struct i2c_client
*client
)
891 struct rj54n1
*rj54n1
= to_rj54n1(client
);
892 int ret
= rj54n1_set_clock(client
);
895 ret
= reg_write_multiple(client
, bank_7
, ARRAY_SIZE(bank_7
));
897 ret
= reg_write_multiple(client
, bank_10
, ARRAY_SIZE(bank_10
));
899 /* Set binning divisors */
901 ret
= reg_write(client
, RJ54N1_SCALE_1_2_LEV
, 3 | (7 << 4));
903 ret
= reg_write(client
, RJ54N1_SCALE_4_LEV
, 0xf);
905 /* Switch to fixed resize mode */
907 ret
= reg_write(client
, RJ54N1_RESIZE_CONTROL
,
908 RESIZE_HOLD_SEL
| 1);
912 ret
= reg_write(client
, RJ54N1_Y_GAIN
, 0x84);
915 * Mirror the image back: default is upside down and left-to-right...
916 * Set manual preview / still shot switching
919 ret
= reg_write(client
, RJ54N1_MIRROR_STILL_MODE
, 0x27);
922 ret
= reg_write_multiple(client
, bank_4
, ARRAY_SIZE(bank_4
));
924 /* Auto exposure area */
926 ret
= reg_write(client
, RJ54N1_EXPOSURE_CONTROL
, 0x80);
927 /* Check current auto WB config */
929 ret
= reg_read(client
, RJ54N1_WB_SEL_WEIGHT_I
);
931 rj54n1
->auto_wb
= ret
& 0x80;
932 ret
= reg_write_multiple(client
, bank_5
, ARRAY_SIZE(bank_5
));
935 ret
= reg_write_multiple(client
, bank_8
, ARRAY_SIZE(bank_8
));
938 ret
= reg_write(client
, RJ54N1_RESET_STANDBY
,
939 E_EXCLK
| DSP_RSTX
| SEN_RSTX
);
943 ret
= rj54n1_commit(client
);
945 /* Take DSP, TG, sensor out of reset */
947 ret
= reg_write(client
, RJ54N1_RESET_STANDBY
,
948 E_EXCLK
| DSP_RSTX
| TG_RSTX
| SEN_RSTX
);
950 /* Start register update? Same register as 0x?FE in many bank_* sets */
952 ret
= reg_write(client
, RJ54N1_FWFLG
, 2);
954 /* Constant taken from manufacturer's example */
960 static int rj54n1_try_fmt(struct v4l2_subdev
*sd
,
961 struct v4l2_mbus_framefmt
*mf
)
963 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
964 struct rj54n1
*rj54n1
= to_rj54n1(client
);
965 const struct rj54n1_datafmt
*fmt
;
966 int align
= mf
->code
== V4L2_MBUS_FMT_SBGGR10_1X10
||
967 mf
->code
== V4L2_MBUS_FMT_SBGGR10_2X8_PADHI_BE
||
968 mf
->code
== V4L2_MBUS_FMT_SBGGR10_2X8_PADLO_BE
||
969 mf
->code
== V4L2_MBUS_FMT_SBGGR10_2X8_PADHI_LE
||
970 mf
->code
== V4L2_MBUS_FMT_SBGGR10_2X8_PADLO_LE
;
972 dev_dbg(&client
->dev
, "%s: code = %d, width = %u, height = %u\n",
973 __func__
, mf
->code
, mf
->width
, mf
->height
);
975 fmt
= rj54n1_find_datafmt(mf
->code
, rj54n1_colour_fmts
,
976 ARRAY_SIZE(rj54n1_colour_fmts
));
979 mf
->code
= fmt
->code
;
982 mf
->field
= V4L2_FIELD_NONE
;
983 mf
->colorspace
= fmt
->colorspace
;
985 v4l_bound_align_image(&mf
->width
, 112, RJ54N1_MAX_WIDTH
, align
,
986 &mf
->height
, 84, RJ54N1_MAX_HEIGHT
, align
, 0);
991 static int rj54n1_s_fmt(struct v4l2_subdev
*sd
,
992 struct v4l2_mbus_framefmt
*mf
)
994 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
995 struct rj54n1
*rj54n1
= to_rj54n1(client
);
996 const struct rj54n1_datafmt
*fmt
;
997 int output_w
, output_h
, max_w
, max_h
,
998 input_w
= rj54n1
->rect
.width
, input_h
= rj54n1
->rect
.height
;
1002 * The host driver can call us without .try_fmt(), so, we have to take
1005 rj54n1_try_fmt(sd
, mf
);
1008 * Verify if the sensor has just been powered on. TODO: replace this
1009 * with proper PM, when a suitable API is available.
1011 ret
= reg_read(client
, RJ54N1_RESET_STANDBY
);
1015 if (!(ret
& E_EXCLK
)) {
1016 ret
= rj54n1_reg_init(client
);
1021 dev_dbg(&client
->dev
, "%s: code = %d, width = %u, height = %u\n",
1022 __func__
, mf
->code
, mf
->width
, mf
->height
);
1024 /* RA_SEL_UL is only relevant for raw modes, ignored otherwise. */
1026 case V4L2_MBUS_FMT_YUYV8_2X8
:
1027 ret
= reg_write(client
, RJ54N1_OUT_SEL
, 0);
1029 ret
= reg_set(client
, RJ54N1_BYTE_SWAP
, 8, 8);
1031 case V4L2_MBUS_FMT_YVYU8_2X8
:
1032 ret
= reg_write(client
, RJ54N1_OUT_SEL
, 0);
1034 ret
= reg_set(client
, RJ54N1_BYTE_SWAP
, 0, 8);
1036 case V4L2_MBUS_FMT_RGB565_2X8_LE
:
1037 ret
= reg_write(client
, RJ54N1_OUT_SEL
, 0x11);
1039 ret
= reg_set(client
, RJ54N1_BYTE_SWAP
, 8, 8);
1041 case V4L2_MBUS_FMT_RGB565_2X8_BE
:
1042 ret
= reg_write(client
, RJ54N1_OUT_SEL
, 0x11);
1044 ret
= reg_set(client
, RJ54N1_BYTE_SWAP
, 0, 8);
1046 case V4L2_MBUS_FMT_SBGGR10_2X8_PADLO_LE
:
1047 ret
= reg_write(client
, RJ54N1_OUT_SEL
, 4);
1049 ret
= reg_set(client
, RJ54N1_BYTE_SWAP
, 8, 8);
1051 ret
= reg_write(client
, RJ54N1_RA_SEL_UL
, 0);
1053 case V4L2_MBUS_FMT_SBGGR10_2X8_PADHI_LE
:
1054 ret
= reg_write(client
, RJ54N1_OUT_SEL
, 4);
1056 ret
= reg_set(client
, RJ54N1_BYTE_SWAP
, 8, 8);
1058 ret
= reg_write(client
, RJ54N1_RA_SEL_UL
, 8);
1060 case V4L2_MBUS_FMT_SBGGR10_2X8_PADLO_BE
:
1061 ret
= reg_write(client
, RJ54N1_OUT_SEL
, 4);
1063 ret
= reg_set(client
, RJ54N1_BYTE_SWAP
, 0, 8);
1065 ret
= reg_write(client
, RJ54N1_RA_SEL_UL
, 0);
1067 case V4L2_MBUS_FMT_SBGGR10_2X8_PADHI_BE
:
1068 ret
= reg_write(client
, RJ54N1_OUT_SEL
, 4);
1070 ret
= reg_set(client
, RJ54N1_BYTE_SWAP
, 0, 8);
1072 ret
= reg_write(client
, RJ54N1_RA_SEL_UL
, 8);
1074 case V4L2_MBUS_FMT_SBGGR10_1X10
:
1075 ret
= reg_write(client
, RJ54N1_OUT_SEL
, 5);
1081 /* Special case: a raw mode with 10 bits of data per clock tick */
1083 ret
= reg_set(client
, RJ54N1_OCLK_SEL_EN
,
1084 (mf
->code
== V4L2_MBUS_FMT_SBGGR10_1X10
) << 1, 2);
1089 /* Supported scales 1:1 >= scale > 1:16 */
1090 max_w
= mf
->width
* (16 * 1024 - 1) / 1024;
1091 if (input_w
> max_w
)
1093 max_h
= mf
->height
* (16 * 1024 - 1) / 1024;
1094 if (input_h
> max_h
)
1097 output_w
= mf
->width
;
1098 output_h
= mf
->height
;
1100 ret
= rj54n1_sensor_scale(sd
, &input_w
, &input_h
, &output_w
, &output_h
);
1104 fmt
= rj54n1_find_datafmt(mf
->code
, rj54n1_colour_fmts
,
1105 ARRAY_SIZE(rj54n1_colour_fmts
));
1108 rj54n1
->resize
= ret
;
1109 rj54n1
->rect
.width
= input_w
;
1110 rj54n1
->rect
.height
= input_h
;
1111 rj54n1
->width
= output_w
;
1112 rj54n1
->height
= output_h
;
1114 mf
->width
= output_w
;
1115 mf
->height
= output_h
;
1116 mf
->field
= V4L2_FIELD_NONE
;
1117 mf
->colorspace
= fmt
->colorspace
;
1122 static int rj54n1_g_chip_ident(struct v4l2_subdev
*sd
,
1123 struct v4l2_dbg_chip_ident
*id
)
1125 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
1127 if (id
->match
.type
!= V4L2_CHIP_MATCH_I2C_ADDR
)
1130 if (id
->match
.addr
!= client
->addr
)
1133 id
->ident
= V4L2_IDENT_RJ54N1CB0C
;
1139 #ifdef CONFIG_VIDEO_ADV_DEBUG
1140 static int rj54n1_g_register(struct v4l2_subdev
*sd
,
1141 struct v4l2_dbg_register
*reg
)
1143 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
1145 if (reg
->match
.type
!= V4L2_CHIP_MATCH_I2C_ADDR
||
1146 reg
->reg
< 0x400 || reg
->reg
> 0x1fff)
1147 /* Registers > 0x0800 are only available from Sharp support */
1150 if (reg
->match
.addr
!= client
->addr
)
1154 reg
->val
= reg_read(client
, reg
->reg
);
1156 if (reg
->val
> 0xff)
1162 static int rj54n1_s_register(struct v4l2_subdev
*sd
,
1163 struct v4l2_dbg_register
*reg
)
1165 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
1167 if (reg
->match
.type
!= V4L2_CHIP_MATCH_I2C_ADDR
||
1168 reg
->reg
< 0x400 || reg
->reg
> 0x1fff)
1169 /* Registers >= 0x0800 are only available from Sharp support */
1172 if (reg
->match
.addr
!= client
->addr
)
1175 if (reg_write(client
, reg
->reg
, reg
->val
) < 0)
1182 static int rj54n1_s_ctrl(struct v4l2_ctrl
*ctrl
)
1184 struct rj54n1
*rj54n1
= container_of(ctrl
->handler
, struct rj54n1
, hdl
);
1185 struct v4l2_subdev
*sd
= &rj54n1
->subdev
;
1186 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
1190 case V4L2_CID_VFLIP
:
1192 data
= reg_set(client
, RJ54N1_MIRROR_STILL_MODE
, 0, 1);
1194 data
= reg_set(client
, RJ54N1_MIRROR_STILL_MODE
, 1, 1);
1198 case V4L2_CID_HFLIP
:
1200 data
= reg_set(client
, RJ54N1_MIRROR_STILL_MODE
, 0, 2);
1202 data
= reg_set(client
, RJ54N1_MIRROR_STILL_MODE
, 2, 2);
1207 if (reg_write(client
, RJ54N1_Y_GAIN
, ctrl
->val
* 2) < 0)
1210 case V4L2_CID_AUTO_WHITE_BALANCE
:
1211 /* Auto WB area - whole image */
1212 if (reg_set(client
, RJ54N1_WB_SEL_WEIGHT_I
, ctrl
->val
<< 7,
1215 rj54n1
->auto_wb
= ctrl
->val
;
1222 static const struct v4l2_ctrl_ops rj54n1_ctrl_ops
= {
1223 .s_ctrl
= rj54n1_s_ctrl
,
1226 static struct v4l2_subdev_core_ops rj54n1_subdev_core_ops
= {
1227 .g_chip_ident
= rj54n1_g_chip_ident
,
1228 #ifdef CONFIG_VIDEO_ADV_DEBUG
1229 .g_register
= rj54n1_g_register
,
1230 .s_register
= rj54n1_s_register
,
1234 static int rj54n1_g_mbus_config(struct v4l2_subdev
*sd
,
1235 struct v4l2_mbus_config
*cfg
)
1237 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
1238 struct soc_camera_link
*icl
= soc_camera_i2c_to_link(client
);
1241 V4L2_MBUS_PCLK_SAMPLE_RISING
| V4L2_MBUS_PCLK_SAMPLE_FALLING
|
1242 V4L2_MBUS_MASTER
| V4L2_MBUS_DATA_ACTIVE_HIGH
|
1243 V4L2_MBUS_HSYNC_ACTIVE_HIGH
| V4L2_MBUS_VSYNC_ACTIVE_HIGH
;
1244 cfg
->type
= V4L2_MBUS_PARALLEL
;
1245 cfg
->flags
= soc_camera_apply_board_flags(icl
, cfg
);
1250 static int rj54n1_s_mbus_config(struct v4l2_subdev
*sd
,
1251 const struct v4l2_mbus_config
*cfg
)
1253 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
1254 struct soc_camera_link
*icl
= soc_camera_i2c_to_link(client
);
1256 /* Figures 2.5-1 to 2.5-3 - default falling pixclk edge */
1257 if (soc_camera_apply_board_flags(icl
, cfg
) &
1258 V4L2_MBUS_PCLK_SAMPLE_RISING
)
1259 return reg_write(client
, RJ54N1_OUT_SIGPO
, 1 << 4);
1261 return reg_write(client
, RJ54N1_OUT_SIGPO
, 0);
1264 static struct v4l2_subdev_video_ops rj54n1_subdev_video_ops
= {
1265 .s_stream
= rj54n1_s_stream
,
1266 .s_mbus_fmt
= rj54n1_s_fmt
,
1267 .g_mbus_fmt
= rj54n1_g_fmt
,
1268 .try_mbus_fmt
= rj54n1_try_fmt
,
1269 .enum_mbus_fmt
= rj54n1_enum_fmt
,
1270 .g_crop
= rj54n1_g_crop
,
1271 .s_crop
= rj54n1_s_crop
,
1272 .cropcap
= rj54n1_cropcap
,
1273 .g_mbus_config
= rj54n1_g_mbus_config
,
1274 .s_mbus_config
= rj54n1_s_mbus_config
,
1277 static struct v4l2_subdev_ops rj54n1_subdev_ops
= {
1278 .core
= &rj54n1_subdev_core_ops
,
1279 .video
= &rj54n1_subdev_video_ops
,
1283 * Interface active, can use i2c. If it fails, it can indeed mean, that
1284 * this wasn't our capture interface, so, we wait for the right one
1286 static int rj54n1_video_probe(struct i2c_client
*client
,
1287 struct rj54n1_pdata
*priv
)
1292 /* Read out the chip version register */
1293 data1
= reg_read(client
, RJ54N1_DEV_CODE
);
1294 data2
= reg_read(client
, RJ54N1_DEV_CODE2
);
1296 if (data1
!= 0x51 || data2
!= 0x10) {
1298 dev_info(&client
->dev
, "No RJ54N1CB0C found, read 0x%x:0x%x\n",
1303 /* Configure IOCTL polarity from the platform data: 0 or 1 << 7. */
1304 ret
= reg_write(client
, RJ54N1_IOC
, priv
->ioctl_high
<< 7);
1308 dev_info(&client
->dev
, "Detected a RJ54N1CB0C chip ID 0x%x:0x%x\n",
1315 static int rj54n1_probe(struct i2c_client
*client
,
1316 const struct i2c_device_id
*did
)
1318 struct rj54n1
*rj54n1
;
1319 struct soc_camera_link
*icl
= soc_camera_i2c_to_link(client
);
1320 struct i2c_adapter
*adapter
= to_i2c_adapter(client
->dev
.parent
);
1321 struct rj54n1_pdata
*rj54n1_priv
;
1324 if (!icl
|| !icl
->priv
) {
1325 dev_err(&client
->dev
, "RJ54N1CB0C: missing platform data!\n");
1329 rj54n1_priv
= icl
->priv
;
1331 if (!i2c_check_functionality(adapter
, I2C_FUNC_SMBUS_BYTE_DATA
)) {
1332 dev_warn(&adapter
->dev
,
1333 "I2C-Adapter doesn't support I2C_FUNC_SMBUS_BYTE\n");
1337 rj54n1
= kzalloc(sizeof(struct rj54n1
), GFP_KERNEL
);
1341 v4l2_i2c_subdev_init(&rj54n1
->subdev
, client
, &rj54n1_subdev_ops
);
1342 v4l2_ctrl_handler_init(&rj54n1
->hdl
, 4);
1343 v4l2_ctrl_new_std(&rj54n1
->hdl
, &rj54n1_ctrl_ops
,
1344 V4L2_CID_VFLIP
, 0, 1, 1, 0);
1345 v4l2_ctrl_new_std(&rj54n1
->hdl
, &rj54n1_ctrl_ops
,
1346 V4L2_CID_HFLIP
, 0, 1, 1, 0);
1347 v4l2_ctrl_new_std(&rj54n1
->hdl
, &rj54n1_ctrl_ops
,
1348 V4L2_CID_GAIN
, 0, 127, 1, 66);
1349 v4l2_ctrl_new_std(&rj54n1
->hdl
, &rj54n1_ctrl_ops
,
1350 V4L2_CID_AUTO_WHITE_BALANCE
, 0, 1, 1, 1);
1351 rj54n1
->subdev
.ctrl_handler
= &rj54n1
->hdl
;
1352 if (rj54n1
->hdl
.error
) {
1353 int err
= rj54n1
->hdl
.error
;
1359 rj54n1
->clk_div
= clk_div
;
1360 rj54n1
->rect
.left
= RJ54N1_COLUMN_SKIP
;
1361 rj54n1
->rect
.top
= RJ54N1_ROW_SKIP
;
1362 rj54n1
->rect
.width
= RJ54N1_MAX_WIDTH
;
1363 rj54n1
->rect
.height
= RJ54N1_MAX_HEIGHT
;
1364 rj54n1
->width
= RJ54N1_MAX_WIDTH
;
1365 rj54n1
->height
= RJ54N1_MAX_HEIGHT
;
1366 rj54n1
->fmt
= &rj54n1_colour_fmts
[0];
1367 rj54n1
->resize
= 1024;
1368 rj54n1
->tgclk_mhz
= (rj54n1_priv
->mclk_freq
/ PLL_L
* PLL_N
) /
1369 (clk_div
.ratio_tg
+ 1) / (clk_div
.ratio_t
+ 1);
1371 ret
= rj54n1_video_probe(client
, rj54n1_priv
);
1373 v4l2_ctrl_handler_free(&rj54n1
->hdl
);
1377 return v4l2_ctrl_handler_setup(&rj54n1
->hdl
);
1380 static int rj54n1_remove(struct i2c_client
*client
)
1382 struct rj54n1
*rj54n1
= to_rj54n1(client
);
1383 struct soc_camera_link
*icl
= soc_camera_i2c_to_link(client
);
1385 v4l2_device_unregister_subdev(&rj54n1
->subdev
);
1388 v4l2_ctrl_handler_free(&rj54n1
->hdl
);
1394 static const struct i2c_device_id rj54n1_id
[] = {
1395 { "rj54n1cb0c", 0 },
1398 MODULE_DEVICE_TABLE(i2c
, rj54n1_id
);
1400 static struct i2c_driver rj54n1_i2c_driver
= {
1402 .name
= "rj54n1cb0c",
1404 .probe
= rj54n1_probe
,
1405 .remove
= rj54n1_remove
,
1406 .id_table
= rj54n1_id
,
1409 static int __init
rj54n1_mod_init(void)
1411 return i2c_add_driver(&rj54n1_i2c_driver
);
1414 static void __exit
rj54n1_mod_exit(void)
1416 i2c_del_driver(&rj54n1_i2c_driver
);
1419 module_init(rj54n1_mod_init
);
1420 module_exit(rj54n1_mod_exit
);
1422 MODULE_DESCRIPTION("Sharp RJ54N1CB0C Camera driver");
1423 MODULE_AUTHOR("Guennadi Liakhovetski <g.liakhovetski@gmx.de>");
1424 MODULE_LICENSE("GPL v2");