ov6650.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065
  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 <media/soc_camera.h>
  31. #include <media/soc_mediabus.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 = 0;
  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 = 0;
  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. if (ctrl->val == V4L2_EXPOSURE_AUTO)
  324. ret = ov6650_reg_rmw(client, REG_COMB, COMB_AEC, 0);
  325. else
  326. ret = ov6650_reg_rmw(client, REG_COMB, 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. /* Get chip identification */
  343. static int ov6650_g_chip_ident(struct v4l2_subdev *sd,
  344. struct v4l2_dbg_chip_ident *id)
  345. {
  346. id->ident = V4L2_IDENT_OV6650;
  347. id->revision = 0;
  348. return 0;
  349. }
  350. #ifdef CONFIG_VIDEO_ADV_DEBUG
  351. static int ov6650_get_register(struct v4l2_subdev *sd,
  352. struct v4l2_dbg_register *reg)
  353. {
  354. struct i2c_client *client = v4l2_get_subdevdata(sd);
  355. int ret;
  356. u8 val;
  357. if (reg->reg & ~0xff)
  358. return -EINVAL;
  359. reg->size = 1;
  360. ret = ov6650_reg_read(client, reg->reg, &val);
  361. if (!ret)
  362. reg->val = (__u64)val;
  363. return ret;
  364. }
  365. static int ov6650_set_register(struct v4l2_subdev *sd,
  366. struct v4l2_dbg_register *reg)
  367. {
  368. struct i2c_client *client = v4l2_get_subdevdata(sd);
  369. if (reg->reg & ~0xff || reg->val & ~0xff)
  370. return -EINVAL;
  371. return ov6650_reg_write(client, reg->reg, reg->val);
  372. }
  373. #endif
  374. static int ov6650_g_crop(struct v4l2_subdev *sd, struct v4l2_crop *a)
  375. {
  376. struct i2c_client *client = v4l2_get_subdevdata(sd);
  377. struct ov6650 *priv = to_ov6650(client);
  378. a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  379. a->c = priv->rect;
  380. return 0;
  381. }
  382. static int ov6650_s_crop(struct v4l2_subdev *sd, struct v4l2_crop *a)
  383. {
  384. struct i2c_client *client = v4l2_get_subdevdata(sd);
  385. struct ov6650 *priv = to_ov6650(client);
  386. struct v4l2_rect *rect = &a->c;
  387. int ret;
  388. if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  389. return -EINVAL;
  390. rect->left = ALIGN(rect->left, 2);
  391. rect->width = ALIGN(rect->width, 2);
  392. rect->top = ALIGN(rect->top, 2);
  393. rect->height = ALIGN(rect->height, 2);
  394. soc_camera_limit_side(&rect->left, &rect->width,
  395. DEF_HSTRT << 1, 2, W_CIF);
  396. soc_camera_limit_side(&rect->top, &rect->height,
  397. DEF_VSTRT << 1, 2, H_CIF);
  398. ret = ov6650_reg_write(client, REG_HSTRT, rect->left >> 1);
  399. if (!ret) {
  400. priv->rect.left = rect->left;
  401. ret = ov6650_reg_write(client, REG_HSTOP,
  402. (rect->left + rect->width) >> 1);
  403. }
  404. if (!ret) {
  405. priv->rect.width = rect->width;
  406. ret = ov6650_reg_write(client, REG_VSTRT, rect->top >> 1);
  407. }
  408. if (!ret) {
  409. priv->rect.top = rect->top;
  410. ret = ov6650_reg_write(client, REG_VSTOP,
  411. (rect->top + rect->height) >> 1);
  412. }
  413. if (!ret)
  414. priv->rect.height = rect->height;
  415. return ret;
  416. }
  417. static int ov6650_cropcap(struct v4l2_subdev *sd, struct v4l2_cropcap *a)
  418. {
  419. if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  420. return -EINVAL;
  421. a->bounds.left = DEF_HSTRT << 1;
  422. a->bounds.top = DEF_VSTRT << 1;
  423. a->bounds.width = W_CIF;
  424. a->bounds.height = H_CIF;
  425. a->defrect = a->bounds;
  426. a->pixelaspect.numerator = 1;
  427. a->pixelaspect.denominator = 1;
  428. return 0;
  429. }
  430. static int ov6650_g_fmt(struct v4l2_subdev *sd,
  431. struct v4l2_mbus_framefmt *mf)
  432. {
  433. struct i2c_client *client = v4l2_get_subdevdata(sd);
  434. struct ov6650 *priv = to_ov6650(client);
  435. mf->width = priv->rect.width >> priv->half_scale;
  436. mf->height = priv->rect.height >> priv->half_scale;
  437. mf->code = priv->code;
  438. mf->colorspace = priv->colorspace;
  439. mf->field = V4L2_FIELD_NONE;
  440. return 0;
  441. }
  442. static bool is_unscaled_ok(int width, int height, struct v4l2_rect *rect)
  443. {
  444. return width > rect->width >> 1 || height > rect->height >> 1;
  445. }
  446. static u8 to_clkrc(struct v4l2_fract *timeperframe,
  447. unsigned long pclk_limit, unsigned long pclk_max)
  448. {
  449. unsigned long pclk;
  450. if (timeperframe->numerator && timeperframe->denominator)
  451. pclk = pclk_max * timeperframe->denominator /
  452. (FRAME_RATE_MAX * timeperframe->numerator);
  453. else
  454. pclk = pclk_max;
  455. if (pclk_limit && pclk_limit < pclk)
  456. pclk = pclk_limit;
  457. return (pclk_max - 1) / pclk;
  458. }
  459. /* set the format we will capture in */
  460. static int ov6650_s_fmt(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf)
  461. {
  462. struct i2c_client *client = v4l2_get_subdevdata(sd);
  463. struct soc_camera_device *icd = (struct soc_camera_device *)sd->grp_id;
  464. struct soc_camera_sense *sense = icd->sense;
  465. struct ov6650 *priv = to_ov6650(client);
  466. bool half_scale = !is_unscaled_ok(mf->width, mf->height, &priv->rect);
  467. struct v4l2_crop a = {
  468. .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
  469. .c = {
  470. .left = priv->rect.left + (priv->rect.width >> 1) -
  471. (mf->width >> (1 - half_scale)),
  472. .top = priv->rect.top + (priv->rect.height >> 1) -
  473. (mf->height >> (1 - half_scale)),
  474. .width = mf->width << half_scale,
  475. .height = mf->height << half_scale,
  476. },
  477. };
  478. enum v4l2_mbus_pixelcode code = mf->code;
  479. unsigned long mclk, pclk;
  480. u8 coma_set = 0, coma_mask = 0, coml_set, coml_mask, clkrc;
  481. int ret;
  482. /* select color matrix configuration for given color encoding */
  483. switch (code) {
  484. case V4L2_MBUS_FMT_Y8_1X8:
  485. dev_dbg(&client->dev, "pixel format GREY8_1X8\n");
  486. coma_mask |= COMA_RGB | COMA_WORD_SWAP | COMA_BYTE_SWAP;
  487. coma_set |= COMA_BW;
  488. break;
  489. case V4L2_MBUS_FMT_YUYV8_2X8:
  490. dev_dbg(&client->dev, "pixel format YUYV8_2X8_LE\n");
  491. coma_mask |= COMA_RGB | COMA_BW | COMA_BYTE_SWAP;
  492. coma_set |= COMA_WORD_SWAP;
  493. break;
  494. case V4L2_MBUS_FMT_YVYU8_2X8:
  495. dev_dbg(&client->dev, "pixel format YVYU8_2X8_LE (untested)\n");
  496. coma_mask |= COMA_RGB | COMA_BW | COMA_WORD_SWAP |
  497. COMA_BYTE_SWAP;
  498. break;
  499. case V4L2_MBUS_FMT_UYVY8_2X8:
  500. dev_dbg(&client->dev, "pixel format YUYV8_2X8_BE\n");
  501. if (half_scale) {
  502. coma_mask |= COMA_RGB | COMA_BW | COMA_WORD_SWAP;
  503. coma_set |= COMA_BYTE_SWAP;
  504. } else {
  505. coma_mask |= COMA_RGB | COMA_BW;
  506. coma_set |= COMA_BYTE_SWAP | COMA_WORD_SWAP;
  507. }
  508. break;
  509. case V4L2_MBUS_FMT_VYUY8_2X8:
  510. dev_dbg(&client->dev, "pixel format YVYU8_2X8_BE (untested)\n");
  511. if (half_scale) {
  512. coma_mask |= COMA_RGB | COMA_BW;
  513. coma_set |= COMA_BYTE_SWAP | COMA_WORD_SWAP;
  514. } else {
  515. coma_mask |= COMA_RGB | COMA_BW | COMA_WORD_SWAP;
  516. coma_set |= COMA_BYTE_SWAP;
  517. }
  518. break;
  519. case V4L2_MBUS_FMT_SBGGR8_1X8:
  520. dev_dbg(&client->dev, "pixel format SBGGR8_1X8 (untested)\n");
  521. coma_mask |= COMA_BW | COMA_BYTE_SWAP | COMA_WORD_SWAP;
  522. coma_set |= COMA_RAW_RGB | COMA_RGB;
  523. break;
  524. default:
  525. dev_err(&client->dev, "Pixel format not handled: 0x%x\n", code);
  526. return -EINVAL;
  527. }
  528. priv->code = code;
  529. if (code == V4L2_MBUS_FMT_Y8_1X8 ||
  530. code == V4L2_MBUS_FMT_SBGGR8_1X8) {
  531. coml_mask = COML_ONE_CHANNEL;
  532. coml_set = 0;
  533. priv->pclk_max = 4000000;
  534. } else {
  535. coml_mask = 0;
  536. coml_set = COML_ONE_CHANNEL;
  537. priv->pclk_max = 8000000;
  538. }
  539. if (code == V4L2_MBUS_FMT_SBGGR8_1X8)
  540. priv->colorspace = V4L2_COLORSPACE_SRGB;
  541. else if (code != 0)
  542. priv->colorspace = V4L2_COLORSPACE_JPEG;
  543. if (half_scale) {
  544. dev_dbg(&client->dev, "max resolution: QCIF\n");
  545. coma_set |= COMA_QCIF;
  546. priv->pclk_max /= 2;
  547. } else {
  548. dev_dbg(&client->dev, "max resolution: CIF\n");
  549. coma_mask |= COMA_QCIF;
  550. }
  551. priv->half_scale = half_scale;
  552. if (sense) {
  553. if (sense->master_clock == 8000000) {
  554. dev_dbg(&client->dev, "8MHz input clock\n");
  555. clkrc = CLKRC_6MHz;
  556. } else if (sense->master_clock == 12000000) {
  557. dev_dbg(&client->dev, "12MHz input clock\n");
  558. clkrc = CLKRC_12MHz;
  559. } else if (sense->master_clock == 16000000) {
  560. dev_dbg(&client->dev, "16MHz input clock\n");
  561. clkrc = CLKRC_16MHz;
  562. } else if (sense->master_clock == 24000000) {
  563. dev_dbg(&client->dev, "24MHz input clock\n");
  564. clkrc = CLKRC_24MHz;
  565. } else {
  566. dev_err(&client->dev,
  567. "unspported input clock, check platform data\n");
  568. return -EINVAL;
  569. }
  570. mclk = sense->master_clock;
  571. priv->pclk_limit = sense->pixel_clock_max;
  572. } else {
  573. clkrc = CLKRC_24MHz;
  574. mclk = 24000000;
  575. priv->pclk_limit = 0;
  576. dev_dbg(&client->dev, "using default 24MHz input clock\n");
  577. }
  578. clkrc |= to_clkrc(&priv->tpf, priv->pclk_limit, priv->pclk_max);
  579. pclk = priv->pclk_max / GET_CLKRC_DIV(clkrc);
  580. dev_dbg(&client->dev, "pixel clock divider: %ld.%ld\n",
  581. mclk / pclk, 10 * mclk % pclk / pclk);
  582. ret = ov6650_s_crop(sd, &a);
  583. if (!ret)
  584. ret = ov6650_reg_rmw(client, REG_COMA, coma_set, coma_mask);
  585. if (!ret)
  586. ret = ov6650_reg_write(client, REG_CLKRC, clkrc);
  587. if (!ret)
  588. ret = ov6650_reg_rmw(client, REG_COML, coml_set, coml_mask);
  589. if (!ret) {
  590. mf->colorspace = priv->colorspace;
  591. mf->width = priv->rect.width >> half_scale;
  592. mf->height = priv->rect.height >> half_scale;
  593. }
  594. return ret;
  595. }
  596. static int ov6650_try_fmt(struct v4l2_subdev *sd,
  597. struct v4l2_mbus_framefmt *mf)
  598. {
  599. struct i2c_client *client = v4l2_get_subdevdata(sd);
  600. struct ov6650 *priv = to_ov6650(client);
  601. if (is_unscaled_ok(mf->width, mf->height, &priv->rect))
  602. v4l_bound_align_image(&mf->width, 2, W_CIF, 1,
  603. &mf->height, 2, H_CIF, 1, 0);
  604. mf->field = V4L2_FIELD_NONE;
  605. switch (mf->code) {
  606. case V4L2_MBUS_FMT_Y10_1X10:
  607. mf->code = V4L2_MBUS_FMT_Y8_1X8;
  608. case V4L2_MBUS_FMT_Y8_1X8:
  609. case V4L2_MBUS_FMT_YVYU8_2X8:
  610. case V4L2_MBUS_FMT_YUYV8_2X8:
  611. case V4L2_MBUS_FMT_VYUY8_2X8:
  612. case V4L2_MBUS_FMT_UYVY8_2X8:
  613. mf->colorspace = V4L2_COLORSPACE_JPEG;
  614. break;
  615. default:
  616. mf->code = V4L2_MBUS_FMT_SBGGR8_1X8;
  617. case V4L2_MBUS_FMT_SBGGR8_1X8:
  618. mf->colorspace = V4L2_COLORSPACE_SRGB;
  619. break;
  620. }
  621. return 0;
  622. }
  623. static int ov6650_enum_fmt(struct v4l2_subdev *sd, unsigned int index,
  624. enum v4l2_mbus_pixelcode *code)
  625. {
  626. if (index >= ARRAY_SIZE(ov6650_codes))
  627. return -EINVAL;
  628. *code = ov6650_codes[index];
  629. return 0;
  630. }
  631. static int ov6650_g_parm(struct v4l2_subdev *sd, struct v4l2_streamparm *parms)
  632. {
  633. struct i2c_client *client = v4l2_get_subdevdata(sd);
  634. struct ov6650 *priv = to_ov6650(client);
  635. struct v4l2_captureparm *cp = &parms->parm.capture;
  636. if (parms->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  637. return -EINVAL;
  638. memset(cp, 0, sizeof(*cp));
  639. cp->capability = V4L2_CAP_TIMEPERFRAME;
  640. cp->timeperframe.numerator = GET_CLKRC_DIV(to_clkrc(&priv->tpf,
  641. priv->pclk_limit, priv->pclk_max));
  642. cp->timeperframe.denominator = FRAME_RATE_MAX;
  643. dev_dbg(&client->dev, "Frame interval: %u/%u s\n",
  644. cp->timeperframe.numerator, cp->timeperframe.denominator);
  645. return 0;
  646. }
  647. static int ov6650_s_parm(struct v4l2_subdev *sd, struct v4l2_streamparm *parms)
  648. {
  649. struct i2c_client *client = v4l2_get_subdevdata(sd);
  650. struct ov6650 *priv = to_ov6650(client);
  651. struct v4l2_captureparm *cp = &parms->parm.capture;
  652. struct v4l2_fract *tpf = &cp->timeperframe;
  653. int div, ret;
  654. u8 clkrc;
  655. if (parms->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  656. return -EINVAL;
  657. if (cp->extendedmode != 0)
  658. return -EINVAL;
  659. if (tpf->numerator == 0 || tpf->denominator == 0)
  660. div = 1; /* Reset to full rate */
  661. else
  662. div = (tpf->numerator * FRAME_RATE_MAX) / tpf->denominator;
  663. if (div == 0)
  664. div = 1;
  665. else if (div > GET_CLKRC_DIV(CLKRC_DIV_MASK))
  666. div = GET_CLKRC_DIV(CLKRC_DIV_MASK);
  667. /*
  668. * Keep result to be used as tpf limit
  669. * for subseqent clock divider calculations
  670. */
  671. priv->tpf.numerator = div;
  672. priv->tpf.denominator = FRAME_RATE_MAX;
  673. clkrc = to_clkrc(&priv->tpf, priv->pclk_limit, priv->pclk_max);
  674. ret = ov6650_reg_rmw(client, REG_CLKRC, clkrc, CLKRC_DIV_MASK);
  675. if (!ret) {
  676. tpf->numerator = GET_CLKRC_DIV(clkrc);
  677. tpf->denominator = FRAME_RATE_MAX;
  678. }
  679. return ret;
  680. }
  681. /* Soft reset the camera. This has nothing to do with the RESET pin! */
  682. static int ov6650_reset(struct i2c_client *client)
  683. {
  684. int ret;
  685. dev_dbg(&client->dev, "reset\n");
  686. ret = ov6650_reg_rmw(client, REG_COMA, COMA_RESET, 0);
  687. if (ret)
  688. dev_err(&client->dev,
  689. "An error occurred while entering soft reset!\n");
  690. return ret;
  691. }
  692. /* program default register values */
  693. static int ov6650_prog_dflt(struct i2c_client *client)
  694. {
  695. int ret;
  696. dev_dbg(&client->dev, "initializing\n");
  697. ret = ov6650_reg_write(client, REG_COMA, 0); /* ~COMA_RESET */
  698. if (!ret)
  699. ret = ov6650_reg_rmw(client, REG_COMB, 0, COMB_BAND_FILTER);
  700. return ret;
  701. }
  702. static int ov6650_video_probe(struct i2c_client *client)
  703. {
  704. u8 pidh, pidl, midh, midl;
  705. int ret = 0;
  706. /*
  707. * check and show product ID and manufacturer ID
  708. */
  709. ret = ov6650_reg_read(client, REG_PIDH, &pidh);
  710. if (!ret)
  711. ret = ov6650_reg_read(client, REG_PIDL, &pidl);
  712. if (!ret)
  713. ret = ov6650_reg_read(client, REG_MIDH, &midh);
  714. if (!ret)
  715. ret = ov6650_reg_read(client, REG_MIDL, &midl);
  716. if (ret)
  717. return ret;
  718. if ((pidh != OV6650_PIDH) || (pidl != OV6650_PIDL)) {
  719. dev_err(&client->dev, "Product ID error 0x%02x:0x%02x\n",
  720. pidh, pidl);
  721. return -ENODEV;
  722. }
  723. dev_info(&client->dev,
  724. "ov6650 Product ID 0x%02x:0x%02x Manufacturer ID 0x%02x:0x%02x\n",
  725. pidh, pidl, midh, midl);
  726. ret = ov6650_reset(client);
  727. if (!ret)
  728. ret = ov6650_prog_dflt(client);
  729. return ret;
  730. }
  731. static const struct v4l2_ctrl_ops ov6550_ctrl_ops = {
  732. .g_volatile_ctrl = ov6550_g_volatile_ctrl,
  733. .s_ctrl = ov6550_s_ctrl,
  734. };
  735. static struct v4l2_subdev_core_ops ov6650_core_ops = {
  736. .g_chip_ident = ov6650_g_chip_ident,
  737. #ifdef CONFIG_VIDEO_ADV_DEBUG
  738. .g_register = ov6650_get_register,
  739. .s_register = ov6650_set_register,
  740. #endif
  741. };
  742. /* Request bus settings on camera side */
  743. static int ov6650_g_mbus_config(struct v4l2_subdev *sd,
  744. struct v4l2_mbus_config *cfg)
  745. {
  746. struct i2c_client *client = v4l2_get_subdevdata(sd);
  747. struct soc_camera_link *icl = soc_camera_i2c_to_link(client);
  748. cfg->flags = V4L2_MBUS_MASTER |
  749. V4L2_MBUS_PCLK_SAMPLE_RISING | V4L2_MBUS_PCLK_SAMPLE_FALLING |
  750. V4L2_MBUS_HSYNC_ACTIVE_HIGH | V4L2_MBUS_HSYNC_ACTIVE_LOW |
  751. V4L2_MBUS_VSYNC_ACTIVE_HIGH | V4L2_MBUS_VSYNC_ACTIVE_LOW |
  752. V4L2_MBUS_DATA_ACTIVE_HIGH;
  753. cfg->type = V4L2_MBUS_PARALLEL;
  754. cfg->flags = soc_camera_apply_board_flags(icl, cfg);
  755. return 0;
  756. }
  757. /* Alter bus settings on camera side */
  758. static int ov6650_s_mbus_config(struct v4l2_subdev *sd,
  759. const struct v4l2_mbus_config *cfg)
  760. {
  761. struct i2c_client *client = v4l2_get_subdevdata(sd);
  762. struct soc_camera_link *icl = soc_camera_i2c_to_link(client);
  763. unsigned long flags = soc_camera_apply_board_flags(icl, cfg);
  764. int ret;
  765. if (flags & V4L2_MBUS_PCLK_SAMPLE_RISING)
  766. ret = ov6650_reg_rmw(client, REG_COMJ, COMJ_PCLK_RISING, 0);
  767. else
  768. ret = ov6650_reg_rmw(client, REG_COMJ, 0, COMJ_PCLK_RISING);
  769. if (ret)
  770. return ret;
  771. if (flags & V4L2_MBUS_HSYNC_ACTIVE_LOW)
  772. ret = ov6650_reg_rmw(client, REG_COMF, COMF_HREF_LOW, 0);
  773. else
  774. ret = ov6650_reg_rmw(client, REG_COMF, 0, COMF_HREF_LOW);
  775. if (ret)
  776. return ret;
  777. if (flags & V4L2_MBUS_VSYNC_ACTIVE_HIGH)
  778. ret = ov6650_reg_rmw(client, REG_COMJ, COMJ_VSYNC_HIGH, 0);
  779. else
  780. ret = ov6650_reg_rmw(client, REG_COMJ, 0, COMJ_VSYNC_HIGH);
  781. return ret;
  782. }
  783. static struct v4l2_subdev_video_ops ov6650_video_ops = {
  784. .s_stream = ov6650_s_stream,
  785. .g_mbus_fmt = ov6650_g_fmt,
  786. .s_mbus_fmt = ov6650_s_fmt,
  787. .try_mbus_fmt = ov6650_try_fmt,
  788. .enum_mbus_fmt = ov6650_enum_fmt,
  789. .cropcap = ov6650_cropcap,
  790. .g_crop = ov6650_g_crop,
  791. .s_crop = ov6650_s_crop,
  792. .g_parm = ov6650_g_parm,
  793. .s_parm = ov6650_s_parm,
  794. .g_mbus_config = ov6650_g_mbus_config,
  795. .s_mbus_config = ov6650_s_mbus_config,
  796. };
  797. static struct v4l2_subdev_ops ov6650_subdev_ops = {
  798. .core = &ov6650_core_ops,
  799. .video = &ov6650_video_ops,
  800. };
  801. /*
  802. * i2c_driver function
  803. */
  804. static int ov6650_probe(struct i2c_client *client,
  805. const struct i2c_device_id *did)
  806. {
  807. struct ov6650 *priv;
  808. struct soc_camera_link *icl = soc_camera_i2c_to_link(client);
  809. int ret;
  810. if (!icl) {
  811. dev_err(&client->dev, "Missing platform_data for driver\n");
  812. return -EINVAL;
  813. }
  814. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  815. if (!priv) {
  816. dev_err(&client->dev,
  817. "Failed to allocate memory for private data!\n");
  818. return -ENOMEM;
  819. }
  820. v4l2_i2c_subdev_init(&priv->subdev, client, &ov6650_subdev_ops);
  821. v4l2_ctrl_handler_init(&priv->hdl, 13);
  822. v4l2_ctrl_new_std(&priv->hdl, &ov6550_ctrl_ops,
  823. V4L2_CID_VFLIP, 0, 1, 1, 0);
  824. v4l2_ctrl_new_std(&priv->hdl, &ov6550_ctrl_ops,
  825. V4L2_CID_HFLIP, 0, 1, 1, 0);
  826. priv->autogain = v4l2_ctrl_new_std(&priv->hdl, &ov6550_ctrl_ops,
  827. V4L2_CID_AUTOGAIN, 0, 1, 1, 1);
  828. priv->gain = v4l2_ctrl_new_std(&priv->hdl, &ov6550_ctrl_ops,
  829. V4L2_CID_GAIN, 0, 0x3f, 1, DEF_GAIN);
  830. priv->autowb = v4l2_ctrl_new_std(&priv->hdl, &ov6550_ctrl_ops,
  831. V4L2_CID_AUTO_WHITE_BALANCE, 0, 1, 1, 1);
  832. priv->blue = v4l2_ctrl_new_std(&priv->hdl, &ov6550_ctrl_ops,
  833. V4L2_CID_BLUE_BALANCE, 0, 0xff, 1, DEF_BLUE);
  834. priv->red = v4l2_ctrl_new_std(&priv->hdl, &ov6550_ctrl_ops,
  835. V4L2_CID_RED_BALANCE, 0, 0xff, 1, DEF_RED);
  836. v4l2_ctrl_new_std(&priv->hdl, &ov6550_ctrl_ops,
  837. V4L2_CID_SATURATION, 0, 0xf, 1, 0x8);
  838. v4l2_ctrl_new_std(&priv->hdl, &ov6550_ctrl_ops,
  839. V4L2_CID_HUE, 0, HUE_MASK, 1, DEF_HUE);
  840. v4l2_ctrl_new_std(&priv->hdl, &ov6550_ctrl_ops,
  841. V4L2_CID_BRIGHTNESS, 0, 0xff, 1, 0x80);
  842. priv->autoexposure = v4l2_ctrl_new_std_menu(&priv->hdl,
  843. &ov6550_ctrl_ops, V4L2_CID_EXPOSURE_AUTO, 1, 0,
  844. V4L2_EXPOSURE_AUTO);
  845. priv->exposure = v4l2_ctrl_new_std(&priv->hdl, &ov6550_ctrl_ops,
  846. V4L2_CID_EXPOSURE, 0, 0xff, 1, DEF_AECH);
  847. v4l2_ctrl_new_std(&priv->hdl, &ov6550_ctrl_ops,
  848. V4L2_CID_GAMMA, 0, 0xff, 1, 0x12);
  849. priv->subdev.ctrl_handler = &priv->hdl;
  850. if (priv->hdl.error) {
  851. int err = priv->hdl.error;
  852. kfree(priv);
  853. return err;
  854. }
  855. v4l2_ctrl_auto_cluster(2, &priv->autogain, 0, true);
  856. v4l2_ctrl_auto_cluster(3, &priv->autowb, 0, true);
  857. v4l2_ctrl_auto_cluster(2, &priv->autoexposure,
  858. V4L2_EXPOSURE_MANUAL, true);
  859. priv->rect.left = DEF_HSTRT << 1;
  860. priv->rect.top = DEF_VSTRT << 1;
  861. priv->rect.width = W_CIF;
  862. priv->rect.height = H_CIF;
  863. priv->half_scale = false;
  864. priv->code = V4L2_MBUS_FMT_YUYV8_2X8;
  865. priv->colorspace = V4L2_COLORSPACE_JPEG;
  866. ret = ov6650_video_probe(client);
  867. if (!ret)
  868. ret = v4l2_ctrl_handler_setup(&priv->hdl);
  869. if (ret) {
  870. v4l2_ctrl_handler_free(&priv->hdl);
  871. kfree(priv);
  872. }
  873. return ret;
  874. }
  875. static int ov6650_remove(struct i2c_client *client)
  876. {
  877. struct ov6650 *priv = to_ov6650(client);
  878. v4l2_device_unregister_subdev(&priv->subdev);
  879. v4l2_ctrl_handler_free(&priv->hdl);
  880. kfree(priv);
  881. return 0;
  882. }
  883. static const struct i2c_device_id ov6650_id[] = {
  884. { "ov6650", 0 },
  885. { }
  886. };
  887. MODULE_DEVICE_TABLE(i2c, ov6650_id);
  888. static struct i2c_driver ov6650_i2c_driver = {
  889. .driver = {
  890. .name = "ov6650",
  891. },
  892. .probe = ov6650_probe,
  893. .remove = ov6650_remove,
  894. .id_table = ov6650_id,
  895. };
  896. static int __init ov6650_module_init(void)
  897. {
  898. return i2c_add_driver(&ov6650_i2c_driver);
  899. }
  900. static void __exit ov6650_module_exit(void)
  901. {
  902. i2c_del_driver(&ov6650_i2c_driver);
  903. }
  904. module_init(ov6650_module_init);
  905. module_exit(ov6650_module_exit);
  906. MODULE_DESCRIPTION("SoC Camera driver for OmniVision OV6650");
  907. MODULE_AUTHOR("Janusz Krzysztofik <jkrzyszt@tis.icnet.pl>");
  908. MODULE_LICENSE("GPL v2");