ov6650.c 26 KB

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