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