mt9t031.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879
  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. enum {
  167. MT9T031_CTRL_VFLIP,
  168. MT9T031_CTRL_HFLIP,
  169. MT9T031_CTRL_GAIN,
  170. MT9T031_CTRL_EXPOSURE,
  171. MT9T031_CTRL_EXPOSURE_AUTO,
  172. };
  173. static const struct v4l2_queryctrl mt9t031_controls[] = {
  174. [MT9T031_CTRL_VFLIP] = {
  175. .id = V4L2_CID_VFLIP,
  176. .type = V4L2_CTRL_TYPE_BOOLEAN,
  177. .name = "Flip Vertically",
  178. .minimum = 0,
  179. .maximum = 1,
  180. .step = 1,
  181. .default_value = 0,
  182. },
  183. [MT9T031_CTRL_HFLIP] = {
  184. .id = V4L2_CID_HFLIP,
  185. .type = V4L2_CTRL_TYPE_BOOLEAN,
  186. .name = "Flip Horizontally",
  187. .minimum = 0,
  188. .maximum = 1,
  189. .step = 1,
  190. .default_value = 0,
  191. },
  192. [MT9T031_CTRL_GAIN] = {
  193. .id = V4L2_CID_GAIN,
  194. .type = V4L2_CTRL_TYPE_INTEGER,
  195. .name = "Gain",
  196. .minimum = 0,
  197. .maximum = 127,
  198. .step = 1,
  199. .default_value = 64,
  200. .flags = V4L2_CTRL_FLAG_SLIDER,
  201. },
  202. [MT9T031_CTRL_EXPOSURE] = {
  203. .id = V4L2_CID_EXPOSURE,
  204. .type = V4L2_CTRL_TYPE_INTEGER,
  205. .name = "Exposure",
  206. .minimum = 1,
  207. .maximum = 255,
  208. .step = 1,
  209. .default_value = 255,
  210. .flags = V4L2_CTRL_FLAG_SLIDER,
  211. },
  212. [MT9T031_CTRL_EXPOSURE_AUTO] = {
  213. .id = V4L2_CID_EXPOSURE_AUTO,
  214. .type = V4L2_CTRL_TYPE_BOOLEAN,
  215. .name = "Automatic Exposure",
  216. .minimum = 0,
  217. .maximum = 1,
  218. .step = 1,
  219. .default_value = 1,
  220. }
  221. };
  222. static struct soc_camera_ops mt9t031_ops = {
  223. .set_bus_param = mt9t031_set_bus_param,
  224. .query_bus_param = mt9t031_query_bus_param,
  225. .controls = mt9t031_controls,
  226. .num_controls = ARRAY_SIZE(mt9t031_controls),
  227. };
  228. /* target must be _even_ */
  229. static u16 mt9t031_skip(s32 *source, s32 target, s32 max)
  230. {
  231. unsigned int skip;
  232. if (*source < target + target / 2) {
  233. *source = target;
  234. return 1;
  235. }
  236. skip = min(max, *source + target / 2) / target;
  237. if (skip > 8)
  238. skip = 8;
  239. *source = target * skip;
  240. return skip;
  241. }
  242. /* rect is the sensor rectangle, the caller guarantees parameter validity */
  243. static int mt9t031_set_params(struct i2c_client *client,
  244. struct v4l2_rect *rect, u16 xskip, u16 yskip)
  245. {
  246. struct mt9t031 *mt9t031 = to_mt9t031(client);
  247. int ret;
  248. u16 xbin, ybin;
  249. const u16 hblank = MT9T031_HORIZONTAL_BLANK,
  250. vblank = MT9T031_VERTICAL_BLANK;
  251. xbin = min(xskip, (u16)3);
  252. ybin = min(yskip, (u16)3);
  253. /*
  254. * Could just do roundup(rect->left, [xy]bin * 2); but this is cheaper.
  255. * There is always a valid suitably aligned value. The worst case is
  256. * xbin = 3, width = 2048. Then we will start at 36, the last read out
  257. * pixel will be 2083, which is < 2085 - first black pixel.
  258. *
  259. * MT9T031 datasheet imposes window left border alignment, depending on
  260. * the selected xskip. Failing to conform to this requirement produces
  261. * dark horizontal stripes in the image. However, even obeying to this
  262. * requirement doesn't eliminate the stripes in all configurations. They
  263. * appear "locally reproducibly," but can differ between tests under
  264. * different lighting conditions.
  265. */
  266. switch (xbin) {
  267. case 1:
  268. rect->left &= ~1;
  269. break;
  270. case 2:
  271. rect->left &= ~3;
  272. break;
  273. case 3:
  274. rect->left = rect->left > roundup(MT9T031_COLUMN_SKIP, 6) ?
  275. (rect->left / 6) * 6 : roundup(MT9T031_COLUMN_SKIP, 6);
  276. }
  277. rect->top &= ~1;
  278. dev_dbg(&client->dev, "skip %u:%u, rect %ux%u@%u:%u\n",
  279. xskip, yskip, rect->width, rect->height, rect->left, rect->top);
  280. /* Disable register update, reconfigure atomically */
  281. ret = reg_set(client, MT9T031_OUTPUT_CONTROL, 1);
  282. if (ret < 0)
  283. return ret;
  284. /* Blanking and start values - default... */
  285. ret = reg_write(client, MT9T031_HORIZONTAL_BLANKING, hblank);
  286. if (ret >= 0)
  287. ret = reg_write(client, MT9T031_VERTICAL_BLANKING, vblank);
  288. if (yskip != mt9t031->yskip || xskip != mt9t031->xskip) {
  289. /* Binning, skipping */
  290. if (ret >= 0)
  291. ret = reg_write(client, MT9T031_COLUMN_ADDRESS_MODE,
  292. ((xbin - 1) << 4) | (xskip - 1));
  293. if (ret >= 0)
  294. ret = reg_write(client, MT9T031_ROW_ADDRESS_MODE,
  295. ((ybin - 1) << 4) | (yskip - 1));
  296. }
  297. dev_dbg(&client->dev, "new physical left %u, top %u\n",
  298. rect->left, rect->top);
  299. /*
  300. * The caller provides a supported format, as guaranteed by
  301. * icd->try_fmt_cap(), soc_camera_s_crop() and soc_camera_cropcap()
  302. */
  303. if (ret >= 0)
  304. ret = reg_write(client, MT9T031_COLUMN_START, rect->left);
  305. if (ret >= 0)
  306. ret = reg_write(client, MT9T031_ROW_START, rect->top);
  307. if (ret >= 0)
  308. ret = reg_write(client, MT9T031_WINDOW_WIDTH, rect->width - 1);
  309. if (ret >= 0)
  310. ret = reg_write(client, MT9T031_WINDOW_HEIGHT,
  311. rect->height + mt9t031->y_skip_top - 1);
  312. if (ret >= 0 && mt9t031->autoexposure) {
  313. unsigned int total_h = rect->height + mt9t031->y_skip_top + vblank;
  314. ret = set_shutter(client, total_h);
  315. if (ret >= 0) {
  316. const u32 shutter_max = MT9T031_MAX_HEIGHT + vblank;
  317. const struct v4l2_queryctrl *qctrl =
  318. &mt9t031_controls[MT9T031_CTRL_EXPOSURE];
  319. mt9t031->exposure = (shutter_max / 2 + (total_h - 1) *
  320. (qctrl->maximum - qctrl->minimum)) /
  321. shutter_max + qctrl->minimum;
  322. }
  323. }
  324. /* Re-enable register update, commit all changes */
  325. if (ret >= 0)
  326. ret = reg_clear(client, MT9T031_OUTPUT_CONTROL, 1);
  327. if (ret >= 0) {
  328. mt9t031->rect = *rect;
  329. mt9t031->xskip = xskip;
  330. mt9t031->yskip = yskip;
  331. }
  332. return ret < 0 ? ret : 0;
  333. }
  334. static int mt9t031_s_crop(struct v4l2_subdev *sd, struct v4l2_crop *a)
  335. {
  336. struct v4l2_rect rect = a->c;
  337. struct i2c_client *client = sd->priv;
  338. struct mt9t031 *mt9t031 = to_mt9t031(client);
  339. rect.width = ALIGN(rect.width, 2);
  340. rect.height = ALIGN(rect.height, 2);
  341. soc_camera_limit_side(&rect.left, &rect.width,
  342. MT9T031_COLUMN_SKIP, MT9T031_MIN_WIDTH, MT9T031_MAX_WIDTH);
  343. soc_camera_limit_side(&rect.top, &rect.height,
  344. MT9T031_ROW_SKIP, MT9T031_MIN_HEIGHT, MT9T031_MAX_HEIGHT);
  345. return mt9t031_set_params(client, &rect, mt9t031->xskip, mt9t031->yskip);
  346. }
  347. static int mt9t031_g_crop(struct v4l2_subdev *sd, struct v4l2_crop *a)
  348. {
  349. struct i2c_client *client = sd->priv;
  350. struct mt9t031 *mt9t031 = to_mt9t031(client);
  351. a->c = mt9t031->rect;
  352. a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  353. return 0;
  354. }
  355. static int mt9t031_cropcap(struct v4l2_subdev *sd, struct v4l2_cropcap *a)
  356. {
  357. a->bounds.left = MT9T031_COLUMN_SKIP;
  358. a->bounds.top = MT9T031_ROW_SKIP;
  359. a->bounds.width = MT9T031_MAX_WIDTH;
  360. a->bounds.height = MT9T031_MAX_HEIGHT;
  361. a->defrect = a->bounds;
  362. a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  363. a->pixelaspect.numerator = 1;
  364. a->pixelaspect.denominator = 1;
  365. return 0;
  366. }
  367. static int mt9t031_g_fmt(struct v4l2_subdev *sd,
  368. struct v4l2_mbus_framefmt *mf)
  369. {
  370. struct i2c_client *client = sd->priv;
  371. struct mt9t031 *mt9t031 = to_mt9t031(client);
  372. mf->width = mt9t031->rect.width / mt9t031->xskip;
  373. mf->height = mt9t031->rect.height / mt9t031->yskip;
  374. mf->code = V4L2_MBUS_FMT_SBGGR10_1X10;
  375. mf->colorspace = V4L2_COLORSPACE_SRGB;
  376. mf->field = V4L2_FIELD_NONE;
  377. return 0;
  378. }
  379. static int mt9t031_s_fmt(struct v4l2_subdev *sd,
  380. struct v4l2_mbus_framefmt *mf)
  381. {
  382. struct i2c_client *client = sd->priv;
  383. struct mt9t031 *mt9t031 = to_mt9t031(client);
  384. u16 xskip, yskip;
  385. struct v4l2_rect rect = mt9t031->rect;
  386. /*
  387. * try_fmt has put width and height within limits.
  388. * S_FMT: use binning and skipping for scaling
  389. */
  390. xskip = mt9t031_skip(&rect.width, mf->width, MT9T031_MAX_WIDTH);
  391. yskip = mt9t031_skip(&rect.height, mf->height, MT9T031_MAX_HEIGHT);
  392. mf->code = V4L2_MBUS_FMT_SBGGR10_1X10;
  393. mf->colorspace = V4L2_COLORSPACE_SRGB;
  394. /* mt9t031_set_params() doesn't change width and height */
  395. return mt9t031_set_params(client, &rect, xskip, yskip);
  396. }
  397. /*
  398. * If a user window larger than sensor window is requested, we'll increase the
  399. * sensor window.
  400. */
  401. static int mt9t031_try_fmt(struct v4l2_subdev *sd,
  402. struct v4l2_mbus_framefmt *mf)
  403. {
  404. v4l_bound_align_image(
  405. &mf->width, MT9T031_MIN_WIDTH, MT9T031_MAX_WIDTH, 1,
  406. &mf->height, MT9T031_MIN_HEIGHT, MT9T031_MAX_HEIGHT, 1, 0);
  407. mf->code = V4L2_MBUS_FMT_SBGGR10_1X10;
  408. mf->colorspace = V4L2_COLORSPACE_SRGB;
  409. return 0;
  410. }
  411. static int mt9t031_g_chip_ident(struct v4l2_subdev *sd,
  412. struct v4l2_dbg_chip_ident *id)
  413. {
  414. struct i2c_client *client = sd->priv;
  415. struct mt9t031 *mt9t031 = to_mt9t031(client);
  416. if (id->match.type != V4L2_CHIP_MATCH_I2C_ADDR)
  417. return -EINVAL;
  418. if (id->match.addr != client->addr)
  419. return -ENODEV;
  420. id->ident = mt9t031->model;
  421. id->revision = 0;
  422. return 0;
  423. }
  424. #ifdef CONFIG_VIDEO_ADV_DEBUG
  425. static int mt9t031_g_register(struct v4l2_subdev *sd,
  426. struct v4l2_dbg_register *reg)
  427. {
  428. struct i2c_client *client = sd->priv;
  429. if (reg->match.type != V4L2_CHIP_MATCH_I2C_ADDR || reg->reg > 0xff)
  430. return -EINVAL;
  431. if (reg->match.addr != client->addr)
  432. return -ENODEV;
  433. reg->val = reg_read(client, reg->reg);
  434. if (reg->val > 0xffff)
  435. return -EIO;
  436. return 0;
  437. }
  438. static int mt9t031_s_register(struct v4l2_subdev *sd,
  439. struct v4l2_dbg_register *reg)
  440. {
  441. struct i2c_client *client = sd->priv;
  442. if (reg->match.type != V4L2_CHIP_MATCH_I2C_ADDR || reg->reg > 0xff)
  443. return -EINVAL;
  444. if (reg->match.addr != client->addr)
  445. return -ENODEV;
  446. if (reg_write(client, reg->reg, reg->val) < 0)
  447. return -EIO;
  448. return 0;
  449. }
  450. #endif
  451. static int mt9t031_g_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
  452. {
  453. struct i2c_client *client = sd->priv;
  454. struct mt9t031 *mt9t031 = to_mt9t031(client);
  455. int data;
  456. switch (ctrl->id) {
  457. case V4L2_CID_VFLIP:
  458. data = reg_read(client, MT9T031_READ_MODE_2);
  459. if (data < 0)
  460. return -EIO;
  461. ctrl->value = !!(data & 0x8000);
  462. break;
  463. case V4L2_CID_HFLIP:
  464. data = reg_read(client, MT9T031_READ_MODE_2);
  465. if (data < 0)
  466. return -EIO;
  467. ctrl->value = !!(data & 0x4000);
  468. break;
  469. case V4L2_CID_EXPOSURE_AUTO:
  470. ctrl->value = mt9t031->autoexposure;
  471. break;
  472. case V4L2_CID_GAIN:
  473. ctrl->value = mt9t031->gain;
  474. break;
  475. case V4L2_CID_EXPOSURE:
  476. ctrl->value = mt9t031->exposure;
  477. break;
  478. }
  479. return 0;
  480. }
  481. static int mt9t031_s_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
  482. {
  483. struct i2c_client *client = sd->priv;
  484. struct mt9t031 *mt9t031 = to_mt9t031(client);
  485. const struct v4l2_queryctrl *qctrl;
  486. int data;
  487. switch (ctrl->id) {
  488. case V4L2_CID_VFLIP:
  489. if (ctrl->value)
  490. data = reg_set(client, MT9T031_READ_MODE_2, 0x8000);
  491. else
  492. data = reg_clear(client, MT9T031_READ_MODE_2, 0x8000);
  493. if (data < 0)
  494. return -EIO;
  495. break;
  496. case V4L2_CID_HFLIP:
  497. if (ctrl->value)
  498. data = reg_set(client, MT9T031_READ_MODE_2, 0x4000);
  499. else
  500. data = reg_clear(client, MT9T031_READ_MODE_2, 0x4000);
  501. if (data < 0)
  502. return -EIO;
  503. break;
  504. case V4L2_CID_GAIN:
  505. qctrl = &mt9t031_controls[MT9T031_CTRL_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. qctrl = &mt9t031_controls[MT9T031_CTRL_EXPOSURE];
  542. /* mt9t031 has maximum == default */
  543. if (ctrl->value > qctrl->maximum || ctrl->value < qctrl->minimum)
  544. return -EINVAL;
  545. else {
  546. const unsigned long range = qctrl->maximum - qctrl->minimum;
  547. const u32 shutter = ((ctrl->value - qctrl->minimum) * 1048 +
  548. range / 2) / range + 1;
  549. u32 old;
  550. get_shutter(client, &old);
  551. dev_dbg(&client->dev, "Set shutter from %u to %u\n",
  552. old, shutter);
  553. if (set_shutter(client, shutter) < 0)
  554. return -EIO;
  555. mt9t031->exposure = ctrl->value;
  556. mt9t031->autoexposure = 0;
  557. }
  558. break;
  559. case V4L2_CID_EXPOSURE_AUTO:
  560. if (ctrl->value) {
  561. const u16 vblank = MT9T031_VERTICAL_BLANK;
  562. const u32 shutter_max = MT9T031_MAX_HEIGHT + vblank;
  563. unsigned int total_h = mt9t031->rect.height +
  564. mt9t031->y_skip_top + vblank;
  565. if (set_shutter(client, total_h) < 0)
  566. return -EIO;
  567. qctrl = &mt9t031_controls[MT9T031_CTRL_EXPOSURE];
  568. mt9t031->exposure = (shutter_max / 2 + (total_h - 1) *
  569. (qctrl->maximum - qctrl->minimum)) /
  570. shutter_max + qctrl->minimum;
  571. mt9t031->autoexposure = 1;
  572. } else
  573. mt9t031->autoexposure = 0;
  574. break;
  575. default:
  576. return -EINVAL;
  577. }
  578. return 0;
  579. }
  580. /*
  581. * Interface active, can use i2c. If it fails, it can indeed mean, that
  582. * this wasn't our capture interface, so, we wait for the right one
  583. */
  584. static int mt9t031_video_probe(struct i2c_client *client)
  585. {
  586. struct mt9t031 *mt9t031 = to_mt9t031(client);
  587. s32 data;
  588. int ret;
  589. /* Enable the chip */
  590. data = reg_write(client, MT9T031_CHIP_ENABLE, 1);
  591. dev_dbg(&client->dev, "write: %d\n", data);
  592. /* Read out the chip version register */
  593. data = reg_read(client, MT9T031_CHIP_VERSION);
  594. switch (data) {
  595. case 0x1621:
  596. mt9t031->model = V4L2_IDENT_MT9T031;
  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 int mt9t031_enum_fmt(struct v4l2_subdev *sd, int index,
  629. enum v4l2_mbus_pixelcode *code)
  630. {
  631. if (index)
  632. return -EINVAL;
  633. *code = V4L2_MBUS_FMT_SBGGR10_1X10;
  634. return 0;
  635. }
  636. static struct v4l2_subdev_video_ops mt9t031_subdev_video_ops = {
  637. .s_stream = mt9t031_s_stream,
  638. .s_mbus_fmt = mt9t031_s_fmt,
  639. .g_mbus_fmt = mt9t031_g_fmt,
  640. .try_mbus_fmt = mt9t031_try_fmt,
  641. .s_crop = mt9t031_s_crop,
  642. .g_crop = mt9t031_g_crop,
  643. .cropcap = mt9t031_cropcap,
  644. .enum_mbus_fmt = mt9t031_enum_fmt,
  645. };
  646. static struct v4l2_subdev_sensor_ops mt9t031_subdev_sensor_ops = {
  647. .g_skip_top_lines = mt9t031_g_skip_top_lines,
  648. };
  649. static struct v4l2_subdev_ops mt9t031_subdev_ops = {
  650. .core = &mt9t031_subdev_core_ops,
  651. .video = &mt9t031_subdev_video_ops,
  652. .sensor = &mt9t031_subdev_sensor_ops,
  653. };
  654. static int mt9t031_probe(struct i2c_client *client,
  655. const struct i2c_device_id *did)
  656. {
  657. struct mt9t031 *mt9t031;
  658. struct soc_camera_device *icd = client->dev.platform_data;
  659. struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
  660. int ret;
  661. if (icd) {
  662. struct soc_camera_link *icl = to_soc_camera_link(icd);
  663. if (!icl) {
  664. dev_err(&client->dev, "MT9T031 driver needs platform data\n");
  665. return -EINVAL;
  666. }
  667. icd->ops = &mt9t031_ops;
  668. }
  669. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WORD_DATA)) {
  670. dev_warn(&adapter->dev,
  671. "I2C-Adapter doesn't support I2C_FUNC_SMBUS_WORD\n");
  672. return -EIO;
  673. }
  674. mt9t031 = kzalloc(sizeof(struct mt9t031), GFP_KERNEL);
  675. if (!mt9t031)
  676. return -ENOMEM;
  677. v4l2_i2c_subdev_init(&mt9t031->subdev, client, &mt9t031_subdev_ops);
  678. mt9t031->y_skip_top = 0;
  679. mt9t031->rect.left = MT9T031_COLUMN_SKIP;
  680. mt9t031->rect.top = MT9T031_ROW_SKIP;
  681. mt9t031->rect.width = MT9T031_MAX_WIDTH;
  682. mt9t031->rect.height = MT9T031_MAX_HEIGHT;
  683. /*
  684. * Simulated autoexposure. If enabled, we calculate shutter width
  685. * ourselves in the driver based on vertical blanking and frame width
  686. */
  687. mt9t031->autoexposure = 1;
  688. mt9t031->xskip = 1;
  689. mt9t031->yskip = 1;
  690. mt9t031_idle(client);
  691. ret = mt9t031_video_probe(client);
  692. mt9t031_disable(client);
  693. if (ret) {
  694. if (icd)
  695. icd->ops = NULL;
  696. i2c_set_clientdata(client, NULL);
  697. kfree(mt9t031);
  698. }
  699. return ret;
  700. }
  701. static int mt9t031_remove(struct i2c_client *client)
  702. {
  703. struct mt9t031 *mt9t031 = to_mt9t031(client);
  704. struct soc_camera_device *icd = client->dev.platform_data;
  705. if (icd)
  706. icd->ops = NULL;
  707. i2c_set_clientdata(client, NULL);
  708. client->driver = NULL;
  709. kfree(mt9t031);
  710. return 0;
  711. }
  712. static const struct i2c_device_id mt9t031_id[] = {
  713. { "mt9t031", 0 },
  714. { }
  715. };
  716. MODULE_DEVICE_TABLE(i2c, mt9t031_id);
  717. static struct i2c_driver mt9t031_i2c_driver = {
  718. .driver = {
  719. .name = "mt9t031",
  720. },
  721. .probe = mt9t031_probe,
  722. .remove = mt9t031_remove,
  723. .id_table = mt9t031_id,
  724. };
  725. static int __init mt9t031_mod_init(void)
  726. {
  727. return i2c_add_driver(&mt9t031_i2c_driver);
  728. }
  729. static void __exit mt9t031_mod_exit(void)
  730. {
  731. i2c_del_driver(&mt9t031_i2c_driver);
  732. }
  733. module_init(mt9t031_mod_init);
  734. module_exit(mt9t031_mod_exit);
  735. MODULE_DESCRIPTION("Micron MT9T031 Camera driver");
  736. MODULE_AUTHOR("Guennadi Liakhovetski <lg@denx.de>");
  737. MODULE_LICENSE("GPL v2");