mt9p031.c 33 KB

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