1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Copyright (C) 2017 NXP Semiconductors. 4 * Author: Marco Franchi <marco.franchi@nxp.com> 5 * 6 * Based on Panel Simple driver by Thierry Reding <treding@nvidia.com> 7 */ 8 9 #include <linux/backlight.h> 10 #include <linux/delay.h> 11 #include <linux/module.h> 12 #include <linux/of.h> 13 #include <linux/platform_device.h> 14 #include <linux/regulator/consumer.h> 15 16 #include <video/display_timing.h> 17 #include <video/videomode.h> 18 19 #include <drm/drm_crtc.h> 20 #include <drm/drm_device.h> 21 #include <drm/drm_panel.h> 22 23 struct seiko_panel_desc { 24 const struct drm_display_mode *modes; 25 unsigned int num_modes; 26 const struct display_timing *timings; 27 unsigned int num_timings; 28 29 unsigned int bpc; 30 31 /** 32 * @width: width (in millimeters) of the panel's active display area 33 * @height: height (in millimeters) of the panel's active display area 34 */ 35 struct { 36 unsigned int width; 37 unsigned int height; 38 } size; 39 40 u32 bus_format; 41 u32 bus_flags; 42 }; 43 44 struct seiko_panel { 45 struct drm_panel base; 46 bool prepared; 47 bool enabled; 48 const struct seiko_panel_desc *desc; 49 struct backlight_device *backlight; 50 struct regulator *dvdd; 51 struct regulator *avdd; 52 }; 53 54 static inline struct seiko_panel *to_seiko_panel(struct drm_panel *panel) 55 { 56 return container_of(panel, struct seiko_panel, base); 57 } 58 59 static int seiko_panel_get_fixed_modes(struct seiko_panel *panel) 60 { 61 struct drm_connector *connector = panel->base.connector; 62 struct drm_device *drm = panel->base.drm; 63 struct drm_display_mode *mode; 64 unsigned int i, num = 0; 65 66 if (!panel->desc) 67 return 0; 68 69 for (i = 0; i < panel->desc->num_timings; i++) { 70 const struct display_timing *dt = &panel->desc->timings[i]; 71 struct videomode vm; 72 73 videomode_from_timing(dt, &vm); 74 mode = drm_mode_create(drm); 75 if (!mode) { 76 dev_err(drm->dev, "failed to add mode %ux%u\n", 77 dt->hactive.typ, dt->vactive.typ); 78 continue; 79 } 80 81 drm_display_mode_from_videomode(&vm, mode); 82 83 mode->type |= DRM_MODE_TYPE_DRIVER; 84 85 if (panel->desc->num_timings == 1) 86 mode->type |= DRM_MODE_TYPE_PREFERRED; 87 88 drm_mode_probed_add(connector, mode); 89 num++; 90 } 91 92 for (i = 0; i < panel->desc->num_modes; i++) { 93 const struct drm_display_mode *m = &panel->desc->modes[i]; 94 95 mode = drm_mode_duplicate(drm, m); 96 if (!mode) { 97 dev_err(drm->dev, "failed to add mode %ux%u@%u\n", 98 m->hdisplay, m->vdisplay, m->vrefresh); 99 continue; 100 } 101 102 mode->type |= DRM_MODE_TYPE_DRIVER; 103 104 if (panel->desc->num_modes == 1) 105 mode->type |= DRM_MODE_TYPE_PREFERRED; 106 107 drm_mode_set_name(mode); 108 109 drm_mode_probed_add(connector, mode); 110 num++; 111 } 112 113 connector->display_info.bpc = panel->desc->bpc; 114 connector->display_info.width_mm = panel->desc->size.width; 115 connector->display_info.height_mm = panel->desc->size.height; 116 if (panel->desc->bus_format) 117 drm_display_info_set_bus_formats(&connector->display_info, 118 &panel->desc->bus_format, 1); 119 connector->display_info.bus_flags = panel->desc->bus_flags; 120 121 return num; 122 } 123 124 static int seiko_panel_disable(struct drm_panel *panel) 125 { 126 struct seiko_panel *p = to_seiko_panel(panel); 127 128 if (!p->enabled) 129 return 0; 130 131 if (p->backlight) { 132 p->backlight->props.power = FB_BLANK_POWERDOWN; 133 p->backlight->props.state |= BL_CORE_FBBLANK; 134 backlight_update_status(p->backlight); 135 } 136 137 p->enabled = false; 138 139 return 0; 140 } 141 142 static int seiko_panel_unprepare(struct drm_panel *panel) 143 { 144 struct seiko_panel *p = to_seiko_panel(panel); 145 146 if (!p->prepared) 147 return 0; 148 149 regulator_disable(p->avdd); 150 151 /* Add a 100ms delay as per the panel datasheet */ 152 msleep(100); 153 154 regulator_disable(p->dvdd); 155 156 p->prepared = false; 157 158 return 0; 159 } 160 161 static int seiko_panel_prepare(struct drm_panel *panel) 162 { 163 struct seiko_panel *p = to_seiko_panel(panel); 164 int err; 165 166 if (p->prepared) 167 return 0; 168 169 err = regulator_enable(p->dvdd); 170 if (err < 0) { 171 dev_err(panel->dev, "failed to enable dvdd: %d\n", err); 172 return err; 173 } 174 175 /* Add a 100ms delay as per the panel datasheet */ 176 msleep(100); 177 178 err = regulator_enable(p->avdd); 179 if (err < 0) { 180 dev_err(panel->dev, "failed to enable avdd: %d\n", err); 181 goto disable_dvdd; 182 } 183 184 p->prepared = true; 185 186 return 0; 187 188 disable_dvdd: 189 regulator_disable(p->dvdd); 190 return err; 191 } 192 193 static int seiko_panel_enable(struct drm_panel *panel) 194 { 195 struct seiko_panel *p = to_seiko_panel(panel); 196 197 if (p->enabled) 198 return 0; 199 200 if (p->backlight) { 201 p->backlight->props.state &= ~BL_CORE_FBBLANK; 202 p->backlight->props.power = FB_BLANK_UNBLANK; 203 backlight_update_status(p->backlight); 204 } 205 206 p->enabled = true; 207 208 return 0; 209 } 210 211 static int seiko_panel_get_modes(struct drm_panel *panel) 212 { 213 struct seiko_panel *p = to_seiko_panel(panel); 214 215 /* add hard-coded panel modes */ 216 return seiko_panel_get_fixed_modes(p); 217 } 218 219 static int seiko_panel_get_timings(struct drm_panel *panel, 220 unsigned int num_timings, 221 struct display_timing *timings) 222 { 223 struct seiko_panel *p = to_seiko_panel(panel); 224 unsigned int i; 225 226 if (p->desc->num_timings < num_timings) 227 num_timings = p->desc->num_timings; 228 229 if (timings) 230 for (i = 0; i < num_timings; i++) 231 timings[i] = p->desc->timings[i]; 232 233 return p->desc->num_timings; 234 } 235 236 static const struct drm_panel_funcs seiko_panel_funcs = { 237 .disable = seiko_panel_disable, 238 .unprepare = seiko_panel_unprepare, 239 .prepare = seiko_panel_prepare, 240 .enable = seiko_panel_enable, 241 .get_modes = seiko_panel_get_modes, 242 .get_timings = seiko_panel_get_timings, 243 }; 244 245 static int seiko_panel_probe(struct device *dev, 246 const struct seiko_panel_desc *desc) 247 { 248 struct device_node *backlight; 249 struct seiko_panel *panel; 250 int err; 251 252 panel = devm_kzalloc(dev, sizeof(*panel), GFP_KERNEL); 253 if (!panel) 254 return -ENOMEM; 255 256 panel->enabled = false; 257 panel->prepared = false; 258 panel->desc = desc; 259 260 panel->dvdd = devm_regulator_get(dev, "dvdd"); 261 if (IS_ERR(panel->dvdd)) 262 return PTR_ERR(panel->dvdd); 263 264 panel->avdd = devm_regulator_get(dev, "avdd"); 265 if (IS_ERR(panel->avdd)) 266 return PTR_ERR(panel->avdd); 267 268 backlight = of_parse_phandle(dev->of_node, "backlight", 0); 269 if (backlight) { 270 panel->backlight = of_find_backlight_by_node(backlight); 271 of_node_put(backlight); 272 273 if (!panel->backlight) 274 return -EPROBE_DEFER; 275 } 276 277 drm_panel_init(&panel->base); 278 panel->base.dev = dev; 279 panel->base.funcs = &seiko_panel_funcs; 280 281 err = drm_panel_add(&panel->base); 282 if (err < 0) 283 return err; 284 285 dev_set_drvdata(dev, panel); 286 287 return 0; 288 } 289 290 static int seiko_panel_remove(struct platform_device *pdev) 291 { 292 struct seiko_panel *panel = dev_get_drvdata(&pdev->dev); 293 294 drm_panel_remove(&panel->base); 295 296 seiko_panel_disable(&panel->base); 297 298 if (panel->backlight) 299 put_device(&panel->backlight->dev); 300 301 return 0; 302 } 303 304 static void seiko_panel_shutdown(struct platform_device *pdev) 305 { 306 struct seiko_panel *panel = dev_get_drvdata(&pdev->dev); 307 308 seiko_panel_disable(&panel->base); 309 } 310 311 static const struct display_timing seiko_43wvf1g_timing = { 312 .pixelclock = { 33500000, 33500000, 33500000 }, 313 .hactive = { 800, 800, 800 }, 314 .hfront_porch = { 164, 164, 164 }, 315 .hback_porch = { 89, 89, 89 }, 316 .hsync_len = { 10, 10, 10 }, 317 .vactive = { 480, 480, 480 }, 318 .vfront_porch = { 10, 10, 10 }, 319 .vback_porch = { 23, 23, 23 }, 320 .vsync_len = { 10, 10, 10 }, 321 .flags = DISPLAY_FLAGS_DE_LOW, 322 }; 323 324 static const struct seiko_panel_desc seiko_43wvf1g = { 325 .timings = &seiko_43wvf1g_timing, 326 .num_timings = 1, 327 .bpc = 8, 328 .size = { 329 .width = 93, 330 .height = 57, 331 }, 332 .bus_format = MEDIA_BUS_FMT_RGB888_1X24, 333 .bus_flags = DRM_BUS_FLAG_DE_HIGH | DRM_BUS_FLAG_PIXDATA_DRIVE_NEGEDGE, 334 }; 335 336 static const struct of_device_id platform_of_match[] = { 337 { 338 .compatible = "sii,43wvf1g", 339 .data = &seiko_43wvf1g, 340 }, { 341 /* sentinel */ 342 } 343 }; 344 MODULE_DEVICE_TABLE(of, platform_of_match); 345 346 static int seiko_panel_platform_probe(struct platform_device *pdev) 347 { 348 const struct of_device_id *id; 349 350 id = of_match_node(platform_of_match, pdev->dev.of_node); 351 if (!id) 352 return -ENODEV; 353 354 return seiko_panel_probe(&pdev->dev, id->data); 355 } 356 357 static struct platform_driver seiko_panel_platform_driver = { 358 .driver = { 359 .name = "seiko_panel", 360 .of_match_table = platform_of_match, 361 }, 362 .probe = seiko_panel_platform_probe, 363 .remove = seiko_panel_remove, 364 .shutdown = seiko_panel_shutdown, 365 }; 366 module_platform_driver(seiko_panel_platform_driver); 367 368 MODULE_AUTHOR("Marco Franchi <marco.franchi@nxp.com>"); 369 MODULE_DESCRIPTION("Seiko 43WVF1G panel driver"); 370 MODULE_LICENSE("GPL v2"); 371