hda_tegra.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582
  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. snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
  214. azx_stop_chip(chip);
  215. azx_enter_link_reset(chip);
  216. hda_tegra_disable_clocks(hda);
  217. return 0;
  218. }
  219. static int hda_tegra_resume(struct device *dev)
  220. {
  221. struct snd_card *card = dev_get_drvdata(dev);
  222. struct azx *chip = card->private_data;
  223. struct hda_tegra *hda = container_of(chip, struct hda_tegra, chip);
  224. hda_tegra_enable_clocks(hda);
  225. hda_tegra_init(hda);
  226. azx_init_chip(chip, 1);
  227. snd_power_change_state(card, SNDRV_CTL_POWER_D0);
  228. return 0;
  229. }
  230. #endif /* CONFIG_PM_SLEEP */
  231. static const struct dev_pm_ops hda_tegra_pm = {
  232. SET_SYSTEM_SLEEP_PM_OPS(hda_tegra_suspend, hda_tegra_resume)
  233. };
  234. static int hda_tegra_dev_disconnect(struct snd_device *device)
  235. {
  236. struct azx *chip = device->device_data;
  237. chip->bus.shutdown = 1;
  238. return 0;
  239. }
  240. /*
  241. * destructor
  242. */
  243. static int hda_tegra_dev_free(struct snd_device *device)
  244. {
  245. struct azx *chip = device->device_data;
  246. struct hda_tegra *hda = container_of(chip, struct hda_tegra, chip);
  247. cancel_work_sync(&hda->probe_work);
  248. if (azx_bus(chip)->chip_init) {
  249. azx_stop_all_streams(chip);
  250. azx_stop_chip(chip);
  251. }
  252. azx_free_stream_pages(chip);
  253. azx_free_streams(chip);
  254. snd_hdac_bus_exit(azx_bus(chip));
  255. return 0;
  256. }
  257. static int hda_tegra_init_chip(struct azx *chip, struct platform_device *pdev)
  258. {
  259. struct hda_tegra *hda = container_of(chip, struct hda_tegra, chip);
  260. struct hdac_bus *bus = azx_bus(chip);
  261. struct device *dev = hda->dev;
  262. struct resource *res;
  263. int err;
  264. hda->hda_clk = devm_clk_get(dev, "hda");
  265. if (IS_ERR(hda->hda_clk)) {
  266. dev_err(dev, "failed to get hda clock\n");
  267. return PTR_ERR(hda->hda_clk);
  268. }
  269. hda->hda2codec_2x_clk = devm_clk_get(dev, "hda2codec_2x");
  270. if (IS_ERR(hda->hda2codec_2x_clk)) {
  271. dev_err(dev, "failed to get hda2codec_2x clock\n");
  272. return PTR_ERR(hda->hda2codec_2x_clk);
  273. }
  274. hda->hda2hdmi_clk = devm_clk_get(dev, "hda2hdmi");
  275. if (IS_ERR(hda->hda2hdmi_clk)) {
  276. dev_err(dev, "failed to get hda2hdmi clock\n");
  277. return PTR_ERR(hda->hda2hdmi_clk);
  278. }
  279. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  280. hda->regs = devm_ioremap_resource(dev, res);
  281. if (IS_ERR(hda->regs))
  282. return PTR_ERR(hda->regs);
  283. bus->remap_addr = hda->regs + HDA_BAR0;
  284. bus->addr = res->start + HDA_BAR0;
  285. err = hda_tegra_enable_clocks(hda);
  286. if (err) {
  287. dev_err(dev, "failed to get enable clocks\n");
  288. return err;
  289. }
  290. hda_tegra_init(hda);
  291. return 0;
  292. }
  293. static int hda_tegra_first_init(struct azx *chip, struct platform_device *pdev)
  294. {
  295. struct hdac_bus *bus = azx_bus(chip);
  296. struct snd_card *card = chip->card;
  297. int err;
  298. unsigned short gcap;
  299. int irq_id = platform_get_irq(pdev, 0);
  300. err = hda_tegra_init_chip(chip, pdev);
  301. if (err)
  302. return err;
  303. err = devm_request_irq(chip->card->dev, irq_id, azx_interrupt,
  304. IRQF_SHARED, KBUILD_MODNAME, chip);
  305. if (err) {
  306. dev_err(chip->card->dev,
  307. "unable to request IRQ %d, disabling device\n",
  308. irq_id);
  309. return err;
  310. }
  311. bus->irq = irq_id;
  312. synchronize_irq(bus->irq);
  313. gcap = azx_readw(chip, GCAP);
  314. dev_dbg(card->dev, "chipset global capabilities = 0x%x\n", gcap);
  315. /* read number of streams from GCAP register instead of using
  316. * hardcoded value
  317. */
  318. chip->capture_streams = (gcap >> 8) & 0x0f;
  319. chip->playback_streams = (gcap >> 12) & 0x0f;
  320. if (!chip->playback_streams && !chip->capture_streams) {
  321. /* gcap didn't give any info, switching to old method */
  322. chip->playback_streams = NUM_PLAYBACK_SD;
  323. chip->capture_streams = NUM_CAPTURE_SD;
  324. }
  325. chip->capture_index_offset = 0;
  326. chip->playback_index_offset = chip->capture_streams;
  327. chip->num_streams = chip->playback_streams + chip->capture_streams;
  328. /* initialize streams */
  329. err = azx_init_streams(chip);
  330. if (err < 0) {
  331. dev_err(card->dev, "failed to initialize streams: %d\n", err);
  332. return err;
  333. }
  334. err = azx_alloc_stream_pages(chip);
  335. if (err < 0) {
  336. dev_err(card->dev, "failed to allocate stream pages: %d\n",
  337. err);
  338. return err;
  339. }
  340. /* initialize chip */
  341. azx_init_chip(chip, 1);
  342. /* codec detection */
  343. if (!bus->codec_mask) {
  344. dev_err(card->dev, "no codecs found!\n");
  345. return -ENODEV;
  346. }
  347. strcpy(card->driver, "tegra-hda");
  348. strcpy(card->shortname, "tegra-hda");
  349. snprintf(card->longname, sizeof(card->longname),
  350. "%s at 0x%lx irq %i",
  351. card->shortname, bus->addr, bus->irq);
  352. return 0;
  353. }
  354. /*
  355. * constructor
  356. */
  357. static void hda_tegra_probe_work(struct work_struct *work);
  358. static int hda_tegra_create(struct snd_card *card,
  359. unsigned int driver_caps,
  360. struct hda_tegra *hda)
  361. {
  362. static struct snd_device_ops ops = {
  363. .dev_disconnect = hda_tegra_dev_disconnect,
  364. .dev_free = hda_tegra_dev_free,
  365. };
  366. struct azx *chip;
  367. int err;
  368. chip = &hda->chip;
  369. mutex_init(&chip->open_mutex);
  370. chip->card = card;
  371. chip->ops = &hda_tegra_ops;
  372. chip->driver_caps = driver_caps;
  373. chip->driver_type = driver_caps & 0xff;
  374. chip->dev_index = 0;
  375. INIT_LIST_HEAD(&chip->pcm_list);
  376. chip->codec_probe_mask = -1;
  377. chip->single_cmd = false;
  378. chip->snoop = true;
  379. INIT_WORK(&hda->probe_work, hda_tegra_probe_work);
  380. err = azx_bus_init(chip, NULL, &hda_tegra_io_ops);
  381. if (err < 0)
  382. return err;
  383. chip->bus.needs_damn_long_delay = 1;
  384. err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops);
  385. if (err < 0) {
  386. dev_err(card->dev, "Error creating device\n");
  387. return err;
  388. }
  389. return 0;
  390. }
  391. static const struct of_device_id hda_tegra_match[] = {
  392. { .compatible = "nvidia,tegra30-hda" },
  393. {},
  394. };
  395. MODULE_DEVICE_TABLE(of, hda_tegra_match);
  396. static int hda_tegra_probe(struct platform_device *pdev)
  397. {
  398. const unsigned int driver_flags = AZX_DCAPS_CORBRP_SELF_CLEAR;
  399. struct snd_card *card;
  400. struct azx *chip;
  401. struct hda_tegra *hda;
  402. int err;
  403. hda = devm_kzalloc(&pdev->dev, sizeof(*hda), GFP_KERNEL);
  404. if (!hda)
  405. return -ENOMEM;
  406. hda->dev = &pdev->dev;
  407. chip = &hda->chip;
  408. err = snd_card_new(&pdev->dev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
  409. THIS_MODULE, 0, &card);
  410. if (err < 0) {
  411. dev_err(&pdev->dev, "Error creating card!\n");
  412. return err;
  413. }
  414. err = hda_tegra_create(card, driver_flags, hda);
  415. if (err < 0)
  416. goto out_free;
  417. card->private_data = chip;
  418. dev_set_drvdata(&pdev->dev, card);
  419. schedule_work(&hda->probe_work);
  420. return 0;
  421. out_free:
  422. snd_card_free(card);
  423. return err;
  424. }
  425. static void hda_tegra_probe_work(struct work_struct *work)
  426. {
  427. struct hda_tegra *hda = container_of(work, struct hda_tegra, probe_work);
  428. struct azx *chip = &hda->chip;
  429. struct platform_device *pdev = to_platform_device(hda->dev);
  430. int err;
  431. err = hda_tegra_first_init(chip, pdev);
  432. if (err < 0)
  433. goto out_free;
  434. /* create codec instances */
  435. err = azx_probe_codecs(chip, 0);
  436. if (err < 0)
  437. goto out_free;
  438. err = azx_codec_configure(chip);
  439. if (err < 0)
  440. goto out_free;
  441. err = snd_card_register(chip->card);
  442. if (err < 0)
  443. goto out_free;
  444. chip->running = 1;
  445. snd_hda_set_power_save(&chip->bus, power_save * 1000);
  446. out_free:
  447. return; /* no error return from async probe */
  448. }
  449. static int hda_tegra_remove(struct platform_device *pdev)
  450. {
  451. return snd_card_free(dev_get_drvdata(&pdev->dev));
  452. }
  453. static void hda_tegra_shutdown(struct platform_device *pdev)
  454. {
  455. struct snd_card *card = dev_get_drvdata(&pdev->dev);
  456. struct azx *chip;
  457. if (!card)
  458. return;
  459. chip = card->private_data;
  460. if (chip && chip->running)
  461. azx_stop_chip(chip);
  462. }
  463. static struct platform_driver tegra_platform_hda = {
  464. .driver = {
  465. .name = "tegra-hda",
  466. .pm = &hda_tegra_pm,
  467. .of_match_table = hda_tegra_match,
  468. },
  469. .probe = hda_tegra_probe,
  470. .remove = hda_tegra_remove,
  471. .shutdown = hda_tegra_shutdown,
  472. };
  473. module_platform_driver(tegra_platform_hda);
  474. MODULE_DESCRIPTION("Tegra HDA bus driver");
  475. MODULE_LICENSE("GPL v2");