sta529.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. /*
  2. * ASoC codec driver for spear platform
  3. *
  4. * sound/soc/codecs/sta529.c -- spear ALSA Soc codec driver
  5. *
  6. * Copyright (C) 2012 ST Microelectronics
  7. * Rajeev Kumar <rajeev-dlh.kumar@st.com>
  8. *
  9. * This file is licensed under the terms of the GNU General Public
  10. * License version 2. This program is licensed "as is" without any
  11. * warranty of any kind, whether express or implied.
  12. */
  13. #include <linux/clk.h>
  14. #include <linux/init.h>
  15. #include <linux/i2c.h>
  16. #include <linux/io.h>
  17. #include <linux/module.h>
  18. #include <linux/moduleparam.h>
  19. #include <linux/pm.h>
  20. #include <linux/regmap.h>
  21. #include <linux/slab.h>
  22. #include <sound/core.h>
  23. #include <sound/initval.h>
  24. #include <sound/pcm.h>
  25. #include <sound/pcm_params.h>
  26. #include <sound/soc.h>
  27. #include <sound/soc-dapm.h>
  28. #include <sound/tlv.h>
  29. /* STA529 Register offsets */
  30. #define STA529_FFXCFG0 0x00
  31. #define STA529_FFXCFG1 0x01
  32. #define STA529_MVOL 0x02
  33. #define STA529_LVOL 0x03
  34. #define STA529_RVOL 0x04
  35. #define STA529_TTF0 0x05
  36. #define STA529_TTF1 0x06
  37. #define STA529_TTP0 0x07
  38. #define STA529_TTP1 0x08
  39. #define STA529_S2PCFG0 0x0A
  40. #define STA529_S2PCFG1 0x0B
  41. #define STA529_P2SCFG0 0x0C
  42. #define STA529_P2SCFG1 0x0D
  43. #define STA529_PLLCFG0 0x14
  44. #define STA529_PLLCFG1 0x15
  45. #define STA529_PLLCFG2 0x16
  46. #define STA529_PLLCFG3 0x17
  47. #define STA529_PLLPFE 0x18
  48. #define STA529_PLLST 0x19
  49. #define STA529_ADCCFG 0x1E /*mic_select*/
  50. #define STA529_CKOCFG 0x1F
  51. #define STA529_MISC 0x20
  52. #define STA529_PADST0 0x21
  53. #define STA529_PADST1 0x22
  54. #define STA529_FFXST 0x23
  55. #define STA529_PWMIN1 0x2D
  56. #define STA529_PWMIN2 0x2E
  57. #define STA529_POWST 0x32
  58. #define STA529_MAX_REGISTER 0x32
  59. #define STA529_RATES (SNDRV_PCM_RATE_8000 | \
  60. SNDRV_PCM_RATE_11025 | \
  61. SNDRV_PCM_RATE_16000 | \
  62. SNDRV_PCM_RATE_22050 | \
  63. SNDRV_PCM_RATE_32000 | \
  64. SNDRV_PCM_RATE_44100 | \
  65. SNDRV_PCM_RATE_48000)
  66. #define STA529_FORMAT (SNDRV_PCM_FMTBIT_S16_LE | \
  67. SNDRV_PCM_FMTBIT_S24_LE | \
  68. SNDRV_PCM_FMTBIT_S32_LE)
  69. #define S2PC_VALUE 0x98
  70. #define CLOCK_OUT 0x60
  71. #define LEFT_J_DATA_FORMAT 0x10
  72. #define I2S_DATA_FORMAT 0x12
  73. #define RIGHT_J_DATA_FORMAT 0x14
  74. #define CODEC_MUTE_VAL 0x80
  75. #define POWER_CNTLMSAK 0x40
  76. #define POWER_STDBY 0x40
  77. #define FFX_MASK 0x80
  78. #define FFX_OFF 0x80
  79. #define POWER_UP 0x00
  80. #define FFX_CLK_ENB 0x01
  81. #define FFX_CLK_DIS 0x00
  82. #define FFX_CLK_MSK 0x01
  83. #define PLAY_FREQ_RANGE_MSK 0x70
  84. #define CAP_FREQ_RANGE_MSK 0x0C
  85. #define PDATA_LEN_MSK 0xC0
  86. #define BCLK_TO_FS_MSK 0x30
  87. #define AUDIO_MUTE_MSK 0x80
  88. static const struct reg_default sta529_reg_defaults[] = {
  89. { 0, 0x35 }, /* R0 - FFX Configuration reg 0 */
  90. { 1, 0xc8 }, /* R1 - FFX Configuration reg 1 */
  91. { 2, 0x50 }, /* R2 - Master Volume */
  92. { 3, 0x00 }, /* R3 - Left Volume */
  93. { 4, 0x00 }, /* R4 - Right Volume */
  94. { 10, 0xb2 }, /* R10 - S2P Config Reg 0 */
  95. { 11, 0x41 }, /* R11 - S2P Config Reg 1 */
  96. { 12, 0x92 }, /* R12 - P2S Config Reg 0 */
  97. { 13, 0x41 }, /* R13 - P2S Config Reg 1 */
  98. { 30, 0xd2 }, /* R30 - ADC Config Reg */
  99. { 31, 0x40 }, /* R31 - clock Out Reg */
  100. { 32, 0x21 }, /* R32 - Misc Register */
  101. };
  102. struct sta529 {
  103. struct regmap *regmap;
  104. };
  105. static bool sta529_readable(struct device *dev, unsigned int reg)
  106. {
  107. switch (reg) {
  108. case STA529_FFXCFG0:
  109. case STA529_FFXCFG1:
  110. case STA529_MVOL:
  111. case STA529_LVOL:
  112. case STA529_RVOL:
  113. case STA529_S2PCFG0:
  114. case STA529_S2PCFG1:
  115. case STA529_P2SCFG0:
  116. case STA529_P2SCFG1:
  117. case STA529_ADCCFG:
  118. case STA529_CKOCFG:
  119. case STA529_MISC:
  120. return true;
  121. default:
  122. return false;
  123. }
  124. }
  125. static const char *pwm_mode_text[] = { "Binary", "Headphone", "Ternary",
  126. "Phase-shift"};
  127. static const DECLARE_TLV_DB_SCALE(out_gain_tlv, -9150, 50, 0);
  128. static const DECLARE_TLV_DB_SCALE(master_vol_tlv, -12750, 50, 0);
  129. static const SOC_ENUM_SINGLE_DECL(pwm_src, STA529_FFXCFG1, 4, pwm_mode_text);
  130. static const struct snd_kcontrol_new sta529_snd_controls[] = {
  131. SOC_DOUBLE_R_TLV("Digital Playback Volume", STA529_LVOL, STA529_RVOL, 0,
  132. 127, 0, out_gain_tlv),
  133. SOC_SINGLE_TLV("Master Playback Volume", STA529_MVOL, 0, 127, 1,
  134. master_vol_tlv),
  135. SOC_ENUM("PWM Select", pwm_src),
  136. };
  137. static int sta529_set_bias_level(struct snd_soc_codec *codec, enum
  138. snd_soc_bias_level level)
  139. {
  140. struct sta529 *sta529 = snd_soc_codec_get_drvdata(codec);
  141. switch (level) {
  142. case SND_SOC_BIAS_ON:
  143. case SND_SOC_BIAS_PREPARE:
  144. snd_soc_update_bits(codec, STA529_FFXCFG0, POWER_CNTLMSAK,
  145. POWER_UP);
  146. snd_soc_update_bits(codec, STA529_MISC, FFX_CLK_MSK,
  147. FFX_CLK_ENB);
  148. break;
  149. case SND_SOC_BIAS_STANDBY:
  150. if (codec->dapm.bias_level == SND_SOC_BIAS_OFF)
  151. regcache_sync(sta529->regmap);
  152. snd_soc_update_bits(codec, STA529_FFXCFG0,
  153. POWER_CNTLMSAK, POWER_STDBY);
  154. /* Making FFX output to zero */
  155. snd_soc_update_bits(codec, STA529_FFXCFG0, FFX_MASK,
  156. FFX_OFF);
  157. snd_soc_update_bits(codec, STA529_MISC, FFX_CLK_MSK,
  158. FFX_CLK_DIS);
  159. break;
  160. case SND_SOC_BIAS_OFF:
  161. break;
  162. }
  163. /*
  164. * store the label for powers down audio subsystem for suspend.This is
  165. * used by soc core layer
  166. */
  167. codec->dapm.bias_level = level;
  168. return 0;
  169. }
  170. static int sta529_hw_params(struct snd_pcm_substream *substream,
  171. struct snd_pcm_hw_params *params,
  172. struct snd_soc_dai *dai)
  173. {
  174. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  175. struct snd_soc_codec *codec = rtd->codec;
  176. int pdata, play_freq_val, record_freq_val;
  177. int bclk_to_fs_ratio;
  178. switch (params_format(params)) {
  179. case SNDRV_PCM_FORMAT_S16_LE:
  180. pdata = 1;
  181. bclk_to_fs_ratio = 0;
  182. break;
  183. case SNDRV_PCM_FORMAT_S24_LE:
  184. pdata = 2;
  185. bclk_to_fs_ratio = 1;
  186. break;
  187. case SNDRV_PCM_FORMAT_S32_LE:
  188. pdata = 3;
  189. bclk_to_fs_ratio = 2;
  190. break;
  191. default:
  192. dev_err(codec->dev, "Unsupported format\n");
  193. return -EINVAL;
  194. }
  195. switch (params_rate(params)) {
  196. case 8000:
  197. case 11025:
  198. play_freq_val = 0;
  199. record_freq_val = 2;
  200. break;
  201. case 16000:
  202. case 22050:
  203. play_freq_val = 1;
  204. record_freq_val = 0;
  205. break;
  206. case 32000:
  207. case 44100:
  208. case 48000:
  209. play_freq_val = 2;
  210. record_freq_val = 0;
  211. break;
  212. default:
  213. dev_err(codec->dev, "Unsupported rate\n");
  214. return -EINVAL;
  215. }
  216. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  217. snd_soc_update_bits(codec, STA529_S2PCFG1, PDATA_LEN_MSK,
  218. pdata << 6);
  219. snd_soc_update_bits(codec, STA529_S2PCFG1, BCLK_TO_FS_MSK,
  220. bclk_to_fs_ratio << 4);
  221. snd_soc_update_bits(codec, STA529_MISC, PLAY_FREQ_RANGE_MSK,
  222. play_freq_val << 4);
  223. } else {
  224. snd_soc_update_bits(codec, STA529_P2SCFG1, PDATA_LEN_MSK,
  225. pdata << 6);
  226. snd_soc_update_bits(codec, STA529_P2SCFG1, BCLK_TO_FS_MSK,
  227. bclk_to_fs_ratio << 4);
  228. snd_soc_update_bits(codec, STA529_MISC, CAP_FREQ_RANGE_MSK,
  229. record_freq_val << 2);
  230. }
  231. return 0;
  232. }
  233. static int sta529_mute(struct snd_soc_dai *dai, int mute)
  234. {
  235. u8 val = 0;
  236. if (mute)
  237. val |= CODEC_MUTE_VAL;
  238. snd_soc_update_bits(dai->codec, STA529_FFXCFG0, AUDIO_MUTE_MSK, val);
  239. return 0;
  240. }
  241. static int sta529_set_dai_fmt(struct snd_soc_dai *codec_dai, u32 fmt)
  242. {
  243. struct snd_soc_codec *codec = codec_dai->codec;
  244. u8 mode = 0;
  245. /* interface format */
  246. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  247. case SND_SOC_DAIFMT_LEFT_J:
  248. mode = LEFT_J_DATA_FORMAT;
  249. break;
  250. case SND_SOC_DAIFMT_I2S:
  251. mode = I2S_DATA_FORMAT;
  252. break;
  253. case SND_SOC_DAIFMT_RIGHT_J:
  254. mode = RIGHT_J_DATA_FORMAT;
  255. break;
  256. default:
  257. return -EINVAL;
  258. }
  259. snd_soc_update_bits(codec, STA529_S2PCFG0, 0x0D, mode);
  260. return 0;
  261. }
  262. static const struct snd_soc_dai_ops sta529_dai_ops = {
  263. .hw_params = sta529_hw_params,
  264. .set_fmt = sta529_set_dai_fmt,
  265. .digital_mute = sta529_mute,
  266. };
  267. static struct snd_soc_dai_driver sta529_dai = {
  268. .name = "sta529-audio",
  269. .playback = {
  270. .stream_name = "Playback",
  271. .channels_min = 2,
  272. .channels_max = 2,
  273. .rates = STA529_RATES,
  274. .formats = STA529_FORMAT,
  275. },
  276. .capture = {
  277. .stream_name = "Capture",
  278. .channels_min = 2,
  279. .channels_max = 2,
  280. .rates = STA529_RATES,
  281. .formats = STA529_FORMAT,
  282. },
  283. .ops = &sta529_dai_ops,
  284. };
  285. static int sta529_probe(struct snd_soc_codec *codec)
  286. {
  287. struct sta529 *sta529 = snd_soc_codec_get_drvdata(codec);
  288. int ret;
  289. codec->control_data = sta529->regmap;
  290. ret = snd_soc_codec_set_cache_io(codec, 8, 8, SND_SOC_REGMAP);
  291. if (ret < 0) {
  292. dev_err(codec->dev, "Failed to set cache I/O: %d\n", ret);
  293. return ret;
  294. }
  295. sta529_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
  296. return 0;
  297. }
  298. /* power down chip */
  299. static int sta529_remove(struct snd_soc_codec *codec)
  300. {
  301. sta529_set_bias_level(codec, SND_SOC_BIAS_OFF);
  302. return 0;
  303. }
  304. static int sta529_suspend(struct snd_soc_codec *codec)
  305. {
  306. sta529_set_bias_level(codec, SND_SOC_BIAS_OFF);
  307. return 0;
  308. }
  309. static int sta529_resume(struct snd_soc_codec *codec)
  310. {
  311. sta529_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
  312. return 0;
  313. }
  314. static const struct snd_soc_codec_driver sta529_codec_driver = {
  315. .probe = sta529_probe,
  316. .remove = sta529_remove,
  317. .set_bias_level = sta529_set_bias_level,
  318. .suspend = sta529_suspend,
  319. .resume = sta529_resume,
  320. .controls = sta529_snd_controls,
  321. .num_controls = ARRAY_SIZE(sta529_snd_controls),
  322. };
  323. static const struct regmap_config sta529_regmap = {
  324. .reg_bits = 8,
  325. .val_bits = 8,
  326. .max_register = STA529_MAX_REGISTER,
  327. .readable_reg = sta529_readable,
  328. .cache_type = REGCACHE_RBTREE,
  329. .reg_defaults = sta529_reg_defaults,
  330. .num_reg_defaults = ARRAY_SIZE(sta529_reg_defaults),
  331. };
  332. static int sta529_i2c_probe(struct i2c_client *i2c,
  333. const struct i2c_device_id *id)
  334. {
  335. struct sta529 *sta529;
  336. int ret;
  337. if (!i2c_check_functionality(i2c->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  338. return -EINVAL;
  339. sta529 = devm_kzalloc(&i2c->dev, sizeof(struct sta529), GFP_KERNEL);
  340. if (sta529 == NULL) {
  341. dev_err(&i2c->dev, "Can not allocate memory\n");
  342. return -ENOMEM;
  343. }
  344. sta529->regmap = devm_regmap_init_i2c(i2c, &sta529_regmap);
  345. if (IS_ERR(sta529->regmap)) {
  346. ret = PTR_ERR(sta529->regmap);
  347. dev_err(&i2c->dev, "Failed to allocate regmap: %d\n", ret);
  348. return ret;
  349. }
  350. i2c_set_clientdata(i2c, sta529);
  351. ret = snd_soc_register_codec(&i2c->dev,
  352. &sta529_codec_driver, &sta529_dai, 1);
  353. if (ret != 0)
  354. dev_err(&i2c->dev, "Failed to register CODEC: %d\n", ret);
  355. return ret;
  356. }
  357. static int sta529_i2c_remove(struct i2c_client *client)
  358. {
  359. snd_soc_unregister_codec(&client->dev);
  360. return 0;
  361. }
  362. static const struct i2c_device_id sta529_i2c_id[] = {
  363. { "sta529", 0 },
  364. { }
  365. };
  366. MODULE_DEVICE_TABLE(i2c, sta529_i2c_id);
  367. static struct i2c_driver sta529_i2c_driver = {
  368. .driver = {
  369. .name = "sta529",
  370. .owner = THIS_MODULE,
  371. },
  372. .probe = sta529_i2c_probe,
  373. .remove = sta529_i2c_remove,
  374. .id_table = sta529_i2c_id,
  375. };
  376. module_i2c_driver(sta529_i2c_driver);
  377. MODULE_DESCRIPTION("ASoC STA529 codec driver");
  378. MODULE_AUTHOR("Rajeev Kumar <rajeev-dlh.kumar@st.com>");
  379. MODULE_LICENSE("GPL");