18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * MAX9768 AMP driver 48c2ecf20Sopenharmony_ci * 58c2ecf20Sopenharmony_ci * Copyright (C) 2011, 2012 by Wolfram Sang, Pengutronix e.K. 68c2ecf20Sopenharmony_ci */ 78c2ecf20Sopenharmony_ci 88c2ecf20Sopenharmony_ci#include <linux/init.h> 98c2ecf20Sopenharmony_ci#include <linux/module.h> 108c2ecf20Sopenharmony_ci#include <linux/i2c.h> 118c2ecf20Sopenharmony_ci#include <linux/slab.h> 128c2ecf20Sopenharmony_ci#include <linux/gpio.h> 138c2ecf20Sopenharmony_ci#include <linux/regmap.h> 148c2ecf20Sopenharmony_ci 158c2ecf20Sopenharmony_ci#include <sound/core.h> 168c2ecf20Sopenharmony_ci#include <sound/soc.h> 178c2ecf20Sopenharmony_ci#include <sound/tlv.h> 188c2ecf20Sopenharmony_ci#include <sound/max9768.h> 198c2ecf20Sopenharmony_ci 208c2ecf20Sopenharmony_ci/* "Registers" */ 218c2ecf20Sopenharmony_ci#define MAX9768_VOL 0 228c2ecf20Sopenharmony_ci#define MAX9768_CTRL 3 238c2ecf20Sopenharmony_ci 248c2ecf20Sopenharmony_ci/* Commands */ 258c2ecf20Sopenharmony_ci#define MAX9768_CTRL_PWM 0x15 268c2ecf20Sopenharmony_ci#define MAX9768_CTRL_FILTERLESS 0x16 278c2ecf20Sopenharmony_ci 288c2ecf20Sopenharmony_cistruct max9768 { 298c2ecf20Sopenharmony_ci struct regmap *regmap; 308c2ecf20Sopenharmony_ci int mute_gpio; 318c2ecf20Sopenharmony_ci int shdn_gpio; 328c2ecf20Sopenharmony_ci u32 flags; 338c2ecf20Sopenharmony_ci}; 348c2ecf20Sopenharmony_ci 358c2ecf20Sopenharmony_cistatic const struct reg_default max9768_default_regs[] = { 368c2ecf20Sopenharmony_ci { 0, 0 }, 378c2ecf20Sopenharmony_ci { 3, MAX9768_CTRL_FILTERLESS}, 388c2ecf20Sopenharmony_ci}; 398c2ecf20Sopenharmony_ci 408c2ecf20Sopenharmony_cistatic int max9768_get_gpio(struct snd_kcontrol *kcontrol, 418c2ecf20Sopenharmony_ci struct snd_ctl_elem_value *ucontrol) 428c2ecf20Sopenharmony_ci{ 438c2ecf20Sopenharmony_ci struct snd_soc_component *c = snd_soc_kcontrol_component(kcontrol); 448c2ecf20Sopenharmony_ci struct max9768 *max9768 = snd_soc_component_get_drvdata(c); 458c2ecf20Sopenharmony_ci int val = gpio_get_value_cansleep(max9768->mute_gpio); 468c2ecf20Sopenharmony_ci 478c2ecf20Sopenharmony_ci ucontrol->value.integer.value[0] = !val; 488c2ecf20Sopenharmony_ci 498c2ecf20Sopenharmony_ci return 0; 508c2ecf20Sopenharmony_ci} 518c2ecf20Sopenharmony_ci 528c2ecf20Sopenharmony_cistatic int max9768_set_gpio(struct snd_kcontrol *kcontrol, 538c2ecf20Sopenharmony_ci struct snd_ctl_elem_value *ucontrol) 548c2ecf20Sopenharmony_ci{ 558c2ecf20Sopenharmony_ci struct snd_soc_component *c = snd_soc_kcontrol_component(kcontrol); 568c2ecf20Sopenharmony_ci struct max9768 *max9768 = snd_soc_component_get_drvdata(c); 578c2ecf20Sopenharmony_ci 588c2ecf20Sopenharmony_ci gpio_set_value_cansleep(max9768->mute_gpio, !ucontrol->value.integer.value[0]); 598c2ecf20Sopenharmony_ci 608c2ecf20Sopenharmony_ci return 0; 618c2ecf20Sopenharmony_ci} 628c2ecf20Sopenharmony_ci 638c2ecf20Sopenharmony_cistatic const DECLARE_TLV_DB_RANGE(volume_tlv, 648c2ecf20Sopenharmony_ci 0, 0, TLV_DB_SCALE_ITEM(-16150, 0, 0), 658c2ecf20Sopenharmony_ci 1, 1, TLV_DB_SCALE_ITEM(-9280, 0, 0), 668c2ecf20Sopenharmony_ci 2, 2, TLV_DB_SCALE_ITEM(-9030, 0, 0), 678c2ecf20Sopenharmony_ci 3, 3, TLV_DB_SCALE_ITEM(-8680, 0, 0), 688c2ecf20Sopenharmony_ci 4, 4, TLV_DB_SCALE_ITEM(-8430, 0, 0), 698c2ecf20Sopenharmony_ci 5, 5, TLV_DB_SCALE_ITEM(-8080, 0, 0), 708c2ecf20Sopenharmony_ci 6, 6, TLV_DB_SCALE_ITEM(-7830, 0, 0), 718c2ecf20Sopenharmony_ci 7, 7, TLV_DB_SCALE_ITEM(-7470, 0, 0), 728c2ecf20Sopenharmony_ci 8, 8, TLV_DB_SCALE_ITEM(-7220, 0, 0), 738c2ecf20Sopenharmony_ci 9, 9, TLV_DB_SCALE_ITEM(-6870, 0, 0), 748c2ecf20Sopenharmony_ci 10, 10, TLV_DB_SCALE_ITEM(-6620, 0, 0), 758c2ecf20Sopenharmony_ci 11, 11, TLV_DB_SCALE_ITEM(-6270, 0, 0), 768c2ecf20Sopenharmony_ci 12, 12, TLV_DB_SCALE_ITEM(-6020, 0, 0), 778c2ecf20Sopenharmony_ci 13, 13, TLV_DB_SCALE_ITEM(-5670, 0, 0), 788c2ecf20Sopenharmony_ci 14, 14, TLV_DB_SCALE_ITEM(-5420, 0, 0), 798c2ecf20Sopenharmony_ci 15, 17, TLV_DB_SCALE_ITEM(-5060, 250, 0), 808c2ecf20Sopenharmony_ci 18, 18, TLV_DB_SCALE_ITEM(-4370, 0, 0), 818c2ecf20Sopenharmony_ci 19, 19, TLV_DB_SCALE_ITEM(-4210, 0, 0), 828c2ecf20Sopenharmony_ci 20, 20, TLV_DB_SCALE_ITEM(-3960, 0, 0), 838c2ecf20Sopenharmony_ci 21, 21, TLV_DB_SCALE_ITEM(-3760, 0, 0), 848c2ecf20Sopenharmony_ci 22, 22, TLV_DB_SCALE_ITEM(-3600, 0, 0), 858c2ecf20Sopenharmony_ci 23, 23, TLV_DB_SCALE_ITEM(-3340, 0, 0), 868c2ecf20Sopenharmony_ci 24, 24, TLV_DB_SCALE_ITEM(-3150, 0, 0), 878c2ecf20Sopenharmony_ci 25, 25, TLV_DB_SCALE_ITEM(-2980, 0, 0), 888c2ecf20Sopenharmony_ci 26, 26, TLV_DB_SCALE_ITEM(-2720, 0, 0), 898c2ecf20Sopenharmony_ci 27, 27, TLV_DB_SCALE_ITEM(-2520, 0, 0), 908c2ecf20Sopenharmony_ci 28, 30, TLV_DB_SCALE_ITEM(-2350, 190, 0), 918c2ecf20Sopenharmony_ci 31, 31, TLV_DB_SCALE_ITEM(-1750, 0, 0), 928c2ecf20Sopenharmony_ci 32, 34, TLV_DB_SCALE_ITEM(-1640, 100, 0), 938c2ecf20Sopenharmony_ci 35, 37, TLV_DB_SCALE_ITEM(-1310, 110, 0), 948c2ecf20Sopenharmony_ci 38, 39, TLV_DB_SCALE_ITEM(-990, 100, 0), 958c2ecf20Sopenharmony_ci 40, 40, TLV_DB_SCALE_ITEM(-710, 0, 0), 968c2ecf20Sopenharmony_ci 41, 41, TLV_DB_SCALE_ITEM(-600, 0, 0), 978c2ecf20Sopenharmony_ci 42, 42, TLV_DB_SCALE_ITEM(-500, 0, 0), 988c2ecf20Sopenharmony_ci 43, 43, TLV_DB_SCALE_ITEM(-340, 0, 0), 998c2ecf20Sopenharmony_ci 44, 44, TLV_DB_SCALE_ITEM(-190, 0, 0), 1008c2ecf20Sopenharmony_ci 45, 45, TLV_DB_SCALE_ITEM(-50, 0, 0), 1018c2ecf20Sopenharmony_ci 46, 46, TLV_DB_SCALE_ITEM(50, 0, 0), 1028c2ecf20Sopenharmony_ci 47, 50, TLV_DB_SCALE_ITEM(120, 40, 0), 1038c2ecf20Sopenharmony_ci 51, 57, TLV_DB_SCALE_ITEM(290, 50, 0), 1048c2ecf20Sopenharmony_ci 58, 58, TLV_DB_SCALE_ITEM(650, 0, 0), 1058c2ecf20Sopenharmony_ci 59, 62, TLV_DB_SCALE_ITEM(700, 60, 0), 1068c2ecf20Sopenharmony_ci 63, 63, TLV_DB_SCALE_ITEM(950, 0, 0) 1078c2ecf20Sopenharmony_ci); 1088c2ecf20Sopenharmony_ci 1098c2ecf20Sopenharmony_cistatic const struct snd_kcontrol_new max9768_volume[] = { 1108c2ecf20Sopenharmony_ci SOC_SINGLE_TLV("Playback Volume", MAX9768_VOL, 0, 63, 0, volume_tlv), 1118c2ecf20Sopenharmony_ci}; 1128c2ecf20Sopenharmony_ci 1138c2ecf20Sopenharmony_cistatic const struct snd_kcontrol_new max9768_mute[] = { 1148c2ecf20Sopenharmony_ci SOC_SINGLE_BOOL_EXT("Playback Switch", 0, max9768_get_gpio, max9768_set_gpio), 1158c2ecf20Sopenharmony_ci}; 1168c2ecf20Sopenharmony_ci 1178c2ecf20Sopenharmony_cistatic const struct snd_soc_dapm_widget max9768_dapm_widgets[] = { 1188c2ecf20Sopenharmony_ciSND_SOC_DAPM_INPUT("IN"), 1198c2ecf20Sopenharmony_ci 1208c2ecf20Sopenharmony_ciSND_SOC_DAPM_OUTPUT("OUT+"), 1218c2ecf20Sopenharmony_ciSND_SOC_DAPM_OUTPUT("OUT-"), 1228c2ecf20Sopenharmony_ci}; 1238c2ecf20Sopenharmony_ci 1248c2ecf20Sopenharmony_cistatic const struct snd_soc_dapm_route max9768_dapm_routes[] = { 1258c2ecf20Sopenharmony_ci { "OUT+", NULL, "IN" }, 1268c2ecf20Sopenharmony_ci { "OUT-", NULL, "IN" }, 1278c2ecf20Sopenharmony_ci}; 1288c2ecf20Sopenharmony_ci 1298c2ecf20Sopenharmony_cistatic int max9768_probe(struct snd_soc_component *component) 1308c2ecf20Sopenharmony_ci{ 1318c2ecf20Sopenharmony_ci struct max9768 *max9768 = snd_soc_component_get_drvdata(component); 1328c2ecf20Sopenharmony_ci int ret; 1338c2ecf20Sopenharmony_ci 1348c2ecf20Sopenharmony_ci if (max9768->flags & MAX9768_FLAG_CLASSIC_PWM) { 1358c2ecf20Sopenharmony_ci ret = regmap_write(max9768->regmap, MAX9768_CTRL, 1368c2ecf20Sopenharmony_ci MAX9768_CTRL_PWM); 1378c2ecf20Sopenharmony_ci if (ret) 1388c2ecf20Sopenharmony_ci return ret; 1398c2ecf20Sopenharmony_ci } 1408c2ecf20Sopenharmony_ci 1418c2ecf20Sopenharmony_ci if (gpio_is_valid(max9768->mute_gpio)) { 1428c2ecf20Sopenharmony_ci ret = snd_soc_add_component_controls(component, max9768_mute, 1438c2ecf20Sopenharmony_ci ARRAY_SIZE(max9768_mute)); 1448c2ecf20Sopenharmony_ci if (ret) 1458c2ecf20Sopenharmony_ci return ret; 1468c2ecf20Sopenharmony_ci } 1478c2ecf20Sopenharmony_ci 1488c2ecf20Sopenharmony_ci return 0; 1498c2ecf20Sopenharmony_ci} 1508c2ecf20Sopenharmony_ci 1518c2ecf20Sopenharmony_cistatic const struct snd_soc_component_driver max9768_component_driver = { 1528c2ecf20Sopenharmony_ci .probe = max9768_probe, 1538c2ecf20Sopenharmony_ci .controls = max9768_volume, 1548c2ecf20Sopenharmony_ci .num_controls = ARRAY_SIZE(max9768_volume), 1558c2ecf20Sopenharmony_ci .dapm_widgets = max9768_dapm_widgets, 1568c2ecf20Sopenharmony_ci .num_dapm_widgets = ARRAY_SIZE(max9768_dapm_widgets), 1578c2ecf20Sopenharmony_ci .dapm_routes = max9768_dapm_routes, 1588c2ecf20Sopenharmony_ci .num_dapm_routes = ARRAY_SIZE(max9768_dapm_routes), 1598c2ecf20Sopenharmony_ci}; 1608c2ecf20Sopenharmony_ci 1618c2ecf20Sopenharmony_cistatic const struct regmap_config max9768_i2c_regmap_config = { 1628c2ecf20Sopenharmony_ci .reg_bits = 2, 1638c2ecf20Sopenharmony_ci .val_bits = 6, 1648c2ecf20Sopenharmony_ci .max_register = 3, 1658c2ecf20Sopenharmony_ci .reg_defaults = max9768_default_regs, 1668c2ecf20Sopenharmony_ci .num_reg_defaults = ARRAY_SIZE(max9768_default_regs), 1678c2ecf20Sopenharmony_ci .cache_type = REGCACHE_RBTREE, 1688c2ecf20Sopenharmony_ci}; 1698c2ecf20Sopenharmony_ci 1708c2ecf20Sopenharmony_cistatic int max9768_i2c_probe(struct i2c_client *client, 1718c2ecf20Sopenharmony_ci const struct i2c_device_id *id) 1728c2ecf20Sopenharmony_ci{ 1738c2ecf20Sopenharmony_ci struct max9768 *max9768; 1748c2ecf20Sopenharmony_ci struct max9768_pdata *pdata = client->dev.platform_data; 1758c2ecf20Sopenharmony_ci int err; 1768c2ecf20Sopenharmony_ci 1778c2ecf20Sopenharmony_ci max9768 = devm_kzalloc(&client->dev, sizeof(*max9768), GFP_KERNEL); 1788c2ecf20Sopenharmony_ci if (!max9768) 1798c2ecf20Sopenharmony_ci return -ENOMEM; 1808c2ecf20Sopenharmony_ci 1818c2ecf20Sopenharmony_ci if (pdata) { 1828c2ecf20Sopenharmony_ci /* Mute on powerup to avoid clicks */ 1838c2ecf20Sopenharmony_ci err = devm_gpio_request_one(&client->dev, pdata->mute_gpio, 1848c2ecf20Sopenharmony_ci GPIOF_INIT_HIGH, "MAX9768 Mute"); 1858c2ecf20Sopenharmony_ci max9768->mute_gpio = err ?: pdata->mute_gpio; 1868c2ecf20Sopenharmony_ci 1878c2ecf20Sopenharmony_ci /* Activate chip by releasing shutdown, enables I2C */ 1888c2ecf20Sopenharmony_ci err = devm_gpio_request_one(&client->dev, pdata->shdn_gpio, 1898c2ecf20Sopenharmony_ci GPIOF_INIT_HIGH, "MAX9768 Shutdown"); 1908c2ecf20Sopenharmony_ci max9768->shdn_gpio = err ?: pdata->shdn_gpio; 1918c2ecf20Sopenharmony_ci 1928c2ecf20Sopenharmony_ci max9768->flags = pdata->flags; 1938c2ecf20Sopenharmony_ci } else { 1948c2ecf20Sopenharmony_ci max9768->shdn_gpio = -EINVAL; 1958c2ecf20Sopenharmony_ci max9768->mute_gpio = -EINVAL; 1968c2ecf20Sopenharmony_ci } 1978c2ecf20Sopenharmony_ci 1988c2ecf20Sopenharmony_ci i2c_set_clientdata(client, max9768); 1998c2ecf20Sopenharmony_ci 2008c2ecf20Sopenharmony_ci max9768->regmap = devm_regmap_init_i2c(client, &max9768_i2c_regmap_config); 2018c2ecf20Sopenharmony_ci if (IS_ERR(max9768->regmap)) 2028c2ecf20Sopenharmony_ci return PTR_ERR(max9768->regmap); 2038c2ecf20Sopenharmony_ci 2048c2ecf20Sopenharmony_ci return devm_snd_soc_register_component(&client->dev, 2058c2ecf20Sopenharmony_ci &max9768_component_driver, NULL, 0); 2068c2ecf20Sopenharmony_ci} 2078c2ecf20Sopenharmony_ci 2088c2ecf20Sopenharmony_cistatic const struct i2c_device_id max9768_i2c_id[] = { 2098c2ecf20Sopenharmony_ci { "max9768", 0 }, 2108c2ecf20Sopenharmony_ci { } 2118c2ecf20Sopenharmony_ci}; 2128c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(i2c, max9768_i2c_id); 2138c2ecf20Sopenharmony_ci 2148c2ecf20Sopenharmony_cistatic struct i2c_driver max9768_i2c_driver = { 2158c2ecf20Sopenharmony_ci .driver = { 2168c2ecf20Sopenharmony_ci .name = "max9768", 2178c2ecf20Sopenharmony_ci }, 2188c2ecf20Sopenharmony_ci .probe = max9768_i2c_probe, 2198c2ecf20Sopenharmony_ci .id_table = max9768_i2c_id, 2208c2ecf20Sopenharmony_ci}; 2218c2ecf20Sopenharmony_cimodule_i2c_driver(max9768_i2c_driver); 2228c2ecf20Sopenharmony_ci 2238c2ecf20Sopenharmony_ciMODULE_AUTHOR("Wolfram Sang <kernel@pengutronix.de>"); 2248c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("ASoC MAX9768 amplifier driver"); 2258c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL v2"); 226