Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

ASoC: Add Richtek rt9123 and rt9123p support

Merge series from cy_huang@richtek.com:

This patch series adds Richtek rt9123 and rt9123p support.
It's a 3.2W mono Class-D audio amplifier.

+797
+56
Documentation/devicetree/bindings/sound/richtek,rt9123.yaml
··· 1 + # SPDX-License-Identifier: GPL-2.0-only OR BSD-2-Clause 2 + %YAML 1.2 3 + --- 4 + $id: http://devicetree.org/schemas/sound/richtek,rt9123.yaml# 5 + $schema: http://devicetree.org/meta-schemas/core.yaml# 6 + 7 + title: Richtek RT9123 Audio Amplifier 8 + 9 + maintainers: 10 + - ChiYuan Huang <cy_huang@richtek.com> 11 + 12 + description: 13 + RT9123 is a 3.2W mono Class-D audio amplifier that features high efficiency 14 + and performance with ultra-low quiescent current. The digital audio interface 15 + support various formats, including I2S, left-justified, right-justified, and 16 + TDM formats. 17 + 18 + allOf: 19 + - $ref: dai-common.yaml# 20 + 21 + properties: 22 + compatible: 23 + enum: 24 + - richtek,rt9123 25 + 26 + reg: 27 + maxItems: 1 28 + 29 + '#sound-dai-cells': 30 + const: 0 31 + 32 + enable-gpios: 33 + maxItems: 1 34 + 35 + required: 36 + - compatible 37 + - reg 38 + - '#sound-dai-cells' 39 + 40 + unevaluatedProperties: false 41 + 42 + examples: 43 + - | 44 + #include <dt-bindings/gpio/gpio.h> 45 + 46 + i2c { 47 + #address-cells = <1>; 48 + #size-cells = <0>; 49 + 50 + amplifier@5e { 51 + compatible = "richtek,rt9123"; 52 + reg = <0x5e>; 53 + enable-gpios = <&gpio 26 GPIO_ACTIVE_HIGH>; 54 + #sound-dai-cells = <0>; 55 + }; 56 + };
+48
Documentation/devicetree/bindings/sound/richtek,rt9123p.yaml
··· 1 + # SPDX-License-Identifier: GPL-2.0-only OR BSD-2-Clause 2 + %YAML 1.2 3 + --- 4 + $id: http://devicetree.org/schemas/sound/richtek,rt9123p.yaml# 5 + $schema: http://devicetree.org/meta-schemas/core.yaml# 6 + 7 + title: Richtek RT9123P Audio Amplifier 8 + 9 + maintainers: 10 + - ChiYuan Huang <cy_huang@richtek.com> 11 + 12 + description: 13 + RT9123P is a RT9123 variant which does not support I2C control. 14 + 15 + allOf: 16 + - $ref: dai-common.yaml# 17 + 18 + properties: 19 + compatible: 20 + enum: 21 + - richtek,rt9123p 22 + 23 + '#sound-dai-cells': 24 + const: 0 25 + 26 + enable-gpios: 27 + maxItems: 1 28 + 29 + enable-delay-ms: 30 + description: 31 + Delay time for 'ENABLE' pin changes intended to make I2S clocks ready to 32 + prevent speaker pop noise. The unit is in millisecond. 33 + 34 + required: 35 + - compatible 36 + - '#sound-dai-cells' 37 + 38 + unevaluatedProperties: false 39 + 40 + examples: 41 + - | 42 + #include <dt-bindings/gpio/gpio.h> 43 + 44 + amplifier { 45 + compatible = "richtek,rt9123p"; 46 + enable-gpios = <&gpio 26 GPIO_ACTIVE_HIGH>; 47 + #sound-dai-cells = <0>; 48 + };
+15
sound/soc/codecs/Kconfig
··· 234 234 imply SND_SOC_RT1318_SDW 235 235 imply SND_SOC_RT1320_SDW 236 236 imply SND_SOC_RT9120 237 + imply SND_SOC_RT9123 237 238 imply SND_SOC_RTQ9128 238 239 imply SND_SOC_SDW_MOCKUP 239 240 imply SND_SOC_SGTL5000 ··· 1823 1822 help 1824 1823 Enable support for Richtek RT9120 20W, stereo, inductor-less, 1825 1824 high-efficiency Class-D audio amplifier. 1825 + 1826 + config SND_SOC_RT9123 1827 + tristate "Richtek RT9123 Mono Class-D Amplifier" 1828 + depends on I2C 1829 + select REGMAP_I2C 1830 + help 1831 + Enable support for the I2C control mode of Richtek RT9123 3.2W mono 1832 + Class-D audio amplifier. 1833 + 1834 + config SND_SOC_RT9123P 1835 + tristate "Richtek RT9123P Mono Class-D Amplifier" 1836 + help 1837 + Enable support for the HW control mode of Richtek RT9123P 3.2W mono 1838 + Class-D audio amplifier. 1826 1839 1827 1840 config SND_SOC_RTQ9128 1828 1841 tristate "Richtek RTQ9128 45W Digital Input Amplifier"
+4
sound/soc/codecs/Makefile
··· 270 270 snd-soc-rt721-sdca-y := rt721-sdca.o rt721-sdca-sdw.o 271 271 snd-soc-rt722-sdca-y := rt722-sdca.o rt722-sdca-sdw.o 272 272 snd-soc-rt9120-y := rt9120.o 273 + snd-soc-rt9123-y := rt9123.o 274 + snd-soc-rt9123p-y := rt9123p.o 273 275 snd-soc-rtq9128-y := rtq9128.o 274 276 snd-soc-sdw-mockup-y := sdw-mockup.o 275 277 snd-soc-sgtl5000-y := sgtl5000.o ··· 686 684 obj-$(CONFIG_SND_SOC_RT721_SDCA_SDW) += snd-soc-rt721-sdca.o 687 685 obj-$(CONFIG_SND_SOC_RT722_SDCA_SDW) += snd-soc-rt722-sdca.o 688 686 obj-$(CONFIG_SND_SOC_RT9120) += snd-soc-rt9120.o 687 + obj-$(CONFIG_SND_SOC_RT9123) += snd-soc-rt9123.o 688 + obj-$(CONFIG_SND_SOC_RT9123P) += snd-soc-rt9123p.o 689 689 obj-$(CONFIG_SND_SOC_RTQ9128) += snd-soc-rtq9128.o 690 690 obj-$(CONFIG_SND_SOC_SDW_MOCKUP) += snd-soc-sdw-mockup.o 691 691 obj-$(CONFIG_SND_SOC_SGTL5000) += snd-soc-sgtl5000.o
+503
sound/soc/codecs/rt9123.c
··· 1 + // SPDX-License-Identifier: GPL-2.0-only 2 + // 3 + // rt9123.c -- RT9123 (SW I2C Mode) ALSA SoC Codec driver 4 + // 5 + // Author: ChiYuan Huang <cy_huang@richtek.com> 6 + 7 + #include <linux/acpi.h> 8 + #include <linux/bitfield.h> 9 + #include <linux/bits.h> 10 + #include <linux/byteorder/generic.h> 11 + #include <linux/delay.h> 12 + #include <linux/err.h> 13 + #include <linux/gpio/consumer.h> 14 + #include <linux/i2c.h> 15 + #include <linux/kernel.h> 16 + #include <linux/mod_devicetable.h> 17 + #include <linux/module.h> 18 + #include <linux/of.h> 19 + #include <linux/pm_runtime.h> 20 + #include <linux/property.h> 21 + #include <linux/regmap.h> 22 + #include <sound/pcm.h> 23 + #include <sound/pcm_params.h> 24 + #include <sound/soc.h> 25 + #include <sound/soc-dai.h> 26 + #include <sound/soc-dapm.h> 27 + #include <sound/tlv.h> 28 + 29 + #define RT9123_REG_AMPCTRL 0x01 30 + #define RT9123_REG_I2SOPT 0x02 31 + #define RT9123_REG_TDMRX 0x03 32 + #define RT9123_REG_SILVOLEN 0x04 33 + #define RT9123_REG_VOLGAIN 0x12 34 + #define RT9123_REG_ANAFLAG 0x36 35 + #define RT9123_REG_COMBOID 0xF7 36 + 37 + #define RT9123_MASK_SWRST BIT(15) 38 + #define RT9123_MASK_SWMUTE BIT(14) 39 + #define RT9123_MASK_AMPON BIT(12) 40 + #define RT9123_MASK_AUDBIT GENMASK(14, 12) 41 + #define RT9123_MASK_AUDFMT GENMASK(11, 8) 42 + #define RT9123_MASK_TDMRXLOC GENMASK(4, 0) 43 + #define RT9123_MASK_VENID GENMASK(15, 4) 44 + 45 + #define RT9123_FIXED_VENID 0x340 46 + 47 + struct rt9123_priv { 48 + struct gpio_desc *enable; 49 + unsigned int dai_fmt; 50 + int tdm_slots; 51 + int tdm_slot_width; 52 + }; 53 + 54 + static int rt9123_enable_event(struct snd_soc_dapm_widget *w, struct snd_kcontrol *kcontrol, 55 + int event) 56 + { 57 + struct snd_soc_component *comp = snd_soc_dapm_to_component(w->dapm); 58 + struct device *dev = comp->dev; 59 + unsigned int enable; 60 + int ret; 61 + 62 + switch (event) { 63 + case SND_SOC_DAPM_POST_PMU: 64 + enable = 1; 65 + break; 66 + case SND_SOC_DAPM_POST_PMD: 67 + enable = 0; 68 + break; 69 + default: 70 + return -EINVAL; 71 + } 72 + 73 + ret = pm_runtime_resume_and_get(dev); 74 + if (ret) 75 + return ret; 76 + 77 + /* AMPON bit is located in volatile RG, use pm_runtime to guarantee the RG access */ 78 + snd_soc_component_write_field(comp, RT9123_REG_AMPCTRL, RT9123_MASK_AMPON, enable); 79 + 80 + pm_runtime_mark_last_busy(dev); 81 + pm_runtime_put_autosuspend(dev); 82 + 83 + return 0; 84 + } 85 + 86 + static const struct snd_soc_dapm_widget rt9123_dapm_widgets[] = { 87 + SND_SOC_DAPM_OUTPUT("SPK"), 88 + SND_SOC_DAPM_OUT_DRV_E("Amp Drv", SND_SOC_NOPM, 0, 0, NULL, 0, rt9123_enable_event, 89 + SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD), 90 + }; 91 + 92 + static const struct snd_soc_dapm_route rt9123_dapm_routes[] = { 93 + { "Amp Drv", NULL, "HiFi Playback" }, 94 + { "SPK", NULL, "Amp Drv" }, 95 + }; 96 + 97 + static const DECLARE_TLV_DB_SCALE(dig_tlv, -10375, 25, 0); 98 + static const DECLARE_TLV_DB_RANGE(ana_tlv, 99 + 0, 0, TLV_DB_SCALE_ITEM(-1200, 0, 0), 100 + 1, 9, TLV_DB_SCALE_ITEM(0, 150, 0), 101 + 10, 10, TLV_DB_SCALE_ITEM(1400, 0, 0)); 102 + static const char * const pwmfreq_text[] = { "300KHz", "325KHz", "350KHz", "375KHz" }; 103 + static const struct soc_enum rt9123_pwm_freq_enum = 104 + SOC_ENUM_SINGLE(RT9123_REG_AMPCTRL, 4, ARRAY_SIZE(pwmfreq_text), pwmfreq_text); 105 + static const char * const i2sch_text[] = { "(L+R)/2", "LCH", "RCH", "(L+R)/2" }; 106 + static const struct soc_enum rt9123_i2sch_select_enum = 107 + SOC_ENUM_SINGLE(RT9123_REG_I2SOPT, 4, ARRAY_SIZE(i2sch_text), i2sch_text); 108 + 109 + static int rt9123_kcontrol_name_comp(struct snd_kcontrol *kcontrol, const char *s) 110 + { 111 + struct snd_soc_component *comp = snd_soc_kcontrol_component(kcontrol); 112 + const char *kctlname = kcontrol->id.name; 113 + 114 + if (comp && comp->name_prefix) 115 + kctlname += strlen(comp->name_prefix) + 1; 116 + 117 + return strcmp(kctlname, s); 118 + } 119 + 120 + static int rt9123_xhandler_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol) 121 + { 122 + struct snd_soc_component *comp = snd_soc_kcontrol_component(kcontrol); 123 + struct device *dev = comp->dev; 124 + int ret; 125 + 126 + ret = pm_runtime_resume_and_get(dev); 127 + if (ret) 128 + return ret; 129 + 130 + /* 131 + * Since the RG bitfield for 'Speaker Volume' and 'PWM Frequency Select' are located in 132 + * volatile RG address, special handling here with pm runtime API to guarantee RG read 133 + * operation. 134 + */ 135 + if (rt9123_kcontrol_name_comp(kcontrol, "Speaker Volume") == 0) 136 + ret = snd_soc_get_volsw(kcontrol, ucontrol); 137 + else 138 + ret = snd_soc_get_enum_double(kcontrol, ucontrol); 139 + 140 + if (ret < 0) 141 + dev_err(dev, "Failed to get control (%d)\n", ret); 142 + 143 + pm_runtime_mark_last_busy(dev); 144 + pm_runtime_put_autosuspend(dev); 145 + return ret; 146 + } 147 + 148 + static int rt9123_xhandler_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol) 149 + { 150 + struct snd_soc_component *comp = snd_soc_kcontrol_component(kcontrol); 151 + struct device *dev = comp->dev; 152 + int ret; 153 + 154 + ret = pm_runtime_resume_and_get(dev); 155 + if (ret) 156 + return ret; 157 + 158 + /* 159 + * Since the RG bitfield for 'Speaker Volume' and 'PWM Frequency Select' are located in 160 + * volatile RG address, special handling here with pm runtime API to guarantee RG write 161 + * operation. 162 + */ 163 + if (rt9123_kcontrol_name_comp(kcontrol, "Speaker Volume") == 0) 164 + ret = snd_soc_put_volsw(kcontrol, ucontrol); 165 + else 166 + ret = snd_soc_put_enum_double(kcontrol, ucontrol); 167 + 168 + if (ret < 0) 169 + dev_err(dev, "Failed to put control (%d)\n", ret); 170 + 171 + pm_runtime_mark_last_busy(dev); 172 + pm_runtime_put_autosuspend(dev); 173 + return ret; 174 + } 175 + 176 + static const struct snd_kcontrol_new rt9123_controls[] = { 177 + SOC_SINGLE_TLV("Master Volume", RT9123_REG_VOLGAIN, 2, 511, 1, dig_tlv), 178 + SOC_SINGLE_EXT_TLV("Speaker Volume", RT9123_REG_AMPCTRL, 0, 10, 0, rt9123_xhandler_get, 179 + rt9123_xhandler_put, ana_tlv), 180 + SOC_ENUM_EXT("PWM Frequency Select", rt9123_pwm_freq_enum, rt9123_xhandler_get, 181 + rt9123_xhandler_put), 182 + SOC_ENUM("I2S CH Select", rt9123_i2sch_select_enum), 183 + SOC_SINGLE("Silence Detect Switch", RT9123_REG_SILVOLEN, 14, 1, 0), 184 + }; 185 + 186 + static const struct snd_soc_component_driver rt9123_comp_driver = { 187 + .controls = rt9123_controls, 188 + .num_controls = ARRAY_SIZE(rt9123_controls), 189 + .dapm_widgets = rt9123_dapm_widgets, 190 + .num_dapm_widgets = ARRAY_SIZE(rt9123_dapm_widgets), 191 + .dapm_routes = rt9123_dapm_routes, 192 + .num_dapm_routes = ARRAY_SIZE(rt9123_dapm_routes), 193 + .use_pmdown_time = 1, 194 + .endianness = 1, 195 + }; 196 + 197 + static int rt9123_dai_set_format(struct snd_soc_dai *dai, unsigned int fmt) 198 + { 199 + struct rt9123_priv *rt9123 = snd_soc_dai_get_drvdata(dai); 200 + 201 + rt9123->dai_fmt = fmt; 202 + return 0; 203 + } 204 + 205 + static int rt9123_dai_set_tdm_slot(struct snd_soc_dai *dai, unsigned int tx_mask, 206 + unsigned int rx_mask, int slots, int slot_width) 207 + { 208 + struct rt9123_priv *rt9123 = snd_soc_dai_get_drvdata(dai); 209 + struct snd_soc_component *comp = dai->component; 210 + struct device *dev = dai->dev; 211 + unsigned int rx_loc; 212 + 213 + dev_dbg(dev, "(slots, slot_width) = (%d, %d), (txmask, rxmask) = 0x%x, 0x%x\n", slots, 214 + slot_width, tx_mask, rx_mask); 215 + 216 + if (slots <= 0 || slot_width <= 0 || slots % 2 || slot_width % 8 || 217 + slots * slot_width > 256) { 218 + dev_err(dev, "Invalid slot parameter (%d, %d)\n", slots, slot_width); 219 + return -EINVAL; 220 + } 221 + 222 + if (!rx_mask || hweight_long(rx_mask) > 1 || ffs(rx_mask) > slots) { 223 + dev_err(dev, "Invalid rx_mask 0x%08x, slots = %d\n", rx_mask, slots); 224 + return -EINVAL; 225 + } 226 + 227 + /* Configure rx channel data location */ 228 + rx_loc = (ffs(rx_mask) - 1) * slot_width / 8; 229 + snd_soc_component_write_field(comp, RT9123_REG_TDMRX, RT9123_MASK_TDMRXLOC, rx_loc); 230 + 231 + rt9123->tdm_slots = slots; 232 + rt9123->tdm_slot_width = slot_width; 233 + 234 + return 0; 235 + } 236 + 237 + static int rt9123_dai_hw_params(struct snd_pcm_substream *substream, 238 + struct snd_pcm_hw_params *param, struct snd_soc_dai *dai) 239 + { 240 + struct rt9123_priv *rt9123 = snd_soc_dai_get_drvdata(dai); 241 + struct snd_soc_component *comp = dai->component; 242 + unsigned int fmtval, width, slot_width; 243 + struct device *dev = dai->dev; 244 + unsigned int audfmt, audbit; 245 + 246 + fmtval = FIELD_GET(SND_SOC_DAIFMT_FORMAT_MASK, rt9123->dai_fmt); 247 + if (rt9123->tdm_slots && fmtval != SND_SOC_DAIFMT_DSP_A && fmtval != SND_SOC_DAIFMT_DSP_B) { 248 + dev_err(dev, "TDM only can support DSP_A or DSP_B format\n"); 249 + return -EINVAL; 250 + } 251 + 252 + switch (fmtval) { 253 + case SND_SOC_DAIFMT_I2S: 254 + audfmt = 0; 255 + break; 256 + case SND_SOC_DAIFMT_LEFT_J: 257 + audfmt = 1; 258 + break; 259 + case SND_SOC_DAIFMT_RIGHT_J: 260 + audfmt = 2; 261 + break; 262 + case SND_SOC_DAIFMT_DSP_B: 263 + audfmt = rt9123->tdm_slots ? 4 : 3; 264 + break; 265 + case SND_SOC_DAIFMT_DSP_A: 266 + audfmt = rt9123->tdm_slots ? 12 : 11; 267 + break; 268 + default: 269 + dev_err(dev, "Unsupported format %d\n", fmtval); 270 + return -EINVAL; 271 + } 272 + 273 + switch (width = params_width(param)) { 274 + case 16: 275 + audbit = 0; 276 + break; 277 + case 20: 278 + audbit = 1; 279 + break; 280 + case 24: 281 + audbit = 2; 282 + break; 283 + case 32: 284 + audbit = 3; 285 + break; 286 + case 8: 287 + audbit = 4; 288 + break; 289 + default: 290 + dev_err(dev, "Unsupported width %d\n", width); 291 + return -EINVAL; 292 + } 293 + 294 + slot_width = params_physical_width(param); 295 + if (rt9123->tdm_slots && slot_width > rt9123->tdm_slot_width) { 296 + dev_err(dev, "Slot width is larger than TDM slot width\n"); 297 + return -EINVAL; 298 + } 299 + 300 + snd_soc_component_write_field(comp, RT9123_REG_I2SOPT, RT9123_MASK_AUDFMT, audfmt); 301 + snd_soc_component_write_field(comp, RT9123_REG_I2SOPT, RT9123_MASK_AUDBIT, audbit); 302 + 303 + return 0; 304 + } 305 + 306 + static const struct snd_soc_dai_ops rt9123_dai_ops = { 307 + .set_fmt = rt9123_dai_set_format, 308 + .set_tdm_slot = rt9123_dai_set_tdm_slot, 309 + .hw_params = rt9123_dai_hw_params, 310 + }; 311 + 312 + static struct snd_soc_dai_driver rt9123_dai_driver = { 313 + .name = "HiFi", 314 + .playback = { 315 + .stream_name = "HiFi Playback", 316 + .formats = SNDRV_PCM_FMTBIT_S16 | SNDRV_PCM_FMTBIT_S24 | 317 + SNDRV_PCM_FMTBIT_S32, 318 + .rates = SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_16000 | 319 + SNDRV_PCM_RATE_22050 | SNDRV_PCM_RATE_24000 | 320 + SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | 321 + SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_88200 | 322 + SNDRV_PCM_RATE_96000, 323 + .rate_min = 8000, 324 + .rate_max = 96000, 325 + .channels_min = 1, 326 + .channels_max = 2, 327 + }, 328 + .ops = &rt9123_dai_ops, 329 + }; 330 + 331 + static bool rt9123_readable_reg(struct device *dev, unsigned int reg) 332 + { 333 + switch (reg) { 334 + case 0x00 ... 0x05: 335 + case 0x12 ... 0x13: 336 + case 0x20 ... 0x21: 337 + case 0x36: 338 + return true; 339 + default: 340 + return false; 341 + } 342 + } 343 + 344 + static bool rt9123_writeable_reg(struct device *dev, unsigned int reg) 345 + { 346 + switch (reg) { 347 + case 0x01 ... 0x05: 348 + case 0x12 ... 0x13: 349 + case 0x20 ... 0x21: 350 + return true; 351 + default: 352 + return false; 353 + } 354 + } 355 + 356 + static bool rt9123_volatile_reg(struct device *dev, unsigned int reg) 357 + { 358 + switch (reg) { 359 + case 0x01: 360 + case 0x20: 361 + case 0x36: 362 + return true; 363 + default: 364 + return false; 365 + } 366 + } 367 + 368 + static const struct regmap_config rt9123_regmap_config = { 369 + .name = "rt9123", 370 + .reg_bits = 8, 371 + .val_bits = 16, 372 + .val_format_endian = REGMAP_ENDIAN_BIG, 373 + .readable_reg = rt9123_readable_reg, 374 + .writeable_reg = rt9123_writeable_reg, 375 + .volatile_reg = rt9123_volatile_reg, 376 + .cache_type = REGCACHE_MAPLE, 377 + .num_reg_defaults_raw = RT9123_REG_ANAFLAG + 1, 378 + }; 379 + 380 + static int rt9123_i2c_probe(struct i2c_client *i2c) 381 + { 382 + struct device *dev = &i2c->dev; 383 + struct rt9123_priv *rt9123; 384 + struct regmap *regmap; 385 + __be16 value; 386 + u16 venid; 387 + int ret; 388 + 389 + rt9123 = devm_kzalloc(dev, sizeof(*rt9123), GFP_KERNEL); 390 + if (!rt9123) 391 + return -ENOMEM; 392 + 393 + rt9123->enable = devm_gpiod_get_optional(dev, "enable", GPIOD_OUT_HIGH); 394 + if (IS_ERR(rt9123->enable)) 395 + return PTR_ERR(rt9123->enable); 396 + else if (rt9123->enable) 397 + usleep_range(250, 350); 398 + else 399 + dev_dbg(dev, "No 'enable' GPIO specified, treat it as default on\n"); 400 + 401 + /* Check vendor id information */ 402 + ret = i2c_smbus_read_word_data(i2c, RT9123_REG_COMBOID); 403 + if (ret < 0) 404 + return dev_err_probe(dev, ret, "Failed to read vendor-id\n"); 405 + 406 + venid = be16_to_cpu(ret); 407 + if ((venid & RT9123_MASK_VENID) != RT9123_FIXED_VENID) 408 + return dev_err_probe(dev, -ENODEV, "Incorrect vendor-id 0x%04x\n", venid); 409 + 410 + /* Trigger RG reset before regmap init cache */ 411 + value = cpu_to_be16(RT9123_MASK_SWRST); 412 + ret = i2c_smbus_write_word_data(i2c, RT9123_REG_AMPCTRL, value); 413 + if (ret) 414 + return dev_err_probe(dev, ret, "Failed to trigger RG reset\n"); 415 + 416 + /* Need to wait 10ms for the reset to complete */ 417 + usleep_range(10000, 11000); 418 + 419 + regmap = devm_regmap_init_i2c(i2c, &rt9123_regmap_config); 420 + if (IS_ERR(regmap)) 421 + return dev_err_probe(dev, PTR_ERR(regmap), "Failed to init regmap\n"); 422 + 423 + i2c_set_clientdata(i2c, rt9123); 424 + 425 + pm_runtime_set_autosuspend_delay(dev, 500); 426 + pm_runtime_use_autosuspend(dev); 427 + pm_runtime_set_active(dev); 428 + ret = devm_pm_runtime_enable(dev); 429 + if (ret) 430 + return dev_err_probe(dev, ret, "Failed to enable pm runtime\n"); 431 + 432 + return devm_snd_soc_register_component(dev, &rt9123_comp_driver, &rt9123_dai_driver, 1); 433 + } 434 + 435 + #ifdef CONFIG_PM 436 + static int rt9123_runtime_suspend(struct device *dev) 437 + { 438 + struct rt9123_priv *rt9123 = dev_get_drvdata(dev); 439 + struct regmap *regmap = dev_get_regmap(dev, NULL); 440 + 441 + if (rt9123->enable) { 442 + regcache_cache_only(regmap, true); 443 + regcache_mark_dirty(regmap); 444 + gpiod_set_value(rt9123->enable, 0); 445 + } 446 + 447 + return 0; 448 + } 449 + 450 + static int rt9123_runtime_resume(struct device *dev) 451 + { 452 + struct rt9123_priv *rt9123 = dev_get_drvdata(dev); 453 + struct regmap *regmap = dev_get_regmap(dev, NULL); 454 + int ret; 455 + 456 + if (rt9123->enable) { 457 + gpiod_set_value(rt9123->enable, 1); 458 + usleep_range(250, 350); 459 + 460 + regcache_cache_only(regmap, false); 461 + ret = regcache_sync(regmap); 462 + if (ret) 463 + return ret; 464 + } 465 + 466 + return 0; 467 + } 468 + #endif 469 + 470 + static const struct dev_pm_ops rt9123_dev_pm_ops = { 471 + SET_RUNTIME_PM_OPS(rt9123_runtime_suspend, rt9123_runtime_resume, NULL) 472 + }; 473 + 474 + #ifdef CONFIG_OF 475 + static const struct of_device_id rt9123_device_id[] = { 476 + { .compatible = "richtek,rt9123" }, 477 + {} 478 + }; 479 + MODULE_DEVICE_TABLE(of, rt9123_device_id); 480 + #endif 481 + 482 + #ifdef CONFIG_ACPI 483 + static const struct acpi_device_id rt9123_acpi_match[] = { 484 + { "RT9123", 0 }, 485 + {} 486 + }; 487 + MODULE_DEVICE_TABLE(acpi, rt9123_acpi_match); 488 + #endif 489 + 490 + static struct i2c_driver rt9123_i2c_driver = { 491 + .driver = { 492 + .name = "rt9123", 493 + .of_match_table = of_match_ptr(rt9123_device_id), 494 + .acpi_match_table = ACPI_PTR(rt9123_acpi_match), 495 + .pm = pm_ptr(&rt9123_dev_pm_ops), 496 + }, 497 + .probe = rt9123_i2c_probe, 498 + }; 499 + module_i2c_driver(rt9123_i2c_driver); 500 + 501 + MODULE_AUTHOR("ChiYuan Huang <cy_huang@richtek.com>"); 502 + MODULE_DESCRIPTION("ASoC rt9123 Driver"); 503 + MODULE_LICENSE("GPL");
+171
sound/soc/codecs/rt9123p.c
··· 1 + // SPDX-License-Identifier: GPL-2.0-only 2 + // 3 + // rt9123p.c -- RT9123 (HW Mode) ALSA SoC Codec driver 4 + // 5 + // Author: ChiYuan Huang <cy_huang@richtek.com> 6 + 7 + #include <linux/acpi.h> 8 + #include <linux/delay.h> 9 + #include <linux/err.h> 10 + #include <linux/gpio/consumer.h> 11 + #include <linux/kernel.h> 12 + #include <linux/mod_devicetable.h> 13 + #include <linux/module.h> 14 + #include <linux/of.h> 15 + #include <linux/platform_device.h> 16 + #include <linux/property.h> 17 + #include <sound/pcm.h> 18 + #include <sound/soc.h> 19 + #include <sound/soc-dai.h> 20 + #include <sound/soc-dapm.h> 21 + 22 + struct rt9123p_priv { 23 + struct gpio_desc *enable; 24 + unsigned int enable_delay; 25 + int enable_switch; 26 + }; 27 + 28 + static int rt9123p_daiops_trigger(struct snd_pcm_substream *substream, int cmd, 29 + struct snd_soc_dai *dai) 30 + { 31 + struct snd_soc_component *comp = dai->component; 32 + struct rt9123p_priv *rt9123p = snd_soc_component_get_drvdata(comp); 33 + 34 + if (!rt9123p->enable) 35 + return 0; 36 + 37 + switch (cmd) { 38 + case SNDRV_PCM_TRIGGER_START: 39 + case SNDRV_PCM_TRIGGER_RESUME: 40 + case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: 41 + mdelay(rt9123p->enable_delay); 42 + if (rt9123p->enable_switch) { 43 + gpiod_set_value(rt9123p->enable, 1); 44 + dev_dbg(comp->dev, "set enable to 1"); 45 + } 46 + break; 47 + case SNDRV_PCM_TRIGGER_STOP: 48 + case SNDRV_PCM_TRIGGER_SUSPEND: 49 + case SNDRV_PCM_TRIGGER_PAUSE_PUSH: 50 + gpiod_set_value(rt9123p->enable, 0); 51 + dev_dbg(comp->dev, "set enable to 0"); 52 + break; 53 + default: 54 + break; 55 + } 56 + 57 + return 0; 58 + } 59 + 60 + static int rt9123p_enable_event(struct snd_soc_dapm_widget *w, struct snd_kcontrol *kcontrol, 61 + int event) 62 + { 63 + struct snd_soc_component *comp = snd_soc_dapm_to_component(w->dapm); 64 + struct rt9123p_priv *rt9123p = snd_soc_component_get_drvdata(comp); 65 + 66 + if (event & SND_SOC_DAPM_POST_PMU) 67 + rt9123p->enable_switch = 1; 68 + else if (event & SND_SOC_DAPM_POST_PMD) 69 + rt9123p->enable_switch = 0; 70 + 71 + return 0; 72 + } 73 + 74 + static const struct snd_soc_dapm_widget rt9123p_dapm_widgets[] = { 75 + SND_SOC_DAPM_OUTPUT("SPK"), 76 + SND_SOC_DAPM_OUT_DRV_E("Amp Drv", SND_SOC_NOPM, 0, 0, NULL, 0, rt9123p_enable_event, 77 + SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_POST_PMD), 78 + }; 79 + 80 + static const struct snd_soc_dapm_route rt9123p_dapm_routes[] = { 81 + {"Amp Drv", NULL, "HiFi Playback"}, 82 + {"SPK", NULL, "Amp Drv"}, 83 + }; 84 + 85 + static const struct snd_soc_component_driver rt9123p_comp_driver = { 86 + .dapm_widgets = rt9123p_dapm_widgets, 87 + .num_dapm_widgets = ARRAY_SIZE(rt9123p_dapm_widgets), 88 + .dapm_routes = rt9123p_dapm_routes, 89 + .num_dapm_routes = ARRAY_SIZE(rt9123p_dapm_routes), 90 + .idle_bias_on = 1, 91 + .use_pmdown_time = 1, 92 + .endianness = 1, 93 + }; 94 + 95 + static const struct snd_soc_dai_ops rt9123p_dai_ops = { 96 + .trigger = rt9123p_daiops_trigger, 97 + }; 98 + 99 + static struct snd_soc_dai_driver rt9123p_dai_driver = { 100 + .name = "HiFi", 101 + .playback = { 102 + .stream_name = "HiFi Playback", 103 + .formats = SNDRV_PCM_FMTBIT_S16 | SNDRV_PCM_FMTBIT_S24 | 104 + SNDRV_PCM_FMTBIT_S32, 105 + .rates = SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_16000 | 106 + SNDRV_PCM_RATE_22050 | SNDRV_PCM_RATE_24000 | 107 + SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | 108 + SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_88200 | 109 + SNDRV_PCM_RATE_96000, 110 + .rate_min = 8000, 111 + .rate_max = 96000, 112 + .channels_min = 1, 113 + .channels_max = 2, 114 + }, 115 + .ops = &rt9123p_dai_ops, 116 + }; 117 + 118 + static int rt9123p_platform_probe(struct platform_device *pdev) 119 + { 120 + struct device *dev = &pdev->dev; 121 + struct rt9123p_priv *rt9123p; 122 + int ret; 123 + 124 + rt9123p = devm_kzalloc(dev, sizeof(*rt9123p), GFP_KERNEL); 125 + if (!rt9123p) 126 + return -ENOMEM; 127 + 128 + rt9123p->enable = devm_gpiod_get_optional(dev, "enable", GPIOD_OUT_LOW); 129 + if (IS_ERR(rt9123p->enable)) 130 + return PTR_ERR(rt9123p->enable); 131 + 132 + ret = device_property_read_u32(dev, "enable-delay-ms", &rt9123p->enable_delay); 133 + if (ret) { 134 + rt9123p->enable_delay = 0; 135 + dev_dbg(dev, "no optional property 'enable-delay-ms' found, default: no delay\n"); 136 + } 137 + 138 + platform_set_drvdata(pdev, rt9123p); 139 + 140 + return devm_snd_soc_register_component(dev, &rt9123p_comp_driver, &rt9123p_dai_driver, 1); 141 + } 142 + 143 + #ifdef CONFIG_OF 144 + static const struct of_device_id rt9123p_device_id[] = { 145 + { .compatible = "richtek,rt9123p" }, 146 + {} 147 + }; 148 + MODULE_DEVICE_TABLE(of, rt9123p_device_id); 149 + #endif 150 + 151 + #ifdef CONFIG_ACPI 152 + static const struct acpi_device_id rt9123p_acpi_match[] = { 153 + { "RT9123P", 0 }, 154 + {} 155 + }; 156 + MODULE_DEVICE_TABLE(acpi, rt9123p_acpi_match); 157 + #endif 158 + 159 + static struct platform_driver rt9123p_platform_driver = { 160 + .driver = { 161 + .name = "rt9123p", 162 + .of_match_table = of_match_ptr(rt9123p_device_id), 163 + .acpi_match_table = ACPI_PTR(rt9123p_acpi_match), 164 + }, 165 + .probe = rt9123p_platform_probe, 166 + }; 167 + module_platform_driver(rt9123p_platform_driver); 168 + 169 + MODULE_AUTHOR("ChiYuan Huang <cy_huang@richtek.com>"); 170 + MODULE_DESCRIPTION("ASoC rt9123p Driver"); 171 + MODULE_LICENSE("GPL");