ov6650.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063
  1. /*
  2. * V4L2 SoC Camera 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/soc_camera.h>
  33. #include <media/v4l2-chip-ident.h>
  34. #include <media/v4l2-ctrls.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. bool half_scale; /* scale down output by 2 */
  172. struct v4l2_rect rect; /* sensor cropping window */
  173. unsigned long pclk_limit; /* from host */
  174. unsigned long pclk_max; /* from resolution and format */
  175. struct v4l2_fract tpf; /* as requested with s_parm */
  176. enum v4l2_mbus_pixelcode code;
  177. enum v4l2_colorspace colorspace;
  178. };
  179. static enum v4l2_mbus_pixelcode ov6650_codes[] = {
  180. V4L2_MBUS_FMT_YUYV8_2X8,
  181. V4L2_MBUS_FMT_UYVY8_2X8,
  182. V4L2_MBUS_FMT_YVYU8_2X8,
  183. V4L2_MBUS_FMT_VYUY8_2X8,
  184. V4L2_MBUS_FMT_SBGGR8_1X8,
  185. V4L2_MBUS_FMT_Y8_1X8,
  186. };
  187. /* read a register */
  188. static int ov6650_reg_read(struct i2c_client *client, u8 reg, u8 *val)
  189. {
  190. int ret;
  191. u8 data = reg;
  192. struct i2c_msg msg = {
  193. .addr = client->addr,
  194. .flags = 0,
  195. .len = 1,
  196. .buf = &data,
  197. };
  198. ret = i2c_transfer(client->adapter, &msg, 1);
  199. if (ret < 0)
  200. goto err;
  201. msg.flags = I2C_M_RD;
  202. ret = i2c_transfer(client->adapter, &msg, 1);
  203. if (ret < 0)
  204. goto err;
  205. *val = data;
  206. return 0;
  207. err:
  208. dev_err(&client->dev, "Failed reading register 0x%02x!\n", reg);
  209. return ret;
  210. }
  211. /* write a register */
  212. static int ov6650_reg_write(struct i2c_client *client, u8 reg, u8 val)
  213. {
  214. int ret;
  215. unsigned char data[2] = { reg, val };
  216. struct i2c_msg msg = {
  217. .addr = client->addr,
  218. .flags = 0,
  219. .len = 2,
  220. .buf = data,
  221. };
  222. ret = i2c_transfer(client->adapter, &msg, 1);
  223. udelay(100);
  224. if (ret < 0) {
  225. dev_err(&client->dev, "Failed writing register 0x%02x!\n", reg);
  226. return ret;
  227. }
  228. return 0;
  229. }
  230. /* Read a register, alter its bits, write it back */
  231. static int ov6650_reg_rmw(struct i2c_client *client, u8 reg, u8 set, u8 mask)
  232. {
  233. u8 val;
  234. int ret;
  235. ret = ov6650_reg_read(client, reg, &val);
  236. if (ret) {
  237. dev_err(&client->dev,
  238. "[Read]-Modify-Write of register 0x%02x failed!\n",
  239. reg);
  240. return ret;
  241. }
  242. val &= ~mask;
  243. val |= set;
  244. ret = ov6650_reg_write(client, reg, val);
  245. if (ret)
  246. dev_err(&client->dev,
  247. "Read-Modify-[Write] of register 0x%02x failed!\n",
  248. reg);
  249. return ret;
  250. }
  251. static struct ov6650 *to_ov6650(const struct i2c_client *client)
  252. {
  253. return container_of(i2c_get_clientdata(client), struct ov6650, subdev);
  254. }
  255. /* Start/Stop streaming from the device */
  256. static int ov6650_s_stream(struct v4l2_subdev *sd, int enable)
  257. {
  258. return 0;
  259. }
  260. /* Get status of additional camera capabilities */
  261. static int ov6550_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
  262. {
  263. struct ov6650 *priv = container_of(ctrl->handler, struct ov6650, hdl);
  264. struct v4l2_subdev *sd = &priv->subdev;
  265. struct i2c_client *client = v4l2_get_subdevdata(sd);
  266. uint8_t reg, reg2;
  267. int ret;
  268. switch (ctrl->id) {
  269. case V4L2_CID_AUTOGAIN:
  270. ret = ov6650_reg_read(client, REG_GAIN, &reg);
  271. if (!ret)
  272. priv->gain->val = reg;
  273. return ret;
  274. case V4L2_CID_AUTO_WHITE_BALANCE:
  275. ret = ov6650_reg_read(client, REG_BLUE, &reg);
  276. if (!ret)
  277. ret = ov6650_reg_read(client, REG_RED, &reg2);
  278. if (!ret) {
  279. priv->blue->val = reg;
  280. priv->red->val = reg2;
  281. }
  282. return ret;
  283. case V4L2_CID_EXPOSURE_AUTO:
  284. ret = ov6650_reg_read(client, REG_AECH, &reg);
  285. if (!ret)
  286. priv->exposure->val = reg;
  287. return ret;
  288. }
  289. return -EINVAL;
  290. }
  291. /* Set status of additional camera capabilities */
  292. static int ov6550_s_ctrl(struct v4l2_ctrl *ctrl)
  293. {
  294. struct ov6650 *priv = container_of(ctrl->handler, struct ov6650, hdl);
  295. struct v4l2_subdev *sd = &priv->subdev;
  296. struct i2c_client *client = v4l2_get_subdevdata(sd);
  297. int ret;
  298. switch (ctrl->id) {
  299. case V4L2_CID_AUTOGAIN:
  300. ret = ov6650_reg_rmw(client, REG_COMB,
  301. ctrl->val ? COMB_AGC : 0, COMB_AGC);
  302. if (!ret && !ctrl->val)
  303. ret = ov6650_reg_write(client, REG_GAIN, priv->gain->val);
  304. return ret;
  305. case V4L2_CID_AUTO_WHITE_BALANCE:
  306. ret = ov6650_reg_rmw(client, REG_COMB,
  307. ctrl->val ? COMB_AWB : 0, COMB_AWB);
  308. if (!ret && !ctrl->val) {
  309. ret = ov6650_reg_write(client, REG_BLUE, priv->blue->val);
  310. if (!ret)
  311. ret = ov6650_reg_write(client, REG_RED,
  312. priv->red->val);
  313. }
  314. return ret;
  315. case V4L2_CID_SATURATION:
  316. return ov6650_reg_rmw(client, REG_SAT, SET_SAT(ctrl->val),
  317. SAT_MASK);
  318. case V4L2_CID_HUE:
  319. return ov6650_reg_rmw(client, REG_HUE, SET_HUE(ctrl->val),
  320. HUE_MASK);
  321. case V4L2_CID_BRIGHTNESS:
  322. return ov6650_reg_write(client, REG_BRT, ctrl->val);
  323. case V4L2_CID_EXPOSURE_AUTO:
  324. ret = ov6650_reg_rmw(client, REG_COMB, ctrl->val ==
  325. V4L2_EXPOSURE_AUTO ? COMB_AEC : 0, COMB_AEC);
  326. if (!ret && ctrl->val == V4L2_EXPOSURE_MANUAL)
  327. ret = ov6650_reg_write(client, REG_AECH,
  328. priv->exposure->val);
  329. return ret;
  330. case V4L2_CID_GAMMA:
  331. return ov6650_reg_write(client, REG_GAM1, ctrl->val);
  332. case V4L2_CID_VFLIP:
  333. return ov6650_reg_rmw(client, REG_COMB,
  334. ctrl->val ? COMB_FLIP_V : 0, COMB_FLIP_V);
  335. case V4L2_CID_HFLIP:
  336. return ov6650_reg_rmw(client, REG_COMB,
  337. ctrl->val ? COMB_FLIP_H : 0, COMB_FLIP_H);
  338. }
  339. return -EINVAL;
  340. }
  341. /* Get chip identification */
  342. static int ov6650_g_chip_ident(struct v4l2_subdev *sd,
  343. struct v4l2_dbg_chip_ident *id)
  344. {
  345. id->ident = V4L2_IDENT_OV6650;
  346. id->revision = 0;
  347. return 0;
  348. }
  349. #ifdef CONFIG_VIDEO_ADV_DEBUG
  350. static int ov6650_get_register(struct v4l2_subdev *sd,
  351. struct v4l2_dbg_register *reg)
  352. {
  353. struct i2c_client *client = v4l2_get_subdevdata(sd);
  354. int ret;
  355. u8 val;
  356. if (reg->reg & ~0xff)
  357. return -EINVAL;
  358. reg->size = 1;
  359. ret = ov6650_reg_read(client, reg->reg, &val);
  360. if (!ret)
  361. reg->val = (__u64)val;
  362. return ret;
  363. }
  364. static int ov6650_set_register(struct v4l2_subdev *sd,
  365. struct v4l2_dbg_register *reg)
  366. {
  367. struct i2c_client *client = v4l2_get_subdevdata(sd);
  368. if (reg->reg & ~0xff || reg->val & ~0xff)
  369. return -EINVAL;
  370. return ov6650_reg_write(client, reg->reg, reg->val);
  371. }
  372. #endif
  373. static int ov6650_s_power(struct v4l2_subdev *sd, int on)
  374. {
  375. struct i2c_client *client = v4l2_get_subdevdata(sd);
  376. struct soc_camera_subdev_desc *ssdd = soc_camera_i2c_to_desc(client);
  377. return soc_camera_set_power(&client->dev, ssdd, on);
  378. }
  379. static int ov6650_g_crop(struct v4l2_subdev *sd, struct v4l2_crop *a)
  380. {
  381. struct i2c_client *client = v4l2_get_subdevdata(sd);
  382. struct ov6650 *priv = to_ov6650(client);
  383. a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  384. a->c = priv->rect;
  385. return 0;
  386. }
  387. static int ov6650_s_crop(struct v4l2_subdev *sd, const struct v4l2_crop *a)
  388. {
  389. struct i2c_client *client = v4l2_get_subdevdata(sd);
  390. struct ov6650 *priv = to_ov6650(client);
  391. struct v4l2_rect rect = a->c;
  392. int ret;
  393. if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  394. return -EINVAL;
  395. rect.left = ALIGN(rect.left, 2);
  396. rect.width = ALIGN(rect.width, 2);
  397. rect.top = ALIGN(rect.top, 2);
  398. rect.height = ALIGN(rect.height, 2);
  399. soc_camera_limit_side(&rect.left, &rect.width,
  400. DEF_HSTRT << 1, 2, W_CIF);
  401. soc_camera_limit_side(&rect.top, &rect.height,
  402. DEF_VSTRT << 1, 2, H_CIF);
  403. ret = ov6650_reg_write(client, REG_HSTRT, rect.left >> 1);
  404. if (!ret) {
  405. priv->rect.left = rect.left;
  406. ret = ov6650_reg_write(client, REG_HSTOP,
  407. (rect.left + rect.width) >> 1);
  408. }
  409. if (!ret) {
  410. priv->rect.width = rect.width;
  411. ret = ov6650_reg_write(client, REG_VSTRT, rect.top >> 1);
  412. }
  413. if (!ret) {
  414. priv->rect.top = rect.top;
  415. ret = ov6650_reg_write(client, REG_VSTOP,
  416. (rect.top + rect.height) >> 1);
  417. }
  418. if (!ret)
  419. priv->rect.height = rect.height;
  420. return ret;
  421. }
  422. static int ov6650_cropcap(struct v4l2_subdev *sd, struct v4l2_cropcap *a)
  423. {
  424. if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  425. return -EINVAL;
  426. a->bounds.left = DEF_HSTRT << 1;
  427. a->bounds.top = DEF_VSTRT << 1;
  428. a->bounds.width = W_CIF;
  429. a->bounds.height = H_CIF;
  430. a->defrect = a->bounds;
  431. a->pixelaspect.numerator = 1;
  432. a->pixelaspect.denominator = 1;
  433. return 0;
  434. }
  435. static int ov6650_g_fmt(struct v4l2_subdev *sd,
  436. struct v4l2_mbus_framefmt *mf)
  437. {
  438. struct i2c_client *client = v4l2_get_subdevdata(sd);
  439. struct ov6650 *priv = to_ov6650(client);
  440. mf->width = priv->rect.width >> priv->half_scale;
  441. mf->height = priv->rect.height >> priv->half_scale;
  442. mf->code = priv->code;
  443. mf->colorspace = priv->colorspace;
  444. mf->field = V4L2_FIELD_NONE;
  445. return 0;
  446. }
  447. static bool is_unscaled_ok(int width, int height, struct v4l2_rect *rect)
  448. {
  449. return width > rect->width >> 1 || height > rect->height >> 1;
  450. }
  451. static u8 to_clkrc(struct v4l2_fract *timeperframe,
  452. unsigned long pclk_limit, unsigned long pclk_max)
  453. {
  454. unsigned long pclk;
  455. if (timeperframe->numerator && timeperframe->denominator)
  456. pclk = pclk_max * timeperframe->denominator /
  457. (FRAME_RATE_MAX * timeperframe->numerator);
  458. else
  459. pclk = pclk_max;
  460. if (pclk_limit && pclk_limit < pclk)
  461. pclk = pclk_limit;
  462. return (pclk_max - 1) / pclk;
  463. }
  464. /* set the format we will capture in */
  465. static int ov6650_s_fmt(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf)
  466. {
  467. struct i2c_client *client = v4l2_get_subdevdata(sd);
  468. struct soc_camera_device *icd = v4l2_get_subdev_hostdata(sd);
  469. struct soc_camera_sense *sense = icd->sense;
  470. struct ov6650 *priv = to_ov6650(client);
  471. bool half_scale = !is_unscaled_ok(mf->width, mf->height, &priv->rect);
  472. struct v4l2_crop a = {
  473. .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
  474. .c = {
  475. .left = priv->rect.left + (priv->rect.width >> 1) -
  476. (mf->width >> (1 - half_scale)),
  477. .top = priv->rect.top + (priv->rect.height >> 1) -
  478. (mf->height >> (1 - half_scale)),
  479. .width = mf->width << half_scale,
  480. .height = mf->height << half_scale,
  481. },
  482. };
  483. enum v4l2_mbus_pixelcode code = mf->code;
  484. unsigned long mclk, pclk;
  485. u8 coma_set = 0, coma_mask = 0, coml_set, coml_mask, clkrc;
  486. int ret;
  487. /* select color matrix configuration for given color encoding */
  488. switch (code) {
  489. case V4L2_MBUS_FMT_Y8_1X8:
  490. dev_dbg(&client->dev, "pixel format GREY8_1X8\n");
  491. coma_mask |= COMA_RGB | COMA_WORD_SWAP | COMA_BYTE_SWAP;
  492. coma_set |= COMA_BW;
  493. break;
  494. case V4L2_MBUS_FMT_YUYV8_2X8:
  495. dev_dbg(&client->dev, "pixel format YUYV8_2X8_LE\n");
  496. coma_mask |= COMA_RGB | COMA_BW | COMA_BYTE_SWAP;
  497. coma_set |= COMA_WORD_SWAP;
  498. break;
  499. case V4L2_MBUS_FMT_YVYU8_2X8:
  500. dev_dbg(&client->dev, "pixel format YVYU8_2X8_LE (untested)\n");
  501. coma_mask |= COMA_RGB | COMA_BW | COMA_WORD_SWAP |
  502. COMA_BYTE_SWAP;
  503. break;
  504. case V4L2_MBUS_FMT_UYVY8_2X8:
  505. dev_dbg(&client->dev, "pixel format YUYV8_2X8_BE\n");
  506. if (half_scale) {
  507. coma_mask |= COMA_RGB | COMA_BW | COMA_WORD_SWAP;
  508. coma_set |= COMA_BYTE_SWAP;
  509. } else {
  510. coma_mask |= COMA_RGB | COMA_BW;
  511. coma_set |= COMA_BYTE_SWAP | COMA_WORD_SWAP;
  512. }
  513. break;
  514. case V4L2_MBUS_FMT_VYUY8_2X8:
  515. dev_dbg(&client->dev, "pixel format YVYU8_2X8_BE (untested)\n");
  516. if (half_scale) {
  517. coma_mask |= COMA_RGB | COMA_BW;
  518. coma_set |= COMA_BYTE_SWAP | COMA_WORD_SWAP;
  519. } else {
  520. coma_mask |= COMA_RGB | COMA_BW | COMA_WORD_SWAP;
  521. coma_set |= COMA_BYTE_SWAP;
  522. }
  523. break;
  524. case V4L2_MBUS_FMT_SBGGR8_1X8:
  525. dev_dbg(&client->dev, "pixel format SBGGR8_1X8 (untested)\n");
  526. coma_mask |= COMA_BW | COMA_BYTE_SWAP | COMA_WORD_SWAP;
  527. coma_set |= COMA_RAW_RGB | COMA_RGB;
  528. break;
  529. default:
  530. dev_err(&client->dev, "Pixel format not handled: 0x%x\n", code);
  531. return -EINVAL;
  532. }
  533. priv->code = code;
  534. if (code == V4L2_MBUS_FMT_Y8_1X8 ||
  535. code == V4L2_MBUS_FMT_SBGGR8_1X8) {
  536. coml_mask = COML_ONE_CHANNEL;
  537. coml_set = 0;
  538. priv->pclk_max = 4000000;
  539. } else {
  540. coml_mask = 0;
  541. coml_set = COML_ONE_CHANNEL;
  542. priv->pclk_max = 8000000;
  543. }
  544. if (code == V4L2_MBUS_FMT_SBGGR8_1X8)
  545. priv->colorspace = V4L2_COLORSPACE_SRGB;
  546. else if (code != 0)
  547. priv->colorspace = V4L2_COLORSPACE_JPEG;
  548. if (half_scale) {
  549. dev_dbg(&client->dev, "max resolution: QCIF\n");
  550. coma_set |= COMA_QCIF;
  551. priv->pclk_max /= 2;
  552. } else {
  553. dev_dbg(&client->dev, "max resolution: CIF\n");
  554. coma_mask |= COMA_QCIF;
  555. }
  556. priv->half_scale = half_scale;
  557. if (sense) {
  558. if (sense->master_clock == 8000000) {
  559. dev_dbg(&client->dev, "8MHz input clock\n");
  560. clkrc = CLKRC_6MHz;
  561. } else if (sense->master_clock == 12000000) {
  562. dev_dbg(&client->dev, "12MHz input clock\n");
  563. clkrc = CLKRC_12MHz;
  564. } else if (sense->master_clock == 16000000) {
  565. dev_dbg(&client->dev, "16MHz input clock\n");
  566. clkrc = CLKRC_16MHz;
  567. } else if (sense->master_clock == 24000000) {
  568. dev_dbg(&client->dev, "24MHz input clock\n");
  569. clkrc = CLKRC_24MHz;
  570. } else {
  571. dev_err(&client->dev,
  572. "unsupported input clock, check platform data\n");
  573. return -EINVAL;
  574. }
  575. mclk = sense->master_clock;
  576. priv->pclk_limit = sense->pixel_clock_max;
  577. } else {
  578. clkrc = CLKRC_24MHz;
  579. mclk = 24000000;
  580. priv->pclk_limit = 0;
  581. dev_dbg(&client->dev, "using default 24MHz input clock\n");
  582. }
  583. clkrc |= to_clkrc(&priv->tpf, priv->pclk_limit, priv->pclk_max);
  584. pclk = priv->pclk_max / GET_CLKRC_DIV(clkrc);
  585. dev_dbg(&client->dev, "pixel clock divider: %ld.%ld\n",
  586. mclk / pclk, 10 * mclk % pclk / pclk);
  587. ret = ov6650_s_crop(sd, &a);
  588. if (!ret)
  589. ret = ov6650_reg_rmw(client, REG_COMA, coma_set, coma_mask);
  590. if (!ret)
  591. ret = ov6650_reg_write(client, REG_CLKRC, clkrc);
  592. if (!ret)
  593. ret = ov6650_reg_rmw(client, REG_COML, coml_set, coml_mask);
  594. if (!ret) {
  595. mf->colorspace = priv->colorspace;
  596. mf->width = priv->rect.width >> half_scale;
  597. mf->height = priv->rect.height >> half_scale;
  598. }
  599. return ret;
  600. }
  601. static int ov6650_try_fmt(struct v4l2_subdev *sd,
  602. struct v4l2_mbus_framefmt *mf)
  603. {
  604. struct i2c_client *client = v4l2_get_subdevdata(sd);
  605. struct ov6650 *priv = to_ov6650(client);
  606. if (is_unscaled_ok(mf->width, mf->height, &priv->rect))
  607. v4l_bound_align_image(&mf->width, 2, W_CIF, 1,
  608. &mf->height, 2, H_CIF, 1, 0);
  609. mf->field = V4L2_FIELD_NONE;
  610. switch (mf->code) {
  611. case V4L2_MBUS_FMT_Y10_1X10:
  612. mf->code = V4L2_MBUS_FMT_Y8_1X8;
  613. case V4L2_MBUS_FMT_Y8_1X8:
  614. case V4L2_MBUS_FMT_YVYU8_2X8:
  615. case V4L2_MBUS_FMT_YUYV8_2X8:
  616. case V4L2_MBUS_FMT_VYUY8_2X8:
  617. case V4L2_MBUS_FMT_UYVY8_2X8:
  618. mf->colorspace = V4L2_COLORSPACE_JPEG;
  619. break;
  620. default:
  621. mf->code = V4L2_MBUS_FMT_SBGGR8_1X8;
  622. case V4L2_MBUS_FMT_SBGGR8_1X8:
  623. mf->colorspace = V4L2_COLORSPACE_SRGB;
  624. break;
  625. }
  626. return 0;
  627. }
  628. static int ov6650_enum_fmt(struct v4l2_subdev *sd, unsigned int index,
  629. enum v4l2_mbus_pixelcode *code)
  630. {
  631. if (index >= ARRAY_SIZE(ov6650_codes))
  632. return -EINVAL;
  633. *code = ov6650_codes[index];
  634. return 0;
  635. }
  636. static int ov6650_g_parm(struct v4l2_subdev *sd, struct v4l2_streamparm *parms)
  637. {
  638. struct i2c_client *client = v4l2_get_subdevdata(sd);
  639. struct ov6650 *priv = to_ov6650(client);
  640. struct v4l2_captureparm *cp = &parms->parm.capture;
  641. if (parms->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  642. return -EINVAL;
  643. memset(cp, 0, sizeof(*cp));
  644. cp->capability = V4L2_CAP_TIMEPERFRAME;
  645. cp->timeperframe.numerator = GET_CLKRC_DIV(to_clkrc(&priv->tpf,
  646. priv->pclk_limit, priv->pclk_max));
  647. cp->timeperframe.denominator = FRAME_RATE_MAX;
  648. dev_dbg(&client->dev, "Frame interval: %u/%u s\n",
  649. cp->timeperframe.numerator, cp->timeperframe.denominator);
  650. return 0;
  651. }
  652. static int ov6650_s_parm(struct v4l2_subdev *sd, struct v4l2_streamparm *parms)
  653. {
  654. struct i2c_client *client = v4l2_get_subdevdata(sd);
  655. struct ov6650 *priv = to_ov6650(client);
  656. struct v4l2_captureparm *cp = &parms->parm.capture;
  657. struct v4l2_fract *tpf = &cp->timeperframe;
  658. int div, ret;
  659. u8 clkrc;
  660. if (parms->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  661. return -EINVAL;
  662. if (cp->extendedmode != 0)
  663. return -EINVAL;
  664. if (tpf->numerator == 0 || tpf->denominator == 0)
  665. div = 1; /* Reset to full rate */
  666. else
  667. div = (tpf->numerator * FRAME_RATE_MAX) / tpf->denominator;
  668. if (div == 0)
  669. div = 1;
  670. else if (div > GET_CLKRC_DIV(CLKRC_DIV_MASK))
  671. div = GET_CLKRC_DIV(CLKRC_DIV_MASK);
  672. /*
  673. * Keep result to be used as tpf limit
  674. * for subseqent clock divider calculations
  675. */
  676. priv->tpf.numerator = div;
  677. priv->tpf.denominator = FRAME_RATE_MAX;
  678. clkrc = to_clkrc(&priv->tpf, priv->pclk_limit, priv->pclk_max);
  679. ret = ov6650_reg_rmw(client, REG_CLKRC, clkrc, CLKRC_DIV_MASK);
  680. if (!ret) {
  681. tpf->numerator = GET_CLKRC_DIV(clkrc);
  682. tpf->denominator = FRAME_RATE_MAX;
  683. }
  684. return ret;
  685. }
  686. /* Soft reset the camera. This has nothing to do with the RESET pin! */
  687. static int ov6650_reset(struct i2c_client *client)
  688. {
  689. int ret;
  690. dev_dbg(&client->dev, "reset\n");
  691. ret = ov6650_reg_rmw(client, REG_COMA, COMA_RESET, 0);
  692. if (ret)
  693. dev_err(&client->dev,
  694. "An error occurred while entering soft reset!\n");
  695. return ret;
  696. }
  697. /* program default register values */
  698. static int ov6650_prog_dflt(struct i2c_client *client)
  699. {
  700. int ret;
  701. dev_dbg(&client->dev, "initializing\n");
  702. ret = ov6650_reg_write(client, REG_COMA, 0); /* ~COMA_RESET */
  703. if (!ret)
  704. ret = ov6650_reg_rmw(client, REG_COMB, 0, COMB_BAND_FILTER);
  705. return ret;
  706. }
  707. static int ov6650_video_probe(struct i2c_client *client)
  708. {
  709. struct ov6650 *priv = to_ov6650(client);
  710. u8 pidh, pidl, midh, midl;
  711. int ret;
  712. ret = ov6650_s_power(&priv->subdev, 1);
  713. if (ret < 0)
  714. return ret;
  715. /*
  716. * check and show product ID and manufacturer ID
  717. */
  718. ret = ov6650_reg_read(client, REG_PIDH, &pidh);
  719. if (!ret)
  720. ret = ov6650_reg_read(client, REG_PIDL, &pidl);
  721. if (!ret)
  722. ret = ov6650_reg_read(client, REG_MIDH, &midh);
  723. if (!ret)
  724. ret = ov6650_reg_read(client, REG_MIDL, &midl);
  725. if (ret)
  726. goto done;
  727. if ((pidh != OV6650_PIDH) || (pidl != OV6650_PIDL)) {
  728. dev_err(&client->dev, "Product ID error 0x%02x:0x%02x\n",
  729. pidh, pidl);
  730. ret = -ENODEV;
  731. goto done;
  732. }
  733. dev_info(&client->dev,
  734. "ov6650 Product ID 0x%02x:0x%02x Manufacturer ID 0x%02x:0x%02x\n",
  735. pidh, pidl, midh, midl);
  736. ret = ov6650_reset(client);
  737. if (!ret)
  738. ret = ov6650_prog_dflt(client);
  739. if (!ret)
  740. ret = v4l2_ctrl_handler_setup(&priv->hdl);
  741. done:
  742. ov6650_s_power(&priv->subdev, 0);
  743. return ret;
  744. }
  745. static const struct v4l2_ctrl_ops ov6550_ctrl_ops = {
  746. .g_volatile_ctrl = ov6550_g_volatile_ctrl,
  747. .s_ctrl = ov6550_s_ctrl,
  748. };
  749. static struct v4l2_subdev_core_ops ov6650_core_ops = {
  750. .g_chip_ident = ov6650_g_chip_ident,
  751. #ifdef CONFIG_VIDEO_ADV_DEBUG
  752. .g_register = ov6650_get_register,
  753. .s_register = ov6650_set_register,
  754. #endif
  755. .s_power = ov6650_s_power,
  756. };
  757. /* Request bus settings on camera side */
  758. static int ov6650_g_mbus_config(struct v4l2_subdev *sd,
  759. struct v4l2_mbus_config *cfg)
  760. {
  761. struct i2c_client *client = v4l2_get_subdevdata(sd);
  762. struct soc_camera_subdev_desc *ssdd = soc_camera_i2c_to_desc(client);
  763. cfg->flags = V4L2_MBUS_MASTER |
  764. V4L2_MBUS_PCLK_SAMPLE_RISING | V4L2_MBUS_PCLK_SAMPLE_FALLING |
  765. V4L2_MBUS_HSYNC_ACTIVE_HIGH | V4L2_MBUS_HSYNC_ACTIVE_LOW |
  766. V4L2_MBUS_VSYNC_ACTIVE_HIGH | V4L2_MBUS_VSYNC_ACTIVE_LOW |
  767. V4L2_MBUS_DATA_ACTIVE_HIGH;
  768. cfg->type = V4L2_MBUS_PARALLEL;
  769. cfg->flags = soc_camera_apply_board_flags(ssdd, cfg);
  770. return 0;
  771. }
  772. /* Alter bus settings on camera side */
  773. static int ov6650_s_mbus_config(struct v4l2_subdev *sd,
  774. const struct v4l2_mbus_config *cfg)
  775. {
  776. struct i2c_client *client = v4l2_get_subdevdata(sd);
  777. struct soc_camera_subdev_desc *ssdd = soc_camera_i2c_to_desc(client);
  778. unsigned long flags = soc_camera_apply_board_flags(ssdd, cfg);
  779. int ret;
  780. if (flags & V4L2_MBUS_PCLK_SAMPLE_RISING)
  781. ret = ov6650_reg_rmw(client, REG_COMJ, COMJ_PCLK_RISING, 0);
  782. else
  783. ret = ov6650_reg_rmw(client, REG_COMJ, 0, COMJ_PCLK_RISING);
  784. if (ret)
  785. return ret;
  786. if (flags & V4L2_MBUS_HSYNC_ACTIVE_LOW)
  787. ret = ov6650_reg_rmw(client, REG_COMF, COMF_HREF_LOW, 0);
  788. else
  789. ret = ov6650_reg_rmw(client, REG_COMF, 0, COMF_HREF_LOW);
  790. if (ret)
  791. return ret;
  792. if (flags & V4L2_MBUS_VSYNC_ACTIVE_HIGH)
  793. ret = ov6650_reg_rmw(client, REG_COMJ, COMJ_VSYNC_HIGH, 0);
  794. else
  795. ret = ov6650_reg_rmw(client, REG_COMJ, 0, COMJ_VSYNC_HIGH);
  796. return ret;
  797. }
  798. static struct v4l2_subdev_video_ops ov6650_video_ops = {
  799. .s_stream = ov6650_s_stream,
  800. .g_mbus_fmt = ov6650_g_fmt,
  801. .s_mbus_fmt = ov6650_s_fmt,
  802. .try_mbus_fmt = ov6650_try_fmt,
  803. .enum_mbus_fmt = ov6650_enum_fmt,
  804. .cropcap = ov6650_cropcap,
  805. .g_crop = ov6650_g_crop,
  806. .s_crop = ov6650_s_crop,
  807. .g_parm = ov6650_g_parm,
  808. .s_parm = ov6650_s_parm,
  809. .g_mbus_config = ov6650_g_mbus_config,
  810. .s_mbus_config = ov6650_s_mbus_config,
  811. };
  812. static struct v4l2_subdev_ops ov6650_subdev_ops = {
  813. .core = &ov6650_core_ops,
  814. .video = &ov6650_video_ops,
  815. };
  816. /*
  817. * i2c_driver function
  818. */
  819. static int ov6650_probe(struct i2c_client *client,
  820. const struct i2c_device_id *did)
  821. {
  822. struct ov6650 *priv;
  823. struct soc_camera_subdev_desc *ssdd = soc_camera_i2c_to_desc(client);
  824. int ret;
  825. if (!ssdd) {
  826. dev_err(&client->dev, "Missing platform_data for driver\n");
  827. return -EINVAL;
  828. }
  829. priv = devm_kzalloc(&client->dev, sizeof(*priv), GFP_KERNEL);
  830. if (!priv) {
  831. dev_err(&client->dev,
  832. "Failed to allocate memory for private data!\n");
  833. return -ENOMEM;
  834. }
  835. v4l2_i2c_subdev_init(&priv->subdev, client, &ov6650_subdev_ops);
  836. v4l2_ctrl_handler_init(&priv->hdl, 13);
  837. v4l2_ctrl_new_std(&priv->hdl, &ov6550_ctrl_ops,
  838. V4L2_CID_VFLIP, 0, 1, 1, 0);
  839. v4l2_ctrl_new_std(&priv->hdl, &ov6550_ctrl_ops,
  840. V4L2_CID_HFLIP, 0, 1, 1, 0);
  841. priv->autogain = v4l2_ctrl_new_std(&priv->hdl, &ov6550_ctrl_ops,
  842. V4L2_CID_AUTOGAIN, 0, 1, 1, 1);
  843. priv->gain = v4l2_ctrl_new_std(&priv->hdl, &ov6550_ctrl_ops,
  844. V4L2_CID_GAIN, 0, 0x3f, 1, DEF_GAIN);
  845. priv->autowb = v4l2_ctrl_new_std(&priv->hdl, &ov6550_ctrl_ops,
  846. V4L2_CID_AUTO_WHITE_BALANCE, 0, 1, 1, 1);
  847. priv->blue = v4l2_ctrl_new_std(&priv->hdl, &ov6550_ctrl_ops,
  848. V4L2_CID_BLUE_BALANCE, 0, 0xff, 1, DEF_BLUE);
  849. priv->red = v4l2_ctrl_new_std(&priv->hdl, &ov6550_ctrl_ops,
  850. V4L2_CID_RED_BALANCE, 0, 0xff, 1, DEF_RED);
  851. v4l2_ctrl_new_std(&priv->hdl, &ov6550_ctrl_ops,
  852. V4L2_CID_SATURATION, 0, 0xf, 1, 0x8);
  853. v4l2_ctrl_new_std(&priv->hdl, &ov6550_ctrl_ops,
  854. V4L2_CID_HUE, 0, HUE_MASK, 1, DEF_HUE);
  855. v4l2_ctrl_new_std(&priv->hdl, &ov6550_ctrl_ops,
  856. V4L2_CID_BRIGHTNESS, 0, 0xff, 1, 0x80);
  857. priv->autoexposure = v4l2_ctrl_new_std_menu(&priv->hdl,
  858. &ov6550_ctrl_ops, V4L2_CID_EXPOSURE_AUTO,
  859. V4L2_EXPOSURE_MANUAL, 0, V4L2_EXPOSURE_AUTO);
  860. priv->exposure = v4l2_ctrl_new_std(&priv->hdl, &ov6550_ctrl_ops,
  861. V4L2_CID_EXPOSURE, 0, 0xff, 1, DEF_AECH);
  862. v4l2_ctrl_new_std(&priv->hdl, &ov6550_ctrl_ops,
  863. V4L2_CID_GAMMA, 0, 0xff, 1, 0x12);
  864. priv->subdev.ctrl_handler = &priv->hdl;
  865. if (priv->hdl.error)
  866. return priv->hdl.error;
  867. v4l2_ctrl_auto_cluster(2, &priv->autogain, 0, true);
  868. v4l2_ctrl_auto_cluster(3, &priv->autowb, 0, true);
  869. v4l2_ctrl_auto_cluster(2, &priv->autoexposure,
  870. V4L2_EXPOSURE_MANUAL, true);
  871. priv->rect.left = DEF_HSTRT << 1;
  872. priv->rect.top = DEF_VSTRT << 1;
  873. priv->rect.width = W_CIF;
  874. priv->rect.height = H_CIF;
  875. priv->half_scale = false;
  876. priv->code = V4L2_MBUS_FMT_YUYV8_2X8;
  877. priv->colorspace = V4L2_COLORSPACE_JPEG;
  878. ret = ov6650_video_probe(client);
  879. if (ret)
  880. v4l2_ctrl_handler_free(&priv->hdl);
  881. return ret;
  882. }
  883. static int ov6650_remove(struct i2c_client *client)
  884. {
  885. struct ov6650 *priv = to_ov6650(client);
  886. v4l2_device_unregister_subdev(&priv->subdev);
  887. v4l2_ctrl_handler_free(&priv->hdl);
  888. return 0;
  889. }
  890. static const struct i2c_device_id ov6650_id[] = {
  891. { "ov6650", 0 },
  892. { }
  893. };
  894. MODULE_DEVICE_TABLE(i2c, ov6650_id);
  895. static struct i2c_driver ov6650_i2c_driver = {
  896. .driver = {
  897. .name = "ov6650",
  898. },
  899. .probe = ov6650_probe,
  900. .remove = ov6650_remove,
  901. .id_table = ov6650_id,
  902. };
  903. module_i2c_driver(ov6650_i2c_driver);
  904. MODULE_DESCRIPTION("SoC Camera driver for OmniVision OV6650");
  905. MODULE_AUTHOR("Janusz Krzysztofik <jkrzyszt@tis.icnet.pl>");
  906. MODULE_LICENSE("GPL v2");