hda_tegra.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583
  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 *addr)
  102. {
  103. writel(value, addr);
  104. }
  105. static u32 hda_tegra_readl(u32 *addr)
  106. {
  107. return readl(addr);
  108. }
  109. static void hda_tegra_writew(u16 value, u16 *addr)
  110. {
  111. unsigned int shift = ((unsigned long)(addr) & 0x3) << 3;
  112. void *dword_addr = (void *)((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 *addr)
  120. {
  121. unsigned int shift = ((unsigned long)(addr) & 0x3) << 3;
  122. void *dword_addr = (void *)((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 *addr)
  128. {
  129. unsigned int shift = ((unsigned long)(addr) & 0x3) << 3;
  130. void *dword_addr = (void *)((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 *addr)
  138. {
  139. unsigned int shift = ((unsigned long)(addr) & 0x3) << 3;
  140. void *dword_addr = (void *)((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. err = hda_tegra_init_chip(chip, pdev);
  303. if (err)
  304. return err;
  305. err = devm_request_irq(chip->card->dev, irq_id, azx_interrupt,
  306. IRQF_SHARED, KBUILD_MODNAME, chip);
  307. if (err) {
  308. dev_err(chip->card->dev,
  309. "unable to request IRQ %d, disabling device\n",
  310. irq_id);
  311. return err;
  312. }
  313. bus->irq = irq_id;
  314. synchronize_irq(bus->irq);
  315. gcap = azx_readw(chip, GCAP);
  316. dev_dbg(card->dev, "chipset global capabilities = 0x%x\n", gcap);
  317. /* read number of streams from GCAP register instead of using
  318. * hardcoded value
  319. */
  320. chip->capture_streams = (gcap >> 8) & 0x0f;
  321. chip->playback_streams = (gcap >> 12) & 0x0f;
  322. if (!chip->playback_streams && !chip->capture_streams) {
  323. /* gcap didn't give any info, switching to old method */
  324. chip->playback_streams = NUM_PLAYBACK_SD;
  325. chip->capture_streams = NUM_CAPTURE_SD;
  326. }
  327. chip->capture_index_offset = 0;
  328. chip->playback_index_offset = chip->capture_streams;
  329. chip->num_streams = chip->playback_streams + chip->capture_streams;
  330. /* initialize streams */
  331. err = azx_init_streams(chip);
  332. if (err < 0) {
  333. dev_err(card->dev, "failed to initialize streams: %d\n", err);
  334. return err;
  335. }
  336. err = azx_alloc_stream_pages(chip);
  337. if (err < 0) {
  338. dev_err(card->dev, "failed to allocate stream pages: %d\n",
  339. err);
  340. return err;
  341. }
  342. /* initialize chip */
  343. azx_init_chip(chip, 1);
  344. /* codec detection */
  345. if (!bus->codec_mask) {
  346. dev_err(card->dev, "no codecs found!\n");
  347. return -ENODEV;
  348. }
  349. strcpy(card->driver, "tegra-hda");
  350. strcpy(card->shortname, "tegra-hda");
  351. snprintf(card->longname, sizeof(card->longname),
  352. "%s at 0x%lx irq %i",
  353. card->shortname, bus->addr, bus->irq);
  354. return 0;
  355. }
  356. /*
  357. * constructor
  358. */
  359. static void hda_tegra_probe_work(struct work_struct *work);
  360. static int hda_tegra_create(struct snd_card *card,
  361. unsigned int driver_caps,
  362. struct hda_tegra *hda)
  363. {
  364. static struct snd_device_ops ops = {
  365. .dev_disconnect = hda_tegra_dev_disconnect,
  366. .dev_free = hda_tegra_dev_free,
  367. };
  368. struct azx *chip;
  369. int err;
  370. chip = &hda->chip;
  371. mutex_init(&chip->open_mutex);
  372. chip->card = card;
  373. chip->ops = &hda_tegra_ops;
  374. chip->driver_caps = driver_caps;
  375. chip->driver_type = driver_caps & 0xff;
  376. chip->dev_index = 0;
  377. INIT_LIST_HEAD(&chip->pcm_list);
  378. chip->codec_probe_mask = -1;
  379. chip->single_cmd = false;
  380. chip->snoop = true;
  381. INIT_WORK(&hda->probe_work, hda_tegra_probe_work);
  382. err = azx_bus_init(chip, NULL, &hda_tegra_io_ops);
  383. if (err < 0)
  384. return err;
  385. chip->bus.needs_damn_long_delay = 1;
  386. err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops);
  387. if (err < 0) {
  388. dev_err(card->dev, "Error creating device\n");
  389. return err;
  390. }
  391. return 0;
  392. }
  393. static const struct of_device_id hda_tegra_match[] = {
  394. { .compatible = "nvidia,tegra30-hda" },
  395. {},
  396. };
  397. MODULE_DEVICE_TABLE(of, hda_tegra_match);
  398. static int hda_tegra_probe(struct platform_device *pdev)
  399. {
  400. const unsigned int driver_flags = AZX_DCAPS_CORBRP_SELF_CLEAR;
  401. struct snd_card *card;
  402. struct azx *chip;
  403. struct hda_tegra *hda;
  404. int err;
  405. hda = devm_kzalloc(&pdev->dev, sizeof(*hda), GFP_KERNEL);
  406. if (!hda)
  407. return -ENOMEM;
  408. hda->dev = &pdev->dev;
  409. chip = &hda->chip;
  410. err = snd_card_new(&pdev->dev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
  411. THIS_MODULE, 0, &card);
  412. if (err < 0) {
  413. dev_err(&pdev->dev, "Error creating card!\n");
  414. return err;
  415. }
  416. err = hda_tegra_create(card, driver_flags, hda);
  417. if (err < 0)
  418. goto out_free;
  419. card->private_data = chip;
  420. dev_set_drvdata(&pdev->dev, card);
  421. schedule_work(&hda->probe_work);
  422. return 0;
  423. out_free:
  424. snd_card_free(card);
  425. return err;
  426. }
  427. static void hda_tegra_probe_work(struct work_struct *work)
  428. {
  429. struct hda_tegra *hda = container_of(work, struct hda_tegra, probe_work);
  430. struct azx *chip = &hda->chip;
  431. struct platform_device *pdev = to_platform_device(hda->dev);
  432. int err;
  433. err = hda_tegra_first_init(chip, pdev);
  434. if (err < 0)
  435. goto out_free;
  436. /* create codec instances */
  437. err = azx_probe_codecs(chip, 0);
  438. if (err < 0)
  439. goto out_free;
  440. err = azx_codec_configure(chip);
  441. if (err < 0)
  442. goto out_free;
  443. err = snd_card_register(chip->card);
  444. if (err < 0)
  445. goto out_free;
  446. chip->running = 1;
  447. snd_hda_set_power_save(&chip->bus, power_save * 1000);
  448. out_free:
  449. return; /* no error return from async probe */
  450. }
  451. static int hda_tegra_remove(struct platform_device *pdev)
  452. {
  453. return snd_card_free(dev_get_drvdata(&pdev->dev));
  454. }
  455. static void hda_tegra_shutdown(struct platform_device *pdev)
  456. {
  457. struct snd_card *card = dev_get_drvdata(&pdev->dev);
  458. struct azx *chip;
  459. if (!card)
  460. return;
  461. chip = card->private_data;
  462. if (chip && chip->running)
  463. azx_stop_chip(chip);
  464. }
  465. static struct platform_driver tegra_platform_hda = {
  466. .driver = {
  467. .name = "tegra-hda",
  468. .pm = &hda_tegra_pm,
  469. .of_match_table = hda_tegra_match,
  470. },
  471. .probe = hda_tegra_probe,
  472. .remove = hda_tegra_remove,
  473. .shutdown = hda_tegra_shutdown,
  474. };
  475. module_platform_driver(tegra_platform_hda);
  476. MODULE_DESCRIPTION("Tegra HDA bus driver");
  477. MODULE_LICENSE("GPL v2");