cttimer.c 11 KB

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