si476x.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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 unsigned int si476x_codec_read(struct snd_soc_codec *codec,
  54. unsigned int reg)
  55. {
  56. int err;
  57. unsigned int val;
  58. struct si476x_core *core = codec->control_data;
  59. si476x_core_lock(core);
  60. if (!si476x_core_is_powered_up(core))
  61. regcache_cache_only(core->regmap, true);
  62. err = regmap_read(core->regmap, reg, &val);
  63. if (!si476x_core_is_powered_up(core))
  64. regcache_cache_only(core->regmap, false);
  65. si476x_core_unlock(core);
  66. if (err < 0)
  67. return err;
  68. return val;
  69. }
  70. static int si476x_codec_write(struct snd_soc_codec *codec,
  71. unsigned int reg, unsigned int val)
  72. {
  73. int err;
  74. struct si476x_core *core = codec->control_data;
  75. si476x_core_lock(core);
  76. if (!si476x_core_is_powered_up(core))
  77. regcache_cache_only(core->regmap, true);
  78. err = regmap_write(core->regmap, reg, val);
  79. if (!si476x_core_is_powered_up(core))
  80. regcache_cache_only(core->regmap, false);
  81. si476x_core_unlock(core);
  82. return err;
  83. }
  84. static int si476x_codec_set_dai_fmt(struct snd_soc_dai *codec_dai,
  85. unsigned int fmt)
  86. {
  87. int err;
  88. u16 format = 0;
  89. if ((fmt & SND_SOC_DAIFMT_MASTER_MASK) != SND_SOC_DAIFMT_CBS_CFS)
  90. return -EINVAL;
  91. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  92. case SND_SOC_DAIFMT_DSP_A:
  93. format |= SI476X_DAUDIO_MODE_DSP_A;
  94. break;
  95. case SND_SOC_DAIFMT_DSP_B:
  96. format |= SI476X_DAUDIO_MODE_DSP_B;
  97. break;
  98. case SND_SOC_DAIFMT_I2S:
  99. format |= SI476X_DAUDIO_MODE_I2S;
  100. break;
  101. case SND_SOC_DAIFMT_RIGHT_J:
  102. format |= SI476X_DAUDIO_MODE_RIGHT_J;
  103. break;
  104. case SND_SOC_DAIFMT_LEFT_J:
  105. format |= SI476X_DAUDIO_MODE_LEFT_J;
  106. break;
  107. default:
  108. return -EINVAL;
  109. }
  110. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  111. case SND_SOC_DAIFMT_DSP_A:
  112. case SND_SOC_DAIFMT_DSP_B:
  113. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  114. case SND_SOC_DAIFMT_NB_NF:
  115. break;
  116. case SND_SOC_DAIFMT_IB_NF:
  117. format |= SI476X_DAUDIO_MODE_IB;
  118. break;
  119. default:
  120. return -EINVAL;
  121. }
  122. break;
  123. case SND_SOC_DAIFMT_I2S:
  124. case SND_SOC_DAIFMT_RIGHT_J:
  125. case SND_SOC_DAIFMT_LEFT_J:
  126. switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
  127. case SND_SOC_DAIFMT_NB_NF:
  128. break;
  129. case SND_SOC_DAIFMT_IB_IF:
  130. format |= SI476X_DAUDIO_MODE_IB |
  131. SI476X_DAUDIO_MODE_IF;
  132. break;
  133. case SND_SOC_DAIFMT_IB_NF:
  134. format |= SI476X_DAUDIO_MODE_IB;
  135. break;
  136. case SND_SOC_DAIFMT_NB_IF:
  137. format |= SI476X_DAUDIO_MODE_IF;
  138. break;
  139. default:
  140. return -EINVAL;
  141. }
  142. break;
  143. default:
  144. return -EINVAL;
  145. }
  146. err = snd_soc_update_bits(codec_dai->codec, SI476X_DIGITAL_IO_OUTPUT_FORMAT,
  147. SI476X_DIGITAL_IO_OUTPUT_FORMAT_MASK,
  148. format);
  149. if (err < 0) {
  150. dev_err(codec_dai->codec->dev, "Failed to set output format\n");
  151. return err;
  152. }
  153. return 0;
  154. }
  155. static int si476x_codec_hw_params(struct snd_pcm_substream *substream,
  156. struct snd_pcm_hw_params *params,
  157. struct snd_soc_dai *dai)
  158. {
  159. int rate, width, err;
  160. rate = params_rate(params);
  161. if (rate < 32000 || rate > 48000) {
  162. dev_err(dai->codec->dev, "Rate: %d is not supported\n", rate);
  163. return -EINVAL;
  164. }
  165. switch (params_format(params)) {
  166. case SNDRV_PCM_FORMAT_S8:
  167. width = SI476X_PCM_FORMAT_S8;
  168. break;
  169. case SNDRV_PCM_FORMAT_S16_LE:
  170. width = SI476X_PCM_FORMAT_S16_LE;
  171. break;
  172. case SNDRV_PCM_FORMAT_S20_3LE:
  173. width = SI476X_PCM_FORMAT_S20_3LE;
  174. break;
  175. case SNDRV_PCM_FORMAT_S24_LE:
  176. width = SI476X_PCM_FORMAT_S24_LE;
  177. break;
  178. default:
  179. return -EINVAL;
  180. }
  181. err = snd_soc_write(dai->codec, SI476X_DIGITAL_IO_OUTPUT_SAMPLE_RATE,
  182. rate);
  183. if (err < 0) {
  184. dev_err(dai->codec->dev, "Failed to set sample rate\n");
  185. return err;
  186. }
  187. err = snd_soc_update_bits(dai->codec, SI476X_DIGITAL_IO_OUTPUT_FORMAT,
  188. SI476X_DIGITAL_IO_OUTPUT_WIDTH_MASK,
  189. (width << SI476X_DIGITAL_IO_SLOT_SIZE_SHIFT) |
  190. (width << SI476X_DIGITAL_IO_SAMPLE_SIZE_SHIFT));
  191. if (err < 0) {
  192. dev_err(dai->codec->dev, "Failed to set output width\n");
  193. return err;
  194. }
  195. return 0;
  196. }
  197. static int si476x_codec_probe(struct snd_soc_codec *codec)
  198. {
  199. codec->control_data = i2c_mfd_cell_to_core(codec->dev);
  200. return 0;
  201. }
  202. static struct snd_soc_dai_ops si476x_dai_ops = {
  203. .hw_params = si476x_codec_hw_params,
  204. .set_fmt = si476x_codec_set_dai_fmt,
  205. };
  206. static struct snd_soc_dai_driver si476x_dai = {
  207. .name = "si476x-codec",
  208. .capture = {
  209. .stream_name = "Capture",
  210. .channels_min = 2,
  211. .channels_max = 2,
  212. .rates = SNDRV_PCM_RATE_32000 |
  213. SNDRV_PCM_RATE_44100 |
  214. SNDRV_PCM_RATE_48000,
  215. .formats = SNDRV_PCM_FMTBIT_S8 |
  216. SNDRV_PCM_FMTBIT_S16_LE |
  217. SNDRV_PCM_FMTBIT_S20_3LE |
  218. SNDRV_PCM_FMTBIT_S24_LE
  219. },
  220. .ops = &si476x_dai_ops,
  221. };
  222. static struct snd_soc_codec_driver soc_codec_dev_si476x = {
  223. .probe = si476x_codec_probe,
  224. .read = si476x_codec_read,
  225. .write = si476x_codec_write,
  226. };
  227. static int si476x_platform_probe(struct platform_device *pdev)
  228. {
  229. return snd_soc_register_codec(&pdev->dev, &soc_codec_dev_si476x,
  230. &si476x_dai, 1);
  231. }
  232. static int si476x_platform_remove(struct platform_device *pdev)
  233. {
  234. snd_soc_unregister_codec(&pdev->dev);
  235. return 0;
  236. }
  237. MODULE_ALIAS("platform:si476x-codec");
  238. static struct platform_driver si476x_platform_driver = {
  239. .driver = {
  240. .name = "si476x-codec",
  241. .owner = THIS_MODULE,
  242. },
  243. .probe = si476x_platform_probe,
  244. .remove = si476x_platform_remove,
  245. };
  246. module_platform_driver(si476x_platform_driver);
  247. MODULE_AUTHOR("Andrey Smirnov <andrew.smirnov@gmail.com>");
  248. MODULE_DESCRIPTION("ASoC Si4761/64 codec driver");
  249. MODULE_LICENSE("GPL");