blob: 72ce063aa0d88a9532391d7214c88c706ce95aad [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 Sarhaedc43302015-12-30 17:40:24 +020023#include <drm/drm_atomic.h>
24#include <drm/drm_atomic_helper.h>
Masahiro Yamadabb2af9b2017-04-24 13:50:32 +090025#include <drm/drm_fb_helper.h>
Noralf Trønnes6025a152017-08-13 15:32:02 +020026#include <drm/drm_gem_framebuffer_helper.h>
Jyri Sarha103cd8b2015-02-10 14:13:23 +020027
Rob Clark16ea9752013-01-08 15:04:28 -060028#include "tilcdc_drv.h"
29#include "tilcdc_regs.h"
30#include "tilcdc_tfp410.h"
Rob Clark0d4bbaf2012-12-18 17:34:16 -060031#include "tilcdc_panel.h"
Jyri Sarha103cd8b2015-02-10 14:13:23 +020032#include "tilcdc_external.h"
Rob Clark16ea9752013-01-08 15:04:28 -060033
Rob Clark16ea9752013-01-08 15:04:28 -060034static LIST_HEAD(module_list);
35
Jyri Sarhabcc5a6f2016-08-11 19:09:43 +030036static const u32 tilcdc_rev1_formats[] = { DRM_FORMAT_RGB565 };
37
38static const u32 tilcdc_straight_formats[] = { DRM_FORMAT_RGB565,
39 DRM_FORMAT_BGR888,
40 DRM_FORMAT_XBGR8888 };
41
42static const u32 tilcdc_crossed_formats[] = { DRM_FORMAT_BGR565,
43 DRM_FORMAT_RGB888,
44 DRM_FORMAT_XRGB8888 };
45
46static const u32 tilcdc_legacy_formats[] = { DRM_FORMAT_RGB565,
47 DRM_FORMAT_RGB888,
48 DRM_FORMAT_XRGB8888 };
49
Rob Clark16ea9752013-01-08 15:04:28 -060050void tilcdc_module_init(struct tilcdc_module *mod, const char *name,
51 const struct tilcdc_module_ops *funcs)
52{
53 mod->name = name;
54 mod->funcs = funcs;
55 INIT_LIST_HEAD(&mod->list);
56 list_add(&mod->list, &module_list);
57}
58
59void tilcdc_module_cleanup(struct tilcdc_module *mod)
60{
61 list_del(&mod->list);
62}
63
64static struct of_device_id tilcdc_of_match[];
65
66static struct drm_framebuffer *tilcdc_fb_create(struct drm_device *dev,
Ville Syrjälä1eb83452015-11-11 19:11:29 +020067 struct drm_file *file_priv, const struct drm_mode_fb_cmd2 *mode_cmd)
Rob Clark16ea9752013-01-08 15:04:28 -060068{
Noralf Trønnes6025a152017-08-13 15:32:02 +020069 return drm_gem_fb_create(dev, file_priv, mode_cmd);
Rob Clark16ea9752013-01-08 15:04:28 -060070}
71
72static void tilcdc_fb_output_poll_changed(struct drm_device *dev)
73{
74 struct tilcdc_drm_private *priv = dev->dev_private;
Markus Elfringc08448172014-11-19 17:05:20 +010075 drm_fbdev_cma_hotplug_event(priv->fbdev);
Rob Clark16ea9752013-01-08 15:04:28 -060076}
77
Wei Yongjun30457672016-09-10 12:32:57 +000078static int tilcdc_atomic_check(struct drm_device *dev,
79 struct drm_atomic_state *state)
Jyri Sarhaedc43302015-12-30 17:40:24 +020080{
81 int ret;
82
83 ret = drm_atomic_helper_check_modeset(dev, state);
84 if (ret)
85 return ret;
86
87 ret = drm_atomic_helper_check_planes(dev, state);
88 if (ret)
89 return ret;
90
91 /*
92 * tilcdc ->atomic_check can update ->mode_changed if pixel format
93 * changes, hence will we check modeset changes again.
94 */
95 ret = drm_atomic_helper_check_modeset(dev, state);
96 if (ret)
97 return ret;
98
99 return ret;
100}
101
102static int tilcdc_commit(struct drm_device *dev,
103 struct drm_atomic_state *state,
104 bool async)
105{
106 int ret;
107
108 ret = drm_atomic_helper_prepare_planes(dev, state);
109 if (ret)
110 return ret;
111
Maarten Lankhorstfad9e432017-07-11 16:33:11 +0200112 ret = drm_atomic_helper_swap_state(state, true);
113 if (ret) {
114 drm_atomic_helper_cleanup_planes(dev, state);
115 return ret;
116 }
Jyri Sarhaedc43302015-12-30 17:40:24 +0200117
118 /*
119 * Everything below can be run asynchronously without the need to grab
120 * any modeset locks at all under one condition: It must be guaranteed
121 * that the asynchronous work has either been cancelled (if the driver
122 * supports it, which at least requires that the framebuffers get
123 * cleaned up with drm_atomic_helper_cleanup_planes()) or completed
124 * before the new state gets committed on the software side with
125 * drm_atomic_helper_swap_state().
126 *
127 * This scheme allows new atomic state updates to be prepared and
128 * checked in parallel to the asynchronous completion of the previous
129 * update. Which is important since compositors need to figure out the
130 * composition of the next frame right after having submitted the
131 * current layout.
132 */
133
134 drm_atomic_helper_commit_modeset_disables(dev, state);
135
Liu Ying2b58e982016-08-29 17:12:03 +0800136 drm_atomic_helper_commit_planes(dev, state, 0);
Jyri Sarhaedc43302015-12-30 17:40:24 +0200137
138 drm_atomic_helper_commit_modeset_enables(dev, state);
139
140 drm_atomic_helper_wait_for_vblanks(dev, state);
141
142 drm_atomic_helper_cleanup_planes(dev, state);
143
Jyri Sarhaedc43302015-12-30 17:40:24 +0200144 return 0;
145}
146
Rob Clark16ea9752013-01-08 15:04:28 -0600147static const struct drm_mode_config_funcs mode_config_funcs = {
148 .fb_create = tilcdc_fb_create,
149 .output_poll_changed = tilcdc_fb_output_poll_changed,
Jyri Sarhaedc43302015-12-30 17:40:24 +0200150 .atomic_check = tilcdc_atomic_check,
151 .atomic_commit = tilcdc_commit,
Rob Clark16ea9752013-01-08 15:04:28 -0600152};
153
Jyri Sarha9963d362016-11-15 22:56:46 +0200154static void modeset_init(struct drm_device *dev)
Rob Clark16ea9752013-01-08 15:04:28 -0600155{
156 struct tilcdc_drm_private *priv = dev->dev_private;
157 struct tilcdc_module *mod;
158
Rob Clark16ea9752013-01-08 15:04:28 -0600159 list_for_each_entry(mod, &module_list, list) {
160 DBG("loading module: %s", mod->name);
161 mod->funcs->modeset_init(mod, dev);
162 }
163
Rob Clark16ea9752013-01-08 15:04:28 -0600164 dev->mode_config.min_width = 0;
165 dev->mode_config.min_height = 0;
166 dev->mode_config.max_width = tilcdc_crtc_max_width(priv->crtc);
167 dev->mode_config.max_height = 2048;
168 dev->mode_config.funcs = &mode_config_funcs;
Rob Clark16ea9752013-01-08 15:04:28 -0600169}
170
171#ifdef CONFIG_CPU_FREQ
172static int cpufreq_transition(struct notifier_block *nb,
173 unsigned long val, void *data)
174{
175 struct tilcdc_drm_private *priv = container_of(nb,
176 struct tilcdc_drm_private, freq_transition);
Jyri Sarhaa6b7eba2016-09-05 20:39:32 +0300177
Jyri Sarha642e5162016-09-06 16:19:54 +0300178 if (val == CPUFREQ_POSTCHANGE)
179 tilcdc_crtc_update_clk(priv->crtc);
Rob Clark16ea9752013-01-08 15:04:28 -0600180
181 return 0;
182}
183#endif
184
185/*
186 * DRM operations:
187 */
188
Jyri Sarha923310b2016-10-17 17:53:33 +0300189static void tilcdc_fini(struct drm_device *dev)
Rob Clark16ea9752013-01-08 15:04:28 -0600190{
191 struct tilcdc_drm_private *priv = dev->dev_private;
Rob Clark16ea9752013-01-08 15:04:28 -0600192
Jyri Sarha9e79e062016-10-18 23:23:27 +0300193 if (priv->crtc)
Jyri Sarha2d53a182016-10-25 12:27:31 +0300194 tilcdc_crtc_shutdown(priv->crtc);
Jyri Sarha103cd8b2015-02-10 14:13:23 +0200195
Jyri Sarha9e79e062016-10-18 23:23:27 +0300196 if (priv->is_registered)
197 drm_dev_unregister(dev);
Jyri Sarha923310b2016-10-17 17:53:33 +0300198
Rob Clark16ea9752013-01-08 15:04:28 -0600199 drm_kms_helper_poll_fini(dev);
Jyri Sarha9e79e062016-10-18 23:23:27 +0300200
201 if (priv->fbdev)
202 drm_fbdev_cma_fini(priv->fbdev);
203
Rob Clark16ea9752013-01-08 15:04:28 -0600204 drm_irq_uninstall(dev);
Jyri Sarha923310b2016-10-17 17:53:33 +0300205 drm_mode_config_cleanup(dev);
Jyri Sarhaec9eab02016-10-31 17:34:22 +0200206 tilcdc_remove_external_device(dev);
Rob Clark16ea9752013-01-08 15:04:28 -0600207
208#ifdef CONFIG_CPU_FREQ
Jyri Sarha9e79e062016-10-18 23:23:27 +0300209 if (priv->freq_transition.notifier_call)
210 cpufreq_unregister_notifier(&priv->freq_transition,
211 CPUFREQ_TRANSITION_NOTIFIER);
Rob Clark16ea9752013-01-08 15:04:28 -0600212#endif
213
214 if (priv->clk)
215 clk_put(priv->clk);
216
217 if (priv->mmio)
218 iounmap(priv->mmio);
219
Jyri Sarha9e79e062016-10-18 23:23:27 +0300220 if (priv->wq) {
221 flush_workqueue(priv->wq);
222 destroy_workqueue(priv->wq);
223 }
Rob Clark16ea9752013-01-08 15:04:28 -0600224
225 dev->dev_private = NULL;
226
227 pm_runtime_disable(dev->dev);
228
Aishwarya Pantce7b7002017-09-26 14:00:19 +0530229 drm_dev_put(dev);
Rob Clark16ea9752013-01-08 15:04:28 -0600230}
231
Jyri Sarha923310b2016-10-17 17:53:33 +0300232static int tilcdc_init(struct drm_driver *ddrv, struct device *dev)
Rob Clark16ea9752013-01-08 15:04:28 -0600233{
Jyri Sarha923310b2016-10-17 17:53:33 +0300234 struct drm_device *ddev;
235 struct platform_device *pdev = to_platform_device(dev);
236 struct device_node *node = dev->of_node;
Rob Clark16ea9752013-01-08 15:04:28 -0600237 struct tilcdc_drm_private *priv;
238 struct resource *res;
Benoit Parrotdc28aa02013-06-18 17:18:31 -0500239 u32 bpp = 0;
Rob Clark16ea9752013-01-08 15:04:28 -0600240 int ret;
241
Jyri Sarha923310b2016-10-17 17:53:33 +0300242 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
Jyri Sarha514d1a12016-06-16 11:28:23 +0300243 if (!priv) {
Jyri Sarha923310b2016-10-17 17:53:33 +0300244 dev_err(dev, "failed to allocate private data\n");
Rob Clark16ea9752013-01-08 15:04:28 -0600245 return -ENOMEM;
246 }
247
Jyri Sarha923310b2016-10-17 17:53:33 +0300248 ddev = drm_dev_alloc(ddrv, dev);
249 if (IS_ERR(ddev))
250 return PTR_ERR(ddev);
251
Jyri Sarha923310b2016-10-17 17:53:33 +0300252 ddev->dev_private = priv;
Jyri Sarha9e79e062016-10-18 23:23:27 +0300253 platform_set_drvdata(pdev, ddev);
254 drm_mode_config_init(ddev);
Rob Clark16ea9752013-01-08 15:04:28 -0600255
Jyri Sarha103cd8b2015-02-10 14:13:23 +0200256 priv->is_componentized =
Jyri Sarha923310b2016-10-17 17:53:33 +0300257 tilcdc_get_external_components(dev, NULL) > 0;
Jyri Sarha103cd8b2015-02-10 14:13:23 +0200258
Rob Clark16ea9752013-01-08 15:04:28 -0600259 priv->wq = alloc_ordered_workqueue("tilcdc", 0);
Ezequiel Garciab478e336b2014-09-02 09:51:15 -0300260 if (!priv->wq) {
261 ret = -ENOMEM;
Jyri Sarha9e79e062016-10-18 23:23:27 +0300262 goto init_failed;
Ezequiel Garciab478e336b2014-09-02 09:51:15 -0300263 }
Rob Clark16ea9752013-01-08 15:04:28 -0600264
265 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
266 if (!res) {
Jyri Sarha923310b2016-10-17 17:53:33 +0300267 dev_err(dev, "failed to get memory resource\n");
Rob Clark16ea9752013-01-08 15:04:28 -0600268 ret = -EINVAL;
Jyri Sarha9e79e062016-10-18 23:23:27 +0300269 goto init_failed;
Rob Clark16ea9752013-01-08 15:04:28 -0600270 }
271
272 priv->mmio = ioremap_nocache(res->start, resource_size(res));
273 if (!priv->mmio) {
Jyri Sarha923310b2016-10-17 17:53:33 +0300274 dev_err(dev, "failed to ioremap\n");
Rob Clark16ea9752013-01-08 15:04:28 -0600275 ret = -ENOMEM;
Jyri Sarha9e79e062016-10-18 23:23:27 +0300276 goto init_failed;
Rob Clark16ea9752013-01-08 15:04:28 -0600277 }
278
Jyri Sarha923310b2016-10-17 17:53:33 +0300279 priv->clk = clk_get(dev, "fck");
Rob Clark16ea9752013-01-08 15:04:28 -0600280 if (IS_ERR(priv->clk)) {
Jyri Sarha923310b2016-10-17 17:53:33 +0300281 dev_err(dev, "failed to get functional clock\n");
Rob Clark16ea9752013-01-08 15:04:28 -0600282 ret = -ENODEV;
Jyri Sarha9e79e062016-10-18 23:23:27 +0300283 goto init_failed;
Rob Clark16ea9752013-01-08 15:04:28 -0600284 }
285
Rob Clark16ea9752013-01-08 15:04:28 -0600286#ifdef CONFIG_CPU_FREQ
Rob Clark16ea9752013-01-08 15:04:28 -0600287 priv->freq_transition.notifier_call = cpufreq_transition;
288 ret = cpufreq_register_notifier(&priv->freq_transition,
289 CPUFREQ_TRANSITION_NOTIFIER);
290 if (ret) {
Jyri Sarha923310b2016-10-17 17:53:33 +0300291 dev_err(dev, "failed to register cpufreq notifier\n");
Jyri Sarha9e79e062016-10-18 23:23:27 +0300292 priv->freq_transition.notifier_call = NULL;
293 goto init_failed;
Rob Clark16ea9752013-01-08 15:04:28 -0600294 }
295#endif
296
297 if (of_property_read_u32(node, "max-bandwidth", &priv->max_bandwidth))
Darren Etheridge4e564342013-06-21 13:52:23 -0500298 priv->max_bandwidth = TILCDC_DEFAULT_MAX_BANDWIDTH;
299
300 DBG("Maximum Bandwidth Value %d", priv->max_bandwidth);
301
Bartosz Golaszewski0186fcc2016-11-28 12:37:23 +0100302 if (of_property_read_u32(node, "max-width", &priv->max_width))
Darren Etheridge4e564342013-06-21 13:52:23 -0500303 priv->max_width = TILCDC_DEFAULT_MAX_WIDTH;
304
305 DBG("Maximum Horizontal Pixel Width Value %dpixels", priv->max_width);
306
Bartosz Golaszewski0186fcc2016-11-28 12:37:23 +0100307 if (of_property_read_u32(node, "max-pixelclock",
Darren Etheridge4e564342013-06-21 13:52:23 -0500308 &priv->max_pixelclock))
309 priv->max_pixelclock = TILCDC_DEFAULT_MAX_PIXELCLOCK;
310
311 DBG("Maximum Pixel Clock Value %dKHz", priv->max_pixelclock);
Rob Clark16ea9752013-01-08 15:04:28 -0600312
Jyri Sarha923310b2016-10-17 17:53:33 +0300313 pm_runtime_enable(dev);
Rob Clark16ea9752013-01-08 15:04:28 -0600314
315 /* Determine LCD IP Version */
Jyri Sarha923310b2016-10-17 17:53:33 +0300316 pm_runtime_get_sync(dev);
317 switch (tilcdc_read(ddev, LCDC_PID_REG)) {
Rob Clark16ea9752013-01-08 15:04:28 -0600318 case 0x4c100102:
319 priv->rev = 1;
320 break;
321 case 0x4f200800:
322 case 0x4f201000:
323 priv->rev = 2;
324 break;
325 default:
Jyri Sarha923310b2016-10-17 17:53:33 +0300326 dev_warn(dev, "Unknown PID Reg value 0x%08x, "
327 "defaulting to LCD revision 1\n",
328 tilcdc_read(ddev, LCDC_PID_REG));
Rob Clark16ea9752013-01-08 15:04:28 -0600329 priv->rev = 1;
330 break;
331 }
332
Jyri Sarha923310b2016-10-17 17:53:33 +0300333 pm_runtime_put_sync(dev);
Rob Clark16ea9752013-01-08 15:04:28 -0600334
Jyri Sarhabcc5a6f2016-08-11 19:09:43 +0300335 if (priv->rev == 1) {
336 DBG("Revision 1 LCDC supports only RGB565 format");
337 priv->pixelformats = tilcdc_rev1_formats;
338 priv->num_pixelformats = ARRAY_SIZE(tilcdc_rev1_formats);
Jyri Sarhac5665382016-08-13 21:08:20 +0300339 bpp = 16;
Jyri Sarhabcc5a6f2016-08-11 19:09:43 +0300340 } else {
341 const char *str = "\0";
342
343 of_property_read_string(node, "blue-and-red-wiring", &str);
344 if (0 == strcmp(str, "crossed")) {
345 DBG("Configured for crossed blue and red wires");
346 priv->pixelformats = tilcdc_crossed_formats;
347 priv->num_pixelformats =
348 ARRAY_SIZE(tilcdc_crossed_formats);
Jyri Sarhac5665382016-08-13 21:08:20 +0300349 bpp = 32; /* Choose bpp with RGB support for fbdef */
Jyri Sarhabcc5a6f2016-08-11 19:09:43 +0300350 } else if (0 == strcmp(str, "straight")) {
351 DBG("Configured for straight blue and red wires");
352 priv->pixelformats = tilcdc_straight_formats;
353 priv->num_pixelformats =
354 ARRAY_SIZE(tilcdc_straight_formats);
Jyri Sarhac5665382016-08-13 21:08:20 +0300355 bpp = 16; /* Choose bpp with RGB support for fbdef */
Jyri Sarhabcc5a6f2016-08-11 19:09:43 +0300356 } else {
357 DBG("Blue and red wiring '%s' unknown, use legacy mode",
358 str);
359 priv->pixelformats = tilcdc_legacy_formats;
360 priv->num_pixelformats =
361 ARRAY_SIZE(tilcdc_legacy_formats);
Jyri Sarhac5665382016-08-13 21:08:20 +0300362 bpp = 16; /* This is just a guess */
Jyri Sarhabcc5a6f2016-08-11 19:09:43 +0300363 }
364 }
365
Jyri Sarha9963d362016-11-15 22:56:46 +0200366 ret = tilcdc_crtc_create(ddev);
Rob Clark16ea9752013-01-08 15:04:28 -0600367 if (ret < 0) {
Jyri Sarha9963d362016-11-15 22:56:46 +0200368 dev_err(dev, "failed to create crtc\n");
Jyri Sarha9e79e062016-10-18 23:23:27 +0300369 goto init_failed;
Rob Clark16ea9752013-01-08 15:04:28 -0600370 }
Jyri Sarha9963d362016-11-15 22:56:46 +0200371 modeset_init(ddev);
Rob Clark16ea9752013-01-08 15:04:28 -0600372
Jyri Sarha103cd8b2015-02-10 14:13:23 +0200373 if (priv->is_componentized) {
Jyri Sarha923310b2016-10-17 17:53:33 +0300374 ret = component_bind_all(dev, ddev);
Jyri Sarha103cd8b2015-02-10 14:13:23 +0200375 if (ret < 0)
Jyri Sarha9e79e062016-10-18 23:23:27 +0300376 goto init_failed;
Jyri Sarha103cd8b2015-02-10 14:13:23 +0200377
Jyri Sarhaec9eab02016-10-31 17:34:22 +0200378 ret = tilcdc_add_component_encoder(ddev);
Jyri Sarha103cd8b2015-02-10 14:13:23 +0200379 if (ret < 0)
Jyri Sarha9e79e062016-10-18 23:23:27 +0300380 goto init_failed;
Jyri Sarhaec9eab02016-10-31 17:34:22 +0200381 } else {
382 ret = tilcdc_attach_external_device(ddev);
383 if (ret)
384 goto init_failed;
Jyri Sarha103cd8b2015-02-10 14:13:23 +0200385 }
386
Jyri Sarhaec9eab02016-10-31 17:34:22 +0200387 if (!priv->external_connector &&
388 ((priv->num_encoders == 0) || (priv->num_connectors == 0))) {
Jyri Sarha923310b2016-10-17 17:53:33 +0300389 dev_err(dev, "no encoders/connectors found\n");
Jyri Sarha103cd8b2015-02-10 14:13:23 +0200390 ret = -ENXIO;
Jyri Sarha9e79e062016-10-18 23:23:27 +0300391 goto init_failed;
Jyri Sarha103cd8b2015-02-10 14:13:23 +0200392 }
393
Jyri Sarha923310b2016-10-17 17:53:33 +0300394 ret = drm_vblank_init(ddev, 1);
Rob Clark16ea9752013-01-08 15:04:28 -0600395 if (ret < 0) {
Jyri Sarha923310b2016-10-17 17:53:33 +0300396 dev_err(dev, "failed to initialize vblank\n");
Jyri Sarha9e79e062016-10-18 23:23:27 +0300397 goto init_failed;
Rob Clark16ea9752013-01-08 15:04:28 -0600398 }
399
Jyri Sarha923310b2016-10-17 17:53:33 +0300400 ret = drm_irq_install(ddev, platform_get_irq(pdev, 0));
Rob Clark16ea9752013-01-08 15:04:28 -0600401 if (ret < 0) {
Jyri Sarha923310b2016-10-17 17:53:33 +0300402 dev_err(dev, "failed to install IRQ handler\n");
Jyri Sarha9e79e062016-10-18 23:23:27 +0300403 goto init_failed;
Rob Clark16ea9752013-01-08 15:04:28 -0600404 }
405
Jyri Sarha923310b2016-10-17 17:53:33 +0300406 drm_mode_config_reset(ddev);
Jyri Sarha522a76f2015-12-29 17:27:32 +0200407
Jyri Sarha923310b2016-10-17 17:53:33 +0300408 priv->fbdev = drm_fbdev_cma_init(ddev, bpp,
Gabriel Krisman Bertazie4563f62017-02-02 14:26:40 -0200409 ddev->mode_config.num_connector);
Ezequiel Garciab478e336b2014-09-02 09:51:15 -0300410 if (IS_ERR(priv->fbdev)) {
411 ret = PTR_ERR(priv->fbdev);
Jyri Sarha9e79e062016-10-18 23:23:27 +0300412 goto init_failed;
Ezequiel Garciab478e336b2014-09-02 09:51:15 -0300413 }
Rob Clark16ea9752013-01-08 15:04:28 -0600414
Jyri Sarha923310b2016-10-17 17:53:33 +0300415 drm_kms_helper_poll_init(ddev);
416
417 ret = drm_dev_register(ddev, 0);
418 if (ret)
Jyri Sarha9e79e062016-10-18 23:23:27 +0300419 goto init_failed;
Rob Clark16ea9752013-01-08 15:04:28 -0600420
Jyri Sarha9e79e062016-10-18 23:23:27 +0300421 priv->is_registered = true;
Rob Clark16ea9752013-01-08 15:04:28 -0600422 return 0;
423
Jyri Sarha9e79e062016-10-18 23:23:27 +0300424init_failed:
425 tilcdc_fini(ddev);
Jyri Sarhad0ec32c2016-02-23 12:44:27 +0200426
Rob Clark16ea9752013-01-08 15:04:28 -0600427 return ret;
428}
429
Rob Clark16ea9752013-01-08 15:04:28 -0600430static void tilcdc_lastclose(struct drm_device *dev)
431{
432 struct tilcdc_drm_private *priv = dev->dev_private;
433 drm_fbdev_cma_restore_mode(priv->fbdev);
434}
435
Daniel Vettere9f0d762013-12-11 11:34:42 +0100436static irqreturn_t tilcdc_irq(int irq, void *arg)
Rob Clark16ea9752013-01-08 15:04:28 -0600437{
438 struct drm_device *dev = arg;
439 struct tilcdc_drm_private *priv = dev->dev_private;
440 return tilcdc_crtc_irq(priv->crtc);
441}
442
Jyri Sarha514d1a12016-06-16 11:28:23 +0300443#if defined(CONFIG_DEBUG_FS)
Rob Clark16ea9752013-01-08 15:04:28 -0600444static const struct {
445 const char *name;
446 uint8_t rev;
447 uint8_t save;
448 uint32_t reg;
Sachin Kamat32501452013-03-02 15:53:08 +0530449} registers[] = {
Rob Clark16ea9752013-01-08 15:04:28 -0600450#define REG(rev, save, reg) { #reg, rev, save, reg }
451 /* exists in revision 1: */
452 REG(1, false, LCDC_PID_REG),
453 REG(1, true, LCDC_CTRL_REG),
454 REG(1, false, LCDC_STAT_REG),
455 REG(1, true, LCDC_RASTER_CTRL_REG),
456 REG(1, true, LCDC_RASTER_TIMING_0_REG),
457 REG(1, true, LCDC_RASTER_TIMING_1_REG),
458 REG(1, true, LCDC_RASTER_TIMING_2_REG),
459 REG(1, true, LCDC_DMA_CTRL_REG),
460 REG(1, true, LCDC_DMA_FB_BASE_ADDR_0_REG),
461 REG(1, true, LCDC_DMA_FB_CEILING_ADDR_0_REG),
462 REG(1, true, LCDC_DMA_FB_BASE_ADDR_1_REG),
463 REG(1, true, LCDC_DMA_FB_CEILING_ADDR_1_REG),
464 /* new in revision 2: */
465 REG(2, false, LCDC_RAW_STAT_REG),
466 REG(2, false, LCDC_MASKED_STAT_REG),
Jyri Sarhaf3a99942016-01-08 12:17:50 +0200467 REG(2, true, LCDC_INT_ENABLE_SET_REG),
Rob Clark16ea9752013-01-08 15:04:28 -0600468 REG(2, false, LCDC_INT_ENABLE_CLR_REG),
469 REG(2, false, LCDC_END_OF_INT_IND_REG),
470 REG(2, true, LCDC_CLK_ENABLE_REG),
Rob Clark16ea9752013-01-08 15:04:28 -0600471#undef REG
472};
Jyri Sarha29ddd6e2015-07-02 16:26:12 +0300473
Rob Clark16ea9752013-01-08 15:04:28 -0600474#endif
475
476#ifdef CONFIG_DEBUG_FS
477static int tilcdc_regs_show(struct seq_file *m, void *arg)
478{
479 struct drm_info_node *node = (struct drm_info_node *) m->private;
480 struct drm_device *dev = node->minor->dev;
481 struct tilcdc_drm_private *priv = dev->dev_private;
482 unsigned i;
483
484 pm_runtime_get_sync(dev->dev);
485
486 seq_printf(m, "revision: %d\n", priv->rev);
487
488 for (i = 0; i < ARRAY_SIZE(registers); i++)
489 if (priv->rev >= registers[i].rev)
490 seq_printf(m, "%s:\t %08x\n", registers[i].name,
491 tilcdc_read(dev, registers[i].reg));
492
493 pm_runtime_put_sync(dev->dev);
494
495 return 0;
496}
497
498static int tilcdc_mm_show(struct seq_file *m, void *arg)
499{
500 struct drm_info_node *node = (struct drm_info_node *) m->private;
501 struct drm_device *dev = node->minor->dev;
Daniel Vetterb5c37142016-12-29 12:09:24 +0100502 struct drm_printer p = drm_seq_file_printer(m);
503 drm_mm_print(&dev->vma_offset_manager->vm_addr_space_mm, &p);
504 return 0;
Rob Clark16ea9752013-01-08 15:04:28 -0600505}
506
507static struct drm_info_list tilcdc_debugfs_list[] = {
508 { "regs", tilcdc_regs_show, 0 },
509 { "mm", tilcdc_mm_show, 0 },
510 { "fb", drm_fb_cma_debugfs_show, 0 },
511};
512
513static int tilcdc_debugfs_init(struct drm_minor *minor)
514{
515 struct drm_device *dev = minor->dev;
516 struct tilcdc_module *mod;
517 int ret;
518
519 ret = drm_debugfs_create_files(tilcdc_debugfs_list,
520 ARRAY_SIZE(tilcdc_debugfs_list),
521 minor->debugfs_root, minor);
522
523 list_for_each_entry(mod, &module_list, list)
524 if (mod->funcs->debugfs_init)
525 mod->funcs->debugfs_init(mod, minor);
526
527 if (ret) {
528 dev_err(dev->dev, "could not install tilcdc_debugfs_list\n");
529 return ret;
530 }
531
532 return ret;
533}
Rob Clark16ea9752013-01-08 15:04:28 -0600534#endif
535
Daniel Vetterd55f7e52017-03-08 15:12:56 +0100536DEFINE_DRM_GEM_CMA_FOPS(fops);
Rob Clark16ea9752013-01-08 15:04:28 -0600537
538static struct drm_driver tilcdc_driver = {
Jyri Sarha9c153902015-06-23 14:31:17 +0300539 .driver_features = (DRIVER_HAVE_IRQ | DRIVER_GEM | DRIVER_MODESET |
Jyri Sarha305198d2016-04-07 15:05:16 +0300540 DRIVER_PRIME | DRIVER_ATOMIC),
Rob Clark16ea9752013-01-08 15:04:28 -0600541 .lastclose = tilcdc_lastclose,
542 .irq_handler = tilcdc_irq,
Daniel Vetteraa0438c2016-05-30 19:53:05 +0200543 .gem_free_object_unlocked = drm_gem_cma_free_object,
Rob Clark16ea9752013-01-08 15:04:28 -0600544 .gem_vm_ops = &drm_gem_cma_vm_ops,
545 .dumb_create = drm_gem_cma_dumb_create,
Jyri Sarha9c153902015-06-23 14:31:17 +0300546
547 .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
548 .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
549 .gem_prime_import = drm_gem_prime_import,
550 .gem_prime_export = drm_gem_prime_export,
551 .gem_prime_get_sg_table = drm_gem_cma_prime_get_sg_table,
552 .gem_prime_import_sg_table = drm_gem_cma_prime_import_sg_table,
553 .gem_prime_vmap = drm_gem_cma_prime_vmap,
554 .gem_prime_vunmap = drm_gem_cma_prime_vunmap,
555 .gem_prime_mmap = drm_gem_cma_prime_mmap,
Rob Clark16ea9752013-01-08 15:04:28 -0600556#ifdef CONFIG_DEBUG_FS
557 .debugfs_init = tilcdc_debugfs_init,
Rob Clark16ea9752013-01-08 15:04:28 -0600558#endif
559 .fops = &fops,
560 .name = "tilcdc",
561 .desc = "TI LCD Controller DRM",
562 .date = "20121205",
563 .major = 1,
564 .minor = 0,
565};
566
567/*
568 * Power management:
569 */
570
571#ifdef CONFIG_PM_SLEEP
572static int tilcdc_pm_suspend(struct device *dev)
573{
574 struct drm_device *ddev = dev_get_drvdata(dev);
575 struct tilcdc_drm_private *priv = ddev->dev_private;
Rob Clark16ea9752013-01-08 15:04:28 -0600576
Jyri Sarha514d1a12016-06-16 11:28:23 +0300577 priv->saved_state = drm_atomic_helper_suspend(ddev);
Rob Clark16ea9752013-01-08 15:04:28 -0600578
Darren Etheridge85fd27f2014-09-19 01:42:57 +0000579 /* Select sleep pin state */
580 pinctrl_pm_select_sleep_state(dev);
581
Rob Clark16ea9752013-01-08 15:04:28 -0600582 return 0;
583}
584
585static int tilcdc_pm_resume(struct device *dev)
586{
587 struct drm_device *ddev = dev_get_drvdata(dev);
588 struct tilcdc_drm_private *priv = ddev->dev_private;
Jyri Sarha514d1a12016-06-16 11:28:23 +0300589 int ret = 0;
Rob Clark16ea9752013-01-08 15:04:28 -0600590
Dave Gerlach416a07f2014-07-29 06:27:58 +0000591 /* Select default pin state */
592 pinctrl_pm_select_default_state(dev);
593
Jyri Sarha514d1a12016-06-16 11:28:23 +0300594 if (priv->saved_state)
595 ret = drm_atomic_helper_resume(ddev, priv->saved_state);
Rob Clark16ea9752013-01-08 15:04:28 -0600596
Jyri Sarha514d1a12016-06-16 11:28:23 +0300597 return ret;
Rob Clark16ea9752013-01-08 15:04:28 -0600598}
599#endif
600
601static const struct dev_pm_ops tilcdc_pm_ops = {
602 SET_SYSTEM_SLEEP_PM_OPS(tilcdc_pm_suspend, tilcdc_pm_resume)
603};
604
605/*
606 * Platform driver:
607 */
Jyri Sarha103cd8b2015-02-10 14:13:23 +0200608static int tilcdc_bind(struct device *dev)
609{
Jyri Sarha923310b2016-10-17 17:53:33 +0300610 return tilcdc_init(&tilcdc_driver, dev);
Jyri Sarha103cd8b2015-02-10 14:13:23 +0200611}
612
613static void tilcdc_unbind(struct device *dev)
614{
Jyri Sarha20a98ac2016-06-23 11:07:16 +0300615 struct drm_device *ddev = dev_get_drvdata(dev);
616
617 /* Check if a subcomponent has already triggered the unloading. */
618 if (!ddev->dev_private)
619 return;
620
Jyri Sarha923310b2016-10-17 17:53:33 +0300621 tilcdc_fini(dev_get_drvdata(dev));
Jyri Sarha103cd8b2015-02-10 14:13:23 +0200622}
623
624static const struct component_master_ops tilcdc_comp_ops = {
625 .bind = tilcdc_bind,
626 .unbind = tilcdc_unbind,
627};
628
Rob Clark16ea9752013-01-08 15:04:28 -0600629static int tilcdc_pdev_probe(struct platform_device *pdev)
630{
Jyri Sarha103cd8b2015-02-10 14:13:23 +0200631 struct component_match *match = NULL;
632 int ret;
633
Rob Clark16ea9752013-01-08 15:04:28 -0600634 /* bail out early if no DT data: */
635 if (!pdev->dev.of_node) {
636 dev_err(&pdev->dev, "device-tree data is missing\n");
637 return -ENXIO;
638 }
639
Jyri Sarha103cd8b2015-02-10 14:13:23 +0200640 ret = tilcdc_get_external_components(&pdev->dev, &match);
641 if (ret < 0)
642 return ret;
643 else if (ret == 0)
Jyri Sarha923310b2016-10-17 17:53:33 +0300644 return tilcdc_init(&tilcdc_driver, &pdev->dev);
Jyri Sarha103cd8b2015-02-10 14:13:23 +0200645 else
646 return component_master_add_with_match(&pdev->dev,
647 &tilcdc_comp_ops,
648 match);
Rob Clark16ea9752013-01-08 15:04:28 -0600649}
650
651static int tilcdc_pdev_remove(struct platform_device *pdev)
652{
Jyri Sarha20a98ac2016-06-23 11:07:16 +0300653 int ret;
Jyri Sarha103cd8b2015-02-10 14:13:23 +0200654
Jyri Sarha20a98ac2016-06-23 11:07:16 +0300655 ret = tilcdc_get_external_components(&pdev->dev, NULL);
656 if (ret < 0)
657 return ret;
658 else if (ret == 0)
Jyri Sarha923310b2016-10-17 17:53:33 +0300659 tilcdc_fini(platform_get_drvdata(pdev));
Jyri Sarha20a98ac2016-06-23 11:07:16 +0300660 else
661 component_master_del(&pdev->dev, &tilcdc_comp_ops);
Rob Clark16ea9752013-01-08 15:04:28 -0600662
663 return 0;
664}
665
666static struct of_device_id tilcdc_of_match[] = {
667 { .compatible = "ti,am33xx-tilcdc", },
Bartosz Golaszewski507b72b2016-10-03 17:45:19 +0200668 { .compatible = "ti,da850-tilcdc", },
Rob Clark16ea9752013-01-08 15:04:28 -0600669 { },
670};
671MODULE_DEVICE_TABLE(of, tilcdc_of_match);
672
673static struct platform_driver tilcdc_platform_driver = {
674 .probe = tilcdc_pdev_probe,
675 .remove = tilcdc_pdev_remove,
676 .driver = {
Rob Clark16ea9752013-01-08 15:04:28 -0600677 .name = "tilcdc",
678 .pm = &tilcdc_pm_ops,
679 .of_match_table = tilcdc_of_match,
680 },
681};
682
683static int __init tilcdc_drm_init(void)
684{
685 DBG("init");
686 tilcdc_tfp410_init();
Rob Clark0d4bbaf2012-12-18 17:34:16 -0600687 tilcdc_panel_init();
Rob Clark16ea9752013-01-08 15:04:28 -0600688 return platform_driver_register(&tilcdc_platform_driver);
689}
690
691static void __exit tilcdc_drm_fini(void)
692{
693 DBG("fini");
Rob Clark16ea9752013-01-08 15:04:28 -0600694 platform_driver_unregister(&tilcdc_platform_driver);
Guido Martínezeb565a22014-06-17 11:17:08 -0300695 tilcdc_panel_fini();
Guido Martínezeb565a22014-06-17 11:17:08 -0300696 tilcdc_tfp410_fini();
Rob Clark16ea9752013-01-08 15:04:28 -0600697}
698
Guido Martínez2023d842014-06-17 11:17:11 -0300699module_init(tilcdc_drm_init);
Rob Clark16ea9752013-01-08 15:04:28 -0600700module_exit(tilcdc_drm_fini);
701
702MODULE_AUTHOR("Rob Clark <robdclark@gmail.com");
703MODULE_DESCRIPTION("TI LCD Controller DRM Driver");
704MODULE_LICENSE("GPL");