1 // SPDX-License-Identifier: GPL-2.0 2 // 3 // Freescale P1022RDK ALSA SoC Machine driver 4 // 5 // Author: Timur Tabi <timur@freescale.com> 6 // 7 // Copyright 2012 Freescale Semiconductor, Inc. 8 // 9 // Note: in order for audio to work correctly, the output controls need 10 // to be enabled, because they control the clock. So for playback, for 11 // example: 12 // 13 // amixer sset 'Left Output Mixer PCM' on 14 // amixer sset 'Right Output Mixer PCM' on 15 16 #include <linux/module.h> 17 #include <linux/fsl/guts.h> 18 #include <linux/interrupt.h> 19 #include <linux/of_address.h> 20 #include <linux/of_device.h> 21 #include <linux/slab.h> 22 #include <sound/soc.h> 23 24 #include "fsl_dma.h" 25 #include "fsl_ssi.h" 26 #include "fsl_utils.h" 27 28 /* P1022-specific PMUXCR and DMUXCR bit definitions */ 29 30 #define CCSR_GUTS_PMUXCR_UART0_I2C1_MASK 0x0001c000 31 #define CCSR_GUTS_PMUXCR_UART0_I2C1_UART0_SSI 0x00010000 32 #define CCSR_GUTS_PMUXCR_UART0_I2C1_SSI 0x00018000 33 34 #define CCSR_GUTS_PMUXCR_SSI_DMA_TDM_MASK 0x00000c00 35 #define CCSR_GUTS_PMUXCR_SSI_DMA_TDM_SSI 0x00000000 36 37 #define CCSR_GUTS_DMUXCR_PAD 1 /* DMA controller/channel set to pad */ 38 #define CCSR_GUTS_DMUXCR_SSI 2 /* DMA controller/channel set to SSI */ 39 40 /* 41 * Set the DMACR register in the GUTS 42 * 43 * The DMACR register determines the source of initiated transfers for each 44 * channel on each DMA controller. Rather than have a bunch of repetitive 45 * macros for the bit patterns, we just have a function that calculates 46 * them. 47 * 48 * guts: Pointer to GUTS structure 49 * co: The DMA controller (0 or 1) 50 * ch: The channel on the DMA controller (0, 1, 2, or 3) 51 * device: The device to set as the target (CCSR_GUTS_DMUXCR_xxx) 52 */ 53 static inline void guts_set_dmuxcr(struct ccsr_guts __iomem *guts, 54 unsigned int co, unsigned int ch, unsigned int device) 55 { 56 unsigned int shift = 16 + (8 * (1 - co) + 2 * (3 - ch)); 57 58 clrsetbits_be32(&guts->dmuxcr, 3 << shift, device << shift); 59 } 60 61 /* There's only one global utilities register */ 62 static phys_addr_t guts_phys; 63 64 /** 65 * machine_data: machine-specific ASoC device data 66 * 67 * This structure contains data for a single sound platform device on an 68 * P1022 RDK. Some of the data is taken from the device tree. 69 */ 70 struct machine_data { 71 struct snd_soc_dai_link dai[2]; 72 struct snd_soc_card card; 73 unsigned int dai_format; 74 unsigned int codec_clk_direction; 75 unsigned int cpu_clk_direction; 76 unsigned int clk_frequency; 77 unsigned int dma_id[2]; /* 0 = DMA1, 1 = DMA2, etc */ 78 unsigned int dma_channel_id[2]; /* 0 = ch 0, 1 = ch 1, etc*/ 79 char platform_name[2][DAI_NAME_SIZE]; /* One for each DMA channel */ 80 }; 81 82 /** 83 * p1022_rdk_machine_probe: initialize the board 84 * 85 * This function is used to initialize the board-specific hardware. 86 * 87 * Here we program the DMACR and PMUXCR registers. 88 */ 89 static int p1022_rdk_machine_probe(struct snd_soc_card *card) 90 { 91 struct machine_data *mdata = 92 container_of(card, struct machine_data, card); 93 struct ccsr_guts __iomem *guts; 94 95 guts = ioremap(guts_phys, sizeof(struct ccsr_guts)); 96 if (!guts) { 97 dev_err(card->dev, "could not map global utilities\n"); 98 return -ENOMEM; 99 } 100 101 /* Enable SSI Tx signal */ 102 clrsetbits_be32(&guts->pmuxcr, CCSR_GUTS_PMUXCR_UART0_I2C1_MASK, 103 CCSR_GUTS_PMUXCR_UART0_I2C1_UART0_SSI); 104 105 /* Enable SSI Rx signal */ 106 clrsetbits_be32(&guts->pmuxcr, CCSR_GUTS_PMUXCR_SSI_DMA_TDM_MASK, 107 CCSR_GUTS_PMUXCR_SSI_DMA_TDM_SSI); 108 109 /* Enable DMA Channel for SSI */ 110 guts_set_dmuxcr(guts, mdata->dma_id[0], mdata->dma_channel_id[0], 111 CCSR_GUTS_DMUXCR_SSI); 112 113 guts_set_dmuxcr(guts, mdata->dma_id[1], mdata->dma_channel_id[1], 114 CCSR_GUTS_DMUXCR_SSI); 115 116 iounmap(guts); 117 118 return 0; 119 } 120 121 /** 122 * p1022_rdk_startup: program the board with various hardware parameters 123 * 124 * This function takes board-specific information, like clock frequencies 125 * and serial data formats, and passes that information to the codec and 126 * transport drivers. 127 */ 128 static int p1022_rdk_startup(struct snd_pcm_substream *substream) 129 { 130 struct snd_soc_pcm_runtime *rtd = substream->private_data; 131 struct machine_data *mdata = 132 container_of(rtd->card, struct machine_data, card); 133 struct device *dev = rtd->card->dev; 134 int ret = 0; 135 136 /* Tell the codec driver what the serial protocol is. */ 137 ret = snd_soc_dai_set_fmt(rtd->codec_dai, mdata->dai_format); 138 if (ret < 0) { 139 dev_err(dev, "could not set codec driver audio format (ret=%i)\n", 140 ret); 141 return ret; 142 } 143 144 ret = snd_soc_dai_set_pll(rtd->codec_dai, 0, 0, mdata->clk_frequency, 145 mdata->clk_frequency); 146 if (ret < 0) { 147 dev_err(dev, "could not set codec PLL frequency (ret=%i)\n", 148 ret); 149 return ret; 150 } 151 152 return 0; 153 } 154 155 /** 156 * p1022_rdk_machine_remove: Remove the sound device 157 * 158 * This function is called to remove the sound device for one SSI. We 159 * de-program the DMACR and PMUXCR register. 160 */ 161 static int p1022_rdk_machine_remove(struct snd_soc_card *card) 162 { 163 struct machine_data *mdata = 164 container_of(card, struct machine_data, card); 165 struct ccsr_guts __iomem *guts; 166 167 guts = ioremap(guts_phys, sizeof(struct ccsr_guts)); 168 if (!guts) { 169 dev_err(card->dev, "could not map global utilities\n"); 170 return -ENOMEM; 171 } 172 173 /* Restore the signal routing */ 174 clrbits32(&guts->pmuxcr, CCSR_GUTS_PMUXCR_UART0_I2C1_MASK); 175 clrbits32(&guts->pmuxcr, CCSR_GUTS_PMUXCR_SSI_DMA_TDM_MASK); 176 guts_set_dmuxcr(guts, mdata->dma_id[0], mdata->dma_channel_id[0], 0); 177 guts_set_dmuxcr(guts, mdata->dma_id[1], mdata->dma_channel_id[1], 0); 178 179 iounmap(guts); 180 181 return 0; 182 } 183 184 /** 185 * p1022_rdk_ops: ASoC machine driver operations 186 */ 187 static const struct snd_soc_ops p1022_rdk_ops = { 188 .startup = p1022_rdk_startup, 189 }; 190 191 /** 192 * p1022_rdk_probe: platform probe function for the machine driver 193 * 194 * Although this is a machine driver, the SSI node is the "master" node with 195 * respect to audio hardware connections. Therefore, we create a new ASoC 196 * device for each new SSI node that has a codec attached. 197 */ 198 static int p1022_rdk_probe(struct platform_device *pdev) 199 { 200 struct device *dev = pdev->dev.parent; 201 /* ssi_pdev is the platform device for the SSI node that probed us */ 202 struct platform_device *ssi_pdev = to_platform_device(dev); 203 struct device_node *np = ssi_pdev->dev.of_node; 204 struct device_node *codec_np = NULL; 205 struct machine_data *mdata; 206 const u32 *iprop; 207 int ret; 208 209 /* Find the codec node for this SSI. */ 210 codec_np = of_parse_phandle(np, "codec-handle", 0); 211 if (!codec_np) { 212 dev_err(dev, "could not find codec node\n"); 213 return -EINVAL; 214 } 215 216 mdata = kzalloc(sizeof(struct machine_data), GFP_KERNEL); 217 if (!mdata) { 218 ret = -ENOMEM; 219 goto error_put; 220 } 221 222 mdata->dai[0].cpu_dai_name = dev_name(&ssi_pdev->dev); 223 mdata->dai[0].ops = &p1022_rdk_ops; 224 225 /* ASoC core can match codec with device node */ 226 mdata->dai[0].codec_of_node = codec_np; 227 228 /* 229 * We register two DAIs per SSI, one for playback and the other for 230 * capture. We support codecs that have separate DAIs for both playback 231 * and capture. 232 */ 233 memcpy(&mdata->dai[1], &mdata->dai[0], sizeof(struct snd_soc_dai_link)); 234 235 /* The DAI names from the codec (snd_soc_dai_driver.name) */ 236 mdata->dai[0].codec_dai_name = "wm8960-hifi"; 237 mdata->dai[1].codec_dai_name = mdata->dai[0].codec_dai_name; 238 239 /* 240 * Configure the SSI for I2S slave mode. Older device trees have 241 * an fsl,mode property, but we ignore that since there's really 242 * only one way to configure the SSI. 243 */ 244 mdata->dai_format = SND_SOC_DAIFMT_NB_NF | 245 SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_CBM_CFM; 246 mdata->codec_clk_direction = SND_SOC_CLOCK_OUT; 247 mdata->cpu_clk_direction = SND_SOC_CLOCK_IN; 248 249 /* 250 * In i2s-slave mode, the codec has its own clock source, so we 251 * need to get the frequency from the device tree and pass it to 252 * the codec driver. 253 */ 254 iprop = of_get_property(codec_np, "clock-frequency", NULL); 255 if (!iprop || !*iprop) { 256 dev_err(&pdev->dev, "codec bus-frequency property is missing or invalid\n"); 257 ret = -EINVAL; 258 goto error; 259 } 260 mdata->clk_frequency = be32_to_cpup(iprop); 261 262 if (!mdata->clk_frequency) { 263 dev_err(&pdev->dev, "unknown clock frequency\n"); 264 ret = -EINVAL; 265 goto error; 266 } 267 268 /* Find the playback DMA channel to use. */ 269 mdata->dai[0].platform_name = mdata->platform_name[0]; 270 ret = fsl_asoc_get_dma_channel(np, "fsl,playback-dma", &mdata->dai[0], 271 &mdata->dma_channel_id[0], 272 &mdata->dma_id[0]); 273 if (ret) { 274 dev_err(&pdev->dev, "missing/invalid playback DMA phandle (ret=%i)\n", 275 ret); 276 goto error; 277 } 278 279 /* Find the capture DMA channel to use. */ 280 mdata->dai[1].platform_name = mdata->platform_name[1]; 281 ret = fsl_asoc_get_dma_channel(np, "fsl,capture-dma", &mdata->dai[1], 282 &mdata->dma_channel_id[1], 283 &mdata->dma_id[1]); 284 if (ret) { 285 dev_err(&pdev->dev, "missing/invalid capture DMA phandle (ret=%i)\n", 286 ret); 287 goto error; 288 } 289 290 /* Initialize our DAI data structure. */ 291 mdata->dai[0].stream_name = "playback"; 292 mdata->dai[1].stream_name = "capture"; 293 mdata->dai[0].name = mdata->dai[0].stream_name; 294 mdata->dai[1].name = mdata->dai[1].stream_name; 295 296 mdata->card.probe = p1022_rdk_machine_probe; 297 mdata->card.remove = p1022_rdk_machine_remove; 298 mdata->card.name = pdev->name; /* The platform driver name */ 299 mdata->card.owner = THIS_MODULE; 300 mdata->card.dev = &pdev->dev; 301 mdata->card.num_links = 2; 302 mdata->card.dai_link = mdata->dai; 303 304 /* Register with ASoC */ 305 ret = snd_soc_register_card(&mdata->card); 306 if (ret) { 307 dev_err(&pdev->dev, "could not register card (ret=%i)\n", ret); 308 goto error; 309 } 310 311 return 0; 312 313 error: 314 kfree(mdata); 315 error_put: 316 of_node_put(codec_np); 317 return ret; 318 } 319 320 /** 321 * p1022_rdk_remove: remove the platform device 322 * 323 * This function is called when the platform device is removed. 324 */ 325 static int p1022_rdk_remove(struct platform_device *pdev) 326 { 327 struct snd_soc_card *card = platform_get_drvdata(pdev); 328 struct machine_data *mdata = 329 container_of(card, struct machine_data, card); 330 331 snd_soc_unregister_card(card); 332 kfree(mdata); 333 334 return 0; 335 } 336 337 static struct platform_driver p1022_rdk_driver = { 338 .probe = p1022_rdk_probe, 339 .remove = p1022_rdk_remove, 340 .driver = { 341 /* 342 * The name must match 'compatible' property in the device tree, 343 * in lowercase letters. 344 */ 345 .name = "snd-soc-p1022rdk", 346 }, 347 }; 348 349 /** 350 * p1022_rdk_init: machine driver initialization. 351 * 352 * This function is called when this module is loaded. 353 */ 354 static int __init p1022_rdk_init(void) 355 { 356 struct device_node *guts_np; 357 struct resource res; 358 359 /* Get the physical address of the global utilities registers */ 360 guts_np = of_find_compatible_node(NULL, NULL, "fsl,p1022-guts"); 361 if (of_address_to_resource(guts_np, 0, &res)) { 362 pr_err("snd-soc-p1022rdk: missing/invalid global utils node\n"); 363 of_node_put(guts_np); 364 return -EINVAL; 365 } 366 guts_phys = res.start; 367 of_node_put(guts_np); 368 369 return platform_driver_register(&p1022_rdk_driver); 370 } 371 372 /** 373 * p1022_rdk_exit: machine driver exit 374 * 375 * This function is called when this driver is unloaded. 376 */ 377 static void __exit p1022_rdk_exit(void) 378 { 379 platform_driver_unregister(&p1022_rdk_driver); 380 } 381 382 late_initcall(p1022_rdk_init); 383 module_exit(p1022_rdk_exit); 384 385 MODULE_AUTHOR("Timur Tabi <timur@freescale.com>"); 386 MODULE_DESCRIPTION("Freescale / iVeia P1022 RDK ALSA SoC machine driver"); 387 MODULE_LICENSE("GPL v2"); 388