1 // SPDX-License-Identifier: MIT
3 * Copyright (C) 2019 Google, Inc.
8 #include <linux/bitops.h>
9 #include <linux/slab.h>
10 #include <linux/workqueue.h>
12 #include <drm/drm_atomic.h>
13 #include <drm/drm_atomic_helper.h>
14 #include <drm/drm_connector.h>
15 #include <drm/drm_crtc.h>
16 #include <drm/drm_device.h>
17 #include <drm/drm_mode_config.h>
18 #include <drm/drm_modeset_lock.h>
19 #include <drm/drm_print.h>
20 #include <drm/drm_self_refresh_helper.h>
25 * This helper library provides an easy way for drivers to leverage the atomic
26 * framework to implement panel self refresh (SR) support. Drivers are
27 * responsible for initializing and cleaning up the SR helpers on load/unload
28 * (see &drm_self_refresh_helper_init/&drm_self_refresh_helper_cleanup).
29 * The connector is responsible for setting
30 * &drm_connector_state.self_refresh_aware to true at runtime if it is SR-aware
31 * (meaning it knows how to initiate self refresh on the panel).
33 * Once a crtc has enabled SR using &drm_self_refresh_helper_init, the
34 * helpers will monitor activity and call back into the driver to enable/disable
35 * SR as appropriate. The best way to think about this is that it's a DPMS
36 * on/off request with &drm_crtc_state.self_refresh_active set in crtc state
37 * that tells you to disable/enable SR on the panel instead of power-cycling it.
39 * During SR, drivers may choose to fully disable their crtc/encoder/bridge
40 * hardware (in which case no driver changes are necessary), or they can inspect
41 * &drm_crtc_state.self_refresh_active if they want to enter low power mode
42 * without full disable (in case full disable/enable is too slow).
44 * SR will be deactivated if there are any atomic updates affecting the
45 * pipe that is in SR mode. If a crtc is driving multiple connectors, all
46 * connectors must be SR aware and all will enter/exit SR mode at the same time.
48 * If the crtc and connector are SR aware, but the panel connected does not
49 * support it (or is otherwise unable to enter SR), the driver should fail
50 * atomic_check when &drm_crtc_state.self_refresh_active is true.
53 struct drm_self_refresh_data {
54 struct drm_crtc *crtc;
55 struct delayed_work entry_work;
56 struct drm_atomic_state *save_state;
57 unsigned int entry_delay_ms;
60 static void drm_self_refresh_helper_entry_work(struct work_struct *work)
62 struct drm_self_refresh_data *sr_data = container_of(
63 to_delayed_work(work),
64 struct drm_self_refresh_data, entry_work);
65 struct drm_crtc *crtc = sr_data->crtc;
66 struct drm_device *dev = crtc->dev;
67 struct drm_modeset_acquire_ctx ctx;
68 struct drm_atomic_state *state;
69 struct drm_connector *conn;
70 struct drm_connector_state *conn_state;
71 struct drm_crtc_state *crtc_state;
74 drm_modeset_acquire_init(&ctx, 0);
76 state = drm_atomic_state_alloc(dev);
83 state->acquire_ctx = &ctx;
85 crtc_state = drm_atomic_get_crtc_state(state, crtc);
86 if (IS_ERR(crtc_state)) {
87 ret = PTR_ERR(crtc_state);
91 if (!crtc_state->enable)
94 ret = drm_atomic_add_affected_connectors(state, crtc);
98 for_each_new_connector_in_state(state, conn, conn_state, i) {
99 if (!conn_state->self_refresh_aware)
103 crtc_state->active = false;
104 crtc_state->self_refresh_active = true;
106 ret = drm_atomic_commit(state);
111 if (ret == -EDEADLK) {
112 drm_atomic_state_clear(state);
113 ret = drm_modeset_backoff(&ctx);
118 drm_atomic_state_put(state);
121 drm_modeset_drop_locks(&ctx);
122 drm_modeset_acquire_fini(&ctx);
126 * drm_self_refresh_helper_alter_state - Alters the atomic state for SR exit
127 * @state: the state currently being checked
129 * Called at the end of atomic check. This function checks the state for flags
130 * incompatible with self refresh exit and changes them. This is a bit
131 * disingenuous since userspace is expecting one thing and we're giving it
132 * another. However in order to keep self refresh entirely hidden from
133 * userspace, this is required.
135 * At the end, we queue up the self refresh entry work so we can enter PSR after
138 void drm_self_refresh_helper_alter_state(struct drm_atomic_state *state)
140 struct drm_crtc *crtc;
141 struct drm_crtc_state *crtc_state;
144 if (state->async_update || !state->allow_modeset) {
145 for_each_old_crtc_in_state(state, crtc, crtc_state, i) {
146 if (crtc_state->self_refresh_active) {
147 state->async_update = false;
148 state->allow_modeset = true;
154 for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
155 struct drm_self_refresh_data *sr_data;
157 /* Don't trigger the entry timer when we're already in SR */
158 if (crtc_state->self_refresh_active)
161 sr_data = crtc->self_refresh_data;
165 mod_delayed_work(system_wq, &sr_data->entry_work,
166 msecs_to_jiffies(sr_data->entry_delay_ms));
169 EXPORT_SYMBOL(drm_self_refresh_helper_alter_state);
172 * drm_self_refresh_helper_init - Initializes self refresh helpers for a crtc
173 * @crtc: the crtc which supports self refresh supported displays
174 * @entry_delay_ms: amount of inactivity to wait before entering self refresh
176 * Returns zero if successful or -errno on failure
178 int drm_self_refresh_helper_init(struct drm_crtc *crtc,
179 unsigned int entry_delay_ms)
181 struct drm_self_refresh_data *sr_data = crtc->self_refresh_data;
183 /* Helper is already initialized */
184 if (WARN_ON(sr_data))
187 sr_data = kzalloc(sizeof(*sr_data), GFP_KERNEL);
191 INIT_DELAYED_WORK(&sr_data->entry_work,
192 drm_self_refresh_helper_entry_work);
193 sr_data->entry_delay_ms = entry_delay_ms;
194 sr_data->crtc = crtc;
196 crtc->self_refresh_data = sr_data;
199 EXPORT_SYMBOL(drm_self_refresh_helper_init);
202 * drm_self_refresh_helper_cleanup - Cleans up self refresh helpers for a crtc
203 * @crtc: the crtc to cleanup
205 void drm_self_refresh_helper_cleanup(struct drm_crtc *crtc)
207 struct drm_self_refresh_data *sr_data = crtc->self_refresh_data;
209 /* Helper is already uninitialized */
213 crtc->self_refresh_data = NULL;
215 cancel_delayed_work_sync(&sr_data->entry_work);
218 EXPORT_SYMBOL(drm_self_refresh_helper_cleanup);