Mark Brown | 4bb3f43 | 2011-04-08 16:49:42 +0900 | [diff] [blame] | 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> |
Mark Brown | 213eb0f | 2011-09-21 20:54:47 +0100 | [diff] [blame] | 15 | #include <linux/slab.h> |
Mark Brown | 4bb3f43 | 2011-04-08 16:49:42 +0900 | [diff] [blame] | 16 | #include <linux/i2c.h> |
Mark Brown | 213eb0f | 2011-09-21 20:54:47 +0100 | [diff] [blame] | 17 | #include <linux/gpio.h> |
Mark Brown | 4bb3f43 | 2011-04-08 16:49:42 +0900 | [diff] [blame] | 18 | |
| 19 | #include <sound/soc.h> |
| 20 | #include <sound/soc-dapm.h> |
Mark Brown | 213eb0f | 2011-09-21 20:54:47 +0100 | [diff] [blame] | 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_codec *codec, |
| 36 | enum snd_soc_bias_level level) |
| 37 | { |
| 38 | struct wm1250_priv *wm1250 = dev_get_drvdata(codec->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 | codec->dapm.bias_level = level; |
| 65 | |
| 66 | return 0; |
| 67 | } |
Mark Brown | 4bb3f43 | 2011-04-08 16:49:42 +0900 | [diff] [blame] | 68 | |
| 69 | static const struct snd_soc_dapm_widget wm1250_ev1_dapm_widgets[] = { |
| 70 | SND_SOC_DAPM_ADC("ADC", "wm1250-ev1 Capture", SND_SOC_NOPM, 0, 0), |
| 71 | SND_SOC_DAPM_DAC("DAC", "wm1250-ev1 Playback", SND_SOC_NOPM, 0, 0), |
| 72 | |
| 73 | SND_SOC_DAPM_INPUT("WM1250 Input"), |
Axel Lin | 979f486 | 2011-05-26 10:54:12 +0800 | [diff] [blame] | 74 | SND_SOC_DAPM_OUTPUT("WM1250 Output"), |
Mark Brown | 4bb3f43 | 2011-04-08 16:49:42 +0900 | [diff] [blame] | 75 | }; |
| 76 | |
| 77 | static const struct snd_soc_dapm_route wm1250_ev1_dapm_routes[] = { |
| 78 | { "ADC", NULL, "WM1250 Input" }, |
| 79 | { "WM1250 Output", NULL, "DAC" }, |
| 80 | }; |
| 81 | |
Mark Brown | fde39a6 | 2012-04-09 19:40:00 +0100 | [diff] [blame] | 82 | static int wm1250_ev1_hw_params(struct snd_pcm_substream *substream, |
| 83 | struct snd_pcm_hw_params *params, |
| 84 | struct snd_soc_dai *dai) |
| 85 | { |
| 86 | struct wm1250_priv *wm1250 = snd_soc_codec_get_drvdata(dai->codec); |
| 87 | |
| 88 | switch (params_rate(params)) { |
| 89 | case 8000: |
| 90 | gpio_set_value(wm1250->gpios[WM1250_EV1_GPIO_CLK_SEL0].gpio, |
| 91 | 1); |
| 92 | gpio_set_value(wm1250->gpios[WM1250_EV1_GPIO_CLK_SEL1].gpio, |
| 93 | 1); |
| 94 | break; |
| 95 | case 16000: |
| 96 | gpio_set_value(wm1250->gpios[WM1250_EV1_GPIO_CLK_SEL0].gpio, |
| 97 | 0); |
| 98 | gpio_set_value(wm1250->gpios[WM1250_EV1_GPIO_CLK_SEL1].gpio, |
| 99 | 1); |
| 100 | break; |
| 101 | case 32000: |
| 102 | gpio_set_value(wm1250->gpios[WM1250_EV1_GPIO_CLK_SEL0].gpio, |
| 103 | 1); |
| 104 | gpio_set_value(wm1250->gpios[WM1250_EV1_GPIO_CLK_SEL1].gpio, |
| 105 | 0); |
| 106 | break; |
| 107 | case 64000: |
| 108 | gpio_set_value(wm1250->gpios[WM1250_EV1_GPIO_CLK_SEL0].gpio, |
| 109 | 0); |
| 110 | gpio_set_value(wm1250->gpios[WM1250_EV1_GPIO_CLK_SEL1].gpio, |
| 111 | 0); |
| 112 | break; |
| 113 | default: |
| 114 | return -EINVAL; |
| 115 | } |
| 116 | |
| 117 | return 0; |
| 118 | } |
| 119 | |
| 120 | static const struct snd_soc_dai_ops wm1250_ev1_ops = { |
| 121 | .hw_params = wm1250_ev1_hw_params, |
| 122 | }; |
| 123 | |
Mark Brown | bcbf4a6 | 2012-07-05 14:30:59 +0100 | [diff] [blame^] | 124 | #define WM1250_EV1_RATES (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_16000 |\ |
| 125 | SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_64000) |
| 126 | |
Mark Brown | 4bb3f43 | 2011-04-08 16:49:42 +0900 | [diff] [blame] | 127 | static struct snd_soc_dai_driver wm1250_ev1_dai = { |
| 128 | .name = "wm1250-ev1", |
| 129 | .playback = { |
| 130 | .stream_name = "Playback", |
| 131 | .channels_min = 1, |
Mark Brown | 5f6ac59 | 2012-04-09 19:38:31 +0100 | [diff] [blame] | 132 | .channels_max = 2, |
Mark Brown | bcbf4a6 | 2012-07-05 14:30:59 +0100 | [diff] [blame^] | 133 | .rates = WM1250_EV1_RATES, |
Mark Brown | 4bb3f43 | 2011-04-08 16:49:42 +0900 | [diff] [blame] | 134 | .formats = SNDRV_PCM_FMTBIT_S16_LE, |
| 135 | }, |
| 136 | .capture = { |
| 137 | .stream_name = "Capture", |
| 138 | .channels_min = 1, |
Mark Brown | 5f6ac59 | 2012-04-09 19:38:31 +0100 | [diff] [blame] | 139 | .channels_max = 2, |
Mark Brown | bcbf4a6 | 2012-07-05 14:30:59 +0100 | [diff] [blame^] | 140 | .rates = WM1250_EV1_RATES, |
Mark Brown | 4bb3f43 | 2011-04-08 16:49:42 +0900 | [diff] [blame] | 141 | .formats = SNDRV_PCM_FMTBIT_S16_LE, |
| 142 | }, |
Mark Brown | fde39a6 | 2012-04-09 19:40:00 +0100 | [diff] [blame] | 143 | .ops = &wm1250_ev1_ops, |
Mark Brown | 4bb3f43 | 2011-04-08 16:49:42 +0900 | [diff] [blame] | 144 | }; |
| 145 | |
| 146 | static struct snd_soc_codec_driver soc_codec_dev_wm1250_ev1 = { |
| 147 | .dapm_widgets = wm1250_ev1_dapm_widgets, |
| 148 | .num_dapm_widgets = ARRAY_SIZE(wm1250_ev1_dapm_widgets), |
| 149 | .dapm_routes = wm1250_ev1_dapm_routes, |
| 150 | .num_dapm_routes = ARRAY_SIZE(wm1250_ev1_dapm_routes), |
Mark Brown | 213eb0f | 2011-09-21 20:54:47 +0100 | [diff] [blame] | 151 | |
| 152 | .set_bias_level = wm1250_ev1_set_bias_level, |
Mark Brown | a850260 | 2011-09-21 21:33:40 +0100 | [diff] [blame] | 153 | .idle_bias_off = true, |
Mark Brown | 4bb3f43 | 2011-04-08 16:49:42 +0900 | [diff] [blame] | 154 | }; |
| 155 | |
Mark Brown | 213eb0f | 2011-09-21 20:54:47 +0100 | [diff] [blame] | 156 | static int __devinit wm1250_ev1_pdata(struct i2c_client *i2c) |
| 157 | { |
| 158 | struct wm1250_ev1_pdata *pdata = dev_get_platdata(&i2c->dev); |
| 159 | struct wm1250_priv *wm1250; |
| 160 | int i, ret; |
| 161 | |
| 162 | if (!pdata) |
| 163 | return 0; |
| 164 | |
Mark Brown | 5fe803f | 2011-11-27 15:56:55 +0000 | [diff] [blame] | 165 | wm1250 = devm_kzalloc(&i2c->dev, sizeof(*wm1250), GFP_KERNEL); |
Mark Brown | 213eb0f | 2011-09-21 20:54:47 +0100 | [diff] [blame] | 166 | if (!wm1250) { |
| 167 | dev_err(&i2c->dev, "Unable to allocate private data\n"); |
| 168 | ret = -ENOMEM; |
| 169 | goto err; |
| 170 | } |
| 171 | |
| 172 | for (i = 0; i < ARRAY_SIZE(wm1250->gpios); i++) { |
| 173 | wm1250->gpios[i].gpio = pdata->gpios[i]; |
| 174 | wm1250->gpios[i].label = wm1250_gpio_names[i]; |
| 175 | wm1250->gpios[i].flags = GPIOF_OUT_INIT_LOW; |
| 176 | } |
| 177 | wm1250->gpios[WM1250_EV1_GPIO_CLK_SEL0].flags = GPIOF_OUT_INIT_HIGH; |
| 178 | wm1250->gpios[WM1250_EV1_GPIO_CLK_SEL1].flags = GPIOF_OUT_INIT_HIGH; |
| 179 | |
| 180 | ret = gpio_request_array(wm1250->gpios, ARRAY_SIZE(wm1250->gpios)); |
| 181 | if (ret != 0) { |
| 182 | dev_err(&i2c->dev, "Failed to get GPIOs: %d\n", ret); |
Mark Brown | 5fe803f | 2011-11-27 15:56:55 +0000 | [diff] [blame] | 183 | goto err; |
Mark Brown | 213eb0f | 2011-09-21 20:54:47 +0100 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | dev_set_drvdata(&i2c->dev, wm1250); |
| 187 | |
| 188 | return ret; |
| 189 | |
Mark Brown | 213eb0f | 2011-09-21 20:54:47 +0100 | [diff] [blame] | 190 | err: |
| 191 | return ret; |
| 192 | } |
| 193 | |
| 194 | static void wm1250_ev1_free(struct i2c_client *i2c) |
| 195 | { |
| 196 | struct wm1250_priv *wm1250 = dev_get_drvdata(&i2c->dev); |
| 197 | |
Mark Brown | 5fe803f | 2011-11-27 15:56:55 +0000 | [diff] [blame] | 198 | if (wm1250) |
Mark Brown | 213eb0f | 2011-09-21 20:54:47 +0100 | [diff] [blame] | 199 | gpio_free_array(wm1250->gpios, ARRAY_SIZE(wm1250->gpios)); |
Mark Brown | 213eb0f | 2011-09-21 20:54:47 +0100 | [diff] [blame] | 200 | } |
| 201 | |
Mark Brown | 4bb3f43 | 2011-04-08 16:49:42 +0900 | [diff] [blame] | 202 | static int __devinit wm1250_ev1_probe(struct i2c_client *i2c, |
Mark Brown | eaefb38 | 2011-07-29 16:27:18 +0100 | [diff] [blame] | 203 | const struct i2c_device_id *i2c_id) |
Mark Brown | 4bb3f43 | 2011-04-08 16:49:42 +0900 | [diff] [blame] | 204 | { |
Mark Brown | 213eb0f | 2011-09-21 20:54:47 +0100 | [diff] [blame] | 205 | int id, board, rev, ret; |
| 206 | |
| 207 | dev_set_drvdata(&i2c->dev, NULL); |
Mark Brown | eaefb38 | 2011-07-29 16:27:18 +0100 | [diff] [blame] | 208 | |
| 209 | board = i2c_smbus_read_byte_data(i2c, 0); |
| 210 | if (board < 0) { |
Mark Brown | c38071c | 2011-07-29 16:27:18 +0100 | [diff] [blame] | 211 | dev_err(&i2c->dev, "Failed to read ID: %d\n", board); |
| 212 | return board; |
Mark Brown | eaefb38 | 2011-07-29 16:27:18 +0100 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | id = (board & 0xfe) >> 2; |
| 216 | rev = board & 0x3; |
| 217 | |
| 218 | if (id != 1) { |
| 219 | dev_err(&i2c->dev, "Unknown board ID %d\n", id); |
| 220 | return -ENODEV; |
| 221 | } |
| 222 | |
Mark Brown | 5c58b73 | 2011-08-14 19:07:33 +0900 | [diff] [blame] | 223 | dev_info(&i2c->dev, "revision %d\n", rev + 1); |
Mark Brown | eaefb38 | 2011-07-29 16:27:18 +0100 | [diff] [blame] | 224 | |
Mark Brown | 213eb0f | 2011-09-21 20:54:47 +0100 | [diff] [blame] | 225 | ret = wm1250_ev1_pdata(i2c); |
| 226 | if (ret != 0) |
| 227 | return ret; |
| 228 | |
| 229 | ret = snd_soc_register_codec(&i2c->dev, &soc_codec_dev_wm1250_ev1, |
| 230 | &wm1250_ev1_dai, 1); |
| 231 | if (ret != 0) { |
| 232 | dev_err(&i2c->dev, "Failed to register CODEC: %d\n", ret); |
| 233 | wm1250_ev1_free(i2c); |
| 234 | return ret; |
| 235 | } |
| 236 | |
| 237 | return 0; |
Mark Brown | 4bb3f43 | 2011-04-08 16:49:42 +0900 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | static int __devexit wm1250_ev1_remove(struct i2c_client *i2c) |
| 241 | { |
| 242 | snd_soc_unregister_codec(&i2c->dev); |
Mark Brown | 213eb0f | 2011-09-21 20:54:47 +0100 | [diff] [blame] | 243 | wm1250_ev1_free(i2c); |
Mark Brown | 4bb3f43 | 2011-04-08 16:49:42 +0900 | [diff] [blame] | 244 | |
| 245 | return 0; |
| 246 | } |
| 247 | |
| 248 | static const struct i2c_device_id wm1250_ev1_i2c_id[] = { |
| 249 | { "wm1250-ev1", 0 }, |
| 250 | { } |
| 251 | }; |
Mark Brown | 420dd71 | 2011-04-11 21:40:29 -0700 | [diff] [blame] | 252 | MODULE_DEVICE_TABLE(i2c, wm1250_ev1_i2c_id); |
Mark Brown | 4bb3f43 | 2011-04-08 16:49:42 +0900 | [diff] [blame] | 253 | |
| 254 | static struct i2c_driver wm1250_ev1_i2c_driver = { |
| 255 | .driver = { |
| 256 | .name = "wm1250-ev1", |
| 257 | .owner = THIS_MODULE, |
| 258 | }, |
| 259 | .probe = wm1250_ev1_probe, |
| 260 | .remove = __devexit_p(wm1250_ev1_remove), |
| 261 | .id_table = wm1250_ev1_i2c_id, |
| 262 | }; |
| 263 | |
Mark Brown | bbdd391 | 2012-04-09 19:37:25 +0100 | [diff] [blame] | 264 | module_i2c_driver(wm1250_ev1_i2c_driver); |
Mark Brown | 4bb3f43 | 2011-04-08 16:49:42 +0900 | [diff] [blame] | 265 | |
| 266 | MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>"); |
| 267 | MODULE_DESCRIPTION("WM1250-EV1 audio I/O module driver"); |
| 268 | MODULE_LICENSE("GPL"); |