1 // SPDX-License-Identifier: GPL-2.0 2 // 3 // ALSA SoC Audio Layer - Samsung S/PDIF Controller driver 4 // 5 // Copyright (c) 2010 Samsung Electronics Co. Ltd 6 // http://www.samsung.com/ 7 8 #include <linux/clk.h> 9 #include <linux/io.h> 10 #include <linux/module.h> 11 12 #include <sound/soc.h> 13 #include <sound/pcm_params.h> 14 15 #include <linux/platform_data/asoc-s3c.h> 16 17 #include "dma.h" 18 #include "spdif.h" 19 20 /* Registers */ 21 #define CLKCON 0x00 22 #define CON 0x04 23 #define BSTAS 0x08 24 #define CSTAS 0x0C 25 #define DATA_OUTBUF 0x10 26 #define DCNT 0x14 27 #define BSTAS_S 0x18 28 #define DCNT_S 0x1C 29 30 #define CLKCTL_MASK 0x7 31 #define CLKCTL_MCLK_EXT (0x1 << 2) 32 #define CLKCTL_PWR_ON (0x1 << 0) 33 34 #define CON_MASK 0x3ffffff 35 #define CON_FIFO_TH_SHIFT 19 36 #define CON_FIFO_TH_MASK (0x7 << 19) 37 #define CON_USERDATA_23RDBIT (0x1 << 12) 38 39 #define CON_SW_RESET (0x1 << 5) 40 41 #define CON_MCLKDIV_MASK (0x3 << 3) 42 #define CON_MCLKDIV_256FS (0x0 << 3) 43 #define CON_MCLKDIV_384FS (0x1 << 3) 44 #define CON_MCLKDIV_512FS (0x2 << 3) 45 46 #define CON_PCM_MASK (0x3 << 1) 47 #define CON_PCM_16BIT (0x0 << 1) 48 #define CON_PCM_20BIT (0x1 << 1) 49 #define CON_PCM_24BIT (0x2 << 1) 50 51 #define CON_PCM_DATA (0x1 << 0) 52 53 #define CSTAS_MASK 0x3fffffff 54 #define CSTAS_SAMP_FREQ_MASK (0xF << 24) 55 #define CSTAS_SAMP_FREQ_44 (0x0 << 24) 56 #define CSTAS_SAMP_FREQ_48 (0x2 << 24) 57 #define CSTAS_SAMP_FREQ_32 (0x3 << 24) 58 #define CSTAS_SAMP_FREQ_96 (0xA << 24) 59 60 #define CSTAS_CATEGORY_MASK (0xFF << 8) 61 #define CSTAS_CATEGORY_CODE_CDP (0x01 << 8) 62 63 #define CSTAS_NO_COPYRIGHT (0x1 << 2) 64 65 /** 66 * struct samsung_spdif_info - Samsung S/PDIF Controller information 67 * @lock: Spin lock for S/PDIF. 68 * @dev: The parent device passed to use from the probe. 69 * @regs: The pointer to the device register block. 70 * @clk_rate: Current clock rate for calcurate ratio. 71 * @pclk: The peri-clock pointer for spdif master operation. 72 * @sclk: The source clock pointer for making sync signals. 73 * @save_clkcon: Backup clkcon reg. in suspend. 74 * @save_con: Backup con reg. in suspend. 75 * @save_cstas: Backup cstas reg. in suspend. 76 * @dma_playback: DMA information for playback channel. 77 */ 78 struct samsung_spdif_info { 79 spinlock_t lock; 80 struct device *dev; 81 void __iomem *regs; 82 unsigned long clk_rate; 83 struct clk *pclk; 84 struct clk *sclk; 85 u32 saved_clkcon; 86 u32 saved_con; 87 u32 saved_cstas; 88 struct snd_dmaengine_dai_dma_data *dma_playback; 89 }; 90 91 static struct snd_dmaengine_dai_dma_data spdif_stereo_out; 92 static struct samsung_spdif_info spdif_info; 93 94 static inline struct samsung_spdif_info *to_info(struct snd_soc_dai *cpu_dai) 95 { 96 return snd_soc_dai_get_drvdata(cpu_dai); 97 } 98 99 static void spdif_snd_txctrl(struct samsung_spdif_info *spdif, int on) 100 { 101 void __iomem *regs = spdif->regs; 102 u32 clkcon; 103 104 dev_dbg(spdif->dev, "Entered %s\n", __func__); 105 106 clkcon = readl(regs + CLKCON) & CLKCTL_MASK; 107 if (on) 108 writel(clkcon | CLKCTL_PWR_ON, regs + CLKCON); 109 else 110 writel(clkcon & ~CLKCTL_PWR_ON, regs + CLKCON); 111 } 112 113 static int spdif_set_sysclk(struct snd_soc_dai *cpu_dai, 114 int clk_id, unsigned int freq, int dir) 115 { 116 struct samsung_spdif_info *spdif = to_info(cpu_dai); 117 u32 clkcon; 118 119 dev_dbg(spdif->dev, "Entered %s\n", __func__); 120 121 clkcon = readl(spdif->regs + CLKCON); 122 123 if (clk_id == SND_SOC_SPDIF_INT_MCLK) 124 clkcon &= ~CLKCTL_MCLK_EXT; 125 else 126 clkcon |= CLKCTL_MCLK_EXT; 127 128 writel(clkcon, spdif->regs + CLKCON); 129 130 spdif->clk_rate = freq; 131 132 return 0; 133 } 134 135 static int spdif_trigger(struct snd_pcm_substream *substream, int cmd, 136 struct snd_soc_dai *dai) 137 { 138 struct snd_soc_pcm_runtime *rtd = substream->private_data; 139 struct samsung_spdif_info *spdif = to_info(rtd->cpu_dai); 140 unsigned long flags; 141 142 dev_dbg(spdif->dev, "Entered %s\n", __func__); 143 144 switch (cmd) { 145 case SNDRV_PCM_TRIGGER_START: 146 case SNDRV_PCM_TRIGGER_RESUME: 147 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: 148 spin_lock_irqsave(&spdif->lock, flags); 149 spdif_snd_txctrl(spdif, 1); 150 spin_unlock_irqrestore(&spdif->lock, flags); 151 break; 152 case SNDRV_PCM_TRIGGER_STOP: 153 case SNDRV_PCM_TRIGGER_SUSPEND: 154 case SNDRV_PCM_TRIGGER_PAUSE_PUSH: 155 spin_lock_irqsave(&spdif->lock, flags); 156 spdif_snd_txctrl(spdif, 0); 157 spin_unlock_irqrestore(&spdif->lock, flags); 158 break; 159 default: 160 return -EINVAL; 161 } 162 163 return 0; 164 } 165 166 static int spdif_sysclk_ratios[] = { 167 512, 384, 256, 168 }; 169 170 static int spdif_hw_params(struct snd_pcm_substream *substream, 171 struct snd_pcm_hw_params *params, 172 struct snd_soc_dai *socdai) 173 { 174 struct snd_soc_pcm_runtime *rtd = substream->private_data; 175 struct samsung_spdif_info *spdif = to_info(rtd->cpu_dai); 176 void __iomem *regs = spdif->regs; 177 struct snd_dmaengine_dai_dma_data *dma_data; 178 u32 con, clkcon, cstas; 179 unsigned long flags; 180 int i, ratio; 181 182 dev_dbg(spdif->dev, "Entered %s\n", __func__); 183 184 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) 185 dma_data = spdif->dma_playback; 186 else { 187 dev_err(spdif->dev, "Capture is not supported\n"); 188 return -EINVAL; 189 } 190 191 snd_soc_dai_set_dma_data(rtd->cpu_dai, substream, dma_data); 192 193 spin_lock_irqsave(&spdif->lock, flags); 194 195 con = readl(regs + CON) & CON_MASK; 196 cstas = readl(regs + CSTAS) & CSTAS_MASK; 197 clkcon = readl(regs + CLKCON) & CLKCTL_MASK; 198 199 con &= ~CON_FIFO_TH_MASK; 200 con |= (0x7 << CON_FIFO_TH_SHIFT); 201 con |= CON_USERDATA_23RDBIT; 202 con |= CON_PCM_DATA; 203 204 con &= ~CON_PCM_MASK; 205 switch (params_width(params)) { 206 case 16: 207 con |= CON_PCM_16BIT; 208 break; 209 default: 210 dev_err(spdif->dev, "Unsupported data size.\n"); 211 goto err; 212 } 213 214 ratio = spdif->clk_rate / params_rate(params); 215 for (i = 0; i < ARRAY_SIZE(spdif_sysclk_ratios); i++) 216 if (ratio == spdif_sysclk_ratios[i]) 217 break; 218 if (i == ARRAY_SIZE(spdif_sysclk_ratios)) { 219 dev_err(spdif->dev, "Invalid clock ratio %ld/%d\n", 220 spdif->clk_rate, params_rate(params)); 221 goto err; 222 } 223 224 con &= ~CON_MCLKDIV_MASK; 225 switch (ratio) { 226 case 256: 227 con |= CON_MCLKDIV_256FS; 228 break; 229 case 384: 230 con |= CON_MCLKDIV_384FS; 231 break; 232 case 512: 233 con |= CON_MCLKDIV_512FS; 234 break; 235 } 236 237 cstas &= ~CSTAS_SAMP_FREQ_MASK; 238 switch (params_rate(params)) { 239 case 44100: 240 cstas |= CSTAS_SAMP_FREQ_44; 241 break; 242 case 48000: 243 cstas |= CSTAS_SAMP_FREQ_48; 244 break; 245 case 32000: 246 cstas |= CSTAS_SAMP_FREQ_32; 247 break; 248 case 96000: 249 cstas |= CSTAS_SAMP_FREQ_96; 250 break; 251 default: 252 dev_err(spdif->dev, "Invalid sampling rate %d\n", 253 params_rate(params)); 254 goto err; 255 } 256 257 cstas &= ~CSTAS_CATEGORY_MASK; 258 cstas |= CSTAS_CATEGORY_CODE_CDP; 259 cstas |= CSTAS_NO_COPYRIGHT; 260 261 writel(con, regs + CON); 262 writel(cstas, regs + CSTAS); 263 writel(clkcon, regs + CLKCON); 264 265 spin_unlock_irqrestore(&spdif->lock, flags); 266 267 return 0; 268 err: 269 spin_unlock_irqrestore(&spdif->lock, flags); 270 return -EINVAL; 271 } 272 273 static void spdif_shutdown(struct snd_pcm_substream *substream, 274 struct snd_soc_dai *dai) 275 { 276 struct snd_soc_pcm_runtime *rtd = substream->private_data; 277 struct samsung_spdif_info *spdif = to_info(rtd->cpu_dai); 278 void __iomem *regs = spdif->regs; 279 u32 con, clkcon; 280 281 dev_dbg(spdif->dev, "Entered %s\n", __func__); 282 283 con = readl(regs + CON) & CON_MASK; 284 clkcon = readl(regs + CLKCON) & CLKCTL_MASK; 285 286 writel(con | CON_SW_RESET, regs + CON); 287 cpu_relax(); 288 289 writel(clkcon & ~CLKCTL_PWR_ON, regs + CLKCON); 290 } 291 292 #ifdef CONFIG_PM 293 static int spdif_suspend(struct snd_soc_dai *cpu_dai) 294 { 295 struct samsung_spdif_info *spdif = to_info(cpu_dai); 296 u32 con = spdif->saved_con; 297 298 dev_dbg(spdif->dev, "Entered %s\n", __func__); 299 300 spdif->saved_clkcon = readl(spdif->regs + CLKCON) & CLKCTL_MASK; 301 spdif->saved_con = readl(spdif->regs + CON) & CON_MASK; 302 spdif->saved_cstas = readl(spdif->regs + CSTAS) & CSTAS_MASK; 303 304 writel(con | CON_SW_RESET, spdif->regs + CON); 305 cpu_relax(); 306 307 return 0; 308 } 309 310 static int spdif_resume(struct snd_soc_dai *cpu_dai) 311 { 312 struct samsung_spdif_info *spdif = to_info(cpu_dai); 313 314 dev_dbg(spdif->dev, "Entered %s\n", __func__); 315 316 writel(spdif->saved_clkcon, spdif->regs + CLKCON); 317 writel(spdif->saved_con, spdif->regs + CON); 318 writel(spdif->saved_cstas, spdif->regs + CSTAS); 319 320 return 0; 321 } 322 #else 323 #define spdif_suspend NULL 324 #define spdif_resume NULL 325 #endif 326 327 static const struct snd_soc_dai_ops spdif_dai_ops = { 328 .set_sysclk = spdif_set_sysclk, 329 .trigger = spdif_trigger, 330 .hw_params = spdif_hw_params, 331 .shutdown = spdif_shutdown, 332 }; 333 334 static struct snd_soc_dai_driver samsung_spdif_dai = { 335 .name = "samsung-spdif", 336 .playback = { 337 .stream_name = "S/PDIF Playback", 338 .channels_min = 2, 339 .channels_max = 2, 340 .rates = (SNDRV_PCM_RATE_32000 | 341 SNDRV_PCM_RATE_44100 | 342 SNDRV_PCM_RATE_48000 | 343 SNDRV_PCM_RATE_96000), 344 .formats = SNDRV_PCM_FMTBIT_S16_LE, }, 345 .ops = &spdif_dai_ops, 346 .suspend = spdif_suspend, 347 .resume = spdif_resume, 348 }; 349 350 static const struct snd_soc_component_driver samsung_spdif_component = { 351 .name = "samsung-spdif", 352 }; 353 354 static int spdif_probe(struct platform_device *pdev) 355 { 356 struct s3c_audio_pdata *spdif_pdata; 357 struct resource *mem_res; 358 struct samsung_spdif_info *spdif; 359 dma_filter_fn filter; 360 int ret; 361 362 spdif_pdata = pdev->dev.platform_data; 363 364 dev_dbg(&pdev->dev, "Entered %s\n", __func__); 365 366 mem_res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 367 if (!mem_res) { 368 dev_err(&pdev->dev, "Unable to get register resource.\n"); 369 return -ENXIO; 370 } 371 372 if (spdif_pdata && spdif_pdata->cfg_gpio 373 && spdif_pdata->cfg_gpio(pdev)) { 374 dev_err(&pdev->dev, "Unable to configure GPIO pins\n"); 375 return -EINVAL; 376 } 377 378 spdif = &spdif_info; 379 spdif->dev = &pdev->dev; 380 381 spin_lock_init(&spdif->lock); 382 383 spdif->pclk = devm_clk_get(&pdev->dev, "spdif"); 384 if (IS_ERR(spdif->pclk)) { 385 dev_err(&pdev->dev, "failed to get peri-clock\n"); 386 ret = -ENOENT; 387 goto err0; 388 } 389 ret = clk_prepare_enable(spdif->pclk); 390 if (ret) 391 goto err0; 392 393 spdif->sclk = devm_clk_get(&pdev->dev, "sclk_spdif"); 394 if (IS_ERR(spdif->sclk)) { 395 dev_err(&pdev->dev, "failed to get internal source clock\n"); 396 ret = -ENOENT; 397 goto err1; 398 } 399 ret = clk_prepare_enable(spdif->sclk); 400 if (ret) 401 goto err1; 402 403 /* Request S/PDIF Register's memory region */ 404 if (!request_mem_region(mem_res->start, 405 resource_size(mem_res), "samsung-spdif")) { 406 dev_err(&pdev->dev, "Unable to request register region\n"); 407 ret = -EBUSY; 408 goto err2; 409 } 410 411 spdif->regs = ioremap(mem_res->start, 0x100); 412 if (spdif->regs == NULL) { 413 dev_err(&pdev->dev, "Cannot ioremap registers\n"); 414 ret = -ENXIO; 415 goto err3; 416 } 417 418 spdif_stereo_out.addr_width = 2; 419 spdif_stereo_out.addr = mem_res->start + DATA_OUTBUF; 420 filter = NULL; 421 if (spdif_pdata) { 422 spdif_stereo_out.filter_data = spdif_pdata->dma_playback; 423 filter = spdif_pdata->dma_filter; 424 } 425 spdif->dma_playback = &spdif_stereo_out; 426 427 ret = samsung_asoc_dma_platform_register(&pdev->dev, filter, 428 NULL, NULL, NULL); 429 if (ret) { 430 dev_err(&pdev->dev, "failed to register DMA: %d\n", ret); 431 goto err4; 432 } 433 434 dev_set_drvdata(&pdev->dev, spdif); 435 436 ret = devm_snd_soc_register_component(&pdev->dev, 437 &samsung_spdif_component, &samsung_spdif_dai, 1); 438 if (ret != 0) { 439 dev_err(&pdev->dev, "fail to register dai\n"); 440 goto err4; 441 } 442 443 return 0; 444 err4: 445 iounmap(spdif->regs); 446 err3: 447 release_mem_region(mem_res->start, resource_size(mem_res)); 448 err2: 449 clk_disable_unprepare(spdif->sclk); 450 err1: 451 clk_disable_unprepare(spdif->pclk); 452 err0: 453 return ret; 454 } 455 456 static int spdif_remove(struct platform_device *pdev) 457 { 458 struct samsung_spdif_info *spdif = &spdif_info; 459 struct resource *mem_res; 460 461 iounmap(spdif->regs); 462 463 mem_res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 464 if (mem_res) 465 release_mem_region(mem_res->start, resource_size(mem_res)); 466 467 clk_disable_unprepare(spdif->sclk); 468 clk_disable_unprepare(spdif->pclk); 469 470 return 0; 471 } 472 473 static struct platform_driver samsung_spdif_driver = { 474 .probe = spdif_probe, 475 .remove = spdif_remove, 476 .driver = { 477 .name = "samsung-spdif", 478 }, 479 }; 480 481 module_platform_driver(samsung_spdif_driver); 482 483 MODULE_AUTHOR("Seungwhan Youn, <sw.youn@samsung.com>"); 484 MODULE_DESCRIPTION("Samsung S/PDIF Controller Driver"); 485 MODULE_LICENSE("GPL"); 486 MODULE_ALIAS("platform:samsung-spdif"); 487