mt9m001.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711
  1. /*
  2. * Driver for MT9M001 CMOS Image Sensor from Micron
  3. *
  4. * Copyright (C) 2008, Guennadi Liakhovetski <kernel@pengutronix.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. /* mt9m001 i2c address 0x5d
  18. * The platform has to define i2c_board_info
  19. * and call i2c_register_board_info() */
  20. /* mt9m001 selected register addresses */
  21. #define MT9M001_CHIP_VERSION 0x00
  22. #define MT9M001_ROW_START 0x01
  23. #define MT9M001_COLUMN_START 0x02
  24. #define MT9M001_WINDOW_HEIGHT 0x03
  25. #define MT9M001_WINDOW_WIDTH 0x04
  26. #define MT9M001_HORIZONTAL_BLANKING 0x05
  27. #define MT9M001_VERTICAL_BLANKING 0x06
  28. #define MT9M001_OUTPUT_CONTROL 0x07
  29. #define MT9M001_SHUTTER_WIDTH 0x09
  30. #define MT9M001_FRAME_RESTART 0x0b
  31. #define MT9M001_SHUTTER_DELAY 0x0c
  32. #define MT9M001_RESET 0x0d
  33. #define MT9M001_READ_OPTIONS1 0x1e
  34. #define MT9M001_READ_OPTIONS2 0x20
  35. #define MT9M001_GLOBAL_GAIN 0x35
  36. #define MT9M001_CHIP_ENABLE 0xF1
  37. static const struct soc_camera_data_format mt9m001_colour_formats[] = {
  38. /* Order important: first natively supported,
  39. * second supported with a GPIO extender */
  40. {
  41. .name = "Bayer (sRGB) 10 bit",
  42. .depth = 10,
  43. .fourcc = V4L2_PIX_FMT_SBGGR16,
  44. .colorspace = V4L2_COLORSPACE_SRGB,
  45. }, {
  46. .name = "Bayer (sRGB) 8 bit",
  47. .depth = 8,
  48. .fourcc = V4L2_PIX_FMT_SBGGR8,
  49. .colorspace = V4L2_COLORSPACE_SRGB,
  50. }
  51. };
  52. static const struct soc_camera_data_format mt9m001_monochrome_formats[] = {
  53. /* Order important - see above */
  54. {
  55. .name = "Monochrome 10 bit",
  56. .depth = 10,
  57. .fourcc = V4L2_PIX_FMT_Y16,
  58. }, {
  59. .name = "Monochrome 8 bit",
  60. .depth = 8,
  61. .fourcc = V4L2_PIX_FMT_GREY,
  62. },
  63. };
  64. struct mt9m001 {
  65. struct i2c_client *client;
  66. struct soc_camera_device icd;
  67. int model; /* V4L2_IDENT_MT9M001* codes from v4l2-chip-ident.h */
  68. unsigned char autoexposure;
  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 mt9m001_init(struct soc_camera_device *icd)
  99. {
  100. struct i2c_client *client = to_i2c_client(icd->control);
  101. struct soc_camera_link *icl = client->dev.platform_data;
  102. int ret;
  103. dev_dbg(icd->vdev->parent, "%s\n", __func__);
  104. if (icl->power) {
  105. ret = icl->power(&client->dev, 1);
  106. if (ret < 0) {
  107. dev_err(icd->vdev->parent,
  108. "Platform failed to power-on the camera.\n");
  109. return ret;
  110. }
  111. }
  112. /* The camera could have been already on, we reset it additionally */
  113. if (icl->reset)
  114. ret = icl->reset(&client->dev);
  115. else
  116. ret = -ENODEV;
  117. if (ret < 0) {
  118. /* Either no platform reset, or platform reset failed */
  119. ret = reg_write(client, MT9M001_RESET, 1);
  120. if (!ret)
  121. ret = reg_write(client, MT9M001_RESET, 0);
  122. }
  123. /* Disable chip, synchronous option update */
  124. if (!ret)
  125. ret = reg_write(client, MT9M001_OUTPUT_CONTROL, 0);
  126. return ret;
  127. }
  128. static int mt9m001_release(struct soc_camera_device *icd)
  129. {
  130. struct i2c_client *client = to_i2c_client(icd->control);
  131. struct soc_camera_link *icl = client->dev.platform_data;
  132. /* Disable the chip */
  133. reg_write(client, MT9M001_OUTPUT_CONTROL, 0);
  134. if (icl->power)
  135. icl->power(&client->dev, 0);
  136. return 0;
  137. }
  138. static int mt9m001_start_capture(struct soc_camera_device *icd)
  139. {
  140. struct i2c_client *client = to_i2c_client(icd->control);
  141. /* Switch to master "normal" mode */
  142. if (reg_write(client, MT9M001_OUTPUT_CONTROL, 2) < 0)
  143. return -EIO;
  144. return 0;
  145. }
  146. static int mt9m001_stop_capture(struct soc_camera_device *icd)
  147. {
  148. struct i2c_client *client = to_i2c_client(icd->control);
  149. /* Stop sensor readout */
  150. if (reg_write(client, MT9M001_OUTPUT_CONTROL, 0) < 0)
  151. return -EIO;
  152. return 0;
  153. }
  154. static int mt9m001_set_bus_param(struct soc_camera_device *icd,
  155. unsigned long flags)
  156. {
  157. struct mt9m001 *mt9m001 = container_of(icd, struct mt9m001, icd);
  158. struct soc_camera_link *icl = mt9m001->client->dev.platform_data;
  159. unsigned long width_flag = flags & SOCAM_DATAWIDTH_MASK;
  160. /* Only one width bit may be set */
  161. if (!is_power_of_2(width_flag))
  162. return -EINVAL;
  163. if (icl->set_bus_param)
  164. return icl->set_bus_param(icl, width_flag);
  165. /*
  166. * Without board specific bus width settings we only support the
  167. * sensors native bus width
  168. */
  169. if (width_flag == SOCAM_DATAWIDTH_10)
  170. return 0;
  171. return -EINVAL;
  172. }
  173. static unsigned long mt9m001_query_bus_param(struct soc_camera_device *icd)
  174. {
  175. struct mt9m001 *mt9m001 = container_of(icd, struct mt9m001, icd);
  176. struct soc_camera_link *icl = mt9m001->client->dev.platform_data;
  177. /* MT9M001 has all capture_format parameters fixed */
  178. unsigned long flags = SOCAM_PCLK_SAMPLE_FALLING |
  179. SOCAM_HSYNC_ACTIVE_HIGH | SOCAM_VSYNC_ACTIVE_HIGH |
  180. SOCAM_DATA_ACTIVE_HIGH | SOCAM_MASTER;
  181. if (icl->query_bus_param)
  182. flags |= icl->query_bus_param(icl) & SOCAM_DATAWIDTH_MASK;
  183. else
  184. flags |= SOCAM_DATAWIDTH_10;
  185. return soc_camera_apply_sensor_flags(icl, flags);
  186. }
  187. static int mt9m001_set_crop(struct soc_camera_device *icd,
  188. struct v4l2_rect *rect)
  189. {
  190. struct i2c_client *client = to_i2c_client(icd->control);
  191. struct mt9m001 *mt9m001 = container_of(icd, struct mt9m001, icd);
  192. int ret;
  193. const u16 hblank = 9, vblank = 25;
  194. /* Blanking and start values - default... */
  195. ret = reg_write(client, MT9M001_HORIZONTAL_BLANKING, hblank);
  196. if (!ret)
  197. ret = reg_write(client, MT9M001_VERTICAL_BLANKING, vblank);
  198. /* The caller provides a supported format, as verified per
  199. * call to icd->try_fmt() */
  200. if (!ret)
  201. ret = reg_write(client, MT9M001_COLUMN_START, rect->left);
  202. if (!ret)
  203. ret = reg_write(client, MT9M001_ROW_START, rect->top);
  204. if (!ret)
  205. ret = reg_write(client, MT9M001_WINDOW_WIDTH, rect->width - 1);
  206. if (!ret)
  207. ret = reg_write(client, MT9M001_WINDOW_HEIGHT,
  208. rect->height + icd->y_skip_top - 1);
  209. if (!ret && mt9m001->autoexposure) {
  210. ret = reg_write(client, MT9M001_SHUTTER_WIDTH,
  211. rect->height + icd->y_skip_top + vblank);
  212. if (!ret) {
  213. const struct v4l2_queryctrl *qctrl =
  214. soc_camera_find_qctrl(icd->ops,
  215. V4L2_CID_EXPOSURE);
  216. icd->exposure = (524 + (rect->height + icd->y_skip_top +
  217. vblank - 1) *
  218. (qctrl->maximum - qctrl->minimum)) /
  219. 1048 + qctrl->minimum;
  220. }
  221. }
  222. return ret;
  223. }
  224. static int mt9m001_set_fmt(struct soc_camera_device *icd,
  225. struct v4l2_format *f)
  226. {
  227. struct v4l2_rect rect = {
  228. .left = icd->x_current,
  229. .top = icd->y_current,
  230. .width = f->fmt.pix.width,
  231. .height = f->fmt.pix.height,
  232. };
  233. /* No support for scaling so far, just crop. TODO: use skipping */
  234. return mt9m001_set_crop(icd, &rect);
  235. }
  236. static int mt9m001_try_fmt(struct soc_camera_device *icd,
  237. struct v4l2_format *f)
  238. {
  239. struct v4l2_pix_format *pix = &f->fmt.pix;
  240. v4l_bound_align_image(&pix->width, 48, 1280, 1,
  241. &pix->height, 32 + icd->y_skip_top,
  242. 1024 + icd->y_skip_top, 0, 0);
  243. return 0;
  244. }
  245. static int mt9m001_get_chip_id(struct soc_camera_device *icd,
  246. struct v4l2_dbg_chip_ident *id)
  247. {
  248. struct mt9m001 *mt9m001 = container_of(icd, struct mt9m001, icd);
  249. if (id->match.type != V4L2_CHIP_MATCH_I2C_ADDR)
  250. return -EINVAL;
  251. if (id->match.addr != mt9m001->client->addr)
  252. return -ENODEV;
  253. id->ident = mt9m001->model;
  254. id->revision = 0;
  255. return 0;
  256. }
  257. #ifdef CONFIG_VIDEO_ADV_DEBUG
  258. static int mt9m001_get_register(struct soc_camera_device *icd,
  259. struct v4l2_dbg_register *reg)
  260. {
  261. struct i2c_client *client = to_i2c_client(icd->control);
  262. if (reg->match.type != V4L2_CHIP_MATCH_I2C_ADDR || reg->reg > 0xff)
  263. return -EINVAL;
  264. if (reg->match.addr != client->addr)
  265. return -ENODEV;
  266. reg->size = 2;
  267. reg->val = reg_read(client, reg->reg);
  268. if (reg->val > 0xffff)
  269. return -EIO;
  270. return 0;
  271. }
  272. static int mt9m001_set_register(struct soc_camera_device *icd,
  273. struct v4l2_dbg_register *reg)
  274. {
  275. struct i2c_client *client = to_i2c_client(icd->control);
  276. if (reg->match.type != V4L2_CHIP_MATCH_I2C_ADDR || reg->reg > 0xff)
  277. return -EINVAL;
  278. if (reg->match.addr != client->addr)
  279. return -ENODEV;
  280. if (reg_write(client, reg->reg, reg->val) < 0)
  281. return -EIO;
  282. return 0;
  283. }
  284. #endif
  285. static const struct v4l2_queryctrl mt9m001_controls[] = {
  286. {
  287. .id = V4L2_CID_VFLIP,
  288. .type = V4L2_CTRL_TYPE_BOOLEAN,
  289. .name = "Flip Vertically",
  290. .minimum = 0,
  291. .maximum = 1,
  292. .step = 1,
  293. .default_value = 0,
  294. }, {
  295. .id = V4L2_CID_GAIN,
  296. .type = V4L2_CTRL_TYPE_INTEGER,
  297. .name = "Gain",
  298. .minimum = 0,
  299. .maximum = 127,
  300. .step = 1,
  301. .default_value = 64,
  302. .flags = V4L2_CTRL_FLAG_SLIDER,
  303. }, {
  304. .id = V4L2_CID_EXPOSURE,
  305. .type = V4L2_CTRL_TYPE_INTEGER,
  306. .name = "Exposure",
  307. .minimum = 1,
  308. .maximum = 255,
  309. .step = 1,
  310. .default_value = 255,
  311. .flags = V4L2_CTRL_FLAG_SLIDER,
  312. }, {
  313. .id = V4L2_CID_EXPOSURE_AUTO,
  314. .type = V4L2_CTRL_TYPE_BOOLEAN,
  315. .name = "Automatic Exposure",
  316. .minimum = 0,
  317. .maximum = 1,
  318. .step = 1,
  319. .default_value = 1,
  320. }
  321. };
  322. static int mt9m001_video_probe(struct soc_camera_device *);
  323. static void mt9m001_video_remove(struct soc_camera_device *);
  324. static int mt9m001_get_control(struct soc_camera_device *, struct v4l2_control *);
  325. static int mt9m001_set_control(struct soc_camera_device *, struct v4l2_control *);
  326. static struct soc_camera_ops mt9m001_ops = {
  327. .owner = THIS_MODULE,
  328. .probe = mt9m001_video_probe,
  329. .remove = mt9m001_video_remove,
  330. .init = mt9m001_init,
  331. .release = mt9m001_release,
  332. .start_capture = mt9m001_start_capture,
  333. .stop_capture = mt9m001_stop_capture,
  334. .set_crop = mt9m001_set_crop,
  335. .set_fmt = mt9m001_set_fmt,
  336. .try_fmt = mt9m001_try_fmt,
  337. .set_bus_param = mt9m001_set_bus_param,
  338. .query_bus_param = mt9m001_query_bus_param,
  339. .controls = mt9m001_controls,
  340. .num_controls = ARRAY_SIZE(mt9m001_controls),
  341. .get_control = mt9m001_get_control,
  342. .set_control = mt9m001_set_control,
  343. .get_chip_id = mt9m001_get_chip_id,
  344. #ifdef CONFIG_VIDEO_ADV_DEBUG
  345. .get_register = mt9m001_get_register,
  346. .set_register = mt9m001_set_register,
  347. #endif
  348. };
  349. static int mt9m001_get_control(struct soc_camera_device *icd, struct v4l2_control *ctrl)
  350. {
  351. struct i2c_client *client = to_i2c_client(icd->control);
  352. struct mt9m001 *mt9m001 = container_of(icd, struct mt9m001, icd);
  353. int data;
  354. switch (ctrl->id) {
  355. case V4L2_CID_VFLIP:
  356. data = reg_read(client, MT9M001_READ_OPTIONS2);
  357. if (data < 0)
  358. return -EIO;
  359. ctrl->value = !!(data & 0x8000);
  360. break;
  361. case V4L2_CID_EXPOSURE_AUTO:
  362. ctrl->value = mt9m001->autoexposure;
  363. break;
  364. }
  365. return 0;
  366. }
  367. static int mt9m001_set_control(struct soc_camera_device *icd, struct v4l2_control *ctrl)
  368. {
  369. struct i2c_client *client = to_i2c_client(icd->control);
  370. struct mt9m001 *mt9m001 = container_of(icd, struct mt9m001, icd);
  371. const struct v4l2_queryctrl *qctrl;
  372. int data;
  373. qctrl = soc_camera_find_qctrl(&mt9m001_ops, ctrl->id);
  374. if (!qctrl)
  375. return -EINVAL;
  376. switch (ctrl->id) {
  377. case V4L2_CID_VFLIP:
  378. if (ctrl->value)
  379. data = reg_set(client, MT9M001_READ_OPTIONS2, 0x8000);
  380. else
  381. data = reg_clear(client, MT9M001_READ_OPTIONS2, 0x8000);
  382. if (data < 0)
  383. return -EIO;
  384. break;
  385. case V4L2_CID_GAIN:
  386. if (ctrl->value > qctrl->maximum || ctrl->value < qctrl->minimum)
  387. return -EINVAL;
  388. /* See Datasheet Table 7, Gain settings. */
  389. if (ctrl->value <= qctrl->default_value) {
  390. /* Pack it into 0..1 step 0.125, register values 0..8 */
  391. unsigned long range = qctrl->default_value - qctrl->minimum;
  392. data = ((ctrl->value - qctrl->minimum) * 8 + range / 2) / range;
  393. dev_dbg(&icd->dev, "Setting gain %d\n", data);
  394. data = reg_write(client, MT9M001_GLOBAL_GAIN, data);
  395. if (data < 0)
  396. return -EIO;
  397. } else {
  398. /* Pack it into 1.125..15 variable step, register values 9..67 */
  399. /* We assume qctrl->maximum - qctrl->default_value - 1 > 0 */
  400. unsigned long range = qctrl->maximum - qctrl->default_value - 1;
  401. unsigned long gain = ((ctrl->value - qctrl->default_value - 1) *
  402. 111 + range / 2) / range + 9;
  403. if (gain <= 32)
  404. data = gain;
  405. else if (gain <= 64)
  406. data = ((gain - 32) * 16 + 16) / 32 + 80;
  407. else
  408. data = ((gain - 64) * 7 + 28) / 56 + 96;
  409. dev_dbg(&icd->dev, "Setting gain from %d to %d\n",
  410. reg_read(client, MT9M001_GLOBAL_GAIN), data);
  411. data = reg_write(client, MT9M001_GLOBAL_GAIN, data);
  412. if (data < 0)
  413. return -EIO;
  414. }
  415. /* Success */
  416. icd->gain = ctrl->value;
  417. break;
  418. case V4L2_CID_EXPOSURE:
  419. /* mt9m001 has maximum == default */
  420. if (ctrl->value > qctrl->maximum || ctrl->value < qctrl->minimum)
  421. return -EINVAL;
  422. else {
  423. unsigned long range = qctrl->maximum - qctrl->minimum;
  424. unsigned long shutter = ((ctrl->value - qctrl->minimum) * 1048 +
  425. range / 2) / range + 1;
  426. dev_dbg(&icd->dev, "Setting shutter width from %d to %lu\n",
  427. reg_read(client, MT9M001_SHUTTER_WIDTH), shutter);
  428. if (reg_write(client, MT9M001_SHUTTER_WIDTH, shutter) < 0)
  429. return -EIO;
  430. icd->exposure = ctrl->value;
  431. mt9m001->autoexposure = 0;
  432. }
  433. break;
  434. case V4L2_CID_EXPOSURE_AUTO:
  435. if (ctrl->value) {
  436. const u16 vblank = 25;
  437. if (reg_write(client, MT9M001_SHUTTER_WIDTH, icd->height +
  438. icd->y_skip_top + vblank) < 0)
  439. return -EIO;
  440. qctrl = soc_camera_find_qctrl(icd->ops, V4L2_CID_EXPOSURE);
  441. icd->exposure = (524 + (icd->height + icd->y_skip_top + vblank - 1) *
  442. (qctrl->maximum - qctrl->minimum)) /
  443. 1048 + qctrl->minimum;
  444. mt9m001->autoexposure = 1;
  445. } else
  446. mt9m001->autoexposure = 0;
  447. break;
  448. }
  449. return 0;
  450. }
  451. /* Interface active, can use i2c. If it fails, it can indeed mean, that
  452. * this wasn't our capture interface, so, we wait for the right one */
  453. static int mt9m001_video_probe(struct soc_camera_device *icd)
  454. {
  455. struct i2c_client *client = to_i2c_client(icd->control);
  456. struct mt9m001 *mt9m001 = container_of(icd, struct mt9m001, icd);
  457. struct soc_camera_link *icl = client->dev.platform_data;
  458. s32 data;
  459. int ret;
  460. unsigned long flags;
  461. /* We must have a parent by now. And it cannot be a wrong one.
  462. * So this entire test is completely redundant. */
  463. if (!icd->dev.parent ||
  464. to_soc_camera_host(icd->dev.parent)->nr != icd->iface)
  465. return -ENODEV;
  466. /* Enable the chip */
  467. data = reg_write(client, MT9M001_CHIP_ENABLE, 1);
  468. dev_dbg(&icd->dev, "write: %d\n", data);
  469. /* Read out the chip version register */
  470. data = reg_read(client, MT9M001_CHIP_VERSION);
  471. /* must be 0x8411 or 0x8421 for colour sensor and 8431 for bw */
  472. switch (data) {
  473. case 0x8411:
  474. case 0x8421:
  475. mt9m001->model = V4L2_IDENT_MT9M001C12ST;
  476. icd->formats = mt9m001_colour_formats;
  477. break;
  478. case 0x8431:
  479. mt9m001->model = V4L2_IDENT_MT9M001C12STM;
  480. icd->formats = mt9m001_monochrome_formats;
  481. break;
  482. default:
  483. ret = -ENODEV;
  484. dev_err(&icd->dev,
  485. "No MT9M001 chip detected, register read %x\n", data);
  486. goto ei2c;
  487. }
  488. icd->num_formats = 0;
  489. /*
  490. * This is a 10bit sensor, so by default we only allow 10bit.
  491. * The platform may support different bus widths due to
  492. * different routing of the data lines.
  493. */
  494. if (icl->query_bus_param)
  495. flags = icl->query_bus_param(icl);
  496. else
  497. flags = SOCAM_DATAWIDTH_10;
  498. if (flags & SOCAM_DATAWIDTH_10)
  499. icd->num_formats++;
  500. else
  501. icd->formats++;
  502. if (flags & SOCAM_DATAWIDTH_8)
  503. icd->num_formats++;
  504. dev_info(&icd->dev, "Detected a MT9M001 chip ID %x (%s)\n", data,
  505. data == 0x8431 ? "C12STM" : "C12ST");
  506. /* Now that we know the model, we can start video */
  507. ret = soc_camera_video_start(icd);
  508. if (ret)
  509. goto eisis;
  510. return 0;
  511. eisis:
  512. ei2c:
  513. return ret;
  514. }
  515. static void mt9m001_video_remove(struct soc_camera_device *icd)
  516. {
  517. struct mt9m001 *mt9m001 = container_of(icd, struct mt9m001, icd);
  518. struct soc_camera_link *icl = mt9m001->client->dev.platform_data;
  519. dev_dbg(&icd->dev, "Video %x removed: %p, %p\n", mt9m001->client->addr,
  520. icd->dev.parent, icd->vdev);
  521. soc_camera_video_stop(icd);
  522. if (icl->free_bus)
  523. icl->free_bus(icl);
  524. }
  525. static int mt9m001_probe(struct i2c_client *client,
  526. const struct i2c_device_id *did)
  527. {
  528. struct mt9m001 *mt9m001;
  529. struct soc_camera_device *icd;
  530. struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
  531. struct soc_camera_link *icl = client->dev.platform_data;
  532. int ret;
  533. if (!icl) {
  534. dev_err(&client->dev, "MT9M001 driver needs platform data\n");
  535. return -EINVAL;
  536. }
  537. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WORD_DATA)) {
  538. dev_warn(&adapter->dev,
  539. "I2C-Adapter doesn't support I2C_FUNC_SMBUS_WORD\n");
  540. return -EIO;
  541. }
  542. mt9m001 = kzalloc(sizeof(struct mt9m001), GFP_KERNEL);
  543. if (!mt9m001)
  544. return -ENOMEM;
  545. mt9m001->client = client;
  546. i2c_set_clientdata(client, mt9m001);
  547. /* Second stage probe - when a capture adapter is there */
  548. icd = &mt9m001->icd;
  549. icd->ops = &mt9m001_ops;
  550. icd->control = &client->dev;
  551. icd->x_min = 20;
  552. icd->y_min = 12;
  553. icd->x_current = 20;
  554. icd->y_current = 12;
  555. icd->width_min = 48;
  556. icd->width_max = 1280;
  557. icd->height_min = 32;
  558. icd->height_max = 1024;
  559. icd->y_skip_top = 1;
  560. icd->iface = icl->bus_id;
  561. /* Simulated autoexposure. If enabled, we calculate shutter width
  562. * ourselves in the driver based on vertical blanking and frame width */
  563. mt9m001->autoexposure = 1;
  564. ret = soc_camera_device_register(icd);
  565. if (ret)
  566. goto eisdr;
  567. return 0;
  568. eisdr:
  569. kfree(mt9m001);
  570. return ret;
  571. }
  572. static int mt9m001_remove(struct i2c_client *client)
  573. {
  574. struct mt9m001 *mt9m001 = i2c_get_clientdata(client);
  575. soc_camera_device_unregister(&mt9m001->icd);
  576. kfree(mt9m001);
  577. return 0;
  578. }
  579. static const struct i2c_device_id mt9m001_id[] = {
  580. { "mt9m001", 0 },
  581. { }
  582. };
  583. MODULE_DEVICE_TABLE(i2c, mt9m001_id);
  584. static struct i2c_driver mt9m001_i2c_driver = {
  585. .driver = {
  586. .name = "mt9m001",
  587. },
  588. .probe = mt9m001_probe,
  589. .remove = mt9m001_remove,
  590. .id_table = mt9m001_id,
  591. };
  592. static int __init mt9m001_mod_init(void)
  593. {
  594. return i2c_add_driver(&mt9m001_i2c_driver);
  595. }
  596. static void __exit mt9m001_mod_exit(void)
  597. {
  598. i2c_del_driver(&mt9m001_i2c_driver);
  599. }
  600. module_init(mt9m001_mod_init);
  601. module_exit(mt9m001_mod_exit);
  602. MODULE_DESCRIPTION("Micron MT9M001 Camera driver");
  603. MODULE_AUTHOR("Guennadi Liakhovetski <kernel@pengutronix.de>");
  604. MODULE_LICENSE("GPL");