mt9v022.c 23 KB

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