intel_auxdevice.c 23 KB

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