ov9640.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832
  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. static enum v4l2_mbus_pixelcode ov9640_codes[] = {
  143. V4L2_MBUS_FMT_YUYV8_2X8_BE,
  144. V4L2_MBUS_FMT_RGB555_2X8_PADHI_LE,
  145. V4L2_MBUS_FMT_RGB565_2X8_LE,
  146. };
  147. static const struct v4l2_queryctrl ov9640_controls[] = {
  148. {
  149. .id = V4L2_CID_VFLIP,
  150. .type = V4L2_CTRL_TYPE_BOOLEAN,
  151. .name = "Flip Vertically",
  152. .minimum = 0,
  153. .maximum = 1,
  154. .step = 1,
  155. .default_value = 0,
  156. },
  157. {
  158. .id = V4L2_CID_HFLIP,
  159. .type = V4L2_CTRL_TYPE_BOOLEAN,
  160. .name = "Flip Horizontally",
  161. .minimum = 0,
  162. .maximum = 1,
  163. .step = 1,
  164. .default_value = 0,
  165. },
  166. };
  167. /* read a register */
  168. static int ov9640_reg_read(struct i2c_client *client, u8 reg, u8 *val)
  169. {
  170. int ret;
  171. u8 data = reg;
  172. struct i2c_msg msg = {
  173. .addr = client->addr,
  174. .flags = 0,
  175. .len = 1,
  176. .buf = &data,
  177. };
  178. ret = i2c_transfer(client->adapter, &msg, 1);
  179. if (ret < 0)
  180. goto err;
  181. msg.flags = I2C_M_RD;
  182. ret = i2c_transfer(client->adapter, &msg, 1);
  183. if (ret < 0)
  184. goto err;
  185. *val = data;
  186. return 0;
  187. err:
  188. dev_err(&client->dev, "Failed reading register 0x%02x!\n", reg);
  189. return ret;
  190. }
  191. /* write a register */
  192. static int ov9640_reg_write(struct i2c_client *client, u8 reg, u8 val)
  193. {
  194. int ret;
  195. u8 _val;
  196. unsigned char data[2] = { reg, val };
  197. struct i2c_msg msg = {
  198. .addr = client->addr,
  199. .flags = 0,
  200. .len = 2,
  201. .buf = data,
  202. };
  203. ret = i2c_transfer(client->adapter, &msg, 1);
  204. if (ret < 0) {
  205. dev_err(&client->dev, "Failed writing register 0x%02x!\n", reg);
  206. return ret;
  207. }
  208. /* we have to read the register back ... no idea why, maybe HW bug */
  209. ret = ov9640_reg_read(client, reg, &_val);
  210. if (ret)
  211. dev_err(&client->dev,
  212. "Failed reading back register 0x%02x!\n", reg);
  213. return 0;
  214. }
  215. /* Read a register, alter its bits, write it back */
  216. static int ov9640_reg_rmw(struct i2c_client *client, u8 reg, u8 set, u8 unset)
  217. {
  218. u8 val;
  219. int ret;
  220. ret = ov9640_reg_read(client, reg, &val);
  221. if (ret) {
  222. dev_err(&client->dev,
  223. "[Read]-Modify-Write of register %02x failed!\n", reg);
  224. return val;
  225. }
  226. val |= set;
  227. val &= ~unset;
  228. ret = ov9640_reg_write(client, reg, val);
  229. if (ret)
  230. dev_err(&client->dev,
  231. "Read-Modify-[Write] of register %02x failed!\n", reg);
  232. return ret;
  233. }
  234. /* Soft reset the camera. This has nothing to do with the RESET pin! */
  235. static int ov9640_reset(struct i2c_client *client)
  236. {
  237. int ret;
  238. ret = ov9640_reg_write(client, OV9640_COM7, OV9640_COM7_SCCB_RESET);
  239. if (ret)
  240. dev_err(&client->dev,
  241. "An error occured while entering soft reset!\n");
  242. return ret;
  243. }
  244. /* Start/Stop streaming from the device */
  245. static int ov9640_s_stream(struct v4l2_subdev *sd, int enable)
  246. {
  247. return 0;
  248. }
  249. /* Alter bus settings on camera side */
  250. static int ov9640_set_bus_param(struct soc_camera_device *icd,
  251. unsigned long flags)
  252. {
  253. return 0;
  254. }
  255. /* Request bus settings on camera side */
  256. static unsigned long ov9640_query_bus_param(struct soc_camera_device *icd)
  257. {
  258. struct soc_camera_link *icl = to_soc_camera_link(icd);
  259. /*
  260. * REVISIT: the camera probably can do 10 bit transfers, but I don't
  261. * have those pins connected on my hardware.
  262. */
  263. unsigned long flags = SOCAM_PCLK_SAMPLE_RISING | SOCAM_MASTER |
  264. SOCAM_VSYNC_ACTIVE_HIGH | SOCAM_HSYNC_ACTIVE_HIGH |
  265. SOCAM_DATA_ACTIVE_HIGH | SOCAM_DATAWIDTH_8;
  266. return soc_camera_apply_sensor_flags(icl, flags);
  267. }
  268. /* Get status of additional camera capabilities */
  269. static int ov9640_g_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
  270. {
  271. struct i2c_client *client = sd->priv;
  272. struct ov9640_priv *priv = container_of(i2c_get_clientdata(client),
  273. struct ov9640_priv, subdev);
  274. switch (ctrl->id) {
  275. case V4L2_CID_VFLIP:
  276. ctrl->value = priv->flag_vflip;
  277. break;
  278. case V4L2_CID_HFLIP:
  279. ctrl->value = priv->flag_hflip;
  280. break;
  281. }
  282. return 0;
  283. }
  284. /* Set status of additional camera capabilities */
  285. static int ov9640_s_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
  286. {
  287. struct i2c_client *client = sd->priv;
  288. struct ov9640_priv *priv = container_of(i2c_get_clientdata(client),
  289. struct ov9640_priv, subdev);
  290. int ret = 0;
  291. switch (ctrl->id) {
  292. case V4L2_CID_VFLIP:
  293. priv->flag_vflip = ctrl->value;
  294. if (ctrl->value)
  295. ret = ov9640_reg_rmw(client, OV9640_MVFP,
  296. OV9640_MVFP_V, 0);
  297. else
  298. ret = ov9640_reg_rmw(client, OV9640_MVFP,
  299. 0, OV9640_MVFP_V);
  300. break;
  301. case V4L2_CID_HFLIP:
  302. priv->flag_hflip = ctrl->value;
  303. if (ctrl->value)
  304. ret = ov9640_reg_rmw(client, OV9640_MVFP,
  305. OV9640_MVFP_H, 0);
  306. else
  307. ret = ov9640_reg_rmw(client, OV9640_MVFP,
  308. 0, OV9640_MVFP_H);
  309. break;
  310. }
  311. return ret;
  312. }
  313. /* Get chip identification */
  314. static int ov9640_g_chip_ident(struct v4l2_subdev *sd,
  315. struct v4l2_dbg_chip_ident *id)
  316. {
  317. struct i2c_client *client = sd->priv;
  318. struct ov9640_priv *priv = container_of(i2c_get_clientdata(client),
  319. struct ov9640_priv, subdev);
  320. id->ident = priv->model;
  321. id->revision = priv->revision;
  322. return 0;
  323. }
  324. #ifdef CONFIG_VIDEO_ADV_DEBUG
  325. static int ov9640_get_register(struct v4l2_subdev *sd,
  326. struct v4l2_dbg_register *reg)
  327. {
  328. struct i2c_client *client = sd->priv;
  329. int ret;
  330. u8 val;
  331. if (reg->reg & ~0xff)
  332. return -EINVAL;
  333. reg->size = 1;
  334. ret = ov9640_reg_read(client, reg->reg, &val);
  335. if (ret)
  336. return ret;
  337. reg->val = (__u64)val;
  338. return 0;
  339. }
  340. static int ov9640_set_register(struct v4l2_subdev *sd,
  341. struct v4l2_dbg_register *reg)
  342. {
  343. struct i2c_client *client = sd->priv;
  344. if (reg->reg & ~0xff || reg->val & ~0xff)
  345. return -EINVAL;
  346. return ov9640_reg_write(client, reg->reg, reg->val);
  347. }
  348. #endif
  349. /* select nearest higher resolution for capture */
  350. static void ov9640_res_roundup(u32 *width, u32 *height)
  351. {
  352. int i;
  353. enum { QQCIF, QQVGA, QCIF, QVGA, CIF, VGA, SXGA };
  354. int res_x[] = { 88, 160, 176, 320, 352, 640, 1280 };
  355. int res_y[] = { 72, 120, 144, 240, 288, 480, 960 };
  356. for (i = 0; i < ARRAY_SIZE(res_x); i++) {
  357. if (res_x[i] >= *width && res_y[i] >= *height) {
  358. *width = res_x[i];
  359. *height = res_y[i];
  360. return;
  361. }
  362. }
  363. *width = res_x[SXGA];
  364. *height = res_y[SXGA];
  365. }
  366. /* Prepare necessary register changes depending on color encoding */
  367. static void ov9640_alter_regs(enum v4l2_mbus_pixelcode code,
  368. struct ov9640_reg_alt *alt)
  369. {
  370. switch (code) {
  371. default:
  372. case V4L2_MBUS_FMT_YUYV8_2X8_BE:
  373. alt->com12 = OV9640_COM12_YUV_AVG;
  374. alt->com13 = OV9640_COM13_Y_DELAY_EN |
  375. OV9640_COM13_YUV_DLY(0x01);
  376. break;
  377. case V4L2_MBUS_FMT_RGB555_2X8_PADHI_LE:
  378. alt->com7 = OV9640_COM7_RGB;
  379. alt->com13 = OV9640_COM13_RGB_AVG;
  380. alt->com15 = OV9640_COM15_RGB_555;
  381. break;
  382. case V4L2_MBUS_FMT_RGB565_2X8_LE:
  383. alt->com7 = OV9640_COM7_RGB;
  384. alt->com13 = OV9640_COM13_RGB_AVG;
  385. alt->com15 = OV9640_COM15_RGB_565;
  386. break;
  387. };
  388. }
  389. /* Setup registers according to resolution and color encoding */
  390. static int ov9640_write_regs(struct i2c_client *client, u32 width,
  391. enum v4l2_mbus_pixelcode code, struct ov9640_reg_alt *alts)
  392. {
  393. const struct ov9640_reg *ov9640_regs, *matrix_regs;
  394. int ov9640_regs_len, matrix_regs_len;
  395. int i, ret;
  396. u8 val;
  397. /* select register configuration for given resolution */
  398. switch (width) {
  399. case W_QQCIF:
  400. ov9640_regs = ov9640_regs_qqcif;
  401. ov9640_regs_len = ARRAY_SIZE(ov9640_regs_qqcif);
  402. break;
  403. case W_QQVGA:
  404. ov9640_regs = ov9640_regs_qqvga;
  405. ov9640_regs_len = ARRAY_SIZE(ov9640_regs_qqvga);
  406. break;
  407. case W_QCIF:
  408. ov9640_regs = ov9640_regs_qcif;
  409. ov9640_regs_len = ARRAY_SIZE(ov9640_regs_qcif);
  410. break;
  411. case W_QVGA:
  412. ov9640_regs = ov9640_regs_qvga;
  413. ov9640_regs_len = ARRAY_SIZE(ov9640_regs_qvga);
  414. break;
  415. case W_CIF:
  416. ov9640_regs = ov9640_regs_cif;
  417. ov9640_regs_len = ARRAY_SIZE(ov9640_regs_cif);
  418. break;
  419. case W_VGA:
  420. ov9640_regs = ov9640_regs_vga;
  421. ov9640_regs_len = ARRAY_SIZE(ov9640_regs_vga);
  422. break;
  423. case W_SXGA:
  424. ov9640_regs = ov9640_regs_sxga;
  425. ov9640_regs_len = ARRAY_SIZE(ov9640_regs_sxga);
  426. break;
  427. default:
  428. dev_err(&client->dev, "Failed to select resolution!\n");
  429. return -EINVAL;
  430. }
  431. /* select color matrix configuration for given color encoding */
  432. if (code == V4L2_MBUS_FMT_YUYV8_2X8_BE) {
  433. matrix_regs = ov9640_regs_yuv;
  434. matrix_regs_len = ARRAY_SIZE(ov9640_regs_yuv);
  435. } else {
  436. matrix_regs = ov9640_regs_rgb;
  437. matrix_regs_len = ARRAY_SIZE(ov9640_regs_rgb);
  438. }
  439. /* write register settings into the module */
  440. for (i = 0; i < ov9640_regs_len; i++) {
  441. val = ov9640_regs[i].val;
  442. switch (ov9640_regs[i].reg) {
  443. case OV9640_COM7:
  444. val |= alts->com7;
  445. break;
  446. case OV9640_COM12:
  447. val |= alts->com12;
  448. break;
  449. case OV9640_COM13:
  450. val |= alts->com13;
  451. break;
  452. case OV9640_COM15:
  453. val |= alts->com15;
  454. break;
  455. }
  456. ret = ov9640_reg_write(client, ov9640_regs[i].reg, val);
  457. if (ret)
  458. return ret;
  459. }
  460. /* write color matrix configuration into the module */
  461. for (i = 0; i < matrix_regs_len; i++) {
  462. ret = ov9640_reg_write(client, matrix_regs[i].reg,
  463. matrix_regs[i].val);
  464. if (ret)
  465. return ret;
  466. }
  467. return 0;
  468. }
  469. /* program default register values */
  470. static int ov9640_prog_dflt(struct i2c_client *client)
  471. {
  472. int i, ret;
  473. for (i = 0; i < ARRAY_SIZE(ov9640_regs_dflt); i++) {
  474. ret = ov9640_reg_write(client, ov9640_regs_dflt[i].reg,
  475. ov9640_regs_dflt[i].val);
  476. if (ret)
  477. return ret;
  478. }
  479. /* wait for the changes to actually happen, 140ms are not enough yet */
  480. mdelay(150);
  481. return 0;
  482. }
  483. /* set the format we will capture in */
  484. static int ov9640_s_fmt(struct v4l2_subdev *sd,
  485. struct v4l2_mbus_framefmt *mf)
  486. {
  487. struct i2c_client *client = sd->priv;
  488. struct ov9640_reg_alt alts = {0};
  489. enum v4l2_colorspace cspace;
  490. enum v4l2_mbus_pixelcode code = mf->code;
  491. int ret;
  492. ov9640_res_roundup(&mf->width, &mf->height);
  493. ov9640_alter_regs(mf->code, &alts);
  494. ov9640_reset(client);
  495. ret = ov9640_prog_dflt(client);
  496. if (ret)
  497. return ret;
  498. switch (code) {
  499. case V4L2_MBUS_FMT_RGB555_2X8_PADHI_LE:
  500. case V4L2_MBUS_FMT_RGB565_2X8_LE:
  501. cspace = V4L2_COLORSPACE_SRGB;
  502. break;
  503. default:
  504. code = V4L2_MBUS_FMT_YUYV8_2X8_BE;
  505. case V4L2_MBUS_FMT_YUYV8_2X8_BE:
  506. cspace = V4L2_COLORSPACE_JPEG;
  507. }
  508. ret = ov9640_write_regs(client, mf->width, code, &alts);
  509. if (!ret) {
  510. mf->code = code;
  511. mf->colorspace = cspace;
  512. }
  513. return ret;
  514. }
  515. static int ov9640_try_fmt(struct v4l2_subdev *sd,
  516. struct v4l2_mbus_framefmt *mf)
  517. {
  518. ov9640_res_roundup(&mf->width, &mf->height);
  519. mf->field = V4L2_FIELD_NONE;
  520. switch (mf->code) {
  521. case V4L2_MBUS_FMT_RGB555_2X8_PADHI_LE:
  522. case V4L2_MBUS_FMT_RGB565_2X8_LE:
  523. mf->colorspace = V4L2_COLORSPACE_SRGB;
  524. break;
  525. default:
  526. mf->code = V4L2_MBUS_FMT_YUYV8_2X8_BE;
  527. case V4L2_MBUS_FMT_YUYV8_2X8_BE:
  528. mf->colorspace = V4L2_COLORSPACE_JPEG;
  529. }
  530. return 0;
  531. }
  532. static int ov9640_enum_fmt(struct v4l2_subdev *sd, int index,
  533. enum v4l2_mbus_pixelcode *code)
  534. {
  535. if ((unsigned int)index >= ARRAY_SIZE(ov9640_codes))
  536. return -EINVAL;
  537. *code = ov9640_codes[index];
  538. return 0;
  539. }
  540. static int ov9640_g_crop(struct v4l2_subdev *sd, struct v4l2_crop *a)
  541. {
  542. a->c.left = 0;
  543. a->c.top = 0;
  544. a->c.width = W_SXGA;
  545. a->c.height = H_SXGA;
  546. a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  547. return 0;
  548. }
  549. static int ov9640_cropcap(struct v4l2_subdev *sd, struct v4l2_cropcap *a)
  550. {
  551. a->bounds.left = 0;
  552. a->bounds.top = 0;
  553. a->bounds.width = W_SXGA;
  554. a->bounds.height = H_SXGA;
  555. a->defrect = a->bounds;
  556. a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  557. a->pixelaspect.numerator = 1;
  558. a->pixelaspect.denominator = 1;
  559. return 0;
  560. }
  561. static int ov9640_video_probe(struct soc_camera_device *icd,
  562. struct i2c_client *client)
  563. {
  564. struct ov9640_priv *priv = i2c_get_clientdata(client);
  565. u8 pid, ver, midh, midl;
  566. const char *devname;
  567. int ret = 0;
  568. /*
  569. * We must have a parent by now. And it cannot be a wrong one.
  570. * So this entire test is completely redundant.
  571. */
  572. if (!icd->dev.parent ||
  573. to_soc_camera_host(icd->dev.parent)->nr != icd->iface) {
  574. dev_err(&client->dev, "Parent missing or invalid!\n");
  575. ret = -ENODEV;
  576. goto err;
  577. }
  578. /*
  579. * check and show product ID and manufacturer ID
  580. */
  581. ret = ov9640_reg_read(client, OV9640_PID, &pid);
  582. if (ret)
  583. goto err;
  584. ret = ov9640_reg_read(client, OV9640_VER, &ver);
  585. if (ret)
  586. goto err;
  587. ret = ov9640_reg_read(client, OV9640_MIDH, &midh);
  588. if (ret)
  589. goto err;
  590. ret = ov9640_reg_read(client, OV9640_MIDL, &midl);
  591. if (ret)
  592. goto err;
  593. switch (VERSION(pid, ver)) {
  594. case OV9640_V2:
  595. devname = "ov9640";
  596. priv->model = V4L2_IDENT_OV9640;
  597. priv->revision = 2;
  598. case OV9640_V3:
  599. devname = "ov9640";
  600. priv->model = V4L2_IDENT_OV9640;
  601. priv->revision = 3;
  602. break;
  603. default:
  604. dev_err(&client->dev, "Product ID error %x:%x\n", pid, ver);
  605. ret = -ENODEV;
  606. goto err;
  607. }
  608. dev_info(&client->dev, "%s Product ID %0x:%0x Manufacturer ID %x:%x\n",
  609. devname, pid, ver, midh, midl);
  610. err:
  611. return ret;
  612. }
  613. static struct soc_camera_ops ov9640_ops = {
  614. .set_bus_param = ov9640_set_bus_param,
  615. .query_bus_param = ov9640_query_bus_param,
  616. .controls = ov9640_controls,
  617. .num_controls = ARRAY_SIZE(ov9640_controls),
  618. };
  619. static struct v4l2_subdev_core_ops ov9640_core_ops = {
  620. .g_ctrl = ov9640_g_ctrl,
  621. .s_ctrl = ov9640_s_ctrl,
  622. .g_chip_ident = ov9640_g_chip_ident,
  623. #ifdef CONFIG_VIDEO_ADV_DEBUG
  624. .g_register = ov9640_get_register,
  625. .s_register = ov9640_set_register,
  626. #endif
  627. };
  628. static struct v4l2_subdev_video_ops ov9640_video_ops = {
  629. .s_stream = ov9640_s_stream,
  630. .s_mbus_fmt = ov9640_s_fmt,
  631. .try_mbus_fmt = ov9640_try_fmt,
  632. .enum_mbus_fmt = ov9640_enum_fmt,
  633. .cropcap = ov9640_cropcap,
  634. .g_crop = ov9640_g_crop,
  635. };
  636. static struct v4l2_subdev_ops ov9640_subdev_ops = {
  637. .core = &ov9640_core_ops,
  638. .video = &ov9640_video_ops,
  639. };
  640. /*
  641. * i2c_driver function
  642. */
  643. static int ov9640_probe(struct i2c_client *client,
  644. const struct i2c_device_id *did)
  645. {
  646. struct ov9640_priv *priv;
  647. struct soc_camera_device *icd = client->dev.platform_data;
  648. struct soc_camera_link *icl;
  649. int ret;
  650. if (!icd) {
  651. dev_err(&client->dev, "Missing soc-camera data!\n");
  652. return -EINVAL;
  653. }
  654. icl = to_soc_camera_link(icd);
  655. if (!icl) {
  656. dev_err(&client->dev, "Missing platform_data for driver\n");
  657. return -EINVAL;
  658. }
  659. priv = kzalloc(sizeof(struct ov9640_priv), GFP_KERNEL);
  660. if (!priv) {
  661. dev_err(&client->dev,
  662. "Failed to allocate memory for private data!\n");
  663. return -ENOMEM;
  664. }
  665. v4l2_i2c_subdev_init(&priv->subdev, client, &ov9640_subdev_ops);
  666. icd->ops = &ov9640_ops;
  667. ret = ov9640_video_probe(icd, client);
  668. if (ret) {
  669. icd->ops = NULL;
  670. i2c_set_clientdata(client, NULL);
  671. kfree(priv);
  672. }
  673. return ret;
  674. }
  675. static int ov9640_remove(struct i2c_client *client)
  676. {
  677. struct ov9640_priv *priv = i2c_get_clientdata(client);
  678. i2c_set_clientdata(client, NULL);
  679. kfree(priv);
  680. return 0;
  681. }
  682. static const struct i2c_device_id ov9640_id[] = {
  683. { "ov9640", 0 },
  684. { }
  685. };
  686. MODULE_DEVICE_TABLE(i2c, ov9640_id);
  687. static struct i2c_driver ov9640_i2c_driver = {
  688. .driver = {
  689. .name = "ov9640",
  690. },
  691. .probe = ov9640_probe,
  692. .remove = ov9640_remove,
  693. .id_table = ov9640_id,
  694. };
  695. static int __init ov9640_module_init(void)
  696. {
  697. return i2c_add_driver(&ov9640_i2c_driver);
  698. }
  699. static void __exit ov9640_module_exit(void)
  700. {
  701. i2c_del_driver(&ov9640_i2c_driver);
  702. }
  703. module_init(ov9640_module_init);
  704. module_exit(ov9640_module_exit);
  705. MODULE_DESCRIPTION("SoC Camera driver for OmniVision OV96xx");
  706. MODULE_AUTHOR("Marek Vasut <marek.vasut@gmail.com>");
  707. MODULE_LICENSE("GPL v2");