mt9t031.c 21 KB

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