mt9v022.c 23 KB

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