~hui.wang/alsa-driver/dkms-packaging.audiosdw-ppa

« back to all changes in this revision

Viewing changes to buildroot/src/oem-audiosdw-lp1836324-0.6ubuntu1.2/soc/codecs/rt1308-sdw.c

  • Committer: Hui Wang
  • Date: 2019-12-13 02:41:40 UTC
  • Revision ID: hui.wang@canonical.com-20191213024140-1cprdcbl3122fn85
insert pc-oem-dkms

Show diffs side-by-side

added added

removed removed

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