mt9m001.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717
  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. 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 i2c_client *client = to_i2c_client(icd->control);
  268. if (reg->match.type != V4L2_CHIP_MATCH_I2C_ADDR || reg->reg > 0xff)
  269. return -EINVAL;
  270. if (reg->match.addr != client->addr)
  271. return -ENODEV;
  272. reg->size = 2;
  273. reg->val = reg_read(client, 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 i2c_client *client = to_i2c_client(icd->control);
  282. if (reg->match.type != V4L2_CHIP_MATCH_I2C_ADDR || reg->reg > 0xff)
  283. return -EINVAL;
  284. if (reg->match.addr != client->addr)
  285. return -ENODEV;
  286. if (reg_write(client, 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 i2c_client *client = to_i2c_client(icd->control);
  358. struct mt9m001 *mt9m001 = container_of(icd, struct mt9m001, icd);
  359. int data;
  360. switch (ctrl->id) {
  361. case V4L2_CID_VFLIP:
  362. data = reg_read(client, MT9M001_READ_OPTIONS2);
  363. if (data < 0)
  364. return -EIO;
  365. ctrl->value = !!(data & 0x8000);
  366. break;
  367. case V4L2_CID_EXPOSURE_AUTO:
  368. ctrl->value = mt9m001->autoexposure;
  369. break;
  370. }
  371. return 0;
  372. }
  373. static int mt9m001_set_control(struct soc_camera_device *icd, struct v4l2_control *ctrl)
  374. {
  375. struct i2c_client *client = to_i2c_client(icd->control);
  376. struct mt9m001 *mt9m001 = container_of(icd, struct mt9m001, icd);
  377. const struct v4l2_queryctrl *qctrl;
  378. int data;
  379. qctrl = soc_camera_find_qctrl(&mt9m001_ops, ctrl->id);
  380. if (!qctrl)
  381. return -EINVAL;
  382. switch (ctrl->id) {
  383. case V4L2_CID_VFLIP:
  384. if (ctrl->value)
  385. data = reg_set(client, MT9M001_READ_OPTIONS2, 0x8000);
  386. else
  387. data = reg_clear(client, MT9M001_READ_OPTIONS2, 0x8000);
  388. if (data < 0)
  389. return -EIO;
  390. break;
  391. case V4L2_CID_GAIN:
  392. if (ctrl->value > qctrl->maximum || ctrl->value < qctrl->minimum)
  393. return -EINVAL;
  394. /* See Datasheet Table 7, Gain settings. */
  395. if (ctrl->value <= qctrl->default_value) {
  396. /* Pack it into 0..1 step 0.125, register values 0..8 */
  397. unsigned long range = qctrl->default_value - qctrl->minimum;
  398. data = ((ctrl->value - qctrl->minimum) * 8 + range / 2) / range;
  399. dev_dbg(&icd->dev, "Setting gain %d\n", data);
  400. data = reg_write(client, MT9M001_GLOBAL_GAIN, data);
  401. if (data < 0)
  402. return -EIO;
  403. } else {
  404. /* Pack it into 1.125..15 variable step, register values 9..67 */
  405. /* We assume qctrl->maximum - qctrl->default_value - 1 > 0 */
  406. unsigned long range = qctrl->maximum - qctrl->default_value - 1;
  407. unsigned long gain = ((ctrl->value - qctrl->default_value - 1) *
  408. 111 + range / 2) / range + 9;
  409. if (gain <= 32)
  410. data = gain;
  411. else if (gain <= 64)
  412. data = ((gain - 32) * 16 + 16) / 32 + 80;
  413. else
  414. data = ((gain - 64) * 7 + 28) / 56 + 96;
  415. dev_dbg(&icd->dev, "Setting gain from %d to %d\n",
  416. reg_read(client, MT9M001_GLOBAL_GAIN), data);
  417. data = reg_write(client, MT9M001_GLOBAL_GAIN, data);
  418. if (data < 0)
  419. return -EIO;
  420. }
  421. /* Success */
  422. icd->gain = ctrl->value;
  423. break;
  424. case V4L2_CID_EXPOSURE:
  425. /* mt9m001 has maximum == default */
  426. if (ctrl->value > qctrl->maximum || ctrl->value < qctrl->minimum)
  427. return -EINVAL;
  428. else {
  429. unsigned long range = qctrl->maximum - qctrl->minimum;
  430. unsigned long shutter = ((ctrl->value - qctrl->minimum) * 1048 +
  431. range / 2) / range + 1;
  432. dev_dbg(&icd->dev, "Setting shutter width from %d to %lu\n",
  433. reg_read(client, MT9M001_SHUTTER_WIDTH), shutter);
  434. if (reg_write(client, MT9M001_SHUTTER_WIDTH, shutter) < 0)
  435. return -EIO;
  436. icd->exposure = ctrl->value;
  437. mt9m001->autoexposure = 0;
  438. }
  439. break;
  440. case V4L2_CID_EXPOSURE_AUTO:
  441. if (ctrl->value) {
  442. const u16 vblank = 25;
  443. if (reg_write(client, MT9M001_SHUTTER_WIDTH, icd->height +
  444. icd->y_skip_top + vblank) < 0)
  445. return -EIO;
  446. qctrl = soc_camera_find_qctrl(icd->ops, V4L2_CID_EXPOSURE);
  447. icd->exposure = (524 + (icd->height + icd->y_skip_top + vblank - 1) *
  448. (qctrl->maximum - qctrl->minimum)) /
  449. 1048 + qctrl->minimum;
  450. mt9m001->autoexposure = 1;
  451. } else
  452. mt9m001->autoexposure = 0;
  453. break;
  454. }
  455. return 0;
  456. }
  457. /* Interface active, can use i2c. If it fails, it can indeed mean, that
  458. * this wasn't our capture interface, so, we wait for the right one */
  459. static int mt9m001_video_probe(struct soc_camera_device *icd)
  460. {
  461. struct i2c_client *client = to_i2c_client(icd->control);
  462. struct mt9m001 *mt9m001 = container_of(icd, struct mt9m001, icd);
  463. struct soc_camera_link *icl = client->dev.platform_data;
  464. s32 data;
  465. int ret;
  466. unsigned long flags;
  467. /* We must have a parent by now. And it cannot be a wrong one.
  468. * So this entire test is completely redundant. */
  469. if (!icd->dev.parent ||
  470. to_soc_camera_host(icd->dev.parent)->nr != icd->iface)
  471. return -ENODEV;
  472. /* Enable the chip */
  473. data = reg_write(client, MT9M001_CHIP_ENABLE, 1);
  474. dev_dbg(&icd->dev, "write: %d\n", data);
  475. /* Read out the chip version register */
  476. data = reg_read(client, MT9M001_CHIP_VERSION);
  477. /* must be 0x8411 or 0x8421 for colour sensor and 8431 for bw */
  478. switch (data) {
  479. case 0x8411:
  480. case 0x8421:
  481. mt9m001->model = V4L2_IDENT_MT9M001C12ST;
  482. icd->formats = mt9m001_colour_formats;
  483. break;
  484. case 0x8431:
  485. mt9m001->model = V4L2_IDENT_MT9M001C12STM;
  486. icd->formats = mt9m001_monochrome_formats;
  487. break;
  488. default:
  489. ret = -ENODEV;
  490. dev_err(&icd->dev,
  491. "No MT9M001 chip detected, register read %x\n", data);
  492. goto ei2c;
  493. }
  494. icd->num_formats = 0;
  495. /*
  496. * This is a 10bit sensor, so by default we only allow 10bit.
  497. * The platform may support different bus widths due to
  498. * different routing of the data lines.
  499. */
  500. if (icl->query_bus_param)
  501. flags = icl->query_bus_param(icl);
  502. else
  503. flags = SOCAM_DATAWIDTH_10;
  504. if (flags & SOCAM_DATAWIDTH_10)
  505. icd->num_formats++;
  506. else
  507. icd->formats++;
  508. if (flags & SOCAM_DATAWIDTH_8)
  509. icd->num_formats++;
  510. dev_info(&icd->dev, "Detected a MT9M001 chip ID %x (%s)\n", data,
  511. data == 0x8431 ? "C12STM" : "C12ST");
  512. /* Now that we know the model, we can start video */
  513. ret = soc_camera_video_start(icd);
  514. if (ret)
  515. goto eisis;
  516. return 0;
  517. eisis:
  518. ei2c:
  519. return ret;
  520. }
  521. static void mt9m001_video_remove(struct soc_camera_device *icd)
  522. {
  523. struct mt9m001 *mt9m001 = container_of(icd, struct mt9m001, icd);
  524. struct soc_camera_link *icl = mt9m001->client->dev.platform_data;
  525. dev_dbg(&icd->dev, "Video %x removed: %p, %p\n", mt9m001->client->addr,
  526. icd->dev.parent, icd->vdev);
  527. soc_camera_video_stop(icd);
  528. if (icl->free_bus)
  529. icl->free_bus(icl);
  530. }
  531. static int mt9m001_probe(struct i2c_client *client,
  532. const struct i2c_device_id *did)
  533. {
  534. struct mt9m001 *mt9m001;
  535. struct soc_camera_device *icd;
  536. struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
  537. struct soc_camera_link *icl = client->dev.platform_data;
  538. int ret;
  539. if (!icl) {
  540. dev_err(&client->dev, "MT9M001 driver needs platform data\n");
  541. return -EINVAL;
  542. }
  543. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WORD_DATA)) {
  544. dev_warn(&adapter->dev,
  545. "I2C-Adapter doesn't support I2C_FUNC_SMBUS_WORD\n");
  546. return -EIO;
  547. }
  548. mt9m001 = kzalloc(sizeof(struct mt9m001), GFP_KERNEL);
  549. if (!mt9m001)
  550. return -ENOMEM;
  551. mt9m001->client = client;
  552. i2c_set_clientdata(client, mt9m001);
  553. /* Second stage probe - when a capture adapter is there */
  554. icd = &mt9m001->icd;
  555. icd->ops = &mt9m001_ops;
  556. icd->control = &client->dev;
  557. icd->x_min = 20;
  558. icd->y_min = 12;
  559. icd->x_current = 20;
  560. icd->y_current = 12;
  561. icd->width_min = 48;
  562. icd->width_max = 1280;
  563. icd->height_min = 32;
  564. icd->height_max = 1024;
  565. icd->y_skip_top = 1;
  566. icd->iface = icl->bus_id;
  567. /* Simulated autoexposure. If enabled, we calculate shutter width
  568. * ourselves in the driver based on vertical blanking and frame width */
  569. mt9m001->autoexposure = 1;
  570. ret = soc_camera_device_register(icd);
  571. if (ret)
  572. goto eisdr;
  573. return 0;
  574. eisdr:
  575. kfree(mt9m001);
  576. return ret;
  577. }
  578. static int mt9m001_remove(struct i2c_client *client)
  579. {
  580. struct mt9m001 *mt9m001 = i2c_get_clientdata(client);
  581. soc_camera_device_unregister(&mt9m001->icd);
  582. kfree(mt9m001);
  583. return 0;
  584. }
  585. static const struct i2c_device_id mt9m001_id[] = {
  586. { "mt9m001", 0 },
  587. { }
  588. };
  589. MODULE_DEVICE_TABLE(i2c, mt9m001_id);
  590. static struct i2c_driver mt9m001_i2c_driver = {
  591. .driver = {
  592. .name = "mt9m001",
  593. },
  594. .probe = mt9m001_probe,
  595. .remove = mt9m001_remove,
  596. .id_table = mt9m001_id,
  597. };
  598. static int __init mt9m001_mod_init(void)
  599. {
  600. return i2c_add_driver(&mt9m001_i2c_driver);
  601. }
  602. static void __exit mt9m001_mod_exit(void)
  603. {
  604. i2c_del_driver(&mt9m001_i2c_driver);
  605. }
  606. module_init(mt9m001_mod_init);
  607. module_exit(mt9m001_mod_exit);
  608. MODULE_DESCRIPTION("Micron MT9M001 Camera driver");
  609. MODULE_AUTHOR("Guennadi Liakhovetski <kernel@pengutronix.de>");
  610. MODULE_LICENSE("GPL");