fc2324e26eca68824466d97109dc3bf153972050
[pandora-kernel.git] / drivers / staging / gma500 / power.c
1 /**************************************************************************
2  * Copyright (c) 2009-2011, Intel Corporation.
3  * All Rights Reserved.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice (including the next
13  * paragraph) shall be included in all copies or substantial portions of the
14  * Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22  * SOFTWARE.
23  *
24  * Authors:
25  *    Benjamin Defnet <benjamin.r.defnet@intel.com>
26  *    Rajesh Poornachandran <rajesh.poornachandran@intel.com>
27  * Massively reworked
28  *    Alan Cox <alan@linux.intel.com>
29  */
30 #include "psb_powermgmt.h"
31 #include "psb_drv.h"
32 #include "psb_reg.h"
33 #include "psb_intel_reg.h"
34 #include <linux/mutex.h>
35 #include <linux/pm_runtime.h>
36
37 static struct mutex power_mutex;
38
39 /**
40  *      gma_power_init          -       initialise power manager
41  *      @dev: our device
42  *
43  *      Set up for power management tracking of our hardware.
44  */
45 void gma_power_init(struct drm_device *dev)
46 {
47         struct drm_psb_private *dev_priv = dev->dev_private;
48
49         /* FIXME: need to sort out fetching apm_reg for both platforms ?? */
50
51         dev_priv->apm_base = dev_priv->apm_reg & 0xffff;
52         dev_priv->ospm_base &= 0xffff;
53
54         dev_priv->display_power = true; /* We start active */
55         dev_priv->display_count = 0;    /* Currently no users */
56         dev_priv->suspended = false;    /* And not suspended */
57         mutex_init(&power_mutex);
58
59         dev_priv->ops->init_pm(dev);
60 }
61
62 /**
63  *      gma_power_uninit        -       end power manager
64  *      @dev: device to end for
65  *
66  *      Undo the effects of gma_power_init
67  */
68 void gma_power_uninit(struct drm_device *dev)
69 {
70         mutex_destroy(&power_mutex);
71         pm_runtime_disable(&dev->pdev->dev);
72         pm_runtime_set_suspended(&dev->pdev->dev);
73 }
74
75
76
77
78 /**
79  *      gma_suspend_display     -       suspend the display logic
80  *      @dev: our DRM device
81  *
82  *      Suspend the display logic of the graphics interface
83  *
84  *      FIXME: This ought to be replaced by a dev_priv-> ops interface
85  *      where the various platforms register their save/restore methods
86  *      and keep them in their own support files.
87  */
88 static void gma_suspend_display(struct drm_device *dev)
89 {
90         struct drm_psb_private *dev_priv = dev->dev_private;
91
92         if (dev_priv->suspended)
93                 return;
94         dev_priv->ops->save_regs(dev);
95         dev_priv->ops->power_down(dev);
96         dev_priv->display_power = false;
97 }
98
99 /**
100  *      gma_resume_display      -       resume display side logic
101  *
102  *      Resume the display hardware restoring state and enabling
103  *      as necessary.
104  */
105 static void gma_resume_display(struct pci_dev *pdev)
106 {
107         struct drm_device *dev = pci_get_drvdata(pdev);
108         struct drm_psb_private *dev_priv = dev->dev_private;
109
110         if (dev_priv->suspended == false)
111                 return;
112
113         /* turn on the display power island */
114         dev_priv->ops->power_up(dev);
115         dev_priv->suspended = false;
116         dev_priv->display_power = true;
117
118         PSB_WVDC32(dev_priv->pge_ctl | _PSB_PGETBL_ENABLED, PSB_PGETBL_CTL);
119         pci_write_config_word(pdev, PSB_GMCH_CTRL,
120                         dev_priv->gmch_ctrl | _PSB_GMCH_ENABLED);
121         dev_priv->ops->restore_regs(dev);
122 }
123
124 /**
125  *      gma_suspend_pci         -       suspend PCI side
126  *      @pdev: PCI device
127  *
128  *      Perform the suspend processing on our PCI device state
129  */
130 static void gma_suspend_pci(struct pci_dev *pdev)
131 {
132         struct drm_device *dev = pci_get_drvdata(pdev);
133         struct drm_psb_private *dev_priv = dev->dev_private;
134         int bsm, vbt;
135
136         if (dev_priv->suspended)
137                 return;
138
139         pci_save_state(pdev);
140         pci_read_config_dword(pdev, 0x5C, &bsm);
141         dev_priv->saveBSM = bsm;
142         pci_read_config_dword(pdev, 0xFC, &vbt);
143         dev_priv->saveVBT = vbt;
144         pci_read_config_dword(pdev, PSB_PCIx_MSI_ADDR_LOC, &dev_priv->msi_addr);
145         pci_read_config_dword(pdev, PSB_PCIx_MSI_DATA_LOC, &dev_priv->msi_data);
146
147         pci_disable_device(pdev);
148         pci_set_power_state(pdev, PCI_D3hot);
149
150         dev_priv->suspended = true;
151 }
152
153 /**
154  *      gma_resume_pci          -       resume helper
155  *      @dev: our PCI device
156  *
157  *      Perform the resume processing on our PCI device state - rewrite
158  *      register state and re-enable the PCI device
159  */
160 static bool gma_resume_pci(struct pci_dev *pdev)
161 {
162         struct drm_device *dev = pci_get_drvdata(pdev);
163         struct drm_psb_private *dev_priv = dev->dev_private;
164         int ret;
165
166         if (!dev_priv->suspended)
167                 return true;
168
169         pci_set_power_state(pdev, PCI_D0);
170         pci_restore_state(pdev);
171         pci_write_config_dword(pdev, 0x5c, dev_priv->saveBSM);
172         pci_write_config_dword(pdev, 0xFC, dev_priv->saveVBT);
173         /* restoring MSI address and data in PCIx space */
174         pci_write_config_dword(pdev, PSB_PCIx_MSI_ADDR_LOC, dev_priv->msi_addr);
175         pci_write_config_dword(pdev, PSB_PCIx_MSI_DATA_LOC, dev_priv->msi_data);
176         ret = pci_enable_device(pdev);
177
178         if (ret != 0)
179                 dev_err(&pdev->dev, "pci_enable failed: %d\n", ret);
180         else
181                 dev_priv->suspended = false;
182         return !dev_priv->suspended;
183 }
184
185 /**
186  *      gma_power_suspend               -       bus callback for suspend
187  *      @pdev: our PCI device
188  *      @state: suspend type
189  *
190  *      Called back by the PCI layer during a suspend of the system. We
191  *      perform the necessary shut down steps and save enough state that
192  *      we can undo this when resume is called.
193  */
194 int gma_power_suspend(struct pci_dev *pdev, pm_message_t state)
195 {
196         struct drm_device *dev = pci_get_drvdata(pdev);
197         struct drm_psb_private *dev_priv = dev->dev_private;
198
199         mutex_lock(&power_mutex);
200         if (!dev_priv->suspended) {
201                 if (dev_priv->display_count) {
202                         mutex_unlock(&power_mutex);
203                         return -EBUSY;
204                 }
205                 psb_irq_uninstall(dev);
206                 gma_suspend_display(dev);
207                 gma_suspend_pci(pdev);
208         }
209         mutex_unlock(&power_mutex);
210         return 0;
211 }
212
213
214 /**
215  *      gma_power_resume                -       resume power
216  *      @pdev: PCI device
217  *
218  *      Resume the PCI side of the graphics and then the displays
219  */
220 int gma_power_resume(struct pci_dev *pdev)
221 {
222         struct drm_device *dev = pci_get_drvdata(pdev);
223
224         mutex_lock(&power_mutex);
225         gma_resume_pci(pdev);
226         gma_resume_display(pdev);
227         psb_irq_preinstall(dev);
228         psb_irq_postinstall(dev);
229         mutex_unlock(&power_mutex);
230         return 0;
231 }
232
233
234
235 /**
236  *      gma_power_is_on         -       returne true if power is on
237  *      @dev: our DRM device
238  *
239  *      Returns true if the display island power is on at this moment
240  */
241 bool gma_power_is_on(struct drm_device *dev)
242 {
243         struct drm_psb_private *dev_priv = dev->dev_private;
244         return dev_priv->display_power;
245 }
246
247
248 /**
249  *      gma_power_begin         -       begin requiring power
250  *      @dev: our DRM device
251  *      @force_on: true to force power on
252  *
253  *      Begin an action that requires the display power island is enabled.
254  *      We refcount the islands.
255  *
256  *      FIXME: locking
257  */
258 bool gma_power_begin(struct drm_device *dev, bool force_on)
259 {
260         struct drm_psb_private *dev_priv = dev->dev_private;
261         int ret;
262
263         /* Power already on ? */
264         if (dev_priv->display_power) {
265                 dev_priv->display_count++;
266                 pm_runtime_get(&dev->pdev->dev);
267                 return true;
268         }
269         if (force_on == false)
270                 return false;
271
272         /* Ok power up needed */
273         ret = gma_resume_pci(dev->pdev);
274         if (ret == 0) {
275                 psb_irq_preinstall(dev);
276                 psb_irq_postinstall(dev);
277                 pm_runtime_get(&dev->pdev->dev);
278                 dev_priv->display_count++;
279                 return true;
280         }
281         return false;
282 }
283
284
285 /**
286  *      gma_power_end           -       end use of power
287  *      @dev: Our DRM device
288  *
289  *      Indicate that one of our gma_power_begin() requested periods when
290  *      the diplay island power is needed has completed.
291  */
292 void gma_power_end(struct drm_device *dev)
293 {
294         struct drm_psb_private *dev_priv = dev->dev_private;
295         dev_priv->display_count--;
296         WARN_ON(dev_priv->display_count < 0);
297         pm_runtime_put(&dev->pdev->dev);
298 }
299
300 int psb_runtime_suspend(struct device *dev)
301 {
302         static pm_message_t dummy;
303         return gma_power_suspend(to_pci_dev(dev), dummy);
304 }
305
306 int psb_runtime_resume(struct device *dev)
307 {
308         return 0;
309 }
310
311 int psb_runtime_idle(struct device *dev)
312 {
313         struct drm_device *drmdev = pci_get_drvdata(to_pci_dev(dev));
314         struct drm_psb_private *dev_priv = drmdev->dev_private;
315         if (dev_priv->display_count)
316                 return 0;
317         else
318                 return 1;
319 }
320