sh_dac_audio.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  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 <sound/core.h>
  30. #include <sound/initval.h>
  31. #include <sound/pcm.h>
  32. #include <sound/sh_dac_audio.h>
  33. #include <asm/clock.h>
  34. #include <asm/hd64461.h>
  35. #include <mach/hp6xx.h>
  36. #include <cpu/dac.h>
  37. MODULE_AUTHOR("Rafael Ignacio Zurita <rizurita@yahoo.com>");
  38. MODULE_DESCRIPTION("SuperH DAC audio driver");
  39. MODULE_LICENSE("GPL");
  40. MODULE_SUPPORTED_DEVICE("{{SuperH DAC audio support}}");
  41. /* Module Parameters */
  42. static int index = SNDRV_DEFAULT_IDX1;
  43. static char *id = SNDRV_DEFAULT_STR1;
  44. module_param(index, int, 0444);
  45. MODULE_PARM_DESC(index, "Index value for SuperH DAC audio.");
  46. module_param(id, charp, 0444);
  47. MODULE_PARM_DESC(id, "ID string for SuperH DAC audio.");
  48. /* main struct */
  49. struct snd_sh_dac {
  50. struct snd_card *card;
  51. struct snd_pcm_substream *substream;
  52. struct hrtimer hrtimer;
  53. ktime_t wakeups_per_second;
  54. int rate;
  55. int empty;
  56. char *data_buffer, *buffer_begin, *buffer_end;
  57. int processed; /* bytes proccesed, to compare with period_size */
  58. int buffer_size;
  59. struct dac_audio_pdata *pdata;
  60. };
  61. static void dac_audio_start_timer(struct snd_sh_dac *chip)
  62. {
  63. hrtimer_start(&chip->hrtimer, chip->wakeups_per_second,
  64. HRTIMER_MODE_REL);
  65. }
  66. static void dac_audio_stop_timer(struct snd_sh_dac *chip)
  67. {
  68. hrtimer_cancel(&chip->hrtimer);
  69. }
  70. static void dac_audio_reset(struct snd_sh_dac *chip)
  71. {
  72. dac_audio_stop_timer(chip);
  73. chip->buffer_begin = chip->buffer_end = chip->data_buffer;
  74. chip->processed = 0;
  75. chip->empty = 1;
  76. }
  77. static void dac_audio_set_rate(struct snd_sh_dac *chip)
  78. {
  79. chip->wakeups_per_second = ktime_set(0, 1000000000 / chip->rate);
  80. }
  81. /* PCM INTERFACE */
  82. static struct snd_pcm_hardware snd_sh_dac_pcm_hw = {
  83. .info = (SNDRV_PCM_INFO_MMAP |
  84. SNDRV_PCM_INFO_MMAP_VALID |
  85. SNDRV_PCM_INFO_INTERLEAVED |
  86. SNDRV_PCM_INFO_HALF_DUPLEX),
  87. .formats = SNDRV_PCM_FMTBIT_U8,
  88. .rates = SNDRV_PCM_RATE_8000,
  89. .rate_min = 8000,
  90. .rate_max = 8000,
  91. .channels_min = 1,
  92. .channels_max = 1,
  93. .buffer_bytes_max = (48*1024),
  94. .period_bytes_min = 1,
  95. .period_bytes_max = (48*1024),
  96. .periods_min = 1,
  97. .periods_max = 1024,
  98. };
  99. static int snd_sh_dac_pcm_open(struct snd_pcm_substream *substream)
  100. {
  101. struct snd_sh_dac *chip = snd_pcm_substream_chip(substream);
  102. struct snd_pcm_runtime *runtime = substream->runtime;
  103. runtime->hw = snd_sh_dac_pcm_hw;
  104. chip->substream = substream;
  105. chip->buffer_begin = chip->buffer_end = chip->data_buffer;
  106. chip->processed = 0;
  107. chip->empty = 1;
  108. chip->pdata->start(chip->pdata);
  109. return 0;
  110. }
  111. static int snd_sh_dac_pcm_close(struct snd_pcm_substream *substream)
  112. {
  113. struct snd_sh_dac *chip = snd_pcm_substream_chip(substream);
  114. chip->substream = NULL;
  115. dac_audio_stop_timer(chip);
  116. chip->pdata->stop(chip->pdata);
  117. return 0;
  118. }
  119. static int snd_sh_dac_pcm_hw_params(struct snd_pcm_substream *substream,
  120. struct snd_pcm_hw_params *hw_params)
  121. {
  122. return snd_pcm_lib_malloc_pages(substream,
  123. params_buffer_bytes(hw_params));
  124. }
  125. static int snd_sh_dac_pcm_hw_free(struct snd_pcm_substream *substream)
  126. {
  127. return snd_pcm_lib_free_pages(substream);
  128. }
  129. static int snd_sh_dac_pcm_prepare(struct snd_pcm_substream *substream)
  130. {
  131. struct snd_sh_dac *chip = snd_pcm_substream_chip(substream);
  132. struct snd_pcm_runtime *runtime = chip->substream->runtime;
  133. chip->buffer_size = runtime->buffer_size;
  134. memset(chip->data_buffer, 0, chip->pdata->buffer_size);
  135. return 0;
  136. }
  137. static int snd_sh_dac_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
  138. {
  139. struct snd_sh_dac *chip = snd_pcm_substream_chip(substream);
  140. switch (cmd) {
  141. case SNDRV_PCM_TRIGGER_START:
  142. dac_audio_start_timer(chip);
  143. break;
  144. case SNDRV_PCM_TRIGGER_STOP:
  145. chip->buffer_begin = chip->buffer_end = chip->data_buffer;
  146. chip->processed = 0;
  147. chip->empty = 1;
  148. dac_audio_stop_timer(chip);
  149. break;
  150. default:
  151. return -EINVAL;
  152. }
  153. return 0;
  154. }
  155. static int snd_sh_dac_pcm_copy(struct snd_pcm_substream *substream, int channel,
  156. snd_pcm_uframes_t pos, void __user *src, snd_pcm_uframes_t count)
  157. {
  158. /* channel is not used (interleaved data) */
  159. struct snd_sh_dac *chip = snd_pcm_substream_chip(substream);
  160. struct snd_pcm_runtime *runtime = substream->runtime;
  161. ssize_t b_count = frames_to_bytes(runtime , count);
  162. ssize_t b_pos = frames_to_bytes(runtime , pos);
  163. if (count < 0)
  164. return -EINVAL;
  165. if (!count)
  166. return 0;
  167. memcpy_toio(chip->data_buffer + b_pos, src, b_count);
  168. chip->buffer_end = chip->data_buffer + b_pos + b_count;
  169. if (chip->empty) {
  170. chip->empty = 0;
  171. dac_audio_start_timer(chip);
  172. }
  173. return 0;
  174. }
  175. static int snd_sh_dac_pcm_silence(struct snd_pcm_substream *substream,
  176. int channel, snd_pcm_uframes_t pos,
  177. snd_pcm_uframes_t count)
  178. {
  179. /* channel is not used (interleaved data) */
  180. struct snd_sh_dac *chip = snd_pcm_substream_chip(substream);
  181. struct snd_pcm_runtime *runtime = substream->runtime;
  182. ssize_t b_count = frames_to_bytes(runtime , count);
  183. ssize_t b_pos = frames_to_bytes(runtime , pos);
  184. if (count < 0)
  185. return -EINVAL;
  186. if (!count)
  187. return 0;
  188. memset_io(chip->data_buffer + b_pos, 0, b_count);
  189. chip->buffer_end = chip->data_buffer + b_pos + b_count;
  190. if (chip->empty) {
  191. chip->empty = 0;
  192. dac_audio_start_timer(chip);
  193. }
  194. return 0;
  195. }
  196. static
  197. snd_pcm_uframes_t snd_sh_dac_pcm_pointer(struct snd_pcm_substream *substream)
  198. {
  199. struct snd_sh_dac *chip = snd_pcm_substream_chip(substream);
  200. int pointer = chip->buffer_begin - chip->data_buffer;
  201. return pointer;
  202. }
  203. /* pcm ops */
  204. static struct snd_pcm_ops snd_sh_dac_pcm_ops = {
  205. .open = snd_sh_dac_pcm_open,
  206. .close = snd_sh_dac_pcm_close,
  207. .ioctl = snd_pcm_lib_ioctl,
  208. .hw_params = snd_sh_dac_pcm_hw_params,
  209. .hw_free = snd_sh_dac_pcm_hw_free,
  210. .prepare = snd_sh_dac_pcm_prepare,
  211. .trigger = snd_sh_dac_pcm_trigger,
  212. .pointer = snd_sh_dac_pcm_pointer,
  213. .copy = snd_sh_dac_pcm_copy,
  214. .silence = snd_sh_dac_pcm_silence,
  215. .mmap = snd_pcm_lib_mmap_iomem,
  216. };
  217. static int __devinit snd_sh_dac_pcm(struct snd_sh_dac *chip, int device)
  218. {
  219. int err;
  220. struct snd_pcm *pcm;
  221. /* device should be always 0 for us */
  222. err = snd_pcm_new(chip->card, "SH_DAC PCM", device, 1, 0, &pcm);
  223. if (err < 0)
  224. return err;
  225. pcm->private_data = chip;
  226. strcpy(pcm->name, "SH_DAC PCM");
  227. snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_sh_dac_pcm_ops);
  228. /* buffer size=48K */
  229. snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_CONTINUOUS,
  230. snd_dma_continuous_data(GFP_KERNEL),
  231. 48 * 1024,
  232. 48 * 1024);
  233. return 0;
  234. }
  235. /* END OF PCM INTERFACE */
  236. /* driver .remove -- destructor */
  237. static int snd_sh_dac_remove(struct platform_device *devptr)
  238. {
  239. snd_card_free(platform_get_drvdata(devptr));
  240. platform_set_drvdata(devptr, NULL);
  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 __devinit 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 __devinit 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_create(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 driver = {
  349. .probe = snd_sh_dac_probe,
  350. .remove = snd_sh_dac_remove,
  351. .driver = {
  352. .name = "dac_audio",
  353. },
  354. };
  355. static int __init sh_dac_init(void)
  356. {
  357. return platform_driver_register(&driver);
  358. }
  359. static void __exit sh_dac_exit(void)
  360. {
  361. platform_driver_unregister(&driver);
  362. }
  363. module_init(sh_dac_init);
  364. module_exit(sh_dac_exit);