mt9v022.c 24 KB

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