xref: /linux/drivers/media/usb/dvb-usb-v2/mxl111sf.h (revision 02680c23d7b3febe45ea3d4f9818c2b2dc89020a)
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3  * Copyright (C) 2010-2014 Michael Krufky (mkrufky@linuxtv.org)
4  *
5  * see Documentation/driver-api/media/drivers/dvb-usb.rst for more information
6  */
7 
8 #ifndef _DVB_USB_MXL111SF_H_
9 #define _DVB_USB_MXL111SF_H_
10 
11 #ifdef DVB_USB_LOG_PREFIX
12 #undef DVB_USB_LOG_PREFIX
13 #endif
14 #define DVB_USB_LOG_PREFIX "mxl111sf"
15 #include "dvb_usb.h"
16 #include <media/tveeprom.h>
17 #include <media/media-entity.h>
18 
19 /* Max transfer size done by I2C transfer functions */
20 #define MXL_MAX_XFER_SIZE  64
21 
22 #define MXL_EP1_REG_READ     1
23 #define MXL_EP2_REG_WRITE    2
24 #define MXL_EP3_INTERRUPT    3
25 #define MXL_EP4_MPEG2        4
26 #define MXL_EP5_I2S          5
27 #define MXL_EP6_656          6
28 #define MXL_EP6_MPEG2        6
29 
30 #ifdef USING_ENUM_mxl111sf_current_mode
31 enum mxl111sf_current_mode {
32 	mxl_mode_dvbt = MXL_EP4_MPEG2,
33 	mxl_mode_mh   = MXL_EP5_I2S,
34 	mxl_mode_atsc = MXL_EP6_MPEG2,
35 };
36 #endif
37 
38 enum mxl111sf_gpio_port_expander {
39 	mxl111sf_gpio_hw,
40 	mxl111sf_PCA9534,
41 };
42 
43 struct mxl111sf_adap_state {
44 	int alt_mode;
45 	int gpio_mode;
46 	int device_mode;
47 	int ep6_clockphase;
48 	int (*fe_init)(struct dvb_frontend *);
49 	int (*fe_sleep)(struct dvb_frontend *);
50 };
51 
52 enum mxl111sf_pads {
53 	MXL111SF_PAD_RF_INPUT,
54 	MXL111SF_PAD_OUTPUT,
55 	MXL111SF_NUM_PADS
56 };
57 
58 struct mxl111sf_state {
59 	struct dvb_usb_device *d;
60 
61 	enum mxl111sf_gpio_port_expander gpio_port_expander;
62 	u8 port_expander_addr;
63 
64 	u8 chip_id;
65 	u8 chip_ver;
66 #define MXL111SF_V6     1
67 #define MXL111SF_V8_100 2
68 #define MXL111SF_V8_200 3
69 	u8 chip_rev;
70 
71 #ifdef USING_ENUM_mxl111sf_current_mode
72 	enum mxl111sf_current_mode current_mode;
73 #endif
74 
75 #define MXL_TUNER_MODE         0
76 #define MXL_SOC_MODE           1
77 #define MXL_DEV_MODE_MASK      0x01
78 #if 1
79 	int device_mode;
80 #endif
81 	/* use usb alt setting 1 for EP4 ISOC transfer (dvb-t),
82 				     EP5 BULK transfer (atsc-mh),
83 				     EP6 BULK transfer (atsc/qam),
84 	   use usb alt setting 2 for EP4 BULK transfer (dvb-t),
85 				     EP5 ISOC transfer (atsc-mh),
86 				     EP6 ISOC transfer (atsc/qam),
87 	 */
88 	int alt_mode;
89 	int gpio_mode;
90 	struct tveeprom tv;
91 
92 	struct mutex fe_lock;
93 	u8 num_frontends;
94 	struct mxl111sf_adap_state adap_state[3];
95 	u8 sndbuf[MXL_MAX_XFER_SIZE];
96 	u8 rcvbuf[MXL_MAX_XFER_SIZE];
97 	struct mutex msg_lock;
98 #ifdef CONFIG_MEDIA_CONTROLLER_DVB
99 	struct media_entity tuner;
100 	struct media_pad tuner_pads[MXL111SF_NUM_PADS];
101 #endif
102 };
103 
104 int mxl111sf_read_reg(struct mxl111sf_state *state, u8 addr, u8 *data);
105 int mxl111sf_write_reg(struct mxl111sf_state *state, u8 addr, u8 data);
106 
107 struct mxl111sf_reg_ctrl_info {
108 	u8 addr;
109 	u8 mask;
110 	u8 data;
111 };
112 
113 int mxl111sf_write_reg_mask(struct mxl111sf_state *state,
114 			    u8 addr, u8 mask, u8 data);
115 int mxl111sf_ctrl_program_regs(struct mxl111sf_state *state,
116 			       struct mxl111sf_reg_ctrl_info *ctrl_reg_info);
117 
118 /* needed for hardware i2c functions in mxl111sf-i2c.c:
119  * mxl111sf_i2c_send_data / mxl111sf_i2c_get_data */
120 int mxl111sf_ctrl_msg(struct mxl111sf_state *state,
121 		      u8 cmd, u8 *wbuf, int wlen, u8 *rbuf, int rlen);
122 
123 #define mxl_printk(kern, fmt, arg...) \
124 	printk(kern "%s: " fmt "\n", __func__, ##arg)
125 
126 #define mxl_info(fmt, arg...) \
127 	mxl_printk(KERN_INFO, fmt, ##arg)
128 
129 extern int dvb_usb_mxl111sf_debug;
130 #define mxl_debug(fmt, arg...) \
131 	if (dvb_usb_mxl111sf_debug) \
132 		mxl_printk(KERN_DEBUG, fmt, ##arg)
133 
134 #define MXL_I2C_DBG 0x04
135 #define MXL_ADV_DBG 0x10
136 #define mxl_debug_adv(fmt, arg...) \
137 	if (dvb_usb_mxl111sf_debug & MXL_ADV_DBG) \
138 		mxl_printk(KERN_DEBUG, fmt, ##arg)
139 
140 #define mxl_i2c(fmt, arg...) \
141 	if (dvb_usb_mxl111sf_debug & MXL_I2C_DBG) \
142 		mxl_printk(KERN_DEBUG, fmt, ##arg)
143 
144 #define mxl_i2c_adv(fmt, arg...) \
145 	if ((dvb_usb_mxl111sf_debug & (MXL_I2C_DBG | MXL_ADV_DBG)) == \
146 		(MXL_I2C_DBG | MXL_ADV_DBG)) \
147 			mxl_printk(KERN_DEBUG, fmt, ##arg)
148 
149 /* The following allows the mxl_fail() macro defined below to work
150  * in externel modules, such as mxl111sf-tuner.ko, even though
151  * dvb_usb_mxl111sf_debug is not defined within those modules */
152 #if (defined(__MXL111SF_TUNER_H__)) || (defined(__MXL111SF_DEMOD_H__))
153 #define MXL_ADV_DEBUG_ENABLED MXL_ADV_DBG
154 #else
155 #define MXL_ADV_DEBUG_ENABLED dvb_usb_mxl111sf_debug
156 #endif
157 
158 #define mxl_fail(ret)							\
159 ({									\
160 	int __ret;							\
161 	__ret = (ret < 0);						\
162 	if ((__ret) && (MXL_ADV_DEBUG_ENABLED & MXL_ADV_DBG))		\
163 		mxl_printk(KERN_ERR, "error %d on line %d",		\
164 			   ret, __LINE__);				\
165 	__ret;								\
166 })
167 
168 #endif /* _DVB_USB_MXL111SF_H_ */
169