gsc-core.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  1. /*
  2. * Copyright (c) 2011 - 2012 Samsung Electronics Co., Ltd.
  3. * http://www.samsung.com
  4. *
  5. * header file for Samsung EXYNOS5 SoC series G-Scaler driver
  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 GSC_CORE_H_
  11. #define GSC_CORE_H_
  12. #include <linux/delay.h>
  13. #include <linux/sched.h>
  14. #include <linux/spinlock.h>
  15. #include <linux/types.h>
  16. #include <linux/videodev2.h>
  17. #include <linux/io.h>
  18. #include <linux/pm_runtime.h>
  19. #include <media/videobuf2-v4l2.h>
  20. #include <media/v4l2-ctrls.h>
  21. #include <media/v4l2-device.h>
  22. #include <media/v4l2-mem2mem.h>
  23. #include <media/v4l2-mediabus.h>
  24. #include <media/videobuf2-dma-contig.h>
  25. #include "gsc-regs.h"
  26. #define CONFIG_VB2_GSC_DMA_CONTIG 1
  27. #define GSC_MODULE_NAME "exynos-gsc"
  28. #define GSC_SHUTDOWN_TIMEOUT ((100*HZ)/1000)
  29. #define GSC_MAX_DEVS 4
  30. #define GSC_M2M_BUF_NUM 0
  31. #define GSC_MAX_CTRL_NUM 10
  32. #define GSC_SC_ALIGN_4 4
  33. #define GSC_SC_ALIGN_2 2
  34. #define DEFAULT_CSC_EQ 1
  35. #define DEFAULT_CSC_RANGE 1
  36. #define GSC_PARAMS (1 << 0)
  37. #define GSC_SRC_FMT (1 << 1)
  38. #define GSC_DST_FMT (1 << 2)
  39. #define GSC_CTX_M2M (1 << 3)
  40. #define GSC_CTX_STOP_REQ (1 << 6)
  41. #define GSC_CTX_ABORT (1 << 7)
  42. enum gsc_dev_flags {
  43. /* for global */
  44. ST_SUSPEND,
  45. /* for m2m node */
  46. ST_M2M_OPEN,
  47. ST_M2M_RUN,
  48. ST_M2M_PEND,
  49. ST_M2M_SUSPENDED,
  50. ST_M2M_SUSPENDING,
  51. };
  52. enum gsc_irq {
  53. GSC_IRQ_DONE,
  54. GSC_IRQ_OVERRUN
  55. };
  56. /**
  57. * enum gsc_datapath - the path of data used for G-Scaler
  58. * @GSC_CAMERA: from camera
  59. * @GSC_DMA: from/to DMA
  60. * @GSC_LOCAL: to local path
  61. * @GSC_WRITEBACK: from FIMD
  62. */
  63. enum gsc_datapath {
  64. GSC_CAMERA = 0x1,
  65. GSC_DMA,
  66. GSC_MIXER,
  67. GSC_FIMD,
  68. GSC_WRITEBACK,
  69. };
  70. enum gsc_color_fmt {
  71. GSC_RGB = 0x1,
  72. GSC_YUV420 = 0x2,
  73. GSC_YUV422 = 0x4,
  74. GSC_YUV444 = 0x8,
  75. };
  76. enum gsc_yuv_fmt {
  77. GSC_LSB_Y = 0x10,
  78. GSC_LSB_C,
  79. GSC_CBCR = 0x20,
  80. GSC_CRCB,
  81. };
  82. #define fh_to_ctx(__fh) container_of(__fh, struct gsc_ctx, fh)
  83. #define is_rgb(x) (!!((x) & 0x1))
  84. #define is_yuv420(x) (!!((x) & 0x2))
  85. #define is_yuv422(x) (!!((x) & 0x4))
  86. #define gsc_m2m_active(dev) test_bit(ST_M2M_RUN, &(dev)->state)
  87. #define gsc_m2m_pending(dev) test_bit(ST_M2M_PEND, &(dev)->state)
  88. #define gsc_m2m_opened(dev) test_bit(ST_M2M_OPEN, &(dev)->state)
  89. #define ctrl_to_ctx(__ctrl) \
  90. container_of((__ctrl)->handler, struct gsc_ctx, ctrl_handler)
  91. /**
  92. * struct gsc_fmt - the driver's internal color format data
  93. * @mbus_code: Media Bus pixel code, -1 if not applicable
  94. * @name: format description
  95. * @pixelformat: the fourcc code for this format, 0 if not applicable
  96. * @yorder: Y/C order
  97. * @corder: Chrominance order control
  98. * @num_planes: number of physically non-contiguous data planes
  99. * @nr_comp: number of physically contiguous data planes
  100. * @depth: per plane driver's private 'number of bits per pixel'
  101. * @flags: flags indicating which operation mode format applies to
  102. */
  103. struct gsc_fmt {
  104. u32 mbus_code;
  105. char *name;
  106. u32 pixelformat;
  107. u32 color;
  108. u32 yorder;
  109. u32 corder;
  110. u16 num_planes;
  111. u16 num_comp;
  112. u8 depth[VIDEO_MAX_PLANES];
  113. u32 flags;
  114. };
  115. /**
  116. * struct gsc_input_buf - the driver's video buffer
  117. * @vb: videobuf2 buffer
  118. * @list : linked list structure for buffer queue
  119. * @idx : index of G-Scaler input buffer
  120. */
  121. struct gsc_input_buf {
  122. struct vb2_v4l2_buffer vb;
  123. struct list_head list;
  124. int idx;
  125. };
  126. /**
  127. * struct gsc_addr - the G-Scaler physical address set
  128. * @y: luminance plane address
  129. * @cb: Cb plane address
  130. * @cr: Cr plane address
  131. */
  132. struct gsc_addr {
  133. dma_addr_t y;
  134. dma_addr_t cb;
  135. dma_addr_t cr;
  136. };
  137. /* struct gsc_ctrls - the G-Scaler control set
  138. * @rotate: rotation degree
  139. * @hflip: horizontal flip
  140. * @vflip: vertical flip
  141. * @global_alpha: the alpha value of current frame
  142. */
  143. struct gsc_ctrls {
  144. struct v4l2_ctrl *rotate;
  145. struct v4l2_ctrl *hflip;
  146. struct v4l2_ctrl *vflip;
  147. struct v4l2_ctrl *global_alpha;
  148. };
  149. /**
  150. * struct gsc_scaler - the configuration data for G-Scaler inetrnal scaler
  151. * @pre_shfactor: pre sclaer shift factor
  152. * @pre_hratio: horizontal ratio of the prescaler
  153. * @pre_vratio: vertical ratio of the prescaler
  154. * @main_hratio: the main scaler's horizontal ratio
  155. * @main_vratio: the main scaler's vertical ratio
  156. */
  157. struct gsc_scaler {
  158. u32 pre_shfactor;
  159. u32 pre_hratio;
  160. u32 pre_vratio;
  161. u32 main_hratio;
  162. u32 main_vratio;
  163. };
  164. struct gsc_dev;
  165. struct gsc_ctx;
  166. /**
  167. * struct gsc_frame - source/target frame properties
  168. * @f_width: SRC : SRCIMG_WIDTH, DST : OUTPUTDMA_WHOLE_IMG_WIDTH
  169. * @f_height: SRC : SRCIMG_HEIGHT, DST : OUTPUTDMA_WHOLE_IMG_HEIGHT
  170. * @crop: cropped(source)/scaled(destination) size
  171. * @payload: image size in bytes (w x h x bpp)
  172. * @addr: image frame buffer physical addresses
  173. * @fmt: G-Scaler color format pointer
  174. * @colorspace: value indicating v4l2_colorspace
  175. * @alpha: frame's alpha value
  176. */
  177. struct gsc_frame {
  178. u32 f_width;
  179. u32 f_height;
  180. struct v4l2_rect crop;
  181. unsigned long payload[VIDEO_MAX_PLANES];
  182. struct gsc_addr addr;
  183. const struct gsc_fmt *fmt;
  184. u32 colorspace;
  185. u8 alpha;
  186. };
  187. /**
  188. * struct gsc_m2m_device - v4l2 memory-to-memory device data
  189. * @vfd: the video device node for v4l2 m2m mode
  190. * @m2m_dev: v4l2 memory-to-memory device data
  191. * @ctx: hardware context data
  192. * @refcnt: the reference counter
  193. */
  194. struct gsc_m2m_device {
  195. struct video_device *vfd;
  196. struct v4l2_m2m_dev *m2m_dev;
  197. struct gsc_ctx *ctx;
  198. int refcnt;
  199. };
  200. /**
  201. * struct gsc_pix_max - image pixel size limits in various IP configurations
  202. *
  203. * @org_scaler_bypass_w: max pixel width when the scaler is disabled
  204. * @org_scaler_bypass_h: max pixel height when the scaler is disabled
  205. * @org_scaler_input_w: max pixel width when the scaler is enabled
  206. * @org_scaler_input_h: max pixel height when the scaler is enabled
  207. * @real_rot_dis_w: max pixel src cropped height with the rotator is off
  208. * @real_rot_dis_h: max pixel src croppped width with the rotator is off
  209. * @real_rot_en_w: max pixel src cropped width with the rotator is on
  210. * @real_rot_en_h: max pixel src cropped height with the rotator is on
  211. * @target_rot_dis_w: max pixel dst scaled width with the rotator is off
  212. * @target_rot_dis_h: max pixel dst scaled height with the rotator is off
  213. * @target_rot_en_w: max pixel dst scaled width with the rotator is on
  214. * @target_rot_en_h: max pixel dst scaled height with the rotator is on
  215. */
  216. struct gsc_pix_max {
  217. u16 org_scaler_bypass_w;
  218. u16 org_scaler_bypass_h;
  219. u16 org_scaler_input_w;
  220. u16 org_scaler_input_h;
  221. u16 real_rot_dis_w;
  222. u16 real_rot_dis_h;
  223. u16 real_rot_en_w;
  224. u16 real_rot_en_h;
  225. u16 target_rot_dis_w;
  226. u16 target_rot_dis_h;
  227. u16 target_rot_en_w;
  228. u16 target_rot_en_h;
  229. };
  230. /**
  231. * struct gsc_pix_min - image pixel size limits in various IP configurations
  232. *
  233. * @org_w: minimum source pixel width
  234. * @org_h: minimum source pixel height
  235. * @real_w: minimum input crop pixel width
  236. * @real_h: minimum input crop pixel height
  237. * @target_rot_dis_w: minimum output scaled pixel height when rotator is off
  238. * @target_rot_dis_h: minimum output scaled pixel height when rotator is off
  239. * @target_rot_en_w: minimum output scaled pixel height when rotator is on
  240. * @target_rot_en_h: minimum output scaled pixel height when rotator is on
  241. */
  242. struct gsc_pix_min {
  243. u16 org_w;
  244. u16 org_h;
  245. u16 real_w;
  246. u16 real_h;
  247. u16 target_rot_dis_w;
  248. u16 target_rot_dis_h;
  249. u16 target_rot_en_w;
  250. u16 target_rot_en_h;
  251. };
  252. struct gsc_pix_align {
  253. u16 org_h;
  254. u16 org_w;
  255. u16 offset_h;
  256. u16 real_w;
  257. u16 real_h;
  258. u16 target_w;
  259. u16 target_h;
  260. };
  261. /**
  262. * struct gsc_variant - G-Scaler variant information
  263. */
  264. struct gsc_variant {
  265. struct gsc_pix_max *pix_max;
  266. struct gsc_pix_min *pix_min;
  267. struct gsc_pix_align *pix_align;
  268. u16 in_buf_cnt;
  269. u16 out_buf_cnt;
  270. u16 sc_up_max;
  271. u16 sc_down_max;
  272. u16 poly_sc_down_max;
  273. u16 pre_sc_down_max;
  274. u16 local_sc_down;
  275. };
  276. /**
  277. * struct gsc_driverdata - per device type driver data for init time.
  278. *
  279. * @variant: the variant information for this driver.
  280. * @lclk_frequency: G-Scaler clock frequency
  281. * @num_entities: the number of g-scalers
  282. */
  283. struct gsc_driverdata {
  284. struct gsc_variant *variant[GSC_MAX_DEVS];
  285. unsigned long lclk_frequency;
  286. int num_entities;
  287. };
  288. /**
  289. * struct gsc_dev - abstraction for G-Scaler entity
  290. * @slock: the spinlock protecting this data structure
  291. * @lock: the mutex protecting this data structure
  292. * @pdev: pointer to the G-Scaler platform device
  293. * @variant: the IP variant information
  294. * @id: G-Scaler device index (0..GSC_MAX_DEVS)
  295. * @clock: clocks required for G-Scaler operation
  296. * @regs: the mapped hardware registers
  297. * @irq_queue: interrupt handler waitqueue
  298. * @m2m: memory-to-memory V4L2 device information
  299. * @state: flags used to synchronize m2m and capture mode operation
  300. * @alloc_ctx: videobuf2 memory allocator context
  301. * @vdev: video device for G-Scaler instance
  302. */
  303. struct gsc_dev {
  304. spinlock_t slock;
  305. struct mutex lock;
  306. struct platform_device *pdev;
  307. struct gsc_variant *variant;
  308. u16 id;
  309. struct clk *clock;
  310. void __iomem *regs;
  311. wait_queue_head_t irq_queue;
  312. struct gsc_m2m_device m2m;
  313. struct exynos_platform_gscaler *pdata;
  314. unsigned long state;
  315. struct vb2_alloc_ctx *alloc_ctx;
  316. struct video_device vdev;
  317. struct v4l2_device v4l2_dev;
  318. };
  319. /**
  320. * gsc_ctx - the device context data
  321. * @s_frame: source frame properties
  322. * @d_frame: destination frame properties
  323. * @in_path: input mode (DMA or camera)
  324. * @out_path: output mode (DMA or FIFO)
  325. * @scaler: image scaler properties
  326. * @flags: additional flags for image conversion
  327. * @state: flags to keep track of user configuration
  328. * @gsc_dev: the G-Scaler device this context applies to
  329. * @m2m_ctx: memory-to-memory device context
  330. * @fh: v4l2 file handle
  331. * @ctrl_handler: v4l2 controls handler
  332. * @gsc_ctrls G-Scaler control set
  333. * @ctrls_rdy: true if the control handler is initialized
  334. */
  335. struct gsc_ctx {
  336. struct gsc_frame s_frame;
  337. struct gsc_frame d_frame;
  338. enum gsc_datapath in_path;
  339. enum gsc_datapath out_path;
  340. struct gsc_scaler scaler;
  341. u32 flags;
  342. u32 state;
  343. int rotation;
  344. unsigned int hflip:1;
  345. unsigned int vflip:1;
  346. struct gsc_dev *gsc_dev;
  347. struct v4l2_m2m_ctx *m2m_ctx;
  348. struct v4l2_fh fh;
  349. struct v4l2_ctrl_handler ctrl_handler;
  350. struct gsc_ctrls gsc_ctrls;
  351. bool ctrls_rdy;
  352. };
  353. void gsc_set_prefbuf(struct gsc_dev *gsc, struct gsc_frame *frm);
  354. int gsc_register_m2m_device(struct gsc_dev *gsc);
  355. void gsc_unregister_m2m_device(struct gsc_dev *gsc);
  356. void gsc_m2m_job_finish(struct gsc_ctx *ctx, int vb_state);
  357. u32 get_plane_size(struct gsc_frame *fr, unsigned int plane);
  358. const struct gsc_fmt *get_format(int index);
  359. const struct gsc_fmt *find_fmt(u32 *pixelformat, u32 *mbus_code, u32 index);
  360. int gsc_enum_fmt_mplane(struct v4l2_fmtdesc *f);
  361. int gsc_try_fmt_mplane(struct gsc_ctx *ctx, struct v4l2_format *f);
  362. void gsc_set_frame_size(struct gsc_frame *frame, int width, int height);
  363. int gsc_g_fmt_mplane(struct gsc_ctx *ctx, struct v4l2_format *f);
  364. void gsc_check_crop_change(u32 tmp_w, u32 tmp_h, u32 *w, u32 *h);
  365. int gsc_g_crop(struct gsc_ctx *ctx, struct v4l2_crop *cr);
  366. int gsc_try_crop(struct gsc_ctx *ctx, struct v4l2_crop *cr);
  367. int gsc_cal_prescaler_ratio(struct gsc_variant *var, u32 src, u32 dst,
  368. u32 *ratio);
  369. void gsc_get_prescaler_shfactor(u32 hratio, u32 vratio, u32 *sh);
  370. void gsc_check_src_scale_info(struct gsc_variant *var,
  371. struct gsc_frame *s_frame,
  372. u32 *wratio, u32 tx, u32 ty, u32 *hratio);
  373. int gsc_check_scaler_ratio(struct gsc_variant *var, int sw, int sh, int dw,
  374. int dh, int rot, int out_path);
  375. int gsc_set_scaler_info(struct gsc_ctx *ctx);
  376. int gsc_ctrls_create(struct gsc_ctx *ctx);
  377. void gsc_ctrls_delete(struct gsc_ctx *ctx);
  378. int gsc_prepare_addr(struct gsc_ctx *ctx, struct vb2_buffer *vb,
  379. struct gsc_frame *frame, struct gsc_addr *addr);
  380. static inline void gsc_ctx_state_lock_set(u32 state, struct gsc_ctx *ctx)
  381. {
  382. unsigned long flags;
  383. spin_lock_irqsave(&ctx->gsc_dev->slock, flags);
  384. ctx->state |= state;
  385. spin_unlock_irqrestore(&ctx->gsc_dev->slock, flags);
  386. }
  387. static inline void gsc_ctx_state_lock_clear(u32 state, struct gsc_ctx *ctx)
  388. {
  389. unsigned long flags;
  390. spin_lock_irqsave(&ctx->gsc_dev->slock, flags);
  391. ctx->state &= ~state;
  392. spin_unlock_irqrestore(&ctx->gsc_dev->slock, flags);
  393. }
  394. static inline int is_tiled(const struct gsc_fmt *fmt)
  395. {
  396. return fmt->pixelformat == V4L2_PIX_FMT_NV12MT_16X16;
  397. }
  398. static inline void gsc_hw_enable_control(struct gsc_dev *dev, bool on)
  399. {
  400. u32 cfg = readl(dev->regs + GSC_ENABLE);
  401. if (on)
  402. cfg |= GSC_ENABLE_ON;
  403. else
  404. cfg &= ~GSC_ENABLE_ON;
  405. writel(cfg, dev->regs + GSC_ENABLE);
  406. }
  407. static inline int gsc_hw_get_irq_status(struct gsc_dev *dev)
  408. {
  409. u32 cfg = readl(dev->regs + GSC_IRQ);
  410. if (cfg & GSC_IRQ_STATUS_OR_IRQ)
  411. return GSC_IRQ_OVERRUN;
  412. else
  413. return GSC_IRQ_DONE;
  414. }
  415. static inline void gsc_hw_clear_irq(struct gsc_dev *dev, int irq)
  416. {
  417. u32 cfg = readl(dev->regs + GSC_IRQ);
  418. if (irq == GSC_IRQ_OVERRUN)
  419. cfg |= GSC_IRQ_STATUS_OR_IRQ;
  420. else if (irq == GSC_IRQ_DONE)
  421. cfg |= GSC_IRQ_STATUS_FRM_DONE_IRQ;
  422. writel(cfg, dev->regs + GSC_IRQ);
  423. }
  424. static inline bool gsc_ctx_state_is_set(u32 mask, struct gsc_ctx *ctx)
  425. {
  426. unsigned long flags;
  427. bool ret;
  428. spin_lock_irqsave(&ctx->gsc_dev->slock, flags);
  429. ret = (ctx->state & mask) == mask;
  430. spin_unlock_irqrestore(&ctx->gsc_dev->slock, flags);
  431. return ret;
  432. }
  433. static inline struct gsc_frame *ctx_get_frame(struct gsc_ctx *ctx,
  434. enum v4l2_buf_type type)
  435. {
  436. struct gsc_frame *frame;
  437. if (V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE == type) {
  438. frame = &ctx->s_frame;
  439. } else if (V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE == type) {
  440. frame = &ctx->d_frame;
  441. } else {
  442. pr_err("Wrong buffer/video queue type (%d)", type);
  443. return ERR_PTR(-EINVAL);
  444. }
  445. return frame;
  446. }
  447. void gsc_hw_set_sw_reset(struct gsc_dev *dev);
  448. int gsc_wait_reset(struct gsc_dev *dev);
  449. void gsc_hw_set_frm_done_irq_mask(struct gsc_dev *dev, bool mask);
  450. void gsc_hw_set_gsc_irq_enable(struct gsc_dev *dev, bool mask);
  451. void gsc_hw_set_input_buf_masking(struct gsc_dev *dev, u32 shift, bool enable);
  452. void gsc_hw_set_output_buf_masking(struct gsc_dev *dev, u32 shift, bool enable);
  453. void gsc_hw_set_input_addr(struct gsc_dev *dev, struct gsc_addr *addr,
  454. int index);
  455. void gsc_hw_set_output_addr(struct gsc_dev *dev, struct gsc_addr *addr,
  456. int index);
  457. void gsc_hw_set_input_path(struct gsc_ctx *ctx);
  458. void gsc_hw_set_in_size(struct gsc_ctx *ctx);
  459. void gsc_hw_set_in_image_rgb(struct gsc_ctx *ctx);
  460. void gsc_hw_set_in_image_format(struct gsc_ctx *ctx);
  461. void gsc_hw_set_output_path(struct gsc_ctx *ctx);
  462. void gsc_hw_set_out_size(struct gsc_ctx *ctx);
  463. void gsc_hw_set_out_image_rgb(struct gsc_ctx *ctx);
  464. void gsc_hw_set_out_image_format(struct gsc_ctx *ctx);
  465. void gsc_hw_set_prescaler(struct gsc_ctx *ctx);
  466. void gsc_hw_set_mainscaler(struct gsc_ctx *ctx);
  467. void gsc_hw_set_rotation(struct gsc_ctx *ctx);
  468. void gsc_hw_set_global_alpha(struct gsc_ctx *ctx);
  469. void gsc_hw_set_sfr_update(struct gsc_ctx *ctx);
  470. #endif /* GSC_CORE_H_ */