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/videodev2.h>
16 #include <media/rj54n1cb0c.h>
17 #include <media/soc_camera.h>
18 #include <media/soc_mediabus.h>
19 #include <media/v4l2-subdev.h>
20 #include <media/v4l2-chip-ident.h>
22 #define RJ54N1_DEV_CODE 0x0400
23 #define RJ54N1_DEV_CODE2 0x0401
24 #define RJ54N1_OUT_SEL 0x0403
25 #define RJ54N1_XY_OUTPUT_SIZE_S_H 0x0404
26 #define RJ54N1_X_OUTPUT_SIZE_S_L 0x0405
27 #define RJ54N1_Y_OUTPUT_SIZE_S_L 0x0406
28 #define RJ54N1_XY_OUTPUT_SIZE_P_H 0x0407
29 #define RJ54N1_X_OUTPUT_SIZE_P_L 0x0408
30 #define RJ54N1_Y_OUTPUT_SIZE_P_L 0x0409
31 #define RJ54N1_LINE_LENGTH_PCK_S_H 0x040a
32 #define RJ54N1_LINE_LENGTH_PCK_S_L 0x040b
33 #define RJ54N1_LINE_LENGTH_PCK_P_H 0x040c
34 #define RJ54N1_LINE_LENGTH_PCK_P_L 0x040d
35 #define RJ54N1_RESIZE_N 0x040e
36 #define RJ54N1_RESIZE_N_STEP 0x040f
37 #define RJ54N1_RESIZE_STEP 0x0410
38 #define RJ54N1_RESIZE_HOLD_H 0x0411
39 #define RJ54N1_RESIZE_HOLD_L 0x0412
40 #define RJ54N1_H_OBEN_OFS 0x0413
41 #define RJ54N1_V_OBEN_OFS 0x0414
42 #define RJ54N1_RESIZE_CONTROL 0x0415
43 #define RJ54N1_STILL_CONTROL 0x0417
44 #define RJ54N1_INC_USE_SEL_H 0x0425
45 #define RJ54N1_INC_USE_SEL_L 0x0426
46 #define RJ54N1_MIRROR_STILL_MODE 0x0427
47 #define RJ54N1_INIT_START 0x0428
48 #define RJ54N1_SCALE_1_2_LEV 0x0429
49 #define RJ54N1_SCALE_4_LEV 0x042a
50 #define RJ54N1_Y_GAIN 0x04d8
51 #define RJ54N1_APT_GAIN_UP 0x04fa
52 #define RJ54N1_RA_SEL_UL 0x0530
53 #define RJ54N1_BYTE_SWAP 0x0531
54 #define RJ54N1_OUT_SIGPO 0x053b
55 #define RJ54N1_WB_SEL_WEIGHT_I 0x054e
56 #define RJ54N1_BIT8_WB 0x0569
57 #define RJ54N1_HCAPS_WB 0x056a
58 #define RJ54N1_VCAPS_WB 0x056b
59 #define RJ54N1_HCAPE_WB 0x056c
60 #define RJ54N1_VCAPE_WB 0x056d
61 #define RJ54N1_EXPOSURE_CONTROL 0x058c
62 #define RJ54N1_FRAME_LENGTH_S_H 0x0595
63 #define RJ54N1_FRAME_LENGTH_S_L 0x0596
64 #define RJ54N1_FRAME_LENGTH_P_H 0x0597
65 #define RJ54N1_FRAME_LENGTH_P_L 0x0598
66 #define RJ54N1_PEAK_H 0x05b7
67 #define RJ54N1_PEAK_50 0x05b8
68 #define RJ54N1_PEAK_60 0x05b9
69 #define RJ54N1_PEAK_DIFF 0x05ba
70 #define RJ54N1_IOC 0x05ef
71 #define RJ54N1_TG_BYPASS 0x0700
72 #define RJ54N1_PLL_L 0x0701
73 #define RJ54N1_PLL_N 0x0702
74 #define RJ54N1_PLL_EN 0x0704
75 #define RJ54N1_RATIO_TG 0x0706
76 #define RJ54N1_RATIO_T 0x0707
77 #define RJ54N1_RATIO_R 0x0708
78 #define RJ54N1_RAMP_TGCLK_EN 0x0709
79 #define RJ54N1_OCLK_DSP 0x0710
80 #define RJ54N1_RATIO_OP 0x0711
81 #define RJ54N1_RATIO_O 0x0712
82 #define RJ54N1_OCLK_SEL_EN 0x0713
83 #define RJ54N1_CLK_RST 0x0717
84 #define RJ54N1_RESET_STANDBY 0x0718
85 #define RJ54N1_FWFLG 0x07fe
87 #define E_EXCLK (1 << 7)
88 #define SOFT_STDBY (1 << 4)
89 #define SEN_RSTX (1 << 2)
90 #define TG_RSTX (1 << 1)
91 #define DSP_RSTX (1 << 0)
93 #define RESIZE_HOLD_SEL (1 << 2)
94 #define RESIZE_GO (1 << 1)
97 * When cropping, the camera automatically centers the cropped region, there
98 * doesn't seem to be a way to specify an explicit location of the rectangle.
100 #define RJ54N1_COLUMN_SKIP 0
101 #define RJ54N1_ROW_SKIP 0
102 #define RJ54N1_MAX_WIDTH 1600
103 #define RJ54N1_MAX_HEIGHT 1200
108 /* I2C addresses: 0x50, 0x51, 0x60, 0x61 */
110 /* RJ54N1CB0C has only one fixed colorspace per pixelcode */
111 struct rj54n1_datafmt {
112 enum v4l2_mbus_pixelcode code;
113 enum v4l2_colorspace colorspace;
116 /* Find a data format by a pixel code in an array */
117 static const struct rj54n1_datafmt *rj54n1_find_datafmt(
118 enum v4l2_mbus_pixelcode code, const struct rj54n1_datafmt *fmt,
122 for (i = 0; i < n; i++)
123 if (fmt[i].code == code)
129 static const struct rj54n1_datafmt rj54n1_colour_fmts[] = {
130 {V4L2_MBUS_FMT_YUYV8_2X8, V4L2_COLORSPACE_JPEG},
131 {V4L2_MBUS_FMT_YVYU8_2X8, V4L2_COLORSPACE_JPEG},
132 {V4L2_MBUS_FMT_RGB565_2X8_LE, V4L2_COLORSPACE_SRGB},
133 {V4L2_MBUS_FMT_RGB565_2X8_BE, V4L2_COLORSPACE_SRGB},
134 {V4L2_MBUS_FMT_SBGGR10_2X8_PADHI_LE, V4L2_COLORSPACE_SRGB},
135 {V4L2_MBUS_FMT_SBGGR10_2X8_PADLO_LE, V4L2_COLORSPACE_SRGB},
136 {V4L2_MBUS_FMT_SBGGR10_2X8_PADHI_BE, V4L2_COLORSPACE_SRGB},
137 {V4L2_MBUS_FMT_SBGGR10_2X8_PADLO_BE, V4L2_COLORSPACE_SRGB},
138 {V4L2_MBUS_FMT_SBGGR10_1X10, V4L2_COLORSPACE_SRGB},
141 struct rj54n1_clock_div {
142 u8 ratio_tg; /* can be 0 or an odd number */
150 struct v4l2_subdev subdev;
151 struct rj54n1_clock_div clk_div;
152 const struct rj54n1_datafmt *fmt;
153 struct v4l2_rect rect; /* Sensor window */
154 unsigned int tgclk_mhz;
156 unsigned short width; /* Output window */
157 unsigned short height;
158 unsigned short resize; /* Sensor * 1024 / resize = Output */
159 unsigned short scale;
163 struct rj54n1_reg_val {
168 static const struct rj54n1_reg_val bank_4[] = {
189 static const struct rj54n1_reg_val bank_5[] = {
210 static const struct rj54n1_reg_val bank_7[] = {
218 static const struct rj54n1_reg_val bank_8[] = {
406 static const struct rj54n1_reg_val bank_10[] = {
410 /* Clock dividers - these are default register values, divider = register + 1 */
411 static const struct rj54n1_clock_div clk_div = {
412 .ratio_tg = 3 /* default: 5 */,
413 .ratio_t = 4 /* default: 1 */,
414 .ratio_r = 4 /* default: 0 */,
415 .ratio_op = 1 /* default: 5 */,
416 .ratio_o = 9 /* default: 0 */,
419 static struct rj54n1 *to_rj54n1(const struct i2c_client *client)
421 return container_of(i2c_get_clientdata(client), struct rj54n1, subdev);
424 static int reg_read(struct i2c_client *client, const u16 reg)
426 struct rj54n1 *rj54n1 = to_rj54n1(client);
430 if (rj54n1->bank != reg >> 8) {
431 dev_dbg(&client->dev, "[0x%x] = 0x%x\n", 0xff, reg >> 8);
432 ret = i2c_smbus_write_byte_data(client, 0xff, reg >> 8);
435 rj54n1->bank = reg >> 8;
437 return i2c_smbus_read_byte_data(client, reg & 0xff);
440 static int reg_write(struct i2c_client *client, const u16 reg,
443 struct rj54n1 *rj54n1 = to_rj54n1(client);
447 if (rj54n1->bank != reg >> 8) {
448 dev_dbg(&client->dev, "[0x%x] = 0x%x\n", 0xff, reg >> 8);
449 ret = i2c_smbus_write_byte_data(client, 0xff, reg >> 8);
452 rj54n1->bank = reg >> 8;
454 dev_dbg(&client->dev, "[0x%x] = 0x%x\n", reg & 0xff, data);
455 return i2c_smbus_write_byte_data(client, reg & 0xff, data);
458 static int reg_set(struct i2c_client *client, const u16 reg,
459 const u8 data, const u8 mask)
463 ret = reg_read(client, reg);
466 return reg_write(client, reg, (ret & ~mask) | (data & mask));
469 static int reg_write_multiple(struct i2c_client *client,
470 const struct rj54n1_reg_val *rv, const int n)
474 for (i = 0; i < n; i++) {
475 ret = reg_write(client, rv->reg, rv->val);
484 static int rj54n1_enum_fmt(struct v4l2_subdev *sd, unsigned int index,
485 enum v4l2_mbus_pixelcode *code)
487 if (index >= ARRAY_SIZE(rj54n1_colour_fmts))
490 *code = rj54n1_colour_fmts[index].code;
494 static int rj54n1_s_stream(struct v4l2_subdev *sd, int enable)
496 struct i2c_client *client = v4l2_get_subdevdata(sd);
498 /* Switch between preview and still shot modes */
499 return reg_set(client, RJ54N1_STILL_CONTROL, (!enable) << 7, 0x80);
502 static int rj54n1_set_bus_param(struct soc_camera_device *icd,
505 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
506 struct i2c_client *client = v4l2_get_subdevdata(sd);
507 /* Figures 2.5-1 to 2.5-3 - default falling pixclk edge */
509 if (flags & SOCAM_PCLK_SAMPLE_RISING)
510 return reg_write(client, RJ54N1_OUT_SIGPO, 1 << 4);
512 return reg_write(client, RJ54N1_OUT_SIGPO, 0);
515 static unsigned long rj54n1_query_bus_param(struct soc_camera_device *icd)
517 struct soc_camera_link *icl = to_soc_camera_link(icd);
518 const unsigned long flags =
519 SOCAM_PCLK_SAMPLE_RISING | SOCAM_PCLK_SAMPLE_FALLING |
520 SOCAM_MASTER | SOCAM_DATAWIDTH_8 |
521 SOCAM_HSYNC_ACTIVE_HIGH | SOCAM_VSYNC_ACTIVE_HIGH |
522 SOCAM_DATA_ACTIVE_HIGH;
524 return soc_camera_apply_sensor_flags(icl, flags);
527 static int rj54n1_set_rect(struct i2c_client *client,
528 u16 reg_x, u16 reg_y, u16 reg_xy,
529 u32 width, u32 height)
533 ret = reg_write(client, reg_xy,
534 ((width >> 4) & 0x70) |
535 ((height >> 8) & 7));
538 ret = reg_write(client, reg_x, width & 0xff);
540 ret = reg_write(client, reg_y, height & 0xff);
546 * Some commands, specifically certain initialisation sequences, require
547 * a commit operation.
549 static int rj54n1_commit(struct i2c_client *client)
551 int ret = reg_write(client, RJ54N1_INIT_START, 1);
554 ret = reg_write(client, RJ54N1_INIT_START, 0);
558 static int rj54n1_sensor_scale(struct v4l2_subdev *sd, s32 *in_w, s32 *in_h,
559 s32 *out_w, s32 *out_h);
561 static int rj54n1_s_crop(struct v4l2_subdev *sd, struct v4l2_crop *a)
563 struct i2c_client *client = v4l2_get_subdevdata(sd);
564 struct rj54n1 *rj54n1 = to_rj54n1(client);
565 struct v4l2_rect *rect = &a->c;
566 int dummy = 0, output_w, output_h,
567 input_w = rect->width, input_h = rect->height;
570 /* arbitrary minimum width and height, edges unimportant */
571 soc_camera_limit_side(&dummy, &input_w,
572 RJ54N1_COLUMN_SKIP, 8, RJ54N1_MAX_WIDTH);
574 soc_camera_limit_side(&dummy, &input_h,
575 RJ54N1_ROW_SKIP, 8, RJ54N1_MAX_HEIGHT);
577 output_w = (input_w * 1024 + rj54n1->resize / 2) / rj54n1->resize;
578 output_h = (input_h * 1024 + rj54n1->resize / 2) / rj54n1->resize;
580 dev_dbg(&client->dev, "Scaling for %dx%d : %u = %dx%d\n",
581 input_w, input_h, rj54n1->resize, output_w, output_h);
583 ret = rj54n1_sensor_scale(sd, &input_w, &input_h, &output_w, &output_h);
587 rj54n1->width = output_w;
588 rj54n1->height = output_h;
589 rj54n1->resize = ret;
590 rj54n1->rect.width = input_w;
591 rj54n1->rect.height = input_h;
596 static int rj54n1_g_crop(struct v4l2_subdev *sd, struct v4l2_crop *a)
598 struct i2c_client *client = v4l2_get_subdevdata(sd);
599 struct rj54n1 *rj54n1 = to_rj54n1(client);
602 a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
607 static int rj54n1_cropcap(struct v4l2_subdev *sd, struct v4l2_cropcap *a)
609 a->bounds.left = RJ54N1_COLUMN_SKIP;
610 a->bounds.top = RJ54N1_ROW_SKIP;
611 a->bounds.width = RJ54N1_MAX_WIDTH;
612 a->bounds.height = RJ54N1_MAX_HEIGHT;
613 a->defrect = a->bounds;
614 a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
615 a->pixelaspect.numerator = 1;
616 a->pixelaspect.denominator = 1;
621 static int rj54n1_g_fmt(struct v4l2_subdev *sd,
622 struct v4l2_mbus_framefmt *mf)
624 struct i2c_client *client = v4l2_get_subdevdata(sd);
625 struct rj54n1 *rj54n1 = to_rj54n1(client);
627 mf->code = rj54n1->fmt->code;
628 mf->colorspace = rj54n1->fmt->colorspace;
629 mf->field = V4L2_FIELD_NONE;
630 mf->width = rj54n1->width;
631 mf->height = rj54n1->height;
637 * The actual geometry configuration routine. It scales the input window into
638 * the output one, updates the window sizes and returns an error or the resize
639 * coefficient on success. Note: we only use the "Fixed Scaling" on this camera.
641 static int rj54n1_sensor_scale(struct v4l2_subdev *sd, s32 *in_w, s32 *in_h,
642 s32 *out_w, s32 *out_h)
644 struct i2c_client *client = v4l2_get_subdevdata(sd);
645 struct rj54n1 *rj54n1 = to_rj54n1(client);
646 unsigned int skip, resize, input_w = *in_w, input_h = *in_h,
647 output_w = *out_w, output_h = *out_h;
648 u16 inc_sel, wb_bit8, wb_left, wb_right, wb_top, wb_bottom;
649 unsigned int peak, peak_50, peak_60;
653 * We have a problem with crops, where the window is larger than 512x384
654 * and output window is larger than a half of the input one. In this
655 * case we have to either reduce the input window to equal or below
656 * 512x384 or the output window to equal or below 1/2 of the input.
658 if (output_w > max(512U, input_w / 2)) {
659 if (2 * output_w > RJ54N1_MAX_WIDTH) {
660 input_w = RJ54N1_MAX_WIDTH;
661 output_w = RJ54N1_MAX_WIDTH / 2;
663 input_w = output_w * 2;
666 dev_dbg(&client->dev, "Adjusted output width: in %u, out %u\n",
670 if (output_h > max(384U, input_h / 2)) {
671 if (2 * output_h > RJ54N1_MAX_HEIGHT) {
672 input_h = RJ54N1_MAX_HEIGHT;
673 output_h = RJ54N1_MAX_HEIGHT / 2;
675 input_h = output_h * 2;
678 dev_dbg(&client->dev, "Adjusted output height: in %u, out %u\n",
682 /* Idea: use the read mode for snapshots, handle separate geometries */
683 ret = rj54n1_set_rect(client, RJ54N1_X_OUTPUT_SIZE_S_L,
684 RJ54N1_Y_OUTPUT_SIZE_S_L,
685 RJ54N1_XY_OUTPUT_SIZE_S_H, output_w, output_h);
687 ret = rj54n1_set_rect(client, RJ54N1_X_OUTPUT_SIZE_P_L,
688 RJ54N1_Y_OUTPUT_SIZE_P_L,
689 RJ54N1_XY_OUTPUT_SIZE_P_H, output_w, output_h);
694 if (output_w > input_w && output_h > input_h) {
700 unsigned int resize_x, resize_y;
701 resize_x = (input_w * 1024 + output_w / 2) / output_w;
702 resize_y = (input_h * 1024 + output_h / 2) / output_h;
704 /* We want max(resize_x, resize_y), check if it still fits */
705 if (resize_x > resize_y &&
706 (output_h * resize_x + 512) / 1024 > RJ54N1_MAX_HEIGHT)
707 resize = (RJ54N1_MAX_HEIGHT * 1024 + output_h / 2) /
709 else if (resize_y > resize_x &&
710 (output_w * resize_y + 512) / 1024 > RJ54N1_MAX_WIDTH)
711 resize = (RJ54N1_MAX_WIDTH * 1024 + output_w / 2) /
714 resize = max(resize_x, resize_y);
716 /* Prohibited value ranges */
727 case 16320 ... 16384:
733 ret = reg_write(client, RJ54N1_RESIZE_HOLD_L, resize & 0xff);
735 ret = reg_write(client, RJ54N1_RESIZE_HOLD_H, resize >> 8);
741 * Configure a skipping bitmask. The sensor will select a skipping value
742 * among set bits automatically. This is very unclear in the datasheet
743 * too. I was told, in this register one enables all skipping values,
744 * that are required for a specific resize, and the camera selects
745 * automatically, which ones to use. But it is unclear how to identify,
746 * which cropping values are needed. Secondly, why don't we just set all
747 * bits and let the camera choose? Would it increase processing time and
748 * reduce the framerate? Using 0xfffc for INC_USE_SEL doesn't seem to
749 * improve the image quality or stability for larger frames (see comment
750 * above), but I didn't check the framerate.
752 skip = min(resize / 1024, 15U);
758 else if (resize & 1023 && skip < 15)
759 inc_sel |= 1 << (skip + 1);
761 ret = reg_write(client, RJ54N1_INC_USE_SEL_L, inc_sel & 0xfc);
763 ret = reg_write(client, RJ54N1_INC_USE_SEL_H, inc_sel >> 8);
765 if (!rj54n1->auto_wb) {
766 /* Auto white balance window */
767 wb_left = output_w / 16;
768 wb_right = (3 * output_w / 4 - 3) / 4;
769 wb_top = output_h / 16;
770 wb_bottom = (3 * output_h / 4 - 3) / 4;
771 wb_bit8 = ((wb_left >> 2) & 0x40) | ((wb_top >> 4) & 0x10) |
772 ((wb_right >> 6) & 4) | ((wb_bottom >> 8) & 1);
775 ret = reg_write(client, RJ54N1_BIT8_WB, wb_bit8);
777 ret = reg_write(client, RJ54N1_HCAPS_WB, wb_left);
779 ret = reg_write(client, RJ54N1_VCAPS_WB, wb_top);
781 ret = reg_write(client, RJ54N1_HCAPE_WB, wb_right);
783 ret = reg_write(client, RJ54N1_VCAPE_WB, wb_bottom);
787 peak = 12 * RJ54N1_MAX_WIDTH * (1 << 14) * resize / rj54n1->tgclk_mhz /
793 ret = reg_write(client, RJ54N1_PEAK_H,
794 ((peak_50 >> 4) & 0xf0) | (peak_60 >> 8));
796 ret = reg_write(client, RJ54N1_PEAK_50, peak_50);
798 ret = reg_write(client, RJ54N1_PEAK_60, peak_60);
800 ret = reg_write(client, RJ54N1_PEAK_DIFF, peak / 150);
804 ret = reg_write(client, RJ54N1_RESIZE_CONTROL,
805 RESIZE_HOLD_SEL | RESIZE_GO | 1);
810 /* Constant taken from manufacturer's example */
813 ret = reg_write(client, RJ54N1_RESIZE_CONTROL, RESIZE_HOLD_SEL | 1);
817 *in_w = (output_w * resize + 512) / 1024;
818 *in_h = (output_h * resize + 512) / 1024;
822 dev_dbg(&client->dev, "Scaled for %dx%d : %u = %ux%u, skip %u\n",
823 *in_w, *in_h, resize, output_w, output_h, skip);
828 static int rj54n1_set_clock(struct i2c_client *client)
830 struct rj54n1 *rj54n1 = to_rj54n1(client);
833 /* Enable external clock */
834 ret = reg_write(client, RJ54N1_RESET_STANDBY, E_EXCLK | SOFT_STDBY);
835 /* Leave stand-by. Note: use this when implementing suspend / resume */
837 ret = reg_write(client, RJ54N1_RESET_STANDBY, E_EXCLK);
840 ret = reg_write(client, RJ54N1_PLL_L, PLL_L);
842 ret = reg_write(client, RJ54N1_PLL_N, PLL_N);
846 ret = reg_write(client, RJ54N1_RATIO_TG,
847 rj54n1->clk_div.ratio_tg);
849 ret = reg_write(client, RJ54N1_RATIO_T,
850 rj54n1->clk_div.ratio_t);
852 ret = reg_write(client, RJ54N1_RATIO_R,
853 rj54n1->clk_div.ratio_r);
855 /* Enable TGCLK & RAMP */
857 ret = reg_write(client, RJ54N1_RAMP_TGCLK_EN, 3);
859 /* Disable clock output */
861 ret = reg_write(client, RJ54N1_OCLK_DSP, 0);
865 ret = reg_write(client, RJ54N1_RATIO_OP,
866 rj54n1->clk_div.ratio_op);
868 ret = reg_write(client, RJ54N1_RATIO_O,
869 rj54n1->clk_div.ratio_o);
873 ret = reg_write(client, RJ54N1_OCLK_SEL_EN, 1);
875 /* Use PLL for Timing Generator, write 2 to reserved bits */
877 ret = reg_write(client, RJ54N1_TG_BYPASS, 2);
879 /* Take sensor out of reset */
881 ret = reg_write(client, RJ54N1_RESET_STANDBY,
885 ret = reg_write(client, RJ54N1_PLL_EN, 1);
887 /* Wait for PLL to stabilise */
890 /* Enable clock to frequency divider */
892 ret = reg_write(client, RJ54N1_CLK_RST, 1);
895 ret = reg_read(client, RJ54N1_CLK_RST);
897 dev_err(&client->dev,
898 "Resetting RJ54N1CB0C clock failed: %d!\n", ret);
903 ret = reg_set(client, RJ54N1_OCLK_DSP, 1, 1);
907 ret = reg_write(client, RJ54N1_OCLK_SEL_EN, 1);
912 static int rj54n1_reg_init(struct i2c_client *client)
914 struct rj54n1 *rj54n1 = to_rj54n1(client);
915 int ret = rj54n1_set_clock(client);
918 ret = reg_write_multiple(client, bank_7, ARRAY_SIZE(bank_7));
920 ret = reg_write_multiple(client, bank_10, ARRAY_SIZE(bank_10));
922 /* Set binning divisors */
924 ret = reg_write(client, RJ54N1_SCALE_1_2_LEV, 3 | (7 << 4));
926 ret = reg_write(client, RJ54N1_SCALE_4_LEV, 0xf);
928 /* Switch to fixed resize mode */
930 ret = reg_write(client, RJ54N1_RESIZE_CONTROL,
931 RESIZE_HOLD_SEL | 1);
935 ret = reg_write(client, RJ54N1_Y_GAIN, 0x84);
938 * Mirror the image back: default is upside down and left-to-right...
939 * Set manual preview / still shot switching
942 ret = reg_write(client, RJ54N1_MIRROR_STILL_MODE, 0x27);
945 ret = reg_write_multiple(client, bank_4, ARRAY_SIZE(bank_4));
947 /* Auto exposure area */
949 ret = reg_write(client, RJ54N1_EXPOSURE_CONTROL, 0x80);
950 /* Check current auto WB config */
952 ret = reg_read(client, RJ54N1_WB_SEL_WEIGHT_I);
954 rj54n1->auto_wb = ret & 0x80;
955 ret = reg_write_multiple(client, bank_5, ARRAY_SIZE(bank_5));
958 ret = reg_write_multiple(client, bank_8, ARRAY_SIZE(bank_8));
961 ret = reg_write(client, RJ54N1_RESET_STANDBY,
962 E_EXCLK | DSP_RSTX | SEN_RSTX);
966 ret = rj54n1_commit(client);
968 /* Take DSP, TG, sensor out of reset */
970 ret = reg_write(client, RJ54N1_RESET_STANDBY,
971 E_EXCLK | DSP_RSTX | TG_RSTX | SEN_RSTX);
973 /* Start register update? Same register as 0x?FE in many bank_* sets */
975 ret = reg_write(client, RJ54N1_FWFLG, 2);
977 /* Constant taken from manufacturer's example */
983 static int rj54n1_try_fmt(struct v4l2_subdev *sd,
984 struct v4l2_mbus_framefmt *mf)
986 struct i2c_client *client = v4l2_get_subdevdata(sd);
987 struct rj54n1 *rj54n1 = to_rj54n1(client);
988 const struct rj54n1_datafmt *fmt;
989 int align = mf->code == V4L2_MBUS_FMT_SBGGR10_1X10 ||
990 mf->code == V4L2_MBUS_FMT_SBGGR10_2X8_PADHI_BE ||
991 mf->code == V4L2_MBUS_FMT_SBGGR10_2X8_PADLO_BE ||
992 mf->code == V4L2_MBUS_FMT_SBGGR10_2X8_PADHI_LE ||
993 mf->code == V4L2_MBUS_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);
1014 static int rj54n1_s_fmt(struct v4l2_subdev *sd,
1015 struct v4l2_mbus_framefmt *mf)
1017 struct i2c_client *client = v4l2_get_subdevdata(sd);
1018 struct rj54n1 *rj54n1 = to_rj54n1(client);
1019 const struct rj54n1_datafmt *fmt;
1020 int output_w, output_h, max_w, max_h,
1021 input_w = rj54n1->rect.width, input_h = rj54n1->rect.height;
1025 * The host driver can call us without .try_fmt(), so, we have to take
1028 rj54n1_try_fmt(sd, mf);
1031 * Verify if the sensor has just been powered on. TODO: replace this
1032 * with proper PM, when a suitable API is available.
1034 ret = reg_read(client, RJ54N1_RESET_STANDBY);
1038 if (!(ret & E_EXCLK)) {
1039 ret = rj54n1_reg_init(client);
1044 dev_dbg(&client->dev, "%s: code = %d, width = %u, height = %u\n",
1045 __func__, mf->code, mf->width, mf->height);
1047 /* RA_SEL_UL is only relevant for raw modes, ignored otherwise. */
1049 case V4L2_MBUS_FMT_YUYV8_2X8:
1050 ret = reg_write(client, RJ54N1_OUT_SEL, 0);
1052 ret = reg_set(client, RJ54N1_BYTE_SWAP, 8, 8);
1054 case V4L2_MBUS_FMT_YVYU8_2X8:
1055 ret = reg_write(client, RJ54N1_OUT_SEL, 0);
1057 ret = reg_set(client, RJ54N1_BYTE_SWAP, 0, 8);
1059 case V4L2_MBUS_FMT_RGB565_2X8_LE:
1060 ret = reg_write(client, RJ54N1_OUT_SEL, 0x11);
1062 ret = reg_set(client, RJ54N1_BYTE_SWAP, 8, 8);
1064 case V4L2_MBUS_FMT_RGB565_2X8_BE:
1065 ret = reg_write(client, RJ54N1_OUT_SEL, 0x11);
1067 ret = reg_set(client, RJ54N1_BYTE_SWAP, 0, 8);
1069 case V4L2_MBUS_FMT_SBGGR10_2X8_PADLO_LE:
1070 ret = reg_write(client, RJ54N1_OUT_SEL, 4);
1072 ret = reg_set(client, RJ54N1_BYTE_SWAP, 8, 8);
1074 ret = reg_write(client, RJ54N1_RA_SEL_UL, 0);
1076 case V4L2_MBUS_FMT_SBGGR10_2X8_PADHI_LE:
1077 ret = reg_write(client, RJ54N1_OUT_SEL, 4);
1079 ret = reg_set(client, RJ54N1_BYTE_SWAP, 8, 8);
1081 ret = reg_write(client, RJ54N1_RA_SEL_UL, 8);
1083 case V4L2_MBUS_FMT_SBGGR10_2X8_PADLO_BE:
1084 ret = reg_write(client, RJ54N1_OUT_SEL, 4);
1086 ret = reg_set(client, RJ54N1_BYTE_SWAP, 0, 8);
1088 ret = reg_write(client, RJ54N1_RA_SEL_UL, 0);
1090 case V4L2_MBUS_FMT_SBGGR10_2X8_PADHI_BE:
1091 ret = reg_write(client, RJ54N1_OUT_SEL, 4);
1093 ret = reg_set(client, RJ54N1_BYTE_SWAP, 0, 8);
1095 ret = reg_write(client, RJ54N1_RA_SEL_UL, 8);
1097 case V4L2_MBUS_FMT_SBGGR10_1X10:
1098 ret = reg_write(client, RJ54N1_OUT_SEL, 5);
1104 /* Special case: a raw mode with 10 bits of data per clock tick */
1106 ret = reg_set(client, RJ54N1_OCLK_SEL_EN,
1107 (mf->code == V4L2_MBUS_FMT_SBGGR10_1X10) << 1, 2);
1112 /* Supported scales 1:1 >= scale > 1:16 */
1113 max_w = mf->width * (16 * 1024 - 1) / 1024;
1114 if (input_w > max_w)
1116 max_h = mf->height * (16 * 1024 - 1) / 1024;
1117 if (input_h > max_h)
1120 output_w = mf->width;
1121 output_h = mf->height;
1123 ret = rj54n1_sensor_scale(sd, &input_w, &input_h, &output_w, &output_h);
1127 fmt = rj54n1_find_datafmt(mf->code, rj54n1_colour_fmts,
1128 ARRAY_SIZE(rj54n1_colour_fmts));
1131 rj54n1->resize = ret;
1132 rj54n1->rect.width = input_w;
1133 rj54n1->rect.height = input_h;
1134 rj54n1->width = output_w;
1135 rj54n1->height = output_h;
1137 mf->width = output_w;
1138 mf->height = output_h;
1139 mf->field = V4L2_FIELD_NONE;
1140 mf->colorspace = fmt->colorspace;
1145 static int rj54n1_g_chip_ident(struct v4l2_subdev *sd,
1146 struct v4l2_dbg_chip_ident *id)
1148 struct i2c_client *client = v4l2_get_subdevdata(sd);
1150 if (id->match.type != V4L2_CHIP_MATCH_I2C_ADDR)
1153 if (id->match.addr != client->addr)
1156 id->ident = V4L2_IDENT_RJ54N1CB0C;
1162 #ifdef CONFIG_VIDEO_ADV_DEBUG
1163 static int rj54n1_g_register(struct v4l2_subdev *sd,
1164 struct v4l2_dbg_register *reg)
1166 struct i2c_client *client = v4l2_get_subdevdata(sd);
1168 if (reg->match.type != V4L2_CHIP_MATCH_I2C_ADDR ||
1169 reg->reg < 0x400 || reg->reg > 0x1fff)
1170 /* Registers > 0x0800 are only available from Sharp support */
1173 if (reg->match.addr != client->addr)
1177 reg->val = reg_read(client, reg->reg);
1179 if (reg->val > 0xff)
1185 static int rj54n1_s_register(struct v4l2_subdev *sd,
1186 struct v4l2_dbg_register *reg)
1188 struct i2c_client *client = v4l2_get_subdevdata(sd);
1190 if (reg->match.type != V4L2_CHIP_MATCH_I2C_ADDR ||
1191 reg->reg < 0x400 || reg->reg > 0x1fff)
1192 /* Registers >= 0x0800 are only available from Sharp support */
1195 if (reg->match.addr != client->addr)
1198 if (reg_write(client, reg->reg, reg->val) < 0)
1205 static const struct v4l2_queryctrl rj54n1_controls[] = {
1207 .id = V4L2_CID_VFLIP,
1208 .type = V4L2_CTRL_TYPE_BOOLEAN,
1209 .name = "Flip Vertically",
1215 .id = V4L2_CID_HFLIP,
1216 .type = V4L2_CTRL_TYPE_BOOLEAN,
1217 .name = "Flip Horizontally",
1223 .id = V4L2_CID_GAIN,
1224 .type = V4L2_CTRL_TYPE_INTEGER,
1229 .default_value = 66,
1230 .flags = V4L2_CTRL_FLAG_SLIDER,
1232 .id = V4L2_CID_AUTO_WHITE_BALANCE,
1233 .type = V4L2_CTRL_TYPE_BOOLEAN,
1234 .name = "Auto white balance",
1242 static struct soc_camera_ops rj54n1_ops = {
1243 .set_bus_param = rj54n1_set_bus_param,
1244 .query_bus_param = rj54n1_query_bus_param,
1245 .controls = rj54n1_controls,
1246 .num_controls = ARRAY_SIZE(rj54n1_controls),
1249 static int rj54n1_g_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
1251 struct i2c_client *client = v4l2_get_subdevdata(sd);
1252 struct rj54n1 *rj54n1 = to_rj54n1(client);
1256 case V4L2_CID_VFLIP:
1257 data = reg_read(client, RJ54N1_MIRROR_STILL_MODE);
1260 ctrl->value = !(data & 1);
1262 case V4L2_CID_HFLIP:
1263 data = reg_read(client, RJ54N1_MIRROR_STILL_MODE);
1266 ctrl->value = !(data & 2);
1269 data = reg_read(client, RJ54N1_Y_GAIN);
1273 ctrl->value = data / 2;
1275 case V4L2_CID_AUTO_WHITE_BALANCE:
1276 ctrl->value = rj54n1->auto_wb;
1283 static int rj54n1_s_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
1286 struct i2c_client *client = v4l2_get_subdevdata(sd);
1287 struct rj54n1 *rj54n1 = to_rj54n1(client);
1288 const struct v4l2_queryctrl *qctrl;
1290 qctrl = soc_camera_find_qctrl(&rj54n1_ops, ctrl->id);
1295 case V4L2_CID_VFLIP:
1297 data = reg_set(client, RJ54N1_MIRROR_STILL_MODE, 0, 1);
1299 data = reg_set(client, RJ54N1_MIRROR_STILL_MODE, 1, 1);
1303 case V4L2_CID_HFLIP:
1305 data = reg_set(client, RJ54N1_MIRROR_STILL_MODE, 0, 2);
1307 data = reg_set(client, RJ54N1_MIRROR_STILL_MODE, 2, 2);
1312 if (ctrl->value > qctrl->maximum ||
1313 ctrl->value < qctrl->minimum)
1315 else if (reg_write(client, RJ54N1_Y_GAIN, ctrl->value * 2) < 0)
1318 case V4L2_CID_AUTO_WHITE_BALANCE:
1319 /* Auto WB area - whole image */
1320 if (reg_set(client, RJ54N1_WB_SEL_WEIGHT_I, ctrl->value << 7,
1323 rj54n1->auto_wb = ctrl->value;
1330 static struct v4l2_subdev_core_ops rj54n1_subdev_core_ops = {
1331 .g_ctrl = rj54n1_g_ctrl,
1332 .s_ctrl = rj54n1_s_ctrl,
1333 .g_chip_ident = rj54n1_g_chip_ident,
1334 #ifdef CONFIG_VIDEO_ADV_DEBUG
1335 .g_register = rj54n1_g_register,
1336 .s_register = rj54n1_s_register,
1340 static struct v4l2_subdev_video_ops rj54n1_subdev_video_ops = {
1341 .s_stream = rj54n1_s_stream,
1342 .s_mbus_fmt = rj54n1_s_fmt,
1343 .g_mbus_fmt = rj54n1_g_fmt,
1344 .try_mbus_fmt = rj54n1_try_fmt,
1345 .enum_mbus_fmt = rj54n1_enum_fmt,
1346 .g_crop = rj54n1_g_crop,
1347 .s_crop = rj54n1_s_crop,
1348 .cropcap = rj54n1_cropcap,
1351 static struct v4l2_subdev_ops rj54n1_subdev_ops = {
1352 .core = &rj54n1_subdev_core_ops,
1353 .video = &rj54n1_subdev_video_ops,
1357 * Interface active, can use i2c. If it fails, it can indeed mean, that
1358 * this wasn't our capture interface, so, we wait for the right one
1360 static int rj54n1_video_probe(struct soc_camera_device *icd,
1361 struct i2c_client *client,
1362 struct rj54n1_pdata *priv)
1367 /* This could be a BUG_ON() or a WARN_ON(), or remove it completely */
1368 if (!icd->dev.parent ||
1369 to_soc_camera_host(icd->dev.parent)->nr != icd->iface)
1372 /* Read out the chip version register */
1373 data1 = reg_read(client, RJ54N1_DEV_CODE);
1374 data2 = reg_read(client, RJ54N1_DEV_CODE2);
1376 if (data1 != 0x51 || data2 != 0x10) {
1378 dev_info(&client->dev, "No RJ54N1CB0C found, read 0x%x:0x%x\n",
1383 /* Configure IOCTL polarity from the platform data: 0 or 1 << 7. */
1384 ret = reg_write(client, RJ54N1_IOC, priv->ioctl_high << 7);
1388 dev_info(&client->dev, "Detected a RJ54N1CB0C chip ID 0x%x:0x%x\n",
1395 static int rj54n1_probe(struct i2c_client *client,
1396 const struct i2c_device_id *did)
1398 struct rj54n1 *rj54n1;
1399 struct soc_camera_device *icd = client->dev.platform_data;
1400 struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
1401 struct soc_camera_link *icl;
1402 struct rj54n1_pdata *rj54n1_priv;
1406 dev_err(&client->dev, "RJ54N1CB0C: missing soc-camera data!\n");
1410 icl = to_soc_camera_link(icd);
1411 if (!icl || !icl->priv) {
1412 dev_err(&client->dev, "RJ54N1CB0C: missing platform data!\n");
1416 rj54n1_priv = icl->priv;
1418 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
1419 dev_warn(&adapter->dev,
1420 "I2C-Adapter doesn't support I2C_FUNC_SMBUS_BYTE\n");
1424 rj54n1 = kzalloc(sizeof(struct rj54n1), GFP_KERNEL);
1428 v4l2_i2c_subdev_init(&rj54n1->subdev, client, &rj54n1_subdev_ops);
1430 icd->ops = &rj54n1_ops;
1432 rj54n1->clk_div = clk_div;
1433 rj54n1->rect.left = RJ54N1_COLUMN_SKIP;
1434 rj54n1->rect.top = RJ54N1_ROW_SKIP;
1435 rj54n1->rect.width = RJ54N1_MAX_WIDTH;
1436 rj54n1->rect.height = RJ54N1_MAX_HEIGHT;
1437 rj54n1->width = RJ54N1_MAX_WIDTH;
1438 rj54n1->height = RJ54N1_MAX_HEIGHT;
1439 rj54n1->fmt = &rj54n1_colour_fmts[0];
1440 rj54n1->resize = 1024;
1441 rj54n1->tgclk_mhz = (rj54n1_priv->mclk_freq / PLL_L * PLL_N) /
1442 (clk_div.ratio_tg + 1) / (clk_div.ratio_t + 1);
1444 ret = rj54n1_video_probe(icd, client, rj54n1_priv);
1454 static int rj54n1_remove(struct i2c_client *client)
1456 struct rj54n1 *rj54n1 = to_rj54n1(client);
1457 struct soc_camera_device *icd = client->dev.platform_data;
1458 struct soc_camera_link *icl = to_soc_camera_link(icd);
1468 static const struct i2c_device_id rj54n1_id[] = {
1469 { "rj54n1cb0c", 0 },
1472 MODULE_DEVICE_TABLE(i2c, rj54n1_id);
1474 static struct i2c_driver rj54n1_i2c_driver = {
1476 .name = "rj54n1cb0c",
1478 .probe = rj54n1_probe,
1479 .remove = rj54n1_remove,
1480 .id_table = rj54n1_id,
1483 static int __init rj54n1_mod_init(void)
1485 return i2c_add_driver(&rj54n1_i2c_driver);
1488 static void __exit rj54n1_mod_exit(void)
1490 i2c_del_driver(&rj54n1_i2c_driver);
1493 module_init(rj54n1_mod_init);
1494 module_exit(rj54n1_mod_exit);
1496 MODULE_DESCRIPTION("Sharp RJ54N1CB0C Camera driver");
1497 MODULE_AUTHOR("Guennadi Liakhovetski <g.liakhovetski@gmx.de>");
1498 MODULE_LICENSE("GPL v2");