^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) //
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) // Freescale ALSA SoC Machine driver utility
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) //
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) // Author: Timur Tabi <timur@freescale.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) //
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) // Copyright 2010 Freescale Semiconductor, Inc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/of_address.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <sound/soc.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include "fsl_utils.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) * fsl_asoc_get_dma_channel - determine the dma channel for a SSI node
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) * @ssi_np: pointer to the SSI device tree node
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) * @name: name of the phandle pointing to the dma channel
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) * @dai: ASoC DAI link pointer to be filled with platform_name
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) * @dma_channel_id: dma channel id to be returned
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) * @dma_id: dma id to be returned
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) * This function determines the dma and channel id for given SSI node. It
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) * also discovers the platform_name for the ASoC DAI link.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) int fsl_asoc_get_dma_channel(struct device_node *ssi_np,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) const char *name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) struct snd_soc_dai_link *dai,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) unsigned int *dma_channel_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) unsigned int *dma_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) struct resource res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) struct device_node *dma_channel_np, *dma_np;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) const __be32 *iprop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) dma_channel_np = of_parse_phandle(ssi_np, name, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) if (!dma_channel_np)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) if (!of_device_is_compatible(dma_channel_np, "fsl,ssi-dma-channel")) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) of_node_put(dma_channel_np);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) /* Determine the dev_name for the device_node. This code mimics the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) * behavior of of_device_make_bus_id(). We need this because ASoC uses
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) * the dev_name() of the device to match the platform (DMA) device with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) * the CPU (SSI) device. It's all ugly and hackish, but it works (for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) * now).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) * dai->platform name should already point to an allocated buffer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) ret = of_address_to_resource(dma_channel_np, 0, &res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) of_node_put(dma_channel_np);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) snprintf((char *)dai->platforms->name, DAI_NAME_SIZE, "%llx.%pOFn",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) (unsigned long long) res.start, dma_channel_np);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) iprop = of_get_property(dma_channel_np, "cell-index", NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) if (!iprop) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) of_node_put(dma_channel_np);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) *dma_channel_id = be32_to_cpup(iprop);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) dma_np = of_get_parent(dma_channel_np);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) iprop = of_get_property(dma_np, "cell-index", NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) if (!iprop) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) of_node_put(dma_np);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) of_node_put(dma_channel_np);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) *dma_id = be32_to_cpup(iprop);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) of_node_put(dma_np);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) of_node_put(dma_channel_np);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) EXPORT_SYMBOL(fsl_asoc_get_dma_channel);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) MODULE_AUTHOR("Timur Tabi <timur@freescale.com>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) MODULE_DESCRIPTION("Freescale ASoC utility code");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) MODULE_LICENSE("GPL v2");