Rob Clark | 16ea975 | 2013-01-08 15:04:28 -0600 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 Texas Instruments |
| 3 | * Author: Rob Clark <robdclark@gmail.com> |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify it |
| 6 | * under the terms of the GNU General Public License version 2 as published by |
| 7 | * the Free Software Foundation. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, but WITHOUT |
| 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 12 | * more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License along with |
| 15 | * this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 | */ |
| 17 | |
| 18 | /* LCDC DRM driver, based on da8xx-fb */ |
| 19 | |
Jyri Sarha | 103cd8b | 2015-02-10 14:13:23 +0200 | [diff] [blame] | 20 | #include <linux/component.h> |
Dave Gerlach | 416a07f | 2014-07-29 06:27:58 +0000 | [diff] [blame] | 21 | #include <linux/pinctrl/consumer.h> |
| 22 | #include <linux/suspend.h> |
Jyri Sarha | 103cd8b | 2015-02-10 14:13:23 +0200 | [diff] [blame] | 23 | |
Rob Clark | 16ea975 | 2013-01-08 15:04:28 -0600 | [diff] [blame] | 24 | #include "tilcdc_drv.h" |
| 25 | #include "tilcdc_regs.h" |
| 26 | #include "tilcdc_tfp410.h" |
Rob Clark | 0d4bbaf | 2012-12-18 17:34:16 -0600 | [diff] [blame] | 27 | #include "tilcdc_panel.h" |
Jyri Sarha | 103cd8b | 2015-02-10 14:13:23 +0200 | [diff] [blame] | 28 | #include "tilcdc_external.h" |
Rob Clark | 16ea975 | 2013-01-08 15:04:28 -0600 | [diff] [blame] | 29 | |
| 30 | #include "drm_fb_helper.h" |
| 31 | |
| 32 | static LIST_HEAD(module_list); |
| 33 | |
| 34 | void tilcdc_module_init(struct tilcdc_module *mod, const char *name, |
| 35 | const struct tilcdc_module_ops *funcs) |
| 36 | { |
| 37 | mod->name = name; |
| 38 | mod->funcs = funcs; |
| 39 | INIT_LIST_HEAD(&mod->list); |
| 40 | list_add(&mod->list, &module_list); |
| 41 | } |
| 42 | |
| 43 | void tilcdc_module_cleanup(struct tilcdc_module *mod) |
| 44 | { |
| 45 | list_del(&mod->list); |
| 46 | } |
| 47 | |
| 48 | static struct of_device_id tilcdc_of_match[]; |
| 49 | |
| 50 | static struct drm_framebuffer *tilcdc_fb_create(struct drm_device *dev, |
Ville Syrjälä | 1eb8345 | 2015-11-11 19:11:29 +0200 | [diff] [blame] | 51 | struct drm_file *file_priv, const struct drm_mode_fb_cmd2 *mode_cmd) |
Rob Clark | 16ea975 | 2013-01-08 15:04:28 -0600 | [diff] [blame] | 52 | { |
| 53 | return drm_fb_cma_create(dev, file_priv, mode_cmd); |
| 54 | } |
| 55 | |
| 56 | static void tilcdc_fb_output_poll_changed(struct drm_device *dev) |
| 57 | { |
| 58 | struct tilcdc_drm_private *priv = dev->dev_private; |
Markus Elfring | c0844817 | 2014-11-19 17:05:20 +0100 | [diff] [blame] | 59 | drm_fbdev_cma_hotplug_event(priv->fbdev); |
Rob Clark | 16ea975 | 2013-01-08 15:04:28 -0600 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | static const struct drm_mode_config_funcs mode_config_funcs = { |
| 63 | .fb_create = tilcdc_fb_create, |
| 64 | .output_poll_changed = tilcdc_fb_output_poll_changed, |
| 65 | }; |
| 66 | |
| 67 | static int modeset_init(struct drm_device *dev) |
| 68 | { |
| 69 | struct tilcdc_drm_private *priv = dev->dev_private; |
| 70 | struct tilcdc_module *mod; |
| 71 | |
| 72 | drm_mode_config_init(dev); |
| 73 | |
| 74 | priv->crtc = tilcdc_crtc_create(dev); |
| 75 | |
| 76 | list_for_each_entry(mod, &module_list, list) { |
| 77 | DBG("loading module: %s", mod->name); |
| 78 | mod->funcs->modeset_init(mod, dev); |
| 79 | } |
| 80 | |
Rob Clark | 16ea975 | 2013-01-08 15:04:28 -0600 | [diff] [blame] | 81 | dev->mode_config.min_width = 0; |
| 82 | dev->mode_config.min_height = 0; |
| 83 | dev->mode_config.max_width = tilcdc_crtc_max_width(priv->crtc); |
| 84 | dev->mode_config.max_height = 2048; |
| 85 | dev->mode_config.funcs = &mode_config_funcs; |
| 86 | |
| 87 | return 0; |
| 88 | } |
| 89 | |
| 90 | #ifdef CONFIG_CPU_FREQ |
| 91 | static int cpufreq_transition(struct notifier_block *nb, |
| 92 | unsigned long val, void *data) |
| 93 | { |
| 94 | struct tilcdc_drm_private *priv = container_of(nb, |
| 95 | struct tilcdc_drm_private, freq_transition); |
| 96 | if (val == CPUFREQ_POSTCHANGE) { |
| 97 | if (priv->lcd_fck_rate != clk_get_rate(priv->clk)) { |
| 98 | priv->lcd_fck_rate = clk_get_rate(priv->clk); |
| 99 | tilcdc_crtc_update_clk(priv->crtc); |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | return 0; |
| 104 | } |
| 105 | #endif |
| 106 | |
| 107 | /* |
| 108 | * DRM operations: |
| 109 | */ |
| 110 | |
| 111 | static int tilcdc_unload(struct drm_device *dev) |
| 112 | { |
| 113 | struct tilcdc_drm_private *priv = dev->dev_private; |
Rob Clark | 16ea975 | 2013-01-08 15:04:28 -0600 | [diff] [blame] | 114 | |
Tomi Valkeinen | 1aea1e7 | 2015-10-19 14:15:26 +0300 | [diff] [blame^] | 115 | tilcdc_crtc_dpms(priv->crtc, DRM_MODE_DPMS_OFF); |
| 116 | |
Jyri Sarha | 103cd8b | 2015-02-10 14:13:23 +0200 | [diff] [blame] | 117 | tilcdc_remove_external_encoders(dev); |
| 118 | |
Guido Martínez | 3a49012 | 2014-06-17 11:17:07 -0300 | [diff] [blame] | 119 | drm_fbdev_cma_fini(priv->fbdev); |
Rob Clark | 16ea975 | 2013-01-08 15:04:28 -0600 | [diff] [blame] | 120 | drm_kms_helper_poll_fini(dev); |
| 121 | drm_mode_config_cleanup(dev); |
| 122 | drm_vblank_cleanup(dev); |
| 123 | |
| 124 | pm_runtime_get_sync(dev->dev); |
| 125 | drm_irq_uninstall(dev); |
| 126 | pm_runtime_put_sync(dev->dev); |
| 127 | |
| 128 | #ifdef CONFIG_CPU_FREQ |
| 129 | cpufreq_unregister_notifier(&priv->freq_transition, |
| 130 | CPUFREQ_TRANSITION_NOTIFIER); |
| 131 | #endif |
| 132 | |
| 133 | if (priv->clk) |
| 134 | clk_put(priv->clk); |
| 135 | |
| 136 | if (priv->mmio) |
| 137 | iounmap(priv->mmio); |
| 138 | |
| 139 | flush_workqueue(priv->wq); |
| 140 | destroy_workqueue(priv->wq); |
| 141 | |
| 142 | dev->dev_private = NULL; |
| 143 | |
| 144 | pm_runtime_disable(dev->dev); |
| 145 | |
Jyri Sarha | 29ddd6e | 2015-07-02 16:26:12 +0300 | [diff] [blame] | 146 | kfree(priv->saved_register); |
Rob Clark | 16ea975 | 2013-01-08 15:04:28 -0600 | [diff] [blame] | 147 | kfree(priv); |
| 148 | |
| 149 | return 0; |
| 150 | } |
| 151 | |
Jyri Sarha | 29ddd6e | 2015-07-02 16:26:12 +0300 | [diff] [blame] | 152 | static size_t tilcdc_num_regs(void); |
| 153 | |
Rob Clark | 16ea975 | 2013-01-08 15:04:28 -0600 | [diff] [blame] | 154 | static int tilcdc_load(struct drm_device *dev, unsigned long flags) |
| 155 | { |
| 156 | struct platform_device *pdev = dev->platformdev; |
| 157 | struct device_node *node = pdev->dev.of_node; |
| 158 | struct tilcdc_drm_private *priv; |
Benoit Parrot | dc28aa0 | 2013-06-18 17:18:31 -0500 | [diff] [blame] | 159 | struct tilcdc_module *mod; |
Rob Clark | 16ea975 | 2013-01-08 15:04:28 -0600 | [diff] [blame] | 160 | struct resource *res; |
Benoit Parrot | dc28aa0 | 2013-06-18 17:18:31 -0500 | [diff] [blame] | 161 | u32 bpp = 0; |
Rob Clark | 16ea975 | 2013-01-08 15:04:28 -0600 | [diff] [blame] | 162 | int ret; |
| 163 | |
| 164 | priv = kzalloc(sizeof(*priv), GFP_KERNEL); |
Jyri Sarha | 29ddd6e | 2015-07-02 16:26:12 +0300 | [diff] [blame] | 165 | if (priv) |
| 166 | priv->saved_register = kcalloc(tilcdc_num_regs(), |
| 167 | sizeof(*priv->saved_register), |
| 168 | GFP_KERNEL); |
| 169 | if (!priv || !priv->saved_register) { |
| 170 | kfree(priv); |
Rob Clark | 16ea975 | 2013-01-08 15:04:28 -0600 | [diff] [blame] | 171 | dev_err(dev->dev, "failed to allocate private data\n"); |
| 172 | return -ENOMEM; |
| 173 | } |
| 174 | |
| 175 | dev->dev_private = priv; |
| 176 | |
Jyri Sarha | 103cd8b | 2015-02-10 14:13:23 +0200 | [diff] [blame] | 177 | priv->is_componentized = |
| 178 | tilcdc_get_external_components(dev->dev, NULL) > 0; |
| 179 | |
Rob Clark | 16ea975 | 2013-01-08 15:04:28 -0600 | [diff] [blame] | 180 | priv->wq = alloc_ordered_workqueue("tilcdc", 0); |
Ezequiel Garcia | b478e336b | 2014-09-02 09:51:15 -0300 | [diff] [blame] | 181 | if (!priv->wq) { |
| 182 | ret = -ENOMEM; |
| 183 | goto fail_free_priv; |
| 184 | } |
Rob Clark | 16ea975 | 2013-01-08 15:04:28 -0600 | [diff] [blame] | 185 | |
| 186 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 187 | if (!res) { |
| 188 | dev_err(dev->dev, "failed to get memory resource\n"); |
| 189 | ret = -EINVAL; |
Ezequiel Garcia | b478e336b | 2014-09-02 09:51:15 -0300 | [diff] [blame] | 190 | goto fail_free_wq; |
Rob Clark | 16ea975 | 2013-01-08 15:04:28 -0600 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | priv->mmio = ioremap_nocache(res->start, resource_size(res)); |
| 194 | if (!priv->mmio) { |
| 195 | dev_err(dev->dev, "failed to ioremap\n"); |
| 196 | ret = -ENOMEM; |
Ezequiel Garcia | b478e336b | 2014-09-02 09:51:15 -0300 | [diff] [blame] | 197 | goto fail_free_wq; |
Rob Clark | 16ea975 | 2013-01-08 15:04:28 -0600 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | priv->clk = clk_get(dev->dev, "fck"); |
| 201 | if (IS_ERR(priv->clk)) { |
| 202 | dev_err(dev->dev, "failed to get functional clock\n"); |
| 203 | ret = -ENODEV; |
Ezequiel Garcia | b478e336b | 2014-09-02 09:51:15 -0300 | [diff] [blame] | 204 | goto fail_iounmap; |
Rob Clark | 16ea975 | 2013-01-08 15:04:28 -0600 | [diff] [blame] | 205 | } |
| 206 | |
Rob Clark | 16ea975 | 2013-01-08 15:04:28 -0600 | [diff] [blame] | 207 | #ifdef CONFIG_CPU_FREQ |
| 208 | priv->lcd_fck_rate = clk_get_rate(priv->clk); |
| 209 | priv->freq_transition.notifier_call = cpufreq_transition; |
| 210 | ret = cpufreq_register_notifier(&priv->freq_transition, |
| 211 | CPUFREQ_TRANSITION_NOTIFIER); |
| 212 | if (ret) { |
| 213 | dev_err(dev->dev, "failed to register cpufreq notifier\n"); |
Darren Etheridge | 3d19306 | 2014-01-15 15:52:36 -0600 | [diff] [blame] | 214 | goto fail_put_clk; |
Rob Clark | 16ea975 | 2013-01-08 15:04:28 -0600 | [diff] [blame] | 215 | } |
| 216 | #endif |
| 217 | |
| 218 | if (of_property_read_u32(node, "max-bandwidth", &priv->max_bandwidth)) |
Darren Etheridge | 4e56434 | 2013-06-21 13:52:23 -0500 | [diff] [blame] | 219 | priv->max_bandwidth = TILCDC_DEFAULT_MAX_BANDWIDTH; |
| 220 | |
| 221 | DBG("Maximum Bandwidth Value %d", priv->max_bandwidth); |
| 222 | |
| 223 | if (of_property_read_u32(node, "ti,max-width", &priv->max_width)) |
| 224 | priv->max_width = TILCDC_DEFAULT_MAX_WIDTH; |
| 225 | |
| 226 | DBG("Maximum Horizontal Pixel Width Value %dpixels", priv->max_width); |
| 227 | |
| 228 | if (of_property_read_u32(node, "ti,max-pixelclock", |
| 229 | &priv->max_pixelclock)) |
| 230 | priv->max_pixelclock = TILCDC_DEFAULT_MAX_PIXELCLOCK; |
| 231 | |
| 232 | DBG("Maximum Pixel Clock Value %dKHz", priv->max_pixelclock); |
Rob Clark | 16ea975 | 2013-01-08 15:04:28 -0600 | [diff] [blame] | 233 | |
| 234 | pm_runtime_enable(dev->dev); |
| 235 | |
| 236 | /* Determine LCD IP Version */ |
| 237 | pm_runtime_get_sync(dev->dev); |
| 238 | switch (tilcdc_read(dev, LCDC_PID_REG)) { |
| 239 | case 0x4c100102: |
| 240 | priv->rev = 1; |
| 241 | break; |
| 242 | case 0x4f200800: |
| 243 | case 0x4f201000: |
| 244 | priv->rev = 2; |
| 245 | break; |
| 246 | default: |
| 247 | dev_warn(dev->dev, "Unknown PID Reg value 0x%08x, " |
| 248 | "defaulting to LCD revision 1\n", |
| 249 | tilcdc_read(dev, LCDC_PID_REG)); |
| 250 | priv->rev = 1; |
| 251 | break; |
| 252 | } |
| 253 | |
| 254 | pm_runtime_put_sync(dev->dev); |
| 255 | |
| 256 | ret = modeset_init(dev); |
| 257 | if (ret < 0) { |
| 258 | dev_err(dev->dev, "failed to initialize mode setting\n"); |
Ezequiel Garcia | b478e336b | 2014-09-02 09:51:15 -0300 | [diff] [blame] | 259 | goto fail_cpufreq_unregister; |
Rob Clark | 16ea975 | 2013-01-08 15:04:28 -0600 | [diff] [blame] | 260 | } |
| 261 | |
Jyri Sarha | 103cd8b | 2015-02-10 14:13:23 +0200 | [diff] [blame] | 262 | platform_set_drvdata(pdev, dev); |
| 263 | |
| 264 | if (priv->is_componentized) { |
| 265 | ret = component_bind_all(dev->dev, dev); |
| 266 | if (ret < 0) |
| 267 | goto fail_mode_config_cleanup; |
| 268 | |
| 269 | ret = tilcdc_add_external_encoders(dev, &bpp); |
| 270 | if (ret < 0) |
| 271 | goto fail_component_cleanup; |
| 272 | } |
| 273 | |
| 274 | if ((priv->num_encoders == 0) || (priv->num_connectors == 0)) { |
| 275 | dev_err(dev->dev, "no encoders/connectors found\n"); |
| 276 | ret = -ENXIO; |
| 277 | goto fail_external_cleanup; |
| 278 | } |
| 279 | |
Rob Clark | 16ea975 | 2013-01-08 15:04:28 -0600 | [diff] [blame] | 280 | ret = drm_vblank_init(dev, 1); |
| 281 | if (ret < 0) { |
| 282 | dev_err(dev->dev, "failed to initialize vblank\n"); |
Jyri Sarha | 103cd8b | 2015-02-10 14:13:23 +0200 | [diff] [blame] | 283 | goto fail_external_cleanup; |
Rob Clark | 16ea975 | 2013-01-08 15:04:28 -0600 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | pm_runtime_get_sync(dev->dev); |
Daniel Vetter | bb0f1b5 | 2013-11-03 21:09:27 +0100 | [diff] [blame] | 287 | ret = drm_irq_install(dev, platform_get_irq(dev->platformdev, 0)); |
Rob Clark | 16ea975 | 2013-01-08 15:04:28 -0600 | [diff] [blame] | 288 | pm_runtime_put_sync(dev->dev); |
| 289 | if (ret < 0) { |
| 290 | dev_err(dev->dev, "failed to install IRQ handler\n"); |
Ezequiel Garcia | b478e336b | 2014-09-02 09:51:15 -0300 | [diff] [blame] | 291 | goto fail_vblank_cleanup; |
Rob Clark | 16ea975 | 2013-01-08 15:04:28 -0600 | [diff] [blame] | 292 | } |
| 293 | |
Benoit Parrot | dc28aa0 | 2013-06-18 17:18:31 -0500 | [diff] [blame] | 294 | list_for_each_entry(mod, &module_list, list) { |
| 295 | DBG("%s: preferred_bpp: %d", mod->name, mod->preferred_bpp); |
| 296 | bpp = mod->preferred_bpp; |
| 297 | if (bpp > 0) |
| 298 | break; |
| 299 | } |
| 300 | |
Maxime Ripard | 4314e19 | 2016-01-14 16:24:56 +0100 | [diff] [blame] | 301 | drm_helper_disable_unused_functions(dev); |
Benoit Parrot | dc28aa0 | 2013-06-18 17:18:31 -0500 | [diff] [blame] | 302 | priv->fbdev = drm_fbdev_cma_init(dev, bpp, |
Rob Clark | 16ea975 | 2013-01-08 15:04:28 -0600 | [diff] [blame] | 303 | dev->mode_config.num_crtc, |
| 304 | dev->mode_config.num_connector); |
Ezequiel Garcia | b478e336b | 2014-09-02 09:51:15 -0300 | [diff] [blame] | 305 | if (IS_ERR(priv->fbdev)) { |
| 306 | ret = PTR_ERR(priv->fbdev); |
| 307 | goto fail_irq_uninstall; |
| 308 | } |
Rob Clark | 16ea975 | 2013-01-08 15:04:28 -0600 | [diff] [blame] | 309 | |
| 310 | drm_kms_helper_poll_init(dev); |
| 311 | |
| 312 | return 0; |
| 313 | |
Ezequiel Garcia | b478e336b | 2014-09-02 09:51:15 -0300 | [diff] [blame] | 314 | fail_irq_uninstall: |
| 315 | pm_runtime_get_sync(dev->dev); |
| 316 | drm_irq_uninstall(dev); |
| 317 | pm_runtime_put_sync(dev->dev); |
| 318 | |
| 319 | fail_vblank_cleanup: |
| 320 | drm_vblank_cleanup(dev); |
| 321 | |
| 322 | fail_mode_config_cleanup: |
| 323 | drm_mode_config_cleanup(dev); |
| 324 | |
Jyri Sarha | 103cd8b | 2015-02-10 14:13:23 +0200 | [diff] [blame] | 325 | fail_component_cleanup: |
| 326 | if (priv->is_componentized) |
| 327 | component_unbind_all(dev->dev, dev); |
| 328 | |
| 329 | fail_external_cleanup: |
| 330 | tilcdc_remove_external_encoders(dev); |
| 331 | |
Ezequiel Garcia | b478e336b | 2014-09-02 09:51:15 -0300 | [diff] [blame] | 332 | fail_cpufreq_unregister: |
| 333 | pm_runtime_disable(dev->dev); |
| 334 | #ifdef CONFIG_CPU_FREQ |
| 335 | cpufreq_unregister_notifier(&priv->freq_transition, |
| 336 | CPUFREQ_TRANSITION_NOTIFIER); |
Ezequiel Garcia | b478e336b | 2014-09-02 09:51:15 -0300 | [diff] [blame] | 337 | |
| 338 | fail_put_clk: |
Grygorii Strashko | 7974dff | 2015-02-25 18:19:43 +0200 | [diff] [blame] | 339 | #endif |
Ezequiel Garcia | b478e336b | 2014-09-02 09:51:15 -0300 | [diff] [blame] | 340 | clk_put(priv->clk); |
| 341 | |
| 342 | fail_iounmap: |
| 343 | iounmap(priv->mmio); |
| 344 | |
| 345 | fail_free_wq: |
| 346 | flush_workqueue(priv->wq); |
| 347 | destroy_workqueue(priv->wq); |
| 348 | |
| 349 | fail_free_priv: |
| 350 | dev->dev_private = NULL; |
Jyri Sarha | 29ddd6e | 2015-07-02 16:26:12 +0300 | [diff] [blame] | 351 | kfree(priv->saved_register); |
Ezequiel Garcia | b478e336b | 2014-09-02 09:51:15 -0300 | [diff] [blame] | 352 | kfree(priv); |
Rob Clark | 16ea975 | 2013-01-08 15:04:28 -0600 | [diff] [blame] | 353 | return ret; |
| 354 | } |
| 355 | |
Rob Clark | 16ea975 | 2013-01-08 15:04:28 -0600 | [diff] [blame] | 356 | static void tilcdc_lastclose(struct drm_device *dev) |
| 357 | { |
| 358 | struct tilcdc_drm_private *priv = dev->dev_private; |
| 359 | drm_fbdev_cma_restore_mode(priv->fbdev); |
| 360 | } |
| 361 | |
Daniel Vetter | e9f0d76 | 2013-12-11 11:34:42 +0100 | [diff] [blame] | 362 | static irqreturn_t tilcdc_irq(int irq, void *arg) |
Rob Clark | 16ea975 | 2013-01-08 15:04:28 -0600 | [diff] [blame] | 363 | { |
| 364 | struct drm_device *dev = arg; |
| 365 | struct tilcdc_drm_private *priv = dev->dev_private; |
| 366 | return tilcdc_crtc_irq(priv->crtc); |
| 367 | } |
| 368 | |
| 369 | static void tilcdc_irq_preinstall(struct drm_device *dev) |
| 370 | { |
| 371 | tilcdc_clear_irqstatus(dev, 0xffffffff); |
| 372 | } |
| 373 | |
| 374 | static int tilcdc_irq_postinstall(struct drm_device *dev) |
| 375 | { |
| 376 | struct tilcdc_drm_private *priv = dev->dev_private; |
| 377 | |
| 378 | /* enable FIFO underflow irq: */ |
Sachin Kamat | a50b24f | 2013-03-02 15:53:07 +0530 | [diff] [blame] | 379 | if (priv->rev == 1) |
Rob Clark | 16ea975 | 2013-01-08 15:04:28 -0600 | [diff] [blame] | 380 | tilcdc_set(dev, LCDC_RASTER_CTRL_REG, LCDC_V1_UNDERFLOW_INT_ENA); |
Sachin Kamat | a50b24f | 2013-03-02 15:53:07 +0530 | [diff] [blame] | 381 | else |
Darren Etheridge | b62222f | 2014-09-25 00:59:31 +0000 | [diff] [blame] | 382 | tilcdc_set(dev, LCDC_INT_ENABLE_SET_REG, |
| 383 | LCDC_V2_UNDERFLOW_INT_ENA | |
| 384 | LCDC_FRAME_DONE); |
Rob Clark | 16ea975 | 2013-01-08 15:04:28 -0600 | [diff] [blame] | 385 | |
| 386 | return 0; |
| 387 | } |
| 388 | |
| 389 | static void tilcdc_irq_uninstall(struct drm_device *dev) |
| 390 | { |
| 391 | struct tilcdc_drm_private *priv = dev->dev_private; |
| 392 | |
| 393 | /* disable irqs that we might have enabled: */ |
| 394 | if (priv->rev == 1) { |
| 395 | tilcdc_clear(dev, LCDC_RASTER_CTRL_REG, |
| 396 | LCDC_V1_UNDERFLOW_INT_ENA | LCDC_V1_PL_INT_ENA); |
| 397 | tilcdc_clear(dev, LCDC_DMA_CTRL_REG, LCDC_V1_END_OF_FRAME_INT_ENA); |
| 398 | } else { |
| 399 | tilcdc_clear(dev, LCDC_INT_ENABLE_SET_REG, |
| 400 | LCDC_V2_UNDERFLOW_INT_ENA | LCDC_V2_PL_INT_ENA | |
| 401 | LCDC_V2_END_OF_FRAME0_INT_ENA | LCDC_V2_END_OF_FRAME1_INT_ENA | |
| 402 | LCDC_FRAME_DONE); |
| 403 | } |
| 404 | |
| 405 | } |
| 406 | |
| 407 | static void enable_vblank(struct drm_device *dev, bool enable) |
| 408 | { |
| 409 | struct tilcdc_drm_private *priv = dev->dev_private; |
| 410 | u32 reg, mask; |
| 411 | |
| 412 | if (priv->rev == 1) { |
| 413 | reg = LCDC_DMA_CTRL_REG; |
| 414 | mask = LCDC_V1_END_OF_FRAME_INT_ENA; |
| 415 | } else { |
| 416 | reg = LCDC_INT_ENABLE_SET_REG; |
| 417 | mask = LCDC_V2_END_OF_FRAME0_INT_ENA | |
Darren Etheridge | b62222f | 2014-09-25 00:59:31 +0000 | [diff] [blame] | 418 | LCDC_V2_END_OF_FRAME1_INT_ENA; |
Rob Clark | 16ea975 | 2013-01-08 15:04:28 -0600 | [diff] [blame] | 419 | } |
| 420 | |
| 421 | if (enable) |
| 422 | tilcdc_set(dev, reg, mask); |
| 423 | else |
| 424 | tilcdc_clear(dev, reg, mask); |
| 425 | } |
| 426 | |
Thierry Reding | 88e7271 | 2015-09-24 18:35:31 +0200 | [diff] [blame] | 427 | static int tilcdc_enable_vblank(struct drm_device *dev, unsigned int pipe) |
Rob Clark | 16ea975 | 2013-01-08 15:04:28 -0600 | [diff] [blame] | 428 | { |
| 429 | enable_vblank(dev, true); |
| 430 | return 0; |
| 431 | } |
| 432 | |
Thierry Reding | 88e7271 | 2015-09-24 18:35:31 +0200 | [diff] [blame] | 433 | static void tilcdc_disable_vblank(struct drm_device *dev, unsigned int pipe) |
Rob Clark | 16ea975 | 2013-01-08 15:04:28 -0600 | [diff] [blame] | 434 | { |
| 435 | enable_vblank(dev, false); |
| 436 | } |
| 437 | |
| 438 | #if defined(CONFIG_DEBUG_FS) || defined(CONFIG_PM_SLEEP) |
| 439 | static const struct { |
| 440 | const char *name; |
| 441 | uint8_t rev; |
| 442 | uint8_t save; |
| 443 | uint32_t reg; |
Sachin Kamat | 3250145 | 2013-03-02 15:53:08 +0530 | [diff] [blame] | 444 | } registers[] = { |
Rob Clark | 16ea975 | 2013-01-08 15:04:28 -0600 | [diff] [blame] | 445 | #define REG(rev, save, reg) { #reg, rev, save, reg } |
| 446 | /* exists in revision 1: */ |
| 447 | REG(1, false, LCDC_PID_REG), |
| 448 | REG(1, true, LCDC_CTRL_REG), |
| 449 | REG(1, false, LCDC_STAT_REG), |
| 450 | REG(1, true, LCDC_RASTER_CTRL_REG), |
| 451 | REG(1, true, LCDC_RASTER_TIMING_0_REG), |
| 452 | REG(1, true, LCDC_RASTER_TIMING_1_REG), |
| 453 | REG(1, true, LCDC_RASTER_TIMING_2_REG), |
| 454 | REG(1, true, LCDC_DMA_CTRL_REG), |
| 455 | REG(1, true, LCDC_DMA_FB_BASE_ADDR_0_REG), |
| 456 | REG(1, true, LCDC_DMA_FB_CEILING_ADDR_0_REG), |
| 457 | REG(1, true, LCDC_DMA_FB_BASE_ADDR_1_REG), |
| 458 | REG(1, true, LCDC_DMA_FB_CEILING_ADDR_1_REG), |
| 459 | /* new in revision 2: */ |
| 460 | REG(2, false, LCDC_RAW_STAT_REG), |
| 461 | REG(2, false, LCDC_MASKED_STAT_REG), |
| 462 | REG(2, false, LCDC_INT_ENABLE_SET_REG), |
| 463 | REG(2, false, LCDC_INT_ENABLE_CLR_REG), |
| 464 | REG(2, false, LCDC_END_OF_INT_IND_REG), |
| 465 | REG(2, true, LCDC_CLK_ENABLE_REG), |
| 466 | REG(2, true, LCDC_INT_ENABLE_SET_REG), |
| 467 | #undef REG |
| 468 | }; |
Jyri Sarha | 29ddd6e | 2015-07-02 16:26:12 +0300 | [diff] [blame] | 469 | |
| 470 | static size_t tilcdc_num_regs(void) |
| 471 | { |
| 472 | return ARRAY_SIZE(registers); |
| 473 | } |
| 474 | #else |
| 475 | static size_t tilcdc_num_regs(void) |
| 476 | { |
| 477 | return 0; |
| 478 | } |
Rob Clark | 16ea975 | 2013-01-08 15:04:28 -0600 | [diff] [blame] | 479 | #endif |
| 480 | |
| 481 | #ifdef CONFIG_DEBUG_FS |
| 482 | static int tilcdc_regs_show(struct seq_file *m, void *arg) |
| 483 | { |
| 484 | struct drm_info_node *node = (struct drm_info_node *) m->private; |
| 485 | struct drm_device *dev = node->minor->dev; |
| 486 | struct tilcdc_drm_private *priv = dev->dev_private; |
| 487 | unsigned i; |
| 488 | |
| 489 | pm_runtime_get_sync(dev->dev); |
| 490 | |
| 491 | seq_printf(m, "revision: %d\n", priv->rev); |
| 492 | |
| 493 | for (i = 0; i < ARRAY_SIZE(registers); i++) |
| 494 | if (priv->rev >= registers[i].rev) |
| 495 | seq_printf(m, "%s:\t %08x\n", registers[i].name, |
| 496 | tilcdc_read(dev, registers[i].reg)); |
| 497 | |
| 498 | pm_runtime_put_sync(dev->dev); |
| 499 | |
| 500 | return 0; |
| 501 | } |
| 502 | |
| 503 | static int tilcdc_mm_show(struct seq_file *m, void *arg) |
| 504 | { |
| 505 | struct drm_info_node *node = (struct drm_info_node *) m->private; |
| 506 | struct drm_device *dev = node->minor->dev; |
Daniel Vetter | b04a590 | 2013-12-11 14:24:46 +0100 | [diff] [blame] | 507 | return drm_mm_dump_table(m, &dev->vma_offset_manager->vm_addr_space_mm); |
Rob Clark | 16ea975 | 2013-01-08 15:04:28 -0600 | [diff] [blame] | 508 | } |
| 509 | |
| 510 | static struct drm_info_list tilcdc_debugfs_list[] = { |
| 511 | { "regs", tilcdc_regs_show, 0 }, |
| 512 | { "mm", tilcdc_mm_show, 0 }, |
| 513 | { "fb", drm_fb_cma_debugfs_show, 0 }, |
| 514 | }; |
| 515 | |
| 516 | static int tilcdc_debugfs_init(struct drm_minor *minor) |
| 517 | { |
| 518 | struct drm_device *dev = minor->dev; |
| 519 | struct tilcdc_module *mod; |
| 520 | int ret; |
| 521 | |
| 522 | ret = drm_debugfs_create_files(tilcdc_debugfs_list, |
| 523 | ARRAY_SIZE(tilcdc_debugfs_list), |
| 524 | minor->debugfs_root, minor); |
| 525 | |
| 526 | list_for_each_entry(mod, &module_list, list) |
| 527 | if (mod->funcs->debugfs_init) |
| 528 | mod->funcs->debugfs_init(mod, minor); |
| 529 | |
| 530 | if (ret) { |
| 531 | dev_err(dev->dev, "could not install tilcdc_debugfs_list\n"); |
| 532 | return ret; |
| 533 | } |
| 534 | |
| 535 | return ret; |
| 536 | } |
| 537 | |
| 538 | static void tilcdc_debugfs_cleanup(struct drm_minor *minor) |
| 539 | { |
| 540 | struct tilcdc_module *mod; |
| 541 | drm_debugfs_remove_files(tilcdc_debugfs_list, |
| 542 | ARRAY_SIZE(tilcdc_debugfs_list), minor); |
| 543 | |
| 544 | list_for_each_entry(mod, &module_list, list) |
| 545 | if (mod->funcs->debugfs_cleanup) |
| 546 | mod->funcs->debugfs_cleanup(mod, minor); |
| 547 | } |
| 548 | #endif |
| 549 | |
| 550 | static const struct file_operations fops = { |
| 551 | .owner = THIS_MODULE, |
| 552 | .open = drm_open, |
| 553 | .release = drm_release, |
| 554 | .unlocked_ioctl = drm_ioctl, |
| 555 | #ifdef CONFIG_COMPAT |
| 556 | .compat_ioctl = drm_compat_ioctl, |
| 557 | #endif |
| 558 | .poll = drm_poll, |
| 559 | .read = drm_read, |
Rob Clark | 16ea975 | 2013-01-08 15:04:28 -0600 | [diff] [blame] | 560 | .llseek = no_llseek, |
| 561 | .mmap = drm_gem_cma_mmap, |
| 562 | }; |
| 563 | |
| 564 | static struct drm_driver tilcdc_driver = { |
Jyri Sarha | 9c15390 | 2015-06-23 14:31:17 +0300 | [diff] [blame] | 565 | .driver_features = (DRIVER_HAVE_IRQ | DRIVER_GEM | DRIVER_MODESET | |
| 566 | DRIVER_PRIME), |
Rob Clark | 16ea975 | 2013-01-08 15:04:28 -0600 | [diff] [blame] | 567 | .load = tilcdc_load, |
| 568 | .unload = tilcdc_unload, |
Rob Clark | 16ea975 | 2013-01-08 15:04:28 -0600 | [diff] [blame] | 569 | .lastclose = tilcdc_lastclose, |
David Herrmann | 915b4d1 | 2014-08-29 12:12:43 +0200 | [diff] [blame] | 570 | .set_busid = drm_platform_set_busid, |
Rob Clark | 16ea975 | 2013-01-08 15:04:28 -0600 | [diff] [blame] | 571 | .irq_handler = tilcdc_irq, |
| 572 | .irq_preinstall = tilcdc_irq_preinstall, |
| 573 | .irq_postinstall = tilcdc_irq_postinstall, |
| 574 | .irq_uninstall = tilcdc_irq_uninstall, |
Ville Syrjälä | b44f840 | 2015-09-30 16:46:48 +0300 | [diff] [blame] | 575 | .get_vblank_counter = drm_vblank_no_hw_counter, |
Rob Clark | 16ea975 | 2013-01-08 15:04:28 -0600 | [diff] [blame] | 576 | .enable_vblank = tilcdc_enable_vblank, |
| 577 | .disable_vblank = tilcdc_disable_vblank, |
| 578 | .gem_free_object = drm_gem_cma_free_object, |
| 579 | .gem_vm_ops = &drm_gem_cma_vm_ops, |
| 580 | .dumb_create = drm_gem_cma_dumb_create, |
| 581 | .dumb_map_offset = drm_gem_cma_dumb_map_offset, |
Daniel Vetter | 43387b3 | 2013-07-16 09:12:04 +0200 | [diff] [blame] | 582 | .dumb_destroy = drm_gem_dumb_destroy, |
Jyri Sarha | 9c15390 | 2015-06-23 14:31:17 +0300 | [diff] [blame] | 583 | |
| 584 | .prime_handle_to_fd = drm_gem_prime_handle_to_fd, |
| 585 | .prime_fd_to_handle = drm_gem_prime_fd_to_handle, |
| 586 | .gem_prime_import = drm_gem_prime_import, |
| 587 | .gem_prime_export = drm_gem_prime_export, |
| 588 | .gem_prime_get_sg_table = drm_gem_cma_prime_get_sg_table, |
| 589 | .gem_prime_import_sg_table = drm_gem_cma_prime_import_sg_table, |
| 590 | .gem_prime_vmap = drm_gem_cma_prime_vmap, |
| 591 | .gem_prime_vunmap = drm_gem_cma_prime_vunmap, |
| 592 | .gem_prime_mmap = drm_gem_cma_prime_mmap, |
Rob Clark | 16ea975 | 2013-01-08 15:04:28 -0600 | [diff] [blame] | 593 | #ifdef CONFIG_DEBUG_FS |
| 594 | .debugfs_init = tilcdc_debugfs_init, |
| 595 | .debugfs_cleanup = tilcdc_debugfs_cleanup, |
| 596 | #endif |
| 597 | .fops = &fops, |
| 598 | .name = "tilcdc", |
| 599 | .desc = "TI LCD Controller DRM", |
| 600 | .date = "20121205", |
| 601 | .major = 1, |
| 602 | .minor = 0, |
| 603 | }; |
| 604 | |
| 605 | /* |
| 606 | * Power management: |
| 607 | */ |
| 608 | |
| 609 | #ifdef CONFIG_PM_SLEEP |
| 610 | static int tilcdc_pm_suspend(struct device *dev) |
| 611 | { |
| 612 | struct drm_device *ddev = dev_get_drvdata(dev); |
| 613 | struct tilcdc_drm_private *priv = ddev->dev_private; |
| 614 | unsigned i, n = 0; |
| 615 | |
| 616 | drm_kms_helper_poll_disable(ddev); |
| 617 | |
Darren Etheridge | 85fd27f | 2014-09-19 01:42:57 +0000 | [diff] [blame] | 618 | /* Select sleep pin state */ |
| 619 | pinctrl_pm_select_sleep_state(dev); |
| 620 | |
| 621 | if (pm_runtime_suspended(dev)) { |
| 622 | priv->ctx_valid = false; |
| 623 | return 0; |
| 624 | } |
| 625 | |
Darren Etheridge | 614b3cfe | 2014-09-25 00:59:32 +0000 | [diff] [blame] | 626 | /* Disable the LCDC controller, to avoid locking up the PRCM */ |
| 627 | tilcdc_crtc_dpms(priv->crtc, DRM_MODE_DPMS_OFF); |
| 628 | |
Rob Clark | 16ea975 | 2013-01-08 15:04:28 -0600 | [diff] [blame] | 629 | /* Save register state: */ |
| 630 | for (i = 0; i < ARRAY_SIZE(registers); i++) |
| 631 | if (registers[i].save && (priv->rev >= registers[i].rev)) |
| 632 | priv->saved_register[n++] = tilcdc_read(ddev, registers[i].reg); |
| 633 | |
Darren Etheridge | 85fd27f | 2014-09-19 01:42:57 +0000 | [diff] [blame] | 634 | priv->ctx_valid = true; |
Dave Gerlach | 416a07f | 2014-07-29 06:27:58 +0000 | [diff] [blame] | 635 | |
Rob Clark | 16ea975 | 2013-01-08 15:04:28 -0600 | [diff] [blame] | 636 | return 0; |
| 637 | } |
| 638 | |
| 639 | static int tilcdc_pm_resume(struct device *dev) |
| 640 | { |
| 641 | struct drm_device *ddev = dev_get_drvdata(dev); |
| 642 | struct tilcdc_drm_private *priv = ddev->dev_private; |
| 643 | unsigned i, n = 0; |
| 644 | |
Dave Gerlach | 416a07f | 2014-07-29 06:27:58 +0000 | [diff] [blame] | 645 | /* Select default pin state */ |
| 646 | pinctrl_pm_select_default_state(dev); |
| 647 | |
Darren Etheridge | 85fd27f | 2014-09-19 01:42:57 +0000 | [diff] [blame] | 648 | if (priv->ctx_valid == true) { |
| 649 | /* Restore register state: */ |
| 650 | for (i = 0; i < ARRAY_SIZE(registers); i++) |
| 651 | if (registers[i].save && |
| 652 | (priv->rev >= registers[i].rev)) |
| 653 | tilcdc_write(ddev, registers[i].reg, |
| 654 | priv->saved_register[n++]); |
| 655 | } |
Rob Clark | 16ea975 | 2013-01-08 15:04:28 -0600 | [diff] [blame] | 656 | |
| 657 | drm_kms_helper_poll_enable(ddev); |
| 658 | |
| 659 | return 0; |
| 660 | } |
| 661 | #endif |
| 662 | |
| 663 | static const struct dev_pm_ops tilcdc_pm_ops = { |
| 664 | SET_SYSTEM_SLEEP_PM_OPS(tilcdc_pm_suspend, tilcdc_pm_resume) |
| 665 | }; |
| 666 | |
| 667 | /* |
| 668 | * Platform driver: |
| 669 | */ |
| 670 | |
Jyri Sarha | 103cd8b | 2015-02-10 14:13:23 +0200 | [diff] [blame] | 671 | static int tilcdc_bind(struct device *dev) |
| 672 | { |
| 673 | return drm_platform_init(&tilcdc_driver, to_platform_device(dev)); |
| 674 | } |
| 675 | |
| 676 | static void tilcdc_unbind(struct device *dev) |
| 677 | { |
| 678 | drm_put_dev(dev_get_drvdata(dev)); |
| 679 | } |
| 680 | |
| 681 | static const struct component_master_ops tilcdc_comp_ops = { |
| 682 | .bind = tilcdc_bind, |
| 683 | .unbind = tilcdc_unbind, |
| 684 | }; |
| 685 | |
Rob Clark | 16ea975 | 2013-01-08 15:04:28 -0600 | [diff] [blame] | 686 | static int tilcdc_pdev_probe(struct platform_device *pdev) |
| 687 | { |
Jyri Sarha | 103cd8b | 2015-02-10 14:13:23 +0200 | [diff] [blame] | 688 | struct component_match *match = NULL; |
| 689 | int ret; |
| 690 | |
Rob Clark | 16ea975 | 2013-01-08 15:04:28 -0600 | [diff] [blame] | 691 | /* bail out early if no DT data: */ |
| 692 | if (!pdev->dev.of_node) { |
| 693 | dev_err(&pdev->dev, "device-tree data is missing\n"); |
| 694 | return -ENXIO; |
| 695 | } |
| 696 | |
Jyri Sarha | 103cd8b | 2015-02-10 14:13:23 +0200 | [diff] [blame] | 697 | ret = tilcdc_get_external_components(&pdev->dev, &match); |
| 698 | if (ret < 0) |
| 699 | return ret; |
| 700 | else if (ret == 0) |
| 701 | return drm_platform_init(&tilcdc_driver, pdev); |
| 702 | else |
| 703 | return component_master_add_with_match(&pdev->dev, |
| 704 | &tilcdc_comp_ops, |
| 705 | match); |
Rob Clark | 16ea975 | 2013-01-08 15:04:28 -0600 | [diff] [blame] | 706 | } |
| 707 | |
| 708 | static int tilcdc_pdev_remove(struct platform_device *pdev) |
| 709 | { |
Jyri Sarha | 103cd8b | 2015-02-10 14:13:23 +0200 | [diff] [blame] | 710 | struct drm_device *ddev = dev_get_drvdata(&pdev->dev); |
| 711 | struct tilcdc_drm_private *priv = ddev->dev_private; |
| 712 | |
| 713 | /* Check if a subcomponent has already triggered the unloading. */ |
| 714 | if (!priv) |
| 715 | return 0; |
| 716 | |
| 717 | if (priv->is_componentized) |
| 718 | component_master_del(&pdev->dev, &tilcdc_comp_ops); |
| 719 | else |
| 720 | drm_put_dev(platform_get_drvdata(pdev)); |
Rob Clark | 16ea975 | 2013-01-08 15:04:28 -0600 | [diff] [blame] | 721 | |
| 722 | return 0; |
| 723 | } |
| 724 | |
| 725 | static struct of_device_id tilcdc_of_match[] = { |
| 726 | { .compatible = "ti,am33xx-tilcdc", }, |
| 727 | { }, |
| 728 | }; |
| 729 | MODULE_DEVICE_TABLE(of, tilcdc_of_match); |
| 730 | |
| 731 | static struct platform_driver tilcdc_platform_driver = { |
| 732 | .probe = tilcdc_pdev_probe, |
| 733 | .remove = tilcdc_pdev_remove, |
| 734 | .driver = { |
Rob Clark | 16ea975 | 2013-01-08 15:04:28 -0600 | [diff] [blame] | 735 | .name = "tilcdc", |
| 736 | .pm = &tilcdc_pm_ops, |
| 737 | .of_match_table = tilcdc_of_match, |
| 738 | }, |
| 739 | }; |
| 740 | |
| 741 | static int __init tilcdc_drm_init(void) |
| 742 | { |
| 743 | DBG("init"); |
| 744 | tilcdc_tfp410_init(); |
Rob Clark | 0d4bbaf | 2012-12-18 17:34:16 -0600 | [diff] [blame] | 745 | tilcdc_panel_init(); |
Rob Clark | 16ea975 | 2013-01-08 15:04:28 -0600 | [diff] [blame] | 746 | return platform_driver_register(&tilcdc_platform_driver); |
| 747 | } |
| 748 | |
| 749 | static void __exit tilcdc_drm_fini(void) |
| 750 | { |
| 751 | DBG("fini"); |
Rob Clark | 16ea975 | 2013-01-08 15:04:28 -0600 | [diff] [blame] | 752 | platform_driver_unregister(&tilcdc_platform_driver); |
Guido Martínez | eb565a2 | 2014-06-17 11:17:08 -0300 | [diff] [blame] | 753 | tilcdc_panel_fini(); |
Guido Martínez | eb565a2 | 2014-06-17 11:17:08 -0300 | [diff] [blame] | 754 | tilcdc_tfp410_fini(); |
Rob Clark | 16ea975 | 2013-01-08 15:04:28 -0600 | [diff] [blame] | 755 | } |
| 756 | |
Guido Martínez | 2023d84 | 2014-06-17 11:17:11 -0300 | [diff] [blame] | 757 | module_init(tilcdc_drm_init); |
Rob Clark | 16ea975 | 2013-01-08 15:04:28 -0600 | [diff] [blame] | 758 | module_exit(tilcdc_drm_fini); |
| 759 | |
| 760 | MODULE_AUTHOR("Rob Clark <robdclark@gmail.com"); |
| 761 | MODULE_DESCRIPTION("TI LCD Controller DRM Driver"); |
| 762 | MODULE_LICENSE("GPL"); |