mt9v022.c 25 KB

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