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