mt9v111.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * V4L2 sensor driver for Aptina MT9V111 image sensor
  4. * Copyright (C) 2018 Jacopo Mondi <jacopo@jmondi.org>
  5. *
  6. * Based on mt9v032 driver
  7. * Copyright (C) 2010, Laurent Pinchart <laurent.pinchart@ideasonboard.com>
  8. * Copyright (C) 2008, Guennadi Liakhovetski <kernel@pengutronix.de>
  9. *
  10. * Based on mt9v011 driver
  11. * Copyright (c) 2009 Mauro Carvalho Chehab <mchehab@kernel.org>
  12. */
  13. #include <linux/clk.h>
  14. #include <linux/delay.h>
  15. #include <linux/gpio/consumer.h>
  16. #include <linux/i2c.h>
  17. #include <linux/of.h>
  18. #include <linux/slab.h>
  19. #include <linux/videodev2.h>
  20. #include <linux/v4l2-mediabus.h>
  21. #include <linux/module.h>
  22. #include <media/v4l2-ctrls.h>
  23. #include <media/v4l2-device.h>
  24. #include <media/v4l2-fwnode.h>
  25. #include <media/v4l2-image-sizes.h>
  26. #include <media/v4l2-subdev.h>
  27. /*
  28. * MT9V111 is a 1/4-Inch CMOS digital image sensor with an integrated
  29. * Image Flow Processing (IFP) engine and a sensor core loosely based on
  30. * MT9V011.
  31. *
  32. * The IFP can produce several output image formats from the sensor core
  33. * output. This driver currently supports only YUYV format permutations.
  34. *
  35. * The driver allows manual frame rate control through set_frame_interval subdev
  36. * operation or V4L2_CID_V/HBLANK controls, but it is known that the
  37. * auto-exposure algorithm might modify the programmed frame rate. While the
  38. * driver initially programs the sensor with auto-exposure and
  39. * auto-white-balancing enabled, it is possible to disable them and more
  40. * precisely control the frame rate.
  41. *
  42. * While it seems possible to instruct the auto-exposure control algorithm to
  43. * respect a programmed frame rate when adjusting the pixel integration time,
  44. * registers controlling this feature are not documented in the public
  45. * available sensor manual used to develop this driver (09005aef80e90084,
  46. * MT9V111_1.fm - Rev. G 1/05 EN).
  47. */
  48. #define MT9V111_CHIP_ID_HIGH 0x82
  49. #define MT9V111_CHIP_ID_LOW 0x3a
  50. #define MT9V111_R01_ADDR_SPACE 0x01
  51. #define MT9V111_R01_IFP 0x01
  52. #define MT9V111_R01_CORE 0x04
  53. #define MT9V111_IFP_R06_OPMODE_CTRL 0x06
  54. #define MT9V111_IFP_R06_OPMODE_CTRL_AWB_EN BIT(1)
  55. #define MT9V111_IFP_R06_OPMODE_CTRL_AE_EN BIT(14)
  56. #define MT9V111_IFP_R07_IFP_RESET 0x07
  57. #define MT9V111_IFP_R07_IFP_RESET_MASK BIT(0)
  58. #define MT9V111_IFP_R08_OUTFMT_CTRL 0x08
  59. #define MT9V111_IFP_R08_OUTFMT_CTRL_FLICKER BIT(11)
  60. #define MT9V111_IFP_R08_OUTFMT_CTRL_PCLK BIT(5)
  61. #define MT9V111_IFP_R3A_OUTFMT_CTRL2 0x3a
  62. #define MT9V111_IFP_R3A_OUTFMT_CTRL2_SWAP_CBCR BIT(0)
  63. #define MT9V111_IFP_R3A_OUTFMT_CTRL2_SWAP_YC BIT(1)
  64. #define MT9V111_IFP_R3A_OUTFMT_CTRL2_SWAP_MASK GENMASK(2, 0)
  65. #define MT9V111_IFP_RA5_HPAN 0xa5
  66. #define MT9V111_IFP_RA6_HZOOM 0xa6
  67. #define MT9V111_IFP_RA7_HOUT 0xa7
  68. #define MT9V111_IFP_RA8_VPAN 0xa8
  69. #define MT9V111_IFP_RA9_VZOOM 0xa9
  70. #define MT9V111_IFP_RAA_VOUT 0xaa
  71. #define MT9V111_IFP_DECIMATION_MASK GENMASK(9, 0)
  72. #define MT9V111_IFP_DECIMATION_FREEZE BIT(15)
  73. #define MT9V111_CORE_R03_WIN_HEIGHT 0x03
  74. #define MT9V111_CORE_R03_WIN_V_OFFS 2
  75. #define MT9V111_CORE_R04_WIN_WIDTH 0x04
  76. #define MT9V111_CORE_R04_WIN_H_OFFS 114
  77. #define MT9V111_CORE_R05_HBLANK 0x05
  78. #define MT9V111_CORE_R05_MIN_HBLANK 0x09
  79. #define MT9V111_CORE_R05_MAX_HBLANK GENMASK(9, 0)
  80. #define MT9V111_CORE_R05_DEF_HBLANK 0x26
  81. #define MT9V111_CORE_R06_VBLANK 0x06
  82. #define MT9V111_CORE_R06_MIN_VBLANK 0x03
  83. #define MT9V111_CORE_R06_MAX_VBLANK GENMASK(11, 0)
  84. #define MT9V111_CORE_R06_DEF_VBLANK 0x04
  85. #define MT9V111_CORE_R07_OUT_CTRL 0x07
  86. #define MT9V111_CORE_R07_OUT_CTRL_SAMPLE BIT(4)
  87. #define MT9V111_CORE_R09_PIXEL_INT 0x09
  88. #define MT9V111_CORE_R09_PIXEL_INT_MASK GENMASK(11, 0)
  89. #define MT9V111_CORE_R0D_CORE_RESET 0x0d
  90. #define MT9V111_CORE_R0D_CORE_RESET_MASK BIT(0)
  91. #define MT9V111_CORE_RFF_CHIP_VER 0xff
  92. #define MT9V111_PIXEL_ARRAY_WIDTH 640
  93. #define MT9V111_PIXEL_ARRAY_HEIGHT 480
  94. #define MT9V111_MAX_CLKIN 27000000
  95. /* The default sensor configuration at startup time. */
  96. static const struct v4l2_mbus_framefmt mt9v111_def_fmt = {
  97. .width = 640,
  98. .height = 480,
  99. .code = MEDIA_BUS_FMT_UYVY8_2X8,
  100. .field = V4L2_FIELD_NONE,
  101. .colorspace = V4L2_COLORSPACE_SRGB,
  102. .ycbcr_enc = V4L2_YCBCR_ENC_601,
  103. .quantization = V4L2_QUANTIZATION_LIM_RANGE,
  104. .xfer_func = V4L2_XFER_FUNC_SRGB,
  105. };
  106. struct mt9v111_dev {
  107. struct device *dev;
  108. struct i2c_client *client;
  109. u8 addr_space;
  110. struct v4l2_subdev sd;
  111. struct media_pad pad;
  112. struct v4l2_ctrl *auto_awb;
  113. struct v4l2_ctrl *auto_exp;
  114. struct v4l2_ctrl *hblank;
  115. struct v4l2_ctrl *vblank;
  116. struct v4l2_ctrl_handler ctrls;
  117. /* Output image format and sizes. */
  118. struct v4l2_mbus_framefmt fmt;
  119. unsigned int fps;
  120. /* Protects power up/down sequences. */
  121. struct mutex pwr_mutex;
  122. int pwr_count;
  123. /* Protects stream on/off sequences. */
  124. struct mutex stream_mutex;
  125. bool streaming;
  126. /* Flags to mark HW settings as not yet applied. */
  127. bool pending;
  128. /* Clock provider and system clock frequency. */
  129. struct clk *clk;
  130. u32 sysclk;
  131. struct gpio_desc *oe;
  132. struct gpio_desc *standby;
  133. struct gpio_desc *reset;
  134. };
  135. #define sd_to_mt9v111(__sd) container_of((__sd), struct mt9v111_dev, sd)
  136. /*
  137. * mt9v111_mbus_fmt - List all media bus formats supported by the driver.
  138. *
  139. * Only list the media bus code here. The image sizes are freely configurable
  140. * in the pixel array sizes range.
  141. *
  142. * The desired frame interval, in the supported frame interval range, is
  143. * obtained by configuring blanking as the sensor does not have a PLL but
  144. * only a fixed clock divider that generates the output pixel clock.
  145. */
  146. static struct mt9v111_mbus_fmt {
  147. u32 code;
  148. } mt9v111_formats[] = {
  149. {
  150. .code = MEDIA_BUS_FMT_UYVY8_2X8,
  151. },
  152. {
  153. .code = MEDIA_BUS_FMT_YUYV8_2X8,
  154. },
  155. {
  156. .code = MEDIA_BUS_FMT_VYUY8_2X8,
  157. },
  158. {
  159. .code = MEDIA_BUS_FMT_YVYU8_2X8,
  160. },
  161. };
  162. static u32 mt9v111_frame_intervals[] = {5, 10, 15, 20, 30};
  163. /*
  164. * mt9v111_frame_sizes - List sensor's supported resolutions.
  165. *
  166. * Resolution generated through decimation in the IFP block from the
  167. * full VGA pixel array.
  168. */
  169. static struct v4l2_rect mt9v111_frame_sizes[] = {
  170. {
  171. .width = 640,
  172. .height = 480,
  173. },
  174. {
  175. .width = 352,
  176. .height = 288
  177. },
  178. {
  179. .width = 320,
  180. .height = 240,
  181. },
  182. {
  183. .width = 176,
  184. .height = 144,
  185. },
  186. {
  187. .width = 160,
  188. .height = 120,
  189. },
  190. };
  191. /* --- Device I/O access --- */
  192. static int __mt9v111_read(struct i2c_client *c, u8 reg, u16 *val)
  193. {
  194. struct i2c_msg msg[2];
  195. __be16 buf;
  196. int ret;
  197. msg[0].addr = c->addr;
  198. msg[0].flags = 0;
  199. msg[0].len = 1;
  200. msg[0].buf = &reg;
  201. msg[1].addr = c->addr;
  202. msg[1].flags = I2C_M_RD;
  203. msg[1].len = 2;
  204. msg[1].buf = (char *)&buf;
  205. ret = i2c_transfer(c->adapter, msg, 2);
  206. if (ret < 0) {
  207. dev_err(&c->dev, "i2c read transfer error: %d\n", ret);
  208. return ret;
  209. }
  210. *val = be16_to_cpu(buf);
  211. dev_dbg(&c->dev, "%s: %x=%x\n", __func__, reg, *val);
  212. return 0;
  213. }
  214. static int __mt9v111_write(struct i2c_client *c, u8 reg, u16 val)
  215. {
  216. struct i2c_msg msg;
  217. u8 buf[3] = { 0 };
  218. int ret;
  219. buf[0] = reg;
  220. buf[1] = val >> 8;
  221. buf[2] = val & 0xff;
  222. msg.addr = c->addr;
  223. msg.flags = 0;
  224. msg.len = 3;
  225. msg.buf = (char *)buf;
  226. dev_dbg(&c->dev, "%s: %x = %x%x\n", __func__, reg, buf[1], buf[2]);
  227. ret = i2c_transfer(c->adapter, &msg, 1);
  228. if (ret < 0) {
  229. dev_err(&c->dev, "i2c write transfer error: %d\n", ret);
  230. return ret;
  231. }
  232. return 0;
  233. }
  234. static int __mt9v111_addr_space_select(struct i2c_client *c, u16 addr_space)
  235. {
  236. struct v4l2_subdev *sd = i2c_get_clientdata(c);
  237. struct mt9v111_dev *mt9v111 = sd_to_mt9v111(sd);
  238. u16 val;
  239. int ret;
  240. if (mt9v111->addr_space == addr_space)
  241. return 0;
  242. ret = __mt9v111_write(c, MT9V111_R01_ADDR_SPACE, addr_space);
  243. if (ret)
  244. return ret;
  245. /* Verify address space has been updated */
  246. ret = __mt9v111_read(c, MT9V111_R01_ADDR_SPACE, &val);
  247. if (ret)
  248. return ret;
  249. if (val != addr_space)
  250. return -EINVAL;
  251. mt9v111->addr_space = addr_space;
  252. return 0;
  253. }
  254. static int mt9v111_read(struct i2c_client *c, u8 addr_space, u8 reg, u16 *val)
  255. {
  256. int ret;
  257. /* Select register address space first. */
  258. ret = __mt9v111_addr_space_select(c, addr_space);
  259. if (ret)
  260. return ret;
  261. ret = __mt9v111_read(c, reg, val);
  262. if (ret)
  263. return ret;
  264. return 0;
  265. }
  266. static int mt9v111_write(struct i2c_client *c, u8 addr_space, u8 reg, u16 val)
  267. {
  268. int ret;
  269. /* Select register address space first. */
  270. ret = __mt9v111_addr_space_select(c, addr_space);
  271. if (ret)
  272. return ret;
  273. ret = __mt9v111_write(c, reg, val);
  274. if (ret)
  275. return ret;
  276. return 0;
  277. }
  278. static int mt9v111_update(struct i2c_client *c, u8 addr_space, u8 reg,
  279. u16 mask, u16 val)
  280. {
  281. u16 current_val;
  282. int ret;
  283. /* Select register address space first. */
  284. ret = __mt9v111_addr_space_select(c, addr_space);
  285. if (ret)
  286. return ret;
  287. /* Read the current register value, then update it. */
  288. ret = __mt9v111_read(c, reg, &current_val);
  289. if (ret)
  290. return ret;
  291. current_val &= ~mask;
  292. current_val |= (val & mask);
  293. ret = __mt9v111_write(c, reg, current_val);
  294. if (ret)
  295. return ret;
  296. return 0;
  297. }
  298. /* --- Sensor HW operations --- */
  299. static int __mt9v111_power_on(struct v4l2_subdev *sd)
  300. {
  301. struct mt9v111_dev *mt9v111 = sd_to_mt9v111(sd);
  302. int ret;
  303. ret = clk_prepare_enable(mt9v111->clk);
  304. if (ret)
  305. return ret;
  306. gpiod_set_value(mt9v111->standby, 0);
  307. usleep_range(500, 1000);
  308. gpiod_set_value(mt9v111->oe, 1);
  309. usleep_range(500, 1000);
  310. return 0;
  311. }
  312. static int __mt9v111_power_off(struct v4l2_subdev *sd)
  313. {
  314. struct mt9v111_dev *mt9v111 = sd_to_mt9v111(sd);
  315. gpiod_set_value(mt9v111->oe, 0);
  316. usleep_range(500, 1000);
  317. gpiod_set_value(mt9v111->standby, 1);
  318. usleep_range(500, 1000);
  319. clk_disable_unprepare(mt9v111->clk);
  320. return 0;
  321. }
  322. static int __mt9v111_hw_reset(struct mt9v111_dev *mt9v111)
  323. {
  324. if (!mt9v111->reset)
  325. return -EINVAL;
  326. gpiod_set_value(mt9v111->reset, 1);
  327. usleep_range(500, 1000);
  328. gpiod_set_value(mt9v111->reset, 0);
  329. usleep_range(500, 1000);
  330. return 0;
  331. }
  332. static int __mt9v111_sw_reset(struct mt9v111_dev *mt9v111)
  333. {
  334. struct i2c_client *c = mt9v111->client;
  335. int ret;
  336. /* Software reset core and IFP blocks. */
  337. ret = mt9v111_update(c, MT9V111_R01_CORE,
  338. MT9V111_CORE_R0D_CORE_RESET,
  339. MT9V111_CORE_R0D_CORE_RESET_MASK, 1);
  340. if (ret)
  341. return ret;
  342. usleep_range(500, 1000);
  343. ret = mt9v111_update(c, MT9V111_R01_CORE,
  344. MT9V111_CORE_R0D_CORE_RESET,
  345. MT9V111_CORE_R0D_CORE_RESET_MASK, 0);
  346. if (ret)
  347. return ret;
  348. usleep_range(500, 1000);
  349. ret = mt9v111_update(c, MT9V111_R01_IFP,
  350. MT9V111_IFP_R07_IFP_RESET,
  351. MT9V111_IFP_R07_IFP_RESET_MASK, 1);
  352. if (ret)
  353. return ret;
  354. usleep_range(500, 1000);
  355. ret = mt9v111_update(c, MT9V111_R01_IFP,
  356. MT9V111_IFP_R07_IFP_RESET,
  357. MT9V111_IFP_R07_IFP_RESET_MASK, 0);
  358. if (ret)
  359. return ret;
  360. usleep_range(500, 1000);
  361. return 0;
  362. }
  363. static int mt9v111_calc_frame_rate(struct mt9v111_dev *mt9v111,
  364. struct v4l2_fract *tpf)
  365. {
  366. unsigned int fps = tpf->numerator ?
  367. tpf->denominator / tpf->numerator :
  368. tpf->denominator;
  369. unsigned int best_diff;
  370. unsigned int frm_cols;
  371. unsigned int row_pclk;
  372. unsigned int best_fps;
  373. unsigned int pclk;
  374. unsigned int diff;
  375. unsigned int idx;
  376. unsigned int hb;
  377. unsigned int vb;
  378. unsigned int i;
  379. int ret;
  380. /* Approximate to the closest supported frame interval. */
  381. best_diff = ~0L;
  382. for (i = 0, idx = 0; i < ARRAY_SIZE(mt9v111_frame_intervals); i++) {
  383. diff = abs(fps - mt9v111_frame_intervals[i]);
  384. if (diff < best_diff) {
  385. idx = i;
  386. best_diff = diff;
  387. }
  388. }
  389. fps = mt9v111_frame_intervals[idx];
  390. /*
  391. * The sensor does not provide a PLL circuitry and pixel clock is
  392. * generated dividing the master clock source by two.
  393. *
  394. * Trow = (W + Hblank + 114) * 2 * (1 / SYSCLK)
  395. * TFrame = Trow * (H + Vblank + 2)
  396. *
  397. * FPS = (SYSCLK / 2) / (Trow * (H + Vblank + 2))
  398. *
  399. * This boils down to tune H and V blanks to best approximate the
  400. * above equation.
  401. *
  402. * Test all available H/V blank values, until we reach the
  403. * desired frame rate.
  404. */
  405. best_fps = vb = hb = 0;
  406. pclk = DIV_ROUND_CLOSEST(mt9v111->sysclk, 2);
  407. row_pclk = MT9V111_PIXEL_ARRAY_WIDTH + 7 + MT9V111_CORE_R04_WIN_H_OFFS;
  408. frm_cols = MT9V111_PIXEL_ARRAY_HEIGHT + 7 + MT9V111_CORE_R03_WIN_V_OFFS;
  409. best_diff = ~0L;
  410. for (vb = MT9V111_CORE_R06_MIN_VBLANK;
  411. vb < MT9V111_CORE_R06_MAX_VBLANK; vb++) {
  412. for (hb = MT9V111_CORE_R05_MIN_HBLANK;
  413. hb < MT9V111_CORE_R05_MAX_HBLANK; hb += 10) {
  414. unsigned int t_frame = (row_pclk + hb) *
  415. (frm_cols + vb);
  416. unsigned int t_fps = DIV_ROUND_CLOSEST(pclk, t_frame);
  417. diff = abs(fps - t_fps);
  418. if (diff < best_diff) {
  419. best_diff = diff;
  420. best_fps = t_fps;
  421. if (diff == 0)
  422. break;
  423. }
  424. }
  425. if (diff == 0)
  426. break;
  427. }
  428. ret = v4l2_ctrl_s_ctrl_int64(mt9v111->hblank, hb);
  429. if (ret)
  430. return ret;
  431. ret = v4l2_ctrl_s_ctrl_int64(mt9v111->vblank, vb);
  432. if (ret)
  433. return ret;
  434. tpf->numerator = 1;
  435. tpf->denominator = best_fps;
  436. return 0;
  437. }
  438. static int mt9v111_hw_config(struct mt9v111_dev *mt9v111)
  439. {
  440. struct i2c_client *c = mt9v111->client;
  441. u16 outfmtctrl2;
  442. int ret;
  443. /* Force device reset. */
  444. ret = __mt9v111_hw_reset(mt9v111);
  445. if (ret == -EINVAL)
  446. ret = __mt9v111_sw_reset(mt9v111);
  447. if (ret)
  448. return ret;
  449. /* Configure internal clock sample rate. */
  450. ret = mt9v111->sysclk < DIV_ROUND_CLOSEST(MT9V111_MAX_CLKIN, 2) ?
  451. mt9v111_update(c, MT9V111_R01_CORE,
  452. MT9V111_CORE_R07_OUT_CTRL,
  453. MT9V111_CORE_R07_OUT_CTRL_SAMPLE, 1) :
  454. mt9v111_update(c, MT9V111_R01_CORE,
  455. MT9V111_CORE_R07_OUT_CTRL,
  456. MT9V111_CORE_R07_OUT_CTRL_SAMPLE, 0);
  457. if (ret)
  458. return ret;
  459. /*
  460. * Configure output image format components ordering.
  461. *
  462. * TODO: IFP block can also output several RGB permutations, we only
  463. * support YUYV permutations at the moment.
  464. */
  465. switch (mt9v111->fmt.code) {
  466. case MEDIA_BUS_FMT_YUYV8_2X8:
  467. outfmtctrl2 = MT9V111_IFP_R3A_OUTFMT_CTRL2_SWAP_YC;
  468. break;
  469. case MEDIA_BUS_FMT_VYUY8_2X8:
  470. outfmtctrl2 = MT9V111_IFP_R3A_OUTFMT_CTRL2_SWAP_CBCR;
  471. break;
  472. case MEDIA_BUS_FMT_YVYU8_2X8:
  473. outfmtctrl2 = MT9V111_IFP_R3A_OUTFMT_CTRL2_SWAP_YC |
  474. MT9V111_IFP_R3A_OUTFMT_CTRL2_SWAP_CBCR;
  475. break;
  476. case MEDIA_BUS_FMT_UYVY8_2X8:
  477. default:
  478. outfmtctrl2 = 0;
  479. break;
  480. }
  481. ret = mt9v111_update(c, MT9V111_R01_IFP, MT9V111_IFP_R3A_OUTFMT_CTRL2,
  482. MT9V111_IFP_R3A_OUTFMT_CTRL2_SWAP_MASK,
  483. outfmtctrl2);
  484. if (ret)
  485. return ret;
  486. /*
  487. * Do not change default sensor's core configuration:
  488. * output the whole 640x480 pixel array, skip 18 columns and 6 rows.
  489. *
  490. * Instead, control the output image size through IFP block.
  491. *
  492. * TODO: No zoom&pan support. Currently we control the output image
  493. * size only through decimation, with no zoom support.
  494. */
  495. ret = mt9v111_write(c, MT9V111_R01_IFP, MT9V111_IFP_RA5_HPAN,
  496. MT9V111_IFP_DECIMATION_FREEZE);
  497. if (ret)
  498. return ret;
  499. ret = mt9v111_write(c, MT9V111_R01_IFP, MT9V111_IFP_RA8_VPAN,
  500. MT9V111_IFP_DECIMATION_FREEZE);
  501. if (ret)
  502. return ret;
  503. ret = mt9v111_write(c, MT9V111_R01_IFP, MT9V111_IFP_RA6_HZOOM,
  504. MT9V111_IFP_DECIMATION_FREEZE |
  505. MT9V111_PIXEL_ARRAY_WIDTH);
  506. if (ret)
  507. return ret;
  508. ret = mt9v111_write(c, MT9V111_R01_IFP, MT9V111_IFP_RA9_VZOOM,
  509. MT9V111_IFP_DECIMATION_FREEZE |
  510. MT9V111_PIXEL_ARRAY_HEIGHT);
  511. if (ret)
  512. return ret;
  513. ret = mt9v111_write(c, MT9V111_R01_IFP, MT9V111_IFP_RA7_HOUT,
  514. MT9V111_IFP_DECIMATION_FREEZE |
  515. mt9v111->fmt.width);
  516. if (ret)
  517. return ret;
  518. ret = mt9v111_write(c, MT9V111_R01_IFP, MT9V111_IFP_RAA_VOUT,
  519. mt9v111->fmt.height);
  520. if (ret)
  521. return ret;
  522. /* Apply controls to set auto exp, auto awb and timings */
  523. ret = v4l2_ctrl_handler_setup(&mt9v111->ctrls);
  524. if (ret)
  525. return ret;
  526. /*
  527. * Set pixel integration time to the whole frame time.
  528. * This value controls the shutter delay when running with AE
  529. * disabled. If longer than frame time, it affects the output
  530. * frame rate.
  531. */
  532. return mt9v111_write(c, MT9V111_R01_CORE, MT9V111_CORE_R09_PIXEL_INT,
  533. MT9V111_PIXEL_ARRAY_HEIGHT);
  534. }
  535. /* --- V4L2 subdev operations --- */
  536. static int mt9v111_s_power(struct v4l2_subdev *sd, int on)
  537. {
  538. struct mt9v111_dev *mt9v111 = sd_to_mt9v111(sd);
  539. int pwr_count;
  540. int ret = 0;
  541. mutex_lock(&mt9v111->pwr_mutex);
  542. /*
  543. * Make sure we're transitioning from 0 to 1, or viceversa,
  544. * before actually changing the power state.
  545. */
  546. pwr_count = mt9v111->pwr_count;
  547. pwr_count += on ? 1 : -1;
  548. if (pwr_count == !!on) {
  549. ret = on ? __mt9v111_power_on(sd) :
  550. __mt9v111_power_off(sd);
  551. if (!ret)
  552. /* All went well, updated power counter. */
  553. mt9v111->pwr_count = pwr_count;
  554. mutex_unlock(&mt9v111->pwr_mutex);
  555. return ret;
  556. }
  557. /*
  558. * Update power counter to keep track of how many nested calls we
  559. * received.
  560. */
  561. WARN_ON(pwr_count < 0 || pwr_count > 1);
  562. mt9v111->pwr_count = pwr_count;
  563. mutex_unlock(&mt9v111->pwr_mutex);
  564. return ret;
  565. }
  566. static int mt9v111_s_stream(struct v4l2_subdev *subdev, int enable)
  567. {
  568. struct mt9v111_dev *mt9v111 = sd_to_mt9v111(subdev);
  569. int ret;
  570. mutex_lock(&mt9v111->stream_mutex);
  571. if (mt9v111->streaming == enable) {
  572. mutex_unlock(&mt9v111->stream_mutex);
  573. return 0;
  574. }
  575. ret = mt9v111_s_power(subdev, enable);
  576. if (ret)
  577. goto error_unlock;
  578. if (enable && mt9v111->pending) {
  579. ret = mt9v111_hw_config(mt9v111);
  580. if (ret)
  581. goto error_unlock;
  582. /*
  583. * No need to update control here as far as only H/VBLANK are
  584. * supported and immediately programmed to registers in .s_ctrl
  585. */
  586. mt9v111->pending = false;
  587. }
  588. mt9v111->streaming = enable ? true : false;
  589. mutex_unlock(&mt9v111->stream_mutex);
  590. return 0;
  591. error_unlock:
  592. mutex_unlock(&mt9v111->stream_mutex);
  593. return ret;
  594. }
  595. static int mt9v111_set_frame_interval(struct v4l2_subdev *sd,
  596. struct v4l2_subdev_state *sd_state,
  597. struct v4l2_subdev_frame_interval *ival)
  598. {
  599. struct mt9v111_dev *mt9v111 = sd_to_mt9v111(sd);
  600. struct v4l2_fract *tpf = &ival->interval;
  601. unsigned int fps = tpf->numerator ?
  602. tpf->denominator / tpf->numerator :
  603. tpf->denominator;
  604. unsigned int max_fps;
  605. /*
  606. * FIXME: Implement support for V4L2_SUBDEV_FORMAT_TRY, using the V4L2
  607. * subdev active state API.
  608. */
  609. if (ival->which != V4L2_SUBDEV_FORMAT_ACTIVE)
  610. return -EINVAL;
  611. if (!tpf->numerator)
  612. tpf->numerator = 1;
  613. mutex_lock(&mt9v111->stream_mutex);
  614. if (mt9v111->streaming) {
  615. mutex_unlock(&mt9v111->stream_mutex);
  616. return -EBUSY;
  617. }
  618. if (mt9v111->fps == fps) {
  619. mutex_unlock(&mt9v111->stream_mutex);
  620. return 0;
  621. }
  622. /* Make sure frame rate/image sizes constraints are respected. */
  623. if (mt9v111->fmt.width < QVGA_WIDTH &&
  624. mt9v111->fmt.height < QVGA_HEIGHT)
  625. max_fps = 90;
  626. else if (mt9v111->fmt.width < CIF_WIDTH &&
  627. mt9v111->fmt.height < CIF_HEIGHT)
  628. max_fps = 60;
  629. else
  630. max_fps = mt9v111->sysclk <
  631. DIV_ROUND_CLOSEST(MT9V111_MAX_CLKIN, 2) ? 15 :
  632. 30;
  633. if (fps > max_fps) {
  634. mutex_unlock(&mt9v111->stream_mutex);
  635. return -EINVAL;
  636. }
  637. mt9v111_calc_frame_rate(mt9v111, tpf);
  638. mt9v111->fps = fps;
  639. mt9v111->pending = true;
  640. mutex_unlock(&mt9v111->stream_mutex);
  641. return 0;
  642. }
  643. static int mt9v111_get_frame_interval(struct v4l2_subdev *sd,
  644. struct v4l2_subdev_state *sd_state,
  645. struct v4l2_subdev_frame_interval *ival)
  646. {
  647. struct mt9v111_dev *mt9v111 = sd_to_mt9v111(sd);
  648. struct v4l2_fract *tpf = &ival->interval;
  649. /*
  650. * FIXME: Implement support for V4L2_SUBDEV_FORMAT_TRY, using the V4L2
  651. * subdev active state API.
  652. */
  653. if (ival->which != V4L2_SUBDEV_FORMAT_ACTIVE)
  654. return -EINVAL;
  655. mutex_lock(&mt9v111->stream_mutex);
  656. tpf->numerator = 1;
  657. tpf->denominator = mt9v111->fps;
  658. mutex_unlock(&mt9v111->stream_mutex);
  659. return 0;
  660. }
  661. static struct v4l2_mbus_framefmt *__mt9v111_get_pad_format(
  662. struct mt9v111_dev *mt9v111,
  663. struct v4l2_subdev_state *sd_state,
  664. unsigned int pad,
  665. enum v4l2_subdev_format_whence which)
  666. {
  667. switch (which) {
  668. case V4L2_SUBDEV_FORMAT_TRY:
  669. return v4l2_subdev_state_get_format(sd_state, pad);
  670. case V4L2_SUBDEV_FORMAT_ACTIVE:
  671. return &mt9v111->fmt;
  672. default:
  673. return NULL;
  674. }
  675. }
  676. static int mt9v111_enum_mbus_code(struct v4l2_subdev *subdev,
  677. struct v4l2_subdev_state *sd_state,
  678. struct v4l2_subdev_mbus_code_enum *code)
  679. {
  680. if (code->pad || code->index > ARRAY_SIZE(mt9v111_formats) - 1)
  681. return -EINVAL;
  682. code->code = mt9v111_formats[code->index].code;
  683. return 0;
  684. }
  685. static int mt9v111_enum_frame_interval(struct v4l2_subdev *sd,
  686. struct v4l2_subdev_state *sd_state,
  687. struct v4l2_subdev_frame_interval_enum *fie)
  688. {
  689. unsigned int i;
  690. if (fie->pad || fie->index >= ARRAY_SIZE(mt9v111_frame_intervals))
  691. return -EINVAL;
  692. for (i = 0; i < ARRAY_SIZE(mt9v111_frame_sizes); i++)
  693. if (fie->width == mt9v111_frame_sizes[i].width &&
  694. fie->height == mt9v111_frame_sizes[i].height)
  695. break;
  696. if (i == ARRAY_SIZE(mt9v111_frame_sizes))
  697. return -EINVAL;
  698. fie->interval.numerator = 1;
  699. fie->interval.denominator = mt9v111_frame_intervals[fie->index];
  700. return 0;
  701. }
  702. static int mt9v111_enum_frame_size(struct v4l2_subdev *subdev,
  703. struct v4l2_subdev_state *sd_state,
  704. struct v4l2_subdev_frame_size_enum *fse)
  705. {
  706. if (fse->pad || fse->index >= ARRAY_SIZE(mt9v111_frame_sizes))
  707. return -EINVAL;
  708. fse->min_width = mt9v111_frame_sizes[fse->index].width;
  709. fse->max_width = mt9v111_frame_sizes[fse->index].width;
  710. fse->min_height = mt9v111_frame_sizes[fse->index].height;
  711. fse->max_height = mt9v111_frame_sizes[fse->index].height;
  712. return 0;
  713. }
  714. static int mt9v111_get_format(struct v4l2_subdev *subdev,
  715. struct v4l2_subdev_state *sd_state,
  716. struct v4l2_subdev_format *format)
  717. {
  718. struct mt9v111_dev *mt9v111 = sd_to_mt9v111(subdev);
  719. if (format->pad)
  720. return -EINVAL;
  721. mutex_lock(&mt9v111->stream_mutex);
  722. format->format = *__mt9v111_get_pad_format(mt9v111, sd_state,
  723. format->pad,
  724. format->which);
  725. mutex_unlock(&mt9v111->stream_mutex);
  726. return 0;
  727. }
  728. static int mt9v111_set_format(struct v4l2_subdev *subdev,
  729. struct v4l2_subdev_state *sd_state,
  730. struct v4l2_subdev_format *format)
  731. {
  732. struct mt9v111_dev *mt9v111 = sd_to_mt9v111(subdev);
  733. struct v4l2_mbus_framefmt new_fmt;
  734. struct v4l2_mbus_framefmt *__fmt;
  735. unsigned int best_fit = ~0L;
  736. unsigned int idx = 0;
  737. unsigned int i;
  738. mutex_lock(&mt9v111->stream_mutex);
  739. if (mt9v111->streaming) {
  740. mutex_unlock(&mt9v111->stream_mutex);
  741. return -EBUSY;
  742. }
  743. if (format->pad) {
  744. mutex_unlock(&mt9v111->stream_mutex);
  745. return -EINVAL;
  746. }
  747. /* Update mbus format code and sizes. */
  748. for (i = 0; i < ARRAY_SIZE(mt9v111_formats); i++) {
  749. if (format->format.code == mt9v111_formats[i].code) {
  750. new_fmt.code = mt9v111_formats[i].code;
  751. break;
  752. }
  753. }
  754. if (i == ARRAY_SIZE(mt9v111_formats))
  755. new_fmt.code = mt9v111_formats[0].code;
  756. for (i = 0; i < ARRAY_SIZE(mt9v111_frame_sizes); i++) {
  757. unsigned int fit = abs(mt9v111_frame_sizes[i].width -
  758. format->format.width) +
  759. abs(mt9v111_frame_sizes[i].height -
  760. format->format.height);
  761. if (fit < best_fit) {
  762. best_fit = fit;
  763. idx = i;
  764. if (fit == 0)
  765. break;
  766. }
  767. }
  768. new_fmt.width = mt9v111_frame_sizes[idx].width;
  769. new_fmt.height = mt9v111_frame_sizes[idx].height;
  770. /* Update the device (or pad) format if it has changed. */
  771. __fmt = __mt9v111_get_pad_format(mt9v111, sd_state, format->pad,
  772. format->which);
  773. /* Format hasn't changed, stop here. */
  774. if (__fmt->code == new_fmt.code &&
  775. __fmt->width == new_fmt.width &&
  776. __fmt->height == new_fmt.height)
  777. goto done;
  778. /* Update the format and sizes, then mark changes as pending. */
  779. __fmt->code = new_fmt.code;
  780. __fmt->width = new_fmt.width;
  781. __fmt->height = new_fmt.height;
  782. if (format->which == V4L2_SUBDEV_FORMAT_ACTIVE)
  783. mt9v111->pending = true;
  784. dev_dbg(mt9v111->dev, "%s: mbus_code: %x - (%ux%u)\n",
  785. __func__, __fmt->code, __fmt->width, __fmt->height);
  786. done:
  787. format->format = *__fmt;
  788. mutex_unlock(&mt9v111->stream_mutex);
  789. return 0;
  790. }
  791. static int mt9v111_init_state(struct v4l2_subdev *subdev,
  792. struct v4l2_subdev_state *sd_state)
  793. {
  794. *v4l2_subdev_state_get_format(sd_state, 0) = mt9v111_def_fmt;
  795. return 0;
  796. }
  797. static const struct v4l2_subdev_core_ops mt9v111_core_ops = {
  798. .s_power = mt9v111_s_power,
  799. };
  800. static const struct v4l2_subdev_video_ops mt9v111_video_ops = {
  801. .s_stream = mt9v111_s_stream,
  802. };
  803. static const struct v4l2_subdev_pad_ops mt9v111_pad_ops = {
  804. .enum_mbus_code = mt9v111_enum_mbus_code,
  805. .enum_frame_size = mt9v111_enum_frame_size,
  806. .enum_frame_interval = mt9v111_enum_frame_interval,
  807. .get_fmt = mt9v111_get_format,
  808. .set_fmt = mt9v111_set_format,
  809. .get_frame_interval = mt9v111_get_frame_interval,
  810. .set_frame_interval = mt9v111_set_frame_interval,
  811. };
  812. static const struct v4l2_subdev_ops mt9v111_ops = {
  813. .core = &mt9v111_core_ops,
  814. .video = &mt9v111_video_ops,
  815. .pad = &mt9v111_pad_ops,
  816. };
  817. static const struct v4l2_subdev_internal_ops mt9v111_internal_ops = {
  818. .init_state = mt9v111_init_state,
  819. };
  820. static const struct media_entity_operations mt9v111_subdev_entity_ops = {
  821. .link_validate = v4l2_subdev_link_validate,
  822. };
  823. /* --- V4L2 ctrl --- */
  824. static int mt9v111_s_ctrl(struct v4l2_ctrl *ctrl)
  825. {
  826. struct mt9v111_dev *mt9v111 = container_of(ctrl->handler,
  827. struct mt9v111_dev,
  828. ctrls);
  829. int ret;
  830. mutex_lock(&mt9v111->pwr_mutex);
  831. /*
  832. * If sensor is powered down, just cache new control values,
  833. * no actual register access.
  834. */
  835. if (!mt9v111->pwr_count) {
  836. mt9v111->pending = true;
  837. mutex_unlock(&mt9v111->pwr_mutex);
  838. return 0;
  839. }
  840. mutex_unlock(&mt9v111->pwr_mutex);
  841. /*
  842. * Flickering control gets disabled if both auto exp and auto awb
  843. * are disabled too. If any of the two is enabled, enable it.
  844. *
  845. * Disabling flickering when ae and awb are off allows a more precise
  846. * control of the programmed frame rate.
  847. */
  848. if (mt9v111->auto_exp->is_new || mt9v111->auto_awb->is_new) {
  849. if (mt9v111->auto_exp->val == V4L2_EXPOSURE_MANUAL &&
  850. mt9v111->auto_awb->val == V4L2_WHITE_BALANCE_MANUAL)
  851. ret = mt9v111_update(mt9v111->client, MT9V111_R01_IFP,
  852. MT9V111_IFP_R08_OUTFMT_CTRL,
  853. MT9V111_IFP_R08_OUTFMT_CTRL_FLICKER,
  854. 0);
  855. else
  856. ret = mt9v111_update(mt9v111->client, MT9V111_R01_IFP,
  857. MT9V111_IFP_R08_OUTFMT_CTRL,
  858. MT9V111_IFP_R08_OUTFMT_CTRL_FLICKER,
  859. 1);
  860. if (ret)
  861. return ret;
  862. }
  863. ret = -EINVAL;
  864. switch (ctrl->id) {
  865. case V4L2_CID_AUTO_WHITE_BALANCE:
  866. ret = mt9v111_update(mt9v111->client, MT9V111_R01_IFP,
  867. MT9V111_IFP_R06_OPMODE_CTRL,
  868. MT9V111_IFP_R06_OPMODE_CTRL_AWB_EN,
  869. ctrl->val == V4L2_WHITE_BALANCE_AUTO ?
  870. MT9V111_IFP_R06_OPMODE_CTRL_AWB_EN : 0);
  871. break;
  872. case V4L2_CID_EXPOSURE_AUTO:
  873. ret = mt9v111_update(mt9v111->client, MT9V111_R01_IFP,
  874. MT9V111_IFP_R06_OPMODE_CTRL,
  875. MT9V111_IFP_R06_OPMODE_CTRL_AE_EN,
  876. ctrl->val == V4L2_EXPOSURE_AUTO ?
  877. MT9V111_IFP_R06_OPMODE_CTRL_AE_EN : 0);
  878. break;
  879. case V4L2_CID_HBLANK:
  880. ret = mt9v111_update(mt9v111->client, MT9V111_R01_CORE,
  881. MT9V111_CORE_R05_HBLANK,
  882. MT9V111_CORE_R05_MAX_HBLANK,
  883. mt9v111->hblank->val);
  884. break;
  885. case V4L2_CID_VBLANK:
  886. ret = mt9v111_update(mt9v111->client, MT9V111_R01_CORE,
  887. MT9V111_CORE_R06_VBLANK,
  888. MT9V111_CORE_R06_MAX_VBLANK,
  889. mt9v111->vblank->val);
  890. break;
  891. }
  892. return ret;
  893. }
  894. static const struct v4l2_ctrl_ops mt9v111_ctrl_ops = {
  895. .s_ctrl = mt9v111_s_ctrl,
  896. };
  897. static int mt9v111_chip_probe(struct mt9v111_dev *mt9v111)
  898. {
  899. int ret;
  900. u16 val;
  901. ret = __mt9v111_power_on(&mt9v111->sd);
  902. if (ret)
  903. return ret;
  904. ret = mt9v111_read(mt9v111->client, MT9V111_R01_CORE,
  905. MT9V111_CORE_RFF_CHIP_VER, &val);
  906. if (ret)
  907. goto power_off;
  908. if ((val >> 8) != MT9V111_CHIP_ID_HIGH &&
  909. (val & 0xff) != MT9V111_CHIP_ID_LOW) {
  910. dev_err(mt9v111->dev,
  911. "Unable to identify MT9V111 chip: 0x%2x%2x\n",
  912. val >> 8, val & 0xff);
  913. ret = -EIO;
  914. goto power_off;
  915. }
  916. dev_dbg(mt9v111->dev, "Chip identified: 0x%2x%2x\n",
  917. val >> 8, val & 0xff);
  918. power_off:
  919. __mt9v111_power_off(&mt9v111->sd);
  920. return ret;
  921. }
  922. static int mt9v111_probe(struct i2c_client *client)
  923. {
  924. struct mt9v111_dev *mt9v111;
  925. struct v4l2_fract tpf;
  926. int ret;
  927. mt9v111 = devm_kzalloc(&client->dev, sizeof(*mt9v111), GFP_KERNEL);
  928. if (!mt9v111)
  929. return -ENOMEM;
  930. mt9v111->dev = &client->dev;
  931. mt9v111->client = client;
  932. mt9v111->clk = devm_v4l2_sensor_clk_get(&client->dev, NULL);
  933. if (IS_ERR(mt9v111->clk))
  934. return dev_err_probe(&client->dev, PTR_ERR(mt9v111->clk),
  935. "failed to get the clock\n");
  936. mt9v111->sysclk = clk_get_rate(mt9v111->clk);
  937. if (mt9v111->sysclk > MT9V111_MAX_CLKIN)
  938. return -EINVAL;
  939. mt9v111->oe = devm_gpiod_get_optional(&client->dev, "enable",
  940. GPIOD_OUT_LOW);
  941. if (IS_ERR(mt9v111->oe)) {
  942. dev_err(&client->dev, "Unable to get GPIO \"enable\": %pe\n",
  943. mt9v111->oe);
  944. return PTR_ERR(mt9v111->oe);
  945. }
  946. mt9v111->standby = devm_gpiod_get_optional(&client->dev, "standby",
  947. GPIOD_OUT_HIGH);
  948. if (IS_ERR(mt9v111->standby)) {
  949. dev_err(&client->dev, "Unable to get GPIO \"standby\": %pe\n",
  950. mt9v111->standby);
  951. return PTR_ERR(mt9v111->standby);
  952. }
  953. mt9v111->reset = devm_gpiod_get_optional(&client->dev, "reset",
  954. GPIOD_OUT_LOW);
  955. if (IS_ERR(mt9v111->reset)) {
  956. dev_err(&client->dev, "Unable to get GPIO \"reset\": %pe\n",
  957. mt9v111->reset);
  958. return PTR_ERR(mt9v111->reset);
  959. }
  960. mutex_init(&mt9v111->pwr_mutex);
  961. mutex_init(&mt9v111->stream_mutex);
  962. v4l2_ctrl_handler_init(&mt9v111->ctrls, 5);
  963. mt9v111->auto_awb = v4l2_ctrl_new_std(&mt9v111->ctrls,
  964. &mt9v111_ctrl_ops,
  965. V4L2_CID_AUTO_WHITE_BALANCE,
  966. 0, 1, 1,
  967. V4L2_WHITE_BALANCE_AUTO);
  968. mt9v111->auto_exp = v4l2_ctrl_new_std_menu(&mt9v111->ctrls,
  969. &mt9v111_ctrl_ops,
  970. V4L2_CID_EXPOSURE_AUTO,
  971. V4L2_EXPOSURE_MANUAL,
  972. 0, V4L2_EXPOSURE_AUTO);
  973. mt9v111->hblank = v4l2_ctrl_new_std(&mt9v111->ctrls, &mt9v111_ctrl_ops,
  974. V4L2_CID_HBLANK,
  975. MT9V111_CORE_R05_MIN_HBLANK,
  976. MT9V111_CORE_R05_MAX_HBLANK, 1,
  977. MT9V111_CORE_R05_DEF_HBLANK);
  978. mt9v111->vblank = v4l2_ctrl_new_std(&mt9v111->ctrls, &mt9v111_ctrl_ops,
  979. V4L2_CID_VBLANK,
  980. MT9V111_CORE_R06_MIN_VBLANK,
  981. MT9V111_CORE_R06_MAX_VBLANK, 1,
  982. MT9V111_CORE_R06_DEF_VBLANK);
  983. /* PIXEL_RATE is fixed: just expose it to user space. */
  984. v4l2_ctrl_new_std(&mt9v111->ctrls, &mt9v111_ctrl_ops,
  985. V4L2_CID_PIXEL_RATE, 0,
  986. DIV_ROUND_CLOSEST(mt9v111->sysclk, 2), 1,
  987. DIV_ROUND_CLOSEST(mt9v111->sysclk, 2));
  988. if (mt9v111->ctrls.error) {
  989. ret = mt9v111->ctrls.error;
  990. goto error_free_ctrls;
  991. }
  992. mt9v111->sd.ctrl_handler = &mt9v111->ctrls;
  993. /* Start with default configuration: 640x480 UYVY. */
  994. mt9v111->fmt = mt9v111_def_fmt;
  995. /* Re-calculate blankings for 640x480@15fps. */
  996. mt9v111->fps = 15;
  997. tpf.numerator = 1;
  998. tpf.denominator = mt9v111->fps;
  999. mt9v111_calc_frame_rate(mt9v111, &tpf);
  1000. mt9v111->pwr_count = 0;
  1001. mt9v111->addr_space = MT9V111_R01_IFP;
  1002. mt9v111->pending = true;
  1003. v4l2_i2c_subdev_init(&mt9v111->sd, client, &mt9v111_ops);
  1004. mt9v111->sd.internal_ops = &mt9v111_internal_ops;
  1005. mt9v111->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
  1006. mt9v111->sd.entity.ops = &mt9v111_subdev_entity_ops;
  1007. mt9v111->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
  1008. mt9v111->pad.flags = MEDIA_PAD_FL_SOURCE;
  1009. ret = media_entity_pads_init(&mt9v111->sd.entity, 1, &mt9v111->pad);
  1010. if (ret)
  1011. goto error_free_entity;
  1012. ret = mt9v111_chip_probe(mt9v111);
  1013. if (ret)
  1014. goto error_free_entity;
  1015. ret = v4l2_async_register_subdev(&mt9v111->sd);
  1016. if (ret)
  1017. goto error_free_entity;
  1018. return 0;
  1019. error_free_entity:
  1020. media_entity_cleanup(&mt9v111->sd.entity);
  1021. error_free_ctrls:
  1022. v4l2_ctrl_handler_free(&mt9v111->ctrls);
  1023. mutex_destroy(&mt9v111->pwr_mutex);
  1024. mutex_destroy(&mt9v111->stream_mutex);
  1025. return ret;
  1026. }
  1027. static void mt9v111_remove(struct i2c_client *client)
  1028. {
  1029. struct v4l2_subdev *sd = i2c_get_clientdata(client);
  1030. struct mt9v111_dev *mt9v111 = sd_to_mt9v111(sd);
  1031. v4l2_async_unregister_subdev(sd);
  1032. media_entity_cleanup(&sd->entity);
  1033. v4l2_ctrl_handler_free(&mt9v111->ctrls);
  1034. mutex_destroy(&mt9v111->pwr_mutex);
  1035. mutex_destroy(&mt9v111->stream_mutex);
  1036. }
  1037. static const struct of_device_id mt9v111_of_match[] = {
  1038. { .compatible = "aptina,mt9v111", },
  1039. { /* sentinel */ }
  1040. };
  1041. MODULE_DEVICE_TABLE(of, mt9v111_of_match);
  1042. static struct i2c_driver mt9v111_driver = {
  1043. .driver = {
  1044. .name = "mt9v111",
  1045. .of_match_table = mt9v111_of_match,
  1046. },
  1047. .probe = mt9v111_probe,
  1048. .remove = mt9v111_remove,
  1049. };
  1050. module_i2c_driver(mt9v111_driver);
  1051. MODULE_DESCRIPTION("V4L2 sensor driver for Aptina MT9V111");
  1052. MODULE_AUTHOR("Jacopo Mondi <jacopo@jmondi.org>");
  1053. MODULE_LICENSE("GPL v2");