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