ov6650.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086
  1. /*
  2. * V4L2 SoC Camera driver for OmniVision OV6650 Camera Sensor
  3. *
  4. * Copyright (C) 2010 Janusz Krzysztofik <[email protected]>
  5. *
  6. * Based on OmniVision OV96xx Camera Driver
  7. * Copyright (C) 2009 Marek Vasut <[email protected]>
  8. *
  9. * Based on ov772x camera driver:
  10. * Copyright (C) 2008 Renesas Solutions Corp.
  11. * Kuninori Morimoto <[email protected]>
  12. *
  13. * Based on ov7670 and soc_camera_platform driver,
  14. * Copyright 2006-7 Jonathan Corbet <[email protected]>
  15. * Copyright (C) 2008 Magnus Damm
  16. * Copyright (C) 2008, Guennadi Liakhovetski <[email protected]>
  17. *
  18. * Hardware specific bits initialy based on former work by Matt Callow
  19. * drivers/media/video/omap/sensor_ov6650.c
  20. * Copyright (C) 2006 Matt Callow
  21. *
  22. * This program is free software; you can redistribute it and/or modify
  23. * it under the terms of the GNU General Public License version 2 as
  24. * published by the Free Software Foundation.
  25. */
  26. #include <linux/bitops.h>
  27. #include <linux/delay.h>
  28. #include <linux/i2c.h>
  29. #include <linux/slab.h>
  30. #include <linux/v4l2-mediabus.h>
  31. #include <linux/module.h>
  32. #include <media/soc_camera.h>
  33. #include <media/v4l2-clk.h>
  34. #include <media/v4l2-ctrls.h>
  35. /* Register definitions */
  36. #define REG_GAIN 0x00 /* range 00 - 3F */
  37. #define REG_BLUE 0x01
  38. #define REG_RED 0x02
  39. #define REG_SAT 0x03 /* [7:4] saturation [0:3] reserved */
  40. #define REG_HUE 0x04 /* [7:6] rsrvd [5] hue en [4:0] hue */
  41. #define REG_BRT 0x06
  42. #define REG_PIDH 0x0a
  43. #define REG_PIDL 0x0b
  44. #define REG_AECH 0x10
  45. #define REG_CLKRC 0x11 /* Data Format and Internal Clock */
  46. /* [7:6] Input system clock (MHz)*/
  47. /* 00=8, 01=12, 10=16, 11=24 */
  48. /* [5:0]: Internal Clock Pre-Scaler */
  49. #define REG_COMA 0x12 /* [7] Reset */
  50. #define REG_COMB 0x13
  51. #define REG_COMC 0x14
  52. #define REG_COMD 0x15
  53. #define REG_COML 0x16
  54. #define REG_HSTRT 0x17
  55. #define REG_HSTOP 0x18
  56. #define REG_VSTRT 0x19
  57. #define REG_VSTOP 0x1a
  58. #define REG_PSHFT 0x1b
  59. #define REG_MIDH 0x1c
  60. #define REG_MIDL 0x1d
  61. #define REG_HSYNS 0x1e
  62. #define REG_HSYNE 0x1f
  63. #define REG_COME 0x20
  64. #define REG_YOFF 0x21
  65. #define REG_UOFF 0x22
  66. #define REG_VOFF 0x23
  67. #define REG_AEW 0x24
  68. #define REG_AEB 0x25
  69. #define REG_COMF 0x26
  70. #define REG_COMG 0x27
  71. #define REG_COMH 0x28
  72. #define REG_COMI 0x29
  73. #define REG_FRARL 0x2b
  74. #define REG_COMJ 0x2c
  75. #define REG_COMK 0x2d
  76. #define REG_AVGY 0x2e
  77. #define REG_REF0 0x2f
  78. #define REG_REF1 0x30
  79. #define REG_REF2 0x31
  80. #define REG_FRAJH 0x32
  81. #define REG_FRAJL 0x33
  82. #define REG_FACT 0x34
  83. #define REG_L1AEC 0x35
  84. #define REG_AVGU 0x36
  85. #define REG_AVGV 0x37
  86. #define REG_SPCB 0x60
  87. #define REG_SPCC 0x61
  88. #define REG_GAM1 0x62
  89. #define REG_GAM2 0x63
  90. #define REG_GAM3 0x64
  91. #define REG_SPCD 0x65
  92. #define REG_SPCE 0x68
  93. #define REG_ADCL 0x69
  94. #define REG_RMCO 0x6c
  95. #define REG_GMCO 0x6d
  96. #define REG_BMCO 0x6e
  97. /* Register bits, values, etc. */
  98. #define OV6650_PIDH 0x66 /* high byte of product ID number */
  99. #define OV6650_PIDL 0x50 /* low byte of product ID number */
  100. #define OV6650_MIDH 0x7F /* high byte of mfg ID */
  101. #define OV6650_MIDL 0xA2 /* low byte of mfg ID */
  102. #define DEF_GAIN 0x00
  103. #define DEF_BLUE 0x80
  104. #define DEF_RED 0x80
  105. #define SAT_SHIFT 4
  106. #define SAT_MASK (0xf << SAT_SHIFT)
  107. #define SET_SAT(x) (((x) << SAT_SHIFT) & SAT_MASK)
  108. #define HUE_EN BIT(5)
  109. #define HUE_MASK 0x1f
  110. #define DEF_HUE 0x10
  111. #define SET_HUE(x) (HUE_EN | ((x) & HUE_MASK))
  112. #define DEF_AECH 0x4D
  113. #define CLKRC_6MHz 0x00
  114. #define CLKRC_12MHz 0x40
  115. #define CLKRC_16MHz 0x80
  116. #define CLKRC_24MHz 0xc0
  117. #define CLKRC_DIV_MASK 0x3f
  118. #define GET_CLKRC_DIV(x) (((x) & CLKRC_DIV_MASK) + 1)
  119. #define COMA_RESET BIT(7)
  120. #define COMA_QCIF BIT(5)
  121. #define COMA_RAW_RGB BIT(4)
  122. #define COMA_RGB BIT(3)
  123. #define COMA_BW BIT(2)
  124. #define COMA_WORD_SWAP BIT(1)
  125. #define COMA_BYTE_SWAP BIT(0)
  126. #define DEF_COMA 0x00
  127. #define COMB_FLIP_V BIT(7)
  128. #define COMB_FLIP_H BIT(5)
  129. #define COMB_BAND_FILTER BIT(4)
  130. #define COMB_AWB BIT(2)
  131. #define COMB_AGC BIT(1)
  132. #define COMB_AEC BIT(0)
  133. #define DEF_COMB 0x5f
  134. #define COML_ONE_CHANNEL BIT(7)
  135. #define DEF_HSTRT 0x24
  136. #define DEF_HSTOP 0xd4
  137. #define DEF_VSTRT 0x04
  138. #define DEF_VSTOP 0x94
  139. #define COMF_HREF_LOW BIT(4)
  140. #define COMJ_PCLK_RISING BIT(4)
  141. #define COMJ_VSYNC_HIGH BIT(0)
  142. /* supported resolutions */
  143. #define W_QCIF (DEF_HSTOP - DEF_HSTRT)
  144. #define W_CIF (W_QCIF << 1)
  145. #define H_QCIF (DEF_VSTOP - DEF_VSTRT)
  146. #define H_CIF (H_QCIF << 1)
  147. #define FRAME_RATE_MAX 30
  148. struct ov6650_reg {
  149. u8 reg;
  150. u8 val;
  151. };
  152. struct ov6650 {
  153. struct v4l2_subdev subdev;
  154. struct v4l2_ctrl_handler hdl;
  155. struct {
  156. /* exposure/autoexposure cluster */
  157. struct v4l2_ctrl *autoexposure;
  158. struct v4l2_ctrl *exposure;
  159. };
  160. struct {
  161. /* gain/autogain cluster */
  162. struct v4l2_ctrl *autogain;
  163. struct v4l2_ctrl *gain;
  164. };
  165. struct {
  166. /* blue/red/autowhitebalance cluster */
  167. struct v4l2_ctrl *autowb;
  168. struct v4l2_ctrl *blue;
  169. struct v4l2_ctrl *red;
  170. };
  171. 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_parm */
  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 soc_camera_subdev_desc *ssdd = soc_camera_i2c_to_desc(client);
  370. struct ov6650 *priv = to_ov6650(client);
  371. return soc_camera_set_power(&client->dev, ssdd, priv->clk, on);
  372. }
  373. static int ov6650_get_selection(struct v4l2_subdev *sd,
  374. struct v4l2_subdev_pad_config *cfg,
  375. struct v4l2_subdev_selection *sel)
  376. {
  377. struct i2c_client *client = v4l2_get_subdevdata(sd);
  378. struct ov6650 *priv = to_ov6650(client);
  379. if (sel->which != V4L2_SUBDEV_FORMAT_ACTIVE)
  380. return -EINVAL;
  381. switch (sel->target) {
  382. case V4L2_SEL_TGT_CROP_BOUNDS:
  383. case V4L2_SEL_TGT_CROP_DEFAULT:
  384. sel->r.left = DEF_HSTRT << 1;
  385. sel->r.top = DEF_VSTRT << 1;
  386. sel->r.width = W_CIF;
  387. sel->r.height = H_CIF;
  388. return 0;
  389. case V4L2_SEL_TGT_CROP:
  390. sel->r = priv->rect;
  391. return 0;
  392. default:
  393. return -EINVAL;
  394. }
  395. }
  396. static int ov6650_set_selection(struct v4l2_subdev *sd,
  397. struct v4l2_subdev_pad_config *cfg,
  398. struct v4l2_subdev_selection *sel)
  399. {
  400. struct i2c_client *client = v4l2_get_subdevdata(sd);
  401. struct ov6650 *priv = to_ov6650(client);
  402. struct v4l2_rect rect = sel->r;
  403. int ret;
  404. if (sel->which != V4L2_SUBDEV_FORMAT_ACTIVE ||
  405. sel->target != V4L2_SEL_TGT_CROP)
  406. return -EINVAL;
  407. rect.left = ALIGN(rect.left, 2);
  408. rect.width = ALIGN(rect.width, 2);
  409. rect.top = ALIGN(rect.top, 2);
  410. rect.height = ALIGN(rect.height, 2);
  411. soc_camera_limit_side(&rect.left, &rect.width,
  412. DEF_HSTRT << 1, 2, W_CIF);
  413. soc_camera_limit_side(&rect.top, &rect.height,
  414. DEF_VSTRT << 1, 2, H_CIF);
  415. ret = ov6650_reg_write(client, REG_HSTRT, rect.left >> 1);
  416. if (!ret) {
  417. priv->rect.left = rect.left;
  418. ret = ov6650_reg_write(client, REG_HSTOP,
  419. (rect.left + rect.width) >> 1);
  420. }
  421. if (!ret) {
  422. priv->rect.width = rect.width;
  423. ret = ov6650_reg_write(client, REG_VSTRT, rect.top >> 1);
  424. }
  425. if (!ret) {
  426. priv->rect.top = rect.top;
  427. ret = ov6650_reg_write(client, REG_VSTOP,
  428. (rect.top + rect.height) >> 1);
  429. }
  430. if (!ret)
  431. priv->rect.height = rect.height;
  432. return ret;
  433. }
  434. static int ov6650_get_fmt(struct v4l2_subdev *sd,
  435. struct v4l2_subdev_pad_config *cfg,
  436. struct v4l2_subdev_format *format)
  437. {
  438. struct v4l2_mbus_framefmt *mf = &format->format;
  439. struct i2c_client *client = v4l2_get_subdevdata(sd);
  440. struct ov6650 *priv = to_ov6650(client);
  441. if (format->pad)
  442. return -EINVAL;
  443. mf->width = priv->rect.width >> priv->half_scale;
  444. mf->height = priv->rect.height >> priv->half_scale;
  445. mf->code = priv->code;
  446. mf->colorspace = priv->colorspace;
  447. mf->field = V4L2_FIELD_NONE;
  448. return 0;
  449. }
  450. static bool is_unscaled_ok(int width, int height, struct v4l2_rect *rect)
  451. {
  452. return width > rect->width >> 1 || height > rect->height >> 1;
  453. }
  454. static u8 to_clkrc(struct v4l2_fract *timeperframe,
  455. unsigned long pclk_limit, unsigned long pclk_max)
  456. {
  457. unsigned long pclk;
  458. if (timeperframe->numerator && timeperframe->denominator)
  459. pclk = pclk_max * timeperframe->denominator /
  460. (FRAME_RATE_MAX * timeperframe->numerator);
  461. else
  462. pclk = pclk_max;
  463. if (pclk_limit && pclk_limit < pclk)
  464. pclk = pclk_limit;
  465. return (pclk_max - 1) / pclk;
  466. }
  467. /* set the format we will capture in */
  468. static int ov6650_s_fmt(struct v4l2_subdev *sd, struct v4l2_mbus_framefmt *mf)
  469. {
  470. struct i2c_client *client = v4l2_get_subdevdata(sd);
  471. struct soc_camera_device *icd = v4l2_get_subdev_hostdata(sd);
  472. struct soc_camera_sense *sense = icd->sense;
  473. struct ov6650 *priv = to_ov6650(client);
  474. bool half_scale = !is_unscaled_ok(mf->width, mf->height, &priv->rect);
  475. struct v4l2_subdev_selection sel = {
  476. .which = V4L2_SUBDEV_FORMAT_ACTIVE,
  477. .target = V4L2_SEL_TGT_CROP,
  478. .r.left = priv->rect.left + (priv->rect.width >> 1) -
  479. (mf->width >> (1 - half_scale)),
  480. .r.top = priv->rect.top + (priv->rect.height >> 1) -
  481. (mf->height >> (1 - half_scale)),
  482. .r.width = mf->width << half_scale,
  483. .r.height = mf->height << half_scale,
  484. };
  485. u32 code = mf->code;
  486. unsigned long mclk, pclk;
  487. u8 coma_set = 0, coma_mask = 0, coml_set, coml_mask, clkrc;
  488. int ret;
  489. /* select color matrix configuration for given color encoding */
  490. switch (code) {
  491. case MEDIA_BUS_FMT_Y8_1X8:
  492. dev_dbg(&client->dev, "pixel format GREY8_1X8\n");
  493. coma_mask |= COMA_RGB | COMA_WORD_SWAP | COMA_BYTE_SWAP;
  494. coma_set |= COMA_BW;
  495. break;
  496. case MEDIA_BUS_FMT_YUYV8_2X8:
  497. dev_dbg(&client->dev, "pixel format YUYV8_2X8_LE\n");
  498. coma_mask |= COMA_RGB | COMA_BW | COMA_BYTE_SWAP;
  499. coma_set |= COMA_WORD_SWAP;
  500. break;
  501. case MEDIA_BUS_FMT_YVYU8_2X8:
  502. dev_dbg(&client->dev, "pixel format YVYU8_2X8_LE (untested)\n");
  503. coma_mask |= COMA_RGB | COMA_BW | COMA_WORD_SWAP |
  504. COMA_BYTE_SWAP;
  505. break;
  506. case MEDIA_BUS_FMT_UYVY8_2X8:
  507. dev_dbg(&client->dev, "pixel format YUYV8_2X8_BE\n");
  508. if (half_scale) {
  509. coma_mask |= COMA_RGB | COMA_BW | COMA_WORD_SWAP;
  510. coma_set |= COMA_BYTE_SWAP;
  511. } else {
  512. coma_mask |= COMA_RGB | COMA_BW;
  513. coma_set |= COMA_BYTE_SWAP | COMA_WORD_SWAP;
  514. }
  515. break;
  516. case MEDIA_BUS_FMT_VYUY8_2X8:
  517. dev_dbg(&client->dev, "pixel format YVYU8_2X8_BE (untested)\n");
  518. if (half_scale) {
  519. coma_mask |= COMA_RGB | COMA_BW;
  520. coma_set |= COMA_BYTE_SWAP | COMA_WORD_SWAP;
  521. } else {
  522. coma_mask |= COMA_RGB | COMA_BW | COMA_WORD_SWAP;
  523. coma_set |= COMA_BYTE_SWAP;
  524. }
  525. break;
  526. case MEDIA_BUS_FMT_SBGGR8_1X8:
  527. dev_dbg(&client->dev, "pixel format SBGGR8_1X8 (untested)\n");
  528. coma_mask |= COMA_BW | COMA_BYTE_SWAP | COMA_WORD_SWAP;
  529. coma_set |= COMA_RAW_RGB | COMA_RGB;
  530. break;
  531. default:
  532. dev_err(&client->dev, "Pixel format not handled: 0x%x\n", code);
  533. return -EINVAL;
  534. }
  535. priv->code = code;
  536. if (code == MEDIA_BUS_FMT_Y8_1X8 ||
  537. code == MEDIA_BUS_FMT_SBGGR8_1X8) {
  538. coml_mask = COML_ONE_CHANNEL;
  539. coml_set = 0;
  540. priv->pclk_max = 4000000;
  541. } else {
  542. coml_mask = 0;
  543. coml_set = COML_ONE_CHANNEL;
  544. priv->pclk_max = 8000000;
  545. }
  546. if (code == MEDIA_BUS_FMT_SBGGR8_1X8)
  547. priv->colorspace = V4L2_COLORSPACE_SRGB;
  548. else if (code != 0)
  549. priv->colorspace = V4L2_COLORSPACE_JPEG;
  550. if (half_scale) {
  551. dev_dbg(&client->dev, "max resolution: QCIF\n");
  552. coma_set |= COMA_QCIF;
  553. priv->pclk_max /= 2;
  554. } else {
  555. dev_dbg(&client->dev, "max resolution: CIF\n");
  556. coma_mask |= COMA_QCIF;
  557. }
  558. priv->half_scale = half_scale;
  559. if (sense) {
  560. if (sense->master_clock == 8000000) {
  561. dev_dbg(&client->dev, "8MHz input clock\n");
  562. clkrc = CLKRC_6MHz;
  563. } else if (sense->master_clock == 12000000) {
  564. dev_dbg(&client->dev, "12MHz input clock\n");
  565. clkrc = CLKRC_12MHz;
  566. } else if (sense->master_clock == 16000000) {
  567. dev_dbg(&client->dev, "16MHz input clock\n");
  568. clkrc = CLKRC_16MHz;
  569. } else if (sense->master_clock == 24000000) {
  570. dev_dbg(&client->dev, "24MHz input clock\n");
  571. clkrc = CLKRC_24MHz;
  572. } else {
  573. dev_err(&client->dev,
  574. "unsupported input clock, check platform data\n");
  575. return -EINVAL;
  576. }
  577. mclk = sense->master_clock;
  578. priv->pclk_limit = sense->pixel_clock_max;
  579. } else {
  580. clkrc = CLKRC_24MHz;
  581. mclk = 24000000;
  582. priv->pclk_limit = 0;
  583. dev_dbg(&client->dev, "using default 24MHz input clock\n");
  584. }
  585. clkrc |= to_clkrc(&priv->tpf, priv->pclk_limit, priv->pclk_max);
  586. pclk = priv->pclk_max / GET_CLKRC_DIV(clkrc);
  587. dev_dbg(&client->dev, "pixel clock divider: %ld.%ld\n",
  588. mclk / pclk, 10 * mclk % pclk / pclk);
  589. ret = ov6650_set_selection(sd, NULL, &sel);
  590. if (!ret)
  591. ret = ov6650_reg_rmw(client, REG_COMA, coma_set, coma_mask);
  592. if (!ret)
  593. ret = ov6650_reg_write(client, REG_CLKRC, clkrc);
  594. if (!ret)
  595. ret = ov6650_reg_rmw(client, REG_COML, coml_set, coml_mask);
  596. if (!ret) {
  597. mf->colorspace = priv->colorspace;
  598. mf->width = priv->rect.width >> half_scale;
  599. mf->height = priv->rect.height >> half_scale;
  600. }
  601. return ret;
  602. }
  603. static int ov6650_set_fmt(struct v4l2_subdev *sd,
  604. struct v4l2_subdev_pad_config *cfg,
  605. struct v4l2_subdev_format *format)
  606. {
  607. struct v4l2_mbus_framefmt *mf = &format->format;
  608. struct i2c_client *client = v4l2_get_subdevdata(sd);
  609. struct ov6650 *priv = to_ov6650(client);
  610. if (format->pad)
  611. return -EINVAL;
  612. if (is_unscaled_ok(mf->width, mf->height, &priv->rect))
  613. v4l_bound_align_image(&mf->width, 2, W_CIF, 1,
  614. &mf->height, 2, H_CIF, 1, 0);
  615. mf->field = V4L2_FIELD_NONE;
  616. switch (mf->code) {
  617. case MEDIA_BUS_FMT_Y10_1X10:
  618. mf->code = MEDIA_BUS_FMT_Y8_1X8;
  619. case MEDIA_BUS_FMT_Y8_1X8:
  620. case MEDIA_BUS_FMT_YVYU8_2X8:
  621. case MEDIA_BUS_FMT_YUYV8_2X8:
  622. case MEDIA_BUS_FMT_VYUY8_2X8:
  623. case MEDIA_BUS_FMT_UYVY8_2X8:
  624. mf->colorspace = V4L2_COLORSPACE_JPEG;
  625. break;
  626. default:
  627. mf->code = MEDIA_BUS_FMT_SBGGR8_1X8;
  628. case MEDIA_BUS_FMT_SBGGR8_1X8:
  629. mf->colorspace = V4L2_COLORSPACE_SRGB;
  630. break;
  631. }
  632. if (format->which == V4L2_SUBDEV_FORMAT_ACTIVE)
  633. return ov6650_s_fmt(sd, mf);
  634. cfg->try_fmt = *mf;
  635. return 0;
  636. }
  637. static int ov6650_enum_mbus_code(struct v4l2_subdev *sd,
  638. struct v4l2_subdev_pad_config *cfg,
  639. struct v4l2_subdev_mbus_code_enum *code)
  640. {
  641. if (code->pad || code->index >= ARRAY_SIZE(ov6650_codes))
  642. return -EINVAL;
  643. code->code = ov6650_codes[code->index];
  644. return 0;
  645. }
  646. static int ov6650_g_parm(struct v4l2_subdev *sd, struct v4l2_streamparm *parms)
  647. {
  648. struct i2c_client *client = v4l2_get_subdevdata(sd);
  649. struct ov6650 *priv = to_ov6650(client);
  650. struct v4l2_captureparm *cp = &parms->parm.capture;
  651. if (parms->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  652. return -EINVAL;
  653. memset(cp, 0, sizeof(*cp));
  654. cp->capability = V4L2_CAP_TIMEPERFRAME;
  655. cp->timeperframe.numerator = GET_CLKRC_DIV(to_clkrc(&priv->tpf,
  656. priv->pclk_limit, priv->pclk_max));
  657. cp->timeperframe.denominator = FRAME_RATE_MAX;
  658. dev_dbg(&client->dev, "Frame interval: %u/%u s\n",
  659. cp->timeperframe.numerator, cp->timeperframe.denominator);
  660. return 0;
  661. }
  662. static int ov6650_s_parm(struct v4l2_subdev *sd, struct v4l2_streamparm *parms)
  663. {
  664. struct i2c_client *client = v4l2_get_subdevdata(sd);
  665. struct ov6650 *priv = to_ov6650(client);
  666. struct v4l2_captureparm *cp = &parms->parm.capture;
  667. struct v4l2_fract *tpf = &cp->timeperframe;
  668. int div, ret;
  669. u8 clkrc;
  670. if (parms->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  671. return -EINVAL;
  672. if (cp->extendedmode != 0)
  673. return -EINVAL;
  674. if (tpf->numerator == 0 || tpf->denominator == 0)
  675. div = 1; /* Reset to full rate */
  676. else
  677. div = (tpf->numerator * FRAME_RATE_MAX) / tpf->denominator;
  678. if (div == 0)
  679. div = 1;
  680. else if (div > GET_CLKRC_DIV(CLKRC_DIV_MASK))
  681. div = GET_CLKRC_DIV(CLKRC_DIV_MASK);
  682. /*
  683. * Keep result to be used as tpf limit
  684. * for subseqent clock divider calculations
  685. */
  686. priv->tpf.numerator = div;
  687. priv->tpf.denominator = FRAME_RATE_MAX;
  688. clkrc = to_clkrc(&priv->tpf, priv->pclk_limit, priv->pclk_max);
  689. ret = ov6650_reg_rmw(client, REG_CLKRC, clkrc, CLKRC_DIV_MASK);
  690. if (!ret) {
  691. tpf->numerator = GET_CLKRC_DIV(clkrc);
  692. tpf->denominator = FRAME_RATE_MAX;
  693. }
  694. return ret;
  695. }
  696. /* Soft reset the camera. This has nothing to do with the RESET pin! */
  697. static int ov6650_reset(struct i2c_client *client)
  698. {
  699. int ret;
  700. dev_dbg(&client->dev, "reset\n");
  701. ret = ov6650_reg_rmw(client, REG_COMA, COMA_RESET, 0);
  702. if (ret)
  703. dev_err(&client->dev,
  704. "An error occurred while entering soft reset!\n");
  705. return ret;
  706. }
  707. /* program default register values */
  708. static int ov6650_prog_dflt(struct i2c_client *client)
  709. {
  710. int ret;
  711. dev_dbg(&client->dev, "initializing\n");
  712. ret = ov6650_reg_write(client, REG_COMA, 0); /* ~COMA_RESET */
  713. if (!ret)
  714. ret = ov6650_reg_rmw(client, REG_COMB, 0, COMB_BAND_FILTER);
  715. return ret;
  716. }
  717. static int ov6650_video_probe(struct i2c_client *client)
  718. {
  719. struct ov6650 *priv = to_ov6650(client);
  720. u8 pidh, pidl, midh, midl;
  721. int ret;
  722. priv->clk = v4l2_clk_get(&client->dev, NULL);
  723. if (IS_ERR(priv->clk)) {
  724. ret = PTR_ERR(priv->clk);
  725. dev_err(&client->dev, "v4l2_clk request err: %d\n", ret);
  726. return ret;
  727. }
  728. ret = ov6650_s_power(&priv->subdev, 1);
  729. if (ret < 0)
  730. goto eclkput;
  731. msleep(20);
  732. /*
  733. * check and show product ID and manufacturer ID
  734. */
  735. ret = ov6650_reg_read(client, REG_PIDH, &pidh);
  736. if (!ret)
  737. ret = ov6650_reg_read(client, REG_PIDL, &pidl);
  738. if (!ret)
  739. ret = ov6650_reg_read(client, REG_MIDH, &midh);
  740. if (!ret)
  741. ret = ov6650_reg_read(client, REG_MIDL, &midl);
  742. if (ret)
  743. goto done;
  744. if ((pidh != OV6650_PIDH) || (pidl != OV6650_PIDL)) {
  745. dev_err(&client->dev, "Product ID error 0x%02x:0x%02x\n",
  746. pidh, pidl);
  747. ret = -ENODEV;
  748. goto done;
  749. }
  750. dev_info(&client->dev,
  751. "ov6650 Product ID 0x%02x:0x%02x Manufacturer ID 0x%02x:0x%02x\n",
  752. pidh, pidl, midh, midl);
  753. ret = ov6650_reset(client);
  754. if (!ret)
  755. ret = ov6650_prog_dflt(client);
  756. if (!ret)
  757. ret = v4l2_ctrl_handler_setup(&priv->hdl);
  758. done:
  759. ov6650_s_power(&priv->subdev, 0);
  760. if (!ret)
  761. return 0;
  762. eclkput:
  763. v4l2_clk_put(priv->clk);
  764. return ret;
  765. }
  766. static const struct v4l2_ctrl_ops ov6550_ctrl_ops = {
  767. .g_volatile_ctrl = ov6550_g_volatile_ctrl,
  768. .s_ctrl = ov6550_s_ctrl,
  769. };
  770. static struct v4l2_subdev_core_ops ov6650_core_ops = {
  771. #ifdef CONFIG_VIDEO_ADV_DEBUG
  772. .g_register = ov6650_get_register,
  773. .s_register = ov6650_set_register,
  774. #endif
  775. .s_power = ov6650_s_power,
  776. };
  777. /* Request bus settings on camera side */
  778. static int ov6650_g_mbus_config(struct v4l2_subdev *sd,
  779. struct v4l2_mbus_config *cfg)
  780. {
  781. struct i2c_client *client = v4l2_get_subdevdata(sd);
  782. struct soc_camera_subdev_desc *ssdd = soc_camera_i2c_to_desc(client);
  783. cfg->flags = V4L2_MBUS_MASTER |
  784. V4L2_MBUS_PCLK_SAMPLE_RISING | V4L2_MBUS_PCLK_SAMPLE_FALLING |
  785. V4L2_MBUS_HSYNC_ACTIVE_HIGH | V4L2_MBUS_HSYNC_ACTIVE_LOW |
  786. V4L2_MBUS_VSYNC_ACTIVE_HIGH | V4L2_MBUS_VSYNC_ACTIVE_LOW |
  787. V4L2_MBUS_DATA_ACTIVE_HIGH;
  788. cfg->type = V4L2_MBUS_PARALLEL;
  789. cfg->flags = soc_camera_apply_board_flags(ssdd, cfg);
  790. return 0;
  791. }
  792. /* Alter bus settings on camera side */
  793. static int ov6650_s_mbus_config(struct v4l2_subdev *sd,
  794. const struct v4l2_mbus_config *cfg)
  795. {
  796. struct i2c_client *client = v4l2_get_subdevdata(sd);
  797. struct soc_camera_subdev_desc *ssdd = soc_camera_i2c_to_desc(client);
  798. unsigned long flags = soc_camera_apply_board_flags(ssdd, cfg);
  799. int ret;
  800. if (flags & V4L2_MBUS_PCLK_SAMPLE_RISING)
  801. ret = ov6650_reg_rmw(client, REG_COMJ, COMJ_PCLK_RISING, 0);
  802. else
  803. ret = ov6650_reg_rmw(client, REG_COMJ, 0, COMJ_PCLK_RISING);
  804. if (ret)
  805. return ret;
  806. if (flags & V4L2_MBUS_HSYNC_ACTIVE_LOW)
  807. ret = ov6650_reg_rmw(client, REG_COMF, COMF_HREF_LOW, 0);
  808. else
  809. ret = ov6650_reg_rmw(client, REG_COMF, 0, COMF_HREF_LOW);
  810. if (ret)
  811. return ret;
  812. if (flags & V4L2_MBUS_VSYNC_ACTIVE_HIGH)
  813. ret = ov6650_reg_rmw(client, REG_COMJ, COMJ_VSYNC_HIGH, 0);
  814. else
  815. ret = ov6650_reg_rmw(client, REG_COMJ, 0, COMJ_VSYNC_HIGH);
  816. return ret;
  817. }
  818. static struct v4l2_subdev_video_ops ov6650_video_ops = {
  819. .s_stream = ov6650_s_stream,
  820. .g_parm = ov6650_g_parm,
  821. .s_parm = ov6650_s_parm,
  822. .g_mbus_config = ov6650_g_mbus_config,
  823. .s_mbus_config = ov6650_s_mbus_config,
  824. };
  825. static const struct v4l2_subdev_pad_ops ov6650_pad_ops = {
  826. .enum_mbus_code = ov6650_enum_mbus_code,
  827. .get_selection = ov6650_get_selection,
  828. .set_selection = ov6650_set_selection,
  829. .get_fmt = ov6650_get_fmt,
  830. .set_fmt = ov6650_set_fmt,
  831. };
  832. static struct v4l2_subdev_ops ov6650_subdev_ops = {
  833. .core = &ov6650_core_ops,
  834. .video = &ov6650_video_ops,
  835. .pad = &ov6650_pad_ops,
  836. };
  837. /*
  838. * i2c_driver function
  839. */
  840. static int ov6650_probe(struct i2c_client *client,
  841. const struct i2c_device_id *did)
  842. {
  843. struct ov6650 *priv;
  844. struct soc_camera_subdev_desc *ssdd = soc_camera_i2c_to_desc(client);
  845. int ret;
  846. if (!ssdd) {
  847. dev_err(&client->dev, "Missing platform_data for driver\n");
  848. return -EINVAL;
  849. }
  850. priv = devm_kzalloc(&client->dev, sizeof(*priv), GFP_KERNEL);
  851. if (!priv) {
  852. dev_err(&client->dev,
  853. "Failed to allocate memory for private data!\n");
  854. return -ENOMEM;
  855. }
  856. v4l2_i2c_subdev_init(&priv->subdev, client, &ov6650_subdev_ops);
  857. v4l2_ctrl_handler_init(&priv->hdl, 13);
  858. v4l2_ctrl_new_std(&priv->hdl, &ov6550_ctrl_ops,
  859. V4L2_CID_VFLIP, 0, 1, 1, 0);
  860. v4l2_ctrl_new_std(&priv->hdl, &ov6550_ctrl_ops,
  861. V4L2_CID_HFLIP, 0, 1, 1, 0);
  862. priv->autogain = v4l2_ctrl_new_std(&priv->hdl, &ov6550_ctrl_ops,
  863. V4L2_CID_AUTOGAIN, 0, 1, 1, 1);
  864. priv->gain = v4l2_ctrl_new_std(&priv->hdl, &ov6550_ctrl_ops,
  865. V4L2_CID_GAIN, 0, 0x3f, 1, DEF_GAIN);
  866. priv->autowb = v4l2_ctrl_new_std(&priv->hdl, &ov6550_ctrl_ops,
  867. V4L2_CID_AUTO_WHITE_BALANCE, 0, 1, 1, 1);
  868. priv->blue = v4l2_ctrl_new_std(&priv->hdl, &ov6550_ctrl_ops,
  869. V4L2_CID_BLUE_BALANCE, 0, 0xff, 1, DEF_BLUE);
  870. priv->red = v4l2_ctrl_new_std(&priv->hdl, &ov6550_ctrl_ops,
  871. V4L2_CID_RED_BALANCE, 0, 0xff, 1, DEF_RED);
  872. v4l2_ctrl_new_std(&priv->hdl, &ov6550_ctrl_ops,
  873. V4L2_CID_SATURATION, 0, 0xf, 1, 0x8);
  874. v4l2_ctrl_new_std(&priv->hdl, &ov6550_ctrl_ops,
  875. V4L2_CID_HUE, 0, HUE_MASK, 1, DEF_HUE);
  876. v4l2_ctrl_new_std(&priv->hdl, &ov6550_ctrl_ops,
  877. V4L2_CID_BRIGHTNESS, 0, 0xff, 1, 0x80);
  878. priv->autoexposure = v4l2_ctrl_new_std_menu(&priv->hdl,
  879. &ov6550_ctrl_ops, V4L2_CID_EXPOSURE_AUTO,
  880. V4L2_EXPOSURE_MANUAL, 0, V4L2_EXPOSURE_AUTO);
  881. priv->exposure = v4l2_ctrl_new_std(&priv->hdl, &ov6550_ctrl_ops,
  882. V4L2_CID_EXPOSURE, 0, 0xff, 1, DEF_AECH);
  883. v4l2_ctrl_new_std(&priv->hdl, &ov6550_ctrl_ops,
  884. V4L2_CID_GAMMA, 0, 0xff, 1, 0x12);
  885. priv->subdev.ctrl_handler = &priv->hdl;
  886. if (priv->hdl.error)
  887. return priv->hdl.error;
  888. v4l2_ctrl_auto_cluster(2, &priv->autogain, 0, true);
  889. v4l2_ctrl_auto_cluster(3, &priv->autowb, 0, true);
  890. v4l2_ctrl_auto_cluster(2, &priv->autoexposure,
  891. V4L2_EXPOSURE_MANUAL, true);
  892. priv->rect.left = DEF_HSTRT << 1;
  893. priv->rect.top = DEF_VSTRT << 1;
  894. priv->rect.width = W_CIF;
  895. priv->rect.height = H_CIF;
  896. priv->half_scale = false;
  897. priv->code = MEDIA_BUS_FMT_YUYV8_2X8;
  898. priv->colorspace = V4L2_COLORSPACE_JPEG;
  899. ret = ov6650_video_probe(client);
  900. if (ret)
  901. v4l2_ctrl_handler_free(&priv->hdl);
  902. return ret;
  903. }
  904. static int ov6650_remove(struct i2c_client *client)
  905. {
  906. struct ov6650 *priv = to_ov6650(client);
  907. v4l2_clk_put(priv->clk);
  908. v4l2_device_unregister_subdev(&priv->subdev);
  909. v4l2_ctrl_handler_free(&priv->hdl);
  910. return 0;
  911. }
  912. static const struct i2c_device_id ov6650_id[] = {
  913. { "ov6650", 0 },
  914. { }
  915. };
  916. MODULE_DEVICE_TABLE(i2c, ov6650_id);
  917. static struct i2c_driver ov6650_i2c_driver = {
  918. .driver = {
  919. .name = "ov6650",
  920. },
  921. .probe = ov6650_probe,
  922. .remove = ov6650_remove,
  923. .id_table = ov6650_id,
  924. };
  925. module_i2c_driver(ov6650_i2c_driver);
  926. MODULE_DESCRIPTION("SoC Camera driver for OmniVision OV6650");
  927. MODULE_AUTHOR("Janusz Krzysztofik <[email protected]>");
  928. MODULE_LICENSE("GPL v2");