1321936Shselasky// SPDX-License-Identifier: GPL-2.0-only 2321936Shselasky// 3321936Shselasky// rt715-sdca-sdw.c -- rt715 ALSA SoC audio driver 4321936Shselasky// 5321936Shselasky// Copyright(c) 2020 Realtek Semiconductor Corp. 6321936Shselasky// 7321936Shselasky// 8321936Shselasky 9321936Shselasky#include <linux/delay.h> 10321936Shselasky#include <linux/device.h> 11321936Shselasky#include <linux/mod_devicetable.h> 12321936Shselasky#include <linux/soundwire/sdw.h> 13321936Shselasky#include <linux/soundwire/sdw_type.h> 14321936Shselasky#include <linux/soundwire/sdw_registers.h> 15321936Shselasky#include <linux/module.h> 16321936Shselasky#include <linux/pm_runtime.h> 17321936Shselasky#include <linux/regmap.h> 18321936Shselasky#include <sound/soc.h> 19321936Shselasky#include "rt715-sdca.h" 20321936Shselasky#include "rt715-sdca-sdw.h" 21321936Shselasky 22321936Shselaskystatic bool rt715_sdca_readable_register(struct device *dev, unsigned int reg) 23321936Shselasky{ 24321936Shselasky switch (reg) { 25321936Shselasky case 0x201a ... 0x2027: 26321936Shselasky case 0x2029 ... 0x202a: 27321936Shselasky case 0x202d ... 0x2034: 28321936Shselasky case 0x2200 ... 0x2204: 29321936Shselasky case 0x2206 ... 0x2212: 30321936Shselasky case 0x2230 ... 0x2239: 31321936Shselasky case 0x2f5b: 32321936Shselasky case SDW_SDCA_CTL(FUN_MIC_ARRAY, RT715_SDCA_SMPU_TRIG_ST_EN, 33321936Shselasky RT715_SDCA_SMPU_TRIG_ST_CTRL, CH_00): 34321936Shselasky return true; 35321936Shselasky default: 36321936Shselasky return false; 37321936Shselasky } 38321936Shselasky} 39321936Shselasky 40321936Shselaskystatic bool rt715_sdca_volatile_register(struct device *dev, unsigned int reg) 41321936Shselasky{ 42321936Shselasky switch (reg) { 43321936Shselasky case 0x201b: 44321936Shselasky case 0x201c: 45321936Shselasky case 0x201d: 46321936Shselasky case 0x201f: 47321936Shselasky case 0x2021: 48321936Shselasky case 0x2023: 49321936Shselasky case 0x2230: 50321936Shselasky case 0x202d ... 0x202f: /* BRA */ 51321936Shselasky case 0x2200 ... 0x2212: /* i2c debug */ 52321936Shselasky case 0x2f07: 53321936Shselasky case 0x2f1b ... 0x2f1e: 54321936Shselasky case 0x2f30 ... 0x2f34: 55321936Shselasky case 0x2f50 ... 0x2f51: 56321936Shselasky case 0x2f53 ... 0x2f59: 57321936Shselasky case 0x2f5c ... 0x2f5f: 58321936Shselasky case SDW_SDCA_CTL(FUN_MIC_ARRAY, RT715_SDCA_SMPU_TRIG_ST_EN, 59321936Shselasky RT715_SDCA_SMPU_TRIG_ST_CTRL, CH_00): /* VAD Searching status */ 60321936Shselasky return true; 61321936Shselasky default: 62321936Shselasky return false; 63321936Shselasky } 64321936Shselasky} 65321936Shselasky 66321936Shselaskystatic bool rt715_sdca_mbq_readable_register(struct device *dev, unsigned int reg) 67321936Shselasky{ 68321936Shselasky switch (reg) { 69321936Shselasky case 0x2000000: 70321936Shselasky case 0x200002b: 71321936Shselasky case 0x2000036: 72321936Shselasky case 0x2000037: 73321936Shselasky case 0x2000039: 74321936Shselasky case 0x2000044: 75321936Shselasky case 0x6100000: 76321936Shselasky return true; 77321936Shselasky default: 78321936Shselasky return false; 79321936Shselasky } 80321936Shselasky} 81321936Shselasky 82321936Shselaskystatic bool rt715_sdca_mbq_volatile_register(struct device *dev, unsigned int reg) 83321936Shselasky{ 84321936Shselasky switch (reg) { 85321936Shselasky case 0x2000000: 86321936Shselasky return true; 87321936Shselasky default: 88321936Shselasky return false; 89321936Shselasky } 90321936Shselasky} 91321936Shselasky 92321936Shselaskystatic const struct regmap_config rt715_sdca_regmap = { 93321936Shselasky .reg_bits = 32, 94321936Shselasky .val_bits = 8, 95321936Shselasky .readable_reg = rt715_sdca_readable_register, 96321936Shselasky .volatile_reg = rt715_sdca_volatile_register, 97321936Shselasky .max_register = 0x43ffffff, 98321936Shselasky .reg_defaults = rt715_reg_defaults_sdca, 99321936Shselasky .num_reg_defaults = ARRAY_SIZE(rt715_reg_defaults_sdca), 100321936Shselasky .cache_type = REGCACHE_MAPLE, 101321936Shselasky .use_single_read = true, 102321936Shselasky .use_single_write = true, 103321936Shselasky}; 104321936Shselasky 105321936Shselaskystatic const struct regmap_config rt715_sdca_mbq_regmap = { 106321936Shselasky .name = "sdw-mbq", 107321936Shselasky .reg_bits = 32, 108321936Shselasky .val_bits = 16, 109321936Shselasky .readable_reg = rt715_sdca_mbq_readable_register, 110321936Shselasky .volatile_reg = rt715_sdca_mbq_volatile_register, 111321936Shselasky .max_register = 0x43ffffff, 112321936Shselasky .reg_defaults = rt715_mbq_reg_defaults_sdca, 113321936Shselasky .num_reg_defaults = ARRAY_SIZE(rt715_mbq_reg_defaults_sdca), 114321936Shselasky .cache_type = REGCACHE_MAPLE, 115321936Shselasky .use_single_read = true, 116321936Shselasky .use_single_write = true, 117321936Shselasky}; 118321936Shselasky 119321936Shselaskystatic int rt715_sdca_update_status(struct sdw_slave *slave, 120321936Shselasky enum sdw_slave_status status) 121321936Shselasky{ 122321936Shselasky struct rt715_sdca_priv *rt715 = dev_get_drvdata(&slave->dev); 123321936Shselasky 124321936Shselasky /* 125321936Shselasky * Perform initialization only if slave status is present and 126321936Shselasky * hw_init flag is false 127321936Shselasky */ 128321936Shselasky if (rt715->hw_init || status != SDW_SLAVE_ATTACHED) 129321936Shselasky return 0; 130321936Shselasky 131321936Shselasky /* perform I/O transfers required for Slave initialization */ 132321936Shselasky return rt715_sdca_io_init(&slave->dev, slave); 133321936Shselasky} 134321936Shselasky 135321936Shselaskystatic int rt715_sdca_read_prop(struct sdw_slave *slave) 136321936Shselasky{ 137321936Shselasky struct sdw_slave_prop *prop = &slave->prop; 138321936Shselasky int nval, i; 139321936Shselasky u32 bit; 140321936Shselasky unsigned long addr; 141321936Shselasky struct sdw_dpn_prop *dpn; 142321936Shselasky 143321936Shselasky prop->paging_support = true; 144321936Shselasky 145321936Shselasky /* first we need to allocate memory for set bits in port lists */ 146321936Shselasky prop->source_ports = 0x50;/* BITMAP: 01010000 */ 147321936Shselasky prop->sink_ports = 0x0; /* BITMAP: 00000000 */ 148321936Shselasky 149321936Shselasky nval = hweight32(prop->source_ports); 150321936Shselasky prop->src_dpn_prop = devm_kcalloc(&slave->dev, nval, 151321936Shselasky sizeof(*prop->src_dpn_prop), 152321936Shselasky GFP_KERNEL); 153321936Shselasky if (!prop->src_dpn_prop) 154321936Shselasky return -ENOMEM; 155321936Shselasky 156321936Shselasky dpn = prop->src_dpn_prop; 157321936Shselasky i = 0; 158321936Shselasky addr = prop->source_ports; 159321936Shselasky for_each_set_bit(bit, &addr, 32) { 160321936Shselasky dpn[i].num = bit; 161321936Shselasky dpn[i].simple_ch_prep_sm = true; 162321936Shselasky dpn[i].ch_prep_timeout = 10; 163321936Shselasky i++; 164321936Shselasky } 165321936Shselasky 166321936Shselasky /* set the timeout values */ 167321936Shselasky prop->clk_stop_timeout = 200; 168321936Shselasky 169321936Shselasky return 0; 170321936Shselasky} 171321936Shselasky 172321936Shselaskystatic const struct sdw_slave_ops rt715_sdca_slave_ops = { 173321936Shselasky .read_prop = rt715_sdca_read_prop, 174321936Shselasky .update_status = rt715_sdca_update_status, 175321936Shselasky}; 176321936Shselasky 177321936Shselaskystatic int rt715_sdca_sdw_probe(struct sdw_slave *slave, 178321936Shselasky const struct sdw_device_id *id) 179{ 180 struct regmap *mbq_regmap, *regmap; 181 182 /* Regmap Initialization */ 183 mbq_regmap = devm_regmap_init_sdw_mbq(slave, &rt715_sdca_mbq_regmap); 184 if (IS_ERR(mbq_regmap)) 185 return PTR_ERR(mbq_regmap); 186 187 regmap = devm_regmap_init_sdw(slave, &rt715_sdca_regmap); 188 if (IS_ERR(regmap)) 189 return PTR_ERR(regmap); 190 191 return rt715_sdca_init(&slave->dev, mbq_regmap, regmap, slave); 192} 193 194static int rt715_sdca_sdw_remove(struct sdw_slave *slave) 195{ 196 pm_runtime_disable(&slave->dev); 197 198 return 0; 199} 200 201static const struct sdw_device_id rt715_sdca_id[] = { 202 SDW_SLAVE_ENTRY_EXT(0x025d, 0x715, 0x3, 0x1, 0), 203 SDW_SLAVE_ENTRY_EXT(0x025d, 0x714, 0x3, 0x1, 0), 204 {}, 205}; 206MODULE_DEVICE_TABLE(sdw, rt715_sdca_id); 207 208static int __maybe_unused rt715_dev_suspend(struct device *dev) 209{ 210 struct rt715_sdca_priv *rt715 = dev_get_drvdata(dev); 211 212 if (!rt715->hw_init) 213 return 0; 214 215 regcache_cache_only(rt715->regmap, true); 216 regcache_mark_dirty(rt715->regmap); 217 regcache_cache_only(rt715->mbq_regmap, true); 218 regcache_mark_dirty(rt715->mbq_regmap); 219 220 return 0; 221} 222 223#define RT715_PROBE_TIMEOUT 5000 224 225static int __maybe_unused rt715_dev_resume(struct device *dev) 226{ 227 struct sdw_slave *slave = dev_to_sdw_dev(dev); 228 struct rt715_sdca_priv *rt715 = dev_get_drvdata(dev); 229 unsigned long time; 230 231 if (!rt715->first_hw_init) 232 return 0; 233 234 if (!slave->unattach_request) 235 goto regmap_sync; 236 237 time = wait_for_completion_timeout(&slave->enumeration_complete, 238 msecs_to_jiffies(RT715_PROBE_TIMEOUT)); 239 if (!time) { 240 dev_err(&slave->dev, "%s: Enumeration not complete, timed out\n", __func__); 241 sdw_show_ping_status(slave->bus, true); 242 243 return -ETIMEDOUT; 244 } 245 246regmap_sync: 247 slave->unattach_request = 0; 248 regcache_cache_only(rt715->regmap, false); 249 regcache_sync_region(rt715->regmap, 250 SDW_SDCA_CTL(FUN_JACK_CODEC, RT715_SDCA_ST_EN, RT715_SDCA_ST_CTRL, 251 CH_00), 252 SDW_SDCA_CTL(FUN_MIC_ARRAY, RT715_SDCA_SMPU_TRIG_ST_EN, 253 RT715_SDCA_SMPU_TRIG_ST_CTRL, CH_00)); 254 regcache_cache_only(rt715->mbq_regmap, false); 255 regcache_sync_region(rt715->mbq_regmap, 0x2000000, 0x61020ff); 256 regcache_sync_region(rt715->mbq_regmap, 257 SDW_SDCA_CTL(FUN_JACK_CODEC, RT715_SDCA_ST_EN, RT715_SDCA_ST_CTRL, 258 CH_00), 259 SDW_SDCA_CTL(FUN_MIC_ARRAY, RT715_SDCA_SMPU_TRIG_ST_EN, 260 RT715_SDCA_SMPU_TRIG_ST_CTRL, CH_00)); 261 262 return 0; 263} 264 265static const struct dev_pm_ops rt715_pm = { 266 SET_SYSTEM_SLEEP_PM_OPS(rt715_dev_suspend, rt715_dev_resume) 267 SET_RUNTIME_PM_OPS(rt715_dev_suspend, rt715_dev_resume, NULL) 268}; 269 270static struct sdw_driver rt715_sdw_driver = { 271 .driver = { 272 .name = "rt715-sdca", 273 .owner = THIS_MODULE, 274 .pm = &rt715_pm, 275 }, 276 .probe = rt715_sdca_sdw_probe, 277 .remove = rt715_sdca_sdw_remove, 278 .ops = &rt715_sdca_slave_ops, 279 .id_table = rt715_sdca_id, 280}; 281module_sdw_driver(rt715_sdw_driver); 282 283MODULE_DESCRIPTION("ASoC RT715 driver SDW SDCA"); 284MODULE_AUTHOR("Jack Yu <jack.yu@realtek.com>"); 285MODULE_LICENSE("GPL v2"); 286