18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0-or-later */ 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * STK1160 driver 48c2ecf20Sopenharmony_ci * 58c2ecf20Sopenharmony_ci * Copyright (C) 2012 Ezequiel Garcia 68c2ecf20Sopenharmony_ci * <elezegarcia--a.t--gmail.com> 78c2ecf20Sopenharmony_ci * 88c2ecf20Sopenharmony_ci * Based on Easycap driver by R.M. Thomas 98c2ecf20Sopenharmony_ci * Copyright (C) 2010 R.M. Thomas 108c2ecf20Sopenharmony_ci * <rmthomas--a.t--sciolus.org> 118c2ecf20Sopenharmony_ci */ 128c2ecf20Sopenharmony_ci 138c2ecf20Sopenharmony_ci#include <linux/i2c.h> 148c2ecf20Sopenharmony_ci#include <sound/core.h> 158c2ecf20Sopenharmony_ci#include <sound/ac97_codec.h> 168c2ecf20Sopenharmony_ci#include <media/videobuf2-v4l2.h> 178c2ecf20Sopenharmony_ci#include <media/v4l2-device.h> 188c2ecf20Sopenharmony_ci#include <media/v4l2-ctrls.h> 198c2ecf20Sopenharmony_ci 208c2ecf20Sopenharmony_ci#define STK1160_VERSION "0.9.5" 218c2ecf20Sopenharmony_ci#define STK1160_VERSION_NUM 0x000905 228c2ecf20Sopenharmony_ci 238c2ecf20Sopenharmony_ci/* Decide on number of packets for each buffer */ 248c2ecf20Sopenharmony_ci#define STK1160_NUM_PACKETS 64 258c2ecf20Sopenharmony_ci 268c2ecf20Sopenharmony_ci/* Number of buffers for isoc transfers */ 278c2ecf20Sopenharmony_ci#define STK1160_NUM_BUFS 16 288c2ecf20Sopenharmony_ci#define STK1160_MIN_BUFS 1 298c2ecf20Sopenharmony_ci 308c2ecf20Sopenharmony_ci/* TODO: This endpoint address should be retrieved */ 318c2ecf20Sopenharmony_ci#define STK1160_EP_VIDEO 0x82 328c2ecf20Sopenharmony_ci#define STK1160_EP_AUDIO 0x81 338c2ecf20Sopenharmony_ci 348c2ecf20Sopenharmony_ci/* Max and min video buffers */ 358c2ecf20Sopenharmony_ci#define STK1160_MIN_VIDEO_BUFFERS 8 368c2ecf20Sopenharmony_ci#define STK1160_MAX_VIDEO_BUFFERS 32 378c2ecf20Sopenharmony_ci 388c2ecf20Sopenharmony_ci#define STK1160_MIN_PKT_SIZE 3072 398c2ecf20Sopenharmony_ci 408c2ecf20Sopenharmony_ci#define STK1160_MAX_INPUT 4 418c2ecf20Sopenharmony_ci#define STK1160_SVIDEO_INPUT 4 428c2ecf20Sopenharmony_ci 438c2ecf20Sopenharmony_ci#define STK1160_AC97_TIMEOUT 50 448c2ecf20Sopenharmony_ci 458c2ecf20Sopenharmony_ci#define STK1160_I2C_TIMEOUT 100 468c2ecf20Sopenharmony_ci 478c2ecf20Sopenharmony_ci/* TODO: Print helpers 488c2ecf20Sopenharmony_ci * I could use dev_xxx, pr_xxx, v4l2_xxx or printk. 498c2ecf20Sopenharmony_ci * However, there isn't a solid consensus on which 508c2ecf20Sopenharmony_ci * new drivers should use. 518c2ecf20Sopenharmony_ci * 528c2ecf20Sopenharmony_ci */ 538c2ecf20Sopenharmony_ci#ifdef DEBUG 548c2ecf20Sopenharmony_ci#define stk1160_dbg(fmt, args...) \ 558c2ecf20Sopenharmony_ci printk(KERN_DEBUG "stk1160: " fmt, ## args) 568c2ecf20Sopenharmony_ci#else 578c2ecf20Sopenharmony_ci#define stk1160_dbg(fmt, args...) 588c2ecf20Sopenharmony_ci#endif 598c2ecf20Sopenharmony_ci 608c2ecf20Sopenharmony_ci#define stk1160_info(fmt, args...) \ 618c2ecf20Sopenharmony_ci pr_info("stk1160: " fmt, ## args) 628c2ecf20Sopenharmony_ci 638c2ecf20Sopenharmony_ci#define stk1160_warn(fmt, args...) \ 648c2ecf20Sopenharmony_ci pr_warn("stk1160: " fmt, ## args) 658c2ecf20Sopenharmony_ci 668c2ecf20Sopenharmony_ci#define stk1160_err(fmt, args...) \ 678c2ecf20Sopenharmony_ci pr_err("stk1160: " fmt, ## args) 688c2ecf20Sopenharmony_ci 698c2ecf20Sopenharmony_ci/* Buffer for one video frame */ 708c2ecf20Sopenharmony_cistruct stk1160_buffer { 718c2ecf20Sopenharmony_ci /* common v4l buffer stuff -- must be first */ 728c2ecf20Sopenharmony_ci struct vb2_v4l2_buffer vb; 738c2ecf20Sopenharmony_ci struct list_head list; 748c2ecf20Sopenharmony_ci 758c2ecf20Sopenharmony_ci void *mem; 768c2ecf20Sopenharmony_ci unsigned int length; /* buffer length */ 778c2ecf20Sopenharmony_ci unsigned int bytesused; /* bytes written */ 788c2ecf20Sopenharmony_ci int odd; /* current oddity */ 798c2ecf20Sopenharmony_ci 808c2ecf20Sopenharmony_ci /* 818c2ecf20Sopenharmony_ci * Since we interlace two fields per frame, 828c2ecf20Sopenharmony_ci * this is different from bytesused. 838c2ecf20Sopenharmony_ci */ 848c2ecf20Sopenharmony_ci unsigned int pos; /* current pos inside buffer */ 858c2ecf20Sopenharmony_ci}; 868c2ecf20Sopenharmony_ci 878c2ecf20Sopenharmony_cistruct stk1160_isoc_ctl { 888c2ecf20Sopenharmony_ci /* max packet size of isoc transaction */ 898c2ecf20Sopenharmony_ci int max_pkt_size; 908c2ecf20Sopenharmony_ci 918c2ecf20Sopenharmony_ci /* number of allocated urbs */ 928c2ecf20Sopenharmony_ci int num_bufs; 938c2ecf20Sopenharmony_ci 948c2ecf20Sopenharmony_ci /* urb for isoc transfers */ 958c2ecf20Sopenharmony_ci struct urb **urb; 968c2ecf20Sopenharmony_ci 978c2ecf20Sopenharmony_ci /* transfer buffers for isoc transfer */ 988c2ecf20Sopenharmony_ci char **transfer_buffer; 998c2ecf20Sopenharmony_ci 1008c2ecf20Sopenharmony_ci /* current buffer */ 1018c2ecf20Sopenharmony_ci struct stk1160_buffer *buf; 1028c2ecf20Sopenharmony_ci}; 1038c2ecf20Sopenharmony_ci 1048c2ecf20Sopenharmony_cistruct stk1160_fmt { 1058c2ecf20Sopenharmony_ci u32 fourcc; /* v4l2 format id */ 1068c2ecf20Sopenharmony_ci int depth; 1078c2ecf20Sopenharmony_ci}; 1088c2ecf20Sopenharmony_ci 1098c2ecf20Sopenharmony_cistruct stk1160 { 1108c2ecf20Sopenharmony_ci struct v4l2_device v4l2_dev; 1118c2ecf20Sopenharmony_ci struct video_device vdev; 1128c2ecf20Sopenharmony_ci struct v4l2_ctrl_handler ctrl_handler; 1138c2ecf20Sopenharmony_ci 1148c2ecf20Sopenharmony_ci struct device *dev; 1158c2ecf20Sopenharmony_ci struct usb_device *udev; 1168c2ecf20Sopenharmony_ci 1178c2ecf20Sopenharmony_ci /* saa7115 subdev */ 1188c2ecf20Sopenharmony_ci struct v4l2_subdev *sd_saa7115; 1198c2ecf20Sopenharmony_ci 1208c2ecf20Sopenharmony_ci /* isoc control struct */ 1218c2ecf20Sopenharmony_ci struct list_head avail_bufs; 1228c2ecf20Sopenharmony_ci 1238c2ecf20Sopenharmony_ci /* video capture */ 1248c2ecf20Sopenharmony_ci struct vb2_queue vb_vidq; 1258c2ecf20Sopenharmony_ci 1268c2ecf20Sopenharmony_ci /* max packet size of isoc transaction */ 1278c2ecf20Sopenharmony_ci int max_pkt_size; 1288c2ecf20Sopenharmony_ci /* array of wMaxPacketSize */ 1298c2ecf20Sopenharmony_ci unsigned int *alt_max_pkt_size; 1308c2ecf20Sopenharmony_ci /* alternate */ 1318c2ecf20Sopenharmony_ci int alt; 1328c2ecf20Sopenharmony_ci /* Number of alternative settings */ 1338c2ecf20Sopenharmony_ci int num_alt; 1348c2ecf20Sopenharmony_ci 1358c2ecf20Sopenharmony_ci struct stk1160_isoc_ctl isoc_ctl; 1368c2ecf20Sopenharmony_ci 1378c2ecf20Sopenharmony_ci /* frame properties */ 1388c2ecf20Sopenharmony_ci int width; /* current frame width */ 1398c2ecf20Sopenharmony_ci int height; /* current frame height */ 1408c2ecf20Sopenharmony_ci unsigned int ctl_input; /* selected input */ 1418c2ecf20Sopenharmony_ci v4l2_std_id norm; /* current norm */ 1428c2ecf20Sopenharmony_ci struct stk1160_fmt *fmt; /* selected format */ 1438c2ecf20Sopenharmony_ci 1448c2ecf20Sopenharmony_ci unsigned int sequence; 1458c2ecf20Sopenharmony_ci 1468c2ecf20Sopenharmony_ci /* i2c i/o */ 1478c2ecf20Sopenharmony_ci struct i2c_adapter i2c_adap; 1488c2ecf20Sopenharmony_ci struct i2c_client i2c_client; 1498c2ecf20Sopenharmony_ci 1508c2ecf20Sopenharmony_ci struct mutex v4l_lock; 1518c2ecf20Sopenharmony_ci struct mutex vb_queue_lock; 1528c2ecf20Sopenharmony_ci spinlock_t buf_lock; 1538c2ecf20Sopenharmony_ci 1548c2ecf20Sopenharmony_ci struct file *fh_owner; /* filehandle ownership */ 1558c2ecf20Sopenharmony_ci 1568c2ecf20Sopenharmony_ci /* EXPERIMENTAL */ 1578c2ecf20Sopenharmony_ci struct snd_card *snd_card; 1588c2ecf20Sopenharmony_ci}; 1598c2ecf20Sopenharmony_ci 1608c2ecf20Sopenharmony_cistruct regval { 1618c2ecf20Sopenharmony_ci u16 reg; 1628c2ecf20Sopenharmony_ci u16 val; 1638c2ecf20Sopenharmony_ci}; 1648c2ecf20Sopenharmony_ci 1658c2ecf20Sopenharmony_ci/* Provided by stk1160-v4l.c */ 1668c2ecf20Sopenharmony_ciint stk1160_vb2_setup(struct stk1160 *dev); 1678c2ecf20Sopenharmony_ciint stk1160_video_register(struct stk1160 *dev); 1688c2ecf20Sopenharmony_civoid stk1160_video_unregister(struct stk1160 *dev); 1698c2ecf20Sopenharmony_civoid stk1160_clear_queue(struct stk1160 *dev, enum vb2_buffer_state vb2_state); 1708c2ecf20Sopenharmony_ci 1718c2ecf20Sopenharmony_ci/* Provided by stk1160-video.c */ 1728c2ecf20Sopenharmony_ciint stk1160_alloc_isoc(struct stk1160 *dev); 1738c2ecf20Sopenharmony_civoid stk1160_free_isoc(struct stk1160 *dev); 1748c2ecf20Sopenharmony_civoid stk1160_cancel_isoc(struct stk1160 *dev); 1758c2ecf20Sopenharmony_civoid stk1160_uninit_isoc(struct stk1160 *dev); 1768c2ecf20Sopenharmony_ci 1778c2ecf20Sopenharmony_ci/* Provided by stk1160-i2c.c */ 1788c2ecf20Sopenharmony_ciint stk1160_i2c_register(struct stk1160 *dev); 1798c2ecf20Sopenharmony_ciint stk1160_i2c_unregister(struct stk1160 *dev); 1808c2ecf20Sopenharmony_ci 1818c2ecf20Sopenharmony_ci/* Provided by stk1160-core.c */ 1828c2ecf20Sopenharmony_ciint stk1160_read_reg(struct stk1160 *dev, u16 reg, u8 *value); 1838c2ecf20Sopenharmony_ciint stk1160_write_reg(struct stk1160 *dev, u16 reg, u16 value); 1848c2ecf20Sopenharmony_ciint stk1160_write_regs_req(struct stk1160 *dev, u8 req, u16 reg, 1858c2ecf20Sopenharmony_ci char *buf, int len); 1868c2ecf20Sopenharmony_ciint stk1160_read_reg_req_len(struct stk1160 *dev, u8 req, u16 reg, 1878c2ecf20Sopenharmony_ci char *buf, int len); 1888c2ecf20Sopenharmony_civoid stk1160_select_input(struct stk1160 *dev); 1898c2ecf20Sopenharmony_ci 1908c2ecf20Sopenharmony_ci/* Provided by stk1160-ac97.c */ 1918c2ecf20Sopenharmony_civoid stk1160_ac97_setup(struct stk1160 *dev); 192