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 diff --git a/drivers/i2c/busses/i2c-pxa.c b/drivers/i2c/busses/i2c-pxa.c
20 index ee83d2e46de0..e5f00ae39861 100644
21 --- a/drivers/i2c/busses/i2c-pxa.c
22 +++ b/drivers/i2c/busses/i2c-pxa.c
23 @@ -327,7 +327,6 @@ static void i2c_pxa_scream_blue_murder(struct pxa_i2c *i2c, const char *why)
24 #endif /* ifdef DEBUG / else */
26 static void i2c_pxa_master_complete(struct pxa_i2c *i2c, int ret);
27 -static irqreturn_t i2c_pxa_handler(int this_irq, void *dev_id);
29 static inline int i2c_pxa_is_slavemode(struct pxa_i2c *i2c)
31 @@ -703,34 +702,6 @@ static inline void i2c_pxa_stop_message(struct pxa_i2c *i2c)
32 writel(icr, _ICR(i2c));
35 -static int i2c_pxa_pio_set_master(struct pxa_i2c *i2c)
37 - /* make timeout the same as for interrupt based functions */
38 - long timeout = 2 * DEF_TIMEOUT;
41 - * Wait for the bus to become free.
43 - while (timeout-- && readl(_ISR(i2c)) & (ISR_IBB | ISR_UB)) {
50 - dev_err(&i2c->adap.dev,
51 - "i2c_pxa: timeout waiting for bus free\n");
58 - writel(readl(_ICR(i2c)) | ICR_SCLE, _ICR(i2c));
64 * PXA I2C send master code
65 * 1. Load master code to IDBR and send it.
66 @@ -759,140 +730,6 @@ static int i2c_pxa_send_mastercode(struct pxa_i2c *i2c)
67 return (timeout == 0) ? I2C_RETRY : 0;
70 -static int i2c_pxa_do_pio_xfer(struct pxa_i2c *i2c,
71 - struct i2c_msg *msg, int num)
73 - unsigned long timeout = 500000; /* 5 seconds */
76 - ret = i2c_pxa_pio_set_master(i2c);
86 - i2c_pxa_start_message(i2c);
88 - while (i2c->msg_num > 0 && --timeout) {
89 - i2c_pxa_handler(0, i2c);
93 - i2c_pxa_stop_message(i2c);
96 - * We place the return code in i2c->msg_idx.
101 - if (timeout == 0) {
102 - i2c_pxa_scream_blue_murder(i2c, "timeout");
110 - * We are protected by the adapter bus mutex.
112 -static int i2c_pxa_do_xfer(struct pxa_i2c *i2c, struct i2c_msg *msg, int num)
118 - * Wait for the bus to become free.
120 - ret = i2c_pxa_wait_bus_not_busy(i2c);
122 - dev_err(&i2c->adap.dev, "i2c_pxa: timeout waiting for bus free\n");
129 - ret = i2c_pxa_set_master(i2c);
131 - dev_err(&i2c->adap.dev, "i2c_pxa_set_master: error %d\n", ret);
135 - if (i2c->high_mode) {
136 - ret = i2c_pxa_send_mastercode(i2c);
138 - dev_err(&i2c->adap.dev, "i2c_pxa_send_mastercode timeout\n");
143 - spin_lock_irq(&i2c->lock);
146 - i2c->msg_num = num;
149 - i2c->irqlogidx = 0;
151 - i2c_pxa_start_message(i2c);
153 - spin_unlock_irq(&i2c->lock);
156 - * The rest of the processing occurs in the interrupt handler.
158 - timeout = wait_event_timeout(i2c->wait, i2c->msg_num == 0, HZ * 5);
159 - i2c_pxa_stop_message(i2c);
162 - * We place the return code in i2c->msg_idx.
164 - ret = i2c->msg_idx;
166 - if (!timeout && i2c->msg_num) {
167 - i2c_pxa_scream_blue_murder(i2c, "timeout");
175 -static int i2c_pxa_pio_xfer(struct i2c_adapter *adap,
176 - struct i2c_msg msgs[], int num)
178 - struct pxa_i2c *i2c = adap->algo_data;
181 - /* If the I2C controller is disabled we need to reset it
182 - (probably due to a suspend/resume destroying state). We do
183 - this here as we can then avoid worrying about resuming the
184 - controller before its users. */
185 - if (!(readl(_ICR(i2c)) & ICR_IUE))
186 - i2c_pxa_reset(i2c);
188 - for (i = adap->retries; i >= 0; i--) {
189 - ret = i2c_pxa_do_pio_xfer(i2c, msgs, num);
190 - if (ret != I2C_RETRY)
194 - dev_dbg(&adap->dev, "Retrying transmission\n");
197 - i2c_pxa_scream_blue_murder(i2c, "exhausted retries");
200 - i2c_pxa_set_slave(i2c, ret);
205 * i2c_pxa_master_complete - complete the message and wake up.
207 @@ -1099,6 +936,71 @@ static irqreturn_t i2c_pxa_handler(int this_irq, void *dev_id)
212 + * We are protected by the adapter bus mutex.
214 +static int i2c_pxa_do_xfer(struct pxa_i2c *i2c, struct i2c_msg *msg, int num)
220 + * Wait for the bus to become free.
222 + ret = i2c_pxa_wait_bus_not_busy(i2c);
224 + dev_err(&i2c->adap.dev, "i2c_pxa: timeout waiting for bus free\n");
231 + ret = i2c_pxa_set_master(i2c);
233 + dev_err(&i2c->adap.dev, "i2c_pxa_set_master: error %d\n", ret);
237 + if (i2c->high_mode) {
238 + ret = i2c_pxa_send_mastercode(i2c);
240 + dev_err(&i2c->adap.dev, "i2c_pxa_send_mastercode timeout\n");
245 + spin_lock_irq(&i2c->lock);
248 + i2c->msg_num = num;
251 + i2c->irqlogidx = 0;
253 + i2c_pxa_start_message(i2c);
255 + spin_unlock_irq(&i2c->lock);
258 + * The rest of the processing occurs in the interrupt handler.
260 + timeout = wait_event_timeout(i2c->wait, i2c->msg_num == 0, HZ * 5);
261 + i2c_pxa_stop_message(i2c);
264 + * We place the return code in i2c->msg_idx.
266 + ret = i2c->msg_idx;
268 + if (!timeout && i2c->msg_num) {
269 + i2c_pxa_scream_blue_murder(i2c, "timeout");
277 static int i2c_pxa_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[], int num)
279 @@ -1132,6 +1034,103 @@ static const struct i2c_algorithm i2c_pxa_algorithm = {
280 .functionality = i2c_pxa_functionality,
283 +/* Non-interrupt mode support */
284 +static int i2c_pxa_pio_set_master(struct pxa_i2c *i2c)
286 + /* make timeout the same as for interrupt based functions */
287 + long timeout = 2 * DEF_TIMEOUT;
290 + * Wait for the bus to become free.
292 + while (timeout-- && readl(_ISR(i2c)) & (ISR_IBB | ISR_UB)) {
299 + dev_err(&i2c->adap.dev,
300 + "i2c_pxa: timeout waiting for bus free\n");
307 + writel(readl(_ICR(i2c)) | ICR_SCLE, _ICR(i2c));
312 +static int i2c_pxa_do_pio_xfer(struct pxa_i2c *i2c,
313 + struct i2c_msg *msg, int num)
315 + unsigned long timeout = 500000; /* 5 seconds */
318 + ret = i2c_pxa_pio_set_master(i2c);
323 + i2c->msg_num = num;
326 + i2c->irqlogidx = 0;
328 + i2c_pxa_start_message(i2c);
330 + while (i2c->msg_num > 0 && --timeout) {
331 + i2c_pxa_handler(0, i2c);
335 + i2c_pxa_stop_message(i2c);
338 + * We place the return code in i2c->msg_idx.
340 + ret = i2c->msg_idx;
343 + if (timeout == 0) {
344 + i2c_pxa_scream_blue_murder(i2c, "timeout");
351 +static int i2c_pxa_pio_xfer(struct i2c_adapter *adap,
352 + struct i2c_msg msgs[], int num)
354 + struct pxa_i2c *i2c = adap->algo_data;
357 + /* If the I2C controller is disabled we need to reset it
358 + (probably due to a suspend/resume destroying state). We do
359 + this here as we can then avoid worrying about resuming the
360 + controller before its users. */
361 + if (!(readl(_ICR(i2c)) & ICR_IUE))
362 + i2c_pxa_reset(i2c);
364 + for (i = adap->retries; i >= 0; i--) {
365 + ret = i2c_pxa_do_pio_xfer(i2c, msgs, num);
366 + if (ret != I2C_RETRY)
370 + dev_dbg(&adap->dev, "Retrying transmission\n");
373 + i2c_pxa_scream_blue_murder(i2c, "exhausted retries");
376 + i2c_pxa_set_slave(i2c, ret);
380 static const struct i2c_algorithm i2c_pxa_pio_algorithm = {
381 .master_xfer = i2c_pxa_pio_xfer,
382 .functionality = i2c_pxa_functionality,