wm1250-ev1.c 5.3 KB

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