si476x.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. /*
  2. * sound/soc/codecs/si476x.c -- Codec driver for SI476X chips
  3. *
  4. * Copyright (C) 2012 Innovative Converged Devices(ICD)
  5. * Copyright (C) 2013 Andrey Smirnov
  6. *
  7. * Author: Andrey Smirnov <andrew.smirnov@gmail.com>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; version 2 of the License.
  12. *
  13. * This program is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * General Public License for more details.
  17. *
  18. */
  19. #include <linux/module.h>
  20. #include <linux/slab.h>
  21. #include <sound/pcm.h>
  22. #include <sound/pcm_params.h>
  23. #include <sound/soc.h>
  24. #include <sound/initval.h>
  25. #include <linux/i2c.h>
  26. #include <linux/mfd/si476x-core.h>
  27. enum si476x_audio_registers {
  28. SI476X_DIGITAL_IO_OUTPUT_FORMAT = 0x0203,
  29. SI476X_DIGITAL_IO_OUTPUT_SAMPLE_RATE = 0x0202,
  30. };
  31. enum si476x_digital_io_output_format {
  32. SI476X_DIGITAL_IO_SLOT_SIZE_SHIFT = 11,
  33. SI476X_DIGITAL_IO_SAMPLE_SIZE_SHIFT = 8,
  34. };
  35. #define SI476X_DIGITAL_IO_OUTPUT_WIDTH_MASK ((0x7 << SI476X_DIGITAL_IO_SLOT_SIZE_SHIFT) | \
  36. (0x7 << SI476X_DIGITAL_IO_SAMPLE_SIZE_SHIFT))
  37. #define SI476X_DIGITAL_IO_OUTPUT_FORMAT_MASK (0x7e)
  38. enum si476x_daudio_formats {
  39. SI476X_DAUDIO_MODE_I2S = (0x0 << 1),
  40. SI476X_DAUDIO_MODE_DSP_A = (0x6 << 1),
  41. SI476X_DAUDIO_MODE_DSP_B = (0x7 << 1),
  42. SI476X_DAUDIO_MODE_LEFT_J = (0x8 << 1),
  43. SI476X_DAUDIO_MODE_RIGHT_J = (0x9 << 1),
  44. SI476X_DAUDIO_MODE_IB = (1 << 5),
  45. SI476X_DAUDIO_MODE_IF = (1 << 6),
  46. };
  47. enum si476x_pcm_format {
  48. SI476X_PCM_FORMAT_S8 = 2,
  49. SI476X_PCM_FORMAT_S16_LE = 4,
  50. SI476X_PCM_FORMAT_S20_3LE = 5,
  51. SI476X_PCM_FORMAT_S24_LE = 6,
  52. };
  53. static const struct snd_soc_dapm_widget si476x_dapm_widgets[] = {
  54. SND_SOC_DAPM_OUTPUT("LOUT"),
  55. SND_SOC_DAPM_OUTPUT("ROUT"),
  56. };
  57. static const struct snd_soc_dapm_route si476x_dapm_routes[] = {
  58. { "Capture", NULL, "LOUT" },
  59. { "Capture", NULL, "ROUT" },
  60. };
  61. static int si476x_codec_set_dai_fmt(struct snd_soc_dai *codec_dai,
  62. unsigned int fmt)
  63. {
  64. struct si476x_core *core = i2c_mfd_cell_to_core(codec_dai->dev);
  65. int err;
  66. u16 format = 0;
  67. if ((fmt & SND_SOC_DAIFMT_MASTER_MASK) != SND_SOC_DAIFMT_CBS_CFS)
  68. return -EINVAL;
  69. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  70. case SND_SOC_DAIFMT_DSP_A:
  71. format |= SI476X_DAUDIO_MODE_DSP_A;
  72. break;
  73. case SND_SOC_DAIFMT_DSP_B:
  74. format |= SI476X_DAUDIO_MODE_DSP_B;
  75. break;
  76. case SND_SOC_DAIFMT_I2S:
  77. format |= SI476X_DAUDIO_MODE_I2S;
  78. break;
  79. case SND_SOC_DAIFMT_RIGHT_J:
  80. format |= SI476X_DAUDIO_MODE_RIGHT_J;
  81. break;
  82. case SND_SOC_DAIFMT_LEFT_J:
  83. format |= SI476X_DAUDIO_MODE_LEFT_J;
  84. break;
  85. default:
  86. return -EINVAL;
  87. }
  88. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  89. case SND_SOC_DAIFMT_DSP_A:
  90. case SND_SOC_DAIFMT_DSP_B:
  91. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  92. case SND_SOC_DAIFMT_NB_NF:
  93. break;
  94. case SND_SOC_DAIFMT_IB_NF:
  95. format |= SI476X_DAUDIO_MODE_IB;
  96. break;
  97. default:
  98. return -EINVAL;
  99. }
  100. break;
  101. case SND_SOC_DAIFMT_I2S:
  102. case SND_SOC_DAIFMT_RIGHT_J:
  103. case SND_SOC_DAIFMT_LEFT_J:
  104. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  105. case SND_SOC_DAIFMT_NB_NF:
  106. break;
  107. case SND_SOC_DAIFMT_IB_IF:
  108. format |= SI476X_DAUDIO_MODE_IB |
  109. SI476X_DAUDIO_MODE_IF;
  110. break;
  111. case SND_SOC_DAIFMT_IB_NF:
  112. format |= SI476X_DAUDIO_MODE_IB;
  113. break;
  114. case SND_SOC_DAIFMT_NB_IF:
  115. format |= SI476X_DAUDIO_MODE_IF;
  116. break;
  117. default:
  118. return -EINVAL;
  119. }
  120. break;
  121. default:
  122. return -EINVAL;
  123. }
  124. si476x_core_lock(core);
  125. err = snd_soc_update_bits(codec_dai->codec, SI476X_DIGITAL_IO_OUTPUT_FORMAT,
  126. SI476X_DIGITAL_IO_OUTPUT_FORMAT_MASK,
  127. format);
  128. si476x_core_unlock(core);
  129. if (err < 0) {
  130. dev_err(codec_dai->codec->dev, "Failed to set output format\n");
  131. return err;
  132. }
  133. return 0;
  134. }
  135. static int si476x_codec_hw_params(struct snd_pcm_substream *substream,
  136. struct snd_pcm_hw_params *params,
  137. struct snd_soc_dai *dai)
  138. {
  139. struct si476x_core *core = i2c_mfd_cell_to_core(dai->dev);
  140. int rate, width, err;
  141. rate = params_rate(params);
  142. if (rate < 32000 || rate > 48000) {
  143. dev_err(dai->codec->dev, "Rate: %d is not supported\n", rate);
  144. return -EINVAL;
  145. }
  146. switch (params_format(params)) {
  147. case SNDRV_PCM_FORMAT_S8:
  148. width = SI476X_PCM_FORMAT_S8;
  149. break;
  150. case SNDRV_PCM_FORMAT_S16_LE:
  151. width = SI476X_PCM_FORMAT_S16_LE;
  152. break;
  153. case SNDRV_PCM_FORMAT_S20_3LE:
  154. width = SI476X_PCM_FORMAT_S20_3LE;
  155. break;
  156. case SNDRV_PCM_FORMAT_S24_LE:
  157. width = SI476X_PCM_FORMAT_S24_LE;
  158. break;
  159. default:
  160. return -EINVAL;
  161. }
  162. si476x_core_lock(core);
  163. err = snd_soc_write(dai->codec, SI476X_DIGITAL_IO_OUTPUT_SAMPLE_RATE,
  164. rate);
  165. if (err < 0) {
  166. dev_err(dai->codec->dev, "Failed to set sample rate\n");
  167. goto out;
  168. }
  169. err = snd_soc_update_bits(dai->codec, SI476X_DIGITAL_IO_OUTPUT_FORMAT,
  170. SI476X_DIGITAL_IO_OUTPUT_WIDTH_MASK,
  171. (width << SI476X_DIGITAL_IO_SLOT_SIZE_SHIFT) |
  172. (width << SI476X_DIGITAL_IO_SAMPLE_SIZE_SHIFT));
  173. if (err < 0) {
  174. dev_err(dai->codec->dev, "Failed to set output width\n");
  175. goto out;
  176. }
  177. out:
  178. si476x_core_unlock(core);
  179. return err;
  180. }
  181. static int si476x_codec_probe(struct snd_soc_codec *codec)
  182. {
  183. codec->control_data = dev_get_regmap(codec->dev->parent, NULL);
  184. return 0;
  185. }
  186. static struct snd_soc_dai_ops si476x_dai_ops = {
  187. .hw_params = si476x_codec_hw_params,
  188. .set_fmt = si476x_codec_set_dai_fmt,
  189. };
  190. static struct snd_soc_dai_driver si476x_dai = {
  191. .name = "si476x-codec",
  192. .capture = {
  193. .stream_name = "Capture",
  194. .channels_min = 2,
  195. .channels_max = 2,
  196. .rates = SNDRV_PCM_RATE_32000 |
  197. SNDRV_PCM_RATE_44100 |
  198. SNDRV_PCM_RATE_48000,
  199. .formats = SNDRV_PCM_FMTBIT_S8 |
  200. SNDRV_PCM_FMTBIT_S16_LE |
  201. SNDRV_PCM_FMTBIT_S20_3LE |
  202. SNDRV_PCM_FMTBIT_S24_LE
  203. },
  204. .ops = &si476x_dai_ops,
  205. };
  206. static struct snd_soc_codec_driver soc_codec_dev_si476x = {
  207. .probe = si476x_codec_probe,
  208. .dapm_widgets = si476x_dapm_widgets,
  209. .num_dapm_widgets = ARRAY_SIZE(si476x_dapm_widgets),
  210. .dapm_routes = si476x_dapm_routes,
  211. .num_dapm_routes = ARRAY_SIZE(si476x_dapm_routes),
  212. };
  213. static int si476x_platform_probe(struct platform_device *pdev)
  214. {
  215. return snd_soc_register_codec(&pdev->dev, &soc_codec_dev_si476x,
  216. &si476x_dai, 1);
  217. }
  218. static int si476x_platform_remove(struct platform_device *pdev)
  219. {
  220. snd_soc_unregister_codec(&pdev->dev);
  221. return 0;
  222. }
  223. MODULE_ALIAS("platform:si476x-codec");
  224. static struct platform_driver si476x_platform_driver = {
  225. .driver = {
  226. .name = "si476x-codec",
  227. .owner = THIS_MODULE,
  228. },
  229. .probe = si476x_platform_probe,
  230. .remove = si476x_platform_remove,
  231. };
  232. module_platform_driver(si476x_platform_driver);
  233. MODULE_AUTHOR("Andrey Smirnov <andrew.smirnov@gmail.com>");
  234. MODULE_DESCRIPTION("ASoC Si4761/64 codec driver");
  235. MODULE_LICENSE("GPL");