|
| 1 | +/* |
| 2 | + * NXP PTN3460 DP/LVDS bridge driver |
| 3 | + * |
| 4 | + * Copyright (C) 2013 Google, Inc. |
| 5 | + * |
| 6 | + * This software is licensed under the terms of the GNU General Public |
| 7 | + * License version 2, as published by the Free Software Foundation, and |
| 8 | + * may be copied, distributed, and modified under those terms. |
| 9 | + * |
| 10 | + * This program is distributed in the hope that it will be useful, |
| 11 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | + * GNU General Public License for more details. |
| 14 | + */ |
| 15 | + |
| 16 | +#include <linux/module.h> |
| 17 | +#include <linux/of.h> |
| 18 | +#include <linux/of_gpio.h> |
| 19 | +#include <linux/i2c.h> |
| 20 | +#include <linux/gpio.h> |
| 21 | +#include <linux/delay.h> |
| 22 | + |
| 23 | +#include "drmP.h" |
| 24 | +#include "drm_edid.h" |
| 25 | +#include "drm_crtc.h" |
| 26 | +#include "drm_crtc_helper.h" |
| 27 | + |
| 28 | +#include "bridge/ptn3460.h" |
| 29 | + |
| 30 | +#define PTN3460_EDID_ADDR 0x0 |
| 31 | +#define PTN3460_EDID_EMULATION_ADDR 0x84 |
| 32 | +#define PTN3460_EDID_ENABLE_EMULATION 0 |
| 33 | +#define PTN3460_EDID_EMULATION_SELECTION 1 |
| 34 | +#define PTN3460_EDID_SRAM_LOAD_ADDR 0x85 |
| 35 | + |
| 36 | +struct ptn3460_bridge { |
| 37 | + struct drm_connector connector; |
| 38 | + struct i2c_client *client; |
| 39 | + struct drm_encoder *encoder; |
| 40 | + struct drm_bridge *bridge; |
| 41 | + struct edid *edid; |
| 42 | + int gpio_pd_n; |
| 43 | + int gpio_rst_n; |
| 44 | + u32 edid_emulation; |
| 45 | + bool enabled; |
| 46 | +}; |
| 47 | + |
| 48 | +static int ptn3460_read_bytes(struct ptn3460_bridge *ptn_bridge, char addr, |
| 49 | + u8 *buf, int len) |
| 50 | +{ |
| 51 | + int ret; |
| 52 | + |
| 53 | + ret = i2c_master_send(ptn_bridge->client, &addr, 1); |
| 54 | + if (ret <= 0) { |
| 55 | + DRM_ERROR("Failed to send i2c command, ret=%d\n", ret); |
| 56 | + return ret; |
| 57 | + } |
| 58 | + |
| 59 | + ret = i2c_master_recv(ptn_bridge->client, buf, len); |
| 60 | + if (ret <= 0) { |
| 61 | + DRM_ERROR("Failed to recv i2c data, ret=%d\n", ret); |
| 62 | + return ret; |
| 63 | + } |
| 64 | + |
| 65 | + return 0; |
| 66 | +} |
| 67 | + |
| 68 | +static int ptn3460_write_byte(struct ptn3460_bridge *ptn_bridge, char addr, |
| 69 | + char val) |
| 70 | +{ |
| 71 | + int ret; |
| 72 | + char buf[2]; |
| 73 | + |
| 74 | + buf[0] = addr; |
| 75 | + buf[1] = val; |
| 76 | + |
| 77 | + ret = i2c_master_send(ptn_bridge->client, buf, ARRAY_SIZE(buf)); |
| 78 | + if (ret <= 0) { |
| 79 | + DRM_ERROR("Failed to send i2c command, ret=%d\n", ret); |
| 80 | + return ret; |
| 81 | + } |
| 82 | + |
| 83 | + return 0; |
| 84 | +} |
| 85 | + |
| 86 | +static int ptn3460_select_edid(struct ptn3460_bridge *ptn_bridge) |
| 87 | +{ |
| 88 | + int ret; |
| 89 | + char val; |
| 90 | + |
| 91 | + /* Load the selected edid into SRAM (accessed at PTN3460_EDID_ADDR) */ |
| 92 | + ret = ptn3460_write_byte(ptn_bridge, PTN3460_EDID_SRAM_LOAD_ADDR, |
| 93 | + ptn_bridge->edid_emulation); |
| 94 | + if (ret) { |
| 95 | + DRM_ERROR("Failed to transfer edid to sram, ret=%d\n", ret); |
| 96 | + return ret; |
| 97 | + } |
| 98 | + |
| 99 | + /* Enable EDID emulation and select the desired EDID */ |
| 100 | + val = 1 << PTN3460_EDID_ENABLE_EMULATION | |
| 101 | + ptn_bridge->edid_emulation << PTN3460_EDID_EMULATION_SELECTION; |
| 102 | + |
| 103 | + ret = ptn3460_write_byte(ptn_bridge, PTN3460_EDID_EMULATION_ADDR, val); |
| 104 | + if (ret) { |
| 105 | + DRM_ERROR("Failed to write edid value, ret=%d\n", ret); |
| 106 | + return ret; |
| 107 | + } |
| 108 | + |
| 109 | + return 0; |
| 110 | +} |
| 111 | + |
| 112 | +static void ptn3460_pre_enable(struct drm_bridge *bridge) |
| 113 | +{ |
| 114 | + struct ptn3460_bridge *ptn_bridge = bridge->driver_private; |
| 115 | + int ret; |
| 116 | + |
| 117 | + if (ptn_bridge->enabled) |
| 118 | + return; |
| 119 | + |
| 120 | + if (gpio_is_valid(ptn_bridge->gpio_pd_n)) |
| 121 | + gpio_set_value(ptn_bridge->gpio_pd_n, 1); |
| 122 | + |
| 123 | + if (gpio_is_valid(ptn_bridge->gpio_rst_n)) { |
| 124 | + gpio_set_value(ptn_bridge->gpio_rst_n, 0); |
| 125 | + udelay(10); |
| 126 | + gpio_set_value(ptn_bridge->gpio_rst_n, 1); |
| 127 | + } |
| 128 | + |
| 129 | + /* |
| 130 | + * There's a bug in the PTN chip where it falsely asserts hotplug before |
| 131 | + * it is fully functional. We're forced to wait for the maximum start up |
| 132 | + * time specified in the chip's datasheet to make sure we're really up. |
| 133 | + */ |
| 134 | + msleep(90); |
| 135 | + |
| 136 | + ret = ptn3460_select_edid(ptn_bridge); |
| 137 | + if (ret) |
| 138 | + DRM_ERROR("Select edid failed ret=%d\n", ret); |
| 139 | + |
| 140 | + ptn_bridge->enabled = true; |
| 141 | +} |
| 142 | + |
| 143 | +static void ptn3460_enable(struct drm_bridge *bridge) |
| 144 | +{ |
| 145 | +} |
| 146 | + |
| 147 | +static void ptn3460_disable(struct drm_bridge *bridge) |
| 148 | +{ |
| 149 | + struct ptn3460_bridge *ptn_bridge = bridge->driver_private; |
| 150 | + |
| 151 | + if (!ptn_bridge->enabled) |
| 152 | + return; |
| 153 | + |
| 154 | + ptn_bridge->enabled = false; |
| 155 | + |
| 156 | + if (gpio_is_valid(ptn_bridge->gpio_rst_n)) |
| 157 | + gpio_set_value(ptn_bridge->gpio_rst_n, 1); |
| 158 | + |
| 159 | + if (gpio_is_valid(ptn_bridge->gpio_pd_n)) |
| 160 | + gpio_set_value(ptn_bridge->gpio_pd_n, 0); |
| 161 | +} |
| 162 | + |
| 163 | +static void ptn3460_post_disable(struct drm_bridge *bridge) |
| 164 | +{ |
| 165 | +} |
| 166 | + |
| 167 | +void ptn3460_bridge_destroy(struct drm_bridge *bridge) |
| 168 | +{ |
| 169 | + struct ptn3460_bridge *ptn_bridge = bridge->driver_private; |
| 170 | + |
| 171 | + drm_bridge_cleanup(bridge); |
| 172 | + if (gpio_is_valid(ptn_bridge->gpio_pd_n)) |
| 173 | + gpio_free(ptn_bridge->gpio_pd_n); |
| 174 | + if (gpio_is_valid(ptn_bridge->gpio_rst_n)) |
| 175 | + gpio_free(ptn_bridge->gpio_rst_n); |
| 176 | + /* Nothing else to free, we've got devm allocated memory */ |
| 177 | +} |
| 178 | + |
| 179 | +struct drm_bridge_funcs ptn3460_bridge_funcs = { |
| 180 | + .pre_enable = ptn3460_pre_enable, |
| 181 | + .enable = ptn3460_enable, |
| 182 | + .disable = ptn3460_disable, |
| 183 | + .post_disable = ptn3460_post_disable, |
| 184 | + .destroy = ptn3460_bridge_destroy, |
| 185 | +}; |
| 186 | + |
| 187 | +int ptn3460_get_modes(struct drm_connector *connector) |
| 188 | +{ |
| 189 | + struct ptn3460_bridge *ptn_bridge; |
| 190 | + u8 *edid; |
| 191 | + int ret, num_modes; |
| 192 | + bool power_off; |
| 193 | + |
| 194 | + ptn_bridge = container_of(connector, struct ptn3460_bridge, connector); |
| 195 | + |
| 196 | + if (ptn_bridge->edid) |
| 197 | + return drm_add_edid_modes(connector, ptn_bridge->edid); |
| 198 | + |
| 199 | + power_off = !ptn_bridge->enabled; |
| 200 | + ptn3460_pre_enable(ptn_bridge->bridge); |
| 201 | + |
| 202 | + edid = kmalloc(EDID_LENGTH, GFP_KERNEL); |
| 203 | + if (!edid) { |
| 204 | + DRM_ERROR("Failed to allocate edid\n"); |
| 205 | + return 0; |
| 206 | + } |
| 207 | + |
| 208 | + ret = ptn3460_read_bytes(ptn_bridge, PTN3460_EDID_ADDR, edid, |
| 209 | + EDID_LENGTH); |
| 210 | + if (ret) { |
| 211 | + kfree(edid); |
| 212 | + num_modes = 0; |
| 213 | + goto out; |
| 214 | + } |
| 215 | + |
| 216 | + ptn_bridge->edid = (struct edid *)edid; |
| 217 | + drm_mode_connector_update_edid_property(connector, ptn_bridge->edid); |
| 218 | + |
| 219 | + num_modes = drm_add_edid_modes(connector, ptn_bridge->edid); |
| 220 | + |
| 221 | +out: |
| 222 | + if (power_off) |
| 223 | + ptn3460_disable(ptn_bridge->bridge); |
| 224 | + |
| 225 | + return num_modes; |
| 226 | +} |
| 227 | + |
| 228 | +static int ptn3460_mode_valid(struct drm_connector *connector, |
| 229 | + struct drm_display_mode *mode) |
| 230 | +{ |
| 231 | + return MODE_OK; |
| 232 | +} |
| 233 | + |
| 234 | +struct drm_encoder *ptn3460_best_encoder(struct drm_connector *connector) |
| 235 | +{ |
| 236 | + struct ptn3460_bridge *ptn_bridge; |
| 237 | + |
| 238 | + ptn_bridge = container_of(connector, struct ptn3460_bridge, connector); |
| 239 | + |
| 240 | + return ptn_bridge->encoder; |
| 241 | +} |
| 242 | + |
| 243 | +struct drm_connector_helper_funcs ptn3460_connector_helper_funcs = { |
| 244 | + .get_modes = ptn3460_get_modes, |
| 245 | + .mode_valid = ptn3460_mode_valid, |
| 246 | + .best_encoder = ptn3460_best_encoder, |
| 247 | +}; |
| 248 | + |
| 249 | +enum drm_connector_status ptn3460_detect(struct drm_connector *connector, |
| 250 | + bool force) |
| 251 | +{ |
| 252 | + return connector_status_connected; |
| 253 | +} |
| 254 | + |
| 255 | +void ptn3460_connector_destroy(struct drm_connector *connector) |
| 256 | +{ |
| 257 | + drm_connector_cleanup(connector); |
| 258 | +} |
| 259 | + |
| 260 | +struct drm_connector_funcs ptn3460_connector_funcs = { |
| 261 | + .dpms = drm_helper_connector_dpms, |
| 262 | + .fill_modes = drm_helper_probe_single_connector_modes, |
| 263 | + .detect = ptn3460_detect, |
| 264 | + .destroy = ptn3460_connector_destroy, |
| 265 | +}; |
| 266 | + |
| 267 | +int ptn3460_init(struct drm_device *dev, struct drm_encoder *encoder, |
| 268 | + struct i2c_client *client, struct device_node *node) |
| 269 | +{ |
| 270 | + int ret; |
| 271 | + struct drm_bridge *bridge; |
| 272 | + struct ptn3460_bridge *ptn_bridge; |
| 273 | + |
| 274 | + bridge = devm_kzalloc(dev->dev, sizeof(*bridge), GFP_KERNEL); |
| 275 | + if (!bridge) { |
| 276 | + DRM_ERROR("Failed to allocate drm bridge\n"); |
| 277 | + return -ENOMEM; |
| 278 | + } |
| 279 | + |
| 280 | + ptn_bridge = devm_kzalloc(dev->dev, sizeof(*ptn_bridge), GFP_KERNEL); |
| 281 | + if (!ptn_bridge) { |
| 282 | + DRM_ERROR("Failed to allocate ptn bridge\n"); |
| 283 | + return -ENOMEM; |
| 284 | + } |
| 285 | + |
| 286 | + ptn_bridge->client = client; |
| 287 | + ptn_bridge->encoder = encoder; |
| 288 | + ptn_bridge->bridge = bridge; |
| 289 | + ptn_bridge->gpio_pd_n = of_get_named_gpio(node, "powerdown-gpio", 0); |
| 290 | + if (gpio_is_valid(ptn_bridge->gpio_pd_n)) { |
| 291 | + ret = gpio_request_one(ptn_bridge->gpio_pd_n, |
| 292 | + GPIOF_OUT_INIT_HIGH, "PTN3460_PD_N"); |
| 293 | + if (ret) { |
| 294 | + DRM_ERROR("Request powerdown-gpio failed (%d)\n", ret); |
| 295 | + return ret; |
| 296 | + } |
| 297 | + } |
| 298 | + |
| 299 | + ptn_bridge->gpio_rst_n = of_get_named_gpio(node, "reset-gpio", 0); |
| 300 | + if (gpio_is_valid(ptn_bridge->gpio_rst_n)) { |
| 301 | + /* |
| 302 | + * Request the reset pin low to avoid the bridge being |
| 303 | + * initialized prematurely |
| 304 | + */ |
| 305 | + ret = gpio_request_one(ptn_bridge->gpio_rst_n, |
| 306 | + GPIOF_OUT_INIT_LOW, "PTN3460_RST_N"); |
| 307 | + if (ret) { |
| 308 | + DRM_ERROR("Request reset-gpio failed (%d)\n", ret); |
| 309 | + gpio_free(ptn_bridge->gpio_pd_n); |
| 310 | + return ret; |
| 311 | + } |
| 312 | + } |
| 313 | + |
| 314 | + ret = of_property_read_u32(node, "edid-emulation", |
| 315 | + &ptn_bridge->edid_emulation); |
| 316 | + if (ret) { |
| 317 | + DRM_ERROR("Can't read edid emulation value\n"); |
| 318 | + goto err; |
| 319 | + } |
| 320 | + |
| 321 | + ret = drm_bridge_init(dev, bridge, &ptn3460_bridge_funcs); |
| 322 | + if (ret) { |
| 323 | + DRM_ERROR("Failed to initialize bridge with drm\n"); |
| 324 | + goto err; |
| 325 | + } |
| 326 | + |
| 327 | + bridge->driver_private = ptn_bridge; |
| 328 | + encoder->bridge = bridge; |
| 329 | + |
| 330 | + ret = drm_connector_init(dev, &ptn_bridge->connector, |
| 331 | + &ptn3460_connector_funcs, DRM_MODE_CONNECTOR_LVDS); |
| 332 | + if (ret) { |
| 333 | + DRM_ERROR("Failed to initialize connector with drm\n"); |
| 334 | + goto err; |
| 335 | + } |
| 336 | + drm_connector_helper_add(&ptn_bridge->connector, |
| 337 | + &ptn3460_connector_helper_funcs); |
| 338 | + drm_sysfs_connector_add(&ptn_bridge->connector); |
| 339 | + drm_mode_connector_attach_encoder(&ptn_bridge->connector, encoder); |
| 340 | + |
| 341 | + return 0; |
| 342 | + |
| 343 | +err: |
| 344 | + if (gpio_is_valid(ptn_bridge->gpio_pd_n)) |
| 345 | + gpio_free(ptn_bridge->gpio_pd_n); |
| 346 | + if (gpio_is_valid(ptn_bridge->gpio_rst_n)) |
| 347 | + gpio_free(ptn_bridge->gpio_rst_n); |
| 348 | + return ret; |
| 349 | +} |
0 commit comments