i2sbus-core.c 12 KB

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