fimc-core.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  1. /*
  2. * Copyright (c) 2010 Samsung Electronics
  3. *
  4. * Sylwester Nawrocki, <s.nawrocki@samsung.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. */
  10. #ifndef FIMC_CORE_H_
  11. #define FIMC_CORE_H_
  12. #include <linux/types.h>
  13. #include <media/videobuf-core.h>
  14. #include <media/v4l2-device.h>
  15. #include <media/v4l2-mem2mem.h>
  16. #include <linux/videodev2.h>
  17. #include "regs-fimc.h"
  18. #define err(fmt, args...) \
  19. printk(KERN_ERR "%s:%d: " fmt "\n", __func__, __LINE__, ##args)
  20. #ifdef DEBUG
  21. #define dbg(fmt, args...) \
  22. printk(KERN_DEBUG "%s:%d: " fmt "\n", __func__, __LINE__, ##args)
  23. #else
  24. #define dbg(fmt, args...)
  25. #endif
  26. #define ctx_m2m_get_frame(frame, ctx, type) do { \
  27. if (V4L2_BUF_TYPE_VIDEO_OUTPUT == (type)) { \
  28. frame = &(ctx)->s_frame; \
  29. } else if (V4L2_BUF_TYPE_VIDEO_CAPTURE == (type)) { \
  30. frame = &(ctx)->d_frame; \
  31. } else { \
  32. v4l2_err(&(ctx)->fimc_dev->m2m.v4l2_dev,\
  33. "Wrong buffer/video queue type (%d)\n", type); \
  34. return -EINVAL; \
  35. } \
  36. } while (0)
  37. #define NUM_FIMC_CLOCKS 2
  38. #define MODULE_NAME "s5p-fimc"
  39. #define FIMC_MAX_DEVS 3
  40. #define FIMC_MAX_OUT_BUFS 4
  41. #define SCALER_MAX_HRATIO 64
  42. #define SCALER_MAX_VRATIO 64
  43. enum {
  44. ST_IDLE,
  45. ST_OUTDMA_RUN,
  46. ST_M2M_PEND,
  47. };
  48. #define fimc_m2m_active(dev) test_bit(ST_OUTDMA_RUN, &(dev)->state)
  49. #define fimc_m2m_pending(dev) test_bit(ST_M2M_PEND, &(dev)->state)
  50. enum fimc_datapath {
  51. FIMC_ITU_CAM_A,
  52. FIMC_ITU_CAM_B,
  53. FIMC_MIPI_CAM,
  54. FIMC_DMA,
  55. FIMC_LCDFIFO,
  56. FIMC_WRITEBACK
  57. };
  58. enum fimc_color_fmt {
  59. S5P_FIMC_RGB565,
  60. S5P_FIMC_RGB666,
  61. S5P_FIMC_RGB888,
  62. S5P_FIMC_YCBCR420,
  63. S5P_FIMC_YCBCR422,
  64. S5P_FIMC_YCBYCR422,
  65. S5P_FIMC_YCRYCB422,
  66. S5P_FIMC_CBYCRY422,
  67. S5P_FIMC_CRYCBY422,
  68. S5P_FIMC_RGB30_LOCAL,
  69. S5P_FIMC_YCBCR444_LOCAL,
  70. S5P_FIMC_MAX_COLOR = S5P_FIMC_YCBCR444_LOCAL,
  71. S5P_FIMC_COLOR_MASK = 0x0F,
  72. };
  73. /* Y/Cb/Cr components order at DMA output for 1 plane YCbCr 4:2:2 formats. */
  74. #define S5P_FIMC_OUT_CRYCBY S5P_CIOCTRL_ORDER422_CRYCBY
  75. #define S5P_FIMC_OUT_CBYCRY S5P_CIOCTRL_ORDER422_YCRYCB
  76. #define S5P_FIMC_OUT_YCRYCB S5P_CIOCTRL_ORDER422_CBYCRY
  77. #define S5P_FIMC_OUT_YCBYCR S5P_CIOCTRL_ORDER422_YCBYCR
  78. /* Input Y/Cb/Cr components order for 1 plane YCbCr 4:2:2 color formats. */
  79. #define S5P_FIMC_IN_CRYCBY S5P_MSCTRL_ORDER422_CRYCBY
  80. #define S5P_FIMC_IN_CBYCRY S5P_MSCTRL_ORDER422_YCRYCB
  81. #define S5P_FIMC_IN_YCRYCB S5P_MSCTRL_ORDER422_CBYCRY
  82. #define S5P_FIMC_IN_YCBYCR S5P_MSCTRL_ORDER422_YCBYCR
  83. /* Cb/Cr chrominance components order for 2 plane Y/CbCr 4:2:2 formats. */
  84. #define S5P_FIMC_LSB_CRCB S5P_CIOCTRL_ORDER422_2P_LSB_CRCB
  85. /* The embedded image effect selection */
  86. #define S5P_FIMC_EFFECT_ORIGINAL S5P_CIIMGEFF_FIN_BYPASS
  87. #define S5P_FIMC_EFFECT_ARBITRARY S5P_CIIMGEFF_FIN_ARBITRARY
  88. #define S5P_FIMC_EFFECT_NEGATIVE S5P_CIIMGEFF_FIN_NEGATIVE
  89. #define S5P_FIMC_EFFECT_ARTFREEZE S5P_CIIMGEFF_FIN_ARTFREEZE
  90. #define S5P_FIMC_EFFECT_EMBOSSING S5P_CIIMGEFF_FIN_EMBOSSING
  91. #define S5P_FIMC_EFFECT_SIKHOUETTE S5P_CIIMGEFF_FIN_SILHOUETTE
  92. /* The hardware context state. */
  93. #define FIMC_PARAMS (1 << 0)
  94. #define FIMC_SRC_ADDR (1 << 1)
  95. #define FIMC_DST_ADDR (1 << 2)
  96. #define FIMC_SRC_FMT (1 << 3)
  97. #define FIMC_DST_FMT (1 << 4)
  98. /* Image conversion flags */
  99. #define FIMC_IN_DMA_ACCESS_TILED (1 << 0)
  100. #define FIMC_IN_DMA_ACCESS_LINEAR (0 << 0)
  101. #define FIMC_OUT_DMA_ACCESS_TILED (1 << 1)
  102. #define FIMC_OUT_DMA_ACCESS_LINEAR (0 << 1)
  103. #define FIMC_SCAN_MODE_PROGRESSIVE (0 << 2)
  104. #define FIMC_SCAN_MODE_INTERLACED (1 << 2)
  105. /* YCbCr data dynamic range for RGB-YUV color conversion. Y/Cb/Cr: (0 ~ 255) */
  106. #define FIMC_COLOR_RANGE_WIDE (0 << 3)
  107. /* Y (16 ~ 235), Cb/Cr (16 ~ 240) */
  108. #define FIMC_COLOR_RANGE_NARROW (1 << 3)
  109. #define FLIP_NONE 0
  110. #define FLIP_X_AXIS 1
  111. #define FLIP_Y_AXIS 2
  112. #define FLIP_XY_AXIS (FLIP_X_AXIS | FLIP_Y_AXIS)
  113. /**
  114. * struct fimc_fmt - the driver's internal color format data
  115. * @name: format description
  116. * @fourcc: the fourcc code for this format
  117. * @color: the corresponding fimc_color_fmt
  118. * @depth: number of bits per pixel
  119. * @buff_cnt: number of physically non-contiguous data planes
  120. * @planes_cnt: number of physically contiguous data planes
  121. */
  122. struct fimc_fmt {
  123. char *name;
  124. u32 fourcc;
  125. u32 color;
  126. u32 depth;
  127. u16 buff_cnt;
  128. u16 planes_cnt;
  129. };
  130. /**
  131. * struct fimc_dma_offset - pixel offset information for DMA
  132. * @y_h: y value horizontal offset
  133. * @y_v: y value vertical offset
  134. * @cb_h: cb value horizontal offset
  135. * @cb_v: cb value vertical offset
  136. * @cr_h: cr value horizontal offset
  137. * @cr_v: cr value vertical offset
  138. */
  139. struct fimc_dma_offset {
  140. int y_h;
  141. int y_v;
  142. int cb_h;
  143. int cb_v;
  144. int cr_h;
  145. int cr_v;
  146. };
  147. /**
  148. * struct fimc_effect - the configuration data for the "Arbitrary" image effect
  149. * @type: effect type
  150. * @pat_cb: cr value when type is "arbitrary"
  151. * @pat_cr: cr value when type is "arbitrary"
  152. */
  153. struct fimc_effect {
  154. u32 type;
  155. u8 pat_cb;
  156. u8 pat_cr;
  157. };
  158. /**
  159. * struct fimc_scaler - the configuration data for FIMC inetrnal scaler
  160. *
  161. * @enabled: the flag set when the scaler is used
  162. * @hfactor: horizontal shift factor
  163. * @vfactor: vertical shift factor
  164. * @pre_hratio: horizontal ratio of the prescaler
  165. * @pre_vratio: vertical ratio of the prescaler
  166. * @pre_dst_width: the prescaler's destination width
  167. * @pre_dst_height: the prescaler's destination height
  168. * @scaleup_h: flag indicating scaling up horizontally
  169. * @scaleup_v: flag indicating scaling up vertically
  170. * @main_hratio: the main scaler's horizontal ratio
  171. * @main_vratio: the main scaler's vertical ratio
  172. * @real_width: source width - offset
  173. * @real_height: source height - offset
  174. * @copy_mode: flag set if one-to-one mode is used, i.e. no scaling
  175. * and color format conversion
  176. */
  177. struct fimc_scaler {
  178. u32 enabled;
  179. u32 hfactor;
  180. u32 vfactor;
  181. u32 pre_hratio;
  182. u32 pre_vratio;
  183. u32 pre_dst_width;
  184. u32 pre_dst_height;
  185. u32 scaleup_h;
  186. u32 scaleup_v;
  187. u32 main_hratio;
  188. u32 main_vratio;
  189. u32 real_width;
  190. u32 real_height;
  191. u32 copy_mode;
  192. };
  193. /**
  194. * struct fimc_addr - the FIMC physical address set for DMA
  195. *
  196. * @y: luminance plane physical address
  197. * @cb: Cb plane physical address
  198. * @cr: Cr plane physical address
  199. */
  200. struct fimc_addr {
  201. u32 y;
  202. u32 cb;
  203. u32 cr;
  204. };
  205. /**
  206. * struct fimc_vid_buffer - the driver's video buffer
  207. * @vb: v4l videobuf buffer
  208. */
  209. struct fimc_vid_buffer {
  210. struct videobuf_buffer vb;
  211. };
  212. /**
  213. * struct fimc_frame - input/output frame format properties
  214. *
  215. * @f_width: image full width (virtual screen size)
  216. * @f_height: image full height (virtual screen size)
  217. * @o_width: original image width as set by S_FMT
  218. * @o_height: original image height as set by S_FMT
  219. * @offs_h: image horizontal pixel offset
  220. * @offs_v: image vertical pixel offset
  221. * @width: image pixel width
  222. * @height: image pixel weight
  223. * @paddr: image frame buffer physical addresses
  224. * @buf_cnt: number of buffers depending on a color format
  225. * @size: image size in bytes
  226. * @color: color format
  227. * @dma_offset: DMA offset in bytes
  228. */
  229. struct fimc_frame {
  230. u32 f_width;
  231. u32 f_height;
  232. u32 o_width;
  233. u32 o_height;
  234. u32 offs_h;
  235. u32 offs_v;
  236. u32 width;
  237. u32 height;
  238. u32 size;
  239. struct fimc_addr paddr;
  240. struct fimc_dma_offset dma_offset;
  241. struct fimc_fmt *fmt;
  242. };
  243. /**
  244. * struct fimc_m2m_device - v4l2 memory-to-memory device data
  245. * @vfd: the video device node for v4l2 m2m mode
  246. * @v4l2_dev: v4l2 device for m2m mode
  247. * @m2m_dev: v4l2 memory-to-memory device data
  248. * @ctx: hardware context data
  249. * @refcnt: the reference counter
  250. */
  251. struct fimc_m2m_device {
  252. struct video_device *vfd;
  253. struct v4l2_device v4l2_dev;
  254. struct v4l2_m2m_dev *m2m_dev;
  255. struct fimc_ctx *ctx;
  256. int refcnt;
  257. };
  258. /**
  259. * struct samsung_fimc_variant - camera interface variant information
  260. *
  261. * @pix_hoff: indicate whether horizontal offset is in pixels or in bytes
  262. * @has_inp_rot: set if has input rotator
  263. * @has_out_rot: set if has output rotator
  264. * @min_inp_pixsize: minimum input pixel size
  265. * @min_out_pixsize: minimum output pixel size
  266. * @scaler_en_w: maximum input pixel width when the scaler is enabled
  267. * @scaler_dis_w: maximum input pixel width when the scaler is disabled
  268. * @in_rot_en_h: maximum input width when the input rotator is used
  269. * @in_rot_dis_w: maximum input width when the input rotator is used
  270. * @out_rot_en_w: maximum output width for the output rotator enabled
  271. * @out_rot_dis_w: maximum output width for the output rotator enabled
  272. */
  273. struct samsung_fimc_variant {
  274. unsigned int pix_hoff:1;
  275. unsigned int has_inp_rot:1;
  276. unsigned int has_out_rot:1;
  277. u16 min_inp_pixsize;
  278. u16 min_out_pixsize;
  279. u16 scaler_en_w;
  280. u16 scaler_dis_w;
  281. u16 in_rot_en_h;
  282. u16 in_rot_dis_w;
  283. u16 out_rot_en_w;
  284. u16 out_rot_dis_w;
  285. };
  286. /**
  287. * struct samsung_fimc_driverdata - per-device type driver data for init time.
  288. *
  289. * @variant: the variant information for this driver.
  290. * @dev_cnt: number of fimc sub-devices available in SoC
  291. */
  292. struct samsung_fimc_driverdata {
  293. struct samsung_fimc_variant *variant[FIMC_MAX_DEVS];
  294. int devs_cnt;
  295. };
  296. struct fimc_ctx;
  297. /**
  298. * struct fimc_subdev - abstraction for a FIMC entity
  299. *
  300. * @slock: the spinlock protecting this data structure
  301. * @lock: the mutex protecting this data structure
  302. * @pdev: pointer to the FIMC platform device
  303. * @id: FIMC device index (0..2)
  304. * @clock[]: the clocks required for FIMC operation
  305. * @regs: the mapped hardware registers
  306. * @regs_res: the resource claimed for IO registers
  307. * @irq: interrupt number of the FIMC subdevice
  308. * @irqlock: spinlock protecting videbuffer queue
  309. * @m2m: memory-to-memory V4L2 device information
  310. * @state: the FIMC device state flags
  311. */
  312. struct fimc_dev {
  313. spinlock_t slock;
  314. struct mutex lock;
  315. struct platform_device *pdev;
  316. struct samsung_fimc_variant *variant;
  317. int id;
  318. struct clk *clock[NUM_FIMC_CLOCKS];
  319. void __iomem *regs;
  320. struct resource *regs_res;
  321. int irq;
  322. spinlock_t irqlock;
  323. struct workqueue_struct *work_queue;
  324. struct fimc_m2m_device m2m;
  325. unsigned long state;
  326. };
  327. /**
  328. * fimc_ctx - the device context data
  329. *
  330. * @lock: mutex protecting this data structure
  331. * @s_frame: source frame properties
  332. * @d_frame: destination frame properties
  333. * @out_order_1p: output 1-plane YCBCR order
  334. * @out_order_2p: output 2-plane YCBCR order
  335. * @in_order_1p input 1-plane YCBCR order
  336. * @in_order_2p: input 2-plane YCBCR order
  337. * @in_path: input mode (DMA or camera)
  338. * @out_path: output mode (DMA or FIFO)
  339. * @scaler: image scaler properties
  340. * @effect: image effect
  341. * @rotation: image clockwise rotation in degrees
  342. * @flip: image flip mode
  343. * @flags: an additional flags for image conversion
  344. * @state: flags to keep track of user configuration
  345. * @fimc_dev: the FIMC device this context applies to
  346. * @m2m_ctx: memory-to-memory device context
  347. */
  348. struct fimc_ctx {
  349. spinlock_t slock;
  350. struct fimc_frame s_frame;
  351. struct fimc_frame d_frame;
  352. u32 out_order_1p;
  353. u32 out_order_2p;
  354. u32 in_order_1p;
  355. u32 in_order_2p;
  356. enum fimc_datapath in_path;
  357. enum fimc_datapath out_path;
  358. struct fimc_scaler scaler;
  359. struct fimc_effect effect;
  360. int rotation;
  361. u32 flip;
  362. u32 flags;
  363. u32 state;
  364. struct fimc_dev *fimc_dev;
  365. struct v4l2_m2m_ctx *m2m_ctx;
  366. };
  367. static inline int tiled_fmt(struct fimc_fmt *fmt)
  368. {
  369. return 0;
  370. }
  371. static inline void fimc_hw_clear_irq(struct fimc_dev *dev)
  372. {
  373. u32 cfg = readl(dev->regs + S5P_CIGCTRL);
  374. cfg |= S5P_CIGCTRL_IRQ_CLR;
  375. writel(cfg, dev->regs + S5P_CIGCTRL);
  376. }
  377. static inline void fimc_hw_start_scaler(struct fimc_dev *dev)
  378. {
  379. u32 cfg = readl(dev->regs + S5P_CISCCTRL);
  380. cfg |= S5P_CISCCTRL_SCALERSTART;
  381. writel(cfg, dev->regs + S5P_CISCCTRL);
  382. }
  383. static inline void fimc_hw_stop_scaler(struct fimc_dev *dev)
  384. {
  385. u32 cfg = readl(dev->regs + S5P_CISCCTRL);
  386. cfg &= ~S5P_CISCCTRL_SCALERSTART;
  387. writel(cfg, dev->regs + S5P_CISCCTRL);
  388. }
  389. static inline void fimc_hw_dis_capture(struct fimc_dev *dev)
  390. {
  391. u32 cfg = readl(dev->regs + S5P_CIIMGCPT);
  392. cfg &= ~(S5P_CIIMGCPT_IMGCPTEN | S5P_CIIMGCPT_IMGCPTEN_SC);
  393. writel(cfg, dev->regs + S5P_CIIMGCPT);
  394. }
  395. static inline void fimc_hw_start_in_dma(struct fimc_dev *dev)
  396. {
  397. u32 cfg = readl(dev->regs + S5P_MSCTRL);
  398. cfg |= S5P_MSCTRL_ENVID;
  399. writel(cfg, dev->regs + S5P_MSCTRL);
  400. }
  401. static inline void fimc_hw_stop_in_dma(struct fimc_dev *dev)
  402. {
  403. u32 cfg = readl(dev->regs + S5P_MSCTRL);
  404. cfg &= ~S5P_MSCTRL_ENVID;
  405. writel(cfg, dev->regs + S5P_MSCTRL);
  406. }
  407. /* -----------------------------------------------------*/
  408. /* fimc-reg.c */
  409. void fimc_hw_reset(struct fimc_dev *dev);
  410. void fimc_hw_set_rotation(struct fimc_ctx *ctx);
  411. void fimc_hw_set_target_format(struct fimc_ctx *ctx);
  412. void fimc_hw_set_out_dma(struct fimc_ctx *ctx);
  413. void fimc_hw_en_lastirq(struct fimc_dev *dev, int enable);
  414. void fimc_hw_en_irq(struct fimc_dev *dev, int enable);
  415. void fimc_hw_set_prescaler(struct fimc_ctx *ctx);
  416. void fimc_hw_set_scaler(struct fimc_ctx *ctx);
  417. void fimc_hw_en_capture(struct fimc_ctx *ctx);
  418. void fimc_hw_set_effect(struct fimc_ctx *ctx);
  419. void fimc_hw_set_in_dma(struct fimc_ctx *ctx);
  420. void fimc_hw_set_input_path(struct fimc_ctx *ctx);
  421. void fimc_hw_set_output_path(struct fimc_ctx *ctx);
  422. void fimc_hw_set_input_addr(struct fimc_dev *dev, struct fimc_addr *paddr);
  423. void fimc_hw_set_output_addr(struct fimc_dev *dev, struct fimc_addr *paddr);
  424. #endif /* FIMC_CORE_H_ */