sh_dac_audio.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. /*
  2. * sh_dac_audio.c - SuperH DAC audio driver for ALSA
  3. *
  4. * Copyright (c) 2009 by Rafael Ignacio Zurita <rizurita@yahoo.com>
  5. *
  6. *
  7. * Based on sh_dac_audio.c (Copyright (C) 2004, 2005 by Andriy Skulysh)
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  22. *
  23. */
  24. #include <linux/hrtimer.h>
  25. #include <linux/interrupt.h>
  26. #include <linux/io.h>
  27. #include <linux/platform_device.h>
  28. #include <linux/slab.h>
  29. #include <linux/module.h>
  30. #include <sound/core.h>
  31. #include <sound/initval.h>
  32. #include <sound/pcm.h>
  33. #include <sound/sh_dac_audio.h>
  34. #include <asm/clock.h>
  35. #include <asm/hd64461.h>
  36. #include <mach/hp6xx.h>
  37. #include <cpu/dac.h>
  38. MODULE_AUTHOR("Rafael Ignacio Zurita <rizurita@yahoo.com>");
  39. MODULE_DESCRIPTION("SuperH DAC audio driver");
  40. MODULE_LICENSE("GPL");
  41. MODULE_SUPPORTED_DEVICE("{{SuperH DAC audio support}}");
  42. /* Module Parameters */
  43. static int index = SNDRV_DEFAULT_IDX1;
  44. static char *id = SNDRV_DEFAULT_STR1;
  45. module_param(index, int, 0444);
  46. MODULE_PARM_DESC(index, "Index value for SuperH DAC audio.");
  47. module_param(id, charp, 0444);
  48. MODULE_PARM_DESC(id, "ID string for SuperH DAC audio.");
  49. /* main struct */
  50. struct snd_sh_dac {
  51. struct snd_card *card;
  52. struct snd_pcm_substream *substream;
  53. struct hrtimer hrtimer;
  54. ktime_t wakeups_per_second;
  55. int rate;
  56. int empty;
  57. char *data_buffer, *buffer_begin, *buffer_end;
  58. int processed; /* bytes proccesed, to compare with period_size */
  59. int buffer_size;
  60. struct dac_audio_pdata *pdata;
  61. };
  62. static void dac_audio_start_timer(struct snd_sh_dac *chip)
  63. {
  64. hrtimer_start(&chip->hrtimer, chip->wakeups_per_second,
  65. HRTIMER_MODE_REL);
  66. }
  67. static void dac_audio_stop_timer(struct snd_sh_dac *chip)
  68. {
  69. hrtimer_cancel(&chip->hrtimer);
  70. }
  71. static void dac_audio_reset(struct snd_sh_dac *chip)
  72. {
  73. dac_audio_stop_timer(chip);
  74. chip->buffer_begin = chip->buffer_end = chip->data_buffer;
  75. chip->processed = 0;
  76. chip->empty = 1;
  77. }
  78. static void dac_audio_set_rate(struct snd_sh_dac *chip)
  79. {
  80. chip->wakeups_per_second = ktime_set(0, 1000000000 / chip->rate);
  81. }
  82. /* PCM INTERFACE */
  83. static struct snd_pcm_hardware snd_sh_dac_pcm_hw = {
  84. .info = (SNDRV_PCM_INFO_MMAP |
  85. SNDRV_PCM_INFO_MMAP_VALID |
  86. SNDRV_PCM_INFO_INTERLEAVED |
  87. SNDRV_PCM_INFO_HALF_DUPLEX),
  88. .formats = SNDRV_PCM_FMTBIT_U8,
  89. .rates = SNDRV_PCM_RATE_8000,
  90. .rate_min = 8000,
  91. .rate_max = 8000,
  92. .channels_min = 1,
  93. .channels_max = 1,
  94. .buffer_bytes_max = (48*1024),
  95. .period_bytes_min = 1,
  96. .period_bytes_max = (48*1024),
  97. .periods_min = 1,
  98. .periods_max = 1024,
  99. };
  100. static int snd_sh_dac_pcm_open(struct snd_pcm_substream *substream)
  101. {
  102. struct snd_sh_dac *chip = snd_pcm_substream_chip(substream);
  103. struct snd_pcm_runtime *runtime = substream->runtime;
  104. runtime->hw = snd_sh_dac_pcm_hw;
  105. chip->substream = substream;
  106. chip->buffer_begin = chip->buffer_end = chip->data_buffer;
  107. chip->processed = 0;
  108. chip->empty = 1;
  109. chip->pdata->start(chip->pdata);
  110. return 0;
  111. }
  112. static int snd_sh_dac_pcm_close(struct snd_pcm_substream *substream)
  113. {
  114. struct snd_sh_dac *chip = snd_pcm_substream_chip(substream);
  115. chip->substream = NULL;
  116. dac_audio_stop_timer(chip);
  117. chip->pdata->stop(chip->pdata);
  118. return 0;
  119. }
  120. static int snd_sh_dac_pcm_hw_params(struct snd_pcm_substream *substream,
  121. struct snd_pcm_hw_params *hw_params)
  122. {
  123. return snd_pcm_lib_malloc_pages(substream,
  124. params_buffer_bytes(hw_params));
  125. }
  126. static int snd_sh_dac_pcm_hw_free(struct snd_pcm_substream *substream)
  127. {
  128. return snd_pcm_lib_free_pages(substream);
  129. }
  130. static int snd_sh_dac_pcm_prepare(struct snd_pcm_substream *substream)
  131. {
  132. struct snd_sh_dac *chip = snd_pcm_substream_chip(substream);
  133. struct snd_pcm_runtime *runtime = chip->substream->runtime;
  134. chip->buffer_size = runtime->buffer_size;
  135. memset(chip->data_buffer, 0, chip->pdata->buffer_size);
  136. return 0;
  137. }
  138. static int snd_sh_dac_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
  139. {
  140. struct snd_sh_dac *chip = snd_pcm_substream_chip(substream);
  141. switch (cmd) {
  142. case SNDRV_PCM_TRIGGER_START:
  143. dac_audio_start_timer(chip);
  144. break;
  145. case SNDRV_PCM_TRIGGER_STOP:
  146. chip->buffer_begin = chip->buffer_end = chip->data_buffer;
  147. chip->processed = 0;
  148. chip->empty = 1;
  149. dac_audio_stop_timer(chip);
  150. break;
  151. default:
  152. return -EINVAL;
  153. }
  154. return 0;
  155. }
  156. static int snd_sh_dac_pcm_copy(struct snd_pcm_substream *substream, int channel,
  157. snd_pcm_uframes_t pos, void __user *src, snd_pcm_uframes_t count)
  158. {
  159. /* channel is not used (interleaved data) */
  160. struct snd_sh_dac *chip = snd_pcm_substream_chip(substream);
  161. struct snd_pcm_runtime *runtime = substream->runtime;
  162. ssize_t b_count = frames_to_bytes(runtime , count);
  163. ssize_t b_pos = frames_to_bytes(runtime , pos);
  164. if (count < 0)
  165. return -EINVAL;
  166. if (!count)
  167. return 0;
  168. memcpy_toio(chip->data_buffer + b_pos, src, b_count);
  169. chip->buffer_end = chip->data_buffer + b_pos + b_count;
  170. if (chip->empty) {
  171. chip->empty = 0;
  172. dac_audio_start_timer(chip);
  173. }
  174. return 0;
  175. }
  176. static int snd_sh_dac_pcm_silence(struct snd_pcm_substream *substream,
  177. int channel, snd_pcm_uframes_t pos,
  178. snd_pcm_uframes_t count)
  179. {
  180. /* channel is not used (interleaved data) */
  181. struct snd_sh_dac *chip = snd_pcm_substream_chip(substream);
  182. struct snd_pcm_runtime *runtime = substream->runtime;
  183. ssize_t b_count = frames_to_bytes(runtime , count);
  184. ssize_t b_pos = frames_to_bytes(runtime , pos);
  185. if (count < 0)
  186. return -EINVAL;
  187. if (!count)
  188. return 0;
  189. memset_io(chip->data_buffer + b_pos, 0, b_count);
  190. chip->buffer_end = chip->data_buffer + b_pos + b_count;
  191. if (chip->empty) {
  192. chip->empty = 0;
  193. dac_audio_start_timer(chip);
  194. }
  195. return 0;
  196. }
  197. static
  198. snd_pcm_uframes_t snd_sh_dac_pcm_pointer(struct snd_pcm_substream *substream)
  199. {
  200. struct snd_sh_dac *chip = snd_pcm_substream_chip(substream);
  201. int pointer = chip->buffer_begin - chip->data_buffer;
  202. return pointer;
  203. }
  204. /* pcm ops */
  205. static struct snd_pcm_ops snd_sh_dac_pcm_ops = {
  206. .open = snd_sh_dac_pcm_open,
  207. .close = snd_sh_dac_pcm_close,
  208. .ioctl = snd_pcm_lib_ioctl,
  209. .hw_params = snd_sh_dac_pcm_hw_params,
  210. .hw_free = snd_sh_dac_pcm_hw_free,
  211. .prepare = snd_sh_dac_pcm_prepare,
  212. .trigger = snd_sh_dac_pcm_trigger,
  213. .pointer = snd_sh_dac_pcm_pointer,
  214. .copy = snd_sh_dac_pcm_copy,
  215. .silence = snd_sh_dac_pcm_silence,
  216. .mmap = snd_pcm_lib_mmap_iomem,
  217. };
  218. static int snd_sh_dac_pcm(struct snd_sh_dac *chip, int device)
  219. {
  220. int err;
  221. struct snd_pcm *pcm;
  222. /* device should be always 0 for us */
  223. err = snd_pcm_new(chip->card, "SH_DAC PCM", device, 1, 0, &pcm);
  224. if (err < 0)
  225. return err;
  226. pcm->private_data = chip;
  227. strcpy(pcm->name, "SH_DAC PCM");
  228. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_sh_dac_pcm_ops);
  229. /* buffer size=48K */
  230. snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_CONTINUOUS,
  231. snd_dma_continuous_data(GFP_KERNEL),
  232. 48 * 1024,
  233. 48 * 1024);
  234. return 0;
  235. }
  236. /* END OF PCM INTERFACE */
  237. /* driver .remove -- destructor */
  238. static int snd_sh_dac_remove(struct platform_device *devptr)
  239. {
  240. snd_card_free(platform_get_drvdata(devptr));
  241. return 0;
  242. }
  243. /* free -- it has been defined by create */
  244. static int snd_sh_dac_free(struct snd_sh_dac *chip)
  245. {
  246. /* release the data */
  247. kfree(chip->data_buffer);
  248. kfree(chip);
  249. return 0;
  250. }
  251. static int snd_sh_dac_dev_free(struct snd_device *device)
  252. {
  253. struct snd_sh_dac *chip = device->device_data;
  254. return snd_sh_dac_free(chip);
  255. }
  256. static enum hrtimer_restart sh_dac_audio_timer(struct hrtimer *handle)
  257. {
  258. struct snd_sh_dac *chip = container_of(handle, struct snd_sh_dac,
  259. hrtimer);
  260. struct snd_pcm_runtime *runtime = chip->substream->runtime;
  261. ssize_t b_ps = frames_to_bytes(runtime, runtime->period_size);
  262. if (!chip->empty) {
  263. sh_dac_output(*chip->buffer_begin, chip->pdata->channel);
  264. chip->buffer_begin++;
  265. chip->processed++;
  266. if (chip->processed >= b_ps) {
  267. chip->processed -= b_ps;
  268. snd_pcm_period_elapsed(chip->substream);
  269. }
  270. if (chip->buffer_begin == (chip->data_buffer +
  271. chip->buffer_size - 1))
  272. chip->buffer_begin = chip->data_buffer;
  273. if (chip->buffer_begin == chip->buffer_end)
  274. chip->empty = 1;
  275. }
  276. if (!chip->empty)
  277. hrtimer_start(&chip->hrtimer, chip->wakeups_per_second,
  278. HRTIMER_MODE_REL);
  279. return HRTIMER_NORESTART;
  280. }
  281. /* create -- chip-specific constructor for the cards components */
  282. static int snd_sh_dac_create(struct snd_card *card,
  283. struct platform_device *devptr,
  284. struct snd_sh_dac **rchip)
  285. {
  286. struct snd_sh_dac *chip;
  287. int err;
  288. static struct snd_device_ops ops = {
  289. .dev_free = snd_sh_dac_dev_free,
  290. };
  291. *rchip = NULL;
  292. chip = kzalloc(sizeof(*chip), GFP_KERNEL);
  293. if (chip == NULL)
  294. return -ENOMEM;
  295. chip->card = card;
  296. hrtimer_init(&chip->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  297. chip->hrtimer.function = sh_dac_audio_timer;
  298. dac_audio_reset(chip);
  299. chip->rate = 8000;
  300. dac_audio_set_rate(chip);
  301. chip->pdata = devptr->dev.platform_data;
  302. chip->data_buffer = kmalloc(chip->pdata->buffer_size, GFP_KERNEL);
  303. if (chip->data_buffer == NULL) {
  304. kfree(chip);
  305. return -ENOMEM;
  306. }
  307. err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops);
  308. if (err < 0) {
  309. snd_sh_dac_free(chip);
  310. return err;
  311. }
  312. *rchip = chip;
  313. return 0;
  314. }
  315. /* driver .probe -- constructor */
  316. static int snd_sh_dac_probe(struct platform_device *devptr)
  317. {
  318. struct snd_sh_dac *chip;
  319. struct snd_card *card;
  320. int err;
  321. err = snd_card_new(&devptr->dev, index, id, THIS_MODULE, 0, &card);
  322. if (err < 0) {
  323. snd_printk(KERN_ERR "cannot allocate the card\n");
  324. return err;
  325. }
  326. err = snd_sh_dac_create(card, devptr, &chip);
  327. if (err < 0)
  328. goto probe_error;
  329. err = snd_sh_dac_pcm(chip, 0);
  330. if (err < 0)
  331. goto probe_error;
  332. strcpy(card->driver, "snd_sh_dac");
  333. strcpy(card->shortname, "SuperH DAC audio driver");
  334. printk(KERN_INFO "%s %s", card->longname, card->shortname);
  335. err = snd_card_register(card);
  336. if (err < 0)
  337. goto probe_error;
  338. snd_printk("ALSA driver for SuperH DAC audio");
  339. platform_set_drvdata(devptr, card);
  340. return 0;
  341. probe_error:
  342. snd_card_free(card);
  343. return err;
  344. }
  345. /*
  346. * "driver" definition
  347. */
  348. static struct platform_driver sh_dac_driver = {
  349. .probe = snd_sh_dac_probe,
  350. .remove = snd_sh_dac_remove,
  351. .driver = {
  352. .name = "dac_audio",
  353. },
  354. };
  355. module_platform_driver(sh_dac_driver);