mt9t031.c 22 KB

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