mt9m001.c 19 KB

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