xref: /OK3568_Linux_fs/kernel/drivers/media/tuners/tuner-i2c.h (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /* SPDX-License-Identifier: GPL-2.0-or-later */
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun     tuner-i2c.h - i2c interface for different tuners
4*4882a593Smuzhiyun 
5*4882a593Smuzhiyun     Copyright (C) 2007 Michael Krufky (mkrufky@linuxtv.org)
6*4882a593Smuzhiyun 
7*4882a593Smuzhiyun */
8*4882a593Smuzhiyun 
9*4882a593Smuzhiyun #ifndef __TUNER_I2C_H__
10*4882a593Smuzhiyun #define __TUNER_I2C_H__
11*4882a593Smuzhiyun 
12*4882a593Smuzhiyun #include <linux/i2c.h>
13*4882a593Smuzhiyun #include <linux/slab.h>
14*4882a593Smuzhiyun 
15*4882a593Smuzhiyun struct tuner_i2c_props {
16*4882a593Smuzhiyun 	u8 addr;
17*4882a593Smuzhiyun 	struct i2c_adapter *adap;
18*4882a593Smuzhiyun 
19*4882a593Smuzhiyun 	/* used for tuner instance management */
20*4882a593Smuzhiyun 	int count;
21*4882a593Smuzhiyun 	char *name;
22*4882a593Smuzhiyun };
23*4882a593Smuzhiyun 
tuner_i2c_xfer_send(struct tuner_i2c_props * props,unsigned char * buf,int len)24*4882a593Smuzhiyun static inline int tuner_i2c_xfer_send(struct tuner_i2c_props *props,
25*4882a593Smuzhiyun 				      unsigned char *buf, int len)
26*4882a593Smuzhiyun {
27*4882a593Smuzhiyun 	struct i2c_msg msg = { .addr = props->addr, .flags = 0,
28*4882a593Smuzhiyun 			       .buf = buf, .len = len };
29*4882a593Smuzhiyun 	int ret = i2c_transfer(props->adap, &msg, 1);
30*4882a593Smuzhiyun 
31*4882a593Smuzhiyun 	return (ret == 1) ? len : ret;
32*4882a593Smuzhiyun }
33*4882a593Smuzhiyun 
tuner_i2c_xfer_recv(struct tuner_i2c_props * props,unsigned char * buf,int len)34*4882a593Smuzhiyun static inline int tuner_i2c_xfer_recv(struct tuner_i2c_props *props,
35*4882a593Smuzhiyun 				      unsigned char *buf, int len)
36*4882a593Smuzhiyun {
37*4882a593Smuzhiyun 	struct i2c_msg msg = { .addr = props->addr, .flags = I2C_M_RD,
38*4882a593Smuzhiyun 			       .buf = buf, .len = len };
39*4882a593Smuzhiyun 	int ret = i2c_transfer(props->adap, &msg, 1);
40*4882a593Smuzhiyun 
41*4882a593Smuzhiyun 	return (ret == 1) ? len : ret;
42*4882a593Smuzhiyun }
43*4882a593Smuzhiyun 
tuner_i2c_xfer_send_recv(struct tuner_i2c_props * props,unsigned char * obuf,int olen,unsigned char * ibuf,int ilen)44*4882a593Smuzhiyun static inline int tuner_i2c_xfer_send_recv(struct tuner_i2c_props *props,
45*4882a593Smuzhiyun 					   unsigned char *obuf, int olen,
46*4882a593Smuzhiyun 					   unsigned char *ibuf, int ilen)
47*4882a593Smuzhiyun {
48*4882a593Smuzhiyun 	struct i2c_msg msg[2] = { { .addr = props->addr, .flags = 0,
49*4882a593Smuzhiyun 				    .buf = obuf, .len = olen },
50*4882a593Smuzhiyun 				  { .addr = props->addr, .flags = I2C_M_RD,
51*4882a593Smuzhiyun 				    .buf = ibuf, .len = ilen } };
52*4882a593Smuzhiyun 	int ret = i2c_transfer(props->adap, msg, 2);
53*4882a593Smuzhiyun 
54*4882a593Smuzhiyun 	return (ret == 2) ? ilen : ret;
55*4882a593Smuzhiyun }
56*4882a593Smuzhiyun 
57*4882a593Smuzhiyun /* Callers must declare as a global for the module:
58*4882a593Smuzhiyun  *
59*4882a593Smuzhiyun  * static LIST_HEAD(hybrid_tuner_instance_list);
60*4882a593Smuzhiyun  *
61*4882a593Smuzhiyun  * hybrid_tuner_instance_list should be the third argument
62*4882a593Smuzhiyun  * passed into hybrid_tuner_request_state().
63*4882a593Smuzhiyun  *
64*4882a593Smuzhiyun  * state structure must contain the following:
65*4882a593Smuzhiyun  *
66*4882a593Smuzhiyun  *	struct list_head	hybrid_tuner_instance_list;
67*4882a593Smuzhiyun  *	struct tuner_i2c_props	i2c_props;
68*4882a593Smuzhiyun  *
69*4882a593Smuzhiyun  * hybrid_tuner_instance_list (both within state structure and globally)
70*4882a593Smuzhiyun  * is only required if the driver is using hybrid_tuner_request_state
71*4882a593Smuzhiyun  * and hybrid_tuner_release_state to manage state sharing between
72*4882a593Smuzhiyun  * multiple instances of hybrid tuners.
73*4882a593Smuzhiyun  */
74*4882a593Smuzhiyun 
75*4882a593Smuzhiyun #define tuner_printk(kernlvl, i2cprops, fmt, arg...) do {		\
76*4882a593Smuzhiyun 	printk(kernlvl "%s %d-%04x: " fmt, i2cprops.name,		\
77*4882a593Smuzhiyun 			i2cprops.adap ?					\
78*4882a593Smuzhiyun 				i2c_adapter_id(i2cprops.adap) : -1,	\
79*4882a593Smuzhiyun 			i2cprops.addr, ##arg);				\
80*4882a593Smuzhiyun 	 } while (0)
81*4882a593Smuzhiyun 
82*4882a593Smuzhiyun /* TO DO: convert all callers of these macros to pass in
83*4882a593Smuzhiyun  * struct tuner_i2c_props, then remove the macro wrappers */
84*4882a593Smuzhiyun 
85*4882a593Smuzhiyun #define __tuner_warn(i2cprops, fmt, arg...) do {			\
86*4882a593Smuzhiyun 	tuner_printk(KERN_WARNING, i2cprops, fmt, ##arg);		\
87*4882a593Smuzhiyun 	} while (0)
88*4882a593Smuzhiyun 
89*4882a593Smuzhiyun #define __tuner_info(i2cprops, fmt, arg...) do {			\
90*4882a593Smuzhiyun 	tuner_printk(KERN_INFO, i2cprops, fmt, ##arg);			\
91*4882a593Smuzhiyun 	} while (0)
92*4882a593Smuzhiyun 
93*4882a593Smuzhiyun #define __tuner_err(i2cprops, fmt, arg...) do {				\
94*4882a593Smuzhiyun 	tuner_printk(KERN_ERR, i2cprops, fmt, ##arg);			\
95*4882a593Smuzhiyun 	} while (0)
96*4882a593Smuzhiyun 
97*4882a593Smuzhiyun #define __tuner_dbg(i2cprops, fmt, arg...) do {				\
98*4882a593Smuzhiyun 	if ((debug))							\
99*4882a593Smuzhiyun 		tuner_printk(KERN_DEBUG, i2cprops, fmt, ##arg);		\
100*4882a593Smuzhiyun 	} while (0)
101*4882a593Smuzhiyun 
102*4882a593Smuzhiyun #define tuner_warn(fmt, arg...) __tuner_warn(priv->i2c_props, fmt, ##arg)
103*4882a593Smuzhiyun #define tuner_info(fmt, arg...) __tuner_info(priv->i2c_props, fmt, ##arg)
104*4882a593Smuzhiyun #define tuner_err(fmt, arg...) __tuner_err(priv->i2c_props, fmt, ##arg)
105*4882a593Smuzhiyun #define tuner_dbg(fmt, arg...) __tuner_dbg(priv->i2c_props, fmt, ##arg)
106*4882a593Smuzhiyun 
107*4882a593Smuzhiyun /****************************************************************************/
108*4882a593Smuzhiyun 
109*4882a593Smuzhiyun /* The return value of hybrid_tuner_request_state indicates the number of
110*4882a593Smuzhiyun  * instances using this tuner object.
111*4882a593Smuzhiyun  *
112*4882a593Smuzhiyun  * 0 - no instances, indicates an error - kzalloc must have failed
113*4882a593Smuzhiyun  *
114*4882a593Smuzhiyun  * 1 - one instance, indicates that the tuner object was created successfully
115*4882a593Smuzhiyun  *
116*4882a593Smuzhiyun  * 2 (or more) instances, indicates that an existing tuner object was found
117*4882a593Smuzhiyun  */
118*4882a593Smuzhiyun 
119*4882a593Smuzhiyun #define hybrid_tuner_request_state(type, state, list, i2cadap, i2caddr, devname)\
120*4882a593Smuzhiyun ({									\
121*4882a593Smuzhiyun 	int __ret = 0;							\
122*4882a593Smuzhiyun 	list_for_each_entry(state, &list, hybrid_tuner_instance_list) {	\
123*4882a593Smuzhiyun 		if (((i2cadap) && (state->i2c_props.adap)) &&		\
124*4882a593Smuzhiyun 		    ((i2c_adapter_id(state->i2c_props.adap) ==		\
125*4882a593Smuzhiyun 		      i2c_adapter_id(i2cadap)) &&			\
126*4882a593Smuzhiyun 		     (i2caddr == state->i2c_props.addr))) {		\
127*4882a593Smuzhiyun 			__tuner_info(state->i2c_props,			\
128*4882a593Smuzhiyun 				     "attaching existing instance\n");	\
129*4882a593Smuzhiyun 			state->i2c_props.count++;			\
130*4882a593Smuzhiyun 			__ret = state->i2c_props.count;			\
131*4882a593Smuzhiyun 			break;						\
132*4882a593Smuzhiyun 		}							\
133*4882a593Smuzhiyun 	}								\
134*4882a593Smuzhiyun 	if (0 == __ret) {						\
135*4882a593Smuzhiyun 		state = kzalloc(sizeof(type), GFP_KERNEL);		\
136*4882a593Smuzhiyun 		if (NULL == state)					\
137*4882a593Smuzhiyun 			goto __fail;					\
138*4882a593Smuzhiyun 		state->i2c_props.addr = i2caddr;			\
139*4882a593Smuzhiyun 		state->i2c_props.adap = i2cadap;			\
140*4882a593Smuzhiyun 		state->i2c_props.name = devname;			\
141*4882a593Smuzhiyun 		__tuner_info(state->i2c_props,				\
142*4882a593Smuzhiyun 			     "creating new instance\n");		\
143*4882a593Smuzhiyun 		list_add_tail(&state->hybrid_tuner_instance_list, &list);\
144*4882a593Smuzhiyun 		state->i2c_props.count++;				\
145*4882a593Smuzhiyun 		__ret = state->i2c_props.count;				\
146*4882a593Smuzhiyun 	}								\
147*4882a593Smuzhiyun __fail:									\
148*4882a593Smuzhiyun 	__ret;								\
149*4882a593Smuzhiyun })
150*4882a593Smuzhiyun 
151*4882a593Smuzhiyun #define hybrid_tuner_release_state(state)				\
152*4882a593Smuzhiyun ({									\
153*4882a593Smuzhiyun 	int __ret;							\
154*4882a593Smuzhiyun 	state->i2c_props.count--;					\
155*4882a593Smuzhiyun 	__ret = state->i2c_props.count;					\
156*4882a593Smuzhiyun 	if (!state->i2c_props.count) {					\
157*4882a593Smuzhiyun 		__tuner_info(state->i2c_props, "destroying instance\n");\
158*4882a593Smuzhiyun 		list_del(&state->hybrid_tuner_instance_list);		\
159*4882a593Smuzhiyun 		kfree(state);						\
160*4882a593Smuzhiyun 	}								\
161*4882a593Smuzhiyun 	__ret;								\
162*4882a593Smuzhiyun })
163*4882a593Smuzhiyun 
164*4882a593Smuzhiyun #define hybrid_tuner_report_instance_count(state)			\
165*4882a593Smuzhiyun ({									\
166*4882a593Smuzhiyun 	int __ret = 0;							\
167*4882a593Smuzhiyun 	if (state)							\
168*4882a593Smuzhiyun 		__ret = state->i2c_props.count;				\
169*4882a593Smuzhiyun 	__ret;								\
170*4882a593Smuzhiyun })
171*4882a593Smuzhiyun 
172*4882a593Smuzhiyun #endif /* __TUNER_I2C_H__ */
173