mt9v022.c 20 KB

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