core.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  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 <linux/of_address.h>
  14. #include <linux/of_irq.h>
  15. #include <sound/core.h>
  16. #include <asm/macio.h>
  17. #include <asm/dbdma.h>
  18. #include "../soundbus.h"
  19. #include "i2sbus.h"
  20. MODULE_LICENSE("GPL");
  21. MODULE_AUTHOR("Johannes Berg <johannes@sipsolutions.net>");
  22. MODULE_DESCRIPTION("Apple Soundbus: I2S support");
  23. static int force;
  24. module_param(force, int, 0444);
  25. MODULE_PARM_DESC(force, "Force loading i2sbus even when"
  26. " no layout-id property is present");
  27. static const struct of_device_id i2sbus_match[] = {
  28. { .name = "i2s" },
  29. { }
  30. };
  31. MODULE_DEVICE_TABLE(of, i2sbus_match);
  32. static int alloc_dbdma_descriptor_ring(struct i2sbus_dev *i2sdev,
  33. struct dbdma_command_mem *r,
  34. int numcmds)
  35. {
  36. /* one more for rounding, one for branch back, one for stop command */
  37. r->size = (numcmds + 3) * sizeof(struct dbdma_cmd);
  38. /* We use the PCI APIs for now until the generic one gets fixed
  39. * enough or until we get some macio-specific versions
  40. */
  41. r->space = dma_zalloc_coherent(&macio_get_pci_dev(i2sdev->macio)->dev,
  42. r->size, &r->bus_addr, GFP_KERNEL);
  43. if (!r->space)
  44. return -ENOMEM;
  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. iounmap(i2sdev->intfregs);
  63. iounmap(i2sdev->out.dbdma);
  64. iounmap(i2sdev->in.dbdma);
  65. for (i = aoa_resource_i2smmio; i <= aoa_resource_rxdbdma; 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 = aoa_resource_i2smmio; i <= aoa_resource_rxdbdma; 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)
  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. /*
  87. * XXX FIXME: We test the layout_id's here to get the proper way of
  88. * mapping in various registers, thanks to bugs in Apple device-trees.
  89. * We could instead key off the machine model and the name of the i2s
  90. * node (i2s-a). This we'll do when we move it all to macio_asic.c
  91. * and have that export items for each sub-node too.
  92. */
  93. static int i2sbus_get_and_fixup_rsrc(struct device_node *np, int index,
  94. int layout, struct resource *res)
  95. {
  96. struct device_node *parent;
  97. int pindex, rc = -ENXIO;
  98. const u32 *reg;
  99. /* Machines with layout 76 and 36 (K2 based) have a weird device
  100. * tree what we need to special case.
  101. * Normal machines just fetch the resource from the i2s-X node.
  102. * Darwin further divides normal machines into old and new layouts
  103. * with a subtely different code path but that doesn't seem necessary
  104. * in practice, they just bloated it. In addition, even on our K2
  105. * case the i2s-modem node, if we ever want to handle it, uses the
  106. * normal layout
  107. */
  108. if (layout != 76 && layout != 36)
  109. return of_address_to_resource(np, index, res);
  110. parent = of_get_parent(np);
  111. pindex = (index == aoa_resource_i2smmio) ? 0 : 1;
  112. rc = of_address_to_resource(parent, pindex, res);
  113. if (rc)
  114. goto bail;
  115. reg = of_get_property(np, "reg", NULL);
  116. if (reg == NULL) {
  117. rc = -ENXIO;
  118. goto bail;
  119. }
  120. res->start += reg[index * 2];
  121. res->end = res->start + reg[index * 2 + 1] - 1;
  122. bail:
  123. of_node_put(parent);
  124. return rc;
  125. }
  126. /* FIXME: look at device node refcounting */
  127. static int i2sbus_add_dev(struct macio_dev *macio,
  128. struct i2sbus_control *control,
  129. struct device_node *np)
  130. {
  131. struct i2sbus_dev *dev;
  132. struct device_node *child = NULL, *sound = NULL;
  133. struct resource *r;
  134. int i, layout = 0, rlen, ok = force;
  135. static const char *rnames[] = { "i2sbus: %s (control)",
  136. "i2sbus: %s (tx)",
  137. "i2sbus: %s (rx)" };
  138. static irq_handler_t ints[] = {
  139. i2sbus_bus_intr,
  140. i2sbus_tx_intr,
  141. i2sbus_rx_intr
  142. };
  143. if (strlen(np->name) != 5)
  144. return 0;
  145. if (strncmp(np->name, "i2s-", 4))
  146. return 0;
  147. dev = kzalloc(sizeof(struct i2sbus_dev), GFP_KERNEL);
  148. if (!dev)
  149. return 0;
  150. i = 0;
  151. while ((child = of_get_next_child(np, child))) {
  152. if (strcmp(child->name, "sound") == 0) {
  153. i++;
  154. sound = child;
  155. }
  156. }
  157. if (i == 1) {
  158. const u32 *id = of_get_property(sound, "layout-id", NULL);
  159. if (id) {
  160. layout = *id;
  161. snprintf(dev->sound.modalias, 32,
  162. "sound-layout-%d", layout);
  163. ok = 1;
  164. } else {
  165. id = of_get_property(sound, "device-id", NULL);
  166. /*
  167. * We probably cannot handle all device-id machines,
  168. * so restrict to those we do handle for now.
  169. */
  170. if (id && (*id == 22 || *id == 14 || *id == 35 ||
  171. *id == 44)) {
  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.archdata.dma_mask = macio->ofdev.archdata.dma_mask;
  192. dev->sound.ofdev.dev.of_node = np;
  193. dev->sound.ofdev.dev.dma_mask = &dev->sound.ofdev.archdata.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. resource_size(&dev->resources[i]),
  230. dev->rnames[i]);
  231. if (!dev->allocated_resource[i]) {
  232. printk(KERN_ERR "i2sbus: failed to claim resource %d!\n", i);
  233. goto err;
  234. }
  235. }
  236. r = &dev->resources[aoa_resource_i2smmio];
  237. rlen = resource_size(r);
  238. if (rlen < sizeof(struct i2s_interface_regs))
  239. goto err;
  240. dev->intfregs = ioremap(r->start, rlen);
  241. r = &dev->resources[aoa_resource_txdbdma];
  242. rlen = resource_size(r);
  243. if (rlen < sizeof(struct dbdma_regs))
  244. goto err;
  245. dev->out.dbdma = ioremap(r->start, rlen);
  246. r = &dev->resources[aoa_resource_rxdbdma];
  247. rlen = resource_size(r);
  248. if (rlen < sizeof(struct dbdma_regs))
  249. goto err;
  250. dev->in.dbdma = ioremap(r->start, rlen);
  251. if (!dev->intfregs || !dev->out.dbdma || !dev->in.dbdma)
  252. goto err;
  253. if (alloc_dbdma_descriptor_ring(dev, &dev->out.dbdma_ring,
  254. MAX_DBDMA_COMMANDS))
  255. goto err;
  256. if (alloc_dbdma_descriptor_ring(dev, &dev->in.dbdma_ring,
  257. MAX_DBDMA_COMMANDS))
  258. goto err;
  259. if (i2sbus_control_add_dev(dev->control, dev)) {
  260. printk(KERN_ERR "i2sbus: control layer didn't like bus\n");
  261. goto err;
  262. }
  263. if (soundbus_add_one(&dev->sound)) {
  264. printk(KERN_DEBUG "i2sbus: device registration error!\n");
  265. goto err;
  266. }
  267. /* enable this cell */
  268. i2sbus_control_cell(dev->control, dev, 1);
  269. i2sbus_control_enable(dev->control, dev);
  270. i2sbus_control_clock(dev->control, dev, 1);
  271. return 1;
  272. err:
  273. for (i=0;i<3;i++)
  274. if (dev->interrupts[i] != -1)
  275. free_irq(dev->interrupts[i], dev);
  276. free_dbdma_descriptor_ring(dev, &dev->out.dbdma_ring);
  277. free_dbdma_descriptor_ring(dev, &dev->in.dbdma_ring);
  278. iounmap(dev->intfregs);
  279. iounmap(dev->out.dbdma);
  280. iounmap(dev->in.dbdma);
  281. for (i=0;i<3;i++)
  282. release_and_free_resource(dev->allocated_resource[i]);
  283. mutex_destroy(&dev->lock);
  284. kfree(dev);
  285. return 0;
  286. }
  287. static int i2sbus_probe(struct macio_dev* dev, const struct of_device_id *match)
  288. {
  289. struct device_node *np = NULL;
  290. int got = 0, err;
  291. struct i2sbus_control *control = NULL;
  292. err = i2sbus_control_init(dev, &control);
  293. if (err)
  294. return err;
  295. if (!control) {
  296. printk(KERN_ERR "i2sbus_control_init API breakage\n");
  297. return -ENODEV;
  298. }
  299. while ((np = of_get_next_child(dev->ofdev.dev.of_node, np))) {
  300. if (of_device_is_compatible(np, "i2sbus") ||
  301. of_device_is_compatible(np, "i2s-modem")) {
  302. got += i2sbus_add_dev(dev, control, np);
  303. }
  304. }
  305. if (!got) {
  306. /* found none, clean up */
  307. i2sbus_control_destroy(control);
  308. return -ENODEV;
  309. }
  310. dev_set_drvdata(&dev->ofdev.dev, control);
  311. return 0;
  312. }
  313. static int i2sbus_remove(struct macio_dev* dev)
  314. {
  315. struct i2sbus_control *control = dev_get_drvdata(&dev->ofdev.dev);
  316. struct i2sbus_dev *i2sdev, *tmp;
  317. list_for_each_entry_safe(i2sdev, tmp, &control->list, item)
  318. soundbus_remove_one(&i2sdev->sound);
  319. return 0;
  320. }
  321. #ifdef CONFIG_PM
  322. static int i2sbus_suspend(struct macio_dev* dev, pm_message_t state)
  323. {
  324. struct i2sbus_control *control = dev_get_drvdata(&dev->ofdev.dev);
  325. struct codec_info_item *cii;
  326. struct i2sbus_dev* i2sdev;
  327. int err, ret = 0;
  328. list_for_each_entry(i2sdev, &control->list, item) {
  329. /* Notify Alsa */
  330. /* Suspend PCM streams */
  331. snd_pcm_suspend_all(i2sdev->sound.pcm);
  332. /* Notify codecs */
  333. list_for_each_entry(cii, &i2sdev->sound.codec_list, list) {
  334. err = 0;
  335. if (cii->codec->suspend)
  336. err = cii->codec->suspend(cii, state);
  337. if (err)
  338. ret = err;
  339. }
  340. /* wait until streams are stopped */
  341. i2sbus_wait_for_stop_both(i2sdev);
  342. }
  343. return ret;
  344. }
  345. static int i2sbus_resume(struct macio_dev* dev)
  346. {
  347. struct i2sbus_control *control = dev_get_drvdata(&dev->ofdev.dev);
  348. struct codec_info_item *cii;
  349. struct i2sbus_dev* i2sdev;
  350. int err, ret = 0;
  351. list_for_each_entry(i2sdev, &control->list, item) {
  352. /* reset i2s bus format etc. */
  353. i2sbus_pcm_prepare_both(i2sdev);
  354. /* Notify codecs so they can re-initialize */
  355. list_for_each_entry(cii, &i2sdev->sound.codec_list, list) {
  356. err = 0;
  357. if (cii->codec->resume)
  358. err = cii->codec->resume(cii);
  359. if (err)
  360. ret = err;
  361. }
  362. }
  363. return ret;
  364. }
  365. #endif /* CONFIG_PM */
  366. static int i2sbus_shutdown(struct macio_dev* dev)
  367. {
  368. return 0;
  369. }
  370. static struct macio_driver i2sbus_drv = {
  371. .driver = {
  372. .name = "soundbus-i2s",
  373. .owner = THIS_MODULE,
  374. .of_match_table = i2sbus_match,
  375. },
  376. .probe = i2sbus_probe,
  377. .remove = i2sbus_remove,
  378. #ifdef CONFIG_PM
  379. .suspend = i2sbus_suspend,
  380. .resume = i2sbus_resume,
  381. #endif
  382. .shutdown = i2sbus_shutdown,
  383. };
  384. static int __init soundbus_i2sbus_init(void)
  385. {
  386. return macio_register_driver(&i2sbus_drv);
  387. }
  388. static void __exit soundbus_i2sbus_exit(void)
  389. {
  390. macio_unregister_driver(&i2sbus_drv);
  391. }
  392. module_init(soundbus_i2sbus_init);
  393. module_exit(soundbus_i2sbus_exit);