| /* |
| * ALSA SoC Texas Instruments TPA6130A2 headset stereo amplifier driver |
| * |
| * Copyright (C) Nokia Corporation |
| * |
| * Author: Peter Ujfalusi <peter.ujfalusi@ti.com> |
| * |
| * This program is free software; you can redistribute it and/or |
| * modify it under the terms of the GNU General Public License |
| * version 2 as published by the Free Software Foundation. |
| * |
| * This program is distributed in the hope that it will be useful, but |
| * WITHOUT ANY WARRANTY; without even the implied warranty of |
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| * General Public License for more details. |
| * |
| * You should have received a copy of the GNU General Public License |
| * along with this program; if not, write to the Free Software |
| * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA |
| * 02110-1301 USA |
| */ |
| |
| #include <linux/module.h> |
| #include <linux/errno.h> |
| #include <linux/device.h> |
| #include <linux/i2c.h> |
| #include <linux/gpio.h> |
| #include <linux/regulator/consumer.h> |
| #include <linux/slab.h> |
| #include <sound/tpa6130a2-plat.h> |
| #include <sound/soc.h> |
| #include <sound/tlv.h> |
| #include <linux/of.h> |
| #include <linux/of_gpio.h> |
| #include <linux/regmap.h> |
| |
| #include "tpa6130a2.h" |
| |
| enum tpa_model { |
| TPA6130A2, |
| TPA6140A2, |
| }; |
| |
| static struct i2c_client *tpa6130a2_client; |
| |
| /* This struct is used to save the context */ |
| struct tpa6130a2_data { |
| struct mutex mutex; |
| struct regmap *regmap; |
| struct regulator *supply; |
| int power_gpio; |
| u8 power_state:1; |
| enum tpa_model id; |
| }; |
| |
| static int tpa6130a2_power(u8 power) |
| { |
| struct tpa6130a2_data *data; |
| int ret = 0; |
| |
| if (WARN_ON(!tpa6130a2_client)) |
| return -EINVAL; |
| data = i2c_get_clientdata(tpa6130a2_client); |
| |
| mutex_lock(&data->mutex); |
| if (power == data->power_state) |
| goto exit; |
| |
| if (power) { |
| ret = regulator_enable(data->supply); |
| if (ret != 0) { |
| dev_err(&tpa6130a2_client->dev, |
| "Failed to enable supply: %d\n", ret); |
| goto exit; |
| } |
| /* Power on */ |
| if (data->power_gpio >= 0) |
| gpio_set_value(data->power_gpio, 1); |
| |
| data->power_state = 1; |
| ret = regcache_sync(data->regmap); |
| if (ret < 0) { |
| dev_err(&tpa6130a2_client->dev, |
| "Failed to initialize chip\n"); |
| if (data->power_gpio >= 0) |
| gpio_set_value(data->power_gpio, 0); |
| regulator_disable(data->supply); |
| data->power_state = 0; |
| goto exit; |
| } |
| } else { |
| /* set SWS */ |
| regmap_update_bits(data->regmap, TPA6130A2_REG_CONTROL, |
| TPA6130A2_SWS, TPA6130A2_SWS); |
| |
| /* Power off */ |
| if (data->power_gpio >= 0) |
| gpio_set_value(data->power_gpio, 0); |
| |
| ret = regulator_disable(data->supply); |
| if (ret != 0) { |
| dev_err(&tpa6130a2_client->dev, |
| "Failed to disable supply: %d\n", ret); |
| goto exit; |
| } |
| |
| data->power_state = 0; |
| /* device regs does not match the cache state anymore */ |
| regcache_mark_dirty(data->regmap); |
| } |
| |
| exit: |
| mutex_unlock(&data->mutex); |
| return ret; |
| } |
| |
| static int tpa6130a2_get_volsw(struct snd_kcontrol *kcontrol, |
| struct snd_ctl_elem_value *ucontrol) |
| { |
| struct soc_mixer_control *mc = |
| (struct soc_mixer_control *)kcontrol->private_value; |
| struct tpa6130a2_data *data; |
| unsigned int reg = mc->reg; |
| unsigned int shift = mc->shift; |
| int max = mc->max, val; |
| unsigned int mask = (1 << fls(max)) - 1; |
| unsigned int invert = mc->invert; |
| |
| if (WARN_ON(!tpa6130a2_client)) |
| return -EINVAL; |
| data = i2c_get_clientdata(tpa6130a2_client); |
| |
| mutex_lock(&data->mutex); |
| |
| regmap_read(data->regmap, reg, &val); |
| ucontrol->value.integer.value[0] = (val >> shift) & mask; |
| |
| if (invert) |
| ucontrol->value.integer.value[0] = |
| max - ucontrol->value.integer.value[0]; |
| |
| mutex_unlock(&data->mutex); |
| return 0; |
| } |
| |
| static int tpa6130a2_put_volsw(struct snd_kcontrol *kcontrol, |
| struct snd_ctl_elem_value *ucontrol) |
| { |
| struct soc_mixer_control *mc = |
| (struct soc_mixer_control *)kcontrol->private_value; |
| struct tpa6130a2_data *data; |
| unsigned int reg = mc->reg; |
| unsigned int shift = mc->shift; |
| int max = mc->max; |
| unsigned int mask = (1 << fls(max)) - 1; |
| unsigned int invert = mc->invert; |
| unsigned int val = (ucontrol->value.integer.value[0] & mask); |
| bool change; |
| |
| if (WARN_ON(!tpa6130a2_client)) |
| return -EINVAL; |
| data = i2c_get_clientdata(tpa6130a2_client); |
| |
| if (invert) |
| val = max - val; |
| |
| mutex_lock(&data->mutex); |
| regmap_update_bits_check(data->regmap, reg, mask << shift, val << shift, |
| &change); |
| mutex_unlock(&data->mutex); |
| |
| return change; |
| } |
| |
| /* |
| * TPA6130 volume. From -59.5 to 4 dB with increasing step size when going |
| * down in gain. |
| */ |
| static const DECLARE_TLV_DB_RANGE(tpa6130_tlv, |
| 0, 1, TLV_DB_SCALE_ITEM(-5950, 600, 0), |
| 2, 3, TLV_DB_SCALE_ITEM(-5000, 250, 0), |
| 4, 5, TLV_DB_SCALE_ITEM(-4550, 160, 0), |
| 6, 7, TLV_DB_SCALE_ITEM(-4140, 190, 0), |
| 8, 9, TLV_DB_SCALE_ITEM(-3650, 120, 0), |
| 10, 11, TLV_DB_SCALE_ITEM(-3330, 160, 0), |
| 12, 13, TLV_DB_SCALE_ITEM(-3040, 180, 0), |
| 14, 20, TLV_DB_SCALE_ITEM(-2710, 110, 0), |
| 21, 37, TLV_DB_SCALE_ITEM(-1960, 74, 0), |
| 38, 63, TLV_DB_SCALE_ITEM(-720, 45, 0) |
| ); |
| |
| static const struct snd_kcontrol_new tpa6130a2_controls[] = { |
| SOC_SINGLE_EXT_TLV("Headphone Playback Volume", |
| TPA6130A2_REG_VOL_MUTE, 0, 0x3f, 0, |
| tpa6130a2_get_volsw, tpa6130a2_put_volsw, |
| tpa6130_tlv), |
| }; |
| |
| static const DECLARE_TLV_DB_RANGE(tpa6140_tlv, |
| 0, 8, TLV_DB_SCALE_ITEM(-5900, 400, 0), |
| 9, 16, TLV_DB_SCALE_ITEM(-2500, 200, 0), |
| 17, 31, TLV_DB_SCALE_ITEM(-1000, 100, 0) |
| ); |
| |
| static const struct snd_kcontrol_new tpa6140a2_controls[] = { |
| SOC_SINGLE_EXT_TLV("Headphone Playback Volume", |
| TPA6130A2_REG_VOL_MUTE, 1, 0x1f, 0, |
| tpa6130a2_get_volsw, tpa6130a2_put_volsw, |
| tpa6140_tlv), |
| }; |
| |
| /* |
| * Enable or disable channel (left or right) |
| * The bit number for mute and amplifier are the same per channel: |
| * bit 6: Right channel |
| * bit 7: Left channel |
| * in both registers. |
| */ |
| static void tpa6130a2_channel_enable(u8 channel, int enable) |
| { |
| struct tpa6130a2_data *data = i2c_get_clientdata(tpa6130a2_client); |
| |
| if (enable) { |
| /* Enable channel */ |
| /* Enable amplifier */ |
| regmap_update_bits(data->regmap, TPA6130A2_REG_CONTROL, |
| channel | TPA6130A2_SWS, channel & ~TPA6130A2_SWS); |
| |
| /* Unmute channel */ |
| regmap_update_bits(data->regmap, TPA6130A2_REG_VOL_MUTE, |
| channel, 0); |
| } else { |
| /* Disable channel */ |
| /* Mute channel */ |
| regmap_update_bits(data->regmap, TPA6130A2_REG_VOL_MUTE, |
| channel, channel); |
| |
| /* Disable amplifier */ |
| regmap_update_bits(data->regmap, TPA6130A2_REG_CONTROL, |
| channel, 0); |
| } |
| } |
| |
| int tpa6130a2_stereo_enable(struct snd_soc_codec *codec, int enable) |
| { |
| int ret = 0; |
| if (enable) { |
| ret = tpa6130a2_power(1); |
| if (ret < 0) |
| return ret; |
| tpa6130a2_channel_enable(TPA6130A2_HP_EN_R | TPA6130A2_HP_EN_L, |
| 1); |
| } else { |
| tpa6130a2_channel_enable(TPA6130A2_HP_EN_R | TPA6130A2_HP_EN_L, |
| 0); |
| ret = tpa6130a2_power(0); |
| } |
| |
| return ret; |
| } |
| EXPORT_SYMBOL_GPL(tpa6130a2_stereo_enable); |
| |
| static int tpa6130a2_component_probe(struct snd_soc_component *component) |
| { |
| struct tpa6130a2_data *data = snd_soc_component_get_drvdata(component); |
| |
| if (data->id == TPA6140A2) |
| return snd_soc_add_component_controls(component, |
| tpa6140a2_controls, ARRAY_SIZE(tpa6140a2_controls)); |
| else |
| return snd_soc_add_component_controls(component, |
| tpa6130a2_controls, ARRAY_SIZE(tpa6130a2_controls)); |
| } |
| |
| struct snd_soc_component_driver tpa6130a2_component_driver = { |
| .name = "tpa6130a2", |
| .probe = tpa6130a2_component_probe, |
| }; |
| |
| static const struct reg_default tpa6130a2_reg_defaults[] = { |
| { TPA6130A2_REG_CONTROL, TPA6130A2_SWS }, |
| { TPA6130A2_REG_VOL_MUTE, TPA6130A2_MUTE_R | TPA6130A2_MUTE_L }, |
| }; |
| |
| static const struct regmap_config tpa6130a2_regmap_config = { |
| .reg_bits = 8, |
| .val_bits = 8, |
| .max_register = TPA6130A2_REG_VERSION, |
| .reg_defaults = tpa6130a2_reg_defaults, |
| .num_reg_defaults = ARRAY_SIZE(tpa6130a2_reg_defaults), |
| .cache_type = REGCACHE_RBTREE, |
| }; |
| |
| static int tpa6130a2_probe(struct i2c_client *client, |
| const struct i2c_device_id *id) |
| { |
| struct device *dev; |
| struct tpa6130a2_data *data; |
| struct tpa6130a2_platform_data *pdata = client->dev.platform_data; |
| struct device_node *np = client->dev.of_node; |
| const char *regulator; |
| unsigned int version; |
| int ret; |
| |
| dev = &client->dev; |
| |
| data = devm_kzalloc(&client->dev, sizeof(*data), GFP_KERNEL); |
| if (!data) |
| return -ENOMEM; |
| |
| data->regmap = devm_regmap_init_i2c(client, &tpa6130a2_regmap_config); |
| if (IS_ERR(data->regmap)) |
| return PTR_ERR(data->regmap); |
| |
| if (pdata) { |
| data->power_gpio = pdata->power_gpio; |
| } else if (np) { |
| data->power_gpio = of_get_named_gpio(np, "power-gpio", 0); |
| } else { |
| dev_err(dev, "Platform data not set\n"); |
| dump_stack(); |
| return -ENODEV; |
| } |
| |
| tpa6130a2_client = client; |
| |
| i2c_set_clientdata(tpa6130a2_client, data); |
| |
| data->id = id->driver_data; |
| |
| mutex_init(&data->mutex); |
| |
| if (data->power_gpio >= 0) { |
| ret = devm_gpio_request(dev, data->power_gpio, |
| "tpa6130a2 enable"); |
| if (ret < 0) { |
| dev_err(dev, "Failed to request power GPIO (%d)\n", |
| data->power_gpio); |
| goto err_gpio; |
| } |
| gpio_direction_output(data->power_gpio, 0); |
| } |
| |
| switch (data->id) { |
| default: |
| dev_warn(dev, "Unknown TPA model (%d). Assuming 6130A2\n", |
| data->id); |
| case TPA6130A2: |
| regulator = "Vdd"; |
| break; |
| case TPA6140A2: |
| regulator = "AVdd"; |
| break; |
| } |
| |
| data->supply = devm_regulator_get(dev, regulator); |
| if (IS_ERR(data->supply)) { |
| ret = PTR_ERR(data->supply); |
| dev_err(dev, "Failed to request supply: %d\n", ret); |
| goto err_gpio; |
| } |
| |
| ret = tpa6130a2_power(1); |
| if (ret != 0) |
| goto err_gpio; |
| |
| |
| /* Read version */ |
| regmap_read(data->regmap, TPA6130A2_REG_VERSION, &version); |
| version &= TPA6130A2_VERSION_MASK; |
| if ((version != 1) && (version != 2)) |
| dev_warn(dev, "UNTESTED version detected (%d)\n", version); |
| |
| /* Disable the chip */ |
| ret = tpa6130a2_power(0); |
| if (ret != 0) |
| goto err_gpio; |
| |
| return devm_snd_soc_register_component(&client->dev, |
| &tpa6130a2_component_driver, NULL, 0); |
| |
| err_gpio: |
| tpa6130a2_client = NULL; |
| |
| return ret; |
| } |
| |
| static int tpa6130a2_remove(struct i2c_client *client) |
| { |
| tpa6130a2_power(0); |
| tpa6130a2_client = NULL; |
| |
| return 0; |
| } |
| |
| static const struct i2c_device_id tpa6130a2_id[] = { |
| { "tpa6130a2", TPA6130A2 }, |
| { "tpa6140a2", TPA6140A2 }, |
| { } |
| }; |
| MODULE_DEVICE_TABLE(i2c, tpa6130a2_id); |
| |
| #if IS_ENABLED(CONFIG_OF) |
| static const struct of_device_id tpa6130a2_of_match[] = { |
| { .compatible = "ti,tpa6130a2", }, |
| { .compatible = "ti,tpa6140a2" }, |
| {}, |
| }; |
| MODULE_DEVICE_TABLE(of, tpa6130a2_of_match); |
| #endif |
| |
| static struct i2c_driver tpa6130a2_i2c_driver = { |
| .driver = { |
| .name = "tpa6130a2", |
| .of_match_table = of_match_ptr(tpa6130a2_of_match), |
| }, |
| .probe = tpa6130a2_probe, |
| .remove = tpa6130a2_remove, |
| .id_table = tpa6130a2_id, |
| }; |
| |
| module_i2c_driver(tpa6130a2_i2c_driver); |
| |
| MODULE_AUTHOR("Peter Ujfalusi <peter.ujfalusi@ti.com>"); |
| MODULE_DESCRIPTION("TPA6130A2 Headphone amplifier driver"); |
| MODULE_LICENSE("GPL"); |