i2sbus-core.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. /*
  2. * i2sbus driver
  3. *
  4. * Copyright 2006 Johannes Berg <johannes@sipsolutions.net>
  5. *
  6. * GPL v2, can be found in COPYING.
  7. */
  8. #include <linux/module.h>
  9. #include <asm/macio.h>
  10. #include <asm/dbdma.h>
  11. #include <linux/pci.h>
  12. #include <linux/interrupt.h>
  13. #include <sound/driver.h>
  14. #include <sound/core.h>
  15. #include <linux/dma-mapping.h>
  16. #include "../soundbus.h"
  17. #include "i2sbus.h"
  18. MODULE_LICENSE("GPL");
  19. MODULE_AUTHOR("Johannes Berg <johannes@sipsolutions.net>");
  20. MODULE_DESCRIPTION("Apple Soundbus: I2S support");
  21. /* for auto-loading, declare that we handle this weird
  22. * string that macio puts into the relevant device */
  23. MODULE_ALIAS("of:Ni2sTi2sC");
  24. static struct of_device_id i2sbus_match[] = {
  25. { .name = "i2s" },
  26. { }
  27. };
  28. static int alloc_dbdma_descriptor_ring(struct i2sbus_dev *i2sdev,
  29. struct dbdma_command_mem *r,
  30. int numcmds)
  31. {
  32. /* one more for rounding */
  33. r->size = (numcmds+1) * sizeof(struct dbdma_cmd);
  34. /* We use the PCI APIs for now until the generic one gets fixed
  35. * enough or until we get some macio-specific versions
  36. */
  37. r->space = dma_alloc_coherent(
  38. &macio_get_pci_dev(i2sdev->macio)->dev,
  39. r->size,
  40. &r->bus_addr,
  41. GFP_KERNEL);
  42. if (!r->space) return -ENOMEM;
  43. memset(r->space, 0, r->size);
  44. r->cmds = (void*)DBDMA_ALIGN(r->space);
  45. r->bus_cmd_start = r->bus_addr +
  46. (dma_addr_t)((char*)r->cmds - (char*)r->space);
  47. return 0;
  48. }
  49. static void free_dbdma_descriptor_ring(struct i2sbus_dev *i2sdev,
  50. struct dbdma_command_mem *r)
  51. {
  52. if (!r->space) return;
  53. dma_free_coherent(&macio_get_pci_dev(i2sdev->macio)->dev,
  54. r->size, r->space, r->bus_addr);
  55. }
  56. static void i2sbus_release_dev(struct device *dev)
  57. {
  58. struct i2sbus_dev *i2sdev;
  59. int i;
  60. i2sdev = container_of(dev, struct i2sbus_dev, sound.ofdev.dev);
  61. if (i2sdev->intfregs) iounmap(i2sdev->intfregs);
  62. if (i2sdev->out.dbdma) iounmap(i2sdev->out.dbdma);
  63. if (i2sdev->in.dbdma) iounmap(i2sdev->in.dbdma);
  64. for (i=0;i<3;i++)
  65. if (i2sdev->allocated_resource[i])
  66. release_and_free_resource(i2sdev->allocated_resource[i]);
  67. free_dbdma_descriptor_ring(i2sdev, &i2sdev->out.dbdma_ring);
  68. free_dbdma_descriptor_ring(i2sdev, &i2sdev->in.dbdma_ring);
  69. for (i=0;i<3;i++)
  70. free_irq(i2sdev->interrupts[i], i2sdev);
  71. i2sbus_control_remove_dev(i2sdev->control, i2sdev);
  72. mutex_destroy(&i2sdev->lock);
  73. kfree(i2sdev);
  74. }
  75. static irqreturn_t i2sbus_bus_intr(int irq, void *devid, struct pt_regs *regs)
  76. {
  77. struct i2sbus_dev *dev = devid;
  78. u32 intreg;
  79. spin_lock(&dev->low_lock);
  80. intreg = in_le32(&dev->intfregs->intr_ctl);
  81. /* acknowledge interrupt reasons */
  82. out_le32(&dev->intfregs->intr_ctl, intreg);
  83. spin_unlock(&dev->low_lock);
  84. return IRQ_HANDLED;
  85. }
  86. static int force;
  87. module_param(force, int, 0444);
  88. MODULE_PARM_DESC(force, "Force loading i2sbus even when"
  89. " no layout-id property is present");
  90. /* FIXME: look at device node refcounting */
  91. static int i2sbus_add_dev(struct macio_dev *macio,
  92. struct i2sbus_control *control,
  93. struct device_node *np)
  94. {
  95. struct i2sbus_dev *dev;
  96. struct device_node *child = NULL, *sound = NULL;
  97. int i;
  98. static const char *rnames[] = { "i2sbus: %s (control)",
  99. "i2sbus: %s (tx)",
  100. "i2sbus: %s (rx)" };
  101. static irqreturn_t (*ints[])(int irq, void *devid,
  102. struct pt_regs *regs) = {
  103. i2sbus_bus_intr,
  104. i2sbus_tx_intr,
  105. i2sbus_rx_intr
  106. };
  107. if (strlen(np->name) != 5)
  108. return 0;
  109. if (strncmp(np->name, "i2s-", 4))
  110. return 0;
  111. dev = kzalloc(sizeof(struct i2sbus_dev), GFP_KERNEL);
  112. if (!dev)
  113. return 0;
  114. i = 0;
  115. while ((child = of_get_next_child(np, child))) {
  116. if (strcmp(child->name, "sound") == 0) {
  117. i++;
  118. sound = child;
  119. }
  120. }
  121. if (i == 1) {
  122. u32 *layout_id;
  123. layout_id = (u32*) get_property(sound, "layout-id", NULL);
  124. if (layout_id) {
  125. snprintf(dev->sound.modalias, 32,
  126. "sound-layout-%d", *layout_id);
  127. force = 1;
  128. }
  129. }
  130. /* for the time being, until we can handle non-layout-id
  131. * things in some fabric, refuse to attach if there is no
  132. * layout-id property or we haven't been forced to attach.
  133. * When there are two i2s busses and only one has a layout-id,
  134. * then this depends on the order, but that isn't important
  135. * either as the second one in that case is just a modem. */
  136. if (!force) {
  137. kfree(dev);
  138. return -ENODEV;
  139. }
  140. mutex_init(&dev->lock);
  141. spin_lock_init(&dev->low_lock);
  142. dev->sound.ofdev.node = np;
  143. dev->sound.ofdev.dma_mask = macio->ofdev.dma_mask;
  144. dev->sound.ofdev.dev.dma_mask = &dev->sound.ofdev.dma_mask;
  145. dev->sound.ofdev.dev.parent = &macio->ofdev.dev;
  146. dev->sound.ofdev.dev.release = i2sbus_release_dev;
  147. dev->sound.attach_codec = i2sbus_attach_codec;
  148. dev->sound.detach_codec = i2sbus_detach_codec;
  149. dev->sound.pcmid = -1;
  150. dev->macio = macio;
  151. dev->control = control;
  152. dev->bus_number = np->name[4] - 'a';
  153. INIT_LIST_HEAD(&dev->sound.codec_list);
  154. for (i=0;i<3;i++) {
  155. dev->interrupts[i] = -1;
  156. snprintf(dev->rnames[i], sizeof(dev->rnames[i]), rnames[i], np->name);
  157. }
  158. for (i=0;i<3;i++) {
  159. int irq = irq_of_parse_and_map(np, i);
  160. if (request_irq(irq, ints[i], 0, dev->rnames[i], dev))
  161. goto err;
  162. dev->interrupts[i] = irq;
  163. }
  164. for (i=0;i<3;i++) {
  165. if (of_address_to_resource(np, i, &dev->resources[i]))
  166. goto err;
  167. /* if only we could use our resource dev->resources[i]...
  168. * but request_resource doesn't know about parents and
  169. * contained resources... */
  170. dev->allocated_resource[i] =
  171. request_mem_region(dev->resources[i].start,
  172. dev->resources[i].end -
  173. dev->resources[i].start + 1,
  174. dev->rnames[i]);
  175. if (!dev->allocated_resource[i]) {
  176. printk(KERN_ERR "i2sbus: failed to claim resource %d!\n", i);
  177. goto err;
  178. }
  179. }
  180. /* should do sanity checking here about length of them */
  181. dev->intfregs = ioremap(dev->resources[0].start,
  182. dev->resources[0].end-dev->resources[0].start+1);
  183. dev->out.dbdma = ioremap(dev->resources[1].start,
  184. dev->resources[1].end-dev->resources[1].start+1);
  185. dev->in.dbdma = ioremap(dev->resources[2].start,
  186. dev->resources[2].end-dev->resources[2].start+1);
  187. if (!dev->intfregs || !dev->out.dbdma || !dev->in.dbdma)
  188. goto err;
  189. if (alloc_dbdma_descriptor_ring(dev, &dev->out.dbdma_ring,
  190. MAX_DBDMA_COMMANDS))
  191. goto err;
  192. if (alloc_dbdma_descriptor_ring(dev, &dev->in.dbdma_ring,
  193. MAX_DBDMA_COMMANDS))
  194. goto err;
  195. if (i2sbus_control_add_dev(dev->control, dev)) {
  196. printk(KERN_ERR "i2sbus: control layer didn't like bus\n");
  197. goto err;
  198. }
  199. if (soundbus_add_one(&dev->sound)) {
  200. printk(KERN_DEBUG "i2sbus: device registration error!\n");
  201. goto err;
  202. }
  203. /* enable this cell */
  204. i2sbus_control_cell(dev->control, dev, 1);
  205. i2sbus_control_enable(dev->control, dev);
  206. i2sbus_control_clock(dev->control, dev, 1);
  207. return 1;
  208. err:
  209. for (i=0;i<3;i++)
  210. if (dev->interrupts[i] != -1)
  211. free_irq(dev->interrupts[i], dev);
  212. free_dbdma_descriptor_ring(dev, &dev->out.dbdma_ring);
  213. free_dbdma_descriptor_ring(dev, &dev->in.dbdma_ring);
  214. if (dev->intfregs) iounmap(dev->intfregs);
  215. if (dev->out.dbdma) iounmap(dev->out.dbdma);
  216. if (dev->in.dbdma) iounmap(dev->in.dbdma);
  217. for (i=0;i<3;i++)
  218. if (dev->allocated_resource[i])
  219. release_and_free_resource(dev->allocated_resource[i]);
  220. mutex_destroy(&dev->lock);
  221. kfree(dev);
  222. return 0;
  223. }
  224. static int i2sbus_probe(struct macio_dev* dev, const struct of_device_id *match)
  225. {
  226. struct device_node *np = NULL;
  227. int got = 0, err;
  228. struct i2sbus_control *control = NULL;
  229. err = i2sbus_control_init(dev, &control);
  230. if (err)
  231. return err;
  232. if (!control) {
  233. printk(KERN_ERR "i2sbus_control_init API breakage\n");
  234. return -ENODEV;
  235. }
  236. while ((np = of_get_next_child(dev->ofdev.node, np))) {
  237. if (device_is_compatible(np, "i2sbus") ||
  238. device_is_compatible(np, "i2s-modem")) {
  239. got += i2sbus_add_dev(dev, control, np);
  240. }
  241. }
  242. if (!got) {
  243. /* found none, clean up */
  244. i2sbus_control_destroy(control);
  245. return -ENODEV;
  246. }
  247. dev->ofdev.dev.driver_data = control;
  248. return 0;
  249. }
  250. static int i2sbus_remove(struct macio_dev* dev)
  251. {
  252. struct i2sbus_control *control = dev->ofdev.dev.driver_data;
  253. struct i2sbus_dev *i2sdev, *tmp;
  254. list_for_each_entry_safe(i2sdev, tmp, &control->list, item)
  255. soundbus_remove_one(&i2sdev->sound);
  256. return 0;
  257. }
  258. #ifdef CONFIG_PM
  259. static int i2sbus_suspend(struct macio_dev* dev, pm_message_t state)
  260. {
  261. struct i2sbus_control *control = dev->ofdev.dev.driver_data;
  262. struct codec_info_item *cii;
  263. struct i2sbus_dev* i2sdev;
  264. int err, ret = 0;
  265. list_for_each_entry(i2sdev, &control->list, item) {
  266. /* Notify Alsa */
  267. if (i2sdev->sound.pcm) {
  268. /* Suspend PCM streams */
  269. snd_pcm_suspend_all(i2sdev->sound.pcm);
  270. /* Probably useless as we handle
  271. * power transitions ourselves */
  272. snd_power_change_state(i2sdev->sound.pcm->card,
  273. SNDRV_CTL_POWER_D3hot);
  274. }
  275. /* Notify codecs */
  276. list_for_each_entry(cii, &i2sdev->sound.codec_list, list) {
  277. err = 0;
  278. if (cii->codec->suspend)
  279. err = cii->codec->suspend(cii, state);
  280. if (err)
  281. ret = err;
  282. }
  283. }
  284. return ret;
  285. }
  286. static int i2sbus_resume(struct macio_dev* dev)
  287. {
  288. struct i2sbus_control *control = dev->ofdev.dev.driver_data;
  289. struct codec_info_item *cii;
  290. struct i2sbus_dev* i2sdev;
  291. int err, ret = 0;
  292. list_for_each_entry(i2sdev, &control->list, item) {
  293. /* Notify codecs so they can re-initialize */
  294. list_for_each_entry(cii, &i2sdev->sound.codec_list, list) {
  295. err = 0;
  296. if (cii->codec->resume)
  297. err = cii->codec->resume(cii);
  298. if (err)
  299. ret = err;
  300. }
  301. /* Notify Alsa */
  302. if (i2sdev->sound.pcm) {
  303. /* Same comment as above, probably useless */
  304. snd_power_change_state(i2sdev->sound.pcm->card,
  305. SNDRV_CTL_POWER_D0);
  306. }
  307. }
  308. return ret;
  309. }
  310. #endif /* CONFIG_PM */
  311. static int i2sbus_shutdown(struct macio_dev* dev)
  312. {
  313. return 0;
  314. }
  315. static struct macio_driver i2sbus_drv = {
  316. .name = "soundbus-i2s",
  317. .owner = THIS_MODULE,
  318. .match_table = i2sbus_match,
  319. .probe = i2sbus_probe,
  320. .remove = i2sbus_remove,
  321. #ifdef CONFIG_PM
  322. .suspend = i2sbus_suspend,
  323. .resume = i2sbus_resume,
  324. #endif
  325. .shutdown = i2sbus_shutdown,
  326. };
  327. static int __init soundbus_i2sbus_init(void)
  328. {
  329. return macio_register_driver(&i2sbus_drv);
  330. }
  331. static void __exit soundbus_i2sbus_exit(void)
  332. {
  333. macio_unregister_driver(&i2sbus_drv);
  334. }
  335. module_init(soundbus_i2sbus_init);
  336. module_exit(soundbus_i2sbus_exit);