wm1250-ev1.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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. #define WM1250_EV1_RATES (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_16000 |\
  105. SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_64000)
  106. static struct snd_soc_dai_driver wm1250_ev1_dai = {
  107. .name = "wm1250-ev1",
  108. .playback = {
  109. .stream_name = "Playback",
  110. .channels_min = 1,
  111. .channels_max = 2,
  112. .rates = WM1250_EV1_RATES,
  113. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  114. },
  115. .capture = {
  116. .stream_name = "Capture",
  117. .channels_min = 1,
  118. .channels_max = 2,
  119. .rates = WM1250_EV1_RATES,
  120. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  121. },
  122. .ops = &wm1250_ev1_ops,
  123. };
  124. static struct snd_soc_codec_driver soc_codec_dev_wm1250_ev1 = {
  125. .dapm_widgets = wm1250_ev1_dapm_widgets,
  126. .num_dapm_widgets = ARRAY_SIZE(wm1250_ev1_dapm_widgets),
  127. .dapm_routes = wm1250_ev1_dapm_routes,
  128. .num_dapm_routes = ARRAY_SIZE(wm1250_ev1_dapm_routes),
  129. .set_bias_level = wm1250_ev1_set_bias_level,
  130. .idle_bias_off = true,
  131. };
  132. static int wm1250_ev1_pdata(struct i2c_client *i2c)
  133. {
  134. struct wm1250_ev1_pdata *pdata = dev_get_platdata(&i2c->dev);
  135. struct wm1250_priv *wm1250;
  136. int i, ret;
  137. if (!pdata)
  138. return 0;
  139. wm1250 = devm_kzalloc(&i2c->dev, sizeof(*wm1250), GFP_KERNEL);
  140. if (!wm1250) {
  141. dev_err(&i2c->dev, "Unable to allocate private data\n");
  142. ret = -ENOMEM;
  143. goto err;
  144. }
  145. for (i = 0; i < ARRAY_SIZE(wm1250->gpios); i++) {
  146. wm1250->gpios[i].gpio = pdata->gpios[i];
  147. wm1250->gpios[i].label = wm1250_gpio_names[i];
  148. wm1250->gpios[i].flags = GPIOF_OUT_INIT_LOW;
  149. }
  150. wm1250->gpios[WM1250_EV1_GPIO_CLK_SEL0].flags = GPIOF_OUT_INIT_HIGH;
  151. wm1250->gpios[WM1250_EV1_GPIO_CLK_SEL1].flags = GPIOF_OUT_INIT_HIGH;
  152. ret = gpio_request_array(wm1250->gpios, ARRAY_SIZE(wm1250->gpios));
  153. if (ret != 0) {
  154. dev_err(&i2c->dev, "Failed to get GPIOs: %d\n", ret);
  155. goto err;
  156. }
  157. dev_set_drvdata(&i2c->dev, wm1250);
  158. return ret;
  159. err:
  160. return ret;
  161. }
  162. static void wm1250_ev1_free(struct i2c_client *i2c)
  163. {
  164. struct wm1250_priv *wm1250 = dev_get_drvdata(&i2c->dev);
  165. if (wm1250)
  166. gpio_free_array(wm1250->gpios, ARRAY_SIZE(wm1250->gpios));
  167. }
  168. static int wm1250_ev1_probe(struct i2c_client *i2c,
  169. const struct i2c_device_id *i2c_id)
  170. {
  171. int id, board, rev, ret;
  172. dev_set_drvdata(&i2c->dev, NULL);
  173. board = i2c_smbus_read_byte_data(i2c, 0);
  174. if (board < 0) {
  175. dev_err(&i2c->dev, "Failed to read ID: %d\n", board);
  176. return board;
  177. }
  178. id = (board & 0xfe) >> 2;
  179. rev = board & 0x3;
  180. if (id != 1) {
  181. dev_err(&i2c->dev, "Unknown board ID %d\n", id);
  182. return -ENODEV;
  183. }
  184. dev_info(&i2c->dev, "revision %d\n", rev + 1);
  185. ret = wm1250_ev1_pdata(i2c);
  186. if (ret != 0)
  187. return ret;
  188. ret = snd_soc_register_codec(&i2c->dev, &soc_codec_dev_wm1250_ev1,
  189. &wm1250_ev1_dai, 1);
  190. if (ret != 0) {
  191. dev_err(&i2c->dev, "Failed to register CODEC: %d\n", ret);
  192. wm1250_ev1_free(i2c);
  193. return ret;
  194. }
  195. return 0;
  196. }
  197. static int wm1250_ev1_remove(struct i2c_client *i2c)
  198. {
  199. snd_soc_unregister_codec(&i2c->dev);
  200. wm1250_ev1_free(i2c);
  201. return 0;
  202. }
  203. static const struct i2c_device_id wm1250_ev1_i2c_id[] = {
  204. { "wm1250-ev1", 0 },
  205. { }
  206. };
  207. MODULE_DEVICE_TABLE(i2c, wm1250_ev1_i2c_id);
  208. static struct i2c_driver wm1250_ev1_i2c_driver = {
  209. .driver = {
  210. .name = "wm1250-ev1",
  211. .owner = THIS_MODULE,
  212. },
  213. .probe = wm1250_ev1_probe,
  214. .remove = wm1250_ev1_remove,
  215. .id_table = wm1250_ev1_i2c_id,
  216. };
  217. module_i2c_driver(wm1250_ev1_i2c_driver);
  218. MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
  219. MODULE_DESCRIPTION("WM1250-EV1 audio I/O module driver");
  220. MODULE_LICENSE("GPL");