imx074.c 12 KB

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