xref: /linux/sound/soc/codecs/rt715-sdca-sdw.c (revision 02680c23d7b3febe45ea3d4f9818c2b2dc89020a)
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 0x6100000:
74 		return true;
75 	default:
76 		return false;
77 	}
78 }
79 
80 static bool rt715_sdca_mbq_volatile_register(struct device *dev, unsigned int reg)
81 {
82 	switch (reg) {
83 	case 0x2000000:
84 		return true;
85 	default:
86 		return false;
87 	}
88 }
89 
90 static const struct regmap_config rt715_sdca_regmap = {
91 	.reg_bits = 32,
92 	.val_bits = 8,
93 	.readable_reg = rt715_sdca_readable_register,
94 	.volatile_reg = rt715_sdca_volatile_register,
95 	.max_register = 0x43ffffff,
96 	.reg_defaults = rt715_reg_defaults_sdca,
97 	.num_reg_defaults = ARRAY_SIZE(rt715_reg_defaults_sdca),
98 	.cache_type = REGCACHE_RBTREE,
99 	.use_single_read = true,
100 	.use_single_write = true,
101 };
102 
103 static const struct regmap_config rt715_sdca_mbq_regmap = {
104 	.name = "sdw-mbq",
105 	.reg_bits = 32,
106 	.val_bits = 16,
107 	.readable_reg = rt715_sdca_mbq_readable_register,
108 	.volatile_reg = rt715_sdca_mbq_volatile_register,
109 	.max_register = 0x43ffffff,
110 	.reg_defaults = rt715_mbq_reg_defaults_sdca,
111 	.num_reg_defaults = ARRAY_SIZE(rt715_mbq_reg_defaults_sdca),
112 	.cache_type = REGCACHE_RBTREE,
113 	.use_single_read = true,
114 	.use_single_write = true,
115 };
116 
117 static int rt715_sdca_update_status(struct sdw_slave *slave,
118 				enum sdw_slave_status status)
119 {
120 	struct rt715_sdca_priv *rt715 = dev_get_drvdata(&slave->dev);
121 
122 	/* Update the status */
123 	rt715->status = status;
124 
125 	/*
126 	 * Perform initialization only if slave status is present and
127 	 * hw_init flag is false
128 	 */
129 	if (rt715->hw_init || rt715->status != SDW_SLAVE_ATTACHED)
130 		return 0;
131 
132 	/* perform I/O transfers required for Slave initialization */
133 	return rt715_sdca_io_init(&slave->dev, slave);
134 }
135 
136 static int rt715_sdca_read_prop(struct sdw_slave *slave)
137 {
138 	struct sdw_slave_prop *prop = &slave->prop;
139 	int nval, i;
140 	u32 bit;
141 	unsigned long addr;
142 	struct sdw_dpn_prop *dpn;
143 
144 	prop->paging_support = true;
145 
146 	/* first we need to allocate memory for set bits in port lists */
147 	prop->source_ports = 0x50;/* BITMAP: 01010000 */
148 	prop->sink_ports = 0x0;	/* BITMAP:  00000000 */
149 
150 	nval = hweight32(prop->source_ports);
151 	prop->src_dpn_prop = devm_kcalloc(&slave->dev, nval,
152 					sizeof(*prop->src_dpn_prop),
153 					GFP_KERNEL);
154 	if (!prop->src_dpn_prop)
155 		return -ENOMEM;
156 
157 	dpn = prop->src_dpn_prop;
158 	i = 0;
159 	addr = prop->source_ports;
160 	for_each_set_bit(bit, &addr, 32) {
161 		dpn[i].num = bit;
162 		dpn[i].simple_ch_prep_sm = true;
163 		dpn[i].ch_prep_timeout = 10;
164 		i++;
165 	}
166 
167 	/* set the timeout values */
168 	prop->clk_stop_timeout = 20;
169 
170 	return 0;
171 }
172 
173 static struct sdw_slave_ops rt715_sdca_slave_ops = {
174 	.read_prop = rt715_sdca_read_prop,
175 	.update_status = rt715_sdca_update_status,
176 };
177 
178 static int rt715_sdca_sdw_probe(struct sdw_slave *slave,
179 			   const struct sdw_device_id *id)
180 {
181 	struct regmap *mbq_regmap, *regmap;
182 
183 	slave->ops = &rt715_sdca_slave_ops;
184 
185 	/* Regmap Initialization */
186 	mbq_regmap = devm_regmap_init_sdw_mbq(slave, &rt715_sdca_mbq_regmap);
187 	if (IS_ERR(mbq_regmap))
188 		return PTR_ERR(mbq_regmap);
189 
190 	regmap = devm_regmap_init_sdw(slave, &rt715_sdca_regmap);
191 	if (IS_ERR(regmap))
192 		return PTR_ERR(regmap);
193 
194 	return rt715_sdca_init(&slave->dev, mbq_regmap, regmap, slave);
195 }
196 
197 static const struct sdw_device_id rt715_sdca_id[] = {
198 	SDW_SLAVE_ENTRY_EXT(0x025d, 0x715, 0x3, 0x1, 0),
199 	SDW_SLAVE_ENTRY_EXT(0x025d, 0x714, 0x3, 0x1, 0),
200 	{},
201 };
202 MODULE_DEVICE_TABLE(sdw, rt715_sdca_id);
203 
204 static int __maybe_unused rt715_dev_suspend(struct device *dev)
205 {
206 	struct rt715_sdca_priv *rt715 = dev_get_drvdata(dev);
207 
208 	if (!rt715->hw_init)
209 		return 0;
210 
211 	regcache_cache_only(rt715->regmap, true);
212 	regcache_mark_dirty(rt715->regmap);
213 	regcache_cache_only(rt715->mbq_regmap, true);
214 	regcache_mark_dirty(rt715->mbq_regmap);
215 
216 	return 0;
217 }
218 
219 #define RT715_PROBE_TIMEOUT 5000
220 
221 static int __maybe_unused rt715_dev_resume(struct device *dev)
222 {
223 	struct sdw_slave *slave = dev_to_sdw_dev(dev);
224 	struct rt715_sdca_priv *rt715 = dev_get_drvdata(dev);
225 	unsigned long time;
226 
227 	if (!rt715->hw_init)
228 		return 0;
229 
230 	if (!slave->unattach_request)
231 		goto regmap_sync;
232 
233 	time = wait_for_completion_timeout(&slave->enumeration_complete,
234 					   msecs_to_jiffies(RT715_PROBE_TIMEOUT));
235 	if (!time) {
236 		dev_err(&slave->dev, "Enumeration not complete, timed out\n");
237 		return -ETIMEDOUT;
238 	}
239 
240 regmap_sync:
241 	slave->unattach_request = 0;
242 	regcache_cache_only(rt715->regmap, false);
243 	regcache_sync_region(rt715->regmap,
244 		SDW_SDCA_CTL(FUN_JACK_CODEC, RT715_SDCA_ST_EN, RT715_SDCA_ST_CTRL,
245 			CH_00),
246 		SDW_SDCA_CTL(FUN_MIC_ARRAY, RT715_SDCA_SMPU_TRIG_ST_EN,
247 			RT715_SDCA_SMPU_TRIG_ST_CTRL, CH_00));
248 	regcache_cache_only(rt715->mbq_regmap, false);
249 	regcache_sync_region(rt715->mbq_regmap, 0x2000000, 0x61020ff);
250 	regcache_sync_region(rt715->mbq_regmap,
251 		SDW_SDCA_CTL(FUN_JACK_CODEC, RT715_SDCA_ST_EN, RT715_SDCA_ST_CTRL,
252 			CH_00),
253 		SDW_SDCA_CTL(FUN_MIC_ARRAY, RT715_SDCA_SMPU_TRIG_ST_EN,
254 			RT715_SDCA_SMPU_TRIG_ST_CTRL, CH_00));
255 
256 	return 0;
257 }
258 
259 static const struct dev_pm_ops rt715_pm = {
260 	SET_SYSTEM_SLEEP_PM_OPS(rt715_dev_suspend, rt715_dev_resume)
261 	SET_RUNTIME_PM_OPS(rt715_dev_suspend, rt715_dev_resume, NULL)
262 };
263 
264 static struct sdw_driver rt715_sdw_driver = {
265 	.driver = {
266 		.name = "rt715-sdca",
267 		.owner = THIS_MODULE,
268 		.pm = &rt715_pm,
269 	},
270 	.probe = rt715_sdca_sdw_probe,
271 	.ops = &rt715_sdca_slave_ops,
272 	.id_table = rt715_sdca_id,
273 };
274 module_sdw_driver(rt715_sdw_driver);
275 
276 MODULE_DESCRIPTION("ASoC RT715 driver SDW SDCA");
277 MODULE_AUTHOR("Jack Yu <jack.yu@realtek.com>");
278 MODULE_LICENSE("GPL v2");
279