xref: /dflybsd-src/sys/dev/drm/amd/display/dc/gpio/hw_hpd.c (revision b843c749addef9340ee7d4e250b09fdd492602a1)
1*b843c749SSergey Zigachev /*
2*b843c749SSergey Zigachev  * Copyright 2012-15 Advanced Micro Devices, Inc.
3*b843c749SSergey Zigachev  *
4*b843c749SSergey Zigachev  * Permission is hereby granted, free of charge, to any person obtaining a
5*b843c749SSergey Zigachev  * copy of this software and associated documentation files (the "Software"),
6*b843c749SSergey Zigachev  * to deal in the Software without restriction, including without limitation
7*b843c749SSergey Zigachev  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8*b843c749SSergey Zigachev  * and/or sell copies of the Software, and to permit persons to whom the
9*b843c749SSergey Zigachev  * Software is furnished to do so, subject to the following conditions:
10*b843c749SSergey Zigachev  *
11*b843c749SSergey Zigachev  * The above copyright notice and this permission notice shall be included in
12*b843c749SSergey Zigachev  * all copies or substantial portions of the Software.
13*b843c749SSergey Zigachev  *
14*b843c749SSergey Zigachev  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15*b843c749SSergey Zigachev  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16*b843c749SSergey Zigachev  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17*b843c749SSergey Zigachev  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18*b843c749SSergey Zigachev  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19*b843c749SSergey Zigachev  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20*b843c749SSergey Zigachev  * OTHER DEALINGS IN THE SOFTWARE.
21*b843c749SSergey Zigachev  *
22*b843c749SSergey Zigachev  * Authors: AMD
23*b843c749SSergey Zigachev  *
24*b843c749SSergey Zigachev  */
25*b843c749SSergey Zigachev 
26*b843c749SSergey Zigachev #include "dm_services.h"
27*b843c749SSergey Zigachev 
28*b843c749SSergey Zigachev #include "include/gpio_types.h"
29*b843c749SSergey Zigachev #include "hw_gpio.h"
30*b843c749SSergey Zigachev #include "hw_hpd.h"
31*b843c749SSergey Zigachev 
32*b843c749SSergey Zigachev #include "reg_helper.h"
33*b843c749SSergey Zigachev #include "hpd_regs.h"
34*b843c749SSergey Zigachev 
35*b843c749SSergey Zigachev #undef FN
36*b843c749SSergey Zigachev #define FN(reg_name, field_name) \
37*b843c749SSergey Zigachev 	hpd->shifts->field_name, hpd->masks->field_name
38*b843c749SSergey Zigachev 
39*b843c749SSergey Zigachev #define CTX \
40*b843c749SSergey Zigachev 	hpd->base.base.ctx
41*b843c749SSergey Zigachev #define REG(reg)\
42*b843c749SSergey Zigachev 	(hpd->regs->reg)
43*b843c749SSergey Zigachev 
dal_hw_hpd_construct(struct hw_hpd * pin,enum gpio_id id,uint32_t en,struct dc_context * ctx)44*b843c749SSergey Zigachev static void dal_hw_hpd_construct(
45*b843c749SSergey Zigachev 	struct hw_hpd *pin,
46*b843c749SSergey Zigachev 	enum gpio_id id,
47*b843c749SSergey Zigachev 	uint32_t en,
48*b843c749SSergey Zigachev 	struct dc_context *ctx)
49*b843c749SSergey Zigachev {
50*b843c749SSergey Zigachev 	dal_hw_gpio_construct(&pin->base, id, en, ctx);
51*b843c749SSergey Zigachev }
52*b843c749SSergey Zigachev 
dal_hw_hpd_destruct(struct hw_hpd * pin)53*b843c749SSergey Zigachev static void dal_hw_hpd_destruct(
54*b843c749SSergey Zigachev 	struct hw_hpd *pin)
55*b843c749SSergey Zigachev {
56*b843c749SSergey Zigachev 	dal_hw_gpio_destruct(&pin->base);
57*b843c749SSergey Zigachev }
58*b843c749SSergey Zigachev 
59*b843c749SSergey Zigachev 
destruct(struct hw_hpd * hpd)60*b843c749SSergey Zigachev static void destruct(
61*b843c749SSergey Zigachev 	struct hw_hpd *hpd)
62*b843c749SSergey Zigachev {
63*b843c749SSergey Zigachev 	dal_hw_hpd_destruct(hpd);
64*b843c749SSergey Zigachev }
65*b843c749SSergey Zigachev 
destroy(struct hw_gpio_pin ** ptr)66*b843c749SSergey Zigachev static void destroy(
67*b843c749SSergey Zigachev 	struct hw_gpio_pin **ptr)
68*b843c749SSergey Zigachev {
69*b843c749SSergey Zigachev 	struct hw_hpd *hpd = HW_HPD_FROM_BASE(*ptr);
70*b843c749SSergey Zigachev 
71*b843c749SSergey Zigachev 	destruct(hpd);
72*b843c749SSergey Zigachev 
73*b843c749SSergey Zigachev 	kfree(hpd);
74*b843c749SSergey Zigachev 
75*b843c749SSergey Zigachev 	*ptr = NULL;
76*b843c749SSergey Zigachev }
77*b843c749SSergey Zigachev 
get_value(const struct hw_gpio_pin * ptr,uint32_t * value)78*b843c749SSergey Zigachev static enum gpio_result get_value(
79*b843c749SSergey Zigachev 	const struct hw_gpio_pin *ptr,
80*b843c749SSergey Zigachev 	uint32_t *value)
81*b843c749SSergey Zigachev {
82*b843c749SSergey Zigachev 	struct hw_hpd *hpd = HW_HPD_FROM_BASE(ptr);
83*b843c749SSergey Zigachev 	uint32_t hpd_delayed = 0;
84*b843c749SSergey Zigachev 
85*b843c749SSergey Zigachev 	/* in Interrupt mode we ask for SENSE bit */
86*b843c749SSergey Zigachev 
87*b843c749SSergey Zigachev 	if (ptr->mode == GPIO_MODE_INTERRUPT) {
88*b843c749SSergey Zigachev 
89*b843c749SSergey Zigachev 		REG_GET(int_status,
90*b843c749SSergey Zigachev 			DC_HPD_SENSE_DELAYED, &hpd_delayed);
91*b843c749SSergey Zigachev 
92*b843c749SSergey Zigachev 		*value = hpd_delayed;
93*b843c749SSergey Zigachev 		return GPIO_RESULT_OK;
94*b843c749SSergey Zigachev 	}
95*b843c749SSergey Zigachev 
96*b843c749SSergey Zigachev 	/* in any other modes, operate as normal GPIO */
97*b843c749SSergey Zigachev 
98*b843c749SSergey Zigachev 	return dal_hw_gpio_get_value(ptr, value);
99*b843c749SSergey Zigachev }
100*b843c749SSergey Zigachev 
set_config(struct hw_gpio_pin * ptr,const struct gpio_config_data * config_data)101*b843c749SSergey Zigachev static enum gpio_result set_config(
102*b843c749SSergey Zigachev 	struct hw_gpio_pin *ptr,
103*b843c749SSergey Zigachev 	const struct gpio_config_data *config_data)
104*b843c749SSergey Zigachev {
105*b843c749SSergey Zigachev 	struct hw_hpd *hpd = HW_HPD_FROM_BASE(ptr);
106*b843c749SSergey Zigachev 
107*b843c749SSergey Zigachev 	if (!config_data)
108*b843c749SSergey Zigachev 		return GPIO_RESULT_INVALID_DATA;
109*b843c749SSergey Zigachev 
110*b843c749SSergey Zigachev 	REG_UPDATE_2(toggle_filt_cntl,
111*b843c749SSergey Zigachev 		DC_HPD_CONNECT_INT_DELAY, config_data->config.hpd.delay_on_connect / 10,
112*b843c749SSergey Zigachev 		DC_HPD_DISCONNECT_INT_DELAY, config_data->config.hpd.delay_on_disconnect / 10);
113*b843c749SSergey Zigachev 
114*b843c749SSergey Zigachev 	return GPIO_RESULT_OK;
115*b843c749SSergey Zigachev }
116*b843c749SSergey Zigachev 
117*b843c749SSergey Zigachev static const struct hw_gpio_pin_funcs funcs = {
118*b843c749SSergey Zigachev 	.destroy = destroy,
119*b843c749SSergey Zigachev 	.open = dal_hw_gpio_open,
120*b843c749SSergey Zigachev 	.get_value = get_value,
121*b843c749SSergey Zigachev 	.set_value = dal_hw_gpio_set_value,
122*b843c749SSergey Zigachev 	.set_config = set_config,
123*b843c749SSergey Zigachev 	.change_mode = dal_hw_gpio_change_mode,
124*b843c749SSergey Zigachev 	.close = dal_hw_gpio_close,
125*b843c749SSergey Zigachev };
126*b843c749SSergey Zigachev 
construct(struct hw_hpd * hpd,enum gpio_id id,uint32_t en,struct dc_context * ctx)127*b843c749SSergey Zigachev static void construct(
128*b843c749SSergey Zigachev 	struct hw_hpd *hpd,
129*b843c749SSergey Zigachev 	enum gpio_id id,
130*b843c749SSergey Zigachev 	uint32_t en,
131*b843c749SSergey Zigachev 	struct dc_context *ctx)
132*b843c749SSergey Zigachev {
133*b843c749SSergey Zigachev 	dal_hw_hpd_construct(hpd, id, en, ctx);
134*b843c749SSergey Zigachev 	hpd->base.base.funcs = &funcs;
135*b843c749SSergey Zigachev }
136*b843c749SSergey Zigachev 
dal_hw_hpd_create(struct dc_context * ctx,enum gpio_id id,uint32_t en)137*b843c749SSergey Zigachev struct hw_gpio_pin *dal_hw_hpd_create(
138*b843c749SSergey Zigachev 	struct dc_context *ctx,
139*b843c749SSergey Zigachev 	enum gpio_id id,
140*b843c749SSergey Zigachev 	uint32_t en)
141*b843c749SSergey Zigachev {
142*b843c749SSergey Zigachev 	struct hw_hpd *hpd;
143*b843c749SSergey Zigachev 
144*b843c749SSergey Zigachev 	if (id != GPIO_ID_HPD) {
145*b843c749SSergey Zigachev 		ASSERT_CRITICAL(false);
146*b843c749SSergey Zigachev 		return NULL;
147*b843c749SSergey Zigachev 	}
148*b843c749SSergey Zigachev 
149*b843c749SSergey Zigachev 	if ((en < GPIO_HPD_MIN) || (en > GPIO_HPD_MAX)) {
150*b843c749SSergey Zigachev 		ASSERT_CRITICAL(false);
151*b843c749SSergey Zigachev 		return NULL;
152*b843c749SSergey Zigachev 	}
153*b843c749SSergey Zigachev 
154*b843c749SSergey Zigachev 	hpd = kzalloc(sizeof(struct hw_hpd), GFP_KERNEL);
155*b843c749SSergey Zigachev 	if (!hpd) {
156*b843c749SSergey Zigachev 		ASSERT_CRITICAL(false);
157*b843c749SSergey Zigachev 		return NULL;
158*b843c749SSergey Zigachev 	}
159*b843c749SSergey Zigachev 
160*b843c749SSergey Zigachev 	construct(hpd, id, en, ctx);
161*b843c749SSergey Zigachev 	return &hpd->base.base;
162*b843c749SSergey Zigachev }
163