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>
15 #include <linux/module.h>
17 #include <media/rj54n1cb0c.h>
18 #include <media/soc_camera.h>
19 #include <media/soc_mediabus.h>
20 #include <media/v4l2-subdev.h>
21 #include <media/v4l2-chip-ident.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 rj54n1_clock_div clk_div;
153 const struct rj54n1_datafmt *fmt;
154 struct v4l2_rect rect; /* Sensor window */
155 unsigned int tgclk_mhz;
157 unsigned short width; /* Output window */
158 unsigned short height;
159 unsigned short resize; /* Sensor * 1024 / resize = Output */
160 unsigned short scale;
164 struct rj54n1_reg_val {
169 static const struct rj54n1_reg_val bank_4[] = {
190 static const struct rj54n1_reg_val bank_5[] = {
211 static const struct rj54n1_reg_val bank_7[] = {
219 static const struct rj54n1_reg_val bank_8[] = {
407 static const struct rj54n1_reg_val bank_10[] = {
411 /* Clock dividers - these are default register values, divider = register + 1 */
412 static const struct rj54n1_clock_div clk_div = {
413 .ratio_tg = 3 /* default: 5 */,
414 .ratio_t = 4 /* default: 1 */,
415 .ratio_r = 4 /* default: 0 */,
416 .ratio_op = 1 /* default: 5 */,
417 .ratio_o = 9 /* default: 0 */,
420 static struct rj54n1 *to_rj54n1(const struct i2c_client *client)
422 return container_of(i2c_get_clientdata(client), struct rj54n1, subdev);
425 static int reg_read(struct i2c_client *client, const u16 reg)
427 struct rj54n1 *rj54n1 = to_rj54n1(client);
431 if (rj54n1->bank != reg >> 8) {
432 dev_dbg(&client->dev, "[0x%x] = 0x%x\n", 0xff, reg >> 8);
433 ret = i2c_smbus_write_byte_data(client, 0xff, reg >> 8);
436 rj54n1->bank = reg >> 8;
438 return i2c_smbus_read_byte_data(client, reg & 0xff);
441 static int reg_write(struct i2c_client *client, const u16 reg,
444 struct rj54n1 *rj54n1 = to_rj54n1(client);
448 if (rj54n1->bank != reg >> 8) {
449 dev_dbg(&client->dev, "[0x%x] = 0x%x\n", 0xff, reg >> 8);
450 ret = i2c_smbus_write_byte_data(client, 0xff, reg >> 8);
453 rj54n1->bank = reg >> 8;
455 dev_dbg(&client->dev, "[0x%x] = 0x%x\n", reg & 0xff, data);
456 return i2c_smbus_write_byte_data(client, reg & 0xff, data);
459 static int reg_set(struct i2c_client *client, const u16 reg,
460 const u8 data, const u8 mask)
464 ret = reg_read(client, reg);
467 return reg_write(client, reg, (ret & ~mask) | (data & mask));
470 static int reg_write_multiple(struct i2c_client *client,
471 const struct rj54n1_reg_val *rv, const int n)
475 for (i = 0; i < n; i++) {
476 ret = reg_write(client, rv->reg, rv->val);
485 static int rj54n1_enum_fmt(struct v4l2_subdev *sd, unsigned int index,
486 enum v4l2_mbus_pixelcode *code)
488 if (index >= ARRAY_SIZE(rj54n1_colour_fmts))
491 *code = rj54n1_colour_fmts[index].code;
495 static int rj54n1_s_stream(struct v4l2_subdev *sd, int enable)
497 struct i2c_client *client = v4l2_get_subdevdata(sd);
499 /* Switch between preview and still shot modes */
500 return reg_set(client, RJ54N1_STILL_CONTROL, (!enable) << 7, 0x80);
503 static int rj54n1_set_bus_param(struct soc_camera_device *icd,
506 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
507 struct i2c_client *client = v4l2_get_subdevdata(sd);
508 /* Figures 2.5-1 to 2.5-3 - default falling pixclk edge */
510 if (flags & SOCAM_PCLK_SAMPLE_RISING)
511 return reg_write(client, RJ54N1_OUT_SIGPO, 1 << 4);
513 return reg_write(client, RJ54N1_OUT_SIGPO, 0);
516 static unsigned long rj54n1_query_bus_param(struct soc_camera_device *icd)
518 struct soc_camera_link *icl = to_soc_camera_link(icd);
519 const unsigned long flags =
520 SOCAM_PCLK_SAMPLE_RISING | SOCAM_PCLK_SAMPLE_FALLING |
521 SOCAM_MASTER | SOCAM_DATAWIDTH_8 |
522 SOCAM_HSYNC_ACTIVE_HIGH | SOCAM_VSYNC_ACTIVE_HIGH |
523 SOCAM_DATA_ACTIVE_HIGH;
525 return soc_camera_apply_sensor_flags(icl, flags);
528 static int rj54n1_set_rect(struct i2c_client *client,
529 u16 reg_x, u16 reg_y, u16 reg_xy,
530 u32 width, u32 height)
534 ret = reg_write(client, reg_xy,
535 ((width >> 4) & 0x70) |
536 ((height >> 8) & 7));
539 ret = reg_write(client, reg_x, width & 0xff);
541 ret = reg_write(client, reg_y, height & 0xff);
547 * Some commands, specifically certain initialisation sequences, require
548 * a commit operation.
550 static int rj54n1_commit(struct i2c_client *client)
552 int ret = reg_write(client, RJ54N1_INIT_START, 1);
555 ret = reg_write(client, RJ54N1_INIT_START, 0);
559 static int rj54n1_sensor_scale(struct v4l2_subdev *sd, s32 *in_w, s32 *in_h,
560 s32 *out_w, s32 *out_h);
562 static int rj54n1_s_crop(struct v4l2_subdev *sd, struct v4l2_crop *a)
564 struct i2c_client *client = v4l2_get_subdevdata(sd);
565 struct rj54n1 *rj54n1 = to_rj54n1(client);
566 struct v4l2_rect *rect = &a->c;
567 int dummy = 0, output_w, output_h,
568 input_w = rect->width, input_h = rect->height;
571 /* arbitrary minimum width and height, edges unimportant */
572 soc_camera_limit_side(&dummy, &input_w,
573 RJ54N1_COLUMN_SKIP, 8, RJ54N1_MAX_WIDTH);
575 soc_camera_limit_side(&dummy, &input_h,
576 RJ54N1_ROW_SKIP, 8, RJ54N1_MAX_HEIGHT);
578 output_w = (input_w * 1024 + rj54n1->resize / 2) / rj54n1->resize;
579 output_h = (input_h * 1024 + rj54n1->resize / 2) / rj54n1->resize;
581 dev_dbg(&client->dev, "Scaling for %dx%d : %u = %dx%d\n",
582 input_w, input_h, rj54n1->resize, output_w, output_h);
584 ret = rj54n1_sensor_scale(sd, &input_w, &input_h, &output_w, &output_h);
588 rj54n1->width = output_w;
589 rj54n1->height = output_h;
590 rj54n1->resize = ret;
591 rj54n1->rect.width = input_w;
592 rj54n1->rect.height = input_h;
597 static int rj54n1_g_crop(struct v4l2_subdev *sd, struct v4l2_crop *a)
599 struct i2c_client *client = v4l2_get_subdevdata(sd);
600 struct rj54n1 *rj54n1 = to_rj54n1(client);
603 a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
608 static int rj54n1_cropcap(struct v4l2_subdev *sd, struct v4l2_cropcap *a)
610 a->bounds.left = RJ54N1_COLUMN_SKIP;
611 a->bounds.top = RJ54N1_ROW_SKIP;
612 a->bounds.width = RJ54N1_MAX_WIDTH;
613 a->bounds.height = RJ54N1_MAX_HEIGHT;
614 a->defrect = a->bounds;
615 a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
616 a->pixelaspect.numerator = 1;
617 a->pixelaspect.denominator = 1;
622 static int rj54n1_g_fmt(struct v4l2_subdev *sd,
623 struct v4l2_mbus_framefmt *mf)
625 struct i2c_client *client = v4l2_get_subdevdata(sd);
626 struct rj54n1 *rj54n1 = to_rj54n1(client);
628 mf->code = rj54n1->fmt->code;
629 mf->colorspace = rj54n1->fmt->colorspace;
630 mf->field = V4L2_FIELD_NONE;
631 mf->width = rj54n1->width;
632 mf->height = rj54n1->height;
638 * The actual geometry configuration routine. It scales the input window into
639 * the output one, updates the window sizes and returns an error or the resize
640 * coefficient on success. Note: we only use the "Fixed Scaling" on this camera.
642 static int rj54n1_sensor_scale(struct v4l2_subdev *sd, s32 *in_w, s32 *in_h,
643 s32 *out_w, s32 *out_h)
645 struct i2c_client *client = v4l2_get_subdevdata(sd);
646 struct rj54n1 *rj54n1 = to_rj54n1(client);
647 unsigned int skip, resize, input_w = *in_w, input_h = *in_h,
648 output_w = *out_w, output_h = *out_h;
649 u16 inc_sel, wb_bit8, wb_left, wb_right, wb_top, wb_bottom;
650 unsigned int peak, peak_50, peak_60;
654 * We have a problem with crops, where the window is larger than 512x384
655 * and output window is larger than a half of the input one. In this
656 * case we have to either reduce the input window to equal or below
657 * 512x384 or the output window to equal or below 1/2 of the input.
659 if (output_w > max(512U, input_w / 2)) {
660 if (2 * output_w > RJ54N1_MAX_WIDTH) {
661 input_w = RJ54N1_MAX_WIDTH;
662 output_w = RJ54N1_MAX_WIDTH / 2;
664 input_w = output_w * 2;
667 dev_dbg(&client->dev, "Adjusted output width: in %u, out %u\n",
671 if (output_h > max(384U, input_h / 2)) {
672 if (2 * output_h > RJ54N1_MAX_HEIGHT) {
673 input_h = RJ54N1_MAX_HEIGHT;
674 output_h = RJ54N1_MAX_HEIGHT / 2;
676 input_h = output_h * 2;
679 dev_dbg(&client->dev, "Adjusted output height: in %u, out %u\n",
683 /* Idea: use the read mode for snapshots, handle separate geometries */
684 ret = rj54n1_set_rect(client, RJ54N1_X_OUTPUT_SIZE_S_L,
685 RJ54N1_Y_OUTPUT_SIZE_S_L,
686 RJ54N1_XY_OUTPUT_SIZE_S_H, output_w, output_h);
688 ret = rj54n1_set_rect(client, RJ54N1_X_OUTPUT_SIZE_P_L,
689 RJ54N1_Y_OUTPUT_SIZE_P_L,
690 RJ54N1_XY_OUTPUT_SIZE_P_H, output_w, output_h);
695 if (output_w > input_w && output_h > input_h) {
701 unsigned int resize_x, resize_y;
702 resize_x = (input_w * 1024 + output_w / 2) / output_w;
703 resize_y = (input_h * 1024 + output_h / 2) / output_h;
705 /* We want max(resize_x, resize_y), check if it still fits */
706 if (resize_x > resize_y &&
707 (output_h * resize_x + 512) / 1024 > RJ54N1_MAX_HEIGHT)
708 resize = (RJ54N1_MAX_HEIGHT * 1024 + output_h / 2) /
710 else if (resize_y > resize_x &&
711 (output_w * resize_y + 512) / 1024 > RJ54N1_MAX_WIDTH)
712 resize = (RJ54N1_MAX_WIDTH * 1024 + output_w / 2) /
715 resize = max(resize_x, resize_y);
717 /* Prohibited value ranges */
728 case 16320 ... 16384:
734 ret = reg_write(client, RJ54N1_RESIZE_HOLD_L, resize & 0xff);
736 ret = reg_write(client, RJ54N1_RESIZE_HOLD_H, resize >> 8);
742 * Configure a skipping bitmask. The sensor will select a skipping value
743 * among set bits automatically. This is very unclear in the datasheet
744 * too. I was told, in this register one enables all skipping values,
745 * that are required for a specific resize, and the camera selects
746 * automatically, which ones to use. But it is unclear how to identify,
747 * which cropping values are needed. Secondly, why don't we just set all
748 * bits and let the camera choose? Would it increase processing time and
749 * reduce the framerate? Using 0xfffc for INC_USE_SEL doesn't seem to
750 * improve the image quality or stability for larger frames (see comment
751 * above), but I didn't check the framerate.
753 skip = min(resize / 1024, 15U);
759 else if (resize & 1023 && skip < 15)
760 inc_sel |= 1 << (skip + 1);
762 ret = reg_write(client, RJ54N1_INC_USE_SEL_L, inc_sel & 0xfc);
764 ret = reg_write(client, RJ54N1_INC_USE_SEL_H, inc_sel >> 8);
766 if (!rj54n1->auto_wb) {
767 /* Auto white balance window */
768 wb_left = output_w / 16;
769 wb_right = (3 * output_w / 4 - 3) / 4;
770 wb_top = output_h / 16;
771 wb_bottom = (3 * output_h / 4 - 3) / 4;
772 wb_bit8 = ((wb_left >> 2) & 0x40) | ((wb_top >> 4) & 0x10) |
773 ((wb_right >> 6) & 4) | ((wb_bottom >> 8) & 1);
776 ret = reg_write(client, RJ54N1_BIT8_WB, wb_bit8);
778 ret = reg_write(client, RJ54N1_HCAPS_WB, wb_left);
780 ret = reg_write(client, RJ54N1_VCAPS_WB, wb_top);
782 ret = reg_write(client, RJ54N1_HCAPE_WB, wb_right);
784 ret = reg_write(client, RJ54N1_VCAPE_WB, wb_bottom);
788 peak = 12 * RJ54N1_MAX_WIDTH * (1 << 14) * resize / rj54n1->tgclk_mhz /
794 ret = reg_write(client, RJ54N1_PEAK_H,
795 ((peak_50 >> 4) & 0xf0) | (peak_60 >> 8));
797 ret = reg_write(client, RJ54N1_PEAK_50, peak_50);
799 ret = reg_write(client, RJ54N1_PEAK_60, peak_60);
801 ret = reg_write(client, RJ54N1_PEAK_DIFF, peak / 150);
805 ret = reg_write(client, RJ54N1_RESIZE_CONTROL,
806 RESIZE_HOLD_SEL | RESIZE_GO | 1);
811 /* Constant taken from manufacturer's example */
814 ret = reg_write(client, RJ54N1_RESIZE_CONTROL, RESIZE_HOLD_SEL | 1);
818 *in_w = (output_w * resize + 512) / 1024;
819 *in_h = (output_h * resize + 512) / 1024;
823 dev_dbg(&client->dev, "Scaled for %dx%d : %u = %ux%u, skip %u\n",
824 *in_w, *in_h, resize, output_w, output_h, skip);
829 static int rj54n1_set_clock(struct i2c_client *client)
831 struct rj54n1 *rj54n1 = to_rj54n1(client);
834 /* Enable external clock */
835 ret = reg_write(client, RJ54N1_RESET_STANDBY, E_EXCLK | SOFT_STDBY);
836 /* Leave stand-by. Note: use this when implementing suspend / resume */
838 ret = reg_write(client, RJ54N1_RESET_STANDBY, E_EXCLK);
841 ret = reg_write(client, RJ54N1_PLL_L, PLL_L);
843 ret = reg_write(client, RJ54N1_PLL_N, PLL_N);
847 ret = reg_write(client, RJ54N1_RATIO_TG,
848 rj54n1->clk_div.ratio_tg);
850 ret = reg_write(client, RJ54N1_RATIO_T,
851 rj54n1->clk_div.ratio_t);
853 ret = reg_write(client, RJ54N1_RATIO_R,
854 rj54n1->clk_div.ratio_r);
856 /* Enable TGCLK & RAMP */
858 ret = reg_write(client, RJ54N1_RAMP_TGCLK_EN, 3);
860 /* Disable clock output */
862 ret = reg_write(client, RJ54N1_OCLK_DSP, 0);
866 ret = reg_write(client, RJ54N1_RATIO_OP,
867 rj54n1->clk_div.ratio_op);
869 ret = reg_write(client, RJ54N1_RATIO_O,
870 rj54n1->clk_div.ratio_o);
874 ret = reg_write(client, RJ54N1_OCLK_SEL_EN, 1);
876 /* Use PLL for Timing Generator, write 2 to reserved bits */
878 ret = reg_write(client, RJ54N1_TG_BYPASS, 2);
880 /* Take sensor out of reset */
882 ret = reg_write(client, RJ54N1_RESET_STANDBY,
886 ret = reg_write(client, RJ54N1_PLL_EN, 1);
888 /* Wait for PLL to stabilise */
891 /* Enable clock to frequency divider */
893 ret = reg_write(client, RJ54N1_CLK_RST, 1);
896 ret = reg_read(client, RJ54N1_CLK_RST);
898 dev_err(&client->dev,
899 "Resetting RJ54N1CB0C clock failed: %d!\n", ret);
904 ret = reg_set(client, RJ54N1_OCLK_DSP, 1, 1);
908 ret = reg_write(client, RJ54N1_OCLK_SEL_EN, 1);
913 static int rj54n1_reg_init(struct i2c_client *client)
915 struct rj54n1 *rj54n1 = to_rj54n1(client);
916 int ret = rj54n1_set_clock(client);
919 ret = reg_write_multiple(client, bank_7, ARRAY_SIZE(bank_7));
921 ret = reg_write_multiple(client, bank_10, ARRAY_SIZE(bank_10));
923 /* Set binning divisors */
925 ret = reg_write(client, RJ54N1_SCALE_1_2_LEV, 3 | (7 << 4));
927 ret = reg_write(client, RJ54N1_SCALE_4_LEV, 0xf);
929 /* Switch to fixed resize mode */
931 ret = reg_write(client, RJ54N1_RESIZE_CONTROL,
932 RESIZE_HOLD_SEL | 1);
936 ret = reg_write(client, RJ54N1_Y_GAIN, 0x84);
939 * Mirror the image back: default is upside down and left-to-right...
940 * Set manual preview / still shot switching
943 ret = reg_write(client, RJ54N1_MIRROR_STILL_MODE, 0x27);
946 ret = reg_write_multiple(client, bank_4, ARRAY_SIZE(bank_4));
948 /* Auto exposure area */
950 ret = reg_write(client, RJ54N1_EXPOSURE_CONTROL, 0x80);
951 /* Check current auto WB config */
953 ret = reg_read(client, RJ54N1_WB_SEL_WEIGHT_I);
955 rj54n1->auto_wb = ret & 0x80;
956 ret = reg_write_multiple(client, bank_5, ARRAY_SIZE(bank_5));
959 ret = reg_write_multiple(client, bank_8, ARRAY_SIZE(bank_8));
962 ret = reg_write(client, RJ54N1_RESET_STANDBY,
963 E_EXCLK | DSP_RSTX | SEN_RSTX);
967 ret = rj54n1_commit(client);
969 /* Take DSP, TG, sensor out of reset */
971 ret = reg_write(client, RJ54N1_RESET_STANDBY,
972 E_EXCLK | DSP_RSTX | TG_RSTX | SEN_RSTX);
974 /* Start register update? Same register as 0x?FE in many bank_* sets */
976 ret = reg_write(client, RJ54N1_FWFLG, 2);
978 /* Constant taken from manufacturer's example */
984 static int rj54n1_try_fmt(struct v4l2_subdev *sd,
985 struct v4l2_mbus_framefmt *mf)
987 struct i2c_client *client = v4l2_get_subdevdata(sd);
988 struct rj54n1 *rj54n1 = to_rj54n1(client);
989 const struct rj54n1_datafmt *fmt;
990 int align = mf->code == V4L2_MBUS_FMT_SBGGR10_1X10 ||
991 mf->code == V4L2_MBUS_FMT_SBGGR10_2X8_PADHI_BE ||
992 mf->code == V4L2_MBUS_FMT_SBGGR10_2X8_PADLO_BE ||
993 mf->code == V4L2_MBUS_FMT_SBGGR10_2X8_PADHI_LE ||
994 mf->code == V4L2_MBUS_FMT_SBGGR10_2X8_PADLO_LE;
996 dev_dbg(&client->dev, "%s: code = %d, width = %u, height = %u\n",
997 __func__, mf->code, mf->width, mf->height);
999 fmt = rj54n1_find_datafmt(mf->code, rj54n1_colour_fmts,
1000 ARRAY_SIZE(rj54n1_colour_fmts));
1003 mf->code = fmt->code;
1006 mf->field = V4L2_FIELD_NONE;
1007 mf->colorspace = fmt->colorspace;
1009 v4l_bound_align_image(&mf->width, 112, RJ54N1_MAX_WIDTH, align,
1010 &mf->height, 84, RJ54N1_MAX_HEIGHT, align, 0);
1015 static int rj54n1_s_fmt(struct v4l2_subdev *sd,
1016 struct v4l2_mbus_framefmt *mf)
1018 struct i2c_client *client = v4l2_get_subdevdata(sd);
1019 struct rj54n1 *rj54n1 = to_rj54n1(client);
1020 const struct rj54n1_datafmt *fmt;
1021 int output_w, output_h, max_w, max_h,
1022 input_w = rj54n1->rect.width, input_h = rj54n1->rect.height;
1026 * The host driver can call us without .try_fmt(), so, we have to take
1029 rj54n1_try_fmt(sd, mf);
1032 * Verify if the sensor has just been powered on. TODO: replace this
1033 * with proper PM, when a suitable API is available.
1035 ret = reg_read(client, RJ54N1_RESET_STANDBY);
1039 if (!(ret & E_EXCLK)) {
1040 ret = rj54n1_reg_init(client);
1045 dev_dbg(&client->dev, "%s: code = %d, width = %u, height = %u\n",
1046 __func__, mf->code, mf->width, mf->height);
1048 /* RA_SEL_UL is only relevant for raw modes, ignored otherwise. */
1050 case V4L2_MBUS_FMT_YUYV8_2X8:
1051 ret = reg_write(client, RJ54N1_OUT_SEL, 0);
1053 ret = reg_set(client, RJ54N1_BYTE_SWAP, 8, 8);
1055 case V4L2_MBUS_FMT_YVYU8_2X8:
1056 ret = reg_write(client, RJ54N1_OUT_SEL, 0);
1058 ret = reg_set(client, RJ54N1_BYTE_SWAP, 0, 8);
1060 case V4L2_MBUS_FMT_RGB565_2X8_LE:
1061 ret = reg_write(client, RJ54N1_OUT_SEL, 0x11);
1063 ret = reg_set(client, RJ54N1_BYTE_SWAP, 8, 8);
1065 case V4L2_MBUS_FMT_RGB565_2X8_BE:
1066 ret = reg_write(client, RJ54N1_OUT_SEL, 0x11);
1068 ret = reg_set(client, RJ54N1_BYTE_SWAP, 0, 8);
1070 case V4L2_MBUS_FMT_SBGGR10_2X8_PADLO_LE:
1071 ret = reg_write(client, RJ54N1_OUT_SEL, 4);
1073 ret = reg_set(client, RJ54N1_BYTE_SWAP, 8, 8);
1075 ret = reg_write(client, RJ54N1_RA_SEL_UL, 0);
1077 case V4L2_MBUS_FMT_SBGGR10_2X8_PADHI_LE:
1078 ret = reg_write(client, RJ54N1_OUT_SEL, 4);
1080 ret = reg_set(client, RJ54N1_BYTE_SWAP, 8, 8);
1082 ret = reg_write(client, RJ54N1_RA_SEL_UL, 8);
1084 case V4L2_MBUS_FMT_SBGGR10_2X8_PADLO_BE:
1085 ret = reg_write(client, RJ54N1_OUT_SEL, 4);
1087 ret = reg_set(client, RJ54N1_BYTE_SWAP, 0, 8);
1089 ret = reg_write(client, RJ54N1_RA_SEL_UL, 0);
1091 case V4L2_MBUS_FMT_SBGGR10_2X8_PADHI_BE:
1092 ret = reg_write(client, RJ54N1_OUT_SEL, 4);
1094 ret = reg_set(client, RJ54N1_BYTE_SWAP, 0, 8);
1096 ret = reg_write(client, RJ54N1_RA_SEL_UL, 8);
1098 case V4L2_MBUS_FMT_SBGGR10_1X10:
1099 ret = reg_write(client, RJ54N1_OUT_SEL, 5);
1105 /* Special case: a raw mode with 10 bits of data per clock tick */
1107 ret = reg_set(client, RJ54N1_OCLK_SEL_EN,
1108 (mf->code == V4L2_MBUS_FMT_SBGGR10_1X10) << 1, 2);
1113 /* Supported scales 1:1 >= scale > 1:16 */
1114 max_w = mf->width * (16 * 1024 - 1) / 1024;
1115 if (input_w > max_w)
1117 max_h = mf->height * (16 * 1024 - 1) / 1024;
1118 if (input_h > max_h)
1121 output_w = mf->width;
1122 output_h = mf->height;
1124 ret = rj54n1_sensor_scale(sd, &input_w, &input_h, &output_w, &output_h);
1128 fmt = rj54n1_find_datafmt(mf->code, rj54n1_colour_fmts,
1129 ARRAY_SIZE(rj54n1_colour_fmts));
1132 rj54n1->resize = ret;
1133 rj54n1->rect.width = input_w;
1134 rj54n1->rect.height = input_h;
1135 rj54n1->width = output_w;
1136 rj54n1->height = output_h;
1138 mf->width = output_w;
1139 mf->height = output_h;
1140 mf->field = V4L2_FIELD_NONE;
1141 mf->colorspace = fmt->colorspace;
1146 static int rj54n1_g_chip_ident(struct v4l2_subdev *sd,
1147 struct v4l2_dbg_chip_ident *id)
1149 struct i2c_client *client = v4l2_get_subdevdata(sd);
1151 if (id->match.type != V4L2_CHIP_MATCH_I2C_ADDR)
1154 if (id->match.addr != client->addr)
1157 id->ident = V4L2_IDENT_RJ54N1CB0C;
1163 #ifdef CONFIG_VIDEO_ADV_DEBUG
1164 static int rj54n1_g_register(struct v4l2_subdev *sd,
1165 struct v4l2_dbg_register *reg)
1167 struct i2c_client *client = v4l2_get_subdevdata(sd);
1169 if (reg->match.type != V4L2_CHIP_MATCH_I2C_ADDR ||
1170 reg->reg < 0x400 || reg->reg > 0x1fff)
1171 /* Registers > 0x0800 are only available from Sharp support */
1174 if (reg->match.addr != client->addr)
1178 reg->val = reg_read(client, reg->reg);
1180 if (reg->val > 0xff)
1186 static int rj54n1_s_register(struct v4l2_subdev *sd,
1187 struct v4l2_dbg_register *reg)
1189 struct i2c_client *client = v4l2_get_subdevdata(sd);
1191 if (reg->match.type != V4L2_CHIP_MATCH_I2C_ADDR ||
1192 reg->reg < 0x400 || reg->reg > 0x1fff)
1193 /* Registers >= 0x0800 are only available from Sharp support */
1196 if (reg->match.addr != client->addr)
1199 if (reg_write(client, reg->reg, reg->val) < 0)
1206 static const struct v4l2_queryctrl rj54n1_controls[] = {
1208 .id = V4L2_CID_VFLIP,
1209 .type = V4L2_CTRL_TYPE_BOOLEAN,
1210 .name = "Flip Vertically",
1216 .id = V4L2_CID_HFLIP,
1217 .type = V4L2_CTRL_TYPE_BOOLEAN,
1218 .name = "Flip Horizontally",
1224 .id = V4L2_CID_GAIN,
1225 .type = V4L2_CTRL_TYPE_INTEGER,
1230 .default_value = 66,
1231 .flags = V4L2_CTRL_FLAG_SLIDER,
1233 .id = V4L2_CID_AUTO_WHITE_BALANCE,
1234 .type = V4L2_CTRL_TYPE_BOOLEAN,
1235 .name = "Auto white balance",
1243 static struct soc_camera_ops rj54n1_ops = {
1244 .set_bus_param = rj54n1_set_bus_param,
1245 .query_bus_param = rj54n1_query_bus_param,
1246 .controls = rj54n1_controls,
1247 .num_controls = ARRAY_SIZE(rj54n1_controls),
1250 static int rj54n1_g_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
1252 struct i2c_client *client = v4l2_get_subdevdata(sd);
1253 struct rj54n1 *rj54n1 = to_rj54n1(client);
1257 case V4L2_CID_VFLIP:
1258 data = reg_read(client, RJ54N1_MIRROR_STILL_MODE);
1261 ctrl->value = !(data & 1);
1263 case V4L2_CID_HFLIP:
1264 data = reg_read(client, RJ54N1_MIRROR_STILL_MODE);
1267 ctrl->value = !(data & 2);
1270 data = reg_read(client, RJ54N1_Y_GAIN);
1274 ctrl->value = data / 2;
1276 case V4L2_CID_AUTO_WHITE_BALANCE:
1277 ctrl->value = rj54n1->auto_wb;
1284 static int rj54n1_s_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
1287 struct i2c_client *client = v4l2_get_subdevdata(sd);
1288 struct rj54n1 *rj54n1 = to_rj54n1(client);
1289 const struct v4l2_queryctrl *qctrl;
1291 qctrl = soc_camera_find_qctrl(&rj54n1_ops, ctrl->id);
1296 case V4L2_CID_VFLIP:
1298 data = reg_set(client, RJ54N1_MIRROR_STILL_MODE, 0, 1);
1300 data = reg_set(client, RJ54N1_MIRROR_STILL_MODE, 1, 1);
1304 case V4L2_CID_HFLIP:
1306 data = reg_set(client, RJ54N1_MIRROR_STILL_MODE, 0, 2);
1308 data = reg_set(client, RJ54N1_MIRROR_STILL_MODE, 2, 2);
1313 if (ctrl->value > qctrl->maximum ||
1314 ctrl->value < qctrl->minimum)
1316 else if (reg_write(client, RJ54N1_Y_GAIN, ctrl->value * 2) < 0)
1319 case V4L2_CID_AUTO_WHITE_BALANCE:
1320 /* Auto WB area - whole image */
1321 if (reg_set(client, RJ54N1_WB_SEL_WEIGHT_I, ctrl->value << 7,
1324 rj54n1->auto_wb = ctrl->value;
1331 static struct v4l2_subdev_core_ops rj54n1_subdev_core_ops = {
1332 .g_ctrl = rj54n1_g_ctrl,
1333 .s_ctrl = rj54n1_s_ctrl,
1334 .g_chip_ident = rj54n1_g_chip_ident,
1335 #ifdef CONFIG_VIDEO_ADV_DEBUG
1336 .g_register = rj54n1_g_register,
1337 .s_register = rj54n1_s_register,
1341 static struct v4l2_subdev_video_ops rj54n1_subdev_video_ops = {
1342 .s_stream = rj54n1_s_stream,
1343 .s_mbus_fmt = rj54n1_s_fmt,
1344 .g_mbus_fmt = rj54n1_g_fmt,
1345 .try_mbus_fmt = rj54n1_try_fmt,
1346 .enum_mbus_fmt = rj54n1_enum_fmt,
1347 .g_crop = rj54n1_g_crop,
1348 .s_crop = rj54n1_s_crop,
1349 .cropcap = rj54n1_cropcap,
1352 static struct v4l2_subdev_ops rj54n1_subdev_ops = {
1353 .core = &rj54n1_subdev_core_ops,
1354 .video = &rj54n1_subdev_video_ops,
1358 * Interface active, can use i2c. If it fails, it can indeed mean, that
1359 * this wasn't our capture interface, so, we wait for the right one
1361 static int rj54n1_video_probe(struct soc_camera_device *icd,
1362 struct i2c_client *client,
1363 struct rj54n1_pdata *priv)
1368 /* We must have a parent by now. And it cannot be a wrong one. */
1369 BUG_ON(!icd->parent ||
1370 to_soc_camera_host(icd->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");