aboutsummaryrefslogtreecommitdiffstats
path: root/sound/soc/codecs/adau1701.c
diff options
context:
space:
mode:
Diffstat (limited to 'sound/soc/codecs/adau1701.c')
-rw-r--r--sound/soc/codecs/adau1701.c302
1 files changed, 261 insertions, 41 deletions
diff --git a/sound/soc/codecs/adau1701.c b/sound/soc/codecs/adau1701.c
index dafdbe87edeb..0e250f118c0e 100644
--- a/sound/soc/codecs/adau1701.c
+++ b/sound/soc/codecs/adau1701.c
@@ -13,6 +13,10 @@
13#include <linux/i2c.h> 13#include <linux/i2c.h>
14#include <linux/delay.h> 14#include <linux/delay.h>
15#include <linux/slab.h> 15#include <linux/slab.h>
16#include <linux/of.h>
17#include <linux/of_gpio.h>
18#include <linux/of_device.h>
19#include <linux/regmap.h>
16#include <sound/core.h> 20#include <sound/core.h>
17#include <sound/pcm.h> 21#include <sound/pcm.h>
18#include <sound/pcm_params.h> 22#include <sound/pcm_params.h>
@@ -21,16 +25,19 @@
21#include "sigmadsp.h" 25#include "sigmadsp.h"
22#include "adau1701.h" 26#include "adau1701.h"
23 27
24#define ADAU1701_DSPCTRL 0x1c 28#define ADAU1701_DSPCTRL 0x081c
25#define ADAU1701_SEROCTL 0x1e 29#define ADAU1701_SEROCTL 0x081e
26#define ADAU1701_SERICTL 0x1f 30#define ADAU1701_SERICTL 0x081f
27 31
28#define ADAU1701_AUXNPOW 0x22 32#define ADAU1701_AUXNPOW 0x0822
33#define ADAU1701_PINCONF_0 0x0820
34#define ADAU1701_PINCONF_1 0x0821
35#define ADAU1701_AUXNPOW 0x0822
29 36
30#define ADAU1701_OSCIPOW 0x26 37#define ADAU1701_OSCIPOW 0x0826
31#define ADAU1701_DACSET 0x27 38#define ADAU1701_DACSET 0x0827
32 39
33#define ADAU1701_NUM_REGS 0x28 40#define ADAU1701_MAX_REGISTER 0x0828
34 41
35#define ADAU1701_DSPCTRL_CR (1 << 2) 42#define ADAU1701_DSPCTRL_CR (1 << 2)
36#define ADAU1701_DSPCTRL_DAM (1 << 3) 43#define ADAU1701_DSPCTRL_DAM (1 << 3)
@@ -84,10 +91,18 @@
84#define ADAU1701_OSCIPOW_OPD 0x04 91#define ADAU1701_OSCIPOW_OPD 0x04
85#define ADAU1701_DACSET_DACINIT 1 92#define ADAU1701_DACSET_DACINIT 1
86 93
94#define ADAU1707_CLKDIV_UNSET (-1UL)
95
87#define ADAU1701_FIRMWARE "adau1701.bin" 96#define ADAU1701_FIRMWARE "adau1701.bin"
88 97
89struct adau1701 { 98struct adau1701 {
99 int gpio_nreset;
100 int gpio_pll_mode[2];
90 unsigned int dai_fmt; 101 unsigned int dai_fmt;
102 unsigned int pll_clkdiv;
103 unsigned int sysclk;
104 struct regmap *regmap;
105 u8 pin_config[12];
91}; 106};
92 107
93static const struct snd_kcontrol_new adau1701_controls[] = { 108static const struct snd_kcontrol_new adau1701_controls[] = {
@@ -119,10 +134,13 @@ static const struct snd_soc_dapm_route adau1701_dapm_routes[] = {
119 { "ADC", NULL, "IN1" }, 134 { "ADC", NULL, "IN1" },
120}; 135};
121 136
122static unsigned int adau1701_register_size(struct snd_soc_codec *codec, 137static unsigned int adau1701_register_size(struct device *dev,
123 unsigned int reg) 138 unsigned int reg)
124{ 139{
125 switch (reg) { 140 switch (reg) {
141 case ADAU1701_PINCONF_0:
142 case ADAU1701_PINCONF_1:
143 return 3;
126 case ADAU1701_DSPCTRL: 144 case ADAU1701_DSPCTRL:
127 case ADAU1701_SEROCTL: 145 case ADAU1701_SEROCTL:
128 case ADAU1701_AUXNPOW: 146 case ADAU1701_AUXNPOW:
@@ -133,33 +151,42 @@ static unsigned int adau1701_register_size(struct snd_soc_codec *codec,
133 return 1; 151 return 1;
134 } 152 }
135 153
136 dev_err(codec->dev, "Unsupported register address: %d\n", reg); 154 dev_err(dev, "Unsupported register address: %d\n", reg);
137 return 0; 155 return 0;
138} 156}
139 157
140static int adau1701_write(struct snd_soc_codec *codec, unsigned int reg, 158static bool adau1701_volatile_reg(struct device *dev, unsigned int reg)
141 unsigned int value)
142{ 159{
160 switch (reg) {
161 case ADAU1701_DACSET:
162 return true;
163 default:
164 return false;
165 }
166}
167
168static int adau1701_reg_write(void *context, unsigned int reg,
169 unsigned int value)
170{
171 struct i2c_client *client = context;
143 unsigned int i; 172 unsigned int i;
144 unsigned int size; 173 unsigned int size;
145 uint8_t buf[4]; 174 uint8_t buf[5];
146 int ret; 175 int ret;
147 176
148 size = adau1701_register_size(codec, reg); 177 size = adau1701_register_size(&client->dev, reg);
149 if (size == 0) 178 if (size == 0)
150 return -EINVAL; 179 return -EINVAL;
151 180
152 snd_soc_cache_write(codec, reg, value); 181 buf[0] = reg >> 8;
153 182 buf[1] = reg & 0xff;
154 buf[0] = 0x08;
155 buf[1] = reg;
156 183
157 for (i = size + 1; i >= 2; --i) { 184 for (i = size + 1; i >= 2; --i) {
158 buf[i] = value; 185 buf[i] = value;
159 value >>= 8; 186 value >>= 8;
160 } 187 }
161 188
162 ret = i2c_master_send(to_i2c_client(codec->dev), buf, size + 2); 189 ret = i2c_master_send(client, buf, size + 2);
163 if (ret == size + 2) 190 if (ret == size + 2)
164 return 0; 191 return 0;
165 else if (ret < 0) 192 else if (ret < 0)
@@ -168,21 +195,107 @@ static int adau1701_write(struct snd_soc_codec *codec, unsigned int reg,
168 return -EIO; 195 return -EIO;
169} 196}
170 197
171static unsigned int adau1701_read(struct snd_soc_codec *codec, unsigned int reg) 198static int adau1701_reg_read(void *context, unsigned int reg,
199 unsigned int *value)
172{ 200{
173 unsigned int value; 201 int ret;
174 unsigned int ret; 202 unsigned int i;
203 unsigned int size;
204 uint8_t send_buf[2], recv_buf[3];
205 struct i2c_client *client = context;
206 struct i2c_msg msgs[2];
207
208 size = adau1701_register_size(&client->dev, reg);
209 if (size == 0)
210 return -EINVAL;
211
212 send_buf[0] = reg >> 8;
213 send_buf[1] = reg & 0xff;
214
215 msgs[0].addr = client->addr;
216 msgs[0].len = sizeof(send_buf);
217 msgs[0].buf = send_buf;
218 msgs[0].flags = 0;
219
220 msgs[1].addr = client->addr;
221 msgs[1].len = size;
222 msgs[1].buf = recv_buf;
223 msgs[1].flags = I2C_M_RD;
175 224
176 ret = snd_soc_cache_read(codec, reg, &value); 225 ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
177 if (ret) 226 if (ret < 0)
178 return ret; 227 return ret;
228 else if (ret != ARRAY_SIZE(msgs))
229 return -EIO;
230
231 *value = 0;
232
233 for (i = 0; i < size; i++)
234 *value |= recv_buf[i] << (i * 8);
179 235
180 return value; 236 return 0;
181} 237}
182 238
183static int adau1701_load_firmware(struct snd_soc_codec *codec) 239static int adau1701_reset(struct snd_soc_codec *codec, unsigned int clkdiv)
184{ 240{
185 return process_sigma_firmware(codec->control_data, ADAU1701_FIRMWARE); 241 struct adau1701 *adau1701 = snd_soc_codec_get_drvdata(codec);
242 struct i2c_client *client = to_i2c_client(codec->dev);
243 int ret;
244
245 if (clkdiv != ADAU1707_CLKDIV_UNSET &&
246 gpio_is_valid(adau1701->gpio_pll_mode[0]) &&
247 gpio_is_valid(adau1701->gpio_pll_mode[1])) {
248 switch (clkdiv) {
249 case 64:
250 gpio_set_value(adau1701->gpio_pll_mode[0], 0);
251 gpio_set_value(adau1701->gpio_pll_mode[1], 0);
252 break;
253 case 256:
254 gpio_set_value(adau1701->gpio_pll_mode[0], 0);
255 gpio_set_value(adau1701->gpio_pll_mode[1], 1);
256 break;
257 case 384:
258 gpio_set_value(adau1701->gpio_pll_mode[0], 1);
259 gpio_set_value(adau1701->gpio_pll_mode[1], 0);
260 break;
261 case 0: /* fallback */
262 case 512:
263 gpio_set_value(adau1701->gpio_pll_mode[0], 1);
264 gpio_set_value(adau1701->gpio_pll_mode[1], 1);
265 break;
266 }
267 }
268
269 adau1701->pll_clkdiv = clkdiv;
270
271 if (gpio_is_valid(adau1701->gpio_nreset)) {
272 gpio_set_value(adau1701->gpio_nreset, 0);
273 /* minimum reset time is 20ns */
274 udelay(1);
275 gpio_set_value(adau1701->gpio_nreset, 1);
276 /* power-up time may be as long as 85ms */
277 mdelay(85);
278 }
279
280 /*
281 * Postpone the firmware download to a point in time when we
282 * know the correct PLL setup
283 */
284 if (clkdiv != ADAU1707_CLKDIV_UNSET) {
285 ret = process_sigma_firmware(client, ADAU1701_FIRMWARE);
286 if (ret) {
287 dev_warn(codec->dev, "Failed to load firmware\n");
288 return ret;
289 }
290 }
291
292 regmap_write(adau1701->regmap, ADAU1701_DACSET, ADAU1701_DACSET_DACINIT);
293 regmap_write(adau1701->regmap, ADAU1701_DSPCTRL, ADAU1701_DSPCTRL_CR);
294
295 regcache_mark_dirty(adau1701->regmap);
296 regcache_sync(adau1701->regmap);
297
298 return 0;
186} 299}
187 300
188static int adau1701_set_capture_pcm_format(struct snd_soc_codec *codec, 301static int adau1701_set_capture_pcm_format(struct snd_soc_codec *codec,
@@ -259,8 +372,22 @@ static int adau1701_hw_params(struct snd_pcm_substream *substream,
259 struct snd_pcm_hw_params *params, struct snd_soc_dai *dai) 372 struct snd_pcm_hw_params *params, struct snd_soc_dai *dai)
260{ 373{
261 struct snd_soc_codec *codec = dai->codec; 374 struct snd_soc_codec *codec = dai->codec;
375 struct adau1701 *adau1701 = snd_soc_codec_get_drvdata(codec);
376 unsigned int clkdiv = adau1701->sysclk / params_rate(params);
262 snd_pcm_format_t format; 377 snd_pcm_format_t format;
263 unsigned int val; 378 unsigned int val;
379 int ret;
380
381 /*
382 * If the mclk/lrclk ratio changes, the chip needs updated PLL
383 * mode GPIO settings, and a full reset cycle, including a new
384 * firmware upload.
385 */
386 if (clkdiv != adau1701->pll_clkdiv) {
387 ret = adau1701_reset(codec, clkdiv);
388 if (ret < 0)
389 return ret;
390 }
264 391
265 switch (params_rate(params)) { 392 switch (params_rate(params)) {
266 case 192000: 393 case 192000:
@@ -352,8 +479,8 @@ static int adau1701_set_dai_fmt(struct snd_soc_dai *codec_dai,
352 479
353 adau1701->dai_fmt = fmt & SND_SOC_DAIFMT_FORMAT_MASK; 480 adau1701->dai_fmt = fmt & SND_SOC_DAIFMT_FORMAT_MASK;
354 481
355 snd_soc_write(codec, ADAU1701_SERICTL, serictl); 482 regmap_write(adau1701->regmap, ADAU1701_SERICTL, serictl);
356 snd_soc_update_bits(codec, ADAU1701_SEROCTL, 483 regmap_update_bits(adau1701->regmap, ADAU1701_SEROCTL,
357 ~ADAU1701_SEROCTL_WORD_LEN_MASK, seroctl); 484 ~ADAU1701_SEROCTL_WORD_LEN_MASK, seroctl);
358 485
359 return 0; 486 return 0;
@@ -403,6 +530,7 @@ static int adau1701_set_sysclk(struct snd_soc_codec *codec, int clk_id,
403 int source, unsigned int freq, int dir) 530 int source, unsigned int freq, int dir)
404{ 531{
405 unsigned int val; 532 unsigned int val;
533 struct adau1701 *adau1701 = snd_soc_codec_get_drvdata(codec);
406 534
407 switch (clk_id) { 535 switch (clk_id) {
408 case ADAU1701_CLK_SRC_OSC: 536 case ADAU1701_CLK_SRC_OSC:
@@ -416,6 +544,7 @@ static int adau1701_set_sysclk(struct snd_soc_codec *codec, int clk_id,
416 } 544 }
417 545
418 snd_soc_update_bits(codec, ADAU1701_OSCIPOW, ADAU1701_OSCIPOW_OPD, val); 546 snd_soc_update_bits(codec, ADAU1701_OSCIPOW, ADAU1701_OSCIPOW_OPD, val);
547 adau1701->sysclk = freq;
419 548
420 return 0; 549 return 0;
421} 550}
@@ -452,18 +581,47 @@ static struct snd_soc_dai_driver adau1701_dai = {
452 .symmetric_rates = 1, 581 .symmetric_rates = 1,
453}; 582};
454 583
584#ifdef CONFIG_OF
585static const struct of_device_id adau1701_dt_ids[] = {
586 { .compatible = "adi,adau1701", },
587 { }
588};
589MODULE_DEVICE_TABLE(of, adau1701_dt_ids);
590#endif
591
455static int adau1701_probe(struct snd_soc_codec *codec) 592static int adau1701_probe(struct snd_soc_codec *codec)
456{ 593{
457 int ret; 594 int i, ret;
595 unsigned int val;
596 struct adau1701 *adau1701 = snd_soc_codec_get_drvdata(codec);
458 597
459 codec->control_data = to_i2c_client(codec->dev); 598 codec->control_data = to_i2c_client(codec->dev);
460 599
461 ret = adau1701_load_firmware(codec); 600 /*
462 if (ret) 601 * Let the pll_clkdiv variable default to something that won't happen
463 dev_warn(codec->dev, "Failed to load firmware\n"); 602 * at runtime. That way, we can postpone the firmware download from
603 * adau1701_reset() to a point in time when we know the correct PLL
604 * mode parameters.
605 */
606 adau1701->pll_clkdiv = ADAU1707_CLKDIV_UNSET;
607
608 /* initalize with pre-configured pll mode settings */
609 ret = adau1701_reset(codec, adau1701->pll_clkdiv);
610 if (ret < 0)
611 return ret;
612
613 /* set up pin config */
614 val = 0;
615 for (i = 0; i < 6; i++)
616 val |= adau1701->pin_config[i] << (i * 4);
464 617
465 snd_soc_write(codec, ADAU1701_DACSET, ADAU1701_DACSET_DACINIT); 618 regmap_write(adau1701->regmap, ADAU1701_PINCONF_0, val);
466 snd_soc_write(codec, ADAU1701_DSPCTRL, ADAU1701_DSPCTRL_CR); 619
620 val = 0;
621 for (i = 0; i < 6; i++)
622 val |= adau1701->pin_config[i + 6] << (i * 4);
623
624 regmap_write(adau1701->regmap, ADAU1701_PINCONF_1, val);
467 625
468 return 0; 626 return 0;
469} 627}
@@ -473,9 +631,6 @@ static struct snd_soc_codec_driver adau1701_codec_drv = {
473 .set_bias_level = adau1701_set_bias_level, 631 .set_bias_level = adau1701_set_bias_level,
474 .idle_bias_off = true, 632 .idle_bias_off = true,
475 633
476 .reg_cache_size = ADAU1701_NUM_REGS,
477 .reg_word_size = sizeof(u16),
478
479 .controls = adau1701_controls, 634 .controls = adau1701_controls,
480 .num_controls = ARRAY_SIZE(adau1701_controls), 635 .num_controls = ARRAY_SIZE(adau1701_controls),
481 .dapm_widgets = adau1701_dapm_widgets, 636 .dapm_widgets = adau1701_dapm_widgets,
@@ -483,22 +638,86 @@ static struct snd_soc_codec_driver adau1701_codec_drv = {
483 .dapm_routes = adau1701_dapm_routes, 638 .dapm_routes = adau1701_dapm_routes,
484 .num_dapm_routes = ARRAY_SIZE(adau1701_dapm_routes), 639 .num_dapm_routes = ARRAY_SIZE(adau1701_dapm_routes),
485 640
486 .write = adau1701_write,
487 .read = adau1701_read,
488
489 .set_sysclk = adau1701_set_sysclk, 641 .set_sysclk = adau1701_set_sysclk,
490}; 642};
491 643
644static const struct regmap_config adau1701_regmap = {
645 .reg_bits = 16,
646 .val_bits = 32,
647 .max_register = ADAU1701_MAX_REGISTER,
648 .cache_type = REGCACHE_RBTREE,
649 .volatile_reg = adau1701_volatile_reg,
650 .reg_write = adau1701_reg_write,
651 .reg_read = adau1701_reg_read,
652};
653
492static int adau1701_i2c_probe(struct i2c_client *client, 654static int adau1701_i2c_probe(struct i2c_client *client,
493 const struct i2c_device_id *id) 655 const struct i2c_device_id *id)
494{ 656{
495 struct adau1701 *adau1701; 657 struct adau1701 *adau1701;
658 struct device *dev = &client->dev;
659 int gpio_nreset = -EINVAL;
660 int gpio_pll_mode[2] = { -EINVAL, -EINVAL };
496 int ret; 661 int ret;
497 662
498 adau1701 = devm_kzalloc(&client->dev, sizeof(*adau1701), GFP_KERNEL); 663 adau1701 = devm_kzalloc(dev, sizeof(*adau1701), GFP_KERNEL);
499 if (!adau1701) 664 if (!adau1701)
500 return -ENOMEM; 665 return -ENOMEM;
501 666
667 adau1701->regmap = devm_regmap_init(dev, NULL, client,
668 &adau1701_regmap);
669 if (IS_ERR(adau1701->regmap))
670 return PTR_ERR(adau1701->regmap);
671
672 if (dev->of_node) {
673 gpio_nreset = of_get_named_gpio(dev->of_node, "reset-gpio", 0);
674 if (gpio_nreset < 0 && gpio_nreset != -ENOENT)
675 return gpio_nreset;
676
677 gpio_pll_mode[0] = of_get_named_gpio(dev->of_node,
678 "adi,pll-mode-gpios", 0);
679 if (gpio_pll_mode[0] < 0 && gpio_pll_mode[0] != -ENOENT)
680 return gpio_pll_mode[0];
681
682 gpio_pll_mode[1] = of_get_named_gpio(dev->of_node,
683 "adi,pll-mode-gpios", 1);
684 if (gpio_pll_mode[1] < 0 && gpio_pll_mode[1] != -ENOENT)
685 return gpio_pll_mode[1];
686
687 of_property_read_u32(dev->of_node, "adi,pll-clkdiv",
688 &adau1701->pll_clkdiv);
689
690 of_property_read_u8_array(dev->of_node, "adi,pin-config",
691 adau1701->pin_config,
692 ARRAY_SIZE(adau1701->pin_config));
693 }
694
695 if (gpio_is_valid(gpio_nreset)) {
696 ret = devm_gpio_request_one(dev, gpio_nreset, GPIOF_OUT_INIT_LOW,
697 "ADAU1701 Reset");
698 if (ret < 0)
699 return ret;
700 }
701
702 if (gpio_is_valid(gpio_pll_mode[0]) &&
703 gpio_is_valid(gpio_pll_mode[1])) {
704 ret = devm_gpio_request_one(dev, gpio_pll_mode[0],
705 GPIOF_OUT_INIT_LOW,
706 "ADAU1701 PLL mode 0");
707 if (ret < 0)
708 return ret;
709
710 ret = devm_gpio_request_one(dev, gpio_pll_mode[1],
711 GPIOF_OUT_INIT_LOW,
712 "ADAU1701 PLL mode 1");
713 if (ret < 0)
714 return ret;
715 }
716
717 adau1701->gpio_nreset = gpio_nreset;
718 adau1701->gpio_pll_mode[0] = gpio_pll_mode[0];
719 adau1701->gpio_pll_mode[1] = gpio_pll_mode[1];
720
502 i2c_set_clientdata(client, adau1701); 721 i2c_set_clientdata(client, adau1701);
503 ret = snd_soc_register_codec(&client->dev, &adau1701_codec_drv, 722 ret = snd_soc_register_codec(&client->dev, &adau1701_codec_drv,
504 &adau1701_dai, 1); 723 &adau1701_dai, 1);
@@ -521,6 +740,7 @@ static struct i2c_driver adau1701_i2c_driver = {
521 .driver = { 740 .driver = {
522 .name = "adau1701", 741 .name = "adau1701",
523 .owner = THIS_MODULE, 742 .owner = THIS_MODULE,
743 .of_match_table = of_match_ptr(adau1701_dt_ids),
524 }, 744 },
525 .probe = adau1701_i2c_probe, 745 .probe = adau1701_i2c_probe,
526 .remove = adau1701_i2c_remove, 746 .remove = adau1701_i2c_remove,