aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/mfd/pcf50633-core.c
diff options
context:
space:
mode:
authorLars-Peter Clausen <lars@metafoo.de>2010-05-11 20:10:56 -0400
committerSamuel Ortiz <sameo@linux.intel.com>2010-05-27 19:37:50 -0400
commit380c09f6489f1fd773a697e9e2a156c083a34fc5 (patch)
tree4069f2d01ed326a04cd4fabfc0258c3cb7d0c23f /drivers/mfd/pcf50633-core.c
parentf7b2a77fe6f7b13b9cbf1909f032adef0be63ce1 (diff)
mfd: Move pcf50633 irq related functions to its own file.
This reduces code clutter a bit and will ease an migration to genirq. Signed-off-by: Lars-Peter Clausen <lars@metafoo.de> Signed-off-by: Samuel Ortiz <sameo@linux.intel.com>
Diffstat (limited to 'drivers/mfd/pcf50633-core.c')
-rw-r--r--drivers/mfd/pcf50633-core.c303
1 files changed, 10 insertions, 293 deletions
diff --git a/drivers/mfd/pcf50633-core.c b/drivers/mfd/pcf50633-core.c
index b3bef4998744..704736e6e9b9 100644
--- a/drivers/mfd/pcf50633-core.c
+++ b/drivers/mfd/pcf50633-core.c
@@ -21,16 +21,16 @@
21#include <linux/workqueue.h> 21#include <linux/workqueue.h>
22#include <linux/platform_device.h> 22#include <linux/platform_device.h>
23#include <linux/i2c.h> 23#include <linux/i2c.h>
24#include <linux/irq.h>
25#include <linux/slab.h> 24#include <linux/slab.h>
26 25
27#include <linux/mfd/pcf50633/core.h> 26#include <linux/mfd/pcf50633/core.h>
28 27
29/* Two MBCS registers used during cold start */ 28int pcf50633_irq_init(struct pcf50633 *pcf, int irq);
30#define PCF50633_REG_MBCS1 0x4b 29void pcf50633_irq_free(struct pcf50633 *pcf);
31#define PCF50633_REG_MBCS2 0x4c 30#ifdef CONFIG_PM
32#define PCF50633_MBCS1_USBPRES 0x01 31int pcf50633_irq_suspend(struct pcf50633 *pcf);
33#define PCF50633_MBCS1_ADAPTPRES 0x01 32int pcf50633_irq_resume(struct pcf50633 *pcf);
33#endif
34 34
35static int __pcf50633_read(struct pcf50633 *pcf, u8 reg, int num, u8 *data) 35static int __pcf50633_read(struct pcf50633 *pcf, u8 reg, int num, u8 *data)
36{ 36{
@@ -215,228 +215,6 @@ static struct attribute_group pcf_attr_group = {
215 .attrs = pcf_sysfs_entries, 215 .attrs = pcf_sysfs_entries,
216}; 216};
217 217
218int pcf50633_register_irq(struct pcf50633 *pcf, int irq,
219 void (*handler) (int, void *), void *data)
220{
221 if (irq < 0 || irq >= PCF50633_NUM_IRQ || !handler)
222 return -EINVAL;
223
224 if (WARN_ON(pcf->irq_handler[irq].handler))
225 return -EBUSY;
226
227 mutex_lock(&pcf->lock);
228 pcf->irq_handler[irq].handler = handler;
229 pcf->irq_handler[irq].data = data;
230 mutex_unlock(&pcf->lock);
231
232 return 0;
233}
234EXPORT_SYMBOL_GPL(pcf50633_register_irq);
235
236int pcf50633_free_irq(struct pcf50633 *pcf, int irq)
237{
238 if (irq < 0 || irq >= PCF50633_NUM_IRQ)
239 return -EINVAL;
240
241 mutex_lock(&pcf->lock);
242 pcf->irq_handler[irq].handler = NULL;
243 mutex_unlock(&pcf->lock);
244
245 return 0;
246}
247EXPORT_SYMBOL_GPL(pcf50633_free_irq);
248
249static int __pcf50633_irq_mask_set(struct pcf50633 *pcf, int irq, u8 mask)
250{
251 u8 reg, bits, tmp;
252 int ret = 0, idx;
253
254 idx = irq >> 3;
255 reg = PCF50633_REG_INT1M + idx;
256 bits = 1 << (irq & 0x07);
257
258 mutex_lock(&pcf->lock);
259
260 if (mask) {
261 ret = __pcf50633_read(pcf, reg, 1, &tmp);
262 if (ret < 0)
263 goto out;
264
265 tmp |= bits;
266
267 ret = __pcf50633_write(pcf, reg, 1, &tmp);
268 if (ret < 0)
269 goto out;
270
271 pcf->mask_regs[idx] &= ~bits;
272 pcf->mask_regs[idx] |= bits;
273 } else {
274 ret = __pcf50633_read(pcf, reg, 1, &tmp);
275 if (ret < 0)
276 goto out;
277
278 tmp &= ~bits;
279
280 ret = __pcf50633_write(pcf, reg, 1, &tmp);
281 if (ret < 0)
282 goto out;
283
284 pcf->mask_regs[idx] &= ~bits;
285 }
286out:
287 mutex_unlock(&pcf->lock);
288
289 return ret;
290}
291
292int pcf50633_irq_mask(struct pcf50633 *pcf, int irq)
293{
294 dev_dbg(pcf->dev, "Masking IRQ %d\n", irq);
295
296 return __pcf50633_irq_mask_set(pcf, irq, 1);
297}
298EXPORT_SYMBOL_GPL(pcf50633_irq_mask);
299
300int pcf50633_irq_unmask(struct pcf50633 *pcf, int irq)
301{
302 dev_dbg(pcf->dev, "Unmasking IRQ %d\n", irq);
303
304 return __pcf50633_irq_mask_set(pcf, irq, 0);
305}
306EXPORT_SYMBOL_GPL(pcf50633_irq_unmask);
307
308int pcf50633_irq_mask_get(struct pcf50633 *pcf, int irq)
309{
310 u8 reg, bits;
311
312 reg = irq >> 3;
313 bits = 1 << (irq & 0x07);
314
315 return pcf->mask_regs[reg] & bits;
316}
317EXPORT_SYMBOL_GPL(pcf50633_irq_mask_get);
318
319static void pcf50633_irq_call_handler(struct pcf50633 *pcf, int irq)
320{
321 if (pcf->irq_handler[irq].handler)
322 pcf->irq_handler[irq].handler(irq, pcf->irq_handler[irq].data);
323}
324
325/* Maximum amount of time ONKEY is held before emergency action is taken */
326#define PCF50633_ONKEY1S_TIMEOUT 8
327
328static irqreturn_t pcf50633_irq(int irq, void *data)
329{
330 struct pcf50633 *pcf = data;
331 int ret, i, j;
332 u8 pcf_int[5], chgstat;
333
334 /* Read the 5 INT regs in one transaction */
335 ret = pcf50633_read_block(pcf, PCF50633_REG_INT1,
336 ARRAY_SIZE(pcf_int), pcf_int);
337 if (ret != ARRAY_SIZE(pcf_int)) {
338 dev_err(pcf->dev, "Error reading INT registers\n");
339
340 /*
341 * If this doesn't ACK the interrupt to the chip, we'll be
342 * called once again as we're level triggered.
343 */
344 goto out;
345 }
346
347 /* defeat 8s death from lowsys on A5 */
348 pcf50633_reg_write(pcf, PCF50633_REG_OOCSHDWN, 0x04);
349
350 /* We immediately read the usb and adapter status. We thus make sure
351 * only of USBINS/USBREM IRQ handlers are called */
352 if (pcf_int[0] & (PCF50633_INT1_USBINS | PCF50633_INT1_USBREM)) {
353 chgstat = pcf50633_reg_read(pcf, PCF50633_REG_MBCS2);
354 if (chgstat & (0x3 << 4))
355 pcf_int[0] &= ~PCF50633_INT1_USBREM;
356 else
357 pcf_int[0] &= ~PCF50633_INT1_USBINS;
358 }
359
360 /* Make sure only one of ADPINS or ADPREM is set */
361 if (pcf_int[0] & (PCF50633_INT1_ADPINS | PCF50633_INT1_ADPREM)) {
362 chgstat = pcf50633_reg_read(pcf, PCF50633_REG_MBCS2);
363 if (chgstat & (0x3 << 4))
364 pcf_int[0] &= ~PCF50633_INT1_ADPREM;
365 else
366 pcf_int[0] &= ~PCF50633_INT1_ADPINS;
367 }
368
369 dev_dbg(pcf->dev, "INT1=0x%02x INT2=0x%02x INT3=0x%02x "
370 "INT4=0x%02x INT5=0x%02x\n", pcf_int[0],
371 pcf_int[1], pcf_int[2], pcf_int[3], pcf_int[4]);
372
373 /* Some revisions of the chip don't have a 8s standby mode on
374 * ONKEY1S press. We try to manually do it in such cases. */
375 if ((pcf_int[0] & PCF50633_INT1_SECOND) && pcf->onkey1s_held) {
376 dev_info(pcf->dev, "ONKEY1S held for %d secs\n",
377 pcf->onkey1s_held);
378 if (pcf->onkey1s_held++ == PCF50633_ONKEY1S_TIMEOUT)
379 if (pcf->pdata->force_shutdown)
380 pcf->pdata->force_shutdown(pcf);
381 }
382
383 if (pcf_int[2] & PCF50633_INT3_ONKEY1S) {
384 dev_info(pcf->dev, "ONKEY1S held\n");
385 pcf->onkey1s_held = 1 ;
386
387 /* Unmask IRQ_SECOND */
388 pcf50633_reg_clear_bits(pcf, PCF50633_REG_INT1M,
389 PCF50633_INT1_SECOND);
390
391 /* Unmask IRQ_ONKEYR */
392 pcf50633_reg_clear_bits(pcf, PCF50633_REG_INT2M,
393 PCF50633_INT2_ONKEYR);
394 }
395
396 if ((pcf_int[1] & PCF50633_INT2_ONKEYR) && pcf->onkey1s_held) {
397 pcf->onkey1s_held = 0;
398
399 /* Mask SECOND and ONKEYR interrupts */
400 if (pcf->mask_regs[0] & PCF50633_INT1_SECOND)
401 pcf50633_reg_set_bit_mask(pcf,
402 PCF50633_REG_INT1M,
403 PCF50633_INT1_SECOND,
404 PCF50633_INT1_SECOND);
405
406 if (pcf->mask_regs[1] & PCF50633_INT2_ONKEYR)
407 pcf50633_reg_set_bit_mask(pcf,
408 PCF50633_REG_INT2M,
409 PCF50633_INT2_ONKEYR,
410 PCF50633_INT2_ONKEYR);
411 }
412
413 /* Have we just resumed ? */
414 if (pcf->is_suspended) {
415 pcf->is_suspended = 0;
416
417 /* Set the resume reason filtering out non resumers */
418 for (i = 0; i < ARRAY_SIZE(pcf_int); i++)
419 pcf->resume_reason[i] = pcf_int[i] &
420 pcf->pdata->resumers[i];
421
422 /* Make sure we don't pass on any ONKEY events to
423 * userspace now */
424 pcf_int[1] &= ~(PCF50633_INT2_ONKEYR | PCF50633_INT2_ONKEYF);
425 }
426
427 for (i = 0; i < ARRAY_SIZE(pcf_int); i++) {
428 /* Unset masked interrupts */
429 pcf_int[i] &= ~pcf->mask_regs[i];
430
431 for (j = 0; j < 8 ; j++)
432 if (pcf_int[i] & (1 << j))
433 pcf50633_irq_call_handler(pcf, (i * 8) + j);
434 }
435
436out:
437 return IRQ_HANDLED
438}
439
440static void 218static void
441pcf50633_client_dev_register(struct pcf50633 *pcf, const char *name, 219pcf50633_client_dev_register(struct pcf50633 *pcf, const char *name,
442 struct platform_device **pdev) 220 struct platform_device **pdev)
@@ -463,58 +241,17 @@ pcf50633_client_dev_register(struct pcf50633 *pcf, const char *name,
463static int pcf50633_suspend(struct i2c_client *client, pm_message_t state) 241static int pcf50633_suspend(struct i2c_client *client, pm_message_t state)
464{ 242{
465 struct pcf50633 *pcf; 243 struct pcf50633 *pcf;
466 int ret = 0, i;
467 u8 res[5];
468
469 pcf = i2c_get_clientdata(client); 244 pcf = i2c_get_clientdata(client);
470 245
471 /* Make sure our interrupt handlers are not called 246 return pcf50633_irq_suspend(pcf);
472 * henceforth */
473 disable_irq(pcf->irq);
474
475 /* Save the masks */
476 ret = pcf50633_read_block(pcf, PCF50633_REG_INT1M,
477 ARRAY_SIZE(pcf->suspend_irq_masks),
478 pcf->suspend_irq_masks);
479 if (ret < 0) {
480 dev_err(pcf->dev, "error saving irq masks\n");
481 goto out;
482 }
483
484 /* Write wakeup irq masks */
485 for (i = 0; i < ARRAY_SIZE(res); i++)
486 res[i] = ~pcf->pdata->resumers[i];
487
488 ret = pcf50633_write_block(pcf, PCF50633_REG_INT1M,
489 ARRAY_SIZE(res), &res[0]);
490 if (ret < 0) {
491 dev_err(pcf->dev, "error writing wakeup irq masks\n");
492 goto out;
493 }
494
495 pcf->is_suspended = 1;
496
497out:
498 return ret;
499} 247}
500 248
501static int pcf50633_resume(struct i2c_client *client) 249static int pcf50633_resume(struct i2c_client *client)
502{ 250{
503 struct pcf50633 *pcf; 251 struct pcf50633 *pcf;
504 int ret;
505
506 pcf = i2c_get_clientdata(client); 252 pcf = i2c_get_clientdata(client);
507 253
508 /* Write the saved mask registers */ 254 return pcf50633_irq_resume(pcf);
509 ret = pcf50633_write_block(pcf, PCF50633_REG_INT1M,
510 ARRAY_SIZE(pcf->suspend_irq_masks),
511 pcf->suspend_irq_masks);
512 if (ret < 0)
513 dev_err(pcf->dev, "Error restoring saved suspend masks\n");
514
515 enable_irq(pcf->irq);
516
517 return 0;
518} 255}
519#else 256#else
520#define pcf50633_suspend NULL 257#define pcf50633_suspend NULL
@@ -545,7 +282,6 @@ static int __devinit pcf50633_probe(struct i2c_client *client,
545 i2c_set_clientdata(client, pcf); 282 i2c_set_clientdata(client, pcf);
546 pcf->dev = &client->dev; 283 pcf->dev = &client->dev;
547 pcf->i2c_client = client; 284 pcf->i2c_client = client;
548 pcf->irq = client->irq;
549 285
550 version = pcf50633_reg_read(pcf, 0); 286 version = pcf50633_reg_read(pcf, 0);
551 variant = pcf50633_reg_read(pcf, 1); 287 variant = pcf50633_reg_read(pcf, 1);
@@ -558,22 +294,7 @@ static int __devinit pcf50633_probe(struct i2c_client *client,
558 dev_info(pcf->dev, "Probed device version %d variant %d\n", 294 dev_info(pcf->dev, "Probed device version %d variant %d\n",
559 version, variant); 295 version, variant);
560 296
561 /* Enable all interrupts except RTC SECOND */ 297 pcf50633_irq_init(pcf, client->irq);
562 pcf->mask_regs[0] = 0x80;
563 pcf50633_reg_write(pcf, PCF50633_REG_INT1M, pcf->mask_regs[0]);
564 pcf50633_reg_write(pcf, PCF50633_REG_INT2M, 0x00);
565 pcf50633_reg_write(pcf, PCF50633_REG_INT3M, 0x00);
566 pcf50633_reg_write(pcf, PCF50633_REG_INT4M, 0x00);
567 pcf50633_reg_write(pcf, PCF50633_REG_INT5M, 0x00);
568
569 ret = request_threaded_irq(client->irq, NULL, pcf50633_irq,
570 IRQF_TRIGGER_LOW | IRQF_ONESHOT,
571 "pcf50633", pcf);
572
573 if (ret) {
574 dev_err(pcf->dev, "Failed to request IRQ %d\n", ret);
575 goto err_free;
576 }
577 298
578 /* Create sub devices */ 299 /* Create sub devices */
579 pcf50633_client_dev_register(pcf, "pcf50633-input", 300 pcf50633_client_dev_register(pcf, "pcf50633-input",
@@ -605,10 +326,6 @@ static int __devinit pcf50633_probe(struct i2c_client *client,
605 platform_device_add(pdev); 326 platform_device_add(pdev);
606 } 327 }
607 328
608 if (enable_irq_wake(client->irq) < 0)
609 dev_err(pcf->dev, "IRQ %u cannot be enabled as wake-up source"
610 "in this hardware revision", client->irq);
611
612 ret = sysfs_create_group(&client->dev.kobj, &pcf_attr_group); 329 ret = sysfs_create_group(&client->dev.kobj, &pcf_attr_group);
613 if (ret) 330 if (ret)
614 dev_err(pcf->dev, "error creating sysfs entries\n"); 331 dev_err(pcf->dev, "error creating sysfs entries\n");
@@ -630,7 +347,7 @@ static int __devexit pcf50633_remove(struct i2c_client *client)
630 struct pcf50633 *pcf = i2c_get_clientdata(client); 347 struct pcf50633 *pcf = i2c_get_clientdata(client);
631 int i; 348 int i;
632 349
633 free_irq(pcf->irq, pcf); 350 pcf50633_irq_free(pcf);
634 351
635 platform_device_unregister(pcf->input_pdev); 352 platform_device_unregister(pcf->input_pdev);
636 platform_device_unregister(pcf->rtc_pdev); 353 platform_device_unregister(pcf->rtc_pdev);