core.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  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/slab.h>
  10. #include <linux/pci.h>
  11. #include <linux/interrupt.h>
  12. #include <linux/dma-mapping.h>
  13. #include <sound/core.h>
  14. #include <asm/macio.h>
  15. #include <asm/dbdma.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. static int force;
  22. module_param(force, int, 0444);
  23. MODULE_PARM_DESC(force, "Force loading i2sbus even when"
  24. " no layout-id property is present");
  25. static struct of_device_id i2sbus_match[] = {
  26. { .name = "i2s" },
  27. { }
  28. };
  29. MODULE_DEVICE_TABLE(of, i2sbus_match);
  30. static int alloc_dbdma_descriptor_ring(struct i2sbus_dev *i2sdev,
  31. struct dbdma_command_mem *r,
  32. int numcmds)
  33. {
  34. /* one more for rounding, one for branch back, one for stop command */
  35. r->size = (numcmds + 3) * sizeof(struct dbdma_cmd);
  36. /* We use the PCI APIs for now until the generic one gets fixed
  37. * enough or until we get some macio-specific versions
  38. */
  39. r->space = dma_alloc_coherent(
  40. &macio_get_pci_dev(i2sdev->macio)->dev,
  41. r->size,
  42. &r->bus_addr,
  43. GFP_KERNEL);
  44. if (!r->space) return -ENOMEM;
  45. memset(r->space, 0, r->size);
  46. r->cmds = (void*)DBDMA_ALIGN(r->space);
  47. r->bus_cmd_start = r->bus_addr +
  48. (dma_addr_t)((char*)r->cmds - (char*)r->space);
  49. return 0;
  50. }
  51. static void free_dbdma_descriptor_ring(struct i2sbus_dev *i2sdev,
  52. struct dbdma_command_mem *r)
  53. {
  54. if (!r->space) return;
  55. dma_free_coherent(&macio_get_pci_dev(i2sdev->macio)->dev,
  56. r->size, r->space, r->bus_addr);
  57. }
  58. static void i2sbus_release_dev(struct device *dev)
  59. {
  60. struct i2sbus_dev *i2sdev;
  61. int i;
  62. i2sdev = container_of(dev, struct i2sbus_dev, sound.ofdev.dev);
  63. if (i2sdev->intfregs) iounmap(i2sdev->intfregs);
  64. if (i2sdev->out.dbdma) iounmap(i2sdev->out.dbdma);
  65. if (i2sdev->in.dbdma) iounmap(i2sdev->in.dbdma);
  66. for (i = aoa_resource_i2smmio; i <= aoa_resource_rxdbdma; i++)
  67. if (i2sdev->allocated_resource[i])
  68. release_and_free_resource(i2sdev->allocated_resource[i]);
  69. free_dbdma_descriptor_ring(i2sdev, &i2sdev->out.dbdma_ring);
  70. free_dbdma_descriptor_ring(i2sdev, &i2sdev->in.dbdma_ring);
  71. for (i = aoa_resource_i2smmio; i <= aoa_resource_rxdbdma; i++)
  72. free_irq(i2sdev->interrupts[i], i2sdev);
  73. i2sbus_control_remove_dev(i2sdev->control, i2sdev);
  74. mutex_destroy(&i2sdev->lock);
  75. kfree(i2sdev);
  76. }
  77. static irqreturn_t i2sbus_bus_intr(int irq, void *devid)
  78. {
  79. struct i2sbus_dev *dev = devid;
  80. u32 intreg;
  81. spin_lock(&dev->low_lock);
  82. intreg = in_le32(&dev->intfregs->intr_ctl);
  83. /* acknowledge interrupt reasons */
  84. out_le32(&dev->intfregs->intr_ctl, intreg);
  85. spin_unlock(&dev->low_lock);
  86. return IRQ_HANDLED;
  87. }
  88. /*
  89. * XXX FIXME: We test the layout_id's here to get the proper way of
  90. * mapping in various registers, thanks to bugs in Apple device-trees.
  91. * We could instead key off the machine model and the name of the i2s
  92. * node (i2s-a). This we'll do when we move it all to macio_asic.c
  93. * and have that export items for each sub-node too.
  94. */
  95. static int i2sbus_get_and_fixup_rsrc(struct device_node *np, int index,
  96. int layout, struct resource *res)
  97. {
  98. struct device_node *parent;
  99. int pindex, rc = -ENXIO;
  100. const u32 *reg;
  101. /* Machines with layout 76 and 36 (K2 based) have a weird device
  102. * tree what we need to special case.
  103. * Normal machines just fetch the resource from the i2s-X node.
  104. * Darwin further divides normal machines into old and new layouts
  105. * with a subtely different code path but that doesn't seem necessary
  106. * in practice, they just bloated it. In addition, even on our K2
  107. * case the i2s-modem node, if we ever want to handle it, uses the
  108. * normal layout
  109. */
  110. if (layout != 76 && layout != 36)
  111. return of_address_to_resource(np, index, res);
  112. parent = of_get_parent(np);
  113. pindex = (index == aoa_resource_i2smmio) ? 0 : 1;
  114. rc = of_address_to_resource(parent, pindex, res);
  115. if (rc)
  116. goto bail;
  117. reg = of_get_property(np, "reg", NULL);
  118. if (reg == NULL) {
  119. rc = -ENXIO;
  120. goto bail;
  121. }
  122. res->start += reg[index * 2];
  123. res->end = res->start + reg[index * 2 + 1] - 1;
  124. bail:
  125. of_node_put(parent);
  126. return rc;
  127. }
  128. /* FIXME: look at device node refcounting */
  129. static int i2sbus_add_dev(struct macio_dev *macio,
  130. struct i2sbus_control *control,
  131. struct device_node *np)
  132. {
  133. struct i2sbus_dev *dev;
  134. struct device_node *child = NULL, *sound = NULL;
  135. struct resource *r;
  136. int i, layout = 0, rlen, ok = force;
  137. static const char *rnames[] = { "i2sbus: %s (control)",
  138. "i2sbus: %s (tx)",
  139. "i2sbus: %s (rx)" };
  140. static irq_handler_t ints[] = {
  141. i2sbus_bus_intr,
  142. i2sbus_tx_intr,
  143. i2sbus_rx_intr
  144. };
  145. if (strlen(np->name) != 5)
  146. return 0;
  147. if (strncmp(np->name, "i2s-", 4))
  148. return 0;
  149. dev = kzalloc(sizeof(struct i2sbus_dev), GFP_KERNEL);
  150. if (!dev)
  151. return 0;
  152. i = 0;
  153. while ((child = of_get_next_child(np, child))) {
  154. if (strcmp(child->name, "sound") == 0) {
  155. i++;
  156. sound = child;
  157. }
  158. }
  159. if (i == 1) {
  160. const u32 *id = of_get_property(sound, "layout-id", NULL);
  161. if (id) {
  162. layout = *id;
  163. snprintf(dev->sound.modalias, 32,
  164. "sound-layout-%d", layout);
  165. ok = 1;
  166. } else {
  167. id = of_get_property(sound, "device-id", NULL);
  168. /*
  169. * We probably cannot handle all device-id machines,
  170. * so restrict to those we do handle for now.
  171. */
  172. if (id && (*id == 22 || *id == 14 || *id == 35)) {
  173. snprintf(dev->sound.modalias, 32,
  174. "aoa-device-id-%d", *id);
  175. ok = 1;
  176. layout = -1;
  177. }
  178. }
  179. }
  180. /* for the time being, until we can handle non-layout-id
  181. * things in some fabric, refuse to attach if there is no
  182. * layout-id property or we haven't been forced to attach.
  183. * When there are two i2s busses and only one has a layout-id,
  184. * then this depends on the order, but that isn't important
  185. * either as the second one in that case is just a modem. */
  186. if (!ok) {
  187. kfree(dev);
  188. return -ENODEV;
  189. }
  190. mutex_init(&dev->lock);
  191. spin_lock_init(&dev->low_lock);
  192. dev->sound.ofdev.archdata.dma_mask = macio->ofdev.archdata.dma_mask;
  193. dev->sound.ofdev.dev.of_node = np;
  194. dev->sound.ofdev.dev.dma_mask = &dev->sound.ofdev.archdata.dma_mask;
  195. dev->sound.ofdev.dev.parent = &macio->ofdev.dev;
  196. dev->sound.ofdev.dev.release = i2sbus_release_dev;
  197. dev->sound.attach_codec = i2sbus_attach_codec;
  198. dev->sound.detach_codec = i2sbus_detach_codec;
  199. dev->sound.pcmid = -1;
  200. dev->macio = macio;
  201. dev->control = control;
  202. dev->bus_number = np->name[4] - 'a';
  203. INIT_LIST_HEAD(&dev->sound.codec_list);
  204. for (i = aoa_resource_i2smmio; i <= aoa_resource_rxdbdma; i++) {
  205. dev->interrupts[i] = -1;
  206. snprintf(dev->rnames[i], sizeof(dev->rnames[i]),
  207. rnames[i], np->name);
  208. }
  209. for (i = aoa_resource_i2smmio; i <= aoa_resource_rxdbdma; i++) {
  210. int irq = irq_of_parse_and_map(np, i);
  211. if (request_irq(irq, ints[i], 0, dev->rnames[i], dev))
  212. goto err;
  213. dev->interrupts[i] = irq;
  214. }
  215. /* Resource handling is problematic as some device-trees contain
  216. * useless crap (ugh ugh ugh). We work around that here by calling
  217. * specific functions for calculating the appropriate resources.
  218. *
  219. * This will all be moved to macio_asic.c at one point
  220. */
  221. for (i = aoa_resource_i2smmio; i <= aoa_resource_rxdbdma; i++) {
  222. if (i2sbus_get_and_fixup_rsrc(np,i,layout,&dev->resources[i]))
  223. goto err;
  224. /* If only we could use our resource dev->resources[i]...
  225. * but request_resource doesn't know about parents and
  226. * contained resources...
  227. */
  228. dev->allocated_resource[i] =
  229. request_mem_region(dev->resources[i].start,
  230. resource_size(&dev->resources[i]),
  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 = resource_size(r);
  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 = resource_size(r);
  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 = resource_size(r);
  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.dev.of_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. .driver = {
  376. .name = "soundbus-i2s",
  377. .owner = THIS_MODULE,
  378. .of_match_table = i2sbus_match,
  379. },
  380. .probe = i2sbus_probe,
  381. .remove = i2sbus_remove,
  382. #ifdef CONFIG_PM
  383. .suspend = i2sbus_suspend,
  384. .resume = i2sbus_resume,
  385. #endif
  386. .shutdown = i2sbus_shutdown,
  387. };
  388. static int __init soundbus_i2sbus_init(void)
  389. {
  390. return macio_register_driver(&i2sbus_drv);
  391. }
  392. static void __exit soundbus_i2sbus_exit(void)
  393. {
  394. macio_unregister_driver(&i2sbus_drv);
  395. }
  396. module_init(soundbus_i2sbus_init);
  397. module_exit(soundbus_i2sbus_exit);