ov9640.c 20 KB

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