mt9m001.c 18 KB

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