mt9m001.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709
  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 soc_camera_device *icd, const u8 reg)
  71. {
  72. struct mt9m001 *mt9m001 = container_of(icd, struct mt9m001, icd);
  73. struct i2c_client *client = mt9m001->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 mt9m001 *mt9m001 = container_of(icd, struct mt9m001, icd);
  81. return i2c_smbus_write_word_data(mt9m001->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 mt9m001_init(struct soc_camera_device *icd)
  102. {
  103. struct mt9m001 *mt9m001 = container_of(icd, struct mt9m001, icd);
  104. struct soc_camera_link *icl = mt9m001->client->dev.platform_data;
  105. int ret;
  106. dev_dbg(icd->vdev->parent, "%s\n", __func__);
  107. if (icl->power) {
  108. ret = icl->power(&mt9m001->client->dev, 1);
  109. if (ret < 0) {
  110. dev_err(icd->vdev->parent,
  111. "Platform failed to power-on the camera.\n");
  112. return ret;
  113. }
  114. }
  115. /* The camera could have been already on, we reset it additionally */
  116. if (icl->reset)
  117. ret = icl->reset(&mt9m001->client->dev);
  118. else
  119. ret = -ENODEV;
  120. if (ret < 0) {
  121. /* Either no platform reset, or platform reset failed */
  122. ret = reg_write(icd, MT9M001_RESET, 1);
  123. if (!ret)
  124. ret = reg_write(icd, MT9M001_RESET, 0);
  125. }
  126. /* Disable chip, synchronous option update */
  127. if (!ret)
  128. ret = reg_write(icd, MT9M001_OUTPUT_CONTROL, 0);
  129. return ret;
  130. }
  131. static int mt9m001_release(struct soc_camera_device *icd)
  132. {
  133. struct mt9m001 *mt9m001 = container_of(icd, struct mt9m001, icd);
  134. struct soc_camera_link *icl = mt9m001->client->dev.platform_data;
  135. /* Disable the chip */
  136. reg_write(icd, MT9M001_OUTPUT_CONTROL, 0);
  137. if (icl->power)
  138. icl->power(&mt9m001->client->dev, 0);
  139. return 0;
  140. }
  141. static int mt9m001_start_capture(struct soc_camera_device *icd)
  142. {
  143. /* Switch to master "normal" mode */
  144. if (reg_write(icd, MT9M001_OUTPUT_CONTROL, 2) < 0)
  145. return -EIO;
  146. return 0;
  147. }
  148. static int mt9m001_stop_capture(struct soc_camera_device *icd)
  149. {
  150. /* Stop sensor readout */
  151. if (reg_write(icd, MT9M001_OUTPUT_CONTROL, 0) < 0)
  152. return -EIO;
  153. return 0;
  154. }
  155. static int mt9m001_set_bus_param(struct soc_camera_device *icd,
  156. unsigned long flags)
  157. {
  158. struct mt9m001 *mt9m001 = container_of(icd, struct mt9m001, icd);
  159. struct soc_camera_link *icl = mt9m001->client->dev.platform_data;
  160. unsigned long width_flag = flags & SOCAM_DATAWIDTH_MASK;
  161. /* Only one width bit may be set */
  162. if (!is_power_of_2(width_flag))
  163. return -EINVAL;
  164. if (icl->set_bus_param)
  165. return icl->set_bus_param(icl, width_flag);
  166. /*
  167. * Without board specific bus width settings we only support the
  168. * sensors native bus width
  169. */
  170. if (width_flag == SOCAM_DATAWIDTH_10)
  171. return 0;
  172. return -EINVAL;
  173. }
  174. static unsigned long mt9m001_query_bus_param(struct soc_camera_device *icd)
  175. {
  176. struct mt9m001 *mt9m001 = container_of(icd, struct mt9m001, icd);
  177. struct soc_camera_link *icl = mt9m001->client->dev.platform_data;
  178. /* MT9M001 has all capture_format parameters fixed */
  179. unsigned long flags = SOCAM_PCLK_SAMPLE_FALLING |
  180. SOCAM_HSYNC_ACTIVE_HIGH | SOCAM_VSYNC_ACTIVE_HIGH |
  181. SOCAM_DATA_ACTIVE_HIGH | SOCAM_MASTER;
  182. if (icl->query_bus_param)
  183. flags |= icl->query_bus_param(icl) & SOCAM_DATAWIDTH_MASK;
  184. else
  185. flags |= SOCAM_DATAWIDTH_10;
  186. return soc_camera_apply_sensor_flags(icl, flags);
  187. }
  188. static int mt9m001_set_crop(struct soc_camera_device *icd,
  189. struct v4l2_rect *rect)
  190. {
  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(icd, MT9M001_HORIZONTAL_BLANKING, hblank);
  196. if (!ret)
  197. ret = reg_write(icd, 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(icd, MT9M001_COLUMN_START, rect->left);
  202. if (!ret)
  203. ret = reg_write(icd, MT9M001_ROW_START, rect->top);
  204. if (!ret)
  205. ret = reg_write(icd, MT9M001_WINDOW_WIDTH, rect->width - 1);
  206. if (!ret)
  207. ret = reg_write(icd, MT9M001_WINDOW_HEIGHT,
  208. rect->height + icd->y_skip_top - 1);
  209. if (!ret && mt9m001->autoexposure) {
  210. ret = reg_write(icd, 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. if (pix->height < 32 + icd->y_skip_top)
  241. pix->height = 32 + icd->y_skip_top;
  242. if (pix->height > 1024 + icd->y_skip_top)
  243. pix->height = 1024 + icd->y_skip_top;
  244. if (pix->width < 48)
  245. pix->width = 48;
  246. if (pix->width > 1280)
  247. pix->width = 1280;
  248. pix->width &= ~0x01; /* has to be even, unsure why was ~3 */
  249. return 0;
  250. }
  251. static int mt9m001_get_chip_id(struct soc_camera_device *icd,
  252. struct v4l2_dbg_chip_ident *id)
  253. {
  254. struct mt9m001 *mt9m001 = container_of(icd, struct mt9m001, icd);
  255. if (id->match.type != V4L2_CHIP_MATCH_I2C_ADDR)
  256. return -EINVAL;
  257. if (id->match.addr != mt9m001->client->addr)
  258. return -ENODEV;
  259. id->ident = mt9m001->model;
  260. id->revision = 0;
  261. return 0;
  262. }
  263. #ifdef CONFIG_VIDEO_ADV_DEBUG
  264. static int mt9m001_get_register(struct soc_camera_device *icd,
  265. struct v4l2_dbg_register *reg)
  266. {
  267. struct mt9m001 *mt9m001 = container_of(icd, struct mt9m001, icd);
  268. if (reg->match.type != V4L2_CHIP_MATCH_I2C_ADDR || reg->reg > 0xff)
  269. return -EINVAL;
  270. if (reg->match.addr != mt9m001->client->addr)
  271. return -ENODEV;
  272. reg->size = 2;
  273. reg->val = reg_read(icd, reg->reg);
  274. if (reg->val > 0xffff)
  275. return -EIO;
  276. return 0;
  277. }
  278. static int mt9m001_set_register(struct soc_camera_device *icd,
  279. struct v4l2_dbg_register *reg)
  280. {
  281. struct mt9m001 *mt9m001 = container_of(icd, struct mt9m001, icd);
  282. if (reg->match.type != V4L2_CHIP_MATCH_I2C_ADDR || reg->reg > 0xff)
  283. return -EINVAL;
  284. if (reg->match.addr != mt9m001->client->addr)
  285. return -ENODEV;
  286. if (reg_write(icd, reg->reg, reg->val) < 0)
  287. return -EIO;
  288. return 0;
  289. }
  290. #endif
  291. static const struct v4l2_queryctrl mt9m001_controls[] = {
  292. {
  293. .id = V4L2_CID_VFLIP,
  294. .type = V4L2_CTRL_TYPE_BOOLEAN,
  295. .name = "Flip Vertically",
  296. .minimum = 0,
  297. .maximum = 1,
  298. .step = 1,
  299. .default_value = 0,
  300. }, {
  301. .id = V4L2_CID_GAIN,
  302. .type = V4L2_CTRL_TYPE_INTEGER,
  303. .name = "Gain",
  304. .minimum = 0,
  305. .maximum = 127,
  306. .step = 1,
  307. .default_value = 64,
  308. .flags = V4L2_CTRL_FLAG_SLIDER,
  309. }, {
  310. .id = V4L2_CID_EXPOSURE,
  311. .type = V4L2_CTRL_TYPE_INTEGER,
  312. .name = "Exposure",
  313. .minimum = 1,
  314. .maximum = 255,
  315. .step = 1,
  316. .default_value = 255,
  317. .flags = V4L2_CTRL_FLAG_SLIDER,
  318. }, {
  319. .id = V4L2_CID_EXPOSURE_AUTO,
  320. .type = V4L2_CTRL_TYPE_BOOLEAN,
  321. .name = "Automatic Exposure",
  322. .minimum = 0,
  323. .maximum = 1,
  324. .step = 1,
  325. .default_value = 1,
  326. }
  327. };
  328. static int mt9m001_video_probe(struct soc_camera_device *);
  329. static void mt9m001_video_remove(struct soc_camera_device *);
  330. static int mt9m001_get_control(struct soc_camera_device *, struct v4l2_control *);
  331. static int mt9m001_set_control(struct soc_camera_device *, struct v4l2_control *);
  332. static struct soc_camera_ops mt9m001_ops = {
  333. .owner = THIS_MODULE,
  334. .probe = mt9m001_video_probe,
  335. .remove = mt9m001_video_remove,
  336. .init = mt9m001_init,
  337. .release = mt9m001_release,
  338. .start_capture = mt9m001_start_capture,
  339. .stop_capture = mt9m001_stop_capture,
  340. .set_crop = mt9m001_set_crop,
  341. .set_fmt = mt9m001_set_fmt,
  342. .try_fmt = mt9m001_try_fmt,
  343. .set_bus_param = mt9m001_set_bus_param,
  344. .query_bus_param = mt9m001_query_bus_param,
  345. .controls = mt9m001_controls,
  346. .num_controls = ARRAY_SIZE(mt9m001_controls),
  347. .get_control = mt9m001_get_control,
  348. .set_control = mt9m001_set_control,
  349. .get_chip_id = mt9m001_get_chip_id,
  350. #ifdef CONFIG_VIDEO_ADV_DEBUG
  351. .get_register = mt9m001_get_register,
  352. .set_register = mt9m001_set_register,
  353. #endif
  354. };
  355. static int mt9m001_get_control(struct soc_camera_device *icd, struct v4l2_control *ctrl)
  356. {
  357. struct mt9m001 *mt9m001 = container_of(icd, struct mt9m001, icd);
  358. int data;
  359. switch (ctrl->id) {
  360. case V4L2_CID_VFLIP:
  361. data = reg_read(icd, MT9M001_READ_OPTIONS2);
  362. if (data < 0)
  363. return -EIO;
  364. ctrl->value = !!(data & 0x8000);
  365. break;
  366. case V4L2_CID_EXPOSURE_AUTO:
  367. ctrl->value = mt9m001->autoexposure;
  368. break;
  369. }
  370. return 0;
  371. }
  372. static int mt9m001_set_control(struct soc_camera_device *icd, struct v4l2_control *ctrl)
  373. {
  374. struct mt9m001 *mt9m001 = container_of(icd, struct mt9m001, icd);
  375. const struct v4l2_queryctrl *qctrl;
  376. int data;
  377. qctrl = soc_camera_find_qctrl(&mt9m001_ops, ctrl->id);
  378. if (!qctrl)
  379. return -EINVAL;
  380. switch (ctrl->id) {
  381. case V4L2_CID_VFLIP:
  382. if (ctrl->value)
  383. data = reg_set(icd, MT9M001_READ_OPTIONS2, 0x8000);
  384. else
  385. data = reg_clear(icd, MT9M001_READ_OPTIONS2, 0x8000);
  386. if (data < 0)
  387. return -EIO;
  388. break;
  389. case V4L2_CID_GAIN:
  390. if (ctrl->value > qctrl->maximum || ctrl->value < qctrl->minimum)
  391. return -EINVAL;
  392. /* See Datasheet Table 7, Gain settings. */
  393. if (ctrl->value <= qctrl->default_value) {
  394. /* Pack it into 0..1 step 0.125, register values 0..8 */
  395. unsigned long range = qctrl->default_value - qctrl->minimum;
  396. data = ((ctrl->value - qctrl->minimum) * 8 + range / 2) / range;
  397. dev_dbg(&icd->dev, "Setting gain %d\n", data);
  398. data = reg_write(icd, MT9M001_GLOBAL_GAIN, data);
  399. if (data < 0)
  400. return -EIO;
  401. } else {
  402. /* Pack it into 1.125..15 variable step, register values 9..67 */
  403. /* We assume qctrl->maximum - qctrl->default_value - 1 > 0 */
  404. unsigned long range = qctrl->maximum - qctrl->default_value - 1;
  405. unsigned long gain = ((ctrl->value - qctrl->default_value - 1) *
  406. 111 + range / 2) / range + 9;
  407. if (gain <= 32)
  408. data = gain;
  409. else if (gain <= 64)
  410. data = ((gain - 32) * 16 + 16) / 32 + 80;
  411. else
  412. data = ((gain - 64) * 7 + 28) / 56 + 96;
  413. dev_dbg(&icd->dev, "Setting gain from %d to %d\n",
  414. reg_read(icd, MT9M001_GLOBAL_GAIN), data);
  415. data = reg_write(icd, MT9M001_GLOBAL_GAIN, data);
  416. if (data < 0)
  417. return -EIO;
  418. }
  419. /* Success */
  420. icd->gain = ctrl->value;
  421. break;
  422. case V4L2_CID_EXPOSURE:
  423. /* mt9m001 has maximum == default */
  424. if (ctrl->value > qctrl->maximum || ctrl->value < qctrl->minimum)
  425. return -EINVAL;
  426. else {
  427. unsigned long range = qctrl->maximum - qctrl->minimum;
  428. unsigned long shutter = ((ctrl->value - qctrl->minimum) * 1048 +
  429. range / 2) / range + 1;
  430. dev_dbg(&icd->dev, "Setting shutter width from %d to %lu\n",
  431. reg_read(icd, MT9M001_SHUTTER_WIDTH), shutter);
  432. if (reg_write(icd, MT9M001_SHUTTER_WIDTH, shutter) < 0)
  433. return -EIO;
  434. icd->exposure = ctrl->value;
  435. mt9m001->autoexposure = 0;
  436. }
  437. break;
  438. case V4L2_CID_EXPOSURE_AUTO:
  439. if (ctrl->value) {
  440. const u16 vblank = 25;
  441. if (reg_write(icd, MT9M001_SHUTTER_WIDTH, icd->height +
  442. icd->y_skip_top + vblank) < 0)
  443. return -EIO;
  444. qctrl = soc_camera_find_qctrl(icd->ops, V4L2_CID_EXPOSURE);
  445. icd->exposure = (524 + (icd->height + icd->y_skip_top + vblank - 1) *
  446. (qctrl->maximum - qctrl->minimum)) /
  447. 1048 + qctrl->minimum;
  448. mt9m001->autoexposure = 1;
  449. } else
  450. mt9m001->autoexposure = 0;
  451. break;
  452. }
  453. return 0;
  454. }
  455. /* Interface active, can use i2c. If it fails, it can indeed mean, that
  456. * this wasn't our capture interface, so, we wait for the right one */
  457. static int mt9m001_video_probe(struct soc_camera_device *icd)
  458. {
  459. struct mt9m001 *mt9m001 = container_of(icd, struct mt9m001, icd);
  460. struct soc_camera_link *icl = mt9m001->client->dev.platform_data;
  461. s32 data;
  462. int ret;
  463. unsigned long flags;
  464. /* We must have a parent by now. And it cannot be a wrong one.
  465. * So this entire test is completely redundant. */
  466. if (!icd->dev.parent ||
  467. to_soc_camera_host(icd->dev.parent)->nr != icd->iface)
  468. return -ENODEV;
  469. /* Enable the chip */
  470. data = reg_write(icd, MT9M001_CHIP_ENABLE, 1);
  471. dev_dbg(&icd->dev, "write: %d\n", data);
  472. /* Read out the chip version register */
  473. data = reg_read(icd, MT9M001_CHIP_VERSION);
  474. /* must be 0x8411 or 0x8421 for colour sensor and 8431 for bw */
  475. switch (data) {
  476. case 0x8411:
  477. case 0x8421:
  478. mt9m001->model = V4L2_IDENT_MT9M001C12ST;
  479. icd->formats = mt9m001_colour_formats;
  480. break;
  481. case 0x8431:
  482. mt9m001->model = V4L2_IDENT_MT9M001C12STM;
  483. icd->formats = mt9m001_monochrome_formats;
  484. break;
  485. default:
  486. ret = -ENODEV;
  487. dev_err(&icd->dev,
  488. "No MT9M001 chip detected, register read %x\n", data);
  489. goto ei2c;
  490. }
  491. icd->num_formats = 0;
  492. /*
  493. * This is a 10bit sensor, so by default we only allow 10bit.
  494. * The platform may support different bus widths due to
  495. * different routing of the data lines.
  496. */
  497. if (icl->query_bus_param)
  498. flags = icl->query_bus_param(icl);
  499. else
  500. flags = SOCAM_DATAWIDTH_10;
  501. if (flags & SOCAM_DATAWIDTH_10)
  502. icd->num_formats++;
  503. else
  504. icd->formats++;
  505. if (flags & SOCAM_DATAWIDTH_8)
  506. icd->num_formats++;
  507. dev_info(&icd->dev, "Detected a MT9M001 chip ID %x (%s)\n", data,
  508. data == 0x8431 ? "C12STM" : "C12ST");
  509. /* Now that we know the model, we can start video */
  510. ret = soc_camera_video_start(icd);
  511. if (ret)
  512. goto eisis;
  513. return 0;
  514. eisis:
  515. ei2c:
  516. return ret;
  517. }
  518. static void mt9m001_video_remove(struct soc_camera_device *icd)
  519. {
  520. struct mt9m001 *mt9m001 = container_of(icd, struct mt9m001, icd);
  521. dev_dbg(&icd->dev, "Video %x removed: %p, %p\n", mt9m001->client->addr,
  522. icd->dev.parent, icd->vdev);
  523. soc_camera_video_stop(icd);
  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");