14562236bSHarry Wentland /*
24562236bSHarry Wentland  * Copyright 2012-15 Advanced Micro Devices, Inc.
34562236bSHarry Wentland  *
44562236bSHarry Wentland  * Permission is hereby granted, free of charge, to any person obtaining a
54562236bSHarry Wentland  * copy of this software and associated documentation files (the "Software"),
64562236bSHarry Wentland  * to deal in the Software without restriction, including without limitation
74562236bSHarry Wentland  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
84562236bSHarry Wentland  * and/or sell copies of the Software, and to permit persons to whom the
94562236bSHarry Wentland  * Software is furnished to do so, subject to the following conditions:
104562236bSHarry Wentland  *
114562236bSHarry Wentland  * The above copyright notice and this permission notice shall be included in
124562236bSHarry Wentland  * all copies or substantial portions of the Software.
134562236bSHarry Wentland  *
144562236bSHarry Wentland  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
154562236bSHarry Wentland  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
164562236bSHarry Wentland  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
174562236bSHarry Wentland  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
184562236bSHarry Wentland  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
194562236bSHarry Wentland  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
204562236bSHarry Wentland  * OTHER DEALINGS IN THE SOFTWARE.
214562236bSHarry Wentland  *
224562236bSHarry Wentland  * Authors: AMD
234562236bSHarry Wentland  *
244562236bSHarry Wentland  */
254562236bSHarry Wentland 
264562236bSHarry Wentland #include "dm_services.h"
274562236bSHarry Wentland 
284562236bSHarry Wentland #include "include/gpio_types.h"
294562236bSHarry Wentland #include "hw_gpio.h"
304562236bSHarry Wentland #include "hw_ddc.h"
314562236bSHarry Wentland 
324562236bSHarry Wentland #include "reg_helper.h"
334562236bSHarry Wentland #include "gpio_regs.h"
344562236bSHarry Wentland 
354562236bSHarry Wentland 
364562236bSHarry Wentland #undef FN
374562236bSHarry Wentland #define FN(reg_name, field_name) \
384562236bSHarry Wentland 	ddc->shifts->field_name, ddc->masks->field_name
394562236bSHarry Wentland 
404562236bSHarry Wentland #define CTX \
414562236bSHarry Wentland 	ddc->base.base.ctx
424562236bSHarry Wentland #define REG(reg)\
434562236bSHarry Wentland 	(ddc->regs->reg)
444562236bSHarry Wentland 
454562236bSHarry Wentland static void destruct(
464562236bSHarry Wentland 	struct hw_ddc *pin)
474562236bSHarry Wentland {
484562236bSHarry Wentland 	dal_hw_gpio_destruct(&pin->base);
494562236bSHarry Wentland }
504562236bSHarry Wentland 
514562236bSHarry Wentland static void destroy(
524562236bSHarry Wentland 	struct hw_gpio_pin **ptr)
534562236bSHarry Wentland {
544562236bSHarry Wentland 	struct hw_ddc *pin = HW_DDC_FROM_BASE(*ptr);
554562236bSHarry Wentland 
564562236bSHarry Wentland 	destruct(pin);
574562236bSHarry Wentland 
582004f45eSHarry Wentland 	kfree(pin);
594562236bSHarry Wentland 
604562236bSHarry Wentland 	*ptr = NULL;
614562236bSHarry Wentland }
624562236bSHarry Wentland 
634562236bSHarry Wentland static enum gpio_result set_config(
644562236bSHarry Wentland 	struct hw_gpio_pin *ptr,
654562236bSHarry Wentland 	const struct gpio_config_data *config_data)
664562236bSHarry Wentland {
674562236bSHarry Wentland 	struct hw_ddc *ddc = HW_DDC_FROM_BASE(ptr);
684562236bSHarry Wentland 	struct hw_gpio *hw_gpio = NULL;
694562236bSHarry Wentland 	uint32_t regval;
704562236bSHarry Wentland 	uint32_t ddc_data_pd_en = 0;
714562236bSHarry Wentland 	uint32_t ddc_clk_pd_en = 0;
724562236bSHarry Wentland 	uint32_t aux_pad_mode = 0;
734562236bSHarry Wentland 
744562236bSHarry Wentland 	hw_gpio = &ddc->base;
754562236bSHarry Wentland 
764562236bSHarry Wentland 	if (hw_gpio == NULL) {
774562236bSHarry Wentland 		ASSERT_CRITICAL(false);
784562236bSHarry Wentland 		return GPIO_RESULT_NULL_HANDLE;
794562236bSHarry Wentland 	}
804562236bSHarry Wentland 
814562236bSHarry Wentland 	regval = REG_GET_3(gpio.MASK_reg,
824562236bSHarry Wentland 			DC_GPIO_DDC1DATA_PD_EN, &ddc_data_pd_en,
834562236bSHarry Wentland 			DC_GPIO_DDC1CLK_PD_EN, &ddc_clk_pd_en,
844562236bSHarry Wentland 			AUX_PAD1_MODE, &aux_pad_mode);
854562236bSHarry Wentland 
864562236bSHarry Wentland 	switch (config_data->config.ddc.type) {
874562236bSHarry Wentland 	case GPIO_DDC_CONFIG_TYPE_MODE_I2C:
884562236bSHarry Wentland 		/* On plug-in, there is a transient level on the pad
894562236bSHarry Wentland 		 * which must be discharged through the internal pull-down.
904562236bSHarry Wentland 		 * Enable internal pull-down, 2.5msec discharge time
914562236bSHarry Wentland 		 * is required for detection of AUX mode */
924562236bSHarry Wentland 		if (hw_gpio->base.en != GPIO_DDC_LINE_VIP_PAD) {
934562236bSHarry Wentland 			if (!ddc_data_pd_en || !ddc_clk_pd_en) {
944562236bSHarry Wentland 
954562236bSHarry Wentland 				REG_SET_2(gpio.MASK_reg, regval,
964562236bSHarry Wentland 						DC_GPIO_DDC1DATA_PD_EN, 1,
974562236bSHarry Wentland 						DC_GPIO_DDC1CLK_PD_EN, 1);
984562236bSHarry Wentland 
994562236bSHarry Wentland 				if (config_data->type ==
1004562236bSHarry Wentland 						GPIO_CONFIG_TYPE_I2C_AUX_DUAL_MODE)
1014562236bSHarry Wentland 					msleep(3);
1024562236bSHarry Wentland 			}
1034562236bSHarry Wentland 		} else {
1044562236bSHarry Wentland 			uint32_t reg2;
1054562236bSHarry Wentland 			uint32_t sda_pd_dis = 0;
1064562236bSHarry Wentland 			uint32_t scl_pd_dis = 0;
1074562236bSHarry Wentland 
1084562236bSHarry Wentland 			reg2 = REG_GET_2(gpio.MASK_reg,
1094562236bSHarry Wentland 					DC_GPIO_SDA_PD_DIS, &sda_pd_dis,
1104562236bSHarry Wentland 					DC_GPIO_SCL_PD_DIS, &scl_pd_dis);
1114562236bSHarry Wentland 
1124562236bSHarry Wentland 			if (sda_pd_dis) {
1134562236bSHarry Wentland 				REG_SET(gpio.MASK_reg, regval,
1144562236bSHarry Wentland 						DC_GPIO_SDA_PD_DIS, 0);
1154562236bSHarry Wentland 
1164562236bSHarry Wentland 				if (config_data->type ==
1174562236bSHarry Wentland 						GPIO_CONFIG_TYPE_I2C_AUX_DUAL_MODE)
1184562236bSHarry Wentland 					msleep(3);
1194562236bSHarry Wentland 			}
1204562236bSHarry Wentland 
1214562236bSHarry Wentland 			if (!scl_pd_dis) {
1224562236bSHarry Wentland 				REG_SET(gpio.MASK_reg, regval,
1234562236bSHarry Wentland 						DC_GPIO_SCL_PD_DIS, 1);
1244562236bSHarry Wentland 
1254562236bSHarry Wentland 				if (config_data->type ==
1264562236bSHarry Wentland 						GPIO_CONFIG_TYPE_I2C_AUX_DUAL_MODE)
1274562236bSHarry Wentland 					msleep(3);
1284562236bSHarry Wentland 			}
1294562236bSHarry Wentland 		}
1304562236bSHarry Wentland 
1314562236bSHarry Wentland 		if (aux_pad_mode) {
1324562236bSHarry Wentland 			/* let pins to get de-asserted
1334562236bSHarry Wentland 			 * before setting pad to I2C mode */
1344562236bSHarry Wentland 			if (config_data->config.ddc.data_en_bit_present ||
1354562236bSHarry Wentland 				config_data->config.ddc.clock_en_bit_present)
1364562236bSHarry Wentland 				/* [anaumov] in DAL2, there was
1374562236bSHarry Wentland 				 * dc_service_delay_in_microseconds(2000); */
1384562236bSHarry Wentland 				msleep(2);
1394562236bSHarry Wentland 
1404562236bSHarry Wentland 			/* set the I2C pad mode */
1414562236bSHarry Wentland 			/* read the register again,
1424562236bSHarry Wentland 			 * some bits may have been changed */
1434562236bSHarry Wentland 			REG_UPDATE(gpio.MASK_reg,
1444562236bSHarry Wentland 					AUX_PAD1_MODE, 0);
1454562236bSHarry Wentland 		}
1464562236bSHarry Wentland 
1472e35facfSHarry Wentland #if defined(CONFIG_DRM_AMD_DC_DCN2_0)
1482e35facfSHarry Wentland 		if (ddc->regs->dc_gpio_aux_ctrl_5 != 0) {
1492e35facfSHarry Wentland 				REG_UPDATE(dc_gpio_aux_ctrl_5, DDC_PAD_I2CMODE, 1);
1502e35facfSHarry Wentland 		}
1512e35facfSHarry Wentland 		//set  DC_IO_aux_rxsel = 2'b01
1522e35facfSHarry Wentland 		if (ddc->regs->phy_aux_cntl != 0) {
1532e35facfSHarry Wentland 				REG_UPDATE(phy_aux_cntl, AUX_PAD_RXSEL, 1);
1542e35facfSHarry Wentland 		}
1552e35facfSHarry Wentland #endif
1564562236bSHarry Wentland 		return GPIO_RESULT_OK;
1574562236bSHarry Wentland 	case GPIO_DDC_CONFIG_TYPE_MODE_AUX:
1584562236bSHarry Wentland 		/* set the AUX pad mode */
1594562236bSHarry Wentland 		if (!aux_pad_mode) {
1604562236bSHarry Wentland 			REG_SET(gpio.MASK_reg, regval,
1614562236bSHarry Wentland 					AUX_PAD1_MODE, 1);
1624562236bSHarry Wentland 		}
1632e35facfSHarry Wentland #if defined(CONFIG_DRM_AMD_DC_DCN2_0)
1642e35facfSHarry Wentland 		if (ddc->regs->dc_gpio_aux_ctrl_5 != 0) {
1652e35facfSHarry Wentland 			REG_UPDATE(dc_gpio_aux_ctrl_5,
1662e35facfSHarry Wentland 					DDC_PAD_I2CMODE, 0);
1672e35facfSHarry Wentland 		}
1682e35facfSHarry Wentland #endif
1694562236bSHarry Wentland 
1704562236bSHarry Wentland 		return GPIO_RESULT_OK;
1714562236bSHarry Wentland 	case GPIO_DDC_CONFIG_TYPE_POLL_FOR_CONNECT:
1724562236bSHarry Wentland 		if ((hw_gpio->base.en >= GPIO_DDC_LINE_DDC1) &&
1734562236bSHarry Wentland 			(hw_gpio->base.en <= GPIO_DDC_LINE_DDC_VGA)) {
1744562236bSHarry Wentland 			REG_UPDATE_3(ddc_setup,
1754562236bSHarry Wentland 				DC_I2C_DDC1_ENABLE, 1,
1764562236bSHarry Wentland 				DC_I2C_DDC1_EDID_DETECT_ENABLE, 1,
1774562236bSHarry Wentland 				DC_I2C_DDC1_EDID_DETECT_MODE, 0);
1784562236bSHarry Wentland 			return GPIO_RESULT_OK;
1794562236bSHarry Wentland 		}
1804562236bSHarry Wentland 	break;
1814562236bSHarry Wentland 	case GPIO_DDC_CONFIG_TYPE_POLL_FOR_DISCONNECT:
1824562236bSHarry Wentland 		if ((hw_gpio->base.en >= GPIO_DDC_LINE_DDC1) &&
1834562236bSHarry Wentland 			(hw_gpio->base.en <= GPIO_DDC_LINE_DDC_VGA)) {
1844562236bSHarry Wentland 			REG_UPDATE_3(ddc_setup,
1854562236bSHarry Wentland 				DC_I2C_DDC1_ENABLE, 1,
1864562236bSHarry Wentland 				DC_I2C_DDC1_EDID_DETECT_ENABLE, 1,
1874562236bSHarry Wentland 				DC_I2C_DDC1_EDID_DETECT_MODE, 1);
1884562236bSHarry Wentland 			return GPIO_RESULT_OK;
1894562236bSHarry Wentland 		}
1904562236bSHarry Wentland 	break;
1914562236bSHarry Wentland 	case GPIO_DDC_CONFIG_TYPE_DISABLE_POLLING:
1924562236bSHarry Wentland 		if ((hw_gpio->base.en >= GPIO_DDC_LINE_DDC1) &&
1934562236bSHarry Wentland 			(hw_gpio->base.en <= GPIO_DDC_LINE_DDC_VGA)) {
1944562236bSHarry Wentland 			REG_UPDATE_2(ddc_setup,
1954562236bSHarry Wentland 				DC_I2C_DDC1_ENABLE, 0,
1964562236bSHarry Wentland 				DC_I2C_DDC1_EDID_DETECT_ENABLE, 0);
1974562236bSHarry Wentland 			return GPIO_RESULT_OK;
1984562236bSHarry Wentland 		}
1994562236bSHarry Wentland 	break;
2004562236bSHarry Wentland 	}
2014562236bSHarry Wentland 
2024562236bSHarry Wentland 	BREAK_TO_DEBUGGER();
2034562236bSHarry Wentland 
2044562236bSHarry Wentland 	return GPIO_RESULT_NON_SPECIFIC_ERROR;
2054562236bSHarry Wentland }
2064562236bSHarry Wentland 
2074562236bSHarry Wentland static const struct hw_gpio_pin_funcs funcs = {
2084562236bSHarry Wentland 	.destroy = destroy,
2094562236bSHarry Wentland 	.open = dal_hw_gpio_open,
2104562236bSHarry Wentland 	.get_value = dal_hw_gpio_get_value,
2114562236bSHarry Wentland 	.set_value = dal_hw_gpio_set_value,
2124562236bSHarry Wentland 	.set_config = set_config,
2134562236bSHarry Wentland 	.change_mode = dal_hw_gpio_change_mode,
2144562236bSHarry Wentland 	.close = dal_hw_gpio_close,
2154562236bSHarry Wentland };
2164562236bSHarry Wentland 
2170e1c42fdSDave Airlie static void construct(
2184562236bSHarry Wentland 	struct hw_ddc *ddc,
2194562236bSHarry Wentland 	enum gpio_id id,
2204562236bSHarry Wentland 	uint32_t en,
2214562236bSHarry Wentland 	struct dc_context *ctx)
2224562236bSHarry Wentland {
2230e1c42fdSDave Airlie 	dal_hw_gpio_construct(&ddc->base, id, en, ctx);
2244562236bSHarry Wentland 	ddc->base.base.funcs = &funcs;
2254562236bSHarry Wentland }
2264562236bSHarry Wentland 
2274562236bSHarry Wentland struct hw_gpio_pin *dal_hw_ddc_create(
2284562236bSHarry Wentland 	struct dc_context *ctx,
2294562236bSHarry Wentland 	enum gpio_id id,
2304562236bSHarry Wentland 	uint32_t en)
2314562236bSHarry Wentland {
2320e1c42fdSDave Airlie 	struct hw_ddc *pin;
2334562236bSHarry Wentland 
2340e1c42fdSDave Airlie 	if ((en < GPIO_DDC_LINE_MIN) || (en > GPIO_DDC_LINE_MAX)) {
2350e1c42fdSDave Airlie 		ASSERT_CRITICAL(false);
2360e1c42fdSDave Airlie 		return NULL;
2370e1c42fdSDave Airlie 	}
2380e1c42fdSDave Airlie 
2390e1c42fdSDave Airlie 	pin = kzalloc(sizeof(struct hw_ddc), GFP_KERNEL);
2404562236bSHarry Wentland 	if (!pin) {
2414562236bSHarry Wentland 		ASSERT_CRITICAL(false);
2424562236bSHarry Wentland 		return NULL;
2434562236bSHarry Wentland 	}
2444562236bSHarry Wentland 
2450e1c42fdSDave Airlie 	construct(pin, id, en, ctx);
2464562236bSHarry Wentland 	return &pin->base.base;
2474562236bSHarry Wentland }
248