mt9t031.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858
  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-chip-ident.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 i2c_board_info and link to it from
  31. * struct soc_camera_link
  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. int model; /* V4L2_IDENT_MT9T031* codes from v4l2-chip-ident.h */
  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_disable(struct i2c_client *client)
  139. {
  140. /* Disable the chip */
  141. reg_clear(client, MT9T031_OUTPUT_CONTROL, 2);
  142. return 0;
  143. }
  144. static int mt9t031_s_stream(struct v4l2_subdev *sd, int enable)
  145. {
  146. struct i2c_client *client = v4l2_get_subdevdata(sd);
  147. int ret;
  148. if (enable)
  149. /* Switch to master "normal" mode */
  150. ret = reg_set(client, MT9T031_OUTPUT_CONTROL, 2);
  151. else
  152. /* Stop sensor readout */
  153. ret = reg_clear(client, MT9T031_OUTPUT_CONTROL, 2);
  154. if (ret < 0)
  155. return -EIO;
  156. return 0;
  157. }
  158. /* target must be _even_ */
  159. static u16 mt9t031_skip(s32 *source, s32 target, s32 max)
  160. {
  161. unsigned int skip;
  162. if (*source < target + target / 2) {
  163. *source = target;
  164. return 1;
  165. }
  166. skip = min(max, *source + target / 2) / target;
  167. if (skip > 8)
  168. skip = 8;
  169. *source = target * skip;
  170. return skip;
  171. }
  172. /* rect is the sensor rectangle, the caller guarantees parameter validity */
  173. static int mt9t031_set_params(struct i2c_client *client,
  174. struct v4l2_rect *rect, u16 xskip, u16 yskip)
  175. {
  176. struct mt9t031 *mt9t031 = to_mt9t031(client);
  177. int ret;
  178. u16 xbin, ybin;
  179. const u16 hblank = MT9T031_HORIZONTAL_BLANK,
  180. vblank = MT9T031_VERTICAL_BLANK;
  181. xbin = min(xskip, (u16)3);
  182. ybin = min(yskip, (u16)3);
  183. /*
  184. * Could just do roundup(rect->left, [xy]bin * 2); but this is cheaper.
  185. * There is always a valid suitably aligned value. The worst case is
  186. * xbin = 3, width = 2048. Then we will start at 36, the last read out
  187. * pixel will be 2083, which is < 2085 - first black pixel.
  188. *
  189. * MT9T031 datasheet imposes window left border alignment, depending on
  190. * the selected xskip. Failing to conform to this requirement produces
  191. * dark horizontal stripes in the image. However, even obeying to this
  192. * requirement doesn't eliminate the stripes in all configurations. They
  193. * appear "locally reproducibly," but can differ between tests under
  194. * different lighting conditions.
  195. */
  196. switch (xbin) {
  197. case 1:
  198. rect->left &= ~1;
  199. break;
  200. case 2:
  201. rect->left &= ~3;
  202. break;
  203. case 3:
  204. rect->left = rect->left > roundup(MT9T031_COLUMN_SKIP, 6) ?
  205. (rect->left / 6) * 6 : roundup(MT9T031_COLUMN_SKIP, 6);
  206. }
  207. rect->top &= ~1;
  208. dev_dbg(&client->dev, "skip %u:%u, rect %ux%u@%u:%u\n",
  209. xskip, yskip, rect->width, rect->height, rect->left, rect->top);
  210. /* Disable register update, reconfigure atomically */
  211. ret = reg_set(client, MT9T031_OUTPUT_CONTROL, 1);
  212. if (ret < 0)
  213. return ret;
  214. /* Blanking and start values - default... */
  215. ret = reg_write(client, MT9T031_HORIZONTAL_BLANKING, hblank);
  216. if (ret >= 0)
  217. ret = reg_write(client, MT9T031_VERTICAL_BLANKING, vblank);
  218. if (yskip != mt9t031->yskip || xskip != mt9t031->xskip) {
  219. /* Binning, skipping */
  220. if (ret >= 0)
  221. ret = reg_write(client, MT9T031_COLUMN_ADDRESS_MODE,
  222. ((xbin - 1) << 4) | (xskip - 1));
  223. if (ret >= 0)
  224. ret = reg_write(client, MT9T031_ROW_ADDRESS_MODE,
  225. ((ybin - 1) << 4) | (yskip - 1));
  226. }
  227. dev_dbg(&client->dev, "new physical left %u, top %u\n",
  228. rect->left, rect->top);
  229. /*
  230. * The caller provides a supported format, as guaranteed by
  231. * .try_mbus_fmt(), soc_camera_s_crop() and soc_camera_cropcap()
  232. */
  233. if (ret >= 0)
  234. ret = reg_write(client, MT9T031_COLUMN_START, rect->left);
  235. if (ret >= 0)
  236. ret = reg_write(client, MT9T031_ROW_START, rect->top);
  237. if (ret >= 0)
  238. ret = reg_write(client, MT9T031_WINDOW_WIDTH, rect->width - 1);
  239. if (ret >= 0)
  240. ret = reg_write(client, MT9T031_WINDOW_HEIGHT,
  241. rect->height + mt9t031->y_skip_top - 1);
  242. if (ret >= 0 && v4l2_ctrl_g_ctrl(mt9t031->autoexposure) == V4L2_EXPOSURE_AUTO) {
  243. mt9t031->total_h = rect->height + mt9t031->y_skip_top + vblank;
  244. ret = set_shutter(client, mt9t031->total_h);
  245. }
  246. /* Re-enable register update, commit all changes */
  247. if (ret >= 0)
  248. ret = reg_clear(client, MT9T031_OUTPUT_CONTROL, 1);
  249. if (ret >= 0) {
  250. mt9t031->rect = *rect;
  251. mt9t031->xskip = xskip;
  252. mt9t031->yskip = yskip;
  253. }
  254. return ret < 0 ? ret : 0;
  255. }
  256. static int mt9t031_s_crop(struct v4l2_subdev *sd, struct v4l2_crop *a)
  257. {
  258. struct v4l2_rect rect = a->c;
  259. struct i2c_client *client = v4l2_get_subdevdata(sd);
  260. struct mt9t031 *mt9t031 = to_mt9t031(client);
  261. rect.width = ALIGN(rect.width, 2);
  262. rect.height = ALIGN(rect.height, 2);
  263. soc_camera_limit_side(&rect.left, &rect.width,
  264. MT9T031_COLUMN_SKIP, MT9T031_MIN_WIDTH, MT9T031_MAX_WIDTH);
  265. soc_camera_limit_side(&rect.top, &rect.height,
  266. MT9T031_ROW_SKIP, MT9T031_MIN_HEIGHT, MT9T031_MAX_HEIGHT);
  267. return mt9t031_set_params(client, &rect, mt9t031->xskip, mt9t031->yskip);
  268. }
  269. static int mt9t031_g_crop(struct v4l2_subdev *sd, struct v4l2_crop *a)
  270. {
  271. struct i2c_client *client = v4l2_get_subdevdata(sd);
  272. struct mt9t031 *mt9t031 = to_mt9t031(client);
  273. a->c = mt9t031->rect;
  274. a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  275. return 0;
  276. }
  277. static int mt9t031_cropcap(struct v4l2_subdev *sd, struct v4l2_cropcap *a)
  278. {
  279. a->bounds.left = MT9T031_COLUMN_SKIP;
  280. a->bounds.top = MT9T031_ROW_SKIP;
  281. a->bounds.width = MT9T031_MAX_WIDTH;
  282. a->bounds.height = MT9T031_MAX_HEIGHT;
  283. a->defrect = a->bounds;
  284. a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  285. a->pixelaspect.numerator = 1;
  286. a->pixelaspect.denominator = 1;
  287. return 0;
  288. }
  289. static int mt9t031_g_fmt(struct v4l2_subdev *sd,
  290. struct v4l2_mbus_framefmt *mf)
  291. {
  292. struct i2c_client *client = v4l2_get_subdevdata(sd);
  293. struct mt9t031 *mt9t031 = to_mt9t031(client);
  294. mf->width = mt9t031->rect.width / mt9t031->xskip;
  295. mf->height = mt9t031->rect.height / mt9t031->yskip;
  296. mf->code = V4L2_MBUS_FMT_SBGGR10_1X10;
  297. mf->colorspace = V4L2_COLORSPACE_SRGB;
  298. mf->field = V4L2_FIELD_NONE;
  299. return 0;
  300. }
  301. static int mt9t031_s_fmt(struct v4l2_subdev *sd,
  302. struct v4l2_mbus_framefmt *mf)
  303. {
  304. struct i2c_client *client = v4l2_get_subdevdata(sd);
  305. struct mt9t031 *mt9t031 = to_mt9t031(client);
  306. u16 xskip, yskip;
  307. struct v4l2_rect rect = mt9t031->rect;
  308. /*
  309. * try_fmt has put width and height within limits.
  310. * S_FMT: use binning and skipping for scaling
  311. */
  312. xskip = mt9t031_skip(&rect.width, mf->width, MT9T031_MAX_WIDTH);
  313. yskip = mt9t031_skip(&rect.height, mf->height, MT9T031_MAX_HEIGHT);
  314. mf->code = V4L2_MBUS_FMT_SBGGR10_1X10;
  315. mf->colorspace = V4L2_COLORSPACE_SRGB;
  316. /* mt9t031_set_params() doesn't change width and height */
  317. return mt9t031_set_params(client, &rect, xskip, yskip);
  318. }
  319. /*
  320. * If a user window larger than sensor window is requested, we'll increase the
  321. * sensor window.
  322. */
  323. static int mt9t031_try_fmt(struct v4l2_subdev *sd,
  324. struct v4l2_mbus_framefmt *mf)
  325. {
  326. v4l_bound_align_image(
  327. &mf->width, MT9T031_MIN_WIDTH, MT9T031_MAX_WIDTH, 1,
  328. &mf->height, MT9T031_MIN_HEIGHT, MT9T031_MAX_HEIGHT, 1, 0);
  329. mf->code = V4L2_MBUS_FMT_SBGGR10_1X10;
  330. mf->colorspace = V4L2_COLORSPACE_SRGB;
  331. return 0;
  332. }
  333. static int mt9t031_g_chip_ident(struct v4l2_subdev *sd,
  334. struct v4l2_dbg_chip_ident *id)
  335. {
  336. struct i2c_client *client = v4l2_get_subdevdata(sd);
  337. struct mt9t031 *mt9t031 = to_mt9t031(client);
  338. if (id->match.type != V4L2_CHIP_MATCH_I2C_ADDR)
  339. return -EINVAL;
  340. if (id->match.addr != client->addr)
  341. return -ENODEV;
  342. id->ident = mt9t031->model;
  343. id->revision = 0;
  344. return 0;
  345. }
  346. #ifdef CONFIG_VIDEO_ADV_DEBUG
  347. static int mt9t031_g_register(struct v4l2_subdev *sd,
  348. struct v4l2_dbg_register *reg)
  349. {
  350. struct i2c_client *client = v4l2_get_subdevdata(sd);
  351. if (reg->match.type != V4L2_CHIP_MATCH_I2C_ADDR || reg->reg > 0xff)
  352. return -EINVAL;
  353. if (reg->match.addr != client->addr)
  354. return -ENODEV;
  355. reg->val = reg_read(client, reg->reg);
  356. if (reg->val > 0xffff)
  357. return -EIO;
  358. return 0;
  359. }
  360. static int mt9t031_s_register(struct v4l2_subdev *sd,
  361. struct v4l2_dbg_register *reg)
  362. {
  363. struct i2c_client *client = v4l2_get_subdevdata(sd);
  364. if (reg->match.type != V4L2_CHIP_MATCH_I2C_ADDR || reg->reg > 0xff)
  365. return -EINVAL;
  366. if (reg->match.addr != client->addr)
  367. return -ENODEV;
  368. if (reg_write(client, reg->reg, reg->val) < 0)
  369. return -EIO;
  370. return 0;
  371. }
  372. #endif
  373. static int mt9t031_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
  374. {
  375. struct mt9t031 *mt9t031 = container_of(ctrl->handler,
  376. struct mt9t031, hdl);
  377. const u32 shutter_max = MT9T031_MAX_HEIGHT + MT9T031_VERTICAL_BLANK;
  378. s32 min, max;
  379. switch (ctrl->id) {
  380. case V4L2_CID_EXPOSURE_AUTO:
  381. min = mt9t031->exposure->minimum;
  382. max = mt9t031->exposure->maximum;
  383. mt9t031->exposure->val =
  384. (shutter_max / 2 + (mt9t031->total_h - 1) * (max - min))
  385. / shutter_max + min;
  386. break;
  387. }
  388. return 0;
  389. }
  390. static int mt9t031_s_ctrl(struct v4l2_ctrl *ctrl)
  391. {
  392. struct mt9t031 *mt9t031 = container_of(ctrl->handler,
  393. struct mt9t031, hdl);
  394. struct v4l2_subdev *sd = &mt9t031->subdev;
  395. struct i2c_client *client = v4l2_get_subdevdata(sd);
  396. struct v4l2_ctrl *exp = mt9t031->exposure;
  397. int data;
  398. switch (ctrl->id) {
  399. case V4L2_CID_VFLIP:
  400. if (ctrl->val)
  401. data = reg_set(client, MT9T031_READ_MODE_2, 0x8000);
  402. else
  403. data = reg_clear(client, MT9T031_READ_MODE_2, 0x8000);
  404. if (data < 0)
  405. return -EIO;
  406. return 0;
  407. case V4L2_CID_HFLIP:
  408. if (ctrl->val)
  409. data = reg_set(client, MT9T031_READ_MODE_2, 0x4000);
  410. else
  411. data = reg_clear(client, MT9T031_READ_MODE_2, 0x4000);
  412. if (data < 0)
  413. return -EIO;
  414. return 0;
  415. case V4L2_CID_GAIN:
  416. /* See Datasheet Table 7, Gain settings. */
  417. if (ctrl->val <= ctrl->default_value) {
  418. /* Pack it into 0..1 step 0.125, register values 0..8 */
  419. unsigned long range = ctrl->default_value - ctrl->minimum;
  420. data = ((ctrl->val - ctrl->minimum) * 8 + range / 2) / range;
  421. dev_dbg(&client->dev, "Setting gain %d\n", data);
  422. data = reg_write(client, MT9T031_GLOBAL_GAIN, data);
  423. if (data < 0)
  424. return -EIO;
  425. } else {
  426. /* Pack it into 1.125..128 variable step, register values 9..0x7860 */
  427. /* We assume qctrl->maximum - qctrl->default_value - 1 > 0 */
  428. unsigned long range = ctrl->maximum - ctrl->default_value - 1;
  429. /* calculated gain: map 65..127 to 9..1024 step 0.125 */
  430. unsigned long gain = ((ctrl->val - ctrl->default_value - 1) *
  431. 1015 + range / 2) / range + 9;
  432. if (gain <= 32) /* calculated gain 9..32 -> 9..32 */
  433. data = gain;
  434. else if (gain <= 64) /* calculated gain 33..64 -> 0x51..0x60 */
  435. data = ((gain - 32) * 16 + 16) / 32 + 80;
  436. else
  437. /* calculated gain 65..1024 -> (1..120) << 8 + 0x60 */
  438. data = (((gain - 64 + 7) * 32) & 0xff00) | 0x60;
  439. dev_dbg(&client->dev, "Set gain from 0x%x to 0x%x\n",
  440. reg_read(client, MT9T031_GLOBAL_GAIN), data);
  441. data = reg_write(client, MT9T031_GLOBAL_GAIN, data);
  442. if (data < 0)
  443. return -EIO;
  444. }
  445. return 0;
  446. case V4L2_CID_EXPOSURE_AUTO:
  447. if (ctrl->val == V4L2_EXPOSURE_MANUAL) {
  448. unsigned int range = exp->maximum - exp->minimum;
  449. unsigned int shutter = ((exp->val - exp->minimum) * 1048 +
  450. range / 2) / range + 1;
  451. u32 old;
  452. get_shutter(client, &old);
  453. dev_dbg(&client->dev, "Set shutter from %u to %u\n",
  454. old, shutter);
  455. if (set_shutter(client, shutter) < 0)
  456. return -EIO;
  457. } else {
  458. const u16 vblank = MT9T031_VERTICAL_BLANK;
  459. mt9t031->total_h = mt9t031->rect.height +
  460. mt9t031->y_skip_top + vblank;
  461. if (set_shutter(client, mt9t031->total_h) < 0)
  462. return -EIO;
  463. }
  464. return 0;
  465. default:
  466. return -EINVAL;
  467. }
  468. return 0;
  469. }
  470. /*
  471. * Power Management:
  472. * This function does nothing for now but must be present for pm to work
  473. */
  474. static int mt9t031_runtime_suspend(struct device *dev)
  475. {
  476. return 0;
  477. }
  478. /*
  479. * Power Management:
  480. * COLUMN_ADDRESS_MODE and ROW_ADDRESS_MODE are not rewritten if unchanged
  481. * they are however changed at reset if the platform hook is present
  482. * thus we rewrite them with the values stored by the driver
  483. */
  484. static int mt9t031_runtime_resume(struct device *dev)
  485. {
  486. struct video_device *vdev = to_video_device(dev);
  487. struct v4l2_subdev *sd = soc_camera_vdev_to_subdev(vdev);
  488. struct i2c_client *client = v4l2_get_subdevdata(sd);
  489. struct mt9t031 *mt9t031 = to_mt9t031(client);
  490. int ret;
  491. u16 xbin, ybin;
  492. xbin = min(mt9t031->xskip, (u16)3);
  493. ybin = min(mt9t031->yskip, (u16)3);
  494. ret = reg_write(client, MT9T031_COLUMN_ADDRESS_MODE,
  495. ((xbin - 1) << 4) | (mt9t031->xskip - 1));
  496. if (ret < 0)
  497. return ret;
  498. ret = reg_write(client, MT9T031_ROW_ADDRESS_MODE,
  499. ((ybin - 1) << 4) | (mt9t031->yskip - 1));
  500. if (ret < 0)
  501. return ret;
  502. return 0;
  503. }
  504. static struct dev_pm_ops mt9t031_dev_pm_ops = {
  505. .runtime_suspend = mt9t031_runtime_suspend,
  506. .runtime_resume = mt9t031_runtime_resume,
  507. };
  508. static struct device_type mt9t031_dev_type = {
  509. .name = "MT9T031",
  510. .pm = &mt9t031_dev_pm_ops,
  511. };
  512. static int mt9t031_s_power(struct v4l2_subdev *sd, int on)
  513. {
  514. struct i2c_client *client = v4l2_get_subdevdata(sd);
  515. struct video_device *vdev = soc_camera_i2c_to_vdev(client);
  516. if (on)
  517. vdev->dev.type = &mt9t031_dev_type;
  518. else
  519. vdev->dev.type = NULL;
  520. return 0;
  521. }
  522. /*
  523. * Interface active, can use i2c. If it fails, it can indeed mean, that
  524. * this wasn't our capture interface, so, we wait for the right one
  525. */
  526. static int mt9t031_video_probe(struct i2c_client *client)
  527. {
  528. struct mt9t031 *mt9t031 = to_mt9t031(client);
  529. s32 data;
  530. int ret;
  531. /* Enable the chip */
  532. data = reg_write(client, MT9T031_CHIP_ENABLE, 1);
  533. dev_dbg(&client->dev, "write: %d\n", data);
  534. /* Read out the chip version register */
  535. data = reg_read(client, MT9T031_CHIP_VERSION);
  536. switch (data) {
  537. case 0x1621:
  538. mt9t031->model = V4L2_IDENT_MT9T031;
  539. break;
  540. default:
  541. dev_err(&client->dev,
  542. "No MT9T031 chip detected, register read %x\n", data);
  543. return -ENODEV;
  544. }
  545. dev_info(&client->dev, "Detected a MT9T031 chip ID %x\n", data);
  546. ret = mt9t031_idle(client);
  547. if (ret < 0)
  548. dev_err(&client->dev, "Failed to initialise the camera\n");
  549. else
  550. v4l2_ctrl_handler_setup(&mt9t031->hdl);
  551. return ret;
  552. }
  553. static int mt9t031_g_skip_top_lines(struct v4l2_subdev *sd, u32 *lines)
  554. {
  555. struct i2c_client *client = v4l2_get_subdevdata(sd);
  556. struct mt9t031 *mt9t031 = to_mt9t031(client);
  557. *lines = mt9t031->y_skip_top;
  558. return 0;
  559. }
  560. static const struct v4l2_ctrl_ops mt9t031_ctrl_ops = {
  561. .g_volatile_ctrl = mt9t031_g_volatile_ctrl,
  562. .s_ctrl = mt9t031_s_ctrl,
  563. };
  564. static struct v4l2_subdev_core_ops mt9t031_subdev_core_ops = {
  565. .g_chip_ident = mt9t031_g_chip_ident,
  566. .s_power = mt9t031_s_power,
  567. #ifdef CONFIG_VIDEO_ADV_DEBUG
  568. .g_register = mt9t031_g_register,
  569. .s_register = mt9t031_s_register,
  570. #endif
  571. };
  572. static int mt9t031_enum_fmt(struct v4l2_subdev *sd, unsigned int index,
  573. enum v4l2_mbus_pixelcode *code)
  574. {
  575. if (index)
  576. return -EINVAL;
  577. *code = V4L2_MBUS_FMT_SBGGR10_1X10;
  578. return 0;
  579. }
  580. static int mt9t031_g_mbus_config(struct v4l2_subdev *sd,
  581. struct v4l2_mbus_config *cfg)
  582. {
  583. struct i2c_client *client = v4l2_get_subdevdata(sd);
  584. struct soc_camera_link *icl = soc_camera_i2c_to_link(client);
  585. cfg->flags = V4L2_MBUS_MASTER | V4L2_MBUS_PCLK_SAMPLE_RISING |
  586. V4L2_MBUS_PCLK_SAMPLE_FALLING | V4L2_MBUS_HSYNC_ACTIVE_HIGH |
  587. V4L2_MBUS_VSYNC_ACTIVE_HIGH | V4L2_MBUS_DATA_ACTIVE_HIGH;
  588. cfg->type = V4L2_MBUS_PARALLEL;
  589. cfg->flags = soc_camera_apply_board_flags(icl, cfg);
  590. return 0;
  591. }
  592. static int mt9t031_s_mbus_config(struct v4l2_subdev *sd,
  593. const struct v4l2_mbus_config *cfg)
  594. {
  595. struct i2c_client *client = v4l2_get_subdevdata(sd);
  596. struct soc_camera_link *icl = soc_camera_i2c_to_link(client);
  597. if (soc_camera_apply_board_flags(icl, cfg) &
  598. V4L2_MBUS_PCLK_SAMPLE_FALLING)
  599. return reg_clear(client, MT9T031_PIXEL_CLOCK_CONTROL, 0x8000);
  600. else
  601. return reg_set(client, MT9T031_PIXEL_CLOCK_CONTROL, 0x8000);
  602. }
  603. static struct v4l2_subdev_video_ops mt9t031_subdev_video_ops = {
  604. .s_stream = mt9t031_s_stream,
  605. .s_mbus_fmt = mt9t031_s_fmt,
  606. .g_mbus_fmt = mt9t031_g_fmt,
  607. .try_mbus_fmt = mt9t031_try_fmt,
  608. .s_crop = mt9t031_s_crop,
  609. .g_crop = mt9t031_g_crop,
  610. .cropcap = mt9t031_cropcap,
  611. .enum_mbus_fmt = mt9t031_enum_fmt,
  612. .g_mbus_config = mt9t031_g_mbus_config,
  613. .s_mbus_config = mt9t031_s_mbus_config,
  614. };
  615. static struct v4l2_subdev_sensor_ops mt9t031_subdev_sensor_ops = {
  616. .g_skip_top_lines = mt9t031_g_skip_top_lines,
  617. };
  618. static struct v4l2_subdev_ops mt9t031_subdev_ops = {
  619. .core = &mt9t031_subdev_core_ops,
  620. .video = &mt9t031_subdev_video_ops,
  621. .sensor = &mt9t031_subdev_sensor_ops,
  622. };
  623. static int mt9t031_probe(struct i2c_client *client,
  624. const struct i2c_device_id *did)
  625. {
  626. struct mt9t031 *mt9t031;
  627. struct soc_camera_link *icl = soc_camera_i2c_to_link(client);
  628. struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
  629. int ret;
  630. if (!icl) {
  631. dev_err(&client->dev, "MT9T031 driver needs platform data\n");
  632. return -EINVAL;
  633. }
  634. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WORD_DATA)) {
  635. dev_warn(&adapter->dev,
  636. "I2C-Adapter doesn't support I2C_FUNC_SMBUS_WORD\n");
  637. return -EIO;
  638. }
  639. mt9t031 = kzalloc(sizeof(struct mt9t031), GFP_KERNEL);
  640. if (!mt9t031)
  641. return -ENOMEM;
  642. v4l2_i2c_subdev_init(&mt9t031->subdev, client, &mt9t031_subdev_ops);
  643. v4l2_ctrl_handler_init(&mt9t031->hdl, 5);
  644. v4l2_ctrl_new_std(&mt9t031->hdl, &mt9t031_ctrl_ops,
  645. V4L2_CID_VFLIP, 0, 1, 1, 0);
  646. v4l2_ctrl_new_std(&mt9t031->hdl, &mt9t031_ctrl_ops,
  647. V4L2_CID_HFLIP, 0, 1, 1, 0);
  648. v4l2_ctrl_new_std(&mt9t031->hdl, &mt9t031_ctrl_ops,
  649. V4L2_CID_GAIN, 0, 127, 1, 64);
  650. /*
  651. * Simulated autoexposure. If enabled, we calculate shutter width
  652. * ourselves in the driver based on vertical blanking and frame width
  653. */
  654. mt9t031->autoexposure = v4l2_ctrl_new_std_menu(&mt9t031->hdl,
  655. &mt9t031_ctrl_ops, V4L2_CID_EXPOSURE_AUTO, 1, 0,
  656. V4L2_EXPOSURE_AUTO);
  657. mt9t031->exposure = v4l2_ctrl_new_std(&mt9t031->hdl, &mt9t031_ctrl_ops,
  658. V4L2_CID_EXPOSURE, 1, 255, 1, 255);
  659. mt9t031->subdev.ctrl_handler = &mt9t031->hdl;
  660. if (mt9t031->hdl.error) {
  661. int err = mt9t031->hdl.error;
  662. kfree(mt9t031);
  663. return err;
  664. }
  665. v4l2_ctrl_auto_cluster(2, &mt9t031->autoexposure,
  666. V4L2_EXPOSURE_MANUAL, true);
  667. mt9t031->y_skip_top = 0;
  668. mt9t031->rect.left = MT9T031_COLUMN_SKIP;
  669. mt9t031->rect.top = MT9T031_ROW_SKIP;
  670. mt9t031->rect.width = MT9T031_MAX_WIDTH;
  671. mt9t031->rect.height = MT9T031_MAX_HEIGHT;
  672. mt9t031->xskip = 1;
  673. mt9t031->yskip = 1;
  674. mt9t031_idle(client);
  675. ret = mt9t031_video_probe(client);
  676. mt9t031_disable(client);
  677. if (ret) {
  678. v4l2_ctrl_handler_free(&mt9t031->hdl);
  679. kfree(mt9t031);
  680. }
  681. return ret;
  682. }
  683. static int mt9t031_remove(struct i2c_client *client)
  684. {
  685. struct mt9t031 *mt9t031 = to_mt9t031(client);
  686. v4l2_device_unregister_subdev(&mt9t031->subdev);
  687. v4l2_ctrl_handler_free(&mt9t031->hdl);
  688. kfree(mt9t031);
  689. return 0;
  690. }
  691. static const struct i2c_device_id mt9t031_id[] = {
  692. { "mt9t031", 0 },
  693. { }
  694. };
  695. MODULE_DEVICE_TABLE(i2c, mt9t031_id);
  696. static struct i2c_driver mt9t031_i2c_driver = {
  697. .driver = {
  698. .name = "mt9t031",
  699. },
  700. .probe = mt9t031_probe,
  701. .remove = mt9t031_remove,
  702. .id_table = mt9t031_id,
  703. };
  704. module_i2c_driver(mt9t031_i2c_driver);
  705. MODULE_DESCRIPTION("Micron MT9T031 Camera driver");
  706. MODULE_AUTHOR("Guennadi Liakhovetski <lg@denx.de>");
  707. MODULE_LICENSE("GPL v2");