mt9t031.c 25 KB

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