mt9t031.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876
  1. /*
  2. * Driver for MT9T031 CMOS Image Sensor from Micron
  3. *
  4. * Copyright (C) 2008, Guennadi Liakhovetski, DENX Software Engineering <lg@denx.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/log2.h>
  14. #include <media/v4l2-subdev.h>
  15. #include <media/v4l2-chip-ident.h>
  16. #include <media/soc_camera.h>
  17. /*
  18. * mt9t031 i2c address 0x5d
  19. * The platform has to define i2c_board_info and link to it from
  20. * struct soc_camera_link
  21. */
  22. /* mt9t031 selected register addresses */
  23. #define MT9T031_CHIP_VERSION 0x00
  24. #define MT9T031_ROW_START 0x01
  25. #define MT9T031_COLUMN_START 0x02
  26. #define MT9T031_WINDOW_HEIGHT 0x03
  27. #define MT9T031_WINDOW_WIDTH 0x04
  28. #define MT9T031_HORIZONTAL_BLANKING 0x05
  29. #define MT9T031_VERTICAL_BLANKING 0x06
  30. #define MT9T031_OUTPUT_CONTROL 0x07
  31. #define MT9T031_SHUTTER_WIDTH_UPPER 0x08
  32. #define MT9T031_SHUTTER_WIDTH 0x09
  33. #define MT9T031_PIXEL_CLOCK_CONTROL 0x0a
  34. #define MT9T031_FRAME_RESTART 0x0b
  35. #define MT9T031_SHUTTER_DELAY 0x0c
  36. #define MT9T031_RESET 0x0d
  37. #define MT9T031_READ_MODE_1 0x1e
  38. #define MT9T031_READ_MODE_2 0x20
  39. #define MT9T031_READ_MODE_3 0x21
  40. #define MT9T031_ROW_ADDRESS_MODE 0x22
  41. #define MT9T031_COLUMN_ADDRESS_MODE 0x23
  42. #define MT9T031_GLOBAL_GAIN 0x35
  43. #define MT9T031_CHIP_ENABLE 0xF8
  44. #define MT9T031_MAX_HEIGHT 1536
  45. #define MT9T031_MAX_WIDTH 2048
  46. #define MT9T031_MIN_HEIGHT 2
  47. #define MT9T031_MIN_WIDTH 18
  48. #define MT9T031_HORIZONTAL_BLANK 142
  49. #define MT9T031_VERTICAL_BLANK 25
  50. #define MT9T031_COLUMN_SKIP 32
  51. #define MT9T031_ROW_SKIP 20
  52. #define MT9T031_BUS_PARAM (SOCAM_PCLK_SAMPLE_RISING | \
  53. SOCAM_PCLK_SAMPLE_FALLING | SOCAM_HSYNC_ACTIVE_HIGH | \
  54. SOCAM_VSYNC_ACTIVE_HIGH | SOCAM_DATA_ACTIVE_HIGH | \
  55. SOCAM_MASTER | SOCAM_DATAWIDTH_10)
  56. struct mt9t031 {
  57. struct v4l2_subdev subdev;
  58. struct v4l2_rect rect; /* Sensor window */
  59. int model; /* V4L2_IDENT_MT9T031* codes from v4l2-chip-ident.h */
  60. u16 xskip;
  61. u16 yskip;
  62. unsigned int gain;
  63. unsigned short y_skip_top; /* Lines to skip at the top */
  64. unsigned int exposure;
  65. unsigned char autoexposure;
  66. };
  67. static struct mt9t031 *to_mt9t031(const struct i2c_client *client)
  68. {
  69. return container_of(i2c_get_clientdata(client), struct mt9t031, subdev);
  70. }
  71. static int reg_read(struct i2c_client *client, const u8 reg)
  72. {
  73. s32 data = i2c_smbus_read_word_data(client, reg);
  74. return data < 0 ? data : swab16(data);
  75. }
  76. static int reg_write(struct i2c_client *client, const u8 reg,
  77. const u16 data)
  78. {
  79. return i2c_smbus_write_word_data(client, reg, swab16(data));
  80. }
  81. static int reg_set(struct i2c_client *client, const u8 reg,
  82. const u16 data)
  83. {
  84. int ret;
  85. ret = reg_read(client, reg);
  86. if (ret < 0)
  87. return ret;
  88. return reg_write(client, reg, ret | data);
  89. }
  90. static int reg_clear(struct i2c_client *client, const u8 reg,
  91. const u16 data)
  92. {
  93. int ret;
  94. ret = reg_read(client, reg);
  95. if (ret < 0)
  96. return ret;
  97. return reg_write(client, reg, ret & ~data);
  98. }
  99. static int set_shutter(struct i2c_client *client, const u32 data)
  100. {
  101. int ret;
  102. ret = reg_write(client, MT9T031_SHUTTER_WIDTH_UPPER, data >> 16);
  103. if (ret >= 0)
  104. ret = reg_write(client, MT9T031_SHUTTER_WIDTH, data & 0xffff);
  105. return ret;
  106. }
  107. static int get_shutter(struct i2c_client *client, u32 *data)
  108. {
  109. int ret;
  110. ret = reg_read(client, MT9T031_SHUTTER_WIDTH_UPPER);
  111. *data = ret << 16;
  112. if (ret >= 0)
  113. ret = reg_read(client, MT9T031_SHUTTER_WIDTH);
  114. *data |= ret & 0xffff;
  115. return ret < 0 ? ret : 0;
  116. }
  117. static int mt9t031_idle(struct i2c_client *client)
  118. {
  119. int ret;
  120. /* Disable chip output, synchronous option update */
  121. ret = reg_write(client, MT9T031_RESET, 1);
  122. if (ret >= 0)
  123. ret = reg_write(client, MT9T031_RESET, 0);
  124. if (ret >= 0)
  125. ret = reg_clear(client, MT9T031_OUTPUT_CONTROL, 2);
  126. return ret >= 0 ? 0 : -EIO;
  127. }
  128. static int mt9t031_disable(struct i2c_client *client)
  129. {
  130. /* Disable the chip */
  131. reg_clear(client, MT9T031_OUTPUT_CONTROL, 2);
  132. return 0;
  133. }
  134. static int mt9t031_s_stream(struct v4l2_subdev *sd, int enable)
  135. {
  136. struct i2c_client *client = sd->priv;
  137. int ret;
  138. if (enable)
  139. /* Switch to master "normal" mode */
  140. ret = reg_set(client, MT9T031_OUTPUT_CONTROL, 2);
  141. else
  142. /* Stop sensor readout */
  143. ret = reg_clear(client, MT9T031_OUTPUT_CONTROL, 2);
  144. if (ret < 0)
  145. return -EIO;
  146. return 0;
  147. }
  148. static int mt9t031_set_bus_param(struct soc_camera_device *icd,
  149. unsigned long flags)
  150. {
  151. struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
  152. /* The caller should have queried our parameters, check anyway */
  153. if (flags & ~MT9T031_BUS_PARAM)
  154. return -EINVAL;
  155. if (flags & SOCAM_PCLK_SAMPLE_FALLING)
  156. reg_clear(client, MT9T031_PIXEL_CLOCK_CONTROL, 0x8000);
  157. else
  158. reg_set(client, MT9T031_PIXEL_CLOCK_CONTROL, 0x8000);
  159. return 0;
  160. }
  161. static unsigned long mt9t031_query_bus_param(struct soc_camera_device *icd)
  162. {
  163. struct soc_camera_link *icl = to_soc_camera_link(icd);
  164. return soc_camera_apply_sensor_flags(icl, MT9T031_BUS_PARAM);
  165. }
  166. /* target must be _even_ */
  167. static u16 mt9t031_skip(s32 *source, s32 target, s32 max)
  168. {
  169. unsigned int skip;
  170. if (*source < target + target / 2) {
  171. *source = target;
  172. return 1;
  173. }
  174. skip = min(max, *source + target / 2) / target;
  175. if (skip > 8)
  176. skip = 8;
  177. *source = target * skip;
  178. return skip;
  179. }
  180. /* rect is the sensor rectangle, the caller guarantees parameter validity */
  181. static int mt9t031_set_params(struct soc_camera_device *icd,
  182. struct v4l2_rect *rect, u16 xskip, u16 yskip)
  183. {
  184. struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
  185. struct mt9t031 *mt9t031 = to_mt9t031(client);
  186. int ret;
  187. u16 xbin, ybin;
  188. const u16 hblank = MT9T031_HORIZONTAL_BLANK,
  189. vblank = MT9T031_VERTICAL_BLANK;
  190. xbin = min(xskip, (u16)3);
  191. ybin = min(yskip, (u16)3);
  192. /*
  193. * Could just do roundup(rect->left, [xy]bin * 2); but this is cheaper.
  194. * There is always a valid suitably aligned value. The worst case is
  195. * xbin = 3, width = 2048. Then we will start at 36, the last read out
  196. * pixel will be 2083, which is < 2085 - first black pixel.
  197. *
  198. * MT9T031 datasheet imposes window left border alignment, depending on
  199. * the selected xskip. Failing to conform to this requirement produces
  200. * dark horizontal stripes in the image. However, even obeying to this
  201. * requirement doesn't eliminate the stripes in all configurations. They
  202. * appear "locally reproducibly," but can differ between tests under
  203. * different lighting conditions.
  204. */
  205. switch (xbin) {
  206. case 1:
  207. rect->left &= ~1;
  208. break;
  209. case 2:
  210. rect->left &= ~3;
  211. break;
  212. case 3:
  213. rect->left = rect->left > roundup(MT9T031_COLUMN_SKIP, 6) ?
  214. (rect->left / 6) * 6 : roundup(MT9T031_COLUMN_SKIP, 6);
  215. }
  216. rect->top &= ~1;
  217. dev_dbg(&client->dev, "skip %u:%u, rect %ux%u@%u:%u\n",
  218. xskip, yskip, rect->width, rect->height, rect->left, rect->top);
  219. /* Disable register update, reconfigure atomically */
  220. ret = reg_set(client, MT9T031_OUTPUT_CONTROL, 1);
  221. if (ret < 0)
  222. return ret;
  223. /* Blanking and start values - default... */
  224. ret = reg_write(client, MT9T031_HORIZONTAL_BLANKING, hblank);
  225. if (ret >= 0)
  226. ret = reg_write(client, MT9T031_VERTICAL_BLANKING, vblank);
  227. if (yskip != mt9t031->yskip || xskip != mt9t031->xskip) {
  228. /* Binning, skipping */
  229. if (ret >= 0)
  230. ret = reg_write(client, MT9T031_COLUMN_ADDRESS_MODE,
  231. ((xbin - 1) << 4) | (xskip - 1));
  232. if (ret >= 0)
  233. ret = reg_write(client, MT9T031_ROW_ADDRESS_MODE,
  234. ((ybin - 1) << 4) | (yskip - 1));
  235. }
  236. dev_dbg(&client->dev, "new physical left %u, top %u\n",
  237. rect->left, rect->top);
  238. /*
  239. * The caller provides a supported format, as guaranteed by
  240. * icd->try_fmt_cap(), soc_camera_s_crop() and soc_camera_cropcap()
  241. */
  242. if (ret >= 0)
  243. ret = reg_write(client, MT9T031_COLUMN_START, rect->left);
  244. if (ret >= 0)
  245. ret = reg_write(client, MT9T031_ROW_START, rect->top);
  246. if (ret >= 0)
  247. ret = reg_write(client, MT9T031_WINDOW_WIDTH, rect->width - 1);
  248. if (ret >= 0)
  249. ret = reg_write(client, MT9T031_WINDOW_HEIGHT,
  250. rect->height + mt9t031->y_skip_top - 1);
  251. if (ret >= 0 && mt9t031->autoexposure) {
  252. unsigned int total_h = rect->height + mt9t031->y_skip_top + vblank;
  253. ret = set_shutter(client, total_h);
  254. if (ret >= 0) {
  255. const u32 shutter_max = MT9T031_MAX_HEIGHT + vblank;
  256. const struct v4l2_queryctrl *qctrl =
  257. soc_camera_find_qctrl(icd->ops,
  258. V4L2_CID_EXPOSURE);
  259. mt9t031->exposure = (shutter_max / 2 + (total_h - 1) *
  260. (qctrl->maximum - qctrl->minimum)) /
  261. shutter_max + qctrl->minimum;
  262. }
  263. }
  264. /* Re-enable register update, commit all changes */
  265. if (ret >= 0)
  266. ret = reg_clear(client, MT9T031_OUTPUT_CONTROL, 1);
  267. if (ret >= 0) {
  268. mt9t031->rect = *rect;
  269. mt9t031->xskip = xskip;
  270. mt9t031->yskip = yskip;
  271. }
  272. return ret < 0 ? ret : 0;
  273. }
  274. static int mt9t031_s_crop(struct v4l2_subdev *sd, struct v4l2_crop *a)
  275. {
  276. struct v4l2_rect rect = a->c;
  277. struct i2c_client *client = sd->priv;
  278. struct mt9t031 *mt9t031 = to_mt9t031(client);
  279. struct soc_camera_device *icd = client->dev.platform_data;
  280. rect.width = ALIGN(rect.width, 2);
  281. rect.height = ALIGN(rect.height, 2);
  282. soc_camera_limit_side(&rect.left, &rect.width,
  283. MT9T031_COLUMN_SKIP, MT9T031_MIN_WIDTH, MT9T031_MAX_WIDTH);
  284. soc_camera_limit_side(&rect.top, &rect.height,
  285. MT9T031_ROW_SKIP, MT9T031_MIN_HEIGHT, MT9T031_MAX_HEIGHT);
  286. return mt9t031_set_params(icd, &rect, mt9t031->xskip, mt9t031->yskip);
  287. }
  288. static int mt9t031_g_crop(struct v4l2_subdev *sd, struct v4l2_crop *a)
  289. {
  290. struct i2c_client *client = sd->priv;
  291. struct mt9t031 *mt9t031 = to_mt9t031(client);
  292. a->c = mt9t031->rect;
  293. a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  294. return 0;
  295. }
  296. static int mt9t031_cropcap(struct v4l2_subdev *sd, struct v4l2_cropcap *a)
  297. {
  298. a->bounds.left = MT9T031_COLUMN_SKIP;
  299. a->bounds.top = MT9T031_ROW_SKIP;
  300. a->bounds.width = MT9T031_MAX_WIDTH;
  301. a->bounds.height = MT9T031_MAX_HEIGHT;
  302. a->defrect = a->bounds;
  303. a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  304. a->pixelaspect.numerator = 1;
  305. a->pixelaspect.denominator = 1;
  306. return 0;
  307. }
  308. static int mt9t031_g_fmt(struct v4l2_subdev *sd,
  309. struct v4l2_mbus_framefmt *mf)
  310. {
  311. struct i2c_client *client = sd->priv;
  312. struct mt9t031 *mt9t031 = to_mt9t031(client);
  313. mf->width = mt9t031->rect.width / mt9t031->xskip;
  314. mf->height = mt9t031->rect.height / mt9t031->yskip;
  315. mf->code = V4L2_MBUS_FMT_SBGGR10_1X10;
  316. mf->colorspace = V4L2_COLORSPACE_SRGB;
  317. mf->field = V4L2_FIELD_NONE;
  318. return 0;
  319. }
  320. static int mt9t031_s_fmt(struct v4l2_subdev *sd,
  321. struct v4l2_mbus_framefmt *mf)
  322. {
  323. struct i2c_client *client = sd->priv;
  324. struct mt9t031 *mt9t031 = to_mt9t031(client);
  325. struct soc_camera_device *icd = client->dev.platform_data;
  326. u16 xskip, yskip;
  327. struct v4l2_rect rect = mt9t031->rect;
  328. /*
  329. * try_fmt has put width and height within limits.
  330. * S_FMT: use binning and skipping for scaling
  331. */
  332. xskip = mt9t031_skip(&rect.width, mf->width, MT9T031_MAX_WIDTH);
  333. yskip = mt9t031_skip(&rect.height, mf->height, MT9T031_MAX_HEIGHT);
  334. mf->code = V4L2_MBUS_FMT_SBGGR10_1X10;
  335. mf->colorspace = V4L2_COLORSPACE_SRGB;
  336. /* mt9t031_set_params() doesn't change width and height */
  337. return mt9t031_set_params(icd, &rect, xskip, yskip);
  338. }
  339. /*
  340. * If a user window larger than sensor window is requested, we'll increase the
  341. * sensor window.
  342. */
  343. static int mt9t031_try_fmt(struct v4l2_subdev *sd,
  344. struct v4l2_mbus_framefmt *mf)
  345. {
  346. v4l_bound_align_image(
  347. &mf->width, MT9T031_MIN_WIDTH, MT9T031_MAX_WIDTH, 1,
  348. &mf->height, MT9T031_MIN_HEIGHT, MT9T031_MAX_HEIGHT, 1, 0);
  349. mf->code = V4L2_MBUS_FMT_SBGGR10_1X10;
  350. mf->colorspace = V4L2_COLORSPACE_SRGB;
  351. return 0;
  352. }
  353. static int mt9t031_g_chip_ident(struct v4l2_subdev *sd,
  354. struct v4l2_dbg_chip_ident *id)
  355. {
  356. struct i2c_client *client = sd->priv;
  357. struct mt9t031 *mt9t031 = to_mt9t031(client);
  358. if (id->match.type != V4L2_CHIP_MATCH_I2C_ADDR)
  359. return -EINVAL;
  360. if (id->match.addr != client->addr)
  361. return -ENODEV;
  362. id->ident = mt9t031->model;
  363. id->revision = 0;
  364. return 0;
  365. }
  366. #ifdef CONFIG_VIDEO_ADV_DEBUG
  367. static int mt9t031_g_register(struct v4l2_subdev *sd,
  368. struct v4l2_dbg_register *reg)
  369. {
  370. struct i2c_client *client = sd->priv;
  371. if (reg->match.type != V4L2_CHIP_MATCH_I2C_ADDR || reg->reg > 0xff)
  372. return -EINVAL;
  373. if (reg->match.addr != client->addr)
  374. return -ENODEV;
  375. reg->val = reg_read(client, reg->reg);
  376. if (reg->val > 0xffff)
  377. return -EIO;
  378. return 0;
  379. }
  380. static int mt9t031_s_register(struct v4l2_subdev *sd,
  381. struct v4l2_dbg_register *reg)
  382. {
  383. struct i2c_client *client = sd->priv;
  384. if (reg->match.type != V4L2_CHIP_MATCH_I2C_ADDR || reg->reg > 0xff)
  385. return -EINVAL;
  386. if (reg->match.addr != client->addr)
  387. return -ENODEV;
  388. if (reg_write(client, reg->reg, reg->val) < 0)
  389. return -EIO;
  390. return 0;
  391. }
  392. #endif
  393. static const struct v4l2_queryctrl mt9t031_controls[] = {
  394. {
  395. .id = V4L2_CID_VFLIP,
  396. .type = V4L2_CTRL_TYPE_BOOLEAN,
  397. .name = "Flip Vertically",
  398. .minimum = 0,
  399. .maximum = 1,
  400. .step = 1,
  401. .default_value = 0,
  402. }, {
  403. .id = V4L2_CID_HFLIP,
  404. .type = V4L2_CTRL_TYPE_BOOLEAN,
  405. .name = "Flip Horizontally",
  406. .minimum = 0,
  407. .maximum = 1,
  408. .step = 1,
  409. .default_value = 0,
  410. }, {
  411. .id = V4L2_CID_GAIN,
  412. .type = V4L2_CTRL_TYPE_INTEGER,
  413. .name = "Gain",
  414. .minimum = 0,
  415. .maximum = 127,
  416. .step = 1,
  417. .default_value = 64,
  418. .flags = V4L2_CTRL_FLAG_SLIDER,
  419. }, {
  420. .id = V4L2_CID_EXPOSURE,
  421. .type = V4L2_CTRL_TYPE_INTEGER,
  422. .name = "Exposure",
  423. .minimum = 1,
  424. .maximum = 255,
  425. .step = 1,
  426. .default_value = 255,
  427. .flags = V4L2_CTRL_FLAG_SLIDER,
  428. }, {
  429. .id = V4L2_CID_EXPOSURE_AUTO,
  430. .type = V4L2_CTRL_TYPE_BOOLEAN,
  431. .name = "Automatic Exposure",
  432. .minimum = 0,
  433. .maximum = 1,
  434. .step = 1,
  435. .default_value = 1,
  436. }
  437. };
  438. static struct soc_camera_ops mt9t031_ops = {
  439. .set_bus_param = mt9t031_set_bus_param,
  440. .query_bus_param = mt9t031_query_bus_param,
  441. .controls = mt9t031_controls,
  442. .num_controls = ARRAY_SIZE(mt9t031_controls),
  443. };
  444. static int mt9t031_g_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
  445. {
  446. struct i2c_client *client = sd->priv;
  447. struct mt9t031 *mt9t031 = to_mt9t031(client);
  448. int data;
  449. switch (ctrl->id) {
  450. case V4L2_CID_VFLIP:
  451. data = reg_read(client, MT9T031_READ_MODE_2);
  452. if (data < 0)
  453. return -EIO;
  454. ctrl->value = !!(data & 0x8000);
  455. break;
  456. case V4L2_CID_HFLIP:
  457. data = reg_read(client, MT9T031_READ_MODE_2);
  458. if (data < 0)
  459. return -EIO;
  460. ctrl->value = !!(data & 0x4000);
  461. break;
  462. case V4L2_CID_EXPOSURE_AUTO:
  463. ctrl->value = mt9t031->autoexposure;
  464. break;
  465. case V4L2_CID_GAIN:
  466. ctrl->value = mt9t031->gain;
  467. break;
  468. case V4L2_CID_EXPOSURE:
  469. ctrl->value = mt9t031->exposure;
  470. break;
  471. }
  472. return 0;
  473. }
  474. static int mt9t031_s_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
  475. {
  476. struct i2c_client *client = sd->priv;
  477. struct mt9t031 *mt9t031 = to_mt9t031(client);
  478. struct soc_camera_device *icd = client->dev.platform_data;
  479. const struct v4l2_queryctrl *qctrl;
  480. int data;
  481. qctrl = soc_camera_find_qctrl(&mt9t031_ops, ctrl->id);
  482. if (!qctrl)
  483. return -EINVAL;
  484. switch (ctrl->id) {
  485. case V4L2_CID_VFLIP:
  486. if (ctrl->value)
  487. data = reg_set(client, MT9T031_READ_MODE_2, 0x8000);
  488. else
  489. data = reg_clear(client, MT9T031_READ_MODE_2, 0x8000);
  490. if (data < 0)
  491. return -EIO;
  492. break;
  493. case V4L2_CID_HFLIP:
  494. if (ctrl->value)
  495. data = reg_set(client, MT9T031_READ_MODE_2, 0x4000);
  496. else
  497. data = reg_clear(client, MT9T031_READ_MODE_2, 0x4000);
  498. if (data < 0)
  499. return -EIO;
  500. break;
  501. case V4L2_CID_GAIN:
  502. if (ctrl->value > qctrl->maximum || ctrl->value < qctrl->minimum)
  503. return -EINVAL;
  504. /* See Datasheet Table 7, Gain settings. */
  505. if (ctrl->value <= qctrl->default_value) {
  506. /* Pack it into 0..1 step 0.125, register values 0..8 */
  507. unsigned long range = qctrl->default_value - qctrl->minimum;
  508. data = ((ctrl->value - qctrl->minimum) * 8 + range / 2) / range;
  509. dev_dbg(&client->dev, "Setting gain %d\n", data);
  510. data = reg_write(client, MT9T031_GLOBAL_GAIN, data);
  511. if (data < 0)
  512. return -EIO;
  513. } else {
  514. /* Pack it into 1.125..128 variable step, register values 9..0x7860 */
  515. /* We assume qctrl->maximum - qctrl->default_value - 1 > 0 */
  516. unsigned long range = qctrl->maximum - qctrl->default_value - 1;
  517. /* calculated gain: map 65..127 to 9..1024 step 0.125 */
  518. unsigned long gain = ((ctrl->value - qctrl->default_value - 1) *
  519. 1015 + range / 2) / range + 9;
  520. if (gain <= 32) /* calculated gain 9..32 -> 9..32 */
  521. data = gain;
  522. else if (gain <= 64) /* calculated gain 33..64 -> 0x51..0x60 */
  523. data = ((gain - 32) * 16 + 16) / 32 + 80;
  524. else
  525. /* calculated gain 65..1024 -> (1..120) << 8 + 0x60 */
  526. data = (((gain - 64 + 7) * 32) & 0xff00) | 0x60;
  527. dev_dbg(&client->dev, "Set gain from 0x%x to 0x%x\n",
  528. reg_read(client, MT9T031_GLOBAL_GAIN), data);
  529. data = reg_write(client, MT9T031_GLOBAL_GAIN, data);
  530. if (data < 0)
  531. return -EIO;
  532. }
  533. /* Success */
  534. mt9t031->gain = ctrl->value;
  535. break;
  536. case V4L2_CID_EXPOSURE:
  537. /* mt9t031 has maximum == default */
  538. if (ctrl->value > qctrl->maximum || ctrl->value < qctrl->minimum)
  539. return -EINVAL;
  540. else {
  541. const unsigned long range = qctrl->maximum - qctrl->minimum;
  542. const u32 shutter = ((ctrl->value - qctrl->minimum) * 1048 +
  543. range / 2) / range + 1;
  544. u32 old;
  545. get_shutter(client, &old);
  546. dev_dbg(&client->dev, "Set shutter from %u to %u\n",
  547. old, shutter);
  548. if (set_shutter(client, shutter) < 0)
  549. return -EIO;
  550. mt9t031->exposure = ctrl->value;
  551. mt9t031->autoexposure = 0;
  552. }
  553. break;
  554. case V4L2_CID_EXPOSURE_AUTO:
  555. if (ctrl->value) {
  556. const u16 vblank = MT9T031_VERTICAL_BLANK;
  557. const u32 shutter_max = MT9T031_MAX_HEIGHT + vblank;
  558. unsigned int total_h = mt9t031->rect.height +
  559. mt9t031->y_skip_top + vblank;
  560. if (set_shutter(client, total_h) < 0)
  561. return -EIO;
  562. qctrl = soc_camera_find_qctrl(icd->ops, V4L2_CID_EXPOSURE);
  563. mt9t031->exposure = (shutter_max / 2 + (total_h - 1) *
  564. (qctrl->maximum - qctrl->minimum)) /
  565. shutter_max + qctrl->minimum;
  566. mt9t031->autoexposure = 1;
  567. } else
  568. mt9t031->autoexposure = 0;
  569. break;
  570. }
  571. return 0;
  572. }
  573. /*
  574. * Interface active, can use i2c. If it fails, it can indeed mean, that
  575. * this wasn't our capture interface, so, we wait for the right one
  576. */
  577. static int mt9t031_video_probe(struct i2c_client *client)
  578. {
  579. struct mt9t031 *mt9t031 = to_mt9t031(client);
  580. s32 data;
  581. int ret;
  582. /* Enable the chip */
  583. data = reg_write(client, MT9T031_CHIP_ENABLE, 1);
  584. dev_dbg(&client->dev, "write: %d\n", data);
  585. /* Read out the chip version register */
  586. data = reg_read(client, MT9T031_CHIP_VERSION);
  587. switch (data) {
  588. case 0x1621:
  589. mt9t031->model = V4L2_IDENT_MT9T031;
  590. break;
  591. default:
  592. dev_err(&client->dev,
  593. "No MT9T031 chip detected, register read %x\n", data);
  594. return -ENODEV;
  595. }
  596. dev_info(&client->dev, "Detected a MT9T031 chip ID %x\n", data);
  597. ret = mt9t031_idle(client);
  598. if (ret < 0)
  599. dev_err(&client->dev, "Failed to initialise the camera\n");
  600. /* mt9t031_idle() has reset the chip to default. */
  601. mt9t031->exposure = 255;
  602. mt9t031->gain = 64;
  603. return ret;
  604. }
  605. static int mt9t031_g_skip_top_lines(struct v4l2_subdev *sd, u32 *lines)
  606. {
  607. struct i2c_client *client = sd->priv;
  608. struct mt9t031 *mt9t031 = to_mt9t031(client);
  609. *lines = mt9t031->y_skip_top;
  610. return 0;
  611. }
  612. static struct v4l2_subdev_core_ops mt9t031_subdev_core_ops = {
  613. .g_ctrl = mt9t031_g_ctrl,
  614. .s_ctrl = mt9t031_s_ctrl,
  615. .g_chip_ident = mt9t031_g_chip_ident,
  616. #ifdef CONFIG_VIDEO_ADV_DEBUG
  617. .g_register = mt9t031_g_register,
  618. .s_register = mt9t031_s_register,
  619. #endif
  620. };
  621. static int mt9t031_enum_fmt(struct v4l2_subdev *sd, int index,
  622. enum v4l2_mbus_pixelcode *code)
  623. {
  624. if (index)
  625. return -EINVAL;
  626. *code = V4L2_MBUS_FMT_SBGGR10_1X10;
  627. return 0;
  628. }
  629. static struct v4l2_subdev_video_ops mt9t031_subdev_video_ops = {
  630. .s_stream = mt9t031_s_stream,
  631. .s_mbus_fmt = mt9t031_s_fmt,
  632. .g_mbus_fmt = mt9t031_g_fmt,
  633. .try_mbus_fmt = mt9t031_try_fmt,
  634. .s_crop = mt9t031_s_crop,
  635. .g_crop = mt9t031_g_crop,
  636. .cropcap = mt9t031_cropcap,
  637. .enum_mbus_fmt = mt9t031_enum_fmt,
  638. };
  639. static struct v4l2_subdev_sensor_ops mt9t031_subdev_sensor_ops = {
  640. .g_skip_top_lines = mt9t031_g_skip_top_lines,
  641. };
  642. static struct v4l2_subdev_ops mt9t031_subdev_ops = {
  643. .core = &mt9t031_subdev_core_ops,
  644. .video = &mt9t031_subdev_video_ops,
  645. .sensor = &mt9t031_subdev_sensor_ops,
  646. };
  647. static int mt9t031_probe(struct i2c_client *client,
  648. const struct i2c_device_id *did)
  649. {
  650. struct mt9t031 *mt9t031;
  651. struct soc_camera_device *icd = client->dev.platform_data;
  652. struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
  653. struct soc_camera_link *icl;
  654. int ret;
  655. if (!icd) {
  656. dev_err(&client->dev, "MT9T031: missing soc-camera data!\n");
  657. return -EINVAL;
  658. }
  659. icl = to_soc_camera_link(icd);
  660. if (!icl) {
  661. dev_err(&client->dev, "MT9T031 driver needs platform data\n");
  662. return -EINVAL;
  663. }
  664. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WORD_DATA)) {
  665. dev_warn(&adapter->dev,
  666. "I2C-Adapter doesn't support I2C_FUNC_SMBUS_WORD\n");
  667. return -EIO;
  668. }
  669. mt9t031 = kzalloc(sizeof(struct mt9t031), GFP_KERNEL);
  670. if (!mt9t031)
  671. return -ENOMEM;
  672. v4l2_i2c_subdev_init(&mt9t031->subdev, client, &mt9t031_subdev_ops);
  673. /* Second stage probe - when a capture adapter is there */
  674. icd->ops = &mt9t031_ops;
  675. mt9t031->y_skip_top = 0;
  676. mt9t031->rect.left = MT9T031_COLUMN_SKIP;
  677. mt9t031->rect.top = MT9T031_ROW_SKIP;
  678. mt9t031->rect.width = MT9T031_MAX_WIDTH;
  679. mt9t031->rect.height = MT9T031_MAX_HEIGHT;
  680. /*
  681. * Simulated autoexposure. If enabled, we calculate shutter width
  682. * ourselves in the driver based on vertical blanking and frame width
  683. */
  684. mt9t031->autoexposure = 1;
  685. mt9t031->xskip = 1;
  686. mt9t031->yskip = 1;
  687. mt9t031_idle(client);
  688. ret = mt9t031_video_probe(client);
  689. mt9t031_disable(client);
  690. if (ret) {
  691. icd->ops = NULL;
  692. i2c_set_clientdata(client, NULL);
  693. kfree(mt9t031);
  694. }
  695. return ret;
  696. }
  697. static int mt9t031_remove(struct i2c_client *client)
  698. {
  699. struct mt9t031 *mt9t031 = to_mt9t031(client);
  700. struct soc_camera_device *icd = client->dev.platform_data;
  701. icd->ops = NULL;
  702. i2c_set_clientdata(client, NULL);
  703. client->driver = NULL;
  704. kfree(mt9t031);
  705. return 0;
  706. }
  707. static const struct i2c_device_id mt9t031_id[] = {
  708. { "mt9t031", 0 },
  709. { }
  710. };
  711. MODULE_DEVICE_TABLE(i2c, mt9t031_id);
  712. static struct i2c_driver mt9t031_i2c_driver = {
  713. .driver = {
  714. .name = "mt9t031",
  715. },
  716. .probe = mt9t031_probe,
  717. .remove = mt9t031_remove,
  718. .id_table = mt9t031_id,
  719. };
  720. static int __init mt9t031_mod_init(void)
  721. {
  722. return i2c_add_driver(&mt9t031_i2c_driver);
  723. }
  724. static void __exit mt9t031_mod_exit(void)
  725. {
  726. i2c_del_driver(&mt9t031_i2c_driver);
  727. }
  728. module_init(mt9t031_mod_init);
  729. module_exit(mt9t031_mod_exit);
  730. MODULE_DESCRIPTION("Micron MT9T031 Camera driver");
  731. MODULE_AUTHOR("Guennadi Liakhovetski <lg@denx.de>");
  732. MODULE_LICENSE("GPL v2");