mt9p031.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940
  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/delay.h>
  15. #include <linux/device.h>
  16. #include <linux/module.h>
  17. #include <linux/i2c.h>
  18. #include <linux/log2.h>
  19. #include <linux/pm.h>
  20. #include <linux/slab.h>
  21. #include <linux/videodev2.h>
  22. #include <media/mt9p031.h>
  23. #include <media/v4l2-chip-ident.h>
  24. #include <media/v4l2-ctrls.h>
  25. #include <media/v4l2-device.h>
  26. #include <media/v4l2-subdev.h>
  27. #include "aptina-pll.h"
  28. #define MT9P031_PIXEL_ARRAY_WIDTH 2752
  29. #define MT9P031_PIXEL_ARRAY_HEIGHT 2004
  30. #define MT9P031_CHIP_VERSION 0x00
  31. #define MT9P031_CHIP_VERSION_VALUE 0x1801
  32. #define MT9P031_ROW_START 0x01
  33. #define MT9P031_ROW_START_MIN 0
  34. #define MT9P031_ROW_START_MAX 2004
  35. #define MT9P031_ROW_START_DEF 54
  36. #define MT9P031_COLUMN_START 0x02
  37. #define MT9P031_COLUMN_START_MIN 0
  38. #define MT9P031_COLUMN_START_MAX 2750
  39. #define MT9P031_COLUMN_START_DEF 16
  40. #define MT9P031_WINDOW_HEIGHT 0x03
  41. #define MT9P031_WINDOW_HEIGHT_MIN 2
  42. #define MT9P031_WINDOW_HEIGHT_MAX 2006
  43. #define MT9P031_WINDOW_HEIGHT_DEF 1944
  44. #define MT9P031_WINDOW_WIDTH 0x04
  45. #define MT9P031_WINDOW_WIDTH_MIN 2
  46. #define MT9P031_WINDOW_WIDTH_MAX 2752
  47. #define MT9P031_WINDOW_WIDTH_DEF 2592
  48. #define MT9P031_HORIZONTAL_BLANK 0x05
  49. #define MT9P031_HORIZONTAL_BLANK_MIN 0
  50. #define MT9P031_HORIZONTAL_BLANK_MAX 4095
  51. #define MT9P031_VERTICAL_BLANK 0x06
  52. #define MT9P031_VERTICAL_BLANK_MIN 0
  53. #define MT9P031_VERTICAL_BLANK_MAX 4095
  54. #define MT9P031_VERTICAL_BLANK_DEF 25
  55. #define MT9P031_OUTPUT_CONTROL 0x07
  56. #define MT9P031_OUTPUT_CONTROL_CEN 2
  57. #define MT9P031_OUTPUT_CONTROL_SYN 1
  58. #define MT9P031_OUTPUT_CONTROL_DEF 0x1f82
  59. #define MT9P031_SHUTTER_WIDTH_UPPER 0x08
  60. #define MT9P031_SHUTTER_WIDTH_LOWER 0x09
  61. #define MT9P031_SHUTTER_WIDTH_MIN 1
  62. #define MT9P031_SHUTTER_WIDTH_MAX 1048575
  63. #define MT9P031_SHUTTER_WIDTH_DEF 1943
  64. #define MT9P031_PLL_CONTROL 0x10
  65. #define MT9P031_PLL_CONTROL_PWROFF 0x0050
  66. #define MT9P031_PLL_CONTROL_PWRON 0x0051
  67. #define MT9P031_PLL_CONTROL_USEPLL 0x0052
  68. #define MT9P031_PLL_CONFIG_1 0x11
  69. #define MT9P031_PLL_CONFIG_2 0x12
  70. #define MT9P031_PIXEL_CLOCK_CONTROL 0x0a
  71. #define MT9P031_FRAME_RESTART 0x0b
  72. #define MT9P031_SHUTTER_DELAY 0x0c
  73. #define MT9P031_RST 0x0d
  74. #define MT9P031_RST_ENABLE 1
  75. #define MT9P031_RST_DISABLE 0
  76. #define MT9P031_READ_MODE_1 0x1e
  77. #define MT9P031_READ_MODE_2 0x20
  78. #define MT9P031_READ_MODE_2_ROW_MIR (1 << 15)
  79. #define MT9P031_READ_MODE_2_COL_MIR (1 << 14)
  80. #define MT9P031_READ_MODE_2_ROW_BLC (1 << 6)
  81. #define MT9P031_ROW_ADDRESS_MODE 0x22
  82. #define MT9P031_COLUMN_ADDRESS_MODE 0x23
  83. #define MT9P031_GLOBAL_GAIN 0x35
  84. #define MT9P031_GLOBAL_GAIN_MIN 8
  85. #define MT9P031_GLOBAL_GAIN_MAX 1024
  86. #define MT9P031_GLOBAL_GAIN_DEF 8
  87. #define MT9P031_GLOBAL_GAIN_MULT (1 << 6)
  88. #define MT9P031_ROW_BLACK_DEF_OFFSET 0x4b
  89. #define MT9P031_TEST_PATTERN 0xa0
  90. #define MT9P031_TEST_PATTERN_SHIFT 3
  91. #define MT9P031_TEST_PATTERN_ENABLE (1 << 0)
  92. #define MT9P031_TEST_PATTERN_DISABLE (0 << 0)
  93. #define MT9P031_TEST_PATTERN_GREEN 0xa1
  94. #define MT9P031_TEST_PATTERN_RED 0xa2
  95. #define MT9P031_TEST_PATTERN_BLUE 0xa3
  96. struct mt9p031 {
  97. struct v4l2_subdev subdev;
  98. struct media_pad pad;
  99. struct v4l2_rect crop; /* Sensor window */
  100. struct v4l2_mbus_framefmt format;
  101. struct v4l2_ctrl_handler ctrls;
  102. struct mt9p031_platform_data *pdata;
  103. struct mutex power_lock; /* lock to protect power_count */
  104. int power_count;
  105. struct aptina_pll pll;
  106. /* Registers cache */
  107. u16 output_control;
  108. u16 mode2;
  109. };
  110. static struct mt9p031 *to_mt9p031(struct v4l2_subdev *sd)
  111. {
  112. return container_of(sd, struct mt9p031, subdev);
  113. }
  114. static int mt9p031_read(struct i2c_client *client, u8 reg)
  115. {
  116. return i2c_smbus_read_word_swapped(client, reg);
  117. }
  118. static int mt9p031_write(struct i2c_client *client, u8 reg, u16 data)
  119. {
  120. return i2c_smbus_write_word_swapped(client, reg, data);
  121. }
  122. static int mt9p031_set_output_control(struct mt9p031 *mt9p031, u16 clear,
  123. u16 set)
  124. {
  125. struct i2c_client *client = v4l2_get_subdevdata(&mt9p031->subdev);
  126. u16 value = (mt9p031->output_control & ~clear) | set;
  127. int ret;
  128. ret = mt9p031_write(client, MT9P031_OUTPUT_CONTROL, value);
  129. if (ret < 0)
  130. return ret;
  131. mt9p031->output_control = value;
  132. return 0;
  133. }
  134. static int mt9p031_set_mode2(struct mt9p031 *mt9p031, u16 clear, u16 set)
  135. {
  136. struct i2c_client *client = v4l2_get_subdevdata(&mt9p031->subdev);
  137. u16 value = (mt9p031->mode2 & ~clear) | set;
  138. int ret;
  139. ret = mt9p031_write(client, MT9P031_READ_MODE_2, value);
  140. if (ret < 0)
  141. return ret;
  142. mt9p031->mode2 = value;
  143. return 0;
  144. }
  145. static int mt9p031_reset(struct mt9p031 *mt9p031)
  146. {
  147. struct i2c_client *client = v4l2_get_subdevdata(&mt9p031->subdev);
  148. int ret;
  149. /* Disable chip output, synchronous option update */
  150. ret = mt9p031_write(client, MT9P031_RST, MT9P031_RST_ENABLE);
  151. if (ret < 0)
  152. return ret;
  153. ret = mt9p031_write(client, MT9P031_RST, MT9P031_RST_DISABLE);
  154. if (ret < 0)
  155. return ret;
  156. return mt9p031_set_output_control(mt9p031, MT9P031_OUTPUT_CONTROL_CEN,
  157. 0);
  158. }
  159. static int mt9p031_pll_setup(struct mt9p031 *mt9p031)
  160. {
  161. static const struct aptina_pll_limits limits = {
  162. .ext_clock_min = 6000000,
  163. .ext_clock_max = 27000000,
  164. .int_clock_min = 2000000,
  165. .int_clock_max = 13500000,
  166. .out_clock_min = 180000000,
  167. .out_clock_max = 360000000,
  168. .pix_clock_max = 96000000,
  169. .n_min = 1,
  170. .n_max = 64,
  171. .m_min = 16,
  172. .m_max = 255,
  173. .p1_min = 1,
  174. .p1_max = 128,
  175. };
  176. struct i2c_client *client = v4l2_get_subdevdata(&mt9p031->subdev);
  177. struct mt9p031_platform_data *pdata = mt9p031->pdata;
  178. mt9p031->pll.ext_clock = pdata->ext_freq;
  179. mt9p031->pll.pix_clock = pdata->target_freq;
  180. return aptina_pll_calculate(&client->dev, &limits, &mt9p031->pll);
  181. }
  182. static int mt9p031_pll_enable(struct mt9p031 *mt9p031)
  183. {
  184. struct i2c_client *client = v4l2_get_subdevdata(&mt9p031->subdev);
  185. int ret;
  186. ret = mt9p031_write(client, MT9P031_PLL_CONTROL,
  187. MT9P031_PLL_CONTROL_PWRON);
  188. if (ret < 0)
  189. return ret;
  190. ret = mt9p031_write(client, MT9P031_PLL_CONFIG_1,
  191. (mt9p031->pll.m << 8) | (mt9p031->pll.n - 1));
  192. if (ret < 0)
  193. return ret;
  194. ret = mt9p031_write(client, MT9P031_PLL_CONFIG_2, mt9p031->pll.p1 - 1);
  195. if (ret < 0)
  196. return ret;
  197. usleep_range(1000, 2000);
  198. ret = mt9p031_write(client, MT9P031_PLL_CONTROL,
  199. MT9P031_PLL_CONTROL_PWRON |
  200. MT9P031_PLL_CONTROL_USEPLL);
  201. return ret;
  202. }
  203. static inline int mt9p031_pll_disable(struct mt9p031 *mt9p031)
  204. {
  205. struct i2c_client *client = v4l2_get_subdevdata(&mt9p031->subdev);
  206. return mt9p031_write(client, MT9P031_PLL_CONTROL,
  207. MT9P031_PLL_CONTROL_PWROFF);
  208. }
  209. static int mt9p031_power_on(struct mt9p031 *mt9p031)
  210. {
  211. /* Ensure RESET_BAR is low */
  212. if (mt9p031->pdata->reset) {
  213. mt9p031->pdata->reset(&mt9p031->subdev, 1);
  214. usleep_range(1000, 2000);
  215. }
  216. /* Emable clock */
  217. if (mt9p031->pdata->set_xclk)
  218. mt9p031->pdata->set_xclk(&mt9p031->subdev,
  219. mt9p031->pdata->ext_freq);
  220. /* Now RESET_BAR must be high */
  221. if (mt9p031->pdata->reset) {
  222. mt9p031->pdata->reset(&mt9p031->subdev, 0);
  223. usleep_range(1000, 2000);
  224. }
  225. return 0;
  226. }
  227. static void mt9p031_power_off(struct mt9p031 *mt9p031)
  228. {
  229. if (mt9p031->pdata->reset) {
  230. mt9p031->pdata->reset(&mt9p031->subdev, 1);
  231. usleep_range(1000, 2000);
  232. }
  233. if (mt9p031->pdata->set_xclk)
  234. mt9p031->pdata->set_xclk(&mt9p031->subdev, 0);
  235. }
  236. static int __mt9p031_set_power(struct mt9p031 *mt9p031, bool on)
  237. {
  238. struct i2c_client *client = v4l2_get_subdevdata(&mt9p031->subdev);
  239. int ret;
  240. if (!on) {
  241. mt9p031_power_off(mt9p031);
  242. return 0;
  243. }
  244. ret = mt9p031_power_on(mt9p031);
  245. if (ret < 0)
  246. return ret;
  247. ret = mt9p031_reset(mt9p031);
  248. if (ret < 0) {
  249. dev_err(&client->dev, "Failed to reset the camera\n");
  250. return ret;
  251. }
  252. return v4l2_ctrl_handler_setup(&mt9p031->ctrls);
  253. }
  254. /* -----------------------------------------------------------------------------
  255. * V4L2 subdev video operations
  256. */
  257. static int mt9p031_set_params(struct mt9p031 *mt9p031)
  258. {
  259. struct i2c_client *client = v4l2_get_subdevdata(&mt9p031->subdev);
  260. struct v4l2_mbus_framefmt *format = &mt9p031->format;
  261. const struct v4l2_rect *crop = &mt9p031->crop;
  262. unsigned int hblank;
  263. unsigned int vblank;
  264. unsigned int xskip;
  265. unsigned int yskip;
  266. unsigned int xbin;
  267. unsigned int ybin;
  268. int ret;
  269. /* Windows position and size.
  270. *
  271. * TODO: Make sure the start coordinates and window size match the
  272. * skipping, binning and mirroring (see description of registers 2 and 4
  273. * in table 13, and Binning section on page 41).
  274. */
  275. ret = mt9p031_write(client, MT9P031_COLUMN_START, crop->left);
  276. if (ret < 0)
  277. return ret;
  278. ret = mt9p031_write(client, MT9P031_ROW_START, crop->top);
  279. if (ret < 0)
  280. return ret;
  281. ret = mt9p031_write(client, MT9P031_WINDOW_WIDTH, crop->width - 1);
  282. if (ret < 0)
  283. return ret;
  284. ret = mt9p031_write(client, MT9P031_WINDOW_HEIGHT, crop->height - 1);
  285. if (ret < 0)
  286. return ret;
  287. /* Row and column binning and skipping. Use the maximum binning value
  288. * compatible with the skipping settings.
  289. */
  290. xskip = DIV_ROUND_CLOSEST(crop->width, format->width);
  291. yskip = DIV_ROUND_CLOSEST(crop->height, format->height);
  292. xbin = 1 << (ffs(xskip) - 1);
  293. ybin = 1 << (ffs(yskip) - 1);
  294. ret = mt9p031_write(client, MT9P031_COLUMN_ADDRESS_MODE,
  295. ((xbin - 1) << 4) | (xskip - 1));
  296. if (ret < 0)
  297. return ret;
  298. ret = mt9p031_write(client, MT9P031_ROW_ADDRESS_MODE,
  299. ((ybin - 1) << 4) | (yskip - 1));
  300. if (ret < 0)
  301. return ret;
  302. /* Blanking - use minimum value for horizontal blanking and default
  303. * value for vertical blanking.
  304. */
  305. hblank = 346 * ybin + 64 + (80 >> max_t(unsigned int, xbin, 3));
  306. vblank = MT9P031_VERTICAL_BLANK_DEF;
  307. ret = mt9p031_write(client, MT9P031_HORIZONTAL_BLANK, hblank);
  308. if (ret < 0)
  309. return ret;
  310. ret = mt9p031_write(client, MT9P031_VERTICAL_BLANK, vblank);
  311. if (ret < 0)
  312. return ret;
  313. return ret;
  314. }
  315. static int mt9p031_s_stream(struct v4l2_subdev *subdev, int enable)
  316. {
  317. struct mt9p031 *mt9p031 = to_mt9p031(subdev);
  318. int ret;
  319. if (!enable) {
  320. /* Stop sensor readout */
  321. ret = mt9p031_set_output_control(mt9p031,
  322. MT9P031_OUTPUT_CONTROL_CEN, 0);
  323. if (ret < 0)
  324. return ret;
  325. return mt9p031_pll_disable(mt9p031);
  326. }
  327. ret = mt9p031_set_params(mt9p031);
  328. if (ret < 0)
  329. return ret;
  330. /* Switch to master "normal" mode */
  331. ret = mt9p031_set_output_control(mt9p031, 0,
  332. MT9P031_OUTPUT_CONTROL_CEN);
  333. if (ret < 0)
  334. return ret;
  335. return mt9p031_pll_enable(mt9p031);
  336. }
  337. static int mt9p031_enum_mbus_code(struct v4l2_subdev *subdev,
  338. struct v4l2_subdev_fh *fh,
  339. struct v4l2_subdev_mbus_code_enum *code)
  340. {
  341. struct mt9p031 *mt9p031 = to_mt9p031(subdev);
  342. if (code->pad || code->index)
  343. return -EINVAL;
  344. code->code = mt9p031->format.code;
  345. return 0;
  346. }
  347. static int mt9p031_enum_frame_size(struct v4l2_subdev *subdev,
  348. struct v4l2_subdev_fh *fh,
  349. struct v4l2_subdev_frame_size_enum *fse)
  350. {
  351. struct mt9p031 *mt9p031 = to_mt9p031(subdev);
  352. if (fse->index >= 8 || fse->code != mt9p031->format.code)
  353. return -EINVAL;
  354. fse->min_width = MT9P031_WINDOW_WIDTH_DEF
  355. / min_t(unsigned int, 7, fse->index + 1);
  356. fse->max_width = fse->min_width;
  357. fse->min_height = MT9P031_WINDOW_HEIGHT_DEF / (fse->index + 1);
  358. fse->max_height = fse->min_height;
  359. return 0;
  360. }
  361. static struct v4l2_mbus_framefmt *
  362. __mt9p031_get_pad_format(struct mt9p031 *mt9p031, struct v4l2_subdev_fh *fh,
  363. unsigned int pad, u32 which)
  364. {
  365. switch (which) {
  366. case V4L2_SUBDEV_FORMAT_TRY:
  367. return v4l2_subdev_get_try_format(fh, pad);
  368. case V4L2_SUBDEV_FORMAT_ACTIVE:
  369. return &mt9p031->format;
  370. default:
  371. return NULL;
  372. }
  373. }
  374. static struct v4l2_rect *
  375. __mt9p031_get_pad_crop(struct mt9p031 *mt9p031, struct v4l2_subdev_fh *fh,
  376. unsigned int pad, u32 which)
  377. {
  378. switch (which) {
  379. case V4L2_SUBDEV_FORMAT_TRY:
  380. return v4l2_subdev_get_try_crop(fh, pad);
  381. case V4L2_SUBDEV_FORMAT_ACTIVE:
  382. return &mt9p031->crop;
  383. default:
  384. return NULL;
  385. }
  386. }
  387. static int mt9p031_get_format(struct v4l2_subdev *subdev,
  388. struct v4l2_subdev_fh *fh,
  389. struct v4l2_subdev_format *fmt)
  390. {
  391. struct mt9p031 *mt9p031 = to_mt9p031(subdev);
  392. fmt->format = *__mt9p031_get_pad_format(mt9p031, fh, fmt->pad,
  393. fmt->which);
  394. return 0;
  395. }
  396. static int mt9p031_set_format(struct v4l2_subdev *subdev,
  397. struct v4l2_subdev_fh *fh,
  398. struct v4l2_subdev_format *format)
  399. {
  400. struct mt9p031 *mt9p031 = to_mt9p031(subdev);
  401. struct v4l2_mbus_framefmt *__format;
  402. struct v4l2_rect *__crop;
  403. unsigned int width;
  404. unsigned int height;
  405. unsigned int hratio;
  406. unsigned int vratio;
  407. __crop = __mt9p031_get_pad_crop(mt9p031, fh, format->pad,
  408. format->which);
  409. /* Clamp the width and height to avoid dividing by zero. */
  410. width = clamp_t(unsigned int, ALIGN(format->format.width, 2),
  411. max(__crop->width / 7, MT9P031_WINDOW_WIDTH_MIN),
  412. __crop->width);
  413. height = clamp_t(unsigned int, ALIGN(format->format.height, 2),
  414. max(__crop->height / 8, MT9P031_WINDOW_HEIGHT_MIN),
  415. __crop->height);
  416. hratio = DIV_ROUND_CLOSEST(__crop->width, width);
  417. vratio = DIV_ROUND_CLOSEST(__crop->height, height);
  418. __format = __mt9p031_get_pad_format(mt9p031, fh, format->pad,
  419. format->which);
  420. __format->width = __crop->width / hratio;
  421. __format->height = __crop->height / vratio;
  422. format->format = *__format;
  423. return 0;
  424. }
  425. static int mt9p031_get_crop(struct v4l2_subdev *subdev,
  426. struct v4l2_subdev_fh *fh,
  427. struct v4l2_subdev_crop *crop)
  428. {
  429. struct mt9p031 *mt9p031 = to_mt9p031(subdev);
  430. crop->rect = *__mt9p031_get_pad_crop(mt9p031, fh, crop->pad,
  431. crop->which);
  432. return 0;
  433. }
  434. static int mt9p031_set_crop(struct v4l2_subdev *subdev,
  435. struct v4l2_subdev_fh *fh,
  436. struct v4l2_subdev_crop *crop)
  437. {
  438. struct mt9p031 *mt9p031 = to_mt9p031(subdev);
  439. struct v4l2_mbus_framefmt *__format;
  440. struct v4l2_rect *__crop;
  441. struct v4l2_rect rect;
  442. /* Clamp the crop rectangle boundaries and align them to a multiple of 2
  443. * pixels to ensure a GRBG Bayer pattern.
  444. */
  445. rect.left = clamp(ALIGN(crop->rect.left, 2), MT9P031_COLUMN_START_MIN,
  446. MT9P031_COLUMN_START_MAX);
  447. rect.top = clamp(ALIGN(crop->rect.top, 2), MT9P031_ROW_START_MIN,
  448. MT9P031_ROW_START_MAX);
  449. rect.width = clamp(ALIGN(crop->rect.width, 2),
  450. MT9P031_WINDOW_WIDTH_MIN,
  451. MT9P031_WINDOW_WIDTH_MAX);
  452. rect.height = clamp(ALIGN(crop->rect.height, 2),
  453. MT9P031_WINDOW_HEIGHT_MIN,
  454. MT9P031_WINDOW_HEIGHT_MAX);
  455. rect.width = min(rect.width, MT9P031_PIXEL_ARRAY_WIDTH - rect.left);
  456. rect.height = min(rect.height, MT9P031_PIXEL_ARRAY_HEIGHT - rect.top);
  457. __crop = __mt9p031_get_pad_crop(mt9p031, fh, crop->pad, crop->which);
  458. if (rect.width != __crop->width || rect.height != __crop->height) {
  459. /* Reset the output image size if the crop rectangle size has
  460. * been modified.
  461. */
  462. __format = __mt9p031_get_pad_format(mt9p031, fh, crop->pad,
  463. crop->which);
  464. __format->width = rect.width;
  465. __format->height = rect.height;
  466. }
  467. *__crop = rect;
  468. crop->rect = rect;
  469. return 0;
  470. }
  471. /* -----------------------------------------------------------------------------
  472. * V4L2 subdev control operations
  473. */
  474. #define V4L2_CID_TEST_PATTERN (V4L2_CID_USER_BASE | 0x1001)
  475. static int mt9p031_s_ctrl(struct v4l2_ctrl *ctrl)
  476. {
  477. struct mt9p031 *mt9p031 =
  478. container_of(ctrl->handler, struct mt9p031, ctrls);
  479. struct i2c_client *client = v4l2_get_subdevdata(&mt9p031->subdev);
  480. u16 data;
  481. int ret;
  482. switch (ctrl->id) {
  483. case V4L2_CID_EXPOSURE:
  484. ret = mt9p031_write(client, MT9P031_SHUTTER_WIDTH_UPPER,
  485. (ctrl->val >> 16) & 0xffff);
  486. if (ret < 0)
  487. return ret;
  488. return mt9p031_write(client, MT9P031_SHUTTER_WIDTH_LOWER,
  489. ctrl->val & 0xffff);
  490. case V4L2_CID_GAIN:
  491. /* Gain is controlled by 2 analog stages and a digital stage.
  492. * Valid values for the 3 stages are
  493. *
  494. * Stage Min Max Step
  495. * ------------------------------------------
  496. * First analog stage x1 x2 1
  497. * Second analog stage x1 x4 0.125
  498. * Digital stage x1 x16 0.125
  499. *
  500. * To minimize noise, the gain stages should be used in the
  501. * second analog stage, first analog stage, digital stage order.
  502. * Gain from a previous stage should be pushed to its maximum
  503. * value before the next stage is used.
  504. */
  505. if (ctrl->val <= 32) {
  506. data = ctrl->val;
  507. } else if (ctrl->val <= 64) {
  508. ctrl->val &= ~1;
  509. data = (1 << 6) | (ctrl->val >> 1);
  510. } else {
  511. ctrl->val &= ~7;
  512. data = ((ctrl->val - 64) << 5) | (1 << 6) | 32;
  513. }
  514. return mt9p031_write(client, MT9P031_GLOBAL_GAIN, data);
  515. case V4L2_CID_HFLIP:
  516. if (ctrl->val)
  517. return mt9p031_set_mode2(mt9p031,
  518. 0, MT9P031_READ_MODE_2_COL_MIR);
  519. else
  520. return mt9p031_set_mode2(mt9p031,
  521. MT9P031_READ_MODE_2_COL_MIR, 0);
  522. case V4L2_CID_VFLIP:
  523. if (ctrl->val)
  524. return mt9p031_set_mode2(mt9p031,
  525. 0, MT9P031_READ_MODE_2_ROW_MIR);
  526. else
  527. return mt9p031_set_mode2(mt9p031,
  528. MT9P031_READ_MODE_2_ROW_MIR, 0);
  529. case V4L2_CID_TEST_PATTERN:
  530. if (!ctrl->val) {
  531. ret = mt9p031_set_mode2(mt9p031,
  532. 0, MT9P031_READ_MODE_2_ROW_BLC);
  533. if (ret < 0)
  534. return ret;
  535. return mt9p031_write(client, MT9P031_TEST_PATTERN,
  536. MT9P031_TEST_PATTERN_DISABLE);
  537. }
  538. ret = mt9p031_write(client, MT9P031_TEST_PATTERN_GREEN, 0x05a0);
  539. if (ret < 0)
  540. return ret;
  541. ret = mt9p031_write(client, MT9P031_TEST_PATTERN_RED, 0x0a50);
  542. if (ret < 0)
  543. return ret;
  544. ret = mt9p031_write(client, MT9P031_TEST_PATTERN_BLUE, 0x0aa0);
  545. if (ret < 0)
  546. return ret;
  547. ret = mt9p031_set_mode2(mt9p031, MT9P031_READ_MODE_2_ROW_BLC,
  548. 0);
  549. if (ret < 0)
  550. return ret;
  551. ret = mt9p031_write(client, MT9P031_ROW_BLACK_DEF_OFFSET, 0);
  552. if (ret < 0)
  553. return ret;
  554. return mt9p031_write(client, MT9P031_TEST_PATTERN,
  555. ((ctrl->val - 1) << MT9P031_TEST_PATTERN_SHIFT)
  556. | MT9P031_TEST_PATTERN_ENABLE);
  557. }
  558. return 0;
  559. }
  560. static struct v4l2_ctrl_ops mt9p031_ctrl_ops = {
  561. .s_ctrl = mt9p031_s_ctrl,
  562. };
  563. static const char * const mt9p031_test_pattern_menu[] = {
  564. "Disabled",
  565. "Color Field",
  566. "Horizontal Gradient",
  567. "Vertical Gradient",
  568. "Diagonal Gradient",
  569. "Classic Test Pattern",
  570. "Walking 1s",
  571. "Monochrome Horizontal Bars",
  572. "Monochrome Vertical Bars",
  573. "Vertical Color Bars",
  574. };
  575. static const struct v4l2_ctrl_config mt9p031_ctrls[] = {
  576. {
  577. .ops = &mt9p031_ctrl_ops,
  578. .id = V4L2_CID_TEST_PATTERN,
  579. .type = V4L2_CTRL_TYPE_MENU,
  580. .name = "Test Pattern",
  581. .min = 0,
  582. .max = ARRAY_SIZE(mt9p031_test_pattern_menu) - 1,
  583. .step = 0,
  584. .def = 0,
  585. .flags = 0,
  586. .menu_skip_mask = 0,
  587. .qmenu = mt9p031_test_pattern_menu,
  588. }
  589. };
  590. /* -----------------------------------------------------------------------------
  591. * V4L2 subdev core operations
  592. */
  593. static int mt9p031_set_power(struct v4l2_subdev *subdev, int on)
  594. {
  595. struct mt9p031 *mt9p031 = to_mt9p031(subdev);
  596. int ret = 0;
  597. mutex_lock(&mt9p031->power_lock);
  598. /* If the power count is modified from 0 to != 0 or from != 0 to 0,
  599. * update the power state.
  600. */
  601. if (mt9p031->power_count == !on) {
  602. ret = __mt9p031_set_power(mt9p031, !!on);
  603. if (ret < 0)
  604. goto out;
  605. }
  606. /* Update the power count. */
  607. mt9p031->power_count += on ? 1 : -1;
  608. WARN_ON(mt9p031->power_count < 0);
  609. out:
  610. mutex_unlock(&mt9p031->power_lock);
  611. return ret;
  612. }
  613. /* -----------------------------------------------------------------------------
  614. * V4L2 subdev internal operations
  615. */
  616. static int mt9p031_registered(struct v4l2_subdev *subdev)
  617. {
  618. struct i2c_client *client = v4l2_get_subdevdata(subdev);
  619. struct mt9p031 *mt9p031 = to_mt9p031(subdev);
  620. s32 data;
  621. int ret;
  622. ret = mt9p031_power_on(mt9p031);
  623. if (ret < 0) {
  624. dev_err(&client->dev, "MT9P031 power up failed\n");
  625. return ret;
  626. }
  627. /* Read out the chip version register */
  628. data = mt9p031_read(client, MT9P031_CHIP_VERSION);
  629. if (data != MT9P031_CHIP_VERSION_VALUE) {
  630. dev_err(&client->dev, "MT9P031 not detected, wrong version "
  631. "0x%04x\n", data);
  632. return -ENODEV;
  633. }
  634. mt9p031_power_off(mt9p031);
  635. dev_info(&client->dev, "MT9P031 detected at address 0x%02x\n",
  636. client->addr);
  637. return ret;
  638. }
  639. static int mt9p031_open(struct v4l2_subdev *subdev, struct v4l2_subdev_fh *fh)
  640. {
  641. struct mt9p031 *mt9p031 = to_mt9p031(subdev);
  642. struct v4l2_mbus_framefmt *format;
  643. struct v4l2_rect *crop;
  644. crop = v4l2_subdev_get_try_crop(fh, 0);
  645. crop->left = MT9P031_COLUMN_START_DEF;
  646. crop->top = MT9P031_ROW_START_DEF;
  647. crop->width = MT9P031_WINDOW_WIDTH_DEF;
  648. crop->height = MT9P031_WINDOW_HEIGHT_DEF;
  649. format = v4l2_subdev_get_try_format(fh, 0);
  650. if (mt9p031->pdata->version == MT9P031_MONOCHROME_VERSION)
  651. format->code = V4L2_MBUS_FMT_Y12_1X12;
  652. else
  653. format->code = V4L2_MBUS_FMT_SGRBG12_1X12;
  654. format->width = MT9P031_WINDOW_WIDTH_DEF;
  655. format->height = MT9P031_WINDOW_HEIGHT_DEF;
  656. format->field = V4L2_FIELD_NONE;
  657. format->colorspace = V4L2_COLORSPACE_SRGB;
  658. return mt9p031_set_power(subdev, 1);
  659. }
  660. static int mt9p031_close(struct v4l2_subdev *subdev, struct v4l2_subdev_fh *fh)
  661. {
  662. return mt9p031_set_power(subdev, 0);
  663. }
  664. static struct v4l2_subdev_core_ops mt9p031_subdev_core_ops = {
  665. .s_power = mt9p031_set_power,
  666. };
  667. static struct v4l2_subdev_video_ops mt9p031_subdev_video_ops = {
  668. .s_stream = mt9p031_s_stream,
  669. };
  670. static struct v4l2_subdev_pad_ops mt9p031_subdev_pad_ops = {
  671. .enum_mbus_code = mt9p031_enum_mbus_code,
  672. .enum_frame_size = mt9p031_enum_frame_size,
  673. .get_fmt = mt9p031_get_format,
  674. .set_fmt = mt9p031_set_format,
  675. .get_crop = mt9p031_get_crop,
  676. .set_crop = mt9p031_set_crop,
  677. };
  678. static struct v4l2_subdev_ops mt9p031_subdev_ops = {
  679. .core = &mt9p031_subdev_core_ops,
  680. .video = &mt9p031_subdev_video_ops,
  681. .pad = &mt9p031_subdev_pad_ops,
  682. };
  683. static const struct v4l2_subdev_internal_ops mt9p031_subdev_internal_ops = {
  684. .registered = mt9p031_registered,
  685. .open = mt9p031_open,
  686. .close = mt9p031_close,
  687. };
  688. /* -----------------------------------------------------------------------------
  689. * Driver initialization and probing
  690. */
  691. static int mt9p031_probe(struct i2c_client *client,
  692. const struct i2c_device_id *did)
  693. {
  694. struct mt9p031_platform_data *pdata = client->dev.platform_data;
  695. struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
  696. struct mt9p031 *mt9p031;
  697. unsigned int i;
  698. int ret;
  699. if (pdata == NULL) {
  700. dev_err(&client->dev, "No platform data\n");
  701. return -EINVAL;
  702. }
  703. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WORD_DATA)) {
  704. dev_warn(&client->dev,
  705. "I2C-Adapter doesn't support I2C_FUNC_SMBUS_WORD\n");
  706. return -EIO;
  707. }
  708. mt9p031 = kzalloc(sizeof(*mt9p031), GFP_KERNEL);
  709. if (mt9p031 == NULL)
  710. return -ENOMEM;
  711. mt9p031->pdata = pdata;
  712. mt9p031->output_control = MT9P031_OUTPUT_CONTROL_DEF;
  713. mt9p031->mode2 = MT9P031_READ_MODE_2_ROW_BLC;
  714. v4l2_ctrl_handler_init(&mt9p031->ctrls, ARRAY_SIZE(mt9p031_ctrls) + 4);
  715. v4l2_ctrl_new_std(&mt9p031->ctrls, &mt9p031_ctrl_ops,
  716. V4L2_CID_EXPOSURE, MT9P031_SHUTTER_WIDTH_MIN,
  717. MT9P031_SHUTTER_WIDTH_MAX, 1,
  718. MT9P031_SHUTTER_WIDTH_DEF);
  719. v4l2_ctrl_new_std(&mt9p031->ctrls, &mt9p031_ctrl_ops,
  720. V4L2_CID_GAIN, MT9P031_GLOBAL_GAIN_MIN,
  721. MT9P031_GLOBAL_GAIN_MAX, 1, MT9P031_GLOBAL_GAIN_DEF);
  722. v4l2_ctrl_new_std(&mt9p031->ctrls, &mt9p031_ctrl_ops,
  723. V4L2_CID_HFLIP, 0, 1, 1, 0);
  724. v4l2_ctrl_new_std(&mt9p031->ctrls, &mt9p031_ctrl_ops,
  725. V4L2_CID_VFLIP, 0, 1, 1, 0);
  726. for (i = 0; i < ARRAY_SIZE(mt9p031_ctrls); ++i)
  727. v4l2_ctrl_new_custom(&mt9p031->ctrls, &mt9p031_ctrls[i], NULL);
  728. mt9p031->subdev.ctrl_handler = &mt9p031->ctrls;
  729. if (mt9p031->ctrls.error)
  730. printk(KERN_INFO "%s: control initialization error %d\n",
  731. __func__, mt9p031->ctrls.error);
  732. mutex_init(&mt9p031->power_lock);
  733. v4l2_i2c_subdev_init(&mt9p031->subdev, client, &mt9p031_subdev_ops);
  734. mt9p031->subdev.internal_ops = &mt9p031_subdev_internal_ops;
  735. mt9p031->pad.flags = MEDIA_PAD_FL_SOURCE;
  736. ret = media_entity_init(&mt9p031->subdev.entity, 1, &mt9p031->pad, 0);
  737. if (ret < 0)
  738. goto done;
  739. mt9p031->subdev.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
  740. mt9p031->crop.width = MT9P031_WINDOW_WIDTH_DEF;
  741. mt9p031->crop.height = MT9P031_WINDOW_HEIGHT_DEF;
  742. mt9p031->crop.left = MT9P031_COLUMN_START_DEF;
  743. mt9p031->crop.top = MT9P031_ROW_START_DEF;
  744. if (mt9p031->pdata->version == MT9P031_MONOCHROME_VERSION)
  745. mt9p031->format.code = V4L2_MBUS_FMT_Y12_1X12;
  746. else
  747. mt9p031->format.code = V4L2_MBUS_FMT_SGRBG12_1X12;
  748. mt9p031->format.width = MT9P031_WINDOW_WIDTH_DEF;
  749. mt9p031->format.height = MT9P031_WINDOW_HEIGHT_DEF;
  750. mt9p031->format.field = V4L2_FIELD_NONE;
  751. mt9p031->format.colorspace = V4L2_COLORSPACE_SRGB;
  752. ret = mt9p031_pll_setup(mt9p031);
  753. done:
  754. if (ret < 0) {
  755. v4l2_ctrl_handler_free(&mt9p031->ctrls);
  756. media_entity_cleanup(&mt9p031->subdev.entity);
  757. kfree(mt9p031);
  758. }
  759. return ret;
  760. }
  761. static int mt9p031_remove(struct i2c_client *client)
  762. {
  763. struct v4l2_subdev *subdev = i2c_get_clientdata(client);
  764. struct mt9p031 *mt9p031 = to_mt9p031(subdev);
  765. v4l2_ctrl_handler_free(&mt9p031->ctrls);
  766. v4l2_device_unregister_subdev(subdev);
  767. media_entity_cleanup(&subdev->entity);
  768. kfree(mt9p031);
  769. return 0;
  770. }
  771. static const struct i2c_device_id mt9p031_id[] = {
  772. { "mt9p031", 0 },
  773. { }
  774. };
  775. MODULE_DEVICE_TABLE(i2c, mt9p031_id);
  776. static struct i2c_driver mt9p031_i2c_driver = {
  777. .driver = {
  778. .name = "mt9p031",
  779. },
  780. .probe = mt9p031_probe,
  781. .remove = mt9p031_remove,
  782. .id_table = mt9p031_id,
  783. };
  784. module_i2c_driver(mt9p031_i2c_driver);
  785. MODULE_DESCRIPTION("Aptina MT9P031 Camera driver");
  786. MODULE_AUTHOR("Bastian Hecht <hechtb@gmail.com>");
  787. MODULE_LICENSE("GPL v2");