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