hda_tegra.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561
  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. };
  66. #ifdef CONFIG_PM
  67. static int power_save = CONFIG_SND_HDA_POWER_SAVE_DEFAULT;
  68. module_param(power_save, bint, 0644);
  69. MODULE_PARM_DESC(power_save,
  70. "Automatic power-saving timeout (in seconds, 0 = disable).");
  71. #else
  72. #define power_save 0
  73. #endif
  74. /*
  75. * DMA page allocation ops.
  76. */
  77. static int dma_alloc_pages(struct hdac_bus *bus, int type, size_t size,
  78. struct snd_dma_buffer *buf)
  79. {
  80. return snd_dma_alloc_pages(type, bus->dev, size, buf);
  81. }
  82. static void dma_free_pages(struct hdac_bus *bus, struct snd_dma_buffer *buf)
  83. {
  84. snd_dma_free_pages(buf);
  85. }
  86. static int substream_alloc_pages(struct azx *chip,
  87. struct snd_pcm_substream *substream,
  88. size_t size)
  89. {
  90. return snd_pcm_lib_malloc_pages(substream, size);
  91. }
  92. static int substream_free_pages(struct azx *chip,
  93. struct snd_pcm_substream *substream)
  94. {
  95. return snd_pcm_lib_free_pages(substream);
  96. }
  97. /*
  98. * Register access ops. Tegra HDA register access is DWORD only.
  99. */
  100. static void hda_tegra_writel(u32 value, u32 *addr)
  101. {
  102. writel(value, addr);
  103. }
  104. static u32 hda_tegra_readl(u32 *addr)
  105. {
  106. return readl(addr);
  107. }
  108. static void hda_tegra_writew(u16 value, u16 *addr)
  109. {
  110. unsigned int shift = ((unsigned long)(addr) & 0x3) << 3;
  111. void *dword_addr = (void *)((unsigned long)(addr) & ~0x3);
  112. u32 v;
  113. v = readl(dword_addr);
  114. v &= ~(0xffff << shift);
  115. v |= value << shift;
  116. writel(v, dword_addr);
  117. }
  118. static u16 hda_tegra_readw(u16 *addr)
  119. {
  120. unsigned int shift = ((unsigned long)(addr) & 0x3) << 3;
  121. void *dword_addr = (void *)((unsigned long)(addr) & ~0x3);
  122. u32 v;
  123. v = readl(dword_addr);
  124. return (v >> shift) & 0xffff;
  125. }
  126. static void hda_tegra_writeb(u8 value, u8 *addr)
  127. {
  128. unsigned int shift = ((unsigned long)(addr) & 0x3) << 3;
  129. void *dword_addr = (void *)((unsigned long)(addr) & ~0x3);
  130. u32 v;
  131. v = readl(dword_addr);
  132. v &= ~(0xff << shift);
  133. v |= value << shift;
  134. writel(v, dword_addr);
  135. }
  136. static u8 hda_tegra_readb(u8 *addr)
  137. {
  138. unsigned int shift = ((unsigned long)(addr) & 0x3) << 3;
  139. void *dword_addr = (void *)((unsigned long)(addr) & ~0x3);
  140. u32 v;
  141. v = readl(dword_addr);
  142. return (v >> shift) & 0xff;
  143. }
  144. static const struct hdac_io_ops hda_tegra_io_ops = {
  145. .reg_writel = hda_tegra_writel,
  146. .reg_readl = hda_tegra_readl,
  147. .reg_writew = hda_tegra_writew,
  148. .reg_readw = hda_tegra_readw,
  149. .reg_writeb = hda_tegra_writeb,
  150. .reg_readb = hda_tegra_readb,
  151. .dma_alloc_pages = dma_alloc_pages,
  152. .dma_free_pages = dma_free_pages,
  153. };
  154. static const struct hda_controller_ops hda_tegra_ops = {
  155. .substream_alloc_pages = substream_alloc_pages,
  156. .substream_free_pages = substream_free_pages,
  157. };
  158. static void hda_tegra_init(struct hda_tegra *hda)
  159. {
  160. u32 v;
  161. /* Enable PCI access */
  162. v = readl(hda->regs + HDA_IPFS_CONFIG);
  163. v |= HDA_IPFS_EN_FPCI;
  164. writel(v, hda->regs + HDA_IPFS_CONFIG);
  165. /* Enable MEM/IO space and bus master */
  166. v = readl(hda->regs + HDA_CFG_CMD);
  167. v &= ~HDA_DISABLE_INTR;
  168. v |= HDA_ENABLE_MEM_SPACE | HDA_ENABLE_IO_SPACE |
  169. HDA_ENABLE_BUS_MASTER | HDA_ENABLE_SERR;
  170. writel(v, hda->regs + HDA_CFG_CMD);
  171. writel(HDA_BAR0_INIT_PROGRAM, hda->regs + HDA_CFG_BAR0);
  172. writel(HDA_BAR0_FINAL_PROGRAM, hda->regs + HDA_CFG_BAR0);
  173. writel(HDA_FPCI_BAR0_START, hda->regs + HDA_IPFS_FPCI_BAR0);
  174. v = readl(hda->regs + HDA_IPFS_INTR_MASK);
  175. v |= HDA_IPFS_EN_INTR;
  176. writel(v, hda->regs + HDA_IPFS_INTR_MASK);
  177. }
  178. static int hda_tegra_enable_clocks(struct hda_tegra *data)
  179. {
  180. int rc;
  181. rc = clk_prepare_enable(data->hda_clk);
  182. if (rc)
  183. return rc;
  184. rc = clk_prepare_enable(data->hda2codec_2x_clk);
  185. if (rc)
  186. goto disable_hda;
  187. rc = clk_prepare_enable(data->hda2hdmi_clk);
  188. if (rc)
  189. goto disable_codec_2x;
  190. return 0;
  191. disable_codec_2x:
  192. clk_disable_unprepare(data->hda2codec_2x_clk);
  193. disable_hda:
  194. clk_disable_unprepare(data->hda_clk);
  195. return rc;
  196. }
  197. #ifdef CONFIG_PM_SLEEP
  198. static void hda_tegra_disable_clocks(struct hda_tegra *data)
  199. {
  200. clk_disable_unprepare(data->hda2hdmi_clk);
  201. clk_disable_unprepare(data->hda2codec_2x_clk);
  202. clk_disable_unprepare(data->hda_clk);
  203. }
  204. /*
  205. * power management
  206. */
  207. static int hda_tegra_suspend(struct device *dev)
  208. {
  209. struct snd_card *card = dev_get_drvdata(dev);
  210. struct azx *chip = card->private_data;
  211. struct hda_tegra *hda = container_of(chip, struct hda_tegra, chip);
  212. snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
  213. azx_stop_chip(chip);
  214. azx_enter_link_reset(chip);
  215. hda_tegra_disable_clocks(hda);
  216. return 0;
  217. }
  218. static int hda_tegra_resume(struct device *dev)
  219. {
  220. struct snd_card *card = dev_get_drvdata(dev);
  221. struct azx *chip = card->private_data;
  222. struct hda_tegra *hda = container_of(chip, struct hda_tegra, chip);
  223. hda_tegra_enable_clocks(hda);
  224. hda_tegra_init(hda);
  225. azx_init_chip(chip, 1);
  226. snd_power_change_state(card, SNDRV_CTL_POWER_D0);
  227. return 0;
  228. }
  229. #endif /* CONFIG_PM_SLEEP */
  230. static const struct dev_pm_ops hda_tegra_pm = {
  231. SET_SYSTEM_SLEEP_PM_OPS(hda_tegra_suspend, hda_tegra_resume)
  232. };
  233. static int hda_tegra_dev_disconnect(struct snd_device *device)
  234. {
  235. struct azx *chip = device->device_data;
  236. chip->bus.shutdown = 1;
  237. return 0;
  238. }
  239. /*
  240. * destructor
  241. */
  242. static int hda_tegra_dev_free(struct snd_device *device)
  243. {
  244. struct azx *chip = device->device_data;
  245. if (azx_bus(chip)->chip_init) {
  246. azx_stop_all_streams(chip);
  247. azx_stop_chip(chip);
  248. }
  249. azx_free_stream_pages(chip);
  250. azx_free_streams(chip);
  251. snd_hdac_bus_exit(azx_bus(chip));
  252. return 0;
  253. }
  254. static int hda_tegra_init_chip(struct azx *chip, struct platform_device *pdev)
  255. {
  256. struct hda_tegra *hda = container_of(chip, struct hda_tegra, chip);
  257. struct hdac_bus *bus = azx_bus(chip);
  258. struct device *dev = hda->dev;
  259. struct resource *res;
  260. int err;
  261. hda->hda_clk = devm_clk_get(dev, "hda");
  262. if (IS_ERR(hda->hda_clk)) {
  263. dev_err(dev, "failed to get hda clock\n");
  264. return PTR_ERR(hda->hda_clk);
  265. }
  266. hda->hda2codec_2x_clk = devm_clk_get(dev, "hda2codec_2x");
  267. if (IS_ERR(hda->hda2codec_2x_clk)) {
  268. dev_err(dev, "failed to get hda2codec_2x clock\n");
  269. return PTR_ERR(hda->hda2codec_2x_clk);
  270. }
  271. hda->hda2hdmi_clk = devm_clk_get(dev, "hda2hdmi");
  272. if (IS_ERR(hda->hda2hdmi_clk)) {
  273. dev_err(dev, "failed to get hda2hdmi clock\n");
  274. return PTR_ERR(hda->hda2hdmi_clk);
  275. }
  276. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  277. hda->regs = devm_ioremap_resource(dev, res);
  278. if (IS_ERR(hda->regs))
  279. return PTR_ERR(hda->regs);
  280. bus->remap_addr = hda->regs + HDA_BAR0;
  281. bus->addr = res->start + HDA_BAR0;
  282. err = hda_tegra_enable_clocks(hda);
  283. if (err) {
  284. dev_err(dev, "failed to get enable clocks\n");
  285. return err;
  286. }
  287. hda_tegra_init(hda);
  288. return 0;
  289. }
  290. static int hda_tegra_first_init(struct azx *chip, struct platform_device *pdev)
  291. {
  292. struct hdac_bus *bus = azx_bus(chip);
  293. struct snd_card *card = chip->card;
  294. int err;
  295. unsigned short gcap;
  296. int irq_id = platform_get_irq(pdev, 0);
  297. err = hda_tegra_init_chip(chip, pdev);
  298. if (err)
  299. return err;
  300. err = devm_request_irq(chip->card->dev, irq_id, azx_interrupt,
  301. IRQF_SHARED, KBUILD_MODNAME, chip);
  302. if (err) {
  303. dev_err(chip->card->dev,
  304. "unable to request IRQ %d, disabling device\n",
  305. irq_id);
  306. return err;
  307. }
  308. bus->irq = irq_id;
  309. synchronize_irq(bus->irq);
  310. gcap = azx_readw(chip, GCAP);
  311. dev_dbg(card->dev, "chipset global capabilities = 0x%x\n", gcap);
  312. /* read number of streams from GCAP register instead of using
  313. * hardcoded value
  314. */
  315. chip->capture_streams = (gcap >> 8) & 0x0f;
  316. chip->playback_streams = (gcap >> 12) & 0x0f;
  317. if (!chip->playback_streams && !chip->capture_streams) {
  318. /* gcap didn't give any info, switching to old method */
  319. chip->playback_streams = NUM_PLAYBACK_SD;
  320. chip->capture_streams = NUM_CAPTURE_SD;
  321. }
  322. chip->capture_index_offset = 0;
  323. chip->playback_index_offset = chip->capture_streams;
  324. chip->num_streams = chip->playback_streams + chip->capture_streams;
  325. /* initialize streams */
  326. err = azx_init_streams(chip);
  327. if (err < 0) {
  328. dev_err(card->dev, "failed to initialize streams: %d\n", err);
  329. return err;
  330. }
  331. err = azx_alloc_stream_pages(chip);
  332. if (err < 0) {
  333. dev_err(card->dev, "failed to allocate stream pages: %d\n",
  334. err);
  335. return err;
  336. }
  337. /* initialize chip */
  338. azx_init_chip(chip, 1);
  339. /* codec detection */
  340. if (!bus->codec_mask) {
  341. dev_err(card->dev, "no codecs found!\n");
  342. return -ENODEV;
  343. }
  344. strcpy(card->driver, "tegra-hda");
  345. strcpy(card->shortname, "tegra-hda");
  346. snprintf(card->longname, sizeof(card->longname),
  347. "%s at 0x%lx irq %i",
  348. card->shortname, bus->addr, bus->irq);
  349. return 0;
  350. }
  351. /*
  352. * constructor
  353. */
  354. static int hda_tegra_create(struct snd_card *card,
  355. unsigned int driver_caps,
  356. struct hda_tegra *hda)
  357. {
  358. static struct snd_device_ops ops = {
  359. .dev_disconnect = hda_tegra_dev_disconnect,
  360. .dev_free = hda_tegra_dev_free,
  361. };
  362. struct azx *chip;
  363. int err;
  364. chip = &hda->chip;
  365. mutex_init(&chip->open_mutex);
  366. chip->card = card;
  367. chip->ops = &hda_tegra_ops;
  368. chip->driver_caps = driver_caps;
  369. chip->driver_type = driver_caps & 0xff;
  370. chip->dev_index = 0;
  371. INIT_LIST_HEAD(&chip->pcm_list);
  372. chip->codec_probe_mask = -1;
  373. chip->single_cmd = false;
  374. chip->snoop = true;
  375. err = azx_bus_init(chip, NULL, &hda_tegra_io_ops);
  376. if (err < 0)
  377. return err;
  378. err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops);
  379. if (err < 0) {
  380. dev_err(card->dev, "Error creating device\n");
  381. return err;
  382. }
  383. return 0;
  384. }
  385. static const struct of_device_id hda_tegra_match[] = {
  386. { .compatible = "nvidia,tegra30-hda" },
  387. {},
  388. };
  389. MODULE_DEVICE_TABLE(of, hda_tegra_match);
  390. static int hda_tegra_probe(struct platform_device *pdev)
  391. {
  392. const unsigned int driver_flags = AZX_DCAPS_RIRB_DELAY |
  393. AZX_DCAPS_CORBRP_SELF_CLEAR;
  394. struct snd_card *card;
  395. struct azx *chip;
  396. struct hda_tegra *hda;
  397. int err;
  398. hda = devm_kzalloc(&pdev->dev, sizeof(*hda), GFP_KERNEL);
  399. if (!hda)
  400. return -ENOMEM;
  401. hda->dev = &pdev->dev;
  402. chip = &hda->chip;
  403. err = snd_card_new(&pdev->dev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
  404. THIS_MODULE, 0, &card);
  405. if (err < 0) {
  406. dev_err(&pdev->dev, "Error creating card!\n");
  407. return err;
  408. }
  409. err = hda_tegra_create(card, driver_flags, hda);
  410. if (err < 0)
  411. goto out_free;
  412. card->private_data = chip;
  413. dev_set_drvdata(&pdev->dev, card);
  414. err = hda_tegra_first_init(chip, pdev);
  415. if (err < 0)
  416. goto out_free;
  417. /* create codec instances */
  418. err = azx_probe_codecs(chip, 0);
  419. if (err < 0)
  420. goto out_free;
  421. err = azx_codec_configure(chip);
  422. if (err < 0)
  423. goto out_free;
  424. err = snd_card_register(chip->card);
  425. if (err < 0)
  426. goto out_free;
  427. chip->running = 1;
  428. snd_hda_set_power_save(&chip->bus, power_save * 1000);
  429. return 0;
  430. out_free:
  431. snd_card_free(card);
  432. return err;
  433. }
  434. static int hda_tegra_remove(struct platform_device *pdev)
  435. {
  436. return snd_card_free(dev_get_drvdata(&pdev->dev));
  437. }
  438. static void hda_tegra_shutdown(struct platform_device *pdev)
  439. {
  440. struct snd_card *card = dev_get_drvdata(&pdev->dev);
  441. struct azx *chip;
  442. if (!card)
  443. return;
  444. chip = card->private_data;
  445. if (chip && chip->running)
  446. azx_stop_chip(chip);
  447. }
  448. static struct platform_driver tegra_platform_hda = {
  449. .driver = {
  450. .name = "tegra-hda",
  451. .pm = &hda_tegra_pm,
  452. .of_match_table = hda_tegra_match,
  453. },
  454. .probe = hda_tegra_probe,
  455. .remove = hda_tegra_remove,
  456. .shutdown = hda_tegra_shutdown,
  457. };
  458. module_platform_driver(tegra_platform_hda);
  459. MODULE_DESCRIPTION("Tegra HDA bus driver");
  460. MODULE_LICENSE("GPL v2");