18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0-only */ 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * Copyright (C) 2015 VanguardiaSur - www.vanguardiasur.com.ar 48c2ecf20Sopenharmony_ci * 58c2ecf20Sopenharmony_ci * Copyright (C) 2015 Industrial Research Institute for Automation 68c2ecf20Sopenharmony_ci * and Measurements PIAP 78c2ecf20Sopenharmony_ci * Written by Krzysztof Ha?asa 88c2ecf20Sopenharmony_ci */ 98c2ecf20Sopenharmony_ci 108c2ecf20Sopenharmony_ci#include <linux/mutex.h> 118c2ecf20Sopenharmony_ci#include <linux/pci.h> 128c2ecf20Sopenharmony_ci#include <linux/timer.h> 138c2ecf20Sopenharmony_ci#include <linux/videodev2.h> 148c2ecf20Sopenharmony_ci#include <media/v4l2-common.h> 158c2ecf20Sopenharmony_ci#include <media/v4l2-ctrls.h> 168c2ecf20Sopenharmony_ci#include <media/v4l2-device.h> 178c2ecf20Sopenharmony_ci#include <media/v4l2-ioctl.h> 188c2ecf20Sopenharmony_ci#include <media/videobuf2-v4l2.h> 198c2ecf20Sopenharmony_ci#include <sound/pcm.h> 208c2ecf20Sopenharmony_ci 218c2ecf20Sopenharmony_ci#include "tw686x-regs.h" 228c2ecf20Sopenharmony_ci 238c2ecf20Sopenharmony_ci#define TYPE_MAX_CHANNELS 0x0f 248c2ecf20Sopenharmony_ci#define TYPE_SECOND_GEN 0x10 258c2ecf20Sopenharmony_ci#define TW686X_DEF_PHASE_REF 0x1518 268c2ecf20Sopenharmony_ci 278c2ecf20Sopenharmony_ci#define TW686X_AUDIO_PAGE_MAX 16 288c2ecf20Sopenharmony_ci#define TW686X_AUDIO_PERIODS_MIN 2 298c2ecf20Sopenharmony_ci#define TW686X_AUDIO_PERIODS_MAX TW686X_AUDIO_PAGE_MAX 308c2ecf20Sopenharmony_ci 318c2ecf20Sopenharmony_ci#define TW686X_DMA_MODE_MEMCPY 0 328c2ecf20Sopenharmony_ci#define TW686X_DMA_MODE_CONTIG 1 338c2ecf20Sopenharmony_ci#define TW686X_DMA_MODE_SG 2 348c2ecf20Sopenharmony_ci 358c2ecf20Sopenharmony_cistruct tw686x_format { 368c2ecf20Sopenharmony_ci char *name; 378c2ecf20Sopenharmony_ci unsigned int fourcc; 388c2ecf20Sopenharmony_ci unsigned int depth; 398c2ecf20Sopenharmony_ci unsigned int mode; 408c2ecf20Sopenharmony_ci}; 418c2ecf20Sopenharmony_ci 428c2ecf20Sopenharmony_cistruct tw686x_dma_desc { 438c2ecf20Sopenharmony_ci dma_addr_t phys; 448c2ecf20Sopenharmony_ci void *virt; 458c2ecf20Sopenharmony_ci unsigned int size; 468c2ecf20Sopenharmony_ci}; 478c2ecf20Sopenharmony_ci 488c2ecf20Sopenharmony_cistruct tw686x_sg_desc { 498c2ecf20Sopenharmony_ci /* 3 MSBits for flags, 13 LSBits for length */ 508c2ecf20Sopenharmony_ci __le32 flags_length; 518c2ecf20Sopenharmony_ci __le32 phys; 528c2ecf20Sopenharmony_ci}; 538c2ecf20Sopenharmony_ci 548c2ecf20Sopenharmony_cistruct tw686x_audio_buf { 558c2ecf20Sopenharmony_ci dma_addr_t dma; 568c2ecf20Sopenharmony_ci void *virt; 578c2ecf20Sopenharmony_ci struct list_head list; 588c2ecf20Sopenharmony_ci}; 598c2ecf20Sopenharmony_ci 608c2ecf20Sopenharmony_cistruct tw686x_v4l2_buf { 618c2ecf20Sopenharmony_ci struct vb2_v4l2_buffer vb; 628c2ecf20Sopenharmony_ci struct list_head list; 638c2ecf20Sopenharmony_ci}; 648c2ecf20Sopenharmony_ci 658c2ecf20Sopenharmony_cistruct tw686x_audio_channel { 668c2ecf20Sopenharmony_ci struct tw686x_dev *dev; 678c2ecf20Sopenharmony_ci struct snd_pcm_substream *ss; 688c2ecf20Sopenharmony_ci unsigned int ch; 698c2ecf20Sopenharmony_ci struct tw686x_audio_buf *curr_bufs[2]; 708c2ecf20Sopenharmony_ci struct tw686x_dma_desc dma_descs[2]; 718c2ecf20Sopenharmony_ci dma_addr_t ptr; 728c2ecf20Sopenharmony_ci 738c2ecf20Sopenharmony_ci struct tw686x_audio_buf buf[TW686X_AUDIO_PAGE_MAX]; 748c2ecf20Sopenharmony_ci struct list_head buf_list; 758c2ecf20Sopenharmony_ci spinlock_t lock; 768c2ecf20Sopenharmony_ci}; 778c2ecf20Sopenharmony_ci 788c2ecf20Sopenharmony_cistruct tw686x_video_channel { 798c2ecf20Sopenharmony_ci struct tw686x_dev *dev; 808c2ecf20Sopenharmony_ci 818c2ecf20Sopenharmony_ci struct vb2_queue vidq; 828c2ecf20Sopenharmony_ci struct list_head vidq_queued; 838c2ecf20Sopenharmony_ci struct video_device *device; 848c2ecf20Sopenharmony_ci struct tw686x_v4l2_buf *curr_bufs[2]; 858c2ecf20Sopenharmony_ci struct tw686x_dma_desc dma_descs[2]; 868c2ecf20Sopenharmony_ci struct tw686x_sg_desc *sg_descs[2]; 878c2ecf20Sopenharmony_ci 888c2ecf20Sopenharmony_ci struct v4l2_ctrl_handler ctrl_handler; 898c2ecf20Sopenharmony_ci const struct tw686x_format *format; 908c2ecf20Sopenharmony_ci struct mutex vb_mutex; 918c2ecf20Sopenharmony_ci spinlock_t qlock; 928c2ecf20Sopenharmony_ci v4l2_std_id video_standard; 938c2ecf20Sopenharmony_ci unsigned int width, height; 948c2ecf20Sopenharmony_ci unsigned int h_halve, v_halve; 958c2ecf20Sopenharmony_ci unsigned int ch; 968c2ecf20Sopenharmony_ci unsigned int num; 978c2ecf20Sopenharmony_ci unsigned int fps; 988c2ecf20Sopenharmony_ci unsigned int input; 998c2ecf20Sopenharmony_ci unsigned int sequence; 1008c2ecf20Sopenharmony_ci unsigned int pb; 1018c2ecf20Sopenharmony_ci bool no_signal; 1028c2ecf20Sopenharmony_ci}; 1038c2ecf20Sopenharmony_ci 1048c2ecf20Sopenharmony_cistruct tw686x_dma_ops { 1058c2ecf20Sopenharmony_ci int (*setup)(struct tw686x_dev *dev); 1068c2ecf20Sopenharmony_ci int (*alloc)(struct tw686x_video_channel *vc, unsigned int pb); 1078c2ecf20Sopenharmony_ci void (*free)(struct tw686x_video_channel *vc, unsigned int pb); 1088c2ecf20Sopenharmony_ci void (*buf_refill)(struct tw686x_video_channel *vc, unsigned int pb); 1098c2ecf20Sopenharmony_ci const struct vb2_mem_ops *mem_ops; 1108c2ecf20Sopenharmony_ci enum v4l2_field field; 1118c2ecf20Sopenharmony_ci u32 hw_dma_mode; 1128c2ecf20Sopenharmony_ci}; 1138c2ecf20Sopenharmony_ci 1148c2ecf20Sopenharmony_ci/** 1158c2ecf20Sopenharmony_ci * struct tw686x_dev - global device status 1168c2ecf20Sopenharmony_ci * @lock: spinlock controlling access to the 1178c2ecf20Sopenharmony_ci * shared device registers (DMA enable/disable). 1188c2ecf20Sopenharmony_ci */ 1198c2ecf20Sopenharmony_cistruct tw686x_dev { 1208c2ecf20Sopenharmony_ci spinlock_t lock; 1218c2ecf20Sopenharmony_ci 1228c2ecf20Sopenharmony_ci struct v4l2_device v4l2_dev; 1238c2ecf20Sopenharmony_ci struct snd_card *snd_card; 1248c2ecf20Sopenharmony_ci 1258c2ecf20Sopenharmony_ci char name[32]; 1268c2ecf20Sopenharmony_ci unsigned int type; 1278c2ecf20Sopenharmony_ci unsigned int dma_mode; 1288c2ecf20Sopenharmony_ci struct pci_dev *pci_dev; 1298c2ecf20Sopenharmony_ci __u32 __iomem *mmio; 1308c2ecf20Sopenharmony_ci 1318c2ecf20Sopenharmony_ci const struct tw686x_dma_ops *dma_ops; 1328c2ecf20Sopenharmony_ci struct tw686x_video_channel *video_channels; 1338c2ecf20Sopenharmony_ci struct tw686x_audio_channel *audio_channels; 1348c2ecf20Sopenharmony_ci 1358c2ecf20Sopenharmony_ci /* Per-device audio parameters */ 1368c2ecf20Sopenharmony_ci int audio_rate; 1378c2ecf20Sopenharmony_ci int period_size; 1388c2ecf20Sopenharmony_ci int audio_enabled; 1398c2ecf20Sopenharmony_ci 1408c2ecf20Sopenharmony_ci struct timer_list dma_delay_timer; 1418c2ecf20Sopenharmony_ci u32 pending_dma_en; /* must be protected by lock */ 1428c2ecf20Sopenharmony_ci u32 pending_dma_cmd; /* must be protected by lock */ 1438c2ecf20Sopenharmony_ci}; 1448c2ecf20Sopenharmony_ci 1458c2ecf20Sopenharmony_cistatic inline uint32_t reg_read(struct tw686x_dev *dev, unsigned int reg) 1468c2ecf20Sopenharmony_ci{ 1478c2ecf20Sopenharmony_ci return readl(dev->mmio + reg); 1488c2ecf20Sopenharmony_ci} 1498c2ecf20Sopenharmony_ci 1508c2ecf20Sopenharmony_cistatic inline void reg_write(struct tw686x_dev *dev, unsigned int reg, 1518c2ecf20Sopenharmony_ci uint32_t value) 1528c2ecf20Sopenharmony_ci{ 1538c2ecf20Sopenharmony_ci writel(value, dev->mmio + reg); 1548c2ecf20Sopenharmony_ci} 1558c2ecf20Sopenharmony_ci 1568c2ecf20Sopenharmony_cistatic inline unsigned int max_channels(struct tw686x_dev *dev) 1578c2ecf20Sopenharmony_ci{ 1588c2ecf20Sopenharmony_ci return dev->type & TYPE_MAX_CHANNELS; /* 4 or 8 channels */ 1598c2ecf20Sopenharmony_ci} 1608c2ecf20Sopenharmony_ci 1618c2ecf20Sopenharmony_cistatic inline unsigned is_second_gen(struct tw686x_dev *dev) 1628c2ecf20Sopenharmony_ci{ 1638c2ecf20Sopenharmony_ci /* each channel has its own DMA SG table */ 1648c2ecf20Sopenharmony_ci return dev->type & TYPE_SECOND_GEN; 1658c2ecf20Sopenharmony_ci} 1668c2ecf20Sopenharmony_ci 1678c2ecf20Sopenharmony_civoid tw686x_enable_channel(struct tw686x_dev *dev, unsigned int channel); 1688c2ecf20Sopenharmony_civoid tw686x_disable_channel(struct tw686x_dev *dev, unsigned int channel); 1698c2ecf20Sopenharmony_ci 1708c2ecf20Sopenharmony_ciint tw686x_video_init(struct tw686x_dev *dev); 1718c2ecf20Sopenharmony_civoid tw686x_video_free(struct tw686x_dev *dev); 1728c2ecf20Sopenharmony_civoid tw686x_video_irq(struct tw686x_dev *dev, unsigned long requests, 1738c2ecf20Sopenharmony_ci unsigned int pb_status, unsigned int fifo_status, 1748c2ecf20Sopenharmony_ci unsigned int *reset_ch); 1758c2ecf20Sopenharmony_ci 1768c2ecf20Sopenharmony_ciint tw686x_audio_init(struct tw686x_dev *dev); 1778c2ecf20Sopenharmony_civoid tw686x_audio_free(struct tw686x_dev *dev); 1788c2ecf20Sopenharmony_civoid tw686x_audio_irq(struct tw686x_dev *dev, unsigned long requests, 1798c2ecf20Sopenharmony_ci unsigned int pb_status); 180