ov6650.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072
  1. /*
  2. * V4L2 subdevice driver for OmniVision OV6650 Camera Sensor
  3. *
  4. * Copyright (C) 2010 Janusz Krzysztofik <jkrzyszt@tis.icnet.pl>
  5. *
  6. * Based on OmniVision OV96xx Camera Driver
  7. * Copyright (C) 2009 Marek Vasut <marek.vasut@gmail.com>
  8. *
  9. * Based on ov772x camera driver:
  10. * Copyright (C) 2008 Renesas Solutions Corp.
  11. * Kuninori Morimoto <morimoto.kuninori@renesas.com>
  12. *
  13. * Based on ov7670 and soc_camera_platform driver,
  14. * Copyright 2006-7 Jonathan Corbet <corbet@lwn.net>
  15. * Copyright (C) 2008 Magnus Damm
  16. * Copyright (C) 2008, Guennadi Liakhovetski <kernel@pengutronix.de>
  17. *
  18. * Hardware specific bits initialy based on former work by Matt Callow
  19. * drivers/media/video/omap/sensor_ov6650.c
  20. * Copyright (C) 2006 Matt Callow
  21. *
  22. * This program is free software; you can redistribute it and/or modify
  23. * it under the terms of the GNU General Public License version 2 as
  24. * published by the Free Software Foundation.
  25. */
  26. #include <linux/bitops.h>
  27. #include <linux/delay.h>
  28. #include <linux/i2c.h>
  29. #include <linux/slab.h>
  30. #include <linux/v4l2-mediabus.h>
  31. #include <linux/module.h>
  32. #include <media/v4l2-clk.h>
  33. #include <media/v4l2-ctrls.h>
  34. #include <media/v4l2-device.h>
  35. /* Register definitions */
  36. #define REG_GAIN 0x00 /* range 00 - 3F */
  37. #define REG_BLUE 0x01
  38. #define REG_RED 0x02
  39. #define REG_SAT 0x03 /* [7:4] saturation [0:3] reserved */
  40. #define REG_HUE 0x04 /* [7:6] rsrvd [5] hue en [4:0] hue */
  41. #define REG_BRT 0x06
  42. #define REG_PIDH 0x0a
  43. #define REG_PIDL 0x0b
  44. #define REG_AECH 0x10
  45. #define REG_CLKRC 0x11 /* Data Format and Internal Clock */
  46. /* [7:6] Input system clock (MHz)*/
  47. /* 00=8, 01=12, 10=16, 11=24 */
  48. /* [5:0]: Internal Clock Pre-Scaler */
  49. #define REG_COMA 0x12 /* [7] Reset */
  50. #define REG_COMB 0x13
  51. #define REG_COMC 0x14
  52. #define REG_COMD 0x15
  53. #define REG_COML 0x16
  54. #define REG_HSTRT 0x17
  55. #define REG_HSTOP 0x18
  56. #define REG_VSTRT 0x19
  57. #define REG_VSTOP 0x1a
  58. #define REG_PSHFT 0x1b
  59. #define REG_MIDH 0x1c
  60. #define REG_MIDL 0x1d
  61. #define REG_HSYNS 0x1e
  62. #define REG_HSYNE 0x1f
  63. #define REG_COME 0x20
  64. #define REG_YOFF 0x21
  65. #define REG_UOFF 0x22
  66. #define REG_VOFF 0x23
  67. #define REG_AEW 0x24
  68. #define REG_AEB 0x25
  69. #define REG_COMF 0x26
  70. #define REG_COMG 0x27
  71. #define REG_COMH 0x28
  72. #define REG_COMI 0x29
  73. #define REG_FRARL 0x2b
  74. #define REG_COMJ 0x2c
  75. #define REG_COMK 0x2d
  76. #define REG_AVGY 0x2e
  77. #define REG_REF0 0x2f
  78. #define REG_REF1 0x30
  79. #define REG_REF2 0x31
  80. #define REG_FRAJH 0x32
  81. #define REG_FRAJL 0x33
  82. #define REG_FACT 0x34
  83. #define REG_L1AEC 0x35
  84. #define REG_AVGU 0x36
  85. #define REG_AVGV 0x37
  86. #define REG_SPCB 0x60
  87. #define REG_SPCC 0x61
  88. #define REG_GAM1 0x62
  89. #define REG_GAM2 0x63
  90. #define REG_GAM3 0x64
  91. #define REG_SPCD 0x65
  92. #define REG_SPCE 0x68
  93. #define REG_ADCL 0x69
  94. #define REG_RMCO 0x6c
  95. #define REG_GMCO 0x6d
  96. #define REG_BMCO 0x6e
  97. /* Register bits, values, etc. */
  98. #define OV6650_PIDH 0x66 /* high byte of product ID number */
  99. #define OV6650_PIDL 0x50 /* low byte of product ID number */
  100. #define OV6650_MIDH 0x7F /* high byte of mfg ID */
  101. #define OV6650_MIDL 0xA2 /* low byte of mfg ID */
  102. #define DEF_GAIN 0x00
  103. #define DEF_BLUE 0x80
  104. #define DEF_RED 0x80
  105. #define SAT_SHIFT 4
  106. #define SAT_MASK (0xf << SAT_SHIFT)
  107. #define SET_SAT(x) (((x) << SAT_SHIFT) & SAT_MASK)
  108. #define HUE_EN BIT(5)
  109. #define HUE_MASK 0x1f
  110. #define DEF_HUE 0x10
  111. #define SET_HUE(x) (HUE_EN | ((x) & HUE_MASK))
  112. #define DEF_AECH 0x4D
  113. #define CLKRC_6MHz 0x00
  114. #define CLKRC_12MHz 0x40
  115. #define CLKRC_16MHz 0x80
  116. #define CLKRC_24MHz 0xc0
  117. #define CLKRC_DIV_MASK 0x3f
  118. #define GET_CLKRC_DIV(x) (((x) & CLKRC_DIV_MASK) + 1)
  119. #define COMA_RESET BIT(7)
  120. #define COMA_QCIF BIT(5)
  121. #define COMA_RAW_RGB BIT(4)
  122. #define COMA_RGB BIT(3)
  123. #define COMA_BW BIT(2)
  124. #define COMA_WORD_SWAP BIT(1)
  125. #define COMA_BYTE_SWAP BIT(0)
  126. #define DEF_COMA 0x00
  127. #define COMB_FLIP_V BIT(7)
  128. #define COMB_FLIP_H BIT(5)
  129. #define COMB_BAND_FILTER BIT(4)
  130. #define COMB_AWB BIT(2)
  131. #define COMB_AGC BIT(1)
  132. #define COMB_AEC BIT(0)
  133. #define DEF_COMB 0x5f
  134. #define COML_ONE_CHANNEL BIT(7)
  135. #define DEF_HSTRT 0x24
  136. #define DEF_HSTOP 0xd4
  137. #define DEF_VSTRT 0x04
  138. #define DEF_VSTOP 0x94
  139. #define COMF_HREF_LOW BIT(4)
  140. #define COMJ_PCLK_RISING BIT(4)
  141. #define COMJ_VSYNC_HIGH BIT(0)
  142. /* supported resolutions */
  143. #define W_QCIF (DEF_HSTOP - DEF_HSTRT)
  144. #define W_CIF (W_QCIF << 1)
  145. #define H_QCIF (DEF_VSTOP - DEF_VSTRT)
  146. #define H_CIF (H_QCIF << 1)
  147. #define FRAME_RATE_MAX 30
  148. struct ov6650_reg {
  149. u8 reg;
  150. u8 val;
  151. };
  152. struct ov6650 {
  153. struct v4l2_subdev subdev;
  154. struct v4l2_ctrl_handler hdl;
  155. struct {
  156. /* exposure/autoexposure cluster */
  157. struct v4l2_ctrl *autoexposure;
  158. struct v4l2_ctrl *exposure;
  159. };
  160. struct {
  161. /* gain/autogain cluster */
  162. struct v4l2_ctrl *autogain;
  163. struct v4l2_ctrl *gain;
  164. };
  165. struct {
  166. /* blue/red/autowhitebalance cluster */
  167. struct v4l2_ctrl *autowb;
  168. struct v4l2_ctrl *blue;
  169. struct v4l2_ctrl *red;
  170. };
  171. struct v4l2_clk *clk;
  172. bool half_scale; /* scale down output by 2 */
  173. struct v4l2_rect rect; /* sensor cropping window */
  174. unsigned long pclk_limit; /* from host */
  175. unsigned long pclk_max; /* from resolution and format */
  176. struct v4l2_fract tpf; /* as requested with s_frame_interval */
  177. u32 code;
  178. };
  179. static u32 ov6650_codes[] = {
  180. MEDIA_BUS_FMT_YUYV8_2X8,
  181. MEDIA_BUS_FMT_UYVY8_2X8,
  182. MEDIA_BUS_FMT_YVYU8_2X8,
  183. MEDIA_BUS_FMT_VYUY8_2X8,
  184. MEDIA_BUS_FMT_SBGGR8_1X8,
  185. MEDIA_BUS_FMT_Y8_1X8,
  186. };
  187. static const struct v4l2_mbus_framefmt ov6650_def_fmt = {
  188. .width = W_CIF,
  189. .height = H_CIF,
  190. .code = MEDIA_BUS_FMT_SBGGR8_1X8,
  191. .colorspace = V4L2_COLORSPACE_SRGB,
  192. .field = V4L2_FIELD_NONE,
  193. .ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT,
  194. .quantization = V4L2_QUANTIZATION_DEFAULT,
  195. .xfer_func = V4L2_XFER_FUNC_DEFAULT,
  196. };
  197. /* read a register */
  198. static int ov6650_reg_read(struct i2c_client *client, u8 reg, u8 *val)
  199. {
  200. int ret;
  201. u8 data = reg;
  202. struct i2c_msg msg = {
  203. .addr = client->addr,
  204. .flags = 0,
  205. .len = 1,
  206. .buf = &data,
  207. };
  208. ret = i2c_transfer(client->adapter, &msg, 1);
  209. if (ret < 0)
  210. goto err;
  211. msg.flags = I2C_M_RD;
  212. ret = i2c_transfer(client->adapter, &msg, 1);
  213. if (ret < 0)
  214. goto err;
  215. *val = data;
  216. return 0;
  217. err:
  218. dev_err(&client->dev, "Failed reading register 0x%02x!\n", reg);
  219. return ret;
  220. }
  221. /* write a register */
  222. static int ov6650_reg_write(struct i2c_client *client, u8 reg, u8 val)
  223. {
  224. int ret;
  225. unsigned char data[2] = { reg, val };
  226. struct i2c_msg msg = {
  227. .addr = client->addr,
  228. .flags = 0,
  229. .len = 2,
  230. .buf = data,
  231. };
  232. ret = i2c_transfer(client->adapter, &msg, 1);
  233. udelay(100);
  234. if (ret < 0) {
  235. dev_err(&client->dev, "Failed writing register 0x%02x!\n", reg);
  236. return ret;
  237. }
  238. return 0;
  239. }
  240. /* Read a register, alter its bits, write it back */
  241. static int ov6650_reg_rmw(struct i2c_client *client, u8 reg, u8 set, u8 mask)
  242. {
  243. u8 val;
  244. int ret;
  245. ret = ov6650_reg_read(client, reg, &val);
  246. if (ret) {
  247. dev_err(&client->dev,
  248. "[Read]-Modify-Write of register 0x%02x failed!\n",
  249. reg);
  250. return ret;
  251. }
  252. val &= ~mask;
  253. val |= set;
  254. ret = ov6650_reg_write(client, reg, val);
  255. if (ret)
  256. dev_err(&client->dev,
  257. "Read-Modify-[Write] of register 0x%02x failed!\n",
  258. reg);
  259. return ret;
  260. }
  261. static struct ov6650 *to_ov6650(const struct i2c_client *client)
  262. {
  263. return container_of(i2c_get_clientdata(client), struct ov6650, subdev);
  264. }
  265. /* Start/Stop streaming from the device */
  266. static int ov6650_s_stream(struct v4l2_subdev *sd, int enable)
  267. {
  268. return 0;
  269. }
  270. /* Get status of additional camera capabilities */
  271. static int ov6550_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
  272. {
  273. struct ov6650 *priv = container_of(ctrl->handler, struct ov6650, hdl);
  274. struct v4l2_subdev *sd = &priv->subdev;
  275. struct i2c_client *client = v4l2_get_subdevdata(sd);
  276. uint8_t reg, reg2;
  277. int ret;
  278. switch (ctrl->id) {
  279. case V4L2_CID_AUTOGAIN:
  280. ret = ov6650_reg_read(client, REG_GAIN, &reg);
  281. if (!ret)
  282. priv->gain->val = reg;
  283. return ret;
  284. case V4L2_CID_AUTO_WHITE_BALANCE:
  285. ret = ov6650_reg_read(client, REG_BLUE, &reg);
  286. if (!ret)
  287. ret = ov6650_reg_read(client, REG_RED, &reg2);
  288. if (!ret) {
  289. priv->blue->val = reg;
  290. priv->red->val = reg2;
  291. }
  292. return ret;
  293. case V4L2_CID_EXPOSURE_AUTO:
  294. ret = ov6650_reg_read(client, REG_AECH, &reg);
  295. if (!ret)
  296. priv->exposure->val = reg;
  297. return ret;
  298. }
  299. return -EINVAL;
  300. }
  301. /* Set status of additional camera capabilities */
  302. static int ov6550_s_ctrl(struct v4l2_ctrl *ctrl)
  303. {
  304. struct ov6650 *priv = container_of(ctrl->handler, struct ov6650, hdl);
  305. struct v4l2_subdev *sd = &priv->subdev;
  306. struct i2c_client *client = v4l2_get_subdevdata(sd);
  307. int ret;
  308. switch (ctrl->id) {
  309. case V4L2_CID_AUTOGAIN:
  310. ret = ov6650_reg_rmw(client, REG_COMB,
  311. ctrl->val ? COMB_AGC : 0, COMB_AGC);
  312. if (!ret && !ctrl->val)
  313. ret = ov6650_reg_write(client, REG_GAIN, priv->gain->val);
  314. return ret;
  315. case V4L2_CID_AUTO_WHITE_BALANCE:
  316. ret = ov6650_reg_rmw(client, REG_COMB,
  317. ctrl->val ? COMB_AWB : 0, COMB_AWB);
  318. if (!ret && !ctrl->val) {
  319. ret = ov6650_reg_write(client, REG_BLUE, priv->blue->val);
  320. if (!ret)
  321. ret = ov6650_reg_write(client, REG_RED,
  322. priv->red->val);
  323. }
  324. return ret;
  325. case V4L2_CID_SATURATION:
  326. return ov6650_reg_rmw(client, REG_SAT, SET_SAT(ctrl->val),
  327. SAT_MASK);
  328. case V4L2_CID_HUE:
  329. return ov6650_reg_rmw(client, REG_HUE, SET_HUE(ctrl->val),
  330. HUE_MASK);
  331. case V4L2_CID_BRIGHTNESS:
  332. return ov6650_reg_write(client, REG_BRT, ctrl->val);
  333. case V4L2_CID_EXPOSURE_AUTO:
  334. ret = ov6650_reg_rmw(client, REG_COMB, ctrl->val ==
  335. V4L2_EXPOSURE_AUTO ? COMB_AEC : 0, COMB_AEC);
  336. if (!ret && ctrl->val == V4L2_EXPOSURE_MANUAL)
  337. ret = ov6650_reg_write(client, REG_AECH,
  338. priv->exposure->val);
  339. return ret;
  340. case V4L2_CID_GAMMA:
  341. return ov6650_reg_write(client, REG_GAM1, ctrl->val);
  342. case V4L2_CID_VFLIP:
  343. return ov6650_reg_rmw(client, REG_COMB,
  344. ctrl->val ? COMB_FLIP_V : 0, COMB_FLIP_V);
  345. case V4L2_CID_HFLIP:
  346. return ov6650_reg_rmw(client, REG_COMB,
  347. ctrl->val ? COMB_FLIP_H : 0, COMB_FLIP_H);
  348. }
  349. return -EINVAL;
  350. }
  351. #ifdef CONFIG_VIDEO_ADV_DEBUG
  352. static int ov6650_get_register(struct v4l2_subdev *sd,
  353. struct v4l2_dbg_register *reg)
  354. {
  355. struct i2c_client *client = v4l2_get_subdevdata(sd);
  356. int ret;
  357. u8 val;
  358. if (reg->reg & ~0xff)
  359. return -EINVAL;
  360. reg->size = 1;
  361. ret = ov6650_reg_read(client, reg->reg, &val);
  362. if (!ret)
  363. reg->val = (__u64)val;
  364. return ret;
  365. }
  366. static int ov6650_set_register(struct v4l2_subdev *sd,
  367. const struct v4l2_dbg_register *reg)
  368. {
  369. struct i2c_client *client = v4l2_get_subdevdata(sd);
  370. if (reg->reg & ~0xff || reg->val & ~0xff)
  371. return -EINVAL;
  372. return ov6650_reg_write(client, reg->reg, reg->val);
  373. }
  374. #endif
  375. static int ov6650_s_power(struct v4l2_subdev *sd, int on)
  376. {
  377. struct i2c_client *client = v4l2_get_subdevdata(sd);
  378. struct ov6650 *priv = to_ov6650(client);
  379. int ret = 0;
  380. if (on)
  381. ret = v4l2_clk_enable(priv->clk);
  382. else
  383. v4l2_clk_disable(priv->clk);
  384. return ret;
  385. }
  386. static int ov6650_get_selection(struct v4l2_subdev *sd,
  387. struct v4l2_subdev_pad_config *cfg,
  388. struct v4l2_subdev_selection *sel)
  389. {
  390. struct i2c_client *client = v4l2_get_subdevdata(sd);
  391. struct ov6650 *priv = to_ov6650(client);
  392. if (sel->which != V4L2_SUBDEV_FORMAT_ACTIVE)
  393. return -EINVAL;
  394. switch (sel->target) {
  395. case V4L2_SEL_TGT_CROP_BOUNDS:
  396. case V4L2_SEL_TGT_CROP_DEFAULT:
  397. sel->r.left = DEF_HSTRT << 1;
  398. sel->r.top = DEF_VSTRT << 1;
  399. sel->r.width = W_CIF;
  400. sel->r.height = H_CIF;
  401. return 0;
  402. case V4L2_SEL_TGT_CROP:
  403. sel->r = priv->rect;
  404. return 0;
  405. default:
  406. return -EINVAL;
  407. }
  408. }
  409. static int ov6650_set_selection(struct v4l2_subdev *sd,
  410. struct v4l2_subdev_pad_config *cfg,
  411. struct v4l2_subdev_selection *sel)
  412. {
  413. struct i2c_client *client = v4l2_get_subdevdata(sd);
  414. struct ov6650 *priv = to_ov6650(client);
  415. int ret;
  416. if (sel->which != V4L2_SUBDEV_FORMAT_ACTIVE ||
  417. sel->target != V4L2_SEL_TGT_CROP)
  418. return -EINVAL;
  419. v4l_bound_align_image(&sel->r.width, 2, W_CIF, 1,
  420. &sel->r.height, 2, H_CIF, 1, 0);
  421. v4l_bound_align_image(&sel->r.left, DEF_HSTRT << 1,
  422. (DEF_HSTRT << 1) + W_CIF - (__s32)sel->r.width, 1,
  423. &sel->r.top, DEF_VSTRT << 1,
  424. (DEF_VSTRT << 1) + H_CIF - (__s32)sel->r.height,
  425. 1, 0);
  426. ret = ov6650_reg_write(client, REG_HSTRT, sel->r.left >> 1);
  427. if (!ret) {
  428. priv->rect.width += priv->rect.left - sel->r.left;
  429. priv->rect.left = sel->r.left;
  430. ret = ov6650_reg_write(client, REG_HSTOP,
  431. (sel->r.left + sel->r.width) >> 1);
  432. }
  433. if (!ret) {
  434. priv->rect.width = sel->r.width;
  435. ret = ov6650_reg_write(client, REG_VSTRT, sel->r.top >> 1);
  436. }
  437. if (!ret) {
  438. priv->rect.height += priv->rect.top - sel->r.top;
  439. priv->rect.top = sel->r.top;
  440. ret = ov6650_reg_write(client, REG_VSTOP,
  441. (sel->r.top + sel->r.height) >> 1);
  442. }
  443. if (!ret)
  444. priv->rect.height = sel->r.height;
  445. return ret;
  446. }
  447. static int ov6650_get_fmt(struct v4l2_subdev *sd,
  448. struct v4l2_subdev_pad_config *cfg,
  449. struct v4l2_subdev_format *format)
  450. {
  451. struct v4l2_mbus_framefmt *mf = &format->format;
  452. struct i2c_client *client = v4l2_get_subdevdata(sd);
  453. struct ov6650 *priv = to_ov6650(client);
  454. if (format->pad)
  455. return -EINVAL;
  456. /* initialize response with default media bus frame format */
  457. *mf = ov6650_def_fmt;
  458. /* update media bus format code and frame size */
  459. if (format->which == V4L2_SUBDEV_FORMAT_TRY) {
  460. mf->width = cfg->try_fmt.width;
  461. mf->height = cfg->try_fmt.height;
  462. mf->code = cfg->try_fmt.code;
  463. } else {
  464. mf->width = priv->rect.width >> priv->half_scale;
  465. mf->height = priv->rect.height >> priv->half_scale;
  466. mf->code = priv->code;
  467. }
  468. return 0;
  469. }
  470. static bool is_unscaled_ok(int width, int height, struct v4l2_rect *rect)
  471. {
  472. return width > rect->width >> 1 || height > rect->height >> 1;
  473. }
  474. static u8 to_clkrc(struct v4l2_fract *timeperframe,
  475. unsigned long pclk_limit, unsigned long pclk_max)
  476. {
  477. unsigned long pclk;
  478. if (timeperframe->numerator && timeperframe->denominator)
  479. pclk = pclk_max * timeperframe->denominator /
  480. (FRAME_RATE_MAX * timeperframe->numerator);
  481. else
  482. pclk = pclk_max;
  483. if (pclk_limit && pclk_limit < pclk)
  484. pclk = pclk_limit;
  485. return (pclk_max - 1) / pclk;
  486. }
  487. /* set the format we will capture in */
  488. static int ov6650_s_fmt(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf)
  489. {
  490. struct i2c_client *client = v4l2_get_subdevdata(sd);
  491. struct ov6650 *priv = to_ov6650(client);
  492. bool half_scale = !is_unscaled_ok(mf->width, mf->height, &priv->rect);
  493. struct v4l2_subdev_selection sel = {
  494. .which = V4L2_SUBDEV_FORMAT_ACTIVE,
  495. .target = V4L2_SEL_TGT_CROP,
  496. .r.left = priv->rect.left + (priv->rect.width >> 1) -
  497. (mf->width >> (1 - half_scale)),
  498. .r.top = priv->rect.top + (priv->rect.height >> 1) -
  499. (mf->height >> (1 - half_scale)),
  500. .r.width = mf->width << half_scale,
  501. .r.height = mf->height << half_scale,
  502. };
  503. u32 code = mf->code;
  504. unsigned long mclk, pclk;
  505. u8 coma_set = 0, coma_mask = 0, coml_set, coml_mask, clkrc;
  506. int ret;
  507. /* select color matrix configuration for given color encoding */
  508. switch (code) {
  509. case MEDIA_BUS_FMT_Y8_1X8:
  510. dev_dbg(&client->dev, "pixel format GREY8_1X8\n");
  511. coma_mask |= COMA_RGB | COMA_WORD_SWAP | COMA_BYTE_SWAP;
  512. coma_set |= COMA_BW;
  513. break;
  514. case MEDIA_BUS_FMT_YUYV8_2X8:
  515. dev_dbg(&client->dev, "pixel format YUYV8_2X8_LE\n");
  516. coma_mask |= COMA_RGB | COMA_BW | COMA_BYTE_SWAP;
  517. coma_set |= COMA_WORD_SWAP;
  518. break;
  519. case MEDIA_BUS_FMT_YVYU8_2X8:
  520. dev_dbg(&client->dev, "pixel format YVYU8_2X8_LE (untested)\n");
  521. coma_mask |= COMA_RGB | COMA_BW | COMA_WORD_SWAP |
  522. COMA_BYTE_SWAP;
  523. break;
  524. case MEDIA_BUS_FMT_UYVY8_2X8:
  525. dev_dbg(&client->dev, "pixel format YUYV8_2X8_BE\n");
  526. if (half_scale) {
  527. coma_mask |= COMA_RGB | COMA_BW | COMA_WORD_SWAP;
  528. coma_set |= COMA_BYTE_SWAP;
  529. } else {
  530. coma_mask |= COMA_RGB | COMA_BW;
  531. coma_set |= COMA_BYTE_SWAP | COMA_WORD_SWAP;
  532. }
  533. break;
  534. case MEDIA_BUS_FMT_VYUY8_2X8:
  535. dev_dbg(&client->dev, "pixel format YVYU8_2X8_BE (untested)\n");
  536. if (half_scale) {
  537. coma_mask |= COMA_RGB | COMA_BW;
  538. coma_set |= COMA_BYTE_SWAP | COMA_WORD_SWAP;
  539. } else {
  540. coma_mask |= COMA_RGB | COMA_BW | COMA_WORD_SWAP;
  541. coma_set |= COMA_BYTE_SWAP;
  542. }
  543. break;
  544. case MEDIA_BUS_FMT_SBGGR8_1X8:
  545. dev_dbg(&client->dev, "pixel format SBGGR8_1X8 (untested)\n");
  546. coma_mask |= COMA_BW | COMA_BYTE_SWAP | COMA_WORD_SWAP;
  547. coma_set |= COMA_RAW_RGB | COMA_RGB;
  548. break;
  549. default:
  550. dev_err(&client->dev, "Pixel format not handled: 0x%x\n", code);
  551. return -EINVAL;
  552. }
  553. if (code == MEDIA_BUS_FMT_Y8_1X8 ||
  554. code == MEDIA_BUS_FMT_SBGGR8_1X8) {
  555. coml_mask = COML_ONE_CHANNEL;
  556. coml_set = 0;
  557. priv->pclk_max = 4000000;
  558. } else {
  559. coml_mask = 0;
  560. coml_set = COML_ONE_CHANNEL;
  561. priv->pclk_max = 8000000;
  562. }
  563. if (half_scale) {
  564. dev_dbg(&client->dev, "max resolution: QCIF\n");
  565. coma_set |= COMA_QCIF;
  566. priv->pclk_max /= 2;
  567. } else {
  568. dev_dbg(&client->dev, "max resolution: CIF\n");
  569. coma_mask |= COMA_QCIF;
  570. }
  571. clkrc = CLKRC_12MHz;
  572. mclk = 12000000;
  573. priv->pclk_limit = 1334000;
  574. dev_dbg(&client->dev, "using 12MHz input clock\n");
  575. clkrc |= to_clkrc(&priv->tpf, priv->pclk_limit, priv->pclk_max);
  576. pclk = priv->pclk_max / GET_CLKRC_DIV(clkrc);
  577. dev_dbg(&client->dev, "pixel clock divider: %ld.%ld\n",
  578. mclk / pclk, 10 * mclk % pclk / pclk);
  579. ret = ov6650_set_selection(sd, NULL, &sel);
  580. if (!ret)
  581. ret = ov6650_reg_rmw(client, REG_COMA, coma_set, coma_mask);
  582. if (!ret)
  583. ret = ov6650_reg_write(client, REG_CLKRC, clkrc);
  584. if (!ret) {
  585. priv->half_scale = half_scale;
  586. ret = ov6650_reg_rmw(client, REG_COML, coml_set, coml_mask);
  587. }
  588. if (!ret)
  589. priv->code = code;
  590. return ret;
  591. }
  592. static int ov6650_set_fmt(struct v4l2_subdev *sd,
  593. struct v4l2_subdev_pad_config *cfg,
  594. struct v4l2_subdev_format *format)
  595. {
  596. struct v4l2_mbus_framefmt *mf = &format->format;
  597. struct i2c_client *client = v4l2_get_subdevdata(sd);
  598. struct ov6650 *priv = to_ov6650(client);
  599. if (format->pad)
  600. return -EINVAL;
  601. if (is_unscaled_ok(mf->width, mf->height, &priv->rect))
  602. v4l_bound_align_image(&mf->width, 2, W_CIF, 1,
  603. &mf->height, 2, H_CIF, 1, 0);
  604. switch (mf->code) {
  605. case MEDIA_BUS_FMT_Y10_1X10:
  606. mf->code = MEDIA_BUS_FMT_Y8_1X8;
  607. /* fall through */
  608. case MEDIA_BUS_FMT_Y8_1X8:
  609. case MEDIA_BUS_FMT_YVYU8_2X8:
  610. case MEDIA_BUS_FMT_YUYV8_2X8:
  611. case MEDIA_BUS_FMT_VYUY8_2X8:
  612. case MEDIA_BUS_FMT_UYVY8_2X8:
  613. break;
  614. default:
  615. mf->code = MEDIA_BUS_FMT_SBGGR8_1X8;
  616. /* fall through */
  617. case MEDIA_BUS_FMT_SBGGR8_1X8:
  618. break;
  619. }
  620. if (format->which == V4L2_SUBDEV_FORMAT_TRY) {
  621. /* store media bus format code and frame size in pad config */
  622. cfg->try_fmt.width = mf->width;
  623. cfg->try_fmt.height = mf->height;
  624. cfg->try_fmt.code = mf->code;
  625. /* return default mbus frame format updated with pad config */
  626. *mf = ov6650_def_fmt;
  627. mf->width = cfg->try_fmt.width;
  628. mf->height = cfg->try_fmt.height;
  629. mf->code = cfg->try_fmt.code;
  630. } else {
  631. /* apply new media bus format code and frame size */
  632. int ret = ov6650_s_fmt(sd, mf);
  633. if (ret)
  634. return ret;
  635. /* return default format updated with active size and code */
  636. *mf = ov6650_def_fmt;
  637. mf->width = priv->rect.width >> priv->half_scale;
  638. mf->height = priv->rect.height >> priv->half_scale;
  639. mf->code = priv->code;
  640. }
  641. return 0;
  642. }
  643. static int ov6650_enum_mbus_code(struct v4l2_subdev *sd,
  644. struct v4l2_subdev_pad_config *cfg,
  645. struct v4l2_subdev_mbus_code_enum *code)
  646. {
  647. if (code->pad || code->index >= ARRAY_SIZE(ov6650_codes))
  648. return -EINVAL;
  649. code->code = ov6650_codes[code->index];
  650. return 0;
  651. }
  652. static int ov6650_g_frame_interval(struct v4l2_subdev *sd,
  653. struct v4l2_subdev_frame_interval *ival)
  654. {
  655. struct i2c_client *client = v4l2_get_subdevdata(sd);
  656. struct ov6650 *priv = to_ov6650(client);
  657. ival->interval.numerator = GET_CLKRC_DIV(to_clkrc(&priv->tpf,
  658. priv->pclk_limit, priv->pclk_max));
  659. ival->interval.denominator = FRAME_RATE_MAX;
  660. dev_dbg(&client->dev, "Frame interval: %u/%u s\n",
  661. ival->interval.numerator, ival->interval.denominator);
  662. return 0;
  663. }
  664. static int ov6650_s_frame_interval(struct v4l2_subdev *sd,
  665. struct v4l2_subdev_frame_interval *ival)
  666. {
  667. struct i2c_client *client = v4l2_get_subdevdata(sd);
  668. struct ov6650 *priv = to_ov6650(client);
  669. struct v4l2_fract *tpf = &ival->interval;
  670. int div, ret;
  671. u8 clkrc;
  672. if (tpf->numerator == 0 || tpf->denominator == 0)
  673. div = 1; /* Reset to full rate */
  674. else
  675. div = (tpf->numerator * FRAME_RATE_MAX) / tpf->denominator;
  676. if (div == 0)
  677. div = 1;
  678. else if (div > GET_CLKRC_DIV(CLKRC_DIV_MASK))
  679. div = GET_CLKRC_DIV(CLKRC_DIV_MASK);
  680. /*
  681. * Keep result to be used as tpf limit
  682. * for subseqent clock divider calculations
  683. */
  684. priv->tpf.numerator = div;
  685. priv->tpf.denominator = FRAME_RATE_MAX;
  686. clkrc = to_clkrc(&priv->tpf, priv->pclk_limit, priv->pclk_max);
  687. ret = ov6650_reg_rmw(client, REG_CLKRC, clkrc, CLKRC_DIV_MASK);
  688. if (!ret) {
  689. tpf->numerator = GET_CLKRC_DIV(clkrc);
  690. tpf->denominator = FRAME_RATE_MAX;
  691. }
  692. return ret;
  693. }
  694. /* Soft reset the camera. This has nothing to do with the RESET pin! */
  695. static int ov6650_reset(struct i2c_client *client)
  696. {
  697. int ret;
  698. dev_dbg(&client->dev, "reset\n");
  699. ret = ov6650_reg_rmw(client, REG_COMA, COMA_RESET, 0);
  700. if (ret)
  701. dev_err(&client->dev,
  702. "An error occurred while entering soft reset!\n");
  703. return ret;
  704. }
  705. /* program default register values */
  706. static int ov6650_prog_dflt(struct i2c_client *client)
  707. {
  708. int ret;
  709. dev_dbg(&client->dev, "initializing\n");
  710. ret = ov6650_reg_write(client, REG_COMA, 0); /* ~COMA_RESET */
  711. if (!ret)
  712. ret = ov6650_reg_rmw(client, REG_COMB, 0, COMB_BAND_FILTER);
  713. return ret;
  714. }
  715. static int ov6650_video_probe(struct i2c_client *client)
  716. {
  717. struct ov6650 *priv = to_ov6650(client);
  718. u8 pidh, pidl, midh, midl;
  719. int ret;
  720. priv->clk = v4l2_clk_get(&client->dev, NULL);
  721. if (IS_ERR(priv->clk)) {
  722. ret = PTR_ERR(priv->clk);
  723. dev_err(&client->dev, "v4l2_clk request err: %d\n", ret);
  724. return ret;
  725. }
  726. ret = ov6650_s_power(&priv->subdev, 1);
  727. if (ret < 0)
  728. goto eclkput;
  729. msleep(20);
  730. /*
  731. * check and show product ID and manufacturer ID
  732. */
  733. ret = ov6650_reg_read(client, REG_PIDH, &pidh);
  734. if (!ret)
  735. ret = ov6650_reg_read(client, REG_PIDL, &pidl);
  736. if (!ret)
  737. ret = ov6650_reg_read(client, REG_MIDH, &midh);
  738. if (!ret)
  739. ret = ov6650_reg_read(client, REG_MIDL, &midl);
  740. if (ret)
  741. goto done;
  742. if ((pidh != OV6650_PIDH) || (pidl != OV6650_PIDL)) {
  743. dev_err(&client->dev, "Product ID error 0x%02x:0x%02x\n",
  744. pidh, pidl);
  745. ret = -ENODEV;
  746. goto done;
  747. }
  748. dev_info(&client->dev,
  749. "ov6650 Product ID 0x%02x:0x%02x Manufacturer ID 0x%02x:0x%02x\n",
  750. pidh, pidl, midh, midl);
  751. ret = ov6650_reset(client);
  752. if (!ret)
  753. ret = ov6650_prog_dflt(client);
  754. if (!ret)
  755. ret = v4l2_ctrl_handler_setup(&priv->hdl);
  756. done:
  757. ov6650_s_power(&priv->subdev, 0);
  758. if (!ret)
  759. return 0;
  760. eclkput:
  761. v4l2_clk_put(priv->clk);
  762. return ret;
  763. }
  764. static const struct v4l2_ctrl_ops ov6550_ctrl_ops = {
  765. .g_volatile_ctrl = ov6550_g_volatile_ctrl,
  766. .s_ctrl = ov6550_s_ctrl,
  767. };
  768. static const struct v4l2_subdev_core_ops ov6650_core_ops = {
  769. #ifdef CONFIG_VIDEO_ADV_DEBUG
  770. .g_register = ov6650_get_register,
  771. .s_register = ov6650_set_register,
  772. #endif
  773. .s_power = ov6650_s_power,
  774. };
  775. /* Request bus settings on camera side */
  776. static int ov6650_g_mbus_config(struct v4l2_subdev *sd,
  777. struct v4l2_mbus_config *cfg)
  778. {
  779. cfg->flags = V4L2_MBUS_MASTER |
  780. V4L2_MBUS_PCLK_SAMPLE_RISING | V4L2_MBUS_PCLK_SAMPLE_FALLING |
  781. V4L2_MBUS_HSYNC_ACTIVE_HIGH | V4L2_MBUS_HSYNC_ACTIVE_LOW |
  782. V4L2_MBUS_VSYNC_ACTIVE_HIGH | V4L2_MBUS_VSYNC_ACTIVE_LOW |
  783. V4L2_MBUS_DATA_ACTIVE_HIGH;
  784. cfg->type = V4L2_MBUS_PARALLEL;
  785. return 0;
  786. }
  787. /* Alter bus settings on camera side */
  788. static int ov6650_s_mbus_config(struct v4l2_subdev *sd,
  789. const struct v4l2_mbus_config *cfg)
  790. {
  791. struct i2c_client *client = v4l2_get_subdevdata(sd);
  792. int ret;
  793. if (cfg->flags & V4L2_MBUS_PCLK_SAMPLE_RISING)
  794. ret = ov6650_reg_rmw(client, REG_COMJ, COMJ_PCLK_RISING, 0);
  795. else
  796. ret = ov6650_reg_rmw(client, REG_COMJ, 0, COMJ_PCLK_RISING);
  797. if (ret)
  798. return ret;
  799. if (cfg->flags & V4L2_MBUS_HSYNC_ACTIVE_LOW)
  800. ret = ov6650_reg_rmw(client, REG_COMF, COMF_HREF_LOW, 0);
  801. else
  802. ret = ov6650_reg_rmw(client, REG_COMF, 0, COMF_HREF_LOW);
  803. if (ret)
  804. return ret;
  805. if (cfg->flags & V4L2_MBUS_VSYNC_ACTIVE_HIGH)
  806. ret = ov6650_reg_rmw(client, REG_COMJ, COMJ_VSYNC_HIGH, 0);
  807. else
  808. ret = ov6650_reg_rmw(client, REG_COMJ, 0, COMJ_VSYNC_HIGH);
  809. return ret;
  810. }
  811. static const struct v4l2_subdev_video_ops ov6650_video_ops = {
  812. .s_stream = ov6650_s_stream,
  813. .g_frame_interval = ov6650_g_frame_interval,
  814. .s_frame_interval = ov6650_s_frame_interval,
  815. .g_mbus_config = ov6650_g_mbus_config,
  816. .s_mbus_config = ov6650_s_mbus_config,
  817. };
  818. static const struct v4l2_subdev_pad_ops ov6650_pad_ops = {
  819. .enum_mbus_code = ov6650_enum_mbus_code,
  820. .get_selection = ov6650_get_selection,
  821. .set_selection = ov6650_set_selection,
  822. .get_fmt = ov6650_get_fmt,
  823. .set_fmt = ov6650_set_fmt,
  824. };
  825. static const struct v4l2_subdev_ops ov6650_subdev_ops = {
  826. .core = &ov6650_core_ops,
  827. .video = &ov6650_video_ops,
  828. .pad = &ov6650_pad_ops,
  829. };
  830. /*
  831. * i2c_driver function
  832. */
  833. static int ov6650_probe(struct i2c_client *client,
  834. const struct i2c_device_id *did)
  835. {
  836. struct ov6650 *priv;
  837. int ret;
  838. priv = devm_kzalloc(&client->dev, sizeof(*priv), GFP_KERNEL);
  839. if (!priv)
  840. return -ENOMEM;
  841. v4l2_i2c_subdev_init(&priv->subdev, client, &ov6650_subdev_ops);
  842. v4l2_ctrl_handler_init(&priv->hdl, 13);
  843. v4l2_ctrl_new_std(&priv->hdl, &ov6550_ctrl_ops,
  844. V4L2_CID_VFLIP, 0, 1, 1, 0);
  845. v4l2_ctrl_new_std(&priv->hdl, &ov6550_ctrl_ops,
  846. V4L2_CID_HFLIP, 0, 1, 1, 0);
  847. priv->autogain = v4l2_ctrl_new_std(&priv->hdl, &ov6550_ctrl_ops,
  848. V4L2_CID_AUTOGAIN, 0, 1, 1, 1);
  849. priv->gain = v4l2_ctrl_new_std(&priv->hdl, &ov6550_ctrl_ops,
  850. V4L2_CID_GAIN, 0, 0x3f, 1, DEF_GAIN);
  851. priv->autowb = v4l2_ctrl_new_std(&priv->hdl, &ov6550_ctrl_ops,
  852. V4L2_CID_AUTO_WHITE_BALANCE, 0, 1, 1, 1);
  853. priv->blue = v4l2_ctrl_new_std(&priv->hdl, &ov6550_ctrl_ops,
  854. V4L2_CID_BLUE_BALANCE, 0, 0xff, 1, DEF_BLUE);
  855. priv->red = v4l2_ctrl_new_std(&priv->hdl, &ov6550_ctrl_ops,
  856. V4L2_CID_RED_BALANCE, 0, 0xff, 1, DEF_RED);
  857. v4l2_ctrl_new_std(&priv->hdl, &ov6550_ctrl_ops,
  858. V4L2_CID_SATURATION, 0, 0xf, 1, 0x8);
  859. v4l2_ctrl_new_std(&priv->hdl, &ov6550_ctrl_ops,
  860. V4L2_CID_HUE, 0, HUE_MASK, 1, DEF_HUE);
  861. v4l2_ctrl_new_std(&priv->hdl, &ov6550_ctrl_ops,
  862. V4L2_CID_BRIGHTNESS, 0, 0xff, 1, 0x80);
  863. priv->autoexposure = v4l2_ctrl_new_std_menu(&priv->hdl,
  864. &ov6550_ctrl_ops, V4L2_CID_EXPOSURE_AUTO,
  865. V4L2_EXPOSURE_MANUAL, 0, V4L2_EXPOSURE_AUTO);
  866. priv->exposure = v4l2_ctrl_new_std(&priv->hdl, &ov6550_ctrl_ops,
  867. V4L2_CID_EXPOSURE, 0, 0xff, 1, DEF_AECH);
  868. v4l2_ctrl_new_std(&priv->hdl, &ov6550_ctrl_ops,
  869. V4L2_CID_GAMMA, 0, 0xff, 1, 0x12);
  870. priv->subdev.ctrl_handler = &priv->hdl;
  871. if (priv->hdl.error)
  872. return priv->hdl.error;
  873. v4l2_ctrl_auto_cluster(2, &priv->autogain, 0, true);
  874. v4l2_ctrl_auto_cluster(3, &priv->autowb, 0, true);
  875. v4l2_ctrl_auto_cluster(2, &priv->autoexposure,
  876. V4L2_EXPOSURE_MANUAL, true);
  877. priv->rect.left = DEF_HSTRT << 1;
  878. priv->rect.top = DEF_VSTRT << 1;
  879. priv->rect.width = W_CIF;
  880. priv->rect.height = H_CIF;
  881. priv->half_scale = false;
  882. priv->code = MEDIA_BUS_FMT_YUYV8_2X8;
  883. ret = ov6650_video_probe(client);
  884. if (ret)
  885. v4l2_ctrl_handler_free(&priv->hdl);
  886. return ret;
  887. }
  888. static int ov6650_remove(struct i2c_client *client)
  889. {
  890. struct ov6650 *priv = to_ov6650(client);
  891. v4l2_clk_put(priv->clk);
  892. v4l2_device_unregister_subdev(&priv->subdev);
  893. v4l2_ctrl_handler_free(&priv->hdl);
  894. return 0;
  895. }
  896. static const struct i2c_device_id ov6650_id[] = {
  897. { "ov6650", 0 },
  898. { }
  899. };
  900. MODULE_DEVICE_TABLE(i2c, ov6650_id);
  901. static struct i2c_driver ov6650_i2c_driver = {
  902. .driver = {
  903. .name = "ov6650",
  904. },
  905. .probe = ov6650_probe,
  906. .remove = ov6650_remove,
  907. .id_table = ov6650_id,
  908. };
  909. module_i2c_driver(ov6650_i2c_driver);
  910. MODULE_DESCRIPTION("SoC Camera driver for OmniVision OV6650");
  911. MODULE_AUTHOR("Janusz Krzysztofik <jkrzyszt@tis.icnet.pl>");
  912. MODULE_LICENSE("GPL v2");