mt9t031.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777
  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_fmt(struct soc_camera_device *icd,
  181. __u32 pixfmt, struct v4l2_rect *rect)
  182. {
  183. struct mt9t031 *mt9t031 = container_of(icd, struct mt9t031, icd);
  184. int ret;
  185. const u16 hblank = MT9T031_HORIZONTAL_BLANK,
  186. vblank = MT9T031_VERTICAL_BLANK;
  187. u16 xbin, xskip, ybin, yskip, width, height, left, top;
  188. if (pixfmt) {
  189. /*
  190. * try_fmt has put rectangle within limits.
  191. * S_FMT - use binning and skipping for scaling, recalculate
  192. * limits, used for cropping
  193. */
  194. /* Is this more optimal than just a division? */
  195. for (xskip = 8; xskip > 1; xskip--)
  196. if (rect->width * xskip <= MT9T031_MAX_WIDTH)
  197. break;
  198. for (yskip = 8; yskip > 1; yskip--)
  199. if (rect->height * yskip <= MT9T031_MAX_HEIGHT)
  200. break;
  201. recalculate_limits(icd, xskip, yskip);
  202. } else {
  203. /* CROP - no change in scaling, or in limits */
  204. xskip = mt9t031->xskip;
  205. yskip = mt9t031->yskip;
  206. }
  207. /* Make sure we don't exceed sensor limits */
  208. if (rect->left + rect->width > icd->width_max)
  209. rect->left = (icd->width_max - rect->width) / 2 + icd->x_min;
  210. if (rect->top + rect->height > icd->height_max)
  211. rect->top = (icd->height_max - rect->height) / 2 + icd->y_min;
  212. width = rect->width * xskip;
  213. height = rect->height * yskip;
  214. left = rect->left * xskip;
  215. top = rect->top * yskip;
  216. xbin = min(xskip, (u16)3);
  217. ybin = min(yskip, (u16)3);
  218. dev_dbg(&icd->dev, "xskip %u, width %u/%u, yskip %u, height %u/%u\n",
  219. xskip, width, rect->width, yskip, height, rect->height);
  220. /* Could just do roundup(rect->left, [xy]bin * 2); but this is cheaper */
  221. switch (xbin) {
  222. case 2:
  223. left = (left + 3) & ~3;
  224. break;
  225. case 3:
  226. left = roundup(left, 6);
  227. }
  228. switch (ybin) {
  229. case 2:
  230. top = (top + 3) & ~3;
  231. break;
  232. case 3:
  233. top = roundup(top, 6);
  234. }
  235. /* Disable register update, reconfigure atomically */
  236. ret = reg_set(icd, MT9T031_OUTPUT_CONTROL, 1);
  237. if (ret < 0)
  238. return ret;
  239. /* Blanking and start values - default... */
  240. ret = reg_write(icd, MT9T031_HORIZONTAL_BLANKING, hblank);
  241. if (ret >= 0)
  242. ret = reg_write(icd, MT9T031_VERTICAL_BLANKING, vblank);
  243. if (pixfmt) {
  244. /* Binning, skipping */
  245. if (ret >= 0)
  246. ret = reg_write(icd, MT9T031_COLUMN_ADDRESS_MODE,
  247. ((xbin - 1) << 4) | (xskip - 1));
  248. if (ret >= 0)
  249. ret = reg_write(icd, MT9T031_ROW_ADDRESS_MODE,
  250. ((ybin - 1) << 4) | (yskip - 1));
  251. }
  252. dev_dbg(&icd->dev, "new physical left %u, top %u\n", left, top);
  253. /* The caller provides a supported format, as guaranteed by
  254. * icd->try_fmt_cap(), soc_camera_s_crop() and soc_camera_cropcap() */
  255. if (ret >= 0)
  256. ret = reg_write(icd, MT9T031_COLUMN_START, left);
  257. if (ret >= 0)
  258. ret = reg_write(icd, MT9T031_ROW_START, top);
  259. if (ret >= 0)
  260. ret = reg_write(icd, MT9T031_WINDOW_WIDTH, width - 1);
  261. if (ret >= 0)
  262. ret = reg_write(icd, MT9T031_WINDOW_HEIGHT,
  263. height + icd->y_skip_top - 1);
  264. if (ret >= 0 && mt9t031->autoexposure) {
  265. ret = set_shutter(icd, height + icd->y_skip_top + vblank);
  266. if (ret >= 0) {
  267. const u32 shutter_max = MT9T031_MAX_HEIGHT + vblank;
  268. const struct v4l2_queryctrl *qctrl =
  269. soc_camera_find_qctrl(icd->ops,
  270. V4L2_CID_EXPOSURE);
  271. icd->exposure = (shutter_max / 2 + (height +
  272. icd->y_skip_top + vblank - 1) *
  273. (qctrl->maximum - qctrl->minimum)) /
  274. shutter_max + qctrl->minimum;
  275. }
  276. }
  277. if (!ret && pixfmt) {
  278. mt9t031->xskip = xskip;
  279. mt9t031->yskip = yskip;
  280. }
  281. /* Re-enable register update, commit all changes */
  282. reg_clear(icd, MT9T031_OUTPUT_CONTROL, 1);
  283. return ret < 0 ? ret : 0;
  284. }
  285. static int mt9t031_try_fmt(struct soc_camera_device *icd,
  286. struct v4l2_format *f)
  287. {
  288. struct v4l2_pix_format *pix = &f->fmt.pix;
  289. if (pix->height < MT9T031_MIN_HEIGHT)
  290. pix->height = MT9T031_MIN_HEIGHT;
  291. if (pix->height > MT9T031_MAX_HEIGHT)
  292. pix->height = MT9T031_MAX_HEIGHT;
  293. if (pix->width < MT9T031_MIN_WIDTH)
  294. pix->width = MT9T031_MIN_WIDTH;
  295. if (pix->width > MT9T031_MAX_WIDTH)
  296. pix->width = MT9T031_MAX_WIDTH;
  297. pix->width &= ~0x01; /* has to be even */
  298. pix->height &= ~0x01; /* has to be even */
  299. return 0;
  300. }
  301. static int mt9t031_get_chip_id(struct soc_camera_device *icd,
  302. struct v4l2_dbg_chip_ident *id)
  303. {
  304. struct mt9t031 *mt9t031 = container_of(icd, struct mt9t031, icd);
  305. if (id->match.type != V4L2_CHIP_MATCH_I2C_ADDR)
  306. return -EINVAL;
  307. if (id->match.addr != mt9t031->client->addr)
  308. return -ENODEV;
  309. id->ident = mt9t031->model;
  310. id->revision = 0;
  311. return 0;
  312. }
  313. #ifdef CONFIG_VIDEO_ADV_DEBUG
  314. static int mt9t031_get_register(struct soc_camera_device *icd,
  315. struct v4l2_dbg_register *reg)
  316. {
  317. struct mt9t031 *mt9t031 = container_of(icd, struct mt9t031, icd);
  318. if (reg->match.type != V4L2_CHIP_MATCH_I2C_ADDR || reg->reg > 0xff)
  319. return -EINVAL;
  320. if (reg->match.addr != mt9t031->client->addr)
  321. return -ENODEV;
  322. reg->val = reg_read(icd, reg->reg);
  323. if (reg->val > 0xffff)
  324. return -EIO;
  325. return 0;
  326. }
  327. static int mt9t031_set_register(struct soc_camera_device *icd,
  328. struct v4l2_dbg_register *reg)
  329. {
  330. struct mt9t031 *mt9t031 = container_of(icd, struct mt9t031, icd);
  331. if (reg->match.type != V4L2_CHIP_MATCH_I2C_ADDR || reg->reg > 0xff)
  332. return -EINVAL;
  333. if (reg->match.addr != mt9t031->client->addr)
  334. return -ENODEV;
  335. if (reg_write(icd, reg->reg, reg->val) < 0)
  336. return -EIO;
  337. return 0;
  338. }
  339. #endif
  340. static const struct v4l2_queryctrl mt9t031_controls[] = {
  341. {
  342. .id = V4L2_CID_VFLIP,
  343. .type = V4L2_CTRL_TYPE_BOOLEAN,
  344. .name = "Flip Vertically",
  345. .minimum = 0,
  346. .maximum = 1,
  347. .step = 1,
  348. .default_value = 0,
  349. }, {
  350. .id = V4L2_CID_HFLIP,
  351. .type = V4L2_CTRL_TYPE_BOOLEAN,
  352. .name = "Flip Horizontally",
  353. .minimum = 0,
  354. .maximum = 1,
  355. .step = 1,
  356. .default_value = 0,
  357. }, {
  358. .id = V4L2_CID_GAIN,
  359. .type = V4L2_CTRL_TYPE_INTEGER,
  360. .name = "Gain",
  361. .minimum = 0,
  362. .maximum = 127,
  363. .step = 1,
  364. .default_value = 64,
  365. .flags = V4L2_CTRL_FLAG_SLIDER,
  366. }, {
  367. .id = V4L2_CID_EXPOSURE,
  368. .type = V4L2_CTRL_TYPE_INTEGER,
  369. .name = "Exposure",
  370. .minimum = 1,
  371. .maximum = 255,
  372. .step = 1,
  373. .default_value = 255,
  374. .flags = V4L2_CTRL_FLAG_SLIDER,
  375. }, {
  376. .id = V4L2_CID_EXPOSURE_AUTO,
  377. .type = V4L2_CTRL_TYPE_BOOLEAN,
  378. .name = "Automatic Exposure",
  379. .minimum = 0,
  380. .maximum = 1,
  381. .step = 1,
  382. .default_value = 1,
  383. }
  384. };
  385. static int mt9t031_video_probe(struct soc_camera_device *);
  386. static void mt9t031_video_remove(struct soc_camera_device *);
  387. static int mt9t031_get_control(struct soc_camera_device *, struct v4l2_control *);
  388. static int mt9t031_set_control(struct soc_camera_device *, struct v4l2_control *);
  389. static struct soc_camera_ops mt9t031_ops = {
  390. .owner = THIS_MODULE,
  391. .probe = mt9t031_video_probe,
  392. .remove = mt9t031_video_remove,
  393. .init = mt9t031_init,
  394. .release = mt9t031_release,
  395. .start_capture = mt9t031_start_capture,
  396. .stop_capture = mt9t031_stop_capture,
  397. .set_fmt = mt9t031_set_fmt,
  398. .try_fmt = mt9t031_try_fmt,
  399. .set_bus_param = mt9t031_set_bus_param,
  400. .query_bus_param = mt9t031_query_bus_param,
  401. .controls = mt9t031_controls,
  402. .num_controls = ARRAY_SIZE(mt9t031_controls),
  403. .get_control = mt9t031_get_control,
  404. .set_control = mt9t031_set_control,
  405. .get_chip_id = mt9t031_get_chip_id,
  406. #ifdef CONFIG_VIDEO_ADV_DEBUG
  407. .get_register = mt9t031_get_register,
  408. .set_register = mt9t031_set_register,
  409. #endif
  410. };
  411. static int mt9t031_get_control(struct soc_camera_device *icd, struct v4l2_control *ctrl)
  412. {
  413. struct mt9t031 *mt9t031 = container_of(icd, struct mt9t031, icd);
  414. int data;
  415. switch (ctrl->id) {
  416. case V4L2_CID_VFLIP:
  417. data = reg_read(icd, MT9T031_READ_MODE_2);
  418. if (data < 0)
  419. return -EIO;
  420. ctrl->value = !!(data & 0x8000);
  421. break;
  422. case V4L2_CID_HFLIP:
  423. data = reg_read(icd, MT9T031_READ_MODE_2);
  424. if (data < 0)
  425. return -EIO;
  426. ctrl->value = !!(data & 0x4000);
  427. break;
  428. case V4L2_CID_EXPOSURE_AUTO:
  429. ctrl->value = mt9t031->autoexposure;
  430. break;
  431. }
  432. return 0;
  433. }
  434. static int mt9t031_set_control(struct soc_camera_device *icd, struct v4l2_control *ctrl)
  435. {
  436. struct mt9t031 *mt9t031 = container_of(icd, struct mt9t031, icd);
  437. const struct v4l2_queryctrl *qctrl;
  438. int data;
  439. qctrl = soc_camera_find_qctrl(&mt9t031_ops, ctrl->id);
  440. if (!qctrl)
  441. return -EINVAL;
  442. switch (ctrl->id) {
  443. case V4L2_CID_VFLIP:
  444. if (ctrl->value)
  445. data = reg_set(icd, MT9T031_READ_MODE_2, 0x8000);
  446. else
  447. data = reg_clear(icd, MT9T031_READ_MODE_2, 0x8000);
  448. if (data < 0)
  449. return -EIO;
  450. break;
  451. case V4L2_CID_HFLIP:
  452. if (ctrl->value)
  453. data = reg_set(icd, MT9T031_READ_MODE_2, 0x4000);
  454. else
  455. data = reg_clear(icd, MT9T031_READ_MODE_2, 0x4000);
  456. if (data < 0)
  457. return -EIO;
  458. break;
  459. case V4L2_CID_GAIN:
  460. if (ctrl->value > qctrl->maximum || ctrl->value < qctrl->minimum)
  461. return -EINVAL;
  462. /* See Datasheet Table 7, Gain settings. */
  463. if (ctrl->value <= qctrl->default_value) {
  464. /* Pack it into 0..1 step 0.125, register values 0..8 */
  465. unsigned long range = qctrl->default_value - qctrl->minimum;
  466. data = ((ctrl->value - qctrl->minimum) * 8 + range / 2) / range;
  467. dev_dbg(&icd->dev, "Setting gain %d\n", data);
  468. data = reg_write(icd, MT9T031_GLOBAL_GAIN, data);
  469. if (data < 0)
  470. return -EIO;
  471. } else {
  472. /* Pack it into 1.125..128 variable step, register values 9..0x7860 */
  473. /* We assume qctrl->maximum - qctrl->default_value - 1 > 0 */
  474. unsigned long range = qctrl->maximum - qctrl->default_value - 1;
  475. /* calculated gain: map 65..127 to 9..1024 step 0.125 */
  476. unsigned long gain = ((ctrl->value - qctrl->default_value - 1) *
  477. 1015 + range / 2) / range + 9;
  478. if (gain <= 32) /* calculated gain 9..32 -> 9..32 */
  479. data = gain;
  480. else if (gain <= 64) /* calculated gain 33..64 -> 0x51..0x60 */
  481. data = ((gain - 32) * 16 + 16) / 32 + 80;
  482. else
  483. /* calculated gain 65..1024 -> (1..120) << 8 + 0x60 */
  484. data = (((gain - 64 + 7) * 32) & 0xff00) | 0x60;
  485. dev_dbg(&icd->dev, "Setting gain from 0x%x to 0x%x\n",
  486. reg_read(icd, MT9T031_GLOBAL_GAIN), data);
  487. data = reg_write(icd, MT9T031_GLOBAL_GAIN, data);
  488. if (data < 0)
  489. return -EIO;
  490. }
  491. /* Success */
  492. icd->gain = ctrl->value;
  493. break;
  494. case V4L2_CID_EXPOSURE:
  495. /* mt9t031 has maximum == default */
  496. if (ctrl->value > qctrl->maximum || ctrl->value < qctrl->minimum)
  497. return -EINVAL;
  498. else {
  499. const unsigned long range = qctrl->maximum - qctrl->minimum;
  500. const u32 shutter = ((ctrl->value - qctrl->minimum) * 1048 +
  501. range / 2) / range + 1;
  502. u32 old;
  503. get_shutter(icd, &old);
  504. dev_dbg(&icd->dev, "Setting shutter width from %u to %u\n",
  505. old, shutter);
  506. if (set_shutter(icd, shutter) < 0)
  507. return -EIO;
  508. icd->exposure = ctrl->value;
  509. mt9t031->autoexposure = 0;
  510. }
  511. break;
  512. case V4L2_CID_EXPOSURE_AUTO:
  513. if (ctrl->value) {
  514. const u16 vblank = MT9T031_VERTICAL_BLANK;
  515. const u32 shutter_max = MT9T031_MAX_HEIGHT + vblank;
  516. if (set_shutter(icd, icd->height +
  517. icd->y_skip_top + vblank) < 0)
  518. return -EIO;
  519. qctrl = soc_camera_find_qctrl(icd->ops, V4L2_CID_EXPOSURE);
  520. icd->exposure = (shutter_max / 2 + (icd->height +
  521. icd->y_skip_top + vblank - 1) *
  522. (qctrl->maximum - qctrl->minimum)) /
  523. shutter_max + qctrl->minimum;
  524. mt9t031->autoexposure = 1;
  525. } else
  526. mt9t031->autoexposure = 0;
  527. break;
  528. }
  529. return 0;
  530. }
  531. /* Interface active, can use i2c. If it fails, it can indeed mean, that
  532. * this wasn't our capture interface, so, we wait for the right one */
  533. static int mt9t031_video_probe(struct soc_camera_device *icd)
  534. {
  535. struct mt9t031 *mt9t031 = container_of(icd, struct mt9t031, icd);
  536. s32 data;
  537. int ret;
  538. /* We must have a parent by now. And it cannot be a wrong one.
  539. * So this entire test is completely redundant. */
  540. if (!icd->dev.parent ||
  541. to_soc_camera_host(icd->dev.parent)->nr != icd->iface)
  542. return -ENODEV;
  543. /* Enable the chip */
  544. data = reg_write(icd, MT9T031_CHIP_ENABLE, 1);
  545. dev_dbg(&icd->dev, "write: %d\n", data);
  546. /* Read out the chip version register */
  547. data = reg_read(icd, MT9T031_CHIP_VERSION);
  548. switch (data) {
  549. case 0x1621:
  550. mt9t031->model = V4L2_IDENT_MT9T031;
  551. icd->formats = mt9t031_colour_formats;
  552. icd->num_formats = ARRAY_SIZE(mt9t031_colour_formats);
  553. break;
  554. default:
  555. ret = -ENODEV;
  556. dev_err(&icd->dev,
  557. "No MT9T031 chip detected, register read %x\n", data);
  558. goto ei2c;
  559. }
  560. dev_info(&icd->dev, "Detected a MT9T031 chip ID %x\n", data);
  561. /* Now that we know the model, we can start video */
  562. ret = soc_camera_video_start(icd);
  563. if (ret)
  564. goto evstart;
  565. return 0;
  566. evstart:
  567. ei2c:
  568. return ret;
  569. }
  570. static void mt9t031_video_remove(struct soc_camera_device *icd)
  571. {
  572. struct mt9t031 *mt9t031 = container_of(icd, struct mt9t031, icd);
  573. dev_dbg(&icd->dev, "Video %x removed: %p, %p\n", mt9t031->client->addr,
  574. icd->dev.parent, icd->vdev);
  575. soc_camera_video_stop(icd);
  576. }
  577. static int mt9t031_probe(struct i2c_client *client,
  578. const struct i2c_device_id *did)
  579. {
  580. struct mt9t031 *mt9t031;
  581. struct soc_camera_device *icd;
  582. struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
  583. struct soc_camera_link *icl = client->dev.platform_data;
  584. int ret;
  585. if (!icl) {
  586. dev_err(&client->dev, "MT9T031 driver needs platform data\n");
  587. return -EINVAL;
  588. }
  589. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WORD_DATA)) {
  590. dev_warn(&adapter->dev,
  591. "I2C-Adapter doesn't support I2C_FUNC_SMBUS_WORD\n");
  592. return -EIO;
  593. }
  594. mt9t031 = kzalloc(sizeof(struct mt9t031), GFP_KERNEL);
  595. if (!mt9t031)
  596. return -ENOMEM;
  597. mt9t031->client = client;
  598. i2c_set_clientdata(client, mt9t031);
  599. /* Second stage probe - when a capture adapter is there */
  600. icd = &mt9t031->icd;
  601. icd->ops = &mt9t031_ops;
  602. icd->control = &client->dev;
  603. icd->x_min = MT9T031_COLUMN_SKIP;
  604. icd->y_min = MT9T031_ROW_SKIP;
  605. icd->x_current = icd->x_min;
  606. icd->y_current = icd->y_min;
  607. icd->width_min = MT9T031_MIN_WIDTH;
  608. icd->width_max = MT9T031_MAX_WIDTH;
  609. icd->height_min = MT9T031_MIN_HEIGHT;
  610. icd->height_max = MT9T031_MAX_HEIGHT;
  611. icd->y_skip_top = 0;
  612. icd->iface = icl->bus_id;
  613. /* Simulated autoexposure. If enabled, we calculate shutter width
  614. * ourselves in the driver based on vertical blanking and frame width */
  615. mt9t031->autoexposure = 1;
  616. mt9t031->xskip = 1;
  617. mt9t031->yskip = 1;
  618. ret = soc_camera_device_register(icd);
  619. if (ret)
  620. goto eisdr;
  621. return 0;
  622. eisdr:
  623. i2c_set_clientdata(client, NULL);
  624. kfree(mt9t031);
  625. return ret;
  626. }
  627. static int mt9t031_remove(struct i2c_client *client)
  628. {
  629. struct mt9t031 *mt9t031 = i2c_get_clientdata(client);
  630. soc_camera_device_unregister(&mt9t031->icd);
  631. i2c_set_clientdata(client, NULL);
  632. kfree(mt9t031);
  633. return 0;
  634. }
  635. static const struct i2c_device_id mt9t031_id[] = {
  636. { "mt9t031", 0 },
  637. { }
  638. };
  639. MODULE_DEVICE_TABLE(i2c, mt9t031_id);
  640. static struct i2c_driver mt9t031_i2c_driver = {
  641. .driver = {
  642. .name = "mt9t031",
  643. },
  644. .probe = mt9t031_probe,
  645. .remove = mt9t031_remove,
  646. .id_table = mt9t031_id,
  647. };
  648. static int __init mt9t031_mod_init(void)
  649. {
  650. return i2c_add_driver(&mt9t031_i2c_driver);
  651. }
  652. static void __exit mt9t031_mod_exit(void)
  653. {
  654. i2c_del_driver(&mt9t031_i2c_driver);
  655. }
  656. module_init(mt9t031_mod_init);
  657. module_exit(mt9t031_mod_exit);
  658. MODULE_DESCRIPTION("Micron MT9T031 Camera driver");
  659. MODULE_AUTHOR("Guennadi Liakhovetski <lg@denx.de>");
  660. MODULE_LICENSE("GPL v2");