mt9t031.c 22 KB

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