|
| 1 | +// SPDX-License-Identifier: GPL-2.0 |
| 2 | +// |
| 3 | +// MAX98390 HDA driver |
| 4 | +// |
| 5 | + |
| 6 | +#include <linux/module.h> |
| 7 | +#include <linux/regmap.h> |
| 8 | +#include <sound/hda_codec.h> |
| 9 | +#include <sound/soc.h> |
| 10 | +#include "hda_local.h" |
| 11 | +#include "hda_component.h" |
| 12 | +#include "../generic.h" |
| 13 | +#include "max98390_hda.h" |
| 14 | +#include "max98390_hda_filters.h" |
| 15 | +#include "../../../soc/codecs/max98390.h" |
| 16 | + |
| 17 | +static void max98390_hda_playback_hook(struct device *dev, int action) |
| 18 | +{ |
| 19 | + struct max98390_hda_priv *priv = dev_get_drvdata(dev); |
| 20 | + int ret; |
| 21 | + |
| 22 | + switch (action) { |
| 23 | + case HDA_GEN_PCM_ACT_OPEN: |
| 24 | + |
| 25 | + /* Enable global and speaker amp */ |
| 26 | + ret = regmap_write(priv->regmap, MAX98390_R23FF_GLOBAL_EN, 0x01); |
| 27 | + if (ret < 0) |
| 28 | + dev_err(dev, "Failed to write GLOBAL_EN: %d\n", ret); |
| 29 | + |
| 30 | + ret = regmap_write(priv->regmap, MAX98390_R203A_AMP_EN, 0x81); |
| 31 | + if (ret < 0) |
| 32 | + dev_err(dev, "Failed to write AMP_EN: %d\n", ret); |
| 33 | + |
| 34 | + break; |
| 35 | + |
| 36 | + case HDA_GEN_PCM_ACT_CLOSE: |
| 37 | + /* Disable speaker amp and global */ |
| 38 | + regmap_write(priv->regmap, MAX98390_R203A_AMP_EN, 0x80); |
| 39 | + regmap_write(priv->regmap, MAX98390_R23FF_GLOBAL_EN, 0x00); |
| 40 | + break; |
| 41 | + |
| 42 | + default: |
| 43 | + break; |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +static int max98390_hda_bind(struct device *dev, struct device *master, void *master_data) |
| 48 | +{ |
| 49 | + struct max98390_hda_priv *priv = dev_get_drvdata(dev); |
| 50 | + struct hda_component_parent *parent = master_data; |
| 51 | + struct hda_component *comp; |
| 52 | + |
| 53 | + comp = hda_component_from_index(parent, priv->index); |
| 54 | + if (!comp) |
| 55 | + return -EINVAL; |
| 56 | + |
| 57 | + comp->dev = dev; |
| 58 | + strscpy(comp->name, dev_name(dev), sizeof(comp->name)); |
| 59 | + comp->playback_hook = max98390_hda_playback_hook; |
| 60 | + |
| 61 | + dev_info(dev, "MAX98390 HDA component bound (index %d)\n", priv->index); |
| 62 | + |
| 63 | + return 0; |
| 64 | +} |
| 65 | + |
| 66 | +static void max98390_hda_unbind(struct device *dev, struct device *master, void *master_data) |
| 67 | +{ |
| 68 | + struct max98390_hda_priv *priv = dev_get_drvdata(dev); |
| 69 | + struct hda_component_parent *parent = master_data; |
| 70 | + struct hda_component *comp; |
| 71 | + |
| 72 | + comp = hda_component_from_index(parent, priv->index); |
| 73 | + if (comp && comp->dev == dev) { |
| 74 | + comp->dev = NULL; |
| 75 | + memset(comp->name, 0, sizeof(comp->name)); |
| 76 | + comp->playback_hook = NULL; |
| 77 | + } |
| 78 | + |
| 79 | + dev_info(dev, "MAX98390 HDA component unbound\n"); |
| 80 | +} |
| 81 | + |
| 82 | +static const struct component_ops max98390_hda_comp_ops = { |
| 83 | + .bind = max98390_hda_bind, |
| 84 | + .unbind = max98390_hda_unbind, |
| 85 | +}; |
| 86 | + |
| 87 | +static int max98390_hda_init(struct max98390_hda_priv *priv) |
| 88 | +{ |
| 89 | + int ret; |
| 90 | + unsigned int reg, global_en, amp_en, pcm_rx; |
| 91 | + |
| 92 | + /* Check device ID */ |
| 93 | + ret = regmap_read(priv->regmap, MAX98390_R24FF_REV_ID, ®); |
| 94 | + if (ret < 0) { |
| 95 | + return ret; |
| 96 | + } |
| 97 | + |
| 98 | + /* Software reset */ |
| 99 | + ret = regmap_write(priv->regmap, MAX98390_SOFTWARE_RESET, 0x01); |
| 100 | + if (ret < 0) { |
| 101 | + return ret; |
| 102 | + } |
| 103 | + msleep(20); |
| 104 | + |
| 105 | + /* Basic register initialization (minimal setup for HDA) */ |
| 106 | + regmap_write(priv->regmap, MAX98390_CLK_MON, 0x6f); |
| 107 | + regmap_write(priv->regmap, MAX98390_DAT_MON, 0x00); |
| 108 | + regmap_write(priv->regmap, MAX98390_PWR_GATE_CTL, 0x00); |
| 109 | + regmap_write(priv->regmap, MAX98390_PCM_RX_EN_A, 0x03); |
| 110 | + regmap_write(priv->regmap, MAX98390_ENV_TRACK_VOUT_HEADROOM, 0x0e); |
| 111 | + regmap_write(priv->regmap, MAX98390_BOOST_BYPASS1, 0x46); |
| 112 | + regmap_write(priv->regmap, MAX98390_FET_SCALING3, 0x03); |
| 113 | + |
| 114 | + /* PCM/I2S configuration - CRITICAL for correct audio format */ |
| 115 | + /* 0xC0 = I2S mode, 32-bit samples (standard for HDA) */ |
| 116 | + regmap_write(priv->regmap, MAX98390_PCM_MODE_CFG, 0xc0); |
| 117 | + regmap_write(priv->regmap, MAX98390_PCM_MASTER_MODE, 0x1c); |
| 118 | + regmap_write(priv->regmap, MAX98390_PCM_CLK_SETUP, 0x44); |
| 119 | + regmap_write(priv->regmap, MAX98390_PCM_SR_SETUP, 0x08); |
| 120 | + |
| 121 | + /* RESET EN - Write 0x00 to 0x23FF */ |
| 122 | + regmap_write(priv->regmap, MAX98390_R23FF_GLOBAL_EN, 0x00); |
| 123 | + |
| 124 | + /* Wait 50ms */ |
| 125 | + msleep(50); |
| 126 | + |
| 127 | + /* RESET SPK_EN - Write 0x80 to 0x203A */ |
| 128 | + regmap_write(priv->regmap, MAX98390_R203A_AMP_EN, 0x80); |
| 129 | + |
| 130 | + /* RESET DSP_GLOBAL_EN - Write 0x00 to 0x23E1 */ |
| 131 | + regmap_write(priv->regmap, MAX98390_R23E1_DSP_GLOBAL_EN, 0x00); |
| 132 | + |
| 133 | + /* Step 6: Write non-DSM registers (0x2000-0x2084) - done in configure_filters */ |
| 134 | + /* Step 7: Write DSM registers (0x2100-0x23E0) - done in configure_filters */ |
| 135 | + /* Step 8-10: Enable DSP and amp - done in configure_filters */ |
| 136 | + max98390_configure_filters(priv); |
| 137 | + |
| 138 | + return 0; |
| 139 | +} |
| 140 | + |
| 141 | +int max98390_hda_probe(struct device *dev, const char *device_name, |
| 142 | + int id, int irq, struct regmap *regmap, |
| 143 | + enum max98390_hda_bus_type bus_type, int i2c_addr) |
| 144 | +{ |
| 145 | + struct max98390_hda_priv *priv; |
| 146 | + int ret; |
| 147 | + |
| 148 | + priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL); |
| 149 | + if (!priv) |
| 150 | + return -ENOMEM; |
| 151 | + |
| 152 | + priv->dev = dev; |
| 153 | + priv->regmap = regmap; |
| 154 | + priv->bus_type = bus_type; |
| 155 | + priv->irq = irq; |
| 156 | + priv->index = id; |
| 157 | + priv->i2c_addr = i2c_addr; |
| 158 | + dev_set_drvdata(dev, priv); |
| 159 | + |
| 160 | + ret = max98390_hda_init(priv); |
| 161 | + if (ret) |
| 162 | + return ret; |
| 163 | + |
| 164 | + ret = component_add(dev, &max98390_hda_comp_ops); |
| 165 | + if (ret) { |
| 166 | + return ret; |
| 167 | + } |
| 168 | + |
| 169 | + return 0; |
| 170 | +} |
| 171 | +EXPORT_SYMBOL_NS_GPL(max98390_hda_probe, "SND_HDA_SCODEC_MAX98390"); |
| 172 | + |
| 173 | +void max98390_hda_remove(struct device *dev) |
| 174 | +{ |
| 175 | + struct max98390_hda_priv *priv = dev_get_drvdata(dev); |
| 176 | + |
| 177 | + component_del(dev, &max98390_hda_comp_ops); |
| 178 | + |
| 179 | + if (priv && priv->regmap) { |
| 180 | + /* Disable amp on removal */ |
| 181 | + regmap_write(priv->regmap, MAX98390_R203A_AMP_EN, 0x80); |
| 182 | + } |
| 183 | +} |
| 184 | +EXPORT_SYMBOL_NS_GPL(max98390_hda_remove, "SND_HDA_SCODEC_MAX98390"); |
| 185 | + |
| 186 | +static int max98390_hda_runtime_suspend(struct device *dev) |
| 187 | +{ |
| 188 | + struct max98390_hda_priv *priv = dev_get_drvdata(dev); |
| 189 | + |
| 190 | + regmap_write(priv->regmap, MAX98390_R203A_AMP_EN, 0x80); |
| 191 | + regcache_cache_only(priv->regmap, true); |
| 192 | + regcache_mark_dirty(priv->regmap); |
| 193 | + |
| 194 | + return 0; |
| 195 | +} |
| 196 | + |
| 197 | +static int max98390_hda_runtime_resume(struct device *dev) |
| 198 | +{ |
| 199 | + struct max98390_hda_priv *priv = dev_get_drvdata(dev); |
| 200 | + |
| 201 | + regcache_cache_only(priv->regmap, false); |
| 202 | + regcache_sync(priv->regmap); |
| 203 | + |
| 204 | + return 0; |
| 205 | +} |
| 206 | + |
| 207 | +const struct dev_pm_ops max98390_hda_pm_ops = { |
| 208 | + RUNTIME_PM_OPS(max98390_hda_runtime_suspend, max98390_hda_runtime_resume, NULL) |
| 209 | +}; |
| 210 | +EXPORT_SYMBOL_NS_GPL(max98390_hda_pm_ops, "SND_HDA_SCODEC_MAX98390"); |
| 211 | + |
| 212 | +MODULE_DESCRIPTION("HDA MAX98390 side codec library"); |
| 213 | +MODULE_AUTHOR("Kevin Cuperus <cuperus.kevin@hotmail.com>"); |
| 214 | +MODULE_LICENSE("GPL"); |
0 commit comments