History

rtc: ds1685: use threaded interrupt

Handling of extended interrupts (kickstart, wake-up, ram-clear) was moved off to a work queue, but the interrupts aren't acknowledged in the interrupt handler. This leads to a deadlock, if driver is used with interrupts. To fix this we use a threaded interrupt, get rid of the work queue and do locking with just the rtc mutex lock.

Thomas Bogendoerfer committed 7 years ago

Commit 3b6bddd1 parent e330c3d2 files · +104 −118Browse files

Unified · Split

2 files
  1. drivers/rtc/rtc-ds1685.c +104 −116
  2. include/linux/rtc/ds1685.h +0 −2
drivers/rtc/rtc-ds1685.c+104−116
@@ -510,10 +510,6 @@ static int · 509 unmodified lines
510510 ds1685_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled)
511511 {
512512 struct ds1685_priv *rtc = dev_get_drvdata(dev);
513− unsigned long flags = 0;
514−
515− /* Enable/disable the Alarm IRQ-Enable flag. */
516− spin_lock_irqsave(&rtc->lock, flags);
517513
518514 /* Flip the requisite interrupt-enable bit. */
519515 if (enabled)
@@ -525,7 +521,6 @@ ds1685_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled) · 5 unmodified lines
525521
526522 /* Read Control C to clear all the flag bits. */
527523 rtc->read(rtc, RTC_CTRL_C);
528− spin_unlock_irqrestore(&rtc->lock, flags);
529524
530525 return 0;
531526 }
@@ -533,98 +528,18 @@ ds1685_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled) · 1 unmodified line
533528
534529
535530 /* ----------------------------------------------------------------------- */
536−/* IRQ handler & workqueue. */
537−
538−/**
539− * ds1685_rtc_irq_handler - IRQ handler.
540− * @irq: IRQ number.
541− * @dev_id: platform device pointer.
542− */
543−static irqreturn_t
544−ds1685_rtc_irq_handler(int irq, void *dev_id)
545−{
546− struct platform_device *pdev = dev_id;
547− struct ds1685_priv *rtc = platform_get_drvdata(pdev);
548− u8 ctrlb, ctrlc;
549− unsigned long events = 0;
550− u8 num_irqs = 0;
551−
552− /* Abort early if the device isn't ready yet (i.e., DEBUG_SHIRQ). */
553− if (unlikely(!rtc))
554− return IRQ_HANDLED;
555−
556− /* Ctrlb holds the interrupt-enable bits and ctrlc the flag bits. */
557− spin_lock(&rtc->lock);
558− ctrlb = rtc->read(rtc, RTC_CTRL_B);
559− ctrlc = rtc->read(rtc, RTC_CTRL_C);
560−
561− /* Is the IRQF bit set? */
562− if (likely(ctrlc & RTC_CTRL_C_IRQF)) {
563− /*
564− * We need to determine if it was one of the standard
565− * events: PF, AF, or UF. If so, we handle them and
566− * update the RTC core.
567− */
568− if (likely(ctrlc & RTC_CTRL_B_PAU_MASK)) {
569− events = RTC_IRQF;
570−
571− /* Check for a periodic interrupt. */
572− if ((ctrlb & RTC_CTRL_B_PIE) &&
573− (ctrlc & RTC_CTRL_C_PF)) {
574− events |= RTC_PF;
575− num_irqs++;
576− }
577−
578− /* Check for an alarm interrupt. */
579− if ((ctrlb & RTC_CTRL_B_AIE) &&
580− (ctrlc & RTC_CTRL_C_AF)) {
581− events |= RTC_AF;
582− num_irqs++;
583− }
584−
585− /* Check for an update interrupt. */
586− if ((ctrlb & RTC_CTRL_B_UIE) &&
587− (ctrlc & RTC_CTRL_C_UF)) {
588− events |= RTC_UF;
589− num_irqs++;
590− }
591−
592− rtc_update_irq(rtc->dev, num_irqs, events);
593− } else {
594− /*
595− * One of the "extended" interrupts was received that
596− * is not recognized by the RTC core. These need to
597− * be handled in task context as they can call other
598− * functions and the time spent in irq context needs
599− * to be minimized. Schedule them into a workqueue
600− * and inform the RTC core that the IRQs were handled.
601− */
602− spin_unlock(&rtc->lock);
603− schedule_work(&rtc->work);
604− rtc_update_irq(rtc->dev, 0, 0);
605− return IRQ_HANDLED;
606− }
607− }
608− spin_unlock(&rtc->lock);
609−
610− return events ? IRQ_HANDLED : IRQ_NONE;
611−}
531+/* IRQ handler */
612532
613533 /**
614− * ds1685_rtc_work_queue - work queue handler.
615− * @work: work_struct containing data to work on in task context.
534+ * ds1685_rtc_extended_irq - take care of extended interrupts
535+ * @rtc: pointer to the ds1685 rtc structure.
536+ * @pdev: platform device pointer.
616537 */
617538 static void
618−ds1685_rtc_work_queue(struct work_struct *work)
539+ds1685_rtc_extended_irq(struct ds1685_priv *rtc, struct platform_device *pdev)
619540 {
620− struct ds1685_priv *rtc = container_of(work,
621− struct ds1685_priv, work);
622− struct platform_device *pdev = to_platform_device(&rtc->dev->dev);
623− struct mutex *rtc_mutex = &rtc->dev->ops_lock;
624541 u8 ctrl4a, ctrl4b;
625542
626− mutex_lock(rtc_mutex);
627−
628543 ds1685_rtc_switch_to_bank1(rtc);
629544 ctrl4a = rtc->read(rtc, RTC_EXT_CTRL_4A);
630545 ctrl4b = rtc->read(rtc, RTC_EXT_CTRL_4B);
@@ -703,8 +618,76 @@ ds1685_rtc_work_queue(struct work_struct *work) · 72 unmodified lines
703618 "RAM-Clear IRQ just occurred!\n");
704619 }
705620 ds1685_rtc_switch_to_bank0(rtc);
621+}
622+
623+/**
624+ * ds1685_rtc_irq_handler - IRQ handler.
625+ * @irq: IRQ number.
626+ * @dev_id: platform device pointer.
627+ */
628+static irqreturn_t
629+ds1685_rtc_irq_handler(int irq, void *dev_id)
630+{
631+ struct platform_device *pdev = dev_id;
632+ struct ds1685_priv *rtc = platform_get_drvdata(pdev);
633+ struct mutex *rtc_mutex;
634+ u8 ctrlb, ctrlc;
635+ unsigned long events = 0;
636+ u8 num_irqs = 0;
637+
638+ /* Abort early if the device isn't ready yet (i.e., DEBUG_SHIRQ). */
639+ if (unlikely(!rtc))
640+ return IRQ_HANDLED;
641+
642+ rtc_mutex = &rtc->dev->ops_lock;
643+ mutex_lock(rtc_mutex);
644+
645+ /* Ctrlb holds the interrupt-enable bits and ctrlc the flag bits. */
646+ ctrlb = rtc->read(rtc, RTC_CTRL_B);
647+ ctrlc = rtc->read(rtc, RTC_CTRL_C);
648+
649+ /* Is the IRQF bit set? */
650+ if (likely(ctrlc & RTC_CTRL_C_IRQF)) {
651+ /*
652+ * We need to determine if it was one of the standard
653+ * events: PF, AF, or UF. If so, we handle them and
654+ * update the RTC core.
655+ */
656+ if (likely(ctrlc & RTC_CTRL_B_PAU_MASK)) {
657+ events = RTC_IRQF;
658+
659+ /* Check for a periodic interrupt. */
660+ if ((ctrlb & RTC_CTRL_B_PIE) &&
661+ (ctrlc & RTC_CTRL_C_PF)) {
662+ events |= RTC_PF;
663+ num_irqs++;
664+ }
665+
666+ /* Check for an alarm interrupt. */
667+ if ((ctrlb & RTC_CTRL_B_AIE) &&
668+ (ctrlc & RTC_CTRL_C_AF)) {
669+ events |= RTC_AF;
670+ num_irqs++;
671+ }
706672
673+ /* Check for an update interrupt. */
674+ if ((ctrlb & RTC_CTRL_B_UIE) &&
675+ (ctrlc & RTC_CTRL_C_UF)) {
676+ events |= RTC_UF;
677+ num_irqs++;
678+ }
679+ } else {
680+ /*
681+ * One of the "extended" interrupts was received that
682+ * is not recognized by the RTC core.
683+ */
684+ ds1685_rtc_extended_irq(rtc, pdev);
685+ }
686+ }
687+ rtc_update_irq(rtc->dev, num_irqs, events);
707688 mutex_unlock(rtc_mutex);
689+
690+ return events ? IRQ_HANDLED : IRQ_NONE;
708691 }
709692 /* ----------------------------------------------------------------------- */
710693
@@ -833,11 +816,15 @@ static int ds1685_nvram_read(void *priv, unsigned int pos, void *val, · 122 unmodified lines
833816 size_t size)
834817 {
835818 struct ds1685_priv *rtc = priv;
819+ struct mutex *rtc_mutex = &rtc->dev->ops_lock;
836820 ssize_t count;
837− unsigned long flags = 0;
838821 u8 *buf = val;
822+ int err;
823+
824+ err = mutex_lock_interruptible(rtc_mutex);
825+ if (err)
826+ return err;
839827
840− spin_lock_irqsave(&rtc->lock, flags);
841828 ds1685_rtc_switch_to_bank0(rtc);
842829
843830 /* Read NVRAM in time and bank0 registers. */
@@ -887,7 +874,7 @@ static int ds1685_nvram_read(void *priv, unsigned int pos, void *val, · 43 unmodified lines
887874 ds1685_rtc_switch_to_bank0(rtc);
888875 }
889876 #endif /* !CONFIG_RTC_DRV_DS1689 */
890− spin_unlock_irqrestore(&rtc->lock, flags);
877+ mutex_unlock(rtc_mutex);
891878
892879 return 0;
893880 }
@@ -896,11 +883,15 @@ static int ds1685_nvram_write(void *priv, unsigned int pos, void *val, · 2 unmodified lines
896883 size_t size)
897884 {
898885 struct ds1685_priv *rtc = priv;
886+ struct mutex *rtc_mutex = &rtc->dev->ops_lock;
899887 ssize_t count;
900− unsigned long flags = 0;
901888 u8 *buf = val;
889+ int err;
890+
891+ err = mutex_lock_interruptible(rtc_mutex);
892+ if (err)
893+ return err;
902894
903− spin_lock_irqsave(&rtc->lock, flags);
904895 ds1685_rtc_switch_to_bank0(rtc);
905896
906897 /* Write NVRAM in time and bank0 registers. */
@@ -950,7 +941,7 @@ static int ds1685_nvram_write(void *priv, unsigned int pos, void *val, · 43 unmodified lines
950941 ds1685_rtc_switch_to_bank0(rtc);
951942 }
952943 #endif /* !CONFIG_RTC_DRV_DS1689 */
953− spin_unlock_irqrestore(&rtc->lock, flags);
944+ mutex_unlock(rtc_mutex);
954945
955946 return 0;
956947 }
@@ -1141,9 +1132,7 @@ ds1685_rtc_probe(struct platform_device *pdev) · 184 unmodified lines
11411132 if (pdata->plat_post_ram_clear)
11421133 rtc->post_ram_clear = pdata->plat_post_ram_clear;
11431134
1144− /* Init the spinlock, workqueue, & set the driver data. */
1145− spin_lock_init(&rtc->lock);
1146− INIT_WORK(&rtc->work, ds1685_rtc_work_queue);
1135+ /* set the driver data. */
11471136 platform_set_drvdata(pdev, rtc);
11481137
11491138 /* Turn the oscillator on if is not already on (DV1 = 1). */
@@ -1299,22 +1288,23 @@ ds1685_rtc_probe(struct platform_device *pdev) · 149 unmodified lines
12991288 */
13001289 if (!pdata->no_irq) {
13011290 ret = platform_get_irq(pdev, 0);
1302− if (ret > 0) {
1303− rtc->irq_num = ret;
1304−
1305− /* Request an IRQ. */
1306− ret = devm_request_irq(&pdev->dev, rtc->irq_num,
1307− ds1685_rtc_irq_handler,
1308− IRQF_SHARED, pdev->name, pdev);
1309−
1310− /* Check to see if something came back. */
1311− if (unlikely(ret)) {
1312− dev_warn(&pdev->dev,
1313− "RTC interrupt not available\n");
1314− rtc->irq_num = 0;
1315− }
1316− } else
1291+ if (ret <= 0)
13171292 return ret;
1293+
1294+ rtc->irq_num = ret;
1295+
1296+ /* Request an IRQ. */
1297+ ret = devm_request_threaded_irq(&pdev->dev, rtc->irq_num,
1298+ NULL, ds1685_rtc_irq_handler,
1299+ IRQF_SHARED | IRQF_ONESHOT,
1300+ pdev->name, pdev);
1301+
1302+ /* Check to see if something came back. */
1303+ if (unlikely(ret)) {
1304+ dev_warn(&pdev->dev,
1305+ "RTC interrupt not available\n");
1306+ rtc->irq_num = 0;
1307+ }
13181308 }
13191309 rtc->no_irq = pdata->no_irq;
13201310
@@ -1361,8 +1351,6 @@ ds1685_rtc_remove(struct platform_device *pdev) · 40 unmodified lines
13611351 (rtc->read(rtc, RTC_EXT_CTRL_4A) &
13621352 ~(RTC_CTRL_4A_RWK_MASK)));
13631353
1364− cancel_work_sync(&rtc->work);
1365−
13661354 return 0;
13671355 }
13681356
include/linux/rtc/ds1685.h+0−2
@@ -48,8 +48,6 @@ struct ds1685_priv { · 47 unmodified lines
4848 u32 regstep;
4949 resource_size_t baseaddr;
5050 size_t size;
51− spinlock_t lock;
52− struct work_struct work;
5351 int irq_num;
5452 bool bcd_mode;
5553 bool no_irq;

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t