Path: blob/master/drivers/media/dvb/ngene/ngene-i2c.c
15111 views
/*1* ngene-i2c.c: nGene PCIe bridge driver i2c functions2*3* Copyright (C) 2005-2007 Micronas4*5* Copyright (C) 2008-2009 Ralph Metzler <[email protected]>6* Modifications for new nGene firmware,7* support for EEPROM-copying,8* support for new dual DVB-S2 card prototype9*10*11* This program is free software; you can redistribute it and/or12* modify it under the terms of the GNU General Public License13* version 2 only, as published by the Free Software Foundation.14*15*16* This program is distributed in the hope that it will be useful,17* but WITHOUT ANY WARRANTY; without even the implied warranty of18* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the19* GNU General Public License for more details.20*21*22* You should have received a copy of the GNU General Public License23* along with this program; if not, write to the Free Software24* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA25* 02110-1301, USA26* Or, point your browser to http://www.gnu.org/copyleft/gpl.html27*/2829/* FIXME - some of these can probably be removed */30#include <linux/module.h>31#include <linux/init.h>32#include <linux/delay.h>33#include <linux/slab.h>34#include <linux/poll.h>35#include <linux/io.h>36#include <asm/div64.h>37#include <linux/pci.h>38#include <linux/pci_ids.h>39#include <linux/timer.h>40#include <linux/byteorder/generic.h>41#include <linux/firmware.h>42#include <linux/vmalloc.h>4344#include "ngene.h"4546/* Firmware command for i2c operations */47static int ngene_command_i2c_read(struct ngene *dev, u8 adr,48u8 *out, u8 outlen, u8 *in, u8 inlen, int flag)49{50struct ngene_command com;5152com.cmd.hdr.Opcode = CMD_I2C_READ;53com.cmd.hdr.Length = outlen + 3;54com.cmd.I2CRead.Device = adr << 1;55memcpy(com.cmd.I2CRead.Data, out, outlen);56com.cmd.I2CRead.Data[outlen] = inlen;57com.cmd.I2CRead.Data[outlen + 1] = 0;58com.in_len = outlen + 3;59com.out_len = inlen + 1;6061if (ngene_command(dev, &com) < 0)62return -EIO;6364if ((com.cmd.raw8[0] >> 1) != adr)65return -EIO;6667if (flag)68memcpy(in, com.cmd.raw8, inlen + 1);69else70memcpy(in, com.cmd.raw8 + 1, inlen);71return 0;72}7374static int ngene_command_i2c_write(struct ngene *dev, u8 adr,75u8 *out, u8 outlen)76{77struct ngene_command com;787980com.cmd.hdr.Opcode = CMD_I2C_WRITE;81com.cmd.hdr.Length = outlen + 1;82com.cmd.I2CRead.Device = adr << 1;83memcpy(com.cmd.I2CRead.Data, out, outlen);84com.in_len = outlen + 1;85com.out_len = 1;8687if (ngene_command(dev, &com) < 0)88return -EIO;8990if (com.cmd.raw8[0] == 1)91return -EIO;9293return 0;94}9596static void ngene_i2c_set_bus(struct ngene *dev, int bus)97{98if (!(dev->card_info->i2c_access & 2))99return;100if (dev->i2c_current_bus == bus)101return;102103switch (bus) {104case 0:105ngene_command_gpio_set(dev, 3, 0);106ngene_command_gpio_set(dev, 2, 1);107break;108109case 1:110ngene_command_gpio_set(dev, 2, 0);111ngene_command_gpio_set(dev, 3, 1);112break;113}114dev->i2c_current_bus = bus;115}116117static int ngene_i2c_master_xfer(struct i2c_adapter *adapter,118struct i2c_msg msg[], int num)119{120struct ngene_channel *chan =121(struct ngene_channel *)i2c_get_adapdata(adapter);122struct ngene *dev = chan->dev;123124down(&dev->i2c_switch_mutex);125ngene_i2c_set_bus(dev, chan->number);126127if (num == 2 && msg[1].flags & I2C_M_RD && !(msg[0].flags & I2C_M_RD))128if (!ngene_command_i2c_read(dev, msg[0].addr,129msg[0].buf, msg[0].len,130msg[1].buf, msg[1].len, 0))131goto done;132133if (num == 1 && !(msg[0].flags & I2C_M_RD))134if (!ngene_command_i2c_write(dev, msg[0].addr,135msg[0].buf, msg[0].len))136goto done;137if (num == 1 && (msg[0].flags & I2C_M_RD))138if (!ngene_command_i2c_read(dev, msg[0].addr, NULL, 0,139msg[0].buf, msg[0].len, 0))140goto done;141142up(&dev->i2c_switch_mutex);143return -EIO;144145done:146up(&dev->i2c_switch_mutex);147return num;148}149150151static u32 ngene_i2c_functionality(struct i2c_adapter *adap)152{153return I2C_FUNC_SMBUS_EMUL;154}155156static struct i2c_algorithm ngene_i2c_algo = {157.master_xfer = ngene_i2c_master_xfer,158.functionality = ngene_i2c_functionality,159};160161int ngene_i2c_init(struct ngene *dev, int dev_nr)162{163struct i2c_adapter *adap = &(dev->channel[dev_nr].i2c_adapter);164165i2c_set_adapdata(adap, &(dev->channel[dev_nr]));166167strcpy(adap->name, "nGene");168169adap->algo = &ngene_i2c_algo;170adap->algo_data = (void *)&(dev->channel[dev_nr]);171adap->dev.parent = &dev->pci_dev->dev;172173return i2c_add_adapter(adap);174}175176177178