mt9v022.c 23 KB

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