m5mols_capture.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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 "m5mols.h"
  28. #include "m5mols_reg.h"
  29. /**
  30. * m5mols_read_rational - I2C read of a rational number
  31. *
  32. * Read numerator and denominator from registers @addr_num and @addr_den
  33. * respectively and return the division result in @val.
  34. */
  35. static int m5mols_read_rational(struct v4l2_subdev *sd, u32 addr_num,
  36. u32 addr_den, u32 *val)
  37. {
  38. u32 num, den;
  39. int ret = m5mols_read_u32(sd, addr_num, &num);
  40. if (!ret)
  41. ret = m5mols_read_u32(sd, addr_den, &den);
  42. if (ret)
  43. return ret;
  44. *val = den == 0 ? 0 : num / den;
  45. return ret;
  46. }
  47. /**
  48. * m5mols_capture_info - Gather captured image information
  49. *
  50. * For now it gathers only EXIF information and file size.
  51. */
  52. static int m5mols_capture_info(struct m5mols_info *info)
  53. {
  54. struct m5mols_exif *exif = &info->cap.exif;
  55. struct v4l2_subdev *sd = &info->sd;
  56. int ret;
  57. ret = m5mols_read_rational(sd, EXIF_INFO_EXPTIME_NU,
  58. EXIF_INFO_EXPTIME_DE, &exif->exposure_time);
  59. if (ret)
  60. return ret;
  61. ret = m5mols_read_rational(sd, EXIF_INFO_TV_NU, EXIF_INFO_TV_DE,
  62. &exif->shutter_speed);
  63. if (ret)
  64. return ret;
  65. ret = m5mols_read_rational(sd, EXIF_INFO_AV_NU, EXIF_INFO_AV_DE,
  66. &exif->aperture);
  67. if (ret)
  68. return ret;
  69. ret = m5mols_read_rational(sd, EXIF_INFO_BV_NU, EXIF_INFO_BV_DE,
  70. &exif->brightness);
  71. if (ret)
  72. return ret;
  73. ret = m5mols_read_rational(sd, EXIF_INFO_EBV_NU, EXIF_INFO_EBV_DE,
  74. &exif->exposure_bias);
  75. if (ret)
  76. return ret;
  77. ret = m5mols_read_u16(sd, EXIF_INFO_ISO, &exif->iso_speed);
  78. if (!ret)
  79. ret = m5mols_read_u16(sd, EXIF_INFO_FLASH, &exif->flash);
  80. if (!ret)
  81. ret = m5mols_read_u16(sd, EXIF_INFO_SDR, &exif->sdr);
  82. if (!ret)
  83. ret = m5mols_read_u16(sd, EXIF_INFO_QVAL, &exif->qval);
  84. if (ret)
  85. return ret;
  86. if (!ret)
  87. ret = m5mols_read_u32(sd, CAPC_IMAGE_SIZE, &info->cap.main);
  88. if (!ret)
  89. ret = m5mols_read_u32(sd, CAPC_THUMB_SIZE, &info->cap.thumb);
  90. if (!ret)
  91. info->cap.total = info->cap.main + info->cap.thumb;
  92. return ret;
  93. }
  94. int m5mols_start_capture(struct m5mols_info *info)
  95. {
  96. struct v4l2_subdev *sd = &info->sd;
  97. u8 resolution = info->resolution;
  98. int ret;
  99. /*
  100. * Preparing capture. Setting control & interrupt before entering
  101. * capture mode
  102. *
  103. * 1) change to MONITOR mode for operating control & interrupt
  104. * 2) set controls (considering v4l2_control value & lock 3A)
  105. * 3) set interrupt
  106. * 4) change to CAPTURE mode
  107. */
  108. ret = m5mols_mode(info, REG_MONITOR);
  109. if (!ret)
  110. ret = m5mols_sync_controls(info);
  111. if (!ret)
  112. ret = m5mols_lock_3a(info, true);
  113. if (!ret)
  114. ret = m5mols_enable_interrupt(sd, REG_INT_CAPTURE);
  115. if (!ret)
  116. ret = m5mols_mode(info, REG_CAPTURE);
  117. if (!ret)
  118. /* Wait for capture interrupt, after changing capture mode */
  119. ret = m5mols_wait_interrupt(sd, REG_INT_CAPTURE, 2000);
  120. if (!ret)
  121. ret = m5mols_lock_3a(info, false);
  122. if (ret)
  123. return ret;
  124. /*
  125. * Starting capture. Setting capture frame count and resolution and
  126. * the format(available format: JPEG, Bayer RAW, YUV).
  127. *
  128. * 1) select single or multi(enable to 25), format, size
  129. * 2) set interrupt
  130. * 3) start capture(for main image, now)
  131. * 4) get information
  132. * 5) notify file size to v4l2 device(e.g, to s5p-fimc v4l2 device)
  133. */
  134. ret = m5mols_write(sd, CAPC_SEL_FRAME, 1);
  135. if (!ret)
  136. ret = m5mols_write(sd, CAPP_YUVOUT_MAIN, REG_JPEG);
  137. if (!ret)
  138. ret = m5mols_write(sd, CAPP_MAIN_IMAGE_SIZE, resolution);
  139. if (!ret)
  140. ret = m5mols_enable_interrupt(sd, REG_INT_CAPTURE);
  141. if (!ret)
  142. ret = m5mols_write(sd, CAPC_START, REG_CAP_START_MAIN);
  143. if (!ret) {
  144. /* Wait for the capture completion interrupt */
  145. ret = m5mols_wait_interrupt(sd, REG_INT_CAPTURE, 2000);
  146. if (!ret) {
  147. ret = m5mols_capture_info(info);
  148. if (!ret)
  149. v4l2_subdev_notify(sd, 0, &info->cap.total);
  150. }
  151. }
  152. return ret;
  153. }