wm1250-ev1.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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. #include <linux/init.h>
  13. #include <linux/module.h>
  14. #include <linux/slab.h>
  15. #include <linux/i2c.h>
  16. #include <linux/gpio.h>
  17. #include <sound/soc.h>
  18. #include <sound/soc-dapm.h>
  19. #include <sound/wm1250-ev1.h>
  20. static const char *wm1250_gpio_names[WM1250_EV1_NUM_GPIOS] = {
  21. "WM1250 CLK_ENA",
  22. "WM1250 CLK_SEL0",
  23. "WM1250 CLK_SEL1",
  24. "WM1250 OSR",
  25. "WM1250 MASTER",
  26. };
  27. struct wm1250_priv {
  28. struct gpio gpios[WM1250_EV1_NUM_GPIOS];
  29. };
  30. static int wm1250_ev1_set_bias_level(struct snd_soc_codec *codec,
  31. enum snd_soc_bias_level level)
  32. {
  33. struct wm1250_priv *wm1250 = dev_get_drvdata(codec->dev);
  34. int ena;
  35. if (wm1250)
  36. ena = wm1250->gpios[WM1250_EV1_GPIO_CLK_ENA].gpio;
  37. else
  38. ena = -1;
  39. switch (level) {
  40. case SND_SOC_BIAS_ON:
  41. break;
  42. case SND_SOC_BIAS_PREPARE:
  43. break;
  44. case SND_SOC_BIAS_STANDBY:
  45. if (ena >= 0)
  46. gpio_set_value_cansleep(ena, 1);
  47. break;
  48. case SND_SOC_BIAS_OFF:
  49. if (ena >= 0)
  50. gpio_set_value_cansleep(ena, 0);
  51. break;
  52. }
  53. codec->dapm.bias_level = level;
  54. return 0;
  55. }
  56. static const struct snd_soc_dapm_widget wm1250_ev1_dapm_widgets[] = {
  57. SND_SOC_DAPM_ADC("ADC", "wm1250-ev1 Capture", SND_SOC_NOPM, 0, 0),
  58. SND_SOC_DAPM_DAC("DAC", "wm1250-ev1 Playback", SND_SOC_NOPM, 0, 0),
  59. SND_SOC_DAPM_INPUT("WM1250 Input"),
  60. SND_SOC_DAPM_OUTPUT("WM1250 Output"),
  61. };
  62. static const struct snd_soc_dapm_route wm1250_ev1_dapm_routes[] = {
  63. { "ADC", NULL, "WM1250 Input" },
  64. { "WM1250 Output", NULL, "DAC" },
  65. };
  66. static int wm1250_ev1_hw_params(struct snd_pcm_substream *substream,
  67. struct snd_pcm_hw_params *params,
  68. struct snd_soc_dai *dai)
  69. {
  70. struct wm1250_priv *wm1250 = snd_soc_codec_get_drvdata(dai->codec);
  71. switch (params_rate(params)) {
  72. case 8000:
  73. gpio_set_value(wm1250->gpios[WM1250_EV1_GPIO_CLK_SEL0].gpio,
  74. 1);
  75. gpio_set_value(wm1250->gpios[WM1250_EV1_GPIO_CLK_SEL1].gpio,
  76. 1);
  77. break;
  78. case 16000:
  79. gpio_set_value(wm1250->gpios[WM1250_EV1_GPIO_CLK_SEL0].gpio,
  80. 0);
  81. gpio_set_value(wm1250->gpios[WM1250_EV1_GPIO_CLK_SEL1].gpio,
  82. 1);
  83. break;
  84. case 32000:
  85. gpio_set_value(wm1250->gpios[WM1250_EV1_GPIO_CLK_SEL0].gpio,
  86. 1);
  87. gpio_set_value(wm1250->gpios[WM1250_EV1_GPIO_CLK_SEL1].gpio,
  88. 0);
  89. break;
  90. case 64000:
  91. gpio_set_value(wm1250->gpios[WM1250_EV1_GPIO_CLK_SEL0].gpio,
  92. 0);
  93. gpio_set_value(wm1250->gpios[WM1250_EV1_GPIO_CLK_SEL1].gpio,
  94. 0);
  95. break;
  96. default:
  97. return -EINVAL;
  98. }
  99. return 0;
  100. }
  101. static const struct snd_soc_dai_ops wm1250_ev1_ops = {
  102. .hw_params = wm1250_ev1_hw_params,
  103. };
  104. static struct snd_soc_dai_driver wm1250_ev1_dai = {
  105. .name = "wm1250-ev1",
  106. .playback = {
  107. .stream_name = "Playback",
  108. .channels_min = 1,
  109. .channels_max = 2,
  110. .rates = SNDRV_PCM_RATE_8000,
  111. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  112. },
  113. .capture = {
  114. .stream_name = "Capture",
  115. .channels_min = 1,
  116. .channels_max = 2,
  117. .rates = SNDRV_PCM_RATE_8000,
  118. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  119. },
  120. .ops = &wm1250_ev1_ops,
  121. };
  122. static struct snd_soc_codec_driver soc_codec_dev_wm1250_ev1 = {
  123. .dapm_widgets = wm1250_ev1_dapm_widgets,
  124. .num_dapm_widgets = ARRAY_SIZE(wm1250_ev1_dapm_widgets),
  125. .dapm_routes = wm1250_ev1_dapm_routes,
  126. .num_dapm_routes = ARRAY_SIZE(wm1250_ev1_dapm_routes),
  127. .set_bias_level = wm1250_ev1_set_bias_level,
  128. .idle_bias_off = true,
  129. };
  130. static int __devinit wm1250_ev1_pdata(struct i2c_client *i2c)
  131. {
  132. struct wm1250_ev1_pdata *pdata = dev_get_platdata(&i2c->dev);
  133. struct wm1250_priv *wm1250;
  134. int i, ret;
  135. if (!pdata)
  136. return 0;
  137. wm1250 = devm_kzalloc(&i2c->dev, sizeof(*wm1250), GFP_KERNEL);
  138. if (!wm1250) {
  139. dev_err(&i2c->dev, "Unable to allocate private data\n");
  140. ret = -ENOMEM;
  141. goto err;
  142. }
  143. for (i = 0; i < ARRAY_SIZE(wm1250->gpios); i++) {
  144. wm1250->gpios[i].gpio = pdata->gpios[i];
  145. wm1250->gpios[i].label = wm1250_gpio_names[i];
  146. wm1250->gpios[i].flags = GPIOF_OUT_INIT_LOW;
  147. }
  148. wm1250->gpios[WM1250_EV1_GPIO_CLK_SEL0].flags = GPIOF_OUT_INIT_HIGH;
  149. wm1250->gpios[WM1250_EV1_GPIO_CLK_SEL1].flags = GPIOF_OUT_INIT_HIGH;
  150. ret = gpio_request_array(wm1250->gpios, ARRAY_SIZE(wm1250->gpios));
  151. if (ret != 0) {
  152. dev_err(&i2c->dev, "Failed to get GPIOs: %d\n", ret);
  153. goto err;
  154. }
  155. dev_set_drvdata(&i2c->dev, wm1250);
  156. return ret;
  157. err:
  158. return ret;
  159. }
  160. static void wm1250_ev1_free(struct i2c_client *i2c)
  161. {
  162. struct wm1250_priv *wm1250 = dev_get_drvdata(&i2c->dev);
  163. if (wm1250)
  164. gpio_free_array(wm1250->gpios, ARRAY_SIZE(wm1250->gpios));
  165. }
  166. static int __devinit wm1250_ev1_probe(struct i2c_client *i2c,
  167. const struct i2c_device_id *i2c_id)
  168. {
  169. int id, board, rev, ret;
  170. dev_set_drvdata(&i2c->dev, NULL);
  171. board = i2c_smbus_read_byte_data(i2c, 0);
  172. if (board < 0) {
  173. dev_err(&i2c->dev, "Failed to read ID: %d\n", board);
  174. return board;
  175. }
  176. id = (board & 0xfe) >> 2;
  177. rev = board & 0x3;
  178. if (id != 1) {
  179. dev_err(&i2c->dev, "Unknown board ID %d\n", id);
  180. return -ENODEV;
  181. }
  182. dev_info(&i2c->dev, "revision %d\n", rev + 1);
  183. ret = wm1250_ev1_pdata(i2c);
  184. if (ret != 0)
  185. return ret;
  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. return 0;
  194. }
  195. static int __devexit wm1250_ev1_remove(struct i2c_client *i2c)
  196. {
  197. snd_soc_unregister_codec(&i2c->dev);
  198. wm1250_ev1_free(i2c);
  199. return 0;
  200. }
  201. static const struct i2c_device_id wm1250_ev1_i2c_id[] = {
  202. { "wm1250-ev1", 0 },
  203. { }
  204. };
  205. MODULE_DEVICE_TABLE(i2c, wm1250_ev1_i2c_id);
  206. static struct i2c_driver wm1250_ev1_i2c_driver = {
  207. .driver = {
  208. .name = "wm1250-ev1",
  209. .owner = THIS_MODULE,
  210. },
  211. .probe = wm1250_ev1_probe,
  212. .remove = __devexit_p(wm1250_ev1_remove),
  213. .id_table = wm1250_ev1_i2c_id,
  214. };
  215. module_i2c_driver(wm1250_ev1_i2c_driver);
  216. MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
  217. MODULE_DESCRIPTION("WM1250-EV1 audio I/O module driver");
  218. MODULE_LICENSE("GPL");