cttimer.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  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, S_IRUGO);
  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. 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(unsigned long data)
  59. {
  60. struct ct_timer_instance *ti = (struct ct_timer_instance *)data;
  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. setup_timer(&ti->timer, ct_systimer_callback,
  87. (unsigned long)ti);
  88. }
  89. static void ct_systimer_start(struct ct_timer_instance *ti)
  90. {
  91. struct snd_pcm_runtime *runtime = ti->substream->runtime;
  92. unsigned long flags;
  93. spin_lock_irqsave(&ti->lock, flags);
  94. ti->running = 1;
  95. mod_timer(&ti->timer,
  96. jiffies + (runtime->period_size * HZ +
  97. (runtime->rate - 1)) / runtime->rate);
  98. spin_unlock_irqrestore(&ti->lock, flags);
  99. }
  100. static void ct_systimer_stop(struct ct_timer_instance *ti)
  101. {
  102. unsigned long flags;
  103. spin_lock_irqsave(&ti->lock, flags);
  104. ti->running = 0;
  105. del_timer(&ti->timer);
  106. spin_unlock_irqrestore(&ti->lock, flags);
  107. }
  108. static void ct_systimer_prepare(struct ct_timer_instance *ti)
  109. {
  110. ct_systimer_stop(ti);
  111. try_to_del_timer_sync(&ti->timer);
  112. }
  113. #define ct_systimer_free ct_systimer_prepare
  114. static struct ct_timer_ops ct_systimer_ops = {
  115. .init = ct_systimer_init,
  116. .free_instance = ct_systimer_free,
  117. .prepare = ct_systimer_prepare,
  118. .start = ct_systimer_start,
  119. .stop = ct_systimer_stop,
  120. };
  121. /*
  122. * Handling multiple streams using a global emu20k1 timer irq
  123. */
  124. #define CT_TIMER_FREQ 48000
  125. #define MIN_TICKS 1
  126. #define MAX_TICKS ((1 << 13) - 1)
  127. static void ct_xfitimer_irq_rearm(struct ct_timer *atimer, int ticks)
  128. {
  129. struct hw *hw = atimer->atc->hw;
  130. if (ticks > MAX_TICKS)
  131. ticks = MAX_TICKS;
  132. hw->set_timer_tick(hw, ticks);
  133. if (!atimer->running)
  134. hw->set_timer_irq(hw, 1);
  135. atimer->running = 1;
  136. }
  137. static void ct_xfitimer_irq_stop(struct ct_timer *atimer)
  138. {
  139. if (atimer->running) {
  140. struct hw *hw = atimer->atc->hw;
  141. hw->set_timer_irq(hw, 0);
  142. hw->set_timer_tick(hw, 0);
  143. atimer->running = 0;
  144. }
  145. }
  146. static inline unsigned int ct_xfitimer_get_wc(struct ct_timer *atimer)
  147. {
  148. struct hw *hw = atimer->atc->hw;
  149. return hw->get_wc(hw);
  150. }
  151. /*
  152. * reprogram the timer interval;
  153. * checks the running instance list and determines the next timer interval.
  154. * also updates the each stream position, returns the number of streams
  155. * to call snd_pcm_period_elapsed() appropriately
  156. *
  157. * call this inside the lock and irq disabled
  158. */
  159. static int ct_xfitimer_reprogram(struct ct_timer *atimer, int can_update)
  160. {
  161. struct ct_timer_instance *ti;
  162. unsigned int min_intr = (unsigned int)-1;
  163. int updates = 0;
  164. unsigned int wc, diff;
  165. if (list_empty(&atimer->running_head)) {
  166. ct_xfitimer_irq_stop(atimer);
  167. atimer->reprogram = 0; /* clear flag */
  168. return 0;
  169. }
  170. wc = ct_xfitimer_get_wc(atimer);
  171. diff = wc - atimer->wc;
  172. atimer->wc = wc;
  173. list_for_each_entry(ti, &atimer->running_head, running_list) {
  174. if (ti->frag_count > diff)
  175. ti->frag_count -= diff;
  176. else {
  177. unsigned int pos;
  178. unsigned int period_size, rate;
  179. period_size = ti->substream->runtime->period_size;
  180. rate = ti->substream->runtime->rate;
  181. pos = ti->substream->ops->pointer(ti->substream);
  182. if (pos / period_size != ti->position / period_size) {
  183. ti->need_update = 1;
  184. ti->position = pos;
  185. updates++;
  186. }
  187. pos %= period_size;
  188. pos = period_size - pos;
  189. ti->frag_count = div_u64((u64)pos * CT_TIMER_FREQ +
  190. rate - 1, rate);
  191. }
  192. if (ti->need_update && !can_update)
  193. min_intr = 0; /* pending to the next irq */
  194. if (ti->frag_count < min_intr)
  195. min_intr = ti->frag_count;
  196. }
  197. if (min_intr < MIN_TICKS)
  198. min_intr = MIN_TICKS;
  199. ct_xfitimer_irq_rearm(atimer, min_intr);
  200. atimer->reprogram = 0; /* clear flag */
  201. return updates;
  202. }
  203. /* look through the instance list and call period_elapsed if needed */
  204. static void ct_xfitimer_check_period(struct ct_timer *atimer)
  205. {
  206. struct ct_timer_instance *ti;
  207. unsigned long flags;
  208. spin_lock_irqsave(&atimer->list_lock, flags);
  209. list_for_each_entry(ti, &atimer->instance_head, instance_list) {
  210. if (ti->running && ti->need_update) {
  211. ti->need_update = 0;
  212. ti->apcm->interrupt(ti->apcm);
  213. }
  214. }
  215. spin_unlock_irqrestore(&atimer->list_lock, flags);
  216. }
  217. /* Handle timer-interrupt */
  218. static void ct_xfitimer_callback(struct ct_timer *atimer)
  219. {
  220. int update;
  221. unsigned long flags;
  222. spin_lock_irqsave(&atimer->lock, flags);
  223. atimer->irq_handling = 1;
  224. do {
  225. update = ct_xfitimer_reprogram(atimer, 1);
  226. spin_unlock(&atimer->lock);
  227. if (update)
  228. ct_xfitimer_check_period(atimer);
  229. spin_lock(&atimer->lock);
  230. } while (atimer->reprogram);
  231. atimer->irq_handling = 0;
  232. spin_unlock_irqrestore(&atimer->lock, flags);
  233. }
  234. static void ct_xfitimer_prepare(struct ct_timer_instance *ti)
  235. {
  236. ti->frag_count = ti->substream->runtime->period_size;
  237. ti->running = 0;
  238. ti->need_update = 0;
  239. }
  240. /* start/stop the timer */
  241. static void ct_xfitimer_update(struct ct_timer *atimer)
  242. {
  243. unsigned long flags;
  244. spin_lock_irqsave(&atimer->lock, flags);
  245. if (atimer->irq_handling) {
  246. /* reached from IRQ handler; let it handle later */
  247. atimer->reprogram = 1;
  248. spin_unlock_irqrestore(&atimer->lock, flags);
  249. return;
  250. }
  251. ct_xfitimer_irq_stop(atimer);
  252. ct_xfitimer_reprogram(atimer, 0);
  253. spin_unlock_irqrestore(&atimer->lock, flags);
  254. }
  255. static void ct_xfitimer_start(struct ct_timer_instance *ti)
  256. {
  257. struct ct_timer *atimer = ti->timer_base;
  258. unsigned long flags;
  259. spin_lock_irqsave(&atimer->lock, flags);
  260. if (list_empty(&ti->running_list))
  261. atimer->wc = ct_xfitimer_get_wc(atimer);
  262. ti->running = 1;
  263. ti->need_update = 0;
  264. list_add(&ti->running_list, &atimer->running_head);
  265. spin_unlock_irqrestore(&atimer->lock, flags);
  266. ct_xfitimer_update(atimer);
  267. }
  268. static void ct_xfitimer_stop(struct ct_timer_instance *ti)
  269. {
  270. struct ct_timer *atimer = ti->timer_base;
  271. unsigned long flags;
  272. spin_lock_irqsave(&atimer->lock, flags);
  273. list_del_init(&ti->running_list);
  274. ti->running = 0;
  275. spin_unlock_irqrestore(&atimer->lock, flags);
  276. ct_xfitimer_update(atimer);
  277. }
  278. static void ct_xfitimer_free_global(struct ct_timer *atimer)
  279. {
  280. ct_xfitimer_irq_stop(atimer);
  281. }
  282. static struct ct_timer_ops ct_xfitimer_ops = {
  283. .prepare = ct_xfitimer_prepare,
  284. .start = ct_xfitimer_start,
  285. .stop = ct_xfitimer_stop,
  286. .interrupt = ct_xfitimer_callback,
  287. .free_global = ct_xfitimer_free_global,
  288. };
  289. /*
  290. * timer instance
  291. */
  292. struct ct_timer_instance *
  293. ct_timer_instance_new(struct ct_timer *atimer, struct ct_atc_pcm *apcm)
  294. {
  295. struct ct_timer_instance *ti;
  296. ti = kzalloc(sizeof(*ti), GFP_KERNEL);
  297. if (!ti)
  298. return NULL;
  299. spin_lock_init(&ti->lock);
  300. INIT_LIST_HEAD(&ti->instance_list);
  301. INIT_LIST_HEAD(&ti->running_list);
  302. ti->timer_base = atimer;
  303. ti->apcm = apcm;
  304. ti->substream = apcm->substream;
  305. if (atimer->ops->init)
  306. atimer->ops->init(ti);
  307. spin_lock_irq(&atimer->list_lock);
  308. list_add(&ti->instance_list, &atimer->instance_head);
  309. spin_unlock_irq(&atimer->list_lock);
  310. return ti;
  311. }
  312. void ct_timer_prepare(struct ct_timer_instance *ti)
  313. {
  314. if (ti->timer_base->ops->prepare)
  315. ti->timer_base->ops->prepare(ti);
  316. ti->position = 0;
  317. ti->running = 0;
  318. }
  319. void ct_timer_start(struct ct_timer_instance *ti)
  320. {
  321. struct ct_timer *atimer = ti->timer_base;
  322. atimer->ops->start(ti);
  323. }
  324. void ct_timer_stop(struct ct_timer_instance *ti)
  325. {
  326. struct ct_timer *atimer = ti->timer_base;
  327. atimer->ops->stop(ti);
  328. }
  329. void ct_timer_instance_free(struct ct_timer_instance *ti)
  330. {
  331. struct ct_timer *atimer = ti->timer_base;
  332. atimer->ops->stop(ti); /* to be sure */
  333. if (atimer->ops->free_instance)
  334. atimer->ops->free_instance(ti);
  335. spin_lock_irq(&atimer->list_lock);
  336. list_del(&ti->instance_list);
  337. spin_unlock_irq(&atimer->list_lock);
  338. kfree(ti);
  339. }
  340. /*
  341. * timer manager
  342. */
  343. static void ct_timer_interrupt(void *data, unsigned int status)
  344. {
  345. struct ct_timer *timer = data;
  346. /* Interval timer interrupt */
  347. if ((status & IT_INT) && timer->ops->interrupt)
  348. timer->ops->interrupt(timer);
  349. }
  350. struct ct_timer *ct_timer_new(struct ct_atc *atc)
  351. {
  352. struct ct_timer *atimer;
  353. struct hw *hw;
  354. atimer = kzalloc(sizeof(*atimer), GFP_KERNEL);
  355. if (!atimer)
  356. return NULL;
  357. spin_lock_init(&atimer->lock);
  358. spin_lock_init(&atimer->list_lock);
  359. INIT_LIST_HEAD(&atimer->instance_head);
  360. INIT_LIST_HEAD(&atimer->running_head);
  361. atimer->atc = atc;
  362. hw = atc->hw;
  363. if (!use_system_timer && hw->set_timer_irq) {
  364. dev_info(atc->card->dev, "Use xfi-native timer\n");
  365. atimer->ops = &ct_xfitimer_ops;
  366. hw->irq_callback_data = atimer;
  367. hw->irq_callback = ct_timer_interrupt;
  368. } else {
  369. dev_info(atc->card->dev, "Use system timer\n");
  370. atimer->ops = &ct_systimer_ops;
  371. }
  372. return atimer;
  373. }
  374. void ct_timer_free(struct ct_timer *atimer)
  375. {
  376. struct hw *hw = atimer->atc->hw;
  377. hw->irq_callback = NULL;
  378. if (atimer->ops->free_global)
  379. atimer->ops->free_global(atimer);
  380. kfree(atimer);
  381. }