mt9v022.c 26 KB

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