1 // SPDX-License-Identifier: GPL-2.0+ 2 /* 3 * Generic LVDS panel driver 4 * 5 * Copyright (C) 2016 Laurent Pinchart 6 * Copyright (C) 2016 Renesas Electronics Corporation 7 * 8 * Contact: Laurent Pinchart (laurent.pinchart@ideasonboard.com) 9 */ 10 11 #include <linux/backlight.h> 12 #include <linux/gpio/consumer.h> 13 #include <linux/module.h> 14 #include <linux/of_platform.h> 15 #include <linux/platform_device.h> 16 #include <linux/regulator/consumer.h> 17 #include <linux/slab.h> 18 19 #include <video/display_timing.h> 20 #include <video/of_display_timing.h> 21 #include <video/videomode.h> 22 23 #include <drm/drm_crtc.h> 24 #include <drm/drm_panel.h> 25 26 struct panel_lvds { 27 struct drm_panel panel; 28 struct device *dev; 29 30 const char *label; 31 unsigned int width; 32 unsigned int height; 33 struct videomode video_mode; 34 unsigned int bus_format; 35 bool data_mirror; 36 37 struct backlight_device *backlight; 38 struct regulator *supply; 39 40 struct gpio_desc *enable_gpio; 41 struct gpio_desc *reset_gpio; 42 }; 43 44 static inline struct panel_lvds *to_panel_lvds(struct drm_panel *panel) 45 { 46 return container_of(panel, struct panel_lvds, panel); 47 } 48 49 static int panel_lvds_disable(struct drm_panel *panel) 50 { 51 struct panel_lvds *lvds = to_panel_lvds(panel); 52 53 if (lvds->backlight) { 54 lvds->backlight->props.power = FB_BLANK_POWERDOWN; 55 lvds->backlight->props.state |= BL_CORE_FBBLANK; 56 backlight_update_status(lvds->backlight); 57 } 58 59 return 0; 60 } 61 62 static int panel_lvds_unprepare(struct drm_panel *panel) 63 { 64 struct panel_lvds *lvds = to_panel_lvds(panel); 65 66 if (lvds->enable_gpio) 67 gpiod_set_value_cansleep(lvds->enable_gpio, 0); 68 69 if (lvds->supply) 70 regulator_disable(lvds->supply); 71 72 return 0; 73 } 74 75 static int panel_lvds_prepare(struct drm_panel *panel) 76 { 77 struct panel_lvds *lvds = to_panel_lvds(panel); 78 79 if (lvds->supply) { 80 int err; 81 82 err = regulator_enable(lvds->supply); 83 if (err < 0) { 84 dev_err(lvds->dev, "failed to enable supply: %d\n", 85 err); 86 return err; 87 } 88 } 89 90 if (lvds->enable_gpio) 91 gpiod_set_value_cansleep(lvds->enable_gpio, 1); 92 93 return 0; 94 } 95 96 static int panel_lvds_enable(struct drm_panel *panel) 97 { 98 struct panel_lvds *lvds = to_panel_lvds(panel); 99 100 if (lvds->backlight) { 101 lvds->backlight->props.state &= ~BL_CORE_FBBLANK; 102 lvds->backlight->props.power = FB_BLANK_UNBLANK; 103 backlight_update_status(lvds->backlight); 104 } 105 106 return 0; 107 } 108 109 static int panel_lvds_get_modes(struct drm_panel *panel) 110 { 111 struct panel_lvds *lvds = to_panel_lvds(panel); 112 struct drm_connector *connector = lvds->panel.connector; 113 struct drm_display_mode *mode; 114 115 mode = drm_mode_create(lvds->panel.drm); 116 if (!mode) 117 return 0; 118 119 drm_display_mode_from_videomode(&lvds->video_mode, mode); 120 mode->type |= DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED; 121 drm_mode_probed_add(connector, mode); 122 123 connector->display_info.width_mm = lvds->width; 124 connector->display_info.height_mm = lvds->height; 125 drm_display_info_set_bus_formats(&connector->display_info, 126 &lvds->bus_format, 1); 127 connector->display_info.bus_flags = lvds->data_mirror 128 ? DRM_BUS_FLAG_DATA_LSB_TO_MSB 129 : DRM_BUS_FLAG_DATA_MSB_TO_LSB; 130 131 return 1; 132 } 133 134 static const struct drm_panel_funcs panel_lvds_funcs = { 135 .disable = panel_lvds_disable, 136 .unprepare = panel_lvds_unprepare, 137 .prepare = panel_lvds_prepare, 138 .enable = panel_lvds_enable, 139 .get_modes = panel_lvds_get_modes, 140 }; 141 142 static int panel_lvds_parse_dt(struct panel_lvds *lvds) 143 { 144 struct device_node *np = lvds->dev->of_node; 145 struct display_timing timing; 146 const char *mapping; 147 int ret; 148 149 ret = of_get_display_timing(np, "panel-timing", &timing); 150 if (ret < 0) 151 return ret; 152 153 videomode_from_timing(&timing, &lvds->video_mode); 154 155 ret = of_property_read_u32(np, "width-mm", &lvds->width); 156 if (ret < 0) { 157 dev_err(lvds->dev, "%pOF: invalid or missing %s DT property\n", 158 np, "width-mm"); 159 return -ENODEV; 160 } 161 ret = of_property_read_u32(np, "height-mm", &lvds->height); 162 if (ret < 0) { 163 dev_err(lvds->dev, "%pOF: invalid or missing %s DT property\n", 164 np, "height-mm"); 165 return -ENODEV; 166 } 167 168 of_property_read_string(np, "label", &lvds->label); 169 170 ret = of_property_read_string(np, "data-mapping", &mapping); 171 if (ret < 0) { 172 dev_err(lvds->dev, "%pOF: invalid or missing %s DT property\n", 173 np, "data-mapping"); 174 return -ENODEV; 175 } 176 177 if (!strcmp(mapping, "jeida-18")) { 178 lvds->bus_format = MEDIA_BUS_FMT_RGB666_1X7X3_SPWG; 179 } else if (!strcmp(mapping, "jeida-24")) { 180 lvds->bus_format = MEDIA_BUS_FMT_RGB888_1X7X4_JEIDA; 181 } else if (!strcmp(mapping, "vesa-24")) { 182 lvds->bus_format = MEDIA_BUS_FMT_RGB888_1X7X4_SPWG; 183 } else { 184 dev_err(lvds->dev, "%pOF: invalid or missing %s DT property\n", 185 np, "data-mapping"); 186 return -EINVAL; 187 } 188 189 lvds->data_mirror = of_property_read_bool(np, "data-mirror"); 190 191 return 0; 192 } 193 194 static int panel_lvds_probe(struct platform_device *pdev) 195 { 196 struct panel_lvds *lvds; 197 struct device_node *np; 198 int ret; 199 200 lvds = devm_kzalloc(&pdev->dev, sizeof(*lvds), GFP_KERNEL); 201 if (!lvds) 202 return -ENOMEM; 203 204 lvds->dev = &pdev->dev; 205 206 ret = panel_lvds_parse_dt(lvds); 207 if (ret < 0) 208 return ret; 209 210 lvds->supply = devm_regulator_get_optional(lvds->dev, "power"); 211 if (IS_ERR(lvds->supply)) { 212 ret = PTR_ERR(lvds->supply); 213 214 if (ret != -ENODEV) { 215 if (ret != -EPROBE_DEFER) 216 dev_err(lvds->dev, "failed to request regulator: %d\n", 217 ret); 218 return ret; 219 } 220 221 lvds->supply = NULL; 222 } 223 224 /* Get GPIOs and backlight controller. */ 225 lvds->enable_gpio = devm_gpiod_get_optional(lvds->dev, "enable", 226 GPIOD_OUT_LOW); 227 if (IS_ERR(lvds->enable_gpio)) { 228 ret = PTR_ERR(lvds->enable_gpio); 229 dev_err(lvds->dev, "failed to request %s GPIO: %d\n", 230 "enable", ret); 231 return ret; 232 } 233 234 lvds->reset_gpio = devm_gpiod_get_optional(lvds->dev, "reset", 235 GPIOD_OUT_HIGH); 236 if (IS_ERR(lvds->reset_gpio)) { 237 ret = PTR_ERR(lvds->reset_gpio); 238 dev_err(lvds->dev, "failed to request %s GPIO: %d\n", 239 "reset", ret); 240 return ret; 241 } 242 243 np = of_parse_phandle(lvds->dev->of_node, "backlight", 0); 244 if (np) { 245 lvds->backlight = of_find_backlight_by_node(np); 246 of_node_put(np); 247 248 if (!lvds->backlight) 249 return -EPROBE_DEFER; 250 } 251 252 /* 253 * TODO: Handle all power supplies specified in the DT node in a generic 254 * way for panels that don't care about power supply ordering. LVDS 255 * panels that require a specific power sequence will need a dedicated 256 * driver. 257 */ 258 259 /* Register the panel. */ 260 drm_panel_init(&lvds->panel); 261 lvds->panel.dev = lvds->dev; 262 lvds->panel.funcs = &panel_lvds_funcs; 263 264 ret = drm_panel_add(&lvds->panel); 265 if (ret < 0) 266 goto error; 267 268 dev_set_drvdata(lvds->dev, lvds); 269 return 0; 270 271 error: 272 put_device(&lvds->backlight->dev); 273 return ret; 274 } 275 276 static int panel_lvds_remove(struct platform_device *pdev) 277 { 278 struct panel_lvds *lvds = dev_get_drvdata(&pdev->dev); 279 280 drm_panel_remove(&lvds->panel); 281 282 panel_lvds_disable(&lvds->panel); 283 284 if (lvds->backlight) 285 put_device(&lvds->backlight->dev); 286 287 return 0; 288 } 289 290 static const struct of_device_id panel_lvds_of_table[] = { 291 { .compatible = "panel-lvds", }, 292 { /* Sentinel */ }, 293 }; 294 295 MODULE_DEVICE_TABLE(of, panel_lvds_of_table); 296 297 static struct platform_driver panel_lvds_driver = { 298 .probe = panel_lvds_probe, 299 .remove = panel_lvds_remove, 300 .driver = { 301 .name = "panel-lvds", 302 .of_match_table = panel_lvds_of_table, 303 }, 304 }; 305 306 module_platform_driver(panel_lvds_driver); 307 308 MODULE_AUTHOR("Laurent Pinchart <laurent.pinchart@ideasonboard.com>"); 309 MODULE_DESCRIPTION("LVDS Panel Driver"); 310 MODULE_LICENSE("GPL"); 311