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