wm1250-ev1.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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. return 0;
  54. }
  55. static const struct snd_soc_dapm_widget wm1250_ev1_dapm_widgets[] = {
  56. SND_SOC_DAPM_ADC("ADC", "wm1250-ev1 Capture", SND_SOC_NOPM, 0, 0),
  57. SND_SOC_DAPM_DAC("DAC", "wm1250-ev1 Playback", SND_SOC_NOPM, 0, 0),
  58. SND_SOC_DAPM_INPUT("WM1250 Input"),
  59. SND_SOC_DAPM_OUTPUT("WM1250 Output"),
  60. };
  61. static const struct snd_soc_dapm_route wm1250_ev1_dapm_routes[] = {
  62. { "ADC", NULL, "WM1250 Input" },
  63. { "WM1250 Output", NULL, "DAC" },
  64. };
  65. static int wm1250_ev1_hw_params(struct snd_pcm_substream *substream,
  66. struct snd_pcm_hw_params *params,
  67. struct snd_soc_dai *dai)
  68. {
  69. struct wm1250_priv *wm1250 = snd_soc_codec_get_drvdata(dai->codec);
  70. switch (params_rate(params)) {
  71. case 8000:
  72. gpio_set_value(wm1250->gpios[WM1250_EV1_GPIO_CLK_SEL0].gpio,
  73. 1);
  74. gpio_set_value(wm1250->gpios[WM1250_EV1_GPIO_CLK_SEL1].gpio,
  75. 1);
  76. break;
  77. case 16000:
  78. gpio_set_value(wm1250->gpios[WM1250_EV1_GPIO_CLK_SEL0].gpio,
  79. 0);
  80. gpio_set_value(wm1250->gpios[WM1250_EV1_GPIO_CLK_SEL1].gpio,
  81. 1);
  82. break;
  83. case 32000:
  84. gpio_set_value(wm1250->gpios[WM1250_EV1_GPIO_CLK_SEL0].gpio,
  85. 1);
  86. gpio_set_value(wm1250->gpios[WM1250_EV1_GPIO_CLK_SEL1].gpio,
  87. 0);
  88. break;
  89. case 64000:
  90. gpio_set_value(wm1250->gpios[WM1250_EV1_GPIO_CLK_SEL0].gpio,
  91. 0);
  92. gpio_set_value(wm1250->gpios[WM1250_EV1_GPIO_CLK_SEL1].gpio,
  93. 0);
  94. break;
  95. default:
  96. return -EINVAL;
  97. }
  98. return 0;
  99. }
  100. static const struct snd_soc_dai_ops wm1250_ev1_ops = {
  101. .hw_params = wm1250_ev1_hw_params,
  102. };
  103. #define WM1250_EV1_RATES (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_16000 |\
  104. SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_64000)
  105. static struct snd_soc_dai_driver wm1250_ev1_dai = {
  106. .name = "wm1250-ev1",
  107. .playback = {
  108. .stream_name = "Playback",
  109. .channels_min = 1,
  110. .channels_max = 2,
  111. .rates = WM1250_EV1_RATES,
  112. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  113. },
  114. .capture = {
  115. .stream_name = "Capture",
  116. .channels_min = 1,
  117. .channels_max = 2,
  118. .rates = WM1250_EV1_RATES,
  119. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  120. },
  121. .ops = &wm1250_ev1_ops,
  122. };
  123. static const struct snd_soc_codec_driver soc_codec_dev_wm1250_ev1 = {
  124. .component_driver = {
  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. },
  130. .set_bias_level = wm1250_ev1_set_bias_level,
  131. .idle_bias_off = true,
  132. };
  133. static int wm1250_ev1_pdata(struct i2c_client *i2c)
  134. {
  135. struct wm1250_ev1_pdata *pdata = dev_get_platdata(&i2c->dev);
  136. struct wm1250_priv *wm1250;
  137. int i, ret;
  138. if (!pdata)
  139. return 0;
  140. wm1250 = devm_kzalloc(&i2c->dev, sizeof(*wm1250), GFP_KERNEL);
  141. if (!wm1250) {
  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. },
  212. .probe = wm1250_ev1_probe,
  213. .remove = wm1250_ev1_remove,
  214. .id_table = wm1250_ev1_i2c_id,
  215. };
  216. module_i2c_driver(wm1250_ev1_i2c_driver);
  217. MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
  218. MODULE_DESCRIPTION("WM1250-EV1 audio I/O module driver");
  219. MODULE_LICENSE("GPL");