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 | |
| 82 | static struct snd_soc_dai_driver wm1250_ev1_dai = { |
| 83 | .name = "wm1250-ev1", |
| 84 | .playback = { |
| 85 | .stream_name = "Playback", |
| 86 | .channels_min = 1, |
| 87 | .channels_max = 1, |
| 88 | .rates = SNDRV_PCM_RATE_8000, |
| 89 | .formats = SNDRV_PCM_FMTBIT_S16_LE, |
| 90 | }, |
| 91 | .capture = { |
| 92 | .stream_name = "Capture", |
| 93 | .channels_min = 1, |
| 94 | .channels_max = 1, |
| 95 | .rates = SNDRV_PCM_RATE_8000, |
| 96 | .formats = SNDRV_PCM_FMTBIT_S16_LE, |
| 97 | }, |
| 98 | }; |
| 99 | |
| 100 | static struct snd_soc_codec_driver soc_codec_dev_wm1250_ev1 = { |
| 101 | .dapm_widgets = wm1250_ev1_dapm_widgets, |
| 102 | .num_dapm_widgets = ARRAY_SIZE(wm1250_ev1_dapm_widgets), |
| 103 | .dapm_routes = wm1250_ev1_dapm_routes, |
| 104 | .num_dapm_routes = ARRAY_SIZE(wm1250_ev1_dapm_routes), |
Mark Brown | 213eb0f | 2011-09-21 20:54:47 +0100 | [diff] [blame^] | 105 | |
| 106 | .set_bias_level = wm1250_ev1_set_bias_level, |
Mark Brown | 4bb3f43 | 2011-04-08 16:49:42 +0900 | [diff] [blame] | 107 | }; |
| 108 | |
Mark Brown | 213eb0f | 2011-09-21 20:54:47 +0100 | [diff] [blame^] | 109 | static int __devinit wm1250_ev1_pdata(struct i2c_client *i2c) |
| 110 | { |
| 111 | struct wm1250_ev1_pdata *pdata = dev_get_platdata(&i2c->dev); |
| 112 | struct wm1250_priv *wm1250; |
| 113 | int i, ret; |
| 114 | |
| 115 | if (!pdata) |
| 116 | return 0; |
| 117 | |
| 118 | wm1250 = kzalloc(sizeof(*wm1250), GFP_KERNEL); |
| 119 | if (!wm1250) { |
| 120 | dev_err(&i2c->dev, "Unable to allocate private data\n"); |
| 121 | ret = -ENOMEM; |
| 122 | goto err; |
| 123 | } |
| 124 | |
| 125 | for (i = 0; i < ARRAY_SIZE(wm1250->gpios); i++) { |
| 126 | wm1250->gpios[i].gpio = pdata->gpios[i]; |
| 127 | wm1250->gpios[i].label = wm1250_gpio_names[i]; |
| 128 | wm1250->gpios[i].flags = GPIOF_OUT_INIT_LOW; |
| 129 | } |
| 130 | wm1250->gpios[WM1250_EV1_GPIO_CLK_SEL0].flags = GPIOF_OUT_INIT_HIGH; |
| 131 | wm1250->gpios[WM1250_EV1_GPIO_CLK_SEL1].flags = GPIOF_OUT_INIT_HIGH; |
| 132 | |
| 133 | ret = gpio_request_array(wm1250->gpios, ARRAY_SIZE(wm1250->gpios)); |
| 134 | if (ret != 0) { |
| 135 | dev_err(&i2c->dev, "Failed to get GPIOs: %d\n", ret); |
| 136 | goto err_alloc; |
| 137 | } |
| 138 | |
| 139 | dev_set_drvdata(&i2c->dev, wm1250); |
| 140 | |
| 141 | return ret; |
| 142 | |
| 143 | err_alloc: |
| 144 | kfree(wm1250); |
| 145 | err: |
| 146 | return ret; |
| 147 | } |
| 148 | |
| 149 | static void wm1250_ev1_free(struct i2c_client *i2c) |
| 150 | { |
| 151 | struct wm1250_priv *wm1250 = dev_get_drvdata(&i2c->dev); |
| 152 | |
| 153 | if (wm1250) { |
| 154 | gpio_free_array(wm1250->gpios, ARRAY_SIZE(wm1250->gpios)); |
| 155 | kfree(wm1250); |
| 156 | } |
| 157 | } |
| 158 | |
Mark Brown | 4bb3f43 | 2011-04-08 16:49:42 +0900 | [diff] [blame] | 159 | static int __devinit wm1250_ev1_probe(struct i2c_client *i2c, |
Mark Brown | eaefb38 | 2011-07-29 16:27:18 +0100 | [diff] [blame] | 160 | const struct i2c_device_id *i2c_id) |
Mark Brown | 4bb3f43 | 2011-04-08 16:49:42 +0900 | [diff] [blame] | 161 | { |
Mark Brown | 213eb0f | 2011-09-21 20:54:47 +0100 | [diff] [blame^] | 162 | int id, board, rev, ret; |
| 163 | |
| 164 | dev_set_drvdata(&i2c->dev, NULL); |
Mark Brown | eaefb38 | 2011-07-29 16:27:18 +0100 | [diff] [blame] | 165 | |
| 166 | board = i2c_smbus_read_byte_data(i2c, 0); |
| 167 | if (board < 0) { |
Mark Brown | c38071c | 2011-07-29 16:27:18 +0100 | [diff] [blame] | 168 | dev_err(&i2c->dev, "Failed to read ID: %d\n", board); |
| 169 | return board; |
Mark Brown | eaefb38 | 2011-07-29 16:27:18 +0100 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | id = (board & 0xfe) >> 2; |
| 173 | rev = board & 0x3; |
| 174 | |
| 175 | if (id != 1) { |
| 176 | dev_err(&i2c->dev, "Unknown board ID %d\n", id); |
| 177 | return -ENODEV; |
| 178 | } |
| 179 | |
Mark Brown | 5c58b73 | 2011-08-14 19:07:33 +0900 | [diff] [blame] | 180 | dev_info(&i2c->dev, "revision %d\n", rev + 1); |
Mark Brown | eaefb38 | 2011-07-29 16:27:18 +0100 | [diff] [blame] | 181 | |
Mark Brown | 213eb0f | 2011-09-21 20:54:47 +0100 | [diff] [blame^] | 182 | ret = wm1250_ev1_pdata(i2c); |
| 183 | if (ret != 0) |
| 184 | return ret; |
| 185 | |
| 186 | ret = snd_soc_register_codec(&i2c->dev, &soc_codec_dev_wm1250_ev1, |
| 187 | &wm1250_ev1_dai, 1); |
| 188 | if (ret != 0) { |
| 189 | dev_err(&i2c->dev, "Failed to register CODEC: %d\n", ret); |
| 190 | wm1250_ev1_free(i2c); |
| 191 | return ret; |
| 192 | } |
| 193 | |
| 194 | return 0; |
Mark Brown | 4bb3f43 | 2011-04-08 16:49:42 +0900 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | static int __devexit wm1250_ev1_remove(struct i2c_client *i2c) |
| 198 | { |
| 199 | snd_soc_unregister_codec(&i2c->dev); |
Mark Brown | 213eb0f | 2011-09-21 20:54:47 +0100 | [diff] [blame^] | 200 | wm1250_ev1_free(i2c); |
Mark Brown | 4bb3f43 | 2011-04-08 16:49:42 +0900 | [diff] [blame] | 201 | |
| 202 | return 0; |
| 203 | } |
| 204 | |
| 205 | static const struct i2c_device_id wm1250_ev1_i2c_id[] = { |
| 206 | { "wm1250-ev1", 0 }, |
| 207 | { } |
| 208 | }; |
Mark Brown | 420dd71 | 2011-04-11 21:40:29 -0700 | [diff] [blame] | 209 | MODULE_DEVICE_TABLE(i2c, wm1250_ev1_i2c_id); |
Mark Brown | 4bb3f43 | 2011-04-08 16:49:42 +0900 | [diff] [blame] | 210 | |
| 211 | static struct i2c_driver wm1250_ev1_i2c_driver = { |
| 212 | .driver = { |
| 213 | .name = "wm1250-ev1", |
| 214 | .owner = THIS_MODULE, |
| 215 | }, |
| 216 | .probe = wm1250_ev1_probe, |
| 217 | .remove = __devexit_p(wm1250_ev1_remove), |
| 218 | .id_table = wm1250_ev1_i2c_id, |
| 219 | }; |
| 220 | |
| 221 | static int __init wm1250_ev1_modinit(void) |
| 222 | { |
| 223 | int ret = 0; |
| 224 | |
| 225 | ret = i2c_add_driver(&wm1250_ev1_i2c_driver); |
| 226 | if (ret != 0) |
| 227 | pr_err("Failed to register WM1250-EV1 I2C driver: %d\n", ret); |
| 228 | |
| 229 | return ret; |
| 230 | } |
| 231 | module_init(wm1250_ev1_modinit); |
| 232 | |
| 233 | static void __exit wm1250_ev1_exit(void) |
| 234 | { |
| 235 | i2c_del_driver(&wm1250_ev1_i2c_driver); |
| 236 | } |
| 237 | module_exit(wm1250_ev1_exit); |
| 238 | |
| 239 | MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>"); |
| 240 | MODULE_DESCRIPTION("WM1250-EV1 audio I/O module driver"); |
| 241 | MODULE_LICENSE("GPL"); |