2 * Copyright 2012-15 Advanced Micro Devices, Inc.
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:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
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.
26 #include "dm_services.h"
27 #include "include/gpio_types.h"
30 #include "reg_helper.h"
31 #include "gpio_regs.h"
34 #define FN(reg_name, field_name) \
35 gpio->regs->field_name ## _shift, gpio->regs->field_name ## _mask
42 static void store_registers(
45 REG_GET(MASK_reg
, MASK
, &gpio
->store
.mask
);
46 REG_GET(A_reg
, A
, &gpio
->store
.a
);
47 REG_GET(EN_reg
, EN
, &gpio
->store
.en
);
48 /* TODO store GPIO_MUX_CONTROL if we ever use it */
51 static void restore_registers(
54 REG_UPDATE(MASK_reg
, MASK
, gpio
->store
.mask
);
55 REG_UPDATE(A_reg
, A
, gpio
->store
.a
);
56 REG_UPDATE(EN_reg
, EN
, gpio
->store
.en
);
57 /* TODO restore GPIO_MUX_CONTROL if we ever use it */
60 bool dal_hw_gpio_open(
61 struct hw_gpio_pin
*ptr
,
64 struct hw_gpio
*pin
= FROM_HW_GPIO_PIN(ptr
);
68 ptr
->opened
= (dal_hw_gpio_config_mode(pin
, mode
) == GPIO_RESULT_OK
);
73 enum gpio_result
dal_hw_gpio_get_value(
74 const struct hw_gpio_pin
*ptr
,
77 const struct hw_gpio
*gpio
= FROM_HW_GPIO_PIN(ptr
);
79 enum gpio_result result
= GPIO_RESULT_OK
;
83 case GPIO_MODE_OUTPUT
:
84 case GPIO_MODE_HARDWARE
:
85 case GPIO_MODE_FAST_OUTPUT
:
86 REG_GET(Y_reg
, Y
, value
);
89 result
= GPIO_RESULT_NON_SPECIFIC_ERROR
;
95 enum gpio_result
dal_hw_gpio_set_value(
96 const struct hw_gpio_pin
*ptr
,
99 struct hw_gpio
*gpio
= FROM_HW_GPIO_PIN(ptr
);
101 /* This is the public interface
102 * where the input comes from client, not shifted yet
103 * (because client does not know the shifts). */
106 case GPIO_MODE_OUTPUT
:
107 REG_UPDATE(A_reg
, A
, value
);
108 return GPIO_RESULT_OK
;
109 case GPIO_MODE_FAST_OUTPUT
:
110 /* We use (EN) to faster switch (used in DDC GPIO).
111 * So (A) is grounded, output is driven by (EN = 0)
112 * to pull the line down (output == 0) and (EN=1)
113 * then output is tri-state */
114 REG_UPDATE(EN_reg
, EN
, ~value
);
115 return GPIO_RESULT_OK
;
117 return GPIO_RESULT_NON_SPECIFIC_ERROR
;
121 enum gpio_result
dal_hw_gpio_change_mode(
122 struct hw_gpio_pin
*ptr
,
125 struct hw_gpio
*pin
= FROM_HW_GPIO_PIN(ptr
);
127 return dal_hw_gpio_config_mode(pin
, mode
);
130 void dal_hw_gpio_close(
131 struct hw_gpio_pin
*ptr
)
133 struct hw_gpio
*pin
= FROM_HW_GPIO_PIN(ptr
);
135 restore_registers(pin
);
137 ptr
->mode
= GPIO_MODE_UNKNOWN
;
141 enum gpio_result
dal_hw_gpio_config_mode(
142 struct hw_gpio
*gpio
,
145 gpio
->base
.mode
= mode
;
148 case GPIO_MODE_INPUT
:
149 /* turn off output enable, act as input pin;
150 * program the pin as GPIO, mask out signal driven by HW */
151 REG_UPDATE(EN_reg
, EN
, 0);
152 REG_UPDATE(MASK_reg
, MASK
, 1);
153 return GPIO_RESULT_OK
;
154 case GPIO_MODE_OUTPUT
:
155 /* turn on output enable, act as output pin;
156 * program the pin as GPIO, mask out signal driven by HW */
157 REG_UPDATE(A_reg
, A
, 0);
158 REG_UPDATE(MASK_reg
, MASK
, 1);
159 return GPIO_RESULT_OK
;
160 case GPIO_MODE_FAST_OUTPUT
:
161 /* grounding the A register then use the EN register bit
162 * will have faster effect on the rise time */
163 REG_UPDATE(A_reg
, A
, 0);
164 REG_UPDATE(MASK_reg
, MASK
, 1);
165 return GPIO_RESULT_OK
;
166 case GPIO_MODE_HARDWARE
:
167 /* program the pin as tri-state, pin is driven by HW */
168 REG_UPDATE(MASK_reg
, MASK
, 0);
169 return GPIO_RESULT_OK
;
170 case GPIO_MODE_INTERRUPT
:
171 /* Interrupt mode supported only by HPD (IrqGpio) pins. */
172 REG_UPDATE(MASK_reg
, MASK
, 0);
173 return GPIO_RESULT_OK
;
175 return GPIO_RESULT_NON_SPECIFIC_ERROR
;
179 void dal_hw_gpio_construct(
183 struct dc_context
*ctx
)
188 pin
->base
.mode
= GPIO_MODE_UNKNOWN
;
189 pin
->base
.opened
= false;
196 pin
->mux_supported
= false;
199 void dal_hw_gpio_destruct(
202 ASSERT(!pin
->base
.opened
);