fimc-core.h 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827
  1. /*
  2. * Copyright (C) 2010 - 2011 Samsung Electronics Co., Ltd.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 as
  6. * published by the Free Software Foundation.
  7. */
  8. #ifndef FIMC_CORE_H_
  9. #define FIMC_CORE_H_
  10. /*#define DEBUG*/
  11. #include <linux/platform_device.h>
  12. #include <linux/sched.h>
  13. #include <linux/spinlock.h>
  14. #include <linux/types.h>
  15. #include <linux/videodev2.h>
  16. #include <linux/io.h>
  17. #include <media/media-entity.h>
  18. #include <media/videobuf2-core.h>
  19. #include <media/v4l2-ctrls.h>
  20. #include <media/v4l2-device.h>
  21. #include <media/v4l2-mem2mem.h>
  22. #include <media/v4l2-mediabus.h>
  23. #include <media/s5p_fimc.h>
  24. #include "regs-fimc.h"
  25. #define err(fmt, args...) \
  26. printk(KERN_ERR "%s:%d: " fmt "\n", __func__, __LINE__, ##args)
  27. #define dbg(fmt, args...) \
  28. pr_debug("%s:%d: " fmt "\n", __func__, __LINE__, ##args)
  29. /* Time to wait for next frame VSYNC interrupt while stopping operation. */
  30. #define FIMC_SHUTDOWN_TIMEOUT ((100*HZ)/1000)
  31. #define MAX_FIMC_CLOCKS 2
  32. #define FIMC_MODULE_NAME "s5p-fimc"
  33. #define FIMC_MAX_DEVS 4
  34. #define FIMC_MAX_OUT_BUFS 4
  35. #define SCALER_MAX_HRATIO 64
  36. #define SCALER_MAX_VRATIO 64
  37. #define DMA_MIN_SIZE 8
  38. #define FIMC_CAMIF_MAX_HEIGHT 0x2000
  39. /* indices to the clocks array */
  40. enum {
  41. CLK_BUS,
  42. CLK_GATE,
  43. };
  44. enum fimc_dev_flags {
  45. ST_LPM,
  46. /* m2m node */
  47. ST_M2M_RUN,
  48. ST_M2M_PEND,
  49. ST_M2M_SUSPENDING,
  50. ST_M2M_SUSPENDED,
  51. /* capture node */
  52. ST_CAPT_PEND,
  53. ST_CAPT_RUN,
  54. ST_CAPT_STREAM,
  55. ST_CAPT_ISP_STREAM,
  56. ST_CAPT_SUSPENDED,
  57. ST_CAPT_SHUT,
  58. ST_CAPT_BUSY,
  59. ST_CAPT_APPLY_CFG,
  60. ST_CAPT_JPEG,
  61. };
  62. #define fimc_m2m_active(dev) test_bit(ST_M2M_RUN, &(dev)->state)
  63. #define fimc_m2m_pending(dev) test_bit(ST_M2M_PEND, &(dev)->state)
  64. #define fimc_capture_running(dev) test_bit(ST_CAPT_RUN, &(dev)->state)
  65. #define fimc_capture_pending(dev) test_bit(ST_CAPT_PEND, &(dev)->state)
  66. #define fimc_capture_busy(dev) test_bit(ST_CAPT_BUSY, &(dev)->state)
  67. enum fimc_datapath {
  68. FIMC_CAMERA,
  69. FIMC_DMA,
  70. FIMC_LCDFIFO,
  71. FIMC_WRITEBACK
  72. };
  73. enum fimc_color_fmt {
  74. S5P_FIMC_RGB444 = 0x10,
  75. S5P_FIMC_RGB555,
  76. S5P_FIMC_RGB565,
  77. S5P_FIMC_RGB666,
  78. S5P_FIMC_RGB888,
  79. S5P_FIMC_RGB30_LOCAL,
  80. S5P_FIMC_YCBCR420 = 0x20,
  81. S5P_FIMC_YCBYCR422,
  82. S5P_FIMC_YCRYCB422,
  83. S5P_FIMC_CBYCRY422,
  84. S5P_FIMC_CRYCBY422,
  85. S5P_FIMC_YCBCR444_LOCAL,
  86. S5P_FIMC_JPEG = 0x40,
  87. };
  88. #define fimc_fmt_is_rgb(x) (!!((x) & 0x10))
  89. #define fimc_fmt_is_jpeg(x) (!!((x) & 0x40))
  90. #define IS_M2M(__strt) ((__strt) == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE || \
  91. __strt == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
  92. /* Cb/Cr chrominance components order for 2 plane Y/CbCr 4:2:2 formats. */
  93. #define S5P_FIMC_LSB_CRCB S5P_CIOCTRL_ORDER422_2P_LSB_CRCB
  94. /* The embedded image effect selection */
  95. #define S5P_FIMC_EFFECT_ORIGINAL S5P_CIIMGEFF_FIN_BYPASS
  96. #define S5P_FIMC_EFFECT_ARBITRARY S5P_CIIMGEFF_FIN_ARBITRARY
  97. #define S5P_FIMC_EFFECT_NEGATIVE S5P_CIIMGEFF_FIN_NEGATIVE
  98. #define S5P_FIMC_EFFECT_ARTFREEZE S5P_CIIMGEFF_FIN_ARTFREEZE
  99. #define S5P_FIMC_EFFECT_EMBOSSING S5P_CIIMGEFF_FIN_EMBOSSING
  100. #define S5P_FIMC_EFFECT_SIKHOUETTE S5P_CIIMGEFF_FIN_SILHOUETTE
  101. /* The hardware context state. */
  102. #define FIMC_PARAMS (1 << 0)
  103. #define FIMC_SRC_ADDR (1 << 1)
  104. #define FIMC_DST_ADDR (1 << 2)
  105. #define FIMC_SRC_FMT (1 << 3)
  106. #define FIMC_DST_FMT (1 << 4)
  107. #define FIMC_DST_CROP (1 << 5)
  108. #define FIMC_CTX_M2M (1 << 16)
  109. #define FIMC_CTX_CAP (1 << 17)
  110. #define FIMC_CTX_SHUT (1 << 18)
  111. /* Image conversion flags */
  112. #define FIMC_IN_DMA_ACCESS_TILED (1 << 0)
  113. #define FIMC_IN_DMA_ACCESS_LINEAR (0 << 0)
  114. #define FIMC_OUT_DMA_ACCESS_TILED (1 << 1)
  115. #define FIMC_OUT_DMA_ACCESS_LINEAR (0 << 1)
  116. #define FIMC_SCAN_MODE_PROGRESSIVE (0 << 2)
  117. #define FIMC_SCAN_MODE_INTERLACED (1 << 2)
  118. /*
  119. * YCbCr data dynamic range for RGB-YUV color conversion.
  120. * Y/Cb/Cr: (0 ~ 255) */
  121. #define FIMC_COLOR_RANGE_WIDE (0 << 3)
  122. /* Y (16 ~ 235), Cb/Cr (16 ~ 240) */
  123. #define FIMC_COLOR_RANGE_NARROW (1 << 3)
  124. /**
  125. * struct fimc_fmt - the driver's internal color format data
  126. * @mbus_code: Media Bus pixel code, -1 if not applicable
  127. * @name: format description
  128. * @fourcc: the fourcc code for this format, 0 if not applicable
  129. * @color: the corresponding fimc_color_fmt
  130. * @memplanes: number of physically non-contiguous data planes
  131. * @colplanes: number of physically contiguous data planes
  132. * @depth: per plane driver's private 'number of bits per pixel'
  133. * @flags: flags indicating which operation mode format applies to
  134. */
  135. struct fimc_fmt {
  136. enum v4l2_mbus_pixelcode mbus_code;
  137. char *name;
  138. u32 fourcc;
  139. u32 color;
  140. u16 memplanes;
  141. u16 colplanes;
  142. u8 depth[VIDEO_MAX_PLANES];
  143. u16 flags;
  144. #define FMT_FLAGS_CAM (1 << 0)
  145. #define FMT_FLAGS_M2M_IN (1 << 1)
  146. #define FMT_FLAGS_M2M_OUT (1 << 2)
  147. #define FMT_FLAGS_M2M (1 << 1 | 1 << 2)
  148. #define FMT_HAS_ALPHA (1 << 3)
  149. };
  150. /**
  151. * struct fimc_dma_offset - pixel offset information for DMA
  152. * @y_h: y value horizontal offset
  153. * @y_v: y value vertical offset
  154. * @cb_h: cb value horizontal offset
  155. * @cb_v: cb value vertical offset
  156. * @cr_h: cr value horizontal offset
  157. * @cr_v: cr value vertical offset
  158. */
  159. struct fimc_dma_offset {
  160. int y_h;
  161. int y_v;
  162. int cb_h;
  163. int cb_v;
  164. int cr_h;
  165. int cr_v;
  166. };
  167. /**
  168. * struct fimc_effect - color effect information
  169. * @type: effect type
  170. * @pat_cb: cr value when type is "arbitrary"
  171. * @pat_cr: cr value when type is "arbitrary"
  172. */
  173. struct fimc_effect {
  174. u32 type;
  175. u8 pat_cb;
  176. u8 pat_cr;
  177. };
  178. /**
  179. * struct fimc_scaler - the configuration data for FIMC inetrnal scaler
  180. * @scaleup_h: flag indicating scaling up horizontally
  181. * @scaleup_v: flag indicating scaling up vertically
  182. * @copy_mode: flag indicating transparent DMA transfer (no scaling
  183. * and color format conversion)
  184. * @enabled: flag indicating if the scaler is used
  185. * @hfactor: horizontal shift factor
  186. * @vfactor: vertical shift factor
  187. * @pre_hratio: horizontal ratio of the prescaler
  188. * @pre_vratio: vertical ratio of the prescaler
  189. * @pre_dst_width: the prescaler's destination width
  190. * @pre_dst_height: the prescaler's destination height
  191. * @main_hratio: the main scaler's horizontal ratio
  192. * @main_vratio: the main scaler's vertical ratio
  193. * @real_width: source pixel (width - offset)
  194. * @real_height: source pixel (height - offset)
  195. */
  196. struct fimc_scaler {
  197. unsigned int scaleup_h:1;
  198. unsigned int scaleup_v:1;
  199. unsigned int copy_mode:1;
  200. unsigned int enabled:1;
  201. u32 hfactor;
  202. u32 vfactor;
  203. u32 pre_hratio;
  204. u32 pre_vratio;
  205. u32 pre_dst_width;
  206. u32 pre_dst_height;
  207. u32 main_hratio;
  208. u32 main_vratio;
  209. u32 real_width;
  210. u32 real_height;
  211. };
  212. /**
  213. * struct fimc_addr - the FIMC physical address set for DMA
  214. * @y: luminance plane physical address
  215. * @cb: Cb plane physical address
  216. * @cr: Cr plane physical address
  217. */
  218. struct fimc_addr {
  219. u32 y;
  220. u32 cb;
  221. u32 cr;
  222. };
  223. /**
  224. * struct fimc_vid_buffer - the driver's video buffer
  225. * @vb: v4l videobuf buffer
  226. * @list: linked list structure for buffer queue
  227. * @paddr: precalculated physical address set
  228. * @index: buffer index for the output DMA engine
  229. */
  230. struct fimc_vid_buffer {
  231. struct vb2_buffer vb;
  232. struct list_head list;
  233. struct fimc_addr paddr;
  234. int index;
  235. };
  236. /**
  237. * struct fimc_frame - source/target frame properties
  238. * @f_width: image full width (virtual screen size)
  239. * @f_height: image full height (virtual screen size)
  240. * @o_width: original image width as set by S_FMT
  241. * @o_height: original image height as set by S_FMT
  242. * @offs_h: image horizontal pixel offset
  243. * @offs_v: image vertical pixel offset
  244. * @width: image pixel width
  245. * @height: image pixel weight
  246. * @payload: image size in bytes (w x h x bpp)
  247. * @paddr: image frame buffer physical addresses
  248. * @dma_offset: DMA offset in bytes
  249. * @fmt: fimc color format pointer
  250. */
  251. struct fimc_frame {
  252. u32 f_width;
  253. u32 f_height;
  254. u32 o_width;
  255. u32 o_height;
  256. u32 offs_h;
  257. u32 offs_v;
  258. u32 width;
  259. u32 height;
  260. unsigned long payload[VIDEO_MAX_PLANES];
  261. struct fimc_addr paddr;
  262. struct fimc_dma_offset dma_offset;
  263. struct fimc_fmt *fmt;
  264. u8 alpha;
  265. };
  266. /**
  267. * struct fimc_m2m_device - v4l2 memory-to-memory device data
  268. * @vfd: the video device node for v4l2 m2m mode
  269. * @m2m_dev: v4l2 memory-to-memory device data
  270. * @ctx: hardware context data
  271. * @refcnt: the reference counter
  272. */
  273. struct fimc_m2m_device {
  274. struct video_device *vfd;
  275. struct v4l2_m2m_dev *m2m_dev;
  276. struct fimc_ctx *ctx;
  277. int refcnt;
  278. };
  279. #define FIMC_SD_PAD_SINK 0
  280. #define FIMC_SD_PAD_SOURCE 1
  281. #define FIMC_SD_PADS_NUM 2
  282. /**
  283. * struct fimc_vid_cap - camera capture device information
  284. * @ctx: hardware context data
  285. * @vfd: video device node for camera capture mode
  286. * @subdev: subdev exposing the FIMC processing block
  287. * @vd_pad: fimc video capture node pad
  288. * @sd_pads: fimc video processing block pads
  289. * @mf: media bus format at the FIMC camera input (and the scaler output) pad
  290. * @pending_buf_q: the pending buffer queue head
  291. * @active_buf_q: the queue head of buffers scheduled in hardware
  292. * @vbq: the capture am video buffer queue
  293. * @active_buf_cnt: number of video buffers scheduled in hardware
  294. * @buf_index: index for managing the output DMA buffers
  295. * @frame_count: the frame counter for statistics
  296. * @reqbufs_count: the number of buffers requested in REQBUFS ioctl
  297. * @input_index: input (camera sensor) index
  298. * @refcnt: driver's private reference counter
  299. * @input: capture input type, grp_id of the attached subdev
  300. * @user_subdev_api: true if subdevs are not configured by the host driver
  301. */
  302. struct fimc_vid_cap {
  303. struct fimc_ctx *ctx;
  304. struct vb2_alloc_ctx *alloc_ctx;
  305. struct video_device *vfd;
  306. struct v4l2_subdev *subdev;
  307. struct media_pad vd_pad;
  308. struct v4l2_mbus_framefmt mf;
  309. struct media_pad sd_pads[FIMC_SD_PADS_NUM];
  310. struct list_head pending_buf_q;
  311. struct list_head active_buf_q;
  312. struct vb2_queue vbq;
  313. int active_buf_cnt;
  314. int buf_index;
  315. unsigned int frame_count;
  316. unsigned int reqbufs_count;
  317. int input_index;
  318. int refcnt;
  319. u32 input;
  320. bool user_subdev_api;
  321. };
  322. /**
  323. * struct fimc_pix_limit - image pixel size limits in various IP configurations
  324. *
  325. * @scaler_en_w: max input pixel width when the scaler is enabled
  326. * @scaler_dis_w: max input pixel width when the scaler is disabled
  327. * @in_rot_en_h: max input width with the input rotator is on
  328. * @in_rot_dis_w: max input width with the input rotator is off
  329. * @out_rot_en_w: max output width with the output rotator on
  330. * @out_rot_dis_w: max output width with the output rotator off
  331. */
  332. struct fimc_pix_limit {
  333. u16 scaler_en_w;
  334. u16 scaler_dis_w;
  335. u16 in_rot_en_h;
  336. u16 in_rot_dis_w;
  337. u16 out_rot_en_w;
  338. u16 out_rot_dis_w;
  339. };
  340. /**
  341. * struct samsung_fimc_variant - camera interface variant information
  342. *
  343. * @pix_hoff: indicate whether horizontal offset is in pixels or in bytes
  344. * @has_inp_rot: set if has input rotator
  345. * @has_out_rot: set if has output rotator
  346. * @has_cistatus2: 1 if CISTATUS2 register is present in this IP revision
  347. * @has_mainscaler_ext: 1 if extended mainscaler ratios in CIEXTEN register
  348. * are present in this IP revision
  349. * @has_cam_if: set if this instance has a camera input interface
  350. * @pix_limit: pixel size constraints for the scaler
  351. * @min_inp_pixsize: minimum input pixel size
  352. * @min_out_pixsize: minimum output pixel size
  353. * @hor_offs_align: horizontal pixel offset aligment
  354. * @min_vsize_align: minimum vertical pixel size alignment
  355. * @out_buf_count: the number of buffers in output DMA sequence
  356. */
  357. struct samsung_fimc_variant {
  358. unsigned int pix_hoff:1;
  359. unsigned int has_inp_rot:1;
  360. unsigned int has_out_rot:1;
  361. unsigned int has_cistatus2:1;
  362. unsigned int has_mainscaler_ext:1;
  363. unsigned int has_cam_if:1;
  364. unsigned int has_alpha:1;
  365. struct fimc_pix_limit *pix_limit;
  366. u16 min_inp_pixsize;
  367. u16 min_out_pixsize;
  368. u16 hor_offs_align;
  369. u16 min_vsize_align;
  370. u16 out_buf_count;
  371. };
  372. /**
  373. * struct samsung_fimc_driverdata - per device type driver data for init time.
  374. *
  375. * @variant: the variant information for this driver.
  376. * @dev_cnt: number of fimc sub-devices available in SoC
  377. * @lclk_frequency: fimc bus clock frequency
  378. */
  379. struct samsung_fimc_driverdata {
  380. struct samsung_fimc_variant *variant[FIMC_MAX_DEVS];
  381. unsigned long lclk_frequency;
  382. int num_entities;
  383. };
  384. struct fimc_pipeline {
  385. struct media_pipeline *pipe;
  386. struct v4l2_subdev *sensor;
  387. struct v4l2_subdev *csis;
  388. };
  389. struct fimc_ctx;
  390. /**
  391. * struct fimc_dev - abstraction for FIMC entity
  392. * @slock: the spinlock protecting this data structure
  393. * @lock: the mutex protecting this data structure
  394. * @pdev: pointer to the FIMC platform device
  395. * @pdata: pointer to the device platform data
  396. * @variant: the IP variant information
  397. * @id: FIMC device index (0..FIMC_MAX_DEVS)
  398. * @num_clocks: the number of clocks managed by this device instance
  399. * @clock: clocks required for FIMC operation
  400. * @regs: the mapped hardware registers
  401. * @irq: FIMC interrupt number
  402. * @irq_queue: interrupt handler waitqueue
  403. * @v4l2_dev: root v4l2_device
  404. * @m2m: memory-to-memory V4L2 device information
  405. * @vid_cap: camera capture device information
  406. * @state: flags used to synchronize m2m and capture mode operation
  407. * @alloc_ctx: videobuf2 memory allocator context
  408. * @pipeline: fimc video capture pipeline data structure
  409. */
  410. struct fimc_dev {
  411. spinlock_t slock;
  412. struct mutex lock;
  413. struct platform_device *pdev;
  414. struct s5p_platform_fimc *pdata;
  415. struct samsung_fimc_variant *variant;
  416. u16 id;
  417. u16 num_clocks;
  418. struct clk *clock[MAX_FIMC_CLOCKS];
  419. void __iomem *regs;
  420. int irq;
  421. wait_queue_head_t irq_queue;
  422. struct v4l2_device *v4l2_dev;
  423. struct fimc_m2m_device m2m;
  424. struct fimc_vid_cap vid_cap;
  425. unsigned long state;
  426. struct vb2_alloc_ctx *alloc_ctx;
  427. struct fimc_pipeline pipeline;
  428. };
  429. /**
  430. * fimc_ctx - the device context data
  431. * @slock: spinlock protecting this data structure
  432. * @s_frame: source frame properties
  433. * @d_frame: destination frame properties
  434. * @out_order_1p: output 1-plane YCBCR order
  435. * @out_order_2p: output 2-plane YCBCR order
  436. * @in_order_1p input 1-plane YCBCR order
  437. * @in_order_2p: input 2-plane YCBCR order
  438. * @in_path: input mode (DMA or camera)
  439. * @out_path: output mode (DMA or FIFO)
  440. * @scaler: image scaler properties
  441. * @effect: image effect
  442. * @rotation: image clockwise rotation in degrees
  443. * @hflip: indicates image horizontal flip if set
  444. * @vflip: indicates image vertical flip if set
  445. * @flags: additional flags for image conversion
  446. * @state: flags to keep track of user configuration
  447. * @fimc_dev: the FIMC device this context applies to
  448. * @m2m_ctx: memory-to-memory device context
  449. * @fh: v4l2 file handle
  450. * @ctrl_handler: v4l2 controls handler
  451. * @ctrl_rotate image rotation control
  452. * @ctrl_hflip horizontal flip control
  453. * @ctrl_vflip vertical flip control
  454. * @ctrl_alpha RGB alpha control
  455. * @ctrls_rdy: true if the control handler is initialized
  456. */
  457. struct fimc_ctx {
  458. spinlock_t slock;
  459. struct fimc_frame s_frame;
  460. struct fimc_frame d_frame;
  461. u32 out_order_1p;
  462. u32 out_order_2p;
  463. u32 in_order_1p;
  464. u32 in_order_2p;
  465. enum fimc_datapath in_path;
  466. enum fimc_datapath out_path;
  467. struct fimc_scaler scaler;
  468. struct fimc_effect effect;
  469. int rotation;
  470. unsigned int hflip:1;
  471. unsigned int vflip:1;
  472. u32 flags;
  473. u32 state;
  474. struct fimc_dev *fimc_dev;
  475. struct v4l2_m2m_ctx *m2m_ctx;
  476. struct v4l2_fh fh;
  477. struct v4l2_ctrl_handler ctrl_handler;
  478. struct v4l2_ctrl *ctrl_rotate;
  479. struct v4l2_ctrl *ctrl_hflip;
  480. struct v4l2_ctrl *ctrl_vflip;
  481. struct v4l2_ctrl *ctrl_alpha;
  482. bool ctrls_rdy;
  483. };
  484. #define fh_to_ctx(__fh) container_of(__fh, struct fimc_ctx, fh)
  485. static inline void set_frame_bounds(struct fimc_frame *f, u32 width, u32 height)
  486. {
  487. f->o_width = width;
  488. f->o_height = height;
  489. f->f_width = width;
  490. f->f_height = height;
  491. }
  492. static inline void set_frame_crop(struct fimc_frame *f,
  493. u32 left, u32 top, u32 width, u32 height)
  494. {
  495. f->offs_h = left;
  496. f->offs_v = top;
  497. f->width = width;
  498. f->height = height;
  499. }
  500. static inline u32 fimc_get_format_depth(struct fimc_fmt *ff)
  501. {
  502. u32 i, depth = 0;
  503. if (ff != NULL)
  504. for (i = 0; i < ff->colplanes; i++)
  505. depth += ff->depth[i];
  506. return depth;
  507. }
  508. static inline bool fimc_capture_active(struct fimc_dev *fimc)
  509. {
  510. unsigned long flags;
  511. bool ret;
  512. spin_lock_irqsave(&fimc->slock, flags);
  513. ret = !!(fimc->state & (1 << ST_CAPT_RUN) ||
  514. fimc->state & (1 << ST_CAPT_PEND));
  515. spin_unlock_irqrestore(&fimc->slock, flags);
  516. return ret;
  517. }
  518. static inline void fimc_ctx_state_lock_set(u32 state, struct fimc_ctx *ctx)
  519. {
  520. unsigned long flags;
  521. spin_lock_irqsave(&ctx->slock, flags);
  522. ctx->state |= state;
  523. spin_unlock_irqrestore(&ctx->slock, flags);
  524. }
  525. static inline bool fimc_ctx_state_is_set(u32 mask, struct fimc_ctx *ctx)
  526. {
  527. unsigned long flags;
  528. bool ret;
  529. spin_lock_irqsave(&ctx->slock, flags);
  530. ret = (ctx->state & mask) == mask;
  531. spin_unlock_irqrestore(&ctx->slock, flags);
  532. return ret;
  533. }
  534. static inline int tiled_fmt(struct fimc_fmt *fmt)
  535. {
  536. return fmt->fourcc == V4L2_PIX_FMT_NV12MT;
  537. }
  538. /* Return the alpha component bit mask */
  539. static inline int fimc_get_alpha_mask(struct fimc_fmt *fmt)
  540. {
  541. switch (fmt->color) {
  542. case S5P_FIMC_RGB444: return 0x0f;
  543. case S5P_FIMC_RGB555: return 0x01;
  544. case S5P_FIMC_RGB888: return 0xff;
  545. default: return 0;
  546. };
  547. }
  548. static inline void fimc_hw_clear_irq(struct fimc_dev *dev)
  549. {
  550. u32 cfg = readl(dev->regs + S5P_CIGCTRL);
  551. cfg |= S5P_CIGCTRL_IRQ_CLR;
  552. writel(cfg, dev->regs + S5P_CIGCTRL);
  553. }
  554. static inline void fimc_hw_enable_scaler(struct fimc_dev *dev, bool on)
  555. {
  556. u32 cfg = readl(dev->regs + S5P_CISCCTRL);
  557. if (on)
  558. cfg |= S5P_CISCCTRL_SCALERSTART;
  559. else
  560. cfg &= ~S5P_CISCCTRL_SCALERSTART;
  561. writel(cfg, dev->regs + S5P_CISCCTRL);
  562. }
  563. static inline void fimc_hw_activate_input_dma(struct fimc_dev *dev, bool on)
  564. {
  565. u32 cfg = readl(dev->regs + S5P_MSCTRL);
  566. if (on)
  567. cfg |= S5P_MSCTRL_ENVID;
  568. else
  569. cfg &= ~S5P_MSCTRL_ENVID;
  570. writel(cfg, dev->regs + S5P_MSCTRL);
  571. }
  572. static inline void fimc_hw_dis_capture(struct fimc_dev *dev)
  573. {
  574. u32 cfg = readl(dev->regs + S5P_CIIMGCPT);
  575. cfg &= ~(S5P_CIIMGCPT_IMGCPTEN | S5P_CIIMGCPT_IMGCPTEN_SC);
  576. writel(cfg, dev->regs + S5P_CIIMGCPT);
  577. }
  578. /**
  579. * fimc_hw_set_dma_seq - configure output DMA buffer sequence
  580. * @mask: each bit corresponds to one of 32 output buffer registers set
  581. * 1 to include buffer in the sequence, 0 to disable
  582. *
  583. * This function mask output DMA ring buffers, i.e. it allows to configure
  584. * which of the output buffer address registers will be used by the DMA
  585. * engine.
  586. */
  587. static inline void fimc_hw_set_dma_seq(struct fimc_dev *dev, u32 mask)
  588. {
  589. writel(mask, dev->regs + S5P_CIFCNTSEQ);
  590. }
  591. static inline struct fimc_frame *ctx_get_frame(struct fimc_ctx *ctx,
  592. enum v4l2_buf_type type)
  593. {
  594. struct fimc_frame *frame;
  595. if (V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE == type) {
  596. if (fimc_ctx_state_is_set(FIMC_CTX_M2M, ctx))
  597. frame = &ctx->s_frame;
  598. else
  599. return ERR_PTR(-EINVAL);
  600. } else if (V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE == type) {
  601. frame = &ctx->d_frame;
  602. } else {
  603. v4l2_err(ctx->fimc_dev->v4l2_dev,
  604. "Wrong buffer/video queue type (%d)\n", type);
  605. return ERR_PTR(-EINVAL);
  606. }
  607. return frame;
  608. }
  609. /* Return an index to the buffer actually being written. */
  610. static inline u32 fimc_hw_get_frame_index(struct fimc_dev *dev)
  611. {
  612. u32 reg;
  613. if (dev->variant->has_cistatus2) {
  614. reg = readl(dev->regs + S5P_CISTATUS2) & 0x3F;
  615. return reg > 0 ? --reg : reg;
  616. } else {
  617. reg = readl(dev->regs + S5P_CISTATUS);
  618. return (reg & S5P_CISTATUS_FRAMECNT_MASK) >>
  619. S5P_CISTATUS_FRAMECNT_SHIFT;
  620. }
  621. }
  622. /* -----------------------------------------------------*/
  623. /* fimc-reg.c */
  624. void fimc_hw_reset(struct fimc_dev *fimc);
  625. void fimc_hw_set_rotation(struct fimc_ctx *ctx);
  626. void fimc_hw_set_target_format(struct fimc_ctx *ctx);
  627. void fimc_hw_set_out_dma(struct fimc_ctx *ctx);
  628. void fimc_hw_en_lastirq(struct fimc_dev *fimc, int enable);
  629. void fimc_hw_en_irq(struct fimc_dev *fimc, int enable);
  630. void fimc_hw_set_prescaler(struct fimc_ctx *ctx);
  631. void fimc_hw_set_mainscaler(struct fimc_ctx *ctx);
  632. void fimc_hw_en_capture(struct fimc_ctx *ctx);
  633. void fimc_hw_set_effect(struct fimc_ctx *ctx, bool active);
  634. void fimc_hw_set_rgb_alpha(struct fimc_ctx *ctx);
  635. void fimc_hw_set_in_dma(struct fimc_ctx *ctx);
  636. void fimc_hw_set_input_path(struct fimc_ctx *ctx);
  637. void fimc_hw_set_output_path(struct fimc_ctx *ctx);
  638. void fimc_hw_set_input_addr(struct fimc_dev *fimc, struct fimc_addr *paddr);
  639. void fimc_hw_set_output_addr(struct fimc_dev *fimc, struct fimc_addr *paddr,
  640. int index);
  641. int fimc_hw_set_camera_source(struct fimc_dev *fimc,
  642. struct s5p_fimc_isp_info *cam);
  643. int fimc_hw_set_camera_offset(struct fimc_dev *fimc, struct fimc_frame *f);
  644. int fimc_hw_set_camera_polarity(struct fimc_dev *fimc,
  645. struct s5p_fimc_isp_info *cam);
  646. int fimc_hw_set_camera_type(struct fimc_dev *fimc,
  647. struct s5p_fimc_isp_info *cam);
  648. /* -----------------------------------------------------*/
  649. /* fimc-core.c */
  650. int fimc_vidioc_enum_fmt_mplane(struct file *file, void *priv,
  651. struct v4l2_fmtdesc *f);
  652. int fimc_ctrls_create(struct fimc_ctx *ctx);
  653. void fimc_ctrls_delete(struct fimc_ctx *ctx);
  654. void fimc_ctrls_activate(struct fimc_ctx *ctx, bool active);
  655. void fimc_alpha_ctrl_update(struct fimc_ctx *ctx);
  656. int fimc_fill_format(struct fimc_frame *frame, struct v4l2_format *f);
  657. void fimc_adjust_mplane_format(struct fimc_fmt *fmt, u32 width, u32 height,
  658. struct v4l2_pix_format_mplane *pix);
  659. struct fimc_fmt *fimc_find_format(const u32 *pixelformat, const u32 *mbus_code,
  660. unsigned int mask, int index);
  661. int fimc_check_scaler_ratio(struct fimc_ctx *ctx, int sw, int sh,
  662. int dw, int dh, int rotation);
  663. int fimc_set_scaler_info(struct fimc_ctx *ctx);
  664. int fimc_prepare_config(struct fimc_ctx *ctx, u32 flags);
  665. int fimc_prepare_addr(struct fimc_ctx *ctx, struct vb2_buffer *vb,
  666. struct fimc_frame *frame, struct fimc_addr *paddr);
  667. void fimc_prepare_dma_offset(struct fimc_ctx *ctx, struct fimc_frame *f);
  668. void fimc_set_yuv_order(struct fimc_ctx *ctx);
  669. void fimc_fill_frame(struct fimc_frame *frame, struct v4l2_format *f);
  670. void fimc_capture_irq_handler(struct fimc_dev *fimc, bool done);
  671. int fimc_register_m2m_device(struct fimc_dev *fimc,
  672. struct v4l2_device *v4l2_dev);
  673. void fimc_unregister_m2m_device(struct fimc_dev *fimc);
  674. int fimc_register_driver(void);
  675. void fimc_unregister_driver(void);
  676. /* -----------------------------------------------------*/
  677. /* fimc-capture.c */
  678. int fimc_register_capture_device(struct fimc_dev *fimc,
  679. struct v4l2_device *v4l2_dev);
  680. void fimc_unregister_capture_device(struct fimc_dev *fimc);
  681. int fimc_capture_ctrls_create(struct fimc_dev *fimc);
  682. int fimc_vid_cap_buf_queue(struct fimc_dev *fimc,
  683. struct fimc_vid_buffer *fimc_vb);
  684. void fimc_sensor_notify(struct v4l2_subdev *sd, unsigned int notification,
  685. void *arg);
  686. int fimc_capture_suspend(struct fimc_dev *fimc);
  687. int fimc_capture_resume(struct fimc_dev *fimc);
  688. int fimc_capture_config_update(struct fimc_ctx *ctx);
  689. /* Locking: the caller holds fimc->slock */
  690. static inline void fimc_activate_capture(struct fimc_ctx *ctx)
  691. {
  692. fimc_hw_enable_scaler(ctx->fimc_dev, ctx->scaler.enabled);
  693. fimc_hw_en_capture(ctx);
  694. }
  695. static inline void fimc_deactivate_capture(struct fimc_dev *fimc)
  696. {
  697. fimc_hw_en_lastirq(fimc, true);
  698. fimc_hw_dis_capture(fimc);
  699. fimc_hw_enable_scaler(fimc, false);
  700. fimc_hw_en_lastirq(fimc, false);
  701. }
  702. /*
  703. * Buffer list manipulation functions. Must be called with fimc.slock held.
  704. */
  705. /**
  706. * fimc_active_queue_add - add buffer to the capture active buffers queue
  707. * @buf: buffer to add to the active buffers list
  708. */
  709. static inline void fimc_active_queue_add(struct fimc_vid_cap *vid_cap,
  710. struct fimc_vid_buffer *buf)
  711. {
  712. list_add_tail(&buf->list, &vid_cap->active_buf_q);
  713. vid_cap->active_buf_cnt++;
  714. }
  715. /**
  716. * fimc_active_queue_pop - pop buffer from the capture active buffers queue
  717. *
  718. * The caller must assure the active_buf_q list is not empty.
  719. */
  720. static inline struct fimc_vid_buffer *fimc_active_queue_pop(
  721. struct fimc_vid_cap *vid_cap)
  722. {
  723. struct fimc_vid_buffer *buf;
  724. buf = list_entry(vid_cap->active_buf_q.next,
  725. struct fimc_vid_buffer, list);
  726. list_del(&buf->list);
  727. vid_cap->active_buf_cnt--;
  728. return buf;
  729. }
  730. /**
  731. * fimc_pending_queue_add - add buffer to the capture pending buffers queue
  732. * @buf: buffer to add to the pending buffers list
  733. */
  734. static inline void fimc_pending_queue_add(struct fimc_vid_cap *vid_cap,
  735. struct fimc_vid_buffer *buf)
  736. {
  737. list_add_tail(&buf->list, &vid_cap->pending_buf_q);
  738. }
  739. /**
  740. * fimc_pending_queue_pop - pop buffer from the capture pending buffers queue
  741. *
  742. * The caller must assure the pending_buf_q list is not empty.
  743. */
  744. static inline struct fimc_vid_buffer *fimc_pending_queue_pop(
  745. struct fimc_vid_cap *vid_cap)
  746. {
  747. struct fimc_vid_buffer *buf;
  748. buf = list_entry(vid_cap->pending_buf_q.next,
  749. struct fimc_vid_buffer, list);
  750. list_del(&buf->list);
  751. return buf;
  752. }
  753. #endif /* FIMC_CORE_H_ */