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 dev_err(lvds->dev, "%pOF: problems parsing panel-timing (%d)\n", 152 np, ret); 153 return ret; 154 } 155 156 videomode_from_timing(&timing, &lvds->video_mode); 157 158 ret = of_property_read_u32(np, "width-mm", &lvds->width); 159 if (ret < 0) { 160 dev_err(lvds->dev, "%pOF: invalid or missing %s DT property\n", 161 np, "width-mm"); 162 return -ENODEV; 163 } 164 ret = of_property_read_u32(np, "height-mm", &lvds->height); 165 if (ret < 0) { 166 dev_err(lvds->dev, "%pOF: invalid or missing %s DT property\n", 167 np, "height-mm"); 168 return -ENODEV; 169 } 170 171 of_property_read_string(np, "label", &lvds->label); 172 173 ret = of_property_read_string(np, "data-mapping", &mapping); 174 if (ret < 0) { 175 dev_err(lvds->dev, "%pOF: invalid or missing %s DT property\n", 176 np, "data-mapping"); 177 return -ENODEV; 178 } 179 180 if (!strcmp(mapping, "jeida-18")) { 181 lvds->bus_format = MEDIA_BUS_FMT_RGB666_1X7X3_SPWG; 182 } else if (!strcmp(mapping, "jeida-24")) { 183 lvds->bus_format = MEDIA_BUS_FMT_RGB888_1X7X4_JEIDA; 184 } else if (!strcmp(mapping, "vesa-24")) { 185 lvds->bus_format = MEDIA_BUS_FMT_RGB888_1X7X4_SPWG; 186 } else { 187 dev_err(lvds->dev, "%pOF: invalid or missing %s DT property\n", 188 np, "data-mapping"); 189 return -EINVAL; 190 } 191 192 lvds->data_mirror = of_property_read_bool(np, "data-mirror"); 193 194 return 0; 195 } 196 197 static int panel_lvds_probe(struct platform_device *pdev) 198 { 199 struct panel_lvds *lvds; 200 struct device_node *np; 201 int ret; 202 203 lvds = devm_kzalloc(&pdev->dev, sizeof(*lvds), GFP_KERNEL); 204 if (!lvds) 205 return -ENOMEM; 206 207 lvds->dev = &pdev->dev; 208 209 ret = panel_lvds_parse_dt(lvds); 210 if (ret < 0) 211 return ret; 212 213 lvds->supply = devm_regulator_get_optional(lvds->dev, "power"); 214 if (IS_ERR(lvds->supply)) { 215 ret = PTR_ERR(lvds->supply); 216 217 if (ret != -ENODEV) { 218 if (ret != -EPROBE_DEFER) 219 dev_err(lvds->dev, "failed to request regulator: %d\n", 220 ret); 221 return ret; 222 } 223 224 lvds->supply = NULL; 225 } 226 227 /* Get GPIOs and backlight controller. */ 228 lvds->enable_gpio = devm_gpiod_get_optional(lvds->dev, "enable", 229 GPIOD_OUT_LOW); 230 if (IS_ERR(lvds->enable_gpio)) { 231 ret = PTR_ERR(lvds->enable_gpio); 232 dev_err(lvds->dev, "failed to request %s GPIO: %d\n", 233 "enable", ret); 234 return ret; 235 } 236 237 lvds->reset_gpio = devm_gpiod_get_optional(lvds->dev, "reset", 238 GPIOD_OUT_HIGH); 239 if (IS_ERR(lvds->reset_gpio)) { 240 ret = PTR_ERR(lvds->reset_gpio); 241 dev_err(lvds->dev, "failed to request %s GPIO: %d\n", 242 "reset", ret); 243 return ret; 244 } 245 246 np = of_parse_phandle(lvds->dev->of_node, "backlight", 0); 247 if (np) { 248 lvds->backlight = of_find_backlight_by_node(np); 249 of_node_put(np); 250 251 if (!lvds->backlight) 252 return -EPROBE_DEFER; 253 } 254 255 /* 256 * TODO: Handle all power supplies specified in the DT node in a generic 257 * way for panels that don't care about power supply ordering. LVDS 258 * panels that require a specific power sequence will need a dedicated 259 * driver. 260 */ 261 262 /* Register the panel. */ 263 drm_panel_init(&lvds->panel); 264 lvds->panel.dev = lvds->dev; 265 lvds->panel.funcs = &panel_lvds_funcs; 266 267 ret = drm_panel_add(&lvds->panel); 268 if (ret < 0) 269 goto error; 270 271 dev_set_drvdata(lvds->dev, lvds); 272 return 0; 273 274 error: 275 put_device(&lvds->backlight->dev); 276 return ret; 277 } 278 279 static int panel_lvds_remove(struct platform_device *pdev) 280 { 281 struct panel_lvds *lvds = dev_get_drvdata(&pdev->dev); 282 283 drm_panel_remove(&lvds->panel); 284 285 panel_lvds_disable(&lvds->panel); 286 287 if (lvds->backlight) 288 put_device(&lvds->backlight->dev); 289 290 return 0; 291 } 292 293 static const struct of_device_id panel_lvds_of_table[] = { 294 { .compatible = "panel-lvds", }, 295 { /* Sentinel */ }, 296 }; 297 298 MODULE_DEVICE_TABLE(of, panel_lvds_of_table); 299 300 static struct platform_driver panel_lvds_driver = { 301 .probe = panel_lvds_probe, 302 .remove = panel_lvds_remove, 303 .driver = { 304 .name = "panel-lvds", 305 .of_match_table = panel_lvds_of_table, 306 }, 307 }; 308 309 module_platform_driver(panel_lvds_driver); 310 311 MODULE_AUTHOR("Laurent Pinchart <laurent.pinchart@ideasonboard.com>"); 312 MODULE_DESCRIPTION("LVDS Panel Driver"); 313 MODULE_LICENSE("GPL"); 314