mt9v022.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989
  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 ret;
  245. /* Bayer format - even size lengths */
  246. if (mt9v022->fmts == mt9v022_colour_fmts) {
  247. rect.width = ALIGN(rect.width, 2);
  248. rect.height = ALIGN(rect.height, 2);
  249. /* Let the user play with the starting pixel */
  250. }
  251. soc_camera_limit_side(&rect.left, &rect.width,
  252. MT9V022_COLUMN_SKIP, MT9V022_MIN_WIDTH, MT9V022_MAX_WIDTH);
  253. soc_camera_limit_side(&rect.top, &rect.height,
  254. MT9V022_ROW_SKIP, MT9V022_MIN_HEIGHT, MT9V022_MAX_HEIGHT);
  255. /* Like in example app. Contradicts the datasheet though */
  256. ret = reg_read(client, MT9V022_AEC_AGC_ENABLE);
  257. if (ret >= 0) {
  258. if (ret & 1) /* Autoexposure */
  259. ret = reg_write(client, mt9v022->reg->max_total_shutter_width,
  260. rect.height + mt9v022->y_skip_top + 43);
  261. /*
  262. * If autoexposure is off, there is no need to set
  263. * MT9V022_TOTAL_SHUTTER_WIDTH here. Autoexposure can be off
  264. * only if the user has set exposure manually, using the
  265. * V4L2_CID_EXPOSURE_AUTO with the value V4L2_EXPOSURE_MANUAL.
  266. * In this case the register MT9V022_TOTAL_SHUTTER_WIDTH
  267. * already contains the correct value.
  268. */
  269. }
  270. /* Setup frame format: defaults apart from width and height */
  271. if (!ret)
  272. ret = reg_write(client, MT9V022_COLUMN_START, rect.left);
  273. if (!ret)
  274. ret = reg_write(client, MT9V022_ROW_START, rect.top);
  275. if (!ret)
  276. /*
  277. * Default 94, Phytec driver says:
  278. * "width + horizontal blank >= 660"
  279. */
  280. ret = v4l2_ctrl_s_ctrl(mt9v022->hblank,
  281. rect.width > 660 - 43 ? 43 : 660 - rect.width);
  282. if (!ret)
  283. ret = v4l2_ctrl_s_ctrl(mt9v022->vblank, 45);
  284. if (!ret)
  285. ret = reg_write(client, MT9V022_WINDOW_WIDTH, rect.width);
  286. if (!ret)
  287. ret = reg_write(client, MT9V022_WINDOW_HEIGHT,
  288. rect.height + mt9v022->y_skip_top);
  289. if (ret < 0)
  290. return ret;
  291. dev_dbg(&client->dev, "Frame %dx%d pixel\n", rect.width, rect.height);
  292. mt9v022->rect = rect;
  293. return 0;
  294. }
  295. static int mt9v022_g_crop(struct v4l2_subdev *sd, struct v4l2_crop *a)
  296. {
  297. struct i2c_client *client = v4l2_get_subdevdata(sd);
  298. struct mt9v022 *mt9v022 = to_mt9v022(client);
  299. a->c = mt9v022->rect;
  300. a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  301. return 0;
  302. }
  303. static int mt9v022_cropcap(struct v4l2_subdev *sd, struct v4l2_cropcap *a)
  304. {
  305. a->bounds.left = MT9V022_COLUMN_SKIP;
  306. a->bounds.top = MT9V022_ROW_SKIP;
  307. a->bounds.width = MT9V022_MAX_WIDTH;
  308. a->bounds.height = MT9V022_MAX_HEIGHT;
  309. a->defrect = a->bounds;
  310. a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  311. a->pixelaspect.numerator = 1;
  312. a->pixelaspect.denominator = 1;
  313. return 0;
  314. }
  315. static int mt9v022_g_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. mf->width = mt9v022->rect.width;
  321. mf->height = mt9v022->rect.height;
  322. mf->code = mt9v022->fmt->code;
  323. mf->colorspace = mt9v022->fmt->colorspace;
  324. mf->field = V4L2_FIELD_NONE;
  325. return 0;
  326. }
  327. static int mt9v022_s_fmt(struct v4l2_subdev *sd,
  328. struct v4l2_mbus_framefmt *mf)
  329. {
  330. struct i2c_client *client = v4l2_get_subdevdata(sd);
  331. struct mt9v022 *mt9v022 = to_mt9v022(client);
  332. struct v4l2_crop a = {
  333. .c = {
  334. .left = mt9v022->rect.left,
  335. .top = mt9v022->rect.top,
  336. .width = mf->width,
  337. .height = mf->height,
  338. },
  339. };
  340. int ret;
  341. /*
  342. * The caller provides a supported format, as verified per call to
  343. * .try_mbus_fmt(), datawidth is from our supported format list
  344. */
  345. switch (mf->code) {
  346. case V4L2_MBUS_FMT_Y8_1X8:
  347. case V4L2_MBUS_FMT_Y10_1X10:
  348. if (mt9v022->model != V4L2_IDENT_MT9V022IX7ATM)
  349. return -EINVAL;
  350. break;
  351. case V4L2_MBUS_FMT_SBGGR8_1X8:
  352. case V4L2_MBUS_FMT_SBGGR10_1X10:
  353. if (mt9v022->model != V4L2_IDENT_MT9V022IX7ATC)
  354. return -EINVAL;
  355. break;
  356. default:
  357. return -EINVAL;
  358. }
  359. /* No support for scaling on this camera, just crop. */
  360. ret = mt9v022_s_crop(sd, &a);
  361. if (!ret) {
  362. mf->width = mt9v022->rect.width;
  363. mf->height = mt9v022->rect.height;
  364. mt9v022->fmt = mt9v022_find_datafmt(mf->code,
  365. mt9v022->fmts, mt9v022->num_fmts);
  366. mf->colorspace = mt9v022->fmt->colorspace;
  367. }
  368. return ret;
  369. }
  370. static int mt9v022_try_fmt(struct v4l2_subdev *sd,
  371. struct v4l2_mbus_framefmt *mf)
  372. {
  373. struct i2c_client *client = v4l2_get_subdevdata(sd);
  374. struct mt9v022 *mt9v022 = to_mt9v022(client);
  375. const struct mt9v022_datafmt *fmt;
  376. int align = mf->code == V4L2_MBUS_FMT_SBGGR8_1X8 ||
  377. mf->code == V4L2_MBUS_FMT_SBGGR10_1X10;
  378. v4l_bound_align_image(&mf->width, MT9V022_MIN_WIDTH,
  379. MT9V022_MAX_WIDTH, align,
  380. &mf->height, MT9V022_MIN_HEIGHT + mt9v022->y_skip_top,
  381. MT9V022_MAX_HEIGHT + mt9v022->y_skip_top, align, 0);
  382. fmt = mt9v022_find_datafmt(mf->code, mt9v022->fmts,
  383. mt9v022->num_fmts);
  384. if (!fmt) {
  385. fmt = mt9v022->fmt;
  386. mf->code = fmt->code;
  387. }
  388. mf->colorspace = fmt->colorspace;
  389. return 0;
  390. }
  391. static int mt9v022_g_chip_ident(struct v4l2_subdev *sd,
  392. struct v4l2_dbg_chip_ident *id)
  393. {
  394. struct i2c_client *client = v4l2_get_subdevdata(sd);
  395. struct mt9v022 *mt9v022 = to_mt9v022(client);
  396. if (id->match.type != V4L2_CHIP_MATCH_I2C_ADDR)
  397. return -EINVAL;
  398. if (id->match.addr != client->addr)
  399. return -ENODEV;
  400. id->ident = mt9v022->model;
  401. id->revision = 0;
  402. return 0;
  403. }
  404. #ifdef CONFIG_VIDEO_ADV_DEBUG
  405. static int mt9v022_g_register(struct v4l2_subdev *sd,
  406. struct v4l2_dbg_register *reg)
  407. {
  408. struct i2c_client *client = v4l2_get_subdevdata(sd);
  409. if (reg->match.type != V4L2_CHIP_MATCH_I2C_ADDR || reg->reg > 0xff)
  410. return -EINVAL;
  411. if (reg->match.addr != client->addr)
  412. return -ENODEV;
  413. reg->size = 2;
  414. reg->val = reg_read(client, reg->reg);
  415. if (reg->val > 0xffff)
  416. return -EIO;
  417. return 0;
  418. }
  419. static int mt9v022_s_register(struct v4l2_subdev *sd,
  420. struct v4l2_dbg_register *reg)
  421. {
  422. struct i2c_client *client = v4l2_get_subdevdata(sd);
  423. if (reg->match.type != V4L2_CHIP_MATCH_I2C_ADDR || reg->reg > 0xff)
  424. return -EINVAL;
  425. if (reg->match.addr != client->addr)
  426. return -ENODEV;
  427. if (reg_write(client, reg->reg, reg->val) < 0)
  428. return -EIO;
  429. return 0;
  430. }
  431. #endif
  432. static int mt9v022_s_power(struct v4l2_subdev *sd, int on)
  433. {
  434. struct i2c_client *client = v4l2_get_subdevdata(sd);
  435. struct soc_camera_subdev_desc *ssdd = soc_camera_i2c_to_desc(client);
  436. return soc_camera_set_power(&client->dev, ssdd, on);
  437. }
  438. static int mt9v022_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
  439. {
  440. struct mt9v022 *mt9v022 = container_of(ctrl->handler,
  441. struct mt9v022, hdl);
  442. struct v4l2_subdev *sd = &mt9v022->subdev;
  443. struct i2c_client *client = v4l2_get_subdevdata(sd);
  444. struct v4l2_ctrl *gain = mt9v022->gain;
  445. struct v4l2_ctrl *exp = mt9v022->exposure;
  446. unsigned long range;
  447. int data;
  448. switch (ctrl->id) {
  449. case V4L2_CID_AUTOGAIN:
  450. data = reg_read(client, MT9V022_ANALOG_GAIN);
  451. if (data < 0)
  452. return -EIO;
  453. range = gain->maximum - gain->minimum;
  454. gain->val = ((data - 16) * range + 24) / 48 + gain->minimum;
  455. return 0;
  456. case V4L2_CID_EXPOSURE_AUTO:
  457. data = reg_read(client, MT9V022_TOTAL_SHUTTER_WIDTH);
  458. if (data < 0)
  459. return -EIO;
  460. range = exp->maximum - exp->minimum;
  461. exp->val = ((data - 1) * range + 239) / 479 + exp->minimum;
  462. return 0;
  463. case V4L2_CID_HBLANK:
  464. data = reg_read(client, MT9V022_HORIZONTAL_BLANKING);
  465. if (data < 0)
  466. return -EIO;
  467. ctrl->val = data;
  468. return 0;
  469. case V4L2_CID_VBLANK:
  470. data = reg_read(client, MT9V022_VERTICAL_BLANKING);
  471. if (data < 0)
  472. return -EIO;
  473. ctrl->val = data;
  474. return 0;
  475. }
  476. return -EINVAL;
  477. }
  478. static int mt9v022_s_ctrl(struct v4l2_ctrl *ctrl)
  479. {
  480. struct mt9v022 *mt9v022 = container_of(ctrl->handler,
  481. struct mt9v022, hdl);
  482. struct v4l2_subdev *sd = &mt9v022->subdev;
  483. struct i2c_client *client = v4l2_get_subdevdata(sd);
  484. int data;
  485. switch (ctrl->id) {
  486. case V4L2_CID_VFLIP:
  487. if (ctrl->val)
  488. data = reg_set(client, MT9V022_READ_MODE, 0x10);
  489. else
  490. data = reg_clear(client, MT9V022_READ_MODE, 0x10);
  491. if (data < 0)
  492. return -EIO;
  493. return 0;
  494. case V4L2_CID_HFLIP:
  495. if (ctrl->val)
  496. data = reg_set(client, MT9V022_READ_MODE, 0x20);
  497. else
  498. data = reg_clear(client, MT9V022_READ_MODE, 0x20);
  499. if (data < 0)
  500. return -EIO;
  501. return 0;
  502. case V4L2_CID_AUTOGAIN:
  503. if (ctrl->val) {
  504. if (reg_set(client, MT9V022_AEC_AGC_ENABLE, 0x2) < 0)
  505. return -EIO;
  506. } else {
  507. struct v4l2_ctrl *gain = mt9v022->gain;
  508. /* mt9v022 has minimum == default */
  509. unsigned long range = gain->maximum - gain->minimum;
  510. /* Valid values 16 to 64, 32 to 64 must be even. */
  511. unsigned long gain_val = ((gain->val - gain->minimum) *
  512. 48 + range / 2) / range + 16;
  513. if (gain_val >= 32)
  514. gain_val &= ~1;
  515. /*
  516. * The user wants to set gain manually, hope, she
  517. * knows, what she's doing... Switch AGC off.
  518. */
  519. if (reg_clear(client, MT9V022_AEC_AGC_ENABLE, 0x2) < 0)
  520. return -EIO;
  521. dev_dbg(&client->dev, "Setting gain from %d to %lu\n",
  522. reg_read(client, MT9V022_ANALOG_GAIN), gain_val);
  523. if (reg_write(client, MT9V022_ANALOG_GAIN, gain_val) < 0)
  524. return -EIO;
  525. }
  526. return 0;
  527. case V4L2_CID_EXPOSURE_AUTO:
  528. if (ctrl->val == V4L2_EXPOSURE_AUTO) {
  529. data = reg_set(client, MT9V022_AEC_AGC_ENABLE, 0x1);
  530. } else {
  531. struct v4l2_ctrl *exp = mt9v022->exposure;
  532. unsigned long range = exp->maximum - exp->minimum;
  533. unsigned long shutter = ((exp->val - exp->minimum) *
  534. 479 + range / 2) / range + 1;
  535. /*
  536. * The user wants to set shutter width manually, hope,
  537. * she knows, what she's doing... Switch AEC off.
  538. */
  539. data = reg_clear(client, MT9V022_AEC_AGC_ENABLE, 0x1);
  540. if (data < 0)
  541. return -EIO;
  542. dev_dbg(&client->dev, "Shutter width from %d to %lu\n",
  543. reg_read(client, MT9V022_TOTAL_SHUTTER_WIDTH),
  544. shutter);
  545. if (reg_write(client, MT9V022_TOTAL_SHUTTER_WIDTH,
  546. shutter) < 0)
  547. return -EIO;
  548. }
  549. return 0;
  550. case V4L2_CID_HBLANK:
  551. if (reg_write(client, MT9V022_HORIZONTAL_BLANKING,
  552. ctrl->val) < 0)
  553. return -EIO;
  554. return 0;
  555. case V4L2_CID_VBLANK:
  556. if (reg_write(client, MT9V022_VERTICAL_BLANKING,
  557. ctrl->val) < 0)
  558. return -EIO;
  559. return 0;
  560. }
  561. return -EINVAL;
  562. }
  563. /*
  564. * Interface active, can use i2c. If it fails, it can indeed mean, that
  565. * this wasn't our capture interface, so, we wait for the right one
  566. */
  567. static int mt9v022_video_probe(struct i2c_client *client)
  568. {
  569. struct mt9v022 *mt9v022 = to_mt9v022(client);
  570. struct soc_camera_subdev_desc *ssdd = soc_camera_i2c_to_desc(client);
  571. s32 data;
  572. int ret;
  573. unsigned long flags;
  574. ret = mt9v022_s_power(&mt9v022->subdev, 1);
  575. if (ret < 0)
  576. return ret;
  577. /* Read out the chip version register */
  578. data = reg_read(client, MT9V022_CHIP_VERSION);
  579. /* must be 0x1311, 0x1313 or 0x1324 */
  580. if (data != 0x1311 && data != 0x1313 && data != 0x1324) {
  581. ret = -ENODEV;
  582. dev_info(&client->dev, "No MT9V022 found, ID register 0x%x\n",
  583. data);
  584. goto ei2c;
  585. }
  586. mt9v022->chip_version = data;
  587. mt9v022->reg = is_mt9v024(data) ? &mt9v024_register :
  588. &mt9v022_register;
  589. /* Soft reset */
  590. ret = reg_write(client, MT9V022_RESET, 1);
  591. if (ret < 0)
  592. goto ei2c;
  593. /* 15 clock cycles */
  594. udelay(200);
  595. if (reg_read(client, MT9V022_RESET)) {
  596. dev_err(&client->dev, "Resetting MT9V022 failed!\n");
  597. if (ret > 0)
  598. ret = -EIO;
  599. goto ei2c;
  600. }
  601. /* Set monochrome or colour sensor type */
  602. if (sensor_type && (!strcmp("colour", sensor_type) ||
  603. !strcmp("color", sensor_type))) {
  604. ret = reg_write(client, MT9V022_PIXEL_OPERATION_MODE, 4 | 0x11);
  605. mt9v022->model = V4L2_IDENT_MT9V022IX7ATC;
  606. mt9v022->fmts = mt9v022_colour_fmts;
  607. } else {
  608. ret = reg_write(client, MT9V022_PIXEL_OPERATION_MODE, 0x11);
  609. mt9v022->model = V4L2_IDENT_MT9V022IX7ATM;
  610. mt9v022->fmts = mt9v022_monochrome_fmts;
  611. }
  612. if (ret < 0)
  613. goto ei2c;
  614. mt9v022->num_fmts = 0;
  615. /*
  616. * This is a 10bit sensor, so by default we only allow 10bit.
  617. * The platform may support different bus widths due to
  618. * different routing of the data lines.
  619. */
  620. if (ssdd->query_bus_param)
  621. flags = ssdd->query_bus_param(ssdd);
  622. else
  623. flags = SOCAM_DATAWIDTH_10;
  624. if (flags & SOCAM_DATAWIDTH_10)
  625. mt9v022->num_fmts++;
  626. else
  627. mt9v022->fmts++;
  628. if (flags & SOCAM_DATAWIDTH_8)
  629. mt9v022->num_fmts++;
  630. mt9v022->fmt = &mt9v022->fmts[0];
  631. dev_info(&client->dev, "Detected a MT9V022 chip ID %x, %s sensor\n",
  632. data, mt9v022->model == V4L2_IDENT_MT9V022IX7ATM ?
  633. "monochrome" : "colour");
  634. ret = mt9v022_init(client);
  635. if (ret < 0)
  636. dev_err(&client->dev, "Failed to initialise the camera\n");
  637. ei2c:
  638. mt9v022_s_power(&mt9v022->subdev, 0);
  639. return ret;
  640. }
  641. static int mt9v022_g_skip_top_lines(struct v4l2_subdev *sd, u32 *lines)
  642. {
  643. struct i2c_client *client = v4l2_get_subdevdata(sd);
  644. struct mt9v022 *mt9v022 = to_mt9v022(client);
  645. *lines = mt9v022->y_skip_top;
  646. return 0;
  647. }
  648. static const struct v4l2_ctrl_ops mt9v022_ctrl_ops = {
  649. .g_volatile_ctrl = mt9v022_g_volatile_ctrl,
  650. .s_ctrl = mt9v022_s_ctrl,
  651. };
  652. static struct v4l2_subdev_core_ops mt9v022_subdev_core_ops = {
  653. .g_chip_ident = mt9v022_g_chip_ident,
  654. #ifdef CONFIG_VIDEO_ADV_DEBUG
  655. .g_register = mt9v022_g_register,
  656. .s_register = mt9v022_s_register,
  657. #endif
  658. .s_power = mt9v022_s_power,
  659. };
  660. static int mt9v022_enum_fmt(struct v4l2_subdev *sd, unsigned int index,
  661. enum v4l2_mbus_pixelcode *code)
  662. {
  663. struct i2c_client *client = v4l2_get_subdevdata(sd);
  664. struct mt9v022 *mt9v022 = to_mt9v022(client);
  665. if (index >= mt9v022->num_fmts)
  666. return -EINVAL;
  667. *code = mt9v022->fmts[index].code;
  668. return 0;
  669. }
  670. static int mt9v022_g_mbus_config(struct v4l2_subdev *sd,
  671. struct v4l2_mbus_config *cfg)
  672. {
  673. struct i2c_client *client = v4l2_get_subdevdata(sd);
  674. struct soc_camera_subdev_desc *ssdd = soc_camera_i2c_to_desc(client);
  675. cfg->flags = V4L2_MBUS_MASTER | V4L2_MBUS_SLAVE |
  676. V4L2_MBUS_PCLK_SAMPLE_RISING | V4L2_MBUS_PCLK_SAMPLE_FALLING |
  677. V4L2_MBUS_HSYNC_ACTIVE_HIGH | V4L2_MBUS_HSYNC_ACTIVE_LOW |
  678. V4L2_MBUS_VSYNC_ACTIVE_HIGH | V4L2_MBUS_VSYNC_ACTIVE_LOW |
  679. V4L2_MBUS_DATA_ACTIVE_HIGH;
  680. cfg->type = V4L2_MBUS_PARALLEL;
  681. cfg->flags = soc_camera_apply_board_flags(ssdd, cfg);
  682. return 0;
  683. }
  684. static int mt9v022_s_mbus_config(struct v4l2_subdev *sd,
  685. const struct v4l2_mbus_config *cfg)
  686. {
  687. struct i2c_client *client = v4l2_get_subdevdata(sd);
  688. struct soc_camera_subdev_desc *ssdd = soc_camera_i2c_to_desc(client);
  689. struct mt9v022 *mt9v022 = to_mt9v022(client);
  690. unsigned long flags = soc_camera_apply_board_flags(ssdd, cfg);
  691. unsigned int bps = soc_mbus_get_fmtdesc(mt9v022->fmt->code)->bits_per_sample;
  692. int ret;
  693. u16 pixclk = 0;
  694. if (ssdd->set_bus_param) {
  695. ret = ssdd->set_bus_param(ssdd, 1 << (bps - 1));
  696. if (ret)
  697. return ret;
  698. } else if (bps != 10) {
  699. /*
  700. * Without board specific bus width settings we only support the
  701. * sensors native bus width
  702. */
  703. return -EINVAL;
  704. }
  705. if (flags & V4L2_MBUS_PCLK_SAMPLE_FALLING)
  706. pixclk |= 0x10;
  707. if (!(flags & V4L2_MBUS_HSYNC_ACTIVE_HIGH))
  708. pixclk |= 0x1;
  709. if (!(flags & V4L2_MBUS_VSYNC_ACTIVE_HIGH))
  710. pixclk |= 0x2;
  711. ret = reg_write(client, mt9v022->reg->pixclk_fv_lv, pixclk);
  712. if (ret < 0)
  713. return ret;
  714. if (!(flags & V4L2_MBUS_MASTER))
  715. mt9v022->chip_control &= ~0x8;
  716. ret = reg_write(client, MT9V022_CHIP_CONTROL, mt9v022->chip_control);
  717. if (ret < 0)
  718. return ret;
  719. dev_dbg(&client->dev, "Calculated pixclk 0x%x, chip control 0x%x\n",
  720. pixclk, mt9v022->chip_control);
  721. return 0;
  722. }
  723. static struct v4l2_subdev_video_ops mt9v022_subdev_video_ops = {
  724. .s_stream = mt9v022_s_stream,
  725. .s_mbus_fmt = mt9v022_s_fmt,
  726. .g_mbus_fmt = mt9v022_g_fmt,
  727. .try_mbus_fmt = mt9v022_try_fmt,
  728. .s_crop = mt9v022_s_crop,
  729. .g_crop = mt9v022_g_crop,
  730. .cropcap = mt9v022_cropcap,
  731. .enum_mbus_fmt = mt9v022_enum_fmt,
  732. .g_mbus_config = mt9v022_g_mbus_config,
  733. .s_mbus_config = mt9v022_s_mbus_config,
  734. };
  735. static struct v4l2_subdev_sensor_ops mt9v022_subdev_sensor_ops = {
  736. .g_skip_top_lines = mt9v022_g_skip_top_lines,
  737. };
  738. static struct v4l2_subdev_ops mt9v022_subdev_ops = {
  739. .core = &mt9v022_subdev_core_ops,
  740. .video = &mt9v022_subdev_video_ops,
  741. .sensor = &mt9v022_subdev_sensor_ops,
  742. };
  743. static int mt9v022_probe(struct i2c_client *client,
  744. const struct i2c_device_id *did)
  745. {
  746. struct mt9v022 *mt9v022;
  747. struct soc_camera_subdev_desc *ssdd = soc_camera_i2c_to_desc(client);
  748. struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
  749. struct mt9v022_platform_data *pdata;
  750. int ret;
  751. if (!ssdd) {
  752. dev_err(&client->dev, "MT9V022 driver needs platform data\n");
  753. return -EINVAL;
  754. }
  755. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WORD_DATA)) {
  756. dev_warn(&adapter->dev,
  757. "I2C-Adapter doesn't support I2C_FUNC_SMBUS_WORD\n");
  758. return -EIO;
  759. }
  760. mt9v022 = devm_kzalloc(&client->dev, sizeof(struct mt9v022), GFP_KERNEL);
  761. if (!mt9v022)
  762. return -ENOMEM;
  763. pdata = ssdd->drv_priv;
  764. v4l2_i2c_subdev_init(&mt9v022->subdev, client, &mt9v022_subdev_ops);
  765. v4l2_ctrl_handler_init(&mt9v022->hdl, 6);
  766. v4l2_ctrl_new_std(&mt9v022->hdl, &mt9v022_ctrl_ops,
  767. V4L2_CID_VFLIP, 0, 1, 1, 0);
  768. v4l2_ctrl_new_std(&mt9v022->hdl, &mt9v022_ctrl_ops,
  769. V4L2_CID_HFLIP, 0, 1, 1, 0);
  770. mt9v022->autogain = v4l2_ctrl_new_std(&mt9v022->hdl, &mt9v022_ctrl_ops,
  771. V4L2_CID_AUTOGAIN, 0, 1, 1, 1);
  772. mt9v022->gain = v4l2_ctrl_new_std(&mt9v022->hdl, &mt9v022_ctrl_ops,
  773. V4L2_CID_GAIN, 0, 127, 1, 64);
  774. /*
  775. * Simulated autoexposure. If enabled, we calculate shutter width
  776. * ourselves in the driver based on vertical blanking and frame width
  777. */
  778. mt9v022->autoexposure = v4l2_ctrl_new_std_menu(&mt9v022->hdl,
  779. &mt9v022_ctrl_ops, V4L2_CID_EXPOSURE_AUTO, 1, 0,
  780. V4L2_EXPOSURE_AUTO);
  781. mt9v022->exposure = v4l2_ctrl_new_std(&mt9v022->hdl, &mt9v022_ctrl_ops,
  782. V4L2_CID_EXPOSURE, 1, 255, 1, 255);
  783. mt9v022->hblank = v4l2_ctrl_new_std(&mt9v022->hdl, &mt9v022_ctrl_ops,
  784. V4L2_CID_HBLANK, MT9V022_HORIZONTAL_BLANKING_MIN,
  785. MT9V022_HORIZONTAL_BLANKING_MAX, 1,
  786. MT9V022_HORIZONTAL_BLANKING_DEF);
  787. mt9v022->vblank = v4l2_ctrl_new_std(&mt9v022->hdl, &mt9v022_ctrl_ops,
  788. V4L2_CID_VBLANK, MT9V022_VERTICAL_BLANKING_MIN,
  789. MT9V022_VERTICAL_BLANKING_MAX, 1,
  790. MT9V022_VERTICAL_BLANKING_DEF);
  791. mt9v022->subdev.ctrl_handler = &mt9v022->hdl;
  792. if (mt9v022->hdl.error) {
  793. int err = mt9v022->hdl.error;
  794. dev_err(&client->dev, "control initialisation err %d\n", err);
  795. return err;
  796. }
  797. v4l2_ctrl_auto_cluster(2, &mt9v022->autoexposure,
  798. V4L2_EXPOSURE_MANUAL, true);
  799. v4l2_ctrl_auto_cluster(2, &mt9v022->autogain, 0, true);
  800. mt9v022->chip_control = MT9V022_CHIP_CONTROL_DEFAULT;
  801. /*
  802. * On some platforms the first read out line is corrupted.
  803. * Workaround it by skipping if indicated by platform data.
  804. */
  805. mt9v022->y_skip_top = pdata ? pdata->y_skip_top : 0;
  806. mt9v022->rect.left = MT9V022_COLUMN_SKIP;
  807. mt9v022->rect.top = MT9V022_ROW_SKIP;
  808. mt9v022->rect.width = MT9V022_MAX_WIDTH;
  809. mt9v022->rect.height = MT9V022_MAX_HEIGHT;
  810. ret = mt9v022_video_probe(client);
  811. if (ret)
  812. v4l2_ctrl_handler_free(&mt9v022->hdl);
  813. return ret;
  814. }
  815. static int mt9v022_remove(struct i2c_client *client)
  816. {
  817. struct mt9v022 *mt9v022 = to_mt9v022(client);
  818. struct soc_camera_subdev_desc *ssdd = soc_camera_i2c_to_desc(client);
  819. v4l2_device_unregister_subdev(&mt9v022->subdev);
  820. if (ssdd->free_bus)
  821. ssdd->free_bus(ssdd);
  822. v4l2_ctrl_handler_free(&mt9v022->hdl);
  823. return 0;
  824. }
  825. static const struct i2c_device_id mt9v022_id[] = {
  826. { "mt9v022", 0 },
  827. { }
  828. };
  829. MODULE_DEVICE_TABLE(i2c, mt9v022_id);
  830. static struct i2c_driver mt9v022_i2c_driver = {
  831. .driver = {
  832. .name = "mt9v022",
  833. },
  834. .probe = mt9v022_probe,
  835. .remove = mt9v022_remove,
  836. .id_table = mt9v022_id,
  837. };
  838. module_i2c_driver(mt9v022_i2c_driver);
  839. MODULE_DESCRIPTION("Micron MT9V022 Camera driver");
  840. MODULE_AUTHOR("Guennadi Liakhovetski <kernel@pengutronix.de>");
  841. MODULE_LICENSE("GPL");