// SPDX-License-Identifier: GPL-2.0
/*
* Driver for RJ54N1CB0C CMOS Image Sensor from Sharp
*
* Copyright ( C ) 2018 , Jacopo Mondi < jacopo @ jmondi . org >
*
* Copyright ( C ) 2009 , Guennadi Liakhovetski < g . liakhovetski @ gmx . de >
*/
#include <linux/clk.h>
#include <linux/delay.h>
#include <linux/gpio/consumer.h>
#include <linux/i2c.h>
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/v4l2-mediabus.h>
#include <linux/videodev2.h>
#include <media/i2c/rj54n1cb0c.h>
#include <media/v4l2-device.h>
#include <media/v4l2-ctrls.h>
#include <media/v4l2-subdev.h>
#define RJ54N1_DEV_CODE 0 x0400
#define RJ54N1_DEV_CODE2 0 x0401
#define RJ54N1_OUT_SEL 0 x0403
#define RJ54N1_XY_OUTPUT_SIZE_S_H 0 x0404
#define RJ54N1_X_OUTPUT_SIZE_S_L 0 x0405
#define RJ54N1_Y_OUTPUT_SIZE_S_L 0 x0406
#define RJ54N1_XY_OUTPUT_SIZE_P_H 0 x0407
#define RJ54N1_X_OUTPUT_SIZE_P_L 0 x0408
#define RJ54N1_Y_OUTPUT_SIZE_P_L 0 x0409
#define RJ54N1_LINE_LENGTH_PCK_S_H 0 x040a
#define RJ54N1_LINE_LENGTH_PCK_S_L 0 x040b
#define RJ54N1_LINE_LENGTH_PCK_P_H 0 x040c
#define RJ54N1_LINE_LENGTH_PCK_P_L 0 x040d
#define RJ54N1_RESIZE_N 0 x040e
#define RJ54N1_RESIZE_N_STEP 0 x040f
#define RJ54N1_RESIZE_STEP 0 x0410
#define RJ54N1_RESIZE_HOLD_H 0 x0411
#define RJ54N1_RESIZE_HOLD_L 0 x0412
#define RJ54N1_H_OBEN_OFS 0 x0413
#define RJ54N1_V_OBEN_OFS 0 x0414
#define RJ54N1_RESIZE_CONTROL 0 x0415
#define RJ54N1_STILL_CONTROL 0 x0417
#define RJ54N1_INC_USE_SEL_H 0 x0425
#define RJ54N1_INC_USE_SEL_L 0 x0426
#define RJ54N1_MIRROR_STILL_MODE 0 x0427
#define RJ54N1_INIT_START 0 x0428
#define RJ54N1_SCALE_1_2_LEV 0 x0429
#define RJ54N1_SCALE_4_LEV 0 x042a
#define RJ54N1_Y_GAIN 0 x04d8
#define RJ54N1_APT_GAIN_UP 0 x04fa
#define RJ54N1_RA_SEL_UL 0 x0530
#define RJ54N1_BYTE_SWAP 0 x0531
#define RJ54N1_OUT_SIGPO 0 x053b
#define RJ54N1_WB_SEL_WEIGHT_I 0 x054e
#define RJ54N1_BIT8_WB 0 x0569
#define RJ54N1_HCAPS_WB 0 x056a
#define RJ54N1_VCAPS_WB 0 x056b
#define RJ54N1_HCAPE_WB 0 x056c
#define RJ54N1_VCAPE_WB 0 x056d
#define RJ54N1_EXPOSURE_CONTROL 0 x058c
#define RJ54N1_FRAME_LENGTH_S_H 0 x0595
#define RJ54N1_FRAME_LENGTH_S_L 0 x0596
#define RJ54N1_FRAME_LENGTH_P_H 0 x0597
#define RJ54N1_FRAME_LENGTH_P_L 0 x0598
#define RJ54N1_PEAK_H 0 x05b7
#define RJ54N1_PEAK_50 0 x05b8
#define RJ54N1_PEAK_60 0 x05b9
#define RJ54N1_PEAK_DIFF 0 x05ba
#define RJ54N1_IOC 0 x05ef
#define RJ54N1_TG_BYPASS 0 x0700
#define RJ54N1_PLL_L 0 x0701
#define RJ54N1_PLL_N 0 x0702
#define RJ54N1_PLL_EN 0 x0704
#define RJ54N1_RATIO_TG 0 x0706
#define RJ54N1_RATIO_T 0 x0707
#define RJ54N1_RATIO_R 0 x0708
#define RJ54N1_RAMP_TGCLK_EN 0 x0709
#define RJ54N1_OCLK_DSP 0 x0710
#define RJ54N1_RATIO_OP 0 x0711
#define RJ54N1_RATIO_O 0 x0712
#define RJ54N1_OCLK_SEL_EN 0 x0713
#define RJ54N1_CLK_RST 0 x0717
#define RJ54N1_RESET_STANDBY 0 x0718
#define RJ54N1_FWFLG 0 x07fe
#define E_EXCLK (1 << 7 )
#define SOFT_STDBY (1 << 4 )
#define SEN_RSTX (1 << 2 )
#define TG_RSTX (1 << 1 )
#define DSP_RSTX (1 << 0 )
#define RESIZE_HOLD_SEL (1 << 2 )
#define RESIZE_GO (1 << 1 )
/*
* When cropping , the camera automatically centers the cropped region , there
* doesn ' t seem to be a way to specify an explicit location of the rectangle .
*/
#define RJ54N1_COLUMN_SKIP 0
#define RJ54N1_ROW_SKIP 0
#define RJ54N1_MAX_WIDTH 1600
#define RJ54N1_MAX_HEIGHT 1200
#define PLL_L 2
#define PLL_N 0 x31
/* I2C addresses: 0x50, 0x51, 0x60, 0x61 */
/* RJ54N1CB0C has only one fixed colorspace per pixelcode */
struct rj54n1_datafmt {
u32 code;
enum v4l2_colorspace colorspace;
};
/* Find a data format by a pixel code in an array */
static const struct rj54n1_datafmt *rj54n1_find_datafmt(
u32 code, const struct rj54n1_datafmt *fmt,
int n)
{
int i;
for (i = 0 ; i < n; i++)
if (fmt[i].code == code)
return fmt + i;
return NULL;
}
static const struct rj54n1_datafmt rj54n1_colour_fmts[] = {
{MEDIA_BUS_FMT_YUYV8_2X8, V4L2_COLORSPACE_JPEG},
{MEDIA_BUS_FMT_YVYU8_2X8, V4L2_COLORSPACE_JPEG},
{MEDIA_BUS_FMT_RGB565_2X8_LE, V4L2_COLORSPACE_SRGB},
{MEDIA_BUS_FMT_RGB565_2X8_BE, V4L2_COLORSPACE_SRGB},
{MEDIA_BUS_FMT_SBGGR10_2X8_PADHI_LE, V4L2_COLORSPACE_SRGB},
{MEDIA_BUS_FMT_SBGGR10_2X8_PADLO_LE, V4L2_COLORSPACE_SRGB},
{MEDIA_BUS_FMT_SBGGR10_2X8_PADHI_BE, V4L2_COLORSPACE_SRGB},
{MEDIA_BUS_FMT_SBGGR10_2X8_PADLO_BE, V4L2_COLORSPACE_SRGB},
{MEDIA_BUS_FMT_SBGGR10_1X10, V4L2_COLORSPACE_SRGB},
};
struct rj54n1_clock_div {
u8 ratio_tg; /* can be 0 or an odd number */
u8 ratio_t;
u8 ratio_r;
u8 ratio_op;
u8 ratio_o;
};
struct rj54n1 {
struct v4l2_subdev subdev;
struct v4l2_ctrl_handler hdl;
struct clk *clk;
struct gpio_desc *pwup_gpio;
struct gpio_desc *enable_gpio;
struct rj54n1_clock_div clk_div;
const struct rj54n1_datafmt *fmt;
struct v4l2_rect rect; /* Sensor window */
unsigned int tgclk_mhz;
bool auto_wb;
unsigned short width; /* Output window */
unsigned short height;
unsigned short resize; /* Sensor * 1024 / resize = Output */
unsigned short scale;
u8 bank;
};
struct rj54n1_reg_val {
u16 reg;
u8 val;
};
static const struct rj54n1_reg_val bank_4[] = {
{0 x417, 0 },
{0 x42c, 0 },
{0 x42d, 0 xf0},
{0 x42e, 0 },
{0 x42f, 0 x50},
{0 x430, 0 xf5},
{0 x431, 0 x16},
{0 x432, 0 x20},
{0 x433, 0 },
{0 x434, 0 xc8},
{0 x43c, 8 },
{0 x43e, 0 x90},
{0 x445, 0 x83},
{0 x4ba, 0 x58},
{0 x4bb, 4 },
{0 x4bc, 0 x20},
{0 x4db, 4 },
{0 x4fe, 2 },
};
static const struct rj54n1_reg_val bank_5[] = {
{0 x514, 0 },
{0 x516, 0 },
{0 x518, 0 },
{0 x51a, 0 },
{0 x51d, 0 xff},
{0 x56f, 0 x28},
{0 x575, 0 x40},
{0 x5bc, 0 x48},
{0 x5c1, 6 },
{0 x5e5, 0 x11},
{0 x5e6, 0 x43},
{0 x5e7, 0 x33},
{0 x5e8, 0 x21},
{0 x5e9, 0 x30},
{0 x5ea, 0 x0},
{0 x5eb, 0 xa5},
{0 x5ec, 0 xff},
{0 x5fe, 2 },
};
static const struct rj54n1_reg_val bank_7[] = {
{0 x70a, 0 },
{0 x714, 0 xff},
{0 x715, 0 xff},
{0 x716, 0 x1f},
{0 x7FE, 2 },
};
static const struct rj54n1_reg_val bank_8[] = {
{0 x800, 0 x00},
{0 x801, 0 x01},
{0 x802, 0 x61},
{0 x805, 0 x00},
{0 x806, 0 x00},
{0 x807, 0 x00},
{0 x808, 0 x00},
{0 x809, 0 x01},
{0 x80A, 0 x61},
{0 x80B, 0 x00},
{0 x80C, 0 x01},
{0 x80D, 0 x00},
{0 x80E, 0 x00},
{0 x80F, 0 x00},
{0 x810, 0 x00},
{0 x811, 0 x01},
{0 x812, 0 x61},
{0 x813, 0 x00},
{0 x814, 0 x11},
{0 x815, 0 x00},
{0 x816, 0 x41},
{0 x817, 0 x00},
{0 x818, 0 x51},
{0 x819, 0 x01},
{0 x81A, 0 x1F},
{0 x81B, 0 x00},
{0 x81C, 0 x01},
{0 x81D, 0 x00},
{0 x81E, 0 x11},
{0 x81F, 0 x00},
{0 x820, 0 x41},
{0 x821, 0 x00},
{0 x822, 0 x51},
{0 x823, 0 x00},
{0 x824, 0 x00},
{0 x825, 0 x00},
{0 x826, 0 x47},
{0 x827, 0 x01},
{0 x828, 0 x4F},
{0 x829, 0 x00},
{0 x82A, 0 x00},
{0 x82B, 0 x00},
{0 x82C, 0 x30},
{0 x82D, 0 x00},
{0 x82E, 0 x40},
{0 x82F, 0 x00},
{0 x830, 0 xB3},
{0 x831, 0 x00},
{0 x832, 0 xE3},
{0 x833, 0 x00},
{0 x834, 0 x00},
{0 x835, 0 x00},
{0 x836, 0 x00},
{0 x837, 0 x00},
{0 x838, 0 x00},
{0 x839, 0 x01},
{0 x83A, 0 x61},
{0 x83B, 0 x00},
{0 x83C, 0 x01},
{0 x83D, 0 x00},
{0 x83E, 0 x00},
{0 x83F, 0 x00},
{0 x840, 0 x00},
{0 x841, 0 x01},
{0 x842, 0 x61},
{0 x843, 0 x00},
{0 x844, 0 x1D},
{0 x845, 0 x00},
{0 x846, 0 x00},
{0 x847, 0 x00},
{0 x848, 0 x00},
{0 x849, 0 x01},
{0 x84A, 0 x1F},
{0 x84B, 0 x00},
{0 x84C, 0 x05},
{0 x84D, 0 x00},
{0 x84E, 0 x19},
{0 x84F, 0 x01},
{0 x850, 0 x21},
{0 x851, 0 x01},
{0 x852, 0 x5D},
{0 x853, 0 x00},
{0 x854, 0 x00},
{0 x855, 0 x00},
{0 x856, 0 x19},
{0 x857, 0 x01},
{0 x858, 0 x21},
{0 x859, 0 x00},
{0 x85A, 0 x00},
{0 x85B, 0 x00},
{0 x85C, 0 x00},
{0 x85D, 0 x00},
{0 x85E, 0 x00},
{0 x85F, 0 x00},
{0 x860, 0 xB3},
{0 x861, 0 x00},
{0 x862, 0 xE3},
{0 x863, 0 x00},
{0 x864, 0 x00},
{0 x865, 0 x00},
{0 x866, 0 x00},
{0 x867, 0 x00},
{0 x868, 0 x00},
{0 x869, 0 xE2},
{0 x86A, 0 x00},
{0 x86B, 0 x01},
{0 x86C, 0 x06},
{0 x86D, 0 x00},
{0 x86E, 0 x00},
{0 x86F, 0 x00},
{0 x870, 0 x60},
{0 x871, 0 x8C},
{0 x872, 0 x10},
{0 x873, 0 x00},
{0 x874, 0 xE0},
{0 x875, 0 x00},
{0 x876, 0 x27},
{0 x877, 0 x01},
{0 x878, 0 x00},
{0 x879, 0 x00},
{0 x87A, 0 x00},
{0 x87B, 0 x03},
{0 x87C, 0 x00},
{0 x87D, 0 x00},
{0 x87E, 0 x00},
{0 x87F, 0 x00},
{0 x880, 0 x00},
{0 x881, 0 x00},
{0 x882, 0 x00},
{0 x883, 0 x00},
{0 x884, 0 x00},
{0 x885, 0 x00},
{0 x886, 0 xF8},
{0 x887, 0 x00},
{0 x888, 0 x03},
{0 x889, 0 x00},
{0 x88A, 0 x64},
{0 x88B, 0 x00},
{0 x88C, 0 x03},
{0 x88D, 0 x00},
{0 x88E, 0 xB1},
{0 x88F, 0 x00},
{0 x890, 0 x03},
{0 x891, 0 x01},
{0 x892, 0 x1D},
{0 x893, 0 x00},
{0 x894, 0 x03},
{0 x895, 0 x01},
{0 x896, 0 x4B},
{0 x897, 0 x00},
{0 x898, 0 xE5},
{0 x899, 0 x00},
{0 x89A, 0 x01},
{0 x89B, 0 x00},
{0 x89C, 0 x01},
{0 x89D, 0 x04},
{0 x89E, 0 xC8},
{0 x89F, 0 x00},
{0 x8A0, 0 x01},
{0 x8A1, 0 x01},
{0 x8A2, 0 x61},
{0 x8A3, 0 x00},
{0 x8A4, 0 x01},
{0 x8A5, 0 x00},
{0 x8A6, 0 x00},
{0 x8A7, 0 x00},
{0 x8A8, 0 x00},
{0 x8A9, 0 x00},
{0 x8AA, 0 x7F},
{0 x8AB, 0 x03},
{0 x8AC, 0 x00},
{0 x8AD, 0 x00},
{0 x8AE, 0 x00},
{0 x8AF, 0 x00},
{0 x8B0, 0 x00},
{0 x8B1, 0 x00},
{0 x8B6, 0 x00},
{0 x8B7, 0 x01},
{0 x8B8, 0 x00},
{0 x8B9, 0 x00},
{0 x8BA, 0 x02},
{0 x8BB, 0 x00},
{0 x8BC, 0 xFF},
{0 x8BD, 0 x00},
{0 x8FE, 2 },
};
static const struct rj54n1_reg_val bank_10[] = {
{0 x10bf, 0 x69}
};
/* Clock dividers - these are default register values, divider = register + 1 */
static const struct rj54n1_clock_div clk_div = {
.ratio_tg = 3 /* default: 5 */,
.ratio_t = 4 /* default: 1 */,
.ratio_r = 4 /* default: 0 */,
.ratio_op = 1 /* default: 5 */,
.ratio_o = 9 /* default: 0 */,
};
static struct rj54n1 *to_rj54n1(const struct i2c_client *client)
{
return container_of(i2c_get_clientdata(client), struct rj54n1, subdev);
}
static int reg_read(struct i2c_client *client, const u16 reg)
{
struct rj54n1 *rj54n1 = to_rj54n1(client);
int ret;
/* set bank */
if (rj54n1->bank != reg >> 8 ) {
dev_dbg(&client->dev, "[0x%x] = 0x%x\n" , 0 xff, reg >> 8 );
ret = i2c_smbus_write_byte_data(client, 0 xff, reg >> 8 );
if (ret < 0 )
return ret;
rj54n1->bank = reg >> 8 ;
}
return i2c_smbus_read_byte_data(client, reg & 0 xff);
}
static int reg_write(struct i2c_client *client, const u16 reg,
const u8 data)
{
struct rj54n1 *rj54n1 = to_rj54n1(client);
int ret;
/* set bank */
if (rj54n1->bank != reg >> 8 ) {
dev_dbg(&client->dev, "[0x%x] = 0x%x\n" , 0 xff, reg >> 8 );
ret = i2c_smbus_write_byte_data(client, 0 xff, reg >> 8 );
if (ret < 0 )
return ret;
rj54n1->bank = reg >> 8 ;
}
dev_dbg(&client->dev, "[0x%x] = 0x%x\n" , reg & 0 xff, data);
return i2c_smbus_write_byte_data(client, reg & 0 xff, data);
}
static int reg_set(struct i2c_client *client, const u16 reg,
const u8 data, const u8 mask)
{
int ret;
ret = reg_read(client, reg);
if (ret < 0 )
return ret;
return reg_write(client, reg, (ret & ~mask) | (data & mask));
}
static int reg_write_multiple(struct i2c_client *client,
const struct rj54n1_reg_val *rv, const int n)
{
int i, ret;
for (i = 0 ; i < n; i++) {
ret = reg_write(client, rv->reg, rv->val);
if (ret < 0 )
return ret;
rv++;
}
return 0 ;
}
static int rj54n1_enum_mbus_code(struct v4l2_subdev *sd,
struct v4l2_subdev_state *sd_state,
struct v4l2_subdev_mbus_code_enum *code)
{
if (code->pad || code->index >= ARRAY_SIZE(rj54n1_colour_fmts))
return -EINVAL;
code->code = rj54n1_colour_fmts[code->index].code;
return 0 ;
}
static int rj54n1_s_stream(struct v4l2_subdev *sd, int enable)
{
struct i2c_client *client = v4l2_get_subdevdata(sd);
/* Switch between preview and still shot modes */
return reg_set(client, RJ54N1_STILL_CONTROL, (!enable) << 7 , 0 x80);
}
static int rj54n1_set_rect(struct i2c_client *client,
u16 reg_x, u16 reg_y, u16 reg_xy,
u32 width, u32 height)
{
int ret;
ret = reg_write(client, reg_xy,
((width >> 4 ) & 0 x70) |
((height >> 8 ) & 7 ));
if (!ret)
ret = reg_write(client, reg_x, width & 0 xff);
if (!ret)
ret = reg_write(client, reg_y, height & 0 xff);
return ret;
}
/*
* Some commands , specifically certain initialisation sequences , require
* a commit operation .
*/
static int rj54n1_commit(struct i2c_client *client)
{
int ret = reg_write(client, RJ54N1_INIT_START, 1 );
msleep(10 );
if (!ret)
ret = reg_write(client, RJ54N1_INIT_START, 0 );
return ret;
}
static int rj54n1_sensor_scale(struct v4l2_subdev *sd, s32 *in_w, s32 *in_h,
s32 *out_w, s32 *out_h);
static int rj54n1_set_selection(struct v4l2_subdev *sd,
struct v4l2_subdev_state *sd_state,
struct v4l2_subdev_selection *sel)
{
struct i2c_client *client = v4l2_get_subdevdata(sd);
struct rj54n1 *rj54n1 = to_rj54n1(client);
const struct v4l2_rect *rect = &sel->r;
int output_w, output_h, input_w = rect->width, input_h = rect->height;
int ret;
if (sel->which != V4L2_SUBDEV_FORMAT_ACTIVE ||
sel->target != V4L2_SEL_TGT_CROP)
return -EINVAL;
/* arbitrary minimum width and height, edges unimportant */
v4l_bound_align_image(&input_w, 8 , RJ54N1_MAX_WIDTH, 0 ,
&input_h, 8 , RJ54N1_MAX_HEIGHT, 0 , 0 );
output_w = (input_w * 1024 + rj54n1->resize / 2 ) / rj54n1->resize;
output_h = (input_h * 1024 + rj54n1->resize / 2 ) / rj54n1->resize;
dev_dbg(&client->dev, "Scaling for %dx%d : %u = %dx%d\n" ,
input_w, input_h, rj54n1->resize, output_w, output_h);
ret = rj54n1_sensor_scale(sd, &input_w, &input_h, &output_w, &output_h);
if (ret < 0 )
return ret;
rj54n1->width = output_w;
rj54n1->height = output_h;
rj54n1->resize = ret;
rj54n1->rect.width = input_w;
rj54n1->rect.height = input_h;
return 0 ;
}
static int rj54n1_get_selection(struct v4l2_subdev *sd,
struct v4l2_subdev_state *sd_state,
struct v4l2_subdev_selection *sel)
{
struct i2c_client *client = v4l2_get_subdevdata(sd);
struct rj54n1 *rj54n1 = to_rj54n1(client);
if (sel->which != V4L2_SUBDEV_FORMAT_ACTIVE)
return -EINVAL;
switch (sel->target) {
case V4L2_SEL_TGT_CROP_BOUNDS:
sel->r.left = RJ54N1_COLUMN_SKIP;
sel->r.top = RJ54N1_ROW_SKIP;
sel->r.width = RJ54N1_MAX_WIDTH;
sel->r.height = RJ54N1_MAX_HEIGHT;
return 0 ;
case V4L2_SEL_TGT_CROP:
sel->r = rj54n1->rect;
return 0 ;
default :
return -EINVAL;
}
}
static int rj54n1_get_fmt(struct v4l2_subdev *sd,
struct v4l2_subdev_state *sd_state,
struct v4l2_subdev_format *format)
{
struct v4l2_mbus_framefmt *mf = &format->format;
struct i2c_client *client = v4l2_get_subdevdata(sd);
struct rj54n1 *rj54n1 = to_rj54n1(client);
if (format->pad)
return -EINVAL;
mf->code = rj54n1->fmt->code;
mf->colorspace = rj54n1->fmt->colorspace;
mf->ycbcr_enc = V4L2_YCBCR_ENC_601;
mf->xfer_func = V4L2_XFER_FUNC_SRGB;
mf->quantization = V4L2_QUANTIZATION_DEFAULT;
mf->field = V4L2_FIELD_NONE;
mf->width = rj54n1->width;
mf->height = rj54n1->height;
return 0 ;
}
/*
* The actual geometry configuration routine . It scales the input window into
* the output one , updates the window sizes and returns an error or the resize
* coefficient on success . Note : we only use the " Fixed Scaling " on this camera .
*/
static int rj54n1_sensor_scale(struct v4l2_subdev *sd, s32 *in_w, s32 *in_h,
s32 *out_w, s32 *out_h)
{
struct i2c_client *client = v4l2_get_subdevdata(sd);
struct rj54n1 *rj54n1 = to_rj54n1(client);
unsigned int skip, resize, input_w = *in_w, input_h = *in_h,
output_w = *out_w, output_h = *out_h;
u16 inc_sel, wb_bit8, wb_left, wb_right, wb_top, wb_bottom;
unsigned int peak, peak_50, peak_60;
int ret;
/*
* We have a problem with crops , where the window is larger than 512 x384
* and output window is larger than a half of the input one . In this
* case we have to either reduce the input window to equal or below
* 512 x384 or the output window to equal or below 1 / 2 of the input .
*/
if (output_w > max(512 U, input_w / 2 )) {
if (2 * output_w > RJ54N1_MAX_WIDTH) {
input_w = RJ54N1_MAX_WIDTH;
output_w = RJ54N1_MAX_WIDTH / 2 ;
} else {
input_w = output_w * 2 ;
}
dev_dbg(&client->dev, "Adjusted output width: in %u, out %u\n" ,
input_w, output_w);
}
if (output_h > max(384 U, input_h / 2 )) {
if (2 * output_h > RJ54N1_MAX_HEIGHT) {
input_h = RJ54N1_MAX_HEIGHT;
output_h = RJ54N1_MAX_HEIGHT / 2 ;
} else {
input_h = output_h * 2 ;
}
dev_dbg(&client->dev, "Adjusted output height: in %u, out %u\n" ,
input_h, output_h);
}
/* Idea: use the read mode for snapshots, handle separate geometries */
ret = rj54n1_set_rect(client, RJ54N1_X_OUTPUT_SIZE_S_L,
RJ54N1_Y_OUTPUT_SIZE_S_L,
RJ54N1_XY_OUTPUT_SIZE_S_H, output_w, output_h);
if (!ret)
ret = rj54n1_set_rect(client, RJ54N1_X_OUTPUT_SIZE_P_L,
RJ54N1_Y_OUTPUT_SIZE_P_L,
RJ54N1_XY_OUTPUT_SIZE_P_H, output_w, output_h);
if (ret < 0 )
return ret;
if (output_w > input_w && output_h > input_h) {
input_w = output_w;
input_h = output_h;
resize = 1024 ;
} else {
unsigned int resize_x, resize_y;
resize_x = (input_w * 1024 + output_w / 2 ) / output_w;
resize_y = (input_h * 1024 + output_h / 2 ) / output_h;
/* We want max(resize_x, resize_y), check if it still fits */
if (resize_x > resize_y &&
(output_h * resize_x + 512 ) / 1024 > RJ54N1_MAX_HEIGHT)
resize = (RJ54N1_MAX_HEIGHT * 1024 + output_h / 2 ) /
output_h;
else if (resize_y > resize_x &&
(output_w * resize_y + 512 ) / 1024 > RJ54N1_MAX_WIDTH)
resize = (RJ54N1_MAX_WIDTH * 1024 + output_w / 2 ) /
output_w;
else
resize = max(resize_x, resize_y);
/* Prohibited value ranges */
switch (resize) {
case 2040 ... 2047 :
resize = 2039 ;
break ;
case 4080 ... 4095 :
resize = 4079 ;
break ;
case 8160 ... 8191 :
resize = 8159 ;
break ;
case 16320 ... 16384 :
resize = 16319 ;
}
}
/* Set scaling */
ret = reg_write(client, RJ54N1_RESIZE_HOLD_L, resize & 0 xff);
if (!ret)
ret = reg_write(client, RJ54N1_RESIZE_HOLD_H, resize >> 8 );
if (ret < 0 )
return ret;
/*
* Configure a skipping bitmask . The sensor will select a skipping value
* among set bits automatically . This is very unclear in the datasheet
* too . I was told , in this register one enables all skipping values ,
* that are required for a specific resize , and the camera selects
* automatically , which ones to use . But it is unclear how to identify ,
* which cropping values are needed . Secondly , why don ' t we just set all
* bits and let the camera choose ? Would it increase processing time and
* reduce the framerate ? Using 0 xfffc for INC_USE_SEL doesn ' t seem to
* improve the image quality or stability for larger frames ( see comment
* above ) , but I didn ' t check the framerate .
*/
skip = min(resize / 1024 , 15 U);
inc_sel = 1 << skip;
if (inc_sel <= 2 )
inc_sel = 0 xc;
else if (resize & 1023 && skip < 15 )
inc_sel |= 1 << (skip + 1 );
ret = reg_write(client, RJ54N1_INC_USE_SEL_L, inc_sel & 0 xfc);
if (!ret)
ret = reg_write(client, RJ54N1_INC_USE_SEL_H, inc_sel >> 8 );
if (!rj54n1->auto_wb) {
/* Auto white balance window */
wb_left = output_w / 16 ;
wb_right = (3 * output_w / 4 - 3 ) / 4 ;
wb_top = output_h / 16 ;
wb_bottom = (3 * output_h / 4 - 3 ) / 4 ;
wb_bit8 = ((wb_left >> 2 ) & 0 x40) | ((wb_top >> 4 ) & 0 x10) |
((wb_right >> 6 ) & 4 ) | ((wb_bottom >> 8 ) & 1 );
if (!ret)
ret = reg_write(client, RJ54N1_BIT8_WB, wb_bit8);
if (!ret)
ret = reg_write(client, RJ54N1_HCAPS_WB, wb_left);
if (!ret)
ret = reg_write(client, RJ54N1_VCAPS_WB, wb_top);
if (!ret)
ret = reg_write(client, RJ54N1_HCAPE_WB, wb_right);
if (!ret)
ret = reg_write(client, RJ54N1_VCAPE_WB, wb_bottom);
}
/* Antiflicker */
peak = 12 * RJ54N1_MAX_WIDTH * (1 << 14 ) * resize / rj54n1->tgclk_mhz /
10000 ;
peak_50 = peak / 6 ;
peak_60 = peak / 5 ;
if (!ret)
ret = reg_write(client, RJ54N1_PEAK_H,
((peak_50 >> 4 ) & 0 xf0) | (peak_60 >> 8 ));
if (!ret)
ret = reg_write(client, RJ54N1_PEAK_50, peak_50);
if (!ret)
ret = reg_write(client, RJ54N1_PEAK_60, peak_60);
if (!ret)
ret = reg_write(client, RJ54N1_PEAK_DIFF, peak / 150 );
/* Start resizing */
if (!ret)
ret = reg_write(client, RJ54N1_RESIZE_CONTROL,
RESIZE_HOLD_SEL | RESIZE_GO | 1 );
if (ret < 0 )
return ret;
/* Constant taken from manufacturer's example */
msleep(230 );
ret = reg_write(client, RJ54N1_RESIZE_CONTROL, RESIZE_HOLD_SEL | 1 );
if (ret < 0 )
return ret;
*in_w = (output_w * resize + 512 ) / 1024 ;
*in_h = (output_h * resize + 512 ) / 1024 ;
*out_w = output_w;
*out_h = output_h;
dev_dbg(&client->dev, "Scaled for %dx%d : %u = %ux%u, skip %u\n" ,
*in_w, *in_h, resize, output_w, output_h, skip);
return resize;
}
static int rj54n1_set_clock(struct i2c_client *client)
{
struct rj54n1 *rj54n1 = to_rj54n1(client);
int ret;
/* Enable external clock */
ret = reg_write(client, RJ54N1_RESET_STANDBY, E_EXCLK | SOFT_STDBY);
/* Leave stand-by. Note: use this when implementing suspend / resume */
if (!ret)
ret = reg_write(client, RJ54N1_RESET_STANDBY, E_EXCLK);
if (!ret)
ret = reg_write(client, RJ54N1_PLL_L, PLL_L);
if (!ret)
ret = reg_write(client, RJ54N1_PLL_N, PLL_N);
/* TGCLK dividers */
if (!ret)
ret = reg_write(client, RJ54N1_RATIO_TG,
rj54n1->clk_div.ratio_tg);
if (!ret)
ret = reg_write(client, RJ54N1_RATIO_T,
rj54n1->clk_div.ratio_t);
if (!ret)
ret = reg_write(client, RJ54N1_RATIO_R,
rj54n1->clk_div.ratio_r);
/* Enable TGCLK & RAMP */
if (!ret)
ret = reg_write(client, RJ54N1_RAMP_TGCLK_EN, 3 );
/* Disable clock output */
if (!ret)
ret = reg_write(client, RJ54N1_OCLK_DSP, 0 );
/* Set divisors */
if (!ret)
ret = reg_write(client, RJ54N1_RATIO_OP,
rj54n1->clk_div.ratio_op);
if (!ret)
ret = reg_write(client, RJ54N1_RATIO_O,
rj54n1->clk_div.ratio_o);
/* Enable OCLK */
if (!ret)
ret = reg_write(client, RJ54N1_OCLK_SEL_EN, 1 );
/* Use PLL for Timing Generator, write 2 to reserved bits */
if (!ret)
ret = reg_write(client, RJ54N1_TG_BYPASS, 2 );
/* Take sensor out of reset */
if (!ret)
ret = reg_write(client, RJ54N1_RESET_STANDBY,
E_EXCLK | SEN_RSTX);
/* Enable PLL */
if (!ret)
ret = reg_write(client, RJ54N1_PLL_EN, 1 );
/* Wait for PLL to stabilise */
msleep(10 );
/* Enable clock to frequency divider */
if (!ret)
ret = reg_write(client, RJ54N1_CLK_RST, 1 );
if (!ret)
ret = reg_read(client, RJ54N1_CLK_RST);
if (ret != 1 ) {
dev_err(&client->dev,
"Resetting RJ54N1CB0C clock failed: %d!\n" , ret);
return -EIO;
}
/* Start the PLL */
ret = reg_set(client, RJ54N1_OCLK_DSP, 1 , 1 );
/* Enable OCLK */
if (!ret)
ret = reg_write(client, RJ54N1_OCLK_SEL_EN, 1 );
return ret;
}
static int rj54n1_reg_init(struct i2c_client *client)
{
struct rj54n1 *rj54n1 = to_rj54n1(client);
int ret = rj54n1_set_clock(client);
if (!ret)
ret = reg_write_multiple(client, bank_7, ARRAY_SIZE(bank_7));
if (!ret)
ret = reg_write_multiple(client, bank_10, ARRAY_SIZE(bank_10));
/* Set binning divisors */
if (!ret)
ret = reg_write(client, RJ54N1_SCALE_1_2_LEV, 3 | (7 << 4 ));
if (!ret)
ret = reg_write(client, RJ54N1_SCALE_4_LEV, 0 xf);
/* Switch to fixed resize mode */
if (!ret)
ret = reg_write(client, RJ54N1_RESIZE_CONTROL,
RESIZE_HOLD_SEL | 1 );
/* Set gain */
if (!ret)
ret = reg_write(client, RJ54N1_Y_GAIN, 0 x84);
/*
* Mirror the image back : default is upside down and left - to - right . . .
* Set manual preview / still shot switching
*/
if (!ret)
ret = reg_write(client, RJ54N1_MIRROR_STILL_MODE, 0 x27);
if (!ret)
ret = reg_write_multiple(client, bank_4, ARRAY_SIZE(bank_4));
/* Auto exposure area */
if (!ret)
ret = reg_write(client, RJ54N1_EXPOSURE_CONTROL, 0 x80);
/* Check current auto WB config */
if (!ret)
ret = reg_read(client, RJ54N1_WB_SEL_WEIGHT_I);
if (ret >= 0 ) {
rj54n1->auto_wb = ret & 0 x80;
ret = reg_write_multiple(client, bank_5, ARRAY_SIZE(bank_5));
}
if (!ret)
ret = reg_write_multiple(client, bank_8, ARRAY_SIZE(bank_8));
if (!ret)
ret = reg_write(client, RJ54N1_RESET_STANDBY,
E_EXCLK | DSP_RSTX | SEN_RSTX);
/* Commit init */
if (!ret)
ret = rj54n1_commit(client);
/* Take DSP, TG, sensor out of reset */
if (!ret)
ret = reg_write(client, RJ54N1_RESET_STANDBY,
E_EXCLK | DSP_RSTX | TG_RSTX | SEN_RSTX);
/* Start register update? Same register as 0x?FE in many bank_* sets */
if (!ret)
ret = reg_write(client, RJ54N1_FWFLG, 2 );
/* Constant taken from manufacturer's example */
msleep(700 );
return ret;
}
static int rj54n1_set_fmt(struct v4l2_subdev *sd,
struct v4l2_subdev_state *sd_state,
struct v4l2_subdev_format *format)
{
struct v4l2_mbus_framefmt *mf = &format->format;
struct i2c_client *client = v4l2_get_subdevdata(sd);
struct rj54n1 *rj54n1 = to_rj54n1(client);
const struct rj54n1_datafmt *fmt;
int output_w, output_h, max_w, max_h,
input_w = rj54n1->rect.width, input_h = rj54n1->rect.height;
int align = mf->code == MEDIA_BUS_FMT_SBGGR10_1X10 ||
mf->code == MEDIA_BUS_FMT_SBGGR10_2X8_PADHI_BE ||
mf->code == MEDIA_BUS_FMT_SBGGR10_2X8_PADLO_BE ||
mf->code == MEDIA_BUS_FMT_SBGGR10_2X8_PADHI_LE ||
mf->code == MEDIA_BUS_FMT_SBGGR10_2X8_PADLO_LE;
int ret;
if (format->pad)
return -EINVAL;
dev_dbg(&client->dev, "%s: code = %d, width = %u, height = %u\n" ,
__func__, mf->code, mf->width, mf->height);
fmt = rj54n1_find_datafmt(mf->code, rj54n1_colour_fmts,
ARRAY_SIZE(rj54n1_colour_fmts));
if (!fmt) {
fmt = rj54n1->fmt;
mf->code = fmt->code;
}
mf->field = V4L2_FIELD_NONE;
mf->colorspace = fmt->colorspace;
v4l_bound_align_image(&mf->width, 112 , RJ54N1_MAX_WIDTH, align,
&mf->height, 84 , RJ54N1_MAX_HEIGHT, align, 0 );
if (format->which == V4L2_SUBDEV_FORMAT_TRY)
return 0 ;
/*
* Verify if the sensor has just been powered on . TODO : replace this
* with proper PM , when a suitable API is available .
*/
ret = reg_read(client, RJ54N1_RESET_STANDBY);
if (ret < 0 )
return ret;
if (!(ret & E_EXCLK)) {
ret = rj54n1_reg_init(client);
if (ret < 0 )
return ret;
}
/* RA_SEL_UL is only relevant for raw modes, ignored otherwise. */
switch (mf->code) {
case MEDIA_BUS_FMT_YUYV8_2X8:
ret = reg_write(client, RJ54N1_OUT_SEL, 0 );
if (!ret)
ret = reg_set(client, RJ54N1_BYTE_SWAP, 8 , 8 );
break ;
case MEDIA_BUS_FMT_YVYU8_2X8:
ret = reg_write(client, RJ54N1_OUT_SEL, 0 );
if (!ret)
ret = reg_set(client, RJ54N1_BYTE_SWAP, 0 , 8 );
break ;
case MEDIA_BUS_FMT_RGB565_2X8_LE:
ret = reg_write(client, RJ54N1_OUT_SEL, 0 x11);
if (!ret)
ret = reg_set(client, RJ54N1_BYTE_SWAP, 8 , 8 );
break ;
case MEDIA_BUS_FMT_RGB565_2X8_BE:
ret = reg_write(client, RJ54N1_OUT_SEL, 0 x11);
if (!ret)
ret = reg_set(client, RJ54N1_BYTE_SWAP, 0 , 8 );
break ;
case MEDIA_BUS_FMT_SBGGR10_2X8_PADLO_LE:
ret = reg_write(client, RJ54N1_OUT_SEL, 4 );
if (!ret)
ret = reg_set(client, RJ54N1_BYTE_SWAP, 8 , 8 );
if (!ret)
ret = reg_write(client, RJ54N1_RA_SEL_UL, 0 );
break ;
case MEDIA_BUS_FMT_SBGGR10_2X8_PADHI_LE:
ret = reg_write(client, RJ54N1_OUT_SEL, 4 );
if (!ret)
ret = reg_set(client, RJ54N1_BYTE_SWAP, 8 , 8 );
if (!ret)
ret = reg_write(client, RJ54N1_RA_SEL_UL, 8 );
break ;
case MEDIA_BUS_FMT_SBGGR10_2X8_PADLO_BE:
ret = reg_write(client, RJ54N1_OUT_SEL, 4 );
if (!ret)
ret = reg_set(client, RJ54N1_BYTE_SWAP, 0 , 8 );
if (!ret)
ret = reg_write(client, RJ54N1_RA_SEL_UL, 0 );
break ;
case MEDIA_BUS_FMT_SBGGR10_2X8_PADHI_BE:
ret = reg_write(client, RJ54N1_OUT_SEL, 4 );
if (!ret)
ret = reg_set(client, RJ54N1_BYTE_SWAP, 0 , 8 );
if (!ret)
ret = reg_write(client, RJ54N1_RA_SEL_UL, 8 );
break ;
case MEDIA_BUS_FMT_SBGGR10_1X10:
ret = reg_write(client, RJ54N1_OUT_SEL, 5 );
break ;
default :
ret = -EINVAL;
}
/* Special case: a raw mode with 10 bits of data per clock tick */
if (!ret)
ret = reg_set(client, RJ54N1_OCLK_SEL_EN,
(mf->code == MEDIA_BUS_FMT_SBGGR10_1X10) << 1 , 2 );
if (ret < 0 )
return ret;
/* Supported scales 1:1 >= scale > 1:16 */
max_w = mf->width * (16 * 1024 - 1 ) / 1024 ;
if (input_w > max_w)
input_w = max_w;
max_h = mf->height * (16 * 1024 - 1 ) / 1024 ;
if (input_h > max_h)
input_h = max_h;
output_w = mf->width;
output_h = mf->height;
ret = rj54n1_sensor_scale(sd, &input_w, &input_h, &output_w, &output_h);
if (ret < 0 )
return ret;
fmt = rj54n1_find_datafmt(mf->code, rj54n1_colour_fmts,
ARRAY_SIZE(rj54n1_colour_fmts));
rj54n1->fmt = fmt;
rj54n1->resize = ret;
rj54n1->rect.width = input_w;
rj54n1->rect.height = input_h;
rj54n1->width = output_w;
rj54n1->height = output_h;
mf->width = output_w;
mf->height = output_h;
mf->field = V4L2_FIELD_NONE;
mf->colorspace = fmt->colorspace;
return 0 ;
}
#ifdef CONFIG_VIDEO_ADV_DEBUG
static int rj54n1_g_register(struct v4l2_subdev *sd,
struct v4l2_dbg_register *reg)
{
struct i2c_client *client = v4l2_get_subdevdata(sd);
if (reg->reg < 0 x400 || reg->reg > 0 x1fff)
/* Registers > 0x0800 are only available from Sharp support */
return -EINVAL;
reg->size = 1 ;
reg->val = reg_read(client, reg->reg);
if (reg->val > 0 xff)
return -EIO;
return 0 ;
}
static int rj54n1_s_register(struct v4l2_subdev *sd,
const struct v4l2_dbg_register *reg)
{
struct i2c_client *client = v4l2_get_subdevdata(sd);
if (reg->reg < 0 x400 || reg->reg > 0 x1fff)
/* Registers >= 0x0800 are only available from Sharp support */
return -EINVAL;
if (reg_write(client, reg->reg, reg->val) < 0 )
return -EIO;
return 0 ;
}
#endif
static int rj54n1_s_power(struct v4l2_subdev *sd, int on)
{
struct i2c_client *client = v4l2_get_subdevdata(sd);
struct rj54n1 *rj54n1 = to_rj54n1(client);
if (on) {
if (rj54n1->pwup_gpio)
gpiod_set_value(rj54n1->pwup_gpio, 1 );
if (rj54n1->enable_gpio)
gpiod_set_value(rj54n1->enable_gpio, 1 );
msleep(1 );
return clk_prepare_enable(rj54n1->clk);
}
clk_disable_unprepare(rj54n1->clk);
if (rj54n1->enable_gpio)
gpiod_set_value(rj54n1->enable_gpio, 0 );
if (rj54n1->pwup_gpio)
gpiod_set_value(rj54n1->pwup_gpio, 0 );
return 0 ;
}
static int rj54n1_s_ctrl(struct v4l2_ctrl *ctrl)
{
struct rj54n1 *rj54n1 = container_of(ctrl->handler, struct rj54n1, hdl);
struct v4l2_subdev *sd = &rj54n1->subdev;
struct i2c_client *client = v4l2_get_subdevdata(sd);
int data;
switch (ctrl->id) {
case V4L2_CID_VFLIP:
if (ctrl->val)
data = reg_set(client, RJ54N1_MIRROR_STILL_MODE, 0 , 1 );
else
data = reg_set(client, RJ54N1_MIRROR_STILL_MODE, 1 , 1 );
if (data < 0 )
return -EIO;
return 0 ;
case V4L2_CID_HFLIP:
if (ctrl->val)
data = reg_set(client, RJ54N1_MIRROR_STILL_MODE, 0 , 2 );
else
data = reg_set(client, RJ54N1_MIRROR_STILL_MODE, 2 , 2 );
if (data < 0 )
return -EIO;
return 0 ;
case V4L2_CID_GAIN:
if (reg_write(client, RJ54N1_Y_GAIN, ctrl->val * 2 ) < 0 )
return -EIO;
return 0 ;
case V4L2_CID_AUTO_WHITE_BALANCE:
/* Auto WB area - whole image */
if (reg_set(client, RJ54N1_WB_SEL_WEIGHT_I, ctrl->val << 7 ,
0 x80) < 0 )
return -EIO;
rj54n1->auto_wb = ctrl->val;
return 0 ;
}
return -EINVAL;
}
static const struct v4l2_ctrl_ops rj54n1_ctrl_ops = {
.s_ctrl = rj54n1_s_ctrl,
};
static const struct v4l2_subdev_core_ops rj54n1_subdev_core_ops = {
#ifdef CONFIG_VIDEO_ADV_DEBUG
.g_register = rj54n1_g_register,
.s_register = rj54n1_s_register,
#endif
.s_power = rj54n1_s_power,
};
static const struct v4l2_subdev_video_ops rj54n1_subdev_video_ops = {
.s_stream = rj54n1_s_stream,
};
static const struct v4l2_subdev_pad_ops rj54n1_subdev_pad_ops = {
.enum_mbus_code = rj54n1_enum_mbus_code,
.get_selection = rj54n1_get_selection,
.set_selection = rj54n1_set_selection,
.get_fmt = rj54n1_get_fmt,
.set_fmt = rj54n1_set_fmt,
};
static const struct v4l2_subdev_ops rj54n1_subdev_ops = {
.core = &rj54n1_subdev_core_ops,
.video = &rj54n1_subdev_video_ops,
.pad = &rj54n1_subdev_pad_ops,
};
/*
* Interface active , can use i2c . If it fails , it can indeed mean , that
* this wasn ' t our capture interface , so , we wait for the right one
*/
static int rj54n1_video_probe(struct i2c_client *client,
struct rj54n1_pdata *priv)
{
struct rj54n1 *rj54n1 = to_rj54n1(client);
int data1, data2;
int ret;
ret = rj54n1_s_power(&rj54n1->subdev, 1 );
if (ret < 0 )
return ret;
/* Read out the chip version register */
data1 = reg_read(client, RJ54N1_DEV_CODE);
data2 = reg_read(client, RJ54N1_DEV_CODE2);
if (data1 != 0 x51 || data2 != 0 x10) {
ret = -ENODEV;
dev_info(&client->dev, "No RJ54N1CB0C found, read 0x%x:0x%x\n" ,
data1, data2);
goto done;
}
/* Configure IOCTL polarity from the platform data: 0 or 1 << 7. */
ret = reg_write(client, RJ54N1_IOC, priv->ioctl_high << 7 );
if (ret < 0 )
goto done;
dev_info(&client->dev, "Detected a RJ54N1CB0C chip ID 0x%x:0x%x\n" ,
data1, data2);
ret = v4l2_ctrl_handler_setup(&rj54n1->hdl);
done:
rj54n1_s_power(&rj54n1->subdev, 0 );
return ret;
}
static int rj54n1_probe(struct i2c_client *client)
{
struct rj54n1 *rj54n1;
struct i2c_adapter *adapter = client->adapter;
struct rj54n1_pdata *rj54n1_priv;
int ret;
if (!client->dev.platform_data) {
dev_err(&client->dev, "RJ54N1CB0C: missing platform data!\n" );
return -EINVAL;
}
rj54n1_priv = client->dev.platform_data;
if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
dev_warn(&adapter->dev,
"I2C-Adapter doesn't support I2C_FUNC_SMBUS_BYTE\n" );
return -EIO;
}
rj54n1 = devm_kzalloc(&client->dev, sizeof (struct rj54n1), GFP_KERNEL);
if (!rj54n1)
return -ENOMEM;
v4l2_i2c_subdev_init(&rj54n1->subdev, client, &rj54n1_subdev_ops);
v4l2_ctrl_handler_init(&rj54n1->hdl, 4 );
v4l2_ctrl_new_std(&rj54n1->hdl, &rj54n1_ctrl_ops,
V4L2_CID_VFLIP, 0 , 1 , 1 , 0 );
v4l2_ctrl_new_std(&rj54n1->hdl, &rj54n1_ctrl_ops,
V4L2_CID_HFLIP, 0 , 1 , 1 , 0 );
v4l2_ctrl_new_std(&rj54n1->hdl, &rj54n1_ctrl_ops,
V4L2_CID_GAIN, 0 , 127 , 1 , 66 );
v4l2_ctrl_new_std(&rj54n1->hdl, &rj54n1_ctrl_ops,
V4L2_CID_AUTO_WHITE_BALANCE, 0 , 1 , 1 , 1 );
if (rj54n1->hdl.error) {
ret = rj54n1->hdl.error;
goto err_free_ctrl;
}
rj54n1->subdev.ctrl_handler = &rj54n1->hdl;
rj54n1->clk_div = clk_div;
rj54n1->rect.left = RJ54N1_COLUMN_SKIP;
rj54n1->rect.top = RJ54N1_ROW_SKIP;
rj54n1->rect.width = RJ54N1_MAX_WIDTH;
rj54n1->rect.height = RJ54N1_MAX_HEIGHT;
rj54n1->width = RJ54N1_MAX_WIDTH;
rj54n1->height = RJ54N1_MAX_HEIGHT;
rj54n1->fmt = &rj54n1_colour_fmts[0 ];
rj54n1->resize = 1024 ;
rj54n1->tgclk_mhz = (rj54n1_priv->mclk_freq / PLL_L * PLL_N) /
(clk_div.ratio_tg + 1 ) / (clk_div.ratio_t + 1 );
rj54n1->clk = clk_get(&client->dev, NULL);
if (IS_ERR(rj54n1->clk)) {
ret = PTR_ERR(rj54n1->clk);
goto err_free_ctrl;
}
rj54n1->pwup_gpio = gpiod_get_optional(&client->dev, "powerup" ,
GPIOD_OUT_LOW);
if (IS_ERR(rj54n1->pwup_gpio)) {
dev_info(&client->dev, "Unable to get GPIO \" powerup\": %ld\n" ,
PTR_ERR(rj54n1->pwup_gpio));
ret = PTR_ERR(rj54n1->pwup_gpio);
goto err_clk_put;
}
rj54n1->enable_gpio = gpiod_get_optional(&client->dev, "enable" ,
GPIOD_OUT_LOW);
if (IS_ERR(rj54n1->enable_gpio)) {
dev_info(&client->dev, "Unable to get GPIO \" enable\": %ld\n" ,
PTR_ERR(rj54n1->enable_gpio));
ret = PTR_ERR(rj54n1->enable_gpio);
goto err_gpio_put;
}
ret = rj54n1_video_probe(client, rj54n1_priv);
if (ret < 0 )
goto err_gpio_put;
ret = v4l2_async_register_subdev(&rj54n1->subdev);
if (ret)
goto err_gpio_put;
return 0 ;
err_gpio_put:
if (rj54n1->enable_gpio)
gpiod_put(rj54n1->enable_gpio);
if (rj54n1->pwup_gpio)
gpiod_put(rj54n1->pwup_gpio);
err_clk_put:
clk_put(rj54n1->clk);
err_free_ctrl:
v4l2_ctrl_handler_free(&rj54n1->hdl);
return ret;
}
static void rj54n1_remove(struct i2c_client *client)
{
struct rj54n1 *rj54n1 = to_rj54n1(client);
if (rj54n1->enable_gpio)
gpiod_put(rj54n1->enable_gpio);
if (rj54n1->pwup_gpio)
gpiod_put(rj54n1->pwup_gpio);
clk_put(rj54n1->clk);
v4l2_ctrl_handler_free(&rj54n1->hdl);
v4l2_async_unregister_subdev(&rj54n1->subdev);
}
static const struct i2c_device_id rj54n1_id[] = {
{ "rj54n1cb0c" },
{ }
};
MODULE_DEVICE_TABLE(i2c, rj54n1_id);
static struct i2c_driver rj54n1_i2c_driver = {
.driver = {
.name = "rj54n1cb0c" ,
},
.probe = rj54n1_probe,
.remove = rj54n1_remove,
.id_table = rj54n1_id,
};
module_i2c_driver(rj54n1_i2c_driver);
MODULE_DESCRIPTION("Sharp RJ54N1CB0C Camera driver" );
MODULE_AUTHOR("Guennadi Liakhovetski <g.liakhovetski@gmx.de>" );
MODULE_LICENSE("GPL v2" );
Messung V0.5 in Prozent C=95 H=97 G=95
¤ Dauer der Verarbeitung: 0.17 Sekunden
(vorverarbeitet am 2026-09-29)
¤
*© Formatika GbR, Deutschland