atmel_hlcdc_crtc.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. /*
  2. * Copyright (C) 2014 Traphandler
  3. * Copyright (C) 2014 Free Electrons
  4. *
  5. * Author: Jean-Jacques Hiblot <jjhiblot@traphandler.com>
  6. * Author: Boris BREZILLON <boris.brezillon@free-electrons.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of the GNU General Public License version 2 as published by
  10. * the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful, but WITHOUT
  13. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  15. * more details.
  16. *
  17. * You should have received a copy of the GNU General Public License along with
  18. * this program. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. #include <linux/clk.h>
  21. #include <linux/pm.h>
  22. #include <linux/pm_runtime.h>
  23. #include <linux/pinctrl/consumer.h>
  24. #include <drm/drm_crtc.h>
  25. #include <drm/drm_crtc_helper.h>
  26. #include <drm/drmP.h>
  27. #include <video/videomode.h>
  28. #include "atmel_hlcdc_dc.h"
  29. /**
  30. * Atmel HLCDC CRTC structure
  31. *
  32. * @base: base DRM CRTC structure
  33. * @hlcdc: pointer to the atmel_hlcdc structure provided by the MFD device
  34. * @event: pointer to the current page flip event
  35. * @id: CRTC id (returned by drm_crtc_index)
  36. * @enabled: CRTC state
  37. */
  38. struct atmel_hlcdc_crtc {
  39. struct drm_crtc base;
  40. struct atmel_hlcdc_dc *dc;
  41. struct drm_pending_vblank_event *event;
  42. int id;
  43. bool enabled;
  44. };
  45. static inline struct atmel_hlcdc_crtc *
  46. drm_crtc_to_atmel_hlcdc_crtc(struct drm_crtc *crtc)
  47. {
  48. return container_of(crtc, struct atmel_hlcdc_crtc, base);
  49. }
  50. static void atmel_hlcdc_crtc_mode_set_nofb(struct drm_crtc *c)
  51. {
  52. struct atmel_hlcdc_crtc *crtc = drm_crtc_to_atmel_hlcdc_crtc(c);
  53. struct regmap *regmap = crtc->dc->hlcdc->regmap;
  54. struct drm_display_mode *adj = &c->state->adjusted_mode;
  55. unsigned long mode_rate;
  56. struct videomode vm;
  57. unsigned long prate;
  58. unsigned int cfg;
  59. int div;
  60. vm.vfront_porch = adj->crtc_vsync_start - adj->crtc_vdisplay;
  61. vm.vback_porch = adj->crtc_vtotal - adj->crtc_vsync_end;
  62. vm.vsync_len = adj->crtc_vsync_end - adj->crtc_vsync_start;
  63. vm.hfront_porch = adj->crtc_hsync_start - adj->crtc_hdisplay;
  64. vm.hback_porch = adj->crtc_htotal - adj->crtc_hsync_end;
  65. vm.hsync_len = adj->crtc_hsync_end - adj->crtc_hsync_start;
  66. regmap_write(regmap, ATMEL_HLCDC_CFG(1),
  67. (vm.hsync_len - 1) | ((vm.vsync_len - 1) << 16));
  68. regmap_write(regmap, ATMEL_HLCDC_CFG(2),
  69. (vm.vfront_porch - 1) | (vm.vback_porch << 16));
  70. regmap_write(regmap, ATMEL_HLCDC_CFG(3),
  71. (vm.hfront_porch - 1) | ((vm.hback_porch - 1) << 16));
  72. regmap_write(regmap, ATMEL_HLCDC_CFG(4),
  73. (adj->crtc_hdisplay - 1) |
  74. ((adj->crtc_vdisplay - 1) << 16));
  75. cfg = 0;
  76. prate = clk_get_rate(crtc->dc->hlcdc->sys_clk);
  77. mode_rate = adj->crtc_clock * 1000;
  78. if ((prate / 2) < mode_rate) {
  79. prate *= 2;
  80. cfg |= ATMEL_HLCDC_CLKSEL;
  81. }
  82. div = DIV_ROUND_UP(prate, mode_rate);
  83. if (div < 2)
  84. div = 2;
  85. cfg |= ATMEL_HLCDC_CLKDIV(div);
  86. regmap_update_bits(regmap, ATMEL_HLCDC_CFG(0),
  87. ATMEL_HLCDC_CLKSEL | ATMEL_HLCDC_CLKDIV_MASK |
  88. ATMEL_HLCDC_CLKPOL, cfg);
  89. cfg = 0;
  90. if (adj->flags & DRM_MODE_FLAG_NVSYNC)
  91. cfg |= ATMEL_HLCDC_VSPOL;
  92. if (adj->flags & DRM_MODE_FLAG_NHSYNC)
  93. cfg |= ATMEL_HLCDC_HSPOL;
  94. regmap_update_bits(regmap, ATMEL_HLCDC_CFG(5),
  95. ATMEL_HLCDC_HSPOL | ATMEL_HLCDC_VSPOL |
  96. ATMEL_HLCDC_VSPDLYS | ATMEL_HLCDC_VSPDLYE |
  97. ATMEL_HLCDC_DISPPOL | ATMEL_HLCDC_DISPDLY |
  98. ATMEL_HLCDC_VSPSU | ATMEL_HLCDC_VSPHO |
  99. ATMEL_HLCDC_GUARDTIME_MASK,
  100. cfg);
  101. }
  102. static bool atmel_hlcdc_crtc_mode_fixup(struct drm_crtc *crtc,
  103. const struct drm_display_mode *mode,
  104. struct drm_display_mode *adjusted_mode)
  105. {
  106. return true;
  107. }
  108. static void atmel_hlcdc_crtc_disable(struct drm_crtc *c)
  109. {
  110. struct drm_device *dev = c->dev;
  111. struct atmel_hlcdc_crtc *crtc = drm_crtc_to_atmel_hlcdc_crtc(c);
  112. struct regmap *regmap = crtc->dc->hlcdc->regmap;
  113. unsigned int status;
  114. if (!crtc->enabled)
  115. return;
  116. drm_crtc_vblank_off(c);
  117. pm_runtime_get_sync(dev->dev);
  118. regmap_write(regmap, ATMEL_HLCDC_DIS, ATMEL_HLCDC_DISP);
  119. while (!regmap_read(regmap, ATMEL_HLCDC_SR, &status) &&
  120. (status & ATMEL_HLCDC_DISP))
  121. cpu_relax();
  122. regmap_write(regmap, ATMEL_HLCDC_DIS, ATMEL_HLCDC_SYNC);
  123. while (!regmap_read(regmap, ATMEL_HLCDC_SR, &status) &&
  124. (status & ATMEL_HLCDC_SYNC))
  125. cpu_relax();
  126. regmap_write(regmap, ATMEL_HLCDC_DIS, ATMEL_HLCDC_PIXEL_CLK);
  127. while (!regmap_read(regmap, ATMEL_HLCDC_SR, &status) &&
  128. (status & ATMEL_HLCDC_PIXEL_CLK))
  129. cpu_relax();
  130. clk_disable_unprepare(crtc->dc->hlcdc->sys_clk);
  131. pinctrl_pm_select_sleep_state(dev->dev);
  132. pm_runtime_allow(dev->dev);
  133. pm_runtime_put_sync(dev->dev);
  134. crtc->enabled = false;
  135. }
  136. static void atmel_hlcdc_crtc_enable(struct drm_crtc *c)
  137. {
  138. struct drm_device *dev = c->dev;
  139. struct atmel_hlcdc_crtc *crtc = drm_crtc_to_atmel_hlcdc_crtc(c);
  140. struct regmap *regmap = crtc->dc->hlcdc->regmap;
  141. unsigned int status;
  142. if (crtc->enabled)
  143. return;
  144. pm_runtime_get_sync(dev->dev);
  145. pm_runtime_forbid(dev->dev);
  146. pinctrl_pm_select_default_state(dev->dev);
  147. clk_prepare_enable(crtc->dc->hlcdc->sys_clk);
  148. regmap_write(regmap, ATMEL_HLCDC_EN, ATMEL_HLCDC_PIXEL_CLK);
  149. while (!regmap_read(regmap, ATMEL_HLCDC_SR, &status) &&
  150. !(status & ATMEL_HLCDC_PIXEL_CLK))
  151. cpu_relax();
  152. regmap_write(regmap, ATMEL_HLCDC_EN, ATMEL_HLCDC_SYNC);
  153. while (!regmap_read(regmap, ATMEL_HLCDC_SR, &status) &&
  154. !(status & ATMEL_HLCDC_SYNC))
  155. cpu_relax();
  156. regmap_write(regmap, ATMEL_HLCDC_EN, ATMEL_HLCDC_DISP);
  157. while (!regmap_read(regmap, ATMEL_HLCDC_SR, &status) &&
  158. !(status & ATMEL_HLCDC_DISP))
  159. cpu_relax();
  160. pm_runtime_put_sync(dev->dev);
  161. drm_crtc_vblank_on(c);
  162. crtc->enabled = true;
  163. }
  164. void atmel_hlcdc_crtc_suspend(struct drm_crtc *c)
  165. {
  166. struct atmel_hlcdc_crtc *crtc = drm_crtc_to_atmel_hlcdc_crtc(c);
  167. if (crtc->enabled) {
  168. atmel_hlcdc_crtc_disable(c);
  169. /* save enable state for resume */
  170. crtc->enabled = true;
  171. }
  172. }
  173. void atmel_hlcdc_crtc_resume(struct drm_crtc *c)
  174. {
  175. struct atmel_hlcdc_crtc *crtc = drm_crtc_to_atmel_hlcdc_crtc(c);
  176. if (crtc->enabled) {
  177. crtc->enabled = false;
  178. atmel_hlcdc_crtc_enable(c);
  179. }
  180. }
  181. static int atmel_hlcdc_crtc_atomic_check(struct drm_crtc *c,
  182. struct drm_crtc_state *s)
  183. {
  184. struct atmel_hlcdc_crtc *crtc = drm_crtc_to_atmel_hlcdc_crtc(c);
  185. if (atmel_hlcdc_dc_mode_valid(crtc->dc, &s->adjusted_mode) != MODE_OK)
  186. return -EINVAL;
  187. return atmel_hlcdc_plane_prepare_disc_area(s);
  188. }
  189. static void atmel_hlcdc_crtc_atomic_begin(struct drm_crtc *c,
  190. struct drm_crtc_state *old_s)
  191. {
  192. struct atmel_hlcdc_crtc *crtc = drm_crtc_to_atmel_hlcdc_crtc(c);
  193. if (c->state->event) {
  194. c->state->event->pipe = drm_crtc_index(c);
  195. WARN_ON(drm_crtc_vblank_get(c) != 0);
  196. crtc->event = c->state->event;
  197. c->state->event = NULL;
  198. }
  199. }
  200. static void atmel_hlcdc_crtc_atomic_flush(struct drm_crtc *crtc,
  201. struct drm_crtc_state *old_s)
  202. {
  203. /* TODO: write common plane control register if available */
  204. }
  205. static const struct drm_crtc_helper_funcs lcdc_crtc_helper_funcs = {
  206. .mode_fixup = atmel_hlcdc_crtc_mode_fixup,
  207. .mode_set = drm_helper_crtc_mode_set,
  208. .mode_set_nofb = atmel_hlcdc_crtc_mode_set_nofb,
  209. .mode_set_base = drm_helper_crtc_mode_set_base,
  210. .disable = atmel_hlcdc_crtc_disable,
  211. .enable = atmel_hlcdc_crtc_enable,
  212. .atomic_check = atmel_hlcdc_crtc_atomic_check,
  213. .atomic_begin = atmel_hlcdc_crtc_atomic_begin,
  214. .atomic_flush = atmel_hlcdc_crtc_atomic_flush,
  215. };
  216. static void atmel_hlcdc_crtc_destroy(struct drm_crtc *c)
  217. {
  218. struct atmel_hlcdc_crtc *crtc = drm_crtc_to_atmel_hlcdc_crtc(c);
  219. drm_crtc_cleanup(c);
  220. kfree(crtc);
  221. }
  222. void atmel_hlcdc_crtc_cancel_page_flip(struct drm_crtc *c,
  223. struct drm_file *file)
  224. {
  225. struct atmel_hlcdc_crtc *crtc = drm_crtc_to_atmel_hlcdc_crtc(c);
  226. struct drm_pending_vblank_event *event;
  227. struct drm_device *dev = c->dev;
  228. unsigned long flags;
  229. spin_lock_irqsave(&dev->event_lock, flags);
  230. event = crtc->event;
  231. if (event && event->base.file_priv == file) {
  232. event->base.destroy(&event->base);
  233. drm_vblank_put(dev, crtc->id);
  234. crtc->event = NULL;
  235. }
  236. spin_unlock_irqrestore(&dev->event_lock, flags);
  237. }
  238. static void atmel_hlcdc_crtc_finish_page_flip(struct atmel_hlcdc_crtc *crtc)
  239. {
  240. struct drm_device *dev = crtc->base.dev;
  241. unsigned long flags;
  242. spin_lock_irqsave(&dev->event_lock, flags);
  243. if (crtc->event) {
  244. drm_send_vblank_event(dev, crtc->id, crtc->event);
  245. drm_vblank_put(dev, crtc->id);
  246. crtc->event = NULL;
  247. }
  248. spin_unlock_irqrestore(&dev->event_lock, flags);
  249. }
  250. void atmel_hlcdc_crtc_irq(struct drm_crtc *c)
  251. {
  252. drm_handle_vblank(c->dev, 0);
  253. atmel_hlcdc_crtc_finish_page_flip(drm_crtc_to_atmel_hlcdc_crtc(c));
  254. }
  255. static const struct drm_crtc_funcs atmel_hlcdc_crtc_funcs = {
  256. .page_flip = drm_atomic_helper_page_flip,
  257. .set_config = drm_atomic_helper_set_config,
  258. .destroy = atmel_hlcdc_crtc_destroy,
  259. .reset = drm_atomic_helper_crtc_reset,
  260. .atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state,
  261. .atomic_destroy_state = drm_atomic_helper_crtc_destroy_state,
  262. };
  263. int atmel_hlcdc_crtc_create(struct drm_device *dev)
  264. {
  265. struct atmel_hlcdc_dc *dc = dev->dev_private;
  266. struct atmel_hlcdc_planes *planes = dc->planes;
  267. struct atmel_hlcdc_crtc *crtc;
  268. int ret;
  269. int i;
  270. crtc = kzalloc(sizeof(*crtc), GFP_KERNEL);
  271. if (!crtc)
  272. return -ENOMEM;
  273. crtc->dc = dc;
  274. ret = drm_crtc_init_with_planes(dev, &crtc->base,
  275. &planes->primary->base,
  276. planes->cursor ? &planes->cursor->base : NULL,
  277. &atmel_hlcdc_crtc_funcs);
  278. if (ret < 0)
  279. goto fail;
  280. crtc->id = drm_crtc_index(&crtc->base);
  281. if (planes->cursor)
  282. planes->cursor->base.possible_crtcs = 1 << crtc->id;
  283. for (i = 0; i < planes->noverlays; i++)
  284. planes->overlays[i]->base.possible_crtcs = 1 << crtc->id;
  285. drm_crtc_helper_add(&crtc->base, &lcdc_crtc_helper_funcs);
  286. drm_crtc_vblank_reset(&crtc->base);
  287. dc->crtc = &crtc->base;
  288. return 0;
  289. fail:
  290. atmel_hlcdc_crtc_destroy(&crtc->base);
  291. return ret;
  292. }