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 #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 struct mxl111sf_state {
53 	struct dvb_usb_device *d;
54 
55 	enum mxl111sf_gpio_port_expander gpio_port_expander;
56 	u8 port_expander_addr;
57 
58 	u8 chip_id;
59 	u8 chip_ver;
60 #define MXL111SF_V6     1
61 #define MXL111SF_V8_100 2
62 #define MXL111SF_V8_200 3
63 	u8 chip_rev;
64 
65 #ifdef USING_ENUM_mxl111sf_current_mode
66 	enum mxl111sf_current_mode current_mode;
67 #endif
68 
69 #define MXL_TUNER_MODE         0
70 #define MXL_SOC_MODE           1
71 #define MXL_DEV_MODE_MASK      0x01
72 #if 1
73 	int device_mode;
74 #endif
75 	/* use usb alt setting 1 for EP4 ISOC transfer (dvb-t),
76 				     EP5 BULK transfer (atsc-mh),
77 				     EP6 BULK transfer (atsc/qam),
78 	   use usb alt setting 2 for EP4 BULK transfer (dvb-t),
79 				     EP5 ISOC transfer (atsc-mh),
80 				     EP6 ISOC transfer (atsc/qam),
81 	 */
82 	int alt_mode;
83 	int gpio_mode;
84 	struct tveeprom tv;
85 
86 	struct mutex fe_lock;
87 	u8 num_frontends;
88 	struct mxl111sf_adap_state adap_state[3];
89 #ifdef CONFIG_MEDIA_CONTROLLER_DVB
90 	struct media_entity tuner;
91 	struct media_pad tuner_pads[2];
92 #endif
93 };
94 
95 int mxl111sf_read_reg(struct mxl111sf_state *state, u8 addr, u8 *data);
96 int mxl111sf_write_reg(struct mxl111sf_state *state, u8 addr, u8 data);
97 
98 struct mxl111sf_reg_ctrl_info {
99 	u8 addr;
100 	u8 mask;
101 	u8 data;
102 };
103 
104 int mxl111sf_write_reg_mask(struct mxl111sf_state *state,
105 			    u8 addr, u8 mask, u8 data);
106 int mxl111sf_ctrl_program_regs(struct mxl111sf_state *state,
107 			       struct mxl111sf_reg_ctrl_info *ctrl_reg_info);
108 
109 /* needed for hardware i2c functions in mxl111sf-i2c.c:
110  * mxl111sf_i2c_send_data / mxl111sf_i2c_get_data */
111 int mxl111sf_ctrl_msg(struct dvb_usb_device *d,
112 		      u8 cmd, u8 *wbuf, int wlen, u8 *rbuf, int rlen);
113 
114 #define mxl_printk(kern, fmt, arg...) \
115 	printk(kern "%s: " fmt "\n", __func__, ##arg)
116 
117 #define mxl_info(fmt, arg...) \
118 	mxl_printk(KERN_INFO, fmt, ##arg)
119 
120 extern int dvb_usb_mxl111sf_debug;
121 #define mxl_debug(fmt, arg...) \
122 	if (dvb_usb_mxl111sf_debug) \
123 		mxl_printk(KERN_DEBUG, fmt, ##arg)
124 
125 #define MXL_I2C_DBG 0x04
126 #define MXL_ADV_DBG 0x10
127 #define mxl_debug_adv(fmt, arg...) \
128 	if (dvb_usb_mxl111sf_debug & MXL_ADV_DBG) \
129 		mxl_printk(KERN_DEBUG, fmt, ##arg)
130 
131 #define mxl_i2c(fmt, arg...) \
132 	if (dvb_usb_mxl111sf_debug & MXL_I2C_DBG) \
133 		mxl_printk(KERN_DEBUG, fmt, ##arg)
134 
135 #define mxl_i2c_adv(fmt, arg...) \
136 	if ((dvb_usb_mxl111sf_debug & (MXL_I2C_DBG | MXL_ADV_DBG)) == \
137 		(MXL_I2C_DBG | MXL_ADV_DBG)) \
138 			mxl_printk(KERN_DEBUG, fmt, ##arg)
139 
140 /* The following allows the mxl_fail() macro defined below to work
141  * in externel modules, such as mxl111sf-tuner.ko, even though
142  * dvb_usb_mxl111sf_debug is not defined within those modules */
143 #if (defined(__MXL111SF_TUNER_H__)) || (defined(__MXL111SF_DEMOD_H__))
144 #define MXL_ADV_DEBUG_ENABLED MXL_ADV_DBG
145 #else
146 #define MXL_ADV_DEBUG_ENABLED dvb_usb_mxl111sf_debug
147 #endif
148 
149 #define mxl_fail(ret)							\
150 ({									\
151 	int __ret;							\
152 	__ret = (ret < 0);						\
153 	if ((__ret) && (MXL_ADV_DEBUG_ENABLED & MXL_ADV_DBG))		\
154 		mxl_printk(KERN_ERR, "error %d on line %d",		\
155 			   ret, __LINE__);				\
156 	__ret;								\
157 })
158 
159 #endif /* _DVB_USB_MXL111SF_H_ */
160