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