Linux Audio

Check our new training course

Loading...
v6.13.7
  1// SPDX-License-Identifier: GPL-2.0-only
  2/*
  3 * ALSA SoC Texas Instruments TPA6130A2 headset stereo amplifier driver
  4 *
  5 * Copyright (C) Nokia Corporation
  6 *
  7 * Author: Peter Ujfalusi <peter.ujfalusi@ti.com>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
  8 */
  9
 10#include <linux/module.h>
 11#include <linux/errno.h>
 12#include <linux/device.h>
 13#include <linux/i2c.h>
 14#include <linux/gpio.h>
 15#include <linux/regulator/consumer.h>
 16#include <linux/slab.h>
 17#include <sound/tpa6130a2-plat.h>
 18#include <sound/soc.h>
 19#include <sound/tlv.h>
 20#include <linux/of.h>
 21#include <linux/of_gpio.h>
 22#include <linux/regmap.h>
 23
 24#include "tpa6130a2.h"
 25
 26enum tpa_model {
 27	TPA6130A2,
 28	TPA6140A2,
 29};
 30
 
 
 31/* This struct is used to save the context */
 32struct tpa6130a2_data {
 33	struct device *dev;
 34	struct regmap *regmap;
 35	struct regulator *supply;
 36	int power_gpio;
 
 37	enum tpa_model id;
 38};
 39
 40static int tpa6130a2_power(struct tpa6130a2_data *data, bool enable)
 41{
 42	int ret = 0, ret2;
 
 43
 44	if (enable) {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 45		ret = regulator_enable(data->supply);
 46		if (ret != 0) {
 47			dev_err(data->dev,
 48				"Failed to enable supply: %d\n", ret);
 49			return ret;
 50		}
 51		/* Power on */
 52		if (data->power_gpio >= 0)
 53			gpio_set_value(data->power_gpio, 1);
 54
 55		/* Sync registers */
 56		regcache_cache_only(data->regmap, false);
 57		ret = regcache_sync(data->regmap);
 58		if (ret != 0) {
 59			dev_err(data->dev,
 60				"Failed to sync registers: %d\n", ret);
 61			regcache_cache_only(data->regmap, true);
 62			if (data->power_gpio >= 0)
 63				gpio_set_value(data->power_gpio, 0);
 64			ret2 = regulator_disable(data->supply);
 65			if (ret2 != 0)
 66				dev_err(data->dev,
 67					"Failed to disable supply: %d\n", ret2);
 68			return ret;
 69		}
 70	} else {
 71		/* Powered off device does not retain registers. While device
 72		 * is off, any register updates (i.e. volume changes) should
 73		 * happen in cache only.
 74		 */
 75		regcache_mark_dirty(data->regmap);
 76		regcache_cache_only(data->regmap, true);
 77
 78		/* Power off */
 79		if (data->power_gpio >= 0)
 80			gpio_set_value(data->power_gpio, 0);
 81
 82		ret = regulator_disable(data->supply);
 83		if (ret != 0) {
 84			dev_err(data->dev,
 85				"Failed to disable supply: %d\n", ret);
 86			return ret;
 87		}
 
 
 88	}
 89
 
 
 90	return ret;
 91}
 92
 93static int tpa6130a2_power_event(struct snd_soc_dapm_widget *w,
 94				 struct snd_kcontrol *kctrl, int event)
 95{
 96	struct snd_soc_component *c = snd_soc_dapm_to_component(w->dapm);
 97	struct tpa6130a2_data *data = snd_soc_component_get_drvdata(c);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 98
 99	if (SND_SOC_DAPM_EVENT_ON(event)) {
100		/* Before widget power up: turn chip on, sync registers */
101		return tpa6130a2_power(data, true);
102	} else {
103		/* After widget power down: turn chip off */
104		return tpa6130a2_power(data, false);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
105	}
 
 
 
 
 
 
 
 
106}
107
108/*
109 * TPA6130 volume. From -59.5 to 4 dB with increasing step size when going
110 * down in gain.
111 */
112static const DECLARE_TLV_DB_RANGE(tpa6130_tlv,
 
113	0, 1, TLV_DB_SCALE_ITEM(-5950, 600, 0),
114	2, 3, TLV_DB_SCALE_ITEM(-5000, 250, 0),
115	4, 5, TLV_DB_SCALE_ITEM(-4550, 160, 0),
116	6, 7, TLV_DB_SCALE_ITEM(-4140, 190, 0),
117	8, 9, TLV_DB_SCALE_ITEM(-3650, 120, 0),
118	10, 11, TLV_DB_SCALE_ITEM(-3330, 160, 0),
119	12, 13, TLV_DB_SCALE_ITEM(-3040, 180, 0),
120	14, 20, TLV_DB_SCALE_ITEM(-2710, 110, 0),
121	21, 37, TLV_DB_SCALE_ITEM(-1960, 74, 0),
122	38, 63, TLV_DB_SCALE_ITEM(-720, 45, 0)
123);
124
125static const struct snd_kcontrol_new tpa6130a2_controls[] = {
126	SOC_SINGLE_TLV("Headphone Playback Volume",
127		       TPA6130A2_REG_VOL_MUTE, 0, 0x3f, 0,
 
128		       tpa6130_tlv),
129};
130
131static const DECLARE_TLV_DB_RANGE(tpa6140_tlv,
 
132	0, 8, TLV_DB_SCALE_ITEM(-5900, 400, 0),
133	9, 16, TLV_DB_SCALE_ITEM(-2500, 200, 0),
134	17, 31, TLV_DB_SCALE_ITEM(-1000, 100, 0)
135);
136
137static const struct snd_kcontrol_new tpa6140a2_controls[] = {
138	SOC_SINGLE_TLV("Headphone Playback Volume",
139		       TPA6130A2_REG_VOL_MUTE, 1, 0x1f, 0,
 
140		       tpa6140_tlv),
141};
142
143static int tpa6130a2_component_probe(struct snd_soc_component *component)
 
 
 
 
 
 
 
144{
145	struct tpa6130a2_data *data = snd_soc_component_get_drvdata(component);
146
147	if (data->id == TPA6140A2)
148		return snd_soc_add_component_controls(component,
149			tpa6140a2_controls, ARRAY_SIZE(tpa6140a2_controls));
150	else
151		return snd_soc_add_component_controls(component,
152			tpa6130a2_controls, ARRAY_SIZE(tpa6130a2_controls));
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
153}
154
155static const struct snd_soc_dapm_widget tpa6130a2_dapm_widgets[] = {
156	SND_SOC_DAPM_INPUT("LEFTIN"),
157	SND_SOC_DAPM_INPUT("RIGHTIN"),
158	SND_SOC_DAPM_OUTPUT("HPLEFT"),
159	SND_SOC_DAPM_OUTPUT("HPRIGHT"),
160
161	SND_SOC_DAPM_PGA("Left Mute", TPA6130A2_REG_VOL_MUTE,
162			 TPA6130A2_HP_EN_L_SHIFT, 1, NULL, 0),
163	SND_SOC_DAPM_PGA("Right Mute", TPA6130A2_REG_VOL_MUTE,
164			 TPA6130A2_HP_EN_R_SHIFT, 1, NULL, 0),
165	SND_SOC_DAPM_PGA("Left PGA", TPA6130A2_REG_CONTROL,
166			 TPA6130A2_HP_EN_L_SHIFT, 0, NULL, 0),
167	SND_SOC_DAPM_PGA("Right PGA", TPA6130A2_REG_CONTROL,
168			 TPA6130A2_HP_EN_R_SHIFT, 0, NULL, 0),
169
170	SND_SOC_DAPM_SUPPLY("Power", TPA6130A2_REG_CONTROL,
171			    TPA6130A2_SWS_SHIFT, 1, tpa6130a2_power_event,
172			    SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
173};
174
175static const struct snd_soc_dapm_route tpa6130a2_dapm_routes[] = {
176	{ "Left PGA", NULL, "LEFTIN" },
177	{ "Right PGA", NULL, "RIGHTIN" },
178
179	{ "Left Mute", NULL, "Left PGA" },
180	{ "Right Mute", NULL, "Right PGA" },
181
182	{ "HPLEFT", NULL, "Left Mute" },
183	{ "HPRIGHT", NULL, "Right Mute" },
184
185	{ "Left PGA", NULL, "Power" },
186	{ "Right PGA", NULL, "Power" },
187};
188
189static const struct snd_soc_component_driver tpa6130a2_component_driver = {
190	.name = "tpa6130a2",
191	.probe = tpa6130a2_component_probe,
192	.dapm_widgets = tpa6130a2_dapm_widgets,
193	.num_dapm_widgets = ARRAY_SIZE(tpa6130a2_dapm_widgets),
194	.dapm_routes = tpa6130a2_dapm_routes,
195	.num_dapm_routes = ARRAY_SIZE(tpa6130a2_dapm_routes),
196};
197
198static const struct reg_default tpa6130a2_reg_defaults[] = {
199	{ TPA6130A2_REG_CONTROL, TPA6130A2_SWS },
200	{ TPA6130A2_REG_VOL_MUTE, TPA6130A2_MUTE_R | TPA6130A2_MUTE_L },
201};
202
203static const struct regmap_config tpa6130a2_regmap_config = {
204	.reg_bits = 8,
205	.val_bits = 8,
206	.max_register = TPA6130A2_REG_VERSION,
207	.reg_defaults = tpa6130a2_reg_defaults,
208	.num_reg_defaults = ARRAY_SIZE(tpa6130a2_reg_defaults),
209	.cache_type = REGCACHE_RBTREE,
210};
211
212static const struct i2c_device_id tpa6130a2_id[] = {
213	{ "tpa6130a2", TPA6130A2 },
214	{ "tpa6140a2", TPA6140A2 },
215	{ }
216};
217MODULE_DEVICE_TABLE(i2c, tpa6130a2_id);
 
 
218
219static int tpa6130a2_probe(struct i2c_client *client)
 
220{
221	struct device *dev;
222	struct tpa6130a2_data *data;
223	struct tpa6130a2_platform_data *pdata = client->dev.platform_data;
224	struct device_node *np = client->dev.of_node;
225	const struct i2c_device_id *id;
226	const char *regulator;
227	unsigned int version;
228	int ret;
229
230	dev = &client->dev;
231
232	data = devm_kzalloc(&client->dev, sizeof(*data), GFP_KERNEL);
233	if (!data)
 
234		return -ENOMEM;
235
236	data->dev = dev;
237
238	data->regmap = devm_regmap_init_i2c(client, &tpa6130a2_regmap_config);
239	if (IS_ERR(data->regmap))
240		return PTR_ERR(data->regmap);
241
242	if (pdata) {
243		data->power_gpio = pdata->power_gpio;
244	} else if (np) {
245		data->power_gpio = of_get_named_gpio(np, "power-gpio", 0);
246	} else {
247		dev_err(dev, "Platform data not set\n");
248		dump_stack();
249		return -ENODEV;
250	}
251
252	i2c_set_clientdata(client, data);
 
 
253
254	id = i2c_match_id(tpa6130a2_id, client);
255	data->id = id->driver_data;
256
 
 
 
 
 
 
 
257	if (data->power_gpio >= 0) {
258		ret = devm_gpio_request(dev, data->power_gpio,
259					"tpa6130a2 enable");
260		if (ret < 0) {
261			dev_err(dev, "Failed to request power GPIO (%d)\n",
262				data->power_gpio);
263			return ret;
264		}
265		gpio_direction_output(data->power_gpio, 0);
266	}
267
268	switch (data->id) {
269	default:
270		dev_warn(dev, "Unknown TPA model (%d). Assuming 6130A2\n",
271			 data->id);
272		fallthrough;
273	case TPA6130A2:
274		regulator = "Vdd";
275		break;
276	case TPA6140A2:
277		regulator = "AVdd";
278		break;
279	}
280
281	data->supply = devm_regulator_get(dev, regulator);
282	if (IS_ERR(data->supply)) {
283		ret = PTR_ERR(data->supply);
284		dev_err(dev, "Failed to request supply: %d\n", ret);
285		return ret;
286	}
287
288	ret = tpa6130a2_power(data, true);
289	if (ret != 0)
290		return ret;
291
292
293	/* Read version */
294	regmap_read(data->regmap, TPA6130A2_REG_VERSION, &version);
295	version &= TPA6130A2_VERSION_MASK;
296	if ((version != 1) && (version != 2))
297		dev_warn(dev, "UNTESTED version detected (%d)\n", version);
298
299	/* Disable the chip */
300	ret = tpa6130a2_power(data, false);
301	if (ret != 0)
302		return ret;
 
 
303
304	return devm_snd_soc_register_component(&client->dev,
305			&tpa6130a2_component_driver, NULL, 0);
 
 
306}
307
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
308#if IS_ENABLED(CONFIG_OF)
309static const struct of_device_id tpa6130a2_of_match[] = {
310	{ .compatible = "ti,tpa6130a2", },
311	{ .compatible = "ti,tpa6140a2" },
312	{},
313};
314MODULE_DEVICE_TABLE(of, tpa6130a2_of_match);
315#endif
316
317static struct i2c_driver tpa6130a2_i2c_driver = {
318	.driver = {
319		.name = "tpa6130a2",
 
320		.of_match_table = of_match_ptr(tpa6130a2_of_match),
321	},
322	.probe = tpa6130a2_probe,
 
323	.id_table = tpa6130a2_id,
324};
325
326module_i2c_driver(tpa6130a2_i2c_driver);
327
328MODULE_AUTHOR("Peter Ujfalusi <peter.ujfalusi@ti.com>");
329MODULE_DESCRIPTION("TPA6130A2 Headphone amplifier driver");
330MODULE_LICENSE("GPL");
v3.15
 
  1/*
  2 * ALSA SoC Texas Instruments TPA6130A2 headset stereo amplifier driver
  3 *
  4 * Copyright (C) Nokia Corporation
  5 *
  6 * Author: Peter Ujfalusi <peter.ujfalusi@ti.com>
  7 *
  8 * This program is free software; you can redistribute it and/or
  9 * modify it under the terms of the GNU General Public License
 10 * version 2 as published by the Free Software Foundation.
 11 *
 12 * This program is distributed in the hope that it will be useful, but
 13 * WITHOUT ANY WARRANTY; without even the implied warranty of
 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 15 * General Public License for more details.
 16 *
 17 * You should have received a copy of the GNU General Public License
 18 * along with this program; if not, write to the Free Software
 19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 20 * 02110-1301 USA
 21 */
 22
 23#include <linux/module.h>
 24#include <linux/errno.h>
 25#include <linux/device.h>
 26#include <linux/i2c.h>
 27#include <linux/gpio.h>
 28#include <linux/regulator/consumer.h>
 29#include <linux/slab.h>
 30#include <sound/tpa6130a2-plat.h>
 31#include <sound/soc.h>
 32#include <sound/tlv.h>
 
 33#include <linux/of_gpio.h>
 
 34
 35#include "tpa6130a2.h"
 36
 37enum tpa_model {
 38	TPA6130A2,
 39	TPA6140A2,
 40};
 41
 42static struct i2c_client *tpa6130a2_client;
 43
 44/* This struct is used to save the context */
 45struct tpa6130a2_data {
 46	struct mutex mutex;
 47	unsigned char regs[TPA6130A2_CACHEREGNUM];
 48	struct regulator *supply;
 49	int power_gpio;
 50	u8 power_state:1;
 51	enum tpa_model id;
 52};
 53
 54static int tpa6130a2_i2c_read(int reg)
 55{
 56	struct tpa6130a2_data *data;
 57	int val;
 58
 59	if (WARN_ON(!tpa6130a2_client))
 60		return -EINVAL;
 61	data = i2c_get_clientdata(tpa6130a2_client);
 62
 63	/* If powered off, return the cached value */
 64	if (data->power_state) {
 65		val = i2c_smbus_read_byte_data(tpa6130a2_client, reg);
 66		if (val < 0)
 67			dev_err(&tpa6130a2_client->dev, "Read failed\n");
 68		else
 69			data->regs[reg] = val;
 70	} else {
 71		val = data->regs[reg];
 72	}
 73
 74	return val;
 75}
 76
 77static int tpa6130a2_i2c_write(int reg, u8 value)
 78{
 79	struct tpa6130a2_data *data;
 80	int val = 0;
 81
 82	if (WARN_ON(!tpa6130a2_client))
 83		return -EINVAL;
 84	data = i2c_get_clientdata(tpa6130a2_client);
 85
 86	if (data->power_state) {
 87		val = i2c_smbus_write_byte_data(tpa6130a2_client, reg, value);
 88		if (val < 0) {
 89			dev_err(&tpa6130a2_client->dev, "Write failed\n");
 90			return val;
 91		}
 92	}
 93
 94	/* Either powered on or off, we save the context */
 95	data->regs[reg] = value;
 96
 97	return val;
 98}
 99
100static u8 tpa6130a2_read(int reg)
101{
102	struct tpa6130a2_data *data;
103
104	if (WARN_ON(!tpa6130a2_client))
105		return 0;
106	data = i2c_get_clientdata(tpa6130a2_client);
107
108	return data->regs[reg];
109}
110
111static int tpa6130a2_initialize(void)
112{
113	struct tpa6130a2_data *data;
114	int i, ret = 0;
115
116	if (WARN_ON(!tpa6130a2_client))
117		return -EINVAL;
118	data = i2c_get_clientdata(tpa6130a2_client);
119
120	for (i = 1; i < TPA6130A2_REG_VERSION; i++) {
121		ret = tpa6130a2_i2c_write(i, data->regs[i]);
122		if (ret < 0)
123			break;
124	}
125
126	return ret;
127}
128
129static int tpa6130a2_power(u8 power)
130{
131	struct	tpa6130a2_data *data;
132	u8	val;
133	int	ret = 0;
134
135	if (WARN_ON(!tpa6130a2_client))
136		return -EINVAL;
137	data = i2c_get_clientdata(tpa6130a2_client);
138
139	mutex_lock(&data->mutex);
140	if (power == data->power_state)
141		goto exit;
142
143	if (power) {
144		ret = regulator_enable(data->supply);
145		if (ret != 0) {
146			dev_err(&tpa6130a2_client->dev,
147				"Failed to enable supply: %d\n", ret);
148			goto exit;
149		}
150		/* Power on */
151		if (data->power_gpio >= 0)
152			gpio_set_value(data->power_gpio, 1);
153
154		data->power_state = 1;
155		ret = tpa6130a2_initialize();
156		if (ret < 0) {
157			dev_err(&tpa6130a2_client->dev,
158				"Failed to initialize chip\n");
 
 
159			if (data->power_gpio >= 0)
160				gpio_set_value(data->power_gpio, 0);
161			regulator_disable(data->supply);
162			data->power_state = 0;
163			goto exit;
 
 
164		}
165	} else {
166		/* set SWS */
167		val = tpa6130a2_read(TPA6130A2_REG_CONTROL);
168		val |= TPA6130A2_SWS;
169		tpa6130a2_i2c_write(TPA6130A2_REG_CONTROL, val);
 
 
170
171		/* Power off */
172		if (data->power_gpio >= 0)
173			gpio_set_value(data->power_gpio, 0);
174
175		ret = regulator_disable(data->supply);
176		if (ret != 0) {
177			dev_err(&tpa6130a2_client->dev,
178				"Failed to disable supply: %d\n", ret);
179			goto exit;
180		}
181
182		data->power_state = 0;
183	}
184
185exit:
186	mutex_unlock(&data->mutex);
187	return ret;
188}
189
190static int tpa6130a2_get_volsw(struct snd_kcontrol *kcontrol,
191		struct snd_ctl_elem_value *ucontrol)
192{
193	struct soc_mixer_control *mc =
194		(struct soc_mixer_control *)kcontrol->private_value;
195	struct tpa6130a2_data *data;
196	unsigned int reg = mc->reg;
197	unsigned int shift = mc->shift;
198	int max = mc->max;
199	unsigned int mask = (1 << fls(max)) - 1;
200	unsigned int invert = mc->invert;
201
202	if (WARN_ON(!tpa6130a2_client))
203		return -EINVAL;
204	data = i2c_get_clientdata(tpa6130a2_client);
205
206	mutex_lock(&data->mutex);
207
208	ucontrol->value.integer.value[0] =
209		(tpa6130a2_read(reg) >> shift) & mask;
210
211	if (invert)
212		ucontrol->value.integer.value[0] =
213			max - ucontrol->value.integer.value[0];
214
215	mutex_unlock(&data->mutex);
216	return 0;
217}
218
219static int tpa6130a2_put_volsw(struct snd_kcontrol *kcontrol,
220		struct snd_ctl_elem_value *ucontrol)
221{
222	struct soc_mixer_control *mc =
223		(struct soc_mixer_control *)kcontrol->private_value;
224	struct tpa6130a2_data *data;
225	unsigned int reg = mc->reg;
226	unsigned int shift = mc->shift;
227	int max = mc->max;
228	unsigned int mask = (1 << fls(max)) - 1;
229	unsigned int invert = mc->invert;
230	unsigned int val = (ucontrol->value.integer.value[0] & mask);
231	unsigned int val_reg;
232
233	if (WARN_ON(!tpa6130a2_client))
234		return -EINVAL;
235	data = i2c_get_clientdata(tpa6130a2_client);
236
237	if (invert)
238		val = max - val;
239
240	mutex_lock(&data->mutex);
241
242	val_reg = tpa6130a2_read(reg);
243	if (((val_reg >> shift) & mask) == val) {
244		mutex_unlock(&data->mutex);
245		return 0;
246	}
247
248	val_reg &= ~(mask << shift);
249	val_reg |= val << shift;
250	tpa6130a2_i2c_write(reg, val_reg);
251
252	mutex_unlock(&data->mutex);
253
254	return 1;
255}
256
257/*
258 * TPA6130 volume. From -59.5 to 4 dB with increasing step size when going
259 * down in gain.
260 */
261static const unsigned int tpa6130_tlv[] = {
262	TLV_DB_RANGE_HEAD(10),
263	0, 1, TLV_DB_SCALE_ITEM(-5950, 600, 0),
264	2, 3, TLV_DB_SCALE_ITEM(-5000, 250, 0),
265	4, 5, TLV_DB_SCALE_ITEM(-4550, 160, 0),
266	6, 7, TLV_DB_SCALE_ITEM(-4140, 190, 0),
267	8, 9, TLV_DB_SCALE_ITEM(-3650, 120, 0),
268	10, 11, TLV_DB_SCALE_ITEM(-3330, 160, 0),
269	12, 13, TLV_DB_SCALE_ITEM(-3040, 180, 0),
270	14, 20, TLV_DB_SCALE_ITEM(-2710, 110, 0),
271	21, 37, TLV_DB_SCALE_ITEM(-1960, 74, 0),
272	38, 63, TLV_DB_SCALE_ITEM(-720, 45, 0),
273};
274
275static const struct snd_kcontrol_new tpa6130a2_controls[] = {
276	SOC_SINGLE_EXT_TLV("TPA6130A2 Headphone Playback Volume",
277		       TPA6130A2_REG_VOL_MUTE, 0, 0x3f, 0,
278		       tpa6130a2_get_volsw, tpa6130a2_put_volsw,
279		       tpa6130_tlv),
280};
281
282static const unsigned int tpa6140_tlv[] = {
283	TLV_DB_RANGE_HEAD(3),
284	0, 8, TLV_DB_SCALE_ITEM(-5900, 400, 0),
285	9, 16, TLV_DB_SCALE_ITEM(-2500, 200, 0),
286	17, 31, TLV_DB_SCALE_ITEM(-1000, 100, 0),
287};
288
289static const struct snd_kcontrol_new tpa6140a2_controls[] = {
290	SOC_SINGLE_EXT_TLV("TPA6140A2 Headphone Playback Volume",
291		       TPA6130A2_REG_VOL_MUTE, 1, 0x1f, 0,
292		       tpa6130a2_get_volsw, tpa6130a2_put_volsw,
293		       tpa6140_tlv),
294};
295
296/*
297 * Enable or disable channel (left or right)
298 * The bit number for mute and amplifier are the same per channel:
299 * bit 6: Right channel
300 * bit 7: Left channel
301 * in both registers.
302 */
303static void tpa6130a2_channel_enable(u8 channel, int enable)
304{
305	u8	val;
306
307	if (enable) {
308		/* Enable channel */
309		/* Enable amplifier */
310		val = tpa6130a2_read(TPA6130A2_REG_CONTROL);
311		val |= channel;
312		val &= ~TPA6130A2_SWS;
313		tpa6130a2_i2c_write(TPA6130A2_REG_CONTROL, val);
314
315		/* Unmute channel */
316		val = tpa6130a2_read(TPA6130A2_REG_VOL_MUTE);
317		val &= ~channel;
318		tpa6130a2_i2c_write(TPA6130A2_REG_VOL_MUTE, val);
319	} else {
320		/* Disable channel */
321		/* Mute channel */
322		val = tpa6130a2_read(TPA6130A2_REG_VOL_MUTE);
323		val |= channel;
324		tpa6130a2_i2c_write(TPA6130A2_REG_VOL_MUTE, val);
325
326		/* Disable amplifier */
327		val = tpa6130a2_read(TPA6130A2_REG_CONTROL);
328		val &= ~channel;
329		tpa6130a2_i2c_write(TPA6130A2_REG_CONTROL, val);
330	}
331}
332
333int tpa6130a2_stereo_enable(struct snd_soc_codec *codec, int enable)
334{
335	int ret = 0;
336	if (enable) {
337		ret = tpa6130a2_power(1);
338		if (ret < 0)
339			return ret;
340		tpa6130a2_channel_enable(TPA6130A2_HP_EN_R | TPA6130A2_HP_EN_L,
341					 1);
342	} else {
343		tpa6130a2_channel_enable(TPA6130A2_HP_EN_R | TPA6130A2_HP_EN_L,
344					 0);
345		ret = tpa6130a2_power(0);
346	}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
347
348	return ret;
349}
350EXPORT_SYMBOL_GPL(tpa6130a2_stereo_enable);
351
352int tpa6130a2_add_controls(struct snd_soc_codec *codec)
353{
354	struct	tpa6130a2_data *data;
 
 
 
 
 
355
356	if (tpa6130a2_client == NULL)
357		return -ENODEV;
 
 
358
359	data = i2c_get_clientdata(tpa6130a2_client);
 
 
 
 
 
 
 
360
361	if (data->id == TPA6140A2)
362		return snd_soc_add_codec_controls(codec, tpa6140a2_controls,
363						ARRAY_SIZE(tpa6140a2_controls));
364	else
365		return snd_soc_add_codec_controls(codec, tpa6130a2_controls,
366						ARRAY_SIZE(tpa6130a2_controls));
367}
368EXPORT_SYMBOL_GPL(tpa6130a2_add_controls);
369
370static int tpa6130a2_probe(struct i2c_client *client,
371			   const struct i2c_device_id *id)
372{
373	struct device *dev;
374	struct tpa6130a2_data *data;
375	struct tpa6130a2_platform_data *pdata = client->dev.platform_data;
376	struct device_node *np = client->dev.of_node;
 
377	const char *regulator;
 
378	int ret;
379
380	dev = &client->dev;
381
382	data = devm_kzalloc(&client->dev, sizeof(*data), GFP_KERNEL);
383	if (data == NULL) {
384		dev_err(dev, "Can not allocate memory\n");
385		return -ENOMEM;
386	}
 
 
 
 
 
387
388	if (pdata) {
389		data->power_gpio = pdata->power_gpio;
390	} else if (np) {
391		data->power_gpio = of_get_named_gpio(np, "power-gpio", 0);
392	} else {
393		dev_err(dev, "Platform data not set\n");
394		dump_stack();
395		return -ENODEV;
396	}
397
398	tpa6130a2_client = client;
399
400	i2c_set_clientdata(tpa6130a2_client, data);
401
 
402	data->id = id->driver_data;
403
404	mutex_init(&data->mutex);
405
406	/* Set default register values */
407	data->regs[TPA6130A2_REG_CONTROL] =	TPA6130A2_SWS;
408	data->regs[TPA6130A2_REG_VOL_MUTE] =	TPA6130A2_MUTE_R |
409						TPA6130A2_MUTE_L;
410
411	if (data->power_gpio >= 0) {
412		ret = devm_gpio_request(dev, data->power_gpio,
413					"tpa6130a2 enable");
414		if (ret < 0) {
415			dev_err(dev, "Failed to request power GPIO (%d)\n",
416				data->power_gpio);
417			goto err_gpio;
418		}
419		gpio_direction_output(data->power_gpio, 0);
420	}
421
422	switch (data->id) {
423	default:
424		dev_warn(dev, "Unknown TPA model (%d). Assuming 6130A2\n",
425			 data->id);
 
426	case TPA6130A2:
427		regulator = "Vdd";
428		break;
429	case TPA6140A2:
430		regulator = "AVdd";
431		break;
432	}
433
434	data->supply = devm_regulator_get(dev, regulator);
435	if (IS_ERR(data->supply)) {
436		ret = PTR_ERR(data->supply);
437		dev_err(dev, "Failed to request supply: %d\n", ret);
438		goto err_gpio;
439	}
440
441	ret = tpa6130a2_power(1);
442	if (ret != 0)
443		goto err_gpio;
444
445
446	/* Read version */
447	ret = tpa6130a2_i2c_read(TPA6130A2_REG_VERSION) &
448				 TPA6130A2_VERSION_MASK;
449	if ((ret != 1) && (ret != 2))
450		dev_warn(dev, "UNTESTED version detected (%d)\n", ret);
451
452	/* Disable the chip */
453	ret = tpa6130a2_power(0);
454	if (ret != 0)
455		goto err_gpio;
456
457	return 0;
458
459err_gpio:
460	tpa6130a2_client = NULL;
461
462	return ret;
463}
464
465static int tpa6130a2_remove(struct i2c_client *client)
466{
467	tpa6130a2_power(0);
468	tpa6130a2_client = NULL;
469
470	return 0;
471}
472
473static const struct i2c_device_id tpa6130a2_id[] = {
474	{ "tpa6130a2", TPA6130A2 },
475	{ "tpa6140a2", TPA6140A2 },
476	{ }
477};
478MODULE_DEVICE_TABLE(i2c, tpa6130a2_id);
479
480#if IS_ENABLED(CONFIG_OF)
481static const struct of_device_id tpa6130a2_of_match[] = {
482	{ .compatible = "ti,tpa6130a2", },
483	{ .compatible = "ti,tpa6140a2" },
484	{},
485};
486MODULE_DEVICE_TABLE(of, tpa6130a2_of_match);
487#endif
488
489static struct i2c_driver tpa6130a2_i2c_driver = {
490	.driver = {
491		.name = "tpa6130a2",
492		.owner = THIS_MODULE,
493		.of_match_table = of_match_ptr(tpa6130a2_of_match),
494	},
495	.probe = tpa6130a2_probe,
496	.remove = tpa6130a2_remove,
497	.id_table = tpa6130a2_id,
498};
499
500module_i2c_driver(tpa6130a2_i2c_driver);
501
502MODULE_AUTHOR("Peter Ujfalusi <peter.ujfalusi@ti.com>");
503MODULE_DESCRIPTION("TPA6130A2 Headphone amplifier driver");
504MODULE_LICENSE("GPL");