mt9v022.c 23 KB

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