intel.c 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045
  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/debugfs.h>
  8. #include <linux/delay.h>
  9. #include <linux/module.h>
  10. #include <linux/interrupt.h>
  11. #include <linux/io.h>
  12. #include <linux/auxiliary_bus.h>
  13. #include <sound/pcm_params.h>
  14. #include <linux/pm_runtime.h>
  15. #include <sound/soc.h>
  16. #include <linux/soundwire/sdw_registers.h>
  17. #include <linux/soundwire/sdw.h>
  18. #include <linux/soundwire/sdw_intel.h>
  19. #include "cadence_master.h"
  20. #include "bus.h"
  21. #include "intel.h"
  22. /* IDA min selected to avoid conflicts with HDaudio/iDISP SDI values */
  23. #define INTEL_DEV_NUM_IDA_MIN 4
  24. #define INTEL_MASTER_SUSPEND_DELAY_MS 3000
  25. #define INTEL_MASTER_RESET_ITERATIONS 10
  26. /*
  27. * debug/config flags for the Intel SoundWire Master.
  28. *
  29. * Since we may have multiple masters active, we can have up to 8
  30. * flags reused in each byte, with master0 using the ls-byte, etc.
  31. */
  32. #define SDW_INTEL_MASTER_DISABLE_PM_RUNTIME BIT(0)
  33. #define SDW_INTEL_MASTER_DISABLE_CLOCK_STOP BIT(1)
  34. #define SDW_INTEL_MASTER_DISABLE_PM_RUNTIME_IDLE BIT(2)
  35. #define SDW_INTEL_MASTER_DISABLE_MULTI_LINK BIT(3)
  36. static int md_flags;
  37. module_param_named(sdw_md_flags, md_flags, int, 0444);
  38. MODULE_PARM_DESC(sdw_md_flags, "SoundWire Intel Master device flags (0x0 all off)");
  39. enum intel_pdi_type {
  40. INTEL_PDI_IN = 0,
  41. INTEL_PDI_OUT = 1,
  42. INTEL_PDI_BD = 2,
  43. };
  44. #define cdns_to_intel(_cdns) container_of(_cdns, struct sdw_intel, cdns)
  45. /*
  46. * Read, write helpers for HW registers
  47. */
  48. static inline int intel_readl(void __iomem *base, int offset)
  49. {
  50. return readl(base + offset);
  51. }
  52. static inline void intel_writel(void __iomem *base, int offset, int value)
  53. {
  54. writel(value, base + offset);
  55. }
  56. static inline u16 intel_readw(void __iomem *base, int offset)
  57. {
  58. return readw(base + offset);
  59. }
  60. static inline void intel_writew(void __iomem *base, int offset, u16 value)
  61. {
  62. writew(value, base + offset);
  63. }
  64. static int intel_wait_bit(void __iomem *base, int offset, u32 mask, u32 target)
  65. {
  66. int timeout = 10;
  67. u32 reg_read;
  68. do {
  69. reg_read = readl(base + offset);
  70. if ((reg_read & mask) == target)
  71. return 0;
  72. timeout--;
  73. usleep_range(50, 100);
  74. } while (timeout != 0);
  75. return -EAGAIN;
  76. }
  77. static int intel_clear_bit(void __iomem *base, int offset, u32 value, u32 mask)
  78. {
  79. writel(value, base + offset);
  80. return intel_wait_bit(base, offset, mask, 0);
  81. }
  82. static int intel_set_bit(void __iomem *base, int offset, u32 value, u32 mask)
  83. {
  84. writel(value, base + offset);
  85. return intel_wait_bit(base, offset, mask, mask);
  86. }
  87. /*
  88. * debugfs
  89. */
  90. #ifdef CONFIG_DEBUG_FS
  91. #define RD_BUF (2 * PAGE_SIZE)
  92. static ssize_t intel_sprintf(void __iomem *mem, bool l,
  93. char *buf, size_t pos, unsigned int reg)
  94. {
  95. int value;
  96. if (l)
  97. value = intel_readl(mem, reg);
  98. else
  99. value = intel_readw(mem, reg);
  100. return scnprintf(buf + pos, RD_BUF - pos, "%4x\t%4x\n", reg, value);
  101. }
  102. static int intel_reg_show(struct seq_file *s_file, void *data)
  103. {
  104. struct sdw_intel *sdw = s_file->private;
  105. void __iomem *s = sdw->link_res->shim;
  106. void __iomem *a = sdw->link_res->alh;
  107. char *buf;
  108. ssize_t ret;
  109. int i, j;
  110. unsigned int links, reg;
  111. buf = kzalloc(RD_BUF, GFP_KERNEL);
  112. if (!buf)
  113. return -ENOMEM;
  114. links = intel_readl(s, SDW_SHIM_LCAP) & SDW_SHIM_LCAP_LCOUNT_MASK;
  115. ret = scnprintf(buf, RD_BUF, "Register Value\n");
  116. ret += scnprintf(buf + ret, RD_BUF - ret, "\nShim\n");
  117. for (i = 0; i < links; i++) {
  118. reg = SDW_SHIM_LCAP + i * 4;
  119. ret += intel_sprintf(s, true, buf, ret, reg);
  120. }
  121. for (i = 0; i < links; i++) {
  122. ret += scnprintf(buf + ret, RD_BUF - ret, "\nLink%d\n", i);
  123. ret += intel_sprintf(s, false, buf, ret, SDW_SHIM_CTLSCAP(i));
  124. ret += intel_sprintf(s, false, buf, ret, SDW_SHIM_CTLS0CM(i));
  125. ret += intel_sprintf(s, false, buf, ret, SDW_SHIM_CTLS1CM(i));
  126. ret += intel_sprintf(s, false, buf, ret, SDW_SHIM_CTLS2CM(i));
  127. ret += intel_sprintf(s, false, buf, ret, SDW_SHIM_CTLS3CM(i));
  128. ret += intel_sprintf(s, false, buf, ret, SDW_SHIM_PCMSCAP(i));
  129. ret += scnprintf(buf + ret, RD_BUF - ret, "\n PCMSyCH registers\n");
  130. /*
  131. * the value 10 is the number of PDIs. We will need a
  132. * cleanup to remove hard-coded Intel configurations
  133. * from cadence_master.c
  134. */
  135. for (j = 0; j < 10; j++) {
  136. ret += intel_sprintf(s, false, buf, ret,
  137. SDW_SHIM_PCMSYCHM(i, j));
  138. ret += intel_sprintf(s, false, buf, ret,
  139. SDW_SHIM_PCMSYCHC(i, j));
  140. }
  141. ret += scnprintf(buf + ret, RD_BUF - ret, "\n IOCTL, CTMCTL\n");
  142. ret += intel_sprintf(s, false, buf, ret, SDW_SHIM_IOCTL(i));
  143. ret += intel_sprintf(s, false, buf, ret, SDW_SHIM_CTMCTL(i));
  144. }
  145. ret += scnprintf(buf + ret, RD_BUF - ret, "\nWake registers\n");
  146. ret += intel_sprintf(s, false, buf, ret, SDW_SHIM_WAKEEN);
  147. ret += intel_sprintf(s, false, buf, ret, SDW_SHIM_WAKESTS);
  148. ret += scnprintf(buf + ret, RD_BUF - ret, "\nALH STRMzCFG\n");
  149. for (i = 0; i < SDW_ALH_NUM_STREAMS; i++)
  150. ret += intel_sprintf(a, true, buf, ret, SDW_ALH_STRMZCFG(i));
  151. seq_printf(s_file, "%s", buf);
  152. kfree(buf);
  153. return 0;
  154. }
  155. DEFINE_SHOW_ATTRIBUTE(intel_reg);
  156. static int intel_set_m_datamode(void *data, u64 value)
  157. {
  158. struct sdw_intel *sdw = data;
  159. struct sdw_bus *bus = &sdw->cdns.bus;
  160. if (value > SDW_PORT_DATA_MODE_STATIC_1)
  161. return -EINVAL;
  162. /* Userspace changed the hardware state behind the kernel's back */
  163. add_taint(TAINT_USER, LOCKDEP_STILL_OK);
  164. bus->params.m_data_mode = value;
  165. return 0;
  166. }
  167. DEFINE_DEBUGFS_ATTRIBUTE(intel_set_m_datamode_fops, NULL,
  168. intel_set_m_datamode, "%llu\n");
  169. static int intel_set_s_datamode(void *data, u64 value)
  170. {
  171. struct sdw_intel *sdw = data;
  172. struct sdw_bus *bus = &sdw->cdns.bus;
  173. if (value > SDW_PORT_DATA_MODE_STATIC_1)
  174. return -EINVAL;
  175. /* Userspace changed the hardware state behind the kernel's back */
  176. add_taint(TAINT_USER, LOCKDEP_STILL_OK);
  177. bus->params.s_data_mode = value;
  178. return 0;
  179. }
  180. DEFINE_DEBUGFS_ATTRIBUTE(intel_set_s_datamode_fops, NULL,
  181. intel_set_s_datamode, "%llu\n");
  182. static void intel_debugfs_init(struct sdw_intel *sdw)
  183. {
  184. struct dentry *root = sdw->cdns.bus.debugfs;
  185. if (!root)
  186. return;
  187. sdw->debugfs = debugfs_create_dir("intel-sdw", root);
  188. debugfs_create_file("intel-registers", 0400, sdw->debugfs, sdw,
  189. &intel_reg_fops);
  190. debugfs_create_file("intel-m-datamode", 0200, sdw->debugfs, sdw,
  191. &intel_set_m_datamode_fops);
  192. debugfs_create_file("intel-s-datamode", 0200, sdw->debugfs, sdw,
  193. &intel_set_s_datamode_fops);
  194. sdw_cdns_debugfs_init(&sdw->cdns, sdw->debugfs);
  195. }
  196. static void intel_debugfs_exit(struct sdw_intel *sdw)
  197. {
  198. debugfs_remove_recursive(sdw->debugfs);
  199. }
  200. #else
  201. static void intel_debugfs_init(struct sdw_intel *sdw) {}
  202. static void intel_debugfs_exit(struct sdw_intel *sdw) {}
  203. #endif /* CONFIG_DEBUG_FS */
  204. /*
  205. * shim ops
  206. */
  207. /* this needs to be called with shim_lock */
  208. static void intel_shim_glue_to_master_ip(struct sdw_intel *sdw)
  209. {
  210. void __iomem *shim = sdw->link_res->shim;
  211. unsigned int link_id = sdw->instance;
  212. u16 ioctl;
  213. /* Switch to MIP from Glue logic */
  214. ioctl = intel_readw(shim, SDW_SHIM_IOCTL(link_id));
  215. ioctl &= ~(SDW_SHIM_IOCTL_DOE);
  216. intel_writew(shim, SDW_SHIM_IOCTL(link_id), ioctl);
  217. usleep_range(10, 15);
  218. ioctl &= ~(SDW_SHIM_IOCTL_DO);
  219. intel_writew(shim, SDW_SHIM_IOCTL(link_id), ioctl);
  220. usleep_range(10, 15);
  221. ioctl |= (SDW_SHIM_IOCTL_MIF);
  222. intel_writew(shim, SDW_SHIM_IOCTL(link_id), ioctl);
  223. usleep_range(10, 15);
  224. ioctl &= ~(SDW_SHIM_IOCTL_BKE);
  225. ioctl &= ~(SDW_SHIM_IOCTL_COE);
  226. intel_writew(shim, SDW_SHIM_IOCTL(link_id), ioctl);
  227. usleep_range(10, 15);
  228. /* at this point Master IP has full control of the I/Os */
  229. }
  230. /* this needs to be called with shim_lock */
  231. static void intel_shim_master_ip_to_glue(struct sdw_intel *sdw)
  232. {
  233. unsigned int link_id = sdw->instance;
  234. void __iomem *shim = sdw->link_res->shim;
  235. u16 ioctl;
  236. /* Glue logic */
  237. ioctl = intel_readw(shim, SDW_SHIM_IOCTL(link_id));
  238. ioctl |= SDW_SHIM_IOCTL_BKE;
  239. ioctl |= SDW_SHIM_IOCTL_COE;
  240. intel_writew(shim, SDW_SHIM_IOCTL(link_id), ioctl);
  241. usleep_range(10, 15);
  242. ioctl &= ~(SDW_SHIM_IOCTL_MIF);
  243. intel_writew(shim, SDW_SHIM_IOCTL(link_id), ioctl);
  244. usleep_range(10, 15);
  245. /* at this point Integration Glue has full control of the I/Os */
  246. }
  247. /* this needs to be called with shim_lock */
  248. static void intel_shim_init(struct sdw_intel *sdw)
  249. {
  250. void __iomem *shim = sdw->link_res->shim;
  251. unsigned int link_id = sdw->instance;
  252. u16 ioctl = 0, act = 0;
  253. /* Initialize Shim */
  254. ioctl |= SDW_SHIM_IOCTL_BKE;
  255. intel_writew(shim, SDW_SHIM_IOCTL(link_id), ioctl);
  256. usleep_range(10, 15);
  257. ioctl |= SDW_SHIM_IOCTL_WPDD;
  258. intel_writew(shim, SDW_SHIM_IOCTL(link_id), ioctl);
  259. usleep_range(10, 15);
  260. ioctl |= SDW_SHIM_IOCTL_DO;
  261. intel_writew(shim, SDW_SHIM_IOCTL(link_id), ioctl);
  262. usleep_range(10, 15);
  263. ioctl |= SDW_SHIM_IOCTL_DOE;
  264. intel_writew(shim, SDW_SHIM_IOCTL(link_id), ioctl);
  265. usleep_range(10, 15);
  266. intel_shim_glue_to_master_ip(sdw);
  267. u16p_replace_bits(&act, 0x1, SDW_SHIM_CTMCTL_DOAIS);
  268. act |= SDW_SHIM_CTMCTL_DACTQE;
  269. act |= SDW_SHIM_CTMCTL_DODS;
  270. intel_writew(shim, SDW_SHIM_CTMCTL(link_id), act);
  271. usleep_range(10, 15);
  272. }
  273. static int intel_shim_check_wake(struct sdw_intel *sdw)
  274. {
  275. void __iomem *shim;
  276. u16 wake_sts;
  277. shim = sdw->link_res->shim;
  278. wake_sts = intel_readw(shim, SDW_SHIM_WAKESTS);
  279. return wake_sts & BIT(sdw->instance);
  280. }
  281. static void intel_shim_wake(struct sdw_intel *sdw, bool wake_enable)
  282. {
  283. void __iomem *shim = sdw->link_res->shim;
  284. unsigned int link_id = sdw->instance;
  285. u16 wake_en, wake_sts;
  286. mutex_lock(sdw->link_res->shim_lock);
  287. wake_en = intel_readw(shim, SDW_SHIM_WAKEEN);
  288. if (wake_enable) {
  289. /* Enable the wakeup */
  290. wake_en |= (SDW_SHIM_WAKEEN_ENABLE << link_id);
  291. intel_writew(shim, SDW_SHIM_WAKEEN, wake_en);
  292. } else {
  293. /* Disable the wake up interrupt */
  294. wake_en &= ~(SDW_SHIM_WAKEEN_ENABLE << link_id);
  295. intel_writew(shim, SDW_SHIM_WAKEEN, wake_en);
  296. /* Clear wake status */
  297. wake_sts = intel_readw(shim, SDW_SHIM_WAKESTS);
  298. wake_sts |= (SDW_SHIM_WAKESTS_STATUS << link_id);
  299. intel_writew(shim, SDW_SHIM_WAKESTS, wake_sts);
  300. }
  301. mutex_unlock(sdw->link_res->shim_lock);
  302. }
  303. static int intel_link_power_up(struct sdw_intel *sdw)
  304. {
  305. unsigned int link_id = sdw->instance;
  306. void __iomem *shim = sdw->link_res->shim;
  307. u32 *shim_mask = sdw->link_res->shim_mask;
  308. struct sdw_bus *bus = &sdw->cdns.bus;
  309. struct sdw_master_prop *prop = &bus->prop;
  310. u32 spa_mask, cpa_mask;
  311. u32 link_control;
  312. int ret = 0;
  313. u32 syncprd;
  314. u32 sync_reg;
  315. mutex_lock(sdw->link_res->shim_lock);
  316. /*
  317. * The hardware relies on an internal counter, typically 4kHz,
  318. * to generate the SoundWire SSP - which defines a 'safe'
  319. * synchronization point between commands and audio transport
  320. * and allows for multi link synchronization. The SYNCPRD value
  321. * is only dependent on the oscillator clock provided to
  322. * the IP, so adjust based on _DSD properties reported in DSDT
  323. * tables. The values reported are based on either 24MHz
  324. * (CNL/CML) or 38.4 MHz (ICL/TGL+).
  325. */
  326. if (prop->mclk_freq % 6000000)
  327. syncprd = SDW_SHIM_SYNC_SYNCPRD_VAL_38_4;
  328. else
  329. syncprd = SDW_SHIM_SYNC_SYNCPRD_VAL_24;
  330. if (!*shim_mask) {
  331. dev_dbg(sdw->cdns.dev, "powering up all links\n");
  332. /* we first need to program the SyncPRD/CPU registers */
  333. dev_dbg(sdw->cdns.dev,
  334. "first link up, programming SYNCPRD\n");
  335. /* set SyncPRD period */
  336. sync_reg = intel_readl(shim, SDW_SHIM_SYNC);
  337. u32p_replace_bits(&sync_reg, syncprd, SDW_SHIM_SYNC_SYNCPRD);
  338. /* Set SyncCPU bit */
  339. sync_reg |= SDW_SHIM_SYNC_SYNCCPU;
  340. intel_writel(shim, SDW_SHIM_SYNC, sync_reg);
  341. /* Link power up sequence */
  342. link_control = intel_readl(shim, SDW_SHIM_LCTL);
  343. /* only power-up enabled links */
  344. spa_mask = FIELD_PREP(SDW_SHIM_LCTL_SPA_MASK, sdw->link_res->link_mask);
  345. cpa_mask = FIELD_PREP(SDW_SHIM_LCTL_CPA_MASK, sdw->link_res->link_mask);
  346. link_control |= spa_mask;
  347. ret = intel_set_bit(shim, SDW_SHIM_LCTL, link_control, cpa_mask);
  348. if (ret < 0) {
  349. dev_err(sdw->cdns.dev, "Failed to power up link: %d\n", ret);
  350. goto out;
  351. }
  352. /* SyncCPU will change once link is active */
  353. ret = intel_wait_bit(shim, SDW_SHIM_SYNC,
  354. SDW_SHIM_SYNC_SYNCCPU, 0);
  355. if (ret < 0) {
  356. dev_err(sdw->cdns.dev,
  357. "Failed to set SHIM_SYNC: %d\n", ret);
  358. goto out;
  359. }
  360. }
  361. *shim_mask |= BIT(link_id);
  362. sdw->cdns.link_up = true;
  363. intel_shim_init(sdw);
  364. out:
  365. mutex_unlock(sdw->link_res->shim_lock);
  366. return ret;
  367. }
  368. static int intel_link_power_down(struct sdw_intel *sdw)
  369. {
  370. u32 link_control, spa_mask, cpa_mask;
  371. unsigned int link_id = sdw->instance;
  372. void __iomem *shim = sdw->link_res->shim;
  373. u32 *shim_mask = sdw->link_res->shim_mask;
  374. int ret = 0;
  375. mutex_lock(sdw->link_res->shim_lock);
  376. if (!(*shim_mask & BIT(link_id)))
  377. dev_err(sdw->cdns.dev,
  378. "%s: Unbalanced power-up/down calls\n", __func__);
  379. sdw->cdns.link_up = false;
  380. intel_shim_master_ip_to_glue(sdw);
  381. *shim_mask &= ~BIT(link_id);
  382. if (!*shim_mask) {
  383. dev_dbg(sdw->cdns.dev, "powering down all links\n");
  384. /* Link power down sequence */
  385. link_control = intel_readl(shim, SDW_SHIM_LCTL);
  386. /* only power-down enabled links */
  387. spa_mask = FIELD_PREP(SDW_SHIM_LCTL_SPA_MASK, ~sdw->link_res->link_mask);
  388. cpa_mask = FIELD_PREP(SDW_SHIM_LCTL_CPA_MASK, sdw->link_res->link_mask);
  389. link_control &= spa_mask;
  390. ret = intel_clear_bit(shim, SDW_SHIM_LCTL, link_control, cpa_mask);
  391. if (ret < 0) {
  392. dev_err(sdw->cdns.dev, "%s: could not power down link\n", __func__);
  393. /*
  394. * we leave the sdw->cdns.link_up flag as false since we've disabled
  395. * the link at this point and cannot handle interrupts any longer.
  396. */
  397. }
  398. }
  399. mutex_unlock(sdw->link_res->shim_lock);
  400. return ret;
  401. }
  402. static void intel_shim_sync_arm(struct sdw_intel *sdw)
  403. {
  404. void __iomem *shim = sdw->link_res->shim;
  405. u32 sync_reg;
  406. mutex_lock(sdw->link_res->shim_lock);
  407. /* update SYNC register */
  408. sync_reg = intel_readl(shim, SDW_SHIM_SYNC);
  409. sync_reg |= (SDW_SHIM_SYNC_CMDSYNC << sdw->instance);
  410. intel_writel(shim, SDW_SHIM_SYNC, sync_reg);
  411. mutex_unlock(sdw->link_res->shim_lock);
  412. }
  413. static int intel_shim_sync_go_unlocked(struct sdw_intel *sdw)
  414. {
  415. void __iomem *shim = sdw->link_res->shim;
  416. u32 sync_reg;
  417. int ret;
  418. /* Read SYNC register */
  419. sync_reg = intel_readl(shim, SDW_SHIM_SYNC);
  420. /*
  421. * Set SyncGO bit to synchronously trigger a bank switch for
  422. * all the masters. A write to SYNCGO bit clears CMDSYNC bit for all
  423. * the Masters.
  424. */
  425. sync_reg |= SDW_SHIM_SYNC_SYNCGO;
  426. ret = intel_clear_bit(shim, SDW_SHIM_SYNC, sync_reg,
  427. SDW_SHIM_SYNC_SYNCGO);
  428. if (ret < 0)
  429. dev_err(sdw->cdns.dev, "SyncGO clear failed: %d\n", ret);
  430. return ret;
  431. }
  432. static int intel_shim_sync_go(struct sdw_intel *sdw)
  433. {
  434. int ret;
  435. mutex_lock(sdw->link_res->shim_lock);
  436. ret = intel_shim_sync_go_unlocked(sdw);
  437. mutex_unlock(sdw->link_res->shim_lock);
  438. return ret;
  439. }
  440. /*
  441. * PDI routines
  442. */
  443. static void intel_pdi_init(struct sdw_intel *sdw,
  444. struct sdw_cdns_stream_config *config)
  445. {
  446. void __iomem *shim = sdw->link_res->shim;
  447. unsigned int link_id = sdw->instance;
  448. int pcm_cap;
  449. /* PCM Stream Capability */
  450. pcm_cap = intel_readw(shim, SDW_SHIM_PCMSCAP(link_id));
  451. config->pcm_bd = FIELD_GET(SDW_SHIM_PCMSCAP_BSS, pcm_cap);
  452. config->pcm_in = FIELD_GET(SDW_SHIM_PCMSCAP_ISS, pcm_cap);
  453. config->pcm_out = FIELD_GET(SDW_SHIM_PCMSCAP_OSS, pcm_cap);
  454. dev_dbg(sdw->cdns.dev, "PCM cap bd:%d in:%d out:%d\n",
  455. config->pcm_bd, config->pcm_in, config->pcm_out);
  456. }
  457. static int
  458. intel_pdi_get_ch_cap(struct sdw_intel *sdw, unsigned int pdi_num)
  459. {
  460. void __iomem *shim = sdw->link_res->shim;
  461. unsigned int link_id = sdw->instance;
  462. int count;
  463. count = intel_readw(shim, SDW_SHIM_PCMSYCHC(link_id, pdi_num));
  464. /*
  465. * WORKAROUND: on all existing Intel controllers, pdi
  466. * number 2 reports channel count as 1 even though it
  467. * supports 8 channels. Performing hardcoding for pdi
  468. * number 2.
  469. */
  470. if (pdi_num == 2)
  471. count = 7;
  472. /* zero based values for channel count in register */
  473. count++;
  474. return count;
  475. }
  476. static int intel_pdi_get_ch_update(struct sdw_intel *sdw,
  477. struct sdw_cdns_pdi *pdi,
  478. unsigned int num_pdi,
  479. unsigned int *num_ch)
  480. {
  481. int i, ch_count = 0;
  482. for (i = 0; i < num_pdi; i++) {
  483. pdi->ch_count = intel_pdi_get_ch_cap(sdw, pdi->num);
  484. ch_count += pdi->ch_count;
  485. pdi++;
  486. }
  487. *num_ch = ch_count;
  488. return 0;
  489. }
  490. static int intel_pdi_stream_ch_update(struct sdw_intel *sdw,
  491. struct sdw_cdns_streams *stream)
  492. {
  493. intel_pdi_get_ch_update(sdw, stream->bd, stream->num_bd,
  494. &stream->num_ch_bd);
  495. intel_pdi_get_ch_update(sdw, stream->in, stream->num_in,
  496. &stream->num_ch_in);
  497. intel_pdi_get_ch_update(sdw, stream->out, stream->num_out,
  498. &stream->num_ch_out);
  499. return 0;
  500. }
  501. static int intel_pdi_ch_update(struct sdw_intel *sdw)
  502. {
  503. intel_pdi_stream_ch_update(sdw, &sdw->cdns.pcm);
  504. return 0;
  505. }
  506. static void
  507. intel_pdi_shim_configure(struct sdw_intel *sdw, struct sdw_cdns_pdi *pdi)
  508. {
  509. void __iomem *shim = sdw->link_res->shim;
  510. unsigned int link_id = sdw->instance;
  511. int pdi_conf = 0;
  512. /* the Bulk and PCM streams are not contiguous */
  513. pdi->intel_alh_id = (link_id * 16) + pdi->num + 3;
  514. if (pdi->num >= 2)
  515. pdi->intel_alh_id += 2;
  516. /*
  517. * Program stream parameters to stream SHIM register
  518. * This is applicable for PCM stream only.
  519. */
  520. if (pdi->type != SDW_STREAM_PCM)
  521. return;
  522. if (pdi->dir == SDW_DATA_DIR_RX)
  523. pdi_conf |= SDW_SHIM_PCMSYCM_DIR;
  524. else
  525. pdi_conf &= ~(SDW_SHIM_PCMSYCM_DIR);
  526. u32p_replace_bits(&pdi_conf, pdi->intel_alh_id, SDW_SHIM_PCMSYCM_STREAM);
  527. u32p_replace_bits(&pdi_conf, pdi->l_ch_num, SDW_SHIM_PCMSYCM_LCHN);
  528. u32p_replace_bits(&pdi_conf, pdi->h_ch_num, SDW_SHIM_PCMSYCM_HCHN);
  529. intel_writew(shim, SDW_SHIM_PCMSYCHM(link_id, pdi->num), pdi_conf);
  530. }
  531. static void
  532. intel_pdi_alh_configure(struct sdw_intel *sdw, struct sdw_cdns_pdi *pdi)
  533. {
  534. void __iomem *alh = sdw->link_res->alh;
  535. unsigned int link_id = sdw->instance;
  536. unsigned int conf;
  537. /* the Bulk and PCM streams are not contiguous */
  538. pdi->intel_alh_id = (link_id * 16) + pdi->num + 3;
  539. if (pdi->num >= 2)
  540. pdi->intel_alh_id += 2;
  541. /* Program Stream config ALH register */
  542. conf = intel_readl(alh, SDW_ALH_STRMZCFG(pdi->intel_alh_id));
  543. u32p_replace_bits(&conf, SDW_ALH_STRMZCFG_DMAT_VAL, SDW_ALH_STRMZCFG_DMAT);
  544. u32p_replace_bits(&conf, pdi->ch_count - 1, SDW_ALH_STRMZCFG_CHN);
  545. intel_writel(alh, SDW_ALH_STRMZCFG(pdi->intel_alh_id), conf);
  546. }
  547. static int intel_params_stream(struct sdw_intel *sdw,
  548. int stream,
  549. struct snd_soc_dai *dai,
  550. struct snd_pcm_hw_params *hw_params,
  551. int link_id, int alh_stream_id)
  552. {
  553. struct sdw_intel_link_res *res = sdw->link_res;
  554. struct sdw_intel_stream_params_data params_data;
  555. params_data.stream = stream; /* direction */
  556. params_data.dai = dai;
  557. params_data.hw_params = hw_params;
  558. params_data.link_id = link_id;
  559. params_data.alh_stream_id = alh_stream_id;
  560. if (res->ops && res->ops->params_stream && res->dev)
  561. return res->ops->params_stream(res->dev,
  562. &params_data);
  563. return -EIO;
  564. }
  565. static int intel_free_stream(struct sdw_intel *sdw,
  566. int stream,
  567. struct snd_soc_dai *dai,
  568. int link_id)
  569. {
  570. struct sdw_intel_link_res *res = sdw->link_res;
  571. struct sdw_intel_stream_free_data free_data;
  572. free_data.stream = stream; /* direction */
  573. free_data.dai = dai;
  574. free_data.link_id = link_id;
  575. if (res->ops && res->ops->free_stream && res->dev)
  576. return res->ops->free_stream(res->dev,
  577. &free_data);
  578. return 0;
  579. }
  580. /*
  581. * bank switch routines
  582. */
  583. static int intel_pre_bank_switch(struct sdw_bus *bus)
  584. {
  585. struct sdw_cdns *cdns = bus_to_cdns(bus);
  586. struct sdw_intel *sdw = cdns_to_intel(cdns);
  587. /* Write to register only for multi-link */
  588. if (!bus->multi_link)
  589. return 0;
  590. intel_shim_sync_arm(sdw);
  591. return 0;
  592. }
  593. static int intel_post_bank_switch(struct sdw_bus *bus)
  594. {
  595. struct sdw_cdns *cdns = bus_to_cdns(bus);
  596. struct sdw_intel *sdw = cdns_to_intel(cdns);
  597. void __iomem *shim = sdw->link_res->shim;
  598. int sync_reg, ret;
  599. /* Write to register only for multi-link */
  600. if (!bus->multi_link)
  601. return 0;
  602. mutex_lock(sdw->link_res->shim_lock);
  603. /* Read SYNC register */
  604. sync_reg = intel_readl(shim, SDW_SHIM_SYNC);
  605. /*
  606. * post_bank_switch() ops is called from the bus in loop for
  607. * all the Masters in the steam with the expectation that
  608. * we trigger the bankswitch for the only first Master in the list
  609. * and do nothing for the other Masters
  610. *
  611. * So, set the SYNCGO bit only if CMDSYNC bit is set for any Master.
  612. */
  613. if (!(sync_reg & SDW_SHIM_SYNC_CMDSYNC_MASK)) {
  614. ret = 0;
  615. goto unlock;
  616. }
  617. ret = intel_shim_sync_go_unlocked(sdw);
  618. unlock:
  619. mutex_unlock(sdw->link_res->shim_lock);
  620. if (ret < 0)
  621. dev_err(sdw->cdns.dev, "Post bank switch failed: %d\n", ret);
  622. return ret;
  623. }
  624. /*
  625. * DAI routines
  626. */
  627. static int intel_startup(struct snd_pcm_substream *substream,
  628. struct snd_soc_dai *dai)
  629. {
  630. struct sdw_cdns *cdns = snd_soc_dai_get_drvdata(dai);
  631. int ret;
  632. ret = pm_runtime_resume_and_get(cdns->dev);
  633. if (ret < 0 && ret != -EACCES) {
  634. dev_err_ratelimited(cdns->dev,
  635. "pm_runtime_resume_and_get failed in %s, ret %d\n",
  636. __func__, ret);
  637. return ret;
  638. }
  639. return 0;
  640. }
  641. static int intel_hw_params(struct snd_pcm_substream *substream,
  642. struct snd_pcm_hw_params *params,
  643. struct snd_soc_dai *dai)
  644. {
  645. struct sdw_cdns *cdns = snd_soc_dai_get_drvdata(dai);
  646. struct sdw_intel *sdw = cdns_to_intel(cdns);
  647. struct sdw_cdns_dai_runtime *dai_runtime;
  648. struct sdw_cdns_pdi *pdi;
  649. struct sdw_stream_config sconfig;
  650. struct sdw_port_config *pconfig;
  651. int ch, dir;
  652. int ret;
  653. dai_runtime = snd_soc_dai_get_dma_data(dai, substream);
  654. if (!dai_runtime)
  655. return -EIO;
  656. ch = params_channels(params);
  657. if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
  658. dir = SDW_DATA_DIR_RX;
  659. else
  660. dir = SDW_DATA_DIR_TX;
  661. pdi = sdw_cdns_alloc_pdi(cdns, &cdns->pcm, ch, dir, dai->id);
  662. if (!pdi) {
  663. ret = -EINVAL;
  664. goto error;
  665. }
  666. /* do run-time configurations for SHIM, ALH and PDI/PORT */
  667. intel_pdi_shim_configure(sdw, pdi);
  668. intel_pdi_alh_configure(sdw, pdi);
  669. sdw_cdns_config_stream(cdns, ch, dir, pdi);
  670. /* store pdi and hw_params, may be needed in prepare step */
  671. dai_runtime->paused = false;
  672. dai_runtime->suspended = false;
  673. dai_runtime->pdi = pdi;
  674. /* Inform DSP about PDI stream number */
  675. ret = intel_params_stream(sdw, substream->stream, dai, params,
  676. sdw->instance,
  677. pdi->intel_alh_id);
  678. if (ret)
  679. goto error;
  680. sconfig.direction = dir;
  681. sconfig.ch_count = ch;
  682. sconfig.frame_rate = params_rate(params);
  683. sconfig.type = dai_runtime->stream_type;
  684. sconfig.bps = snd_pcm_format_width(params_format(params));
  685. /* Port configuration */
  686. pconfig = kzalloc(sizeof(*pconfig), GFP_KERNEL);
  687. if (!pconfig) {
  688. ret = -ENOMEM;
  689. goto error;
  690. }
  691. pconfig->num = pdi->num;
  692. pconfig->ch_mask = (1 << ch) - 1;
  693. ret = sdw_stream_add_master(&cdns->bus, &sconfig,
  694. pconfig, 1, dai_runtime->stream);
  695. if (ret)
  696. dev_err(cdns->dev, "add master to stream failed:%d\n", ret);
  697. kfree(pconfig);
  698. error:
  699. return ret;
  700. }
  701. static int intel_prepare(struct snd_pcm_substream *substream,
  702. struct snd_soc_dai *dai)
  703. {
  704. struct sdw_cdns *cdns = snd_soc_dai_get_drvdata(dai);
  705. struct sdw_intel *sdw = cdns_to_intel(cdns);
  706. struct sdw_cdns_dai_runtime *dai_runtime;
  707. int ch, dir;
  708. int ret = 0;
  709. dai_runtime = snd_soc_dai_get_dma_data(dai, substream);
  710. if (!dai_runtime) {
  711. dev_err(dai->dev, "failed to get dai runtime in %s\n",
  712. __func__);
  713. return -EIO;
  714. }
  715. if (dai_runtime->suspended) {
  716. struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
  717. struct snd_pcm_hw_params *hw_params;
  718. hw_params = &rtd->dpcm[substream->stream].hw_params;
  719. dai_runtime->suspended = false;
  720. /*
  721. * .prepare() is called after system resume, where we
  722. * need to reinitialize the SHIM/ALH/Cadence IP.
  723. * .prepare() is also called to deal with underflows,
  724. * but in those cases we cannot touch ALH/SHIM
  725. * registers
  726. */
  727. /* configure stream */
  728. ch = params_channels(hw_params);
  729. if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
  730. dir = SDW_DATA_DIR_RX;
  731. else
  732. dir = SDW_DATA_DIR_TX;
  733. intel_pdi_shim_configure(sdw, dai_runtime->pdi);
  734. intel_pdi_alh_configure(sdw, dai_runtime->pdi);
  735. sdw_cdns_config_stream(cdns, ch, dir, dai_runtime->pdi);
  736. /* Inform DSP about PDI stream number */
  737. ret = intel_params_stream(sdw, substream->stream, dai,
  738. hw_params,
  739. sdw->instance,
  740. dai_runtime->pdi->intel_alh_id);
  741. }
  742. return ret;
  743. }
  744. static int
  745. intel_hw_free(struct snd_pcm_substream *substream, struct snd_soc_dai *dai)
  746. {
  747. struct sdw_cdns *cdns = snd_soc_dai_get_drvdata(dai);
  748. struct sdw_intel *sdw = cdns_to_intel(cdns);
  749. struct sdw_cdns_dai_runtime *dai_runtime;
  750. int ret;
  751. dai_runtime = snd_soc_dai_get_dma_data(dai, substream);
  752. if (!dai_runtime)
  753. return -EIO;
  754. /*
  755. * The sdw stream state will transition to RELEASED when stream->
  756. * master_list is empty. So the stream state will transition to
  757. * DEPREPARED for the first cpu-dai and to RELEASED for the last
  758. * cpu-dai.
  759. */
  760. ret = sdw_stream_remove_master(&cdns->bus, dai_runtime->stream);
  761. if (ret < 0) {
  762. dev_err(dai->dev, "remove master from stream %s failed: %d\n",
  763. dai_runtime->stream->name, ret);
  764. return ret;
  765. }
  766. ret = intel_free_stream(sdw, substream->stream, dai, sdw->instance);
  767. if (ret < 0) {
  768. dev_err(dai->dev, "intel_free_stream: failed %d\n", ret);
  769. return ret;
  770. }
  771. dai_runtime->pdi = NULL;
  772. return 0;
  773. }
  774. static void intel_shutdown(struct snd_pcm_substream *substream,
  775. struct snd_soc_dai *dai)
  776. {
  777. struct sdw_cdns *cdns = snd_soc_dai_get_drvdata(dai);
  778. pm_runtime_mark_last_busy(cdns->dev);
  779. pm_runtime_put_autosuspend(cdns->dev);
  780. }
  781. static int intel_pcm_set_sdw_stream(struct snd_soc_dai *dai,
  782. void *stream, int direction)
  783. {
  784. return cdns_set_sdw_stream(dai, stream, direction);
  785. }
  786. static void *intel_get_sdw_stream(struct snd_soc_dai *dai,
  787. int direction)
  788. {
  789. struct sdw_cdns_dai_runtime *dai_runtime;
  790. if (direction == SNDRV_PCM_STREAM_PLAYBACK)
  791. dai_runtime = dai->playback_dma_data;
  792. else
  793. dai_runtime = dai->capture_dma_data;
  794. if (!dai_runtime)
  795. return ERR_PTR(-EINVAL);
  796. return dai_runtime->stream;
  797. }
  798. static int intel_trigger(struct snd_pcm_substream *substream, int cmd, struct snd_soc_dai *dai)
  799. {
  800. struct sdw_cdns *cdns = snd_soc_dai_get_drvdata(dai);
  801. struct sdw_intel *sdw = cdns_to_intel(cdns);
  802. struct sdw_intel_link_res *res = sdw->link_res;
  803. struct sdw_cdns_dai_runtime *dai_runtime;
  804. int ret = 0;
  805. /*
  806. * The .trigger callback is used to send required IPC to audio
  807. * firmware. The .free_stream callback will still be called
  808. * by intel_free_stream() in the TRIGGER_SUSPEND case.
  809. */
  810. if (res->ops && res->ops->trigger)
  811. res->ops->trigger(dai, cmd, substream->stream);
  812. dai_runtime = snd_soc_dai_get_dma_data(dai, substream);
  813. if (!dai_runtime) {
  814. dev_err(dai->dev, "failed to get dai runtime in %s\n",
  815. __func__);
  816. return -EIO;
  817. }
  818. switch (cmd) {
  819. case SNDRV_PCM_TRIGGER_SUSPEND:
  820. /*
  821. * The .prepare callback is used to deal with xruns and resume operations.
  822. * In the case of xruns, the DMAs and SHIM registers cannot be touched,
  823. * but for resume operations the DMAs and SHIM registers need to be initialized.
  824. * the .trigger callback is used to track the suspend case only.
  825. */
  826. dai_runtime->suspended = true;
  827. ret = intel_free_stream(sdw, substream->stream, dai, sdw->instance);
  828. break;
  829. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  830. dai_runtime->paused = true;
  831. break;
  832. case SNDRV_PCM_TRIGGER_STOP:
  833. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  834. dai_runtime->paused = false;
  835. break;
  836. default:
  837. break;
  838. }
  839. return ret;
  840. }
  841. static int intel_component_probe(struct snd_soc_component *component)
  842. {
  843. int ret;
  844. /*
  845. * make sure the device is pm_runtime_active before initiating
  846. * bus transactions during the card registration.
  847. * We use pm_runtime_resume() here, without taking a reference
  848. * and releasing it immediately.
  849. */
  850. ret = pm_runtime_resume(component->dev);
  851. if (ret < 0 && ret != -EACCES)
  852. return ret;
  853. return 0;
  854. }
  855. static int intel_component_dais_suspend(struct snd_soc_component *component)
  856. {
  857. struct snd_soc_dai *dai;
  858. /*
  859. * In the corner case where a SUSPEND happens during a PAUSE, the ALSA core
  860. * does not throw the TRIGGER_SUSPEND. This leaves the DAIs in an unbalanced state.
  861. * Since the component suspend is called last, we can trap this corner case
  862. * and force the DAIs to release their resources.
  863. */
  864. for_each_component_dais(component, dai) {
  865. struct sdw_cdns *cdns = snd_soc_dai_get_drvdata(dai);
  866. struct sdw_intel *sdw = cdns_to_intel(cdns);
  867. struct sdw_cdns_dai_runtime *dai_runtime;
  868. int stream;
  869. int ret;
  870. dai_runtime = dai->playback_dma_data;
  871. stream = SNDRV_PCM_STREAM_PLAYBACK;
  872. if (!dai_runtime) {
  873. dai_runtime = dai->capture_dma_data;
  874. stream = SNDRV_PCM_STREAM_CAPTURE;
  875. }
  876. if (!dai_runtime)
  877. continue;
  878. if (dai_runtime->suspended)
  879. continue;
  880. if (dai_runtime->paused) {
  881. dai_runtime->suspended = true;
  882. ret = intel_free_stream(sdw, stream, dai, sdw->instance);
  883. if (ret < 0)
  884. return ret;
  885. }
  886. }
  887. return 0;
  888. }
  889. static const struct snd_soc_dai_ops intel_pcm_dai_ops = {
  890. .startup = intel_startup,
  891. .hw_params = intel_hw_params,
  892. .prepare = intel_prepare,
  893. .hw_free = intel_hw_free,
  894. .trigger = intel_trigger,
  895. .shutdown = intel_shutdown,
  896. .set_stream = intel_pcm_set_sdw_stream,
  897. .get_stream = intel_get_sdw_stream,
  898. };
  899. static const struct snd_soc_component_driver dai_component = {
  900. .name = "soundwire",
  901. .probe = intel_component_probe,
  902. .suspend = intel_component_dais_suspend,
  903. .legacy_dai_naming = 1,
  904. };
  905. static int intel_create_dai(struct sdw_cdns *cdns,
  906. struct snd_soc_dai_driver *dais,
  907. enum intel_pdi_type type,
  908. u32 num, u32 off, u32 max_ch)
  909. {
  910. int i;
  911. if (num == 0)
  912. return 0;
  913. /* TODO: Read supported rates/formats from hardware */
  914. for (i = off; i < (off + num); i++) {
  915. dais[i].name = devm_kasprintf(cdns->dev, GFP_KERNEL,
  916. "SDW%d Pin%d",
  917. cdns->instance, i);
  918. if (!dais[i].name)
  919. return -ENOMEM;
  920. if (type == INTEL_PDI_BD || type == INTEL_PDI_OUT) {
  921. dais[i].playback.channels_min = 1;
  922. dais[i].playback.channels_max = max_ch;
  923. dais[i].playback.rates = SNDRV_PCM_RATE_48000;
  924. dais[i].playback.formats = SNDRV_PCM_FMTBIT_S16_LE;
  925. }
  926. if (type == INTEL_PDI_BD || type == INTEL_PDI_IN) {
  927. dais[i].capture.channels_min = 1;
  928. dais[i].capture.channels_max = max_ch;
  929. dais[i].capture.rates = SNDRV_PCM_RATE_48000;
  930. dais[i].capture.formats = SNDRV_PCM_FMTBIT_S16_LE;
  931. }
  932. dais[i].ops = &intel_pcm_dai_ops;
  933. }
  934. return 0;
  935. }
  936. static int intel_register_dai(struct sdw_intel *sdw)
  937. {
  938. struct sdw_cdns_stream_config config;
  939. struct sdw_cdns *cdns = &sdw->cdns;
  940. struct sdw_cdns_streams *stream;
  941. struct snd_soc_dai_driver *dais;
  942. int num_dai, ret, off = 0;
  943. /* Read the PDI config and initialize cadence PDI */
  944. intel_pdi_init(sdw, &config);
  945. ret = sdw_cdns_pdi_init(cdns, config);
  946. if (ret)
  947. return ret;
  948. intel_pdi_ch_update(sdw);
  949. /* DAIs are created based on total number of PDIs supported */
  950. num_dai = cdns->pcm.num_pdi;
  951. dais = devm_kcalloc(cdns->dev, num_dai, sizeof(*dais), GFP_KERNEL);
  952. if (!dais)
  953. return -ENOMEM;
  954. /* Create PCM DAIs */
  955. stream = &cdns->pcm;
  956. ret = intel_create_dai(cdns, dais, INTEL_PDI_IN, cdns->pcm.num_in,
  957. off, stream->num_ch_in);
  958. if (ret)
  959. return ret;
  960. off += cdns->pcm.num_in;
  961. ret = intel_create_dai(cdns, dais, INTEL_PDI_OUT, cdns->pcm.num_out,
  962. off, stream->num_ch_out);
  963. if (ret)
  964. return ret;
  965. off += cdns->pcm.num_out;
  966. ret = intel_create_dai(cdns, dais, INTEL_PDI_BD, cdns->pcm.num_bd,
  967. off, stream->num_ch_bd);
  968. if (ret)
  969. return ret;
  970. return devm_snd_soc_register_component(cdns->dev, &dai_component,
  971. dais, num_dai);
  972. }
  973. static int intel_start_bus(struct sdw_intel *sdw)
  974. {
  975. struct device *dev = sdw->cdns.dev;
  976. struct sdw_cdns *cdns = &sdw->cdns;
  977. struct sdw_bus *bus = &cdns->bus;
  978. int ret;
  979. ret = sdw_cdns_enable_interrupt(cdns, true);
  980. if (ret < 0) {
  981. dev_err(dev, "%s: cannot enable interrupts: %d\n", __func__, ret);
  982. return ret;
  983. }
  984. /*
  985. * follow recommended programming flows to avoid timeouts when
  986. * gsync is enabled
  987. */
  988. if (bus->multi_link)
  989. intel_shim_sync_arm(sdw);
  990. ret = sdw_cdns_init(cdns);
  991. if (ret < 0) {
  992. dev_err(dev, "%s: unable to initialize Cadence IP: %d\n", __func__, ret);
  993. goto err_interrupt;
  994. }
  995. ret = sdw_cdns_exit_reset(cdns);
  996. if (ret < 0) {
  997. dev_err(dev, "%s: unable to exit bus reset sequence: %d\n", __func__, ret);
  998. goto err_interrupt;
  999. }
  1000. if (bus->multi_link) {
  1001. ret = intel_shim_sync_go(sdw);
  1002. if (ret < 0) {
  1003. dev_err(dev, "%s: sync go failed: %d\n", __func__, ret);
  1004. goto err_interrupt;
  1005. }
  1006. }
  1007. sdw_cdns_check_self_clearing_bits(cdns, __func__,
  1008. true, INTEL_MASTER_RESET_ITERATIONS);
  1009. return 0;
  1010. err_interrupt:
  1011. sdw_cdns_enable_interrupt(cdns, false);
  1012. return ret;
  1013. }
  1014. static int intel_start_bus_after_reset(struct sdw_intel *sdw)
  1015. {
  1016. struct device *dev = sdw->cdns.dev;
  1017. struct sdw_cdns *cdns = &sdw->cdns;
  1018. struct sdw_bus *bus = &cdns->bus;
  1019. bool clock_stop0;
  1020. int status;
  1021. int ret;
  1022. /*
  1023. * An exception condition occurs for the CLK_STOP_BUS_RESET
  1024. * case if one or more masters remain active. In this condition,
  1025. * all the masters are powered on for they are in the same power
  1026. * domain. Master can preserve its context for clock stop0, so
  1027. * there is no need to clear slave status and reset bus.
  1028. */
  1029. clock_stop0 = sdw_cdns_is_clock_stop(&sdw->cdns);
  1030. if (!clock_stop0) {
  1031. /*
  1032. * make sure all Slaves are tagged as UNATTACHED and
  1033. * provide reason for reinitialization
  1034. */
  1035. status = SDW_UNATTACH_REQUEST_MASTER_RESET;
  1036. sdw_clear_slave_status(bus, status);
  1037. ret = sdw_cdns_enable_interrupt(cdns, true);
  1038. if (ret < 0) {
  1039. dev_err(dev, "cannot enable interrupts during resume\n");
  1040. return ret;
  1041. }
  1042. /*
  1043. * follow recommended programming flows to avoid
  1044. * timeouts when gsync is enabled
  1045. */
  1046. if (bus->multi_link)
  1047. intel_shim_sync_arm(sdw);
  1048. /*
  1049. * Re-initialize the IP since it was powered-off
  1050. */
  1051. sdw_cdns_init(&sdw->cdns);
  1052. } else {
  1053. ret = sdw_cdns_enable_interrupt(cdns, true);
  1054. if (ret < 0) {
  1055. dev_err(dev, "cannot enable interrupts during resume\n");
  1056. return ret;
  1057. }
  1058. }
  1059. ret = sdw_cdns_clock_restart(cdns, !clock_stop0);
  1060. if (ret < 0) {
  1061. dev_err(dev, "unable to restart clock during resume\n");
  1062. goto err_interrupt;
  1063. }
  1064. if (!clock_stop0) {
  1065. ret = sdw_cdns_exit_reset(cdns);
  1066. if (ret < 0) {
  1067. dev_err(dev, "unable to exit bus reset sequence during resume\n");
  1068. goto err_interrupt;
  1069. }
  1070. if (bus->multi_link) {
  1071. ret = intel_shim_sync_go(sdw);
  1072. if (ret < 0) {
  1073. dev_err(sdw->cdns.dev, "sync go failed during resume\n");
  1074. goto err_interrupt;
  1075. }
  1076. }
  1077. }
  1078. sdw_cdns_check_self_clearing_bits(cdns, __func__, true, INTEL_MASTER_RESET_ITERATIONS);
  1079. return 0;
  1080. err_interrupt:
  1081. sdw_cdns_enable_interrupt(cdns, false);
  1082. return ret;
  1083. }
  1084. static void intel_check_clock_stop(struct sdw_intel *sdw)
  1085. {
  1086. struct device *dev = sdw->cdns.dev;
  1087. bool clock_stop0;
  1088. clock_stop0 = sdw_cdns_is_clock_stop(&sdw->cdns);
  1089. if (!clock_stop0)
  1090. dev_err(dev, "%s: invalid configuration, clock was not stopped\n", __func__);
  1091. }
  1092. static int intel_start_bus_after_clock_stop(struct sdw_intel *sdw)
  1093. {
  1094. struct device *dev = sdw->cdns.dev;
  1095. struct sdw_cdns *cdns = &sdw->cdns;
  1096. int ret;
  1097. ret = sdw_cdns_enable_interrupt(cdns, true);
  1098. if (ret < 0) {
  1099. dev_err(dev, "%s: cannot enable interrupts: %d\n", __func__, ret);
  1100. return ret;
  1101. }
  1102. ret = sdw_cdns_clock_restart(cdns, false);
  1103. if (ret < 0) {
  1104. dev_err(dev, "%s: unable to restart clock: %d\n", __func__, ret);
  1105. sdw_cdns_enable_interrupt(cdns, false);
  1106. return ret;
  1107. }
  1108. sdw_cdns_check_self_clearing_bits(cdns, "intel_resume_runtime no_quirks",
  1109. true, INTEL_MASTER_RESET_ITERATIONS);
  1110. return 0;
  1111. }
  1112. static int intel_stop_bus(struct sdw_intel *sdw, bool clock_stop)
  1113. {
  1114. struct device *dev = sdw->cdns.dev;
  1115. struct sdw_cdns *cdns = &sdw->cdns;
  1116. bool wake_enable = false;
  1117. int ret;
  1118. if (clock_stop) {
  1119. ret = sdw_cdns_clock_stop(cdns, true);
  1120. if (ret < 0)
  1121. dev_err(dev, "%s: cannot stop clock: %d\n", __func__, ret);
  1122. else
  1123. wake_enable = true;
  1124. }
  1125. ret = sdw_cdns_enable_interrupt(cdns, false);
  1126. if (ret < 0) {
  1127. dev_err(dev, "%s: cannot disable interrupts: %d\n", __func__, ret);
  1128. return ret;
  1129. }
  1130. ret = intel_link_power_down(sdw);
  1131. if (ret) {
  1132. dev_err(dev, "%s: Link power down failed: %d\n", __func__, ret);
  1133. return ret;
  1134. }
  1135. intel_shim_wake(sdw, wake_enable);
  1136. return 0;
  1137. }
  1138. static int sdw_master_read_intel_prop(struct sdw_bus *bus)
  1139. {
  1140. struct sdw_master_prop *prop = &bus->prop;
  1141. struct fwnode_handle *link;
  1142. char name[32];
  1143. u32 quirk_mask;
  1144. /* Find master handle */
  1145. snprintf(name, sizeof(name),
  1146. "mipi-sdw-link-%d-subproperties", bus->link_id);
  1147. link = device_get_named_child_node(bus->dev, name);
  1148. if (!link) {
  1149. dev_err(bus->dev, "Master node %s not found\n", name);
  1150. return -EIO;
  1151. }
  1152. fwnode_property_read_u32(link,
  1153. "intel-sdw-ip-clock",
  1154. &prop->mclk_freq);
  1155. /* the values reported by BIOS are the 2x clock, not the bus clock */
  1156. prop->mclk_freq /= 2;
  1157. fwnode_property_read_u32(link,
  1158. "intel-quirk-mask",
  1159. &quirk_mask);
  1160. if (quirk_mask & SDW_INTEL_QUIRK_MASK_BUS_DISABLE)
  1161. prop->hw_disabled = true;
  1162. prop->quirks = SDW_MASTER_QUIRKS_CLEAR_INITIAL_CLASH |
  1163. SDW_MASTER_QUIRKS_CLEAR_INITIAL_PARITY;
  1164. return 0;
  1165. }
  1166. static int intel_prop_read(struct sdw_bus *bus)
  1167. {
  1168. /* Initialize with default handler to read all DisCo properties */
  1169. sdw_master_read_prop(bus);
  1170. /* read Intel-specific properties */
  1171. sdw_master_read_intel_prop(bus);
  1172. return 0;
  1173. }
  1174. static struct sdw_master_ops sdw_intel_ops = {
  1175. .read_prop = intel_prop_read,
  1176. .override_adr = sdw_dmi_override_adr,
  1177. .xfer_msg = cdns_xfer_msg,
  1178. .xfer_msg_defer = cdns_xfer_msg_defer,
  1179. .reset_page_addr = cdns_reset_page_addr,
  1180. .set_bus_conf = cdns_bus_conf,
  1181. .pre_bank_switch = intel_pre_bank_switch,
  1182. .post_bank_switch = intel_post_bank_switch,
  1183. .read_ping_status = cdns_read_ping_status,
  1184. };
  1185. /*
  1186. * probe and init (aux_dev_id argument is required by function prototype but not used)
  1187. */
  1188. static int intel_link_probe(struct auxiliary_device *auxdev,
  1189. const struct auxiliary_device_id *aux_dev_id)
  1190. {
  1191. struct device *dev = &auxdev->dev;
  1192. struct sdw_intel_link_dev *ldev = auxiliary_dev_to_sdw_intel_link_dev(auxdev);
  1193. struct sdw_intel *sdw;
  1194. struct sdw_cdns *cdns;
  1195. struct sdw_bus *bus;
  1196. int ret;
  1197. sdw = devm_kzalloc(dev, sizeof(*sdw), GFP_KERNEL);
  1198. if (!sdw)
  1199. return -ENOMEM;
  1200. cdns = &sdw->cdns;
  1201. bus = &cdns->bus;
  1202. sdw->instance = auxdev->id;
  1203. sdw->link_res = &ldev->link_res;
  1204. cdns->dev = dev;
  1205. cdns->registers = sdw->link_res->registers;
  1206. cdns->instance = sdw->instance;
  1207. cdns->msg_count = 0;
  1208. bus->link_id = auxdev->id;
  1209. bus->dev_num_ida_min = INTEL_DEV_NUM_IDA_MIN;
  1210. bus->clk_stop_timeout = 1;
  1211. sdw_cdns_probe(cdns);
  1212. /* Set ops */
  1213. bus->ops = &sdw_intel_ops;
  1214. /* set driver data, accessed by snd_soc_dai_get_drvdata() */
  1215. auxiliary_set_drvdata(auxdev, cdns);
  1216. /* use generic bandwidth allocation algorithm */
  1217. sdw->cdns.bus.compute_params = sdw_compute_params;
  1218. /* avoid resuming from pm_runtime suspend if it's not required */
  1219. dev_pm_set_driver_flags(dev, DPM_FLAG_SMART_SUSPEND);
  1220. ret = sdw_bus_master_add(bus, dev, dev->fwnode);
  1221. if (ret) {
  1222. dev_err(dev, "sdw_bus_master_add fail: %d\n", ret);
  1223. return ret;
  1224. }
  1225. if (bus->prop.hw_disabled)
  1226. dev_info(dev,
  1227. "SoundWire master %d is disabled, will be ignored\n",
  1228. bus->link_id);
  1229. /*
  1230. * Ignore BIOS err_threshold, it's a really bad idea when dealing
  1231. * with multiple hardware synchronized links
  1232. */
  1233. bus->prop.err_threshold = 0;
  1234. return 0;
  1235. }
  1236. int intel_link_startup(struct auxiliary_device *auxdev)
  1237. {
  1238. struct device *dev = &auxdev->dev;
  1239. struct sdw_cdns *cdns = auxiliary_get_drvdata(auxdev);
  1240. struct sdw_intel *sdw = cdns_to_intel(cdns);
  1241. struct sdw_bus *bus = &cdns->bus;
  1242. int link_flags;
  1243. bool multi_link;
  1244. u32 clock_stop_quirks;
  1245. int ret;
  1246. if (bus->prop.hw_disabled) {
  1247. dev_info(dev,
  1248. "SoundWire master %d is disabled, ignoring\n",
  1249. sdw->instance);
  1250. return 0;
  1251. }
  1252. link_flags = md_flags >> (bus->link_id * 8);
  1253. multi_link = !(link_flags & SDW_INTEL_MASTER_DISABLE_MULTI_LINK);
  1254. if (!multi_link) {
  1255. dev_dbg(dev, "Multi-link is disabled\n");
  1256. } else {
  1257. /*
  1258. * hardware-based synchronization is required regardless
  1259. * of the number of segments used by a stream: SSP-based
  1260. * synchronization is gated by gsync when the multi-master
  1261. * mode is set.
  1262. */
  1263. bus->hw_sync_min_links = 1;
  1264. }
  1265. bus->multi_link = multi_link;
  1266. /* Initialize shim, controller */
  1267. ret = intel_link_power_up(sdw);
  1268. if (ret)
  1269. goto err_init;
  1270. /* Register DAIs */
  1271. ret = intel_register_dai(sdw);
  1272. if (ret) {
  1273. dev_err(dev, "DAI registration failed: %d\n", ret);
  1274. goto err_power_up;
  1275. }
  1276. intel_debugfs_init(sdw);
  1277. /* start bus */
  1278. ret = intel_start_bus(sdw);
  1279. if (ret) {
  1280. dev_err(dev, "bus start failed: %d\n", ret);
  1281. goto err_power_up;
  1282. }
  1283. /* Enable runtime PM */
  1284. if (!(link_flags & SDW_INTEL_MASTER_DISABLE_PM_RUNTIME)) {
  1285. pm_runtime_set_autosuspend_delay(dev,
  1286. INTEL_MASTER_SUSPEND_DELAY_MS);
  1287. pm_runtime_use_autosuspend(dev);
  1288. pm_runtime_mark_last_busy(dev);
  1289. pm_runtime_set_active(dev);
  1290. pm_runtime_enable(dev);
  1291. }
  1292. clock_stop_quirks = sdw->link_res->clock_stop_quirks;
  1293. if (clock_stop_quirks & SDW_INTEL_CLK_STOP_NOT_ALLOWED) {
  1294. /*
  1295. * To keep the clock running we need to prevent
  1296. * pm_runtime suspend from happening by increasing the
  1297. * reference count.
  1298. * This quirk is specified by the parent PCI device in
  1299. * case of specific latency requirements. It will have
  1300. * no effect if pm_runtime is disabled by the user via
  1301. * a module parameter for testing purposes.
  1302. */
  1303. pm_runtime_get_noresume(dev);
  1304. }
  1305. /*
  1306. * The runtime PM status of Slave devices is "Unsupported"
  1307. * until they report as ATTACHED. If they don't, e.g. because
  1308. * there are no Slave devices populated or if the power-on is
  1309. * delayed or dependent on a power switch, the Master will
  1310. * remain active and prevent its parent from suspending.
  1311. *
  1312. * Conditionally force the pm_runtime core to re-evaluate the
  1313. * Master status in the absence of any Slave activity. A quirk
  1314. * is provided to e.g. deal with Slaves that may be powered on
  1315. * with a delay. A more complete solution would require the
  1316. * definition of Master properties.
  1317. */
  1318. if (!(link_flags & SDW_INTEL_MASTER_DISABLE_PM_RUNTIME_IDLE))
  1319. pm_runtime_idle(dev);
  1320. sdw->startup_done = true;
  1321. return 0;
  1322. err_power_up:
  1323. intel_link_power_down(sdw);
  1324. err_init:
  1325. return ret;
  1326. }
  1327. static void intel_link_remove(struct auxiliary_device *auxdev)
  1328. {
  1329. struct sdw_cdns *cdns = auxiliary_get_drvdata(auxdev);
  1330. struct sdw_intel *sdw = cdns_to_intel(cdns);
  1331. struct sdw_bus *bus = &cdns->bus;
  1332. /*
  1333. * Since pm_runtime is already disabled, we don't decrease
  1334. * the refcount when the clock_stop_quirk is
  1335. * SDW_INTEL_CLK_STOP_NOT_ALLOWED
  1336. */
  1337. if (!bus->prop.hw_disabled) {
  1338. intel_debugfs_exit(sdw);
  1339. sdw_cdns_enable_interrupt(cdns, false);
  1340. }
  1341. sdw_bus_master_delete(bus);
  1342. }
  1343. int intel_link_process_wakeen_event(struct auxiliary_device *auxdev)
  1344. {
  1345. struct device *dev = &auxdev->dev;
  1346. struct sdw_intel *sdw;
  1347. struct sdw_bus *bus;
  1348. sdw = auxiliary_get_drvdata(auxdev);
  1349. bus = &sdw->cdns.bus;
  1350. if (bus->prop.hw_disabled || !sdw->startup_done) {
  1351. dev_dbg(dev, "SoundWire master %d is disabled or not-started, ignoring\n",
  1352. bus->link_id);
  1353. return 0;
  1354. }
  1355. if (!intel_shim_check_wake(sdw))
  1356. return 0;
  1357. /* disable WAKEEN interrupt ASAP to prevent interrupt flood */
  1358. intel_shim_wake(sdw, false);
  1359. /*
  1360. * resume the Master, which will generate a bus reset and result in
  1361. * Slaves re-attaching and be re-enumerated. The SoundWire physical
  1362. * device which generated the wake will trigger an interrupt, which
  1363. * will in turn cause the corresponding Linux Slave device to be
  1364. * resumed and the Slave codec driver to check the status.
  1365. */
  1366. pm_request_resume(dev);
  1367. return 0;
  1368. }
  1369. /*
  1370. * PM calls
  1371. */
  1372. static int intel_resume_child_device(struct device *dev, void *data)
  1373. {
  1374. int ret;
  1375. struct sdw_slave *slave = dev_to_sdw_dev(dev);
  1376. if (!slave->probed) {
  1377. dev_dbg(dev, "skipping device, no probed driver\n");
  1378. return 0;
  1379. }
  1380. if (!slave->dev_num_sticky) {
  1381. dev_dbg(dev, "skipping device, never detected on bus\n");
  1382. return 0;
  1383. }
  1384. ret = pm_request_resume(dev);
  1385. if (ret < 0)
  1386. dev_err(dev, "%s: pm_request_resume failed: %d\n", __func__, ret);
  1387. return ret;
  1388. }
  1389. static int __maybe_unused intel_pm_prepare(struct device *dev)
  1390. {
  1391. struct sdw_cdns *cdns = dev_get_drvdata(dev);
  1392. struct sdw_intel *sdw = cdns_to_intel(cdns);
  1393. struct sdw_bus *bus = &cdns->bus;
  1394. u32 clock_stop_quirks;
  1395. int ret;
  1396. if (bus->prop.hw_disabled || !sdw->startup_done) {
  1397. dev_dbg(dev, "SoundWire master %d is disabled or not-started, ignoring\n",
  1398. bus->link_id);
  1399. return 0;
  1400. }
  1401. clock_stop_quirks = sdw->link_res->clock_stop_quirks;
  1402. if (pm_runtime_suspended(dev) &&
  1403. pm_runtime_suspended(dev->parent) &&
  1404. ((clock_stop_quirks & SDW_INTEL_CLK_STOP_BUS_RESET) ||
  1405. !clock_stop_quirks)) {
  1406. /*
  1407. * if we've enabled clock stop, and the parent is suspended, the SHIM registers
  1408. * are not accessible and the shim wake cannot be disabled.
  1409. * The only solution is to resume the entire bus to full power
  1410. */
  1411. /*
  1412. * If any operation in this block fails, we keep going since we don't want
  1413. * to prevent system suspend from happening and errors should be recoverable
  1414. * on resume.
  1415. */
  1416. /*
  1417. * first resume the device for this link. This will also by construction
  1418. * resume the PCI parent device.
  1419. */
  1420. ret = pm_request_resume(dev);
  1421. if (ret < 0) {
  1422. dev_err(dev, "%s: pm_request_resume failed: %d\n", __func__, ret);
  1423. return 0;
  1424. }
  1425. /*
  1426. * Continue resuming the entire bus (parent + child devices) to exit
  1427. * the clock stop mode. If there are no devices connected on this link
  1428. * this is a no-op.
  1429. * The resume to full power could have been implemented with a .prepare
  1430. * step in SoundWire codec drivers. This would however require a lot
  1431. * of code to handle an Intel-specific corner case. It is simpler in
  1432. * practice to add a loop at the link level.
  1433. */
  1434. ret = device_for_each_child(bus->dev, NULL, intel_resume_child_device);
  1435. if (ret < 0)
  1436. dev_err(dev, "%s: intel_resume_child_device failed: %d\n", __func__, ret);
  1437. }
  1438. return 0;
  1439. }
  1440. static int __maybe_unused intel_suspend(struct device *dev)
  1441. {
  1442. struct sdw_cdns *cdns = dev_get_drvdata(dev);
  1443. struct sdw_intel *sdw = cdns_to_intel(cdns);
  1444. struct sdw_bus *bus = &cdns->bus;
  1445. u32 clock_stop_quirks;
  1446. int ret;
  1447. if (bus->prop.hw_disabled || !sdw->startup_done) {
  1448. dev_dbg(dev, "SoundWire master %d is disabled or not-started, ignoring\n",
  1449. bus->link_id);
  1450. return 0;
  1451. }
  1452. if (pm_runtime_suspended(dev)) {
  1453. dev_dbg(dev, "pm_runtime status: suspended\n");
  1454. clock_stop_quirks = sdw->link_res->clock_stop_quirks;
  1455. if ((clock_stop_quirks & SDW_INTEL_CLK_STOP_BUS_RESET) ||
  1456. !clock_stop_quirks) {
  1457. if (pm_runtime_suspended(dev->parent)) {
  1458. /*
  1459. * paranoia check: this should not happen with the .prepare
  1460. * resume to full power
  1461. */
  1462. dev_err(dev, "%s: invalid config: parent is suspended\n", __func__);
  1463. } else {
  1464. intel_shim_wake(sdw, false);
  1465. }
  1466. }
  1467. return 0;
  1468. }
  1469. ret = intel_stop_bus(sdw, false);
  1470. if (ret < 0) {
  1471. dev_err(dev, "%s: cannot stop bus: %d\n", __func__, ret);
  1472. return ret;
  1473. }
  1474. return 0;
  1475. }
  1476. static int __maybe_unused intel_suspend_runtime(struct device *dev)
  1477. {
  1478. struct sdw_cdns *cdns = dev_get_drvdata(dev);
  1479. struct sdw_intel *sdw = cdns_to_intel(cdns);
  1480. struct sdw_bus *bus = &cdns->bus;
  1481. u32 clock_stop_quirks;
  1482. int ret;
  1483. if (bus->prop.hw_disabled || !sdw->startup_done) {
  1484. dev_dbg(dev, "SoundWire master %d is disabled or not-started, ignoring\n",
  1485. bus->link_id);
  1486. return 0;
  1487. }
  1488. clock_stop_quirks = sdw->link_res->clock_stop_quirks;
  1489. if (clock_stop_quirks & SDW_INTEL_CLK_STOP_TEARDOWN) {
  1490. ret = intel_stop_bus(sdw, false);
  1491. if (ret < 0) {
  1492. dev_err(dev, "%s: cannot stop bus during teardown: %d\n",
  1493. __func__, ret);
  1494. return ret;
  1495. }
  1496. } else if (clock_stop_quirks & SDW_INTEL_CLK_STOP_BUS_RESET || !clock_stop_quirks) {
  1497. ret = intel_stop_bus(sdw, true);
  1498. if (ret < 0) {
  1499. dev_err(dev, "%s: cannot stop bus during clock_stop: %d\n",
  1500. __func__, ret);
  1501. return ret;
  1502. }
  1503. } else {
  1504. dev_err(dev, "%s clock_stop_quirks %x unsupported\n",
  1505. __func__, clock_stop_quirks);
  1506. ret = -EINVAL;
  1507. }
  1508. return ret;
  1509. }
  1510. static int __maybe_unused intel_resume(struct device *dev)
  1511. {
  1512. struct sdw_cdns *cdns = dev_get_drvdata(dev);
  1513. struct sdw_intel *sdw = cdns_to_intel(cdns);
  1514. struct sdw_bus *bus = &cdns->bus;
  1515. int link_flags;
  1516. int ret;
  1517. if (bus->prop.hw_disabled || !sdw->startup_done) {
  1518. dev_dbg(dev, "SoundWire master %d is disabled or not-started, ignoring\n",
  1519. bus->link_id);
  1520. return 0;
  1521. }
  1522. link_flags = md_flags >> (bus->link_id * 8);
  1523. if (pm_runtime_suspended(dev)) {
  1524. dev_dbg(dev, "pm_runtime status was suspended, forcing active\n");
  1525. /* follow required sequence from runtime_pm.rst */
  1526. pm_runtime_disable(dev);
  1527. pm_runtime_set_active(dev);
  1528. pm_runtime_mark_last_busy(dev);
  1529. pm_runtime_enable(dev);
  1530. link_flags = md_flags >> (bus->link_id * 8);
  1531. if (!(link_flags & SDW_INTEL_MASTER_DISABLE_PM_RUNTIME_IDLE))
  1532. pm_runtime_idle(dev);
  1533. }
  1534. ret = intel_link_power_up(sdw);
  1535. if (ret) {
  1536. dev_err(dev, "%s failed: %d\n", __func__, ret);
  1537. return ret;
  1538. }
  1539. /*
  1540. * make sure all Slaves are tagged as UNATTACHED and provide
  1541. * reason for reinitialization
  1542. */
  1543. sdw_clear_slave_status(bus, SDW_UNATTACH_REQUEST_MASTER_RESET);
  1544. ret = intel_start_bus(sdw);
  1545. if (ret < 0) {
  1546. dev_err(dev, "cannot start bus during resume\n");
  1547. intel_link_power_down(sdw);
  1548. return ret;
  1549. }
  1550. /*
  1551. * after system resume, the pm_runtime suspend() may kick in
  1552. * during the enumeration, before any children device force the
  1553. * master device to remain active. Using pm_runtime_get()
  1554. * routines is not really possible, since it'd prevent the
  1555. * master from suspending.
  1556. * A reasonable compromise is to update the pm_runtime
  1557. * counters and delay the pm_runtime suspend by several
  1558. * seconds, by when all enumeration should be complete.
  1559. */
  1560. pm_runtime_mark_last_busy(dev);
  1561. return 0;
  1562. }
  1563. static int __maybe_unused intel_resume_runtime(struct device *dev)
  1564. {
  1565. struct sdw_cdns *cdns = dev_get_drvdata(dev);
  1566. struct sdw_intel *sdw = cdns_to_intel(cdns);
  1567. struct sdw_bus *bus = &cdns->bus;
  1568. u32 clock_stop_quirks;
  1569. int ret;
  1570. if (bus->prop.hw_disabled || !sdw->startup_done) {
  1571. dev_dbg(dev, "SoundWire master %d is disabled or not-started, ignoring\n",
  1572. bus->link_id);
  1573. return 0;
  1574. }
  1575. /* unconditionally disable WAKEEN interrupt */
  1576. intel_shim_wake(sdw, false);
  1577. clock_stop_quirks = sdw->link_res->clock_stop_quirks;
  1578. if (clock_stop_quirks & SDW_INTEL_CLK_STOP_TEARDOWN) {
  1579. ret = intel_link_power_up(sdw);
  1580. if (ret) {
  1581. dev_err(dev, "%s: power_up failed after teardown: %d\n", __func__, ret);
  1582. return ret;
  1583. }
  1584. /*
  1585. * make sure all Slaves are tagged as UNATTACHED and provide
  1586. * reason for reinitialization
  1587. */
  1588. sdw_clear_slave_status(bus, SDW_UNATTACH_REQUEST_MASTER_RESET);
  1589. ret = intel_start_bus(sdw);
  1590. if (ret < 0) {
  1591. dev_err(dev, "%s: cannot start bus after teardown: %d\n", __func__, ret);
  1592. intel_link_power_down(sdw);
  1593. return ret;
  1594. }
  1595. } else if (clock_stop_quirks & SDW_INTEL_CLK_STOP_BUS_RESET) {
  1596. ret = intel_link_power_up(sdw);
  1597. if (ret) {
  1598. dev_err(dev, "%s: power_up failed after bus reset: %d\n", __func__, ret);
  1599. return ret;
  1600. }
  1601. ret = intel_start_bus_after_reset(sdw);
  1602. if (ret < 0) {
  1603. dev_err(dev, "%s: cannot start bus after reset: %d\n", __func__, ret);
  1604. intel_link_power_down(sdw);
  1605. return ret;
  1606. }
  1607. } else if (!clock_stop_quirks) {
  1608. intel_check_clock_stop(sdw);
  1609. ret = intel_link_power_up(sdw);
  1610. if (ret) {
  1611. dev_err(dev, "%s: power_up failed: %d\n", __func__, ret);
  1612. return ret;
  1613. }
  1614. ret = intel_start_bus_after_clock_stop(sdw);
  1615. if (ret < 0) {
  1616. dev_err(dev, "%s: cannot start bus after clock stop: %d\n", __func__, ret);
  1617. intel_link_power_down(sdw);
  1618. return ret;
  1619. }
  1620. } else {
  1621. dev_err(dev, "%s: clock_stop_quirks %x unsupported\n",
  1622. __func__, clock_stop_quirks);
  1623. ret = -EINVAL;
  1624. }
  1625. return ret;
  1626. }
  1627. static const struct dev_pm_ops intel_pm = {
  1628. .prepare = intel_pm_prepare,
  1629. SET_SYSTEM_SLEEP_PM_OPS(intel_suspend, intel_resume)
  1630. SET_RUNTIME_PM_OPS(intel_suspend_runtime, intel_resume_runtime, NULL)
  1631. };
  1632. static const struct auxiliary_device_id intel_link_id_table[] = {
  1633. { .name = "soundwire_intel.link" },
  1634. {},
  1635. };
  1636. MODULE_DEVICE_TABLE(auxiliary, intel_link_id_table);
  1637. static struct auxiliary_driver sdw_intel_drv = {
  1638. .probe = intel_link_probe,
  1639. .remove = intel_link_remove,
  1640. .driver = {
  1641. /* auxiliary_driver_register() sets .name to be the modname */
  1642. .pm = &intel_pm,
  1643. },
  1644. .id_table = intel_link_id_table
  1645. };
  1646. module_auxiliary_driver(sdw_intel_drv);
  1647. MODULE_LICENSE("Dual BSD/GPL");
  1648. MODULE_DESCRIPTION("Intel Soundwire Link Driver");