mt9v022.c 21 KB

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