• Home
  • History
  • Annotate
  • Line#
  • Navigate
  • Raw
  • Download
  • only in /netgear-R7000-V1.0.7.12_1.2.5/components/opensource/linux/linux-2.6.36/drivers/staging/go7007/
1/*
2 * Copyright (C) 2005-2006 Micronas USA Inc.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License (Version 2) as
6 * published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software Foundation,
15 * Inc., 59 Temple Place - Suite 330, Boston MA 02111-1307, USA.
16 */
17
18#include <linux/module.h>
19#include <linux/init.h>
20#include <linux/delay.h>
21#include <linux/sched.h>
22#include <linux/list.h>
23#include <linux/unistd.h>
24#include <linux/time.h>
25#include <linux/device.h>
26#include <linux/i2c.h>
27#include <linux/mutex.h>
28#include <linux/uaccess.h>
29#include <asm/system.h>
30
31#include "go7007-priv.h"
32#include "wis-i2c.h"
33
34/********************* Driver for on-board I2C adapter *********************/
35
36/* #define GO7007_I2C_DEBUG */
37
38#define SPI_I2C_ADDR_BASE		0x1400
39#define STATUS_REG_ADDR			(SPI_I2C_ADDR_BASE + 0x2)
40#define I2C_CTRL_REG_ADDR		(SPI_I2C_ADDR_BASE + 0x6)
41#define I2C_DEV_UP_ADDR_REG_ADDR	(SPI_I2C_ADDR_BASE + 0x7)
42#define I2C_LO_ADDR_REG_ADDR		(SPI_I2C_ADDR_BASE + 0x8)
43#define I2C_DATA_REG_ADDR		(SPI_I2C_ADDR_BASE + 0x9)
44#define I2C_CLKFREQ_REG_ADDR		(SPI_I2C_ADDR_BASE + 0xa)
45
46#define I2C_STATE_MASK			0x0007
47#define I2C_READ_READY_MASK		0x0008
48
49/* There is only one I2C port on the TW2804 that feeds all four GO7007 VIPs
50 * on the Adlink PCI-MPG24, so access is shared between all of them. */
51static DEFINE_MUTEX(adlink_mpg24_i2c_lock);
52
53static int go7007_i2c_xfer(struct go7007 *go, u16 addr, int read,
54		u16 command, int flags, u8 *data)
55{
56	int i, ret = -1;
57	u16 val;
58
59	if (go->status == STATUS_SHUTDOWN)
60		return -1;
61
62#ifdef GO7007_I2C_DEBUG
63	if (read)
64		printk(KERN_DEBUG "go7007-i2c: reading 0x%02x on 0x%02x\n",
65			command, addr);
66	else
67		printk(KERN_DEBUG
68			"go7007-i2c: writing 0x%02x to 0x%02x on 0x%02x\n",
69			*data, command, addr);
70#endif
71
72	mutex_lock(&go->hw_lock);
73
74	if (go->board_id == GO7007_BOARDID_ADLINK_MPG24) {
75		/* Bridge the I2C port on this GO7007 to the shared bus */
76		mutex_lock(&adlink_mpg24_i2c_lock);
77		go7007_write_addr(go, 0x3c82, 0x0020);
78	}
79
80	/* Wait for I2C adapter to be ready */
81	for (i = 0; i < 10; ++i) {
82		if (go7007_read_addr(go, STATUS_REG_ADDR, &val) < 0)
83			goto i2c_done;
84		if (!(val & I2C_STATE_MASK))
85			break;
86		msleep(100);
87	}
88	if (i == 10) {
89		printk(KERN_ERR "go7007-i2c: I2C adapter is hung\n");
90		goto i2c_done;
91	}
92
93	/* Set target register (command) */
94	go7007_write_addr(go, I2C_CTRL_REG_ADDR, flags);
95	go7007_write_addr(go, I2C_LO_ADDR_REG_ADDR, command);
96
97	/* If we're writing, send the data and target address and we're done */
98	if (!read) {
99		go7007_write_addr(go, I2C_DATA_REG_ADDR, *data);
100		go7007_write_addr(go, I2C_DEV_UP_ADDR_REG_ADDR,
101					(addr << 9) | (command >> 8));
102		ret = 0;
103		goto i2c_done;
104	}
105
106	/* Otherwise, we're reading.  First clear i2c_rx_data_rdy. */
107	if (go7007_read_addr(go, I2C_DATA_REG_ADDR, &val) < 0)
108		goto i2c_done;
109
110	/* Send the target address plus read flag */
111	go7007_write_addr(go, I2C_DEV_UP_ADDR_REG_ADDR,
112			(addr << 9) | 0x0100 | (command >> 8));
113
114	/* Wait for i2c_rx_data_rdy */
115	for (i = 0; i < 10; ++i) {
116		if (go7007_read_addr(go, STATUS_REG_ADDR, &val) < 0)
117			goto i2c_done;
118		if (val & I2C_READ_READY_MASK)
119			break;
120		msleep(100);
121	}
122	if (i == 10) {
123		printk(KERN_ERR "go7007-i2c: I2C adapter is hung\n");
124		goto i2c_done;
125	}
126
127	/* Retrieve the read byte */
128	if (go7007_read_addr(go, I2C_DATA_REG_ADDR, &val) < 0)
129		goto i2c_done;
130	*data = val;
131	ret = 0;
132
133i2c_done:
134	if (go->board_id == GO7007_BOARDID_ADLINK_MPG24) {
135		/* Isolate the I2C port on this GO7007 from the shared bus */
136		go7007_write_addr(go, 0x3c82, 0x0000);
137		mutex_unlock(&adlink_mpg24_i2c_lock);
138	}
139	mutex_unlock(&go->hw_lock);
140	return ret;
141}
142
143static int go7007_smbus_xfer(struct i2c_adapter *adapter, u16 addr,
144		unsigned short flags, char read_write,
145		u8 command, int size, union i2c_smbus_data *data)
146{
147	struct go7007 *go = i2c_get_adapdata(adapter);
148
149	if (size != I2C_SMBUS_BYTE_DATA)
150		return -1;
151	return go7007_i2c_xfer(go, addr, read_write == I2C_SMBUS_READ, command,
152			flags & I2C_CLIENT_SCCB ? 0x10 : 0x00, &data->byte);
153}
154
155/* VERY LIMITED I2C master xfer function -- only needed because the
156 * SMBus functions only support 8-bit commands and the SAA7135 uses
157 * 16-bit commands.  The I2C interface on the GO7007, as limited as
158 * it is, does support this mode. */
159
160static int go7007_i2c_master_xfer(struct i2c_adapter *adapter,
161					struct i2c_msg msgs[], int num)
162{
163	struct go7007 *go = i2c_get_adapdata(adapter);
164	int i;
165
166	for (i = 0; i < num; ++i) {
167		/* We can only do two things here -- write three bytes, or
168		 * write two bytes and read one byte. */
169		if (msgs[i].len == 2) {
170			if (i + 1 == num || msgs[i].addr != msgs[i + 1].addr ||
171					(msgs[i].flags & I2C_M_RD) ||
172					!(msgs[i + 1].flags & I2C_M_RD) ||
173					msgs[i + 1].len != 1)
174				return -1;
175			if (go7007_i2c_xfer(go, msgs[i].addr, 1,
176					(msgs[i].buf[0] << 8) | msgs[i].buf[1],
177					0x01, &msgs[i + 1].buf[0]) < 0)
178				return -1;
179			++i;
180		} else if (msgs[i].len == 3) {
181			if (msgs[i].flags & I2C_M_RD)
182				return -1;
183			if (msgs[i].len != 3)
184				return -1;
185			if (go7007_i2c_xfer(go, msgs[i].addr, 0,
186					(msgs[i].buf[0] << 8) | msgs[i].buf[1],
187					0x01, &msgs[i].buf[2]) < 0)
188				return -1;
189		} else
190			return -1;
191	}
192
193	return 0;
194}
195
196static u32 go7007_functionality(struct i2c_adapter *adapter)
197{
198	return I2C_FUNC_SMBUS_BYTE_DATA;
199}
200
201static struct i2c_algorithm go7007_algo = {
202	.smbus_xfer	= go7007_smbus_xfer,
203	.master_xfer	= go7007_i2c_master_xfer,
204	.functionality	= go7007_functionality,
205};
206
207static struct i2c_adapter go7007_adap_templ = {
208	.owner			= THIS_MODULE,
209	.name			= "WIS GO7007SB",
210	.algo			= &go7007_algo,
211};
212
213int go7007_i2c_init(struct go7007 *go)
214{
215	memcpy(&go->i2c_adapter, &go7007_adap_templ,
216			sizeof(go7007_adap_templ));
217	go->i2c_adapter.dev.parent = go->dev;
218	i2c_set_adapdata(&go->i2c_adapter, go);
219	if (i2c_add_adapter(&go->i2c_adapter) < 0) {
220		printk(KERN_ERR
221			"go7007-i2c: error: i2c_add_adapter failed\n");
222		return -1;
223	}
224	return 0;
225}
226