mt9v022.c 20 KB

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