mt9t031.c 19 KB

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