1 // SPDX-License-Identifier: GPL-2.0-only
3 * wm0010.c -- WM0010 DSP Driver
5 * Copyright 2012 Wolfson Microelectronics PLC.
7 * Authors: Mark Brown <broonie@opensource.wolfsonmicro.com>
8 * Dimitris Papastamos <dp@opensource.wolfsonmicro.com>
9 * Scott Ling <sl@opensource.wolfsonmicro.com>
12 #include <linux/module.h>
13 #include <linux/moduleparam.h>
14 #include <linux/interrupt.h>
15 #include <linux/irqreturn.h>
16 #include <linux/init.h>
17 #include <linux/spi/spi.h>
18 #include <linux/firmware.h>
19 #include <linux/delay.h>
21 #include <linux/gpio/consumer.h>
22 #include <linux/regulator/consumer.h>
23 #include <linux/mutex.h>
24 #include <linux/workqueue.h>
26 #include <sound/soc.h>
27 #include <sound/wm0010.h>
29 #define DEVICE_ID_WM0010 10
31 /* We only support v1 of the .dfw INFO record */
32 #define INFO_VERSION 1
51 u8 tool_major_version;
52 u8 tool_minor_version;
68 static struct pll_clock_map {
70 int max_pll_spi_speed;
72 } pll_clock_map[] = { /* Dividers */
73 { 22000000, 26000000, 0x00201f11 }, /* 2,32,2 */
74 { 18000000, 26000000, 0x00203f21 }, /* 2,64,4 */
75 { 14000000, 26000000, 0x00202620 }, /* 1,39,4 */
76 { 10000000, 22000000, 0x00203120 }, /* 1,50,4 */
77 { 6500000, 22000000, 0x00204520 }, /* 1,70,4 */
78 { 5500000, 22000000, 0x00103f10 }, /* 1,64,2 */
90 struct snd_soc_component *component;
95 struct wm0010_pdata pdata;
97 struct gpio_desc *reset;
99 struct regulator_bulk_data core_supplies[2];
100 struct regulator *dbvdd;
104 enum wm0010_state state;
109 int board_max_spi_speed;
115 struct completion boot_completion;
118 struct wm0010_spi_msg {
119 struct spi_message m;
120 struct spi_transfer t;
126 static const struct snd_soc_dapm_widget wm0010_dapm_widgets[] = {
127 SND_SOC_DAPM_SUPPLY("CLKIN", SND_SOC_NOPM, 0, 0, NULL, 0),
130 static const struct snd_soc_dapm_route wm0010_dapm_routes[] = {
131 { "SDI2 Capture", NULL, "SDI1 Playback" },
132 { "SDI1 Capture", NULL, "SDI2 Playback" },
134 { "SDI1 Capture", NULL, "CLKIN" },
135 { "SDI2 Capture", NULL, "CLKIN" },
136 { "SDI1 Playback", NULL, "CLKIN" },
137 { "SDI2 Playback", NULL, "CLKIN" },
140 static const char *wm0010_state_to_str(enum wm0010_state state)
142 static const char * const state_to_str[] = {
150 if (state < 0 || state >= ARRAY_SIZE(state_to_str))
152 return state_to_str[state];
155 /* Called with wm0010->lock held */
156 static void wm0010_halt(struct snd_soc_component *component)
158 struct wm0010_priv *wm0010 = snd_soc_component_get_drvdata(component);
160 enum wm0010_state state;
162 /* Fetch the wm0010 state */
163 spin_lock_irqsave(&wm0010->irq_lock, flags);
164 state = wm0010->state;
165 spin_unlock_irqrestore(&wm0010->irq_lock, flags);
168 case WM0010_POWER_OFF:
169 /* If there's nothing to do, bail out */
171 case WM0010_OUT_OF_RESET:
174 case WM0010_FIRMWARE:
175 /* Remember to put chip back into reset */
176 gpiod_set_value_cansleep(wm0010->reset, 1);
177 /* Disable the regulators */
178 regulator_disable(wm0010->dbvdd);
179 regulator_bulk_disable(ARRAY_SIZE(wm0010->core_supplies),
180 wm0010->core_supplies);
184 spin_lock_irqsave(&wm0010->irq_lock, flags);
185 wm0010->state = WM0010_POWER_OFF;
186 spin_unlock_irqrestore(&wm0010->irq_lock, flags);
189 struct wm0010_boot_xfer {
190 struct list_head list;
191 struct snd_soc_component *component;
192 struct completion *done;
193 struct spi_message m;
194 struct spi_transfer t;
197 /* Called with wm0010->lock held */
198 static void wm0010_mark_boot_failure(struct wm0010_priv *wm0010)
200 enum wm0010_state state;
203 spin_lock_irqsave(&wm0010->irq_lock, flags);
204 state = wm0010->state;
205 spin_unlock_irqrestore(&wm0010->irq_lock, flags);
207 dev_err(wm0010->dev, "Failed to transition from `%s' state to `%s' state\n",
208 wm0010_state_to_str(state), wm0010_state_to_str(state + 1));
210 wm0010->boot_failed = true;
213 static void wm0010_boot_xfer_complete(void *data)
215 struct wm0010_boot_xfer *xfer = data;
216 struct snd_soc_component *component = xfer->component;
217 struct wm0010_priv *wm0010 = snd_soc_component_get_drvdata(component);
218 u32 *out32 = xfer->t.rx_buf;
221 if (xfer->m.status != 0) {
222 dev_err(component->dev, "SPI transfer failed: %d\n",
224 wm0010_mark_boot_failure(wm0010);
226 complete(xfer->done);
230 for (i = 0; i < xfer->t.len / 4; i++) {
231 dev_dbg(component->dev, "%d: %04x\n", i, out32[i]);
233 switch (be32_to_cpu(out32[i])) {
235 dev_err(component->dev,
236 "%d: ROM error reported in stage 2\n", i);
237 wm0010_mark_boot_failure(wm0010);
241 if (wm0010->state < WM0010_STAGE2)
243 dev_err(component->dev,
244 "%d: ROM bootloader running in stage 2\n", i);
245 wm0010_mark_boot_failure(wm0010);
249 dev_dbg(component->dev, "Stage2 loader running\n");
253 dev_dbg(component->dev, "CODE_HDR packet received\n");
257 dev_dbg(component->dev, "CODE_DATA packet received\n");
261 dev_dbg(component->dev, "Download complete\n");
265 dev_dbg(component->dev, "Application start\n");
269 dev_dbg(component->dev, "PLL packet received\n");
270 wm0010->pll_running = true;
274 dev_err(component->dev, "Device reports image too long\n");
275 wm0010_mark_boot_failure(wm0010);
279 dev_err(component->dev, "Device reports bad SPI packet\n");
280 wm0010_mark_boot_failure(wm0010);
284 dev_err(component->dev, "Device reports SPI read overflow\n");
285 wm0010_mark_boot_failure(wm0010);
289 dev_err(component->dev, "Device reports SPI underclock\n");
290 wm0010_mark_boot_failure(wm0010);
294 dev_err(component->dev, "Device reports bad header packet\n");
295 wm0010_mark_boot_failure(wm0010);
299 dev_err(component->dev, "Device reports invalid packet type\n");
300 wm0010_mark_boot_failure(wm0010);
304 dev_err(component->dev, "Device reports data before header error\n");
305 wm0010_mark_boot_failure(wm0010);
309 dev_err(component->dev, "Device reports invalid PLL packet\n");
313 dev_err(component->dev, "Device reports packet alignment error\n");
314 wm0010_mark_boot_failure(wm0010);
318 dev_err(component->dev, "Unrecognised return 0x%x\n",
319 be32_to_cpu(out32[i]));
320 wm0010_mark_boot_failure(wm0010);
324 if (wm0010->boot_failed)
329 complete(xfer->done);
332 static void byte_swap_64(u64 *data_in, u64 *data_out, u32 len)
336 for (i = 0; i < len / 8; i++)
337 data_out[i] = cpu_to_be64(le64_to_cpu(data_in[i]));
340 static int wm0010_firmware_load(const char *name, struct snd_soc_component *component)
342 struct spi_device *spi = to_spi_device(component->dev);
343 struct wm0010_priv *wm0010 = snd_soc_component_get_drvdata(component);
344 struct list_head xfer_list;
345 struct wm0010_boot_xfer *xfer;
347 DECLARE_COMPLETION_ONSTACK(done);
348 const struct firmware *fw;
349 const struct dfw_binrec *rec;
350 const struct dfw_inforec *inforec;
355 INIT_LIST_HEAD(&xfer_list);
357 ret = reject_firmware(&fw, name, component->dev);
359 dev_err(component->dev, "Failed to request application(%s): %d\n",
364 rec = (const struct dfw_binrec *)fw->data;
365 inforec = (const struct dfw_inforec *)rec->data;
367 dsp = inforec->dsp_target;
368 wm0010->boot_failed = false;
369 if (WARN_ON(!list_empty(&xfer_list)))
372 /* First record should be INFO */
373 if (rec->command != DFW_CMD_INFO) {
374 dev_err(component->dev, "First record not INFO\r\n");
379 if (inforec->info_version != INFO_VERSION) {
380 dev_err(component->dev,
381 "Unsupported version (%02d) of INFO record\r\n",
382 inforec->info_version);
387 dev_dbg(component->dev, "Version v%02d INFO record found\r\n",
388 inforec->info_version);
390 /* Check it's a DSP file */
391 if (dsp != DEVICE_ID_WM0010) {
392 dev_err(component->dev, "Not a WM0010 firmware file.\r\n");
397 /* Skip the info record as we don't need to send it */
398 offset += ((rec->length) + 8);
399 rec = (void *)&rec->data[rec->length];
401 while (offset < fw->size) {
402 dev_dbg(component->dev,
403 "Packet: command %d, data length = 0x%x\r\n",
404 rec->command, rec->length);
405 len = rec->length + 8;
407 xfer = kzalloc(sizeof(*xfer), GFP_KERNEL);
413 xfer->component = component;
414 list_add_tail(&xfer->list, &xfer_list);
416 out = kzalloc(len, GFP_KERNEL | GFP_DMA);
421 xfer->t.rx_buf = out;
423 img = kzalloc(len, GFP_KERNEL | GFP_DMA);
428 xfer->t.tx_buf = img;
430 byte_swap_64((u64 *)&rec->command, img, len);
432 spi_message_init(&xfer->m);
433 xfer->m.complete = wm0010_boot_xfer_complete;
434 xfer->m.context = xfer;
436 xfer->t.bits_per_word = 8;
438 if (!wm0010->pll_running) {
439 xfer->t.speed_hz = wm0010->sysclk / 6;
441 xfer->t.speed_hz = wm0010->max_spi_freq;
443 if (wm0010->board_max_spi_speed &&
444 (wm0010->board_max_spi_speed < wm0010->max_spi_freq))
445 xfer->t.speed_hz = wm0010->board_max_spi_speed;
448 /* Store max usable spi frequency for later use */
449 wm0010->max_spi_freq = xfer->t.speed_hz;
451 spi_message_add_tail(&xfer->t, &xfer->m);
453 offset += ((rec->length) + 8);
454 rec = (void *)&rec->data[rec->length];
456 if (offset >= fw->size) {
457 dev_dbg(component->dev, "All transfers scheduled\n");
461 ret = spi_async(spi, &xfer->m);
463 dev_err(component->dev, "Write failed: %d\n", ret);
467 if (wm0010->boot_failed) {
468 dev_dbg(component->dev, "Boot fail!\n");
474 wait_for_completion(&done);
479 while (!list_empty(&xfer_list)) {
480 xfer = list_first_entry(&xfer_list, struct wm0010_boot_xfer,
482 kfree(xfer->t.rx_buf);
483 kfree(xfer->t.tx_buf);
484 list_del(&xfer->list);
489 release_firmware(fw);
493 static int wm0010_stage2_load(struct snd_soc_component *component)
495 struct spi_device *spi = to_spi_device(component->dev);
496 struct wm0010_priv *wm0010 = snd_soc_component_get_drvdata(component);
497 const struct firmware *fw;
498 struct spi_message m;
499 struct spi_transfer t;
505 ret = reject_firmware(&fw, "/*(DEBLOBBED)*/", component->dev);
507 dev_err(component->dev, "Failed to request stage2 loader: %d\n",
512 dev_dbg(component->dev, "Downloading %zu byte stage 2 loader\n", fw->size);
514 /* Copy to local buffer first as vmalloc causes problems for dma */
515 img = kmemdup(&fw->data[0], fw->size, GFP_KERNEL | GFP_DMA);
521 out = kzalloc(fw->size, GFP_KERNEL | GFP_DMA);
527 spi_message_init(&m);
528 memset(&t, 0, sizeof(t));
533 t.speed_hz = wm0010->sysclk / 10;
534 spi_message_add_tail(&t, &m);
536 dev_dbg(component->dev, "Starting initial download at %dHz\n",
539 ret = spi_sync(spi, &m);
541 dev_err(component->dev, "Initial download failed: %d\n", ret);
545 /* Look for errors from the boot ROM */
546 for (i = 0; i < fw->size; i++) {
547 if (out[i] != 0x55) {
548 dev_err(component->dev, "Boot ROM error: %x in %d\n",
550 wm0010_mark_boot_failure(wm0010);
560 release_firmware(fw);
565 static int wm0010_boot(struct snd_soc_component *component)
567 struct spi_device *spi = to_spi_device(component->dev);
568 struct wm0010_priv *wm0010 = snd_soc_component_get_drvdata(component);
571 struct spi_message m;
572 struct spi_transfer t;
573 struct dfw_pllrec pll_rec;
579 spin_lock_irqsave(&wm0010->irq_lock, flags);
580 if (wm0010->state != WM0010_POWER_OFF)
581 dev_warn(wm0010->dev, "DSP already powered up!\n");
582 spin_unlock_irqrestore(&wm0010->irq_lock, flags);
584 if (wm0010->sysclk > 26000000) {
585 dev_err(component->dev, "Max DSP clock frequency is 26MHz\n");
590 mutex_lock(&wm0010->lock);
591 wm0010->pll_running = false;
593 dev_dbg(component->dev, "max_spi_freq: %d\n", wm0010->max_spi_freq);
595 ret = regulator_bulk_enable(ARRAY_SIZE(wm0010->core_supplies),
596 wm0010->core_supplies);
598 dev_err(&spi->dev, "Failed to enable core supplies: %d\n",
600 mutex_unlock(&wm0010->lock);
604 ret = regulator_enable(wm0010->dbvdd);
606 dev_err(&spi->dev, "Failed to enable DBVDD: %d\n", ret);
611 gpiod_set_value_cansleep(wm0010->reset, 0);
612 spin_lock_irqsave(&wm0010->irq_lock, flags);
613 wm0010->state = WM0010_OUT_OF_RESET;
614 spin_unlock_irqrestore(&wm0010->irq_lock, flags);
616 if (!wait_for_completion_timeout(&wm0010->boot_completion,
617 msecs_to_jiffies(20)))
618 dev_err(component->dev, "Failed to get interrupt from DSP\n");
620 spin_lock_irqsave(&wm0010->irq_lock, flags);
621 wm0010->state = WM0010_BOOTROM;
622 spin_unlock_irqrestore(&wm0010->irq_lock, flags);
624 ret = wm0010_stage2_load(component);
628 if (!wait_for_completion_timeout(&wm0010->boot_completion,
629 msecs_to_jiffies(20)))
630 dev_err(component->dev, "Failed to get interrupt from DSP loader.\n");
632 spin_lock_irqsave(&wm0010->irq_lock, flags);
633 wm0010->state = WM0010_STAGE2;
634 spin_unlock_irqrestore(&wm0010->irq_lock, flags);
636 /* Only initialise PLL if max_spi_freq initialised */
637 if (wm0010->max_spi_freq) {
639 /* Initialise a PLL record */
640 memset(&pll_rec, 0, sizeof(pll_rec));
641 pll_rec.command = DFW_CMD_PLL;
642 pll_rec.length = (sizeof(pll_rec) - 8);
644 /* On wm0010 only the CLKCTRL1 value is used */
645 pll_rec.clkctrl1 = wm0010->pll_clkctrl1;
648 len = pll_rec.length + 8;
649 out = kzalloc(len, GFP_KERNEL | GFP_DMA);
653 img_swap = kzalloc(len, GFP_KERNEL | GFP_DMA);
657 /* We need to re-order for 0010 */
658 byte_swap_64((u64 *)&pll_rec, img_swap, len);
660 spi_message_init(&m);
661 memset(&t, 0, sizeof(t));
666 t.speed_hz = wm0010->sysclk / 6;
667 spi_message_add_tail(&t, &m);
669 ret = spi_sync(spi, &m);
671 dev_err(component->dev, "First PLL write failed: %d\n", ret);
675 /* Use a second send of the message to get the return status */
676 ret = spi_sync(spi, &m);
678 dev_err(component->dev, "Second PLL write failed: %d\n", ret);
684 /* Look for PLL active code from the DSP */
685 for (i = 0; i < len / 4; i++) {
686 if (*p == 0x0e00ed0f) {
687 dev_dbg(component->dev, "PLL packet received\n");
688 wm0010->pll_running = true;
697 dev_dbg(component->dev, "Not enabling DSP PLL.");
699 ret = wm0010_firmware_load("/*(DEBLOBBED)*/", component);
704 spin_lock_irqsave(&wm0010->irq_lock, flags);
705 wm0010->state = WM0010_FIRMWARE;
706 spin_unlock_irqrestore(&wm0010->irq_lock, flags);
708 mutex_unlock(&wm0010->lock);
717 /* Put the chip back into reset */
718 wm0010_halt(component);
719 mutex_unlock(&wm0010->lock);
723 mutex_unlock(&wm0010->lock);
724 regulator_bulk_disable(ARRAY_SIZE(wm0010->core_supplies),
725 wm0010->core_supplies);
730 static int wm0010_set_bias_level(struct snd_soc_component *component,
731 enum snd_soc_bias_level level)
733 struct wm0010_priv *wm0010 = snd_soc_component_get_drvdata(component);
736 case SND_SOC_BIAS_ON:
737 if (snd_soc_component_get_bias_level(component) == SND_SOC_BIAS_PREPARE)
738 wm0010_boot(component);
740 case SND_SOC_BIAS_PREPARE:
742 case SND_SOC_BIAS_STANDBY:
743 if (snd_soc_component_get_bias_level(component) == SND_SOC_BIAS_PREPARE) {
744 mutex_lock(&wm0010->lock);
745 wm0010_halt(component);
746 mutex_unlock(&wm0010->lock);
749 case SND_SOC_BIAS_OFF:
756 static int wm0010_set_sysclk(struct snd_soc_component *component, int source,
757 int clk_id, unsigned int freq, int dir)
759 struct wm0010_priv *wm0010 = snd_soc_component_get_drvdata(component);
762 wm0010->sysclk = freq;
764 if (freq < pll_clock_map[ARRAY_SIZE(pll_clock_map)-1].max_sysclk) {
765 wm0010->max_spi_freq = 0;
767 for (i = 0; i < ARRAY_SIZE(pll_clock_map); i++)
768 if (freq >= pll_clock_map[i].max_sysclk) {
769 wm0010->max_spi_freq = pll_clock_map[i].max_pll_spi_speed;
770 wm0010->pll_clkctrl1 = pll_clock_map[i].pll_clkctrl1;
778 static int wm0010_probe(struct snd_soc_component *component);
780 static const struct snd_soc_component_driver soc_component_dev_wm0010 = {
781 .probe = wm0010_probe,
782 .set_bias_level = wm0010_set_bias_level,
783 .set_sysclk = wm0010_set_sysclk,
784 .dapm_widgets = wm0010_dapm_widgets,
785 .num_dapm_widgets = ARRAY_SIZE(wm0010_dapm_widgets),
786 .dapm_routes = wm0010_dapm_routes,
787 .num_dapm_routes = ARRAY_SIZE(wm0010_dapm_routes),
788 .use_pmdown_time = 1,
792 #define WM0010_RATES (SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000)
793 #define WM0010_FORMATS (SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_S16_LE |\
794 SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S24_LE |\
795 SNDRV_PCM_FMTBIT_S32_LE)
797 static struct snd_soc_dai_driver wm0010_dai[] = {
799 .name = "wm0010-sdi1",
801 .stream_name = "SDI1 Playback",
804 .rates = WM0010_RATES,
805 .formats = WM0010_FORMATS,
808 .stream_name = "SDI1 Capture",
811 .rates = WM0010_RATES,
812 .formats = WM0010_FORMATS,
816 .name = "wm0010-sdi2",
818 .stream_name = "SDI2 Playback",
821 .rates = WM0010_RATES,
822 .formats = WM0010_FORMATS,
825 .stream_name = "SDI2 Capture",
828 .rates = WM0010_RATES,
829 .formats = WM0010_FORMATS,
834 static irqreturn_t wm0010_irq(int irq, void *data)
836 struct wm0010_priv *wm0010 = data;
838 switch (wm0010->state) {
839 case WM0010_OUT_OF_RESET:
842 spin_lock(&wm0010->irq_lock);
843 complete(&wm0010->boot_completion);
844 spin_unlock(&wm0010->irq_lock);
853 static int wm0010_probe(struct snd_soc_component *component)
855 struct wm0010_priv *wm0010 = snd_soc_component_get_drvdata(component);
857 wm0010->component = component;
862 static int wm0010_spi_probe(struct spi_device *spi)
867 struct wm0010_priv *wm0010;
869 wm0010 = devm_kzalloc(&spi->dev, sizeof(*wm0010),
874 mutex_init(&wm0010->lock);
875 spin_lock_init(&wm0010->irq_lock);
877 spi_set_drvdata(spi, wm0010);
878 wm0010->dev = &spi->dev;
880 if (dev_get_platdata(&spi->dev))
881 memcpy(&wm0010->pdata, dev_get_platdata(&spi->dev),
882 sizeof(wm0010->pdata));
884 init_completion(&wm0010->boot_completion);
886 wm0010->core_supplies[0].supply = "AVDD";
887 wm0010->core_supplies[1].supply = "DCVDD";
888 ret = devm_regulator_bulk_get(wm0010->dev, ARRAY_SIZE(wm0010->core_supplies),
889 wm0010->core_supplies);
891 dev_err(wm0010->dev, "Failed to obtain core supplies: %d\n",
896 wm0010->dbvdd = devm_regulator_get(wm0010->dev, "DBVDD");
897 if (IS_ERR(wm0010->dbvdd)) {
898 ret = PTR_ERR(wm0010->dbvdd);
899 dev_err(wm0010->dev, "Failed to obtain DBVDD: %d\n", ret);
903 wm0010->reset = devm_gpiod_get(wm0010->dev, "reset", GPIOD_OUT_HIGH);
904 if (IS_ERR(wm0010->reset))
905 return dev_err_probe(wm0010->dev, PTR_ERR(wm0010->reset),
906 "could not get RESET GPIO\n");
907 gpiod_set_consumer_name(wm0010->reset, "wm0010 reset");
909 wm0010->state = WM0010_POWER_OFF;
912 if (wm0010->pdata.irq_flags)
913 trigger = wm0010->pdata.irq_flags;
915 trigger = IRQF_TRIGGER_FALLING;
916 trigger |= IRQF_ONESHOT;
918 ret = request_threaded_irq(irq, NULL, wm0010_irq, trigger,
921 dev_err(wm0010->dev, "Failed to request IRQ %d: %d\n",
927 ret = irq_set_irq_wake(irq, 1);
929 dev_err(wm0010->dev, "Failed to set IRQ %d as wake source: %d\n",
934 if (spi->max_speed_hz)
935 wm0010->board_max_spi_speed = spi->max_speed_hz;
937 wm0010->board_max_spi_speed = 0;
939 ret = devm_snd_soc_register_component(&spi->dev,
940 &soc_component_dev_wm0010, wm0010_dai,
941 ARRAY_SIZE(wm0010_dai));
948 static void wm0010_spi_remove(struct spi_device *spi)
950 struct wm0010_priv *wm0010 = spi_get_drvdata(spi);
952 gpiod_set_value_cansleep(wm0010->reset, 1);
954 irq_set_irq_wake(wm0010->irq, 0);
957 free_irq(wm0010->irq, wm0010);
960 static struct spi_driver wm0010_spi_driver = {
964 .probe = wm0010_spi_probe,
965 .remove = wm0010_spi_remove,
968 module_spi_driver(wm0010_spi_driver);
970 MODULE_DESCRIPTION("ASoC WM0010 driver");
971 MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
972 MODULE_LICENSE("GPL");