Ian Lartey | 992bee4 | 2010-07-31 00:32:11 +0100 | [diff] [blame] | 1 | /* |
| 2 | * wm8741.c -- WM8741 ALSA SoC Audio driver |
| 3 | * |
Mark Brown | 656baae | 2012-05-23 12:39:07 +0100 | [diff] [blame] | 4 | * Copyright 2010-1 Wolfson Microelectronics plc |
Ian Lartey | 992bee4 | 2010-07-31 00:32:11 +0100 | [diff] [blame] | 5 | * |
| 6 | * Author: Ian Lartey <ian@opensource.wolfsonmicro.com> |
| 7 | * |
| 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 version 2 as |
| 11 | * published by the Free Software Foundation. |
| 12 | */ |
| 13 | |
| 14 | #include <linux/module.h> |
| 15 | #include <linux/moduleparam.h> |
| 16 | #include <linux/init.h> |
| 17 | #include <linux/delay.h> |
| 18 | #include <linux/pm.h> |
| 19 | #include <linux/i2c.h> |
Mark Brown | 39e9b8d | 2011-08-03 17:30:57 +0900 | [diff] [blame] | 20 | #include <linux/spi/spi.h> |
Mark Brown | fe98c0c | 2012-09-10 18:00:21 +0800 | [diff] [blame] | 21 | #include <linux/regmap.h> |
Ian Lartey | 992bee4 | 2010-07-31 00:32:11 +0100 | [diff] [blame] | 22 | #include <linux/regulator/consumer.h> |
| 23 | #include <linux/slab.h> |
Mark Brown | 80080ec | 2011-08-03 17:31:26 +0900 | [diff] [blame] | 24 | #include <linux/of_device.h> |
Ian Lartey | 992bee4 | 2010-07-31 00:32:11 +0100 | [diff] [blame] | 25 | #include <sound/core.h> |
| 26 | #include <sound/pcm.h> |
| 27 | #include <sound/pcm_params.h> |
| 28 | #include <sound/soc.h> |
Ian Lartey | 992bee4 | 2010-07-31 00:32:11 +0100 | [diff] [blame] | 29 | #include <sound/initval.h> |
| 30 | #include <sound/tlv.h> |
| 31 | |
| 32 | #include "wm8741.h" |
| 33 | |
Ian Lartey | 992bee4 | 2010-07-31 00:32:11 +0100 | [diff] [blame] | 34 | #define WM8741_NUM_SUPPLIES 2 |
| 35 | static const char *wm8741_supply_names[WM8741_NUM_SUPPLIES] = { |
| 36 | "AVDD", |
| 37 | "DVDD", |
| 38 | }; |
| 39 | |
Ian Lartey | 3fe4a5e | 2010-08-27 15:26:27 +0100 | [diff] [blame] | 40 | #define WM8741_NUM_RATES 6 |
Ian Lartey | 992bee4 | 2010-07-31 00:32:11 +0100 | [diff] [blame] | 41 | |
| 42 | /* codec private data */ |
| 43 | struct wm8741_priv { |
Sergej Sawazki | c354b54 | 2015-05-13 11:39:01 +0200 | [diff] [blame] | 44 | struct wm8741_platform_data pdata; |
Mark Brown | fe98c0c | 2012-09-10 18:00:21 +0800 | [diff] [blame] | 45 | struct regmap *regmap; |
Ian Lartey | 992bee4 | 2010-07-31 00:32:11 +0100 | [diff] [blame] | 46 | struct regulator_bulk_data supplies[WM8741_NUM_SUPPLIES]; |
| 47 | unsigned int sysclk; |
Lars-Peter Clausen | 70bad2c | 2014-02-05 21:54:35 +0100 | [diff] [blame] | 48 | const struct snd_pcm_hw_constraint_list *sysclk_constraints; |
Ian Lartey | 992bee4 | 2010-07-31 00:32:11 +0100 | [diff] [blame] | 49 | }; |
| 50 | |
Mark Brown | fe98c0c | 2012-09-10 18:00:21 +0800 | [diff] [blame] | 51 | static const struct reg_default wm8741_reg_defaults[] = { |
| 52 | { 0, 0x0000 }, /* R0 - DACLLSB Attenuation */ |
| 53 | { 1, 0x0000 }, /* R1 - DACLMSB Attenuation */ |
| 54 | { 2, 0x0000 }, /* R2 - DACRLSB Attenuation */ |
| 55 | { 3, 0x0000 }, /* R3 - DACRMSB Attenuation */ |
| 56 | { 4, 0x0000 }, /* R4 - Volume Control */ |
| 57 | { 5, 0x000A }, /* R5 - Format Control */ |
| 58 | { 6, 0x0000 }, /* R6 - Filter Control */ |
| 59 | { 7, 0x0000 }, /* R7 - Mode Control 1 */ |
| 60 | { 8, 0x0002 }, /* R8 - Mode Control 2 */ |
| 61 | { 32, 0x0002 }, /* R32 - ADDITONAL_CONTROL_1 */ |
Ian Lartey | 992bee4 | 2010-07-31 00:32:11 +0100 | [diff] [blame] | 62 | }; |
| 63 | |
Mark Brown | fe98c0c | 2012-09-10 18:00:21 +0800 | [diff] [blame] | 64 | static bool wm8741_readable(struct device *dev, unsigned int reg) |
| 65 | { |
| 66 | switch (reg) { |
| 67 | case WM8741_DACLLSB_ATTENUATION: |
| 68 | case WM8741_DACLMSB_ATTENUATION: |
| 69 | case WM8741_DACRLSB_ATTENUATION: |
| 70 | case WM8741_DACRMSB_ATTENUATION: |
| 71 | case WM8741_VOLUME_CONTROL: |
| 72 | case WM8741_FORMAT_CONTROL: |
| 73 | case WM8741_FILTER_CONTROL: |
| 74 | case WM8741_MODE_CONTROL_1: |
| 75 | case WM8741_MODE_CONTROL_2: |
| 76 | case WM8741_ADDITIONAL_CONTROL_1: |
| 77 | return true; |
| 78 | default: |
| 79 | return false; |
| 80 | } |
| 81 | } |
Ian Lartey | 992bee4 | 2010-07-31 00:32:11 +0100 | [diff] [blame] | 82 | |
| 83 | static int wm8741_reset(struct snd_soc_codec *codec) |
| 84 | { |
| 85 | return snd_soc_write(codec, WM8741_RESET, 0); |
| 86 | } |
| 87 | |
| 88 | static const DECLARE_TLV_DB_SCALE(dac_tlv_fine, -12700, 13, 0); |
| 89 | static const DECLARE_TLV_DB_SCALE(dac_tlv, -12700, 400, 0); |
| 90 | |
Sergej Sawazki | c354b54 | 2015-05-13 11:39:01 +0200 | [diff] [blame] | 91 | static const struct snd_kcontrol_new wm8741_snd_controls_stereo[] = { |
Ian Lartey | 992bee4 | 2010-07-31 00:32:11 +0100 | [diff] [blame] | 92 | SOC_DOUBLE_R_TLV("Fine Playback Volume", WM8741_DACLLSB_ATTENUATION, |
| 93 | WM8741_DACRLSB_ATTENUATION, 1, 255, 1, dac_tlv_fine), |
| 94 | SOC_DOUBLE_R_TLV("Playback Volume", WM8741_DACLMSB_ATTENUATION, |
| 95 | WM8741_DACRMSB_ATTENUATION, 0, 511, 1, dac_tlv), |
| 96 | }; |
| 97 | |
Sergej Sawazki | c354b54 | 2015-05-13 11:39:01 +0200 | [diff] [blame] | 98 | static const struct snd_kcontrol_new wm8741_snd_controls_mono_left[] = { |
| 99 | SOC_SINGLE_TLV("Fine Playback Volume", WM8741_DACLLSB_ATTENUATION, |
| 100 | 1, 255, 1, dac_tlv_fine), |
| 101 | SOC_SINGLE_TLV("Playback Volume", WM8741_DACLMSB_ATTENUATION, |
| 102 | 0, 511, 1, dac_tlv), |
| 103 | }; |
| 104 | |
| 105 | static const struct snd_kcontrol_new wm8741_snd_controls_mono_right[] = { |
| 106 | SOC_SINGLE_TLV("Fine Playback Volume", WM8741_DACRLSB_ATTENUATION, |
| 107 | 1, 255, 1, dac_tlv_fine), |
| 108 | SOC_SINGLE_TLV("Playback Volume", WM8741_DACRMSB_ATTENUATION, |
| 109 | 0, 511, 1, dac_tlv), |
| 110 | }; |
| 111 | |
Ian Lartey | 992bee4 | 2010-07-31 00:32:11 +0100 | [diff] [blame] | 112 | static const struct snd_soc_dapm_widget wm8741_dapm_widgets[] = { |
| 113 | SND_SOC_DAPM_DAC("DACL", "Playback", SND_SOC_NOPM, 0, 0), |
| 114 | SND_SOC_DAPM_DAC("DACR", "Playback", SND_SOC_NOPM, 0, 0), |
| 115 | SND_SOC_DAPM_OUTPUT("VOUTLP"), |
| 116 | SND_SOC_DAPM_OUTPUT("VOUTLN"), |
| 117 | SND_SOC_DAPM_OUTPUT("VOUTRP"), |
| 118 | SND_SOC_DAPM_OUTPUT("VOUTRN"), |
| 119 | }; |
| 120 | |
Mark Brown | 0e62780 | 2011-12-03 17:15:06 +0000 | [diff] [blame] | 121 | static const struct snd_soc_dapm_route wm8741_dapm_routes[] = { |
Ian Lartey | 992bee4 | 2010-07-31 00:32:11 +0100 | [diff] [blame] | 122 | { "VOUTLP", NULL, "DACL" }, |
| 123 | { "VOUTLN", NULL, "DACL" }, |
| 124 | { "VOUTRP", NULL, "DACR" }, |
| 125 | { "VOUTRN", NULL, "DACR" }, |
| 126 | }; |
| 127 | |
Lars-Peter Clausen | 70bad2c | 2014-02-05 21:54:35 +0100 | [diff] [blame] | 128 | static const unsigned int rates_11289[] = { |
Sergej Sawazki | 8787041 | 2015-03-24 21:13:22 +0100 | [diff] [blame] | 129 | 44100, 88200, |
Ian Lartey | 3fe4a5e | 2010-08-27 15:26:27 +0100 | [diff] [blame] | 130 | }; |
| 131 | |
Lars-Peter Clausen | 70bad2c | 2014-02-05 21:54:35 +0100 | [diff] [blame] | 132 | static const struct snd_pcm_hw_constraint_list constraints_11289 = { |
Ian Lartey | 3fe4a5e | 2010-08-27 15:26:27 +0100 | [diff] [blame] | 133 | .count = ARRAY_SIZE(rates_11289), |
| 134 | .list = rates_11289, |
| 135 | }; |
| 136 | |
Lars-Peter Clausen | 70bad2c | 2014-02-05 21:54:35 +0100 | [diff] [blame] | 137 | static const unsigned int rates_12288[] = { |
Ian Lartey | 3fe4a5e | 2010-08-27 15:26:27 +0100 | [diff] [blame] | 138 | 32000, 48000, 96000, |
| 139 | }; |
| 140 | |
Lars-Peter Clausen | 70bad2c | 2014-02-05 21:54:35 +0100 | [diff] [blame] | 141 | static const struct snd_pcm_hw_constraint_list constraints_12288 = { |
Ian Lartey | 3fe4a5e | 2010-08-27 15:26:27 +0100 | [diff] [blame] | 142 | .count = ARRAY_SIZE(rates_12288), |
| 143 | .list = rates_12288, |
| 144 | }; |
| 145 | |
Lars-Peter Clausen | 70bad2c | 2014-02-05 21:54:35 +0100 | [diff] [blame] | 146 | static const unsigned int rates_16384[] = { |
Ian Lartey | 3fe4a5e | 2010-08-27 15:26:27 +0100 | [diff] [blame] | 147 | 32000, |
| 148 | }; |
| 149 | |
Lars-Peter Clausen | 70bad2c | 2014-02-05 21:54:35 +0100 | [diff] [blame] | 150 | static const struct snd_pcm_hw_constraint_list constraints_16384 = { |
Ian Lartey | 3fe4a5e | 2010-08-27 15:26:27 +0100 | [diff] [blame] | 151 | .count = ARRAY_SIZE(rates_16384), |
| 152 | .list = rates_16384, |
| 153 | }; |
| 154 | |
Lars-Peter Clausen | 70bad2c | 2014-02-05 21:54:35 +0100 | [diff] [blame] | 155 | static const unsigned int rates_16934[] = { |
Sergej Sawazki | 8787041 | 2015-03-24 21:13:22 +0100 | [diff] [blame] | 156 | 44100, 88200, |
Ian Lartey | 3fe4a5e | 2010-08-27 15:26:27 +0100 | [diff] [blame] | 157 | }; |
| 158 | |
Lars-Peter Clausen | 70bad2c | 2014-02-05 21:54:35 +0100 | [diff] [blame] | 159 | static const struct snd_pcm_hw_constraint_list constraints_16934 = { |
Ian Lartey | 3fe4a5e | 2010-08-27 15:26:27 +0100 | [diff] [blame] | 160 | .count = ARRAY_SIZE(rates_16934), |
| 161 | .list = rates_16934, |
| 162 | }; |
| 163 | |
Lars-Peter Clausen | 70bad2c | 2014-02-05 21:54:35 +0100 | [diff] [blame] | 164 | static const unsigned int rates_18432[] = { |
Ian Lartey | 3fe4a5e | 2010-08-27 15:26:27 +0100 | [diff] [blame] | 165 | 48000, 96000, |
| 166 | }; |
| 167 | |
Lars-Peter Clausen | 70bad2c | 2014-02-05 21:54:35 +0100 | [diff] [blame] | 168 | static const struct snd_pcm_hw_constraint_list constraints_18432 = { |
Ian Lartey | 3fe4a5e | 2010-08-27 15:26:27 +0100 | [diff] [blame] | 169 | .count = ARRAY_SIZE(rates_18432), |
| 170 | .list = rates_18432, |
| 171 | }; |
| 172 | |
Lars-Peter Clausen | 70bad2c | 2014-02-05 21:54:35 +0100 | [diff] [blame] | 173 | static const unsigned int rates_22579[] = { |
Sergej Sawazki | 8787041 | 2015-03-24 21:13:22 +0100 | [diff] [blame] | 174 | 44100, 88200, 176400 |
Ian Lartey | 3fe4a5e | 2010-08-27 15:26:27 +0100 | [diff] [blame] | 175 | }; |
| 176 | |
Lars-Peter Clausen | 70bad2c | 2014-02-05 21:54:35 +0100 | [diff] [blame] | 177 | static const struct snd_pcm_hw_constraint_list constraints_22579 = { |
Ian Lartey | 3fe4a5e | 2010-08-27 15:26:27 +0100 | [diff] [blame] | 178 | .count = ARRAY_SIZE(rates_22579), |
| 179 | .list = rates_22579, |
| 180 | }; |
| 181 | |
Lars-Peter Clausen | 70bad2c | 2014-02-05 21:54:35 +0100 | [diff] [blame] | 182 | static const unsigned int rates_24576[] = { |
Ian Lartey | 3fe4a5e | 2010-08-27 15:26:27 +0100 | [diff] [blame] | 183 | 32000, 48000, 96000, 192000 |
| 184 | }; |
| 185 | |
Lars-Peter Clausen | 70bad2c | 2014-02-05 21:54:35 +0100 | [diff] [blame] | 186 | static const struct snd_pcm_hw_constraint_list constraints_24576 = { |
Ian Lartey | 3fe4a5e | 2010-08-27 15:26:27 +0100 | [diff] [blame] | 187 | .count = ARRAY_SIZE(rates_24576), |
| 188 | .list = rates_24576, |
| 189 | }; |
| 190 | |
Lars-Peter Clausen | 70bad2c | 2014-02-05 21:54:35 +0100 | [diff] [blame] | 191 | static const unsigned int rates_36864[] = { |
Sergej Sawazki | 8787041 | 2015-03-24 21:13:22 +0100 | [diff] [blame] | 192 | 48000, 96000, 192000 |
Ian Lartey | 3fe4a5e | 2010-08-27 15:26:27 +0100 | [diff] [blame] | 193 | }; |
| 194 | |
Lars-Peter Clausen | 70bad2c | 2014-02-05 21:54:35 +0100 | [diff] [blame] | 195 | static const struct snd_pcm_hw_constraint_list constraints_36864 = { |
Ian Lartey | 3fe4a5e | 2010-08-27 15:26:27 +0100 | [diff] [blame] | 196 | .count = ARRAY_SIZE(rates_36864), |
| 197 | .list = rates_36864, |
Ian Lartey | 992bee4 | 2010-07-31 00:32:11 +0100 | [diff] [blame] | 198 | }; |
| 199 | |
Ian Lartey | 992bee4 | 2010-07-31 00:32:11 +0100 | [diff] [blame] | 200 | static int wm8741_startup(struct snd_pcm_substream *substream, |
| 201 | struct snd_soc_dai *dai) |
| 202 | { |
| 203 | struct snd_soc_codec *codec = dai->codec; |
| 204 | struct wm8741_priv *wm8741 = snd_soc_codec_get_drvdata(codec); |
| 205 | |
Sergej Sawazki | e369bd0 | 2015-06-06 11:25:48 +0200 | [diff] [blame] | 206 | if (wm8741->sysclk) |
| 207 | snd_pcm_hw_constraint_list(substream->runtime, 0, |
| 208 | SNDRV_PCM_HW_PARAM_RATE, |
| 209 | wm8741->sysclk_constraints); |
Ian Lartey | 992bee4 | 2010-07-31 00:32:11 +0100 | [diff] [blame] | 210 | |
| 211 | return 0; |
| 212 | } |
| 213 | |
| 214 | static int wm8741_hw_params(struct snd_pcm_substream *substream, |
| 215 | struct snd_pcm_hw_params *params, |
| 216 | struct snd_soc_dai *dai) |
| 217 | { |
Mark Brown | e6968a1 | 2012-04-04 15:58:16 +0100 | [diff] [blame] | 218 | struct snd_soc_codec *codec = dai->codec; |
Ian Lartey | 992bee4 | 2010-07-31 00:32:11 +0100 | [diff] [blame] | 219 | struct wm8741_priv *wm8741 = snd_soc_codec_get_drvdata(codec); |
| 220 | u16 iface = snd_soc_read(codec, WM8741_FORMAT_CONTROL) & 0x1FC; |
| 221 | int i; |
| 222 | |
Sergej Sawazki | e369bd0 | 2015-06-06 11:25:48 +0200 | [diff] [blame] | 223 | /* The set of sample rates that can be supported depends on the |
| 224 | * MCLK supplied to the CODEC - enforce this. |
| 225 | */ |
| 226 | if (!wm8741->sysclk) { |
| 227 | dev_err(codec->dev, |
| 228 | "No MCLK configured, call set_sysclk() on init or in hw_params\n"); |
| 229 | return -EINVAL; |
| 230 | } |
| 231 | |
| 232 | /* Find a supported LRCLK rate */ |
| 233 | for (i = 0; i < wm8741->sysclk_constraints->count; i++) { |
| 234 | if (wm8741->sysclk_constraints->list[i] == params_rate(params)) |
Ian Lartey | 992bee4 | 2010-07-31 00:32:11 +0100 | [diff] [blame] | 235 | break; |
| 236 | } |
| 237 | |
Sergej Sawazki | e369bd0 | 2015-06-06 11:25:48 +0200 | [diff] [blame] | 238 | if (i == wm8741->sysclk_constraints->count) { |
| 239 | dev_err(codec->dev, "LRCLK %d unsupported with MCLK %d\n", |
| 240 | params_rate(params), wm8741->sysclk); |
Ian Lartey | 992bee4 | 2010-07-31 00:32:11 +0100 | [diff] [blame] | 241 | return -EINVAL; |
| 242 | } |
| 243 | |
| 244 | /* bit size */ |
Mark Brown | 34967ad | 2014-07-31 12:51:45 +0100 | [diff] [blame] | 245 | switch (params_width(params)) { |
| 246 | case 16: |
Ian Lartey | 992bee4 | 2010-07-31 00:32:11 +0100 | [diff] [blame] | 247 | break; |
Mark Brown | 34967ad | 2014-07-31 12:51:45 +0100 | [diff] [blame] | 248 | case 20: |
Ian Lartey | 992bee4 | 2010-07-31 00:32:11 +0100 | [diff] [blame] | 249 | iface |= 0x0001; |
| 250 | break; |
Mark Brown | 34967ad | 2014-07-31 12:51:45 +0100 | [diff] [blame] | 251 | case 24: |
Ian Lartey | 992bee4 | 2010-07-31 00:32:11 +0100 | [diff] [blame] | 252 | iface |= 0x0002; |
| 253 | break; |
Mark Brown | 34967ad | 2014-07-31 12:51:45 +0100 | [diff] [blame] | 254 | case 32: |
Ian Lartey | 992bee4 | 2010-07-31 00:32:11 +0100 | [diff] [blame] | 255 | iface |= 0x0003; |
| 256 | break; |
| 257 | default: |
| 258 | dev_dbg(codec->dev, "wm8741_hw_params: Unsupported bit size param = %d", |
Mark Brown | 34967ad | 2014-07-31 12:51:45 +0100 | [diff] [blame] | 259 | params_width(params)); |
Ian Lartey | 992bee4 | 2010-07-31 00:32:11 +0100 | [diff] [blame] | 260 | return -EINVAL; |
| 261 | } |
| 262 | |
Sergej Sawazki | e369bd0 | 2015-06-06 11:25:48 +0200 | [diff] [blame] | 263 | dev_dbg(codec->dev, "wm8741_hw_params: bit size param = %d, rate param = %d", |
| 264 | params_width(params), params_rate(params)); |
Ian Lartey | 992bee4 | 2010-07-31 00:32:11 +0100 | [diff] [blame] | 265 | |
| 266 | snd_soc_write(codec, WM8741_FORMAT_CONTROL, iface); |
| 267 | return 0; |
| 268 | } |
| 269 | |
| 270 | static int wm8741_set_dai_sysclk(struct snd_soc_dai *codec_dai, |
| 271 | int clk_id, unsigned int freq, int dir) |
| 272 | { |
| 273 | struct snd_soc_codec *codec = codec_dai->codec; |
| 274 | struct wm8741_priv *wm8741 = snd_soc_codec_get_drvdata(codec); |
Ian Lartey | 992bee4 | 2010-07-31 00:32:11 +0100 | [diff] [blame] | 275 | |
| 276 | dev_dbg(codec->dev, "wm8741_set_dai_sysclk info: freq=%dHz\n", freq); |
| 277 | |
Ian Lartey | 3fe4a5e | 2010-08-27 15:26:27 +0100 | [diff] [blame] | 278 | switch (freq) { |
Sergej Sawazki | e369bd0 | 2015-06-06 11:25:48 +0200 | [diff] [blame] | 279 | case 0: |
| 280 | wm8741->sysclk_constraints = NULL; |
| 281 | wm8741->sysclk = freq; |
| 282 | return 0; |
| 283 | |
Ian Lartey | 3fe4a5e | 2010-08-27 15:26:27 +0100 | [diff] [blame] | 284 | case 11289600: |
| 285 | wm8741->sysclk_constraints = &constraints_11289; |
| 286 | wm8741->sysclk = freq; |
| 287 | return 0; |
Ian Lartey | 992bee4 | 2010-07-31 00:32:11 +0100 | [diff] [blame] | 288 | |
Ian Lartey | 3fe4a5e | 2010-08-27 15:26:27 +0100 | [diff] [blame] | 289 | case 12288000: |
| 290 | wm8741->sysclk_constraints = &constraints_12288; |
| 291 | wm8741->sysclk = freq; |
| 292 | return 0; |
Ian Lartey | 992bee4 | 2010-07-31 00:32:11 +0100 | [diff] [blame] | 293 | |
Ian Lartey | 3fe4a5e | 2010-08-27 15:26:27 +0100 | [diff] [blame] | 294 | case 16384000: |
| 295 | wm8741->sysclk_constraints = &constraints_16384; |
| 296 | wm8741->sysclk = freq; |
| 297 | return 0; |
Ian Lartey | 992bee4 | 2010-07-31 00:32:11 +0100 | [diff] [blame] | 298 | |
Ian Lartey | 3fe4a5e | 2010-08-27 15:26:27 +0100 | [diff] [blame] | 299 | case 16934400: |
| 300 | wm8741->sysclk_constraints = &constraints_16934; |
| 301 | wm8741->sysclk = freq; |
| 302 | return 0; |
| 303 | |
| 304 | case 18432000: |
| 305 | wm8741->sysclk_constraints = &constraints_18432; |
| 306 | wm8741->sysclk = freq; |
| 307 | return 0; |
| 308 | |
| 309 | case 22579200: |
| 310 | case 33868800: |
| 311 | wm8741->sysclk_constraints = &constraints_22579; |
| 312 | wm8741->sysclk = freq; |
| 313 | return 0; |
| 314 | |
| 315 | case 24576000: |
| 316 | wm8741->sysclk_constraints = &constraints_24576; |
| 317 | wm8741->sysclk = freq; |
| 318 | return 0; |
| 319 | |
| 320 | case 36864000: |
| 321 | wm8741->sysclk_constraints = &constraints_36864; |
| 322 | wm8741->sysclk = freq; |
| 323 | return 0; |
Ian Lartey | 992bee4 | 2010-07-31 00:32:11 +0100 | [diff] [blame] | 324 | } |
Ian Lartey | 3fe4a5e | 2010-08-27 15:26:27 +0100 | [diff] [blame] | 325 | return -EINVAL; |
Ian Lartey | 992bee4 | 2010-07-31 00:32:11 +0100 | [diff] [blame] | 326 | } |
| 327 | |
| 328 | static int wm8741_set_dai_fmt(struct snd_soc_dai *codec_dai, |
| 329 | unsigned int fmt) |
| 330 | { |
| 331 | struct snd_soc_codec *codec = codec_dai->codec; |
| 332 | u16 iface = snd_soc_read(codec, WM8741_FORMAT_CONTROL) & 0x1C3; |
| 333 | |
| 334 | /* check master/slave audio interface */ |
| 335 | switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) { |
| 336 | case SND_SOC_DAIFMT_CBS_CFS: |
| 337 | break; |
| 338 | default: |
| 339 | return -EINVAL; |
| 340 | } |
| 341 | |
| 342 | /* interface format */ |
| 343 | switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) { |
| 344 | case SND_SOC_DAIFMT_I2S: |
| 345 | iface |= 0x0008; |
| 346 | break; |
| 347 | case SND_SOC_DAIFMT_RIGHT_J: |
| 348 | break; |
| 349 | case SND_SOC_DAIFMT_LEFT_J: |
| 350 | iface |= 0x0004; |
| 351 | break; |
| 352 | case SND_SOC_DAIFMT_DSP_A: |
Axel Lin | 3a34010 | 2011-10-17 20:14:56 +0800 | [diff] [blame] | 353 | iface |= 0x000C; |
Ian Lartey | 992bee4 | 2010-07-31 00:32:11 +0100 | [diff] [blame] | 354 | break; |
| 355 | case SND_SOC_DAIFMT_DSP_B: |
Axel Lin | 3a34010 | 2011-10-17 20:14:56 +0800 | [diff] [blame] | 356 | iface |= 0x001C; |
Ian Lartey | 992bee4 | 2010-07-31 00:32:11 +0100 | [diff] [blame] | 357 | break; |
| 358 | default: |
| 359 | return -EINVAL; |
| 360 | } |
| 361 | |
| 362 | /* clock inversion */ |
| 363 | switch (fmt & SND_SOC_DAIFMT_INV_MASK) { |
| 364 | case SND_SOC_DAIFMT_NB_NF: |
| 365 | break; |
| 366 | case SND_SOC_DAIFMT_IB_IF: |
| 367 | iface |= 0x0010; |
| 368 | break; |
| 369 | case SND_SOC_DAIFMT_IB_NF: |
| 370 | iface |= 0x0020; |
| 371 | break; |
| 372 | case SND_SOC_DAIFMT_NB_IF: |
| 373 | iface |= 0x0030; |
| 374 | break; |
| 375 | default: |
| 376 | return -EINVAL; |
| 377 | } |
| 378 | |
| 379 | |
| 380 | dev_dbg(codec->dev, "wm8741_set_dai_fmt: Format=%x, Clock Inv=%x\n", |
| 381 | fmt & SND_SOC_DAIFMT_FORMAT_MASK, |
| 382 | ((fmt & SND_SOC_DAIFMT_INV_MASK))); |
| 383 | |
| 384 | snd_soc_write(codec, WM8741_FORMAT_CONTROL, iface); |
| 385 | return 0; |
| 386 | } |
| 387 | |
| 388 | #define WM8741_RATES (SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | \ |
| 389 | SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_88200 | \ |
| 390 | SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_176400 | \ |
| 391 | SNDRV_PCM_RATE_192000) |
| 392 | |
| 393 | #define WM8741_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\ |
| 394 | SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE) |
| 395 | |
Lars-Peter Clausen | 85e7652 | 2011-11-23 11:40:40 +0100 | [diff] [blame] | 396 | static const struct snd_soc_dai_ops wm8741_dai_ops = { |
Ian Lartey | 992bee4 | 2010-07-31 00:32:11 +0100 | [diff] [blame] | 397 | .startup = wm8741_startup, |
| 398 | .hw_params = wm8741_hw_params, |
| 399 | .set_sysclk = wm8741_set_dai_sysclk, |
| 400 | .set_fmt = wm8741_set_dai_fmt, |
| 401 | }; |
| 402 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 403 | static struct snd_soc_dai_driver wm8741_dai = { |
Ian Lartey | 30e2d36 | 2010-08-20 17:18:44 +0100 | [diff] [blame] | 404 | .name = "wm8741", |
Ian Lartey | 992bee4 | 2010-07-31 00:32:11 +0100 | [diff] [blame] | 405 | .playback = { |
| 406 | .stream_name = "Playback", |
Sergej Sawazki | c354b54 | 2015-05-13 11:39:01 +0200 | [diff] [blame] | 407 | .channels_min = 2, |
Ian Lartey | 992bee4 | 2010-07-31 00:32:11 +0100 | [diff] [blame] | 408 | .channels_max = 2, |
| 409 | .rates = WM8741_RATES, |
| 410 | .formats = WM8741_FORMATS, |
| 411 | }, |
| 412 | .ops = &wm8741_dai_ops, |
| 413 | }; |
Ian Lartey | 992bee4 | 2010-07-31 00:32:11 +0100 | [diff] [blame] | 414 | |
| 415 | #ifdef CONFIG_PM |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 416 | static int wm8741_resume(struct snd_soc_codec *codec) |
Ian Lartey | 992bee4 | 2010-07-31 00:32:11 +0100 | [diff] [blame] | 417 | { |
Axel Lin | df3431b | 2011-10-17 20:16:37 +0800 | [diff] [blame] | 418 | snd_soc_cache_sync(codec); |
Ian Lartey | 992bee4 | 2010-07-31 00:32:11 +0100 | [diff] [blame] | 419 | return 0; |
| 420 | } |
| 421 | #else |
Ian Lartey | 992bee4 | 2010-07-31 00:32:11 +0100 | [diff] [blame] | 422 | #define wm8741_resume NULL |
| 423 | #endif |
| 424 | |
Sergej Sawazki | c354b54 | 2015-05-13 11:39:01 +0200 | [diff] [blame] | 425 | static int wm8741_configure(struct snd_soc_codec *codec) |
| 426 | { |
| 427 | struct wm8741_priv *wm8741 = snd_soc_codec_get_drvdata(codec); |
| 428 | |
| 429 | /* Configure differential mode */ |
| 430 | switch (wm8741->pdata.diff_mode) { |
| 431 | case WM8741_DIFF_MODE_STEREO: |
| 432 | case WM8741_DIFF_MODE_STEREO_REVERSED: |
| 433 | case WM8741_DIFF_MODE_MONO_LEFT: |
| 434 | case WM8741_DIFF_MODE_MONO_RIGHT: |
| 435 | snd_soc_update_bits(codec, WM8741_MODE_CONTROL_2, |
| 436 | WM8741_DIFF_MASK, |
| 437 | wm8741->pdata.diff_mode << WM8741_DIFF_SHIFT); |
| 438 | break; |
| 439 | default: |
| 440 | return -EINVAL; |
| 441 | } |
| 442 | |
| 443 | /* Change some default settings - latch VU */ |
| 444 | snd_soc_update_bits(codec, WM8741_DACLLSB_ATTENUATION, |
| 445 | WM8741_UPDATELL, WM8741_UPDATELL); |
| 446 | snd_soc_update_bits(codec, WM8741_DACLMSB_ATTENUATION, |
| 447 | WM8741_UPDATELM, WM8741_UPDATELM); |
| 448 | snd_soc_update_bits(codec, WM8741_DACRLSB_ATTENUATION, |
| 449 | WM8741_UPDATERL, WM8741_UPDATERL); |
| 450 | snd_soc_update_bits(codec, WM8741_DACRMSB_ATTENUATION, |
| 451 | WM8741_UPDATERM, WM8741_UPDATERM); |
| 452 | |
| 453 | return 0; |
| 454 | } |
| 455 | |
| 456 | static int wm8741_add_controls(struct snd_soc_codec *codec) |
| 457 | { |
| 458 | struct wm8741_priv *wm8741 = snd_soc_codec_get_drvdata(codec); |
| 459 | |
| 460 | switch (wm8741->pdata.diff_mode) { |
| 461 | case WM8741_DIFF_MODE_STEREO: |
| 462 | case WM8741_DIFF_MODE_STEREO_REVERSED: |
| 463 | snd_soc_add_codec_controls(codec, |
| 464 | wm8741_snd_controls_stereo, |
| 465 | ARRAY_SIZE(wm8741_snd_controls_stereo)); |
| 466 | break; |
| 467 | case WM8741_DIFF_MODE_MONO_LEFT: |
| 468 | snd_soc_add_codec_controls(codec, |
| 469 | wm8741_snd_controls_mono_left, |
| 470 | ARRAY_SIZE(wm8741_snd_controls_mono_left)); |
| 471 | break; |
| 472 | case WM8741_DIFF_MODE_MONO_RIGHT: |
| 473 | snd_soc_add_codec_controls(codec, |
| 474 | wm8741_snd_controls_mono_right, |
| 475 | ARRAY_SIZE(wm8741_snd_controls_mono_right)); |
| 476 | break; |
| 477 | default: |
| 478 | return -EINVAL; |
| 479 | } |
| 480 | |
| 481 | return 0; |
| 482 | } |
| 483 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 484 | static int wm8741_probe(struct snd_soc_codec *codec) |
Ian Lartey | 992bee4 | 2010-07-31 00:32:11 +0100 | [diff] [blame] | 485 | { |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 486 | struct wm8741_priv *wm8741 = snd_soc_codec_get_drvdata(codec); |
Ian Lartey | 992bee4 | 2010-07-31 00:32:11 +0100 | [diff] [blame] | 487 | int ret = 0; |
Mark Brown | 398575d | 2011-08-03 17:16:11 +0900 | [diff] [blame] | 488 | |
| 489 | ret = regulator_bulk_enable(ARRAY_SIZE(wm8741->supplies), |
| 490 | wm8741->supplies); |
| 491 | if (ret != 0) { |
| 492 | dev_err(codec->dev, "Failed to enable supplies: %d\n", ret); |
| 493 | goto err_get; |
| 494 | } |
Ian Lartey | 992bee4 | 2010-07-31 00:32:11 +0100 | [diff] [blame] | 495 | |
Ian Lartey | 992bee4 | 2010-07-31 00:32:11 +0100 | [diff] [blame] | 496 | ret = wm8741_reset(codec); |
| 497 | if (ret < 0) { |
| 498 | dev_err(codec->dev, "Failed to issue reset\n"); |
Mark Brown | 398575d | 2011-08-03 17:16:11 +0900 | [diff] [blame] | 499 | goto err_enable; |
Ian Lartey | 992bee4 | 2010-07-31 00:32:11 +0100 | [diff] [blame] | 500 | } |
| 501 | |
Sergej Sawazki | c354b54 | 2015-05-13 11:39:01 +0200 | [diff] [blame] | 502 | ret = wm8741_configure(codec); |
| 503 | if (ret < 0) { |
| 504 | dev_err(codec->dev, "Failed to change default settings\n"); |
| 505 | goto err_enable; |
| 506 | } |
| 507 | |
| 508 | ret = wm8741_add_controls(codec); |
| 509 | if (ret < 0) { |
| 510 | dev_err(codec->dev, "Failed to add controls\n"); |
| 511 | goto err_enable; |
| 512 | } |
Ian Lartey | 992bee4 | 2010-07-31 00:32:11 +0100 | [diff] [blame] | 513 | |
Ian Lartey | 992bee4 | 2010-07-31 00:32:11 +0100 | [diff] [blame] | 514 | dev_dbg(codec->dev, "Successful registration\n"); |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 515 | return ret; |
Mark Brown | 398575d | 2011-08-03 17:16:11 +0900 | [diff] [blame] | 516 | |
| 517 | err_enable: |
| 518 | regulator_bulk_disable(ARRAY_SIZE(wm8741->supplies), wm8741->supplies); |
| 519 | err_get: |
Mark Brown | 398575d | 2011-08-03 17:16:11 +0900 | [diff] [blame] | 520 | return ret; |
| 521 | } |
| 522 | |
| 523 | static int wm8741_remove(struct snd_soc_codec *codec) |
| 524 | { |
| 525 | struct wm8741_priv *wm8741 = snd_soc_codec_get_drvdata(codec); |
| 526 | |
| 527 | regulator_bulk_disable(ARRAY_SIZE(wm8741->supplies), wm8741->supplies); |
Mark Brown | 398575d | 2011-08-03 17:16:11 +0900 | [diff] [blame] | 528 | |
| 529 | return 0; |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 530 | } |
| 531 | |
| 532 | static struct snd_soc_codec_driver soc_codec_dev_wm8741 = { |
| 533 | .probe = wm8741_probe, |
Mark Brown | 398575d | 2011-08-03 17:16:11 +0900 | [diff] [blame] | 534 | .remove = wm8741_remove, |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 535 | .resume = wm8741_resume, |
Mark Brown | 0e62780 | 2011-12-03 17:15:06 +0000 | [diff] [blame] | 536 | |
Mark Brown | 0e62780 | 2011-12-03 17:15:06 +0000 | [diff] [blame] | 537 | .dapm_widgets = wm8741_dapm_widgets, |
| 538 | .num_dapm_widgets = ARRAY_SIZE(wm8741_dapm_widgets), |
| 539 | .dapm_routes = wm8741_dapm_routes, |
| 540 | .num_dapm_routes = ARRAY_SIZE(wm8741_dapm_routes), |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 541 | }; |
| 542 | |
Mark Brown | 80080ec | 2011-08-03 17:31:26 +0900 | [diff] [blame] | 543 | static const struct of_device_id wm8741_of_match[] = { |
| 544 | { .compatible = "wlf,wm8741", }, |
| 545 | { } |
| 546 | }; |
| 547 | MODULE_DEVICE_TABLE(of, wm8741_of_match); |
| 548 | |
Mark Brown | fe98c0c | 2012-09-10 18:00:21 +0800 | [diff] [blame] | 549 | static const struct regmap_config wm8741_regmap = { |
| 550 | .reg_bits = 7, |
| 551 | .val_bits = 9, |
| 552 | .max_register = WM8741_MAX_REGISTER, |
| 553 | |
| 554 | .reg_defaults = wm8741_reg_defaults, |
| 555 | .num_reg_defaults = ARRAY_SIZE(wm8741_reg_defaults), |
| 556 | .cache_type = REGCACHE_RBTREE, |
| 557 | |
| 558 | .readable_reg = wm8741_readable, |
| 559 | }; |
| 560 | |
Sergej Sawazki | c354b54 | 2015-05-13 11:39:01 +0200 | [diff] [blame] | 561 | static int wm8741_set_pdata(struct device *dev, struct wm8741_priv *wm8741) |
| 562 | { |
| 563 | const struct wm8741_platform_data *pdata = dev_get_platdata(dev); |
| 564 | u32 diff_mode; |
| 565 | |
| 566 | if (dev->of_node) { |
| 567 | if (of_property_read_u32(dev->of_node, "diff-mode", &diff_mode) |
| 568 | >= 0) |
| 569 | wm8741->pdata.diff_mode = diff_mode; |
| 570 | } else { |
| 571 | if (pdata != NULL) |
| 572 | memcpy(&wm8741->pdata, pdata, sizeof(wm8741->pdata)); |
| 573 | } |
| 574 | |
| 575 | return 0; |
| 576 | } |
| 577 | |
Fabio Estevam | 26090a8 | 2013-11-21 12:38:45 -0200 | [diff] [blame] | 578 | #if IS_ENABLED(CONFIG_I2C) |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 579 | static int wm8741_i2c_probe(struct i2c_client *i2c, |
| 580 | const struct i2c_device_id *id) |
| 581 | { |
| 582 | struct wm8741_priv *wm8741; |
Mark Brown | d978055 | 2012-09-10 17:52:59 +0800 | [diff] [blame] | 583 | int ret, i; |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 584 | |
Mark Brown | 5aefb30 | 2011-12-03 17:17:05 +0000 | [diff] [blame] | 585 | wm8741 = devm_kzalloc(&i2c->dev, sizeof(struct wm8741_priv), |
| 586 | GFP_KERNEL); |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 587 | if (wm8741 == NULL) |
| 588 | return -ENOMEM; |
| 589 | |
Mark Brown | d978055 | 2012-09-10 17:52:59 +0800 | [diff] [blame] | 590 | for (i = 0; i < ARRAY_SIZE(wm8741->supplies); i++) |
| 591 | wm8741->supplies[i].supply = wm8741_supply_names[i]; |
| 592 | |
| 593 | ret = devm_regulator_bulk_get(&i2c->dev, ARRAY_SIZE(wm8741->supplies), |
| 594 | wm8741->supplies); |
| 595 | if (ret != 0) { |
Mark Brown | fe98c0c | 2012-09-10 18:00:21 +0800 | [diff] [blame] | 596 | dev_err(&i2c->dev, "Failed to request supplies: %d\n", ret); |
| 597 | return ret; |
| 598 | } |
| 599 | |
Tushar Behera | fd64c45 | 2012-11-22 09:38:37 +0530 | [diff] [blame] | 600 | wm8741->regmap = devm_regmap_init_i2c(i2c, &wm8741_regmap); |
Mark Brown | fe98c0c | 2012-09-10 18:00:21 +0800 | [diff] [blame] | 601 | if (IS_ERR(wm8741->regmap)) { |
| 602 | ret = PTR_ERR(wm8741->regmap); |
| 603 | dev_err(&i2c->dev, "Failed to init regmap: %d\n", ret); |
| 604 | return ret; |
Mark Brown | d978055 | 2012-09-10 17:52:59 +0800 | [diff] [blame] | 605 | } |
| 606 | |
Dan Carpenter | 2d52d17 | 2015-05-20 10:40:35 +0300 | [diff] [blame] | 607 | ret = wm8741_set_pdata(&i2c->dev, wm8741); |
Sergej Sawazki | c354b54 | 2015-05-13 11:39:01 +0200 | [diff] [blame] | 608 | if (ret != 0) { |
| 609 | dev_err(&i2c->dev, "Failed to set pdata: %d\n", ret); |
| 610 | return ret; |
| 611 | } |
| 612 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 613 | i2c_set_clientdata(i2c, wm8741); |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 614 | |
Mark Brown | 398575d | 2011-08-03 17:16:11 +0900 | [diff] [blame] | 615 | ret = snd_soc_register_codec(&i2c->dev, |
| 616 | &soc_codec_dev_wm8741, &wm8741_dai, 1); |
Mark Brown | 398575d | 2011-08-03 17:16:11 +0900 | [diff] [blame] | 617 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 618 | return ret; |
Ian Lartey | 992bee4 | 2010-07-31 00:32:11 +0100 | [diff] [blame] | 619 | } |
| 620 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 621 | static int wm8741_i2c_remove(struct i2c_client *client) |
Ian Lartey | 992bee4 | 2010-07-31 00:32:11 +0100 | [diff] [blame] | 622 | { |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 623 | snd_soc_unregister_codec(&client->dev); |
Ian Lartey | 992bee4 | 2010-07-31 00:32:11 +0100 | [diff] [blame] | 624 | return 0; |
| 625 | } |
| 626 | |
| 627 | static const struct i2c_device_id wm8741_i2c_id[] = { |
| 628 | { "wm8741", 0 }, |
| 629 | { } |
| 630 | }; |
| 631 | MODULE_DEVICE_TABLE(i2c, wm8741_i2c_id); |
| 632 | |
Ian Lartey | 992bee4 | 2010-07-31 00:32:11 +0100 | [diff] [blame] | 633 | static struct i2c_driver wm8741_i2c_driver = { |
| 634 | .driver = { |
Mark Brown | 0473e61 | 2011-08-03 16:52:10 +0900 | [diff] [blame] | 635 | .name = "wm8741", |
Ian Lartey | 992bee4 | 2010-07-31 00:32:11 +0100 | [diff] [blame] | 636 | .owner = THIS_MODULE, |
Mark Brown | 80080ec | 2011-08-03 17:31:26 +0900 | [diff] [blame] | 637 | .of_match_table = wm8741_of_match, |
Ian Lartey | 992bee4 | 2010-07-31 00:32:11 +0100 | [diff] [blame] | 638 | }, |
| 639 | .probe = wm8741_i2c_probe, |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 640 | .remove = wm8741_i2c_remove, |
Ian Lartey | 992bee4 | 2010-07-31 00:32:11 +0100 | [diff] [blame] | 641 | .id_table = wm8741_i2c_id, |
| 642 | }; |
| 643 | #endif |
| 644 | |
Mark Brown | 39e9b8d | 2011-08-03 17:30:57 +0900 | [diff] [blame] | 645 | #if defined(CONFIG_SPI_MASTER) |
Bill Pemberton | 7a79e94 | 2012-12-07 09:26:37 -0500 | [diff] [blame] | 646 | static int wm8741_spi_probe(struct spi_device *spi) |
Mark Brown | 39e9b8d | 2011-08-03 17:30:57 +0900 | [diff] [blame] | 647 | { |
| 648 | struct wm8741_priv *wm8741; |
Mark Brown | d978055 | 2012-09-10 17:52:59 +0800 | [diff] [blame] | 649 | int ret, i; |
Mark Brown | 39e9b8d | 2011-08-03 17:30:57 +0900 | [diff] [blame] | 650 | |
Mark Brown | 5aefb30 | 2011-12-03 17:17:05 +0000 | [diff] [blame] | 651 | wm8741 = devm_kzalloc(&spi->dev, sizeof(struct wm8741_priv), |
| 652 | GFP_KERNEL); |
Mark Brown | 39e9b8d | 2011-08-03 17:30:57 +0900 | [diff] [blame] | 653 | if (wm8741 == NULL) |
| 654 | return -ENOMEM; |
| 655 | |
Mark Brown | d978055 | 2012-09-10 17:52:59 +0800 | [diff] [blame] | 656 | for (i = 0; i < ARRAY_SIZE(wm8741->supplies); i++) |
| 657 | wm8741->supplies[i].supply = wm8741_supply_names[i]; |
| 658 | |
Mark Brown | fe98c0c | 2012-09-10 18:00:21 +0800 | [diff] [blame] | 659 | ret = devm_regulator_bulk_get(&spi->dev, ARRAY_SIZE(wm8741->supplies), |
Mark Brown | d978055 | 2012-09-10 17:52:59 +0800 | [diff] [blame] | 660 | wm8741->supplies); |
| 661 | if (ret != 0) { |
| 662 | dev_err(&spi->dev, "Failed to request supplies: %d\n", ret); |
Mark Brown | fe98c0c | 2012-09-10 18:00:21 +0800 | [diff] [blame] | 663 | return ret; |
Mark Brown | d978055 | 2012-09-10 17:52:59 +0800 | [diff] [blame] | 664 | } |
| 665 | |
Tushar Behera | fd64c45 | 2012-11-22 09:38:37 +0530 | [diff] [blame] | 666 | wm8741->regmap = devm_regmap_init_spi(spi, &wm8741_regmap); |
Mark Brown | fe98c0c | 2012-09-10 18:00:21 +0800 | [diff] [blame] | 667 | if (IS_ERR(wm8741->regmap)) { |
| 668 | ret = PTR_ERR(wm8741->regmap); |
| 669 | dev_err(&spi->dev, "Failed to init regmap: %d\n", ret); |
| 670 | return ret; |
| 671 | } |
| 672 | |
Dan Carpenter | 2d52d17 | 2015-05-20 10:40:35 +0300 | [diff] [blame] | 673 | ret = wm8741_set_pdata(&spi->dev, wm8741); |
Sergej Sawazki | c354b54 | 2015-05-13 11:39:01 +0200 | [diff] [blame] | 674 | if (ret != 0) { |
| 675 | dev_err(&spi->dev, "Failed to set pdata: %d\n", ret); |
| 676 | return ret; |
| 677 | } |
| 678 | |
Mark Brown | 39e9b8d | 2011-08-03 17:30:57 +0900 | [diff] [blame] | 679 | spi_set_drvdata(spi, wm8741); |
| 680 | |
| 681 | ret = snd_soc_register_codec(&spi->dev, |
| 682 | &soc_codec_dev_wm8741, &wm8741_dai, 1); |
Mark Brown | 39e9b8d | 2011-08-03 17:30:57 +0900 | [diff] [blame] | 683 | return ret; |
| 684 | } |
| 685 | |
Bill Pemberton | 7a79e94 | 2012-12-07 09:26:37 -0500 | [diff] [blame] | 686 | static int wm8741_spi_remove(struct spi_device *spi) |
Mark Brown | 39e9b8d | 2011-08-03 17:30:57 +0900 | [diff] [blame] | 687 | { |
| 688 | snd_soc_unregister_codec(&spi->dev); |
Mark Brown | 39e9b8d | 2011-08-03 17:30:57 +0900 | [diff] [blame] | 689 | return 0; |
| 690 | } |
| 691 | |
| 692 | static struct spi_driver wm8741_spi_driver = { |
| 693 | .driver = { |
| 694 | .name = "wm8741", |
| 695 | .owner = THIS_MODULE, |
Mark Brown | 80080ec | 2011-08-03 17:31:26 +0900 | [diff] [blame] | 696 | .of_match_table = wm8741_of_match, |
Mark Brown | 39e9b8d | 2011-08-03 17:30:57 +0900 | [diff] [blame] | 697 | }, |
| 698 | .probe = wm8741_spi_probe, |
Bill Pemberton | 7a79e94 | 2012-12-07 09:26:37 -0500 | [diff] [blame] | 699 | .remove = wm8741_spi_remove, |
Mark Brown | 39e9b8d | 2011-08-03 17:30:57 +0900 | [diff] [blame] | 700 | }; |
| 701 | #endif /* CONFIG_SPI_MASTER */ |
| 702 | |
Ian Lartey | 992bee4 | 2010-07-31 00:32:11 +0100 | [diff] [blame] | 703 | static int __init wm8741_modinit(void) |
| 704 | { |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 705 | int ret = 0; |
| 706 | |
Fabio Estevam | 26090a8 | 2013-11-21 12:38:45 -0200 | [diff] [blame] | 707 | #if IS_ENABLED(CONFIG_I2C) |
Ian Lartey | 992bee4 | 2010-07-31 00:32:11 +0100 | [diff] [blame] | 708 | ret = i2c_add_driver(&wm8741_i2c_driver); |
Ian Lartey | 3fe4a5e | 2010-08-27 15:26:27 +0100 | [diff] [blame] | 709 | if (ret != 0) |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 710 | pr_err("Failed to register WM8741 I2C driver: %d\n", ret); |
Ian Lartey | 992bee4 | 2010-07-31 00:32:11 +0100 | [diff] [blame] | 711 | #endif |
Mark Brown | 39e9b8d | 2011-08-03 17:30:57 +0900 | [diff] [blame] | 712 | #if defined(CONFIG_SPI_MASTER) |
| 713 | ret = spi_register_driver(&wm8741_spi_driver); |
| 714 | if (ret != 0) { |
| 715 | printk(KERN_ERR "Failed to register wm8741 SPI driver: %d\n", |
| 716 | ret); |
| 717 | } |
| 718 | #endif |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 719 | |
| 720 | return ret; |
Ian Lartey | 992bee4 | 2010-07-31 00:32:11 +0100 | [diff] [blame] | 721 | } |
| 722 | module_init(wm8741_modinit); |
| 723 | |
| 724 | static void __exit wm8741_exit(void) |
| 725 | { |
Mark Brown | 39e9b8d | 2011-08-03 17:30:57 +0900 | [diff] [blame] | 726 | #if defined(CONFIG_SPI_MASTER) |
| 727 | spi_unregister_driver(&wm8741_spi_driver); |
| 728 | #endif |
Fabio Estevam | 26090a8 | 2013-11-21 12:38:45 -0200 | [diff] [blame] | 729 | #if IS_ENABLED(CONFIG_I2C) |
Ian Lartey | 992bee4 | 2010-07-31 00:32:11 +0100 | [diff] [blame] | 730 | i2c_del_driver(&wm8741_i2c_driver); |
| 731 | #endif |
| 732 | } |
| 733 | module_exit(wm8741_exit); |
| 734 | |
| 735 | MODULE_DESCRIPTION("ASoC WM8741 driver"); |
| 736 | MODULE_AUTHOR("Ian Lartey <ian@opensource.wolfsonmicro.com>"); |
| 737 | MODULE_LICENSE("GPL"); |