intel.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866
  1. // SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause)
  2. // Copyright(c) 2015-17 Intel Corporation.
  3. /*
  4. * Soundwire Intel Master Driver
  5. */
  6. #include <linux/acpi.h>
  7. #include <linux/delay.h>
  8. #include <linux/interrupt.h>
  9. #include <linux/platform_device.h>
  10. #include <sound/pcm_params.h>
  11. #include <sound/soc.h>
  12. #include <linux/soundwire/sdw_registers.h>
  13. #include <linux/soundwire/sdw.h>
  14. #include <linux/soundwire/sdw_intel.h>
  15. #include "cadence_master.h"
  16. #include "intel.h"
  17. /* Intel SHIM Registers Definition */
  18. #define SDW_SHIM_LCAP 0x0
  19. #define SDW_SHIM_LCTL 0x4
  20. #define SDW_SHIM_IPPTR 0x8
  21. #define SDW_SHIM_SYNC 0xC
  22. #define SDW_SHIM_CTLSCAP(x) (0x010 + 0x60 * x)
  23. #define SDW_SHIM_CTLS0CM(x) (0x012 + 0x60 * x)
  24. #define SDW_SHIM_CTLS1CM(x) (0x014 + 0x60 * x)
  25. #define SDW_SHIM_CTLS2CM(x) (0x016 + 0x60 * x)
  26. #define SDW_SHIM_CTLS3CM(x) (0x018 + 0x60 * x)
  27. #define SDW_SHIM_PCMSCAP(x) (0x020 + 0x60 * x)
  28. #define SDW_SHIM_PCMSYCHM(x, y) (0x022 + (0x60 * x) + (0x2 * y))
  29. #define SDW_SHIM_PCMSYCHC(x, y) (0x042 + (0x60 * x) + (0x2 * y))
  30. #define SDW_SHIM_PDMSCAP(x) (0x062 + 0x60 * x)
  31. #define SDW_SHIM_IOCTL(x) (0x06C + 0x60 * x)
  32. #define SDW_SHIM_CTMCTL(x) (0x06E + 0x60 * x)
  33. #define SDW_SHIM_WAKEEN 0x190
  34. #define SDW_SHIM_WAKESTS 0x192
  35. #define SDW_SHIM_LCTL_SPA BIT(0)
  36. #define SDW_SHIM_LCTL_CPA BIT(8)
  37. #define SDW_SHIM_SYNC_SYNCPRD_VAL 0x176F
  38. #define SDW_SHIM_SYNC_SYNCPRD GENMASK(14, 0)
  39. #define SDW_SHIM_SYNC_SYNCCPU BIT(15)
  40. #define SDW_SHIM_SYNC_CMDSYNC_MASK GENMASK(19, 16)
  41. #define SDW_SHIM_SYNC_CMDSYNC BIT(16)
  42. #define SDW_SHIM_SYNC_SYNCGO BIT(24)
  43. #define SDW_SHIM_PCMSCAP_ISS GENMASK(3, 0)
  44. #define SDW_SHIM_PCMSCAP_OSS GENMASK(7, 4)
  45. #define SDW_SHIM_PCMSCAP_BSS GENMASK(12, 8)
  46. #define SDW_SHIM_PCMSYCM_LCHN GENMASK(3, 0)
  47. #define SDW_SHIM_PCMSYCM_HCHN GENMASK(7, 4)
  48. #define SDW_SHIM_PCMSYCM_STREAM GENMASK(13, 8)
  49. #define SDW_SHIM_PCMSYCM_DIR BIT(15)
  50. #define SDW_SHIM_PDMSCAP_ISS GENMASK(3, 0)
  51. #define SDW_SHIM_PDMSCAP_OSS GENMASK(7, 4)
  52. #define SDW_SHIM_PDMSCAP_BSS GENMASK(12, 8)
  53. #define SDW_SHIM_PDMSCAP_CPSS GENMASK(15, 13)
  54. #define SDW_SHIM_IOCTL_MIF BIT(0)
  55. #define SDW_SHIM_IOCTL_CO BIT(1)
  56. #define SDW_SHIM_IOCTL_COE BIT(2)
  57. #define SDW_SHIM_IOCTL_DO BIT(3)
  58. #define SDW_SHIM_IOCTL_DOE BIT(4)
  59. #define SDW_SHIM_IOCTL_BKE BIT(5)
  60. #define SDW_SHIM_IOCTL_WPDD BIT(6)
  61. #define SDW_SHIM_IOCTL_CIBD BIT(8)
  62. #define SDW_SHIM_IOCTL_DIBD BIT(9)
  63. #define SDW_SHIM_CTMCTL_DACTQE BIT(0)
  64. #define SDW_SHIM_CTMCTL_DODS BIT(1)
  65. #define SDW_SHIM_CTMCTL_DOAIS GENMASK(4, 3)
  66. #define SDW_SHIM_WAKEEN_ENABLE BIT(0)
  67. #define SDW_SHIM_WAKESTS_STATUS BIT(0)
  68. /* Intel ALH Register definitions */
  69. #define SDW_ALH_STRMZCFG(x) (0x000 + (0x4 * x))
  70. #define SDW_ALH_STRMZCFG_DMAT_VAL 0x3
  71. #define SDW_ALH_STRMZCFG_DMAT GENMASK(7, 0)
  72. #define SDW_ALH_STRMZCFG_CHN GENMASK(19, 16)
  73. enum intel_pdi_type {
  74. INTEL_PDI_IN = 0,
  75. INTEL_PDI_OUT = 1,
  76. INTEL_PDI_BD = 2,
  77. };
  78. struct sdw_intel {
  79. struct sdw_cdns cdns;
  80. int instance;
  81. struct sdw_intel_link_res *res;
  82. };
  83. #define cdns_to_intel(_cdns) container_of(_cdns, struct sdw_intel, cdns)
  84. /*
  85. * Read, write helpers for HW registers
  86. */
  87. static inline int intel_readl(void __iomem *base, int offset)
  88. {
  89. return readl(base + offset);
  90. }
  91. static inline void intel_writel(void __iomem *base, int offset, int value)
  92. {
  93. writel(value, base + offset);
  94. }
  95. static inline u16 intel_readw(void __iomem *base, int offset)
  96. {
  97. return readw(base + offset);
  98. }
  99. static inline void intel_writew(void __iomem *base, int offset, u16 value)
  100. {
  101. writew(value, base + offset);
  102. }
  103. static int intel_clear_bit(void __iomem *base, int offset, u32 value, u32 mask)
  104. {
  105. int timeout = 10;
  106. u32 reg_read;
  107. writel(value, base + offset);
  108. do {
  109. reg_read = readl(base + offset);
  110. if (!(reg_read & mask))
  111. return 0;
  112. timeout--;
  113. udelay(50);
  114. } while (timeout != 0);
  115. return -EAGAIN;
  116. }
  117. static int intel_set_bit(void __iomem *base, int offset, u32 value, u32 mask)
  118. {
  119. int timeout = 10;
  120. u32 reg_read;
  121. writel(value, base + offset);
  122. do {
  123. reg_read = readl(base + offset);
  124. if (reg_read & mask)
  125. return 0;
  126. timeout--;
  127. udelay(50);
  128. } while (timeout != 0);
  129. return -EAGAIN;
  130. }
  131. /*
  132. * shim ops
  133. */
  134. static int intel_link_power_up(struct sdw_intel *sdw)
  135. {
  136. unsigned int link_id = sdw->instance;
  137. void __iomem *shim = sdw->res->shim;
  138. int spa_mask, cpa_mask;
  139. int link_control, ret;
  140. /* Link power up sequence */
  141. link_control = intel_readl(shim, SDW_SHIM_LCTL);
  142. spa_mask = (SDW_SHIM_LCTL_SPA << link_id);
  143. cpa_mask = (SDW_SHIM_LCTL_CPA << link_id);
  144. link_control |= spa_mask;
  145. ret = intel_set_bit(shim, SDW_SHIM_LCTL, link_control, cpa_mask);
  146. if (ret < 0)
  147. return ret;
  148. sdw->cdns.link_up = true;
  149. return 0;
  150. }
  151. static int intel_shim_init(struct sdw_intel *sdw)
  152. {
  153. void __iomem *shim = sdw->res->shim;
  154. unsigned int link_id = sdw->instance;
  155. int sync_reg, ret;
  156. u16 ioctl = 0, act = 0;
  157. /* Initialize Shim */
  158. ioctl |= SDW_SHIM_IOCTL_BKE;
  159. intel_writew(shim, SDW_SHIM_IOCTL(link_id), ioctl);
  160. ioctl |= SDW_SHIM_IOCTL_WPDD;
  161. intel_writew(shim, SDW_SHIM_IOCTL(link_id), ioctl);
  162. ioctl |= SDW_SHIM_IOCTL_DO;
  163. intel_writew(shim, SDW_SHIM_IOCTL(link_id), ioctl);
  164. ioctl |= SDW_SHIM_IOCTL_DOE;
  165. intel_writew(shim, SDW_SHIM_IOCTL(link_id), ioctl);
  166. /* Switch to MIP from Glue logic */
  167. ioctl = intel_readw(shim, SDW_SHIM_IOCTL(link_id));
  168. ioctl &= ~(SDW_SHIM_IOCTL_DOE);
  169. intel_writew(shim, SDW_SHIM_IOCTL(link_id), ioctl);
  170. ioctl &= ~(SDW_SHIM_IOCTL_DO);
  171. intel_writew(shim, SDW_SHIM_IOCTL(link_id), ioctl);
  172. ioctl |= (SDW_SHIM_IOCTL_MIF);
  173. intel_writew(shim, SDW_SHIM_IOCTL(link_id), ioctl);
  174. ioctl &= ~(SDW_SHIM_IOCTL_BKE);
  175. ioctl &= ~(SDW_SHIM_IOCTL_COE);
  176. intel_writew(shim, SDW_SHIM_IOCTL(link_id), ioctl);
  177. act |= 0x1 << SDW_REG_SHIFT(SDW_SHIM_CTMCTL_DOAIS);
  178. act |= SDW_SHIM_CTMCTL_DACTQE;
  179. act |= SDW_SHIM_CTMCTL_DODS;
  180. intel_writew(shim, SDW_SHIM_CTMCTL(link_id), act);
  181. /* Now set SyncPRD period */
  182. sync_reg = intel_readl(shim, SDW_SHIM_SYNC);
  183. sync_reg |= (SDW_SHIM_SYNC_SYNCPRD_VAL <<
  184. SDW_REG_SHIFT(SDW_SHIM_SYNC_SYNCPRD));
  185. /* Set SyncCPU bit */
  186. sync_reg |= SDW_SHIM_SYNC_SYNCCPU;
  187. ret = intel_clear_bit(shim, SDW_SHIM_SYNC, sync_reg,
  188. SDW_SHIM_SYNC_SYNCCPU);
  189. if (ret < 0)
  190. dev_err(sdw->cdns.dev, "Failed to set sync period: %d", ret);
  191. return ret;
  192. }
  193. /*
  194. * PDI routines
  195. */
  196. static void intel_pdi_init(struct sdw_intel *sdw,
  197. struct sdw_cdns_stream_config *config)
  198. {
  199. void __iomem *shim = sdw->res->shim;
  200. unsigned int link_id = sdw->instance;
  201. int pcm_cap, pdm_cap;
  202. /* PCM Stream Capability */
  203. pcm_cap = intel_readw(shim, SDW_SHIM_PCMSCAP(link_id));
  204. config->pcm_bd = (pcm_cap & SDW_SHIM_PCMSCAP_BSS) >>
  205. SDW_REG_SHIFT(SDW_SHIM_PCMSCAP_BSS);
  206. config->pcm_in = (pcm_cap & SDW_SHIM_PCMSCAP_ISS) >>
  207. SDW_REG_SHIFT(SDW_SHIM_PCMSCAP_ISS);
  208. config->pcm_out = (pcm_cap & SDW_SHIM_PCMSCAP_OSS) >>
  209. SDW_REG_SHIFT(SDW_SHIM_PCMSCAP_OSS);
  210. /* PDM Stream Capability */
  211. pdm_cap = intel_readw(shim, SDW_SHIM_PDMSCAP(link_id));
  212. config->pdm_bd = (pdm_cap & SDW_SHIM_PDMSCAP_BSS) >>
  213. SDW_REG_SHIFT(SDW_SHIM_PDMSCAP_BSS);
  214. config->pdm_in = (pdm_cap & SDW_SHIM_PDMSCAP_ISS) >>
  215. SDW_REG_SHIFT(SDW_SHIM_PDMSCAP_ISS);
  216. config->pdm_out = (pdm_cap & SDW_SHIM_PDMSCAP_OSS) >>
  217. SDW_REG_SHIFT(SDW_SHIM_PDMSCAP_OSS);
  218. }
  219. static int
  220. intel_pdi_get_ch_cap(struct sdw_intel *sdw, unsigned int pdi_num, bool pcm)
  221. {
  222. void __iomem *shim = sdw->res->shim;
  223. unsigned int link_id = sdw->instance;
  224. int count;
  225. if (pcm) {
  226. count = intel_readw(shim, SDW_SHIM_PCMSYCHC(link_id, pdi_num));
  227. } else {
  228. count = intel_readw(shim, SDW_SHIM_PDMSCAP(link_id));
  229. count = ((count & SDW_SHIM_PDMSCAP_CPSS) >>
  230. SDW_REG_SHIFT(SDW_SHIM_PDMSCAP_CPSS));
  231. }
  232. /* zero based values for channel count in register */
  233. count++;
  234. return count;
  235. }
  236. static int intel_pdi_get_ch_update(struct sdw_intel *sdw,
  237. struct sdw_cdns_pdi *pdi,
  238. unsigned int num_pdi,
  239. unsigned int *num_ch, bool pcm)
  240. {
  241. int i, ch_count = 0;
  242. for (i = 0; i < num_pdi; i++) {
  243. pdi->ch_count = intel_pdi_get_ch_cap(sdw, pdi->num, pcm);
  244. ch_count += pdi->ch_count;
  245. pdi++;
  246. }
  247. *num_ch = ch_count;
  248. return 0;
  249. }
  250. static int intel_pdi_stream_ch_update(struct sdw_intel *sdw,
  251. struct sdw_cdns_streams *stream, bool pcm)
  252. {
  253. intel_pdi_get_ch_update(sdw, stream->bd, stream->num_bd,
  254. &stream->num_ch_bd, pcm);
  255. intel_pdi_get_ch_update(sdw, stream->in, stream->num_in,
  256. &stream->num_ch_in, pcm);
  257. intel_pdi_get_ch_update(sdw, stream->out, stream->num_out,
  258. &stream->num_ch_out, pcm);
  259. return 0;
  260. }
  261. static int intel_pdi_ch_update(struct sdw_intel *sdw)
  262. {
  263. /* First update PCM streams followed by PDM streams */
  264. intel_pdi_stream_ch_update(sdw, &sdw->cdns.pcm, true);
  265. intel_pdi_stream_ch_update(sdw, &sdw->cdns.pdm, false);
  266. return 0;
  267. }
  268. static void
  269. intel_pdi_shim_configure(struct sdw_intel *sdw, struct sdw_cdns_pdi *pdi)
  270. {
  271. void __iomem *shim = sdw->res->shim;
  272. unsigned int link_id = sdw->instance;
  273. int pdi_conf = 0;
  274. pdi->intel_alh_id = (link_id * 16) + pdi->num + 5;
  275. /*
  276. * Program stream parameters to stream SHIM register
  277. * This is applicable for PCM stream only.
  278. */
  279. if (pdi->type != SDW_STREAM_PCM)
  280. return;
  281. if (pdi->dir == SDW_DATA_DIR_RX)
  282. pdi_conf |= SDW_SHIM_PCMSYCM_DIR;
  283. else
  284. pdi_conf &= ~(SDW_SHIM_PCMSYCM_DIR);
  285. pdi_conf |= (pdi->intel_alh_id <<
  286. SDW_REG_SHIFT(SDW_SHIM_PCMSYCM_STREAM));
  287. pdi_conf |= (pdi->l_ch_num << SDW_REG_SHIFT(SDW_SHIM_PCMSYCM_LCHN));
  288. pdi_conf |= (pdi->h_ch_num << SDW_REG_SHIFT(SDW_SHIM_PCMSYCM_HCHN));
  289. intel_writew(shim, SDW_SHIM_PCMSYCHM(link_id, pdi->num), pdi_conf);
  290. }
  291. static void
  292. intel_pdi_alh_configure(struct sdw_intel *sdw, struct sdw_cdns_pdi *pdi)
  293. {
  294. void __iomem *alh = sdw->res->alh;
  295. unsigned int link_id = sdw->instance;
  296. unsigned int conf;
  297. pdi->intel_alh_id = (link_id * 16) + pdi->num + 5;
  298. /* Program Stream config ALH register */
  299. conf = intel_readl(alh, SDW_ALH_STRMZCFG(pdi->intel_alh_id));
  300. conf |= (SDW_ALH_STRMZCFG_DMAT_VAL <<
  301. SDW_REG_SHIFT(SDW_ALH_STRMZCFG_DMAT));
  302. conf |= ((pdi->ch_count - 1) <<
  303. SDW_REG_SHIFT(SDW_ALH_STRMZCFG_CHN));
  304. intel_writel(alh, SDW_ALH_STRMZCFG(pdi->intel_alh_id), conf);
  305. }
  306. static int intel_config_stream(struct sdw_intel *sdw,
  307. struct snd_pcm_substream *substream,
  308. struct snd_soc_dai *dai,
  309. struct snd_pcm_hw_params *hw_params, int link_id)
  310. {
  311. if (sdw->res->ops && sdw->res->ops->config_stream)
  312. return sdw->res->ops->config_stream(sdw->res->arg,
  313. substream, dai, hw_params, link_id);
  314. return -EIO;
  315. }
  316. /*
  317. * DAI routines
  318. */
  319. static struct sdw_cdns_port *intel_alloc_port(struct sdw_intel *sdw,
  320. u32 ch, u32 dir, bool pcm)
  321. {
  322. struct sdw_cdns *cdns = &sdw->cdns;
  323. struct sdw_cdns_port *port = NULL;
  324. int i, ret = 0;
  325. for (i = 0; i < cdns->num_ports; i++) {
  326. if (cdns->ports[i].assigned == true)
  327. continue;
  328. port = &cdns->ports[i];
  329. port->assigned = true;
  330. port->direction = dir;
  331. port->ch = ch;
  332. break;
  333. }
  334. if (!port) {
  335. dev_err(cdns->dev, "Unable to find a free port\n");
  336. return NULL;
  337. }
  338. if (pcm) {
  339. ret = sdw_cdns_alloc_stream(cdns, &cdns->pcm, port, ch, dir);
  340. if (ret)
  341. goto out;
  342. intel_pdi_shim_configure(sdw, port->pdi);
  343. sdw_cdns_config_stream(cdns, port, ch, dir, port->pdi);
  344. intel_pdi_alh_configure(sdw, port->pdi);
  345. } else {
  346. ret = sdw_cdns_alloc_stream(cdns, &cdns->pdm, port, ch, dir);
  347. }
  348. out:
  349. if (ret) {
  350. port->assigned = false;
  351. port = NULL;
  352. }
  353. return port;
  354. }
  355. static void intel_port_cleanup(struct sdw_cdns_dma_data *dma)
  356. {
  357. int i;
  358. for (i = 0; i < dma->nr_ports; i++) {
  359. if (dma->port[i]) {
  360. dma->port[i]->pdi->assigned = false;
  361. dma->port[i]->pdi = NULL;
  362. dma->port[i]->assigned = false;
  363. dma->port[i] = NULL;
  364. }
  365. }
  366. }
  367. static int intel_hw_params(struct snd_pcm_substream *substream,
  368. struct snd_pcm_hw_params *params,
  369. struct snd_soc_dai *dai)
  370. {
  371. struct sdw_cdns *cdns = snd_soc_dai_get_drvdata(dai);
  372. struct sdw_intel *sdw = cdns_to_intel(cdns);
  373. struct sdw_cdns_dma_data *dma;
  374. struct sdw_stream_config sconfig;
  375. struct sdw_port_config *pconfig;
  376. int ret, i, ch, dir;
  377. bool pcm = true;
  378. dma = snd_soc_dai_get_dma_data(dai, substream);
  379. if (!dma)
  380. return -EIO;
  381. ch = params_channels(params);
  382. if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
  383. dir = SDW_DATA_DIR_RX;
  384. else
  385. dir = SDW_DATA_DIR_TX;
  386. if (dma->stream_type == SDW_STREAM_PDM) {
  387. /* TODO: Check whether PDM decimator is already in use */
  388. dma->nr_ports = sdw_cdns_get_stream(cdns, &cdns->pdm, ch, dir);
  389. pcm = false;
  390. } else {
  391. dma->nr_ports = sdw_cdns_get_stream(cdns, &cdns->pcm, ch, dir);
  392. }
  393. if (!dma->nr_ports) {
  394. dev_err(dai->dev, "ports/resources not available");
  395. return -EINVAL;
  396. }
  397. dma->port = kcalloc(dma->nr_ports, sizeof(*dma->port), GFP_KERNEL);
  398. if (!dma->port)
  399. return -ENOMEM;
  400. for (i = 0; i < dma->nr_ports; i++) {
  401. dma->port[i] = intel_alloc_port(sdw, ch, dir, pcm);
  402. if (!dma->port[i]) {
  403. ret = -EINVAL;
  404. goto port_error;
  405. }
  406. }
  407. /* Inform DSP about PDI stream number */
  408. for (i = 0; i < dma->nr_ports; i++) {
  409. ret = intel_config_stream(sdw, substream, dai, params,
  410. dma->port[i]->pdi->intel_alh_id);
  411. if (ret)
  412. goto port_error;
  413. }
  414. sconfig.direction = dir;
  415. sconfig.ch_count = ch;
  416. sconfig.frame_rate = params_rate(params);
  417. sconfig.type = dma->stream_type;
  418. if (dma->stream_type == SDW_STREAM_PDM) {
  419. sconfig.frame_rate *= 50;
  420. sconfig.bps = 1;
  421. } else {
  422. sconfig.bps = snd_pcm_format_width(params_format(params));
  423. }
  424. /* Port configuration */
  425. pconfig = kcalloc(dma->nr_ports, sizeof(*pconfig), GFP_KERNEL);
  426. if (!pconfig) {
  427. ret = -ENOMEM;
  428. goto port_error;
  429. }
  430. for (i = 0; i < dma->nr_ports; i++) {
  431. pconfig[i].num = dma->port[i]->num;
  432. pconfig[i].ch_mask = (1 << ch) - 1;
  433. }
  434. ret = sdw_stream_add_master(&cdns->bus, &sconfig,
  435. pconfig, dma->nr_ports, dma->stream);
  436. if (ret) {
  437. dev_err(cdns->dev, "add master to stream failed:%d", ret);
  438. goto stream_error;
  439. }
  440. kfree(pconfig);
  441. return ret;
  442. stream_error:
  443. kfree(pconfig);
  444. port_error:
  445. intel_port_cleanup(dma);
  446. kfree(dma->port);
  447. return ret;
  448. }
  449. static int
  450. intel_hw_free(struct snd_pcm_substream *substream, struct snd_soc_dai *dai)
  451. {
  452. struct sdw_cdns *cdns = snd_soc_dai_get_drvdata(dai);
  453. struct sdw_cdns_dma_data *dma;
  454. int ret;
  455. dma = snd_soc_dai_get_dma_data(dai, substream);
  456. if (!dma)
  457. return -EIO;
  458. ret = sdw_stream_remove_master(&cdns->bus, dma->stream);
  459. if (ret < 0)
  460. dev_err(dai->dev, "remove master from stream %s failed: %d",
  461. dma->stream->name, ret);
  462. intel_port_cleanup(dma);
  463. kfree(dma->port);
  464. return ret;
  465. }
  466. static int intel_pcm_set_sdw_stream(struct snd_soc_dai *dai,
  467. void *stream, int direction)
  468. {
  469. return cdns_set_sdw_stream(dai, stream, true, direction);
  470. }
  471. static int intel_pdm_set_sdw_stream(struct snd_soc_dai *dai,
  472. void *stream, int direction)
  473. {
  474. return cdns_set_sdw_stream(dai, stream, false, direction);
  475. }
  476. static struct snd_soc_dai_ops intel_pcm_dai_ops = {
  477. .hw_params = intel_hw_params,
  478. .hw_free = intel_hw_free,
  479. .shutdown = sdw_cdns_shutdown,
  480. .set_sdw_stream = intel_pcm_set_sdw_stream,
  481. };
  482. static struct snd_soc_dai_ops intel_pdm_dai_ops = {
  483. .hw_params = intel_hw_params,
  484. .hw_free = intel_hw_free,
  485. .shutdown = sdw_cdns_shutdown,
  486. .set_sdw_stream = intel_pdm_set_sdw_stream,
  487. };
  488. static const struct snd_soc_component_driver dai_component = {
  489. .name = "soundwire",
  490. };
  491. static int intel_create_dai(struct sdw_cdns *cdns,
  492. struct snd_soc_dai_driver *dais,
  493. enum intel_pdi_type type,
  494. u32 num, u32 off, u32 max_ch, bool pcm)
  495. {
  496. int i;
  497. if (num == 0)
  498. return 0;
  499. /* TODO: Read supported rates/formats from hardware */
  500. for (i = off; i < (off + num); i++) {
  501. dais[i].name = kasprintf(GFP_KERNEL, "SDW%d Pin%d",
  502. cdns->instance, i);
  503. if (!dais[i].name)
  504. return -ENOMEM;
  505. if (type == INTEL_PDI_BD || type == INTEL_PDI_OUT) {
  506. dais[i].playback.stream_name = kasprintf(GFP_KERNEL,
  507. "SDW%d Tx%d",
  508. cdns->instance, i);
  509. if (!dais[i].playback.stream_name) {
  510. kfree(dais[i].name);
  511. return -ENOMEM;
  512. }
  513. dais[i].playback.channels_min = 1;
  514. dais[i].playback.channels_max = max_ch;
  515. dais[i].playback.rates = SNDRV_PCM_RATE_48000;
  516. dais[i].playback.formats = SNDRV_PCM_FMTBIT_S16_LE;
  517. }
  518. if (type == INTEL_PDI_BD || type == INTEL_PDI_IN) {
  519. dais[i].capture.stream_name = kasprintf(GFP_KERNEL,
  520. "SDW%d Rx%d",
  521. cdns->instance, i);
  522. if (!dais[i].capture.stream_name) {
  523. kfree(dais[i].name);
  524. kfree(dais[i].playback.stream_name);
  525. return -ENOMEM;
  526. }
  527. dais[i].playback.channels_min = 1;
  528. dais[i].playback.channels_max = max_ch;
  529. dais[i].capture.rates = SNDRV_PCM_RATE_48000;
  530. dais[i].capture.formats = SNDRV_PCM_FMTBIT_S16_LE;
  531. }
  532. dais[i].id = SDW_DAI_ID_RANGE_START + i;
  533. if (pcm)
  534. dais[i].ops = &intel_pcm_dai_ops;
  535. else
  536. dais[i].ops = &intel_pdm_dai_ops;
  537. }
  538. return 0;
  539. }
  540. static int intel_register_dai(struct sdw_intel *sdw)
  541. {
  542. struct sdw_cdns *cdns = &sdw->cdns;
  543. struct sdw_cdns_streams *stream;
  544. struct snd_soc_dai_driver *dais;
  545. int num_dai, ret, off = 0;
  546. /* DAIs are created based on total number of PDIs supported */
  547. num_dai = cdns->pcm.num_pdi + cdns->pdm.num_pdi;
  548. dais = devm_kcalloc(cdns->dev, num_dai, sizeof(*dais), GFP_KERNEL);
  549. if (!dais)
  550. return -ENOMEM;
  551. /* Create PCM DAIs */
  552. stream = &cdns->pcm;
  553. ret = intel_create_dai(cdns, dais, INTEL_PDI_IN,
  554. stream->num_in, off, stream->num_ch_in, true);
  555. if (ret)
  556. return ret;
  557. off += cdns->pcm.num_in;
  558. ret = intel_create_dai(cdns, dais, INTEL_PDI_OUT,
  559. cdns->pcm.num_out, off, stream->num_ch_out, true);
  560. if (ret)
  561. return ret;
  562. off += cdns->pcm.num_out;
  563. ret = intel_create_dai(cdns, dais, INTEL_PDI_BD,
  564. cdns->pcm.num_bd, off, stream->num_ch_bd, true);
  565. if (ret)
  566. return ret;
  567. /* Create PDM DAIs */
  568. stream = &cdns->pdm;
  569. off += cdns->pcm.num_bd;
  570. ret = intel_create_dai(cdns, dais, INTEL_PDI_IN,
  571. cdns->pdm.num_in, off, stream->num_ch_in, false);
  572. if (ret)
  573. return ret;
  574. off += cdns->pdm.num_in;
  575. ret = intel_create_dai(cdns, dais, INTEL_PDI_OUT,
  576. cdns->pdm.num_out, off, stream->num_ch_out, false);
  577. if (ret)
  578. return ret;
  579. off += cdns->pdm.num_bd;
  580. ret = intel_create_dai(cdns, dais, INTEL_PDI_BD,
  581. cdns->pdm.num_bd, off, stream->num_ch_bd, false);
  582. if (ret)
  583. return ret;
  584. return snd_soc_register_component(cdns->dev, &dai_component,
  585. dais, num_dai);
  586. }
  587. static int intel_prop_read(struct sdw_bus *bus)
  588. {
  589. /* Initialize with default handler to read all DisCo properties */
  590. sdw_master_read_prop(bus);
  591. /* BIOS is not giving some values correctly. So, lets override them */
  592. bus->prop.num_freq = 1;
  593. bus->prop.freq = devm_kcalloc(bus->dev, sizeof(*bus->prop.freq),
  594. bus->prop.num_freq, GFP_KERNEL);
  595. if (!bus->prop.freq)
  596. return -ENOMEM;
  597. bus->prop.freq[0] = bus->prop.max_freq;
  598. bus->prop.err_threshold = 5;
  599. return 0;
  600. }
  601. static struct sdw_master_ops sdw_intel_ops = {
  602. .read_prop = sdw_master_read_prop,
  603. .xfer_msg = cdns_xfer_msg,
  604. .xfer_msg_defer = cdns_xfer_msg_defer,
  605. .reset_page_addr = cdns_reset_page_addr,
  606. .set_bus_conf = cdns_bus_conf,
  607. };
  608. /*
  609. * probe and init
  610. */
  611. static int intel_probe(struct platform_device *pdev)
  612. {
  613. struct sdw_cdns_stream_config config;
  614. struct sdw_intel *sdw;
  615. int ret;
  616. sdw = devm_kzalloc(&pdev->dev, sizeof(*sdw), GFP_KERNEL);
  617. if (!sdw)
  618. return -ENOMEM;
  619. sdw->instance = pdev->id;
  620. sdw->res = dev_get_platdata(&pdev->dev);
  621. sdw->cdns.dev = &pdev->dev;
  622. sdw->cdns.registers = sdw->res->registers;
  623. sdw->cdns.instance = sdw->instance;
  624. sdw->cdns.msg_count = 0;
  625. sdw->cdns.bus.dev = &pdev->dev;
  626. sdw->cdns.bus.link_id = pdev->id;
  627. sdw_cdns_probe(&sdw->cdns);
  628. /* Set property read ops */
  629. sdw_intel_ops.read_prop = intel_prop_read;
  630. sdw->cdns.bus.ops = &sdw_intel_ops;
  631. sdw_intel_ops.read_prop = intel_prop_read;
  632. sdw->cdns.bus.ops = &sdw_intel_ops;
  633. platform_set_drvdata(pdev, sdw);
  634. ret = sdw_add_bus_master(&sdw->cdns.bus);
  635. if (ret) {
  636. dev_err(&pdev->dev, "sdw_add_bus_master fail: %d\n", ret);
  637. goto err_master_reg;
  638. }
  639. /* Initialize shim and controller */
  640. intel_link_power_up(sdw);
  641. intel_shim_init(sdw);
  642. ret = sdw_cdns_init(&sdw->cdns);
  643. if (ret)
  644. goto err_init;
  645. ret = sdw_cdns_enable_interrupt(&sdw->cdns);
  646. /* Read the PDI config and initialize cadence PDI */
  647. intel_pdi_init(sdw, &config);
  648. ret = sdw_cdns_pdi_init(&sdw->cdns, config);
  649. if (ret)
  650. goto err_init;
  651. intel_pdi_ch_update(sdw);
  652. /* Acquire IRQ */
  653. ret = request_threaded_irq(sdw->res->irq, sdw_cdns_irq,
  654. sdw_cdns_thread, IRQF_SHARED, KBUILD_MODNAME,
  655. &sdw->cdns);
  656. if (ret < 0) {
  657. dev_err(sdw->cdns.dev, "unable to grab IRQ %d, disabling device\n",
  658. sdw->res->irq);
  659. goto err_init;
  660. }
  661. /* Register DAIs */
  662. ret = intel_register_dai(sdw);
  663. if (ret) {
  664. dev_err(sdw->cdns.dev, "DAI registration failed: %d", ret);
  665. snd_soc_unregister_component(sdw->cdns.dev);
  666. goto err_dai;
  667. }
  668. return 0;
  669. err_dai:
  670. free_irq(sdw->res->irq, sdw);
  671. err_init:
  672. sdw_delete_bus_master(&sdw->cdns.bus);
  673. err_master_reg:
  674. return ret;
  675. }
  676. static int intel_remove(struct platform_device *pdev)
  677. {
  678. struct sdw_intel *sdw;
  679. sdw = platform_get_drvdata(pdev);
  680. free_irq(sdw->res->irq, sdw);
  681. snd_soc_unregister_component(sdw->cdns.dev);
  682. sdw_delete_bus_master(&sdw->cdns.bus);
  683. return 0;
  684. }
  685. static struct platform_driver sdw_intel_drv = {
  686. .probe = intel_probe,
  687. .remove = intel_remove,
  688. .driver = {
  689. .name = "int-sdw",
  690. },
  691. };
  692. module_platform_driver(sdw_intel_drv);
  693. MODULE_LICENSE("Dual BSD/GPL");
  694. MODULE_ALIAS("platform:int-sdw");
  695. MODULE_DESCRIPTION("Intel Soundwire Master Driver");