mt9t031.c 23 KB

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