mt9v022.c 23 KB

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