sta529.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  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 DATA_FORMAT_MSK 0x0E
  72. #define LEFT_J_DATA_FORMAT 0x00
  73. #define I2S_DATA_FORMAT 0x02
  74. #define RIGHT_J_DATA_FORMAT 0x04
  75. #define CODEC_MUTE_VAL 0x80
  76. #define POWER_CNTLMSAK 0x40
  77. #define POWER_STDBY 0x40
  78. #define FFX_MASK 0x80
  79. #define FFX_OFF 0x80
  80. #define POWER_UP 0x00
  81. #define FFX_CLK_ENB 0x01
  82. #define FFX_CLK_DIS 0x00
  83. #define FFX_CLK_MSK 0x01
  84. #define PLAY_FREQ_RANGE_MSK 0x70
  85. #define CAP_FREQ_RANGE_MSK 0x0C
  86. #define PDATA_LEN_MSK 0xC0
  87. #define BCLK_TO_FS_MSK 0x30
  88. #define AUDIO_MUTE_MSK 0x80
  89. static const struct reg_default sta529_reg_defaults[] = {
  90. { 0, 0x35 }, /* R0 - FFX Configuration reg 0 */
  91. { 1, 0xc8 }, /* R1 - FFX Configuration reg 1 */
  92. { 2, 0x50 }, /* R2 - Master Volume */
  93. { 3, 0x00 }, /* R3 - Left Volume */
  94. { 4, 0x00 }, /* R4 - Right Volume */
  95. { 10, 0xb2 }, /* R10 - S2P Config Reg 0 */
  96. { 11, 0x41 }, /* R11 - S2P Config Reg 1 */
  97. { 12, 0x92 }, /* R12 - P2S Config Reg 0 */
  98. { 13, 0x41 }, /* R13 - P2S Config Reg 1 */
  99. { 30, 0xd2 }, /* R30 - ADC Config Reg */
  100. { 31, 0x40 }, /* R31 - clock Out Reg */
  101. { 32, 0x21 }, /* R32 - Misc Register */
  102. };
  103. struct sta529 {
  104. struct regmap *regmap;
  105. };
  106. static bool sta529_readable(struct device *dev, unsigned int reg)
  107. {
  108. switch (reg) {
  109. case STA529_FFXCFG0:
  110. case STA529_FFXCFG1:
  111. case STA529_MVOL:
  112. case STA529_LVOL:
  113. case STA529_RVOL:
  114. case STA529_S2PCFG0:
  115. case STA529_S2PCFG1:
  116. case STA529_P2SCFG0:
  117. case STA529_P2SCFG1:
  118. case STA529_ADCCFG:
  119. case STA529_CKOCFG:
  120. case STA529_MISC:
  121. return true;
  122. default:
  123. return false;
  124. }
  125. }
  126. static const char *pwm_mode_text[] = { "Binary", "Headphone", "Ternary",
  127. "Phase-shift"};
  128. static const DECLARE_TLV_DB_SCALE(out_gain_tlv, -9150, 50, 0);
  129. static const DECLARE_TLV_DB_SCALE(master_vol_tlv, -12750, 50, 0);
  130. static const SOC_ENUM_SINGLE_DECL(pwm_src, STA529_FFXCFG1, 4, pwm_mode_text);
  131. static const struct snd_kcontrol_new sta529_snd_controls[] = {
  132. SOC_DOUBLE_R_TLV("Digital Playback Volume", STA529_LVOL, STA529_RVOL, 0,
  133. 127, 0, out_gain_tlv),
  134. SOC_SINGLE_TLV("Master Playback Volume", STA529_MVOL, 0, 127, 1,
  135. master_vol_tlv),
  136. SOC_ENUM("PWM Select", pwm_src),
  137. };
  138. static int sta529_set_bias_level(struct snd_soc_codec *codec, enum
  139. snd_soc_bias_level level)
  140. {
  141. struct sta529 *sta529 = snd_soc_codec_get_drvdata(codec);
  142. switch (level) {
  143. case SND_SOC_BIAS_ON:
  144. case SND_SOC_BIAS_PREPARE:
  145. snd_soc_update_bits(codec, STA529_FFXCFG0, POWER_CNTLMSAK,
  146. POWER_UP);
  147. snd_soc_update_bits(codec, STA529_MISC, FFX_CLK_MSK,
  148. FFX_CLK_ENB);
  149. break;
  150. case SND_SOC_BIAS_STANDBY:
  151. if (codec->dapm.bias_level == SND_SOC_BIAS_OFF)
  152. regcache_sync(sta529->regmap);
  153. snd_soc_update_bits(codec, STA529_FFXCFG0,
  154. POWER_CNTLMSAK, POWER_STDBY);
  155. /* Making FFX output to zero */
  156. snd_soc_update_bits(codec, STA529_FFXCFG0, FFX_MASK,
  157. FFX_OFF);
  158. snd_soc_update_bits(codec, STA529_MISC, FFX_CLK_MSK,
  159. FFX_CLK_DIS);
  160. break;
  161. case SND_SOC_BIAS_OFF:
  162. break;
  163. }
  164. /*
  165. * store the label for powers down audio subsystem for suspend.This is
  166. * used by soc core layer
  167. */
  168. codec->dapm.bias_level = level;
  169. return 0;
  170. }
  171. static int sta529_hw_params(struct snd_pcm_substream *substream,
  172. struct snd_pcm_hw_params *params,
  173. struct snd_soc_dai *dai)
  174. {
  175. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  176. struct snd_soc_codec *codec = rtd->codec;
  177. int pdata, play_freq_val, record_freq_val;
  178. int bclk_to_fs_ratio;
  179. switch (params_format(params)) {
  180. case SNDRV_PCM_FORMAT_S16_LE:
  181. pdata = 1;
  182. bclk_to_fs_ratio = 0;
  183. break;
  184. case SNDRV_PCM_FORMAT_S24_LE:
  185. pdata = 2;
  186. bclk_to_fs_ratio = 1;
  187. break;
  188. case SNDRV_PCM_FORMAT_S32_LE:
  189. pdata = 3;
  190. bclk_to_fs_ratio = 2;
  191. break;
  192. default:
  193. dev_err(codec->dev, "Unsupported format\n");
  194. return -EINVAL;
  195. }
  196. switch (params_rate(params)) {
  197. case 8000:
  198. case 11025:
  199. play_freq_val = 0;
  200. record_freq_val = 2;
  201. break;
  202. case 16000:
  203. case 22050:
  204. play_freq_val = 1;
  205. record_freq_val = 0;
  206. break;
  207. case 32000:
  208. case 44100:
  209. case 48000:
  210. play_freq_val = 2;
  211. record_freq_val = 0;
  212. break;
  213. default:
  214. dev_err(codec->dev, "Unsupported rate\n");
  215. return -EINVAL;
  216. }
  217. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  218. snd_soc_update_bits(codec, STA529_S2PCFG1, PDATA_LEN_MSK,
  219. pdata << 6);
  220. snd_soc_update_bits(codec, STA529_S2PCFG1, BCLK_TO_FS_MSK,
  221. bclk_to_fs_ratio << 4);
  222. snd_soc_update_bits(codec, STA529_MISC, PLAY_FREQ_RANGE_MSK,
  223. play_freq_val << 4);
  224. } else {
  225. snd_soc_update_bits(codec, STA529_P2SCFG1, PDATA_LEN_MSK,
  226. pdata << 6);
  227. snd_soc_update_bits(codec, STA529_P2SCFG1, BCLK_TO_FS_MSK,
  228. bclk_to_fs_ratio << 4);
  229. snd_soc_update_bits(codec, STA529_MISC, CAP_FREQ_RANGE_MSK,
  230. record_freq_val << 2);
  231. }
  232. return 0;
  233. }
  234. static int sta529_mute(struct snd_soc_dai *dai, int mute)
  235. {
  236. u8 val = 0;
  237. if (mute)
  238. val |= CODEC_MUTE_VAL;
  239. snd_soc_update_bits(dai->codec, STA529_FFXCFG0, AUDIO_MUTE_MSK, val);
  240. return 0;
  241. }
  242. static int sta529_set_dai_fmt(struct snd_soc_dai *codec_dai, u32 fmt)
  243. {
  244. struct snd_soc_codec *codec = codec_dai->codec;
  245. u8 mode = 0;
  246. /* interface format */
  247. switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
  248. case SND_SOC_DAIFMT_LEFT_J:
  249. mode = LEFT_J_DATA_FORMAT;
  250. break;
  251. case SND_SOC_DAIFMT_I2S:
  252. mode = I2S_DATA_FORMAT;
  253. break;
  254. case SND_SOC_DAIFMT_RIGHT_J:
  255. mode = RIGHT_J_DATA_FORMAT;
  256. break;
  257. default:
  258. return -EINVAL;
  259. }
  260. snd_soc_update_bits(codec, STA529_S2PCFG0, DATA_FORMAT_MSK, mode);
  261. return 0;
  262. }
  263. static const struct snd_soc_dai_ops sta529_dai_ops = {
  264. .hw_params = sta529_hw_params,
  265. .set_fmt = sta529_set_dai_fmt,
  266. .digital_mute = sta529_mute,
  267. };
  268. static struct snd_soc_dai_driver sta529_dai = {
  269. .name = "sta529-audio",
  270. .playback = {
  271. .stream_name = "Playback",
  272. .channels_min = 2,
  273. .channels_max = 2,
  274. .rates = STA529_RATES,
  275. .formats = STA529_FORMAT,
  276. },
  277. .capture = {
  278. .stream_name = "Capture",
  279. .channels_min = 2,
  280. .channels_max = 2,
  281. .rates = STA529_RATES,
  282. .formats = STA529_FORMAT,
  283. },
  284. .ops = &sta529_dai_ops,
  285. };
  286. static int sta529_probe(struct snd_soc_codec *codec)
  287. {
  288. struct sta529 *sta529 = snd_soc_codec_get_drvdata(codec);
  289. int ret;
  290. codec->control_data = sta529->regmap;
  291. ret = snd_soc_codec_set_cache_io(codec, 8, 8, SND_SOC_REGMAP);
  292. if (ret < 0) {
  293. dev_err(codec->dev, "Failed to set cache I/O: %d\n", ret);
  294. return ret;
  295. }
  296. sta529_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
  297. return 0;
  298. }
  299. /* power down chip */
  300. static int sta529_remove(struct snd_soc_codec *codec)
  301. {
  302. sta529_set_bias_level(codec, SND_SOC_BIAS_OFF);
  303. return 0;
  304. }
  305. static int sta529_suspend(struct snd_soc_codec *codec)
  306. {
  307. sta529_set_bias_level(codec, SND_SOC_BIAS_OFF);
  308. return 0;
  309. }
  310. static int sta529_resume(struct snd_soc_codec *codec)
  311. {
  312. sta529_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
  313. return 0;
  314. }
  315. static const struct snd_soc_codec_driver sta529_codec_driver = {
  316. .probe = sta529_probe,
  317. .remove = sta529_remove,
  318. .set_bias_level = sta529_set_bias_level,
  319. .suspend = sta529_suspend,
  320. .resume = sta529_resume,
  321. .controls = sta529_snd_controls,
  322. .num_controls = ARRAY_SIZE(sta529_snd_controls),
  323. };
  324. static const struct regmap_config sta529_regmap = {
  325. .reg_bits = 8,
  326. .val_bits = 8,
  327. .max_register = STA529_MAX_REGISTER,
  328. .readable_reg = sta529_readable,
  329. .cache_type = REGCACHE_RBTREE,
  330. .reg_defaults = sta529_reg_defaults,
  331. .num_reg_defaults = ARRAY_SIZE(sta529_reg_defaults),
  332. };
  333. static int sta529_i2c_probe(struct i2c_client *i2c,
  334. const struct i2c_device_id *id)
  335. {
  336. struct sta529 *sta529;
  337. int ret;
  338. if (!i2c_check_functionality(i2c->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  339. return -EINVAL;
  340. sta529 = devm_kzalloc(&i2c->dev, sizeof(struct sta529), GFP_KERNEL);
  341. if (sta529 == NULL) {
  342. dev_err(&i2c->dev, "Can not allocate memory\n");
  343. return -ENOMEM;
  344. }
  345. sta529->regmap = devm_regmap_init_i2c(i2c, &sta529_regmap);
  346. if (IS_ERR(sta529->regmap)) {
  347. ret = PTR_ERR(sta529->regmap);
  348. dev_err(&i2c->dev, "Failed to allocate regmap: %d\n", ret);
  349. return ret;
  350. }
  351. i2c_set_clientdata(i2c, sta529);
  352. ret = snd_soc_register_codec(&i2c->dev,
  353. &sta529_codec_driver, &sta529_dai, 1);
  354. if (ret != 0)
  355. dev_err(&i2c->dev, "Failed to register CODEC: %d\n", ret);
  356. return ret;
  357. }
  358. static int sta529_i2c_remove(struct i2c_client *client)
  359. {
  360. snd_soc_unregister_codec(&client->dev);
  361. return 0;
  362. }
  363. static const struct i2c_device_id sta529_i2c_id[] = {
  364. { "sta529", 0 },
  365. { }
  366. };
  367. MODULE_DEVICE_TABLE(i2c, sta529_i2c_id);
  368. static struct i2c_driver sta529_i2c_driver = {
  369. .driver = {
  370. .name = "sta529",
  371. .owner = THIS_MODULE,
  372. },
  373. .probe = sta529_i2c_probe,
  374. .remove = sta529_i2c_remove,
  375. .id_table = sta529_i2c_id,
  376. };
  377. module_i2c_driver(sta529_i2c_driver);
  378. MODULE_DESCRIPTION("ASoC STA529 codec driver");
  379. MODULE_AUTHOR("Rajeev Kumar <rajeev-dlh.kumar@st.com>");
  380. MODULE_LICENSE("GPL");