mt9v022.c 24 KB

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