| 1 |
// SPDX-License-Identifier: GPL-2.0-only |
| 2 |
/* |
| 3 |
* Driver for the NTP8918 Audio Amplifier |
| 4 |
* |
| 5 |
* Copyright (c) 2024, SaluteDevices. All Rights Reserved. |
| 6 |
* |
| 7 |
* Author: Igor Prusov <[email protected]> |
| 8 |
*/ |
| 9 |
|
| 10 |
#include <linux/kernel.h> |
| 11 |
#include <linux/reset.h> |
| 12 |
#include <linux/i2c.h> |
| 13 |
#include <linux/regmap.h> |
| 14 |
#include <linux/clk.h> |
| 15 |
#include <linux/clk-provider.h> |
| 16 |
|
| 17 |
#include <sound/initval.h> |
| 18 |
#include <sound/core.h> |
| 19 |
#include <sound/pcm.h> |
| 20 |
#include <sound/pcm_params.h> |
| 21 |
#include <sound/soc.h> |
| 22 |
#include <sound/soc-component.h> |
| 23 |
#include <sound/tlv.h> |
| 24 |
|
| 25 |
#include "ntpfw.h" |
| 26 |
|
| 27 |
#define NTP8918_RATES (SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | \ |
| 28 |
SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_96000) |
| 29 |
|
| 30 |
#define NTP8918_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | \ |
| 31 |
SNDRV_PCM_FMTBIT_S20_3LE | \ |
| 32 |
SNDRV_PCM_FMTBIT_S24_LE | \ |
| 33 |
SNDRV_PCM_FMTBIT_S32_LE) |
| 34 |
|
| 35 |
#define NTP8918_INPUT_FMT 0x0 |
| 36 |
#define NTP8918_INPUT_FMT_MASTER_MODE BIT(0) |
| 37 |
#define NTP8918_INPUT_FMT_GSA_MODE BIT(1) |
| 38 |
#define NTP8918_GSA_FMT 0x1 |
| 39 |
#define NTP8918_GSA_BS_MASK GENMASK(3, 2) |
| 40 |
#define NTP8918_GSA_BS(x) ((x) << 2) |
| 41 |
#define NTP8918_GSA_RIGHT_J BIT(0) |
| 42 |
#define NTP8918_GSA_LSB BIT(1) |
| 43 |
#define NTP8918_MCLK_FREQ_CTRL 0x2 |
| 44 |
#define NTP8918_MCLK_FREQ_MCF GENMASK(1, 0) |
| 45 |
#define NTP8918_MASTER_VOL 0x0C |
| 46 |
#define NTP8918_CHNL_A_VOL 0x17 |
| 47 |
#define NTP8918_CHNL_B_VOL 0x18 |
| 48 |
#define NTP8918_SOFT_MUTE 0x33 |
| 49 |
#define NTP8918_SOFT_MUTE_SM1 BIT(0) |
| 50 |
#define NTP8918_SOFT_MUTE_SM2 BIT(1) |
| 51 |
#define NTP8918_PWM_SWITCH 0x34 |
| 52 |
#define NTP8918_PWM_MASK_CTRL0 0x35 |
| 53 |
#define REG_MAX NTP8918_PWM_MASK_CTRL0 |
| 54 |
|
| 55 |
#define NTP8918_FW_NAME "eq_8918.bin" |
| 56 |
#define NTP8918_FW_MAGIC 0x38393138 /* "8918" */ |
| 57 |
|
| 58 |
struct ntp8918_priv { |
| 59 |
struct i2c_client *i2c; |
| 60 |
struct clk *bck; |
| 61 |
struct reset_control *reset; |
| 62 |
unsigned int format; |
| 63 |
}; |
| 64 |
|
| 65 |
static const DECLARE_TLV_DB_SCALE(ntp8918_master_vol_scale, -12550, 50, 0); |
| 66 |
|
| 67 |
static const struct snd_kcontrol_new ntp8918_vol_control[] = { |
| 68 |
SOC_SINGLE_RANGE_TLV("Playback Volume", NTP8918_MASTER_VOL, 0, |
| 69 |
0x04, 0xff, 0, ntp8918_master_vol_scale), |
| 70 |
SOC_SINGLE("Playback Switch", NTP8918_PWM_MASK_CTRL0, 1, 1, 1), |
| 71 |
}; |
| 72 |
|
| 73 |
static void ntp8918_reset_gpio(struct ntp8918_priv *ntp8918) |
| 74 |
{ |
| 75 |
/* |
| 76 |
* Proper initialization sequence for NTP8918 amplifier requires driving |
| 77 |
* /RESET signal low during power up for at least 0.1us. The sequence is, |
| 78 |
* according to NTP8918 datasheet, 6.2 Timing Sequence 1: |
| 79 |
* Deassert for T2 >= 1ms... |
| 80 |
*/ |
| 81 |
reset_control_deassert(ntp8918->reset); |
| 82 |
fsleep(1000); |
| 83 |
|
| 84 |
/* ...Assert for T3 >= 0.1us... */ |
| 85 |
reset_control_assert(ntp8918->reset); |
| 86 |
fsleep(1); |
| 87 |
|
| 88 |
/* ...Deassert, and wait for T4 >= 0.5ms before sound on sequence. */ |
| 89 |
reset_control_deassert(ntp8918->reset); |
| 90 |
fsleep(500); |
| 91 |
} |
| 92 |
|
| 93 |
static const struct reg_sequence ntp8918_sound_off[] = { |
| 94 |
{ NTP8918_MASTER_VOL, 0 }, |
| 95 |
}; |
| 96 |
|
| 97 |
static const struct reg_sequence ntp8918_sound_on[] = { |
| 98 |
{ NTP8918_MASTER_VOL, 0b11 }, |
| 99 |
}; |
| 100 |
|
| 101 |
static int ntp8918_load_firmware(struct ntp8918_priv *ntp8918) |
| 102 |
{ |
| 103 |
int ret; |
| 104 |
|
| 105 |
ret = ntpfw_load(ntp8918->i2c, NTP8918_FW_NAME, NTP8918_FW_MAGIC); |
| 106 |
if (ret == -ENOENT) { |
| 107 |
dev_warn_once(&ntp8918->i2c->dev, "Could not find firmware %s\n", |
| 108 |
NTP8918_FW_NAME); |
| 109 |
return 0; |
| 110 |
} |
| 111 |
|
| 112 |
return ret; |
| 113 |
} |
| 114 |
|
| 115 |
static int ntp8918_snd_suspend(struct snd_soc_component *component) |
| 116 |
{ |
| 117 |
struct ntp8918_priv *ntp8918 = snd_soc_component_get_drvdata(component); |
| 118 |
|
| 119 |
regcache_cache_only(component->regmap, true); |
| 120 |
|
| 121 |
regmap_multi_reg_write_bypassed(component->regmap, |
| 122 |
ntp8918_sound_off, |
| 123 |
ARRAY_SIZE(ntp8918_sound_off)); |
| 124 |
|
| 125 |
/* |
| 126 |
* According to NTP8918 datasheet, 6.2 Timing Sequence 1: |
| 127 |
* wait after sound off for T6 >= 0.5ms |
| 128 |
*/ |
| 129 |
fsleep(500); |
| 130 |
reset_control_assert(ntp8918->reset); |
| 131 |
|
| 132 |
regcache_mark_dirty(component->regmap); |
| 133 |
clk_disable_unprepare(ntp8918->bck); |
| 134 |
|
| 135 |
return 0; |
| 136 |
} |
| 137 |
|
| 138 |
static int ntp8918_snd_resume(struct snd_soc_component *component) |
| 139 |
{ |
| 140 |
struct ntp8918_priv *ntp8918 = snd_soc_component_get_drvdata(component); |
| 141 |
int ret; |
| 142 |
|
| 143 |
ret = clk_prepare_enable(ntp8918->bck); |
| 144 |
if (ret) |
| 145 |
return ret; |
| 146 |
|
| 147 |
ntp8918_reset_gpio(ntp8918); |
| 148 |
|
| 149 |
regmap_multi_reg_write_bypassed(component->regmap, |
| 150 |
ntp8918_sound_on, |
| 151 |
ARRAY_SIZE(ntp8918_sound_on)); |
| 152 |
|
| 153 |
ret = ntp8918_load_firmware(ntp8918); |
| 154 |
if (ret) { |
| 155 |
dev_err(&ntp8918->i2c->dev, "Failed to load firmware\n"); |
| 156 |
return ret; |
| 157 |
} |
| 158 |
|
| 159 |
regcache_cache_only(component->regmap, false); |
| 160 |
snd_soc_component_cache_sync(component); |
| 161 |
|
| 162 |
return 0; |
| 163 |
} |
| 164 |
|
| 165 |
static int ntp8918_probe(struct snd_soc_component *component) |
| 166 |
{ |
| 167 |
int ret; |
| 168 |
struct ntp8918_priv *ntp8918 = snd_soc_component_get_drvdata(component); |
| 169 |
struct device *dev = component->dev; |
| 170 |
|
| 171 |
ret = snd_soc_add_component_controls(component, ntp8918_vol_control, |
| 172 |
ARRAY_SIZE(ntp8918_vol_control)); |
| 173 |
if (ret) |
| 174 |
return dev_err_probe(dev, ret, "Failed to add controls\n"); |
| 175 |
|
| 176 |
ret = ntp8918_load_firmware(ntp8918); |
| 177 |
if (ret) |
| 178 |
return dev_err_probe(dev, ret, "Failed to load firmware\n"); |
| 179 |
|
| 180 |
return 0; |
| 181 |
} |
| 182 |
|
| 183 |
static const struct snd_soc_dapm_widget ntp8918_dapm_widgets[] = { |
| 184 |
SND_SOC_DAPM_DAC("AIFIN", "Playback", SND_SOC_NOPM, 0, 0), |
| 185 |
|
| 186 |
SND_SOC_DAPM_OUTPUT("OUT1"), |
| 187 |
SND_SOC_DAPM_OUTPUT("OUT2"), |
| 188 |
}; |
| 189 |
|
| 190 |
static const struct snd_soc_dapm_route ntp8918_dapm_routes[] = { |
| 191 |
{ "OUT1", NULL, "AIFIN" }, |
| 192 |
{ "OUT2", NULL, "AIFIN" }, |
| 193 |
}; |
| 194 |
|
| 195 |
static const struct snd_soc_component_driver soc_component_ntp8918 = { |
| 196 |
.probe = ntp8918_probe, |
| 197 |
.suspend = ntp8918_snd_suspend, |
| 198 |
.resume = ntp8918_snd_resume, |
| 199 |
.dapm_widgets = ntp8918_dapm_widgets, |
| 200 |
.num_dapm_widgets = ARRAY_SIZE(ntp8918_dapm_widgets), |
| 201 |
.dapm_routes = ntp8918_dapm_routes, |
| 202 |
.num_dapm_routes = ARRAY_SIZE(ntp8918_dapm_routes), |
| 203 |
}; |
| 204 |
|
| 205 |
static int ntp8918_hw_params(struct snd_pcm_substream *substream, |
| 206 |
struct snd_pcm_hw_params *params, |
| 207 |
struct snd_soc_dai *dai) |
| 208 |
{ |
| 209 |
struct snd_soc_component *component = dai->component; |
| 210 |
struct ntp8918_priv *ntp8918 = snd_soc_component_get_drvdata(component); |
| 211 |
unsigned int input_fmt = 0; |
| 212 |
unsigned int gsa_fmt = 0; |
| 213 |
unsigned int gsa_fmt_mask; |
| 214 |
unsigned int mcf; |
| 215 |
int bclk; |
| 216 |
int ret; |
| 217 |
|
| 218 |
bclk = snd_soc_params_to_bclk(params); |
| 219 |
switch (bclk) { |
| 220 |
case 3072000: |
| 221 |
case 2822400: |
| 222 |
mcf = 0; |
| 223 |
break; |
| 224 |
case 6144000: |
| 225 |
mcf = 1; |
| 226 |
break; |
| 227 |
case 2048000: |
| 228 |
mcf = 2; |
| 229 |
break; |
| 230 |
default: |
| 231 |
return -EINVAL; |
| 232 |
} |
| 233 |
|
| 234 |
ret = snd_soc_component_update_bits(component, NTP8918_MCLK_FREQ_CTRL, |
| 235 |
NTP8918_MCLK_FREQ_MCF, mcf); |
| 236 |
if (ret) |
| 237 |
return ret; |
| 238 |
|
| 239 |
switch (ntp8918->format) { |
| 240 |
case SND_SOC_DAIFMT_I2S: |
| 241 |
break; |
| 242 |
case SND_SOC_DAIFMT_RIGHT_J: |
| 243 |
input_fmt |= NTP8918_INPUT_FMT_GSA_MODE; |
| 244 |
gsa_fmt |= NTP8918_GSA_RIGHT_J; |
| 245 |
break; |
| 246 |
case SND_SOC_DAIFMT_LEFT_J: |
| 247 |
input_fmt |= NTP8918_INPUT_FMT_GSA_MODE; |
| 248 |
break; |
| 249 |
} |
| 250 |
|
| 251 |
ret = snd_soc_component_update_bits(component, NTP8918_INPUT_FMT, |
| 252 |
NTP8918_INPUT_FMT_MASTER_MODE | |
| 253 |
NTP8918_INPUT_FMT_GSA_MODE, |
| 254 |
input_fmt); |
| 255 |
|
| 256 |
if (!(input_fmt & NTP8918_INPUT_FMT_GSA_MODE) || ret < 0) |
| 257 |
return ret; |
| 258 |
|
| 259 |
switch (params_width(params)) { |
| 260 |
case 24: |
| 261 |
gsa_fmt |= NTP8918_GSA_BS(0); |
| 262 |
break; |
| 263 |
case 20: |
| 264 |
gsa_fmt |= NTP8918_GSA_BS(1); |
| 265 |
break; |
| 266 |
case 18: |
| 267 |
gsa_fmt |= NTP8918_GSA_BS(2); |
| 268 |
break; |
| 269 |
case 16: |
| 270 |
gsa_fmt |= NTP8918_GSA_BS(3); |
| 271 |
break; |
| 272 |
default: |
| 273 |
return -EINVAL; |
| 274 |
} |
| 275 |
|
| 276 |
gsa_fmt_mask = NTP8918_GSA_BS_MASK | |
| 277 |
NTP8918_GSA_RIGHT_J | |
| 278 |
NTP8918_GSA_LSB; |
| 279 |
return snd_soc_component_update_bits(component, NTP8918_GSA_FMT, |
| 280 |
gsa_fmt_mask, gsa_fmt); |
| 281 |
} |
| 282 |
|
| 283 |
static int ntp8918_set_fmt(struct snd_soc_dai *dai, unsigned int fmt) |
| 284 |
{ |
| 285 |
struct snd_soc_component *component = dai->component; |
| 286 |
struct ntp8918_priv *ntp8918 = snd_soc_component_get_drvdata(component); |
| 287 |
|
| 288 |
switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) { |
| 289 |
case SND_SOC_DAIFMT_I2S: |
| 290 |
case SND_SOC_DAIFMT_RIGHT_J: |
| 291 |
case SND_SOC_DAIFMT_LEFT_J: |
| 292 |
ntp8918->format = fmt & SND_SOC_DAIFMT_FORMAT_MASK; |
| 293 |
break; |
| 294 |
default: |
| 295 |
return -EINVAL; |
| 296 |
} |
| 297 |
return 0; |
| 298 |
} |
| 299 |
|
| 300 |
static int ntp8918_digital_mute(struct snd_soc_dai *dai, int mute, int stream) |
| 301 |
{ |
| 302 |
unsigned int mute_mask = NTP8918_SOFT_MUTE_SM1 | |
| 303 |
NTP8918_SOFT_MUTE_SM2; |
| 304 |
|
| 305 |
return snd_soc_component_update_bits(dai->component, NTP8918_SOFT_MUTE, |
| 306 |
mute_mask, mute ? mute_mask : 0); |
| 307 |
} |
| 308 |
|
| 309 |
static const struct snd_soc_dai_ops ntp8918_dai_ops = { |
| 310 |
.hw_params = ntp8918_hw_params, |
| 311 |
.set_fmt = ntp8918_set_fmt, |
| 312 |
.mute_stream = ntp8918_digital_mute, |
| 313 |
}; |
| 314 |
|
| 315 |
static struct snd_soc_dai_driver ntp8918_dai = { |
| 316 |
.name = "ntp8918-amplifier", |
| 317 |
.playback = { |
| 318 |
.stream_name = "Playback", |
| 319 |
.channels_min = 1, |
| 320 |
.channels_max = 2, |
| 321 |
.rates = NTP8918_RATES, |
| 322 |
.formats = NTP8918_FORMATS, |
| 323 |
}, |
| 324 |
.ops = &ntp8918_dai_ops, |
| 325 |
}; |
| 326 |
|
| 327 |
static const struct regmap_config ntp8918_regmap = { |
| 328 |
.reg_bits = 8, |
| 329 |
.val_bits = 8, |
| 330 |
.max_register = REG_MAX, |
| 331 |
.cache_type = REGCACHE_MAPLE, |
| 332 |
}; |
| 333 |
|
| 334 |
static int ntp8918_i2c_probe(struct i2c_client *i2c) |
| 335 |
{ |
| 336 |
struct ntp8918_priv *ntp8918; |
| 337 |
int ret; |
| 338 |
struct regmap *regmap; |
| 339 |
|
| 340 |
ntp8918 = devm_kzalloc(&i2c->dev, sizeof(*ntp8918), GFP_KERNEL); |
| 341 |
if (!ntp8918) |
| 342 |
return -ENOMEM; |
| 343 |
|
| 344 |
ntp8918->i2c = i2c; |
| 345 |
|
| 346 |
ntp8918->reset = devm_reset_control_get_shared(&i2c->dev, NULL); |
| 347 |
if (IS_ERR(ntp8918->reset)) |
| 348 |
return dev_err_probe(&i2c->dev, PTR_ERR(ntp8918->reset), "Failed to get reset\n"); |
| 349 |
|
| 350 |
dev_set_drvdata(&i2c->dev, ntp8918); |
| 351 |
|
| 352 |
ntp8918_reset_gpio(ntp8918); |
| 353 |
|
| 354 |
regmap = devm_regmap_init_i2c(i2c, &ntp8918_regmap); |
| 355 |
if (IS_ERR(regmap)) |
| 356 |
return dev_err_probe(&i2c->dev, PTR_ERR(regmap), |
| 357 |
"Failed to allocate regmap\n"); |
| 358 |
|
| 359 |
ret = devm_snd_soc_register_component(&i2c->dev, &soc_component_ntp8918, |
| 360 |
&ntp8918_dai, 1); |
| 361 |
if (ret) |
| 362 |
return dev_err_probe(&i2c->dev, ret, |
| 363 |
"Failed to register component\n"); |
| 364 |
|
| 365 |
ntp8918->bck = devm_clk_get_enabled(&i2c->dev, "bck"); |
| 366 |
if (IS_ERR(ntp8918->bck)) |
| 367 |
return dev_err_probe(&i2c->dev, PTR_ERR(ntp8918->bck), "failed to get bck clock\n"); |
| 368 |
|
| 369 |
return 0; |
| 370 |
} |
| 371 |
|
| 372 |
static const struct i2c_device_id ntp8918_i2c_id[] = { |
| 373 |
{ "ntp8918" }, |
| 374 |
{} |
| 375 |
}; |
| 376 |
MODULE_DEVICE_TABLE(i2c, ntp8918_i2c_id); |
| 377 |
|
| 378 |
static const struct of_device_id ntp8918_of_match[] = { |
| 379 |
{.compatible = "neofidelity,ntp8918"}, |
| 380 |
{} |
| 381 |
}; |
| 382 |
MODULE_DEVICE_TABLE(of, ntp8918_of_match); |
| 383 |
|
| 384 |
static struct i2c_driver ntp8918_i2c_driver = { |
| 385 |
.probe = ntp8918_i2c_probe, |
| 386 |
.id_table = ntp8918_i2c_id, |
| 387 |
.driver = { |
| 388 |
.name = "ntp8918", |
| 389 |
.of_match_table = ntp8918_of_match, |
| 390 |
}, |
| 391 |
}; |
| 392 |
module_i2c_driver(ntp8918_i2c_driver); |
| 393 |
|
| 394 |
MODULE_AUTHOR("Igor Prusov <[email protected]>"); |
| 395 |
MODULE_DESCRIPTION("NTP8918 Audio Amplifier Driver"); |
| 396 |
MODULE_LICENSE("GPL"); |