18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0-only */
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Copyright (C) 2010-2014 Michael Krufky (mkrufky@linuxtv.org)
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * see Documentation/driver-api/media/drivers/dvb-usb.rst for more information
68c2ecf20Sopenharmony_ci */
78c2ecf20Sopenharmony_ci
88c2ecf20Sopenharmony_ci#ifndef _DVB_USB_MXL111SF_H_
98c2ecf20Sopenharmony_ci#define _DVB_USB_MXL111SF_H_
108c2ecf20Sopenharmony_ci
118c2ecf20Sopenharmony_ci#ifdef DVB_USB_LOG_PREFIX
128c2ecf20Sopenharmony_ci#undef DVB_USB_LOG_PREFIX
138c2ecf20Sopenharmony_ci#endif
148c2ecf20Sopenharmony_ci#define DVB_USB_LOG_PREFIX "mxl111sf"
158c2ecf20Sopenharmony_ci#include "dvb_usb.h"
168c2ecf20Sopenharmony_ci#include <media/tveeprom.h>
178c2ecf20Sopenharmony_ci#include <media/media-entity.h>
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_ci/* Max transfer size done by I2C transfer functions */
208c2ecf20Sopenharmony_ci#define MXL_MAX_XFER_SIZE  64
218c2ecf20Sopenharmony_ci
228c2ecf20Sopenharmony_ci#define MXL_EP1_REG_READ     1
238c2ecf20Sopenharmony_ci#define MXL_EP2_REG_WRITE    2
248c2ecf20Sopenharmony_ci#define MXL_EP3_INTERRUPT    3
258c2ecf20Sopenharmony_ci#define MXL_EP4_MPEG2        4
268c2ecf20Sopenharmony_ci#define MXL_EP5_I2S          5
278c2ecf20Sopenharmony_ci#define MXL_EP6_656          6
288c2ecf20Sopenharmony_ci#define MXL_EP6_MPEG2        6
298c2ecf20Sopenharmony_ci
308c2ecf20Sopenharmony_ci#ifdef USING_ENUM_mxl111sf_current_mode
318c2ecf20Sopenharmony_cienum mxl111sf_current_mode {
328c2ecf20Sopenharmony_ci	mxl_mode_dvbt = MXL_EP4_MPEG2,
338c2ecf20Sopenharmony_ci	mxl_mode_mh   = MXL_EP5_I2S,
348c2ecf20Sopenharmony_ci	mxl_mode_atsc = MXL_EP6_MPEG2,
358c2ecf20Sopenharmony_ci};
368c2ecf20Sopenharmony_ci#endif
378c2ecf20Sopenharmony_ci
388c2ecf20Sopenharmony_cienum mxl111sf_gpio_port_expander {
398c2ecf20Sopenharmony_ci	mxl111sf_gpio_hw,
408c2ecf20Sopenharmony_ci	mxl111sf_PCA9534,
418c2ecf20Sopenharmony_ci};
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_cistruct mxl111sf_adap_state {
448c2ecf20Sopenharmony_ci	int alt_mode;
458c2ecf20Sopenharmony_ci	int gpio_mode;
468c2ecf20Sopenharmony_ci	int device_mode;
478c2ecf20Sopenharmony_ci	int ep6_clockphase;
488c2ecf20Sopenharmony_ci	int (*fe_init)(struct dvb_frontend *);
498c2ecf20Sopenharmony_ci	int (*fe_sleep)(struct dvb_frontend *);
508c2ecf20Sopenharmony_ci};
518c2ecf20Sopenharmony_ci
528c2ecf20Sopenharmony_cienum mxl111sf_pads {
538c2ecf20Sopenharmony_ci	MXL111SF_PAD_RF_INPUT,
548c2ecf20Sopenharmony_ci	MXL111SF_PAD_OUTPUT,
558c2ecf20Sopenharmony_ci	MXL111SF_NUM_PADS
568c2ecf20Sopenharmony_ci};
578c2ecf20Sopenharmony_ci
588c2ecf20Sopenharmony_cistruct mxl111sf_state {
598c2ecf20Sopenharmony_ci	struct dvb_usb_device *d;
608c2ecf20Sopenharmony_ci
618c2ecf20Sopenharmony_ci	enum mxl111sf_gpio_port_expander gpio_port_expander;
628c2ecf20Sopenharmony_ci	u8 port_expander_addr;
638c2ecf20Sopenharmony_ci
648c2ecf20Sopenharmony_ci	u8 chip_id;
658c2ecf20Sopenharmony_ci	u8 chip_ver;
668c2ecf20Sopenharmony_ci#define MXL111SF_V6     1
678c2ecf20Sopenharmony_ci#define MXL111SF_V8_100 2
688c2ecf20Sopenharmony_ci#define MXL111SF_V8_200 3
698c2ecf20Sopenharmony_ci	u8 chip_rev;
708c2ecf20Sopenharmony_ci
718c2ecf20Sopenharmony_ci#ifdef USING_ENUM_mxl111sf_current_mode
728c2ecf20Sopenharmony_ci	enum mxl111sf_current_mode current_mode;
738c2ecf20Sopenharmony_ci#endif
748c2ecf20Sopenharmony_ci
758c2ecf20Sopenharmony_ci#define MXL_TUNER_MODE         0
768c2ecf20Sopenharmony_ci#define MXL_SOC_MODE           1
778c2ecf20Sopenharmony_ci#define MXL_DEV_MODE_MASK      0x01
788c2ecf20Sopenharmony_ci#if 1
798c2ecf20Sopenharmony_ci	int device_mode;
808c2ecf20Sopenharmony_ci#endif
818c2ecf20Sopenharmony_ci	/* use usb alt setting 1 for EP4 ISOC transfer (dvb-t),
828c2ecf20Sopenharmony_ci				     EP5 BULK transfer (atsc-mh),
838c2ecf20Sopenharmony_ci				     EP6 BULK transfer (atsc/qam),
848c2ecf20Sopenharmony_ci	   use usb alt setting 2 for EP4 BULK transfer (dvb-t),
858c2ecf20Sopenharmony_ci				     EP5 ISOC transfer (atsc-mh),
868c2ecf20Sopenharmony_ci				     EP6 ISOC transfer (atsc/qam),
878c2ecf20Sopenharmony_ci	 */
888c2ecf20Sopenharmony_ci	int alt_mode;
898c2ecf20Sopenharmony_ci	int gpio_mode;
908c2ecf20Sopenharmony_ci	struct tveeprom tv;
918c2ecf20Sopenharmony_ci
928c2ecf20Sopenharmony_ci	struct mutex fe_lock;
938c2ecf20Sopenharmony_ci	u8 num_frontends;
948c2ecf20Sopenharmony_ci	struct mxl111sf_adap_state adap_state[3];
958c2ecf20Sopenharmony_ci	u8 sndbuf[MXL_MAX_XFER_SIZE];
968c2ecf20Sopenharmony_ci	u8 rcvbuf[MXL_MAX_XFER_SIZE];
978c2ecf20Sopenharmony_ci	struct mutex msg_lock;
988c2ecf20Sopenharmony_ci#ifdef CONFIG_MEDIA_CONTROLLER_DVB
998c2ecf20Sopenharmony_ci	struct media_entity tuner;
1008c2ecf20Sopenharmony_ci	struct media_pad tuner_pads[MXL111SF_NUM_PADS];
1018c2ecf20Sopenharmony_ci#endif
1028c2ecf20Sopenharmony_ci};
1038c2ecf20Sopenharmony_ci
1048c2ecf20Sopenharmony_ciint mxl111sf_read_reg(struct mxl111sf_state *state, u8 addr, u8 *data);
1058c2ecf20Sopenharmony_ciint mxl111sf_write_reg(struct mxl111sf_state *state, u8 addr, u8 data);
1068c2ecf20Sopenharmony_ci
1078c2ecf20Sopenharmony_cistruct mxl111sf_reg_ctrl_info {
1088c2ecf20Sopenharmony_ci	u8 addr;
1098c2ecf20Sopenharmony_ci	u8 mask;
1108c2ecf20Sopenharmony_ci	u8 data;
1118c2ecf20Sopenharmony_ci};
1128c2ecf20Sopenharmony_ci
1138c2ecf20Sopenharmony_ciint mxl111sf_write_reg_mask(struct mxl111sf_state *state,
1148c2ecf20Sopenharmony_ci			    u8 addr, u8 mask, u8 data);
1158c2ecf20Sopenharmony_ciint mxl111sf_ctrl_program_regs(struct mxl111sf_state *state,
1168c2ecf20Sopenharmony_ci			       struct mxl111sf_reg_ctrl_info *ctrl_reg_info);
1178c2ecf20Sopenharmony_ci
1188c2ecf20Sopenharmony_ci/* needed for hardware i2c functions in mxl111sf-i2c.c:
1198c2ecf20Sopenharmony_ci * mxl111sf_i2c_send_data / mxl111sf_i2c_get_data */
1208c2ecf20Sopenharmony_ciint mxl111sf_ctrl_msg(struct mxl111sf_state *state,
1218c2ecf20Sopenharmony_ci		      u8 cmd, u8 *wbuf, int wlen, u8 *rbuf, int rlen);
1228c2ecf20Sopenharmony_ci
1238c2ecf20Sopenharmony_ci#define mxl_printk(kern, fmt, arg...) \
1248c2ecf20Sopenharmony_ci	printk(kern "%s: " fmt "\n", __func__, ##arg)
1258c2ecf20Sopenharmony_ci
1268c2ecf20Sopenharmony_ci#define mxl_info(fmt, arg...) \
1278c2ecf20Sopenharmony_ci	mxl_printk(KERN_INFO, fmt, ##arg)
1288c2ecf20Sopenharmony_ci
1298c2ecf20Sopenharmony_ciextern int dvb_usb_mxl111sf_debug;
1308c2ecf20Sopenharmony_ci#define mxl_debug(fmt, arg...) \
1318c2ecf20Sopenharmony_ci	if (dvb_usb_mxl111sf_debug) \
1328c2ecf20Sopenharmony_ci		mxl_printk(KERN_DEBUG, fmt, ##arg)
1338c2ecf20Sopenharmony_ci
1348c2ecf20Sopenharmony_ci#define MXL_I2C_DBG 0x04
1358c2ecf20Sopenharmony_ci#define MXL_ADV_DBG 0x10
1368c2ecf20Sopenharmony_ci#define mxl_debug_adv(fmt, arg...) \
1378c2ecf20Sopenharmony_ci	if (dvb_usb_mxl111sf_debug & MXL_ADV_DBG) \
1388c2ecf20Sopenharmony_ci		mxl_printk(KERN_DEBUG, fmt, ##arg)
1398c2ecf20Sopenharmony_ci
1408c2ecf20Sopenharmony_ci#define mxl_i2c(fmt, arg...) \
1418c2ecf20Sopenharmony_ci	if (dvb_usb_mxl111sf_debug & MXL_I2C_DBG) \
1428c2ecf20Sopenharmony_ci		mxl_printk(KERN_DEBUG, fmt, ##arg)
1438c2ecf20Sopenharmony_ci
1448c2ecf20Sopenharmony_ci#define mxl_i2c_adv(fmt, arg...) \
1458c2ecf20Sopenharmony_ci	if ((dvb_usb_mxl111sf_debug & (MXL_I2C_DBG | MXL_ADV_DBG)) == \
1468c2ecf20Sopenharmony_ci		(MXL_I2C_DBG | MXL_ADV_DBG)) \
1478c2ecf20Sopenharmony_ci			mxl_printk(KERN_DEBUG, fmt, ##arg)
1488c2ecf20Sopenharmony_ci
1498c2ecf20Sopenharmony_ci/* The following allows the mxl_fail() macro defined below to work
1508c2ecf20Sopenharmony_ci * in externel modules, such as mxl111sf-tuner.ko, even though
1518c2ecf20Sopenharmony_ci * dvb_usb_mxl111sf_debug is not defined within those modules */
1528c2ecf20Sopenharmony_ci#if (defined(__MXL111SF_TUNER_H__)) || (defined(__MXL111SF_DEMOD_H__))
1538c2ecf20Sopenharmony_ci#define MXL_ADV_DEBUG_ENABLED MXL_ADV_DBG
1548c2ecf20Sopenharmony_ci#else
1558c2ecf20Sopenharmony_ci#define MXL_ADV_DEBUG_ENABLED dvb_usb_mxl111sf_debug
1568c2ecf20Sopenharmony_ci#endif
1578c2ecf20Sopenharmony_ci
1588c2ecf20Sopenharmony_ci#define mxl_fail(ret)							\
1598c2ecf20Sopenharmony_ci({									\
1608c2ecf20Sopenharmony_ci	int __ret;							\
1618c2ecf20Sopenharmony_ci	__ret = (ret < 0);						\
1628c2ecf20Sopenharmony_ci	if ((__ret) && (MXL_ADV_DEBUG_ENABLED & MXL_ADV_DBG))		\
1638c2ecf20Sopenharmony_ci		mxl_printk(KERN_ERR, "error %d on line %d",		\
1648c2ecf20Sopenharmony_ci			   ret, __LINE__);				\
1658c2ecf20Sopenharmony_ci	__ret;								\
1668c2ecf20Sopenharmony_ci})
1678c2ecf20Sopenharmony_ci
1688c2ecf20Sopenharmony_ci#endif /* _DVB_USB_MXL111SF_H_ */
169