1 // SPDX-License-Identifier: GPL-2.0
3 * Driver for IMX074 CMOS Image Sensor from Sony
5 * Copyright (C) 2010, Guennadi Liakhovetski <g.liakhovetski@gmx.de>
7 * Partially inspired by the IMX074 driver from the Android / MSM tree
9 #include <linux/delay.h>
10 #include <linux/i2c.h>
11 #include <linux/v4l2-mediabus.h>
12 #include <linux/slab.h>
13 #include <linux/videodev2.h>
14 #include <linux/module.h>
16 #include <media/soc_camera.h>
17 #include <media/v4l2-async.h>
18 #include <media/v4l2-clk.h>
19 #include <media/v4l2-subdev.h>
21 /* IMX074 registers */
23 #define MODE_SELECT 0x0100
24 #define IMAGE_ORIENTATION 0x0101
25 #define GROUPED_PARAMETER_HOLD 0x0104
27 /* Integration Time */
28 #define COARSE_INTEGRATION_TIME_HI 0x0202
29 #define COARSE_INTEGRATION_TIME_LO 0x0203
31 #define ANALOGUE_GAIN_CODE_GLOBAL_HI 0x0204
32 #define ANALOGUE_GAIN_CODE_GLOBAL_LO 0x0205
35 #define PRE_PLL_CLK_DIV 0x0305
36 #define PLL_MULTIPLIER 0x0307
37 #define PLSTATIM 0x302b
38 #define VNDMY_ABLMGSHLMT 0x300a
39 #define Y_OPBADDR_START_DI 0x3014
41 #define FRAME_LENGTH_LINES_HI 0x0340
42 #define FRAME_LENGTH_LINES_LO 0x0341
43 #define LINE_LENGTH_PCK_HI 0x0342
44 #define LINE_LENGTH_PCK_LO 0x0343
45 #define YADDR_START 0x0347
46 #define YADDR_END 0x034b
47 #define X_OUTPUT_SIZE_MSB 0x034c
48 #define X_OUTPUT_SIZE_LSB 0x034d
49 #define Y_OUTPUT_SIZE_MSB 0x034e
50 #define Y_OUTPUT_SIZE_LSB 0x034f
51 #define X_EVEN_INC 0x0381
52 #define X_ODD_INC 0x0383
53 #define Y_EVEN_INC 0x0385
54 #define Y_ODD_INC 0x0387
56 #define HMODEADD 0x3001
57 #define VMODEADD 0x3016
58 #define VAPPLINE_START 0x3069
59 #define VAPPLINE_END 0x306b
60 #define SHUTTER 0x3086
61 #define HADDAVE 0x30e8
62 #define LANESEL 0x3301
64 /* IMX074 supported geometry */
65 #define IMX074_WIDTH 1052
66 #define IMX074_HEIGHT 780
68 /* IMX074 has only one fixed colorspace per pixelcode */
69 struct imx074_datafmt {
71 enum v4l2_colorspace colorspace;
75 struct v4l2_subdev subdev;
76 const struct imx074_datafmt *fmt;
80 static const struct imx074_datafmt imx074_colour_fmts[] = {
81 {MEDIA_BUS_FMT_SBGGR8_1X8, V4L2_COLORSPACE_SRGB},
84 static struct imx074 *to_imx074(const struct i2c_client *client)
86 return container_of(i2c_get_clientdata(client), struct imx074, subdev);
89 /* Find a data format by a pixel code in an array */
90 static const struct imx074_datafmt *imx074_find_datafmt(u32 code)
94 for (i = 0; i < ARRAY_SIZE(imx074_colour_fmts); i++)
95 if (imx074_colour_fmts[i].code == code)
96 return imx074_colour_fmts + i;
101 static int reg_write(struct i2c_client *client, const u16 addr, const u8 data)
103 struct i2c_adapter *adap = client->adapter;
108 msg.addr = client->addr;
117 ret = i2c_transfer(adap, &msg, 1);
121 return ret == 1 ? 0 : -EIO;
124 static int reg_read(struct i2c_client *client, const u16 addr)
126 u8 buf[2] = {addr >> 8, addr & 0xff};
128 struct i2c_msg msgs[] = {
130 .addr = client->addr,
135 .addr = client->addr,
142 ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
144 dev_warn(&client->dev, "Reading register %x from %x failed\n",
149 return buf[0] & 0xff; /* no sign-extension */
152 static int imx074_set_fmt(struct v4l2_subdev *sd,
153 struct v4l2_subdev_pad_config *cfg,
154 struct v4l2_subdev_format *format)
156 struct v4l2_mbus_framefmt *mf = &format->format;
157 const struct imx074_datafmt *fmt = imx074_find_datafmt(mf->code);
158 struct i2c_client *client = v4l2_get_subdevdata(sd);
159 struct imx074 *priv = to_imx074(client);
164 dev_dbg(sd->v4l2_dev->dev, "%s(%u)\n", __func__, mf->code);
167 /* MIPI CSI could have changed the format, double-check */
168 if (format->which == V4L2_SUBDEV_FORMAT_ACTIVE)
170 mf->code = imx074_colour_fmts[0].code;
171 mf->colorspace = imx074_colour_fmts[0].colorspace;
174 mf->width = IMX074_WIDTH;
175 mf->height = IMX074_HEIGHT;
176 mf->field = V4L2_FIELD_NONE;
178 if (format->which == V4L2_SUBDEV_FORMAT_ACTIVE)
186 static int imx074_get_fmt(struct v4l2_subdev *sd,
187 struct v4l2_subdev_pad_config *cfg,
188 struct v4l2_subdev_format *format)
190 struct v4l2_mbus_framefmt *mf = &format->format;
191 struct i2c_client *client = v4l2_get_subdevdata(sd);
192 struct imx074 *priv = to_imx074(client);
194 const struct imx074_datafmt *fmt = priv->fmt;
199 mf->code = fmt->code;
200 mf->colorspace = fmt->colorspace;
201 mf->width = IMX074_WIDTH;
202 mf->height = IMX074_HEIGHT;
203 mf->field = V4L2_FIELD_NONE;
208 static int imx074_get_selection(struct v4l2_subdev *sd,
209 struct v4l2_subdev_pad_config *cfg,
210 struct v4l2_subdev_selection *sel)
212 if (sel->which != V4L2_SUBDEV_FORMAT_ACTIVE)
217 sel->r.width = IMX074_WIDTH;
218 sel->r.height = IMX074_HEIGHT;
220 switch (sel->target) {
221 case V4L2_SEL_TGT_CROP_BOUNDS:
222 case V4L2_SEL_TGT_CROP:
229 static int imx074_enum_mbus_code(struct v4l2_subdev *sd,
230 struct v4l2_subdev_pad_config *cfg,
231 struct v4l2_subdev_mbus_code_enum *code)
234 (unsigned int)code->index >= ARRAY_SIZE(imx074_colour_fmts))
237 code->code = imx074_colour_fmts[code->index].code;
241 static int imx074_s_stream(struct v4l2_subdev *sd, int enable)
243 struct i2c_client *client = v4l2_get_subdevdata(sd);
245 /* MODE_SELECT: stream or standby */
246 return reg_write(client, MODE_SELECT, !!enable);
249 static int imx074_s_power(struct v4l2_subdev *sd, int on)
251 struct i2c_client *client = v4l2_get_subdevdata(sd);
252 struct soc_camera_subdev_desc *ssdd = soc_camera_i2c_to_desc(client);
253 struct imx074 *priv = to_imx074(client);
255 return soc_camera_set_power(&client->dev, ssdd, priv->clk, on);
258 static int imx074_g_mbus_config(struct v4l2_subdev *sd,
259 struct v4l2_mbus_config *cfg)
261 cfg->type = V4L2_MBUS_CSI2_DPHY;
262 cfg->flags = V4L2_MBUS_CSI2_2_LANE |
263 V4L2_MBUS_CSI2_CHANNEL_0 |
264 V4L2_MBUS_CSI2_CONTINUOUS_CLOCK;
269 static const struct v4l2_subdev_video_ops imx074_subdev_video_ops = {
270 .s_stream = imx074_s_stream,
271 .g_mbus_config = imx074_g_mbus_config,
274 static const struct v4l2_subdev_core_ops imx074_subdev_core_ops = {
275 .s_power = imx074_s_power,
278 static const struct v4l2_subdev_pad_ops imx074_subdev_pad_ops = {
279 .enum_mbus_code = imx074_enum_mbus_code,
280 .get_selection = imx074_get_selection,
281 .get_fmt = imx074_get_fmt,
282 .set_fmt = imx074_set_fmt,
285 static const struct v4l2_subdev_ops imx074_subdev_ops = {
286 .core = &imx074_subdev_core_ops,
287 .video = &imx074_subdev_video_ops,
288 .pad = &imx074_subdev_pad_ops,
291 static int imx074_video_probe(struct i2c_client *client)
293 struct v4l2_subdev *subdev = i2c_get_clientdata(client);
297 ret = imx074_s_power(subdev, 1);
301 /* Read sensor Model ID */
302 ret = reg_read(client, 0);
308 ret = reg_read(client, 1);
314 dev_info(&client->dev, "Chip ID 0x%04x detected\n", id);
321 /* PLL Setting EXTCLK=24MHz, 22.5times */
322 reg_write(client, PLL_MULTIPLIER, 0x2D);
323 reg_write(client, PRE_PLL_CLK_DIV, 0x02);
324 reg_write(client, PLSTATIM, 0x4B);
327 reg_write(client, 0x3024, 0x00);
329 reg_write(client, IMAGE_ORIENTATION, 0x00);
332 * 0x08+0x08 = top 8 bits
333 * 0x0a+0x08 = compressed 8-bits
334 * 0x0a+0x0a = 10 bits
336 reg_write(client, 0x0112, 0x08);
337 reg_write(client, 0x0113, 0x08);
339 /* Base setting for High frame mode */
340 reg_write(client, VNDMY_ABLMGSHLMT, 0x80);
341 reg_write(client, Y_OPBADDR_START_DI, 0x08);
342 reg_write(client, 0x3015, 0x37);
343 reg_write(client, 0x301C, 0x01);
344 reg_write(client, 0x302C, 0x05);
345 reg_write(client, 0x3031, 0x26);
346 reg_write(client, 0x3041, 0x60);
347 reg_write(client, 0x3051, 0x24);
348 reg_write(client, 0x3053, 0x34);
349 reg_write(client, 0x3057, 0xC0);
350 reg_write(client, 0x305C, 0x09);
351 reg_write(client, 0x305D, 0x07);
352 reg_write(client, 0x3060, 0x30);
353 reg_write(client, 0x3065, 0x00);
354 reg_write(client, 0x30AA, 0x08);
355 reg_write(client, 0x30AB, 0x1C);
356 reg_write(client, 0x30B0, 0x32);
357 reg_write(client, 0x30B2, 0x83);
358 reg_write(client, 0x30D3, 0x04);
359 reg_write(client, 0x3106, 0x78);
360 reg_write(client, 0x310C, 0x82);
361 reg_write(client, 0x3304, 0x05);
362 reg_write(client, 0x3305, 0x04);
363 reg_write(client, 0x3306, 0x11);
364 reg_write(client, 0x3307, 0x02);
365 reg_write(client, 0x3308, 0x0C);
366 reg_write(client, 0x3309, 0x06);
367 reg_write(client, 0x330A, 0x08);
368 reg_write(client, 0x330B, 0x04);
369 reg_write(client, 0x330C, 0x08);
370 reg_write(client, 0x330D, 0x06);
371 reg_write(client, 0x330E, 0x01);
372 reg_write(client, 0x3381, 0x00);
374 /* V : 1/2V-addition (1,3), H : 1/2H-averaging (1,3) -> Full HD */
375 /* 1608 = 1560 + 48 (black lines) */
376 reg_write(client, FRAME_LENGTH_LINES_HI, 0x06);
377 reg_write(client, FRAME_LENGTH_LINES_LO, 0x48);
378 reg_write(client, YADDR_START, 0x00);
379 reg_write(client, YADDR_END, 0x2F);
381 reg_write(client, X_OUTPUT_SIZE_MSB, 0x08);
382 reg_write(client, X_OUTPUT_SIZE_LSB, 0x38);
384 reg_write(client, Y_OUTPUT_SIZE_MSB, 0x06);
385 reg_write(client, Y_OUTPUT_SIZE_LSB, 0x18);
386 reg_write(client, X_EVEN_INC, 0x01);
387 reg_write(client, X_ODD_INC, 0x03);
388 reg_write(client, Y_EVEN_INC, 0x01);
389 reg_write(client, Y_ODD_INC, 0x03);
390 reg_write(client, HMODEADD, 0x00);
391 reg_write(client, VMODEADD, 0x16);
392 reg_write(client, VAPPLINE_START, 0x24);
393 reg_write(client, VAPPLINE_END, 0x53);
394 reg_write(client, SHUTTER, 0x00);
395 reg_write(client, HADDAVE, 0x80);
397 reg_write(client, LANESEL, 0x00);
399 reg_write(client, GROUPED_PARAMETER_HOLD, 0x00); /* off */
404 imx074_s_power(subdev, 0);
408 static int imx074_probe(struct i2c_client *client,
409 const struct i2c_device_id *did)
412 struct i2c_adapter *adapter = client->adapter;
413 struct soc_camera_subdev_desc *ssdd = soc_camera_i2c_to_desc(client);
417 dev_err(&client->dev, "IMX074: missing platform data!\n");
421 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
422 dev_warn(&adapter->dev,
423 "I2C-Adapter doesn't support I2C_FUNC_SMBUS_BYTE\n");
427 priv = devm_kzalloc(&client->dev, sizeof(struct imx074), GFP_KERNEL);
431 v4l2_i2c_subdev_init(&priv->subdev, client, &imx074_subdev_ops);
433 priv->fmt = &imx074_colour_fmts[0];
435 priv->clk = v4l2_clk_get(&client->dev, "mclk");
436 if (IS_ERR(priv->clk)) {
437 dev_info(&client->dev, "Error %ld getting clock\n", PTR_ERR(priv->clk));
438 return -EPROBE_DEFER;
441 ret = soc_camera_power_init(&client->dev, ssdd);
445 ret = imx074_video_probe(client);
449 ret = v4l2_async_register_subdev(&priv->subdev);
455 v4l2_clk_put(priv->clk);
459 static int imx074_remove(struct i2c_client *client)
461 struct soc_camera_subdev_desc *ssdd = soc_camera_i2c_to_desc(client);
462 struct imx074 *priv = to_imx074(client);
464 v4l2_async_unregister_subdev(&priv->subdev);
465 v4l2_clk_put(priv->clk);
468 ssdd->free_bus(ssdd);
473 static const struct i2c_device_id imx074_id[] = {
477 MODULE_DEVICE_TABLE(i2c, imx074_id);
479 static struct i2c_driver imx074_i2c_driver = {
483 .probe = imx074_probe,
484 .remove = imx074_remove,
485 .id_table = imx074_id,
488 module_i2c_driver(imx074_i2c_driver);
490 MODULE_DESCRIPTION("Sony IMX074 Camera driver");
491 MODULE_AUTHOR("Guennadi Liakhovetski <g.liakhovetski@gmx.de>");
492 MODULE_LICENSE("GPL v2");