xref: /openbmc/linux/sound/soc/codecs/wm1250-ev1.c (revision 39506cf83bb1b4df80dee1492f8cebc26cec355e)
14bb3f43cSMark Brown /*
24bb3f43cSMark Brown  * Driver for the 1250-EV1 audio I/O module
34bb3f43cSMark Brown  *
44bb3f43cSMark Brown  * Copyright 2011 Wolfson Microelectronics plc
54bb3f43cSMark Brown  *
64bb3f43cSMark Brown  *  This program is free software; you can redistribute  it and/or modify it
74bb3f43cSMark Brown  *  under  the terms of  the GNU General  Public License as published by the
84bb3f43cSMark Brown  *  Free Software Foundation;  either version 2 of the  License, or (at your
94bb3f43cSMark Brown  *  option) any later version.
104bb3f43cSMark Brown  *
114bb3f43cSMark Brown  */
124bb3f43cSMark Brown 
134bb3f43cSMark Brown #include <linux/init.h>
144bb3f43cSMark Brown #include <linux/module.h>
15213eb0fbSMark Brown #include <linux/slab.h>
164bb3f43cSMark Brown #include <linux/i2c.h>
17213eb0fbSMark Brown #include <linux/gpio.h>
184bb3f43cSMark Brown 
194bb3f43cSMark Brown #include <sound/soc.h>
204bb3f43cSMark Brown #include <sound/soc-dapm.h>
21213eb0fbSMark Brown #include <sound/wm1250-ev1.h>
22213eb0fbSMark Brown 
23213eb0fbSMark Brown static const char *wm1250_gpio_names[WM1250_EV1_NUM_GPIOS] = {
24213eb0fbSMark Brown 	"WM1250 CLK_ENA",
25213eb0fbSMark Brown 	"WM1250 CLK_SEL0",
26213eb0fbSMark Brown 	"WM1250 CLK_SEL1",
27213eb0fbSMark Brown 	"WM1250 OSR",
28213eb0fbSMark Brown 	"WM1250 MASTER",
29213eb0fbSMark Brown };
30213eb0fbSMark Brown 
31213eb0fbSMark Brown struct wm1250_priv {
32213eb0fbSMark Brown 	struct gpio gpios[WM1250_EV1_NUM_GPIOS];
33213eb0fbSMark Brown };
34213eb0fbSMark Brown 
35*39506cf8SKuninori Morimoto static int wm1250_ev1_set_bias_level(struct snd_soc_component *component,
36213eb0fbSMark Brown 				     enum snd_soc_bias_level level)
37213eb0fbSMark Brown {
38*39506cf8SKuninori Morimoto 	struct wm1250_priv *wm1250 = dev_get_drvdata(component->dev);
39213eb0fbSMark Brown 	int ena;
40213eb0fbSMark Brown 
41213eb0fbSMark Brown 	if (wm1250)
42213eb0fbSMark Brown 		ena = wm1250->gpios[WM1250_EV1_GPIO_CLK_ENA].gpio;
43213eb0fbSMark Brown 	else
44213eb0fbSMark Brown 		ena = -1;
45213eb0fbSMark Brown 
46213eb0fbSMark Brown 	switch (level) {
47213eb0fbSMark Brown 	case SND_SOC_BIAS_ON:
48213eb0fbSMark Brown 		break;
49213eb0fbSMark Brown 
50213eb0fbSMark Brown 	case SND_SOC_BIAS_PREPARE:
51213eb0fbSMark Brown 		break;
52213eb0fbSMark Brown 
53213eb0fbSMark Brown 	case SND_SOC_BIAS_STANDBY:
54213eb0fbSMark Brown 		if (ena >= 0)
55213eb0fbSMark Brown 			gpio_set_value_cansleep(ena, 1);
56213eb0fbSMark Brown 		break;
57213eb0fbSMark Brown 
58213eb0fbSMark Brown 	case SND_SOC_BIAS_OFF:
59213eb0fbSMark Brown 		if (ena >= 0)
60213eb0fbSMark Brown 			gpio_set_value_cansleep(ena, 0);
61213eb0fbSMark Brown 		break;
62213eb0fbSMark Brown 	}
63213eb0fbSMark Brown 
64213eb0fbSMark Brown 	return 0;
65213eb0fbSMark Brown }
664bb3f43cSMark Brown 
674bb3f43cSMark Brown static const struct snd_soc_dapm_widget wm1250_ev1_dapm_widgets[] = {
684bb3f43cSMark Brown SND_SOC_DAPM_ADC("ADC", "wm1250-ev1 Capture", SND_SOC_NOPM, 0, 0),
694bb3f43cSMark Brown SND_SOC_DAPM_DAC("DAC", "wm1250-ev1 Playback", SND_SOC_NOPM, 0, 0),
704bb3f43cSMark Brown 
714bb3f43cSMark Brown SND_SOC_DAPM_INPUT("WM1250 Input"),
72979f4869SAxel Lin SND_SOC_DAPM_OUTPUT("WM1250 Output"),
734bb3f43cSMark Brown };
744bb3f43cSMark Brown 
754bb3f43cSMark Brown static const struct snd_soc_dapm_route wm1250_ev1_dapm_routes[] = {
764bb3f43cSMark Brown 	{ "ADC", NULL, "WM1250 Input" },
774bb3f43cSMark Brown 	{ "WM1250 Output", NULL, "DAC" },
784bb3f43cSMark Brown };
794bb3f43cSMark Brown 
80fde39a6bSMark Brown static int wm1250_ev1_hw_params(struct snd_pcm_substream *substream,
81fde39a6bSMark Brown 				struct snd_pcm_hw_params *params,
82fde39a6bSMark Brown 				struct snd_soc_dai *dai)
83fde39a6bSMark Brown {
84*39506cf8SKuninori Morimoto 	struct wm1250_priv *wm1250 = snd_soc_component_get_drvdata(dai->component);
85fde39a6bSMark Brown 
86fde39a6bSMark Brown 	switch (params_rate(params)) {
87fde39a6bSMark Brown 	case 8000:
88fde39a6bSMark Brown 		gpio_set_value(wm1250->gpios[WM1250_EV1_GPIO_CLK_SEL0].gpio,
89fde39a6bSMark Brown 			       1);
90fde39a6bSMark Brown 		gpio_set_value(wm1250->gpios[WM1250_EV1_GPIO_CLK_SEL1].gpio,
91fde39a6bSMark Brown 			       1);
92fde39a6bSMark Brown 		break;
93fde39a6bSMark Brown 	case 16000:
94fde39a6bSMark Brown 		gpio_set_value(wm1250->gpios[WM1250_EV1_GPIO_CLK_SEL0].gpio,
95fde39a6bSMark Brown 			       0);
96fde39a6bSMark Brown 		gpio_set_value(wm1250->gpios[WM1250_EV1_GPIO_CLK_SEL1].gpio,
97fde39a6bSMark Brown 			       1);
98fde39a6bSMark Brown 		break;
99fde39a6bSMark Brown 	case 32000:
100fde39a6bSMark Brown 		gpio_set_value(wm1250->gpios[WM1250_EV1_GPIO_CLK_SEL0].gpio,
101fde39a6bSMark Brown 			       1);
102fde39a6bSMark Brown 		gpio_set_value(wm1250->gpios[WM1250_EV1_GPIO_CLK_SEL1].gpio,
103fde39a6bSMark Brown 			       0);
104fde39a6bSMark Brown 		break;
105fde39a6bSMark Brown 	case 64000:
106fde39a6bSMark Brown 		gpio_set_value(wm1250->gpios[WM1250_EV1_GPIO_CLK_SEL0].gpio,
107fde39a6bSMark Brown 			       0);
108fde39a6bSMark Brown 		gpio_set_value(wm1250->gpios[WM1250_EV1_GPIO_CLK_SEL1].gpio,
109fde39a6bSMark Brown 			       0);
110fde39a6bSMark Brown 		break;
111fde39a6bSMark Brown 	default:
112fde39a6bSMark Brown 		return -EINVAL;
113fde39a6bSMark Brown 	}
114fde39a6bSMark Brown 
115fde39a6bSMark Brown 	return 0;
116fde39a6bSMark Brown }
117fde39a6bSMark Brown 
118fde39a6bSMark Brown static const struct snd_soc_dai_ops wm1250_ev1_ops = {
119fde39a6bSMark Brown 	.hw_params = wm1250_ev1_hw_params,
120fde39a6bSMark Brown };
121fde39a6bSMark Brown 
122bcbf4a69SMark Brown #define WM1250_EV1_RATES (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_16000 |\
123bcbf4a69SMark Brown 			  SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_64000)
124bcbf4a69SMark Brown 
1254bb3f43cSMark Brown static struct snd_soc_dai_driver wm1250_ev1_dai = {
1264bb3f43cSMark Brown 	.name = "wm1250-ev1",
1274bb3f43cSMark Brown 	.playback = {
1284bb3f43cSMark Brown 		.stream_name = "Playback",
1294bb3f43cSMark Brown 		.channels_min = 1,
1305f6ac59fSMark Brown 		.channels_max = 2,
131bcbf4a69SMark Brown 		.rates = WM1250_EV1_RATES,
1324bb3f43cSMark Brown 		.formats = SNDRV_PCM_FMTBIT_S16_LE,
1334bb3f43cSMark Brown 	},
1344bb3f43cSMark Brown 	.capture = {
1354bb3f43cSMark Brown 		.stream_name = "Capture",
1364bb3f43cSMark Brown 		.channels_min = 1,
1375f6ac59fSMark Brown 		.channels_max = 2,
138bcbf4a69SMark Brown 		.rates = WM1250_EV1_RATES,
1394bb3f43cSMark Brown 		.formats = SNDRV_PCM_FMTBIT_S16_LE,
1404bb3f43cSMark Brown 	},
141fde39a6bSMark Brown 	.ops = &wm1250_ev1_ops,
1424bb3f43cSMark Brown };
1434bb3f43cSMark Brown 
144*39506cf8SKuninori Morimoto static const struct snd_soc_component_driver soc_component_dev_wm1250_ev1 = {
1454bb3f43cSMark Brown 	.dapm_widgets		= wm1250_ev1_dapm_widgets,
1464bb3f43cSMark Brown 	.num_dapm_widgets	= ARRAY_SIZE(wm1250_ev1_dapm_widgets),
1474bb3f43cSMark Brown 	.dapm_routes		= wm1250_ev1_dapm_routes,
1484bb3f43cSMark Brown 	.num_dapm_routes	= ARRAY_SIZE(wm1250_ev1_dapm_routes),
149213eb0fbSMark Brown 	.set_bias_level		= wm1250_ev1_set_bias_level,
150*39506cf8SKuninori Morimoto 	.use_pmdown_time	= 1,
151*39506cf8SKuninori Morimoto 	.endianness		= 1,
152*39506cf8SKuninori Morimoto 	.non_legacy_dai_naming	= 1,
1534bb3f43cSMark Brown };
1544bb3f43cSMark Brown 
1557a79e94eSBill Pemberton static int wm1250_ev1_pdata(struct i2c_client *i2c)
156213eb0fbSMark Brown {
157213eb0fbSMark Brown 	struct wm1250_ev1_pdata *pdata = dev_get_platdata(&i2c->dev);
158213eb0fbSMark Brown 	struct wm1250_priv *wm1250;
159213eb0fbSMark Brown 	int i, ret;
160213eb0fbSMark Brown 
161213eb0fbSMark Brown 	if (!pdata)
162213eb0fbSMark Brown 		return 0;
163213eb0fbSMark Brown 
1645fe803f5SMark Brown 	wm1250 = devm_kzalloc(&i2c->dev, sizeof(*wm1250), GFP_KERNEL);
165213eb0fbSMark Brown 	if (!wm1250) {
166213eb0fbSMark Brown 		ret = -ENOMEM;
167213eb0fbSMark Brown 		goto err;
168213eb0fbSMark Brown 	}
169213eb0fbSMark Brown 
170213eb0fbSMark Brown 	for (i = 0; i < ARRAY_SIZE(wm1250->gpios); i++) {
171213eb0fbSMark Brown 		wm1250->gpios[i].gpio = pdata->gpios[i];
172213eb0fbSMark Brown 		wm1250->gpios[i].label = wm1250_gpio_names[i];
173213eb0fbSMark Brown 		wm1250->gpios[i].flags = GPIOF_OUT_INIT_LOW;
174213eb0fbSMark Brown 	}
175213eb0fbSMark Brown 	wm1250->gpios[WM1250_EV1_GPIO_CLK_SEL0].flags = GPIOF_OUT_INIT_HIGH;
176213eb0fbSMark Brown 	wm1250->gpios[WM1250_EV1_GPIO_CLK_SEL1].flags = GPIOF_OUT_INIT_HIGH;
177213eb0fbSMark Brown 
178213eb0fbSMark Brown 	ret = gpio_request_array(wm1250->gpios, ARRAY_SIZE(wm1250->gpios));
179213eb0fbSMark Brown 	if (ret != 0) {
180213eb0fbSMark Brown 		dev_err(&i2c->dev, "Failed to get GPIOs: %d\n", ret);
1815fe803f5SMark Brown 		goto err;
182213eb0fbSMark Brown 	}
183213eb0fbSMark Brown 
184213eb0fbSMark Brown 	dev_set_drvdata(&i2c->dev, wm1250);
185213eb0fbSMark Brown 
186213eb0fbSMark Brown 	return ret;
187213eb0fbSMark Brown 
188213eb0fbSMark Brown err:
189213eb0fbSMark Brown 	return ret;
190213eb0fbSMark Brown }
191213eb0fbSMark Brown 
192213eb0fbSMark Brown static void wm1250_ev1_free(struct i2c_client *i2c)
193213eb0fbSMark Brown {
194213eb0fbSMark Brown 	struct wm1250_priv *wm1250 = dev_get_drvdata(&i2c->dev);
195213eb0fbSMark Brown 
1965fe803f5SMark Brown 	if (wm1250)
197213eb0fbSMark Brown 		gpio_free_array(wm1250->gpios, ARRAY_SIZE(wm1250->gpios));
198213eb0fbSMark Brown }
199213eb0fbSMark Brown 
2007a79e94eSBill Pemberton static int wm1250_ev1_probe(struct i2c_client *i2c,
201eaefb38fSMark Brown 			    const struct i2c_device_id *i2c_id)
2024bb3f43cSMark Brown {
203213eb0fbSMark Brown 	int id, board, rev, ret;
204213eb0fbSMark Brown 
205213eb0fbSMark Brown 	dev_set_drvdata(&i2c->dev, NULL);
206eaefb38fSMark Brown 
207eaefb38fSMark Brown 	board = i2c_smbus_read_byte_data(i2c, 0);
208eaefb38fSMark Brown 	if (board < 0) {
209c38071c0SMark Brown 		dev_err(&i2c->dev, "Failed to read ID: %d\n", board);
210c38071c0SMark Brown 		return board;
211eaefb38fSMark Brown 	}
212eaefb38fSMark Brown 
213eaefb38fSMark Brown 	id = (board & 0xfe) >> 2;
214eaefb38fSMark Brown 	rev = board & 0x3;
215eaefb38fSMark Brown 
216eaefb38fSMark Brown 	if (id != 1) {
217eaefb38fSMark Brown 		dev_err(&i2c->dev, "Unknown board ID %d\n", id);
218eaefb38fSMark Brown 		return -ENODEV;
219eaefb38fSMark Brown 	}
220eaefb38fSMark Brown 
2215c58b739SMark Brown 	dev_info(&i2c->dev, "revision %d\n", rev + 1);
222eaefb38fSMark Brown 
223213eb0fbSMark Brown 	ret = wm1250_ev1_pdata(i2c);
224213eb0fbSMark Brown 	if (ret != 0)
225213eb0fbSMark Brown 		return ret;
226213eb0fbSMark Brown 
227*39506cf8SKuninori Morimoto 	ret = devm_snd_soc_register_component(&i2c->dev, &soc_component_dev_wm1250_ev1,
2284bb3f43cSMark Brown 				     &wm1250_ev1_dai, 1);
229213eb0fbSMark Brown 	if (ret != 0) {
230213eb0fbSMark Brown 		dev_err(&i2c->dev, "Failed to register CODEC: %d\n", ret);
231213eb0fbSMark Brown 		wm1250_ev1_free(i2c);
232213eb0fbSMark Brown 		return ret;
233213eb0fbSMark Brown 	}
234213eb0fbSMark Brown 
235213eb0fbSMark Brown 	return 0;
2364bb3f43cSMark Brown }
2374bb3f43cSMark Brown 
2387a79e94eSBill Pemberton static int wm1250_ev1_remove(struct i2c_client *i2c)
2394bb3f43cSMark Brown {
240213eb0fbSMark Brown 	wm1250_ev1_free(i2c);
2414bb3f43cSMark Brown 
2424bb3f43cSMark Brown 	return 0;
2434bb3f43cSMark Brown }
2444bb3f43cSMark Brown 
2454bb3f43cSMark Brown static const struct i2c_device_id wm1250_ev1_i2c_id[] = {
2464bb3f43cSMark Brown 	{ "wm1250-ev1", 0 },
2474bb3f43cSMark Brown 	{ }
2484bb3f43cSMark Brown };
249420dd718SMark Brown MODULE_DEVICE_TABLE(i2c, wm1250_ev1_i2c_id);
2504bb3f43cSMark Brown 
2514bb3f43cSMark Brown static struct i2c_driver wm1250_ev1_i2c_driver = {
2524bb3f43cSMark Brown 	.driver = {
2534bb3f43cSMark Brown 		.name = "wm1250-ev1",
2544bb3f43cSMark Brown 	},
2554bb3f43cSMark Brown 	.probe =    wm1250_ev1_probe,
2567a79e94eSBill Pemberton 	.remove =   wm1250_ev1_remove,
2574bb3f43cSMark Brown 	.id_table = wm1250_ev1_i2c_id,
2584bb3f43cSMark Brown };
2594bb3f43cSMark Brown 
260bbdd3915SMark Brown module_i2c_driver(wm1250_ev1_i2c_driver);
2614bb3f43cSMark Brown 
2624bb3f43cSMark Brown MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
2634bb3f43cSMark Brown MODULE_DESCRIPTION("WM1250-EV1 audio I/O module driver");
2644bb3f43cSMark Brown MODULE_LICENSE("GPL");
265