thp: clear_copy_huge_page
[linux/fpc-iii.git] / drivers / staging / solo6x10 / solo6010-i2c.c
blob60b69cd0d09d9005a5815c6b48306c020478419b
1 /*
2 * Copyright (C) 2010 Bluecherry, LLC www.bluecherrydvr.com
3 * Copyright (C) 2010 Ben Collins <bcollins@bluecherry.net>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 /* XXX: The SOLO6010 i2c does not have separate interrupts for each i2c
21 * channel. The bus can only handle one i2c event at a time. The below handles
22 * this all wrong. We should be using the status registers to see if the bus
23 * is in use, and have a global lock to check the status register. Also,
24 * the bulk of the work should be handled out-of-interrupt. The ugly loops
25 * that occur during interrupt scare me. The ISR should merely signal
26 * thread context, ACK the interrupt, and move on. -- BenC */
28 #include <linux/kernel.h>
30 #include "solo6010.h"
32 u8 solo_i2c_readbyte(struct solo6010_dev *solo_dev, int id, u8 addr, u8 off)
34 struct i2c_msg msgs[2];
35 u8 data;
37 msgs[0].flags = 0;
38 msgs[0].addr = addr;
39 msgs[0].len = 1;
40 msgs[0].buf = &off;
42 msgs[1].flags = I2C_M_RD;
43 msgs[1].addr = addr;
44 msgs[1].len = 1;
45 msgs[1].buf = &data;
47 i2c_transfer(&solo_dev->i2c_adap[id], msgs, 2);
49 return data;
52 void solo_i2c_writebyte(struct solo6010_dev *solo_dev, int id, u8 addr,
53 u8 off, u8 data)
55 struct i2c_msg msgs;
56 u8 buf[2];
58 buf[0] = off;
59 buf[1] = data;
60 msgs.flags = 0;
61 msgs.addr = addr;
62 msgs.len = 2;
63 msgs.buf = buf;
65 i2c_transfer(&solo_dev->i2c_adap[id], &msgs, 1);
68 static void solo_i2c_flush(struct solo6010_dev *solo_dev, int wr)
70 u32 ctrl;
72 ctrl = SOLO_IIC_CH_SET(solo_dev->i2c_id);
74 if (solo_dev->i2c_state == IIC_STATE_START)
75 ctrl |= SOLO_IIC_START;
77 if (wr) {
78 ctrl |= SOLO_IIC_WRITE;
79 } else {
80 ctrl |= SOLO_IIC_READ;
81 if (!(solo_dev->i2c_msg->flags & I2C_M_NO_RD_ACK))
82 ctrl |= SOLO_IIC_ACK_EN;
85 if (solo_dev->i2c_msg_ptr == solo_dev->i2c_msg->len)
86 ctrl |= SOLO_IIC_STOP;
88 solo_reg_write(solo_dev, SOLO_IIC_CTRL, ctrl);
91 static void solo_i2c_start(struct solo6010_dev *solo_dev)
93 u32 addr = solo_dev->i2c_msg->addr << 1;
95 if (solo_dev->i2c_msg->flags & I2C_M_RD)
96 addr |= 1;
98 solo_dev->i2c_state = IIC_STATE_START;
99 solo_reg_write(solo_dev, SOLO_IIC_TXD, addr);
100 solo_i2c_flush(solo_dev, 1);
103 static void solo_i2c_stop(struct solo6010_dev *solo_dev)
105 solo6010_irq_off(solo_dev, SOLO_IRQ_IIC);
106 solo_reg_write(solo_dev, SOLO_IIC_CTRL, 0);
107 solo_dev->i2c_state = IIC_STATE_STOP;
108 wake_up(&solo_dev->i2c_wait);
111 static int solo_i2c_handle_read(struct solo6010_dev *solo_dev)
113 prepare_read:
114 if (solo_dev->i2c_msg_ptr != solo_dev->i2c_msg->len) {
115 solo_i2c_flush(solo_dev, 0);
116 return 0;
119 solo_dev->i2c_msg_ptr = 0;
120 solo_dev->i2c_msg++;
121 solo_dev->i2c_msg_num--;
123 if (solo_dev->i2c_msg_num == 0) {
124 solo_i2c_stop(solo_dev);
125 return 0;
128 if (!(solo_dev->i2c_msg->flags & I2C_M_NOSTART)) {
129 solo_i2c_start(solo_dev);
130 } else {
131 if (solo_dev->i2c_msg->flags & I2C_M_RD)
132 goto prepare_read;
133 else
134 solo_i2c_stop(solo_dev);
137 return 0;
140 static int solo_i2c_handle_write(struct solo6010_dev *solo_dev)
142 retry_write:
143 if (solo_dev->i2c_msg_ptr != solo_dev->i2c_msg->len) {
144 solo_reg_write(solo_dev, SOLO_IIC_TXD,
145 solo_dev->i2c_msg->buf[solo_dev->i2c_msg_ptr]);
146 solo_dev->i2c_msg_ptr++;
147 solo_i2c_flush(solo_dev, 1);
148 return 0;
151 solo_dev->i2c_msg_ptr = 0;
152 solo_dev->i2c_msg++;
153 solo_dev->i2c_msg_num--;
155 if (solo_dev->i2c_msg_num == 0) {
156 solo_i2c_stop(solo_dev);
157 return 0;
160 if (!(solo_dev->i2c_msg->flags & I2C_M_NOSTART)) {
161 solo_i2c_start(solo_dev);
162 } else {
163 if (solo_dev->i2c_msg->flags & I2C_M_RD)
164 solo_i2c_stop(solo_dev);
165 else
166 goto retry_write;
169 return 0;
172 int solo_i2c_isr(struct solo6010_dev *solo_dev)
174 u32 status = solo_reg_read(solo_dev, SOLO_IIC_CTRL);
175 int ret = -EINVAL;
177 solo_reg_write(solo_dev, SOLO_IRQ_STAT, SOLO_IRQ_IIC);
179 if (status & (SOLO_IIC_STATE_TRNS & SOLO_IIC_STATE_SIG_ERR) ||
180 solo_dev->i2c_id < 0) {
181 solo_i2c_stop(solo_dev);
182 return -ENXIO;
185 switch (solo_dev->i2c_state) {
186 case IIC_STATE_START:
187 if (solo_dev->i2c_msg->flags & I2C_M_RD) {
188 solo_dev->i2c_state = IIC_STATE_READ;
189 ret = solo_i2c_handle_read(solo_dev);
190 break;
193 solo_dev->i2c_state = IIC_STATE_WRITE;
194 case IIC_STATE_WRITE:
195 ret = solo_i2c_handle_write(solo_dev);
196 break;
198 case IIC_STATE_READ:
199 solo_dev->i2c_msg->buf[solo_dev->i2c_msg_ptr] =
200 solo_reg_read(solo_dev, SOLO_IIC_RXD);
201 solo_dev->i2c_msg_ptr++;
203 ret = solo_i2c_handle_read(solo_dev);
204 break;
206 default:
207 solo_i2c_stop(solo_dev);
210 return ret;
213 static int solo_i2c_master_xfer(struct i2c_adapter *adap,
214 struct i2c_msg msgs[], int num)
216 struct solo6010_dev *solo_dev = adap->algo_data;
217 unsigned long timeout;
218 int ret;
219 int i;
220 DEFINE_WAIT(wait);
222 for (i = 0; i < SOLO_I2C_ADAPTERS; i++) {
223 if (&solo_dev->i2c_adap[i] == adap)
224 break;
227 if (i == SOLO_I2C_ADAPTERS)
228 return num; /* XXX Right return value for failure? */
230 mutex_lock(&solo_dev->i2c_mutex);
231 solo_dev->i2c_id = i;
232 solo_dev->i2c_msg = msgs;
233 solo_dev->i2c_msg_num = num;
234 solo_dev->i2c_msg_ptr = 0;
236 solo_reg_write(solo_dev, SOLO_IIC_CTRL, 0);
237 solo6010_irq_on(solo_dev, SOLO_IRQ_IIC);
238 solo_i2c_start(solo_dev);
240 timeout = HZ / 2;
242 for (;;) {
243 prepare_to_wait(&solo_dev->i2c_wait, &wait, TASK_INTERRUPTIBLE);
245 if (solo_dev->i2c_state == IIC_STATE_STOP)
246 break;
248 timeout = schedule_timeout(timeout);
249 if (!timeout)
250 break;
252 if (signal_pending(current))
253 break;
256 finish_wait(&solo_dev->i2c_wait, &wait);
257 ret = num - solo_dev->i2c_msg_num;
258 solo_dev->i2c_state = IIC_STATE_IDLE;
259 solo_dev->i2c_id = -1;
261 mutex_unlock(&solo_dev->i2c_mutex);
263 return ret;
266 static u32 solo_i2c_functionality(struct i2c_adapter *adap)
268 return I2C_FUNC_I2C;
271 static struct i2c_algorithm solo_i2c_algo = {
272 .master_xfer = solo_i2c_master_xfer,
273 .functionality = solo_i2c_functionality,
276 int solo_i2c_init(struct solo6010_dev *solo_dev)
278 int i;
279 int ret;
281 solo_reg_write(solo_dev, SOLO_IIC_CFG,
282 SOLO_IIC_PRESCALE(8) | SOLO_IIC_ENABLE);
284 solo_dev->i2c_id = -1;
285 solo_dev->i2c_state = IIC_STATE_IDLE;
286 init_waitqueue_head(&solo_dev->i2c_wait);
287 mutex_init(&solo_dev->i2c_mutex);
289 for (i = 0; i < SOLO_I2C_ADAPTERS; i++) {
290 struct i2c_adapter *adap = &solo_dev->i2c_adap[i];
292 snprintf(adap->name, I2C_NAME_SIZE, "%s I2C %d",
293 SOLO6010_NAME, i);
294 adap->algo = &solo_i2c_algo;
295 adap->algo_data = solo_dev;
296 adap->retries = 1;
297 adap->dev.parent = &solo_dev->pdev->dev;
299 ret = i2c_add_adapter(adap);
300 if (ret) {
301 adap->algo_data = NULL;
302 break;
306 if (ret) {
307 for (i = 0; i < SOLO_I2C_ADAPTERS; i++) {
308 if (!solo_dev->i2c_adap[i].algo_data)
309 break;
310 i2c_del_adapter(&solo_dev->i2c_adap[i]);
311 solo_dev->i2c_adap[i].algo_data = NULL;
313 return ret;
316 dev_info(&solo_dev->pdev->dev, "Enabled %d i2c adapters\n",
317 SOLO_I2C_ADAPTERS);
319 return 0;
322 void solo_i2c_exit(struct solo6010_dev *solo_dev)
324 int i;
326 for (i = 0; i < SOLO_I2C_ADAPTERS; i++) {
327 if (!solo_dev->i2c_adap[i].algo_data)
328 continue;
329 i2c_del_adapter(&solo_dev->i2c_adap[i]);
330 solo_dev->i2c_adap[i].algo_data = NULL;