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