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