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 
26c366be54SSam Ravnborg #include <linux/delay.h>
27c366be54SSam Ravnborg 
284562236bSHarry Wentland #include "dm_services.h"
294562236bSHarry Wentland 
304562236bSHarry Wentland #include "include/gpio_types.h"
314562236bSHarry Wentland #include "hw_gpio.h"
324562236bSHarry Wentland #include "hw_ddc.h"
334562236bSHarry Wentland 
344562236bSHarry Wentland #include "reg_helper.h"
354562236bSHarry Wentland #include "gpio_regs.h"
364562236bSHarry Wentland 
374562236bSHarry Wentland 
384562236bSHarry Wentland #undef FN
394562236bSHarry Wentland #define FN(reg_name, field_name) \
404562236bSHarry Wentland 	ddc->shifts->field_name, ddc->masks->field_name
414562236bSHarry Wentland 
424562236bSHarry Wentland #define CTX \
434562236bSHarry Wentland 	ddc->base.base.ctx
444562236bSHarry Wentland #define REG(reg)\
454562236bSHarry Wentland 	(ddc->regs->reg)
464562236bSHarry Wentland 
474562236bSHarry Wentland static void destruct(
484562236bSHarry Wentland 	struct hw_ddc *pin)
494562236bSHarry Wentland {
504562236bSHarry Wentland 	dal_hw_gpio_destruct(&pin->base);
514562236bSHarry Wentland }
524562236bSHarry Wentland 
534562236bSHarry Wentland static void destroy(
544562236bSHarry Wentland 	struct hw_gpio_pin **ptr)
554562236bSHarry Wentland {
564562236bSHarry Wentland 	struct hw_ddc *pin = HW_DDC_FROM_BASE(*ptr);
574562236bSHarry Wentland 
584562236bSHarry Wentland 	destruct(pin);
594562236bSHarry Wentland 
602004f45eSHarry Wentland 	kfree(pin);
614562236bSHarry Wentland 
624562236bSHarry Wentland 	*ptr = NULL;
634562236bSHarry Wentland }
644562236bSHarry Wentland 
654562236bSHarry Wentland static enum gpio_result set_config(
664562236bSHarry Wentland 	struct hw_gpio_pin *ptr,
674562236bSHarry Wentland 	const struct gpio_config_data *config_data)
684562236bSHarry Wentland {
694562236bSHarry Wentland 	struct hw_ddc *ddc = HW_DDC_FROM_BASE(ptr);
704562236bSHarry Wentland 	struct hw_gpio *hw_gpio = NULL;
714562236bSHarry Wentland 	uint32_t regval;
724562236bSHarry Wentland 	uint32_t ddc_data_pd_en = 0;
734562236bSHarry Wentland 	uint32_t ddc_clk_pd_en = 0;
744562236bSHarry Wentland 	uint32_t aux_pad_mode = 0;
754562236bSHarry Wentland 
764562236bSHarry Wentland 	hw_gpio = &ddc->base;
774562236bSHarry Wentland 
784562236bSHarry Wentland 	if (hw_gpio == NULL) {
794562236bSHarry Wentland 		ASSERT_CRITICAL(false);
804562236bSHarry Wentland 		return GPIO_RESULT_NULL_HANDLE;
814562236bSHarry Wentland 	}
824562236bSHarry Wentland 
834562236bSHarry Wentland 	regval = REG_GET_3(gpio.MASK_reg,
844562236bSHarry Wentland 			DC_GPIO_DDC1DATA_PD_EN, &ddc_data_pd_en,
854562236bSHarry Wentland 			DC_GPIO_DDC1CLK_PD_EN, &ddc_clk_pd_en,
864562236bSHarry Wentland 			AUX_PAD1_MODE, &aux_pad_mode);
874562236bSHarry Wentland 
884562236bSHarry Wentland 	switch (config_data->config.ddc.type) {
894562236bSHarry Wentland 	case GPIO_DDC_CONFIG_TYPE_MODE_I2C:
904562236bSHarry Wentland 		/* On plug-in, there is a transient level on the pad
914562236bSHarry Wentland 		 * which must be discharged through the internal pull-down.
924562236bSHarry Wentland 		 * Enable internal pull-down, 2.5msec discharge time
934562236bSHarry Wentland 		 * is required for detection of AUX mode */
944562236bSHarry Wentland 		if (hw_gpio->base.en != GPIO_DDC_LINE_VIP_PAD) {
954562236bSHarry Wentland 			if (!ddc_data_pd_en || !ddc_clk_pd_en) {
964562236bSHarry Wentland 
974562236bSHarry Wentland 				REG_SET_2(gpio.MASK_reg, regval,
984562236bSHarry Wentland 						DC_GPIO_DDC1DATA_PD_EN, 1,
994562236bSHarry Wentland 						DC_GPIO_DDC1CLK_PD_EN, 1);
1004562236bSHarry Wentland 
1014562236bSHarry Wentland 				if (config_data->type ==
1024562236bSHarry Wentland 						GPIO_CONFIG_TYPE_I2C_AUX_DUAL_MODE)
1034562236bSHarry Wentland 					msleep(3);
1044562236bSHarry Wentland 			}
1054562236bSHarry Wentland 		} else {
1064562236bSHarry Wentland 			uint32_t reg2;
1074562236bSHarry Wentland 			uint32_t sda_pd_dis = 0;
1084562236bSHarry Wentland 			uint32_t scl_pd_dis = 0;
1094562236bSHarry Wentland 
1104562236bSHarry Wentland 			reg2 = REG_GET_2(gpio.MASK_reg,
1114562236bSHarry Wentland 					DC_GPIO_SDA_PD_DIS, &sda_pd_dis,
1124562236bSHarry Wentland 					DC_GPIO_SCL_PD_DIS, &scl_pd_dis);
1134562236bSHarry Wentland 
1144562236bSHarry Wentland 			if (sda_pd_dis) {
1154562236bSHarry Wentland 				REG_SET(gpio.MASK_reg, regval,
1164562236bSHarry Wentland 						DC_GPIO_SDA_PD_DIS, 0);
1174562236bSHarry Wentland 
1184562236bSHarry Wentland 				if (config_data->type ==
1194562236bSHarry Wentland 						GPIO_CONFIG_TYPE_I2C_AUX_DUAL_MODE)
1204562236bSHarry Wentland 					msleep(3);
1214562236bSHarry Wentland 			}
1224562236bSHarry Wentland 
1234562236bSHarry Wentland 			if (!scl_pd_dis) {
1244562236bSHarry Wentland 				REG_SET(gpio.MASK_reg, regval,
1254562236bSHarry Wentland 						DC_GPIO_SCL_PD_DIS, 1);
1264562236bSHarry Wentland 
1274562236bSHarry Wentland 				if (config_data->type ==
1284562236bSHarry Wentland 						GPIO_CONFIG_TYPE_I2C_AUX_DUAL_MODE)
1294562236bSHarry Wentland 					msleep(3);
1304562236bSHarry Wentland 			}
1314562236bSHarry Wentland 		}
1324562236bSHarry Wentland 
1334562236bSHarry Wentland 		if (aux_pad_mode) {
1344562236bSHarry Wentland 			/* let pins to get de-asserted
1354562236bSHarry Wentland 			 * before setting pad to I2C mode */
1364562236bSHarry Wentland 			if (config_data->config.ddc.data_en_bit_present ||
1374562236bSHarry Wentland 				config_data->config.ddc.clock_en_bit_present)
1384562236bSHarry Wentland 				/* [anaumov] in DAL2, there was
1394562236bSHarry Wentland 				 * dc_service_delay_in_microseconds(2000); */
1404562236bSHarry Wentland 				msleep(2);
1414562236bSHarry Wentland 
1424562236bSHarry Wentland 			/* set the I2C pad mode */
1434562236bSHarry Wentland 			/* read the register again,
1444562236bSHarry Wentland 			 * some bits may have been changed */
1454562236bSHarry Wentland 			REG_UPDATE(gpio.MASK_reg,
1464562236bSHarry Wentland 					AUX_PAD1_MODE, 0);
1474562236bSHarry Wentland 		}
1484562236bSHarry Wentland 
1494562236bSHarry Wentland 		return GPIO_RESULT_OK;
1504562236bSHarry Wentland 	case GPIO_DDC_CONFIG_TYPE_MODE_AUX:
1514562236bSHarry Wentland 		/* set the AUX pad mode */
1524562236bSHarry Wentland 		if (!aux_pad_mode) {
1534562236bSHarry Wentland 			REG_SET(gpio.MASK_reg, regval,
1544562236bSHarry Wentland 					AUX_PAD1_MODE, 1);
1554562236bSHarry Wentland 		}
1564562236bSHarry Wentland 
1574562236bSHarry Wentland 		return GPIO_RESULT_OK;
1584562236bSHarry Wentland 	case GPIO_DDC_CONFIG_TYPE_POLL_FOR_CONNECT:
1594562236bSHarry Wentland 		if ((hw_gpio->base.en >= GPIO_DDC_LINE_DDC1) &&
1604562236bSHarry Wentland 			(hw_gpio->base.en <= GPIO_DDC_LINE_DDC_VGA)) {
1614562236bSHarry Wentland 			REG_UPDATE_3(ddc_setup,
1624562236bSHarry Wentland 				DC_I2C_DDC1_ENABLE, 1,
1634562236bSHarry Wentland 				DC_I2C_DDC1_EDID_DETECT_ENABLE, 1,
1644562236bSHarry Wentland 				DC_I2C_DDC1_EDID_DETECT_MODE, 0);
1654562236bSHarry Wentland 			return GPIO_RESULT_OK;
1664562236bSHarry Wentland 		}
1674562236bSHarry Wentland 	break;
1684562236bSHarry Wentland 	case GPIO_DDC_CONFIG_TYPE_POLL_FOR_DISCONNECT:
1694562236bSHarry Wentland 		if ((hw_gpio->base.en >= GPIO_DDC_LINE_DDC1) &&
1704562236bSHarry Wentland 			(hw_gpio->base.en <= GPIO_DDC_LINE_DDC_VGA)) {
1714562236bSHarry Wentland 			REG_UPDATE_3(ddc_setup,
1724562236bSHarry Wentland 				DC_I2C_DDC1_ENABLE, 1,
1734562236bSHarry Wentland 				DC_I2C_DDC1_EDID_DETECT_ENABLE, 1,
1744562236bSHarry Wentland 				DC_I2C_DDC1_EDID_DETECT_MODE, 1);
1754562236bSHarry Wentland 			return GPIO_RESULT_OK;
1764562236bSHarry Wentland 		}
1774562236bSHarry Wentland 	break;
1784562236bSHarry Wentland 	case GPIO_DDC_CONFIG_TYPE_DISABLE_POLLING:
1794562236bSHarry Wentland 		if ((hw_gpio->base.en >= GPIO_DDC_LINE_DDC1) &&
1804562236bSHarry Wentland 			(hw_gpio->base.en <= GPIO_DDC_LINE_DDC_VGA)) {
1814562236bSHarry Wentland 			REG_UPDATE_2(ddc_setup,
1824562236bSHarry Wentland 				DC_I2C_DDC1_ENABLE, 0,
1834562236bSHarry Wentland 				DC_I2C_DDC1_EDID_DETECT_ENABLE, 0);
1844562236bSHarry Wentland 			return GPIO_RESULT_OK;
1854562236bSHarry Wentland 		}
1864562236bSHarry Wentland 	break;
1874562236bSHarry Wentland 	}
1884562236bSHarry Wentland 
1894562236bSHarry Wentland 	BREAK_TO_DEBUGGER();
1904562236bSHarry Wentland 
1914562236bSHarry Wentland 	return GPIO_RESULT_NON_SPECIFIC_ERROR;
1924562236bSHarry Wentland }
1934562236bSHarry Wentland 
1944562236bSHarry Wentland static const struct hw_gpio_pin_funcs funcs = {
1954562236bSHarry Wentland 	.destroy = destroy,
1964562236bSHarry Wentland 	.open = dal_hw_gpio_open,
1974562236bSHarry Wentland 	.get_value = dal_hw_gpio_get_value,
1984562236bSHarry Wentland 	.set_value = dal_hw_gpio_set_value,
1994562236bSHarry Wentland 	.set_config = set_config,
2004562236bSHarry Wentland 	.change_mode = dal_hw_gpio_change_mode,
2014562236bSHarry Wentland 	.close = dal_hw_gpio_close,
2024562236bSHarry Wentland };
2034562236bSHarry Wentland 
2040e1c42fdSDave Airlie static void construct(
2054562236bSHarry Wentland 	struct hw_ddc *ddc,
2064562236bSHarry Wentland 	enum gpio_id id,
2074562236bSHarry Wentland 	uint32_t en,
2084562236bSHarry Wentland 	struct dc_context *ctx)
2094562236bSHarry Wentland {
2100e1c42fdSDave Airlie 	dal_hw_gpio_construct(&ddc->base, id, en, ctx);
2114562236bSHarry Wentland 	ddc->base.base.funcs = &funcs;
2124562236bSHarry Wentland }
2134562236bSHarry Wentland 
2144562236bSHarry Wentland struct hw_gpio_pin *dal_hw_ddc_create(
2154562236bSHarry Wentland 	struct dc_context *ctx,
2164562236bSHarry Wentland 	enum gpio_id id,
2174562236bSHarry Wentland 	uint32_t en)
2184562236bSHarry Wentland {
2190e1c42fdSDave Airlie 	struct hw_ddc *pin;
2204562236bSHarry Wentland 
2210e1c42fdSDave Airlie 	if ((en < GPIO_DDC_LINE_MIN) || (en > GPIO_DDC_LINE_MAX)) {
2220e1c42fdSDave Airlie 		ASSERT_CRITICAL(false);
2230e1c42fdSDave Airlie 		return NULL;
2240e1c42fdSDave Airlie 	}
2250e1c42fdSDave Airlie 
2260e1c42fdSDave Airlie 	pin = kzalloc(sizeof(struct hw_ddc), GFP_KERNEL);
2274562236bSHarry Wentland 	if (!pin) {
2284562236bSHarry Wentland 		ASSERT_CRITICAL(false);
2294562236bSHarry Wentland 		return NULL;
2304562236bSHarry Wentland 	}
2314562236bSHarry Wentland 
2320e1c42fdSDave Airlie 	construct(pin, id, en, ctx);
2334562236bSHarry Wentland 	return &pin->base.base;
2344562236bSHarry Wentland }
235