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
cix_ipbloq_hda_dev_disconnect(struct snd_device * device)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
cix_ipbloq_hda_dev_free(struct snd_device * device)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
cix_ipbloq_hda_probe_codec(struct cix_ipbloq_hda * hda)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
cix_ipbloq_hda_init(struct cix_ipbloq_hda * hda,struct azx * chip,struct platform_device * pdev)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 dev_err(hda->dev, "failed to get the irq, err = %d\n", irq_id);
120 return irq_id;
121 }
122
123 err = devm_request_irq(hda->dev, irq_id, azx_interrupt,
124 0, KBUILD_MODNAME, chip);
125 if (err < 0)
126 return dev_err_probe(hda->dev, err,
127 "unable to request IRQ %d : err = %d\n", irq_id, err);
128 bus->irq = irq_id;
129 card->sync_irq = bus->irq;
130
131 gcap = azx_readw(chip, GCAP);
132 chip->capture_streams = (gcap >> 8) & 0x0f;
133 chip->playback_streams = (gcap >> 12) & 0x0f;
134 chip->capture_index_offset = 0;
135 chip->playback_index_offset = chip->capture_streams;
136 chip->num_streams = chip->playback_streams + chip->capture_streams;
137
138 /* initialize streams */
139 err = azx_init_streams(chip);
140 if (err < 0) {
141 dev_err(hda->dev, "failed to initialize streams: %d\n", err);
142 return err;
143 }
144
145 err = azx_alloc_stream_pages(chip);
146 if (err < 0) {
147 dev_err(hda->dev, "failed to allocate stream pages: %d\n", err);
148 return err;
149 }
150
151 /* initialize chip */
152 azx_init_chip(chip, 1);
153
154 /* codec detection */
155 if (!bus->codec_mask) {
156 dev_err(hda->dev, "no codecs found\n");
157 return -ENODEV;
158 }
159 dev_dbg(card->dev, "codec detection mask = 0x%lx\n", bus->codec_mask);
160
161 /* driver name */
162 strscpy(card->driver, drv_name, sizeof(card->driver));
163
164 /* shortname for card */
165 sname = of_get_property(pdev->dev.of_node, "model", NULL);
166 if (!sname)
167 sname = drv_name;
168 if (strlen(sname) > sizeof(card->shortname))
169 dev_dbg(card->dev, "truncating shortname for card\n");
170 strscpy(card->shortname, sname, sizeof(card->shortname));
171
172 /* longname for card */
173 snprintf(card->longname, sizeof(card->longname),
174 "%s at 0x%lx irq %i",
175 card->shortname, bus->addr, bus->irq);
176
177 return 0;
178 }
179
cix_ipbloq_hda_create(struct cix_ipbloq_hda * hda,struct snd_card * card,unsigned int driver_caps)180 static int cix_ipbloq_hda_create(struct cix_ipbloq_hda *hda,
181 struct snd_card *card,
182 unsigned int driver_caps)
183 {
184 static const struct snd_device_ops ops = {
185 .dev_disconnect = cix_ipbloq_hda_dev_disconnect,
186 .dev_free = cix_ipbloq_hda_dev_free,
187 };
188 struct azx *chip;
189 int err;
190
191 chip = &hda->chip;
192 chip->card = card;
193 chip->ops = &cix_ipbloq_hda_ops;
194 chip->driver_caps = driver_caps;
195 chip->driver_type = driver_caps & 0xff;
196 chip->dev_index = 0;
197 chip->single_cmd = 0;
198 chip->codec_probe_mask = -1;
199 chip->align_buffer_size = 1;
200 chip->jackpoll_interval = msecs_to_jiffies(CIX_IPBLOQ_JACKPOLL_DEFAULT_TIME_MS);
201 mutex_init(&chip->open_mutex);
202 INIT_LIST_HEAD(&chip->pcm_list);
203
204 /*
205 * HD-audio controllers appear pretty inaccurate about the update-IRQ timing.
206 * The IRQ is issued before actually the data is processed. So use stream
207 * link position by default instead of dma position buffer.
208 */
209 chip->get_position[0] = chip->get_position[1] = azx_get_pos_lpib;
210
211 err = azx_bus_init(chip, NULL);
212 if (err < 0) {
213 dev_err(hda->dev, "failed to init bus, err = %d\n", err);
214 return err;
215 }
216
217 /* RIRBSTS.RINTFL cannot be cleared, cause interrupt storm */
218 chip->bus.core.polling_mode = 1;
219 chip->bus.core.not_use_interrupts = 1;
220
221 chip->bus.core.aligned_mmio = 1;
222 chip->bus.core.dma_stop_delay = 100;
223 chip->bus.core.addr_offset = (dma_addr_t)CIX_IPBLOQ_SKY1_ADDR_HOST_TO_HDAC_OFFSET;
224
225 chip->bus.jackpoll_in_suspend = 1;
226
227 err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops);
228 if (err < 0) {
229 dev_err(card->dev, "failed to create device, err = %d\n", err);
230 return err;
231 }
232
233 return 0;
234 }
235
cix_ipbloq_hda_probe(struct platform_device * pdev)236 static int cix_ipbloq_hda_probe(struct platform_device *pdev)
237 {
238 const unsigned int driver_flags = AZX_DCAPS_PM_RUNTIME;
239 struct cix_ipbloq_hda *hda;
240 struct snd_card *card;
241 struct azx *chip;
242 int err;
243
244 hda = devm_kzalloc(&pdev->dev, sizeof(*hda), GFP_KERNEL);
245 if (!hda)
246 return -ENOMEM;
247 hda->dev = &pdev->dev;
248
249 hda->reset = devm_reset_control_get(hda->dev, NULL);
250 if (IS_ERR(hda->reset))
251 return dev_err_probe(hda->dev, PTR_ERR(hda->reset),
252 "failed to get reset, err = %ld\n", PTR_ERR(hda->reset));
253
254 hda->clocks[hda->nclocks++].id = "ipg";
255 hda->clocks[hda->nclocks++].id = "per";
256 err = devm_clk_bulk_get(hda->dev, hda->nclocks, hda->clocks);
257 if (err < 0)
258 return dev_err_probe(hda->dev, err, "failed to get clk, err = %d\n", err);
259
260 dma_set_mask_and_coherent(hda->dev, DMA_BIT_MASK(32));
261
262 err = of_reserved_mem_device_init(hda->dev);
263 if (err < 0 && err != -ENODEV) {
264 dev_err(hda->dev,
265 "failed to init reserved mem for DMA, err = %d\n", err);
266 return err;
267 }
268
269 err = snd_card_new(hda->dev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1,
270 THIS_MODULE, 0, &card);
271 if (err < 0)
272 return dev_err_probe(hda->dev, err, "failed to crate card, err = %d\n", err);
273
274 err = cix_ipbloq_hda_create(hda, card, driver_flags);
275 if (err < 0)
276 goto out_free_card;
277
278 chip = &hda->chip;
279 card->private_data = chip;
280 dev_set_drvdata(hda->dev, card);
281
282 pm_runtime_enable(hda->dev);
283 if (!azx_has_pm_runtime(chip))
284 pm_runtime_forbid(hda->dev);
285
286 err = pm_runtime_resume_and_get(hda->dev);
287 if (err < 0) {
288 dev_err(hda->dev, "runtime resume and get failed, err = %d\n", err);
289 goto out_free_device;
290 }
291
292 err = cix_ipbloq_hda_init(hda, chip, pdev);
293 if (err < 0)
294 goto out_free_device;
295
296 err = cix_ipbloq_hda_probe_codec(hda);
297 if (err < 0)
298 goto out_free_device;
299
300 pm_runtime_put(hda->dev);
301
302 return 0;
303
304 out_free_device:
305 snd_device_free(card, chip);
306 out_free_card:
307 snd_card_free(card);
308
309 return err;
310 }
311
cix_ipbloq_hda_remove(struct platform_device * pdev)312 static void cix_ipbloq_hda_remove(struct platform_device *pdev)
313 {
314 struct snd_card *card = dev_get_drvdata(&pdev->dev);
315 struct azx *chip = card->private_data;
316
317 snd_device_free(card, chip);
318 snd_card_free(card);
319
320 pm_runtime_disable(&pdev->dev);
321 }
322
cix_ipbloq_hda_shutdown(struct platform_device * pdev)323 static void cix_ipbloq_hda_shutdown(struct platform_device *pdev)
324 {
325 struct snd_card *card = dev_get_drvdata(&pdev->dev);
326 struct azx *chip;
327
328 if (!card)
329 return;
330
331 chip = card->private_data;
332 if (chip && chip->running)
333 azx_stop_chip(chip);
334 }
335
cix_ipbloq_hda_suspend(struct device * dev)336 static int cix_ipbloq_hda_suspend(struct device *dev)
337 {
338 struct snd_card *card = dev_get_drvdata(dev);
339 int rc;
340
341 rc = pm_runtime_force_suspend(dev);
342 if (rc < 0)
343 return rc;
344 snd_power_change_state(card, SNDRV_CTL_POWER_D3cold);
345
346 return 0;
347 }
348
cix_ipbloq_hda_resume(struct device * dev)349 static int cix_ipbloq_hda_resume(struct device *dev)
350 {
351 struct snd_card *card = dev_get_drvdata(dev);
352 int rc;
353
354 rc = pm_runtime_force_resume(dev);
355 if (rc < 0)
356 return rc;
357 snd_power_change_state(card, SNDRV_CTL_POWER_D0);
358
359 return 0;
360 }
361
cix_ipbloq_hda_runtime_suspend(struct device * dev)362 static int cix_ipbloq_hda_runtime_suspend(struct device *dev)
363 {
364 struct snd_card *card = dev_get_drvdata(dev);
365 struct azx *chip = card->private_data;
366 struct cix_ipbloq_hda *hda = container_of(chip, struct cix_ipbloq_hda, chip);
367
368 if (chip && chip->running) {
369 azx_stop_chip(chip);
370 azx_enter_link_reset(chip);
371 }
372
373 clk_bulk_disable_unprepare(hda->nclocks, hda->clocks);
374
375 return 0;
376 }
377
cix_ipbloq_hda_runtime_resume(struct device * dev)378 static int cix_ipbloq_hda_runtime_resume(struct device *dev)
379 {
380 struct snd_card *card = dev_get_drvdata(dev);
381 struct azx *chip = card->private_data;
382 struct cix_ipbloq_hda *hda = container_of(chip, struct cix_ipbloq_hda, chip);
383 int rc;
384
385 rc = clk_bulk_prepare_enable(hda->nclocks, hda->clocks);
386 if (rc) {
387 dev_err(dev, "failed to enable clk bulk, rc: %d\n", rc);
388 return rc;
389 }
390
391 rc = reset_control_assert(hda->reset);
392 if (rc) {
393 dev_err(dev, "failed to assert reset, rc: %d\n", rc);
394 return rc;
395 }
396
397 rc = reset_control_deassert(hda->reset);
398 if (rc) {
399 dev_err(dev, "failed to deassert reset, rc: %d\n", rc);
400 return rc;
401 }
402
403 if (chip && chip->running)
404 azx_init_chip(chip, 1);
405
406 return 0;
407 }
408
409 static const struct dev_pm_ops cix_ipbloq_hda_pm = {
410 SYSTEM_SLEEP_PM_OPS(cix_ipbloq_hda_suspend,
411 cix_ipbloq_hda_resume)
412 RUNTIME_PM_OPS(cix_ipbloq_hda_runtime_suspend,
413 cix_ipbloq_hda_runtime_resume, NULL)
414 };
415
416 static const struct of_device_id cix_ipbloq_hda_match[] = {
417 { .compatible = "cix,sky1-ipbloq-hda" },
418 { /* sentinel */ },
419 };
420 MODULE_DEVICE_TABLE(of, cix_ipbloq_hda_match);
421
422 static struct platform_driver cix_ipbloq_hda_driver = {
423 .driver = {
424 .name = "cix-ipbloq-hda",
425 .pm = pm_ptr(&cix_ipbloq_hda_pm),
426 .of_match_table = cix_ipbloq_hda_match,
427 },
428 .probe = cix_ipbloq_hda_probe,
429 .remove = cix_ipbloq_hda_remove,
430 .shutdown = cix_ipbloq_hda_shutdown,
431 };
432 module_platform_driver(cix_ipbloq_hda_driver);
433
434 MODULE_LICENSE("GPL");
435 MODULE_DESCRIPTION("CIX IPBLOQ HDA bus driver");
436 MODULE_AUTHOR("Joakim Zhang <joakim.zhang@cixtech.com>");
437