xref: /linux/sound/soc/codecs/rt1308-sdw.c (revision 9e4730586e0b3ca423e7147104790908a1adb1ba)
1a87a6653SShuming Fan // SPDX-License-Identifier: GPL-2.0
2a87a6653SShuming Fan //
3a87a6653SShuming Fan // rt1308-sdw.c -- rt1308 ALSA SoC audio driver
4a87a6653SShuming Fan //
5a87a6653SShuming Fan // Copyright(c) 2019 Realtek Semiconductor Corp.
6a87a6653SShuming Fan //
7a87a6653SShuming Fan //
8a87a6653SShuming Fan #include <linux/delay.h>
9a87a6653SShuming Fan #include <linux/device.h>
10a87a6653SShuming Fan #include <linux/pm_runtime.h>
11a87a6653SShuming Fan #include <linux/mod_devicetable.h>
12a87a6653SShuming Fan #include <linux/soundwire/sdw.h>
13a87a6653SShuming Fan #include <linux/soundwire/sdw_type.h>
14a87a6653SShuming Fan #include <linux/soundwire/sdw_registers.h>
15a87a6653SShuming Fan #include <linux/module.h>
16a87a6653SShuming Fan #include <linux/regmap.h>
17a87a6653SShuming Fan #include <sound/core.h>
18a87a6653SShuming Fan #include <sound/pcm.h>
19a87a6653SShuming Fan #include <sound/pcm_params.h>
20a87a6653SShuming Fan #include <sound/soc.h>
21a87a6653SShuming Fan #include <sound/soc-dapm.h>
22a87a6653SShuming Fan #include <sound/initval.h>
23a87a6653SShuming Fan 
24a87a6653SShuming Fan #include "rt1308.h"
25a87a6653SShuming Fan #include "rt1308-sdw.h"
26a87a6653SShuming Fan 
27a87a6653SShuming Fan static bool rt1308_readable_register(struct device *dev, unsigned int reg)
28a87a6653SShuming Fan {
29a87a6653SShuming Fan 	switch (reg) {
30a87a6653SShuming Fan 	case 0x00e0:
31a87a6653SShuming Fan 	case 0x00f0:
32a87a6653SShuming Fan 	case 0x2f01 ... 0x2f07:
33a87a6653SShuming Fan 	case 0x3000 ... 0x3001:
34a87a6653SShuming Fan 	case 0x3004 ... 0x3005:
35a87a6653SShuming Fan 	case 0x3008:
36a87a6653SShuming Fan 	case 0x300a:
37a87a6653SShuming Fan 	case 0xc000 ... 0xcff3:
38a87a6653SShuming Fan 		return true;
39a87a6653SShuming Fan 	default:
40a87a6653SShuming Fan 		return false;
41a87a6653SShuming Fan 	}
42a87a6653SShuming Fan }
43a87a6653SShuming Fan 
44a87a6653SShuming Fan static bool rt1308_volatile_register(struct device *dev, unsigned int reg)
45a87a6653SShuming Fan {
46a87a6653SShuming Fan 	switch (reg) {
47a87a6653SShuming Fan 	case 0x2f01 ... 0x2f07:
48a87a6653SShuming Fan 	case 0x3000 ... 0x3001:
49a87a6653SShuming Fan 	case 0x3004 ... 0x3005:
50a87a6653SShuming Fan 	case 0x3008:
51a87a6653SShuming Fan 	case 0x300a:
52a87a6653SShuming Fan 	case 0xc000:
53a87a6653SShuming Fan 		return true;
54a87a6653SShuming Fan 	default:
55a87a6653SShuming Fan 		return false;
56a87a6653SShuming Fan 	}
57a87a6653SShuming Fan }
58a87a6653SShuming Fan 
59a87a6653SShuming Fan static const struct regmap_config rt1308_sdw_regmap = {
60a87a6653SShuming Fan 	.reg_bits = 32,
61a87a6653SShuming Fan 	.val_bits = 8,
62a87a6653SShuming Fan 	.readable_reg = rt1308_readable_register,
63a87a6653SShuming Fan 	.volatile_reg = rt1308_volatile_register,
64a87a6653SShuming Fan 	.max_register = 0xcfff,
65a87a6653SShuming Fan 	.reg_defaults = rt1308_reg_defaults,
66a87a6653SShuming Fan 	.num_reg_defaults = ARRAY_SIZE(rt1308_reg_defaults),
67a87a6653SShuming Fan 	.cache_type = REGCACHE_RBTREE,
68a87a6653SShuming Fan 	.use_single_read = true,
69a87a6653SShuming Fan 	.use_single_write = true,
70a87a6653SShuming Fan };
71a87a6653SShuming Fan 
72a87a6653SShuming Fan /* Bus clock frequency */
73a87a6653SShuming Fan #define RT1308_CLK_FREQ_9600000HZ 9600000
74a87a6653SShuming Fan #define RT1308_CLK_FREQ_12000000HZ 12000000
75a87a6653SShuming Fan #define RT1308_CLK_FREQ_6000000HZ 6000000
76a87a6653SShuming Fan #define RT1308_CLK_FREQ_4800000HZ 4800000
77a87a6653SShuming Fan #define RT1308_CLK_FREQ_2400000HZ 2400000
78a87a6653SShuming Fan #define RT1308_CLK_FREQ_12288000HZ 12288000
79a87a6653SShuming Fan 
80a87a6653SShuming Fan static int rt1308_clock_config(struct device *dev)
81a87a6653SShuming Fan {
82a87a6653SShuming Fan 	struct rt1308_sdw_priv *rt1308 = dev_get_drvdata(dev);
83a87a6653SShuming Fan 	unsigned int clk_freq, value;
84a87a6653SShuming Fan 
85a87a6653SShuming Fan 	clk_freq = (rt1308->params.curr_dr_freq >> 1);
86a87a6653SShuming Fan 
87a87a6653SShuming Fan 	switch (clk_freq) {
88a87a6653SShuming Fan 	case RT1308_CLK_FREQ_12000000HZ:
89a87a6653SShuming Fan 		value = 0x0;
90a87a6653SShuming Fan 		break;
91a87a6653SShuming Fan 	case RT1308_CLK_FREQ_6000000HZ:
92a87a6653SShuming Fan 		value = 0x1;
93a87a6653SShuming Fan 		break;
94a87a6653SShuming Fan 	case RT1308_CLK_FREQ_9600000HZ:
95a87a6653SShuming Fan 		value = 0x2;
96a87a6653SShuming Fan 		break;
97a87a6653SShuming Fan 	case RT1308_CLK_FREQ_4800000HZ:
98a87a6653SShuming Fan 		value = 0x3;
99a87a6653SShuming Fan 		break;
100a87a6653SShuming Fan 	case RT1308_CLK_FREQ_2400000HZ:
101a87a6653SShuming Fan 		value = 0x4;
102a87a6653SShuming Fan 		break;
103a87a6653SShuming Fan 	case RT1308_CLK_FREQ_12288000HZ:
104a87a6653SShuming Fan 		value = 0x5;
105a87a6653SShuming Fan 		break;
106a87a6653SShuming Fan 	default:
107a87a6653SShuming Fan 		return -EINVAL;
108a87a6653SShuming Fan 	}
109a87a6653SShuming Fan 
110a87a6653SShuming Fan 	regmap_write(rt1308->regmap, 0xe0, value);
111a87a6653SShuming Fan 	regmap_write(rt1308->regmap, 0xf0, value);
112a87a6653SShuming Fan 
113a87a6653SShuming Fan 	dev_dbg(dev, "%s complete, clk_freq=%d\n", __func__, clk_freq);
114a87a6653SShuming Fan 
115a87a6653SShuming Fan 	return 0;
116a87a6653SShuming Fan }
117a87a6653SShuming Fan 
118a87a6653SShuming Fan static int rt1308_read_prop(struct sdw_slave *slave)
119a87a6653SShuming Fan {
120a87a6653SShuming Fan 	struct sdw_slave_prop *prop = &slave->prop;
121a87a6653SShuming Fan 	int nval, i, num_of_ports = 1;
122a87a6653SShuming Fan 	u32 bit;
123a87a6653SShuming Fan 	unsigned long addr;
124a87a6653SShuming Fan 	struct sdw_dpn_prop *dpn;
125a87a6653SShuming Fan 
126a87a6653SShuming Fan 	prop->paging_support = true;
127a87a6653SShuming Fan 
128a87a6653SShuming Fan 	/* first we need to allocate memory for set bits in port lists */
129a87a6653SShuming Fan 	prop->source_ports = 0x00; /* BITMAP: 00010100 (not enable yet) */
130a87a6653SShuming Fan 	prop->sink_ports = 0x2; /* BITMAP:  00000010 */
131a87a6653SShuming Fan 
132a87a6653SShuming Fan 	/* for sink */
133a87a6653SShuming Fan 	nval = hweight32(prop->sink_ports);
134a87a6653SShuming Fan 	num_of_ports += nval;
135a87a6653SShuming Fan 	prop->sink_dpn_prop = devm_kcalloc(&slave->dev, nval,
136a87a6653SShuming Fan 						sizeof(*prop->sink_dpn_prop),
137a87a6653SShuming Fan 						GFP_KERNEL);
138a87a6653SShuming Fan 	if (!prop->sink_dpn_prop)
139a87a6653SShuming Fan 		return -ENOMEM;
140a87a6653SShuming Fan 
141a87a6653SShuming Fan 	i = 0;
142a87a6653SShuming Fan 	dpn = prop->sink_dpn_prop;
143a87a6653SShuming Fan 	addr = prop->sink_ports;
144a87a6653SShuming Fan 	for_each_set_bit(bit, &addr, 32) {
145a87a6653SShuming Fan 		dpn[i].num = bit;
146a87a6653SShuming Fan 		dpn[i].type = SDW_DPN_FULL;
147a87a6653SShuming Fan 		dpn[i].simple_ch_prep_sm = true;
148a87a6653SShuming Fan 		dpn[i].ch_prep_timeout = 10;
149a87a6653SShuming Fan 		i++;
150a87a6653SShuming Fan 	}
151a87a6653SShuming Fan 
152a87a6653SShuming Fan 	/* Allocate port_ready based on num_of_ports */
153a87a6653SShuming Fan 	slave->port_ready = devm_kcalloc(&slave->dev, num_of_ports,
154a87a6653SShuming Fan 					sizeof(*slave->port_ready),
155a87a6653SShuming Fan 					GFP_KERNEL);
156a87a6653SShuming Fan 	if (!slave->port_ready)
157a87a6653SShuming Fan 		return -ENOMEM;
158a87a6653SShuming Fan 
159a87a6653SShuming Fan 	/* Initialize completion */
160a87a6653SShuming Fan 	for (i = 0; i < num_of_ports; i++)
161a87a6653SShuming Fan 		init_completion(&slave->port_ready[i]);
162a87a6653SShuming Fan 
163a87a6653SShuming Fan 	/* set the timeout values */
164a87a6653SShuming Fan 	prop->clk_stop_timeout = 20;
165a87a6653SShuming Fan 
166a87a6653SShuming Fan 	dev_dbg(&slave->dev, "%s\n", __func__);
167a87a6653SShuming Fan 
168a87a6653SShuming Fan 	return 0;
169a87a6653SShuming Fan }
170a87a6653SShuming Fan 
171a87a6653SShuming Fan static int rt1308_io_init(struct device *dev, struct sdw_slave *slave)
172a87a6653SShuming Fan {
173a87a6653SShuming Fan 	struct rt1308_sdw_priv *rt1308 = dev_get_drvdata(dev);
174a87a6653SShuming Fan 	int ret = 0;
175a87a6653SShuming Fan 	unsigned int efuse_m_btl_l, efuse_m_btl_r, tmp;
176a87a6653SShuming Fan 	unsigned int efuse_c_btl_l, efuse_c_btl_r;
177a87a6653SShuming Fan 
178a87a6653SShuming Fan 	if (rt1308->hw_init)
179a87a6653SShuming Fan 		return 0;
180a87a6653SShuming Fan 
181a87a6653SShuming Fan 	if (rt1308->first_hw_init) {
182a87a6653SShuming Fan 		regcache_cache_only(rt1308->regmap, false);
183a87a6653SShuming Fan 		regcache_cache_bypass(rt1308->regmap, true);
184a87a6653SShuming Fan 	}
185a87a6653SShuming Fan 
186a87a6653SShuming Fan 	/*
187a87a6653SShuming Fan 	 * PM runtime is only enabled when a Slave reports as Attached
188a87a6653SShuming Fan 	 */
189a87a6653SShuming Fan 	if (!rt1308->first_hw_init) {
190a87a6653SShuming Fan 		/* set autosuspend parameters */
191a87a6653SShuming Fan 		pm_runtime_set_autosuspend_delay(&slave->dev, 3000);
192a87a6653SShuming Fan 		pm_runtime_use_autosuspend(&slave->dev);
193a87a6653SShuming Fan 
194a87a6653SShuming Fan 		/* update count of parent 'active' children */
195a87a6653SShuming Fan 		pm_runtime_set_active(&slave->dev);
196a87a6653SShuming Fan 
197a87a6653SShuming Fan 		/* make sure the device does not suspend immediately */
198a87a6653SShuming Fan 		pm_runtime_mark_last_busy(&slave->dev);
199a87a6653SShuming Fan 
200a87a6653SShuming Fan 		pm_runtime_enable(&slave->dev);
201a87a6653SShuming Fan 	}
202a87a6653SShuming Fan 
203a87a6653SShuming Fan 	pm_runtime_get_noresume(&slave->dev);
204a87a6653SShuming Fan 
205a87a6653SShuming Fan 	/* sw reset */
206a87a6653SShuming Fan 	regmap_write(rt1308->regmap, RT1308_SDW_RESET, 0);
207a87a6653SShuming Fan 
208a87a6653SShuming Fan 	/* read efuse */
209a87a6653SShuming Fan 	regmap_write(rt1308->regmap, 0xc360, 0x01);
210a87a6653SShuming Fan 	regmap_write(rt1308->regmap, 0xc361, 0x80);
211a87a6653SShuming Fan 	regmap_write(rt1308->regmap, 0xc7f0, 0x04);
212a87a6653SShuming Fan 	regmap_write(rt1308->regmap, 0xc7f1, 0xfe);
213a87a6653SShuming Fan 	msleep(100);
214a87a6653SShuming Fan 	regmap_write(rt1308->regmap, 0xc7f0, 0x44);
215a87a6653SShuming Fan 	msleep(20);
216a87a6653SShuming Fan 	regmap_write(rt1308->regmap, 0xc240, 0x10);
217a87a6653SShuming Fan 
218a87a6653SShuming Fan 	regmap_read(rt1308->regmap, 0xc861, &tmp);
219a87a6653SShuming Fan 	efuse_m_btl_l = tmp;
220a87a6653SShuming Fan 	regmap_read(rt1308->regmap, 0xc860, &tmp);
221a87a6653SShuming Fan 	efuse_m_btl_l = efuse_m_btl_l | (tmp << 8);
222a87a6653SShuming Fan 	regmap_read(rt1308->regmap, 0xc863, &tmp);
223a87a6653SShuming Fan 	efuse_c_btl_l = tmp;
224a87a6653SShuming Fan 	regmap_read(rt1308->regmap, 0xc862, &tmp);
225a87a6653SShuming Fan 	efuse_c_btl_l = efuse_c_btl_l | (tmp << 8);
226a87a6653SShuming Fan 	regmap_read(rt1308->regmap, 0xc871, &tmp);
227a87a6653SShuming Fan 	efuse_m_btl_r = tmp;
228a87a6653SShuming Fan 	regmap_read(rt1308->regmap, 0xc870, &tmp);
229a87a6653SShuming Fan 	efuse_m_btl_r = efuse_m_btl_r | (tmp << 8);
230a87a6653SShuming Fan 	regmap_read(rt1308->regmap, 0xc873, &tmp);
231a87a6653SShuming Fan 	efuse_c_btl_r = tmp;
232a87a6653SShuming Fan 	regmap_read(rt1308->regmap, 0xc872, &tmp);
233a87a6653SShuming Fan 	efuse_c_btl_r = efuse_c_btl_r | (tmp << 8);
234110f44c2SPierre-Louis Bossart 	dev_dbg(&slave->dev, "%s m_btl_l=0x%x, m_btl_r=0x%x\n", __func__,
235a87a6653SShuming Fan 		efuse_m_btl_l, efuse_m_btl_r);
236110f44c2SPierre-Louis Bossart 	dev_dbg(&slave->dev, "%s c_btl_l=0x%x, c_btl_r=0x%x\n", __func__,
237a87a6653SShuming Fan 		efuse_c_btl_l, efuse_c_btl_r);
238a87a6653SShuming Fan 
239a87a6653SShuming Fan 	/* initial settings */
240a87a6653SShuming Fan 	regmap_write(rt1308->regmap, 0xc103, 0xc0);
241a87a6653SShuming Fan 	regmap_write(rt1308->regmap, 0xc030, 0x17);
242a87a6653SShuming Fan 	regmap_write(rt1308->regmap, 0xc031, 0x81);
243a87a6653SShuming Fan 	regmap_write(rt1308->regmap, 0xc032, 0x26);
244a87a6653SShuming Fan 	regmap_write(rt1308->regmap, 0xc040, 0x80);
245a87a6653SShuming Fan 	regmap_write(rt1308->regmap, 0xc041, 0x80);
246a87a6653SShuming Fan 	regmap_write(rt1308->regmap, 0xc042, 0x06);
247a87a6653SShuming Fan 	regmap_write(rt1308->regmap, 0xc052, 0x0a);
248a87a6653SShuming Fan 	regmap_write(rt1308->regmap, 0xc080, 0x0a);
249a87a6653SShuming Fan 	regmap_write(rt1308->regmap, 0xc060, 0x02);
250a87a6653SShuming Fan 	regmap_write(rt1308->regmap, 0xc061, 0x75);
251a87a6653SShuming Fan 	regmap_write(rt1308->regmap, 0xc062, 0x05);
252a87a6653SShuming Fan 	regmap_write(rt1308->regmap, 0xc171, 0x07);
253a87a6653SShuming Fan 	regmap_write(rt1308->regmap, 0xc173, 0x0d);
254a87a6653SShuming Fan 	regmap_write(rt1308->regmap, 0xc311, 0x7f);
255a87a6653SShuming Fan 	regmap_write(rt1308->regmap, 0xc900, 0x90);
256a87a6653SShuming Fan 	regmap_write(rt1308->regmap, 0xc1a0, 0x84);
257a87a6653SShuming Fan 	regmap_write(rt1308->regmap, 0xc1a1, 0x01);
258a87a6653SShuming Fan 	regmap_write(rt1308->regmap, 0xc360, 0x78);
259a87a6653SShuming Fan 	regmap_write(rt1308->regmap, 0xc361, 0x87);
260a87a6653SShuming Fan 	regmap_write(rt1308->regmap, 0xc0a1, 0x71);
261a87a6653SShuming Fan 	regmap_write(rt1308->regmap, 0xc210, 0x00);
262a87a6653SShuming Fan 	regmap_write(rt1308->regmap, 0xc070, 0x00);
263a87a6653SShuming Fan 	regmap_write(rt1308->regmap, 0xc100, 0xd7);
264a87a6653SShuming Fan 	regmap_write(rt1308->regmap, 0xc101, 0xd7);
265a87a6653SShuming Fan 	regmap_write(rt1308->regmap, 0xc300, 0x09);
266a87a6653SShuming Fan 
267a87a6653SShuming Fan 	if (rt1308->first_hw_init) {
268a87a6653SShuming Fan 		regcache_cache_bypass(rt1308->regmap, false);
269a87a6653SShuming Fan 		regcache_mark_dirty(rt1308->regmap);
270a87a6653SShuming Fan 	} else
271a87a6653SShuming Fan 		rt1308->first_hw_init = true;
272a87a6653SShuming Fan 
273a87a6653SShuming Fan 	/* Mark Slave initialization complete */
274a87a6653SShuming Fan 	rt1308->hw_init = true;
275a87a6653SShuming Fan 
276a87a6653SShuming Fan 	pm_runtime_mark_last_busy(&slave->dev);
277a87a6653SShuming Fan 	pm_runtime_put_autosuspend(&slave->dev);
278a87a6653SShuming Fan 
279a87a6653SShuming Fan 	dev_dbg(&slave->dev, "%s hw_init complete\n", __func__);
280a87a6653SShuming Fan 
281a87a6653SShuming Fan 	return ret;
282a87a6653SShuming Fan }
283a87a6653SShuming Fan 
284a87a6653SShuming Fan static int rt1308_update_status(struct sdw_slave *slave,
285a87a6653SShuming Fan 					enum sdw_slave_status status)
286a87a6653SShuming Fan {
287a87a6653SShuming Fan 	struct  rt1308_sdw_priv *rt1308 = dev_get_drvdata(&slave->dev);
288a87a6653SShuming Fan 
289a87a6653SShuming Fan 	/* Update the status */
290a87a6653SShuming Fan 	rt1308->status = status;
291a87a6653SShuming Fan 
292a87a6653SShuming Fan 	if (status == SDW_SLAVE_UNATTACHED)
293a87a6653SShuming Fan 		rt1308->hw_init = false;
294a87a6653SShuming Fan 
295a87a6653SShuming Fan 	/*
296a87a6653SShuming Fan 	 * Perform initialization only if slave status is present and
297a87a6653SShuming Fan 	 * hw_init flag is false
298a87a6653SShuming Fan 	 */
299a87a6653SShuming Fan 	if (rt1308->hw_init || rt1308->status != SDW_SLAVE_ATTACHED)
300a87a6653SShuming Fan 		return 0;
301a87a6653SShuming Fan 
302a87a6653SShuming Fan 	/* perform I/O transfers required for Slave initialization */
303a87a6653SShuming Fan 	return rt1308_io_init(&slave->dev, slave);
304a87a6653SShuming Fan }
305a87a6653SShuming Fan 
306a87a6653SShuming Fan static int rt1308_bus_config(struct sdw_slave *slave,
307a87a6653SShuming Fan 				struct sdw_bus_params *params)
308a87a6653SShuming Fan {
309a87a6653SShuming Fan 	struct rt1308_sdw_priv *rt1308 = dev_get_drvdata(&slave->dev);
310a87a6653SShuming Fan 	int ret;
311a87a6653SShuming Fan 
312a87a6653SShuming Fan 	memcpy(&rt1308->params, params, sizeof(*params));
313a87a6653SShuming Fan 
314a87a6653SShuming Fan 	ret = rt1308_clock_config(&slave->dev);
315a87a6653SShuming Fan 	if (ret < 0)
316a87a6653SShuming Fan 		dev_err(&slave->dev, "Invalid clk config");
317a87a6653SShuming Fan 
318a87a6653SShuming Fan 	return ret;
319a87a6653SShuming Fan }
320a87a6653SShuming Fan 
321a87a6653SShuming Fan static int rt1308_interrupt_callback(struct sdw_slave *slave,
322a87a6653SShuming Fan 					struct sdw_slave_intr_status *status)
323a87a6653SShuming Fan {
324a87a6653SShuming Fan 	dev_dbg(&slave->dev,
325a87a6653SShuming Fan 		"%s control_port_stat=%x", __func__, status->control_port);
326a87a6653SShuming Fan 
327a87a6653SShuming Fan 	return 0;
328a87a6653SShuming Fan }
329a87a6653SShuming Fan 
330a87a6653SShuming Fan static int rt1308_classd_event(struct snd_soc_dapm_widget *w,
331a87a6653SShuming Fan 	struct snd_kcontrol *kcontrol, int event)
332a87a6653SShuming Fan {
333a87a6653SShuming Fan 	struct snd_soc_component *component =
334a87a6653SShuming Fan 		snd_soc_dapm_to_component(w->dapm);
335a87a6653SShuming Fan 
336a87a6653SShuming Fan 	switch (event) {
337a87a6653SShuming Fan 	case SND_SOC_DAPM_POST_PMU:
338a87a6653SShuming Fan 		msleep(30);
339a87a6653SShuming Fan 		snd_soc_component_update_bits(component,
340a87a6653SShuming Fan 			RT1308_SDW_OFFSET | (RT1308_POWER_STATUS << 4),
341a87a6653SShuming Fan 			0x3,	0x3);
342a87a6653SShuming Fan 		msleep(40);
343a87a6653SShuming Fan 		break;
344a87a6653SShuming Fan 	case SND_SOC_DAPM_PRE_PMD:
345a87a6653SShuming Fan 		snd_soc_component_update_bits(component,
346a87a6653SShuming Fan 			RT1308_SDW_OFFSET | (RT1308_POWER_STATUS << 4),
347a87a6653SShuming Fan 			0x3, 0);
348a87a6653SShuming Fan 		usleep_range(150000, 200000);
349a87a6653SShuming Fan 		break;
350a87a6653SShuming Fan 
351a87a6653SShuming Fan 	default:
352a87a6653SShuming Fan 		break;
353a87a6653SShuming Fan 	}
354a87a6653SShuming Fan 
355a87a6653SShuming Fan 	return 0;
356a87a6653SShuming Fan }
357a87a6653SShuming Fan 
358a87a6653SShuming Fan static const char * const rt1308_rx_data_ch_select[] = {
359a87a6653SShuming Fan 	"LR",
360a87a6653SShuming Fan 	"LL",
361a87a6653SShuming Fan 	"RL",
362a87a6653SShuming Fan 	"RR",
363a87a6653SShuming Fan };
364a87a6653SShuming Fan 
365a87a6653SShuming Fan static SOC_ENUM_SINGLE_DECL(rt1308_rx_data_ch_enum,
366a87a6653SShuming Fan 	RT1308_SDW_OFFSET | (RT1308_DATA_PATH << 4), 0,
367a87a6653SShuming Fan 	rt1308_rx_data_ch_select);
368a87a6653SShuming Fan 
369a87a6653SShuming Fan static const struct snd_kcontrol_new rt1308_snd_controls[] = {
370a87a6653SShuming Fan 
371a87a6653SShuming Fan 	/* I2S Data Channel Selection */
372a87a6653SShuming Fan 	SOC_ENUM("RX Channel Select", rt1308_rx_data_ch_enum),
373a87a6653SShuming Fan };
374a87a6653SShuming Fan 
375a87a6653SShuming Fan static const struct snd_kcontrol_new rt1308_sto_dac_l =
376a87a6653SShuming Fan 	SOC_DAPM_SINGLE_AUTODISABLE("Switch",
377a87a6653SShuming Fan 		RT1308_SDW_OFFSET_BYTE3 | (RT1308_DAC_SET << 4),
378a87a6653SShuming Fan 		RT1308_DVOL_MUTE_L_EN_SFT, 1, 1);
379a87a6653SShuming Fan 
380a87a6653SShuming Fan static const struct snd_kcontrol_new rt1308_sto_dac_r =
381a87a6653SShuming Fan 	SOC_DAPM_SINGLE_AUTODISABLE("Switch",
382a87a6653SShuming Fan 		RT1308_SDW_OFFSET_BYTE3 | (RT1308_DAC_SET << 4),
383a87a6653SShuming Fan 		RT1308_DVOL_MUTE_R_EN_SFT, 1, 1);
384a87a6653SShuming Fan 
385a87a6653SShuming Fan static const struct snd_soc_dapm_widget rt1308_dapm_widgets[] = {
386a87a6653SShuming Fan 	/* Audio Interface */
387a87a6653SShuming Fan 	SND_SOC_DAPM_AIF_IN("AIF1RX", "DP1 Playback", 0, SND_SOC_NOPM, 0, 0),
388a87a6653SShuming Fan 
389a87a6653SShuming Fan 	/* Supply Widgets */
390a87a6653SShuming Fan 	SND_SOC_DAPM_SUPPLY("MBIAS20U",
391a87a6653SShuming Fan 		RT1308_SDW_OFFSET | (RT1308_POWER << 4),	7, 0, NULL, 0),
392a87a6653SShuming Fan 	SND_SOC_DAPM_SUPPLY("ALDO",
393a87a6653SShuming Fan 		RT1308_SDW_OFFSET | (RT1308_POWER << 4),	6, 0, NULL, 0),
394a87a6653SShuming Fan 	SND_SOC_DAPM_SUPPLY("DBG",
395a87a6653SShuming Fan 		RT1308_SDW_OFFSET | (RT1308_POWER << 4),	5, 0, NULL, 0),
396a87a6653SShuming Fan 	SND_SOC_DAPM_SUPPLY("DACL",
397a87a6653SShuming Fan 		RT1308_SDW_OFFSET | (RT1308_POWER << 4),	4, 0, NULL, 0),
398a87a6653SShuming Fan 	SND_SOC_DAPM_SUPPLY("CLK25M",
399a87a6653SShuming Fan 		RT1308_SDW_OFFSET | (RT1308_POWER << 4),	2, 0, NULL, 0),
400a87a6653SShuming Fan 	SND_SOC_DAPM_SUPPLY("ADC_R",
401a87a6653SShuming Fan 		RT1308_SDW_OFFSET | (RT1308_POWER << 4),	1, 0, NULL, 0),
402a87a6653SShuming Fan 	SND_SOC_DAPM_SUPPLY("ADC_L",
403a87a6653SShuming Fan 		RT1308_SDW_OFFSET | (RT1308_POWER << 4),	0, 0, NULL, 0),
404a87a6653SShuming Fan 	SND_SOC_DAPM_SUPPLY("DAC Power",
405a87a6653SShuming Fan 		RT1308_SDW_OFFSET | (RT1308_POWER << 4),	3, 0, NULL, 0),
406a87a6653SShuming Fan 
407a87a6653SShuming Fan 	SND_SOC_DAPM_SUPPLY("DLDO",
408a87a6653SShuming Fan 		RT1308_SDW_OFFSET_BYTE1 | (RT1308_POWER << 4),	5, 0, NULL, 0),
409a87a6653SShuming Fan 	SND_SOC_DAPM_SUPPLY("VREF",
410a87a6653SShuming Fan 		RT1308_SDW_OFFSET_BYTE1 | (RT1308_POWER << 4),	4, 0, NULL, 0),
411a87a6653SShuming Fan 	SND_SOC_DAPM_SUPPLY("MIXER_R",
412a87a6653SShuming Fan 		RT1308_SDW_OFFSET_BYTE1 | (RT1308_POWER << 4),	2, 0, NULL, 0),
413a87a6653SShuming Fan 	SND_SOC_DAPM_SUPPLY("MIXER_L",
414a87a6653SShuming Fan 		RT1308_SDW_OFFSET_BYTE1 | (RT1308_POWER << 4),	1, 0, NULL, 0),
415a87a6653SShuming Fan 	SND_SOC_DAPM_SUPPLY("MBIAS4U",
416a87a6653SShuming Fan 		RT1308_SDW_OFFSET_BYTE1 | (RT1308_POWER << 4),	0, 0, NULL, 0),
417a87a6653SShuming Fan 
418a87a6653SShuming Fan 	SND_SOC_DAPM_SUPPLY("PLL2_LDO",
419a87a6653SShuming Fan 		RT1308_SDW_OFFSET_BYTE2 | (RT1308_POWER << 4), 4, 0, NULL, 0),
420a87a6653SShuming Fan 	SND_SOC_DAPM_SUPPLY("PLL2B",
421a87a6653SShuming Fan 		RT1308_SDW_OFFSET_BYTE2 | (RT1308_POWER << 4), 3, 0, NULL, 0),
422a87a6653SShuming Fan 	SND_SOC_DAPM_SUPPLY("PLL2F",
423a87a6653SShuming Fan 		RT1308_SDW_OFFSET_BYTE2 | (RT1308_POWER << 4), 2, 0, NULL, 0),
424a87a6653SShuming Fan 	SND_SOC_DAPM_SUPPLY("PLL2F2",
425a87a6653SShuming Fan 		RT1308_SDW_OFFSET_BYTE2 | (RT1308_POWER << 4), 1, 0, NULL, 0),
426a87a6653SShuming Fan 	SND_SOC_DAPM_SUPPLY("PLL2B2",
427a87a6653SShuming Fan 		RT1308_SDW_OFFSET_BYTE2 | (RT1308_POWER << 4), 0, 0, NULL, 0),
428a87a6653SShuming Fan 
429a87a6653SShuming Fan 	/* Digital Interface */
430a87a6653SShuming Fan 	SND_SOC_DAPM_DAC("DAC", NULL, SND_SOC_NOPM, 0, 0),
431a87a6653SShuming Fan 	SND_SOC_DAPM_SWITCH("DAC L", SND_SOC_NOPM, 0, 0, &rt1308_sto_dac_l),
432a87a6653SShuming Fan 	SND_SOC_DAPM_SWITCH("DAC R", SND_SOC_NOPM, 0, 0, &rt1308_sto_dac_r),
433a87a6653SShuming Fan 
434a87a6653SShuming Fan 	/* Output Lines */
435a87a6653SShuming Fan 	SND_SOC_DAPM_PGA_E("CLASS D", SND_SOC_NOPM, 0, 0, NULL, 0,
436a87a6653SShuming Fan 		rt1308_classd_event,
437a87a6653SShuming Fan 		SND_SOC_DAPM_PRE_PMD | SND_SOC_DAPM_POST_PMU),
438a87a6653SShuming Fan 	SND_SOC_DAPM_OUTPUT("SPOL"),
439a87a6653SShuming Fan 	SND_SOC_DAPM_OUTPUT("SPOR"),
440a87a6653SShuming Fan };
441a87a6653SShuming Fan 
442a87a6653SShuming Fan static const struct snd_soc_dapm_route rt1308_dapm_routes[] = {
443a87a6653SShuming Fan 
444a87a6653SShuming Fan 	{ "DAC", NULL, "AIF1RX" },
445a87a6653SShuming Fan 
446a87a6653SShuming Fan 	{ "DAC", NULL, "MBIAS20U" },
447a87a6653SShuming Fan 	{ "DAC", NULL, "ALDO" },
448a87a6653SShuming Fan 	{ "DAC", NULL, "DBG" },
449a87a6653SShuming Fan 	{ "DAC", NULL, "DACL" },
450a87a6653SShuming Fan 	{ "DAC", NULL, "CLK25M" },
451a87a6653SShuming Fan 	{ "DAC", NULL, "ADC_R" },
452a87a6653SShuming Fan 	{ "DAC", NULL, "ADC_L" },
453a87a6653SShuming Fan 	{ "DAC", NULL, "DLDO" },
454a87a6653SShuming Fan 	{ "DAC", NULL, "VREF" },
455a87a6653SShuming Fan 	{ "DAC", NULL, "MIXER_R" },
456a87a6653SShuming Fan 	{ "DAC", NULL, "MIXER_L" },
457a87a6653SShuming Fan 	{ "DAC", NULL, "MBIAS4U" },
458a87a6653SShuming Fan 	{ "DAC", NULL, "PLL2_LDO" },
459a87a6653SShuming Fan 	{ "DAC", NULL, "PLL2B" },
460a87a6653SShuming Fan 	{ "DAC", NULL, "PLL2F" },
461a87a6653SShuming Fan 	{ "DAC", NULL, "PLL2F2" },
462a87a6653SShuming Fan 	{ "DAC", NULL, "PLL2B2" },
463a87a6653SShuming Fan 
464a87a6653SShuming Fan 	{ "DAC L", "Switch", "DAC" },
465a87a6653SShuming Fan 	{ "DAC R", "Switch", "DAC" },
466a87a6653SShuming Fan 	{ "DAC L", NULL, "DAC Power" },
467a87a6653SShuming Fan 	{ "DAC R", NULL, "DAC Power" },
468a87a6653SShuming Fan 
469a87a6653SShuming Fan 	{ "CLASS D", NULL, "DAC L" },
470a87a6653SShuming Fan 	{ "CLASS D", NULL, "DAC R" },
471a87a6653SShuming Fan 	{ "SPOL", NULL, "CLASS D" },
472a87a6653SShuming Fan 	{ "SPOR", NULL, "CLASS D" },
473a87a6653SShuming Fan };
474a87a6653SShuming Fan 
475a87a6653SShuming Fan static int rt1308_set_sdw_stream(struct snd_soc_dai *dai, void *sdw_stream,
476a87a6653SShuming Fan 				int direction)
477a87a6653SShuming Fan {
478a87a6653SShuming Fan 	struct sdw_stream_data *stream;
479a87a6653SShuming Fan 
48007b542feSPierre-Louis Bossart 	if (!sdw_stream)
48107b542feSPierre-Louis Bossart 		return 0;
48207b542feSPierre-Louis Bossart 
483a87a6653SShuming Fan 	stream = kzalloc(sizeof(*stream), GFP_KERNEL);
484a87a6653SShuming Fan 	if (!stream)
485a87a6653SShuming Fan 		return -ENOMEM;
486a87a6653SShuming Fan 
487a87a6653SShuming Fan 	stream->sdw_stream = (struct sdw_stream_runtime *)sdw_stream;
488a87a6653SShuming Fan 
489a87a6653SShuming Fan 	/* Use tx_mask or rx_mask to configure stream tag and set dma_data */
490a87a6653SShuming Fan 	if (direction == SNDRV_PCM_STREAM_PLAYBACK)
491a87a6653SShuming Fan 		dai->playback_dma_data = stream;
492a87a6653SShuming Fan 	else
493a87a6653SShuming Fan 		dai->capture_dma_data = stream;
494a87a6653SShuming Fan 
495a87a6653SShuming Fan 	return 0;
496a87a6653SShuming Fan }
497a87a6653SShuming Fan 
498a87a6653SShuming Fan static void rt1308_sdw_shutdown(struct snd_pcm_substream *substream,
499a87a6653SShuming Fan 				struct snd_soc_dai *dai)
500a87a6653SShuming Fan {
501a87a6653SShuming Fan 	struct sdw_stream_data *stream;
502a87a6653SShuming Fan 
503a87a6653SShuming Fan 	stream = snd_soc_dai_get_dma_data(dai, substream);
504a87a6653SShuming Fan 	snd_soc_dai_set_dma_data(dai, substream, NULL);
505a87a6653SShuming Fan 	kfree(stream);
506a87a6653SShuming Fan }
507a87a6653SShuming Fan 
508f7cc9b99SPierre-Louis Bossart static int rt1308_sdw_set_tdm_slot(struct snd_soc_dai *dai,
509f7cc9b99SPierre-Louis Bossart 				   unsigned int tx_mask,
510f7cc9b99SPierre-Louis Bossart 				   unsigned int rx_mask,
511f7cc9b99SPierre-Louis Bossart 				   int slots, int slot_width)
512f7cc9b99SPierre-Louis Bossart {
513f7cc9b99SPierre-Louis Bossart 	struct snd_soc_component *component = dai->component;
514f7cc9b99SPierre-Louis Bossart 	struct rt1308_sdw_priv *rt1308 =
515f7cc9b99SPierre-Louis Bossart 		snd_soc_component_get_drvdata(component);
516f7cc9b99SPierre-Louis Bossart 
517f7cc9b99SPierre-Louis Bossart 	if (tx_mask)
518f7cc9b99SPierre-Louis Bossart 		return -EINVAL;
519f7cc9b99SPierre-Louis Bossart 
520f7cc9b99SPierre-Louis Bossart 	if (slots > 2)
521f7cc9b99SPierre-Louis Bossart 		return -EINVAL;
522f7cc9b99SPierre-Louis Bossart 
523f7cc9b99SPierre-Louis Bossart 	rt1308->rx_mask = rx_mask;
524f7cc9b99SPierre-Louis Bossart 	rt1308->slots = slots;
525f7cc9b99SPierre-Louis Bossart 	/* slot_width is not used since it's irrelevant for SoundWire */
526f7cc9b99SPierre-Louis Bossart 
527f7cc9b99SPierre-Louis Bossart 	return 0;
528f7cc9b99SPierre-Louis Bossart }
529f7cc9b99SPierre-Louis Bossart 
530a87a6653SShuming Fan static int rt1308_sdw_hw_params(struct snd_pcm_substream *substream,
531a87a6653SShuming Fan 	struct snd_pcm_hw_params *params, struct snd_soc_dai *dai)
532a87a6653SShuming Fan {
533a87a6653SShuming Fan 	struct snd_soc_component *component = dai->component;
534a87a6653SShuming Fan 	struct rt1308_sdw_priv *rt1308 =
535a87a6653SShuming Fan 		snd_soc_component_get_drvdata(component);
536a87a6653SShuming Fan 	struct sdw_stream_config stream_config;
537a87a6653SShuming Fan 	struct sdw_port_config port_config;
538a87a6653SShuming Fan 	enum sdw_data_direction direction;
539a87a6653SShuming Fan 	struct sdw_stream_data *stream;
54027a18e9eSPierre-Louis Bossart 	int retval, port, num_channels, ch_mask;
541a87a6653SShuming Fan 
542a87a6653SShuming Fan 	dev_dbg(dai->dev, "%s %s", __func__, dai->name);
543a87a6653SShuming Fan 	stream = snd_soc_dai_get_dma_data(dai, substream);
544a87a6653SShuming Fan 
545a87a6653SShuming Fan 	if (!stream)
546a87a6653SShuming Fan 		return -EINVAL;
547a87a6653SShuming Fan 
548a87a6653SShuming Fan 	if (!rt1308->sdw_slave)
549a87a6653SShuming Fan 		return -EINVAL;
550a87a6653SShuming Fan 
551a87a6653SShuming Fan 	/* SoundWire specific configuration */
552a87a6653SShuming Fan 	/* port 1 for playback */
553a87a6653SShuming Fan 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
554a87a6653SShuming Fan 		direction = SDW_DATA_DIR_RX;
555a87a6653SShuming Fan 		port = 1;
556a87a6653SShuming Fan 	} else {
557a87a6653SShuming Fan 		return -EINVAL;
558a87a6653SShuming Fan 	}
559a87a6653SShuming Fan 
56027a18e9eSPierre-Louis Bossart 	if (rt1308->slots) {
56127a18e9eSPierre-Louis Bossart 		num_channels = rt1308->slots;
56227a18e9eSPierre-Louis Bossart 		ch_mask = rt1308->rx_mask;
56327a18e9eSPierre-Louis Bossart 	} else {
56427a18e9eSPierre-Louis Bossart 		num_channels = params_channels(params);
56527a18e9eSPierre-Louis Bossart 		ch_mask = (1 << num_channels) - 1;
56627a18e9eSPierre-Louis Bossart 	}
56727a18e9eSPierre-Louis Bossart 
568a87a6653SShuming Fan 	stream_config.frame_rate = params_rate(params);
56927a18e9eSPierre-Louis Bossart 	stream_config.ch_count = num_channels;
570a87a6653SShuming Fan 	stream_config.bps = snd_pcm_format_width(params_format(params));
571a87a6653SShuming Fan 	stream_config.direction = direction;
572a87a6653SShuming Fan 
57327a18e9eSPierre-Louis Bossart 	port_config.ch_mask = ch_mask;
574a87a6653SShuming Fan 	port_config.num = port;
575a87a6653SShuming Fan 
576a87a6653SShuming Fan 	retval = sdw_stream_add_slave(rt1308->sdw_slave, &stream_config,
577a87a6653SShuming Fan 				&port_config, 1, stream->sdw_stream);
578a87a6653SShuming Fan 	if (retval) {
579a87a6653SShuming Fan 		dev_err(dai->dev, "Unable to configure port\n");
580a87a6653SShuming Fan 		return retval;
581a87a6653SShuming Fan 	}
582a87a6653SShuming Fan 
583a87a6653SShuming Fan 	return retval;
584a87a6653SShuming Fan }
585a87a6653SShuming Fan 
586a87a6653SShuming Fan static int rt1308_sdw_pcm_hw_free(struct snd_pcm_substream *substream,
587a87a6653SShuming Fan 				struct snd_soc_dai *dai)
588a87a6653SShuming Fan {
589a87a6653SShuming Fan 	struct snd_soc_component *component = dai->component;
590a87a6653SShuming Fan 	struct rt1308_sdw_priv *rt1308 =
591a87a6653SShuming Fan 		snd_soc_component_get_drvdata(component);
592a87a6653SShuming Fan 	struct sdw_stream_data *stream =
593a87a6653SShuming Fan 		snd_soc_dai_get_dma_data(dai, substream);
594a87a6653SShuming Fan 
595a87a6653SShuming Fan 	if (!rt1308->sdw_slave)
596a87a6653SShuming Fan 		return -EINVAL;
597a87a6653SShuming Fan 
598a87a6653SShuming Fan 	sdw_stream_remove_slave(rt1308->sdw_slave, stream->sdw_stream);
599a87a6653SShuming Fan 	return 0;
600a87a6653SShuming Fan }
601a87a6653SShuming Fan 
602a87a6653SShuming Fan /*
603a87a6653SShuming Fan  * slave_ops: callbacks for get_clock_stop_mode, clock_stop and
604a87a6653SShuming Fan  * port_prep are not defined for now
605a87a6653SShuming Fan  */
606a87a6653SShuming Fan static struct sdw_slave_ops rt1308_slave_ops = {
607a87a6653SShuming Fan 	.read_prop = rt1308_read_prop,
608a87a6653SShuming Fan 	.interrupt_callback = rt1308_interrupt_callback,
609a87a6653SShuming Fan 	.update_status = rt1308_update_status,
610a87a6653SShuming Fan 	.bus_config = rt1308_bus_config,
611a87a6653SShuming Fan };
612a87a6653SShuming Fan 
613a87a6653SShuming Fan static const struct snd_soc_component_driver soc_component_sdw_rt1308 = {
614a87a6653SShuming Fan 	.controls = rt1308_snd_controls,
615a87a6653SShuming Fan 	.num_controls = ARRAY_SIZE(rt1308_snd_controls),
616a87a6653SShuming Fan 	.dapm_widgets = rt1308_dapm_widgets,
617a87a6653SShuming Fan 	.num_dapm_widgets = ARRAY_SIZE(rt1308_dapm_widgets),
618a87a6653SShuming Fan 	.dapm_routes = rt1308_dapm_routes,
619a87a6653SShuming Fan 	.num_dapm_routes = ARRAY_SIZE(rt1308_dapm_routes),
620a87a6653SShuming Fan };
621a87a6653SShuming Fan 
622a87a6653SShuming Fan static const struct snd_soc_dai_ops rt1308_aif_dai_ops = {
623a87a6653SShuming Fan 	.hw_params = rt1308_sdw_hw_params,
624a87a6653SShuming Fan 	.hw_free	= rt1308_sdw_pcm_hw_free,
625a87a6653SShuming Fan 	.set_sdw_stream	= rt1308_set_sdw_stream,
626a87a6653SShuming Fan 	.shutdown	= rt1308_sdw_shutdown,
627f7cc9b99SPierre-Louis Bossart 	.set_tdm_slot	= rt1308_sdw_set_tdm_slot,
628a87a6653SShuming Fan };
629a87a6653SShuming Fan 
630a87a6653SShuming Fan #define RT1308_STEREO_RATES SNDRV_PCM_RATE_48000
631a87a6653SShuming Fan #define RT1308_FORMATS (SNDRV_PCM_FMTBIT_S8 | \
632a87a6653SShuming Fan 			SNDRV_PCM_FMTBIT_S20_3LE | SNDRV_PCM_FMTBIT_S16_LE | \
633a87a6653SShuming Fan 			SNDRV_PCM_FMTBIT_S24_LE)
634a87a6653SShuming Fan 
635a87a6653SShuming Fan static struct snd_soc_dai_driver rt1308_sdw_dai[] = {
636a87a6653SShuming Fan 	{
637a87a6653SShuming Fan 		.name = "rt1308-aif",
638a87a6653SShuming Fan 		.playback = {
639a87a6653SShuming Fan 			.stream_name = "DP1 Playback",
640a87a6653SShuming Fan 			.channels_min = 1,
641a87a6653SShuming Fan 			.channels_max = 2,
642a87a6653SShuming Fan 			.rates = RT1308_STEREO_RATES,
643a87a6653SShuming Fan 			.formats = RT1308_FORMATS,
644a87a6653SShuming Fan 		},
645a87a6653SShuming Fan 		.ops = &rt1308_aif_dai_ops,
646a87a6653SShuming Fan 	},
647a87a6653SShuming Fan };
648a87a6653SShuming Fan 
649a87a6653SShuming Fan static int rt1308_sdw_init(struct device *dev, struct regmap *regmap,
650a87a6653SShuming Fan 				struct sdw_slave *slave)
651a87a6653SShuming Fan {
652a87a6653SShuming Fan 	struct rt1308_sdw_priv *rt1308;
653a87a6653SShuming Fan 	int ret;
654a87a6653SShuming Fan 
655a87a6653SShuming Fan 	rt1308 = devm_kzalloc(dev, sizeof(*rt1308), GFP_KERNEL);
656a87a6653SShuming Fan 	if (!rt1308)
657a87a6653SShuming Fan 		return -ENOMEM;
658a87a6653SShuming Fan 
659a87a6653SShuming Fan 	dev_set_drvdata(dev, rt1308);
660a87a6653SShuming Fan 	rt1308->sdw_slave = slave;
661a87a6653SShuming Fan 	rt1308->regmap = regmap;
662a87a6653SShuming Fan 
663a87a6653SShuming Fan 	/*
664a87a6653SShuming Fan 	 * Mark hw_init to false
665a87a6653SShuming Fan 	 * HW init will be performed when device reports present
666a87a6653SShuming Fan 	 */
667a87a6653SShuming Fan 	rt1308->hw_init = false;
668a87a6653SShuming Fan 	rt1308->first_hw_init = false;
669a87a6653SShuming Fan 
670a87a6653SShuming Fan 	ret =  devm_snd_soc_register_component(dev,
671a87a6653SShuming Fan 				&soc_component_sdw_rt1308,
672a87a6653SShuming Fan 				rt1308_sdw_dai,
673a87a6653SShuming Fan 				ARRAY_SIZE(rt1308_sdw_dai));
674a87a6653SShuming Fan 
675a87a6653SShuming Fan 	dev_dbg(&slave->dev, "%s\n", __func__);
676a87a6653SShuming Fan 
677a87a6653SShuming Fan 	return ret;
678a87a6653SShuming Fan }
679a87a6653SShuming Fan 
680a87a6653SShuming Fan static int rt1308_sdw_probe(struct sdw_slave *slave,
681a87a6653SShuming Fan 				const struct sdw_device_id *id)
682a87a6653SShuming Fan {
683a87a6653SShuming Fan 	struct regmap *regmap;
684a87a6653SShuming Fan 
685a87a6653SShuming Fan 	/* Regmap Initialization */
686a87a6653SShuming Fan 	regmap = devm_regmap_init_sdw(slave, &rt1308_sdw_regmap);
687a87a6653SShuming Fan 	if (!regmap)
688a87a6653SShuming Fan 		return -EINVAL;
689a87a6653SShuming Fan 
690a87a6653SShuming Fan 	rt1308_sdw_init(&slave->dev, regmap, slave);
691a87a6653SShuming Fan 
692a87a6653SShuming Fan 	return 0;
693a87a6653SShuming Fan }
694a87a6653SShuming Fan 
695a87a6653SShuming Fan static const struct sdw_device_id rt1308_id[] = {
696*9e473058SPierre-Louis Bossart 	SDW_SLAVE_ENTRY_EXT(0x025d, 0x1308, 0x2, 0, 0),
697a87a6653SShuming Fan 	{},
698a87a6653SShuming Fan };
699a87a6653SShuming Fan MODULE_DEVICE_TABLE(sdw, rt1308_id);
700a87a6653SShuming Fan 
701faa37a93STakashi Iwai static int __maybe_unused rt1308_dev_suspend(struct device *dev)
702a87a6653SShuming Fan {
703a87a6653SShuming Fan 	struct rt1308_sdw_priv *rt1308 = dev_get_drvdata(dev);
704a87a6653SShuming Fan 
705a87a6653SShuming Fan 	if (!rt1308->hw_init)
706a87a6653SShuming Fan 		return 0;
707a87a6653SShuming Fan 
708a87a6653SShuming Fan 	regcache_cache_only(rt1308->regmap, true);
709a87a6653SShuming Fan 
710a87a6653SShuming Fan 	return 0;
711a87a6653SShuming Fan }
712a87a6653SShuming Fan 
713a87a6653SShuming Fan #define RT1308_PROBE_TIMEOUT 2000
714a87a6653SShuming Fan 
715faa37a93STakashi Iwai static int __maybe_unused rt1308_dev_resume(struct device *dev)
716a87a6653SShuming Fan {
717a87a6653SShuming Fan 	struct sdw_slave *slave = dev_to_sdw_dev(dev);
718a87a6653SShuming Fan 	struct rt1308_sdw_priv *rt1308 = dev_get_drvdata(dev);
719a87a6653SShuming Fan 	unsigned long time;
720a87a6653SShuming Fan 
721a87a6653SShuming Fan 	if (!rt1308->hw_init)
722a87a6653SShuming Fan 		return 0;
723a87a6653SShuming Fan 
724a87a6653SShuming Fan 	if (!slave->unattach_request)
725a87a6653SShuming Fan 		goto regmap_sync;
726a87a6653SShuming Fan 
727a87a6653SShuming Fan 	time = wait_for_completion_timeout(&slave->initialization_complete,
728a87a6653SShuming Fan 				msecs_to_jiffies(RT1308_PROBE_TIMEOUT));
729a87a6653SShuming Fan 	if (!time) {
730a87a6653SShuming Fan 		dev_err(&slave->dev, "Initialization not complete, timed out\n");
731a87a6653SShuming Fan 		return -ETIMEDOUT;
732a87a6653SShuming Fan 	}
733a87a6653SShuming Fan 
734a87a6653SShuming Fan regmap_sync:
735a87a6653SShuming Fan 	slave->unattach_request = 0;
736a87a6653SShuming Fan 	regcache_cache_only(rt1308->regmap, false);
737a87a6653SShuming Fan 	regcache_sync_region(rt1308->regmap, 0xc000, 0xcfff);
738a87a6653SShuming Fan 
739a87a6653SShuming Fan 	return 0;
740a87a6653SShuming Fan }
741a87a6653SShuming Fan 
742a87a6653SShuming Fan static const struct dev_pm_ops rt1308_pm = {
743a87a6653SShuming Fan 	SET_SYSTEM_SLEEP_PM_OPS(rt1308_dev_suspend, rt1308_dev_resume)
744a87a6653SShuming Fan 	SET_RUNTIME_PM_OPS(rt1308_dev_suspend, rt1308_dev_resume, NULL)
745a87a6653SShuming Fan };
746a87a6653SShuming Fan 
747a87a6653SShuming Fan static struct sdw_driver rt1308_sdw_driver = {
748a87a6653SShuming Fan 	.driver = {
749a87a6653SShuming Fan 		.name = "rt1308",
750a87a6653SShuming Fan 		.owner = THIS_MODULE,
751a87a6653SShuming Fan 		.pm = &rt1308_pm,
752a87a6653SShuming Fan 	},
753a87a6653SShuming Fan 	.probe = rt1308_sdw_probe,
754a87a6653SShuming Fan 	.ops = &rt1308_slave_ops,
755a87a6653SShuming Fan 	.id_table = rt1308_id,
756a87a6653SShuming Fan };
757a87a6653SShuming Fan module_sdw_driver(rt1308_sdw_driver);
758a87a6653SShuming Fan 
759a87a6653SShuming Fan MODULE_DESCRIPTION("ASoC RT1308 driver SDW");
760a87a6653SShuming Fan MODULE_AUTHOR("Shuming Fan <shumingf@realtek.com>");
761a87a6653SShuming Fan MODULE_LICENSE("GPL v2");
762