Philipp Zabel | b7482f5 | 2008-05-28 17:58:06 +0100 | [diff] [blame] | 1 | /* |
| 2 | * uda1380.c - Philips UDA1380 ALSA SoC audio driver |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License version 2 as |
| 6 | * published by the Free Software Foundation. |
| 7 | * |
Philipp Zabel | 1abd918 | 2009-06-15 22:18:23 +0200 | [diff] [blame] | 8 | * Copyright (c) 2007-2009 Philipp Zabel <philipp.zabel@gmail.com> |
Philipp Zabel | b7482f5 | 2008-05-28 17:58:06 +0100 | [diff] [blame] | 9 | * |
| 10 | * Modified by Richard Purdie <richard@openedhand.com> to fit into SoC |
| 11 | * codec model. |
| 12 | * |
| 13 | * Copyright (c) 2005 Giorgio Padrin <giorgio@mandarinlogiq.org> |
| 14 | * Copyright 2005 Openedhand Ltd. |
| 15 | */ |
| 16 | |
| 17 | #include <linux/module.h> |
| 18 | #include <linux/init.h> |
| 19 | #include <linux/types.h> |
Philipp Zabel | b7482f5 | 2008-05-28 17:58:06 +0100 | [diff] [blame] | 20 | #include <linux/slab.h> |
| 21 | #include <linux/errno.h> |
Philipp Zabel | 1abd918 | 2009-06-15 22:18:23 +0200 | [diff] [blame] | 22 | #include <linux/gpio.h> |
Philipp Zabel | b7482f5 | 2008-05-28 17:58:06 +0100 | [diff] [blame] | 23 | #include <linux/delay.h> |
| 24 | #include <linux/i2c.h> |
Philipp Zabel | ef9e5e5 | 2009-03-03 16:10:54 +0100 | [diff] [blame] | 25 | #include <linux/workqueue.h> |
Philipp Zabel | b7482f5 | 2008-05-28 17:58:06 +0100 | [diff] [blame] | 26 | #include <sound/core.h> |
| 27 | #include <sound/control.h> |
| 28 | #include <sound/initval.h> |
Philipp Zabel | b7482f5 | 2008-05-28 17:58:06 +0100 | [diff] [blame] | 29 | #include <sound/soc.h> |
Philipp Zabel | b7482f5 | 2008-05-28 17:58:06 +0100 | [diff] [blame] | 30 | #include <sound/tlv.h> |
Philipp Zabel | 1abd918 | 2009-06-15 22:18:23 +0200 | [diff] [blame] | 31 | #include <sound/uda1380.h> |
Philipp Zabel | b7482f5 | 2008-05-28 17:58:06 +0100 | [diff] [blame] | 32 | |
| 33 | #include "uda1380.h" |
| 34 | |
Philipp Zabel | 1abd918 | 2009-06-15 22:18:23 +0200 | [diff] [blame] | 35 | /* codec private data */ |
| 36 | struct uda1380_priv { |
Kuninori Morimoto | b40822d | 2018-01-29 04:44:09 +0000 | [diff] [blame] | 37 | struct snd_soc_component *component; |
Philipp Zabel | 1abd918 | 2009-06-15 22:18:23 +0200 | [diff] [blame] | 38 | unsigned int dac_clk; |
| 39 | struct work_struct work; |
Kuninori Morimoto | eaa5321 | 2017-11-09 00:19:33 +0000 | [diff] [blame] | 40 | struct i2c_client *i2c; |
Kuninori Morimoto | c001bf6 | 2017-11-14 01:04:08 +0000 | [diff] [blame] | 41 | u16 *reg_cache; |
Philipp Zabel | 1abd918 | 2009-06-15 22:18:23 +0200 | [diff] [blame] | 42 | }; |
| 43 | |
Philipp Zabel | b7482f5 | 2008-05-28 17:58:06 +0100 | [diff] [blame] | 44 | /* |
| 45 | * uda1380 register cache |
| 46 | */ |
| 47 | static const u16 uda1380_reg[UDA1380_CACHEREGNUM] = { |
| 48 | 0x0502, 0x0000, 0x0000, 0x3f3f, |
| 49 | 0x0202, 0x0000, 0x0000, 0x0000, |
| 50 | 0x0000, 0x0000, 0x0000, 0x0000, |
| 51 | 0x0000, 0x0000, 0x0000, 0x0000, |
| 52 | 0x0000, 0xff00, 0x0000, 0x4800, |
| 53 | 0x0000, 0x0000, 0x0000, 0x0000, |
| 54 | 0x0000, 0x0000, 0x0000, 0x0000, |
| 55 | 0x0000, 0x0000, 0x0000, 0x0000, |
| 56 | 0x0000, 0x8000, 0x0002, 0x0000, |
| 57 | }; |
| 58 | |
Philipp Zabel | ef9e5e5 | 2009-03-03 16:10:54 +0100 | [diff] [blame] | 59 | static unsigned long uda1380_cache_dirty; |
| 60 | |
Philipp Zabel | b7482f5 | 2008-05-28 17:58:06 +0100 | [diff] [blame] | 61 | /* |
| 62 | * read uda1380 register cache |
| 63 | */ |
Kuninori Morimoto | b40822d | 2018-01-29 04:44:09 +0000 | [diff] [blame] | 64 | static inline unsigned int uda1380_read_reg_cache(struct snd_soc_component *component, |
Philipp Zabel | b7482f5 | 2008-05-28 17:58:06 +0100 | [diff] [blame] | 65 | unsigned int reg) |
| 66 | { |
Kuninori Morimoto | b40822d | 2018-01-29 04:44:09 +0000 | [diff] [blame] | 67 | struct uda1380_priv *uda1380 = snd_soc_component_get_drvdata(component); |
Kuninori Morimoto | c001bf6 | 2017-11-14 01:04:08 +0000 | [diff] [blame] | 68 | u16 *cache = uda1380->reg_cache; |
| 69 | |
Philipp Zabel | b7482f5 | 2008-05-28 17:58:06 +0100 | [diff] [blame] | 70 | if (reg == UDA1380_RESET) |
| 71 | return 0; |
| 72 | if (reg >= UDA1380_CACHEREGNUM) |
| 73 | return -1; |
| 74 | return cache[reg]; |
| 75 | } |
| 76 | |
| 77 | /* |
| 78 | * write uda1380 register cache |
| 79 | */ |
Kuninori Morimoto | b40822d | 2018-01-29 04:44:09 +0000 | [diff] [blame] | 80 | static inline void uda1380_write_reg_cache(struct snd_soc_component *component, |
Philipp Zabel | b7482f5 | 2008-05-28 17:58:06 +0100 | [diff] [blame] | 81 | u16 reg, unsigned int value) |
| 82 | { |
Kuninori Morimoto | b40822d | 2018-01-29 04:44:09 +0000 | [diff] [blame] | 83 | struct uda1380_priv *uda1380 = snd_soc_component_get_drvdata(component); |
Kuninori Morimoto | c001bf6 | 2017-11-14 01:04:08 +0000 | [diff] [blame] | 84 | u16 *cache = uda1380->reg_cache; |
Philipp Zabel | ef9e5e5 | 2009-03-03 16:10:54 +0100 | [diff] [blame] | 85 | |
Philipp Zabel | b7482f5 | 2008-05-28 17:58:06 +0100 | [diff] [blame] | 86 | if (reg >= UDA1380_CACHEREGNUM) |
| 87 | return; |
Philipp Zabel | ef9e5e5 | 2009-03-03 16:10:54 +0100 | [diff] [blame] | 88 | if ((reg >= 0x10) && (cache[reg] != value)) |
| 89 | set_bit(reg - 0x10, &uda1380_cache_dirty); |
Philipp Zabel | b7482f5 | 2008-05-28 17:58:06 +0100 | [diff] [blame] | 90 | cache[reg] = value; |
| 91 | } |
| 92 | |
| 93 | /* |
| 94 | * write to the UDA1380 register space |
| 95 | */ |
Kuninori Morimoto | b40822d | 2018-01-29 04:44:09 +0000 | [diff] [blame] | 96 | static int uda1380_write(struct snd_soc_component *component, unsigned int reg, |
Philipp Zabel | b7482f5 | 2008-05-28 17:58:06 +0100 | [diff] [blame] | 97 | unsigned int value) |
| 98 | { |
Kuninori Morimoto | b40822d | 2018-01-29 04:44:09 +0000 | [diff] [blame] | 99 | struct uda1380_priv *uda1380 = snd_soc_component_get_drvdata(component); |
Philipp Zabel | b7482f5 | 2008-05-28 17:58:06 +0100 | [diff] [blame] | 100 | u8 data[3]; |
| 101 | |
| 102 | /* data is |
| 103 | * data[0] is register offset |
| 104 | * data[1] is MS byte |
| 105 | * data[2] is LS byte |
| 106 | */ |
| 107 | data[0] = reg; |
| 108 | data[1] = (value & 0xff00) >> 8; |
| 109 | data[2] = value & 0x00ff; |
| 110 | |
Kuninori Morimoto | b40822d | 2018-01-29 04:44:09 +0000 | [diff] [blame] | 111 | uda1380_write_reg_cache(component, reg, value); |
Philipp Zabel | b7482f5 | 2008-05-28 17:58:06 +0100 | [diff] [blame] | 112 | |
| 113 | /* the interpolator & decimator regs must only be written when the |
| 114 | * codec DAI is active. |
| 115 | */ |
Kuninori Morimoto | b40822d | 2018-01-29 04:44:09 +0000 | [diff] [blame] | 116 | if (!snd_soc_component_is_active(component) && (reg >= UDA1380_MVOL)) |
Philipp Zabel | b7482f5 | 2008-05-28 17:58:06 +0100 | [diff] [blame] | 117 | return 0; |
| 118 | pr_debug("uda1380: hw write %x val %x\n", reg, value); |
Kuninori Morimoto | eaa5321 | 2017-11-09 00:19:33 +0000 | [diff] [blame] | 119 | if (i2c_master_send(uda1380->i2c, data, 3) == 3) { |
Philipp Zabel | b7482f5 | 2008-05-28 17:58:06 +0100 | [diff] [blame] | 120 | unsigned int val; |
Kuninori Morimoto | eaa5321 | 2017-11-09 00:19:33 +0000 | [diff] [blame] | 121 | i2c_master_send(uda1380->i2c, data, 1); |
| 122 | i2c_master_recv(uda1380->i2c, data, 2); |
Philipp Zabel | b7482f5 | 2008-05-28 17:58:06 +0100 | [diff] [blame] | 123 | val = (data[0]<<8) | data[1]; |
| 124 | if (val != value) { |
| 125 | pr_debug("uda1380: READ BACK VAL %x\n", |
| 126 | (data[0]<<8) | data[1]); |
| 127 | return -EIO; |
| 128 | } |
Philipp Zabel | ef9e5e5 | 2009-03-03 16:10:54 +0100 | [diff] [blame] | 129 | if (reg >= 0x10) |
| 130 | clear_bit(reg - 0x10, &uda1380_cache_dirty); |
Philipp Zabel | b7482f5 | 2008-05-28 17:58:06 +0100 | [diff] [blame] | 131 | return 0; |
| 132 | } else |
| 133 | return -EIO; |
| 134 | } |
| 135 | |
Kuninori Morimoto | b40822d | 2018-01-29 04:44:09 +0000 | [diff] [blame] | 136 | static void uda1380_sync_cache(struct snd_soc_component *component) |
Vasily Khoruzhick | 8614d310 | 2010-08-30 11:28:07 +0300 | [diff] [blame] | 137 | { |
Kuninori Morimoto | b40822d | 2018-01-29 04:44:09 +0000 | [diff] [blame] | 138 | struct uda1380_priv *uda1380 = snd_soc_component_get_drvdata(component); |
Vasily Khoruzhick | 8614d310 | 2010-08-30 11:28:07 +0300 | [diff] [blame] | 139 | int reg; |
| 140 | u8 data[3]; |
Kuninori Morimoto | c001bf6 | 2017-11-14 01:04:08 +0000 | [diff] [blame] | 141 | u16 *cache = uda1380->reg_cache; |
Vasily Khoruzhick | 8614d310 | 2010-08-30 11:28:07 +0300 | [diff] [blame] | 142 | |
| 143 | /* Sync reg_cache with the hardware */ |
| 144 | for (reg = 0; reg < UDA1380_MVOL; reg++) { |
| 145 | data[0] = reg; |
| 146 | data[1] = (cache[reg] & 0xff00) >> 8; |
| 147 | data[2] = cache[reg] & 0x00ff; |
Kuninori Morimoto | eaa5321 | 2017-11-09 00:19:33 +0000 | [diff] [blame] | 148 | if (i2c_master_send(uda1380->i2c, data, 3) != 3) |
Kuninori Morimoto | b40822d | 2018-01-29 04:44:09 +0000 | [diff] [blame] | 149 | dev_err(component->dev, "%s: write to reg 0x%x failed\n", |
Vasily Khoruzhick | 8614d310 | 2010-08-30 11:28:07 +0300 | [diff] [blame] | 150 | __func__, reg); |
| 151 | } |
| 152 | } |
| 153 | |
Kuninori Morimoto | b40822d | 2018-01-29 04:44:09 +0000 | [diff] [blame] | 154 | static int uda1380_reset(struct snd_soc_component *component) |
Vasily Khoruzhick | 8614d310 | 2010-08-30 11:28:07 +0300 | [diff] [blame] | 155 | { |
Kuninori Morimoto | b40822d | 2018-01-29 04:44:09 +0000 | [diff] [blame] | 156 | struct uda1380_platform_data *pdata = component->dev->platform_data; |
| 157 | struct uda1380_priv *uda1380 = snd_soc_component_get_drvdata(component); |
Vasily Khoruzhick | 8614d310 | 2010-08-30 11:28:07 +0300 | [diff] [blame] | 158 | |
| 159 | if (gpio_is_valid(pdata->gpio_reset)) { |
| 160 | gpio_set_value(pdata->gpio_reset, 1); |
| 161 | mdelay(1); |
| 162 | gpio_set_value(pdata->gpio_reset, 0); |
| 163 | } else { |
| 164 | u8 data[3]; |
| 165 | |
| 166 | data[0] = UDA1380_RESET; |
| 167 | data[1] = 0; |
| 168 | data[2] = 0; |
| 169 | |
Kuninori Morimoto | eaa5321 | 2017-11-09 00:19:33 +0000 | [diff] [blame] | 170 | if (i2c_master_send(uda1380->i2c, data, 3) != 3) { |
Kuninori Morimoto | b40822d | 2018-01-29 04:44:09 +0000 | [diff] [blame] | 171 | dev_err(component->dev, "%s: failed\n", __func__); |
Vasily Khoruzhick | 8614d310 | 2010-08-30 11:28:07 +0300 | [diff] [blame] | 172 | return -EIO; |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | return 0; |
| 177 | } |
Philipp Zabel | b7482f5 | 2008-05-28 17:58:06 +0100 | [diff] [blame] | 178 | |
Philipp Zabel | ef9e5e5 | 2009-03-03 16:10:54 +0100 | [diff] [blame] | 179 | static void uda1380_flush_work(struct work_struct *work) |
| 180 | { |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 181 | struct uda1380_priv *uda1380 = container_of(work, struct uda1380_priv, work); |
Kuninori Morimoto | b40822d | 2018-01-29 04:44:09 +0000 | [diff] [blame] | 182 | struct snd_soc_component *uda1380_component = uda1380->component; |
Philipp Zabel | ef9e5e5 | 2009-03-03 16:10:54 +0100 | [diff] [blame] | 183 | int bit, reg; |
| 184 | |
Akinobu Mita | 984b3f5 | 2010-03-05 13:41:37 -0800 | [diff] [blame] | 185 | for_each_set_bit(bit, &uda1380_cache_dirty, UDA1380_CACHEREGNUM - 0x10) { |
Philipp Zabel | ef9e5e5 | 2009-03-03 16:10:54 +0100 | [diff] [blame] | 186 | reg = 0x10 + bit; |
| 187 | pr_debug("uda1380: flush reg %x val %x:\n", reg, |
Kuninori Morimoto | b40822d | 2018-01-29 04:44:09 +0000 | [diff] [blame] | 188 | uda1380_read_reg_cache(uda1380_component, reg)); |
| 189 | uda1380_write(uda1380_component, reg, |
| 190 | uda1380_read_reg_cache(uda1380_component, reg)); |
Philipp Zabel | ef9e5e5 | 2009-03-03 16:10:54 +0100 | [diff] [blame] | 191 | clear_bit(bit, &uda1380_cache_dirty); |
| 192 | } |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 193 | |
Philipp Zabel | ef9e5e5 | 2009-03-03 16:10:54 +0100 | [diff] [blame] | 194 | } |
| 195 | |
Philipp Zabel | b7482f5 | 2008-05-28 17:58:06 +0100 | [diff] [blame] | 196 | /* declarations of ALSA reg_elem_REAL controls */ |
| 197 | static const char *uda1380_deemp[] = { |
| 198 | "None", |
| 199 | "32kHz", |
| 200 | "44.1kHz", |
| 201 | "48kHz", |
| 202 | "96kHz", |
| 203 | }; |
| 204 | static const char *uda1380_input_sel[] = { |
| 205 | "Line", |
| 206 | "Mic + Line R", |
| 207 | "Line L", |
| 208 | "Mic", |
| 209 | }; |
| 210 | static const char *uda1380_output_sel[] = { |
| 211 | "DAC", |
| 212 | "Analog Mixer", |
| 213 | }; |
| 214 | static const char *uda1380_spf_mode[] = { |
| 215 | "Flat", |
| 216 | "Minimum1", |
| 217 | "Minimum2", |
| 218 | "Maximum" |
| 219 | }; |
| 220 | static const char *uda1380_capture_sel[] = { |
| 221 | "ADC", |
| 222 | "Digital Mixer" |
| 223 | }; |
| 224 | static const char *uda1380_sel_ns[] = { |
| 225 | "3rd-order", |
| 226 | "5th-order" |
| 227 | }; |
| 228 | static const char *uda1380_mix_control[] = { |
| 229 | "off", |
| 230 | "PCM only", |
| 231 | "before sound processing", |
| 232 | "after sound processing" |
| 233 | }; |
| 234 | static const char *uda1380_sdet_setting[] = { |
| 235 | "3200", |
| 236 | "4800", |
| 237 | "9600", |
| 238 | "19200" |
| 239 | }; |
| 240 | static const char *uda1380_os_setting[] = { |
| 241 | "single-speed", |
| 242 | "double-speed (no mixing)", |
| 243 | "quad-speed (no mixing)" |
| 244 | }; |
| 245 | |
| 246 | static const struct soc_enum uda1380_deemp_enum[] = { |
Takashi Iwai | 1dbb348 | 2014-02-18 10:32:16 +0100 | [diff] [blame] | 247 | SOC_ENUM_SINGLE(UDA1380_DEEMP, 8, ARRAY_SIZE(uda1380_deemp), |
| 248 | uda1380_deemp), |
| 249 | SOC_ENUM_SINGLE(UDA1380_DEEMP, 0, ARRAY_SIZE(uda1380_deemp), |
| 250 | uda1380_deemp), |
Philipp Zabel | b7482f5 | 2008-05-28 17:58:06 +0100 | [diff] [blame] | 251 | }; |
Takashi Iwai | 1dbb348 | 2014-02-18 10:32:16 +0100 | [diff] [blame] | 252 | static SOC_ENUM_SINGLE_DECL(uda1380_input_sel_enum, |
| 253 | UDA1380_ADC, 2, uda1380_input_sel); /* SEL_MIC, SEL_LNA */ |
| 254 | static SOC_ENUM_SINGLE_DECL(uda1380_output_sel_enum, |
| 255 | UDA1380_PM, 7, uda1380_output_sel); /* R02_EN_AVC */ |
| 256 | static SOC_ENUM_SINGLE_DECL(uda1380_spf_enum, |
| 257 | UDA1380_MODE, 14, uda1380_spf_mode); /* M */ |
| 258 | static SOC_ENUM_SINGLE_DECL(uda1380_capture_sel_enum, |
| 259 | UDA1380_IFACE, 6, uda1380_capture_sel); /* SEL_SOURCE */ |
| 260 | static SOC_ENUM_SINGLE_DECL(uda1380_sel_ns_enum, |
| 261 | UDA1380_MIXER, 14, uda1380_sel_ns); /* SEL_NS */ |
| 262 | static SOC_ENUM_SINGLE_DECL(uda1380_mix_enum, |
| 263 | UDA1380_MIXER, 12, uda1380_mix_control); /* MIX, MIX_POS */ |
| 264 | static SOC_ENUM_SINGLE_DECL(uda1380_sdet_enum, |
| 265 | UDA1380_MIXER, 4, uda1380_sdet_setting); /* SD_VALUE */ |
| 266 | static SOC_ENUM_SINGLE_DECL(uda1380_os_enum, |
| 267 | UDA1380_MIXER, 0, uda1380_os_setting); /* OS */ |
Philipp Zabel | b7482f5 | 2008-05-28 17:58:06 +0100 | [diff] [blame] | 268 | |
| 269 | /* |
| 270 | * from -48 dB in 1.5 dB steps (mute instead of -49.5 dB) |
| 271 | */ |
| 272 | static DECLARE_TLV_DB_SCALE(amix_tlv, -4950, 150, 1); |
| 273 | |
| 274 | /* |
| 275 | * from -78 dB in 1 dB steps (3 dB steps, really. LSB are ignored), |
| 276 | * from -66 dB in 0.5 dB steps (2 dB steps, really) and |
| 277 | * from -52 dB in 0.25 dB steps |
| 278 | */ |
Lars-Peter Clausen | 5ee0b8f | 2015-08-02 17:19:57 +0200 | [diff] [blame] | 279 | static const DECLARE_TLV_DB_RANGE(mvol_tlv, |
Philipp Zabel | b7482f5 | 2008-05-28 17:58:06 +0100 | [diff] [blame] | 280 | 0, 15, TLV_DB_SCALE_ITEM(-8200, 100, 1), |
| 281 | 16, 43, TLV_DB_SCALE_ITEM(-6600, 50, 0), |
Lars-Peter Clausen | 5ee0b8f | 2015-08-02 17:19:57 +0200 | [diff] [blame] | 282 | 44, 252, TLV_DB_SCALE_ITEM(-5200, 25, 0) |
| 283 | ); |
Philipp Zabel | b7482f5 | 2008-05-28 17:58:06 +0100 | [diff] [blame] | 284 | |
| 285 | /* |
| 286 | * from -72 dB in 1.5 dB steps (6 dB steps really), |
| 287 | * from -66 dB in 0.75 dB steps (3 dB steps really), |
| 288 | * from -60 dB in 0.5 dB steps (2 dB steps really) and |
| 289 | * from -46 dB in 0.25 dB steps |
| 290 | */ |
Lars-Peter Clausen | 5ee0b8f | 2015-08-02 17:19:57 +0200 | [diff] [blame] | 291 | static const DECLARE_TLV_DB_RANGE(vc_tlv, |
Philipp Zabel | b7482f5 | 2008-05-28 17:58:06 +0100 | [diff] [blame] | 292 | 0, 7, TLV_DB_SCALE_ITEM(-7800, 150, 1), |
| 293 | 8, 15, TLV_DB_SCALE_ITEM(-6600, 75, 0), |
| 294 | 16, 43, TLV_DB_SCALE_ITEM(-6000, 50, 0), |
Lars-Peter Clausen | 5ee0b8f | 2015-08-02 17:19:57 +0200 | [diff] [blame] | 295 | 44, 228, TLV_DB_SCALE_ITEM(-4600, 25, 0) |
| 296 | ); |
Philipp Zabel | b7482f5 | 2008-05-28 17:58:06 +0100 | [diff] [blame] | 297 | |
| 298 | /* from 0 to 6 dB in 2 dB steps if SPF mode != flat */ |
| 299 | static DECLARE_TLV_DB_SCALE(tr_tlv, 0, 200, 0); |
| 300 | |
| 301 | /* from 0 to 24 dB in 2 dB steps, if SPF mode == maximum, otherwise cuts |
| 302 | * off at 18 dB max) */ |
| 303 | static DECLARE_TLV_DB_SCALE(bb_tlv, 0, 200, 0); |
| 304 | |
| 305 | /* from -63 to 24 dB in 0.5 dB steps (-128...48) */ |
| 306 | static DECLARE_TLV_DB_SCALE(dec_tlv, -6400, 50, 1); |
| 307 | |
| 308 | /* from 0 to 24 dB in 3 dB steps */ |
| 309 | static DECLARE_TLV_DB_SCALE(pga_tlv, 0, 300, 0); |
| 310 | |
| 311 | /* from 0 to 30 dB in 2 dB steps */ |
| 312 | static DECLARE_TLV_DB_SCALE(vga_tlv, 0, 200, 0); |
| 313 | |
| 314 | static const struct snd_kcontrol_new uda1380_snd_controls[] = { |
| 315 | SOC_DOUBLE_TLV("Analog Mixer Volume", UDA1380_AMIX, 0, 8, 44, 1, amix_tlv), /* AVCR, AVCL */ |
| 316 | SOC_DOUBLE_TLV("Master Playback Volume", UDA1380_MVOL, 0, 8, 252, 1, mvol_tlv), /* MVCL, MVCR */ |
| 317 | SOC_SINGLE_TLV("ADC Playback Volume", UDA1380_MIXVOL, 8, 228, 1, vc_tlv), /* VC2 */ |
| 318 | SOC_SINGLE_TLV("PCM Playback Volume", UDA1380_MIXVOL, 0, 228, 1, vc_tlv), /* VC1 */ |
| 319 | SOC_ENUM("Sound Processing Filter", uda1380_spf_enum), /* M */ |
| 320 | SOC_DOUBLE_TLV("Tone Control - Treble", UDA1380_MODE, 4, 12, 3, 0, tr_tlv), /* TRL, TRR */ |
| 321 | SOC_DOUBLE_TLV("Tone Control - Bass", UDA1380_MODE, 0, 8, 15, 0, bb_tlv), /* BBL, BBR */ |
| 322 | /**/ SOC_SINGLE("Master Playback Switch", UDA1380_DEEMP, 14, 1, 1), /* MTM */ |
| 323 | SOC_SINGLE("ADC Playback Switch", UDA1380_DEEMP, 11, 1, 1), /* MT2 from decimation filter */ |
| 324 | SOC_ENUM("ADC Playback De-emphasis", uda1380_deemp_enum[0]), /* DE2 */ |
| 325 | SOC_SINGLE("PCM Playback Switch", UDA1380_DEEMP, 3, 1, 1), /* MT1, from digital data input */ |
| 326 | SOC_ENUM("PCM Playback De-emphasis", uda1380_deemp_enum[1]), /* DE1 */ |
| 327 | SOC_SINGLE("DAC Polarity inverting Switch", UDA1380_MIXER, 15, 1, 0), /* DA_POL_INV */ |
| 328 | SOC_ENUM("Noise Shaper", uda1380_sel_ns_enum), /* SEL_NS */ |
| 329 | SOC_ENUM("Digital Mixer Signal Control", uda1380_mix_enum), /* MIX_POS, MIX */ |
Philipp Zabel | b7482f5 | 2008-05-28 17:58:06 +0100 | [diff] [blame] | 330 | SOC_SINGLE("Silence Detector Switch", UDA1380_MIXER, 6, 1, 0), /* SDET_ON */ |
| 331 | SOC_ENUM("Silence Detector Setting", uda1380_sdet_enum), /* SD_VALUE */ |
| 332 | SOC_ENUM("Oversampling Input", uda1380_os_enum), /* OS */ |
| 333 | SOC_DOUBLE_S8_TLV("ADC Capture Volume", UDA1380_DEC, -128, 48, dec_tlv), /* ML_DEC, MR_DEC */ |
| 334 | /**/ SOC_SINGLE("ADC Capture Switch", UDA1380_PGA, 15, 1, 1), /* MT_ADC */ |
| 335 | SOC_DOUBLE_TLV("Line Capture Volume", UDA1380_PGA, 0, 8, 8, 0, pga_tlv), /* PGA_GAINCTRLL, PGA_GAINCTRLR */ |
| 336 | SOC_SINGLE("ADC Polarity inverting Switch", UDA1380_ADC, 12, 1, 0), /* ADCPOL_INV */ |
| 337 | SOC_SINGLE_TLV("Mic Capture Volume", UDA1380_ADC, 8, 15, 0, vga_tlv), /* VGA_CTRL */ |
| 338 | SOC_SINGLE("DC Filter Bypass Switch", UDA1380_ADC, 1, 1, 0), /* SKIP_DCFIL (before decimator) */ |
| 339 | SOC_SINGLE("DC Filter Enable Switch", UDA1380_ADC, 0, 1, 0), /* EN_DCFIL (at output of decimator) */ |
| 340 | SOC_SINGLE("AGC Timing", UDA1380_AGC, 8, 7, 0), /* TODO: enum, see table 62 */ |
| 341 | SOC_SINGLE("AGC Target level", UDA1380_AGC, 2, 3, 1), /* AGC_LEVEL */ |
| 342 | /* -5.5, -8, -11.5, -14 dBFS */ |
| 343 | SOC_SINGLE("AGC Switch", UDA1380_AGC, 0, 1, 0), |
| 344 | }; |
| 345 | |
Philipp Zabel | b7482f5 | 2008-05-28 17:58:06 +0100 | [diff] [blame] | 346 | /* Input mux */ |
| 347 | static const struct snd_kcontrol_new uda1380_input_mux_control = |
| 348 | SOC_DAPM_ENUM("Route", uda1380_input_sel_enum); |
| 349 | |
| 350 | /* Output mux */ |
| 351 | static const struct snd_kcontrol_new uda1380_output_mux_control = |
| 352 | SOC_DAPM_ENUM("Route", uda1380_output_sel_enum); |
| 353 | |
| 354 | /* Capture mux */ |
| 355 | static const struct snd_kcontrol_new uda1380_capture_mux_control = |
| 356 | SOC_DAPM_ENUM("Route", uda1380_capture_sel_enum); |
| 357 | |
| 358 | |
| 359 | static const struct snd_soc_dapm_widget uda1380_dapm_widgets[] = { |
| 360 | SND_SOC_DAPM_MUX("Input Mux", SND_SOC_NOPM, 0, 0, |
| 361 | &uda1380_input_mux_control), |
| 362 | SND_SOC_DAPM_MUX("Output Mux", SND_SOC_NOPM, 0, 0, |
| 363 | &uda1380_output_mux_control), |
| 364 | SND_SOC_DAPM_MUX("Capture Mux", SND_SOC_NOPM, 0, 0, |
| 365 | &uda1380_capture_mux_control), |
| 366 | SND_SOC_DAPM_PGA("Left PGA", UDA1380_PM, 3, 0, NULL, 0), |
| 367 | SND_SOC_DAPM_PGA("Right PGA", UDA1380_PM, 1, 0, NULL, 0), |
| 368 | SND_SOC_DAPM_PGA("Mic LNA", UDA1380_PM, 4, 0, NULL, 0), |
| 369 | SND_SOC_DAPM_ADC("Left ADC", "Left Capture", UDA1380_PM, 2, 0), |
| 370 | SND_SOC_DAPM_ADC("Right ADC", "Right Capture", UDA1380_PM, 0, 0), |
| 371 | SND_SOC_DAPM_INPUT("VINM"), |
| 372 | SND_SOC_DAPM_INPUT("VINL"), |
| 373 | SND_SOC_DAPM_INPUT("VINR"), |
| 374 | SND_SOC_DAPM_MIXER("Analog Mixer", UDA1380_PM, 6, 0, NULL, 0), |
| 375 | SND_SOC_DAPM_OUTPUT("VOUTLHP"), |
| 376 | SND_SOC_DAPM_OUTPUT("VOUTRHP"), |
| 377 | SND_SOC_DAPM_OUTPUT("VOUTL"), |
| 378 | SND_SOC_DAPM_OUTPUT("VOUTR"), |
| 379 | SND_SOC_DAPM_DAC("DAC", "Playback", UDA1380_PM, 10, 0), |
| 380 | SND_SOC_DAPM_PGA("HeadPhone Driver", UDA1380_PM, 13, 0, NULL, 0), |
| 381 | }; |
| 382 | |
Axel Lin | 58fa8e4 | 2011-12-19 13:54:38 +0800 | [diff] [blame] | 383 | static const struct snd_soc_dapm_route uda1380_dapm_routes[] = { |
Philipp Zabel | b7482f5 | 2008-05-28 17:58:06 +0100 | [diff] [blame] | 384 | |
| 385 | /* output mux */ |
| 386 | {"HeadPhone Driver", NULL, "Output Mux"}, |
| 387 | {"VOUTR", NULL, "Output Mux"}, |
| 388 | {"VOUTL", NULL, "Output Mux"}, |
| 389 | |
| 390 | {"Analog Mixer", NULL, "VINR"}, |
| 391 | {"Analog Mixer", NULL, "VINL"}, |
| 392 | {"Analog Mixer", NULL, "DAC"}, |
| 393 | |
| 394 | {"Output Mux", "DAC", "DAC"}, |
| 395 | {"Output Mux", "Analog Mixer", "Analog Mixer"}, |
| 396 | |
| 397 | /* {"DAC", "Digital Mixer", "I2S" } */ |
| 398 | |
| 399 | /* headphone driver */ |
| 400 | {"VOUTLHP", NULL, "HeadPhone Driver"}, |
| 401 | {"VOUTRHP", NULL, "HeadPhone Driver"}, |
| 402 | |
| 403 | /* input mux */ |
| 404 | {"Left ADC", NULL, "Input Mux"}, |
| 405 | {"Input Mux", "Mic", "Mic LNA"}, |
| 406 | {"Input Mux", "Mic + Line R", "Mic LNA"}, |
| 407 | {"Input Mux", "Line L", "Left PGA"}, |
| 408 | {"Input Mux", "Line", "Left PGA"}, |
| 409 | |
| 410 | /* right input */ |
| 411 | {"Right ADC", "Mic + Line R", "Right PGA"}, |
| 412 | {"Right ADC", "Line", "Right PGA"}, |
| 413 | |
| 414 | /* inputs */ |
| 415 | {"Mic LNA", NULL, "VINM"}, |
| 416 | {"Left PGA", NULL, "VINL"}, |
| 417 | {"Right PGA", NULL, "VINR"}, |
| 418 | }; |
| 419 | |
Philipp Zabel | 5b24744 | 2009-02-03 17:19:40 +0100 | [diff] [blame] | 420 | static int uda1380_set_dai_fmt_both(struct snd_soc_dai *codec_dai, |
Philipp Zabel | b7482f5 | 2008-05-28 17:58:06 +0100 | [diff] [blame] | 421 | unsigned int fmt) |
| 422 | { |
Kuninori Morimoto | b40822d | 2018-01-29 04:44:09 +0000 | [diff] [blame] | 423 | struct snd_soc_component *component = codec_dai->component; |
Philipp Zabel | b7482f5 | 2008-05-28 17:58:06 +0100 | [diff] [blame] | 424 | int iface; |
| 425 | |
| 426 | /* set up DAI based upon fmt */ |
Kuninori Morimoto | b40822d | 2018-01-29 04:44:09 +0000 | [diff] [blame] | 427 | iface = uda1380_read_reg_cache(component, UDA1380_IFACE); |
Philipp Zabel | b7482f5 | 2008-05-28 17:58:06 +0100 | [diff] [blame] | 428 | iface &= ~(R01_SFORI_MASK | R01_SIM | R01_SFORO_MASK); |
| 429 | |
Philipp Zabel | b7482f5 | 2008-05-28 17:58:06 +0100 | [diff] [blame] | 430 | switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) { |
| 431 | case SND_SOC_DAIFMT_I2S: |
| 432 | iface |= R01_SFORI_I2S | R01_SFORO_I2S; |
| 433 | break; |
| 434 | case SND_SOC_DAIFMT_LSB: |
Philipp Zabel | 5b24744 | 2009-02-03 17:19:40 +0100 | [diff] [blame] | 435 | iface |= R01_SFORI_LSB16 | R01_SFORO_LSB16; |
Philipp Zabel | b7482f5 | 2008-05-28 17:58:06 +0100 | [diff] [blame] | 436 | break; |
| 437 | case SND_SOC_DAIFMT_MSB: |
Philipp Zabel | 5b24744 | 2009-02-03 17:19:40 +0100 | [diff] [blame] | 438 | iface |= R01_SFORI_MSB | R01_SFORO_MSB; |
| 439 | } |
| 440 | |
Philipp Zabel | 5f2a938 | 2009-03-03 16:10:52 +0100 | [diff] [blame] | 441 | /* DATAI is slave only, so in single-link mode, this has to be slave */ |
| 442 | if ((fmt & SND_SOC_DAIFMT_MASTER_MASK) != SND_SOC_DAIFMT_CBS_CFS) |
| 443 | return -EINVAL; |
Philipp Zabel | 5b24744 | 2009-02-03 17:19:40 +0100 | [diff] [blame] | 444 | |
Kuninori Morimoto | b40822d | 2018-01-29 04:44:09 +0000 | [diff] [blame] | 445 | uda1380_write_reg_cache(component, UDA1380_IFACE, iface); |
Philipp Zabel | 5b24744 | 2009-02-03 17:19:40 +0100 | [diff] [blame] | 446 | |
| 447 | return 0; |
| 448 | } |
| 449 | |
| 450 | static int uda1380_set_dai_fmt_playback(struct snd_soc_dai *codec_dai, |
| 451 | unsigned int fmt) |
| 452 | { |
Kuninori Morimoto | b40822d | 2018-01-29 04:44:09 +0000 | [diff] [blame] | 453 | struct snd_soc_component *component = codec_dai->component; |
Philipp Zabel | 5b24744 | 2009-02-03 17:19:40 +0100 | [diff] [blame] | 454 | int iface; |
| 455 | |
| 456 | /* set up DAI based upon fmt */ |
Kuninori Morimoto | b40822d | 2018-01-29 04:44:09 +0000 | [diff] [blame] | 457 | iface = uda1380_read_reg_cache(component, UDA1380_IFACE); |
Philipp Zabel | 5b24744 | 2009-02-03 17:19:40 +0100 | [diff] [blame] | 458 | iface &= ~R01_SFORI_MASK; |
| 459 | |
| 460 | switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) { |
| 461 | case SND_SOC_DAIFMT_I2S: |
| 462 | iface |= R01_SFORI_I2S; |
| 463 | break; |
| 464 | case SND_SOC_DAIFMT_LSB: |
| 465 | iface |= R01_SFORI_LSB16; |
| 466 | break; |
| 467 | case SND_SOC_DAIFMT_MSB: |
| 468 | iface |= R01_SFORI_MSB; |
| 469 | } |
| 470 | |
Philipp Zabel | 5f2a938 | 2009-03-03 16:10:52 +0100 | [diff] [blame] | 471 | /* DATAI is slave only, so this has to be slave */ |
| 472 | if ((fmt & SND_SOC_DAIFMT_MASTER_MASK) != SND_SOC_DAIFMT_CBS_CFS) |
| 473 | return -EINVAL; |
| 474 | |
Kuninori Morimoto | b40822d | 2018-01-29 04:44:09 +0000 | [diff] [blame] | 475 | uda1380_write(component, UDA1380_IFACE, iface); |
Philipp Zabel | 5b24744 | 2009-02-03 17:19:40 +0100 | [diff] [blame] | 476 | |
| 477 | return 0; |
| 478 | } |
| 479 | |
| 480 | static int uda1380_set_dai_fmt_capture(struct snd_soc_dai *codec_dai, |
| 481 | unsigned int fmt) |
| 482 | { |
Kuninori Morimoto | b40822d | 2018-01-29 04:44:09 +0000 | [diff] [blame] | 483 | struct snd_soc_component *component = codec_dai->component; |
Philipp Zabel | 5b24744 | 2009-02-03 17:19:40 +0100 | [diff] [blame] | 484 | int iface; |
| 485 | |
| 486 | /* set up DAI based upon fmt */ |
Kuninori Morimoto | b40822d | 2018-01-29 04:44:09 +0000 | [diff] [blame] | 487 | iface = uda1380_read_reg_cache(component, UDA1380_IFACE); |
Philipp Zabel | 5b24744 | 2009-02-03 17:19:40 +0100 | [diff] [blame] | 488 | iface &= ~(R01_SIM | R01_SFORO_MASK); |
| 489 | |
| 490 | switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) { |
| 491 | case SND_SOC_DAIFMT_I2S: |
| 492 | iface |= R01_SFORO_I2S; |
| 493 | break; |
| 494 | case SND_SOC_DAIFMT_LSB: |
| 495 | iface |= R01_SFORO_LSB16; |
| 496 | break; |
| 497 | case SND_SOC_DAIFMT_MSB: |
| 498 | iface |= R01_SFORO_MSB; |
Philipp Zabel | b7482f5 | 2008-05-28 17:58:06 +0100 | [diff] [blame] | 499 | } |
| 500 | |
| 501 | if ((fmt & SND_SOC_DAIFMT_MASTER_MASK) == SND_SOC_DAIFMT_CBM_CFM) |
| 502 | iface |= R01_SIM; |
| 503 | |
Kuninori Morimoto | b40822d | 2018-01-29 04:44:09 +0000 | [diff] [blame] | 504 | uda1380_write(component, UDA1380_IFACE, iface); |
Philipp Zabel | b7482f5 | 2008-05-28 17:58:06 +0100 | [diff] [blame] | 505 | |
| 506 | return 0; |
| 507 | } |
| 508 | |
Philipp Zabel | ef9e5e5 | 2009-03-03 16:10:54 +0100 | [diff] [blame] | 509 | static int uda1380_trigger(struct snd_pcm_substream *substream, int cmd, |
| 510 | struct snd_soc_dai *dai) |
Philipp Zabel | b7482f5 | 2008-05-28 17:58:06 +0100 | [diff] [blame] | 511 | { |
Kuninori Morimoto | b40822d | 2018-01-29 04:44:09 +0000 | [diff] [blame] | 512 | struct snd_soc_component *component = dai->component; |
| 513 | struct uda1380_priv *uda1380 = snd_soc_component_get_drvdata(component); |
| 514 | int mixer = uda1380_read_reg_cache(component, UDA1380_MIXER); |
Philipp Zabel | b7482f5 | 2008-05-28 17:58:06 +0100 | [diff] [blame] | 515 | |
Philipp Zabel | ef9e5e5 | 2009-03-03 16:10:54 +0100 | [diff] [blame] | 516 | switch (cmd) { |
| 517 | case SNDRV_PCM_TRIGGER_START: |
| 518 | case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: |
Kuninori Morimoto | b40822d | 2018-01-29 04:44:09 +0000 | [diff] [blame] | 519 | uda1380_write_reg_cache(component, UDA1380_MIXER, |
Philipp Zabel | ef9e5e5 | 2009-03-03 16:10:54 +0100 | [diff] [blame] | 520 | mixer & ~R14_SILENCE); |
Philipp Zabel | 1abd918 | 2009-06-15 22:18:23 +0200 | [diff] [blame] | 521 | schedule_work(&uda1380->work); |
Philipp Zabel | ef9e5e5 | 2009-03-03 16:10:54 +0100 | [diff] [blame] | 522 | break; |
| 523 | case SNDRV_PCM_TRIGGER_STOP: |
| 524 | case SNDRV_PCM_TRIGGER_PAUSE_PUSH: |
Kuninori Morimoto | b40822d | 2018-01-29 04:44:09 +0000 | [diff] [blame] | 525 | uda1380_write_reg_cache(component, UDA1380_MIXER, |
Philipp Zabel | ef9e5e5 | 2009-03-03 16:10:54 +0100 | [diff] [blame] | 526 | mixer | R14_SILENCE); |
Philipp Zabel | 1abd918 | 2009-06-15 22:18:23 +0200 | [diff] [blame] | 527 | schedule_work(&uda1380->work); |
Philipp Zabel | ef9e5e5 | 2009-03-03 16:10:54 +0100 | [diff] [blame] | 528 | break; |
Philipp Zabel | b7482f5 | 2008-05-28 17:58:06 +0100 | [diff] [blame] | 529 | } |
Philipp Zabel | b7482f5 | 2008-05-28 17:58:06 +0100 | [diff] [blame] | 530 | return 0; |
| 531 | } |
| 532 | |
| 533 | static int uda1380_pcm_hw_params(struct snd_pcm_substream *substream, |
Mark Brown | dee89c4 | 2008-11-18 22:11:38 +0000 | [diff] [blame] | 534 | struct snd_pcm_hw_params *params, |
| 535 | struct snd_soc_dai *dai) |
Philipp Zabel | b7482f5 | 2008-05-28 17:58:06 +0100 | [diff] [blame] | 536 | { |
Kuninori Morimoto | b40822d | 2018-01-29 04:44:09 +0000 | [diff] [blame] | 537 | struct snd_soc_component *component = dai->component; |
| 538 | u16 clk = uda1380_read_reg_cache(component, UDA1380_CLK); |
Philipp Zabel | b7482f5 | 2008-05-28 17:58:06 +0100 | [diff] [blame] | 539 | |
| 540 | /* set WSPLL power and divider if running from this clock */ |
| 541 | if (clk & R00_DAC_CLK) { |
| 542 | int rate = params_rate(params); |
Kuninori Morimoto | b40822d | 2018-01-29 04:44:09 +0000 | [diff] [blame] | 543 | u16 pm = uda1380_read_reg_cache(component, UDA1380_PM); |
Philipp Zabel | b7482f5 | 2008-05-28 17:58:06 +0100 | [diff] [blame] | 544 | clk &= ~0x3; /* clear SEL_LOOP_DIV */ |
| 545 | switch (rate) { |
| 546 | case 6250 ... 12500: |
| 547 | clk |= 0x0; |
| 548 | break; |
| 549 | case 12501 ... 25000: |
| 550 | clk |= 0x1; |
| 551 | break; |
| 552 | case 25001 ... 50000: |
| 553 | clk |= 0x2; |
| 554 | break; |
| 555 | case 50001 ... 100000: |
| 556 | clk |= 0x3; |
| 557 | break; |
| 558 | } |
Kuninori Morimoto | b40822d | 2018-01-29 04:44:09 +0000 | [diff] [blame] | 559 | uda1380_write(component, UDA1380_PM, R02_PON_PLL | pm); |
Philipp Zabel | b7482f5 | 2008-05-28 17:58:06 +0100 | [diff] [blame] | 560 | } |
| 561 | |
| 562 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) |
| 563 | clk |= R00_EN_DAC | R00_EN_INT; |
| 564 | else |
| 565 | clk |= R00_EN_ADC | R00_EN_DEC; |
| 566 | |
Kuninori Morimoto | b40822d | 2018-01-29 04:44:09 +0000 | [diff] [blame] | 567 | uda1380_write(component, UDA1380_CLK, clk); |
Philipp Zabel | b7482f5 | 2008-05-28 17:58:06 +0100 | [diff] [blame] | 568 | return 0; |
| 569 | } |
| 570 | |
Mark Brown | dee89c4 | 2008-11-18 22:11:38 +0000 | [diff] [blame] | 571 | static void uda1380_pcm_shutdown(struct snd_pcm_substream *substream, |
| 572 | struct snd_soc_dai *dai) |
Philipp Zabel | b7482f5 | 2008-05-28 17:58:06 +0100 | [diff] [blame] | 573 | { |
Kuninori Morimoto | b40822d | 2018-01-29 04:44:09 +0000 | [diff] [blame] | 574 | struct snd_soc_component *component = dai->component; |
| 575 | u16 clk = uda1380_read_reg_cache(component, UDA1380_CLK); |
Philipp Zabel | b7482f5 | 2008-05-28 17:58:06 +0100 | [diff] [blame] | 576 | |
| 577 | /* shut down WSPLL power if running from this clock */ |
| 578 | if (clk & R00_DAC_CLK) { |
Kuninori Morimoto | b40822d | 2018-01-29 04:44:09 +0000 | [diff] [blame] | 579 | u16 pm = uda1380_read_reg_cache(component, UDA1380_PM); |
| 580 | uda1380_write(component, UDA1380_PM, ~R02_PON_PLL & pm); |
Philipp Zabel | b7482f5 | 2008-05-28 17:58:06 +0100 | [diff] [blame] | 581 | } |
| 582 | |
| 583 | if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) |
| 584 | clk &= ~(R00_EN_DAC | R00_EN_INT); |
| 585 | else |
| 586 | clk &= ~(R00_EN_ADC | R00_EN_DEC); |
| 587 | |
Kuninori Morimoto | b40822d | 2018-01-29 04:44:09 +0000 | [diff] [blame] | 588 | uda1380_write(component, UDA1380_CLK, clk); |
Philipp Zabel | b7482f5 | 2008-05-28 17:58:06 +0100 | [diff] [blame] | 589 | } |
| 590 | |
Kuninori Morimoto | b40822d | 2018-01-29 04:44:09 +0000 | [diff] [blame] | 591 | static int uda1380_set_bias_level(struct snd_soc_component *component, |
Philipp Zabel | b7482f5 | 2008-05-28 17:58:06 +0100 | [diff] [blame] | 592 | enum snd_soc_bias_level level) |
| 593 | { |
Kuninori Morimoto | b40822d | 2018-01-29 04:44:09 +0000 | [diff] [blame] | 594 | int pm = uda1380_read_reg_cache(component, UDA1380_PM); |
Vasily Khoruzhick | 8614d310 | 2010-08-30 11:28:07 +0300 | [diff] [blame] | 595 | int reg; |
Kuninori Morimoto | b40822d | 2018-01-29 04:44:09 +0000 | [diff] [blame] | 596 | struct uda1380_platform_data *pdata = component->dev->platform_data; |
Vasily Khoruzhick | 8614d310 | 2010-08-30 11:28:07 +0300 | [diff] [blame] | 597 | |
Philipp Zabel | b7482f5 | 2008-05-28 17:58:06 +0100 | [diff] [blame] | 598 | switch (level) { |
| 599 | case SND_SOC_BIAS_ON: |
| 600 | case SND_SOC_BIAS_PREPARE: |
Vasily Khoruzhick | 8614d310 | 2010-08-30 11:28:07 +0300 | [diff] [blame] | 601 | /* ADC, DAC on */ |
Kuninori Morimoto | b40822d | 2018-01-29 04:44:09 +0000 | [diff] [blame] | 602 | uda1380_write(component, UDA1380_PM, R02_PON_BIAS | pm); |
Philipp Zabel | b7482f5 | 2008-05-28 17:58:06 +0100 | [diff] [blame] | 603 | break; |
| 604 | case SND_SOC_BIAS_STANDBY: |
Kuninori Morimoto | b40822d | 2018-01-29 04:44:09 +0000 | [diff] [blame] | 605 | if (snd_soc_component_get_bias_level(component) == SND_SOC_BIAS_OFF) { |
Vasily Khoruzhick | 8614d310 | 2010-08-30 11:28:07 +0300 | [diff] [blame] | 606 | if (gpio_is_valid(pdata->gpio_power)) { |
| 607 | gpio_set_value(pdata->gpio_power, 1); |
Vasily Khoruzhick | 8e3dce4 | 2010-09-07 17:04:17 +0300 | [diff] [blame] | 608 | mdelay(1); |
Kuninori Morimoto | b40822d | 2018-01-29 04:44:09 +0000 | [diff] [blame] | 609 | uda1380_reset(component); |
Vasily Khoruzhick | 8614d310 | 2010-08-30 11:28:07 +0300 | [diff] [blame] | 610 | } |
| 611 | |
Kuninori Morimoto | b40822d | 2018-01-29 04:44:09 +0000 | [diff] [blame] | 612 | uda1380_sync_cache(component); |
Vasily Khoruzhick | 8614d310 | 2010-08-30 11:28:07 +0300 | [diff] [blame] | 613 | } |
Kuninori Morimoto | b40822d | 2018-01-29 04:44:09 +0000 | [diff] [blame] | 614 | uda1380_write(component, UDA1380_PM, 0x0); |
Philipp Zabel | b7482f5 | 2008-05-28 17:58:06 +0100 | [diff] [blame] | 615 | break; |
Vasily Khoruzhick | 8614d310 | 2010-08-30 11:28:07 +0300 | [diff] [blame] | 616 | case SND_SOC_BIAS_OFF: |
| 617 | if (!gpio_is_valid(pdata->gpio_power)) |
| 618 | break; |
| 619 | |
| 620 | gpio_set_value(pdata->gpio_power, 0); |
| 621 | |
| 622 | /* Mark mixer regs cache dirty to sync them with |
| 623 | * codec regs on power on. |
| 624 | */ |
| 625 | for (reg = UDA1380_MVOL; reg < UDA1380_CACHEREGNUM; reg++) |
| 626 | set_bit(reg - 0x10, &uda1380_cache_dirty); |
Philipp Zabel | b7482f5 | 2008-05-28 17:58:06 +0100 | [diff] [blame] | 627 | } |
Philipp Zabel | b7482f5 | 2008-05-28 17:58:06 +0100 | [diff] [blame] | 628 | return 0; |
| 629 | } |
| 630 | |
| 631 | #define UDA1380_RATES (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_11025 |\ |
| 632 | SNDRV_PCM_RATE_16000 | SNDRV_PCM_RATE_22050 |\ |
| 633 | SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000) |
| 634 | |
Lars-Peter Clausen | 85e7652 | 2011-11-23 11:40:40 +0100 | [diff] [blame] | 635 | static const struct snd_soc_dai_ops uda1380_dai_ops = { |
Eric Miao | 6335d05 | 2009-03-03 09:41:00 +0800 | [diff] [blame] | 636 | .hw_params = uda1380_pcm_hw_params, |
| 637 | .shutdown = uda1380_pcm_shutdown, |
Mark Brown | 65ec1cd | 2009-03-11 16:51:31 +0000 | [diff] [blame] | 638 | .trigger = uda1380_trigger, |
Eric Miao | 6335d05 | 2009-03-03 09:41:00 +0800 | [diff] [blame] | 639 | .set_fmt = uda1380_set_dai_fmt_both, |
| 640 | }; |
| 641 | |
Lars-Peter Clausen | 85e7652 | 2011-11-23 11:40:40 +0100 | [diff] [blame] | 642 | static const struct snd_soc_dai_ops uda1380_dai_ops_playback = { |
Eric Miao | 6335d05 | 2009-03-03 09:41:00 +0800 | [diff] [blame] | 643 | .hw_params = uda1380_pcm_hw_params, |
| 644 | .shutdown = uda1380_pcm_shutdown, |
Mark Brown | 65ec1cd | 2009-03-11 16:51:31 +0000 | [diff] [blame] | 645 | .trigger = uda1380_trigger, |
Eric Miao | 6335d05 | 2009-03-03 09:41:00 +0800 | [diff] [blame] | 646 | .set_fmt = uda1380_set_dai_fmt_playback, |
| 647 | }; |
| 648 | |
Lars-Peter Clausen | 85e7652 | 2011-11-23 11:40:40 +0100 | [diff] [blame] | 649 | static const struct snd_soc_dai_ops uda1380_dai_ops_capture = { |
Eric Miao | 6335d05 | 2009-03-03 09:41:00 +0800 | [diff] [blame] | 650 | .hw_params = uda1380_pcm_hw_params, |
| 651 | .shutdown = uda1380_pcm_shutdown, |
Mark Brown | 65ec1cd | 2009-03-11 16:51:31 +0000 | [diff] [blame] | 652 | .trigger = uda1380_trigger, |
Eric Miao | 6335d05 | 2009-03-03 09:41:00 +0800 | [diff] [blame] | 653 | .set_fmt = uda1380_set_dai_fmt_capture, |
| 654 | }; |
| 655 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 656 | static struct snd_soc_dai_driver uda1380_dai[] = { |
Philipp Zabel | b7482f5 | 2008-05-28 17:58:06 +0100 | [diff] [blame] | 657 | { |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 658 | .name = "uda1380-hifi", |
Philipp Zabel | b7482f5 | 2008-05-28 17:58:06 +0100 | [diff] [blame] | 659 | .playback = { |
| 660 | .stream_name = "Playback", |
| 661 | .channels_min = 1, |
| 662 | .channels_max = 2, |
| 663 | .rates = UDA1380_RATES, |
| 664 | .formats = SNDRV_PCM_FMTBIT_S16_LE,}, |
| 665 | .capture = { |
| 666 | .stream_name = "Capture", |
| 667 | .channels_min = 1, |
| 668 | .channels_max = 2, |
| 669 | .rates = UDA1380_RATES, |
| 670 | .formats = SNDRV_PCM_FMTBIT_S16_LE,}, |
Eric Miao | 6335d05 | 2009-03-03 09:41:00 +0800 | [diff] [blame] | 671 | .ops = &uda1380_dai_ops, |
Philipp Zabel | b7482f5 | 2008-05-28 17:58:06 +0100 | [diff] [blame] | 672 | }, |
| 673 | { /* playback only - dual interface */ |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 674 | .name = "uda1380-hifi-playback", |
Philipp Zabel | b7482f5 | 2008-05-28 17:58:06 +0100 | [diff] [blame] | 675 | .playback = { |
| 676 | .stream_name = "Playback", |
| 677 | .channels_min = 1, |
| 678 | .channels_max = 2, |
| 679 | .rates = UDA1380_RATES, |
| 680 | .formats = SNDRV_PCM_FMTBIT_S16_LE, |
| 681 | }, |
Eric Miao | 6335d05 | 2009-03-03 09:41:00 +0800 | [diff] [blame] | 682 | .ops = &uda1380_dai_ops_playback, |
Philipp Zabel | b7482f5 | 2008-05-28 17:58:06 +0100 | [diff] [blame] | 683 | }, |
| 684 | { /* capture only - dual interface*/ |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 685 | .name = "uda1380-hifi-capture", |
Philipp Zabel | b7482f5 | 2008-05-28 17:58:06 +0100 | [diff] [blame] | 686 | .capture = { |
| 687 | .stream_name = "Capture", |
| 688 | .channels_min = 1, |
| 689 | .channels_max = 2, |
| 690 | .rates = UDA1380_RATES, |
| 691 | .formats = SNDRV_PCM_FMTBIT_S16_LE, |
| 692 | }, |
Eric Miao | 6335d05 | 2009-03-03 09:41:00 +0800 | [diff] [blame] | 693 | .ops = &uda1380_dai_ops_capture, |
Philipp Zabel | b7482f5 | 2008-05-28 17:58:06 +0100 | [diff] [blame] | 694 | }, |
| 695 | }; |
Philipp Zabel | b7482f5 | 2008-05-28 17:58:06 +0100 | [diff] [blame] | 696 | |
Kuninori Morimoto | b40822d | 2018-01-29 04:44:09 +0000 | [diff] [blame] | 697 | static int uda1380_probe(struct snd_soc_component *component) |
Philipp Zabel | b7482f5 | 2008-05-28 17:58:06 +0100 | [diff] [blame] | 698 | { |
Kuninori Morimoto | b40822d | 2018-01-29 04:44:09 +0000 | [diff] [blame] | 699 | struct uda1380_platform_data *pdata =component->dev->platform_data; |
| 700 | struct uda1380_priv *uda1380 = snd_soc_component_get_drvdata(component); |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 701 | int ret; |
Philipp Zabel | b7482f5 | 2008-05-28 17:58:06 +0100 | [diff] [blame] | 702 | |
Kuninori Morimoto | b40822d | 2018-01-29 04:44:09 +0000 | [diff] [blame] | 703 | uda1380->component = component; |
Philipp Zabel | b7482f5 | 2008-05-28 17:58:06 +0100 | [diff] [blame] | 704 | |
Lars-Peter Clausen | 222e728 | 2016-11-23 10:30:11 +0100 | [diff] [blame] | 705 | if (!gpio_is_valid(pdata->gpio_power)) { |
Kuninori Morimoto | b40822d | 2018-01-29 04:44:09 +0000 | [diff] [blame] | 706 | ret = uda1380_reset(component); |
Axel Lin | 68020db | 2011-12-05 07:58:25 +0800 | [diff] [blame] | 707 | if (ret) |
Lars-Peter Clausen | 222e728 | 2016-11-23 10:30:11 +0100 | [diff] [blame] | 708 | return ret; |
Philipp Zabel | b7482f5 | 2008-05-28 17:58:06 +0100 | [diff] [blame] | 709 | } |
| 710 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 711 | INIT_WORK(&uda1380->work, uda1380_flush_work); |
| 712 | |
Philipp Zabel | b7482f5 | 2008-05-28 17:58:06 +0100 | [diff] [blame] | 713 | /* set clock input */ |
Philipp Zabel | 1abd918 | 2009-06-15 22:18:23 +0200 | [diff] [blame] | 714 | switch (pdata->dac_clk) { |
Philipp Zabel | b7482f5 | 2008-05-28 17:58:06 +0100 | [diff] [blame] | 715 | case UDA1380_DAC_CLK_SYSCLK: |
Kuninori Morimoto | b40822d | 2018-01-29 04:44:09 +0000 | [diff] [blame] | 716 | uda1380_write_reg_cache(component, UDA1380_CLK, 0); |
Philipp Zabel | b7482f5 | 2008-05-28 17:58:06 +0100 | [diff] [blame] | 717 | break; |
| 718 | case UDA1380_DAC_CLK_WSPLL: |
Kuninori Morimoto | b40822d | 2018-01-29 04:44:09 +0000 | [diff] [blame] | 719 | uda1380_write_reg_cache(component, UDA1380_CLK, |
Vasily Khoruzhick | 8614d310 | 2010-08-30 11:28:07 +0300 | [diff] [blame] | 720 | R00_DAC_CLK); |
Philipp Zabel | b7482f5 | 2008-05-28 17:58:06 +0100 | [diff] [blame] | 721 | break; |
| 722 | } |
| 723 | |
Philipp Zabel | 1abd918 | 2009-06-15 22:18:23 +0200 | [diff] [blame] | 724 | return 0; |
Philipp Zabel | 1abd918 | 2009-06-15 22:18:23 +0200 | [diff] [blame] | 725 | } |
Philipp Zabel | b7482f5 | 2008-05-28 17:58:06 +0100 | [diff] [blame] | 726 | |
Kuninori Morimoto | b40822d | 2018-01-29 04:44:09 +0000 | [diff] [blame] | 727 | static const struct snd_soc_component_driver soc_component_dev_uda1380 = { |
| 728 | .probe = uda1380_probe, |
| 729 | .read = uda1380_read_reg_cache, |
| 730 | .write = uda1380_write, |
| 731 | .set_bias_level = uda1380_set_bias_level, |
| 732 | .controls = uda1380_snd_controls, |
| 733 | .num_controls = ARRAY_SIZE(uda1380_snd_controls), |
| 734 | .dapm_widgets = uda1380_dapm_widgets, |
| 735 | .num_dapm_widgets = ARRAY_SIZE(uda1380_dapm_widgets), |
| 736 | .dapm_routes = uda1380_dapm_routes, |
| 737 | .num_dapm_routes = ARRAY_SIZE(uda1380_dapm_routes), |
| 738 | .suspend_bias_off = 1, |
| 739 | .idle_bias_on = 1, |
| 740 | .use_pmdown_time = 1, |
| 741 | .endianness = 1, |
| 742 | .non_legacy_dai_naming = 1, |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 743 | }; |
| 744 | |
Bill Pemberton | 7a79e94 | 2012-12-07 09:26:37 -0500 | [diff] [blame] | 745 | static int uda1380_i2c_probe(struct i2c_client *i2c, |
| 746 | const struct i2c_device_id *id) |
Philipp Zabel | b7482f5 | 2008-05-28 17:58:06 +0100 | [diff] [blame] | 747 | { |
Lars-Peter Clausen | 222e728 | 2016-11-23 10:30:11 +0100 | [diff] [blame] | 748 | struct uda1380_platform_data *pdata = i2c->dev.platform_data; |
Philipp Zabel | 1abd918 | 2009-06-15 22:18:23 +0200 | [diff] [blame] | 749 | struct uda1380_priv *uda1380; |
Philipp Zabel | b7482f5 | 2008-05-28 17:58:06 +0100 | [diff] [blame] | 750 | int ret; |
| 751 | |
Lars-Peter Clausen | 222e728 | 2016-11-23 10:30:11 +0100 | [diff] [blame] | 752 | if (!pdata) |
| 753 | return -EINVAL; |
| 754 | |
Axel Lin | 6ce91ad | 2011-12-26 20:58:14 +0800 | [diff] [blame] | 755 | uda1380 = devm_kzalloc(&i2c->dev, sizeof(struct uda1380_priv), |
| 756 | GFP_KERNEL); |
Philipp Zabel | 1abd918 | 2009-06-15 22:18:23 +0200 | [diff] [blame] | 757 | if (uda1380 == NULL) |
| 758 | return -ENOMEM; |
| 759 | |
Lars-Peter Clausen | 222e728 | 2016-11-23 10:30:11 +0100 | [diff] [blame] | 760 | if (gpio_is_valid(pdata->gpio_reset)) { |
| 761 | ret = devm_gpio_request_one(&i2c->dev, pdata->gpio_reset, |
| 762 | GPIOF_OUT_INIT_LOW, "uda1380 reset"); |
| 763 | if (ret) |
| 764 | return ret; |
| 765 | } |
| 766 | |
| 767 | if (gpio_is_valid(pdata->gpio_power)) { |
| 768 | ret = devm_gpio_request_one(&i2c->dev, pdata->gpio_power, |
| 769 | GPIOF_OUT_INIT_LOW, "uda1380 power"); |
| 770 | if (ret) |
| 771 | return ret; |
| 772 | } |
| 773 | |
Kuninori Morimoto | c001bf6 | 2017-11-14 01:04:08 +0000 | [diff] [blame] | 774 | uda1380->reg_cache = devm_kmemdup(&i2c->dev, |
| 775 | uda1380_reg, |
| 776 | ARRAY_SIZE(uda1380_reg) * sizeof(u16), |
| 777 | GFP_KERNEL); |
| 778 | if (!uda1380->reg_cache) |
| 779 | return -ENOMEM; |
| 780 | |
Philipp Zabel | 1abd918 | 2009-06-15 22:18:23 +0200 | [diff] [blame] | 781 | i2c_set_clientdata(i2c, uda1380); |
Kuninori Morimoto | eaa5321 | 2017-11-09 00:19:33 +0000 | [diff] [blame] | 782 | uda1380->i2c = i2c; |
Philipp Zabel | b7482f5 | 2008-05-28 17:58:06 +0100 | [diff] [blame] | 783 | |
Kuninori Morimoto | b40822d | 2018-01-29 04:44:09 +0000 | [diff] [blame] | 784 | ret = devm_snd_soc_register_component(&i2c->dev, |
| 785 | &soc_component_dev_uda1380, uda1380_dai, ARRAY_SIZE(uda1380_dai)); |
Philipp Zabel | b7482f5 | 2008-05-28 17:58:06 +0100 | [diff] [blame] | 786 | return ret; |
| 787 | } |
| 788 | |
Jean Delvare | 88fc39d | 2008-09-01 18:46:58 +0100 | [diff] [blame] | 789 | static const struct i2c_device_id uda1380_i2c_id[] = { |
| 790 | { "uda1380", 0 }, |
| 791 | { } |
| 792 | }; |
| 793 | MODULE_DEVICE_TABLE(i2c, uda1380_i2c_id); |
Philipp Zabel | b7482f5 | 2008-05-28 17:58:06 +0100 | [diff] [blame] | 794 | |
Javier Martinez Canillas | ea22a26 | 2017-04-04 15:26:28 -0400 | [diff] [blame] | 795 | static const struct of_device_id uda1380_of_match[] = { |
| 796 | { .compatible = "nxp,uda1380", }, |
| 797 | { } |
| 798 | }; |
| 799 | MODULE_DEVICE_TABLE(of, uda1380_of_match); |
| 800 | |
Philipp Zabel | b7482f5 | 2008-05-28 17:58:06 +0100 | [diff] [blame] | 801 | static struct i2c_driver uda1380_i2c_driver = { |
| 802 | .driver = { |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 803 | .name = "uda1380-codec", |
Javier Martinez Canillas | ea22a26 | 2017-04-04 15:26:28 -0400 | [diff] [blame] | 804 | .of_match_table = uda1380_of_match, |
Philipp Zabel | b7482f5 | 2008-05-28 17:58:06 +0100 | [diff] [blame] | 805 | }, |
Jean Delvare | 88fc39d | 2008-09-01 18:46:58 +0100 | [diff] [blame] | 806 | .probe = uda1380_i2c_probe, |
Jean Delvare | 88fc39d | 2008-09-01 18:46:58 +0100 | [diff] [blame] | 807 | .id_table = uda1380_i2c_id, |
Philipp Zabel | b7482f5 | 2008-05-28 17:58:06 +0100 | [diff] [blame] | 808 | }; |
Philipp Zabel | b7482f5 | 2008-05-28 17:58:06 +0100 | [diff] [blame] | 809 | |
Kuninori Morimoto | 4a5cf13 | 2016-11-17 01:12:30 +0000 | [diff] [blame] | 810 | module_i2c_driver(uda1380_i2c_driver); |
Mark Brown | 64089b8 | 2008-12-08 19:17:58 +0000 | [diff] [blame] | 811 | |
Philipp Zabel | b7482f5 | 2008-05-28 17:58:06 +0100 | [diff] [blame] | 812 | MODULE_AUTHOR("Giorgio Padrin"); |
| 813 | MODULE_DESCRIPTION("Audio support for codec Philips UDA1380"); |
| 814 | MODULE_LICENSE("GPL"); |