mt9t031.c 23 KB

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