cix-ipbloq.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. // SPDX-License-Identifier: GPL-2.0
  2. // Copyright 2025 Cix Technology Group Co., Ltd.
  3. #include <linux/clk.h>
  4. #include <linux/interrupt.h>
  5. #include <linux/io.h>
  6. #include <linux/kernel.h>
  7. #include <linux/module.h>
  8. #include <linux/mutex.h>
  9. #include <linux/of.h>
  10. #include <linux/of_reserved_mem.h>
  11. #include <linux/platform_device.h>
  12. #include <linux/pm_runtime.h>
  13. #include <linux/reset.h>
  14. #include <linux/string.h>
  15. #include <sound/hda_codec.h>
  16. #include "hda_controller.h"
  17. #define CIX_IPBLOQ_JACKPOLL_DEFAULT_TIME_MS 1000
  18. #define CIX_IPBLOQ_POWER_SAVE_DEFAULT_TIME_MS 100
  19. #define CIX_IPBLOQ_SKY1_ADDR_HOST_TO_HDAC_OFFSET (-0x90000000ULL)
  20. struct cix_ipbloq_hda {
  21. struct azx chip;
  22. struct device *dev;
  23. void __iomem *regs;
  24. struct reset_control *reset;
  25. struct clk_bulk_data clocks[2];
  26. unsigned int nclocks;
  27. };
  28. static const struct hda_controller_ops cix_ipbloq_hda_ops;
  29. static int cix_ipbloq_hda_dev_disconnect(struct snd_device *device)
  30. {
  31. struct azx *chip = device->device_data;
  32. chip->bus.shutdown = 1;
  33. return 0;
  34. }
  35. static int cix_ipbloq_hda_dev_free(struct snd_device *device)
  36. {
  37. struct azx *chip = device->device_data;
  38. if (azx_bus(chip)->chip_init) {
  39. azx_stop_all_streams(chip);
  40. azx_stop_chip(chip);
  41. }
  42. azx_free_stream_pages(chip);
  43. azx_free_streams(chip);
  44. snd_hdac_bus_exit(azx_bus(chip));
  45. return 0;
  46. }
  47. static int cix_ipbloq_hda_probe_codec(struct cix_ipbloq_hda *hda)
  48. {
  49. struct azx *chip = &hda->chip;
  50. struct hdac_bus *bus = azx_bus(chip);
  51. int err;
  52. to_hda_bus(bus)->bus_probing = 1;
  53. /* create codec instances */
  54. err = azx_probe_codecs(chip, 8);
  55. if (err < 0) {
  56. dev_err(hda->dev, "probe codecs failed: %d\n", err);
  57. return err;
  58. }
  59. err = azx_codec_configure(chip);
  60. if (err < 0) {
  61. dev_err(hda->dev, "codec configure failed: %d\n", err);
  62. return err;
  63. }
  64. err = snd_card_register(chip->card);
  65. if (err < 0) {
  66. dev_err(hda->dev, "card register failed: %d\n", err);
  67. return err;
  68. }
  69. chip->running = 1;
  70. to_hda_bus(bus)->bus_probing = 0;
  71. snd_hda_set_power_save(&chip->bus, CIX_IPBLOQ_POWER_SAVE_DEFAULT_TIME_MS);
  72. return 0;
  73. }
  74. static int cix_ipbloq_hda_init(struct cix_ipbloq_hda *hda,
  75. struct azx *chip,
  76. struct platform_device *pdev)
  77. {
  78. const char *sname = NULL, *drv_name = "cix-ipbloq-hda";
  79. struct hdac_bus *bus = azx_bus(chip);
  80. struct snd_card *card = chip->card;
  81. struct resource *res;
  82. unsigned short gcap;
  83. int irq_id, err;
  84. hda->regs = devm_platform_get_and_ioremap_resource(pdev, 0, &res);
  85. if (IS_ERR(hda->regs)) {
  86. dev_err(hda->dev, "failed to get and ioremap resource\n");
  87. return PTR_ERR(hda->regs);
  88. }
  89. bus->remap_addr = hda->regs;
  90. bus->addr = res->start;
  91. irq_id = platform_get_irq(pdev, 0);
  92. if (irq_id < 0)
  93. return irq_id;
  94. err = devm_request_irq(hda->dev, irq_id, azx_interrupt,
  95. 0, KBUILD_MODNAME, chip);
  96. if (err < 0)
  97. return dev_err_probe(hda->dev, err,
  98. "unable to request IRQ %d : err = %d\n", irq_id, err);
  99. bus->irq = irq_id;
  100. card->sync_irq = bus->irq;
  101. gcap = azx_readw(chip, GCAP);
  102. chip->capture_streams = (gcap >> 8) & 0x0f;
  103. chip->playback_streams = (gcap >> 12) & 0x0f;
  104. chip->capture_index_offset = 0;
  105. chip->playback_index_offset = chip->capture_streams;
  106. chip->num_streams = chip->playback_streams + chip->capture_streams;
  107. /* initialize streams */
  108. err = azx_init_streams(chip);
  109. if (err < 0) {
  110. dev_err(hda->dev, "failed to initialize streams: %d\n", err);
  111. return err;
  112. }
  113. err = azx_alloc_stream_pages(chip);
  114. if (err < 0) {
  115. dev_err(hda->dev, "failed to allocate stream pages: %d\n", err);
  116. return err;
  117. }
  118. /* initialize chip */
  119. azx_init_chip(chip, 1);
  120. /* codec detection */
  121. if (!bus->codec_mask) {
  122. dev_err(hda->dev, "no codecs found\n");
  123. return -ENODEV;
  124. }
  125. dev_dbg(card->dev, "codec detection mask = 0x%lx\n", bus->codec_mask);
  126. /* driver name */
  127. strscpy(card->driver, drv_name, sizeof(card->driver));
  128. /* shortname for card */
  129. sname = of_get_property(pdev->dev.of_node, "model", NULL);
  130. if (!sname)
  131. sname = drv_name;
  132. if (strlen(sname) > sizeof(card->shortname))
  133. dev_dbg(card->dev, "truncating shortname for card\n");
  134. strscpy(card->shortname, sname, sizeof(card->shortname));
  135. /* longname for card */
  136. snprintf(card->longname, sizeof(card->longname),
  137. "%s at 0x%lx irq %i",
  138. card->shortname, bus->addr, bus->irq);
  139. return 0;
  140. }
  141. static int cix_ipbloq_hda_create(struct cix_ipbloq_hda *hda,
  142. struct snd_card *card,
  143. unsigned int driver_caps)
  144. {
  145. static const struct snd_device_ops ops = {
  146. .dev_disconnect = cix_ipbloq_hda_dev_disconnect,
  147. .dev_free = cix_ipbloq_hda_dev_free,
  148. };
  149. struct azx *chip;
  150. int err;
  151. chip = &hda->chip;
  152. chip->card = card;
  153. chip->ops = &cix_ipbloq_hda_ops;
  154. chip->driver_caps = driver_caps;
  155. chip->driver_type = driver_caps & 0xff;
  156. chip->dev_index = 0;
  157. chip->single_cmd = 0;
  158. chip->codec_probe_mask = -1;
  159. chip->align_buffer_size = 1;
  160. chip->jackpoll_interval = msecs_to_jiffies(CIX_IPBLOQ_JACKPOLL_DEFAULT_TIME_MS);
  161. mutex_init(&chip->open_mutex);
  162. INIT_LIST_HEAD(&chip->pcm_list);
  163. /*
  164. * HD-audio controllers appear pretty inaccurate about the update-IRQ timing.
  165. * The IRQ is issued before actually the data is processed. So use stream
  166. * link position by default instead of dma position buffer.
  167. */
  168. chip->get_position[0] = chip->get_position[1] = azx_get_pos_lpib;
  169. err = azx_bus_init(chip, NULL);
  170. if (err < 0) {
  171. dev_err(hda->dev, "failed to init bus, err = %d\n", err);
  172. return err;
  173. }
  174. /* RIRBSTS.RINTFL cannot be cleared, cause interrupt storm */
  175. chip->bus.core.polling_mode = 1;
  176. chip->bus.core.not_use_interrupts = 1;
  177. chip->bus.core.aligned_mmio = 1;
  178. chip->bus.core.dma_stop_delay = 100;
  179. chip->bus.core.addr_offset = (dma_addr_t)CIX_IPBLOQ_SKY1_ADDR_HOST_TO_HDAC_OFFSET;
  180. chip->bus.jackpoll_in_suspend = 1;
  181. err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops);
  182. if (err < 0) {
  183. dev_err(card->dev, "failed to create device, err = %d\n", err);
  184. return err;
  185. }
  186. return 0;
  187. }
  188. static int cix_ipbloq_hda_probe(struct platform_device *pdev)
  189. {
  190. const unsigned int driver_flags = AZX_DCAPS_PM_RUNTIME;
  191. struct cix_ipbloq_hda *hda;
  192. struct snd_card *card;
  193. struct azx *chip;
  194. int err;
  195. hda = devm_kzalloc(&pdev->dev, sizeof(*hda), GFP_KERNEL);
  196. if (!hda)
  197. return -ENOMEM;
  198. hda->dev = &pdev->dev;
  199. hda->reset = devm_reset_control_get(hda->dev, NULL);
  200. if (IS_ERR(hda->reset))
  201. return dev_err_probe(hda->dev, PTR_ERR(hda->reset),
  202. "failed to get reset, err = %ld\n", PTR_ERR(hda->reset));
  203. hda->clocks[hda->nclocks++].id = "ipg";
  204. hda->clocks[hda->nclocks++].id = "per";
  205. err = devm_clk_bulk_get(hda->dev, hda->nclocks, hda->clocks);
  206. if (err < 0)
  207. return dev_err_probe(hda->dev, err, "failed to get clk, err = %d\n", err);
  208. dma_set_mask_and_coherent(hda->dev, DMA_BIT_MASK(32));
  209. err = of_reserved_mem_device_init(hda->dev);
  210. if (err < 0 && err != -ENODEV) {
  211. dev_err(hda->dev,
  212. "failed to init reserved mem for DMA, err = %d\n", err);
  213. return err;
  214. }
  215. err = snd_card_new(hda->dev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
  216. THIS_MODULE, 0, &card);
  217. if (err < 0)
  218. return dev_err_probe(hda->dev, err, "failed to crate card, err = %d\n", err);
  219. err = cix_ipbloq_hda_create(hda, card, driver_flags);
  220. if (err < 0)
  221. goto out_free_card;
  222. chip = &hda->chip;
  223. card->private_data = chip;
  224. dev_set_drvdata(hda->dev, card);
  225. pm_runtime_enable(hda->dev);
  226. if (!azx_has_pm_runtime(chip))
  227. pm_runtime_forbid(hda->dev);
  228. err = pm_runtime_resume_and_get(hda->dev);
  229. if (err < 0) {
  230. dev_err(hda->dev, "runtime resume and get failed, err = %d\n", err);
  231. goto out_free_device;
  232. }
  233. err = cix_ipbloq_hda_init(hda, chip, pdev);
  234. if (err < 0)
  235. goto out_free_device;
  236. err = cix_ipbloq_hda_probe_codec(hda);
  237. if (err < 0)
  238. goto out_free_device;
  239. pm_runtime_put(hda->dev);
  240. return 0;
  241. out_free_device:
  242. snd_device_free(card, chip);
  243. out_free_card:
  244. snd_card_free(card);
  245. return err;
  246. }
  247. static void cix_ipbloq_hda_remove(struct platform_device *pdev)
  248. {
  249. struct snd_card *card = dev_get_drvdata(&pdev->dev);
  250. struct azx *chip = card->private_data;
  251. snd_device_free(card, chip);
  252. snd_card_free(card);
  253. pm_runtime_disable(&pdev->dev);
  254. }
  255. static void cix_ipbloq_hda_shutdown(struct platform_device *pdev)
  256. {
  257. struct snd_card *card = dev_get_drvdata(&pdev->dev);
  258. struct azx *chip;
  259. if (!card)
  260. return;
  261. chip = card->private_data;
  262. if (chip && chip->running)
  263. azx_stop_chip(chip);
  264. }
  265. static int cix_ipbloq_hda_suspend(struct device *dev)
  266. {
  267. struct snd_card *card = dev_get_drvdata(dev);
  268. int rc;
  269. rc = pm_runtime_force_suspend(dev);
  270. if (rc < 0)
  271. return rc;
  272. snd_power_change_state(card, SNDRV_CTL_POWER_D3cold);
  273. return 0;
  274. }
  275. static int cix_ipbloq_hda_resume(struct device *dev)
  276. {
  277. struct snd_card *card = dev_get_drvdata(dev);
  278. int rc;
  279. rc = pm_runtime_force_resume(dev);
  280. if (rc < 0)
  281. return rc;
  282. snd_power_change_state(card, SNDRV_CTL_POWER_D0);
  283. return 0;
  284. }
  285. static int cix_ipbloq_hda_runtime_suspend(struct device *dev)
  286. {
  287. struct snd_card *card = dev_get_drvdata(dev);
  288. struct azx *chip = card->private_data;
  289. struct cix_ipbloq_hda *hda = container_of(chip, struct cix_ipbloq_hda, chip);
  290. if (chip && chip->running) {
  291. azx_stop_chip(chip);
  292. azx_enter_link_reset(chip);
  293. }
  294. clk_bulk_disable_unprepare(hda->nclocks, hda->clocks);
  295. return 0;
  296. }
  297. static int cix_ipbloq_hda_runtime_resume(struct device *dev)
  298. {
  299. struct snd_card *card = dev_get_drvdata(dev);
  300. struct azx *chip = card->private_data;
  301. struct cix_ipbloq_hda *hda = container_of(chip, struct cix_ipbloq_hda, chip);
  302. int rc;
  303. rc = clk_bulk_prepare_enable(hda->nclocks, hda->clocks);
  304. if (rc) {
  305. dev_err(dev, "failed to enable clk bulk, rc: %d\n", rc);
  306. return rc;
  307. }
  308. rc = reset_control_assert(hda->reset);
  309. if (rc) {
  310. dev_err(dev, "failed to assert reset, rc: %d\n", rc);
  311. return rc;
  312. }
  313. rc = reset_control_deassert(hda->reset);
  314. if (rc) {
  315. dev_err(dev, "failed to deassert reset, rc: %d\n", rc);
  316. return rc;
  317. }
  318. if (chip && chip->running)
  319. azx_init_chip(chip, 1);
  320. return 0;
  321. }
  322. static const struct dev_pm_ops cix_ipbloq_hda_pm = {
  323. SYSTEM_SLEEP_PM_OPS(cix_ipbloq_hda_suspend,
  324. cix_ipbloq_hda_resume)
  325. RUNTIME_PM_OPS(cix_ipbloq_hda_runtime_suspend,
  326. cix_ipbloq_hda_runtime_resume, NULL)
  327. };
  328. static const struct of_device_id cix_ipbloq_hda_match[] = {
  329. { .compatible = "cix,sky1-ipbloq-hda" },
  330. { /* sentinel */ },
  331. };
  332. MODULE_DEVICE_TABLE(of, cix_ipbloq_hda_match);
  333. static struct platform_driver cix_ipbloq_hda_driver = {
  334. .driver = {
  335. .name = "cix-ipbloq-hda",
  336. .pm = pm_ptr(&cix_ipbloq_hda_pm),
  337. .of_match_table = cix_ipbloq_hda_match,
  338. },
  339. .probe = cix_ipbloq_hda_probe,
  340. .remove = cix_ipbloq_hda_remove,
  341. .shutdown = cix_ipbloq_hda_shutdown,
  342. };
  343. module_platform_driver(cix_ipbloq_hda_driver);
  344. MODULE_LICENSE("GPL");
  345. MODULE_DESCRIPTION("CIX IPBLOQ HDA bus driver");
  346. MODULE_AUTHOR("Joakim Zhang <joakim.zhang@cixtech.com>");