xref: /linux/sound/soc/codecs/rt715-sdca-sdw.c (revision dec1c62e91ba268ab2a6e339d4d7a59287d5eba1)
1 // SPDX-License-Identifier: GPL-2.0-only
2 //
3 // rt715-sdca-sdw.c -- rt715 ALSA SoC audio driver
4 //
5 // Copyright(c) 2020 Realtek Semiconductor Corp.
6 //
7 //
8 
9 #include <linux/delay.h>
10 #include <linux/device.h>
11 #include <linux/mod_devicetable.h>
12 #include <linux/soundwire/sdw.h>
13 #include <linux/soundwire/sdw_type.h>
14 #include <linux/soundwire/sdw_registers.h>
15 #include <linux/module.h>
16 #include <linux/regmap.h>
17 #include <sound/soc.h>
18 #include "rt715-sdca.h"
19 #include "rt715-sdca-sdw.h"
20 
21 static bool rt715_sdca_readable_register(struct device *dev, unsigned int reg)
22 {
23 	switch (reg) {
24 	case 0x201a ... 0x2027:
25 	case 0x2029 ... 0x202a:
26 	case 0x202d ... 0x2034:
27 	case 0x2200 ... 0x2204:
28 	case 0x2206 ... 0x2212:
29 	case 0x2230 ... 0x2239:
30 	case 0x2f5b:
31 	case SDW_SDCA_CTL(FUN_MIC_ARRAY, RT715_SDCA_SMPU_TRIG_ST_EN,
32 		RT715_SDCA_SMPU_TRIG_ST_CTRL, CH_00):
33 		return true;
34 	default:
35 		return false;
36 	}
37 }
38 
39 static bool rt715_sdca_volatile_register(struct device *dev, unsigned int reg)
40 {
41 	switch (reg) {
42 	case 0x201b:
43 	case 0x201c:
44 	case 0x201d:
45 	case 0x201f:
46 	case 0x2021:
47 	case 0x2023:
48 	case 0x2230:
49 	case 0x202d ... 0x202f: /* BRA */
50 	case 0x2200 ... 0x2212: /* i2c debug */
51 	case 0x2f07:
52 	case 0x2f1b ... 0x2f1e:
53 	case 0x2f30 ... 0x2f34:
54 	case 0x2f50 ... 0x2f51:
55 	case 0x2f53 ... 0x2f59:
56 	case 0x2f5c ... 0x2f5f:
57 	case SDW_SDCA_CTL(FUN_MIC_ARRAY, RT715_SDCA_SMPU_TRIG_ST_EN,
58 		RT715_SDCA_SMPU_TRIG_ST_CTRL, CH_00): /* VAD Searching status */
59 		return true;
60 	default:
61 		return false;
62 	}
63 }
64 
65 static bool rt715_sdca_mbq_readable_register(struct device *dev, unsigned int reg)
66 {
67 	switch (reg) {
68 	case 0x2000000:
69 	case 0x200002b:
70 	case 0x2000036:
71 	case 0x2000037:
72 	case 0x2000039:
73 	case 0x2000044:
74 	case 0x6100000:
75 		return true;
76 	default:
77 		return false;
78 	}
79 }
80 
81 static bool rt715_sdca_mbq_volatile_register(struct device *dev, unsigned int reg)
82 {
83 	switch (reg) {
84 	case 0x2000000:
85 		return true;
86 	default:
87 		return false;
88 	}
89 }
90 
91 static const struct regmap_config rt715_sdca_regmap = {
92 	.reg_bits = 32,
93 	.val_bits = 8,
94 	.readable_reg = rt715_sdca_readable_register,
95 	.volatile_reg = rt715_sdca_volatile_register,
96 	.max_register = 0x43ffffff,
97 	.reg_defaults = rt715_reg_defaults_sdca,
98 	.num_reg_defaults = ARRAY_SIZE(rt715_reg_defaults_sdca),
99 	.cache_type = REGCACHE_RBTREE,
100 	.use_single_read = true,
101 	.use_single_write = true,
102 };
103 
104 static const struct regmap_config rt715_sdca_mbq_regmap = {
105 	.name = "sdw-mbq",
106 	.reg_bits = 32,
107 	.val_bits = 16,
108 	.readable_reg = rt715_sdca_mbq_readable_register,
109 	.volatile_reg = rt715_sdca_mbq_volatile_register,
110 	.max_register = 0x43ffffff,
111 	.reg_defaults = rt715_mbq_reg_defaults_sdca,
112 	.num_reg_defaults = ARRAY_SIZE(rt715_mbq_reg_defaults_sdca),
113 	.cache_type = REGCACHE_RBTREE,
114 	.use_single_read = true,
115 	.use_single_write = true,
116 };
117 
118 static int rt715_sdca_update_status(struct sdw_slave *slave,
119 				enum sdw_slave_status status)
120 {
121 	struct rt715_sdca_priv *rt715 = dev_get_drvdata(&slave->dev);
122 
123 	/* Update the status */
124 	rt715->status = status;
125 
126 	/*
127 	 * Perform initialization only if slave status is present and
128 	 * hw_init flag is false
129 	 */
130 	if (rt715->hw_init || rt715->status != SDW_SLAVE_ATTACHED)
131 		return 0;
132 
133 	/* perform I/O transfers required for Slave initialization */
134 	return rt715_sdca_io_init(&slave->dev, slave);
135 }
136 
137 static int rt715_sdca_read_prop(struct sdw_slave *slave)
138 {
139 	struct sdw_slave_prop *prop = &slave->prop;
140 	int nval, i;
141 	u32 bit;
142 	unsigned long addr;
143 	struct sdw_dpn_prop *dpn;
144 
145 	prop->paging_support = true;
146 
147 	/* first we need to allocate memory for set bits in port lists */
148 	prop->source_ports = 0x50;/* BITMAP: 01010000 */
149 	prop->sink_ports = 0x0;	/* BITMAP:  00000000 */
150 
151 	nval = hweight32(prop->source_ports);
152 	prop->src_dpn_prop = devm_kcalloc(&slave->dev, nval,
153 					sizeof(*prop->src_dpn_prop),
154 					GFP_KERNEL);
155 	if (!prop->src_dpn_prop)
156 		return -ENOMEM;
157 
158 	dpn = prop->src_dpn_prop;
159 	i = 0;
160 	addr = prop->source_ports;
161 	for_each_set_bit(bit, &addr, 32) {
162 		dpn[i].num = bit;
163 		dpn[i].simple_ch_prep_sm = true;
164 		dpn[i].ch_prep_timeout = 10;
165 		i++;
166 	}
167 
168 	/* set the timeout values */
169 	prop->clk_stop_timeout = 20;
170 
171 	return 0;
172 }
173 
174 static struct sdw_slave_ops rt715_sdca_slave_ops = {
175 	.read_prop = rt715_sdca_read_prop,
176 	.update_status = rt715_sdca_update_status,
177 };
178 
179 static int rt715_sdca_sdw_probe(struct sdw_slave *slave,
180 			   const struct sdw_device_id *id)
181 {
182 	struct regmap *mbq_regmap, *regmap;
183 
184 	/* Regmap Initialization */
185 	mbq_regmap = devm_regmap_init_sdw_mbq(slave, &rt715_sdca_mbq_regmap);
186 	if (IS_ERR(mbq_regmap))
187 		return PTR_ERR(mbq_regmap);
188 
189 	regmap = devm_regmap_init_sdw(slave, &rt715_sdca_regmap);
190 	if (IS_ERR(regmap))
191 		return PTR_ERR(regmap);
192 
193 	return rt715_sdca_init(&slave->dev, mbq_regmap, regmap, slave);
194 }
195 
196 static const struct sdw_device_id rt715_sdca_id[] = {
197 	SDW_SLAVE_ENTRY_EXT(0x025d, 0x715, 0x3, 0x1, 0),
198 	SDW_SLAVE_ENTRY_EXT(0x025d, 0x714, 0x3, 0x1, 0),
199 	{},
200 };
201 MODULE_DEVICE_TABLE(sdw, rt715_sdca_id);
202 
203 static int __maybe_unused rt715_dev_suspend(struct device *dev)
204 {
205 	struct rt715_sdca_priv *rt715 = dev_get_drvdata(dev);
206 
207 	if (!rt715->hw_init)
208 		return 0;
209 
210 	regcache_cache_only(rt715->regmap, true);
211 	regcache_mark_dirty(rt715->regmap);
212 	regcache_cache_only(rt715->mbq_regmap, true);
213 	regcache_mark_dirty(rt715->mbq_regmap);
214 
215 	return 0;
216 }
217 
218 #define RT715_PROBE_TIMEOUT 5000
219 
220 static int __maybe_unused rt715_dev_resume(struct device *dev)
221 {
222 	struct sdw_slave *slave = dev_to_sdw_dev(dev);
223 	struct rt715_sdca_priv *rt715 = dev_get_drvdata(dev);
224 	unsigned long time;
225 
226 	if (!rt715->first_hw_init)
227 		return 0;
228 
229 	if (!slave->unattach_request)
230 		goto regmap_sync;
231 
232 	time = wait_for_completion_timeout(&slave->enumeration_complete,
233 					   msecs_to_jiffies(RT715_PROBE_TIMEOUT));
234 	if (!time) {
235 		dev_err(&slave->dev, "Enumeration not complete, timed out\n");
236 		return -ETIMEDOUT;
237 	}
238 
239 regmap_sync:
240 	slave->unattach_request = 0;
241 	regcache_cache_only(rt715->regmap, false);
242 	regcache_sync_region(rt715->regmap,
243 		SDW_SDCA_CTL(FUN_JACK_CODEC, RT715_SDCA_ST_EN, RT715_SDCA_ST_CTRL,
244 			CH_00),
245 		SDW_SDCA_CTL(FUN_MIC_ARRAY, RT715_SDCA_SMPU_TRIG_ST_EN,
246 			RT715_SDCA_SMPU_TRIG_ST_CTRL, CH_00));
247 	regcache_cache_only(rt715->mbq_regmap, false);
248 	regcache_sync_region(rt715->mbq_regmap, 0x2000000, 0x61020ff);
249 	regcache_sync_region(rt715->mbq_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 
255 	return 0;
256 }
257 
258 static const struct dev_pm_ops rt715_pm = {
259 	SET_SYSTEM_SLEEP_PM_OPS(rt715_dev_suspend, rt715_dev_resume)
260 	SET_RUNTIME_PM_OPS(rt715_dev_suspend, rt715_dev_resume, NULL)
261 };
262 
263 static struct sdw_driver rt715_sdw_driver = {
264 	.driver = {
265 		.name = "rt715-sdca",
266 		.owner = THIS_MODULE,
267 		.pm = &rt715_pm,
268 	},
269 	.probe = rt715_sdca_sdw_probe,
270 	.ops = &rt715_sdca_slave_ops,
271 	.id_table = rt715_sdca_id,
272 };
273 module_sdw_driver(rt715_sdw_driver);
274 
275 MODULE_DESCRIPTION("ASoC RT715 driver SDW SDCA");
276 MODULE_AUTHOR("Jack Yu <jack.yu@realtek.com>");
277 MODULE_LICENSE("GPL v2");
278