1/*	$NetBSD: amdgpu_hw_hpd.c,v 1.3 2021/12/19 12:02:39 riastradh Exp $	*/
2
3/*
4 * Copyright 2012-15 Advanced Micro Devices, Inc.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 * OTHER DEALINGS IN THE SOFTWARE.
23 *
24 * Authors: AMD
25 *
26 */
27
28#include <sys/cdefs.h>
29__KERNEL_RCSID(0, "$NetBSD: amdgpu_hw_hpd.c,v 1.3 2021/12/19 12:02:39 riastradh Exp $");
30
31#include <linux/slab.h>
32
33#include "dm_services.h"
34
35#include "include/gpio_interface.h"
36#include "include/gpio_types.h"
37#include "hw_gpio.h"
38#include "hw_hpd.h"
39
40#include "reg_helper.h"
41#include "hpd_regs.h"
42
43#undef FN
44#define FN(reg_name, field_name) \
45	hpd->shifts->field_name, hpd->masks->field_name
46
47#define CTX \
48	hpd->base.base.ctx
49#define REG(reg)\
50	(hpd->regs->reg)
51
52struct gpio;
53
54static void dal_hw_hpd_destruct(
55	struct hw_hpd *pin)
56{
57	dal_hw_gpio_destruct(&pin->base);
58}
59
60static void dal_hw_hpd_destroy(
61	struct hw_gpio_pin **ptr)
62{
63	struct hw_hpd *hpd = HW_HPD_FROM_BASE(*ptr);
64
65	dal_hw_hpd_destruct(hpd);
66
67	kfree(hpd);
68
69	*ptr = NULL;
70}
71
72static enum gpio_result get_value(
73	const struct hw_gpio_pin *ptr,
74	uint32_t *value)
75{
76	const struct hw_gpio *gpio = const_container_of(ptr, struct hw_gpio, base);
77	const struct hw_hpd *hpd = const_container_of(gpio, struct hw_hpd, base);
78	uint32_t hpd_delayed = 0;
79
80	/* in Interrupt mode we ask for SENSE bit */
81
82	if (ptr->mode == GPIO_MODE_INTERRUPT) {
83
84		REG_GET(int_status,
85			DC_HPD_SENSE_DELAYED, &hpd_delayed);
86
87		*value = hpd_delayed;
88		return GPIO_RESULT_OK;
89	}
90
91	/* in any other modes, operate as normal GPIO */
92
93	return dal_hw_gpio_get_value(ptr, value);
94}
95
96static enum gpio_result set_config(
97	struct hw_gpio_pin *ptr,
98	const struct gpio_config_data *config_data)
99{
100	struct hw_hpd *hpd = HW_HPD_FROM_BASE(ptr);
101
102	if (!config_data)
103		return GPIO_RESULT_INVALID_DATA;
104
105	REG_UPDATE_2(toggle_filt_cntl,
106		DC_HPD_CONNECT_INT_DELAY, config_data->config.hpd.delay_on_connect / 10,
107		DC_HPD_DISCONNECT_INT_DELAY, config_data->config.hpd.delay_on_disconnect / 10);
108
109	return GPIO_RESULT_OK;
110}
111
112static const struct hw_gpio_pin_funcs funcs = {
113	.destroy = dal_hw_hpd_destroy,
114	.open = dal_hw_gpio_open,
115	.get_value = get_value,
116	.set_value = dal_hw_gpio_set_value,
117	.set_config = set_config,
118	.change_mode = dal_hw_gpio_change_mode,
119	.close = dal_hw_gpio_close,
120};
121
122static void dal_hw_hpd_construct(
123	struct hw_hpd *pin,
124	enum gpio_id id,
125	uint32_t en,
126	struct dc_context *ctx)
127{
128	dal_hw_gpio_construct(&pin->base, id, en, ctx);
129	pin->base.base.funcs = &funcs;
130}
131
132void dal_hw_hpd_init(
133	struct hw_hpd **hw_hpd,
134	struct dc_context *ctx,
135	enum gpio_id id,
136	uint32_t en)
137{
138	if ((en < GPIO_DDC_LINE_MIN) || (en > GPIO_DDC_LINE_MAX)) {
139		ASSERT_CRITICAL(false);
140		*hw_hpd = NULL;
141	}
142
143	*hw_hpd = kzalloc(sizeof(struct hw_hpd), GFP_KERNEL);
144	if (!*hw_hpd) {
145		ASSERT_CRITICAL(false);
146		return;
147	}
148
149	dal_hw_hpd_construct(*hw_hpd, id, en, ctx);
150}
151
152struct hw_gpio_pin *dal_hw_hpd_get_pin(struct gpio *gpio)
153{
154	struct hw_hpd *hw_hpd = dal_gpio_get_hpd(gpio);
155
156	return &hw_hpd->base.base;
157}
158