mt9v022.c 24 KB

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