1 /* 2 * rt5514-spi.c -- RT5514 SPI driver 3 * 4 * Copyright 2015 Realtek Semiconductor Corp. 5 * Author: Oder Chiou <oder_chiou@realtek.com> 6 * 7 * This program is free software; you can redistribute it and/or modify 8 * it under the terms of the GNU General Public License version 2 as 9 * published by the Free Software Foundation. 10 */ 11 12 #include <linux/module.h> 13 #include <linux/input.h> 14 #include <linux/spi/spi.h> 15 #include <linux/device.h> 16 #include <linux/init.h> 17 #include <linux/delay.h> 18 #include <linux/interrupt.h> 19 #include <linux/irq.h> 20 #include <linux/slab.h> 21 #include <linux/gpio.h> 22 #include <linux/sched.h> 23 #include <linux/uaccess.h> 24 #include <linux/regulator/consumer.h> 25 #include <linux/pm_qos.h> 26 #include <linux/sysfs.h> 27 #include <linux/clk.h> 28 #include <sound/core.h> 29 #include <sound/pcm.h> 30 #include <sound/pcm_params.h> 31 #include <sound/soc.h> 32 #include <sound/soc-dapm.h> 33 #include <sound/initval.h> 34 #include <sound/tlv.h> 35 36 #include "rt5514-spi.h" 37 38 static struct spi_device *rt5514_spi; 39 40 struct rt5514_dsp { 41 struct device *dev; 42 struct delayed_work copy_work; 43 struct mutex dma_lock; 44 struct snd_pcm_substream *substream; 45 unsigned int buf_base, buf_limit, buf_rp; 46 size_t buf_size, get_size, dma_offset; 47 }; 48 49 static const struct snd_pcm_hardware rt5514_spi_pcm_hardware = { 50 .info = SNDRV_PCM_INFO_MMAP | 51 SNDRV_PCM_INFO_MMAP_VALID | 52 SNDRV_PCM_INFO_INTERLEAVED, 53 .formats = SNDRV_PCM_FMTBIT_S16_LE, 54 .period_bytes_min = PAGE_SIZE, 55 .period_bytes_max = 0x20000 / 8, 56 .periods_min = 8, 57 .periods_max = 8, 58 .channels_min = 1, 59 .channels_max = 1, 60 .buffer_bytes_max = 0x20000, 61 }; 62 63 static struct snd_soc_dai_driver rt5514_spi_dai = { 64 .name = "rt5514-dsp-cpu-dai", 65 .id = 0, 66 .capture = { 67 .stream_name = "DSP Capture", 68 .channels_min = 1, 69 .channels_max = 1, 70 .rates = SNDRV_PCM_RATE_16000, 71 .formats = SNDRV_PCM_FMTBIT_S16_LE, 72 }, 73 }; 74 75 static void rt5514_spi_copy_work(struct work_struct *work) 76 { 77 struct rt5514_dsp *rt5514_dsp = 78 container_of(work, struct rt5514_dsp, copy_work.work); 79 struct snd_pcm_runtime *runtime; 80 size_t period_bytes, truncated_bytes = 0; 81 unsigned int cur_wp, remain_data; 82 u8 buf[8]; 83 84 mutex_lock(&rt5514_dsp->dma_lock); 85 if (!rt5514_dsp->substream) { 86 dev_err(rt5514_dsp->dev, "No pcm substream\n"); 87 goto done; 88 } 89 90 runtime = rt5514_dsp->substream->runtime; 91 period_bytes = snd_pcm_lib_period_bytes(rt5514_dsp->substream); 92 93 if (rt5514_dsp->get_size >= rt5514_dsp->buf_size) { 94 rt5514_spi_burst_read(RT5514_BUFFER_VOICE_WP, (u8 *)&buf, 95 sizeof(buf)); 96 cur_wp = buf[0] | buf[1] << 8 | buf[2] << 16 | 97 buf[3] << 24; 98 99 if (cur_wp >= rt5514_dsp->buf_rp) 100 remain_data = (cur_wp - rt5514_dsp->buf_rp); 101 else 102 remain_data = 103 (rt5514_dsp->buf_limit - rt5514_dsp->buf_rp) + 104 (cur_wp - rt5514_dsp->buf_base); 105 106 if (remain_data < period_bytes) { 107 schedule_delayed_work(&rt5514_dsp->copy_work, 5); 108 goto done; 109 } 110 } 111 112 if (rt5514_dsp->buf_rp + period_bytes <= rt5514_dsp->buf_limit) { 113 rt5514_spi_burst_read(rt5514_dsp->buf_rp, 114 runtime->dma_area + rt5514_dsp->dma_offset, 115 period_bytes); 116 117 if (rt5514_dsp->buf_rp + period_bytes == rt5514_dsp->buf_limit) 118 rt5514_dsp->buf_rp = rt5514_dsp->buf_base; 119 else 120 rt5514_dsp->buf_rp += period_bytes; 121 } else { 122 truncated_bytes = rt5514_dsp->buf_limit - rt5514_dsp->buf_rp; 123 rt5514_spi_burst_read(rt5514_dsp->buf_rp, 124 runtime->dma_area + rt5514_dsp->dma_offset, 125 truncated_bytes); 126 127 rt5514_spi_burst_read(rt5514_dsp->buf_base, 128 runtime->dma_area + rt5514_dsp->dma_offset + 129 truncated_bytes, period_bytes - truncated_bytes); 130 131 rt5514_dsp->buf_rp = rt5514_dsp->buf_base + period_bytes - 132 truncated_bytes; 133 } 134 135 rt5514_dsp->get_size += period_bytes; 136 rt5514_dsp->dma_offset += period_bytes; 137 if (rt5514_dsp->dma_offset >= runtime->dma_bytes) 138 rt5514_dsp->dma_offset = 0; 139 140 snd_pcm_period_elapsed(rt5514_dsp->substream); 141 142 schedule_delayed_work(&rt5514_dsp->copy_work, 5); 143 144 done: 145 mutex_unlock(&rt5514_dsp->dma_lock); 146 } 147 148 static void rt5514_schedule_copy(struct rt5514_dsp *rt5514_dsp) 149 { 150 size_t period_bytes; 151 u8 buf[8]; 152 153 if (!rt5514_dsp->substream) 154 return; 155 156 period_bytes = snd_pcm_lib_period_bytes(rt5514_dsp->substream); 157 rt5514_dsp->get_size = 0; 158 159 /** 160 * The address area x1800XXXX is the register address, and it cannot 161 * support spi burst read perfectly. So we use the spi burst read 162 * individually to make sure the data correctly. 163 */ 164 rt5514_spi_burst_read(RT5514_BUFFER_VOICE_BASE, (u8 *)&buf, 165 sizeof(buf)); 166 rt5514_dsp->buf_base = buf[0] | buf[1] << 8 | buf[2] << 16 | 167 buf[3] << 24; 168 169 rt5514_spi_burst_read(RT5514_BUFFER_VOICE_LIMIT, (u8 *)&buf, 170 sizeof(buf)); 171 rt5514_dsp->buf_limit = buf[0] | buf[1] << 8 | buf[2] << 16 | 172 buf[3] << 24; 173 174 rt5514_spi_burst_read(RT5514_BUFFER_VOICE_WP, (u8 *)&buf, 175 sizeof(buf)); 176 rt5514_dsp->buf_rp = buf[0] | buf[1] << 8 | buf[2] << 16 | 177 buf[3] << 24; 178 179 if (rt5514_dsp->buf_rp % 8) 180 rt5514_dsp->buf_rp = (rt5514_dsp->buf_rp / 8) * 8; 181 182 rt5514_dsp->buf_size = rt5514_dsp->buf_limit - rt5514_dsp->buf_base; 183 184 if (rt5514_dsp->buf_size % period_bytes) 185 rt5514_dsp->buf_size = (rt5514_dsp->buf_size / period_bytes) * 186 period_bytes; 187 188 if (rt5514_dsp->buf_base && rt5514_dsp->buf_limit && 189 rt5514_dsp->buf_rp && rt5514_dsp->buf_size) 190 schedule_delayed_work(&rt5514_dsp->copy_work, 0); 191 } 192 193 static irqreturn_t rt5514_spi_irq(int irq, void *data) 194 { 195 struct rt5514_dsp *rt5514_dsp = data; 196 197 rt5514_schedule_copy(rt5514_dsp); 198 199 return IRQ_HANDLED; 200 } 201 202 /* PCM for streaming audio from the DSP buffer */ 203 static int rt5514_spi_pcm_open(struct snd_pcm_substream *substream) 204 { 205 snd_soc_set_runtime_hwparams(substream, &rt5514_spi_pcm_hardware); 206 207 return 0; 208 } 209 210 static int rt5514_spi_hw_params(struct snd_pcm_substream *substream, 211 struct snd_pcm_hw_params *hw_params) 212 { 213 struct snd_soc_pcm_runtime *rtd = substream->private_data; 214 struct rt5514_dsp *rt5514_dsp = 215 snd_soc_platform_get_drvdata(rtd->platform); 216 int ret; 217 u8 buf[8]; 218 219 mutex_lock(&rt5514_dsp->dma_lock); 220 ret = snd_pcm_lib_alloc_vmalloc_buffer(substream, 221 params_buffer_bytes(hw_params)); 222 rt5514_dsp->substream = substream; 223 rt5514_dsp->dma_offset = 0; 224 225 /* Read IRQ status and schedule copy accordingly. */ 226 rt5514_spi_burst_read(RT5514_IRQ_CTRL, (u8 *)&buf, sizeof(buf)); 227 if (buf[0] & RT5514_IRQ_STATUS_BIT) 228 rt5514_schedule_copy(rt5514_dsp); 229 230 mutex_unlock(&rt5514_dsp->dma_lock); 231 232 return ret; 233 } 234 235 static int rt5514_spi_hw_free(struct snd_pcm_substream *substream) 236 { 237 struct snd_soc_pcm_runtime *rtd = substream->private_data; 238 struct rt5514_dsp *rt5514_dsp = 239 snd_soc_platform_get_drvdata(rtd->platform); 240 241 mutex_lock(&rt5514_dsp->dma_lock); 242 rt5514_dsp->substream = NULL; 243 mutex_unlock(&rt5514_dsp->dma_lock); 244 245 cancel_delayed_work_sync(&rt5514_dsp->copy_work); 246 247 return snd_pcm_lib_free_vmalloc_buffer(substream); 248 } 249 250 static snd_pcm_uframes_t rt5514_spi_pcm_pointer( 251 struct snd_pcm_substream *substream) 252 { 253 struct snd_pcm_runtime *runtime = substream->runtime; 254 struct snd_soc_pcm_runtime *rtd = substream->private_data; 255 struct rt5514_dsp *rt5514_dsp = 256 snd_soc_platform_get_drvdata(rtd->platform); 257 258 return bytes_to_frames(runtime, rt5514_dsp->dma_offset); 259 } 260 261 static const struct snd_pcm_ops rt5514_spi_pcm_ops = { 262 .open = rt5514_spi_pcm_open, 263 .hw_params = rt5514_spi_hw_params, 264 .hw_free = rt5514_spi_hw_free, 265 .pointer = rt5514_spi_pcm_pointer, 266 .mmap = snd_pcm_lib_mmap_vmalloc, 267 .page = snd_pcm_lib_get_vmalloc_page, 268 }; 269 270 static int rt5514_spi_pcm_probe(struct snd_soc_platform *platform) 271 { 272 struct rt5514_dsp *rt5514_dsp; 273 int ret; 274 275 rt5514_dsp = devm_kzalloc(platform->dev, sizeof(*rt5514_dsp), 276 GFP_KERNEL); 277 278 rt5514_dsp->dev = &rt5514_spi->dev; 279 mutex_init(&rt5514_dsp->dma_lock); 280 INIT_DELAYED_WORK(&rt5514_dsp->copy_work, rt5514_spi_copy_work); 281 snd_soc_platform_set_drvdata(platform, rt5514_dsp); 282 283 if (rt5514_spi->irq) { 284 ret = devm_request_threaded_irq(&rt5514_spi->dev, 285 rt5514_spi->irq, NULL, rt5514_spi_irq, 286 IRQF_TRIGGER_RISING | IRQF_ONESHOT, "rt5514-spi", 287 rt5514_dsp); 288 if (ret) 289 dev_err(&rt5514_spi->dev, 290 "%s Failed to reguest IRQ: %d\n", __func__, 291 ret); 292 } 293 294 return 0; 295 } 296 297 static const struct snd_soc_platform_driver rt5514_spi_platform = { 298 .probe = rt5514_spi_pcm_probe, 299 .ops = &rt5514_spi_pcm_ops, 300 }; 301 302 static const struct snd_soc_component_driver rt5514_spi_dai_component = { 303 .name = "rt5514-spi-dai", 304 }; 305 306 /** 307 * rt5514_spi_burst_read - Read data from SPI by rt5514 address. 308 * @addr: Start address. 309 * @rxbuf: Data Buffer for reading. 310 * @len: Data length, it must be a multiple of 8. 311 * 312 * 313 * Returns true for success. 314 */ 315 int rt5514_spi_burst_read(unsigned int addr, u8 *rxbuf, size_t len) 316 { 317 u8 spi_cmd = RT5514_SPI_CMD_BURST_READ; 318 int status; 319 u8 write_buf[8]; 320 unsigned int i, end, offset = 0; 321 322 struct spi_message message; 323 struct spi_transfer x[3]; 324 325 while (offset < len) { 326 if (offset + RT5514_SPI_BUF_LEN <= len) 327 end = RT5514_SPI_BUF_LEN; 328 else 329 end = len % RT5514_SPI_BUF_LEN; 330 331 write_buf[0] = spi_cmd; 332 write_buf[1] = ((addr + offset) & 0xff000000) >> 24; 333 write_buf[2] = ((addr + offset) & 0x00ff0000) >> 16; 334 write_buf[3] = ((addr + offset) & 0x0000ff00) >> 8; 335 write_buf[4] = ((addr + offset) & 0x000000ff) >> 0; 336 337 spi_message_init(&message); 338 memset(x, 0, sizeof(x)); 339 340 x[0].len = 5; 341 x[0].tx_buf = write_buf; 342 spi_message_add_tail(&x[0], &message); 343 344 x[1].len = 4; 345 x[1].tx_buf = write_buf; 346 spi_message_add_tail(&x[1], &message); 347 348 x[2].len = end; 349 x[2].rx_buf = rxbuf + offset; 350 spi_message_add_tail(&x[2], &message); 351 352 status = spi_sync(rt5514_spi, &message); 353 354 if (status) 355 return false; 356 357 offset += RT5514_SPI_BUF_LEN; 358 } 359 360 for (i = 0; i < len; i += 8) { 361 write_buf[0] = rxbuf[i + 0]; 362 write_buf[1] = rxbuf[i + 1]; 363 write_buf[2] = rxbuf[i + 2]; 364 write_buf[3] = rxbuf[i + 3]; 365 write_buf[4] = rxbuf[i + 4]; 366 write_buf[5] = rxbuf[i + 5]; 367 write_buf[6] = rxbuf[i + 6]; 368 write_buf[7] = rxbuf[i + 7]; 369 370 rxbuf[i + 0] = write_buf[7]; 371 rxbuf[i + 1] = write_buf[6]; 372 rxbuf[i + 2] = write_buf[5]; 373 rxbuf[i + 3] = write_buf[4]; 374 rxbuf[i + 4] = write_buf[3]; 375 rxbuf[i + 5] = write_buf[2]; 376 rxbuf[i + 6] = write_buf[1]; 377 rxbuf[i + 7] = write_buf[0]; 378 } 379 380 return true; 381 } 382 383 /** 384 * rt5514_spi_burst_write - Write data to SPI by rt5514 address. 385 * @addr: Start address. 386 * @txbuf: Data Buffer for writng. 387 * @len: Data length, it must be a multiple of 8. 388 * 389 * 390 * Returns true for success. 391 */ 392 int rt5514_spi_burst_write(u32 addr, const u8 *txbuf, size_t len) 393 { 394 u8 spi_cmd = RT5514_SPI_CMD_BURST_WRITE; 395 u8 *write_buf; 396 unsigned int i, end, offset = 0; 397 398 write_buf = kmalloc(RT5514_SPI_BUF_LEN + 6, GFP_KERNEL); 399 400 if (write_buf == NULL) 401 return -ENOMEM; 402 403 while (offset < len) { 404 if (offset + RT5514_SPI_BUF_LEN <= len) 405 end = RT5514_SPI_BUF_LEN; 406 else 407 end = len % RT5514_SPI_BUF_LEN; 408 409 write_buf[0] = spi_cmd; 410 write_buf[1] = ((addr + offset) & 0xff000000) >> 24; 411 write_buf[2] = ((addr + offset) & 0x00ff0000) >> 16; 412 write_buf[3] = ((addr + offset) & 0x0000ff00) >> 8; 413 write_buf[4] = ((addr + offset) & 0x000000ff) >> 0; 414 415 for (i = 0; i < end; i += 8) { 416 write_buf[i + 12] = txbuf[offset + i + 0]; 417 write_buf[i + 11] = txbuf[offset + i + 1]; 418 write_buf[i + 10] = txbuf[offset + i + 2]; 419 write_buf[i + 9] = txbuf[offset + i + 3]; 420 write_buf[i + 8] = txbuf[offset + i + 4]; 421 write_buf[i + 7] = txbuf[offset + i + 5]; 422 write_buf[i + 6] = txbuf[offset + i + 6]; 423 write_buf[i + 5] = txbuf[offset + i + 7]; 424 } 425 426 write_buf[end + 5] = spi_cmd; 427 428 spi_write(rt5514_spi, write_buf, end + 6); 429 430 offset += RT5514_SPI_BUF_LEN; 431 } 432 433 kfree(write_buf); 434 435 return 0; 436 } 437 EXPORT_SYMBOL_GPL(rt5514_spi_burst_write); 438 439 static int rt5514_spi_probe(struct spi_device *spi) 440 { 441 int ret; 442 443 rt5514_spi = spi; 444 445 ret = devm_snd_soc_register_platform(&spi->dev, &rt5514_spi_platform); 446 if (ret < 0) { 447 dev_err(&spi->dev, "Failed to register platform.\n"); 448 return ret; 449 } 450 451 ret = devm_snd_soc_register_component(&spi->dev, 452 &rt5514_spi_dai_component, 453 &rt5514_spi_dai, 1); 454 if (ret < 0) { 455 dev_err(&spi->dev, "Failed to register component.\n"); 456 return ret; 457 } 458 459 device_init_wakeup(&spi->dev, true); 460 461 return 0; 462 } 463 464 static int __maybe_unused rt5514_suspend(struct device *dev) 465 { 466 int irq = to_spi_device(dev)->irq; 467 468 if (device_may_wakeup(dev)) 469 enable_irq_wake(irq); 470 471 return 0; 472 } 473 474 static int __maybe_unused rt5514_resume(struct device *dev) 475 { 476 struct snd_soc_platform *platform = snd_soc_lookup_platform(dev); 477 struct rt5514_dsp *rt5514_dsp = 478 snd_soc_platform_get_drvdata(platform); 479 int irq = to_spi_device(dev)->irq; 480 u8 buf[8]; 481 482 if (device_may_wakeup(dev)) 483 disable_irq_wake(irq); 484 485 if (rt5514_dsp->substream) { 486 rt5514_spi_burst_read(RT5514_IRQ_CTRL, (u8 *)&buf, sizeof(buf)); 487 if (buf[0] & RT5514_IRQ_STATUS_BIT) 488 rt5514_schedule_copy(rt5514_dsp); 489 } 490 491 return 0; 492 } 493 494 static const struct dev_pm_ops rt5514_pm_ops = { 495 SET_SYSTEM_SLEEP_PM_OPS(rt5514_suspend, rt5514_resume) 496 }; 497 498 static const struct of_device_id rt5514_of_match[] = { 499 { .compatible = "realtek,rt5514", }, 500 {}, 501 }; 502 MODULE_DEVICE_TABLE(of, rt5514_of_match); 503 504 static struct spi_driver rt5514_spi_driver = { 505 .driver = { 506 .name = "rt5514", 507 .pm = &rt5514_pm_ops, 508 .of_match_table = of_match_ptr(rt5514_of_match), 509 }, 510 .probe = rt5514_spi_probe, 511 }; 512 module_spi_driver(rt5514_spi_driver); 513 514 MODULE_DESCRIPTION("RT5514 SPI driver"); 515 MODULE_AUTHOR("Oder Chiou <oder_chiou@realtek.com>"); 516 MODULE_LICENSE("GPL v2"); 517