ov9640.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801
  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/videodev2.h>
  27. #include <media/v4l2-chip-ident.h>
  28. #include <media/v4l2-common.h>
  29. #include <media/soc_camera.h>
  30. #include "ov9640.h"
  31. /* default register setup */
  32. static const struct ov9640_reg ov9640_regs_dflt[] = {
  33. { OV9640_COM5, OV9640_COM5_SYSCLK | OV9640_COM5_LONGEXP },
  34. { OV9640_COM6, OV9640_COM6_OPT_BLC | OV9640_COM6_ADBLC_BIAS |
  35. OV9640_COM6_FMT_RST | OV9640_COM6_ADBLC_OPTEN },
  36. { OV9640_PSHFT, OV9640_PSHFT_VAL(0x01) },
  37. { OV9640_ACOM, OV9640_ACOM_2X_ANALOG | OV9640_ACOM_RSVD },
  38. { OV9640_TSLB, OV9640_TSLB_YUYV_UYVY },
  39. { OV9640_COM16, OV9640_COM16_RB_AVG },
  40. /* Gamma curve P */
  41. { 0x6c, 0x40 }, { 0x6d, 0x30 }, { 0x6e, 0x4b }, { 0x6f, 0x60 },
  42. { 0x70, 0x70 }, { 0x71, 0x70 }, { 0x72, 0x70 }, { 0x73, 0x70 },
  43. { 0x74, 0x60 }, { 0x75, 0x60 }, { 0x76, 0x50 }, { 0x77, 0x48 },
  44. { 0x78, 0x3a }, { 0x79, 0x2e }, { 0x7a, 0x28 }, { 0x7b, 0x22 },
  45. /* Gamma curve T */
  46. { 0x7c, 0x04 }, { 0x7d, 0x07 }, { 0x7e, 0x10 }, { 0x7f, 0x28 },
  47. { 0x80, 0x36 }, { 0x81, 0x44 }, { 0x82, 0x52 }, { 0x83, 0x60 },
  48. { 0x84, 0x6c }, { 0x85, 0x78 }, { 0x86, 0x8c }, { 0x87, 0x9e },
  49. { 0x88, 0xbb }, { 0x89, 0xd2 }, { 0x8a, 0xe6 },
  50. };
  51. /* Configurations
  52. * NOTE: for YUV, alter the following registers:
  53. * COM12 |= OV9640_COM12_YUV_AVG
  54. *
  55. * for RGB, alter the following registers:
  56. * COM7 |= OV9640_COM7_RGB
  57. * COM13 |= OV9640_COM13_RGB_AVG
  58. * COM15 |= proper RGB color encoding mode
  59. */
  60. static const struct ov9640_reg ov9640_regs_qqcif[] = {
  61. { OV9640_CLKRC, OV9640_CLKRC_DPLL_EN | OV9640_CLKRC_DIV(0x0f) },
  62. { OV9640_COM1, OV9640_COM1_QQFMT | OV9640_COM1_HREF_2SKIP },
  63. { OV9640_COM4, OV9640_COM4_QQ_VP | OV9640_COM4_RSVD },
  64. { OV9640_COM7, OV9640_COM7_QCIF },
  65. { OV9640_COM12, OV9640_COM12_RSVD },
  66. { OV9640_COM13, OV9640_COM13_GAMMA_RAW | OV9640_COM13_MATRIX_EN },
  67. { OV9640_COM15, OV9640_COM15_OR_10F0 },
  68. };
  69. static const struct ov9640_reg ov9640_regs_qqvga[] = {
  70. { OV9640_CLKRC, OV9640_CLKRC_DPLL_EN | OV9640_CLKRC_DIV(0x07) },
  71. { OV9640_COM1, OV9640_COM1_QQFMT | OV9640_COM1_HREF_2SKIP },
  72. { OV9640_COM4, OV9640_COM4_QQ_VP | OV9640_COM4_RSVD },
  73. { OV9640_COM7, OV9640_COM7_QVGA },
  74. { OV9640_COM12, OV9640_COM12_RSVD },
  75. { OV9640_COM13, OV9640_COM13_GAMMA_RAW | OV9640_COM13_MATRIX_EN },
  76. { OV9640_COM15, OV9640_COM15_OR_10F0 },
  77. };
  78. static const struct ov9640_reg ov9640_regs_qcif[] = {
  79. { OV9640_CLKRC, OV9640_CLKRC_DPLL_EN | OV9640_CLKRC_DIV(0x07) },
  80. { OV9640_COM4, OV9640_COM4_QQ_VP | OV9640_COM4_RSVD },
  81. { OV9640_COM7, OV9640_COM7_QCIF },
  82. { OV9640_COM12, OV9640_COM12_RSVD },
  83. { OV9640_COM13, OV9640_COM13_GAMMA_RAW | OV9640_COM13_MATRIX_EN },
  84. { OV9640_COM15, OV9640_COM15_OR_10F0 },
  85. };
  86. static const struct ov9640_reg ov9640_regs_qvga[] = {
  87. { OV9640_CLKRC, OV9640_CLKRC_DPLL_EN | OV9640_CLKRC_DIV(0x03) },
  88. { OV9640_COM4, OV9640_COM4_QQ_VP | OV9640_COM4_RSVD },
  89. { OV9640_COM7, OV9640_COM7_QVGA },
  90. { OV9640_COM12, OV9640_COM12_RSVD },
  91. { OV9640_COM13, OV9640_COM13_GAMMA_RAW | OV9640_COM13_MATRIX_EN },
  92. { OV9640_COM15, OV9640_COM15_OR_10F0 },
  93. };
  94. static const struct ov9640_reg ov9640_regs_cif[] = {
  95. { OV9640_CLKRC, OV9640_CLKRC_DPLL_EN | OV9640_CLKRC_DIV(0x03) },
  96. { OV9640_COM3, OV9640_COM3_VP },
  97. { OV9640_COM7, OV9640_COM7_CIF },
  98. { OV9640_COM12, OV9640_COM12_RSVD },
  99. { OV9640_COM13, OV9640_COM13_GAMMA_RAW | OV9640_COM13_MATRIX_EN },
  100. { OV9640_COM15, OV9640_COM15_OR_10F0 },
  101. };
  102. static const struct ov9640_reg ov9640_regs_vga[] = {
  103. { OV9640_CLKRC, OV9640_CLKRC_DPLL_EN | OV9640_CLKRC_DIV(0x01) },
  104. { OV9640_COM3, OV9640_COM3_VP },
  105. { OV9640_COM7, OV9640_COM7_VGA },
  106. { OV9640_COM12, OV9640_COM12_RSVD },
  107. { OV9640_COM13, OV9640_COM13_GAMMA_RAW | OV9640_COM13_MATRIX_EN },
  108. { OV9640_COM15, OV9640_COM15_OR_10F0 },
  109. };
  110. static const struct ov9640_reg ov9640_regs_sxga[] = {
  111. { OV9640_CLKRC, OV9640_CLKRC_DPLL_EN | OV9640_CLKRC_DIV(0x01) },
  112. { OV9640_COM3, OV9640_COM3_VP },
  113. { OV9640_COM7, 0 },
  114. { OV9640_COM12, OV9640_COM12_RSVD },
  115. { OV9640_COM13, OV9640_COM13_GAMMA_RAW | OV9640_COM13_MATRIX_EN },
  116. { OV9640_COM15, OV9640_COM15_OR_10F0 },
  117. };
  118. static const struct ov9640_reg ov9640_regs_yuv[] = {
  119. { OV9640_MTX1, 0x58 },
  120. { OV9640_MTX2, 0x48 },
  121. { OV9640_MTX3, 0x10 },
  122. { OV9640_MTX4, 0x28 },
  123. { OV9640_MTX5, 0x48 },
  124. { OV9640_MTX6, 0x70 },
  125. { OV9640_MTX7, 0x40 },
  126. { OV9640_MTX8, 0x40 },
  127. { OV9640_MTX9, 0x40 },
  128. { OV9640_MTXS, 0x0f },
  129. };
  130. static const struct ov9640_reg ov9640_regs_rgb[] = {
  131. { OV9640_MTX1, 0x71 },
  132. { OV9640_MTX2, 0x3e },
  133. { OV9640_MTX3, 0x0c },
  134. { OV9640_MTX4, 0x33 },
  135. { OV9640_MTX5, 0x72 },
  136. { OV9640_MTX6, 0x00 },
  137. { OV9640_MTX7, 0x2b },
  138. { OV9640_MTX8, 0x66 },
  139. { OV9640_MTX9, 0xd2 },
  140. { OV9640_MTXS, 0x65 },
  141. };
  142. /*
  143. * TODO: this sensor also supports RGB555 and RGB565 formats, but support for
  144. * them has not yet been sufficiently tested and so it is not included with
  145. * this version of the driver. To test and debug these formats add two entries
  146. * to the below array, see ov722x.c for an example.
  147. */
  148. static const struct soc_camera_data_format ov9640_fmt_lists[] = {
  149. {
  150. .name = "UYVY",
  151. .fourcc = V4L2_PIX_FMT_UYVY,
  152. .depth = 16,
  153. .colorspace = V4L2_COLORSPACE_JPEG,
  154. },
  155. };
  156. static const struct v4l2_queryctrl ov9640_controls[] = {
  157. {
  158. .id = V4L2_CID_VFLIP,
  159. .type = V4L2_CTRL_TYPE_BOOLEAN,
  160. .name = "Flip Vertically",
  161. .minimum = 0,
  162. .maximum = 1,
  163. .step = 1,
  164. .default_value = 0,
  165. },
  166. {
  167. .id = V4L2_CID_HFLIP,
  168. .type = V4L2_CTRL_TYPE_BOOLEAN,
  169. .name = "Flip Horizontally",
  170. .minimum = 0,
  171. .maximum = 1,
  172. .step = 1,
  173. .default_value = 0,
  174. },
  175. };
  176. /* read a register */
  177. static int ov9640_reg_read(struct i2c_client *client, u8 reg, u8 *val)
  178. {
  179. int ret;
  180. u8 data = reg;
  181. struct i2c_msg msg = {
  182. .addr = client->addr,
  183. .flags = 0,
  184. .len = 1,
  185. .buf = &data,
  186. };
  187. ret = i2c_transfer(client->adapter, &msg, 1);
  188. if (ret < 0)
  189. goto err;
  190. msg.flags = I2C_M_RD;
  191. ret = i2c_transfer(client->adapter, &msg, 1);
  192. if (ret < 0)
  193. goto err;
  194. *val = data;
  195. return 0;
  196. err:
  197. dev_err(&client->dev, "Failed reading register 0x%02x!\n", reg);
  198. return ret;
  199. }
  200. /* write a register */
  201. static int ov9640_reg_write(struct i2c_client *client, u8 reg, u8 val)
  202. {
  203. int ret;
  204. u8 _val;
  205. unsigned char data[2] = { reg, val };
  206. struct i2c_msg msg = {
  207. .addr = client->addr,
  208. .flags = 0,
  209. .len = 2,
  210. .buf = data,
  211. };
  212. ret = i2c_transfer(client->adapter, &msg, 1);
  213. if (ret < 0) {
  214. dev_err(&client->dev, "Failed writing register 0x%02x!\n", reg);
  215. return ret;
  216. }
  217. /* we have to read the register back ... no idea why, maybe HW bug */
  218. ret = ov9640_reg_read(client, reg, &_val);
  219. if (ret)
  220. dev_err(&client->dev,
  221. "Failed reading back register 0x%02x!\n", reg);
  222. return 0;
  223. }
  224. /* Read a register, alter its bits, write it back */
  225. static int ov9640_reg_rmw(struct i2c_client *client, u8 reg, u8 set, u8 unset)
  226. {
  227. u8 val;
  228. int ret;
  229. ret = ov9640_reg_read(client, reg, &val);
  230. if (ret) {
  231. dev_err(&client->dev,
  232. "[Read]-Modify-Write of register %02x failed!\n", reg);
  233. return val;
  234. }
  235. val |= set;
  236. val &= ~unset;
  237. ret = ov9640_reg_write(client, reg, val);
  238. if (ret)
  239. dev_err(&client->dev,
  240. "Read-Modify-[Write] of register %02x failed!\n", reg);
  241. return ret;
  242. }
  243. /* Soft reset the camera. This has nothing to do with the RESET pin! */
  244. static int ov9640_reset(struct i2c_client *client)
  245. {
  246. int ret;
  247. ret = ov9640_reg_write(client, OV9640_COM7, OV9640_COM7_SCCB_RESET);
  248. if (ret)
  249. dev_err(&client->dev,
  250. "An error occured while entering soft reset!\n");
  251. return ret;
  252. }
  253. /* Start/Stop streaming from the device */
  254. static int ov9640_s_stream(struct v4l2_subdev *sd, int enable)
  255. {
  256. return 0;
  257. }
  258. /* Alter bus settings on camera side */
  259. static int ov9640_set_bus_param(struct soc_camera_device *icd,
  260. unsigned long flags)
  261. {
  262. return 0;
  263. }
  264. /* Request bus settings on camera side */
  265. static unsigned long ov9640_query_bus_param(struct soc_camera_device *icd)
  266. {
  267. struct soc_camera_link *icl = to_soc_camera_link(icd);
  268. /*
  269. * REVISIT: the camera probably can do 10 bit transfers, but I don't
  270. * have those pins connected on my hardware.
  271. */
  272. unsigned long flags = SOCAM_PCLK_SAMPLE_RISING | SOCAM_MASTER |
  273. SOCAM_VSYNC_ACTIVE_HIGH | SOCAM_HSYNC_ACTIVE_HIGH |
  274. SOCAM_DATA_ACTIVE_HIGH | SOCAM_DATAWIDTH_8;
  275. return soc_camera_apply_sensor_flags(icl, flags);
  276. }
  277. /* Get status of additional camera capabilities */
  278. static int ov9640_g_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
  279. {
  280. struct i2c_client *client = sd->priv;
  281. struct ov9640_priv *priv = container_of(i2c_get_clientdata(client),
  282. struct ov9640_priv, subdev);
  283. switch (ctrl->id) {
  284. case V4L2_CID_VFLIP:
  285. ctrl->value = priv->flag_vflip;
  286. break;
  287. case V4L2_CID_HFLIP:
  288. ctrl->value = priv->flag_hflip;
  289. break;
  290. }
  291. return 0;
  292. }
  293. /* Set status of additional camera capabilities */
  294. static int ov9640_s_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
  295. {
  296. struct i2c_client *client = sd->priv;
  297. struct ov9640_priv *priv = container_of(i2c_get_clientdata(client),
  298. struct ov9640_priv, subdev);
  299. int ret = 0;
  300. switch (ctrl->id) {
  301. case V4L2_CID_VFLIP:
  302. priv->flag_vflip = ctrl->value;
  303. if (ctrl->value)
  304. ret = ov9640_reg_rmw(client, OV9640_MVFP,
  305. OV9640_MVFP_V, 0);
  306. else
  307. ret = ov9640_reg_rmw(client, OV9640_MVFP,
  308. 0, OV9640_MVFP_V);
  309. break;
  310. case V4L2_CID_HFLIP:
  311. priv->flag_hflip = ctrl->value;
  312. if (ctrl->value)
  313. ret = ov9640_reg_rmw(client, OV9640_MVFP,
  314. OV9640_MVFP_H, 0);
  315. else
  316. ret = ov9640_reg_rmw(client, OV9640_MVFP,
  317. 0, OV9640_MVFP_H);
  318. break;
  319. }
  320. return ret;
  321. }
  322. /* Get chip identification */
  323. static int ov9640_g_chip_ident(struct v4l2_subdev *sd,
  324. struct v4l2_dbg_chip_ident *id)
  325. {
  326. struct i2c_client *client = sd->priv;
  327. struct ov9640_priv *priv = container_of(i2c_get_clientdata(client),
  328. struct ov9640_priv, subdev);
  329. id->ident = priv->model;
  330. id->revision = priv->revision;
  331. return 0;
  332. }
  333. #ifdef CONFIG_VIDEO_ADV_DEBUG
  334. static int ov9640_get_register(struct v4l2_subdev *sd,
  335. struct v4l2_dbg_register *reg)
  336. {
  337. struct i2c_client *client = sd->priv;
  338. int ret;
  339. u8 val;
  340. if (reg->reg & ~0xff)
  341. return -EINVAL;
  342. reg->size = 1;
  343. ret = ov9640_reg_read(client, reg->reg, &val);
  344. if (ret)
  345. return ret;
  346. reg->val = (__u64)val;
  347. return 0;
  348. }
  349. static int ov9640_set_register(struct v4l2_subdev *sd,
  350. struct v4l2_dbg_register *reg)
  351. {
  352. struct i2c_client *client = sd->priv;
  353. if (reg->reg & ~0xff || reg->val & ~0xff)
  354. return -EINVAL;
  355. return ov9640_reg_write(client, reg->reg, reg->val);
  356. }
  357. #endif
  358. /* select nearest higher resolution for capture */
  359. static void ov9640_res_roundup(u32 *width, u32 *height)
  360. {
  361. int i;
  362. enum { QQCIF, QQVGA, QCIF, QVGA, CIF, VGA, SXGA };
  363. int res_x[] = { 88, 160, 176, 320, 352, 640, 1280 };
  364. int res_y[] = { 72, 120, 144, 240, 288, 480, 960 };
  365. for (i = 0; i < ARRAY_SIZE(res_x); i++) {
  366. if (res_x[i] >= *width && res_y[i] >= *height) {
  367. *width = res_x[i];
  368. *height = res_y[i];
  369. return;
  370. }
  371. }
  372. *width = res_x[SXGA];
  373. *height = res_y[SXGA];
  374. }
  375. /* Prepare necessary register changes depending on color encoding */
  376. static void ov9640_alter_regs(u32 pixfmt, struct ov9640_reg_alt *alt)
  377. {
  378. switch (pixfmt) {
  379. case V4L2_PIX_FMT_UYVY:
  380. alt->com12 = OV9640_COM12_YUV_AVG;
  381. alt->com13 = OV9640_COM13_Y_DELAY_EN |
  382. OV9640_COM13_YUV_DLY(0x01);
  383. break;
  384. case V4L2_PIX_FMT_RGB555:
  385. alt->com7 = OV9640_COM7_RGB;
  386. alt->com13 = OV9640_COM13_RGB_AVG;
  387. alt->com15 = OV9640_COM15_RGB_555;
  388. break;
  389. case V4L2_PIX_FMT_RGB565:
  390. alt->com7 = OV9640_COM7_RGB;
  391. alt->com13 = OV9640_COM13_RGB_AVG;
  392. alt->com15 = OV9640_COM15_RGB_565;
  393. break;
  394. };
  395. }
  396. /* Setup registers according to resolution and color encoding */
  397. static int ov9640_write_regs(struct i2c_client *client,
  398. u32 width, u32 pixfmt, struct ov9640_reg_alt *alts)
  399. {
  400. const struct ov9640_reg *ov9640_regs, *matrix_regs;
  401. int ov9640_regs_len, matrix_regs_len;
  402. int i, ret;
  403. u8 val;
  404. /* select register configuration for given resolution */
  405. switch (width) {
  406. case W_QQCIF:
  407. ov9640_regs = ov9640_regs_qqcif;
  408. ov9640_regs_len = ARRAY_SIZE(ov9640_regs_qqcif);
  409. break;
  410. case W_QQVGA:
  411. ov9640_regs = ov9640_regs_qqvga;
  412. ov9640_regs_len = ARRAY_SIZE(ov9640_regs_qqvga);
  413. break;
  414. case W_QCIF:
  415. ov9640_regs = ov9640_regs_qcif;
  416. ov9640_regs_len = ARRAY_SIZE(ov9640_regs_qcif);
  417. break;
  418. case W_QVGA:
  419. ov9640_regs = ov9640_regs_qvga;
  420. ov9640_regs_len = ARRAY_SIZE(ov9640_regs_qvga);
  421. break;
  422. case W_CIF:
  423. ov9640_regs = ov9640_regs_cif;
  424. ov9640_regs_len = ARRAY_SIZE(ov9640_regs_cif);
  425. break;
  426. case W_VGA:
  427. ov9640_regs = ov9640_regs_vga;
  428. ov9640_regs_len = ARRAY_SIZE(ov9640_regs_vga);
  429. break;
  430. case W_SXGA:
  431. ov9640_regs = ov9640_regs_sxga;
  432. ov9640_regs_len = ARRAY_SIZE(ov9640_regs_sxga);
  433. break;
  434. default:
  435. dev_err(&client->dev, "Failed to select resolution!\n");
  436. return -EINVAL;
  437. }
  438. /* select color matrix configuration for given color encoding */
  439. if (pixfmt == V4L2_PIX_FMT_UYVY) {
  440. matrix_regs = ov9640_regs_yuv;
  441. matrix_regs_len = ARRAY_SIZE(ov9640_regs_yuv);
  442. } else {
  443. matrix_regs = ov9640_regs_rgb;
  444. matrix_regs_len = ARRAY_SIZE(ov9640_regs_rgb);
  445. }
  446. /* write register settings into the module */
  447. for (i = 0; i < ov9640_regs_len; i++) {
  448. val = ov9640_regs[i].val;
  449. switch (ov9640_regs[i].reg) {
  450. case OV9640_COM7:
  451. val |= alts->com7;
  452. break;
  453. case OV9640_COM12:
  454. val |= alts->com12;
  455. break;
  456. case OV9640_COM13:
  457. val |= alts->com13;
  458. break;
  459. case OV9640_COM15:
  460. val |= alts->com15;
  461. break;
  462. }
  463. ret = ov9640_reg_write(client, ov9640_regs[i].reg, val);
  464. if (ret)
  465. return ret;
  466. }
  467. /* write color matrix configuration into the module */
  468. for (i = 0; i < matrix_regs_len; i++) {
  469. ret = ov9640_reg_write(client, matrix_regs[i].reg,
  470. matrix_regs[i].val);
  471. if (ret)
  472. return ret;
  473. }
  474. return 0;
  475. }
  476. /* program default register values */
  477. static int ov9640_prog_dflt(struct i2c_client *client)
  478. {
  479. int i, ret;
  480. for (i = 0; i < ARRAY_SIZE(ov9640_regs_dflt); i++) {
  481. ret = ov9640_reg_write(client, ov9640_regs_dflt[i].reg,
  482. ov9640_regs_dflt[i].val);
  483. if (ret)
  484. return ret;
  485. }
  486. /* wait for the changes to actually happen, 140ms are not enough yet */
  487. mdelay(150);
  488. return 0;
  489. }
  490. /* set the format we will capture in */
  491. static int ov9640_s_fmt(struct v4l2_subdev *sd, struct v4l2_format *f)
  492. {
  493. struct i2c_client *client = sd->priv;
  494. struct v4l2_pix_format *pix = &f->fmt.pix;
  495. struct ov9640_reg_alt alts = {0};
  496. int ret;
  497. ov9640_res_roundup(&pix->width, &pix->height);
  498. ov9640_alter_regs(pix->pixelformat, &alts);
  499. ov9640_reset(client);
  500. ret = ov9640_prog_dflt(client);
  501. if (ret)
  502. return ret;
  503. return ov9640_write_regs(client, pix->width, pix->pixelformat, &alts);
  504. }
  505. static int ov9640_try_fmt(struct v4l2_subdev *sd, struct v4l2_format *f)
  506. {
  507. struct v4l2_pix_format *pix = &f->fmt.pix;
  508. ov9640_res_roundup(&pix->width, &pix->height);
  509. pix->field = V4L2_FIELD_NONE;
  510. return 0;
  511. }
  512. static int ov9640_g_crop(struct v4l2_subdev *sd, struct v4l2_crop *a)
  513. {
  514. a->c.left = 0;
  515. a->c.top = 0;
  516. a->c.width = W_SXGA;
  517. a->c.height = H_SXGA;
  518. a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  519. return 0;
  520. }
  521. static int ov9640_cropcap(struct v4l2_subdev *sd, struct v4l2_cropcap *a)
  522. {
  523. a->bounds.left = 0;
  524. a->bounds.top = 0;
  525. a->bounds.width = W_SXGA;
  526. a->bounds.height = H_SXGA;
  527. a->defrect = a->bounds;
  528. a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  529. a->pixelaspect.numerator = 1;
  530. a->pixelaspect.denominator = 1;
  531. return 0;
  532. }
  533. static int ov9640_video_probe(struct soc_camera_device *icd,
  534. struct i2c_client *client)
  535. {
  536. struct ov9640_priv *priv = i2c_get_clientdata(client);
  537. u8 pid, ver, midh, midl;
  538. const char *devname;
  539. int ret = 0;
  540. /*
  541. * We must have a parent by now. And it cannot be a wrong one.
  542. * So this entire test is completely redundant.
  543. */
  544. if (!icd->dev.parent ||
  545. to_soc_camera_host(icd->dev.parent)->nr != icd->iface) {
  546. dev_err(&client->dev, "Parent missing or invalid!\n");
  547. ret = -ENODEV;
  548. goto err;
  549. }
  550. icd->formats = ov9640_fmt_lists;
  551. icd->num_formats = ARRAY_SIZE(ov9640_fmt_lists);
  552. /*
  553. * check and show product ID and manufacturer ID
  554. */
  555. ret = ov9640_reg_read(client, OV9640_PID, &pid);
  556. if (ret)
  557. goto err;
  558. ret = ov9640_reg_read(client, OV9640_VER, &ver);
  559. if (ret)
  560. goto err;
  561. ret = ov9640_reg_read(client, OV9640_MIDH, &midh);
  562. if (ret)
  563. goto err;
  564. ret = ov9640_reg_read(client, OV9640_MIDL, &midl);
  565. if (ret)
  566. goto err;
  567. switch (VERSION(pid, ver)) {
  568. case OV9640_V2:
  569. devname = "ov9640";
  570. priv->model = V4L2_IDENT_OV9640;
  571. priv->revision = 2;
  572. case OV9640_V3:
  573. devname = "ov9640";
  574. priv->model = V4L2_IDENT_OV9640;
  575. priv->revision = 3;
  576. break;
  577. default:
  578. dev_err(&client->dev, "Product ID error %x:%x\n", pid, ver);
  579. ret = -ENODEV;
  580. goto err;
  581. }
  582. dev_info(&client->dev, "%s Product ID %0x:%0x Manufacturer ID %x:%x\n",
  583. devname, pid, ver, midh, midl);
  584. err:
  585. return ret;
  586. }
  587. static struct soc_camera_ops ov9640_ops = {
  588. .set_bus_param = ov9640_set_bus_param,
  589. .query_bus_param = ov9640_query_bus_param,
  590. .controls = ov9640_controls,
  591. .num_controls = ARRAY_SIZE(ov9640_controls),
  592. };
  593. static struct v4l2_subdev_core_ops ov9640_core_ops = {
  594. .g_ctrl = ov9640_g_ctrl,
  595. .s_ctrl = ov9640_s_ctrl,
  596. .g_chip_ident = ov9640_g_chip_ident,
  597. #ifdef CONFIG_VIDEO_ADV_DEBUG
  598. .g_register = ov9640_get_register,
  599. .s_register = ov9640_set_register,
  600. #endif
  601. };
  602. static struct v4l2_subdev_video_ops ov9640_video_ops = {
  603. .s_stream = ov9640_s_stream,
  604. .s_fmt = ov9640_s_fmt,
  605. .try_fmt = ov9640_try_fmt,
  606. .cropcap = ov9640_cropcap,
  607. .g_crop = ov9640_g_crop,
  608. };
  609. static struct v4l2_subdev_ops ov9640_subdev_ops = {
  610. .core = &ov9640_core_ops,
  611. .video = &ov9640_video_ops,
  612. };
  613. /*
  614. * i2c_driver function
  615. */
  616. static int ov9640_probe(struct i2c_client *client,
  617. const struct i2c_device_id *did)
  618. {
  619. struct ov9640_priv *priv;
  620. struct soc_camera_device *icd = client->dev.platform_data;
  621. struct soc_camera_link *icl;
  622. int ret;
  623. if (!icd) {
  624. dev_err(&client->dev, "Missing soc-camera data!\n");
  625. return -EINVAL;
  626. }
  627. icl = to_soc_camera_link(icd);
  628. if (!icl) {
  629. dev_err(&client->dev, "Missing platform_data for driver\n");
  630. return -EINVAL;
  631. }
  632. priv = kzalloc(sizeof(struct ov9640_priv), GFP_KERNEL);
  633. if (!priv) {
  634. dev_err(&client->dev,
  635. "Failed to allocate memory for private data!\n");
  636. return -ENOMEM;
  637. }
  638. v4l2_i2c_subdev_init(&priv->subdev, client, &ov9640_subdev_ops);
  639. icd->ops = &ov9640_ops;
  640. ret = ov9640_video_probe(icd, client);
  641. if (ret) {
  642. icd->ops = NULL;
  643. i2c_set_clientdata(client, NULL);
  644. kfree(priv);
  645. }
  646. return ret;
  647. }
  648. static int ov9640_remove(struct i2c_client *client)
  649. {
  650. struct ov9640_priv *priv = i2c_get_clientdata(client);
  651. i2c_set_clientdata(client, NULL);
  652. kfree(priv);
  653. return 0;
  654. }
  655. static const struct i2c_device_id ov9640_id[] = {
  656. { "ov9640", 0 },
  657. { }
  658. };
  659. MODULE_DEVICE_TABLE(i2c, ov9640_id);
  660. static struct i2c_driver ov9640_i2c_driver = {
  661. .driver = {
  662. .name = "ov9640",
  663. },
  664. .probe = ov9640_probe,
  665. .remove = ov9640_remove,
  666. .id_table = ov9640_id,
  667. };
  668. static int __init ov9640_module_init(void)
  669. {
  670. return i2c_add_driver(&ov9640_i2c_driver);
  671. }
  672. static void __exit ov9640_module_exit(void)
  673. {
  674. i2c_del_driver(&ov9640_i2c_driver);
  675. }
  676. module_init(ov9640_module_init);
  677. module_exit(ov9640_module_exit);
  678. MODULE_DESCRIPTION("SoC Camera driver for OmniVision OV96xx");
  679. MODULE_AUTHOR("Marek Vasut <marek.vasut@gmail.com>");
  680. MODULE_LICENSE("GPL v2");