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>
32 u8
solo_i2c_readbyte(struct solo6010_dev
*solo_dev
, int id
, u8 addr
, u8 off
)
34 struct i2c_msg msgs
[2];
42 msgs
[1].flags
= I2C_M_RD
;
47 i2c_transfer(&solo_dev
->i2c_adap
[id
], msgs
, 2);
52 void solo_i2c_writebyte(struct solo6010_dev
*solo_dev
, int id
, u8 addr
,
65 i2c_transfer(&solo_dev
->i2c_adap
[id
], &msgs
, 1);
68 static void solo_i2c_flush(struct solo6010_dev
*solo_dev
, int wr
)
72 ctrl
= SOLO_IIC_CH_SET(solo_dev
->i2c_id
);
74 if (solo_dev
->i2c_state
== IIC_STATE_START
)
75 ctrl
|= SOLO_IIC_START
;
78 ctrl
|= SOLO_IIC_WRITE
;
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
)
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
)
114 if (solo_dev
->i2c_msg_ptr
!= solo_dev
->i2c_msg
->len
) {
115 solo_i2c_flush(solo_dev
, 0);
119 solo_dev
->i2c_msg_ptr
= 0;
121 solo_dev
->i2c_msg_num
--;
123 if (solo_dev
->i2c_msg_num
== 0) {
124 solo_i2c_stop(solo_dev
);
128 if (!(solo_dev
->i2c_msg
->flags
& I2C_M_NOSTART
)) {
129 solo_i2c_start(solo_dev
);
131 if (solo_dev
->i2c_msg
->flags
& I2C_M_RD
)
134 solo_i2c_stop(solo_dev
);
140 static int solo_i2c_handle_write(struct solo6010_dev
*solo_dev
)
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);
151 solo_dev
->i2c_msg_ptr
= 0;
153 solo_dev
->i2c_msg_num
--;
155 if (solo_dev
->i2c_msg_num
== 0) {
156 solo_i2c_stop(solo_dev
);
160 if (!(solo_dev
->i2c_msg
->flags
& I2C_M_NOSTART
)) {
161 solo_i2c_start(solo_dev
);
163 if (solo_dev
->i2c_msg
->flags
& I2C_M_RD
)
164 solo_i2c_stop(solo_dev
);
172 int solo_i2c_isr(struct solo6010_dev
*solo_dev
)
174 u32 status
= solo_reg_read(solo_dev
, SOLO_IIC_CTRL
);
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
);
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
);
193 solo_dev
->i2c_state
= IIC_STATE_WRITE
;
194 case IIC_STATE_WRITE
:
195 ret
= solo_i2c_handle_write(solo_dev
);
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
);
207 solo_i2c_stop(solo_dev
);
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
;
222 for (i
= 0; i
< SOLO_I2C_ADAPTERS
; i
++) {
223 if (&solo_dev
->i2c_adap
[i
] == adap
)
227 if (i
== SOLO_I2C_ADAPTERS
)
228 return num
; // XXX Right return value for failure?
230 down(&solo_dev
->i2c_sem
);
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
);
243 prepare_to_wait(&solo_dev
->i2c_wait
, &wait
, TASK_INTERRUPTIBLE
);
245 if (solo_dev
->i2c_state
== IIC_STATE_STOP
)
248 timeout
= schedule_timeout(timeout
);
252 if (signal_pending(current
))
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 up(&solo_dev
->i2c_sem
);
266 static u32
solo_i2c_functionality(struct i2c_adapter
*adap
)
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
)
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 sema_init(&solo_dev
->i2c_sem
, 1);
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",
294 adap
->algo
= &solo_i2c_algo
;
295 adap
->algo_data
= solo_dev
;
297 adap
->dev
.parent
= &solo_dev
->pdev
->dev
;
299 if ((ret
= i2c_add_adapter(adap
))) {
300 adap
->algo_data
= NULL
;
306 for (i
= 0; i
< SOLO_I2C_ADAPTERS
; i
++) {
307 if (!solo_dev
->i2c_adap
[i
].algo_data
)
309 i2c_del_adapter(&solo_dev
->i2c_adap
[i
]);
310 solo_dev
->i2c_adap
[i
].algo_data
= NULL
;
315 dev_info(&solo_dev
->pdev
->dev
, "Enabled %d i2c adapters\n",
321 void solo_i2c_exit(struct solo6010_dev
*solo_dev
)
325 for (i
= 0; i
< SOLO_I2C_ADAPTERS
; i
++) {
326 if (!solo_dev
->i2c_adap
[i
].algo_data
)
328 i2c_del_adapter(&solo_dev
->i2c_adap
[i
]);
329 solo_dev
->i2c_adap
[i
].algo_data
= NULL
;