Loading...
1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * MAX9768 AMP driver
4 *
5 * Copyright (C) 2011, 2012 by Wolfram Sang, Pengutronix e.K.
6 */
7
8#include <linux/init.h>
9#include <linux/module.h>
10#include <linux/i2c.h>
11#include <linux/slab.h>
12#include <linux/gpio/consumer.h>
13#include <linux/regmap.h>
14
15#include <sound/core.h>
16#include <sound/soc.h>
17#include <sound/tlv.h>
18#include <sound/max9768.h>
19
20/* "Registers" */
21#define MAX9768_VOL 0
22#define MAX9768_CTRL 3
23
24/* Commands */
25#define MAX9768_CTRL_PWM 0x15
26#define MAX9768_CTRL_FILTERLESS 0x16
27
28struct max9768 {
29 struct regmap *regmap;
30 struct gpio_desc *mute;
31 struct gpio_desc *shdn;
32 u32 flags;
33};
34
35static const struct reg_default max9768_default_regs[] = {
36 { 0, 0 },
37 { 3, MAX9768_CTRL_FILTERLESS},
38};
39
40static int max9768_get_gpio(struct snd_kcontrol *kcontrol,
41 struct snd_ctl_elem_value *ucontrol)
42{
43 struct snd_soc_component *c = snd_soc_kcontrol_component(kcontrol);
44 struct max9768 *max9768 = snd_soc_component_get_drvdata(c);
45 int val = gpiod_get_value_cansleep(max9768->mute);
46
47 ucontrol->value.integer.value[0] = !val;
48
49 return 0;
50}
51
52static int max9768_set_gpio(struct snd_kcontrol *kcontrol,
53 struct snd_ctl_elem_value *ucontrol)
54{
55 struct snd_soc_component *c = snd_soc_kcontrol_component(kcontrol);
56 struct max9768 *max9768 = snd_soc_component_get_drvdata(c);
57 bool val = !ucontrol->value.integer.value[0];
58 int ret;
59
60 if (val != gpiod_get_value_cansleep(max9768->mute))
61 ret = 1;
62 else
63 ret = 0;
64
65 gpiod_set_value_cansleep(max9768->mute, val);
66
67 return ret;
68}
69
70static const DECLARE_TLV_DB_RANGE(volume_tlv,
71 0, 0, TLV_DB_SCALE_ITEM(-16150, 0, 0),
72 1, 1, TLV_DB_SCALE_ITEM(-9280, 0, 0),
73 2, 2, TLV_DB_SCALE_ITEM(-9030, 0, 0),
74 3, 3, TLV_DB_SCALE_ITEM(-8680, 0, 0),
75 4, 4, TLV_DB_SCALE_ITEM(-8430, 0, 0),
76 5, 5, TLV_DB_SCALE_ITEM(-8080, 0, 0),
77 6, 6, TLV_DB_SCALE_ITEM(-7830, 0, 0),
78 7, 7, TLV_DB_SCALE_ITEM(-7470, 0, 0),
79 8, 8, TLV_DB_SCALE_ITEM(-7220, 0, 0),
80 9, 9, TLV_DB_SCALE_ITEM(-6870, 0, 0),
81 10, 10, TLV_DB_SCALE_ITEM(-6620, 0, 0),
82 11, 11, TLV_DB_SCALE_ITEM(-6270, 0, 0),
83 12, 12, TLV_DB_SCALE_ITEM(-6020, 0, 0),
84 13, 13, TLV_DB_SCALE_ITEM(-5670, 0, 0),
85 14, 14, TLV_DB_SCALE_ITEM(-5420, 0, 0),
86 15, 17, TLV_DB_SCALE_ITEM(-5060, 250, 0),
87 18, 18, TLV_DB_SCALE_ITEM(-4370, 0, 0),
88 19, 19, TLV_DB_SCALE_ITEM(-4210, 0, 0),
89 20, 20, TLV_DB_SCALE_ITEM(-3960, 0, 0),
90 21, 21, TLV_DB_SCALE_ITEM(-3760, 0, 0),
91 22, 22, TLV_DB_SCALE_ITEM(-3600, 0, 0),
92 23, 23, TLV_DB_SCALE_ITEM(-3340, 0, 0),
93 24, 24, TLV_DB_SCALE_ITEM(-3150, 0, 0),
94 25, 25, TLV_DB_SCALE_ITEM(-2980, 0, 0),
95 26, 26, TLV_DB_SCALE_ITEM(-2720, 0, 0),
96 27, 27, TLV_DB_SCALE_ITEM(-2520, 0, 0),
97 28, 30, TLV_DB_SCALE_ITEM(-2350, 190, 0),
98 31, 31, TLV_DB_SCALE_ITEM(-1750, 0, 0),
99 32, 34, TLV_DB_SCALE_ITEM(-1640, 100, 0),
100 35, 37, TLV_DB_SCALE_ITEM(-1310, 110, 0),
101 38, 39, TLV_DB_SCALE_ITEM(-990, 100, 0),
102 40, 40, TLV_DB_SCALE_ITEM(-710, 0, 0),
103 41, 41, TLV_DB_SCALE_ITEM(-600, 0, 0),
104 42, 42, TLV_DB_SCALE_ITEM(-500, 0, 0),
105 43, 43, TLV_DB_SCALE_ITEM(-340, 0, 0),
106 44, 44, TLV_DB_SCALE_ITEM(-190, 0, 0),
107 45, 45, TLV_DB_SCALE_ITEM(-50, 0, 0),
108 46, 46, TLV_DB_SCALE_ITEM(50, 0, 0),
109 47, 50, TLV_DB_SCALE_ITEM(120, 40, 0),
110 51, 57, TLV_DB_SCALE_ITEM(290, 50, 0),
111 58, 58, TLV_DB_SCALE_ITEM(650, 0, 0),
112 59, 62, TLV_DB_SCALE_ITEM(700, 60, 0),
113 63, 63, TLV_DB_SCALE_ITEM(950, 0, 0)
114);
115
116static const struct snd_kcontrol_new max9768_volume[] = {
117 SOC_SINGLE_TLV("Playback Volume", MAX9768_VOL, 0, 63, 0, volume_tlv),
118};
119
120static const struct snd_kcontrol_new max9768_mute[] = {
121 SOC_SINGLE_BOOL_EXT("Playback Switch", 0, max9768_get_gpio, max9768_set_gpio),
122};
123
124static const struct snd_soc_dapm_widget max9768_dapm_widgets[] = {
125SND_SOC_DAPM_INPUT("IN"),
126
127SND_SOC_DAPM_OUTPUT("OUT+"),
128SND_SOC_DAPM_OUTPUT("OUT-"),
129};
130
131static const struct snd_soc_dapm_route max9768_dapm_routes[] = {
132 { "OUT+", NULL, "IN" },
133 { "OUT-", NULL, "IN" },
134};
135
136static int max9768_probe(struct snd_soc_component *component)
137{
138 struct max9768 *max9768 = snd_soc_component_get_drvdata(component);
139 int ret;
140
141 if (max9768->flags & MAX9768_FLAG_CLASSIC_PWM) {
142 ret = regmap_write(max9768->regmap, MAX9768_CTRL,
143 MAX9768_CTRL_PWM);
144 if (ret)
145 return ret;
146 }
147
148 if (max9768->mute) {
149 ret = snd_soc_add_component_controls(component, max9768_mute,
150 ARRAY_SIZE(max9768_mute));
151 if (ret)
152 return ret;
153 }
154
155 return 0;
156}
157
158static const struct snd_soc_component_driver max9768_component_driver = {
159 .probe = max9768_probe,
160 .controls = max9768_volume,
161 .num_controls = ARRAY_SIZE(max9768_volume),
162 .dapm_widgets = max9768_dapm_widgets,
163 .num_dapm_widgets = ARRAY_SIZE(max9768_dapm_widgets),
164 .dapm_routes = max9768_dapm_routes,
165 .num_dapm_routes = ARRAY_SIZE(max9768_dapm_routes),
166};
167
168static const struct regmap_config max9768_i2c_regmap_config = {
169 .reg_bits = 2,
170 .val_bits = 6,
171 .max_register = 3,
172 .reg_defaults = max9768_default_regs,
173 .num_reg_defaults = ARRAY_SIZE(max9768_default_regs),
174 .cache_type = REGCACHE_RBTREE,
175};
176
177static int max9768_i2c_probe(struct i2c_client *client)
178{
179 struct max9768 *max9768;
180 struct max9768_pdata *pdata = client->dev.platform_data;
181
182 max9768 = devm_kzalloc(&client->dev, sizeof(*max9768), GFP_KERNEL);
183 if (!max9768)
184 return -ENOMEM;
185
186 /* Mute on powerup to avoid clicks */
187 max9768->mute = devm_gpiod_get_optional(&client->dev,
188 "mute",
189 GPIOD_OUT_HIGH);
190 if (IS_ERR(max9768->mute))
191 return PTR_ERR(max9768->mute);
192 gpiod_set_consumer_name(max9768->mute, "MAX9768 Mute");
193
194 /* Activate chip by releasing shutdown, enables I2C */
195 max9768->shdn = devm_gpiod_get_optional(&client->dev,
196 "shutdown",
197 GPIOD_OUT_HIGH);
198 if (IS_ERR(max9768->shdn))
199 return PTR_ERR(max9768->shdn);
200 gpiod_set_consumer_name(max9768->shdn, "MAX9768 Shutdown");
201
202 if (pdata)
203 max9768->flags = pdata->flags;
204
205 i2c_set_clientdata(client, max9768);
206
207 max9768->regmap = devm_regmap_init_i2c(client, &max9768_i2c_regmap_config);
208 if (IS_ERR(max9768->regmap))
209 return PTR_ERR(max9768->regmap);
210
211 return devm_snd_soc_register_component(&client->dev,
212 &max9768_component_driver, NULL, 0);
213}
214
215static const struct i2c_device_id max9768_i2c_id[] = {
216 { "max9768" },
217 { }
218};
219MODULE_DEVICE_TABLE(i2c, max9768_i2c_id);
220
221static struct i2c_driver max9768_i2c_driver = {
222 .driver = {
223 .name = "max9768",
224 },
225 .probe = max9768_i2c_probe,
226 .id_table = max9768_i2c_id,
227};
228module_i2c_driver(max9768_i2c_driver);
229
230MODULE_AUTHOR("Wolfram Sang <kernel@pengutronix.de>");
231MODULE_DESCRIPTION("ASoC MAX9768 amplifier driver");
232MODULE_LICENSE("GPL v2");
1/*
2 * MAX9768 AMP driver
3 *
4 * Copyright (C) 2011, 2012 by Wolfram Sang, Pengutronix e.K.
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; version 2 of the License.
9 */
10
11#include <linux/init.h>
12#include <linux/module.h>
13#include <linux/i2c.h>
14#include <linux/slab.h>
15#include <linux/gpio.h>
16#include <linux/regmap.h>
17
18#include <sound/core.h>
19#include <sound/soc.h>
20#include <sound/tlv.h>
21#include <sound/max9768.h>
22
23/* "Registers" */
24#define MAX9768_VOL 0
25#define MAX9768_CTRL 3
26
27/* Commands */
28#define MAX9768_CTRL_PWM 0x15
29#define MAX9768_CTRL_FILTERLESS 0x16
30
31struct max9768 {
32 struct regmap *regmap;
33 int mute_gpio;
34 int shdn_gpio;
35 u32 flags;
36};
37
38static const struct reg_default max9768_default_regs[] = {
39 { 0, 0 },
40 { 3, MAX9768_CTRL_FILTERLESS},
41};
42
43static int max9768_get_gpio(struct snd_kcontrol *kcontrol,
44 struct snd_ctl_elem_value *ucontrol)
45{
46 struct snd_soc_component *c = snd_soc_kcontrol_component(kcontrol);
47 struct max9768 *max9768 = snd_soc_component_get_drvdata(c);
48 int val = gpio_get_value_cansleep(max9768->mute_gpio);
49
50 ucontrol->value.integer.value[0] = !val;
51
52 return 0;
53}
54
55static int max9768_set_gpio(struct snd_kcontrol *kcontrol,
56 struct snd_ctl_elem_value *ucontrol)
57{
58 struct snd_soc_component *c = snd_soc_kcontrol_component(kcontrol);
59 struct max9768 *max9768 = snd_soc_component_get_drvdata(c);
60
61 gpio_set_value_cansleep(max9768->mute_gpio, !ucontrol->value.integer.value[0]);
62
63 return 0;
64}
65
66static const DECLARE_TLV_DB_RANGE(volume_tlv,
67 0, 0, TLV_DB_SCALE_ITEM(-16150, 0, 0),
68 1, 1, TLV_DB_SCALE_ITEM(-9280, 0, 0),
69 2, 2, TLV_DB_SCALE_ITEM(-9030, 0, 0),
70 3, 3, TLV_DB_SCALE_ITEM(-8680, 0, 0),
71 4, 4, TLV_DB_SCALE_ITEM(-8430, 0, 0),
72 5, 5, TLV_DB_SCALE_ITEM(-8080, 0, 0),
73 6, 6, TLV_DB_SCALE_ITEM(-7830, 0, 0),
74 7, 7, TLV_DB_SCALE_ITEM(-7470, 0, 0),
75 8, 8, TLV_DB_SCALE_ITEM(-7220, 0, 0),
76 9, 9, TLV_DB_SCALE_ITEM(-6870, 0, 0),
77 10, 10, TLV_DB_SCALE_ITEM(-6620, 0, 0),
78 11, 11, TLV_DB_SCALE_ITEM(-6270, 0, 0),
79 12, 12, TLV_DB_SCALE_ITEM(-6020, 0, 0),
80 13, 13, TLV_DB_SCALE_ITEM(-5670, 0, 0),
81 14, 14, TLV_DB_SCALE_ITEM(-5420, 0, 0),
82 15, 17, TLV_DB_SCALE_ITEM(-5060, 250, 0),
83 18, 18, TLV_DB_SCALE_ITEM(-4370, 0, 0),
84 19, 19, TLV_DB_SCALE_ITEM(-4210, 0, 0),
85 20, 20, TLV_DB_SCALE_ITEM(-3960, 0, 0),
86 21, 21, TLV_DB_SCALE_ITEM(-3760, 0, 0),
87 22, 22, TLV_DB_SCALE_ITEM(-3600, 0, 0),
88 23, 23, TLV_DB_SCALE_ITEM(-3340, 0, 0),
89 24, 24, TLV_DB_SCALE_ITEM(-3150, 0, 0),
90 25, 25, TLV_DB_SCALE_ITEM(-2980, 0, 0),
91 26, 26, TLV_DB_SCALE_ITEM(-2720, 0, 0),
92 27, 27, TLV_DB_SCALE_ITEM(-2520, 0, 0),
93 28, 30, TLV_DB_SCALE_ITEM(-2350, 190, 0),
94 31, 31, TLV_DB_SCALE_ITEM(-1750, 0, 0),
95 32, 34, TLV_DB_SCALE_ITEM(-1640, 100, 0),
96 35, 37, TLV_DB_SCALE_ITEM(-1310, 110, 0),
97 38, 39, TLV_DB_SCALE_ITEM(-990, 100, 0),
98 40, 40, TLV_DB_SCALE_ITEM(-710, 0, 0),
99 41, 41, TLV_DB_SCALE_ITEM(-600, 0, 0),
100 42, 42, TLV_DB_SCALE_ITEM(-500, 0, 0),
101 43, 43, TLV_DB_SCALE_ITEM(-340, 0, 0),
102 44, 44, TLV_DB_SCALE_ITEM(-190, 0, 0),
103 45, 45, TLV_DB_SCALE_ITEM(-50, 0, 0),
104 46, 46, TLV_DB_SCALE_ITEM(50, 0, 0),
105 47, 50, TLV_DB_SCALE_ITEM(120, 40, 0),
106 51, 57, TLV_DB_SCALE_ITEM(290, 50, 0),
107 58, 58, TLV_DB_SCALE_ITEM(650, 0, 0),
108 59, 62, TLV_DB_SCALE_ITEM(700, 60, 0),
109 63, 63, TLV_DB_SCALE_ITEM(950, 0, 0)
110);
111
112static const struct snd_kcontrol_new max9768_volume[] = {
113 SOC_SINGLE_TLV("Playback Volume", MAX9768_VOL, 0, 63, 0, volume_tlv),
114};
115
116static const struct snd_kcontrol_new max9768_mute[] = {
117 SOC_SINGLE_BOOL_EXT("Playback Switch", 0, max9768_get_gpio, max9768_set_gpio),
118};
119
120static const struct snd_soc_dapm_widget max9768_dapm_widgets[] = {
121SND_SOC_DAPM_INPUT("IN"),
122
123SND_SOC_DAPM_OUTPUT("OUT+"),
124SND_SOC_DAPM_OUTPUT("OUT-"),
125};
126
127static const struct snd_soc_dapm_route max9768_dapm_routes[] = {
128 { "OUT+", NULL, "IN" },
129 { "OUT-", NULL, "IN" },
130};
131
132static int max9768_probe(struct snd_soc_component *component)
133{
134 struct max9768 *max9768 = snd_soc_component_get_drvdata(component);
135 int ret;
136
137 if (max9768->flags & MAX9768_FLAG_CLASSIC_PWM) {
138 ret = regmap_write(max9768->regmap, MAX9768_CTRL,
139 MAX9768_CTRL_PWM);
140 if (ret)
141 return ret;
142 }
143
144 if (gpio_is_valid(max9768->mute_gpio)) {
145 ret = snd_soc_add_component_controls(component, max9768_mute,
146 ARRAY_SIZE(max9768_mute));
147 if (ret)
148 return ret;
149 }
150
151 return 0;
152}
153
154static struct snd_soc_component_driver max9768_component_driver = {
155 .probe = max9768_probe,
156 .controls = max9768_volume,
157 .num_controls = ARRAY_SIZE(max9768_volume),
158 .dapm_widgets = max9768_dapm_widgets,
159 .num_dapm_widgets = ARRAY_SIZE(max9768_dapm_widgets),
160 .dapm_routes = max9768_dapm_routes,
161 .num_dapm_routes = ARRAY_SIZE(max9768_dapm_routes),
162};
163
164static const struct regmap_config max9768_i2c_regmap_config = {
165 .reg_bits = 2,
166 .val_bits = 6,
167 .max_register = 3,
168 .reg_defaults = max9768_default_regs,
169 .num_reg_defaults = ARRAY_SIZE(max9768_default_regs),
170 .cache_type = REGCACHE_RBTREE,
171};
172
173static int max9768_i2c_probe(struct i2c_client *client,
174 const struct i2c_device_id *id)
175{
176 struct max9768 *max9768;
177 struct max9768_pdata *pdata = client->dev.platform_data;
178 int err;
179
180 max9768 = devm_kzalloc(&client->dev, sizeof(*max9768), GFP_KERNEL);
181 if (!max9768)
182 return -ENOMEM;
183
184 if (pdata) {
185 /* Mute on powerup to avoid clicks */
186 err = devm_gpio_request_one(&client->dev, pdata->mute_gpio,
187 GPIOF_INIT_HIGH, "MAX9768 Mute");
188 max9768->mute_gpio = err ?: pdata->mute_gpio;
189
190 /* Activate chip by releasing shutdown, enables I2C */
191 err = devm_gpio_request_one(&client->dev, pdata->shdn_gpio,
192 GPIOF_INIT_HIGH, "MAX9768 Shutdown");
193 max9768->shdn_gpio = err ?: pdata->shdn_gpio;
194
195 max9768->flags = pdata->flags;
196 } else {
197 max9768->shdn_gpio = -EINVAL;
198 max9768->mute_gpio = -EINVAL;
199 }
200
201 i2c_set_clientdata(client, max9768);
202
203 max9768->regmap = devm_regmap_init_i2c(client, &max9768_i2c_regmap_config);
204 if (IS_ERR(max9768->regmap))
205 return PTR_ERR(max9768->regmap);
206
207 return devm_snd_soc_register_component(&client->dev,
208 &max9768_component_driver, NULL, 0);
209}
210
211static const struct i2c_device_id max9768_i2c_id[] = {
212 { "max9768", 0 },
213 { }
214};
215MODULE_DEVICE_TABLE(i2c, max9768_i2c_id);
216
217static struct i2c_driver max9768_i2c_driver = {
218 .driver = {
219 .name = "max9768",
220 },
221 .probe = max9768_i2c_probe,
222 .id_table = max9768_i2c_id,
223};
224module_i2c_driver(max9768_i2c_driver);
225
226MODULE_AUTHOR("Wolfram Sang <w.sang@pengutronix.de>");
227MODULE_DESCRIPTION("ASoC MAX9768 amplifier driver");
228MODULE_LICENSE("GPL v2");