mt9v111.c 32 KB

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