ov9640.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746
  1. /*
  2. * OmniVision OV96xx Camera Driver
  3. *
  4. * Copyright (C) 2009 Marek Vasut <marek.vasut@gmail.com>
  5. *
  6. * Based on ov772x camera driver:
  7. *
  8. * Copyright (C) 2008 Renesas Solutions Corp.
  9. * Kuninori Morimoto <morimoto.kuninori@renesas.com>
  10. *
  11. * Based on ov7670 and soc_camera_platform driver,
  12. *
  13. * Copyright 2006-7 Jonathan Corbet <corbet@lwn.net>
  14. * Copyright (C) 2008 Magnus Damm
  15. * Copyright (C) 2008, Guennadi Liakhovetski <kernel@pengutronix.de>
  16. *
  17. * This program is free software; you can redistribute it and/or modify
  18. * it under the terms of the GNU General Public License version 2 as
  19. * published by the Free Software Foundation.
  20. */
  21. #include <linux/init.h>
  22. #include <linux/module.h>
  23. #include <linux/i2c.h>
  24. #include <linux/slab.h>
  25. #include <linux/delay.h>
  26. #include <linux/v4l2-mediabus.h>
  27. #include <linux/videodev2.h>
  28. #include <media/soc_camera.h>
  29. #include <media/v4l2-chip-ident.h>
  30. #include <media/v4l2-common.h>
  31. #include <media/v4l2-ctrls.h>
  32. #include "ov9640.h"
  33. #define to_ov9640_sensor(sd) container_of(sd, struct ov9640_priv, subdev)
  34. /* default register setup */
  35. static const struct ov9640_reg ov9640_regs_dflt[] = {
  36. { OV9640_COM5, OV9640_COM5_SYSCLK | OV9640_COM5_LONGEXP },
  37. { OV9640_COM6, OV9640_COM6_OPT_BLC | OV9640_COM6_ADBLC_BIAS |
  38. OV9640_COM6_FMT_RST | OV9640_COM6_ADBLC_OPTEN },
  39. { OV9640_PSHFT, OV9640_PSHFT_VAL(0x01) },
  40. { OV9640_ACOM, OV9640_ACOM_2X_ANALOG | OV9640_ACOM_RSVD },
  41. { OV9640_TSLB, OV9640_TSLB_YUYV_UYVY },
  42. { OV9640_COM16, OV9640_COM16_RB_AVG },
  43. /* Gamma curve P */
  44. { 0x6c, 0x40 }, { 0x6d, 0x30 }, { 0x6e, 0x4b }, { 0x6f, 0x60 },
  45. { 0x70, 0x70 }, { 0x71, 0x70 }, { 0x72, 0x70 }, { 0x73, 0x70 },
  46. { 0x74, 0x60 }, { 0x75, 0x60 }, { 0x76, 0x50 }, { 0x77, 0x48 },
  47. { 0x78, 0x3a }, { 0x79, 0x2e }, { 0x7a, 0x28 }, { 0x7b, 0x22 },
  48. /* Gamma curve T */
  49. { 0x7c, 0x04 }, { 0x7d, 0x07 }, { 0x7e, 0x10 }, { 0x7f, 0x28 },
  50. { 0x80, 0x36 }, { 0x81, 0x44 }, { 0x82, 0x52 }, { 0x83, 0x60 },
  51. { 0x84, 0x6c }, { 0x85, 0x78 }, { 0x86, 0x8c }, { 0x87, 0x9e },
  52. { 0x88, 0xbb }, { 0x89, 0xd2 }, { 0x8a, 0xe6 },
  53. };
  54. /* Configurations
  55. * NOTE: for YUV, alter the following registers:
  56. * COM12 |= OV9640_COM12_YUV_AVG
  57. *
  58. * for RGB, alter the following registers:
  59. * COM7 |= OV9640_COM7_RGB
  60. * COM13 |= OV9640_COM13_RGB_AVG
  61. * COM15 |= proper RGB color encoding mode
  62. */
  63. static const struct ov9640_reg ov9640_regs_qqcif[] = {
  64. { OV9640_CLKRC, OV9640_CLKRC_DPLL_EN | OV9640_CLKRC_DIV(0x0f) },
  65. { OV9640_COM1, OV9640_COM1_QQFMT | OV9640_COM1_HREF_2SKIP },
  66. { OV9640_COM4, OV9640_COM4_QQ_VP | OV9640_COM4_RSVD },
  67. { OV9640_COM7, OV9640_COM7_QCIF },
  68. { OV9640_COM12, OV9640_COM12_RSVD },
  69. { OV9640_COM13, OV9640_COM13_GAMMA_RAW | OV9640_COM13_MATRIX_EN },
  70. { OV9640_COM15, OV9640_COM15_OR_10F0 },
  71. };
  72. static const struct ov9640_reg ov9640_regs_qqvga[] = {
  73. { OV9640_CLKRC, OV9640_CLKRC_DPLL_EN | OV9640_CLKRC_DIV(0x07) },
  74. { OV9640_COM1, OV9640_COM1_QQFMT | OV9640_COM1_HREF_2SKIP },
  75. { OV9640_COM4, OV9640_COM4_QQ_VP | OV9640_COM4_RSVD },
  76. { OV9640_COM7, OV9640_COM7_QVGA },
  77. { OV9640_COM12, OV9640_COM12_RSVD },
  78. { OV9640_COM13, OV9640_COM13_GAMMA_RAW | OV9640_COM13_MATRIX_EN },
  79. { OV9640_COM15, OV9640_COM15_OR_10F0 },
  80. };
  81. static const struct ov9640_reg ov9640_regs_qcif[] = {
  82. { OV9640_CLKRC, OV9640_CLKRC_DPLL_EN | OV9640_CLKRC_DIV(0x07) },
  83. { OV9640_COM4, OV9640_COM4_QQ_VP | OV9640_COM4_RSVD },
  84. { OV9640_COM7, OV9640_COM7_QCIF },
  85. { OV9640_COM12, OV9640_COM12_RSVD },
  86. { OV9640_COM13, OV9640_COM13_GAMMA_RAW | OV9640_COM13_MATRIX_EN },
  87. { OV9640_COM15, OV9640_COM15_OR_10F0 },
  88. };
  89. static const struct ov9640_reg ov9640_regs_qvga[] = {
  90. { OV9640_CLKRC, OV9640_CLKRC_DPLL_EN | OV9640_CLKRC_DIV(0x03) },
  91. { OV9640_COM4, OV9640_COM4_QQ_VP | OV9640_COM4_RSVD },
  92. { OV9640_COM7, OV9640_COM7_QVGA },
  93. { OV9640_COM12, OV9640_COM12_RSVD },
  94. { OV9640_COM13, OV9640_COM13_GAMMA_RAW | OV9640_COM13_MATRIX_EN },
  95. { OV9640_COM15, OV9640_COM15_OR_10F0 },
  96. };
  97. static const struct ov9640_reg ov9640_regs_cif[] = {
  98. { OV9640_CLKRC, OV9640_CLKRC_DPLL_EN | OV9640_CLKRC_DIV(0x03) },
  99. { OV9640_COM3, OV9640_COM3_VP },
  100. { OV9640_COM7, OV9640_COM7_CIF },
  101. { OV9640_COM12, OV9640_COM12_RSVD },
  102. { OV9640_COM13, OV9640_COM13_GAMMA_RAW | OV9640_COM13_MATRIX_EN },
  103. { OV9640_COM15, OV9640_COM15_OR_10F0 },
  104. };
  105. static const struct ov9640_reg ov9640_regs_vga[] = {
  106. { OV9640_CLKRC, OV9640_CLKRC_DPLL_EN | OV9640_CLKRC_DIV(0x01) },
  107. { OV9640_COM3, OV9640_COM3_VP },
  108. { OV9640_COM7, OV9640_COM7_VGA },
  109. { OV9640_COM12, OV9640_COM12_RSVD },
  110. { OV9640_COM13, OV9640_COM13_GAMMA_RAW | OV9640_COM13_MATRIX_EN },
  111. { OV9640_COM15, OV9640_COM15_OR_10F0 },
  112. };
  113. static const struct ov9640_reg ov9640_regs_sxga[] = {
  114. { OV9640_CLKRC, OV9640_CLKRC_DPLL_EN | OV9640_CLKRC_DIV(0x01) },
  115. { OV9640_COM3, OV9640_COM3_VP },
  116. { OV9640_COM7, 0 },
  117. { OV9640_COM12, OV9640_COM12_RSVD },
  118. { OV9640_COM13, OV9640_COM13_GAMMA_RAW | OV9640_COM13_MATRIX_EN },
  119. { OV9640_COM15, OV9640_COM15_OR_10F0 },
  120. };
  121. static const struct ov9640_reg ov9640_regs_yuv[] = {
  122. { OV9640_MTX1, 0x58 },
  123. { OV9640_MTX2, 0x48 },
  124. { OV9640_MTX3, 0x10 },
  125. { OV9640_MTX4, 0x28 },
  126. { OV9640_MTX5, 0x48 },
  127. { OV9640_MTX6, 0x70 },
  128. { OV9640_MTX7, 0x40 },
  129. { OV9640_MTX8, 0x40 },
  130. { OV9640_MTX9, 0x40 },
  131. { OV9640_MTXS, 0x0f },
  132. };
  133. static const struct ov9640_reg ov9640_regs_rgb[] = {
  134. { OV9640_MTX1, 0x71 },
  135. { OV9640_MTX2, 0x3e },
  136. { OV9640_MTX3, 0x0c },
  137. { OV9640_MTX4, 0x33 },
  138. { OV9640_MTX5, 0x72 },
  139. { OV9640_MTX6, 0x00 },
  140. { OV9640_MTX7, 0x2b },
  141. { OV9640_MTX8, 0x66 },
  142. { OV9640_MTX9, 0xd2 },
  143. { OV9640_MTXS, 0x65 },
  144. };
  145. static enum v4l2_mbus_pixelcode ov9640_codes[] = {
  146. V4L2_MBUS_FMT_UYVY8_2X8,
  147. V4L2_MBUS_FMT_RGB555_2X8_PADHI_LE,
  148. V4L2_MBUS_FMT_RGB565_2X8_LE,
  149. };
  150. /* read a register */
  151. static int ov9640_reg_read(struct i2c_client *client, u8 reg, u8 *val)
  152. {
  153. int ret;
  154. u8 data = reg;
  155. struct i2c_msg msg = {
  156. .addr = client->addr,
  157. .flags = 0,
  158. .len = 1,
  159. .buf = &data,
  160. };
  161. ret = i2c_transfer(client->adapter, &msg, 1);
  162. if (ret < 0)
  163. goto err;
  164. msg.flags = I2C_M_RD;
  165. ret = i2c_transfer(client->adapter, &msg, 1);
  166. if (ret < 0)
  167. goto err;
  168. *val = data;
  169. return 0;
  170. err:
  171. dev_err(&client->dev, "Failed reading register 0x%02x!\n", reg);
  172. return ret;
  173. }
  174. /* write a register */
  175. static int ov9640_reg_write(struct i2c_client *client, u8 reg, u8 val)
  176. {
  177. int ret;
  178. u8 _val;
  179. unsigned char data[2] = { reg, val };
  180. struct i2c_msg msg = {
  181. .addr = client->addr,
  182. .flags = 0,
  183. .len = 2,
  184. .buf = data,
  185. };
  186. ret = i2c_transfer(client->adapter, &msg, 1);
  187. if (ret < 0) {
  188. dev_err(&client->dev, "Failed writing register 0x%02x!\n", reg);
  189. return ret;
  190. }
  191. /* we have to read the register back ... no idea why, maybe HW bug */
  192. ret = ov9640_reg_read(client, reg, &_val);
  193. if (ret)
  194. dev_err(&client->dev,
  195. "Failed reading back register 0x%02x!\n", reg);
  196. return 0;
  197. }
  198. /* Read a register, alter its bits, write it back */
  199. static int ov9640_reg_rmw(struct i2c_client *client, u8 reg, u8 set, u8 unset)
  200. {
  201. u8 val;
  202. int ret;
  203. ret = ov9640_reg_read(client, reg, &val);
  204. if (ret) {
  205. dev_err(&client->dev,
  206. "[Read]-Modify-Write of register %02x failed!\n", reg);
  207. return val;
  208. }
  209. val |= set;
  210. val &= ~unset;
  211. ret = ov9640_reg_write(client, reg, val);
  212. if (ret)
  213. dev_err(&client->dev,
  214. "Read-Modify-[Write] of register %02x failed!\n", reg);
  215. return ret;
  216. }
  217. /* Soft reset the camera. This has nothing to do with the RESET pin! */
  218. static int ov9640_reset(struct i2c_client *client)
  219. {
  220. int ret;
  221. ret = ov9640_reg_write(client, OV9640_COM7, OV9640_COM7_SCCB_RESET);
  222. if (ret)
  223. dev_err(&client->dev,
  224. "An error occurred while entering soft reset!\n");
  225. return ret;
  226. }
  227. /* Start/Stop streaming from the device */
  228. static int ov9640_s_stream(struct v4l2_subdev *sd, int enable)
  229. {
  230. return 0;
  231. }
  232. /* Set status of additional camera capabilities */
  233. static int ov9640_s_ctrl(struct v4l2_ctrl *ctrl)
  234. {
  235. struct ov9640_priv *priv = container_of(ctrl->handler, struct ov9640_priv, hdl);
  236. struct i2c_client *client = v4l2_get_subdevdata(&priv->subdev);
  237. switch (ctrl->id) {
  238. case V4L2_CID_VFLIP:
  239. if (ctrl->val)
  240. return ov9640_reg_rmw(client, OV9640_MVFP,
  241. OV9640_MVFP_V, 0);
  242. return ov9640_reg_rmw(client, OV9640_MVFP, 0, OV9640_MVFP_V);
  243. case V4L2_CID_HFLIP:
  244. if (ctrl->val)
  245. return ov9640_reg_rmw(client, OV9640_MVFP,
  246. OV9640_MVFP_H, 0);
  247. return ov9640_reg_rmw(client, OV9640_MVFP, 0, OV9640_MVFP_H);
  248. }
  249. return -EINVAL;
  250. }
  251. /* Get chip identification */
  252. static int ov9640_g_chip_ident(struct v4l2_subdev *sd,
  253. struct v4l2_dbg_chip_ident *id)
  254. {
  255. struct ov9640_priv *priv = to_ov9640_sensor(sd);
  256. id->ident = priv->model;
  257. id->revision = priv->revision;
  258. return 0;
  259. }
  260. #ifdef CONFIG_VIDEO_ADV_DEBUG
  261. static int ov9640_get_register(struct v4l2_subdev *sd,
  262. struct v4l2_dbg_register *reg)
  263. {
  264. struct i2c_client *client = v4l2_get_subdevdata(sd);
  265. int ret;
  266. u8 val;
  267. if (reg->reg & ~0xff)
  268. return -EINVAL;
  269. reg->size = 1;
  270. ret = ov9640_reg_read(client, reg->reg, &val);
  271. if (ret)
  272. return ret;
  273. reg->val = (__u64)val;
  274. return 0;
  275. }
  276. static int ov9640_set_register(struct v4l2_subdev *sd,
  277. struct v4l2_dbg_register *reg)
  278. {
  279. struct i2c_client *client = v4l2_get_subdevdata(sd);
  280. if (reg->reg & ~0xff || reg->val & ~0xff)
  281. return -EINVAL;
  282. return ov9640_reg_write(client, reg->reg, reg->val);
  283. }
  284. #endif
  285. /* select nearest higher resolution for capture */
  286. static void ov9640_res_roundup(u32 *width, u32 *height)
  287. {
  288. int i;
  289. enum { QQCIF, QQVGA, QCIF, QVGA, CIF, VGA, SXGA };
  290. int res_x[] = { 88, 160, 176, 320, 352, 640, 1280 };
  291. int res_y[] = { 72, 120, 144, 240, 288, 480, 960 };
  292. for (i = 0; i < ARRAY_SIZE(res_x); i++) {
  293. if (res_x[i] >= *width && res_y[i] >= *height) {
  294. *width = res_x[i];
  295. *height = res_y[i];
  296. return;
  297. }
  298. }
  299. *width = res_x[SXGA];
  300. *height = res_y[SXGA];
  301. }
  302. /* Prepare necessary register changes depending on color encoding */
  303. static void ov9640_alter_regs(enum v4l2_mbus_pixelcode code,
  304. struct ov9640_reg_alt *alt)
  305. {
  306. switch (code) {
  307. default:
  308. case V4L2_MBUS_FMT_UYVY8_2X8:
  309. alt->com12 = OV9640_COM12_YUV_AVG;
  310. alt->com13 = OV9640_COM13_Y_DELAY_EN |
  311. OV9640_COM13_YUV_DLY(0x01);
  312. break;
  313. case V4L2_MBUS_FMT_RGB555_2X8_PADHI_LE:
  314. alt->com7 = OV9640_COM7_RGB;
  315. alt->com13 = OV9640_COM13_RGB_AVG;
  316. alt->com15 = OV9640_COM15_RGB_555;
  317. break;
  318. case V4L2_MBUS_FMT_RGB565_2X8_LE:
  319. alt->com7 = OV9640_COM7_RGB;
  320. alt->com13 = OV9640_COM13_RGB_AVG;
  321. alt->com15 = OV9640_COM15_RGB_565;
  322. break;
  323. };
  324. }
  325. /* Setup registers according to resolution and color encoding */
  326. static int ov9640_write_regs(struct i2c_client *client, u32 width,
  327. enum v4l2_mbus_pixelcode code, struct ov9640_reg_alt *alts)
  328. {
  329. const struct ov9640_reg *ov9640_regs, *matrix_regs;
  330. int ov9640_regs_len, matrix_regs_len;
  331. int i, ret;
  332. u8 val;
  333. /* select register configuration for given resolution */
  334. switch (width) {
  335. case W_QQCIF:
  336. ov9640_regs = ov9640_regs_qqcif;
  337. ov9640_regs_len = ARRAY_SIZE(ov9640_regs_qqcif);
  338. break;
  339. case W_QQVGA:
  340. ov9640_regs = ov9640_regs_qqvga;
  341. ov9640_regs_len = ARRAY_SIZE(ov9640_regs_qqvga);
  342. break;
  343. case W_QCIF:
  344. ov9640_regs = ov9640_regs_qcif;
  345. ov9640_regs_len = ARRAY_SIZE(ov9640_regs_qcif);
  346. break;
  347. case W_QVGA:
  348. ov9640_regs = ov9640_regs_qvga;
  349. ov9640_regs_len = ARRAY_SIZE(ov9640_regs_qvga);
  350. break;
  351. case W_CIF:
  352. ov9640_regs = ov9640_regs_cif;
  353. ov9640_regs_len = ARRAY_SIZE(ov9640_regs_cif);
  354. break;
  355. case W_VGA:
  356. ov9640_regs = ov9640_regs_vga;
  357. ov9640_regs_len = ARRAY_SIZE(ov9640_regs_vga);
  358. break;
  359. case W_SXGA:
  360. ov9640_regs = ov9640_regs_sxga;
  361. ov9640_regs_len = ARRAY_SIZE(ov9640_regs_sxga);
  362. break;
  363. default:
  364. dev_err(&client->dev, "Failed to select resolution!\n");
  365. return -EINVAL;
  366. }
  367. /* select color matrix configuration for given color encoding */
  368. if (code == V4L2_MBUS_FMT_UYVY8_2X8) {
  369. matrix_regs = ov9640_regs_yuv;
  370. matrix_regs_len = ARRAY_SIZE(ov9640_regs_yuv);
  371. } else {
  372. matrix_regs = ov9640_regs_rgb;
  373. matrix_regs_len = ARRAY_SIZE(ov9640_regs_rgb);
  374. }
  375. /* write register settings into the module */
  376. for (i = 0; i < ov9640_regs_len; i++) {
  377. val = ov9640_regs[i].val;
  378. switch (ov9640_regs[i].reg) {
  379. case OV9640_COM7:
  380. val |= alts->com7;
  381. break;
  382. case OV9640_COM12:
  383. val |= alts->com12;
  384. break;
  385. case OV9640_COM13:
  386. val |= alts->com13;
  387. break;
  388. case OV9640_COM15:
  389. val |= alts->com15;
  390. break;
  391. }
  392. ret = ov9640_reg_write(client, ov9640_regs[i].reg, val);
  393. if (ret)
  394. return ret;
  395. }
  396. /* write color matrix configuration into the module */
  397. for (i = 0; i < matrix_regs_len; i++) {
  398. ret = ov9640_reg_write(client, matrix_regs[i].reg,
  399. matrix_regs[i].val);
  400. if (ret)
  401. return ret;
  402. }
  403. return 0;
  404. }
  405. /* program default register values */
  406. static int ov9640_prog_dflt(struct i2c_client *client)
  407. {
  408. int i, ret;
  409. for (i = 0; i < ARRAY_SIZE(ov9640_regs_dflt); i++) {
  410. ret = ov9640_reg_write(client, ov9640_regs_dflt[i].reg,
  411. ov9640_regs_dflt[i].val);
  412. if (ret)
  413. return ret;
  414. }
  415. /* wait for the changes to actually happen, 140ms are not enough yet */
  416. mdelay(150);
  417. return 0;
  418. }
  419. /* set the format we will capture in */
  420. static int ov9640_s_fmt(struct v4l2_subdev *sd,
  421. struct v4l2_mbus_framefmt *mf)
  422. {
  423. struct i2c_client *client = v4l2_get_subdevdata(sd);
  424. struct ov9640_reg_alt alts = {0};
  425. enum v4l2_colorspace cspace;
  426. enum v4l2_mbus_pixelcode code = mf->code;
  427. int ret;
  428. ov9640_res_roundup(&mf->width, &mf->height);
  429. ov9640_alter_regs(mf->code, &alts);
  430. ov9640_reset(client);
  431. ret = ov9640_prog_dflt(client);
  432. if (ret)
  433. return ret;
  434. switch (code) {
  435. case V4L2_MBUS_FMT_RGB555_2X8_PADHI_LE:
  436. case V4L2_MBUS_FMT_RGB565_2X8_LE:
  437. cspace = V4L2_COLORSPACE_SRGB;
  438. break;
  439. default:
  440. code = V4L2_MBUS_FMT_UYVY8_2X8;
  441. case V4L2_MBUS_FMT_UYVY8_2X8:
  442. cspace = V4L2_COLORSPACE_JPEG;
  443. }
  444. ret = ov9640_write_regs(client, mf->width, code, &alts);
  445. if (!ret) {
  446. mf->code = code;
  447. mf->colorspace = cspace;
  448. }
  449. return ret;
  450. }
  451. static int ov9640_try_fmt(struct v4l2_subdev *sd,
  452. struct v4l2_mbus_framefmt *mf)
  453. {
  454. ov9640_res_roundup(&mf->width, &mf->height);
  455. mf->field = V4L2_FIELD_NONE;
  456. switch (mf->code) {
  457. case V4L2_MBUS_FMT_RGB555_2X8_PADHI_LE:
  458. case V4L2_MBUS_FMT_RGB565_2X8_LE:
  459. mf->colorspace = V4L2_COLORSPACE_SRGB;
  460. break;
  461. default:
  462. mf->code = V4L2_MBUS_FMT_UYVY8_2X8;
  463. case V4L2_MBUS_FMT_UYVY8_2X8:
  464. mf->colorspace = V4L2_COLORSPACE_JPEG;
  465. }
  466. return 0;
  467. }
  468. static int ov9640_enum_fmt(struct v4l2_subdev *sd, unsigned int index,
  469. enum v4l2_mbus_pixelcode *code)
  470. {
  471. if (index >= ARRAY_SIZE(ov9640_codes))
  472. return -EINVAL;
  473. *code = ov9640_codes[index];
  474. return 0;
  475. }
  476. static int ov9640_g_crop(struct v4l2_subdev *sd, struct v4l2_crop *a)
  477. {
  478. a->c.left = 0;
  479. a->c.top = 0;
  480. a->c.width = W_SXGA;
  481. a->c.height = H_SXGA;
  482. a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  483. return 0;
  484. }
  485. static int ov9640_cropcap(struct v4l2_subdev *sd, struct v4l2_cropcap *a)
  486. {
  487. a->bounds.left = 0;
  488. a->bounds.top = 0;
  489. a->bounds.width = W_SXGA;
  490. a->bounds.height = H_SXGA;
  491. a->defrect = a->bounds;
  492. a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  493. a->pixelaspect.numerator = 1;
  494. a->pixelaspect.denominator = 1;
  495. return 0;
  496. }
  497. static int ov9640_video_probe(struct i2c_client *client)
  498. {
  499. struct v4l2_subdev *sd = i2c_get_clientdata(client);
  500. struct ov9640_priv *priv = to_ov9640_sensor(sd);
  501. u8 pid, ver, midh, midl;
  502. const char *devname;
  503. int ret = 0;
  504. /*
  505. * check and show product ID and manufacturer ID
  506. */
  507. ret = ov9640_reg_read(client, OV9640_PID, &pid);
  508. if (!ret)
  509. ret = ov9640_reg_read(client, OV9640_VER, &ver);
  510. if (!ret)
  511. ret = ov9640_reg_read(client, OV9640_MIDH, &midh);
  512. if (!ret)
  513. ret = ov9640_reg_read(client, OV9640_MIDL, &midl);
  514. if (ret)
  515. return ret;
  516. switch (VERSION(pid, ver)) {
  517. case OV9640_V2:
  518. devname = "ov9640";
  519. priv->model = V4L2_IDENT_OV9640;
  520. priv->revision = 2;
  521. case OV9640_V3:
  522. devname = "ov9640";
  523. priv->model = V4L2_IDENT_OV9640;
  524. priv->revision = 3;
  525. break;
  526. default:
  527. dev_err(&client->dev, "Product ID error %x:%x\n", pid, ver);
  528. return -ENODEV;
  529. }
  530. dev_info(&client->dev, "%s Product ID %0x:%0x Manufacturer ID %x:%x\n",
  531. devname, pid, ver, midh, midl);
  532. return v4l2_ctrl_handler_setup(&priv->hdl);
  533. }
  534. static const struct v4l2_ctrl_ops ov9640_ctrl_ops = {
  535. .s_ctrl = ov9640_s_ctrl,
  536. };
  537. static struct v4l2_subdev_core_ops ov9640_core_ops = {
  538. .g_chip_ident = ov9640_g_chip_ident,
  539. #ifdef CONFIG_VIDEO_ADV_DEBUG
  540. .g_register = ov9640_get_register,
  541. .s_register = ov9640_set_register,
  542. #endif
  543. };
  544. /* Request bus settings on camera side */
  545. static int ov9640_g_mbus_config(struct v4l2_subdev *sd,
  546. struct v4l2_mbus_config *cfg)
  547. {
  548. struct i2c_client *client = v4l2_get_subdevdata(sd);
  549. struct soc_camera_link *icl = soc_camera_i2c_to_link(client);
  550. cfg->flags = V4L2_MBUS_PCLK_SAMPLE_RISING | V4L2_MBUS_MASTER |
  551. V4L2_MBUS_VSYNC_ACTIVE_HIGH | V4L2_MBUS_HSYNC_ACTIVE_HIGH |
  552. V4L2_MBUS_DATA_ACTIVE_HIGH;
  553. cfg->type = V4L2_MBUS_PARALLEL;
  554. cfg->flags = soc_camera_apply_board_flags(icl, cfg);
  555. return 0;
  556. }
  557. static struct v4l2_subdev_video_ops ov9640_video_ops = {
  558. .s_stream = ov9640_s_stream,
  559. .s_mbus_fmt = ov9640_s_fmt,
  560. .try_mbus_fmt = ov9640_try_fmt,
  561. .enum_mbus_fmt = ov9640_enum_fmt,
  562. .cropcap = ov9640_cropcap,
  563. .g_crop = ov9640_g_crop,
  564. .g_mbus_config = ov9640_g_mbus_config,
  565. };
  566. static struct v4l2_subdev_ops ov9640_subdev_ops = {
  567. .core = &ov9640_core_ops,
  568. .video = &ov9640_video_ops,
  569. };
  570. /*
  571. * i2c_driver function
  572. */
  573. static int ov9640_probe(struct i2c_client *client,
  574. const struct i2c_device_id *did)
  575. {
  576. struct ov9640_priv *priv;
  577. struct soc_camera_link *icl = soc_camera_i2c_to_link(client);
  578. int ret;
  579. if (!icl) {
  580. dev_err(&client->dev, "Missing platform_data for driver\n");
  581. return -EINVAL;
  582. }
  583. priv = kzalloc(sizeof(struct ov9640_priv), GFP_KERNEL);
  584. if (!priv) {
  585. dev_err(&client->dev,
  586. "Failed to allocate memory for private data!\n");
  587. return -ENOMEM;
  588. }
  589. v4l2_i2c_subdev_init(&priv->subdev, client, &ov9640_subdev_ops);
  590. v4l2_ctrl_handler_init(&priv->hdl, 2);
  591. v4l2_ctrl_new_std(&priv->hdl, &ov9640_ctrl_ops,
  592. V4L2_CID_VFLIP, 0, 1, 1, 0);
  593. v4l2_ctrl_new_std(&priv->hdl, &ov9640_ctrl_ops,
  594. V4L2_CID_HFLIP, 0, 1, 1, 0);
  595. priv->subdev.ctrl_handler = &priv->hdl;
  596. if (priv->hdl.error) {
  597. int err = priv->hdl.error;
  598. kfree(priv);
  599. return err;
  600. }
  601. ret = ov9640_video_probe(client);
  602. if (ret) {
  603. v4l2_ctrl_handler_free(&priv->hdl);
  604. kfree(priv);
  605. }
  606. return ret;
  607. }
  608. static int ov9640_remove(struct i2c_client *client)
  609. {
  610. struct v4l2_subdev *sd = i2c_get_clientdata(client);
  611. struct ov9640_priv *priv = to_ov9640_sensor(sd);
  612. v4l2_device_unregister_subdev(&priv->subdev);
  613. v4l2_ctrl_handler_free(&priv->hdl);
  614. kfree(priv);
  615. return 0;
  616. }
  617. static const struct i2c_device_id ov9640_id[] = {
  618. { "ov9640", 0 },
  619. { }
  620. };
  621. MODULE_DEVICE_TABLE(i2c, ov9640_id);
  622. static struct i2c_driver ov9640_i2c_driver = {
  623. .driver = {
  624. .name = "ov9640",
  625. },
  626. .probe = ov9640_probe,
  627. .remove = ov9640_remove,
  628. .id_table = ov9640_id,
  629. };
  630. module_i2c_driver(ov9640_i2c_driver);
  631. MODULE_DESCRIPTION("SoC Camera driver for OmniVision OV96xx");
  632. MODULE_AUTHOR("Marek Vasut <marek.vasut@gmail.com>");
  633. MODULE_LICENSE("GPL v2");