mt9v022.c 22 KB

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