mt9v022.c 27 KB

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