m5mols_capture.c 5.2 KB

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