ov9640.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814
  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. /* Get status of additional camera capabilities */
  252. static int ov9640_g_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
  253. {
  254. struct ov9640_priv *priv = to_ov9640_sensor(sd);
  255. switch (ctrl->id) {
  256. case V4L2_CID_VFLIP:
  257. ctrl->value = priv->flag_vflip;
  258. break;
  259. case V4L2_CID_HFLIP:
  260. ctrl->value = priv->flag_hflip;
  261. break;
  262. }
  263. return 0;
  264. }
  265. /* Set status of additional camera capabilities */
  266. static int ov9640_s_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
  267. {
  268. struct i2c_client *client = v4l2_get_subdevdata(sd);
  269. struct ov9640_priv *priv = to_ov9640_sensor(sd);
  270. int ret = 0;
  271. switch (ctrl->id) {
  272. case V4L2_CID_VFLIP:
  273. priv->flag_vflip = ctrl->value;
  274. if (ctrl->value)
  275. ret = ov9640_reg_rmw(client, OV9640_MVFP,
  276. OV9640_MVFP_V, 0);
  277. else
  278. ret = ov9640_reg_rmw(client, OV9640_MVFP,
  279. 0, OV9640_MVFP_V);
  280. break;
  281. case V4L2_CID_HFLIP:
  282. priv->flag_hflip = ctrl->value;
  283. if (ctrl->value)
  284. ret = ov9640_reg_rmw(client, OV9640_MVFP,
  285. OV9640_MVFP_H, 0);
  286. else
  287. ret = ov9640_reg_rmw(client, OV9640_MVFP,
  288. 0, OV9640_MVFP_H);
  289. break;
  290. }
  291. return ret;
  292. }
  293. /* Get chip identification */
  294. static int ov9640_g_chip_ident(struct v4l2_subdev *sd,
  295. struct v4l2_dbg_chip_ident *id)
  296. {
  297. struct ov9640_priv *priv = to_ov9640_sensor(sd);
  298. id->ident = priv->model;
  299. id->revision = priv->revision;
  300. return 0;
  301. }
  302. #ifdef CONFIG_VIDEO_ADV_DEBUG
  303. static int ov9640_get_register(struct v4l2_subdev *sd,
  304. struct v4l2_dbg_register *reg)
  305. {
  306. struct i2c_client *client = v4l2_get_subdevdata(sd);
  307. int ret;
  308. u8 val;
  309. if (reg->reg & ~0xff)
  310. return -EINVAL;
  311. reg->size = 1;
  312. ret = ov9640_reg_read(client, reg->reg, &val);
  313. if (ret)
  314. return ret;
  315. reg->val = (__u64)val;
  316. return 0;
  317. }
  318. static int ov9640_set_register(struct v4l2_subdev *sd,
  319. struct v4l2_dbg_register *reg)
  320. {
  321. struct i2c_client *client = v4l2_get_subdevdata(sd);
  322. if (reg->reg & ~0xff || reg->val & ~0xff)
  323. return -EINVAL;
  324. return ov9640_reg_write(client, reg->reg, reg->val);
  325. }
  326. #endif
  327. /* select nearest higher resolution for capture */
  328. static void ov9640_res_roundup(u32 *width, u32 *height)
  329. {
  330. int i;
  331. enum { QQCIF, QQVGA, QCIF, QVGA, CIF, VGA, SXGA };
  332. int res_x[] = { 88, 160, 176, 320, 352, 640, 1280 };
  333. int res_y[] = { 72, 120, 144, 240, 288, 480, 960 };
  334. for (i = 0; i < ARRAY_SIZE(res_x); i++) {
  335. if (res_x[i] >= *width && res_y[i] >= *height) {
  336. *width = res_x[i];
  337. *height = res_y[i];
  338. return;
  339. }
  340. }
  341. *width = res_x[SXGA];
  342. *height = res_y[SXGA];
  343. }
  344. /* Prepare necessary register changes depending on color encoding */
  345. static void ov9640_alter_regs(enum v4l2_mbus_pixelcode code,
  346. struct ov9640_reg_alt *alt)
  347. {
  348. switch (code) {
  349. default:
  350. case V4L2_MBUS_FMT_UYVY8_2X8:
  351. alt->com12 = OV9640_COM12_YUV_AVG;
  352. alt->com13 = OV9640_COM13_Y_DELAY_EN |
  353. OV9640_COM13_YUV_DLY(0x01);
  354. break;
  355. case V4L2_MBUS_FMT_RGB555_2X8_PADHI_LE:
  356. alt->com7 = OV9640_COM7_RGB;
  357. alt->com13 = OV9640_COM13_RGB_AVG;
  358. alt->com15 = OV9640_COM15_RGB_555;
  359. break;
  360. case V4L2_MBUS_FMT_RGB565_2X8_LE:
  361. alt->com7 = OV9640_COM7_RGB;
  362. alt->com13 = OV9640_COM13_RGB_AVG;
  363. alt->com15 = OV9640_COM15_RGB_565;
  364. break;
  365. };
  366. }
  367. /* Setup registers according to resolution and color encoding */
  368. static int ov9640_write_regs(struct i2c_client *client, u32 width,
  369. enum v4l2_mbus_pixelcode code, struct ov9640_reg_alt *alts)
  370. {
  371. const struct ov9640_reg *ov9640_regs, *matrix_regs;
  372. int ov9640_regs_len, matrix_regs_len;
  373. int i, ret;
  374. u8 val;
  375. /* select register configuration for given resolution */
  376. switch (width) {
  377. case W_QQCIF:
  378. ov9640_regs = ov9640_regs_qqcif;
  379. ov9640_regs_len = ARRAY_SIZE(ov9640_regs_qqcif);
  380. break;
  381. case W_QQVGA:
  382. ov9640_regs = ov9640_regs_qqvga;
  383. ov9640_regs_len = ARRAY_SIZE(ov9640_regs_qqvga);
  384. break;
  385. case W_QCIF:
  386. ov9640_regs = ov9640_regs_qcif;
  387. ov9640_regs_len = ARRAY_SIZE(ov9640_regs_qcif);
  388. break;
  389. case W_QVGA:
  390. ov9640_regs = ov9640_regs_qvga;
  391. ov9640_regs_len = ARRAY_SIZE(ov9640_regs_qvga);
  392. break;
  393. case W_CIF:
  394. ov9640_regs = ov9640_regs_cif;
  395. ov9640_regs_len = ARRAY_SIZE(ov9640_regs_cif);
  396. break;
  397. case W_VGA:
  398. ov9640_regs = ov9640_regs_vga;
  399. ov9640_regs_len = ARRAY_SIZE(ov9640_regs_vga);
  400. break;
  401. case W_SXGA:
  402. ov9640_regs = ov9640_regs_sxga;
  403. ov9640_regs_len = ARRAY_SIZE(ov9640_regs_sxga);
  404. break;
  405. default:
  406. dev_err(&client->dev, "Failed to select resolution!\n");
  407. return -EINVAL;
  408. }
  409. /* select color matrix configuration for given color encoding */
  410. if (code == V4L2_MBUS_FMT_UYVY8_2X8) {
  411. matrix_regs = ov9640_regs_yuv;
  412. matrix_regs_len = ARRAY_SIZE(ov9640_regs_yuv);
  413. } else {
  414. matrix_regs = ov9640_regs_rgb;
  415. matrix_regs_len = ARRAY_SIZE(ov9640_regs_rgb);
  416. }
  417. /* write register settings into the module */
  418. for (i = 0; i < ov9640_regs_len; i++) {
  419. val = ov9640_regs[i].val;
  420. switch (ov9640_regs[i].reg) {
  421. case OV9640_COM7:
  422. val |= alts->com7;
  423. break;
  424. case OV9640_COM12:
  425. val |= alts->com12;
  426. break;
  427. case OV9640_COM13:
  428. val |= alts->com13;
  429. break;
  430. case OV9640_COM15:
  431. val |= alts->com15;
  432. break;
  433. }
  434. ret = ov9640_reg_write(client, ov9640_regs[i].reg, val);
  435. if (ret)
  436. return ret;
  437. }
  438. /* write color matrix configuration into the module */
  439. for (i = 0; i < matrix_regs_len; i++) {
  440. ret = ov9640_reg_write(client, matrix_regs[i].reg,
  441. matrix_regs[i].val);
  442. if (ret)
  443. return ret;
  444. }
  445. return 0;
  446. }
  447. /* program default register values */
  448. static int ov9640_prog_dflt(struct i2c_client *client)
  449. {
  450. int i, ret;
  451. for (i = 0; i < ARRAY_SIZE(ov9640_regs_dflt); i++) {
  452. ret = ov9640_reg_write(client, ov9640_regs_dflt[i].reg,
  453. ov9640_regs_dflt[i].val);
  454. if (ret)
  455. return ret;
  456. }
  457. /* wait for the changes to actually happen, 140ms are not enough yet */
  458. mdelay(150);
  459. return 0;
  460. }
  461. /* set the format we will capture in */
  462. static int ov9640_s_fmt(struct v4l2_subdev *sd,
  463. struct v4l2_mbus_framefmt *mf)
  464. {
  465. struct i2c_client *client = v4l2_get_subdevdata(sd);
  466. struct ov9640_reg_alt alts = {0};
  467. enum v4l2_colorspace cspace;
  468. enum v4l2_mbus_pixelcode code = mf->code;
  469. int ret;
  470. ov9640_res_roundup(&mf->width, &mf->height);
  471. ov9640_alter_regs(mf->code, &alts);
  472. ov9640_reset(client);
  473. ret = ov9640_prog_dflt(client);
  474. if (ret)
  475. return ret;
  476. switch (code) {
  477. case V4L2_MBUS_FMT_RGB555_2X8_PADHI_LE:
  478. case V4L2_MBUS_FMT_RGB565_2X8_LE:
  479. cspace = V4L2_COLORSPACE_SRGB;
  480. break;
  481. default:
  482. code = V4L2_MBUS_FMT_UYVY8_2X8;
  483. case V4L2_MBUS_FMT_UYVY8_2X8:
  484. cspace = V4L2_COLORSPACE_JPEG;
  485. }
  486. ret = ov9640_write_regs(client, mf->width, code, &alts);
  487. if (!ret) {
  488. mf->code = code;
  489. mf->colorspace = cspace;
  490. }
  491. return ret;
  492. }
  493. static int ov9640_try_fmt(struct v4l2_subdev *sd,
  494. struct v4l2_mbus_framefmt *mf)
  495. {
  496. ov9640_res_roundup(&mf->width, &mf->height);
  497. mf->field = V4L2_FIELD_NONE;
  498. switch (mf->code) {
  499. case V4L2_MBUS_FMT_RGB555_2X8_PADHI_LE:
  500. case V4L2_MBUS_FMT_RGB565_2X8_LE:
  501. mf->colorspace = V4L2_COLORSPACE_SRGB;
  502. break;
  503. default:
  504. mf->code = V4L2_MBUS_FMT_UYVY8_2X8;
  505. case V4L2_MBUS_FMT_UYVY8_2X8:
  506. mf->colorspace = V4L2_COLORSPACE_JPEG;
  507. }
  508. return 0;
  509. }
  510. static int ov9640_enum_fmt(struct v4l2_subdev *sd, unsigned int index,
  511. enum v4l2_mbus_pixelcode *code)
  512. {
  513. if (index >= ARRAY_SIZE(ov9640_codes))
  514. return -EINVAL;
  515. *code = ov9640_codes[index];
  516. return 0;
  517. }
  518. static int ov9640_g_crop(struct v4l2_subdev *sd, struct v4l2_crop *a)
  519. {
  520. a->c.left = 0;
  521. a->c.top = 0;
  522. a->c.width = W_SXGA;
  523. a->c.height = H_SXGA;
  524. a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  525. return 0;
  526. }
  527. static int ov9640_cropcap(struct v4l2_subdev *sd, struct v4l2_cropcap *a)
  528. {
  529. a->bounds.left = 0;
  530. a->bounds.top = 0;
  531. a->bounds.width = W_SXGA;
  532. a->bounds.height = H_SXGA;
  533. a->defrect = a->bounds;
  534. a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  535. a->pixelaspect.numerator = 1;
  536. a->pixelaspect.denominator = 1;
  537. return 0;
  538. }
  539. static int ov9640_video_probe(struct soc_camera_device *icd,
  540. struct i2c_client *client)
  541. {
  542. struct v4l2_subdev *sd = i2c_get_clientdata(client);
  543. struct ov9640_priv *priv = to_ov9640_sensor(sd);
  544. u8 pid, ver, midh, midl;
  545. const char *devname;
  546. int ret = 0;
  547. /* We must have a parent by now. And it cannot be a wrong one. */
  548. BUG_ON(!icd->parent ||
  549. to_soc_camera_host(icd->parent)->nr != icd->iface);
  550. /*
  551. * check and show product ID and manufacturer ID
  552. */
  553. ret = ov9640_reg_read(client, OV9640_PID, &pid);
  554. if (ret)
  555. goto err;
  556. ret = ov9640_reg_read(client, OV9640_VER, &ver);
  557. if (ret)
  558. goto err;
  559. ret = ov9640_reg_read(client, OV9640_MIDH, &midh);
  560. if (ret)
  561. goto err;
  562. ret = ov9640_reg_read(client, OV9640_MIDL, &midl);
  563. if (ret)
  564. goto err;
  565. switch (VERSION(pid, ver)) {
  566. case OV9640_V2:
  567. devname = "ov9640";
  568. priv->model = V4L2_IDENT_OV9640;
  569. priv->revision = 2;
  570. case OV9640_V3:
  571. devname = "ov9640";
  572. priv->model = V4L2_IDENT_OV9640;
  573. priv->revision = 3;
  574. break;
  575. default:
  576. dev_err(&client->dev, "Product ID error %x:%x\n", pid, ver);
  577. ret = -ENODEV;
  578. goto err;
  579. }
  580. dev_info(&client->dev, "%s Product ID %0x:%0x Manufacturer ID %x:%x\n",
  581. devname, pid, ver, midh, midl);
  582. err:
  583. return ret;
  584. }
  585. static struct soc_camera_ops ov9640_ops = {
  586. .controls = ov9640_controls,
  587. .num_controls = ARRAY_SIZE(ov9640_controls),
  588. };
  589. static struct v4l2_subdev_core_ops ov9640_core_ops = {
  590. .g_ctrl = ov9640_g_ctrl,
  591. .s_ctrl = ov9640_s_ctrl,
  592. .g_chip_ident = ov9640_g_chip_ident,
  593. #ifdef CONFIG_VIDEO_ADV_DEBUG
  594. .g_register = ov9640_get_register,
  595. .s_register = ov9640_set_register,
  596. #endif
  597. };
  598. /* Request bus settings on camera side */
  599. static int ov9640_g_mbus_config(struct v4l2_subdev *sd,
  600. struct v4l2_mbus_config *cfg)
  601. {
  602. struct i2c_client *client = v4l2_get_subdevdata(sd);
  603. struct soc_camera_device *icd = client->dev.platform_data;
  604. struct soc_camera_link *icl = to_soc_camera_link(icd);
  605. cfg->flags = V4L2_MBUS_PCLK_SAMPLE_RISING | V4L2_MBUS_MASTER |
  606. V4L2_MBUS_VSYNC_ACTIVE_HIGH | V4L2_MBUS_HSYNC_ACTIVE_HIGH |
  607. V4L2_MBUS_DATA_ACTIVE_HIGH;
  608. cfg->type = V4L2_MBUS_PARALLEL;
  609. cfg->flags = soc_camera_apply_board_flags(icl, cfg);
  610. return 0;
  611. }
  612. static struct v4l2_subdev_video_ops ov9640_video_ops = {
  613. .s_stream = ov9640_s_stream,
  614. .s_mbus_fmt = ov9640_s_fmt,
  615. .try_mbus_fmt = ov9640_try_fmt,
  616. .enum_mbus_fmt = ov9640_enum_fmt,
  617. .cropcap = ov9640_cropcap,
  618. .g_crop = ov9640_g_crop,
  619. .g_mbus_config = ov9640_g_mbus_config,
  620. };
  621. static struct v4l2_subdev_ops ov9640_subdev_ops = {
  622. .core = &ov9640_core_ops,
  623. .video = &ov9640_video_ops,
  624. };
  625. /*
  626. * i2c_driver function
  627. */
  628. static int ov9640_probe(struct i2c_client *client,
  629. const struct i2c_device_id *did)
  630. {
  631. struct ov9640_priv *priv;
  632. struct soc_camera_device *icd = client->dev.platform_data;
  633. struct soc_camera_link *icl;
  634. int ret;
  635. if (!icd) {
  636. dev_err(&client->dev, "Missing soc-camera data!\n");
  637. return -EINVAL;
  638. }
  639. icl = to_soc_camera_link(icd);
  640. if (!icl) {
  641. dev_err(&client->dev, "Missing platform_data for driver\n");
  642. return -EINVAL;
  643. }
  644. priv = kzalloc(sizeof(struct ov9640_priv), GFP_KERNEL);
  645. if (!priv) {
  646. dev_err(&client->dev,
  647. "Failed to allocate memory for private data!\n");
  648. return -ENOMEM;
  649. }
  650. v4l2_i2c_subdev_init(&priv->subdev, client, &ov9640_subdev_ops);
  651. icd->ops = &ov9640_ops;
  652. ret = ov9640_video_probe(icd, client);
  653. if (ret) {
  654. icd->ops = NULL;
  655. kfree(priv);
  656. }
  657. return ret;
  658. }
  659. static int ov9640_remove(struct i2c_client *client)
  660. {
  661. struct v4l2_subdev *sd = i2c_get_clientdata(client);
  662. struct ov9640_priv *priv = to_ov9640_sensor(sd);
  663. kfree(priv);
  664. return 0;
  665. }
  666. static const struct i2c_device_id ov9640_id[] = {
  667. { "ov9640", 0 },
  668. { }
  669. };
  670. MODULE_DEVICE_TABLE(i2c, ov9640_id);
  671. static struct i2c_driver ov9640_i2c_driver = {
  672. .driver = {
  673. .name = "ov9640",
  674. },
  675. .probe = ov9640_probe,
  676. .remove = ov9640_remove,
  677. .id_table = ov9640_id,
  678. };
  679. static int __init ov9640_module_init(void)
  680. {
  681. return i2c_add_driver(&ov9640_i2c_driver);
  682. }
  683. static void __exit ov9640_module_exit(void)
  684. {
  685. i2c_del_driver(&ov9640_i2c_driver);
  686. }
  687. module_init(ov9640_module_init);
  688. module_exit(ov9640_module_exit);
  689. MODULE_DESCRIPTION("SoC Camera driver for OmniVision OV96xx");
  690. MODULE_AUTHOR("Marek Vasut <marek.vasut@gmail.com>");
  691. MODULE_LICENSE("GPL v2");