intel.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928
  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. * bank switch routines
  318. */
  319. static int intel_pre_bank_switch(struct sdw_bus *bus)
  320. {
  321. struct sdw_cdns *cdns = bus_to_cdns(bus);
  322. struct sdw_intel *sdw = cdns_to_intel(cdns);
  323. void __iomem *shim = sdw->res->shim;
  324. int sync_reg;
  325. /* Write to register only for multi-link */
  326. if (!bus->multi_link)
  327. return 0;
  328. /* Read SYNC register */
  329. sync_reg = intel_readl(shim, SDW_SHIM_SYNC);
  330. sync_reg |= SDW_SHIM_SYNC_CMDSYNC << sdw->instance;
  331. intel_writel(shim, SDW_SHIM_SYNC, sync_reg);
  332. return 0;
  333. }
  334. static int intel_post_bank_switch(struct sdw_bus *bus)
  335. {
  336. struct sdw_cdns *cdns = bus_to_cdns(bus);
  337. struct sdw_intel *sdw = cdns_to_intel(cdns);
  338. void __iomem *shim = sdw->res->shim;
  339. int sync_reg, ret;
  340. /* Write to register only for multi-link */
  341. if (!bus->multi_link)
  342. return 0;
  343. /* Read SYNC register */
  344. sync_reg = intel_readl(shim, SDW_SHIM_SYNC);
  345. /*
  346. * post_bank_switch() ops is called from the bus in loop for
  347. * all the Masters in the steam with the expectation that
  348. * we trigger the bankswitch for the only first Master in the list
  349. * and do nothing for the other Masters
  350. *
  351. * So, set the SYNCGO bit only if CMDSYNC bit is set for any Master.
  352. */
  353. if (!(sync_reg & SDW_SHIM_SYNC_CMDSYNC_MASK))
  354. return 0;
  355. /*
  356. * Set SyncGO bit to synchronously trigger a bank switch for
  357. * all the masters. A write to SYNCGO bit clears CMDSYNC bit for all
  358. * the Masters.
  359. */
  360. sync_reg |= SDW_SHIM_SYNC_SYNCGO;
  361. ret = intel_clear_bit(shim, SDW_SHIM_SYNC, sync_reg,
  362. SDW_SHIM_SYNC_SYNCGO);
  363. if (ret < 0)
  364. dev_err(sdw->cdns.dev, "Post bank switch failed: %d", ret);
  365. return ret;
  366. }
  367. /*
  368. * DAI routines
  369. */
  370. static struct sdw_cdns_port *intel_alloc_port(struct sdw_intel *sdw,
  371. u32 ch, u32 dir, bool pcm)
  372. {
  373. struct sdw_cdns *cdns = &sdw->cdns;
  374. struct sdw_cdns_port *port = NULL;
  375. int i, ret = 0;
  376. for (i = 0; i < cdns->num_ports; i++) {
  377. if (cdns->ports[i].assigned == true)
  378. continue;
  379. port = &cdns->ports[i];
  380. port->assigned = true;
  381. port->direction = dir;
  382. port->ch = ch;
  383. break;
  384. }
  385. if (!port) {
  386. dev_err(cdns->dev, "Unable to find a free port\n");
  387. return NULL;
  388. }
  389. if (pcm) {
  390. ret = sdw_cdns_alloc_stream(cdns, &cdns->pcm, port, ch, dir);
  391. if (ret)
  392. goto out;
  393. intel_pdi_shim_configure(sdw, port->pdi);
  394. sdw_cdns_config_stream(cdns, port, ch, dir, port->pdi);
  395. intel_pdi_alh_configure(sdw, port->pdi);
  396. } else {
  397. ret = sdw_cdns_alloc_stream(cdns, &cdns->pdm, port, ch, dir);
  398. }
  399. out:
  400. if (ret) {
  401. port->assigned = false;
  402. port = NULL;
  403. }
  404. return port;
  405. }
  406. static void intel_port_cleanup(struct sdw_cdns_dma_data *dma)
  407. {
  408. int i;
  409. for (i = 0; i < dma->nr_ports; i++) {
  410. if (dma->port[i]) {
  411. dma->port[i]->pdi->assigned = false;
  412. dma->port[i]->pdi = NULL;
  413. dma->port[i]->assigned = false;
  414. dma->port[i] = NULL;
  415. }
  416. }
  417. }
  418. static int intel_hw_params(struct snd_pcm_substream *substream,
  419. struct snd_pcm_hw_params *params,
  420. struct snd_soc_dai *dai)
  421. {
  422. struct sdw_cdns *cdns = snd_soc_dai_get_drvdata(dai);
  423. struct sdw_intel *sdw = cdns_to_intel(cdns);
  424. struct sdw_cdns_dma_data *dma;
  425. struct sdw_stream_config sconfig;
  426. struct sdw_port_config *pconfig;
  427. int ret, i, ch, dir;
  428. bool pcm = true;
  429. dma = snd_soc_dai_get_dma_data(dai, substream);
  430. if (!dma)
  431. return -EIO;
  432. ch = params_channels(params);
  433. if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
  434. dir = SDW_DATA_DIR_RX;
  435. else
  436. dir = SDW_DATA_DIR_TX;
  437. if (dma->stream_type == SDW_STREAM_PDM) {
  438. /* TODO: Check whether PDM decimator is already in use */
  439. dma->nr_ports = sdw_cdns_get_stream(cdns, &cdns->pdm, ch, dir);
  440. pcm = false;
  441. } else {
  442. dma->nr_ports = sdw_cdns_get_stream(cdns, &cdns->pcm, ch, dir);
  443. }
  444. if (!dma->nr_ports) {
  445. dev_err(dai->dev, "ports/resources not available");
  446. return -EINVAL;
  447. }
  448. dma->port = kcalloc(dma->nr_ports, sizeof(*dma->port), GFP_KERNEL);
  449. if (!dma->port)
  450. return -ENOMEM;
  451. for (i = 0; i < dma->nr_ports; i++) {
  452. dma->port[i] = intel_alloc_port(sdw, ch, dir, pcm);
  453. if (!dma->port[i]) {
  454. ret = -EINVAL;
  455. goto port_error;
  456. }
  457. }
  458. /* Inform DSP about PDI stream number */
  459. for (i = 0; i < dma->nr_ports; i++) {
  460. ret = intel_config_stream(sdw, substream, dai, params,
  461. dma->port[i]->pdi->intel_alh_id);
  462. if (ret)
  463. goto port_error;
  464. }
  465. sconfig.direction = dir;
  466. sconfig.ch_count = ch;
  467. sconfig.frame_rate = params_rate(params);
  468. sconfig.type = dma->stream_type;
  469. if (dma->stream_type == SDW_STREAM_PDM) {
  470. sconfig.frame_rate *= 50;
  471. sconfig.bps = 1;
  472. } else {
  473. sconfig.bps = snd_pcm_format_width(params_format(params));
  474. }
  475. /* Port configuration */
  476. pconfig = kcalloc(dma->nr_ports, sizeof(*pconfig), GFP_KERNEL);
  477. if (!pconfig) {
  478. ret = -ENOMEM;
  479. goto port_error;
  480. }
  481. for (i = 0; i < dma->nr_ports; i++) {
  482. pconfig[i].num = dma->port[i]->num;
  483. pconfig[i].ch_mask = (1 << ch) - 1;
  484. }
  485. ret = sdw_stream_add_master(&cdns->bus, &sconfig,
  486. pconfig, dma->nr_ports, dma->stream);
  487. if (ret) {
  488. dev_err(cdns->dev, "add master to stream failed:%d", ret);
  489. goto stream_error;
  490. }
  491. kfree(pconfig);
  492. return ret;
  493. stream_error:
  494. kfree(pconfig);
  495. port_error:
  496. intel_port_cleanup(dma);
  497. kfree(dma->port);
  498. return ret;
  499. }
  500. static int
  501. intel_hw_free(struct snd_pcm_substream *substream, struct snd_soc_dai *dai)
  502. {
  503. struct sdw_cdns *cdns = snd_soc_dai_get_drvdata(dai);
  504. struct sdw_cdns_dma_data *dma;
  505. int ret;
  506. dma = snd_soc_dai_get_dma_data(dai, substream);
  507. if (!dma)
  508. return -EIO;
  509. ret = sdw_stream_remove_master(&cdns->bus, dma->stream);
  510. if (ret < 0)
  511. dev_err(dai->dev, "remove master from stream %s failed: %d",
  512. dma->stream->name, ret);
  513. intel_port_cleanup(dma);
  514. kfree(dma->port);
  515. return ret;
  516. }
  517. static int intel_pcm_set_sdw_stream(struct snd_soc_dai *dai,
  518. void *stream, int direction)
  519. {
  520. return cdns_set_sdw_stream(dai, stream, true, direction);
  521. }
  522. static int intel_pdm_set_sdw_stream(struct snd_soc_dai *dai,
  523. void *stream, int direction)
  524. {
  525. return cdns_set_sdw_stream(dai, stream, false, direction);
  526. }
  527. static struct snd_soc_dai_ops intel_pcm_dai_ops = {
  528. .hw_params = intel_hw_params,
  529. .hw_free = intel_hw_free,
  530. .shutdown = sdw_cdns_shutdown,
  531. .set_sdw_stream = intel_pcm_set_sdw_stream,
  532. };
  533. static struct snd_soc_dai_ops intel_pdm_dai_ops = {
  534. .hw_params = intel_hw_params,
  535. .hw_free = intel_hw_free,
  536. .shutdown = sdw_cdns_shutdown,
  537. .set_sdw_stream = intel_pdm_set_sdw_stream,
  538. };
  539. static const struct snd_soc_component_driver dai_component = {
  540. .name = "soundwire",
  541. };
  542. static int intel_create_dai(struct sdw_cdns *cdns,
  543. struct snd_soc_dai_driver *dais,
  544. enum intel_pdi_type type,
  545. u32 num, u32 off, u32 max_ch, bool pcm)
  546. {
  547. int i;
  548. if (num == 0)
  549. return 0;
  550. /* TODO: Read supported rates/formats from hardware */
  551. for (i = off; i < (off + num); i++) {
  552. dais[i].name = kasprintf(GFP_KERNEL, "SDW%d Pin%d",
  553. cdns->instance, i);
  554. if (!dais[i].name)
  555. return -ENOMEM;
  556. if (type == INTEL_PDI_BD || type == INTEL_PDI_OUT) {
  557. dais[i].playback.stream_name = kasprintf(GFP_KERNEL,
  558. "SDW%d Tx%d",
  559. cdns->instance, i);
  560. if (!dais[i].playback.stream_name) {
  561. kfree(dais[i].name);
  562. return -ENOMEM;
  563. }
  564. dais[i].playback.channels_min = 1;
  565. dais[i].playback.channels_max = max_ch;
  566. dais[i].playback.rates = SNDRV_PCM_RATE_48000;
  567. dais[i].playback.formats = SNDRV_PCM_FMTBIT_S16_LE;
  568. }
  569. if (type == INTEL_PDI_BD || type == INTEL_PDI_IN) {
  570. dais[i].capture.stream_name = kasprintf(GFP_KERNEL,
  571. "SDW%d Rx%d",
  572. cdns->instance, i);
  573. if (!dais[i].capture.stream_name) {
  574. kfree(dais[i].name);
  575. kfree(dais[i].playback.stream_name);
  576. return -ENOMEM;
  577. }
  578. dais[i].playback.channels_min = 1;
  579. dais[i].playback.channels_max = max_ch;
  580. dais[i].capture.rates = SNDRV_PCM_RATE_48000;
  581. dais[i].capture.formats = SNDRV_PCM_FMTBIT_S16_LE;
  582. }
  583. dais[i].id = SDW_DAI_ID_RANGE_START + i;
  584. if (pcm)
  585. dais[i].ops = &intel_pcm_dai_ops;
  586. else
  587. dais[i].ops = &intel_pdm_dai_ops;
  588. }
  589. return 0;
  590. }
  591. static int intel_register_dai(struct sdw_intel *sdw)
  592. {
  593. struct sdw_cdns *cdns = &sdw->cdns;
  594. struct sdw_cdns_streams *stream;
  595. struct snd_soc_dai_driver *dais;
  596. int num_dai, ret, off = 0;
  597. /* DAIs are created based on total number of PDIs supported */
  598. num_dai = cdns->pcm.num_pdi + cdns->pdm.num_pdi;
  599. dais = devm_kcalloc(cdns->dev, num_dai, sizeof(*dais), GFP_KERNEL);
  600. if (!dais)
  601. return -ENOMEM;
  602. /* Create PCM DAIs */
  603. stream = &cdns->pcm;
  604. ret = intel_create_dai(cdns, dais, INTEL_PDI_IN,
  605. stream->num_in, off, stream->num_ch_in, true);
  606. if (ret)
  607. return ret;
  608. off += cdns->pcm.num_in;
  609. ret = intel_create_dai(cdns, dais, INTEL_PDI_OUT,
  610. cdns->pcm.num_out, off, stream->num_ch_out, true);
  611. if (ret)
  612. return ret;
  613. off += cdns->pcm.num_out;
  614. ret = intel_create_dai(cdns, dais, INTEL_PDI_BD,
  615. cdns->pcm.num_bd, off, stream->num_ch_bd, true);
  616. if (ret)
  617. return ret;
  618. /* Create PDM DAIs */
  619. stream = &cdns->pdm;
  620. off += cdns->pcm.num_bd;
  621. ret = intel_create_dai(cdns, dais, INTEL_PDI_IN,
  622. cdns->pdm.num_in, off, stream->num_ch_in, false);
  623. if (ret)
  624. return ret;
  625. off += cdns->pdm.num_in;
  626. ret = intel_create_dai(cdns, dais, INTEL_PDI_OUT,
  627. cdns->pdm.num_out, off, stream->num_ch_out, false);
  628. if (ret)
  629. return ret;
  630. off += cdns->pdm.num_bd;
  631. ret = intel_create_dai(cdns, dais, INTEL_PDI_BD,
  632. cdns->pdm.num_bd, off, stream->num_ch_bd, false);
  633. if (ret)
  634. return ret;
  635. return snd_soc_register_component(cdns->dev, &dai_component,
  636. dais, num_dai);
  637. }
  638. static int intel_prop_read(struct sdw_bus *bus)
  639. {
  640. /* Initialize with default handler to read all DisCo properties */
  641. sdw_master_read_prop(bus);
  642. /* BIOS is not giving some values correctly. So, lets override them */
  643. bus->prop.num_freq = 1;
  644. bus->prop.freq = devm_kcalloc(bus->dev, sizeof(*bus->prop.freq),
  645. bus->prop.num_freq, GFP_KERNEL);
  646. if (!bus->prop.freq)
  647. return -ENOMEM;
  648. bus->prop.freq[0] = bus->prop.max_freq;
  649. bus->prop.err_threshold = 5;
  650. return 0;
  651. }
  652. static struct sdw_master_ops sdw_intel_ops = {
  653. .read_prop = sdw_master_read_prop,
  654. .xfer_msg = cdns_xfer_msg,
  655. .xfer_msg_defer = cdns_xfer_msg_defer,
  656. .reset_page_addr = cdns_reset_page_addr,
  657. .set_bus_conf = cdns_bus_conf,
  658. .pre_bank_switch = intel_pre_bank_switch,
  659. .post_bank_switch = intel_post_bank_switch,
  660. };
  661. /*
  662. * probe and init
  663. */
  664. static int intel_probe(struct platform_device *pdev)
  665. {
  666. struct sdw_cdns_stream_config config;
  667. struct sdw_intel *sdw;
  668. int ret;
  669. sdw = devm_kzalloc(&pdev->dev, sizeof(*sdw), GFP_KERNEL);
  670. if (!sdw)
  671. return -ENOMEM;
  672. sdw->instance = pdev->id;
  673. sdw->res = dev_get_platdata(&pdev->dev);
  674. sdw->cdns.dev = &pdev->dev;
  675. sdw->cdns.registers = sdw->res->registers;
  676. sdw->cdns.instance = sdw->instance;
  677. sdw->cdns.msg_count = 0;
  678. sdw->cdns.bus.dev = &pdev->dev;
  679. sdw->cdns.bus.link_id = pdev->id;
  680. sdw_cdns_probe(&sdw->cdns);
  681. /* Set property read ops */
  682. sdw_intel_ops.read_prop = intel_prop_read;
  683. sdw->cdns.bus.ops = &sdw_intel_ops;
  684. platform_set_drvdata(pdev, sdw);
  685. ret = sdw_add_bus_master(&sdw->cdns.bus);
  686. if (ret) {
  687. dev_err(&pdev->dev, "sdw_add_bus_master fail: %d\n", ret);
  688. goto err_master_reg;
  689. }
  690. /* Initialize shim and controller */
  691. intel_link_power_up(sdw);
  692. intel_shim_init(sdw);
  693. ret = sdw_cdns_init(&sdw->cdns);
  694. if (ret)
  695. goto err_init;
  696. ret = sdw_cdns_enable_interrupt(&sdw->cdns);
  697. /* Read the PDI config and initialize cadence PDI */
  698. intel_pdi_init(sdw, &config);
  699. ret = sdw_cdns_pdi_init(&sdw->cdns, config);
  700. if (ret)
  701. goto err_init;
  702. intel_pdi_ch_update(sdw);
  703. /* Acquire IRQ */
  704. ret = request_threaded_irq(sdw->res->irq, sdw_cdns_irq,
  705. sdw_cdns_thread, IRQF_SHARED, KBUILD_MODNAME,
  706. &sdw->cdns);
  707. if (ret < 0) {
  708. dev_err(sdw->cdns.dev, "unable to grab IRQ %d, disabling device\n",
  709. sdw->res->irq);
  710. goto err_init;
  711. }
  712. /* Register DAIs */
  713. ret = intel_register_dai(sdw);
  714. if (ret) {
  715. dev_err(sdw->cdns.dev, "DAI registration failed: %d", ret);
  716. snd_soc_unregister_component(sdw->cdns.dev);
  717. goto err_dai;
  718. }
  719. return 0;
  720. err_dai:
  721. free_irq(sdw->res->irq, sdw);
  722. err_init:
  723. sdw_delete_bus_master(&sdw->cdns.bus);
  724. err_master_reg:
  725. return ret;
  726. }
  727. static int intel_remove(struct platform_device *pdev)
  728. {
  729. struct sdw_intel *sdw;
  730. sdw = platform_get_drvdata(pdev);
  731. free_irq(sdw->res->irq, sdw);
  732. snd_soc_unregister_component(sdw->cdns.dev);
  733. sdw_delete_bus_master(&sdw->cdns.bus);
  734. return 0;
  735. }
  736. static struct platform_driver sdw_intel_drv = {
  737. .probe = intel_probe,
  738. .remove = intel_remove,
  739. .driver = {
  740. .name = "int-sdw",
  741. },
  742. };
  743. module_platform_driver(sdw_intel_drv);
  744. MODULE_LICENSE("Dual BSD/GPL");
  745. MODULE_ALIAS("platform:int-sdw");
  746. MODULE_DESCRIPTION("Intel Soundwire Master Driver");