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>
16 #include <linux/module.h>
18 #include <media/i2c/rj54n1cb0c.h>
19 #include <media/soc_camera.h>
20 #include <media/v4l2-clk.h>
21 #include <media/v4l2-subdev.h>
22 #include <media/v4l2-ctrls.h>
24 #define RJ54N1_DEV_CODE 0x0400
25 #define RJ54N1_DEV_CODE2 0x0401
26 #define RJ54N1_OUT_SEL 0x0403
27 #define RJ54N1_XY_OUTPUT_SIZE_S_H 0x0404
28 #define RJ54N1_X_OUTPUT_SIZE_S_L 0x0405
29 #define RJ54N1_Y_OUTPUT_SIZE_S_L 0x0406
30 #define RJ54N1_XY_OUTPUT_SIZE_P_H 0x0407
31 #define RJ54N1_X_OUTPUT_SIZE_P_L 0x0408
32 #define RJ54N1_Y_OUTPUT_SIZE_P_L 0x0409
33 #define RJ54N1_LINE_LENGTH_PCK_S_H 0x040a
34 #define RJ54N1_LINE_LENGTH_PCK_S_L 0x040b
35 #define RJ54N1_LINE_LENGTH_PCK_P_H 0x040c
36 #define RJ54N1_LINE_LENGTH_PCK_P_L 0x040d
37 #define RJ54N1_RESIZE_N 0x040e
38 #define RJ54N1_RESIZE_N_STEP 0x040f
39 #define RJ54N1_RESIZE_STEP 0x0410
40 #define RJ54N1_RESIZE_HOLD_H 0x0411
41 #define RJ54N1_RESIZE_HOLD_L 0x0412
42 #define RJ54N1_H_OBEN_OFS 0x0413
43 #define RJ54N1_V_OBEN_OFS 0x0414
44 #define RJ54N1_RESIZE_CONTROL 0x0415
45 #define RJ54N1_STILL_CONTROL 0x0417
46 #define RJ54N1_INC_USE_SEL_H 0x0425
47 #define RJ54N1_INC_USE_SEL_L 0x0426
48 #define RJ54N1_MIRROR_STILL_MODE 0x0427
49 #define RJ54N1_INIT_START 0x0428
50 #define RJ54N1_SCALE_1_2_LEV 0x0429
51 #define RJ54N1_SCALE_4_LEV 0x042a
52 #define RJ54N1_Y_GAIN 0x04d8
53 #define RJ54N1_APT_GAIN_UP 0x04fa
54 #define RJ54N1_RA_SEL_UL 0x0530
55 #define RJ54N1_BYTE_SWAP 0x0531
56 #define RJ54N1_OUT_SIGPO 0x053b
57 #define RJ54N1_WB_SEL_WEIGHT_I 0x054e
58 #define RJ54N1_BIT8_WB 0x0569
59 #define RJ54N1_HCAPS_WB 0x056a
60 #define RJ54N1_VCAPS_WB 0x056b
61 #define RJ54N1_HCAPE_WB 0x056c
62 #define RJ54N1_VCAPE_WB 0x056d
63 #define RJ54N1_EXPOSURE_CONTROL 0x058c
64 #define RJ54N1_FRAME_LENGTH_S_H 0x0595
65 #define RJ54N1_FRAME_LENGTH_S_L 0x0596
66 #define RJ54N1_FRAME_LENGTH_P_H 0x0597
67 #define RJ54N1_FRAME_LENGTH_P_L 0x0598
68 #define RJ54N1_PEAK_H 0x05b7
69 #define RJ54N1_PEAK_50 0x05b8
70 #define RJ54N1_PEAK_60 0x05b9
71 #define RJ54N1_PEAK_DIFF 0x05ba
72 #define RJ54N1_IOC 0x05ef
73 #define RJ54N1_TG_BYPASS 0x0700
74 #define RJ54N1_PLL_L 0x0701
75 #define RJ54N1_PLL_N 0x0702
76 #define RJ54N1_PLL_EN 0x0704
77 #define RJ54N1_RATIO_TG 0x0706
78 #define RJ54N1_RATIO_T 0x0707
79 #define RJ54N1_RATIO_R 0x0708
80 #define RJ54N1_RAMP_TGCLK_EN 0x0709
81 #define RJ54N1_OCLK_DSP 0x0710
82 #define RJ54N1_RATIO_OP 0x0711
83 #define RJ54N1_RATIO_O 0x0712
84 #define RJ54N1_OCLK_SEL_EN 0x0713
85 #define RJ54N1_CLK_RST 0x0717
86 #define RJ54N1_RESET_STANDBY 0x0718
87 #define RJ54N1_FWFLG 0x07fe
89 #define E_EXCLK (1 << 7)
90 #define SOFT_STDBY (1 << 4)
91 #define SEN_RSTX (1 << 2)
92 #define TG_RSTX (1 << 1)
93 #define DSP_RSTX (1 << 0)
95 #define RESIZE_HOLD_SEL (1 << 2)
96 #define RESIZE_GO (1 << 1)
99 * When cropping, the camera automatically centers the cropped region, there
100 * doesn't seem to be a way to specify an explicit location of the rectangle.
102 #define RJ54N1_COLUMN_SKIP 0
103 #define RJ54N1_ROW_SKIP 0
104 #define RJ54N1_MAX_WIDTH 1600
105 #define RJ54N1_MAX_HEIGHT 1200
110 /* I2C addresses: 0x50, 0x51, 0x60, 0x61 */
112 /* RJ54N1CB0C has only one fixed colorspace per pixelcode */
113 struct rj54n1_datafmt
{
115 enum v4l2_colorspace colorspace
;
118 /* Find a data format by a pixel code in an array */
119 static const struct rj54n1_datafmt
*rj54n1_find_datafmt(
120 u32 code
, const struct rj54n1_datafmt
*fmt
,
124 for (i
= 0; i
< n
; i
++)
125 if (fmt
[i
].code
== code
)
131 static const struct rj54n1_datafmt rj54n1_colour_fmts
[] = {
132 {MEDIA_BUS_FMT_YUYV8_2X8
, V4L2_COLORSPACE_JPEG
},
133 {MEDIA_BUS_FMT_YVYU8_2X8
, V4L2_COLORSPACE_JPEG
},
134 {MEDIA_BUS_FMT_RGB565_2X8_LE
, V4L2_COLORSPACE_SRGB
},
135 {MEDIA_BUS_FMT_RGB565_2X8_BE
, V4L2_COLORSPACE_SRGB
},
136 {MEDIA_BUS_FMT_SBGGR10_2X8_PADHI_LE
, V4L2_COLORSPACE_SRGB
},
137 {MEDIA_BUS_FMT_SBGGR10_2X8_PADLO_LE
, V4L2_COLORSPACE_SRGB
},
138 {MEDIA_BUS_FMT_SBGGR10_2X8_PADHI_BE
, V4L2_COLORSPACE_SRGB
},
139 {MEDIA_BUS_FMT_SBGGR10_2X8_PADLO_BE
, V4L2_COLORSPACE_SRGB
},
140 {MEDIA_BUS_FMT_SBGGR10_1X10
, V4L2_COLORSPACE_SRGB
},
143 struct rj54n1_clock_div
{
144 u8 ratio_tg
; /* can be 0 or an odd number */
152 struct v4l2_subdev subdev
;
153 struct v4l2_ctrl_handler hdl
;
154 struct v4l2_clk
*clk
;
155 struct rj54n1_clock_div clk_div
;
156 const struct rj54n1_datafmt
*fmt
;
157 struct v4l2_rect rect
; /* Sensor window */
158 unsigned int tgclk_mhz
;
160 unsigned short width
; /* Output window */
161 unsigned short height
;
162 unsigned short resize
; /* Sensor * 1024 / resize = Output */
163 unsigned short scale
;
167 struct rj54n1_reg_val
{
172 static const struct rj54n1_reg_val bank_4
[] = {
193 static const struct rj54n1_reg_val bank_5
[] = {
214 static const struct rj54n1_reg_val bank_7
[] = {
222 static const struct rj54n1_reg_val bank_8
[] = {
410 static const struct rj54n1_reg_val bank_10
[] = {
414 /* Clock dividers - these are default register values, divider = register + 1 */
415 static const struct rj54n1_clock_div clk_div
= {
416 .ratio_tg
= 3 /* default: 5 */,
417 .ratio_t
= 4 /* default: 1 */,
418 .ratio_r
= 4 /* default: 0 */,
419 .ratio_op
= 1 /* default: 5 */,
420 .ratio_o
= 9 /* default: 0 */,
423 static struct rj54n1
*to_rj54n1(const struct i2c_client
*client
)
425 return container_of(i2c_get_clientdata(client
), struct rj54n1
, subdev
);
428 static int reg_read(struct i2c_client
*client
, const u16 reg
)
430 struct rj54n1
*rj54n1
= to_rj54n1(client
);
434 if (rj54n1
->bank
!= reg
>> 8) {
435 dev_dbg(&client
->dev
, "[0x%x] = 0x%x\n", 0xff, reg
>> 8);
436 ret
= i2c_smbus_write_byte_data(client
, 0xff, reg
>> 8);
439 rj54n1
->bank
= reg
>> 8;
441 return i2c_smbus_read_byte_data(client
, reg
& 0xff);
444 static int reg_write(struct i2c_client
*client
, const u16 reg
,
447 struct rj54n1
*rj54n1
= to_rj54n1(client
);
451 if (rj54n1
->bank
!= reg
>> 8) {
452 dev_dbg(&client
->dev
, "[0x%x] = 0x%x\n", 0xff, reg
>> 8);
453 ret
= i2c_smbus_write_byte_data(client
, 0xff, reg
>> 8);
456 rj54n1
->bank
= reg
>> 8;
458 dev_dbg(&client
->dev
, "[0x%x] = 0x%x\n", reg
& 0xff, data
);
459 return i2c_smbus_write_byte_data(client
, reg
& 0xff, data
);
462 static int reg_set(struct i2c_client
*client
, const u16 reg
,
463 const u8 data
, const u8 mask
)
467 ret
= reg_read(client
, reg
);
470 return reg_write(client
, reg
, (ret
& ~mask
) | (data
& mask
));
473 static int reg_write_multiple(struct i2c_client
*client
,
474 const struct rj54n1_reg_val
*rv
, const int n
)
478 for (i
= 0; i
< n
; i
++) {
479 ret
= reg_write(client
, rv
->reg
, rv
->val
);
488 static int rj54n1_enum_mbus_code(struct v4l2_subdev
*sd
,
489 struct v4l2_subdev_pad_config
*cfg
,
490 struct v4l2_subdev_mbus_code_enum
*code
)
492 if (code
->pad
|| code
->index
>= ARRAY_SIZE(rj54n1_colour_fmts
))
495 code
->code
= rj54n1_colour_fmts
[code
->index
].code
;
499 static int rj54n1_s_stream(struct v4l2_subdev
*sd
, int enable
)
501 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
503 /* Switch between preview and still shot modes */
504 return reg_set(client
, RJ54N1_STILL_CONTROL
, (!enable
) << 7, 0x80);
507 static int rj54n1_set_rect(struct i2c_client
*client
,
508 u16 reg_x
, u16 reg_y
, u16 reg_xy
,
509 u32 width
, u32 height
)
513 ret
= reg_write(client
, reg_xy
,
514 ((width
>> 4) & 0x70) |
515 ((height
>> 8) & 7));
518 ret
= reg_write(client
, reg_x
, width
& 0xff);
520 ret
= reg_write(client
, reg_y
, height
& 0xff);
526 * Some commands, specifically certain initialisation sequences, require
527 * a commit operation.
529 static int rj54n1_commit(struct i2c_client
*client
)
531 int ret
= reg_write(client
, RJ54N1_INIT_START
, 1);
534 ret
= reg_write(client
, RJ54N1_INIT_START
, 0);
538 static int rj54n1_sensor_scale(struct v4l2_subdev
*sd
, s32
*in_w
, s32
*in_h
,
539 s32
*out_w
, s32
*out_h
);
541 static int rj54n1_set_selection(struct v4l2_subdev
*sd
,
542 struct v4l2_subdev_pad_config
*cfg
,
543 struct v4l2_subdev_selection
*sel
)
545 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
546 struct rj54n1
*rj54n1
= to_rj54n1(client
);
547 const struct v4l2_rect
*rect
= &sel
->r
;
548 int dummy
= 0, output_w
, output_h
,
549 input_w
= rect
->width
, input_h
= rect
->height
;
552 if (sel
->which
!= V4L2_SUBDEV_FORMAT_ACTIVE
||
553 sel
->target
!= V4L2_SEL_TGT_CROP
)
556 /* arbitrary minimum width and height, edges unimportant */
557 soc_camera_limit_side(&dummy
, &input_w
,
558 RJ54N1_COLUMN_SKIP
, 8, RJ54N1_MAX_WIDTH
);
560 soc_camera_limit_side(&dummy
, &input_h
,
561 RJ54N1_ROW_SKIP
, 8, RJ54N1_MAX_HEIGHT
);
563 output_w
= (input_w
* 1024 + rj54n1
->resize
/ 2) / rj54n1
->resize
;
564 output_h
= (input_h
* 1024 + rj54n1
->resize
/ 2) / rj54n1
->resize
;
566 dev_dbg(&client
->dev
, "Scaling for %dx%d : %u = %dx%d\n",
567 input_w
, input_h
, rj54n1
->resize
, output_w
, output_h
);
569 ret
= rj54n1_sensor_scale(sd
, &input_w
, &input_h
, &output_w
, &output_h
);
573 rj54n1
->width
= output_w
;
574 rj54n1
->height
= output_h
;
575 rj54n1
->resize
= ret
;
576 rj54n1
->rect
.width
= input_w
;
577 rj54n1
->rect
.height
= input_h
;
582 static int rj54n1_get_selection(struct v4l2_subdev
*sd
,
583 struct v4l2_subdev_pad_config
*cfg
,
584 struct v4l2_subdev_selection
*sel
)
586 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
587 struct rj54n1
*rj54n1
= to_rj54n1(client
);
589 if (sel
->which
!= V4L2_SUBDEV_FORMAT_ACTIVE
)
592 switch (sel
->target
) {
593 case V4L2_SEL_TGT_CROP_BOUNDS
:
594 case V4L2_SEL_TGT_CROP_DEFAULT
:
595 sel
->r
.left
= RJ54N1_COLUMN_SKIP
;
596 sel
->r
.top
= RJ54N1_ROW_SKIP
;
597 sel
->r
.width
= RJ54N1_MAX_WIDTH
;
598 sel
->r
.height
= RJ54N1_MAX_HEIGHT
;
600 case V4L2_SEL_TGT_CROP
:
601 sel
->r
= rj54n1
->rect
;
608 static int rj54n1_get_fmt(struct v4l2_subdev
*sd
,
609 struct v4l2_subdev_pad_config
*cfg
,
610 struct v4l2_subdev_format
*format
)
612 struct v4l2_mbus_framefmt
*mf
= &format
->format
;
613 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
614 struct rj54n1
*rj54n1
= to_rj54n1(client
);
619 mf
->code
= rj54n1
->fmt
->code
;
620 mf
->colorspace
= rj54n1
->fmt
->colorspace
;
621 mf
->field
= V4L2_FIELD_NONE
;
622 mf
->width
= rj54n1
->width
;
623 mf
->height
= rj54n1
->height
;
629 * The actual geometry configuration routine. It scales the input window into
630 * the output one, updates the window sizes and returns an error or the resize
631 * coefficient on success. Note: we only use the "Fixed Scaling" on this camera.
633 static int rj54n1_sensor_scale(struct v4l2_subdev
*sd
, s32
*in_w
, s32
*in_h
,
634 s32
*out_w
, s32
*out_h
)
636 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
637 struct rj54n1
*rj54n1
= to_rj54n1(client
);
638 unsigned int skip
, resize
, input_w
= *in_w
, input_h
= *in_h
,
639 output_w
= *out_w
, output_h
= *out_h
;
640 u16 inc_sel
, wb_bit8
, wb_left
, wb_right
, wb_top
, wb_bottom
;
641 unsigned int peak
, peak_50
, peak_60
;
645 * We have a problem with crops, where the window is larger than 512x384
646 * and output window is larger than a half of the input one. In this
647 * case we have to either reduce the input window to equal or below
648 * 512x384 or the output window to equal or below 1/2 of the input.
650 if (output_w
> max(512U, input_w
/ 2)) {
651 if (2 * output_w
> RJ54N1_MAX_WIDTH
) {
652 input_w
= RJ54N1_MAX_WIDTH
;
653 output_w
= RJ54N1_MAX_WIDTH
/ 2;
655 input_w
= output_w
* 2;
658 dev_dbg(&client
->dev
, "Adjusted output width: in %u, out %u\n",
662 if (output_h
> max(384U, input_h
/ 2)) {
663 if (2 * output_h
> RJ54N1_MAX_HEIGHT
) {
664 input_h
= RJ54N1_MAX_HEIGHT
;
665 output_h
= RJ54N1_MAX_HEIGHT
/ 2;
667 input_h
= output_h
* 2;
670 dev_dbg(&client
->dev
, "Adjusted output height: in %u, out %u\n",
674 /* Idea: use the read mode for snapshots, handle separate geometries */
675 ret
= rj54n1_set_rect(client
, RJ54N1_X_OUTPUT_SIZE_S_L
,
676 RJ54N1_Y_OUTPUT_SIZE_S_L
,
677 RJ54N1_XY_OUTPUT_SIZE_S_H
, output_w
, output_h
);
679 ret
= rj54n1_set_rect(client
, RJ54N1_X_OUTPUT_SIZE_P_L
,
680 RJ54N1_Y_OUTPUT_SIZE_P_L
,
681 RJ54N1_XY_OUTPUT_SIZE_P_H
, output_w
, output_h
);
686 if (output_w
> input_w
&& output_h
> input_h
) {
692 unsigned int resize_x
, resize_y
;
693 resize_x
= (input_w
* 1024 + output_w
/ 2) / output_w
;
694 resize_y
= (input_h
* 1024 + output_h
/ 2) / output_h
;
696 /* We want max(resize_x, resize_y), check if it still fits */
697 if (resize_x
> resize_y
&&
698 (output_h
* resize_x
+ 512) / 1024 > RJ54N1_MAX_HEIGHT
)
699 resize
= (RJ54N1_MAX_HEIGHT
* 1024 + output_h
/ 2) /
701 else if (resize_y
> resize_x
&&
702 (output_w
* resize_y
+ 512) / 1024 > RJ54N1_MAX_WIDTH
)
703 resize
= (RJ54N1_MAX_WIDTH
* 1024 + output_w
/ 2) /
706 resize
= max(resize_x
, resize_y
);
708 /* Prohibited value ranges */
719 case 16320 ... 16384:
725 ret
= reg_write(client
, RJ54N1_RESIZE_HOLD_L
, resize
& 0xff);
727 ret
= reg_write(client
, RJ54N1_RESIZE_HOLD_H
, resize
>> 8);
733 * Configure a skipping bitmask. The sensor will select a skipping value
734 * among set bits automatically. This is very unclear in the datasheet
735 * too. I was told, in this register one enables all skipping values,
736 * that are required for a specific resize, and the camera selects
737 * automatically, which ones to use. But it is unclear how to identify,
738 * which cropping values are needed. Secondly, why don't we just set all
739 * bits and let the camera choose? Would it increase processing time and
740 * reduce the framerate? Using 0xfffc for INC_USE_SEL doesn't seem to
741 * improve the image quality or stability for larger frames (see comment
742 * above), but I didn't check the framerate.
744 skip
= min(resize
/ 1024, 15U);
750 else if (resize
& 1023 && skip
< 15)
751 inc_sel
|= 1 << (skip
+ 1);
753 ret
= reg_write(client
, RJ54N1_INC_USE_SEL_L
, inc_sel
& 0xfc);
755 ret
= reg_write(client
, RJ54N1_INC_USE_SEL_H
, inc_sel
>> 8);
757 if (!rj54n1
->auto_wb
) {
758 /* Auto white balance window */
759 wb_left
= output_w
/ 16;
760 wb_right
= (3 * output_w
/ 4 - 3) / 4;
761 wb_top
= output_h
/ 16;
762 wb_bottom
= (3 * output_h
/ 4 - 3) / 4;
763 wb_bit8
= ((wb_left
>> 2) & 0x40) | ((wb_top
>> 4) & 0x10) |
764 ((wb_right
>> 6) & 4) | ((wb_bottom
>> 8) & 1);
767 ret
= reg_write(client
, RJ54N1_BIT8_WB
, wb_bit8
);
769 ret
= reg_write(client
, RJ54N1_HCAPS_WB
, wb_left
);
771 ret
= reg_write(client
, RJ54N1_VCAPS_WB
, wb_top
);
773 ret
= reg_write(client
, RJ54N1_HCAPE_WB
, wb_right
);
775 ret
= reg_write(client
, RJ54N1_VCAPE_WB
, wb_bottom
);
779 peak
= 12 * RJ54N1_MAX_WIDTH
* (1 << 14) * resize
/ rj54n1
->tgclk_mhz
/
785 ret
= reg_write(client
, RJ54N1_PEAK_H
,
786 ((peak_50
>> 4) & 0xf0) | (peak_60
>> 8));
788 ret
= reg_write(client
, RJ54N1_PEAK_50
, peak_50
);
790 ret
= reg_write(client
, RJ54N1_PEAK_60
, peak_60
);
792 ret
= reg_write(client
, RJ54N1_PEAK_DIFF
, peak
/ 150);
796 ret
= reg_write(client
, RJ54N1_RESIZE_CONTROL
,
797 RESIZE_HOLD_SEL
| RESIZE_GO
| 1);
802 /* Constant taken from manufacturer's example */
805 ret
= reg_write(client
, RJ54N1_RESIZE_CONTROL
, RESIZE_HOLD_SEL
| 1);
809 *in_w
= (output_w
* resize
+ 512) / 1024;
810 *in_h
= (output_h
* resize
+ 512) / 1024;
814 dev_dbg(&client
->dev
, "Scaled for %dx%d : %u = %ux%u, skip %u\n",
815 *in_w
, *in_h
, resize
, output_w
, output_h
, skip
);
820 static int rj54n1_set_clock(struct i2c_client
*client
)
822 struct rj54n1
*rj54n1
= to_rj54n1(client
);
825 /* Enable external clock */
826 ret
= reg_write(client
, RJ54N1_RESET_STANDBY
, E_EXCLK
| SOFT_STDBY
);
827 /* Leave stand-by. Note: use this when implementing suspend / resume */
829 ret
= reg_write(client
, RJ54N1_RESET_STANDBY
, E_EXCLK
);
832 ret
= reg_write(client
, RJ54N1_PLL_L
, PLL_L
);
834 ret
= reg_write(client
, RJ54N1_PLL_N
, PLL_N
);
838 ret
= reg_write(client
, RJ54N1_RATIO_TG
,
839 rj54n1
->clk_div
.ratio_tg
);
841 ret
= reg_write(client
, RJ54N1_RATIO_T
,
842 rj54n1
->clk_div
.ratio_t
);
844 ret
= reg_write(client
, RJ54N1_RATIO_R
,
845 rj54n1
->clk_div
.ratio_r
);
847 /* Enable TGCLK & RAMP */
849 ret
= reg_write(client
, RJ54N1_RAMP_TGCLK_EN
, 3);
851 /* Disable clock output */
853 ret
= reg_write(client
, RJ54N1_OCLK_DSP
, 0);
857 ret
= reg_write(client
, RJ54N1_RATIO_OP
,
858 rj54n1
->clk_div
.ratio_op
);
860 ret
= reg_write(client
, RJ54N1_RATIO_O
,
861 rj54n1
->clk_div
.ratio_o
);
865 ret
= reg_write(client
, RJ54N1_OCLK_SEL_EN
, 1);
867 /* Use PLL for Timing Generator, write 2 to reserved bits */
869 ret
= reg_write(client
, RJ54N1_TG_BYPASS
, 2);
871 /* Take sensor out of reset */
873 ret
= reg_write(client
, RJ54N1_RESET_STANDBY
,
877 ret
= reg_write(client
, RJ54N1_PLL_EN
, 1);
879 /* Wait for PLL to stabilise */
882 /* Enable clock to frequency divider */
884 ret
= reg_write(client
, RJ54N1_CLK_RST
, 1);
887 ret
= reg_read(client
, RJ54N1_CLK_RST
);
889 dev_err(&client
->dev
,
890 "Resetting RJ54N1CB0C clock failed: %d!\n", ret
);
895 ret
= reg_set(client
, RJ54N1_OCLK_DSP
, 1, 1);
899 ret
= reg_write(client
, RJ54N1_OCLK_SEL_EN
, 1);
904 static int rj54n1_reg_init(struct i2c_client
*client
)
906 struct rj54n1
*rj54n1
= to_rj54n1(client
);
907 int ret
= rj54n1_set_clock(client
);
910 ret
= reg_write_multiple(client
, bank_7
, ARRAY_SIZE(bank_7
));
912 ret
= reg_write_multiple(client
, bank_10
, ARRAY_SIZE(bank_10
));
914 /* Set binning divisors */
916 ret
= reg_write(client
, RJ54N1_SCALE_1_2_LEV
, 3 | (7 << 4));
918 ret
= reg_write(client
, RJ54N1_SCALE_4_LEV
, 0xf);
920 /* Switch to fixed resize mode */
922 ret
= reg_write(client
, RJ54N1_RESIZE_CONTROL
,
923 RESIZE_HOLD_SEL
| 1);
927 ret
= reg_write(client
, RJ54N1_Y_GAIN
, 0x84);
930 * Mirror the image back: default is upside down and left-to-right...
931 * Set manual preview / still shot switching
934 ret
= reg_write(client
, RJ54N1_MIRROR_STILL_MODE
, 0x27);
937 ret
= reg_write_multiple(client
, bank_4
, ARRAY_SIZE(bank_4
));
939 /* Auto exposure area */
941 ret
= reg_write(client
, RJ54N1_EXPOSURE_CONTROL
, 0x80);
942 /* Check current auto WB config */
944 ret
= reg_read(client
, RJ54N1_WB_SEL_WEIGHT_I
);
946 rj54n1
->auto_wb
= ret
& 0x80;
947 ret
= reg_write_multiple(client
, bank_5
, ARRAY_SIZE(bank_5
));
950 ret
= reg_write_multiple(client
, bank_8
, ARRAY_SIZE(bank_8
));
953 ret
= reg_write(client
, RJ54N1_RESET_STANDBY
,
954 E_EXCLK
| DSP_RSTX
| SEN_RSTX
);
958 ret
= rj54n1_commit(client
);
960 /* Take DSP, TG, sensor out of reset */
962 ret
= reg_write(client
, RJ54N1_RESET_STANDBY
,
963 E_EXCLK
| DSP_RSTX
| TG_RSTX
| SEN_RSTX
);
965 /* Start register update? Same register as 0x?FE in many bank_* sets */
967 ret
= reg_write(client
, RJ54N1_FWFLG
, 2);
969 /* Constant taken from manufacturer's example */
975 static int rj54n1_set_fmt(struct v4l2_subdev
*sd
,
976 struct v4l2_subdev_pad_config
*cfg
,
977 struct v4l2_subdev_format
*format
)
979 struct v4l2_mbus_framefmt
*mf
= &format
->format
;
980 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
981 struct rj54n1
*rj54n1
= to_rj54n1(client
);
982 const struct rj54n1_datafmt
*fmt
;
983 int output_w
, output_h
, max_w
, max_h
,
984 input_w
= rj54n1
->rect
.width
, input_h
= rj54n1
->rect
.height
;
985 int align
= mf
->code
== MEDIA_BUS_FMT_SBGGR10_1X10
||
986 mf
->code
== MEDIA_BUS_FMT_SBGGR10_2X8_PADHI_BE
||
987 mf
->code
== MEDIA_BUS_FMT_SBGGR10_2X8_PADLO_BE
||
988 mf
->code
== MEDIA_BUS_FMT_SBGGR10_2X8_PADHI_LE
||
989 mf
->code
== MEDIA_BUS_FMT_SBGGR10_2X8_PADLO_LE
;
995 dev_dbg(&client
->dev
, "%s: code = %d, width = %u, height = %u\n",
996 __func__
, mf
->code
, mf
->width
, mf
->height
);
998 fmt
= rj54n1_find_datafmt(mf
->code
, rj54n1_colour_fmts
,
999 ARRAY_SIZE(rj54n1_colour_fmts
));
1002 mf
->code
= fmt
->code
;
1005 mf
->field
= V4L2_FIELD_NONE
;
1006 mf
->colorspace
= fmt
->colorspace
;
1008 v4l_bound_align_image(&mf
->width
, 112, RJ54N1_MAX_WIDTH
, align
,
1009 &mf
->height
, 84, RJ54N1_MAX_HEIGHT
, align
, 0);
1011 if (format
->which
== V4L2_SUBDEV_FORMAT_TRY
) {
1017 * Verify if the sensor has just been powered on. TODO: replace this
1018 * with proper PM, when a suitable API is available.
1020 ret
= reg_read(client
, RJ54N1_RESET_STANDBY
);
1024 if (!(ret
& E_EXCLK
)) {
1025 ret
= rj54n1_reg_init(client
);
1030 /* RA_SEL_UL is only relevant for raw modes, ignored otherwise. */
1032 case MEDIA_BUS_FMT_YUYV8_2X8
:
1033 ret
= reg_write(client
, RJ54N1_OUT_SEL
, 0);
1035 ret
= reg_set(client
, RJ54N1_BYTE_SWAP
, 8, 8);
1037 case MEDIA_BUS_FMT_YVYU8_2X8
:
1038 ret
= reg_write(client
, RJ54N1_OUT_SEL
, 0);
1040 ret
= reg_set(client
, RJ54N1_BYTE_SWAP
, 0, 8);
1042 case MEDIA_BUS_FMT_RGB565_2X8_LE
:
1043 ret
= reg_write(client
, RJ54N1_OUT_SEL
, 0x11);
1045 ret
= reg_set(client
, RJ54N1_BYTE_SWAP
, 8, 8);
1047 case MEDIA_BUS_FMT_RGB565_2X8_BE
:
1048 ret
= reg_write(client
, RJ54N1_OUT_SEL
, 0x11);
1050 ret
= reg_set(client
, RJ54N1_BYTE_SWAP
, 0, 8);
1052 case MEDIA_BUS_FMT_SBGGR10_2X8_PADLO_LE
:
1053 ret
= reg_write(client
, RJ54N1_OUT_SEL
, 4);
1055 ret
= reg_set(client
, RJ54N1_BYTE_SWAP
, 8, 8);
1057 ret
= reg_write(client
, RJ54N1_RA_SEL_UL
, 0);
1059 case MEDIA_BUS_FMT_SBGGR10_2X8_PADHI_LE
:
1060 ret
= reg_write(client
, RJ54N1_OUT_SEL
, 4);
1062 ret
= reg_set(client
, RJ54N1_BYTE_SWAP
, 8, 8);
1064 ret
= reg_write(client
, RJ54N1_RA_SEL_UL
, 8);
1066 case MEDIA_BUS_FMT_SBGGR10_2X8_PADLO_BE
:
1067 ret
= reg_write(client
, RJ54N1_OUT_SEL
, 4);
1069 ret
= reg_set(client
, RJ54N1_BYTE_SWAP
, 0, 8);
1071 ret
= reg_write(client
, RJ54N1_RA_SEL_UL
, 0);
1073 case MEDIA_BUS_FMT_SBGGR10_2X8_PADHI_BE
:
1074 ret
= reg_write(client
, RJ54N1_OUT_SEL
, 4);
1076 ret
= reg_set(client
, RJ54N1_BYTE_SWAP
, 0, 8);
1078 ret
= reg_write(client
, RJ54N1_RA_SEL_UL
, 8);
1080 case MEDIA_BUS_FMT_SBGGR10_1X10
:
1081 ret
= reg_write(client
, RJ54N1_OUT_SEL
, 5);
1087 /* Special case: a raw mode with 10 bits of data per clock tick */
1089 ret
= reg_set(client
, RJ54N1_OCLK_SEL_EN
,
1090 (mf
->code
== MEDIA_BUS_FMT_SBGGR10_1X10
) << 1, 2);
1095 /* Supported scales 1:1 >= scale > 1:16 */
1096 max_w
= mf
->width
* (16 * 1024 - 1) / 1024;
1097 if (input_w
> max_w
)
1099 max_h
= mf
->height
* (16 * 1024 - 1) / 1024;
1100 if (input_h
> max_h
)
1103 output_w
= mf
->width
;
1104 output_h
= mf
->height
;
1106 ret
= rj54n1_sensor_scale(sd
, &input_w
, &input_h
, &output_w
, &output_h
);
1110 fmt
= rj54n1_find_datafmt(mf
->code
, rj54n1_colour_fmts
,
1111 ARRAY_SIZE(rj54n1_colour_fmts
));
1114 rj54n1
->resize
= ret
;
1115 rj54n1
->rect
.width
= input_w
;
1116 rj54n1
->rect
.height
= input_h
;
1117 rj54n1
->width
= output_w
;
1118 rj54n1
->height
= output_h
;
1120 mf
->width
= output_w
;
1121 mf
->height
= output_h
;
1122 mf
->field
= V4L2_FIELD_NONE
;
1123 mf
->colorspace
= fmt
->colorspace
;
1128 #ifdef CONFIG_VIDEO_ADV_DEBUG
1129 static int rj54n1_g_register(struct v4l2_subdev
*sd
,
1130 struct v4l2_dbg_register
*reg
)
1132 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
1134 if (reg
->reg
< 0x400 || reg
->reg
> 0x1fff)
1135 /* Registers > 0x0800 are only available from Sharp support */
1139 reg
->val
= reg_read(client
, reg
->reg
);
1141 if (reg
->val
> 0xff)
1147 static int rj54n1_s_register(struct v4l2_subdev
*sd
,
1148 const struct v4l2_dbg_register
*reg
)
1150 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
1152 if (reg
->reg
< 0x400 || reg
->reg
> 0x1fff)
1153 /* Registers >= 0x0800 are only available from Sharp support */
1156 if (reg_write(client
, reg
->reg
, reg
->val
) < 0)
1163 static int rj54n1_s_power(struct v4l2_subdev
*sd
, int on
)
1165 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
1166 struct soc_camera_subdev_desc
*ssdd
= soc_camera_i2c_to_desc(client
);
1167 struct rj54n1
*rj54n1
= to_rj54n1(client
);
1169 return soc_camera_set_power(&client
->dev
, ssdd
, rj54n1
->clk
, on
);
1172 static int rj54n1_s_ctrl(struct v4l2_ctrl
*ctrl
)
1174 struct rj54n1
*rj54n1
= container_of(ctrl
->handler
, struct rj54n1
, hdl
);
1175 struct v4l2_subdev
*sd
= &rj54n1
->subdev
;
1176 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
1180 case V4L2_CID_VFLIP
:
1182 data
= reg_set(client
, RJ54N1_MIRROR_STILL_MODE
, 0, 1);
1184 data
= reg_set(client
, RJ54N1_MIRROR_STILL_MODE
, 1, 1);
1188 case V4L2_CID_HFLIP
:
1190 data
= reg_set(client
, RJ54N1_MIRROR_STILL_MODE
, 0, 2);
1192 data
= reg_set(client
, RJ54N1_MIRROR_STILL_MODE
, 2, 2);
1197 if (reg_write(client
, RJ54N1_Y_GAIN
, ctrl
->val
* 2) < 0)
1200 case V4L2_CID_AUTO_WHITE_BALANCE
:
1201 /* Auto WB area - whole image */
1202 if (reg_set(client
, RJ54N1_WB_SEL_WEIGHT_I
, ctrl
->val
<< 7,
1205 rj54n1
->auto_wb
= ctrl
->val
;
1212 static const struct v4l2_ctrl_ops rj54n1_ctrl_ops
= {
1213 .s_ctrl
= rj54n1_s_ctrl
,
1216 static struct v4l2_subdev_core_ops rj54n1_subdev_core_ops
= {
1217 #ifdef CONFIG_VIDEO_ADV_DEBUG
1218 .g_register
= rj54n1_g_register
,
1219 .s_register
= rj54n1_s_register
,
1221 .s_power
= rj54n1_s_power
,
1224 static int rj54n1_g_mbus_config(struct v4l2_subdev
*sd
,
1225 struct v4l2_mbus_config
*cfg
)
1227 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
1228 struct soc_camera_subdev_desc
*ssdd
= soc_camera_i2c_to_desc(client
);
1231 V4L2_MBUS_PCLK_SAMPLE_RISING
| V4L2_MBUS_PCLK_SAMPLE_FALLING
|
1232 V4L2_MBUS_MASTER
| V4L2_MBUS_DATA_ACTIVE_HIGH
|
1233 V4L2_MBUS_HSYNC_ACTIVE_HIGH
| V4L2_MBUS_VSYNC_ACTIVE_HIGH
;
1234 cfg
->type
= V4L2_MBUS_PARALLEL
;
1235 cfg
->flags
= soc_camera_apply_board_flags(ssdd
, cfg
);
1240 static int rj54n1_s_mbus_config(struct v4l2_subdev
*sd
,
1241 const struct v4l2_mbus_config
*cfg
)
1243 struct i2c_client
*client
= v4l2_get_subdevdata(sd
);
1244 struct soc_camera_subdev_desc
*ssdd
= soc_camera_i2c_to_desc(client
);
1246 /* Figures 2.5-1 to 2.5-3 - default falling pixclk edge */
1247 if (soc_camera_apply_board_flags(ssdd
, cfg
) &
1248 V4L2_MBUS_PCLK_SAMPLE_RISING
)
1249 return reg_write(client
, RJ54N1_OUT_SIGPO
, 1 << 4);
1251 return reg_write(client
, RJ54N1_OUT_SIGPO
, 0);
1254 static struct v4l2_subdev_video_ops rj54n1_subdev_video_ops
= {
1255 .s_stream
= rj54n1_s_stream
,
1256 .g_mbus_config
= rj54n1_g_mbus_config
,
1257 .s_mbus_config
= rj54n1_s_mbus_config
,
1260 static const struct v4l2_subdev_pad_ops rj54n1_subdev_pad_ops
= {
1261 .enum_mbus_code
= rj54n1_enum_mbus_code
,
1262 .get_selection
= rj54n1_get_selection
,
1263 .set_selection
= rj54n1_set_selection
,
1264 .get_fmt
= rj54n1_get_fmt
,
1265 .set_fmt
= rj54n1_set_fmt
,
1268 static struct v4l2_subdev_ops rj54n1_subdev_ops
= {
1269 .core
= &rj54n1_subdev_core_ops
,
1270 .video
= &rj54n1_subdev_video_ops
,
1271 .pad
= &rj54n1_subdev_pad_ops
,
1275 * Interface active, can use i2c. If it fails, it can indeed mean, that
1276 * this wasn't our capture interface, so, we wait for the right one
1278 static int rj54n1_video_probe(struct i2c_client
*client
,
1279 struct rj54n1_pdata
*priv
)
1281 struct rj54n1
*rj54n1
= to_rj54n1(client
);
1285 ret
= rj54n1_s_power(&rj54n1
->subdev
, 1);
1289 /* Read out the chip version register */
1290 data1
= reg_read(client
, RJ54N1_DEV_CODE
);
1291 data2
= reg_read(client
, RJ54N1_DEV_CODE2
);
1293 if (data1
!= 0x51 || data2
!= 0x10) {
1295 dev_info(&client
->dev
, "No RJ54N1CB0C found, read 0x%x:0x%x\n",
1300 /* Configure IOCTL polarity from the platform data: 0 or 1 << 7. */
1301 ret
= reg_write(client
, RJ54N1_IOC
, priv
->ioctl_high
<< 7);
1305 dev_info(&client
->dev
, "Detected a RJ54N1CB0C chip ID 0x%x:0x%x\n",
1308 ret
= v4l2_ctrl_handler_setup(&rj54n1
->hdl
);
1311 rj54n1_s_power(&rj54n1
->subdev
, 0);
1315 static int rj54n1_probe(struct i2c_client
*client
,
1316 const struct i2c_device_id
*did
)
1318 struct rj54n1
*rj54n1
;
1319 struct soc_camera_subdev_desc
*ssdd
= soc_camera_i2c_to_desc(client
);
1320 struct i2c_adapter
*adapter
= to_i2c_adapter(client
->dev
.parent
);
1321 struct rj54n1_pdata
*rj54n1_priv
;
1324 if (!ssdd
|| !ssdd
->drv_priv
) {
1325 dev_err(&client
->dev
, "RJ54N1CB0C: missing platform data!\n");
1329 rj54n1_priv
= ssdd
->drv_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
= devm_kzalloc(&client
->dev
, 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 return rj54n1
->hdl
.error
;
1355 rj54n1
->clk_div
= clk_div
;
1356 rj54n1
->rect
.left
= RJ54N1_COLUMN_SKIP
;
1357 rj54n1
->rect
.top
= RJ54N1_ROW_SKIP
;
1358 rj54n1
->rect
.width
= RJ54N1_MAX_WIDTH
;
1359 rj54n1
->rect
.height
= RJ54N1_MAX_HEIGHT
;
1360 rj54n1
->width
= RJ54N1_MAX_WIDTH
;
1361 rj54n1
->height
= RJ54N1_MAX_HEIGHT
;
1362 rj54n1
->fmt
= &rj54n1_colour_fmts
[0];
1363 rj54n1
->resize
= 1024;
1364 rj54n1
->tgclk_mhz
= (rj54n1_priv
->mclk_freq
/ PLL_L
* PLL_N
) /
1365 (clk_div
.ratio_tg
+ 1) / (clk_div
.ratio_t
+ 1);
1367 rj54n1
->clk
= v4l2_clk_get(&client
->dev
, "mclk");
1368 if (IS_ERR(rj54n1
->clk
)) {
1369 ret
= PTR_ERR(rj54n1
->clk
);
1373 ret
= rj54n1_video_probe(client
, rj54n1_priv
);
1375 v4l2_clk_put(rj54n1
->clk
);
1377 v4l2_ctrl_handler_free(&rj54n1
->hdl
);
1383 static int rj54n1_remove(struct i2c_client
*client
)
1385 struct rj54n1
*rj54n1
= to_rj54n1(client
);
1386 struct soc_camera_subdev_desc
*ssdd
= soc_camera_i2c_to_desc(client
);
1388 v4l2_clk_put(rj54n1
->clk
);
1389 v4l2_device_unregister_subdev(&rj54n1
->subdev
);
1391 ssdd
->free_bus(ssdd
);
1392 v4l2_ctrl_handler_free(&rj54n1
->hdl
);
1397 static const struct i2c_device_id rj54n1_id
[] = {
1398 { "rj54n1cb0c", 0 },
1401 MODULE_DEVICE_TABLE(i2c
, rj54n1_id
);
1403 static struct i2c_driver rj54n1_i2c_driver
= {
1405 .name
= "rj54n1cb0c",
1407 .probe
= rj54n1_probe
,
1408 .remove
= rj54n1_remove
,
1409 .id_table
= rj54n1_id
,
1412 module_i2c_driver(rj54n1_i2c_driver
);
1414 MODULE_DESCRIPTION("Sharp RJ54N1CB0C Camera driver");
1415 MODULE_AUTHOR("Guennadi Liakhovetski <g.liakhovetski@gmx.de>");
1416 MODULE_LICENSE("GPL v2");