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_rect(struct i2c_client *client,
503 u16 reg_x, u16 reg_y, u16 reg_xy,
504 u32 width, u32 height)
508 ret = reg_write(client, reg_xy,
509 ((width >> 4) & 0x70) |
510 ((height >> 8) & 7));
513 ret = reg_write(client, reg_x, width & 0xff);
515 ret = reg_write(client, reg_y, height & 0xff);
521 * Some commands, specifically certain initialisation sequences, require
522 * a commit operation.
524 static int rj54n1_commit(struct i2c_client *client)
526 int ret = reg_write(client, RJ54N1_INIT_START, 1);
529 ret = reg_write(client, RJ54N1_INIT_START, 0);
533 static int rj54n1_sensor_scale(struct v4l2_subdev *sd, s32 *in_w, s32 *in_h,
534 s32 *out_w, s32 *out_h);
536 static int rj54n1_s_crop(struct v4l2_subdev *sd, struct v4l2_crop *a)
538 struct i2c_client *client = v4l2_get_subdevdata(sd);
539 struct rj54n1 *rj54n1 = to_rj54n1(client);
540 struct v4l2_rect *rect = &a->c;
541 int dummy = 0, output_w, output_h,
542 input_w = rect->width, input_h = rect->height;
545 /* arbitrary minimum width and height, edges unimportant */
546 soc_camera_limit_side(&dummy, &input_w,
547 RJ54N1_COLUMN_SKIP, 8, RJ54N1_MAX_WIDTH);
549 soc_camera_limit_side(&dummy, &input_h,
550 RJ54N1_ROW_SKIP, 8, RJ54N1_MAX_HEIGHT);
552 output_w = (input_w * 1024 + rj54n1->resize / 2) / rj54n1->resize;
553 output_h = (input_h * 1024 + rj54n1->resize / 2) / rj54n1->resize;
555 dev_dbg(&client->dev, "Scaling for %dx%d : %u = %dx%d\n",
556 input_w, input_h, rj54n1->resize, output_w, output_h);
558 ret = rj54n1_sensor_scale(sd, &input_w, &input_h, &output_w, &output_h);
562 rj54n1->width = output_w;
563 rj54n1->height = output_h;
564 rj54n1->resize = ret;
565 rj54n1->rect.width = input_w;
566 rj54n1->rect.height = input_h;
571 static int rj54n1_g_crop(struct v4l2_subdev *sd, struct v4l2_crop *a)
573 struct i2c_client *client = v4l2_get_subdevdata(sd);
574 struct rj54n1 *rj54n1 = to_rj54n1(client);
577 a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
582 static int rj54n1_cropcap(struct v4l2_subdev *sd, struct v4l2_cropcap *a)
584 a->bounds.left = RJ54N1_COLUMN_SKIP;
585 a->bounds.top = RJ54N1_ROW_SKIP;
586 a->bounds.width = RJ54N1_MAX_WIDTH;
587 a->bounds.height = RJ54N1_MAX_HEIGHT;
588 a->defrect = a->bounds;
589 a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
590 a->pixelaspect.numerator = 1;
591 a->pixelaspect.denominator = 1;
596 static int rj54n1_g_fmt(struct v4l2_subdev *sd,
597 struct v4l2_mbus_framefmt *mf)
599 struct i2c_client *client = v4l2_get_subdevdata(sd);
600 struct rj54n1 *rj54n1 = to_rj54n1(client);
602 mf->code = rj54n1->fmt->code;
603 mf->colorspace = rj54n1->fmt->colorspace;
604 mf->field = V4L2_FIELD_NONE;
605 mf->width = rj54n1->width;
606 mf->height = rj54n1->height;
612 * The actual geometry configuration routine. It scales the input window into
613 * the output one, updates the window sizes and returns an error or the resize
614 * coefficient on success. Note: we only use the "Fixed Scaling" on this camera.
616 static int rj54n1_sensor_scale(struct v4l2_subdev *sd, s32 *in_w, s32 *in_h,
617 s32 *out_w, s32 *out_h)
619 struct i2c_client *client = v4l2_get_subdevdata(sd);
620 struct rj54n1 *rj54n1 = to_rj54n1(client);
621 unsigned int skip, resize, input_w = *in_w, input_h = *in_h,
622 output_w = *out_w, output_h = *out_h;
623 u16 inc_sel, wb_bit8, wb_left, wb_right, wb_top, wb_bottom;
624 unsigned int peak, peak_50, peak_60;
628 * We have a problem with crops, where the window is larger than 512x384
629 * and output window is larger than a half of the input one. In this
630 * case we have to either reduce the input window to equal or below
631 * 512x384 or the output window to equal or below 1/2 of the input.
633 if (output_w > max(512U, input_w / 2)) {
634 if (2 * output_w > RJ54N1_MAX_WIDTH) {
635 input_w = RJ54N1_MAX_WIDTH;
636 output_w = RJ54N1_MAX_WIDTH / 2;
638 input_w = output_w * 2;
641 dev_dbg(&client->dev, "Adjusted output width: in %u, out %u\n",
645 if (output_h > max(384U, input_h / 2)) {
646 if (2 * output_h > RJ54N1_MAX_HEIGHT) {
647 input_h = RJ54N1_MAX_HEIGHT;
648 output_h = RJ54N1_MAX_HEIGHT / 2;
650 input_h = output_h * 2;
653 dev_dbg(&client->dev, "Adjusted output height: in %u, out %u\n",
657 /* Idea: use the read mode for snapshots, handle separate geometries */
658 ret = rj54n1_set_rect(client, RJ54N1_X_OUTPUT_SIZE_S_L,
659 RJ54N1_Y_OUTPUT_SIZE_S_L,
660 RJ54N1_XY_OUTPUT_SIZE_S_H, output_w, output_h);
662 ret = rj54n1_set_rect(client, RJ54N1_X_OUTPUT_SIZE_P_L,
663 RJ54N1_Y_OUTPUT_SIZE_P_L,
664 RJ54N1_XY_OUTPUT_SIZE_P_H, output_w, output_h);
669 if (output_w > input_w && output_h > input_h) {
675 unsigned int resize_x, resize_y;
676 resize_x = (input_w * 1024 + output_w / 2) / output_w;
677 resize_y = (input_h * 1024 + output_h / 2) / output_h;
679 /* We want max(resize_x, resize_y), check if it still fits */
680 if (resize_x > resize_y &&
681 (output_h * resize_x + 512) / 1024 > RJ54N1_MAX_HEIGHT)
682 resize = (RJ54N1_MAX_HEIGHT * 1024 + output_h / 2) /
684 else if (resize_y > resize_x &&
685 (output_w * resize_y + 512) / 1024 > RJ54N1_MAX_WIDTH)
686 resize = (RJ54N1_MAX_WIDTH * 1024 + output_w / 2) /
689 resize = max(resize_x, resize_y);
691 /* Prohibited value ranges */
702 case 16320 ... 16384:
708 ret = reg_write(client, RJ54N1_RESIZE_HOLD_L, resize & 0xff);
710 ret = reg_write(client, RJ54N1_RESIZE_HOLD_H, resize >> 8);
716 * Configure a skipping bitmask. The sensor will select a skipping value
717 * among set bits automatically. This is very unclear in the datasheet
718 * too. I was told, in this register one enables all skipping values,
719 * that are required for a specific resize, and the camera selects
720 * automatically, which ones to use. But it is unclear how to identify,
721 * which cropping values are needed. Secondly, why don't we just set all
722 * bits and let the camera choose? Would it increase processing time and
723 * reduce the framerate? Using 0xfffc for INC_USE_SEL doesn't seem to
724 * improve the image quality or stability for larger frames (see comment
725 * above), but I didn't check the framerate.
727 skip = min(resize / 1024, 15U);
733 else if (resize & 1023 && skip < 15)
734 inc_sel |= 1 << (skip + 1);
736 ret = reg_write(client, RJ54N1_INC_USE_SEL_L, inc_sel & 0xfc);
738 ret = reg_write(client, RJ54N1_INC_USE_SEL_H, inc_sel >> 8);
740 if (!rj54n1->auto_wb) {
741 /* Auto white balance window */
742 wb_left = output_w / 16;
743 wb_right = (3 * output_w / 4 - 3) / 4;
744 wb_top = output_h / 16;
745 wb_bottom = (3 * output_h / 4 - 3) / 4;
746 wb_bit8 = ((wb_left >> 2) & 0x40) | ((wb_top >> 4) & 0x10) |
747 ((wb_right >> 6) & 4) | ((wb_bottom >> 8) & 1);
750 ret = reg_write(client, RJ54N1_BIT8_WB, wb_bit8);
752 ret = reg_write(client, RJ54N1_HCAPS_WB, wb_left);
754 ret = reg_write(client, RJ54N1_VCAPS_WB, wb_top);
756 ret = reg_write(client, RJ54N1_HCAPE_WB, wb_right);
758 ret = reg_write(client, RJ54N1_VCAPE_WB, wb_bottom);
762 peak = 12 * RJ54N1_MAX_WIDTH * (1 << 14) * resize / rj54n1->tgclk_mhz /
768 ret = reg_write(client, RJ54N1_PEAK_H,
769 ((peak_50 >> 4) & 0xf0) | (peak_60 >> 8));
771 ret = reg_write(client, RJ54N1_PEAK_50, peak_50);
773 ret = reg_write(client, RJ54N1_PEAK_60, peak_60);
775 ret = reg_write(client, RJ54N1_PEAK_DIFF, peak / 150);
779 ret = reg_write(client, RJ54N1_RESIZE_CONTROL,
780 RESIZE_HOLD_SEL | RESIZE_GO | 1);
785 /* Constant taken from manufacturer's example */
788 ret = reg_write(client, RJ54N1_RESIZE_CONTROL, RESIZE_HOLD_SEL | 1);
792 *in_w = (output_w * resize + 512) / 1024;
793 *in_h = (output_h * resize + 512) / 1024;
797 dev_dbg(&client->dev, "Scaled for %dx%d : %u = %ux%u, skip %u\n",
798 *in_w, *in_h, resize, output_w, output_h, skip);
803 static int rj54n1_set_clock(struct i2c_client *client)
805 struct rj54n1 *rj54n1 = to_rj54n1(client);
808 /* Enable external clock */
809 ret = reg_write(client, RJ54N1_RESET_STANDBY, E_EXCLK | SOFT_STDBY);
810 /* Leave stand-by. Note: use this when implementing suspend / resume */
812 ret = reg_write(client, RJ54N1_RESET_STANDBY, E_EXCLK);
815 ret = reg_write(client, RJ54N1_PLL_L, PLL_L);
817 ret = reg_write(client, RJ54N1_PLL_N, PLL_N);
821 ret = reg_write(client, RJ54N1_RATIO_TG,
822 rj54n1->clk_div.ratio_tg);
824 ret = reg_write(client, RJ54N1_RATIO_T,
825 rj54n1->clk_div.ratio_t);
827 ret = reg_write(client, RJ54N1_RATIO_R,
828 rj54n1->clk_div.ratio_r);
830 /* Enable TGCLK & RAMP */
832 ret = reg_write(client, RJ54N1_RAMP_TGCLK_EN, 3);
834 /* Disable clock output */
836 ret = reg_write(client, RJ54N1_OCLK_DSP, 0);
840 ret = reg_write(client, RJ54N1_RATIO_OP,
841 rj54n1->clk_div.ratio_op);
843 ret = reg_write(client, RJ54N1_RATIO_O,
844 rj54n1->clk_div.ratio_o);
848 ret = reg_write(client, RJ54N1_OCLK_SEL_EN, 1);
850 /* Use PLL for Timing Generator, write 2 to reserved bits */
852 ret = reg_write(client, RJ54N1_TG_BYPASS, 2);
854 /* Take sensor out of reset */
856 ret = reg_write(client, RJ54N1_RESET_STANDBY,
860 ret = reg_write(client, RJ54N1_PLL_EN, 1);
862 /* Wait for PLL to stabilise */
865 /* Enable clock to frequency divider */
867 ret = reg_write(client, RJ54N1_CLK_RST, 1);
870 ret = reg_read(client, RJ54N1_CLK_RST);
872 dev_err(&client->dev,
873 "Resetting RJ54N1CB0C clock failed: %d!\n", ret);
878 ret = reg_set(client, RJ54N1_OCLK_DSP, 1, 1);
882 ret = reg_write(client, RJ54N1_OCLK_SEL_EN, 1);
887 static int rj54n1_reg_init(struct i2c_client *client)
889 struct rj54n1 *rj54n1 = to_rj54n1(client);
890 int ret = rj54n1_set_clock(client);
893 ret = reg_write_multiple(client, bank_7, ARRAY_SIZE(bank_7));
895 ret = reg_write_multiple(client, bank_10, ARRAY_SIZE(bank_10));
897 /* Set binning divisors */
899 ret = reg_write(client, RJ54N1_SCALE_1_2_LEV, 3 | (7 << 4));
901 ret = reg_write(client, RJ54N1_SCALE_4_LEV, 0xf);
903 /* Switch to fixed resize mode */
905 ret = reg_write(client, RJ54N1_RESIZE_CONTROL,
906 RESIZE_HOLD_SEL | 1);
910 ret = reg_write(client, RJ54N1_Y_GAIN, 0x84);
913 * Mirror the image back: default is upside down and left-to-right...
914 * Set manual preview / still shot switching
917 ret = reg_write(client, RJ54N1_MIRROR_STILL_MODE, 0x27);
920 ret = reg_write_multiple(client, bank_4, ARRAY_SIZE(bank_4));
922 /* Auto exposure area */
924 ret = reg_write(client, RJ54N1_EXPOSURE_CONTROL, 0x80);
925 /* Check current auto WB config */
927 ret = reg_read(client, RJ54N1_WB_SEL_WEIGHT_I);
929 rj54n1->auto_wb = ret & 0x80;
930 ret = reg_write_multiple(client, bank_5, ARRAY_SIZE(bank_5));
933 ret = reg_write_multiple(client, bank_8, ARRAY_SIZE(bank_8));
936 ret = reg_write(client, RJ54N1_RESET_STANDBY,
937 E_EXCLK | DSP_RSTX | SEN_RSTX);
941 ret = rj54n1_commit(client);
943 /* Take DSP, TG, sensor out of reset */
945 ret = reg_write(client, RJ54N1_RESET_STANDBY,
946 E_EXCLK | DSP_RSTX | TG_RSTX | SEN_RSTX);
948 /* Start register update? Same register as 0x?FE in many bank_* sets */
950 ret = reg_write(client, RJ54N1_FWFLG, 2);
952 /* Constant taken from manufacturer's example */
958 static int rj54n1_try_fmt(struct v4l2_subdev *sd,
959 struct v4l2_mbus_framefmt *mf)
961 struct i2c_client *client = v4l2_get_subdevdata(sd);
962 struct rj54n1 *rj54n1 = to_rj54n1(client);
963 const struct rj54n1_datafmt *fmt;
964 int align = mf->code == V4L2_MBUS_FMT_SBGGR10_1X10 ||
965 mf->code == V4L2_MBUS_FMT_SBGGR10_2X8_PADHI_BE ||
966 mf->code == V4L2_MBUS_FMT_SBGGR10_2X8_PADLO_BE ||
967 mf->code == V4L2_MBUS_FMT_SBGGR10_2X8_PADHI_LE ||
968 mf->code == V4L2_MBUS_FMT_SBGGR10_2X8_PADLO_LE;
970 dev_dbg(&client->dev, "%s: code = %d, width = %u, height = %u\n",
971 __func__, mf->code, mf->width, mf->height);
973 fmt = rj54n1_find_datafmt(mf->code, rj54n1_colour_fmts,
974 ARRAY_SIZE(rj54n1_colour_fmts));
977 mf->code = fmt->code;
980 mf->field = V4L2_FIELD_NONE;
981 mf->colorspace = fmt->colorspace;
983 v4l_bound_align_image(&mf->width, 112, RJ54N1_MAX_WIDTH, align,
984 &mf->height, 84, RJ54N1_MAX_HEIGHT, align, 0);
989 static int rj54n1_s_fmt(struct v4l2_subdev *sd,
990 struct v4l2_mbus_framefmt *mf)
992 struct i2c_client *client = v4l2_get_subdevdata(sd);
993 struct rj54n1 *rj54n1 = to_rj54n1(client);
994 const struct rj54n1_datafmt *fmt;
995 int output_w, output_h, max_w, max_h,
996 input_w = rj54n1->rect.width, input_h = rj54n1->rect.height;
1000 * The host driver can call us without .try_fmt(), so, we have to take
1003 rj54n1_try_fmt(sd, mf);
1006 * Verify if the sensor has just been powered on. TODO: replace this
1007 * with proper PM, when a suitable API is available.
1009 ret = reg_read(client, RJ54N1_RESET_STANDBY);
1013 if (!(ret & E_EXCLK)) {
1014 ret = rj54n1_reg_init(client);
1019 dev_dbg(&client->dev, "%s: code = %d, width = %u, height = %u\n",
1020 __func__, mf->code, mf->width, mf->height);
1022 /* RA_SEL_UL is only relevant for raw modes, ignored otherwise. */
1024 case V4L2_MBUS_FMT_YUYV8_2X8:
1025 ret = reg_write(client, RJ54N1_OUT_SEL, 0);
1027 ret = reg_set(client, RJ54N1_BYTE_SWAP, 8, 8);
1029 case V4L2_MBUS_FMT_YVYU8_2X8:
1030 ret = reg_write(client, RJ54N1_OUT_SEL, 0);
1032 ret = reg_set(client, RJ54N1_BYTE_SWAP, 0, 8);
1034 case V4L2_MBUS_FMT_RGB565_2X8_LE:
1035 ret = reg_write(client, RJ54N1_OUT_SEL, 0x11);
1037 ret = reg_set(client, RJ54N1_BYTE_SWAP, 8, 8);
1039 case V4L2_MBUS_FMT_RGB565_2X8_BE:
1040 ret = reg_write(client, RJ54N1_OUT_SEL, 0x11);
1042 ret = reg_set(client, RJ54N1_BYTE_SWAP, 0, 8);
1044 case V4L2_MBUS_FMT_SBGGR10_2X8_PADLO_LE:
1045 ret = reg_write(client, RJ54N1_OUT_SEL, 4);
1047 ret = reg_set(client, RJ54N1_BYTE_SWAP, 8, 8);
1049 ret = reg_write(client, RJ54N1_RA_SEL_UL, 0);
1051 case V4L2_MBUS_FMT_SBGGR10_2X8_PADHI_LE:
1052 ret = reg_write(client, RJ54N1_OUT_SEL, 4);
1054 ret = reg_set(client, RJ54N1_BYTE_SWAP, 8, 8);
1056 ret = reg_write(client, RJ54N1_RA_SEL_UL, 8);
1058 case V4L2_MBUS_FMT_SBGGR10_2X8_PADLO_BE:
1059 ret = reg_write(client, RJ54N1_OUT_SEL, 4);
1061 ret = reg_set(client, RJ54N1_BYTE_SWAP, 0, 8);
1063 ret = reg_write(client, RJ54N1_RA_SEL_UL, 0);
1065 case V4L2_MBUS_FMT_SBGGR10_2X8_PADHI_BE:
1066 ret = reg_write(client, RJ54N1_OUT_SEL, 4);
1068 ret = reg_set(client, RJ54N1_BYTE_SWAP, 0, 8);
1070 ret = reg_write(client, RJ54N1_RA_SEL_UL, 8);
1072 case V4L2_MBUS_FMT_SBGGR10_1X10:
1073 ret = reg_write(client, RJ54N1_OUT_SEL, 5);
1079 /* Special case: a raw mode with 10 bits of data per clock tick */
1081 ret = reg_set(client, RJ54N1_OCLK_SEL_EN,
1082 (mf->code == V4L2_MBUS_FMT_SBGGR10_1X10) << 1, 2);
1087 /* Supported scales 1:1 >= scale > 1:16 */
1088 max_w = mf->width * (16 * 1024 - 1) / 1024;
1089 if (input_w > max_w)
1091 max_h = mf->height * (16 * 1024 - 1) / 1024;
1092 if (input_h > max_h)
1095 output_w = mf->width;
1096 output_h = mf->height;
1098 ret = rj54n1_sensor_scale(sd, &input_w, &input_h, &output_w, &output_h);
1102 fmt = rj54n1_find_datafmt(mf->code, rj54n1_colour_fmts,
1103 ARRAY_SIZE(rj54n1_colour_fmts));
1106 rj54n1->resize = ret;
1107 rj54n1->rect.width = input_w;
1108 rj54n1->rect.height = input_h;
1109 rj54n1->width = output_w;
1110 rj54n1->height = output_h;
1112 mf->width = output_w;
1113 mf->height = output_h;
1114 mf->field = V4L2_FIELD_NONE;
1115 mf->colorspace = fmt->colorspace;
1120 static int rj54n1_g_chip_ident(struct v4l2_subdev *sd,
1121 struct v4l2_dbg_chip_ident *id)
1123 struct i2c_client *client = v4l2_get_subdevdata(sd);
1125 if (id->match.type != V4L2_CHIP_MATCH_I2C_ADDR)
1128 if (id->match.addr != client->addr)
1131 id->ident = V4L2_IDENT_RJ54N1CB0C;
1137 #ifdef CONFIG_VIDEO_ADV_DEBUG
1138 static int rj54n1_g_register(struct v4l2_subdev *sd,
1139 struct v4l2_dbg_register *reg)
1141 struct i2c_client *client = v4l2_get_subdevdata(sd);
1143 if (reg->match.type != V4L2_CHIP_MATCH_I2C_ADDR ||
1144 reg->reg < 0x400 || reg->reg > 0x1fff)
1145 /* Registers > 0x0800 are only available from Sharp support */
1148 if (reg->match.addr != client->addr)
1152 reg->val = reg_read(client, reg->reg);
1154 if (reg->val > 0xff)
1160 static int rj54n1_s_register(struct v4l2_subdev *sd,
1161 struct v4l2_dbg_register *reg)
1163 struct i2c_client *client = v4l2_get_subdevdata(sd);
1165 if (reg->match.type != V4L2_CHIP_MATCH_I2C_ADDR ||
1166 reg->reg < 0x400 || reg->reg > 0x1fff)
1167 /* Registers >= 0x0800 are only available from Sharp support */
1170 if (reg->match.addr != client->addr)
1173 if (reg_write(client, reg->reg, reg->val) < 0)
1180 static const struct v4l2_queryctrl rj54n1_controls[] = {
1182 .id = V4L2_CID_VFLIP,
1183 .type = V4L2_CTRL_TYPE_BOOLEAN,
1184 .name = "Flip Vertically",
1190 .id = V4L2_CID_HFLIP,
1191 .type = V4L2_CTRL_TYPE_BOOLEAN,
1192 .name = "Flip Horizontally",
1198 .id = V4L2_CID_GAIN,
1199 .type = V4L2_CTRL_TYPE_INTEGER,
1204 .default_value = 66,
1205 .flags = V4L2_CTRL_FLAG_SLIDER,
1207 .id = V4L2_CID_AUTO_WHITE_BALANCE,
1208 .type = V4L2_CTRL_TYPE_BOOLEAN,
1209 .name = "Auto white balance",
1217 static struct soc_camera_ops rj54n1_ops = {
1218 .controls = rj54n1_controls,
1219 .num_controls = ARRAY_SIZE(rj54n1_controls),
1222 static int rj54n1_g_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
1224 struct i2c_client *client = v4l2_get_subdevdata(sd);
1225 struct rj54n1 *rj54n1 = to_rj54n1(client);
1229 case V4L2_CID_VFLIP:
1230 data = reg_read(client, RJ54N1_MIRROR_STILL_MODE);
1233 ctrl->value = !(data & 1);
1235 case V4L2_CID_HFLIP:
1236 data = reg_read(client, RJ54N1_MIRROR_STILL_MODE);
1239 ctrl->value = !(data & 2);
1242 data = reg_read(client, RJ54N1_Y_GAIN);
1246 ctrl->value = data / 2;
1248 case V4L2_CID_AUTO_WHITE_BALANCE:
1249 ctrl->value = rj54n1->auto_wb;
1256 static int rj54n1_s_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
1259 struct i2c_client *client = v4l2_get_subdevdata(sd);
1260 struct rj54n1 *rj54n1 = to_rj54n1(client);
1261 const struct v4l2_queryctrl *qctrl;
1263 qctrl = soc_camera_find_qctrl(&rj54n1_ops, ctrl->id);
1268 case V4L2_CID_VFLIP:
1270 data = reg_set(client, RJ54N1_MIRROR_STILL_MODE, 0, 1);
1272 data = reg_set(client, RJ54N1_MIRROR_STILL_MODE, 1, 1);
1276 case V4L2_CID_HFLIP:
1278 data = reg_set(client, RJ54N1_MIRROR_STILL_MODE, 0, 2);
1280 data = reg_set(client, RJ54N1_MIRROR_STILL_MODE, 2, 2);
1285 if (ctrl->value > qctrl->maximum ||
1286 ctrl->value < qctrl->minimum)
1288 else if (reg_write(client, RJ54N1_Y_GAIN, ctrl->value * 2) < 0)
1291 case V4L2_CID_AUTO_WHITE_BALANCE:
1292 /* Auto WB area - whole image */
1293 if (reg_set(client, RJ54N1_WB_SEL_WEIGHT_I, ctrl->value << 7,
1296 rj54n1->auto_wb = ctrl->value;
1303 static struct v4l2_subdev_core_ops rj54n1_subdev_core_ops = {
1304 .g_ctrl = rj54n1_g_ctrl,
1305 .s_ctrl = rj54n1_s_ctrl,
1306 .g_chip_ident = rj54n1_g_chip_ident,
1307 #ifdef CONFIG_VIDEO_ADV_DEBUG
1308 .g_register = rj54n1_g_register,
1309 .s_register = rj54n1_s_register,
1313 static int rj54n1_g_mbus_config(struct v4l2_subdev *sd,
1314 struct v4l2_mbus_config *cfg)
1316 struct i2c_client *client = v4l2_get_subdevdata(sd);
1317 struct soc_camera_device *icd = client->dev.platform_data;
1318 struct soc_camera_link *icl = to_soc_camera_link(icd);
1321 V4L2_MBUS_PCLK_SAMPLE_RISING | V4L2_MBUS_PCLK_SAMPLE_FALLING |
1322 V4L2_MBUS_MASTER | V4L2_MBUS_DATA_ACTIVE_HIGH |
1323 V4L2_MBUS_HSYNC_ACTIVE_HIGH | V4L2_MBUS_VSYNC_ACTIVE_HIGH;
1324 cfg->type = V4L2_MBUS_PARALLEL;
1325 cfg->flags = soc_camera_apply_board_flags(icl, cfg);
1330 static int rj54n1_s_mbus_config(struct v4l2_subdev *sd,
1331 const struct v4l2_mbus_config *cfg)
1333 struct i2c_client *client = v4l2_get_subdevdata(sd);
1334 struct soc_camera_device *icd = client->dev.platform_data;
1335 struct soc_camera_link *icl = to_soc_camera_link(icd);
1337 /* Figures 2.5-1 to 2.5-3 - default falling pixclk edge */
1338 if (soc_camera_apply_board_flags(icl, cfg) &
1339 V4L2_MBUS_PCLK_SAMPLE_RISING)
1340 return reg_write(client, RJ54N1_OUT_SIGPO, 1 << 4);
1342 return reg_write(client, RJ54N1_OUT_SIGPO, 0);
1345 static struct v4l2_subdev_video_ops rj54n1_subdev_video_ops = {
1346 .s_stream = rj54n1_s_stream,
1347 .s_mbus_fmt = rj54n1_s_fmt,
1348 .g_mbus_fmt = rj54n1_g_fmt,
1349 .try_mbus_fmt = rj54n1_try_fmt,
1350 .enum_mbus_fmt = rj54n1_enum_fmt,
1351 .g_crop = rj54n1_g_crop,
1352 .s_crop = rj54n1_s_crop,
1353 .cropcap = rj54n1_cropcap,
1354 .g_mbus_config = rj54n1_g_mbus_config,
1355 .s_mbus_config = rj54n1_s_mbus_config,
1358 static struct v4l2_subdev_ops rj54n1_subdev_ops = {
1359 .core = &rj54n1_subdev_core_ops,
1360 .video = &rj54n1_subdev_video_ops,
1364 * Interface active, can use i2c. If it fails, it can indeed mean, that
1365 * this wasn't our capture interface, so, we wait for the right one
1367 static int rj54n1_video_probe(struct soc_camera_device *icd,
1368 struct i2c_client *client,
1369 struct rj54n1_pdata *priv)
1374 /* We must have a parent by now. And it cannot be a wrong one. */
1375 BUG_ON(!icd->parent ||
1376 to_soc_camera_host(icd->parent)->nr != icd->iface);
1378 /* Read out the chip version register */
1379 data1 = reg_read(client, RJ54N1_DEV_CODE);
1380 data2 = reg_read(client, RJ54N1_DEV_CODE2);
1382 if (data1 != 0x51 || data2 != 0x10) {
1384 dev_info(&client->dev, "No RJ54N1CB0C found, read 0x%x:0x%x\n",
1389 /* Configure IOCTL polarity from the platform data: 0 or 1 << 7. */
1390 ret = reg_write(client, RJ54N1_IOC, priv->ioctl_high << 7);
1394 dev_info(&client->dev, "Detected a RJ54N1CB0C chip ID 0x%x:0x%x\n",
1401 static int rj54n1_probe(struct i2c_client *client,
1402 const struct i2c_device_id *did)
1404 struct rj54n1 *rj54n1;
1405 struct soc_camera_device *icd = client->dev.platform_data;
1406 struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
1407 struct soc_camera_link *icl;
1408 struct rj54n1_pdata *rj54n1_priv;
1412 dev_err(&client->dev, "RJ54N1CB0C: missing soc-camera data!\n");
1416 icl = to_soc_camera_link(icd);
1417 if (!icl || !icl->priv) {
1418 dev_err(&client->dev, "RJ54N1CB0C: missing platform data!\n");
1422 rj54n1_priv = icl->priv;
1424 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
1425 dev_warn(&adapter->dev,
1426 "I2C-Adapter doesn't support I2C_FUNC_SMBUS_BYTE\n");
1430 rj54n1 = kzalloc(sizeof(struct rj54n1), GFP_KERNEL);
1434 v4l2_i2c_subdev_init(&rj54n1->subdev, client, &rj54n1_subdev_ops);
1436 icd->ops = &rj54n1_ops;
1438 rj54n1->clk_div = clk_div;
1439 rj54n1->rect.left = RJ54N1_COLUMN_SKIP;
1440 rj54n1->rect.top = RJ54N1_ROW_SKIP;
1441 rj54n1->rect.width = RJ54N1_MAX_WIDTH;
1442 rj54n1->rect.height = RJ54N1_MAX_HEIGHT;
1443 rj54n1->width = RJ54N1_MAX_WIDTH;
1444 rj54n1->height = RJ54N1_MAX_HEIGHT;
1445 rj54n1->fmt = &rj54n1_colour_fmts[0];
1446 rj54n1->resize = 1024;
1447 rj54n1->tgclk_mhz = (rj54n1_priv->mclk_freq / PLL_L * PLL_N) /
1448 (clk_div.ratio_tg + 1) / (clk_div.ratio_t + 1);
1450 ret = rj54n1_video_probe(icd, client, rj54n1_priv);
1460 static int rj54n1_remove(struct i2c_client *client)
1462 struct rj54n1 *rj54n1 = to_rj54n1(client);
1463 struct soc_camera_device *icd = client->dev.platform_data;
1464 struct soc_camera_link *icl = to_soc_camera_link(icd);
1474 static const struct i2c_device_id rj54n1_id[] = {
1475 { "rj54n1cb0c", 0 },
1478 MODULE_DEVICE_TABLE(i2c, rj54n1_id);
1480 static struct i2c_driver rj54n1_i2c_driver = {
1482 .name = "rj54n1cb0c",
1484 .probe = rj54n1_probe,
1485 .remove = rj54n1_remove,
1486 .id_table = rj54n1_id,
1489 static int __init rj54n1_mod_init(void)
1491 return i2c_add_driver(&rj54n1_i2c_driver);
1494 static void __exit rj54n1_mod_exit(void)
1496 i2c_del_driver(&rj54n1_i2c_driver);
1499 module_init(rj54n1_mod_init);
1500 module_exit(rj54n1_mod_exit);
1502 MODULE_DESCRIPTION("Sharp RJ54N1CB0C Camera driver");
1503 MODULE_AUTHOR("Guennadi Liakhovetski <g.liakhovetski@gmx.de>");
1504 MODULE_LICENSE("GPL v2");