wm1250-ev1.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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 struct snd_soc_dai_driver wm1250_ev1_dai = {
  67. .name = "wm1250-ev1",
  68. .playback = {
  69. .stream_name = "Playback",
  70. .channels_min = 1,
  71. .channels_max = 1,
  72. .rates = SNDRV_PCM_RATE_8000,
  73. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  74. },
  75. .capture = {
  76. .stream_name = "Capture",
  77. .channels_min = 1,
  78. .channels_max = 1,
  79. .rates = SNDRV_PCM_RATE_8000,
  80. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  81. },
  82. };
  83. static struct snd_soc_codec_driver soc_codec_dev_wm1250_ev1 = {
  84. .dapm_widgets = wm1250_ev1_dapm_widgets,
  85. .num_dapm_widgets = ARRAY_SIZE(wm1250_ev1_dapm_widgets),
  86. .dapm_routes = wm1250_ev1_dapm_routes,
  87. .num_dapm_routes = ARRAY_SIZE(wm1250_ev1_dapm_routes),
  88. .set_bias_level = wm1250_ev1_set_bias_level,
  89. .idle_bias_off = true,
  90. };
  91. static int __devinit wm1250_ev1_pdata(struct i2c_client *i2c)
  92. {
  93. struct wm1250_ev1_pdata *pdata = dev_get_platdata(&i2c->dev);
  94. struct wm1250_priv *wm1250;
  95. int i, ret;
  96. if (!pdata)
  97. return 0;
  98. wm1250 = devm_kzalloc(&i2c->dev, sizeof(*wm1250), GFP_KERNEL);
  99. if (!wm1250) {
  100. dev_err(&i2c->dev, "Unable to allocate private data\n");
  101. ret = -ENOMEM;
  102. goto err;
  103. }
  104. for (i = 0; i < ARRAY_SIZE(wm1250->gpios); i++) {
  105. wm1250->gpios[i].gpio = pdata->gpios[i];
  106. wm1250->gpios[i].label = wm1250_gpio_names[i];
  107. wm1250->gpios[i].flags = GPIOF_OUT_INIT_LOW;
  108. }
  109. wm1250->gpios[WM1250_EV1_GPIO_CLK_SEL0].flags = GPIOF_OUT_INIT_HIGH;
  110. wm1250->gpios[WM1250_EV1_GPIO_CLK_SEL1].flags = GPIOF_OUT_INIT_HIGH;
  111. ret = gpio_request_array(wm1250->gpios, ARRAY_SIZE(wm1250->gpios));
  112. if (ret != 0) {
  113. dev_err(&i2c->dev, "Failed to get GPIOs: %d\n", ret);
  114. goto err;
  115. }
  116. dev_set_drvdata(&i2c->dev, wm1250);
  117. return ret;
  118. err:
  119. return ret;
  120. }
  121. static void wm1250_ev1_free(struct i2c_client *i2c)
  122. {
  123. struct wm1250_priv *wm1250 = dev_get_drvdata(&i2c->dev);
  124. if (wm1250)
  125. gpio_free_array(wm1250->gpios, ARRAY_SIZE(wm1250->gpios));
  126. }
  127. static int __devinit wm1250_ev1_probe(struct i2c_client *i2c,
  128. const struct i2c_device_id *i2c_id)
  129. {
  130. int id, board, rev, ret;
  131. dev_set_drvdata(&i2c->dev, NULL);
  132. board = i2c_smbus_read_byte_data(i2c, 0);
  133. if (board < 0) {
  134. dev_err(&i2c->dev, "Failed to read ID: %d\n", board);
  135. return board;
  136. }
  137. id = (board & 0xfe) >> 2;
  138. rev = board & 0x3;
  139. if (id != 1) {
  140. dev_err(&i2c->dev, "Unknown board ID %d\n", id);
  141. return -ENODEV;
  142. }
  143. dev_info(&i2c->dev, "revision %d\n", rev + 1);
  144. ret = wm1250_ev1_pdata(i2c);
  145. if (ret != 0)
  146. return ret;
  147. ret = snd_soc_register_codec(&i2c->dev, &soc_codec_dev_wm1250_ev1,
  148. &wm1250_ev1_dai, 1);
  149. if (ret != 0) {
  150. dev_err(&i2c->dev, "Failed to register CODEC: %d\n", ret);
  151. wm1250_ev1_free(i2c);
  152. return ret;
  153. }
  154. return 0;
  155. }
  156. static int __devexit wm1250_ev1_remove(struct i2c_client *i2c)
  157. {
  158. snd_soc_unregister_codec(&i2c->dev);
  159. wm1250_ev1_free(i2c);
  160. return 0;
  161. }
  162. static const struct i2c_device_id wm1250_ev1_i2c_id[] = {
  163. { "wm1250-ev1", 0 },
  164. { }
  165. };
  166. MODULE_DEVICE_TABLE(i2c, wm1250_ev1_i2c_id);
  167. static struct i2c_driver wm1250_ev1_i2c_driver = {
  168. .driver = {
  169. .name = "wm1250-ev1",
  170. .owner = THIS_MODULE,
  171. },
  172. .probe = wm1250_ev1_probe,
  173. .remove = __devexit_p(wm1250_ev1_remove),
  174. .id_table = wm1250_ev1_i2c_id,
  175. };
  176. static int __init wm1250_ev1_modinit(void)
  177. {
  178. int ret = 0;
  179. ret = i2c_add_driver(&wm1250_ev1_i2c_driver);
  180. if (ret != 0)
  181. pr_err("Failed to register WM1250-EV1 I2C driver: %d\n", ret);
  182. return ret;
  183. }
  184. module_init(wm1250_ev1_modinit);
  185. static void __exit wm1250_ev1_exit(void)
  186. {
  187. i2c_del_driver(&wm1250_ev1_i2c_driver);
  188. }
  189. module_exit(wm1250_ev1_exit);
  190. MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
  191. MODULE_DESCRIPTION("WM1250-EV1 audio I/O module driver");
  192. MODULE_LICENSE("GPL");