mt9v022.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877
  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_dbg_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.addr != 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_dbg_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.addr != mt9v022->client->addr)
  383. return -ENODEV;
  384. reg->size = 2;
  385. reg->val = reg_read(icd, reg->reg);
  386. if (reg->val > 0xffff)
  387. return -EIO;
  388. return 0;
  389. }
  390. static int mt9v022_set_register(struct soc_camera_device *icd,
  391. struct v4l2_dbg_register *reg)
  392. {
  393. struct mt9v022 *mt9v022 = container_of(icd, struct mt9v022, icd);
  394. if (reg->match.type != V4L2_CHIP_MATCH_I2C_ADDR || reg->reg > 0xff)
  395. return -EINVAL;
  396. if (reg->match.addr != mt9v022->client->addr)
  397. return -ENODEV;
  398. if (reg_write(icd, reg->reg, reg->val) < 0)
  399. return -EIO;
  400. return 0;
  401. }
  402. #endif
  403. static const struct v4l2_queryctrl mt9v022_controls[] = {
  404. {
  405. .id = V4L2_CID_VFLIP,
  406. .type = V4L2_CTRL_TYPE_BOOLEAN,
  407. .name = "Flip Vertically",
  408. .minimum = 0,
  409. .maximum = 1,
  410. .step = 1,
  411. .default_value = 0,
  412. }, {
  413. .id = V4L2_CID_HFLIP,
  414. .type = V4L2_CTRL_TYPE_BOOLEAN,
  415. .name = "Flip Horizontally",
  416. .minimum = 0,
  417. .maximum = 1,
  418. .step = 1,
  419. .default_value = 0,
  420. }, {
  421. .id = V4L2_CID_GAIN,
  422. .type = V4L2_CTRL_TYPE_INTEGER,
  423. .name = "Analog Gain",
  424. .minimum = 64,
  425. .maximum = 127,
  426. .step = 1,
  427. .default_value = 64,
  428. .flags = V4L2_CTRL_FLAG_SLIDER,
  429. }, {
  430. .id = V4L2_CID_EXPOSURE,
  431. .type = V4L2_CTRL_TYPE_INTEGER,
  432. .name = "Exposure",
  433. .minimum = 1,
  434. .maximum = 255,
  435. .step = 1,
  436. .default_value = 255,
  437. .flags = V4L2_CTRL_FLAG_SLIDER,
  438. }, {
  439. .id = V4L2_CID_AUTOGAIN,
  440. .type = V4L2_CTRL_TYPE_BOOLEAN,
  441. .name = "Automatic Gain",
  442. .minimum = 0,
  443. .maximum = 1,
  444. .step = 1,
  445. .default_value = 1,
  446. }, {
  447. .id = V4L2_CID_EXPOSURE_AUTO,
  448. .type = V4L2_CTRL_TYPE_BOOLEAN,
  449. .name = "Automatic Exposure",
  450. .minimum = 0,
  451. .maximum = 1,
  452. .step = 1,
  453. .default_value = 1,
  454. }
  455. };
  456. static int mt9v022_video_probe(struct soc_camera_device *);
  457. static void mt9v022_video_remove(struct soc_camera_device *);
  458. static int mt9v022_get_control(struct soc_camera_device *, struct v4l2_control *);
  459. static int mt9v022_set_control(struct soc_camera_device *, struct v4l2_control *);
  460. static struct soc_camera_ops mt9v022_ops = {
  461. .owner = THIS_MODULE,
  462. .probe = mt9v022_video_probe,
  463. .remove = mt9v022_video_remove,
  464. .init = mt9v022_init,
  465. .release = mt9v022_release,
  466. .start_capture = mt9v022_start_capture,
  467. .stop_capture = mt9v022_stop_capture,
  468. .set_fmt = mt9v022_set_fmt,
  469. .try_fmt = mt9v022_try_fmt,
  470. .set_bus_param = mt9v022_set_bus_param,
  471. .query_bus_param = mt9v022_query_bus_param,
  472. .controls = mt9v022_controls,
  473. .num_controls = ARRAY_SIZE(mt9v022_controls),
  474. .get_control = mt9v022_get_control,
  475. .set_control = mt9v022_set_control,
  476. .get_chip_id = mt9v022_get_chip_id,
  477. #ifdef CONFIG_VIDEO_ADV_DEBUG
  478. .get_register = mt9v022_get_register,
  479. .set_register = mt9v022_set_register,
  480. #endif
  481. };
  482. static int mt9v022_get_control(struct soc_camera_device *icd,
  483. struct v4l2_control *ctrl)
  484. {
  485. int data;
  486. switch (ctrl->id) {
  487. case V4L2_CID_VFLIP:
  488. data = reg_read(icd, MT9V022_READ_MODE);
  489. if (data < 0)
  490. return -EIO;
  491. ctrl->value = !!(data & 0x10);
  492. break;
  493. case V4L2_CID_HFLIP:
  494. data = reg_read(icd, MT9V022_READ_MODE);
  495. if (data < 0)
  496. return -EIO;
  497. ctrl->value = !!(data & 0x20);
  498. break;
  499. case V4L2_CID_EXPOSURE_AUTO:
  500. data = reg_read(icd, MT9V022_AEC_AGC_ENABLE);
  501. if (data < 0)
  502. return -EIO;
  503. ctrl->value = !!(data & 0x1);
  504. break;
  505. case V4L2_CID_AUTOGAIN:
  506. data = reg_read(icd, MT9V022_AEC_AGC_ENABLE);
  507. if (data < 0)
  508. return -EIO;
  509. ctrl->value = !!(data & 0x2);
  510. break;
  511. }
  512. return 0;
  513. }
  514. static int mt9v022_set_control(struct soc_camera_device *icd,
  515. struct v4l2_control *ctrl)
  516. {
  517. int data;
  518. const struct v4l2_queryctrl *qctrl;
  519. qctrl = soc_camera_find_qctrl(&mt9v022_ops, ctrl->id);
  520. if (!qctrl)
  521. return -EINVAL;
  522. switch (ctrl->id) {
  523. case V4L2_CID_VFLIP:
  524. if (ctrl->value)
  525. data = reg_set(icd, MT9V022_READ_MODE, 0x10);
  526. else
  527. data = reg_clear(icd, MT9V022_READ_MODE, 0x10);
  528. if (data < 0)
  529. return -EIO;
  530. break;
  531. case V4L2_CID_HFLIP:
  532. if (ctrl->value)
  533. data = reg_set(icd, MT9V022_READ_MODE, 0x20);
  534. else
  535. data = reg_clear(icd, MT9V022_READ_MODE, 0x20);
  536. if (data < 0)
  537. return -EIO;
  538. break;
  539. case V4L2_CID_GAIN:
  540. /* mt9v022 has minimum == default */
  541. if (ctrl->value > qctrl->maximum || ctrl->value < qctrl->minimum)
  542. return -EINVAL;
  543. else {
  544. unsigned long range = qctrl->maximum - qctrl->minimum;
  545. /* Datasheet says 16 to 64. autogain only works properly
  546. * after setting gain to maximum 14. Larger values
  547. * produce "white fly" noise effect. On the whole,
  548. * manually setting analog gain does no good. */
  549. unsigned long gain = ((ctrl->value - qctrl->minimum) *
  550. 10 + range / 2) / range + 4;
  551. if (gain >= 32)
  552. gain &= ~1;
  553. /* The user wants to set gain manually, hope, she
  554. * knows, what she's doing... Switch AGC off. */
  555. if (reg_clear(icd, MT9V022_AEC_AGC_ENABLE, 0x2) < 0)
  556. return -EIO;
  557. dev_info(&icd->dev, "Setting gain from %d to %lu\n",
  558. reg_read(icd, MT9V022_ANALOG_GAIN), gain);
  559. if (reg_write(icd, MT9V022_ANALOG_GAIN, gain) < 0)
  560. return -EIO;
  561. icd->gain = ctrl->value;
  562. }
  563. break;
  564. case V4L2_CID_EXPOSURE:
  565. /* mt9v022 has maximum == default */
  566. if (ctrl->value > qctrl->maximum || ctrl->value < qctrl->minimum)
  567. return -EINVAL;
  568. else {
  569. unsigned long range = qctrl->maximum - qctrl->minimum;
  570. unsigned long shutter = ((ctrl->value - qctrl->minimum) *
  571. 479 + range / 2) / range + 1;
  572. /* The user wants to set shutter width manually, hope,
  573. * she knows, what she's doing... Switch AEC off. */
  574. if (reg_clear(icd, MT9V022_AEC_AGC_ENABLE, 0x1) < 0)
  575. return -EIO;
  576. dev_dbg(&icd->dev, "Shutter width from %d to %lu\n",
  577. reg_read(icd, MT9V022_TOTAL_SHUTTER_WIDTH),
  578. shutter);
  579. if (reg_write(icd, MT9V022_TOTAL_SHUTTER_WIDTH,
  580. shutter) < 0)
  581. return -EIO;
  582. icd->exposure = ctrl->value;
  583. }
  584. break;
  585. case V4L2_CID_AUTOGAIN:
  586. if (ctrl->value)
  587. data = reg_set(icd, MT9V022_AEC_AGC_ENABLE, 0x2);
  588. else
  589. data = reg_clear(icd, MT9V022_AEC_AGC_ENABLE, 0x2);
  590. if (data < 0)
  591. return -EIO;
  592. break;
  593. case V4L2_CID_EXPOSURE_AUTO:
  594. if (ctrl->value)
  595. data = reg_set(icd, MT9V022_AEC_AGC_ENABLE, 0x1);
  596. else
  597. data = reg_clear(icd, MT9V022_AEC_AGC_ENABLE, 0x1);
  598. if (data < 0)
  599. return -EIO;
  600. break;
  601. }
  602. return 0;
  603. }
  604. /* Interface active, can use i2c. If it fails, it can indeed mean, that
  605. * this wasn't our capture interface, so, we wait for the right one */
  606. static int mt9v022_video_probe(struct soc_camera_device *icd)
  607. {
  608. struct mt9v022 *mt9v022 = container_of(icd, struct mt9v022, icd);
  609. struct soc_camera_link *icl = mt9v022->client->dev.platform_data;
  610. s32 data;
  611. int ret;
  612. if (!icd->dev.parent ||
  613. to_soc_camera_host(icd->dev.parent)->nr != icd->iface)
  614. return -ENODEV;
  615. /* Read out the chip version register */
  616. data = reg_read(icd, MT9V022_CHIP_VERSION);
  617. /* must be 0x1311 or 0x1313 */
  618. if (data != 0x1311 && data != 0x1313) {
  619. ret = -ENODEV;
  620. dev_info(&icd->dev, "No MT9V022 detected, ID register 0x%x\n",
  621. data);
  622. goto ei2c;
  623. }
  624. /* Soft reset */
  625. ret = reg_write(icd, MT9V022_RESET, 1);
  626. if (ret < 0)
  627. goto ei2c;
  628. /* 15 clock cycles */
  629. udelay(200);
  630. if (reg_read(icd, MT9V022_RESET)) {
  631. dev_err(&icd->dev, "Resetting MT9V022 failed!\n");
  632. goto ei2c;
  633. }
  634. /* Set monochrome or colour sensor type */
  635. if (sensor_type && (!strcmp("colour", sensor_type) ||
  636. !strcmp("color", sensor_type))) {
  637. ret = reg_write(icd, MT9V022_PIXEL_OPERATION_MODE, 4 | 0x11);
  638. mt9v022->model = V4L2_IDENT_MT9V022IX7ATC;
  639. icd->formats = mt9v022_colour_formats;
  640. if (gpio_is_valid(icl->gpio))
  641. icd->num_formats = ARRAY_SIZE(mt9v022_colour_formats);
  642. else
  643. icd->num_formats = 1;
  644. } else {
  645. ret = reg_write(icd, MT9V022_PIXEL_OPERATION_MODE, 0x11);
  646. mt9v022->model = V4L2_IDENT_MT9V022IX7ATM;
  647. icd->formats = mt9v022_monochrome_formats;
  648. if (gpio_is_valid(icl->gpio))
  649. icd->num_formats = ARRAY_SIZE(mt9v022_monochrome_formats);
  650. else
  651. icd->num_formats = 1;
  652. }
  653. if (!ret)
  654. ret = soc_camera_video_start(icd);
  655. if (ret < 0)
  656. goto eisis;
  657. dev_info(&icd->dev, "Detected a MT9V022 chip ID %x, %s sensor\n",
  658. data, mt9v022->model == V4L2_IDENT_MT9V022IX7ATM ?
  659. "monochrome" : "colour");
  660. return 0;
  661. eisis:
  662. ei2c:
  663. return ret;
  664. }
  665. static void mt9v022_video_remove(struct soc_camera_device *icd)
  666. {
  667. struct mt9v022 *mt9v022 = container_of(icd, struct mt9v022, icd);
  668. dev_dbg(&icd->dev, "Video %x removed: %p, %p\n", mt9v022->client->addr,
  669. icd->dev.parent, icd->vdev);
  670. soc_camera_video_stop(icd);
  671. }
  672. static int mt9v022_probe(struct i2c_client *client,
  673. const struct i2c_device_id *did)
  674. {
  675. struct mt9v022 *mt9v022;
  676. struct soc_camera_device *icd;
  677. struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
  678. struct soc_camera_link *icl = client->dev.platform_data;
  679. int ret;
  680. if (!icl) {
  681. dev_err(&client->dev, "MT9V022 driver needs platform data\n");
  682. return -EINVAL;
  683. }
  684. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WORD_DATA)) {
  685. dev_warn(&adapter->dev,
  686. "I2C-Adapter doesn't support I2C_FUNC_SMBUS_WORD\n");
  687. return -EIO;
  688. }
  689. mt9v022 = kzalloc(sizeof(struct mt9v022), GFP_KERNEL);
  690. if (!mt9v022)
  691. return -ENOMEM;
  692. mt9v022->chip_control = MT9V022_CHIP_CONTROL_DEFAULT;
  693. mt9v022->client = client;
  694. i2c_set_clientdata(client, mt9v022);
  695. icd = &mt9v022->icd;
  696. icd->ops = &mt9v022_ops;
  697. icd->control = &client->dev;
  698. icd->x_min = 1;
  699. icd->y_min = 4;
  700. icd->x_current = 1;
  701. icd->y_current = 4;
  702. icd->width_min = 48;
  703. icd->width_max = 752;
  704. icd->height_min = 32;
  705. icd->height_max = 480;
  706. icd->y_skip_top = 1;
  707. icd->iface = icl->bus_id;
  708. /* Default datawidth - this is the only width this camera (normally)
  709. * supports. It is only with extra logic that it can support
  710. * other widths. Therefore it seems to be a sensible default. */
  711. mt9v022->datawidth = 10;
  712. ret = bus_switch_request(mt9v022, icl);
  713. if (ret)
  714. goto eswinit;
  715. ret = soc_camera_device_register(icd);
  716. if (ret)
  717. goto eisdr;
  718. return 0;
  719. eisdr:
  720. bus_switch_release(mt9v022);
  721. eswinit:
  722. kfree(mt9v022);
  723. return ret;
  724. }
  725. static int mt9v022_remove(struct i2c_client *client)
  726. {
  727. struct mt9v022 *mt9v022 = i2c_get_clientdata(client);
  728. soc_camera_device_unregister(&mt9v022->icd);
  729. bus_switch_release(mt9v022);
  730. kfree(mt9v022);
  731. return 0;
  732. }
  733. static const struct i2c_device_id mt9v022_id[] = {
  734. { "mt9v022", 0 },
  735. { }
  736. };
  737. MODULE_DEVICE_TABLE(i2c, mt9v022_id);
  738. static struct i2c_driver mt9v022_i2c_driver = {
  739. .driver = {
  740. .name = "mt9v022",
  741. },
  742. .probe = mt9v022_probe,
  743. .remove = mt9v022_remove,
  744. .id_table = mt9v022_id,
  745. };
  746. static int __init mt9v022_mod_init(void)
  747. {
  748. return i2c_add_driver(&mt9v022_i2c_driver);
  749. }
  750. static void __exit mt9v022_mod_exit(void)
  751. {
  752. i2c_del_driver(&mt9v022_i2c_driver);
  753. }
  754. module_init(mt9v022_mod_init);
  755. module_exit(mt9v022_mod_exit);
  756. MODULE_DESCRIPTION("Micron MT9V022 Camera driver");
  757. MODULE_AUTHOR("Guennadi Liakhovetski <kernel@pengutronix.de>");
  758. MODULE_LICENSE("GPL");