/drivers/media/video/mt9t031.c

https://bitbucket.org/ndreys/linux-sunxi · C · 934 lines · 702 code · 151 blank · 81 comment · 81 complexity · 58427ad11c130a19bccb4808d137a7c6 MD5 · raw file

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