xref: /linux/sound/soc/codecs/max9768.c (revision 33e02dc69afbd8f1b85a51d74d72f139ba4ca623)
1b886d83cSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2394d2bbaSWolfram Sang /*
3394d2bbaSWolfram Sang  * MAX9768 AMP driver
4394d2bbaSWolfram Sang  *
5394d2bbaSWolfram Sang  * Copyright (C) 2011, 2012 by Wolfram Sang, Pengutronix e.K.
6394d2bbaSWolfram Sang  */
7394d2bbaSWolfram Sang 
8394d2bbaSWolfram Sang #include <linux/init.h>
9394d2bbaSWolfram Sang #include <linux/module.h>
10394d2bbaSWolfram Sang #include <linux/i2c.h>
11394d2bbaSWolfram Sang #include <linux/slab.h>
12637a7969SLinus Walleij #include <linux/gpio/consumer.h>
13394d2bbaSWolfram Sang #include <linux/regmap.h>
14394d2bbaSWolfram Sang 
15394d2bbaSWolfram Sang #include <sound/core.h>
16394d2bbaSWolfram Sang #include <sound/soc.h>
17394d2bbaSWolfram Sang #include <sound/tlv.h>
18394d2bbaSWolfram Sang #include <sound/max9768.h>
19394d2bbaSWolfram Sang 
20394d2bbaSWolfram Sang /* "Registers" */
21394d2bbaSWolfram Sang #define MAX9768_VOL 0
22394d2bbaSWolfram Sang #define MAX9768_CTRL 3
23394d2bbaSWolfram Sang 
24394d2bbaSWolfram Sang /* Commands */
25394d2bbaSWolfram Sang #define MAX9768_CTRL_PWM 0x15
26394d2bbaSWolfram Sang #define MAX9768_CTRL_FILTERLESS 0x16
27394d2bbaSWolfram Sang 
28394d2bbaSWolfram Sang struct max9768 {
29394d2bbaSWolfram Sang 	struct regmap *regmap;
30637a7969SLinus Walleij 	struct gpio_desc *mute;
31637a7969SLinus Walleij 	struct gpio_desc *shdn;
32394d2bbaSWolfram Sang 	u32 flags;
33394d2bbaSWolfram Sang };
34394d2bbaSWolfram Sang 
35c418a84aSAxel Lin static const struct reg_default max9768_default_regs[] = {
36394d2bbaSWolfram Sang 	{ 0, 0 },
37394d2bbaSWolfram Sang 	{ 3,  MAX9768_CTRL_FILTERLESS},
38394d2bbaSWolfram Sang };
39394d2bbaSWolfram Sang 
max9768_get_gpio(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)40394d2bbaSWolfram Sang static int max9768_get_gpio(struct snd_kcontrol *kcontrol,
41394d2bbaSWolfram Sang 	struct snd_ctl_elem_value *ucontrol)
42394d2bbaSWolfram Sang {
4304b5cbd8SLars-Peter Clausen 	struct snd_soc_component *c = snd_soc_kcontrol_component(kcontrol);
4404b5cbd8SLars-Peter Clausen 	struct max9768 *max9768 = snd_soc_component_get_drvdata(c);
45637a7969SLinus Walleij 	int val = gpiod_get_value_cansleep(max9768->mute);
46394d2bbaSWolfram Sang 
47394d2bbaSWolfram Sang 	ucontrol->value.integer.value[0] = !val;
48394d2bbaSWolfram Sang 
49394d2bbaSWolfram Sang 	return 0;
50394d2bbaSWolfram Sang }
51394d2bbaSWolfram Sang 
max9768_set_gpio(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)52394d2bbaSWolfram Sang static int max9768_set_gpio(struct snd_kcontrol *kcontrol,
53394d2bbaSWolfram Sang 	struct snd_ctl_elem_value *ucontrol)
54394d2bbaSWolfram Sang {
5504b5cbd8SLars-Peter Clausen 	struct snd_soc_component *c = snd_soc_kcontrol_component(kcontrol);
5604b5cbd8SLars-Peter Clausen 	struct max9768 *max9768 = snd_soc_component_get_drvdata(c);
57394d2bbaSWolfram Sang 
58637a7969SLinus Walleij 	gpiod_set_value_cansleep(max9768->mute, !ucontrol->value.integer.value[0]);
59394d2bbaSWolfram Sang 
60394d2bbaSWolfram Sang 	return 0;
61394d2bbaSWolfram Sang }
62394d2bbaSWolfram Sang 
6344744eecSLars-Peter Clausen static const DECLARE_TLV_DB_RANGE(volume_tlv,
64394d2bbaSWolfram Sang 	0, 0, TLV_DB_SCALE_ITEM(-16150, 0, 0),
65394d2bbaSWolfram Sang 	1, 1, TLV_DB_SCALE_ITEM(-9280, 0, 0),
66394d2bbaSWolfram Sang 	2, 2, TLV_DB_SCALE_ITEM(-9030, 0, 0),
67394d2bbaSWolfram Sang 	3, 3, TLV_DB_SCALE_ITEM(-8680, 0, 0),
68394d2bbaSWolfram Sang 	4, 4, TLV_DB_SCALE_ITEM(-8430, 0, 0),
69394d2bbaSWolfram Sang 	5, 5, TLV_DB_SCALE_ITEM(-8080, 0, 0),
70394d2bbaSWolfram Sang 	6, 6, TLV_DB_SCALE_ITEM(-7830, 0, 0),
71394d2bbaSWolfram Sang 	7, 7, TLV_DB_SCALE_ITEM(-7470, 0, 0),
72394d2bbaSWolfram Sang 	8, 8, TLV_DB_SCALE_ITEM(-7220, 0, 0),
73394d2bbaSWolfram Sang 	9, 9, TLV_DB_SCALE_ITEM(-6870, 0, 0),
74394d2bbaSWolfram Sang 	10, 10, TLV_DB_SCALE_ITEM(-6620, 0, 0),
75394d2bbaSWolfram Sang 	11, 11, TLV_DB_SCALE_ITEM(-6270, 0, 0),
76394d2bbaSWolfram Sang 	12, 12, TLV_DB_SCALE_ITEM(-6020, 0, 0),
77394d2bbaSWolfram Sang 	13, 13, TLV_DB_SCALE_ITEM(-5670, 0, 0),
78394d2bbaSWolfram Sang 	14, 14, TLV_DB_SCALE_ITEM(-5420, 0, 0),
79394d2bbaSWolfram Sang 	15, 17, TLV_DB_SCALE_ITEM(-5060, 250, 0),
80394d2bbaSWolfram Sang 	18, 18, TLV_DB_SCALE_ITEM(-4370, 0, 0),
81394d2bbaSWolfram Sang 	19, 19, TLV_DB_SCALE_ITEM(-4210, 0, 0),
82394d2bbaSWolfram Sang 	20, 20, TLV_DB_SCALE_ITEM(-3960, 0, 0),
83394d2bbaSWolfram Sang 	21, 21, TLV_DB_SCALE_ITEM(-3760, 0, 0),
84394d2bbaSWolfram Sang 	22, 22, TLV_DB_SCALE_ITEM(-3600, 0, 0),
85394d2bbaSWolfram Sang 	23, 23, TLV_DB_SCALE_ITEM(-3340, 0, 0),
86394d2bbaSWolfram Sang 	24, 24, TLV_DB_SCALE_ITEM(-3150, 0, 0),
87394d2bbaSWolfram Sang 	25, 25, TLV_DB_SCALE_ITEM(-2980, 0, 0),
88394d2bbaSWolfram Sang 	26, 26, TLV_DB_SCALE_ITEM(-2720, 0, 0),
89394d2bbaSWolfram Sang 	27, 27, TLV_DB_SCALE_ITEM(-2520, 0, 0),
90394d2bbaSWolfram Sang 	28, 30, TLV_DB_SCALE_ITEM(-2350, 190, 0),
91394d2bbaSWolfram Sang 	31, 31, TLV_DB_SCALE_ITEM(-1750, 0, 0),
92394d2bbaSWolfram Sang 	32, 34, TLV_DB_SCALE_ITEM(-1640, 100, 0),
93394d2bbaSWolfram Sang 	35, 37, TLV_DB_SCALE_ITEM(-1310, 110, 0),
94394d2bbaSWolfram Sang 	38, 39, TLV_DB_SCALE_ITEM(-990, 100, 0),
95394d2bbaSWolfram Sang 	40, 40, TLV_DB_SCALE_ITEM(-710, 0, 0),
96394d2bbaSWolfram Sang 	41, 41, TLV_DB_SCALE_ITEM(-600, 0, 0),
97394d2bbaSWolfram Sang 	42, 42, TLV_DB_SCALE_ITEM(-500, 0, 0),
98394d2bbaSWolfram Sang 	43, 43, TLV_DB_SCALE_ITEM(-340, 0, 0),
99394d2bbaSWolfram Sang 	44, 44, TLV_DB_SCALE_ITEM(-190, 0, 0),
100394d2bbaSWolfram Sang 	45, 45, TLV_DB_SCALE_ITEM(-50, 0, 0),
101394d2bbaSWolfram Sang 	46, 46, TLV_DB_SCALE_ITEM(50, 0, 0),
102394d2bbaSWolfram Sang 	47, 50, TLV_DB_SCALE_ITEM(120, 40, 0),
103394d2bbaSWolfram Sang 	51, 57, TLV_DB_SCALE_ITEM(290, 50, 0),
104394d2bbaSWolfram Sang 	58, 58, TLV_DB_SCALE_ITEM(650, 0, 0),
105394d2bbaSWolfram Sang 	59, 62, TLV_DB_SCALE_ITEM(700, 60, 0),
10644744eecSLars-Peter Clausen 	63, 63, TLV_DB_SCALE_ITEM(950, 0, 0)
10744744eecSLars-Peter Clausen );
108394d2bbaSWolfram Sang 
109394d2bbaSWolfram Sang static const struct snd_kcontrol_new max9768_volume[] = {
110394d2bbaSWolfram Sang 	SOC_SINGLE_TLV("Playback Volume", MAX9768_VOL, 0, 63, 0, volume_tlv),
111394d2bbaSWolfram Sang };
112394d2bbaSWolfram Sang 
113394d2bbaSWolfram Sang static const struct snd_kcontrol_new max9768_mute[] = {
11469edea8aSMark Brown 	SOC_SINGLE_BOOL_EXT("Playback Switch", 0, max9768_get_gpio, max9768_set_gpio),
115394d2bbaSWolfram Sang };
116394d2bbaSWolfram Sang 
117439fe8a7SMark Brown static const struct snd_soc_dapm_widget max9768_dapm_widgets[] = {
118439fe8a7SMark Brown SND_SOC_DAPM_INPUT("IN"),
119439fe8a7SMark Brown 
120439fe8a7SMark Brown SND_SOC_DAPM_OUTPUT("OUT+"),
121439fe8a7SMark Brown SND_SOC_DAPM_OUTPUT("OUT-"),
122439fe8a7SMark Brown };
123439fe8a7SMark Brown 
124439fe8a7SMark Brown static const struct snd_soc_dapm_route max9768_dapm_routes[] = {
125439fe8a7SMark Brown 	{ "OUT+", NULL, "IN" },
126439fe8a7SMark Brown 	{ "OUT-", NULL, "IN" },
127439fe8a7SMark Brown };
128439fe8a7SMark Brown 
max9768_probe(struct snd_soc_component * component)12904b5cbd8SLars-Peter Clausen static int max9768_probe(struct snd_soc_component *component)
130394d2bbaSWolfram Sang {
13104b5cbd8SLars-Peter Clausen 	struct max9768 *max9768 = snd_soc_component_get_drvdata(component);
132394d2bbaSWolfram Sang 	int ret;
133394d2bbaSWolfram Sang 
134394d2bbaSWolfram Sang 	if (max9768->flags & MAX9768_FLAG_CLASSIC_PWM) {
13504b5cbd8SLars-Peter Clausen 		ret = regmap_write(max9768->regmap, MAX9768_CTRL,
13604b5cbd8SLars-Peter Clausen 			MAX9768_CTRL_PWM);
137394d2bbaSWolfram Sang 		if (ret)
138394d2bbaSWolfram Sang 			return ret;
139394d2bbaSWolfram Sang 	}
140394d2bbaSWolfram Sang 
141637a7969SLinus Walleij 	if (max9768->mute) {
14204b5cbd8SLars-Peter Clausen 		ret = snd_soc_add_component_controls(component, max9768_mute,
143394d2bbaSWolfram Sang 				ARRAY_SIZE(max9768_mute));
144394d2bbaSWolfram Sang 		if (ret)
145394d2bbaSWolfram Sang 			return ret;
146394d2bbaSWolfram Sang 	}
147394d2bbaSWolfram Sang 
148394d2bbaSWolfram Sang 	return 0;
149394d2bbaSWolfram Sang }
150394d2bbaSWolfram Sang 
1516e37f933SBhumika Goyal static const struct snd_soc_component_driver max9768_component_driver = {
152394d2bbaSWolfram Sang 	.probe = max9768_probe,
153394d2bbaSWolfram Sang 	.controls = max9768_volume,
154394d2bbaSWolfram Sang 	.num_controls = ARRAY_SIZE(max9768_volume),
155439fe8a7SMark Brown 	.dapm_widgets = max9768_dapm_widgets,
156439fe8a7SMark Brown 	.num_dapm_widgets = ARRAY_SIZE(max9768_dapm_widgets),
157439fe8a7SMark Brown 	.dapm_routes = max9768_dapm_routes,
158439fe8a7SMark Brown 	.num_dapm_routes = ARRAY_SIZE(max9768_dapm_routes),
159394d2bbaSWolfram Sang };
160394d2bbaSWolfram Sang 
161394d2bbaSWolfram Sang static const struct regmap_config max9768_i2c_regmap_config = {
162394d2bbaSWolfram Sang 	.reg_bits = 2,
163394d2bbaSWolfram Sang 	.val_bits = 6,
164394d2bbaSWolfram Sang 	.max_register = 3,
165394d2bbaSWolfram Sang 	.reg_defaults = max9768_default_regs,
166394d2bbaSWolfram Sang 	.num_reg_defaults = ARRAY_SIZE(max9768_default_regs),
167394d2bbaSWolfram Sang 	.cache_type = REGCACHE_RBTREE,
168394d2bbaSWolfram Sang };
169394d2bbaSWolfram Sang 
max9768_i2c_probe(struct i2c_client * client)170fead49e3SStephen Kitt static int max9768_i2c_probe(struct i2c_client *client)
171394d2bbaSWolfram Sang {
172394d2bbaSWolfram Sang 	struct max9768 *max9768;
173394d2bbaSWolfram Sang 	struct max9768_pdata *pdata = client->dev.platform_data;
174394d2bbaSWolfram Sang 
175394d2bbaSWolfram Sang 	max9768 = devm_kzalloc(&client->dev, sizeof(*max9768), GFP_KERNEL);
176394d2bbaSWolfram Sang 	if (!max9768)
177394d2bbaSWolfram Sang 		return -ENOMEM;
178394d2bbaSWolfram Sang 
179394d2bbaSWolfram Sang 	/* Mute on powerup to avoid clicks */
180637a7969SLinus Walleij 	max9768->mute = devm_gpiod_get_optional(&client->dev,
181637a7969SLinus Walleij 						"mute",
182637a7969SLinus Walleij 						GPIOD_OUT_HIGH);
183637a7969SLinus Walleij 	if (IS_ERR(max9768->mute))
184637a7969SLinus Walleij 		return PTR_ERR(max9768->mute);
185637a7969SLinus Walleij 	gpiod_set_consumer_name(max9768->mute, "MAX9768 Mute");
186394d2bbaSWolfram Sang 
187394d2bbaSWolfram Sang 	/* Activate chip by releasing shutdown, enables I2C */
188637a7969SLinus Walleij 	max9768->shdn = devm_gpiod_get_optional(&client->dev,
189637a7969SLinus Walleij 						"shutdown",
190637a7969SLinus Walleij 						GPIOD_OUT_HIGH);
191637a7969SLinus Walleij 	if (IS_ERR(max9768->shdn))
192637a7969SLinus Walleij 		return PTR_ERR(max9768->shdn);
193637a7969SLinus Walleij 	gpiod_set_consumer_name(max9768->shdn, "MAX9768 Shutdown");
194394d2bbaSWolfram Sang 
195637a7969SLinus Walleij 	if (pdata)
196394d2bbaSWolfram Sang 		max9768->flags = pdata->flags;
197394d2bbaSWolfram Sang 
198394d2bbaSWolfram Sang 	i2c_set_clientdata(client, max9768);
199394d2bbaSWolfram Sang 
2008f255e7bSSachin Kamat 	max9768->regmap = devm_regmap_init_i2c(client, &max9768_i2c_regmap_config);
201d79cca1aSLars-Peter Clausen 	if (IS_ERR(max9768->regmap))
202d79cca1aSLars-Peter Clausen 		return PTR_ERR(max9768->regmap);
203394d2bbaSWolfram Sang 
20404b5cbd8SLars-Peter Clausen 	return devm_snd_soc_register_component(&client->dev,
20504b5cbd8SLars-Peter Clausen 		&max9768_component_driver, NULL, 0);
206394d2bbaSWolfram Sang }
207394d2bbaSWolfram Sang 
208394d2bbaSWolfram Sang static const struct i2c_device_id max9768_i2c_id[] = {
209*ba2a2c37SUwe Kleine-König 	{ "max9768" },
210394d2bbaSWolfram Sang 	{ }
211394d2bbaSWolfram Sang };
212394d2bbaSWolfram Sang MODULE_DEVICE_TABLE(i2c, max9768_i2c_id);
213394d2bbaSWolfram Sang 
214394d2bbaSWolfram Sang static struct i2c_driver max9768_i2c_driver = {
215394d2bbaSWolfram Sang 	.driver = {
216394d2bbaSWolfram Sang 		.name = "max9768",
217394d2bbaSWolfram Sang 	},
2189abcd240SUwe Kleine-König 	.probe = max9768_i2c_probe,
219394d2bbaSWolfram Sang 	.id_table = max9768_i2c_id,
220394d2bbaSWolfram Sang };
221394d2bbaSWolfram Sang module_i2c_driver(max9768_i2c_driver);
222394d2bbaSWolfram Sang 
223454b3766SWolfram Sang MODULE_AUTHOR("Wolfram Sang <kernel@pengutronix.de>");
224394d2bbaSWolfram Sang MODULE_DESCRIPTION("ASoC MAX9768 amplifier driver");
225394d2bbaSWolfram Sang MODULE_LICENSE("GPL v2");
226