hda_tegra.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587
  1. /*
  2. *
  3. * Implementation of primary ALSA driver code base for NVIDIA Tegra HDA.
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms and conditions of the GNU General Public License,
  7. * version 2, as published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  12. * more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. *
  17. */
  18. #include <linux/clk.h>
  19. #include <linux/clocksource.h>
  20. #include <linux/completion.h>
  21. #include <linux/delay.h>
  22. #include <linux/dma-mapping.h>
  23. #include <linux/init.h>
  24. #include <linux/interrupt.h>
  25. #include <linux/io.h>
  26. #include <linux/kernel.h>
  27. #include <linux/module.h>
  28. #include <linux/moduleparam.h>
  29. #include <linux/mutex.h>
  30. #include <linux/of_device.h>
  31. #include <linux/slab.h>
  32. #include <linux/time.h>
  33. #include <sound/core.h>
  34. #include <sound/initval.h>
  35. #include "hda_codec.h"
  36. #include "hda_controller.h"
  37. /* Defines for Nvidia Tegra HDA support */
  38. #define HDA_BAR0 0x8000
  39. #define HDA_CFG_CMD 0x1004
  40. #define HDA_CFG_BAR0 0x1010
  41. #define HDA_ENABLE_IO_SPACE (1 << 0)
  42. #define HDA_ENABLE_MEM_SPACE (1 << 1)
  43. #define HDA_ENABLE_BUS_MASTER (1 << 2)
  44. #define HDA_ENABLE_SERR (1 << 8)
  45. #define HDA_DISABLE_INTR (1 << 10)
  46. #define HDA_BAR0_INIT_PROGRAM 0xFFFFFFFF
  47. #define HDA_BAR0_FINAL_PROGRAM (1 << 14)
  48. /* IPFS */
  49. #define HDA_IPFS_CONFIG 0x180
  50. #define HDA_IPFS_EN_FPCI 0x1
  51. #define HDA_IPFS_FPCI_BAR0 0x80
  52. #define HDA_FPCI_BAR0_START 0x40
  53. #define HDA_IPFS_INTR_MASK 0x188
  54. #define HDA_IPFS_EN_INTR (1 << 16)
  55. /* max number of SDs */
  56. #define NUM_CAPTURE_SD 1
  57. #define NUM_PLAYBACK_SD 1
  58. struct hda_tegra {
  59. struct azx chip;
  60. struct device *dev;
  61. struct clk *hda_clk;
  62. struct clk *hda2codec_2x_clk;
  63. struct clk *hda2hdmi_clk;
  64. void __iomem *regs;
  65. struct work_struct probe_work;
  66. };
  67. #ifdef CONFIG_PM
  68. static int power_save = CONFIG_SND_HDA_POWER_SAVE_DEFAULT;
  69. module_param(power_save, bint, 0644);
  70. MODULE_PARM_DESC(power_save,
  71. "Automatic power-saving timeout (in seconds, 0 = disable).");
  72. #else
  73. #define power_save 0
  74. #endif
  75. /*
  76. * DMA page allocation ops.
  77. */
  78. static int dma_alloc_pages(struct hdac_bus *bus, int type, size_t size,
  79. struct snd_dma_buffer *buf)
  80. {
  81. return snd_dma_alloc_pages(type, bus->dev, size, buf);
  82. }
  83. static void dma_free_pages(struct hdac_bus *bus, struct snd_dma_buffer *buf)
  84. {
  85. snd_dma_free_pages(buf);
  86. }
  87. static int substream_alloc_pages(struct azx *chip,
  88. struct snd_pcm_substream *substream,
  89. size_t size)
  90. {
  91. return snd_pcm_lib_malloc_pages(substream, size);
  92. }
  93. static int substream_free_pages(struct azx *chip,
  94. struct snd_pcm_substream *substream)
  95. {
  96. return snd_pcm_lib_free_pages(substream);
  97. }
  98. /*
  99. * Register access ops. Tegra HDA register access is DWORD only.
  100. */
  101. static void hda_tegra_writel(u32 value, u32 __iomem *addr)
  102. {
  103. writel(value, addr);
  104. }
  105. static u32 hda_tegra_readl(u32 __iomem *addr)
  106. {
  107. return readl(addr);
  108. }
  109. static void hda_tegra_writew(u16 value, u16 __iomem *addr)
  110. {
  111. unsigned int shift = ((unsigned long)(addr) & 0x3) << 3;
  112. void __iomem *dword_addr = (void __iomem *)((unsigned long)(addr) & ~0x3);
  113. u32 v;
  114. v = readl(dword_addr);
  115. v &= ~(0xffff << shift);
  116. v |= value << shift;
  117. writel(v, dword_addr);
  118. }
  119. static u16 hda_tegra_readw(u16 __iomem *addr)
  120. {
  121. unsigned int shift = ((unsigned long)(addr) & 0x3) << 3;
  122. void __iomem *dword_addr = (void __iomem *)((unsigned long)(addr) & ~0x3);
  123. u32 v;
  124. v = readl(dword_addr);
  125. return (v >> shift) & 0xffff;
  126. }
  127. static void hda_tegra_writeb(u8 value, u8 __iomem *addr)
  128. {
  129. unsigned int shift = ((unsigned long)(addr) & 0x3) << 3;
  130. void __iomem *dword_addr = (void __iomem *)((unsigned long)(addr) & ~0x3);
  131. u32 v;
  132. v = readl(dword_addr);
  133. v &= ~(0xff << shift);
  134. v |= value << shift;
  135. writel(v, dword_addr);
  136. }
  137. static u8 hda_tegra_readb(u8 __iomem *addr)
  138. {
  139. unsigned int shift = ((unsigned long)(addr) & 0x3) << 3;
  140. void __iomem *dword_addr = (void __iomem *)((unsigned long)(addr) & ~0x3);
  141. u32 v;
  142. v = readl(dword_addr);
  143. return (v >> shift) & 0xff;
  144. }
  145. static const struct hdac_io_ops hda_tegra_io_ops = {
  146. .reg_writel = hda_tegra_writel,
  147. .reg_readl = hda_tegra_readl,
  148. .reg_writew = hda_tegra_writew,
  149. .reg_readw = hda_tegra_readw,
  150. .reg_writeb = hda_tegra_writeb,
  151. .reg_readb = hda_tegra_readb,
  152. .dma_alloc_pages = dma_alloc_pages,
  153. .dma_free_pages = dma_free_pages,
  154. };
  155. static const struct hda_controller_ops hda_tegra_ops = {
  156. .substream_alloc_pages = substream_alloc_pages,
  157. .substream_free_pages = substream_free_pages,
  158. };
  159. static void hda_tegra_init(struct hda_tegra *hda)
  160. {
  161. u32 v;
  162. /* Enable PCI access */
  163. v = readl(hda->regs + HDA_IPFS_CONFIG);
  164. v |= HDA_IPFS_EN_FPCI;
  165. writel(v, hda->regs + HDA_IPFS_CONFIG);
  166. /* Enable MEM/IO space and bus master */
  167. v = readl(hda->regs + HDA_CFG_CMD);
  168. v &= ~HDA_DISABLE_INTR;
  169. v |= HDA_ENABLE_MEM_SPACE | HDA_ENABLE_IO_SPACE |
  170. HDA_ENABLE_BUS_MASTER | HDA_ENABLE_SERR;
  171. writel(v, hda->regs + HDA_CFG_CMD);
  172. writel(HDA_BAR0_INIT_PROGRAM, hda->regs + HDA_CFG_BAR0);
  173. writel(HDA_BAR0_FINAL_PROGRAM, hda->regs + HDA_CFG_BAR0);
  174. writel(HDA_FPCI_BAR0_START, hda->regs + HDA_IPFS_FPCI_BAR0);
  175. v = readl(hda->regs + HDA_IPFS_INTR_MASK);
  176. v |= HDA_IPFS_EN_INTR;
  177. writel(v, hda->regs + HDA_IPFS_INTR_MASK);
  178. }
  179. static int hda_tegra_enable_clocks(struct hda_tegra *data)
  180. {
  181. int rc;
  182. rc = clk_prepare_enable(data->hda_clk);
  183. if (rc)
  184. return rc;
  185. rc = clk_prepare_enable(data->hda2codec_2x_clk);
  186. if (rc)
  187. goto disable_hda;
  188. rc = clk_prepare_enable(data->hda2hdmi_clk);
  189. if (rc)
  190. goto disable_codec_2x;
  191. return 0;
  192. disable_codec_2x:
  193. clk_disable_unprepare(data->hda2codec_2x_clk);
  194. disable_hda:
  195. clk_disable_unprepare(data->hda_clk);
  196. return rc;
  197. }
  198. #ifdef CONFIG_PM_SLEEP
  199. static void hda_tegra_disable_clocks(struct hda_tegra *data)
  200. {
  201. clk_disable_unprepare(data->hda2hdmi_clk);
  202. clk_disable_unprepare(data->hda2codec_2x_clk);
  203. clk_disable_unprepare(data->hda_clk);
  204. }
  205. /*
  206. * power management
  207. */
  208. static int hda_tegra_suspend(struct device *dev)
  209. {
  210. struct snd_card *card = dev_get_drvdata(dev);
  211. struct azx *chip = card->private_data;
  212. struct hda_tegra *hda = container_of(chip, struct hda_tegra, chip);
  213. struct hdac_bus *bus = azx_bus(chip);
  214. snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
  215. azx_stop_chip(chip);
  216. synchronize_irq(bus->irq);
  217. azx_enter_link_reset(chip);
  218. hda_tegra_disable_clocks(hda);
  219. return 0;
  220. }
  221. static int hda_tegra_resume(struct device *dev)
  222. {
  223. struct snd_card *card = dev_get_drvdata(dev);
  224. struct azx *chip = card->private_data;
  225. struct hda_tegra *hda = container_of(chip, struct hda_tegra, chip);
  226. hda_tegra_enable_clocks(hda);
  227. hda_tegra_init(hda);
  228. azx_init_chip(chip, 1);
  229. snd_power_change_state(card, SNDRV_CTL_POWER_D0);
  230. return 0;
  231. }
  232. #endif /* CONFIG_PM_SLEEP */
  233. static const struct dev_pm_ops hda_tegra_pm = {
  234. SET_SYSTEM_SLEEP_PM_OPS(hda_tegra_suspend, hda_tegra_resume)
  235. };
  236. static int hda_tegra_dev_disconnect(struct snd_device *device)
  237. {
  238. struct azx *chip = device->device_data;
  239. chip->bus.shutdown = 1;
  240. return 0;
  241. }
  242. /*
  243. * destructor
  244. */
  245. static int hda_tegra_dev_free(struct snd_device *device)
  246. {
  247. struct azx *chip = device->device_data;
  248. struct hda_tegra *hda = container_of(chip, struct hda_tegra, chip);
  249. cancel_work_sync(&hda->probe_work);
  250. if (azx_bus(chip)->chip_init) {
  251. azx_stop_all_streams(chip);
  252. azx_stop_chip(chip);
  253. }
  254. azx_free_stream_pages(chip);
  255. azx_free_streams(chip);
  256. snd_hdac_bus_exit(azx_bus(chip));
  257. return 0;
  258. }
  259. static int hda_tegra_init_chip(struct azx *chip, struct platform_device *pdev)
  260. {
  261. struct hda_tegra *hda = container_of(chip, struct hda_tegra, chip);
  262. struct hdac_bus *bus = azx_bus(chip);
  263. struct device *dev = hda->dev;
  264. struct resource *res;
  265. int err;
  266. hda->hda_clk = devm_clk_get(dev, "hda");
  267. if (IS_ERR(hda->hda_clk)) {
  268. dev_err(dev, "failed to get hda clock\n");
  269. return PTR_ERR(hda->hda_clk);
  270. }
  271. hda->hda2codec_2x_clk = devm_clk_get(dev, "hda2codec_2x");
  272. if (IS_ERR(hda->hda2codec_2x_clk)) {
  273. dev_err(dev, "failed to get hda2codec_2x clock\n");
  274. return PTR_ERR(hda->hda2codec_2x_clk);
  275. }
  276. hda->hda2hdmi_clk = devm_clk_get(dev, "hda2hdmi");
  277. if (IS_ERR(hda->hda2hdmi_clk)) {
  278. dev_err(dev, "failed to get hda2hdmi clock\n");
  279. return PTR_ERR(hda->hda2hdmi_clk);
  280. }
  281. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  282. hda->regs = devm_ioremap_resource(dev, res);
  283. if (IS_ERR(hda->regs))
  284. return PTR_ERR(hda->regs);
  285. bus->remap_addr = hda->regs + HDA_BAR0;
  286. bus->addr = res->start + HDA_BAR0;
  287. err = hda_tegra_enable_clocks(hda);
  288. if (err) {
  289. dev_err(dev, "failed to get enable clocks\n");
  290. return err;
  291. }
  292. hda_tegra_init(hda);
  293. return 0;
  294. }
  295. static int hda_tegra_first_init(struct azx *chip, struct platform_device *pdev)
  296. {
  297. struct hdac_bus *bus = azx_bus(chip);
  298. struct snd_card *card = chip->card;
  299. int err;
  300. unsigned short gcap;
  301. int irq_id = platform_get_irq(pdev, 0);
  302. if (irq_id < 0)
  303. return irq_id;
  304. err = hda_tegra_init_chip(chip, pdev);
  305. if (err)
  306. return err;
  307. err = devm_request_irq(chip->card->dev, irq_id, azx_interrupt,
  308. IRQF_SHARED, KBUILD_MODNAME, chip);
  309. if (err) {
  310. dev_err(chip->card->dev,
  311. "unable to request IRQ %d, disabling device\n",
  312. irq_id);
  313. return err;
  314. }
  315. bus->irq = irq_id;
  316. synchronize_irq(bus->irq);
  317. gcap = azx_readw(chip, GCAP);
  318. dev_dbg(card->dev, "chipset global capabilities = 0x%x\n", gcap);
  319. /* read number of streams from GCAP register instead of using
  320. * hardcoded value
  321. */
  322. chip->capture_streams = (gcap >> 8) & 0x0f;
  323. chip->playback_streams = (gcap >> 12) & 0x0f;
  324. if (!chip->playback_streams && !chip->capture_streams) {
  325. /* gcap didn't give any info, switching to old method */
  326. chip->playback_streams = NUM_PLAYBACK_SD;
  327. chip->capture_streams = NUM_CAPTURE_SD;
  328. }
  329. chip->capture_index_offset = 0;
  330. chip->playback_index_offset = chip->capture_streams;
  331. chip->num_streams = chip->playback_streams + chip->capture_streams;
  332. /* initialize streams */
  333. err = azx_init_streams(chip);
  334. if (err < 0) {
  335. dev_err(card->dev, "failed to initialize streams: %d\n", err);
  336. return err;
  337. }
  338. err = azx_alloc_stream_pages(chip);
  339. if (err < 0) {
  340. dev_err(card->dev, "failed to allocate stream pages: %d\n",
  341. err);
  342. return err;
  343. }
  344. /* initialize chip */
  345. azx_init_chip(chip, 1);
  346. /* codec detection */
  347. if (!bus->codec_mask) {
  348. dev_err(card->dev, "no codecs found!\n");
  349. return -ENODEV;
  350. }
  351. strcpy(card->driver, "tegra-hda");
  352. strcpy(card->shortname, "tegra-hda");
  353. snprintf(card->longname, sizeof(card->longname),
  354. "%s at 0x%lx irq %i",
  355. card->shortname, bus->addr, bus->irq);
  356. return 0;
  357. }
  358. /*
  359. * constructor
  360. */
  361. static void hda_tegra_probe_work(struct work_struct *work);
  362. static int hda_tegra_create(struct snd_card *card,
  363. unsigned int driver_caps,
  364. struct hda_tegra *hda)
  365. {
  366. static struct snd_device_ops ops = {
  367. .dev_disconnect = hda_tegra_dev_disconnect,
  368. .dev_free = hda_tegra_dev_free,
  369. };
  370. struct azx *chip;
  371. int err;
  372. chip = &hda->chip;
  373. mutex_init(&chip->open_mutex);
  374. chip->card = card;
  375. chip->ops = &hda_tegra_ops;
  376. chip->driver_caps = driver_caps;
  377. chip->driver_type = driver_caps & 0xff;
  378. chip->dev_index = 0;
  379. INIT_LIST_HEAD(&chip->pcm_list);
  380. chip->codec_probe_mask = -1;
  381. chip->single_cmd = false;
  382. chip->snoop = true;
  383. INIT_WORK(&hda->probe_work, hda_tegra_probe_work);
  384. err = azx_bus_init(chip, NULL, &hda_tegra_io_ops);
  385. if (err < 0)
  386. return err;
  387. chip->bus.needs_damn_long_delay = 1;
  388. err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops);
  389. if (err < 0) {
  390. dev_err(card->dev, "Error creating device\n");
  391. return err;
  392. }
  393. return 0;
  394. }
  395. static const struct of_device_id hda_tegra_match[] = {
  396. { .compatible = "nvidia,tegra30-hda" },
  397. {},
  398. };
  399. MODULE_DEVICE_TABLE(of, hda_tegra_match);
  400. static int hda_tegra_probe(struct platform_device *pdev)
  401. {
  402. const unsigned int driver_flags = AZX_DCAPS_CORBRP_SELF_CLEAR;
  403. struct snd_card *card;
  404. struct azx *chip;
  405. struct hda_tegra *hda;
  406. int err;
  407. hda = devm_kzalloc(&pdev->dev, sizeof(*hda), GFP_KERNEL);
  408. if (!hda)
  409. return -ENOMEM;
  410. hda->dev = &pdev->dev;
  411. chip = &hda->chip;
  412. err = snd_card_new(&pdev->dev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
  413. THIS_MODULE, 0, &card);
  414. if (err < 0) {
  415. dev_err(&pdev->dev, "Error creating card!\n");
  416. return err;
  417. }
  418. err = hda_tegra_create(card, driver_flags, hda);
  419. if (err < 0)
  420. goto out_free;
  421. card->private_data = chip;
  422. dev_set_drvdata(&pdev->dev, card);
  423. schedule_work(&hda->probe_work);
  424. return 0;
  425. out_free:
  426. snd_card_free(card);
  427. return err;
  428. }
  429. static void hda_tegra_probe_work(struct work_struct *work)
  430. {
  431. struct hda_tegra *hda = container_of(work, struct hda_tegra, probe_work);
  432. struct azx *chip = &hda->chip;
  433. struct platform_device *pdev = to_platform_device(hda->dev);
  434. int err;
  435. err = hda_tegra_first_init(chip, pdev);
  436. if (err < 0)
  437. goto out_free;
  438. /* create codec instances */
  439. err = azx_probe_codecs(chip, 0);
  440. if (err < 0)
  441. goto out_free;
  442. err = azx_codec_configure(chip);
  443. if (err < 0)
  444. goto out_free;
  445. err = snd_card_register(chip->card);
  446. if (err < 0)
  447. goto out_free;
  448. chip->running = 1;
  449. snd_hda_set_power_save(&chip->bus, power_save * 1000);
  450. out_free:
  451. return; /* no error return from async probe */
  452. }
  453. static int hda_tegra_remove(struct platform_device *pdev)
  454. {
  455. return snd_card_free(dev_get_drvdata(&pdev->dev));
  456. }
  457. static void hda_tegra_shutdown(struct platform_device *pdev)
  458. {
  459. struct snd_card *card = dev_get_drvdata(&pdev->dev);
  460. struct azx *chip;
  461. if (!card)
  462. return;
  463. chip = card->private_data;
  464. if (chip && chip->running)
  465. azx_stop_chip(chip);
  466. }
  467. static struct platform_driver tegra_platform_hda = {
  468. .driver = {
  469. .name = "tegra-hda",
  470. .pm = &hda_tegra_pm,
  471. .of_match_table = hda_tegra_match,
  472. },
  473. .probe = hda_tegra_probe,
  474. .remove = hda_tegra_remove,
  475. .shutdown = hda_tegra_shutdown,
  476. };
  477. module_platform_driver(tegra_platform_hda);
  478. MODULE_DESCRIPTION("Tegra HDA bus driver");
  479. MODULE_LICENSE("GPL v2");