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/delay.h> 17 #include <linux/gpio.h> 18 #include <linux/gpio/consumer.h> 19 #include <linux/i2c.h> 20 #include <linux/module.h> 21 #include <linux/of.h> 22 #include <linux/of_gpio.h> 23 #include <drm/drm_atomic_helper.h> 24 #include <drm/drm_crtc.h> 25 #include <drm/drm_crtc_helper.h> 26 #include <drm/drm_edid.h> 27 #include <drm/drm_of.h> 28 #include <drm/drm_panel.h> 29 #include <drm/drmP.h> 30 31 #define PTN3460_EDID_ADDR 0x0 32 #define PTN3460_EDID_EMULATION_ADDR 0x84 33 #define PTN3460_EDID_ENABLE_EMULATION 0 34 #define PTN3460_EDID_EMULATION_SELECTION 1 35 #define PTN3460_EDID_SRAM_LOAD_ADDR 0x85 36 37 struct ptn3460_bridge { 38 struct drm_connector connector; 39 struct i2c_client *client; 40 struct drm_bridge bridge; 41 struct edid *edid; 42 struct drm_panel *panel; 43 struct gpio_desc *gpio_pd_n; 44 struct gpio_desc *gpio_rst_n; 45 u32 edid_emulation; 46 bool enabled; 47 }; 48 49 static inline struct ptn3460_bridge * 50 bridge_to_ptn3460(struct drm_bridge *bridge) 51 { 52 return container_of(bridge, struct ptn3460_bridge, bridge); 53 } 54 55 static inline struct ptn3460_bridge * 56 connector_to_ptn3460(struct drm_connector *connector) 57 { 58 return container_of(connector, struct ptn3460_bridge, connector); 59 } 60 61 static int ptn3460_read_bytes(struct ptn3460_bridge *ptn_bridge, char addr, 62 u8 *buf, int len) 63 { 64 int ret; 65 66 ret = i2c_master_send(ptn_bridge->client, &addr, 1); 67 if (ret <= 0) { 68 DRM_ERROR("Failed to send i2c command, ret=%d\n", ret); 69 return ret; 70 } 71 72 ret = i2c_master_recv(ptn_bridge->client, buf, len); 73 if (ret <= 0) { 74 DRM_ERROR("Failed to recv i2c data, ret=%d\n", ret); 75 return ret; 76 } 77 78 return 0; 79 } 80 81 static int ptn3460_write_byte(struct ptn3460_bridge *ptn_bridge, char addr, 82 char val) 83 { 84 int ret; 85 char buf[2]; 86 87 buf[0] = addr; 88 buf[1] = val; 89 90 ret = i2c_master_send(ptn_bridge->client, buf, ARRAY_SIZE(buf)); 91 if (ret <= 0) { 92 DRM_ERROR("Failed to send i2c command, ret=%d\n", ret); 93 return ret; 94 } 95 96 return 0; 97 } 98 99 static int ptn3460_select_edid(struct ptn3460_bridge *ptn_bridge) 100 { 101 int ret; 102 char val; 103 104 /* Load the selected edid into SRAM (accessed at PTN3460_EDID_ADDR) */ 105 ret = ptn3460_write_byte(ptn_bridge, PTN3460_EDID_SRAM_LOAD_ADDR, 106 ptn_bridge->edid_emulation); 107 if (ret) { 108 DRM_ERROR("Failed to transfer EDID to sram, ret=%d\n", ret); 109 return ret; 110 } 111 112 /* Enable EDID emulation and select the desired EDID */ 113 val = 1 << PTN3460_EDID_ENABLE_EMULATION | 114 ptn_bridge->edid_emulation << PTN3460_EDID_EMULATION_SELECTION; 115 116 ret = ptn3460_write_byte(ptn_bridge, PTN3460_EDID_EMULATION_ADDR, val); 117 if (ret) { 118 DRM_ERROR("Failed to write EDID value, ret=%d\n", ret); 119 return ret; 120 } 121 122 return 0; 123 } 124 125 static void ptn3460_pre_enable(struct drm_bridge *bridge) 126 { 127 struct ptn3460_bridge *ptn_bridge = bridge_to_ptn3460(bridge); 128 int ret; 129 130 if (ptn_bridge->enabled) 131 return; 132 133 gpiod_set_value(ptn_bridge->gpio_pd_n, 1); 134 135 gpiod_set_value(ptn_bridge->gpio_rst_n, 0); 136 usleep_range(10, 20); 137 gpiod_set_value(ptn_bridge->gpio_rst_n, 1); 138 139 if (drm_panel_prepare(ptn_bridge->panel)) { 140 DRM_ERROR("failed to prepare panel\n"); 141 return; 142 } 143 144 /* 145 * There's a bug in the PTN chip where it falsely asserts hotplug before 146 * it is fully functional. We're forced to wait for the maximum start up 147 * time specified in the chip's datasheet to make sure we're really up. 148 */ 149 msleep(90); 150 151 ret = ptn3460_select_edid(ptn_bridge); 152 if (ret) 153 DRM_ERROR("Select EDID failed ret=%d\n", ret); 154 155 ptn_bridge->enabled = true; 156 } 157 158 static void ptn3460_enable(struct drm_bridge *bridge) 159 { 160 struct ptn3460_bridge *ptn_bridge = bridge_to_ptn3460(bridge); 161 162 if (drm_panel_enable(ptn_bridge->panel)) { 163 DRM_ERROR("failed to enable panel\n"); 164 return; 165 } 166 } 167 168 static void ptn3460_disable(struct drm_bridge *bridge) 169 { 170 struct ptn3460_bridge *ptn_bridge = bridge_to_ptn3460(bridge); 171 172 if (!ptn_bridge->enabled) 173 return; 174 175 ptn_bridge->enabled = false; 176 177 if (drm_panel_disable(ptn_bridge->panel)) { 178 DRM_ERROR("failed to disable panel\n"); 179 return; 180 } 181 182 gpiod_set_value(ptn_bridge->gpio_rst_n, 1); 183 gpiod_set_value(ptn_bridge->gpio_pd_n, 0); 184 } 185 186 static void ptn3460_post_disable(struct drm_bridge *bridge) 187 { 188 struct ptn3460_bridge *ptn_bridge = bridge_to_ptn3460(bridge); 189 190 if (drm_panel_unprepare(ptn_bridge->panel)) { 191 DRM_ERROR("failed to unprepare panel\n"); 192 return; 193 } 194 } 195 196 static int ptn3460_get_modes(struct drm_connector *connector) 197 { 198 struct ptn3460_bridge *ptn_bridge; 199 u8 *edid; 200 int ret, num_modes = 0; 201 bool power_off; 202 203 ptn_bridge = connector_to_ptn3460(connector); 204 205 if (ptn_bridge->edid) 206 return drm_add_edid_modes(connector, ptn_bridge->edid); 207 208 power_off = !ptn_bridge->enabled; 209 ptn3460_pre_enable(&ptn_bridge->bridge); 210 211 edid = kmalloc(EDID_LENGTH, GFP_KERNEL); 212 if (!edid) { 213 DRM_ERROR("Failed to allocate EDID\n"); 214 return 0; 215 } 216 217 ret = ptn3460_read_bytes(ptn_bridge, PTN3460_EDID_ADDR, edid, 218 EDID_LENGTH); 219 if (ret) { 220 kfree(edid); 221 goto out; 222 } 223 224 ptn_bridge->edid = (struct edid *)edid; 225 drm_mode_connector_update_edid_property(connector, ptn_bridge->edid); 226 227 num_modes = drm_add_edid_modes(connector, ptn_bridge->edid); 228 229 out: 230 if (power_off) 231 ptn3460_disable(&ptn_bridge->bridge); 232 233 return num_modes; 234 } 235 236 static const struct drm_connector_helper_funcs ptn3460_connector_helper_funcs = { 237 .get_modes = ptn3460_get_modes, 238 }; 239 240 static const struct drm_connector_funcs ptn3460_connector_funcs = { 241 .fill_modes = drm_helper_probe_single_connector_modes, 242 .destroy = drm_connector_cleanup, 243 .reset = drm_atomic_helper_connector_reset, 244 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state, 245 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state, 246 }; 247 248 static int ptn3460_bridge_attach(struct drm_bridge *bridge) 249 { 250 struct ptn3460_bridge *ptn_bridge = bridge_to_ptn3460(bridge); 251 int ret; 252 253 if (!bridge->encoder) { 254 DRM_ERROR("Parent encoder object not found"); 255 return -ENODEV; 256 } 257 258 ptn_bridge->connector.polled = DRM_CONNECTOR_POLL_HPD; 259 ret = drm_connector_init(bridge->dev, &ptn_bridge->connector, 260 &ptn3460_connector_funcs, DRM_MODE_CONNECTOR_LVDS); 261 if (ret) { 262 DRM_ERROR("Failed to initialize connector with drm\n"); 263 return ret; 264 } 265 drm_connector_helper_add(&ptn_bridge->connector, 266 &ptn3460_connector_helper_funcs); 267 drm_connector_register(&ptn_bridge->connector); 268 drm_mode_connector_attach_encoder(&ptn_bridge->connector, 269 bridge->encoder); 270 271 if (ptn_bridge->panel) 272 drm_panel_attach(ptn_bridge->panel, &ptn_bridge->connector); 273 274 drm_helper_hpd_irq_event(ptn_bridge->connector.dev); 275 276 return ret; 277 } 278 279 static const struct drm_bridge_funcs ptn3460_bridge_funcs = { 280 .pre_enable = ptn3460_pre_enable, 281 .enable = ptn3460_enable, 282 .disable = ptn3460_disable, 283 .post_disable = ptn3460_post_disable, 284 .attach = ptn3460_bridge_attach, 285 }; 286 287 static int ptn3460_probe(struct i2c_client *client, 288 const struct i2c_device_id *id) 289 { 290 struct device *dev = &client->dev; 291 struct ptn3460_bridge *ptn_bridge; 292 int ret; 293 294 ptn_bridge = devm_kzalloc(dev, sizeof(*ptn_bridge), GFP_KERNEL); 295 if (!ptn_bridge) { 296 return -ENOMEM; 297 } 298 299 ret = drm_of_find_panel_or_bridge(dev->of_node, 0, 0, &ptn_bridge->panel, NULL); 300 if (ret) 301 return ret; 302 303 ptn_bridge->client = client; 304 305 ptn_bridge->gpio_pd_n = devm_gpiod_get(&client->dev, "powerdown", 306 GPIOD_OUT_HIGH); 307 if (IS_ERR(ptn_bridge->gpio_pd_n)) { 308 ret = PTR_ERR(ptn_bridge->gpio_pd_n); 309 dev_err(dev, "cannot get gpio_pd_n %d\n", ret); 310 return ret; 311 } 312 313 /* 314 * Request the reset pin low to avoid the bridge being 315 * initialized prematurely 316 */ 317 ptn_bridge->gpio_rst_n = devm_gpiod_get(&client->dev, "reset", 318 GPIOD_OUT_LOW); 319 if (IS_ERR(ptn_bridge->gpio_rst_n)) { 320 ret = PTR_ERR(ptn_bridge->gpio_rst_n); 321 DRM_ERROR("cannot get gpio_rst_n %d\n", ret); 322 return ret; 323 } 324 325 ret = of_property_read_u32(dev->of_node, "edid-emulation", 326 &ptn_bridge->edid_emulation); 327 if (ret) { 328 dev_err(dev, "Can't read EDID emulation value\n"); 329 return ret; 330 } 331 332 ptn_bridge->bridge.funcs = &ptn3460_bridge_funcs; 333 ptn_bridge->bridge.of_node = dev->of_node; 334 drm_bridge_add(&ptn_bridge->bridge); 335 336 i2c_set_clientdata(client, ptn_bridge); 337 338 return 0; 339 } 340 341 static int ptn3460_remove(struct i2c_client *client) 342 { 343 struct ptn3460_bridge *ptn_bridge = i2c_get_clientdata(client); 344 345 drm_bridge_remove(&ptn_bridge->bridge); 346 347 return 0; 348 } 349 350 static const struct i2c_device_id ptn3460_i2c_table[] = { 351 {"ptn3460", 0}, 352 {}, 353 }; 354 MODULE_DEVICE_TABLE(i2c, ptn3460_i2c_table); 355 356 static const struct of_device_id ptn3460_match[] = { 357 { .compatible = "nxp,ptn3460" }, 358 {}, 359 }; 360 MODULE_DEVICE_TABLE(of, ptn3460_match); 361 362 static struct i2c_driver ptn3460_driver = { 363 .id_table = ptn3460_i2c_table, 364 .probe = ptn3460_probe, 365 .remove = ptn3460_remove, 366 .driver = { 367 .name = "nxp,ptn3460", 368 .of_match_table = ptn3460_match, 369 }, 370 }; 371 module_i2c_driver(ptn3460_driver); 372 373 MODULE_AUTHOR("Sean Paul <seanpaul@chromium.org>"); 374 MODULE_DESCRIPTION("NXP ptn3460 eDP-LVDS converter driver"); 375 MODULE_LICENSE("GPL v2"); 376