mt9t031.c 23 KB

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