Lars-Peter Clausen | 9b0a25f | 2011-03-07 08:04:55 +0100 | [diff] [blame] | 1 | /* |
| 2 | * LM4857 AMP driver |
| 3 | * |
| 4 | * Copyright 2007 Wolfson Microelectronics PLC. |
| 5 | * Author: Graeme Gregory |
Mark Brown | 9a185b9 | 2011-10-06 11:10:01 +0100 | [diff] [blame] | 6 | * graeme.gregory@wolfsonmicro.com |
Lars-Peter Clausen | 9b0a25f | 2011-03-07 08:04:55 +0100 | [diff] [blame] | 7 | * Copyright 2011 Lars-Peter Clausen <lars@metafoo.de> |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify it |
| 10 | * under the terms of the GNU General Public License as published by the |
| 11 | * Free Software Foundation; either version 2 of the License, or (at your |
| 12 | * option) any later version. |
| 13 | * |
| 14 | */ |
| 15 | |
| 16 | #include <linux/init.h> |
| 17 | #include <linux/module.h> |
| 18 | #include <linux/i2c.h> |
Lars-Peter Clausen | 9b27096 | 2013-07-28 18:45:29 +0200 | [diff] [blame] | 19 | #include <linux/regmap.h> |
Lars-Peter Clausen | 9b0a25f | 2011-03-07 08:04:55 +0100 | [diff] [blame] | 20 | #include <linux/slab.h> |
| 21 | |
| 22 | #include <sound/core.h> |
| 23 | #include <sound/soc.h> |
| 24 | #include <sound/tlv.h> |
| 25 | |
| 26 | struct lm4857 { |
Lars-Peter Clausen | 9b27096 | 2013-07-28 18:45:29 +0200 | [diff] [blame] | 27 | struct regmap *regmap; |
Lars-Peter Clausen | 9b0a25f | 2011-03-07 08:04:55 +0100 | [diff] [blame] | 28 | uint8_t mode; |
| 29 | }; |
| 30 | |
Lars-Peter Clausen | 9b27096 | 2013-07-28 18:45:29 +0200 | [diff] [blame] | 31 | static const struct reg_default lm4857_default_regs[] = { |
| 32 | { 0x0, 0x00 }, |
| 33 | { 0x1, 0x00 }, |
| 34 | { 0x2, 0x00 }, |
| 35 | { 0x3, 0x00 }, |
Lars-Peter Clausen | 9b0a25f | 2011-03-07 08:04:55 +0100 | [diff] [blame] | 36 | }; |
| 37 | |
| 38 | /* The register offsets in the cache array */ |
| 39 | #define LM4857_MVOL 0 |
| 40 | #define LM4857_LVOL 1 |
| 41 | #define LM4857_RVOL 2 |
| 42 | #define LM4857_CTRL 3 |
| 43 | |
| 44 | /* the shifts required to set these bits */ |
| 45 | #define LM4857_3D 5 |
| 46 | #define LM4857_WAKEUP 5 |
| 47 | #define LM4857_EPGAIN 4 |
| 48 | |
Lars-Peter Clausen | 9b0a25f | 2011-03-07 08:04:55 +0100 | [diff] [blame] | 49 | static int lm4857_get_mode(struct snd_kcontrol *kcontrol, |
| 50 | struct snd_ctl_elem_value *ucontrol) |
| 51 | { |
Lars-Peter Clausen | ea53bf7 | 2014-03-18 09:02:04 +0100 | [diff] [blame] | 52 | struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol); |
Lars-Peter Clausen | 9b0a25f | 2011-03-07 08:04:55 +0100 | [diff] [blame] | 53 | struct lm4857 *lm4857 = snd_soc_codec_get_drvdata(codec); |
| 54 | |
| 55 | ucontrol->value.integer.value[0] = lm4857->mode; |
| 56 | |
| 57 | return 0; |
| 58 | } |
| 59 | |
| 60 | static int lm4857_set_mode(struct snd_kcontrol *kcontrol, |
| 61 | struct snd_ctl_elem_value *ucontrol) |
| 62 | { |
Lars-Peter Clausen | ea53bf7 | 2014-03-18 09:02:04 +0100 | [diff] [blame] | 63 | struct snd_soc_codec *codec = snd_soc_kcontrol_codec(kcontrol); |
Lars-Peter Clausen | 9b0a25f | 2011-03-07 08:04:55 +0100 | [diff] [blame] | 64 | struct lm4857 *lm4857 = snd_soc_codec_get_drvdata(codec); |
| 65 | uint8_t value = ucontrol->value.integer.value[0]; |
| 66 | |
| 67 | lm4857->mode = value; |
| 68 | |
| 69 | if (codec->dapm.bias_level == SND_SOC_BIAS_ON) |
Lars-Peter Clausen | 9b27096 | 2013-07-28 18:45:29 +0200 | [diff] [blame] | 70 | regmap_update_bits(lm4857->regmap, LM4857_CTRL, 0x0F, value + 6); |
Lars-Peter Clausen | 9b0a25f | 2011-03-07 08:04:55 +0100 | [diff] [blame] | 71 | |
| 72 | return 1; |
| 73 | } |
| 74 | |
| 75 | static int lm4857_set_bias_level(struct snd_soc_codec *codec, |
| 76 | enum snd_soc_bias_level level) |
| 77 | { |
| 78 | struct lm4857 *lm4857 = snd_soc_codec_get_drvdata(codec); |
| 79 | |
| 80 | switch (level) { |
| 81 | case SND_SOC_BIAS_ON: |
Lars-Peter Clausen | 9b27096 | 2013-07-28 18:45:29 +0200 | [diff] [blame] | 82 | regmap_update_bits(lm4857->regmap, LM4857_CTRL, 0x0F, |
| 83 | lm4857->mode + 6); |
Lars-Peter Clausen | 9b0a25f | 2011-03-07 08:04:55 +0100 | [diff] [blame] | 84 | break; |
| 85 | case SND_SOC_BIAS_STANDBY: |
Lars-Peter Clausen | 9b27096 | 2013-07-28 18:45:29 +0200 | [diff] [blame] | 86 | regmap_update_bits(lm4857->regmap, LM4857_CTRL, 0x0F, 0); |
Lars-Peter Clausen | 9b0a25f | 2011-03-07 08:04:55 +0100 | [diff] [blame] | 87 | break; |
| 88 | default: |
| 89 | break; |
| 90 | } |
| 91 | |
| 92 | codec->dapm.bias_level = level; |
| 93 | |
| 94 | return 0; |
| 95 | } |
| 96 | |
| 97 | static const char *lm4857_mode[] = { |
| 98 | "Earpiece", |
| 99 | "Loudspeaker", |
| 100 | "Loudspeaker + Headphone", |
| 101 | "Headphone", |
| 102 | }; |
| 103 | |
Takashi Iwai | 6415e30 | 2014-02-18 10:09:52 +0100 | [diff] [blame] | 104 | static SOC_ENUM_SINGLE_EXT_DECL(lm4857_mode_enum, lm4857_mode); |
Lars-Peter Clausen | 9b0a25f | 2011-03-07 08:04:55 +0100 | [diff] [blame] | 105 | |
| 106 | static const struct snd_soc_dapm_widget lm4857_dapm_widgets[] = { |
| 107 | SND_SOC_DAPM_INPUT("IN"), |
| 108 | |
| 109 | SND_SOC_DAPM_OUTPUT("LS"), |
| 110 | SND_SOC_DAPM_OUTPUT("HP"), |
| 111 | SND_SOC_DAPM_OUTPUT("EP"), |
| 112 | }; |
| 113 | |
| 114 | static const DECLARE_TLV_DB_SCALE(stereo_tlv, -4050, 150, 0); |
| 115 | static const DECLARE_TLV_DB_SCALE(mono_tlv, -3450, 150, 0); |
| 116 | |
| 117 | static const struct snd_kcontrol_new lm4857_controls[] = { |
| 118 | SOC_SINGLE_TLV("Left Playback Volume", LM4857_LVOL, 0, 31, 0, |
| 119 | stereo_tlv), |
| 120 | SOC_SINGLE_TLV("Right Playback Volume", LM4857_RVOL, 0, 31, 0, |
| 121 | stereo_tlv), |
| 122 | SOC_SINGLE_TLV("Mono Playback Volume", LM4857_MVOL, 0, 31, 0, |
| 123 | mono_tlv), |
| 124 | SOC_SINGLE("Spk 3D Playback Switch", LM4857_LVOL, LM4857_3D, 1, 0), |
| 125 | SOC_SINGLE("HP 3D Playback Switch", LM4857_RVOL, LM4857_3D, 1, 0), |
| 126 | SOC_SINGLE("Fast Wakeup Playback Switch", LM4857_CTRL, |
| 127 | LM4857_WAKEUP, 1, 0), |
| 128 | SOC_SINGLE("Earpiece 6dB Playback Switch", LM4857_CTRL, |
| 129 | LM4857_EPGAIN, 1, 0), |
| 130 | |
| 131 | SOC_ENUM_EXT("Mode", lm4857_mode_enum, |
| 132 | lm4857_get_mode, lm4857_set_mode), |
| 133 | }; |
| 134 | |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 135 | /* There is a demux between the input signal and the output signals. |
Lars-Peter Clausen | 9b0a25f | 2011-03-07 08:04:55 +0100 | [diff] [blame] | 136 | * Currently there is no easy way to model it in ASoC and since it does not make |
| 137 | * much of a difference in practice simply connect the input direclty to the |
| 138 | * outputs. */ |
| 139 | static const struct snd_soc_dapm_route lm4857_routes[] = { |
| 140 | {"LS", NULL, "IN"}, |
| 141 | {"HP", NULL, "IN"}, |
| 142 | {"EP", NULL, "IN"}, |
| 143 | }; |
| 144 | |
Lars-Peter Clausen | 9b0a25f | 2011-03-07 08:04:55 +0100 | [diff] [blame] | 145 | static struct snd_soc_codec_driver soc_codec_dev_lm4857 = { |
Lars-Peter Clausen | 9b0a25f | 2011-03-07 08:04:55 +0100 | [diff] [blame] | 146 | .set_bias_level = lm4857_set_bias_level, |
Lars-Peter Clausen | 07ccc0f | 2013-07-28 18:45:28 +0200 | [diff] [blame] | 147 | |
| 148 | .controls = lm4857_controls, |
| 149 | .num_controls = ARRAY_SIZE(lm4857_controls), |
| 150 | .dapm_widgets = lm4857_dapm_widgets, |
| 151 | .num_dapm_widgets = ARRAY_SIZE(lm4857_dapm_widgets), |
| 152 | .dapm_routes = lm4857_routes, |
| 153 | .num_dapm_routes = ARRAY_SIZE(lm4857_routes), |
Lars-Peter Clausen | 9b0a25f | 2011-03-07 08:04:55 +0100 | [diff] [blame] | 154 | }; |
| 155 | |
Lars-Peter Clausen | 9b27096 | 2013-07-28 18:45:29 +0200 | [diff] [blame] | 156 | static const struct regmap_config lm4857_regmap_config = { |
| 157 | .val_bits = 6, |
| 158 | .reg_bits = 2, |
| 159 | |
| 160 | .max_register = LM4857_CTRL, |
| 161 | |
| 162 | .cache_type = REGCACHE_FLAT, |
| 163 | .reg_defaults = lm4857_default_regs, |
| 164 | .num_reg_defaults = ARRAY_SIZE(lm4857_default_regs), |
| 165 | }; |
| 166 | |
Bill Pemberton | 7a79e94 | 2012-12-07 09:26:37 -0500 | [diff] [blame] | 167 | static int lm4857_i2c_probe(struct i2c_client *i2c, |
| 168 | const struct i2c_device_id *id) |
Lars-Peter Clausen | 9b0a25f | 2011-03-07 08:04:55 +0100 | [diff] [blame] | 169 | { |
| 170 | struct lm4857 *lm4857; |
Lars-Peter Clausen | 9b0a25f | 2011-03-07 08:04:55 +0100 | [diff] [blame] | 171 | |
Axel Lin | eb3bb97 | 2011-12-26 20:56:25 +0800 | [diff] [blame] | 172 | lm4857 = devm_kzalloc(&i2c->dev, sizeof(*lm4857), GFP_KERNEL); |
Lars-Peter Clausen | 9b0a25f | 2011-03-07 08:04:55 +0100 | [diff] [blame] | 173 | if (!lm4857) |
| 174 | return -ENOMEM; |
| 175 | |
| 176 | i2c_set_clientdata(i2c, lm4857); |
| 177 | |
Lars-Peter Clausen | 9b27096 | 2013-07-28 18:45:29 +0200 | [diff] [blame] | 178 | lm4857->regmap = devm_regmap_init_i2c(i2c, &lm4857_regmap_config); |
| 179 | if (IS_ERR(lm4857->regmap)) |
| 180 | return PTR_ERR(lm4857->regmap); |
Lars-Peter Clausen | 9b0a25f | 2011-03-07 08:04:55 +0100 | [diff] [blame] | 181 | |
Lars-Peter Clausen | 9b27096 | 2013-07-28 18:45:29 +0200 | [diff] [blame] | 182 | return snd_soc_register_codec(&i2c->dev, &soc_codec_dev_lm4857, NULL, 0); |
Lars-Peter Clausen | 9b0a25f | 2011-03-07 08:04:55 +0100 | [diff] [blame] | 183 | } |
| 184 | |
Bill Pemberton | 7a79e94 | 2012-12-07 09:26:37 -0500 | [diff] [blame] | 185 | static int lm4857_i2c_remove(struct i2c_client *i2c) |
Lars-Peter Clausen | 9b0a25f | 2011-03-07 08:04:55 +0100 | [diff] [blame] | 186 | { |
Lars-Peter Clausen | 9b0a25f | 2011-03-07 08:04:55 +0100 | [diff] [blame] | 187 | snd_soc_unregister_codec(&i2c->dev); |
Lars-Peter Clausen | 9b0a25f | 2011-03-07 08:04:55 +0100 | [diff] [blame] | 188 | return 0; |
| 189 | } |
| 190 | |
| 191 | static const struct i2c_device_id lm4857_i2c_id[] = { |
| 192 | { "lm4857", 0 }, |
| 193 | { } |
| 194 | }; |
| 195 | MODULE_DEVICE_TABLE(i2c, lm4857_i2c_id); |
| 196 | |
| 197 | static struct i2c_driver lm4857_i2c_driver = { |
| 198 | .driver = { |
| 199 | .name = "lm4857", |
| 200 | .owner = THIS_MODULE, |
| 201 | }, |
| 202 | .probe = lm4857_i2c_probe, |
Bill Pemberton | 7a79e94 | 2012-12-07 09:26:37 -0500 | [diff] [blame] | 203 | .remove = lm4857_i2c_remove, |
Lars-Peter Clausen | 9b0a25f | 2011-03-07 08:04:55 +0100 | [diff] [blame] | 204 | .id_table = lm4857_i2c_id, |
| 205 | }; |
| 206 | |
Sachin Kamat | f6ec139 | 2012-08-06 17:25:49 +0530 | [diff] [blame] | 207 | module_i2c_driver(lm4857_i2c_driver); |
Lars-Peter Clausen | 9b0a25f | 2011-03-07 08:04:55 +0100 | [diff] [blame] | 208 | |
| 209 | MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>"); |
| 210 | MODULE_DESCRIPTION("LM4857 amplifier driver"); |
| 211 | MODULE_LICENSE("GPL"); |