12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later 24bb3f43cSMark Brown /* 34bb3f43cSMark Brown * Driver for the 1250-EV1 audio I/O module 44bb3f43cSMark Brown * 54bb3f43cSMark Brown * Copyright 2011 Wolfson Microelectronics plc 64bb3f43cSMark Brown */ 74bb3f43cSMark Brown 84bb3f43cSMark Brown #include <linux/init.h> 94bb3f43cSMark Brown #include <linux/module.h> 10213eb0fbSMark Brown #include <linux/slab.h> 114bb3f43cSMark Brown #include <linux/i2c.h> 12213eb0fbSMark Brown #include <linux/gpio.h> 134bb3f43cSMark Brown 144bb3f43cSMark Brown #include <sound/soc.h> 154bb3f43cSMark Brown #include <sound/soc-dapm.h> 16213eb0fbSMark Brown #include <sound/wm1250-ev1.h> 17213eb0fbSMark Brown 18213eb0fbSMark Brown static const char *wm1250_gpio_names[WM1250_EV1_NUM_GPIOS] = { 19213eb0fbSMark Brown "WM1250 CLK_ENA", 20213eb0fbSMark Brown "WM1250 CLK_SEL0", 21213eb0fbSMark Brown "WM1250 CLK_SEL1", 22213eb0fbSMark Brown "WM1250 OSR", 23213eb0fbSMark Brown "WM1250 MASTER", 24213eb0fbSMark Brown }; 25213eb0fbSMark Brown 26213eb0fbSMark Brown struct wm1250_priv { 27213eb0fbSMark Brown struct gpio gpios[WM1250_EV1_NUM_GPIOS]; 28213eb0fbSMark Brown }; 29213eb0fbSMark Brown 3039506cf8SKuninori Morimoto static int wm1250_ev1_set_bias_level(struct snd_soc_component *component, 31213eb0fbSMark Brown enum snd_soc_bias_level level) 32213eb0fbSMark Brown { 3339506cf8SKuninori Morimoto struct wm1250_priv *wm1250 = dev_get_drvdata(component->dev); 34213eb0fbSMark Brown int ena; 35213eb0fbSMark Brown 36213eb0fbSMark Brown if (wm1250) 37213eb0fbSMark Brown ena = wm1250->gpios[WM1250_EV1_GPIO_CLK_ENA].gpio; 38213eb0fbSMark Brown else 39213eb0fbSMark Brown ena = -1; 40213eb0fbSMark Brown 41213eb0fbSMark Brown switch (level) { 42213eb0fbSMark Brown case SND_SOC_BIAS_ON: 43213eb0fbSMark Brown break; 44213eb0fbSMark Brown 45213eb0fbSMark Brown case SND_SOC_BIAS_PREPARE: 46213eb0fbSMark Brown break; 47213eb0fbSMark Brown 48213eb0fbSMark Brown case SND_SOC_BIAS_STANDBY: 49213eb0fbSMark Brown if (ena >= 0) 50213eb0fbSMark Brown gpio_set_value_cansleep(ena, 1); 51213eb0fbSMark Brown break; 52213eb0fbSMark Brown 53213eb0fbSMark Brown case SND_SOC_BIAS_OFF: 54213eb0fbSMark Brown if (ena >= 0) 55213eb0fbSMark Brown gpio_set_value_cansleep(ena, 0); 56213eb0fbSMark Brown break; 57213eb0fbSMark Brown } 58213eb0fbSMark Brown 59213eb0fbSMark Brown return 0; 60213eb0fbSMark Brown } 614bb3f43cSMark Brown 624bb3f43cSMark Brown static const struct snd_soc_dapm_widget wm1250_ev1_dapm_widgets[] = { 634bb3f43cSMark Brown SND_SOC_DAPM_ADC("ADC", "wm1250-ev1 Capture", SND_SOC_NOPM, 0, 0), 644bb3f43cSMark Brown SND_SOC_DAPM_DAC("DAC", "wm1250-ev1 Playback", SND_SOC_NOPM, 0, 0), 654bb3f43cSMark Brown 664bb3f43cSMark Brown SND_SOC_DAPM_INPUT("WM1250 Input"), 67979f4869SAxel Lin SND_SOC_DAPM_OUTPUT("WM1250 Output"), 684bb3f43cSMark Brown }; 694bb3f43cSMark Brown 704bb3f43cSMark Brown static const struct snd_soc_dapm_route wm1250_ev1_dapm_routes[] = { 714bb3f43cSMark Brown { "ADC", NULL, "WM1250 Input" }, 724bb3f43cSMark Brown { "WM1250 Output", NULL, "DAC" }, 734bb3f43cSMark Brown }; 744bb3f43cSMark Brown 75fde39a6bSMark Brown static int wm1250_ev1_hw_params(struct snd_pcm_substream *substream, 76fde39a6bSMark Brown struct snd_pcm_hw_params *params, 77fde39a6bSMark Brown struct snd_soc_dai *dai) 78fde39a6bSMark Brown { 7939506cf8SKuninori Morimoto struct wm1250_priv *wm1250 = snd_soc_component_get_drvdata(dai->component); 80fde39a6bSMark Brown 81fde39a6bSMark Brown switch (params_rate(params)) { 82fde39a6bSMark Brown case 8000: 83fde39a6bSMark Brown gpio_set_value(wm1250->gpios[WM1250_EV1_GPIO_CLK_SEL0].gpio, 84fde39a6bSMark Brown 1); 85fde39a6bSMark Brown gpio_set_value(wm1250->gpios[WM1250_EV1_GPIO_CLK_SEL1].gpio, 86fde39a6bSMark Brown 1); 87fde39a6bSMark Brown break; 88fde39a6bSMark Brown case 16000: 89fde39a6bSMark Brown gpio_set_value(wm1250->gpios[WM1250_EV1_GPIO_CLK_SEL0].gpio, 90fde39a6bSMark Brown 0); 91fde39a6bSMark Brown gpio_set_value(wm1250->gpios[WM1250_EV1_GPIO_CLK_SEL1].gpio, 92fde39a6bSMark Brown 1); 93fde39a6bSMark Brown break; 94fde39a6bSMark Brown case 32000: 95fde39a6bSMark Brown gpio_set_value(wm1250->gpios[WM1250_EV1_GPIO_CLK_SEL0].gpio, 96fde39a6bSMark Brown 1); 97fde39a6bSMark Brown gpio_set_value(wm1250->gpios[WM1250_EV1_GPIO_CLK_SEL1].gpio, 98fde39a6bSMark Brown 0); 99fde39a6bSMark Brown break; 100fde39a6bSMark Brown case 64000: 101fde39a6bSMark Brown gpio_set_value(wm1250->gpios[WM1250_EV1_GPIO_CLK_SEL0].gpio, 102fde39a6bSMark Brown 0); 103fde39a6bSMark Brown gpio_set_value(wm1250->gpios[WM1250_EV1_GPIO_CLK_SEL1].gpio, 104fde39a6bSMark Brown 0); 105fde39a6bSMark Brown break; 106fde39a6bSMark Brown default: 107fde39a6bSMark Brown return -EINVAL; 108fde39a6bSMark Brown } 109fde39a6bSMark Brown 110fde39a6bSMark Brown return 0; 111fde39a6bSMark Brown } 112fde39a6bSMark Brown 113fde39a6bSMark Brown static const struct snd_soc_dai_ops wm1250_ev1_ops = { 114fde39a6bSMark Brown .hw_params = wm1250_ev1_hw_params, 115fde39a6bSMark Brown }; 116fde39a6bSMark Brown 117bcbf4a69SMark Brown #define WM1250_EV1_RATES (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_16000 |\ 118bcbf4a69SMark Brown SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_64000) 119bcbf4a69SMark Brown 1204bb3f43cSMark Brown static struct snd_soc_dai_driver wm1250_ev1_dai = { 1214bb3f43cSMark Brown .name = "wm1250-ev1", 1224bb3f43cSMark Brown .playback = { 1234bb3f43cSMark Brown .stream_name = "Playback", 1244bb3f43cSMark Brown .channels_min = 1, 1255f6ac59fSMark Brown .channels_max = 2, 126bcbf4a69SMark Brown .rates = WM1250_EV1_RATES, 1274bb3f43cSMark Brown .formats = SNDRV_PCM_FMTBIT_S16_LE, 1284bb3f43cSMark Brown }, 1294bb3f43cSMark Brown .capture = { 1304bb3f43cSMark Brown .stream_name = "Capture", 1314bb3f43cSMark Brown .channels_min = 1, 1325f6ac59fSMark Brown .channels_max = 2, 133bcbf4a69SMark Brown .rates = WM1250_EV1_RATES, 1344bb3f43cSMark Brown .formats = SNDRV_PCM_FMTBIT_S16_LE, 1354bb3f43cSMark Brown }, 136fde39a6bSMark Brown .ops = &wm1250_ev1_ops, 1374bb3f43cSMark Brown }; 1384bb3f43cSMark Brown 13939506cf8SKuninori Morimoto static const struct snd_soc_component_driver soc_component_dev_wm1250_ev1 = { 1404bb3f43cSMark Brown .dapm_widgets = wm1250_ev1_dapm_widgets, 1414bb3f43cSMark Brown .num_dapm_widgets = ARRAY_SIZE(wm1250_ev1_dapm_widgets), 1424bb3f43cSMark Brown .dapm_routes = wm1250_ev1_dapm_routes, 1434bb3f43cSMark Brown .num_dapm_routes = ARRAY_SIZE(wm1250_ev1_dapm_routes), 144213eb0fbSMark Brown .set_bias_level = wm1250_ev1_set_bias_level, 14539506cf8SKuninori Morimoto .use_pmdown_time = 1, 14639506cf8SKuninori Morimoto .endianness = 1, 1474bb3f43cSMark Brown }; 1484bb3f43cSMark Brown 1497a79e94eSBill Pemberton static int wm1250_ev1_pdata(struct i2c_client *i2c) 150213eb0fbSMark Brown { 151213eb0fbSMark Brown struct wm1250_ev1_pdata *pdata = dev_get_platdata(&i2c->dev); 152213eb0fbSMark Brown struct wm1250_priv *wm1250; 153213eb0fbSMark Brown int i, ret; 154213eb0fbSMark Brown 155213eb0fbSMark Brown if (!pdata) 156213eb0fbSMark Brown return 0; 157213eb0fbSMark Brown 1585fe803f5SMark Brown wm1250 = devm_kzalloc(&i2c->dev, sizeof(*wm1250), GFP_KERNEL); 159213eb0fbSMark Brown if (!wm1250) { 160213eb0fbSMark Brown ret = -ENOMEM; 161213eb0fbSMark Brown goto err; 162213eb0fbSMark Brown } 163213eb0fbSMark Brown 164213eb0fbSMark Brown for (i = 0; i < ARRAY_SIZE(wm1250->gpios); i++) { 165213eb0fbSMark Brown wm1250->gpios[i].gpio = pdata->gpios[i]; 166213eb0fbSMark Brown wm1250->gpios[i].label = wm1250_gpio_names[i]; 167213eb0fbSMark Brown wm1250->gpios[i].flags = GPIOF_OUT_INIT_LOW; 168213eb0fbSMark Brown } 169213eb0fbSMark Brown wm1250->gpios[WM1250_EV1_GPIO_CLK_SEL0].flags = GPIOF_OUT_INIT_HIGH; 170213eb0fbSMark Brown wm1250->gpios[WM1250_EV1_GPIO_CLK_SEL1].flags = GPIOF_OUT_INIT_HIGH; 171213eb0fbSMark Brown 172213eb0fbSMark Brown ret = gpio_request_array(wm1250->gpios, ARRAY_SIZE(wm1250->gpios)); 173213eb0fbSMark Brown if (ret != 0) { 174213eb0fbSMark Brown dev_err(&i2c->dev, "Failed to get GPIOs: %d\n", ret); 1755fe803f5SMark Brown goto err; 176213eb0fbSMark Brown } 177213eb0fbSMark Brown 178213eb0fbSMark Brown dev_set_drvdata(&i2c->dev, wm1250); 179213eb0fbSMark Brown 180213eb0fbSMark Brown return ret; 181213eb0fbSMark Brown 182213eb0fbSMark Brown err: 183213eb0fbSMark Brown return ret; 184213eb0fbSMark Brown } 185213eb0fbSMark Brown 186213eb0fbSMark Brown static void wm1250_ev1_free(struct i2c_client *i2c) 187213eb0fbSMark Brown { 188213eb0fbSMark Brown struct wm1250_priv *wm1250 = dev_get_drvdata(&i2c->dev); 189213eb0fbSMark Brown 1905fe803f5SMark Brown if (wm1250) 191213eb0fbSMark Brown gpio_free_array(wm1250->gpios, ARRAY_SIZE(wm1250->gpios)); 192213eb0fbSMark Brown } 193213eb0fbSMark Brown 19497b0b6e3SStephen Kitt static int wm1250_ev1_probe(struct i2c_client *i2c) 1954bb3f43cSMark Brown { 196213eb0fbSMark Brown int id, board, rev, ret; 197213eb0fbSMark Brown 198213eb0fbSMark Brown dev_set_drvdata(&i2c->dev, NULL); 199eaefb38fSMark Brown 200eaefb38fSMark Brown board = i2c_smbus_read_byte_data(i2c, 0); 201eaefb38fSMark Brown if (board < 0) { 202c38071c0SMark Brown dev_err(&i2c->dev, "Failed to read ID: %d\n", board); 203c38071c0SMark Brown return board; 204eaefb38fSMark Brown } 205eaefb38fSMark Brown 206eaefb38fSMark Brown id = (board & 0xfe) >> 2; 207eaefb38fSMark Brown rev = board & 0x3; 208eaefb38fSMark Brown 209eaefb38fSMark Brown if (id != 1) { 210eaefb38fSMark Brown dev_err(&i2c->dev, "Unknown board ID %d\n", id); 211eaefb38fSMark Brown return -ENODEV; 212eaefb38fSMark Brown } 213eaefb38fSMark Brown 2145c58b739SMark Brown dev_info(&i2c->dev, "revision %d\n", rev + 1); 215eaefb38fSMark Brown 216213eb0fbSMark Brown ret = wm1250_ev1_pdata(i2c); 217213eb0fbSMark Brown if (ret != 0) 218213eb0fbSMark Brown return ret; 219213eb0fbSMark Brown 22039506cf8SKuninori Morimoto ret = devm_snd_soc_register_component(&i2c->dev, &soc_component_dev_wm1250_ev1, 2214bb3f43cSMark Brown &wm1250_ev1_dai, 1); 222213eb0fbSMark Brown if (ret != 0) { 223213eb0fbSMark Brown dev_err(&i2c->dev, "Failed to register CODEC: %d\n", ret); 224213eb0fbSMark Brown wm1250_ev1_free(i2c); 225213eb0fbSMark Brown return ret; 226213eb0fbSMark Brown } 227213eb0fbSMark Brown 228213eb0fbSMark Brown return 0; 2294bb3f43cSMark Brown } 2304bb3f43cSMark Brown 231ed5c2f5fSUwe Kleine-König static void wm1250_ev1_remove(struct i2c_client *i2c) 2324bb3f43cSMark Brown { 233213eb0fbSMark Brown wm1250_ev1_free(i2c); 2344bb3f43cSMark Brown } 2354bb3f43cSMark Brown 2364bb3f43cSMark Brown static const struct i2c_device_id wm1250_ev1_i2c_id[] = { 2374bb3f43cSMark Brown { "wm1250-ev1", 0 }, 2384bb3f43cSMark Brown { } 2394bb3f43cSMark Brown }; 240420dd718SMark Brown MODULE_DEVICE_TABLE(i2c, wm1250_ev1_i2c_id); 2414bb3f43cSMark Brown 2424bb3f43cSMark Brown static struct i2c_driver wm1250_ev1_i2c_driver = { 2434bb3f43cSMark Brown .driver = { 2444bb3f43cSMark Brown .name = "wm1250-ev1", 2454bb3f43cSMark Brown }, 246*9abcd240SUwe Kleine-König .probe = wm1250_ev1_probe, 2477a79e94eSBill Pemberton .remove = wm1250_ev1_remove, 2484bb3f43cSMark Brown .id_table = wm1250_ev1_i2c_id, 2494bb3f43cSMark Brown }; 2504bb3f43cSMark Brown 251bbdd3915SMark Brown module_i2c_driver(wm1250_ev1_i2c_driver); 2524bb3f43cSMark Brown 2534bb3f43cSMark Brown MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>"); 2544bb3f43cSMark Brown MODULE_DESCRIPTION("WM1250-EV1 audio I/O module driver"); 2554bb3f43cSMark Brown MODULE_LICENSE("GPL"); 256