si476x.c 6.3 KB

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