mt9t031.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799
  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/videodev2.h>
  11. #include <linux/slab.h>
  12. #include <linux/i2c.h>
  13. #include <linux/log2.h>
  14. #include <media/v4l2-common.h>
  15. #include <media/v4l2-chip-ident.h>
  16. #include <media/soc_camera.h>
  17. /* mt9t031 i2c address 0x5d
  18. * The platform has to define i2c_board_info
  19. * and call i2c_register_board_info() */
  20. /* mt9t031 selected register addresses */
  21. #define MT9T031_CHIP_VERSION 0x00
  22. #define MT9T031_ROW_START 0x01
  23. #define MT9T031_COLUMN_START 0x02
  24. #define MT9T031_WINDOW_HEIGHT 0x03
  25. #define MT9T031_WINDOW_WIDTH 0x04
  26. #define MT9T031_HORIZONTAL_BLANKING 0x05
  27. #define MT9T031_VERTICAL_BLANKING 0x06
  28. #define MT9T031_OUTPUT_CONTROL 0x07
  29. #define MT9T031_SHUTTER_WIDTH_UPPER 0x08
  30. #define MT9T031_SHUTTER_WIDTH 0x09
  31. #define MT9T031_PIXEL_CLOCK_CONTROL 0x0a
  32. #define MT9T031_FRAME_RESTART 0x0b
  33. #define MT9T031_SHUTTER_DELAY 0x0c
  34. #define MT9T031_RESET 0x0d
  35. #define MT9T031_READ_MODE_1 0x1e
  36. #define MT9T031_READ_MODE_2 0x20
  37. #define MT9T031_READ_MODE_3 0x21
  38. #define MT9T031_ROW_ADDRESS_MODE 0x22
  39. #define MT9T031_COLUMN_ADDRESS_MODE 0x23
  40. #define MT9T031_GLOBAL_GAIN 0x35
  41. #define MT9T031_CHIP_ENABLE 0xF8
  42. #define MT9T031_MAX_HEIGHT 1536
  43. #define MT9T031_MAX_WIDTH 2048
  44. #define MT9T031_MIN_HEIGHT 2
  45. #define MT9T031_MIN_WIDTH 2
  46. #define MT9T031_HORIZONTAL_BLANK 142
  47. #define MT9T031_VERTICAL_BLANK 25
  48. #define MT9T031_COLUMN_SKIP 32
  49. #define MT9T031_ROW_SKIP 20
  50. #define MT9T031_BUS_PARAM (SOCAM_PCLK_SAMPLE_RISING | \
  51. SOCAM_PCLK_SAMPLE_FALLING | SOCAM_HSYNC_ACTIVE_HIGH | \
  52. SOCAM_VSYNC_ACTIVE_HIGH | SOCAM_DATA_ACTIVE_HIGH | \
  53. SOCAM_MASTER | SOCAM_DATAWIDTH_10)
  54. static const struct soc_camera_data_format mt9t031_colour_formats[] = {
  55. {
  56. .name = "Bayer (sRGB) 10 bit",
  57. .depth = 10,
  58. .fourcc = V4L2_PIX_FMT_SGRBG10,
  59. .colorspace = V4L2_COLORSPACE_SRGB,
  60. }
  61. };
  62. struct mt9t031 {
  63. struct i2c_client *client;
  64. struct soc_camera_device icd;
  65. int model; /* V4L2_IDENT_MT9T031* codes from v4l2-chip-ident.h */
  66. unsigned char autoexposure;
  67. u16 xskip;
  68. u16 yskip;
  69. };
  70. static int reg_read(struct soc_camera_device *icd, const u8 reg)
  71. {
  72. struct mt9t031 *mt9t031 = container_of(icd, struct mt9t031, icd);
  73. struct i2c_client *client = mt9t031->client;
  74. s32 data = i2c_smbus_read_word_data(client, reg);
  75. return data < 0 ? data : swab16(data);
  76. }
  77. static int reg_write(struct soc_camera_device *icd, const u8 reg,
  78. const u16 data)
  79. {
  80. struct mt9t031 *mt9t031 = container_of(icd, struct mt9t031, icd);
  81. return i2c_smbus_write_word_data(mt9t031->client, reg, swab16(data));
  82. }
  83. static int reg_set(struct soc_camera_device *icd, const u8 reg,
  84. const u16 data)
  85. {
  86. int ret;
  87. ret = reg_read(icd, reg);
  88. if (ret < 0)
  89. return ret;
  90. return reg_write(icd, reg, ret | data);
  91. }
  92. static int reg_clear(struct soc_camera_device *icd, const u8 reg,
  93. const u16 data)
  94. {
  95. int ret;
  96. ret = reg_read(icd, reg);
  97. if (ret < 0)
  98. return ret;
  99. return reg_write(icd, reg, ret & ~data);
  100. }
  101. static int set_shutter(struct soc_camera_device *icd, const u32 data)
  102. {
  103. int ret;
  104. ret = reg_write(icd, MT9T031_SHUTTER_WIDTH_UPPER, data >> 16);
  105. if (ret >= 0)
  106. ret = reg_write(icd, MT9T031_SHUTTER_WIDTH, data & 0xffff);
  107. return ret;
  108. }
  109. static int get_shutter(struct soc_camera_device *icd, u32 *data)
  110. {
  111. int ret;
  112. ret = reg_read(icd, MT9T031_SHUTTER_WIDTH_UPPER);
  113. *data = ret << 16;
  114. if (ret >= 0)
  115. ret = reg_read(icd, MT9T031_SHUTTER_WIDTH);
  116. *data |= ret & 0xffff;
  117. return ret < 0 ? ret : 0;
  118. }
  119. static int mt9t031_init(struct soc_camera_device *icd)
  120. {
  121. int ret;
  122. /* Disable chip output, synchronous option update */
  123. dev_dbg(icd->vdev->parent, "%s\n", __func__);
  124. ret = reg_write(icd, MT9T031_RESET, 1);
  125. if (ret >= 0)
  126. ret = reg_write(icd, MT9T031_RESET, 0);
  127. if (ret >= 0)
  128. ret = reg_clear(icd, MT9T031_OUTPUT_CONTROL, 2);
  129. return ret >= 0 ? 0 : -EIO;
  130. }
  131. static int mt9t031_release(struct soc_camera_device *icd)
  132. {
  133. /* Disable the chip */
  134. reg_clear(icd, MT9T031_OUTPUT_CONTROL, 2);
  135. return 0;
  136. }
  137. static int mt9t031_start_capture(struct soc_camera_device *icd)
  138. {
  139. /* Switch to master "normal" mode */
  140. if (reg_set(icd, MT9T031_OUTPUT_CONTROL, 2) < 0)
  141. return -EIO;
  142. return 0;
  143. }
  144. static int mt9t031_stop_capture(struct soc_camera_device *icd)
  145. {
  146. /* Stop sensor readout */
  147. if (reg_clear(icd, MT9T031_OUTPUT_CONTROL, 2) < 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. /* The caller should have queried our parameters, check anyway */
  155. if (flags & ~MT9T031_BUS_PARAM)
  156. return -EINVAL;
  157. if (flags & SOCAM_PCLK_SAMPLE_FALLING)
  158. reg_set(icd, MT9T031_PIXEL_CLOCK_CONTROL, 0x8000);
  159. else
  160. reg_clear(icd, MT9T031_PIXEL_CLOCK_CONTROL, 0x8000);
  161. return 0;
  162. }
  163. static unsigned long mt9t031_query_bus_param(struct soc_camera_device *icd)
  164. {
  165. struct mt9t031 *mt9t031 = container_of(icd, struct mt9t031, icd);
  166. struct soc_camera_link *icl = mt9t031->client->dev.platform_data;
  167. return soc_camera_apply_sensor_flags(icl, MT9T031_BUS_PARAM);
  168. }
  169. /* Round up minima and round down maxima */
  170. static void recalculate_limits(struct soc_camera_device *icd,
  171. u16 xskip, u16 yskip)
  172. {
  173. icd->x_min = (MT9T031_COLUMN_SKIP + xskip - 1) / xskip;
  174. icd->y_min = (MT9T031_ROW_SKIP + yskip - 1) / yskip;
  175. icd->width_min = (MT9T031_MIN_WIDTH + xskip - 1) / xskip;
  176. icd->height_min = (MT9T031_MIN_HEIGHT + yskip - 1) / yskip;
  177. icd->width_max = MT9T031_MAX_WIDTH / xskip;
  178. icd->height_max = MT9T031_MAX_HEIGHT / yskip;
  179. }
  180. static int mt9t031_set_params(struct soc_camera_device *icd,
  181. struct v4l2_rect *rect, u16 xskip, u16 yskip)
  182. {
  183. struct mt9t031 *mt9t031 = container_of(icd, struct mt9t031, icd);
  184. int ret;
  185. u16 xbin, ybin, width, height, left, top;
  186. const u16 hblank = MT9T031_HORIZONTAL_BLANK,
  187. vblank = MT9T031_VERTICAL_BLANK;
  188. /* Make sure we don't exceed sensor limits */
  189. if (rect->left + rect->width > icd->width_max)
  190. rect->left = (icd->width_max - rect->width) / 2 + icd->x_min;
  191. if (rect->top + rect->height > icd->height_max)
  192. rect->top = (icd->height_max - rect->height) / 2 + icd->y_min;
  193. width = rect->width * xskip;
  194. height = rect->height * yskip;
  195. left = rect->left * xskip;
  196. top = rect->top * yskip;
  197. xbin = min(xskip, (u16)3);
  198. ybin = min(yskip, (u16)3);
  199. dev_dbg(&icd->dev, "xskip %u, width %u/%u, yskip %u, height %u/%u\n",
  200. xskip, width, rect->width, yskip, height, rect->height);
  201. /* Could just do roundup(rect->left, [xy]bin * 2); but this is cheaper */
  202. switch (xbin) {
  203. case 2:
  204. left = (left + 3) & ~3;
  205. break;
  206. case 3:
  207. left = roundup(left, 6);
  208. }
  209. switch (ybin) {
  210. case 2:
  211. top = (top + 3) & ~3;
  212. break;
  213. case 3:
  214. top = roundup(top, 6);
  215. }
  216. /* Disable register update, reconfigure atomically */
  217. ret = reg_set(icd, MT9T031_OUTPUT_CONTROL, 1);
  218. if (ret < 0)
  219. return ret;
  220. /* Blanking and start values - default... */
  221. ret = reg_write(icd, MT9T031_HORIZONTAL_BLANKING, hblank);
  222. if (ret >= 0)
  223. ret = reg_write(icd, MT9T031_VERTICAL_BLANKING, vblank);
  224. if (yskip != mt9t031->yskip || xskip != mt9t031->xskip) {
  225. /* Binning, skipping */
  226. if (ret >= 0)
  227. ret = reg_write(icd, MT9T031_COLUMN_ADDRESS_MODE,
  228. ((xbin - 1) << 4) | (xskip - 1));
  229. if (ret >= 0)
  230. ret = reg_write(icd, MT9T031_ROW_ADDRESS_MODE,
  231. ((ybin - 1) << 4) | (yskip - 1));
  232. }
  233. dev_dbg(&icd->dev, "new physical left %u, top %u\n", left, top);
  234. /* The caller provides a supported format, as guaranteed by
  235. * icd->try_fmt_cap(), soc_camera_s_crop() and soc_camera_cropcap() */
  236. if (ret >= 0)
  237. ret = reg_write(icd, MT9T031_COLUMN_START, left);
  238. if (ret >= 0)
  239. ret = reg_write(icd, MT9T031_ROW_START, top);
  240. if (ret >= 0)
  241. ret = reg_write(icd, MT9T031_WINDOW_WIDTH, width - 1);
  242. if (ret >= 0)
  243. ret = reg_write(icd, MT9T031_WINDOW_HEIGHT,
  244. height + icd->y_skip_top - 1);
  245. if (ret >= 0 && mt9t031->autoexposure) {
  246. ret = set_shutter(icd, height + icd->y_skip_top + vblank);
  247. if (ret >= 0) {
  248. const u32 shutter_max = MT9T031_MAX_HEIGHT + vblank;
  249. const struct v4l2_queryctrl *qctrl =
  250. soc_camera_find_qctrl(icd->ops,
  251. V4L2_CID_EXPOSURE);
  252. icd->exposure = (shutter_max / 2 + (height +
  253. icd->y_skip_top + vblank - 1) *
  254. (qctrl->maximum - qctrl->minimum)) /
  255. shutter_max + qctrl->minimum;
  256. }
  257. }
  258. /* Re-enable register update, commit all changes */
  259. if (ret >= 0)
  260. ret = reg_clear(icd, MT9T031_OUTPUT_CONTROL, 1);
  261. return ret < 0 ? ret : 0;
  262. }
  263. static int mt9t031_set_crop(struct soc_camera_device *icd,
  264. struct v4l2_rect *rect)
  265. {
  266. struct mt9t031 *mt9t031 = container_of(icd, struct mt9t031, icd);
  267. /* CROP - no change in scaling, or in limits */
  268. return mt9t031_set_params(icd, rect, mt9t031->xskip, mt9t031->yskip);
  269. }
  270. static int mt9t031_set_fmt(struct soc_camera_device *icd,
  271. struct v4l2_format *f)
  272. {
  273. struct mt9t031 *mt9t031 = container_of(icd, struct mt9t031, icd);
  274. int ret;
  275. u16 xskip, yskip;
  276. struct v4l2_rect rect = {
  277. .left = icd->x_current,
  278. .top = icd->y_current,
  279. .width = f->fmt.pix.width,
  280. .height = f->fmt.pix.height,
  281. };
  282. /*
  283. * try_fmt has put rectangle within limits.
  284. * S_FMT - use binning and skipping for scaling, recalculate
  285. * limits, used for cropping
  286. */
  287. /* Is this more optimal than just a division? */
  288. for (xskip = 8; xskip > 1; xskip--)
  289. if (rect.width * xskip <= MT9T031_MAX_WIDTH)
  290. break;
  291. for (yskip = 8; yskip > 1; yskip--)
  292. if (rect.height * yskip <= MT9T031_MAX_HEIGHT)
  293. break;
  294. recalculate_limits(icd, xskip, yskip);
  295. ret = mt9t031_set_params(icd, &rect, xskip, yskip);
  296. if (!ret) {
  297. mt9t031->xskip = xskip;
  298. mt9t031->yskip = yskip;
  299. }
  300. return ret;
  301. }
  302. static int mt9t031_try_fmt(struct soc_camera_device *icd,
  303. struct v4l2_format *f)
  304. {
  305. struct v4l2_pix_format *pix = &f->fmt.pix;
  306. if (pix->height < MT9T031_MIN_HEIGHT)
  307. pix->height = MT9T031_MIN_HEIGHT;
  308. if (pix->height > MT9T031_MAX_HEIGHT)
  309. pix->height = MT9T031_MAX_HEIGHT;
  310. if (pix->width < MT9T031_MIN_WIDTH)
  311. pix->width = MT9T031_MIN_WIDTH;
  312. if (pix->width > MT9T031_MAX_WIDTH)
  313. pix->width = MT9T031_MAX_WIDTH;
  314. pix->width &= ~0x01; /* has to be even */
  315. pix->height &= ~0x01; /* has to be even */
  316. return 0;
  317. }
  318. static int mt9t031_get_chip_id(struct soc_camera_device *icd,
  319. struct v4l2_dbg_chip_ident *id)
  320. {
  321. struct mt9t031 *mt9t031 = container_of(icd, struct mt9t031, icd);
  322. if (id->match.type != V4L2_CHIP_MATCH_I2C_ADDR)
  323. return -EINVAL;
  324. if (id->match.addr != mt9t031->client->addr)
  325. return -ENODEV;
  326. id->ident = mt9t031->model;
  327. id->revision = 0;
  328. return 0;
  329. }
  330. #ifdef CONFIG_VIDEO_ADV_DEBUG
  331. static int mt9t031_get_register(struct soc_camera_device *icd,
  332. struct v4l2_dbg_register *reg)
  333. {
  334. struct mt9t031 *mt9t031 = container_of(icd, struct mt9t031, icd);
  335. if (reg->match.type != V4L2_CHIP_MATCH_I2C_ADDR || reg->reg > 0xff)
  336. return -EINVAL;
  337. if (reg->match.addr != mt9t031->client->addr)
  338. return -ENODEV;
  339. reg->val = reg_read(icd, reg->reg);
  340. if (reg->val > 0xffff)
  341. return -EIO;
  342. return 0;
  343. }
  344. static int mt9t031_set_register(struct soc_camera_device *icd,
  345. struct v4l2_dbg_register *reg)
  346. {
  347. struct mt9t031 *mt9t031 = container_of(icd, struct mt9t031, icd);
  348. if (reg->match.type != V4L2_CHIP_MATCH_I2C_ADDR || reg->reg > 0xff)
  349. return -EINVAL;
  350. if (reg->match.addr != mt9t031->client->addr)
  351. return -ENODEV;
  352. if (reg_write(icd, reg->reg, reg->val) < 0)
  353. return -EIO;
  354. return 0;
  355. }
  356. #endif
  357. static const struct v4l2_queryctrl mt9t031_controls[] = {
  358. {
  359. .id = V4L2_CID_VFLIP,
  360. .type = V4L2_CTRL_TYPE_BOOLEAN,
  361. .name = "Flip Vertically",
  362. .minimum = 0,
  363. .maximum = 1,
  364. .step = 1,
  365. .default_value = 0,
  366. }, {
  367. .id = V4L2_CID_HFLIP,
  368. .type = V4L2_CTRL_TYPE_BOOLEAN,
  369. .name = "Flip Horizontally",
  370. .minimum = 0,
  371. .maximum = 1,
  372. .step = 1,
  373. .default_value = 0,
  374. }, {
  375. .id = V4L2_CID_GAIN,
  376. .type = V4L2_CTRL_TYPE_INTEGER,
  377. .name = "Gain",
  378. .minimum = 0,
  379. .maximum = 127,
  380. .step = 1,
  381. .default_value = 64,
  382. .flags = V4L2_CTRL_FLAG_SLIDER,
  383. }, {
  384. .id = V4L2_CID_EXPOSURE,
  385. .type = V4L2_CTRL_TYPE_INTEGER,
  386. .name = "Exposure",
  387. .minimum = 1,
  388. .maximum = 255,
  389. .step = 1,
  390. .default_value = 255,
  391. .flags = V4L2_CTRL_FLAG_SLIDER,
  392. }, {
  393. .id = V4L2_CID_EXPOSURE_AUTO,
  394. .type = V4L2_CTRL_TYPE_BOOLEAN,
  395. .name = "Automatic Exposure",
  396. .minimum = 0,
  397. .maximum = 1,
  398. .step = 1,
  399. .default_value = 1,
  400. }
  401. };
  402. static int mt9t031_video_probe(struct soc_camera_device *);
  403. static void mt9t031_video_remove(struct soc_camera_device *);
  404. static int mt9t031_get_control(struct soc_camera_device *, struct v4l2_control *);
  405. static int mt9t031_set_control(struct soc_camera_device *, struct v4l2_control *);
  406. static struct soc_camera_ops mt9t031_ops = {
  407. .owner = THIS_MODULE,
  408. .probe = mt9t031_video_probe,
  409. .remove = mt9t031_video_remove,
  410. .init = mt9t031_init,
  411. .release = mt9t031_release,
  412. .start_capture = mt9t031_start_capture,
  413. .stop_capture = mt9t031_stop_capture,
  414. .set_crop = mt9t031_set_crop,
  415. .set_fmt = mt9t031_set_fmt,
  416. .try_fmt = mt9t031_try_fmt,
  417. .set_bus_param = mt9t031_set_bus_param,
  418. .query_bus_param = mt9t031_query_bus_param,
  419. .controls = mt9t031_controls,
  420. .num_controls = ARRAY_SIZE(mt9t031_controls),
  421. .get_control = mt9t031_get_control,
  422. .set_control = mt9t031_set_control,
  423. .get_chip_id = mt9t031_get_chip_id,
  424. #ifdef CONFIG_VIDEO_ADV_DEBUG
  425. .get_register = mt9t031_get_register,
  426. .set_register = mt9t031_set_register,
  427. #endif
  428. };
  429. static int mt9t031_get_control(struct soc_camera_device *icd, struct v4l2_control *ctrl)
  430. {
  431. struct mt9t031 *mt9t031 = container_of(icd, struct mt9t031, icd);
  432. int data;
  433. switch (ctrl->id) {
  434. case V4L2_CID_VFLIP:
  435. data = reg_read(icd, MT9T031_READ_MODE_2);
  436. if (data < 0)
  437. return -EIO;
  438. ctrl->value = !!(data & 0x8000);
  439. break;
  440. case V4L2_CID_HFLIP:
  441. data = reg_read(icd, MT9T031_READ_MODE_2);
  442. if (data < 0)
  443. return -EIO;
  444. ctrl->value = !!(data & 0x4000);
  445. break;
  446. case V4L2_CID_EXPOSURE_AUTO:
  447. ctrl->value = mt9t031->autoexposure;
  448. break;
  449. }
  450. return 0;
  451. }
  452. static int mt9t031_set_control(struct soc_camera_device *icd, struct v4l2_control *ctrl)
  453. {
  454. struct mt9t031 *mt9t031 = container_of(icd, struct mt9t031, icd);
  455. const struct v4l2_queryctrl *qctrl;
  456. int data;
  457. qctrl = soc_camera_find_qctrl(&mt9t031_ops, ctrl->id);
  458. if (!qctrl)
  459. return -EINVAL;
  460. switch (ctrl->id) {
  461. case V4L2_CID_VFLIP:
  462. if (ctrl->value)
  463. data = reg_set(icd, MT9T031_READ_MODE_2, 0x8000);
  464. else
  465. data = reg_clear(icd, MT9T031_READ_MODE_2, 0x8000);
  466. if (data < 0)
  467. return -EIO;
  468. break;
  469. case V4L2_CID_HFLIP:
  470. if (ctrl->value)
  471. data = reg_set(icd, MT9T031_READ_MODE_2, 0x4000);
  472. else
  473. data = reg_clear(icd, MT9T031_READ_MODE_2, 0x4000);
  474. if (data < 0)
  475. return -EIO;
  476. break;
  477. case V4L2_CID_GAIN:
  478. if (ctrl->value > qctrl->maximum || ctrl->value < qctrl->minimum)
  479. return -EINVAL;
  480. /* See Datasheet Table 7, Gain settings. */
  481. if (ctrl->value <= qctrl->default_value) {
  482. /* Pack it into 0..1 step 0.125, register values 0..8 */
  483. unsigned long range = qctrl->default_value - qctrl->minimum;
  484. data = ((ctrl->value - qctrl->minimum) * 8 + range / 2) / range;
  485. dev_dbg(&icd->dev, "Setting gain %d\n", data);
  486. data = reg_write(icd, MT9T031_GLOBAL_GAIN, data);
  487. if (data < 0)
  488. return -EIO;
  489. } else {
  490. /* Pack it into 1.125..128 variable step, register values 9..0x7860 */
  491. /* We assume qctrl->maximum - qctrl->default_value - 1 > 0 */
  492. unsigned long range = qctrl->maximum - qctrl->default_value - 1;
  493. /* calculated gain: map 65..127 to 9..1024 step 0.125 */
  494. unsigned long gain = ((ctrl->value - qctrl->default_value - 1) *
  495. 1015 + range / 2) / range + 9;
  496. if (gain <= 32) /* calculated gain 9..32 -> 9..32 */
  497. data = gain;
  498. else if (gain <= 64) /* calculated gain 33..64 -> 0x51..0x60 */
  499. data = ((gain - 32) * 16 + 16) / 32 + 80;
  500. else
  501. /* calculated gain 65..1024 -> (1..120) << 8 + 0x60 */
  502. data = (((gain - 64 + 7) * 32) & 0xff00) | 0x60;
  503. dev_dbg(&icd->dev, "Setting gain from 0x%x to 0x%x\n",
  504. reg_read(icd, MT9T031_GLOBAL_GAIN), data);
  505. data = reg_write(icd, MT9T031_GLOBAL_GAIN, data);
  506. if (data < 0)
  507. return -EIO;
  508. }
  509. /* Success */
  510. icd->gain = ctrl->value;
  511. break;
  512. case V4L2_CID_EXPOSURE:
  513. /* mt9t031 has maximum == default */
  514. if (ctrl->value > qctrl->maximum || ctrl->value < qctrl->minimum)
  515. return -EINVAL;
  516. else {
  517. const unsigned long range = qctrl->maximum - qctrl->minimum;
  518. const u32 shutter = ((ctrl->value - qctrl->minimum) * 1048 +
  519. range / 2) / range + 1;
  520. u32 old;
  521. get_shutter(icd, &old);
  522. dev_dbg(&icd->dev, "Setting shutter width from %u to %u\n",
  523. old, shutter);
  524. if (set_shutter(icd, shutter) < 0)
  525. return -EIO;
  526. icd->exposure = ctrl->value;
  527. mt9t031->autoexposure = 0;
  528. }
  529. break;
  530. case V4L2_CID_EXPOSURE_AUTO:
  531. if (ctrl->value) {
  532. const u16 vblank = MT9T031_VERTICAL_BLANK;
  533. const u32 shutter_max = MT9T031_MAX_HEIGHT + vblank;
  534. if (set_shutter(icd, icd->height +
  535. icd->y_skip_top + vblank) < 0)
  536. return -EIO;
  537. qctrl = soc_camera_find_qctrl(icd->ops, V4L2_CID_EXPOSURE);
  538. icd->exposure = (shutter_max / 2 + (icd->height +
  539. icd->y_skip_top + vblank - 1) *
  540. (qctrl->maximum - qctrl->minimum)) /
  541. shutter_max + qctrl->minimum;
  542. mt9t031->autoexposure = 1;
  543. } else
  544. mt9t031->autoexposure = 0;
  545. break;
  546. }
  547. return 0;
  548. }
  549. /* Interface active, can use i2c. If it fails, it can indeed mean, that
  550. * this wasn't our capture interface, so, we wait for the right one */
  551. static int mt9t031_video_probe(struct soc_camera_device *icd)
  552. {
  553. struct mt9t031 *mt9t031 = container_of(icd, struct mt9t031, icd);
  554. s32 data;
  555. int ret;
  556. /* We must have a parent by now. And it cannot be a wrong one.
  557. * So this entire test is completely redundant. */
  558. if (!icd->dev.parent ||
  559. to_soc_camera_host(icd->dev.parent)->nr != icd->iface)
  560. return -ENODEV;
  561. /* Enable the chip */
  562. data = reg_write(icd, MT9T031_CHIP_ENABLE, 1);
  563. dev_dbg(&icd->dev, "write: %d\n", data);
  564. /* Read out the chip version register */
  565. data = reg_read(icd, MT9T031_CHIP_VERSION);
  566. switch (data) {
  567. case 0x1621:
  568. mt9t031->model = V4L2_IDENT_MT9T031;
  569. icd->formats = mt9t031_colour_formats;
  570. icd->num_formats = ARRAY_SIZE(mt9t031_colour_formats);
  571. break;
  572. default:
  573. ret = -ENODEV;
  574. dev_err(&icd->dev,
  575. "No MT9T031 chip detected, register read %x\n", data);
  576. goto ei2c;
  577. }
  578. dev_info(&icd->dev, "Detected a MT9T031 chip ID %x\n", data);
  579. /* Now that we know the model, we can start video */
  580. ret = soc_camera_video_start(icd);
  581. if (ret)
  582. goto evstart;
  583. return 0;
  584. evstart:
  585. ei2c:
  586. return ret;
  587. }
  588. static void mt9t031_video_remove(struct soc_camera_device *icd)
  589. {
  590. struct mt9t031 *mt9t031 = container_of(icd, struct mt9t031, icd);
  591. dev_dbg(&icd->dev, "Video %x removed: %p, %p\n", mt9t031->client->addr,
  592. icd->dev.parent, icd->vdev);
  593. soc_camera_video_stop(icd);
  594. }
  595. static int mt9t031_probe(struct i2c_client *client,
  596. const struct i2c_device_id *did)
  597. {
  598. struct mt9t031 *mt9t031;
  599. struct soc_camera_device *icd;
  600. struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
  601. struct soc_camera_link *icl = client->dev.platform_data;
  602. int ret;
  603. if (!icl) {
  604. dev_err(&client->dev, "MT9T031 driver needs platform data\n");
  605. return -EINVAL;
  606. }
  607. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WORD_DATA)) {
  608. dev_warn(&adapter->dev,
  609. "I2C-Adapter doesn't support I2C_FUNC_SMBUS_WORD\n");
  610. return -EIO;
  611. }
  612. mt9t031 = kzalloc(sizeof(struct mt9t031), GFP_KERNEL);
  613. if (!mt9t031)
  614. return -ENOMEM;
  615. mt9t031->client = client;
  616. i2c_set_clientdata(client, mt9t031);
  617. /* Second stage probe - when a capture adapter is there */
  618. icd = &mt9t031->icd;
  619. icd->ops = &mt9t031_ops;
  620. icd->control = &client->dev;
  621. icd->x_min = MT9T031_COLUMN_SKIP;
  622. icd->y_min = MT9T031_ROW_SKIP;
  623. icd->x_current = icd->x_min;
  624. icd->y_current = icd->y_min;
  625. icd->width_min = MT9T031_MIN_WIDTH;
  626. icd->width_max = MT9T031_MAX_WIDTH;
  627. icd->height_min = MT9T031_MIN_HEIGHT;
  628. icd->height_max = MT9T031_MAX_HEIGHT;
  629. icd->y_skip_top = 0;
  630. icd->iface = icl->bus_id;
  631. /* Simulated autoexposure. If enabled, we calculate shutter width
  632. * ourselves in the driver based on vertical blanking and frame width */
  633. mt9t031->autoexposure = 1;
  634. mt9t031->xskip = 1;
  635. mt9t031->yskip = 1;
  636. ret = soc_camera_device_register(icd);
  637. if (ret)
  638. goto eisdr;
  639. return 0;
  640. eisdr:
  641. i2c_set_clientdata(client, NULL);
  642. kfree(mt9t031);
  643. return ret;
  644. }
  645. static int mt9t031_remove(struct i2c_client *client)
  646. {
  647. struct mt9t031 *mt9t031 = i2c_get_clientdata(client);
  648. soc_camera_device_unregister(&mt9t031->icd);
  649. i2c_set_clientdata(client, NULL);
  650. kfree(mt9t031);
  651. return 0;
  652. }
  653. static const struct i2c_device_id mt9t031_id[] = {
  654. { "mt9t031", 0 },
  655. { }
  656. };
  657. MODULE_DEVICE_TABLE(i2c, mt9t031_id);
  658. static struct i2c_driver mt9t031_i2c_driver = {
  659. .driver = {
  660. .name = "mt9t031",
  661. },
  662. .probe = mt9t031_probe,
  663. .remove = mt9t031_remove,
  664. .id_table = mt9t031_id,
  665. };
  666. static int __init mt9t031_mod_init(void)
  667. {
  668. return i2c_add_driver(&mt9t031_i2c_driver);
  669. }
  670. static void __exit mt9t031_mod_exit(void)
  671. {
  672. i2c_del_driver(&mt9t031_i2c_driver);
  673. }
  674. module_init(mt9t031_mod_init);
  675. module_exit(mt9t031_mod_exit);
  676. MODULE_DESCRIPTION("Micron MT9T031 Camera driver");
  677. MODULE_AUTHOR("Guennadi Liakhovetski <lg@denx.de>");
  678. MODULE_LICENSE("GPL v2");