mt9m001.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772
  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_DATA_ACTIVE_HIGH | 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_crop(struct soc_camera_device *icd,
  246. 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_set_fmt(struct soc_camera_device *icd,
  282. struct v4l2_format *f)
  283. {
  284. struct v4l2_rect rect = {
  285. .left = icd->x_current,
  286. .top = icd->y_current,
  287. .width = f->fmt.pix.width,
  288. .height = f->fmt.pix.height,
  289. };
  290. /* No support for scaling so far, just crop. TODO: use skipping */
  291. return mt9m001_set_crop(icd, &rect);
  292. }
  293. static int mt9m001_try_fmt(struct soc_camera_device *icd,
  294. struct v4l2_format *f)
  295. {
  296. struct v4l2_pix_format *pix = &f->fmt.pix;
  297. if (pix->height < 32 + icd->y_skip_top)
  298. pix->height = 32 + icd->y_skip_top;
  299. if (pix->height > 1024 + icd->y_skip_top)
  300. pix->height = 1024 + icd->y_skip_top;
  301. if (pix->width < 48)
  302. pix->width = 48;
  303. if (pix->width > 1280)
  304. pix->width = 1280;
  305. pix->width &= ~0x01; /* has to be even, unsure why was ~3 */
  306. return 0;
  307. }
  308. static int mt9m001_get_chip_id(struct soc_camera_device *icd,
  309. struct v4l2_dbg_chip_ident *id)
  310. {
  311. struct mt9m001 *mt9m001 = container_of(icd, struct mt9m001, icd);
  312. if (id->match.type != V4L2_CHIP_MATCH_I2C_ADDR)
  313. return -EINVAL;
  314. if (id->match.addr != mt9m001->client->addr)
  315. return -ENODEV;
  316. id->ident = mt9m001->model;
  317. id->revision = 0;
  318. return 0;
  319. }
  320. #ifdef CONFIG_VIDEO_ADV_DEBUG
  321. static int mt9m001_get_register(struct soc_camera_device *icd,
  322. struct v4l2_dbg_register *reg)
  323. {
  324. struct mt9m001 *mt9m001 = container_of(icd, struct mt9m001, icd);
  325. if (reg->match.type != V4L2_CHIP_MATCH_I2C_ADDR || reg->reg > 0xff)
  326. return -EINVAL;
  327. if (reg->match.addr != mt9m001->client->addr)
  328. return -ENODEV;
  329. reg->size = 2;
  330. reg->val = reg_read(icd, reg->reg);
  331. if (reg->val > 0xffff)
  332. return -EIO;
  333. return 0;
  334. }
  335. static int mt9m001_set_register(struct soc_camera_device *icd,
  336. struct v4l2_dbg_register *reg)
  337. {
  338. struct mt9m001 *mt9m001 = container_of(icd, struct mt9m001, icd);
  339. if (reg->match.type != V4L2_CHIP_MATCH_I2C_ADDR || reg->reg > 0xff)
  340. return -EINVAL;
  341. if (reg->match.addr != mt9m001->client->addr)
  342. return -ENODEV;
  343. if (reg_write(icd, reg->reg, reg->val) < 0)
  344. return -EIO;
  345. return 0;
  346. }
  347. #endif
  348. static const struct v4l2_queryctrl mt9m001_controls[] = {
  349. {
  350. .id = V4L2_CID_VFLIP,
  351. .type = V4L2_CTRL_TYPE_BOOLEAN,
  352. .name = "Flip Vertically",
  353. .minimum = 0,
  354. .maximum = 1,
  355. .step = 1,
  356. .default_value = 0,
  357. }, {
  358. .id = V4L2_CID_GAIN,
  359. .type = V4L2_CTRL_TYPE_INTEGER,
  360. .name = "Gain",
  361. .minimum = 0,
  362. .maximum = 127,
  363. .step = 1,
  364. .default_value = 64,
  365. .flags = V4L2_CTRL_FLAG_SLIDER,
  366. }, {
  367. .id = V4L2_CID_EXPOSURE,
  368. .type = V4L2_CTRL_TYPE_INTEGER,
  369. .name = "Exposure",
  370. .minimum = 1,
  371. .maximum = 255,
  372. .step = 1,
  373. .default_value = 255,
  374. .flags = V4L2_CTRL_FLAG_SLIDER,
  375. }, {
  376. .id = V4L2_CID_EXPOSURE_AUTO,
  377. .type = V4L2_CTRL_TYPE_BOOLEAN,
  378. .name = "Automatic Exposure",
  379. .minimum = 0,
  380. .maximum = 1,
  381. .step = 1,
  382. .default_value = 1,
  383. }
  384. };
  385. static int mt9m001_video_probe(struct soc_camera_device *);
  386. static void mt9m001_video_remove(struct soc_camera_device *);
  387. static int mt9m001_get_control(struct soc_camera_device *, struct v4l2_control *);
  388. static int mt9m001_set_control(struct soc_camera_device *, struct v4l2_control *);
  389. static struct soc_camera_ops mt9m001_ops = {
  390. .owner = THIS_MODULE,
  391. .probe = mt9m001_video_probe,
  392. .remove = mt9m001_video_remove,
  393. .init = mt9m001_init,
  394. .release = mt9m001_release,
  395. .start_capture = mt9m001_start_capture,
  396. .stop_capture = mt9m001_stop_capture,
  397. .set_crop = mt9m001_set_crop,
  398. .set_fmt = mt9m001_set_fmt,
  399. .try_fmt = mt9m001_try_fmt,
  400. .set_bus_param = mt9m001_set_bus_param,
  401. .query_bus_param = mt9m001_query_bus_param,
  402. .controls = mt9m001_controls,
  403. .num_controls = ARRAY_SIZE(mt9m001_controls),
  404. .get_control = mt9m001_get_control,
  405. .set_control = mt9m001_set_control,
  406. .get_chip_id = mt9m001_get_chip_id,
  407. #ifdef CONFIG_VIDEO_ADV_DEBUG
  408. .get_register = mt9m001_get_register,
  409. .set_register = mt9m001_set_register,
  410. #endif
  411. };
  412. static int mt9m001_get_control(struct soc_camera_device *icd, struct v4l2_control *ctrl)
  413. {
  414. struct mt9m001 *mt9m001 = container_of(icd, struct mt9m001, icd);
  415. int data;
  416. switch (ctrl->id) {
  417. case V4L2_CID_VFLIP:
  418. data = reg_read(icd, MT9M001_READ_OPTIONS2);
  419. if (data < 0)
  420. return -EIO;
  421. ctrl->value = !!(data & 0x8000);
  422. break;
  423. case V4L2_CID_EXPOSURE_AUTO:
  424. ctrl->value = mt9m001->autoexposure;
  425. break;
  426. }
  427. return 0;
  428. }
  429. static int mt9m001_set_control(struct soc_camera_device *icd, struct v4l2_control *ctrl)
  430. {
  431. struct mt9m001 *mt9m001 = container_of(icd, struct mt9m001, icd);
  432. const struct v4l2_queryctrl *qctrl;
  433. int data;
  434. qctrl = soc_camera_find_qctrl(&mt9m001_ops, ctrl->id);
  435. if (!qctrl)
  436. return -EINVAL;
  437. switch (ctrl->id) {
  438. case V4L2_CID_VFLIP:
  439. if (ctrl->value)
  440. data = reg_set(icd, MT9M001_READ_OPTIONS2, 0x8000);
  441. else
  442. data = reg_clear(icd, MT9M001_READ_OPTIONS2, 0x8000);
  443. if (data < 0)
  444. return -EIO;
  445. break;
  446. case V4L2_CID_GAIN:
  447. if (ctrl->value > qctrl->maximum || ctrl->value < qctrl->minimum)
  448. return -EINVAL;
  449. /* See Datasheet Table 7, Gain settings. */
  450. if (ctrl->value <= qctrl->default_value) {
  451. /* Pack it into 0..1 step 0.125, register values 0..8 */
  452. unsigned long range = qctrl->default_value - qctrl->minimum;
  453. data = ((ctrl->value - qctrl->minimum) * 8 + range / 2) / range;
  454. dev_dbg(&icd->dev, "Setting gain %d\n", data);
  455. data = reg_write(icd, MT9M001_GLOBAL_GAIN, data);
  456. if (data < 0)
  457. return -EIO;
  458. } else {
  459. /* Pack it into 1.125..15 variable step, register values 9..67 */
  460. /* We assume qctrl->maximum - qctrl->default_value - 1 > 0 */
  461. unsigned long range = qctrl->maximum - qctrl->default_value - 1;
  462. unsigned long gain = ((ctrl->value - qctrl->default_value - 1) *
  463. 111 + range / 2) / range + 9;
  464. if (gain <= 32)
  465. data = gain;
  466. else if (gain <= 64)
  467. data = ((gain - 32) * 16 + 16) / 32 + 80;
  468. else
  469. data = ((gain - 64) * 7 + 28) / 56 + 96;
  470. dev_dbg(&icd->dev, "Setting gain from %d to %d\n",
  471. reg_read(icd, MT9M001_GLOBAL_GAIN), data);
  472. data = reg_write(icd, MT9M001_GLOBAL_GAIN, data);
  473. if (data < 0)
  474. return -EIO;
  475. }
  476. /* Success */
  477. icd->gain = ctrl->value;
  478. break;
  479. case V4L2_CID_EXPOSURE:
  480. /* mt9m001 has maximum == default */
  481. if (ctrl->value > qctrl->maximum || ctrl->value < qctrl->minimum)
  482. return -EINVAL;
  483. else {
  484. unsigned long range = qctrl->maximum - qctrl->minimum;
  485. unsigned long shutter = ((ctrl->value - qctrl->minimum) * 1048 +
  486. range / 2) / range + 1;
  487. dev_dbg(&icd->dev, "Setting shutter width from %d to %lu\n",
  488. reg_read(icd, MT9M001_SHUTTER_WIDTH), shutter);
  489. if (reg_write(icd, MT9M001_SHUTTER_WIDTH, shutter) < 0)
  490. return -EIO;
  491. icd->exposure = ctrl->value;
  492. mt9m001->autoexposure = 0;
  493. }
  494. break;
  495. case V4L2_CID_EXPOSURE_AUTO:
  496. if (ctrl->value) {
  497. const u16 vblank = 25;
  498. if (reg_write(icd, MT9M001_SHUTTER_WIDTH, icd->height +
  499. icd->y_skip_top + vblank) < 0)
  500. return -EIO;
  501. qctrl = soc_camera_find_qctrl(icd->ops, V4L2_CID_EXPOSURE);
  502. icd->exposure = (524 + (icd->height + icd->y_skip_top + vblank - 1) *
  503. (qctrl->maximum - qctrl->minimum)) /
  504. 1048 + qctrl->minimum;
  505. mt9m001->autoexposure = 1;
  506. } else
  507. mt9m001->autoexposure = 0;
  508. break;
  509. }
  510. return 0;
  511. }
  512. /* Interface active, can use i2c. If it fails, it can indeed mean, that
  513. * this wasn't our capture interface, so, we wait for the right one */
  514. static int mt9m001_video_probe(struct soc_camera_device *icd)
  515. {
  516. struct mt9m001 *mt9m001 = container_of(icd, struct mt9m001, icd);
  517. struct soc_camera_link *icl = mt9m001->client->dev.platform_data;
  518. s32 data;
  519. int ret;
  520. /* We must have a parent by now. And it cannot be a wrong one.
  521. * So this entire test is completely redundant. */
  522. if (!icd->dev.parent ||
  523. to_soc_camera_host(icd->dev.parent)->nr != icd->iface)
  524. return -ENODEV;
  525. /* Enable the chip */
  526. data = reg_write(icd, MT9M001_CHIP_ENABLE, 1);
  527. dev_dbg(&icd->dev, "write: %d\n", data);
  528. /* Read out the chip version register */
  529. data = reg_read(icd, MT9M001_CHIP_VERSION);
  530. /* must be 0x8411 or 0x8421 for colour sensor and 8431 for bw */
  531. switch (data) {
  532. case 0x8411:
  533. case 0x8421:
  534. mt9m001->model = V4L2_IDENT_MT9M001C12ST;
  535. icd->formats = mt9m001_colour_formats;
  536. if (gpio_is_valid(icl->gpio))
  537. icd->num_formats = ARRAY_SIZE(mt9m001_colour_formats);
  538. else
  539. icd->num_formats = 1;
  540. break;
  541. case 0x8431:
  542. mt9m001->model = V4L2_IDENT_MT9M001C12STM;
  543. icd->formats = mt9m001_monochrome_formats;
  544. if (gpio_is_valid(icl->gpio))
  545. icd->num_formats = ARRAY_SIZE(mt9m001_monochrome_formats);
  546. else
  547. icd->num_formats = 1;
  548. break;
  549. default:
  550. ret = -ENODEV;
  551. dev_err(&icd->dev,
  552. "No MT9M001 chip detected, register read %x\n", data);
  553. goto ei2c;
  554. }
  555. dev_info(&icd->dev, "Detected a MT9M001 chip ID %x (%s)\n", data,
  556. data == 0x8431 ? "C12STM" : "C12ST");
  557. /* Now that we know the model, we can start video */
  558. ret = soc_camera_video_start(icd);
  559. if (ret)
  560. goto eisis;
  561. return 0;
  562. eisis:
  563. ei2c:
  564. return ret;
  565. }
  566. static void mt9m001_video_remove(struct soc_camera_device *icd)
  567. {
  568. struct mt9m001 *mt9m001 = container_of(icd, struct mt9m001, icd);
  569. dev_dbg(&icd->dev, "Video %x removed: %p, %p\n", mt9m001->client->addr,
  570. icd->dev.parent, icd->vdev);
  571. soc_camera_video_stop(icd);
  572. }
  573. static int mt9m001_probe(struct i2c_client *client,
  574. const struct i2c_device_id *did)
  575. {
  576. struct mt9m001 *mt9m001;
  577. struct soc_camera_device *icd;
  578. struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
  579. struct soc_camera_link *icl = client->dev.platform_data;
  580. int ret;
  581. if (!icl) {
  582. dev_err(&client->dev, "MT9M001 driver needs platform data\n");
  583. return -EINVAL;
  584. }
  585. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WORD_DATA)) {
  586. dev_warn(&adapter->dev,
  587. "I2C-Adapter doesn't support I2C_FUNC_SMBUS_WORD\n");
  588. return -EIO;
  589. }
  590. mt9m001 = kzalloc(sizeof(struct mt9m001), GFP_KERNEL);
  591. if (!mt9m001)
  592. return -ENOMEM;
  593. mt9m001->client = client;
  594. i2c_set_clientdata(client, mt9m001);
  595. /* Second stage probe - when a capture adapter is there */
  596. icd = &mt9m001->icd;
  597. icd->ops = &mt9m001_ops;
  598. icd->control = &client->dev;
  599. icd->x_min = 20;
  600. icd->y_min = 12;
  601. icd->x_current = 20;
  602. icd->y_current = 12;
  603. icd->width_min = 48;
  604. icd->width_max = 1280;
  605. icd->height_min = 32;
  606. icd->height_max = 1024;
  607. icd->y_skip_top = 1;
  608. icd->iface = icl->bus_id;
  609. /* Default datawidth - this is the only width this camera (normally)
  610. * supports. It is only with extra logic that it can support
  611. * other widths. Therefore it seems to be a sensible default. */
  612. mt9m001->datawidth = 10;
  613. /* Simulated autoexposure. If enabled, we calculate shutter width
  614. * ourselves in the driver based on vertical blanking and frame width */
  615. mt9m001->autoexposure = 1;
  616. ret = bus_switch_request(mt9m001, icl);
  617. if (ret)
  618. goto eswinit;
  619. ret = soc_camera_device_register(icd);
  620. if (ret)
  621. goto eisdr;
  622. return 0;
  623. eisdr:
  624. bus_switch_release(mt9m001);
  625. eswinit:
  626. kfree(mt9m001);
  627. return ret;
  628. }
  629. static int mt9m001_remove(struct i2c_client *client)
  630. {
  631. struct mt9m001 *mt9m001 = i2c_get_clientdata(client);
  632. soc_camera_device_unregister(&mt9m001->icd);
  633. bus_switch_release(mt9m001);
  634. kfree(mt9m001);
  635. return 0;
  636. }
  637. static const struct i2c_device_id mt9m001_id[] = {
  638. { "mt9m001", 0 },
  639. { }
  640. };
  641. MODULE_DEVICE_TABLE(i2c, mt9m001_id);
  642. static struct i2c_driver mt9m001_i2c_driver = {
  643. .driver = {
  644. .name = "mt9m001",
  645. },
  646. .probe = mt9m001_probe,
  647. .remove = mt9m001_remove,
  648. .id_table = mt9m001_id,
  649. };
  650. static int __init mt9m001_mod_init(void)
  651. {
  652. return i2c_add_driver(&mt9m001_i2c_driver);
  653. }
  654. static void __exit mt9m001_mod_exit(void)
  655. {
  656. i2c_del_driver(&mt9m001_i2c_driver);
  657. }
  658. module_init(mt9m001_mod_init);
  659. module_exit(mt9m001_mod_exit);
  660. MODULE_DESCRIPTION("Micron MT9M001 Camera driver");
  661. MODULE_AUTHOR("Guennadi Liakhovetski <kernel@pengutronix.de>");
  662. MODULE_LICENSE("GPL");