Sean Paul | a9fe713 | 2014-02-24 19:31:24 +0900 | [diff] [blame] | 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 | |
Sean Paul | a9fe713 | 2014-02-24 19:31:24 +0900 | [diff] [blame] | 228 | struct drm_encoder *ptn3460_best_encoder(struct drm_connector *connector) |
| 229 | { |
| 230 | struct ptn3460_bridge *ptn_bridge; |
| 231 | |
| 232 | ptn_bridge = container_of(connector, struct ptn3460_bridge, connector); |
| 233 | |
| 234 | return ptn_bridge->encoder; |
| 235 | } |
| 236 | |
| 237 | struct drm_connector_helper_funcs ptn3460_connector_helper_funcs = { |
| 238 | .get_modes = ptn3460_get_modes, |
Sean Paul | a9fe713 | 2014-02-24 19:31:24 +0900 | [diff] [blame] | 239 | .best_encoder = ptn3460_best_encoder, |
| 240 | }; |
| 241 | |
| 242 | enum drm_connector_status ptn3460_detect(struct drm_connector *connector, |
| 243 | bool force) |
| 244 | { |
| 245 | return connector_status_connected; |
| 246 | } |
| 247 | |
| 248 | void ptn3460_connector_destroy(struct drm_connector *connector) |
| 249 | { |
| 250 | drm_connector_cleanup(connector); |
| 251 | } |
| 252 | |
| 253 | struct drm_connector_funcs ptn3460_connector_funcs = { |
| 254 | .dpms = drm_helper_connector_dpms, |
| 255 | .fill_modes = drm_helper_probe_single_connector_modes, |
| 256 | .detect = ptn3460_detect, |
| 257 | .destroy = ptn3460_connector_destroy, |
| 258 | }; |
| 259 | |
| 260 | int ptn3460_init(struct drm_device *dev, struct drm_encoder *encoder, |
| 261 | struct i2c_client *client, struct device_node *node) |
| 262 | { |
| 263 | int ret; |
| 264 | struct drm_bridge *bridge; |
| 265 | struct ptn3460_bridge *ptn_bridge; |
| 266 | |
| 267 | bridge = devm_kzalloc(dev->dev, sizeof(*bridge), GFP_KERNEL); |
| 268 | if (!bridge) { |
| 269 | DRM_ERROR("Failed to allocate drm bridge\n"); |
| 270 | return -ENOMEM; |
| 271 | } |
| 272 | |
| 273 | ptn_bridge = devm_kzalloc(dev->dev, sizeof(*ptn_bridge), GFP_KERNEL); |
| 274 | if (!ptn_bridge) { |
| 275 | DRM_ERROR("Failed to allocate ptn bridge\n"); |
| 276 | return -ENOMEM; |
| 277 | } |
| 278 | |
| 279 | ptn_bridge->client = client; |
| 280 | ptn_bridge->encoder = encoder; |
| 281 | ptn_bridge->bridge = bridge; |
| 282 | ptn_bridge->gpio_pd_n = of_get_named_gpio(node, "powerdown-gpio", 0); |
| 283 | if (gpio_is_valid(ptn_bridge->gpio_pd_n)) { |
| 284 | ret = gpio_request_one(ptn_bridge->gpio_pd_n, |
| 285 | GPIOF_OUT_INIT_HIGH, "PTN3460_PD_N"); |
| 286 | if (ret) { |
| 287 | DRM_ERROR("Request powerdown-gpio failed (%d)\n", ret); |
| 288 | return ret; |
| 289 | } |
| 290 | } |
| 291 | |
| 292 | ptn_bridge->gpio_rst_n = of_get_named_gpio(node, "reset-gpio", 0); |
| 293 | if (gpio_is_valid(ptn_bridge->gpio_rst_n)) { |
| 294 | /* |
| 295 | * Request the reset pin low to avoid the bridge being |
| 296 | * initialized prematurely |
| 297 | */ |
| 298 | ret = gpio_request_one(ptn_bridge->gpio_rst_n, |
| 299 | GPIOF_OUT_INIT_LOW, "PTN3460_RST_N"); |
| 300 | if (ret) { |
| 301 | DRM_ERROR("Request reset-gpio failed (%d)\n", ret); |
| 302 | gpio_free(ptn_bridge->gpio_pd_n); |
| 303 | return ret; |
| 304 | } |
| 305 | } |
| 306 | |
| 307 | ret = of_property_read_u32(node, "edid-emulation", |
| 308 | &ptn_bridge->edid_emulation); |
| 309 | if (ret) { |
| 310 | DRM_ERROR("Can't read edid emulation value\n"); |
| 311 | goto err; |
| 312 | } |
| 313 | |
| 314 | ret = drm_bridge_init(dev, bridge, &ptn3460_bridge_funcs); |
| 315 | if (ret) { |
| 316 | DRM_ERROR("Failed to initialize bridge with drm\n"); |
| 317 | goto err; |
| 318 | } |
| 319 | |
| 320 | bridge->driver_private = ptn_bridge; |
| 321 | encoder->bridge = bridge; |
| 322 | |
| 323 | ret = drm_connector_init(dev, &ptn_bridge->connector, |
| 324 | &ptn3460_connector_funcs, DRM_MODE_CONNECTOR_LVDS); |
| 325 | if (ret) { |
| 326 | DRM_ERROR("Failed to initialize connector with drm\n"); |
| 327 | goto err; |
| 328 | } |
| 329 | drm_connector_helper_add(&ptn_bridge->connector, |
| 330 | &ptn3460_connector_helper_funcs); |
| 331 | drm_sysfs_connector_add(&ptn_bridge->connector); |
| 332 | drm_mode_connector_attach_encoder(&ptn_bridge->connector, encoder); |
| 333 | |
| 334 | return 0; |
| 335 | |
| 336 | err: |
| 337 | if (gpio_is_valid(ptn_bridge->gpio_pd_n)) |
| 338 | gpio_free(ptn_bridge->gpio_pd_n); |
| 339 | if (gpio_is_valid(ptn_bridge->gpio_rst_n)) |
| 340 | gpio_free(ptn_bridge->gpio_rst_n); |
| 341 | return ret; |
| 342 | } |
Inki Dae | 96e112c | 2014-04-04 12:14:51 +0900 | [diff] [blame] | 343 | EXPORT_SYMBOL(ptn3460_init); |