intel_auxdevice.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872
  1. // SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause)
  2. // Copyright(c) 2015-22 Intel Corporation.
  3. /*
  4. * Soundwire Intel Manager 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. #include "intel_auxdevice.h"
  23. #define INTEL_MASTER_SUSPEND_DELAY_MS 3000
  24. /*
  25. * debug/config flags for the Intel SoundWire Master.
  26. *
  27. * Since we may have multiple masters active, we can have up to 8
  28. * flags reused in each byte, with master0 using the ls-byte, etc.
  29. */
  30. #define SDW_INTEL_MASTER_DISABLE_PM_RUNTIME BIT(0)
  31. #define SDW_INTEL_MASTER_DISABLE_CLOCK_STOP BIT(1)
  32. #define SDW_INTEL_MASTER_DISABLE_PM_RUNTIME_IDLE BIT(2)
  33. #define SDW_INTEL_MASTER_DISABLE_MULTI_LINK BIT(3)
  34. static int md_flags;
  35. module_param_named(sdw_md_flags, md_flags, int, 0444);
  36. MODULE_PARM_DESC(sdw_md_flags, "SoundWire Intel Master device flags (0x0 all off)");
  37. static int mclk_divider;
  38. module_param_named(sdw_mclk_divider, mclk_divider, int, 0444);
  39. MODULE_PARM_DESC(sdw_mclk_divider, "SoundWire Intel mclk divider");
  40. struct wake_capable_part {
  41. const u16 mfg_id;
  42. const u16 part_id;
  43. };
  44. static struct wake_capable_part wake_capable_list[] = {
  45. {0x01fa, 0x4243},
  46. {0x01fa, 0x4245},
  47. {0x025d, 0x5682},
  48. {0x025d, 0x700},
  49. {0x025d, 0x711},
  50. {0x025d, 0x1712},
  51. {0x025d, 0x1713},
  52. {0x025d, 0x1716},
  53. {0x025d, 0x1717},
  54. {0x025d, 0x712},
  55. {0x025d, 0x713},
  56. {0x025d, 0x714},
  57. {0x025d, 0x715},
  58. {0x025d, 0x716},
  59. {0x025d, 0x717},
  60. {0x025d, 0x721},
  61. {0x025d, 0x722},
  62. };
  63. static bool is_wake_capable(struct sdw_slave *slave)
  64. {
  65. int i;
  66. for (i = 0; i < ARRAY_SIZE(wake_capable_list); i++)
  67. if (slave->id.part_id == wake_capable_list[i].part_id &&
  68. slave->id.mfg_id == wake_capable_list[i].mfg_id)
  69. return true;
  70. return false;
  71. }
  72. static int generic_bpt_send_async(struct sdw_bus *bus, struct sdw_slave *slave,
  73. struct sdw_bpt_msg *msg)
  74. {
  75. struct sdw_cdns *cdns = bus_to_cdns(bus);
  76. struct sdw_intel *sdw = cdns_to_intel(cdns);
  77. if (sdw->link_res->hw_ops->bpt_send_async)
  78. return sdw->link_res->hw_ops->bpt_send_async(sdw, slave, msg);
  79. return -EOPNOTSUPP;
  80. }
  81. static int generic_bpt_wait(struct sdw_bus *bus, struct sdw_slave *slave, struct sdw_bpt_msg *msg)
  82. {
  83. struct sdw_cdns *cdns = bus_to_cdns(bus);
  84. struct sdw_intel *sdw = cdns_to_intel(cdns);
  85. if (sdw->link_res->hw_ops->bpt_wait)
  86. return sdw->link_res->hw_ops->bpt_wait(sdw, slave, msg);
  87. return -EOPNOTSUPP;
  88. }
  89. static int generic_pre_bank_switch(struct sdw_bus *bus)
  90. {
  91. struct sdw_cdns *cdns = bus_to_cdns(bus);
  92. struct sdw_intel *sdw = cdns_to_intel(cdns);
  93. return sdw->link_res->hw_ops->pre_bank_switch(sdw);
  94. }
  95. static int generic_post_bank_switch(struct sdw_bus *bus)
  96. {
  97. struct sdw_cdns *cdns = bus_to_cdns(bus);
  98. struct sdw_intel *sdw = cdns_to_intel(cdns);
  99. return sdw->link_res->hw_ops->post_bank_switch(sdw);
  100. }
  101. static void generic_new_peripheral_assigned(struct sdw_bus *bus,
  102. struct sdw_slave *slave,
  103. int dev_num)
  104. {
  105. struct sdw_cdns *cdns = bus_to_cdns(bus);
  106. struct sdw_intel *sdw = cdns_to_intel(cdns);
  107. int dev_num_min;
  108. int dev_num_max;
  109. bool wake_capable = slave->prop.wake_capable || is_wake_capable(slave);
  110. if (wake_capable) {
  111. dev_num_min = SDW_INTEL_DEV_NUM_IDA_MIN;
  112. dev_num_max = SDW_MAX_DEVICES;
  113. } else {
  114. dev_num_min = 1;
  115. dev_num_max = SDW_INTEL_DEV_NUM_IDA_MIN - 1;
  116. }
  117. /* paranoia check, this should never happen */
  118. if (dev_num < dev_num_min || dev_num > dev_num_max) {
  119. dev_err(bus->dev, "%s: invalid dev_num %d, wake supported %d\n",
  120. __func__, dev_num, slave->prop.wake_capable);
  121. return;
  122. }
  123. if (sdw->link_res->hw_ops->program_sdi && wake_capable)
  124. sdw->link_res->hw_ops->program_sdi(sdw, dev_num);
  125. }
  126. static int sdw_master_read_intel_prop(struct sdw_bus *bus)
  127. {
  128. struct sdw_master_prop *prop = &bus->prop;
  129. struct sdw_intel_prop *intel_prop;
  130. struct fwnode_handle *link;
  131. char name[32];
  132. u32 quirk_mask;
  133. /* Find master handle */
  134. snprintf(name, sizeof(name),
  135. "mipi-sdw-link-%d-subproperties", bus->link_id);
  136. link = device_get_named_child_node(bus->dev, name);
  137. if (!link) {
  138. dev_err(bus->dev, "Master node %s not found\n", name);
  139. return -EIO;
  140. }
  141. fwnode_property_read_u32(link,
  142. "intel-sdw-ip-clock",
  143. &prop->mclk_freq);
  144. if (mclk_divider)
  145. /* use kernel parameter for BIOS or board work-arounds */
  146. prop->mclk_freq /= mclk_divider;
  147. else
  148. /* the values reported by BIOS are the 2x clock, not the bus clock */
  149. prop->mclk_freq /= 2;
  150. fwnode_property_read_u32(link,
  151. "intel-quirk-mask",
  152. &quirk_mask);
  153. if (quirk_mask & SDW_INTEL_QUIRK_MASK_BUS_DISABLE)
  154. prop->hw_disabled = true;
  155. prop->quirks = SDW_MASTER_QUIRKS_CLEAR_INITIAL_CLASH |
  156. SDW_MASTER_QUIRKS_CLEAR_INITIAL_PARITY;
  157. intel_prop = devm_kzalloc(bus->dev, sizeof(*intel_prop), GFP_KERNEL);
  158. if (!intel_prop) {
  159. fwnode_handle_put(link);
  160. return -ENOMEM;
  161. }
  162. /* initialize with hardware defaults, in case the properties are not found */
  163. intel_prop->clde = 0x0;
  164. intel_prop->doaise2 = 0x0;
  165. intel_prop->dodse2 = 0x0;
  166. intel_prop->clds = 0x0;
  167. intel_prop->clss = 0x0;
  168. intel_prop->doaise = 0x1;
  169. intel_prop->doais = 0x3;
  170. intel_prop->dodse = 0x0;
  171. intel_prop->dods = 0x1;
  172. fwnode_property_read_u16(link,
  173. "intel-sdw-clde",
  174. &intel_prop->clde);
  175. fwnode_property_read_u16(link,
  176. "intel-sdw-doaise2",
  177. &intel_prop->doaise2);
  178. fwnode_property_read_u16(link,
  179. "intel-sdw-dodse2",
  180. &intel_prop->dodse2);
  181. fwnode_property_read_u16(link,
  182. "intel-sdw-clds",
  183. &intel_prop->clds);
  184. fwnode_property_read_u16(link,
  185. "intel-sdw-clss",
  186. &intel_prop->clss);
  187. fwnode_property_read_u16(link,
  188. "intel-sdw-doaise",
  189. &intel_prop->doaise);
  190. fwnode_property_read_u16(link,
  191. "intel-sdw-doais",
  192. &intel_prop->doais);
  193. fwnode_property_read_u16(link,
  194. "intel-sdw-dodse",
  195. &intel_prop->dodse);
  196. fwnode_property_read_u16(link,
  197. "intel-sdw-dods",
  198. &intel_prop->dods);
  199. bus->vendor_specific_prop = intel_prop;
  200. dev_dbg(bus->dev, "doaise %#x doais %#x dodse %#x dods %#x\n",
  201. intel_prop->doaise,
  202. intel_prop->doais,
  203. intel_prop->dodse,
  204. intel_prop->dods);
  205. fwnode_handle_put(link);
  206. return 0;
  207. }
  208. static int intel_prop_read(struct sdw_bus *bus)
  209. {
  210. /* Initialize with default handler to read all DisCo properties */
  211. sdw_master_read_prop(bus);
  212. /* read Intel-specific properties */
  213. sdw_master_read_intel_prop(bus);
  214. return 0;
  215. }
  216. static DEFINE_IDA(intel_peripheral_ida);
  217. static int intel_get_device_num_ida(struct sdw_bus *bus, struct sdw_slave *slave)
  218. {
  219. int bit;
  220. if (slave->prop.wake_capable || is_wake_capable(slave))
  221. return ida_alloc_range(&intel_peripheral_ida,
  222. SDW_INTEL_DEV_NUM_IDA_MIN, SDW_MAX_DEVICES,
  223. GFP_KERNEL);
  224. bit = find_first_zero_bit(slave->bus->assigned, SDW_MAX_DEVICES);
  225. if (bit == SDW_MAX_DEVICES)
  226. return -ENODEV;
  227. return bit;
  228. }
  229. static void intel_put_device_num_ida(struct sdw_bus *bus, struct sdw_slave *slave)
  230. {
  231. if (slave->prop.wake_capable || is_wake_capable(slave))
  232. ida_free(&intel_peripheral_ida, slave->dev_num);
  233. }
  234. static struct sdw_master_ops sdw_intel_ops = {
  235. .read_prop = intel_prop_read,
  236. .override_adr = sdw_dmi_override_adr,
  237. .xfer_msg = cdns_xfer_msg,
  238. .xfer_msg_defer = cdns_xfer_msg_defer,
  239. .set_bus_conf = cdns_bus_conf,
  240. .pre_bank_switch = generic_pre_bank_switch,
  241. .post_bank_switch = generic_post_bank_switch,
  242. .read_ping_status = cdns_read_ping_status,
  243. .get_device_num = intel_get_device_num_ida,
  244. .put_device_num = intel_put_device_num_ida,
  245. .new_peripheral_assigned = generic_new_peripheral_assigned,
  246. .bpt_send_async = generic_bpt_send_async,
  247. .bpt_wait = generic_bpt_wait,
  248. };
  249. /*
  250. * probe and init (aux_dev_id argument is required by function prototype but not used)
  251. */
  252. static int intel_link_probe(struct auxiliary_device *auxdev,
  253. const struct auxiliary_device_id *aux_dev_id)
  254. {
  255. struct device *dev = &auxdev->dev;
  256. struct sdw_intel_link_dev *ldev = auxiliary_dev_to_sdw_intel_link_dev(auxdev);
  257. struct sdw_intel *sdw;
  258. struct sdw_cdns *cdns;
  259. struct sdw_bus *bus;
  260. int ret;
  261. sdw = devm_kzalloc(dev, sizeof(*sdw), GFP_KERNEL);
  262. if (!sdw)
  263. return -ENOMEM;
  264. cdns = &sdw->cdns;
  265. bus = &cdns->bus;
  266. sdw->instance = auxdev->id;
  267. sdw->link_res = &ldev->link_res;
  268. cdns->dev = dev;
  269. cdns->registers = sdw->link_res->registers;
  270. cdns->ip_offset = sdw->link_res->ip_offset;
  271. cdns->instance = sdw->instance;
  272. cdns->msg_count = 0;
  273. /* single controller for all SoundWire links */
  274. bus->controller_id = 0;
  275. bus->link_id = auxdev->id;
  276. bus->clk_stop_timeout = 1;
  277. /*
  278. * paranoia check: make sure ACPI-reported number of links is aligned with
  279. * hardware capabilities.
  280. */
  281. ret = sdw_intel_get_link_count(sdw);
  282. if (ret < 0) {
  283. dev_err(dev, "%s: sdw_intel_get_link_count failed: %d\n", __func__, ret);
  284. return ret;
  285. }
  286. if (ret <= sdw->instance) {
  287. dev_err(dev, "%s: invalid link id %d, link count %d\n", __func__, auxdev->id, ret);
  288. return -EINVAL;
  289. }
  290. sdw_cdns_probe(cdns);
  291. /* Set ops */
  292. bus->ops = &sdw_intel_ops;
  293. /* set driver data, accessed by snd_soc_dai_get_drvdata() */
  294. auxiliary_set_drvdata(auxdev, cdns);
  295. /* use generic bandwidth allocation algorithm */
  296. sdw->cdns.bus.compute_params = sdw_compute_params;
  297. ret = sdw_bus_master_add(bus, dev, dev->fwnode);
  298. if (ret) {
  299. dev_err(dev, "sdw_bus_master_add fail: %d\n", ret);
  300. return ret;
  301. }
  302. if (bus->prop.hw_disabled)
  303. dev_info(dev,
  304. "SoundWire master %d is disabled, will be ignored\n",
  305. bus->link_id);
  306. /*
  307. * Ignore BIOS err_threshold, it's a really bad idea when dealing
  308. * with multiple hardware synchronized links
  309. */
  310. bus->prop.err_threshold = 0;
  311. return 0;
  312. }
  313. int intel_link_startup(struct auxiliary_device *auxdev)
  314. {
  315. struct device *dev = &auxdev->dev;
  316. struct sdw_cdns *cdns = auxiliary_get_drvdata(auxdev);
  317. struct sdw_intel *sdw = cdns_to_intel(cdns);
  318. struct sdw_bus *bus = &cdns->bus;
  319. int link_flags;
  320. bool multi_link;
  321. u32 clock_stop_quirks;
  322. int ret;
  323. if (bus->prop.hw_disabled) {
  324. dev_info(dev,
  325. "SoundWire master %d is disabled, ignoring\n",
  326. sdw->instance);
  327. return 0;
  328. }
  329. link_flags = md_flags >> (bus->link_id * 8);
  330. multi_link = !(link_flags & SDW_INTEL_MASTER_DISABLE_MULTI_LINK);
  331. if (!multi_link) {
  332. dev_dbg(dev, "Multi-link is disabled\n");
  333. } else {
  334. /*
  335. * hardware-based synchronization is required regardless
  336. * of the number of segments used by a stream: SSP-based
  337. * synchronization is gated by gsync when the multi-master
  338. * mode is set.
  339. */
  340. bus->hw_sync_min_links = 1;
  341. }
  342. bus->multi_link = multi_link;
  343. /* Initialize shim, controller */
  344. ret = sdw_intel_link_power_up(sdw);
  345. if (ret)
  346. goto err_init;
  347. /* Register DAIs */
  348. ret = sdw_intel_register_dai(sdw);
  349. if (ret) {
  350. dev_err(dev, "DAI registration failed: %d\n", ret);
  351. goto err_power_up;
  352. }
  353. sdw_intel_debugfs_init(sdw);
  354. /* Enable runtime PM */
  355. if (!(link_flags & SDW_INTEL_MASTER_DISABLE_PM_RUNTIME)) {
  356. pm_runtime_set_autosuspend_delay(dev,
  357. INTEL_MASTER_SUSPEND_DELAY_MS);
  358. pm_runtime_use_autosuspend(dev);
  359. pm_runtime_mark_last_busy(dev);
  360. pm_runtime_set_active(dev);
  361. pm_runtime_enable(dev);
  362. pm_runtime_resume(bus->dev);
  363. }
  364. /* start bus */
  365. ret = sdw_intel_start_bus(sdw);
  366. if (ret) {
  367. dev_err(dev, "bus start failed: %d\n", ret);
  368. goto err_pm_runtime;
  369. }
  370. clock_stop_quirks = sdw->link_res->clock_stop_quirks;
  371. if (clock_stop_quirks & SDW_INTEL_CLK_STOP_NOT_ALLOWED) {
  372. /*
  373. * To keep the clock running we need to prevent
  374. * pm_runtime suspend from happening by increasing the
  375. * reference count.
  376. * This quirk is specified by the parent PCI device in
  377. * case of specific latency requirements. It will have
  378. * no effect if pm_runtime is disabled by the user via
  379. * a module parameter for testing purposes.
  380. */
  381. pm_runtime_get_noresume(dev);
  382. }
  383. /*
  384. * The runtime PM status of Slave devices is "Unsupported"
  385. * until they report as ATTACHED. If they don't, e.g. because
  386. * there are no Slave devices populated or if the power-on is
  387. * delayed or dependent on a power switch, the Master will
  388. * remain active and prevent its parent from suspending.
  389. *
  390. * Conditionally force the pm_runtime core to re-evaluate the
  391. * Master status in the absence of any Slave activity. A quirk
  392. * is provided to e.g. deal with Slaves that may be powered on
  393. * with a delay. A more complete solution would require the
  394. * definition of Master properties.
  395. */
  396. if (!(link_flags & SDW_INTEL_MASTER_DISABLE_PM_RUNTIME_IDLE)) {
  397. pm_runtime_mark_last_busy(bus->dev);
  398. pm_runtime_mark_last_busy(dev);
  399. pm_runtime_idle(dev);
  400. }
  401. sdw->startup_done = true;
  402. return 0;
  403. err_pm_runtime:
  404. if (!(link_flags & SDW_INTEL_MASTER_DISABLE_PM_RUNTIME))
  405. pm_runtime_disable(dev);
  406. err_power_up:
  407. sdw_intel_link_power_down(sdw);
  408. err_init:
  409. return ret;
  410. }
  411. static void intel_link_remove(struct auxiliary_device *auxdev)
  412. {
  413. struct sdw_cdns *cdns = auxiliary_get_drvdata(auxdev);
  414. struct sdw_intel *sdw = cdns_to_intel(cdns);
  415. struct sdw_bus *bus = &cdns->bus;
  416. /*
  417. * Since pm_runtime is already disabled, we don't decrease
  418. * the refcount when the clock_stop_quirk is
  419. * SDW_INTEL_CLK_STOP_NOT_ALLOWED
  420. */
  421. if (!bus->prop.hw_disabled) {
  422. sdw_intel_debugfs_exit(sdw);
  423. cancel_delayed_work_sync(&cdns->attach_dwork);
  424. sdw_cdns_enable_interrupt(cdns, false);
  425. }
  426. sdw_bus_master_delete(bus);
  427. }
  428. int intel_link_process_wakeen_event(struct auxiliary_device *auxdev)
  429. {
  430. struct device *dev = &auxdev->dev;
  431. struct sdw_intel *sdw;
  432. struct sdw_bus *bus;
  433. sdw = auxiliary_get_drvdata(auxdev);
  434. bus = &sdw->cdns.bus;
  435. if (bus->prop.hw_disabled || !sdw->startup_done) {
  436. dev_dbg(dev, "SoundWire master %d is disabled or not-started, ignoring\n",
  437. bus->link_id);
  438. return 0;
  439. }
  440. if (!sdw_intel_shim_check_wake(sdw))
  441. return 0;
  442. /* disable WAKEEN interrupt ASAP to prevent interrupt flood */
  443. sdw_intel_shim_wake(sdw, false);
  444. /*
  445. * resume the Master, which will generate a bus reset and result in
  446. * Slaves re-attaching and be re-enumerated. The SoundWire physical
  447. * device which generated the wake will trigger an interrupt, which
  448. * will in turn cause the corresponding Linux Slave device to be
  449. * resumed and the Slave codec driver to check the status.
  450. */
  451. pm_request_resume(dev);
  452. return 0;
  453. }
  454. /*
  455. * PM calls
  456. */
  457. int intel_resume_child_device(struct device *dev, void *data)
  458. {
  459. int ret;
  460. struct sdw_slave *slave = dev_to_sdw_dev(dev);
  461. if (!slave->probed) {
  462. dev_dbg(dev, "skipping device, no probed driver\n");
  463. return 0;
  464. }
  465. if (!slave->dev_num_sticky) {
  466. dev_dbg(dev, "skipping device, never detected on bus\n");
  467. return 0;
  468. }
  469. ret = pm_runtime_resume(dev);
  470. if (ret < 0) {
  471. dev_err(dev, "%s: pm_runtime_resume failed: %d\n", __func__, ret);
  472. return ret;
  473. }
  474. return 0;
  475. }
  476. static int __maybe_unused intel_pm_prepare(struct device *dev)
  477. {
  478. struct sdw_cdns *cdns = dev_get_drvdata(dev);
  479. struct sdw_intel *sdw = cdns_to_intel(cdns);
  480. struct sdw_bus *bus = &cdns->bus;
  481. u32 clock_stop_quirks;
  482. int ret;
  483. if (bus->prop.hw_disabled || !sdw->startup_done) {
  484. dev_dbg(dev, "SoundWire master %d is disabled or not-started, ignoring\n",
  485. bus->link_id);
  486. return 0;
  487. }
  488. clock_stop_quirks = sdw->link_res->clock_stop_quirks;
  489. if (pm_runtime_suspended(dev) &&
  490. pm_runtime_suspended(dev->parent) &&
  491. ((clock_stop_quirks & SDW_INTEL_CLK_STOP_BUS_RESET) ||
  492. !clock_stop_quirks)) {
  493. /*
  494. * if we've enabled clock stop, and the parent is suspended, the SHIM registers
  495. * are not accessible and the shim wake cannot be disabled.
  496. * The only solution is to resume the entire bus to full power
  497. */
  498. /*
  499. * If any operation in this block fails, we keep going since we don't want
  500. * to prevent system suspend from happening and errors should be recoverable
  501. * on resume.
  502. */
  503. /*
  504. * first resume the device for this link. This will also by construction
  505. * resume the PCI parent device.
  506. */
  507. ret = pm_runtime_resume(dev);
  508. if (ret < 0) {
  509. dev_err(dev, "%s: pm_runtime_resume failed: %d\n", __func__, ret);
  510. return 0;
  511. }
  512. /*
  513. * Continue resuming the entire bus (parent + child devices) to exit
  514. * the clock stop mode. If there are no devices connected on this link
  515. * this is a no-op.
  516. * The resume to full power could have been implemented with a .prepare
  517. * step in SoundWire codec drivers. This would however require a lot
  518. * of code to handle an Intel-specific corner case. It is simpler in
  519. * practice to add a loop at the link level.
  520. */
  521. ret = device_for_each_child(bus->dev, NULL, intel_resume_child_device);
  522. if (ret < 0)
  523. dev_err(dev, "%s: intel_resume_child_device failed: %d\n", __func__, ret);
  524. }
  525. return 0;
  526. }
  527. static int __maybe_unused intel_suspend(struct device *dev)
  528. {
  529. struct sdw_cdns *cdns = dev_get_drvdata(dev);
  530. struct sdw_intel *sdw = cdns_to_intel(cdns);
  531. struct sdw_bus *bus = &cdns->bus;
  532. u32 clock_stop_quirks;
  533. int ret;
  534. if (bus->prop.hw_disabled || !sdw->startup_done) {
  535. dev_dbg(dev, "SoundWire master %d is disabled or not-started, ignoring\n",
  536. bus->link_id);
  537. return 0;
  538. }
  539. /* Prevent runtime PM from racing with the code below. */
  540. pm_runtime_disable(dev);
  541. if (pm_runtime_status_suspended(dev)) {
  542. dev_dbg(dev, "pm_runtime status: suspended\n");
  543. clock_stop_quirks = sdw->link_res->clock_stop_quirks;
  544. if ((clock_stop_quirks & SDW_INTEL_CLK_STOP_BUS_RESET) ||
  545. !clock_stop_quirks) {
  546. if (pm_runtime_status_suspended(dev->parent)) {
  547. /*
  548. * paranoia check: this should not happen with the .prepare
  549. * resume to full power
  550. */
  551. dev_err(dev, "%s: invalid config: parent is suspended\n", __func__);
  552. } else {
  553. sdw_intel_shim_wake(sdw, false);
  554. }
  555. }
  556. return 0;
  557. }
  558. ret = sdw_intel_stop_bus(sdw, false);
  559. if (ret < 0) {
  560. dev_err(dev, "%s: cannot stop bus: %d\n", __func__, ret);
  561. return ret;
  562. }
  563. return 0;
  564. }
  565. static int __maybe_unused intel_suspend_runtime(struct device *dev)
  566. {
  567. struct sdw_cdns *cdns = dev_get_drvdata(dev);
  568. struct sdw_intel *sdw = cdns_to_intel(cdns);
  569. struct sdw_bus *bus = &cdns->bus;
  570. u32 clock_stop_quirks;
  571. int ret;
  572. if (bus->prop.hw_disabled || !sdw->startup_done) {
  573. dev_dbg(dev, "SoundWire master %d is disabled or not-started, ignoring\n",
  574. bus->link_id);
  575. return 0;
  576. }
  577. clock_stop_quirks = sdw->link_res->clock_stop_quirks;
  578. if (clock_stop_quirks & SDW_INTEL_CLK_STOP_TEARDOWN) {
  579. ret = sdw_intel_stop_bus(sdw, false);
  580. if (ret < 0) {
  581. dev_err(dev, "%s: cannot stop bus during teardown: %d\n",
  582. __func__, ret);
  583. return ret;
  584. }
  585. } else if (clock_stop_quirks & SDW_INTEL_CLK_STOP_BUS_RESET || !clock_stop_quirks) {
  586. ret = sdw_intel_stop_bus(sdw, true);
  587. if (ret < 0) {
  588. dev_err(dev, "%s: cannot stop bus during clock_stop: %d\n",
  589. __func__, ret);
  590. return ret;
  591. }
  592. } else {
  593. dev_err(dev, "%s clock_stop_quirks %x unsupported\n",
  594. __func__, clock_stop_quirks);
  595. ret = -EINVAL;
  596. }
  597. return ret;
  598. }
  599. static int __maybe_unused intel_resume(struct device *dev)
  600. {
  601. struct sdw_cdns *cdns = dev_get_drvdata(dev);
  602. struct sdw_intel *sdw = cdns_to_intel(cdns);
  603. struct sdw_bus *bus = &cdns->bus;
  604. int ret;
  605. if (bus->prop.hw_disabled || !sdw->startup_done) {
  606. dev_dbg(dev, "SoundWire master %d is disabled or not-started, ignoring\n",
  607. bus->link_id);
  608. return 0;
  609. }
  610. ret = sdw_intel_link_power_up(sdw);
  611. if (ret) {
  612. dev_err(dev, "%s failed: %d\n", __func__, ret);
  613. return ret;
  614. }
  615. /*
  616. * make sure all Slaves are tagged as UNATTACHED and provide
  617. * reason for reinitialization
  618. */
  619. sdw_clear_slave_status(bus, SDW_UNATTACH_REQUEST_MASTER_RESET);
  620. ret = sdw_intel_start_bus(sdw);
  621. if (ret < 0) {
  622. dev_err(dev, "cannot start bus during resume\n");
  623. sdw_intel_link_power_down(sdw);
  624. return ret;
  625. }
  626. /*
  627. * Runtime PM has been disabled in intel_suspend(), so set the status
  628. * to active because the device has just been resumed and re-enable
  629. * runtime PM.
  630. */
  631. pm_runtime_set_active(dev);
  632. pm_runtime_enable(dev);
  633. /*
  634. * after system resume, the pm_runtime suspend() may kick in
  635. * during the enumeration, before any children device force the
  636. * master device to remain active. Using pm_runtime_get()
  637. * routines is not really possible, since it'd prevent the
  638. * master from suspending.
  639. * A reasonable compromise is to update the pm_runtime
  640. * counters and delay the pm_runtime suspend by several
  641. * seconds, by when all enumeration should be complete.
  642. */
  643. pm_runtime_mark_last_busy(bus->dev);
  644. pm_runtime_mark_last_busy(dev);
  645. return 0;
  646. }
  647. static int __maybe_unused intel_resume_runtime(struct device *dev)
  648. {
  649. struct sdw_cdns *cdns = dev_get_drvdata(dev);
  650. struct sdw_intel *sdw = cdns_to_intel(cdns);
  651. struct sdw_bus *bus = &cdns->bus;
  652. u32 clock_stop_quirks;
  653. int ret;
  654. if (bus->prop.hw_disabled || !sdw->startup_done) {
  655. dev_dbg(dev, "SoundWire master %d is disabled or not-started, ignoring\n",
  656. bus->link_id);
  657. return 0;
  658. }
  659. /* unconditionally disable WAKEEN interrupt */
  660. sdw_intel_shim_wake(sdw, false);
  661. clock_stop_quirks = sdw->link_res->clock_stop_quirks;
  662. if (clock_stop_quirks & SDW_INTEL_CLK_STOP_TEARDOWN) {
  663. ret = sdw_intel_link_power_up(sdw);
  664. if (ret) {
  665. dev_err(dev, "%s: power_up failed after teardown: %d\n", __func__, ret);
  666. return ret;
  667. }
  668. /*
  669. * make sure all Slaves are tagged as UNATTACHED and provide
  670. * reason for reinitialization
  671. */
  672. sdw_clear_slave_status(bus, SDW_UNATTACH_REQUEST_MASTER_RESET);
  673. ret = sdw_intel_start_bus(sdw);
  674. if (ret < 0) {
  675. dev_err(dev, "%s: cannot start bus after teardown: %d\n", __func__, ret);
  676. sdw_intel_link_power_down(sdw);
  677. return ret;
  678. }
  679. } else if (clock_stop_quirks & SDW_INTEL_CLK_STOP_BUS_RESET) {
  680. ret = sdw_intel_link_power_up(sdw);
  681. if (ret) {
  682. dev_err(dev, "%s: power_up failed after bus reset: %d\n", __func__, ret);
  683. return ret;
  684. }
  685. ret = sdw_intel_start_bus_after_reset(sdw);
  686. if (ret < 0) {
  687. dev_err(dev, "%s: cannot start bus after reset: %d\n", __func__, ret);
  688. sdw_intel_link_power_down(sdw);
  689. return ret;
  690. }
  691. } else if (!clock_stop_quirks) {
  692. sdw_intel_check_clock_stop(sdw);
  693. ret = sdw_intel_link_power_up(sdw);
  694. if (ret) {
  695. dev_err(dev, "%s: power_up failed: %d\n", __func__, ret);
  696. return ret;
  697. }
  698. ret = sdw_intel_start_bus_after_clock_stop(sdw);
  699. if (ret < 0) {
  700. dev_err(dev, "%s: cannot start bus after clock stop: %d\n", __func__, ret);
  701. sdw_intel_link_power_down(sdw);
  702. return ret;
  703. }
  704. } else {
  705. dev_err(dev, "%s: clock_stop_quirks %x unsupported\n",
  706. __func__, clock_stop_quirks);
  707. ret = -EINVAL;
  708. }
  709. return ret;
  710. }
  711. static const struct dev_pm_ops intel_pm = {
  712. .prepare = intel_pm_prepare,
  713. SET_SYSTEM_SLEEP_PM_OPS(intel_suspend, intel_resume)
  714. SET_RUNTIME_PM_OPS(intel_suspend_runtime, intel_resume_runtime, NULL)
  715. };
  716. static const struct auxiliary_device_id intel_link_id_table[] = {
  717. { .name = "soundwire_intel.link" },
  718. {},
  719. };
  720. MODULE_DEVICE_TABLE(auxiliary, intel_link_id_table);
  721. static struct auxiliary_driver sdw_intel_drv = {
  722. .probe = intel_link_probe,
  723. .remove = intel_link_remove,
  724. .driver = {
  725. /* auxiliary_driver_register() sets .name to be the modname */
  726. .pm = &intel_pm,
  727. },
  728. .id_table = intel_link_id_table
  729. };
  730. module_auxiliary_driver(sdw_intel_drv);
  731. MODULE_LICENSE("Dual BSD/GPL");
  732. MODULE_DESCRIPTION("Intel Soundwire Link Driver");