mt9v022.c 20 KB

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