18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0-or-later */
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci *  tw68 driver common header file
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci *  Much of this code is derived from the cx88 and sa7134 drivers, which
68c2ecf20Sopenharmony_ci *  were in turn derived from the bt87x driver.  The original work was by
78c2ecf20Sopenharmony_ci *  Gerd Knorr; more recently the code was enhanced by Mauro Carvalho Chehab,
88c2ecf20Sopenharmony_ci *  Hans Verkuil, Andy Walls and many others.  Their work is gratefully
98c2ecf20Sopenharmony_ci *  acknowledged.  Full credit goes to them - any problems within this code
108c2ecf20Sopenharmony_ci *  are mine.
118c2ecf20Sopenharmony_ci *
128c2ecf20Sopenharmony_ci *  Copyright (C) 2009  William M. Brack
138c2ecf20Sopenharmony_ci *
148c2ecf20Sopenharmony_ci *  Refactored and updated to the latest v4l core frameworks:
158c2ecf20Sopenharmony_ci *
168c2ecf20Sopenharmony_ci *  Copyright (C) 2014 Hans Verkuil <hverkuil@xs4all.nl>
178c2ecf20Sopenharmony_ci */
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_ci#include <linux/pci.h>
208c2ecf20Sopenharmony_ci#include <linux/videodev2.h>
218c2ecf20Sopenharmony_ci#include <linux/notifier.h>
228c2ecf20Sopenharmony_ci#include <linux/delay.h>
238c2ecf20Sopenharmony_ci#include <linux/mutex.h>
248c2ecf20Sopenharmony_ci#include <linux/io.h>
258c2ecf20Sopenharmony_ci
268c2ecf20Sopenharmony_ci#include <media/v4l2-common.h>
278c2ecf20Sopenharmony_ci#include <media/v4l2-ioctl.h>
288c2ecf20Sopenharmony_ci#include <media/v4l2-ctrls.h>
298c2ecf20Sopenharmony_ci#include <media/v4l2-device.h>
308c2ecf20Sopenharmony_ci#include <media/videobuf2-v4l2.h>
318c2ecf20Sopenharmony_ci#include <media/videobuf2-dma-sg.h>
328c2ecf20Sopenharmony_ci
338c2ecf20Sopenharmony_ci#include "tw68-reg.h"
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_ci#define	UNSET	(-1U)
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_ci#define TW68_NORMS ( \
388c2ecf20Sopenharmony_ci	V4L2_STD_NTSC    | V4L2_STD_PAL       | V4L2_STD_SECAM    | \
398c2ecf20Sopenharmony_ci	V4L2_STD_PAL_M   | V4L2_STD_PAL_Nc    | V4L2_STD_PAL_60)
408c2ecf20Sopenharmony_ci
418c2ecf20Sopenharmony_ci#define	TW68_VID_INTS	(TW68_FFERR | TW68_PABORT | TW68_DMAPERR | \
428c2ecf20Sopenharmony_ci			 TW68_FFOF   | TW68_DMAPI)
438c2ecf20Sopenharmony_ci/* TW6800 chips have trouble with these, so we don't set them for that chip */
448c2ecf20Sopenharmony_ci#define	TW68_VID_INTSX	(TW68_FDMIS | TW68_HLOCK | TW68_VLOCK)
458c2ecf20Sopenharmony_ci
468c2ecf20Sopenharmony_ci#define	TW68_I2C_INTS	(TW68_SBERR | TW68_SBDONE | TW68_SBERR2  | \
478c2ecf20Sopenharmony_ci			 TW68_SBDONE2)
488c2ecf20Sopenharmony_ci
498c2ecf20Sopenharmony_cienum tw68_decoder_type {
508c2ecf20Sopenharmony_ci	TW6800,
518c2ecf20Sopenharmony_ci	TW6801,
528c2ecf20Sopenharmony_ci	TW6804,
538c2ecf20Sopenharmony_ci	TWXXXX,
548c2ecf20Sopenharmony_ci};
558c2ecf20Sopenharmony_ci
568c2ecf20Sopenharmony_ci/* ----------------------------------------------------------- */
578c2ecf20Sopenharmony_ci/* static data                                                 */
588c2ecf20Sopenharmony_ci
598c2ecf20Sopenharmony_cistruct tw68_tvnorm {
608c2ecf20Sopenharmony_ci	char		*name;
618c2ecf20Sopenharmony_ci	v4l2_std_id	id;
628c2ecf20Sopenharmony_ci
638c2ecf20Sopenharmony_ci	/* video decoder */
648c2ecf20Sopenharmony_ci	u32	sync_control;
658c2ecf20Sopenharmony_ci	u32	luma_control;
668c2ecf20Sopenharmony_ci	u32	chroma_ctrl1;
678c2ecf20Sopenharmony_ci	u32	chroma_gain;
688c2ecf20Sopenharmony_ci	u32	chroma_ctrl2;
698c2ecf20Sopenharmony_ci	u32	vgate_misc;
708c2ecf20Sopenharmony_ci
718c2ecf20Sopenharmony_ci	/* video scaler */
728c2ecf20Sopenharmony_ci	u32	h_delay;
738c2ecf20Sopenharmony_ci	u32	h_delay0;	/* for TW6800 */
748c2ecf20Sopenharmony_ci	u32	h_start;
758c2ecf20Sopenharmony_ci	u32	h_stop;
768c2ecf20Sopenharmony_ci	u32	v_delay;
778c2ecf20Sopenharmony_ci	u32	video_v_start;
788c2ecf20Sopenharmony_ci	u32	video_v_stop;
798c2ecf20Sopenharmony_ci	u32	vbi_v_start_0;
808c2ecf20Sopenharmony_ci	u32	vbi_v_stop_0;
818c2ecf20Sopenharmony_ci	u32	vbi_v_start_1;
828c2ecf20Sopenharmony_ci
838c2ecf20Sopenharmony_ci	/* Techwell specific */
848c2ecf20Sopenharmony_ci	u32	format;
858c2ecf20Sopenharmony_ci};
868c2ecf20Sopenharmony_ci
878c2ecf20Sopenharmony_cistruct tw68_format {
888c2ecf20Sopenharmony_ci	u32	fourcc;
898c2ecf20Sopenharmony_ci	u32	depth;
908c2ecf20Sopenharmony_ci	u32	twformat;
918c2ecf20Sopenharmony_ci};
928c2ecf20Sopenharmony_ci
938c2ecf20Sopenharmony_ci/* ----------------------------------------------------------- */
948c2ecf20Sopenharmony_ci/* card configuration					  */
958c2ecf20Sopenharmony_ci
968c2ecf20Sopenharmony_ci#define TW68_BOARD_NOAUTO		UNSET
978c2ecf20Sopenharmony_ci#define TW68_BOARD_UNKNOWN		0
988c2ecf20Sopenharmony_ci#define	TW68_BOARD_GENERIC_6802		1
998c2ecf20Sopenharmony_ci
1008c2ecf20Sopenharmony_ci#define	TW68_MAXBOARDS			16
1018c2ecf20Sopenharmony_ci#define	TW68_INPUT_MAX			4
1028c2ecf20Sopenharmony_ci
1038c2ecf20Sopenharmony_ci/* ----------------------------------------------------------- */
1048c2ecf20Sopenharmony_ci/* device / file handle status                                 */
1058c2ecf20Sopenharmony_ci
1068c2ecf20Sopenharmony_ci#define	BUFFER_TIMEOUT	msecs_to_jiffies(500)	/* 0.5 seconds */
1078c2ecf20Sopenharmony_ci
1088c2ecf20Sopenharmony_cistruct tw68_dev;	/* forward delclaration */
1098c2ecf20Sopenharmony_ci
1108c2ecf20Sopenharmony_ci/* buffer for one video/vbi/ts frame */
1118c2ecf20Sopenharmony_cistruct tw68_buf {
1128c2ecf20Sopenharmony_ci	struct vb2_v4l2_buffer vb;
1138c2ecf20Sopenharmony_ci	struct list_head list;
1148c2ecf20Sopenharmony_ci
1158c2ecf20Sopenharmony_ci	unsigned int   size;
1168c2ecf20Sopenharmony_ci	__le32         *cpu;
1178c2ecf20Sopenharmony_ci	__le32         *jmp;
1188c2ecf20Sopenharmony_ci	dma_addr_t     dma;
1198c2ecf20Sopenharmony_ci};
1208c2ecf20Sopenharmony_ci
1218c2ecf20Sopenharmony_cistruct tw68_fmt {
1228c2ecf20Sopenharmony_ci	char			*name;
1238c2ecf20Sopenharmony_ci	u32			fourcc;	/* v4l2 format id */
1248c2ecf20Sopenharmony_ci	int			depth;
1258c2ecf20Sopenharmony_ci	int			flags;
1268c2ecf20Sopenharmony_ci	u32			twformat;
1278c2ecf20Sopenharmony_ci};
1288c2ecf20Sopenharmony_ci
1298c2ecf20Sopenharmony_ci/* global device status */
1308c2ecf20Sopenharmony_cistruct tw68_dev {
1318c2ecf20Sopenharmony_ci	struct mutex		lock;
1328c2ecf20Sopenharmony_ci	spinlock_t		slock;
1338c2ecf20Sopenharmony_ci	u16			instance;
1348c2ecf20Sopenharmony_ci	struct v4l2_device	v4l2_dev;
1358c2ecf20Sopenharmony_ci
1368c2ecf20Sopenharmony_ci	/* various device info */
1378c2ecf20Sopenharmony_ci	enum tw68_decoder_type	vdecoder;
1388c2ecf20Sopenharmony_ci	struct video_device	vdev;
1398c2ecf20Sopenharmony_ci	struct v4l2_ctrl_handler hdl;
1408c2ecf20Sopenharmony_ci
1418c2ecf20Sopenharmony_ci	/* pci i/o */
1428c2ecf20Sopenharmony_ci	char			*name;
1438c2ecf20Sopenharmony_ci	struct pci_dev		*pci;
1448c2ecf20Sopenharmony_ci	unsigned char		pci_rev, pci_lat;
1458c2ecf20Sopenharmony_ci	u32			__iomem *lmmio;
1468c2ecf20Sopenharmony_ci	u8			__iomem *bmmio;
1478c2ecf20Sopenharmony_ci	u32			pci_irqmask;
1488c2ecf20Sopenharmony_ci	/* The irq mask to be used will depend upon the chip type */
1498c2ecf20Sopenharmony_ci	u32			board_virqmask;
1508c2ecf20Sopenharmony_ci
1518c2ecf20Sopenharmony_ci	/* video capture */
1528c2ecf20Sopenharmony_ci	const struct tw68_format *fmt;
1538c2ecf20Sopenharmony_ci	unsigned		width, height;
1548c2ecf20Sopenharmony_ci	unsigned		seqnr;
1558c2ecf20Sopenharmony_ci	unsigned		field;
1568c2ecf20Sopenharmony_ci	struct vb2_queue	vidq;
1578c2ecf20Sopenharmony_ci	struct list_head	active;
1588c2ecf20Sopenharmony_ci
1598c2ecf20Sopenharmony_ci	/* various v4l controls */
1608c2ecf20Sopenharmony_ci	const struct tw68_tvnorm *tvnorm;	/* video */
1618c2ecf20Sopenharmony_ci
1628c2ecf20Sopenharmony_ci	int			input;
1638c2ecf20Sopenharmony_ci};
1648c2ecf20Sopenharmony_ci
1658c2ecf20Sopenharmony_ci/* ----------------------------------------------------------- */
1668c2ecf20Sopenharmony_ci
1678c2ecf20Sopenharmony_ci#define tw_readl(reg)		readl(dev->lmmio + ((reg) >> 2))
1688c2ecf20Sopenharmony_ci#define	tw_readb(reg)		readb(dev->bmmio + (reg))
1698c2ecf20Sopenharmony_ci#define tw_writel(reg, value)	writel((value), dev->lmmio + ((reg) >> 2))
1708c2ecf20Sopenharmony_ci#define	tw_writeb(reg, value)	writeb((value), dev->bmmio + (reg))
1718c2ecf20Sopenharmony_ci
1728c2ecf20Sopenharmony_ci#define tw_andorl(reg, mask, value) \
1738c2ecf20Sopenharmony_ci		writel((readl(dev->lmmio+((reg)>>2)) & ~(mask)) |\
1748c2ecf20Sopenharmony_ci		((value) & (mask)), dev->lmmio+((reg)>>2))
1758c2ecf20Sopenharmony_ci#define	tw_andorb(reg, mask, value) \
1768c2ecf20Sopenharmony_ci		writeb((readb(dev->bmmio + (reg)) & ~(mask)) |\
1778c2ecf20Sopenharmony_ci		((value) & (mask)), dev->bmmio+(reg))
1788c2ecf20Sopenharmony_ci#define tw_setl(reg, bit)	tw_andorl((reg), (bit), (bit))
1798c2ecf20Sopenharmony_ci#define	tw_setb(reg, bit)	tw_andorb((reg), (bit), (bit))
1808c2ecf20Sopenharmony_ci#define	tw_clearl(reg, bit)	\
1818c2ecf20Sopenharmony_ci		writel((readl(dev->lmmio + ((reg) >> 2)) & ~(bit)), \
1828c2ecf20Sopenharmony_ci		dev->lmmio + ((reg) >> 2))
1838c2ecf20Sopenharmony_ci#define	tw_clearb(reg, bit)	\
1848c2ecf20Sopenharmony_ci		writeb((readb(dev->bmmio+(reg)) & ~(bit)), \
1858c2ecf20Sopenharmony_ci		dev->bmmio + (reg))
1868c2ecf20Sopenharmony_ci
1878c2ecf20Sopenharmony_ci#define tw_wait(us) { udelay(us); }
1888c2ecf20Sopenharmony_ci
1898c2ecf20Sopenharmony_ci/* ----------------------------------------------------------- */
1908c2ecf20Sopenharmony_ci/* tw68-video.c                                                */
1918c2ecf20Sopenharmony_ci
1928c2ecf20Sopenharmony_civoid tw68_set_tvnorm_hw(struct tw68_dev *dev);
1938c2ecf20Sopenharmony_ci
1948c2ecf20Sopenharmony_ciint tw68_video_init1(struct tw68_dev *dev);
1958c2ecf20Sopenharmony_ciint tw68_video_init2(struct tw68_dev *dev, int video_nr);
1968c2ecf20Sopenharmony_civoid tw68_irq_video_done(struct tw68_dev *dev, unsigned long status);
1978c2ecf20Sopenharmony_ciint tw68_video_start_dma(struct tw68_dev *dev, struct tw68_buf *buf);
1988c2ecf20Sopenharmony_ci
1998c2ecf20Sopenharmony_ci/* ----------------------------------------------------------- */
2008c2ecf20Sopenharmony_ci/* tw68-risc.c                                                 */
2018c2ecf20Sopenharmony_ci
2028c2ecf20Sopenharmony_ciint tw68_risc_buffer(struct pci_dev *pci, struct tw68_buf *buf,
2038c2ecf20Sopenharmony_ci	struct scatterlist *sglist, unsigned int top_offset,
2048c2ecf20Sopenharmony_ci	unsigned int bottom_offset, unsigned int bpl,
2058c2ecf20Sopenharmony_ci	unsigned int padding, unsigned int lines);
206