1 From eaa55efc0b559abebbcf2acea6ce267cea4c26f2 Mon Sep 17 00:00:00 2001
2 From: Andy Green <andy@openmoko.com>
3 Date: Wed, 2 Jul 2008 22:41:52 +0100
4 Subject: [PATCH] fix-pcf50633-disable-irq-from-suspend-until-resume.patch
6 Disable pcf interrupt (not for wake, just as interrupt) in
7 suspend, re-enable it again just before we force-call the
8 workqueue function at end of pcf resume, which leads to
9 pcf interrupt source registers getting cleared so it can
10 signal an interrupt normally again.
12 This change ends the uncontrolled appearance of pcf interrupts
13 during resume time which previously caused the work to attempt
14 to use the I2C stuff before i2c host device had itself resumed.
15 Now the isr work is only queued, and the isr work function called,
16 definitively after pcf resume completes.
18 In suspend time, the work function may have been queued some
19 time before and be pending, and it could still show up at a
20 bad time. Therefore if the work function sees that it is
21 coming since the start of pcf50633 suspend function, it
22 aborts without attempting to read the pcf interrupt regs,
23 leaving them for resume to take care of.
25 USB current limit and no battery work functions are also made
26 aware of suspend state and act accordingly.
28 Lastly I noticed that in early resume, i2c_get_clientdata(&pcf->client)
29 returns NULL, presumably because i2c device is still suspended. This
30 could easily make trouble for async events like interrupt work,
31 since pcf pointer is the client data. Disabling appearance of the
32 work until after pcf50633 resume will also avoid that.
34 Signed-off-by: Andy Green <andy@openmoko.com>
36 drivers/i2c/chips/pcf50633.c | 93 +++++++++++++++++++++++++++++-------------
37 1 files changed, 65 insertions(+), 28 deletions(-)
39 diff --git a/drivers/i2c/chips/pcf50633.c b/drivers/i2c/chips/pcf50633.c
40 index c014384..120fdbc 100644
41 --- a/drivers/i2c/chips/pcf50633.c
42 +++ b/drivers/i2c/chips/pcf50633.c
43 @@ -632,6 +632,11 @@ static void pcf50633_work_usbcurlim(struct work_struct *work)
45 mutex_lock(&pcf->working_lock_usb_curlimit);
47 + /* just can't cope with it if we are suspending, don't reschedule */
48 + if ((pcf->suspend_state == PCF50633_SS_STARTING_SUSPEND) ||
49 + (pcf->suspend_state == PCF50633_SS_COMPLETED_SUSPEND))
52 dev_info(&pcf->client.dev, "pcf50633_work_usbcurlim\n");
54 if (!pcf->probe_completed)
55 @@ -663,7 +668,8 @@ bail:
57 dev_info(&pcf->client.dev, "pcf50633_work_usbcurlim rescheduling\n");
58 if (!schedule_work(&pcf->work_usb_curlimit))
59 - dev_err(&pcf->client.dev, "work item may be lost\n");
60 + dev_err(&pcf->client.dev, "curlim reschedule work "
61 + "already queued\n");
63 mutex_unlock(&pcf->working_lock_usb_curlimit);
64 /* don't spew, delaying whatever else is happening */
65 @@ -700,7 +706,7 @@ int pcf50633_notify_usb_current_limit_change(struct pcf50633_data *pcf,
66 pcf->pending_curlimit = ma;
68 if (!schedule_work(&pcf->work_usb_curlimit))
69 - dev_err(&pcf->client.dev, "work item may be lost\n");
70 + dev_err(&pcf->client.dev, "curlim work item already queued\n");
74 @@ -727,6 +733,9 @@ static void pcf50633_work_nobat(struct work_struct *work)
78 + if (pcf->suspend_state != PCF50633_SS_RUNNING)
81 /* there's a battery in there now? */
82 if (reg_read(pcf, PCF50633_REG_MBCS3) & 0x40) {
84 @@ -761,6 +770,10 @@ static void pcf50633_work(struct work_struct *work)
85 mutex_lock(&pcf->working_lock);
89 + if (!&pcf->client.dev)
93 * if we are presently suspending, we are not in a position to deal
94 * with pcf50633 interrupts at all.
95 @@ -784,42 +797,55 @@ static void pcf50633_work(struct work_struct *work)
96 * reloaded with their pre-suspend states yet. Therefore we will
97 * defer our service if we are called like that until our resume has
100 + * This shouldn't happen any more because we disable servicing this
101 + * interrupt in suspend and don't re-enable it until resume is
105 - if (pcf->have_been_suspended && (pcf->have_been_suspended < 3))
106 + if (pcf->suspend_state &&
107 + (pcf->suspend_state != PCF50633_SS_COMPLETED_RESUME))
110 + /* this is the case early in resume! Sanity check! */
111 + if (i2c_get_clientdata(&pcf->client) == NULL)
115 - * datasheet says we have to read the five IRQ
116 - * status regs in one transaction
118 - ret = i2c_smbus_read_i2c_block_data(&pcf->client, PCF50633_REG_INT1, 5,
121 + * datasheet says we have to read the five IRQ
122 + * status regs in one transaction
124 + ret = i2c_smbus_read_i2c_block_data(&pcf->client,
128 + if (ret != sizeof(pcfirq)) {
129 dev_info(&pcf->client.dev,
130 - "Oh crap PMU IRQ register read failed -- "
131 - "retrying later %d\n", ret);
132 + "Oh crap PMU IRQ register read failed -- "
133 + "retrying later %d\n", ret);
135 - * it shouldn't fail, we no longer attempt to use I2C while
136 - * it can be suspended. But we don't have much option but to
137 - * retry if if it ever did fail, because if we don't service
138 - * the interrupt to clear it, we will never see another PMU
140 + * it shouldn't fail, we no longer attempt to use
141 + * I2C while it can be suspended. But we don't have
142 + * much option but to retry if if it ever did fail,
143 + * because if we don't service the interrupt to clear
144 + * it, we will never see another PMU interrupt edge.
149 - /* hey did we just resume? */
151 - if (pcf->have_been_suspended) {
152 - /* resume is officially over now then */
153 - pcf->have_been_suspended = 0;
154 + /* hey did we just resume? (because we don't get here unless we are
155 + * running normally or the first call after resumption)
158 + if (pcf->suspend_state != PCF50633_SS_RUNNING) {
160 - * grab a copy of resume interrupt reasons
161 - * from pcf50633 POV
163 + * grab a copy of resume interrupt reasons
164 + * from pcf50633 POV
166 memcpy(pcf->pcfirq_resume, pcfirq, sizeof(pcf->pcfirq_resume));
168 + /* pcf50633 resume is really really over now then */
169 + pcf->suspend_state = PCF50633_SS_RUNNING;
172 if (!pcf->coldplug_done) {
173 @@ -1169,6 +1195,7 @@ static void pcf50633_work(struct work_struct *work)
179 input_sync(pcf->input_dev);
180 put_device(&pcf->client.dev);
181 @@ -1202,7 +1229,7 @@ static irqreturn_t pcf50633_irq(int irq, void *_pcf)
183 get_device(&pcf->client.dev);
184 if (!schedule_work(&pcf->work) && !pcf->working)
185 - dev_err(&pcf->client.dev, "work item may be lost\n");
186 + dev_err(&pcf->client.dev, "pcf irq work already queued\n");
190 @@ -2310,7 +2337,7 @@ int pcf50633_report_resumers(struct pcf50633_data *pcf, char *buf)
194 - for (n = 0; n < 40; n++)
195 + for (n = 0; n < ARRAY_SIZE(int_names); n++)
197 if (pcf->pcfirq_resume[n >> 3] & (1 >> (n & 7)))
198 end += sprintf(end, " * %s\n", int_names[n]);
199 @@ -2359,6 +2386,15 @@ static int pcf50633_suspend(struct device *dev, pm_message_t state)
201 mutex_lock(&pcf->lock);
203 + pcf->suspend_state = PCF50633_SS_STARTING_SUSPEND;
205 + /* we are not going to service any further interrupts until we
206 + * resume. If the IRQ workqueue is still pending in the background,
207 + * it will bail when it sees we set suspend state above
210 + disable_irq(pcf->irq);
212 /* Save all registers that don't "survive" standby state */
213 pcf->standby_regs.ooctim2 = __reg_read(pcf, PCF50633_REG_OOCTIM2);
215 @@ -2502,6 +2538,8 @@ static int pcf50633_resume(struct device *dev)
217 pcf->suspend_state = PCF50633_SS_COMPLETED_RESUME;
219 + enable_irq(pcf->irq);
221 mutex_unlock(&pcf->lock);
223 /* gratuitous call to PCF work function, in the case that the PCF
224 @@ -2511,8 +2549,7 @@ static int pcf50633_resume(struct device *dev)
227 get_device(&pcf->client.dev);
228 - if (!schedule_work(&pcf->work) && !pcf->working)
229 - dev_err(&pcf->client.dev, "resume work item may be lost\n");
230 + pcf50633_work(&pcf->work);
232 callback_all_resume_dependencies(&pcf->resume_dependency);