19b0a25f0SLars-Peter Clausen /* 29b0a25f0SLars-Peter Clausen * LM4857 AMP driver 39b0a25f0SLars-Peter Clausen * 49b0a25f0SLars-Peter Clausen * Copyright 2007 Wolfson Microelectronics PLC. 59b0a25f0SLars-Peter Clausen * Author: Graeme Gregory 69a185b9aSMark Brown * graeme.gregory@wolfsonmicro.com 79b0a25f0SLars-Peter Clausen * Copyright 2011 Lars-Peter Clausen <lars@metafoo.de> 89b0a25f0SLars-Peter Clausen * 99b0a25f0SLars-Peter Clausen * This program is free software; you can redistribute it and/or modify it 109b0a25f0SLars-Peter Clausen * under the terms of the GNU General Public License as published by the 119b0a25f0SLars-Peter Clausen * Free Software Foundation; either version 2 of the License, or (at your 129b0a25f0SLars-Peter Clausen * option) any later version. 139b0a25f0SLars-Peter Clausen * 149b0a25f0SLars-Peter Clausen */ 159b0a25f0SLars-Peter Clausen 169b0a25f0SLars-Peter Clausen #include <linux/init.h> 179b0a25f0SLars-Peter Clausen #include <linux/module.h> 189b0a25f0SLars-Peter Clausen #include <linux/i2c.h> 19*9b270968SLars-Peter Clausen #include <linux/regmap.h> 209b0a25f0SLars-Peter Clausen #include <linux/slab.h> 219b0a25f0SLars-Peter Clausen 229b0a25f0SLars-Peter Clausen #include <sound/core.h> 239b0a25f0SLars-Peter Clausen #include <sound/soc.h> 249b0a25f0SLars-Peter Clausen #include <sound/tlv.h> 259b0a25f0SLars-Peter Clausen 269b0a25f0SLars-Peter Clausen struct lm4857 { 27*9b270968SLars-Peter Clausen struct regmap *regmap; 289b0a25f0SLars-Peter Clausen uint8_t mode; 299b0a25f0SLars-Peter Clausen }; 309b0a25f0SLars-Peter Clausen 31*9b270968SLars-Peter Clausen static const struct reg_default lm4857_default_regs[] = { 32*9b270968SLars-Peter Clausen { 0x0, 0x00 }, 33*9b270968SLars-Peter Clausen { 0x1, 0x00 }, 34*9b270968SLars-Peter Clausen { 0x2, 0x00 }, 35*9b270968SLars-Peter Clausen { 0x3, 0x00 }, 369b0a25f0SLars-Peter Clausen }; 379b0a25f0SLars-Peter Clausen 389b0a25f0SLars-Peter Clausen /* The register offsets in the cache array */ 399b0a25f0SLars-Peter Clausen #define LM4857_MVOL 0 409b0a25f0SLars-Peter Clausen #define LM4857_LVOL 1 419b0a25f0SLars-Peter Clausen #define LM4857_RVOL 2 429b0a25f0SLars-Peter Clausen #define LM4857_CTRL 3 439b0a25f0SLars-Peter Clausen 449b0a25f0SLars-Peter Clausen /* the shifts required to set these bits */ 459b0a25f0SLars-Peter Clausen #define LM4857_3D 5 469b0a25f0SLars-Peter Clausen #define LM4857_WAKEUP 5 479b0a25f0SLars-Peter Clausen #define LM4857_EPGAIN 4 489b0a25f0SLars-Peter Clausen 499b0a25f0SLars-Peter Clausen static int lm4857_get_mode(struct snd_kcontrol *kcontrol, 509b0a25f0SLars-Peter Clausen struct snd_ctl_elem_value *ucontrol) 519b0a25f0SLars-Peter Clausen { 529b0a25f0SLars-Peter Clausen struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); 539b0a25f0SLars-Peter Clausen struct lm4857 *lm4857 = snd_soc_codec_get_drvdata(codec); 549b0a25f0SLars-Peter Clausen 559b0a25f0SLars-Peter Clausen ucontrol->value.integer.value[0] = lm4857->mode; 569b0a25f0SLars-Peter Clausen 579b0a25f0SLars-Peter Clausen return 0; 589b0a25f0SLars-Peter Clausen } 599b0a25f0SLars-Peter Clausen 609b0a25f0SLars-Peter Clausen static int lm4857_set_mode(struct snd_kcontrol *kcontrol, 619b0a25f0SLars-Peter Clausen struct snd_ctl_elem_value *ucontrol) 629b0a25f0SLars-Peter Clausen { 639b0a25f0SLars-Peter Clausen struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); 649b0a25f0SLars-Peter Clausen struct lm4857 *lm4857 = snd_soc_codec_get_drvdata(codec); 659b0a25f0SLars-Peter Clausen uint8_t value = ucontrol->value.integer.value[0]; 669b0a25f0SLars-Peter Clausen 679b0a25f0SLars-Peter Clausen lm4857->mode = value; 689b0a25f0SLars-Peter Clausen 699b0a25f0SLars-Peter Clausen if (codec->dapm.bias_level == SND_SOC_BIAS_ON) 70*9b270968SLars-Peter Clausen regmap_update_bits(lm4857->regmap, LM4857_CTRL, 0x0F, value + 6); 719b0a25f0SLars-Peter Clausen 729b0a25f0SLars-Peter Clausen return 1; 739b0a25f0SLars-Peter Clausen } 749b0a25f0SLars-Peter Clausen 759b0a25f0SLars-Peter Clausen static int lm4857_set_bias_level(struct snd_soc_codec *codec, 769b0a25f0SLars-Peter Clausen enum snd_soc_bias_level level) 779b0a25f0SLars-Peter Clausen { 789b0a25f0SLars-Peter Clausen struct lm4857 *lm4857 = snd_soc_codec_get_drvdata(codec); 799b0a25f0SLars-Peter Clausen 809b0a25f0SLars-Peter Clausen switch (level) { 819b0a25f0SLars-Peter Clausen case SND_SOC_BIAS_ON: 82*9b270968SLars-Peter Clausen regmap_update_bits(lm4857->regmap, LM4857_CTRL, 0x0F, 83*9b270968SLars-Peter Clausen lm4857->mode + 6); 849b0a25f0SLars-Peter Clausen break; 859b0a25f0SLars-Peter Clausen case SND_SOC_BIAS_STANDBY: 86*9b270968SLars-Peter Clausen regmap_update_bits(lm4857->regmap, LM4857_CTRL, 0x0F, 0); 879b0a25f0SLars-Peter Clausen break; 889b0a25f0SLars-Peter Clausen default: 899b0a25f0SLars-Peter Clausen break; 909b0a25f0SLars-Peter Clausen } 919b0a25f0SLars-Peter Clausen 929b0a25f0SLars-Peter Clausen codec->dapm.bias_level = level; 939b0a25f0SLars-Peter Clausen 949b0a25f0SLars-Peter Clausen return 0; 959b0a25f0SLars-Peter Clausen } 969b0a25f0SLars-Peter Clausen 979b0a25f0SLars-Peter Clausen static const char *lm4857_mode[] = { 989b0a25f0SLars-Peter Clausen "Earpiece", 999b0a25f0SLars-Peter Clausen "Loudspeaker", 1009b0a25f0SLars-Peter Clausen "Loudspeaker + Headphone", 1019b0a25f0SLars-Peter Clausen "Headphone", 1029b0a25f0SLars-Peter Clausen }; 1039b0a25f0SLars-Peter Clausen 1049b0a25f0SLars-Peter Clausen static const struct soc_enum lm4857_mode_enum = 1059b0a25f0SLars-Peter Clausen SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(lm4857_mode), lm4857_mode); 1069b0a25f0SLars-Peter Clausen 1079b0a25f0SLars-Peter Clausen static const struct snd_soc_dapm_widget lm4857_dapm_widgets[] = { 1089b0a25f0SLars-Peter Clausen SND_SOC_DAPM_INPUT("IN"), 1099b0a25f0SLars-Peter Clausen 1109b0a25f0SLars-Peter Clausen SND_SOC_DAPM_OUTPUT("LS"), 1119b0a25f0SLars-Peter Clausen SND_SOC_DAPM_OUTPUT("HP"), 1129b0a25f0SLars-Peter Clausen SND_SOC_DAPM_OUTPUT("EP"), 1139b0a25f0SLars-Peter Clausen }; 1149b0a25f0SLars-Peter Clausen 1159b0a25f0SLars-Peter Clausen static const DECLARE_TLV_DB_SCALE(stereo_tlv, -4050, 150, 0); 1169b0a25f0SLars-Peter Clausen static const DECLARE_TLV_DB_SCALE(mono_tlv, -3450, 150, 0); 1179b0a25f0SLars-Peter Clausen 1189b0a25f0SLars-Peter Clausen static const struct snd_kcontrol_new lm4857_controls[] = { 1199b0a25f0SLars-Peter Clausen SOC_SINGLE_TLV("Left Playback Volume", LM4857_LVOL, 0, 31, 0, 1209b0a25f0SLars-Peter Clausen stereo_tlv), 1219b0a25f0SLars-Peter Clausen SOC_SINGLE_TLV("Right Playback Volume", LM4857_RVOL, 0, 31, 0, 1229b0a25f0SLars-Peter Clausen stereo_tlv), 1239b0a25f0SLars-Peter Clausen SOC_SINGLE_TLV("Mono Playback Volume", LM4857_MVOL, 0, 31, 0, 1249b0a25f0SLars-Peter Clausen mono_tlv), 1259b0a25f0SLars-Peter Clausen SOC_SINGLE("Spk 3D Playback Switch", LM4857_LVOL, LM4857_3D, 1, 0), 1269b0a25f0SLars-Peter Clausen SOC_SINGLE("HP 3D Playback Switch", LM4857_RVOL, LM4857_3D, 1, 0), 1279b0a25f0SLars-Peter Clausen SOC_SINGLE("Fast Wakeup Playback Switch", LM4857_CTRL, 1289b0a25f0SLars-Peter Clausen LM4857_WAKEUP, 1, 0), 1299b0a25f0SLars-Peter Clausen SOC_SINGLE("Earpiece 6dB Playback Switch", LM4857_CTRL, 1309b0a25f0SLars-Peter Clausen LM4857_EPGAIN, 1, 0), 1319b0a25f0SLars-Peter Clausen 1329b0a25f0SLars-Peter Clausen SOC_ENUM_EXT("Mode", lm4857_mode_enum, 1339b0a25f0SLars-Peter Clausen lm4857_get_mode, lm4857_set_mode), 1349b0a25f0SLars-Peter Clausen }; 1359b0a25f0SLars-Peter Clausen 13625985edcSLucas De Marchi /* There is a demux between the input signal and the output signals. 1379b0a25f0SLars-Peter Clausen * Currently there is no easy way to model it in ASoC and since it does not make 1389b0a25f0SLars-Peter Clausen * much of a difference in practice simply connect the input direclty to the 1399b0a25f0SLars-Peter Clausen * outputs. */ 1409b0a25f0SLars-Peter Clausen static const struct snd_soc_dapm_route lm4857_routes[] = { 1419b0a25f0SLars-Peter Clausen {"LS", NULL, "IN"}, 1429b0a25f0SLars-Peter Clausen {"HP", NULL, "IN"}, 1439b0a25f0SLars-Peter Clausen {"EP", NULL, "IN"}, 1449b0a25f0SLars-Peter Clausen }; 1459b0a25f0SLars-Peter Clausen 1469b0a25f0SLars-Peter Clausen static struct snd_soc_codec_driver soc_codec_dev_lm4857 = { 1479b0a25f0SLars-Peter Clausen .set_bias_level = lm4857_set_bias_level, 14807ccc0f4SLars-Peter Clausen 14907ccc0f4SLars-Peter Clausen .controls = lm4857_controls, 15007ccc0f4SLars-Peter Clausen .num_controls = ARRAY_SIZE(lm4857_controls), 15107ccc0f4SLars-Peter Clausen .dapm_widgets = lm4857_dapm_widgets, 15207ccc0f4SLars-Peter Clausen .num_dapm_widgets = ARRAY_SIZE(lm4857_dapm_widgets), 15307ccc0f4SLars-Peter Clausen .dapm_routes = lm4857_routes, 15407ccc0f4SLars-Peter Clausen .num_dapm_routes = ARRAY_SIZE(lm4857_routes), 1559b0a25f0SLars-Peter Clausen }; 1569b0a25f0SLars-Peter Clausen 157*9b270968SLars-Peter Clausen static const struct regmap_config lm4857_regmap_config = { 158*9b270968SLars-Peter Clausen .val_bits = 6, 159*9b270968SLars-Peter Clausen .reg_bits = 2, 160*9b270968SLars-Peter Clausen 161*9b270968SLars-Peter Clausen .max_register = LM4857_CTRL, 162*9b270968SLars-Peter Clausen 163*9b270968SLars-Peter Clausen .cache_type = REGCACHE_FLAT, 164*9b270968SLars-Peter Clausen .reg_defaults = lm4857_default_regs, 165*9b270968SLars-Peter Clausen .num_reg_defaults = ARRAY_SIZE(lm4857_default_regs), 166*9b270968SLars-Peter Clausen }; 167*9b270968SLars-Peter Clausen 1687a79e94eSBill Pemberton static int lm4857_i2c_probe(struct i2c_client *i2c, 1699b0a25f0SLars-Peter Clausen const struct i2c_device_id *id) 1709b0a25f0SLars-Peter Clausen { 1719b0a25f0SLars-Peter Clausen struct lm4857 *lm4857; 1729b0a25f0SLars-Peter Clausen 173eb3bb97cSAxel Lin lm4857 = devm_kzalloc(&i2c->dev, sizeof(*lm4857), GFP_KERNEL); 1749b0a25f0SLars-Peter Clausen if (!lm4857) 1759b0a25f0SLars-Peter Clausen return -ENOMEM; 1769b0a25f0SLars-Peter Clausen 1779b0a25f0SLars-Peter Clausen i2c_set_clientdata(i2c, lm4857); 1789b0a25f0SLars-Peter Clausen 179*9b270968SLars-Peter Clausen lm4857->regmap = devm_regmap_init_i2c(i2c, &lm4857_regmap_config); 180*9b270968SLars-Peter Clausen if (IS_ERR(lm4857->regmap)) 181*9b270968SLars-Peter Clausen return PTR_ERR(lm4857->regmap); 1829b0a25f0SLars-Peter Clausen 183*9b270968SLars-Peter Clausen return snd_soc_register_codec(&i2c->dev, &soc_codec_dev_lm4857, NULL, 0); 1849b0a25f0SLars-Peter Clausen } 1859b0a25f0SLars-Peter Clausen 1867a79e94eSBill Pemberton static int lm4857_i2c_remove(struct i2c_client *i2c) 1879b0a25f0SLars-Peter Clausen { 1889b0a25f0SLars-Peter Clausen snd_soc_unregister_codec(&i2c->dev); 1899b0a25f0SLars-Peter Clausen return 0; 1909b0a25f0SLars-Peter Clausen } 1919b0a25f0SLars-Peter Clausen 1929b0a25f0SLars-Peter Clausen static const struct i2c_device_id lm4857_i2c_id[] = { 1939b0a25f0SLars-Peter Clausen { "lm4857", 0 }, 1949b0a25f0SLars-Peter Clausen { } 1959b0a25f0SLars-Peter Clausen }; 1969b0a25f0SLars-Peter Clausen MODULE_DEVICE_TABLE(i2c, lm4857_i2c_id); 1979b0a25f0SLars-Peter Clausen 1989b0a25f0SLars-Peter Clausen static struct i2c_driver lm4857_i2c_driver = { 1999b0a25f0SLars-Peter Clausen .driver = { 2009b0a25f0SLars-Peter Clausen .name = "lm4857", 2019b0a25f0SLars-Peter Clausen .owner = THIS_MODULE, 2029b0a25f0SLars-Peter Clausen }, 2039b0a25f0SLars-Peter Clausen .probe = lm4857_i2c_probe, 2047a79e94eSBill Pemberton .remove = lm4857_i2c_remove, 2059b0a25f0SLars-Peter Clausen .id_table = lm4857_i2c_id, 2069b0a25f0SLars-Peter Clausen }; 2079b0a25f0SLars-Peter Clausen 208f6ec139fSSachin Kamat module_i2c_driver(lm4857_i2c_driver); 2099b0a25f0SLars-Peter Clausen 2109b0a25f0SLars-Peter Clausen MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>"); 2119b0a25f0SLars-Peter Clausen MODULE_DESCRIPTION("LM4857 amplifier driver"); 2129b0a25f0SLars-Peter Clausen MODULE_LICENSE("GPL"); 213