2 * Copyright (C) Fuzhou Rockchip Electronics Co.Ltd
3 * Author: Yakir Yang <ykk@rock-chips.com>
5 * This software is licensed under the terms of the GNU General Public
6 * License version 2, as published by the Free Software Foundation, and
7 * may be copied, distributed, and modified under those terms.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
16 #include <drm/drm_crtc_helper.h>
18 #include "rockchip_drm_drv.h"
19 #include "rockchip_drm_psr.h"
21 #define PSR_FLUSH_TIMEOUT msecs_to_jiffies(100)
30 struct list_head list;
31 struct drm_encoder *encoder;
37 struct timer_list flush_timer;
39 void (*set)(struct drm_encoder *encoder, bool enable);
42 static struct psr_drv *find_psr_by_crtc(struct drm_crtc *crtc)
44 struct rockchip_drm_private *drm_drv = crtc->dev->dev_private;
48 spin_lock_irqsave(&drm_drv->psr_list_lock, flags);
49 list_for_each_entry(psr, &drm_drv->psr_list, list) {
50 if (psr->encoder->crtc == crtc)
53 psr = ERR_PTR(-ENODEV);
56 spin_unlock_irqrestore(&drm_drv->psr_list_lock, flags);
60 static void psr_set_state_locked(struct psr_drv *psr, enum psr_state state)
63 * Allowed finite state machine:
65 * PSR_ENABLE < = = = = = > PSR_FLUSH
69 * PSR_DISABLE < - - - - - - - - -
71 if (state == psr->state || !psr->active)
74 /* Already disabled in flush, change the state, but not the hardware */
75 if (state == PSR_DISABLE && psr->state == PSR_FLUSH) {
82 /* Actually commit the state change to hardware */
85 psr->set(psr->encoder, true);
90 psr->set(psr->encoder, false);
95 static void psr_set_state(struct psr_drv *psr, enum psr_state state)
99 spin_lock_irqsave(&psr->lock, flags);
100 psr_set_state_locked(psr, state);
101 spin_unlock_irqrestore(&psr->lock, flags);
104 static void psr_flush_handler(unsigned long data)
106 struct psr_drv *psr = (struct psr_drv *)data;
109 /* If the state has changed since we initiated the flush, do nothing */
110 spin_lock_irqsave(&psr->lock, flags);
111 if (psr->state == PSR_FLUSH)
112 psr_set_state_locked(psr, PSR_ENABLE);
113 spin_unlock_irqrestore(&psr->lock, flags);
117 * rockchip_drm_psr_activate - activate PSR on the given pipe
118 * @crtc: CRTC to obtain the PSR encoder
121 * Zero on success, negative errno on failure.
123 int rockchip_drm_psr_activate(struct drm_crtc *crtc)
125 struct psr_drv *psr = find_psr_by_crtc(crtc);
131 spin_lock_irqsave(&psr->lock, flags);
133 spin_unlock_irqrestore(&psr->lock, flags);
137 EXPORT_SYMBOL(rockchip_drm_psr_activate);
140 * rockchip_drm_psr_deactivate - deactivate PSR on the given pipe
141 * @crtc: CRTC to obtain the PSR encoder
144 * Zero on success, negative errno on failure.
146 int rockchip_drm_psr_deactivate(struct drm_crtc *crtc)
148 struct psr_drv *psr = find_psr_by_crtc(crtc);
154 spin_lock_irqsave(&psr->lock, flags);
156 spin_unlock_irqrestore(&psr->lock, flags);
157 del_timer_sync(&psr->flush_timer);
161 EXPORT_SYMBOL(rockchip_drm_psr_deactivate);
163 static void rockchip_drm_do_flush(struct psr_drv *psr)
165 mod_timer(&psr->flush_timer,
166 round_jiffies_up(jiffies + PSR_FLUSH_TIMEOUT));
167 psr_set_state(psr, PSR_FLUSH);
171 * rockchip_drm_psr_flush - flush a single pipe
172 * @crtc: CRTC of the pipe to flush
175 * 0 on success, -errno on fail
177 int rockchip_drm_psr_flush(struct drm_crtc *crtc)
179 struct psr_drv *psr = find_psr_by_crtc(crtc);
183 rockchip_drm_do_flush(psr);
186 EXPORT_SYMBOL(rockchip_drm_psr_flush);
189 * rockchip_drm_psr_flush_all - force to flush all registered PSR encoders
192 * Disable the PSR function for all registered encoders, and then enable the
193 * PSR function back after PSR_FLUSH_TIMEOUT. If encoder PSR state have been
194 * changed during flush time, then keep the state no change after flush
198 * Zero on success, negative errno on failure.
200 void rockchip_drm_psr_flush_all(struct drm_device *dev)
202 struct rockchip_drm_private *drm_drv = dev->dev_private;
206 spin_lock_irqsave(&drm_drv->psr_list_lock, flags);
207 list_for_each_entry(psr, &drm_drv->psr_list, list)
208 rockchip_drm_do_flush(psr);
209 spin_unlock_irqrestore(&drm_drv->psr_list_lock, flags);
211 EXPORT_SYMBOL(rockchip_drm_psr_flush_all);
214 * rockchip_drm_psr_register - register encoder to psr driver
215 * @encoder: encoder that obtain the PSR function
216 * @psr_set: call back to set PSR state
219 * Zero on success, negative errno on failure.
221 int rockchip_drm_psr_register(struct drm_encoder *encoder,
222 void (*psr_set)(struct drm_encoder *, bool enable))
224 struct rockchip_drm_private *drm_drv = encoder->dev->dev_private;
228 if (!encoder || !psr_set)
231 psr = kzalloc(sizeof(struct psr_drv), GFP_KERNEL);
235 setup_timer(&psr->flush_timer, psr_flush_handler, (unsigned long)psr);
236 spin_lock_init(&psr->lock);
239 psr->state = PSR_DISABLE;
240 psr->encoder = encoder;
243 spin_lock_irqsave(&drm_drv->psr_list_lock, flags);
244 list_add_tail(&psr->list, &drm_drv->psr_list);
245 spin_unlock_irqrestore(&drm_drv->psr_list_lock, flags);
249 EXPORT_SYMBOL(rockchip_drm_psr_register);
252 * rockchip_drm_psr_unregister - unregister encoder to psr driver
253 * @encoder: encoder that obtain the PSR function
254 * @psr_set: call back to set PSR state
257 * Zero on success, negative errno on failure.
259 void rockchip_drm_psr_unregister(struct drm_encoder *encoder)
261 struct rockchip_drm_private *drm_drv = encoder->dev->dev_private;
262 struct psr_drv *psr, *n;
265 spin_lock_irqsave(&drm_drv->psr_list_lock, flags);
266 list_for_each_entry_safe(psr, n, &drm_drv->psr_list, list) {
267 if (psr->encoder == encoder) {
268 del_timer(&psr->flush_timer);
269 list_del(&psr->list);
273 spin_unlock_irqrestore(&drm_drv->psr_list_lock, flags);
275 EXPORT_SYMBOL(rockchip_drm_psr_unregister);