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