mt9v022.c 21 KB

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