mt9v022.c 21 KB

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