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