mt9v022.c 24 KB

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