mt9v022.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836
  1. /*
  2. * Driver for MT9V022 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/delay.h>
  14. #include <linux/log2.h>
  15. #include <media/v4l2-common.h>
  16. #include <media/v4l2-chip-ident.h>
  17. #include <media/soc_camera.h>
  18. /* mt9v022 i2c address 0x48, 0x4c, 0x58, 0x5c
  19. * The platform has to define i2c_board_info
  20. * and call i2c_register_board_info() */
  21. static char *sensor_type;
  22. module_param(sensor_type, charp, S_IRUGO);
  23. MODULE_PARM_DESC(sensor_type, "Sensor type: \"colour\" or \"monochrome\"");
  24. /* mt9v022 selected register addresses */
  25. #define MT9V022_CHIP_VERSION 0x00
  26. #define MT9V022_COLUMN_START 0x01
  27. #define MT9V022_ROW_START 0x02
  28. #define MT9V022_WINDOW_HEIGHT 0x03
  29. #define MT9V022_WINDOW_WIDTH 0x04
  30. #define MT9V022_HORIZONTAL_BLANKING 0x05
  31. #define MT9V022_VERTICAL_BLANKING 0x06
  32. #define MT9V022_CHIP_CONTROL 0x07
  33. #define MT9V022_SHUTTER_WIDTH1 0x08
  34. #define MT9V022_SHUTTER_WIDTH2 0x09
  35. #define MT9V022_SHUTTER_WIDTH_CTRL 0x0a
  36. #define MT9V022_TOTAL_SHUTTER_WIDTH 0x0b
  37. #define MT9V022_RESET 0x0c
  38. #define MT9V022_READ_MODE 0x0d
  39. #define MT9V022_MONITOR_MODE 0x0e
  40. #define MT9V022_PIXEL_OPERATION_MODE 0x0f
  41. #define MT9V022_LED_OUT_CONTROL 0x1b
  42. #define MT9V022_ADC_MODE_CONTROL 0x1c
  43. #define MT9V022_ANALOG_GAIN 0x34
  44. #define MT9V022_BLACK_LEVEL_CALIB_CTRL 0x47
  45. #define MT9V022_PIXCLK_FV_LV 0x74
  46. #define MT9V022_DIGITAL_TEST_PATTERN 0x7f
  47. #define MT9V022_AEC_AGC_ENABLE 0xAF
  48. #define MT9V022_MAX_TOTAL_SHUTTER_WIDTH 0xBD
  49. /* Progressive scan, master, defaults */
  50. #define MT9V022_CHIP_CONTROL_DEFAULT 0x188
  51. static const struct soc_camera_data_format mt9v022_colour_formats[] = {
  52. /* Order important: first natively supported,
  53. * second supported with a GPIO extender */
  54. {
  55. .name = "Bayer (sRGB) 10 bit",
  56. .depth = 10,
  57. .fourcc = V4L2_PIX_FMT_SBGGR16,
  58. .colorspace = V4L2_COLORSPACE_SRGB,
  59. }, {
  60. .name = "Bayer (sRGB) 8 bit",
  61. .depth = 8,
  62. .fourcc = V4L2_PIX_FMT_SBGGR8,
  63. .colorspace = V4L2_COLORSPACE_SRGB,
  64. }
  65. };
  66. static const struct soc_camera_data_format mt9v022_monochrome_formats[] = {
  67. /* Order important - see above */
  68. {
  69. .name = "Monochrome 10 bit",
  70. .depth = 10,
  71. .fourcc = V4L2_PIX_FMT_Y16,
  72. }, {
  73. .name = "Monochrome 8 bit",
  74. .depth = 8,
  75. .fourcc = V4L2_PIX_FMT_GREY,
  76. },
  77. };
  78. struct mt9v022 {
  79. struct i2c_client *client;
  80. struct soc_camera_device icd;
  81. int model; /* V4L2_IDENT_MT9V022* codes from v4l2-chip-ident.h */
  82. u16 chip_control;
  83. };
  84. static int reg_read(struct soc_camera_device *icd, const u8 reg)
  85. {
  86. struct mt9v022 *mt9v022 = container_of(icd, struct mt9v022, icd);
  87. struct i2c_client *client = mt9v022->client;
  88. s32 data = i2c_smbus_read_word_data(client, reg);
  89. return data < 0 ? data : swab16(data);
  90. }
  91. static int reg_write(struct soc_camera_device *icd, const u8 reg,
  92. const u16 data)
  93. {
  94. struct mt9v022 *mt9v022 = container_of(icd, struct mt9v022, icd);
  95. return i2c_smbus_write_word_data(mt9v022->client, reg, swab16(data));
  96. }
  97. static int reg_set(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 reg_clear(struct soc_camera_device *icd, const u8 reg,
  107. const u16 data)
  108. {
  109. int ret;
  110. ret = reg_read(icd, reg);
  111. if (ret < 0)
  112. return ret;
  113. return reg_write(icd, reg, ret & ~data);
  114. }
  115. static int mt9v022_init(struct soc_camera_device *icd)
  116. {
  117. struct mt9v022 *mt9v022 = container_of(icd, struct mt9v022, icd);
  118. struct soc_camera_link *icl = mt9v022->client->dev.platform_data;
  119. int ret;
  120. if (icl->power) {
  121. ret = icl->power(&mt9v022->client->dev, 1);
  122. if (ret < 0) {
  123. dev_err(icd->vdev->parent,
  124. "Platform failed to power-on the camera.\n");
  125. return ret;
  126. }
  127. }
  128. /*
  129. * The camera could have been already on, we hard-reset it additionally,
  130. * if available. Soft reset is done in video_probe().
  131. */
  132. if (icl->reset)
  133. icl->reset(&mt9v022->client->dev);
  134. /* Almost the default mode: master, parallel, simultaneous, and an
  135. * undocumented bit 0x200, which is present in table 7, but not in 8,
  136. * plus snapshot mode to disable scan for now */
  137. mt9v022->chip_control |= 0x10;
  138. ret = reg_write(icd, MT9V022_CHIP_CONTROL, mt9v022->chip_control);
  139. if (!ret)
  140. ret = reg_write(icd, MT9V022_READ_MODE, 0x300);
  141. /* All defaults */
  142. if (!ret)
  143. /* AEC, AGC on */
  144. ret = reg_set(icd, MT9V022_AEC_AGC_ENABLE, 0x3);
  145. if (!ret)
  146. ret = reg_write(icd, MT9V022_MAX_TOTAL_SHUTTER_WIDTH, 480);
  147. if (!ret)
  148. /* default - auto */
  149. ret = reg_clear(icd, MT9V022_BLACK_LEVEL_CALIB_CTRL, 1);
  150. if (!ret)
  151. ret = reg_write(icd, MT9V022_DIGITAL_TEST_PATTERN, 0);
  152. return ret;
  153. }
  154. static int mt9v022_release(struct soc_camera_device *icd)
  155. {
  156. struct mt9v022 *mt9v022 = container_of(icd, struct mt9v022, icd);
  157. struct soc_camera_link *icl = mt9v022->client->dev.platform_data;
  158. if (icl->power)
  159. icl->power(&mt9v022->client->dev, 0);
  160. return 0;
  161. }
  162. static int mt9v022_start_capture(struct soc_camera_device *icd)
  163. {
  164. struct mt9v022 *mt9v022 = container_of(icd, struct mt9v022, icd);
  165. /* Switch to master "normal" mode */
  166. mt9v022->chip_control &= ~0x10;
  167. if (reg_write(icd, MT9V022_CHIP_CONTROL,
  168. mt9v022->chip_control) < 0)
  169. return -EIO;
  170. return 0;
  171. }
  172. static int mt9v022_stop_capture(struct soc_camera_device *icd)
  173. {
  174. struct mt9v022 *mt9v022 = container_of(icd, struct mt9v022, icd);
  175. /* Switch to snapshot mode */
  176. mt9v022->chip_control |= 0x10;
  177. if (reg_write(icd, MT9V022_CHIP_CONTROL,
  178. mt9v022->chip_control) < 0)
  179. return -EIO;
  180. return 0;
  181. }
  182. static int mt9v022_set_bus_param(struct soc_camera_device *icd,
  183. unsigned long flags)
  184. {
  185. struct mt9v022 *mt9v022 = container_of(icd, struct mt9v022, icd);
  186. struct soc_camera_link *icl = mt9v022->client->dev.platform_data;
  187. unsigned int width_flag = flags & SOCAM_DATAWIDTH_MASK;
  188. int ret;
  189. u16 pixclk = 0;
  190. /* Only one width bit may be set */
  191. if (!is_power_of_2(width_flag))
  192. return -EINVAL;
  193. if (icl->set_bus_param) {
  194. ret = icl->set_bus_param(icl, width_flag);
  195. if (ret)
  196. return ret;
  197. } else {
  198. /*
  199. * Without board specific bus width settings we only support the
  200. * sensors native bus width
  201. */
  202. if (width_flag != SOCAM_DATAWIDTH_10)
  203. return -EINVAL;
  204. }
  205. flags = soc_camera_apply_sensor_flags(icl, flags);
  206. if (flags & SOCAM_PCLK_SAMPLE_RISING)
  207. pixclk |= 0x10;
  208. if (!(flags & SOCAM_HSYNC_ACTIVE_HIGH))
  209. pixclk |= 0x1;
  210. if (!(flags & SOCAM_VSYNC_ACTIVE_HIGH))
  211. pixclk |= 0x2;
  212. ret = reg_write(icd, MT9V022_PIXCLK_FV_LV, pixclk);
  213. if (ret < 0)
  214. return ret;
  215. if (!(flags & SOCAM_MASTER))
  216. mt9v022->chip_control &= ~0x8;
  217. ret = reg_write(icd, MT9V022_CHIP_CONTROL, mt9v022->chip_control);
  218. if (ret < 0)
  219. return ret;
  220. dev_dbg(&icd->dev, "Calculated pixclk 0x%x, chip control 0x%x\n",
  221. pixclk, mt9v022->chip_control);
  222. return 0;
  223. }
  224. static unsigned long mt9v022_query_bus_param(struct soc_camera_device *icd)
  225. {
  226. struct mt9v022 *mt9v022 = container_of(icd, struct mt9v022, icd);
  227. struct soc_camera_link *icl = mt9v022->client->dev.platform_data;
  228. unsigned int width_flag;
  229. if (icl->query_bus_param)
  230. width_flag = icl->query_bus_param(icl) &
  231. SOCAM_DATAWIDTH_MASK;
  232. else
  233. width_flag = SOCAM_DATAWIDTH_10;
  234. return SOCAM_PCLK_SAMPLE_RISING | SOCAM_PCLK_SAMPLE_FALLING |
  235. SOCAM_HSYNC_ACTIVE_HIGH | SOCAM_HSYNC_ACTIVE_LOW |
  236. SOCAM_VSYNC_ACTIVE_HIGH | SOCAM_VSYNC_ACTIVE_LOW |
  237. SOCAM_DATA_ACTIVE_HIGH | SOCAM_MASTER | SOCAM_SLAVE |
  238. width_flag;
  239. }
  240. static int mt9v022_set_crop(struct soc_camera_device *icd,
  241. struct v4l2_rect *rect)
  242. {
  243. int ret;
  244. /* Like in example app. Contradicts the datasheet though */
  245. ret = reg_read(icd, MT9V022_AEC_AGC_ENABLE);
  246. if (ret >= 0) {
  247. if (ret & 1) /* Autoexposure */
  248. ret = reg_write(icd, MT9V022_MAX_TOTAL_SHUTTER_WIDTH,
  249. rect->height + icd->y_skip_top + 43);
  250. else
  251. ret = reg_write(icd, MT9V022_TOTAL_SHUTTER_WIDTH,
  252. rect->height + icd->y_skip_top + 43);
  253. }
  254. /* Setup frame format: defaults apart from width and height */
  255. if (!ret)
  256. ret = reg_write(icd, MT9V022_COLUMN_START, rect->left);
  257. if (!ret)
  258. ret = reg_write(icd, MT9V022_ROW_START, rect->top);
  259. if (!ret)
  260. /* Default 94, Phytec driver says:
  261. * "width + horizontal blank >= 660" */
  262. ret = reg_write(icd, MT9V022_HORIZONTAL_BLANKING,
  263. rect->width > 660 - 43 ? 43 :
  264. 660 - rect->width);
  265. if (!ret)
  266. ret = reg_write(icd, MT9V022_VERTICAL_BLANKING, 45);
  267. if (!ret)
  268. ret = reg_write(icd, MT9V022_WINDOW_WIDTH, rect->width);
  269. if (!ret)
  270. ret = reg_write(icd, MT9V022_WINDOW_HEIGHT,
  271. rect->height + icd->y_skip_top);
  272. if (ret < 0)
  273. return ret;
  274. dev_dbg(&icd->dev, "Frame %ux%u pixel\n", rect->width, rect->height);
  275. return 0;
  276. }
  277. static int mt9v022_set_fmt(struct soc_camera_device *icd,
  278. struct v4l2_format *f)
  279. {
  280. struct mt9v022 *mt9v022 = container_of(icd, struct mt9v022, icd);
  281. struct v4l2_pix_format *pix = &f->fmt.pix;
  282. struct v4l2_rect rect = {
  283. .left = icd->x_current,
  284. .top = icd->y_current,
  285. .width = pix->width,
  286. .height = pix->height,
  287. };
  288. /* The caller provides a supported format, as verified per call to
  289. * icd->try_fmt(), datawidth is from our supported format list */
  290. switch (pix->pixelformat) {
  291. case V4L2_PIX_FMT_GREY:
  292. case V4L2_PIX_FMT_Y16:
  293. if (mt9v022->model != V4L2_IDENT_MT9V022IX7ATM)
  294. return -EINVAL;
  295. break;
  296. case V4L2_PIX_FMT_SBGGR8:
  297. case V4L2_PIX_FMT_SBGGR16:
  298. if (mt9v022->model != V4L2_IDENT_MT9V022IX7ATC)
  299. return -EINVAL;
  300. break;
  301. case 0:
  302. /* No format change, only geometry */
  303. break;
  304. default:
  305. return -EINVAL;
  306. }
  307. /* No support for scaling on this camera, just crop. */
  308. return mt9v022_set_crop(icd, &rect);
  309. }
  310. static int mt9v022_try_fmt(struct soc_camera_device *icd,
  311. struct v4l2_format *f)
  312. {
  313. struct v4l2_pix_format *pix = &f->fmt.pix;
  314. if (pix->height < 32 + icd->y_skip_top)
  315. pix->height = 32 + icd->y_skip_top;
  316. if (pix->height > 480 + icd->y_skip_top)
  317. pix->height = 480 + icd->y_skip_top;
  318. if (pix->width < 48)
  319. pix->width = 48;
  320. if (pix->width > 752)
  321. pix->width = 752;
  322. pix->width &= ~0x03; /* ? */
  323. return 0;
  324. }
  325. static int mt9v022_get_chip_id(struct soc_camera_device *icd,
  326. struct v4l2_dbg_chip_ident *id)
  327. {
  328. struct mt9v022 *mt9v022 = container_of(icd, struct mt9v022, icd);
  329. if (id->match.type != V4L2_CHIP_MATCH_I2C_ADDR)
  330. return -EINVAL;
  331. if (id->match.addr != mt9v022->client->addr)
  332. return -ENODEV;
  333. id->ident = mt9v022->model;
  334. id->revision = 0;
  335. return 0;
  336. }
  337. #ifdef CONFIG_VIDEO_ADV_DEBUG
  338. static int mt9v022_get_register(struct soc_camera_device *icd,
  339. struct v4l2_dbg_register *reg)
  340. {
  341. struct mt9v022 *mt9v022 = container_of(icd, struct mt9v022, icd);
  342. if (reg->match.type != V4L2_CHIP_MATCH_I2C_ADDR || reg->reg > 0xff)
  343. return -EINVAL;
  344. if (reg->match.addr != mt9v022->client->addr)
  345. return -ENODEV;
  346. reg->size = 2;
  347. reg->val = reg_read(icd, reg->reg);
  348. if (reg->val > 0xffff)
  349. return -EIO;
  350. return 0;
  351. }
  352. static int mt9v022_set_register(struct soc_camera_device *icd,
  353. struct v4l2_dbg_register *reg)
  354. {
  355. struct mt9v022 *mt9v022 = container_of(icd, struct mt9v022, icd);
  356. if (reg->match.type != V4L2_CHIP_MATCH_I2C_ADDR || reg->reg > 0xff)
  357. return -EINVAL;
  358. if (reg->match.addr != mt9v022->client->addr)
  359. return -ENODEV;
  360. if (reg_write(icd, reg->reg, reg->val) < 0)
  361. return -EIO;
  362. return 0;
  363. }
  364. #endif
  365. static const struct v4l2_queryctrl mt9v022_controls[] = {
  366. {
  367. .id = V4L2_CID_VFLIP,
  368. .type = V4L2_CTRL_TYPE_BOOLEAN,
  369. .name = "Flip Vertically",
  370. .minimum = 0,
  371. .maximum = 1,
  372. .step = 1,
  373. .default_value = 0,
  374. }, {
  375. .id = V4L2_CID_HFLIP,
  376. .type = V4L2_CTRL_TYPE_BOOLEAN,
  377. .name = "Flip Horizontally",
  378. .minimum = 0,
  379. .maximum = 1,
  380. .step = 1,
  381. .default_value = 0,
  382. }, {
  383. .id = V4L2_CID_GAIN,
  384. .type = V4L2_CTRL_TYPE_INTEGER,
  385. .name = "Analog Gain",
  386. .minimum = 64,
  387. .maximum = 127,
  388. .step = 1,
  389. .default_value = 64,
  390. .flags = V4L2_CTRL_FLAG_SLIDER,
  391. }, {
  392. .id = V4L2_CID_EXPOSURE,
  393. .type = V4L2_CTRL_TYPE_INTEGER,
  394. .name = "Exposure",
  395. .minimum = 1,
  396. .maximum = 255,
  397. .step = 1,
  398. .default_value = 255,
  399. .flags = V4L2_CTRL_FLAG_SLIDER,
  400. }, {
  401. .id = V4L2_CID_AUTOGAIN,
  402. .type = V4L2_CTRL_TYPE_BOOLEAN,
  403. .name = "Automatic Gain",
  404. .minimum = 0,
  405. .maximum = 1,
  406. .step = 1,
  407. .default_value = 1,
  408. }, {
  409. .id = V4L2_CID_EXPOSURE_AUTO,
  410. .type = V4L2_CTRL_TYPE_BOOLEAN,
  411. .name = "Automatic Exposure",
  412. .minimum = 0,
  413. .maximum = 1,
  414. .step = 1,
  415. .default_value = 1,
  416. }
  417. };
  418. static int mt9v022_video_probe(struct soc_camera_device *);
  419. static void mt9v022_video_remove(struct soc_camera_device *);
  420. static int mt9v022_get_control(struct soc_camera_device *, struct v4l2_control *);
  421. static int mt9v022_set_control(struct soc_camera_device *, struct v4l2_control *);
  422. static struct soc_camera_ops mt9v022_ops = {
  423. .owner = THIS_MODULE,
  424. .probe = mt9v022_video_probe,
  425. .remove = mt9v022_video_remove,
  426. .init = mt9v022_init,
  427. .release = mt9v022_release,
  428. .start_capture = mt9v022_start_capture,
  429. .stop_capture = mt9v022_stop_capture,
  430. .set_crop = mt9v022_set_crop,
  431. .set_fmt = mt9v022_set_fmt,
  432. .try_fmt = mt9v022_try_fmt,
  433. .set_bus_param = mt9v022_set_bus_param,
  434. .query_bus_param = mt9v022_query_bus_param,
  435. .controls = mt9v022_controls,
  436. .num_controls = ARRAY_SIZE(mt9v022_controls),
  437. .get_control = mt9v022_get_control,
  438. .set_control = mt9v022_set_control,
  439. .get_chip_id = mt9v022_get_chip_id,
  440. #ifdef CONFIG_VIDEO_ADV_DEBUG
  441. .get_register = mt9v022_get_register,
  442. .set_register = mt9v022_set_register,
  443. #endif
  444. };
  445. static int mt9v022_get_control(struct soc_camera_device *icd,
  446. struct v4l2_control *ctrl)
  447. {
  448. int data;
  449. switch (ctrl->id) {
  450. case V4L2_CID_VFLIP:
  451. data = reg_read(icd, MT9V022_READ_MODE);
  452. if (data < 0)
  453. return -EIO;
  454. ctrl->value = !!(data & 0x10);
  455. break;
  456. case V4L2_CID_HFLIP:
  457. data = reg_read(icd, MT9V022_READ_MODE);
  458. if (data < 0)
  459. return -EIO;
  460. ctrl->value = !!(data & 0x20);
  461. break;
  462. case V4L2_CID_EXPOSURE_AUTO:
  463. data = reg_read(icd, MT9V022_AEC_AGC_ENABLE);
  464. if (data < 0)
  465. return -EIO;
  466. ctrl->value = !!(data & 0x1);
  467. break;
  468. case V4L2_CID_AUTOGAIN:
  469. data = reg_read(icd, MT9V022_AEC_AGC_ENABLE);
  470. if (data < 0)
  471. return -EIO;
  472. ctrl->value = !!(data & 0x2);
  473. break;
  474. }
  475. return 0;
  476. }
  477. static int mt9v022_set_control(struct soc_camera_device *icd,
  478. struct v4l2_control *ctrl)
  479. {
  480. int data;
  481. const struct v4l2_queryctrl *qctrl;
  482. qctrl = soc_camera_find_qctrl(&mt9v022_ops, ctrl->id);
  483. if (!qctrl)
  484. return -EINVAL;
  485. switch (ctrl->id) {
  486. case V4L2_CID_VFLIP:
  487. if (ctrl->value)
  488. data = reg_set(icd, MT9V022_READ_MODE, 0x10);
  489. else
  490. data = reg_clear(icd, MT9V022_READ_MODE, 0x10);
  491. if (data < 0)
  492. return -EIO;
  493. break;
  494. case V4L2_CID_HFLIP:
  495. if (ctrl->value)
  496. data = reg_set(icd, MT9V022_READ_MODE, 0x20);
  497. else
  498. data = reg_clear(icd, MT9V022_READ_MODE, 0x20);
  499. if (data < 0)
  500. return -EIO;
  501. break;
  502. case V4L2_CID_GAIN:
  503. /* mt9v022 has minimum == default */
  504. if (ctrl->value > qctrl->maximum || ctrl->value < qctrl->minimum)
  505. return -EINVAL;
  506. else {
  507. unsigned long range = qctrl->maximum - qctrl->minimum;
  508. /* Datasheet says 16 to 64. autogain only works properly
  509. * after setting gain to maximum 14. Larger values
  510. * produce "white fly" noise effect. On the whole,
  511. * manually setting analog gain does no good. */
  512. unsigned long gain = ((ctrl->value - qctrl->minimum) *
  513. 10 + range / 2) / range + 4;
  514. if (gain >= 32)
  515. gain &= ~1;
  516. /* The user wants to set gain manually, hope, she
  517. * knows, what she's doing... Switch AGC off. */
  518. if (reg_clear(icd, MT9V022_AEC_AGC_ENABLE, 0x2) < 0)
  519. return -EIO;
  520. dev_info(&icd->dev, "Setting gain from %d to %lu\n",
  521. reg_read(icd, MT9V022_ANALOG_GAIN), gain);
  522. if (reg_write(icd, MT9V022_ANALOG_GAIN, gain) < 0)
  523. return -EIO;
  524. icd->gain = ctrl->value;
  525. }
  526. break;
  527. case V4L2_CID_EXPOSURE:
  528. /* mt9v022 has maximum == default */
  529. if (ctrl->value > qctrl->maximum || ctrl->value < qctrl->minimum)
  530. return -EINVAL;
  531. else {
  532. unsigned long range = qctrl->maximum - qctrl->minimum;
  533. unsigned long shutter = ((ctrl->value - qctrl->minimum) *
  534. 479 + range / 2) / range + 1;
  535. /* The user wants to set shutter width manually, hope,
  536. * she knows, what she's doing... Switch AEC off. */
  537. if (reg_clear(icd, MT9V022_AEC_AGC_ENABLE, 0x1) < 0)
  538. return -EIO;
  539. dev_dbg(&icd->dev, "Shutter width from %d to %lu\n",
  540. reg_read(icd, MT9V022_TOTAL_SHUTTER_WIDTH),
  541. shutter);
  542. if (reg_write(icd, MT9V022_TOTAL_SHUTTER_WIDTH,
  543. shutter) < 0)
  544. return -EIO;
  545. icd->exposure = ctrl->value;
  546. }
  547. break;
  548. case V4L2_CID_AUTOGAIN:
  549. if (ctrl->value)
  550. data = reg_set(icd, MT9V022_AEC_AGC_ENABLE, 0x2);
  551. else
  552. data = reg_clear(icd, MT9V022_AEC_AGC_ENABLE, 0x2);
  553. if (data < 0)
  554. return -EIO;
  555. break;
  556. case V4L2_CID_EXPOSURE_AUTO:
  557. if (ctrl->value)
  558. data = reg_set(icd, MT9V022_AEC_AGC_ENABLE, 0x1);
  559. else
  560. data = reg_clear(icd, MT9V022_AEC_AGC_ENABLE, 0x1);
  561. if (data < 0)
  562. return -EIO;
  563. break;
  564. }
  565. return 0;
  566. }
  567. /* Interface active, can use i2c. If it fails, it can indeed mean, that
  568. * this wasn't our capture interface, so, we wait for the right one */
  569. static int mt9v022_video_probe(struct soc_camera_device *icd)
  570. {
  571. struct mt9v022 *mt9v022 = container_of(icd, struct mt9v022, icd);
  572. struct soc_camera_link *icl = mt9v022->client->dev.platform_data;
  573. s32 data;
  574. int ret;
  575. unsigned long flags;
  576. if (!icd->dev.parent ||
  577. to_soc_camera_host(icd->dev.parent)->nr != icd->iface)
  578. return -ENODEV;
  579. /* Read out the chip version register */
  580. data = reg_read(icd, MT9V022_CHIP_VERSION);
  581. /* must be 0x1311 or 0x1313 */
  582. if (data != 0x1311 && data != 0x1313) {
  583. ret = -ENODEV;
  584. dev_info(&icd->dev, "No MT9V022 detected, ID register 0x%x\n",
  585. data);
  586. goto ei2c;
  587. }
  588. /* Soft reset */
  589. ret = reg_write(icd, MT9V022_RESET, 1);
  590. if (ret < 0)
  591. goto ei2c;
  592. /* 15 clock cycles */
  593. udelay(200);
  594. if (reg_read(icd, MT9V022_RESET)) {
  595. dev_err(&icd->dev, "Resetting MT9V022 failed!\n");
  596. goto ei2c;
  597. }
  598. /* Set monochrome or colour sensor type */
  599. if (sensor_type && (!strcmp("colour", sensor_type) ||
  600. !strcmp("color", sensor_type))) {
  601. ret = reg_write(icd, MT9V022_PIXEL_OPERATION_MODE, 4 | 0x11);
  602. mt9v022->model = V4L2_IDENT_MT9V022IX7ATC;
  603. icd->formats = mt9v022_colour_formats;
  604. } else {
  605. ret = reg_write(icd, MT9V022_PIXEL_OPERATION_MODE, 0x11);
  606. mt9v022->model = V4L2_IDENT_MT9V022IX7ATM;
  607. icd->formats = mt9v022_monochrome_formats;
  608. }
  609. if (ret < 0)
  610. goto eisis;
  611. icd->num_formats = 0;
  612. /*
  613. * This is a 10bit sensor, so by default we only allow 10bit.
  614. * The platform may support different bus widths due to
  615. * different routing of the data lines.
  616. */
  617. if (icl->query_bus_param)
  618. flags = icl->query_bus_param(icl);
  619. else
  620. flags = SOCAM_DATAWIDTH_10;
  621. if (flags & SOCAM_DATAWIDTH_10)
  622. icd->num_formats++;
  623. else
  624. icd->formats++;
  625. if (flags & SOCAM_DATAWIDTH_8)
  626. icd->num_formats++;
  627. ret = soc_camera_video_start(icd);
  628. if (ret < 0)
  629. goto eisis;
  630. dev_info(&icd->dev, "Detected a MT9V022 chip ID %x, %s sensor\n",
  631. data, mt9v022->model == V4L2_IDENT_MT9V022IX7ATM ?
  632. "monochrome" : "colour");
  633. return 0;
  634. eisis:
  635. ei2c:
  636. return ret;
  637. }
  638. static void mt9v022_video_remove(struct soc_camera_device *icd)
  639. {
  640. struct mt9v022 *mt9v022 = container_of(icd, struct mt9v022, icd);
  641. dev_dbg(&icd->dev, "Video %x removed: %p, %p\n", mt9v022->client->addr,
  642. icd->dev.parent, icd->vdev);
  643. soc_camera_video_stop(icd);
  644. }
  645. static int mt9v022_probe(struct i2c_client *client,
  646. const struct i2c_device_id *did)
  647. {
  648. struct mt9v022 *mt9v022;
  649. struct soc_camera_device *icd;
  650. struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
  651. struct soc_camera_link *icl = client->dev.platform_data;
  652. int ret;
  653. if (!icl) {
  654. dev_err(&client->dev, "MT9V022 driver needs platform data\n");
  655. return -EINVAL;
  656. }
  657. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WORD_DATA)) {
  658. dev_warn(&adapter->dev,
  659. "I2C-Adapter doesn't support I2C_FUNC_SMBUS_WORD\n");
  660. return -EIO;
  661. }
  662. mt9v022 = kzalloc(sizeof(struct mt9v022), GFP_KERNEL);
  663. if (!mt9v022)
  664. return -ENOMEM;
  665. mt9v022->chip_control = MT9V022_CHIP_CONTROL_DEFAULT;
  666. mt9v022->client = client;
  667. i2c_set_clientdata(client, mt9v022);
  668. icd = &mt9v022->icd;
  669. icd->ops = &mt9v022_ops;
  670. icd->control = &client->dev;
  671. icd->x_min = 1;
  672. icd->y_min = 4;
  673. icd->x_current = 1;
  674. icd->y_current = 4;
  675. icd->width_min = 48;
  676. icd->width_max = 752;
  677. icd->height_min = 32;
  678. icd->height_max = 480;
  679. icd->y_skip_top = 1;
  680. icd->iface = icl->bus_id;
  681. ret = soc_camera_device_register(icd);
  682. if (ret)
  683. goto eisdr;
  684. return 0;
  685. eisdr:
  686. kfree(mt9v022);
  687. return ret;
  688. }
  689. static int mt9v022_remove(struct i2c_client *client)
  690. {
  691. struct mt9v022 *mt9v022 = i2c_get_clientdata(client);
  692. soc_camera_device_unregister(&mt9v022->icd);
  693. kfree(mt9v022);
  694. return 0;
  695. }
  696. static const struct i2c_device_id mt9v022_id[] = {
  697. { "mt9v022", 0 },
  698. { }
  699. };
  700. MODULE_DEVICE_TABLE(i2c, mt9v022_id);
  701. static struct i2c_driver mt9v022_i2c_driver = {
  702. .driver = {
  703. .name = "mt9v022",
  704. },
  705. .probe = mt9v022_probe,
  706. .remove = mt9v022_remove,
  707. .id_table = mt9v022_id,
  708. };
  709. static int __init mt9v022_mod_init(void)
  710. {
  711. return i2c_add_driver(&mt9v022_i2c_driver);
  712. }
  713. static void __exit mt9v022_mod_exit(void)
  714. {
  715. i2c_del_driver(&mt9v022_i2c_driver);
  716. }
  717. module_init(mt9v022_mod_init);
  718. module_exit(mt9v022_mod_exit);
  719. MODULE_DESCRIPTION("Micron MT9V022 Camera driver");
  720. MODULE_AUTHOR("Guennadi Liakhovetski <kernel@pengutronix.de>");
  721. MODULE_LICENSE("GPL");