1 /*
2  * Copyright 2021 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 "reg_helper.h"
27 #include "core_types.h"
28 #include "dc_dmub_srv.h"
29 #include "dcn31_panel_cntl.h"
30 #include "atom.h"
31 
32 #define TO_DCN31_PANEL_CNTL(panel_cntl)\
33 	container_of(panel_cntl, struct dcn31_panel_cntl, base)
34 
35 #define CTX \
36 	dcn31_panel_cntl->base.ctx
37 
38 #define DC_LOGGER \
39 	dcn31_panel_cntl->base.ctx->logger
40 
dcn31_query_backlight_info(struct panel_cntl * panel_cntl,union dmub_rb_cmd * cmd)41 static bool dcn31_query_backlight_info(struct panel_cntl *panel_cntl, union dmub_rb_cmd *cmd)
42 {
43 	struct dcn31_panel_cntl *dcn31_panel_cntl = TO_DCN31_PANEL_CNTL(panel_cntl);
44 	struct dc_dmub_srv *dc_dmub_srv = panel_cntl->ctx->dmub_srv;
45 
46 	if (!dc_dmub_srv)
47 		return false;
48 
49 	memset(cmd, 0, sizeof(*cmd));
50 	cmd->panel_cntl.header.type = DMUB_CMD__PANEL_CNTL;
51 	cmd->panel_cntl.header.sub_type = DMUB_CMD__PANEL_CNTL_QUERY_BACKLIGHT_INFO;
52 	cmd->panel_cntl.header.payload_bytes = sizeof(cmd->panel_cntl.data);
53 	cmd->panel_cntl.data.inst = dcn31_panel_cntl->base.inst;
54 
55 	return dc_dmub_srv_cmd_with_reply_data(dc_dmub_srv, cmd);
56 }
57 
dcn31_get_16_bit_backlight_from_pwm(struct panel_cntl * panel_cntl)58 static uint32_t dcn31_get_16_bit_backlight_from_pwm(struct panel_cntl *panel_cntl)
59 {
60 	union dmub_rb_cmd cmd;
61 
62 	if (!dcn31_query_backlight_info(panel_cntl, &cmd))
63 		return 0;
64 
65 	return cmd.panel_cntl.data.current_backlight;
66 }
67 
dcn31_panel_cntl_hw_init(struct panel_cntl * panel_cntl)68 uint32_t dcn31_panel_cntl_hw_init(struct panel_cntl *panel_cntl)
69 {
70 	struct dcn31_panel_cntl *dcn31_panel_cntl = TO_DCN31_PANEL_CNTL(panel_cntl);
71 	struct dc_dmub_srv *dc_dmub_srv = panel_cntl->ctx->dmub_srv;
72 	union dmub_rb_cmd cmd;
73 
74 	if (!dc_dmub_srv)
75 		return 0;
76 
77 	memset(&cmd, 0, sizeof(cmd));
78 	cmd.panel_cntl.header.type = DMUB_CMD__PANEL_CNTL;
79 	cmd.panel_cntl.header.sub_type = DMUB_CMD__PANEL_CNTL_HW_INIT;
80 	cmd.panel_cntl.header.payload_bytes = sizeof(cmd.panel_cntl.data);
81 	cmd.panel_cntl.data.inst = dcn31_panel_cntl->base.inst;
82 	cmd.panel_cntl.data.bl_pwm_cntl = panel_cntl->stored_backlight_registers.BL_PWM_CNTL;
83 	cmd.panel_cntl.data.bl_pwm_period_cntl = panel_cntl->stored_backlight_registers.BL_PWM_PERIOD_CNTL;
84 	cmd.panel_cntl.data.bl_pwm_ref_div1 =
85 		panel_cntl->stored_backlight_registers.LVTMA_PWRSEQ_REF_DIV_BL_PWM_REF_DIV;
86 
87 	if (!dc_dmub_srv_cmd_with_reply_data(dc_dmub_srv, &cmd))
88 		return 0;
89 
90 	panel_cntl->stored_backlight_registers.BL_PWM_CNTL = cmd.panel_cntl.data.bl_pwm_cntl;
91 	panel_cntl->stored_backlight_registers.BL_PWM_CNTL2 = 0; /* unused */
92 	panel_cntl->stored_backlight_registers.BL_PWM_PERIOD_CNTL = cmd.panel_cntl.data.bl_pwm_period_cntl;
93 	panel_cntl->stored_backlight_registers.LVTMA_PWRSEQ_REF_DIV_BL_PWM_REF_DIV =
94 		cmd.panel_cntl.data.bl_pwm_ref_div1;
95 
96 	return cmd.panel_cntl.data.current_backlight;
97 }
98 
dcn31_panel_cntl_destroy(struct panel_cntl ** panel_cntl)99 void dcn31_panel_cntl_destroy(struct panel_cntl **panel_cntl)
100 {
101 	struct dcn31_panel_cntl *dcn31_panel_cntl = TO_DCN31_PANEL_CNTL(*panel_cntl);
102 
103 	kfree(dcn31_panel_cntl);
104 	*panel_cntl = NULL;
105 }
106 
dcn31_is_panel_backlight_on(struct panel_cntl * panel_cntl)107 bool dcn31_is_panel_backlight_on(struct panel_cntl *panel_cntl)
108 {
109 	union dmub_rb_cmd cmd;
110 
111 	if (!dcn31_query_backlight_info(panel_cntl, &cmd))
112 		return false;
113 
114 	return cmd.panel_cntl.data.is_backlight_on;
115 }
116 
dcn31_is_panel_powered_on(struct panel_cntl * panel_cntl)117 bool dcn31_is_panel_powered_on(struct panel_cntl *panel_cntl)
118 {
119 	union dmub_rb_cmd cmd;
120 
121 	if (!dcn31_query_backlight_info(panel_cntl, &cmd))
122 		return false;
123 
124 	return cmd.panel_cntl.data.is_powered_on;
125 }
126 
dcn31_store_backlight_level(struct panel_cntl * panel_cntl)127 void dcn31_store_backlight_level(struct panel_cntl *panel_cntl)
128 {
129 	union dmub_rb_cmd cmd;
130 
131 	if (!dcn31_query_backlight_info(panel_cntl, &cmd))
132 		return;
133 
134 	panel_cntl->stored_backlight_registers.BL_PWM_CNTL = cmd.panel_cntl.data.bl_pwm_cntl;
135 	panel_cntl->stored_backlight_registers.BL_PWM_CNTL2 = 0; /* unused */
136 	panel_cntl->stored_backlight_registers.BL_PWM_PERIOD_CNTL = cmd.panel_cntl.data.bl_pwm_period_cntl;
137 	panel_cntl->stored_backlight_registers.LVTMA_PWRSEQ_REF_DIV_BL_PWM_REF_DIV =
138 		cmd.panel_cntl.data.bl_pwm_ref_div1;
139 }
140 
141 static const struct panel_cntl_funcs dcn31_link_panel_cntl_funcs = {
142 	.destroy = dcn31_panel_cntl_destroy,
143 	.hw_init = dcn31_panel_cntl_hw_init,
144 	.is_panel_backlight_on = dcn31_is_panel_backlight_on,
145 	.is_panel_powered_on = dcn31_is_panel_powered_on,
146 	.store_backlight_level = dcn31_store_backlight_level,
147 	.get_current_backlight = dcn31_get_16_bit_backlight_from_pwm,
148 };
149 
dcn31_panel_cntl_construct(struct dcn31_panel_cntl * dcn31_panel_cntl,const struct panel_cntl_init_data * init_data)150 void dcn31_panel_cntl_construct(
151 	struct dcn31_panel_cntl *dcn31_panel_cntl,
152 	const struct panel_cntl_init_data *init_data)
153 {
154 	dcn31_panel_cntl->base.funcs = &dcn31_link_panel_cntl_funcs;
155 	dcn31_panel_cntl->base.ctx = init_data->ctx;
156 	dcn31_panel_cntl->base.inst = init_data->inst;
157 }
158