blob: d96083d62e1134009bafb1acc3b47c7d88894faa [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: */
Jyri Sarha947df7e32015-12-09 12:16:11 +0200379 if (priv->rev == 1) {
Rob Clark16ea9752013-01-08 15:04:28 -0600380 tilcdc_set(dev, LCDC_RASTER_CTRL_REG, LCDC_V1_UNDERFLOW_INT_ENA);
Jyri Sarha947df7e32015-12-09 12:16:11 +0200381 } else {
382 tilcdc_write(dev, LCDC_INT_ENABLE_SET_REG,
Darren Etheridgeb62222f2014-09-25 00:59:31 +0000383 LCDC_V2_UNDERFLOW_INT_ENA |
Tomi Valkeinen2b2080d2015-10-20 09:37:27 +0300384 LCDC_V2_END_OF_FRAME0_INT_ENA |
Darren Etheridgeb62222f2014-09-25 00:59:31 +0000385 LCDC_FRAME_DONE);
Jyri Sarha947df7e32015-12-09 12:16:11 +0200386 }
Rob Clark16ea9752013-01-08 15:04:28 -0600387
388 return 0;
389}
390
391static void tilcdc_irq_uninstall(struct drm_device *dev)
392{
393 struct tilcdc_drm_private *priv = dev->dev_private;
394
395 /* disable irqs that we might have enabled: */
396 if (priv->rev == 1) {
397 tilcdc_clear(dev, LCDC_RASTER_CTRL_REG,
398 LCDC_V1_UNDERFLOW_INT_ENA | LCDC_V1_PL_INT_ENA);
399 tilcdc_clear(dev, LCDC_DMA_CTRL_REG, LCDC_V1_END_OF_FRAME_INT_ENA);
400 } else {
Jyri Sarha947df7e32015-12-09 12:16:11 +0200401 tilcdc_write(dev, LCDC_INT_ENABLE_CLR_REG,
Rob Clark16ea9752013-01-08 15:04:28 -0600402 LCDC_V2_UNDERFLOW_INT_ENA | LCDC_V2_PL_INT_ENA |
Tomi Valkeinen2b2080d2015-10-20 09:37:27 +0300403 LCDC_V2_END_OF_FRAME0_INT_ENA |
Rob Clark16ea9752013-01-08 15:04:28 -0600404 LCDC_FRAME_DONE);
405 }
Rob Clark16ea9752013-01-08 15:04:28 -0600406}
407
Thierry Reding88e72712015-09-24 18:35:31 +0200408static int tilcdc_enable_vblank(struct drm_device *dev, unsigned int pipe)
Rob Clark16ea9752013-01-08 15:04:28 -0600409{
Rob Clark16ea9752013-01-08 15:04:28 -0600410 return 0;
411}
412
Thierry Reding88e72712015-09-24 18:35:31 +0200413static void tilcdc_disable_vblank(struct drm_device *dev, unsigned int pipe)
Rob Clark16ea9752013-01-08 15:04:28 -0600414{
Tomi Valkeinen2b2080d2015-10-20 09:37:27 +0300415 return;
Rob Clark16ea9752013-01-08 15:04:28 -0600416}
417
418#if defined(CONFIG_DEBUG_FS) || defined(CONFIG_PM_SLEEP)
419static const struct {
420 const char *name;
421 uint8_t rev;
422 uint8_t save;
423 uint32_t reg;
Sachin Kamat32501452013-03-02 15:53:08 +0530424} registers[] = {
Rob Clark16ea9752013-01-08 15:04:28 -0600425#define REG(rev, save, reg) { #reg, rev, save, reg }
426 /* exists in revision 1: */
427 REG(1, false, LCDC_PID_REG),
428 REG(1, true, LCDC_CTRL_REG),
429 REG(1, false, LCDC_STAT_REG),
430 REG(1, true, LCDC_RASTER_CTRL_REG),
431 REG(1, true, LCDC_RASTER_TIMING_0_REG),
432 REG(1, true, LCDC_RASTER_TIMING_1_REG),
433 REG(1, true, LCDC_RASTER_TIMING_2_REG),
434 REG(1, true, LCDC_DMA_CTRL_REG),
435 REG(1, true, LCDC_DMA_FB_BASE_ADDR_0_REG),
436 REG(1, true, LCDC_DMA_FB_CEILING_ADDR_0_REG),
437 REG(1, true, LCDC_DMA_FB_BASE_ADDR_1_REG),
438 REG(1, true, LCDC_DMA_FB_CEILING_ADDR_1_REG),
439 /* new in revision 2: */
440 REG(2, false, LCDC_RAW_STAT_REG),
441 REG(2, false, LCDC_MASKED_STAT_REG),
Jyri Sarhaf3a99942016-01-08 12:17:50 +0200442 REG(2, true, LCDC_INT_ENABLE_SET_REG),
Rob Clark16ea9752013-01-08 15:04:28 -0600443 REG(2, false, LCDC_INT_ENABLE_CLR_REG),
444 REG(2, false, LCDC_END_OF_INT_IND_REG),
445 REG(2, true, LCDC_CLK_ENABLE_REG),
Rob Clark16ea9752013-01-08 15:04:28 -0600446#undef REG
447};
Jyri Sarha29ddd6e2015-07-02 16:26:12 +0300448
449static size_t tilcdc_num_regs(void)
450{
451 return ARRAY_SIZE(registers);
452}
453#else
454static size_t tilcdc_num_regs(void)
455{
456 return 0;
457}
Rob Clark16ea9752013-01-08 15:04:28 -0600458#endif
459
460#ifdef CONFIG_DEBUG_FS
461static int tilcdc_regs_show(struct seq_file *m, void *arg)
462{
463 struct drm_info_node *node = (struct drm_info_node *) m->private;
464 struct drm_device *dev = node->minor->dev;
465 struct tilcdc_drm_private *priv = dev->dev_private;
466 unsigned i;
467
468 pm_runtime_get_sync(dev->dev);
469
470 seq_printf(m, "revision: %d\n", priv->rev);
471
472 for (i = 0; i < ARRAY_SIZE(registers); i++)
473 if (priv->rev >= registers[i].rev)
474 seq_printf(m, "%s:\t %08x\n", registers[i].name,
475 tilcdc_read(dev, registers[i].reg));
476
477 pm_runtime_put_sync(dev->dev);
478
479 return 0;
480}
481
482static int tilcdc_mm_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;
Daniel Vetterb04a5902013-12-11 14:24:46 +0100486 return drm_mm_dump_table(m, &dev->vma_offset_manager->vm_addr_space_mm);
Rob Clark16ea9752013-01-08 15:04:28 -0600487}
488
489static struct drm_info_list tilcdc_debugfs_list[] = {
490 { "regs", tilcdc_regs_show, 0 },
491 { "mm", tilcdc_mm_show, 0 },
492 { "fb", drm_fb_cma_debugfs_show, 0 },
493};
494
495static int tilcdc_debugfs_init(struct drm_minor *minor)
496{
497 struct drm_device *dev = minor->dev;
498 struct tilcdc_module *mod;
499 int ret;
500
501 ret = drm_debugfs_create_files(tilcdc_debugfs_list,
502 ARRAY_SIZE(tilcdc_debugfs_list),
503 minor->debugfs_root, minor);
504
505 list_for_each_entry(mod, &module_list, list)
506 if (mod->funcs->debugfs_init)
507 mod->funcs->debugfs_init(mod, minor);
508
509 if (ret) {
510 dev_err(dev->dev, "could not install tilcdc_debugfs_list\n");
511 return ret;
512 }
513
514 return ret;
515}
516
517static void tilcdc_debugfs_cleanup(struct drm_minor *minor)
518{
519 struct tilcdc_module *mod;
520 drm_debugfs_remove_files(tilcdc_debugfs_list,
521 ARRAY_SIZE(tilcdc_debugfs_list), minor);
522
523 list_for_each_entry(mod, &module_list, list)
524 if (mod->funcs->debugfs_cleanup)
525 mod->funcs->debugfs_cleanup(mod, minor);
526}
527#endif
528
529static const struct file_operations fops = {
530 .owner = THIS_MODULE,
531 .open = drm_open,
532 .release = drm_release,
533 .unlocked_ioctl = drm_ioctl,
534#ifdef CONFIG_COMPAT
535 .compat_ioctl = drm_compat_ioctl,
536#endif
537 .poll = drm_poll,
538 .read = drm_read,
Rob Clark16ea9752013-01-08 15:04:28 -0600539 .llseek = no_llseek,
540 .mmap = drm_gem_cma_mmap,
541};
542
543static struct drm_driver tilcdc_driver = {
Jyri Sarha9c153902015-06-23 14:31:17 +0300544 .driver_features = (DRIVER_HAVE_IRQ | DRIVER_GEM | DRIVER_MODESET |
545 DRIVER_PRIME),
Rob Clark16ea9752013-01-08 15:04:28 -0600546 .load = tilcdc_load,
547 .unload = tilcdc_unload,
Rob Clark16ea9752013-01-08 15:04:28 -0600548 .lastclose = tilcdc_lastclose,
David Herrmann915b4d12014-08-29 12:12:43 +0200549 .set_busid = drm_platform_set_busid,
Rob Clark16ea9752013-01-08 15:04:28 -0600550 .irq_handler = tilcdc_irq,
551 .irq_preinstall = tilcdc_irq_preinstall,
552 .irq_postinstall = tilcdc_irq_postinstall,
553 .irq_uninstall = tilcdc_irq_uninstall,
Ville Syrjäläb44f8402015-09-30 16:46:48 +0300554 .get_vblank_counter = drm_vblank_no_hw_counter,
Rob Clark16ea9752013-01-08 15:04:28 -0600555 .enable_vblank = tilcdc_enable_vblank,
556 .disable_vblank = tilcdc_disable_vblank,
557 .gem_free_object = drm_gem_cma_free_object,
558 .gem_vm_ops = &drm_gem_cma_vm_ops,
559 .dumb_create = drm_gem_cma_dumb_create,
560 .dumb_map_offset = drm_gem_cma_dumb_map_offset,
Daniel Vetter43387b32013-07-16 09:12:04 +0200561 .dumb_destroy = drm_gem_dumb_destroy,
Jyri Sarha9c153902015-06-23 14:31:17 +0300562
563 .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
564 .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
565 .gem_prime_import = drm_gem_prime_import,
566 .gem_prime_export = drm_gem_prime_export,
567 .gem_prime_get_sg_table = drm_gem_cma_prime_get_sg_table,
568 .gem_prime_import_sg_table = drm_gem_cma_prime_import_sg_table,
569 .gem_prime_vmap = drm_gem_cma_prime_vmap,
570 .gem_prime_vunmap = drm_gem_cma_prime_vunmap,
571 .gem_prime_mmap = drm_gem_cma_prime_mmap,
Rob Clark16ea9752013-01-08 15:04:28 -0600572#ifdef CONFIG_DEBUG_FS
573 .debugfs_init = tilcdc_debugfs_init,
574 .debugfs_cleanup = tilcdc_debugfs_cleanup,
575#endif
576 .fops = &fops,
577 .name = "tilcdc",
578 .desc = "TI LCD Controller DRM",
579 .date = "20121205",
580 .major = 1,
581 .minor = 0,
582};
583
584/*
585 * Power management:
586 */
587
588#ifdef CONFIG_PM_SLEEP
589static int tilcdc_pm_suspend(struct device *dev)
590{
591 struct drm_device *ddev = dev_get_drvdata(dev);
592 struct tilcdc_drm_private *priv = ddev->dev_private;
593 unsigned i, n = 0;
594
595 drm_kms_helper_poll_disable(ddev);
596
Darren Etheridge85fd27f2014-09-19 01:42:57 +0000597 /* Select sleep pin state */
598 pinctrl_pm_select_sleep_state(dev);
599
600 if (pm_runtime_suspended(dev)) {
601 priv->ctx_valid = false;
602 return 0;
603 }
604
Darren Etheridge614b3cfe2014-09-25 00:59:32 +0000605 /* Disable the LCDC controller, to avoid locking up the PRCM */
606 tilcdc_crtc_dpms(priv->crtc, DRM_MODE_DPMS_OFF);
607
Rob Clark16ea9752013-01-08 15:04:28 -0600608 /* Save register state: */
609 for (i = 0; i < ARRAY_SIZE(registers); i++)
610 if (registers[i].save && (priv->rev >= registers[i].rev))
611 priv->saved_register[n++] = tilcdc_read(ddev, registers[i].reg);
612
Darren Etheridge85fd27f2014-09-19 01:42:57 +0000613 priv->ctx_valid = true;
Dave Gerlach416a07f2014-07-29 06:27:58 +0000614
Rob Clark16ea9752013-01-08 15:04:28 -0600615 return 0;
616}
617
618static int tilcdc_pm_resume(struct device *dev)
619{
620 struct drm_device *ddev = dev_get_drvdata(dev);
621 struct tilcdc_drm_private *priv = ddev->dev_private;
622 unsigned i, n = 0;
623
Dave Gerlach416a07f2014-07-29 06:27:58 +0000624 /* Select default pin state */
625 pinctrl_pm_select_default_state(dev);
626
Darren Etheridge85fd27f2014-09-19 01:42:57 +0000627 if (priv->ctx_valid == true) {
628 /* Restore register state: */
629 for (i = 0; i < ARRAY_SIZE(registers); i++)
630 if (registers[i].save &&
631 (priv->rev >= registers[i].rev))
632 tilcdc_write(ddev, registers[i].reg,
633 priv->saved_register[n++]);
634 }
Rob Clark16ea9752013-01-08 15:04:28 -0600635
636 drm_kms_helper_poll_enable(ddev);
637
638 return 0;
639}
640#endif
641
642static const struct dev_pm_ops tilcdc_pm_ops = {
643 SET_SYSTEM_SLEEP_PM_OPS(tilcdc_pm_suspend, tilcdc_pm_resume)
644};
645
646/*
647 * Platform driver:
648 */
649
Jyri Sarha103cd8b2015-02-10 14:13:23 +0200650static int tilcdc_bind(struct device *dev)
651{
652 return drm_platform_init(&tilcdc_driver, to_platform_device(dev));
653}
654
655static void tilcdc_unbind(struct device *dev)
656{
657 drm_put_dev(dev_get_drvdata(dev));
658}
659
660static const struct component_master_ops tilcdc_comp_ops = {
661 .bind = tilcdc_bind,
662 .unbind = tilcdc_unbind,
663};
664
Rob Clark16ea9752013-01-08 15:04:28 -0600665static int tilcdc_pdev_probe(struct platform_device *pdev)
666{
Jyri Sarha103cd8b2015-02-10 14:13:23 +0200667 struct component_match *match = NULL;
668 int ret;
669
Rob Clark16ea9752013-01-08 15:04:28 -0600670 /* bail out early if no DT data: */
671 if (!pdev->dev.of_node) {
672 dev_err(&pdev->dev, "device-tree data is missing\n");
673 return -ENXIO;
674 }
675
Jyri Sarha103cd8b2015-02-10 14:13:23 +0200676 ret = tilcdc_get_external_components(&pdev->dev, &match);
677 if (ret < 0)
678 return ret;
679 else if (ret == 0)
680 return drm_platform_init(&tilcdc_driver, pdev);
681 else
682 return component_master_add_with_match(&pdev->dev,
683 &tilcdc_comp_ops,
684 match);
Rob Clark16ea9752013-01-08 15:04:28 -0600685}
686
687static int tilcdc_pdev_remove(struct platform_device *pdev)
688{
Jyri Sarha103cd8b2015-02-10 14:13:23 +0200689 struct drm_device *ddev = dev_get_drvdata(&pdev->dev);
690 struct tilcdc_drm_private *priv = ddev->dev_private;
691
692 /* Check if a subcomponent has already triggered the unloading. */
693 if (!priv)
694 return 0;
695
696 if (priv->is_componentized)
697 component_master_del(&pdev->dev, &tilcdc_comp_ops);
698 else
699 drm_put_dev(platform_get_drvdata(pdev));
Rob Clark16ea9752013-01-08 15:04:28 -0600700
701 return 0;
702}
703
704static struct of_device_id tilcdc_of_match[] = {
705 { .compatible = "ti,am33xx-tilcdc", },
706 { },
707};
708MODULE_DEVICE_TABLE(of, tilcdc_of_match);
709
710static struct platform_driver tilcdc_platform_driver = {
711 .probe = tilcdc_pdev_probe,
712 .remove = tilcdc_pdev_remove,
713 .driver = {
Rob Clark16ea9752013-01-08 15:04:28 -0600714 .name = "tilcdc",
715 .pm = &tilcdc_pm_ops,
716 .of_match_table = tilcdc_of_match,
717 },
718};
719
720static int __init tilcdc_drm_init(void)
721{
722 DBG("init");
723 tilcdc_tfp410_init();
Rob Clark0d4bbaf2012-12-18 17:34:16 -0600724 tilcdc_panel_init();
Rob Clark16ea9752013-01-08 15:04:28 -0600725 return platform_driver_register(&tilcdc_platform_driver);
726}
727
728static void __exit tilcdc_drm_fini(void)
729{
730 DBG("fini");
Rob Clark16ea9752013-01-08 15:04:28 -0600731 platform_driver_unregister(&tilcdc_platform_driver);
Guido Martínezeb565a22014-06-17 11:17:08 -0300732 tilcdc_panel_fini();
Guido Martínezeb565a22014-06-17 11:17:08 -0300733 tilcdc_tfp410_fini();
Rob Clark16ea9752013-01-08 15:04:28 -0600734}
735
Guido Martínez2023d842014-06-17 11:17:11 -0300736module_init(tilcdc_drm_init);
Rob Clark16ea9752013-01-08 15:04:28 -0600737module_exit(tilcdc_drm_fini);
738
739MODULE_AUTHOR("Rob Clark <robdclark@gmail.com");
740MODULE_DESCRIPTION("TI LCD Controller DRM Driver");
741MODULE_LICENSE("GPL");