intel_atomisp2_pm.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Dummy driver for Intel's Image Signal Processor found on Bay and Cherry
  4. * Trail devices. The sole purpose of this driver is to allow the ISP to
  5. * be put in D3.
  6. *
  7. * Copyright (C) 2018 Hans de Goede <hdegoede@redhat.com>
  8. *
  9. * Based on various non upstream patches for ISP support:
  10. * Copyright (C) 2010-2017 Intel Corporation. All rights reserved.
  11. * Copyright (c) 2010 Silicon Hive www.siliconhive.com.
  12. */
  13. #include <linux/delay.h>
  14. #include <linux/module.h>
  15. #include <linux/mod_devicetable.h>
  16. #include <linux/pci.h>
  17. #include <linux/pm_runtime.h>
  18. #include <asm/iosf_mbi.h>
  19. /* PCI configuration regs */
  20. #define PCI_INTERRUPT_CTRL 0x9c
  21. #define PCI_CSI_CONTROL 0xe8
  22. #define PCI_CSI_CONTROL_PORTS_OFF_MASK 0x7
  23. /* IOSF BT_MBI_UNIT_PMC regs */
  24. #define ISPSSPM0 0x39
  25. #define ISPSSPM0_ISPSSC_OFFSET 0
  26. #define ISPSSPM0_ISPSSC_MASK 0x00000003
  27. #define ISPSSPM0_ISPSSS_OFFSET 24
  28. #define ISPSSPM0_ISPSSS_MASK 0x03000000
  29. #define ISPSSPM0_IUNIT_POWER_ON 0x0
  30. #define ISPSSPM0_IUNIT_POWER_OFF 0x3
  31. static int isp_set_power(struct pci_dev *dev, bool enable)
  32. {
  33. unsigned long timeout;
  34. u32 val = enable ? ISPSSPM0_IUNIT_POWER_ON :
  35. ISPSSPM0_IUNIT_POWER_OFF;
  36. /* Write to ISPSSPM0 bit[1:0] to power on/off the IUNIT */
  37. iosf_mbi_modify(BT_MBI_UNIT_PMC, MBI_REG_READ, ISPSSPM0,
  38. val, ISPSSPM0_ISPSSC_MASK);
  39. /*
  40. * There should be no IUNIT access while power-down is
  41. * in progress HW sighting: 4567865
  42. * Wait up to 50 ms for the IUNIT to shut down.
  43. * And we do the same for power on.
  44. */
  45. timeout = jiffies + msecs_to_jiffies(50);
  46. while (1) {
  47. u32 tmp;
  48. /* Wait until ISPSSPM0 bit[25:24] shows the right value */
  49. iosf_mbi_read(BT_MBI_UNIT_PMC, MBI_REG_READ, ISPSSPM0, &tmp);
  50. tmp = (tmp & ISPSSPM0_ISPSSS_MASK) >> ISPSSPM0_ISPSSS_OFFSET;
  51. if (tmp == val)
  52. break;
  53. if (time_after(jiffies, timeout)) {
  54. dev_err(&dev->dev, "IUNIT power-%s timeout.\n",
  55. enable ? "on" : "off");
  56. return -EBUSY;
  57. }
  58. usleep_range(1000, 2000);
  59. }
  60. return 0;
  61. }
  62. static int isp_probe(struct pci_dev *dev, const struct pci_device_id *id)
  63. {
  64. pm_runtime_allow(&dev->dev);
  65. pm_runtime_put_sync_suspend(&dev->dev);
  66. return 0;
  67. }
  68. static void isp_remove(struct pci_dev *dev)
  69. {
  70. pm_runtime_get_sync(&dev->dev);
  71. pm_runtime_forbid(&dev->dev);
  72. }
  73. static int isp_pci_suspend(struct device *dev)
  74. {
  75. struct pci_dev *pdev = to_pci_dev(dev);
  76. u32 val;
  77. pci_write_config_dword(pdev, PCI_INTERRUPT_CTRL, 0);
  78. /*
  79. * MRFLD IUNIT DPHY is located in an always-power-on island
  80. * MRFLD HW design need all CSI ports are disabled before
  81. * powering down the IUNIT.
  82. */
  83. pci_read_config_dword(pdev, PCI_CSI_CONTROL, &val);
  84. val |= PCI_CSI_CONTROL_PORTS_OFF_MASK;
  85. pci_write_config_dword(pdev, PCI_CSI_CONTROL, val);
  86. /*
  87. * We lose config space access when punit power gates
  88. * the ISP. Can't use pci_set_power_state() because
  89. * pmcsr won't actually change when we write to it.
  90. */
  91. pci_save_state(pdev);
  92. pdev->current_state = PCI_D3cold;
  93. isp_set_power(pdev, false);
  94. return 0;
  95. }
  96. static int isp_pci_resume(struct device *dev)
  97. {
  98. struct pci_dev *pdev = to_pci_dev(dev);
  99. isp_set_power(pdev, true);
  100. pdev->current_state = PCI_D0;
  101. pci_restore_state(pdev);
  102. return 0;
  103. }
  104. static UNIVERSAL_DEV_PM_OPS(isp_pm_ops, isp_pci_suspend,
  105. isp_pci_resume, NULL);
  106. static const struct pci_device_id isp_id_table[] = {
  107. { PCI_VDEVICE(INTEL, 0x0f38), },
  108. { PCI_VDEVICE(INTEL, 0x22b8), },
  109. { 0, }
  110. };
  111. MODULE_DEVICE_TABLE(pci, isp_id_table);
  112. static struct pci_driver isp_pci_driver = {
  113. .name = "intel_atomisp2_pm",
  114. .id_table = isp_id_table,
  115. .probe = isp_probe,
  116. .remove = isp_remove,
  117. .driver.pm = &isp_pm_ops,
  118. };
  119. module_pci_driver(isp_pci_driver);
  120. MODULE_DESCRIPTION("Intel AtomISP2 dummy / power-management drv (for suspend)");
  121. MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
  122. MODULE_LICENSE("GPL v2");