1/*	$NetBSD: amdgpu_hw_generic.c,v 1.2 2021/12/18 23:45:04 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_generic.c,v 1.2 2021/12/18 23:45:04 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_generic.h"
39
40#include "reg_helper.h"
41#include "generic_regs.h"
42
43#undef FN
44#define FN(reg_name, field_name) \
45	generic->shifts->field_name, generic->masks->field_name
46
47#define CTX \
48	generic->base.base.ctx
49#define REG(reg)\
50	(generic->regs->reg)
51
52struct gpio;
53
54static void dal_hw_generic_destruct(
55	struct hw_generic *pin)
56{
57	dal_hw_gpio_destruct(&pin->base);
58}
59
60static void dal_hw_generic_destroy(
61	struct hw_gpio_pin **ptr)
62{
63	struct hw_generic *generic = HW_GENERIC_FROM_BASE(*ptr);
64
65	dal_hw_generic_destruct(generic);
66
67	kfree(generic);
68
69	*ptr = NULL;
70}
71
72static enum gpio_result set_config(
73	struct hw_gpio_pin *ptr,
74	const struct gpio_config_data *config_data)
75{
76	struct hw_generic *generic = HW_GENERIC_FROM_BASE(ptr);
77
78	if (!config_data)
79		return GPIO_RESULT_INVALID_DATA;
80
81	REG_UPDATE_2(mux,
82		GENERIC_EN, config_data->config.generic_mux.enable_output_from_mux,
83		GENERIC_SEL, config_data->config.generic_mux.mux_select);
84
85	return GPIO_RESULT_OK;
86}
87
88static const struct hw_gpio_pin_funcs funcs = {
89	.destroy = dal_hw_generic_destroy,
90	.open = dal_hw_gpio_open,
91	.get_value = dal_hw_gpio_get_value,
92	.set_value = dal_hw_gpio_set_value,
93	.set_config = set_config,
94	.change_mode = dal_hw_gpio_change_mode,
95	.close = dal_hw_gpio_close,
96};
97
98static void dal_hw_generic_construct(
99	struct hw_generic *pin,
100	enum gpio_id id,
101	uint32_t en,
102	struct dc_context *ctx)
103{
104	dal_hw_gpio_construct(&pin->base, id, en, ctx);
105	pin->base.base.funcs = &funcs;
106}
107
108void dal_hw_generic_init(
109	struct hw_generic **hw_generic,
110	struct dc_context *ctx,
111	enum gpio_id id,
112	uint32_t en)
113{
114	if ((en < GPIO_DDC_LINE_MIN) || (en > GPIO_DDC_LINE_MAX)) {
115		ASSERT_CRITICAL(false);
116		*hw_generic = NULL;
117	}
118
119	*hw_generic = kzalloc(sizeof(struct hw_generic), GFP_KERNEL);
120	if (!*hw_generic) {
121		ASSERT_CRITICAL(false);
122		return;
123	}
124
125	dal_hw_generic_construct(*hw_generic, id, en, ctx);
126}
127
128
129struct hw_gpio_pin *dal_hw_generic_get_pin(struct gpio *gpio)
130{
131	struct hw_generic *hw_generic = dal_gpio_get_generic(gpio);
132
133	return &hw_generic->base.base;
134}
135