mt9v022.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047
  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/soc_camera.h>
  16. #include <media/soc_mediabus.h>
  17. #include <media/v4l2-subdev.h>
  18. #include <media/v4l2-chip-ident.h>
  19. /*
  20. * mt9v022 i2c address 0x48, 0x4c, 0x58, 0x5c
  21. * The platform has to define ctruct i2c_board_info objects and link to them
  22. * from struct soc_camera_link
  23. */
  24. static char *sensor_type;
  25. module_param(sensor_type, charp, S_IRUGO);
  26. MODULE_PARM_DESC(sensor_type, "Sensor type: \"colour\" or \"monochrome\"");
  27. /* mt9v022 selected register addresses */
  28. #define MT9V022_CHIP_VERSION 0x00
  29. #define MT9V022_COLUMN_START 0x01
  30. #define MT9V022_ROW_START 0x02
  31. #define MT9V022_WINDOW_HEIGHT 0x03
  32. #define MT9V022_WINDOW_WIDTH 0x04
  33. #define MT9V022_HORIZONTAL_BLANKING 0x05
  34. #define MT9V022_VERTICAL_BLANKING 0x06
  35. #define MT9V022_CHIP_CONTROL 0x07
  36. #define MT9V022_SHUTTER_WIDTH1 0x08
  37. #define MT9V022_SHUTTER_WIDTH2 0x09
  38. #define MT9V022_SHUTTER_WIDTH_CTRL 0x0a
  39. #define MT9V022_TOTAL_SHUTTER_WIDTH 0x0b
  40. #define MT9V022_RESET 0x0c
  41. #define MT9V022_READ_MODE 0x0d
  42. #define MT9V022_MONITOR_MODE 0x0e
  43. #define MT9V022_PIXEL_OPERATION_MODE 0x0f
  44. #define MT9V022_LED_OUT_CONTROL 0x1b
  45. #define MT9V022_ADC_MODE_CONTROL 0x1c
  46. #define MT9V022_ANALOG_GAIN 0x35
  47. #define MT9V022_BLACK_LEVEL_CALIB_CTRL 0x47
  48. #define MT9V022_PIXCLK_FV_LV 0x74
  49. #define MT9V022_DIGITAL_TEST_PATTERN 0x7f
  50. #define MT9V022_AEC_AGC_ENABLE 0xAF
  51. #define MT9V022_MAX_TOTAL_SHUTTER_WIDTH 0xBD
  52. /* Progressive scan, master, defaults */
  53. #define MT9V022_CHIP_CONTROL_DEFAULT 0x188
  54. #define MT9V022_MAX_WIDTH 752
  55. #define MT9V022_MAX_HEIGHT 480
  56. #define MT9V022_MIN_WIDTH 48
  57. #define MT9V022_MIN_HEIGHT 32
  58. #define MT9V022_COLUMN_SKIP 1
  59. #define MT9V022_ROW_SKIP 4
  60. /* MT9V022 has only one fixed colorspace per pixelcode */
  61. struct mt9v022_datafmt {
  62. enum v4l2_mbus_pixelcode code;
  63. enum v4l2_colorspace colorspace;
  64. };
  65. /* Find a data format by a pixel code in an array */
  66. static const struct mt9v022_datafmt *mt9v022_find_datafmt(
  67. enum v4l2_mbus_pixelcode code, const struct mt9v022_datafmt *fmt,
  68. int n)
  69. {
  70. int i;
  71. for (i = 0; i < n; i++)
  72. if (fmt[i].code == code)
  73. return fmt + i;
  74. return NULL;
  75. }
  76. static const struct mt9v022_datafmt mt9v022_colour_fmts[] = {
  77. /*
  78. * Order important: first natively supported,
  79. * second supported with a GPIO extender
  80. */
  81. {V4L2_MBUS_FMT_SBGGR10_1X10, V4L2_COLORSPACE_SRGB},
  82. {V4L2_MBUS_FMT_SBGGR8_1X8, V4L2_COLORSPACE_SRGB},
  83. };
  84. static const struct mt9v022_datafmt mt9v022_monochrome_fmts[] = {
  85. /* Order important - see above */
  86. {V4L2_MBUS_FMT_Y10_1X10, V4L2_COLORSPACE_JPEG},
  87. {V4L2_MBUS_FMT_Y8_1X8, V4L2_COLORSPACE_JPEG},
  88. };
  89. struct mt9v022 {
  90. struct v4l2_subdev subdev;
  91. struct v4l2_rect rect; /* Sensor window */
  92. const struct mt9v022_datafmt *fmt;
  93. const struct mt9v022_datafmt *fmts;
  94. int num_fmts;
  95. int model; /* V4L2_IDENT_MT9V022* codes from v4l2-chip-ident.h */
  96. u16 chip_control;
  97. unsigned short y_skip_top; /* Lines to skip at the top */
  98. };
  99. static struct mt9v022 *to_mt9v022(const struct i2c_client *client)
  100. {
  101. return container_of(i2c_get_clientdata(client), struct mt9v022, subdev);
  102. }
  103. static int reg_read(struct i2c_client *client, const u8 reg)
  104. {
  105. s32 data = i2c_smbus_read_word_data(client, reg);
  106. return data < 0 ? data : swab16(data);
  107. }
  108. static int reg_write(struct i2c_client *client, const u8 reg,
  109. const u16 data)
  110. {
  111. return i2c_smbus_write_word_data(client, reg, swab16(data));
  112. }
  113. static int reg_set(struct i2c_client *client, const u8 reg,
  114. const u16 data)
  115. {
  116. int ret;
  117. ret = reg_read(client, reg);
  118. if (ret < 0)
  119. return ret;
  120. return reg_write(client, reg, ret | data);
  121. }
  122. static int reg_clear(struct i2c_client *client, const u8 reg,
  123. const u16 data)
  124. {
  125. int ret;
  126. ret = reg_read(client, reg);
  127. if (ret < 0)
  128. return ret;
  129. return reg_write(client, reg, ret & ~data);
  130. }
  131. static int mt9v022_init(struct i2c_client *client)
  132. {
  133. struct mt9v022 *mt9v022 = to_mt9v022(client);
  134. int ret;
  135. /*
  136. * Almost the default mode: master, parallel, simultaneous, and an
  137. * undocumented bit 0x200, which is present in table 7, but not in 8,
  138. * plus snapshot mode to disable scan for now
  139. */
  140. mt9v022->chip_control |= 0x10;
  141. ret = reg_write(client, MT9V022_CHIP_CONTROL, mt9v022->chip_control);
  142. if (!ret)
  143. ret = reg_write(client, MT9V022_READ_MODE, 0x300);
  144. /* All defaults */
  145. if (!ret)
  146. /* AEC, AGC on */
  147. ret = reg_set(client, MT9V022_AEC_AGC_ENABLE, 0x3);
  148. if (!ret)
  149. ret = reg_write(client, MT9V022_ANALOG_GAIN, 16);
  150. if (!ret)
  151. ret = reg_write(client, MT9V022_TOTAL_SHUTTER_WIDTH, 480);
  152. if (!ret)
  153. ret = reg_write(client, MT9V022_MAX_TOTAL_SHUTTER_WIDTH, 480);
  154. if (!ret)
  155. /* default - auto */
  156. ret = reg_clear(client, MT9V022_BLACK_LEVEL_CALIB_CTRL, 1);
  157. if (!ret)
  158. ret = reg_write(client, MT9V022_DIGITAL_TEST_PATTERN, 0);
  159. return ret;
  160. }
  161. static int mt9v022_s_stream(struct v4l2_subdev *sd, int enable)
  162. {
  163. struct i2c_client *client = v4l2_get_subdevdata(sd);
  164. struct mt9v022 *mt9v022 = to_mt9v022(client);
  165. if (enable)
  166. /* Switch to master "normal" mode */
  167. mt9v022->chip_control &= ~0x10;
  168. else
  169. /* Switch to snapshot mode */
  170. mt9v022->chip_control |= 0x10;
  171. if (reg_write(client, MT9V022_CHIP_CONTROL, mt9v022->chip_control) < 0)
  172. return -EIO;
  173. return 0;
  174. }
  175. static int mt9v022_set_bus_param(struct soc_camera_device *icd,
  176. unsigned long flags)
  177. {
  178. struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
  179. struct mt9v022 *mt9v022 = to_mt9v022(client);
  180. struct soc_camera_link *icl = to_soc_camera_link(icd);
  181. unsigned int width_flag = flags & SOCAM_DATAWIDTH_MASK;
  182. int ret;
  183. u16 pixclk = 0;
  184. /* Only one width bit may be set */
  185. if (!is_power_of_2(width_flag))
  186. return -EINVAL;
  187. if (icl->set_bus_param) {
  188. ret = icl->set_bus_param(icl, width_flag);
  189. if (ret)
  190. return ret;
  191. } else {
  192. /*
  193. * Without board specific bus width settings we only support the
  194. * sensors native bus width
  195. */
  196. if (width_flag != SOCAM_DATAWIDTH_10)
  197. return -EINVAL;
  198. }
  199. flags = soc_camera_apply_sensor_flags(icl, flags);
  200. if (flags & SOCAM_PCLK_SAMPLE_FALLING)
  201. pixclk |= 0x10;
  202. if (!(flags & SOCAM_HSYNC_ACTIVE_HIGH))
  203. pixclk |= 0x1;
  204. if (!(flags & SOCAM_VSYNC_ACTIVE_HIGH))
  205. pixclk |= 0x2;
  206. ret = reg_write(client, MT9V022_PIXCLK_FV_LV, pixclk);
  207. if (ret < 0)
  208. return ret;
  209. if (!(flags & SOCAM_MASTER))
  210. mt9v022->chip_control &= ~0x8;
  211. ret = reg_write(client, MT9V022_CHIP_CONTROL, mt9v022->chip_control);
  212. if (ret < 0)
  213. return ret;
  214. dev_dbg(&client->dev, "Calculated pixclk 0x%x, chip control 0x%x\n",
  215. pixclk, mt9v022->chip_control);
  216. return 0;
  217. }
  218. static unsigned long mt9v022_query_bus_param(struct soc_camera_device *icd)
  219. {
  220. struct soc_camera_link *icl = to_soc_camera_link(icd);
  221. unsigned int flags = SOCAM_MASTER | SOCAM_SLAVE |
  222. SOCAM_PCLK_SAMPLE_RISING | SOCAM_PCLK_SAMPLE_FALLING |
  223. SOCAM_HSYNC_ACTIVE_HIGH | SOCAM_HSYNC_ACTIVE_LOW |
  224. SOCAM_VSYNC_ACTIVE_HIGH | SOCAM_VSYNC_ACTIVE_LOW |
  225. SOCAM_DATA_ACTIVE_HIGH;
  226. if (icl->query_bus_param)
  227. flags |= icl->query_bus_param(icl) & SOCAM_DATAWIDTH_MASK;
  228. else
  229. flags |= SOCAM_DATAWIDTH_10;
  230. return soc_camera_apply_sensor_flags(icl, flags);
  231. }
  232. static int mt9v022_s_crop(struct v4l2_subdev *sd, struct v4l2_crop *a)
  233. {
  234. struct i2c_client *client = v4l2_get_subdevdata(sd);
  235. struct mt9v022 *mt9v022 = to_mt9v022(client);
  236. struct v4l2_rect rect = a->c;
  237. int ret;
  238. /* Bayer format - even size lengths */
  239. if (mt9v022->fmts == mt9v022_colour_fmts) {
  240. rect.width = ALIGN(rect.width, 2);
  241. rect.height = ALIGN(rect.height, 2);
  242. /* Let the user play with the starting pixel */
  243. }
  244. soc_camera_limit_side(&rect.left, &rect.width,
  245. MT9V022_COLUMN_SKIP, MT9V022_MIN_WIDTH, MT9V022_MAX_WIDTH);
  246. soc_camera_limit_side(&rect.top, &rect.height,
  247. MT9V022_ROW_SKIP, MT9V022_MIN_HEIGHT, MT9V022_MAX_HEIGHT);
  248. /* Like in example app. Contradicts the datasheet though */
  249. ret = reg_read(client, MT9V022_AEC_AGC_ENABLE);
  250. if (ret >= 0) {
  251. if (ret & 1) /* Autoexposure */
  252. ret = reg_write(client, MT9V022_MAX_TOTAL_SHUTTER_WIDTH,
  253. rect.height + mt9v022->y_skip_top + 43);
  254. else
  255. ret = reg_write(client, MT9V022_TOTAL_SHUTTER_WIDTH,
  256. rect.height + mt9v022->y_skip_top + 43);
  257. }
  258. /* Setup frame format: defaults apart from width and height */
  259. if (!ret)
  260. ret = reg_write(client, MT9V022_COLUMN_START, rect.left);
  261. if (!ret)
  262. ret = reg_write(client, MT9V022_ROW_START, rect.top);
  263. if (!ret)
  264. /*
  265. * Default 94, Phytec driver says:
  266. * "width + horizontal blank >= 660"
  267. */
  268. ret = reg_write(client, MT9V022_HORIZONTAL_BLANKING,
  269. rect.width > 660 - 43 ? 43 :
  270. 660 - rect.width);
  271. if (!ret)
  272. ret = reg_write(client, MT9V022_VERTICAL_BLANKING, 45);
  273. if (!ret)
  274. ret = reg_write(client, MT9V022_WINDOW_WIDTH, rect.width);
  275. if (!ret)
  276. ret = reg_write(client, MT9V022_WINDOW_HEIGHT,
  277. rect.height + mt9v022->y_skip_top);
  278. if (ret < 0)
  279. return ret;
  280. dev_dbg(&client->dev, "Frame %dx%d pixel\n", rect.width, rect.height);
  281. mt9v022->rect = rect;
  282. return 0;
  283. }
  284. static int mt9v022_g_crop(struct v4l2_subdev *sd, struct v4l2_crop *a)
  285. {
  286. struct i2c_client *client = v4l2_get_subdevdata(sd);
  287. struct mt9v022 *mt9v022 = to_mt9v022(client);
  288. a->c = mt9v022->rect;
  289. a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  290. return 0;
  291. }
  292. static int mt9v022_cropcap(struct v4l2_subdev *sd, struct v4l2_cropcap *a)
  293. {
  294. a->bounds.left = MT9V022_COLUMN_SKIP;
  295. a->bounds.top = MT9V022_ROW_SKIP;
  296. a->bounds.width = MT9V022_MAX_WIDTH;
  297. a->bounds.height = MT9V022_MAX_HEIGHT;
  298. a->defrect = a->bounds;
  299. a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  300. a->pixelaspect.numerator = 1;
  301. a->pixelaspect.denominator = 1;
  302. return 0;
  303. }
  304. static int mt9v022_g_fmt(struct v4l2_subdev *sd,
  305. struct v4l2_mbus_framefmt *mf)
  306. {
  307. struct i2c_client *client = v4l2_get_subdevdata(sd);
  308. struct mt9v022 *mt9v022 = to_mt9v022(client);
  309. mf->width = mt9v022->rect.width;
  310. mf->height = mt9v022->rect.height;
  311. mf->code = mt9v022->fmt->code;
  312. mf->colorspace = mt9v022->fmt->colorspace;
  313. mf->field = V4L2_FIELD_NONE;
  314. return 0;
  315. }
  316. static int mt9v022_s_fmt(struct v4l2_subdev *sd,
  317. struct v4l2_mbus_framefmt *mf)
  318. {
  319. struct i2c_client *client = v4l2_get_subdevdata(sd);
  320. struct mt9v022 *mt9v022 = to_mt9v022(client);
  321. struct v4l2_crop a = {
  322. .c = {
  323. .left = mt9v022->rect.left,
  324. .top = mt9v022->rect.top,
  325. .width = mf->width,
  326. .height = mf->height,
  327. },
  328. };
  329. int ret;
  330. /*
  331. * The caller provides a supported format, as verified per call to
  332. * icd->try_fmt(), datawidth is from our supported format list
  333. */
  334. switch (mf->code) {
  335. case V4L2_MBUS_FMT_Y8_1X8:
  336. case V4L2_MBUS_FMT_Y10_1X10:
  337. if (mt9v022->model != V4L2_IDENT_MT9V022IX7ATM)
  338. return -EINVAL;
  339. break;
  340. case V4L2_MBUS_FMT_SBGGR8_1X8:
  341. case V4L2_MBUS_FMT_SBGGR10_1X10:
  342. if (mt9v022->model != V4L2_IDENT_MT9V022IX7ATC)
  343. return -EINVAL;
  344. break;
  345. default:
  346. return -EINVAL;
  347. }
  348. /* No support for scaling on this camera, just crop. */
  349. ret = mt9v022_s_crop(sd, &a);
  350. if (!ret) {
  351. mf->width = mt9v022->rect.width;
  352. mf->height = mt9v022->rect.height;
  353. mt9v022->fmt = mt9v022_find_datafmt(mf->code,
  354. mt9v022->fmts, mt9v022->num_fmts);
  355. mf->colorspace = mt9v022->fmt->colorspace;
  356. }
  357. return ret;
  358. }
  359. static int mt9v022_try_fmt(struct v4l2_subdev *sd,
  360. struct v4l2_mbus_framefmt *mf)
  361. {
  362. struct i2c_client *client = v4l2_get_subdevdata(sd);
  363. struct mt9v022 *mt9v022 = to_mt9v022(client);
  364. const struct mt9v022_datafmt *fmt;
  365. int align = mf->code == V4L2_MBUS_FMT_SBGGR8_1X8 ||
  366. mf->code == V4L2_MBUS_FMT_SBGGR10_1X10;
  367. v4l_bound_align_image(&mf->width, MT9V022_MIN_WIDTH,
  368. MT9V022_MAX_WIDTH, align,
  369. &mf->height, MT9V022_MIN_HEIGHT + mt9v022->y_skip_top,
  370. MT9V022_MAX_HEIGHT + mt9v022->y_skip_top, align, 0);
  371. fmt = mt9v022_find_datafmt(mf->code, mt9v022->fmts,
  372. mt9v022->num_fmts);
  373. if (!fmt) {
  374. fmt = mt9v022->fmt;
  375. mf->code = fmt->code;
  376. }
  377. mf->colorspace = fmt->colorspace;
  378. return 0;
  379. }
  380. static int mt9v022_g_chip_ident(struct v4l2_subdev *sd,
  381. struct v4l2_dbg_chip_ident *id)
  382. {
  383. struct i2c_client *client = v4l2_get_subdevdata(sd);
  384. struct mt9v022 *mt9v022 = to_mt9v022(client);
  385. if (id->match.type != V4L2_CHIP_MATCH_I2C_ADDR)
  386. return -EINVAL;
  387. if (id->match.addr != client->addr)
  388. return -ENODEV;
  389. id->ident = mt9v022->model;
  390. id->revision = 0;
  391. return 0;
  392. }
  393. #ifdef CONFIG_VIDEO_ADV_DEBUG
  394. static int mt9v022_g_register(struct v4l2_subdev *sd,
  395. struct v4l2_dbg_register *reg)
  396. {
  397. struct i2c_client *client = v4l2_get_subdevdata(sd);
  398. if (reg->match.type != V4L2_CHIP_MATCH_I2C_ADDR || reg->reg > 0xff)
  399. return -EINVAL;
  400. if (reg->match.addr != client->addr)
  401. return -ENODEV;
  402. reg->size = 2;
  403. reg->val = reg_read(client, reg->reg);
  404. if (reg->val > 0xffff)
  405. return -EIO;
  406. return 0;
  407. }
  408. static int mt9v022_s_register(struct v4l2_subdev *sd,
  409. struct v4l2_dbg_register *reg)
  410. {
  411. struct i2c_client *client = v4l2_get_subdevdata(sd);
  412. if (reg->match.type != V4L2_CHIP_MATCH_I2C_ADDR || reg->reg > 0xff)
  413. return -EINVAL;
  414. if (reg->match.addr != client->addr)
  415. return -ENODEV;
  416. if (reg_write(client, reg->reg, reg->val) < 0)
  417. return -EIO;
  418. return 0;
  419. }
  420. #endif
  421. static const struct v4l2_queryctrl mt9v022_controls[] = {
  422. {
  423. .id = V4L2_CID_VFLIP,
  424. .type = V4L2_CTRL_TYPE_BOOLEAN,
  425. .name = "Flip Vertically",
  426. .minimum = 0,
  427. .maximum = 1,
  428. .step = 1,
  429. .default_value = 0,
  430. }, {
  431. .id = V4L2_CID_HFLIP,
  432. .type = V4L2_CTRL_TYPE_BOOLEAN,
  433. .name = "Flip Horizontally",
  434. .minimum = 0,
  435. .maximum = 1,
  436. .step = 1,
  437. .default_value = 0,
  438. }, {
  439. .id = V4L2_CID_GAIN,
  440. .type = V4L2_CTRL_TYPE_INTEGER,
  441. .name = "Analog Gain",
  442. .minimum = 64,
  443. .maximum = 127,
  444. .step = 1,
  445. .default_value = 64,
  446. .flags = V4L2_CTRL_FLAG_SLIDER,
  447. }, {
  448. .id = V4L2_CID_EXPOSURE,
  449. .type = V4L2_CTRL_TYPE_INTEGER,
  450. .name = "Exposure",
  451. .minimum = 1,
  452. .maximum = 255,
  453. .step = 1,
  454. .default_value = 255,
  455. .flags = V4L2_CTRL_FLAG_SLIDER,
  456. }, {
  457. .id = V4L2_CID_AUTOGAIN,
  458. .type = V4L2_CTRL_TYPE_BOOLEAN,
  459. .name = "Automatic Gain",
  460. .minimum = 0,
  461. .maximum = 1,
  462. .step = 1,
  463. .default_value = 1,
  464. }, {
  465. .id = V4L2_CID_EXPOSURE_AUTO,
  466. .type = V4L2_CTRL_TYPE_BOOLEAN,
  467. .name = "Automatic Exposure",
  468. .minimum = 0,
  469. .maximum = 1,
  470. .step = 1,
  471. .default_value = 1,
  472. }
  473. };
  474. static struct soc_camera_ops mt9v022_ops = {
  475. .set_bus_param = mt9v022_set_bus_param,
  476. .query_bus_param = mt9v022_query_bus_param,
  477. .controls = mt9v022_controls,
  478. .num_controls = ARRAY_SIZE(mt9v022_controls),
  479. };
  480. static int mt9v022_g_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
  481. {
  482. struct i2c_client *client = v4l2_get_subdevdata(sd);
  483. const struct v4l2_queryctrl *qctrl;
  484. unsigned long range;
  485. int data;
  486. qctrl = soc_camera_find_qctrl(&mt9v022_ops, ctrl->id);
  487. switch (ctrl->id) {
  488. case V4L2_CID_VFLIP:
  489. data = reg_read(client, MT9V022_READ_MODE);
  490. if (data < 0)
  491. return -EIO;
  492. ctrl->value = !!(data & 0x10);
  493. break;
  494. case V4L2_CID_HFLIP:
  495. data = reg_read(client, MT9V022_READ_MODE);
  496. if (data < 0)
  497. return -EIO;
  498. ctrl->value = !!(data & 0x20);
  499. break;
  500. case V4L2_CID_EXPOSURE_AUTO:
  501. data = reg_read(client, MT9V022_AEC_AGC_ENABLE);
  502. if (data < 0)
  503. return -EIO;
  504. ctrl->value = !!(data & 0x1);
  505. break;
  506. case V4L2_CID_AUTOGAIN:
  507. data = reg_read(client, MT9V022_AEC_AGC_ENABLE);
  508. if (data < 0)
  509. return -EIO;
  510. ctrl->value = !!(data & 0x2);
  511. break;
  512. case V4L2_CID_GAIN:
  513. data = reg_read(client, MT9V022_ANALOG_GAIN);
  514. if (data < 0)
  515. return -EIO;
  516. range = qctrl->maximum - qctrl->minimum;
  517. ctrl->value = ((data - 16) * range + 24) / 48 + qctrl->minimum;
  518. break;
  519. case V4L2_CID_EXPOSURE:
  520. data = reg_read(client, MT9V022_TOTAL_SHUTTER_WIDTH);
  521. if (data < 0)
  522. return -EIO;
  523. range = qctrl->maximum - qctrl->minimum;
  524. ctrl->value = ((data - 1) * range + 239) / 479 + qctrl->minimum;
  525. break;
  526. }
  527. return 0;
  528. }
  529. static int mt9v022_s_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
  530. {
  531. int data;
  532. struct i2c_client *client = v4l2_get_subdevdata(sd);
  533. const struct v4l2_queryctrl *qctrl;
  534. qctrl = soc_camera_find_qctrl(&mt9v022_ops, ctrl->id);
  535. if (!qctrl)
  536. return -EINVAL;
  537. switch (ctrl->id) {
  538. case V4L2_CID_VFLIP:
  539. if (ctrl->value)
  540. data = reg_set(client, MT9V022_READ_MODE, 0x10);
  541. else
  542. data = reg_clear(client, MT9V022_READ_MODE, 0x10);
  543. if (data < 0)
  544. return -EIO;
  545. break;
  546. case V4L2_CID_HFLIP:
  547. if (ctrl->value)
  548. data = reg_set(client, MT9V022_READ_MODE, 0x20);
  549. else
  550. data = reg_clear(client, MT9V022_READ_MODE, 0x20);
  551. if (data < 0)
  552. return -EIO;
  553. break;
  554. case V4L2_CID_GAIN:
  555. /* mt9v022 has minimum == default */
  556. if (ctrl->value > qctrl->maximum || ctrl->value < qctrl->minimum)
  557. return -EINVAL;
  558. else {
  559. unsigned long range = qctrl->maximum - qctrl->minimum;
  560. /* Valid values 16 to 64, 32 to 64 must be even. */
  561. unsigned long gain = ((ctrl->value - qctrl->minimum) *
  562. 48 + range / 2) / range + 16;
  563. if (gain >= 32)
  564. gain &= ~1;
  565. /*
  566. * The user wants to set gain manually, hope, she
  567. * knows, what she's doing... Switch AGC off.
  568. */
  569. if (reg_clear(client, MT9V022_AEC_AGC_ENABLE, 0x2) < 0)
  570. return -EIO;
  571. dev_dbg(&client->dev, "Setting gain from %d to %lu\n",
  572. reg_read(client, MT9V022_ANALOG_GAIN), gain);
  573. if (reg_write(client, MT9V022_ANALOG_GAIN, gain) < 0)
  574. return -EIO;
  575. }
  576. break;
  577. case V4L2_CID_EXPOSURE:
  578. /* mt9v022 has maximum == default */
  579. if (ctrl->value > qctrl->maximum || ctrl->value < qctrl->minimum)
  580. return -EINVAL;
  581. else {
  582. unsigned long range = qctrl->maximum - qctrl->minimum;
  583. unsigned long shutter = ((ctrl->value - qctrl->minimum) *
  584. 479 + range / 2) / range + 1;
  585. /*
  586. * The user wants to set shutter width manually, hope,
  587. * she knows, what she's doing... Switch AEC off.
  588. */
  589. if (reg_clear(client, MT9V022_AEC_AGC_ENABLE, 0x1) < 0)
  590. return -EIO;
  591. dev_dbg(&client->dev, "Shutter width from %d to %lu\n",
  592. reg_read(client, MT9V022_TOTAL_SHUTTER_WIDTH),
  593. shutter);
  594. if (reg_write(client, MT9V022_TOTAL_SHUTTER_WIDTH,
  595. shutter) < 0)
  596. return -EIO;
  597. }
  598. break;
  599. case V4L2_CID_AUTOGAIN:
  600. if (ctrl->value)
  601. data = reg_set(client, MT9V022_AEC_AGC_ENABLE, 0x2);
  602. else
  603. data = reg_clear(client, MT9V022_AEC_AGC_ENABLE, 0x2);
  604. if (data < 0)
  605. return -EIO;
  606. break;
  607. case V4L2_CID_EXPOSURE_AUTO:
  608. if (ctrl->value)
  609. data = reg_set(client, MT9V022_AEC_AGC_ENABLE, 0x1);
  610. else
  611. data = reg_clear(client, MT9V022_AEC_AGC_ENABLE, 0x1);
  612. if (data < 0)
  613. return -EIO;
  614. break;
  615. }
  616. return 0;
  617. }
  618. /*
  619. * Interface active, can use i2c. If it fails, it can indeed mean, that
  620. * this wasn't our capture interface, so, we wait for the right one
  621. */
  622. static int mt9v022_video_probe(struct soc_camera_device *icd,
  623. struct i2c_client *client)
  624. {
  625. struct mt9v022 *mt9v022 = to_mt9v022(client);
  626. struct soc_camera_link *icl = to_soc_camera_link(icd);
  627. s32 data;
  628. int ret;
  629. unsigned long flags;
  630. /* We must have a parent by now. And it cannot be a wrong one. */
  631. BUG_ON(!icd->parent ||
  632. to_soc_camera_host(icd->parent)->nr != icd->iface);
  633. /* Read out the chip version register */
  634. data = reg_read(client, MT9V022_CHIP_VERSION);
  635. /* must be 0x1311 or 0x1313 */
  636. if (data != 0x1311 && data != 0x1313) {
  637. ret = -ENODEV;
  638. dev_info(&client->dev, "No MT9V022 found, ID register 0x%x\n",
  639. data);
  640. goto ei2c;
  641. }
  642. /* Soft reset */
  643. ret = reg_write(client, MT9V022_RESET, 1);
  644. if (ret < 0)
  645. goto ei2c;
  646. /* 15 clock cycles */
  647. udelay(200);
  648. if (reg_read(client, MT9V022_RESET)) {
  649. dev_err(&client->dev, "Resetting MT9V022 failed!\n");
  650. if (ret > 0)
  651. ret = -EIO;
  652. goto ei2c;
  653. }
  654. /* Set monochrome or colour sensor type */
  655. if (sensor_type && (!strcmp("colour", sensor_type) ||
  656. !strcmp("color", sensor_type))) {
  657. ret = reg_write(client, MT9V022_PIXEL_OPERATION_MODE, 4 | 0x11);
  658. mt9v022->model = V4L2_IDENT_MT9V022IX7ATC;
  659. mt9v022->fmts = mt9v022_colour_fmts;
  660. } else {
  661. ret = reg_write(client, MT9V022_PIXEL_OPERATION_MODE, 0x11);
  662. mt9v022->model = V4L2_IDENT_MT9V022IX7ATM;
  663. mt9v022->fmts = mt9v022_monochrome_fmts;
  664. }
  665. if (ret < 0)
  666. goto ei2c;
  667. mt9v022->num_fmts = 0;
  668. /*
  669. * This is a 10bit sensor, so by default we only allow 10bit.
  670. * The platform may support different bus widths due to
  671. * different routing of the data lines.
  672. */
  673. if (icl->query_bus_param)
  674. flags = icl->query_bus_param(icl);
  675. else
  676. flags = SOCAM_DATAWIDTH_10;
  677. if (flags & SOCAM_DATAWIDTH_10)
  678. mt9v022->num_fmts++;
  679. else
  680. mt9v022->fmts++;
  681. if (flags & SOCAM_DATAWIDTH_8)
  682. mt9v022->num_fmts++;
  683. mt9v022->fmt = &mt9v022->fmts[0];
  684. dev_info(&client->dev, "Detected a MT9V022 chip ID %x, %s sensor\n",
  685. data, mt9v022->model == V4L2_IDENT_MT9V022IX7ATM ?
  686. "monochrome" : "colour");
  687. ret = mt9v022_init(client);
  688. if (ret < 0)
  689. dev_err(&client->dev, "Failed to initialise the camera\n");
  690. ei2c:
  691. return ret;
  692. }
  693. static void mt9v022_video_remove(struct soc_camera_device *icd)
  694. {
  695. struct soc_camera_link *icl = to_soc_camera_link(icd);
  696. dev_dbg(icd->pdev, "Video removed: %p, %p\n",
  697. icd->parent, icd->vdev);
  698. if (icl->free_bus)
  699. icl->free_bus(icl);
  700. }
  701. static int mt9v022_g_skip_top_lines(struct v4l2_subdev *sd, u32 *lines)
  702. {
  703. struct i2c_client *client = v4l2_get_subdevdata(sd);
  704. struct mt9v022 *mt9v022 = to_mt9v022(client);
  705. *lines = mt9v022->y_skip_top;
  706. return 0;
  707. }
  708. static struct v4l2_subdev_core_ops mt9v022_subdev_core_ops = {
  709. .g_ctrl = mt9v022_g_ctrl,
  710. .s_ctrl = mt9v022_s_ctrl,
  711. .g_chip_ident = mt9v022_g_chip_ident,
  712. #ifdef CONFIG_VIDEO_ADV_DEBUG
  713. .g_register = mt9v022_g_register,
  714. .s_register = mt9v022_s_register,
  715. #endif
  716. };
  717. static int mt9v022_enum_fmt(struct v4l2_subdev *sd, unsigned int index,
  718. enum v4l2_mbus_pixelcode *code)
  719. {
  720. struct i2c_client *client = v4l2_get_subdevdata(sd);
  721. struct mt9v022 *mt9v022 = to_mt9v022(client);
  722. if (index >= mt9v022->num_fmts)
  723. return -EINVAL;
  724. *code = mt9v022->fmts[index].code;
  725. return 0;
  726. }
  727. static int mt9v022_g_mbus_config(struct v4l2_subdev *sd,
  728. struct v4l2_mbus_config *cfg)
  729. {
  730. struct i2c_client *client = v4l2_get_subdevdata(sd);
  731. struct soc_camera_device *icd = client->dev.platform_data;
  732. struct soc_camera_link *icl = to_soc_camera_link(icd);
  733. cfg->flags = V4L2_MBUS_MASTER | V4L2_MBUS_SLAVE |
  734. V4L2_MBUS_PCLK_SAMPLE_RISING | V4L2_MBUS_PCLK_SAMPLE_FALLING |
  735. V4L2_MBUS_HSYNC_ACTIVE_HIGH | V4L2_MBUS_HSYNC_ACTIVE_LOW |
  736. V4L2_MBUS_VSYNC_ACTIVE_HIGH | V4L2_MBUS_VSYNC_ACTIVE_LOW |
  737. V4L2_MBUS_DATA_ACTIVE_HIGH;
  738. cfg->type = V4L2_MBUS_PARALLEL;
  739. cfg->flags = soc_camera_apply_board_flags(icl, cfg);
  740. return 0;
  741. }
  742. static int mt9v022_s_mbus_config(struct v4l2_subdev *sd,
  743. const struct v4l2_mbus_config *cfg)
  744. {
  745. struct i2c_client *client = v4l2_get_subdevdata(sd);
  746. struct soc_camera_device *icd = client->dev.platform_data;
  747. struct soc_camera_link *icl = to_soc_camera_link(icd);
  748. struct mt9v022 *mt9v022 = to_mt9v022(client);
  749. unsigned long flags = soc_camera_apply_board_flags(icl, cfg);
  750. /*
  751. * Cannot use icd->current_fmt->host_fmt->bits_per_sample, because that
  752. * is the number of bits, that the host has to sample, not the number of
  753. * bits, that we have to send. See mx3_camera.c for an example of 10-bit
  754. * formats being truncated to 8 bits by the host.
  755. */
  756. unsigned int bps = soc_mbus_get_fmtdesc(icd->current_fmt->code)->bits_per_sample;
  757. int ret;
  758. u16 pixclk = 0;
  759. dev_info(icd->pdev, "set %d: %s, %dbps\n", icd->current_fmt->code,
  760. icd->current_fmt->host_fmt->name, bps);
  761. if (icl->set_bus_param) {
  762. ret = icl->set_bus_param(icl, 1 << (bps - 1));
  763. if (ret)
  764. return ret;
  765. } else if (bps != 10) {
  766. /*
  767. * Without board specific bus width settings we only support the
  768. * sensors native bus width
  769. */
  770. return -EINVAL;
  771. }
  772. if (flags & V4L2_MBUS_PCLK_SAMPLE_FALLING)
  773. pixclk |= 0x10;
  774. if (!(flags & V4L2_MBUS_HSYNC_ACTIVE_HIGH))
  775. pixclk |= 0x1;
  776. if (!(flags & V4L2_MBUS_VSYNC_ACTIVE_HIGH))
  777. pixclk |= 0x2;
  778. ret = reg_write(client, MT9V022_PIXCLK_FV_LV, pixclk);
  779. if (ret < 0)
  780. return ret;
  781. if (!(flags & V4L2_MBUS_MASTER))
  782. mt9v022->chip_control &= ~0x8;
  783. ret = reg_write(client, MT9V022_CHIP_CONTROL, mt9v022->chip_control);
  784. if (ret < 0)
  785. return ret;
  786. dev_dbg(&client->dev, "Calculated pixclk 0x%x, chip control 0x%x\n",
  787. pixclk, mt9v022->chip_control);
  788. return 0;
  789. }
  790. static struct v4l2_subdev_video_ops mt9v022_subdev_video_ops = {
  791. .s_stream = mt9v022_s_stream,
  792. .s_mbus_fmt = mt9v022_s_fmt,
  793. .g_mbus_fmt = mt9v022_g_fmt,
  794. .try_mbus_fmt = mt9v022_try_fmt,
  795. .s_crop = mt9v022_s_crop,
  796. .g_crop = mt9v022_g_crop,
  797. .cropcap = mt9v022_cropcap,
  798. .enum_mbus_fmt = mt9v022_enum_fmt,
  799. .g_mbus_config = mt9v022_g_mbus_config,
  800. .s_mbus_config = mt9v022_s_mbus_config,
  801. };
  802. static struct v4l2_subdev_sensor_ops mt9v022_subdev_sensor_ops = {
  803. .g_skip_top_lines = mt9v022_g_skip_top_lines,
  804. };
  805. static struct v4l2_subdev_ops mt9v022_subdev_ops = {
  806. .core = &mt9v022_subdev_core_ops,
  807. .video = &mt9v022_subdev_video_ops,
  808. .sensor = &mt9v022_subdev_sensor_ops,
  809. };
  810. static int mt9v022_probe(struct i2c_client *client,
  811. const struct i2c_device_id *did)
  812. {
  813. struct mt9v022 *mt9v022;
  814. struct soc_camera_device *icd = client->dev.platform_data;
  815. struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
  816. struct soc_camera_link *icl;
  817. int ret;
  818. if (!icd) {
  819. dev_err(&client->dev, "MT9V022: missing soc-camera data!\n");
  820. return -EINVAL;
  821. }
  822. icl = to_soc_camera_link(icd);
  823. if (!icl) {
  824. dev_err(&client->dev, "MT9V022 driver needs platform data\n");
  825. return -EINVAL;
  826. }
  827. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WORD_DATA)) {
  828. dev_warn(&adapter->dev,
  829. "I2C-Adapter doesn't support I2C_FUNC_SMBUS_WORD\n");
  830. return -EIO;
  831. }
  832. mt9v022 = kzalloc(sizeof(struct mt9v022), GFP_KERNEL);
  833. if (!mt9v022)
  834. return -ENOMEM;
  835. v4l2_i2c_subdev_init(&mt9v022->subdev, client, &mt9v022_subdev_ops);
  836. mt9v022->chip_control = MT9V022_CHIP_CONTROL_DEFAULT;
  837. icd->ops = &mt9v022_ops;
  838. /*
  839. * MT9V022 _really_ corrupts the first read out line.
  840. * TODO: verify on i.MX31
  841. */
  842. mt9v022->y_skip_top = 1;
  843. mt9v022->rect.left = MT9V022_COLUMN_SKIP;
  844. mt9v022->rect.top = MT9V022_ROW_SKIP;
  845. mt9v022->rect.width = MT9V022_MAX_WIDTH;
  846. mt9v022->rect.height = MT9V022_MAX_HEIGHT;
  847. ret = mt9v022_video_probe(icd, client);
  848. if (ret) {
  849. icd->ops = NULL;
  850. kfree(mt9v022);
  851. }
  852. return ret;
  853. }
  854. static int mt9v022_remove(struct i2c_client *client)
  855. {
  856. struct mt9v022 *mt9v022 = to_mt9v022(client);
  857. struct soc_camera_device *icd = client->dev.platform_data;
  858. icd->ops = NULL;
  859. mt9v022_video_remove(icd);
  860. kfree(mt9v022);
  861. return 0;
  862. }
  863. static const struct i2c_device_id mt9v022_id[] = {
  864. { "mt9v022", 0 },
  865. { }
  866. };
  867. MODULE_DEVICE_TABLE(i2c, mt9v022_id);
  868. static struct i2c_driver mt9v022_i2c_driver = {
  869. .driver = {
  870. .name = "mt9v022",
  871. },
  872. .probe = mt9v022_probe,
  873. .remove = mt9v022_remove,
  874. .id_table = mt9v022_id,
  875. };
  876. static int __init mt9v022_mod_init(void)
  877. {
  878. return i2c_add_driver(&mt9v022_i2c_driver);
  879. }
  880. static void __exit mt9v022_mod_exit(void)
  881. {
  882. i2c_del_driver(&mt9v022_i2c_driver);
  883. }
  884. module_init(mt9v022_mod_init);
  885. module_exit(mt9v022_mod_exit);
  886. MODULE_DESCRIPTION("Micron MT9V022 Camera driver");
  887. MODULE_AUTHOR("Guennadi Liakhovetski <kernel@pengutronix.de>");
  888. MODULE_LICENSE("GPL");