blob: d4e219b6b98f24636af3e4857bb297db005c10f8 [file] [log] [blame]
Lars-Peter Clausen631ed8a2011-06-13 15:26:20 +02001/*
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 Clausen631ed8a2011-06-13 15:26:20 +020015#include <linux/slab.h>
Daniel Mack04561ea2013-05-23 15:46:05 +020016#include <linux/of.h>
17#include <linux/of_gpio.h>
18#include <linux/of_device.h>
Daniel Mack45405d52013-06-24 16:31:31 +020019#include <linux/regmap.h>
Lars-Peter Clausen631ed8a2011-06-13 15:26:20 +020020#include <sound/core.h>
21#include <sound/pcm.h>
22#include <sound/pcm_params.h>
23#include <sound/soc.h>
24
Lars-Peter Clausena3a1ec62014-11-19 18:29:07 +010025#include <asm/unaligned.h>
26
Lars-Peter Clausen40216ce2011-11-28 09:44:17 +010027#include "sigmadsp.h"
Lars-Peter Clausen631ed8a2011-06-13 15:26:20 +020028#include "adau1701.h"
29
Lars-Peter Clausena3a1ec62014-11-19 18:29:07 +010030#define ADAU1701_SAFELOAD_DATA(i) (0x0810 + (i))
31#define ADAU1701_SAFELOAD_ADDR(i) (0x0815 + (i))
32
Daniel Mack45405d52013-06-24 16:31:31 +020033#define ADAU1701_DSPCTRL 0x081c
34#define ADAU1701_SEROCTL 0x081e
35#define ADAU1701_SERICTL 0x081f
Lars-Peter Clausen631ed8a2011-06-13 15:26:20 +020036
Daniel Mack45405d52013-06-24 16:31:31 +020037#define ADAU1701_AUXNPOW 0x0822
Daniel Mack97d0a862013-06-24 16:31:32 +020038#define ADAU1701_PINCONF_0 0x0820
39#define ADAU1701_PINCONF_1 0x0821
40#define ADAU1701_AUXNPOW 0x0822
Lars-Peter Clausen631ed8a2011-06-13 15:26:20 +020041
Daniel Mack45405d52013-06-24 16:31:31 +020042#define ADAU1701_OSCIPOW 0x0826
43#define ADAU1701_DACSET 0x0827
Lars-Peter Clausen631ed8a2011-06-13 15:26:20 +020044
Daniel Mack45405d52013-06-24 16:31:31 +020045#define ADAU1701_MAX_REGISTER 0x0828
Lars-Peter Clausen631ed8a2011-06-13 15:26:20 +020046
47#define ADAU1701_DSPCTRL_CR (1 << 2)
48#define ADAU1701_DSPCTRL_DAM (1 << 3)
49#define ADAU1701_DSPCTRL_ADM (1 << 4)
Lars-Peter Clausena3a1ec62014-11-19 18:29:07 +010050#define ADAU1701_DSPCTRL_IST (1 << 5)
Lars-Peter Clausen631ed8a2011-06-13 15:26:20 +020051#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 Clausene20970a2014-01-08 11:22:25 +010080#define ADAU1701_SEROCTL_WORD_LEN_16 0x0002
Lars-Peter Clausen631ed8a2011-06-13 15:26:20 +020081#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 Carpenterba51cbb2013-07-25 19:40:17 +0300100#define ADAU1707_CLKDIV_UNSET (-1U)
Daniel Mack2352d4b2013-06-24 16:31:30 +0200101
Lars-Peter Clausen631ed8a2011-06-13 15:26:20 +0200102#define ADAU1701_FIRMWARE "adau1701.bin"
103
104struct adau1701 {
Daniel Mackf724ba32013-06-07 13:53:04 +0200105 int gpio_nreset;
Daniel Mack2352d4b2013-06-24 16:31:30 +0200106 int gpio_pll_mode[2];
Lars-Peter Clausen631ed8a2011-06-13 15:26:20 +0200107 unsigned int dai_fmt;
Daniel Mack2352d4b2013-06-24 16:31:30 +0200108 unsigned int pll_clkdiv;
109 unsigned int sysclk;
Daniel Mack45405d52013-06-24 16:31:31 +0200110 struct regmap *regmap;
Lars-Peter Clausena3a1ec62014-11-19 18:29:07 +0100111 struct i2c_client *client;
Daniel Mack97d0a862013-06-24 16:31:32 +0200112 u8 pin_config[12];
Lars-Peter Clausend48b0882014-11-19 18:29:05 +0100113
114 struct sigmadsp *sigmadsp;
Lars-Peter Clausen631ed8a2011-06-13 15:26:20 +0200115};
116
117static const struct snd_kcontrol_new adau1701_controls[] = {
118 SOC_SINGLE("Master Capture Switch", ADAU1701_DSPCTRL, 4, 1, 0),
119};
120
121static 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
136static 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 Mack45405d52013-06-24 16:31:31 +0200146static unsigned int adau1701_register_size(struct device *dev,
Lars-Peter Clausen631ed8a2011-06-13 15:26:20 +0200147 unsigned int reg)
148{
149 switch (reg) {
Daniel Mack97d0a862013-06-24 16:31:32 +0200150 case ADAU1701_PINCONF_0:
151 case ADAU1701_PINCONF_1:
152 return 3;
Lars-Peter Clausen631ed8a2011-06-13 15:26:20 +0200153 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 Mack45405d52013-06-24 16:31:31 +0200163 dev_err(dev, "Unsupported register address: %d\n", reg);
Lars-Peter Clausen631ed8a2011-06-13 15:26:20 +0200164 return 0;
165}
166
Daniel Mack45405d52013-06-24 16:31:31 +0200167static bool adau1701_volatile_reg(struct device *dev, unsigned int reg)
Lars-Peter Clausen631ed8a2011-06-13 15:26:20 +0200168{
Daniel Mack45405d52013-06-24 16:31:31 +0200169 switch (reg) {
170 case ADAU1701_DACSET:
Lars-Peter Clausena3a1ec62014-11-19 18:29:07 +0100171 case ADAU1701_DSPCTRL:
Daniel Mack45405d52013-06-24 16:31:31 +0200172 return true;
173 default:
174 return false;
175 }
176}
177
178static int adau1701_reg_write(void *context, unsigned int reg,
179 unsigned int value)
180{
181 struct i2c_client *client = context;
Dan Carpenterc2097402011-06-20 10:11:25 +0300182 unsigned int i;
Lars-Peter Clausen631ed8a2011-06-13 15:26:20 +0200183 unsigned int size;
Daniel Mack97d0a862013-06-24 16:31:32 +0200184 uint8_t buf[5];
Dan Carpenterc2097402011-06-20 10:11:25 +0300185 int ret;
Lars-Peter Clausen631ed8a2011-06-13 15:26:20 +0200186
Daniel Mack45405d52013-06-24 16:31:31 +0200187 size = adau1701_register_size(&client->dev, reg);
Lars-Peter Clausen631ed8a2011-06-13 15:26:20 +0200188 if (size == 0)
189 return -EINVAL;
190
Daniel Mack45405d52013-06-24 16:31:31 +0200191 buf[0] = reg >> 8;
192 buf[1] = reg & 0xff;
Lars-Peter Clausen631ed8a2011-06-13 15:26:20 +0200193
194 for (i = size + 1; i >= 2; --i) {
195 buf[i] = value;
196 value >>= 8;
197 }
198
Daniel Mack45405d52013-06-24 16:31:31 +0200199 ret = i2c_master_send(client, buf, size + 2);
Lars-Peter Clausen631ed8a2011-06-13 15:26:20 +0200200 if (ret == size + 2)
201 return 0;
202 else if (ret < 0)
203 return ret;
204 else
205 return -EIO;
206}
207
Daniel Mack45405d52013-06-24 16:31:31 +0200208static int adau1701_reg_read(void *context, unsigned int reg,
209 unsigned int *value)
Lars-Peter Clausen631ed8a2011-06-13 15:26:20 +0200210{
Daniel Mack45405d52013-06-24 16:31:31 +0200211 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 Clausen631ed8a2011-06-13 15:26:20 +0200217
Daniel Mack45405d52013-06-24 16:31:31 +0200218 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 Clausen631ed8a2011-06-13 15:26:20 +0200237 return ret;
Daniel Mack45405d52013-06-24 16:31:31 +0200238 else if (ret != ARRAY_SIZE(msgs))
239 return -EIO;
Lars-Peter Clausen631ed8a2011-06-13 15:26:20 +0200240
Daniel Mack45405d52013-06-24 16:31:31 +0200241 *value = 0;
242
Daniel Mack3ad80b82014-07-03 16:51:36 +0200243 for (i = 0; i < size; i++) {
244 *value <<= 8;
245 *value |= recv_buf[i];
246 }
Daniel Mack45405d52013-06-24 16:31:31 +0200247
248 return 0;
Lars-Peter Clausen631ed8a2011-06-13 15:26:20 +0200249}
250
Lars-Peter Clausena3a1ec62014-11-19 18:29:07 +0100251static 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
291static const struct sigmadsp_ops adau1701_sigmadsp_ops = {
292 .safeload = adau1701_safeload,
293};
294
Lars-Peter Clausend48b0882014-11-19 18:29:05 +0100295static int adau1701_reset(struct snd_soc_codec *codec, unsigned int clkdiv,
296 unsigned int rate)
Lars-Peter Clausen631ed8a2011-06-13 15:26:20 +0200297{
Daniel Mackf724ba32013-06-07 13:53:04 +0200298 struct adau1701 *adau1701 = snd_soc_codec_get_drvdata(codec);
Daniel Mackde9fc722013-06-24 16:31:29 +0200299 int ret;
Daniel Mackf724ba32013-06-07 13:53:04 +0200300
Lars-Peter Clausend48b0882014-11-19 18:29:05 +0100301 sigmadsp_reset(adau1701->sigmadsp);
302
Daniel Mack2352d4b2013-06-24 16:31:30 +0200303 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 Brown9190aeb2013-08-11 15:07:36 +0100308 gpio_set_value_cansleep(adau1701->gpio_pll_mode[0], 0);
309 gpio_set_value_cansleep(adau1701->gpio_pll_mode[1], 0);
Daniel Mack2352d4b2013-06-24 16:31:30 +0200310 break;
311 case 256:
Mark Brown9190aeb2013-08-11 15:07:36 +0100312 gpio_set_value_cansleep(adau1701->gpio_pll_mode[0], 0);
313 gpio_set_value_cansleep(adau1701->gpio_pll_mode[1], 1);
Daniel Mack2352d4b2013-06-24 16:31:30 +0200314 break;
315 case 384:
Mark Brown9190aeb2013-08-11 15:07:36 +0100316 gpio_set_value_cansleep(adau1701->gpio_pll_mode[0], 1);
317 gpio_set_value_cansleep(adau1701->gpio_pll_mode[1], 0);
Daniel Mack2352d4b2013-06-24 16:31:30 +0200318 break;
319 case 0: /* fallback */
320 case 512:
Mark Brown9190aeb2013-08-11 15:07:36 +0100321 gpio_set_value_cansleep(adau1701->gpio_pll_mode[0], 1);
322 gpio_set_value_cansleep(adau1701->gpio_pll_mode[1], 1);
Daniel Mack2352d4b2013-06-24 16:31:30 +0200323 break;
324 }
325 }
326
327 adau1701->pll_clkdiv = clkdiv;
328
Daniel Mackde9fc722013-06-24 16:31:29 +0200329 if (gpio_is_valid(adau1701->gpio_nreset)) {
Mark Brown9190aeb2013-08-11 15:07:36 +0100330 gpio_set_value_cansleep(adau1701->gpio_nreset, 0);
Daniel Mackde9fc722013-06-24 16:31:29 +0200331 /* minimum reset time is 20ns */
332 udelay(1);
Mark Brown9190aeb2013-08-11 15:07:36 +0100333 gpio_set_value_cansleep(adau1701->gpio_nreset, 1);
Daniel Mackde9fc722013-06-24 16:31:29 +0200334 /* power-up time may be as long as 85ms */
335 mdelay(85);
336 }
Daniel Mackf724ba32013-06-07 13:53:04 +0200337
Daniel Mack2352d4b2013-06-24 16:31:30 +0200338 /*
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 Clausend48b0882014-11-19 18:29:05 +0100343 ret = sigmadsp_setup(adau1701->sigmadsp, rate);
Daniel Mack2352d4b2013-06-24 16:31:30 +0200344 if (ret) {
345 dev_warn(codec->dev, "Failed to load firmware\n");
346 return ret;
347 }
Daniel Mackf724ba32013-06-07 13:53:04 +0200348 }
349
Daniel Mack45405d52013-06-24 16:31:31 +0200350 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 Mackf724ba32013-06-07 13:53:04 +0200355
356 return 0;
Lars-Peter Clausen631ed8a2011-06-13 15:26:20 +0200357}
358
359static int adau1701_set_capture_pcm_format(struct snd_soc_codec *codec,
Mark Brown9b58e712014-01-08 18:50:25 +0000360 struct snd_pcm_hw_params *params)
Lars-Peter Clausen631ed8a2011-06-13 15:26:20 +0200361{
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 Brown9b58e712014-01-08 18:50:25 +0000366 switch (params_width(params)) {
367 case 16:
Lars-Peter Clausen631ed8a2011-06-13 15:26:20 +0200368 val = ADAU1701_SEROCTL_WORD_LEN_16;
369 break;
Mark Brown9b58e712014-01-08 18:50:25 +0000370 case 20:
Lars-Peter Clausen631ed8a2011-06-13 15:26:20 +0200371 val = ADAU1701_SEROCTL_WORD_LEN_20;
372 break;
Mark Brown9b58e712014-01-08 18:50:25 +0000373 case 24:
Lars-Peter Clausen631ed8a2011-06-13 15:26:20 +0200374 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 Brown9b58e712014-01-08 18:50:25 +0000381 switch (params_width(params)) {
382 case 16:
Lars-Peter Clausen631ed8a2011-06-13 15:26:20 +0200383 val |= ADAU1701_SEROCTL_MSB_DEALY16;
384 break;
Mark Brown9b58e712014-01-08 18:50:25 +0000385 case 20:
Lars-Peter Clausen631ed8a2011-06-13 15:26:20 +0200386 val |= ADAU1701_SEROCTL_MSB_DEALY12;
387 break;
Mark Brown9b58e712014-01-08 18:50:25 +0000388 case 24:
Lars-Peter Clausen631ed8a2011-06-13 15:26:20 +0200389 val |= ADAU1701_SEROCTL_MSB_DEALY8;
390 break;
391 }
392 mask |= ADAU1701_SEROCTL_MSB_DEALY_MASK;
393 }
394
Daniel Mackee441142013-06-27 22:00:04 +0200395 regmap_update_bits(adau1701->regmap, ADAU1701_SEROCTL, mask, val);
Lars-Peter Clausen631ed8a2011-06-13 15:26:20 +0200396
397 return 0;
398}
399
400static int adau1701_set_playback_pcm_format(struct snd_soc_codec *codec,
Mark Brown9b58e712014-01-08 18:50:25 +0000401 struct snd_pcm_hw_params *params)
Lars-Peter Clausen631ed8a2011-06-13 15:26:20 +0200402{
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 Brown9b58e712014-01-08 18:50:25 +0000409 switch (params_width(params)) {
410 case 16:
Lars-Peter Clausen631ed8a2011-06-13 15:26:20 +0200411 val = ADAU1701_SERICTL_RIGHTJ_16;
412 break;
Mark Brown9b58e712014-01-08 18:50:25 +0000413 case 20:
Lars-Peter Clausen631ed8a2011-06-13 15:26:20 +0200414 val = ADAU1701_SERICTL_RIGHTJ_20;
415 break;
Mark Brown9b58e712014-01-08 18:50:25 +0000416 case 24:
Lars-Peter Clausen631ed8a2011-06-13 15:26:20 +0200417 val = ADAU1701_SERICTL_RIGHTJ_24;
418 break;
419 default:
420 return -EINVAL;
421 }
422
Daniel Mackee441142013-06-27 22:00:04 +0200423 regmap_update_bits(adau1701->regmap, ADAU1701_SERICTL,
Lars-Peter Clausen631ed8a2011-06-13 15:26:20 +0200424 ADAU1701_SERICTL_MODE_MASK, val);
425
426 return 0;
427}
428
429static int adau1701_hw_params(struct snd_pcm_substream *substream,
430 struct snd_pcm_hw_params *params, struct snd_soc_dai *dai)
431{
Mark Browne6968a12012-04-04 15:58:16 +0100432 struct snd_soc_codec *codec = dai->codec;
Daniel Mack2352d4b2013-06-24 16:31:30 +0200433 struct adau1701 *adau1701 = snd_soc_codec_get_drvdata(codec);
434 unsigned int clkdiv = adau1701->sysclk / params_rate(params);
Lars-Peter Clausen631ed8a2011-06-13 15:26:20 +0200435 unsigned int val;
Daniel Mack2352d4b2013-06-24 16:31:30 +0200436 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 Clausend48b0882014-11-19 18:29:05 +0100444 ret = adau1701_reset(codec, clkdiv, params_rate(params));
Daniel Mack2352d4b2013-06-24 16:31:30 +0200445 if (ret < 0)
446 return ret;
447 }
Lars-Peter Clausen631ed8a2011-06-13 15:26:20 +0200448
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 Mackee441142013-06-27 22:00:04 +0200463 regmap_update_bits(adau1701->regmap, ADAU1701_DSPCTRL,
Lars-Peter Clausen631ed8a2011-06-13 15:26:20 +0200464 ADAU1701_DSPCTRL_SR_MASK, val);
465
Lars-Peter Clausen631ed8a2011-06-13 15:26:20 +0200466 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
Mark Brown9b58e712014-01-08 18:50:25 +0000467 return adau1701_set_playback_pcm_format(codec, params);
Lars-Peter Clausen631ed8a2011-06-13 15:26:20 +0200468 else
Mark Brown9b58e712014-01-08 18:50:25 +0000469 return adau1701_set_capture_pcm_format(codec, params);
Lars-Peter Clausen631ed8a2011-06-13 15:26:20 +0200470}
471
472static 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 Mack45405d52013-06-24 16:31:31 +0200538 regmap_write(adau1701->regmap, ADAU1701_SERICTL, serictl);
539 regmap_update_bits(adau1701->regmap, ADAU1701_SEROCTL,
Lars-Peter Clausen631ed8a2011-06-13 15:26:20 +0200540 ~ADAU1701_SEROCTL_WORD_LEN_MASK, seroctl);
541
542 return 0;
543}
544
545static 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 Mackee441142013-06-27 22:00:04 +0200549 struct adau1701 *adau1701 = snd_soc_codec_get_drvdata(codec);
Lars-Peter Clausen631ed8a2011-06-13 15:26:20 +0200550
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 Mackee441142013-06-27 22:00:04 +0200558 regmap_update_bits(adau1701->regmap,
559 ADAU1701_AUXNPOW, mask, 0x00);
Lars-Peter Clausen631ed8a2011-06-13 15:26:20 +0200560 break;
561 case SND_SOC_BIAS_OFF:
562 /* Disable VREF and VREF buffer */
Daniel Mackee441142013-06-27 22:00:04 +0200563 regmap_update_bits(adau1701->regmap,
564 ADAU1701_AUXNPOW, mask, mask);
Lars-Peter Clausen631ed8a2011-06-13 15:26:20 +0200565 break;
566 }
567
568 codec->dapm.bias_level = level;
569 return 0;
570}
571
572static 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 Mackee441142013-06-27 22:00:04 +0200576 struct adau1701 *adau1701 = snd_soc_codec_get_drvdata(codec);
Lars-Peter Clausen631ed8a2011-06-13 15:26:20 +0200577 unsigned int val;
578
579 if (mute)
580 val = 0;
581 else
582 val = mask;
583
Daniel Mackee441142013-06-27 22:00:04 +0200584 regmap_update_bits(adau1701->regmap, ADAU1701_DSPCTRL, mask, val);
Lars-Peter Clausen631ed8a2011-06-13 15:26:20 +0200585
586 return 0;
587}
588
589static int adau1701_set_sysclk(struct snd_soc_codec *codec, int clk_id,
Axel Lin21326db2011-09-28 13:48:35 +0800590 int source, unsigned int freq, int dir)
Lars-Peter Clausen631ed8a2011-06-13 15:26:20 +0200591{
592 unsigned int val;
Daniel Mack2352d4b2013-06-24 16:31:30 +0200593 struct adau1701 *adau1701 = snd_soc_codec_get_drvdata(codec);
Lars-Peter Clausen631ed8a2011-06-13 15:26:20 +0200594
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 Mackee441142013-06-27 22:00:04 +0200606 regmap_update_bits(adau1701->regmap, ADAU1701_OSCIPOW,
607 ADAU1701_OSCIPOW_OPD, val);
Daniel Mack2352d4b2013-06-24 16:31:30 +0200608 adau1701->sysclk = freq;
Lars-Peter Clausen631ed8a2011-06-13 15:26:20 +0200609
610 return 0;
611}
612
Lars-Peter Clausend48b0882014-11-19 18:29:05 +0100613static 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 Clausen631ed8a2011-06-13 15:26:20 +0200621#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 Clausen890754a2011-11-23 14:11:21 +0100627static const struct snd_soc_dai_ops adau1701_dai_ops = {
Lars-Peter Clausen631ed8a2011-06-13 15:26:20 +0200628 .set_fmt = adau1701_set_dai_fmt,
629 .hw_params = adau1701_hw_params,
630 .digital_mute = adau1701_digital_mute,
Lars-Peter Clausend48b0882014-11-19 18:29:05 +0100631 .startup = adau1701_startup,
Lars-Peter Clausen631ed8a2011-06-13 15:26:20 +0200632};
633
634static 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 Mack04561ea2013-05-23 15:46:05 +0200654#ifdef CONFIG_OF
655static const struct of_device_id adau1701_dt_ids[] = {
656 { .compatible = "adi,adau1701", },
657 { }
658};
659MODULE_DEVICE_TABLE(of, adau1701_dt_ids);
660#endif
661
Lars-Peter Clausen631ed8a2011-06-13 15:26:20 +0200662static int adau1701_probe(struct snd_soc_codec *codec)
663{
Daniel Mack97d0a862013-06-24 16:31:32 +0200664 int i, ret;
665 unsigned int val;
Daniel Mack2352d4b2013-06-24 16:31:30 +0200666 struct adau1701 *adau1701 = snd_soc_codec_get_drvdata(codec);
Lars-Peter Clausen631ed8a2011-06-13 15:26:20 +0200667
Lars-Peter Clausend48b0882014-11-19 18:29:05 +0100668 ret = sigmadsp_attach(adau1701->sigmadsp, &codec->component);
669 if (ret)
670 return ret;
671
Daniel Mack2352d4b2013-06-24 16:31:30 +0200672 /*
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 Clausend48b0882014-11-19 18:29:05 +0100681 ret = adau1701_reset(codec, adau1701->pll_clkdiv, 0);
Daniel Mack2352d4b2013-06-24 16:31:30 +0200682 if (ret < 0)
Daniel Mackf724ba32013-06-07 13:53:04 +0200683 return ret;
Lars-Peter Clausen631ed8a2011-06-13 15:26:20 +0200684
Daniel Mack97d0a862013-06-24 16:31:32 +0200685 /* 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 Clausen631ed8a2011-06-13 15:26:20 +0200698 return 0;
699}
700
701static struct snd_soc_codec_driver adau1701_codec_drv = {
702 .probe = adau1701_probe,
703 .set_bias_level = adau1701_set_bias_level,
Axel Lineb3032f2012-01-27 18:02:09 +0800704 .idle_bias_off = true,
Lars-Peter Clausen631ed8a2011-06-13 15:26:20 +0200705
Lars-Peter Clausen631ed8a2011-06-13 15:26:20 +0200706 .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 Clausen631ed8a2011-06-13 15:26:20 +0200713 .set_sysclk = adau1701_set_sysclk,
714};
715
Daniel Mack45405d52013-06-24 16:31:31 +0200716static 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 Pemberton7a79e942012-12-07 09:26:37 -0500726static int adau1701_i2c_probe(struct i2c_client *client,
727 const struct i2c_device_id *id)
Lars-Peter Clausen631ed8a2011-06-13 15:26:20 +0200728{
729 struct adau1701 *adau1701;
Daniel Mack04561ea2013-05-23 15:46:05 +0200730 struct device *dev = &client->dev;
731 int gpio_nreset = -EINVAL;
Daniel Mack2352d4b2013-06-24 16:31:30 +0200732 int gpio_pll_mode[2] = { -EINVAL, -EINVAL };
Lars-Peter Clausen631ed8a2011-06-13 15:26:20 +0200733 int ret;
734
Daniel Mack04561ea2013-05-23 15:46:05 +0200735 adau1701 = devm_kzalloc(dev, sizeof(*adau1701), GFP_KERNEL);
Lars-Peter Clausen631ed8a2011-06-13 15:26:20 +0200736 if (!adau1701)
737 return -ENOMEM;
738
Lars-Peter Clausena3a1ec62014-11-19 18:29:07 +0100739 adau1701->client = client;
Daniel Mack45405d52013-06-24 16:31:31 +0200740 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 Mack04561ea2013-05-23 15:46:05 +0200745 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 Mack2352d4b2013-06-24 16:31:30 +0200749
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 Mack97d0a862013-06-24 16:31:32 +0200759
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 Mack04561ea2013-05-23 15:46:05 +0200766 }
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 Mack04561ea2013-05-23 15:46:05 +0200773 }
774
Daniel Mack2352d4b2013-06-24 16:31:30 +0200775 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 Mackf724ba32013-06-07 13:53:04 +0200790 adau1701->gpio_nreset = gpio_nreset;
Daniel Mack2352d4b2013-06-24 16:31:30 +0200791 adau1701->gpio_pll_mode[0] = gpio_pll_mode[0];
792 adau1701->gpio_pll_mode[1] = gpio_pll_mode[1];
Daniel Mackf724ba32013-06-07 13:53:04 +0200793
Lars-Peter Clausen631ed8a2011-06-13 15:26:20 +0200794 i2c_set_clientdata(client, adau1701);
Lars-Peter Clausend48b0882014-11-19 18:29:05 +0100795
Lars-Peter Clausena3a1ec62014-11-19 18:29:07 +0100796 adau1701->sigmadsp = devm_sigmadsp_init_i2c(client,
797 &adau1701_sigmadsp_ops, ADAU1701_FIRMWARE);
Lars-Peter Clausend48b0882014-11-19 18:29:05 +0100798 if (IS_ERR(adau1701->sigmadsp))
799 return PTR_ERR(adau1701->sigmadsp);
800
Lars-Peter Clausen631ed8a2011-06-13 15:26:20 +0200801 ret = snd_soc_register_codec(&client->dev, &adau1701_codec_drv,
802 &adau1701_dai, 1);
Lars-Peter Clausen631ed8a2011-06-13 15:26:20 +0200803 return ret;
804}
805
Bill Pemberton7a79e942012-12-07 09:26:37 -0500806static int adau1701_i2c_remove(struct i2c_client *client)
Lars-Peter Clausen631ed8a2011-06-13 15:26:20 +0200807{
808 snd_soc_unregister_codec(&client->dev);
Lars-Peter Clausen631ed8a2011-06-13 15:26:20 +0200809 return 0;
810}
811
812static const struct i2c_device_id adau1701_i2c_id[] = {
Lars-Peter Clausen96b9bc62013-07-22 18:49:53 +0200813 { "adau1401", 0 },
814 { "adau1401a", 0 },
Lars-Peter Clausen631ed8a2011-06-13 15:26:20 +0200815 { "adau1701", 0 },
Lars-Peter Clausen96b9bc62013-07-22 18:49:53 +0200816 { "adau1702", 0 },
Lars-Peter Clausen631ed8a2011-06-13 15:26:20 +0200817 { }
818};
819MODULE_DEVICE_TABLE(i2c, adau1701_i2c_id);
820
821static struct i2c_driver adau1701_i2c_driver = {
822 .driver = {
823 .name = "adau1701",
824 .owner = THIS_MODULE,
Daniel Mack04561ea2013-05-23 15:46:05 +0200825 .of_match_table = of_match_ptr(adau1701_dt_ids),
Lars-Peter Clausen631ed8a2011-06-13 15:26:20 +0200826 },
827 .probe = adau1701_i2c_probe,
Bill Pemberton7a79e942012-12-07 09:26:37 -0500828 .remove = adau1701_i2c_remove,
Lars-Peter Clausen631ed8a2011-06-13 15:26:20 +0200829 .id_table = adau1701_i2c_id,
830};
831
Sachin Kamatbeb22de2012-08-06 17:25:45 +0530832module_i2c_driver(adau1701_i2c_driver);
Lars-Peter Clausen631ed8a2011-06-13 15:26:20 +0200833
834MODULE_DESCRIPTION("ASoC ADAU1701 SigmaDSP driver");
835MODULE_AUTHOR("Cliff Cai <cliff.cai@analog.com>");
836MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
837MODULE_LICENSE("GPL");