Vinod Koul | 71bb8a1 | 2017-12-14 11:19:43 +0530 | [diff] [blame] | 1 | // SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause) |
| 2 | // Copyright(c) 2015-17 Intel Corporation. |
| 3 | |
| 4 | /* |
| 5 | * Soundwire Intel Master Driver |
| 6 | */ |
| 7 | |
| 8 | #include <linux/acpi.h> |
Pierre-Louis Bossart | 79ee663 | 2019-08-21 13:58:20 -0500 | [diff] [blame] | 9 | #include <linux/debugfs.h> |
Vinod Koul | 71bb8a1 | 2017-12-14 11:19:43 +0530 | [diff] [blame] | 10 | #include <linux/delay.h> |
Paul Gortmaker | 4abbd78 | 2019-04-13 11:12:52 -0400 | [diff] [blame] | 11 | #include <linux/module.h> |
Vinod Koul | 71bb8a1 | 2017-12-14 11:19:43 +0530 | [diff] [blame] | 12 | #include <linux/interrupt.h> |
Pierre-Louis Bossart | df72b71 | 2019-09-16 13:57:38 -0500 | [diff] [blame] | 13 | #include <linux/io.h> |
Vinod Koul | 71bb8a1 | 2017-12-14 11:19:43 +0530 | [diff] [blame] | 14 | #include <linux/platform_device.h> |
Vinod Koul | 37a2d22 | 2018-04-26 18:38:58 +0530 | [diff] [blame] | 15 | #include <sound/pcm_params.h> |
| 16 | #include <sound/soc.h> |
Vinod Koul | 71bb8a1 | 2017-12-14 11:19:43 +0530 | [diff] [blame] | 17 | #include <linux/soundwire/sdw_registers.h> |
| 18 | #include <linux/soundwire/sdw.h> |
| 19 | #include <linux/soundwire/sdw_intel.h> |
| 20 | #include "cadence_master.h" |
Pierre-Louis Bossart | 79ee663 | 2019-08-21 13:58:20 -0500 | [diff] [blame] | 21 | #include "bus.h" |
Vinod Koul | 71bb8a1 | 2017-12-14 11:19:43 +0530 | [diff] [blame] | 22 | #include "intel.h" |
| 23 | |
| 24 | /* Intel SHIM Registers Definition */ |
| 25 | #define SDW_SHIM_LCAP 0x0 |
| 26 | #define SDW_SHIM_LCTL 0x4 |
| 27 | #define SDW_SHIM_IPPTR 0x8 |
| 28 | #define SDW_SHIM_SYNC 0xC |
| 29 | |
Pierre-Louis Bossart | 7cc6e31 | 2019-05-01 10:57:39 -0500 | [diff] [blame] | 30 | #define SDW_SHIM_CTLSCAP(x) (0x010 + 0x60 * (x)) |
| 31 | #define SDW_SHIM_CTLS0CM(x) (0x012 + 0x60 * (x)) |
| 32 | #define SDW_SHIM_CTLS1CM(x) (0x014 + 0x60 * (x)) |
| 33 | #define SDW_SHIM_CTLS2CM(x) (0x016 + 0x60 * (x)) |
| 34 | #define SDW_SHIM_CTLS3CM(x) (0x018 + 0x60 * (x)) |
| 35 | #define SDW_SHIM_PCMSCAP(x) (0x020 + 0x60 * (x)) |
Vinod Koul | 71bb8a1 | 2017-12-14 11:19:43 +0530 | [diff] [blame] | 36 | |
Pierre-Louis Bossart | 7cc6e31 | 2019-05-01 10:57:39 -0500 | [diff] [blame] | 37 | #define SDW_SHIM_PCMSYCHM(x, y) (0x022 + (0x60 * (x)) + (0x2 * (y))) |
| 38 | #define SDW_SHIM_PCMSYCHC(x, y) (0x042 + (0x60 * (x)) + (0x2 * (y))) |
| 39 | #define SDW_SHIM_PDMSCAP(x) (0x062 + 0x60 * (x)) |
| 40 | #define SDW_SHIM_IOCTL(x) (0x06C + 0x60 * (x)) |
| 41 | #define SDW_SHIM_CTMCTL(x) (0x06E + 0x60 * (x)) |
Vinod Koul | 71bb8a1 | 2017-12-14 11:19:43 +0530 | [diff] [blame] | 42 | |
| 43 | #define SDW_SHIM_WAKEEN 0x190 |
| 44 | #define SDW_SHIM_WAKESTS 0x192 |
| 45 | |
| 46 | #define SDW_SHIM_LCTL_SPA BIT(0) |
| 47 | #define SDW_SHIM_LCTL_CPA BIT(8) |
| 48 | |
| 49 | #define SDW_SHIM_SYNC_SYNCPRD_VAL 0x176F |
| 50 | #define SDW_SHIM_SYNC_SYNCPRD GENMASK(14, 0) |
| 51 | #define SDW_SHIM_SYNC_SYNCCPU BIT(15) |
| 52 | #define SDW_SHIM_SYNC_CMDSYNC_MASK GENMASK(19, 16) |
| 53 | #define SDW_SHIM_SYNC_CMDSYNC BIT(16) |
| 54 | #define SDW_SHIM_SYNC_SYNCGO BIT(24) |
| 55 | |
| 56 | #define SDW_SHIM_PCMSCAP_ISS GENMASK(3, 0) |
| 57 | #define SDW_SHIM_PCMSCAP_OSS GENMASK(7, 4) |
| 58 | #define SDW_SHIM_PCMSCAP_BSS GENMASK(12, 8) |
| 59 | |
| 60 | #define SDW_SHIM_PCMSYCM_LCHN GENMASK(3, 0) |
| 61 | #define SDW_SHIM_PCMSYCM_HCHN GENMASK(7, 4) |
| 62 | #define SDW_SHIM_PCMSYCM_STREAM GENMASK(13, 8) |
| 63 | #define SDW_SHIM_PCMSYCM_DIR BIT(15) |
| 64 | |
| 65 | #define SDW_SHIM_PDMSCAP_ISS GENMASK(3, 0) |
| 66 | #define SDW_SHIM_PDMSCAP_OSS GENMASK(7, 4) |
| 67 | #define SDW_SHIM_PDMSCAP_BSS GENMASK(12, 8) |
| 68 | #define SDW_SHIM_PDMSCAP_CPSS GENMASK(15, 13) |
| 69 | |
| 70 | #define SDW_SHIM_IOCTL_MIF BIT(0) |
| 71 | #define SDW_SHIM_IOCTL_CO BIT(1) |
| 72 | #define SDW_SHIM_IOCTL_COE BIT(2) |
| 73 | #define SDW_SHIM_IOCTL_DO BIT(3) |
| 74 | #define SDW_SHIM_IOCTL_DOE BIT(4) |
| 75 | #define SDW_SHIM_IOCTL_BKE BIT(5) |
| 76 | #define SDW_SHIM_IOCTL_WPDD BIT(6) |
| 77 | #define SDW_SHIM_IOCTL_CIBD BIT(8) |
| 78 | #define SDW_SHIM_IOCTL_DIBD BIT(9) |
| 79 | |
| 80 | #define SDW_SHIM_CTMCTL_DACTQE BIT(0) |
| 81 | #define SDW_SHIM_CTMCTL_DODS BIT(1) |
| 82 | #define SDW_SHIM_CTMCTL_DOAIS GENMASK(4, 3) |
| 83 | |
| 84 | #define SDW_SHIM_WAKEEN_ENABLE BIT(0) |
| 85 | #define SDW_SHIM_WAKESTS_STATUS BIT(0) |
| 86 | |
| 87 | /* Intel ALH Register definitions */ |
Pierre-Louis Bossart | 7cc6e31 | 2019-05-01 10:57:39 -0500 | [diff] [blame] | 88 | #define SDW_ALH_STRMZCFG(x) (0x000 + (0x4 * (x))) |
Pierre-Louis Bossart | 79ee663 | 2019-08-21 13:58:20 -0500 | [diff] [blame] | 89 | #define SDW_ALH_NUM_STREAMS 64 |
Vinod Koul | 71bb8a1 | 2017-12-14 11:19:43 +0530 | [diff] [blame] | 90 | |
| 91 | #define SDW_ALH_STRMZCFG_DMAT_VAL 0x3 |
| 92 | #define SDW_ALH_STRMZCFG_DMAT GENMASK(7, 0) |
| 93 | #define SDW_ALH_STRMZCFG_CHN GENMASK(19, 16) |
| 94 | |
Pierre-Louis Bossart | 395713d | 2019-08-21 13:58:21 -0500 | [diff] [blame] | 95 | #define SDW_INTEL_QUIRK_MASK_BUS_DISABLE BIT(1) |
| 96 | |
Vinod Koul | c46302e | 2018-04-26 18:39:05 +0530 | [diff] [blame] | 97 | enum intel_pdi_type { |
| 98 | INTEL_PDI_IN = 0, |
| 99 | INTEL_PDI_OUT = 1, |
| 100 | INTEL_PDI_BD = 2, |
| 101 | }; |
| 102 | |
Vinod Koul | 71bb8a1 | 2017-12-14 11:19:43 +0530 | [diff] [blame] | 103 | struct sdw_intel { |
| 104 | struct sdw_cdns cdns; |
| 105 | int instance; |
| 106 | struct sdw_intel_link_res *res; |
Pierre-Louis Bossart | 79ee663 | 2019-08-21 13:58:20 -0500 | [diff] [blame] | 107 | #ifdef CONFIG_DEBUG_FS |
| 108 | struct dentry *debugfs; |
| 109 | #endif |
Vinod Koul | 71bb8a1 | 2017-12-14 11:19:43 +0530 | [diff] [blame] | 110 | }; |
| 111 | |
| 112 | #define cdns_to_intel(_cdns) container_of(_cdns, struct sdw_intel, cdns) |
| 113 | |
| 114 | /* |
| 115 | * Read, write helpers for HW registers |
| 116 | */ |
| 117 | static inline int intel_readl(void __iomem *base, int offset) |
| 118 | { |
| 119 | return readl(base + offset); |
| 120 | } |
| 121 | |
| 122 | static inline void intel_writel(void __iomem *base, int offset, int value) |
| 123 | { |
| 124 | writel(value, base + offset); |
| 125 | } |
| 126 | |
| 127 | static inline u16 intel_readw(void __iomem *base, int offset) |
| 128 | { |
| 129 | return readw(base + offset); |
| 130 | } |
| 131 | |
| 132 | static inline void intel_writew(void __iomem *base, int offset, u16 value) |
| 133 | { |
| 134 | writew(value, base + offset); |
| 135 | } |
| 136 | |
| 137 | static int intel_clear_bit(void __iomem *base, int offset, u32 value, u32 mask) |
| 138 | { |
| 139 | int timeout = 10; |
| 140 | u32 reg_read; |
| 141 | |
| 142 | writel(value, base + offset); |
| 143 | do { |
| 144 | reg_read = readl(base + offset); |
| 145 | if (!(reg_read & mask)) |
| 146 | return 0; |
| 147 | |
| 148 | timeout--; |
| 149 | udelay(50); |
| 150 | } while (timeout != 0); |
| 151 | |
| 152 | return -EAGAIN; |
| 153 | } |
| 154 | |
| 155 | static int intel_set_bit(void __iomem *base, int offset, u32 value, u32 mask) |
| 156 | { |
| 157 | int timeout = 10; |
| 158 | u32 reg_read; |
| 159 | |
| 160 | writel(value, base + offset); |
| 161 | do { |
| 162 | reg_read = readl(base + offset); |
| 163 | if (reg_read & mask) |
| 164 | return 0; |
| 165 | |
| 166 | timeout--; |
| 167 | udelay(50); |
| 168 | } while (timeout != 0); |
| 169 | |
| 170 | return -EAGAIN; |
| 171 | } |
| 172 | |
| 173 | /* |
Pierre-Louis Bossart | 79ee663 | 2019-08-21 13:58:20 -0500 | [diff] [blame] | 174 | * debugfs |
| 175 | */ |
| 176 | #ifdef CONFIG_DEBUG_FS |
| 177 | |
| 178 | #define RD_BUF (2 * PAGE_SIZE) |
| 179 | |
| 180 | static ssize_t intel_sprintf(void __iomem *mem, bool l, |
| 181 | char *buf, size_t pos, unsigned int reg) |
| 182 | { |
| 183 | int value; |
| 184 | |
| 185 | if (l) |
| 186 | value = intel_readl(mem, reg); |
| 187 | else |
| 188 | value = intel_readw(mem, reg); |
| 189 | |
| 190 | return scnprintf(buf + pos, RD_BUF - pos, "%4x\t%4x\n", reg, value); |
| 191 | } |
| 192 | |
| 193 | static int intel_reg_show(struct seq_file *s_file, void *data) |
| 194 | { |
| 195 | struct sdw_intel *sdw = s_file->private; |
| 196 | void __iomem *s = sdw->res->shim; |
| 197 | void __iomem *a = sdw->res->alh; |
| 198 | char *buf; |
| 199 | ssize_t ret; |
| 200 | int i, j; |
| 201 | unsigned int links, reg; |
| 202 | |
| 203 | buf = kzalloc(RD_BUF, GFP_KERNEL); |
| 204 | if (!buf) |
| 205 | return -ENOMEM; |
| 206 | |
| 207 | links = intel_readl(s, SDW_SHIM_LCAP) & GENMASK(2, 0); |
| 208 | |
| 209 | ret = scnprintf(buf, RD_BUF, "Register Value\n"); |
| 210 | ret += scnprintf(buf + ret, RD_BUF - ret, "\nShim\n"); |
| 211 | |
| 212 | for (i = 0; i < links; i++) { |
| 213 | reg = SDW_SHIM_LCAP + i * 4; |
| 214 | ret += intel_sprintf(s, true, buf, ret, reg); |
| 215 | } |
| 216 | |
| 217 | for (i = 0; i < links; i++) { |
| 218 | ret += scnprintf(buf + ret, RD_BUF - ret, "\nLink%d\n", i); |
| 219 | ret += intel_sprintf(s, false, buf, ret, SDW_SHIM_CTLSCAP(i)); |
| 220 | ret += intel_sprintf(s, false, buf, ret, SDW_SHIM_CTLS0CM(i)); |
| 221 | ret += intel_sprintf(s, false, buf, ret, SDW_SHIM_CTLS1CM(i)); |
| 222 | ret += intel_sprintf(s, false, buf, ret, SDW_SHIM_CTLS2CM(i)); |
| 223 | ret += intel_sprintf(s, false, buf, ret, SDW_SHIM_CTLS3CM(i)); |
| 224 | ret += intel_sprintf(s, false, buf, ret, SDW_SHIM_PCMSCAP(i)); |
| 225 | |
| 226 | ret += scnprintf(buf + ret, RD_BUF - ret, "\n PCMSyCH registers\n"); |
| 227 | |
| 228 | /* |
| 229 | * the value 10 is the number of PDIs. We will need a |
| 230 | * cleanup to remove hard-coded Intel configurations |
| 231 | * from cadence_master.c |
| 232 | */ |
| 233 | for (j = 0; j < 10; j++) { |
| 234 | ret += intel_sprintf(s, false, buf, ret, |
| 235 | SDW_SHIM_PCMSYCHM(i, j)); |
| 236 | ret += intel_sprintf(s, false, buf, ret, |
| 237 | SDW_SHIM_PCMSYCHC(i, j)); |
| 238 | } |
| 239 | ret += scnprintf(buf + ret, RD_BUF - ret, "\n PDMSCAP, IOCTL, CTMCTL\n"); |
| 240 | |
| 241 | ret += intel_sprintf(s, false, buf, ret, SDW_SHIM_PDMSCAP(i)); |
| 242 | ret += intel_sprintf(s, false, buf, ret, SDW_SHIM_IOCTL(i)); |
| 243 | ret += intel_sprintf(s, false, buf, ret, SDW_SHIM_CTMCTL(i)); |
| 244 | } |
| 245 | |
| 246 | ret += scnprintf(buf + ret, RD_BUF - ret, "\nWake registers\n"); |
| 247 | ret += intel_sprintf(s, false, buf, ret, SDW_SHIM_WAKEEN); |
| 248 | ret += intel_sprintf(s, false, buf, ret, SDW_SHIM_WAKESTS); |
| 249 | |
| 250 | ret += scnprintf(buf + ret, RD_BUF - ret, "\nALH STRMzCFG\n"); |
| 251 | for (i = 0; i < SDW_ALH_NUM_STREAMS; i++) |
| 252 | ret += intel_sprintf(a, true, buf, ret, SDW_ALH_STRMZCFG(i)); |
| 253 | |
| 254 | seq_printf(s_file, "%s", buf); |
| 255 | kfree(buf); |
| 256 | |
| 257 | return 0; |
| 258 | } |
| 259 | DEFINE_SHOW_ATTRIBUTE(intel_reg); |
| 260 | |
| 261 | static void intel_debugfs_init(struct sdw_intel *sdw) |
| 262 | { |
| 263 | struct dentry *root = sdw->cdns.bus.debugfs; |
| 264 | |
| 265 | if (!root) |
| 266 | return; |
| 267 | |
| 268 | sdw->debugfs = debugfs_create_dir("intel-sdw", root); |
| 269 | |
| 270 | debugfs_create_file("intel-registers", 0400, sdw->debugfs, sdw, |
| 271 | &intel_reg_fops); |
| 272 | |
| 273 | sdw_cdns_debugfs_init(&sdw->cdns, sdw->debugfs); |
| 274 | } |
| 275 | |
| 276 | static void intel_debugfs_exit(struct sdw_intel *sdw) |
| 277 | { |
| 278 | debugfs_remove_recursive(sdw->debugfs); |
| 279 | } |
| 280 | #else |
| 281 | static void intel_debugfs_init(struct sdw_intel *sdw) {} |
| 282 | static void intel_debugfs_exit(struct sdw_intel *sdw) {} |
| 283 | #endif /* CONFIG_DEBUG_FS */ |
| 284 | |
| 285 | /* |
Vinod Koul | 71bb8a1 | 2017-12-14 11:19:43 +0530 | [diff] [blame] | 286 | * shim ops |
| 287 | */ |
| 288 | |
| 289 | static int intel_link_power_up(struct sdw_intel *sdw) |
| 290 | { |
| 291 | unsigned int link_id = sdw->instance; |
| 292 | void __iomem *shim = sdw->res->shim; |
| 293 | int spa_mask, cpa_mask; |
| 294 | int link_control, ret; |
| 295 | |
| 296 | /* Link power up sequence */ |
| 297 | link_control = intel_readl(shim, SDW_SHIM_LCTL); |
| 298 | spa_mask = (SDW_SHIM_LCTL_SPA << link_id); |
| 299 | cpa_mask = (SDW_SHIM_LCTL_CPA << link_id); |
| 300 | link_control |= spa_mask; |
| 301 | |
| 302 | ret = intel_set_bit(shim, SDW_SHIM_LCTL, link_control, cpa_mask); |
| 303 | if (ret < 0) |
| 304 | return ret; |
| 305 | |
| 306 | sdw->cdns.link_up = true; |
| 307 | return 0; |
| 308 | } |
| 309 | |
| 310 | static int intel_shim_init(struct sdw_intel *sdw) |
| 311 | { |
| 312 | void __iomem *shim = sdw->res->shim; |
| 313 | unsigned int link_id = sdw->instance; |
| 314 | int sync_reg, ret; |
| 315 | u16 ioctl = 0, act = 0; |
| 316 | |
| 317 | /* Initialize Shim */ |
| 318 | ioctl |= SDW_SHIM_IOCTL_BKE; |
| 319 | intel_writew(shim, SDW_SHIM_IOCTL(link_id), ioctl); |
| 320 | |
| 321 | ioctl |= SDW_SHIM_IOCTL_WPDD; |
| 322 | intel_writew(shim, SDW_SHIM_IOCTL(link_id), ioctl); |
| 323 | |
| 324 | ioctl |= SDW_SHIM_IOCTL_DO; |
| 325 | intel_writew(shim, SDW_SHIM_IOCTL(link_id), ioctl); |
| 326 | |
| 327 | ioctl |= SDW_SHIM_IOCTL_DOE; |
| 328 | intel_writew(shim, SDW_SHIM_IOCTL(link_id), ioctl); |
| 329 | |
| 330 | /* Switch to MIP from Glue logic */ |
| 331 | ioctl = intel_readw(shim, SDW_SHIM_IOCTL(link_id)); |
| 332 | |
| 333 | ioctl &= ~(SDW_SHIM_IOCTL_DOE); |
| 334 | intel_writew(shim, SDW_SHIM_IOCTL(link_id), ioctl); |
| 335 | |
| 336 | ioctl &= ~(SDW_SHIM_IOCTL_DO); |
| 337 | intel_writew(shim, SDW_SHIM_IOCTL(link_id), ioctl); |
| 338 | |
| 339 | ioctl |= (SDW_SHIM_IOCTL_MIF); |
| 340 | intel_writew(shim, SDW_SHIM_IOCTL(link_id), ioctl); |
| 341 | |
| 342 | ioctl &= ~(SDW_SHIM_IOCTL_BKE); |
| 343 | ioctl &= ~(SDW_SHIM_IOCTL_COE); |
| 344 | |
| 345 | intel_writew(shim, SDW_SHIM_IOCTL(link_id), ioctl); |
| 346 | |
| 347 | act |= 0x1 << SDW_REG_SHIFT(SDW_SHIM_CTMCTL_DOAIS); |
| 348 | act |= SDW_SHIM_CTMCTL_DACTQE; |
| 349 | act |= SDW_SHIM_CTMCTL_DODS; |
| 350 | intel_writew(shim, SDW_SHIM_CTMCTL(link_id), act); |
| 351 | |
| 352 | /* Now set SyncPRD period */ |
| 353 | sync_reg = intel_readl(shim, SDW_SHIM_SYNC); |
| 354 | sync_reg |= (SDW_SHIM_SYNC_SYNCPRD_VAL << |
| 355 | SDW_REG_SHIFT(SDW_SHIM_SYNC_SYNCPRD)); |
| 356 | |
| 357 | /* Set SyncCPU bit */ |
| 358 | sync_reg |= SDW_SHIM_SYNC_SYNCCPU; |
| 359 | ret = intel_clear_bit(shim, SDW_SHIM_SYNC, sync_reg, |
Pierre-Louis Bossart | d542bc9 | 2019-05-01 10:57:38 -0500 | [diff] [blame] | 360 | SDW_SHIM_SYNC_SYNCCPU); |
Vinod Koul | 71bb8a1 | 2017-12-14 11:19:43 +0530 | [diff] [blame] | 361 | if (ret < 0) |
Pierre-Louis Bossart | 17ed5be | 2019-05-01 10:57:45 -0500 | [diff] [blame] | 362 | dev_err(sdw->cdns.dev, "Failed to set sync period: %d\n", ret); |
Vinod Koul | 71bb8a1 | 2017-12-14 11:19:43 +0530 | [diff] [blame] | 363 | |
| 364 | return ret; |
| 365 | } |
| 366 | |
Vinod Koul | 37a2d22 | 2018-04-26 18:38:58 +0530 | [diff] [blame] | 367 | /* |
| 368 | * PDI routines |
| 369 | */ |
| 370 | static void intel_pdi_init(struct sdw_intel *sdw, |
Pierre-Louis Bossart | d542bc9 | 2019-05-01 10:57:38 -0500 | [diff] [blame] | 371 | struct sdw_cdns_stream_config *config) |
Vinod Koul | 37a2d22 | 2018-04-26 18:38:58 +0530 | [diff] [blame] | 372 | { |
| 373 | void __iomem *shim = sdw->res->shim; |
| 374 | unsigned int link_id = sdw->instance; |
| 375 | int pcm_cap, pdm_cap; |
| 376 | |
| 377 | /* PCM Stream Capability */ |
| 378 | pcm_cap = intel_readw(shim, SDW_SHIM_PCMSCAP(link_id)); |
| 379 | |
| 380 | config->pcm_bd = (pcm_cap & SDW_SHIM_PCMSCAP_BSS) >> |
| 381 | SDW_REG_SHIFT(SDW_SHIM_PCMSCAP_BSS); |
| 382 | config->pcm_in = (pcm_cap & SDW_SHIM_PCMSCAP_ISS) >> |
| 383 | SDW_REG_SHIFT(SDW_SHIM_PCMSCAP_ISS); |
| 384 | config->pcm_out = (pcm_cap & SDW_SHIM_PCMSCAP_OSS) >> |
| 385 | SDW_REG_SHIFT(SDW_SHIM_PCMSCAP_OSS); |
| 386 | |
Pierre-Louis Bossart | 121f436 | 2019-05-22 14:47:29 -0500 | [diff] [blame] | 387 | dev_dbg(sdw->cdns.dev, "PCM cap bd:%d in:%d out:%d\n", |
| 388 | config->pcm_bd, config->pcm_in, config->pcm_out); |
| 389 | |
Vinod Koul | 37a2d22 | 2018-04-26 18:38:58 +0530 | [diff] [blame] | 390 | /* PDM Stream Capability */ |
| 391 | pdm_cap = intel_readw(shim, SDW_SHIM_PDMSCAP(link_id)); |
| 392 | |
| 393 | config->pdm_bd = (pdm_cap & SDW_SHIM_PDMSCAP_BSS) >> |
| 394 | SDW_REG_SHIFT(SDW_SHIM_PDMSCAP_BSS); |
| 395 | config->pdm_in = (pdm_cap & SDW_SHIM_PDMSCAP_ISS) >> |
| 396 | SDW_REG_SHIFT(SDW_SHIM_PDMSCAP_ISS); |
| 397 | config->pdm_out = (pdm_cap & SDW_SHIM_PDMSCAP_OSS) >> |
| 398 | SDW_REG_SHIFT(SDW_SHIM_PDMSCAP_OSS); |
Pierre-Louis Bossart | 121f436 | 2019-05-22 14:47:29 -0500 | [diff] [blame] | 399 | |
| 400 | dev_dbg(sdw->cdns.dev, "PDM cap bd:%d in:%d out:%d\n", |
| 401 | config->pdm_bd, config->pdm_in, config->pdm_out); |
Vinod Koul | 37a2d22 | 2018-04-26 18:38:58 +0530 | [diff] [blame] | 402 | } |
| 403 | |
| 404 | static int |
| 405 | intel_pdi_get_ch_cap(struct sdw_intel *sdw, unsigned int pdi_num, bool pcm) |
| 406 | { |
| 407 | void __iomem *shim = sdw->res->shim; |
| 408 | unsigned int link_id = sdw->instance; |
| 409 | int count; |
| 410 | |
| 411 | if (pcm) { |
| 412 | count = intel_readw(shim, SDW_SHIM_PCMSYCHC(link_id, pdi_num)); |
Pierre-Louis Bossart | 1804633 | 2019-08-05 19:55:07 -0500 | [diff] [blame] | 413 | |
| 414 | /* |
| 415 | * WORKAROUND: on all existing Intel controllers, pdi |
| 416 | * number 2 reports channel count as 1 even though it |
| 417 | * supports 8 channels. Performing hardcoding for pdi |
| 418 | * number 2. |
| 419 | */ |
| 420 | if (pdi_num == 2) |
| 421 | count = 7; |
| 422 | |
Vinod Koul | 37a2d22 | 2018-04-26 18:38:58 +0530 | [diff] [blame] | 423 | } else { |
| 424 | count = intel_readw(shim, SDW_SHIM_PDMSCAP(link_id)); |
| 425 | count = ((count & SDW_SHIM_PDMSCAP_CPSS) >> |
| 426 | SDW_REG_SHIFT(SDW_SHIM_PDMSCAP_CPSS)); |
| 427 | } |
| 428 | |
| 429 | /* zero based values for channel count in register */ |
| 430 | count++; |
| 431 | |
| 432 | return count; |
| 433 | } |
| 434 | |
| 435 | static int intel_pdi_get_ch_update(struct sdw_intel *sdw, |
Pierre-Louis Bossart | d542bc9 | 2019-05-01 10:57:38 -0500 | [diff] [blame] | 436 | struct sdw_cdns_pdi *pdi, |
| 437 | unsigned int num_pdi, |
| 438 | unsigned int *num_ch, bool pcm) |
Vinod Koul | 37a2d22 | 2018-04-26 18:38:58 +0530 | [diff] [blame] | 439 | { |
| 440 | int i, ch_count = 0; |
| 441 | |
| 442 | for (i = 0; i < num_pdi; i++) { |
| 443 | pdi->ch_count = intel_pdi_get_ch_cap(sdw, pdi->num, pcm); |
| 444 | ch_count += pdi->ch_count; |
| 445 | pdi++; |
| 446 | } |
| 447 | |
| 448 | *num_ch = ch_count; |
| 449 | return 0; |
| 450 | } |
| 451 | |
| 452 | static int intel_pdi_stream_ch_update(struct sdw_intel *sdw, |
Pierre-Louis Bossart | d542bc9 | 2019-05-01 10:57:38 -0500 | [diff] [blame] | 453 | struct sdw_cdns_streams *stream, bool pcm) |
Vinod Koul | 37a2d22 | 2018-04-26 18:38:58 +0530 | [diff] [blame] | 454 | { |
| 455 | intel_pdi_get_ch_update(sdw, stream->bd, stream->num_bd, |
Pierre-Louis Bossart | d542bc9 | 2019-05-01 10:57:38 -0500 | [diff] [blame] | 456 | &stream->num_ch_bd, pcm); |
Vinod Koul | 37a2d22 | 2018-04-26 18:38:58 +0530 | [diff] [blame] | 457 | |
| 458 | intel_pdi_get_ch_update(sdw, stream->in, stream->num_in, |
Pierre-Louis Bossart | d542bc9 | 2019-05-01 10:57:38 -0500 | [diff] [blame] | 459 | &stream->num_ch_in, pcm); |
Vinod Koul | 37a2d22 | 2018-04-26 18:38:58 +0530 | [diff] [blame] | 460 | |
| 461 | intel_pdi_get_ch_update(sdw, stream->out, stream->num_out, |
Pierre-Louis Bossart | d542bc9 | 2019-05-01 10:57:38 -0500 | [diff] [blame] | 462 | &stream->num_ch_out, pcm); |
Vinod Koul | 37a2d22 | 2018-04-26 18:38:58 +0530 | [diff] [blame] | 463 | |
| 464 | return 0; |
| 465 | } |
| 466 | |
| 467 | static int intel_pdi_ch_update(struct sdw_intel *sdw) |
| 468 | { |
| 469 | /* First update PCM streams followed by PDM streams */ |
| 470 | intel_pdi_stream_ch_update(sdw, &sdw->cdns.pcm, true); |
| 471 | intel_pdi_stream_ch_update(sdw, &sdw->cdns.pdm, false); |
| 472 | |
| 473 | return 0; |
| 474 | } |
| 475 | |
| 476 | static void |
| 477 | intel_pdi_shim_configure(struct sdw_intel *sdw, struct sdw_cdns_pdi *pdi) |
| 478 | { |
| 479 | void __iomem *shim = sdw->res->shim; |
| 480 | unsigned int link_id = sdw->instance; |
| 481 | int pdi_conf = 0; |
| 482 | |
| 483 | pdi->intel_alh_id = (link_id * 16) + pdi->num + 5; |
| 484 | |
| 485 | /* |
| 486 | * Program stream parameters to stream SHIM register |
| 487 | * This is applicable for PCM stream only. |
| 488 | */ |
| 489 | if (pdi->type != SDW_STREAM_PCM) |
| 490 | return; |
| 491 | |
| 492 | if (pdi->dir == SDW_DATA_DIR_RX) |
| 493 | pdi_conf |= SDW_SHIM_PCMSYCM_DIR; |
| 494 | else |
| 495 | pdi_conf &= ~(SDW_SHIM_PCMSYCM_DIR); |
| 496 | |
| 497 | pdi_conf |= (pdi->intel_alh_id << |
| 498 | SDW_REG_SHIFT(SDW_SHIM_PCMSYCM_STREAM)); |
| 499 | pdi_conf |= (pdi->l_ch_num << SDW_REG_SHIFT(SDW_SHIM_PCMSYCM_LCHN)); |
| 500 | pdi_conf |= (pdi->h_ch_num << SDW_REG_SHIFT(SDW_SHIM_PCMSYCM_HCHN)); |
| 501 | |
| 502 | intel_writew(shim, SDW_SHIM_PCMSYCHM(link_id, pdi->num), pdi_conf); |
| 503 | } |
| 504 | |
| 505 | static void |
| 506 | intel_pdi_alh_configure(struct sdw_intel *sdw, struct sdw_cdns_pdi *pdi) |
| 507 | { |
| 508 | void __iomem *alh = sdw->res->alh; |
| 509 | unsigned int link_id = sdw->instance; |
| 510 | unsigned int conf; |
| 511 | |
| 512 | pdi->intel_alh_id = (link_id * 16) + pdi->num + 5; |
| 513 | |
| 514 | /* Program Stream config ALH register */ |
| 515 | conf = intel_readl(alh, SDW_ALH_STRMZCFG(pdi->intel_alh_id)); |
| 516 | |
| 517 | conf |= (SDW_ALH_STRMZCFG_DMAT_VAL << |
| 518 | SDW_REG_SHIFT(SDW_ALH_STRMZCFG_DMAT)); |
| 519 | |
| 520 | conf |= ((pdi->ch_count - 1) << |
| 521 | SDW_REG_SHIFT(SDW_ALH_STRMZCFG_CHN)); |
| 522 | |
| 523 | intel_writel(alh, SDW_ALH_STRMZCFG(pdi->intel_alh_id), conf); |
| 524 | } |
| 525 | |
Vinod Koul | c46302e | 2018-04-26 18:39:05 +0530 | [diff] [blame] | 526 | static int intel_config_stream(struct sdw_intel *sdw, |
Pierre-Louis Bossart | d542bc9 | 2019-05-01 10:57:38 -0500 | [diff] [blame] | 527 | struct snd_pcm_substream *substream, |
| 528 | struct snd_soc_dai *dai, |
| 529 | struct snd_pcm_hw_params *hw_params, int link_id) |
Vinod Koul | c46302e | 2018-04-26 18:39:05 +0530 | [diff] [blame] | 530 | { |
Pierre-Louis Bossart | 05c8afe4 | 2019-08-05 19:55:06 -0500 | [diff] [blame] | 531 | struct sdw_intel_link_res *res = sdw->res; |
| 532 | |
| 533 | if (res->ops && res->ops->config_stream && res->arg) |
| 534 | return res->ops->config_stream(res->arg, |
Vinod Koul | c46302e | 2018-04-26 18:39:05 +0530 | [diff] [blame] | 535 | substream, dai, hw_params, link_id); |
| 536 | |
| 537 | return -EIO; |
| 538 | } |
| 539 | |
| 540 | /* |
Shreyas NC | 30246e2 | 2018-07-27 14:44:17 +0530 | [diff] [blame] | 541 | * bank switch routines |
| 542 | */ |
| 543 | |
| 544 | static int intel_pre_bank_switch(struct sdw_bus *bus) |
| 545 | { |
| 546 | struct sdw_cdns *cdns = bus_to_cdns(bus); |
| 547 | struct sdw_intel *sdw = cdns_to_intel(cdns); |
| 548 | void __iomem *shim = sdw->res->shim; |
| 549 | int sync_reg; |
| 550 | |
| 551 | /* Write to register only for multi-link */ |
| 552 | if (!bus->multi_link) |
| 553 | return 0; |
| 554 | |
| 555 | /* Read SYNC register */ |
| 556 | sync_reg = intel_readl(shim, SDW_SHIM_SYNC); |
| 557 | sync_reg |= SDW_SHIM_SYNC_CMDSYNC << sdw->instance; |
| 558 | intel_writel(shim, SDW_SHIM_SYNC, sync_reg); |
| 559 | |
| 560 | return 0; |
| 561 | } |
| 562 | |
| 563 | static int intel_post_bank_switch(struct sdw_bus *bus) |
| 564 | { |
| 565 | struct sdw_cdns *cdns = bus_to_cdns(bus); |
| 566 | struct sdw_intel *sdw = cdns_to_intel(cdns); |
| 567 | void __iomem *shim = sdw->res->shim; |
| 568 | int sync_reg, ret; |
| 569 | |
| 570 | /* Write to register only for multi-link */ |
| 571 | if (!bus->multi_link) |
| 572 | return 0; |
| 573 | |
| 574 | /* Read SYNC register */ |
| 575 | sync_reg = intel_readl(shim, SDW_SHIM_SYNC); |
| 576 | |
| 577 | /* |
| 578 | * post_bank_switch() ops is called from the bus in loop for |
| 579 | * all the Masters in the steam with the expectation that |
| 580 | * we trigger the bankswitch for the only first Master in the list |
| 581 | * and do nothing for the other Masters |
| 582 | * |
| 583 | * So, set the SYNCGO bit only if CMDSYNC bit is set for any Master. |
| 584 | */ |
| 585 | if (!(sync_reg & SDW_SHIM_SYNC_CMDSYNC_MASK)) |
| 586 | return 0; |
| 587 | |
| 588 | /* |
| 589 | * Set SyncGO bit to synchronously trigger a bank switch for |
| 590 | * all the masters. A write to SYNCGO bit clears CMDSYNC bit for all |
| 591 | * the Masters. |
| 592 | */ |
| 593 | sync_reg |= SDW_SHIM_SYNC_SYNCGO; |
| 594 | |
| 595 | ret = intel_clear_bit(shim, SDW_SHIM_SYNC, sync_reg, |
Pierre-Louis Bossart | d542bc9 | 2019-05-01 10:57:38 -0500 | [diff] [blame] | 596 | SDW_SHIM_SYNC_SYNCGO); |
Shreyas NC | 30246e2 | 2018-07-27 14:44:17 +0530 | [diff] [blame] | 597 | if (ret < 0) |
Pierre-Louis Bossart | 17ed5be | 2019-05-01 10:57:45 -0500 | [diff] [blame] | 598 | dev_err(sdw->cdns.dev, "Post bank switch failed: %d\n", ret); |
Shreyas NC | 30246e2 | 2018-07-27 14:44:17 +0530 | [diff] [blame] | 599 | |
| 600 | return ret; |
| 601 | } |
| 602 | |
| 603 | /* |
Vinod Koul | c46302e | 2018-04-26 18:39:05 +0530 | [diff] [blame] | 604 | * DAI routines |
| 605 | */ |
| 606 | |
Vinod Koul | c46302e | 2018-04-26 18:39:05 +0530 | [diff] [blame] | 607 | static int intel_hw_params(struct snd_pcm_substream *substream, |
Pierre-Louis Bossart | d542bc9 | 2019-05-01 10:57:38 -0500 | [diff] [blame] | 608 | struct snd_pcm_hw_params *params, |
| 609 | struct snd_soc_dai *dai) |
Vinod Koul | c46302e | 2018-04-26 18:39:05 +0530 | [diff] [blame] | 610 | { |
| 611 | struct sdw_cdns *cdns = snd_soc_dai_get_drvdata(dai); |
| 612 | struct sdw_intel *sdw = cdns_to_intel(cdns); |
| 613 | struct sdw_cdns_dma_data *dma; |
Pierre-Louis Bossart | 57a3479 | 2019-09-16 14:23:46 -0500 | [diff] [blame] | 614 | struct sdw_cdns_pdi *pdi; |
Vinod Koul | c46302e | 2018-04-26 18:39:05 +0530 | [diff] [blame] | 615 | struct sdw_stream_config sconfig; |
| 616 | struct sdw_port_config *pconfig; |
Pierre-Louis Bossart | 57a3479 | 2019-09-16 14:23:46 -0500 | [diff] [blame] | 617 | int ch, dir; |
| 618 | int ret; |
Vinod Koul | c46302e | 2018-04-26 18:39:05 +0530 | [diff] [blame] | 619 | bool pcm = true; |
| 620 | |
| 621 | dma = snd_soc_dai_get_dma_data(dai, substream); |
| 622 | if (!dma) |
| 623 | return -EIO; |
| 624 | |
| 625 | ch = params_channels(params); |
| 626 | if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) |
| 627 | dir = SDW_DATA_DIR_RX; |
| 628 | else |
| 629 | dir = SDW_DATA_DIR_TX; |
| 630 | |
Pierre-Louis Bossart | 57a3479 | 2019-09-16 14:23:46 -0500 | [diff] [blame] | 631 | if (dma->stream_type == SDW_STREAM_PDM) |
Vinod Koul | c46302e | 2018-04-26 18:39:05 +0530 | [diff] [blame] | 632 | pcm = false; |
Pierre-Louis Bossart | 57a3479 | 2019-09-16 14:23:46 -0500 | [diff] [blame] | 633 | |
Pierre-Louis Bossart | 57a3479 | 2019-09-16 14:23:46 -0500 | [diff] [blame] | 634 | if (pcm) |
Bard Liao | 1b53385e | 2019-09-16 14:23:48 -0500 | [diff] [blame] | 635 | pdi = sdw_cdns_alloc_pdi(cdns, &cdns->pcm, ch, dir, dai->id); |
Pierre-Louis Bossart | 57a3479 | 2019-09-16 14:23:46 -0500 | [diff] [blame] | 636 | else |
Bard Liao | 1b53385e | 2019-09-16 14:23:48 -0500 | [diff] [blame] | 637 | pdi = sdw_cdns_alloc_pdi(cdns, &cdns->pdm, ch, dir, dai->id); |
Pierre-Louis Bossart | 57a3479 | 2019-09-16 14:23:46 -0500 | [diff] [blame] | 638 | |
| 639 | if (!pdi) { |
| 640 | ret = -EINVAL; |
| 641 | goto error; |
Vinod Koul | c46302e | 2018-04-26 18:39:05 +0530 | [diff] [blame] | 642 | } |
| 643 | |
Pierre-Louis Bossart | 57a3479 | 2019-09-16 14:23:46 -0500 | [diff] [blame] | 644 | /* do run-time configurations for SHIM, ALH and PDI/PORT */ |
| 645 | intel_pdi_shim_configure(sdw, pdi); |
| 646 | intel_pdi_alh_configure(sdw, pdi); |
| 647 | sdw_cdns_config_stream(cdns, ch, dir, pdi); |
Vinod Koul | c46302e | 2018-04-26 18:39:05 +0530 | [diff] [blame] | 648 | |
Vinod Koul | c46302e | 2018-04-26 18:39:05 +0530 | [diff] [blame] | 649 | |
| 650 | /* Inform DSP about PDI stream number */ |
Pierre-Louis Bossart | 57a3479 | 2019-09-16 14:23:46 -0500 | [diff] [blame] | 651 | ret = intel_config_stream(sdw, substream, dai, params, |
| 652 | pdi->intel_alh_id); |
| 653 | if (ret) |
| 654 | goto error; |
Vinod Koul | c46302e | 2018-04-26 18:39:05 +0530 | [diff] [blame] | 655 | |
| 656 | sconfig.direction = dir; |
| 657 | sconfig.ch_count = ch; |
| 658 | sconfig.frame_rate = params_rate(params); |
| 659 | sconfig.type = dma->stream_type; |
| 660 | |
| 661 | if (dma->stream_type == SDW_STREAM_PDM) { |
| 662 | sconfig.frame_rate *= 50; |
| 663 | sconfig.bps = 1; |
| 664 | } else { |
| 665 | sconfig.bps = snd_pcm_format_width(params_format(params)); |
| 666 | } |
| 667 | |
| 668 | /* Port configuration */ |
Pierre-Louis Bossart | 57a3479 | 2019-09-16 14:23:46 -0500 | [diff] [blame] | 669 | pconfig = kcalloc(1, sizeof(*pconfig), GFP_KERNEL); |
Vinod Koul | c46302e | 2018-04-26 18:39:05 +0530 | [diff] [blame] | 670 | if (!pconfig) { |
| 671 | ret = -ENOMEM; |
Pierre-Louis Bossart | 57a3479 | 2019-09-16 14:23:46 -0500 | [diff] [blame] | 672 | goto error; |
Vinod Koul | c46302e | 2018-04-26 18:39:05 +0530 | [diff] [blame] | 673 | } |
| 674 | |
Pierre-Louis Bossart | 57a3479 | 2019-09-16 14:23:46 -0500 | [diff] [blame] | 675 | pconfig->num = pdi->num; |
| 676 | pconfig->ch_mask = (1 << ch) - 1; |
Vinod Koul | c46302e | 2018-04-26 18:39:05 +0530 | [diff] [blame] | 677 | |
| 678 | ret = sdw_stream_add_master(&cdns->bus, &sconfig, |
Pierre-Louis Bossart | 57a3479 | 2019-09-16 14:23:46 -0500 | [diff] [blame] | 679 | pconfig, 1, dma->stream); |
| 680 | if (ret) |
Pierre-Louis Bossart | 17ed5be | 2019-05-01 10:57:45 -0500 | [diff] [blame] | 681 | dev_err(cdns->dev, "add master to stream failed:%d\n", ret); |
Vinod Koul | c46302e | 2018-04-26 18:39:05 +0530 | [diff] [blame] | 682 | |
| 683 | kfree(pconfig); |
Pierre-Louis Bossart | 57a3479 | 2019-09-16 14:23:46 -0500 | [diff] [blame] | 684 | error: |
Vinod Koul | c46302e | 2018-04-26 18:39:05 +0530 | [diff] [blame] | 685 | return ret; |
| 686 | } |
| 687 | |
| 688 | static int |
| 689 | intel_hw_free(struct snd_pcm_substream *substream, struct snd_soc_dai *dai) |
| 690 | { |
| 691 | struct sdw_cdns *cdns = snd_soc_dai_get_drvdata(dai); |
| 692 | struct sdw_cdns_dma_data *dma; |
| 693 | int ret; |
| 694 | |
| 695 | dma = snd_soc_dai_get_dma_data(dai, substream); |
| 696 | if (!dma) |
| 697 | return -EIO; |
| 698 | |
| 699 | ret = sdw_stream_remove_master(&cdns->bus, dma->stream); |
| 700 | if (ret < 0) |
Pierre-Louis Bossart | 17ed5be | 2019-05-01 10:57:45 -0500 | [diff] [blame] | 701 | dev_err(dai->dev, "remove master from stream %s failed: %d\n", |
Pierre-Louis Bossart | d542bc9 | 2019-05-01 10:57:38 -0500 | [diff] [blame] | 702 | dma->stream->name, ret); |
Vinod Koul | c46302e | 2018-04-26 18:39:05 +0530 | [diff] [blame] | 703 | |
Vinod Koul | c46302e | 2018-04-26 18:39:05 +0530 | [diff] [blame] | 704 | return ret; |
| 705 | } |
| 706 | |
Pierre-Louis Bossart | 183c768 | 2019-08-05 19:55:22 -0500 | [diff] [blame] | 707 | static void intel_shutdown(struct snd_pcm_substream *substream, |
| 708 | struct snd_soc_dai *dai) |
| 709 | { |
| 710 | struct sdw_cdns_dma_data *dma; |
| 711 | |
| 712 | dma = snd_soc_dai_get_dma_data(dai, substream); |
| 713 | if (!dma) |
| 714 | return; |
| 715 | |
| 716 | snd_soc_dai_set_dma_data(dai, substream, NULL); |
| 717 | kfree(dma); |
| 718 | } |
| 719 | |
Vinod Koul | c46302e | 2018-04-26 18:39:05 +0530 | [diff] [blame] | 720 | static int intel_pcm_set_sdw_stream(struct snd_soc_dai *dai, |
Pierre-Louis Bossart | d542bc9 | 2019-05-01 10:57:38 -0500 | [diff] [blame] | 721 | void *stream, int direction) |
Vinod Koul | c46302e | 2018-04-26 18:39:05 +0530 | [diff] [blame] | 722 | { |
| 723 | return cdns_set_sdw_stream(dai, stream, true, direction); |
| 724 | } |
| 725 | |
| 726 | static int intel_pdm_set_sdw_stream(struct snd_soc_dai *dai, |
Pierre-Louis Bossart | d542bc9 | 2019-05-01 10:57:38 -0500 | [diff] [blame] | 727 | void *stream, int direction) |
Vinod Koul | c46302e | 2018-04-26 18:39:05 +0530 | [diff] [blame] | 728 | { |
| 729 | return cdns_set_sdw_stream(dai, stream, false, direction); |
| 730 | } |
| 731 | |
Julia Lawall | b163559 | 2018-10-27 15:34:42 +0200 | [diff] [blame] | 732 | static const struct snd_soc_dai_ops intel_pcm_dai_ops = { |
Vinod Koul | c46302e | 2018-04-26 18:39:05 +0530 | [diff] [blame] | 733 | .hw_params = intel_hw_params, |
| 734 | .hw_free = intel_hw_free, |
Pierre-Louis Bossart | 183c768 | 2019-08-05 19:55:22 -0500 | [diff] [blame] | 735 | .shutdown = intel_shutdown, |
Vinod Koul | c46302e | 2018-04-26 18:39:05 +0530 | [diff] [blame] | 736 | .set_sdw_stream = intel_pcm_set_sdw_stream, |
| 737 | }; |
| 738 | |
Julia Lawall | b163559 | 2018-10-27 15:34:42 +0200 | [diff] [blame] | 739 | static const struct snd_soc_dai_ops intel_pdm_dai_ops = { |
Vinod Koul | c46302e | 2018-04-26 18:39:05 +0530 | [diff] [blame] | 740 | .hw_params = intel_hw_params, |
| 741 | .hw_free = intel_hw_free, |
Pierre-Louis Bossart | 183c768 | 2019-08-05 19:55:22 -0500 | [diff] [blame] | 742 | .shutdown = intel_shutdown, |
Vinod Koul | c46302e | 2018-04-26 18:39:05 +0530 | [diff] [blame] | 743 | .set_sdw_stream = intel_pdm_set_sdw_stream, |
| 744 | }; |
| 745 | |
| 746 | static const struct snd_soc_component_driver dai_component = { |
| 747 | .name = "soundwire", |
| 748 | }; |
| 749 | |
| 750 | static int intel_create_dai(struct sdw_cdns *cdns, |
Pierre-Louis Bossart | d542bc9 | 2019-05-01 10:57:38 -0500 | [diff] [blame] | 751 | struct snd_soc_dai_driver *dais, |
| 752 | enum intel_pdi_type type, |
| 753 | u32 num, u32 off, u32 max_ch, bool pcm) |
Vinod Koul | c46302e | 2018-04-26 18:39:05 +0530 | [diff] [blame] | 754 | { |
| 755 | int i; |
| 756 | |
| 757 | if (num == 0) |
| 758 | return 0; |
| 759 | |
| 760 | /* TODO: Read supported rates/formats from hardware */ |
| 761 | for (i = off; i < (off + num); i++) { |
| 762 | dais[i].name = kasprintf(GFP_KERNEL, "SDW%d Pin%d", |
Pierre-Louis Bossart | d542bc9 | 2019-05-01 10:57:38 -0500 | [diff] [blame] | 763 | cdns->instance, i); |
Vinod Koul | c46302e | 2018-04-26 18:39:05 +0530 | [diff] [blame] | 764 | if (!dais[i].name) |
| 765 | return -ENOMEM; |
| 766 | |
| 767 | if (type == INTEL_PDI_BD || type == INTEL_PDI_OUT) { |
Vinod Koul | c46302e | 2018-04-26 18:39:05 +0530 | [diff] [blame] | 768 | dais[i].playback.channels_min = 1; |
| 769 | dais[i].playback.channels_max = max_ch; |
| 770 | dais[i].playback.rates = SNDRV_PCM_RATE_48000; |
| 771 | dais[i].playback.formats = SNDRV_PCM_FMTBIT_S16_LE; |
| 772 | } |
| 773 | |
| 774 | if (type == INTEL_PDI_BD || type == INTEL_PDI_IN) { |
Srinivas Kandagatla | 3919412 | 2019-06-06 12:23:04 +0100 | [diff] [blame] | 775 | dais[i].capture.channels_min = 1; |
| 776 | dais[i].capture.channels_max = max_ch; |
Vinod Koul | c46302e | 2018-04-26 18:39:05 +0530 | [diff] [blame] | 777 | dais[i].capture.rates = SNDRV_PCM_RATE_48000; |
| 778 | dais[i].capture.formats = SNDRV_PCM_FMTBIT_S16_LE; |
| 779 | } |
| 780 | |
Vinod Koul | c46302e | 2018-04-26 18:39:05 +0530 | [diff] [blame] | 781 | if (pcm) |
| 782 | dais[i].ops = &intel_pcm_dai_ops; |
| 783 | else |
| 784 | dais[i].ops = &intel_pdm_dai_ops; |
| 785 | } |
| 786 | |
| 787 | return 0; |
| 788 | } |
| 789 | |
| 790 | static int intel_register_dai(struct sdw_intel *sdw) |
| 791 | { |
| 792 | struct sdw_cdns *cdns = &sdw->cdns; |
| 793 | struct sdw_cdns_streams *stream; |
| 794 | struct snd_soc_dai_driver *dais; |
| 795 | int num_dai, ret, off = 0; |
| 796 | |
| 797 | /* DAIs are created based on total number of PDIs supported */ |
| 798 | num_dai = cdns->pcm.num_pdi + cdns->pdm.num_pdi; |
| 799 | |
| 800 | dais = devm_kcalloc(cdns->dev, num_dai, sizeof(*dais), GFP_KERNEL); |
| 801 | if (!dais) |
| 802 | return -ENOMEM; |
| 803 | |
| 804 | /* Create PCM DAIs */ |
| 805 | stream = &cdns->pcm; |
| 806 | |
Vinod Koul | 1215dae | 2019-05-02 16:29:25 +0530 | [diff] [blame] | 807 | ret = intel_create_dai(cdns, dais, INTEL_PDI_IN, stream->num_in, |
| 808 | off, stream->num_ch_in, true); |
Vinod Koul | c46302e | 2018-04-26 18:39:05 +0530 | [diff] [blame] | 809 | if (ret) |
| 810 | return ret; |
| 811 | |
| 812 | off += cdns->pcm.num_in; |
Vinod Koul | 1215dae | 2019-05-02 16:29:25 +0530 | [diff] [blame] | 813 | ret = intel_create_dai(cdns, dais, INTEL_PDI_OUT, cdns->pcm.num_out, |
| 814 | off, stream->num_ch_out, true); |
Vinod Koul | c46302e | 2018-04-26 18:39:05 +0530 | [diff] [blame] | 815 | if (ret) |
| 816 | return ret; |
| 817 | |
| 818 | off += cdns->pcm.num_out; |
Vinod Koul | 1215dae | 2019-05-02 16:29:25 +0530 | [diff] [blame] | 819 | ret = intel_create_dai(cdns, dais, INTEL_PDI_BD, cdns->pcm.num_bd, |
| 820 | off, stream->num_ch_bd, true); |
Vinod Koul | c46302e | 2018-04-26 18:39:05 +0530 | [diff] [blame] | 821 | if (ret) |
| 822 | return ret; |
| 823 | |
| 824 | /* Create PDM DAIs */ |
| 825 | stream = &cdns->pdm; |
| 826 | off += cdns->pcm.num_bd; |
Vinod Koul | 1215dae | 2019-05-02 16:29:25 +0530 | [diff] [blame] | 827 | ret = intel_create_dai(cdns, dais, INTEL_PDI_IN, cdns->pdm.num_in, |
| 828 | off, stream->num_ch_in, false); |
Vinod Koul | c46302e | 2018-04-26 18:39:05 +0530 | [diff] [blame] | 829 | if (ret) |
| 830 | return ret; |
| 831 | |
| 832 | off += cdns->pdm.num_in; |
Vinod Koul | 1215dae | 2019-05-02 16:29:25 +0530 | [diff] [blame] | 833 | ret = intel_create_dai(cdns, dais, INTEL_PDI_OUT, cdns->pdm.num_out, |
| 834 | off, stream->num_ch_out, false); |
Vinod Koul | c46302e | 2018-04-26 18:39:05 +0530 | [diff] [blame] | 835 | if (ret) |
| 836 | return ret; |
| 837 | |
| 838 | off += cdns->pdm.num_bd; |
Vinod Koul | 1215dae | 2019-05-02 16:29:25 +0530 | [diff] [blame] | 839 | ret = intel_create_dai(cdns, dais, INTEL_PDI_BD, cdns->pdm.num_bd, |
| 840 | off, stream->num_ch_bd, false); |
Vinod Koul | c46302e | 2018-04-26 18:39:05 +0530 | [diff] [blame] | 841 | if (ret) |
| 842 | return ret; |
| 843 | |
| 844 | return snd_soc_register_component(cdns->dev, &dai_component, |
Pierre-Louis Bossart | d542bc9 | 2019-05-01 10:57:38 -0500 | [diff] [blame] | 845 | dais, num_dai); |
Vinod Koul | c46302e | 2018-04-26 18:39:05 +0530 | [diff] [blame] | 846 | } |
| 847 | |
Pierre-Louis Bossart | 085f4ac | 2019-08-05 19:55:16 -0500 | [diff] [blame] | 848 | static int sdw_master_read_intel_prop(struct sdw_bus *bus) |
| 849 | { |
| 850 | struct sdw_master_prop *prop = &bus->prop; |
| 851 | struct fwnode_handle *link; |
| 852 | char name[32]; |
Pierre-Louis Bossart | 395713d | 2019-08-21 13:58:21 -0500 | [diff] [blame] | 853 | u32 quirk_mask; |
Pierre-Louis Bossart | 085f4ac | 2019-08-05 19:55:16 -0500 | [diff] [blame] | 854 | |
| 855 | /* Find master handle */ |
| 856 | snprintf(name, sizeof(name), |
| 857 | "mipi-sdw-link-%d-subproperties", bus->link_id); |
| 858 | |
| 859 | link = device_get_named_child_node(bus->dev, name); |
| 860 | if (!link) { |
| 861 | dev_err(bus->dev, "Master node %s not found\n", name); |
| 862 | return -EIO; |
| 863 | } |
| 864 | |
| 865 | fwnode_property_read_u32(link, |
| 866 | "intel-sdw-ip-clock", |
| 867 | &prop->mclk_freq); |
Pierre-Louis Bossart | 395713d | 2019-08-21 13:58:21 -0500 | [diff] [blame] | 868 | |
| 869 | fwnode_property_read_u32(link, |
| 870 | "intel-quirk-mask", |
| 871 | &quirk_mask); |
| 872 | |
| 873 | if (quirk_mask & SDW_INTEL_QUIRK_MASK_BUS_DISABLE) |
| 874 | prop->hw_disabled = true; |
| 875 | |
Pierre-Louis Bossart | 085f4ac | 2019-08-05 19:55:16 -0500 | [diff] [blame] | 876 | return 0; |
| 877 | } |
| 878 | |
Vinod Koul | 71bb8a1 | 2017-12-14 11:19:43 +0530 | [diff] [blame] | 879 | static int intel_prop_read(struct sdw_bus *bus) |
| 880 | { |
| 881 | /* Initialize with default handler to read all DisCo properties */ |
| 882 | sdw_master_read_prop(bus); |
| 883 | |
Pierre-Louis Bossart | 085f4ac | 2019-08-05 19:55:16 -0500 | [diff] [blame] | 884 | /* read Intel-specific properties */ |
| 885 | sdw_master_read_intel_prop(bus); |
| 886 | |
Vinod Koul | 71bb8a1 | 2017-12-14 11:19:43 +0530 | [diff] [blame] | 887 | return 0; |
| 888 | } |
| 889 | |
Shreyas NC | c91605f | 2018-04-26 18:38:43 +0530 | [diff] [blame] | 890 | static struct sdw_master_ops sdw_intel_ops = { |
| 891 | .read_prop = sdw_master_read_prop, |
| 892 | .xfer_msg = cdns_xfer_msg, |
| 893 | .xfer_msg_defer = cdns_xfer_msg_defer, |
| 894 | .reset_page_addr = cdns_reset_page_addr, |
Vinod Koul | 07abeff | 2018-04-26 18:38:48 +0530 | [diff] [blame] | 895 | .set_bus_conf = cdns_bus_conf, |
Shreyas NC | 30246e2 | 2018-07-27 14:44:17 +0530 | [diff] [blame] | 896 | .pre_bank_switch = intel_pre_bank_switch, |
| 897 | .post_bank_switch = intel_post_bank_switch, |
Shreyas NC | c91605f | 2018-04-26 18:38:43 +0530 | [diff] [blame] | 898 | }; |
| 899 | |
Pierre-Louis Bossart | dfbe642 | 2019-10-22 18:54:46 -0500 | [diff] [blame] | 900 | static int intel_init(struct sdw_intel *sdw) |
| 901 | { |
| 902 | /* Initialize shim and controller */ |
| 903 | intel_link_power_up(sdw); |
| 904 | intel_shim_init(sdw); |
| 905 | |
| 906 | return sdw_cdns_init(&sdw->cdns); |
| 907 | } |
| 908 | |
Vinod Koul | 71bb8a1 | 2017-12-14 11:19:43 +0530 | [diff] [blame] | 909 | /* |
| 910 | * probe and init |
| 911 | */ |
| 912 | static int intel_probe(struct platform_device *pdev) |
| 913 | { |
Vinod Koul | 37a2d22 | 2018-04-26 18:38:58 +0530 | [diff] [blame] | 914 | struct sdw_cdns_stream_config config; |
Vinod Koul | 71bb8a1 | 2017-12-14 11:19:43 +0530 | [diff] [blame] | 915 | struct sdw_intel *sdw; |
| 916 | int ret; |
| 917 | |
| 918 | sdw = devm_kzalloc(&pdev->dev, sizeof(*sdw), GFP_KERNEL); |
| 919 | if (!sdw) |
| 920 | return -ENOMEM; |
| 921 | |
| 922 | sdw->instance = pdev->id; |
| 923 | sdw->res = dev_get_platdata(&pdev->dev); |
| 924 | sdw->cdns.dev = &pdev->dev; |
| 925 | sdw->cdns.registers = sdw->res->registers; |
| 926 | sdw->cdns.instance = sdw->instance; |
| 927 | sdw->cdns.msg_count = 0; |
| 928 | sdw->cdns.bus.dev = &pdev->dev; |
| 929 | sdw->cdns.bus.link_id = pdev->id; |
| 930 | |
| 931 | sdw_cdns_probe(&sdw->cdns); |
| 932 | |
| 933 | /* Set property read ops */ |
Shreyas NC | c91605f | 2018-04-26 18:38:43 +0530 | [diff] [blame] | 934 | sdw_intel_ops.read_prop = intel_prop_read; |
| 935 | sdw->cdns.bus.ops = &sdw_intel_ops; |
Vinod Koul | 71bb8a1 | 2017-12-14 11:19:43 +0530 | [diff] [blame] | 936 | |
| 937 | platform_set_drvdata(pdev, sdw); |
| 938 | |
| 939 | ret = sdw_add_bus_master(&sdw->cdns.bus); |
| 940 | if (ret) { |
| 941 | dev_err(&pdev->dev, "sdw_add_bus_master fail: %d\n", ret); |
Pierre-Louis Bossart | 9e3d47f | 2019-10-22 18:54:47 -0500 | [diff] [blame^] | 942 | return ret; |
Vinod Koul | 71bb8a1 | 2017-12-14 11:19:43 +0530 | [diff] [blame] | 943 | } |
| 944 | |
Pierre-Louis Bossart | 395713d | 2019-08-21 13:58:21 -0500 | [diff] [blame] | 945 | if (sdw->cdns.bus.prop.hw_disabled) { |
| 946 | dev_info(&pdev->dev, "SoundWire master %d is disabled, ignoring\n", |
| 947 | sdw->cdns.bus.link_id); |
| 948 | return 0; |
| 949 | } |
| 950 | |
Pierre-Louis Bossart | dfbe642 | 2019-10-22 18:54:46 -0500 | [diff] [blame] | 951 | /* Initialize shim, controller and Cadence IP */ |
| 952 | ret = intel_init(sdw); |
Vinod Koul | 71bb8a1 | 2017-12-14 11:19:43 +0530 | [diff] [blame] | 953 | if (ret) |
| 954 | goto err_init; |
| 955 | |
Vinod Koul | 37a2d22 | 2018-04-26 18:38:58 +0530 | [diff] [blame] | 956 | /* Read the PDI config and initialize cadence PDI */ |
| 957 | intel_pdi_init(sdw, &config); |
| 958 | ret = sdw_cdns_pdi_init(&sdw->cdns, config); |
Vinod Koul | 71bb8a1 | 2017-12-14 11:19:43 +0530 | [diff] [blame] | 959 | if (ret) |
| 960 | goto err_init; |
| 961 | |
Vinod Koul | 37a2d22 | 2018-04-26 18:38:58 +0530 | [diff] [blame] | 962 | intel_pdi_ch_update(sdw); |
| 963 | |
Vinod Koul | 71bb8a1 | 2017-12-14 11:19:43 +0530 | [diff] [blame] | 964 | /* Acquire IRQ */ |
Pierre-Louis Bossart | d542bc9 | 2019-05-01 10:57:38 -0500 | [diff] [blame] | 965 | ret = request_threaded_irq(sdw->res->irq, sdw_cdns_irq, sdw_cdns_thread, |
| 966 | IRQF_SHARED, KBUILD_MODNAME, &sdw->cdns); |
Vinod Koul | 71bb8a1 | 2017-12-14 11:19:43 +0530 | [diff] [blame] | 967 | if (ret < 0) { |
| 968 | dev_err(sdw->cdns.dev, "unable to grab IRQ %d, disabling device\n", |
Pierre-Louis Bossart | d542bc9 | 2019-05-01 10:57:38 -0500 | [diff] [blame] | 969 | sdw->res->irq); |
Vinod Koul | 71bb8a1 | 2017-12-14 11:19:43 +0530 | [diff] [blame] | 970 | goto err_init; |
| 971 | } |
| 972 | |
Pierre-Louis Bossart | 9e3d47f | 2019-10-22 18:54:47 -0500 | [diff] [blame^] | 973 | ret = sdw_cdns_enable_interrupt(&sdw->cdns, true); |
Pierre-Louis Bossart | 49ea07d | 2019-10-22 18:54:44 -0500 | [diff] [blame] | 974 | if (ret < 0) { |
| 975 | dev_err(sdw->cdns.dev, "cannot enable interrupts\n"); |
| 976 | goto err_init; |
| 977 | } |
| 978 | |
| 979 | ret = sdw_cdns_exit_reset(&sdw->cdns); |
| 980 | if (ret < 0) { |
| 981 | dev_err(sdw->cdns.dev, "unable to exit bus reset sequence\n"); |
Pierre-Louis Bossart | 9e3d47f | 2019-10-22 18:54:47 -0500 | [diff] [blame^] | 982 | goto err_interrupt; |
Pierre-Louis Bossart | 49ea07d | 2019-10-22 18:54:44 -0500 | [diff] [blame] | 983 | } |
| 984 | |
Vinod Koul | c46302e | 2018-04-26 18:39:05 +0530 | [diff] [blame] | 985 | /* Register DAIs */ |
| 986 | ret = intel_register_dai(sdw); |
| 987 | if (ret) { |
Pierre-Louis Bossart | 17ed5be | 2019-05-01 10:57:45 -0500 | [diff] [blame] | 988 | dev_err(sdw->cdns.dev, "DAI registration failed: %d\n", ret); |
Vinod Koul | c46302e | 2018-04-26 18:39:05 +0530 | [diff] [blame] | 989 | snd_soc_unregister_component(sdw->cdns.dev); |
Pierre-Louis Bossart | 9e3d47f | 2019-10-22 18:54:47 -0500 | [diff] [blame^] | 990 | goto err_interrupt; |
Vinod Koul | c46302e | 2018-04-26 18:39:05 +0530 | [diff] [blame] | 991 | } |
| 992 | |
Pierre-Louis Bossart | 79ee663 | 2019-08-21 13:58:20 -0500 | [diff] [blame] | 993 | intel_debugfs_init(sdw); |
| 994 | |
Vinod Koul | 71bb8a1 | 2017-12-14 11:19:43 +0530 | [diff] [blame] | 995 | return 0; |
| 996 | |
Pierre-Louis Bossart | 9e3d47f | 2019-10-22 18:54:47 -0500 | [diff] [blame^] | 997 | err_interrupt: |
| 998 | sdw_cdns_enable_interrupt(&sdw->cdns, false); |
Vinod Koul | c46302e | 2018-04-26 18:39:05 +0530 | [diff] [blame] | 999 | free_irq(sdw->res->irq, sdw); |
Vinod Koul | 71bb8a1 | 2017-12-14 11:19:43 +0530 | [diff] [blame] | 1000 | err_init: |
| 1001 | sdw_delete_bus_master(&sdw->cdns.bus); |
Vinod Koul | 71bb8a1 | 2017-12-14 11:19:43 +0530 | [diff] [blame] | 1002 | return ret; |
| 1003 | } |
| 1004 | |
| 1005 | static int intel_remove(struct platform_device *pdev) |
| 1006 | { |
| 1007 | struct sdw_intel *sdw; |
| 1008 | |
| 1009 | sdw = platform_get_drvdata(pdev); |
| 1010 | |
Pierre-Louis Bossart | 395713d | 2019-08-21 13:58:21 -0500 | [diff] [blame] | 1011 | if (!sdw->cdns.bus.prop.hw_disabled) { |
| 1012 | intel_debugfs_exit(sdw); |
Pierre-Louis Bossart | 9e3d47f | 2019-10-22 18:54:47 -0500 | [diff] [blame^] | 1013 | sdw_cdns_enable_interrupt(&sdw->cdns, false); |
Pierre-Louis Bossart | 395713d | 2019-08-21 13:58:21 -0500 | [diff] [blame] | 1014 | free_irq(sdw->res->irq, sdw); |
| 1015 | snd_soc_unregister_component(sdw->cdns.dev); |
| 1016 | } |
Vinod Koul | 71bb8a1 | 2017-12-14 11:19:43 +0530 | [diff] [blame] | 1017 | sdw_delete_bus_master(&sdw->cdns.bus); |
| 1018 | |
| 1019 | return 0; |
| 1020 | } |
| 1021 | |
| 1022 | static struct platform_driver sdw_intel_drv = { |
| 1023 | .probe = intel_probe, |
| 1024 | .remove = intel_remove, |
| 1025 | .driver = { |
| 1026 | .name = "int-sdw", |
| 1027 | |
| 1028 | }, |
| 1029 | }; |
| 1030 | |
| 1031 | module_platform_driver(sdw_intel_drv); |
| 1032 | |
| 1033 | MODULE_LICENSE("Dual BSD/GPL"); |
| 1034 | MODULE_ALIAS("platform:int-sdw"); |
| 1035 | MODULE_DESCRIPTION("Intel Soundwire Master Driver"); |