Michael Trimarchi | 13b02fa | 2013-08-03 16:20:43 +0200 | [diff] [blame] | 1 | /* |
| 2 | * PCM1792A ASoC codec driver |
| 3 | * |
| 4 | * Copyright (c) Amarula Solutions B.V. 2013 |
| 5 | * |
| 6 | * Michael Trimarchi <michael@amarulasolutions.com> |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or |
| 9 | * modify it under the terms of the GNU General Public License |
| 10 | * as published by the Free Software Foundation; either version 2 |
| 11 | * of the License, or (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | */ |
| 18 | |
| 19 | #include <linux/module.h> |
| 20 | #include <linux/slab.h> |
| 21 | #include <linux/kernel.h> |
| 22 | #include <linux/device.h> |
| 23 | #include <linux/spi/spi.h> |
| 24 | |
| 25 | #include <sound/core.h> |
| 26 | #include <sound/pcm.h> |
| 27 | #include <sound/pcm_params.h> |
| 28 | #include <sound/initval.h> |
| 29 | #include <sound/soc.h> |
| 30 | #include <sound/tlv.h> |
Sachin Kamat | 4b2fa51 | 2013-10-11 17:23:58 +0530 | [diff] [blame] | 31 | #include <linux/of.h> |
Michael Trimarchi | 13b02fa | 2013-08-03 16:20:43 +0200 | [diff] [blame] | 32 | #include <linux/of_device.h> |
| 33 | |
| 34 | #include "pcm1792a.h" |
| 35 | |
| 36 | #define PCM1792A_DAC_VOL_LEFT 0x10 |
| 37 | #define PCM1792A_DAC_VOL_RIGHT 0x11 |
| 38 | #define PCM1792A_FMT_CONTROL 0x12 |
| 39 | #define PCM1792A_SOFT_MUTE PCM1792A_FMT_CONTROL |
| 40 | |
| 41 | #define PCM1792A_FMT_MASK 0x70 |
| 42 | #define PCM1792A_FMT_SHIFT 4 |
| 43 | #define PCM1792A_MUTE_MASK 0x01 |
| 44 | #define PCM1792A_MUTE_SHIFT 0 |
| 45 | #define PCM1792A_ATLD_ENABLE (1 << 7) |
| 46 | |
| 47 | static const struct reg_default pcm1792a_reg_defaults[] = { |
| 48 | { 0x10, 0xff }, |
| 49 | { 0x11, 0xff }, |
| 50 | { 0x12, 0x50 }, |
| 51 | { 0x13, 0x00 }, |
| 52 | { 0x14, 0x00 }, |
| 53 | { 0x15, 0x01 }, |
| 54 | { 0x16, 0x00 }, |
| 55 | { 0x17, 0x00 }, |
| 56 | }; |
| 57 | |
| 58 | static bool pcm1792a_accessible_reg(struct device *dev, unsigned int reg) |
| 59 | { |
| 60 | return reg >= 0x10 && reg <= 0x17; |
| 61 | } |
| 62 | |
| 63 | static bool pcm1792a_writeable_reg(struct device *dev, unsigned register reg) |
| 64 | { |
| 65 | bool accessible; |
| 66 | |
| 67 | accessible = pcm1792a_accessible_reg(dev, reg); |
| 68 | |
| 69 | return accessible && reg != 0x16 && reg != 0x17; |
| 70 | } |
| 71 | |
| 72 | struct pcm1792a_private { |
| 73 | struct regmap *regmap; |
| 74 | unsigned int format; |
| 75 | unsigned int rate; |
| 76 | }; |
| 77 | |
| 78 | static int pcm1792a_set_dai_fmt(struct snd_soc_dai *codec_dai, |
| 79 | unsigned int format) |
| 80 | { |
| 81 | struct snd_soc_codec *codec = codec_dai->codec; |
| 82 | struct pcm1792a_private *priv = snd_soc_codec_get_drvdata(codec); |
| 83 | |
| 84 | priv->format = format; |
| 85 | |
| 86 | return 0; |
| 87 | } |
| 88 | |
| 89 | static int pcm1792a_digital_mute(struct snd_soc_dai *dai, int mute) |
| 90 | { |
| 91 | struct snd_soc_codec *codec = dai->codec; |
| 92 | struct pcm1792a_private *priv = snd_soc_codec_get_drvdata(codec); |
| 93 | int ret; |
| 94 | |
| 95 | ret = regmap_update_bits(priv->regmap, PCM1792A_SOFT_MUTE, |
| 96 | PCM1792A_MUTE_MASK, !!mute); |
| 97 | if (ret < 0) |
| 98 | return ret; |
| 99 | |
| 100 | return 0; |
| 101 | } |
| 102 | |
| 103 | static int pcm1792a_hw_params(struct snd_pcm_substream *substream, |
| 104 | struct snd_pcm_hw_params *params, |
| 105 | struct snd_soc_dai *dai) |
| 106 | { |
| 107 | struct snd_soc_codec *codec = dai->codec; |
| 108 | struct pcm1792a_private *priv = snd_soc_codec_get_drvdata(codec); |
| 109 | int val = 0, ret; |
Michael Trimarchi | 13b02fa | 2013-08-03 16:20:43 +0200 | [diff] [blame] | 110 | |
| 111 | priv->rate = params_rate(params); |
| 112 | |
| 113 | switch (priv->format & SND_SOC_DAIFMT_FORMAT_MASK) { |
| 114 | case SND_SOC_DAIFMT_RIGHT_J: |
Mark Brown | e48c6d7 | 2014-01-24 16:07:16 +0000 | [diff] [blame] | 115 | switch (params_width(params)) { |
| 116 | case 24: |
| 117 | case 32: |
| 118 | val = 2; |
| 119 | break; |
| 120 | case 16: |
| 121 | val = 0; |
| 122 | break; |
| 123 | default: |
| 124 | return -EINVAL; |
| 125 | } |
Michael Trimarchi | 13b02fa | 2013-08-03 16:20:43 +0200 | [diff] [blame] | 126 | break; |
| 127 | case SND_SOC_DAIFMT_I2S: |
Mark Brown | e48c6d7 | 2014-01-24 16:07:16 +0000 | [diff] [blame] | 128 | switch (params_width(params)) { |
| 129 | case 24: |
| 130 | case 32: |
| 131 | val = 5; |
| 132 | break; |
| 133 | case 16: |
| 134 | val = 4; |
| 135 | break; |
| 136 | default: |
| 137 | return -EINVAL; |
| 138 | } |
Michael Trimarchi | 13b02fa | 2013-08-03 16:20:43 +0200 | [diff] [blame] | 139 | break; |
| 140 | default: |
| 141 | dev_err(codec->dev, "Invalid DAI format\n"); |
| 142 | return -EINVAL; |
| 143 | } |
| 144 | |
| 145 | val = val << PCM1792A_FMT_SHIFT | PCM1792A_ATLD_ENABLE; |
| 146 | |
| 147 | ret = regmap_update_bits(priv->regmap, PCM1792A_FMT_CONTROL, |
| 148 | PCM1792A_FMT_MASK | PCM1792A_ATLD_ENABLE, val); |
| 149 | if (ret < 0) |
| 150 | return ret; |
| 151 | |
| 152 | return 0; |
| 153 | } |
| 154 | |
| 155 | static const struct snd_soc_dai_ops pcm1792a_dai_ops = { |
| 156 | .set_fmt = pcm1792a_set_dai_fmt, |
| 157 | .hw_params = pcm1792a_hw_params, |
| 158 | .digital_mute = pcm1792a_digital_mute, |
| 159 | }; |
| 160 | |
| 161 | static const DECLARE_TLV_DB_SCALE(pcm1792a_dac_tlv, -12000, 50, 1); |
| 162 | |
| 163 | static const struct snd_kcontrol_new pcm1792a_controls[] = { |
| 164 | SOC_DOUBLE_R_RANGE_TLV("DAC Playback Volume", PCM1792A_DAC_VOL_LEFT, |
| 165 | PCM1792A_DAC_VOL_RIGHT, 0, 0xf, 0xff, 0, |
| 166 | pcm1792a_dac_tlv), |
| 167 | }; |
| 168 | |
Mark Brown | e7a5cb4 | 2013-08-13 18:30:00 +0100 | [diff] [blame] | 169 | static const struct snd_soc_dapm_widget pcm1792a_dapm_widgets[] = { |
| 170 | SND_SOC_DAPM_OUTPUT("IOUTL+"), |
| 171 | SND_SOC_DAPM_OUTPUT("IOUTL-"), |
| 172 | SND_SOC_DAPM_OUTPUT("IOUTR+"), |
| 173 | SND_SOC_DAPM_OUTPUT("IOUTR-"), |
| 174 | }; |
| 175 | |
| 176 | static const struct snd_soc_dapm_route pcm1792a_dapm_routes[] = { |
| 177 | { "IOUTL+", NULL, "Playback" }, |
| 178 | { "IOUTL-", NULL, "Playback" }, |
| 179 | { "IOUTR+", NULL, "Playback" }, |
| 180 | { "IOUTR-", NULL, "Playback" }, |
| 181 | }; |
| 182 | |
Michael Trimarchi | 13b02fa | 2013-08-03 16:20:43 +0200 | [diff] [blame] | 183 | static struct snd_soc_dai_driver pcm1792a_dai = { |
| 184 | .name = "pcm1792a-hifi", |
| 185 | .playback = { |
| 186 | .stream_name = "Playback", |
| 187 | .channels_min = 2, |
| 188 | .channels_max = 2, |
| 189 | .rates = PCM1792A_RATES, |
| 190 | .formats = PCM1792A_FORMATS, }, |
Michael Trimarchi | 13b02fa | 2013-08-03 16:20:43 +0200 | [diff] [blame] | 191 | .ops = &pcm1792a_dai_ops, |
| 192 | }; |
| 193 | |
Michael Trimarchi | 13b02fa | 2013-08-03 16:20:43 +0200 | [diff] [blame] | 194 | static const struct of_device_id pcm1792a_of_match[] = { |
| 195 | { .compatible = "ti,pcm1792a", }, |
| 196 | { } |
| 197 | }; |
| 198 | MODULE_DEVICE_TABLE(of, pcm1792a_of_match); |
Michael Trimarchi | 13b02fa | 2013-08-03 16:20:43 +0200 | [diff] [blame] | 199 | |
| 200 | static const struct regmap_config pcm1792a_regmap = { |
| 201 | .reg_bits = 8, |
| 202 | .val_bits = 8, |
Axel Lin | c92f66e | 2013-10-12 17:26:49 +0800 | [diff] [blame] | 203 | .max_register = 23, |
Michael Trimarchi | 13b02fa | 2013-08-03 16:20:43 +0200 | [diff] [blame] | 204 | .reg_defaults = pcm1792a_reg_defaults, |
| 205 | .num_reg_defaults = ARRAY_SIZE(pcm1792a_reg_defaults), |
| 206 | .writeable_reg = pcm1792a_writeable_reg, |
| 207 | .readable_reg = pcm1792a_accessible_reg, |
| 208 | }; |
| 209 | |
| 210 | static struct snd_soc_codec_driver soc_codec_dev_pcm1792a = { |
| 211 | .controls = pcm1792a_controls, |
| 212 | .num_controls = ARRAY_SIZE(pcm1792a_controls), |
Mark Brown | e7a5cb4 | 2013-08-13 18:30:00 +0100 | [diff] [blame] | 213 | .dapm_widgets = pcm1792a_dapm_widgets, |
| 214 | .num_dapm_widgets = ARRAY_SIZE(pcm1792a_dapm_widgets), |
| 215 | .dapm_routes = pcm1792a_dapm_routes, |
| 216 | .num_dapm_routes = ARRAY_SIZE(pcm1792a_dapm_routes), |
Michael Trimarchi | 13b02fa | 2013-08-03 16:20:43 +0200 | [diff] [blame] | 217 | }; |
| 218 | |
| 219 | static int pcm1792a_spi_probe(struct spi_device *spi) |
| 220 | { |
| 221 | struct pcm1792a_private *pcm1792a; |
| 222 | int ret; |
| 223 | |
| 224 | pcm1792a = devm_kzalloc(&spi->dev, sizeof(struct pcm1792a_private), |
| 225 | GFP_KERNEL); |
| 226 | if (!pcm1792a) |
| 227 | return -ENOMEM; |
| 228 | |
| 229 | spi_set_drvdata(spi, pcm1792a); |
| 230 | |
| 231 | pcm1792a->regmap = devm_regmap_init_spi(spi, &pcm1792a_regmap); |
| 232 | if (IS_ERR(pcm1792a->regmap)) { |
| 233 | ret = PTR_ERR(pcm1792a->regmap); |
| 234 | dev_err(&spi->dev, "Failed to register regmap: %d\n", ret); |
| 235 | return ret; |
| 236 | } |
| 237 | |
| 238 | return snd_soc_register_codec(&spi->dev, |
| 239 | &soc_codec_dev_pcm1792a, &pcm1792a_dai, 1); |
| 240 | } |
| 241 | |
| 242 | static int pcm1792a_spi_remove(struct spi_device *spi) |
| 243 | { |
| 244 | snd_soc_unregister_codec(&spi->dev); |
| 245 | return 0; |
| 246 | } |
| 247 | |
| 248 | static const struct spi_device_id pcm1792a_spi_ids[] = { |
| 249 | { "pcm1792a", 0 }, |
| 250 | { }, |
| 251 | }; |
| 252 | MODULE_DEVICE_TABLE(spi, pcm1792a_spi_ids); |
| 253 | |
| 254 | static struct spi_driver pcm1792a_codec_driver = { |
| 255 | .driver = { |
| 256 | .name = "pcm1792a", |
| 257 | .owner = THIS_MODULE, |
Mark Brown | 55af2d2 | 2013-08-05 18:20:53 +0100 | [diff] [blame] | 258 | .of_match_table = of_match_ptr(pcm1792a_of_match), |
Michael Trimarchi | 13b02fa | 2013-08-03 16:20:43 +0200 | [diff] [blame] | 259 | }, |
| 260 | .id_table = pcm1792a_spi_ids, |
| 261 | .probe = pcm1792a_spi_probe, |
| 262 | .remove = pcm1792a_spi_remove, |
| 263 | }; |
| 264 | |
| 265 | module_spi_driver(pcm1792a_codec_driver); |
| 266 | |
| 267 | MODULE_DESCRIPTION("ASoC PCM1792A driver"); |
| 268 | MODULE_AUTHOR("Michael Trimarchi <michael@amarulasolutions.com>"); |
| 269 | MODULE_LICENSE("GPL"); |