intel.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883
  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. /*
  228. * WORKAROUND: on all existing Intel controllers, pdi
  229. * number 2 reports channel count as 1 even though it
  230. * supports 8 channels. Performing hardcoding for pdi
  231. * number 2.
  232. */
  233. if (pdi_num == 2)
  234. count = 7;
  235. } else {
  236. count = intel_readw(shim, SDW_SHIM_PDMSCAP(link_id));
  237. count = ((count & SDW_SHIM_PDMSCAP_CPSS) >>
  238. SDW_REG_SHIFT(SDW_SHIM_PDMSCAP_CPSS));
  239. }
  240. /* zero based values for channel count in register */
  241. count++;
  242. return count;
  243. }
  244. static int intel_pdi_get_ch_update(struct sdw_intel *sdw,
  245. struct sdw_cdns_pdi *pdi,
  246. unsigned int num_pdi,
  247. unsigned int *num_ch, bool pcm)
  248. {
  249. int i, ch_count = 0;
  250. for (i = 0; i < num_pdi; i++) {
  251. pdi->ch_count = intel_pdi_get_ch_cap(sdw, pdi->num, pcm);
  252. ch_count += pdi->ch_count;
  253. pdi++;
  254. }
  255. *num_ch = ch_count;
  256. return 0;
  257. }
  258. static int intel_pdi_stream_ch_update(struct sdw_intel *sdw,
  259. struct sdw_cdns_streams *stream, bool pcm)
  260. {
  261. intel_pdi_get_ch_update(sdw, stream->bd, stream->num_bd,
  262. &stream->num_ch_bd, pcm);
  263. intel_pdi_get_ch_update(sdw, stream->in, stream->num_in,
  264. &stream->num_ch_in, pcm);
  265. intel_pdi_get_ch_update(sdw, stream->out, stream->num_out,
  266. &stream->num_ch_out, pcm);
  267. return 0;
  268. }
  269. static int intel_pdi_ch_update(struct sdw_intel *sdw)
  270. {
  271. /* First update PCM streams followed by PDM streams */
  272. intel_pdi_stream_ch_update(sdw, &sdw->cdns.pcm, true);
  273. intel_pdi_stream_ch_update(sdw, &sdw->cdns.pdm, false);
  274. return 0;
  275. }
  276. static void
  277. intel_pdi_shim_configure(struct sdw_intel *sdw, struct sdw_cdns_pdi *pdi)
  278. {
  279. void __iomem *shim = sdw->res->shim;
  280. unsigned int link_id = sdw->instance;
  281. int pdi_conf = 0;
  282. /* the Bulk and PCM streams are not contiguous */
  283. pdi->intel_alh_id = (link_id * 16) + pdi->num + 3;
  284. if (pdi->num >= 2)
  285. pdi->intel_alh_id += 2;
  286. /*
  287. * Program stream parameters to stream SHIM register
  288. * This is applicable for PCM stream only.
  289. */
  290. if (pdi->type != SDW_STREAM_PCM)
  291. return;
  292. if (pdi->dir == SDW_DATA_DIR_RX)
  293. pdi_conf |= SDW_SHIM_PCMSYCM_DIR;
  294. else
  295. pdi_conf &= ~(SDW_SHIM_PCMSYCM_DIR);
  296. pdi_conf |= (pdi->intel_alh_id <<
  297. SDW_REG_SHIFT(SDW_SHIM_PCMSYCM_STREAM));
  298. pdi_conf |= (pdi->l_ch_num << SDW_REG_SHIFT(SDW_SHIM_PCMSYCM_LCHN));
  299. pdi_conf |= (pdi->h_ch_num << SDW_REG_SHIFT(SDW_SHIM_PCMSYCM_HCHN));
  300. intel_writew(shim, SDW_SHIM_PCMSYCHM(link_id, pdi->num), pdi_conf);
  301. }
  302. static void
  303. intel_pdi_alh_configure(struct sdw_intel *sdw, struct sdw_cdns_pdi *pdi)
  304. {
  305. void __iomem *alh = sdw->res->alh;
  306. unsigned int link_id = sdw->instance;
  307. unsigned int conf;
  308. /* the Bulk and PCM streams are not contiguous */
  309. pdi->intel_alh_id = (link_id * 16) + pdi->num + 3;
  310. if (pdi->num >= 2)
  311. pdi->intel_alh_id += 2;
  312. /* Program Stream config ALH register */
  313. conf = intel_readl(alh, SDW_ALH_STRMZCFG(pdi->intel_alh_id));
  314. conf |= (SDW_ALH_STRMZCFG_DMAT_VAL <<
  315. SDW_REG_SHIFT(SDW_ALH_STRMZCFG_DMAT));
  316. conf |= ((pdi->ch_count - 1) <<
  317. SDW_REG_SHIFT(SDW_ALH_STRMZCFG_CHN));
  318. intel_writel(alh, SDW_ALH_STRMZCFG(pdi->intel_alh_id), conf);
  319. }
  320. static int intel_config_stream(struct sdw_intel *sdw,
  321. struct snd_pcm_substream *substream,
  322. struct snd_soc_dai *dai,
  323. struct snd_pcm_hw_params *hw_params, int link_id)
  324. {
  325. if (sdw->res->ops && sdw->res->ops->config_stream)
  326. return sdw->res->ops->config_stream(sdw->res->arg,
  327. substream, dai, hw_params, link_id);
  328. return -EIO;
  329. }
  330. /*
  331. * DAI routines
  332. */
  333. static struct sdw_cdns_port *intel_alloc_port(struct sdw_intel *sdw,
  334. u32 ch, u32 dir, bool pcm)
  335. {
  336. struct sdw_cdns *cdns = &sdw->cdns;
  337. struct sdw_cdns_port *port = NULL;
  338. int i, ret = 0;
  339. for (i = 0; i < cdns->num_ports; i++) {
  340. if (cdns->ports[i].assigned == true)
  341. continue;
  342. port = &cdns->ports[i];
  343. port->assigned = true;
  344. port->direction = dir;
  345. port->ch = ch;
  346. break;
  347. }
  348. if (!port) {
  349. dev_err(cdns->dev, "Unable to find a free port\n");
  350. return NULL;
  351. }
  352. if (pcm) {
  353. ret = sdw_cdns_alloc_stream(cdns, &cdns->pcm, port, ch, dir);
  354. if (ret)
  355. goto out;
  356. intel_pdi_shim_configure(sdw, port->pdi);
  357. sdw_cdns_config_stream(cdns, port, ch, dir, port->pdi);
  358. intel_pdi_alh_configure(sdw, port->pdi);
  359. } else {
  360. ret = sdw_cdns_alloc_stream(cdns, &cdns->pdm, port, ch, dir);
  361. }
  362. out:
  363. if (ret) {
  364. port->assigned = false;
  365. port = NULL;
  366. }
  367. return port;
  368. }
  369. static void intel_port_cleanup(struct sdw_cdns_dma_data *dma)
  370. {
  371. int i;
  372. for (i = 0; i < dma->nr_ports; i++) {
  373. if (dma->port[i]) {
  374. dma->port[i]->pdi->assigned = false;
  375. dma->port[i]->pdi = NULL;
  376. dma->port[i]->assigned = false;
  377. dma->port[i] = NULL;
  378. }
  379. }
  380. }
  381. static int intel_hw_params(struct snd_pcm_substream *substream,
  382. struct snd_pcm_hw_params *params,
  383. struct snd_soc_dai *dai)
  384. {
  385. struct sdw_cdns *cdns = snd_soc_dai_get_drvdata(dai);
  386. struct sdw_intel *sdw = cdns_to_intel(cdns);
  387. struct sdw_cdns_dma_data *dma;
  388. struct sdw_stream_config sconfig;
  389. struct sdw_port_config *pconfig;
  390. int ret, i, ch, dir;
  391. bool pcm = true;
  392. dma = snd_soc_dai_get_dma_data(dai, substream);
  393. if (!dma)
  394. return -EIO;
  395. ch = params_channels(params);
  396. if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
  397. dir = SDW_DATA_DIR_RX;
  398. else
  399. dir = SDW_DATA_DIR_TX;
  400. if (dma->stream_type == SDW_STREAM_PDM) {
  401. /* TODO: Check whether PDM decimator is already in use */
  402. dma->nr_ports = sdw_cdns_get_stream(cdns, &cdns->pdm, ch, dir);
  403. pcm = false;
  404. } else {
  405. dma->nr_ports = sdw_cdns_get_stream(cdns, &cdns->pcm, ch, dir);
  406. }
  407. if (!dma->nr_ports) {
  408. dev_err(dai->dev, "ports/resources not available");
  409. return -EINVAL;
  410. }
  411. dma->port = kcalloc(dma->nr_ports, sizeof(*dma->port), GFP_KERNEL);
  412. if (!dma->port)
  413. return -ENOMEM;
  414. for (i = 0; i < dma->nr_ports; i++) {
  415. dma->port[i] = intel_alloc_port(sdw, ch, dir, pcm);
  416. if (!dma->port[i]) {
  417. ret = -EINVAL;
  418. goto port_error;
  419. }
  420. }
  421. /* Inform DSP about PDI stream number */
  422. for (i = 0; i < dma->nr_ports; i++) {
  423. ret = intel_config_stream(sdw, substream, dai, params,
  424. dma->port[i]->pdi->intel_alh_id);
  425. if (ret)
  426. goto port_error;
  427. }
  428. sconfig.direction = dir;
  429. sconfig.ch_count = ch;
  430. sconfig.frame_rate = params_rate(params);
  431. sconfig.type = dma->stream_type;
  432. if (dma->stream_type == SDW_STREAM_PDM) {
  433. sconfig.frame_rate *= 50;
  434. sconfig.bps = 1;
  435. } else {
  436. sconfig.bps = snd_pcm_format_width(params_format(params));
  437. }
  438. /* Port configuration */
  439. pconfig = kcalloc(dma->nr_ports, sizeof(*pconfig), GFP_KERNEL);
  440. if (!pconfig) {
  441. ret = -ENOMEM;
  442. goto port_error;
  443. }
  444. for (i = 0; i < dma->nr_ports; i++) {
  445. pconfig[i].num = dma->port[i]->num;
  446. pconfig[i].ch_mask = (1 << ch) - 1;
  447. }
  448. ret = sdw_stream_add_master(&cdns->bus, &sconfig,
  449. pconfig, dma->nr_ports, dma->stream);
  450. if (ret) {
  451. dev_err(cdns->dev, "add master to stream failed:%d", ret);
  452. goto stream_error;
  453. }
  454. kfree(pconfig);
  455. return ret;
  456. stream_error:
  457. kfree(pconfig);
  458. port_error:
  459. intel_port_cleanup(dma);
  460. kfree(dma->port);
  461. return ret;
  462. }
  463. static int
  464. intel_hw_free(struct snd_pcm_substream *substream, struct snd_soc_dai *dai)
  465. {
  466. struct sdw_cdns *cdns = snd_soc_dai_get_drvdata(dai);
  467. struct sdw_cdns_dma_data *dma;
  468. int ret;
  469. dma = snd_soc_dai_get_dma_data(dai, substream);
  470. if (!dma)
  471. return -EIO;
  472. ret = sdw_stream_remove_master(&cdns->bus, dma->stream);
  473. if (ret < 0)
  474. dev_err(dai->dev, "remove master from stream %s failed: %d",
  475. dma->stream->name, ret);
  476. intel_port_cleanup(dma);
  477. kfree(dma->port);
  478. return ret;
  479. }
  480. static int intel_pcm_set_sdw_stream(struct snd_soc_dai *dai,
  481. void *stream, int direction)
  482. {
  483. return cdns_set_sdw_stream(dai, stream, true, direction);
  484. }
  485. static int intel_pdm_set_sdw_stream(struct snd_soc_dai *dai,
  486. void *stream, int direction)
  487. {
  488. return cdns_set_sdw_stream(dai, stream, false, direction);
  489. }
  490. static struct snd_soc_dai_ops intel_pcm_dai_ops = {
  491. .hw_params = intel_hw_params,
  492. .hw_free = intel_hw_free,
  493. .shutdown = sdw_cdns_shutdown,
  494. .set_sdw_stream = intel_pcm_set_sdw_stream,
  495. };
  496. static struct snd_soc_dai_ops intel_pdm_dai_ops = {
  497. .hw_params = intel_hw_params,
  498. .hw_free = intel_hw_free,
  499. .shutdown = sdw_cdns_shutdown,
  500. .set_sdw_stream = intel_pdm_set_sdw_stream,
  501. };
  502. static const struct snd_soc_component_driver dai_component = {
  503. .name = "soundwire",
  504. };
  505. static int intel_create_dai(struct sdw_cdns *cdns,
  506. struct snd_soc_dai_driver *dais,
  507. enum intel_pdi_type type,
  508. u32 num, u32 off, u32 max_ch, bool pcm)
  509. {
  510. int i;
  511. if (num == 0)
  512. return 0;
  513. /* TODO: Read supported rates/formats from hardware */
  514. for (i = off; i < (off + num); i++) {
  515. dais[i].name = kasprintf(GFP_KERNEL, "SDW%d Pin%d",
  516. cdns->instance, i);
  517. if (!dais[i].name)
  518. return -ENOMEM;
  519. if (type == INTEL_PDI_BD || type == INTEL_PDI_OUT) {
  520. dais[i].playback.stream_name = kasprintf(GFP_KERNEL,
  521. "SDW%d Tx%d",
  522. cdns->instance, i);
  523. if (!dais[i].playback.stream_name) {
  524. kfree(dais[i].name);
  525. return -ENOMEM;
  526. }
  527. dais[i].playback.channels_min = 1;
  528. dais[i].playback.channels_max = max_ch;
  529. dais[i].playback.rates = SNDRV_PCM_RATE_48000;
  530. dais[i].playback.formats = SNDRV_PCM_FMTBIT_S16_LE;
  531. }
  532. if (type == INTEL_PDI_BD || type == INTEL_PDI_IN) {
  533. dais[i].capture.stream_name = kasprintf(GFP_KERNEL,
  534. "SDW%d Rx%d",
  535. cdns->instance, i);
  536. if (!dais[i].capture.stream_name) {
  537. kfree(dais[i].name);
  538. kfree(dais[i].playback.stream_name);
  539. return -ENOMEM;
  540. }
  541. dais[i].capture.channels_min = 1;
  542. dais[i].capture.channels_max = max_ch;
  543. dais[i].capture.rates = SNDRV_PCM_RATE_48000;
  544. dais[i].capture.formats = SNDRV_PCM_FMTBIT_S16_LE;
  545. }
  546. dais[i].id = SDW_DAI_ID_RANGE_START + i;
  547. if (pcm)
  548. dais[i].ops = &intel_pcm_dai_ops;
  549. else
  550. dais[i].ops = &intel_pdm_dai_ops;
  551. }
  552. return 0;
  553. }
  554. static int intel_register_dai(struct sdw_intel *sdw)
  555. {
  556. struct sdw_cdns *cdns = &sdw->cdns;
  557. struct sdw_cdns_streams *stream;
  558. struct snd_soc_dai_driver *dais;
  559. int num_dai, ret, off = 0;
  560. /* DAIs are created based on total number of PDIs supported */
  561. num_dai = cdns->pcm.num_pdi + cdns->pdm.num_pdi;
  562. dais = devm_kcalloc(cdns->dev, num_dai, sizeof(*dais), GFP_KERNEL);
  563. if (!dais)
  564. return -ENOMEM;
  565. /* Create PCM DAIs */
  566. stream = &cdns->pcm;
  567. ret = intel_create_dai(cdns, dais, INTEL_PDI_IN,
  568. stream->num_in, off, stream->num_ch_in, true);
  569. if (ret)
  570. return ret;
  571. off += cdns->pcm.num_in;
  572. ret = intel_create_dai(cdns, dais, INTEL_PDI_OUT,
  573. cdns->pcm.num_out, off, stream->num_ch_out, true);
  574. if (ret)
  575. return ret;
  576. off += cdns->pcm.num_out;
  577. ret = intel_create_dai(cdns, dais, INTEL_PDI_BD,
  578. cdns->pcm.num_bd, off, stream->num_ch_bd, true);
  579. if (ret)
  580. return ret;
  581. /* Create PDM DAIs */
  582. stream = &cdns->pdm;
  583. off += cdns->pcm.num_bd;
  584. ret = intel_create_dai(cdns, dais, INTEL_PDI_IN,
  585. cdns->pdm.num_in, off, stream->num_ch_in, false);
  586. if (ret)
  587. return ret;
  588. off += cdns->pdm.num_in;
  589. ret = intel_create_dai(cdns, dais, INTEL_PDI_OUT,
  590. cdns->pdm.num_out, off, stream->num_ch_out, false);
  591. if (ret)
  592. return ret;
  593. off += cdns->pdm.num_bd;
  594. ret = intel_create_dai(cdns, dais, INTEL_PDI_BD,
  595. cdns->pdm.num_bd, off, stream->num_ch_bd, false);
  596. if (ret)
  597. return ret;
  598. return snd_soc_register_component(cdns->dev, &dai_component,
  599. dais, num_dai);
  600. }
  601. static int intel_prop_read(struct sdw_bus *bus)
  602. {
  603. /* Initialize with default handler to read all DisCo properties */
  604. sdw_master_read_prop(bus);
  605. /* BIOS is not giving some values correctly. So, lets override them */
  606. bus->prop.num_freq = 1;
  607. bus->prop.freq = devm_kcalloc(bus->dev, sizeof(*bus->prop.freq),
  608. bus->prop.num_freq, GFP_KERNEL);
  609. if (!bus->prop.freq)
  610. return -ENOMEM;
  611. bus->prop.freq[0] = bus->prop.max_freq;
  612. bus->prop.err_threshold = 5;
  613. return 0;
  614. }
  615. static struct sdw_master_ops sdw_intel_ops = {
  616. .read_prop = sdw_master_read_prop,
  617. .xfer_msg = cdns_xfer_msg,
  618. .xfer_msg_defer = cdns_xfer_msg_defer,
  619. .reset_page_addr = cdns_reset_page_addr,
  620. .set_bus_conf = cdns_bus_conf,
  621. };
  622. /*
  623. * probe and init
  624. */
  625. static int intel_probe(struct platform_device *pdev)
  626. {
  627. struct sdw_cdns_stream_config config;
  628. struct sdw_intel *sdw;
  629. int ret;
  630. sdw = devm_kzalloc(&pdev->dev, sizeof(*sdw), GFP_KERNEL);
  631. if (!sdw)
  632. return -ENOMEM;
  633. sdw->instance = pdev->id;
  634. sdw->res = dev_get_platdata(&pdev->dev);
  635. sdw->cdns.dev = &pdev->dev;
  636. sdw->cdns.registers = sdw->res->registers;
  637. sdw->cdns.instance = sdw->instance;
  638. sdw->cdns.msg_count = 0;
  639. sdw->cdns.bus.dev = &pdev->dev;
  640. sdw->cdns.bus.link_id = pdev->id;
  641. sdw_cdns_probe(&sdw->cdns);
  642. /* Set property read ops */
  643. sdw_intel_ops.read_prop = intel_prop_read;
  644. sdw->cdns.bus.ops = &sdw_intel_ops;
  645. sdw_intel_ops.read_prop = intel_prop_read;
  646. sdw->cdns.bus.ops = &sdw_intel_ops;
  647. platform_set_drvdata(pdev, sdw);
  648. ret = sdw_add_bus_master(&sdw->cdns.bus);
  649. if (ret) {
  650. dev_err(&pdev->dev, "sdw_add_bus_master fail: %d\n", ret);
  651. goto err_master_reg;
  652. }
  653. /* Initialize shim and controller */
  654. intel_link_power_up(sdw);
  655. intel_shim_init(sdw);
  656. ret = sdw_cdns_init(&sdw->cdns);
  657. if (ret)
  658. goto err_init;
  659. ret = sdw_cdns_enable_interrupt(&sdw->cdns);
  660. /* Read the PDI config and initialize cadence PDI */
  661. intel_pdi_init(sdw, &config);
  662. ret = sdw_cdns_pdi_init(&sdw->cdns, config);
  663. if (ret)
  664. goto err_init;
  665. intel_pdi_ch_update(sdw);
  666. /* Acquire IRQ */
  667. ret = request_threaded_irq(sdw->res->irq, sdw_cdns_irq,
  668. sdw_cdns_thread, IRQF_SHARED, KBUILD_MODNAME,
  669. &sdw->cdns);
  670. if (ret < 0) {
  671. dev_err(sdw->cdns.dev, "unable to grab IRQ %d, disabling device\n",
  672. sdw->res->irq);
  673. goto err_init;
  674. }
  675. /* Register DAIs */
  676. ret = intel_register_dai(sdw);
  677. if (ret) {
  678. dev_err(sdw->cdns.dev, "DAI registration failed: %d", ret);
  679. snd_soc_unregister_component(sdw->cdns.dev);
  680. goto err_dai;
  681. }
  682. return 0;
  683. err_dai:
  684. free_irq(sdw->res->irq, sdw);
  685. err_init:
  686. sdw_delete_bus_master(&sdw->cdns.bus);
  687. err_master_reg:
  688. return ret;
  689. }
  690. static int intel_remove(struct platform_device *pdev)
  691. {
  692. struct sdw_intel *sdw;
  693. sdw = platform_get_drvdata(pdev);
  694. free_irq(sdw->res->irq, sdw);
  695. snd_soc_unregister_component(sdw->cdns.dev);
  696. sdw_delete_bus_master(&sdw->cdns.bus);
  697. return 0;
  698. }
  699. static struct platform_driver sdw_intel_drv = {
  700. .probe = intel_probe,
  701. .remove = intel_remove,
  702. .driver = {
  703. .name = "int-sdw",
  704. },
  705. };
  706. module_platform_driver(sdw_intel_drv);
  707. MODULE_LICENSE("Dual BSD/GPL");
  708. MODULE_ALIAS("platform:int-sdw");
  709. MODULE_DESCRIPTION("Intel Soundwire Master Driver");