mt9p031.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198
  1. /*
  2. * Driver for MT9P031 CMOS Image Sensor from Aptina
  3. *
  4. * Copyright (C) 2011, Laurent Pinchart <laurent.pinchart@ideasonboard.com>
  5. * Copyright (C) 2011, Javier Martin <javier.martin@vista-silicon.com>
  6. * Copyright (C) 2011, Guennadi Liakhovetski <g.liakhovetski@gmx.de>
  7. *
  8. * Based on the MT9V032 driver and Bastian Hecht's code.
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License version 2 as
  12. * published by the Free Software Foundation.
  13. */
  14. #include <linux/clk.h>
  15. #include <linux/delay.h>
  16. #include <linux/device.h>
  17. #include <linux/gpio/consumer.h>
  18. #include <linux/i2c.h>
  19. #include <linux/log2.h>
  20. #include <linux/module.h>
  21. #include <linux/of.h>
  22. #include <linux/of_graph.h>
  23. #include <linux/pm.h>
  24. #include <linux/regulator/consumer.h>
  25. #include <linux/slab.h>
  26. #include <linux/videodev2.h>
  27. #include <media/mt9p031.h>
  28. #include <media/v4l2-async.h>
  29. #include <media/v4l2-ctrls.h>
  30. #include <media/v4l2-device.h>
  31. #include <media/v4l2-subdev.h>
  32. #include "aptina-pll.h"
  33. #define MT9P031_PIXEL_ARRAY_WIDTH 2752
  34. #define MT9P031_PIXEL_ARRAY_HEIGHT 2004
  35. #define MT9P031_CHIP_VERSION 0x00
  36. #define MT9P031_CHIP_VERSION_VALUE 0x1801
  37. #define MT9P031_ROW_START 0x01
  38. #define MT9P031_ROW_START_MIN 0
  39. #define MT9P031_ROW_START_MAX 2004
  40. #define MT9P031_ROW_START_DEF 54
  41. #define MT9P031_COLUMN_START 0x02
  42. #define MT9P031_COLUMN_START_MIN 0
  43. #define MT9P031_COLUMN_START_MAX 2750
  44. #define MT9P031_COLUMN_START_DEF 16
  45. #define MT9P031_WINDOW_HEIGHT 0x03
  46. #define MT9P031_WINDOW_HEIGHT_MIN 2
  47. #define MT9P031_WINDOW_HEIGHT_MAX 2006
  48. #define MT9P031_WINDOW_HEIGHT_DEF 1944
  49. #define MT9P031_WINDOW_WIDTH 0x04
  50. #define MT9P031_WINDOW_WIDTH_MIN 2
  51. #define MT9P031_WINDOW_WIDTH_MAX 2752
  52. #define MT9P031_WINDOW_WIDTH_DEF 2592
  53. #define MT9P031_HORIZONTAL_BLANK 0x05
  54. #define MT9P031_HORIZONTAL_BLANK_MIN 0
  55. #define MT9P031_HORIZONTAL_BLANK_MAX 4095
  56. #define MT9P031_VERTICAL_BLANK 0x06
  57. #define MT9P031_VERTICAL_BLANK_MIN 1
  58. #define MT9P031_VERTICAL_BLANK_MAX 4096
  59. #define MT9P031_VERTICAL_BLANK_DEF 26
  60. #define MT9P031_OUTPUT_CONTROL 0x07
  61. #define MT9P031_OUTPUT_CONTROL_CEN 2
  62. #define MT9P031_OUTPUT_CONTROL_SYN 1
  63. #define MT9P031_OUTPUT_CONTROL_DEF 0x1f82
  64. #define MT9P031_SHUTTER_WIDTH_UPPER 0x08
  65. #define MT9P031_SHUTTER_WIDTH_LOWER 0x09
  66. #define MT9P031_SHUTTER_WIDTH_MIN 1
  67. #define MT9P031_SHUTTER_WIDTH_MAX 1048575
  68. #define MT9P031_SHUTTER_WIDTH_DEF 1943
  69. #define MT9P031_PLL_CONTROL 0x10
  70. #define MT9P031_PLL_CONTROL_PWROFF 0x0050
  71. #define MT9P031_PLL_CONTROL_PWRON 0x0051
  72. #define MT9P031_PLL_CONTROL_USEPLL 0x0052
  73. #define MT9P031_PLL_CONFIG_1 0x11
  74. #define MT9P031_PLL_CONFIG_2 0x12
  75. #define MT9P031_PIXEL_CLOCK_CONTROL 0x0a
  76. #define MT9P031_PIXEL_CLOCK_INVERT (1 << 15)
  77. #define MT9P031_PIXEL_CLOCK_SHIFT(n) ((n) << 8)
  78. #define MT9P031_PIXEL_CLOCK_DIVIDE(n) ((n) << 0)
  79. #define MT9P031_FRAME_RESTART 0x0b
  80. #define MT9P031_SHUTTER_DELAY 0x0c
  81. #define MT9P031_RST 0x0d
  82. #define MT9P031_RST_ENABLE 1
  83. #define MT9P031_RST_DISABLE 0
  84. #define MT9P031_READ_MODE_1 0x1e
  85. #define MT9P031_READ_MODE_2 0x20
  86. #define MT9P031_READ_MODE_2_ROW_MIR (1 << 15)
  87. #define MT9P031_READ_MODE_2_COL_MIR (1 << 14)
  88. #define MT9P031_READ_MODE_2_ROW_BLC (1 << 6)
  89. #define MT9P031_ROW_ADDRESS_MODE 0x22
  90. #define MT9P031_COLUMN_ADDRESS_MODE 0x23
  91. #define MT9P031_GLOBAL_GAIN 0x35
  92. #define MT9P031_GLOBAL_GAIN_MIN 8
  93. #define MT9P031_GLOBAL_GAIN_MAX 1024
  94. #define MT9P031_GLOBAL_GAIN_DEF 8
  95. #define MT9P031_GLOBAL_GAIN_MULT (1 << 6)
  96. #define MT9P031_ROW_BLACK_TARGET 0x49
  97. #define MT9P031_ROW_BLACK_DEF_OFFSET 0x4b
  98. #define MT9P031_GREEN1_OFFSET 0x60
  99. #define MT9P031_GREEN2_OFFSET 0x61
  100. #define MT9P031_BLACK_LEVEL_CALIBRATION 0x62
  101. #define MT9P031_BLC_MANUAL_BLC (1 << 0)
  102. #define MT9P031_RED_OFFSET 0x63
  103. #define MT9P031_BLUE_OFFSET 0x64
  104. #define MT9P031_TEST_PATTERN 0xa0
  105. #define MT9P031_TEST_PATTERN_SHIFT 3
  106. #define MT9P031_TEST_PATTERN_ENABLE (1 << 0)
  107. #define MT9P031_TEST_PATTERN_DISABLE (0 << 0)
  108. #define MT9P031_TEST_PATTERN_GREEN 0xa1
  109. #define MT9P031_TEST_PATTERN_RED 0xa2
  110. #define MT9P031_TEST_PATTERN_BLUE 0xa3
  111. enum mt9p031_model {
  112. MT9P031_MODEL_COLOR,
  113. MT9P031_MODEL_MONOCHROME,
  114. };
  115. struct mt9p031 {
  116. struct v4l2_subdev subdev;
  117. struct media_pad pad;
  118. struct v4l2_rect crop; /* Sensor window */
  119. struct v4l2_mbus_framefmt format;
  120. struct mt9p031_platform_data *pdata;
  121. struct mutex power_lock; /* lock to protect power_count */
  122. int power_count;
  123. struct clk *clk;
  124. struct regulator_bulk_data regulators[3];
  125. enum mt9p031_model model;
  126. struct aptina_pll pll;
  127. unsigned int clk_div;
  128. bool use_pll;
  129. struct gpio_desc *reset;
  130. struct v4l2_ctrl_handler ctrls;
  131. struct v4l2_ctrl *blc_auto;
  132. struct v4l2_ctrl *blc_offset;
  133. /* Registers cache */
  134. u16 output_control;
  135. u16 mode2;
  136. };
  137. static struct mt9p031 *to_mt9p031(struct v4l2_subdev *sd)
  138. {
  139. return container_of(sd, struct mt9p031, subdev);
  140. }
  141. static int mt9p031_read(struct i2c_client *client, u8 reg)
  142. {
  143. return i2c_smbus_read_word_swapped(client, reg);
  144. }
  145. static int mt9p031_write(struct i2c_client *client, u8 reg, u16 data)
  146. {
  147. return i2c_smbus_write_word_swapped(client, reg, data);
  148. }
  149. static int mt9p031_set_output_control(struct mt9p031 *mt9p031, u16 clear,
  150. u16 set)
  151. {
  152. struct i2c_client *client = v4l2_get_subdevdata(&mt9p031->subdev);
  153. u16 value = (mt9p031->output_control & ~clear) | set;
  154. int ret;
  155. ret = mt9p031_write(client, MT9P031_OUTPUT_CONTROL, value);
  156. if (ret < 0)
  157. return ret;
  158. mt9p031->output_control = value;
  159. return 0;
  160. }
  161. static int mt9p031_set_mode2(struct mt9p031 *mt9p031, u16 clear, u16 set)
  162. {
  163. struct i2c_client *client = v4l2_get_subdevdata(&mt9p031->subdev);
  164. u16 value = (mt9p031->mode2 & ~clear) | set;
  165. int ret;
  166. ret = mt9p031_write(client, MT9P031_READ_MODE_2, value);
  167. if (ret < 0)
  168. return ret;
  169. mt9p031->mode2 = value;
  170. return 0;
  171. }
  172. static int mt9p031_reset(struct mt9p031 *mt9p031)
  173. {
  174. struct i2c_client *client = v4l2_get_subdevdata(&mt9p031->subdev);
  175. int ret;
  176. /* Disable chip output, synchronous option update */
  177. ret = mt9p031_write(client, MT9P031_RST, MT9P031_RST_ENABLE);
  178. if (ret < 0)
  179. return ret;
  180. ret = mt9p031_write(client, MT9P031_RST, MT9P031_RST_DISABLE);
  181. if (ret < 0)
  182. return ret;
  183. ret = mt9p031_write(client, MT9P031_PIXEL_CLOCK_CONTROL,
  184. MT9P031_PIXEL_CLOCK_DIVIDE(mt9p031->clk_div));
  185. if (ret < 0)
  186. return ret;
  187. return mt9p031_set_output_control(mt9p031, MT9P031_OUTPUT_CONTROL_CEN,
  188. 0);
  189. }
  190. static int mt9p031_clk_setup(struct mt9p031 *mt9p031)
  191. {
  192. static const struct aptina_pll_limits limits = {
  193. .ext_clock_min = 6000000,
  194. .ext_clock_max = 27000000,
  195. .int_clock_min = 2000000,
  196. .int_clock_max = 13500000,
  197. .out_clock_min = 180000000,
  198. .out_clock_max = 360000000,
  199. .pix_clock_max = 96000000,
  200. .n_min = 1,
  201. .n_max = 64,
  202. .m_min = 16,
  203. .m_max = 255,
  204. .p1_min = 1,
  205. .p1_max = 128,
  206. };
  207. struct i2c_client *client = v4l2_get_subdevdata(&mt9p031->subdev);
  208. struct mt9p031_platform_data *pdata = mt9p031->pdata;
  209. int ret;
  210. mt9p031->clk = devm_clk_get(&client->dev, NULL);
  211. if (IS_ERR(mt9p031->clk))
  212. return PTR_ERR(mt9p031->clk);
  213. ret = clk_set_rate(mt9p031->clk, pdata->ext_freq);
  214. if (ret < 0)
  215. return ret;
  216. /* If the external clock frequency is out of bounds for the PLL use the
  217. * pixel clock divider only and disable the PLL.
  218. */
  219. if (pdata->ext_freq > limits.ext_clock_max) {
  220. unsigned int div;
  221. div = DIV_ROUND_UP(pdata->ext_freq, pdata->target_freq);
  222. div = roundup_pow_of_two(div) / 2;
  223. mt9p031->clk_div = min_t(unsigned int, div, 64);
  224. mt9p031->use_pll = false;
  225. return 0;
  226. }
  227. mt9p031->pll.ext_clock = pdata->ext_freq;
  228. mt9p031->pll.pix_clock = pdata->target_freq;
  229. mt9p031->use_pll = true;
  230. return aptina_pll_calculate(&client->dev, &limits, &mt9p031->pll);
  231. }
  232. static int mt9p031_pll_enable(struct mt9p031 *mt9p031)
  233. {
  234. struct i2c_client *client = v4l2_get_subdevdata(&mt9p031->subdev);
  235. int ret;
  236. if (!mt9p031->use_pll)
  237. return 0;
  238. ret = mt9p031_write(client, MT9P031_PLL_CONTROL,
  239. MT9P031_PLL_CONTROL_PWRON);
  240. if (ret < 0)
  241. return ret;
  242. ret = mt9p031_write(client, MT9P031_PLL_CONFIG_1,
  243. (mt9p031->pll.m << 8) | (mt9p031->pll.n - 1));
  244. if (ret < 0)
  245. return ret;
  246. ret = mt9p031_write(client, MT9P031_PLL_CONFIG_2, mt9p031->pll.p1 - 1);
  247. if (ret < 0)
  248. return ret;
  249. usleep_range(1000, 2000);
  250. ret = mt9p031_write(client, MT9P031_PLL_CONTROL,
  251. MT9P031_PLL_CONTROL_PWRON |
  252. MT9P031_PLL_CONTROL_USEPLL);
  253. return ret;
  254. }
  255. static inline int mt9p031_pll_disable(struct mt9p031 *mt9p031)
  256. {
  257. struct i2c_client *client = v4l2_get_subdevdata(&mt9p031->subdev);
  258. if (!mt9p031->use_pll)
  259. return 0;
  260. return mt9p031_write(client, MT9P031_PLL_CONTROL,
  261. MT9P031_PLL_CONTROL_PWROFF);
  262. }
  263. static int mt9p031_power_on(struct mt9p031 *mt9p031)
  264. {
  265. int ret;
  266. /* Ensure RESET_BAR is active */
  267. if (mt9p031->reset) {
  268. gpiod_set_value(mt9p031->reset, 1);
  269. usleep_range(1000, 2000);
  270. }
  271. /* Bring up the supplies */
  272. ret = regulator_bulk_enable(ARRAY_SIZE(mt9p031->regulators),
  273. mt9p031->regulators);
  274. if (ret < 0)
  275. return ret;
  276. /* Enable clock */
  277. if (mt9p031->clk) {
  278. ret = clk_prepare_enable(mt9p031->clk);
  279. if (ret) {
  280. regulator_bulk_disable(ARRAY_SIZE(mt9p031->regulators),
  281. mt9p031->regulators);
  282. return ret;
  283. }
  284. }
  285. /* Now RESET_BAR must be high */
  286. if (mt9p031->reset) {
  287. gpiod_set_value(mt9p031->reset, 0);
  288. usleep_range(1000, 2000);
  289. }
  290. return 0;
  291. }
  292. static void mt9p031_power_off(struct mt9p031 *mt9p031)
  293. {
  294. if (mt9p031->reset) {
  295. gpiod_set_value(mt9p031->reset, 1);
  296. usleep_range(1000, 2000);
  297. }
  298. regulator_bulk_disable(ARRAY_SIZE(mt9p031->regulators),
  299. mt9p031->regulators);
  300. if (mt9p031->clk)
  301. clk_disable_unprepare(mt9p031->clk);
  302. }
  303. static int __mt9p031_set_power(struct mt9p031 *mt9p031, bool on)
  304. {
  305. struct i2c_client *client = v4l2_get_subdevdata(&mt9p031->subdev);
  306. int ret;
  307. if (!on) {
  308. mt9p031_power_off(mt9p031);
  309. return 0;
  310. }
  311. ret = mt9p031_power_on(mt9p031);
  312. if (ret < 0)
  313. return ret;
  314. ret = mt9p031_reset(mt9p031);
  315. if (ret < 0) {
  316. dev_err(&client->dev, "Failed to reset the camera\n");
  317. return ret;
  318. }
  319. return v4l2_ctrl_handler_setup(&mt9p031->ctrls);
  320. }
  321. /* -----------------------------------------------------------------------------
  322. * V4L2 subdev video operations
  323. */
  324. static int mt9p031_set_params(struct mt9p031 *mt9p031)
  325. {
  326. struct i2c_client *client = v4l2_get_subdevdata(&mt9p031->subdev);
  327. struct v4l2_mbus_framefmt *format = &mt9p031->format;
  328. const struct v4l2_rect *crop = &mt9p031->crop;
  329. unsigned int hblank;
  330. unsigned int vblank;
  331. unsigned int xskip;
  332. unsigned int yskip;
  333. unsigned int xbin;
  334. unsigned int ybin;
  335. int ret;
  336. /* Windows position and size.
  337. *
  338. * TODO: Make sure the start coordinates and window size match the
  339. * skipping, binning and mirroring (see description of registers 2 and 4
  340. * in table 13, and Binning section on page 41).
  341. */
  342. ret = mt9p031_write(client, MT9P031_COLUMN_START, crop->left);
  343. if (ret < 0)
  344. return ret;
  345. ret = mt9p031_write(client, MT9P031_ROW_START, crop->top);
  346. if (ret < 0)
  347. return ret;
  348. ret = mt9p031_write(client, MT9P031_WINDOW_WIDTH, crop->width - 1);
  349. if (ret < 0)
  350. return ret;
  351. ret = mt9p031_write(client, MT9P031_WINDOW_HEIGHT, crop->height - 1);
  352. if (ret < 0)
  353. return ret;
  354. /* Row and column binning and skipping. Use the maximum binning value
  355. * compatible with the skipping settings.
  356. */
  357. xskip = DIV_ROUND_CLOSEST(crop->width, format->width);
  358. yskip = DIV_ROUND_CLOSEST(crop->height, format->height);
  359. xbin = 1 << (ffs(xskip) - 1);
  360. ybin = 1 << (ffs(yskip) - 1);
  361. ret = mt9p031_write(client, MT9P031_COLUMN_ADDRESS_MODE,
  362. ((xbin - 1) << 4) | (xskip - 1));
  363. if (ret < 0)
  364. return ret;
  365. ret = mt9p031_write(client, MT9P031_ROW_ADDRESS_MODE,
  366. ((ybin - 1) << 4) | (yskip - 1));
  367. if (ret < 0)
  368. return ret;
  369. /* Blanking - use minimum value for horizontal blanking and default
  370. * value for vertical blanking.
  371. */
  372. hblank = 346 * ybin + 64 + (80 >> min_t(unsigned int, xbin, 3));
  373. vblank = MT9P031_VERTICAL_BLANK_DEF;
  374. ret = mt9p031_write(client, MT9P031_HORIZONTAL_BLANK, hblank - 1);
  375. if (ret < 0)
  376. return ret;
  377. ret = mt9p031_write(client, MT9P031_VERTICAL_BLANK, vblank - 1);
  378. if (ret < 0)
  379. return ret;
  380. return ret;
  381. }
  382. static int mt9p031_s_stream(struct v4l2_subdev *subdev, int enable)
  383. {
  384. struct mt9p031 *mt9p031 = to_mt9p031(subdev);
  385. int ret;
  386. if (!enable) {
  387. /* Stop sensor readout */
  388. ret = mt9p031_set_output_control(mt9p031,
  389. MT9P031_OUTPUT_CONTROL_CEN, 0);
  390. if (ret < 0)
  391. return ret;
  392. return mt9p031_pll_disable(mt9p031);
  393. }
  394. ret = mt9p031_set_params(mt9p031);
  395. if (ret < 0)
  396. return ret;
  397. /* Switch to master "normal" mode */
  398. ret = mt9p031_set_output_control(mt9p031, 0,
  399. MT9P031_OUTPUT_CONTROL_CEN);
  400. if (ret < 0)
  401. return ret;
  402. return mt9p031_pll_enable(mt9p031);
  403. }
  404. static int mt9p031_enum_mbus_code(struct v4l2_subdev *subdev,
  405. struct v4l2_subdev_pad_config *cfg,
  406. struct v4l2_subdev_mbus_code_enum *code)
  407. {
  408. struct mt9p031 *mt9p031 = to_mt9p031(subdev);
  409. if (code->pad || code->index)
  410. return -EINVAL;
  411. code->code = mt9p031->format.code;
  412. return 0;
  413. }
  414. static int mt9p031_enum_frame_size(struct v4l2_subdev *subdev,
  415. struct v4l2_subdev_pad_config *cfg,
  416. struct v4l2_subdev_frame_size_enum *fse)
  417. {
  418. struct mt9p031 *mt9p031 = to_mt9p031(subdev);
  419. if (fse->index >= 8 || fse->code != mt9p031->format.code)
  420. return -EINVAL;
  421. fse->min_width = MT9P031_WINDOW_WIDTH_DEF
  422. / min_t(unsigned int, 7, fse->index + 1);
  423. fse->max_width = fse->min_width;
  424. fse->min_height = MT9P031_WINDOW_HEIGHT_DEF / (fse->index + 1);
  425. fse->max_height = fse->min_height;
  426. return 0;
  427. }
  428. static struct v4l2_mbus_framefmt *
  429. __mt9p031_get_pad_format(struct mt9p031 *mt9p031, struct v4l2_subdev_pad_config *cfg,
  430. unsigned int pad, u32 which)
  431. {
  432. switch (which) {
  433. case V4L2_SUBDEV_FORMAT_TRY:
  434. return v4l2_subdev_get_try_format(&mt9p031->subdev, cfg, pad);
  435. case V4L2_SUBDEV_FORMAT_ACTIVE:
  436. return &mt9p031->format;
  437. default:
  438. return NULL;
  439. }
  440. }
  441. static struct v4l2_rect *
  442. __mt9p031_get_pad_crop(struct mt9p031 *mt9p031, struct v4l2_subdev_pad_config *cfg,
  443. unsigned int pad, u32 which)
  444. {
  445. switch (which) {
  446. case V4L2_SUBDEV_FORMAT_TRY:
  447. return v4l2_subdev_get_try_crop(&mt9p031->subdev, cfg, pad);
  448. case V4L2_SUBDEV_FORMAT_ACTIVE:
  449. return &mt9p031->crop;
  450. default:
  451. return NULL;
  452. }
  453. }
  454. static int mt9p031_get_format(struct v4l2_subdev *subdev,
  455. struct v4l2_subdev_pad_config *cfg,
  456. struct v4l2_subdev_format *fmt)
  457. {
  458. struct mt9p031 *mt9p031 = to_mt9p031(subdev);
  459. fmt->format = *__mt9p031_get_pad_format(mt9p031, cfg, fmt->pad,
  460. fmt->which);
  461. return 0;
  462. }
  463. static int mt9p031_set_format(struct v4l2_subdev *subdev,
  464. struct v4l2_subdev_pad_config *cfg,
  465. struct v4l2_subdev_format *format)
  466. {
  467. struct mt9p031 *mt9p031 = to_mt9p031(subdev);
  468. struct v4l2_mbus_framefmt *__format;
  469. struct v4l2_rect *__crop;
  470. unsigned int width;
  471. unsigned int height;
  472. unsigned int hratio;
  473. unsigned int vratio;
  474. __crop = __mt9p031_get_pad_crop(mt9p031, cfg, format->pad,
  475. format->which);
  476. /* Clamp the width and height to avoid dividing by zero. */
  477. width = clamp_t(unsigned int, ALIGN(format->format.width, 2),
  478. max_t(unsigned int, __crop->width / 7,
  479. MT9P031_WINDOW_WIDTH_MIN),
  480. __crop->width);
  481. height = clamp_t(unsigned int, ALIGN(format->format.height, 2),
  482. max_t(unsigned int, __crop->height / 8,
  483. MT9P031_WINDOW_HEIGHT_MIN),
  484. __crop->height);
  485. hratio = DIV_ROUND_CLOSEST(__crop->width, width);
  486. vratio = DIV_ROUND_CLOSEST(__crop->height, height);
  487. __format = __mt9p031_get_pad_format(mt9p031, cfg, format->pad,
  488. format->which);
  489. __format->width = __crop->width / hratio;
  490. __format->height = __crop->height / vratio;
  491. format->format = *__format;
  492. return 0;
  493. }
  494. static int mt9p031_get_selection(struct v4l2_subdev *subdev,
  495. struct v4l2_subdev_pad_config *cfg,
  496. struct v4l2_subdev_selection *sel)
  497. {
  498. struct mt9p031 *mt9p031 = to_mt9p031(subdev);
  499. if (sel->target != V4L2_SEL_TGT_CROP)
  500. return -EINVAL;
  501. sel->r = *__mt9p031_get_pad_crop(mt9p031, cfg, sel->pad, sel->which);
  502. return 0;
  503. }
  504. static int mt9p031_set_selection(struct v4l2_subdev *subdev,
  505. struct v4l2_subdev_pad_config *cfg,
  506. struct v4l2_subdev_selection *sel)
  507. {
  508. struct mt9p031 *mt9p031 = to_mt9p031(subdev);
  509. struct v4l2_mbus_framefmt *__format;
  510. struct v4l2_rect *__crop;
  511. struct v4l2_rect rect;
  512. if (sel->target != V4L2_SEL_TGT_CROP)
  513. return -EINVAL;
  514. /* Clamp the crop rectangle boundaries and align them to a multiple of 2
  515. * pixels to ensure a GRBG Bayer pattern.
  516. */
  517. rect.left = clamp(ALIGN(sel->r.left, 2), MT9P031_COLUMN_START_MIN,
  518. MT9P031_COLUMN_START_MAX);
  519. rect.top = clamp(ALIGN(sel->r.top, 2), MT9P031_ROW_START_MIN,
  520. MT9P031_ROW_START_MAX);
  521. rect.width = clamp_t(unsigned int, ALIGN(sel->r.width, 2),
  522. MT9P031_WINDOW_WIDTH_MIN,
  523. MT9P031_WINDOW_WIDTH_MAX);
  524. rect.height = clamp_t(unsigned int, ALIGN(sel->r.height, 2),
  525. MT9P031_WINDOW_HEIGHT_MIN,
  526. MT9P031_WINDOW_HEIGHT_MAX);
  527. rect.width = min_t(unsigned int, rect.width,
  528. MT9P031_PIXEL_ARRAY_WIDTH - rect.left);
  529. rect.height = min_t(unsigned int, rect.height,
  530. MT9P031_PIXEL_ARRAY_HEIGHT - rect.top);
  531. __crop = __mt9p031_get_pad_crop(mt9p031, cfg, sel->pad, sel->which);
  532. if (rect.width != __crop->width || rect.height != __crop->height) {
  533. /* Reset the output image size if the crop rectangle size has
  534. * been modified.
  535. */
  536. __format = __mt9p031_get_pad_format(mt9p031, cfg, sel->pad,
  537. sel->which);
  538. __format->width = rect.width;
  539. __format->height = rect.height;
  540. }
  541. *__crop = rect;
  542. sel->r = rect;
  543. return 0;
  544. }
  545. /* -----------------------------------------------------------------------------
  546. * V4L2 subdev control operations
  547. */
  548. #define V4L2_CID_BLC_AUTO (V4L2_CID_USER_BASE | 0x1002)
  549. #define V4L2_CID_BLC_TARGET_LEVEL (V4L2_CID_USER_BASE | 0x1003)
  550. #define V4L2_CID_BLC_ANALOG_OFFSET (V4L2_CID_USER_BASE | 0x1004)
  551. #define V4L2_CID_BLC_DIGITAL_OFFSET (V4L2_CID_USER_BASE | 0x1005)
  552. static int mt9p031_restore_blc(struct mt9p031 *mt9p031)
  553. {
  554. struct i2c_client *client = v4l2_get_subdevdata(&mt9p031->subdev);
  555. int ret;
  556. if (mt9p031->blc_auto->cur.val != 0) {
  557. ret = mt9p031_set_mode2(mt9p031, 0,
  558. MT9P031_READ_MODE_2_ROW_BLC);
  559. if (ret < 0)
  560. return ret;
  561. }
  562. if (mt9p031->blc_offset->cur.val != 0) {
  563. ret = mt9p031_write(client, MT9P031_ROW_BLACK_TARGET,
  564. mt9p031->blc_offset->cur.val);
  565. if (ret < 0)
  566. return ret;
  567. }
  568. return 0;
  569. }
  570. static int mt9p031_s_ctrl(struct v4l2_ctrl *ctrl)
  571. {
  572. struct mt9p031 *mt9p031 =
  573. container_of(ctrl->handler, struct mt9p031, ctrls);
  574. struct i2c_client *client = v4l2_get_subdevdata(&mt9p031->subdev);
  575. u16 data;
  576. int ret;
  577. if (ctrl->flags & V4L2_CTRL_FLAG_INACTIVE)
  578. return 0;
  579. switch (ctrl->id) {
  580. case V4L2_CID_EXPOSURE:
  581. ret = mt9p031_write(client, MT9P031_SHUTTER_WIDTH_UPPER,
  582. (ctrl->val >> 16) & 0xffff);
  583. if (ret < 0)
  584. return ret;
  585. return mt9p031_write(client, MT9P031_SHUTTER_WIDTH_LOWER,
  586. ctrl->val & 0xffff);
  587. case V4L2_CID_GAIN:
  588. /* Gain is controlled by 2 analog stages and a digital stage.
  589. * Valid values for the 3 stages are
  590. *
  591. * Stage Min Max Step
  592. * ------------------------------------------
  593. * First analog stage x1 x2 1
  594. * Second analog stage x1 x4 0.125
  595. * Digital stage x1 x16 0.125
  596. *
  597. * To minimize noise, the gain stages should be used in the
  598. * second analog stage, first analog stage, digital stage order.
  599. * Gain from a previous stage should be pushed to its maximum
  600. * value before the next stage is used.
  601. */
  602. if (ctrl->val <= 32) {
  603. data = ctrl->val;
  604. } else if (ctrl->val <= 64) {
  605. ctrl->val &= ~1;
  606. data = (1 << 6) | (ctrl->val >> 1);
  607. } else {
  608. ctrl->val &= ~7;
  609. data = ((ctrl->val - 64) << 5) | (1 << 6) | 32;
  610. }
  611. return mt9p031_write(client, MT9P031_GLOBAL_GAIN, data);
  612. case V4L2_CID_HFLIP:
  613. if (ctrl->val)
  614. return mt9p031_set_mode2(mt9p031,
  615. 0, MT9P031_READ_MODE_2_COL_MIR);
  616. else
  617. return mt9p031_set_mode2(mt9p031,
  618. MT9P031_READ_MODE_2_COL_MIR, 0);
  619. case V4L2_CID_VFLIP:
  620. if (ctrl->val)
  621. return mt9p031_set_mode2(mt9p031,
  622. 0, MT9P031_READ_MODE_2_ROW_MIR);
  623. else
  624. return mt9p031_set_mode2(mt9p031,
  625. MT9P031_READ_MODE_2_ROW_MIR, 0);
  626. case V4L2_CID_TEST_PATTERN:
  627. /* The digital side of the Black Level Calibration function must
  628. * be disabled when generating a test pattern to avoid artifacts
  629. * in the image. Activate (deactivate) the BLC-related controls
  630. * when the test pattern is enabled (disabled).
  631. */
  632. v4l2_ctrl_activate(mt9p031->blc_auto, ctrl->val == 0);
  633. v4l2_ctrl_activate(mt9p031->blc_offset, ctrl->val == 0);
  634. if (!ctrl->val) {
  635. /* Restore the BLC settings. */
  636. ret = mt9p031_restore_blc(mt9p031);
  637. if (ret < 0)
  638. return ret;
  639. return mt9p031_write(client, MT9P031_TEST_PATTERN,
  640. MT9P031_TEST_PATTERN_DISABLE);
  641. }
  642. ret = mt9p031_write(client, MT9P031_TEST_PATTERN_GREEN, 0x05a0);
  643. if (ret < 0)
  644. return ret;
  645. ret = mt9p031_write(client, MT9P031_TEST_PATTERN_RED, 0x0a50);
  646. if (ret < 0)
  647. return ret;
  648. ret = mt9p031_write(client, MT9P031_TEST_PATTERN_BLUE, 0x0aa0);
  649. if (ret < 0)
  650. return ret;
  651. /* Disable digital BLC when generating a test pattern. */
  652. ret = mt9p031_set_mode2(mt9p031, MT9P031_READ_MODE_2_ROW_BLC,
  653. 0);
  654. if (ret < 0)
  655. return ret;
  656. ret = mt9p031_write(client, MT9P031_ROW_BLACK_DEF_OFFSET, 0);
  657. if (ret < 0)
  658. return ret;
  659. return mt9p031_write(client, MT9P031_TEST_PATTERN,
  660. ((ctrl->val - 1) << MT9P031_TEST_PATTERN_SHIFT)
  661. | MT9P031_TEST_PATTERN_ENABLE);
  662. case V4L2_CID_BLC_AUTO:
  663. ret = mt9p031_set_mode2(mt9p031,
  664. ctrl->val ? 0 : MT9P031_READ_MODE_2_ROW_BLC,
  665. ctrl->val ? MT9P031_READ_MODE_2_ROW_BLC : 0);
  666. if (ret < 0)
  667. return ret;
  668. return mt9p031_write(client, MT9P031_BLACK_LEVEL_CALIBRATION,
  669. ctrl->val ? 0 : MT9P031_BLC_MANUAL_BLC);
  670. case V4L2_CID_BLC_TARGET_LEVEL:
  671. return mt9p031_write(client, MT9P031_ROW_BLACK_TARGET,
  672. ctrl->val);
  673. case V4L2_CID_BLC_ANALOG_OFFSET:
  674. data = ctrl->val & ((1 << 9) - 1);
  675. ret = mt9p031_write(client, MT9P031_GREEN1_OFFSET, data);
  676. if (ret < 0)
  677. return ret;
  678. ret = mt9p031_write(client, MT9P031_GREEN2_OFFSET, data);
  679. if (ret < 0)
  680. return ret;
  681. ret = mt9p031_write(client, MT9P031_RED_OFFSET, data);
  682. if (ret < 0)
  683. return ret;
  684. return mt9p031_write(client, MT9P031_BLUE_OFFSET, data);
  685. case V4L2_CID_BLC_DIGITAL_OFFSET:
  686. return mt9p031_write(client, MT9P031_ROW_BLACK_DEF_OFFSET,
  687. ctrl->val & ((1 << 12) - 1));
  688. }
  689. return 0;
  690. }
  691. static struct v4l2_ctrl_ops mt9p031_ctrl_ops = {
  692. .s_ctrl = mt9p031_s_ctrl,
  693. };
  694. static const char * const mt9p031_test_pattern_menu[] = {
  695. "Disabled",
  696. "Color Field",
  697. "Horizontal Gradient",
  698. "Vertical Gradient",
  699. "Diagonal Gradient",
  700. "Classic Test Pattern",
  701. "Walking 1s",
  702. "Monochrome Horizontal Bars",
  703. "Monochrome Vertical Bars",
  704. "Vertical Color Bars",
  705. };
  706. static const struct v4l2_ctrl_config mt9p031_ctrls[] = {
  707. {
  708. .ops = &mt9p031_ctrl_ops,
  709. .id = V4L2_CID_BLC_AUTO,
  710. .type = V4L2_CTRL_TYPE_BOOLEAN,
  711. .name = "BLC, Auto",
  712. .min = 0,
  713. .max = 1,
  714. .step = 1,
  715. .def = 1,
  716. .flags = 0,
  717. }, {
  718. .ops = &mt9p031_ctrl_ops,
  719. .id = V4L2_CID_BLC_TARGET_LEVEL,
  720. .type = V4L2_CTRL_TYPE_INTEGER,
  721. .name = "BLC Target Level",
  722. .min = 0,
  723. .max = 4095,
  724. .step = 1,
  725. .def = 168,
  726. .flags = 0,
  727. }, {
  728. .ops = &mt9p031_ctrl_ops,
  729. .id = V4L2_CID_BLC_ANALOG_OFFSET,
  730. .type = V4L2_CTRL_TYPE_INTEGER,
  731. .name = "BLC Analog Offset",
  732. .min = -255,
  733. .max = 255,
  734. .step = 1,
  735. .def = 32,
  736. .flags = 0,
  737. }, {
  738. .ops = &mt9p031_ctrl_ops,
  739. .id = V4L2_CID_BLC_DIGITAL_OFFSET,
  740. .type = V4L2_CTRL_TYPE_INTEGER,
  741. .name = "BLC Digital Offset",
  742. .min = -2048,
  743. .max = 2047,
  744. .step = 1,
  745. .def = 40,
  746. .flags = 0,
  747. }
  748. };
  749. /* -----------------------------------------------------------------------------
  750. * V4L2 subdev core operations
  751. */
  752. static int mt9p031_set_power(struct v4l2_subdev *subdev, int on)
  753. {
  754. struct mt9p031 *mt9p031 = to_mt9p031(subdev);
  755. int ret = 0;
  756. mutex_lock(&mt9p031->power_lock);
  757. /* If the power count is modified from 0 to != 0 or from != 0 to 0,
  758. * update the power state.
  759. */
  760. if (mt9p031->power_count == !on) {
  761. ret = __mt9p031_set_power(mt9p031, !!on);
  762. if (ret < 0)
  763. goto out;
  764. }
  765. /* Update the power count. */
  766. mt9p031->power_count += on ? 1 : -1;
  767. WARN_ON(mt9p031->power_count < 0);
  768. out:
  769. mutex_unlock(&mt9p031->power_lock);
  770. return ret;
  771. }
  772. /* -----------------------------------------------------------------------------
  773. * V4L2 subdev internal operations
  774. */
  775. static int mt9p031_registered(struct v4l2_subdev *subdev)
  776. {
  777. struct i2c_client *client = v4l2_get_subdevdata(subdev);
  778. struct mt9p031 *mt9p031 = to_mt9p031(subdev);
  779. s32 data;
  780. int ret;
  781. ret = mt9p031_power_on(mt9p031);
  782. if (ret < 0) {
  783. dev_err(&client->dev, "MT9P031 power up failed\n");
  784. return ret;
  785. }
  786. /* Read out the chip version register */
  787. data = mt9p031_read(client, MT9P031_CHIP_VERSION);
  788. mt9p031_power_off(mt9p031);
  789. if (data != MT9P031_CHIP_VERSION_VALUE) {
  790. dev_err(&client->dev, "MT9P031 not detected, wrong version "
  791. "0x%04x\n", data);
  792. return -ENODEV;
  793. }
  794. dev_info(&client->dev, "MT9P031 detected at address 0x%02x\n",
  795. client->addr);
  796. return 0;
  797. }
  798. static int mt9p031_open(struct v4l2_subdev *subdev, struct v4l2_subdev_fh *fh)
  799. {
  800. struct mt9p031 *mt9p031 = to_mt9p031(subdev);
  801. struct v4l2_mbus_framefmt *format;
  802. struct v4l2_rect *crop;
  803. crop = v4l2_subdev_get_try_crop(subdev, fh->pad, 0);
  804. crop->left = MT9P031_COLUMN_START_DEF;
  805. crop->top = MT9P031_ROW_START_DEF;
  806. crop->width = MT9P031_WINDOW_WIDTH_DEF;
  807. crop->height = MT9P031_WINDOW_HEIGHT_DEF;
  808. format = v4l2_subdev_get_try_format(subdev, fh->pad, 0);
  809. if (mt9p031->model == MT9P031_MODEL_MONOCHROME)
  810. format->code = MEDIA_BUS_FMT_Y12_1X12;
  811. else
  812. format->code = MEDIA_BUS_FMT_SGRBG12_1X12;
  813. format->width = MT9P031_WINDOW_WIDTH_DEF;
  814. format->height = MT9P031_WINDOW_HEIGHT_DEF;
  815. format->field = V4L2_FIELD_NONE;
  816. format->colorspace = V4L2_COLORSPACE_SRGB;
  817. return mt9p031_set_power(subdev, 1);
  818. }
  819. static int mt9p031_close(struct v4l2_subdev *subdev, struct v4l2_subdev_fh *fh)
  820. {
  821. return mt9p031_set_power(subdev, 0);
  822. }
  823. static struct v4l2_subdev_core_ops mt9p031_subdev_core_ops = {
  824. .s_power = mt9p031_set_power,
  825. };
  826. static struct v4l2_subdev_video_ops mt9p031_subdev_video_ops = {
  827. .s_stream = mt9p031_s_stream,
  828. };
  829. static struct v4l2_subdev_pad_ops mt9p031_subdev_pad_ops = {
  830. .enum_mbus_code = mt9p031_enum_mbus_code,
  831. .enum_frame_size = mt9p031_enum_frame_size,
  832. .get_fmt = mt9p031_get_format,
  833. .set_fmt = mt9p031_set_format,
  834. .get_selection = mt9p031_get_selection,
  835. .set_selection = mt9p031_set_selection,
  836. };
  837. static struct v4l2_subdev_ops mt9p031_subdev_ops = {
  838. .core = &mt9p031_subdev_core_ops,
  839. .video = &mt9p031_subdev_video_ops,
  840. .pad = &mt9p031_subdev_pad_ops,
  841. };
  842. static const struct v4l2_subdev_internal_ops mt9p031_subdev_internal_ops = {
  843. .registered = mt9p031_registered,
  844. .open = mt9p031_open,
  845. .close = mt9p031_close,
  846. };
  847. /* -----------------------------------------------------------------------------
  848. * Driver initialization and probing
  849. */
  850. static struct mt9p031_platform_data *
  851. mt9p031_get_pdata(struct i2c_client *client)
  852. {
  853. struct mt9p031_platform_data *pdata;
  854. struct device_node *np;
  855. if (!IS_ENABLED(CONFIG_OF) || !client->dev.of_node)
  856. return client->dev.platform_data;
  857. np = of_graph_get_next_endpoint(client->dev.of_node, NULL);
  858. if (!np)
  859. return NULL;
  860. pdata = devm_kzalloc(&client->dev, sizeof(*pdata), GFP_KERNEL);
  861. if (!pdata)
  862. goto done;
  863. of_property_read_u32(np, "input-clock-frequency", &pdata->ext_freq);
  864. of_property_read_u32(np, "pixel-clock-frequency", &pdata->target_freq);
  865. done:
  866. of_node_put(np);
  867. return pdata;
  868. }
  869. static int mt9p031_probe(struct i2c_client *client,
  870. const struct i2c_device_id *did)
  871. {
  872. struct mt9p031_platform_data *pdata = mt9p031_get_pdata(client);
  873. struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
  874. struct mt9p031 *mt9p031;
  875. unsigned int i;
  876. int ret;
  877. if (pdata == NULL) {
  878. dev_err(&client->dev, "No platform data\n");
  879. return -EINVAL;
  880. }
  881. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WORD_DATA)) {
  882. dev_warn(&client->dev,
  883. "I2C-Adapter doesn't support I2C_FUNC_SMBUS_WORD\n");
  884. return -EIO;
  885. }
  886. mt9p031 = devm_kzalloc(&client->dev, sizeof(*mt9p031), GFP_KERNEL);
  887. if (mt9p031 == NULL)
  888. return -ENOMEM;
  889. mt9p031->pdata = pdata;
  890. mt9p031->output_control = MT9P031_OUTPUT_CONTROL_DEF;
  891. mt9p031->mode2 = MT9P031_READ_MODE_2_ROW_BLC;
  892. mt9p031->model = did->driver_data;
  893. mt9p031->regulators[0].supply = "vdd";
  894. mt9p031->regulators[1].supply = "vdd_io";
  895. mt9p031->regulators[2].supply = "vaa";
  896. ret = devm_regulator_bulk_get(&client->dev, 3, mt9p031->regulators);
  897. if (ret < 0) {
  898. dev_err(&client->dev, "Unable to get regulators\n");
  899. return ret;
  900. }
  901. mutex_init(&mt9p031->power_lock);
  902. v4l2_ctrl_handler_init(&mt9p031->ctrls, ARRAY_SIZE(mt9p031_ctrls) + 6);
  903. v4l2_ctrl_new_std(&mt9p031->ctrls, &mt9p031_ctrl_ops,
  904. V4L2_CID_EXPOSURE, MT9P031_SHUTTER_WIDTH_MIN,
  905. MT9P031_SHUTTER_WIDTH_MAX, 1,
  906. MT9P031_SHUTTER_WIDTH_DEF);
  907. v4l2_ctrl_new_std(&mt9p031->ctrls, &mt9p031_ctrl_ops,
  908. V4L2_CID_GAIN, MT9P031_GLOBAL_GAIN_MIN,
  909. MT9P031_GLOBAL_GAIN_MAX, 1, MT9P031_GLOBAL_GAIN_DEF);
  910. v4l2_ctrl_new_std(&mt9p031->ctrls, &mt9p031_ctrl_ops,
  911. V4L2_CID_HFLIP, 0, 1, 1, 0);
  912. v4l2_ctrl_new_std(&mt9p031->ctrls, &mt9p031_ctrl_ops,
  913. V4L2_CID_VFLIP, 0, 1, 1, 0);
  914. v4l2_ctrl_new_std(&mt9p031->ctrls, &mt9p031_ctrl_ops,
  915. V4L2_CID_PIXEL_RATE, pdata->target_freq,
  916. pdata->target_freq, 1, pdata->target_freq);
  917. v4l2_ctrl_new_std_menu_items(&mt9p031->ctrls, &mt9p031_ctrl_ops,
  918. V4L2_CID_TEST_PATTERN,
  919. ARRAY_SIZE(mt9p031_test_pattern_menu) - 1, 0,
  920. 0, mt9p031_test_pattern_menu);
  921. for (i = 0; i < ARRAY_SIZE(mt9p031_ctrls); ++i)
  922. v4l2_ctrl_new_custom(&mt9p031->ctrls, &mt9p031_ctrls[i], NULL);
  923. mt9p031->subdev.ctrl_handler = &mt9p031->ctrls;
  924. if (mt9p031->ctrls.error) {
  925. printk(KERN_INFO "%s: control initialization error %d\n",
  926. __func__, mt9p031->ctrls.error);
  927. ret = mt9p031->ctrls.error;
  928. goto done;
  929. }
  930. mt9p031->blc_auto = v4l2_ctrl_find(&mt9p031->ctrls, V4L2_CID_BLC_AUTO);
  931. mt9p031->blc_offset = v4l2_ctrl_find(&mt9p031->ctrls,
  932. V4L2_CID_BLC_DIGITAL_OFFSET);
  933. v4l2_i2c_subdev_init(&mt9p031->subdev, client, &mt9p031_subdev_ops);
  934. mt9p031->subdev.internal_ops = &mt9p031_subdev_internal_ops;
  935. mt9p031->pad.flags = MEDIA_PAD_FL_SOURCE;
  936. ret = media_entity_init(&mt9p031->subdev.entity, 1, &mt9p031->pad, 0);
  937. if (ret < 0)
  938. goto done;
  939. mt9p031->subdev.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
  940. mt9p031->crop.width = MT9P031_WINDOW_WIDTH_DEF;
  941. mt9p031->crop.height = MT9P031_WINDOW_HEIGHT_DEF;
  942. mt9p031->crop.left = MT9P031_COLUMN_START_DEF;
  943. mt9p031->crop.top = MT9P031_ROW_START_DEF;
  944. if (mt9p031->model == MT9P031_MODEL_MONOCHROME)
  945. mt9p031->format.code = MEDIA_BUS_FMT_Y12_1X12;
  946. else
  947. mt9p031->format.code = MEDIA_BUS_FMT_SGRBG12_1X12;
  948. mt9p031->format.width = MT9P031_WINDOW_WIDTH_DEF;
  949. mt9p031->format.height = MT9P031_WINDOW_HEIGHT_DEF;
  950. mt9p031->format.field = V4L2_FIELD_NONE;
  951. mt9p031->format.colorspace = V4L2_COLORSPACE_SRGB;
  952. mt9p031->reset = devm_gpiod_get_optional(&client->dev, "reset",
  953. GPIOD_OUT_HIGH);
  954. ret = mt9p031_clk_setup(mt9p031);
  955. if (ret)
  956. goto done;
  957. ret = v4l2_async_register_subdev(&mt9p031->subdev);
  958. done:
  959. if (ret < 0) {
  960. v4l2_ctrl_handler_free(&mt9p031->ctrls);
  961. media_entity_cleanup(&mt9p031->subdev.entity);
  962. mutex_destroy(&mt9p031->power_lock);
  963. }
  964. return ret;
  965. }
  966. static int mt9p031_remove(struct i2c_client *client)
  967. {
  968. struct v4l2_subdev *subdev = i2c_get_clientdata(client);
  969. struct mt9p031 *mt9p031 = to_mt9p031(subdev);
  970. v4l2_ctrl_handler_free(&mt9p031->ctrls);
  971. v4l2_async_unregister_subdev(subdev);
  972. media_entity_cleanup(&subdev->entity);
  973. mutex_destroy(&mt9p031->power_lock);
  974. return 0;
  975. }
  976. static const struct i2c_device_id mt9p031_id[] = {
  977. { "mt9p031", MT9P031_MODEL_COLOR },
  978. { "mt9p031m", MT9P031_MODEL_MONOCHROME },
  979. { }
  980. };
  981. MODULE_DEVICE_TABLE(i2c, mt9p031_id);
  982. #if IS_ENABLED(CONFIG_OF)
  983. static const struct of_device_id mt9p031_of_match[] = {
  984. { .compatible = "aptina,mt9p031", },
  985. { .compatible = "aptina,mt9p031m", },
  986. { /* sentinel */ },
  987. };
  988. MODULE_DEVICE_TABLE(of, mt9p031_of_match);
  989. #endif
  990. static struct i2c_driver mt9p031_i2c_driver = {
  991. .driver = {
  992. .of_match_table = of_match_ptr(mt9p031_of_match),
  993. .name = "mt9p031",
  994. },
  995. .probe = mt9p031_probe,
  996. .remove = mt9p031_remove,
  997. .id_table = mt9p031_id,
  998. };
  999. module_i2c_driver(mt9p031_i2c_driver);
  1000. MODULE_DESCRIPTION("Aptina MT9P031 Camera driver");
  1001. MODULE_AUTHOR("Bastian Hecht <hechtb@gmail.com>");
  1002. MODULE_LICENSE("GPL v2");