mt9m001.c 19 KB

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