ov6650.c 27 KB

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