1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * max9877.c -- amp driver for max9877
4 *
5 * Copyright (C) 2009 Samsung Electronics Co.Ltd
6 * Author: Joonyoung Shim <jy0922.shim@samsung.com>
7 */
8
9 #include <linux/module.h>
10 #include <linux/init.h>
11 #include <linux/i2c.h>
12 #include <linux/regmap.h>
13 #include <sound/soc.h>
14 #include <sound/tlv.h>
15
16 #include "max9877.h"
17
18 static const struct reg_default max9877_regs[] = {
19 { 0, 0x40 },
20 { 1, 0x00 },
21 { 2, 0x00 },
22 { 3, 0x00 },
23 { 4, 0x49 },
24 };
25
26 static const DECLARE_TLV_DB_RANGE(max9877_pgain_tlv,
27 0, 1, TLV_DB_SCALE_ITEM(0, 900, 0),
28 2, 2, TLV_DB_SCALE_ITEM(2000, 0, 0)
29 );
30
31 static const DECLARE_TLV_DB_RANGE(max9877_output_tlv,
32 0, 7, TLV_DB_SCALE_ITEM(-7900, 400, 1),
33 8, 15, TLV_DB_SCALE_ITEM(-4700, 300, 0),
34 16, 23, TLV_DB_SCALE_ITEM(-2300, 200, 0),
35 24, 31, TLV_DB_SCALE_ITEM(-700, 100, 0)
36 );
37
38 static const char *max9877_out_mode[] = {
39 "INA -> SPK",
40 "INA -> HP",
41 "INA -> SPK and HP",
42 "INB -> SPK",
43 "INB -> HP",
44 "INB -> SPK and HP",
45 "INA + INB -> SPK",
46 "INA + INB -> HP",
47 "INA + INB -> SPK and HP",
48 };
49
50 static const char *max9877_osc_mode[] = {
51 "1176KHz",
52 "1100KHz",
53 "700KHz",
54 };
55
56 static const struct soc_enum max9877_enum[] = {
57 SOC_ENUM_SINGLE(MAX9877_OUTPUT_MODE, 0, ARRAY_SIZE(max9877_out_mode),
58 max9877_out_mode),
59 SOC_ENUM_SINGLE(MAX9877_OUTPUT_MODE, MAX9877_OSC_OFFSET,
60 ARRAY_SIZE(max9877_osc_mode), max9877_osc_mode),
61 };
62
63 static const struct snd_kcontrol_new max9877_controls[] = {
64 SOC_SINGLE_TLV("MAX9877 PGAINA Playback Volume",
65 MAX9877_INPUT_MODE, 0, 2, 0, max9877_pgain_tlv),
66 SOC_SINGLE_TLV("MAX9877 PGAINB Playback Volume",
67 MAX9877_INPUT_MODE, 2, 2, 0, max9877_pgain_tlv),
68 SOC_SINGLE_TLV("MAX9877 Amp Speaker Playback Volume",
69 MAX9877_SPK_VOLUME, 0, 31, 0, max9877_output_tlv),
70 SOC_DOUBLE_R_TLV("MAX9877 Amp HP Playback Volume",
71 MAX9877_HPL_VOLUME, MAX9877_HPR_VOLUME, 0, 31, 0,
72 max9877_output_tlv),
73 SOC_SINGLE("MAX9877 INB Stereo Switch",
74 MAX9877_INPUT_MODE, 4, 1, 1),
75 SOC_SINGLE("MAX9877 INA Stereo Switch",
76 MAX9877_INPUT_MODE, 5, 1, 1),
77 SOC_SINGLE("MAX9877 Zero-crossing detection Switch",
78 MAX9877_INPUT_MODE, 6, 1, 0),
79 SOC_SINGLE("MAX9877 Bypass Mode Switch",
80 MAX9877_OUTPUT_MODE, 6, 1, 0),
81 SOC_ENUM("MAX9877 Output Mode", max9877_enum[0]),
82 SOC_ENUM("MAX9877 Oscillator Mode", max9877_enum[1]),
83 };
84
85 static const struct snd_soc_dapm_widget max9877_dapm_widgets[] = {
86 SND_SOC_DAPM_INPUT("INA1"),
87 SND_SOC_DAPM_INPUT("INA2"),
88 SND_SOC_DAPM_INPUT("INB1"),
89 SND_SOC_DAPM_INPUT("INB2"),
90 SND_SOC_DAPM_INPUT("RXIN+"),
91 SND_SOC_DAPM_INPUT("RXIN-"),
92
93 SND_SOC_DAPM_PGA("SHDN", MAX9877_OUTPUT_MODE, 7, 1, NULL, 0),
94
95 SND_SOC_DAPM_OUTPUT("OUT+"),
96 SND_SOC_DAPM_OUTPUT("OUT-"),
97 SND_SOC_DAPM_OUTPUT("HPL"),
98 SND_SOC_DAPM_OUTPUT("HPR"),
99 };
100
101 static const struct snd_soc_dapm_route max9877_dapm_routes[] = {
102 { "SHDN", NULL, "INA1" },
103 { "SHDN", NULL, "INA2" },
104 { "SHDN", NULL, "INB1" },
105 { "SHDN", NULL, "INB2" },
106
107 { "OUT+", NULL, "RXIN+" },
108 { "OUT+", NULL, "SHDN" },
109
110 { "OUT-", NULL, "SHDN" },
111 { "OUT-", NULL, "RXIN-" },
112
113 { "HPL", NULL, "SHDN" },
114 { "HPR", NULL, "SHDN" },
115 };
116
117 static const struct snd_soc_component_driver max9877_component_driver = {
118 .controls = max9877_controls,
119 .num_controls = ARRAY_SIZE(max9877_controls),
120
121 .dapm_widgets = max9877_dapm_widgets,
122 .num_dapm_widgets = ARRAY_SIZE(max9877_dapm_widgets),
123 .dapm_routes = max9877_dapm_routes,
124 .num_dapm_routes = ARRAY_SIZE(max9877_dapm_routes),
125 };
126
127 static const struct regmap_config max9877_regmap = {
128 .reg_bits = 8,
129 .val_bits = 8,
130
131 .reg_defaults = max9877_regs,
132 .num_reg_defaults = ARRAY_SIZE(max9877_regs),
133 .cache_type = REGCACHE_RBTREE,
134 };
135
max9877_i2c_probe(struct i2c_client * client,const struct i2c_device_id * id)136 static int max9877_i2c_probe(struct i2c_client *client,
137 const struct i2c_device_id *id)
138 {
139 struct regmap *regmap;
140 int i;
141
142 regmap = devm_regmap_init_i2c(client, &max9877_regmap);
143 if (IS_ERR(regmap))
144 return PTR_ERR(regmap);
145
146 /* Ensure the device is in reset state */
147 for (i = 0; i < ARRAY_SIZE(max9877_regs); i++)
148 regmap_write(regmap, max9877_regs[i].reg, max9877_regs[i].def);
149
150 return devm_snd_soc_register_component(&client->dev,
151 &max9877_component_driver, NULL, 0);
152 }
153
154 static const struct i2c_device_id max9877_i2c_id[] = {
155 { "max9877", 0 },
156 { }
157 };
158 MODULE_DEVICE_TABLE(i2c, max9877_i2c_id);
159
160 static struct i2c_driver max9877_i2c_driver = {
161 .driver = {
162 .name = "max9877",
163 },
164 .probe = max9877_i2c_probe,
165 .id_table = max9877_i2c_id,
166 };
167
168 module_i2c_driver(max9877_i2c_driver);
169
170 MODULE_DESCRIPTION("ASoC MAX9877 amp driver");
171 MODULE_AUTHOR("Joonyoung Shim <jy0922.shim@samsung.com>");
172 MODULE_LICENSE("GPL");
173