staging: delete non-required instances of include <linux/init.h>
[deliverable/linux.git] / drivers / staging / media / go7007 / go7007-i2c.c
CommitLineData
866b8695
GKH
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
866b8695 18#include <linux/module.h>
866b8695
GKH
19#include <linux/delay.h>
20#include <linux/sched.h>
21#include <linux/list.h>
22#include <linux/unistd.h>
23#include <linux/time.h>
24#include <linux/device.h>
25#include <linux/i2c.h>
fd9a40da 26#include <linux/mutex.h>
866b8695 27#include <linux/uaccess.h>
866b8695
GKH
28
29#include "go7007-priv.h"
866b8695 30
866b8695
GKH
31/********************* Driver for on-board I2C adapter *********************/
32
33/* #define GO7007_I2C_DEBUG */
34
35#define SPI_I2C_ADDR_BASE 0x1400
36#define STATUS_REG_ADDR (SPI_I2C_ADDR_BASE + 0x2)
37#define I2C_CTRL_REG_ADDR (SPI_I2C_ADDR_BASE + 0x6)
38#define I2C_DEV_UP_ADDR_REG_ADDR (SPI_I2C_ADDR_BASE + 0x7)
39#define I2C_LO_ADDR_REG_ADDR (SPI_I2C_ADDR_BASE + 0x8)
40#define I2C_DATA_REG_ADDR (SPI_I2C_ADDR_BASE + 0x9)
41#define I2C_CLKFREQ_REG_ADDR (SPI_I2C_ADDR_BASE + 0xa)
42
43#define I2C_STATE_MASK 0x0007
44#define I2C_READ_READY_MASK 0x0008
45
46/* There is only one I2C port on the TW2804 that feeds all four GO7007 VIPs
47 * on the Adlink PCI-MPG24, so access is shared between all of them. */
fd9a40da 48static DEFINE_MUTEX(adlink_mpg24_i2c_lock);
866b8695
GKH
49
50static int go7007_i2c_xfer(struct go7007 *go, u16 addr, int read,
51 u16 command, int flags, u8 *data)
52{
ffb97493 53 int i, ret = -EIO;
866b8695
GKH
54 u16 val;
55
56 if (go->status == STATUS_SHUTDOWN)
ffb97493 57 return -ENODEV;
866b8695
GKH
58
59#ifdef GO7007_I2C_DEBUG
60 if (read)
afc2e8a0 61 dev_dbg(go->dev, "go7007-i2c: reading 0x%02x on 0x%02x\n",
866b8695
GKH
62 command, addr);
63 else
afc2e8a0 64 dev_dbg(go->dev,
866b8695
GKH
65 "go7007-i2c: writing 0x%02x to 0x%02x on 0x%02x\n",
66 *data, command, addr);
67#endif
68
fd9a40da 69 mutex_lock(&go->hw_lock);
866b8695
GKH
70
71 if (go->board_id == GO7007_BOARDID_ADLINK_MPG24) {
72 /* Bridge the I2C port on this GO7007 to the shared bus */
fd9a40da 73 mutex_lock(&adlink_mpg24_i2c_lock);
866b8695
GKH
74 go7007_write_addr(go, 0x3c82, 0x0020);
75 }
76
77 /* Wait for I2C adapter to be ready */
78 for (i = 0; i < 10; ++i) {
79 if (go7007_read_addr(go, STATUS_REG_ADDR, &val) < 0)
80 goto i2c_done;
81 if (!(val & I2C_STATE_MASK))
82 break;
83 msleep(100);
84 }
85 if (i == 10) {
afc2e8a0 86 dev_err(go->dev, "go7007-i2c: I2C adapter is hung\n");
866b8695
GKH
87 goto i2c_done;
88 }
89
90 /* Set target register (command) */
91 go7007_write_addr(go, I2C_CTRL_REG_ADDR, flags);
92 go7007_write_addr(go, I2C_LO_ADDR_REG_ADDR, command);
93
94 /* If we're writing, send the data and target address and we're done */
95 if (!read) {
96 go7007_write_addr(go, I2C_DATA_REG_ADDR, *data);
97 go7007_write_addr(go, I2C_DEV_UP_ADDR_REG_ADDR,
98 (addr << 9) | (command >> 8));
99 ret = 0;
100 goto i2c_done;
101 }
102
103 /* Otherwise, we're reading. First clear i2c_rx_data_rdy. */
104 if (go7007_read_addr(go, I2C_DATA_REG_ADDR, &val) < 0)
105 goto i2c_done;
106
107 /* Send the target address plus read flag */
108 go7007_write_addr(go, I2C_DEV_UP_ADDR_REG_ADDR,
109 (addr << 9) | 0x0100 | (command >> 8));
110
111 /* Wait for i2c_rx_data_rdy */
112 for (i = 0; i < 10; ++i) {
113 if (go7007_read_addr(go, STATUS_REG_ADDR, &val) < 0)
114 goto i2c_done;
115 if (val & I2C_READ_READY_MASK)
116 break;
117 msleep(100);
118 }
119 if (i == 10) {
afc2e8a0 120 dev_err(go->dev, "go7007-i2c: I2C adapter is hung\n");
866b8695
GKH
121 goto i2c_done;
122 }
123
124 /* Retrieve the read byte */
125 if (go7007_read_addr(go, I2C_DATA_REG_ADDR, &val) < 0)
126 goto i2c_done;
127 *data = val;
128 ret = 0;
129
130i2c_done:
131 if (go->board_id == GO7007_BOARDID_ADLINK_MPG24) {
132 /* Isolate the I2C port on this GO7007 from the shared bus */
133 go7007_write_addr(go, 0x3c82, 0x0000);
fd9a40da 134 mutex_unlock(&adlink_mpg24_i2c_lock);
866b8695 135 }
fd9a40da 136 mutex_unlock(&go->hw_lock);
866b8695
GKH
137 return ret;
138}
139
140static int go7007_smbus_xfer(struct i2c_adapter *adapter, u16 addr,
141 unsigned short flags, char read_write,
142 u8 command, int size, union i2c_smbus_data *data)
143{
144 struct go7007 *go = i2c_get_adapdata(adapter);
145
146 if (size != I2C_SMBUS_BYTE_DATA)
ffb97493 147 return -EIO;
866b8695
GKH
148 return go7007_i2c_xfer(go, addr, read_write == I2C_SMBUS_READ, command,
149 flags & I2C_CLIENT_SCCB ? 0x10 : 0x00, &data->byte);
150}
151
152/* VERY LIMITED I2C master xfer function -- only needed because the
153 * SMBus functions only support 8-bit commands and the SAA7135 uses
154 * 16-bit commands. The I2C interface on the GO7007, as limited as
155 * it is, does support this mode. */
156
157static int go7007_i2c_master_xfer(struct i2c_adapter *adapter,
158 struct i2c_msg msgs[], int num)
159{
160 struct go7007 *go = i2c_get_adapdata(adapter);
161 int i;
162
163 for (i = 0; i < num; ++i) {
164 /* We can only do two things here -- write three bytes, or
165 * write two bytes and read one byte. */
166 if (msgs[i].len == 2) {
167 if (i + 1 == num || msgs[i].addr != msgs[i + 1].addr ||
168 (msgs[i].flags & I2C_M_RD) ||
169 !(msgs[i + 1].flags & I2C_M_RD) ||
170 msgs[i + 1].len != 1)
ffb97493 171 return -EIO;
866b8695
GKH
172 if (go7007_i2c_xfer(go, msgs[i].addr, 1,
173 (msgs[i].buf[0] << 8) | msgs[i].buf[1],
174 0x01, &msgs[i + 1].buf[0]) < 0)
ffb97493 175 return -EIO;
866b8695
GKH
176 ++i;
177 } else if (msgs[i].len == 3) {
178 if (msgs[i].flags & I2C_M_RD)
ffb97493 179 return -EIO;
866b8695 180 if (msgs[i].len != 3)
ffb97493 181 return -EIO;
866b8695
GKH
182 if (go7007_i2c_xfer(go, msgs[i].addr, 0,
183 (msgs[i].buf[0] << 8) | msgs[i].buf[1],
184 0x01, &msgs[i].buf[2]) < 0)
ffb97493 185 return -EIO;
866b8695 186 } else
ffb97493 187 return -EIO;
866b8695
GKH
188 }
189
ffb97493 190 return num;
866b8695
GKH
191}
192
193static u32 go7007_functionality(struct i2c_adapter *adapter)
194{
195 return I2C_FUNC_SMBUS_BYTE_DATA;
196}
197
198static struct i2c_algorithm go7007_algo = {
199 .smbus_xfer = go7007_smbus_xfer,
200 .master_xfer = go7007_i2c_master_xfer,
201 .functionality = go7007_functionality,
202};
203
204static struct i2c_adapter go7007_adap_templ = {
205 .owner = THIS_MODULE,
866b8695 206 .name = "WIS GO7007SB",
866b8695
GKH
207 .algo = &go7007_algo,
208};
209
210int go7007_i2c_init(struct go7007 *go)
211{
212 memcpy(&go->i2c_adapter, &go7007_adap_templ,
213 sizeof(go7007_adap_templ));
214 go->i2c_adapter.dev.parent = go->dev;
215 i2c_set_adapdata(&go->i2c_adapter, go);
216 if (i2c_add_adapter(&go->i2c_adapter) < 0) {
afc2e8a0 217 dev_err(go->dev,
866b8695
GKH
218 "go7007-i2c: error: i2c_add_adapter failed\n");
219 return -1;
220 }
221 return 0;
222}
This page took 0.521798 seconds and 5 git commands to generate.