mt9m001.c 18 KB

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