ov9640.c 20 KB

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