m5mols_capture.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*
  2. * The Capture code for Fujitsu M-5MOLS ISP
  3. *
  4. * Copyright (C) 2011 Samsung Electronics Co., Ltd.
  5. * Author: HeungJun Kim <riverful.kim@samsung.com>
  6. *
  7. * Copyright (C) 2009 Samsung Electronics Co., Ltd.
  8. * Author: Dongsoo Nathaniel Kim <dongsoo45.kim@samsung.com>
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. */
  15. #include <linux/i2c.h>
  16. #include <linux/slab.h>
  17. #include <linux/irq.h>
  18. #include <linux/interrupt.h>
  19. #include <linux/delay.h>
  20. #include <linux/gpio.h>
  21. #include <linux/regulator/consumer.h>
  22. #include <linux/videodev2.h>
  23. #include <media/v4l2-ctrls.h>
  24. #include <media/v4l2-device.h>
  25. #include <media/v4l2-subdev.h>
  26. #include <media/m5mols.h>
  27. #include <media/exynos-fimc.h>
  28. #include "m5mols.h"
  29. #include "m5mols_reg.h"
  30. /**
  31. * m5mols_read_rational - I2C read of a rational number
  32. *
  33. * Read numerator and denominator from registers @addr_num and @addr_den
  34. * respectively and return the division result in @val.
  35. */
  36. static int m5mols_read_rational(struct v4l2_subdev *sd, u32 addr_num,
  37. u32 addr_den, u32 *val)
  38. {
  39. u32 num, den;
  40. int ret = m5mols_read_u32(sd, addr_num, &num);
  41. if (!ret)
  42. ret = m5mols_read_u32(sd, addr_den, &den);
  43. if (ret)
  44. return ret;
  45. *val = den == 0 ? 0 : num / den;
  46. return ret;
  47. }
  48. /**
  49. * m5mols_capture_info - Gather captured image information
  50. *
  51. * For now it gathers only EXIF information and file size.
  52. */
  53. static int m5mols_capture_info(struct m5mols_info *info)
  54. {
  55. struct m5mols_exif *exif = &info->cap.exif;
  56. struct v4l2_subdev *sd = &info->sd;
  57. int ret;
  58. ret = m5mols_read_rational(sd, EXIF_INFO_EXPTIME_NU,
  59. EXIF_INFO_EXPTIME_DE, &exif->exposure_time);
  60. if (ret)
  61. return ret;
  62. ret = m5mols_read_rational(sd, EXIF_INFO_TV_NU, EXIF_INFO_TV_DE,
  63. &exif->shutter_speed);
  64. if (ret)
  65. return ret;
  66. ret = m5mols_read_rational(sd, EXIF_INFO_AV_NU, EXIF_INFO_AV_DE,
  67. &exif->aperture);
  68. if (ret)
  69. return ret;
  70. ret = m5mols_read_rational(sd, EXIF_INFO_BV_NU, EXIF_INFO_BV_DE,
  71. &exif->brightness);
  72. if (ret)
  73. return ret;
  74. ret = m5mols_read_rational(sd, EXIF_INFO_EBV_NU, EXIF_INFO_EBV_DE,
  75. &exif->exposure_bias);
  76. if (ret)
  77. return ret;
  78. ret = m5mols_read_u16(sd, EXIF_INFO_ISO, &exif->iso_speed);
  79. if (!ret)
  80. ret = m5mols_read_u16(sd, EXIF_INFO_FLASH, &exif->flash);
  81. if (!ret)
  82. ret = m5mols_read_u16(sd, EXIF_INFO_SDR, &exif->sdr);
  83. if (!ret)
  84. ret = m5mols_read_u16(sd, EXIF_INFO_QVAL, &exif->qval);
  85. if (ret)
  86. return ret;
  87. if (!ret)
  88. ret = m5mols_read_u32(sd, CAPC_IMAGE_SIZE, &info->cap.main);
  89. if (!ret)
  90. ret = m5mols_read_u32(sd, CAPC_THUMB_SIZE, &info->cap.thumb);
  91. if (!ret)
  92. info->cap.total = info->cap.main + info->cap.thumb;
  93. return ret;
  94. }
  95. int m5mols_start_capture(struct m5mols_info *info)
  96. {
  97. unsigned int framesize = info->cap.buf_size - M5MOLS_JPEG_TAGS_SIZE;
  98. struct v4l2_subdev *sd = &info->sd;
  99. int ret;
  100. /*
  101. * Synchronize the controls, set the capture frame resolution and color
  102. * format. The frame capture is initiated during switching from Monitor
  103. * to Capture mode.
  104. */
  105. ret = m5mols_set_mode(info, REG_MONITOR);
  106. if (!ret)
  107. ret = m5mols_restore_controls(info);
  108. if (!ret)
  109. ret = m5mols_write(sd, CAPP_YUVOUT_MAIN, REG_JPEG);
  110. if (!ret)
  111. ret = m5mols_write(sd, CAPP_MAIN_IMAGE_SIZE, info->resolution);
  112. if (!ret)
  113. ret = m5mols_write(sd, CAPP_JPEG_SIZE_MAX, framesize);
  114. if (!ret)
  115. ret = m5mols_set_mode(info, REG_CAPTURE);
  116. if (!ret)
  117. /* Wait until a frame is captured to ISP internal memory */
  118. ret = m5mols_wait_interrupt(sd, REG_INT_CAPTURE, 2000);
  119. if (ret)
  120. return ret;
  121. /*
  122. * Initiate the captured data transfer to a MIPI-CSI receiver.
  123. */
  124. ret = m5mols_write(sd, CAPC_SEL_FRAME, 1);
  125. if (!ret)
  126. ret = m5mols_write(sd, CAPC_START, REG_CAP_START_MAIN);
  127. if (!ret) {
  128. bool captured = false;
  129. unsigned int size;
  130. /* Wait for the capture completion interrupt */
  131. ret = m5mols_wait_interrupt(sd, REG_INT_CAPTURE, 2000);
  132. if (!ret) {
  133. captured = true;
  134. ret = m5mols_capture_info(info);
  135. }
  136. size = captured ? info->cap.main : 0;
  137. v4l2_dbg(1, m5mols_debug, sd, "%s: size: %d, thumb.: %d B\n",
  138. __func__, size, info->cap.thumb);
  139. v4l2_subdev_notify(sd, S5P_FIMC_TX_END_NOTIFY, &size);
  140. }
  141. return ret;
  142. }