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