1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * ALSA SoC Texas Instruments TPA6130A2 headset stereo amplifier driver
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * Copyright (C) Nokia Corporation
6*4882a593Smuzhiyun *
7*4882a593Smuzhiyun * Author: Peter Ujfalusi <peter.ujfalusi@ti.com>
8*4882a593Smuzhiyun */
9*4882a593Smuzhiyun
10*4882a593Smuzhiyun #include <linux/module.h>
11*4882a593Smuzhiyun #include <linux/errno.h>
12*4882a593Smuzhiyun #include <linux/device.h>
13*4882a593Smuzhiyun #include <linux/i2c.h>
14*4882a593Smuzhiyun #include <linux/gpio.h>
15*4882a593Smuzhiyun #include <linux/regulator/consumer.h>
16*4882a593Smuzhiyun #include <linux/slab.h>
17*4882a593Smuzhiyun #include <sound/tpa6130a2-plat.h>
18*4882a593Smuzhiyun #include <sound/soc.h>
19*4882a593Smuzhiyun #include <sound/tlv.h>
20*4882a593Smuzhiyun #include <linux/of.h>
21*4882a593Smuzhiyun #include <linux/of_gpio.h>
22*4882a593Smuzhiyun #include <linux/regmap.h>
23*4882a593Smuzhiyun
24*4882a593Smuzhiyun #include "tpa6130a2.h"
25*4882a593Smuzhiyun
26*4882a593Smuzhiyun enum tpa_model {
27*4882a593Smuzhiyun TPA6130A2,
28*4882a593Smuzhiyun TPA6140A2,
29*4882a593Smuzhiyun };
30*4882a593Smuzhiyun
31*4882a593Smuzhiyun /* This struct is used to save the context */
32*4882a593Smuzhiyun struct tpa6130a2_data {
33*4882a593Smuzhiyun struct device *dev;
34*4882a593Smuzhiyun struct regmap *regmap;
35*4882a593Smuzhiyun struct regulator *supply;
36*4882a593Smuzhiyun int power_gpio;
37*4882a593Smuzhiyun enum tpa_model id;
38*4882a593Smuzhiyun };
39*4882a593Smuzhiyun
tpa6130a2_power(struct tpa6130a2_data * data,bool enable)40*4882a593Smuzhiyun static int tpa6130a2_power(struct tpa6130a2_data *data, bool enable)
41*4882a593Smuzhiyun {
42*4882a593Smuzhiyun int ret = 0, ret2;
43*4882a593Smuzhiyun
44*4882a593Smuzhiyun if (enable) {
45*4882a593Smuzhiyun ret = regulator_enable(data->supply);
46*4882a593Smuzhiyun if (ret != 0) {
47*4882a593Smuzhiyun dev_err(data->dev,
48*4882a593Smuzhiyun "Failed to enable supply: %d\n", ret);
49*4882a593Smuzhiyun return ret;
50*4882a593Smuzhiyun }
51*4882a593Smuzhiyun /* Power on */
52*4882a593Smuzhiyun if (data->power_gpio >= 0)
53*4882a593Smuzhiyun gpio_set_value(data->power_gpio, 1);
54*4882a593Smuzhiyun
55*4882a593Smuzhiyun /* Sync registers */
56*4882a593Smuzhiyun regcache_cache_only(data->regmap, false);
57*4882a593Smuzhiyun ret = regcache_sync(data->regmap);
58*4882a593Smuzhiyun if (ret != 0) {
59*4882a593Smuzhiyun dev_err(data->dev,
60*4882a593Smuzhiyun "Failed to sync registers: %d\n", ret);
61*4882a593Smuzhiyun regcache_cache_only(data->regmap, true);
62*4882a593Smuzhiyun if (data->power_gpio >= 0)
63*4882a593Smuzhiyun gpio_set_value(data->power_gpio, 0);
64*4882a593Smuzhiyun ret2 = regulator_disable(data->supply);
65*4882a593Smuzhiyun if (ret2 != 0)
66*4882a593Smuzhiyun dev_err(data->dev,
67*4882a593Smuzhiyun "Failed to disable supply: %d\n", ret2);
68*4882a593Smuzhiyun return ret;
69*4882a593Smuzhiyun }
70*4882a593Smuzhiyun } else {
71*4882a593Smuzhiyun /* Powered off device does not retain registers. While device
72*4882a593Smuzhiyun * is off, any register updates (i.e. volume changes) should
73*4882a593Smuzhiyun * happen in cache only.
74*4882a593Smuzhiyun */
75*4882a593Smuzhiyun regcache_mark_dirty(data->regmap);
76*4882a593Smuzhiyun regcache_cache_only(data->regmap, true);
77*4882a593Smuzhiyun
78*4882a593Smuzhiyun /* Power off */
79*4882a593Smuzhiyun if (data->power_gpio >= 0)
80*4882a593Smuzhiyun gpio_set_value(data->power_gpio, 0);
81*4882a593Smuzhiyun
82*4882a593Smuzhiyun ret = regulator_disable(data->supply);
83*4882a593Smuzhiyun if (ret != 0) {
84*4882a593Smuzhiyun dev_err(data->dev,
85*4882a593Smuzhiyun "Failed to disable supply: %d\n", ret);
86*4882a593Smuzhiyun return ret;
87*4882a593Smuzhiyun }
88*4882a593Smuzhiyun }
89*4882a593Smuzhiyun
90*4882a593Smuzhiyun return ret;
91*4882a593Smuzhiyun }
92*4882a593Smuzhiyun
tpa6130a2_power_event(struct snd_soc_dapm_widget * w,struct snd_kcontrol * kctrl,int event)93*4882a593Smuzhiyun static int tpa6130a2_power_event(struct snd_soc_dapm_widget *w,
94*4882a593Smuzhiyun struct snd_kcontrol *kctrl, int event)
95*4882a593Smuzhiyun {
96*4882a593Smuzhiyun struct snd_soc_component *c = snd_soc_dapm_to_component(w->dapm);
97*4882a593Smuzhiyun struct tpa6130a2_data *data = snd_soc_component_get_drvdata(c);
98*4882a593Smuzhiyun
99*4882a593Smuzhiyun if (SND_SOC_DAPM_EVENT_ON(event)) {
100*4882a593Smuzhiyun /* Before widget power up: turn chip on, sync registers */
101*4882a593Smuzhiyun return tpa6130a2_power(data, true);
102*4882a593Smuzhiyun } else {
103*4882a593Smuzhiyun /* After widget power down: turn chip off */
104*4882a593Smuzhiyun return tpa6130a2_power(data, false);
105*4882a593Smuzhiyun }
106*4882a593Smuzhiyun }
107*4882a593Smuzhiyun
108*4882a593Smuzhiyun /*
109*4882a593Smuzhiyun * TPA6130 volume. From -59.5 to 4 dB with increasing step size when going
110*4882a593Smuzhiyun * down in gain.
111*4882a593Smuzhiyun */
112*4882a593Smuzhiyun static const DECLARE_TLV_DB_RANGE(tpa6130_tlv,
113*4882a593Smuzhiyun 0, 1, TLV_DB_SCALE_ITEM(-5950, 600, 0),
114*4882a593Smuzhiyun 2, 3, TLV_DB_SCALE_ITEM(-5000, 250, 0),
115*4882a593Smuzhiyun 4, 5, TLV_DB_SCALE_ITEM(-4550, 160, 0),
116*4882a593Smuzhiyun 6, 7, TLV_DB_SCALE_ITEM(-4140, 190, 0),
117*4882a593Smuzhiyun 8, 9, TLV_DB_SCALE_ITEM(-3650, 120, 0),
118*4882a593Smuzhiyun 10, 11, TLV_DB_SCALE_ITEM(-3330, 160, 0),
119*4882a593Smuzhiyun 12, 13, TLV_DB_SCALE_ITEM(-3040, 180, 0),
120*4882a593Smuzhiyun 14, 20, TLV_DB_SCALE_ITEM(-2710, 110, 0),
121*4882a593Smuzhiyun 21, 37, TLV_DB_SCALE_ITEM(-1960, 74, 0),
122*4882a593Smuzhiyun 38, 63, TLV_DB_SCALE_ITEM(-720, 45, 0)
123*4882a593Smuzhiyun );
124*4882a593Smuzhiyun
125*4882a593Smuzhiyun static const struct snd_kcontrol_new tpa6130a2_controls[] = {
126*4882a593Smuzhiyun SOC_SINGLE_TLV("Headphone Playback Volume",
127*4882a593Smuzhiyun TPA6130A2_REG_VOL_MUTE, 0, 0x3f, 0,
128*4882a593Smuzhiyun tpa6130_tlv),
129*4882a593Smuzhiyun };
130*4882a593Smuzhiyun
131*4882a593Smuzhiyun static const DECLARE_TLV_DB_RANGE(tpa6140_tlv,
132*4882a593Smuzhiyun 0, 8, TLV_DB_SCALE_ITEM(-5900, 400, 0),
133*4882a593Smuzhiyun 9, 16, TLV_DB_SCALE_ITEM(-2500, 200, 0),
134*4882a593Smuzhiyun 17, 31, TLV_DB_SCALE_ITEM(-1000, 100, 0)
135*4882a593Smuzhiyun );
136*4882a593Smuzhiyun
137*4882a593Smuzhiyun static const struct snd_kcontrol_new tpa6140a2_controls[] = {
138*4882a593Smuzhiyun SOC_SINGLE_TLV("Headphone Playback Volume",
139*4882a593Smuzhiyun TPA6130A2_REG_VOL_MUTE, 1, 0x1f, 0,
140*4882a593Smuzhiyun tpa6140_tlv),
141*4882a593Smuzhiyun };
142*4882a593Smuzhiyun
tpa6130a2_component_probe(struct snd_soc_component * component)143*4882a593Smuzhiyun static int tpa6130a2_component_probe(struct snd_soc_component *component)
144*4882a593Smuzhiyun {
145*4882a593Smuzhiyun struct tpa6130a2_data *data = snd_soc_component_get_drvdata(component);
146*4882a593Smuzhiyun
147*4882a593Smuzhiyun if (data->id == TPA6140A2)
148*4882a593Smuzhiyun return snd_soc_add_component_controls(component,
149*4882a593Smuzhiyun tpa6140a2_controls, ARRAY_SIZE(tpa6140a2_controls));
150*4882a593Smuzhiyun else
151*4882a593Smuzhiyun return snd_soc_add_component_controls(component,
152*4882a593Smuzhiyun tpa6130a2_controls, ARRAY_SIZE(tpa6130a2_controls));
153*4882a593Smuzhiyun }
154*4882a593Smuzhiyun
155*4882a593Smuzhiyun static const struct snd_soc_dapm_widget tpa6130a2_dapm_widgets[] = {
156*4882a593Smuzhiyun SND_SOC_DAPM_INPUT("LEFTIN"),
157*4882a593Smuzhiyun SND_SOC_DAPM_INPUT("RIGHTIN"),
158*4882a593Smuzhiyun SND_SOC_DAPM_OUTPUT("HPLEFT"),
159*4882a593Smuzhiyun SND_SOC_DAPM_OUTPUT("HPRIGHT"),
160*4882a593Smuzhiyun
161*4882a593Smuzhiyun SND_SOC_DAPM_PGA("Left Mute", TPA6130A2_REG_VOL_MUTE,
162*4882a593Smuzhiyun TPA6130A2_HP_EN_L_SHIFT, 1, NULL, 0),
163*4882a593Smuzhiyun SND_SOC_DAPM_PGA("Right Mute", TPA6130A2_REG_VOL_MUTE,
164*4882a593Smuzhiyun TPA6130A2_HP_EN_R_SHIFT, 1, NULL, 0),
165*4882a593Smuzhiyun SND_SOC_DAPM_PGA("Left PGA", TPA6130A2_REG_CONTROL,
166*4882a593Smuzhiyun TPA6130A2_HP_EN_L_SHIFT, 0, NULL, 0),
167*4882a593Smuzhiyun SND_SOC_DAPM_PGA("Right PGA", TPA6130A2_REG_CONTROL,
168*4882a593Smuzhiyun TPA6130A2_HP_EN_R_SHIFT, 0, NULL, 0),
169*4882a593Smuzhiyun
170*4882a593Smuzhiyun SND_SOC_DAPM_SUPPLY("Power", TPA6130A2_REG_CONTROL,
171*4882a593Smuzhiyun TPA6130A2_SWS_SHIFT, 1, tpa6130a2_power_event,
172*4882a593Smuzhiyun SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
173*4882a593Smuzhiyun };
174*4882a593Smuzhiyun
175*4882a593Smuzhiyun static const struct snd_soc_dapm_route tpa6130a2_dapm_routes[] = {
176*4882a593Smuzhiyun { "Left PGA", NULL, "LEFTIN" },
177*4882a593Smuzhiyun { "Right PGA", NULL, "RIGHTIN" },
178*4882a593Smuzhiyun
179*4882a593Smuzhiyun { "Left Mute", NULL, "Left PGA" },
180*4882a593Smuzhiyun { "Right Mute", NULL, "Right PGA" },
181*4882a593Smuzhiyun
182*4882a593Smuzhiyun { "HPLEFT", NULL, "Left Mute" },
183*4882a593Smuzhiyun { "HPRIGHT", NULL, "Right Mute" },
184*4882a593Smuzhiyun
185*4882a593Smuzhiyun { "Left PGA", NULL, "Power" },
186*4882a593Smuzhiyun { "Right PGA", NULL, "Power" },
187*4882a593Smuzhiyun };
188*4882a593Smuzhiyun
189*4882a593Smuzhiyun static const struct snd_soc_component_driver tpa6130a2_component_driver = {
190*4882a593Smuzhiyun .name = "tpa6130a2",
191*4882a593Smuzhiyun .probe = tpa6130a2_component_probe,
192*4882a593Smuzhiyun .dapm_widgets = tpa6130a2_dapm_widgets,
193*4882a593Smuzhiyun .num_dapm_widgets = ARRAY_SIZE(tpa6130a2_dapm_widgets),
194*4882a593Smuzhiyun .dapm_routes = tpa6130a2_dapm_routes,
195*4882a593Smuzhiyun .num_dapm_routes = ARRAY_SIZE(tpa6130a2_dapm_routes),
196*4882a593Smuzhiyun };
197*4882a593Smuzhiyun
198*4882a593Smuzhiyun static const struct reg_default tpa6130a2_reg_defaults[] = {
199*4882a593Smuzhiyun { TPA6130A2_REG_CONTROL, TPA6130A2_SWS },
200*4882a593Smuzhiyun { TPA6130A2_REG_VOL_MUTE, TPA6130A2_MUTE_R | TPA6130A2_MUTE_L },
201*4882a593Smuzhiyun };
202*4882a593Smuzhiyun
203*4882a593Smuzhiyun static const struct regmap_config tpa6130a2_regmap_config = {
204*4882a593Smuzhiyun .reg_bits = 8,
205*4882a593Smuzhiyun .val_bits = 8,
206*4882a593Smuzhiyun .max_register = TPA6130A2_REG_VERSION,
207*4882a593Smuzhiyun .reg_defaults = tpa6130a2_reg_defaults,
208*4882a593Smuzhiyun .num_reg_defaults = ARRAY_SIZE(tpa6130a2_reg_defaults),
209*4882a593Smuzhiyun .cache_type = REGCACHE_RBTREE,
210*4882a593Smuzhiyun };
211*4882a593Smuzhiyun
tpa6130a2_probe(struct i2c_client * client,const struct i2c_device_id * id)212*4882a593Smuzhiyun static int tpa6130a2_probe(struct i2c_client *client,
213*4882a593Smuzhiyun const struct i2c_device_id *id)
214*4882a593Smuzhiyun {
215*4882a593Smuzhiyun struct device *dev;
216*4882a593Smuzhiyun struct tpa6130a2_data *data;
217*4882a593Smuzhiyun struct tpa6130a2_platform_data *pdata = client->dev.platform_data;
218*4882a593Smuzhiyun struct device_node *np = client->dev.of_node;
219*4882a593Smuzhiyun const char *regulator;
220*4882a593Smuzhiyun unsigned int version;
221*4882a593Smuzhiyun int ret;
222*4882a593Smuzhiyun
223*4882a593Smuzhiyun dev = &client->dev;
224*4882a593Smuzhiyun
225*4882a593Smuzhiyun data = devm_kzalloc(&client->dev, sizeof(*data), GFP_KERNEL);
226*4882a593Smuzhiyun if (!data)
227*4882a593Smuzhiyun return -ENOMEM;
228*4882a593Smuzhiyun
229*4882a593Smuzhiyun data->dev = dev;
230*4882a593Smuzhiyun
231*4882a593Smuzhiyun data->regmap = devm_regmap_init_i2c(client, &tpa6130a2_regmap_config);
232*4882a593Smuzhiyun if (IS_ERR(data->regmap))
233*4882a593Smuzhiyun return PTR_ERR(data->regmap);
234*4882a593Smuzhiyun
235*4882a593Smuzhiyun if (pdata) {
236*4882a593Smuzhiyun data->power_gpio = pdata->power_gpio;
237*4882a593Smuzhiyun } else if (np) {
238*4882a593Smuzhiyun data->power_gpio = of_get_named_gpio(np, "power-gpio", 0);
239*4882a593Smuzhiyun } else {
240*4882a593Smuzhiyun dev_err(dev, "Platform data not set\n");
241*4882a593Smuzhiyun dump_stack();
242*4882a593Smuzhiyun return -ENODEV;
243*4882a593Smuzhiyun }
244*4882a593Smuzhiyun
245*4882a593Smuzhiyun i2c_set_clientdata(client, data);
246*4882a593Smuzhiyun
247*4882a593Smuzhiyun data->id = id->driver_data;
248*4882a593Smuzhiyun
249*4882a593Smuzhiyun if (data->power_gpio >= 0) {
250*4882a593Smuzhiyun ret = devm_gpio_request(dev, data->power_gpio,
251*4882a593Smuzhiyun "tpa6130a2 enable");
252*4882a593Smuzhiyun if (ret < 0) {
253*4882a593Smuzhiyun dev_err(dev, "Failed to request power GPIO (%d)\n",
254*4882a593Smuzhiyun data->power_gpio);
255*4882a593Smuzhiyun return ret;
256*4882a593Smuzhiyun }
257*4882a593Smuzhiyun gpio_direction_output(data->power_gpio, 0);
258*4882a593Smuzhiyun }
259*4882a593Smuzhiyun
260*4882a593Smuzhiyun switch (data->id) {
261*4882a593Smuzhiyun default:
262*4882a593Smuzhiyun dev_warn(dev, "Unknown TPA model (%d). Assuming 6130A2\n",
263*4882a593Smuzhiyun data->id);
264*4882a593Smuzhiyun fallthrough;
265*4882a593Smuzhiyun case TPA6130A2:
266*4882a593Smuzhiyun regulator = "Vdd";
267*4882a593Smuzhiyun break;
268*4882a593Smuzhiyun case TPA6140A2:
269*4882a593Smuzhiyun regulator = "AVdd";
270*4882a593Smuzhiyun break;
271*4882a593Smuzhiyun }
272*4882a593Smuzhiyun
273*4882a593Smuzhiyun data->supply = devm_regulator_get(dev, regulator);
274*4882a593Smuzhiyun if (IS_ERR(data->supply)) {
275*4882a593Smuzhiyun ret = PTR_ERR(data->supply);
276*4882a593Smuzhiyun dev_err(dev, "Failed to request supply: %d\n", ret);
277*4882a593Smuzhiyun return ret;
278*4882a593Smuzhiyun }
279*4882a593Smuzhiyun
280*4882a593Smuzhiyun ret = tpa6130a2_power(data, true);
281*4882a593Smuzhiyun if (ret != 0)
282*4882a593Smuzhiyun return ret;
283*4882a593Smuzhiyun
284*4882a593Smuzhiyun
285*4882a593Smuzhiyun /* Read version */
286*4882a593Smuzhiyun regmap_read(data->regmap, TPA6130A2_REG_VERSION, &version);
287*4882a593Smuzhiyun version &= TPA6130A2_VERSION_MASK;
288*4882a593Smuzhiyun if ((version != 1) && (version != 2))
289*4882a593Smuzhiyun dev_warn(dev, "UNTESTED version detected (%d)\n", version);
290*4882a593Smuzhiyun
291*4882a593Smuzhiyun /* Disable the chip */
292*4882a593Smuzhiyun ret = tpa6130a2_power(data, false);
293*4882a593Smuzhiyun if (ret != 0)
294*4882a593Smuzhiyun return ret;
295*4882a593Smuzhiyun
296*4882a593Smuzhiyun return devm_snd_soc_register_component(&client->dev,
297*4882a593Smuzhiyun &tpa6130a2_component_driver, NULL, 0);
298*4882a593Smuzhiyun }
299*4882a593Smuzhiyun
300*4882a593Smuzhiyun static const struct i2c_device_id tpa6130a2_id[] = {
301*4882a593Smuzhiyun { "tpa6130a2", TPA6130A2 },
302*4882a593Smuzhiyun { "tpa6140a2", TPA6140A2 },
303*4882a593Smuzhiyun { }
304*4882a593Smuzhiyun };
305*4882a593Smuzhiyun MODULE_DEVICE_TABLE(i2c, tpa6130a2_id);
306*4882a593Smuzhiyun
307*4882a593Smuzhiyun #if IS_ENABLED(CONFIG_OF)
308*4882a593Smuzhiyun static const struct of_device_id tpa6130a2_of_match[] = {
309*4882a593Smuzhiyun { .compatible = "ti,tpa6130a2", },
310*4882a593Smuzhiyun { .compatible = "ti,tpa6140a2" },
311*4882a593Smuzhiyun {},
312*4882a593Smuzhiyun };
313*4882a593Smuzhiyun MODULE_DEVICE_TABLE(of, tpa6130a2_of_match);
314*4882a593Smuzhiyun #endif
315*4882a593Smuzhiyun
316*4882a593Smuzhiyun static struct i2c_driver tpa6130a2_i2c_driver = {
317*4882a593Smuzhiyun .driver = {
318*4882a593Smuzhiyun .name = "tpa6130a2",
319*4882a593Smuzhiyun .of_match_table = of_match_ptr(tpa6130a2_of_match),
320*4882a593Smuzhiyun },
321*4882a593Smuzhiyun .probe = tpa6130a2_probe,
322*4882a593Smuzhiyun .id_table = tpa6130a2_id,
323*4882a593Smuzhiyun };
324*4882a593Smuzhiyun
325*4882a593Smuzhiyun module_i2c_driver(tpa6130a2_i2c_driver);
326*4882a593Smuzhiyun
327*4882a593Smuzhiyun MODULE_AUTHOR("Peter Ujfalusi <peter.ujfalusi@ti.com>");
328*4882a593Smuzhiyun MODULE_DESCRIPTION("TPA6130A2 Headphone amplifier driver");
329*4882a593Smuzhiyun MODULE_LICENSE("GPL");
330