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