mt9m001.c 18 KB

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