mt9v022.c 23 KB

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