imx074.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  1. /*
  2. * Driver for IMX074 CMOS Image Sensor from Sony
  3. *
  4. * Copyright (C) 2010, Guennadi Liakhovetski <g.liakhovetski@gmx.de>
  5. *
  6. * Partially inspired by the IMX074 driver from the Android / MSM tree
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. */
  12. #include <linux/delay.h>
  13. #include <linux/i2c.h>
  14. #include <linux/v4l2-mediabus.h>
  15. #include <linux/slab.h>
  16. #include <linux/videodev2.h>
  17. #include <linux/module.h>
  18. #include <media/soc_camera.h>
  19. #include <media/v4l2-async.h>
  20. #include <media/v4l2-clk.h>
  21. #include <media/v4l2-subdev.h>
  22. /* IMX074 registers */
  23. #define MODE_SELECT 0x0100
  24. #define IMAGE_ORIENTATION 0x0101
  25. #define GROUPED_PARAMETER_HOLD 0x0104
  26. /* Integration Time */
  27. #define COARSE_INTEGRATION_TIME_HI 0x0202
  28. #define COARSE_INTEGRATION_TIME_LO 0x0203
  29. /* Gain */
  30. #define ANALOGUE_GAIN_CODE_GLOBAL_HI 0x0204
  31. #define ANALOGUE_GAIN_CODE_GLOBAL_LO 0x0205
  32. /* PLL registers */
  33. #define PRE_PLL_CLK_DIV 0x0305
  34. #define PLL_MULTIPLIER 0x0307
  35. #define PLSTATIM 0x302b
  36. #define VNDMY_ABLMGSHLMT 0x300a
  37. #define Y_OPBADDR_START_DI 0x3014
  38. /* mode setting */
  39. #define FRAME_LENGTH_LINES_HI 0x0340
  40. #define FRAME_LENGTH_LINES_LO 0x0341
  41. #define LINE_LENGTH_PCK_HI 0x0342
  42. #define LINE_LENGTH_PCK_LO 0x0343
  43. #define YADDR_START 0x0347
  44. #define YADDR_END 0x034b
  45. #define X_OUTPUT_SIZE_MSB 0x034c
  46. #define X_OUTPUT_SIZE_LSB 0x034d
  47. #define Y_OUTPUT_SIZE_MSB 0x034e
  48. #define Y_OUTPUT_SIZE_LSB 0x034f
  49. #define X_EVEN_INC 0x0381
  50. #define X_ODD_INC 0x0383
  51. #define Y_EVEN_INC 0x0385
  52. #define Y_ODD_INC 0x0387
  53. #define HMODEADD 0x3001
  54. #define VMODEADD 0x3016
  55. #define VAPPLINE_START 0x3069
  56. #define VAPPLINE_END 0x306b
  57. #define SHUTTER 0x3086
  58. #define HADDAVE 0x30e8
  59. #define LANESEL 0x3301
  60. /* IMX074 supported geometry */
  61. #define IMX074_WIDTH 1052
  62. #define IMX074_HEIGHT 780
  63. /* IMX074 has only one fixed colorspace per pixelcode */
  64. struct imx074_datafmt {
  65. u32 code;
  66. enum v4l2_colorspace colorspace;
  67. };
  68. struct imx074 {
  69. struct v4l2_subdev subdev;
  70. const struct imx074_datafmt *fmt;
  71. struct v4l2_clk *clk;
  72. };
  73. static const struct imx074_datafmt imx074_colour_fmts[] = {
  74. {MEDIA_BUS_FMT_SBGGR8_1X8, V4L2_COLORSPACE_SRGB},
  75. };
  76. static struct imx074 *to_imx074(const struct i2c_client *client)
  77. {
  78. return container_of(i2c_get_clientdata(client), struct imx074, subdev);
  79. }
  80. /* Find a data format by a pixel code in an array */
  81. static const struct imx074_datafmt *imx074_find_datafmt(u32 code)
  82. {
  83. int i;
  84. for (i = 0; i < ARRAY_SIZE(imx074_colour_fmts); i++)
  85. if (imx074_colour_fmts[i].code == code)
  86. return imx074_colour_fmts + i;
  87. return NULL;
  88. }
  89. static int reg_write(struct i2c_client *client, const u16 addr, const u8 data)
  90. {
  91. struct i2c_adapter *adap = client->adapter;
  92. struct i2c_msg msg;
  93. unsigned char tx[3];
  94. int ret;
  95. msg.addr = client->addr;
  96. msg.buf = tx;
  97. msg.len = 3;
  98. msg.flags = 0;
  99. tx[0] = addr >> 8;
  100. tx[1] = addr & 0xff;
  101. tx[2] = data;
  102. ret = i2c_transfer(adap, &msg, 1);
  103. mdelay(2);
  104. return ret == 1 ? 0 : -EIO;
  105. }
  106. static int reg_read(struct i2c_client *client, const u16 addr)
  107. {
  108. u8 buf[2] = {addr >> 8, addr & 0xff};
  109. int ret;
  110. struct i2c_msg msgs[] = {
  111. {
  112. .addr = client->addr,
  113. .flags = 0,
  114. .len = 2,
  115. .buf = buf,
  116. }, {
  117. .addr = client->addr,
  118. .flags = I2C_M_RD,
  119. .len = 2,
  120. .buf = buf,
  121. },
  122. };
  123. ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
  124. if (ret < 0) {
  125. dev_warn(&client->dev, "Reading register %x from %x failed\n",
  126. addr, client->addr);
  127. return ret;
  128. }
  129. return buf[0] & 0xff; /* no sign-extension */
  130. }
  131. static int imx074_set_fmt(struct v4l2_subdev *sd,
  132. struct v4l2_subdev_pad_config *cfg,
  133. struct v4l2_subdev_format *format)
  134. {
  135. struct v4l2_mbus_framefmt *mf = &format->format;
  136. const struct imx074_datafmt *fmt = imx074_find_datafmt(mf->code);
  137. struct i2c_client *client = v4l2_get_subdevdata(sd);
  138. struct imx074 *priv = to_imx074(client);
  139. if (format->pad)
  140. return -EINVAL;
  141. dev_dbg(sd->v4l2_dev->dev, "%s(%u)\n", __func__, mf->code);
  142. if (!fmt) {
  143. /* MIPI CSI could have changed the format, double-check */
  144. if (format->which == V4L2_SUBDEV_FORMAT_ACTIVE)
  145. return -EINVAL;
  146. mf->code = imx074_colour_fmts[0].code;
  147. mf->colorspace = imx074_colour_fmts[0].colorspace;
  148. }
  149. mf->width = IMX074_WIDTH;
  150. mf->height = IMX074_HEIGHT;
  151. mf->field = V4L2_FIELD_NONE;
  152. if (format->which == V4L2_SUBDEV_FORMAT_ACTIVE)
  153. priv->fmt = imx074_find_datafmt(mf->code);
  154. else
  155. cfg->try_fmt = *mf;
  156. return 0;
  157. }
  158. static int imx074_get_fmt(struct v4l2_subdev *sd,
  159. struct v4l2_subdev_pad_config *cfg,
  160. struct v4l2_subdev_format *format)
  161. {
  162. struct v4l2_mbus_framefmt *mf = &format->format;
  163. struct i2c_client *client = v4l2_get_subdevdata(sd);
  164. struct imx074 *priv = to_imx074(client);
  165. const struct imx074_datafmt *fmt = priv->fmt;
  166. if (format->pad)
  167. return -EINVAL;
  168. mf->code = fmt->code;
  169. mf->colorspace = fmt->colorspace;
  170. mf->width = IMX074_WIDTH;
  171. mf->height = IMX074_HEIGHT;
  172. mf->field = V4L2_FIELD_NONE;
  173. return 0;
  174. }
  175. static int imx074_g_crop(struct v4l2_subdev *sd, struct v4l2_crop *a)
  176. {
  177. struct v4l2_rect *rect = &a->c;
  178. a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  179. rect->top = 0;
  180. rect->left = 0;
  181. rect->width = IMX074_WIDTH;
  182. rect->height = IMX074_HEIGHT;
  183. return 0;
  184. }
  185. static int imx074_cropcap(struct v4l2_subdev *sd, struct v4l2_cropcap *a)
  186. {
  187. a->bounds.left = 0;
  188. a->bounds.top = 0;
  189. a->bounds.width = IMX074_WIDTH;
  190. a->bounds.height = IMX074_HEIGHT;
  191. a->defrect = a->bounds;
  192. a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  193. a->pixelaspect.numerator = 1;
  194. a->pixelaspect.denominator = 1;
  195. return 0;
  196. }
  197. static int imx074_enum_mbus_code(struct v4l2_subdev *sd,
  198. struct v4l2_subdev_pad_config *cfg,
  199. struct v4l2_subdev_mbus_code_enum *code)
  200. {
  201. if (code->pad ||
  202. (unsigned int)code->index >= ARRAY_SIZE(imx074_colour_fmts))
  203. return -EINVAL;
  204. code->code = imx074_colour_fmts[code->index].code;
  205. return 0;
  206. }
  207. static int imx074_s_stream(struct v4l2_subdev *sd, int enable)
  208. {
  209. struct i2c_client *client = v4l2_get_subdevdata(sd);
  210. /* MODE_SELECT: stream or standby */
  211. return reg_write(client, MODE_SELECT, !!enable);
  212. }
  213. static int imx074_s_power(struct v4l2_subdev *sd, int on)
  214. {
  215. struct i2c_client *client = v4l2_get_subdevdata(sd);
  216. struct soc_camera_subdev_desc *ssdd = soc_camera_i2c_to_desc(client);
  217. struct imx074 *priv = to_imx074(client);
  218. return soc_camera_set_power(&client->dev, ssdd, priv->clk, on);
  219. }
  220. static int imx074_g_mbus_config(struct v4l2_subdev *sd,
  221. struct v4l2_mbus_config *cfg)
  222. {
  223. cfg->type = V4L2_MBUS_CSI2;
  224. cfg->flags = V4L2_MBUS_CSI2_2_LANE |
  225. V4L2_MBUS_CSI2_CHANNEL_0 |
  226. V4L2_MBUS_CSI2_CONTINUOUS_CLOCK;
  227. return 0;
  228. }
  229. static struct v4l2_subdev_video_ops imx074_subdev_video_ops = {
  230. .s_stream = imx074_s_stream,
  231. .g_crop = imx074_g_crop,
  232. .cropcap = imx074_cropcap,
  233. .g_mbus_config = imx074_g_mbus_config,
  234. };
  235. static struct v4l2_subdev_core_ops imx074_subdev_core_ops = {
  236. .s_power = imx074_s_power,
  237. };
  238. static const struct v4l2_subdev_pad_ops imx074_subdev_pad_ops = {
  239. .enum_mbus_code = imx074_enum_mbus_code,
  240. .get_fmt = imx074_get_fmt,
  241. .set_fmt = imx074_set_fmt,
  242. };
  243. static struct v4l2_subdev_ops imx074_subdev_ops = {
  244. .core = &imx074_subdev_core_ops,
  245. .video = &imx074_subdev_video_ops,
  246. .pad = &imx074_subdev_pad_ops,
  247. };
  248. static int imx074_video_probe(struct i2c_client *client)
  249. {
  250. struct v4l2_subdev *subdev = i2c_get_clientdata(client);
  251. int ret;
  252. u16 id;
  253. ret = imx074_s_power(subdev, 1);
  254. if (ret < 0)
  255. return ret;
  256. /* Read sensor Model ID */
  257. ret = reg_read(client, 0);
  258. if (ret < 0)
  259. goto done;
  260. id = ret << 8;
  261. ret = reg_read(client, 1);
  262. if (ret < 0)
  263. goto done;
  264. id |= ret;
  265. dev_info(&client->dev, "Chip ID 0x%04x detected\n", id);
  266. if (id != 0x74) {
  267. ret = -ENODEV;
  268. goto done;
  269. }
  270. /* PLL Setting EXTCLK=24MHz, 22.5times */
  271. reg_write(client, PLL_MULTIPLIER, 0x2D);
  272. reg_write(client, PRE_PLL_CLK_DIV, 0x02);
  273. reg_write(client, PLSTATIM, 0x4B);
  274. /* 2-lane mode */
  275. reg_write(client, 0x3024, 0x00);
  276. reg_write(client, IMAGE_ORIENTATION, 0x00);
  277. /* select RAW mode:
  278. * 0x08+0x08 = top 8 bits
  279. * 0x0a+0x08 = compressed 8-bits
  280. * 0x0a+0x0a = 10 bits
  281. */
  282. reg_write(client, 0x0112, 0x08);
  283. reg_write(client, 0x0113, 0x08);
  284. /* Base setting for High frame mode */
  285. reg_write(client, VNDMY_ABLMGSHLMT, 0x80);
  286. reg_write(client, Y_OPBADDR_START_DI, 0x08);
  287. reg_write(client, 0x3015, 0x37);
  288. reg_write(client, 0x301C, 0x01);
  289. reg_write(client, 0x302C, 0x05);
  290. reg_write(client, 0x3031, 0x26);
  291. reg_write(client, 0x3041, 0x60);
  292. reg_write(client, 0x3051, 0x24);
  293. reg_write(client, 0x3053, 0x34);
  294. reg_write(client, 0x3057, 0xC0);
  295. reg_write(client, 0x305C, 0x09);
  296. reg_write(client, 0x305D, 0x07);
  297. reg_write(client, 0x3060, 0x30);
  298. reg_write(client, 0x3065, 0x00);
  299. reg_write(client, 0x30AA, 0x08);
  300. reg_write(client, 0x30AB, 0x1C);
  301. reg_write(client, 0x30B0, 0x32);
  302. reg_write(client, 0x30B2, 0x83);
  303. reg_write(client, 0x30D3, 0x04);
  304. reg_write(client, 0x3106, 0x78);
  305. reg_write(client, 0x310C, 0x82);
  306. reg_write(client, 0x3304, 0x05);
  307. reg_write(client, 0x3305, 0x04);
  308. reg_write(client, 0x3306, 0x11);
  309. reg_write(client, 0x3307, 0x02);
  310. reg_write(client, 0x3308, 0x0C);
  311. reg_write(client, 0x3309, 0x06);
  312. reg_write(client, 0x330A, 0x08);
  313. reg_write(client, 0x330B, 0x04);
  314. reg_write(client, 0x330C, 0x08);
  315. reg_write(client, 0x330D, 0x06);
  316. reg_write(client, 0x330E, 0x01);
  317. reg_write(client, 0x3381, 0x00);
  318. /* V : 1/2V-addition (1,3), H : 1/2H-averaging (1,3) -> Full HD */
  319. /* 1608 = 1560 + 48 (black lines) */
  320. reg_write(client, FRAME_LENGTH_LINES_HI, 0x06);
  321. reg_write(client, FRAME_LENGTH_LINES_LO, 0x48);
  322. reg_write(client, YADDR_START, 0x00);
  323. reg_write(client, YADDR_END, 0x2F);
  324. /* 0x838 == 2104 */
  325. reg_write(client, X_OUTPUT_SIZE_MSB, 0x08);
  326. reg_write(client, X_OUTPUT_SIZE_LSB, 0x38);
  327. /* 0x618 == 1560 */
  328. reg_write(client, Y_OUTPUT_SIZE_MSB, 0x06);
  329. reg_write(client, Y_OUTPUT_SIZE_LSB, 0x18);
  330. reg_write(client, X_EVEN_INC, 0x01);
  331. reg_write(client, X_ODD_INC, 0x03);
  332. reg_write(client, Y_EVEN_INC, 0x01);
  333. reg_write(client, Y_ODD_INC, 0x03);
  334. reg_write(client, HMODEADD, 0x00);
  335. reg_write(client, VMODEADD, 0x16);
  336. reg_write(client, VAPPLINE_START, 0x24);
  337. reg_write(client, VAPPLINE_END, 0x53);
  338. reg_write(client, SHUTTER, 0x00);
  339. reg_write(client, HADDAVE, 0x80);
  340. reg_write(client, LANESEL, 0x00);
  341. reg_write(client, GROUPED_PARAMETER_HOLD, 0x00); /* off */
  342. ret = 0;
  343. done:
  344. imx074_s_power(subdev, 0);
  345. return ret;
  346. }
  347. static int imx074_probe(struct i2c_client *client,
  348. const struct i2c_device_id *did)
  349. {
  350. struct imx074 *priv;
  351. struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
  352. struct soc_camera_subdev_desc *ssdd = soc_camera_i2c_to_desc(client);
  353. int ret;
  354. if (!ssdd) {
  355. dev_err(&client->dev, "IMX074: missing platform data!\n");
  356. return -EINVAL;
  357. }
  358. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
  359. dev_warn(&adapter->dev,
  360. "I2C-Adapter doesn't support I2C_FUNC_SMBUS_BYTE\n");
  361. return -EIO;
  362. }
  363. priv = devm_kzalloc(&client->dev, sizeof(struct imx074), GFP_KERNEL);
  364. if (!priv)
  365. return -ENOMEM;
  366. v4l2_i2c_subdev_init(&priv->subdev, client, &imx074_subdev_ops);
  367. priv->fmt = &imx074_colour_fmts[0];
  368. priv->clk = v4l2_clk_get(&client->dev, "mclk");
  369. if (IS_ERR(priv->clk)) {
  370. dev_info(&client->dev, "Error %ld getting clock\n", PTR_ERR(priv->clk));
  371. return -EPROBE_DEFER;
  372. }
  373. ret = soc_camera_power_init(&client->dev, ssdd);
  374. if (ret < 0)
  375. goto epwrinit;
  376. ret = imx074_video_probe(client);
  377. if (ret < 0)
  378. goto eprobe;
  379. ret = v4l2_async_register_subdev(&priv->subdev);
  380. if (!ret)
  381. return 0;
  382. epwrinit:
  383. eprobe:
  384. v4l2_clk_put(priv->clk);
  385. return ret;
  386. }
  387. static int imx074_remove(struct i2c_client *client)
  388. {
  389. struct soc_camera_subdev_desc *ssdd = soc_camera_i2c_to_desc(client);
  390. struct imx074 *priv = to_imx074(client);
  391. v4l2_async_unregister_subdev(&priv->subdev);
  392. v4l2_clk_put(priv->clk);
  393. if (ssdd->free_bus)
  394. ssdd->free_bus(ssdd);
  395. return 0;
  396. }
  397. static const struct i2c_device_id imx074_id[] = {
  398. { "imx074", 0 },
  399. { }
  400. };
  401. MODULE_DEVICE_TABLE(i2c, imx074_id);
  402. static struct i2c_driver imx074_i2c_driver = {
  403. .driver = {
  404. .name = "imx074",
  405. },
  406. .probe = imx074_probe,
  407. .remove = imx074_remove,
  408. .id_table = imx074_id,
  409. };
  410. module_i2c_driver(imx074_i2c_driver);
  411. MODULE_DESCRIPTION("Sony IMX074 Camera driver");
  412. MODULE_AUTHOR("Guennadi Liakhovetski <g.liakhovetski@gmx.de>");
  413. MODULE_LICENSE("GPL v2");