mt9t031.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824
  1. /*
  2. * Driver for MT9T031 CMOS Image Sensor from Micron
  3. *
  4. * Copyright (C) 2008, Guennadi Liakhovetski, DENX Software Engineering <lg@denx.de>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. */
  10. #include <linux/device.h>
  11. #include <linux/i2c.h>
  12. #include <linux/log2.h>
  13. #include <linux/pm.h>
  14. #include <linux/slab.h>
  15. #include <linux/v4l2-mediabus.h>
  16. #include <linux/videodev2.h>
  17. #include <linux/module.h>
  18. #include <media/soc_camera.h>
  19. #include <media/v4l2-subdev.h>
  20. #include <media/v4l2-ctrls.h>
  21. /*
  22. * ATTENTION: this driver still cannot be used outside of the soc-camera
  23. * framework because of its PM implementation, using the video_device node.
  24. * If hardware becomes available for testing, alternative PM approaches shall
  25. * be considered and tested.
  26. */
  27. /*
  28. * mt9t031 i2c address 0x5d
  29. * The platform has to define struct i2c_board_info objects and link to them
  30. * from struct soc_camera_host_desc
  31. */
  32. /* mt9t031 selected register addresses */
  33. #define MT9T031_CHIP_VERSION 0x00
  34. #define MT9T031_ROW_START 0x01
  35. #define MT9T031_COLUMN_START 0x02
  36. #define MT9T031_WINDOW_HEIGHT 0x03
  37. #define MT9T031_WINDOW_WIDTH 0x04
  38. #define MT9T031_HORIZONTAL_BLANKING 0x05
  39. #define MT9T031_VERTICAL_BLANKING 0x06
  40. #define MT9T031_OUTPUT_CONTROL 0x07
  41. #define MT9T031_SHUTTER_WIDTH_UPPER 0x08
  42. #define MT9T031_SHUTTER_WIDTH 0x09
  43. #define MT9T031_PIXEL_CLOCK_CONTROL 0x0a
  44. #define MT9T031_FRAME_RESTART 0x0b
  45. #define MT9T031_SHUTTER_DELAY 0x0c
  46. #define MT9T031_RESET 0x0d
  47. #define MT9T031_READ_MODE_1 0x1e
  48. #define MT9T031_READ_MODE_2 0x20
  49. #define MT9T031_READ_MODE_3 0x21
  50. #define MT9T031_ROW_ADDRESS_MODE 0x22
  51. #define MT9T031_COLUMN_ADDRESS_MODE 0x23
  52. #define MT9T031_GLOBAL_GAIN 0x35
  53. #define MT9T031_CHIP_ENABLE 0xF8
  54. #define MT9T031_MAX_HEIGHT 1536
  55. #define MT9T031_MAX_WIDTH 2048
  56. #define MT9T031_MIN_HEIGHT 2
  57. #define MT9T031_MIN_WIDTH 18
  58. #define MT9T031_HORIZONTAL_BLANK 142
  59. #define MT9T031_VERTICAL_BLANK 25
  60. #define MT9T031_COLUMN_SKIP 32
  61. #define MT9T031_ROW_SKIP 20
  62. struct mt9t031 {
  63. struct v4l2_subdev subdev;
  64. struct v4l2_ctrl_handler hdl;
  65. struct {
  66. /* exposure/auto-exposure cluster */
  67. struct v4l2_ctrl *autoexposure;
  68. struct v4l2_ctrl *exposure;
  69. };
  70. struct v4l2_rect rect; /* Sensor window */
  71. u16 xskip;
  72. u16 yskip;
  73. unsigned int total_h;
  74. unsigned short y_skip_top; /* Lines to skip at the top */
  75. };
  76. static struct mt9t031 *to_mt9t031(const struct i2c_client *client)
  77. {
  78. return container_of(i2c_get_clientdata(client), struct mt9t031, subdev);
  79. }
  80. static int reg_read(struct i2c_client *client, const u8 reg)
  81. {
  82. return i2c_smbus_read_word_swapped(client, reg);
  83. }
  84. static int reg_write(struct i2c_client *client, const u8 reg,
  85. const u16 data)
  86. {
  87. return i2c_smbus_write_word_swapped(client, reg, data);
  88. }
  89. static int reg_set(struct i2c_client *client, const u8 reg,
  90. const u16 data)
  91. {
  92. int ret;
  93. ret = reg_read(client, reg);
  94. if (ret < 0)
  95. return ret;
  96. return reg_write(client, reg, ret | data);
  97. }
  98. static int reg_clear(struct i2c_client *client, const u8 reg,
  99. const u16 data)
  100. {
  101. int ret;
  102. ret = reg_read(client, reg);
  103. if (ret < 0)
  104. return ret;
  105. return reg_write(client, reg, ret & ~data);
  106. }
  107. static int set_shutter(struct i2c_client *client, const u32 data)
  108. {
  109. int ret;
  110. ret = reg_write(client, MT9T031_SHUTTER_WIDTH_UPPER, data >> 16);
  111. if (ret >= 0)
  112. ret = reg_write(client, MT9T031_SHUTTER_WIDTH, data & 0xffff);
  113. return ret;
  114. }
  115. static int get_shutter(struct i2c_client *client, u32 *data)
  116. {
  117. int ret;
  118. ret = reg_read(client, MT9T031_SHUTTER_WIDTH_UPPER);
  119. *data = ret << 16;
  120. if (ret >= 0)
  121. ret = reg_read(client, MT9T031_SHUTTER_WIDTH);
  122. *data |= ret & 0xffff;
  123. return ret < 0 ? ret : 0;
  124. }
  125. static int mt9t031_idle(struct i2c_client *client)
  126. {
  127. int ret;
  128. /* Disable chip output, synchronous option update */
  129. ret = reg_write(client, MT9T031_RESET, 1);
  130. if (ret >= 0)
  131. ret = reg_write(client, MT9T031_RESET, 0);
  132. if (ret >= 0)
  133. ret = reg_clear(client, MT9T031_OUTPUT_CONTROL, 2);
  134. return ret >= 0 ? 0 : -EIO;
  135. }
  136. static int mt9t031_s_stream(struct v4l2_subdev *sd, int enable)
  137. {
  138. struct i2c_client *client = v4l2_get_subdevdata(sd);
  139. int ret;
  140. if (enable)
  141. /* Switch to master "normal" mode */
  142. ret = reg_set(client, MT9T031_OUTPUT_CONTROL, 2);
  143. else
  144. /* Stop sensor readout */
  145. ret = reg_clear(client, MT9T031_OUTPUT_CONTROL, 2);
  146. if (ret < 0)
  147. return -EIO;
  148. return 0;
  149. }
  150. /* target must be _even_ */
  151. static u16 mt9t031_skip(s32 *source, s32 target, s32 max)
  152. {
  153. unsigned int skip;
  154. if (*source < target + target / 2) {
  155. *source = target;
  156. return 1;
  157. }
  158. skip = min(max, *source + target / 2) / target;
  159. if (skip > 8)
  160. skip = 8;
  161. *source = target * skip;
  162. return skip;
  163. }
  164. /* rect is the sensor rectangle, the caller guarantees parameter validity */
  165. static int mt9t031_set_params(struct i2c_client *client,
  166. struct v4l2_rect *rect, u16 xskip, u16 yskip)
  167. {
  168. struct mt9t031 *mt9t031 = to_mt9t031(client);
  169. int ret;
  170. u16 xbin, ybin;
  171. const u16 hblank = MT9T031_HORIZONTAL_BLANK,
  172. vblank = MT9T031_VERTICAL_BLANK;
  173. xbin = min(xskip, (u16)3);
  174. ybin = min(yskip, (u16)3);
  175. /*
  176. * Could just do roundup(rect->left, [xy]bin * 2); but this is cheaper.
  177. * There is always a valid suitably aligned value. The worst case is
  178. * xbin = 3, width = 2048. Then we will start at 36, the last read out
  179. * pixel will be 2083, which is < 2085 - first black pixel.
  180. *
  181. * MT9T031 datasheet imposes window left border alignment, depending on
  182. * the selected xskip. Failing to conform to this requirement produces
  183. * dark horizontal stripes in the image. However, even obeying to this
  184. * requirement doesn't eliminate the stripes in all configurations. They
  185. * appear "locally reproducibly," but can differ between tests under
  186. * different lighting conditions.
  187. */
  188. switch (xbin) {
  189. case 1:
  190. rect->left &= ~1;
  191. break;
  192. case 2:
  193. rect->left &= ~3;
  194. break;
  195. case 3:
  196. rect->left = rect->left > roundup(MT9T031_COLUMN_SKIP, 6) ?
  197. (rect->left / 6) * 6 : roundup(MT9T031_COLUMN_SKIP, 6);
  198. }
  199. rect->top &= ~1;
  200. dev_dbg(&client->dev, "skip %u:%u, rect %ux%u@%u:%u\n",
  201. xskip, yskip, rect->width, rect->height, rect->left, rect->top);
  202. /* Disable register update, reconfigure atomically */
  203. ret = reg_set(client, MT9T031_OUTPUT_CONTROL, 1);
  204. if (ret < 0)
  205. return ret;
  206. /* Blanking and start values - default... */
  207. ret = reg_write(client, MT9T031_HORIZONTAL_BLANKING, hblank);
  208. if (ret >= 0)
  209. ret = reg_write(client, MT9T031_VERTICAL_BLANKING, vblank);
  210. if (yskip != mt9t031->yskip || xskip != mt9t031->xskip) {
  211. /* Binning, skipping */
  212. if (ret >= 0)
  213. ret = reg_write(client, MT9T031_COLUMN_ADDRESS_MODE,
  214. ((xbin - 1) << 4) | (xskip - 1));
  215. if (ret >= 0)
  216. ret = reg_write(client, MT9T031_ROW_ADDRESS_MODE,
  217. ((ybin - 1) << 4) | (yskip - 1));
  218. }
  219. dev_dbg(&client->dev, "new physical left %u, top %u\n",
  220. rect->left, rect->top);
  221. /*
  222. * The caller provides a supported format, as guaranteed by
  223. * .try_mbus_fmt(), soc_camera_s_crop() and soc_camera_cropcap()
  224. */
  225. if (ret >= 0)
  226. ret = reg_write(client, MT9T031_COLUMN_START, rect->left);
  227. if (ret >= 0)
  228. ret = reg_write(client, MT9T031_ROW_START, rect->top);
  229. if (ret >= 0)
  230. ret = reg_write(client, MT9T031_WINDOW_WIDTH, rect->width - 1);
  231. if (ret >= 0)
  232. ret = reg_write(client, MT9T031_WINDOW_HEIGHT,
  233. rect->height + mt9t031->y_skip_top - 1);
  234. if (ret >= 0 && v4l2_ctrl_g_ctrl(mt9t031->autoexposure) == V4L2_EXPOSURE_AUTO) {
  235. mt9t031->total_h = rect->height + mt9t031->y_skip_top + vblank;
  236. ret = set_shutter(client, mt9t031->total_h);
  237. }
  238. /* Re-enable register update, commit all changes */
  239. if (ret >= 0)
  240. ret = reg_clear(client, MT9T031_OUTPUT_CONTROL, 1);
  241. if (ret >= 0) {
  242. mt9t031->rect = *rect;
  243. mt9t031->xskip = xskip;
  244. mt9t031->yskip = yskip;
  245. }
  246. return ret < 0 ? ret : 0;
  247. }
  248. static int mt9t031_s_crop(struct v4l2_subdev *sd, const struct v4l2_crop *a)
  249. {
  250. struct v4l2_rect rect = a->c;
  251. struct i2c_client *client = v4l2_get_subdevdata(sd);
  252. struct mt9t031 *mt9t031 = to_mt9t031(client);
  253. rect.width = ALIGN(rect.width, 2);
  254. rect.height = ALIGN(rect.height, 2);
  255. soc_camera_limit_side(&rect.left, &rect.width,
  256. MT9T031_COLUMN_SKIP, MT9T031_MIN_WIDTH, MT9T031_MAX_WIDTH);
  257. soc_camera_limit_side(&rect.top, &rect.height,
  258. MT9T031_ROW_SKIP, MT9T031_MIN_HEIGHT, MT9T031_MAX_HEIGHT);
  259. return mt9t031_set_params(client, &rect, mt9t031->xskip, mt9t031->yskip);
  260. }
  261. static int mt9t031_g_crop(struct v4l2_subdev *sd, struct v4l2_crop *a)
  262. {
  263. struct i2c_client *client = v4l2_get_subdevdata(sd);
  264. struct mt9t031 *mt9t031 = to_mt9t031(client);
  265. a->c = mt9t031->rect;
  266. a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  267. return 0;
  268. }
  269. static int mt9t031_cropcap(struct v4l2_subdev *sd, struct v4l2_cropcap *a)
  270. {
  271. a->bounds.left = MT9T031_COLUMN_SKIP;
  272. a->bounds.top = MT9T031_ROW_SKIP;
  273. a->bounds.width = MT9T031_MAX_WIDTH;
  274. a->bounds.height = MT9T031_MAX_HEIGHT;
  275. a->defrect = a->bounds;
  276. a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  277. a->pixelaspect.numerator = 1;
  278. a->pixelaspect.denominator = 1;
  279. return 0;
  280. }
  281. static int mt9t031_g_fmt(struct v4l2_subdev *sd,
  282. struct v4l2_mbus_framefmt *mf)
  283. {
  284. struct i2c_client *client = v4l2_get_subdevdata(sd);
  285. struct mt9t031 *mt9t031 = to_mt9t031(client);
  286. mf->width = mt9t031->rect.width / mt9t031->xskip;
  287. mf->height = mt9t031->rect.height / mt9t031->yskip;
  288. mf->code = V4L2_MBUS_FMT_SBGGR10_1X10;
  289. mf->colorspace = V4L2_COLORSPACE_SRGB;
  290. mf->field = V4L2_FIELD_NONE;
  291. return 0;
  292. }
  293. static int mt9t031_s_fmt(struct v4l2_subdev *sd,
  294. struct v4l2_mbus_framefmt *mf)
  295. {
  296. struct i2c_client *client = v4l2_get_subdevdata(sd);
  297. struct mt9t031 *mt9t031 = to_mt9t031(client);
  298. u16 xskip, yskip;
  299. struct v4l2_rect rect = mt9t031->rect;
  300. /*
  301. * try_fmt has put width and height within limits.
  302. * S_FMT: use binning and skipping for scaling
  303. */
  304. xskip = mt9t031_skip(&rect.width, mf->width, MT9T031_MAX_WIDTH);
  305. yskip = mt9t031_skip(&rect.height, mf->height, MT9T031_MAX_HEIGHT);
  306. mf->code = V4L2_MBUS_FMT_SBGGR10_1X10;
  307. mf->colorspace = V4L2_COLORSPACE_SRGB;
  308. /* mt9t031_set_params() doesn't change width and height */
  309. return mt9t031_set_params(client, &rect, xskip, yskip);
  310. }
  311. /*
  312. * If a user window larger than sensor window is requested, we'll increase the
  313. * sensor window.
  314. */
  315. static int mt9t031_try_fmt(struct v4l2_subdev *sd,
  316. struct v4l2_mbus_framefmt *mf)
  317. {
  318. v4l_bound_align_image(
  319. &mf->width, MT9T031_MIN_WIDTH, MT9T031_MAX_WIDTH, 1,
  320. &mf->height, MT9T031_MIN_HEIGHT, MT9T031_MAX_HEIGHT, 1, 0);
  321. mf->code = V4L2_MBUS_FMT_SBGGR10_1X10;
  322. mf->colorspace = V4L2_COLORSPACE_SRGB;
  323. return 0;
  324. }
  325. #ifdef CONFIG_VIDEO_ADV_DEBUG
  326. static int mt9t031_g_register(struct v4l2_subdev *sd,
  327. struct v4l2_dbg_register *reg)
  328. {
  329. struct i2c_client *client = v4l2_get_subdevdata(sd);
  330. if (reg->reg > 0xff)
  331. return -EINVAL;
  332. reg->size = 1;
  333. reg->val = reg_read(client, reg->reg);
  334. if (reg->val > 0xffff)
  335. return -EIO;
  336. return 0;
  337. }
  338. static int mt9t031_s_register(struct v4l2_subdev *sd,
  339. const struct v4l2_dbg_register *reg)
  340. {
  341. struct i2c_client *client = v4l2_get_subdevdata(sd);
  342. if (reg->reg > 0xff)
  343. return -EINVAL;
  344. if (reg_write(client, reg->reg, reg->val) < 0)
  345. return -EIO;
  346. return 0;
  347. }
  348. #endif
  349. static int mt9t031_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
  350. {
  351. struct mt9t031 *mt9t031 = container_of(ctrl->handler,
  352. struct mt9t031, hdl);
  353. const u32 shutter_max = MT9T031_MAX_HEIGHT + MT9T031_VERTICAL_BLANK;
  354. s32 min, max;
  355. switch (ctrl->id) {
  356. case V4L2_CID_EXPOSURE_AUTO:
  357. min = mt9t031->exposure->minimum;
  358. max = mt9t031->exposure->maximum;
  359. mt9t031->exposure->val =
  360. (shutter_max / 2 + (mt9t031->total_h - 1) * (max - min))
  361. / shutter_max + min;
  362. break;
  363. }
  364. return 0;
  365. }
  366. static int mt9t031_s_ctrl(struct v4l2_ctrl *ctrl)
  367. {
  368. struct mt9t031 *mt9t031 = container_of(ctrl->handler,
  369. struct mt9t031, hdl);
  370. struct v4l2_subdev *sd = &mt9t031->subdev;
  371. struct i2c_client *client = v4l2_get_subdevdata(sd);
  372. struct v4l2_ctrl *exp = mt9t031->exposure;
  373. int data;
  374. switch (ctrl->id) {
  375. case V4L2_CID_VFLIP:
  376. if (ctrl->val)
  377. data = reg_set(client, MT9T031_READ_MODE_2, 0x8000);
  378. else
  379. data = reg_clear(client, MT9T031_READ_MODE_2, 0x8000);
  380. if (data < 0)
  381. return -EIO;
  382. return 0;
  383. case V4L2_CID_HFLIP:
  384. if (ctrl->val)
  385. data = reg_set(client, MT9T031_READ_MODE_2, 0x4000);
  386. else
  387. data = reg_clear(client, MT9T031_READ_MODE_2, 0x4000);
  388. if (data < 0)
  389. return -EIO;
  390. return 0;
  391. case V4L2_CID_GAIN:
  392. /* See Datasheet Table 7, Gain settings. */
  393. if (ctrl->val <= ctrl->default_value) {
  394. /* Pack it into 0..1 step 0.125, register values 0..8 */
  395. unsigned long range = ctrl->default_value - ctrl->minimum;
  396. data = ((ctrl->val - ctrl->minimum) * 8 + range / 2) / range;
  397. dev_dbg(&client->dev, "Setting gain %d\n", data);
  398. data = reg_write(client, MT9T031_GLOBAL_GAIN, data);
  399. if (data < 0)
  400. return -EIO;
  401. } else {
  402. /* Pack it into 1.125..128 variable step, register values 9..0x7860 */
  403. /* We assume qctrl->maximum - qctrl->default_value - 1 > 0 */
  404. unsigned long range = ctrl->maximum - ctrl->default_value - 1;
  405. /* calculated gain: map 65..127 to 9..1024 step 0.125 */
  406. unsigned long gain = ((ctrl->val - ctrl->default_value - 1) *
  407. 1015 + range / 2) / range + 9;
  408. if (gain <= 32) /* calculated gain 9..32 -> 9..32 */
  409. data = gain;
  410. else if (gain <= 64) /* calculated gain 33..64 -> 0x51..0x60 */
  411. data = ((gain - 32) * 16 + 16) / 32 + 80;
  412. else
  413. /* calculated gain 65..1024 -> (1..120) << 8 + 0x60 */
  414. data = (((gain - 64 + 7) * 32) & 0xff00) | 0x60;
  415. dev_dbg(&client->dev, "Set gain from 0x%x to 0x%x\n",
  416. reg_read(client, MT9T031_GLOBAL_GAIN), data);
  417. data = reg_write(client, MT9T031_GLOBAL_GAIN, data);
  418. if (data < 0)
  419. return -EIO;
  420. }
  421. return 0;
  422. case V4L2_CID_EXPOSURE_AUTO:
  423. if (ctrl->val == V4L2_EXPOSURE_MANUAL) {
  424. unsigned int range = exp->maximum - exp->minimum;
  425. unsigned int shutter = ((exp->val - exp->minimum) * 1048 +
  426. range / 2) / range + 1;
  427. u32 old;
  428. get_shutter(client, &old);
  429. dev_dbg(&client->dev, "Set shutter from %u to %u\n",
  430. old, shutter);
  431. if (set_shutter(client, shutter) < 0)
  432. return -EIO;
  433. } else {
  434. const u16 vblank = MT9T031_VERTICAL_BLANK;
  435. mt9t031->total_h = mt9t031->rect.height +
  436. mt9t031->y_skip_top + vblank;
  437. if (set_shutter(client, mt9t031->total_h) < 0)
  438. return -EIO;
  439. }
  440. return 0;
  441. default:
  442. return -EINVAL;
  443. }
  444. return 0;
  445. }
  446. /*
  447. * Power Management:
  448. * This function does nothing for now but must be present for pm to work
  449. */
  450. static int mt9t031_runtime_suspend(struct device *dev)
  451. {
  452. return 0;
  453. }
  454. /*
  455. * Power Management:
  456. * COLUMN_ADDRESS_MODE and ROW_ADDRESS_MODE are not rewritten if unchanged
  457. * they are however changed at reset if the platform hook is present
  458. * thus we rewrite them with the values stored by the driver
  459. */
  460. static int mt9t031_runtime_resume(struct device *dev)
  461. {
  462. struct video_device *vdev = to_video_device(dev);
  463. struct v4l2_subdev *sd = soc_camera_vdev_to_subdev(vdev);
  464. struct i2c_client *client = v4l2_get_subdevdata(sd);
  465. struct mt9t031 *mt9t031 = to_mt9t031(client);
  466. int ret;
  467. u16 xbin, ybin;
  468. xbin = min(mt9t031->xskip, (u16)3);
  469. ybin = min(mt9t031->yskip, (u16)3);
  470. ret = reg_write(client, MT9T031_COLUMN_ADDRESS_MODE,
  471. ((xbin - 1) << 4) | (mt9t031->xskip - 1));
  472. if (ret < 0)
  473. return ret;
  474. ret = reg_write(client, MT9T031_ROW_ADDRESS_MODE,
  475. ((ybin - 1) << 4) | (mt9t031->yskip - 1));
  476. if (ret < 0)
  477. return ret;
  478. return 0;
  479. }
  480. static const struct dev_pm_ops mt9t031_dev_pm_ops = {
  481. .runtime_suspend = mt9t031_runtime_suspend,
  482. .runtime_resume = mt9t031_runtime_resume,
  483. };
  484. static struct device_type mt9t031_dev_type = {
  485. .name = "MT9T031",
  486. .pm = &mt9t031_dev_pm_ops,
  487. };
  488. static int mt9t031_s_power(struct v4l2_subdev *sd, int on)
  489. {
  490. struct i2c_client *client = v4l2_get_subdevdata(sd);
  491. struct soc_camera_subdev_desc *ssdd = soc_camera_i2c_to_desc(client);
  492. struct video_device *vdev = soc_camera_i2c_to_vdev(client);
  493. int ret;
  494. if (on) {
  495. ret = soc_camera_power_on(&client->dev, ssdd);
  496. if (ret < 0)
  497. return ret;
  498. vdev->dev.type = &mt9t031_dev_type;
  499. } else {
  500. vdev->dev.type = NULL;
  501. soc_camera_power_off(&client->dev, ssdd);
  502. }
  503. return 0;
  504. }
  505. /*
  506. * Interface active, can use i2c. If it fails, it can indeed mean, that
  507. * this wasn't our capture interface, so, we wait for the right one
  508. */
  509. static int mt9t031_video_probe(struct i2c_client *client)
  510. {
  511. struct mt9t031 *mt9t031 = to_mt9t031(client);
  512. s32 data;
  513. int ret;
  514. ret = mt9t031_s_power(&mt9t031->subdev, 1);
  515. if (ret < 0)
  516. return ret;
  517. ret = mt9t031_idle(client);
  518. if (ret < 0) {
  519. dev_err(&client->dev, "Failed to initialise the camera\n");
  520. goto done;
  521. }
  522. /* Read out the chip version register */
  523. data = reg_read(client, MT9T031_CHIP_VERSION);
  524. switch (data) {
  525. case 0x1621:
  526. break;
  527. default:
  528. dev_err(&client->dev,
  529. "No MT9T031 chip detected, register read %x\n", data);
  530. ret = -ENODEV;
  531. goto done;
  532. }
  533. dev_info(&client->dev, "Detected a MT9T031 chip ID %x\n", data);
  534. ret = v4l2_ctrl_handler_setup(&mt9t031->hdl);
  535. done:
  536. mt9t031_s_power(&mt9t031->subdev, 0);
  537. return ret;
  538. }
  539. static int mt9t031_g_skip_top_lines(struct v4l2_subdev *sd, u32 *lines)
  540. {
  541. struct i2c_client *client = v4l2_get_subdevdata(sd);
  542. struct mt9t031 *mt9t031 = to_mt9t031(client);
  543. *lines = mt9t031->y_skip_top;
  544. return 0;
  545. }
  546. static const struct v4l2_ctrl_ops mt9t031_ctrl_ops = {
  547. .g_volatile_ctrl = mt9t031_g_volatile_ctrl,
  548. .s_ctrl = mt9t031_s_ctrl,
  549. };
  550. static struct v4l2_subdev_core_ops mt9t031_subdev_core_ops = {
  551. .s_power = mt9t031_s_power,
  552. #ifdef CONFIG_VIDEO_ADV_DEBUG
  553. .g_register = mt9t031_g_register,
  554. .s_register = mt9t031_s_register,
  555. #endif
  556. };
  557. static int mt9t031_enum_fmt(struct v4l2_subdev *sd, unsigned int index,
  558. enum v4l2_mbus_pixelcode *code)
  559. {
  560. if (index)
  561. return -EINVAL;
  562. *code = V4L2_MBUS_FMT_SBGGR10_1X10;
  563. return 0;
  564. }
  565. static int mt9t031_g_mbus_config(struct v4l2_subdev *sd,
  566. struct v4l2_mbus_config *cfg)
  567. {
  568. struct i2c_client *client = v4l2_get_subdevdata(sd);
  569. struct soc_camera_subdev_desc *ssdd = soc_camera_i2c_to_desc(client);
  570. cfg->flags = V4L2_MBUS_MASTER | V4L2_MBUS_PCLK_SAMPLE_RISING |
  571. V4L2_MBUS_PCLK_SAMPLE_FALLING | V4L2_MBUS_HSYNC_ACTIVE_HIGH |
  572. V4L2_MBUS_VSYNC_ACTIVE_HIGH | V4L2_MBUS_DATA_ACTIVE_HIGH;
  573. cfg->type = V4L2_MBUS_PARALLEL;
  574. cfg->flags = soc_camera_apply_board_flags(ssdd, cfg);
  575. return 0;
  576. }
  577. static int mt9t031_s_mbus_config(struct v4l2_subdev *sd,
  578. const struct v4l2_mbus_config *cfg)
  579. {
  580. struct i2c_client *client = v4l2_get_subdevdata(sd);
  581. struct soc_camera_subdev_desc *ssdd = soc_camera_i2c_to_desc(client);
  582. if (soc_camera_apply_board_flags(ssdd, cfg) &
  583. V4L2_MBUS_PCLK_SAMPLE_FALLING)
  584. return reg_clear(client, MT9T031_PIXEL_CLOCK_CONTROL, 0x8000);
  585. else
  586. return reg_set(client, MT9T031_PIXEL_CLOCK_CONTROL, 0x8000);
  587. }
  588. static struct v4l2_subdev_video_ops mt9t031_subdev_video_ops = {
  589. .s_stream = mt9t031_s_stream,
  590. .s_mbus_fmt = mt9t031_s_fmt,
  591. .g_mbus_fmt = mt9t031_g_fmt,
  592. .try_mbus_fmt = mt9t031_try_fmt,
  593. .s_crop = mt9t031_s_crop,
  594. .g_crop = mt9t031_g_crop,
  595. .cropcap = mt9t031_cropcap,
  596. .enum_mbus_fmt = mt9t031_enum_fmt,
  597. .g_mbus_config = mt9t031_g_mbus_config,
  598. .s_mbus_config = mt9t031_s_mbus_config,
  599. };
  600. static struct v4l2_subdev_sensor_ops mt9t031_subdev_sensor_ops = {
  601. .g_skip_top_lines = mt9t031_g_skip_top_lines,
  602. };
  603. static struct v4l2_subdev_ops mt9t031_subdev_ops = {
  604. .core = &mt9t031_subdev_core_ops,
  605. .video = &mt9t031_subdev_video_ops,
  606. .sensor = &mt9t031_subdev_sensor_ops,
  607. };
  608. static int mt9t031_probe(struct i2c_client *client,
  609. const struct i2c_device_id *did)
  610. {
  611. struct mt9t031 *mt9t031;
  612. struct soc_camera_subdev_desc *ssdd = soc_camera_i2c_to_desc(client);
  613. struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
  614. int ret;
  615. if (!ssdd) {
  616. dev_err(&client->dev, "MT9T031 driver needs platform data\n");
  617. return -EINVAL;
  618. }
  619. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WORD_DATA)) {
  620. dev_warn(&adapter->dev,
  621. "I2C-Adapter doesn't support I2C_FUNC_SMBUS_WORD\n");
  622. return -EIO;
  623. }
  624. mt9t031 = devm_kzalloc(&client->dev, sizeof(struct mt9t031), GFP_KERNEL);
  625. if (!mt9t031)
  626. return -ENOMEM;
  627. v4l2_i2c_subdev_init(&mt9t031->subdev, client, &mt9t031_subdev_ops);
  628. v4l2_ctrl_handler_init(&mt9t031->hdl, 5);
  629. v4l2_ctrl_new_std(&mt9t031->hdl, &mt9t031_ctrl_ops,
  630. V4L2_CID_VFLIP, 0, 1, 1, 0);
  631. v4l2_ctrl_new_std(&mt9t031->hdl, &mt9t031_ctrl_ops,
  632. V4L2_CID_HFLIP, 0, 1, 1, 0);
  633. v4l2_ctrl_new_std(&mt9t031->hdl, &mt9t031_ctrl_ops,
  634. V4L2_CID_GAIN, 0, 127, 1, 64);
  635. /*
  636. * Simulated autoexposure. If enabled, we calculate shutter width
  637. * ourselves in the driver based on vertical blanking and frame width
  638. */
  639. mt9t031->autoexposure = v4l2_ctrl_new_std_menu(&mt9t031->hdl,
  640. &mt9t031_ctrl_ops, V4L2_CID_EXPOSURE_AUTO, 1, 0,
  641. V4L2_EXPOSURE_AUTO);
  642. mt9t031->exposure = v4l2_ctrl_new_std(&mt9t031->hdl, &mt9t031_ctrl_ops,
  643. V4L2_CID_EXPOSURE, 1, 255, 1, 255);
  644. mt9t031->subdev.ctrl_handler = &mt9t031->hdl;
  645. if (mt9t031->hdl.error)
  646. return mt9t031->hdl.error;
  647. v4l2_ctrl_auto_cluster(2, &mt9t031->autoexposure,
  648. V4L2_EXPOSURE_MANUAL, true);
  649. mt9t031->y_skip_top = 0;
  650. mt9t031->rect.left = MT9T031_COLUMN_SKIP;
  651. mt9t031->rect.top = MT9T031_ROW_SKIP;
  652. mt9t031->rect.width = MT9T031_MAX_WIDTH;
  653. mt9t031->rect.height = MT9T031_MAX_HEIGHT;
  654. mt9t031->xskip = 1;
  655. mt9t031->yskip = 1;
  656. ret = mt9t031_video_probe(client);
  657. if (ret)
  658. v4l2_ctrl_handler_free(&mt9t031->hdl);
  659. return ret;
  660. }
  661. static int mt9t031_remove(struct i2c_client *client)
  662. {
  663. struct mt9t031 *mt9t031 = to_mt9t031(client);
  664. v4l2_device_unregister_subdev(&mt9t031->subdev);
  665. v4l2_ctrl_handler_free(&mt9t031->hdl);
  666. return 0;
  667. }
  668. static const struct i2c_device_id mt9t031_id[] = {
  669. { "mt9t031", 0 },
  670. { }
  671. };
  672. MODULE_DEVICE_TABLE(i2c, mt9t031_id);
  673. static struct i2c_driver mt9t031_i2c_driver = {
  674. .driver = {
  675. .name = "mt9t031",
  676. },
  677. .probe = mt9t031_probe,
  678. .remove = mt9t031_remove,
  679. .id_table = mt9t031_id,
  680. };
  681. module_i2c_driver(mt9t031_i2c_driver);
  682. MODULE_DESCRIPTION("Micron MT9T031 Camera driver");
  683. MODULE_AUTHOR("Guennadi Liakhovetski <lg@denx.de>");
  684. MODULE_LICENSE("GPL v2");