Lars-Peter Clausen | 631ed8a | 2011-06-13 15:26:20 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Driver for ADAU1701 SigmaDSP processor |
| 3 | * |
| 4 | * Copyright 2011 Analog Devices Inc. |
| 5 | * Author: Lars-Peter Clausen <lars@metafoo.de> |
| 6 | * based on an inital version by Cliff Cai <cliff.cai@analog.com> |
| 7 | * |
| 8 | * Licensed under the GPL-2 or later. |
| 9 | */ |
| 10 | |
| 11 | #include <linux/module.h> |
| 12 | #include <linux/init.h> |
| 13 | #include <linux/i2c.h> |
| 14 | #include <linux/delay.h> |
Lars-Peter Clausen | 631ed8a | 2011-06-13 15:26:20 +0200 | [diff] [blame] | 15 | #include <linux/slab.h> |
Daniel Mack | 04561ea | 2013-05-23 15:46:05 +0200 | [diff] [blame] | 16 | #include <linux/of.h> |
| 17 | #include <linux/of_gpio.h> |
| 18 | #include <linux/of_device.h> |
Daniel Mack | 45405d5 | 2013-06-24 16:31:31 +0200 | [diff] [blame] | 19 | #include <linux/regmap.h> |
Lars-Peter Clausen | 631ed8a | 2011-06-13 15:26:20 +0200 | [diff] [blame] | 20 | #include <sound/core.h> |
| 21 | #include <sound/pcm.h> |
| 22 | #include <sound/pcm_params.h> |
| 23 | #include <sound/soc.h> |
| 24 | |
Lars-Peter Clausen | a3a1ec6 | 2014-11-19 18:29:07 +0100 | [diff] [blame] | 25 | #include <asm/unaligned.h> |
| 26 | |
Lars-Peter Clausen | 40216ce | 2011-11-28 09:44:17 +0100 | [diff] [blame] | 27 | #include "sigmadsp.h" |
Lars-Peter Clausen | 631ed8a | 2011-06-13 15:26:20 +0200 | [diff] [blame] | 28 | #include "adau1701.h" |
| 29 | |
Lars-Peter Clausen | a3a1ec6 | 2014-11-19 18:29:07 +0100 | [diff] [blame] | 30 | #define ADAU1701_SAFELOAD_DATA(i) (0x0810 + (i)) |
| 31 | #define ADAU1701_SAFELOAD_ADDR(i) (0x0815 + (i)) |
| 32 | |
Daniel Mack | 45405d5 | 2013-06-24 16:31:31 +0200 | [diff] [blame] | 33 | #define ADAU1701_DSPCTRL 0x081c |
| 34 | #define ADAU1701_SEROCTL 0x081e |
| 35 | #define ADAU1701_SERICTL 0x081f |
Lars-Peter Clausen | 631ed8a | 2011-06-13 15:26:20 +0200 | [diff] [blame] | 36 | |
Daniel Mack | 45405d5 | 2013-06-24 16:31:31 +0200 | [diff] [blame] | 37 | #define ADAU1701_AUXNPOW 0x0822 |
Daniel Mack | 97d0a86 | 2013-06-24 16:31:32 +0200 | [diff] [blame] | 38 | #define ADAU1701_PINCONF_0 0x0820 |
| 39 | #define ADAU1701_PINCONF_1 0x0821 |
| 40 | #define ADAU1701_AUXNPOW 0x0822 |
Lars-Peter Clausen | 631ed8a | 2011-06-13 15:26:20 +0200 | [diff] [blame] | 41 | |
Daniel Mack | 45405d5 | 2013-06-24 16:31:31 +0200 | [diff] [blame] | 42 | #define ADAU1701_OSCIPOW 0x0826 |
| 43 | #define ADAU1701_DACSET 0x0827 |
Lars-Peter Clausen | 631ed8a | 2011-06-13 15:26:20 +0200 | [diff] [blame] | 44 | |
Daniel Mack | 45405d5 | 2013-06-24 16:31:31 +0200 | [diff] [blame] | 45 | #define ADAU1701_MAX_REGISTER 0x0828 |
Lars-Peter Clausen | 631ed8a | 2011-06-13 15:26:20 +0200 | [diff] [blame] | 46 | |
| 47 | #define ADAU1701_DSPCTRL_CR (1 << 2) |
| 48 | #define ADAU1701_DSPCTRL_DAM (1 << 3) |
| 49 | #define ADAU1701_DSPCTRL_ADM (1 << 4) |
Lars-Peter Clausen | a3a1ec6 | 2014-11-19 18:29:07 +0100 | [diff] [blame] | 50 | #define ADAU1701_DSPCTRL_IST (1 << 5) |
Lars-Peter Clausen | 631ed8a | 2011-06-13 15:26:20 +0200 | [diff] [blame] | 51 | #define ADAU1701_DSPCTRL_SR_48 0x00 |
| 52 | #define ADAU1701_DSPCTRL_SR_96 0x01 |
| 53 | #define ADAU1701_DSPCTRL_SR_192 0x02 |
| 54 | #define ADAU1701_DSPCTRL_SR_MASK 0x03 |
| 55 | |
| 56 | #define ADAU1701_SEROCTL_INV_LRCLK 0x2000 |
| 57 | #define ADAU1701_SEROCTL_INV_BCLK 0x1000 |
| 58 | #define ADAU1701_SEROCTL_MASTER 0x0800 |
| 59 | |
| 60 | #define ADAU1701_SEROCTL_OBF16 0x0000 |
| 61 | #define ADAU1701_SEROCTL_OBF8 0x0200 |
| 62 | #define ADAU1701_SEROCTL_OBF4 0x0400 |
| 63 | #define ADAU1701_SEROCTL_OBF2 0x0600 |
| 64 | #define ADAU1701_SEROCTL_OBF_MASK 0x0600 |
| 65 | |
| 66 | #define ADAU1701_SEROCTL_OLF1024 0x0000 |
| 67 | #define ADAU1701_SEROCTL_OLF512 0x0080 |
| 68 | #define ADAU1701_SEROCTL_OLF256 0x0100 |
| 69 | #define ADAU1701_SEROCTL_OLF_MASK 0x0180 |
| 70 | |
| 71 | #define ADAU1701_SEROCTL_MSB_DEALY1 0x0000 |
| 72 | #define ADAU1701_SEROCTL_MSB_DEALY0 0x0004 |
| 73 | #define ADAU1701_SEROCTL_MSB_DEALY8 0x0008 |
| 74 | #define ADAU1701_SEROCTL_MSB_DEALY12 0x000c |
| 75 | #define ADAU1701_SEROCTL_MSB_DEALY16 0x0010 |
| 76 | #define ADAU1701_SEROCTL_MSB_DEALY_MASK 0x001c |
| 77 | |
| 78 | #define ADAU1701_SEROCTL_WORD_LEN_24 0x0000 |
| 79 | #define ADAU1701_SEROCTL_WORD_LEN_20 0x0001 |
Lars-Peter Clausen | e20970a | 2014-01-08 11:22:25 +0100 | [diff] [blame] | 80 | #define ADAU1701_SEROCTL_WORD_LEN_16 0x0002 |
Lars-Peter Clausen | 631ed8a | 2011-06-13 15:26:20 +0200 | [diff] [blame] | 81 | #define ADAU1701_SEROCTL_WORD_LEN_MASK 0x0003 |
| 82 | |
| 83 | #define ADAU1701_AUXNPOW_VBPD 0x40 |
| 84 | #define ADAU1701_AUXNPOW_VRPD 0x20 |
| 85 | |
| 86 | #define ADAU1701_SERICTL_I2S 0 |
| 87 | #define ADAU1701_SERICTL_LEFTJ 1 |
| 88 | #define ADAU1701_SERICTL_TDM 2 |
| 89 | #define ADAU1701_SERICTL_RIGHTJ_24 3 |
| 90 | #define ADAU1701_SERICTL_RIGHTJ_20 4 |
| 91 | #define ADAU1701_SERICTL_RIGHTJ_18 5 |
| 92 | #define ADAU1701_SERICTL_RIGHTJ_16 6 |
| 93 | #define ADAU1701_SERICTL_MODE_MASK 7 |
| 94 | #define ADAU1701_SERICTL_INV_BCLK BIT(3) |
| 95 | #define ADAU1701_SERICTL_INV_LRCLK BIT(4) |
| 96 | |
| 97 | #define ADAU1701_OSCIPOW_OPD 0x04 |
| 98 | #define ADAU1701_DACSET_DACINIT 1 |
| 99 | |
Dan Carpenter | ba51cbb | 2013-07-25 19:40:17 +0300 | [diff] [blame] | 100 | #define ADAU1707_CLKDIV_UNSET (-1U) |
Daniel Mack | 2352d4b | 2013-06-24 16:31:30 +0200 | [diff] [blame] | 101 | |
Lars-Peter Clausen | 631ed8a | 2011-06-13 15:26:20 +0200 | [diff] [blame] | 102 | #define ADAU1701_FIRMWARE "adau1701.bin" |
| 103 | |
| 104 | struct adau1701 { |
Daniel Mack | f724ba3 | 2013-06-07 13:53:04 +0200 | [diff] [blame] | 105 | int gpio_nreset; |
Daniel Mack | 2352d4b | 2013-06-24 16:31:30 +0200 | [diff] [blame] | 106 | int gpio_pll_mode[2]; |
Lars-Peter Clausen | 631ed8a | 2011-06-13 15:26:20 +0200 | [diff] [blame] | 107 | unsigned int dai_fmt; |
Daniel Mack | 2352d4b | 2013-06-24 16:31:30 +0200 | [diff] [blame] | 108 | unsigned int pll_clkdiv; |
| 109 | unsigned int sysclk; |
Daniel Mack | 45405d5 | 2013-06-24 16:31:31 +0200 | [diff] [blame] | 110 | struct regmap *regmap; |
Lars-Peter Clausen | a3a1ec6 | 2014-11-19 18:29:07 +0100 | [diff] [blame] | 111 | struct i2c_client *client; |
Daniel Mack | 97d0a86 | 2013-06-24 16:31:32 +0200 | [diff] [blame] | 112 | u8 pin_config[12]; |
Lars-Peter Clausen | d48b088 | 2014-11-19 18:29:05 +0100 | [diff] [blame] | 113 | |
| 114 | struct sigmadsp *sigmadsp; |
Lars-Peter Clausen | 631ed8a | 2011-06-13 15:26:20 +0200 | [diff] [blame] | 115 | }; |
| 116 | |
| 117 | static const struct snd_kcontrol_new adau1701_controls[] = { |
| 118 | SOC_SINGLE("Master Capture Switch", ADAU1701_DSPCTRL, 4, 1, 0), |
| 119 | }; |
| 120 | |
| 121 | static const struct snd_soc_dapm_widget adau1701_dapm_widgets[] = { |
| 122 | SND_SOC_DAPM_DAC("DAC0", "Playback", ADAU1701_AUXNPOW, 3, 1), |
| 123 | SND_SOC_DAPM_DAC("DAC1", "Playback", ADAU1701_AUXNPOW, 2, 1), |
| 124 | SND_SOC_DAPM_DAC("DAC2", "Playback", ADAU1701_AUXNPOW, 1, 1), |
| 125 | SND_SOC_DAPM_DAC("DAC3", "Playback", ADAU1701_AUXNPOW, 0, 1), |
| 126 | SND_SOC_DAPM_ADC("ADC", "Capture", ADAU1701_AUXNPOW, 7, 1), |
| 127 | |
| 128 | SND_SOC_DAPM_OUTPUT("OUT0"), |
| 129 | SND_SOC_DAPM_OUTPUT("OUT1"), |
| 130 | SND_SOC_DAPM_OUTPUT("OUT2"), |
| 131 | SND_SOC_DAPM_OUTPUT("OUT3"), |
| 132 | SND_SOC_DAPM_INPUT("IN0"), |
| 133 | SND_SOC_DAPM_INPUT("IN1"), |
| 134 | }; |
| 135 | |
| 136 | static const struct snd_soc_dapm_route adau1701_dapm_routes[] = { |
| 137 | { "OUT0", NULL, "DAC0" }, |
| 138 | { "OUT1", NULL, "DAC1" }, |
| 139 | { "OUT2", NULL, "DAC2" }, |
| 140 | { "OUT3", NULL, "DAC3" }, |
| 141 | |
| 142 | { "ADC", NULL, "IN0" }, |
| 143 | { "ADC", NULL, "IN1" }, |
| 144 | }; |
| 145 | |
Daniel Mack | 45405d5 | 2013-06-24 16:31:31 +0200 | [diff] [blame] | 146 | static unsigned int adau1701_register_size(struct device *dev, |
Lars-Peter Clausen | 631ed8a | 2011-06-13 15:26:20 +0200 | [diff] [blame] | 147 | unsigned int reg) |
| 148 | { |
| 149 | switch (reg) { |
Daniel Mack | 97d0a86 | 2013-06-24 16:31:32 +0200 | [diff] [blame] | 150 | case ADAU1701_PINCONF_0: |
| 151 | case ADAU1701_PINCONF_1: |
| 152 | return 3; |
Lars-Peter Clausen | 631ed8a | 2011-06-13 15:26:20 +0200 | [diff] [blame] | 153 | case ADAU1701_DSPCTRL: |
| 154 | case ADAU1701_SEROCTL: |
| 155 | case ADAU1701_AUXNPOW: |
| 156 | case ADAU1701_OSCIPOW: |
| 157 | case ADAU1701_DACSET: |
| 158 | return 2; |
| 159 | case ADAU1701_SERICTL: |
| 160 | return 1; |
| 161 | } |
| 162 | |
Daniel Mack | 45405d5 | 2013-06-24 16:31:31 +0200 | [diff] [blame] | 163 | dev_err(dev, "Unsupported register address: %d\n", reg); |
Lars-Peter Clausen | 631ed8a | 2011-06-13 15:26:20 +0200 | [diff] [blame] | 164 | return 0; |
| 165 | } |
| 166 | |
Daniel Mack | 45405d5 | 2013-06-24 16:31:31 +0200 | [diff] [blame] | 167 | static bool adau1701_volatile_reg(struct device *dev, unsigned int reg) |
Lars-Peter Clausen | 631ed8a | 2011-06-13 15:26:20 +0200 | [diff] [blame] | 168 | { |
Daniel Mack | 45405d5 | 2013-06-24 16:31:31 +0200 | [diff] [blame] | 169 | switch (reg) { |
| 170 | case ADAU1701_DACSET: |
Lars-Peter Clausen | a3a1ec6 | 2014-11-19 18:29:07 +0100 | [diff] [blame] | 171 | case ADAU1701_DSPCTRL: |
Daniel Mack | 45405d5 | 2013-06-24 16:31:31 +0200 | [diff] [blame] | 172 | return true; |
| 173 | default: |
| 174 | return false; |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | static int adau1701_reg_write(void *context, unsigned int reg, |
| 179 | unsigned int value) |
| 180 | { |
| 181 | struct i2c_client *client = context; |
Dan Carpenter | c209740 | 2011-06-20 10:11:25 +0300 | [diff] [blame] | 182 | unsigned int i; |
Lars-Peter Clausen | 631ed8a | 2011-06-13 15:26:20 +0200 | [diff] [blame] | 183 | unsigned int size; |
Daniel Mack | 97d0a86 | 2013-06-24 16:31:32 +0200 | [diff] [blame] | 184 | uint8_t buf[5]; |
Dan Carpenter | c209740 | 2011-06-20 10:11:25 +0300 | [diff] [blame] | 185 | int ret; |
Lars-Peter Clausen | 631ed8a | 2011-06-13 15:26:20 +0200 | [diff] [blame] | 186 | |
Daniel Mack | 45405d5 | 2013-06-24 16:31:31 +0200 | [diff] [blame] | 187 | size = adau1701_register_size(&client->dev, reg); |
Lars-Peter Clausen | 631ed8a | 2011-06-13 15:26:20 +0200 | [diff] [blame] | 188 | if (size == 0) |
| 189 | return -EINVAL; |
| 190 | |
Daniel Mack | 45405d5 | 2013-06-24 16:31:31 +0200 | [diff] [blame] | 191 | buf[0] = reg >> 8; |
| 192 | buf[1] = reg & 0xff; |
Lars-Peter Clausen | 631ed8a | 2011-06-13 15:26:20 +0200 | [diff] [blame] | 193 | |
| 194 | for (i = size + 1; i >= 2; --i) { |
| 195 | buf[i] = value; |
| 196 | value >>= 8; |
| 197 | } |
| 198 | |
Daniel Mack | 45405d5 | 2013-06-24 16:31:31 +0200 | [diff] [blame] | 199 | ret = i2c_master_send(client, buf, size + 2); |
Lars-Peter Clausen | 631ed8a | 2011-06-13 15:26:20 +0200 | [diff] [blame] | 200 | if (ret == size + 2) |
| 201 | return 0; |
| 202 | else if (ret < 0) |
| 203 | return ret; |
| 204 | else |
| 205 | return -EIO; |
| 206 | } |
| 207 | |
Daniel Mack | 45405d5 | 2013-06-24 16:31:31 +0200 | [diff] [blame] | 208 | static int adau1701_reg_read(void *context, unsigned int reg, |
| 209 | unsigned int *value) |
Lars-Peter Clausen | 631ed8a | 2011-06-13 15:26:20 +0200 | [diff] [blame] | 210 | { |
Daniel Mack | 45405d5 | 2013-06-24 16:31:31 +0200 | [diff] [blame] | 211 | int ret; |
| 212 | unsigned int i; |
| 213 | unsigned int size; |
| 214 | uint8_t send_buf[2], recv_buf[3]; |
| 215 | struct i2c_client *client = context; |
| 216 | struct i2c_msg msgs[2]; |
Lars-Peter Clausen | 631ed8a | 2011-06-13 15:26:20 +0200 | [diff] [blame] | 217 | |
Daniel Mack | 45405d5 | 2013-06-24 16:31:31 +0200 | [diff] [blame] | 218 | size = adau1701_register_size(&client->dev, reg); |
| 219 | if (size == 0) |
| 220 | return -EINVAL; |
| 221 | |
| 222 | send_buf[0] = reg >> 8; |
| 223 | send_buf[1] = reg & 0xff; |
| 224 | |
| 225 | msgs[0].addr = client->addr; |
| 226 | msgs[0].len = sizeof(send_buf); |
| 227 | msgs[0].buf = send_buf; |
| 228 | msgs[0].flags = 0; |
| 229 | |
| 230 | msgs[1].addr = client->addr; |
| 231 | msgs[1].len = size; |
| 232 | msgs[1].buf = recv_buf; |
| 233 | msgs[1].flags = I2C_M_RD; |
| 234 | |
| 235 | ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs)); |
| 236 | if (ret < 0) |
Lars-Peter Clausen | 631ed8a | 2011-06-13 15:26:20 +0200 | [diff] [blame] | 237 | return ret; |
Daniel Mack | 45405d5 | 2013-06-24 16:31:31 +0200 | [diff] [blame] | 238 | else if (ret != ARRAY_SIZE(msgs)) |
| 239 | return -EIO; |
Lars-Peter Clausen | 631ed8a | 2011-06-13 15:26:20 +0200 | [diff] [blame] | 240 | |
Daniel Mack | 45405d5 | 2013-06-24 16:31:31 +0200 | [diff] [blame] | 241 | *value = 0; |
| 242 | |
Daniel Mack | 3ad80b8 | 2014-07-03 16:51:36 +0200 | [diff] [blame] | 243 | for (i = 0; i < size; i++) { |
| 244 | *value <<= 8; |
| 245 | *value |= recv_buf[i]; |
| 246 | } |
Daniel Mack | 45405d5 | 2013-06-24 16:31:31 +0200 | [diff] [blame] | 247 | |
| 248 | return 0; |
Lars-Peter Clausen | 631ed8a | 2011-06-13 15:26:20 +0200 | [diff] [blame] | 249 | } |
| 250 | |
Lars-Peter Clausen | a3a1ec6 | 2014-11-19 18:29:07 +0100 | [diff] [blame] | 251 | static int adau1701_safeload(struct sigmadsp *sigmadsp, unsigned int addr, |
| 252 | const uint8_t bytes[], size_t len) |
| 253 | { |
| 254 | struct i2c_client *client = to_i2c_client(sigmadsp->dev); |
| 255 | struct adau1701 *adau1701 = i2c_get_clientdata(client); |
| 256 | unsigned int val; |
| 257 | unsigned int i; |
| 258 | uint8_t buf[10]; |
| 259 | int ret; |
| 260 | |
| 261 | ret = regmap_read(adau1701->regmap, ADAU1701_DSPCTRL, &val); |
| 262 | if (ret) |
| 263 | return ret; |
| 264 | |
| 265 | if (val & ADAU1701_DSPCTRL_IST) |
| 266 | msleep(50); |
| 267 | |
| 268 | for (i = 0; i < len / 4; i++) { |
| 269 | put_unaligned_le16(ADAU1701_SAFELOAD_DATA(i), buf); |
| 270 | buf[2] = 0x00; |
| 271 | memcpy(buf + 3, bytes + i * 4, 4); |
| 272 | ret = i2c_master_send(client, buf, 7); |
| 273 | if (ret < 0) |
| 274 | return ret; |
| 275 | else if (ret != 7) |
| 276 | return -EIO; |
| 277 | |
| 278 | put_unaligned_le16(ADAU1701_SAFELOAD_ADDR(i), buf); |
| 279 | put_unaligned_le16(addr + i, buf + 2); |
| 280 | ret = i2c_master_send(client, buf, 4); |
| 281 | if (ret < 0) |
| 282 | return ret; |
| 283 | else if (ret != 4) |
| 284 | return -EIO; |
| 285 | } |
| 286 | |
| 287 | return regmap_update_bits(adau1701->regmap, ADAU1701_DSPCTRL, |
| 288 | ADAU1701_DSPCTRL_IST, ADAU1701_DSPCTRL_IST); |
| 289 | } |
| 290 | |
| 291 | static const struct sigmadsp_ops adau1701_sigmadsp_ops = { |
| 292 | .safeload = adau1701_safeload, |
| 293 | }; |
| 294 | |
Lars-Peter Clausen | d48b088 | 2014-11-19 18:29:05 +0100 | [diff] [blame] | 295 | static int adau1701_reset(struct snd_soc_codec *codec, unsigned int clkdiv, |
| 296 | unsigned int rate) |
Lars-Peter Clausen | 631ed8a | 2011-06-13 15:26:20 +0200 | [diff] [blame] | 297 | { |
Daniel Mack | f724ba3 | 2013-06-07 13:53:04 +0200 | [diff] [blame] | 298 | struct adau1701 *adau1701 = snd_soc_codec_get_drvdata(codec); |
Daniel Mack | de9fc72 | 2013-06-24 16:31:29 +0200 | [diff] [blame] | 299 | int ret; |
Daniel Mack | f724ba3 | 2013-06-07 13:53:04 +0200 | [diff] [blame] | 300 | |
Lars-Peter Clausen | d48b088 | 2014-11-19 18:29:05 +0100 | [diff] [blame] | 301 | sigmadsp_reset(adau1701->sigmadsp); |
| 302 | |
Daniel Mack | 2352d4b | 2013-06-24 16:31:30 +0200 | [diff] [blame] | 303 | if (clkdiv != ADAU1707_CLKDIV_UNSET && |
| 304 | gpio_is_valid(adau1701->gpio_pll_mode[0]) && |
| 305 | gpio_is_valid(adau1701->gpio_pll_mode[1])) { |
| 306 | switch (clkdiv) { |
| 307 | case 64: |
Mark Brown | 9190aeb | 2013-08-11 15:07:36 +0100 | [diff] [blame] | 308 | gpio_set_value_cansleep(adau1701->gpio_pll_mode[0], 0); |
| 309 | gpio_set_value_cansleep(adau1701->gpio_pll_mode[1], 0); |
Daniel Mack | 2352d4b | 2013-06-24 16:31:30 +0200 | [diff] [blame] | 310 | break; |
| 311 | case 256: |
Mark Brown | 9190aeb | 2013-08-11 15:07:36 +0100 | [diff] [blame] | 312 | gpio_set_value_cansleep(adau1701->gpio_pll_mode[0], 0); |
| 313 | gpio_set_value_cansleep(adau1701->gpio_pll_mode[1], 1); |
Daniel Mack | 2352d4b | 2013-06-24 16:31:30 +0200 | [diff] [blame] | 314 | break; |
| 315 | case 384: |
Mark Brown | 9190aeb | 2013-08-11 15:07:36 +0100 | [diff] [blame] | 316 | gpio_set_value_cansleep(adau1701->gpio_pll_mode[0], 1); |
| 317 | gpio_set_value_cansleep(adau1701->gpio_pll_mode[1], 0); |
Daniel Mack | 2352d4b | 2013-06-24 16:31:30 +0200 | [diff] [blame] | 318 | break; |
| 319 | case 0: /* fallback */ |
| 320 | case 512: |
Mark Brown | 9190aeb | 2013-08-11 15:07:36 +0100 | [diff] [blame] | 321 | gpio_set_value_cansleep(adau1701->gpio_pll_mode[0], 1); |
| 322 | gpio_set_value_cansleep(adau1701->gpio_pll_mode[1], 1); |
Daniel Mack | 2352d4b | 2013-06-24 16:31:30 +0200 | [diff] [blame] | 323 | break; |
| 324 | } |
| 325 | } |
| 326 | |
| 327 | adau1701->pll_clkdiv = clkdiv; |
| 328 | |
Daniel Mack | de9fc72 | 2013-06-24 16:31:29 +0200 | [diff] [blame] | 329 | if (gpio_is_valid(adau1701->gpio_nreset)) { |
Mark Brown | 9190aeb | 2013-08-11 15:07:36 +0100 | [diff] [blame] | 330 | gpio_set_value_cansleep(adau1701->gpio_nreset, 0); |
Daniel Mack | de9fc72 | 2013-06-24 16:31:29 +0200 | [diff] [blame] | 331 | /* minimum reset time is 20ns */ |
| 332 | udelay(1); |
Mark Brown | 9190aeb | 2013-08-11 15:07:36 +0100 | [diff] [blame] | 333 | gpio_set_value_cansleep(adau1701->gpio_nreset, 1); |
Daniel Mack | de9fc72 | 2013-06-24 16:31:29 +0200 | [diff] [blame] | 334 | /* power-up time may be as long as 85ms */ |
| 335 | mdelay(85); |
| 336 | } |
Daniel Mack | f724ba3 | 2013-06-07 13:53:04 +0200 | [diff] [blame] | 337 | |
Daniel Mack | 2352d4b | 2013-06-24 16:31:30 +0200 | [diff] [blame] | 338 | /* |
| 339 | * Postpone the firmware download to a point in time when we |
| 340 | * know the correct PLL setup |
| 341 | */ |
| 342 | if (clkdiv != ADAU1707_CLKDIV_UNSET) { |
Lars-Peter Clausen | d48b088 | 2014-11-19 18:29:05 +0100 | [diff] [blame] | 343 | ret = sigmadsp_setup(adau1701->sigmadsp, rate); |
Daniel Mack | 2352d4b | 2013-06-24 16:31:30 +0200 | [diff] [blame] | 344 | if (ret) { |
| 345 | dev_warn(codec->dev, "Failed to load firmware\n"); |
| 346 | return ret; |
| 347 | } |
Daniel Mack | f724ba3 | 2013-06-07 13:53:04 +0200 | [diff] [blame] | 348 | } |
| 349 | |
Daniel Mack | 45405d5 | 2013-06-24 16:31:31 +0200 | [diff] [blame] | 350 | regmap_write(adau1701->regmap, ADAU1701_DACSET, ADAU1701_DACSET_DACINIT); |
| 351 | regmap_write(adau1701->regmap, ADAU1701_DSPCTRL, ADAU1701_DSPCTRL_CR); |
| 352 | |
| 353 | regcache_mark_dirty(adau1701->regmap); |
| 354 | regcache_sync(adau1701->regmap); |
Daniel Mack | f724ba3 | 2013-06-07 13:53:04 +0200 | [diff] [blame] | 355 | |
| 356 | return 0; |
Lars-Peter Clausen | 631ed8a | 2011-06-13 15:26:20 +0200 | [diff] [blame] | 357 | } |
| 358 | |
| 359 | static int adau1701_set_capture_pcm_format(struct snd_soc_codec *codec, |
Mark Brown | 9b58e71 | 2014-01-08 18:50:25 +0000 | [diff] [blame] | 360 | struct snd_pcm_hw_params *params) |
Lars-Peter Clausen | 631ed8a | 2011-06-13 15:26:20 +0200 | [diff] [blame] | 361 | { |
| 362 | struct adau1701 *adau1701 = snd_soc_codec_get_drvdata(codec); |
| 363 | unsigned int mask = ADAU1701_SEROCTL_WORD_LEN_MASK; |
| 364 | unsigned int val; |
| 365 | |
Mark Brown | 9b58e71 | 2014-01-08 18:50:25 +0000 | [diff] [blame] | 366 | switch (params_width(params)) { |
| 367 | case 16: |
Lars-Peter Clausen | 631ed8a | 2011-06-13 15:26:20 +0200 | [diff] [blame] | 368 | val = ADAU1701_SEROCTL_WORD_LEN_16; |
| 369 | break; |
Mark Brown | 9b58e71 | 2014-01-08 18:50:25 +0000 | [diff] [blame] | 370 | case 20: |
Lars-Peter Clausen | 631ed8a | 2011-06-13 15:26:20 +0200 | [diff] [blame] | 371 | val = ADAU1701_SEROCTL_WORD_LEN_20; |
| 372 | break; |
Mark Brown | 9b58e71 | 2014-01-08 18:50:25 +0000 | [diff] [blame] | 373 | case 24: |
Lars-Peter Clausen | 631ed8a | 2011-06-13 15:26:20 +0200 | [diff] [blame] | 374 | val = ADAU1701_SEROCTL_WORD_LEN_24; |
| 375 | break; |
| 376 | default: |
| 377 | return -EINVAL; |
| 378 | } |
| 379 | |
| 380 | if (adau1701->dai_fmt == SND_SOC_DAIFMT_RIGHT_J) { |
Mark Brown | 9b58e71 | 2014-01-08 18:50:25 +0000 | [diff] [blame] | 381 | switch (params_width(params)) { |
| 382 | case 16: |
Lars-Peter Clausen | 631ed8a | 2011-06-13 15:26:20 +0200 | [diff] [blame] | 383 | val |= ADAU1701_SEROCTL_MSB_DEALY16; |
| 384 | break; |
Mark Brown | 9b58e71 | 2014-01-08 18:50:25 +0000 | [diff] [blame] | 385 | case 20: |
Lars-Peter Clausen | 631ed8a | 2011-06-13 15:26:20 +0200 | [diff] [blame] | 386 | val |= ADAU1701_SEROCTL_MSB_DEALY12; |
| 387 | break; |
Mark Brown | 9b58e71 | 2014-01-08 18:50:25 +0000 | [diff] [blame] | 388 | case 24: |
Lars-Peter Clausen | 631ed8a | 2011-06-13 15:26:20 +0200 | [diff] [blame] | 389 | val |= ADAU1701_SEROCTL_MSB_DEALY8; |
| 390 | break; |
| 391 | } |
| 392 | mask |= ADAU1701_SEROCTL_MSB_DEALY_MASK; |
| 393 | } |
| 394 | |
Daniel Mack | ee44114 | 2013-06-27 22:00:04 +0200 | [diff] [blame] | 395 | regmap_update_bits(adau1701->regmap, ADAU1701_SEROCTL, mask, val); |
Lars-Peter Clausen | 631ed8a | 2011-06-13 15:26:20 +0200 | [diff] [blame] | 396 | |
| 397 | return 0; |
| 398 | } |
| 399 | |
| 400 | static int adau1701_set_playback_pcm_format(struct snd_soc_codec *codec, |
Mark Brown | 9b58e71 | 2014-01-08 18:50:25 +0000 | [diff] [blame] | 401 | struct snd_pcm_hw_params *params) |
Lars-Peter Clausen | 631ed8a | 2011-06-13 15:26:20 +0200 | [diff] [blame] | 402 | { |
| 403 | struct adau1701 *adau1701 = snd_soc_codec_get_drvdata(codec); |
| 404 | unsigned int val; |
| 405 | |
| 406 | if (adau1701->dai_fmt != SND_SOC_DAIFMT_RIGHT_J) |
| 407 | return 0; |
| 408 | |
Mark Brown | 9b58e71 | 2014-01-08 18:50:25 +0000 | [diff] [blame] | 409 | switch (params_width(params)) { |
| 410 | case 16: |
Lars-Peter Clausen | 631ed8a | 2011-06-13 15:26:20 +0200 | [diff] [blame] | 411 | val = ADAU1701_SERICTL_RIGHTJ_16; |
| 412 | break; |
Mark Brown | 9b58e71 | 2014-01-08 18:50:25 +0000 | [diff] [blame] | 413 | case 20: |
Lars-Peter Clausen | 631ed8a | 2011-06-13 15:26:20 +0200 | [diff] [blame] | 414 | val = ADAU1701_SERICTL_RIGHTJ_20; |
| 415 | break; |
Mark Brown | 9b58e71 | 2014-01-08 18:50:25 +0000 | [diff] [blame] | 416 | case 24: |
Lars-Peter Clausen | 631ed8a | 2011-06-13 15:26:20 +0200 | [diff] [blame] | 417 | val = ADAU1701_SERICTL_RIGHTJ_24; |
| 418 | break; |
| 419 | default: |
| 420 | return -EINVAL; |
| 421 | } |
| 422 | |
Daniel Mack | ee44114 | 2013-06-27 22:00:04 +0200 | [diff] [blame] | 423 | regmap_update_bits(adau1701->regmap, ADAU1701_SERICTL, |
Lars-Peter Clausen | 631ed8a | 2011-06-13 15:26:20 +0200 | [diff] [blame] | 424 | ADAU1701_SERICTL_MODE_MASK, val); |
| 425 | |
| 426 | return 0; |
| 427 | } |
| 428 | |
| 429 | static int adau1701_hw_params(struct snd_pcm_substream *substream, |
| 430 | struct snd_pcm_hw_params *params, struct snd_soc_dai *dai) |
| 431 | { |
Mark Brown | e6968a1 | 2012-04-04 15:58:16 +0100 | [diff] [blame] | 432 | struct snd_soc_codec *codec = dai->codec; |
Daniel Mack | 2352d4b | 2013-06-24 16:31:30 +0200 | [diff] [blame] | 433 | struct adau1701 *adau1701 = snd_soc_codec_get_drvdata(codec); |
| 434 | unsigned int clkdiv = adau1701->sysclk / params_rate(params); |
Lars-Peter Clausen | 631ed8a | 2011-06-13 15:26:20 +0200 | [diff] [blame] | 435 | unsigned int val; |
Daniel Mack | 2352d4b | 2013-06-24 16:31:30 +0200 | [diff] [blame] | 436 | int ret; |
| 437 | |
| 438 | /* |
| 439 | * If the mclk/lrclk ratio changes, the chip needs updated PLL |
| 440 | * mode GPIO settings, and a full reset cycle, including a new |
| 441 | * firmware upload. |
| 442 | */ |
| 443 | if (clkdiv != adau1701->pll_clkdiv) { |
Lars-Peter Clausen | d48b088 | 2014-11-19 18:29:05 +0100 | [diff] [blame] | 444 | ret = adau1701_reset(codec, clkdiv, params_rate(params)); |
Daniel Mack | 2352d4b | 2013-06-24 16:31:30 +0200 | [diff] [blame] | 445 | if (ret < 0) |
| 446 | return ret; |
| 447 | } |
Lars-Peter Clausen | 631ed8a | 2011-06-13 15:26:20 +0200 | [diff] [blame] | 448 | |
| 449 | switch (params_rate(params)) { |
| 450 | case 192000: |
| 451 | val = ADAU1701_DSPCTRL_SR_192; |
| 452 | break; |
| 453 | case 96000: |
| 454 | val = ADAU1701_DSPCTRL_SR_96; |
| 455 | break; |
| 456 | case 48000: |
| 457 | val = ADAU1701_DSPCTRL_SR_48; |
| 458 | break; |
| 459 | default: |
| 460 | return -EINVAL; |
| 461 | } |
| 462 | |
Daniel Mack | ee44114 | 2013-06-27 22:00:04 +0200 | [diff] [blame] | 463 | regmap_update_bits(adau1701->regmap, ADAU1701_DSPCTRL, |
Lars-Peter Clausen | 631ed8a | 2011-06-13 15:26:20 +0200 | [diff] [blame] | 464 | ADAU1701_DSPCTRL_SR_MASK, val); |
| 465 | |
Lars-Peter Clausen | 631ed8a | 2011-06-13 15:26:20 +0200 | [diff] [blame] | 466 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) |
Mark Brown | 9b58e71 | 2014-01-08 18:50:25 +0000 | [diff] [blame] | 467 | return adau1701_set_playback_pcm_format(codec, params); |
Lars-Peter Clausen | 631ed8a | 2011-06-13 15:26:20 +0200 | [diff] [blame] | 468 | else |
Mark Brown | 9b58e71 | 2014-01-08 18:50:25 +0000 | [diff] [blame] | 469 | return adau1701_set_capture_pcm_format(codec, params); |
Lars-Peter Clausen | 631ed8a | 2011-06-13 15:26:20 +0200 | [diff] [blame] | 470 | } |
| 471 | |
| 472 | static int adau1701_set_dai_fmt(struct snd_soc_dai *codec_dai, |
| 473 | unsigned int fmt) |
| 474 | { |
| 475 | struct snd_soc_codec *codec = codec_dai->codec; |
| 476 | struct adau1701 *adau1701 = snd_soc_codec_get_drvdata(codec); |
| 477 | unsigned int serictl = 0x00, seroctl = 0x00; |
| 478 | bool invert_lrclk; |
| 479 | |
| 480 | switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) { |
| 481 | case SND_SOC_DAIFMT_CBM_CFM: |
| 482 | /* master, 64-bits per sample, 1 frame per sample */ |
| 483 | seroctl |= ADAU1701_SEROCTL_MASTER | ADAU1701_SEROCTL_OBF16 |
| 484 | | ADAU1701_SEROCTL_OLF1024; |
| 485 | break; |
| 486 | case SND_SOC_DAIFMT_CBS_CFS: |
| 487 | break; |
| 488 | default: |
| 489 | return -EINVAL; |
| 490 | } |
| 491 | |
| 492 | /* clock inversion */ |
| 493 | switch (fmt & SND_SOC_DAIFMT_INV_MASK) { |
| 494 | case SND_SOC_DAIFMT_NB_NF: |
| 495 | invert_lrclk = false; |
| 496 | break; |
| 497 | case SND_SOC_DAIFMT_NB_IF: |
| 498 | invert_lrclk = true; |
| 499 | break; |
| 500 | case SND_SOC_DAIFMT_IB_NF: |
| 501 | invert_lrclk = false; |
| 502 | serictl |= ADAU1701_SERICTL_INV_BCLK; |
| 503 | seroctl |= ADAU1701_SEROCTL_INV_BCLK; |
| 504 | break; |
| 505 | case SND_SOC_DAIFMT_IB_IF: |
| 506 | invert_lrclk = true; |
| 507 | serictl |= ADAU1701_SERICTL_INV_BCLK; |
| 508 | seroctl |= ADAU1701_SEROCTL_INV_BCLK; |
| 509 | break; |
| 510 | default: |
| 511 | return -EINVAL; |
| 512 | } |
| 513 | |
| 514 | switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) { |
| 515 | case SND_SOC_DAIFMT_I2S: |
| 516 | break; |
| 517 | case SND_SOC_DAIFMT_LEFT_J: |
| 518 | serictl |= ADAU1701_SERICTL_LEFTJ; |
| 519 | seroctl |= ADAU1701_SEROCTL_MSB_DEALY0; |
| 520 | invert_lrclk = !invert_lrclk; |
| 521 | break; |
| 522 | case SND_SOC_DAIFMT_RIGHT_J: |
| 523 | serictl |= ADAU1701_SERICTL_RIGHTJ_24; |
| 524 | seroctl |= ADAU1701_SEROCTL_MSB_DEALY8; |
| 525 | invert_lrclk = !invert_lrclk; |
| 526 | break; |
| 527 | default: |
| 528 | return -EINVAL; |
| 529 | } |
| 530 | |
| 531 | if (invert_lrclk) { |
| 532 | seroctl |= ADAU1701_SEROCTL_INV_LRCLK; |
| 533 | serictl |= ADAU1701_SERICTL_INV_LRCLK; |
| 534 | } |
| 535 | |
| 536 | adau1701->dai_fmt = fmt & SND_SOC_DAIFMT_FORMAT_MASK; |
| 537 | |
Daniel Mack | 45405d5 | 2013-06-24 16:31:31 +0200 | [diff] [blame] | 538 | regmap_write(adau1701->regmap, ADAU1701_SERICTL, serictl); |
| 539 | regmap_update_bits(adau1701->regmap, ADAU1701_SEROCTL, |
Lars-Peter Clausen | 631ed8a | 2011-06-13 15:26:20 +0200 | [diff] [blame] | 540 | ~ADAU1701_SEROCTL_WORD_LEN_MASK, seroctl); |
| 541 | |
| 542 | return 0; |
| 543 | } |
| 544 | |
| 545 | static int adau1701_set_bias_level(struct snd_soc_codec *codec, |
| 546 | enum snd_soc_bias_level level) |
| 547 | { |
| 548 | unsigned int mask = ADAU1701_AUXNPOW_VBPD | ADAU1701_AUXNPOW_VRPD; |
Daniel Mack | ee44114 | 2013-06-27 22:00:04 +0200 | [diff] [blame] | 549 | struct adau1701 *adau1701 = snd_soc_codec_get_drvdata(codec); |
Lars-Peter Clausen | 631ed8a | 2011-06-13 15:26:20 +0200 | [diff] [blame] | 550 | |
| 551 | switch (level) { |
| 552 | case SND_SOC_BIAS_ON: |
| 553 | break; |
| 554 | case SND_SOC_BIAS_PREPARE: |
| 555 | break; |
| 556 | case SND_SOC_BIAS_STANDBY: |
| 557 | /* Enable VREF and VREF buffer */ |
Daniel Mack | ee44114 | 2013-06-27 22:00:04 +0200 | [diff] [blame] | 558 | regmap_update_bits(adau1701->regmap, |
| 559 | ADAU1701_AUXNPOW, mask, 0x00); |
Lars-Peter Clausen | 631ed8a | 2011-06-13 15:26:20 +0200 | [diff] [blame] | 560 | break; |
| 561 | case SND_SOC_BIAS_OFF: |
| 562 | /* Disable VREF and VREF buffer */ |
Daniel Mack | ee44114 | 2013-06-27 22:00:04 +0200 | [diff] [blame] | 563 | regmap_update_bits(adau1701->regmap, |
| 564 | ADAU1701_AUXNPOW, mask, mask); |
Lars-Peter Clausen | 631ed8a | 2011-06-13 15:26:20 +0200 | [diff] [blame] | 565 | break; |
| 566 | } |
| 567 | |
| 568 | codec->dapm.bias_level = level; |
| 569 | return 0; |
| 570 | } |
| 571 | |
| 572 | static int adau1701_digital_mute(struct snd_soc_dai *dai, int mute) |
| 573 | { |
| 574 | struct snd_soc_codec *codec = dai->codec; |
| 575 | unsigned int mask = ADAU1701_DSPCTRL_DAM; |
Daniel Mack | ee44114 | 2013-06-27 22:00:04 +0200 | [diff] [blame] | 576 | struct adau1701 *adau1701 = snd_soc_codec_get_drvdata(codec); |
Lars-Peter Clausen | 631ed8a | 2011-06-13 15:26:20 +0200 | [diff] [blame] | 577 | unsigned int val; |
| 578 | |
| 579 | if (mute) |
| 580 | val = 0; |
| 581 | else |
| 582 | val = mask; |
| 583 | |
Daniel Mack | ee44114 | 2013-06-27 22:00:04 +0200 | [diff] [blame] | 584 | regmap_update_bits(adau1701->regmap, ADAU1701_DSPCTRL, mask, val); |
Lars-Peter Clausen | 631ed8a | 2011-06-13 15:26:20 +0200 | [diff] [blame] | 585 | |
| 586 | return 0; |
| 587 | } |
| 588 | |
| 589 | static int adau1701_set_sysclk(struct snd_soc_codec *codec, int clk_id, |
Axel Lin | 21326db | 2011-09-28 13:48:35 +0800 | [diff] [blame] | 590 | int source, unsigned int freq, int dir) |
Lars-Peter Clausen | 631ed8a | 2011-06-13 15:26:20 +0200 | [diff] [blame] | 591 | { |
| 592 | unsigned int val; |
Daniel Mack | 2352d4b | 2013-06-24 16:31:30 +0200 | [diff] [blame] | 593 | struct adau1701 *adau1701 = snd_soc_codec_get_drvdata(codec); |
Lars-Peter Clausen | 631ed8a | 2011-06-13 15:26:20 +0200 | [diff] [blame] | 594 | |
| 595 | switch (clk_id) { |
| 596 | case ADAU1701_CLK_SRC_OSC: |
| 597 | val = 0x0; |
| 598 | break; |
| 599 | case ADAU1701_CLK_SRC_MCLK: |
| 600 | val = ADAU1701_OSCIPOW_OPD; |
| 601 | break; |
| 602 | default: |
| 603 | return -EINVAL; |
| 604 | } |
| 605 | |
Daniel Mack | ee44114 | 2013-06-27 22:00:04 +0200 | [diff] [blame] | 606 | regmap_update_bits(adau1701->regmap, ADAU1701_OSCIPOW, |
| 607 | ADAU1701_OSCIPOW_OPD, val); |
Daniel Mack | 2352d4b | 2013-06-24 16:31:30 +0200 | [diff] [blame] | 608 | adau1701->sysclk = freq; |
Lars-Peter Clausen | 631ed8a | 2011-06-13 15:26:20 +0200 | [diff] [blame] | 609 | |
| 610 | return 0; |
| 611 | } |
| 612 | |
Lars-Peter Clausen | d48b088 | 2014-11-19 18:29:05 +0100 | [diff] [blame] | 613 | static int adau1701_startup(struct snd_pcm_substream *substream, |
| 614 | struct snd_soc_dai *dai) |
| 615 | { |
| 616 | struct adau1701 *adau1701 = snd_soc_codec_get_drvdata(dai->codec); |
| 617 | |
| 618 | return sigmadsp_restrict_params(adau1701->sigmadsp, substream); |
| 619 | } |
| 620 | |
Lars-Peter Clausen | 631ed8a | 2011-06-13 15:26:20 +0200 | [diff] [blame] | 621 | #define ADAU1701_RATES (SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_96000 | \ |
| 622 | SNDRV_PCM_RATE_192000) |
| 623 | |
| 624 | #define ADAU1701_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\ |
| 625 | SNDRV_PCM_FMTBIT_S24_LE) |
| 626 | |
Lars-Peter Clausen | 890754a | 2011-11-23 14:11:21 +0100 | [diff] [blame] | 627 | static const struct snd_soc_dai_ops adau1701_dai_ops = { |
Lars-Peter Clausen | 631ed8a | 2011-06-13 15:26:20 +0200 | [diff] [blame] | 628 | .set_fmt = adau1701_set_dai_fmt, |
| 629 | .hw_params = adau1701_hw_params, |
| 630 | .digital_mute = adau1701_digital_mute, |
Lars-Peter Clausen | d48b088 | 2014-11-19 18:29:05 +0100 | [diff] [blame] | 631 | .startup = adau1701_startup, |
Lars-Peter Clausen | 631ed8a | 2011-06-13 15:26:20 +0200 | [diff] [blame] | 632 | }; |
| 633 | |
| 634 | static struct snd_soc_dai_driver adau1701_dai = { |
| 635 | .name = "adau1701", |
| 636 | .playback = { |
| 637 | .stream_name = "Playback", |
| 638 | .channels_min = 2, |
| 639 | .channels_max = 8, |
| 640 | .rates = ADAU1701_RATES, |
| 641 | .formats = ADAU1701_FORMATS, |
| 642 | }, |
| 643 | .capture = { |
| 644 | .stream_name = "Capture", |
| 645 | .channels_min = 2, |
| 646 | .channels_max = 8, |
| 647 | .rates = ADAU1701_RATES, |
| 648 | .formats = ADAU1701_FORMATS, |
| 649 | }, |
| 650 | .ops = &adau1701_dai_ops, |
| 651 | .symmetric_rates = 1, |
| 652 | }; |
| 653 | |
Daniel Mack | 04561ea | 2013-05-23 15:46:05 +0200 | [diff] [blame] | 654 | #ifdef CONFIG_OF |
| 655 | static const struct of_device_id adau1701_dt_ids[] = { |
| 656 | { .compatible = "adi,adau1701", }, |
| 657 | { } |
| 658 | }; |
| 659 | MODULE_DEVICE_TABLE(of, adau1701_dt_ids); |
| 660 | #endif |
| 661 | |
Lars-Peter Clausen | 631ed8a | 2011-06-13 15:26:20 +0200 | [diff] [blame] | 662 | static int adau1701_probe(struct snd_soc_codec *codec) |
| 663 | { |
Daniel Mack | 97d0a86 | 2013-06-24 16:31:32 +0200 | [diff] [blame] | 664 | int i, ret; |
| 665 | unsigned int val; |
Daniel Mack | 2352d4b | 2013-06-24 16:31:30 +0200 | [diff] [blame] | 666 | struct adau1701 *adau1701 = snd_soc_codec_get_drvdata(codec); |
Lars-Peter Clausen | 631ed8a | 2011-06-13 15:26:20 +0200 | [diff] [blame] | 667 | |
Lars-Peter Clausen | d48b088 | 2014-11-19 18:29:05 +0100 | [diff] [blame] | 668 | ret = sigmadsp_attach(adau1701->sigmadsp, &codec->component); |
| 669 | if (ret) |
| 670 | return ret; |
| 671 | |
Daniel Mack | 2352d4b | 2013-06-24 16:31:30 +0200 | [diff] [blame] | 672 | /* |
| 673 | * Let the pll_clkdiv variable default to something that won't happen |
| 674 | * at runtime. That way, we can postpone the firmware download from |
| 675 | * adau1701_reset() to a point in time when we know the correct PLL |
| 676 | * mode parameters. |
| 677 | */ |
| 678 | adau1701->pll_clkdiv = ADAU1707_CLKDIV_UNSET; |
| 679 | |
| 680 | /* initalize with pre-configured pll mode settings */ |
Lars-Peter Clausen | d48b088 | 2014-11-19 18:29:05 +0100 | [diff] [blame] | 681 | ret = adau1701_reset(codec, adau1701->pll_clkdiv, 0); |
Daniel Mack | 2352d4b | 2013-06-24 16:31:30 +0200 | [diff] [blame] | 682 | if (ret < 0) |
Daniel Mack | f724ba3 | 2013-06-07 13:53:04 +0200 | [diff] [blame] | 683 | return ret; |
Lars-Peter Clausen | 631ed8a | 2011-06-13 15:26:20 +0200 | [diff] [blame] | 684 | |
Daniel Mack | 97d0a86 | 2013-06-24 16:31:32 +0200 | [diff] [blame] | 685 | /* set up pin config */ |
| 686 | val = 0; |
| 687 | for (i = 0; i < 6; i++) |
| 688 | val |= adau1701->pin_config[i] << (i * 4); |
| 689 | |
| 690 | regmap_write(adau1701->regmap, ADAU1701_PINCONF_0, val); |
| 691 | |
| 692 | val = 0; |
| 693 | for (i = 0; i < 6; i++) |
| 694 | val |= adau1701->pin_config[i + 6] << (i * 4); |
| 695 | |
| 696 | regmap_write(adau1701->regmap, ADAU1701_PINCONF_1, val); |
| 697 | |
Lars-Peter Clausen | 631ed8a | 2011-06-13 15:26:20 +0200 | [diff] [blame] | 698 | return 0; |
| 699 | } |
| 700 | |
| 701 | static struct snd_soc_codec_driver adau1701_codec_drv = { |
| 702 | .probe = adau1701_probe, |
| 703 | .set_bias_level = adau1701_set_bias_level, |
Axel Lin | eb3032f | 2012-01-27 18:02:09 +0800 | [diff] [blame] | 704 | .idle_bias_off = true, |
Lars-Peter Clausen | 631ed8a | 2011-06-13 15:26:20 +0200 | [diff] [blame] | 705 | |
Lars-Peter Clausen | 631ed8a | 2011-06-13 15:26:20 +0200 | [diff] [blame] | 706 | .controls = adau1701_controls, |
| 707 | .num_controls = ARRAY_SIZE(adau1701_controls), |
| 708 | .dapm_widgets = adau1701_dapm_widgets, |
| 709 | .num_dapm_widgets = ARRAY_SIZE(adau1701_dapm_widgets), |
| 710 | .dapm_routes = adau1701_dapm_routes, |
| 711 | .num_dapm_routes = ARRAY_SIZE(adau1701_dapm_routes), |
| 712 | |
Lars-Peter Clausen | 631ed8a | 2011-06-13 15:26:20 +0200 | [diff] [blame] | 713 | .set_sysclk = adau1701_set_sysclk, |
| 714 | }; |
| 715 | |
Daniel Mack | 45405d5 | 2013-06-24 16:31:31 +0200 | [diff] [blame] | 716 | static const struct regmap_config adau1701_regmap = { |
| 717 | .reg_bits = 16, |
| 718 | .val_bits = 32, |
| 719 | .max_register = ADAU1701_MAX_REGISTER, |
| 720 | .cache_type = REGCACHE_RBTREE, |
| 721 | .volatile_reg = adau1701_volatile_reg, |
| 722 | .reg_write = adau1701_reg_write, |
| 723 | .reg_read = adau1701_reg_read, |
| 724 | }; |
| 725 | |
Bill Pemberton | 7a79e94 | 2012-12-07 09:26:37 -0500 | [diff] [blame] | 726 | static int adau1701_i2c_probe(struct i2c_client *client, |
| 727 | const struct i2c_device_id *id) |
Lars-Peter Clausen | 631ed8a | 2011-06-13 15:26:20 +0200 | [diff] [blame] | 728 | { |
| 729 | struct adau1701 *adau1701; |
Daniel Mack | 04561ea | 2013-05-23 15:46:05 +0200 | [diff] [blame] | 730 | struct device *dev = &client->dev; |
| 731 | int gpio_nreset = -EINVAL; |
Daniel Mack | 2352d4b | 2013-06-24 16:31:30 +0200 | [diff] [blame] | 732 | int gpio_pll_mode[2] = { -EINVAL, -EINVAL }; |
Lars-Peter Clausen | 631ed8a | 2011-06-13 15:26:20 +0200 | [diff] [blame] | 733 | int ret; |
| 734 | |
Daniel Mack | 04561ea | 2013-05-23 15:46:05 +0200 | [diff] [blame] | 735 | adau1701 = devm_kzalloc(dev, sizeof(*adau1701), GFP_KERNEL); |
Lars-Peter Clausen | 631ed8a | 2011-06-13 15:26:20 +0200 | [diff] [blame] | 736 | if (!adau1701) |
| 737 | return -ENOMEM; |
| 738 | |
Lars-Peter Clausen | a3a1ec6 | 2014-11-19 18:29:07 +0100 | [diff] [blame] | 739 | adau1701->client = client; |
Daniel Mack | 45405d5 | 2013-06-24 16:31:31 +0200 | [diff] [blame] | 740 | adau1701->regmap = devm_regmap_init(dev, NULL, client, |
| 741 | &adau1701_regmap); |
| 742 | if (IS_ERR(adau1701->regmap)) |
| 743 | return PTR_ERR(adau1701->regmap); |
| 744 | |
Daniel Mack | 04561ea | 2013-05-23 15:46:05 +0200 | [diff] [blame] | 745 | if (dev->of_node) { |
| 746 | gpio_nreset = of_get_named_gpio(dev->of_node, "reset-gpio", 0); |
| 747 | if (gpio_nreset < 0 && gpio_nreset != -ENOENT) |
| 748 | return gpio_nreset; |
Daniel Mack | 2352d4b | 2013-06-24 16:31:30 +0200 | [diff] [blame] | 749 | |
| 750 | gpio_pll_mode[0] = of_get_named_gpio(dev->of_node, |
| 751 | "adi,pll-mode-gpios", 0); |
| 752 | if (gpio_pll_mode[0] < 0 && gpio_pll_mode[0] != -ENOENT) |
| 753 | return gpio_pll_mode[0]; |
| 754 | |
| 755 | gpio_pll_mode[1] = of_get_named_gpio(dev->of_node, |
| 756 | "adi,pll-mode-gpios", 1); |
| 757 | if (gpio_pll_mode[1] < 0 && gpio_pll_mode[1] != -ENOENT) |
| 758 | return gpio_pll_mode[1]; |
Daniel Mack | 97d0a86 | 2013-06-24 16:31:32 +0200 | [diff] [blame] | 759 | |
| 760 | of_property_read_u32(dev->of_node, "adi,pll-clkdiv", |
| 761 | &adau1701->pll_clkdiv); |
| 762 | |
| 763 | of_property_read_u8_array(dev->of_node, "adi,pin-config", |
| 764 | adau1701->pin_config, |
| 765 | ARRAY_SIZE(adau1701->pin_config)); |
Daniel Mack | 04561ea | 2013-05-23 15:46:05 +0200 | [diff] [blame] | 766 | } |
| 767 | |
| 768 | if (gpio_is_valid(gpio_nreset)) { |
| 769 | ret = devm_gpio_request_one(dev, gpio_nreset, GPIOF_OUT_INIT_LOW, |
| 770 | "ADAU1701 Reset"); |
| 771 | if (ret < 0) |
| 772 | return ret; |
Daniel Mack | 04561ea | 2013-05-23 15:46:05 +0200 | [diff] [blame] | 773 | } |
| 774 | |
Daniel Mack | 2352d4b | 2013-06-24 16:31:30 +0200 | [diff] [blame] | 775 | if (gpio_is_valid(gpio_pll_mode[0]) && |
| 776 | gpio_is_valid(gpio_pll_mode[1])) { |
| 777 | ret = devm_gpio_request_one(dev, gpio_pll_mode[0], |
| 778 | GPIOF_OUT_INIT_LOW, |
| 779 | "ADAU1701 PLL mode 0"); |
| 780 | if (ret < 0) |
| 781 | return ret; |
| 782 | |
| 783 | ret = devm_gpio_request_one(dev, gpio_pll_mode[1], |
| 784 | GPIOF_OUT_INIT_LOW, |
| 785 | "ADAU1701 PLL mode 1"); |
| 786 | if (ret < 0) |
| 787 | return ret; |
| 788 | } |
| 789 | |
Daniel Mack | f724ba3 | 2013-06-07 13:53:04 +0200 | [diff] [blame] | 790 | adau1701->gpio_nreset = gpio_nreset; |
Daniel Mack | 2352d4b | 2013-06-24 16:31:30 +0200 | [diff] [blame] | 791 | adau1701->gpio_pll_mode[0] = gpio_pll_mode[0]; |
| 792 | adau1701->gpio_pll_mode[1] = gpio_pll_mode[1]; |
Daniel Mack | f724ba3 | 2013-06-07 13:53:04 +0200 | [diff] [blame] | 793 | |
Lars-Peter Clausen | 631ed8a | 2011-06-13 15:26:20 +0200 | [diff] [blame] | 794 | i2c_set_clientdata(client, adau1701); |
Lars-Peter Clausen | d48b088 | 2014-11-19 18:29:05 +0100 | [diff] [blame] | 795 | |
Lars-Peter Clausen | a3a1ec6 | 2014-11-19 18:29:07 +0100 | [diff] [blame] | 796 | adau1701->sigmadsp = devm_sigmadsp_init_i2c(client, |
| 797 | &adau1701_sigmadsp_ops, ADAU1701_FIRMWARE); |
Lars-Peter Clausen | d48b088 | 2014-11-19 18:29:05 +0100 | [diff] [blame] | 798 | if (IS_ERR(adau1701->sigmadsp)) |
| 799 | return PTR_ERR(adau1701->sigmadsp); |
| 800 | |
Lars-Peter Clausen | 631ed8a | 2011-06-13 15:26:20 +0200 | [diff] [blame] | 801 | ret = snd_soc_register_codec(&client->dev, &adau1701_codec_drv, |
| 802 | &adau1701_dai, 1); |
Lars-Peter Clausen | 631ed8a | 2011-06-13 15:26:20 +0200 | [diff] [blame] | 803 | return ret; |
| 804 | } |
| 805 | |
Bill Pemberton | 7a79e94 | 2012-12-07 09:26:37 -0500 | [diff] [blame] | 806 | static int adau1701_i2c_remove(struct i2c_client *client) |
Lars-Peter Clausen | 631ed8a | 2011-06-13 15:26:20 +0200 | [diff] [blame] | 807 | { |
| 808 | snd_soc_unregister_codec(&client->dev); |
Lars-Peter Clausen | 631ed8a | 2011-06-13 15:26:20 +0200 | [diff] [blame] | 809 | return 0; |
| 810 | } |
| 811 | |
| 812 | static const struct i2c_device_id adau1701_i2c_id[] = { |
Lars-Peter Clausen | 96b9bc6 | 2013-07-22 18:49:53 +0200 | [diff] [blame] | 813 | { "adau1401", 0 }, |
| 814 | { "adau1401a", 0 }, |
Lars-Peter Clausen | 631ed8a | 2011-06-13 15:26:20 +0200 | [diff] [blame] | 815 | { "adau1701", 0 }, |
Lars-Peter Clausen | 96b9bc6 | 2013-07-22 18:49:53 +0200 | [diff] [blame] | 816 | { "adau1702", 0 }, |
Lars-Peter Clausen | 631ed8a | 2011-06-13 15:26:20 +0200 | [diff] [blame] | 817 | { } |
| 818 | }; |
| 819 | MODULE_DEVICE_TABLE(i2c, adau1701_i2c_id); |
| 820 | |
| 821 | static struct i2c_driver adau1701_i2c_driver = { |
| 822 | .driver = { |
| 823 | .name = "adau1701", |
| 824 | .owner = THIS_MODULE, |
Daniel Mack | 04561ea | 2013-05-23 15:46:05 +0200 | [diff] [blame] | 825 | .of_match_table = of_match_ptr(adau1701_dt_ids), |
Lars-Peter Clausen | 631ed8a | 2011-06-13 15:26:20 +0200 | [diff] [blame] | 826 | }, |
| 827 | .probe = adau1701_i2c_probe, |
Bill Pemberton | 7a79e94 | 2012-12-07 09:26:37 -0500 | [diff] [blame] | 828 | .remove = adau1701_i2c_remove, |
Lars-Peter Clausen | 631ed8a | 2011-06-13 15:26:20 +0200 | [diff] [blame] | 829 | .id_table = adau1701_i2c_id, |
| 830 | }; |
| 831 | |
Sachin Kamat | beb22de | 2012-08-06 17:25:45 +0530 | [diff] [blame] | 832 | module_i2c_driver(adau1701_i2c_driver); |
Lars-Peter Clausen | 631ed8a | 2011-06-13 15:26:20 +0200 | [diff] [blame] | 833 | |
| 834 | MODULE_DESCRIPTION("ASoC ADAU1701 SigmaDSP driver"); |
| 835 | MODULE_AUTHOR("Cliff Cai <cliff.cai@analog.com>"); |
| 836 | MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>"); |
| 837 | MODULE_LICENSE("GPL"); |