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> 199b0a25f0SLars-Peter Clausen #include <linux/slab.h> 209b0a25f0SLars-Peter Clausen 219b0a25f0SLars-Peter Clausen #include <sound/core.h> 229b0a25f0SLars-Peter Clausen #include <sound/soc.h> 239b0a25f0SLars-Peter Clausen #include <sound/tlv.h> 249b0a25f0SLars-Peter Clausen 259b0a25f0SLars-Peter Clausen struct lm4857 { 269b0a25f0SLars-Peter Clausen struct i2c_client *i2c; 279b0a25f0SLars-Peter Clausen uint8_t mode; 289b0a25f0SLars-Peter Clausen }; 299b0a25f0SLars-Peter Clausen 309b0a25f0SLars-Peter Clausen static const uint8_t lm4857_default_regs[] = { 319b0a25f0SLars-Peter Clausen 0x00, 0x00, 0x00, 0x00, 329b0a25f0SLars-Peter Clausen }; 339b0a25f0SLars-Peter Clausen 349b0a25f0SLars-Peter Clausen /* The register offsets in the cache array */ 359b0a25f0SLars-Peter Clausen #define LM4857_MVOL 0 369b0a25f0SLars-Peter Clausen #define LM4857_LVOL 1 379b0a25f0SLars-Peter Clausen #define LM4857_RVOL 2 389b0a25f0SLars-Peter Clausen #define LM4857_CTRL 3 399b0a25f0SLars-Peter Clausen 409b0a25f0SLars-Peter Clausen /* the shifts required to set these bits */ 419b0a25f0SLars-Peter Clausen #define LM4857_3D 5 429b0a25f0SLars-Peter Clausen #define LM4857_WAKEUP 5 439b0a25f0SLars-Peter Clausen #define LM4857_EPGAIN 4 449b0a25f0SLars-Peter Clausen 459b0a25f0SLars-Peter Clausen static int lm4857_write(struct snd_soc_codec *codec, unsigned int reg, 469b0a25f0SLars-Peter Clausen unsigned int value) 479b0a25f0SLars-Peter Clausen { 489b0a25f0SLars-Peter Clausen uint8_t data; 499b0a25f0SLars-Peter Clausen int ret; 509b0a25f0SLars-Peter Clausen 519b0a25f0SLars-Peter Clausen ret = snd_soc_cache_write(codec, reg, value); 529b0a25f0SLars-Peter Clausen if (ret < 0) 539b0a25f0SLars-Peter Clausen return ret; 549b0a25f0SLars-Peter Clausen 559b0a25f0SLars-Peter Clausen data = (reg << 6) | value; 569b0a25f0SLars-Peter Clausen ret = i2c_master_send(codec->control_data, &data, 1); 579b0a25f0SLars-Peter Clausen if (ret != 1) { 589b0a25f0SLars-Peter Clausen dev_err(codec->dev, "Failed to write register: %d\n", ret); 599b0a25f0SLars-Peter Clausen return ret; 609b0a25f0SLars-Peter Clausen } 619b0a25f0SLars-Peter Clausen 629b0a25f0SLars-Peter Clausen return 0; 639b0a25f0SLars-Peter Clausen } 649b0a25f0SLars-Peter Clausen 659b0a25f0SLars-Peter Clausen static unsigned int lm4857_read(struct snd_soc_codec *codec, 669b0a25f0SLars-Peter Clausen unsigned int reg) 679b0a25f0SLars-Peter Clausen { 689b0a25f0SLars-Peter Clausen unsigned int val; 699b0a25f0SLars-Peter Clausen int ret; 709b0a25f0SLars-Peter Clausen 719b0a25f0SLars-Peter Clausen ret = snd_soc_cache_read(codec, reg, &val); 729b0a25f0SLars-Peter Clausen if (ret) 739b0a25f0SLars-Peter Clausen return -1; 749b0a25f0SLars-Peter Clausen 759b0a25f0SLars-Peter Clausen return val; 769b0a25f0SLars-Peter Clausen } 779b0a25f0SLars-Peter Clausen 789b0a25f0SLars-Peter Clausen static int lm4857_get_mode(struct snd_kcontrol *kcontrol, 799b0a25f0SLars-Peter Clausen struct snd_ctl_elem_value *ucontrol) 809b0a25f0SLars-Peter Clausen { 819b0a25f0SLars-Peter Clausen struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); 829b0a25f0SLars-Peter Clausen struct lm4857 *lm4857 = snd_soc_codec_get_drvdata(codec); 839b0a25f0SLars-Peter Clausen 849b0a25f0SLars-Peter Clausen ucontrol->value.integer.value[0] = lm4857->mode; 859b0a25f0SLars-Peter Clausen 869b0a25f0SLars-Peter Clausen return 0; 879b0a25f0SLars-Peter Clausen } 889b0a25f0SLars-Peter Clausen 899b0a25f0SLars-Peter Clausen static int lm4857_set_mode(struct snd_kcontrol *kcontrol, 909b0a25f0SLars-Peter Clausen struct snd_ctl_elem_value *ucontrol) 919b0a25f0SLars-Peter Clausen { 929b0a25f0SLars-Peter Clausen struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol); 939b0a25f0SLars-Peter Clausen struct lm4857 *lm4857 = snd_soc_codec_get_drvdata(codec); 949b0a25f0SLars-Peter Clausen uint8_t value = ucontrol->value.integer.value[0]; 959b0a25f0SLars-Peter Clausen 969b0a25f0SLars-Peter Clausen lm4857->mode = value; 979b0a25f0SLars-Peter Clausen 989b0a25f0SLars-Peter Clausen if (codec->dapm.bias_level == SND_SOC_BIAS_ON) 999b0a25f0SLars-Peter Clausen snd_soc_update_bits(codec, LM4857_CTRL, 0x0F, value + 6); 1009b0a25f0SLars-Peter Clausen 1019b0a25f0SLars-Peter Clausen return 1; 1029b0a25f0SLars-Peter Clausen } 1039b0a25f0SLars-Peter Clausen 1049b0a25f0SLars-Peter Clausen static int lm4857_set_bias_level(struct snd_soc_codec *codec, 1059b0a25f0SLars-Peter Clausen enum snd_soc_bias_level level) 1069b0a25f0SLars-Peter Clausen { 1079b0a25f0SLars-Peter Clausen struct lm4857 *lm4857 = snd_soc_codec_get_drvdata(codec); 1089b0a25f0SLars-Peter Clausen 1099b0a25f0SLars-Peter Clausen switch (level) { 1109b0a25f0SLars-Peter Clausen case SND_SOC_BIAS_ON: 1119b0a25f0SLars-Peter Clausen snd_soc_update_bits(codec, LM4857_CTRL, 0x0F, lm4857->mode + 6); 1129b0a25f0SLars-Peter Clausen break; 1139b0a25f0SLars-Peter Clausen case SND_SOC_BIAS_STANDBY: 1149b0a25f0SLars-Peter Clausen snd_soc_update_bits(codec, LM4857_CTRL, 0x0F, 0); 1159b0a25f0SLars-Peter Clausen break; 1169b0a25f0SLars-Peter Clausen default: 1179b0a25f0SLars-Peter Clausen break; 1189b0a25f0SLars-Peter Clausen } 1199b0a25f0SLars-Peter Clausen 1209b0a25f0SLars-Peter Clausen codec->dapm.bias_level = level; 1219b0a25f0SLars-Peter Clausen 1229b0a25f0SLars-Peter Clausen return 0; 1239b0a25f0SLars-Peter Clausen } 1249b0a25f0SLars-Peter Clausen 1259b0a25f0SLars-Peter Clausen static const char *lm4857_mode[] = { 1269b0a25f0SLars-Peter Clausen "Earpiece", 1279b0a25f0SLars-Peter Clausen "Loudspeaker", 1289b0a25f0SLars-Peter Clausen "Loudspeaker + Headphone", 1299b0a25f0SLars-Peter Clausen "Headphone", 1309b0a25f0SLars-Peter Clausen }; 1319b0a25f0SLars-Peter Clausen 1329b0a25f0SLars-Peter Clausen static const struct soc_enum lm4857_mode_enum = 1339b0a25f0SLars-Peter Clausen SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(lm4857_mode), lm4857_mode); 1349b0a25f0SLars-Peter Clausen 1359b0a25f0SLars-Peter Clausen static const struct snd_soc_dapm_widget lm4857_dapm_widgets[] = { 1369b0a25f0SLars-Peter Clausen SND_SOC_DAPM_INPUT("IN"), 1379b0a25f0SLars-Peter Clausen 1389b0a25f0SLars-Peter Clausen SND_SOC_DAPM_OUTPUT("LS"), 1399b0a25f0SLars-Peter Clausen SND_SOC_DAPM_OUTPUT("HP"), 1409b0a25f0SLars-Peter Clausen SND_SOC_DAPM_OUTPUT("EP"), 1419b0a25f0SLars-Peter Clausen }; 1429b0a25f0SLars-Peter Clausen 1439b0a25f0SLars-Peter Clausen static const DECLARE_TLV_DB_SCALE(stereo_tlv, -4050, 150, 0); 1449b0a25f0SLars-Peter Clausen static const DECLARE_TLV_DB_SCALE(mono_tlv, -3450, 150, 0); 1459b0a25f0SLars-Peter Clausen 1469b0a25f0SLars-Peter Clausen static const struct snd_kcontrol_new lm4857_controls[] = { 1479b0a25f0SLars-Peter Clausen SOC_SINGLE_TLV("Left Playback Volume", LM4857_LVOL, 0, 31, 0, 1489b0a25f0SLars-Peter Clausen stereo_tlv), 1499b0a25f0SLars-Peter Clausen SOC_SINGLE_TLV("Right Playback Volume", LM4857_RVOL, 0, 31, 0, 1509b0a25f0SLars-Peter Clausen stereo_tlv), 1519b0a25f0SLars-Peter Clausen SOC_SINGLE_TLV("Mono Playback Volume", LM4857_MVOL, 0, 31, 0, 1529b0a25f0SLars-Peter Clausen mono_tlv), 1539b0a25f0SLars-Peter Clausen SOC_SINGLE("Spk 3D Playback Switch", LM4857_LVOL, LM4857_3D, 1, 0), 1549b0a25f0SLars-Peter Clausen SOC_SINGLE("HP 3D Playback Switch", LM4857_RVOL, LM4857_3D, 1, 0), 1559b0a25f0SLars-Peter Clausen SOC_SINGLE("Fast Wakeup Playback Switch", LM4857_CTRL, 1569b0a25f0SLars-Peter Clausen LM4857_WAKEUP, 1, 0), 1579b0a25f0SLars-Peter Clausen SOC_SINGLE("Earpiece 6dB Playback Switch", LM4857_CTRL, 1589b0a25f0SLars-Peter Clausen LM4857_EPGAIN, 1, 0), 1599b0a25f0SLars-Peter Clausen 1609b0a25f0SLars-Peter Clausen SOC_ENUM_EXT("Mode", lm4857_mode_enum, 1619b0a25f0SLars-Peter Clausen lm4857_get_mode, lm4857_set_mode), 1629b0a25f0SLars-Peter Clausen }; 1639b0a25f0SLars-Peter Clausen 16425985edcSLucas De Marchi /* There is a demux between the input signal and the output signals. 1659b0a25f0SLars-Peter Clausen * Currently there is no easy way to model it in ASoC and since it does not make 1669b0a25f0SLars-Peter Clausen * much of a difference in practice simply connect the input direclty to the 1679b0a25f0SLars-Peter Clausen * outputs. */ 1689b0a25f0SLars-Peter Clausen static const struct snd_soc_dapm_route lm4857_routes[] = { 1699b0a25f0SLars-Peter Clausen {"LS", NULL, "IN"}, 1709b0a25f0SLars-Peter Clausen {"HP", NULL, "IN"}, 1719b0a25f0SLars-Peter Clausen {"EP", NULL, "IN"}, 1729b0a25f0SLars-Peter Clausen }; 1739b0a25f0SLars-Peter Clausen 1749b0a25f0SLars-Peter Clausen static int lm4857_probe(struct snd_soc_codec *codec) 1759b0a25f0SLars-Peter Clausen { 1769b0a25f0SLars-Peter Clausen struct lm4857 *lm4857 = snd_soc_codec_get_drvdata(codec); 1779b0a25f0SLars-Peter Clausen struct snd_soc_dapm_context *dapm = &codec->dapm; 1789b0a25f0SLars-Peter Clausen int ret; 1799b0a25f0SLars-Peter Clausen 1809b0a25f0SLars-Peter Clausen codec->control_data = lm4857->i2c; 1819b0a25f0SLars-Peter Clausen 182022658beSLiam Girdwood ret = snd_soc_add_codec_controls(codec, lm4857_controls, 1839b0a25f0SLars-Peter Clausen ARRAY_SIZE(lm4857_controls)); 1849b0a25f0SLars-Peter Clausen if (ret) 1859b0a25f0SLars-Peter Clausen return ret; 1869b0a25f0SLars-Peter Clausen 1879b0a25f0SLars-Peter Clausen ret = snd_soc_dapm_new_controls(dapm, lm4857_dapm_widgets, 1889b0a25f0SLars-Peter Clausen ARRAY_SIZE(lm4857_dapm_widgets)); 1899b0a25f0SLars-Peter Clausen if (ret) 1909b0a25f0SLars-Peter Clausen return ret; 1919b0a25f0SLars-Peter Clausen 1929b0a25f0SLars-Peter Clausen ret = snd_soc_dapm_add_routes(dapm, lm4857_routes, 1939b0a25f0SLars-Peter Clausen ARRAY_SIZE(lm4857_routes)); 1949b0a25f0SLars-Peter Clausen if (ret) 1959b0a25f0SLars-Peter Clausen return ret; 1969b0a25f0SLars-Peter Clausen 1979b0a25f0SLars-Peter Clausen snd_soc_dapm_new_widgets(dapm); 1989b0a25f0SLars-Peter Clausen 1999b0a25f0SLars-Peter Clausen return 0; 2009b0a25f0SLars-Peter Clausen } 2019b0a25f0SLars-Peter Clausen 2029b0a25f0SLars-Peter Clausen static struct snd_soc_codec_driver soc_codec_dev_lm4857 = { 2039b0a25f0SLars-Peter Clausen .write = lm4857_write, 2049b0a25f0SLars-Peter Clausen .read = lm4857_read, 2059b0a25f0SLars-Peter Clausen .probe = lm4857_probe, 2069b0a25f0SLars-Peter Clausen .reg_cache_size = ARRAY_SIZE(lm4857_default_regs), 2079b0a25f0SLars-Peter Clausen .reg_word_size = sizeof(uint8_t), 2089b0a25f0SLars-Peter Clausen .reg_cache_default = lm4857_default_regs, 2099b0a25f0SLars-Peter Clausen .set_bias_level = lm4857_set_bias_level, 2109b0a25f0SLars-Peter Clausen }; 2119b0a25f0SLars-Peter Clausen 212*7a79e94eSBill Pemberton static int lm4857_i2c_probe(struct i2c_client *i2c, 2139b0a25f0SLars-Peter Clausen const struct i2c_device_id *id) 2149b0a25f0SLars-Peter Clausen { 2159b0a25f0SLars-Peter Clausen struct lm4857 *lm4857; 2169b0a25f0SLars-Peter Clausen int ret; 2179b0a25f0SLars-Peter Clausen 218eb3bb97cSAxel Lin lm4857 = devm_kzalloc(&i2c->dev, sizeof(*lm4857), GFP_KERNEL); 2199b0a25f0SLars-Peter Clausen if (!lm4857) 2209b0a25f0SLars-Peter Clausen return -ENOMEM; 2219b0a25f0SLars-Peter Clausen 2229b0a25f0SLars-Peter Clausen i2c_set_clientdata(i2c, lm4857); 2239b0a25f0SLars-Peter Clausen 2249b0a25f0SLars-Peter Clausen lm4857->i2c = i2c; 2259b0a25f0SLars-Peter Clausen 2269b0a25f0SLars-Peter Clausen ret = snd_soc_register_codec(&i2c->dev, &soc_codec_dev_lm4857, NULL, 0); 2279b0a25f0SLars-Peter Clausen 2289b0a25f0SLars-Peter Clausen return ret; 2299b0a25f0SLars-Peter Clausen } 2309b0a25f0SLars-Peter Clausen 231*7a79e94eSBill Pemberton static int lm4857_i2c_remove(struct i2c_client *i2c) 2329b0a25f0SLars-Peter Clausen { 2339b0a25f0SLars-Peter Clausen snd_soc_unregister_codec(&i2c->dev); 2349b0a25f0SLars-Peter Clausen return 0; 2359b0a25f0SLars-Peter Clausen } 2369b0a25f0SLars-Peter Clausen 2379b0a25f0SLars-Peter Clausen static const struct i2c_device_id lm4857_i2c_id[] = { 2389b0a25f0SLars-Peter Clausen { "lm4857", 0 }, 2399b0a25f0SLars-Peter Clausen { } 2409b0a25f0SLars-Peter Clausen }; 2419b0a25f0SLars-Peter Clausen MODULE_DEVICE_TABLE(i2c, lm4857_i2c_id); 2429b0a25f0SLars-Peter Clausen 2439b0a25f0SLars-Peter Clausen static struct i2c_driver lm4857_i2c_driver = { 2449b0a25f0SLars-Peter Clausen .driver = { 2459b0a25f0SLars-Peter Clausen .name = "lm4857", 2469b0a25f0SLars-Peter Clausen .owner = THIS_MODULE, 2479b0a25f0SLars-Peter Clausen }, 2489b0a25f0SLars-Peter Clausen .probe = lm4857_i2c_probe, 249*7a79e94eSBill Pemberton .remove = lm4857_i2c_remove, 2509b0a25f0SLars-Peter Clausen .id_table = lm4857_i2c_id, 2519b0a25f0SLars-Peter Clausen }; 2529b0a25f0SLars-Peter Clausen 253f6ec139fSSachin Kamat module_i2c_driver(lm4857_i2c_driver); 2549b0a25f0SLars-Peter Clausen 2559b0a25f0SLars-Peter Clausen MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>"); 2569b0a25f0SLars-Peter Clausen MODULE_DESCRIPTION("LM4857 amplifier driver"); 2579b0a25f0SLars-Peter Clausen MODULE_LICENSE("GPL"); 258