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 264fc4dca8SSam Ravnborg #include <linux/slab.h> 274fc4dca8SSam Ravnborg 284562236bSHarry Wentland #include "dm_services.h" 294562236bSHarry Wentland 3091db9311SSu Sung Chung #include "include/gpio_interface.h" 314562236bSHarry Wentland #include "include/gpio_types.h" 324562236bSHarry Wentland #include "hw_gpio.h" 334562236bSHarry Wentland #include "hw_hpd.h" 344562236bSHarry Wentland 354562236bSHarry Wentland #include "reg_helper.h" 364562236bSHarry Wentland #include "hpd_regs.h" 374562236bSHarry Wentland 384562236bSHarry Wentland #undef FN 394562236bSHarry Wentland #define FN(reg_name, field_name) \ 404562236bSHarry Wentland hpd->shifts->field_name, hpd->masks->field_name 414562236bSHarry Wentland 424562236bSHarry Wentland #define CTX \ 434562236bSHarry Wentland hpd->base.base.ctx 444562236bSHarry Wentland #define REG(reg)\ 454562236bSHarry Wentland (hpd->regs->reg) 464562236bSHarry Wentland 4791db9311SSu Sung Chung struct gpio; 4891db9311SSu Sung Chung 494562236bSHarry Wentland static void dal_hw_hpd_destruct( 504562236bSHarry Wentland struct hw_hpd *pin) 514562236bSHarry Wentland { 524562236bSHarry Wentland dal_hw_gpio_destruct(&pin->base); 534562236bSHarry Wentland } 544562236bSHarry Wentland 55d9e32672SAnthony Koo static void dal_hw_hpd_destroy( 564562236bSHarry Wentland struct hw_gpio_pin **ptr) 574562236bSHarry Wentland { 584562236bSHarry Wentland struct hw_hpd *hpd = HW_HPD_FROM_BASE(*ptr); 594562236bSHarry Wentland 60d9e32672SAnthony Koo dal_hw_hpd_destruct(hpd); 614562236bSHarry Wentland 622004f45eSHarry Wentland kfree(hpd); 634562236bSHarry Wentland 644562236bSHarry Wentland *ptr = NULL; 654562236bSHarry Wentland } 664562236bSHarry Wentland 674562236bSHarry Wentland static enum gpio_result get_value( 684562236bSHarry Wentland const struct hw_gpio_pin *ptr, 694562236bSHarry Wentland uint32_t *value) 704562236bSHarry Wentland { 714562236bSHarry Wentland struct hw_hpd *hpd = HW_HPD_FROM_BASE(ptr); 724562236bSHarry Wentland uint32_t hpd_delayed = 0; 734562236bSHarry Wentland 744562236bSHarry Wentland /* in Interrupt mode we ask for SENSE bit */ 754562236bSHarry Wentland 764562236bSHarry Wentland if (ptr->mode == GPIO_MODE_INTERRUPT) { 774562236bSHarry Wentland 784562236bSHarry Wentland REG_GET(int_status, 794562236bSHarry Wentland DC_HPD_SENSE_DELAYED, &hpd_delayed); 804562236bSHarry Wentland 814562236bSHarry Wentland *value = hpd_delayed; 824562236bSHarry Wentland return GPIO_RESULT_OK; 834562236bSHarry Wentland } 844562236bSHarry Wentland 854562236bSHarry Wentland /* in any other modes, operate as normal GPIO */ 864562236bSHarry Wentland 874562236bSHarry Wentland return dal_hw_gpio_get_value(ptr, value); 884562236bSHarry Wentland } 894562236bSHarry Wentland 904562236bSHarry Wentland static enum gpio_result set_config( 914562236bSHarry Wentland struct hw_gpio_pin *ptr, 924562236bSHarry Wentland const struct gpio_config_data *config_data) 934562236bSHarry Wentland { 944562236bSHarry Wentland struct hw_hpd *hpd = HW_HPD_FROM_BASE(ptr); 954562236bSHarry Wentland 964562236bSHarry Wentland if (!config_data) 974562236bSHarry Wentland return GPIO_RESULT_INVALID_DATA; 984562236bSHarry Wentland 994562236bSHarry Wentland REG_UPDATE_2(toggle_filt_cntl, 1004562236bSHarry Wentland DC_HPD_CONNECT_INT_DELAY, config_data->config.hpd.delay_on_connect / 10, 1014562236bSHarry Wentland DC_HPD_DISCONNECT_INT_DELAY, config_data->config.hpd.delay_on_disconnect / 10); 1024562236bSHarry Wentland 1034562236bSHarry Wentland return GPIO_RESULT_OK; 1044562236bSHarry Wentland } 1054562236bSHarry Wentland 1064562236bSHarry Wentland static const struct hw_gpio_pin_funcs funcs = { 107d9e32672SAnthony Koo .destroy = dal_hw_hpd_destroy, 1084562236bSHarry Wentland .open = dal_hw_gpio_open, 1094562236bSHarry Wentland .get_value = get_value, 1104562236bSHarry Wentland .set_value = dal_hw_gpio_set_value, 1114562236bSHarry Wentland .set_config = set_config, 1124562236bSHarry Wentland .change_mode = dal_hw_gpio_change_mode, 1134562236bSHarry Wentland .close = dal_hw_gpio_close, 1144562236bSHarry Wentland }; 1154562236bSHarry Wentland 116d9e32672SAnthony Koo static void dal_hw_hpd_construct( 117d9e32672SAnthony Koo struct hw_hpd *pin, 1184562236bSHarry Wentland enum gpio_id id, 1194562236bSHarry Wentland uint32_t en, 1204562236bSHarry Wentland struct dc_context *ctx) 1214562236bSHarry Wentland { 122d9e32672SAnthony Koo dal_hw_gpio_construct(&pin->base, id, en, ctx); 123d9e32672SAnthony Koo pin->base.base.funcs = &funcs; 1244562236bSHarry Wentland } 1254562236bSHarry Wentland 12691db9311SSu Sung Chung void dal_hw_hpd_init( 12791db9311SSu Sung Chung struct hw_hpd **hw_hpd, 1284562236bSHarry Wentland struct dc_context *ctx, 1294562236bSHarry Wentland enum gpio_id id, 1304562236bSHarry Wentland uint32_t en) 1314562236bSHarry Wentland { 13291db9311SSu Sung Chung if ((en < GPIO_DDC_LINE_MIN) || (en > GPIO_DDC_LINE_MAX)) { 1330e1c42fdSDave Airlie ASSERT_CRITICAL(false); 13491db9311SSu Sung Chung *hw_hpd = NULL; 1350e1c42fdSDave Airlie } 1360e1c42fdSDave Airlie 13791db9311SSu Sung Chung *hw_hpd = kzalloc(sizeof(struct hw_hpd), GFP_KERNEL); 13891db9311SSu Sung Chung if (!*hw_hpd) { 1390e1c42fdSDave Airlie ASSERT_CRITICAL(false); 14091db9311SSu Sung Chung return; 1410e1c42fdSDave Airlie } 1420e1c42fdSDave Airlie 143d9e32672SAnthony Koo dal_hw_hpd_construct(*hw_hpd, id, en, ctx); 1444562236bSHarry Wentland } 1454562236bSHarry Wentland 14691db9311SSu Sung Chung struct hw_gpio_pin *dal_hw_hpd_get_pin(struct gpio *gpio) 14791db9311SSu Sung Chung { 14891db9311SSu Sung Chung struct hw_hpd *hw_hpd = dal_gpio_get_hpd(gpio); 14991db9311SSu Sung Chung 15091db9311SSu Sung Chung return &hw_hpd->base.base; 1514562236bSHarry Wentland } 152