mt9m001.c 18 KB

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