mt9v022.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844
  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. if (pix->height < 32 + icd->y_skip_top)
  317. pix->height = 32 + icd->y_skip_top;
  318. if (pix->height > 480 + icd->y_skip_top)
  319. pix->height = 480 + icd->y_skip_top;
  320. if (pix->width < 48)
  321. pix->width = 48;
  322. if (pix->width > 752)
  323. pix->width = 752;
  324. pix->width &= ~0x03; /* ? */
  325. return 0;
  326. }
  327. static int mt9v022_get_chip_id(struct soc_camera_device *icd,
  328. struct v4l2_dbg_chip_ident *id)
  329. {
  330. struct mt9v022 *mt9v022 = container_of(icd, struct mt9v022, icd);
  331. if (id->match.type != V4L2_CHIP_MATCH_I2C_ADDR)
  332. return -EINVAL;
  333. if (id->match.addr != mt9v022->client->addr)
  334. return -ENODEV;
  335. id->ident = mt9v022->model;
  336. id->revision = 0;
  337. return 0;
  338. }
  339. #ifdef CONFIG_VIDEO_ADV_DEBUG
  340. static int mt9v022_get_register(struct soc_camera_device *icd,
  341. struct v4l2_dbg_register *reg)
  342. {
  343. struct i2c_client *client = to_i2c_client(icd->control);
  344. if (reg->match.type != V4L2_CHIP_MATCH_I2C_ADDR || reg->reg > 0xff)
  345. return -EINVAL;
  346. if (reg->match.addr != client->addr)
  347. return -ENODEV;
  348. reg->size = 2;
  349. reg->val = reg_read(client, reg->reg);
  350. if (reg->val > 0xffff)
  351. return -EIO;
  352. return 0;
  353. }
  354. static int mt9v022_set_register(struct soc_camera_device *icd,
  355. struct v4l2_dbg_register *reg)
  356. {
  357. struct i2c_client *client = to_i2c_client(icd->control);
  358. if (reg->match.type != V4L2_CHIP_MATCH_I2C_ADDR || reg->reg > 0xff)
  359. return -EINVAL;
  360. if (reg->match.addr != client->addr)
  361. return -ENODEV;
  362. if (reg_write(client, reg->reg, reg->val) < 0)
  363. return -EIO;
  364. return 0;
  365. }
  366. #endif
  367. static const struct v4l2_queryctrl mt9v022_controls[] = {
  368. {
  369. .id = V4L2_CID_VFLIP,
  370. .type = V4L2_CTRL_TYPE_BOOLEAN,
  371. .name = "Flip Vertically",
  372. .minimum = 0,
  373. .maximum = 1,
  374. .step = 1,
  375. .default_value = 0,
  376. }, {
  377. .id = V4L2_CID_HFLIP,
  378. .type = V4L2_CTRL_TYPE_BOOLEAN,
  379. .name = "Flip Horizontally",
  380. .minimum = 0,
  381. .maximum = 1,
  382. .step = 1,
  383. .default_value = 0,
  384. }, {
  385. .id = V4L2_CID_GAIN,
  386. .type = V4L2_CTRL_TYPE_INTEGER,
  387. .name = "Analog Gain",
  388. .minimum = 64,
  389. .maximum = 127,
  390. .step = 1,
  391. .default_value = 64,
  392. .flags = V4L2_CTRL_FLAG_SLIDER,
  393. }, {
  394. .id = V4L2_CID_EXPOSURE,
  395. .type = V4L2_CTRL_TYPE_INTEGER,
  396. .name = "Exposure",
  397. .minimum = 1,
  398. .maximum = 255,
  399. .step = 1,
  400. .default_value = 255,
  401. .flags = V4L2_CTRL_FLAG_SLIDER,
  402. }, {
  403. .id = V4L2_CID_AUTOGAIN,
  404. .type = V4L2_CTRL_TYPE_BOOLEAN,
  405. .name = "Automatic Gain",
  406. .minimum = 0,
  407. .maximum = 1,
  408. .step = 1,
  409. .default_value = 1,
  410. }, {
  411. .id = V4L2_CID_EXPOSURE_AUTO,
  412. .type = V4L2_CTRL_TYPE_BOOLEAN,
  413. .name = "Automatic Exposure",
  414. .minimum = 0,
  415. .maximum = 1,
  416. .step = 1,
  417. .default_value = 1,
  418. }
  419. };
  420. static int mt9v022_video_probe(struct soc_camera_device *);
  421. static void mt9v022_video_remove(struct soc_camera_device *);
  422. static int mt9v022_get_control(struct soc_camera_device *, struct v4l2_control *);
  423. static int mt9v022_set_control(struct soc_camera_device *, struct v4l2_control *);
  424. static struct soc_camera_ops mt9v022_ops = {
  425. .owner = THIS_MODULE,
  426. .probe = mt9v022_video_probe,
  427. .remove = mt9v022_video_remove,
  428. .init = mt9v022_init,
  429. .release = mt9v022_release,
  430. .start_capture = mt9v022_start_capture,
  431. .stop_capture = mt9v022_stop_capture,
  432. .set_crop = mt9v022_set_crop,
  433. .set_fmt = mt9v022_set_fmt,
  434. .try_fmt = mt9v022_try_fmt,
  435. .set_bus_param = mt9v022_set_bus_param,
  436. .query_bus_param = mt9v022_query_bus_param,
  437. .controls = mt9v022_controls,
  438. .num_controls = ARRAY_SIZE(mt9v022_controls),
  439. .get_control = mt9v022_get_control,
  440. .set_control = mt9v022_set_control,
  441. .get_chip_id = mt9v022_get_chip_id,
  442. #ifdef CONFIG_VIDEO_ADV_DEBUG
  443. .get_register = mt9v022_get_register,
  444. .set_register = mt9v022_set_register,
  445. #endif
  446. };
  447. static int mt9v022_get_control(struct soc_camera_device *icd,
  448. struct v4l2_control *ctrl)
  449. {
  450. struct i2c_client *client = to_i2c_client(icd->control);
  451. int data;
  452. switch (ctrl->id) {
  453. case V4L2_CID_VFLIP:
  454. data = reg_read(client, MT9V022_READ_MODE);
  455. if (data < 0)
  456. return -EIO;
  457. ctrl->value = !!(data & 0x10);
  458. break;
  459. case V4L2_CID_HFLIP:
  460. data = reg_read(client, MT9V022_READ_MODE);
  461. if (data < 0)
  462. return -EIO;
  463. ctrl->value = !!(data & 0x20);
  464. break;
  465. case V4L2_CID_EXPOSURE_AUTO:
  466. data = reg_read(client, MT9V022_AEC_AGC_ENABLE);
  467. if (data < 0)
  468. return -EIO;
  469. ctrl->value = !!(data & 0x1);
  470. break;
  471. case V4L2_CID_AUTOGAIN:
  472. data = reg_read(client, MT9V022_AEC_AGC_ENABLE);
  473. if (data < 0)
  474. return -EIO;
  475. ctrl->value = !!(data & 0x2);
  476. break;
  477. }
  478. return 0;
  479. }
  480. static int mt9v022_set_control(struct soc_camera_device *icd,
  481. struct v4l2_control *ctrl)
  482. {
  483. int data;
  484. struct i2c_client *client = to_i2c_client(icd->control);
  485. const struct v4l2_queryctrl *qctrl;
  486. qctrl = soc_camera_find_qctrl(&mt9v022_ops, ctrl->id);
  487. if (!qctrl)
  488. return -EINVAL;
  489. switch (ctrl->id) {
  490. case V4L2_CID_VFLIP:
  491. if (ctrl->value)
  492. data = reg_set(client, MT9V022_READ_MODE, 0x10);
  493. else
  494. data = reg_clear(client, MT9V022_READ_MODE, 0x10);
  495. if (data < 0)
  496. return -EIO;
  497. break;
  498. case V4L2_CID_HFLIP:
  499. if (ctrl->value)
  500. data = reg_set(client, MT9V022_READ_MODE, 0x20);
  501. else
  502. data = reg_clear(client, MT9V022_READ_MODE, 0x20);
  503. if (data < 0)
  504. return -EIO;
  505. break;
  506. case V4L2_CID_GAIN:
  507. /* mt9v022 has minimum == default */
  508. if (ctrl->value > qctrl->maximum || ctrl->value < qctrl->minimum)
  509. return -EINVAL;
  510. else {
  511. unsigned long range = qctrl->maximum - qctrl->minimum;
  512. /* Datasheet says 16 to 64. autogain only works properly
  513. * after setting gain to maximum 14. Larger values
  514. * produce "white fly" noise effect. On the whole,
  515. * manually setting analog gain does no good. */
  516. unsigned long gain = ((ctrl->value - qctrl->minimum) *
  517. 10 + range / 2) / range + 4;
  518. if (gain >= 32)
  519. gain &= ~1;
  520. /* The user wants to set gain manually, hope, she
  521. * knows, what she's doing... Switch AGC off. */
  522. if (reg_clear(client, MT9V022_AEC_AGC_ENABLE, 0x2) < 0)
  523. return -EIO;
  524. dev_info(&icd->dev, "Setting gain from %d to %lu\n",
  525. reg_read(client, MT9V022_ANALOG_GAIN), gain);
  526. if (reg_write(client, MT9V022_ANALOG_GAIN, gain) < 0)
  527. return -EIO;
  528. icd->gain = ctrl->value;
  529. }
  530. break;
  531. case V4L2_CID_EXPOSURE:
  532. /* mt9v022 has maximum == default */
  533. if (ctrl->value > qctrl->maximum || ctrl->value < qctrl->minimum)
  534. return -EINVAL;
  535. else {
  536. unsigned long range = qctrl->maximum - qctrl->minimum;
  537. unsigned long shutter = ((ctrl->value - qctrl->minimum) *
  538. 479 + range / 2) / range + 1;
  539. /* The user wants to set shutter width manually, hope,
  540. * she knows, what she's doing... Switch AEC off. */
  541. if (reg_clear(client, MT9V022_AEC_AGC_ENABLE, 0x1) < 0)
  542. return -EIO;
  543. dev_dbg(&icd->dev, "Shutter width from %d to %lu\n",
  544. reg_read(client, MT9V022_TOTAL_SHUTTER_WIDTH),
  545. shutter);
  546. if (reg_write(client, MT9V022_TOTAL_SHUTTER_WIDTH,
  547. shutter) < 0)
  548. return -EIO;
  549. icd->exposure = ctrl->value;
  550. }
  551. break;
  552. case V4L2_CID_AUTOGAIN:
  553. if (ctrl->value)
  554. data = reg_set(client, MT9V022_AEC_AGC_ENABLE, 0x2);
  555. else
  556. data = reg_clear(client, MT9V022_AEC_AGC_ENABLE, 0x2);
  557. if (data < 0)
  558. return -EIO;
  559. break;
  560. case V4L2_CID_EXPOSURE_AUTO:
  561. if (ctrl->value)
  562. data = reg_set(client, MT9V022_AEC_AGC_ENABLE, 0x1);
  563. else
  564. data = reg_clear(client, MT9V022_AEC_AGC_ENABLE, 0x1);
  565. if (data < 0)
  566. return -EIO;
  567. break;
  568. }
  569. return 0;
  570. }
  571. /* Interface active, can use i2c. If it fails, it can indeed mean, that
  572. * this wasn't our capture interface, so, we wait for the right one */
  573. static int mt9v022_video_probe(struct soc_camera_device *icd)
  574. {
  575. struct i2c_client *client = to_i2c_client(icd->control);
  576. struct mt9v022 *mt9v022 = container_of(icd, struct mt9v022, icd);
  577. struct soc_camera_link *icl = client->dev.platform_data;
  578. s32 data;
  579. int ret;
  580. unsigned long flags;
  581. if (!icd->dev.parent ||
  582. to_soc_camera_host(icd->dev.parent)->nr != icd->iface)
  583. return -ENODEV;
  584. /* Read out the chip version register */
  585. data = reg_read(client, MT9V022_CHIP_VERSION);
  586. /* must be 0x1311 or 0x1313 */
  587. if (data != 0x1311 && data != 0x1313) {
  588. ret = -ENODEV;
  589. dev_info(&icd->dev, "No MT9V022 detected, ID register 0x%x\n",
  590. data);
  591. goto ei2c;
  592. }
  593. /* Soft reset */
  594. ret = reg_write(client, MT9V022_RESET, 1);
  595. if (ret < 0)
  596. goto ei2c;
  597. /* 15 clock cycles */
  598. udelay(200);
  599. if (reg_read(client, MT9V022_RESET)) {
  600. dev_err(&icd->dev, "Resetting MT9V022 failed!\n");
  601. goto ei2c;
  602. }
  603. /* Set monochrome or colour sensor type */
  604. if (sensor_type && (!strcmp("colour", sensor_type) ||
  605. !strcmp("color", sensor_type))) {
  606. ret = reg_write(client, MT9V022_PIXEL_OPERATION_MODE, 4 | 0x11);
  607. mt9v022->model = V4L2_IDENT_MT9V022IX7ATC;
  608. icd->formats = mt9v022_colour_formats;
  609. } else {
  610. ret = reg_write(client, MT9V022_PIXEL_OPERATION_MODE, 0x11);
  611. mt9v022->model = V4L2_IDENT_MT9V022IX7ATM;
  612. icd->formats = mt9v022_monochrome_formats;
  613. }
  614. if (ret < 0)
  615. goto eisis;
  616. icd->num_formats = 0;
  617. /*
  618. * This is a 10bit sensor, so by default we only allow 10bit.
  619. * The platform may support different bus widths due to
  620. * different routing of the data lines.
  621. */
  622. if (icl->query_bus_param)
  623. flags = icl->query_bus_param(icl);
  624. else
  625. flags = SOCAM_DATAWIDTH_10;
  626. if (flags & SOCAM_DATAWIDTH_10)
  627. icd->num_formats++;
  628. else
  629. icd->formats++;
  630. if (flags & SOCAM_DATAWIDTH_8)
  631. icd->num_formats++;
  632. ret = soc_camera_video_start(icd);
  633. if (ret < 0)
  634. goto eisis;
  635. dev_info(&icd->dev, "Detected a MT9V022 chip ID %x, %s sensor\n",
  636. data, mt9v022->model == V4L2_IDENT_MT9V022IX7ATM ?
  637. "monochrome" : "colour");
  638. return 0;
  639. eisis:
  640. ei2c:
  641. return ret;
  642. }
  643. static void mt9v022_video_remove(struct soc_camera_device *icd)
  644. {
  645. struct mt9v022 *mt9v022 = container_of(icd, struct mt9v022, icd);
  646. struct soc_camera_link *icl = mt9v022->client->dev.platform_data;
  647. dev_dbg(&icd->dev, "Video %x removed: %p, %p\n", mt9v022->client->addr,
  648. icd->dev.parent, icd->vdev);
  649. soc_camera_video_stop(icd);
  650. if (icl->free_bus)
  651. icl->free_bus(icl);
  652. }
  653. static int mt9v022_probe(struct i2c_client *client,
  654. const struct i2c_device_id *did)
  655. {
  656. struct mt9v022 *mt9v022;
  657. struct soc_camera_device *icd;
  658. struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
  659. struct soc_camera_link *icl = client->dev.platform_data;
  660. int ret;
  661. if (!icl) {
  662. dev_err(&client->dev, "MT9V022 driver needs platform data\n");
  663. return -EINVAL;
  664. }
  665. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WORD_DATA)) {
  666. dev_warn(&adapter->dev,
  667. "I2C-Adapter doesn't support I2C_FUNC_SMBUS_WORD\n");
  668. return -EIO;
  669. }
  670. mt9v022 = kzalloc(sizeof(struct mt9v022), GFP_KERNEL);
  671. if (!mt9v022)
  672. return -ENOMEM;
  673. mt9v022->chip_control = MT9V022_CHIP_CONTROL_DEFAULT;
  674. mt9v022->client = client;
  675. i2c_set_clientdata(client, mt9v022);
  676. icd = &mt9v022->icd;
  677. icd->ops = &mt9v022_ops;
  678. icd->control = &client->dev;
  679. icd->x_min = 1;
  680. icd->y_min = 4;
  681. icd->x_current = 1;
  682. icd->y_current = 4;
  683. icd->width_min = 48;
  684. icd->width_max = 752;
  685. icd->height_min = 32;
  686. icd->height_max = 480;
  687. icd->y_skip_top = 1;
  688. icd->iface = icl->bus_id;
  689. ret = soc_camera_device_register(icd);
  690. if (ret)
  691. goto eisdr;
  692. return 0;
  693. eisdr:
  694. kfree(mt9v022);
  695. return ret;
  696. }
  697. static int mt9v022_remove(struct i2c_client *client)
  698. {
  699. struct mt9v022 *mt9v022 = i2c_get_clientdata(client);
  700. soc_camera_device_unregister(&mt9v022->icd);
  701. kfree(mt9v022);
  702. return 0;
  703. }
  704. static const struct i2c_device_id mt9v022_id[] = {
  705. { "mt9v022", 0 },
  706. { }
  707. };
  708. MODULE_DEVICE_TABLE(i2c, mt9v022_id);
  709. static struct i2c_driver mt9v022_i2c_driver = {
  710. .driver = {
  711. .name = "mt9v022",
  712. },
  713. .probe = mt9v022_probe,
  714. .remove = mt9v022_remove,
  715. .id_table = mt9v022_id,
  716. };
  717. static int __init mt9v022_mod_init(void)
  718. {
  719. return i2c_add_driver(&mt9v022_i2c_driver);
  720. }
  721. static void __exit mt9v022_mod_exit(void)
  722. {
  723. i2c_del_driver(&mt9v022_i2c_driver);
  724. }
  725. module_init(mt9v022_mod_init);
  726. module_exit(mt9v022_mod_exit);
  727. MODULE_DESCRIPTION("Micron MT9V022 Camera driver");
  728. MODULE_AUTHOR("Guennadi Liakhovetski <kernel@pengutronix.de>");
  729. MODULE_LICENSE("GPL");