Mark Brown | 3a66d38 | 2010-02-11 13:27:19 +0000 | [diff] [blame] | 1 | /* |
| 2 | * wm2000.c -- WM2000 ALSA Soc Audio driver |
| 3 | * |
Mark Brown | 656baae | 2012-05-23 12:39:07 +0100 | [diff] [blame] | 4 | * Copyright 2008-2011 Wolfson Microelectronics PLC. |
Mark Brown | 3a66d38 | 2010-02-11 13:27:19 +0000 | [diff] [blame] | 5 | * |
| 6 | * Author: Mark Brown <broonie@opensource.wolfsonmicro.com> |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License version 2 as |
| 10 | * published by the Free Software Foundation. |
| 11 | * |
| 12 | * The download image for the WM2000 will be requested as |
| 13 | * 'wm2000_anc.bin' by default (overridable via platform data) at |
| 14 | * runtime and is expected to be in flat binary format. This is |
| 15 | * generated by Wolfson configuration tools and includes |
Markus Elfring | cdbd9b0 | 2017-11-24 08:02:57 +0100 | [diff] [blame] | 16 | * system-specific calibration information. If supplied as a |
Mark Brown | 3a66d38 | 2010-02-11 13:27:19 +0000 | [diff] [blame] | 17 | * sequence of ASCII-encoded hexidecimal bytes this can be converted |
| 18 | * into a flat binary with a command such as this on the command line: |
| 19 | * |
| 20 | * perl -e 'while (<>) { s/[\r\n]+// ; printf("%c", hex($_)); }' |
| 21 | * < file > wm2000_anc.bin |
| 22 | */ |
| 23 | |
| 24 | #include <linux/module.h> |
| 25 | #include <linux/moduleparam.h> |
Mark Brown | 3a66d38 | 2010-02-11 13:27:19 +0000 | [diff] [blame] | 26 | #include <linux/kernel.h> |
| 27 | #include <linux/init.h> |
| 28 | #include <linux/firmware.h> |
Mark Brown | 514cfd6 | 2012-12-13 17:29:00 +0900 | [diff] [blame] | 29 | #include <linux/clk.h> |
Mark Brown | 3a66d38 | 2010-02-11 13:27:19 +0000 | [diff] [blame] | 30 | #include <linux/delay.h> |
| 31 | #include <linux/pm.h> |
| 32 | #include <linux/i2c.h> |
Mark Brown | 8aa1fe8 | 2011-12-02 21:57:19 +0000 | [diff] [blame] | 33 | #include <linux/regmap.h> |
Mark Brown | 3a66d38 | 2010-02-11 13:27:19 +0000 | [diff] [blame] | 34 | #include <linux/debugfs.h> |
Mark Brown | a89be93 | 2012-09-22 18:33:23 -0400 | [diff] [blame] | 35 | #include <linux/regulator/consumer.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 36 | #include <linux/slab.h> |
Mark Brown | 3a66d38 | 2010-02-11 13:27:19 +0000 | [diff] [blame] | 37 | #include <sound/core.h> |
| 38 | #include <sound/pcm.h> |
| 39 | #include <sound/pcm_params.h> |
| 40 | #include <sound/soc.h> |
Mark Brown | 3a66d38 | 2010-02-11 13:27:19 +0000 | [diff] [blame] | 41 | #include <sound/initval.h> |
| 42 | #include <sound/tlv.h> |
| 43 | |
| 44 | #include <sound/wm2000.h> |
| 45 | |
| 46 | #include "wm2000.h" |
| 47 | |
Mark Brown | a89be93 | 2012-09-22 18:33:23 -0400 | [diff] [blame] | 48 | #define WM2000_NUM_SUPPLIES 3 |
| 49 | |
| 50 | static const char *wm2000_supplies[WM2000_NUM_SUPPLIES] = { |
| 51 | "SPKVDD", |
| 52 | "DBVDD", |
| 53 | "DCVDD", |
| 54 | }; |
| 55 | |
Mark Brown | 3a66d38 | 2010-02-11 13:27:19 +0000 | [diff] [blame] | 56 | enum wm2000_anc_mode { |
| 57 | ANC_ACTIVE = 0, |
| 58 | ANC_BYPASS = 1, |
| 59 | ANC_STANDBY = 2, |
| 60 | ANC_OFF = 3, |
| 61 | }; |
| 62 | |
| 63 | struct wm2000_priv { |
| 64 | struct i2c_client *i2c; |
Mark Brown | 8aa1fe8 | 2011-12-02 21:57:19 +0000 | [diff] [blame] | 65 | struct regmap *regmap; |
Mark Brown | 514cfd6 | 2012-12-13 17:29:00 +0900 | [diff] [blame] | 66 | struct clk *mclk; |
Mark Brown | 3a66d38 | 2010-02-11 13:27:19 +0000 | [diff] [blame] | 67 | |
Mark Brown | a89be93 | 2012-09-22 18:33:23 -0400 | [diff] [blame] | 68 | struct regulator_bulk_data supplies[WM2000_NUM_SUPPLIES]; |
| 69 | |
Mark Brown | 3a66d38 | 2010-02-11 13:27:19 +0000 | [diff] [blame] | 70 | enum wm2000_anc_mode anc_mode; |
| 71 | |
| 72 | unsigned int anc_active:1; |
| 73 | unsigned int anc_eng_ena:1; |
| 74 | unsigned int spk_ena:1; |
| 75 | |
Mark Brown | 3a66d38 | 2010-02-11 13:27:19 +0000 | [diff] [blame] | 76 | unsigned int speech_clarity:1; |
| 77 | |
| 78 | int anc_download_size; |
| 79 | char *anc_download; |
Mark Brown | 8e9bb42 | 2013-01-23 18:38:54 +0800 | [diff] [blame] | 80 | |
| 81 | struct mutex lock; |
Mark Brown | 3a66d38 | 2010-02-11 13:27:19 +0000 | [diff] [blame] | 82 | }; |
| 83 | |
Mark Brown | 3a66d38 | 2010-02-11 13:27:19 +0000 | [diff] [blame] | 84 | static int wm2000_write(struct i2c_client *i2c, unsigned int reg, |
| 85 | unsigned int value) |
| 86 | { |
Mark Brown | 8aa1fe8 | 2011-12-02 21:57:19 +0000 | [diff] [blame] | 87 | struct wm2000_priv *wm2000 = i2c_get_clientdata(i2c); |
| 88 | return regmap_write(wm2000->regmap, reg, value); |
Mark Brown | 3a66d38 | 2010-02-11 13:27:19 +0000 | [diff] [blame] | 89 | } |
| 90 | |
Mark Brown | 3a66d38 | 2010-02-11 13:27:19 +0000 | [diff] [blame] | 91 | static void wm2000_reset(struct wm2000_priv *wm2000) |
| 92 | { |
| 93 | struct i2c_client *i2c = wm2000->i2c; |
| 94 | |
| 95 | wm2000_write(i2c, WM2000_REG_SYS_CTL2, WM2000_ANC_ENG_CLR); |
| 96 | wm2000_write(i2c, WM2000_REG_SYS_CTL2, WM2000_RAM_CLR); |
| 97 | wm2000_write(i2c, WM2000_REG_ID1, 0); |
| 98 | |
| 99 | wm2000->anc_mode = ANC_OFF; |
| 100 | } |
| 101 | |
| 102 | static int wm2000_poll_bit(struct i2c_client *i2c, |
Mark Brown | 3ff1ec2 | 2012-06-08 06:53:48 +0800 | [diff] [blame] | 103 | unsigned int reg, u8 mask) |
Mark Brown | 3a66d38 | 2010-02-11 13:27:19 +0000 | [diff] [blame] | 104 | { |
Lucas Tanure | c5d0948 | 2018-10-19 17:44:22 +0100 | [diff] [blame] | 105 | struct wm2000_priv *wm2000 = i2c_get_clientdata(i2c); |
Mark Brown | 3ff1ec2 | 2012-06-08 06:53:48 +0800 | [diff] [blame] | 106 | int timeout = 4000; |
Lucas Tanure | c5d0948 | 2018-10-19 17:44:22 +0100 | [diff] [blame] | 107 | unsigned int val; |
Mark Brown | 3a66d38 | 2010-02-11 13:27:19 +0000 | [diff] [blame] | 108 | |
Lucas Tanure | c5d0948 | 2018-10-19 17:44:22 +0100 | [diff] [blame] | 109 | regmap_read(wm2000->regmap, reg, &val); |
Mark Brown | 3a66d38 | 2010-02-11 13:27:19 +0000 | [diff] [blame] | 110 | |
| 111 | while (!(val & mask) && --timeout) { |
| 112 | msleep(1); |
Lucas Tanure | c5d0948 | 2018-10-19 17:44:22 +0100 | [diff] [blame] | 113 | regmap_read(wm2000->regmap, reg, &val); |
Mark Brown | 3a66d38 | 2010-02-11 13:27:19 +0000 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | if (timeout == 0) |
| 117 | return 0; |
| 118 | else |
| 119 | return 1; |
| 120 | } |
| 121 | |
| 122 | static int wm2000_power_up(struct i2c_client *i2c, int analogue) |
| 123 | { |
| 124 | struct wm2000_priv *wm2000 = dev_get_drvdata(&i2c->dev); |
Mark Brown | d61100b | 2012-12-14 15:16:58 +0900 | [diff] [blame] | 125 | unsigned long rate; |
Lucas Tanure | c5d0948 | 2018-10-19 17:44:22 +0100 | [diff] [blame] | 126 | unsigned int val; |
Mark Brown | 3ff1ec2 | 2012-06-08 06:53:48 +0800 | [diff] [blame] | 127 | int ret; |
Mark Brown | 3a66d38 | 2010-02-11 13:27:19 +0000 | [diff] [blame] | 128 | |
Takashi Iwai | bf90e89 | 2013-11-05 18:39:53 +0100 | [diff] [blame] | 129 | if (WARN_ON(wm2000->anc_mode != ANC_OFF)) |
| 130 | return -EINVAL; |
Mark Brown | 3a66d38 | 2010-02-11 13:27:19 +0000 | [diff] [blame] | 131 | |
| 132 | dev_dbg(&i2c->dev, "Beginning power up\n"); |
| 133 | |
Mark Brown | a89be93 | 2012-09-22 18:33:23 -0400 | [diff] [blame] | 134 | ret = regulator_bulk_enable(WM2000_NUM_SUPPLIES, wm2000->supplies); |
| 135 | if (ret != 0) { |
| 136 | dev_err(&i2c->dev, "Failed to enable supplies: %d\n", ret); |
| 137 | return ret; |
| 138 | } |
| 139 | |
Mark Brown | d61100b | 2012-12-14 15:16:58 +0900 | [diff] [blame] | 140 | rate = clk_get_rate(wm2000->mclk); |
| 141 | if (rate <= 13500000) { |
Mark Brown | 3a66d38 | 2010-02-11 13:27:19 +0000 | [diff] [blame] | 142 | dev_dbg(&i2c->dev, "Disabling MCLK divider\n"); |
| 143 | wm2000_write(i2c, WM2000_REG_SYS_CTL2, |
| 144 | WM2000_MCLK_DIV2_ENA_CLR); |
| 145 | } else { |
| 146 | dev_dbg(&i2c->dev, "Enabling MCLK divider\n"); |
| 147 | wm2000_write(i2c, WM2000_REG_SYS_CTL2, |
| 148 | WM2000_MCLK_DIV2_ENA_SET); |
| 149 | } |
| 150 | |
| 151 | wm2000_write(i2c, WM2000_REG_SYS_CTL2, WM2000_ANC_ENG_CLR); |
| 152 | wm2000_write(i2c, WM2000_REG_SYS_CTL2, WM2000_ANC_ENG_SET); |
| 153 | |
| 154 | /* Wait for ANC engine to become ready */ |
| 155 | if (!wm2000_poll_bit(i2c, WM2000_REG_ANC_STAT, |
Mark Brown | 3ff1ec2 | 2012-06-08 06:53:48 +0800 | [diff] [blame] | 156 | WM2000_ANC_ENG_IDLE)) { |
Mark Brown | 3a66d38 | 2010-02-11 13:27:19 +0000 | [diff] [blame] | 157 | dev_err(&i2c->dev, "ANC engine failed to reset\n"); |
Mark Brown | a89be93 | 2012-09-22 18:33:23 -0400 | [diff] [blame] | 158 | regulator_bulk_disable(WM2000_NUM_SUPPLIES, wm2000->supplies); |
Mark Brown | 3a66d38 | 2010-02-11 13:27:19 +0000 | [diff] [blame] | 159 | return -ETIMEDOUT; |
| 160 | } |
| 161 | |
| 162 | if (!wm2000_poll_bit(i2c, WM2000_REG_SYS_STATUS, |
Mark Brown | 3ff1ec2 | 2012-06-08 06:53:48 +0800 | [diff] [blame] | 163 | WM2000_STATUS_BOOT_COMPLETE)) { |
Mark Brown | 3a66d38 | 2010-02-11 13:27:19 +0000 | [diff] [blame] | 164 | dev_err(&i2c->dev, "ANC engine failed to initialise\n"); |
Mark Brown | a89be93 | 2012-09-22 18:33:23 -0400 | [diff] [blame] | 165 | regulator_bulk_disable(WM2000_NUM_SUPPLIES, wm2000->supplies); |
Mark Brown | 3a66d38 | 2010-02-11 13:27:19 +0000 | [diff] [blame] | 166 | return -ETIMEDOUT; |
| 167 | } |
| 168 | |
| 169 | wm2000_write(i2c, WM2000_REG_SYS_CTL2, WM2000_RAM_SET); |
| 170 | |
| 171 | /* Open code download of the data since it is the only bulk |
| 172 | * write we do. */ |
| 173 | dev_dbg(&i2c->dev, "Downloading %d bytes\n", |
| 174 | wm2000->anc_download_size - 2); |
| 175 | |
| 176 | ret = i2c_master_send(i2c, wm2000->anc_download, |
| 177 | wm2000->anc_download_size); |
| 178 | if (ret < 0) { |
| 179 | dev_err(&i2c->dev, "i2c_transfer() failed: %d\n", ret); |
Mark Brown | a89be93 | 2012-09-22 18:33:23 -0400 | [diff] [blame] | 180 | regulator_bulk_disable(WM2000_NUM_SUPPLIES, wm2000->supplies); |
Mark Brown | 3a66d38 | 2010-02-11 13:27:19 +0000 | [diff] [blame] | 181 | return ret; |
| 182 | } |
| 183 | if (ret != wm2000->anc_download_size) { |
| 184 | dev_err(&i2c->dev, "i2c_transfer() failed, %d != %d\n", |
| 185 | ret, wm2000->anc_download_size); |
Mark Brown | a89be93 | 2012-09-22 18:33:23 -0400 | [diff] [blame] | 186 | regulator_bulk_disable(WM2000_NUM_SUPPLIES, wm2000->supplies); |
Mark Brown | 3a66d38 | 2010-02-11 13:27:19 +0000 | [diff] [blame] | 187 | return -EIO; |
| 188 | } |
| 189 | |
| 190 | dev_dbg(&i2c->dev, "Download complete\n"); |
| 191 | |
| 192 | if (analogue) { |
Mark Brown | 3ff1ec2 | 2012-06-08 06:53:48 +0800 | [diff] [blame] | 193 | wm2000_write(i2c, WM2000_REG_ANA_VMID_PU_TIME, 248 / 4); |
Mark Brown | 3a66d38 | 2010-02-11 13:27:19 +0000 | [diff] [blame] | 194 | |
| 195 | wm2000_write(i2c, WM2000_REG_SYS_MODE_CNTRL, |
| 196 | WM2000_MODE_ANA_SEQ_INCLUDE | |
| 197 | WM2000_MODE_MOUSE_ENABLE | |
| 198 | WM2000_MODE_THERMAL_ENABLE); |
| 199 | } else { |
Mark Brown | 3a66d38 | 2010-02-11 13:27:19 +0000 | [diff] [blame] | 200 | wm2000_write(i2c, WM2000_REG_SYS_MODE_CNTRL, |
| 201 | WM2000_MODE_MOUSE_ENABLE | |
| 202 | WM2000_MODE_THERMAL_ENABLE); |
| 203 | } |
| 204 | |
Lucas Tanure | c5d0948 | 2018-10-19 17:44:22 +0100 | [diff] [blame] | 205 | ret = regmap_read(wm2000->regmap, WM2000_REG_SPEECH_CLARITY, &val); |
| 206 | if (ret != 0) { |
| 207 | dev_err(&i2c->dev, "Unable to read Speech Clarity: %d\n", ret); |
| 208 | regulator_bulk_disable(WM2000_NUM_SUPPLIES, wm2000->supplies); |
| 209 | return ret; |
| 210 | } |
Mark Brown | 3a66d38 | 2010-02-11 13:27:19 +0000 | [diff] [blame] | 211 | if (wm2000->speech_clarity) |
Lucas Tanure | c5d0948 | 2018-10-19 17:44:22 +0100 | [diff] [blame] | 212 | val |= WM2000_SPEECH_CLARITY; |
Mark Brown | 267f8fa | 2013-01-04 21:18:12 +0000 | [diff] [blame] | 213 | else |
Lucas Tanure | c5d0948 | 2018-10-19 17:44:22 +0100 | [diff] [blame] | 214 | val &= ~WM2000_SPEECH_CLARITY; |
| 215 | wm2000_write(i2c, WM2000_REG_SPEECH_CLARITY, val); |
Mark Brown | 3a66d38 | 2010-02-11 13:27:19 +0000 | [diff] [blame] | 216 | |
| 217 | wm2000_write(i2c, WM2000_REG_SYS_START0, 0x33); |
| 218 | wm2000_write(i2c, WM2000_REG_SYS_START1, 0x02); |
| 219 | |
| 220 | wm2000_write(i2c, WM2000_REG_SYS_CTL2, WM2000_ANC_INT_N_CLR); |
| 221 | |
| 222 | if (!wm2000_poll_bit(i2c, WM2000_REG_SYS_STATUS, |
Mark Brown | 3ff1ec2 | 2012-06-08 06:53:48 +0800 | [diff] [blame] | 223 | WM2000_STATUS_MOUSE_ACTIVE)) { |
| 224 | dev_err(&i2c->dev, "Timed out waiting for device\n"); |
Mark Brown | a89be93 | 2012-09-22 18:33:23 -0400 | [diff] [blame] | 225 | regulator_bulk_disable(WM2000_NUM_SUPPLIES, wm2000->supplies); |
Mark Brown | 3a66d38 | 2010-02-11 13:27:19 +0000 | [diff] [blame] | 226 | return -ETIMEDOUT; |
| 227 | } |
| 228 | |
| 229 | dev_dbg(&i2c->dev, "ANC active\n"); |
| 230 | if (analogue) |
| 231 | dev_dbg(&i2c->dev, "Analogue active\n"); |
| 232 | wm2000->anc_mode = ANC_ACTIVE; |
| 233 | |
| 234 | return 0; |
| 235 | } |
| 236 | |
| 237 | static int wm2000_power_down(struct i2c_client *i2c, int analogue) |
| 238 | { |
| 239 | struct wm2000_priv *wm2000 = dev_get_drvdata(&i2c->dev); |
Mark Brown | 3a66d38 | 2010-02-11 13:27:19 +0000 | [diff] [blame] | 240 | |
| 241 | if (analogue) { |
Mark Brown | 3ff1ec2 | 2012-06-08 06:53:48 +0800 | [diff] [blame] | 242 | wm2000_write(i2c, WM2000_REG_ANA_VMID_PD_TIME, 248 / 4); |
Mark Brown | 3a66d38 | 2010-02-11 13:27:19 +0000 | [diff] [blame] | 243 | wm2000_write(i2c, WM2000_REG_SYS_MODE_CNTRL, |
| 244 | WM2000_MODE_ANA_SEQ_INCLUDE | |
| 245 | WM2000_MODE_POWER_DOWN); |
| 246 | } else { |
Mark Brown | 3a66d38 | 2010-02-11 13:27:19 +0000 | [diff] [blame] | 247 | wm2000_write(i2c, WM2000_REG_SYS_MODE_CNTRL, |
| 248 | WM2000_MODE_POWER_DOWN); |
| 249 | } |
| 250 | |
| 251 | if (!wm2000_poll_bit(i2c, WM2000_REG_SYS_STATUS, |
Mark Brown | 3ff1ec2 | 2012-06-08 06:53:48 +0800 | [diff] [blame] | 252 | WM2000_STATUS_POWER_DOWN_COMPLETE)) { |
Mark Brown | 3a66d38 | 2010-02-11 13:27:19 +0000 | [diff] [blame] | 253 | dev_err(&i2c->dev, "Timeout waiting for ANC power down\n"); |
| 254 | return -ETIMEDOUT; |
| 255 | } |
| 256 | |
| 257 | if (!wm2000_poll_bit(i2c, WM2000_REG_ANC_STAT, |
Mark Brown | 3ff1ec2 | 2012-06-08 06:53:48 +0800 | [diff] [blame] | 258 | WM2000_ANC_ENG_IDLE)) { |
Mark Brown | 3a66d38 | 2010-02-11 13:27:19 +0000 | [diff] [blame] | 259 | dev_err(&i2c->dev, "Timeout waiting for ANC engine idle\n"); |
| 260 | return -ETIMEDOUT; |
| 261 | } |
| 262 | |
Mark Brown | a89be93 | 2012-09-22 18:33:23 -0400 | [diff] [blame] | 263 | regulator_bulk_disable(WM2000_NUM_SUPPLIES, wm2000->supplies); |
| 264 | |
Mark Brown | 3a66d38 | 2010-02-11 13:27:19 +0000 | [diff] [blame] | 265 | dev_dbg(&i2c->dev, "powered off\n"); |
| 266 | wm2000->anc_mode = ANC_OFF; |
| 267 | |
| 268 | return 0; |
| 269 | } |
| 270 | |
| 271 | static int wm2000_enter_bypass(struct i2c_client *i2c, int analogue) |
| 272 | { |
| 273 | struct wm2000_priv *wm2000 = dev_get_drvdata(&i2c->dev); |
| 274 | |
Takashi Iwai | bf90e89 | 2013-11-05 18:39:53 +0100 | [diff] [blame] | 275 | if (WARN_ON(wm2000->anc_mode != ANC_ACTIVE)) |
| 276 | return -EINVAL; |
Mark Brown | 3a66d38 | 2010-02-11 13:27:19 +0000 | [diff] [blame] | 277 | |
| 278 | if (analogue) { |
| 279 | wm2000_write(i2c, WM2000_REG_SYS_MODE_CNTRL, |
| 280 | WM2000_MODE_ANA_SEQ_INCLUDE | |
| 281 | WM2000_MODE_THERMAL_ENABLE | |
| 282 | WM2000_MODE_BYPASS_ENTRY); |
| 283 | } else { |
| 284 | wm2000_write(i2c, WM2000_REG_SYS_MODE_CNTRL, |
| 285 | WM2000_MODE_THERMAL_ENABLE | |
| 286 | WM2000_MODE_BYPASS_ENTRY); |
| 287 | } |
| 288 | |
| 289 | if (!wm2000_poll_bit(i2c, WM2000_REG_SYS_STATUS, |
Mark Brown | 3ff1ec2 | 2012-06-08 06:53:48 +0800 | [diff] [blame] | 290 | WM2000_STATUS_ANC_DISABLED)) { |
Mark Brown | 3a66d38 | 2010-02-11 13:27:19 +0000 | [diff] [blame] | 291 | dev_err(&i2c->dev, "Timeout waiting for ANC disable\n"); |
| 292 | return -ETIMEDOUT; |
| 293 | } |
| 294 | |
| 295 | if (!wm2000_poll_bit(i2c, WM2000_REG_ANC_STAT, |
Mark Brown | 3ff1ec2 | 2012-06-08 06:53:48 +0800 | [diff] [blame] | 296 | WM2000_ANC_ENG_IDLE)) { |
Mark Brown | 3a66d38 | 2010-02-11 13:27:19 +0000 | [diff] [blame] | 297 | dev_err(&i2c->dev, "Timeout waiting for ANC engine idle\n"); |
| 298 | return -ETIMEDOUT; |
| 299 | } |
| 300 | |
| 301 | wm2000_write(i2c, WM2000_REG_SYS_CTL1, WM2000_SYS_STBY); |
| 302 | wm2000_write(i2c, WM2000_REG_SYS_CTL2, WM2000_RAM_CLR); |
| 303 | |
| 304 | wm2000->anc_mode = ANC_BYPASS; |
| 305 | dev_dbg(&i2c->dev, "bypass enabled\n"); |
| 306 | |
| 307 | return 0; |
| 308 | } |
| 309 | |
| 310 | static int wm2000_exit_bypass(struct i2c_client *i2c, int analogue) |
| 311 | { |
| 312 | struct wm2000_priv *wm2000 = dev_get_drvdata(&i2c->dev); |
| 313 | |
Takashi Iwai | bf90e89 | 2013-11-05 18:39:53 +0100 | [diff] [blame] | 314 | if (WARN_ON(wm2000->anc_mode != ANC_BYPASS)) |
| 315 | return -EINVAL; |
Mark Brown | 3a66d38 | 2010-02-11 13:27:19 +0000 | [diff] [blame] | 316 | |
| 317 | wm2000_write(i2c, WM2000_REG_SYS_CTL1, 0); |
| 318 | |
| 319 | if (analogue) { |
| 320 | wm2000_write(i2c, WM2000_REG_SYS_MODE_CNTRL, |
| 321 | WM2000_MODE_ANA_SEQ_INCLUDE | |
| 322 | WM2000_MODE_MOUSE_ENABLE | |
| 323 | WM2000_MODE_THERMAL_ENABLE); |
| 324 | } else { |
| 325 | wm2000_write(i2c, WM2000_REG_SYS_MODE_CNTRL, |
| 326 | WM2000_MODE_MOUSE_ENABLE | |
| 327 | WM2000_MODE_THERMAL_ENABLE); |
| 328 | } |
| 329 | |
| 330 | wm2000_write(i2c, WM2000_REG_SYS_CTL2, WM2000_RAM_SET); |
| 331 | wm2000_write(i2c, WM2000_REG_SYS_CTL2, WM2000_ANC_INT_N_CLR); |
| 332 | |
| 333 | if (!wm2000_poll_bit(i2c, WM2000_REG_SYS_STATUS, |
Mark Brown | 3ff1ec2 | 2012-06-08 06:53:48 +0800 | [diff] [blame] | 334 | WM2000_STATUS_MOUSE_ACTIVE)) { |
Mark Brown | 3a66d38 | 2010-02-11 13:27:19 +0000 | [diff] [blame] | 335 | dev_err(&i2c->dev, "Timed out waiting for MOUSE\n"); |
| 336 | return -ETIMEDOUT; |
| 337 | } |
| 338 | |
| 339 | wm2000->anc_mode = ANC_ACTIVE; |
| 340 | dev_dbg(&i2c->dev, "MOUSE active\n"); |
| 341 | |
| 342 | return 0; |
| 343 | } |
| 344 | |
| 345 | static int wm2000_enter_standby(struct i2c_client *i2c, int analogue) |
| 346 | { |
| 347 | struct wm2000_priv *wm2000 = dev_get_drvdata(&i2c->dev); |
Mark Brown | 3a66d38 | 2010-02-11 13:27:19 +0000 | [diff] [blame] | 348 | |
Takashi Iwai | bf90e89 | 2013-11-05 18:39:53 +0100 | [diff] [blame] | 349 | if (WARN_ON(wm2000->anc_mode != ANC_ACTIVE)) |
| 350 | return -EINVAL; |
Mark Brown | 3a66d38 | 2010-02-11 13:27:19 +0000 | [diff] [blame] | 351 | |
| 352 | if (analogue) { |
Mark Brown | 3ff1ec2 | 2012-06-08 06:53:48 +0800 | [diff] [blame] | 353 | wm2000_write(i2c, WM2000_REG_ANA_VMID_PD_TIME, 248 / 4); |
Mark Brown | 3a66d38 | 2010-02-11 13:27:19 +0000 | [diff] [blame] | 354 | |
| 355 | wm2000_write(i2c, WM2000_REG_SYS_MODE_CNTRL, |
| 356 | WM2000_MODE_ANA_SEQ_INCLUDE | |
| 357 | WM2000_MODE_THERMAL_ENABLE | |
| 358 | WM2000_MODE_STANDBY_ENTRY); |
| 359 | } else { |
Mark Brown | 3a66d38 | 2010-02-11 13:27:19 +0000 | [diff] [blame] | 360 | wm2000_write(i2c, WM2000_REG_SYS_MODE_CNTRL, |
| 361 | WM2000_MODE_THERMAL_ENABLE | |
| 362 | WM2000_MODE_STANDBY_ENTRY); |
| 363 | } |
| 364 | |
| 365 | if (!wm2000_poll_bit(i2c, WM2000_REG_SYS_STATUS, |
Mark Brown | 3ff1ec2 | 2012-06-08 06:53:48 +0800 | [diff] [blame] | 366 | WM2000_STATUS_ANC_DISABLED)) { |
Mark Brown | 3a66d38 | 2010-02-11 13:27:19 +0000 | [diff] [blame] | 367 | dev_err(&i2c->dev, |
| 368 | "Timed out waiting for ANC disable after 1ms\n"); |
| 369 | return -ETIMEDOUT; |
| 370 | } |
| 371 | |
Mark Brown | 3ff1ec2 | 2012-06-08 06:53:48 +0800 | [diff] [blame] | 372 | if (!wm2000_poll_bit(i2c, WM2000_REG_ANC_STAT, WM2000_ANC_ENG_IDLE)) { |
Mark Brown | 3a66d38 | 2010-02-11 13:27:19 +0000 | [diff] [blame] | 373 | dev_err(&i2c->dev, |
Mark Brown | 3ff1ec2 | 2012-06-08 06:53:48 +0800 | [diff] [blame] | 374 | "Timed out waiting for standby\n"); |
Mark Brown | 3a66d38 | 2010-02-11 13:27:19 +0000 | [diff] [blame] | 375 | return -ETIMEDOUT; |
| 376 | } |
| 377 | |
| 378 | wm2000_write(i2c, WM2000_REG_SYS_CTL1, WM2000_SYS_STBY); |
| 379 | wm2000_write(i2c, WM2000_REG_SYS_CTL2, WM2000_RAM_CLR); |
| 380 | |
| 381 | wm2000->anc_mode = ANC_STANDBY; |
| 382 | dev_dbg(&i2c->dev, "standby\n"); |
| 383 | if (analogue) |
| 384 | dev_dbg(&i2c->dev, "Analogue disabled\n"); |
| 385 | |
| 386 | return 0; |
| 387 | } |
| 388 | |
| 389 | static int wm2000_exit_standby(struct i2c_client *i2c, int analogue) |
| 390 | { |
| 391 | struct wm2000_priv *wm2000 = dev_get_drvdata(&i2c->dev); |
Mark Brown | 3a66d38 | 2010-02-11 13:27:19 +0000 | [diff] [blame] | 392 | |
Takashi Iwai | bf90e89 | 2013-11-05 18:39:53 +0100 | [diff] [blame] | 393 | if (WARN_ON(wm2000->anc_mode != ANC_STANDBY)) |
| 394 | return -EINVAL; |
Mark Brown | 3a66d38 | 2010-02-11 13:27:19 +0000 | [diff] [blame] | 395 | |
| 396 | wm2000_write(i2c, WM2000_REG_SYS_CTL1, 0); |
| 397 | |
| 398 | if (analogue) { |
Mark Brown | 3ff1ec2 | 2012-06-08 06:53:48 +0800 | [diff] [blame] | 399 | wm2000_write(i2c, WM2000_REG_ANA_VMID_PU_TIME, 248 / 4); |
Mark Brown | 3a66d38 | 2010-02-11 13:27:19 +0000 | [diff] [blame] | 400 | |
| 401 | wm2000_write(i2c, WM2000_REG_SYS_MODE_CNTRL, |
| 402 | WM2000_MODE_ANA_SEQ_INCLUDE | |
| 403 | WM2000_MODE_THERMAL_ENABLE | |
| 404 | WM2000_MODE_MOUSE_ENABLE); |
| 405 | } else { |
Mark Brown | 3a66d38 | 2010-02-11 13:27:19 +0000 | [diff] [blame] | 406 | wm2000_write(i2c, WM2000_REG_SYS_MODE_CNTRL, |
| 407 | WM2000_MODE_THERMAL_ENABLE | |
| 408 | WM2000_MODE_MOUSE_ENABLE); |
| 409 | } |
| 410 | |
| 411 | wm2000_write(i2c, WM2000_REG_SYS_CTL2, WM2000_RAM_SET); |
| 412 | wm2000_write(i2c, WM2000_REG_SYS_CTL2, WM2000_ANC_INT_N_CLR); |
| 413 | |
| 414 | if (!wm2000_poll_bit(i2c, WM2000_REG_SYS_STATUS, |
Mark Brown | 3ff1ec2 | 2012-06-08 06:53:48 +0800 | [diff] [blame] | 415 | WM2000_STATUS_MOUSE_ACTIVE)) { |
| 416 | dev_err(&i2c->dev, "Timed out waiting for MOUSE\n"); |
Mark Brown | 3a66d38 | 2010-02-11 13:27:19 +0000 | [diff] [blame] | 417 | return -ETIMEDOUT; |
| 418 | } |
| 419 | |
| 420 | wm2000->anc_mode = ANC_ACTIVE; |
| 421 | dev_dbg(&i2c->dev, "MOUSE active\n"); |
| 422 | if (analogue) |
| 423 | dev_dbg(&i2c->dev, "Analogue enabled\n"); |
| 424 | |
| 425 | return 0; |
| 426 | } |
| 427 | |
| 428 | typedef int (*wm2000_mode_fn)(struct i2c_client *i2c, int analogue); |
| 429 | |
| 430 | static struct { |
| 431 | enum wm2000_anc_mode source; |
| 432 | enum wm2000_anc_mode dest; |
| 433 | int analogue; |
| 434 | wm2000_mode_fn step[2]; |
| 435 | } anc_transitions[] = { |
| 436 | { |
| 437 | .source = ANC_OFF, |
| 438 | .dest = ANC_ACTIVE, |
| 439 | .analogue = 1, |
| 440 | .step = { |
| 441 | wm2000_power_up, |
| 442 | }, |
| 443 | }, |
| 444 | { |
| 445 | .source = ANC_OFF, |
| 446 | .dest = ANC_STANDBY, |
| 447 | .step = { |
| 448 | wm2000_power_up, |
| 449 | wm2000_enter_standby, |
| 450 | }, |
| 451 | }, |
| 452 | { |
| 453 | .source = ANC_OFF, |
| 454 | .dest = ANC_BYPASS, |
| 455 | .analogue = 1, |
| 456 | .step = { |
| 457 | wm2000_power_up, |
| 458 | wm2000_enter_bypass, |
| 459 | }, |
| 460 | }, |
| 461 | { |
| 462 | .source = ANC_ACTIVE, |
| 463 | .dest = ANC_BYPASS, |
| 464 | .analogue = 1, |
| 465 | .step = { |
| 466 | wm2000_enter_bypass, |
| 467 | }, |
| 468 | }, |
| 469 | { |
| 470 | .source = ANC_ACTIVE, |
| 471 | .dest = ANC_STANDBY, |
| 472 | .analogue = 1, |
| 473 | .step = { |
| 474 | wm2000_enter_standby, |
| 475 | }, |
| 476 | }, |
| 477 | { |
| 478 | .source = ANC_ACTIVE, |
| 479 | .dest = ANC_OFF, |
| 480 | .analogue = 1, |
| 481 | .step = { |
| 482 | wm2000_power_down, |
| 483 | }, |
| 484 | }, |
| 485 | { |
| 486 | .source = ANC_BYPASS, |
| 487 | .dest = ANC_ACTIVE, |
| 488 | .analogue = 1, |
| 489 | .step = { |
| 490 | wm2000_exit_bypass, |
| 491 | }, |
| 492 | }, |
| 493 | { |
| 494 | .source = ANC_BYPASS, |
| 495 | .dest = ANC_STANDBY, |
| 496 | .analogue = 1, |
| 497 | .step = { |
| 498 | wm2000_exit_bypass, |
| 499 | wm2000_enter_standby, |
| 500 | }, |
| 501 | }, |
| 502 | { |
| 503 | .source = ANC_BYPASS, |
| 504 | .dest = ANC_OFF, |
| 505 | .step = { |
| 506 | wm2000_exit_bypass, |
| 507 | wm2000_power_down, |
| 508 | }, |
| 509 | }, |
| 510 | { |
| 511 | .source = ANC_STANDBY, |
| 512 | .dest = ANC_ACTIVE, |
| 513 | .analogue = 1, |
| 514 | .step = { |
| 515 | wm2000_exit_standby, |
| 516 | }, |
| 517 | }, |
| 518 | { |
| 519 | .source = ANC_STANDBY, |
| 520 | .dest = ANC_BYPASS, |
| 521 | .analogue = 1, |
| 522 | .step = { |
| 523 | wm2000_exit_standby, |
| 524 | wm2000_enter_bypass, |
| 525 | }, |
| 526 | }, |
| 527 | { |
| 528 | .source = ANC_STANDBY, |
| 529 | .dest = ANC_OFF, |
| 530 | .step = { |
| 531 | wm2000_exit_standby, |
| 532 | wm2000_power_down, |
| 533 | }, |
| 534 | }, |
| 535 | }; |
| 536 | |
| 537 | static int wm2000_anc_transition(struct wm2000_priv *wm2000, |
| 538 | enum wm2000_anc_mode mode) |
| 539 | { |
| 540 | struct i2c_client *i2c = wm2000->i2c; |
| 541 | int i, j; |
| 542 | int ret; |
| 543 | |
| 544 | if (wm2000->anc_mode == mode) |
| 545 | return 0; |
| 546 | |
| 547 | for (i = 0; i < ARRAY_SIZE(anc_transitions); i++) |
| 548 | if (anc_transitions[i].source == wm2000->anc_mode && |
| 549 | anc_transitions[i].dest == mode) |
| 550 | break; |
| 551 | if (i == ARRAY_SIZE(anc_transitions)) { |
| 552 | dev_err(&i2c->dev, "No transition for %d->%d\n", |
| 553 | wm2000->anc_mode, mode); |
| 554 | return -EINVAL; |
| 555 | } |
| 556 | |
Mark Brown | 514cfd6 | 2012-12-13 17:29:00 +0900 | [diff] [blame] | 557 | /* Maintain clock while active */ |
| 558 | if (anc_transitions[i].source == ANC_OFF) { |
| 559 | ret = clk_prepare_enable(wm2000->mclk); |
| 560 | if (ret != 0) { |
| 561 | dev_err(&i2c->dev, "Failed to enable MCLK: %d\n", ret); |
| 562 | return ret; |
| 563 | } |
| 564 | } |
| 565 | |
Mark Brown | 3a66d38 | 2010-02-11 13:27:19 +0000 | [diff] [blame] | 566 | for (j = 0; j < ARRAY_SIZE(anc_transitions[j].step); j++) { |
| 567 | if (!anc_transitions[i].step[j]) |
| 568 | break; |
| 569 | ret = anc_transitions[i].step[j](i2c, |
| 570 | anc_transitions[i].analogue); |
| 571 | if (ret != 0) |
| 572 | return ret; |
| 573 | } |
| 574 | |
Mark Brown | 514cfd6 | 2012-12-13 17:29:00 +0900 | [diff] [blame] | 575 | if (anc_transitions[i].dest == ANC_OFF) |
| 576 | clk_disable_unprepare(wm2000->mclk); |
| 577 | |
Charles Keepax | fa54aad | 2016-08-11 14:40:04 +0100 | [diff] [blame] | 578 | return 0; |
Mark Brown | 3a66d38 | 2010-02-11 13:27:19 +0000 | [diff] [blame] | 579 | } |
| 580 | |
| 581 | static int wm2000_anc_set_mode(struct wm2000_priv *wm2000) |
| 582 | { |
| 583 | struct i2c_client *i2c = wm2000->i2c; |
| 584 | enum wm2000_anc_mode mode; |
| 585 | |
| 586 | if (wm2000->anc_eng_ena && wm2000->spk_ena) |
| 587 | if (wm2000->anc_active) |
| 588 | mode = ANC_ACTIVE; |
| 589 | else |
| 590 | mode = ANC_BYPASS; |
| 591 | else |
| 592 | mode = ANC_STANDBY; |
| 593 | |
| 594 | dev_dbg(&i2c->dev, "Set mode %d (enabled %d, mute %d, active %d)\n", |
| 595 | mode, wm2000->anc_eng_ena, !wm2000->spk_ena, |
| 596 | wm2000->anc_active); |
| 597 | |
| 598 | return wm2000_anc_transition(wm2000, mode); |
| 599 | } |
| 600 | |
| 601 | static int wm2000_anc_mode_get(struct snd_kcontrol *kcontrol, |
| 602 | struct snd_ctl_elem_value *ucontrol) |
| 603 | { |
Kuninori Morimoto | e5449af | 2018-01-29 02:59:05 +0000 | [diff] [blame] | 604 | struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol); |
| 605 | struct wm2000_priv *wm2000 = dev_get_drvdata(component->dev); |
Mark Brown | 3a66d38 | 2010-02-11 13:27:19 +0000 | [diff] [blame] | 606 | |
Takashi Iwai | 00a14c2 | 2015-03-10 12:39:09 +0100 | [diff] [blame] | 607 | ucontrol->value.integer.value[0] = wm2000->anc_active; |
Mark Brown | 3a66d38 | 2010-02-11 13:27:19 +0000 | [diff] [blame] | 608 | |
| 609 | return 0; |
| 610 | } |
| 611 | |
| 612 | static int wm2000_anc_mode_put(struct snd_kcontrol *kcontrol, |
| 613 | struct snd_ctl_elem_value *ucontrol) |
| 614 | { |
Kuninori Morimoto | e5449af | 2018-01-29 02:59:05 +0000 | [diff] [blame] | 615 | struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol); |
| 616 | struct wm2000_priv *wm2000 = dev_get_drvdata(component->dev); |
Dan Carpenter | 8444f59 | 2015-10-13 10:12:45 +0300 | [diff] [blame] | 617 | unsigned int anc_active = ucontrol->value.integer.value[0]; |
Mark Brown | 8e9bb42 | 2013-01-23 18:38:54 +0800 | [diff] [blame] | 618 | int ret; |
Mark Brown | 3a66d38 | 2010-02-11 13:27:19 +0000 | [diff] [blame] | 619 | |
| 620 | if (anc_active > 1) |
| 621 | return -EINVAL; |
| 622 | |
Mark Brown | 8e9bb42 | 2013-01-23 18:38:54 +0800 | [diff] [blame] | 623 | mutex_lock(&wm2000->lock); |
| 624 | |
Mark Brown | 3a66d38 | 2010-02-11 13:27:19 +0000 | [diff] [blame] | 625 | wm2000->anc_active = anc_active; |
| 626 | |
Mark Brown | 8e9bb42 | 2013-01-23 18:38:54 +0800 | [diff] [blame] | 627 | ret = wm2000_anc_set_mode(wm2000); |
| 628 | |
| 629 | mutex_unlock(&wm2000->lock); |
| 630 | |
| 631 | return ret; |
Mark Brown | 3a66d38 | 2010-02-11 13:27:19 +0000 | [diff] [blame] | 632 | } |
| 633 | |
| 634 | static int wm2000_speaker_get(struct snd_kcontrol *kcontrol, |
| 635 | struct snd_ctl_elem_value *ucontrol) |
| 636 | { |
Kuninori Morimoto | e5449af | 2018-01-29 02:59:05 +0000 | [diff] [blame] | 637 | struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol); |
| 638 | struct wm2000_priv *wm2000 = dev_get_drvdata(component->dev); |
Mark Brown | 3a66d38 | 2010-02-11 13:27:19 +0000 | [diff] [blame] | 639 | |
Takashi Iwai | 00a14c2 | 2015-03-10 12:39:09 +0100 | [diff] [blame] | 640 | ucontrol->value.integer.value[0] = wm2000->spk_ena; |
Mark Brown | 3a66d38 | 2010-02-11 13:27:19 +0000 | [diff] [blame] | 641 | |
| 642 | return 0; |
| 643 | } |
| 644 | |
| 645 | static int wm2000_speaker_put(struct snd_kcontrol *kcontrol, |
| 646 | struct snd_ctl_elem_value *ucontrol) |
| 647 | { |
Kuninori Morimoto | e5449af | 2018-01-29 02:59:05 +0000 | [diff] [blame] | 648 | struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol); |
| 649 | struct wm2000_priv *wm2000 = dev_get_drvdata(component->dev); |
Dan Carpenter | 8444f59 | 2015-10-13 10:12:45 +0300 | [diff] [blame] | 650 | unsigned int val = ucontrol->value.integer.value[0]; |
Mark Brown | 8e9bb42 | 2013-01-23 18:38:54 +0800 | [diff] [blame] | 651 | int ret; |
Mark Brown | 3a66d38 | 2010-02-11 13:27:19 +0000 | [diff] [blame] | 652 | |
| 653 | if (val > 1) |
| 654 | return -EINVAL; |
| 655 | |
Mark Brown | 8e9bb42 | 2013-01-23 18:38:54 +0800 | [diff] [blame] | 656 | mutex_lock(&wm2000->lock); |
| 657 | |
Mark Brown | 3a66d38 | 2010-02-11 13:27:19 +0000 | [diff] [blame] | 658 | wm2000->spk_ena = val; |
| 659 | |
Mark Brown | 8e9bb42 | 2013-01-23 18:38:54 +0800 | [diff] [blame] | 660 | ret = wm2000_anc_set_mode(wm2000); |
| 661 | |
| 662 | mutex_unlock(&wm2000->lock); |
| 663 | |
| 664 | return ret; |
Mark Brown | 3a66d38 | 2010-02-11 13:27:19 +0000 | [diff] [blame] | 665 | } |
| 666 | |
| 667 | static const struct snd_kcontrol_new wm2000_controls[] = { |
Mark Brown | 3f3af6e | 2013-01-30 21:35:44 +0800 | [diff] [blame] | 668 | SOC_SINGLE("ANC Volume", WM2000_REG_ANC_GAIN_CTRL, 0, 255, 0), |
Mark Brown | 3a66d38 | 2010-02-11 13:27:19 +0000 | [diff] [blame] | 669 | SOC_SINGLE_BOOL_EXT("WM2000 ANC Switch", 0, |
| 670 | wm2000_anc_mode_get, |
| 671 | wm2000_anc_mode_put), |
| 672 | SOC_SINGLE_BOOL_EXT("WM2000 Switch", 0, |
| 673 | wm2000_speaker_get, |
| 674 | wm2000_speaker_put), |
| 675 | }; |
| 676 | |
| 677 | static int wm2000_anc_power_event(struct snd_soc_dapm_widget *w, |
| 678 | struct snd_kcontrol *kcontrol, int event) |
| 679 | { |
Kuninori Morimoto | e5449af | 2018-01-29 02:59:05 +0000 | [diff] [blame] | 680 | struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm); |
| 681 | struct wm2000_priv *wm2000 = dev_get_drvdata(component->dev); |
Mark Brown | 8e9bb42 | 2013-01-23 18:38:54 +0800 | [diff] [blame] | 682 | int ret; |
| 683 | |
| 684 | mutex_lock(&wm2000->lock); |
Mark Brown | 3a66d38 | 2010-02-11 13:27:19 +0000 | [diff] [blame] | 685 | |
| 686 | if (SND_SOC_DAPM_EVENT_ON(event)) |
| 687 | wm2000->anc_eng_ena = 1; |
| 688 | |
| 689 | if (SND_SOC_DAPM_EVENT_OFF(event)) |
| 690 | wm2000->anc_eng_ena = 0; |
| 691 | |
Mark Brown | 8e9bb42 | 2013-01-23 18:38:54 +0800 | [diff] [blame] | 692 | ret = wm2000_anc_set_mode(wm2000); |
| 693 | |
| 694 | mutex_unlock(&wm2000->lock); |
| 695 | |
| 696 | return ret; |
Mark Brown | 3a66d38 | 2010-02-11 13:27:19 +0000 | [diff] [blame] | 697 | } |
| 698 | |
| 699 | static const struct snd_soc_dapm_widget wm2000_dapm_widgets[] = { |
| 700 | /* Externally visible pins */ |
Mark Brown | 4911ccd | 2011-12-02 21:59:18 +0000 | [diff] [blame] | 701 | SND_SOC_DAPM_OUTPUT("SPKN"), |
| 702 | SND_SOC_DAPM_OUTPUT("SPKP"), |
Mark Brown | 3a66d38 | 2010-02-11 13:27:19 +0000 | [diff] [blame] | 703 | |
Mark Brown | 4911ccd | 2011-12-02 21:59:18 +0000 | [diff] [blame] | 704 | SND_SOC_DAPM_INPUT("LINN"), |
| 705 | SND_SOC_DAPM_INPUT("LINP"), |
Mark Brown | 3a66d38 | 2010-02-11 13:27:19 +0000 | [diff] [blame] | 706 | |
| 707 | SND_SOC_DAPM_PGA_E("ANC Engine", SND_SOC_NOPM, 0, 0, NULL, 0, |
| 708 | wm2000_anc_power_event, |
| 709 | SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD), |
| 710 | }; |
| 711 | |
| 712 | /* Target, Path, Source */ |
Mark Brown | 4911ccd | 2011-12-02 21:59:18 +0000 | [diff] [blame] | 713 | static const struct snd_soc_dapm_route wm2000_audio_map[] = { |
| 714 | { "SPKN", NULL, "ANC Engine" }, |
| 715 | { "SPKP", NULL, "ANC Engine" }, |
| 716 | { "ANC Engine", NULL, "LINN" }, |
| 717 | { "ANC Engine", NULL, "LINP" }, |
Mark Brown | 3a66d38 | 2010-02-11 13:27:19 +0000 | [diff] [blame] | 718 | }; |
| 719 | |
Mark Brown | 4911ccd | 2011-12-02 21:59:18 +0000 | [diff] [blame] | 720 | #ifdef CONFIG_PM |
Kuninori Morimoto | e5449af | 2018-01-29 02:59:05 +0000 | [diff] [blame] | 721 | static int wm2000_suspend(struct snd_soc_component *component) |
Mark Brown | 3a66d38 | 2010-02-11 13:27:19 +0000 | [diff] [blame] | 722 | { |
Kuninori Morimoto | e5449af | 2018-01-29 02:59:05 +0000 | [diff] [blame] | 723 | struct wm2000_priv *wm2000 = dev_get_drvdata(component->dev); |
Mark Brown | 3a66d38 | 2010-02-11 13:27:19 +0000 | [diff] [blame] | 724 | |
Mark Brown | 4911ccd | 2011-12-02 21:59:18 +0000 | [diff] [blame] | 725 | return wm2000_anc_transition(wm2000, ANC_OFF); |
Mark Brown | 3a66d38 | 2010-02-11 13:27:19 +0000 | [diff] [blame] | 726 | } |
Mark Brown | 4911ccd | 2011-12-02 21:59:18 +0000 | [diff] [blame] | 727 | |
Kuninori Morimoto | e5449af | 2018-01-29 02:59:05 +0000 | [diff] [blame] | 728 | static int wm2000_resume(struct snd_soc_component *component) |
Mark Brown | 4911ccd | 2011-12-02 21:59:18 +0000 | [diff] [blame] | 729 | { |
Kuninori Morimoto | e5449af | 2018-01-29 02:59:05 +0000 | [diff] [blame] | 730 | struct wm2000_priv *wm2000 = dev_get_drvdata(component->dev); |
Mark Brown | 4911ccd | 2011-12-02 21:59:18 +0000 | [diff] [blame] | 731 | |
| 732 | return wm2000_anc_set_mode(wm2000); |
| 733 | } |
| 734 | #else |
| 735 | #define wm2000_suspend NULL |
| 736 | #define wm2000_resume NULL |
| 737 | #endif |
Mark Brown | 3a66d38 | 2010-02-11 13:27:19 +0000 | [diff] [blame] | 738 | |
Mark Brown | 51cc7ed | 2012-05-31 14:48:07 +0100 | [diff] [blame] | 739 | static bool wm2000_readable_reg(struct device *dev, unsigned int reg) |
| 740 | { |
| 741 | switch (reg) { |
| 742 | case WM2000_REG_SYS_START: |
Mark Brown | 33e7546 | 2013-01-22 15:51:08 +0900 | [diff] [blame] | 743 | case WM2000_REG_ANC_GAIN_CTRL: |
| 744 | case WM2000_REG_MSE_TH1: |
| 745 | case WM2000_REG_MSE_TH2: |
Mark Brown | 51cc7ed | 2012-05-31 14:48:07 +0100 | [diff] [blame] | 746 | case WM2000_REG_SPEECH_CLARITY: |
| 747 | case WM2000_REG_SYS_WATCHDOG: |
| 748 | case WM2000_REG_ANA_VMID_PD_TIME: |
| 749 | case WM2000_REG_ANA_VMID_PU_TIME: |
| 750 | case WM2000_REG_CAT_FLTR_INDX: |
| 751 | case WM2000_REG_CAT_GAIN_0: |
| 752 | case WM2000_REG_SYS_STATUS: |
| 753 | case WM2000_REG_SYS_MODE_CNTRL: |
| 754 | case WM2000_REG_SYS_START0: |
| 755 | case WM2000_REG_SYS_START1: |
| 756 | case WM2000_REG_ID1: |
| 757 | case WM2000_REG_ID2: |
| 758 | case WM2000_REG_REVISON: |
| 759 | case WM2000_REG_SYS_CTL1: |
| 760 | case WM2000_REG_SYS_CTL2: |
| 761 | case WM2000_REG_ANC_STAT: |
| 762 | case WM2000_REG_IF_CTL: |
Mark Brown | 939dc51 | 2013-03-29 13:03:39 +0800 | [diff] [blame] | 763 | case WM2000_REG_ANA_MIC_CTL: |
| 764 | case WM2000_REG_SPK_CTL: |
Mark Brown | 51cc7ed | 2012-05-31 14:48:07 +0100 | [diff] [blame] | 765 | return true; |
| 766 | default: |
| 767 | return false; |
| 768 | } |
| 769 | } |
| 770 | |
Mark Brown | 8aa1fe8 | 2011-12-02 21:57:19 +0000 | [diff] [blame] | 771 | static const struct regmap_config wm2000_regmap = { |
Mark Brown | d0e12f3 | 2012-09-26 11:57:30 +0100 | [diff] [blame] | 772 | .reg_bits = 16, |
Mark Brown | 8aa1fe8 | 2011-12-02 21:57:19 +0000 | [diff] [blame] | 773 | .val_bits = 8, |
Mark Brown | 51cc7ed | 2012-05-31 14:48:07 +0100 | [diff] [blame] | 774 | |
Mark Brown | 939dc51 | 2013-03-29 13:03:39 +0800 | [diff] [blame] | 775 | .max_register = WM2000_REG_SPK_CTL, |
Mark Brown | 51cc7ed | 2012-05-31 14:48:07 +0100 | [diff] [blame] | 776 | .readable_reg = wm2000_readable_reg, |
Mark Brown | 8aa1fe8 | 2011-12-02 21:57:19 +0000 | [diff] [blame] | 777 | }; |
| 778 | |
Kuninori Morimoto | e5449af | 2018-01-29 02:59:05 +0000 | [diff] [blame] | 779 | static int wm2000_probe(struct snd_soc_component *component) |
Mark Brown | 4911ccd | 2011-12-02 21:59:18 +0000 | [diff] [blame] | 780 | { |
Kuninori Morimoto | e5449af | 2018-01-29 02:59:05 +0000 | [diff] [blame] | 781 | struct wm2000_priv *wm2000 = dev_get_drvdata(component->dev); |
Mark Brown | 4911ccd | 2011-12-02 21:59:18 +0000 | [diff] [blame] | 782 | |
| 783 | /* This will trigger a transition to standby mode by default */ |
| 784 | wm2000_anc_set_mode(wm2000); |
| 785 | |
| 786 | return 0; |
| 787 | } |
| 788 | |
Kuninori Morimoto | e5449af | 2018-01-29 02:59:05 +0000 | [diff] [blame] | 789 | static void wm2000_remove(struct snd_soc_component *component) |
Mark Brown | 4911ccd | 2011-12-02 21:59:18 +0000 | [diff] [blame] | 790 | { |
Kuninori Morimoto | e5449af | 2018-01-29 02:59:05 +0000 | [diff] [blame] | 791 | struct wm2000_priv *wm2000 = dev_get_drvdata(component->dev); |
Mark Brown | 4911ccd | 2011-12-02 21:59:18 +0000 | [diff] [blame] | 792 | |
Kuninori Morimoto | e5449af | 2018-01-29 02:59:05 +0000 | [diff] [blame] | 793 | wm2000_anc_transition(wm2000, ANC_OFF); |
Mark Brown | 4911ccd | 2011-12-02 21:59:18 +0000 | [diff] [blame] | 794 | } |
| 795 | |
Kuninori Morimoto | e5449af | 2018-01-29 02:59:05 +0000 | [diff] [blame] | 796 | static const struct snd_soc_component_driver soc_component_dev_wm2000 = { |
| 797 | .probe = wm2000_probe, |
| 798 | .remove = wm2000_remove, |
| 799 | .suspend = wm2000_suspend, |
| 800 | .resume = wm2000_resume, |
| 801 | .controls = wm2000_controls, |
| 802 | .num_controls = ARRAY_SIZE(wm2000_controls), |
| 803 | .dapm_widgets = wm2000_dapm_widgets, |
| 804 | .num_dapm_widgets = ARRAY_SIZE(wm2000_dapm_widgets), |
| 805 | .dapm_routes = wm2000_audio_map, |
| 806 | .num_dapm_routes = ARRAY_SIZE(wm2000_audio_map), |
| 807 | .idle_bias_on = 1, |
| 808 | .use_pmdown_time = 1, |
| 809 | .endianness = 1, |
| 810 | .non_legacy_dai_naming = 1, |
Mark Brown | 4911ccd | 2011-12-02 21:59:18 +0000 | [diff] [blame] | 811 | }; |
| 812 | |
Bill Pemberton | 7a79e94 | 2012-12-07 09:26:37 -0500 | [diff] [blame] | 813 | static int wm2000_i2c_probe(struct i2c_client *i2c, |
| 814 | const struct i2c_device_id *i2c_id) |
Mark Brown | 3a66d38 | 2010-02-11 13:27:19 +0000 | [diff] [blame] | 815 | { |
| 816 | struct wm2000_priv *wm2000; |
| 817 | struct wm2000_platform_data *pdata; |
| 818 | const char *filename; |
Jesper Juhl | c83f1d7 | 2012-01-23 22:28:44 +0100 | [diff] [blame] | 819 | const struct firmware *fw = NULL; |
Mark Brown | a89be93 | 2012-09-22 18:33:23 -0400 | [diff] [blame] | 820 | int ret, i; |
Lucas Tanure | c5d0948 | 2018-10-19 17:44:22 +0100 | [diff] [blame] | 821 | unsigned int reg; |
Mark Brown | 3a66d38 | 2010-02-11 13:27:19 +0000 | [diff] [blame] | 822 | u16 id; |
| 823 | |
Markus Elfring | bf0842b | 2017-11-24 08:18:14 +0100 | [diff] [blame] | 824 | wm2000 = devm_kzalloc(&i2c->dev, sizeof(*wm2000), GFP_KERNEL); |
Sachin Kamat | a0f6211 | 2014-06-20 15:29:07 +0530 | [diff] [blame] | 825 | if (!wm2000) |
Mark Brown | 3a66d38 | 2010-02-11 13:27:19 +0000 | [diff] [blame] | 826 | return -ENOMEM; |
Mark Brown | 3a66d38 | 2010-02-11 13:27:19 +0000 | [diff] [blame] | 827 | |
Mark Brown | 8e9bb42 | 2013-01-23 18:38:54 +0800 | [diff] [blame] | 828 | mutex_init(&wm2000->lock); |
| 829 | |
Mark Brown | 8aa1fe8 | 2011-12-02 21:57:19 +0000 | [diff] [blame] | 830 | dev_set_drvdata(&i2c->dev, wm2000); |
| 831 | |
Mark Brown | 8fed54a | 2012-09-22 18:32:08 -0400 | [diff] [blame] | 832 | wm2000->regmap = devm_regmap_init_i2c(i2c, &wm2000_regmap); |
Mark Brown | 8aa1fe8 | 2011-12-02 21:57:19 +0000 | [diff] [blame] | 833 | if (IS_ERR(wm2000->regmap)) { |
| 834 | ret = PTR_ERR(wm2000->regmap); |
| 835 | dev_err(&i2c->dev, "Failed to allocate register map: %d\n", |
| 836 | ret); |
Jesper Juhl | c83f1d7 | 2012-01-23 22:28:44 +0100 | [diff] [blame] | 837 | goto out; |
Mark Brown | 8aa1fe8 | 2011-12-02 21:57:19 +0000 | [diff] [blame] | 838 | } |
| 839 | |
Mark Brown | a89be93 | 2012-09-22 18:33:23 -0400 | [diff] [blame] | 840 | for (i = 0; i < WM2000_NUM_SUPPLIES; i++) |
| 841 | wm2000->supplies[i].supply = wm2000_supplies[i]; |
| 842 | |
| 843 | ret = devm_regulator_bulk_get(&i2c->dev, WM2000_NUM_SUPPLIES, |
| 844 | wm2000->supplies); |
| 845 | if (ret != 0) { |
| 846 | dev_err(&i2c->dev, "Failed to get supplies: %d\n", ret); |
| 847 | return ret; |
| 848 | } |
| 849 | |
| 850 | ret = regulator_bulk_enable(WM2000_NUM_SUPPLIES, wm2000->supplies); |
| 851 | if (ret != 0) { |
| 852 | dev_err(&i2c->dev, "Failed to enable supplies: %d\n", ret); |
| 853 | return ret; |
| 854 | } |
| 855 | |
Mark Brown | 3a66d38 | 2010-02-11 13:27:19 +0000 | [diff] [blame] | 856 | /* Verify that this is a WM2000 */ |
Lucas Tanure | c5d0948 | 2018-10-19 17:44:22 +0100 | [diff] [blame] | 857 | ret = regmap_read(wm2000->regmap, WM2000_REG_ID1, ®); |
| 858 | if (ret != 0) { |
| 859 | dev_err(&i2c->dev, "Unable to read ID1: %d\n", ret); |
| 860 | return ret; |
| 861 | } |
Mark Brown | 3a66d38 | 2010-02-11 13:27:19 +0000 | [diff] [blame] | 862 | id = reg << 8; |
Lucas Tanure | c5d0948 | 2018-10-19 17:44:22 +0100 | [diff] [blame] | 863 | ret = regmap_read(wm2000->regmap, WM2000_REG_ID2, ®); |
| 864 | if (ret != 0) { |
| 865 | dev_err(&i2c->dev, "Unable to read ID2: %d\n", ret); |
| 866 | return ret; |
| 867 | } |
Mark Brown | 3a66d38 | 2010-02-11 13:27:19 +0000 | [diff] [blame] | 868 | id |= reg & 0xff; |
| 869 | |
| 870 | if (id != 0x2000) { |
| 871 | dev_err(&i2c->dev, "Device is not a WM2000 - ID %x\n", id); |
| 872 | ret = -ENODEV; |
Mark Brown | a89be93 | 2012-09-22 18:33:23 -0400 | [diff] [blame] | 873 | goto err_supplies; |
Mark Brown | 3a66d38 | 2010-02-11 13:27:19 +0000 | [diff] [blame] | 874 | } |
| 875 | |
Lucas Tanure | c5d0948 | 2018-10-19 17:44:22 +0100 | [diff] [blame] | 876 | ret = regmap_read(wm2000->regmap, WM2000_REG_REVISON, ®); |
| 877 | if (ret != 0) { |
| 878 | dev_err(&i2c->dev, "Unable to read Revision: %d\n", ret); |
| 879 | return ret; |
| 880 | } |
Mark Brown | 3a66d38 | 2010-02-11 13:27:19 +0000 | [diff] [blame] | 881 | dev_info(&i2c->dev, "revision %c\n", reg + 'A'); |
| 882 | |
Mark Brown | 514cfd6 | 2012-12-13 17:29:00 +0900 | [diff] [blame] | 883 | wm2000->mclk = devm_clk_get(&i2c->dev, "MCLK"); |
| 884 | if (IS_ERR(wm2000->mclk)) { |
| 885 | ret = PTR_ERR(wm2000->mclk); |
| 886 | dev_err(&i2c->dev, "Failed to get MCLK: %d\n", ret); |
| 887 | goto err_supplies; |
| 888 | } |
| 889 | |
Mark Brown | 3a66d38 | 2010-02-11 13:27:19 +0000 | [diff] [blame] | 890 | filename = "wm2000_anc.bin"; |
| 891 | pdata = dev_get_platdata(&i2c->dev); |
| 892 | if (pdata) { |
Mark Brown | 3a66d38 | 2010-02-11 13:27:19 +0000 | [diff] [blame] | 893 | wm2000->speech_clarity = !pdata->speech_enh_disable; |
| 894 | |
| 895 | if (pdata->download_file) |
| 896 | filename = pdata->download_file; |
| 897 | } |
| 898 | |
| 899 | ret = request_firmware(&fw, filename, &i2c->dev); |
| 900 | if (ret != 0) { |
| 901 | dev_err(&i2c->dev, "Failed to acquire ANC data: %d\n", ret); |
Mark Brown | a89be93 | 2012-09-22 18:33:23 -0400 | [diff] [blame] | 902 | goto err_supplies; |
Mark Brown | 3a66d38 | 2010-02-11 13:27:19 +0000 | [diff] [blame] | 903 | } |
| 904 | |
| 905 | /* Pre-cook the concatenation of the register address onto the image */ |
| 906 | wm2000->anc_download_size = fw->size + 2; |
Mark Brown | b03e96e | 2011-12-02 21:28:31 +0000 | [diff] [blame] | 907 | wm2000->anc_download = devm_kzalloc(&i2c->dev, |
| 908 | wm2000->anc_download_size, |
| 909 | GFP_KERNEL); |
Mark Brown | 3a66d38 | 2010-02-11 13:27:19 +0000 | [diff] [blame] | 910 | if (wm2000->anc_download == NULL) { |
Mark Brown | 3a66d38 | 2010-02-11 13:27:19 +0000 | [diff] [blame] | 911 | ret = -ENOMEM; |
Mark Brown | a89be93 | 2012-09-22 18:33:23 -0400 | [diff] [blame] | 912 | goto err_supplies; |
Mark Brown | 3a66d38 | 2010-02-11 13:27:19 +0000 | [diff] [blame] | 913 | } |
| 914 | |
| 915 | wm2000->anc_download[0] = 0x80; |
| 916 | wm2000->anc_download[1] = 0x00; |
| 917 | memcpy(wm2000->anc_download + 2, fw->data, fw->size); |
| 918 | |
Mark Brown | 3a66d38 | 2010-02-11 13:27:19 +0000 | [diff] [blame] | 919 | wm2000->anc_eng_ena = 1; |
Mark Brown | e71fa37 | 2010-06-15 15:14:00 +0100 | [diff] [blame] | 920 | wm2000->anc_active = 1; |
| 921 | wm2000->spk_ena = 1; |
Mark Brown | 3a66d38 | 2010-02-11 13:27:19 +0000 | [diff] [blame] | 922 | wm2000->i2c = i2c; |
| 923 | |
| 924 | wm2000_reset(wm2000); |
| 925 | |
Kuninori Morimoto | e5449af | 2018-01-29 02:59:05 +0000 | [diff] [blame] | 926 | ret = devm_snd_soc_register_component(&i2c->dev, |
| 927 | &soc_component_dev_wm2000, NULL, 0); |
Mark Brown | 3a66d38 | 2010-02-11 13:27:19 +0000 | [diff] [blame] | 928 | |
Mark Brown | a89be93 | 2012-09-22 18:33:23 -0400 | [diff] [blame] | 929 | err_supplies: |
| 930 | regulator_bulk_disable(WM2000_NUM_SUPPLIES, wm2000->supplies); |
Mark Brown | 3a66d38 | 2010-02-11 13:27:19 +0000 | [diff] [blame] | 931 | |
Jesper Juhl | c83f1d7 | 2012-01-23 22:28:44 +0100 | [diff] [blame] | 932 | out: |
| 933 | release_firmware(fw); |
Mark Brown | 3a66d38 | 2010-02-11 13:27:19 +0000 | [diff] [blame] | 934 | return ret; |
| 935 | } |
| 936 | |
Mark Brown | 3a66d38 | 2010-02-11 13:27:19 +0000 | [diff] [blame] | 937 | static const struct i2c_device_id wm2000_i2c_id[] = { |
| 938 | { "wm2000", 0 }, |
| 939 | { } |
| 940 | }; |
| 941 | MODULE_DEVICE_TABLE(i2c, wm2000_i2c_id); |
| 942 | |
| 943 | static struct i2c_driver wm2000_i2c_driver = { |
| 944 | .driver = { |
| 945 | .name = "wm2000", |
Mark Brown | 3a66d38 | 2010-02-11 13:27:19 +0000 | [diff] [blame] | 946 | }, |
| 947 | .probe = wm2000_i2c_probe, |
Mark Brown | 3a66d38 | 2010-02-11 13:27:19 +0000 | [diff] [blame] | 948 | .id_table = wm2000_i2c_id, |
| 949 | }; |
| 950 | |
Sachin Kamat | 3a4bfd8 | 2012-08-06 17:25:54 +0530 | [diff] [blame] | 951 | module_i2c_driver(wm2000_i2c_driver); |
Mark Brown | 3a66d38 | 2010-02-11 13:27:19 +0000 | [diff] [blame] | 952 | |
| 953 | MODULE_DESCRIPTION("ASoC WM2000 driver"); |
| 954 | MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfonmicro.com>"); |
| 955 | MODULE_LICENSE("GPL"); |