i2sbus-core.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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. if (macio_irq_count(macio) != 3)
  112. return 0;
  113. dev = kzalloc(sizeof(struct i2sbus_dev), GFP_KERNEL);
  114. if (!dev)
  115. return 0;
  116. i = 0;
  117. while ((child = of_get_next_child(np, child))) {
  118. if (strcmp(child->name, "sound") == 0) {
  119. i++;
  120. sound = child;
  121. }
  122. }
  123. if (i == 1) {
  124. u32 *layout_id;
  125. layout_id = (u32*) get_property(sound, "layout-id", NULL);
  126. if (layout_id) {
  127. snprintf(dev->sound.modalias, 32,
  128. "sound-layout-%d", *layout_id);
  129. force = 1;
  130. }
  131. }
  132. /* for the time being, until we can handle non-layout-id
  133. * things in some fabric, refuse to attach if there is no
  134. * layout-id property or we haven't been forced to attach.
  135. * When there are two i2s busses and only one has a layout-id,
  136. * then this depends on the order, but that isn't important
  137. * either as the second one in that case is just a modem. */
  138. if (!force) {
  139. kfree(dev);
  140. return -ENODEV;
  141. }
  142. mutex_init(&dev->lock);
  143. spin_lock_init(&dev->low_lock);
  144. dev->sound.ofdev.node = np;
  145. dev->sound.ofdev.dma_mask = macio->ofdev.dma_mask;
  146. dev->sound.ofdev.dev.dma_mask = &dev->sound.ofdev.dma_mask;
  147. dev->sound.ofdev.dev.parent = &macio->ofdev.dev;
  148. dev->sound.ofdev.dev.release = i2sbus_release_dev;
  149. dev->sound.attach_codec = i2sbus_attach_codec;
  150. dev->sound.detach_codec = i2sbus_detach_codec;
  151. dev->sound.pcmid = -1;
  152. dev->macio = macio;
  153. dev->control = control;
  154. dev->bus_number = np->name[4] - 'a';
  155. INIT_LIST_HEAD(&dev->sound.codec_list);
  156. for (i=0;i<3;i++) {
  157. dev->interrupts[i] = -1;
  158. snprintf(dev->rnames[i], sizeof(dev->rnames[i]), rnames[i], np->name);
  159. }
  160. for (i=0;i<3;i++) {
  161. if (request_irq(macio_irq(macio, i), ints[i], 0,
  162. dev->rnames[i], dev))
  163. goto err;
  164. dev->interrupts[i] = macio_irq(macio, i);
  165. }
  166. for (i=0;i<3;i++) {
  167. if (of_address_to_resource(np, i, &dev->resources[i]))
  168. goto err;
  169. /* if only we could use our resource dev->resources[i]...
  170. * but request_resource doesn't know about parents and
  171. * contained resources... */
  172. dev->allocated_resource[i] =
  173. request_mem_region(dev->resources[i].start,
  174. dev->resources[i].end -
  175. dev->resources[i].start + 1,
  176. dev->rnames[i]);
  177. if (!dev->allocated_resource[i]) {
  178. printk(KERN_ERR "i2sbus: failed to claim resource %d!\n", i);
  179. goto err;
  180. }
  181. }
  182. /* should do sanity checking here about length of them */
  183. dev->intfregs = ioremap(dev->resources[0].start,
  184. dev->resources[0].end-dev->resources[0].start+1);
  185. dev->out.dbdma = ioremap(dev->resources[1].start,
  186. dev->resources[1].end-dev->resources[1].start+1);
  187. dev->in.dbdma = ioremap(dev->resources[2].start,
  188. dev->resources[2].end-dev->resources[2].start+1);
  189. if (!dev->intfregs || !dev->out.dbdma || !dev->in.dbdma)
  190. goto err;
  191. if (alloc_dbdma_descriptor_ring(dev, &dev->out.dbdma_ring,
  192. MAX_DBDMA_COMMANDS))
  193. goto err;
  194. if (alloc_dbdma_descriptor_ring(dev, &dev->in.dbdma_ring,
  195. MAX_DBDMA_COMMANDS))
  196. goto err;
  197. if (i2sbus_control_add_dev(dev->control, dev)) {
  198. printk(KERN_ERR "i2sbus: control layer didn't like bus\n");
  199. goto err;
  200. }
  201. if (soundbus_add_one(&dev->sound)) {
  202. printk(KERN_DEBUG "i2sbus: device registration error!\n");
  203. goto err;
  204. }
  205. /* enable this cell */
  206. i2sbus_control_cell(dev->control, dev, 1);
  207. i2sbus_control_enable(dev->control, dev);
  208. i2sbus_control_clock(dev->control, dev, 1);
  209. return 1;
  210. err:
  211. for (i=0;i<3;i++)
  212. if (dev->interrupts[i] != -1)
  213. free_irq(dev->interrupts[i], dev);
  214. free_dbdma_descriptor_ring(dev, &dev->out.dbdma_ring);
  215. free_dbdma_descriptor_ring(dev, &dev->in.dbdma_ring);
  216. if (dev->intfregs) iounmap(dev->intfregs);
  217. if (dev->out.dbdma) iounmap(dev->out.dbdma);
  218. if (dev->in.dbdma) iounmap(dev->in.dbdma);
  219. for (i=0;i<3;i++)
  220. if (dev->allocated_resource[i])
  221. release_and_free_resource(dev->allocated_resource[i]);
  222. mutex_destroy(&dev->lock);
  223. kfree(dev);
  224. return 0;
  225. }
  226. static int i2sbus_probe(struct macio_dev* dev, const struct of_device_id *match)
  227. {
  228. struct device_node *np = NULL;
  229. int got = 0, err;
  230. struct i2sbus_control *control = NULL;
  231. err = i2sbus_control_init(dev, &control);
  232. if (err)
  233. return err;
  234. if (!control) {
  235. printk(KERN_ERR "i2sbus_control_init API breakage\n");
  236. return -ENODEV;
  237. }
  238. while ((np = of_get_next_child(dev->ofdev.node, np))) {
  239. if (device_is_compatible(np, "i2sbus") ||
  240. device_is_compatible(np, "i2s-modem")) {
  241. got += i2sbus_add_dev(dev, control, np);
  242. }
  243. }
  244. if (!got) {
  245. /* found none, clean up */
  246. i2sbus_control_destroy(control);
  247. return -ENODEV;
  248. }
  249. dev->ofdev.dev.driver_data = control;
  250. return 0;
  251. }
  252. static int i2sbus_remove(struct macio_dev* dev)
  253. {
  254. struct i2sbus_control *control = dev->ofdev.dev.driver_data;
  255. struct i2sbus_dev *i2sdev, *tmp;
  256. list_for_each_entry_safe(i2sdev, tmp, &control->list, item)
  257. soundbus_remove_one(&i2sdev->sound);
  258. return 0;
  259. }
  260. #ifdef CONFIG_PM
  261. static int i2sbus_suspend(struct macio_dev* dev, pm_message_t state)
  262. {
  263. struct i2sbus_control *control = dev->ofdev.dev.driver_data;
  264. struct codec_info_item *cii;
  265. struct i2sbus_dev* i2sdev;
  266. int err, ret = 0;
  267. list_for_each_entry(i2sdev, &control->list, item) {
  268. /* Notify Alsa */
  269. if (i2sdev->sound.pcm) {
  270. /* Suspend PCM streams */
  271. snd_pcm_suspend_all(i2sdev->sound.pcm);
  272. /* Probably useless as we handle
  273. * power transitions ourselves */
  274. snd_power_change_state(i2sdev->sound.pcm->card,
  275. SNDRV_CTL_POWER_D3hot);
  276. }
  277. /* Notify codecs */
  278. list_for_each_entry(cii, &i2sdev->sound.codec_list, list) {
  279. err = 0;
  280. if (cii->codec->suspend)
  281. err = cii->codec->suspend(cii, state);
  282. if (err)
  283. ret = err;
  284. }
  285. }
  286. return ret;
  287. }
  288. static int i2sbus_resume(struct macio_dev* dev)
  289. {
  290. struct i2sbus_control *control = dev->ofdev.dev.driver_data;
  291. struct codec_info_item *cii;
  292. struct i2sbus_dev* i2sdev;
  293. int err, ret = 0;
  294. list_for_each_entry(i2sdev, &control->list, item) {
  295. /* Notify codecs so they can re-initialize */
  296. list_for_each_entry(cii, &i2sdev->sound.codec_list, list) {
  297. err = 0;
  298. if (cii->codec->resume)
  299. err = cii->codec->resume(cii);
  300. if (err)
  301. ret = err;
  302. }
  303. /* Notify Alsa */
  304. if (i2sdev->sound.pcm) {
  305. /* Same comment as above, probably useless */
  306. snd_power_change_state(i2sdev->sound.pcm->card,
  307. SNDRV_CTL_POWER_D0);
  308. }
  309. }
  310. return ret;
  311. }
  312. #endif /* CONFIG_PM */
  313. static int i2sbus_shutdown(struct macio_dev* dev)
  314. {
  315. return 0;
  316. }
  317. static struct macio_driver i2sbus_drv = {
  318. .name = "soundbus-i2s",
  319. .owner = THIS_MODULE,
  320. .match_table = i2sbus_match,
  321. .probe = i2sbus_probe,
  322. .remove = i2sbus_remove,
  323. #ifdef CONFIG_PM
  324. .suspend = i2sbus_suspend,
  325. .resume = i2sbus_resume,
  326. #endif
  327. .shutdown = i2sbus_shutdown,
  328. };
  329. static int __init soundbus_i2sbus_init(void)
  330. {
  331. return macio_register_driver(&i2sbus_drv);
  332. }
  333. static void __exit soundbus_i2sbus_exit(void)
  334. {
  335. macio_unregister_driver(&i2sbus_drv);
  336. }
  337. module_init(soundbus_i2sbus_init);
  338. module_exit(soundbus_i2sbus_exit);