1dc55ac3bSJyri Sarha /* 2dc55ac3bSJyri Sarha * Copyright (C) 2016 Texas Instruments 3dc55ac3bSJyri Sarha * Author: Jyri Sarha <jsarha@ti.com> 4dc55ac3bSJyri Sarha * 5dc55ac3bSJyri Sarha * This program is free software; you can redistribute it and/or modify it 6dc55ac3bSJyri Sarha * under the terms of the GNU General Public License version 2 as published by 7dc55ac3bSJyri Sarha * the Free Software Foundation. 8dc55ac3bSJyri Sarha * 9dc55ac3bSJyri Sarha */ 10dc55ac3bSJyri Sarha 11f56c9202SChristopher Spinrath #include <linux/delay.h> 12f56c9202SChristopher Spinrath #include <linux/fwnode.h> 13f56c9202SChristopher Spinrath #include <linux/gpio/consumer.h> 14f56c9202SChristopher Spinrath #include <linux/irq.h> 15dc55ac3bSJyri Sarha #include <linux/module.h> 16dc55ac3bSJyri Sarha #include <linux/of_graph.h> 17dc55ac3bSJyri Sarha #include <linux/platform_device.h> 18dc55ac3bSJyri Sarha #include <linux/i2c.h> 19dc55ac3bSJyri Sarha 20dc55ac3bSJyri Sarha #include <drm/drmP.h> 21dc55ac3bSJyri Sarha #include <drm/drm_atomic_helper.h> 22dc55ac3bSJyri Sarha #include <drm/drm_crtc.h> 23fcd70cd3SDaniel Vetter #include <drm/drm_probe_helper.h> 24dc55ac3bSJyri Sarha 25f56c9202SChristopher Spinrath #define HOTPLUG_DEBOUNCE_MS 1100 26f56c9202SChristopher Spinrath 27dc55ac3bSJyri Sarha struct tfp410 { 28dc55ac3bSJyri Sarha struct drm_bridge bridge; 29dc55ac3bSJyri Sarha struct drm_connector connector; 3060b903c3SLaurent Pinchart unsigned int connector_type; 31dc55ac3bSJyri Sarha 32dc55ac3bSJyri Sarha struct i2c_adapter *ddc; 33f56c9202SChristopher Spinrath struct gpio_desc *hpd; 34f56c9202SChristopher Spinrath struct delayed_work hpd_work; 3538c02db7SLaurent Pinchart struct gpio_desc *powerdown; 36dc55ac3bSJyri Sarha 37*897dae56SLaurent Pinchart struct drm_bridge_timings timings; 38*897dae56SLaurent Pinchart 39dc55ac3bSJyri Sarha struct device *dev; 40dc55ac3bSJyri Sarha }; 41dc55ac3bSJyri Sarha 42dc55ac3bSJyri Sarha static inline struct tfp410 * 43dc55ac3bSJyri Sarha drm_bridge_to_tfp410(struct drm_bridge *bridge) 44dc55ac3bSJyri Sarha { 45dc55ac3bSJyri Sarha return container_of(bridge, struct tfp410, bridge); 46dc55ac3bSJyri Sarha } 47dc55ac3bSJyri Sarha 48dc55ac3bSJyri Sarha static inline struct tfp410 * 49dc55ac3bSJyri Sarha drm_connector_to_tfp410(struct drm_connector *connector) 50dc55ac3bSJyri Sarha { 51dc55ac3bSJyri Sarha return container_of(connector, struct tfp410, connector); 52dc55ac3bSJyri Sarha } 53dc55ac3bSJyri Sarha 54dc55ac3bSJyri Sarha static int tfp410_get_modes(struct drm_connector *connector) 55dc55ac3bSJyri Sarha { 56dc55ac3bSJyri Sarha struct tfp410 *dvi = drm_connector_to_tfp410(connector); 57dc55ac3bSJyri Sarha struct edid *edid; 58dc55ac3bSJyri Sarha int ret; 59dc55ac3bSJyri Sarha 60dc55ac3bSJyri Sarha if (!dvi->ddc) 61dc55ac3bSJyri Sarha goto fallback; 62dc55ac3bSJyri Sarha 63dc55ac3bSJyri Sarha edid = drm_get_edid(connector, dvi->ddc); 64dc55ac3bSJyri Sarha if (!edid) { 65dc55ac3bSJyri Sarha DRM_INFO("EDID read failed. Fallback to standard modes\n"); 66dc55ac3bSJyri Sarha goto fallback; 67dc55ac3bSJyri Sarha } 68dc55ac3bSJyri Sarha 69c555f023SDaniel Vetter drm_connector_update_edid_property(connector, edid); 70dc55ac3bSJyri Sarha 71dc55ac3bSJyri Sarha return drm_add_edid_modes(connector, edid); 72dc55ac3bSJyri Sarha fallback: 73dc55ac3bSJyri Sarha /* No EDID, fallback on the XGA standard modes */ 74dc55ac3bSJyri Sarha ret = drm_add_modes_noedid(connector, 1920, 1200); 75dc55ac3bSJyri Sarha 76dc55ac3bSJyri Sarha /* And prefer a mode pretty much anything can handle */ 77dc55ac3bSJyri Sarha drm_set_preferred_mode(connector, 1024, 768); 78dc55ac3bSJyri Sarha 79dc55ac3bSJyri Sarha return ret; 80dc55ac3bSJyri Sarha } 81dc55ac3bSJyri Sarha 82dc55ac3bSJyri Sarha static const struct drm_connector_helper_funcs tfp410_con_helper_funcs = { 83dc55ac3bSJyri Sarha .get_modes = tfp410_get_modes, 84dc55ac3bSJyri Sarha }; 85dc55ac3bSJyri Sarha 86dc55ac3bSJyri Sarha static enum drm_connector_status 87dc55ac3bSJyri Sarha tfp410_connector_detect(struct drm_connector *connector, bool force) 88dc55ac3bSJyri Sarha { 89dc55ac3bSJyri Sarha struct tfp410 *dvi = drm_connector_to_tfp410(connector); 90dc55ac3bSJyri Sarha 91f56c9202SChristopher Spinrath if (dvi->hpd) { 92f56c9202SChristopher Spinrath if (gpiod_get_value_cansleep(dvi->hpd)) 93f56c9202SChristopher Spinrath return connector_status_connected; 94f56c9202SChristopher Spinrath else 95f56c9202SChristopher Spinrath return connector_status_disconnected; 96f56c9202SChristopher Spinrath } 97f56c9202SChristopher Spinrath 98dc55ac3bSJyri Sarha if (dvi->ddc) { 99dc55ac3bSJyri Sarha if (drm_probe_ddc(dvi->ddc)) 100dc55ac3bSJyri Sarha return connector_status_connected; 101dc55ac3bSJyri Sarha else 102dc55ac3bSJyri Sarha return connector_status_disconnected; 103dc55ac3bSJyri Sarha } 104dc55ac3bSJyri Sarha 105dc55ac3bSJyri Sarha return connector_status_unknown; 106dc55ac3bSJyri Sarha } 107dc55ac3bSJyri Sarha 108dc55ac3bSJyri Sarha static const struct drm_connector_funcs tfp410_con_funcs = { 109dc55ac3bSJyri Sarha .detect = tfp410_connector_detect, 110dc55ac3bSJyri Sarha .fill_modes = drm_helper_probe_single_connector_modes, 111dc55ac3bSJyri Sarha .destroy = drm_connector_cleanup, 112dc55ac3bSJyri Sarha .reset = drm_atomic_helper_connector_reset, 113dc55ac3bSJyri Sarha .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state, 114dc55ac3bSJyri Sarha .atomic_destroy_state = drm_atomic_helper_connector_destroy_state, 115dc55ac3bSJyri Sarha }; 116dc55ac3bSJyri Sarha 117dc55ac3bSJyri Sarha static int tfp410_attach(struct drm_bridge *bridge) 118dc55ac3bSJyri Sarha { 119dc55ac3bSJyri Sarha struct tfp410 *dvi = drm_bridge_to_tfp410(bridge); 120dc55ac3bSJyri Sarha int ret; 121dc55ac3bSJyri Sarha 122dc55ac3bSJyri Sarha if (!bridge->encoder) { 123dc55ac3bSJyri Sarha dev_err(dvi->dev, "Missing encoder\n"); 124dc55ac3bSJyri Sarha return -ENODEV; 125dc55ac3bSJyri Sarha } 126dc55ac3bSJyri Sarha 127f56c9202SChristopher Spinrath if (dvi->hpd) 128f56c9202SChristopher Spinrath dvi->connector.polled = DRM_CONNECTOR_POLL_HPD; 129f56c9202SChristopher Spinrath 130dc55ac3bSJyri Sarha drm_connector_helper_add(&dvi->connector, 131dc55ac3bSJyri Sarha &tfp410_con_helper_funcs); 132dc55ac3bSJyri Sarha ret = drm_connector_init(bridge->dev, &dvi->connector, 13360b903c3SLaurent Pinchart &tfp410_con_funcs, dvi->connector_type); 134dc55ac3bSJyri Sarha if (ret) { 135dc55ac3bSJyri Sarha dev_err(dvi->dev, "drm_connector_init() failed: %d\n", ret); 136dc55ac3bSJyri Sarha return ret; 137dc55ac3bSJyri Sarha } 138dc55ac3bSJyri Sarha 139cde4c44dSDaniel Vetter drm_connector_attach_encoder(&dvi->connector, 140dc55ac3bSJyri Sarha bridge->encoder); 141dc55ac3bSJyri Sarha 142dc55ac3bSJyri Sarha return 0; 143dc55ac3bSJyri Sarha } 144dc55ac3bSJyri Sarha 14538c02db7SLaurent Pinchart static void tfp410_enable(struct drm_bridge *bridge) 14638c02db7SLaurent Pinchart { 14738c02db7SLaurent Pinchart struct tfp410 *dvi = drm_bridge_to_tfp410(bridge); 14838c02db7SLaurent Pinchart 14938c02db7SLaurent Pinchart gpiod_set_value_cansleep(dvi->powerdown, 0); 15038c02db7SLaurent Pinchart } 15138c02db7SLaurent Pinchart 15238c02db7SLaurent Pinchart static void tfp410_disable(struct drm_bridge *bridge) 15338c02db7SLaurent Pinchart { 15438c02db7SLaurent Pinchart struct tfp410 *dvi = drm_bridge_to_tfp410(bridge); 15538c02db7SLaurent Pinchart 15638c02db7SLaurent Pinchart gpiod_set_value_cansleep(dvi->powerdown, 1); 15738c02db7SLaurent Pinchart } 15838c02db7SLaurent Pinchart 159dc55ac3bSJyri Sarha static const struct drm_bridge_funcs tfp410_bridge_funcs = { 160dc55ac3bSJyri Sarha .attach = tfp410_attach, 16138c02db7SLaurent Pinchart .enable = tfp410_enable, 16238c02db7SLaurent Pinchart .disable = tfp410_disable, 163dc55ac3bSJyri Sarha }; 164dc55ac3bSJyri Sarha 165f56c9202SChristopher Spinrath static void tfp410_hpd_work_func(struct work_struct *work) 166f56c9202SChristopher Spinrath { 167f56c9202SChristopher Spinrath struct tfp410 *dvi; 168f56c9202SChristopher Spinrath 169f56c9202SChristopher Spinrath dvi = container_of(work, struct tfp410, hpd_work.work); 170f56c9202SChristopher Spinrath 171f56c9202SChristopher Spinrath if (dvi->bridge.dev) 172f56c9202SChristopher Spinrath drm_helper_hpd_irq_event(dvi->bridge.dev); 173f56c9202SChristopher Spinrath } 174f56c9202SChristopher Spinrath 175f56c9202SChristopher Spinrath static irqreturn_t tfp410_hpd_irq_thread(int irq, void *arg) 176f56c9202SChristopher Spinrath { 177f56c9202SChristopher Spinrath struct tfp410 *dvi = arg; 178f56c9202SChristopher Spinrath 179f56c9202SChristopher Spinrath mod_delayed_work(system_wq, &dvi->hpd_work, 180f56c9202SChristopher Spinrath msecs_to_jiffies(HOTPLUG_DEBOUNCE_MS)); 181f56c9202SChristopher Spinrath 182f56c9202SChristopher Spinrath return IRQ_HANDLED; 183f56c9202SChristopher Spinrath } 184f56c9202SChristopher Spinrath 185*897dae56SLaurent Pinchart static const struct drm_bridge_timings tfp410_default_timings = { 186*897dae56SLaurent Pinchart .input_bus_flags = DRM_BUS_FLAG_PIXDATA_SAMPLE_POSEDGE 187*897dae56SLaurent Pinchart | DRM_BUS_FLAG_DE_HIGH, 188*897dae56SLaurent Pinchart .setup_time_ps = 1200, 189*897dae56SLaurent Pinchart .hold_time_ps = 1300, 190*897dae56SLaurent Pinchart }; 191*897dae56SLaurent Pinchart 192*897dae56SLaurent Pinchart static int tfp410_parse_timings(struct tfp410 *dvi, bool i2c) 193*897dae56SLaurent Pinchart { 194*897dae56SLaurent Pinchart struct drm_bridge_timings *timings = &dvi->timings; 195*897dae56SLaurent Pinchart struct device_node *ep; 196*897dae56SLaurent Pinchart u32 pclk_sample = 0; 197*897dae56SLaurent Pinchart s32 deskew = 0; 198*897dae56SLaurent Pinchart 199*897dae56SLaurent Pinchart /* Start with defaults. */ 200*897dae56SLaurent Pinchart *timings = tfp410_default_timings; 201*897dae56SLaurent Pinchart 202*897dae56SLaurent Pinchart if (i2c) 203*897dae56SLaurent Pinchart /* 204*897dae56SLaurent Pinchart * In I2C mode timings are configured through the I2C interface. 205*897dae56SLaurent Pinchart * As the driver doesn't support I2C configuration yet, we just 206*897dae56SLaurent Pinchart * go with the defaults (BSEL=1, DSEL=1, DKEN=0, EDGE=1). 207*897dae56SLaurent Pinchart */ 208*897dae56SLaurent Pinchart return 0; 209*897dae56SLaurent Pinchart 210*897dae56SLaurent Pinchart /* 211*897dae56SLaurent Pinchart * In non-I2C mode, timings are configured through the BSEL, DSEL, DKEN 212*897dae56SLaurent Pinchart * and EDGE pins. They are specified in DT through endpoint properties 213*897dae56SLaurent Pinchart * and vendor-specific properties. 214*897dae56SLaurent Pinchart */ 215*897dae56SLaurent Pinchart ep = of_graph_get_endpoint_by_regs(dvi->dev->of_node, 0, 0); 216*897dae56SLaurent Pinchart if (!ep) 217*897dae56SLaurent Pinchart return -EINVAL; 218*897dae56SLaurent Pinchart 219*897dae56SLaurent Pinchart /* Get the sampling edge from the endpoint. */ 220*897dae56SLaurent Pinchart of_property_read_u32(ep, "pclk-sample", &pclk_sample); 221*897dae56SLaurent Pinchart of_node_put(ep); 222*897dae56SLaurent Pinchart 223*897dae56SLaurent Pinchart timings->input_bus_flags = DRM_BUS_FLAG_DE_HIGH; 224*897dae56SLaurent Pinchart 225*897dae56SLaurent Pinchart switch (pclk_sample) { 226*897dae56SLaurent Pinchart case 0: 227*897dae56SLaurent Pinchart timings->input_bus_flags |= DRM_BUS_FLAG_PIXDATA_SAMPLE_NEGEDGE 228*897dae56SLaurent Pinchart | DRM_BUS_FLAG_SYNC_SAMPLE_NEGEDGE; 229*897dae56SLaurent Pinchart break; 230*897dae56SLaurent Pinchart case 1: 231*897dae56SLaurent Pinchart timings->input_bus_flags |= DRM_BUS_FLAG_PIXDATA_SAMPLE_POSEDGE 232*897dae56SLaurent Pinchart | DRM_BUS_FLAG_SYNC_SAMPLE_POSEDGE; 233*897dae56SLaurent Pinchart break; 234*897dae56SLaurent Pinchart default: 235*897dae56SLaurent Pinchart return -EINVAL; 236*897dae56SLaurent Pinchart } 237*897dae56SLaurent Pinchart 238*897dae56SLaurent Pinchart /* Get the setup and hold time from vendor-specific properties. */ 239*897dae56SLaurent Pinchart of_property_read_u32(dvi->dev->of_node, "ti,deskew", (u32 *)&deskew); 240*897dae56SLaurent Pinchart if (deskew < -4 || deskew > 3) 241*897dae56SLaurent Pinchart return -EINVAL; 242*897dae56SLaurent Pinchart 243*897dae56SLaurent Pinchart timings->setup_time_ps = min(0, 1200 - 350 * deskew); 244*897dae56SLaurent Pinchart timings->hold_time_ps = min(0, 1300 + 350 * deskew); 245*897dae56SLaurent Pinchart 246*897dae56SLaurent Pinchart return 0; 247*897dae56SLaurent Pinchart } 248*897dae56SLaurent Pinchart 249f56c9202SChristopher Spinrath static int tfp410_get_connector_properties(struct tfp410 *dvi) 250dc55ac3bSJyri Sarha { 25186418f90SRob Herring struct device_node *connector_node, *ddc_phandle; 252dc55ac3bSJyri Sarha int ret = 0; 253dc55ac3bSJyri Sarha 254dc55ac3bSJyri Sarha /* port@1 is the connector node */ 25586418f90SRob Herring connector_node = of_graph_get_remote_node(dvi->dev->of_node, 1, -1); 256dc55ac3bSJyri Sarha if (!connector_node) 25786418f90SRob Herring return -ENODEV; 258dc55ac3bSJyri Sarha 25960b903c3SLaurent Pinchart if (of_device_is_compatible(connector_node, "hdmi-connector")) 26060b903c3SLaurent Pinchart dvi->connector_type = DRM_MODE_CONNECTOR_HDMIA; 26160b903c3SLaurent Pinchart else 26260b903c3SLaurent Pinchart dvi->connector_type = DRM_MODE_CONNECTOR_DVID; 26360b903c3SLaurent Pinchart 264f56c9202SChristopher Spinrath dvi->hpd = fwnode_get_named_gpiod(&connector_node->fwnode, 265f56c9202SChristopher Spinrath "hpd-gpios", 0, GPIOD_IN, "hpd"); 266f56c9202SChristopher Spinrath if (IS_ERR(dvi->hpd)) { 267f56c9202SChristopher Spinrath ret = PTR_ERR(dvi->hpd); 268f56c9202SChristopher Spinrath dvi->hpd = NULL; 269f56c9202SChristopher Spinrath if (ret == -ENOENT) 270f56c9202SChristopher Spinrath ret = 0; 271f56c9202SChristopher Spinrath else 272f56c9202SChristopher Spinrath goto fail; 273f56c9202SChristopher Spinrath } 274f56c9202SChristopher Spinrath 275dc55ac3bSJyri Sarha ddc_phandle = of_parse_phandle(connector_node, "ddc-i2c-bus", 0); 276dc55ac3bSJyri Sarha if (!ddc_phandle) 277dc55ac3bSJyri Sarha goto fail; 278dc55ac3bSJyri Sarha 279dc55ac3bSJyri Sarha dvi->ddc = of_get_i2c_adapter_by_node(ddc_phandle); 280dc55ac3bSJyri Sarha if (dvi->ddc) 281dc55ac3bSJyri Sarha dev_info(dvi->dev, "Connector's ddc i2c bus found\n"); 282dc55ac3bSJyri Sarha else 283dc55ac3bSJyri Sarha ret = -EPROBE_DEFER; 284dc55ac3bSJyri Sarha 285dc55ac3bSJyri Sarha of_node_put(ddc_phandle); 28686418f90SRob Herring 28786418f90SRob Herring fail: 28886418f90SRob Herring of_node_put(connector_node); 289dc55ac3bSJyri Sarha return ret; 290dc55ac3bSJyri Sarha } 291dc55ac3bSJyri Sarha 292*897dae56SLaurent Pinchart static int tfp410_init(struct device *dev, bool i2c) 293dc55ac3bSJyri Sarha { 294dc55ac3bSJyri Sarha struct tfp410 *dvi; 295dc55ac3bSJyri Sarha int ret; 296dc55ac3bSJyri Sarha 297dc55ac3bSJyri Sarha if (!dev->of_node) { 298dc55ac3bSJyri Sarha dev_err(dev, "device-tree data is missing\n"); 299dc55ac3bSJyri Sarha return -ENXIO; 300dc55ac3bSJyri Sarha } 301dc55ac3bSJyri Sarha 302dc55ac3bSJyri Sarha dvi = devm_kzalloc(dev, sizeof(*dvi), GFP_KERNEL); 303dc55ac3bSJyri Sarha if (!dvi) 304dc55ac3bSJyri Sarha return -ENOMEM; 305dc55ac3bSJyri Sarha dev_set_drvdata(dev, dvi); 306dc55ac3bSJyri Sarha 307dc55ac3bSJyri Sarha dvi->bridge.funcs = &tfp410_bridge_funcs; 308dc55ac3bSJyri Sarha dvi->bridge.of_node = dev->of_node; 309*897dae56SLaurent Pinchart dvi->bridge.timings = &dvi->timings; 310dc55ac3bSJyri Sarha dvi->dev = dev; 311dc55ac3bSJyri Sarha 312*897dae56SLaurent Pinchart ret = tfp410_parse_timings(dvi, i2c); 313*897dae56SLaurent Pinchart if (ret) 314*897dae56SLaurent Pinchart goto fail; 315*897dae56SLaurent Pinchart 316f56c9202SChristopher Spinrath ret = tfp410_get_connector_properties(dvi); 317dc55ac3bSJyri Sarha if (ret) 318dc55ac3bSJyri Sarha goto fail; 319dc55ac3bSJyri Sarha 32038c02db7SLaurent Pinchart dvi->powerdown = devm_gpiod_get_optional(dev, "powerdown", 32138c02db7SLaurent Pinchart GPIOD_OUT_HIGH); 32238c02db7SLaurent Pinchart if (IS_ERR(dvi->powerdown)) { 32338c02db7SLaurent Pinchart dev_err(dev, "failed to parse powerdown gpio\n"); 32438c02db7SLaurent Pinchart return PTR_ERR(dvi->powerdown); 32538c02db7SLaurent Pinchart } 32638c02db7SLaurent Pinchart 327f56c9202SChristopher Spinrath if (dvi->hpd) { 328f56c9202SChristopher Spinrath INIT_DELAYED_WORK(&dvi->hpd_work, tfp410_hpd_work_func); 329f56c9202SChristopher Spinrath 330f56c9202SChristopher Spinrath ret = devm_request_threaded_irq(dev, gpiod_to_irq(dvi->hpd), 331f56c9202SChristopher Spinrath NULL, tfp410_hpd_irq_thread, IRQF_TRIGGER_RISING | 332f56c9202SChristopher Spinrath IRQF_TRIGGER_FALLING | IRQF_ONESHOT, 333f56c9202SChristopher Spinrath "hdmi-hpd", dvi); 334f56c9202SChristopher Spinrath if (ret) { 335f56c9202SChristopher Spinrath DRM_ERROR("failed to register hpd interrupt\n"); 336f56c9202SChristopher Spinrath goto fail; 337f56c9202SChristopher Spinrath } 338f56c9202SChristopher Spinrath } 339f56c9202SChristopher Spinrath 340f74c5275SInki Dae drm_bridge_add(&dvi->bridge); 341dc55ac3bSJyri Sarha 342dc55ac3bSJyri Sarha return 0; 343dc55ac3bSJyri Sarha fail: 344dc55ac3bSJyri Sarha i2c_put_adapter(dvi->ddc); 345f56c9202SChristopher Spinrath if (dvi->hpd) 346f56c9202SChristopher Spinrath gpiod_put(dvi->hpd); 347dc55ac3bSJyri Sarha return ret; 348dc55ac3bSJyri Sarha } 349dc55ac3bSJyri Sarha 350dc55ac3bSJyri Sarha static int tfp410_fini(struct device *dev) 351dc55ac3bSJyri Sarha { 352dc55ac3bSJyri Sarha struct tfp410 *dvi = dev_get_drvdata(dev); 353dc55ac3bSJyri Sarha 354f56c9202SChristopher Spinrath cancel_delayed_work_sync(&dvi->hpd_work); 355f56c9202SChristopher Spinrath 356dc55ac3bSJyri Sarha drm_bridge_remove(&dvi->bridge); 357dc55ac3bSJyri Sarha 358dc55ac3bSJyri Sarha if (dvi->ddc) 359dc55ac3bSJyri Sarha i2c_put_adapter(dvi->ddc); 360f56c9202SChristopher Spinrath if (dvi->hpd) 361f56c9202SChristopher Spinrath gpiod_put(dvi->hpd); 362dc55ac3bSJyri Sarha 363dc55ac3bSJyri Sarha return 0; 364dc55ac3bSJyri Sarha } 365dc55ac3bSJyri Sarha 366dc55ac3bSJyri Sarha static int tfp410_probe(struct platform_device *pdev) 367dc55ac3bSJyri Sarha { 368*897dae56SLaurent Pinchart return tfp410_init(&pdev->dev, false); 369dc55ac3bSJyri Sarha } 370dc55ac3bSJyri Sarha 371dc55ac3bSJyri Sarha static int tfp410_remove(struct platform_device *pdev) 372dc55ac3bSJyri Sarha { 373dc55ac3bSJyri Sarha return tfp410_fini(&pdev->dev); 374dc55ac3bSJyri Sarha } 375dc55ac3bSJyri Sarha 376dc55ac3bSJyri Sarha static const struct of_device_id tfp410_match[] = { 377dc55ac3bSJyri Sarha { .compatible = "ti,tfp410" }, 378dc55ac3bSJyri Sarha {}, 379dc55ac3bSJyri Sarha }; 380dc55ac3bSJyri Sarha MODULE_DEVICE_TABLE(of, tfp410_match); 381dc55ac3bSJyri Sarha 3825b47d089SWei Yongjun static struct platform_driver tfp410_platform_driver = { 383dc55ac3bSJyri Sarha .probe = tfp410_probe, 384dc55ac3bSJyri Sarha .remove = tfp410_remove, 385dc55ac3bSJyri Sarha .driver = { 386dc55ac3bSJyri Sarha .name = "tfp410-bridge", 387dc55ac3bSJyri Sarha .of_match_table = tfp410_match, 388dc55ac3bSJyri Sarha }, 389dc55ac3bSJyri Sarha }; 390dc55ac3bSJyri Sarha 391dc55ac3bSJyri Sarha #if IS_ENABLED(CONFIG_I2C) 392dc55ac3bSJyri Sarha /* There is currently no i2c functionality. */ 393dc55ac3bSJyri Sarha static int tfp410_i2c_probe(struct i2c_client *client, 394dc55ac3bSJyri Sarha const struct i2c_device_id *id) 395dc55ac3bSJyri Sarha { 396dc55ac3bSJyri Sarha int reg; 397dc55ac3bSJyri Sarha 398dc55ac3bSJyri Sarha if (!client->dev.of_node || 399dc55ac3bSJyri Sarha of_property_read_u32(client->dev.of_node, "reg", ®)) { 400dc55ac3bSJyri Sarha dev_err(&client->dev, 401dc55ac3bSJyri Sarha "Can't get i2c reg property from device-tree\n"); 402dc55ac3bSJyri Sarha return -ENXIO; 403dc55ac3bSJyri Sarha } 404dc55ac3bSJyri Sarha 405*897dae56SLaurent Pinchart return tfp410_init(&client->dev, true); 406dc55ac3bSJyri Sarha } 407dc55ac3bSJyri Sarha 408dc55ac3bSJyri Sarha static int tfp410_i2c_remove(struct i2c_client *client) 409dc55ac3bSJyri Sarha { 410dc55ac3bSJyri Sarha return tfp410_fini(&client->dev); 411dc55ac3bSJyri Sarha } 412dc55ac3bSJyri Sarha 413dc55ac3bSJyri Sarha static const struct i2c_device_id tfp410_i2c_ids[] = { 414dc55ac3bSJyri Sarha { "tfp410", 0 }, 415dc55ac3bSJyri Sarha { } 416dc55ac3bSJyri Sarha }; 417dc55ac3bSJyri Sarha MODULE_DEVICE_TABLE(i2c, tfp410_i2c_ids); 418dc55ac3bSJyri Sarha 419dc55ac3bSJyri Sarha static struct i2c_driver tfp410_i2c_driver = { 420dc55ac3bSJyri Sarha .driver = { 421dc55ac3bSJyri Sarha .name = "tfp410", 422dc55ac3bSJyri Sarha .of_match_table = of_match_ptr(tfp410_match), 423dc55ac3bSJyri Sarha }, 424dc55ac3bSJyri Sarha .id_table = tfp410_i2c_ids, 425dc55ac3bSJyri Sarha .probe = tfp410_i2c_probe, 426dc55ac3bSJyri Sarha .remove = tfp410_i2c_remove, 427dc55ac3bSJyri Sarha }; 428dc55ac3bSJyri Sarha #endif /* IS_ENABLED(CONFIG_I2C) */ 429dc55ac3bSJyri Sarha 430dc55ac3bSJyri Sarha static struct { 431dc55ac3bSJyri Sarha uint i2c:1; 432dc55ac3bSJyri Sarha uint platform:1; 433dc55ac3bSJyri Sarha } tfp410_registered_driver; 434dc55ac3bSJyri Sarha 435dc55ac3bSJyri Sarha static int __init tfp410_module_init(void) 436dc55ac3bSJyri Sarha { 437dc55ac3bSJyri Sarha int ret; 438dc55ac3bSJyri Sarha 439dc55ac3bSJyri Sarha #if IS_ENABLED(CONFIG_I2C) 440dc55ac3bSJyri Sarha ret = i2c_add_driver(&tfp410_i2c_driver); 441dc55ac3bSJyri Sarha if (ret) 442dc55ac3bSJyri Sarha pr_err("%s: registering i2c driver failed: %d", 443dc55ac3bSJyri Sarha __func__, ret); 444dc55ac3bSJyri Sarha else 445dc55ac3bSJyri Sarha tfp410_registered_driver.i2c = 1; 446dc55ac3bSJyri Sarha #endif 447dc55ac3bSJyri Sarha 448dc55ac3bSJyri Sarha ret = platform_driver_register(&tfp410_platform_driver); 449dc55ac3bSJyri Sarha if (ret) 450dc55ac3bSJyri Sarha pr_err("%s: registering platform driver failed: %d", 451dc55ac3bSJyri Sarha __func__, ret); 452dc55ac3bSJyri Sarha else 453dc55ac3bSJyri Sarha tfp410_registered_driver.platform = 1; 454dc55ac3bSJyri Sarha 455dc55ac3bSJyri Sarha if (tfp410_registered_driver.i2c || 456dc55ac3bSJyri Sarha tfp410_registered_driver.platform) 457dc55ac3bSJyri Sarha return 0; 458dc55ac3bSJyri Sarha 459dc55ac3bSJyri Sarha return ret; 460dc55ac3bSJyri Sarha } 461dc55ac3bSJyri Sarha module_init(tfp410_module_init); 462dc55ac3bSJyri Sarha 463dc55ac3bSJyri Sarha static void __exit tfp410_module_exit(void) 464dc55ac3bSJyri Sarha { 465dc55ac3bSJyri Sarha #if IS_ENABLED(CONFIG_I2C) 466dc55ac3bSJyri Sarha if (tfp410_registered_driver.i2c) 467dc55ac3bSJyri Sarha i2c_del_driver(&tfp410_i2c_driver); 468dc55ac3bSJyri Sarha #endif 469dc55ac3bSJyri Sarha if (tfp410_registered_driver.platform) 470dc55ac3bSJyri Sarha platform_driver_unregister(&tfp410_platform_driver); 471dc55ac3bSJyri Sarha } 472dc55ac3bSJyri Sarha module_exit(tfp410_module_exit); 473dc55ac3bSJyri Sarha 474dc55ac3bSJyri Sarha MODULE_AUTHOR("Jyri Sarha <jsarha@ti.com>"); 475dc55ac3bSJyri Sarha MODULE_DESCRIPTION("TI TFP410 DVI bridge driver"); 476dc55ac3bSJyri Sarha MODULE_LICENSE("GPL"); 477