^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * PCM timer handling on ctxfi
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <linux/math64.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/moduleparam.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <sound/core.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <sound/pcm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include "ctatc.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include "cthardware.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include "cttimer.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) static bool use_system_timer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) MODULE_PARM_DESC(use_system_timer, "Force to use system-timer");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) module_param(use_system_timer, bool, 0444);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) struct ct_timer_ops {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) void (*init)(struct ct_timer_instance *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) void (*prepare)(struct ct_timer_instance *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) void (*start)(struct ct_timer_instance *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) void (*stop)(struct ct_timer_instance *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) void (*free_instance)(struct ct_timer_instance *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) void (*interrupt)(struct ct_timer *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) void (*free_global)(struct ct_timer *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) /* timer instance -- assigned to each PCM stream */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) struct ct_timer_instance {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) spinlock_t lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) struct ct_timer *timer_base;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) struct ct_atc_pcm *apcm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) struct snd_pcm_substream *substream;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) struct timer_list timer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) struct list_head instance_list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) struct list_head running_list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) unsigned int position;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) unsigned int frag_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) unsigned int running:1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) unsigned int need_update:1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) /* timer instance manager */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) struct ct_timer {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) spinlock_t lock; /* global timer lock (for xfitimer) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) spinlock_t list_lock; /* lock for instance list */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) struct ct_atc *atc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) const struct ct_timer_ops *ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) struct list_head instance_head;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) struct list_head running_head;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) unsigned int wc; /* current wallclock */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) unsigned int irq_handling:1; /* in IRQ handling */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) unsigned int reprogram:1; /* need to reprogram the internval */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) unsigned int running:1; /* global timer running */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) * system-timer-based updates
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) static void ct_systimer_callback(struct timer_list *t)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) struct ct_timer_instance *ti = from_timer(ti, t, timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) struct snd_pcm_substream *substream = ti->substream;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) struct snd_pcm_runtime *runtime = substream->runtime;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) struct ct_atc_pcm *apcm = ti->apcm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) unsigned int period_size = runtime->period_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) unsigned int buffer_size = runtime->buffer_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) unsigned int position, dist, interval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) position = substream->ops->pointer(substream);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) dist = (position + buffer_size - ti->position) % buffer_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) if (dist >= period_size ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) position / period_size != ti->position / period_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) apcm->interrupt(apcm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) ti->position = position;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) /* Add extra HZ*5/1000 to avoid overrun issue when recording
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) * at 8kHz in 8-bit format or at 88kHz in 24-bit format. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) interval = ((period_size - (position % period_size))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) * HZ + (runtime->rate - 1)) / runtime->rate + HZ * 5 / 1000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) spin_lock_irqsave(&ti->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) if (ti->running)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) mod_timer(&ti->timer, jiffies + interval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) spin_unlock_irqrestore(&ti->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) static void ct_systimer_init(struct ct_timer_instance *ti)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) timer_setup(&ti->timer, ct_systimer_callback, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) static void ct_systimer_start(struct ct_timer_instance *ti)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) struct snd_pcm_runtime *runtime = ti->substream->runtime;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) spin_lock_irqsave(&ti->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) ti->running = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) mod_timer(&ti->timer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) jiffies + (runtime->period_size * HZ +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) (runtime->rate - 1)) / runtime->rate);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) spin_unlock_irqrestore(&ti->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) static void ct_systimer_stop(struct ct_timer_instance *ti)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) spin_lock_irqsave(&ti->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) ti->running = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) del_timer(&ti->timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) spin_unlock_irqrestore(&ti->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) static void ct_systimer_prepare(struct ct_timer_instance *ti)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) ct_systimer_stop(ti);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) try_to_del_timer_sync(&ti->timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) #define ct_systimer_free ct_systimer_prepare
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) static const struct ct_timer_ops ct_systimer_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) .init = ct_systimer_init,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) .free_instance = ct_systimer_free,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) .prepare = ct_systimer_prepare,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) .start = ct_systimer_start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) .stop = ct_systimer_stop,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) * Handling multiple streams using a global emu20k1 timer irq
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) #define CT_TIMER_FREQ 48000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) #define MIN_TICKS 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) #define MAX_TICKS ((1 << 13) - 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) static void ct_xfitimer_irq_rearm(struct ct_timer *atimer, int ticks)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) struct hw *hw = atimer->atc->hw;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) if (ticks > MAX_TICKS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) ticks = MAX_TICKS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) hw->set_timer_tick(hw, ticks);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) if (!atimer->running)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) hw->set_timer_irq(hw, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) atimer->running = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) static void ct_xfitimer_irq_stop(struct ct_timer *atimer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) if (atimer->running) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) struct hw *hw = atimer->atc->hw;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) hw->set_timer_irq(hw, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) hw->set_timer_tick(hw, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) atimer->running = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) static inline unsigned int ct_xfitimer_get_wc(struct ct_timer *atimer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) struct hw *hw = atimer->atc->hw;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) return hw->get_wc(hw);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) * reprogram the timer interval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) * checks the running instance list and determines the next timer interval.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) * also updates the each stream position, returns the number of streams
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) * to call snd_pcm_period_elapsed() appropriately
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) * call this inside the lock and irq disabled
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) static int ct_xfitimer_reprogram(struct ct_timer *atimer, int can_update)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) struct ct_timer_instance *ti;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) unsigned int min_intr = (unsigned int)-1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) int updates = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) unsigned int wc, diff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) if (list_empty(&atimer->running_head)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) ct_xfitimer_irq_stop(atimer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) atimer->reprogram = 0; /* clear flag */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) wc = ct_xfitimer_get_wc(atimer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) diff = wc - atimer->wc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) atimer->wc = wc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) list_for_each_entry(ti, &atimer->running_head, running_list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) if (ti->frag_count > diff)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) ti->frag_count -= diff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) unsigned int pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) unsigned int period_size, rate;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) period_size = ti->substream->runtime->period_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) rate = ti->substream->runtime->rate;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) pos = ti->substream->ops->pointer(ti->substream);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) if (pos / period_size != ti->position / period_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) ti->need_update = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) ti->position = pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) updates++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) pos %= period_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) pos = period_size - pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) ti->frag_count = div_u64((u64)pos * CT_TIMER_FREQ +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) rate - 1, rate);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) if (ti->need_update && !can_update)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) min_intr = 0; /* pending to the next irq */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) if (ti->frag_count < min_intr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) min_intr = ti->frag_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) if (min_intr < MIN_TICKS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) min_intr = MIN_TICKS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) ct_xfitimer_irq_rearm(atimer, min_intr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) atimer->reprogram = 0; /* clear flag */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) return updates;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) /* look through the instance list and call period_elapsed if needed */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) static void ct_xfitimer_check_period(struct ct_timer *atimer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) struct ct_timer_instance *ti;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) spin_lock_irqsave(&atimer->list_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) list_for_each_entry(ti, &atimer->instance_head, instance_list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) if (ti->running && ti->need_update) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) ti->need_update = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) ti->apcm->interrupt(ti->apcm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) spin_unlock_irqrestore(&atimer->list_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) /* Handle timer-interrupt */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) static void ct_xfitimer_callback(struct ct_timer *atimer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) int update;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) spin_lock_irqsave(&atimer->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) atimer->irq_handling = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) update = ct_xfitimer_reprogram(atimer, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) spin_unlock(&atimer->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) if (update)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) ct_xfitimer_check_period(atimer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) spin_lock(&atimer->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) } while (atimer->reprogram);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) atimer->irq_handling = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) spin_unlock_irqrestore(&atimer->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) static void ct_xfitimer_prepare(struct ct_timer_instance *ti)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) ti->frag_count = ti->substream->runtime->period_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) ti->running = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) ti->need_update = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) /* start/stop the timer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) static void ct_xfitimer_update(struct ct_timer *atimer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) spin_lock_irqsave(&atimer->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) if (atimer->irq_handling) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) /* reached from IRQ handler; let it handle later */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) atimer->reprogram = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) spin_unlock_irqrestore(&atimer->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) ct_xfitimer_irq_stop(atimer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) ct_xfitimer_reprogram(atimer, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) spin_unlock_irqrestore(&atimer->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) static void ct_xfitimer_start(struct ct_timer_instance *ti)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) struct ct_timer *atimer = ti->timer_base;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) spin_lock_irqsave(&atimer->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) if (list_empty(&ti->running_list))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) atimer->wc = ct_xfitimer_get_wc(atimer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) ti->running = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) ti->need_update = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) list_add(&ti->running_list, &atimer->running_head);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) spin_unlock_irqrestore(&atimer->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) ct_xfitimer_update(atimer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) static void ct_xfitimer_stop(struct ct_timer_instance *ti)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) struct ct_timer *atimer = ti->timer_base;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) spin_lock_irqsave(&atimer->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) list_del_init(&ti->running_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) ti->running = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) spin_unlock_irqrestore(&atimer->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) ct_xfitimer_update(atimer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) static void ct_xfitimer_free_global(struct ct_timer *atimer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) ct_xfitimer_irq_stop(atimer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) static const struct ct_timer_ops ct_xfitimer_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) .prepare = ct_xfitimer_prepare,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) .start = ct_xfitimer_start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) .stop = ct_xfitimer_stop,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) .interrupt = ct_xfitimer_callback,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) .free_global = ct_xfitimer_free_global,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) * timer instance
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) struct ct_timer_instance *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) ct_timer_instance_new(struct ct_timer *atimer, struct ct_atc_pcm *apcm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) struct ct_timer_instance *ti;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) ti = kzalloc(sizeof(*ti), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) if (!ti)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) spin_lock_init(&ti->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) INIT_LIST_HEAD(&ti->instance_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) INIT_LIST_HEAD(&ti->running_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) ti->timer_base = atimer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) ti->apcm = apcm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) ti->substream = apcm->substream;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) if (atimer->ops->init)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) atimer->ops->init(ti);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) spin_lock_irq(&atimer->list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) list_add(&ti->instance_list, &atimer->instance_head);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) spin_unlock_irq(&atimer->list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) return ti;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) void ct_timer_prepare(struct ct_timer_instance *ti)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) if (ti->timer_base->ops->prepare)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) ti->timer_base->ops->prepare(ti);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) ti->position = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) ti->running = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) void ct_timer_start(struct ct_timer_instance *ti)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) struct ct_timer *atimer = ti->timer_base;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) atimer->ops->start(ti);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) void ct_timer_stop(struct ct_timer_instance *ti)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) struct ct_timer *atimer = ti->timer_base;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) atimer->ops->stop(ti);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) void ct_timer_instance_free(struct ct_timer_instance *ti)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) struct ct_timer *atimer = ti->timer_base;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) atimer->ops->stop(ti); /* to be sure */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) if (atimer->ops->free_instance)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) atimer->ops->free_instance(ti);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) spin_lock_irq(&atimer->list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) list_del(&ti->instance_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) spin_unlock_irq(&atimer->list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) kfree(ti);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) * timer manager
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) static void ct_timer_interrupt(void *data, unsigned int status)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) struct ct_timer *timer = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) /* Interval timer interrupt */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) if ((status & IT_INT) && timer->ops->interrupt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) timer->ops->interrupt(timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) struct ct_timer *ct_timer_new(struct ct_atc *atc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) struct ct_timer *atimer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) struct hw *hw;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) atimer = kzalloc(sizeof(*atimer), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) if (!atimer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) spin_lock_init(&atimer->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) spin_lock_init(&atimer->list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) INIT_LIST_HEAD(&atimer->instance_head);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) INIT_LIST_HEAD(&atimer->running_head);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) atimer->atc = atc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) hw = atc->hw;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) if (!use_system_timer && hw->set_timer_irq) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) dev_info(atc->card->dev, "Use xfi-native timer\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) atimer->ops = &ct_xfitimer_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) hw->irq_callback_data = atimer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) hw->irq_callback = ct_timer_interrupt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) dev_info(atc->card->dev, "Use system timer\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) atimer->ops = &ct_systimer_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) return atimer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) void ct_timer_free(struct ct_timer *atimer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) struct hw *hw = atimer->atc->hw;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) hw->irq_callback = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) if (atimer->ops->free_global)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) atimer->ops->free_global(atimer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) kfree(atimer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439)