mt9t031.c 21 KB

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