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