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 
584562236bSHarry Wentland 	dm_free(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 
1474562236bSHarry Wentland 		return GPIO_RESULT_OK;
1484562236bSHarry Wentland 	case GPIO_DDC_CONFIG_TYPE_MODE_AUX:
1494562236bSHarry Wentland 		/* set the AUX pad mode */
1504562236bSHarry Wentland 		if (!aux_pad_mode) {
1514562236bSHarry Wentland 			REG_SET(gpio.MASK_reg, regval,
1524562236bSHarry Wentland 					AUX_PAD1_MODE, 1);
1534562236bSHarry Wentland 		}
1544562236bSHarry Wentland 
1554562236bSHarry Wentland 		return GPIO_RESULT_OK;
1564562236bSHarry Wentland 	case GPIO_DDC_CONFIG_TYPE_POLL_FOR_CONNECT:
1574562236bSHarry Wentland 		if ((hw_gpio->base.en >= GPIO_DDC_LINE_DDC1) &&
1584562236bSHarry Wentland 			(hw_gpio->base.en <= GPIO_DDC_LINE_DDC_VGA)) {
1594562236bSHarry Wentland 			REG_UPDATE_3(ddc_setup,
1604562236bSHarry Wentland 				DC_I2C_DDC1_ENABLE, 1,
1614562236bSHarry Wentland 				DC_I2C_DDC1_EDID_DETECT_ENABLE, 1,
1624562236bSHarry Wentland 				DC_I2C_DDC1_EDID_DETECT_MODE, 0);
1634562236bSHarry Wentland 			return GPIO_RESULT_OK;
1644562236bSHarry Wentland 		}
1654562236bSHarry Wentland 	break;
1664562236bSHarry Wentland 	case GPIO_DDC_CONFIG_TYPE_POLL_FOR_DISCONNECT:
1674562236bSHarry Wentland 		if ((hw_gpio->base.en >= GPIO_DDC_LINE_DDC1) &&
1684562236bSHarry Wentland 			(hw_gpio->base.en <= GPIO_DDC_LINE_DDC_VGA)) {
1694562236bSHarry Wentland 			REG_UPDATE_3(ddc_setup,
1704562236bSHarry Wentland 				DC_I2C_DDC1_ENABLE, 1,
1714562236bSHarry Wentland 				DC_I2C_DDC1_EDID_DETECT_ENABLE, 1,
1724562236bSHarry Wentland 				DC_I2C_DDC1_EDID_DETECT_MODE, 1);
1734562236bSHarry Wentland 			return GPIO_RESULT_OK;
1744562236bSHarry Wentland 		}
1754562236bSHarry Wentland 	break;
1764562236bSHarry Wentland 	case GPIO_DDC_CONFIG_TYPE_DISABLE_POLLING:
1774562236bSHarry Wentland 		if ((hw_gpio->base.en >= GPIO_DDC_LINE_DDC1) &&
1784562236bSHarry Wentland 			(hw_gpio->base.en <= GPIO_DDC_LINE_DDC_VGA)) {
1794562236bSHarry Wentland 			REG_UPDATE_2(ddc_setup,
1804562236bSHarry Wentland 				DC_I2C_DDC1_ENABLE, 0,
1814562236bSHarry Wentland 				DC_I2C_DDC1_EDID_DETECT_ENABLE, 0);
1824562236bSHarry Wentland 			return GPIO_RESULT_OK;
1834562236bSHarry Wentland 		}
1844562236bSHarry Wentland 	break;
1854562236bSHarry Wentland 	}
1864562236bSHarry Wentland 
1874562236bSHarry Wentland 	BREAK_TO_DEBUGGER();
1884562236bSHarry Wentland 
1894562236bSHarry Wentland 	return GPIO_RESULT_NON_SPECIFIC_ERROR;
1904562236bSHarry Wentland }
1914562236bSHarry Wentland 
1924562236bSHarry Wentland static const struct hw_gpio_pin_funcs funcs = {
1934562236bSHarry Wentland 	.destroy = destroy,
1944562236bSHarry Wentland 	.open = dal_hw_gpio_open,
1954562236bSHarry Wentland 	.get_value = dal_hw_gpio_get_value,
1964562236bSHarry Wentland 	.set_value = dal_hw_gpio_set_value,
1974562236bSHarry Wentland 	.set_config = set_config,
1984562236bSHarry Wentland 	.change_mode = dal_hw_gpio_change_mode,
1994562236bSHarry Wentland 	.close = dal_hw_gpio_close,
2004562236bSHarry Wentland };
2014562236bSHarry Wentland 
2024562236bSHarry Wentland static bool construct(
2034562236bSHarry Wentland 	struct hw_ddc *ddc,
2044562236bSHarry Wentland 	enum gpio_id id,
2054562236bSHarry Wentland 	uint32_t en,
2064562236bSHarry Wentland 	struct dc_context *ctx)
2074562236bSHarry Wentland {
2084562236bSHarry Wentland 	if ((en < GPIO_DDC_LINE_MIN) || (en > GPIO_DDC_LINE_MAX)) {
2094562236bSHarry Wentland 		ASSERT_CRITICAL(false);
2104562236bSHarry Wentland 		return false;
2114562236bSHarry Wentland 	}
2124562236bSHarry Wentland 
2134562236bSHarry Wentland 	if (!dal_hw_gpio_construct(&ddc->base, id, en, ctx)) {
2144562236bSHarry Wentland 		ASSERT_CRITICAL(false);
2154562236bSHarry Wentland 		return false;
2164562236bSHarry Wentland 	}
2174562236bSHarry Wentland 
2184562236bSHarry Wentland 	ddc->base.base.funcs = &funcs;
2194562236bSHarry Wentland 
2204562236bSHarry Wentland 	return true;
2214562236bSHarry Wentland }
2224562236bSHarry Wentland 
2234562236bSHarry Wentland struct hw_gpio_pin *dal_hw_ddc_create(
2244562236bSHarry Wentland 	struct dc_context *ctx,
2254562236bSHarry Wentland 	enum gpio_id id,
2264562236bSHarry Wentland 	uint32_t en)
2274562236bSHarry Wentland {
2284562236bSHarry Wentland 	struct hw_ddc *pin = dm_alloc(sizeof(struct hw_ddc));
2294562236bSHarry Wentland 
2304562236bSHarry Wentland 	if (!pin) {
2314562236bSHarry Wentland 		ASSERT_CRITICAL(false);
2324562236bSHarry Wentland 		return NULL;
2334562236bSHarry Wentland 	}
2344562236bSHarry Wentland 
2354562236bSHarry Wentland 	if (construct(pin, id, en, ctx))
2364562236bSHarry Wentland 		return &pin->base.base;
2374562236bSHarry Wentland 
2384562236bSHarry Wentland 	ASSERT_CRITICAL(false);
2394562236bSHarry Wentland 
2404562236bSHarry Wentland 	dm_free(pin);
2414562236bSHarry Wentland 
2424562236bSHarry Wentland 	return NULL;
2434562236bSHarry Wentland }
244