blob: 477fe0aade866bea99040fc8ca8b134883fc2919 [file] [log] [blame]
Devin Heitmuellercb1c0f82010-03-13 17:10:24 -03001/*
2 * ngene-i2c.c: nGene PCIe bridge driver i2c functions
3 *
4 * Copyright (C) 2005-2007 Micronas
5 *
6 * Copyright (C) 2008-2009 Ralph Metzler <rjkm@metzlerbros.de>
7 * Modifications for new nGene firmware,
8 * support for EEPROM-copying,
9 * support for new dual DVB-S2 card prototype
10 *
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * version 2 only, as published by the Free Software Foundation.
15 *
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
26 * 02110-1301, USA
27 * Or, point your browser to http://www.gnu.org/copyleft/gpl.html
28 */
29
30/* FIXME - some of these can probably be removed */
31#include <linux/module.h>
32#include <linux/init.h>
33#include <linux/delay.h>
34#include <linux/slab.h>
35#include <linux/poll.h>
36#include <linux/io.h>
37#include <asm/div64.h>
38#include <linux/pci.h>
39#include <linux/pci_ids.h>
40#include <linux/smp_lock.h>
41#include <linux/timer.h>
Devin Heitmuellercb1c0f82010-03-13 17:10:24 -030042#include <linux/byteorder/generic.h>
43#include <linux/firmware.h>
44#include <linux/vmalloc.h>
45
46#include "ngene.h"
47
48/* Firmware command for i2c operations */
49static int ngene_command_i2c_read(struct ngene *dev, u8 adr,
50 u8 *out, u8 outlen, u8 *in, u8 inlen, int flag)
51{
52 struct ngene_command com;
53
54 com.cmd.hdr.Opcode = CMD_I2C_READ;
55 com.cmd.hdr.Length = outlen + 3;
56 com.cmd.I2CRead.Device = adr << 1;
57 memcpy(com.cmd.I2CRead.Data, out, outlen);
58 com.cmd.I2CRead.Data[outlen] = inlen;
59 com.cmd.I2CRead.Data[outlen + 1] = 0;
60 com.in_len = outlen + 3;
61 com.out_len = inlen + 1;
62
63 if (ngene_command(dev, &com) < 0)
64 return -EIO;
65
66 if ((com.cmd.raw8[0] >> 1) != adr)
67 return -EIO;
68
69 if (flag)
70 memcpy(in, com.cmd.raw8, inlen + 1);
71 else
72 memcpy(in, com.cmd.raw8 + 1, inlen);
73 return 0;
74}
75
76static int ngene_command_i2c_write(struct ngene *dev, u8 adr,
77 u8 *out, u8 outlen)
78{
79 struct ngene_command com;
80
81
82 com.cmd.hdr.Opcode = CMD_I2C_WRITE;
83 com.cmd.hdr.Length = outlen + 1;
84 com.cmd.I2CRead.Device = adr << 1;
85 memcpy(com.cmd.I2CRead.Data, out, outlen);
86 com.in_len = outlen + 1;
87 com.out_len = 1;
88
89 if (ngene_command(dev, &com) < 0)
90 return -EIO;
91
92 if (com.cmd.raw8[0] == 1)
93 return -EIO;
94
95 return 0;
96}
97
98static void ngene_i2c_set_bus(struct ngene *dev, int bus)
99{
100 if (!(dev->card_info->i2c_access & 2))
101 return;
102 if (dev->i2c_current_bus == bus)
103 return;
104
105 switch (bus) {
106 case 0:
107 ngene_command_gpio_set(dev, 3, 0);
108 ngene_command_gpio_set(dev, 2, 1);
109 break;
110
111 case 1:
112 ngene_command_gpio_set(dev, 2, 0);
113 ngene_command_gpio_set(dev, 3, 1);
114 break;
115 }
116 dev->i2c_current_bus = bus;
117}
118
119static int ngene_i2c_master_xfer(struct i2c_adapter *adapter,
120 struct i2c_msg msg[], int num)
121{
122 struct ngene_channel *chan =
123 (struct ngene_channel *)i2c_get_adapdata(adapter);
124 struct ngene *dev = chan->dev;
125
126 down(&dev->i2c_switch_mutex);
127 ngene_i2c_set_bus(dev, chan->number);
128
129 if (num == 2 && msg[1].flags & I2C_M_RD && !(msg[0].flags & I2C_M_RD))
130 if (!ngene_command_i2c_read(dev, msg[0].addr,
131 msg[0].buf, msg[0].len,
132 msg[1].buf, msg[1].len, 0))
133 goto done;
134
135 if (num == 1 && !(msg[0].flags & I2C_M_RD))
136 if (!ngene_command_i2c_write(dev, msg[0].addr,
137 msg[0].buf, msg[0].len))
138 goto done;
139 if (num == 1 && (msg[0].flags & I2C_M_RD))
140 if (!ngene_command_i2c_read(dev, msg[0].addr, NULL, 0,
141 msg[0].buf, msg[0].len, 0))
142 goto done;
143
144 up(&dev->i2c_switch_mutex);
145 return -EIO;
146
147done:
148 up(&dev->i2c_switch_mutex);
149 return num;
150}
151
152
153static u32 ngene_i2c_functionality(struct i2c_adapter *adap)
154{
155 return I2C_FUNC_SMBUS_EMUL;
156}
157
158static struct i2c_algorithm ngene_i2c_algo = {
159 .master_xfer = ngene_i2c_master_xfer,
160 .functionality = ngene_i2c_functionality,
161};
162
163int ngene_i2c_init(struct ngene *dev, int dev_nr)
164{
165 struct i2c_adapter *adap = &(dev->channel[dev_nr].i2c_adapter);
166
167 i2c_set_adapdata(adap, &(dev->channel[dev_nr]));
168 adap->class = I2C_CLASS_TV_DIGITAL | I2C_CLASS_TV_ANALOG;
169
170 strcpy(adap->name, "nGene");
171
172 adap->algo = &ngene_i2c_algo;
173 adap->algo_data = (void *)&(dev->channel[dev_nr]);
174 adap->dev.parent = &dev->pci_dev->dev;
175
176 return i2c_add_adapter(adap);
177}
178