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