mt9m001.c 19 KB

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