Mark Brown | 5a3af12 | 2014-02-06 12:03:27 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Driver for the PCM512x CODECs |
| 3 | * |
| 4 | * Author: Mark Brown <broonie@linaro.org> |
| 5 | * Copyright 2014 Linaro Ltd |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or |
| 8 | * modify it under the terms of the GNU General Public License |
| 9 | * version 2 as published by the Free Software Foundation. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, but |
| 12 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | * General Public License for more details. |
| 15 | */ |
| 16 | |
| 17 | |
| 18 | #include <linux/init.h> |
| 19 | #include <linux/module.h> |
| 20 | #include <linux/clk.h> |
| 21 | #include <linux/i2c.h> |
| 22 | #include <linux/pm_runtime.h> |
| 23 | #include <linux/regmap.h> |
| 24 | #include <linux/regulator/consumer.h> |
| 25 | #include <linux/spi/spi.h> |
| 26 | #include <sound/soc.h> |
| 27 | #include <sound/soc-dapm.h> |
| 28 | #include <sound/tlv.h> |
| 29 | |
| 30 | #include "pcm512x.h" |
| 31 | |
| 32 | #define PCM512x_NUM_SUPPLIES 3 |
Mark Brown | 06d0ffc | 2014-02-06 14:33:52 +0000 | [diff] [blame] | 33 | static const char * const pcm512x_supply_names[PCM512x_NUM_SUPPLIES] = { |
Mark Brown | 5a3af12 | 2014-02-06 12:03:27 +0000 | [diff] [blame] | 34 | "AVDD", |
| 35 | "DVDD", |
| 36 | "CPVDD", |
| 37 | }; |
| 38 | |
| 39 | struct pcm512x_priv { |
| 40 | struct regmap *regmap; |
| 41 | struct clk *sclk; |
| 42 | struct regulator_bulk_data supplies[PCM512x_NUM_SUPPLIES]; |
| 43 | struct notifier_block supply_nb[PCM512x_NUM_SUPPLIES]; |
| 44 | }; |
| 45 | |
| 46 | /* |
| 47 | * We can't use the same notifier block for more than one supply and |
| 48 | * there's no way I can see to get from a callback to the caller |
| 49 | * except container_of(). |
| 50 | */ |
| 51 | #define PCM512x_REGULATOR_EVENT(n) \ |
| 52 | static int pcm512x_regulator_event_##n(struct notifier_block *nb, \ |
| 53 | unsigned long event, void *data) \ |
| 54 | { \ |
| 55 | struct pcm512x_priv *pcm512x = container_of(nb, struct pcm512x_priv, \ |
| 56 | supply_nb[n]); \ |
| 57 | if (event & REGULATOR_EVENT_DISABLE) { \ |
| 58 | regcache_mark_dirty(pcm512x->regmap); \ |
| 59 | regcache_cache_only(pcm512x->regmap, true); \ |
| 60 | } \ |
| 61 | return 0; \ |
| 62 | } |
| 63 | |
| 64 | PCM512x_REGULATOR_EVENT(0) |
| 65 | PCM512x_REGULATOR_EVENT(1) |
| 66 | PCM512x_REGULATOR_EVENT(2) |
| 67 | |
| 68 | static const struct reg_default pcm512x_reg_defaults[] = { |
Mark Brown | 806d646 | 2014-02-07 19:08:11 +0000 | [diff] [blame^] | 69 | { PCM512x_RESET, 0x00 }, |
| 70 | { PCM512x_POWER, 0x00 }, |
| 71 | { PCM512x_MUTE, 0x00 }, |
| 72 | { PCM512x_DSP, 0x00 }, |
| 73 | { PCM512x_PLL_REF, 0x00 }, |
| 74 | { PCM512x_DAC_ROUTING, 0x11 }, |
| 75 | { PCM512x_DSP_PROGRAM, 0x01 }, |
| 76 | { PCM512x_CLKDET, 0x00 }, |
| 77 | { PCM512x_AUTO_MUTE, 0x00 }, |
| 78 | { PCM512x_ERROR_DETECT, 0x00 }, |
| 79 | { PCM512x_DIGITAL_VOLUME_1, 0x00 }, |
| 80 | { PCM512x_DIGITAL_VOLUME_2, 0x30 }, |
| 81 | { PCM512x_DIGITAL_VOLUME_3, 0x30 }, |
| 82 | { PCM512x_DIGITAL_MUTE_1, 0x22 }, |
| 83 | { PCM512x_DIGITAL_MUTE_2, 0x00 }, |
| 84 | { PCM512x_DIGITAL_MUTE_3, 0x07 }, |
| 85 | { PCM512x_OUTPUT_AMPLITUDE, 0x00 }, |
| 86 | { PCM512x_ANALOG_GAIN_CTRL, 0x00 }, |
| 87 | { PCM512x_UNDERVOLTAGE_PROT, 0x00 }, |
| 88 | { PCM512x_ANALOG_MUTE_CTRL, 0x00 }, |
| 89 | { PCM512x_ANALOG_GAIN_BOOST, 0x00 }, |
| 90 | { PCM512x_VCOM_CTRL_1, 0x00 }, |
| 91 | { PCM512x_VCOM_CTRL_2, 0x01 }, |
Mark Brown | 5a3af12 | 2014-02-06 12:03:27 +0000 | [diff] [blame] | 92 | }; |
| 93 | |
| 94 | static bool pcm512x_readable(struct device *dev, unsigned int reg) |
| 95 | { |
| 96 | switch (reg) { |
| 97 | case PCM512x_RESET: |
| 98 | case PCM512x_POWER: |
| 99 | case PCM512x_MUTE: |
| 100 | case PCM512x_PLL_EN: |
| 101 | case PCM512x_SPI_MISO_FUNCTION: |
| 102 | case PCM512x_DSP: |
| 103 | case PCM512x_GPIO_EN: |
| 104 | case PCM512x_BCLK_LRCLK_CFG: |
| 105 | case PCM512x_DSP_GPIO_INPUT: |
| 106 | case PCM512x_MASTER_MODE: |
| 107 | case PCM512x_PLL_REF: |
| 108 | case PCM512x_PLL_COEFF_0: |
| 109 | case PCM512x_PLL_COEFF_1: |
| 110 | case PCM512x_PLL_COEFF_2: |
| 111 | case PCM512x_PLL_COEFF_3: |
| 112 | case PCM512x_PLL_COEFF_4: |
| 113 | case PCM512x_DSP_CLKDIV: |
| 114 | case PCM512x_DAC_CLKDIV: |
| 115 | case PCM512x_NCP_CLKDIV: |
| 116 | case PCM512x_OSR_CLKDIV: |
| 117 | case PCM512x_MASTER_CLKDIV_1: |
| 118 | case PCM512x_MASTER_CLKDIV_2: |
| 119 | case PCM512x_FS_SPEED_MODE: |
| 120 | case PCM512x_IDAC_1: |
| 121 | case PCM512x_IDAC_2: |
| 122 | case PCM512x_ERROR_DETECT: |
| 123 | case PCM512x_I2S_1: |
| 124 | case PCM512x_I2S_2: |
| 125 | case PCM512x_DAC_ROUTING: |
| 126 | case PCM512x_DSP_PROGRAM: |
| 127 | case PCM512x_CLKDET: |
| 128 | case PCM512x_AUTO_MUTE: |
| 129 | case PCM512x_DIGITAL_VOLUME_1: |
| 130 | case PCM512x_DIGITAL_VOLUME_2: |
| 131 | case PCM512x_DIGITAL_VOLUME_3: |
| 132 | case PCM512x_DIGITAL_MUTE_1: |
| 133 | case PCM512x_DIGITAL_MUTE_2: |
| 134 | case PCM512x_DIGITAL_MUTE_3: |
| 135 | case PCM512x_GPIO_OUTPUT_1: |
| 136 | case PCM512x_GPIO_OUTPUT_2: |
| 137 | case PCM512x_GPIO_OUTPUT_3: |
| 138 | case PCM512x_GPIO_OUTPUT_4: |
| 139 | case PCM512x_GPIO_OUTPUT_5: |
| 140 | case PCM512x_GPIO_OUTPUT_6: |
| 141 | case PCM512x_GPIO_CONTROL_1: |
| 142 | case PCM512x_GPIO_CONTROL_2: |
| 143 | case PCM512x_OVERFLOW: |
| 144 | case PCM512x_RATE_DET_1: |
| 145 | case PCM512x_RATE_DET_2: |
| 146 | case PCM512x_RATE_DET_3: |
| 147 | case PCM512x_RATE_DET_4: |
| 148 | case PCM512x_ANALOG_MUTE_DET: |
| 149 | case PCM512x_GPIN: |
| 150 | case PCM512x_DIGITAL_MUTE_DET: |
Mark Brown | 806d646 | 2014-02-07 19:08:11 +0000 | [diff] [blame^] | 151 | case PCM512x_OUTPUT_AMPLITUDE: |
| 152 | case PCM512x_ANALOG_GAIN_CTRL: |
| 153 | case PCM512x_UNDERVOLTAGE_PROT: |
| 154 | case PCM512x_ANALOG_MUTE_CTRL: |
| 155 | case PCM512x_ANALOG_GAIN_BOOST: |
| 156 | case PCM512x_VCOM_CTRL_1: |
| 157 | case PCM512x_VCOM_CTRL_2: |
| 158 | case PCM512x_CRAM_CTRL: |
Mark Brown | 5a3af12 | 2014-02-06 12:03:27 +0000 | [diff] [blame] | 159 | return true; |
| 160 | default: |
Mark Brown | 806d646 | 2014-02-07 19:08:11 +0000 | [diff] [blame^] | 161 | /* There are 256 raw register addresses */ |
| 162 | return reg < 0xff; |
Mark Brown | 5a3af12 | 2014-02-06 12:03:27 +0000 | [diff] [blame] | 163 | } |
| 164 | } |
| 165 | |
| 166 | static bool pcm512x_volatile(struct device *dev, unsigned int reg) |
| 167 | { |
| 168 | switch (reg) { |
| 169 | case PCM512x_PLL_EN: |
| 170 | case PCM512x_OVERFLOW: |
| 171 | case PCM512x_RATE_DET_1: |
| 172 | case PCM512x_RATE_DET_2: |
| 173 | case PCM512x_RATE_DET_3: |
| 174 | case PCM512x_RATE_DET_4: |
| 175 | case PCM512x_ANALOG_MUTE_DET: |
| 176 | case PCM512x_GPIN: |
| 177 | case PCM512x_DIGITAL_MUTE_DET: |
Mark Brown | 806d646 | 2014-02-07 19:08:11 +0000 | [diff] [blame^] | 178 | case PCM512x_CRAM_CTRL: |
Mark Brown | 5a3af12 | 2014-02-06 12:03:27 +0000 | [diff] [blame] | 179 | return true; |
| 180 | default: |
Mark Brown | 806d646 | 2014-02-07 19:08:11 +0000 | [diff] [blame^] | 181 | /* There are 256 raw register addresses */ |
| 182 | return reg < 0xff; |
Mark Brown | 5a3af12 | 2014-02-06 12:03:27 +0000 | [diff] [blame] | 183 | } |
| 184 | } |
| 185 | |
| 186 | static const DECLARE_TLV_DB_SCALE(digital_tlv, -10350, 50, 1); |
| 187 | |
Mark Brown | 06d0ffc | 2014-02-06 14:33:52 +0000 | [diff] [blame] | 188 | static const char * const pcm512x_dsp_program_texts[] = { |
Mark Brown | 5a3af12 | 2014-02-06 12:03:27 +0000 | [diff] [blame] | 189 | "FIR interpolation with de-emphasis", |
| 190 | "Low latency IIR with de-emphasis", |
| 191 | "Fixed process flow", |
| 192 | "High attenuation with de-emphasis", |
| 193 | "Ringing-less low latency FIR", |
| 194 | }; |
| 195 | |
| 196 | static const unsigned int pcm512x_dsp_program_values[] = { |
| 197 | 1, |
| 198 | 2, |
| 199 | 3, |
| 200 | 5, |
| 201 | 7, |
| 202 | }; |
| 203 | |
| 204 | static const SOC_VALUE_ENUM_SINGLE_DECL(pcm512x_dsp_program, |
| 205 | PCM512x_DSP_PROGRAM, 0, 0x1f, |
| 206 | pcm512x_dsp_program_texts, |
| 207 | pcm512x_dsp_program_values); |
| 208 | |
Mark Brown | 06d0ffc | 2014-02-06 14:33:52 +0000 | [diff] [blame] | 209 | static const char * const pcm512x_clk_missing_text[] = { |
Mark Brown | 5a3af12 | 2014-02-06 12:03:27 +0000 | [diff] [blame] | 210 | "1s", "2s", "3s", "4s", "5s", "6s", "7s", "8s" |
| 211 | }; |
| 212 | |
| 213 | static const struct soc_enum pcm512x_clk_missing = |
| 214 | SOC_ENUM_SINGLE(PCM512x_CLKDET, 0, 8, pcm512x_clk_missing_text); |
| 215 | |
Mark Brown | 06d0ffc | 2014-02-06 14:33:52 +0000 | [diff] [blame] | 216 | static const char * const pcm512x_autom_text[] = { |
Mark Brown | 5a3af12 | 2014-02-06 12:03:27 +0000 | [diff] [blame] | 217 | "21ms", "106ms", "213ms", "533ms", "1.07s", "2.13s", "5.33s", "10.66s" |
| 218 | }; |
| 219 | |
| 220 | static const struct soc_enum pcm512x_autom_l = |
| 221 | SOC_ENUM_SINGLE(PCM512x_AUTO_MUTE, PCM512x_ATML_SHIFT, 8, |
| 222 | pcm512x_autom_text); |
| 223 | |
| 224 | static const struct soc_enum pcm512x_autom_r = |
| 225 | SOC_ENUM_SINGLE(PCM512x_AUTO_MUTE, PCM512x_ATMR_SHIFT, 8, |
| 226 | pcm512x_autom_text); |
| 227 | |
Mark Brown | 06d0ffc | 2014-02-06 14:33:52 +0000 | [diff] [blame] | 228 | static const char * const pcm512x_ramp_rate_text[] = { |
Mark Brown | 5a3af12 | 2014-02-06 12:03:27 +0000 | [diff] [blame] | 229 | "1 sample/update", "2 samples/update", "4 samples/update", |
| 230 | "Immediate" |
| 231 | }; |
| 232 | |
| 233 | static const struct soc_enum pcm512x_vndf = |
| 234 | SOC_ENUM_SINGLE(PCM512x_DIGITAL_MUTE_1, PCM512x_VNDF_SHIFT, 4, |
| 235 | pcm512x_ramp_rate_text); |
| 236 | |
| 237 | static const struct soc_enum pcm512x_vnuf = |
| 238 | SOC_ENUM_SINGLE(PCM512x_DIGITAL_MUTE_1, PCM512x_VNUF_SHIFT, 4, |
| 239 | pcm512x_ramp_rate_text); |
| 240 | |
| 241 | static const struct soc_enum pcm512x_vedf = |
| 242 | SOC_ENUM_SINGLE(PCM512x_DIGITAL_MUTE_2, PCM512x_VEDF_SHIFT, 4, |
| 243 | pcm512x_ramp_rate_text); |
| 244 | |
Mark Brown | 06d0ffc | 2014-02-06 14:33:52 +0000 | [diff] [blame] | 245 | static const char * const pcm512x_ramp_step_text[] = { |
Mark Brown | 5a3af12 | 2014-02-06 12:03:27 +0000 | [diff] [blame] | 246 | "4dB/step", "2dB/step", "1dB/step", "0.5dB/step" |
| 247 | }; |
| 248 | |
| 249 | static const struct soc_enum pcm512x_vnds = |
| 250 | SOC_ENUM_SINGLE(PCM512x_DIGITAL_MUTE_1, PCM512x_VNDS_SHIFT, 4, |
| 251 | pcm512x_ramp_step_text); |
| 252 | |
| 253 | static const struct soc_enum pcm512x_vnus = |
| 254 | SOC_ENUM_SINGLE(PCM512x_DIGITAL_MUTE_1, PCM512x_VNUS_SHIFT, 4, |
| 255 | pcm512x_ramp_step_text); |
| 256 | |
| 257 | static const struct soc_enum pcm512x_veds = |
| 258 | SOC_ENUM_SINGLE(PCM512x_DIGITAL_MUTE_2, PCM512x_VEDS_SHIFT, 4, |
| 259 | pcm512x_ramp_step_text); |
| 260 | |
| 261 | static const struct snd_kcontrol_new pcm512x_controls[] = { |
| 262 | SOC_DOUBLE_R_TLV("Playback Digital Volume", PCM512x_DIGITAL_VOLUME_2, |
| 263 | PCM512x_DIGITAL_VOLUME_3, 0, 255, 1, digital_tlv), |
| 264 | SOC_DOUBLE("Playback Digital Switch", PCM512x_MUTE, PCM512x_RQML_SHIFT, |
| 265 | PCM512x_RQMR_SHIFT, 1, 1), |
| 266 | |
| 267 | SOC_SINGLE("Deemphasis Switch", PCM512x_DSP, PCM512x_DEMP_SHIFT, 1, 1), |
| 268 | SOC_VALUE_ENUM("DSP Program", pcm512x_dsp_program), |
| 269 | |
| 270 | SOC_ENUM("Clock Missing Period", pcm512x_clk_missing), |
| 271 | SOC_ENUM("Auto Mute Time Left", pcm512x_autom_l), |
| 272 | SOC_ENUM("Auto Mute Time Right", pcm512x_autom_r), |
| 273 | SOC_SINGLE("Auto Mute Mono Switch", PCM512x_DIGITAL_MUTE_3, |
| 274 | PCM512x_ACTL_SHIFT, 1, 0), |
| 275 | SOC_DOUBLE("Auto Mute Switch", PCM512x_DIGITAL_MUTE_3, PCM512x_AMLE_SHIFT, |
| 276 | PCM512x_AMLR_SHIFT, 1, 0), |
| 277 | |
| 278 | SOC_ENUM("Volume Ramp Down Rate", pcm512x_vndf), |
| 279 | SOC_ENUM("Volume Ramp Down Step", pcm512x_vnds), |
| 280 | SOC_ENUM("Volume Ramp Up Rate", pcm512x_vnuf), |
| 281 | SOC_ENUM("Volume Ramp Up Step", pcm512x_vnus), |
| 282 | SOC_ENUM("Volume Ramp Down Emergency Rate", pcm512x_vedf), |
| 283 | SOC_ENUM("Volume Ramp Down Emergency Step", pcm512x_veds), |
| 284 | }; |
| 285 | |
| 286 | static const struct snd_soc_dapm_widget pcm512x_dapm_widgets[] = { |
| 287 | SND_SOC_DAPM_DAC("DACL", NULL, SND_SOC_NOPM, 0, 0), |
| 288 | SND_SOC_DAPM_DAC("DACR", NULL, SND_SOC_NOPM, 0, 0), |
| 289 | |
| 290 | SND_SOC_DAPM_OUTPUT("OUTL"), |
| 291 | SND_SOC_DAPM_OUTPUT("OUTR"), |
| 292 | }; |
| 293 | |
| 294 | static const struct snd_soc_dapm_route pcm512x_dapm_routes[] = { |
| 295 | { "DACL", NULL, "Playback" }, |
| 296 | { "DACR", NULL, "Playback" }, |
| 297 | |
| 298 | { "OUTL", NULL, "DACL" }, |
| 299 | { "OUTR", NULL, "DACR" }, |
| 300 | }; |
| 301 | |
| 302 | static int pcm512x_set_bias_level(struct snd_soc_codec *codec, |
| 303 | enum snd_soc_bias_level level) |
| 304 | { |
| 305 | struct pcm512x_priv *pcm512x = dev_get_drvdata(codec->dev); |
| 306 | int ret; |
| 307 | |
| 308 | switch (level) { |
| 309 | case SND_SOC_BIAS_ON: |
| 310 | case SND_SOC_BIAS_PREPARE: |
| 311 | break; |
| 312 | |
| 313 | case SND_SOC_BIAS_STANDBY: |
| 314 | ret = regmap_update_bits(pcm512x->regmap, PCM512x_POWER, |
| 315 | PCM512x_RQST, 0); |
| 316 | if (ret != 0) { |
| 317 | dev_err(codec->dev, "Failed to remove standby: %d\n", |
| 318 | ret); |
| 319 | return ret; |
| 320 | } |
| 321 | break; |
| 322 | |
| 323 | case SND_SOC_BIAS_OFF: |
| 324 | ret = regmap_update_bits(pcm512x->regmap, PCM512x_POWER, |
| 325 | PCM512x_RQST, PCM512x_RQST); |
| 326 | if (ret != 0) { |
| 327 | dev_err(codec->dev, "Failed to request standby: %d\n", |
| 328 | ret); |
| 329 | return ret; |
| 330 | } |
| 331 | break; |
| 332 | } |
| 333 | |
| 334 | codec->dapm.bias_level = level; |
| 335 | |
| 336 | return 0; |
| 337 | } |
| 338 | |
| 339 | static struct snd_soc_dai_driver pcm512x_dai = { |
| 340 | .name = "pcm512x-hifi", |
| 341 | .playback = { |
| 342 | .stream_name = "Playback", |
| 343 | .channels_min = 2, |
| 344 | .channels_max = 2, |
| 345 | .rates = SNDRV_PCM_RATE_8000_192000, |
| 346 | .formats = SNDRV_PCM_FMTBIT_S16_LE | |
| 347 | SNDRV_PCM_FMTBIT_S24_LE | |
| 348 | SNDRV_PCM_FMTBIT_S32_LE |
| 349 | }, |
| 350 | }; |
| 351 | |
| 352 | static struct snd_soc_codec_driver pcm512x_codec_driver = { |
| 353 | .set_bias_level = pcm512x_set_bias_level, |
| 354 | .idle_bias_off = true, |
| 355 | |
| 356 | .controls = pcm512x_controls, |
| 357 | .num_controls = ARRAY_SIZE(pcm512x_controls), |
| 358 | .dapm_widgets = pcm512x_dapm_widgets, |
| 359 | .num_dapm_widgets = ARRAY_SIZE(pcm512x_dapm_widgets), |
| 360 | .dapm_routes = pcm512x_dapm_routes, |
| 361 | .num_dapm_routes = ARRAY_SIZE(pcm512x_dapm_routes), |
| 362 | }; |
| 363 | |
Mark Brown | 806d646 | 2014-02-07 19:08:11 +0000 | [diff] [blame^] | 364 | static const struct regmap_range_cfg pcm512x_range = { |
| 365 | .name = "Pages", .range_min = PCM512x_VIRT_BASE, |
| 366 | .range_max = PCM512x_MAX_REGISTER, |
| 367 | .selector_reg = PCM512x_PAGE, |
| 368 | .selector_mask = 0xff, |
| 369 | .window_start = 0, .window_len = 0x100, |
| 370 | }; |
| 371 | |
Mark Brown | 5a3af12 | 2014-02-06 12:03:27 +0000 | [diff] [blame] | 372 | static const struct regmap_config pcm512x_regmap = { |
| 373 | .reg_bits = 8, |
| 374 | .val_bits = 8, |
| 375 | |
| 376 | .readable_reg = pcm512x_readable, |
| 377 | .volatile_reg = pcm512x_volatile, |
| 378 | |
Mark Brown | 806d646 | 2014-02-07 19:08:11 +0000 | [diff] [blame^] | 379 | .ranges = &pcm512x_range, |
| 380 | .num_ranges = 1, |
| 381 | |
Mark Brown | 5a3af12 | 2014-02-06 12:03:27 +0000 | [diff] [blame] | 382 | .max_register = PCM512x_MAX_REGISTER, |
| 383 | .reg_defaults = pcm512x_reg_defaults, |
| 384 | .num_reg_defaults = ARRAY_SIZE(pcm512x_reg_defaults), |
| 385 | .cache_type = REGCACHE_RBTREE, |
| 386 | }; |
| 387 | |
| 388 | static const struct of_device_id pcm512x_of_match[] = { |
| 389 | { .compatible = "ti,pcm5121", }, |
| 390 | { .compatible = "ti,pcm5122", }, |
| 391 | { } |
| 392 | }; |
| 393 | MODULE_DEVICE_TABLE(of, pcm512x_of_match); |
| 394 | |
| 395 | static int pcm512x_probe(struct device *dev, struct regmap *regmap) |
| 396 | { |
| 397 | struct pcm512x_priv *pcm512x; |
| 398 | int i, ret; |
| 399 | |
| 400 | pcm512x = devm_kzalloc(dev, sizeof(struct pcm512x_priv), GFP_KERNEL); |
| 401 | if (!pcm512x) |
| 402 | return -ENOMEM; |
| 403 | |
| 404 | dev_set_drvdata(dev, pcm512x); |
| 405 | pcm512x->regmap = regmap; |
| 406 | |
| 407 | for (i = 0; i < ARRAY_SIZE(pcm512x->supplies); i++) |
| 408 | pcm512x->supplies[i].supply = pcm512x_supply_names[i]; |
| 409 | |
| 410 | ret = devm_regulator_bulk_get(dev, ARRAY_SIZE(pcm512x->supplies), |
| 411 | pcm512x->supplies); |
| 412 | if (ret != 0) { |
| 413 | dev_err(dev, "Failed to get supplies: %d\n", ret); |
| 414 | return ret; |
| 415 | } |
| 416 | |
| 417 | pcm512x->supply_nb[0].notifier_call = pcm512x_regulator_event_0; |
| 418 | pcm512x->supply_nb[1].notifier_call = pcm512x_regulator_event_1; |
| 419 | pcm512x->supply_nb[2].notifier_call = pcm512x_regulator_event_2; |
| 420 | |
| 421 | for (i = 0; i < ARRAY_SIZE(pcm512x->supplies); i++) { |
| 422 | ret = regulator_register_notifier(pcm512x->supplies[i].consumer, |
| 423 | &pcm512x->supply_nb[i]); |
| 424 | if (ret != 0) { |
| 425 | dev_err(dev, |
| 426 | "Failed to register regulator notifier: %d\n", |
| 427 | ret); |
| 428 | } |
| 429 | } |
| 430 | |
| 431 | ret = regulator_bulk_enable(ARRAY_SIZE(pcm512x->supplies), |
| 432 | pcm512x->supplies); |
| 433 | if (ret != 0) { |
| 434 | dev_err(dev, "Failed to enable supplies: %d\n", ret); |
| 435 | return ret; |
| 436 | } |
| 437 | |
| 438 | /* Reset the device, verifying I/O in the process for I2C */ |
| 439 | ret = regmap_write(regmap, PCM512x_RESET, |
| 440 | PCM512x_RSTM | PCM512x_RSTR); |
| 441 | if (ret != 0) { |
| 442 | dev_err(dev, "Failed to reset device: %d\n", ret); |
| 443 | goto err; |
| 444 | } |
| 445 | |
| 446 | ret = regmap_write(regmap, PCM512x_RESET, 0); |
| 447 | if (ret != 0) { |
| 448 | dev_err(dev, "Failed to reset device: %d\n", ret); |
| 449 | goto err; |
| 450 | } |
| 451 | |
| 452 | pcm512x->sclk = devm_clk_get(dev, NULL); |
| 453 | if (IS_ERR(pcm512x->sclk)) { |
| 454 | if (PTR_ERR(pcm512x->sclk) == -EPROBE_DEFER) |
| 455 | return -EPROBE_DEFER; |
| 456 | |
| 457 | dev_info(dev, "No SCLK, using BCLK: %ld\n", |
| 458 | PTR_ERR(pcm512x->sclk)); |
| 459 | |
| 460 | /* Disable reporting of missing SCLK as an error */ |
| 461 | regmap_update_bits(regmap, PCM512x_ERROR_DETECT, |
| 462 | PCM512x_IDCH, PCM512x_IDCH); |
| 463 | |
| 464 | /* Switch PLL input to BCLK */ |
| 465 | regmap_update_bits(regmap, PCM512x_PLL_REF, |
| 466 | PCM512x_SREF, PCM512x_SREF); |
| 467 | } else { |
| 468 | ret = clk_prepare_enable(pcm512x->sclk); |
| 469 | if (ret != 0) { |
| 470 | dev_err(dev, "Failed to enable SCLK: %d\n", ret); |
| 471 | return ret; |
| 472 | } |
| 473 | } |
| 474 | |
| 475 | /* Default to standby mode */ |
| 476 | ret = regmap_update_bits(pcm512x->regmap, PCM512x_POWER, |
| 477 | PCM512x_RQST, PCM512x_RQST); |
| 478 | if (ret != 0) { |
| 479 | dev_err(dev, "Failed to request standby: %d\n", |
| 480 | ret); |
| 481 | goto err_clk; |
| 482 | } |
| 483 | |
| 484 | pm_runtime_set_active(dev); |
| 485 | pm_runtime_enable(dev); |
| 486 | pm_runtime_idle(dev); |
| 487 | |
| 488 | ret = snd_soc_register_codec(dev, &pcm512x_codec_driver, |
| 489 | &pcm512x_dai, 1); |
| 490 | if (ret != 0) { |
| 491 | dev_err(dev, "Failed to register CODEC: %d\n", ret); |
| 492 | goto err_pm; |
| 493 | } |
| 494 | |
| 495 | return 0; |
| 496 | |
| 497 | err_pm: |
| 498 | pm_runtime_disable(dev); |
| 499 | err_clk: |
| 500 | if (!IS_ERR(pcm512x->sclk)) |
| 501 | clk_disable_unprepare(pcm512x->sclk); |
| 502 | err: |
| 503 | regulator_bulk_disable(ARRAY_SIZE(pcm512x->supplies), |
| 504 | pcm512x->supplies); |
| 505 | return ret; |
| 506 | } |
| 507 | |
| 508 | static void pcm512x_remove(struct device *dev) |
| 509 | { |
| 510 | struct pcm512x_priv *pcm512x = dev_get_drvdata(dev); |
| 511 | |
| 512 | snd_soc_unregister_codec(dev); |
| 513 | pm_runtime_disable(dev); |
| 514 | if (!IS_ERR(pcm512x->sclk)) |
| 515 | clk_disable_unprepare(pcm512x->sclk); |
| 516 | regulator_bulk_disable(ARRAY_SIZE(pcm512x->supplies), |
| 517 | pcm512x->supplies); |
| 518 | } |
| 519 | |
| 520 | static int pcm512x_suspend(struct device *dev) |
| 521 | { |
| 522 | struct pcm512x_priv *pcm512x = dev_get_drvdata(dev); |
| 523 | int ret; |
| 524 | |
| 525 | ret = regmap_update_bits(pcm512x->regmap, PCM512x_POWER, |
| 526 | PCM512x_RQPD, PCM512x_RQPD); |
| 527 | if (ret != 0) { |
| 528 | dev_err(dev, "Failed to request power down: %d\n", ret); |
| 529 | return ret; |
| 530 | } |
| 531 | |
| 532 | ret = regulator_bulk_disable(ARRAY_SIZE(pcm512x->supplies), |
| 533 | pcm512x->supplies); |
| 534 | if (ret != 0) { |
| 535 | dev_err(dev, "Failed to disable supplies: %d\n", ret); |
| 536 | return ret; |
| 537 | } |
| 538 | |
| 539 | if (!IS_ERR(pcm512x->sclk)) |
| 540 | clk_disable_unprepare(pcm512x->sclk); |
| 541 | |
| 542 | return 0; |
| 543 | } |
| 544 | |
| 545 | static int pcm512x_resume(struct device *dev) |
| 546 | { |
| 547 | struct pcm512x_priv *pcm512x = dev_get_drvdata(dev); |
| 548 | int ret; |
| 549 | |
| 550 | if (!IS_ERR(pcm512x->sclk)) { |
| 551 | ret = clk_prepare_enable(pcm512x->sclk); |
| 552 | if (ret != 0) { |
| 553 | dev_err(dev, "Failed to enable SCLK: %d\n", ret); |
| 554 | return ret; |
| 555 | } |
| 556 | } |
| 557 | |
| 558 | ret = regulator_bulk_enable(ARRAY_SIZE(pcm512x->supplies), |
| 559 | pcm512x->supplies); |
| 560 | if (ret != 0) { |
| 561 | dev_err(dev, "Failed to enable supplies: %d\n", ret); |
| 562 | return ret; |
| 563 | } |
| 564 | |
| 565 | regcache_cache_only(pcm512x->regmap, false); |
| 566 | ret = regcache_sync(pcm512x->regmap); |
| 567 | if (ret != 0) { |
| 568 | dev_err(dev, "Failed to sync cache: %d\n", ret); |
| 569 | return ret; |
| 570 | } |
| 571 | |
| 572 | ret = regmap_update_bits(pcm512x->regmap, PCM512x_POWER, |
| 573 | PCM512x_RQPD, 0); |
| 574 | if (ret != 0) { |
| 575 | dev_err(dev, "Failed to remove power down: %d\n", ret); |
| 576 | return ret; |
| 577 | } |
| 578 | |
| 579 | return 0; |
| 580 | } |
| 581 | |
| 582 | static const struct dev_pm_ops pcm512x_pm_ops = { |
| 583 | SET_RUNTIME_PM_OPS(pcm512x_suspend, pcm512x_resume, NULL) |
| 584 | }; |
| 585 | |
| 586 | #if IS_ENABLED(CONFIG_I2C) |
| 587 | static int pcm512x_i2c_probe(struct i2c_client *i2c, |
| 588 | const struct i2c_device_id *id) |
| 589 | { |
| 590 | struct regmap *regmap; |
| 591 | |
| 592 | regmap = devm_regmap_init_i2c(i2c, &pcm512x_regmap); |
| 593 | if (IS_ERR(regmap)) |
| 594 | return PTR_ERR(regmap); |
| 595 | |
| 596 | return pcm512x_probe(&i2c->dev, regmap); |
| 597 | } |
| 598 | |
| 599 | static int pcm512x_i2c_remove(struct i2c_client *i2c) |
| 600 | { |
| 601 | pcm512x_remove(&i2c->dev); |
| 602 | return 0; |
| 603 | } |
| 604 | |
| 605 | static const struct i2c_device_id pcm512x_i2c_id[] = { |
| 606 | { "pcm5121", }, |
| 607 | { "pcm5122", }, |
| 608 | { } |
| 609 | }; |
| 610 | MODULE_DEVICE_TABLE(i2c, pcm512x_i2c_id); |
| 611 | |
| 612 | static struct i2c_driver pcm512x_i2c_driver = { |
| 613 | .probe = pcm512x_i2c_probe, |
| 614 | .remove = pcm512x_i2c_remove, |
| 615 | .id_table = pcm512x_i2c_id, |
| 616 | .driver = { |
| 617 | .name = "pcm512x", |
| 618 | .owner = THIS_MODULE, |
| 619 | .of_match_table = pcm512x_of_match, |
| 620 | .pm = &pcm512x_pm_ops, |
| 621 | }, |
| 622 | }; |
| 623 | #endif |
| 624 | |
| 625 | #if defined(CONFIG_SPI_MASTER) |
| 626 | static int pcm512x_spi_probe(struct spi_device *spi) |
| 627 | { |
| 628 | struct regmap *regmap; |
| 629 | int ret; |
| 630 | |
| 631 | regmap = devm_regmap_init_spi(spi, &pcm512x_regmap); |
| 632 | if (IS_ERR(regmap)) { |
| 633 | ret = PTR_ERR(regmap); |
| 634 | return ret; |
| 635 | } |
| 636 | |
| 637 | return pcm512x_probe(&spi->dev, regmap); |
| 638 | } |
| 639 | |
| 640 | static int pcm512x_spi_remove(struct spi_device *spi) |
| 641 | { |
| 642 | pcm512x_remove(&spi->dev); |
| 643 | return 0; |
| 644 | } |
| 645 | |
| 646 | static const struct spi_device_id pcm512x_spi_id[] = { |
| 647 | { "pcm5121", }, |
| 648 | { "pcm5122", }, |
| 649 | { }, |
| 650 | }; |
| 651 | MODULE_DEVICE_TABLE(spi, pcm512x_spi_id); |
| 652 | |
| 653 | static struct spi_driver pcm512x_spi_driver = { |
| 654 | .probe = pcm512x_spi_probe, |
| 655 | .remove = pcm512x_spi_remove, |
| 656 | .id_table = pcm512x_spi_id, |
| 657 | .driver = { |
| 658 | .name = "pcm512x", |
| 659 | .owner = THIS_MODULE, |
| 660 | .of_match_table = pcm512x_of_match, |
| 661 | .pm = &pcm512x_pm_ops, |
| 662 | }, |
| 663 | }; |
| 664 | #endif |
| 665 | |
| 666 | static int __init pcm512x_modinit(void) |
| 667 | { |
| 668 | int ret = 0; |
| 669 | |
| 670 | #if IS_ENABLED(CONFIG_I2C) |
| 671 | ret = i2c_add_driver(&pcm512x_i2c_driver); |
| 672 | if (ret) { |
| 673 | printk(KERN_ERR "Failed to register pcm512x I2C driver: %d\n", |
| 674 | ret); |
| 675 | } |
| 676 | #endif |
| 677 | #if defined(CONFIG_SPI_MASTER) |
| 678 | ret = spi_register_driver(&pcm512x_spi_driver); |
| 679 | if (ret != 0) { |
| 680 | printk(KERN_ERR "Failed to register pcm512x SPI driver: %d\n", |
| 681 | ret); |
| 682 | } |
| 683 | #endif |
| 684 | return ret; |
| 685 | } |
| 686 | module_init(pcm512x_modinit); |
| 687 | |
| 688 | static void __exit pcm512x_exit(void) |
| 689 | { |
| 690 | #if IS_ENABLED(CONFIG_I2C) |
| 691 | i2c_del_driver(&pcm512x_i2c_driver); |
| 692 | #endif |
| 693 | #if defined(CONFIG_SPI_MASTER) |
| 694 | spi_unregister_driver(&pcm512x_spi_driver); |
| 695 | #endif |
| 696 | } |
| 697 | module_exit(pcm512x_exit); |
| 698 | |
| 699 | MODULE_DESCRIPTION("ASoC PCM512x codec driver"); |
| 700 | MODULE_AUTHOR("Mark Brown <broonie@linaro.org>"); |
| 701 | MODULE_LICENSE("GPL v2"); |