mt9v022.c 22 KB

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