Path: blob/master/drivers/media/common/tuners/tuner-i2c.h
15112 views
/*1tuner-i2c.h - i2c interface for different tuners23Copyright (C) 2007 Michael Krufky ([email protected])45This program is free software; you can redistribute it and/or modify6it under the terms of the GNU General Public License as published by7the Free Software Foundation; either version 2 of the License, or8(at your option) any later version.910This program is distributed in the hope that it will be useful,11but WITHOUT ANY WARRANTY; without even the implied warranty of12MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the13GNU General Public License for more details.1415You should have received a copy of the GNU General Public License16along with this program; if not, write to the Free Software17Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.18*/1920#ifndef __TUNER_I2C_H__21#define __TUNER_I2C_H__2223#include <linux/i2c.h>24#include <linux/slab.h>2526struct tuner_i2c_props {27u8 addr;28struct i2c_adapter *adap;2930/* used for tuner instance management */31int count;32char *name;33};3435static inline int tuner_i2c_xfer_send(struct tuner_i2c_props *props, char *buf, int len)36{37struct i2c_msg msg = { .addr = props->addr, .flags = 0,38.buf = buf, .len = len };39int ret = i2c_transfer(props->adap, &msg, 1);4041return (ret == 1) ? len : ret;42}4344static inline int tuner_i2c_xfer_recv(struct tuner_i2c_props *props, char *buf, int len)45{46struct i2c_msg msg = { .addr = props->addr, .flags = I2C_M_RD,47.buf = buf, .len = len };48int ret = i2c_transfer(props->adap, &msg, 1);4950return (ret == 1) ? len : ret;51}5253static inline int tuner_i2c_xfer_send_recv(struct tuner_i2c_props *props,54char *obuf, int olen,55char *ibuf, int ilen)56{57struct i2c_msg msg[2] = { { .addr = props->addr, .flags = 0,58.buf = obuf, .len = olen },59{ .addr = props->addr, .flags = I2C_M_RD,60.buf = ibuf, .len = ilen } };61int ret = i2c_transfer(props->adap, msg, 2);6263return (ret == 2) ? ilen : ret;64}6566/* Callers must declare as a global for the module:67*68* static LIST_HEAD(hybrid_tuner_instance_list);69*70* hybrid_tuner_instance_list should be the third argument71* passed into hybrid_tuner_request_state().72*73* state structure must contain the following:74*75* struct list_head hybrid_tuner_instance_list;76* struct tuner_i2c_props i2c_props;77*78* hybrid_tuner_instance_list (both within state structure and globally)79* is only required if the driver is using hybrid_tuner_request_state80* and hybrid_tuner_release_state to manage state sharing between81* multiple instances of hybrid tuners.82*/8384#define tuner_printk(kernlvl, i2cprops, fmt, arg...) do { \85printk(kernlvl "%s %d-%04x: " fmt, i2cprops.name, \86i2cprops.adap ? \87i2c_adapter_id(i2cprops.adap) : -1, \88i2cprops.addr, ##arg); \89} while (0)9091/* TO DO: convert all callers of these macros to pass in92* struct tuner_i2c_props, then remove the macro wrappers */9394#define __tuner_warn(i2cprops, fmt, arg...) do { \95tuner_printk(KERN_WARNING, i2cprops, fmt, ##arg); \96} while (0)9798#define __tuner_info(i2cprops, fmt, arg...) do { \99tuner_printk(KERN_INFO, i2cprops, fmt, ##arg); \100} while (0)101102#define __tuner_err(i2cprops, fmt, arg...) do { \103tuner_printk(KERN_ERR, i2cprops, fmt, ##arg); \104} while (0)105106#define __tuner_dbg(i2cprops, fmt, arg...) do { \107if ((debug)) \108tuner_printk(KERN_DEBUG, i2cprops, fmt, ##arg); \109} while (0)110111#define tuner_warn(fmt, arg...) __tuner_warn(priv->i2c_props, fmt, ##arg)112#define tuner_info(fmt, arg...) __tuner_info(priv->i2c_props, fmt, ##arg)113#define tuner_err(fmt, arg...) __tuner_err(priv->i2c_props, fmt, ##arg)114#define tuner_dbg(fmt, arg...) __tuner_dbg(priv->i2c_props, fmt, ##arg)115116/****************************************************************************/117118/* The return value of hybrid_tuner_request_state indicates the number of119* instances using this tuner object.120*121* 0 - no instances, indicates an error - kzalloc must have failed122*123* 1 - one instance, indicates that the tuner object was created successfully124*125* 2 (or more) instances, indicates that an existing tuner object was found126*/127128#define hybrid_tuner_request_state(type, state, list, i2cadap, i2caddr, devname)\129({ \130int __ret = 0; \131list_for_each_entry(state, &list, hybrid_tuner_instance_list) { \132if (((i2cadap) && (state->i2c_props.adap)) && \133((i2c_adapter_id(state->i2c_props.adap) == \134i2c_adapter_id(i2cadap)) && \135(i2caddr == state->i2c_props.addr))) { \136__tuner_info(state->i2c_props, \137"attaching existing instance\n"); \138state->i2c_props.count++; \139__ret = state->i2c_props.count; \140break; \141} \142} \143if (0 == __ret) { \144state = kzalloc(sizeof(type), GFP_KERNEL); \145if (NULL == state) \146goto __fail; \147state->i2c_props.addr = i2caddr; \148state->i2c_props.adap = i2cadap; \149state->i2c_props.name = devname; \150__tuner_info(state->i2c_props, \151"creating new instance\n"); \152list_add_tail(&state->hybrid_tuner_instance_list, &list);\153state->i2c_props.count++; \154__ret = state->i2c_props.count; \155} \156__fail: \157__ret; \158})159160#define hybrid_tuner_release_state(state) \161({ \162int __ret; \163state->i2c_props.count--; \164__ret = state->i2c_props.count; \165if (!state->i2c_props.count) { \166__tuner_info(state->i2c_props, "destroying instance\n");\167list_del(&state->hybrid_tuner_instance_list); \168kfree(state); \169} \170__ret; \171})172173#define hybrid_tuner_report_instance_count(state) \174({ \175int __ret = 0; \176if (state) \177__ret = state->i2c_props.count; \178__ret; \179})180181#endif /* __TUNER_I2C_H__ */182183184