2 * Copyright (C) 2012 Texas Instruments
3 * Author: Rob Clark <robdclark@gmail.com>
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.
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
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/>.
18 #include <linux/i2c.h>
19 #include <linux/pinctrl/pinmux.h>
20 #include <linux/pinctrl/consumer.h>
21 #include <drm/drm_encoder_slave.h>
23 #include "tilcdc_drv.h"
26 struct tilcdc_module base
;
27 struct i2c_adapter
*i2c
;
29 #define to_slave_module(x) container_of(x, struct slave_module, base)
31 static const struct tilcdc_panel_info slave_info
= {
48 struct slave_encoder
{
49 struct drm_encoder_slave base
;
50 struct slave_module
*mod
;
52 #define to_slave_encoder(x) container_of(to_encoder_slave(x), struct slave_encoder, base)
54 static inline struct drm_encoder_slave_funcs
*
55 get_slave_funcs(struct drm_encoder
*enc
)
57 return to_encoder_slave(enc
)->slave_funcs
;
60 static void slave_encoder_destroy(struct drm_encoder
*encoder
)
62 struct slave_encoder
*slave_encoder
= to_slave_encoder(encoder
);
63 if (get_slave_funcs(encoder
))
64 get_slave_funcs(encoder
)->destroy(encoder
);
65 drm_encoder_cleanup(encoder
);
69 static void slave_encoder_prepare(struct drm_encoder
*encoder
)
71 drm_i2c_encoder_prepare(encoder
);
72 tilcdc_crtc_set_panel_info(encoder
->crtc
, &slave_info
);
75 static bool slave_encoder_fixup(struct drm_encoder
*encoder
,
76 const struct drm_display_mode
*mode
,
77 struct drm_display_mode
*adjusted_mode
)
80 * tilcdc does not generate VESA-complient sync but aligns
81 * VS on the second edge of HS instead of first edge.
82 * We use adjusted_mode, to fixup sync by aligning both rising
83 * edges and add HSKEW offset to let the slave encoder fix it up.
85 adjusted_mode
->hskew
= mode
->hsync_end
- mode
->hsync_start
;
86 adjusted_mode
->flags
|= DRM_MODE_FLAG_HSKEW
;
88 if (mode
->flags
& DRM_MODE_FLAG_NHSYNC
) {
89 adjusted_mode
->flags
|= DRM_MODE_FLAG_PHSYNC
;
90 adjusted_mode
->flags
&= ~DRM_MODE_FLAG_NHSYNC
;
92 adjusted_mode
->flags
|= DRM_MODE_FLAG_NHSYNC
;
93 adjusted_mode
->flags
&= ~DRM_MODE_FLAG_PHSYNC
;
96 return drm_i2c_encoder_mode_fixup(encoder
, mode
, adjusted_mode
);
100 static const struct drm_encoder_funcs slave_encoder_funcs
= {
101 .destroy
= slave_encoder_destroy
,
104 static const struct drm_encoder_helper_funcs slave_encoder_helper_funcs
= {
105 .dpms
= drm_i2c_encoder_dpms
,
106 .mode_fixup
= slave_encoder_fixup
,
107 .prepare
= slave_encoder_prepare
,
108 .commit
= drm_i2c_encoder_commit
,
109 .mode_set
= drm_i2c_encoder_mode_set
,
110 .save
= drm_i2c_encoder_save
,
111 .restore
= drm_i2c_encoder_restore
,
114 static const struct i2c_board_info info
= {
115 I2C_BOARD_INFO("tda998x", 0x70)
118 static struct drm_encoder
*slave_encoder_create(struct drm_device
*dev
,
119 struct slave_module
*mod
)
121 struct slave_encoder
*slave_encoder
;
122 struct drm_encoder
*encoder
;
125 slave_encoder
= kzalloc(sizeof(*slave_encoder
), GFP_KERNEL
);
126 if (!slave_encoder
) {
127 dev_err(dev
->dev
, "allocation failed\n");
131 slave_encoder
->mod
= mod
;
133 encoder
= &slave_encoder
->base
.base
;
134 encoder
->possible_crtcs
= 1;
136 ret
= drm_encoder_init(dev
, encoder
, &slave_encoder_funcs
,
137 DRM_MODE_ENCODER_TMDS
);
141 drm_encoder_helper_add(encoder
, &slave_encoder_helper_funcs
);
143 ret
= drm_i2c_encoder_init(dev
, to_encoder_slave(encoder
), mod
->i2c
, &info
);
150 slave_encoder_destroy(encoder
);
158 struct slave_connector
{
159 struct drm_connector base
;
161 struct drm_encoder
*encoder
; /* our connected encoder */
162 struct slave_module
*mod
;
164 #define to_slave_connector(x) container_of(x, struct slave_connector, base)
166 static void slave_connector_destroy(struct drm_connector
*connector
)
168 struct slave_connector
*slave_connector
= to_slave_connector(connector
);
169 drm_connector_cleanup(connector
);
170 kfree(slave_connector
);
173 static enum drm_connector_status
slave_connector_detect(
174 struct drm_connector
*connector
,
177 struct drm_encoder
*encoder
= to_slave_connector(connector
)->encoder
;
178 return get_slave_funcs(encoder
)->detect(encoder
, connector
);
181 static int slave_connector_get_modes(struct drm_connector
*connector
)
183 struct drm_encoder
*encoder
= to_slave_connector(connector
)->encoder
;
184 return get_slave_funcs(encoder
)->get_modes(encoder
, connector
);
187 static int slave_connector_mode_valid(struct drm_connector
*connector
,
188 struct drm_display_mode
*mode
)
190 struct drm_encoder
*encoder
= to_slave_connector(connector
)->encoder
;
191 struct tilcdc_drm_private
*priv
= connector
->dev
->dev_private
;
194 ret
= tilcdc_crtc_mode_valid(priv
->crtc
, mode
);
198 return get_slave_funcs(encoder
)->mode_valid(encoder
, mode
);
201 static struct drm_encoder
*slave_connector_best_encoder(
202 struct drm_connector
*connector
)
204 struct slave_connector
*slave_connector
= to_slave_connector(connector
);
205 return slave_connector
->encoder
;
208 static int slave_connector_set_property(struct drm_connector
*connector
,
209 struct drm_property
*property
, uint64_t value
)
211 struct drm_encoder
*encoder
= to_slave_connector(connector
)->encoder
;
212 return get_slave_funcs(encoder
)->set_property(encoder
,
213 connector
, property
, value
);
216 static const struct drm_connector_funcs slave_connector_funcs
= {
217 .destroy
= slave_connector_destroy
,
218 .dpms
= drm_helper_connector_dpms
,
219 .detect
= slave_connector_detect
,
220 .fill_modes
= drm_helper_probe_single_connector_modes
,
221 .set_property
= slave_connector_set_property
,
224 static const struct drm_connector_helper_funcs slave_connector_helper_funcs
= {
225 .get_modes
= slave_connector_get_modes
,
226 .mode_valid
= slave_connector_mode_valid
,
227 .best_encoder
= slave_connector_best_encoder
,
230 static struct drm_connector
*slave_connector_create(struct drm_device
*dev
,
231 struct slave_module
*mod
, struct drm_encoder
*encoder
)
233 struct slave_connector
*slave_connector
;
234 struct drm_connector
*connector
;
237 slave_connector
= kzalloc(sizeof(*slave_connector
), GFP_KERNEL
);
238 if (!slave_connector
) {
239 dev_err(dev
->dev
, "allocation failed\n");
243 slave_connector
->encoder
= encoder
;
244 slave_connector
->mod
= mod
;
246 connector
= &slave_connector
->base
;
248 drm_connector_init(dev
, connector
, &slave_connector_funcs
,
249 DRM_MODE_CONNECTOR_HDMIA
);
250 drm_connector_helper_add(connector
, &slave_connector_helper_funcs
);
252 connector
->polled
= DRM_CONNECTOR_POLL_CONNECT
|
253 DRM_CONNECTOR_POLL_DISCONNECT
;
255 connector
->interlace_allowed
= 0;
256 connector
->doublescan_allowed
= 0;
258 get_slave_funcs(encoder
)->create_resources(encoder
, connector
);
260 ret
= drm_mode_connector_attach_encoder(connector
, encoder
);
264 drm_sysfs_connector_add(connector
);
269 slave_connector_destroy(connector
);
277 static int slave_modeset_init(struct tilcdc_module
*mod
, struct drm_device
*dev
)
279 struct slave_module
*slave_mod
= to_slave_module(mod
);
280 struct tilcdc_drm_private
*priv
= dev
->dev_private
;
281 struct drm_encoder
*encoder
;
282 struct drm_connector
*connector
;
284 encoder
= slave_encoder_create(dev
, slave_mod
);
288 connector
= slave_connector_create(dev
, slave_mod
, encoder
);
292 priv
->encoders
[priv
->num_encoders
++] = encoder
;
293 priv
->connectors
[priv
->num_connectors
++] = connector
;
298 static void slave_destroy(struct tilcdc_module
*mod
)
300 struct slave_module
*slave_mod
= to_slave_module(mod
);
302 tilcdc_module_cleanup(mod
);
306 static const struct tilcdc_module_ops slave_module_ops
= {
307 .modeset_init
= slave_modeset_init
,
308 .destroy
= slave_destroy
,
315 static struct of_device_id slave_of_match
[];
317 static int slave_probe(struct platform_device
*pdev
)
319 struct device_node
*node
= pdev
->dev
.of_node
;
320 struct device_node
*i2c_node
;
321 struct slave_module
*slave_mod
;
322 struct tilcdc_module
*mod
;
323 struct pinctrl
*pinctrl
;
324 uint32_t i2c_phandle
;
325 struct i2c_adapter
*slavei2c
;
328 /* bail out early if no DT data: */
330 dev_err(&pdev
->dev
, "device-tree data is missing\n");
334 /* Bail out early if i2c not specified */
335 if (of_property_read_u32(node
, "i2c", &i2c_phandle
)) {
336 dev_err(&pdev
->dev
, "could not get i2c bus phandle\n");
340 i2c_node
= of_find_node_by_phandle(i2c_phandle
);
342 dev_err(&pdev
->dev
, "could not get i2c bus node\n");
346 /* but defer the probe if it can't be initialized it might come later */
347 slavei2c
= of_find_i2c_adapter_by_node(i2c_node
);
348 of_node_put(i2c_node
);
352 tilcdc_slave_probedefer(true);
353 dev_err(&pdev
->dev
, "could not get i2c\n");
357 slave_mod
= kzalloc(sizeof(*slave_mod
), GFP_KERNEL
);
361 mod
= &slave_mod
->base
;
363 mod
->preferred_bpp
= slave_info
.bpp
;
365 slave_mod
->i2c
= slavei2c
;
367 tilcdc_module_init(mod
, "slave", &slave_module_ops
);
369 pinctrl
= devm_pinctrl_get_select_default(&pdev
->dev
);
371 dev_warn(&pdev
->dev
, "pins are not configured\n");
373 tilcdc_slave_probedefer(false);
378 static int slave_remove(struct platform_device
*pdev
)
383 static struct of_device_id slave_of_match
[] = {
384 { .compatible
= "ti,tilcdc,slave", },
388 struct platform_driver slave_driver
= {
389 .probe
= slave_probe
,
390 .remove
= slave_remove
,
392 .owner
= THIS_MODULE
,
394 .of_match_table
= slave_of_match
,
398 int __init
tilcdc_slave_init(void)
400 return platform_driver_register(&slave_driver
);
403 void __exit
tilcdc_slave_fini(void)
405 platform_driver_unregister(&slave_driver
);