Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * MAX9768 AMP driver
0004  *
0005  * Copyright (C) 2011, 2012 by Wolfram Sang, Pengutronix e.K.
0006  */
0007 
0008 #include <linux/init.h>
0009 #include <linux/module.h>
0010 #include <linux/i2c.h>
0011 #include <linux/slab.h>
0012 #include <linux/gpio.h>
0013 #include <linux/regmap.h>
0014 
0015 #include <sound/core.h>
0016 #include <sound/soc.h>
0017 #include <sound/tlv.h>
0018 #include <sound/max9768.h>
0019 
0020 /* "Registers" */
0021 #define MAX9768_VOL 0
0022 #define MAX9768_CTRL 3
0023 
0024 /* Commands */
0025 #define MAX9768_CTRL_PWM 0x15
0026 #define MAX9768_CTRL_FILTERLESS 0x16
0027 
0028 struct max9768 {
0029     struct regmap *regmap;
0030     int mute_gpio;
0031     int shdn_gpio;
0032     u32 flags;
0033 };
0034 
0035 static const struct reg_default max9768_default_regs[] = {
0036     { 0, 0 },
0037     { 3,  MAX9768_CTRL_FILTERLESS},
0038 };
0039 
0040 static int max9768_get_gpio(struct snd_kcontrol *kcontrol,
0041     struct snd_ctl_elem_value *ucontrol)
0042 {
0043     struct snd_soc_component *c = snd_soc_kcontrol_component(kcontrol);
0044     struct max9768 *max9768 = snd_soc_component_get_drvdata(c);
0045     int val = gpio_get_value_cansleep(max9768->mute_gpio);
0046 
0047     ucontrol->value.integer.value[0] = !val;
0048 
0049     return 0;
0050 }
0051 
0052 static int max9768_set_gpio(struct snd_kcontrol *kcontrol,
0053     struct snd_ctl_elem_value *ucontrol)
0054 {
0055     struct snd_soc_component *c = snd_soc_kcontrol_component(kcontrol);
0056     struct max9768 *max9768 = snd_soc_component_get_drvdata(c);
0057 
0058     gpio_set_value_cansleep(max9768->mute_gpio, !ucontrol->value.integer.value[0]);
0059 
0060     return 0;
0061 }
0062 
0063 static const DECLARE_TLV_DB_RANGE(volume_tlv,
0064     0, 0, TLV_DB_SCALE_ITEM(-16150, 0, 0),
0065     1, 1, TLV_DB_SCALE_ITEM(-9280, 0, 0),
0066     2, 2, TLV_DB_SCALE_ITEM(-9030, 0, 0),
0067     3, 3, TLV_DB_SCALE_ITEM(-8680, 0, 0),
0068     4, 4, TLV_DB_SCALE_ITEM(-8430, 0, 0),
0069     5, 5, TLV_DB_SCALE_ITEM(-8080, 0, 0),
0070     6, 6, TLV_DB_SCALE_ITEM(-7830, 0, 0),
0071     7, 7, TLV_DB_SCALE_ITEM(-7470, 0, 0),
0072     8, 8, TLV_DB_SCALE_ITEM(-7220, 0, 0),
0073     9, 9, TLV_DB_SCALE_ITEM(-6870, 0, 0),
0074     10, 10, TLV_DB_SCALE_ITEM(-6620, 0, 0),
0075     11, 11, TLV_DB_SCALE_ITEM(-6270, 0, 0),
0076     12, 12, TLV_DB_SCALE_ITEM(-6020, 0, 0),
0077     13, 13, TLV_DB_SCALE_ITEM(-5670, 0, 0),
0078     14, 14, TLV_DB_SCALE_ITEM(-5420, 0, 0),
0079     15, 17, TLV_DB_SCALE_ITEM(-5060, 250, 0),
0080     18, 18, TLV_DB_SCALE_ITEM(-4370, 0, 0),
0081     19, 19, TLV_DB_SCALE_ITEM(-4210, 0, 0),
0082     20, 20, TLV_DB_SCALE_ITEM(-3960, 0, 0),
0083     21, 21, TLV_DB_SCALE_ITEM(-3760, 0, 0),
0084     22, 22, TLV_DB_SCALE_ITEM(-3600, 0, 0),
0085     23, 23, TLV_DB_SCALE_ITEM(-3340, 0, 0),
0086     24, 24, TLV_DB_SCALE_ITEM(-3150, 0, 0),
0087     25, 25, TLV_DB_SCALE_ITEM(-2980, 0, 0),
0088     26, 26, TLV_DB_SCALE_ITEM(-2720, 0, 0),
0089     27, 27, TLV_DB_SCALE_ITEM(-2520, 0, 0),
0090     28, 30, TLV_DB_SCALE_ITEM(-2350, 190, 0),
0091     31, 31, TLV_DB_SCALE_ITEM(-1750, 0, 0),
0092     32, 34, TLV_DB_SCALE_ITEM(-1640, 100, 0),
0093     35, 37, TLV_DB_SCALE_ITEM(-1310, 110, 0),
0094     38, 39, TLV_DB_SCALE_ITEM(-990, 100, 0),
0095     40, 40, TLV_DB_SCALE_ITEM(-710, 0, 0),
0096     41, 41, TLV_DB_SCALE_ITEM(-600, 0, 0),
0097     42, 42, TLV_DB_SCALE_ITEM(-500, 0, 0),
0098     43, 43, TLV_DB_SCALE_ITEM(-340, 0, 0),
0099     44, 44, TLV_DB_SCALE_ITEM(-190, 0, 0),
0100     45, 45, TLV_DB_SCALE_ITEM(-50, 0, 0),
0101     46, 46, TLV_DB_SCALE_ITEM(50, 0, 0),
0102     47, 50, TLV_DB_SCALE_ITEM(120, 40, 0),
0103     51, 57, TLV_DB_SCALE_ITEM(290, 50, 0),
0104     58, 58, TLV_DB_SCALE_ITEM(650, 0, 0),
0105     59, 62, TLV_DB_SCALE_ITEM(700, 60, 0),
0106     63, 63, TLV_DB_SCALE_ITEM(950, 0, 0)
0107 );
0108 
0109 static const struct snd_kcontrol_new max9768_volume[] = {
0110     SOC_SINGLE_TLV("Playback Volume", MAX9768_VOL, 0, 63, 0, volume_tlv),
0111 };
0112 
0113 static const struct snd_kcontrol_new max9768_mute[] = {
0114     SOC_SINGLE_BOOL_EXT("Playback Switch", 0, max9768_get_gpio, max9768_set_gpio),
0115 };
0116 
0117 static const struct snd_soc_dapm_widget max9768_dapm_widgets[] = {
0118 SND_SOC_DAPM_INPUT("IN"),
0119 
0120 SND_SOC_DAPM_OUTPUT("OUT+"),
0121 SND_SOC_DAPM_OUTPUT("OUT-"),
0122 };
0123 
0124 static const struct snd_soc_dapm_route max9768_dapm_routes[] = {
0125     { "OUT+", NULL, "IN" },
0126     { "OUT-", NULL, "IN" },
0127 };
0128 
0129 static int max9768_probe(struct snd_soc_component *component)
0130 {
0131     struct max9768 *max9768 = snd_soc_component_get_drvdata(component);
0132     int ret;
0133 
0134     if (max9768->flags & MAX9768_FLAG_CLASSIC_PWM) {
0135         ret = regmap_write(max9768->regmap, MAX9768_CTRL,
0136             MAX9768_CTRL_PWM);
0137         if (ret)
0138             return ret;
0139     }
0140 
0141     if (gpio_is_valid(max9768->mute_gpio)) {
0142         ret = snd_soc_add_component_controls(component, max9768_mute,
0143                 ARRAY_SIZE(max9768_mute));
0144         if (ret)
0145             return ret;
0146     }
0147 
0148     return 0;
0149 }
0150 
0151 static const struct snd_soc_component_driver max9768_component_driver = {
0152     .probe = max9768_probe,
0153     .controls = max9768_volume,
0154     .num_controls = ARRAY_SIZE(max9768_volume),
0155     .dapm_widgets = max9768_dapm_widgets,
0156     .num_dapm_widgets = ARRAY_SIZE(max9768_dapm_widgets),
0157     .dapm_routes = max9768_dapm_routes,
0158     .num_dapm_routes = ARRAY_SIZE(max9768_dapm_routes),
0159 };
0160 
0161 static const struct regmap_config max9768_i2c_regmap_config = {
0162     .reg_bits = 2,
0163     .val_bits = 6,
0164     .max_register = 3,
0165     .reg_defaults = max9768_default_regs,
0166     .num_reg_defaults = ARRAY_SIZE(max9768_default_regs),
0167     .cache_type = REGCACHE_RBTREE,
0168 };
0169 
0170 static int max9768_i2c_probe(struct i2c_client *client)
0171 {
0172     struct max9768 *max9768;
0173     struct max9768_pdata *pdata = client->dev.platform_data;
0174     int err;
0175 
0176     max9768 = devm_kzalloc(&client->dev, sizeof(*max9768), GFP_KERNEL);
0177     if (!max9768)
0178         return -ENOMEM;
0179 
0180     if (pdata) {
0181         /* Mute on powerup to avoid clicks */
0182         err = devm_gpio_request_one(&client->dev, pdata->mute_gpio,
0183                 GPIOF_INIT_HIGH, "MAX9768 Mute");
0184         max9768->mute_gpio = err ?: pdata->mute_gpio;
0185 
0186         /* Activate chip by releasing shutdown, enables I2C */
0187         err = devm_gpio_request_one(&client->dev, pdata->shdn_gpio,
0188                 GPIOF_INIT_HIGH, "MAX9768 Shutdown");
0189         max9768->shdn_gpio = err ?: pdata->shdn_gpio;
0190 
0191         max9768->flags = pdata->flags;
0192     } else {
0193         max9768->shdn_gpio = -EINVAL;
0194         max9768->mute_gpio = -EINVAL;
0195     }
0196 
0197     i2c_set_clientdata(client, max9768);
0198 
0199     max9768->regmap = devm_regmap_init_i2c(client, &max9768_i2c_regmap_config);
0200     if (IS_ERR(max9768->regmap))
0201         return PTR_ERR(max9768->regmap);
0202 
0203     return devm_snd_soc_register_component(&client->dev,
0204         &max9768_component_driver, NULL, 0);
0205 }
0206 
0207 static const struct i2c_device_id max9768_i2c_id[] = {
0208     { "max9768", 0 },
0209     { }
0210 };
0211 MODULE_DEVICE_TABLE(i2c, max9768_i2c_id);
0212 
0213 static struct i2c_driver max9768_i2c_driver = {
0214     .driver = {
0215         .name = "max9768",
0216     },
0217     .probe_new = max9768_i2c_probe,
0218     .id_table = max9768_i2c_id,
0219 };
0220 module_i2c_driver(max9768_i2c_driver);
0221 
0222 MODULE_AUTHOR("Wolfram Sang <kernel@pengutronix.de>");
0223 MODULE_DESCRIPTION("ASoC MAX9768 amplifier driver");
0224 MODULE_LICENSE("GPL v2");