acpi.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * ALSA driver for ACPI-based HDA Controllers.
  4. */
  5. #include <linux/module.h>
  6. #include <linux/platform_device.h>
  7. #include <linux/acpi.h>
  8. #include <sound/hda_codec.h>
  9. #include "hda_controller.h"
  10. struct hda_acpi {
  11. struct azx azx;
  12. struct snd_card *card;
  13. struct platform_device *pdev;
  14. void __iomem *regs;
  15. struct work_struct probe_work;
  16. const struct hda_data *data;
  17. };
  18. /**
  19. * struct hda_data - Optional device-specific data
  20. * @short_name: Used for the ALSA card name; defaults to KBUILD_MODNAME
  21. * @long_name: Used for longer description; defaults to short_name
  22. * @flags: Passed to &azx->driver_caps
  23. *
  24. * A pointer to a record of this type may be stored in the
  25. * &acpi_device_id->driver_data field of an ACPI match table entry in order to
  26. * customize the naming and behavior of a particular device. All fields are
  27. * optional and sensible defaults will be selected in their absence.
  28. */
  29. struct hda_data {
  30. const char *short_name;
  31. const char *long_name;
  32. unsigned long flags;
  33. };
  34. static int hda_acpi_dev_disconnect(struct snd_device *device)
  35. {
  36. struct azx *chip = device->device_data;
  37. chip->bus.shutdown = 1;
  38. return 0;
  39. }
  40. static int hda_acpi_dev_free(struct snd_device *device)
  41. {
  42. struct azx *azx = device->device_data;
  43. struct hda_acpi *hda = container_of(azx, struct hda_acpi, azx);
  44. cancel_work_sync(&hda->probe_work);
  45. if (azx_bus(azx)->chip_init) {
  46. azx_stop_all_streams(azx);
  47. azx_stop_chip(azx);
  48. }
  49. azx_free_stream_pages(azx);
  50. azx_free_streams(azx);
  51. snd_hdac_bus_exit(azx_bus(azx));
  52. return 0;
  53. }
  54. static int hda_acpi_init(struct hda_acpi *hda)
  55. {
  56. struct hdac_bus *bus = azx_bus(&hda->azx);
  57. struct snd_card *card = hda->azx.card;
  58. struct device *dev = &hda->pdev->dev;
  59. struct azx *azx = &hda->azx;
  60. struct resource *res;
  61. unsigned short gcap;
  62. const char *sname, *lname;
  63. int err, irq;
  64. /* The base address for the HDA registers and the interrupt are wrapped
  65. * in an ACPI _CRS object which can be parsed by platform_get_irq() and
  66. * devm_platform_get_and_ioremap_resource()
  67. */
  68. irq = platform_get_irq(hda->pdev, 0);
  69. if (irq < 0)
  70. return irq;
  71. hda->regs = devm_platform_get_and_ioremap_resource(hda->pdev, 0, &res);
  72. if (IS_ERR(hda->regs))
  73. return PTR_ERR(hda->regs);
  74. bus->remap_addr = hda->regs;
  75. bus->addr = res->start;
  76. err = devm_request_irq(dev, irq, azx_interrupt,
  77. IRQF_SHARED, KBUILD_MODNAME, azx);
  78. if (err) {
  79. dev_err(dev, "unable to request IRQ %d, disabling device\n",
  80. irq);
  81. return err;
  82. }
  83. bus->irq = irq;
  84. bus->dma_stop_delay = 100;
  85. card->sync_irq = bus->irq;
  86. gcap = azx_readw(azx, GCAP);
  87. dev_dbg(dev, "chipset global capabilities = 0x%x\n", gcap);
  88. azx->align_buffer_size = 1;
  89. azx->capture_streams = (gcap >> 8) & 0x0f;
  90. azx->playback_streams = (gcap >> 12) & 0x0f;
  91. azx->capture_index_offset = 0;
  92. azx->playback_index_offset = azx->capture_streams;
  93. azx->num_streams = azx->playback_streams + azx->capture_streams;
  94. err = azx_init_streams(azx);
  95. if (err < 0) {
  96. dev_err(dev, "failed to initialize streams: %d\n", err);
  97. return err;
  98. }
  99. err = azx_alloc_stream_pages(azx);
  100. if (err < 0) {
  101. dev_err(dev, "failed to allocate stream pages: %d\n", err);
  102. return err;
  103. }
  104. azx_init_chip(azx, 1);
  105. if (!bus->codec_mask) {
  106. dev_err(dev, "no codecs found!\n");
  107. return -ENODEV;
  108. }
  109. strscpy(card->driver, "hda-acpi");
  110. sname = hda->data->short_name ? hda->data->short_name : KBUILD_MODNAME;
  111. if (strlen(sname) > sizeof(card->shortname))
  112. dev_info(dev, "truncating shortname for card %s\n", sname);
  113. strscpy(card->shortname, sname);
  114. lname = hda->data->long_name ? hda->data->long_name : sname;
  115. snprintf(card->longname, sizeof(card->longname),
  116. "%s at 0x%lx irq %i", lname, bus->addr, bus->irq);
  117. return 0;
  118. }
  119. static void hda_acpi_probe_work(struct work_struct *work)
  120. {
  121. struct hda_acpi *hda = container_of(work, struct hda_acpi, probe_work);
  122. struct azx *chip = &hda->azx;
  123. int err;
  124. err = hda_acpi_init(hda);
  125. if (err < 0)
  126. return;
  127. err = azx_probe_codecs(chip, 8);
  128. if (err < 0)
  129. return;
  130. err = azx_codec_configure(chip);
  131. if (err < 0)
  132. return;
  133. err = snd_card_register(chip->card);
  134. if (err < 0)
  135. return;
  136. chip->running = 1;
  137. }
  138. static int hda_acpi_create(struct hda_acpi *hda)
  139. {
  140. static const struct snd_device_ops ops = {
  141. .dev_disconnect = hda_acpi_dev_disconnect,
  142. .dev_free = hda_acpi_dev_free,
  143. };
  144. static const struct hda_controller_ops null_ops;
  145. struct azx *azx = &hda->azx;
  146. int err;
  147. mutex_init(&azx->open_mutex);
  148. azx->card = hda->card;
  149. INIT_LIST_HEAD(&azx->pcm_list);
  150. azx->ops = &null_ops;
  151. azx->driver_caps = hda->data->flags;
  152. azx->driver_type = hda->data->flags & 0xff;
  153. azx->codec_probe_mask = -1;
  154. err = azx_bus_init(azx, NULL);
  155. if (err < 0)
  156. return err;
  157. err = snd_device_new(hda->card, SNDRV_DEV_LOWLEVEL, &hda->azx, &ops);
  158. if (err < 0) {
  159. dev_err(&hda->pdev->dev, "Error creating device\n");
  160. return err;
  161. }
  162. return 0;
  163. }
  164. static int hda_acpi_probe(struct platform_device *pdev)
  165. {
  166. struct hda_acpi *hda;
  167. int err;
  168. hda = devm_kzalloc(&pdev->dev, sizeof(*hda), GFP_KERNEL);
  169. if (!hda)
  170. return -ENOMEM;
  171. hda->pdev = pdev;
  172. hda->data = acpi_device_get_match_data(&pdev->dev);
  173. /* Fall back to defaults if the table didn't have a *struct hda_data */
  174. if (!hda->data)
  175. hda->data = devm_kzalloc(&pdev->dev, sizeof(*hda->data),
  176. GFP_KERNEL);
  177. if (!hda->data)
  178. return -ENOMEM;
  179. err = snd_card_new(&pdev->dev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
  180. THIS_MODULE, 0, &hda->card);
  181. if (err < 0) {
  182. dev_err(&pdev->dev, "Error creating card!\n");
  183. return err;
  184. }
  185. INIT_WORK(&hda->probe_work, hda_acpi_probe_work);
  186. err = hda_acpi_create(hda);
  187. if (err < 0)
  188. goto out_free;
  189. hda->card->private_data = &hda->azx;
  190. dev_set_drvdata(&pdev->dev, hda->card);
  191. schedule_work(&hda->probe_work);
  192. return 0;
  193. out_free:
  194. snd_card_free(hda->card);
  195. return err;
  196. }
  197. static void hda_acpi_remove(struct platform_device *pdev)
  198. {
  199. snd_card_free(dev_get_drvdata(&pdev->dev));
  200. }
  201. static void hda_acpi_shutdown(struct platform_device *pdev)
  202. {
  203. struct snd_card *card = dev_get_drvdata(&pdev->dev);
  204. struct azx *chip;
  205. if (!card)
  206. return;
  207. chip = card->private_data;
  208. if (chip && chip->running)
  209. azx_stop_chip(chip);
  210. }
  211. static int hda_acpi_suspend(struct device *dev)
  212. {
  213. struct snd_card *card = dev_get_drvdata(dev);
  214. int rc;
  215. rc = pm_runtime_force_suspend(dev);
  216. if (rc < 0)
  217. return rc;
  218. snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
  219. return 0;
  220. }
  221. static int hda_acpi_resume(struct device *dev)
  222. {
  223. struct snd_card *card = dev_get_drvdata(dev);
  224. int rc;
  225. rc = pm_runtime_force_resume(dev);
  226. if (rc < 0)
  227. return rc;
  228. snd_power_change_state(card, SNDRV_CTL_POWER_D0);
  229. return 0;
  230. }
  231. static const struct dev_pm_ops hda_acpi_pm = {
  232. SYSTEM_SLEEP_PM_OPS(hda_acpi_suspend, hda_acpi_resume)
  233. };
  234. static const struct hda_data nvidia_hda_data = {
  235. .short_name = "NVIDIA",
  236. .long_name = "NVIDIA HDA Controller",
  237. .flags = AZX_DCAPS_CORBRP_SELF_CLEAR,
  238. };
  239. static const struct acpi_device_id hda_acpi_match[] = {
  240. { .id = "NVDA2014", .driver_data = (uintptr_t) &nvidia_hda_data },
  241. { .id = "NVDA2015", .driver_data = (uintptr_t) &nvidia_hda_data },
  242. {},
  243. };
  244. MODULE_DEVICE_TABLE(acpi, hda_acpi_match);
  245. static struct platform_driver hda_acpi_platform_driver = {
  246. .driver = {
  247. .name = KBUILD_MODNAME,
  248. .pm = &hda_acpi_pm,
  249. .acpi_match_table = hda_acpi_match,
  250. },
  251. .probe = hda_acpi_probe,
  252. .remove = hda_acpi_remove,
  253. .shutdown = hda_acpi_shutdown,
  254. };
  255. module_platform_driver(hda_acpi_platform_driver);
  256. MODULE_DESCRIPTION("Driver for ACPI-based HDA Controllers");
  257. MODULE_LICENSE("GPL");