sta529.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  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 <rajeevkumar.linux@gmail.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 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_codec *codec = dai->codec;
  176. int pdata, play_freq_val, record_freq_val;
  177. int bclk_to_fs_ratio;
  178. switch (params_width(params)) {
  179. case 16:
  180. pdata = 1;
  181. bclk_to_fs_ratio = 0;
  182. break;
  183. case 24:
  184. pdata = 2;
  185. bclk_to_fs_ratio = 1;
  186. break;
  187. case 32:
  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, DATA_FORMAT_MSK, 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. sta529_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
  288. return 0;
  289. }
  290. /* power down chip */
  291. static int sta529_remove(struct snd_soc_codec *codec)
  292. {
  293. sta529_set_bias_level(codec, SND_SOC_BIAS_OFF);
  294. return 0;
  295. }
  296. static int sta529_suspend(struct snd_soc_codec *codec)
  297. {
  298. sta529_set_bias_level(codec, SND_SOC_BIAS_OFF);
  299. return 0;
  300. }
  301. static int sta529_resume(struct snd_soc_codec *codec)
  302. {
  303. sta529_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
  304. return 0;
  305. }
  306. static const struct snd_soc_codec_driver sta529_codec_driver = {
  307. .probe = sta529_probe,
  308. .remove = sta529_remove,
  309. .set_bias_level = sta529_set_bias_level,
  310. .suspend = sta529_suspend,
  311. .resume = sta529_resume,
  312. .controls = sta529_snd_controls,
  313. .num_controls = ARRAY_SIZE(sta529_snd_controls),
  314. };
  315. static const struct regmap_config sta529_regmap = {
  316. .reg_bits = 8,
  317. .val_bits = 8,
  318. .max_register = STA529_MAX_REGISTER,
  319. .readable_reg = sta529_readable,
  320. .cache_type = REGCACHE_RBTREE,
  321. .reg_defaults = sta529_reg_defaults,
  322. .num_reg_defaults = ARRAY_SIZE(sta529_reg_defaults),
  323. };
  324. static int sta529_i2c_probe(struct i2c_client *i2c,
  325. const struct i2c_device_id *id)
  326. {
  327. struct sta529 *sta529;
  328. int ret;
  329. if (!i2c_check_functionality(i2c->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  330. return -EINVAL;
  331. sta529 = devm_kzalloc(&i2c->dev, sizeof(struct sta529), GFP_KERNEL);
  332. if (!sta529)
  333. return -ENOMEM;
  334. sta529->regmap = devm_regmap_init_i2c(i2c, &sta529_regmap);
  335. if (IS_ERR(sta529->regmap)) {
  336. ret = PTR_ERR(sta529->regmap);
  337. dev_err(&i2c->dev, "Failed to allocate regmap: %d\n", ret);
  338. return ret;
  339. }
  340. i2c_set_clientdata(i2c, sta529);
  341. ret = snd_soc_register_codec(&i2c->dev,
  342. &sta529_codec_driver, &sta529_dai, 1);
  343. if (ret != 0)
  344. dev_err(&i2c->dev, "Failed to register CODEC: %d\n", ret);
  345. return ret;
  346. }
  347. static int sta529_i2c_remove(struct i2c_client *client)
  348. {
  349. snd_soc_unregister_codec(&client->dev);
  350. return 0;
  351. }
  352. static const struct i2c_device_id sta529_i2c_id[] = {
  353. { "sta529", 0 },
  354. { }
  355. };
  356. MODULE_DEVICE_TABLE(i2c, sta529_i2c_id);
  357. static struct i2c_driver sta529_i2c_driver = {
  358. .driver = {
  359. .name = "sta529",
  360. .owner = THIS_MODULE,
  361. },
  362. .probe = sta529_i2c_probe,
  363. .remove = sta529_i2c_remove,
  364. .id_table = sta529_i2c_id,
  365. };
  366. module_i2c_driver(sta529_i2c_driver);
  367. MODULE_DESCRIPTION("ASoC STA529 codec driver");
  368. MODULE_AUTHOR("Rajeev Kumar <rajeevkumar.linux@gmail.com>");
  369. MODULE_LICENSE("GPL");