xref: /openbsd/sys/dev/pci/drm/amd/display/dc/gpio/hw_gpio.c (revision fb4d8502)
1*fb4d8502Sjsg /*
2*fb4d8502Sjsg  * Copyright 2012-15 Advanced Micro Devices, Inc.
3*fb4d8502Sjsg  *
4*fb4d8502Sjsg  * Permission is hereby granted, free of charge, to any person obtaining a
5*fb4d8502Sjsg  * copy of this software and associated documentation files (the "Software"),
6*fb4d8502Sjsg  * to deal in the Software without restriction, including without limitation
7*fb4d8502Sjsg  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8*fb4d8502Sjsg  * and/or sell copies of the Software, and to permit persons to whom the
9*fb4d8502Sjsg  * Software is furnished to do so, subject to the following conditions:
10*fb4d8502Sjsg  *
11*fb4d8502Sjsg  * The above copyright notice and this permission notice shall be included in
12*fb4d8502Sjsg  * all copies or substantial portions of the Software.
13*fb4d8502Sjsg  *
14*fb4d8502Sjsg  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15*fb4d8502Sjsg  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16*fb4d8502Sjsg  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17*fb4d8502Sjsg  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18*fb4d8502Sjsg  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19*fb4d8502Sjsg  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20*fb4d8502Sjsg  * OTHER DEALINGS IN THE SOFTWARE.
21*fb4d8502Sjsg  *
22*fb4d8502Sjsg  * Authors: AMD
23*fb4d8502Sjsg  *
24*fb4d8502Sjsg  */
25*fb4d8502Sjsg 
26*fb4d8502Sjsg #include "dm_services.h"
27*fb4d8502Sjsg #include "include/gpio_types.h"
28*fb4d8502Sjsg #include "hw_gpio.h"
29*fb4d8502Sjsg 
30*fb4d8502Sjsg #include "reg_helper.h"
31*fb4d8502Sjsg #include "gpio_regs.h"
32*fb4d8502Sjsg 
33*fb4d8502Sjsg #undef FN
34*fb4d8502Sjsg #define FN(reg_name, field_name) \
35*fb4d8502Sjsg 	gpio->regs->field_name ## _shift, gpio->regs->field_name ## _mask
36*fb4d8502Sjsg 
37*fb4d8502Sjsg #define CTX \
38*fb4d8502Sjsg 	gpio->base.ctx
39*fb4d8502Sjsg #define REG(reg)\
40*fb4d8502Sjsg 	(gpio->regs->reg)
41*fb4d8502Sjsg 
store_registers(struct hw_gpio * gpio)42*fb4d8502Sjsg static void store_registers(
43*fb4d8502Sjsg 	struct hw_gpio *gpio)
44*fb4d8502Sjsg {
45*fb4d8502Sjsg 	REG_GET(MASK_reg, MASK, &gpio->store.mask);
46*fb4d8502Sjsg 	REG_GET(A_reg, A, &gpio->store.a);
47*fb4d8502Sjsg 	REG_GET(EN_reg, EN, &gpio->store.en);
48*fb4d8502Sjsg 	/* TODO store GPIO_MUX_CONTROL if we ever use it */
49*fb4d8502Sjsg }
50*fb4d8502Sjsg 
restore_registers(struct hw_gpio * gpio)51*fb4d8502Sjsg static void restore_registers(
52*fb4d8502Sjsg 	struct hw_gpio *gpio)
53*fb4d8502Sjsg {
54*fb4d8502Sjsg 	REG_UPDATE(MASK_reg, MASK, gpio->store.mask);
55*fb4d8502Sjsg 	REG_UPDATE(A_reg, A, gpio->store.a);
56*fb4d8502Sjsg 	REG_UPDATE(EN_reg, EN, gpio->store.en);
57*fb4d8502Sjsg 	/* TODO restore GPIO_MUX_CONTROL if we ever use it */
58*fb4d8502Sjsg }
59*fb4d8502Sjsg 
dal_hw_gpio_open(struct hw_gpio_pin * ptr,enum gpio_mode mode)60*fb4d8502Sjsg bool dal_hw_gpio_open(
61*fb4d8502Sjsg 	struct hw_gpio_pin *ptr,
62*fb4d8502Sjsg 	enum gpio_mode mode)
63*fb4d8502Sjsg {
64*fb4d8502Sjsg 	struct hw_gpio *pin = FROM_HW_GPIO_PIN(ptr);
65*fb4d8502Sjsg 
66*fb4d8502Sjsg 	store_registers(pin);
67*fb4d8502Sjsg 
68*fb4d8502Sjsg 	ptr->opened = (dal_hw_gpio_config_mode(pin, mode) == GPIO_RESULT_OK);
69*fb4d8502Sjsg 
70*fb4d8502Sjsg 	return ptr->opened;
71*fb4d8502Sjsg }
72*fb4d8502Sjsg 
dal_hw_gpio_get_value(const struct hw_gpio_pin * ptr,uint32_t * value)73*fb4d8502Sjsg enum gpio_result dal_hw_gpio_get_value(
74*fb4d8502Sjsg 	const struct hw_gpio_pin *ptr,
75*fb4d8502Sjsg 	uint32_t *value)
76*fb4d8502Sjsg {
77*fb4d8502Sjsg 	const struct hw_gpio *gpio = FROM_HW_GPIO_PIN(ptr);
78*fb4d8502Sjsg 
79*fb4d8502Sjsg 	enum gpio_result result = GPIO_RESULT_OK;
80*fb4d8502Sjsg 
81*fb4d8502Sjsg 	switch (ptr->mode) {
82*fb4d8502Sjsg 	case GPIO_MODE_INPUT:
83*fb4d8502Sjsg 	case GPIO_MODE_OUTPUT:
84*fb4d8502Sjsg 	case GPIO_MODE_HARDWARE:
85*fb4d8502Sjsg 	case GPIO_MODE_FAST_OUTPUT:
86*fb4d8502Sjsg 		REG_GET(Y_reg, Y, value);
87*fb4d8502Sjsg 		break;
88*fb4d8502Sjsg 	default:
89*fb4d8502Sjsg 		result = GPIO_RESULT_NON_SPECIFIC_ERROR;
90*fb4d8502Sjsg 	}
91*fb4d8502Sjsg 
92*fb4d8502Sjsg 	return result;
93*fb4d8502Sjsg }
94*fb4d8502Sjsg 
dal_hw_gpio_set_value(const struct hw_gpio_pin * ptr,uint32_t value)95*fb4d8502Sjsg enum gpio_result dal_hw_gpio_set_value(
96*fb4d8502Sjsg 	const struct hw_gpio_pin *ptr,
97*fb4d8502Sjsg 	uint32_t value)
98*fb4d8502Sjsg {
99*fb4d8502Sjsg 	struct hw_gpio *gpio = FROM_HW_GPIO_PIN(ptr);
100*fb4d8502Sjsg 
101*fb4d8502Sjsg 	/* This is the public interface
102*fb4d8502Sjsg 	 * where the input comes from client, not shifted yet
103*fb4d8502Sjsg 	 * (because client does not know the shifts). */
104*fb4d8502Sjsg 
105*fb4d8502Sjsg 	switch (ptr->mode) {
106*fb4d8502Sjsg 	case GPIO_MODE_OUTPUT:
107*fb4d8502Sjsg 		REG_UPDATE(A_reg, A, value);
108*fb4d8502Sjsg 		return GPIO_RESULT_OK;
109*fb4d8502Sjsg 	case GPIO_MODE_FAST_OUTPUT:
110*fb4d8502Sjsg 		/* We use (EN) to faster switch (used in DDC GPIO).
111*fb4d8502Sjsg 		 * So (A) is grounded, output is driven by (EN = 0)
112*fb4d8502Sjsg 		 * to pull the line down (output == 0) and (EN=1)
113*fb4d8502Sjsg 		 * then output is tri-state */
114*fb4d8502Sjsg 		REG_UPDATE(EN_reg, EN, ~value);
115*fb4d8502Sjsg 		return GPIO_RESULT_OK;
116*fb4d8502Sjsg 	default:
117*fb4d8502Sjsg 		return GPIO_RESULT_NON_SPECIFIC_ERROR;
118*fb4d8502Sjsg 	}
119*fb4d8502Sjsg }
120*fb4d8502Sjsg 
dal_hw_gpio_change_mode(struct hw_gpio_pin * ptr,enum gpio_mode mode)121*fb4d8502Sjsg enum gpio_result dal_hw_gpio_change_mode(
122*fb4d8502Sjsg 	struct hw_gpio_pin *ptr,
123*fb4d8502Sjsg 	enum gpio_mode mode)
124*fb4d8502Sjsg {
125*fb4d8502Sjsg 	struct hw_gpio *pin = FROM_HW_GPIO_PIN(ptr);
126*fb4d8502Sjsg 
127*fb4d8502Sjsg 	return dal_hw_gpio_config_mode(pin, mode);
128*fb4d8502Sjsg }
129*fb4d8502Sjsg 
dal_hw_gpio_close(struct hw_gpio_pin * ptr)130*fb4d8502Sjsg void dal_hw_gpio_close(
131*fb4d8502Sjsg 	struct hw_gpio_pin *ptr)
132*fb4d8502Sjsg {
133*fb4d8502Sjsg 	struct hw_gpio *pin = FROM_HW_GPIO_PIN(ptr);
134*fb4d8502Sjsg 
135*fb4d8502Sjsg 	restore_registers(pin);
136*fb4d8502Sjsg 
137*fb4d8502Sjsg 	ptr->mode = GPIO_MODE_UNKNOWN;
138*fb4d8502Sjsg 	ptr->opened = false;
139*fb4d8502Sjsg }
140*fb4d8502Sjsg 
dal_hw_gpio_config_mode(struct hw_gpio * gpio,enum gpio_mode mode)141*fb4d8502Sjsg enum gpio_result dal_hw_gpio_config_mode(
142*fb4d8502Sjsg 	struct hw_gpio *gpio,
143*fb4d8502Sjsg 	enum gpio_mode mode)
144*fb4d8502Sjsg {
145*fb4d8502Sjsg 	gpio->base.mode = mode;
146*fb4d8502Sjsg 
147*fb4d8502Sjsg 	switch (mode) {
148*fb4d8502Sjsg 	case GPIO_MODE_INPUT:
149*fb4d8502Sjsg 		/* turn off output enable, act as input pin;
150*fb4d8502Sjsg 		 * program the pin as GPIO, mask out signal driven by HW */
151*fb4d8502Sjsg 		REG_UPDATE(EN_reg, EN, 0);
152*fb4d8502Sjsg 		REG_UPDATE(MASK_reg, MASK, 1);
153*fb4d8502Sjsg 		return GPIO_RESULT_OK;
154*fb4d8502Sjsg 	case GPIO_MODE_OUTPUT:
155*fb4d8502Sjsg 		/* turn on output enable, act as output pin;
156*fb4d8502Sjsg 		 * program the pin as GPIO, mask out signal driven by HW */
157*fb4d8502Sjsg 		REG_UPDATE(A_reg, A, 0);
158*fb4d8502Sjsg 		REG_UPDATE(MASK_reg, MASK, 1);
159*fb4d8502Sjsg 		return GPIO_RESULT_OK;
160*fb4d8502Sjsg 	case GPIO_MODE_FAST_OUTPUT:
161*fb4d8502Sjsg 		/* grounding the A register then use the EN register bit
162*fb4d8502Sjsg 		 * will have faster effect on the rise time */
163*fb4d8502Sjsg 		REG_UPDATE(A_reg, A, 0);
164*fb4d8502Sjsg 		REG_UPDATE(MASK_reg, MASK, 1);
165*fb4d8502Sjsg 		return GPIO_RESULT_OK;
166*fb4d8502Sjsg 	case GPIO_MODE_HARDWARE:
167*fb4d8502Sjsg 		/* program the pin as tri-state, pin is driven by HW */
168*fb4d8502Sjsg 		REG_UPDATE(MASK_reg, MASK, 0);
169*fb4d8502Sjsg 		return GPIO_RESULT_OK;
170*fb4d8502Sjsg 	case GPIO_MODE_INTERRUPT:
171*fb4d8502Sjsg 		/* Interrupt mode supported only by HPD (IrqGpio) pins. */
172*fb4d8502Sjsg 		REG_UPDATE(MASK_reg, MASK, 0);
173*fb4d8502Sjsg 		return GPIO_RESULT_OK;
174*fb4d8502Sjsg 	default:
175*fb4d8502Sjsg 		return GPIO_RESULT_NON_SPECIFIC_ERROR;
176*fb4d8502Sjsg 	}
177*fb4d8502Sjsg }
178*fb4d8502Sjsg 
dal_hw_gpio_construct(struct hw_gpio * pin,enum gpio_id id,uint32_t en,struct dc_context * ctx)179*fb4d8502Sjsg void dal_hw_gpio_construct(
180*fb4d8502Sjsg 	struct hw_gpio *pin,
181*fb4d8502Sjsg 	enum gpio_id id,
182*fb4d8502Sjsg 	uint32_t en,
183*fb4d8502Sjsg 	struct dc_context *ctx)
184*fb4d8502Sjsg {
185*fb4d8502Sjsg 	pin->base.ctx = ctx;
186*fb4d8502Sjsg 	pin->base.id = id;
187*fb4d8502Sjsg 	pin->base.en = en;
188*fb4d8502Sjsg 	pin->base.mode = GPIO_MODE_UNKNOWN;
189*fb4d8502Sjsg 	pin->base.opened = false;
190*fb4d8502Sjsg 
191*fb4d8502Sjsg 	pin->store.mask = 0;
192*fb4d8502Sjsg 	pin->store.a = 0;
193*fb4d8502Sjsg 	pin->store.en = 0;
194*fb4d8502Sjsg 	pin->store.mux = 0;
195*fb4d8502Sjsg 
196*fb4d8502Sjsg 	pin->mux_supported = false;
197*fb4d8502Sjsg }
198*fb4d8502Sjsg 
dal_hw_gpio_destruct(struct hw_gpio * pin)199*fb4d8502Sjsg void dal_hw_gpio_destruct(
200*fb4d8502Sjsg 	struct hw_gpio *pin)
201*fb4d8502Sjsg {
202*fb4d8502Sjsg 	ASSERT(!pin->base.opened);
203*fb4d8502Sjsg }
204