1 From: Russell King <rmk+kernel@armlinux.org.uk>
2 Bcc: linux@mail.armlinux.org.uk
3 Cc: linux-i2c@vger.kernel.org
4 Subject: [PATCH 04/17] i2c: pxa: re-arrange functions to flow better
6 Content-Disposition: inline
7 Content-Transfer-Encoding: 8bit
8 Content-Type: text/plain; charset="utf-8"
10 Re-arrange the PXA I2C code to avoid forward declarations, and keep
11 similar functionality (e.g. the non-IRQ mode support) together. This
12 improves code readability.
14 Signed-off-by: Russell King <rmk+kernel@armlinux.org.uk>
16 drivers/i2c/busses/i2c-pxa.c | 325 +++++++++++++++++------------------
17 1 file changed, 162 insertions(+), 163 deletions(-)
19 --- a/drivers/i2c/busses/i2c-pxa.c
20 +++ b/drivers/i2c/busses/i2c-pxa.c
21 @@ -326,7 +326,6 @@ static void i2c_pxa_scream_blue_murder(s
22 #endif /* ifdef DEBUG / else */
24 static void i2c_pxa_master_complete(struct pxa_i2c *i2c, int ret);
25 -static irqreturn_t i2c_pxa_handler(int this_irq, void *dev_id);
27 static inline int i2c_pxa_is_slavemode(struct pxa_i2c *i2c)
29 @@ -697,34 +696,6 @@ static inline void i2c_pxa_stop_message(
30 writel(icr, _ICR(i2c));
33 -static int i2c_pxa_pio_set_master(struct pxa_i2c *i2c)
35 - /* make timeout the same as for interrupt based functions */
36 - long timeout = 2 * DEF_TIMEOUT;
39 - * Wait for the bus to become free.
41 - while (timeout-- && readl(_ISR(i2c)) & (ISR_IBB | ISR_UB)) {
48 - dev_err(&i2c->adap.dev,
49 - "i2c_pxa: timeout waiting for bus free\n");
56 - writel(readl(_ICR(i2c)) | ICR_SCLE, _ICR(i2c));
62 * PXA I2C send master code
63 * 1. Load master code to IDBR and send it.
64 @@ -753,140 +724,6 @@ static int i2c_pxa_send_mastercode(struc
65 return (timeout == 0) ? I2C_RETRY : 0;
68 -static int i2c_pxa_do_pio_xfer(struct pxa_i2c *i2c,
69 - struct i2c_msg *msg, int num)
71 - unsigned long timeout = 500000; /* 5 seconds */
74 - ret = i2c_pxa_pio_set_master(i2c);
84 - i2c_pxa_start_message(i2c);
86 - while (i2c->msg_num > 0 && --timeout) {
87 - i2c_pxa_handler(0, i2c);
91 - i2c_pxa_stop_message(i2c);
94 - * We place the return code in i2c->msg_idx.
100 - i2c_pxa_scream_blue_murder(i2c, "timeout");
108 - * We are protected by the adapter bus mutex.
110 -static int i2c_pxa_do_xfer(struct pxa_i2c *i2c, struct i2c_msg *msg, int num)
116 - * Wait for the bus to become free.
118 - ret = i2c_pxa_wait_bus_not_busy(i2c);
120 - dev_err(&i2c->adap.dev, "i2c_pxa: timeout waiting for bus free\n");
127 - ret = i2c_pxa_set_master(i2c);
129 - dev_err(&i2c->adap.dev, "i2c_pxa_set_master: error %d\n", ret);
133 - if (i2c->high_mode) {
134 - ret = i2c_pxa_send_mastercode(i2c);
136 - dev_err(&i2c->adap.dev, "i2c_pxa_send_mastercode timeout\n");
141 - spin_lock_irq(&i2c->lock);
144 - i2c->msg_num = num;
147 - i2c->irqlogidx = 0;
149 - i2c_pxa_start_message(i2c);
151 - spin_unlock_irq(&i2c->lock);
154 - * The rest of the processing occurs in the interrupt handler.
156 - timeout = wait_event_timeout(i2c->wait, i2c->msg_num == 0, HZ * 5);
157 - i2c_pxa_stop_message(i2c);
160 - * We place the return code in i2c->msg_idx.
162 - ret = i2c->msg_idx;
164 - if (!timeout && i2c->msg_num) {
165 - i2c_pxa_scream_blue_murder(i2c, "timeout");
173 -static int i2c_pxa_pio_xfer(struct i2c_adapter *adap,
174 - struct i2c_msg msgs[], int num)
176 - struct pxa_i2c *i2c = adap->algo_data;
179 - /* If the I2C controller is disabled we need to reset it
180 - (probably due to a suspend/resume destroying state). We do
181 - this here as we can then avoid worrying about resuming the
182 - controller before its users. */
183 - if (!(readl(_ICR(i2c)) & ICR_IUE))
184 - i2c_pxa_reset(i2c);
186 - for (i = adap->retries; i >= 0; i--) {
187 - ret = i2c_pxa_do_pio_xfer(i2c, msgs, num);
188 - if (ret != I2C_RETRY)
192 - dev_dbg(&adap->dev, "Retrying transmission\n");
195 - i2c_pxa_scream_blue_murder(i2c, "exhausted retries");
198 - i2c_pxa_set_slave(i2c, ret);
203 * i2c_pxa_master_complete - complete the message and wake up.
205 @@ -1093,6 +930,71 @@ static irqreturn_t i2c_pxa_handler(int t
210 + * We are protected by the adapter bus mutex.
212 +static int i2c_pxa_do_xfer(struct pxa_i2c *i2c, struct i2c_msg *msg, int num)
218 + * Wait for the bus to become free.
220 + ret = i2c_pxa_wait_bus_not_busy(i2c);
222 + dev_err(&i2c->adap.dev, "i2c_pxa: timeout waiting for bus free\n");
229 + ret = i2c_pxa_set_master(i2c);
231 + dev_err(&i2c->adap.dev, "i2c_pxa_set_master: error %d\n", ret);
235 + if (i2c->high_mode) {
236 + ret = i2c_pxa_send_mastercode(i2c);
238 + dev_err(&i2c->adap.dev, "i2c_pxa_send_mastercode timeout\n");
243 + spin_lock_irq(&i2c->lock);
246 + i2c->msg_num = num;
249 + i2c->irqlogidx = 0;
251 + i2c_pxa_start_message(i2c);
253 + spin_unlock_irq(&i2c->lock);
256 + * The rest of the processing occurs in the interrupt handler.
258 + timeout = wait_event_timeout(i2c->wait, i2c->msg_num == 0, HZ * 5);
259 + i2c_pxa_stop_message(i2c);
262 + * We place the return code in i2c->msg_idx.
264 + ret = i2c->msg_idx;
266 + if (!timeout && i2c->msg_num) {
267 + i2c_pxa_scream_blue_murder(i2c, "timeout");
275 static int i2c_pxa_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[], int num)
277 @@ -1126,6 +1028,103 @@ static const struct i2c_algorithm i2c_px
278 .functionality = i2c_pxa_functionality,
281 +/* Non-interrupt mode support */
282 +static int i2c_pxa_pio_set_master(struct pxa_i2c *i2c)
284 + /* make timeout the same as for interrupt based functions */
285 + long timeout = 2 * DEF_TIMEOUT;
288 + * Wait for the bus to become free.
290 + while (timeout-- && readl(_ISR(i2c)) & (ISR_IBB | ISR_UB)) {
297 + dev_err(&i2c->adap.dev,
298 + "i2c_pxa: timeout waiting for bus free\n");
305 + writel(readl(_ICR(i2c)) | ICR_SCLE, _ICR(i2c));
310 +static int i2c_pxa_do_pio_xfer(struct pxa_i2c *i2c,
311 + struct i2c_msg *msg, int num)
313 + unsigned long timeout = 500000; /* 5 seconds */
316 + ret = i2c_pxa_pio_set_master(i2c);
321 + i2c->msg_num = num;
324 + i2c->irqlogidx = 0;
326 + i2c_pxa_start_message(i2c);
328 + while (i2c->msg_num > 0 && --timeout) {
329 + i2c_pxa_handler(0, i2c);
333 + i2c_pxa_stop_message(i2c);
336 + * We place the return code in i2c->msg_idx.
338 + ret = i2c->msg_idx;
341 + if (timeout == 0) {
342 + i2c_pxa_scream_blue_murder(i2c, "timeout");
349 +static int i2c_pxa_pio_xfer(struct i2c_adapter *adap,
350 + struct i2c_msg msgs[], int num)
352 + struct pxa_i2c *i2c = adap->algo_data;
355 + /* If the I2C controller is disabled we need to reset it
356 + (probably due to a suspend/resume destroying state). We do
357 + this here as we can then avoid worrying about resuming the
358 + controller before its users. */
359 + if (!(readl(_ICR(i2c)) & ICR_IUE))
360 + i2c_pxa_reset(i2c);
362 + for (i = adap->retries; i >= 0; i--) {
363 + ret = i2c_pxa_do_pio_xfer(i2c, msgs, num);
364 + if (ret != I2C_RETRY)
368 + dev_dbg(&adap->dev, "Retrying transmission\n");
371 + i2c_pxa_scream_blue_murder(i2c, "exhausted retries");
374 + i2c_pxa_set_slave(i2c, ret);
378 static const struct i2c_algorithm i2c_pxa_pio_algorithm = {
379 .master_xfer = i2c_pxa_pio_xfer,
380 .functionality = i2c_pxa_functionality,