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 
dal_hw_generic_destruct(struct hw_generic * pin)49 static void dal_hw_generic_destruct(
50 	struct hw_generic *pin)
51 {
52 	dal_hw_gpio_destruct(&pin->base);
53 }
54 
dal_hw_generic_destroy(struct hw_gpio_pin ** ptr)55 static void dal_hw_generic_destroy(
56 	struct hw_gpio_pin **ptr)
57 {
58 	struct hw_generic *generic = HW_GENERIC_FROM_BASE(*ptr);
59 
60 	dal_hw_generic_destruct(generic);
61 
62 	kfree(generic);
63 
64 	*ptr = NULL;
65 }
66 
set_config(struct hw_gpio_pin * ptr,const struct gpio_config_data * config_data)67 static enum gpio_result set_config(
68 	struct hw_gpio_pin *ptr,
69 	const struct gpio_config_data *config_data)
70 {
71 	struct hw_generic *generic = HW_GENERIC_FROM_BASE(ptr);
72 
73 	if (!config_data)
74 		return GPIO_RESULT_INVALID_DATA;
75 
76 	REG_UPDATE_2(mux,
77 		GENERIC_EN, config_data->config.generic_mux.enable_output_from_mux,
78 		GENERIC_SEL, config_data->config.generic_mux.mux_select);
79 
80 	return GPIO_RESULT_OK;
81 }
82 
83 static const struct hw_gpio_pin_funcs funcs = {
84 	.destroy = dal_hw_generic_destroy,
85 	.open = dal_hw_gpio_open,
86 	.get_value = dal_hw_gpio_get_value,
87 	.set_value = dal_hw_gpio_set_value,
88 	.set_config = set_config,
89 	.change_mode = dal_hw_gpio_change_mode,
90 	.close = dal_hw_gpio_close,
91 };
92 
dal_hw_generic_construct(struct hw_generic * pin,enum gpio_id id,uint32_t en,struct dc_context * ctx)93 static void dal_hw_generic_construct(
94 	struct hw_generic *pin,
95 	enum gpio_id id,
96 	uint32_t en,
97 	struct dc_context *ctx)
98 {
99 	dal_hw_gpio_construct(&pin->base, id, en, ctx);
100 	pin->base.base.funcs = &funcs;
101 }
102 
dal_hw_generic_init(struct hw_generic ** hw_generic,struct dc_context * ctx,enum gpio_id id,uint32_t en)103 void dal_hw_generic_init(
104 	struct hw_generic **hw_generic,
105 	struct dc_context *ctx,
106 	enum gpio_id id,
107 	uint32_t en)
108 {
109 	if (en > GPIO_DDC_LINE_MAX) {
110 		ASSERT_CRITICAL(false);
111 		*hw_generic = NULL;
112 	}
113 
114 	*hw_generic = kzalloc(sizeof(struct hw_generic), GFP_KERNEL);
115 	if (!*hw_generic) {
116 		ASSERT_CRITICAL(false);
117 		return;
118 	}
119 
120 	dal_hw_generic_construct(*hw_generic, id, en, ctx);
121 }
122 
123 
dal_hw_generic_get_pin(struct gpio * gpio)124 struct hw_gpio_pin *dal_hw_generic_get_pin(struct gpio *gpio)
125 {
126 	struct hw_generic *hw_generic = dal_gpio_get_generic(gpio);
127 
128 	return &hw_generic->base.base;
129 }
130