12 #include <linux/i2c.h>
13 #include <linux/slab.h>
15 #include <linux/videodev2.h>
16 #include <linux/module.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)
102 #define RJ54N1_COLUMN_SKIP 0
103 #define RJ54N1_ROW_SKIP 0
104 #define RJ54N1_MAX_WIDTH 1600
105 #define RJ54N1_MAX_HEIGHT 1200
124 for (i = 0; i <
n; i++)
125 if (fmt[i].code == code)
433 if (rj54n1->
bank != reg >> 8) {
434 dev_dbg(&client->
dev,
"[0x%x] = 0x%x\n", 0xff, reg >> 8);
438 rj54n1->
bank = reg >> 8;
446 struct rj54n1 *rj54n1 = to_rj54n1(client);
450 if (rj54n1->
bank != reg >> 8) {
451 dev_dbg(&client->
dev,
"[0x%x] = 0x%x\n", 0xff, reg >> 8);
455 rj54n1->
bank = reg >> 8;
457 dev_dbg(&client->
dev,
"[0x%x] = 0x%x\n", reg & 0xff, data);
469 return reg_write(client, reg, (ret & ~mask) | (data & mask));
472 static int reg_write_multiple(
struct i2c_client *client,
477 for (i = 0; i <
n; i++) {
499 struct i2c_client *client = v4l2_get_subdevdata(sd);
505 static int rj54n1_set_rect(
struct i2c_client *client,
512 ((width >> 4) & 0x70) |
513 ((height >> 8) & 7));
516 ret =
reg_write(client, reg_x, width & 0xff);
518 ret =
reg_write(client, reg_y, height & 0xff);
527 static int rj54n1_commit(
struct i2c_client *client)
541 struct i2c_client *client = v4l2_get_subdevdata(sd);
542 struct rj54n1 *rj54n1 = to_rj54n1(client);
544 int dummy = 0, output_w, output_h,
549 soc_camera_limit_side(&dummy, &input_w,
552 soc_camera_limit_side(&dummy, &input_h,
555 output_w = (input_w * 1024 + rj54n1->
resize / 2) / rj54n1->
resize;
556 output_h = (input_h * 1024 + rj54n1->
resize / 2) / rj54n1->
resize;
558 dev_dbg(&client->
dev,
"Scaling for %dx%d : %u = %dx%d\n",
559 input_w, input_h, rj54n1->
resize, output_w, output_h);
561 ret = rj54n1_sensor_scale(sd, &input_w, &input_h, &output_w, &output_h);
565 rj54n1->
width = output_w;
566 rj54n1->
height = output_h;
568 rj54n1->
rect.width = input_w;
569 rj54n1->
rect.height = input_h;
576 struct i2c_client *client = v4l2_get_subdevdata(sd);
577 struct rj54n1 *rj54n1 = to_rj54n1(client);
600 struct v4l2_mbus_framefmt *mf)
602 struct i2c_client *client = v4l2_get_subdevdata(sd);
603 struct rj54n1 *rj54n1 = to_rj54n1(client);
605 mf->code = rj54n1->
fmt->code;
606 mf->colorspace = rj54n1->
fmt->colorspace;
608 mf->width = rj54n1->
width;
609 mf->height = rj54n1->
height;
622 struct i2c_client *client = v4l2_get_subdevdata(sd);
623 struct rj54n1 *rj54n1 = to_rj54n1(client);
624 unsigned int skip,
resize, input_w = *in_w, input_h = *in_h,
625 output_w = *out_w, output_h = *out_h;
626 u16 inc_sel, wb_bit8, wb_left, wb_right, wb_top, wb_bottom;
627 unsigned int peak, peak_50, peak_60;
636 if (output_w >
max(512
U, input_w / 2)) {
641 input_w = output_w * 2;
644 dev_dbg(&client->
dev,
"Adjusted output width: in %u, out %u\n",
648 if (output_h >
max(384
U, input_h / 2)) {
653 input_h = output_h * 2;
656 dev_dbg(&client->
dev,
"Adjusted output height: in %u, out %u\n",
672 if (output_w > input_w && output_h > input_h) {
678 unsigned int resize_x, resize_y;
679 resize_x = (input_w * 1024 + output_w / 2) / output_w;
680 resize_y = (input_h * 1024 + output_h / 2) / output_h;
683 if (resize_x > resize_y &&
687 else if (resize_y > resize_x &&
692 resize =
max(resize_x, resize_y);
705 case 16320 ... 16384:
730 skip =
min(resize / 1024, 15
U);
736 else if (resize & 1023 && skip < 15)
737 inc_sel |= 1 << (skip + 1);
745 wb_left = output_w / 16;
746 wb_right = (3 * output_w / 4 - 3) / 4;
747 wb_top = output_h / 16;
748 wb_bottom = (3 * output_h / 4 - 3) / 4;
749 wb_bit8 = ((wb_left >> 2) & 0x40) | ((wb_top >> 4) & 0x10) |
750 ((wb_right >> 6) & 4) | ((wb_bottom >> 8) & 1);
772 ((peak_50 >> 4) & 0xf0) | (peak_60 >> 8));
795 *in_w = (output_w * resize + 512) / 1024;
796 *in_h = (output_h * resize + 512) / 1024;
800 dev_dbg(&client->
dev,
"Scaled for %dx%d : %u = %ux%u, skip %u\n",
801 *in_w, *in_h, resize, output_w, output_h, skip);
806 static int rj54n1_set_clock(
struct i2c_client *client)
808 struct rj54n1 *rj54n1 = to_rj54n1(client);
876 "Resetting RJ54N1CB0C clock failed: %d!\n", ret);
890 static int rj54n1_reg_init(
struct i2c_client *client)
892 struct rj54n1 *rj54n1 = to_rj54n1(client);
893 int ret = rj54n1_set_clock(client);
896 ret = reg_write_multiple(client, bank_7,
ARRAY_SIZE(bank_7));
898 ret = reg_write_multiple(client, bank_10,
ARRAY_SIZE(bank_10));
923 ret = reg_write_multiple(client, bank_4,
ARRAY_SIZE(bank_4));
933 ret = reg_write_multiple(client, bank_5,
ARRAY_SIZE(bank_5));
936 ret = reg_write_multiple(client, bank_8,
ARRAY_SIZE(bank_8));
944 ret = rj54n1_commit(client);
962 struct v4l2_mbus_framefmt *mf)
964 struct i2c_client *client = v4l2_get_subdevdata(sd);
965 struct rj54n1 *rj54n1 = to_rj54n1(client);
973 dev_dbg(&client->
dev,
"%s: code = %d, width = %u, height = %u\n",
974 __func__, mf->code, mf->width, mf->height);
976 fmt = rj54n1_find_datafmt(mf->code, rj54n1_colour_fmts,
980 mf->code = fmt->
code;
993 struct v4l2_mbus_framefmt *mf)
995 struct i2c_client *client = v4l2_get_subdevdata(sd);
996 struct rj54n1 *rj54n1 = to_rj54n1(client);
998 int output_w, output_h, max_w, max_h,
999 input_w = rj54n1->
rect.width, input_h = rj54n1->
rect.height;
1006 rj54n1_try_fmt(sd, mf);
1017 ret = rj54n1_reg_init(client);
1022 dev_dbg(&client->
dev,
"%s: code = %d, width = %u, height = %u\n",
1023 __func__, mf->code, mf->width, mf->height);
1091 max_w = mf->width * (16 * 1024 - 1) / 1024;
1092 if (input_w > max_w)
1094 max_h = mf->height * (16 * 1024 - 1) / 1024;
1095 if (input_h > max_h)
1098 output_w = mf->width;
1099 output_h = mf->height;
1101 ret = rj54n1_sensor_scale(sd, &input_w, &input_h, &output_w, &output_h);
1105 fmt = rj54n1_find_datafmt(mf->code, rj54n1_colour_fmts,
1110 rj54n1->
rect.width = input_w;
1111 rj54n1->
rect.height = input_h;
1112 rj54n1->
width = output_w;
1113 rj54n1->
height = output_h;
1115 mf->width = output_w;
1116 mf->height = output_h;
1123 static int rj54n1_g_chip_ident(
struct v4l2_subdev *sd,
1126 struct i2c_client *client = v4l2_get_subdevdata(sd);
1140 #ifdef CONFIG_VIDEO_ADV_DEBUG
1141 static int rj54n1_g_register(
struct v4l2_subdev *sd,
1144 struct i2c_client *client = v4l2_get_subdevdata(sd);
1147 reg->
reg < 0x400 || reg->
reg > 0x1fff)
1157 if (reg->
val > 0xff)
1163 static int rj54n1_s_register(
struct v4l2_subdev *sd,
1166 struct i2c_client *client = v4l2_get_subdevdata(sd);
1169 reg->
reg < 0x400 || reg->
reg > 0x1fff)
1183 static int rj54n1_s_power(
struct v4l2_subdev *sd,
int on)
1185 struct i2c_client *client = v4l2_get_subdevdata(sd);
1188 return soc_camera_set_power(&client->
dev, icl, on);
1195 struct i2c_client *client = v4l2_get_subdevdata(sd);
1232 .s_ctrl = rj54n1_s_ctrl,
1236 .g_chip_ident = rj54n1_g_chip_ident,
1237 #ifdef CONFIG_VIDEO_ADV_DEBUG
1238 .g_register = rj54n1_g_register,
1239 .s_register = rj54n1_s_register,
1241 .s_power = rj54n1_s_power,
1244 static int rj54n1_g_mbus_config(
struct v4l2_subdev *sd,
1247 struct i2c_client *client = v4l2_get_subdevdata(sd);
1260 static int rj54n1_s_mbus_config(
struct v4l2_subdev *sd,
1263 struct i2c_client *client = v4l2_get_subdevdata(sd);
1275 .s_stream = rj54n1_s_stream,
1276 .s_mbus_fmt = rj54n1_s_fmt,
1277 .g_mbus_fmt = rj54n1_g_fmt,
1278 .try_mbus_fmt = rj54n1_try_fmt,
1279 .enum_mbus_fmt = rj54n1_enum_fmt,
1280 .g_crop = rj54n1_g_crop,
1281 .s_crop = rj54n1_s_crop,
1282 .cropcap = rj54n1_cropcap,
1283 .g_mbus_config = rj54n1_g_mbus_config,
1284 .s_mbus_config = rj54n1_s_mbus_config,
1288 .core = &rj54n1_subdev_core_ops,
1289 .video = &rj54n1_subdev_video_ops,
1296 static int rj54n1_video_probe(
struct i2c_client *client,
1299 struct rj54n1 *rj54n1 = to_rj54n1(client);
1303 ret = rj54n1_s_power(&rj54n1->
subdev, 1);
1311 if (data1 != 0x51 || data2 != 0x10) {
1313 dev_info(&client->
dev,
"No RJ54N1CB0C found, read 0x%x:0x%x\n",
1323 dev_info(&client->
dev,
"Detected a RJ54N1CB0C chip ID 0x%x:0x%x\n",
1329 rj54n1_s_power(&rj54n1->
subdev, 0);
1333 static int rj54n1_probe(
struct i2c_client *client,
1336 struct rj54n1 *rj54n1;
1342 if (!icl || !icl->
priv) {
1343 dev_err(&client->
dev,
"RJ54N1CB0C: missing platform data!\n");
1347 rj54n1_priv = icl->
priv;
1351 "I2C-Adapter doesn't support I2C_FUNC_SMBUS_BYTE\n");
1355 rj54n1 = kzalloc(
sizeof(
struct rj54n1),
GFP_KERNEL);
1369 rj54n1->
subdev.ctrl_handler = &rj54n1->
hdl;
1370 if (rj54n1->
hdl.error) {
1371 int err = rj54n1->
hdl.error;
1384 rj54n1->
fmt = &rj54n1_colour_fmts[0];
1389 ret = rj54n1_video_probe(client, rj54n1_priv);
1398 static int rj54n1_remove(
struct i2c_client *client)
1400 struct rj54n1 *rj54n1 = to_rj54n1(client);
1413 {
"rj54n1cb0c", 0 },
1418 static struct i2c_driver rj54n1_i2c_driver = {
1420 .name =
"rj54n1cb0c",
1422 .probe = rj54n1_probe,
1423 .remove = rj54n1_remove,
1424 .id_table = rj54n1_id,