soc-component.c 14.9 KB
Newer Older
K
Kuninori Morimoto 已提交
1 2 3 4 5 6 7
// SPDX-License-Identifier: GPL-2.0
//
// soc-component.c
//
// Copyright (C) 2019 Renesas Electronics Corp.
// Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
//
8
#include <linux/module.h>
K
Kuninori Morimoto 已提交
9 10
#include <sound/soc.h>

11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
#define soc_component_ret(dai, ret) _soc_component_ret(dai, __func__, ret)
static inline int _soc_component_ret(struct snd_soc_component *component,
				     const char *func, int ret)
{
	/* Positive/Zero values are not errors */
	if (ret >= 0)
		return ret;

	/* Negative values might be errors */
	switch (ret) {
	case -EPROBE_DEFER:
	case -ENOTSUPP:
		break;
	default:
		dev_err(component->dev,
			"ASoC: error at %s on %s: %d\n",
			func, component->name, ret);
	}

	return ret;
}

33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
int snd_soc_component_initialize(struct snd_soc_component *component,
				 const struct snd_soc_component_driver *driver,
				 struct device *dev, const char *name)
{
	INIT_LIST_HEAD(&component->dai_list);
	INIT_LIST_HEAD(&component->dobj_list);
	INIT_LIST_HEAD(&component->card_list);
	mutex_init(&component->io_mutex);

	component->name		= name;
	component->dev		= dev;
	component->driver	= driver;

	return 0;
}

K
Kuninori Morimoto 已提交
49 50 51 52 53 54 55 56 57 58 59 60 61 62
/**
 * snd_soc_component_set_sysclk - configure COMPONENT system or master clock.
 * @component: COMPONENT
 * @clk_id: DAI specific clock ID
 * @source: Source for the clock
 * @freq: new clock frequency in Hz
 * @dir: new clock direction - input/output.
 *
 * Configures the CODEC master (MCLK) or system (SYSCLK) clocking.
 */
int snd_soc_component_set_sysclk(struct snd_soc_component *component,
				 int clk_id, int source, unsigned int freq,
				 int dir)
{
63 64
	int ret = -ENOTSUPP;

K
Kuninori Morimoto 已提交
65
	if (component->driver->set_sysclk)
66
		ret = component->driver->set_sysclk(component, clk_id, source,
K
Kuninori Morimoto 已提交
67 68
						     freq, dir);

69
	return soc_component_ret(component, ret);
K
Kuninori Morimoto 已提交
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
}
EXPORT_SYMBOL_GPL(snd_soc_component_set_sysclk);

/*
 * snd_soc_component_set_pll - configure component PLL.
 * @component: COMPONENT
 * @pll_id: DAI specific PLL ID
 * @source: DAI specific source for the PLL
 * @freq_in: PLL input clock frequency in Hz
 * @freq_out: requested PLL output clock frequency in Hz
 *
 * Configures and enables PLL to generate output clock based on input clock.
 */
int snd_soc_component_set_pll(struct snd_soc_component *component, int pll_id,
			      int source, unsigned int freq_in,
			      unsigned int freq_out)
{
87 88
	int ret = -EINVAL;

K
Kuninori Morimoto 已提交
89
	if (component->driver->set_pll)
90
		ret = component->driver->set_pll(component, pll_id, source,
K
Kuninori Morimoto 已提交
91 92
						  freq_in, freq_out);

93
	return soc_component_ret(component, ret);
K
Kuninori Morimoto 已提交
94 95 96
}
EXPORT_SYMBOL_GPL(snd_soc_component_set_pll);

97 98 99 100 101 102 103
void snd_soc_component_seq_notifier(struct snd_soc_component *component,
				    enum snd_soc_dapm_type type, int subseq)
{
	if (component->driver->seq_notifier)
		component->driver->seq_notifier(component, type, subseq);
}

104 105 106
int snd_soc_component_stream_event(struct snd_soc_component *component,
				   int event)
{
107 108
	int ret = 0;

109
	if (component->driver->stream_event)
110
		ret = component->driver->stream_event(component, event);
111

112
	return soc_component_ret(component, ret);
113 114
}

115 116 117
int snd_soc_component_set_bias_level(struct snd_soc_component *component,
				     enum snd_soc_bias_level level)
{
118 119
	int ret = 0;

120
	if (component->driver->set_bias_level)
121
		ret = component->driver->set_bias_level(component, level);
122

123
	return soc_component_ret(component, ret);
124 125
}

126 127 128 129
static int soc_component_pin(struct snd_soc_component *component,
			     const char *pin,
			     int (*pin_func)(struct snd_soc_dapm_context *dapm,
					     const char *pin))
K
Kuninori Morimoto 已提交
130 131 132 133 134 135
{
	struct snd_soc_dapm_context *dapm =
		snd_soc_component_get_dapm(component);
	char *full_name;
	int ret;

136 137 138 139
	if (!component->name_prefix) {
		ret = pin_func(dapm, pin);
		goto end;
	}
K
Kuninori Morimoto 已提交
140 141

	full_name = kasprintf(GFP_KERNEL, "%s %s", component->name_prefix, pin);
142 143 144 145
	if (!full_name) {
		ret = -ENOMEM;
		goto end;
	}
K
Kuninori Morimoto 已提交
146

147
	ret = pin_func(dapm, full_name);
K
Kuninori Morimoto 已提交
148
	kfree(full_name);
149 150
end:
	return soc_component_ret(component, ret);
K
Kuninori Morimoto 已提交
151
}
152 153 154 155 156 157

int snd_soc_component_enable_pin(struct snd_soc_component *component,
				 const char *pin)
{
	return soc_component_pin(component, pin, snd_soc_dapm_enable_pin);
}
K
Kuninori Morimoto 已提交
158 159 160 161 162
EXPORT_SYMBOL_GPL(snd_soc_component_enable_pin);

int snd_soc_component_enable_pin_unlocked(struct snd_soc_component *component,
					  const char *pin)
{
163
	return soc_component_pin(component, pin, snd_soc_dapm_enable_pin_unlocked);
K
Kuninori Morimoto 已提交
164 165 166 167 168 169
}
EXPORT_SYMBOL_GPL(snd_soc_component_enable_pin_unlocked);

int snd_soc_component_disable_pin(struct snd_soc_component *component,
				  const char *pin)
{
170
	return soc_component_pin(component, pin, snd_soc_dapm_disable_pin);
K
Kuninori Morimoto 已提交
171 172 173 174 175 176
}
EXPORT_SYMBOL_GPL(snd_soc_component_disable_pin);

int snd_soc_component_disable_pin_unlocked(struct snd_soc_component *component,
					   const char *pin)
{
177
	return soc_component_pin(component, pin, snd_soc_dapm_disable_pin_unlocked);
K
Kuninori Morimoto 已提交
178 179 180 181 182 183
}
EXPORT_SYMBOL_GPL(snd_soc_component_disable_pin_unlocked);

int snd_soc_component_nc_pin(struct snd_soc_component *component,
			     const char *pin)
{
184
	return soc_component_pin(component, pin, snd_soc_dapm_nc_pin);
K
Kuninori Morimoto 已提交
185 186 187 188 189 190
}
EXPORT_SYMBOL_GPL(snd_soc_component_nc_pin);

int snd_soc_component_nc_pin_unlocked(struct snd_soc_component *component,
				      const char *pin)
{
191
	return soc_component_pin(component, pin, snd_soc_dapm_nc_pin_unlocked);
K
Kuninori Morimoto 已提交
192 193 194 195 196 197
}
EXPORT_SYMBOL_GPL(snd_soc_component_nc_pin_unlocked);

int snd_soc_component_get_pin_status(struct snd_soc_component *component,
				     const char *pin)
{
198
	return soc_component_pin(component, pin, snd_soc_dapm_get_pin_status);
K
Kuninori Morimoto 已提交
199 200 201 202 203 204
}
EXPORT_SYMBOL_GPL(snd_soc_component_get_pin_status);

int snd_soc_component_force_enable_pin(struct snd_soc_component *component,
				       const char *pin)
{
205
	return soc_component_pin(component, pin, snd_soc_dapm_force_enable_pin);
K
Kuninori Morimoto 已提交
206 207 208 209 210 211 212
}
EXPORT_SYMBOL_GPL(snd_soc_component_force_enable_pin);

int snd_soc_component_force_enable_pin_unlocked(
	struct snd_soc_component *component,
	const char *pin)
{
213
	return soc_component_pin(component, pin, snd_soc_dapm_force_enable_pin_unlocked);
K
Kuninori Morimoto 已提交
214 215 216 217 218 219 220 221 222 223 224 225 226 227
}
EXPORT_SYMBOL_GPL(snd_soc_component_force_enable_pin_unlocked);

/**
 * snd_soc_component_set_jack - configure component jack.
 * @component: COMPONENTs
 * @jack: structure to use for the jack
 * @data: can be used if codec driver need extra data for configuring jack
 *
 * Configures and enables jack detection function.
 */
int snd_soc_component_set_jack(struct snd_soc_component *component,
			       struct snd_soc_jack *jack, void *data)
{
228 229
	int ret = -ENOTSUPP;

K
Kuninori Morimoto 已提交
230
	if (component->driver->set_jack)
231
		ret = component->driver->set_jack(component, jack, data);
K
Kuninori Morimoto 已提交
232

233
	return soc_component_ret(component, ret);
K
Kuninori Morimoto 已提交
234 235
}
EXPORT_SYMBOL_GPL(snd_soc_component_set_jack);
236 237 238 239

int snd_soc_component_module_get(struct snd_soc_component *component,
				 int upon_open)
{
240 241
	int ret = 0;

242 243
	if (component->driver->module_get_upon_open == !!upon_open &&
	    !try_module_get(component->dev->driver->owner))
244
		ret = -ENODEV;
245

246
	return soc_component_ret(component, ret);
247 248 249 250 251 252 253 254
}

void snd_soc_component_module_put(struct snd_soc_component *component,
				  int upon_open)
{
	if (component->driver->module_get_upon_open == !!upon_open)
		module_put(component->dev->driver->owner);
}
255 256 257 258

int snd_soc_component_open(struct snd_soc_component *component,
			   struct snd_pcm_substream *substream)
{
259 260
	int ret = 0;

261
	if (component->driver->open)
262 263 264
		ret = component->driver->open(component, substream);

	return soc_component_ret(component, ret);
265
}
266 267 268 269

int snd_soc_component_close(struct snd_soc_component *component,
			    struct snd_pcm_substream *substream)
{
270 271
	int ret = 0;

272
	if (component->driver->close)
273 274 275
		ret = component->driver->close(component, substream);

	return soc_component_ret(component, ret);
276
}
277 278 279 280

int snd_soc_component_prepare(struct snd_soc_component *component,
			      struct snd_pcm_substream *substream)
{
281 282
	int ret = 0;

283
	if (component->driver->prepare)
284 285 286
		ret = component->driver->prepare(component, substream);

	return soc_component_ret(component, ret);
287
}
288 289 290 291 292

int snd_soc_component_hw_params(struct snd_soc_component *component,
				struct snd_pcm_substream *substream,
				struct snd_pcm_hw_params *params)
{
293 294
	int ret = 0;

295
	if (component->driver->hw_params)
296 297 298 299
		ret = component->driver->hw_params(component,
						   substream, params);

	return soc_component_ret(component, ret);
300
}
301 302 303 304

int snd_soc_component_hw_free(struct snd_soc_component *component,
			       struct snd_pcm_substream *substream)
{
305 306
	int ret = 0;

307
	if (component->driver->hw_free)
308 309 310
		ret = component->driver->hw_free(component, substream);

	return soc_component_ret(component, ret);
311
}
312 313 314 315 316

int snd_soc_component_trigger(struct snd_soc_component *component,
			      struct snd_pcm_substream *substream,
			      int cmd)
{
317 318
	int ret = 0;

319
	if (component->driver->trigger)
320 321 322
		ret = component->driver->trigger(component, substream, cmd);

	return soc_component_ret(component, ret);
323
}
324 325 326 327 328 329 330

void snd_soc_component_suspend(struct snd_soc_component *component)
{
	if (component->driver->suspend)
		component->driver->suspend(component);
	component->suspended = 1;
}
331 332 333 334 335 336 337

void snd_soc_component_resume(struct snd_soc_component *component)
{
	if (component->driver->resume)
		component->driver->resume(component);
	component->suspended = 0;
}
338 339 340 341 342

int snd_soc_component_is_suspended(struct snd_soc_component *component)
{
	return component->suspended;
}
343 344 345

int snd_soc_component_probe(struct snd_soc_component *component)
{
346 347
	int ret = 0;

348
	if (component->driver->probe)
349
		ret = component->driver->probe(component);
350

351
	return soc_component_ret(component, ret);
352
}
353 354 355 356 357 358

void snd_soc_component_remove(struct snd_soc_component *component)
{
	if (component->driver->remove)
		component->driver->remove(component);
}
359 360 361 362

int snd_soc_component_of_xlate_dai_id(struct snd_soc_component *component,
				      struct device_node *ep)
{
363 364
	int ret = -ENOTSUPP;

365
	if (component->driver->of_xlate_dai_id)
366
		ret = component->driver->of_xlate_dai_id(component, ep);
367

368
	return soc_component_ret(component, ret);
369
}
370 371 372 373 374

int snd_soc_component_of_xlate_dai_name(struct snd_soc_component *component,
					struct of_phandle_args *args,
					const char **dai_name)
{
375 376
	int ret = -ENOTSUPP;

377
	if (component->driver->of_xlate_dai_name)
378 379 380 381
		ret = component->driver->of_xlate_dai_name(component,
							   args, dai_name);

	return soc_component_ret(component, ret);
382
}
383

384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433
void snd_soc_component_setup_regmap(struct snd_soc_component *component)
{
	int val_bytes = regmap_get_val_bytes(component->regmap);

	/* Errors are legitimate for non-integer byte multiples */
	if (val_bytes > 0)
		component->val_bytes = val_bytes;
}

#ifdef CONFIG_REGMAP

/**
 * snd_soc_component_init_regmap() - Initialize regmap instance for the
 *                                   component
 * @component: The component for which to initialize the regmap instance
 * @regmap: The regmap instance that should be used by the component
 *
 * This function allows deferred assignment of the regmap instance that is
 * associated with the component. Only use this if the regmap instance is not
 * yet ready when the component is registered. The function must also be called
 * before the first IO attempt of the component.
 */
void snd_soc_component_init_regmap(struct snd_soc_component *component,
				   struct regmap *regmap)
{
	component->regmap = regmap;
	snd_soc_component_setup_regmap(component);
}
EXPORT_SYMBOL_GPL(snd_soc_component_init_regmap);

/**
 * snd_soc_component_exit_regmap() - De-initialize regmap instance for the
 *                                   component
 * @component: The component for which to de-initialize the regmap instance
 *
 * Calls regmap_exit() on the regmap instance associated to the component and
 * removes the regmap instance from the component.
 *
 * This function should only be used if snd_soc_component_init_regmap() was used
 * to initialize the regmap instance.
 */
void snd_soc_component_exit_regmap(struct snd_soc_component *component)
{
	regmap_exit(component->regmap);
	component->regmap = NULL;
}
EXPORT_SYMBOL_GPL(snd_soc_component_exit_regmap);

#endif

434 435 436 437
int snd_soc_pcm_component_pointer(struct snd_pcm_substream *substream)
{
	struct snd_soc_pcm_runtime *rtd = substream->private_data;
	struct snd_soc_component *component;
438
	int i;
439

440
	/* FIXME: use 1st pointer */
441
	for_each_rtd_components(rtd, i, component)
442 443
		if (component->driver->pointer)
			return component->driver->pointer(component, substream);
444 445 446

	return 0;
}
447 448 449 450 451 452

int snd_soc_pcm_component_ioctl(struct snd_pcm_substream *substream,
				unsigned int cmd, void *arg)
{
	struct snd_soc_pcm_runtime *rtd = substream->private_data;
	struct snd_soc_component *component;
453
	int i;
454

455
	/* FIXME: use 1st ioctl */
456
	for_each_rtd_components(rtd, i, component)
457
		if (component->driver->ioctl)
458 459 460 461
			return soc_component_ret(
				component,
				component->driver->ioctl(component,
							 substream, cmd, arg));
462 463 464

	return snd_pcm_lib_ioctl(substream, cmd, arg);
}
465

466 467 468 469
int snd_soc_pcm_component_sync_stop(struct snd_pcm_substream *substream)
{
	struct snd_soc_pcm_runtime *rtd = substream->private_data;
	struct snd_soc_component *component;
470
	int i, ret;
471

472
	for_each_rtd_components(rtd, i, component) {
473
		if (component->driver->sync_stop) {
474 475 476
			ret = component->driver->sync_stop(component,
							   substream);
			if (ret < 0)
477
				soc_component_ret(component, ret);
478 479 480 481 482 483
		}
	}

	return 0;
}

484 485 486 487 488 489
int snd_soc_pcm_component_copy_user(struct snd_pcm_substream *substream,
				    int channel, unsigned long pos,
				    void __user *buf, unsigned long bytes)
{
	struct snd_soc_pcm_runtime *rtd = substream->private_data;
	struct snd_soc_component *component;
490
	int i;
491

492
	/* FIXME. it returns 1st copy now */
493
	for_each_rtd_components(rtd, i, component)
494
		if (component->driver->copy_user)
495 496 497 498 499
			return soc_component_ret(
				component,
				component->driver->copy_user(
					component, substream, channel,
					pos, buf, bytes));
500 501 502

	return -EINVAL;
}
503 504 505 506 507 508 509

struct page *snd_soc_pcm_component_page(struct snd_pcm_substream *substream,
					unsigned long offset)
{
	struct snd_soc_pcm_runtime *rtd = substream->private_data;
	struct snd_soc_component *component;
	struct page *page;
510
	int i;
511

512
	/* FIXME. it returns 1st page now */
513
	for_each_rtd_components(rtd, i, component) {
514 515 516 517 518 519
		if (component->driver->page) {
			page = component->driver->page(component,
						       substream, offset);
			if (page)
				return page;
		}
520 521 522 523
	}

	return NULL;
}
524 525 526 527 528 529

int snd_soc_pcm_component_mmap(struct snd_pcm_substream *substream,
			       struct vm_area_struct *vma)
{
	struct snd_soc_pcm_runtime *rtd = substream->private_data;
	struct snd_soc_component *component;
530
	int i;
531

532
	/* FIXME. it returns 1st mmap now */
533
	for_each_rtd_components(rtd, i, component)
534
		if (component->driver->mmap)
535 536 537 538
			soc_component_ret(
				component,
				component->driver->mmap(component,
							substream, vma));
539 540 541

	return -EINVAL;
}
542

543
int snd_soc_pcm_component_new(struct snd_soc_pcm_runtime *rtd)
544 545 546
{
	struct snd_soc_component *component;
	int ret;
547
	int i;
548

549
	for_each_rtd_components(rtd, i, component) {
550 551 552
		if (component->driver->pcm_construct) {
			ret = component->driver->pcm_construct(component, rtd);
			if (ret < 0)
553
				soc_component_ret(component, ret);
554
		}
555 556 557 558
	}

	return 0;
}
559

560
void snd_soc_pcm_component_free(struct snd_soc_pcm_runtime *rtd)
561 562
{
	struct snd_soc_component *component;
563
	int i;
564

565 566 567
	if (!rtd->pcm)
		return;

568
	for_each_rtd_components(rtd, i, component)
569
		if (component->driver->pcm_destruct)
570
			component->driver->pcm_destruct(component, rtd->pcm);
571
}