summaryrefslogtreecommitdiff
path: root/sound/soc/sdca/sdca_class.c
blob: 79fdb672be1d7e67ee482d7f35e58f6c32921e1f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
// SPDX-License-Identifier: GPL-2.0
// Copyright (C) 2025 Cirrus Logic, Inc. and
//                    Cirrus Logic International Semiconductor Ltd.

/*
 * The MIPI SDCA specification is available for public downloads at
 * https://www.mipi.org/mipi-sdca-v1-0-download
 */

#include <linux/device.h>
#include <linux/err.h>
#include <linux/module.h>
#include <linux/pm.h>
#include <linux/pm_runtime.h>
#include <linux/regmap.h>
#include <linux/soundwire/sdw.h>
#include <linux/soundwire/sdw_registers.h>
#include <linux/soundwire/sdw_type.h>
#include <sound/sdca.h>
#include <sound/sdca_function.h>
#include <sound/sdca_interrupts.h>
#include <sound/sdca_regmap.h>
#include <sound/sdca_class.h>

#define CLASS_SDW_ATTACH_TIMEOUT_MS	5000

static int class_read_prop(struct sdw_slave *sdw)
{
	struct sdw_slave_prop *prop = &sdw->prop;

	sdw_slave_read_prop(sdw);

	prop->use_domain_irq = true;
	prop->scp_int1_mask = SDW_SCP_INT1_BUS_CLASH | SDW_SCP_INT1_PARITY |
			      SDW_SCP_INT1_IMPL_DEF;

	return 0;
}

static const struct sdw_slave_ops class_sdw_ops = {
	.read_prop	= class_read_prop,
};

static void class_regmap_lock(void *data)
{
	struct mutex *lock = data;

	mutex_lock(lock);
}

static void class_regmap_unlock(void *data)
{
	struct mutex *lock = data;

	mutex_unlock(lock);
}

static bool class_dev_regmap_volatile(struct device *dev, unsigned int reg)
{
	switch (reg) {
	case SDW_SCP_SDCA_INTMASK1 ... SDW_SCP_SDCA_INTMASK4:
		return false;
	default:
		return true;
	}
}

static bool class_dev_regmap_precious(struct device *dev, unsigned int reg)
{
	switch (reg) {
	case SDW_SCP_SDCA_INT1 ... SDW_SCP_SDCA_INT4:
	case SDW_SCP_SDCA_INTMASK1 ... SDW_SCP_SDCA_INTMASK4:
		return false;
	default:
		return true;
	}
}

static const struct regmap_config class_dev_regmap_config = {
	.name			= "sdca-device",
	.reg_bits		= 32,
	.val_bits		= 8,

	.max_register		= SDW_SDCA_MAX_REGISTER,
	.volatile_reg		= class_dev_regmap_volatile,
	.precious_reg		= class_dev_regmap_precious,

	.cache_type		= REGCACHE_MAPLE,

	.lock			= class_regmap_lock,
	.unlock			= class_regmap_unlock,
};

static void class_remove_functions(void *data)
{
	struct sdca_class_drv *drv = data;

	sdca_dev_unregister_functions(drv->sdw);
}

static void class_boot_work(struct work_struct *work)
{
	struct sdca_class_drv *drv = container_of(work,
						  struct sdca_class_drv,
						  boot_work);
	int ret;

	ret = sdw_slave_wait_for_init(drv->sdw, CLASS_SDW_ATTACH_TIMEOUT_MS);
	if (ret)
		goto err;

	regcache_cache_only(drv->dev_regmap, false);

	drv->irq_info = devm_sdca_irq_allocate(drv->dev, drv->dev_regmap,
					       drv->sdw->irq);
	if (IS_ERR(drv->irq_info))
		goto err;

	ret = sdca_dev_register_functions(drv->sdw);
	if (ret)
		goto err;

	/* Ensure function drivers are removed before the IRQ is destroyed */
	ret = devm_add_action_or_reset(drv->dev, class_remove_functions, drv);
	if (ret)
		goto err;

	dev_dbg(drv->dev, "boot work complete\n");

	pm_runtime_mark_last_busy(drv->dev);
	pm_runtime_put_autosuspend(drv->dev);

	return;

err:
	pm_runtime_put_sync(drv->dev);
}

/**
 * sdca_class_probe - SDCA class SoundWire slave probe helper
 * @sdw: SoundWire slave
 * @drv: caller-allocated sdca_class_drv storage.  The caller (a codec
 *       driver, or the built-in class_sdw_driver in this file) owns the
 *       allocation and sets its own dev_set_drvdata() -- the framework
 *       does not touch drvdata.  Typically embedded in the codec's own
 *       priv struct so codec drivers can keep per-slave state.
 * @ops: optional codec-provided class callbacks (may be NULL for
 *       pure-generic SDCA parts that need no quirks)
 *
 * Codec-specific SoundWire drivers call this from their .probe after
 * allocating a struct sdca_class_drv (usually embedded in their own
 * priv) and setting drvdata to their priv.  The framework fills in the
 * sdca_class_drv fields, sets up the class regmap, and queues the
 * deferred boot work.
 */
int sdca_class_probe(struct sdw_slave *sdw,
		     struct sdca_class_drv *drv,
		     const struct sdca_class_ops *ops)
{
	struct device *dev = &sdw->dev;
	struct regmap_config *dev_config;
	int ret;

	sdca_lookup_swft(sdw);

	if (!drv)
		return -EINVAL;

	dev_config = devm_kmemdup(dev, &class_dev_regmap_config,
				  sizeof(*dev_config), GFP_KERNEL);
	if (!dev_config)
		return -ENOMEM;

	drv->dev = dev;
	drv->sdw = sdw;
	drv->ops = ops;
	mutex_init(&drv->regmap_lock);
	mutex_init(&drv->init_lock);

	INIT_WORK(&drv->boot_work, class_boot_work);

	dev_config->lock_arg = &drv->regmap_lock;

	drv->dev_regmap = devm_regmap_init_sdw(sdw, dev_config);
	if (IS_ERR(drv->dev_regmap))
		return dev_err_probe(drv->dev, PTR_ERR(drv->dev_regmap),
				     "failed to create device regmap\n");

	regcache_cache_only(drv->dev_regmap, true);

	pm_runtime_set_autosuspend_delay(dev, 250);
	pm_runtime_use_autosuspend(dev);
	pm_runtime_set_active(dev);
	pm_runtime_get_noresume(dev);

	ret = devm_pm_runtime_enable(dev);
	if (ret)
		return ret;

	queue_work(system_long_wq, &drv->boot_work);

	return 0;
}
EXPORT_SYMBOL_NS_GPL(sdca_class_probe, "SND_SOC_SDCA_CLASS");

static int class_sdw_probe(struct sdw_slave *sdw, const struct sdw_device_id *id)
{
	struct sdca_class_drv *drv;

	/*
	 * Pure-generic SDCA parts: no codec priv to embed, so allocate a
	 * bare sdca_class_drv here and stash it in drvdata for the
	 * built-in PM ops to fetch.
	 */
	drv = devm_kzalloc(&sdw->dev, sizeof(*drv), GFP_KERNEL);
	if (!drv)
		return -ENOMEM;

	dev_set_drvdata(&sdw->dev, drv);

	return sdca_class_probe(sdw, drv, NULL);
}

/**
 * sdca_class_remove - SDCA class SoundWire slave remove helper
 * @drv: caller-owned sdca_class_drv (the one handed to sdca_class_probe()).
 *
 * Cancels the deferred boot work so devres can safely free @drv and the
 * embedding codec priv without racing class_boot_work.  Codec-specific
 * SoundWire drivers that call sdca_class_probe() must call this from
 * their .remove with the same drv pointer they passed to probe.
 */
void sdca_class_remove(struct sdca_class_drv *drv)
{
	cancel_work_sync(&drv->boot_work);
}
EXPORT_SYMBOL_NS_GPL(sdca_class_remove, "SND_SOC_SDCA_CLASS");

static void class_sdw_remove(struct sdw_slave *sdw)
{
	struct sdca_class_drv *drv = dev_get_drvdata(&sdw->dev);

	sdca_class_remove(drv);
}

/**
 * sdca_class_system_suspend - SDCA class system suspend helper
 * @drv: caller-owned sdca_class_drv.
 *
 * Codec drivers compose this into their own dev_pm_ops.  Disables the
 * SoundWire interrupt and forces runtime suspend of the underlying
 * class regmap.
 */
int sdca_class_system_suspend(struct sdca_class_drv *drv)
{
	int ret;

	disable_irq(drv->sdw->irq);

	ret = pm_runtime_force_suspend(drv->dev);
	if (ret) {
		dev_err(drv->dev, "failed to force suspend: %d\n", ret);
		return ret;
	}

	return 0;
}
EXPORT_SYMBOL_NS_GPL(sdca_class_system_suspend, "SND_SOC_SDCA_CLASS");

/**
 * sdca_class_system_resume - SDCA class system resume helper
 * @drv: caller-owned sdca_class_drv.
 */
int sdca_class_system_resume(struct sdca_class_drv *drv)
{
	int ret;

	ret = pm_runtime_force_resume(drv->dev);
	if (ret) {
		dev_err(drv->dev, "failed to force resume: %d\n", ret);
		return ret;
	}

	enable_irq(drv->sdw->irq);

	return 0;
}
EXPORT_SYMBOL_NS_GPL(sdca_class_system_resume, "SND_SOC_SDCA_CLASS");

/**
 * sdca_class_runtime_suspend - SDCA class runtime suspend helper
 * @drv: caller-owned sdca_class_drv.
 */
int sdca_class_runtime_suspend(struct sdca_class_drv *drv)
{
	/*
	 * Whilst the driver doesn't power the chip down here, going into runtime
	 * suspend lets the SoundWire bus power down, which means the driver
	 * can't communicate with the device any more.
	 */
	regcache_cache_only(drv->dev_regmap, true);

	return 0;
}
EXPORT_SYMBOL_NS_GPL(sdca_class_runtime_suspend, "SND_SOC_SDCA_CLASS");

/**
 * sdca_class_runtime_resume - SDCA class runtime resume helper
 * @drv: caller-owned sdca_class_drv.
 */
int sdca_class_runtime_resume(struct sdca_class_drv *drv)
{
	int ret;

	ret = sdw_slave_wait_for_init(drv->sdw, CLASS_SDW_ATTACH_TIMEOUT_MS);
	if (ret)
		goto err;

	regcache_cache_only(drv->dev_regmap, false);
	regcache_mark_dirty(drv->dev_regmap);

	ret = regcache_sync(drv->dev_regmap);
	if (ret) {
		dev_err(drv->dev, "failed to restore cache: %d\n", ret);
		goto err;
	}

	return 0;

err:
	regcache_cache_only(drv->dev_regmap, true);

	return ret;
}
EXPORT_SYMBOL_NS_GPL(sdca_class_runtime_resume, "SND_SOC_SDCA_CLASS");

/*
 * Convenience dev_pm_ops used by the built-in class_sdw_driver, which
 * stashes its sdca_class_drv in drvdata directly.  Codec drivers that
 * embed sdca_class_drv in their own priv compose their own dev_pm_ops
 * using the sdca_class_*_suspend/resume helpers above.
 */
static int class_pm_system_suspend(struct device *dev)
{
	return sdca_class_system_suspend(dev_get_drvdata(dev));
}

static int class_pm_system_resume(struct device *dev)
{
	return sdca_class_system_resume(dev_get_drvdata(dev));
}

static int class_pm_runtime_suspend(struct device *dev)
{
	return sdca_class_runtime_suspend(dev_get_drvdata(dev));
}

static int class_pm_runtime_resume(struct device *dev)
{
	return sdca_class_runtime_resume(dev_get_drvdata(dev));
}

static const struct dev_pm_ops sdca_class_pm_ops = {
	SYSTEM_SLEEP_PM_OPS(class_pm_system_suspend, class_pm_system_resume)
	RUNTIME_PM_OPS(class_pm_runtime_suspend, class_pm_runtime_resume, NULL)
};

static const struct sdw_device_id class_sdw_id[] = {
	SDW_SLAVE_ENTRY(0x01FA, 0x4245, 0),
	SDW_SLAVE_ENTRY(0x01FA, 0x4249, 0),
	SDW_SLAVE_ENTRY(0x01FA, 0x4747, 0),
	{}
};
MODULE_DEVICE_TABLE(sdw, class_sdw_id);

static struct sdw_driver class_sdw_driver = {
	.driver = {
		.name		= "sdca_class",
		.pm		= pm_ptr(&sdca_class_pm_ops),
	},

	.probe		= class_sdw_probe,
	.remove		= class_sdw_remove,
	.id_table	= class_sdw_id,
	.ops		= &class_sdw_ops,
};
module_sdw_driver(class_sdw_driver);

MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("SDCA Class Driver");
MODULE_IMPORT_NS("SND_SOC_SDCA");