si476x.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. #include <linux/module.h>
  2. #include <linux/slab.h>
  3. #include <sound/pcm.h>
  4. #include <sound/pcm_params.h>
  5. #include <sound/soc.h>
  6. #include <sound/initval.h>
  7. #include <linux/i2c.h>
  8. #include <linux/mfd/si476x-core.h>
  9. enum si476x_audio_registers {
  10. SI476X_DIGITAL_IO_OUTPUT_FORMAT = 0x0203,
  11. SI476X_DIGITAL_IO_OUTPUT_SAMPLE_RATE = 0x0202,
  12. };
  13. enum si476x_digital_io_output_format {
  14. SI476X_DIGITAL_IO_SLOT_SIZE_SHIFT = 11,
  15. SI476X_DIGITAL_IO_SAMPLE_SIZE_SHIFT = 8,
  16. };
  17. #define SI476X_DIGITAL_IO_OUTPUT_WIDTH_MASK ((0b111 << SI476X_DIGITAL_IO_SLOT_SIZE_SHIFT) | \
  18. (0b111 << SI476X_DIGITAL_IO_SAMPLE_SIZE_SHIFT))
  19. #define SI476X_DIGITAL_IO_OUTPUT_FORMAT_MASK (0b1111110)
  20. enum si476x_daudio_formats {
  21. SI476X_DAUDIO_MODE_I2S = (0x0 << 1),
  22. SI476X_DAUDIO_MODE_DSP_A = (0x6 << 1),
  23. SI476X_DAUDIO_MODE_DSP_B = (0x7 << 1),
  24. SI476X_DAUDIO_MODE_LEFT_J = (0x8 << 1),
  25. SI476X_DAUDIO_MODE_RIGHT_J = (0x9 << 1),
  26. SI476X_DAUDIO_MODE_IB = (1 << 5),
  27. SI476X_DAUDIO_MODE_IF = (1 << 6),
  28. };
  29. enum si476x_pcm_format {
  30. SI476X_PCM_FORMAT_S8 = 2,
  31. SI476X_PCM_FORMAT_S16_LE = 4,
  32. SI476X_PCM_FORMAT_S20_3LE = 5,
  33. SI476X_PCM_FORMAT_S24_LE = 6,
  34. };
  35. static unsigned int si476x_codec_read(struct snd_soc_codec *codec,
  36. unsigned int reg)
  37. {
  38. int err;
  39. struct si476x_core *core = codec->control_data;
  40. si476x_core_lock(core);
  41. err = si476x_core_cmd_get_property(core, reg);
  42. si476x_core_unlock(core);
  43. return err;
  44. }
  45. static int si476x_codec_write(struct snd_soc_codec *codec,
  46. unsigned int reg, unsigned int val)
  47. {
  48. int err;
  49. struct si476x_core *core = codec->control_data;
  50. si476x_core_lock(core);
  51. err = si476x_core_cmd_set_property(core, reg, val);
  52. si476x_core_unlock(core);
  53. return err;
  54. }
  55. static int si476x_codec_set_dai_fmt(struct snd_soc_dai *codec_dai,
  56. unsigned int fmt)
  57. {
  58. int err;
  59. u16 format = 0;
  60. if ((fmt & SND_SOC_DAIFMT_MASTER_MASK) != SND_SOC_DAIFMT_CBS_CFS)
  61. return -EINVAL;
  62. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  63. case SND_SOC_DAIFMT_DSP_A:
  64. format |= SI476X_DAUDIO_MODE_DSP_A;
  65. break;
  66. case SND_SOC_DAIFMT_DSP_B:
  67. format |= SI476X_DAUDIO_MODE_DSP_B;
  68. break;
  69. case SND_SOC_DAIFMT_I2S:
  70. format |= SI476X_DAUDIO_MODE_I2S;
  71. break;
  72. case SND_SOC_DAIFMT_RIGHT_J:
  73. format |= SI476X_DAUDIO_MODE_RIGHT_J;
  74. break;
  75. case SND_SOC_DAIFMT_LEFT_J:
  76. format |= SI476X_DAUDIO_MODE_LEFT_J;
  77. break;
  78. default:
  79. return -EINVAL;
  80. }
  81. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  82. case SND_SOC_DAIFMT_DSP_A:
  83. case SND_SOC_DAIFMT_DSP_B:
  84. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  85. case SND_SOC_DAIFMT_NB_NF:
  86. break;
  87. case SND_SOC_DAIFMT_IB_NF:
  88. format |= SI476X_DAUDIO_MODE_IB;
  89. break;
  90. default:
  91. return -EINVAL;
  92. }
  93. break;
  94. case SND_SOC_DAIFMT_I2S:
  95. case SND_SOC_DAIFMT_RIGHT_J:
  96. case SND_SOC_DAIFMT_LEFT_J:
  97. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  98. case SND_SOC_DAIFMT_NB_NF:
  99. break;
  100. case SND_SOC_DAIFMT_IB_IF:
  101. format |= SI476X_DAUDIO_MODE_IB |
  102. SI476X_DAUDIO_MODE_IF;
  103. break;
  104. case SND_SOC_DAIFMT_IB_NF:
  105. format |= SI476X_DAUDIO_MODE_IB;
  106. break;
  107. case SND_SOC_DAIFMT_NB_IF:
  108. format |= SI476X_DAUDIO_MODE_IF;
  109. break;
  110. default:
  111. return -EINVAL;
  112. }
  113. break;
  114. default:
  115. return -EINVAL;
  116. }
  117. err = snd_soc_update_bits(codec_dai->codec, SI476X_DIGITAL_IO_OUTPUT_FORMAT,
  118. SI476X_DIGITAL_IO_OUTPUT_FORMAT_MASK,
  119. format);
  120. if (err < 0) {
  121. dev_err(codec_dai->codec->dev, "Failed to set output format\n");
  122. return err;
  123. }
  124. return 0;
  125. }
  126. static int si476x_codec_hw_params(struct snd_pcm_substream *substream,
  127. struct snd_pcm_hw_params *params,
  128. struct snd_soc_dai *dai)
  129. {
  130. int rate, width, err;
  131. rate = params_rate(params);
  132. if (rate < 32000 || rate > 48000) {
  133. dev_err(dai->codec->dev, "Rate: %d is not supported\n", rate);
  134. return -EINVAL;
  135. }
  136. switch (params_format(params)) {
  137. case SNDRV_PCM_FORMAT_S8:
  138. width = SI476X_PCM_FORMAT_S8;
  139. case SNDRV_PCM_FORMAT_S16_LE:
  140. width = SI476X_PCM_FORMAT_S16_LE;
  141. break;
  142. case SNDRV_PCM_FORMAT_S20_3LE:
  143. width = SI476X_PCM_FORMAT_S20_3LE;
  144. break;
  145. case SNDRV_PCM_FORMAT_S24_LE:
  146. width = SI476X_PCM_FORMAT_S24_LE;
  147. break;
  148. default:
  149. return -EINVAL;
  150. }
  151. err = snd_soc_write(dai->codec, SI476X_DIGITAL_IO_OUTPUT_SAMPLE_RATE,
  152. rate);
  153. if (err < 0) {
  154. dev_err(dai->codec->dev, "Failed to set sample rate\n");
  155. return err;
  156. }
  157. err = snd_soc_update_bits(dai->codec, SI476X_DIGITAL_IO_OUTPUT_FORMAT,
  158. SI476X_DIGITAL_IO_OUTPUT_WIDTH_MASK,
  159. (width << SI476X_DIGITAL_IO_SLOT_SIZE_SHIFT) |
  160. (width << SI476X_DIGITAL_IO_SAMPLE_SIZE_SHIFT));
  161. if (err < 0) {
  162. dev_err(dai->codec->dev, "Failed to set output width\n");
  163. return err;
  164. }
  165. return 0;
  166. }
  167. static int si476x_codec_probe(struct snd_soc_codec *codec)
  168. {
  169. codec->control_data = i2c_mfd_cell_to_core(codec->dev);
  170. return 0;
  171. }
  172. static struct snd_soc_dai_ops si476x_dai_ops = {
  173. .hw_params = si476x_codec_hw_params,
  174. .set_fmt = si476x_codec_set_dai_fmt,
  175. };
  176. static struct snd_soc_dai_driver si476x_dai = {
  177. .name = "si476x-codec",
  178. .capture = {
  179. .stream_name = "Capture",
  180. .channels_min = 2,
  181. .channels_max = 2,
  182. .rates = SNDRV_PCM_RATE_32000 |
  183. SNDRV_PCM_RATE_44100 |
  184. SNDRV_PCM_RATE_48000,
  185. .formats = SNDRV_PCM_FMTBIT_S8 |
  186. SNDRV_PCM_FMTBIT_S16_LE |
  187. SNDRV_PCM_FMTBIT_S20_3LE |
  188. SNDRV_PCM_FMTBIT_S24_LE
  189. },
  190. .ops = &si476x_dai_ops,
  191. };
  192. static struct snd_soc_codec_driver soc_codec_dev_si476x = {
  193. .probe = si476x_codec_probe,
  194. .read = si476x_codec_read,
  195. .write = si476x_codec_write,
  196. };
  197. static int si476x_platform_probe(struct platform_device *pdev)
  198. {
  199. return snd_soc_register_codec(&pdev->dev, &soc_codec_dev_si476x,
  200. &si476x_dai, 1);
  201. }
  202. static int si476x_platform_remove(struct platform_device *pdev)
  203. {
  204. snd_soc_unregister_codec(&pdev->dev);
  205. return 0;
  206. }
  207. MODULE_ALIAS("platform:si476x-codec");
  208. static struct platform_driver si476x_platform_driver = {
  209. .driver = {
  210. .name = "si476x-codec",
  211. .owner = THIS_MODULE,
  212. },
  213. .probe = si476x_platform_probe,
  214. .remove = si476x_platform_remove,
  215. };
  216. module_platform_driver(si476x_platform_driver);
  217. MODULE_AUTHOR("Andrey Smirnov <andrey.smirnov@convergeddevices.net>");
  218. MODULE_DESCRIPTION("ASoC Si4761/64 codec driver");
  219. MODULE_LICENSE("GPL");