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