mt9v022.c 24 KB

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