Jianqun Xu | 4495c89 | 2014-07-05 19:13:03 +0800 | [diff] [blame] | 1 | /* sound/soc/rockchip/rockchip_i2s.c |
| 2 | * |
| 3 | * ALSA SoC Audio Layer - Rockchip I2S Controller driver |
| 4 | * |
| 5 | * Copyright (c) 2014 Rockchip Electronics Co. Ltd. |
| 6 | * Author: Jianqun <jay.xu@rock-chips.com> |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License version 2 as |
| 10 | * published by the Free Software Foundation. |
| 11 | */ |
| 12 | |
xujianqun | 1b21572 | 2014-07-11 19:40:05 +0800 | [diff] [blame] | 13 | #include <linux/module.h> |
Jianqun Xu | 4495c89 | 2014-07-05 19:13:03 +0800 | [diff] [blame] | 14 | #include <linux/delay.h> |
| 15 | #include <linux/of_gpio.h> |
| 16 | #include <linux/clk.h> |
| 17 | #include <linux/pm_runtime.h> |
| 18 | #include <linux/regmap.h> |
| 19 | #include <sound/pcm_params.h> |
| 20 | #include <sound/dmaengine_pcm.h> |
| 21 | |
| 22 | #include "rockchip_i2s.h" |
| 23 | |
| 24 | #define DRV_NAME "rockchip-i2s" |
| 25 | |
| 26 | struct rk_i2s_dev { |
| 27 | struct device *dev; |
| 28 | |
| 29 | struct clk *hclk; |
| 30 | struct clk *mclk; |
| 31 | |
| 32 | struct snd_dmaengine_dai_dma_data capture_dma_data; |
| 33 | struct snd_dmaengine_dai_dma_data playback_dma_data; |
| 34 | |
| 35 | struct regmap *regmap; |
| 36 | |
| 37 | /* |
| 38 | * Used to indicate the tx/rx status. |
| 39 | * I2S controller hopes to start the tx and rx together, |
| 40 | * also to stop them when they are both try to stop. |
| 41 | */ |
| 42 | bool tx_start; |
| 43 | bool rx_start; |
Caesar Wang | 2458c37 | 2015-11-06 19:38:14 +0800 | [diff] [blame] | 44 | bool is_master_mode; |
Jianqun Xu | 4495c89 | 2014-07-05 19:13:03 +0800 | [diff] [blame] | 45 | }; |
| 46 | |
| 47 | static int i2s_runtime_suspend(struct device *dev) |
| 48 | { |
| 49 | struct rk_i2s_dev *i2s = dev_get_drvdata(dev); |
| 50 | |
| 51 | clk_disable_unprepare(i2s->mclk); |
| 52 | |
| 53 | return 0; |
| 54 | } |
| 55 | |
| 56 | static int i2s_runtime_resume(struct device *dev) |
| 57 | { |
| 58 | struct rk_i2s_dev *i2s = dev_get_drvdata(dev); |
| 59 | int ret; |
| 60 | |
| 61 | ret = clk_prepare_enable(i2s->mclk); |
| 62 | if (ret) { |
| 63 | dev_err(i2s->dev, "clock enable failed %d\n", ret); |
| 64 | return ret; |
| 65 | } |
| 66 | |
| 67 | return 0; |
| 68 | } |
| 69 | |
| 70 | static inline struct rk_i2s_dev *to_info(struct snd_soc_dai *dai) |
| 71 | { |
| 72 | return snd_soc_dai_get_drvdata(dai); |
| 73 | } |
| 74 | |
| 75 | static void rockchip_snd_txctrl(struct rk_i2s_dev *i2s, int on) |
| 76 | { |
| 77 | unsigned int val = 0; |
| 78 | int retry = 10; |
| 79 | |
| 80 | if (on) { |
| 81 | regmap_update_bits(i2s->regmap, I2S_DMACR, |
| 82 | I2S_DMACR_TDE_ENABLE, I2S_DMACR_TDE_ENABLE); |
| 83 | |
| 84 | regmap_update_bits(i2s->regmap, I2S_XFER, |
| 85 | I2S_XFER_TXS_START | I2S_XFER_RXS_START, |
| 86 | I2S_XFER_TXS_START | I2S_XFER_RXS_START); |
| 87 | |
| 88 | i2s->tx_start = true; |
| 89 | } else { |
| 90 | i2s->tx_start = false; |
| 91 | |
| 92 | regmap_update_bits(i2s->regmap, I2S_DMACR, |
xujianqun | 4c5258a | 2014-07-12 09:02:13 +0800 | [diff] [blame] | 93 | I2S_DMACR_TDE_ENABLE, I2S_DMACR_TDE_DISABLE); |
Jianqun Xu | 4495c89 | 2014-07-05 19:13:03 +0800 | [diff] [blame] | 94 | |
| 95 | if (!i2s->rx_start) { |
| 96 | regmap_update_bits(i2s->regmap, I2S_XFER, |
| 97 | I2S_XFER_TXS_START | |
| 98 | I2S_XFER_RXS_START, |
| 99 | I2S_XFER_TXS_STOP | |
| 100 | I2S_XFER_RXS_STOP); |
| 101 | |
| 102 | regmap_update_bits(i2s->regmap, I2S_CLR, |
xujianqun | 4c5258a | 2014-07-12 09:02:13 +0800 | [diff] [blame] | 103 | I2S_CLR_TXC | I2S_CLR_RXC, |
| 104 | I2S_CLR_TXC | I2S_CLR_RXC); |
Jianqun Xu | 4495c89 | 2014-07-05 19:13:03 +0800 | [diff] [blame] | 105 | |
| 106 | regmap_read(i2s->regmap, I2S_CLR, &val); |
| 107 | |
| 108 | /* Should wait for clear operation to finish */ |
| 109 | while (val) { |
| 110 | regmap_read(i2s->regmap, I2S_CLR, &val); |
| 111 | retry--; |
Sonny Rao | 528a82b4 | 2014-10-08 00:58:51 -0700 | [diff] [blame] | 112 | if (!retry) { |
Jianqun Xu | 4495c89 | 2014-07-05 19:13:03 +0800 | [diff] [blame] | 113 | dev_warn(i2s->dev, "fail to clear\n"); |
Sonny Rao | 528a82b4 | 2014-10-08 00:58:51 -0700 | [diff] [blame] | 114 | break; |
| 115 | } |
Jianqun Xu | 4495c89 | 2014-07-05 19:13:03 +0800 | [diff] [blame] | 116 | } |
| 117 | } |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | static void rockchip_snd_rxctrl(struct rk_i2s_dev *i2s, int on) |
| 122 | { |
| 123 | unsigned int val = 0; |
| 124 | int retry = 10; |
| 125 | |
| 126 | if (on) { |
| 127 | regmap_update_bits(i2s->regmap, I2S_DMACR, |
| 128 | I2S_DMACR_RDE_ENABLE, I2S_DMACR_RDE_ENABLE); |
| 129 | |
| 130 | regmap_update_bits(i2s->regmap, I2S_XFER, |
| 131 | I2S_XFER_TXS_START | I2S_XFER_RXS_START, |
| 132 | I2S_XFER_TXS_START | I2S_XFER_RXS_START); |
| 133 | |
| 134 | i2s->rx_start = true; |
| 135 | } else { |
| 136 | i2s->rx_start = false; |
| 137 | |
| 138 | regmap_update_bits(i2s->regmap, I2S_DMACR, |
| 139 | I2S_DMACR_RDE_ENABLE, I2S_DMACR_RDE_DISABLE); |
| 140 | |
| 141 | if (!i2s->tx_start) { |
| 142 | regmap_update_bits(i2s->regmap, I2S_XFER, |
| 143 | I2S_XFER_TXS_START | |
| 144 | I2S_XFER_RXS_START, |
| 145 | I2S_XFER_TXS_STOP | |
| 146 | I2S_XFER_RXS_STOP); |
| 147 | |
| 148 | regmap_update_bits(i2s->regmap, I2S_CLR, |
xujianqun | 4c5258a | 2014-07-12 09:02:13 +0800 | [diff] [blame] | 149 | I2S_CLR_TXC | I2S_CLR_RXC, |
| 150 | I2S_CLR_TXC | I2S_CLR_RXC); |
Jianqun Xu | 4495c89 | 2014-07-05 19:13:03 +0800 | [diff] [blame] | 151 | |
| 152 | regmap_read(i2s->regmap, I2S_CLR, &val); |
| 153 | |
| 154 | /* Should wait for clear operation to finish */ |
| 155 | while (val) { |
| 156 | regmap_read(i2s->regmap, I2S_CLR, &val); |
| 157 | retry--; |
Jianqun | 29f95bd | 2014-10-29 17:45:51 +0800 | [diff] [blame] | 158 | if (!retry) { |
Jianqun Xu | 4495c89 | 2014-07-05 19:13:03 +0800 | [diff] [blame] | 159 | dev_warn(i2s->dev, "fail to clear\n"); |
Jianqun | 29f95bd | 2014-10-29 17:45:51 +0800 | [diff] [blame] | 160 | break; |
| 161 | } |
Jianqun Xu | 4495c89 | 2014-07-05 19:13:03 +0800 | [diff] [blame] | 162 | } |
| 163 | } |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | static int rockchip_i2s_set_fmt(struct snd_soc_dai *cpu_dai, |
| 168 | unsigned int fmt) |
| 169 | { |
| 170 | struct rk_i2s_dev *i2s = to_info(cpu_dai); |
| 171 | unsigned int mask = 0, val = 0; |
| 172 | |
Jianqun | 07833d8 | 2014-09-13 08:41:03 +0800 | [diff] [blame] | 173 | mask = I2S_CKR_MSS_MASK; |
Jianqun Xu | 4495c89 | 2014-07-05 19:13:03 +0800 | [diff] [blame] | 174 | switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) { |
| 175 | case SND_SOC_DAIFMT_CBS_CFS: |
Jianqun | 07833d8 | 2014-09-13 08:41:03 +0800 | [diff] [blame] | 176 | /* Set source clock in Master mode */ |
| 177 | val = I2S_CKR_MSS_MASTER; |
Caesar Wang | 2458c37 | 2015-11-06 19:38:14 +0800 | [diff] [blame] | 178 | i2s->is_master_mode = true; |
Jianqun Xu | 4495c89 | 2014-07-05 19:13:03 +0800 | [diff] [blame] | 179 | break; |
| 180 | case SND_SOC_DAIFMT_CBM_CFM: |
Jianqun | 07833d8 | 2014-09-13 08:41:03 +0800 | [diff] [blame] | 181 | val = I2S_CKR_MSS_SLAVE; |
Caesar Wang | 2458c37 | 2015-11-06 19:38:14 +0800 | [diff] [blame] | 182 | i2s->is_master_mode = false; |
Jianqun Xu | 4495c89 | 2014-07-05 19:13:03 +0800 | [diff] [blame] | 183 | break; |
| 184 | default: |
| 185 | return -EINVAL; |
| 186 | } |
| 187 | |
| 188 | regmap_update_bits(i2s->regmap, I2S_CKR, mask, val); |
| 189 | |
| 190 | mask = I2S_TXCR_IBM_MASK; |
| 191 | switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) { |
| 192 | case SND_SOC_DAIFMT_RIGHT_J: |
| 193 | val = I2S_TXCR_IBM_RSJM; |
| 194 | break; |
| 195 | case SND_SOC_DAIFMT_LEFT_J: |
| 196 | val = I2S_TXCR_IBM_LSJM; |
| 197 | break; |
| 198 | case SND_SOC_DAIFMT_I2S: |
| 199 | val = I2S_TXCR_IBM_NORMAL; |
| 200 | break; |
| 201 | default: |
| 202 | return -EINVAL; |
| 203 | } |
| 204 | |
| 205 | regmap_update_bits(i2s->regmap, I2S_TXCR, mask, val); |
| 206 | |
| 207 | mask = I2S_RXCR_IBM_MASK; |
| 208 | switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) { |
| 209 | case SND_SOC_DAIFMT_RIGHT_J: |
| 210 | val = I2S_RXCR_IBM_RSJM; |
| 211 | break; |
| 212 | case SND_SOC_DAIFMT_LEFT_J: |
| 213 | val = I2S_RXCR_IBM_LSJM; |
| 214 | break; |
| 215 | case SND_SOC_DAIFMT_I2S: |
| 216 | val = I2S_RXCR_IBM_NORMAL; |
| 217 | break; |
| 218 | default: |
| 219 | return -EINVAL; |
| 220 | } |
| 221 | |
| 222 | regmap_update_bits(i2s->regmap, I2S_RXCR, mask, val); |
| 223 | |
| 224 | return 0; |
| 225 | } |
| 226 | |
| 227 | static int rockchip_i2s_hw_params(struct snd_pcm_substream *substream, |
| 228 | struct snd_pcm_hw_params *params, |
| 229 | struct snd_soc_dai *dai) |
| 230 | { |
| 231 | struct rk_i2s_dev *i2s = to_info(dai); |
Sugar Zhang | b3f2dcd | 2015-10-08 20:40:09 +0800 | [diff] [blame] | 232 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
Jianqun Xu | 4495c89 | 2014-07-05 19:13:03 +0800 | [diff] [blame] | 233 | unsigned int val = 0; |
Caesar Wang | 2458c37 | 2015-11-06 19:38:14 +0800 | [diff] [blame] | 234 | unsigned int mclk_rate, bclk_rate, div_bclk, div_lrck; |
| 235 | |
| 236 | if (i2s->is_master_mode) { |
| 237 | mclk_rate = clk_get_rate(i2s->mclk); |
| 238 | bclk_rate = 2 * 32 * params_rate(params); |
| 239 | if (bclk_rate && mclk_rate % bclk_rate) |
| 240 | return -EINVAL; |
| 241 | |
| 242 | div_bclk = mclk_rate / bclk_rate; |
| 243 | div_lrck = bclk_rate / params_rate(params); |
| 244 | regmap_update_bits(i2s->regmap, I2S_CKR, |
| 245 | I2S_CKR_MDIV_MASK, |
| 246 | I2S_CKR_MDIV(div_bclk)); |
| 247 | |
| 248 | regmap_update_bits(i2s->regmap, I2S_CKR, |
| 249 | I2S_CKR_TSD_MASK | |
| 250 | I2S_CKR_RSD_MASK, |
| 251 | I2S_CKR_TSD(div_lrck) | |
| 252 | I2S_CKR_RSD(div_lrck)); |
| 253 | } |
Jianqun Xu | 4495c89 | 2014-07-05 19:13:03 +0800 | [diff] [blame] | 254 | |
| 255 | switch (params_format(params)) { |
| 256 | case SNDRV_PCM_FORMAT_S8: |
| 257 | val |= I2S_TXCR_VDW(8); |
| 258 | break; |
| 259 | case SNDRV_PCM_FORMAT_S16_LE: |
| 260 | val |= I2S_TXCR_VDW(16); |
| 261 | break; |
| 262 | case SNDRV_PCM_FORMAT_S20_3LE: |
| 263 | val |= I2S_TXCR_VDW(20); |
| 264 | break; |
| 265 | case SNDRV_PCM_FORMAT_S24_LE: |
| 266 | val |= I2S_TXCR_VDW(24); |
| 267 | break; |
| 268 | default: |
| 269 | return -EINVAL; |
| 270 | } |
| 271 | |
Sugar Zhang | 4c9c018 | 2015-10-08 20:40:07 +0800 | [diff] [blame] | 272 | switch (params_channels(params)) { |
| 273 | case 8: |
| 274 | val |= I2S_CHN_8; |
| 275 | break; |
| 276 | case 6: |
| 277 | val |= I2S_CHN_6; |
| 278 | break; |
| 279 | case 4: |
| 280 | val |= I2S_CHN_4; |
| 281 | break; |
| 282 | case 2: |
| 283 | val |= I2S_CHN_2; |
| 284 | break; |
| 285 | default: |
| 286 | dev_err(i2s->dev, "invalid channel: %d\n", |
| 287 | params_channels(params)); |
| 288 | return -EINVAL; |
| 289 | } |
| 290 | |
| 291 | if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) |
| 292 | regmap_update_bits(i2s->regmap, I2S_RXCR, |
| 293 | I2S_RXCR_VDW_MASK | I2S_RXCR_CSR_MASK, |
| 294 | val); |
| 295 | else |
| 296 | regmap_update_bits(i2s->regmap, I2S_TXCR, |
| 297 | I2S_TXCR_VDW_MASK | I2S_TXCR_CSR_MASK, |
| 298 | val); |
| 299 | |
Jianqun Xu | bba1431 | 2014-12-24 17:37:01 +0800 | [diff] [blame] | 300 | regmap_update_bits(i2s->regmap, I2S_DMACR, I2S_DMACR_TDL_MASK, |
| 301 | I2S_DMACR_TDL(16)); |
| 302 | regmap_update_bits(i2s->regmap, I2S_DMACR, I2S_DMACR_RDL_MASK, |
| 303 | I2S_DMACR_RDL(16)); |
Jianqun Xu | 4495c89 | 2014-07-05 19:13:03 +0800 | [diff] [blame] | 304 | |
Sugar Zhang | b3f2dcd | 2015-10-08 20:40:09 +0800 | [diff] [blame] | 305 | val = I2S_CKR_TRCM_TXRX; |
| 306 | if (dai->driver->symmetric_rates || rtd->dai_link->symmetric_rates) |
| 307 | val = I2S_CKR_TRCM_TXSHARE; |
| 308 | |
| 309 | regmap_update_bits(i2s->regmap, I2S_CKR, |
| 310 | I2S_CKR_TRCM_MASK, |
| 311 | val); |
Jianqun Xu | 4495c89 | 2014-07-05 19:13:03 +0800 | [diff] [blame] | 312 | return 0; |
| 313 | } |
| 314 | |
| 315 | static int rockchip_i2s_trigger(struct snd_pcm_substream *substream, |
| 316 | int cmd, struct snd_soc_dai *dai) |
| 317 | { |
| 318 | struct rk_i2s_dev *i2s = to_info(dai); |
| 319 | int ret = 0; |
| 320 | |
| 321 | switch (cmd) { |
| 322 | case SNDRV_PCM_TRIGGER_START: |
| 323 | case SNDRV_PCM_TRIGGER_RESUME: |
| 324 | case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: |
| 325 | if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) |
| 326 | rockchip_snd_rxctrl(i2s, 1); |
| 327 | else |
| 328 | rockchip_snd_txctrl(i2s, 1); |
| 329 | break; |
| 330 | case SNDRV_PCM_TRIGGER_SUSPEND: |
| 331 | case SNDRV_PCM_TRIGGER_STOP: |
| 332 | case SNDRV_PCM_TRIGGER_PAUSE_PUSH: |
| 333 | if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) |
| 334 | rockchip_snd_rxctrl(i2s, 0); |
| 335 | else |
| 336 | rockchip_snd_txctrl(i2s, 0); |
| 337 | break; |
| 338 | default: |
| 339 | ret = -EINVAL; |
| 340 | break; |
| 341 | } |
| 342 | |
| 343 | return ret; |
| 344 | } |
| 345 | |
| 346 | static int rockchip_i2s_set_sysclk(struct snd_soc_dai *cpu_dai, int clk_id, |
| 347 | unsigned int freq, int dir) |
| 348 | { |
| 349 | struct rk_i2s_dev *i2s = to_info(cpu_dai); |
| 350 | int ret; |
| 351 | |
| 352 | ret = clk_set_rate(i2s->mclk, freq); |
| 353 | if (ret) |
| 354 | dev_err(i2s->dev, "Fail to set mclk %d\n", ret); |
| 355 | |
| 356 | return ret; |
| 357 | } |
| 358 | |
Jianqun | 3b40a80 | 2014-09-13 08:41:38 +0800 | [diff] [blame] | 359 | static int rockchip_i2s_dai_probe(struct snd_soc_dai *dai) |
| 360 | { |
| 361 | struct rk_i2s_dev *i2s = snd_soc_dai_get_drvdata(dai); |
| 362 | |
| 363 | dai->capture_dma_data = &i2s->capture_dma_data; |
| 364 | dai->playback_dma_data = &i2s->playback_dma_data; |
| 365 | |
| 366 | return 0; |
| 367 | } |
| 368 | |
Jianqun Xu | 4495c89 | 2014-07-05 19:13:03 +0800 | [diff] [blame] | 369 | static const struct snd_soc_dai_ops rockchip_i2s_dai_ops = { |
| 370 | .hw_params = rockchip_i2s_hw_params, |
| 371 | .set_sysclk = rockchip_i2s_set_sysclk, |
| 372 | .set_fmt = rockchip_i2s_set_fmt, |
| 373 | .trigger = rockchip_i2s_trigger, |
| 374 | }; |
| 375 | |
| 376 | static struct snd_soc_dai_driver rockchip_i2s_dai = { |
Jianqun | 3b40a80 | 2014-09-13 08:41:38 +0800 | [diff] [blame] | 377 | .probe = rockchip_i2s_dai_probe, |
Jianqun Xu | 4495c89 | 2014-07-05 19:13:03 +0800 | [diff] [blame] | 378 | .playback = { |
Jianqun | 3b40a80 | 2014-09-13 08:41:38 +0800 | [diff] [blame] | 379 | .stream_name = "Playback", |
Jianqun Xu | 4495c89 | 2014-07-05 19:13:03 +0800 | [diff] [blame] | 380 | .channels_min = 2, |
| 381 | .channels_max = 8, |
| 382 | .rates = SNDRV_PCM_RATE_8000_192000, |
| 383 | .formats = (SNDRV_PCM_FMTBIT_S8 | |
| 384 | SNDRV_PCM_FMTBIT_S16_LE | |
| 385 | SNDRV_PCM_FMTBIT_S20_3LE | |
| 386 | SNDRV_PCM_FMTBIT_S24_LE), |
| 387 | }, |
| 388 | .capture = { |
Jianqun | 3b40a80 | 2014-09-13 08:41:38 +0800 | [diff] [blame] | 389 | .stream_name = "Capture", |
Jianqun Xu | 4495c89 | 2014-07-05 19:13:03 +0800 | [diff] [blame] | 390 | .channels_min = 2, |
| 391 | .channels_max = 2, |
| 392 | .rates = SNDRV_PCM_RATE_8000_192000, |
| 393 | .formats = (SNDRV_PCM_FMTBIT_S8 | |
| 394 | SNDRV_PCM_FMTBIT_S16_LE | |
| 395 | SNDRV_PCM_FMTBIT_S20_3LE | |
| 396 | SNDRV_PCM_FMTBIT_S24_LE), |
| 397 | }, |
| 398 | .ops = &rockchip_i2s_dai_ops, |
Jianqun Xu | a12d159 | 2015-01-08 10:49:59 +0800 | [diff] [blame] | 399 | .symmetric_rates = 1, |
Jianqun Xu | 4495c89 | 2014-07-05 19:13:03 +0800 | [diff] [blame] | 400 | }; |
| 401 | |
| 402 | static const struct snd_soc_component_driver rockchip_i2s_component = { |
| 403 | .name = DRV_NAME, |
| 404 | }; |
| 405 | |
| 406 | static bool rockchip_i2s_wr_reg(struct device *dev, unsigned int reg) |
| 407 | { |
| 408 | switch (reg) { |
| 409 | case I2S_TXCR: |
| 410 | case I2S_RXCR: |
| 411 | case I2S_CKR: |
| 412 | case I2S_DMACR: |
| 413 | case I2S_INTCR: |
| 414 | case I2S_XFER: |
| 415 | case I2S_CLR: |
| 416 | case I2S_TXDR: |
| 417 | return true; |
| 418 | default: |
| 419 | return false; |
| 420 | } |
| 421 | } |
| 422 | |
| 423 | static bool rockchip_i2s_rd_reg(struct device *dev, unsigned int reg) |
| 424 | { |
| 425 | switch (reg) { |
| 426 | case I2S_TXCR: |
| 427 | case I2S_RXCR: |
| 428 | case I2S_CKR: |
| 429 | case I2S_DMACR: |
| 430 | case I2S_INTCR: |
| 431 | case I2S_XFER: |
| 432 | case I2S_CLR: |
| 433 | case I2S_RXDR: |
Jianqun | 2f1e93f | 2014-09-13 08:42:12 +0800 | [diff] [blame] | 434 | case I2S_FIFOLR: |
| 435 | case I2S_INTSR: |
Jianqun Xu | 4495c89 | 2014-07-05 19:13:03 +0800 | [diff] [blame] | 436 | return true; |
| 437 | default: |
| 438 | return false; |
| 439 | } |
| 440 | } |
| 441 | |
| 442 | static bool rockchip_i2s_volatile_reg(struct device *dev, unsigned int reg) |
| 443 | { |
| 444 | switch (reg) { |
Jianqun Xu | 4495c89 | 2014-07-05 19:13:03 +0800 | [diff] [blame] | 445 | case I2S_INTSR: |
Jianqun | 2f1e93f | 2014-09-13 08:42:12 +0800 | [diff] [blame] | 446 | case I2S_CLR: |
Jianqun Xu | 4495c89 | 2014-07-05 19:13:03 +0800 | [diff] [blame] | 447 | return true; |
| 448 | default: |
| 449 | return false; |
| 450 | } |
| 451 | } |
| 452 | |
| 453 | static bool rockchip_i2s_precious_reg(struct device *dev, unsigned int reg) |
| 454 | { |
| 455 | switch (reg) { |
Jianqun Xu | 4495c89 | 2014-07-05 19:13:03 +0800 | [diff] [blame] | 456 | default: |
| 457 | return false; |
| 458 | } |
| 459 | } |
| 460 | |
| 461 | static const struct regmap_config rockchip_i2s_regmap_config = { |
| 462 | .reg_bits = 32, |
| 463 | .reg_stride = 4, |
| 464 | .val_bits = 32, |
| 465 | .max_register = I2S_RXDR, |
| 466 | .writeable_reg = rockchip_i2s_wr_reg, |
| 467 | .readable_reg = rockchip_i2s_rd_reg, |
| 468 | .volatile_reg = rockchip_i2s_volatile_reg, |
| 469 | .precious_reg = rockchip_i2s_precious_reg, |
| 470 | .cache_type = REGCACHE_FLAT, |
| 471 | }; |
| 472 | |
| 473 | static int rockchip_i2s_probe(struct platform_device *pdev) |
| 474 | { |
Sugar Zhang | 4c9c018 | 2015-10-08 20:40:07 +0800 | [diff] [blame] | 475 | struct device_node *node = pdev->dev.of_node; |
Jianqun Xu | 4495c89 | 2014-07-05 19:13:03 +0800 | [diff] [blame] | 476 | struct rk_i2s_dev *i2s; |
Sugar Zhang | c4f9374 | 2015-11-10 15:32:07 +0800 | [diff] [blame^] | 477 | struct snd_soc_dai_driver *soc_dai; |
Jianqun Xu | 4495c89 | 2014-07-05 19:13:03 +0800 | [diff] [blame] | 478 | struct resource *res; |
| 479 | void __iomem *regs; |
| 480 | int ret; |
Sugar Zhang | 4c9c018 | 2015-10-08 20:40:07 +0800 | [diff] [blame] | 481 | int val; |
Jianqun Xu | 4495c89 | 2014-07-05 19:13:03 +0800 | [diff] [blame] | 482 | |
| 483 | i2s = devm_kzalloc(&pdev->dev, sizeof(*i2s), GFP_KERNEL); |
| 484 | if (!i2s) { |
| 485 | dev_err(&pdev->dev, "Can't allocate rk_i2s_dev\n"); |
| 486 | return -ENOMEM; |
| 487 | } |
| 488 | |
| 489 | /* try to prepare related clocks */ |
| 490 | i2s->hclk = devm_clk_get(&pdev->dev, "i2s_hclk"); |
| 491 | if (IS_ERR(i2s->hclk)) { |
| 492 | dev_err(&pdev->dev, "Can't retrieve i2s bus clock\n"); |
| 493 | return PTR_ERR(i2s->hclk); |
| 494 | } |
Jianqun | 01605ad | 2014-09-13 08:43:13 +0800 | [diff] [blame] | 495 | ret = clk_prepare_enable(i2s->hclk); |
| 496 | if (ret) { |
| 497 | dev_err(i2s->dev, "hclock enable failed %d\n", ret); |
| 498 | return ret; |
| 499 | } |
Jianqun Xu | 4495c89 | 2014-07-05 19:13:03 +0800 | [diff] [blame] | 500 | |
| 501 | i2s->mclk = devm_clk_get(&pdev->dev, "i2s_clk"); |
| 502 | if (IS_ERR(i2s->mclk)) { |
| 503 | dev_err(&pdev->dev, "Can't retrieve i2s master clock\n"); |
| 504 | return PTR_ERR(i2s->mclk); |
| 505 | } |
| 506 | |
| 507 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 508 | regs = devm_ioremap_resource(&pdev->dev, res); |
Wei Yongjun | 55b2194 | 2014-07-28 21:21:00 +0800 | [diff] [blame] | 509 | if (IS_ERR(regs)) |
Jianqun Xu | 4495c89 | 2014-07-05 19:13:03 +0800 | [diff] [blame] | 510 | return PTR_ERR(regs); |
Jianqun Xu | 4495c89 | 2014-07-05 19:13:03 +0800 | [diff] [blame] | 511 | |
| 512 | i2s->regmap = devm_regmap_init_mmio(&pdev->dev, regs, |
| 513 | &rockchip_i2s_regmap_config); |
| 514 | if (IS_ERR(i2s->regmap)) { |
| 515 | dev_err(&pdev->dev, |
| 516 | "Failed to initialise managed register map\n"); |
| 517 | return PTR_ERR(i2s->regmap); |
| 518 | } |
| 519 | |
| 520 | i2s->playback_dma_data.addr = res->start + I2S_TXDR; |
| 521 | i2s->playback_dma_data.addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES; |
Jianqun Xu | 27fd36a | 2014-12-24 17:37:02 +0800 | [diff] [blame] | 522 | i2s->playback_dma_data.maxburst = 4; |
Jianqun Xu | 4495c89 | 2014-07-05 19:13:03 +0800 | [diff] [blame] | 523 | |
| 524 | i2s->capture_dma_data.addr = res->start + I2S_RXDR; |
| 525 | i2s->capture_dma_data.addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES; |
Jianqun Xu | 27fd36a | 2014-12-24 17:37:02 +0800 | [diff] [blame] | 526 | i2s->capture_dma_data.maxburst = 4; |
Jianqun Xu | 4495c89 | 2014-07-05 19:13:03 +0800 | [diff] [blame] | 527 | |
| 528 | i2s->dev = &pdev->dev; |
| 529 | dev_set_drvdata(&pdev->dev, i2s); |
| 530 | |
| 531 | pm_runtime_enable(&pdev->dev); |
| 532 | if (!pm_runtime_enabled(&pdev->dev)) { |
| 533 | ret = i2s_runtime_resume(&pdev->dev); |
| 534 | if (ret) |
| 535 | goto err_pm_disable; |
| 536 | } |
| 537 | |
Sugar Zhang | c4f9374 | 2015-11-10 15:32:07 +0800 | [diff] [blame^] | 538 | soc_dai = devm_kzalloc(&pdev->dev, |
| 539 | sizeof(*soc_dai), GFP_KERNEL); |
| 540 | if (!soc_dai) |
| 541 | return -ENOMEM; |
| 542 | |
| 543 | memcpy(soc_dai, &rockchip_i2s_dai, sizeof(*soc_dai)); |
| 544 | if (!of_property_read_u32(node, "rockchip,playback-channels", &val)) { |
| 545 | if (val >= 2 && val <= 8) |
| 546 | soc_dai->playback.channels_max = val; |
| 547 | } |
| 548 | |
Sugar Zhang | 4c9c018 | 2015-10-08 20:40:07 +0800 | [diff] [blame] | 549 | if (!of_property_read_u32(node, "rockchip,capture-channels", &val)) { |
| 550 | if (val >= 2 && val <= 8) |
Sugar Zhang | c4f9374 | 2015-11-10 15:32:07 +0800 | [diff] [blame^] | 551 | soc_dai->capture.channels_max = val; |
Sugar Zhang | 4c9c018 | 2015-10-08 20:40:07 +0800 | [diff] [blame] | 552 | } |
| 553 | |
Jianqun Xu | 4495c89 | 2014-07-05 19:13:03 +0800 | [diff] [blame] | 554 | ret = devm_snd_soc_register_component(&pdev->dev, |
| 555 | &rockchip_i2s_component, |
Sugar Zhang | c4f9374 | 2015-11-10 15:32:07 +0800 | [diff] [blame^] | 556 | soc_dai, 1); |
| 557 | |
Jianqun Xu | 4495c89 | 2014-07-05 19:13:03 +0800 | [diff] [blame] | 558 | if (ret) { |
| 559 | dev_err(&pdev->dev, "Could not register DAI\n"); |
| 560 | goto err_suspend; |
| 561 | } |
| 562 | |
Vaishali Thakkar | ebb75c0 | 2015-08-15 07:21:00 +0530 | [diff] [blame] | 563 | ret = devm_snd_dmaengine_pcm_register(&pdev->dev, NULL, 0); |
Jianqun Xu | 4495c89 | 2014-07-05 19:13:03 +0800 | [diff] [blame] | 564 | if (ret) { |
| 565 | dev_err(&pdev->dev, "Could not register PCM\n"); |
Vaishali Thakkar | ebb75c0 | 2015-08-15 07:21:00 +0530 | [diff] [blame] | 566 | return ret; |
Jianqun Xu | 4495c89 | 2014-07-05 19:13:03 +0800 | [diff] [blame] | 567 | } |
| 568 | |
| 569 | return 0; |
| 570 | |
Jianqun Xu | 4495c89 | 2014-07-05 19:13:03 +0800 | [diff] [blame] | 571 | err_suspend: |
| 572 | if (!pm_runtime_status_suspended(&pdev->dev)) |
| 573 | i2s_runtime_suspend(&pdev->dev); |
| 574 | err_pm_disable: |
| 575 | pm_runtime_disable(&pdev->dev); |
| 576 | |
| 577 | return ret; |
| 578 | } |
| 579 | |
| 580 | static int rockchip_i2s_remove(struct platform_device *pdev) |
| 581 | { |
| 582 | struct rk_i2s_dev *i2s = dev_get_drvdata(&pdev->dev); |
| 583 | |
| 584 | pm_runtime_disable(&pdev->dev); |
| 585 | if (!pm_runtime_status_suspended(&pdev->dev)) |
| 586 | i2s_runtime_suspend(&pdev->dev); |
| 587 | |
| 588 | clk_disable_unprepare(i2s->mclk); |
| 589 | clk_disable_unprepare(i2s->hclk); |
Jianqun Xu | 4495c89 | 2014-07-05 19:13:03 +0800 | [diff] [blame] | 590 | |
| 591 | return 0; |
| 592 | } |
| 593 | |
| 594 | static const struct of_device_id rockchip_i2s_match[] = { |
| 595 | { .compatible = "rockchip,rk3066-i2s", }, |
| 596 | {}, |
| 597 | }; |
| 598 | |
| 599 | static const struct dev_pm_ops rockchip_i2s_pm_ops = { |
| 600 | SET_RUNTIME_PM_OPS(i2s_runtime_suspend, i2s_runtime_resume, |
| 601 | NULL) |
| 602 | }; |
| 603 | |
| 604 | static struct platform_driver rockchip_i2s_driver = { |
| 605 | .probe = rockchip_i2s_probe, |
| 606 | .remove = rockchip_i2s_remove, |
| 607 | .driver = { |
| 608 | .name = DRV_NAME, |
Jianqun Xu | 4495c89 | 2014-07-05 19:13:03 +0800 | [diff] [blame] | 609 | .of_match_table = of_match_ptr(rockchip_i2s_match), |
| 610 | .pm = &rockchip_i2s_pm_ops, |
| 611 | }, |
| 612 | }; |
| 613 | module_platform_driver(rockchip_i2s_driver); |
| 614 | |
| 615 | MODULE_DESCRIPTION("ROCKCHIP IIS ASoC Interface"); |
| 616 | MODULE_AUTHOR("jianqun <jay.xu@rock-chips.com>"); |
| 617 | MODULE_LICENSE("GPL v2"); |
| 618 | MODULE_ALIAS("platform:" DRV_NAME); |
| 619 | MODULE_DEVICE_TABLE(of, rockchip_i2s_match); |