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