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