mt9v022.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922
  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. s32 data = i2c_smbus_read_word_data(client, reg);
  118. return data < 0 ? data : swab16(data);
  119. }
  120. static int reg_write(struct i2c_client *client, const u8 reg,
  121. const u16 data)
  122. {
  123. return i2c_smbus_write_word_data(client, reg, swab16(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. * icd->try_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 soc_camera_device *icd,
  486. struct i2c_client *client)
  487. {
  488. struct mt9v022 *mt9v022 = to_mt9v022(client);
  489. struct soc_camera_link *icl = to_soc_camera_link(icd);
  490. s32 data;
  491. int ret;
  492. unsigned long flags;
  493. /* We must have a parent by now. And it cannot be a wrong one. */
  494. BUG_ON(!icd->parent ||
  495. to_soc_camera_host(icd->parent)->nr != icd->iface);
  496. /* Read out the chip version register */
  497. data = reg_read(client, MT9V022_CHIP_VERSION);
  498. /* must be 0x1311 or 0x1313 */
  499. if (data != 0x1311 && data != 0x1313) {
  500. ret = -ENODEV;
  501. dev_info(&client->dev, "No MT9V022 found, ID register 0x%x\n",
  502. data);
  503. goto ei2c;
  504. }
  505. /* Soft reset */
  506. ret = reg_write(client, MT9V022_RESET, 1);
  507. if (ret < 0)
  508. goto ei2c;
  509. /* 15 clock cycles */
  510. udelay(200);
  511. if (reg_read(client, MT9V022_RESET)) {
  512. dev_err(&client->dev, "Resetting MT9V022 failed!\n");
  513. if (ret > 0)
  514. ret = -EIO;
  515. goto ei2c;
  516. }
  517. /* Set monochrome or colour sensor type */
  518. if (sensor_type && (!strcmp("colour", sensor_type) ||
  519. !strcmp("color", sensor_type))) {
  520. ret = reg_write(client, MT9V022_PIXEL_OPERATION_MODE, 4 | 0x11);
  521. mt9v022->model = V4L2_IDENT_MT9V022IX7ATC;
  522. mt9v022->fmts = mt9v022_colour_fmts;
  523. } else {
  524. ret = reg_write(client, MT9V022_PIXEL_OPERATION_MODE, 0x11);
  525. mt9v022->model = V4L2_IDENT_MT9V022IX7ATM;
  526. mt9v022->fmts = mt9v022_monochrome_fmts;
  527. }
  528. if (ret < 0)
  529. goto ei2c;
  530. mt9v022->num_fmts = 0;
  531. /*
  532. * This is a 10bit sensor, so by default we only allow 10bit.
  533. * The platform may support different bus widths due to
  534. * different routing of the data lines.
  535. */
  536. if (icl->query_bus_param)
  537. flags = icl->query_bus_param(icl);
  538. else
  539. flags = SOCAM_DATAWIDTH_10;
  540. if (flags & SOCAM_DATAWIDTH_10)
  541. mt9v022->num_fmts++;
  542. else
  543. mt9v022->fmts++;
  544. if (flags & SOCAM_DATAWIDTH_8)
  545. mt9v022->num_fmts++;
  546. mt9v022->fmt = &mt9v022->fmts[0];
  547. dev_info(&client->dev, "Detected a MT9V022 chip ID %x, %s sensor\n",
  548. data, mt9v022->model == V4L2_IDENT_MT9V022IX7ATM ?
  549. "monochrome" : "colour");
  550. ret = mt9v022_init(client);
  551. if (ret < 0)
  552. dev_err(&client->dev, "Failed to initialise the camera\n");
  553. ei2c:
  554. return ret;
  555. }
  556. static void mt9v022_video_remove(struct soc_camera_device *icd)
  557. {
  558. struct soc_camera_link *icl = to_soc_camera_link(icd);
  559. dev_dbg(icd->pdev, "Video removed: %p, %p\n",
  560. icd->parent, icd->vdev);
  561. if (icl->free_bus)
  562. icl->free_bus(icl);
  563. }
  564. static int mt9v022_g_skip_top_lines(struct v4l2_subdev *sd, u32 *lines)
  565. {
  566. struct i2c_client *client = v4l2_get_subdevdata(sd);
  567. struct mt9v022 *mt9v022 = to_mt9v022(client);
  568. *lines = mt9v022->y_skip_top;
  569. return 0;
  570. }
  571. static const struct v4l2_ctrl_ops mt9v022_ctrl_ops = {
  572. .g_volatile_ctrl = mt9v022_g_volatile_ctrl,
  573. .s_ctrl = mt9v022_s_ctrl,
  574. };
  575. static struct v4l2_subdev_core_ops mt9v022_subdev_core_ops = {
  576. .g_chip_ident = mt9v022_g_chip_ident,
  577. #ifdef CONFIG_VIDEO_ADV_DEBUG
  578. .g_register = mt9v022_g_register,
  579. .s_register = mt9v022_s_register,
  580. #endif
  581. };
  582. static int mt9v022_enum_fmt(struct v4l2_subdev *sd, unsigned int index,
  583. enum v4l2_mbus_pixelcode *code)
  584. {
  585. struct i2c_client *client = v4l2_get_subdevdata(sd);
  586. struct mt9v022 *mt9v022 = to_mt9v022(client);
  587. if (index >= mt9v022->num_fmts)
  588. return -EINVAL;
  589. *code = mt9v022->fmts[index].code;
  590. return 0;
  591. }
  592. static int mt9v022_g_mbus_config(struct v4l2_subdev *sd,
  593. struct v4l2_mbus_config *cfg)
  594. {
  595. struct i2c_client *client = v4l2_get_subdevdata(sd);
  596. struct soc_camera_device *icd = client->dev.platform_data;
  597. struct soc_camera_link *icl = to_soc_camera_link(icd);
  598. cfg->flags = V4L2_MBUS_MASTER | V4L2_MBUS_SLAVE |
  599. V4L2_MBUS_PCLK_SAMPLE_RISING | V4L2_MBUS_PCLK_SAMPLE_FALLING |
  600. V4L2_MBUS_HSYNC_ACTIVE_HIGH | V4L2_MBUS_HSYNC_ACTIVE_LOW |
  601. V4L2_MBUS_VSYNC_ACTIVE_HIGH | V4L2_MBUS_VSYNC_ACTIVE_LOW |
  602. V4L2_MBUS_DATA_ACTIVE_HIGH;
  603. cfg->type = V4L2_MBUS_PARALLEL;
  604. cfg->flags = soc_camera_apply_board_flags(icl, cfg);
  605. return 0;
  606. }
  607. static int mt9v022_s_mbus_config(struct v4l2_subdev *sd,
  608. const struct v4l2_mbus_config *cfg)
  609. {
  610. struct i2c_client *client = v4l2_get_subdevdata(sd);
  611. struct soc_camera_device *icd = client->dev.platform_data;
  612. struct soc_camera_link *icl = to_soc_camera_link(icd);
  613. struct mt9v022 *mt9v022 = to_mt9v022(client);
  614. unsigned long flags = soc_camera_apply_board_flags(icl, cfg);
  615. /*
  616. * Cannot use icd->current_fmt->host_fmt->bits_per_sample, because that
  617. * is the number of bits, that the host has to sample, not the number of
  618. * bits, that we have to send. See mx3_camera.c for an example of 10-bit
  619. * formats being truncated to 8 bits by the host.
  620. */
  621. unsigned int bps = soc_mbus_get_fmtdesc(icd->current_fmt->code)->bits_per_sample;
  622. int ret;
  623. u16 pixclk = 0;
  624. dev_info(icd->pdev, "set %d: %s, %dbps\n", icd->current_fmt->code,
  625. icd->current_fmt->host_fmt->name, bps);
  626. if (icl->set_bus_param) {
  627. ret = icl->set_bus_param(icl, 1 << (bps - 1));
  628. if (ret)
  629. return ret;
  630. } else if (bps != 10) {
  631. /*
  632. * Without board specific bus width settings we only support the
  633. * sensors native bus width
  634. */
  635. return -EINVAL;
  636. }
  637. if (flags & V4L2_MBUS_PCLK_SAMPLE_FALLING)
  638. pixclk |= 0x10;
  639. if (!(flags & V4L2_MBUS_HSYNC_ACTIVE_HIGH))
  640. pixclk |= 0x1;
  641. if (!(flags & V4L2_MBUS_VSYNC_ACTIVE_HIGH))
  642. pixclk |= 0x2;
  643. ret = reg_write(client, MT9V022_PIXCLK_FV_LV, pixclk);
  644. if (ret < 0)
  645. return ret;
  646. if (!(flags & V4L2_MBUS_MASTER))
  647. mt9v022->chip_control &= ~0x8;
  648. ret = reg_write(client, MT9V022_CHIP_CONTROL, mt9v022->chip_control);
  649. if (ret < 0)
  650. return ret;
  651. dev_dbg(&client->dev, "Calculated pixclk 0x%x, chip control 0x%x\n",
  652. pixclk, mt9v022->chip_control);
  653. return 0;
  654. }
  655. static struct v4l2_subdev_video_ops mt9v022_subdev_video_ops = {
  656. .s_stream = mt9v022_s_stream,
  657. .s_mbus_fmt = mt9v022_s_fmt,
  658. .g_mbus_fmt = mt9v022_g_fmt,
  659. .try_mbus_fmt = mt9v022_try_fmt,
  660. .s_crop = mt9v022_s_crop,
  661. .g_crop = mt9v022_g_crop,
  662. .cropcap = mt9v022_cropcap,
  663. .enum_mbus_fmt = mt9v022_enum_fmt,
  664. .g_mbus_config = mt9v022_g_mbus_config,
  665. .s_mbus_config = mt9v022_s_mbus_config,
  666. };
  667. static struct v4l2_subdev_sensor_ops mt9v022_subdev_sensor_ops = {
  668. .g_skip_top_lines = mt9v022_g_skip_top_lines,
  669. };
  670. static struct v4l2_subdev_ops mt9v022_subdev_ops = {
  671. .core = &mt9v022_subdev_core_ops,
  672. .video = &mt9v022_subdev_video_ops,
  673. .sensor = &mt9v022_subdev_sensor_ops,
  674. };
  675. static int mt9v022_probe(struct i2c_client *client,
  676. const struct i2c_device_id *did)
  677. {
  678. struct mt9v022 *mt9v022;
  679. struct soc_camera_device *icd = client->dev.platform_data;
  680. struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
  681. struct soc_camera_link *icl;
  682. int ret;
  683. if (!icd) {
  684. dev_err(&client->dev, "MT9V022: missing soc-camera data!\n");
  685. return -EINVAL;
  686. }
  687. icl = to_soc_camera_link(icd);
  688. if (!icl) {
  689. dev_err(&client->dev, "MT9V022 driver needs platform data\n");
  690. return -EINVAL;
  691. }
  692. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WORD_DATA)) {
  693. dev_warn(&adapter->dev,
  694. "I2C-Adapter doesn't support I2C_FUNC_SMBUS_WORD\n");
  695. return -EIO;
  696. }
  697. mt9v022 = kzalloc(sizeof(struct mt9v022), GFP_KERNEL);
  698. if (!mt9v022)
  699. return -ENOMEM;
  700. v4l2_i2c_subdev_init(&mt9v022->subdev, client, &mt9v022_subdev_ops);
  701. v4l2_ctrl_handler_init(&mt9v022->hdl, 6);
  702. v4l2_ctrl_new_std(&mt9v022->hdl, &mt9v022_ctrl_ops,
  703. V4L2_CID_VFLIP, 0, 1, 1, 0);
  704. v4l2_ctrl_new_std(&mt9v022->hdl, &mt9v022_ctrl_ops,
  705. V4L2_CID_HFLIP, 0, 1, 1, 0);
  706. mt9v022->autogain = v4l2_ctrl_new_std(&mt9v022->hdl, &mt9v022_ctrl_ops,
  707. V4L2_CID_AUTOGAIN, 0, 1, 1, 1);
  708. mt9v022->gain = v4l2_ctrl_new_std(&mt9v022->hdl, &mt9v022_ctrl_ops,
  709. V4L2_CID_GAIN, 0, 127, 1, 64);
  710. /*
  711. * Simulated autoexposure. If enabled, we calculate shutter width
  712. * ourselves in the driver based on vertical blanking and frame width
  713. */
  714. mt9v022->autoexposure = v4l2_ctrl_new_std_menu(&mt9v022->hdl,
  715. &mt9v022_ctrl_ops, V4L2_CID_EXPOSURE_AUTO, 1, 0,
  716. V4L2_EXPOSURE_AUTO);
  717. mt9v022->exposure = v4l2_ctrl_new_std(&mt9v022->hdl, &mt9v022_ctrl_ops,
  718. V4L2_CID_EXPOSURE, 1, 255, 1, 255);
  719. mt9v022->subdev.ctrl_handler = &mt9v022->hdl;
  720. if (mt9v022->hdl.error) {
  721. int err = mt9v022->hdl.error;
  722. kfree(mt9v022);
  723. return err;
  724. }
  725. v4l2_ctrl_auto_cluster(2, &mt9v022->autoexposure,
  726. V4L2_EXPOSURE_MANUAL, true);
  727. v4l2_ctrl_auto_cluster(2, &mt9v022->autogain, 0, true);
  728. mt9v022->chip_control = MT9V022_CHIP_CONTROL_DEFAULT;
  729. /*
  730. * MT9V022 _really_ corrupts the first read out line.
  731. * TODO: verify on i.MX31
  732. */
  733. mt9v022->y_skip_top = 1;
  734. mt9v022->rect.left = MT9V022_COLUMN_SKIP;
  735. mt9v022->rect.top = MT9V022_ROW_SKIP;
  736. mt9v022->rect.width = MT9V022_MAX_WIDTH;
  737. mt9v022->rect.height = MT9V022_MAX_HEIGHT;
  738. ret = mt9v022_video_probe(icd, client);
  739. if (ret) {
  740. v4l2_ctrl_handler_free(&mt9v022->hdl);
  741. kfree(mt9v022);
  742. }
  743. return ret;
  744. }
  745. static int mt9v022_remove(struct i2c_client *client)
  746. {
  747. struct mt9v022 *mt9v022 = to_mt9v022(client);
  748. struct soc_camera_device *icd = client->dev.platform_data;
  749. v4l2_device_unregister_subdev(&mt9v022->subdev);
  750. mt9v022_video_remove(icd);
  751. v4l2_ctrl_handler_free(&mt9v022->hdl);
  752. kfree(mt9v022);
  753. return 0;
  754. }
  755. static const struct i2c_device_id mt9v022_id[] = {
  756. { "mt9v022", 0 },
  757. { }
  758. };
  759. MODULE_DEVICE_TABLE(i2c, mt9v022_id);
  760. static struct i2c_driver mt9v022_i2c_driver = {
  761. .driver = {
  762. .name = "mt9v022",
  763. },
  764. .probe = mt9v022_probe,
  765. .remove = mt9v022_remove,
  766. .id_table = mt9v022_id,
  767. };
  768. static int __init mt9v022_mod_init(void)
  769. {
  770. return i2c_add_driver(&mt9v022_i2c_driver);
  771. }
  772. static void __exit mt9v022_mod_exit(void)
  773. {
  774. i2c_del_driver(&mt9v022_i2c_driver);
  775. }
  776. module_init(mt9v022_mod_init);
  777. module_exit(mt9v022_mod_exit);
  778. MODULE_DESCRIPTION("Micron MT9V022 Camera driver");
  779. MODULE_AUTHOR("Guennadi Liakhovetski <kernel@pengutronix.de>");
  780. MODULE_LICENSE("GPL");