Daniel Mack | 4fa8934 | 2013-03-08 13:52:09 +0100 | [diff] [blame] | 1 | /* |
| 2 | * TAS5086 ASoC codec driver |
| 3 | * |
| 4 | * Copyright (c) 2013 Daniel Mack <zonque@gmail.com> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License |
| 8 | * as published by the Free Software Foundation; either version 2 |
| 9 | * of the License, or (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * TODO: |
| 17 | * - implement DAPM and input muxing |
| 18 | * - implement modulation limit |
| 19 | * - implement non-default PWM start |
| 20 | * |
| 21 | * Note that this chip has a very unusual register layout, specifically |
| 22 | * because the registers are of unequal size, and multi-byte registers |
| 23 | * require bulk writes to take effect. Regmap does not support that kind |
| 24 | * of devices. |
| 25 | * |
| 26 | * Currently, the driver does not touch any of the registers >= 0x20, so |
| 27 | * it doesn't matter because the entire map can be accessed as 8-bit |
| 28 | * array. In case more features will be added in the future |
| 29 | * that require access to higher registers, the entire regmap H/W I/O |
| 30 | * routines have to be open-coded. |
| 31 | */ |
| 32 | |
| 33 | #include <linux/module.h> |
| 34 | #include <linux/slab.h> |
| 35 | #include <linux/delay.h> |
| 36 | #include <linux/gpio.h> |
| 37 | #include <linux/i2c.h> |
| 38 | #include <linux/regmap.h> |
| 39 | #include <linux/spi/spi.h> |
| 40 | #include <linux/of_device.h> |
| 41 | #include <linux/of_gpio.h> |
| 42 | #include <sound/pcm.h> |
| 43 | #include <sound/pcm_params.h> |
| 44 | #include <sound/soc.h> |
| 45 | #include <sound/tlv.h> |
| 46 | #include <sound/tas5086.h> |
| 47 | |
| 48 | #define TAS5086_PCM_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | \ |
| 49 | SNDRV_PCM_FMTBIT_S20_3LE | \ |
| 50 | SNDRV_PCM_FMTBIT_S24_3LE) |
| 51 | |
| 52 | #define TAS5086_PCM_RATES (SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | \ |
| 53 | SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_88200 | \ |
| 54 | SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_176400 | \ |
| 55 | SNDRV_PCM_RATE_192000) |
| 56 | |
| 57 | /* |
| 58 | * TAS5086 registers |
| 59 | */ |
| 60 | #define TAS5086_CLOCK_CONTROL 0x00 /* Clock control register */ |
| 61 | #define TAS5086_CLOCK_RATE(val) (val << 5) |
| 62 | #define TAS5086_CLOCK_RATE_MASK (0x7 << 5) |
| 63 | #define TAS5086_CLOCK_RATIO(val) (val << 2) |
| 64 | #define TAS5086_CLOCK_RATIO_MASK (0x7 << 2) |
| 65 | #define TAS5086_CLOCK_SCLK_RATIO_48 (1 << 1) |
| 66 | #define TAS5086_CLOCK_VALID (1 << 0) |
| 67 | |
| 68 | #define TAS5086_DEEMPH_MASK 0x03 |
| 69 | #define TAS5086_SOFT_MUTE_ALL 0x3f |
| 70 | |
| 71 | #define TAS5086_DEV_ID 0x01 /* Device ID register */ |
| 72 | #define TAS5086_ERROR_STATUS 0x02 /* Error status register */ |
| 73 | #define TAS5086_SYS_CONTROL_1 0x03 /* System control register 1 */ |
| 74 | #define TAS5086_SERIAL_DATA_IF 0x04 /* Serial data interface register */ |
| 75 | #define TAS5086_SYS_CONTROL_2 0x05 /* System control register 2 */ |
| 76 | #define TAS5086_SOFT_MUTE 0x06 /* Soft mute register */ |
| 77 | #define TAS5086_MASTER_VOL 0x07 /* Master volume */ |
| 78 | #define TAS5086_CHANNEL_VOL(X) (0x08 + (X)) /* Channel 1-6 volume */ |
| 79 | #define TAS5086_VOLUME_CONTROL 0x09 /* Volume control register */ |
| 80 | #define TAS5086_MOD_LIMIT 0x10 /* Modulation limit register */ |
| 81 | #define TAS5086_PWM_START 0x18 /* PWM start register */ |
| 82 | #define TAS5086_SURROUND 0x19 /* Surround register */ |
| 83 | #define TAS5086_SPLIT_CAP_CHARGE 0x1a /* Split cap charge period register */ |
| 84 | #define TAS5086_OSC_TRIM 0x1b /* Oscillator trim register */ |
| 85 | #define TAS5086_BKNDERR 0x1c |
Daniel Mack | 8892d47 | 2013-06-24 16:25:30 +0200 | [diff] [blame^] | 86 | #define TAS5086_INPUT_MUX 0x20 |
| 87 | #define TAS5086_PWM_OUTPUT_MUX 0x25 |
| 88 | |
| 89 | #define TAS5086_MAX_REGISTER TAS5086_PWM_OUTPUT_MUX |
Daniel Mack | 4fa8934 | 2013-03-08 13:52:09 +0100 | [diff] [blame] | 90 | |
| 91 | /* |
| 92 | * Default TAS5086 power-up configuration |
| 93 | */ |
| 94 | static const struct reg_default tas5086_reg_defaults[] = { |
| 95 | { 0x00, 0x6c }, |
| 96 | { 0x01, 0x03 }, |
| 97 | { 0x02, 0x00 }, |
| 98 | { 0x03, 0xa0 }, |
| 99 | { 0x04, 0x05 }, |
| 100 | { 0x05, 0x60 }, |
| 101 | { 0x06, 0x00 }, |
| 102 | { 0x07, 0xff }, |
| 103 | { 0x08, 0x30 }, |
| 104 | { 0x09, 0x30 }, |
| 105 | { 0x0a, 0x30 }, |
| 106 | { 0x0b, 0x30 }, |
| 107 | { 0x0c, 0x30 }, |
| 108 | { 0x0d, 0x30 }, |
| 109 | { 0x0e, 0xb1 }, |
| 110 | { 0x0f, 0x00 }, |
| 111 | { 0x10, 0x02 }, |
| 112 | { 0x11, 0x00 }, |
| 113 | { 0x12, 0x00 }, |
| 114 | { 0x13, 0x00 }, |
| 115 | { 0x14, 0x00 }, |
| 116 | { 0x15, 0x00 }, |
| 117 | { 0x16, 0x00 }, |
| 118 | { 0x17, 0x00 }, |
| 119 | { 0x18, 0x3f }, |
| 120 | { 0x19, 0x00 }, |
| 121 | { 0x1a, 0x18 }, |
| 122 | { 0x1b, 0x82 }, |
| 123 | { 0x1c, 0x05 }, |
| 124 | }; |
| 125 | |
Daniel Mack | 6b36d37 | 2013-06-24 16:25:29 +0200 | [diff] [blame] | 126 | static int tas5086_register_size(struct device *dev, unsigned int reg) |
| 127 | { |
| 128 | switch (reg) { |
| 129 | case TAS5086_DEV_ID ... TAS5086_BKNDERR: |
| 130 | return 1; |
Daniel Mack | 8892d47 | 2013-06-24 16:25:30 +0200 | [diff] [blame^] | 131 | case TAS5086_INPUT_MUX: |
| 132 | case TAS5086_PWM_OUTPUT_MUX: |
| 133 | return 4; |
Daniel Mack | 6b36d37 | 2013-06-24 16:25:29 +0200 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | dev_err(dev, "Unsupported register address: %d\n", reg); |
| 137 | return 0; |
| 138 | } |
| 139 | |
Daniel Mack | 4fa8934 | 2013-03-08 13:52:09 +0100 | [diff] [blame] | 140 | static bool tas5086_accessible_reg(struct device *dev, unsigned int reg) |
| 141 | { |
Daniel Mack | 8892d47 | 2013-06-24 16:25:30 +0200 | [diff] [blame^] | 142 | switch (reg) { |
| 143 | case 0x0f: |
| 144 | case 0x11 ... 0x17: |
| 145 | case 0x1d ... 0x1f: |
| 146 | return false; |
| 147 | default: |
| 148 | return true; |
| 149 | } |
Daniel Mack | 4fa8934 | 2013-03-08 13:52:09 +0100 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | static bool tas5086_volatile_reg(struct device *dev, unsigned int reg) |
| 153 | { |
| 154 | switch (reg) { |
| 155 | case TAS5086_DEV_ID: |
| 156 | case TAS5086_ERROR_STATUS: |
| 157 | return true; |
| 158 | } |
| 159 | |
| 160 | return false; |
| 161 | } |
| 162 | |
| 163 | static bool tas5086_writeable_reg(struct device *dev, unsigned int reg) |
| 164 | { |
| 165 | return tas5086_accessible_reg(dev, reg) && (reg != TAS5086_DEV_ID); |
| 166 | } |
| 167 | |
Daniel Mack | 6b36d37 | 2013-06-24 16:25:29 +0200 | [diff] [blame] | 168 | static int tas5086_reg_write(void *context, unsigned int reg, |
| 169 | unsigned int value) |
| 170 | { |
| 171 | struct i2c_client *client = context; |
| 172 | unsigned int i, size; |
| 173 | uint8_t buf[5]; |
| 174 | int ret; |
| 175 | |
| 176 | size = tas5086_register_size(&client->dev, reg); |
| 177 | if (size == 0) |
| 178 | return -EINVAL; |
| 179 | |
| 180 | buf[0] = reg; |
| 181 | |
| 182 | for (i = size; i >= 1; --i) { |
| 183 | buf[i] = value; |
| 184 | value >>= 8; |
| 185 | } |
| 186 | |
| 187 | ret = i2c_master_send(client, buf, size + 1); |
| 188 | if (ret == size + 1) |
| 189 | return 0; |
| 190 | else if (ret < 0) |
| 191 | return ret; |
| 192 | else |
| 193 | return -EIO; |
| 194 | } |
| 195 | |
| 196 | static int tas5086_reg_read(void *context, unsigned int reg, |
| 197 | unsigned int *value) |
| 198 | { |
| 199 | struct i2c_client *client = context; |
| 200 | uint8_t send_buf, recv_buf[4]; |
| 201 | struct i2c_msg msgs[2]; |
| 202 | unsigned int size; |
| 203 | unsigned int i; |
| 204 | int ret; |
| 205 | |
| 206 | size = tas5086_register_size(&client->dev, reg); |
| 207 | if (size == 0) |
| 208 | return -EINVAL; |
| 209 | |
| 210 | send_buf = reg; |
| 211 | |
| 212 | msgs[0].addr = client->addr; |
| 213 | msgs[0].len = sizeof(send_buf); |
| 214 | msgs[0].buf = &send_buf; |
| 215 | msgs[0].flags = 0; |
| 216 | |
| 217 | msgs[1].addr = client->addr; |
| 218 | msgs[1].len = size; |
| 219 | msgs[1].buf = recv_buf; |
| 220 | msgs[1].flags = I2C_M_RD; |
| 221 | |
| 222 | ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs)); |
| 223 | if (ret < 0) |
| 224 | return ret; |
| 225 | else if (ret != ARRAY_SIZE(msgs)) |
| 226 | return -EIO; |
| 227 | |
| 228 | *value = 0; |
| 229 | |
| 230 | for (i = 0; i < size; i++) { |
| 231 | *value <<= 8; |
| 232 | *value |= recv_buf[i]; |
| 233 | } |
| 234 | |
| 235 | return 0; |
| 236 | } |
| 237 | |
Daniel Mack | 4fa8934 | 2013-03-08 13:52:09 +0100 | [diff] [blame] | 238 | struct tas5086_private { |
| 239 | struct regmap *regmap; |
| 240 | unsigned int mclk, sclk; |
| 241 | unsigned int format; |
| 242 | bool deemph; |
| 243 | /* Current sample rate for de-emphasis control */ |
| 244 | int rate; |
| 245 | /* GPIO driving Reset pin, if any */ |
| 246 | int gpio_nreset; |
| 247 | }; |
| 248 | |
| 249 | static int tas5086_deemph[] = { 0, 32000, 44100, 48000 }; |
| 250 | |
| 251 | static int tas5086_set_deemph(struct snd_soc_codec *codec) |
| 252 | { |
| 253 | struct tas5086_private *priv = snd_soc_codec_get_drvdata(codec); |
| 254 | int i, val = 0; |
| 255 | |
| 256 | if (priv->deemph) |
| 257 | for (i = 0; i < ARRAY_SIZE(tas5086_deemph); i++) |
| 258 | if (tas5086_deemph[i] == priv->rate) |
| 259 | val = i; |
| 260 | |
| 261 | return regmap_update_bits(priv->regmap, TAS5086_SYS_CONTROL_1, |
| 262 | TAS5086_DEEMPH_MASK, val); |
| 263 | } |
| 264 | |
| 265 | static int tas5086_get_deemph(struct snd_kcontrol *kcontrol, |
| 266 | struct snd_ctl_elem_value *ucontrol) |
| 267 | { |
| 268 | struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); |
| 269 | struct tas5086_private *priv = snd_soc_codec_get_drvdata(codec); |
| 270 | |
| 271 | ucontrol->value.enumerated.item[0] = priv->deemph; |
| 272 | |
| 273 | return 0; |
| 274 | } |
| 275 | |
| 276 | static int tas5086_put_deemph(struct snd_kcontrol *kcontrol, |
| 277 | struct snd_ctl_elem_value *ucontrol) |
| 278 | { |
| 279 | struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); |
| 280 | struct tas5086_private *priv = snd_soc_codec_get_drvdata(codec); |
| 281 | |
| 282 | priv->deemph = ucontrol->value.enumerated.item[0]; |
| 283 | |
| 284 | return tas5086_set_deemph(codec); |
| 285 | } |
| 286 | |
| 287 | |
| 288 | static int tas5086_set_dai_sysclk(struct snd_soc_dai *codec_dai, |
| 289 | int clk_id, unsigned int freq, int dir) |
| 290 | { |
| 291 | struct snd_soc_codec *codec = codec_dai->codec; |
| 292 | struct tas5086_private *priv = snd_soc_codec_get_drvdata(codec); |
| 293 | |
| 294 | switch (clk_id) { |
| 295 | case TAS5086_CLK_IDX_MCLK: |
| 296 | priv->mclk = freq; |
| 297 | break; |
| 298 | case TAS5086_CLK_IDX_SCLK: |
| 299 | priv->sclk = freq; |
| 300 | break; |
| 301 | } |
| 302 | |
| 303 | return 0; |
| 304 | } |
| 305 | |
| 306 | static int tas5086_set_dai_fmt(struct snd_soc_dai *codec_dai, |
| 307 | unsigned int format) |
| 308 | { |
| 309 | struct snd_soc_codec *codec = codec_dai->codec; |
| 310 | struct tas5086_private *priv = snd_soc_codec_get_drvdata(codec); |
| 311 | |
| 312 | /* The TAS5086 can only be slave to all clocks */ |
| 313 | if ((format & SND_SOC_DAIFMT_MASTER_MASK) != SND_SOC_DAIFMT_CBS_CFS) { |
| 314 | dev_err(codec->dev, "Invalid clocking mode\n"); |
| 315 | return -EINVAL; |
| 316 | } |
| 317 | |
| 318 | /* we need to refer to the data format from hw_params() */ |
| 319 | priv->format = format; |
| 320 | |
| 321 | return 0; |
| 322 | } |
| 323 | |
| 324 | static const int tas5086_sample_rates[] = { |
| 325 | 32000, 38000, 44100, 48000, 88200, 96000, 176400, 192000 |
| 326 | }; |
| 327 | |
| 328 | static const int tas5086_ratios[] = { |
| 329 | 64, 128, 192, 256, 384, 512 |
| 330 | }; |
| 331 | |
| 332 | static int index_in_array(const int *array, int len, int needle) |
| 333 | { |
| 334 | int i; |
| 335 | |
| 336 | for (i = 0; i < len; i++) |
| 337 | if (array[i] == needle) |
| 338 | return i; |
| 339 | |
| 340 | return -ENOENT; |
| 341 | } |
| 342 | |
| 343 | static int tas5086_hw_params(struct snd_pcm_substream *substream, |
| 344 | struct snd_pcm_hw_params *params, |
| 345 | struct snd_soc_dai *dai) |
| 346 | { |
| 347 | struct snd_soc_codec *codec = dai->codec; |
| 348 | struct tas5086_private *priv = snd_soc_codec_get_drvdata(codec); |
Dan Carpenter | 28dbd16 | 2013-03-13 08:32:53 +0300 | [diff] [blame] | 349 | int val; |
Daniel Mack | 4fa8934 | 2013-03-08 13:52:09 +0100 | [diff] [blame] | 350 | int ret; |
| 351 | |
| 352 | priv->rate = params_rate(params); |
| 353 | |
| 354 | /* Look up the sample rate and refer to the offset in the list */ |
| 355 | val = index_in_array(tas5086_sample_rates, |
| 356 | ARRAY_SIZE(tas5086_sample_rates), priv->rate); |
| 357 | |
| 358 | if (val < 0) { |
| 359 | dev_err(codec->dev, "Invalid sample rate\n"); |
| 360 | return -EINVAL; |
| 361 | } |
| 362 | |
| 363 | ret = regmap_update_bits(priv->regmap, TAS5086_CLOCK_CONTROL, |
| 364 | TAS5086_CLOCK_RATE_MASK, |
| 365 | TAS5086_CLOCK_RATE(val)); |
| 366 | if (ret < 0) |
| 367 | return ret; |
| 368 | |
| 369 | /* MCLK / Fs ratio */ |
| 370 | val = index_in_array(tas5086_ratios, ARRAY_SIZE(tas5086_ratios), |
| 371 | priv->mclk / priv->rate); |
| 372 | if (val < 0) { |
| 373 | dev_err(codec->dev, "Inavlid MCLK / Fs ratio\n"); |
| 374 | return -EINVAL; |
| 375 | } |
| 376 | |
| 377 | ret = regmap_update_bits(priv->regmap, TAS5086_CLOCK_CONTROL, |
| 378 | TAS5086_CLOCK_RATIO_MASK, |
| 379 | TAS5086_CLOCK_RATIO(val)); |
| 380 | if (ret < 0) |
| 381 | return ret; |
| 382 | |
| 383 | |
| 384 | ret = regmap_update_bits(priv->regmap, TAS5086_CLOCK_CONTROL, |
| 385 | TAS5086_CLOCK_SCLK_RATIO_48, |
| 386 | (priv->sclk == 48 * priv->rate) ? |
| 387 | TAS5086_CLOCK_SCLK_RATIO_48 : 0); |
| 388 | if (ret < 0) |
| 389 | return ret; |
| 390 | |
| 391 | /* |
| 392 | * The chip has a very unituitive register mapping and muxes information |
| 393 | * about data format and sample depth into the same register, but not on |
| 394 | * a logical bit-boundary. Hence, we have to refer to the format passed |
| 395 | * in the set_dai_fmt() callback and set up everything from here. |
| 396 | * |
| 397 | * First, determine the 'base' value, using the format ... |
| 398 | */ |
| 399 | switch (priv->format & SND_SOC_DAIFMT_FORMAT_MASK) { |
| 400 | case SND_SOC_DAIFMT_RIGHT_J: |
| 401 | val = 0x00; |
| 402 | break; |
| 403 | case SND_SOC_DAIFMT_I2S: |
| 404 | val = 0x03; |
| 405 | break; |
| 406 | case SND_SOC_DAIFMT_LEFT_J: |
| 407 | val = 0x06; |
| 408 | break; |
| 409 | default: |
| 410 | dev_err(codec->dev, "Invalid DAI format\n"); |
| 411 | return -EINVAL; |
| 412 | } |
| 413 | |
| 414 | /* ... then add the offset for the sample bit depth. */ |
| 415 | switch (params_format(params)) { |
| 416 | case SNDRV_PCM_FORMAT_S16_LE: |
| 417 | val += 0; |
| 418 | break; |
| 419 | case SNDRV_PCM_FORMAT_S20_3LE: |
| 420 | val += 1; |
| 421 | break; |
| 422 | case SNDRV_PCM_FORMAT_S24_3LE: |
| 423 | val += 2; |
| 424 | break; |
| 425 | default: |
| 426 | dev_err(codec->dev, "Invalid bit width\n"); |
| 427 | return -EINVAL; |
| 428 | }; |
| 429 | |
| 430 | ret = regmap_write(priv->regmap, TAS5086_SERIAL_DATA_IF, val); |
| 431 | if (ret < 0) |
| 432 | return ret; |
| 433 | |
| 434 | /* clock is considered valid now */ |
| 435 | ret = regmap_update_bits(priv->regmap, TAS5086_CLOCK_CONTROL, |
| 436 | TAS5086_CLOCK_VALID, TAS5086_CLOCK_VALID); |
| 437 | if (ret < 0) |
| 438 | return ret; |
| 439 | |
| 440 | return tas5086_set_deemph(codec); |
| 441 | } |
| 442 | |
| 443 | static int tas5086_mute_stream(struct snd_soc_dai *dai, int mute, int stream) |
| 444 | { |
| 445 | struct snd_soc_codec *codec = dai->codec; |
| 446 | struct tas5086_private *priv = snd_soc_codec_get_drvdata(codec); |
| 447 | unsigned int val = 0; |
| 448 | |
| 449 | if (mute) |
| 450 | val = TAS5086_SOFT_MUTE_ALL; |
| 451 | |
| 452 | return regmap_write(priv->regmap, TAS5086_SOFT_MUTE, val); |
| 453 | } |
| 454 | |
| 455 | /* TAS5086 controls */ |
| 456 | static const DECLARE_TLV_DB_SCALE(tas5086_dac_tlv, -10350, 50, 1); |
| 457 | |
| 458 | static const struct snd_kcontrol_new tas5086_controls[] = { |
| 459 | SOC_SINGLE_TLV("Master Playback Volume", TAS5086_MASTER_VOL, |
| 460 | 0, 0xff, 1, tas5086_dac_tlv), |
| 461 | SOC_DOUBLE_R_TLV("Channel 1/2 Playback Volume", |
| 462 | TAS5086_CHANNEL_VOL(0), TAS5086_CHANNEL_VOL(1), |
| 463 | 0, 0xff, 1, tas5086_dac_tlv), |
| 464 | SOC_DOUBLE_R_TLV("Channel 3/4 Playback Volume", |
| 465 | TAS5086_CHANNEL_VOL(2), TAS5086_CHANNEL_VOL(3), |
| 466 | 0, 0xff, 1, tas5086_dac_tlv), |
| 467 | SOC_DOUBLE_R_TLV("Channel 5/6 Playback Volume", |
| 468 | TAS5086_CHANNEL_VOL(4), TAS5086_CHANNEL_VOL(5), |
| 469 | 0, 0xff, 1, tas5086_dac_tlv), |
| 470 | SOC_SINGLE_BOOL_EXT("De-emphasis Switch", 0, |
| 471 | tas5086_get_deemph, tas5086_put_deemph), |
| 472 | }; |
| 473 | |
| 474 | static const struct snd_soc_dai_ops tas5086_dai_ops = { |
| 475 | .hw_params = tas5086_hw_params, |
| 476 | .set_sysclk = tas5086_set_dai_sysclk, |
| 477 | .set_fmt = tas5086_set_dai_fmt, |
| 478 | .mute_stream = tas5086_mute_stream, |
| 479 | }; |
| 480 | |
| 481 | static struct snd_soc_dai_driver tas5086_dai = { |
| 482 | .name = "tas5086-hifi", |
| 483 | .playback = { |
| 484 | .stream_name = "Playback", |
| 485 | .channels_min = 2, |
| 486 | .channels_max = 6, |
| 487 | .rates = TAS5086_PCM_RATES, |
| 488 | .formats = TAS5086_PCM_FORMATS, |
| 489 | }, |
| 490 | .ops = &tas5086_dai_ops, |
| 491 | }; |
| 492 | |
| 493 | #ifdef CONFIG_PM |
| 494 | static int tas5086_soc_resume(struct snd_soc_codec *codec) |
| 495 | { |
| 496 | struct tas5086_private *priv = snd_soc_codec_get_drvdata(codec); |
| 497 | |
| 498 | /* Restore codec state */ |
| 499 | return regcache_sync(priv->regmap); |
| 500 | } |
| 501 | #else |
| 502 | #define tas5086_soc_resume NULL |
| 503 | #endif /* CONFIG_PM */ |
| 504 | |
| 505 | #ifdef CONFIG_OF |
| 506 | static const struct of_device_id tas5086_dt_ids[] = { |
| 507 | { .compatible = "ti,tas5086", }, |
| 508 | { } |
| 509 | }; |
| 510 | MODULE_DEVICE_TABLE(of, tas5086_dt_ids); |
| 511 | #endif |
| 512 | |
| 513 | /* charge period values in microseconds */ |
| 514 | static const int tas5086_charge_period[] = { |
| 515 | 13000, 16900, 23400, 31200, 41600, 54600, 72800, 96200, |
| 516 | 130000, 156000, 234000, 312000, 416000, 546000, 728000, 962000, |
| 517 | 1300000, 169000, 2340000, 3120000, 4160000, 5460000, 7280000, 9620000, |
| 518 | }; |
| 519 | |
| 520 | static int tas5086_probe(struct snd_soc_codec *codec) |
| 521 | { |
| 522 | struct tas5086_private *priv = snd_soc_codec_get_drvdata(codec); |
| 523 | int charge_period = 1300000; /* hardware default is 1300 ms */ |
| 524 | int i, ret; |
| 525 | |
| 526 | if (of_match_device(of_match_ptr(tas5086_dt_ids), codec->dev)) { |
| 527 | struct device_node *of_node = codec->dev->of_node; |
| 528 | of_property_read_u32(of_node, "ti,charge-period", &charge_period); |
| 529 | } |
| 530 | |
| 531 | /* lookup and set split-capacitor charge period */ |
| 532 | if (charge_period == 0) { |
| 533 | regmap_write(priv->regmap, TAS5086_SPLIT_CAP_CHARGE, 0); |
| 534 | } else { |
| 535 | i = index_in_array(tas5086_charge_period, |
| 536 | ARRAY_SIZE(tas5086_charge_period), |
| 537 | charge_period); |
| 538 | if (i >= 0) |
| 539 | regmap_write(priv->regmap, TAS5086_SPLIT_CAP_CHARGE, |
| 540 | i + 0x08); |
| 541 | else |
| 542 | dev_warn(codec->dev, |
| 543 | "Invalid split-cap charge period of %d ns.\n", |
| 544 | charge_period); |
| 545 | } |
| 546 | |
| 547 | /* enable factory trim */ |
| 548 | ret = regmap_write(priv->regmap, TAS5086_OSC_TRIM, 0x00); |
| 549 | if (ret < 0) |
| 550 | return ret; |
| 551 | |
| 552 | /* start all channels */ |
| 553 | ret = regmap_write(priv->regmap, TAS5086_SYS_CONTROL_2, 0x20); |
| 554 | if (ret < 0) |
| 555 | return ret; |
| 556 | |
| 557 | /* set master volume to 0 dB */ |
| 558 | ret = regmap_write(priv->regmap, TAS5086_MASTER_VOL, 0x30); |
| 559 | if (ret < 0) |
| 560 | return ret; |
| 561 | |
| 562 | /* mute all channels for now */ |
| 563 | ret = regmap_write(priv->regmap, TAS5086_SOFT_MUTE, |
| 564 | TAS5086_SOFT_MUTE_ALL); |
| 565 | if (ret < 0) |
| 566 | return ret; |
| 567 | |
| 568 | return 0; |
| 569 | } |
| 570 | |
| 571 | static int tas5086_remove(struct snd_soc_codec *codec) |
| 572 | { |
| 573 | struct tas5086_private *priv = snd_soc_codec_get_drvdata(codec); |
| 574 | |
| 575 | if (gpio_is_valid(priv->gpio_nreset)) |
| 576 | /* Set codec to the reset state */ |
| 577 | gpio_set_value(priv->gpio_nreset, 0); |
| 578 | |
| 579 | return 0; |
| 580 | }; |
| 581 | |
| 582 | static struct snd_soc_codec_driver soc_codec_dev_tas5086 = { |
| 583 | .probe = tas5086_probe, |
| 584 | .remove = tas5086_remove, |
| 585 | .resume = tas5086_soc_resume, |
| 586 | .controls = tas5086_controls, |
| 587 | .num_controls = ARRAY_SIZE(tas5086_controls), |
| 588 | }; |
| 589 | |
| 590 | static const struct i2c_device_id tas5086_i2c_id[] = { |
| 591 | { "tas5086", 0 }, |
| 592 | { } |
| 593 | }; |
| 594 | MODULE_DEVICE_TABLE(i2c, tas5086_i2c_id); |
| 595 | |
| 596 | static const struct regmap_config tas5086_regmap = { |
| 597 | .reg_bits = 8, |
Daniel Mack | 8892d47 | 2013-06-24 16:25:30 +0200 | [diff] [blame^] | 598 | .val_bits = 32, |
| 599 | .max_register = TAS5086_MAX_REGISTER, |
Daniel Mack | 4fa8934 | 2013-03-08 13:52:09 +0100 | [diff] [blame] | 600 | .reg_defaults = tas5086_reg_defaults, |
| 601 | .num_reg_defaults = ARRAY_SIZE(tas5086_reg_defaults), |
| 602 | .cache_type = REGCACHE_RBTREE, |
| 603 | .volatile_reg = tas5086_volatile_reg, |
| 604 | .writeable_reg = tas5086_writeable_reg, |
| 605 | .readable_reg = tas5086_accessible_reg, |
Daniel Mack | 6b36d37 | 2013-06-24 16:25:29 +0200 | [diff] [blame] | 606 | .reg_read = tas5086_reg_read, |
| 607 | .reg_write = tas5086_reg_write, |
Daniel Mack | 4fa8934 | 2013-03-08 13:52:09 +0100 | [diff] [blame] | 608 | }; |
| 609 | |
| 610 | static int tas5086_i2c_probe(struct i2c_client *i2c, |
| 611 | const struct i2c_device_id *id) |
| 612 | { |
| 613 | struct tas5086_private *priv; |
| 614 | struct device *dev = &i2c->dev; |
| 615 | int gpio_nreset = -EINVAL; |
| 616 | int i, ret; |
| 617 | |
| 618 | priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL); |
| 619 | if (!priv) |
| 620 | return -ENOMEM; |
| 621 | |
Daniel Mack | 6b36d37 | 2013-06-24 16:25:29 +0200 | [diff] [blame] | 622 | priv->regmap = devm_regmap_init(dev, NULL, i2c, &tas5086_regmap); |
Daniel Mack | 4fa8934 | 2013-03-08 13:52:09 +0100 | [diff] [blame] | 623 | if (IS_ERR(priv->regmap)) { |
| 624 | ret = PTR_ERR(priv->regmap); |
| 625 | dev_err(&i2c->dev, "Failed to create regmap: %d\n", ret); |
| 626 | return ret; |
| 627 | } |
| 628 | |
| 629 | i2c_set_clientdata(i2c, priv); |
| 630 | |
| 631 | if (of_match_device(of_match_ptr(tas5086_dt_ids), dev)) { |
| 632 | struct device_node *of_node = dev->of_node; |
| 633 | gpio_nreset = of_get_named_gpio(of_node, "reset-gpio", 0); |
| 634 | } |
| 635 | |
| 636 | if (gpio_is_valid(gpio_nreset)) |
| 637 | if (devm_gpio_request(dev, gpio_nreset, "TAS5086 Reset")) |
| 638 | gpio_nreset = -EINVAL; |
| 639 | |
| 640 | if (gpio_is_valid(gpio_nreset)) { |
| 641 | /* Reset codec - minimum assertion time is 400ns */ |
| 642 | gpio_direction_output(gpio_nreset, 0); |
| 643 | udelay(1); |
| 644 | gpio_set_value(gpio_nreset, 1); |
| 645 | |
| 646 | /* Codec needs ~15ms to wake up */ |
| 647 | msleep(15); |
| 648 | } |
| 649 | |
| 650 | priv->gpio_nreset = gpio_nreset; |
| 651 | |
| 652 | /* The TAS5086 always returns 0x03 in its TAS5086_DEV_ID register */ |
| 653 | ret = regmap_read(priv->regmap, TAS5086_DEV_ID, &i); |
| 654 | if (ret < 0) |
| 655 | return ret; |
| 656 | |
| 657 | if (i != 0x3) { |
| 658 | dev_err(dev, |
| 659 | "Failed to identify TAS5086 codec (got %02x)\n", i); |
| 660 | return -ENODEV; |
| 661 | } |
| 662 | |
| 663 | return snd_soc_register_codec(&i2c->dev, &soc_codec_dev_tas5086, |
| 664 | &tas5086_dai, 1); |
| 665 | } |
| 666 | |
| 667 | static int tas5086_i2c_remove(struct i2c_client *i2c) |
| 668 | { |
| 669 | snd_soc_unregister_codec(&i2c->dev); |
| 670 | return 0; |
| 671 | } |
| 672 | |
| 673 | static struct i2c_driver tas5086_i2c_driver = { |
| 674 | .driver = { |
| 675 | .name = "tas5086", |
| 676 | .owner = THIS_MODULE, |
| 677 | .of_match_table = of_match_ptr(tas5086_dt_ids), |
| 678 | }, |
| 679 | .id_table = tas5086_i2c_id, |
| 680 | .probe = tas5086_i2c_probe, |
| 681 | .remove = tas5086_i2c_remove, |
| 682 | }; |
| 683 | |
Wei Yongjun | c300d6d | 2013-03-12 21:36:24 +0800 | [diff] [blame] | 684 | module_i2c_driver(tas5086_i2c_driver); |
Daniel Mack | 4fa8934 | 2013-03-08 13:52:09 +0100 | [diff] [blame] | 685 | |
| 686 | MODULE_AUTHOR("Daniel Mack <zonque@gmail.com>"); |
| 687 | MODULE_DESCRIPTION("Texas Instruments TAS5086 ALSA SoC Codec Driver"); |
| 688 | MODULE_LICENSE("GPL"); |