18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0-only */
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Copyright (C) 2013 - 2014 Texas Instruments, Inc.
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Benoit Parrot <bparrot@ti.com>
68c2ecf20Sopenharmony_ci * Lad, Prabhakar <prabhakar.csengg@gmail.com>
78c2ecf20Sopenharmony_ci */
88c2ecf20Sopenharmony_ci
98c2ecf20Sopenharmony_ci#ifndef AM437X_VPFE_H
108c2ecf20Sopenharmony_ci#define AM437X_VPFE_H
118c2ecf20Sopenharmony_ci
128c2ecf20Sopenharmony_ci#include <linux/am437x-vpfe.h>
138c2ecf20Sopenharmony_ci#include <linux/clk.h>
148c2ecf20Sopenharmony_ci#include <linux/completion.h>
158c2ecf20Sopenharmony_ci#include <linux/device.h>
168c2ecf20Sopenharmony_ci#include <linux/io.h>
178c2ecf20Sopenharmony_ci#include <linux/i2c.h>
188c2ecf20Sopenharmony_ci#include <linux/videodev2.h>
198c2ecf20Sopenharmony_ci
208c2ecf20Sopenharmony_ci#include <media/v4l2-dev.h>
218c2ecf20Sopenharmony_ci#include <media/v4l2-device.h>
228c2ecf20Sopenharmony_ci#include <media/v4l2-ioctl.h>
238c2ecf20Sopenharmony_ci#include <media/videobuf2-v4l2.h>
248c2ecf20Sopenharmony_ci#include <media/videobuf2-dma-contig.h>
258c2ecf20Sopenharmony_ci
268c2ecf20Sopenharmony_ci#include "am437x-vpfe_regs.h"
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_cienum vpfe_pin_pol {
298c2ecf20Sopenharmony_ci	VPFE_PINPOL_POSITIVE = 0,
308c2ecf20Sopenharmony_ci	VPFE_PINPOL_NEGATIVE,
318c2ecf20Sopenharmony_ci};
328c2ecf20Sopenharmony_ci
338c2ecf20Sopenharmony_cienum vpfe_hw_if_type {
348c2ecf20Sopenharmony_ci	/* Raw Bayer */
358c2ecf20Sopenharmony_ci	VPFE_RAW_BAYER = 0,
368c2ecf20Sopenharmony_ci	/* BT656 - 8 bit */
378c2ecf20Sopenharmony_ci	VPFE_BT656,
388c2ecf20Sopenharmony_ci	/* BT656 - 10 bit */
398c2ecf20Sopenharmony_ci	VPFE_BT656_10BIT,
408c2ecf20Sopenharmony_ci	/* YCbCr - 8 bit with external sync */
418c2ecf20Sopenharmony_ci	VPFE_YCBCR_SYNC_8,
428c2ecf20Sopenharmony_ci	/* YCbCr - 16 bit with external sync */
438c2ecf20Sopenharmony_ci	VPFE_YCBCR_SYNC_16,
448c2ecf20Sopenharmony_ci};
458c2ecf20Sopenharmony_ci
468c2ecf20Sopenharmony_ci/* interface description */
478c2ecf20Sopenharmony_cistruct vpfe_hw_if_param {
488c2ecf20Sopenharmony_ci	enum vpfe_hw_if_type if_type;
498c2ecf20Sopenharmony_ci	enum vpfe_pin_pol hdpol;
508c2ecf20Sopenharmony_ci	enum vpfe_pin_pol vdpol;
518c2ecf20Sopenharmony_ci	unsigned int bus_width;
528c2ecf20Sopenharmony_ci};
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_ci#define VPFE_MAX_SUBDEV		1
558c2ecf20Sopenharmony_ci#define VPFE_MAX_INPUTS		1
568c2ecf20Sopenharmony_ci
578c2ecf20Sopenharmony_cistruct vpfe_std_info {
588c2ecf20Sopenharmony_ci	int active_pixels;
598c2ecf20Sopenharmony_ci	int active_lines;
608c2ecf20Sopenharmony_ci	/* current frame format */
618c2ecf20Sopenharmony_ci	int frame_format;
628c2ecf20Sopenharmony_ci};
638c2ecf20Sopenharmony_ci
648c2ecf20Sopenharmony_cistruct vpfe_route {
658c2ecf20Sopenharmony_ci	u32 input;
668c2ecf20Sopenharmony_ci	u32 output;
678c2ecf20Sopenharmony_ci};
688c2ecf20Sopenharmony_ci
698c2ecf20Sopenharmony_cistruct vpfe_subdev_info {
708c2ecf20Sopenharmony_ci	/* Sub device group id */
718c2ecf20Sopenharmony_ci	int grp_id;
728c2ecf20Sopenharmony_ci	/* inputs available at the sub device */
738c2ecf20Sopenharmony_ci	struct v4l2_input inputs[VPFE_MAX_INPUTS];
748c2ecf20Sopenharmony_ci	/* Sub dev routing information for each input */
758c2ecf20Sopenharmony_ci	struct vpfe_route *routes;
768c2ecf20Sopenharmony_ci	/* check if sub dev supports routing */
778c2ecf20Sopenharmony_ci	int can_route;
788c2ecf20Sopenharmony_ci	/* ccdc bus/interface configuration */
798c2ecf20Sopenharmony_ci	struct vpfe_hw_if_param vpfe_param;
808c2ecf20Sopenharmony_ci	struct v4l2_subdev *sd;
818c2ecf20Sopenharmony_ci};
828c2ecf20Sopenharmony_ci
838c2ecf20Sopenharmony_cistruct vpfe_config {
848c2ecf20Sopenharmony_ci	/* information about each subdev */
858c2ecf20Sopenharmony_ci	struct vpfe_subdev_info sub_devs[VPFE_MAX_SUBDEV];
868c2ecf20Sopenharmony_ci	/* Flat array, arranged in groups */
878c2ecf20Sopenharmony_ci	struct v4l2_async_subdev *asd[VPFE_MAX_SUBDEV];
888c2ecf20Sopenharmony_ci};
898c2ecf20Sopenharmony_ci
908c2ecf20Sopenharmony_cistruct vpfe_cap_buffer {
918c2ecf20Sopenharmony_ci	struct vb2_v4l2_buffer vb;
928c2ecf20Sopenharmony_ci	struct list_head list;
938c2ecf20Sopenharmony_ci};
948c2ecf20Sopenharmony_ci
958c2ecf20Sopenharmony_cienum ccdc_pixfmt {
968c2ecf20Sopenharmony_ci	CCDC_PIXFMT_RAW = 0,
978c2ecf20Sopenharmony_ci	CCDC_PIXFMT_YCBCR_16BIT,
988c2ecf20Sopenharmony_ci	CCDC_PIXFMT_YCBCR_8BIT,
998c2ecf20Sopenharmony_ci};
1008c2ecf20Sopenharmony_ci
1018c2ecf20Sopenharmony_cienum ccdc_frmfmt {
1028c2ecf20Sopenharmony_ci	CCDC_FRMFMT_PROGRESSIVE = 0,
1038c2ecf20Sopenharmony_ci	CCDC_FRMFMT_INTERLACED,
1048c2ecf20Sopenharmony_ci};
1058c2ecf20Sopenharmony_ci
1068c2ecf20Sopenharmony_ci/* PIXEL ORDER IN MEMORY from LSB to MSB */
1078c2ecf20Sopenharmony_ci/* only applicable for 8-bit input mode  */
1088c2ecf20Sopenharmony_cienum ccdc_pixorder {
1098c2ecf20Sopenharmony_ci	CCDC_PIXORDER_YCBYCR,
1108c2ecf20Sopenharmony_ci	CCDC_PIXORDER_CBYCRY,
1118c2ecf20Sopenharmony_ci};
1128c2ecf20Sopenharmony_ci
1138c2ecf20Sopenharmony_cienum ccdc_buftype {
1148c2ecf20Sopenharmony_ci	CCDC_BUFTYPE_FLD_INTERLEAVED,
1158c2ecf20Sopenharmony_ci	CCDC_BUFTYPE_FLD_SEPARATED
1168c2ecf20Sopenharmony_ci};
1178c2ecf20Sopenharmony_ci
1188c2ecf20Sopenharmony_ci
1198c2ecf20Sopenharmony_ci/* returns the highest bit used for the gamma */
1208c2ecf20Sopenharmony_cistatic inline u8 ccdc_gamma_width_max_bit(enum vpfe_ccdc_gamma_width width)
1218c2ecf20Sopenharmony_ci{
1228c2ecf20Sopenharmony_ci	return 15 - width;
1238c2ecf20Sopenharmony_ci}
1248c2ecf20Sopenharmony_ci
1258c2ecf20Sopenharmony_ci/* returns the highest bit used for this data size */
1268c2ecf20Sopenharmony_cistatic inline u8 ccdc_data_size_max_bit(enum vpfe_ccdc_data_size sz)
1278c2ecf20Sopenharmony_ci{
1288c2ecf20Sopenharmony_ci	return sz == VPFE_CCDC_DATA_8BITS ? 7 : 15 - sz;
1298c2ecf20Sopenharmony_ci}
1308c2ecf20Sopenharmony_ci
1318c2ecf20Sopenharmony_ci/* Structure for CCDC configuration parameters for raw capture mode */
1328c2ecf20Sopenharmony_cistruct ccdc_params_raw {
1338c2ecf20Sopenharmony_ci	/* pixel format */
1348c2ecf20Sopenharmony_ci	enum ccdc_pixfmt pix_fmt;
1358c2ecf20Sopenharmony_ci	/* progressive or interlaced frame */
1368c2ecf20Sopenharmony_ci	enum ccdc_frmfmt frm_fmt;
1378c2ecf20Sopenharmony_ci	struct v4l2_rect win;
1388c2ecf20Sopenharmony_ci	/* Current Format Bytes Per Pixels */
1398c2ecf20Sopenharmony_ci	unsigned int bytesperpixel;
1408c2ecf20Sopenharmony_ci	/* Current Format Bytes per Lines
1418c2ecf20Sopenharmony_ci	 * (Aligned to 32 bytes) used for HORZ_INFO
1428c2ecf20Sopenharmony_ci	 */
1438c2ecf20Sopenharmony_ci	unsigned int bytesperline;
1448c2ecf20Sopenharmony_ci	/* field id polarity */
1458c2ecf20Sopenharmony_ci	enum vpfe_pin_pol fid_pol;
1468c2ecf20Sopenharmony_ci	/* vertical sync polarity */
1478c2ecf20Sopenharmony_ci	enum vpfe_pin_pol vd_pol;
1488c2ecf20Sopenharmony_ci	/* horizontal sync polarity */
1498c2ecf20Sopenharmony_ci	enum vpfe_pin_pol hd_pol;
1508c2ecf20Sopenharmony_ci	/* interleaved or separated fields */
1518c2ecf20Sopenharmony_ci	enum ccdc_buftype buf_type;
1528c2ecf20Sopenharmony_ci	/*
1538c2ecf20Sopenharmony_ci	 * enable to store the image in inverse
1548c2ecf20Sopenharmony_ci	 * order in memory(bottom to top)
1558c2ecf20Sopenharmony_ci	 */
1568c2ecf20Sopenharmony_ci	unsigned char image_invert_enable;
1578c2ecf20Sopenharmony_ci	/* configurable parameters */
1588c2ecf20Sopenharmony_ci	struct vpfe_ccdc_config_params_raw config_params;
1598c2ecf20Sopenharmony_ci};
1608c2ecf20Sopenharmony_ci
1618c2ecf20Sopenharmony_cistruct ccdc_params_ycbcr {
1628c2ecf20Sopenharmony_ci	/* pixel format */
1638c2ecf20Sopenharmony_ci	enum ccdc_pixfmt pix_fmt;
1648c2ecf20Sopenharmony_ci	/* progressive or interlaced frame */
1658c2ecf20Sopenharmony_ci	enum ccdc_frmfmt frm_fmt;
1668c2ecf20Sopenharmony_ci	struct v4l2_rect win;
1678c2ecf20Sopenharmony_ci	/* Current Format Bytes Per Pixels */
1688c2ecf20Sopenharmony_ci	unsigned int bytesperpixel;
1698c2ecf20Sopenharmony_ci	/* Current Format Bytes per Lines
1708c2ecf20Sopenharmony_ci	 * (Aligned to 32 bytes) used for HORZ_INFO
1718c2ecf20Sopenharmony_ci	 */
1728c2ecf20Sopenharmony_ci	unsigned int bytesperline;
1738c2ecf20Sopenharmony_ci	/* field id polarity */
1748c2ecf20Sopenharmony_ci	enum vpfe_pin_pol fid_pol;
1758c2ecf20Sopenharmony_ci	/* vertical sync polarity */
1768c2ecf20Sopenharmony_ci	enum vpfe_pin_pol vd_pol;
1778c2ecf20Sopenharmony_ci	/* horizontal sync polarity */
1788c2ecf20Sopenharmony_ci	enum vpfe_pin_pol hd_pol;
1798c2ecf20Sopenharmony_ci	/* enable BT.656 embedded sync mode */
1808c2ecf20Sopenharmony_ci	int bt656_enable;
1818c2ecf20Sopenharmony_ci	/* cb:y:cr:y or y:cb:y:cr in memory */
1828c2ecf20Sopenharmony_ci	enum ccdc_pixorder pix_order;
1838c2ecf20Sopenharmony_ci	/* interleaved or separated fields  */
1848c2ecf20Sopenharmony_ci	enum ccdc_buftype buf_type;
1858c2ecf20Sopenharmony_ci};
1868c2ecf20Sopenharmony_ci
1878c2ecf20Sopenharmony_ci/*
1888c2ecf20Sopenharmony_ci * CCDC operational configuration
1898c2ecf20Sopenharmony_ci */
1908c2ecf20Sopenharmony_cistruct ccdc_config {
1918c2ecf20Sopenharmony_ci	/* CCDC interface type */
1928c2ecf20Sopenharmony_ci	enum vpfe_hw_if_type if_type;
1938c2ecf20Sopenharmony_ci	/* Raw Bayer configuration */
1948c2ecf20Sopenharmony_ci	struct ccdc_params_raw bayer;
1958c2ecf20Sopenharmony_ci	/* YCbCr configuration */
1968c2ecf20Sopenharmony_ci	struct ccdc_params_ycbcr ycbcr;
1978c2ecf20Sopenharmony_ci	/* ccdc base address */
1988c2ecf20Sopenharmony_ci	void __iomem *base_addr;
1998c2ecf20Sopenharmony_ci};
2008c2ecf20Sopenharmony_ci
2018c2ecf20Sopenharmony_cistruct vpfe_ccdc {
2028c2ecf20Sopenharmony_ci	struct ccdc_config ccdc_cfg;
2038c2ecf20Sopenharmony_ci	u32 ccdc_ctx[VPFE_REG_END / sizeof(u32)];
2048c2ecf20Sopenharmony_ci};
2058c2ecf20Sopenharmony_ci
2068c2ecf20Sopenharmony_ci/*
2078c2ecf20Sopenharmony_ci * struct vpfe_fmt - VPFE media bus format information
2088c2ecf20Sopenharmony_ci * fourcc: V4L2 pixel format code
2098c2ecf20Sopenharmony_ci * code: V4L2 media bus format code
2108c2ecf20Sopenharmony_ci * bitsperpixel: Bits per pixel over the bus
2118c2ecf20Sopenharmony_ci */
2128c2ecf20Sopenharmony_cistruct vpfe_fmt {
2138c2ecf20Sopenharmony_ci	u32 fourcc;
2148c2ecf20Sopenharmony_ci	u32 code;
2158c2ecf20Sopenharmony_ci	u32 bitsperpixel;
2168c2ecf20Sopenharmony_ci};
2178c2ecf20Sopenharmony_ci
2188c2ecf20Sopenharmony_ci/*
2198c2ecf20Sopenharmony_ci * When formats[] is modified make sure to adjust this value also.
2208c2ecf20Sopenharmony_ci * Expect compile time warnings if VPFE_NUM_FORMATS is smaller then
2218c2ecf20Sopenharmony_ci * the number of elements in formats[].
2228c2ecf20Sopenharmony_ci */
2238c2ecf20Sopenharmony_ci#define VPFE_NUM_FORMATS	10
2248c2ecf20Sopenharmony_ci
2258c2ecf20Sopenharmony_cistruct vpfe_device {
2268c2ecf20Sopenharmony_ci	/* V4l2 specific parameters */
2278c2ecf20Sopenharmony_ci	/* Identifies video device for this channel */
2288c2ecf20Sopenharmony_ci	struct video_device video_dev;
2298c2ecf20Sopenharmony_ci	/* sub devices */
2308c2ecf20Sopenharmony_ci	struct v4l2_subdev **sd;
2318c2ecf20Sopenharmony_ci	/* vpfe cfg */
2328c2ecf20Sopenharmony_ci	struct vpfe_config *cfg;
2338c2ecf20Sopenharmony_ci	/* V4l2 device */
2348c2ecf20Sopenharmony_ci	struct v4l2_device v4l2_dev;
2358c2ecf20Sopenharmony_ci	/* parent device */
2368c2ecf20Sopenharmony_ci	struct device *pdev;
2378c2ecf20Sopenharmony_ci	/* subdevice async Notifier */
2388c2ecf20Sopenharmony_ci	struct v4l2_async_notifier notifier;
2398c2ecf20Sopenharmony_ci	/* Indicates id of the field which is being displayed */
2408c2ecf20Sopenharmony_ci	unsigned field;
2418c2ecf20Sopenharmony_ci	unsigned sequence;
2428c2ecf20Sopenharmony_ci	/* current interface type */
2438c2ecf20Sopenharmony_ci	struct vpfe_hw_if_param vpfe_if_params;
2448c2ecf20Sopenharmony_ci	/* ptr to currently selected sub device */
2458c2ecf20Sopenharmony_ci	struct vpfe_subdev_info *current_subdev;
2468c2ecf20Sopenharmony_ci	/* current input at the sub device */
2478c2ecf20Sopenharmony_ci	int current_input;
2488c2ecf20Sopenharmony_ci	/* Keeps track of the information about the standard */
2498c2ecf20Sopenharmony_ci	struct vpfe_std_info std_info;
2508c2ecf20Sopenharmony_ci	/* std index into std table */
2518c2ecf20Sopenharmony_ci	int std_index;
2528c2ecf20Sopenharmony_ci	/* IRQs used when CCDC output to SDRAM */
2538c2ecf20Sopenharmony_ci	unsigned int irq;
2548c2ecf20Sopenharmony_ci	/* Pointer pointing to current v4l2_buffer */
2558c2ecf20Sopenharmony_ci	struct vpfe_cap_buffer *cur_frm;
2568c2ecf20Sopenharmony_ci	/* Pointer pointing to next v4l2_buffer */
2578c2ecf20Sopenharmony_ci	struct vpfe_cap_buffer *next_frm;
2588c2ecf20Sopenharmony_ci	/* Used to store pixel format */
2598c2ecf20Sopenharmony_ci	struct v4l2_format fmt;
2608c2ecf20Sopenharmony_ci	/* Used to keep a reference to the current vpfe_fmt */
2618c2ecf20Sopenharmony_ci	struct vpfe_fmt *current_vpfe_fmt;
2628c2ecf20Sopenharmony_ci	struct vpfe_fmt	*active_fmt[VPFE_NUM_FORMATS];
2638c2ecf20Sopenharmony_ci	unsigned int num_active_fmt;
2648c2ecf20Sopenharmony_ci
2658c2ecf20Sopenharmony_ci	/*
2668c2ecf20Sopenharmony_ci	 * used when IMP is chained to store the crop window which
2678c2ecf20Sopenharmony_ci	 * is different from the image window
2688c2ecf20Sopenharmony_ci	 */
2698c2ecf20Sopenharmony_ci	struct v4l2_rect crop;
2708c2ecf20Sopenharmony_ci	/* Buffer queue used in video-buf */
2718c2ecf20Sopenharmony_ci	struct vb2_queue buffer_queue;
2728c2ecf20Sopenharmony_ci	/* Queue of filled frames */
2738c2ecf20Sopenharmony_ci	struct list_head dma_queue;
2748c2ecf20Sopenharmony_ci	/* IRQ lock for DMA queue */
2758c2ecf20Sopenharmony_ci	spinlock_t dma_queue_lock;
2768c2ecf20Sopenharmony_ci	/* lock used to access this structure */
2778c2ecf20Sopenharmony_ci	struct mutex lock;
2788c2ecf20Sopenharmony_ci	/*
2798c2ecf20Sopenharmony_ci	 * offset where second field starts from the starting of the
2808c2ecf20Sopenharmony_ci	 * buffer for field separated YCbCr formats
2818c2ecf20Sopenharmony_ci	 */
2828c2ecf20Sopenharmony_ci	u32 field_off;
2838c2ecf20Sopenharmony_ci	struct vpfe_ccdc ccdc;
2848c2ecf20Sopenharmony_ci	int stopping;
2858c2ecf20Sopenharmony_ci	struct completion capture_stop;
2868c2ecf20Sopenharmony_ci};
2878c2ecf20Sopenharmony_ci
2888c2ecf20Sopenharmony_ci#endif	/* AM437X_VPFE_H */
289