mt9v022.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876
  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. struct soc_camera_link *icl = mt9v022->client->dev.platform_data;
  242. unsigned int width_flag = flags & SOCAM_DATAWIDTH_MASK;
  243. int ret;
  244. u16 pixclk = 0;
  245. /* Only one width bit may be set */
  246. if (!is_power_of_2(width_flag))
  247. return -EINVAL;
  248. if ((mt9v022->datawidth != 10 && (width_flag == SOCAM_DATAWIDTH_10)) ||
  249. (mt9v022->datawidth != 9 && (width_flag == SOCAM_DATAWIDTH_9)) ||
  250. (mt9v022->datawidth != 8 && (width_flag == SOCAM_DATAWIDTH_8))) {
  251. /* Well, we actually only can do 10 or 8 bits... */
  252. if (width_flag == SOCAM_DATAWIDTH_9)
  253. return -EINVAL;
  254. ret = bus_switch_act(mt9v022,
  255. width_flag == SOCAM_DATAWIDTH_8);
  256. if (ret < 0)
  257. return ret;
  258. mt9v022->datawidth = width_flag == SOCAM_DATAWIDTH_8 ? 8 : 10;
  259. }
  260. flags = soc_camera_apply_sensor_flags(icl, flags);
  261. if (flags & SOCAM_PCLK_SAMPLE_RISING)
  262. pixclk |= 0x10;
  263. if (!(flags & SOCAM_HSYNC_ACTIVE_HIGH))
  264. pixclk |= 0x1;
  265. if (!(flags & SOCAM_VSYNC_ACTIVE_HIGH))
  266. pixclk |= 0x2;
  267. ret = reg_write(icd, MT9V022_PIXCLK_FV_LV, pixclk);
  268. if (ret < 0)
  269. return ret;
  270. if (!(flags & SOCAM_MASTER))
  271. mt9v022->chip_control &= ~0x8;
  272. ret = reg_write(icd, MT9V022_CHIP_CONTROL, mt9v022->chip_control);
  273. if (ret < 0)
  274. return ret;
  275. dev_dbg(&icd->dev, "Calculated pixclk 0x%x, chip control 0x%x\n",
  276. pixclk, mt9v022->chip_control);
  277. return 0;
  278. }
  279. static unsigned long mt9v022_query_bus_param(struct soc_camera_device *icd)
  280. {
  281. struct mt9v022 *mt9v022 = container_of(icd, struct mt9v022, icd);
  282. unsigned int width_flag = SOCAM_DATAWIDTH_10;
  283. if (bus_switch_possible(mt9v022))
  284. width_flag |= SOCAM_DATAWIDTH_8;
  285. return SOCAM_PCLK_SAMPLE_RISING | SOCAM_PCLK_SAMPLE_FALLING |
  286. SOCAM_HSYNC_ACTIVE_HIGH | SOCAM_HSYNC_ACTIVE_LOW |
  287. SOCAM_VSYNC_ACTIVE_HIGH | SOCAM_VSYNC_ACTIVE_LOW |
  288. SOCAM_MASTER | SOCAM_SLAVE |
  289. width_flag;
  290. }
  291. static int mt9v022_set_fmt(struct soc_camera_device *icd,
  292. __u32 pixfmt, struct v4l2_rect *rect)
  293. {
  294. struct mt9v022 *mt9v022 = container_of(icd, struct mt9v022, icd);
  295. int ret;
  296. /* The caller provides a supported format, as verified per call to
  297. * icd->try_fmt(), datawidth is from our supported format list */
  298. switch (pixfmt) {
  299. case V4L2_PIX_FMT_GREY:
  300. case V4L2_PIX_FMT_Y16:
  301. if (mt9v022->model != V4L2_IDENT_MT9V022IX7ATM)
  302. return -EINVAL;
  303. break;
  304. case V4L2_PIX_FMT_SBGGR8:
  305. case V4L2_PIX_FMT_SBGGR16:
  306. if (mt9v022->model != V4L2_IDENT_MT9V022IX7ATC)
  307. return -EINVAL;
  308. break;
  309. case 0:
  310. /* No format change, only geometry */
  311. break;
  312. default:
  313. return -EINVAL;
  314. }
  315. /* Like in example app. Contradicts the datasheet though */
  316. ret = reg_read(icd, MT9V022_AEC_AGC_ENABLE);
  317. if (ret >= 0) {
  318. if (ret & 1) /* Autoexposure */
  319. ret = reg_write(icd, MT9V022_MAX_TOTAL_SHUTTER_WIDTH,
  320. rect->height + icd->y_skip_top + 43);
  321. else
  322. ret = reg_write(icd, MT9V022_TOTAL_SHUTTER_WIDTH,
  323. rect->height + icd->y_skip_top + 43);
  324. }
  325. /* Setup frame format: defaults apart from width and height */
  326. if (!ret)
  327. ret = reg_write(icd, MT9V022_COLUMN_START, rect->left);
  328. if (!ret)
  329. ret = reg_write(icd, MT9V022_ROW_START, rect->top);
  330. if (!ret)
  331. /* Default 94, Phytec driver says:
  332. * "width + horizontal blank >= 660" */
  333. ret = reg_write(icd, MT9V022_HORIZONTAL_BLANKING,
  334. rect->width > 660 - 43 ? 43 :
  335. 660 - rect->width);
  336. if (!ret)
  337. ret = reg_write(icd, MT9V022_VERTICAL_BLANKING, 45);
  338. if (!ret)
  339. ret = reg_write(icd, MT9V022_WINDOW_WIDTH, rect->width);
  340. if (!ret)
  341. ret = reg_write(icd, MT9V022_WINDOW_HEIGHT,
  342. rect->height + icd->y_skip_top);
  343. if (ret < 0)
  344. return ret;
  345. dev_dbg(&icd->dev, "Frame %ux%u pixel\n", rect->width, rect->height);
  346. return 0;
  347. }
  348. static int mt9v022_try_fmt(struct soc_camera_device *icd,
  349. struct v4l2_format *f)
  350. {
  351. struct v4l2_pix_format *pix = &f->fmt.pix;
  352. if (pix->height < 32 + icd->y_skip_top)
  353. pix->height = 32 + icd->y_skip_top;
  354. if (pix->height > 480 + icd->y_skip_top)
  355. pix->height = 480 + icd->y_skip_top;
  356. if (pix->width < 48)
  357. pix->width = 48;
  358. if (pix->width > 752)
  359. pix->width = 752;
  360. pix->width &= ~0x03; /* ? */
  361. return 0;
  362. }
  363. static int mt9v022_get_chip_id(struct soc_camera_device *icd,
  364. struct v4l2_chip_ident *id)
  365. {
  366. struct mt9v022 *mt9v022 = container_of(icd, struct mt9v022, icd);
  367. if (id->match_type != V4L2_CHIP_MATCH_I2C_ADDR)
  368. return -EINVAL;
  369. if (id->match_chip != mt9v022->client->addr)
  370. return -ENODEV;
  371. id->ident = mt9v022->model;
  372. id->revision = 0;
  373. return 0;
  374. }
  375. #ifdef CONFIG_VIDEO_ADV_DEBUG
  376. static int mt9v022_get_register(struct soc_camera_device *icd,
  377. struct v4l2_register *reg)
  378. {
  379. struct mt9v022 *mt9v022 = container_of(icd, struct mt9v022, icd);
  380. if (reg->match_type != V4L2_CHIP_MATCH_I2C_ADDR || reg->reg > 0xff)
  381. return -EINVAL;
  382. if (reg->match_chip != mt9v022->client->addr)
  383. return -ENODEV;
  384. reg->val = reg_read(icd, reg->reg);
  385. if (reg->val > 0xffff)
  386. return -EIO;
  387. return 0;
  388. }
  389. static int mt9v022_set_register(struct soc_camera_device *icd,
  390. struct v4l2_register *reg)
  391. {
  392. struct mt9v022 *mt9v022 = container_of(icd, struct mt9v022, icd);
  393. if (reg->match_type != V4L2_CHIP_MATCH_I2C_ADDR || reg->reg > 0xff)
  394. return -EINVAL;
  395. if (reg->match_chip != mt9v022->client->addr)
  396. return -ENODEV;
  397. if (reg_write(icd, reg->reg, reg->val) < 0)
  398. return -EIO;
  399. return 0;
  400. }
  401. #endif
  402. static const struct v4l2_queryctrl mt9v022_controls[] = {
  403. {
  404. .id = V4L2_CID_VFLIP,
  405. .type = V4L2_CTRL_TYPE_BOOLEAN,
  406. .name = "Flip Vertically",
  407. .minimum = 0,
  408. .maximum = 1,
  409. .step = 1,
  410. .default_value = 0,
  411. }, {
  412. .id = V4L2_CID_HFLIP,
  413. .type = V4L2_CTRL_TYPE_BOOLEAN,
  414. .name = "Flip Horizontally",
  415. .minimum = 0,
  416. .maximum = 1,
  417. .step = 1,
  418. .default_value = 0,
  419. }, {
  420. .id = V4L2_CID_GAIN,
  421. .type = V4L2_CTRL_TYPE_INTEGER,
  422. .name = "Analog Gain",
  423. .minimum = 64,
  424. .maximum = 127,
  425. .step = 1,
  426. .default_value = 64,
  427. .flags = V4L2_CTRL_FLAG_SLIDER,
  428. }, {
  429. .id = V4L2_CID_EXPOSURE,
  430. .type = V4L2_CTRL_TYPE_INTEGER,
  431. .name = "Exposure",
  432. .minimum = 1,
  433. .maximum = 255,
  434. .step = 1,
  435. .default_value = 255,
  436. .flags = V4L2_CTRL_FLAG_SLIDER,
  437. }, {
  438. .id = V4L2_CID_AUTOGAIN,
  439. .type = V4L2_CTRL_TYPE_BOOLEAN,
  440. .name = "Automatic Gain",
  441. .minimum = 0,
  442. .maximum = 1,
  443. .step = 1,
  444. .default_value = 1,
  445. }, {
  446. .id = V4L2_CID_EXPOSURE_AUTO,
  447. .type = V4L2_CTRL_TYPE_BOOLEAN,
  448. .name = "Automatic Exposure",
  449. .minimum = 0,
  450. .maximum = 1,
  451. .step = 1,
  452. .default_value = 1,
  453. }
  454. };
  455. static int mt9v022_video_probe(struct soc_camera_device *);
  456. static void mt9v022_video_remove(struct soc_camera_device *);
  457. static int mt9v022_get_control(struct soc_camera_device *, struct v4l2_control *);
  458. static int mt9v022_set_control(struct soc_camera_device *, struct v4l2_control *);
  459. static struct soc_camera_ops mt9v022_ops = {
  460. .owner = THIS_MODULE,
  461. .probe = mt9v022_video_probe,
  462. .remove = mt9v022_video_remove,
  463. .init = mt9v022_init,
  464. .release = mt9v022_release,
  465. .start_capture = mt9v022_start_capture,
  466. .stop_capture = mt9v022_stop_capture,
  467. .set_fmt = mt9v022_set_fmt,
  468. .try_fmt = mt9v022_try_fmt,
  469. .set_bus_param = mt9v022_set_bus_param,
  470. .query_bus_param = mt9v022_query_bus_param,
  471. .controls = mt9v022_controls,
  472. .num_controls = ARRAY_SIZE(mt9v022_controls),
  473. .get_control = mt9v022_get_control,
  474. .set_control = mt9v022_set_control,
  475. .get_chip_id = mt9v022_get_chip_id,
  476. #ifdef CONFIG_VIDEO_ADV_DEBUG
  477. .get_register = mt9v022_get_register,
  478. .set_register = mt9v022_set_register,
  479. #endif
  480. };
  481. static int mt9v022_get_control(struct soc_camera_device *icd,
  482. struct v4l2_control *ctrl)
  483. {
  484. int data;
  485. switch (ctrl->id) {
  486. case V4L2_CID_VFLIP:
  487. data = reg_read(icd, MT9V022_READ_MODE);
  488. if (data < 0)
  489. return -EIO;
  490. ctrl->value = !!(data & 0x10);
  491. break;
  492. case V4L2_CID_HFLIP:
  493. data = reg_read(icd, MT9V022_READ_MODE);
  494. if (data < 0)
  495. return -EIO;
  496. ctrl->value = !!(data & 0x20);
  497. break;
  498. case V4L2_CID_EXPOSURE_AUTO:
  499. data = reg_read(icd, MT9V022_AEC_AGC_ENABLE);
  500. if (data < 0)
  501. return -EIO;
  502. ctrl->value = !!(data & 0x1);
  503. break;
  504. case V4L2_CID_AUTOGAIN:
  505. data = reg_read(icd, MT9V022_AEC_AGC_ENABLE);
  506. if (data < 0)
  507. return -EIO;
  508. ctrl->value = !!(data & 0x2);
  509. break;
  510. }
  511. return 0;
  512. }
  513. static int mt9v022_set_control(struct soc_camera_device *icd,
  514. struct v4l2_control *ctrl)
  515. {
  516. int data;
  517. const struct v4l2_queryctrl *qctrl;
  518. qctrl = soc_camera_find_qctrl(&mt9v022_ops, ctrl->id);
  519. if (!qctrl)
  520. return -EINVAL;
  521. switch (ctrl->id) {
  522. case V4L2_CID_VFLIP:
  523. if (ctrl->value)
  524. data = reg_set(icd, MT9V022_READ_MODE, 0x10);
  525. else
  526. data = reg_clear(icd, MT9V022_READ_MODE, 0x10);
  527. if (data < 0)
  528. return -EIO;
  529. break;
  530. case V4L2_CID_HFLIP:
  531. if (ctrl->value)
  532. data = reg_set(icd, MT9V022_READ_MODE, 0x20);
  533. else
  534. data = reg_clear(icd, MT9V022_READ_MODE, 0x20);
  535. if (data < 0)
  536. return -EIO;
  537. break;
  538. case V4L2_CID_GAIN:
  539. /* mt9v022 has minimum == default */
  540. if (ctrl->value > qctrl->maximum || ctrl->value < qctrl->minimum)
  541. return -EINVAL;
  542. else {
  543. unsigned long range = qctrl->maximum - qctrl->minimum;
  544. /* Datasheet says 16 to 64. autogain only works properly
  545. * after setting gain to maximum 14. Larger values
  546. * produce "white fly" noise effect. On the whole,
  547. * manually setting analog gain does no good. */
  548. unsigned long gain = ((ctrl->value - qctrl->minimum) *
  549. 10 + range / 2) / range + 4;
  550. if (gain >= 32)
  551. gain &= ~1;
  552. /* The user wants to set gain manually, hope, she
  553. * knows, what she's doing... Switch AGC off. */
  554. if (reg_clear(icd, MT9V022_AEC_AGC_ENABLE, 0x2) < 0)
  555. return -EIO;
  556. dev_info(&icd->dev, "Setting gain from %d to %lu\n",
  557. reg_read(icd, MT9V022_ANALOG_GAIN), gain);
  558. if (reg_write(icd, MT9V022_ANALOG_GAIN, gain) < 0)
  559. return -EIO;
  560. icd->gain = ctrl->value;
  561. }
  562. break;
  563. case V4L2_CID_EXPOSURE:
  564. /* mt9v022 has maximum == default */
  565. if (ctrl->value > qctrl->maximum || ctrl->value < qctrl->minimum)
  566. return -EINVAL;
  567. else {
  568. unsigned long range = qctrl->maximum - qctrl->minimum;
  569. unsigned long shutter = ((ctrl->value - qctrl->minimum) *
  570. 479 + range / 2) / range + 1;
  571. /* The user wants to set shutter width manually, hope,
  572. * she knows, what she's doing... Switch AEC off. */
  573. if (reg_clear(icd, MT9V022_AEC_AGC_ENABLE, 0x1) < 0)
  574. return -EIO;
  575. dev_dbg(&icd->dev, "Shutter width from %d to %lu\n",
  576. reg_read(icd, MT9V022_TOTAL_SHUTTER_WIDTH),
  577. shutter);
  578. if (reg_write(icd, MT9V022_TOTAL_SHUTTER_WIDTH,
  579. shutter) < 0)
  580. return -EIO;
  581. icd->exposure = ctrl->value;
  582. }
  583. break;
  584. case V4L2_CID_AUTOGAIN:
  585. if (ctrl->value)
  586. data = reg_set(icd, MT9V022_AEC_AGC_ENABLE, 0x2);
  587. else
  588. data = reg_clear(icd, MT9V022_AEC_AGC_ENABLE, 0x2);
  589. if (data < 0)
  590. return -EIO;
  591. break;
  592. case V4L2_CID_EXPOSURE_AUTO:
  593. if (ctrl->value)
  594. data = reg_set(icd, MT9V022_AEC_AGC_ENABLE, 0x1);
  595. else
  596. data = reg_clear(icd, MT9V022_AEC_AGC_ENABLE, 0x1);
  597. if (data < 0)
  598. return -EIO;
  599. break;
  600. }
  601. return 0;
  602. }
  603. /* Interface active, can use i2c. If it fails, it can indeed mean, that
  604. * this wasn't our capture interface, so, we wait for the right one */
  605. static int mt9v022_video_probe(struct soc_camera_device *icd)
  606. {
  607. struct mt9v022 *mt9v022 = container_of(icd, struct mt9v022, icd);
  608. struct soc_camera_link *icl = mt9v022->client->dev.platform_data;
  609. s32 data;
  610. int ret;
  611. if (!icd->dev.parent ||
  612. to_soc_camera_host(icd->dev.parent)->nr != icd->iface)
  613. return -ENODEV;
  614. /* Read out the chip version register */
  615. data = reg_read(icd, MT9V022_CHIP_VERSION);
  616. /* must be 0x1311 or 0x1313 */
  617. if (data != 0x1311 && data != 0x1313) {
  618. ret = -ENODEV;
  619. dev_info(&icd->dev, "No MT9V022 detected, ID register 0x%x\n",
  620. data);
  621. goto ei2c;
  622. }
  623. /* Soft reset */
  624. ret = reg_write(icd, MT9V022_RESET, 1);
  625. if (ret < 0)
  626. goto ei2c;
  627. /* 15 clock cycles */
  628. udelay(200);
  629. if (reg_read(icd, MT9V022_RESET)) {
  630. dev_err(&icd->dev, "Resetting MT9V022 failed!\n");
  631. goto ei2c;
  632. }
  633. /* Set monochrome or colour sensor type */
  634. if (sensor_type && (!strcmp("colour", sensor_type) ||
  635. !strcmp("color", sensor_type))) {
  636. ret = reg_write(icd, MT9V022_PIXEL_OPERATION_MODE, 4 | 0x11);
  637. mt9v022->model = V4L2_IDENT_MT9V022IX7ATC;
  638. icd->formats = mt9v022_colour_formats;
  639. if (gpio_is_valid(icl->gpio))
  640. icd->num_formats = ARRAY_SIZE(mt9v022_colour_formats);
  641. else
  642. icd->num_formats = 1;
  643. } else {
  644. ret = reg_write(icd, MT9V022_PIXEL_OPERATION_MODE, 0x11);
  645. mt9v022->model = V4L2_IDENT_MT9V022IX7ATM;
  646. icd->formats = mt9v022_monochrome_formats;
  647. if (gpio_is_valid(icl->gpio))
  648. icd->num_formats = ARRAY_SIZE(mt9v022_monochrome_formats);
  649. else
  650. icd->num_formats = 1;
  651. }
  652. if (!ret)
  653. ret = soc_camera_video_start(icd);
  654. if (ret < 0)
  655. goto eisis;
  656. dev_info(&icd->dev, "Detected a MT9V022 chip ID %x, %s sensor\n",
  657. data, mt9v022->model == V4L2_IDENT_MT9V022IX7ATM ?
  658. "monochrome" : "colour");
  659. return 0;
  660. eisis:
  661. ei2c:
  662. return ret;
  663. }
  664. static void mt9v022_video_remove(struct soc_camera_device *icd)
  665. {
  666. struct mt9v022 *mt9v022 = container_of(icd, struct mt9v022, icd);
  667. dev_dbg(&icd->dev, "Video %x removed: %p, %p\n", mt9v022->client->addr,
  668. icd->dev.parent, icd->vdev);
  669. soc_camera_video_stop(icd);
  670. }
  671. static int mt9v022_probe(struct i2c_client *client,
  672. const struct i2c_device_id *did)
  673. {
  674. struct mt9v022 *mt9v022;
  675. struct soc_camera_device *icd;
  676. struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
  677. struct soc_camera_link *icl = client->dev.platform_data;
  678. int ret;
  679. if (!icl) {
  680. dev_err(&client->dev, "MT9V022 driver needs platform data\n");
  681. return -EINVAL;
  682. }
  683. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WORD_DATA)) {
  684. dev_warn(&adapter->dev,
  685. "I2C-Adapter doesn't support I2C_FUNC_SMBUS_WORD\n");
  686. return -EIO;
  687. }
  688. mt9v022 = kzalloc(sizeof(struct mt9v022), GFP_KERNEL);
  689. if (!mt9v022)
  690. return -ENOMEM;
  691. mt9v022->chip_control = MT9V022_CHIP_CONTROL_DEFAULT;
  692. mt9v022->client = client;
  693. i2c_set_clientdata(client, mt9v022);
  694. icd = &mt9v022->icd;
  695. icd->ops = &mt9v022_ops;
  696. icd->control = &client->dev;
  697. icd->x_min = 1;
  698. icd->y_min = 4;
  699. icd->x_current = 1;
  700. icd->y_current = 4;
  701. icd->width_min = 48;
  702. icd->width_max = 752;
  703. icd->height_min = 32;
  704. icd->height_max = 480;
  705. icd->y_skip_top = 1;
  706. icd->iface = icl->bus_id;
  707. /* Default datawidth - this is the only width this camera (normally)
  708. * supports. It is only with extra logic that it can support
  709. * other widths. Therefore it seems to be a sensible default. */
  710. mt9v022->datawidth = 10;
  711. ret = bus_switch_request(mt9v022, icl);
  712. if (ret)
  713. goto eswinit;
  714. ret = soc_camera_device_register(icd);
  715. if (ret)
  716. goto eisdr;
  717. return 0;
  718. eisdr:
  719. bus_switch_release(mt9v022);
  720. eswinit:
  721. kfree(mt9v022);
  722. return ret;
  723. }
  724. static int mt9v022_remove(struct i2c_client *client)
  725. {
  726. struct mt9v022 *mt9v022 = i2c_get_clientdata(client);
  727. soc_camera_device_unregister(&mt9v022->icd);
  728. bus_switch_release(mt9v022);
  729. kfree(mt9v022);
  730. return 0;
  731. }
  732. static const struct i2c_device_id mt9v022_id[] = {
  733. { "mt9v022", 0 },
  734. { }
  735. };
  736. MODULE_DEVICE_TABLE(i2c, mt9v022_id);
  737. static struct i2c_driver mt9v022_i2c_driver = {
  738. .driver = {
  739. .name = "mt9v022",
  740. },
  741. .probe = mt9v022_probe,
  742. .remove = mt9v022_remove,
  743. .id_table = mt9v022_id,
  744. };
  745. static int __init mt9v022_mod_init(void)
  746. {
  747. return i2c_add_driver(&mt9v022_i2c_driver);
  748. }
  749. static void __exit mt9v022_mod_exit(void)
  750. {
  751. i2c_del_driver(&mt9v022_i2c_driver);
  752. }
  753. module_init(mt9v022_mod_init);
  754. module_exit(mt9v022_mod_exit);
  755. MODULE_DESCRIPTION("Micron MT9V022 Camera driver");
  756. MODULE_AUTHOR("Guennadi Liakhovetski <kernel@pengutronix.de>");
  757. MODULE_LICENSE("GPL");