camif-core.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. /*
  2. * s3c24xx/s3c64xx SoC series Camera Interface (CAMIF) driver
  3. *
  4. * Copyright (C) 2012 Sylwester Nawrocki <sylvester.nawrocki@gmail.com>
  5. * Copyright (C) 2012 Tomasz Figa <tomasz.figa@gmail.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. */
  11. #ifndef CAMIF_CORE_H_
  12. #define CAMIF_CORE_H_
  13. #include <linux/io.h>
  14. #include <linux/irq.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/sched.h>
  17. #include <linux/spinlock.h>
  18. #include <linux/types.h>
  19. #include <linux/videodev2.h>
  20. #include <media/media-entity.h>
  21. #include <media/v4l2-ctrls.h>
  22. #include <media/v4l2-dev.h>
  23. #include <media/v4l2-device.h>
  24. #include <media/v4l2-mediabus.h>
  25. #include <media/videobuf2-v4l2.h>
  26. #include <media/s3c_camif.h>
  27. #define S3C_CAMIF_DRIVER_NAME "s3c-camif"
  28. #define CAMIF_REQ_BUFS_MIN 3
  29. #define CAMIF_MAX_OUT_BUFS 4
  30. #define CAMIF_MAX_PIX_WIDTH 4096
  31. #define CAMIF_MAX_PIX_HEIGHT 4096
  32. #define SCALER_MAX_RATIO 64
  33. #define CAMIF_DEF_WIDTH 640
  34. #define CAMIF_DEF_HEIGHT 480
  35. #define CAMIF_STOP_TIMEOUT 1500 /* ms */
  36. #define S3C244X_CAMIF_IP_REV 0x20 /* 2.0 */
  37. #define S3C2450_CAMIF_IP_REV 0x30 /* 3.0 - not implemented, not tested */
  38. #define S3C6400_CAMIF_IP_REV 0x31 /* 3.1 - not implemented, not tested */
  39. #define S3C6410_CAMIF_IP_REV 0x32 /* 3.2 */
  40. /* struct camif_vp::state */
  41. #define ST_VP_PENDING (1 << 0)
  42. #define ST_VP_RUNNING (1 << 1)
  43. #define ST_VP_STREAMING (1 << 2)
  44. #define ST_VP_SENSOR_STREAMING (1 << 3)
  45. #define ST_VP_ABORTING (1 << 4)
  46. #define ST_VP_OFF (1 << 5)
  47. #define ST_VP_LASTIRQ (1 << 6)
  48. #define ST_VP_CONFIG (1 << 8)
  49. #define CAMIF_SD_PAD_SINK 0
  50. #define CAMIF_SD_PAD_SOURCE_C 1
  51. #define CAMIF_SD_PAD_SOURCE_P 2
  52. #define CAMIF_SD_PADS_NUM 3
  53. enum img_fmt {
  54. IMG_FMT_RGB565 = 0x0010,
  55. IMG_FMT_RGB666,
  56. IMG_FMT_XRGB8888,
  57. IMG_FMT_YCBCR420 = 0x0020,
  58. IMG_FMT_YCRCB420,
  59. IMG_FMT_YCBCR422P,
  60. IMG_FMT_YCBYCR422 = 0x0040,
  61. IMG_FMT_YCRYCB422,
  62. IMG_FMT_CBYCRY422,
  63. IMG_FMT_CRYCBY422,
  64. };
  65. #define img_fmt_is_rgb(x) ((x) & 0x10)
  66. #define img_fmt_is_ycbcr(x) ((x) & 0x60)
  67. /* Possible values for struct camif_fmt::flags */
  68. #define FMT_FL_S3C24XX_CODEC (1 << 0)
  69. #define FMT_FL_S3C24XX_PREVIEW (1 << 1)
  70. #define FMT_FL_S3C64XX (1 << 2)
  71. /**
  72. * struct camif_fmt - pixel format description
  73. * @fourcc: fourcc code for this format, 0 if not applicable
  74. * @color: a corresponding enum img_fmt
  75. * @colplanes: number of physically contiguous data planes
  76. * @flags: indicate for which SoCs revisions this format is valid
  77. * @depth: bits per pixel (total)
  78. * @ybpp: number of luminance bytes per pixel
  79. */
  80. struct camif_fmt {
  81. char *name;
  82. u32 fourcc;
  83. u32 color;
  84. u16 colplanes;
  85. u16 flags;
  86. u8 depth;
  87. u8 ybpp;
  88. };
  89. /**
  90. * struct camif_dma_offset - pixel offset information for DMA
  91. * @initial: offset (in pixels) to first pixel
  92. * @line: offset (in pixels) from end of line to start of next line
  93. */
  94. struct camif_dma_offset {
  95. int initial;
  96. int line;
  97. };
  98. /**
  99. * struct camif_frame - source/target frame properties
  100. * @f_width: full pixel width
  101. * @f_height: full pixel height
  102. * @rect: crop/composition rectangle
  103. * @dma_offset: DMA offset configuration
  104. */
  105. struct camif_frame {
  106. u16 f_width;
  107. u16 f_height;
  108. struct v4l2_rect rect;
  109. struct camif_dma_offset dma_offset;
  110. };
  111. /* CAMIF clocks enumeration */
  112. enum {
  113. CLK_GATE,
  114. CLK_CAM,
  115. CLK_MAX_NUM,
  116. };
  117. struct vp_pix_limits {
  118. u16 max_out_width;
  119. u16 max_sc_out_width;
  120. u16 out_width_align;
  121. u16 max_height;
  122. u8 min_out_width;
  123. u16 out_hor_offset_align;
  124. };
  125. struct camif_pix_limits {
  126. u16 win_hor_offset_align;
  127. };
  128. /**
  129. * struct s3c_camif_variant - CAMIF variant structure
  130. * @vp_pix_limits: pixel limits for the codec and preview paths
  131. * @camif_pix_limits: pixel limits for the camera input interface
  132. * @ip_revision: the CAMIF IP revision: 0x20 for s3c244x, 0x32 for s3c6410
  133. */
  134. struct s3c_camif_variant {
  135. struct vp_pix_limits vp_pix_limits[2];
  136. struct camif_pix_limits pix_limits;
  137. u8 ip_revision;
  138. u8 has_img_effect;
  139. unsigned int vp_offset;
  140. };
  141. struct s3c_camif_drvdata {
  142. const struct s3c_camif_variant *variant;
  143. unsigned long bus_clk_freq;
  144. };
  145. struct camif_scaler {
  146. u8 scaleup_h;
  147. u8 scaleup_v;
  148. u8 copy;
  149. u8 enable;
  150. u32 h_shift;
  151. u32 v_shift;
  152. u32 pre_h_ratio;
  153. u32 pre_v_ratio;
  154. u32 pre_dst_width;
  155. u32 pre_dst_height;
  156. u32 main_h_ratio;
  157. u32 main_v_ratio;
  158. };
  159. struct camif_dev;
  160. /**
  161. * struct camif_vp - CAMIF data processing path structure (codec/preview)
  162. * @irq_queue: interrupt handling waitqueue
  163. * @irq: interrupt number for this data path
  164. * @camif: pointer to the camif structure
  165. * @pad: media pad for the video node
  166. * @vdev video device
  167. * @ctrl_handler: video node controls handler
  168. * @owner: file handle that own the streaming
  169. * @pending_buf_q: pending (empty) buffers queue head
  170. * @active_buf_q: active (being written) buffers queue head
  171. * @active_buffers: counter of buffer set up at the DMA engine
  172. * @buf_index: identifier of a last empty buffer set up in H/W
  173. * @frame_sequence: image frame sequence counter
  174. * @reqbufs_count: the number of buffers requested
  175. * @scaler: the scaler structure
  176. * @out_fmt: pixel format at this video path output
  177. * @payload: the output data frame payload size
  178. * @out_frame: the output pixel resolution
  179. * @state: the video path's state
  180. * @fmt_flags: flags determining supported pixel formats
  181. * @id: CAMIF id, 0 - codec, 1 - preview
  182. * @rotation: current image rotation value
  183. * @hflip: apply horizontal flip if set
  184. * @vflip: apply vertical flip if set
  185. */
  186. struct camif_vp {
  187. wait_queue_head_t irq_queue;
  188. int irq;
  189. struct camif_dev *camif;
  190. struct media_pad pad;
  191. struct video_device vdev;
  192. struct v4l2_ctrl_handler ctrl_handler;
  193. struct v4l2_fh *owner;
  194. struct vb2_queue vb_queue;
  195. struct list_head pending_buf_q;
  196. struct list_head active_buf_q;
  197. unsigned int active_buffers;
  198. unsigned int buf_index;
  199. unsigned int frame_sequence;
  200. unsigned int reqbufs_count;
  201. struct camif_scaler scaler;
  202. const struct camif_fmt *out_fmt;
  203. unsigned int payload;
  204. struct camif_frame out_frame;
  205. unsigned int state;
  206. u16 fmt_flags;
  207. u8 id;
  208. u16 rotation;
  209. u8 hflip;
  210. u8 vflip;
  211. unsigned int offset;
  212. };
  213. /* Video processing path enumeration */
  214. #define VP_CODEC 0
  215. #define VP_PREVIEW 1
  216. #define CAMIF_VP_NUM 2
  217. /**
  218. * struct camif_dev - the CAMIF driver private data structure
  219. * @media_dev: top-level media device structure
  220. * @v4l2_dev: root v4l2_device
  221. * @subdev: camera interface ("catchcam") subdev
  222. * @mbus_fmt: camera input media bus format
  223. * @camif_crop: camera input interface crop rectangle
  224. * @pads: the camif subdev's media pads
  225. * @stream_count: the camera interface streaming reference counter
  226. * @sensor: image sensor data structure
  227. * @m_pipeline: video entity pipeline description
  228. * @ctrl_handler: v4l2 control handler (owned by @subdev)
  229. * @test_pattern: test pattern controls
  230. * @vp: video path (DMA) description (codec/preview)
  231. * @alloc_ctx: memory buffer allocator context
  232. * @variant: variant information for this device
  233. * @dev: pointer to the CAMIF device struct
  234. * @pdata: a copy of the driver's platform data
  235. * @clock: clocks required for the CAMIF operation
  236. * @lock: mutex protecting this data structure
  237. * @slock: spinlock protecting CAMIF registers
  238. * @io_base: start address of the mmaped CAMIF registers
  239. */
  240. struct camif_dev {
  241. struct media_device media_dev;
  242. struct v4l2_device v4l2_dev;
  243. struct v4l2_subdev subdev;
  244. struct v4l2_mbus_framefmt mbus_fmt;
  245. struct v4l2_rect camif_crop;
  246. struct media_pad pads[CAMIF_SD_PADS_NUM];
  247. int stream_count;
  248. struct cam_sensor {
  249. struct v4l2_subdev *sd;
  250. short power_count;
  251. short stream_count;
  252. } sensor;
  253. struct media_pipeline *m_pipeline;
  254. struct v4l2_ctrl_handler ctrl_handler;
  255. struct v4l2_ctrl *ctrl_test_pattern;
  256. struct {
  257. struct v4l2_ctrl *ctrl_colorfx;
  258. struct v4l2_ctrl *ctrl_colorfx_cbcr;
  259. };
  260. u8 test_pattern;
  261. u8 colorfx;
  262. u8 colorfx_cb;
  263. u8 colorfx_cr;
  264. struct camif_vp vp[CAMIF_VP_NUM];
  265. struct vb2_alloc_ctx *alloc_ctx;
  266. const struct s3c_camif_variant *variant;
  267. struct device *dev;
  268. struct s3c_camif_plat_data pdata;
  269. struct clk *clock[CLK_MAX_NUM];
  270. struct mutex lock;
  271. spinlock_t slock;
  272. void __iomem *io_base;
  273. };
  274. /**
  275. * struct camif_addr - Y/Cb/Cr DMA start address structure
  276. * @y: luminance plane dma address
  277. * @cb: Cb plane dma address
  278. * @cr: Cr plane dma address
  279. */
  280. struct camif_addr {
  281. dma_addr_t y;
  282. dma_addr_t cb;
  283. dma_addr_t cr;
  284. };
  285. /**
  286. * struct camif_buffer - the camif video buffer structure
  287. * @vb: vb2 buffer
  288. * @list: list head for the buffers queue
  289. * @paddr: DMA start addresses
  290. * @index: an identifier of this buffer at the DMA engine
  291. */
  292. struct camif_buffer {
  293. struct vb2_v4l2_buffer vb;
  294. struct list_head list;
  295. struct camif_addr paddr;
  296. unsigned int index;
  297. };
  298. const struct camif_fmt *s3c_camif_find_format(struct camif_vp *vp,
  299. const u32 *pixelformat, int index);
  300. int s3c_camif_register_video_node(struct camif_dev *camif, int idx);
  301. void s3c_camif_unregister_video_node(struct camif_dev *camif, int idx);
  302. irqreturn_t s3c_camif_irq_handler(int irq, void *priv);
  303. int s3c_camif_create_subdev(struct camif_dev *camif);
  304. void s3c_camif_unregister_subdev(struct camif_dev *camif);
  305. int s3c_camif_set_defaults(struct camif_dev *camif);
  306. int s3c_camif_get_scaler_config(struct camif_vp *vp,
  307. struct camif_scaler *scaler);
  308. static inline void camif_active_queue_add(struct camif_vp *vp,
  309. struct camif_buffer *buf)
  310. {
  311. list_add_tail(&buf->list, &vp->active_buf_q);
  312. vp->active_buffers++;
  313. }
  314. static inline struct camif_buffer *camif_active_queue_pop(
  315. struct camif_vp *vp)
  316. {
  317. struct camif_buffer *buf = list_first_entry(&vp->active_buf_q,
  318. struct camif_buffer, list);
  319. list_del(&buf->list);
  320. vp->active_buffers--;
  321. return buf;
  322. }
  323. static inline struct camif_buffer *camif_active_queue_peek(
  324. struct camif_vp *vp, int index)
  325. {
  326. struct camif_buffer *tmp, *buf;
  327. if (WARN_ON(list_empty(&vp->active_buf_q)))
  328. return NULL;
  329. list_for_each_entry_safe(buf, tmp, &vp->active_buf_q, list) {
  330. if (buf->index == index) {
  331. list_del(&buf->list);
  332. vp->active_buffers--;
  333. return buf;
  334. }
  335. }
  336. return NULL;
  337. }
  338. static inline void camif_pending_queue_add(struct camif_vp *vp,
  339. struct camif_buffer *buf)
  340. {
  341. list_add_tail(&buf->list, &vp->pending_buf_q);
  342. }
  343. static inline struct camif_buffer *camif_pending_queue_pop(
  344. struct camif_vp *vp)
  345. {
  346. struct camif_buffer *buf = list_first_entry(&vp->pending_buf_q,
  347. struct camif_buffer, list);
  348. list_del(&buf->list);
  349. return buf;
  350. }
  351. #endif /* CAMIF_CORE_H_ */