mt9v022.c 26 KB

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