blob: 07f43356f9639c408f025ff37fafdf5f323c06d5 [file] [log] [blame]
Mark Brown17a52fd2009-07-05 17:24:50 +01001/*
2 * soc-cache.c -- ASoC register cache helpers
3 *
4 * Copyright 2009 Wolfson Microelectronics PLC.
5 *
6 * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
7 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2 of the License, or (at your
11 * option) any later version.
12 */
13
14#include <sound/soc.h>
Paul Gortmakerd81a6d72011-09-22 09:34:58 -040015#include <linux/export.h>
Lars-Peter Clausenf90fb3f2013-08-31 20:31:15 +020016#include <linux/slab.h>
Mark Brown17a52fd2009-07-05 17:24:50 +010017
Lars-Peter Clausenf90fb3f2013-08-31 20:31:15 +020018int snd_soc_cache_init(struct snd_soc_codec *codec)
19{
20 const struct snd_soc_codec_driver *codec_drv = codec->driver;
21 size_t reg_size;
22
23 reg_size = codec_drv->reg_cache_size * codec_drv->reg_word_size;
24
Xiubo Lib59dce52014-05-19 16:32:09 +080025 if (!reg_size)
Mark Brownb5fc40d2014-06-02 16:08:21 +010026 return 0;
Xiubo Lib59dce52014-05-19 16:32:09 +080027
Lars-Peter Clausenf90fb3f2013-08-31 20:31:15 +020028 dev_dbg(codec->dev, "ASoC: Initializing cache for %s codec\n",
Lars-Peter Clausenf4333202014-06-16 18:13:02 +020029 codec->component.name);
Lars-Peter Clausenf90fb3f2013-08-31 20:31:15 +020030
31 if (codec_drv->reg_cache_default)
32 codec->reg_cache = kmemdup(codec_drv->reg_cache_default,
33 reg_size, GFP_KERNEL);
34 else
35 codec->reg_cache = kzalloc(reg_size, GFP_KERNEL);
36 if (!codec->reg_cache)
37 return -ENOMEM;
38
39 return 0;
40}
41
42/*
43 * NOTE: keep in mind that this function might be called
44 * multiple times.
45 */
46int snd_soc_cache_exit(struct snd_soc_codec *codec)
47{
48 dev_dbg(codec->dev, "ASoC: Destroying cache for %s codec\n",
Lars-Peter Clausenf4333202014-06-16 18:13:02 +020049 codec->component.name);
Lars-Peter Clausenf90fb3f2013-08-31 20:31:15 +020050 kfree(codec->reg_cache);
51 codec->reg_cache = NULL;
52 return 0;
53}