blob: d90388a389039669fb8a59fd2eb2bd61347fa51c [file] [log] [blame]
Peter Ujfalusi493b67e2009-10-09 15:55:41 +03001/*
2 * ALSA SoC Texas Instruments TPA6130A2 headset stereo amplifier driver
3 *
4 * Copyright (C) Nokia Corporation
5 *
Peter Ujfalusib4079ef2011-05-03 18:12:41 +03006 * Author: Peter Ujfalusi <peter.ujfalusi@ti.com>
Peter Ujfalusi493b67e2009-10-09 15:55:41 +03007 *
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>
Ilkka Koskinen7c4e6492009-12-09 12:05:50 +020028#include <linux/regulator/consumer.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090029#include <linux/slab.h>
Peter Ujfalusi493b67e2009-10-09 15:55:41 +030030#include <sound/tpa6130a2-plat.h>
31#include <sound/soc.h>
Peter Ujfalusi493b67e2009-10-09 15:55:41 +030032#include <sound/tlv.h>
Sachin Kamatee5e4532014-04-04 11:29:13 +053033#include <linux/of.h>
Sebastian Reichelf95a4882013-10-23 14:03:28 +020034#include <linux/of_gpio.h>
Helen Koikea0d5ff42016-06-20 14:12:30 -030035#include <linux/regmap.h>
Peter Ujfalusi493b67e2009-10-09 15:55:41 +030036
37#include "tpa6130a2.h"
38
Peter Ujfalusi0722d052011-08-30 14:39:54 +030039enum tpa_model {
40 TPA6130A2,
41 TPA6140A2,
42};
43
Mark Brownebab1b12009-10-09 19:13:47 +010044static struct i2c_client *tpa6130a2_client;
Peter Ujfalusi493b67e2009-10-09 15:55:41 +030045
46/* This struct is used to save the context */
47struct tpa6130a2_data {
48 struct mutex mutex;
Helen Koikea0d5ff42016-06-20 14:12:30 -030049 struct regmap *regmap;
Jarkko Nikulaad8332c2010-05-19 10:52:28 +030050 struct regulator *supply;
Peter Ujfalusi493b67e2009-10-09 15:55:41 +030051 int power_gpio;
Peter Ujfalusid5876ce2010-11-30 16:00:00 +020052 u8 power_state:1;
Peter Ujfalusie5e5b312010-05-04 11:08:18 +030053 enum tpa_model id;
Peter Ujfalusi493b67e2009-10-09 15:55:41 +030054};
55
Peter Ujfalusid5876ce2010-11-30 16:00:00 +020056static int tpa6130a2_power(u8 power)
Peter Ujfalusi493b67e2009-10-09 15:55:41 +030057{
58 struct tpa6130a2_data *data;
Jarkko Nikula75e3f312010-11-03 16:39:00 +020059 int ret = 0;
Peter Ujfalusi493b67e2009-10-09 15:55:41 +030060
Takashi Iwai773392b2013-11-05 18:39:51 +010061 if (WARN_ON(!tpa6130a2_client))
62 return -EINVAL;
Peter Ujfalusi493b67e2009-10-09 15:55:41 +030063 data = i2c_get_clientdata(tpa6130a2_client);
64
65 mutex_lock(&data->mutex);
Peter Ujfalusid5876ce2010-11-30 16:00:00 +020066 if (power == data->power_state)
67 goto exit;
Ilkka Koskinen7c4e6492009-12-09 12:05:50 +020068
Peter Ujfalusid5876ce2010-11-30 16:00:00 +020069 if (power) {
Jarkko Nikulaad8332c2010-05-19 10:52:28 +030070 ret = regulator_enable(data->supply);
Ilkka Koskinen7c4e6492009-12-09 12:05:50 +020071 if (ret != 0) {
72 dev_err(&tpa6130a2_client->dev,
Jarkko Nikulaad8332c2010-05-19 10:52:28 +030073 "Failed to enable supply: %d\n", ret);
Ilkka Koskinen7c4e6492009-12-09 12:05:50 +020074 goto exit;
Peter Ujfalusi493b67e2009-10-09 15:55:41 +030075 }
Jarkko Nikula0656f6c2010-11-08 10:57:57 +020076 /* Power on */
77 if (data->power_gpio >= 0)
78 gpio_set_value(data->power_gpio, 1);
Ilkka Koskinen7c4e6492009-12-09 12:05:50 +020079
80 data->power_state = 1;
Helen Koikea0d5ff42016-06-20 14:12:30 -030081 ret = regcache_sync(data->regmap);
Peter Ujfalusi872a64d2010-10-21 15:03:03 +030082 if (ret < 0) {
83 dev_err(&tpa6130a2_client->dev,
84 "Failed to initialize chip\n");
85 if (data->power_gpio >= 0)
86 gpio_set_value(data->power_gpio, 0);
87 regulator_disable(data->supply);
88 data->power_state = 0;
89 goto exit;
90 }
Peter Ujfalusid5876ce2010-11-30 16:00:00 +020091 } else {
Peter Ujfalusi493b67e2009-10-09 15:55:41 +030092 /* set SWS */
Helen Koikea0d5ff42016-06-20 14:12:30 -030093 regmap_update_bits(data->regmap, TPA6130A2_REG_CONTROL,
94 TPA6130A2_SWS, TPA6130A2_SWS);
Ilkka Koskinen7c4e6492009-12-09 12:05:50 +020095
Peter Ujfalusi493b67e2009-10-09 15:55:41 +030096 /* Power off */
Ilkka Koskinen7c4e6492009-12-09 12:05:50 +020097 if (data->power_gpio >= 0)
Peter Ujfalusi493b67e2009-10-09 15:55:41 +030098 gpio_set_value(data->power_gpio, 0);
Ilkka Koskinen7c4e6492009-12-09 12:05:50 +020099
Jarkko Nikulaad8332c2010-05-19 10:52:28 +0300100 ret = regulator_disable(data->supply);
Ilkka Koskinen7c4e6492009-12-09 12:05:50 +0200101 if (ret != 0) {
102 dev_err(&tpa6130a2_client->dev,
Jarkko Nikulaad8332c2010-05-19 10:52:28 +0300103 "Failed to disable supply: %d\n", ret);
Ilkka Koskinen7c4e6492009-12-09 12:05:50 +0200104 goto exit;
Peter Ujfalusi493b67e2009-10-09 15:55:41 +0300105 }
Ilkka Koskinen7c4e6492009-12-09 12:05:50 +0200106
107 data->power_state = 0;
Helen Koikea0d5ff42016-06-20 14:12:30 -0300108 /* device regs does not match the cache state anymore */
109 regcache_mark_dirty(data->regmap);
Peter Ujfalusi493b67e2009-10-09 15:55:41 +0300110 }
Ilkka Koskinen7c4e6492009-12-09 12:05:50 +0200111
112exit:
Peter Ujfalusi493b67e2009-10-09 15:55:41 +0300113 mutex_unlock(&data->mutex);
Ilkka Koskinen7c4e6492009-12-09 12:05:50 +0200114 return ret;
Peter Ujfalusi493b67e2009-10-09 15:55:41 +0300115}
116
Peter Ujfalusibd843ed2010-05-07 14:24:11 +0300117static int tpa6130a2_get_volsw(struct snd_kcontrol *kcontrol,
Peter Ujfalusi493b67e2009-10-09 15:55:41 +0300118 struct snd_ctl_elem_value *ucontrol)
119{
120 struct soc_mixer_control *mc =
121 (struct soc_mixer_control *)kcontrol->private_value;
122 struct tpa6130a2_data *data;
123 unsigned int reg = mc->reg;
124 unsigned int shift = mc->shift;
Helen Koikea0d5ff42016-06-20 14:12:30 -0300125 int max = mc->max, val;
Peter Ujfalusibd843ed2010-05-07 14:24:11 +0300126 unsigned int mask = (1 << fls(max)) - 1;
Peter Ujfalusi493b67e2009-10-09 15:55:41 +0300127 unsigned int invert = mc->invert;
128
Takashi Iwai773392b2013-11-05 18:39:51 +0100129 if (WARN_ON(!tpa6130a2_client))
130 return -EINVAL;
Peter Ujfalusi493b67e2009-10-09 15:55:41 +0300131 data = i2c_get_clientdata(tpa6130a2_client);
132
133 mutex_lock(&data->mutex);
134
Helen Koikea0d5ff42016-06-20 14:12:30 -0300135 regmap_read(data->regmap, reg, &val);
136 ucontrol->value.integer.value[0] = (val >> shift) & mask;
Peter Ujfalusi493b67e2009-10-09 15:55:41 +0300137
138 if (invert)
139 ucontrol->value.integer.value[0] =
Peter Ujfalusibd843ed2010-05-07 14:24:11 +0300140 max - ucontrol->value.integer.value[0];
Peter Ujfalusi493b67e2009-10-09 15:55:41 +0300141
142 mutex_unlock(&data->mutex);
143 return 0;
144}
145
Peter Ujfalusibd843ed2010-05-07 14:24:11 +0300146static int tpa6130a2_put_volsw(struct snd_kcontrol *kcontrol,
Peter Ujfalusi493b67e2009-10-09 15:55:41 +0300147 struct snd_ctl_elem_value *ucontrol)
148{
149 struct soc_mixer_control *mc =
150 (struct soc_mixer_control *)kcontrol->private_value;
151 struct tpa6130a2_data *data;
152 unsigned int reg = mc->reg;
153 unsigned int shift = mc->shift;
Peter Ujfalusibd843ed2010-05-07 14:24:11 +0300154 int max = mc->max;
155 unsigned int mask = (1 << fls(max)) - 1;
Peter Ujfalusi493b67e2009-10-09 15:55:41 +0300156 unsigned int invert = mc->invert;
157 unsigned int val = (ucontrol->value.integer.value[0] & mask);
Helen Koikea0d5ff42016-06-20 14:12:30 -0300158 bool change;
Peter Ujfalusi493b67e2009-10-09 15:55:41 +0300159
Takashi Iwai773392b2013-11-05 18:39:51 +0100160 if (WARN_ON(!tpa6130a2_client))
161 return -EINVAL;
Peter Ujfalusi493b67e2009-10-09 15:55:41 +0300162 data = i2c_get_clientdata(tpa6130a2_client);
163
164 if (invert)
Peter Ujfalusibd843ed2010-05-07 14:24:11 +0300165 val = max - val;
Peter Ujfalusi493b67e2009-10-09 15:55:41 +0300166
167 mutex_lock(&data->mutex);
Helen Koikea0d5ff42016-06-20 14:12:30 -0300168 regmap_update_bits_check(data->regmap, reg, mask << shift, val << shift,
169 &change);
Peter Ujfalusi493b67e2009-10-09 15:55:41 +0300170 mutex_unlock(&data->mutex);
171
Helen Koikea0d5ff42016-06-20 14:12:30 -0300172 return change;
Peter Ujfalusi493b67e2009-10-09 15:55:41 +0300173}
174
175/*
176 * TPA6130 volume. From -59.5 to 4 dB with increasing step size when going
177 * down in gain.
178 */
Lars-Peter Clausen4e0e5f82015-08-02 17:19:55 +0200179static const DECLARE_TLV_DB_RANGE(tpa6130_tlv,
Peter Ujfalusi493b67e2009-10-09 15:55:41 +0300180 0, 1, TLV_DB_SCALE_ITEM(-5950, 600, 0),
181 2, 3, TLV_DB_SCALE_ITEM(-5000, 250, 0),
182 4, 5, TLV_DB_SCALE_ITEM(-4550, 160, 0),
183 6, 7, TLV_DB_SCALE_ITEM(-4140, 190, 0),
184 8, 9, TLV_DB_SCALE_ITEM(-3650, 120, 0),
185 10, 11, TLV_DB_SCALE_ITEM(-3330, 160, 0),
186 12, 13, TLV_DB_SCALE_ITEM(-3040, 180, 0),
187 14, 20, TLV_DB_SCALE_ITEM(-2710, 110, 0),
188 21, 37, TLV_DB_SCALE_ITEM(-1960, 74, 0),
Lars-Peter Clausen4e0e5f82015-08-02 17:19:55 +0200189 38, 63, TLV_DB_SCALE_ITEM(-720, 45, 0)
190);
Peter Ujfalusi493b67e2009-10-09 15:55:41 +0300191
192static const struct snd_kcontrol_new tpa6130a2_controls[] = {
Helen Koikecb7e6222016-06-20 14:12:29 -0300193 SOC_SINGLE_EXT_TLV("Headphone Playback Volume",
Peter Ujfalusi493b67e2009-10-09 15:55:41 +0300194 TPA6130A2_REG_VOL_MUTE, 0, 0x3f, 0,
Peter Ujfalusibd843ed2010-05-07 14:24:11 +0300195 tpa6130a2_get_volsw, tpa6130a2_put_volsw,
Peter Ujfalusi493b67e2009-10-09 15:55:41 +0300196 tpa6130_tlv),
197};
198
Lars-Peter Clausen4e0e5f82015-08-02 17:19:55 +0200199static const DECLARE_TLV_DB_RANGE(tpa6140_tlv,
Peter Ujfalusie5e5b312010-05-04 11:08:18 +0300200 0, 8, TLV_DB_SCALE_ITEM(-5900, 400, 0),
201 9, 16, TLV_DB_SCALE_ITEM(-2500, 200, 0),
Lars-Peter Clausen4e0e5f82015-08-02 17:19:55 +0200202 17, 31, TLV_DB_SCALE_ITEM(-1000, 100, 0)
203);
Peter Ujfalusie5e5b312010-05-04 11:08:18 +0300204
205static const struct snd_kcontrol_new tpa6140a2_controls[] = {
Helen Koikecb7e6222016-06-20 14:12:29 -0300206 SOC_SINGLE_EXT_TLV("Headphone Playback Volume",
Peter Ujfalusi826e9622010-05-07 14:24:10 +0300207 TPA6130A2_REG_VOL_MUTE, 1, 0x1f, 0,
Peter Ujfalusibd843ed2010-05-07 14:24:11 +0300208 tpa6130a2_get_volsw, tpa6130a2_put_volsw,
Peter Ujfalusi826e9622010-05-07 14:24:10 +0300209 tpa6140_tlv),
Peter Ujfalusie5e5b312010-05-04 11:08:18 +0300210};
211
Peter Ujfalusi493b67e2009-10-09 15:55:41 +0300212/*
213 * Enable or disable channel (left or right)
214 * The bit number for mute and amplifier are the same per channel:
215 * bit 6: Right channel
216 * bit 7: Left channel
217 * in both registers.
218 */
219static void tpa6130a2_channel_enable(u8 channel, int enable)
220{
Helen Koikea0d5ff42016-06-20 14:12:30 -0300221 struct tpa6130a2_data *data = i2c_get_clientdata(tpa6130a2_client);
Peter Ujfalusi493b67e2009-10-09 15:55:41 +0300222
Peter Ujfalusi493b67e2009-10-09 15:55:41 +0300223 if (enable) {
224 /* Enable channel */
225 /* Enable amplifier */
Helen Koikea0d5ff42016-06-20 14:12:30 -0300226 regmap_update_bits(data->regmap, TPA6130A2_REG_CONTROL,
227 channel | TPA6130A2_SWS, channel & ~TPA6130A2_SWS);
Peter Ujfalusi493b67e2009-10-09 15:55:41 +0300228
229 /* Unmute channel */
Helen Koikea0d5ff42016-06-20 14:12:30 -0300230 regmap_update_bits(data->regmap, TPA6130A2_REG_VOL_MUTE,
231 channel, 0);
Peter Ujfalusi493b67e2009-10-09 15:55:41 +0300232 } else {
233 /* Disable channel */
234 /* Mute channel */
Helen Koikea0d5ff42016-06-20 14:12:30 -0300235 regmap_update_bits(data->regmap, TPA6130A2_REG_VOL_MUTE,
236 channel, channel);
Peter Ujfalusi493b67e2009-10-09 15:55:41 +0300237
238 /* Disable amplifier */
Helen Koikea0d5ff42016-06-20 14:12:30 -0300239 regmap_update_bits(data->regmap, TPA6130A2_REG_CONTROL,
240 channel, 0);
Peter Ujfalusi493b67e2009-10-09 15:55:41 +0300241 }
242}
243
Peter Ujfalusi39646872010-12-02 09:29:56 +0200244int tpa6130a2_stereo_enable(struct snd_soc_codec *codec, int enable)
Peter Ujfalusi493b67e2009-10-09 15:55:41 +0300245{
Ilkka Koskinen7c4e6492009-12-09 12:05:50 +0200246 int ret = 0;
Peter Ujfalusi39646872010-12-02 09:29:56 +0200247 if (enable) {
Ilkka Koskinen7c4e6492009-12-09 12:05:50 +0200248 ret = tpa6130a2_power(1);
Peter Ujfalusi39646872010-12-02 09:29:56 +0200249 if (ret < 0)
250 return ret;
251 tpa6130a2_channel_enable(TPA6130A2_HP_EN_R | TPA6130A2_HP_EN_L,
252 1);
253 } else {
254 tpa6130a2_channel_enable(TPA6130A2_HP_EN_R | TPA6130A2_HP_EN_L,
255 0);
Ilkka Koskinen7c4e6492009-12-09 12:05:50 +0200256 ret = tpa6130a2_power(0);
Peter Ujfalusi493b67e2009-10-09 15:55:41 +0300257 }
Peter Ujfalusi39646872010-12-02 09:29:56 +0200258
Ilkka Koskinen7c4e6492009-12-09 12:05:50 +0200259 return ret;
Peter Ujfalusi493b67e2009-10-09 15:55:41 +0300260}
Peter Ujfalusi39646872010-12-02 09:29:56 +0200261EXPORT_SYMBOL_GPL(tpa6130a2_stereo_enable);
Peter Ujfalusi493b67e2009-10-09 15:55:41 +0300262
Helen Koikecb7e6222016-06-20 14:12:29 -0300263static int tpa6130a2_component_probe(struct snd_soc_component *component)
Peter Ujfalusi493b67e2009-10-09 15:55:41 +0300264{
Helen Koikecb7e6222016-06-20 14:12:29 -0300265 struct tpa6130a2_data *data = snd_soc_component_get_drvdata(component);
Peter Ujfalusie5e5b312010-05-04 11:08:18 +0300266
Peter Ujfalusie5e5b312010-05-04 11:08:18 +0300267 if (data->id == TPA6140A2)
Helen Koikecb7e6222016-06-20 14:12:29 -0300268 return snd_soc_add_component_controls(component,
269 tpa6140a2_controls, ARRAY_SIZE(tpa6140a2_controls));
Peter Ujfalusie5e5b312010-05-04 11:08:18 +0300270 else
Helen Koikecb7e6222016-06-20 14:12:29 -0300271 return snd_soc_add_component_controls(component,
272 tpa6130a2_controls, ARRAY_SIZE(tpa6130a2_controls));
Peter Ujfalusi493b67e2009-10-09 15:55:41 +0300273}
Helen Koikecb7e6222016-06-20 14:12:29 -0300274
275struct snd_soc_component_driver tpa6130a2_component_driver = {
276 .name = "tpa6130a2",
277 .probe = tpa6130a2_component_probe,
278};
Peter Ujfalusi493b67e2009-10-09 15:55:41 +0300279
Helen Koikea0d5ff42016-06-20 14:12:30 -0300280static const struct reg_default tpa6130a2_reg_defaults[] = {
281 { TPA6130A2_REG_CONTROL, TPA6130A2_SWS },
282 { TPA6130A2_REG_VOL_MUTE, TPA6130A2_MUTE_R | TPA6130A2_MUTE_L },
283};
284
285static const struct regmap_config tpa6130a2_regmap_config = {
286 .reg_bits = 8,
287 .val_bits = 8,
288 .max_register = TPA6130A2_REG_VERSION,
289 .reg_defaults = tpa6130a2_reg_defaults,
290 .num_reg_defaults = ARRAY_SIZE(tpa6130a2_reg_defaults),
291 .cache_type = REGCACHE_RBTREE,
292};
293
Bill Pemberton7a79e942012-12-07 09:26:37 -0500294static int tpa6130a2_probe(struct i2c_client *client,
295 const struct i2c_device_id *id)
Peter Ujfalusi493b67e2009-10-09 15:55:41 +0300296{
297 struct device *dev;
298 struct tpa6130a2_data *data;
Sebastian Reichelf95a4882013-10-23 14:03:28 +0200299 struct tpa6130a2_platform_data *pdata = client->dev.platform_data;
300 struct device_node *np = client->dev.of_node;
Jarkko Nikulaad8332c2010-05-19 10:52:28 +0300301 const char *regulator;
Helen Koikea0d5ff42016-06-20 14:12:30 -0300302 unsigned int version;
Jarkko Nikulaad8332c2010-05-19 10:52:28 +0300303 int ret;
Peter Ujfalusi493b67e2009-10-09 15:55:41 +0300304
305 dev = &client->dev;
306
Axel Lin6945e9f2011-12-29 12:12:29 +0800307 data = devm_kzalloc(&client->dev, sizeof(*data), GFP_KERNEL);
Sachin Kamat656e3432014-06-20 15:29:02 +0530308 if (!data)
Peter Ujfalusi493b67e2009-10-09 15:55:41 +0300309 return -ENOMEM;
Peter Ujfalusi493b67e2009-10-09 15:55:41 +0300310
Helen Koikea0d5ff42016-06-20 14:12:30 -0300311 data->regmap = devm_regmap_init_i2c(client, &tpa6130a2_regmap_config);
312 if (IS_ERR(data->regmap))
313 return PTR_ERR(data->regmap);
314
Sebastian Reichelf95a4882013-10-23 14:03:28 +0200315 if (pdata) {
316 data->power_gpio = pdata->power_gpio;
317 } else if (np) {
318 data->power_gpio = of_get_named_gpio(np, "power-gpio", 0);
319 } else {
320 dev_err(dev, "Platform data not set\n");
321 dump_stack();
322 return -ENODEV;
323 }
324
Peter Ujfalusi493b67e2009-10-09 15:55:41 +0300325 tpa6130a2_client = client;
326
327 i2c_set_clientdata(tpa6130a2_client, data);
328
Peter Ujfalusi07441002011-08-30 14:39:52 +0300329 data->id = id->driver_data;
Peter Ujfalusi493b67e2009-10-09 15:55:41 +0300330
331 mutex_init(&data->mutex);
332
Peter Ujfalusi493b67e2009-10-09 15:55:41 +0300333 if (data->power_gpio >= 0) {
Sachin Kamatd06080c2012-12-07 16:32:26 +0530334 ret = devm_gpio_request(dev, data->power_gpio,
335 "tpa6130a2 enable");
Peter Ujfalusi493b67e2009-10-09 15:55:41 +0300336 if (ret < 0) {
337 dev_err(dev, "Failed to request power GPIO (%d)\n",
338 data->power_gpio);
Ilkka Koskinen7c4e6492009-12-09 12:05:50 +0200339 goto err_gpio;
Peter Ujfalusi493b67e2009-10-09 15:55:41 +0300340 }
341 gpio_direction_output(data->power_gpio, 0);
Peter Ujfalusi493b67e2009-10-09 15:55:41 +0300342 }
343
Peter Ujfalusie5e5b312010-05-04 11:08:18 +0300344 switch (data->id) {
Ilkka Koskinen21383012010-01-08 17:48:31 +0200345 default:
346 dev_warn(dev, "Unknown TPA model (%d). Assuming 6130A2\n",
Peter Ujfalusi07441002011-08-30 14:39:52 +0300347 data->id);
Jarkko Nikulaad8332c2010-05-19 10:52:28 +0300348 case TPA6130A2:
349 regulator = "Vdd";
350 break;
351 case TPA6140A2:
352 regulator = "AVdd";
353 break;
Ilkka Koskinen21383012010-01-08 17:48:31 +0200354 }
Ilkka Koskinen7c4e6492009-12-09 12:05:50 +0200355
Sachin Kamatd06080c2012-12-07 16:32:26 +0530356 data->supply = devm_regulator_get(dev, regulator);
Jarkko Nikulaad8332c2010-05-19 10:52:28 +0300357 if (IS_ERR(data->supply)) {
358 ret = PTR_ERR(data->supply);
359 dev_err(dev, "Failed to request supply: %d\n", ret);
Sachin Kamatd06080c2012-12-07 16:32:26 +0530360 goto err_gpio;
Ilkka Koskinen7c4e6492009-12-09 12:05:50 +0200361 }
362
363 ret = tpa6130a2_power(1);
364 if (ret != 0)
Sachin Kamatd06080c2012-12-07 16:32:26 +0530365 goto err_gpio;
Ilkka Koskinen7c4e6492009-12-09 12:05:50 +0200366
Peter Ujfalusi493b67e2009-10-09 15:55:41 +0300367
368 /* Read version */
Helen Koikea0d5ff42016-06-20 14:12:30 -0300369 regmap_read(data->regmap, TPA6130A2_REG_VERSION, &version);
370 version &= TPA6130A2_VERSION_MASK;
371 if ((version != 1) && (version != 2))
372 dev_warn(dev, "UNTESTED version detected (%d)\n", version);
Peter Ujfalusi493b67e2009-10-09 15:55:41 +0300373
374 /* Disable the chip */
Ilkka Koskinen7c4e6492009-12-09 12:05:50 +0200375 ret = tpa6130a2_power(0);
376 if (ret != 0)
Sachin Kamatd06080c2012-12-07 16:32:26 +0530377 goto err_gpio;
Peter Ujfalusi493b67e2009-10-09 15:55:41 +0300378
Helen Koikecb7e6222016-06-20 14:12:29 -0300379 return devm_snd_soc_register_component(&client->dev,
380 &tpa6130a2_component_driver, NULL, 0);
Ilkka Koskinen7c4e6492009-12-09 12:05:50 +0200381
Ilkka Koskinen7c4e6492009-12-09 12:05:50 +0200382err_gpio:
Mark Brownebab1b12009-10-09 19:13:47 +0100383 tpa6130a2_client = NULL;
Peter Ujfalusi493b67e2009-10-09 15:55:41 +0300384
385 return ret;
386}
387
Bill Pemberton7a79e942012-12-07 09:26:37 -0500388static int tpa6130a2_remove(struct i2c_client *client)
Peter Ujfalusi493b67e2009-10-09 15:55:41 +0300389{
Peter Ujfalusi493b67e2009-10-09 15:55:41 +0300390 tpa6130a2_power(0);
Mark Brownebab1b12009-10-09 19:13:47 +0100391 tpa6130a2_client = NULL;
Peter Ujfalusi493b67e2009-10-09 15:55:41 +0300392
393 return 0;
394}
395
396static const struct i2c_device_id tpa6130a2_id[] = {
Peter Ujfalusi07441002011-08-30 14:39:52 +0300397 { "tpa6130a2", TPA6130A2 },
398 { "tpa6140a2", TPA6140A2 },
Peter Ujfalusi493b67e2009-10-09 15:55:41 +0300399 { }
400};
401MODULE_DEVICE_TABLE(i2c, tpa6130a2_id);
402
Sebastian Reichelf95a4882013-10-23 14:03:28 +0200403#if IS_ENABLED(CONFIG_OF)
404static const struct of_device_id tpa6130a2_of_match[] = {
405 { .compatible = "ti,tpa6130a2", },
406 { .compatible = "ti,tpa6140a2" },
407 {},
408};
409MODULE_DEVICE_TABLE(of, tpa6130a2_of_match);
410#endif
411
Peter Ujfalusi493b67e2009-10-09 15:55:41 +0300412static struct i2c_driver tpa6130a2_i2c_driver = {
413 .driver = {
414 .name = "tpa6130a2",
Sebastian Reichelf95a4882013-10-23 14:03:28 +0200415 .of_match_table = of_match_ptr(tpa6130a2_of_match),
Peter Ujfalusi493b67e2009-10-09 15:55:41 +0300416 },
417 .probe = tpa6130a2_probe,
Bill Pemberton7a79e942012-12-07 09:26:37 -0500418 .remove = tpa6130a2_remove,
Peter Ujfalusi493b67e2009-10-09 15:55:41 +0300419 .id_table = tpa6130a2_id,
420};
421
Sachin Kamatf062e2b2012-08-06 17:25:38 +0530422module_i2c_driver(tpa6130a2_i2c_driver);
Peter Ujfalusi493b67e2009-10-09 15:55:41 +0300423
Peter Ujfalusib4079ef2011-05-03 18:12:41 +0300424MODULE_AUTHOR("Peter Ujfalusi <peter.ujfalusi@ti.com>");
Peter Ujfalusi493b67e2009-10-09 15:55:41 +0300425MODULE_DESCRIPTION("TPA6130A2 Headphone amplifier driver");
426MODULE_LICENSE("GPL");