Andrey Smirnov | 06d7c13 | 2013-02-18 19:59:35 -0800 | [diff] [blame] | 1 | /* |
| 2 | * sound/soc/codecs/si476x.c -- Codec driver for SI476X chips |
| 3 | * |
| 4 | * Copyright (C) 2012 Innovative Converged Devices(ICD) |
| 5 | * Copyright (C) 2013 Andrey Smirnov |
| 6 | * |
| 7 | * Author: Andrey Smirnov <andrew.smirnov@gmail.com> |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License as published by |
| 11 | * the Free Software Foundation; version 2 of the License. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, but |
| 14 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 16 | * General Public License for more details. |
| 17 | * |
| 18 | */ |
| 19 | |
Andrey Smirnov | 330345e | 2012-10-05 18:55:02 -0700 | [diff] [blame] | 20 | #include <linux/module.h> |
| 21 | #include <linux/slab.h> |
| 22 | #include <sound/pcm.h> |
| 23 | #include <sound/pcm_params.h> |
| 24 | #include <sound/soc.h> |
| 25 | #include <sound/initval.h> |
| 26 | |
| 27 | #include <linux/i2c.h> |
| 28 | |
| 29 | #include <linux/mfd/si476x-core.h> |
| 30 | |
| 31 | enum si476x_audio_registers { |
| 32 | SI476X_DIGITAL_IO_OUTPUT_FORMAT = 0x0203, |
| 33 | SI476X_DIGITAL_IO_OUTPUT_SAMPLE_RATE = 0x0202, |
| 34 | }; |
| 35 | |
| 36 | enum si476x_digital_io_output_format { |
| 37 | SI476X_DIGITAL_IO_SLOT_SIZE_SHIFT = 11, |
| 38 | SI476X_DIGITAL_IO_SAMPLE_SIZE_SHIFT = 8, |
| 39 | }; |
| 40 | |
Andrew Morton | 3b1f9f5 | 2013-07-03 15:00:44 -0700 | [diff] [blame] | 41 | #define SI476X_DIGITAL_IO_OUTPUT_WIDTH_MASK ((0x7 << SI476X_DIGITAL_IO_SLOT_SIZE_SHIFT) | \ |
| 42 | (0x7 << SI476X_DIGITAL_IO_SAMPLE_SIZE_SHIFT)) |
| 43 | #define SI476X_DIGITAL_IO_OUTPUT_FORMAT_MASK (0x7e) |
Andrey Smirnov | 330345e | 2012-10-05 18:55:02 -0700 | [diff] [blame] | 44 | |
| 45 | enum si476x_daudio_formats { |
| 46 | SI476X_DAUDIO_MODE_I2S = (0x0 << 1), |
| 47 | SI476X_DAUDIO_MODE_DSP_A = (0x6 << 1), |
| 48 | SI476X_DAUDIO_MODE_DSP_B = (0x7 << 1), |
| 49 | SI476X_DAUDIO_MODE_LEFT_J = (0x8 << 1), |
| 50 | SI476X_DAUDIO_MODE_RIGHT_J = (0x9 << 1), |
| 51 | |
| 52 | SI476X_DAUDIO_MODE_IB = (1 << 5), |
| 53 | SI476X_DAUDIO_MODE_IF = (1 << 6), |
| 54 | }; |
| 55 | |
| 56 | enum si476x_pcm_format { |
| 57 | SI476X_PCM_FORMAT_S8 = 2, |
| 58 | SI476X_PCM_FORMAT_S16_LE = 4, |
| 59 | SI476X_PCM_FORMAT_S20_3LE = 5, |
| 60 | SI476X_PCM_FORMAT_S24_LE = 6, |
| 61 | }; |
| 62 | |
Mark Brown | ac0b82b | 2013-08-16 11:54:51 +0100 | [diff] [blame] | 63 | static const struct snd_soc_dapm_widget si476x_dapm_widgets[] = { |
| 64 | SND_SOC_DAPM_OUTPUT("LOUT"), |
| 65 | SND_SOC_DAPM_OUTPUT("ROUT"), |
| 66 | }; |
| 67 | |
| 68 | static const struct snd_soc_dapm_route si476x_dapm_routes[] = { |
| 69 | { "Capture", NULL, "LOUT" }, |
| 70 | { "Capture", NULL, "ROUT" }, |
| 71 | }; |
| 72 | |
Andrey Smirnov | 330345e | 2012-10-05 18:55:02 -0700 | [diff] [blame] | 73 | static int si476x_codec_set_dai_fmt(struct snd_soc_dai *codec_dai, |
| 74 | unsigned int fmt) |
| 75 | { |
Mark Brown | cfcff69 | 2013-10-20 18:14:20 +0100 | [diff] [blame^] | 76 | struct si476x_core *core = i2c_mfd_cell_to_core(codec_dai->dev); |
Andrey Smirnov | 330345e | 2012-10-05 18:55:02 -0700 | [diff] [blame] | 77 | int err; |
| 78 | u16 format = 0; |
| 79 | |
| 80 | if ((fmt & SND_SOC_DAIFMT_MASTER_MASK) != SND_SOC_DAIFMT_CBS_CFS) |
| 81 | return -EINVAL; |
| 82 | |
| 83 | switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) { |
| 84 | case SND_SOC_DAIFMT_DSP_A: |
| 85 | format |= SI476X_DAUDIO_MODE_DSP_A; |
| 86 | break; |
| 87 | case SND_SOC_DAIFMT_DSP_B: |
| 88 | format |= SI476X_DAUDIO_MODE_DSP_B; |
| 89 | break; |
| 90 | case SND_SOC_DAIFMT_I2S: |
| 91 | format |= SI476X_DAUDIO_MODE_I2S; |
| 92 | break; |
| 93 | case SND_SOC_DAIFMT_RIGHT_J: |
| 94 | format |= SI476X_DAUDIO_MODE_RIGHT_J; |
| 95 | break; |
| 96 | case SND_SOC_DAIFMT_LEFT_J: |
| 97 | format |= SI476X_DAUDIO_MODE_LEFT_J; |
| 98 | break; |
| 99 | default: |
| 100 | return -EINVAL; |
| 101 | } |
| 102 | |
| 103 | switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) { |
| 104 | case SND_SOC_DAIFMT_DSP_A: |
| 105 | case SND_SOC_DAIFMT_DSP_B: |
| 106 | switch (fmt & SND_SOC_DAIFMT_INV_MASK) { |
| 107 | case SND_SOC_DAIFMT_NB_NF: |
| 108 | break; |
| 109 | case SND_SOC_DAIFMT_IB_NF: |
| 110 | format |= SI476X_DAUDIO_MODE_IB; |
| 111 | break; |
| 112 | default: |
| 113 | return -EINVAL; |
| 114 | } |
| 115 | break; |
| 116 | case SND_SOC_DAIFMT_I2S: |
| 117 | case SND_SOC_DAIFMT_RIGHT_J: |
| 118 | case SND_SOC_DAIFMT_LEFT_J: |
| 119 | switch (fmt & SND_SOC_DAIFMT_INV_MASK) { |
| 120 | case SND_SOC_DAIFMT_NB_NF: |
| 121 | break; |
| 122 | case SND_SOC_DAIFMT_IB_IF: |
| 123 | format |= SI476X_DAUDIO_MODE_IB | |
| 124 | SI476X_DAUDIO_MODE_IF; |
| 125 | break; |
| 126 | case SND_SOC_DAIFMT_IB_NF: |
| 127 | format |= SI476X_DAUDIO_MODE_IB; |
| 128 | break; |
| 129 | case SND_SOC_DAIFMT_NB_IF: |
| 130 | format |= SI476X_DAUDIO_MODE_IF; |
| 131 | break; |
| 132 | default: |
| 133 | return -EINVAL; |
| 134 | } |
| 135 | break; |
| 136 | default: |
| 137 | return -EINVAL; |
| 138 | } |
| 139 | |
Mark Brown | cfcff69 | 2013-10-20 18:14:20 +0100 | [diff] [blame^] | 140 | si476x_core_lock(core); |
| 141 | |
Andrey Smirnov | 330345e | 2012-10-05 18:55:02 -0700 | [diff] [blame] | 142 | err = snd_soc_update_bits(codec_dai->codec, SI476X_DIGITAL_IO_OUTPUT_FORMAT, |
| 143 | SI476X_DIGITAL_IO_OUTPUT_FORMAT_MASK, |
| 144 | format); |
Mark Brown | cfcff69 | 2013-10-20 18:14:20 +0100 | [diff] [blame^] | 145 | |
| 146 | si476x_core_unlock(core); |
| 147 | |
Andrey Smirnov | 330345e | 2012-10-05 18:55:02 -0700 | [diff] [blame] | 148 | if (err < 0) { |
| 149 | dev_err(codec_dai->codec->dev, "Failed to set output format\n"); |
| 150 | return err; |
| 151 | } |
Andrey Smirnov | 06d7c13 | 2013-02-18 19:59:35 -0800 | [diff] [blame] | 152 | |
Andrey Smirnov | 330345e | 2012-10-05 18:55:02 -0700 | [diff] [blame] | 153 | return 0; |
| 154 | } |
| 155 | |
| 156 | static int si476x_codec_hw_params(struct snd_pcm_substream *substream, |
| 157 | struct snd_pcm_hw_params *params, |
| 158 | struct snd_soc_dai *dai) |
| 159 | { |
Mark Brown | cfcff69 | 2013-10-20 18:14:20 +0100 | [diff] [blame^] | 160 | struct si476x_core *core = i2c_mfd_cell_to_core(dai->dev); |
Andrey Smirnov | 330345e | 2012-10-05 18:55:02 -0700 | [diff] [blame] | 161 | int rate, width, err; |
| 162 | |
| 163 | rate = params_rate(params); |
| 164 | if (rate < 32000 || rate > 48000) { |
| 165 | dev_err(dai->codec->dev, "Rate: %d is not supported\n", rate); |
| 166 | return -EINVAL; |
| 167 | } |
| 168 | |
| 169 | switch (params_format(params)) { |
| 170 | case SNDRV_PCM_FORMAT_S8: |
| 171 | width = SI476X_PCM_FORMAT_S8; |
Axel Lin | 4e38b74 | 2012-10-31 16:07:43 +0800 | [diff] [blame] | 172 | break; |
Andrey Smirnov | 330345e | 2012-10-05 18:55:02 -0700 | [diff] [blame] | 173 | case SNDRV_PCM_FORMAT_S16_LE: |
| 174 | width = SI476X_PCM_FORMAT_S16_LE; |
| 175 | break; |
| 176 | case SNDRV_PCM_FORMAT_S20_3LE: |
| 177 | width = SI476X_PCM_FORMAT_S20_3LE; |
| 178 | break; |
| 179 | case SNDRV_PCM_FORMAT_S24_LE: |
| 180 | width = SI476X_PCM_FORMAT_S24_LE; |
| 181 | break; |
| 182 | default: |
| 183 | return -EINVAL; |
| 184 | } |
| 185 | |
Mark Brown | cfcff69 | 2013-10-20 18:14:20 +0100 | [diff] [blame^] | 186 | si476x_core_lock(core); |
| 187 | |
Andrey Smirnov | 330345e | 2012-10-05 18:55:02 -0700 | [diff] [blame] | 188 | err = snd_soc_write(dai->codec, SI476X_DIGITAL_IO_OUTPUT_SAMPLE_RATE, |
| 189 | rate); |
| 190 | if (err < 0) { |
| 191 | dev_err(dai->codec->dev, "Failed to set sample rate\n"); |
Mark Brown | cfcff69 | 2013-10-20 18:14:20 +0100 | [diff] [blame^] | 192 | goto out; |
Andrey Smirnov | 330345e | 2012-10-05 18:55:02 -0700 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | err = snd_soc_update_bits(dai->codec, SI476X_DIGITAL_IO_OUTPUT_FORMAT, |
| 196 | SI476X_DIGITAL_IO_OUTPUT_WIDTH_MASK, |
Andrey Smirnov | 06d7c13 | 2013-02-18 19:59:35 -0800 | [diff] [blame] | 197 | (width << SI476X_DIGITAL_IO_SLOT_SIZE_SHIFT) | |
Andrey Smirnov | 330345e | 2012-10-05 18:55:02 -0700 | [diff] [blame] | 198 | (width << SI476X_DIGITAL_IO_SAMPLE_SIZE_SHIFT)); |
| 199 | if (err < 0) { |
| 200 | dev_err(dai->codec->dev, "Failed to set output width\n"); |
Mark Brown | cfcff69 | 2013-10-20 18:14:20 +0100 | [diff] [blame^] | 201 | goto out; |
Andrey Smirnov | 330345e | 2012-10-05 18:55:02 -0700 | [diff] [blame] | 202 | } |
| 203 | |
Mark Brown | cfcff69 | 2013-10-20 18:14:20 +0100 | [diff] [blame^] | 204 | out: |
| 205 | si476x_core_unlock(core); |
| 206 | |
| 207 | return err; |
Andrey Smirnov | 330345e | 2012-10-05 18:55:02 -0700 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | static int si476x_codec_probe(struct snd_soc_codec *codec) |
| 211 | { |
Mark Brown | d6173df | 2013-09-26 19:36:11 +0100 | [diff] [blame] | 212 | codec->control_data = dev_get_regmap(codec->dev->parent, NULL); |
Andrey Smirnov | 330345e | 2012-10-05 18:55:02 -0700 | [diff] [blame] | 213 | return 0; |
| 214 | } |
| 215 | |
| 216 | static struct snd_soc_dai_ops si476x_dai_ops = { |
| 217 | .hw_params = si476x_codec_hw_params, |
| 218 | .set_fmt = si476x_codec_set_dai_fmt, |
| 219 | }; |
| 220 | |
| 221 | static struct snd_soc_dai_driver si476x_dai = { |
| 222 | .name = "si476x-codec", |
| 223 | .capture = { |
| 224 | .stream_name = "Capture", |
| 225 | .channels_min = 2, |
| 226 | .channels_max = 2, |
| 227 | |
| 228 | .rates = SNDRV_PCM_RATE_32000 | |
| 229 | SNDRV_PCM_RATE_44100 | |
| 230 | SNDRV_PCM_RATE_48000, |
| 231 | .formats = SNDRV_PCM_FMTBIT_S8 | |
| 232 | SNDRV_PCM_FMTBIT_S16_LE | |
| 233 | SNDRV_PCM_FMTBIT_S20_3LE | |
| 234 | SNDRV_PCM_FMTBIT_S24_LE |
| 235 | }, |
| 236 | .ops = &si476x_dai_ops, |
| 237 | }; |
| 238 | |
| 239 | static struct snd_soc_codec_driver soc_codec_dev_si476x = { |
| 240 | .probe = si476x_codec_probe, |
Mark Brown | ac0b82b | 2013-08-16 11:54:51 +0100 | [diff] [blame] | 241 | .dapm_widgets = si476x_dapm_widgets, |
| 242 | .num_dapm_widgets = ARRAY_SIZE(si476x_dapm_widgets), |
| 243 | .dapm_routes = si476x_dapm_routes, |
| 244 | .num_dapm_routes = ARRAY_SIZE(si476x_dapm_routes), |
Andrey Smirnov | 330345e | 2012-10-05 18:55:02 -0700 | [diff] [blame] | 245 | }; |
| 246 | |
Bill Pemberton | 7a79e94 | 2012-12-07 09:26:37 -0500 | [diff] [blame] | 247 | static int si476x_platform_probe(struct platform_device *pdev) |
Andrey Smirnov | 330345e | 2012-10-05 18:55:02 -0700 | [diff] [blame] | 248 | { |
| 249 | return snd_soc_register_codec(&pdev->dev, &soc_codec_dev_si476x, |
| 250 | &si476x_dai, 1); |
| 251 | } |
| 252 | |
Bill Pemberton | 7a79e94 | 2012-12-07 09:26:37 -0500 | [diff] [blame] | 253 | static int si476x_platform_remove(struct platform_device *pdev) |
Andrey Smirnov | 330345e | 2012-10-05 18:55:02 -0700 | [diff] [blame] | 254 | { |
| 255 | snd_soc_unregister_codec(&pdev->dev); |
| 256 | return 0; |
| 257 | } |
| 258 | |
| 259 | MODULE_ALIAS("platform:si476x-codec"); |
| 260 | |
| 261 | static struct platform_driver si476x_platform_driver = { |
| 262 | .driver = { |
| 263 | .name = "si476x-codec", |
| 264 | .owner = THIS_MODULE, |
| 265 | }, |
| 266 | .probe = si476x_platform_probe, |
Bill Pemberton | 7a79e94 | 2012-12-07 09:26:37 -0500 | [diff] [blame] | 267 | .remove = si476x_platform_remove, |
Andrey Smirnov | 330345e | 2012-10-05 18:55:02 -0700 | [diff] [blame] | 268 | }; |
| 269 | module_platform_driver(si476x_platform_driver); |
| 270 | |
Andrey Smirnov | 06d7c13 | 2013-02-18 19:59:35 -0800 | [diff] [blame] | 271 | MODULE_AUTHOR("Andrey Smirnov <andrew.smirnov@gmail.com>"); |
Andrey Smirnov | 330345e | 2012-10-05 18:55:02 -0700 | [diff] [blame] | 272 | MODULE_DESCRIPTION("ASoC Si4761/64 codec driver"); |
| 273 | MODULE_LICENSE("GPL"); |