xref: /dflybsd-src/sys/dev/drm/amd/display/dc/gpio/hw_ddc.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_ddc.h"
31*b843c749SSergey Zigachev 
32*b843c749SSergey Zigachev #include "reg_helper.h"
33*b843c749SSergey Zigachev #include "gpio_regs.h"
34*b843c749SSergey Zigachev 
35*b843c749SSergey Zigachev 
36*b843c749SSergey Zigachev #undef FN
37*b843c749SSergey Zigachev #define FN(reg_name, field_name) \
38*b843c749SSergey Zigachev 	ddc->shifts->field_name, ddc->masks->field_name
39*b843c749SSergey Zigachev 
40*b843c749SSergey Zigachev #define CTX \
41*b843c749SSergey Zigachev 	ddc->base.base.ctx
42*b843c749SSergey Zigachev #define REG(reg)\
43*b843c749SSergey Zigachev 	(ddc->regs->reg)
44*b843c749SSergey Zigachev 
destruct(struct hw_ddc * pin)45*b843c749SSergey Zigachev static void destruct(
46*b843c749SSergey Zigachev 	struct hw_ddc *pin)
47*b843c749SSergey Zigachev {
48*b843c749SSergey Zigachev 	dal_hw_gpio_destruct(&pin->base);
49*b843c749SSergey Zigachev }
50*b843c749SSergey Zigachev 
destroy(struct hw_gpio_pin ** ptr)51*b843c749SSergey Zigachev static void destroy(
52*b843c749SSergey Zigachev 	struct hw_gpio_pin **ptr)
53*b843c749SSergey Zigachev {
54*b843c749SSergey Zigachev 	struct hw_ddc *pin = HW_DDC_FROM_BASE(*ptr);
55*b843c749SSergey Zigachev 
56*b843c749SSergey Zigachev 	destruct(pin);
57*b843c749SSergey Zigachev 
58*b843c749SSergey Zigachev 	kfree(pin);
59*b843c749SSergey Zigachev 
60*b843c749SSergey Zigachev 	*ptr = NULL;
61*b843c749SSergey Zigachev }
62*b843c749SSergey Zigachev 
set_config(struct hw_gpio_pin * ptr,const struct gpio_config_data * config_data)63*b843c749SSergey Zigachev static enum gpio_result set_config(
64*b843c749SSergey Zigachev 	struct hw_gpio_pin *ptr,
65*b843c749SSergey Zigachev 	const struct gpio_config_data *config_data)
66*b843c749SSergey Zigachev {
67*b843c749SSergey Zigachev 	struct hw_ddc *ddc = HW_DDC_FROM_BASE(ptr);
68*b843c749SSergey Zigachev 	struct hw_gpio *hw_gpio = NULL;
69*b843c749SSergey Zigachev 	uint32_t regval;
70*b843c749SSergey Zigachev 	uint32_t ddc_data_pd_en = 0;
71*b843c749SSergey Zigachev 	uint32_t ddc_clk_pd_en = 0;
72*b843c749SSergey Zigachev 	uint32_t aux_pad_mode = 0;
73*b843c749SSergey Zigachev 
74*b843c749SSergey Zigachev 	hw_gpio = &ddc->base;
75*b843c749SSergey Zigachev 
76*b843c749SSergey Zigachev 	if (hw_gpio == NULL) {
77*b843c749SSergey Zigachev 		ASSERT_CRITICAL(false);
78*b843c749SSergey Zigachev 		return GPIO_RESULT_NULL_HANDLE;
79*b843c749SSergey Zigachev 	}
80*b843c749SSergey Zigachev 
81*b843c749SSergey Zigachev 	regval = REG_GET_3(gpio.MASK_reg,
82*b843c749SSergey Zigachev 			DC_GPIO_DDC1DATA_PD_EN, &ddc_data_pd_en,
83*b843c749SSergey Zigachev 			DC_GPIO_DDC1CLK_PD_EN, &ddc_clk_pd_en,
84*b843c749SSergey Zigachev 			AUX_PAD1_MODE, &aux_pad_mode);
85*b843c749SSergey Zigachev 
86*b843c749SSergey Zigachev 	switch (config_data->config.ddc.type) {
87*b843c749SSergey Zigachev 	case GPIO_DDC_CONFIG_TYPE_MODE_I2C:
88*b843c749SSergey Zigachev 		/* On plug-in, there is a transient level on the pad
89*b843c749SSergey Zigachev 		 * which must be discharged through the internal pull-down.
90*b843c749SSergey Zigachev 		 * Enable internal pull-down, 2.5msec discharge time
91*b843c749SSergey Zigachev 		 * is required for detection of AUX mode */
92*b843c749SSergey Zigachev 		if (hw_gpio->base.en != GPIO_DDC_LINE_VIP_PAD) {
93*b843c749SSergey Zigachev 			if (!ddc_data_pd_en || !ddc_clk_pd_en) {
94*b843c749SSergey Zigachev 
95*b843c749SSergey Zigachev 				REG_SET_2(gpio.MASK_reg, regval,
96*b843c749SSergey Zigachev 						DC_GPIO_DDC1DATA_PD_EN, 1,
97*b843c749SSergey Zigachev 						DC_GPIO_DDC1CLK_PD_EN, 1);
98*b843c749SSergey Zigachev 
99*b843c749SSergey Zigachev 				if (config_data->type ==
100*b843c749SSergey Zigachev 						GPIO_CONFIG_TYPE_I2C_AUX_DUAL_MODE)
101*b843c749SSergey Zigachev 					msleep(3);
102*b843c749SSergey Zigachev 			}
103*b843c749SSergey Zigachev 		} else {
104*b843c749SSergey Zigachev 			uint32_t reg2;
105*b843c749SSergey Zigachev 			uint32_t sda_pd_dis = 0;
106*b843c749SSergey Zigachev 			uint32_t scl_pd_dis = 0;
107*b843c749SSergey Zigachev 
108*b843c749SSergey Zigachev 			reg2 = REG_GET_2(gpio.MASK_reg,
109*b843c749SSergey Zigachev 					DC_GPIO_SDA_PD_DIS, &sda_pd_dis,
110*b843c749SSergey Zigachev 					DC_GPIO_SCL_PD_DIS, &scl_pd_dis);
111*b843c749SSergey Zigachev 
112*b843c749SSergey Zigachev 			if (sda_pd_dis) {
113*b843c749SSergey Zigachev 				REG_SET(gpio.MASK_reg, regval,
114*b843c749SSergey Zigachev 						DC_GPIO_SDA_PD_DIS, 0);
115*b843c749SSergey Zigachev 
116*b843c749SSergey Zigachev 				if (config_data->type ==
117*b843c749SSergey Zigachev 						GPIO_CONFIG_TYPE_I2C_AUX_DUAL_MODE)
118*b843c749SSergey Zigachev 					msleep(3);
119*b843c749SSergey Zigachev 			}
120*b843c749SSergey Zigachev 
121*b843c749SSergey Zigachev 			if (!scl_pd_dis) {
122*b843c749SSergey Zigachev 				REG_SET(gpio.MASK_reg, regval,
123*b843c749SSergey Zigachev 						DC_GPIO_SCL_PD_DIS, 1);
124*b843c749SSergey Zigachev 
125*b843c749SSergey Zigachev 				if (config_data->type ==
126*b843c749SSergey Zigachev 						GPIO_CONFIG_TYPE_I2C_AUX_DUAL_MODE)
127*b843c749SSergey Zigachev 					msleep(3);
128*b843c749SSergey Zigachev 			}
129*b843c749SSergey Zigachev 		}
130*b843c749SSergey Zigachev 
131*b843c749SSergey Zigachev 		if (aux_pad_mode) {
132*b843c749SSergey Zigachev 			/* let pins to get de-asserted
133*b843c749SSergey Zigachev 			 * before setting pad to I2C mode */
134*b843c749SSergey Zigachev 			if (config_data->config.ddc.data_en_bit_present ||
135*b843c749SSergey Zigachev 				config_data->config.ddc.clock_en_bit_present)
136*b843c749SSergey Zigachev 				/* [anaumov] in DAL2, there was
137*b843c749SSergey Zigachev 				 * dc_service_delay_in_microseconds(2000); */
138*b843c749SSergey Zigachev 				msleep(2);
139*b843c749SSergey Zigachev 
140*b843c749SSergey Zigachev 			/* set the I2C pad mode */
141*b843c749SSergey Zigachev 			/* read the register again,
142*b843c749SSergey Zigachev 			 * some bits may have been changed */
143*b843c749SSergey Zigachev 			REG_UPDATE(gpio.MASK_reg,
144*b843c749SSergey Zigachev 					AUX_PAD1_MODE, 0);
145*b843c749SSergey Zigachev 		}
146*b843c749SSergey Zigachev 
147*b843c749SSergey Zigachev 		return GPIO_RESULT_OK;
148*b843c749SSergey Zigachev 	case GPIO_DDC_CONFIG_TYPE_MODE_AUX:
149*b843c749SSergey Zigachev 		/* set the AUX pad mode */
150*b843c749SSergey Zigachev 		if (!aux_pad_mode) {
151*b843c749SSergey Zigachev 			REG_SET(gpio.MASK_reg, regval,
152*b843c749SSergey Zigachev 					AUX_PAD1_MODE, 1);
153*b843c749SSergey Zigachev 		}
154*b843c749SSergey Zigachev 
155*b843c749SSergey Zigachev 		return GPIO_RESULT_OK;
156*b843c749SSergey Zigachev 	case GPIO_DDC_CONFIG_TYPE_POLL_FOR_CONNECT:
157*b843c749SSergey Zigachev 		if ((hw_gpio->base.en >= GPIO_DDC_LINE_DDC1) &&
158*b843c749SSergey Zigachev 			(hw_gpio->base.en <= GPIO_DDC_LINE_DDC_VGA)) {
159*b843c749SSergey Zigachev 			REG_UPDATE_3(ddc_setup,
160*b843c749SSergey Zigachev 				DC_I2C_DDC1_ENABLE, 1,
161*b843c749SSergey Zigachev 				DC_I2C_DDC1_EDID_DETECT_ENABLE, 1,
162*b843c749SSergey Zigachev 				DC_I2C_DDC1_EDID_DETECT_MODE, 0);
163*b843c749SSergey Zigachev 			return GPIO_RESULT_OK;
164*b843c749SSergey Zigachev 		}
165*b843c749SSergey Zigachev 	break;
166*b843c749SSergey Zigachev 	case GPIO_DDC_CONFIG_TYPE_POLL_FOR_DISCONNECT:
167*b843c749SSergey Zigachev 		if ((hw_gpio->base.en >= GPIO_DDC_LINE_DDC1) &&
168*b843c749SSergey Zigachev 			(hw_gpio->base.en <= GPIO_DDC_LINE_DDC_VGA)) {
169*b843c749SSergey Zigachev 			REG_UPDATE_3(ddc_setup,
170*b843c749SSergey Zigachev 				DC_I2C_DDC1_ENABLE, 1,
171*b843c749SSergey Zigachev 				DC_I2C_DDC1_EDID_DETECT_ENABLE, 1,
172*b843c749SSergey Zigachev 				DC_I2C_DDC1_EDID_DETECT_MODE, 1);
173*b843c749SSergey Zigachev 			return GPIO_RESULT_OK;
174*b843c749SSergey Zigachev 		}
175*b843c749SSergey Zigachev 	break;
176*b843c749SSergey Zigachev 	case GPIO_DDC_CONFIG_TYPE_DISABLE_POLLING:
177*b843c749SSergey Zigachev 		if ((hw_gpio->base.en >= GPIO_DDC_LINE_DDC1) &&
178*b843c749SSergey Zigachev 			(hw_gpio->base.en <= GPIO_DDC_LINE_DDC_VGA)) {
179*b843c749SSergey Zigachev 			REG_UPDATE_2(ddc_setup,
180*b843c749SSergey Zigachev 				DC_I2C_DDC1_ENABLE, 0,
181*b843c749SSergey Zigachev 				DC_I2C_DDC1_EDID_DETECT_ENABLE, 0);
182*b843c749SSergey Zigachev 			return GPIO_RESULT_OK;
183*b843c749SSergey Zigachev 		}
184*b843c749SSergey Zigachev 	break;
185*b843c749SSergey Zigachev 	}
186*b843c749SSergey Zigachev 
187*b843c749SSergey Zigachev 	BREAK_TO_DEBUGGER();
188*b843c749SSergey Zigachev 
189*b843c749SSergey Zigachev 	return GPIO_RESULT_NON_SPECIFIC_ERROR;
190*b843c749SSergey Zigachev }
191*b843c749SSergey Zigachev 
192*b843c749SSergey Zigachev static const struct hw_gpio_pin_funcs funcs = {
193*b843c749SSergey Zigachev 	.destroy = destroy,
194*b843c749SSergey Zigachev 	.open = dal_hw_gpio_open,
195*b843c749SSergey Zigachev 	.get_value = dal_hw_gpio_get_value,
196*b843c749SSergey Zigachev 	.set_value = dal_hw_gpio_set_value,
197*b843c749SSergey Zigachev 	.set_config = set_config,
198*b843c749SSergey Zigachev 	.change_mode = dal_hw_gpio_change_mode,
199*b843c749SSergey Zigachev 	.close = dal_hw_gpio_close,
200*b843c749SSergey Zigachev };
201*b843c749SSergey Zigachev 
construct(struct hw_ddc * ddc,enum gpio_id id,uint32_t en,struct dc_context * ctx)202*b843c749SSergey Zigachev static void construct(
203*b843c749SSergey Zigachev 	struct hw_ddc *ddc,
204*b843c749SSergey Zigachev 	enum gpio_id id,
205*b843c749SSergey Zigachev 	uint32_t en,
206*b843c749SSergey Zigachev 	struct dc_context *ctx)
207*b843c749SSergey Zigachev {
208*b843c749SSergey Zigachev 	dal_hw_gpio_construct(&ddc->base, id, en, ctx);
209*b843c749SSergey Zigachev 	ddc->base.base.funcs = &funcs;
210*b843c749SSergey Zigachev }
211*b843c749SSergey Zigachev 
dal_hw_ddc_create(struct dc_context * ctx,enum gpio_id id,uint32_t en)212*b843c749SSergey Zigachev struct hw_gpio_pin *dal_hw_ddc_create(
213*b843c749SSergey Zigachev 	struct dc_context *ctx,
214*b843c749SSergey Zigachev 	enum gpio_id id,
215*b843c749SSergey Zigachev 	uint32_t en)
216*b843c749SSergey Zigachev {
217*b843c749SSergey Zigachev 	struct hw_ddc *pin;
218*b843c749SSergey Zigachev 
219*b843c749SSergey Zigachev 	if ((en < GPIO_DDC_LINE_MIN) || (en > GPIO_DDC_LINE_MAX)) {
220*b843c749SSergey Zigachev 		ASSERT_CRITICAL(false);
221*b843c749SSergey Zigachev 		return NULL;
222*b843c749SSergey Zigachev 	}
223*b843c749SSergey Zigachev 
224*b843c749SSergey Zigachev 	pin = kzalloc(sizeof(struct hw_ddc), GFP_KERNEL);
225*b843c749SSergey Zigachev 	if (!pin) {
226*b843c749SSergey Zigachev 		ASSERT_CRITICAL(false);
227*b843c749SSergey Zigachev 		return NULL;
228*b843c749SSergey Zigachev 	}
229*b843c749SSergey Zigachev 
230*b843c749SSergey Zigachev 	construct(pin, id, en, ctx);
231*b843c749SSergey Zigachev 	return &pin->base.base;
232*b843c749SSergey Zigachev }
233