blob: 302f7ebe5b6ff2809936022702bcf8611fc62e7e [file] [log] [blame]
Ben Skeggs45284162010-04-07 12:57:35 +10001/*
2 * Copyright 2010 Red Hat Inc.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 * OTHER DEALINGS IN THE SOFTWARE.
21 *
22 * Authors: Ben Skeggs
23 */
24
25#include "drmP.h"
26#include "nouveau_drv.h"
27#include "nouveau_hw.h"
28
Ben Skeggs2cbd4c82010-11-03 10:18:04 +100029static void nv50_gpio_isr(struct drm_device *dev);
30
Ben Skeggs45284162010-04-07 12:57:35 +100031static int
32nv50_gpio_location(struct dcb_gpio_entry *gpio, uint32_t *reg, uint32_t *shift)
33{
34 const uint32_t nv50_gpio_reg[4] = { 0xe104, 0xe108, 0xe280, 0xe284 };
35
Dan Carpenter55a4c5c2010-04-22 11:40:53 +020036 if (gpio->line >= 32)
Ben Skeggs45284162010-04-07 12:57:35 +100037 return -EINVAL;
38
39 *reg = nv50_gpio_reg[gpio->line >> 3];
40 *shift = (gpio->line & 7) << 2;
41 return 0;
42}
43
44int
45nv50_gpio_get(struct drm_device *dev, enum dcb_gpio_tag tag)
46{
47 struct dcb_gpio_entry *gpio;
48 uint32_t r, s, v;
49
50 gpio = nouveau_bios_gpio_entry(dev, tag);
51 if (!gpio)
52 return -ENOENT;
53
54 if (nv50_gpio_location(gpio, &r, &s))
55 return -EINVAL;
56
57 v = nv_rd32(dev, r) >> (s + 2);
58 return ((v & 1) == (gpio->state[1] & 1));
59}
60
61int
62nv50_gpio_set(struct drm_device *dev, enum dcb_gpio_tag tag, int state)
63{
64 struct dcb_gpio_entry *gpio;
65 uint32_t r, s, v;
66
67 gpio = nouveau_bios_gpio_entry(dev, tag);
68 if (!gpio)
69 return -ENOENT;
70
71 if (nv50_gpio_location(gpio, &r, &s))
72 return -EINVAL;
73
74 v = nv_rd32(dev, r) & ~(0x3 << s);
75 v |= (gpio->state[state] ^ 2) << s;
76 nv_wr32(dev, r, v);
77 return 0;
78}
Ben Skeggsd0875ed2010-07-23 11:31:08 +100079
80void
81nv50_gpio_irq_enable(struct drm_device *dev, enum dcb_gpio_tag tag, bool on)
82{
83 struct dcb_gpio_entry *gpio;
84 u32 reg, mask;
85
86 gpio = nouveau_bios_gpio_entry(dev, tag);
87 if (!gpio) {
88 NV_ERROR(dev, "gpio tag 0x%02x not found\n", tag);
89 return;
90 }
91
92 reg = gpio->line < 16 ? 0xe050 : 0xe070;
93 mask = 0x00010001 << (gpio->line & 0xf);
94
95 nv_wr32(dev, reg + 4, mask);
96 nv_mask(dev, reg + 0, mask, on ? mask : 0);
97}
Ben Skeggsee2e0132010-07-26 09:28:25 +100098
99int
100nv50_gpio_init(struct drm_device *dev)
101{
102 struct drm_nouveau_private *dev_priv = dev->dev_private;
103
104 /* disable, and ack any pending gpio interrupts */
105 nv_wr32(dev, 0xe050, 0x00000000);
106 nv_wr32(dev, 0xe054, 0xffffffff);
107 if (dev_priv->chipset >= 0x90) {
108 nv_wr32(dev, 0xe070, 0x00000000);
109 nv_wr32(dev, 0xe074, 0xffffffff);
110 }
111
Ben Skeggs2cbd4c82010-11-03 10:18:04 +1000112 nouveau_irq_register(dev, 21, nv50_gpio_isr);
Ben Skeggsee2e0132010-07-26 09:28:25 +1000113 return 0;
114}
Ben Skeggs2cbd4c82010-11-03 10:18:04 +1000115
116void
117nv50_gpio_fini(struct drm_device *dev)
118{
119 struct drm_nouveau_private *dev_priv = dev->dev_private;
120
121 nv_wr32(dev, 0xe050, 0x00000000);
122 if (dev_priv->chipset >= 0x90)
123 nv_wr32(dev, 0xe070, 0x00000000);
124 nouveau_irq_unregister(dev, 21);
125}
126
127static void
128nv50_gpio_isr(struct drm_device *dev)
129{
130 struct drm_nouveau_private *dev_priv = dev->dev_private;
131 uint32_t hpd0_bits, hpd1_bits = 0;
132
133 hpd0_bits = nv_rd32(dev, 0xe054);
134 nv_wr32(dev, 0xe054, hpd0_bits);
135
136 if (dev_priv->chipset >= 0x90) {
137 hpd1_bits = nv_rd32(dev, 0xe074);
138 nv_wr32(dev, 0xe074, hpd1_bits);
139 }
140
141 spin_lock(&dev_priv->hpd_state.lock);
142 dev_priv->hpd_state.hpd0_bits |= hpd0_bits;
143 dev_priv->hpd_state.hpd1_bits |= hpd1_bits;
144 spin_unlock(&dev_priv->hpd_state.lock);
145
146 queue_work(dev_priv->wq, &dev_priv->hpd_work);
147}