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