si476x.c 7.4 KB

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