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