^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) /* SPDX-License-Identifier: GPL-2.0-or-later */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * STK1160 driver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (C) 2012 Ezequiel Garcia
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * <elezegarcia--a.t--gmail.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) * Based on Easycap driver by R.M. Thomas
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) * Copyright (C) 2010 R.M. Thomas
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) * <rmthomas--a.t--sciolus.org>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/i2c.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <sound/core.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <sound/ac97_codec.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <media/videobuf2-v4l2.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <media/v4l2-device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <media/v4l2-ctrls.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #define STK1160_VERSION "0.9.5"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #define STK1160_VERSION_NUM 0x000905
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) /* Decide on number of packets for each buffer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #define STK1160_NUM_PACKETS 64
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) /* Number of buffers for isoc transfers */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) #define STK1160_NUM_BUFS 16
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) #define STK1160_MIN_BUFS 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) /* TODO: This endpoint address should be retrieved */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) #define STK1160_EP_VIDEO 0x82
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) #define STK1160_EP_AUDIO 0x81
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) /* Max and min video buffers */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) #define STK1160_MIN_VIDEO_BUFFERS 8
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) #define STK1160_MAX_VIDEO_BUFFERS 32
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) #define STK1160_MIN_PKT_SIZE 3072
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) #define STK1160_MAX_INPUT 4
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) #define STK1160_SVIDEO_INPUT 4
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) #define STK1160_AC97_TIMEOUT 50
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) #define STK1160_I2C_TIMEOUT 100
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) /* TODO: Print helpers
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) * I could use dev_xxx, pr_xxx, v4l2_xxx or printk.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) * However, there isn't a solid consensus on which
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) * new drivers should use.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) #ifdef DEBUG
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) #define stk1160_dbg(fmt, args...) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) printk(KERN_DEBUG "stk1160: " fmt, ## args)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) #define stk1160_dbg(fmt, args...)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) #define stk1160_info(fmt, args...) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) pr_info("stk1160: " fmt, ## args)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) #define stk1160_warn(fmt, args...) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) pr_warn("stk1160: " fmt, ## args)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) #define stk1160_err(fmt, args...) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) pr_err("stk1160: " fmt, ## args)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) /* Buffer for one video frame */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) struct stk1160_buffer {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) /* common v4l buffer stuff -- must be first */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) struct vb2_v4l2_buffer vb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) struct list_head list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) void *mem;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) unsigned int length; /* buffer length */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) unsigned int bytesused; /* bytes written */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) int odd; /* current oddity */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) * Since we interlace two fields per frame,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) * this is different from bytesused.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) unsigned int pos; /* current pos inside buffer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) struct stk1160_isoc_ctl {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) /* max packet size of isoc transaction */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) int max_pkt_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) /* number of allocated urbs */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) int num_bufs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) /* urb for isoc transfers */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) struct urb **urb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) /* transfer buffers for isoc transfer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) char **transfer_buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) /* current buffer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) struct stk1160_buffer *buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) struct stk1160_fmt {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) u32 fourcc; /* v4l2 format id */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) int depth;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) struct stk1160 {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) struct v4l2_device v4l2_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) struct video_device vdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) struct v4l2_ctrl_handler ctrl_handler;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) struct device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) struct usb_device *udev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) /* saa7115 subdev */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) struct v4l2_subdev *sd_saa7115;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) /* isoc control struct */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) struct list_head avail_bufs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) /* video capture */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) struct vb2_queue vb_vidq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) /* max packet size of isoc transaction */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) int max_pkt_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) /* array of wMaxPacketSize */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) unsigned int *alt_max_pkt_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) /* alternate */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) int alt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) /* Number of alternative settings */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) int num_alt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) struct stk1160_isoc_ctl isoc_ctl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) /* frame properties */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) int width; /* current frame width */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) int height; /* current frame height */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) unsigned int ctl_input; /* selected input */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) v4l2_std_id norm; /* current norm */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) struct stk1160_fmt *fmt; /* selected format */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) unsigned int sequence;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) /* i2c i/o */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) struct i2c_adapter i2c_adap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) struct i2c_client i2c_client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) struct mutex v4l_lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) struct mutex vb_queue_lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) spinlock_t buf_lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) struct file *fh_owner; /* filehandle ownership */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) /* EXPERIMENTAL */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) struct snd_card *snd_card;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) struct regval {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) u16 reg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) u16 val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) /* Provided by stk1160-v4l.c */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) int stk1160_vb2_setup(struct stk1160 *dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) int stk1160_video_register(struct stk1160 *dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) void stk1160_video_unregister(struct stk1160 *dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) void stk1160_clear_queue(struct stk1160 *dev, enum vb2_buffer_state vb2_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) /* Provided by stk1160-video.c */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) int stk1160_alloc_isoc(struct stk1160 *dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) void stk1160_free_isoc(struct stk1160 *dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) void stk1160_cancel_isoc(struct stk1160 *dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) void stk1160_uninit_isoc(struct stk1160 *dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) /* Provided by stk1160-i2c.c */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) int stk1160_i2c_register(struct stk1160 *dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) int stk1160_i2c_unregister(struct stk1160 *dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) /* Provided by stk1160-core.c */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) int stk1160_read_reg(struct stk1160 *dev, u16 reg, u8 *value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) int stk1160_write_reg(struct stk1160 *dev, u16 reg, u16 value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) int stk1160_write_regs_req(struct stk1160 *dev, u8 req, u16 reg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) char *buf, int len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) int stk1160_read_reg_req_len(struct stk1160 *dev, u8 req, u16 reg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) char *buf, int len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) void stk1160_select_input(struct stk1160 *dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) /* Provided by stk1160-ac97.c */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) void stk1160_ac97_setup(struct stk1160 *dev);