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