soc-component.c 15.3 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_hw_free(struct snd_soc_component *component,
			       struct snd_pcm_substream *substream)
{
281 282
	int ret = 0;

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

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

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

295
	if (component->driver->trigger)
296 297 298
		ret = component->driver->trigger(component, substream, cmd);

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

void snd_soc_component_suspend(struct snd_soc_component *component)
{
	if (component->driver->suspend)
		component->driver->suspend(component);
	component->suspended = 1;
}
307 308 309 310 311 312 313

void snd_soc_component_resume(struct snd_soc_component *component)
{
	if (component->driver->resume)
		component->driver->resume(component);
	component->suspended = 0;
}
314 315 316 317 318

int snd_soc_component_is_suspended(struct snd_soc_component *component)
{
	return component->suspended;
}
319 320 321

int snd_soc_component_probe(struct snd_soc_component *component)
{
322 323
	int ret = 0;

324
	if (component->driver->probe)
325
		ret = component->driver->probe(component);
326

327
	return soc_component_ret(component, ret);
328
}
329 330 331 332 333 334

void snd_soc_component_remove(struct snd_soc_component *component)
{
	if (component->driver->remove)
		component->driver->remove(component);
}
335 336 337 338

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

341
	if (component->driver->of_xlate_dai_id)
342
		ret = component->driver->of_xlate_dai_id(component, ep);
343

344
	return soc_component_ret(component, ret);
345
}
346 347 348 349 350

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

353
	if (component->driver->of_xlate_dai_name)
354 355 356 357
		ret = component->driver->of_xlate_dai_name(component,
							   args, dai_name);

	return soc_component_ret(component, ret);
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 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409
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

410 411 412 413
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;
414
	int i;
415

416
	/* FIXME: use 1st pointer */
417
	for_each_rtd_components(rtd, i, component)
418 419
		if (component->driver->pointer)
			return component->driver->pointer(component, substream);
420 421 422

	return 0;
}
423 424 425 426 427 428

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;
429
	int i;
430

431
	/* FIXME: use 1st ioctl */
432
	for_each_rtd_components(rtd, i, component)
433
		if (component->driver->ioctl)
434 435 436 437
			return soc_component_ret(
				component,
				component->driver->ioctl(component,
							 substream, cmd, arg));
438 439 440

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

442 443 444 445
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;
446
	int i, ret;
447

448
	for_each_rtd_components(rtd, i, component) {
449
		if (component->driver->sync_stop) {
450 451 452
			ret = component->driver->sync_stop(component,
							   substream);
			if (ret < 0)
453
				soc_component_ret(component, ret);
454 455 456 457 458 459
		}
	}

	return 0;
}

460 461 462 463 464 465
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;
466
	int i;
467

468
	/* FIXME. it returns 1st copy now */
469
	for_each_rtd_components(rtd, i, component)
470
		if (component->driver->copy_user)
471 472 473 474 475
			return soc_component_ret(
				component,
				component->driver->copy_user(
					component, substream, channel,
					pos, buf, bytes));
476 477 478

	return -EINVAL;
}
479 480 481 482 483 484 485

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;
486
	int i;
487

488
	/* FIXME. it returns 1st page now */
489
	for_each_rtd_components(rtd, i, component) {
490 491 492 493 494 495
		if (component->driver->page) {
			page = component->driver->page(component,
						       substream, offset);
			if (page)
				return page;
		}
496 497 498 499
	}

	return NULL;
}
500 501 502 503 504 505

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;
506
	int i;
507

508
	/* FIXME. it returns 1st mmap now */
509
	for_each_rtd_components(rtd, i, component)
510
		if (component->driver->mmap)
511 512 513 514
			soc_component_ret(
				component,
				component->driver->mmap(component,
							substream, vma));
515 516 517

	return -EINVAL;
}
518

519
int snd_soc_pcm_component_new(struct snd_soc_pcm_runtime *rtd)
520 521 522
{
	struct snd_soc_component *component;
	int ret;
523
	int i;
524

525
	for_each_rtd_components(rtd, i, component) {
526 527 528
		if (component->driver->pcm_construct) {
			ret = component->driver->pcm_construct(component, rtd);
			if (ret < 0)
529
				soc_component_ret(component, ret);
530
		}
531 532 533 534
	}

	return 0;
}
535

536
void snd_soc_pcm_component_free(struct snd_soc_pcm_runtime *rtd)
537 538
{
	struct snd_soc_component *component;
539
	int i;
540

541 542 543
	if (!rtd->pcm)
		return;

544
	for_each_rtd_components(rtd, i, component)
545
		if (component->driver->pcm_destruct)
546
			component->driver->pcm_destruct(component, rtd->pcm);
547
}
548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564

int snd_soc_pcm_component_prepare(struct snd_pcm_substream *substream)
{
	struct snd_soc_pcm_runtime *rtd = substream->private_data;
	struct snd_soc_component *component;
	int i, ret;

	for_each_rtd_components(rtd, i, component) {
		if (component->driver->prepare) {
			ret = component->driver->prepare(component, substream);
			if (ret < 0)
				return soc_component_ret(component, ret);
		}
	}

	return 0;
}
565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587

int snd_soc_pcm_component_hw_params(struct snd_pcm_substream *substream,
				    struct snd_pcm_hw_params *params,
				    struct snd_soc_component **last)
{
	struct snd_soc_pcm_runtime *rtd = substream->private_data;
	struct snd_soc_component *component;
	int i, ret;

	for_each_rtd_components(rtd, i, component) {
		if (component->driver->hw_params) {
			ret = component->driver->hw_params(component,
							   substream, params);
			if (ret < 0) {
				*last = component;
				return soc_component_ret(component, ret);
			}
		}
	}

	*last = NULL;
	return 0;
}