imx074.c 12 KB

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