Rob Clark | 6e8de0b | 2013-01-22 16:02:21 -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 | #include <linux/i2c.h> |
Rob Clark | 6e8de0b | 2013-01-22 16:02:21 -0600 | [diff] [blame] | 19 | #include <linux/pinctrl/pinmux.h> |
| 20 | #include <linux/pinctrl/consumer.h> |
| 21 | #include <drm/drm_encoder_slave.h> |
| 22 | |
| 23 | #include "tilcdc_drv.h" |
| 24 | |
| 25 | struct slave_module { |
| 26 | struct tilcdc_module base; |
| 27 | struct i2c_adapter *i2c; |
| 28 | }; |
| 29 | #define to_slave_module(x) container_of(x, struct slave_module, base) |
| 30 | |
| 31 | static const struct tilcdc_panel_info slave_info = { |
| 32 | .bpp = 16, |
| 33 | .ac_bias = 255, |
| 34 | .ac_bias_intrpt = 0, |
| 35 | .dma_burst_sz = 16, |
| 36 | .fdd = 0x80, |
| 37 | .tft_alt_mode = 0, |
| 38 | .sync_edge = 0, |
| 39 | .sync_ctrl = 1, |
| 40 | .raster_order = 0, |
| 41 | }; |
| 42 | |
| 43 | |
| 44 | /* |
| 45 | * Encoder: |
| 46 | */ |
| 47 | |
| 48 | struct slave_encoder { |
| 49 | struct drm_encoder_slave base; |
| 50 | struct slave_module *mod; |
| 51 | }; |
| 52 | #define to_slave_encoder(x) container_of(to_encoder_slave(x), struct slave_encoder, base) |
| 53 | |
| 54 | static inline struct drm_encoder_slave_funcs * |
| 55 | get_slave_funcs(struct drm_encoder *enc) |
| 56 | { |
| 57 | return to_encoder_slave(enc)->slave_funcs; |
| 58 | } |
| 59 | |
| 60 | static void slave_encoder_destroy(struct drm_encoder *encoder) |
| 61 | { |
| 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); |
| 66 | kfree(slave_encoder); |
| 67 | } |
| 68 | |
| 69 | static void slave_encoder_prepare(struct drm_encoder *encoder) |
| 70 | { |
| 71 | drm_i2c_encoder_prepare(encoder); |
| 72 | tilcdc_crtc_set_panel_info(encoder->crtc, &slave_info); |
| 73 | } |
| 74 | |
Darren Etheridge | a976718 | 2013-08-14 21:43:33 +0200 | [diff] [blame] | 75 | static bool slave_encoder_fixup(struct drm_encoder *encoder, |
| 76 | const struct drm_display_mode *mode, |
| 77 | struct drm_display_mode *adjusted_mode) |
| 78 | { |
| 79 | /* |
| 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. |
| 84 | */ |
| 85 | adjusted_mode->hskew = mode->hsync_end - mode->hsync_start; |
| 86 | adjusted_mode->flags |= DRM_MODE_FLAG_HSKEW; |
| 87 | |
| 88 | if (mode->flags & DRM_MODE_FLAG_NHSYNC) { |
| 89 | adjusted_mode->flags |= DRM_MODE_FLAG_PHSYNC; |
| 90 | adjusted_mode->flags &= ~DRM_MODE_FLAG_NHSYNC; |
| 91 | } else { |
| 92 | adjusted_mode->flags |= DRM_MODE_FLAG_NHSYNC; |
| 93 | adjusted_mode->flags &= ~DRM_MODE_FLAG_PHSYNC; |
| 94 | } |
| 95 | |
| 96 | return drm_i2c_encoder_mode_fixup(encoder, mode, adjusted_mode); |
| 97 | } |
| 98 | |
| 99 | |
Rob Clark | 6e8de0b | 2013-01-22 16:02:21 -0600 | [diff] [blame] | 100 | static const struct drm_encoder_funcs slave_encoder_funcs = { |
| 101 | .destroy = slave_encoder_destroy, |
| 102 | }; |
| 103 | |
| 104 | static const struct drm_encoder_helper_funcs slave_encoder_helper_funcs = { |
| 105 | .dpms = drm_i2c_encoder_dpms, |
Darren Etheridge | a976718 | 2013-08-14 21:43:33 +0200 | [diff] [blame] | 106 | .mode_fixup = slave_encoder_fixup, |
Rob Clark | 6e8de0b | 2013-01-22 16:02:21 -0600 | [diff] [blame] | 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, |
| 112 | }; |
| 113 | |
| 114 | static const struct i2c_board_info info = { |
| 115 | I2C_BOARD_INFO("tda998x", 0x70) |
| 116 | }; |
| 117 | |
| 118 | static struct drm_encoder *slave_encoder_create(struct drm_device *dev, |
| 119 | struct slave_module *mod) |
| 120 | { |
| 121 | struct slave_encoder *slave_encoder; |
| 122 | struct drm_encoder *encoder; |
| 123 | int ret; |
| 124 | |
| 125 | slave_encoder = kzalloc(sizeof(*slave_encoder), GFP_KERNEL); |
| 126 | if (!slave_encoder) { |
| 127 | dev_err(dev->dev, "allocation failed\n"); |
| 128 | return NULL; |
| 129 | } |
| 130 | |
| 131 | slave_encoder->mod = mod; |
| 132 | |
| 133 | encoder = &slave_encoder->base.base; |
| 134 | encoder->possible_crtcs = 1; |
| 135 | |
| 136 | ret = drm_encoder_init(dev, encoder, &slave_encoder_funcs, |
| 137 | DRM_MODE_ENCODER_TMDS); |
| 138 | if (ret) |
| 139 | goto fail; |
| 140 | |
| 141 | drm_encoder_helper_add(encoder, &slave_encoder_helper_funcs); |
| 142 | |
| 143 | ret = drm_i2c_encoder_init(dev, to_encoder_slave(encoder), mod->i2c, &info); |
| 144 | if (ret) |
| 145 | goto fail; |
| 146 | |
| 147 | return encoder; |
| 148 | |
| 149 | fail: |
| 150 | slave_encoder_destroy(encoder); |
| 151 | return NULL; |
| 152 | } |
| 153 | |
| 154 | /* |
| 155 | * Connector: |
| 156 | */ |
| 157 | |
| 158 | struct slave_connector { |
| 159 | struct drm_connector base; |
| 160 | |
| 161 | struct drm_encoder *encoder; /* our connected encoder */ |
| 162 | struct slave_module *mod; |
| 163 | }; |
| 164 | #define to_slave_connector(x) container_of(x, struct slave_connector, base) |
| 165 | |
| 166 | static void slave_connector_destroy(struct drm_connector *connector) |
| 167 | { |
| 168 | struct slave_connector *slave_connector = to_slave_connector(connector); |
| 169 | drm_connector_cleanup(connector); |
| 170 | kfree(slave_connector); |
| 171 | } |
| 172 | |
| 173 | static enum drm_connector_status slave_connector_detect( |
| 174 | struct drm_connector *connector, |
| 175 | bool force) |
| 176 | { |
| 177 | struct drm_encoder *encoder = to_slave_connector(connector)->encoder; |
| 178 | return get_slave_funcs(encoder)->detect(encoder, connector); |
| 179 | } |
| 180 | |
| 181 | static int slave_connector_get_modes(struct drm_connector *connector) |
| 182 | { |
| 183 | struct drm_encoder *encoder = to_slave_connector(connector)->encoder; |
| 184 | return get_slave_funcs(encoder)->get_modes(encoder, connector); |
| 185 | } |
| 186 | |
| 187 | static int slave_connector_mode_valid(struct drm_connector *connector, |
| 188 | struct drm_display_mode *mode) |
| 189 | { |
| 190 | struct drm_encoder *encoder = to_slave_connector(connector)->encoder; |
| 191 | struct tilcdc_drm_private *priv = connector->dev->dev_private; |
| 192 | int ret; |
| 193 | |
| 194 | ret = tilcdc_crtc_mode_valid(priv->crtc, mode); |
| 195 | if (ret != MODE_OK) |
| 196 | return ret; |
| 197 | |
| 198 | return get_slave_funcs(encoder)->mode_valid(encoder, mode); |
| 199 | } |
| 200 | |
| 201 | static struct drm_encoder *slave_connector_best_encoder( |
| 202 | struct drm_connector *connector) |
| 203 | { |
| 204 | struct slave_connector *slave_connector = to_slave_connector(connector); |
| 205 | return slave_connector->encoder; |
| 206 | } |
| 207 | |
| 208 | static int slave_connector_set_property(struct drm_connector *connector, |
| 209 | struct drm_property *property, uint64_t value) |
| 210 | { |
| 211 | struct drm_encoder *encoder = to_slave_connector(connector)->encoder; |
| 212 | return get_slave_funcs(encoder)->set_property(encoder, |
| 213 | connector, property, value); |
| 214 | } |
| 215 | |
| 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, |
| 222 | }; |
| 223 | |
| 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, |
| 228 | }; |
| 229 | |
| 230 | static struct drm_connector *slave_connector_create(struct drm_device *dev, |
| 231 | struct slave_module *mod, struct drm_encoder *encoder) |
| 232 | { |
| 233 | struct slave_connector *slave_connector; |
| 234 | struct drm_connector *connector; |
| 235 | int ret; |
| 236 | |
| 237 | slave_connector = kzalloc(sizeof(*slave_connector), GFP_KERNEL); |
| 238 | if (!slave_connector) { |
| 239 | dev_err(dev->dev, "allocation failed\n"); |
| 240 | return NULL; |
| 241 | } |
| 242 | |
| 243 | slave_connector->encoder = encoder; |
| 244 | slave_connector->mod = mod; |
| 245 | |
| 246 | connector = &slave_connector->base; |
| 247 | |
| 248 | drm_connector_init(dev, connector, &slave_connector_funcs, |
| 249 | DRM_MODE_CONNECTOR_HDMIA); |
| 250 | drm_connector_helper_add(connector, &slave_connector_helper_funcs); |
| 251 | |
| 252 | connector->polled = DRM_CONNECTOR_POLL_CONNECT | |
| 253 | DRM_CONNECTOR_POLL_DISCONNECT; |
| 254 | |
| 255 | connector->interlace_allowed = 0; |
| 256 | connector->doublescan_allowed = 0; |
| 257 | |
| 258 | get_slave_funcs(encoder)->create_resources(encoder, connector); |
| 259 | |
| 260 | ret = drm_mode_connector_attach_encoder(connector, encoder); |
| 261 | if (ret) |
| 262 | goto fail; |
| 263 | |
| 264 | drm_sysfs_connector_add(connector); |
| 265 | |
| 266 | return connector; |
| 267 | |
| 268 | fail: |
| 269 | slave_connector_destroy(connector); |
| 270 | return NULL; |
| 271 | } |
| 272 | |
| 273 | /* |
| 274 | * Module: |
| 275 | */ |
| 276 | |
| 277 | static int slave_modeset_init(struct tilcdc_module *mod, struct drm_device *dev) |
| 278 | { |
| 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; |
| 283 | |
| 284 | encoder = slave_encoder_create(dev, slave_mod); |
| 285 | if (!encoder) |
| 286 | return -ENOMEM; |
| 287 | |
| 288 | connector = slave_connector_create(dev, slave_mod, encoder); |
| 289 | if (!connector) |
| 290 | return -ENOMEM; |
| 291 | |
| 292 | priv->encoders[priv->num_encoders++] = encoder; |
| 293 | priv->connectors[priv->num_connectors++] = connector; |
| 294 | |
| 295 | return 0; |
| 296 | } |
| 297 | |
| 298 | static void slave_destroy(struct tilcdc_module *mod) |
| 299 | { |
| 300 | struct slave_module *slave_mod = to_slave_module(mod); |
| 301 | |
| 302 | tilcdc_module_cleanup(mod); |
| 303 | kfree(slave_mod); |
| 304 | } |
| 305 | |
| 306 | static const struct tilcdc_module_ops slave_module_ops = { |
| 307 | .modeset_init = slave_modeset_init, |
| 308 | .destroy = slave_destroy, |
| 309 | }; |
| 310 | |
| 311 | /* |
| 312 | * Device: |
| 313 | */ |
| 314 | |
| 315 | static struct of_device_id slave_of_match[]; |
| 316 | |
| 317 | static int slave_probe(struct platform_device *pdev) |
| 318 | { |
| 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; |
Darren Etheridge | 39de619 | 2013-06-21 13:52:27 -0500 | [diff] [blame] | 325 | struct i2c_adapter *slavei2c; |
Rob Clark | 6e8de0b | 2013-01-22 16:02:21 -0600 | [diff] [blame] | 326 | int ret = -EINVAL; |
| 327 | |
| 328 | /* bail out early if no DT data: */ |
| 329 | if (!node) { |
| 330 | dev_err(&pdev->dev, "device-tree data is missing\n"); |
| 331 | return -ENXIO; |
| 332 | } |
| 333 | |
Darren Etheridge | 39de619 | 2013-06-21 13:52:27 -0500 | [diff] [blame] | 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"); |
| 337 | return ret; |
| 338 | } |
| 339 | |
| 340 | i2c_node = of_find_node_by_phandle(i2c_phandle); |
| 341 | if (!i2c_node) { |
| 342 | dev_err(&pdev->dev, "could not get i2c bus node\n"); |
| 343 | return ret; |
| 344 | } |
| 345 | |
| 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); |
| 349 | |
| 350 | if (!slavei2c) { |
| 351 | ret = -EPROBE_DEFER; |
| 352 | tilcdc_slave_probedefer(true); |
| 353 | dev_err(&pdev->dev, "could not get i2c\n"); |
| 354 | return ret; |
| 355 | } |
| 356 | |
Rob Clark | 6e8de0b | 2013-01-22 16:02:21 -0600 | [diff] [blame] | 357 | slave_mod = kzalloc(sizeof(*slave_mod), GFP_KERNEL); |
| 358 | if (!slave_mod) |
| 359 | return -ENOMEM; |
| 360 | |
| 361 | mod = &slave_mod->base; |
| 362 | |
Darren Etheridge | 39de619 | 2013-06-21 13:52:27 -0500 | [diff] [blame] | 363 | mod->preferred_bpp = slave_info.bpp; |
| 364 | |
| 365 | slave_mod->i2c = slavei2c; |
| 366 | |
Rob Clark | 6e8de0b | 2013-01-22 16:02:21 -0600 | [diff] [blame] | 367 | tilcdc_module_init(mod, "slave", &slave_module_ops); |
| 368 | |
| 369 | pinctrl = devm_pinctrl_get_select_default(&pdev->dev); |
| 370 | if (IS_ERR(pinctrl)) |
| 371 | dev_warn(&pdev->dev, "pins are not configured\n"); |
| 372 | |
Darren Etheridge | 39de619 | 2013-06-21 13:52:27 -0500 | [diff] [blame] | 373 | tilcdc_slave_probedefer(false); |
Rob Clark | 6e8de0b | 2013-01-22 16:02:21 -0600 | [diff] [blame] | 374 | |
| 375 | return 0; |
Rob Clark | 6e8de0b | 2013-01-22 16:02:21 -0600 | [diff] [blame] | 376 | } |
| 377 | |
| 378 | static int slave_remove(struct platform_device *pdev) |
| 379 | { |
| 380 | return 0; |
| 381 | } |
| 382 | |
| 383 | static struct of_device_id slave_of_match[] = { |
| 384 | { .compatible = "ti,tilcdc,slave", }, |
| 385 | { }, |
| 386 | }; |
Rob Clark | 6e8de0b | 2013-01-22 16:02:21 -0600 | [diff] [blame] | 387 | |
| 388 | struct platform_driver slave_driver = { |
| 389 | .probe = slave_probe, |
| 390 | .remove = slave_remove, |
| 391 | .driver = { |
| 392 | .owner = THIS_MODULE, |
| 393 | .name = "slave", |
| 394 | .of_match_table = slave_of_match, |
| 395 | }, |
| 396 | }; |
| 397 | |
| 398 | int __init tilcdc_slave_init(void) |
| 399 | { |
| 400 | return platform_driver_register(&slave_driver); |
| 401 | } |
| 402 | |
| 403 | void __exit tilcdc_slave_fini(void) |
| 404 | { |
| 405 | platform_driver_unregister(&slave_driver); |
| 406 | } |