1320b8b0dSShuming Fan // SPDX-License-Identifier: GPL-2.0 2320b8b0dSShuming Fan // 3320b8b0dSShuming Fan // rt711-sdw.c -- rt711 ALSA SoC audio driver 4320b8b0dSShuming Fan // 5320b8b0dSShuming Fan // Copyright(c) 2019 Realtek Semiconductor Corp. 6320b8b0dSShuming Fan // 7320b8b0dSShuming Fan // 8320b8b0dSShuming Fan 9320b8b0dSShuming Fan #include <linux/delay.h> 10320b8b0dSShuming Fan #include <linux/device.h> 11320b8b0dSShuming Fan #include <linux/mod_devicetable.h> 12320b8b0dSShuming Fan #include <linux/soundwire/sdw.h> 13320b8b0dSShuming Fan #include <linux/soundwire/sdw_type.h> 14320b8b0dSShuming Fan #include <linux/module.h> 15320b8b0dSShuming Fan #include <linux/regmap.h> 16320b8b0dSShuming Fan #include <sound/soc.h> 17320b8b0dSShuming Fan #include "rt711.h" 18320b8b0dSShuming Fan #include "rt711-sdw.h" 19320b8b0dSShuming Fan 20320b8b0dSShuming Fan static bool rt711_readable_register(struct device *dev, unsigned int reg) 21320b8b0dSShuming Fan { 22320b8b0dSShuming Fan switch (reg) { 23320b8b0dSShuming Fan case 0x00e0: 24320b8b0dSShuming Fan case 0x00f0: 25320b8b0dSShuming Fan case 0x2012 ... 0x2016: 26320b8b0dSShuming Fan case 0x201a ... 0x2027: 27320b8b0dSShuming Fan case 0x2029 ... 0x202a: 28320b8b0dSShuming Fan case 0x202d ... 0x2034: 29320b8b0dSShuming Fan case 0x2201 ... 0x2204: 30320b8b0dSShuming Fan case 0x2206 ... 0x2212: 31320b8b0dSShuming Fan case 0x2220 ... 0x2223: 32320b8b0dSShuming Fan case 0x2230 ... 0x2239: 33320b8b0dSShuming Fan case 0x2f01 ... 0x2f0f: 34320b8b0dSShuming Fan case 0x3000 ... 0x3fff: 35320b8b0dSShuming Fan case 0x7000 ... 0x7fff: 36320b8b0dSShuming Fan case 0x8300 ... 0x83ff: 37320b8b0dSShuming Fan case 0x9c00 ... 0x9cff: 38320b8b0dSShuming Fan case 0xb900 ... 0xb9ff: 39320b8b0dSShuming Fan case 0x752009: 40320b8b0dSShuming Fan case 0x752011: 41320b8b0dSShuming Fan case 0x75201a: 42320b8b0dSShuming Fan case 0x752045: 43320b8b0dSShuming Fan case 0x752046: 44320b8b0dSShuming Fan case 0x752048: 45320b8b0dSShuming Fan case 0x75204a: 46320b8b0dSShuming Fan case 0x75206b: 47320b8b0dSShuming Fan case 0x75206f: 48320b8b0dSShuming Fan case 0x752080: 49320b8b0dSShuming Fan case 0x752081: 50320b8b0dSShuming Fan case 0x752091: 51320b8b0dSShuming Fan case 0x755800: 52320b8b0dSShuming Fan return true; 53320b8b0dSShuming Fan default: 54320b8b0dSShuming Fan return false; 55320b8b0dSShuming Fan } 56320b8b0dSShuming Fan } 57320b8b0dSShuming Fan 58320b8b0dSShuming Fan static bool rt711_volatile_register(struct device *dev, unsigned int reg) 59320b8b0dSShuming Fan { 60320b8b0dSShuming Fan switch (reg) { 61320b8b0dSShuming Fan case 0x2016: 62320b8b0dSShuming Fan case 0x201b: 63320b8b0dSShuming Fan case 0x201c: 64320b8b0dSShuming Fan case 0x201d: 65320b8b0dSShuming Fan case 0x201f: 66320b8b0dSShuming Fan case 0x2021: 67320b8b0dSShuming Fan case 0x2023: 68320b8b0dSShuming Fan case 0x2230: 69320b8b0dSShuming Fan case 0x2012 ... 0x2015: /* HD-A read */ 70320b8b0dSShuming Fan case 0x202d ... 0x202f: /* BRA */ 71320b8b0dSShuming Fan case 0x2201 ... 0x2212: /* i2c debug */ 72320b8b0dSShuming Fan case 0x2220 ... 0x2223: /* decoded HD-A */ 73320b8b0dSShuming Fan case 0x9c00 ... 0x9cff: 74320b8b0dSShuming Fan case 0xb900 ... 0xb9ff: 75320b8b0dSShuming Fan case 0xff01: 76320b8b0dSShuming Fan case 0x75201a: 77320b8b0dSShuming Fan case 0x752046: 78320b8b0dSShuming Fan case 0x752080: 79320b8b0dSShuming Fan case 0x752081: 80320b8b0dSShuming Fan case 0x755800: 81320b8b0dSShuming Fan return true; 82320b8b0dSShuming Fan default: 83320b8b0dSShuming Fan return false; 84320b8b0dSShuming Fan } 85320b8b0dSShuming Fan } 86320b8b0dSShuming Fan 87320b8b0dSShuming Fan static int rt711_sdw_read(void *context, unsigned int reg, unsigned int *val) 88320b8b0dSShuming Fan { 89320b8b0dSShuming Fan struct device *dev = context; 90320b8b0dSShuming Fan struct rt711_priv *rt711 = dev_get_drvdata(dev); 91320b8b0dSShuming Fan unsigned int sdw_data_3, sdw_data_2, sdw_data_1, sdw_data_0; 92320b8b0dSShuming Fan unsigned int reg2 = 0, reg3 = 0, reg4 = 0, mask, nid, val2; 93320b8b0dSShuming Fan unsigned int is_hda_reg = 1, is_index_reg = 0; 94320b8b0dSShuming Fan int ret; 95320b8b0dSShuming Fan 96320b8b0dSShuming Fan if (reg > 0xffff) 97320b8b0dSShuming Fan is_index_reg = 1; 98320b8b0dSShuming Fan 99320b8b0dSShuming Fan mask = reg & 0xf000; 100320b8b0dSShuming Fan 101320b8b0dSShuming Fan if (is_index_reg) { /* index registers */ 102320b8b0dSShuming Fan val2 = reg & 0xff; 103320b8b0dSShuming Fan reg = reg >> 8; 104320b8b0dSShuming Fan nid = reg & 0xff; 105320b8b0dSShuming Fan ret = regmap_write(rt711->sdw_regmap, reg, 0); 106320b8b0dSShuming Fan if (ret < 0) 107320b8b0dSShuming Fan return ret; 108320b8b0dSShuming Fan reg2 = reg + 0x1000; 109320b8b0dSShuming Fan reg2 |= 0x80; 110320b8b0dSShuming Fan ret = regmap_write(rt711->sdw_regmap, reg2, val2); 111320b8b0dSShuming Fan if (ret < 0) 112320b8b0dSShuming Fan return ret; 113320b8b0dSShuming Fan 114320b8b0dSShuming Fan reg3 = RT711_PRIV_DATA_R_H | nid; 115320b8b0dSShuming Fan ret = regmap_write(rt711->sdw_regmap, 116320b8b0dSShuming Fan reg3, ((*val >> 8) & 0xff)); 117320b8b0dSShuming Fan if (ret < 0) 118320b8b0dSShuming Fan return ret; 119320b8b0dSShuming Fan reg4 = reg3 + 0x1000; 120320b8b0dSShuming Fan reg4 |= 0x80; 121320b8b0dSShuming Fan ret = regmap_write(rt711->sdw_regmap, reg4, (*val & 0xff)); 122320b8b0dSShuming Fan if (ret < 0) 123320b8b0dSShuming Fan return ret; 124320b8b0dSShuming Fan } else if (mask == 0x3000) { 125320b8b0dSShuming Fan reg += 0x8000; 126320b8b0dSShuming Fan ret = regmap_write(rt711->sdw_regmap, reg, *val); 127320b8b0dSShuming Fan if (ret < 0) 128320b8b0dSShuming Fan return ret; 129320b8b0dSShuming Fan } else if (mask == 0x7000) { 130320b8b0dSShuming Fan reg += 0x2000; 131320b8b0dSShuming Fan reg |= 0x800; 132320b8b0dSShuming Fan ret = regmap_write(rt711->sdw_regmap, 133320b8b0dSShuming Fan reg, ((*val >> 8) & 0xff)); 134320b8b0dSShuming Fan if (ret < 0) 135320b8b0dSShuming Fan return ret; 136320b8b0dSShuming Fan reg2 = reg + 0x1000; 137320b8b0dSShuming Fan reg2 |= 0x80; 138320b8b0dSShuming Fan ret = regmap_write(rt711->sdw_regmap, reg2, (*val & 0xff)); 139320b8b0dSShuming Fan if (ret < 0) 140320b8b0dSShuming Fan return ret; 141320b8b0dSShuming Fan } else if ((reg & 0xff00) == 0x8300) { /* for R channel */ 142320b8b0dSShuming Fan reg2 = reg - 0x1000; 143320b8b0dSShuming Fan reg2 &= ~0x80; 144320b8b0dSShuming Fan ret = regmap_write(rt711->sdw_regmap, 145320b8b0dSShuming Fan reg2, ((*val >> 8) & 0xff)); 146320b8b0dSShuming Fan if (ret < 0) 147320b8b0dSShuming Fan return ret; 148320b8b0dSShuming Fan ret = regmap_write(rt711->sdw_regmap, reg, (*val & 0xff)); 149320b8b0dSShuming Fan if (ret < 0) 150320b8b0dSShuming Fan return ret; 151320b8b0dSShuming Fan } else if (mask == 0x9000) { 152320b8b0dSShuming Fan ret = regmap_write(rt711->sdw_regmap, 153320b8b0dSShuming Fan reg, ((*val >> 8) & 0xff)); 154320b8b0dSShuming Fan if (ret < 0) 155320b8b0dSShuming Fan return ret; 156320b8b0dSShuming Fan reg2 = reg + 0x1000; 157320b8b0dSShuming Fan reg2 |= 0x80; 158320b8b0dSShuming Fan ret = regmap_write(rt711->sdw_regmap, reg2, (*val & 0xff)); 159320b8b0dSShuming Fan if (ret < 0) 160320b8b0dSShuming Fan return ret; 161320b8b0dSShuming Fan } else if (mask == 0xb000) { 162320b8b0dSShuming Fan ret = regmap_write(rt711->sdw_regmap, reg, *val); 163320b8b0dSShuming Fan if (ret < 0) 164320b8b0dSShuming Fan return ret; 165320b8b0dSShuming Fan } else { 166320b8b0dSShuming Fan ret = regmap_read(rt711->sdw_regmap, reg, val); 167320b8b0dSShuming Fan if (ret < 0) 168320b8b0dSShuming Fan return ret; 169320b8b0dSShuming Fan is_hda_reg = 0; 170320b8b0dSShuming Fan } 171320b8b0dSShuming Fan 172320b8b0dSShuming Fan if (is_hda_reg || is_index_reg) { 173320b8b0dSShuming Fan sdw_data_3 = 0; 174320b8b0dSShuming Fan sdw_data_2 = 0; 175320b8b0dSShuming Fan sdw_data_1 = 0; 176320b8b0dSShuming Fan sdw_data_0 = 0; 177320b8b0dSShuming Fan ret = regmap_read(rt711->sdw_regmap, 178320b8b0dSShuming Fan RT711_READ_HDA_3, &sdw_data_3); 179320b8b0dSShuming Fan if (ret < 0) 180320b8b0dSShuming Fan return ret; 181320b8b0dSShuming Fan ret = regmap_read(rt711->sdw_regmap, 182320b8b0dSShuming Fan RT711_READ_HDA_2, &sdw_data_2); 183320b8b0dSShuming Fan if (ret < 0) 184320b8b0dSShuming Fan return ret; 185320b8b0dSShuming Fan ret = regmap_read(rt711->sdw_regmap, 186320b8b0dSShuming Fan RT711_READ_HDA_1, &sdw_data_1); 187320b8b0dSShuming Fan if (ret < 0) 188320b8b0dSShuming Fan return ret; 189320b8b0dSShuming Fan ret = regmap_read(rt711->sdw_regmap, 190320b8b0dSShuming Fan RT711_READ_HDA_0, &sdw_data_0); 191320b8b0dSShuming Fan if (ret < 0) 192320b8b0dSShuming Fan return ret; 193320b8b0dSShuming Fan *val = ((sdw_data_3 & 0xff) << 24) | 194320b8b0dSShuming Fan ((sdw_data_2 & 0xff) << 16) | 195320b8b0dSShuming Fan ((sdw_data_1 & 0xff) << 8) | (sdw_data_0 & 0xff); 196320b8b0dSShuming Fan } 197320b8b0dSShuming Fan 198320b8b0dSShuming Fan if (is_hda_reg == 0) 199320b8b0dSShuming Fan dev_dbg(dev, "[%s] %04x => %08x\n", __func__, reg, *val); 200320b8b0dSShuming Fan else if (is_index_reg) 201320b8b0dSShuming Fan dev_dbg(dev, "[%s] %04x %04x %04x %04x => %08x\n", 202320b8b0dSShuming Fan __func__, reg, reg2, reg3, reg4, *val); 203320b8b0dSShuming Fan else 204320b8b0dSShuming Fan dev_dbg(dev, "[%s] %04x %04x => %08x\n", 205320b8b0dSShuming Fan __func__, reg, reg2, *val); 206320b8b0dSShuming Fan 207320b8b0dSShuming Fan return 0; 208320b8b0dSShuming Fan } 209320b8b0dSShuming Fan 210320b8b0dSShuming Fan static int rt711_sdw_write(void *context, unsigned int reg, unsigned int val) 211320b8b0dSShuming Fan { 212320b8b0dSShuming Fan struct device *dev = context; 213320b8b0dSShuming Fan struct rt711_priv *rt711 = dev_get_drvdata(dev); 214320b8b0dSShuming Fan unsigned int reg2 = 0, reg3, reg4, nid, mask, val2; 215320b8b0dSShuming Fan unsigned int is_index_reg = 0; 216320b8b0dSShuming Fan int ret; 217320b8b0dSShuming Fan 218320b8b0dSShuming Fan if (reg > 0xffff) 219320b8b0dSShuming Fan is_index_reg = 1; 220320b8b0dSShuming Fan 221320b8b0dSShuming Fan mask = reg & 0xf000; 222320b8b0dSShuming Fan 223320b8b0dSShuming Fan if (is_index_reg) { /* index registers */ 224320b8b0dSShuming Fan val2 = reg & 0xff; 225320b8b0dSShuming Fan reg = reg >> 8; 226320b8b0dSShuming Fan nid = reg & 0xff; 227320b8b0dSShuming Fan ret = regmap_write(rt711->sdw_regmap, reg, 0); 228320b8b0dSShuming Fan if (ret < 0) 229320b8b0dSShuming Fan return ret; 230320b8b0dSShuming Fan reg2 = reg + 0x1000; 231320b8b0dSShuming Fan reg2 |= 0x80; 232320b8b0dSShuming Fan ret = regmap_write(rt711->sdw_regmap, reg2, val2); 233320b8b0dSShuming Fan if (ret < 0) 234320b8b0dSShuming Fan return ret; 235320b8b0dSShuming Fan 236320b8b0dSShuming Fan reg3 = RT711_PRIV_DATA_W_H | nid; 237320b8b0dSShuming Fan ret = regmap_write(rt711->sdw_regmap, 238320b8b0dSShuming Fan reg3, ((val >> 8) & 0xff)); 239320b8b0dSShuming Fan if (ret < 0) 240320b8b0dSShuming Fan return ret; 241320b8b0dSShuming Fan reg4 = reg3 + 0x1000; 242320b8b0dSShuming Fan reg4 |= 0x80; 243320b8b0dSShuming Fan ret = regmap_write(rt711->sdw_regmap, reg4, (val & 0xff)); 244320b8b0dSShuming Fan if (ret < 0) 245320b8b0dSShuming Fan return ret; 246320b8b0dSShuming Fan is_index_reg = 1; 247320b8b0dSShuming Fan } else if (reg < 0x4fff) { 248320b8b0dSShuming Fan ret = regmap_write(rt711->sdw_regmap, reg, val); 249320b8b0dSShuming Fan if (ret < 0) 250320b8b0dSShuming Fan return ret; 251320b8b0dSShuming Fan } else if (reg == RT711_FUNC_RESET) { 252320b8b0dSShuming Fan ret = regmap_write(rt711->sdw_regmap, reg, val); 253320b8b0dSShuming Fan if (ret < 0) 254320b8b0dSShuming Fan return ret; 255320b8b0dSShuming Fan } else if (mask == 0x7000) { 256320b8b0dSShuming Fan ret = regmap_write(rt711->sdw_regmap, 257320b8b0dSShuming Fan reg, ((val >> 8) & 0xff)); 258320b8b0dSShuming Fan if (ret < 0) 259320b8b0dSShuming Fan return ret; 260320b8b0dSShuming Fan reg2 = reg + 0x1000; 261320b8b0dSShuming Fan reg2 |= 0x80; 262320b8b0dSShuming Fan ret = regmap_write(rt711->sdw_regmap, reg2, (val & 0xff)); 263320b8b0dSShuming Fan if (ret < 0) 264320b8b0dSShuming Fan return ret; 265320b8b0dSShuming Fan } else if ((reg & 0xff00) == 0x8300) { /* for R channel */ 266320b8b0dSShuming Fan reg2 = reg - 0x1000; 267320b8b0dSShuming Fan reg2 &= ~0x80; 268320b8b0dSShuming Fan ret = regmap_write(rt711->sdw_regmap, 269320b8b0dSShuming Fan reg2, ((val >> 8) & 0xff)); 270320b8b0dSShuming Fan if (ret < 0) 271320b8b0dSShuming Fan return ret; 272320b8b0dSShuming Fan ret = regmap_write(rt711->sdw_regmap, reg, (val & 0xff)); 273320b8b0dSShuming Fan if (ret < 0) 274320b8b0dSShuming Fan return ret; 275320b8b0dSShuming Fan } 276320b8b0dSShuming Fan 277320b8b0dSShuming Fan if (reg2 == 0) 278320b8b0dSShuming Fan dev_dbg(dev, "[%s] %04x <= %04x\n", __func__, reg, val); 279320b8b0dSShuming Fan else if (is_index_reg) 280320b8b0dSShuming Fan dev_dbg(dev, "[%s] %04x %04x %04x %04x <= %04x %04x\n", 281320b8b0dSShuming Fan __func__, reg, reg2, reg3, reg4, val2, val); 282320b8b0dSShuming Fan else 283320b8b0dSShuming Fan dev_dbg(dev, "[%s] %04x %04x <= %04x\n", 284320b8b0dSShuming Fan __func__, reg, reg2, val); 285320b8b0dSShuming Fan 286320b8b0dSShuming Fan return 0; 287320b8b0dSShuming Fan } 288320b8b0dSShuming Fan 289320b8b0dSShuming Fan static const struct regmap_config rt711_regmap = { 290320b8b0dSShuming Fan .reg_bits = 24, 291320b8b0dSShuming Fan .val_bits = 32, 292320b8b0dSShuming Fan .readable_reg = rt711_readable_register, 293320b8b0dSShuming Fan .volatile_reg = rt711_volatile_register, 294320b8b0dSShuming Fan .max_register = 0x755800, 295320b8b0dSShuming Fan .reg_defaults = rt711_reg_defaults, 296320b8b0dSShuming Fan .num_reg_defaults = ARRAY_SIZE(rt711_reg_defaults), 297320b8b0dSShuming Fan .cache_type = REGCACHE_RBTREE, 298320b8b0dSShuming Fan .use_single_read = true, 299320b8b0dSShuming Fan .use_single_write = true, 300320b8b0dSShuming Fan .reg_read = rt711_sdw_read, 301320b8b0dSShuming Fan .reg_write = rt711_sdw_write, 302320b8b0dSShuming Fan }; 303320b8b0dSShuming Fan 304320b8b0dSShuming Fan static const struct regmap_config rt711_sdw_regmap = { 305320b8b0dSShuming Fan .name = "sdw", 306320b8b0dSShuming Fan .reg_bits = 32, 307320b8b0dSShuming Fan .val_bits = 8, 308320b8b0dSShuming Fan .readable_reg = rt711_readable_register, 309320b8b0dSShuming Fan .max_register = 0xff01, 310320b8b0dSShuming Fan .cache_type = REGCACHE_NONE, 311320b8b0dSShuming Fan .use_single_read = true, 312320b8b0dSShuming Fan .use_single_write = true, 313320b8b0dSShuming Fan }; 314320b8b0dSShuming Fan 315320b8b0dSShuming Fan static int rt711_update_status(struct sdw_slave *slave, 316320b8b0dSShuming Fan enum sdw_slave_status status) 317320b8b0dSShuming Fan { 318320b8b0dSShuming Fan struct rt711_priv *rt711 = dev_get_drvdata(&slave->dev); 319320b8b0dSShuming Fan 320320b8b0dSShuming Fan /* Update the status */ 321320b8b0dSShuming Fan rt711->status = status; 322320b8b0dSShuming Fan 323320b8b0dSShuming Fan if (status == SDW_SLAVE_UNATTACHED) 324320b8b0dSShuming Fan rt711->hw_init = false; 325320b8b0dSShuming Fan 326320b8b0dSShuming Fan /* 327320b8b0dSShuming Fan * Perform initialization only if slave status is present and 328320b8b0dSShuming Fan * hw_init flag is false 329320b8b0dSShuming Fan */ 330320b8b0dSShuming Fan if (rt711->hw_init || rt711->status != SDW_SLAVE_ATTACHED) 331320b8b0dSShuming Fan return 0; 332320b8b0dSShuming Fan 333320b8b0dSShuming Fan /* perform I/O transfers required for Slave initialization */ 334320b8b0dSShuming Fan return rt711_io_init(&slave->dev, slave); 335320b8b0dSShuming Fan } 336320b8b0dSShuming Fan 337320b8b0dSShuming Fan static int rt711_read_prop(struct sdw_slave *slave) 338320b8b0dSShuming Fan { 339320b8b0dSShuming Fan struct sdw_slave_prop *prop = &slave->prop; 340320b8b0dSShuming Fan int nval, i, num_of_ports = 1; 341320b8b0dSShuming Fan u32 bit; 342320b8b0dSShuming Fan unsigned long addr; 343320b8b0dSShuming Fan struct sdw_dpn_prop *dpn; 344320b8b0dSShuming Fan 345320b8b0dSShuming Fan prop->paging_support = false; 346320b8b0dSShuming Fan 347320b8b0dSShuming Fan /* first we need to allocate memory for set bits in port lists */ 348320b8b0dSShuming Fan prop->source_ports = 0x14; /* BITMAP: 00010100 */ 349320b8b0dSShuming Fan prop->sink_ports = 0x8; /* BITMAP: 00001000 */ 350320b8b0dSShuming Fan 351320b8b0dSShuming Fan nval = hweight32(prop->source_ports); 352320b8b0dSShuming Fan num_of_ports += nval; 353320b8b0dSShuming Fan prop->src_dpn_prop = devm_kcalloc(&slave->dev, nval, 354320b8b0dSShuming Fan sizeof(*prop->src_dpn_prop), 355320b8b0dSShuming Fan GFP_KERNEL); 356320b8b0dSShuming Fan if (!prop->src_dpn_prop) 357320b8b0dSShuming Fan return -ENOMEM; 358320b8b0dSShuming Fan 359320b8b0dSShuming Fan i = 0; 360320b8b0dSShuming Fan dpn = prop->src_dpn_prop; 361320b8b0dSShuming Fan addr = prop->source_ports; 362320b8b0dSShuming Fan for_each_set_bit(bit, &addr, 32) { 363320b8b0dSShuming Fan dpn[i].num = bit; 364320b8b0dSShuming Fan dpn[i].type = SDW_DPN_FULL; 365320b8b0dSShuming Fan dpn[i].simple_ch_prep_sm = true; 366320b8b0dSShuming Fan dpn[i].ch_prep_timeout = 10; 367320b8b0dSShuming Fan i++; 368320b8b0dSShuming Fan } 369320b8b0dSShuming Fan 370320b8b0dSShuming Fan /* do this again for sink now */ 371320b8b0dSShuming Fan nval = hweight32(prop->sink_ports); 372320b8b0dSShuming Fan num_of_ports += nval; 373320b8b0dSShuming Fan prop->sink_dpn_prop = devm_kcalloc(&slave->dev, nval, 374320b8b0dSShuming Fan sizeof(*prop->sink_dpn_prop), 375320b8b0dSShuming Fan GFP_KERNEL); 376320b8b0dSShuming Fan if (!prop->sink_dpn_prop) 377320b8b0dSShuming Fan return -ENOMEM; 378320b8b0dSShuming Fan 379320b8b0dSShuming Fan i = 0; 380320b8b0dSShuming Fan dpn = prop->sink_dpn_prop; 381320b8b0dSShuming Fan addr = prop->sink_ports; 382320b8b0dSShuming Fan for_each_set_bit(bit, &addr, 32) { 383320b8b0dSShuming Fan dpn[i].num = bit; 384320b8b0dSShuming Fan dpn[i].type = SDW_DPN_FULL; 385320b8b0dSShuming Fan dpn[i].simple_ch_prep_sm = true; 386320b8b0dSShuming Fan dpn[i].ch_prep_timeout = 10; 387320b8b0dSShuming Fan i++; 388320b8b0dSShuming Fan } 389320b8b0dSShuming Fan 390320b8b0dSShuming Fan /* Allocate port_ready based on num_of_ports */ 391320b8b0dSShuming Fan slave->port_ready = devm_kcalloc(&slave->dev, num_of_ports, 392320b8b0dSShuming Fan sizeof(*slave->port_ready), 393320b8b0dSShuming Fan GFP_KERNEL); 394320b8b0dSShuming Fan if (!slave->port_ready) 395320b8b0dSShuming Fan return -ENOMEM; 396320b8b0dSShuming Fan 397320b8b0dSShuming Fan /* Initialize completion */ 398320b8b0dSShuming Fan for (i = 0; i < num_of_ports; i++) 399320b8b0dSShuming Fan init_completion(&slave->port_ready[i]); 400320b8b0dSShuming Fan 401320b8b0dSShuming Fan /* set the timeout values */ 402320b8b0dSShuming Fan prop->clk_stop_timeout = 20; 403320b8b0dSShuming Fan 404320b8b0dSShuming Fan /* wake-up event */ 405320b8b0dSShuming Fan prop->wake_capable = 1; 406320b8b0dSShuming Fan 407320b8b0dSShuming Fan return 0; 408320b8b0dSShuming Fan } 409320b8b0dSShuming Fan 410320b8b0dSShuming Fan static int rt711_bus_config(struct sdw_slave *slave, 411320b8b0dSShuming Fan struct sdw_bus_params *params) 412320b8b0dSShuming Fan { 413320b8b0dSShuming Fan struct rt711_priv *rt711 = dev_get_drvdata(&slave->dev); 414320b8b0dSShuming Fan int ret; 415320b8b0dSShuming Fan 416320b8b0dSShuming Fan memcpy(&rt711->params, params, sizeof(*params)); 417320b8b0dSShuming Fan 418320b8b0dSShuming Fan ret = rt711_clock_config(&slave->dev); 419320b8b0dSShuming Fan if (ret < 0) 420320b8b0dSShuming Fan dev_err(&slave->dev, "Invalid clk config"); 421320b8b0dSShuming Fan 422320b8b0dSShuming Fan return ret; 423320b8b0dSShuming Fan } 424320b8b0dSShuming Fan 425320b8b0dSShuming Fan static int rt711_interrupt_callback(struct sdw_slave *slave, 426320b8b0dSShuming Fan struct sdw_slave_intr_status *status) 427320b8b0dSShuming Fan { 428320b8b0dSShuming Fan struct rt711_priv *rt711 = dev_get_drvdata(&slave->dev); 429320b8b0dSShuming Fan 430320b8b0dSShuming Fan dev_dbg(&slave->dev, 431320b8b0dSShuming Fan "%s control_port_stat=%x", __func__, status->control_port); 432320b8b0dSShuming Fan 433320b8b0dSShuming Fan if (status->control_port & 0x4) { 434320b8b0dSShuming Fan mod_delayed_work(system_power_efficient_wq, 435320b8b0dSShuming Fan &rt711->jack_detect_work, msecs_to_jiffies(250)); 436320b8b0dSShuming Fan } 437320b8b0dSShuming Fan 438320b8b0dSShuming Fan return 0; 439320b8b0dSShuming Fan } 440320b8b0dSShuming Fan 441320b8b0dSShuming Fan static struct sdw_slave_ops rt711_slave_ops = { 442320b8b0dSShuming Fan .read_prop = rt711_read_prop, 443320b8b0dSShuming Fan .interrupt_callback = rt711_interrupt_callback, 444320b8b0dSShuming Fan .update_status = rt711_update_status, 445320b8b0dSShuming Fan .bus_config = rt711_bus_config, 446320b8b0dSShuming Fan }; 447320b8b0dSShuming Fan 448320b8b0dSShuming Fan static int rt711_sdw_probe(struct sdw_slave *slave, 449320b8b0dSShuming Fan const struct sdw_device_id *id) 450320b8b0dSShuming Fan { 451320b8b0dSShuming Fan struct regmap *sdw_regmap, *regmap; 452320b8b0dSShuming Fan 453320b8b0dSShuming Fan /* Regmap Initialization */ 454320b8b0dSShuming Fan sdw_regmap = devm_regmap_init_sdw(slave, &rt711_sdw_regmap); 455be1a4b2cSVinod Koul if (IS_ERR(sdw_regmap)) 456be1a4b2cSVinod Koul return PTR_ERR(sdw_regmap); 457320b8b0dSShuming Fan 458320b8b0dSShuming Fan regmap = devm_regmap_init(&slave->dev, NULL, 459320b8b0dSShuming Fan &slave->dev, &rt711_regmap); 460b03c29c1SWei Yongjun if (IS_ERR(regmap)) 461b03c29c1SWei Yongjun return PTR_ERR(regmap); 462320b8b0dSShuming Fan 463320b8b0dSShuming Fan rt711_init(&slave->dev, sdw_regmap, regmap, slave); 464320b8b0dSShuming Fan 465320b8b0dSShuming Fan return 0; 466320b8b0dSShuming Fan } 467320b8b0dSShuming Fan 468320b8b0dSShuming Fan static int rt711_sdw_remove(struct sdw_slave *slave) 469320b8b0dSShuming Fan { 470320b8b0dSShuming Fan struct rt711_priv *rt711 = dev_get_drvdata(&slave->dev); 471320b8b0dSShuming Fan 472320b8b0dSShuming Fan if (rt711 && rt711->hw_init) { 473320b8b0dSShuming Fan cancel_delayed_work(&rt711->jack_detect_work); 474320b8b0dSShuming Fan cancel_delayed_work(&rt711->jack_btn_check_work); 475320b8b0dSShuming Fan cancel_work_sync(&rt711->calibration_work); 476320b8b0dSShuming Fan } 477320b8b0dSShuming Fan 478320b8b0dSShuming Fan return 0; 479320b8b0dSShuming Fan } 480320b8b0dSShuming Fan 481320b8b0dSShuming Fan static const struct sdw_device_id rt711_id[] = { 482320b8b0dSShuming Fan SDW_SLAVE_ENTRY(0x025d, 0x711, 0), 483320b8b0dSShuming Fan {}, 484320b8b0dSShuming Fan }; 485320b8b0dSShuming Fan MODULE_DEVICE_TABLE(sdw, rt711_id); 486320b8b0dSShuming Fan 487973649d3STakashi Iwai static int __maybe_unused rt711_dev_suspend(struct device *dev) 488320b8b0dSShuming Fan { 489320b8b0dSShuming Fan struct rt711_priv *rt711 = dev_get_drvdata(dev); 490320b8b0dSShuming Fan 491320b8b0dSShuming Fan if (!rt711->hw_init) 492320b8b0dSShuming Fan return 0; 493320b8b0dSShuming Fan 494501ef013SShuming Fan cancel_delayed_work_sync(&rt711->jack_detect_work); 495501ef013SShuming Fan cancel_delayed_work_sync(&rt711->jack_btn_check_work); 496501ef013SShuming Fan cancel_work_sync(&rt711->calibration_work); 497501ef013SShuming Fan 498320b8b0dSShuming Fan regcache_cache_only(rt711->regmap, true); 499320b8b0dSShuming Fan 500320b8b0dSShuming Fan return 0; 501320b8b0dSShuming Fan } 502320b8b0dSShuming Fan 503320b8b0dSShuming Fan #define RT711_PROBE_TIMEOUT 2000 504320b8b0dSShuming Fan 505973649d3STakashi Iwai static int __maybe_unused rt711_dev_resume(struct device *dev) 506320b8b0dSShuming Fan { 507320b8b0dSShuming Fan struct sdw_slave *slave = dev_to_sdw_dev(dev); 508320b8b0dSShuming Fan struct rt711_priv *rt711 = dev_get_drvdata(dev); 509320b8b0dSShuming Fan unsigned long time; 510320b8b0dSShuming Fan 511320b8b0dSShuming Fan if (!rt711->hw_init) 512320b8b0dSShuming Fan return 0; 513320b8b0dSShuming Fan 514320b8b0dSShuming Fan if (!slave->unattach_request) 515320b8b0dSShuming Fan goto regmap_sync; 516320b8b0dSShuming Fan 517320b8b0dSShuming Fan time = wait_for_completion_timeout(&slave->initialization_complete, 518320b8b0dSShuming Fan msecs_to_jiffies(RT711_PROBE_TIMEOUT)); 519320b8b0dSShuming Fan if (!time) { 520320b8b0dSShuming Fan dev_err(&slave->dev, "Initialization not complete, timed out\n"); 521320b8b0dSShuming Fan return -ETIMEDOUT; 522320b8b0dSShuming Fan } 523320b8b0dSShuming Fan 524320b8b0dSShuming Fan regmap_sync: 525320b8b0dSShuming Fan slave->unattach_request = 0; 526320b8b0dSShuming Fan regcache_cache_only(rt711->regmap, false); 527320b8b0dSShuming Fan regcache_sync_region(rt711->regmap, 0x3000, 0x8fff); 528320b8b0dSShuming Fan regcache_sync_region(rt711->regmap, 0x752009, 0x752091); 529320b8b0dSShuming Fan 530320b8b0dSShuming Fan return 0; 531320b8b0dSShuming Fan } 532320b8b0dSShuming Fan 533320b8b0dSShuming Fan static const struct dev_pm_ops rt711_pm = { 534320b8b0dSShuming Fan SET_SYSTEM_SLEEP_PM_OPS(rt711_dev_suspend, rt711_dev_resume) 535320b8b0dSShuming Fan SET_RUNTIME_PM_OPS(rt711_dev_suspend, rt711_dev_resume, NULL) 536320b8b0dSShuming Fan }; 537320b8b0dSShuming Fan 538320b8b0dSShuming Fan static struct sdw_driver rt711_sdw_driver = { 539320b8b0dSShuming Fan .driver = { 540320b8b0dSShuming Fan .name = "rt711", 541320b8b0dSShuming Fan .owner = THIS_MODULE, 542320b8b0dSShuming Fan .pm = &rt711_pm, 543320b8b0dSShuming Fan }, 544320b8b0dSShuming Fan .probe = rt711_sdw_probe, 545320b8b0dSShuming Fan .remove = rt711_sdw_remove, 546320b8b0dSShuming Fan .ops = &rt711_slave_ops, 547320b8b0dSShuming Fan .id_table = rt711_id, 548320b8b0dSShuming Fan }; 549320b8b0dSShuming Fan module_sdw_driver(rt711_sdw_driver); 550320b8b0dSShuming Fan 551320b8b0dSShuming Fan MODULE_DESCRIPTION("ASoC RT711 SDW driver"); 552320b8b0dSShuming Fan MODULE_AUTHOR("Shuming Fan <shumingf@realtek.com>"); 553320b8b0dSShuming Fan MODULE_LICENSE("GPL"); 554