mt9v022.c 23 KB

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