blob: e1f697986289e83ea24c1b17d3f59c84aa4e8001 [file] [log] [blame]
Rob Clark16ea9752013-01-08 15:04:28 -06001/*
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 Sarha103cd8b2015-02-10 14:13:23 +020020#include <linux/component.h>
Dave Gerlach416a07f2014-07-29 06:27:58 +000021#include <linux/pinctrl/consumer.h>
22#include <linux/suspend.h>
Jyri Sarha103cd8b2015-02-10 14:13:23 +020023
Rob Clark16ea9752013-01-08 15:04:28 -060024#include "tilcdc_drv.h"
25#include "tilcdc_regs.h"
26#include "tilcdc_tfp410.h"
Rob Clark0d4bbaf2012-12-18 17:34:16 -060027#include "tilcdc_panel.h"
Jyri Sarha103cd8b2015-02-10 14:13:23 +020028#include "tilcdc_external.h"
Rob Clark16ea9752013-01-08 15:04:28 -060029
30#include "drm_fb_helper.h"
31
32static LIST_HEAD(module_list);
33
34void 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
43void tilcdc_module_cleanup(struct tilcdc_module *mod)
44{
45 list_del(&mod->list);
46}
47
48static struct of_device_id tilcdc_of_match[];
49
50static struct drm_framebuffer *tilcdc_fb_create(struct drm_device *dev,
Ville Syrjälä1eb83452015-11-11 19:11:29 +020051 struct drm_file *file_priv, const struct drm_mode_fb_cmd2 *mode_cmd)
Rob Clark16ea9752013-01-08 15:04:28 -060052{
53 return drm_fb_cma_create(dev, file_priv, mode_cmd);
54}
55
56static void tilcdc_fb_output_poll_changed(struct drm_device *dev)
57{
58 struct tilcdc_drm_private *priv = dev->dev_private;
Markus Elfringc08448172014-11-19 17:05:20 +010059 drm_fbdev_cma_hotplug_event(priv->fbdev);
Rob Clark16ea9752013-01-08 15:04:28 -060060}
61
62static 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
67static 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 Clark16ea9752013-01-08 15:04:28 -060081 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
91static 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
111static int tilcdc_unload(struct drm_device *dev)
112{
113 struct tilcdc_drm_private *priv = dev->dev_private;
Rob Clark16ea9752013-01-08 15:04:28 -0600114
Tomi Valkeinen1aea1e72015-10-19 14:15:26 +0300115 tilcdc_crtc_dpms(priv->crtc, DRM_MODE_DPMS_OFF);
116
Jyri Sarha103cd8b2015-02-10 14:13:23 +0200117 tilcdc_remove_external_encoders(dev);
118
Guido Martínez3a490122014-06-17 11:17:07 -0300119 drm_fbdev_cma_fini(priv->fbdev);
Rob Clark16ea9752013-01-08 15:04:28 -0600120 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 Sarha29ddd6e2015-07-02 16:26:12 +0300146 kfree(priv->saved_register);
Rob Clark16ea9752013-01-08 15:04:28 -0600147 kfree(priv);
148
149 return 0;
150}
151
Jyri Sarha29ddd6e2015-07-02 16:26:12 +0300152static size_t tilcdc_num_regs(void);
153
Rob Clark16ea9752013-01-08 15:04:28 -0600154static 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 Parrotdc28aa02013-06-18 17:18:31 -0500159 struct tilcdc_module *mod;
Rob Clark16ea9752013-01-08 15:04:28 -0600160 struct resource *res;
Benoit Parrotdc28aa02013-06-18 17:18:31 -0500161 u32 bpp = 0;
Rob Clark16ea9752013-01-08 15:04:28 -0600162 int ret;
163
164 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
Jyri Sarha29ddd6e2015-07-02 16:26:12 +0300165 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 Clark16ea9752013-01-08 15:04:28 -0600171 dev_err(dev->dev, "failed to allocate private data\n");
172 return -ENOMEM;
173 }
174
175 dev->dev_private = priv;
176
Jyri Sarha103cd8b2015-02-10 14:13:23 +0200177 priv->is_componentized =
178 tilcdc_get_external_components(dev->dev, NULL) > 0;
179
Rob Clark16ea9752013-01-08 15:04:28 -0600180 priv->wq = alloc_ordered_workqueue("tilcdc", 0);
Ezequiel Garciab478e336b2014-09-02 09:51:15 -0300181 if (!priv->wq) {
182 ret = -ENOMEM;
183 goto fail_free_priv;
184 }
Rob Clark16ea9752013-01-08 15:04:28 -0600185
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 Garciab478e336b2014-09-02 09:51:15 -0300190 goto fail_free_wq;
Rob Clark16ea9752013-01-08 15:04:28 -0600191 }
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 Garciab478e336b2014-09-02 09:51:15 -0300197 goto fail_free_wq;
Rob Clark16ea9752013-01-08 15:04:28 -0600198 }
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 Garciab478e336b2014-09-02 09:51:15 -0300204 goto fail_iounmap;
Rob Clark16ea9752013-01-08 15:04:28 -0600205 }
206
Rob Clark16ea9752013-01-08 15:04:28 -0600207#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 Etheridge3d193062014-01-15 15:52:36 -0600214 goto fail_put_clk;
Rob Clark16ea9752013-01-08 15:04:28 -0600215 }
216#endif
217
218 if (of_property_read_u32(node, "max-bandwidth", &priv->max_bandwidth))
Darren Etheridge4e564342013-06-21 13:52:23 -0500219 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 Clark16ea9752013-01-08 15:04:28 -0600233
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 Garciab478e336b2014-09-02 09:51:15 -0300259 goto fail_cpufreq_unregister;
Rob Clark16ea9752013-01-08 15:04:28 -0600260 }
261
Jyri Sarha103cd8b2015-02-10 14:13:23 +0200262 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 Clark16ea9752013-01-08 15:04:28 -0600280 ret = drm_vblank_init(dev, 1);
281 if (ret < 0) {
282 dev_err(dev->dev, "failed to initialize vblank\n");
Jyri Sarha103cd8b2015-02-10 14:13:23 +0200283 goto fail_external_cleanup;
Rob Clark16ea9752013-01-08 15:04:28 -0600284 }
285
286 pm_runtime_get_sync(dev->dev);
Daniel Vetterbb0f1b52013-11-03 21:09:27 +0100287 ret = drm_irq_install(dev, platform_get_irq(dev->platformdev, 0));
Rob Clark16ea9752013-01-08 15:04:28 -0600288 pm_runtime_put_sync(dev->dev);
289 if (ret < 0) {
290 dev_err(dev->dev, "failed to install IRQ handler\n");
Ezequiel Garciab478e336b2014-09-02 09:51:15 -0300291 goto fail_vblank_cleanup;
Rob Clark16ea9752013-01-08 15:04:28 -0600292 }
293
Benoit Parrotdc28aa02013-06-18 17:18:31 -0500294 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 Ripard4314e192016-01-14 16:24:56 +0100301 drm_helper_disable_unused_functions(dev);
Benoit Parrotdc28aa02013-06-18 17:18:31 -0500302 priv->fbdev = drm_fbdev_cma_init(dev, bpp,
Rob Clark16ea9752013-01-08 15:04:28 -0600303 dev->mode_config.num_crtc,
304 dev->mode_config.num_connector);
Ezequiel Garciab478e336b2014-09-02 09:51:15 -0300305 if (IS_ERR(priv->fbdev)) {
306 ret = PTR_ERR(priv->fbdev);
307 goto fail_irq_uninstall;
308 }
Rob Clark16ea9752013-01-08 15:04:28 -0600309
310 drm_kms_helper_poll_init(dev);
311
312 return 0;
313
Ezequiel Garciab478e336b2014-09-02 09:51:15 -0300314fail_irq_uninstall:
315 pm_runtime_get_sync(dev->dev);
316 drm_irq_uninstall(dev);
317 pm_runtime_put_sync(dev->dev);
318
319fail_vblank_cleanup:
320 drm_vblank_cleanup(dev);
321
322fail_mode_config_cleanup:
323 drm_mode_config_cleanup(dev);
324
Jyri Sarha103cd8b2015-02-10 14:13:23 +0200325fail_component_cleanup:
326 if (priv->is_componentized)
327 component_unbind_all(dev->dev, dev);
328
329fail_external_cleanup:
330 tilcdc_remove_external_encoders(dev);
331
Ezequiel Garciab478e336b2014-09-02 09:51:15 -0300332fail_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 Garciab478e336b2014-09-02 09:51:15 -0300337
338fail_put_clk:
Grygorii Strashko7974dff2015-02-25 18:19:43 +0200339#endif
Ezequiel Garciab478e336b2014-09-02 09:51:15 -0300340 clk_put(priv->clk);
341
342fail_iounmap:
343 iounmap(priv->mmio);
344
345fail_free_wq:
346 flush_workqueue(priv->wq);
347 destroy_workqueue(priv->wq);
348
349fail_free_priv:
350 dev->dev_private = NULL;
Jyri Sarha29ddd6e2015-07-02 16:26:12 +0300351 kfree(priv->saved_register);
Ezequiel Garciab478e336b2014-09-02 09:51:15 -0300352 kfree(priv);
Rob Clark16ea9752013-01-08 15:04:28 -0600353 return ret;
354}
355
Rob Clark16ea9752013-01-08 15:04:28 -0600356static 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 Vettere9f0d762013-12-11 11:34:42 +0100362static irqreturn_t tilcdc_irq(int irq, void *arg)
Rob Clark16ea9752013-01-08 15:04:28 -0600363{
364 struct drm_device *dev = arg;
365 struct tilcdc_drm_private *priv = dev->dev_private;
366 return tilcdc_crtc_irq(priv->crtc);
367}
368
369static void tilcdc_irq_preinstall(struct drm_device *dev)
370{
371 tilcdc_clear_irqstatus(dev, 0xffffffff);
372}
373
374static 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 Kamata50b24f2013-03-02 15:53:07 +0530379 if (priv->rev == 1)
Rob Clark16ea9752013-01-08 15:04:28 -0600380 tilcdc_set(dev, LCDC_RASTER_CTRL_REG, LCDC_V1_UNDERFLOW_INT_ENA);
Sachin Kamata50b24f2013-03-02 15:53:07 +0530381 else
Darren Etheridgeb62222f2014-09-25 00:59:31 +0000382 tilcdc_set(dev, LCDC_INT_ENABLE_SET_REG,
383 LCDC_V2_UNDERFLOW_INT_ENA |
384 LCDC_FRAME_DONE);
Rob Clark16ea9752013-01-08 15:04:28 -0600385
386 return 0;
387}
388
389static 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
407static 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 Etheridgeb62222f2014-09-25 00:59:31 +0000418 LCDC_V2_END_OF_FRAME1_INT_ENA;
Rob Clark16ea9752013-01-08 15:04:28 -0600419 }
420
421 if (enable)
422 tilcdc_set(dev, reg, mask);
423 else
424 tilcdc_clear(dev, reg, mask);
425}
426
Thierry Reding88e72712015-09-24 18:35:31 +0200427static int tilcdc_enable_vblank(struct drm_device *dev, unsigned int pipe)
Rob Clark16ea9752013-01-08 15:04:28 -0600428{
429 enable_vblank(dev, true);
430 return 0;
431}
432
Thierry Reding88e72712015-09-24 18:35:31 +0200433static void tilcdc_disable_vblank(struct drm_device *dev, unsigned int pipe)
Rob Clark16ea9752013-01-08 15:04:28 -0600434{
435 enable_vblank(dev, false);
436}
437
438#if defined(CONFIG_DEBUG_FS) || defined(CONFIG_PM_SLEEP)
439static const struct {
440 const char *name;
441 uint8_t rev;
442 uint8_t save;
443 uint32_t reg;
Sachin Kamat32501452013-03-02 15:53:08 +0530444} registers[] = {
Rob Clark16ea9752013-01-08 15:04:28 -0600445#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 Sarha29ddd6e2015-07-02 16:26:12 +0300469
470static size_t tilcdc_num_regs(void)
471{
472 return ARRAY_SIZE(registers);
473}
474#else
475static size_t tilcdc_num_regs(void)
476{
477 return 0;
478}
Rob Clark16ea9752013-01-08 15:04:28 -0600479#endif
480
481#ifdef CONFIG_DEBUG_FS
482static 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
503static 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 Vetterb04a5902013-12-11 14:24:46 +0100507 return drm_mm_dump_table(m, &dev->vma_offset_manager->vm_addr_space_mm);
Rob Clark16ea9752013-01-08 15:04:28 -0600508}
509
510static 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
516static 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
538static 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
550static 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 Clark16ea9752013-01-08 15:04:28 -0600560 .llseek = no_llseek,
561 .mmap = drm_gem_cma_mmap,
562};
563
564static struct drm_driver tilcdc_driver = {
Jyri Sarha9c153902015-06-23 14:31:17 +0300565 .driver_features = (DRIVER_HAVE_IRQ | DRIVER_GEM | DRIVER_MODESET |
566 DRIVER_PRIME),
Rob Clark16ea9752013-01-08 15:04:28 -0600567 .load = tilcdc_load,
568 .unload = tilcdc_unload,
Rob Clark16ea9752013-01-08 15:04:28 -0600569 .lastclose = tilcdc_lastclose,
David Herrmann915b4d12014-08-29 12:12:43 +0200570 .set_busid = drm_platform_set_busid,
Rob Clark16ea9752013-01-08 15:04:28 -0600571 .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äb44f8402015-09-30 16:46:48 +0300575 .get_vblank_counter = drm_vblank_no_hw_counter,
Rob Clark16ea9752013-01-08 15:04:28 -0600576 .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 Vetter43387b32013-07-16 09:12:04 +0200582 .dumb_destroy = drm_gem_dumb_destroy,
Jyri Sarha9c153902015-06-23 14:31:17 +0300583
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 Clark16ea9752013-01-08 15:04:28 -0600593#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
610static 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 Etheridge85fd27f2014-09-19 01:42:57 +0000618 /* 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 Etheridge614b3cfe2014-09-25 00:59:32 +0000626 /* Disable the LCDC controller, to avoid locking up the PRCM */
627 tilcdc_crtc_dpms(priv->crtc, DRM_MODE_DPMS_OFF);
628
Rob Clark16ea9752013-01-08 15:04:28 -0600629 /* 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 Etheridge85fd27f2014-09-19 01:42:57 +0000634 priv->ctx_valid = true;
Dave Gerlach416a07f2014-07-29 06:27:58 +0000635
Rob Clark16ea9752013-01-08 15:04:28 -0600636 return 0;
637}
638
639static 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 Gerlach416a07f2014-07-29 06:27:58 +0000645 /* Select default pin state */
646 pinctrl_pm_select_default_state(dev);
647
Darren Etheridge85fd27f2014-09-19 01:42:57 +0000648 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 Clark16ea9752013-01-08 15:04:28 -0600656
657 drm_kms_helper_poll_enable(ddev);
658
659 return 0;
660}
661#endif
662
663static 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 Sarha103cd8b2015-02-10 14:13:23 +0200671static int tilcdc_bind(struct device *dev)
672{
673 return drm_platform_init(&tilcdc_driver, to_platform_device(dev));
674}
675
676static void tilcdc_unbind(struct device *dev)
677{
678 drm_put_dev(dev_get_drvdata(dev));
679}
680
681static const struct component_master_ops tilcdc_comp_ops = {
682 .bind = tilcdc_bind,
683 .unbind = tilcdc_unbind,
684};
685
Rob Clark16ea9752013-01-08 15:04:28 -0600686static int tilcdc_pdev_probe(struct platform_device *pdev)
687{
Jyri Sarha103cd8b2015-02-10 14:13:23 +0200688 struct component_match *match = NULL;
689 int ret;
690
Rob Clark16ea9752013-01-08 15:04:28 -0600691 /* 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 Sarha103cd8b2015-02-10 14:13:23 +0200697 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 Clark16ea9752013-01-08 15:04:28 -0600706}
707
708static int tilcdc_pdev_remove(struct platform_device *pdev)
709{
Jyri Sarha103cd8b2015-02-10 14:13:23 +0200710 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 Clark16ea9752013-01-08 15:04:28 -0600721
722 return 0;
723}
724
725static struct of_device_id tilcdc_of_match[] = {
726 { .compatible = "ti,am33xx-tilcdc", },
727 { },
728};
729MODULE_DEVICE_TABLE(of, tilcdc_of_match);
730
731static struct platform_driver tilcdc_platform_driver = {
732 .probe = tilcdc_pdev_probe,
733 .remove = tilcdc_pdev_remove,
734 .driver = {
Rob Clark16ea9752013-01-08 15:04:28 -0600735 .name = "tilcdc",
736 .pm = &tilcdc_pm_ops,
737 .of_match_table = tilcdc_of_match,
738 },
739};
740
741static int __init tilcdc_drm_init(void)
742{
743 DBG("init");
744 tilcdc_tfp410_init();
Rob Clark0d4bbaf2012-12-18 17:34:16 -0600745 tilcdc_panel_init();
Rob Clark16ea9752013-01-08 15:04:28 -0600746 return platform_driver_register(&tilcdc_platform_driver);
747}
748
749static void __exit tilcdc_drm_fini(void)
750{
751 DBG("fini");
Rob Clark16ea9752013-01-08 15:04:28 -0600752 platform_driver_unregister(&tilcdc_platform_driver);
Guido Martínezeb565a22014-06-17 11:17:08 -0300753 tilcdc_panel_fini();
Guido Martínezeb565a22014-06-17 11:17:08 -0300754 tilcdc_tfp410_fini();
Rob Clark16ea9752013-01-08 15:04:28 -0600755}
756
Guido Martínez2023d842014-06-17 11:17:11 -0300757module_init(tilcdc_drm_init);
Rob Clark16ea9752013-01-08 15:04:28 -0600758module_exit(tilcdc_drm_fini);
759
760MODULE_AUTHOR("Rob Clark <robdclark@gmail.com");
761MODULE_DESCRIPTION("TI LCD Controller DRM Driver");
762MODULE_LICENSE("GPL");