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