atmel-classd.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679
  1. /* Atmel ALSA SoC Audio Class D Amplifier (CLASSD) driver
  2. *
  3. * Copyright (C) 2015 Atmel
  4. *
  5. * Author: Songjun Wu <songjun.wu@atmel.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 or later
  9. * as published by the Free Software Foundation.
  10. */
  11. #include <linux/of.h>
  12. #include <linux/clk.h>
  13. #include <linux/module.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/regmap.h>
  16. #include <sound/core.h>
  17. #include <sound/dmaengine_pcm.h>
  18. #include <sound/pcm_params.h>
  19. #include <sound/tlv.h>
  20. #include "atmel-classd.h"
  21. struct atmel_classd_pdata {
  22. bool non_overlap_enable;
  23. int non_overlap_time;
  24. int pwm_type;
  25. const char *card_name;
  26. };
  27. struct atmel_classd {
  28. dma_addr_t phy_base;
  29. struct regmap *regmap;
  30. struct clk *pclk;
  31. struct clk *gclk;
  32. struct clk *aclk;
  33. int irq;
  34. const struct atmel_classd_pdata *pdata;
  35. };
  36. #ifdef CONFIG_OF
  37. static const struct of_device_id atmel_classd_of_match[] = {
  38. {
  39. .compatible = "atmel,sama5d2-classd",
  40. }, {
  41. /* sentinel */
  42. }
  43. };
  44. MODULE_DEVICE_TABLE(of, atmel_classd_of_match);
  45. static struct atmel_classd_pdata *atmel_classd_dt_init(struct device *dev)
  46. {
  47. struct device_node *np = dev->of_node;
  48. struct atmel_classd_pdata *pdata;
  49. const char *pwm_type;
  50. int ret;
  51. if (!np) {
  52. dev_err(dev, "device node not found\n");
  53. return ERR_PTR(-EINVAL);
  54. }
  55. pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
  56. if (!pdata)
  57. return ERR_PTR(-ENOMEM);
  58. ret = of_property_read_string(np, "atmel,pwm-type", &pwm_type);
  59. if ((ret == 0) && (strcmp(pwm_type, "diff") == 0))
  60. pdata->pwm_type = CLASSD_MR_PWMTYP_DIFF;
  61. else
  62. pdata->pwm_type = CLASSD_MR_PWMTYP_SINGLE;
  63. ret = of_property_read_u32(np,
  64. "atmel,non-overlap-time", &pdata->non_overlap_time);
  65. if (ret)
  66. pdata->non_overlap_enable = false;
  67. else
  68. pdata->non_overlap_enable = true;
  69. ret = of_property_read_string(np, "atmel,model", &pdata->card_name);
  70. if (ret)
  71. pdata->card_name = "CLASSD";
  72. return pdata;
  73. }
  74. #else
  75. static inline struct atmel_classd_pdata *
  76. atmel_classd_dt_init(struct device *dev)
  77. {
  78. return ERR_PTR(-EINVAL);
  79. }
  80. #endif
  81. #define ATMEL_CLASSD_RATES (SNDRV_PCM_RATE_8000 \
  82. | SNDRV_PCM_RATE_16000 | SNDRV_PCM_RATE_22050 \
  83. | SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 \
  84. | SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_88200 \
  85. | SNDRV_PCM_RATE_96000)
  86. static const struct snd_pcm_hardware atmel_classd_hw = {
  87. .info = SNDRV_PCM_INFO_MMAP
  88. | SNDRV_PCM_INFO_MMAP_VALID
  89. | SNDRV_PCM_INFO_INTERLEAVED
  90. | SNDRV_PCM_INFO_RESUME
  91. | SNDRV_PCM_INFO_PAUSE,
  92. .formats = (SNDRV_PCM_FMTBIT_S16_LE),
  93. .rates = ATMEL_CLASSD_RATES,
  94. .rate_min = 8000,
  95. .rate_max = 96000,
  96. .channels_min = 2,
  97. .channels_max = 2,
  98. .buffer_bytes_max = 64 * 1024,
  99. .period_bytes_min = 256,
  100. .period_bytes_max = 32 * 1024,
  101. .periods_min = 2,
  102. .periods_max = 256,
  103. };
  104. #define ATMEL_CLASSD_PREALLOC_BUF_SIZE (64 * 1024)
  105. /* cpu dai component */
  106. static int atmel_classd_cpu_dai_startup(struct snd_pcm_substream *substream,
  107. struct snd_soc_dai *cpu_dai)
  108. {
  109. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  110. struct atmel_classd *dd = snd_soc_card_get_drvdata(rtd->card);
  111. regmap_write(dd->regmap, CLASSD_THR, 0x0);
  112. return clk_prepare_enable(dd->pclk);
  113. }
  114. static void atmel_classd_cpu_dai_shutdown(struct snd_pcm_substream *substream,
  115. struct snd_soc_dai *cpu_dai)
  116. {
  117. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  118. struct atmel_classd *dd = snd_soc_card_get_drvdata(rtd->card);
  119. clk_disable_unprepare(dd->pclk);
  120. }
  121. static const struct snd_soc_dai_ops atmel_classd_cpu_dai_ops = {
  122. .startup = atmel_classd_cpu_dai_startup,
  123. .shutdown = atmel_classd_cpu_dai_shutdown,
  124. };
  125. static struct snd_soc_dai_driver atmel_classd_cpu_dai = {
  126. .playback = {
  127. .channels_min = 2,
  128. .channels_max = 2,
  129. .rates = ATMEL_CLASSD_RATES,
  130. .formats = SNDRV_PCM_FMTBIT_S16_LE,},
  131. .ops = &atmel_classd_cpu_dai_ops,
  132. };
  133. static const struct snd_soc_component_driver atmel_classd_cpu_dai_component = {
  134. .name = "atmel-classd",
  135. };
  136. /* platform */
  137. static int
  138. atmel_classd_platform_configure_dma(struct snd_pcm_substream *substream,
  139. struct snd_pcm_hw_params *params,
  140. struct dma_slave_config *slave_config)
  141. {
  142. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  143. struct atmel_classd *dd = snd_soc_card_get_drvdata(rtd->card);
  144. if (params_physical_width(params) != 16) {
  145. dev_err(rtd->platform->dev,
  146. "only supports 16-bit audio data\n");
  147. return -EINVAL;
  148. }
  149. slave_config->direction = DMA_MEM_TO_DEV;
  150. slave_config->dst_addr = dd->phy_base + CLASSD_THR;
  151. slave_config->dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  152. slave_config->dst_maxburst = 1;
  153. slave_config->src_maxburst = 1;
  154. slave_config->device_fc = false;
  155. return 0;
  156. }
  157. static const struct snd_dmaengine_pcm_config
  158. atmel_classd_dmaengine_pcm_config = {
  159. .prepare_slave_config = atmel_classd_platform_configure_dma,
  160. .pcm_hardware = &atmel_classd_hw,
  161. .prealloc_buffer_size = ATMEL_CLASSD_PREALLOC_BUF_SIZE,
  162. };
  163. /* codec */
  164. static const char * const mono_mode_text[] = {
  165. "mix", "sat", "left", "right"
  166. };
  167. static SOC_ENUM_SINGLE_DECL(classd_mono_mode_enum,
  168. CLASSD_INTPMR, CLASSD_INTPMR_MONO_MODE_SHIFT,
  169. mono_mode_text);
  170. static const char * const eqcfg_text[] = {
  171. "Treble-12dB", "Treble-6dB",
  172. "Medium-8dB", "Medium-3dB",
  173. "Bass-12dB", "Bass-6dB",
  174. "0 dB",
  175. "Bass+6dB", "Bass+12dB",
  176. "Medium+3dB", "Medium+8dB",
  177. "Treble+6dB", "Treble+12dB",
  178. };
  179. static const unsigned int eqcfg_value[] = {
  180. CLASSD_INTPMR_EQCFG_T_CUT_12, CLASSD_INTPMR_EQCFG_T_CUT_6,
  181. CLASSD_INTPMR_EQCFG_M_CUT_8, CLASSD_INTPMR_EQCFG_M_CUT_3,
  182. CLASSD_INTPMR_EQCFG_B_CUT_12, CLASSD_INTPMR_EQCFG_B_CUT_6,
  183. CLASSD_INTPMR_EQCFG_FLAT,
  184. CLASSD_INTPMR_EQCFG_B_BOOST_6, CLASSD_INTPMR_EQCFG_B_BOOST_12,
  185. CLASSD_INTPMR_EQCFG_M_BOOST_3, CLASSD_INTPMR_EQCFG_M_BOOST_8,
  186. CLASSD_INTPMR_EQCFG_T_BOOST_6, CLASSD_INTPMR_EQCFG_T_BOOST_12,
  187. };
  188. static SOC_VALUE_ENUM_SINGLE_DECL(classd_eqcfg_enum,
  189. CLASSD_INTPMR, CLASSD_INTPMR_EQCFG_SHIFT, 0xf,
  190. eqcfg_text, eqcfg_value);
  191. static const DECLARE_TLV_DB_SCALE(classd_digital_tlv, -7800, 100, 1);
  192. static const struct snd_kcontrol_new atmel_classd_snd_controls[] = {
  193. SOC_DOUBLE_TLV("Playback Volume", CLASSD_INTPMR,
  194. CLASSD_INTPMR_ATTL_SHIFT, CLASSD_INTPMR_ATTR_SHIFT,
  195. 78, 1, classd_digital_tlv),
  196. SOC_SINGLE("Deemphasis Switch", CLASSD_INTPMR,
  197. CLASSD_INTPMR_DEEMP_SHIFT, 1, 0),
  198. SOC_SINGLE("Mono Switch", CLASSD_INTPMR, CLASSD_INTPMR_MONO_SHIFT, 1, 0),
  199. SOC_SINGLE("Swap Switch", CLASSD_INTPMR, CLASSD_INTPMR_SWAP_SHIFT, 1, 0),
  200. SOC_ENUM("Mono Mode", classd_mono_mode_enum),
  201. SOC_ENUM("EQ", classd_eqcfg_enum),
  202. };
  203. static const char * const pwm_type[] = {
  204. "Single ended", "Differential"
  205. };
  206. static int atmel_classd_codec_probe(struct snd_soc_codec *codec)
  207. {
  208. struct snd_soc_card *card = snd_soc_codec_get_drvdata(codec);
  209. struct atmel_classd *dd = snd_soc_card_get_drvdata(card);
  210. const struct atmel_classd_pdata *pdata = dd->pdata;
  211. u32 mask, val;
  212. mask = CLASSD_MR_PWMTYP_MASK;
  213. val = pdata->pwm_type << CLASSD_MR_PWMTYP_SHIFT;
  214. mask |= CLASSD_MR_NON_OVERLAP_MASK;
  215. if (pdata->non_overlap_enable) {
  216. val |= (CLASSD_MR_NON_OVERLAP_EN
  217. << CLASSD_MR_NON_OVERLAP_SHIFT);
  218. mask |= CLASSD_MR_NOVR_VAL_MASK;
  219. switch (pdata->non_overlap_time) {
  220. case 5:
  221. val |= (CLASSD_MR_NOVR_VAL_5NS
  222. << CLASSD_MR_NOVR_VAL_SHIFT);
  223. break;
  224. case 10:
  225. val |= (CLASSD_MR_NOVR_VAL_10NS
  226. << CLASSD_MR_NOVR_VAL_SHIFT);
  227. break;
  228. case 15:
  229. val |= (CLASSD_MR_NOVR_VAL_15NS
  230. << CLASSD_MR_NOVR_VAL_SHIFT);
  231. break;
  232. case 20:
  233. val |= (CLASSD_MR_NOVR_VAL_20NS
  234. << CLASSD_MR_NOVR_VAL_SHIFT);
  235. break;
  236. default:
  237. val |= (CLASSD_MR_NOVR_VAL_10NS
  238. << CLASSD_MR_NOVR_VAL_SHIFT);
  239. dev_warn(codec->dev,
  240. "non-overlapping value %d is invalid, the default value 10 is specified\n",
  241. pdata->non_overlap_time);
  242. break;
  243. }
  244. }
  245. snd_soc_update_bits(codec, CLASSD_MR, mask, val);
  246. dev_info(codec->dev,
  247. "PWM modulation type is %s, non-overlapping is %s\n",
  248. pwm_type[pdata->pwm_type],
  249. pdata->non_overlap_enable?"enabled":"disabled");
  250. return 0;
  251. }
  252. static struct regmap *atmel_classd_codec_get_remap(struct device *dev)
  253. {
  254. return dev_get_regmap(dev, NULL);
  255. }
  256. static struct snd_soc_codec_driver soc_codec_dev_classd = {
  257. .probe = atmel_classd_codec_probe,
  258. .controls = atmel_classd_snd_controls,
  259. .num_controls = ARRAY_SIZE(atmel_classd_snd_controls),
  260. .get_regmap = atmel_classd_codec_get_remap,
  261. };
  262. /* codec dai component */
  263. static int atmel_classd_codec_dai_startup(struct snd_pcm_substream *substream,
  264. struct snd_soc_dai *codec_dai)
  265. {
  266. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  267. struct atmel_classd *dd = snd_soc_card_get_drvdata(rtd->card);
  268. int ret;
  269. ret = clk_prepare_enable(dd->aclk);
  270. if (ret)
  271. return ret;
  272. return clk_prepare_enable(dd->gclk);
  273. }
  274. static int atmel_classd_codec_dai_digital_mute(struct snd_soc_dai *codec_dai,
  275. int mute)
  276. {
  277. struct snd_soc_codec *codec = codec_dai->codec;
  278. u32 mask, val;
  279. mask = CLASSD_MR_LMUTE_MASK | CLASSD_MR_RMUTE_MASK;
  280. if (mute)
  281. val = mask;
  282. else
  283. val = 0;
  284. snd_soc_update_bits(codec, CLASSD_MR, mask, val);
  285. return 0;
  286. }
  287. #define CLASSD_ACLK_RATE_11M2896_MPY_8 (112896 * 100 * 8)
  288. #define CLASSD_ACLK_RATE_12M288_MPY_8 (12288 * 1000 * 8)
  289. static struct {
  290. int rate;
  291. int sample_rate;
  292. int dsp_clk;
  293. unsigned long aclk_rate;
  294. } const sample_rates[] = {
  295. { 8000, CLASSD_INTPMR_FRAME_8K,
  296. CLASSD_INTPMR_DSP_CLK_FREQ_12M288, CLASSD_ACLK_RATE_12M288_MPY_8 },
  297. { 16000, CLASSD_INTPMR_FRAME_16K,
  298. CLASSD_INTPMR_DSP_CLK_FREQ_12M288, CLASSD_ACLK_RATE_12M288_MPY_8 },
  299. { 32000, CLASSD_INTPMR_FRAME_32K,
  300. CLASSD_INTPMR_DSP_CLK_FREQ_12M288, CLASSD_ACLK_RATE_12M288_MPY_8 },
  301. { 48000, CLASSD_INTPMR_FRAME_48K,
  302. CLASSD_INTPMR_DSP_CLK_FREQ_12M288, CLASSD_ACLK_RATE_12M288_MPY_8 },
  303. { 96000, CLASSD_INTPMR_FRAME_96K,
  304. CLASSD_INTPMR_DSP_CLK_FREQ_12M288, CLASSD_ACLK_RATE_12M288_MPY_8 },
  305. { 22050, CLASSD_INTPMR_FRAME_22K,
  306. CLASSD_INTPMR_DSP_CLK_FREQ_11M2896, CLASSD_ACLK_RATE_11M2896_MPY_8 },
  307. { 44100, CLASSD_INTPMR_FRAME_44K,
  308. CLASSD_INTPMR_DSP_CLK_FREQ_11M2896, CLASSD_ACLK_RATE_11M2896_MPY_8 },
  309. { 88200, CLASSD_INTPMR_FRAME_88K,
  310. CLASSD_INTPMR_DSP_CLK_FREQ_11M2896, CLASSD_ACLK_RATE_11M2896_MPY_8 },
  311. };
  312. static int
  313. atmel_classd_codec_dai_hw_params(struct snd_pcm_substream *substream,
  314. struct snd_pcm_hw_params *params,
  315. struct snd_soc_dai *codec_dai)
  316. {
  317. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  318. struct atmel_classd *dd = snd_soc_card_get_drvdata(rtd->card);
  319. struct snd_soc_codec *codec = codec_dai->codec;
  320. int fs;
  321. int i, best, best_val, cur_val, ret;
  322. u32 mask, val;
  323. fs = params_rate(params);
  324. best = 0;
  325. best_val = abs(fs - sample_rates[0].rate);
  326. for (i = 1; i < ARRAY_SIZE(sample_rates); i++) {
  327. /* Closest match */
  328. cur_val = abs(fs - sample_rates[i].rate);
  329. if (cur_val < best_val) {
  330. best = i;
  331. best_val = cur_val;
  332. }
  333. }
  334. dev_dbg(codec->dev,
  335. "Selected SAMPLE_RATE of %dHz, ACLK_RATE of %ldHz\n",
  336. sample_rates[best].rate, sample_rates[best].aclk_rate);
  337. clk_disable_unprepare(dd->gclk);
  338. clk_disable_unprepare(dd->aclk);
  339. ret = clk_set_rate(dd->aclk, sample_rates[best].aclk_rate);
  340. if (ret)
  341. return ret;
  342. mask = CLASSD_INTPMR_DSP_CLK_FREQ_MASK | CLASSD_INTPMR_FRAME_MASK;
  343. val = (sample_rates[best].dsp_clk << CLASSD_INTPMR_DSP_CLK_FREQ_SHIFT)
  344. | (sample_rates[best].sample_rate << CLASSD_INTPMR_FRAME_SHIFT);
  345. snd_soc_update_bits(codec, CLASSD_INTPMR, mask, val);
  346. ret = clk_prepare_enable(dd->aclk);
  347. if (ret)
  348. return ret;
  349. return clk_prepare_enable(dd->gclk);
  350. }
  351. static void
  352. atmel_classd_codec_dai_shutdown(struct snd_pcm_substream *substream,
  353. struct snd_soc_dai *codec_dai)
  354. {
  355. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  356. struct atmel_classd *dd = snd_soc_card_get_drvdata(rtd->card);
  357. clk_disable_unprepare(dd->gclk);
  358. clk_disable_unprepare(dd->aclk);
  359. }
  360. static int atmel_classd_codec_dai_prepare(struct snd_pcm_substream *substream,
  361. struct snd_soc_dai *codec_dai)
  362. {
  363. struct snd_soc_codec *codec = codec_dai->codec;
  364. snd_soc_update_bits(codec, CLASSD_MR,
  365. CLASSD_MR_LEN_MASK | CLASSD_MR_REN_MASK,
  366. (CLASSD_MR_LEN_DIS << CLASSD_MR_LEN_SHIFT)
  367. |(CLASSD_MR_REN_DIS << CLASSD_MR_REN_SHIFT));
  368. return 0;
  369. }
  370. static int atmel_classd_codec_dai_trigger(struct snd_pcm_substream *substream,
  371. int cmd, struct snd_soc_dai *codec_dai)
  372. {
  373. struct snd_soc_codec *codec = codec_dai->codec;
  374. u32 mask, val;
  375. mask = CLASSD_MR_LEN_MASK | CLASSD_MR_REN_MASK;
  376. switch (cmd) {
  377. case SNDRV_PCM_TRIGGER_START:
  378. case SNDRV_PCM_TRIGGER_RESUME:
  379. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  380. val = mask;
  381. break;
  382. case SNDRV_PCM_TRIGGER_STOP:
  383. case SNDRV_PCM_TRIGGER_SUSPEND:
  384. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  385. val = (CLASSD_MR_LEN_DIS << CLASSD_MR_LEN_SHIFT)
  386. | (CLASSD_MR_REN_DIS << CLASSD_MR_REN_SHIFT);
  387. break;
  388. default:
  389. return -EINVAL;
  390. }
  391. snd_soc_update_bits(codec, CLASSD_MR, mask, val);
  392. return 0;
  393. }
  394. static const struct snd_soc_dai_ops atmel_classd_codec_dai_ops = {
  395. .digital_mute = atmel_classd_codec_dai_digital_mute,
  396. .startup = atmel_classd_codec_dai_startup,
  397. .shutdown = atmel_classd_codec_dai_shutdown,
  398. .hw_params = atmel_classd_codec_dai_hw_params,
  399. .prepare = atmel_classd_codec_dai_prepare,
  400. .trigger = atmel_classd_codec_dai_trigger,
  401. };
  402. #define ATMEL_CLASSD_CODEC_DAI_NAME "atmel-classd-hifi"
  403. static struct snd_soc_dai_driver atmel_classd_codec_dai = {
  404. .name = ATMEL_CLASSD_CODEC_DAI_NAME,
  405. .playback = {
  406. .stream_name = "Playback",
  407. .channels_min = 2,
  408. .channels_max = 2,
  409. .rates = ATMEL_CLASSD_RATES,
  410. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  411. },
  412. .ops = &atmel_classd_codec_dai_ops,
  413. };
  414. /* ASoC sound card */
  415. static int atmel_classd_asoc_card_init(struct device *dev,
  416. struct snd_soc_card *card)
  417. {
  418. struct snd_soc_dai_link *dai_link;
  419. struct atmel_classd *dd = snd_soc_card_get_drvdata(card);
  420. dai_link = devm_kzalloc(dev, sizeof(*dai_link), GFP_KERNEL);
  421. if (!dai_link)
  422. return -ENOMEM;
  423. dai_link->name = "CLASSD";
  424. dai_link->stream_name = "CLASSD PCM";
  425. dai_link->codec_dai_name = ATMEL_CLASSD_CODEC_DAI_NAME;
  426. dai_link->cpu_dai_name = dev_name(dev);
  427. dai_link->codec_name = dev_name(dev);
  428. dai_link->platform_name = dev_name(dev);
  429. card->dai_link = dai_link;
  430. card->num_links = 1;
  431. card->name = dd->pdata->card_name;
  432. card->dev = dev;
  433. return 0;
  434. };
  435. /* regmap configuration */
  436. static const struct reg_default atmel_classd_reg_defaults[] = {
  437. { CLASSD_INTPMR, 0x00301212 },
  438. };
  439. #define ATMEL_CLASSD_REG_MAX 0xE4
  440. static const struct regmap_config atmel_classd_regmap_config = {
  441. .reg_bits = 32,
  442. .reg_stride = 4,
  443. .val_bits = 32,
  444. .max_register = ATMEL_CLASSD_REG_MAX,
  445. .cache_type = REGCACHE_FLAT,
  446. .reg_defaults = atmel_classd_reg_defaults,
  447. .num_reg_defaults = ARRAY_SIZE(atmel_classd_reg_defaults),
  448. };
  449. static int atmel_classd_probe(struct platform_device *pdev)
  450. {
  451. struct device *dev = &pdev->dev;
  452. struct atmel_classd *dd;
  453. struct resource *res;
  454. void __iomem *io_base;
  455. const struct atmel_classd_pdata *pdata;
  456. struct snd_soc_card *card;
  457. int ret;
  458. pdata = dev_get_platdata(dev);
  459. if (!pdata) {
  460. pdata = atmel_classd_dt_init(dev);
  461. if (IS_ERR(pdata))
  462. return PTR_ERR(pdata);
  463. }
  464. dd = devm_kzalloc(dev, sizeof(*dd), GFP_KERNEL);
  465. if (!dd)
  466. return -ENOMEM;
  467. dd->pdata = pdata;
  468. dd->irq = platform_get_irq(pdev, 0);
  469. if (dd->irq < 0) {
  470. ret = dd->irq;
  471. dev_err(dev, "failed to could not get irq: %d\n", ret);
  472. return ret;
  473. }
  474. dd->pclk = devm_clk_get(dev, "pclk");
  475. if (IS_ERR(dd->pclk)) {
  476. ret = PTR_ERR(dd->pclk);
  477. dev_err(dev, "failed to get peripheral clock: %d\n", ret);
  478. return ret;
  479. }
  480. dd->gclk = devm_clk_get(dev, "gclk");
  481. if (IS_ERR(dd->gclk)) {
  482. ret = PTR_ERR(dd->gclk);
  483. dev_err(dev, "failed to get GCK clock: %d\n", ret);
  484. return ret;
  485. }
  486. dd->aclk = devm_clk_get(dev, "aclk");
  487. if (IS_ERR(dd->aclk)) {
  488. ret = PTR_ERR(dd->aclk);
  489. dev_err(dev, "failed to get audio clock: %d\n", ret);
  490. return ret;
  491. }
  492. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  493. if (!res) {
  494. dev_err(dev, "no memory resource\n");
  495. return -ENXIO;
  496. }
  497. io_base = devm_ioremap_resource(dev, res);
  498. if (IS_ERR(io_base)) {
  499. ret = PTR_ERR(io_base);
  500. dev_err(dev, "failed to remap register memory: %d\n", ret);
  501. return ret;
  502. }
  503. dd->phy_base = res->start;
  504. dd->regmap = devm_regmap_init_mmio(dev, io_base,
  505. &atmel_classd_regmap_config);
  506. if (IS_ERR(dd->regmap)) {
  507. ret = PTR_ERR(dd->regmap);
  508. dev_err(dev, "failed to init register map: %d\n", ret);
  509. return ret;
  510. }
  511. ret = devm_snd_soc_register_component(dev,
  512. &atmel_classd_cpu_dai_component,
  513. &atmel_classd_cpu_dai, 1);
  514. if (ret) {
  515. dev_err(dev, "could not register CPU DAI: %d\n", ret);
  516. return ret;
  517. }
  518. ret = devm_snd_dmaengine_pcm_register(dev,
  519. &atmel_classd_dmaengine_pcm_config,
  520. 0);
  521. if (ret) {
  522. dev_err(dev, "could not register platform: %d\n", ret);
  523. return ret;
  524. }
  525. ret = snd_soc_register_codec(dev, &soc_codec_dev_classd,
  526. &atmel_classd_codec_dai, 1);
  527. if (ret) {
  528. dev_err(dev, "could not register codec: %d\n", ret);
  529. return ret;
  530. }
  531. /* register sound card */
  532. card = devm_kzalloc(dev, sizeof(*card), GFP_KERNEL);
  533. if (!card)
  534. return -ENOMEM;
  535. snd_soc_card_set_drvdata(card, dd);
  536. platform_set_drvdata(pdev, card);
  537. ret = atmel_classd_asoc_card_init(dev, card);
  538. if (ret) {
  539. dev_err(dev, "failed to init sound card\n");
  540. return ret;
  541. }
  542. ret = devm_snd_soc_register_card(dev, card);
  543. if (ret) {
  544. dev_err(dev, "failed to register sound card: %d\n", ret);
  545. return ret;
  546. }
  547. return 0;
  548. }
  549. static int atmel_classd_remove(struct platform_device *pdev)
  550. {
  551. snd_soc_unregister_codec(&pdev->dev);
  552. return 0;
  553. }
  554. static struct platform_driver atmel_classd_driver = {
  555. .driver = {
  556. .name = "atmel-classd",
  557. .of_match_table = of_match_ptr(atmel_classd_of_match),
  558. .pm = &snd_soc_pm_ops,
  559. },
  560. .probe = atmel_classd_probe,
  561. .remove = atmel_classd_remove,
  562. };
  563. module_platform_driver(atmel_classd_driver);
  564. MODULE_DESCRIPTION("Atmel ClassD driver under ALSA SoC architecture");
  565. MODULE_AUTHOR("Songjun Wu <songjun.wu@atmel.com>");
  566. MODULE_LICENSE("GPL");