core.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. /*
  2. * i2sbus driver
  3. *
  4. * Copyright 2006-2008 Johannes Berg <johannes@sipsolutions.net>
  5. *
  6. * GPL v2, can be found in COPYING.
  7. */
  8. #include <linux/module.h>
  9. #include <linux/pci.h>
  10. #include <linux/interrupt.h>
  11. #include <linux/dma-mapping.h>
  12. #include <sound/core.h>
  13. #include <asm/macio.h>
  14. #include <asm/dbdma.h>
  15. #include "../soundbus.h"
  16. #include "i2sbus.h"
  17. MODULE_LICENSE("GPL");
  18. MODULE_AUTHOR("Johannes Berg <johannes@sipsolutions.net>");
  19. MODULE_DESCRIPTION("Apple Soundbus: I2S support");
  20. static int force;
  21. module_param(force, int, 0444);
  22. MODULE_PARM_DESC(force, "Force loading i2sbus even when"
  23. " no layout-id property is present");
  24. static struct of_device_id i2sbus_match[] = {
  25. { .name = "i2s" },
  26. { }
  27. };
  28. MODULE_DEVICE_TABLE(of, i2sbus_match);
  29. static int alloc_dbdma_descriptor_ring(struct i2sbus_dev *i2sdev,
  30. struct dbdma_command_mem *r,
  31. int numcmds)
  32. {
  33. /* one more for rounding, one for branch back, one for stop command */
  34. r->size = (numcmds + 3) * sizeof(struct dbdma_cmd);
  35. /* We use the PCI APIs for now until the generic one gets fixed
  36. * enough or until we get some macio-specific versions
  37. */
  38. r->space = dma_alloc_coherent(
  39. &macio_get_pci_dev(i2sdev->macio)->dev,
  40. r->size,
  41. &r->bus_addr,
  42. GFP_KERNEL);
  43. if (!r->space) return -ENOMEM;
  44. memset(r->space, 0, r->size);
  45. r->cmds = (void*)DBDMA_ALIGN(r->space);
  46. r->bus_cmd_start = r->bus_addr +
  47. (dma_addr_t)((char*)r->cmds - (char*)r->space);
  48. return 0;
  49. }
  50. static void free_dbdma_descriptor_ring(struct i2sbus_dev *i2sdev,
  51. struct dbdma_command_mem *r)
  52. {
  53. if (!r->space) return;
  54. dma_free_coherent(&macio_get_pci_dev(i2sdev->macio)->dev,
  55. r->size, r->space, r->bus_addr);
  56. }
  57. static void i2sbus_release_dev(struct device *dev)
  58. {
  59. struct i2sbus_dev *i2sdev;
  60. int i;
  61. i2sdev = container_of(dev, struct i2sbus_dev, sound.ofdev.dev);
  62. if (i2sdev->intfregs) iounmap(i2sdev->intfregs);
  63. if (i2sdev->out.dbdma) iounmap(i2sdev->out.dbdma);
  64. if (i2sdev->in.dbdma) iounmap(i2sdev->in.dbdma);
  65. for (i = aoa_resource_i2smmio; i <= aoa_resource_rxdbdma; i++)
  66. if (i2sdev->allocated_resource[i])
  67. release_and_free_resource(i2sdev->allocated_resource[i]);
  68. free_dbdma_descriptor_ring(i2sdev, &i2sdev->out.dbdma_ring);
  69. free_dbdma_descriptor_ring(i2sdev, &i2sdev->in.dbdma_ring);
  70. for (i = aoa_resource_i2smmio; i <= aoa_resource_rxdbdma; i++)
  71. free_irq(i2sdev->interrupts[i], i2sdev);
  72. i2sbus_control_remove_dev(i2sdev->control, i2sdev);
  73. mutex_destroy(&i2sdev->lock);
  74. kfree(i2sdev);
  75. }
  76. static irqreturn_t i2sbus_bus_intr(int irq, void *devid)
  77. {
  78. struct i2sbus_dev *dev = devid;
  79. u32 intreg;
  80. spin_lock(&dev->low_lock);
  81. intreg = in_le32(&dev->intfregs->intr_ctl);
  82. /* acknowledge interrupt reasons */
  83. out_le32(&dev->intfregs->intr_ctl, intreg);
  84. spin_unlock(&dev->low_lock);
  85. return IRQ_HANDLED;
  86. }
  87. /*
  88. * XXX FIXME: We test the layout_id's here to get the proper way of
  89. * mapping in various registers, thanks to bugs in Apple device-trees.
  90. * We could instead key off the machine model and the name of the i2s
  91. * node (i2s-a). This we'll do when we move it all to macio_asic.c
  92. * and have that export items for each sub-node too.
  93. */
  94. static int i2sbus_get_and_fixup_rsrc(struct device_node *np, int index,
  95. int layout, struct resource *res)
  96. {
  97. struct device_node *parent;
  98. int pindex, rc = -ENXIO;
  99. const u32 *reg;
  100. /* Machines with layout 76 and 36 (K2 based) have a weird device
  101. * tree what we need to special case.
  102. * Normal machines just fetch the resource from the i2s-X node.
  103. * Darwin further divides normal machines into old and new layouts
  104. * with a subtely different code path but that doesn't seem necessary
  105. * in practice, they just bloated it. In addition, even on our K2
  106. * case the i2s-modem node, if we ever want to handle it, uses the
  107. * normal layout
  108. */
  109. if (layout != 76 && layout != 36)
  110. return of_address_to_resource(np, index, res);
  111. parent = of_get_parent(np);
  112. pindex = (index == aoa_resource_i2smmio) ? 0 : 1;
  113. rc = of_address_to_resource(parent, pindex, res);
  114. if (rc)
  115. goto bail;
  116. reg = of_get_property(np, "reg", NULL);
  117. if (reg == NULL) {
  118. rc = -ENXIO;
  119. goto bail;
  120. }
  121. res->start += reg[index * 2];
  122. res->end = res->start + reg[index * 2 + 1] - 1;
  123. bail:
  124. of_node_put(parent);
  125. return rc;
  126. }
  127. /* FIXME: look at device node refcounting */
  128. static int i2sbus_add_dev(struct macio_dev *macio,
  129. struct i2sbus_control *control,
  130. struct device_node *np)
  131. {
  132. struct i2sbus_dev *dev;
  133. struct device_node *child = NULL, *sound = NULL;
  134. struct resource *r;
  135. int i, layout = 0, rlen, ok = force;
  136. static const char *rnames[] = { "i2sbus: %s (control)",
  137. "i2sbus: %s (tx)",
  138. "i2sbus: %s (rx)" };
  139. static irq_handler_t ints[] = {
  140. i2sbus_bus_intr,
  141. i2sbus_tx_intr,
  142. i2sbus_rx_intr
  143. };
  144. if (strlen(np->name) != 5)
  145. return 0;
  146. if (strncmp(np->name, "i2s-", 4))
  147. return 0;
  148. dev = kzalloc(sizeof(struct i2sbus_dev), GFP_KERNEL);
  149. if (!dev)
  150. return 0;
  151. i = 0;
  152. while ((child = of_get_next_child(np, child))) {
  153. if (strcmp(child->name, "sound") == 0) {
  154. i++;
  155. sound = child;
  156. }
  157. }
  158. if (i == 1) {
  159. const u32 *id = of_get_property(sound, "layout-id", NULL);
  160. if (id) {
  161. layout = *id;
  162. snprintf(dev->sound.modalias, 32,
  163. "sound-layout-%d", layout);
  164. ok = 1;
  165. } else {
  166. id = of_get_property(sound, "device-id", NULL);
  167. /*
  168. * We probably cannot handle all device-id machines,
  169. * so restrict to those we do handle for now.
  170. */
  171. if (id && (*id == 22 || *id == 14 || *id == 35)) {
  172. snprintf(dev->sound.modalias, 32,
  173. "aoa-device-id-%d", *id);
  174. ok = 1;
  175. layout = -1;
  176. }
  177. }
  178. }
  179. /* for the time being, until we can handle non-layout-id
  180. * things in some fabric, refuse to attach if there is no
  181. * layout-id property or we haven't been forced to attach.
  182. * When there are two i2s busses and only one has a layout-id,
  183. * then this depends on the order, but that isn't important
  184. * either as the second one in that case is just a modem. */
  185. if (!ok) {
  186. kfree(dev);
  187. return -ENODEV;
  188. }
  189. mutex_init(&dev->lock);
  190. spin_lock_init(&dev->low_lock);
  191. dev->sound.ofdev.node = np;
  192. dev->sound.ofdev.dma_mask = macio->ofdev.dma_mask;
  193. dev->sound.ofdev.dev.dma_mask = &dev->sound.ofdev.dma_mask;
  194. dev->sound.ofdev.dev.parent = &macio->ofdev.dev;
  195. dev->sound.ofdev.dev.release = i2sbus_release_dev;
  196. dev->sound.attach_codec = i2sbus_attach_codec;
  197. dev->sound.detach_codec = i2sbus_detach_codec;
  198. dev->sound.pcmid = -1;
  199. dev->macio = macio;
  200. dev->control = control;
  201. dev->bus_number = np->name[4] - 'a';
  202. INIT_LIST_HEAD(&dev->sound.codec_list);
  203. for (i = aoa_resource_i2smmio; i <= aoa_resource_rxdbdma; i++) {
  204. dev->interrupts[i] = -1;
  205. snprintf(dev->rnames[i], sizeof(dev->rnames[i]),
  206. rnames[i], np->name);
  207. }
  208. for (i = aoa_resource_i2smmio; i <= aoa_resource_rxdbdma; i++) {
  209. int irq = irq_of_parse_and_map(np, i);
  210. if (request_irq(irq, ints[i], 0, dev->rnames[i], dev))
  211. goto err;
  212. dev->interrupts[i] = irq;
  213. }
  214. /* Resource handling is problematic as some device-trees contain
  215. * useless crap (ugh ugh ugh). We work around that here by calling
  216. * specific functions for calculating the appropriate resources.
  217. *
  218. * This will all be moved to macio_asic.c at one point
  219. */
  220. for (i = aoa_resource_i2smmio; i <= aoa_resource_rxdbdma; i++) {
  221. if (i2sbus_get_and_fixup_rsrc(np,i,layout,&dev->resources[i]))
  222. goto err;
  223. /* If only we could use our resource dev->resources[i]...
  224. * but request_resource doesn't know about parents and
  225. * contained resources...
  226. */
  227. dev->allocated_resource[i] =
  228. request_mem_region(dev->resources[i].start,
  229. dev->resources[i].end -
  230. dev->resources[i].start + 1,
  231. dev->rnames[i]);
  232. if (!dev->allocated_resource[i]) {
  233. printk(KERN_ERR "i2sbus: failed to claim resource %d!\n", i);
  234. goto err;
  235. }
  236. }
  237. r = &dev->resources[aoa_resource_i2smmio];
  238. rlen = r->end - r->start + 1;
  239. if (rlen < sizeof(struct i2s_interface_regs))
  240. goto err;
  241. dev->intfregs = ioremap(r->start, rlen);
  242. r = &dev->resources[aoa_resource_txdbdma];
  243. rlen = r->end - r->start + 1;
  244. if (rlen < sizeof(struct dbdma_regs))
  245. goto err;
  246. dev->out.dbdma = ioremap(r->start, rlen);
  247. r = &dev->resources[aoa_resource_rxdbdma];
  248. rlen = r->end - r->start + 1;
  249. if (rlen < sizeof(struct dbdma_regs))
  250. goto err;
  251. dev->in.dbdma = ioremap(r->start, rlen);
  252. if (!dev->intfregs || !dev->out.dbdma || !dev->in.dbdma)
  253. goto err;
  254. if (alloc_dbdma_descriptor_ring(dev, &dev->out.dbdma_ring,
  255. MAX_DBDMA_COMMANDS))
  256. goto err;
  257. if (alloc_dbdma_descriptor_ring(dev, &dev->in.dbdma_ring,
  258. MAX_DBDMA_COMMANDS))
  259. goto err;
  260. if (i2sbus_control_add_dev(dev->control, dev)) {
  261. printk(KERN_ERR "i2sbus: control layer didn't like bus\n");
  262. goto err;
  263. }
  264. if (soundbus_add_one(&dev->sound)) {
  265. printk(KERN_DEBUG "i2sbus: device registration error!\n");
  266. goto err;
  267. }
  268. /* enable this cell */
  269. i2sbus_control_cell(dev->control, dev, 1);
  270. i2sbus_control_enable(dev->control, dev);
  271. i2sbus_control_clock(dev->control, dev, 1);
  272. return 1;
  273. err:
  274. for (i=0;i<3;i++)
  275. if (dev->interrupts[i] != -1)
  276. free_irq(dev->interrupts[i], dev);
  277. free_dbdma_descriptor_ring(dev, &dev->out.dbdma_ring);
  278. free_dbdma_descriptor_ring(dev, &dev->in.dbdma_ring);
  279. if (dev->intfregs) iounmap(dev->intfregs);
  280. if (dev->out.dbdma) iounmap(dev->out.dbdma);
  281. if (dev->in.dbdma) iounmap(dev->in.dbdma);
  282. for (i=0;i<3;i++)
  283. if (dev->allocated_resource[i])
  284. release_and_free_resource(dev->allocated_resource[i]);
  285. mutex_destroy(&dev->lock);
  286. kfree(dev);
  287. return 0;
  288. }
  289. static int i2sbus_probe(struct macio_dev* dev, const struct of_device_id *match)
  290. {
  291. struct device_node *np = NULL;
  292. int got = 0, err;
  293. struct i2sbus_control *control = NULL;
  294. err = i2sbus_control_init(dev, &control);
  295. if (err)
  296. return err;
  297. if (!control) {
  298. printk(KERN_ERR "i2sbus_control_init API breakage\n");
  299. return -ENODEV;
  300. }
  301. while ((np = of_get_next_child(dev->ofdev.node, np))) {
  302. if (of_device_is_compatible(np, "i2sbus") ||
  303. of_device_is_compatible(np, "i2s-modem")) {
  304. got += i2sbus_add_dev(dev, control, np);
  305. }
  306. }
  307. if (!got) {
  308. /* found none, clean up */
  309. i2sbus_control_destroy(control);
  310. return -ENODEV;
  311. }
  312. dev_set_drvdata(&dev->ofdev.dev, control);
  313. return 0;
  314. }
  315. static int i2sbus_remove(struct macio_dev* dev)
  316. {
  317. struct i2sbus_control *control = dev_get_drvdata(&dev->ofdev.dev);
  318. struct i2sbus_dev *i2sdev, *tmp;
  319. list_for_each_entry_safe(i2sdev, tmp, &control->list, item)
  320. soundbus_remove_one(&i2sdev->sound);
  321. return 0;
  322. }
  323. #ifdef CONFIG_PM
  324. static int i2sbus_suspend(struct macio_dev* dev, pm_message_t state)
  325. {
  326. struct i2sbus_control *control = dev_get_drvdata(&dev->ofdev.dev);
  327. struct codec_info_item *cii;
  328. struct i2sbus_dev* i2sdev;
  329. int err, ret = 0;
  330. list_for_each_entry(i2sdev, &control->list, item) {
  331. /* Notify Alsa */
  332. if (i2sdev->sound.pcm) {
  333. /* Suspend PCM streams */
  334. snd_pcm_suspend_all(i2sdev->sound.pcm);
  335. }
  336. /* Notify codecs */
  337. list_for_each_entry(cii, &i2sdev->sound.codec_list, list) {
  338. err = 0;
  339. if (cii->codec->suspend)
  340. err = cii->codec->suspend(cii, state);
  341. if (err)
  342. ret = err;
  343. }
  344. /* wait until streams are stopped */
  345. i2sbus_wait_for_stop_both(i2sdev);
  346. }
  347. return ret;
  348. }
  349. static int i2sbus_resume(struct macio_dev* dev)
  350. {
  351. struct i2sbus_control *control = dev_get_drvdata(&dev->ofdev.dev);
  352. struct codec_info_item *cii;
  353. struct i2sbus_dev* i2sdev;
  354. int err, ret = 0;
  355. list_for_each_entry(i2sdev, &control->list, item) {
  356. /* reset i2s bus format etc. */
  357. i2sbus_pcm_prepare_both(i2sdev);
  358. /* Notify codecs so they can re-initialize */
  359. list_for_each_entry(cii, &i2sdev->sound.codec_list, list) {
  360. err = 0;
  361. if (cii->codec->resume)
  362. err = cii->codec->resume(cii);
  363. if (err)
  364. ret = err;
  365. }
  366. }
  367. return ret;
  368. }
  369. #endif /* CONFIG_PM */
  370. static int i2sbus_shutdown(struct macio_dev* dev)
  371. {
  372. return 0;
  373. }
  374. static struct macio_driver i2sbus_drv = {
  375. .name = "soundbus-i2s",
  376. .owner = THIS_MODULE,
  377. .match_table = i2sbus_match,
  378. .probe = i2sbus_probe,
  379. .remove = i2sbus_remove,
  380. #ifdef CONFIG_PM
  381. .suspend = i2sbus_suspend,
  382. .resume = i2sbus_resume,
  383. #endif
  384. .shutdown = i2sbus_shutdown,
  385. };
  386. static int __init soundbus_i2sbus_init(void)
  387. {
  388. return macio_register_driver(&i2sbus_drv);
  389. }
  390. static void __exit soundbus_i2sbus_exit(void)
  391. {
  392. macio_unregister_driver(&i2sbus_drv);
  393. }
  394. module_init(soundbus_i2sbus_init);
  395. module_exit(soundbus_i2sbus_exit);