xref: /openbmc/linux/sound/soc/codecs/wm1250-ev1.c (revision 5038d21d)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Driver for the 1250-EV1 audio I/O module
4  *
5  * Copyright 2011 Wolfson Microelectronics plc
6  */
7 
8 #include <linux/init.h>
9 #include <linux/module.h>
10 #include <linux/slab.h>
11 #include <linux/i2c.h>
12 #include <linux/gpio.h>
13 
14 #include <sound/soc.h>
15 #include <sound/soc-dapm.h>
16 #include <sound/wm1250-ev1.h>
17 
18 static const char *wm1250_gpio_names[WM1250_EV1_NUM_GPIOS] = {
19 	"WM1250 CLK_ENA",
20 	"WM1250 CLK_SEL0",
21 	"WM1250 CLK_SEL1",
22 	"WM1250 OSR",
23 	"WM1250 MASTER",
24 };
25 
26 struct wm1250_priv {
27 	struct gpio gpios[WM1250_EV1_NUM_GPIOS];
28 };
29 
30 static int wm1250_ev1_set_bias_level(struct snd_soc_component *component,
31 				     enum snd_soc_bias_level level)
32 {
33 	struct wm1250_priv *wm1250 = dev_get_drvdata(component->dev);
34 	int ena;
35 
36 	if (wm1250)
37 		ena = wm1250->gpios[WM1250_EV1_GPIO_CLK_ENA].gpio;
38 	else
39 		ena = -1;
40 
41 	switch (level) {
42 	case SND_SOC_BIAS_ON:
43 		break;
44 
45 	case SND_SOC_BIAS_PREPARE:
46 		break;
47 
48 	case SND_SOC_BIAS_STANDBY:
49 		if (ena >= 0)
50 			gpio_set_value_cansleep(ena, 1);
51 		break;
52 
53 	case SND_SOC_BIAS_OFF:
54 		if (ena >= 0)
55 			gpio_set_value_cansleep(ena, 0);
56 		break;
57 	}
58 
59 	return 0;
60 }
61 
62 static const struct snd_soc_dapm_widget wm1250_ev1_dapm_widgets[] = {
63 SND_SOC_DAPM_ADC("ADC", "wm1250-ev1 Capture", SND_SOC_NOPM, 0, 0),
64 SND_SOC_DAPM_DAC("DAC", "wm1250-ev1 Playback", SND_SOC_NOPM, 0, 0),
65 
66 SND_SOC_DAPM_INPUT("WM1250 Input"),
67 SND_SOC_DAPM_OUTPUT("WM1250 Output"),
68 };
69 
70 static const struct snd_soc_dapm_route wm1250_ev1_dapm_routes[] = {
71 	{ "ADC", NULL, "WM1250 Input" },
72 	{ "WM1250 Output", NULL, "DAC" },
73 };
74 
75 static int wm1250_ev1_hw_params(struct snd_pcm_substream *substream,
76 				struct snd_pcm_hw_params *params,
77 				struct snd_soc_dai *dai)
78 {
79 	struct wm1250_priv *wm1250 = snd_soc_component_get_drvdata(dai->component);
80 
81 	switch (params_rate(params)) {
82 	case 8000:
83 		gpio_set_value(wm1250->gpios[WM1250_EV1_GPIO_CLK_SEL0].gpio,
84 			       1);
85 		gpio_set_value(wm1250->gpios[WM1250_EV1_GPIO_CLK_SEL1].gpio,
86 			       1);
87 		break;
88 	case 16000:
89 		gpio_set_value(wm1250->gpios[WM1250_EV1_GPIO_CLK_SEL0].gpio,
90 			       0);
91 		gpio_set_value(wm1250->gpios[WM1250_EV1_GPIO_CLK_SEL1].gpio,
92 			       1);
93 		break;
94 	case 32000:
95 		gpio_set_value(wm1250->gpios[WM1250_EV1_GPIO_CLK_SEL0].gpio,
96 			       1);
97 		gpio_set_value(wm1250->gpios[WM1250_EV1_GPIO_CLK_SEL1].gpio,
98 			       0);
99 		break;
100 	case 64000:
101 		gpio_set_value(wm1250->gpios[WM1250_EV1_GPIO_CLK_SEL0].gpio,
102 			       0);
103 		gpio_set_value(wm1250->gpios[WM1250_EV1_GPIO_CLK_SEL1].gpio,
104 			       0);
105 		break;
106 	default:
107 		return -EINVAL;
108 	}
109 
110 	return 0;
111 }
112 
113 static const struct snd_soc_dai_ops wm1250_ev1_ops = {
114 	.hw_params = wm1250_ev1_hw_params,
115 };
116 
117 #define WM1250_EV1_RATES (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_16000 |\
118 			  SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_64000)
119 
120 static struct snd_soc_dai_driver wm1250_ev1_dai = {
121 	.name = "wm1250-ev1",
122 	.playback = {
123 		.stream_name = "Playback",
124 		.channels_min = 1,
125 		.channels_max = 2,
126 		.rates = WM1250_EV1_RATES,
127 		.formats = SNDRV_PCM_FMTBIT_S16_LE,
128 	},
129 	.capture = {
130 		.stream_name = "Capture",
131 		.channels_min = 1,
132 		.channels_max = 2,
133 		.rates = WM1250_EV1_RATES,
134 		.formats = SNDRV_PCM_FMTBIT_S16_LE,
135 	},
136 	.ops = &wm1250_ev1_ops,
137 };
138 
139 static const struct snd_soc_component_driver soc_component_dev_wm1250_ev1 = {
140 	.dapm_widgets		= wm1250_ev1_dapm_widgets,
141 	.num_dapm_widgets	= ARRAY_SIZE(wm1250_ev1_dapm_widgets),
142 	.dapm_routes		= wm1250_ev1_dapm_routes,
143 	.num_dapm_routes	= ARRAY_SIZE(wm1250_ev1_dapm_routes),
144 	.set_bias_level		= wm1250_ev1_set_bias_level,
145 	.use_pmdown_time	= 1,
146 	.endianness		= 1,
147 };
148 
149 static int wm1250_ev1_pdata(struct i2c_client *i2c)
150 {
151 	struct wm1250_ev1_pdata *pdata = dev_get_platdata(&i2c->dev);
152 	struct wm1250_priv *wm1250;
153 	int i, ret;
154 
155 	if (!pdata)
156 		return 0;
157 
158 	wm1250 = devm_kzalloc(&i2c->dev, sizeof(*wm1250), GFP_KERNEL);
159 	if (!wm1250) {
160 		ret = -ENOMEM;
161 		goto err;
162 	}
163 
164 	for (i = 0; i < ARRAY_SIZE(wm1250->gpios); i++) {
165 		wm1250->gpios[i].gpio = pdata->gpios[i];
166 		wm1250->gpios[i].label = wm1250_gpio_names[i];
167 		wm1250->gpios[i].flags = GPIOF_OUT_INIT_LOW;
168 	}
169 	wm1250->gpios[WM1250_EV1_GPIO_CLK_SEL0].flags = GPIOF_OUT_INIT_HIGH;
170 	wm1250->gpios[WM1250_EV1_GPIO_CLK_SEL1].flags = GPIOF_OUT_INIT_HIGH;
171 
172 	ret = gpio_request_array(wm1250->gpios, ARRAY_SIZE(wm1250->gpios));
173 	if (ret != 0) {
174 		dev_err(&i2c->dev, "Failed to get GPIOs: %d\n", ret);
175 		goto err;
176 	}
177 
178 	dev_set_drvdata(&i2c->dev, wm1250);
179 
180 	return ret;
181 
182 err:
183 	return ret;
184 }
185 
186 static void wm1250_ev1_free(struct i2c_client *i2c)
187 {
188 	struct wm1250_priv *wm1250 = dev_get_drvdata(&i2c->dev);
189 
190 	if (wm1250)
191 		gpio_free_array(wm1250->gpios, ARRAY_SIZE(wm1250->gpios));
192 }
193 
194 static int wm1250_ev1_probe(struct i2c_client *i2c)
195 {
196 	int id, board, rev, ret;
197 
198 	dev_set_drvdata(&i2c->dev, NULL);
199 
200 	board = i2c_smbus_read_byte_data(i2c, 0);
201 	if (board < 0) {
202 		dev_err(&i2c->dev, "Failed to read ID: %d\n", board);
203 		return board;
204 	}
205 
206 	id = (board & 0xfe) >> 2;
207 	rev = board & 0x3;
208 
209 	if (id != 1) {
210 		dev_err(&i2c->dev, "Unknown board ID %d\n", id);
211 		return -ENODEV;
212 	}
213 
214 	dev_info(&i2c->dev, "revision %d\n", rev + 1);
215 
216 	ret = wm1250_ev1_pdata(i2c);
217 	if (ret != 0)
218 		return ret;
219 
220 	ret = devm_snd_soc_register_component(&i2c->dev, &soc_component_dev_wm1250_ev1,
221 				     &wm1250_ev1_dai, 1);
222 	if (ret != 0) {
223 		dev_err(&i2c->dev, "Failed to register CODEC: %d\n", ret);
224 		wm1250_ev1_free(i2c);
225 		return ret;
226 	}
227 
228 	return 0;
229 }
230 
231 static int wm1250_ev1_remove(struct i2c_client *i2c)
232 {
233 	wm1250_ev1_free(i2c);
234 
235 	return 0;
236 }
237 
238 static const struct i2c_device_id wm1250_ev1_i2c_id[] = {
239 	{ "wm1250-ev1", 0 },
240 	{ }
241 };
242 MODULE_DEVICE_TABLE(i2c, wm1250_ev1_i2c_id);
243 
244 static struct i2c_driver wm1250_ev1_i2c_driver = {
245 	.driver = {
246 		.name = "wm1250-ev1",
247 	},
248 	.probe_new = wm1250_ev1_probe,
249 	.remove =   wm1250_ev1_remove,
250 	.id_table = wm1250_ev1_i2c_id,
251 };
252 
253 module_i2c_driver(wm1250_ev1_i2c_driver);
254 
255 MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
256 MODULE_DESCRIPTION("WM1250-EV1 audio I/O module driver");
257 MODULE_LICENSE("GPL");
258