mt9m001.c 19 KB

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