2 * Copyright (C) 2010-2013 Bluecherry, LLC <http://www.bluecherrydvr.com>
5 * Ben Collins <bcollins@ubuntu.com>
8 * John Brooks <john.brooks@bluecherry.net>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
21 /* XXX: The SOLO6x10 i2c does not have separate interrupts for each i2c
22 * channel. The bus can only handle one i2c event at a time. The below handles
23 * this all wrong. We should be using the status registers to see if the bus
24 * is in use, and have a global lock to check the status register. Also,
25 * the bulk of the work should be handled out-of-interrupt. The ugly loops
26 * that occur during interrupt scare me. The ISR should merely signal
27 * thread context, ACK the interrupt, and move on. -- BenC */
29 #include <linux/kernel.h>
30 #include <linux/sched/signal.h>
34 u8
solo_i2c_readbyte(struct solo_dev
*solo_dev
, int id
, u8 addr
, u8 off
)
36 struct i2c_msg msgs
[2];
44 msgs
[1].flags
= I2C_M_RD
;
49 i2c_transfer(&solo_dev
->i2c_adap
[id
], msgs
, 2);
54 void solo_i2c_writebyte(struct solo_dev
*solo_dev
, int id
, u8 addr
,
67 i2c_transfer(&solo_dev
->i2c_adap
[id
], &msgs
, 1);
70 static void solo_i2c_flush(struct solo_dev
*solo_dev
, int wr
)
74 ctrl
= SOLO_IIC_CH_SET(solo_dev
->i2c_id
);
76 if (solo_dev
->i2c_state
== IIC_STATE_START
)
77 ctrl
|= SOLO_IIC_START
;
80 ctrl
|= SOLO_IIC_WRITE
;
82 ctrl
|= SOLO_IIC_READ
;
83 if (!(solo_dev
->i2c_msg
->flags
& I2C_M_NO_RD_ACK
))
84 ctrl
|= SOLO_IIC_ACK_EN
;
87 if (solo_dev
->i2c_msg_ptr
== solo_dev
->i2c_msg
->len
)
88 ctrl
|= SOLO_IIC_STOP
;
90 solo_reg_write(solo_dev
, SOLO_IIC_CTRL
, ctrl
);
93 static void solo_i2c_start(struct solo_dev
*solo_dev
)
95 u32 addr
= solo_dev
->i2c_msg
->addr
<< 1;
97 if (solo_dev
->i2c_msg
->flags
& I2C_M_RD
)
100 solo_dev
->i2c_state
= IIC_STATE_START
;
101 solo_reg_write(solo_dev
, SOLO_IIC_TXD
, addr
);
102 solo_i2c_flush(solo_dev
, 1);
105 static void solo_i2c_stop(struct solo_dev
*solo_dev
)
107 solo_irq_off(solo_dev
, SOLO_IRQ_IIC
);
108 solo_reg_write(solo_dev
, SOLO_IIC_CTRL
, 0);
109 solo_dev
->i2c_state
= IIC_STATE_STOP
;
110 wake_up(&solo_dev
->i2c_wait
);
113 static int solo_i2c_handle_read(struct solo_dev
*solo_dev
)
116 if (solo_dev
->i2c_msg_ptr
!= solo_dev
->i2c_msg
->len
) {
117 solo_i2c_flush(solo_dev
, 0);
121 solo_dev
->i2c_msg_ptr
= 0;
123 solo_dev
->i2c_msg_num
--;
125 if (solo_dev
->i2c_msg_num
== 0) {
126 solo_i2c_stop(solo_dev
);
130 if (!(solo_dev
->i2c_msg
->flags
& I2C_M_NOSTART
)) {
131 solo_i2c_start(solo_dev
);
133 if (solo_dev
->i2c_msg
->flags
& I2C_M_RD
)
136 solo_i2c_stop(solo_dev
);
142 static int solo_i2c_handle_write(struct solo_dev
*solo_dev
)
145 if (solo_dev
->i2c_msg_ptr
!= solo_dev
->i2c_msg
->len
) {
146 solo_reg_write(solo_dev
, SOLO_IIC_TXD
,
147 solo_dev
->i2c_msg
->buf
[solo_dev
->i2c_msg_ptr
]);
148 solo_dev
->i2c_msg_ptr
++;
149 solo_i2c_flush(solo_dev
, 1);
153 solo_dev
->i2c_msg_ptr
= 0;
155 solo_dev
->i2c_msg_num
--;
157 if (solo_dev
->i2c_msg_num
== 0) {
158 solo_i2c_stop(solo_dev
);
162 if (!(solo_dev
->i2c_msg
->flags
& I2C_M_NOSTART
)) {
163 solo_i2c_start(solo_dev
);
165 if (solo_dev
->i2c_msg
->flags
& I2C_M_RD
)
166 solo_i2c_stop(solo_dev
);
174 int solo_i2c_isr(struct solo_dev
*solo_dev
)
176 u32 status
= solo_reg_read(solo_dev
, SOLO_IIC_CTRL
);
180 if (CHK_FLAGS(status
, SOLO_IIC_STATE_TRNS
| SOLO_IIC_STATE_SIG_ERR
)
181 || solo_dev
->i2c_id
< 0) {
182 solo_i2c_stop(solo_dev
);
186 switch (solo_dev
->i2c_state
) {
187 case IIC_STATE_START
:
188 if (solo_dev
->i2c_msg
->flags
& I2C_M_RD
) {
189 solo_dev
->i2c_state
= IIC_STATE_READ
;
190 ret
= solo_i2c_handle_read(solo_dev
);
194 solo_dev
->i2c_state
= IIC_STATE_WRITE
;
196 case IIC_STATE_WRITE
:
197 ret
= solo_i2c_handle_write(solo_dev
);
201 solo_dev
->i2c_msg
->buf
[solo_dev
->i2c_msg_ptr
] =
202 solo_reg_read(solo_dev
, SOLO_IIC_RXD
);
203 solo_dev
->i2c_msg_ptr
++;
205 ret
= solo_i2c_handle_read(solo_dev
);
209 solo_i2c_stop(solo_dev
);
215 static int solo_i2c_master_xfer(struct i2c_adapter
*adap
,
216 struct i2c_msg msgs
[], int num
)
218 struct solo_dev
*solo_dev
= adap
->algo_data
;
219 unsigned long timeout
;
224 for (i
= 0; i
< SOLO_I2C_ADAPTERS
; i
++) {
225 if (&solo_dev
->i2c_adap
[i
] == adap
)
229 if (i
== SOLO_I2C_ADAPTERS
)
230 return num
; /* XXX Right return value for failure? */
232 mutex_lock(&solo_dev
->i2c_mutex
);
233 solo_dev
->i2c_id
= i
;
234 solo_dev
->i2c_msg
= msgs
;
235 solo_dev
->i2c_msg_num
= num
;
236 solo_dev
->i2c_msg_ptr
= 0;
238 solo_reg_write(solo_dev
, SOLO_IIC_CTRL
, 0);
239 solo_irq_on(solo_dev
, SOLO_IRQ_IIC
);
240 solo_i2c_start(solo_dev
);
245 prepare_to_wait(&solo_dev
->i2c_wait
, &wait
,
248 if (solo_dev
->i2c_state
== IIC_STATE_STOP
)
251 timeout
= schedule_timeout(timeout
);
255 if (signal_pending(current
))
259 finish_wait(&solo_dev
->i2c_wait
, &wait
);
260 ret
= num
- solo_dev
->i2c_msg_num
;
261 solo_dev
->i2c_state
= IIC_STATE_IDLE
;
262 solo_dev
->i2c_id
= -1;
264 mutex_unlock(&solo_dev
->i2c_mutex
);
269 static u32
solo_i2c_functionality(struct i2c_adapter
*adap
)
274 static const struct i2c_algorithm solo_i2c_algo
= {
275 .master_xfer
= solo_i2c_master_xfer
,
276 .functionality
= solo_i2c_functionality
,
279 int solo_i2c_init(struct solo_dev
*solo_dev
)
284 solo_reg_write(solo_dev
, SOLO_IIC_CFG
,
285 SOLO_IIC_PRESCALE(8) | SOLO_IIC_ENABLE
);
287 solo_dev
->i2c_id
= -1;
288 solo_dev
->i2c_state
= IIC_STATE_IDLE
;
289 init_waitqueue_head(&solo_dev
->i2c_wait
);
290 mutex_init(&solo_dev
->i2c_mutex
);
292 for (i
= 0; i
< SOLO_I2C_ADAPTERS
; i
++) {
293 struct i2c_adapter
*adap
= &solo_dev
->i2c_adap
[i
];
295 snprintf(adap
->name
, I2C_NAME_SIZE
, "%s I2C %d",
297 adap
->algo
= &solo_i2c_algo
;
298 adap
->algo_data
= solo_dev
;
300 adap
->dev
.parent
= &solo_dev
->pdev
->dev
;
302 ret
= i2c_add_adapter(adap
);
304 adap
->algo_data
= NULL
;
310 for (i
= 0; i
< SOLO_I2C_ADAPTERS
; i
++) {
311 if (!solo_dev
->i2c_adap
[i
].algo_data
)
313 i2c_del_adapter(&solo_dev
->i2c_adap
[i
]);
314 solo_dev
->i2c_adap
[i
].algo_data
= NULL
;
322 void solo_i2c_exit(struct solo_dev
*solo_dev
)
326 for (i
= 0; i
< SOLO_I2C_ADAPTERS
; i
++) {
327 if (!solo_dev
->i2c_adap
[i
].algo_data
)
329 i2c_del_adapter(&solo_dev
->i2c_adap
[i
]);
330 solo_dev
->i2c_adap
[i
].algo_data
= NULL
;