si476x.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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 <linux/regmap.h>
  24. #include <sound/soc.h>
  25. #include <sound/initval.h>
  26. #include <linux/i2c.h>
  27. #include <linux/mfd/si476x-core.h>
  28. enum si476x_audio_registers {
  29. SI476X_DIGITAL_IO_OUTPUT_FORMAT = 0x0203,
  30. SI476X_DIGITAL_IO_OUTPUT_SAMPLE_RATE = 0x0202,
  31. };
  32. enum si476x_digital_io_output_format {
  33. SI476X_DIGITAL_IO_SLOT_SIZE_SHIFT = 11,
  34. SI476X_DIGITAL_IO_SAMPLE_SIZE_SHIFT = 8,
  35. };
  36. #define SI476X_DIGITAL_IO_OUTPUT_WIDTH_MASK ((0x7 << SI476X_DIGITAL_IO_SLOT_SIZE_SHIFT) | \
  37. (0x7 << SI476X_DIGITAL_IO_SAMPLE_SIZE_SHIFT))
  38. #define SI476X_DIGITAL_IO_OUTPUT_FORMAT_MASK (0x7e)
  39. enum si476x_daudio_formats {
  40. SI476X_DAUDIO_MODE_I2S = (0x0 << 1),
  41. SI476X_DAUDIO_MODE_DSP_A = (0x6 << 1),
  42. SI476X_DAUDIO_MODE_DSP_B = (0x7 << 1),
  43. SI476X_DAUDIO_MODE_LEFT_J = (0x8 << 1),
  44. SI476X_DAUDIO_MODE_RIGHT_J = (0x9 << 1),
  45. SI476X_DAUDIO_MODE_IB = (1 << 5),
  46. SI476X_DAUDIO_MODE_IF = (1 << 6),
  47. };
  48. enum si476x_pcm_format {
  49. SI476X_PCM_FORMAT_S8 = 2,
  50. SI476X_PCM_FORMAT_S16_LE = 4,
  51. SI476X_PCM_FORMAT_S20_3LE = 5,
  52. SI476X_PCM_FORMAT_S24_LE = 6,
  53. };
  54. static const struct snd_soc_dapm_widget si476x_dapm_widgets[] = {
  55. SND_SOC_DAPM_OUTPUT("LOUT"),
  56. SND_SOC_DAPM_OUTPUT("ROUT"),
  57. };
  58. static const struct snd_soc_dapm_route si476x_dapm_routes[] = {
  59. { "Capture", NULL, "LOUT" },
  60. { "Capture", NULL, "ROUT" },
  61. };
  62. static int si476x_codec_set_dai_fmt(struct snd_soc_dai *codec_dai,
  63. unsigned int fmt)
  64. {
  65. struct si476x_core *core = i2c_mfd_cell_to_core(codec_dai->dev);
  66. int err;
  67. u16 format = 0;
  68. if ((fmt & SND_SOC_DAIFMT_MASTER_MASK) != SND_SOC_DAIFMT_CBS_CFS)
  69. return -EINVAL;
  70. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  71. case SND_SOC_DAIFMT_DSP_A:
  72. format |= SI476X_DAUDIO_MODE_DSP_A;
  73. break;
  74. case SND_SOC_DAIFMT_DSP_B:
  75. format |= SI476X_DAUDIO_MODE_DSP_B;
  76. break;
  77. case SND_SOC_DAIFMT_I2S:
  78. format |= SI476X_DAUDIO_MODE_I2S;
  79. break;
  80. case SND_SOC_DAIFMT_RIGHT_J:
  81. format |= SI476X_DAUDIO_MODE_RIGHT_J;
  82. break;
  83. case SND_SOC_DAIFMT_LEFT_J:
  84. format |= SI476X_DAUDIO_MODE_LEFT_J;
  85. break;
  86. default:
  87. return -EINVAL;
  88. }
  89. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  90. case SND_SOC_DAIFMT_DSP_A:
  91. case SND_SOC_DAIFMT_DSP_B:
  92. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  93. case SND_SOC_DAIFMT_NB_NF:
  94. break;
  95. case SND_SOC_DAIFMT_IB_NF:
  96. format |= SI476X_DAUDIO_MODE_IB;
  97. break;
  98. default:
  99. return -EINVAL;
  100. }
  101. break;
  102. case SND_SOC_DAIFMT_I2S:
  103. case SND_SOC_DAIFMT_RIGHT_J:
  104. case SND_SOC_DAIFMT_LEFT_J:
  105. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  106. case SND_SOC_DAIFMT_NB_NF:
  107. break;
  108. case SND_SOC_DAIFMT_IB_IF:
  109. format |= SI476X_DAUDIO_MODE_IB |
  110. SI476X_DAUDIO_MODE_IF;
  111. break;
  112. case SND_SOC_DAIFMT_IB_NF:
  113. format |= SI476X_DAUDIO_MODE_IB;
  114. break;
  115. case SND_SOC_DAIFMT_NB_IF:
  116. format |= SI476X_DAUDIO_MODE_IF;
  117. break;
  118. default:
  119. return -EINVAL;
  120. }
  121. break;
  122. default:
  123. return -EINVAL;
  124. }
  125. si476x_core_lock(core);
  126. err = snd_soc_update_bits(codec_dai->codec, SI476X_DIGITAL_IO_OUTPUT_FORMAT,
  127. SI476X_DIGITAL_IO_OUTPUT_FORMAT_MASK,
  128. format);
  129. si476x_core_unlock(core);
  130. if (err < 0) {
  131. dev_err(codec_dai->codec->dev, "Failed to set output format\n");
  132. return err;
  133. }
  134. return 0;
  135. }
  136. static int si476x_codec_hw_params(struct snd_pcm_substream *substream,
  137. struct snd_pcm_hw_params *params,
  138. struct snd_soc_dai *dai)
  139. {
  140. struct si476x_core *core = i2c_mfd_cell_to_core(dai->dev);
  141. int rate, width, err;
  142. rate = params_rate(params);
  143. if (rate < 32000 || rate > 48000) {
  144. dev_err(dai->codec->dev, "Rate: %d is not supported\n", rate);
  145. return -EINVAL;
  146. }
  147. switch (params_width(params)) {
  148. case 8:
  149. width = SI476X_PCM_FORMAT_S8;
  150. break;
  151. case 16:
  152. width = SI476X_PCM_FORMAT_S16_LE;
  153. break;
  154. case 20:
  155. width = SI476X_PCM_FORMAT_S20_3LE;
  156. break;
  157. case 24:
  158. width = SI476X_PCM_FORMAT_S24_LE;
  159. break;
  160. default:
  161. return -EINVAL;
  162. }
  163. si476x_core_lock(core);
  164. err = snd_soc_write(dai->codec, SI476X_DIGITAL_IO_OUTPUT_SAMPLE_RATE,
  165. rate);
  166. if (err < 0) {
  167. dev_err(dai->codec->dev, "Failed to set sample rate\n");
  168. goto out;
  169. }
  170. err = snd_soc_update_bits(dai->codec, SI476X_DIGITAL_IO_OUTPUT_FORMAT,
  171. SI476X_DIGITAL_IO_OUTPUT_WIDTH_MASK,
  172. (width << SI476X_DIGITAL_IO_SLOT_SIZE_SHIFT) |
  173. (width << SI476X_DIGITAL_IO_SAMPLE_SIZE_SHIFT));
  174. if (err < 0) {
  175. dev_err(dai->codec->dev, "Failed to set output width\n");
  176. goto out;
  177. }
  178. out:
  179. si476x_core_unlock(core);
  180. return err;
  181. }
  182. static const struct snd_soc_dai_ops si476x_dai_ops = {
  183. .hw_params = si476x_codec_hw_params,
  184. .set_fmt = si476x_codec_set_dai_fmt,
  185. };
  186. static struct snd_soc_dai_driver si476x_dai = {
  187. .name = "si476x-codec",
  188. .capture = {
  189. .stream_name = "Capture",
  190. .channels_min = 2,
  191. .channels_max = 2,
  192. .rates = SNDRV_PCM_RATE_32000 |
  193. SNDRV_PCM_RATE_44100 |
  194. SNDRV_PCM_RATE_48000,
  195. .formats = SNDRV_PCM_FMTBIT_S8 |
  196. SNDRV_PCM_FMTBIT_S16_LE |
  197. SNDRV_PCM_FMTBIT_S20_3LE |
  198. SNDRV_PCM_FMTBIT_S24_LE
  199. },
  200. .ops = &si476x_dai_ops,
  201. };
  202. static struct regmap *si476x_get_regmap(struct device *dev)
  203. {
  204. return dev_get_regmap(dev->parent, NULL);
  205. }
  206. static struct snd_soc_codec_driver soc_codec_dev_si476x = {
  207. .get_regmap = si476x_get_regmap,
  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. },
  228. .probe = si476x_platform_probe,
  229. .remove = si476x_platform_remove,
  230. };
  231. module_platform_driver(si476x_platform_driver);
  232. MODULE_AUTHOR("Andrey Smirnov <andrew.smirnov@gmail.com>");
  233. MODULE_DESCRIPTION("ASoC Si4761/64 codec driver");
  234. MODULE_LICENSE("GPL");