1 // SPDX-License-Identifier: GPL-2.0-only 2 /* Copyright (c) 2020 Caleb Connolly <caleb@connolly.tech> 3 * Generated with linux-mdss-dsi-panel-driver-generator from vendor device tree: 4 * Copyright (c) 2020, The Linux Foundation. All rights reserved. 5 */ 6 7 #include <linux/delay.h> 8 #include <linux/gpio/consumer.h> 9 #include <linux/module.h> 10 #include <linux/of.h> 11 #include <linux/of_device.h> 12 #include <linux/regulator/consumer.h> 13 #include <linux/backlight.h> 14 15 #include <video/mipi_display.h> 16 17 #include <drm/drm_mipi_dsi.h> 18 #include <drm/drm_modes.h> 19 #include <drm/drm_panel.h> 20 21 struct sofef00_panel { 22 struct drm_panel panel; 23 struct mipi_dsi_device *dsi; 24 struct regulator *supply; 25 struct gpio_desc *reset_gpio; 26 const struct drm_display_mode *mode; 27 bool prepared; 28 }; 29 30 static inline 31 struct sofef00_panel *to_sofef00_panel(struct drm_panel *panel) 32 { 33 return container_of(panel, struct sofef00_panel, panel); 34 } 35 36 static void sofef00_panel_reset(struct sofef00_panel *ctx) 37 { 38 gpiod_set_value_cansleep(ctx->reset_gpio, 0); 39 usleep_range(5000, 6000); 40 gpiod_set_value_cansleep(ctx->reset_gpio, 1); 41 usleep_range(2000, 3000); 42 gpiod_set_value_cansleep(ctx->reset_gpio, 0); 43 usleep_range(12000, 13000); 44 } 45 46 static int sofef00_panel_on(struct sofef00_panel *ctx) 47 { 48 struct mipi_dsi_device *dsi = ctx->dsi; 49 struct device *dev = &dsi->dev; 50 int ret; 51 52 dsi->mode_flags |= MIPI_DSI_MODE_LPM; 53 54 ret = mipi_dsi_dcs_exit_sleep_mode(dsi); 55 if (ret < 0) { 56 dev_err(dev, "Failed to exit sleep mode: %d\n", ret); 57 return ret; 58 } 59 usleep_range(10000, 11000); 60 61 mipi_dsi_dcs_write_seq(dsi, 0xf0, 0x5a, 0x5a); 62 63 ret = mipi_dsi_dcs_set_tear_on(dsi, MIPI_DSI_DCS_TEAR_MODE_VBLANK); 64 if (ret < 0) { 65 dev_err(dev, "Failed to set tear on: %d\n", ret); 66 return ret; 67 } 68 69 mipi_dsi_dcs_write_seq(dsi, 0xf0, 0xa5, 0xa5); 70 mipi_dsi_dcs_write_seq(dsi, 0xf0, 0x5a, 0x5a); 71 mipi_dsi_dcs_write_seq(dsi, 0xb0, 0x07); 72 mipi_dsi_dcs_write_seq(dsi, 0xb6, 0x12); 73 mipi_dsi_dcs_write_seq(dsi, 0xf0, 0xa5, 0xa5); 74 mipi_dsi_dcs_write_seq(dsi, MIPI_DCS_WRITE_CONTROL_DISPLAY, 0x20); 75 mipi_dsi_dcs_write_seq(dsi, MIPI_DCS_WRITE_POWER_SAVE, 0x00); 76 77 ret = mipi_dsi_dcs_set_display_on(dsi); 78 if (ret < 0) { 79 dev_err(dev, "Failed to set display on: %d\n", ret); 80 return ret; 81 } 82 83 return 0; 84 } 85 86 static int sofef00_panel_off(struct sofef00_panel *ctx) 87 { 88 struct mipi_dsi_device *dsi = ctx->dsi; 89 struct device *dev = &dsi->dev; 90 int ret; 91 92 dsi->mode_flags &= ~MIPI_DSI_MODE_LPM; 93 94 ret = mipi_dsi_dcs_set_display_off(dsi); 95 if (ret < 0) { 96 dev_err(dev, "Failed to set display off: %d\n", ret); 97 return ret; 98 } 99 msleep(40); 100 101 ret = mipi_dsi_dcs_enter_sleep_mode(dsi); 102 if (ret < 0) { 103 dev_err(dev, "Failed to enter sleep mode: %d\n", ret); 104 return ret; 105 } 106 msleep(160); 107 108 return 0; 109 } 110 111 static int sofef00_panel_prepare(struct drm_panel *panel) 112 { 113 struct sofef00_panel *ctx = to_sofef00_panel(panel); 114 struct device *dev = &ctx->dsi->dev; 115 int ret; 116 117 if (ctx->prepared) 118 return 0; 119 120 ret = regulator_enable(ctx->supply); 121 if (ret < 0) { 122 dev_err(dev, "Failed to enable regulator: %d\n", ret); 123 return ret; 124 } 125 126 sofef00_panel_reset(ctx); 127 128 ret = sofef00_panel_on(ctx); 129 if (ret < 0) { 130 dev_err(dev, "Failed to initialize panel: %d\n", ret); 131 gpiod_set_value_cansleep(ctx->reset_gpio, 1); 132 return ret; 133 } 134 135 ctx->prepared = true; 136 return 0; 137 } 138 139 static int sofef00_panel_unprepare(struct drm_panel *panel) 140 { 141 struct sofef00_panel *ctx = to_sofef00_panel(panel); 142 struct device *dev = &ctx->dsi->dev; 143 int ret; 144 145 if (!ctx->prepared) 146 return 0; 147 148 ret = sofef00_panel_off(ctx); 149 if (ret < 0) 150 dev_err(dev, "Failed to un-initialize panel: %d\n", ret); 151 152 regulator_disable(ctx->supply); 153 154 ctx->prepared = false; 155 return 0; 156 } 157 158 static const struct drm_display_mode enchilada_panel_mode = { 159 .clock = (1080 + 112 + 16 + 36) * (2280 + 36 + 8 + 12) * 60 / 1000, 160 .hdisplay = 1080, 161 .hsync_start = 1080 + 112, 162 .hsync_end = 1080 + 112 + 16, 163 .htotal = 1080 + 112 + 16 + 36, 164 .vdisplay = 2280, 165 .vsync_start = 2280 + 36, 166 .vsync_end = 2280 + 36 + 8, 167 .vtotal = 2280 + 36 + 8 + 12, 168 .width_mm = 68, 169 .height_mm = 145, 170 }; 171 172 static const struct drm_display_mode fajita_panel_mode = { 173 .clock = (1080 + 72 + 16 + 36) * (2340 + 32 + 4 + 18) * 60 / 1000, 174 .hdisplay = 1080, 175 .hsync_start = 1080 + 72, 176 .hsync_end = 1080 + 72 + 16, 177 .htotal = 1080 + 72 + 16 + 36, 178 .vdisplay = 2340, 179 .vsync_start = 2340 + 32, 180 .vsync_end = 2340 + 32 + 4, 181 .vtotal = 2340 + 32 + 4 + 18, 182 .width_mm = 68, 183 .height_mm = 145, 184 }; 185 186 static int sofef00_panel_get_modes(struct drm_panel *panel, struct drm_connector *connector) 187 { 188 struct drm_display_mode *mode; 189 struct sofef00_panel *ctx = to_sofef00_panel(panel); 190 191 mode = drm_mode_duplicate(connector->dev, ctx->mode); 192 if (!mode) 193 return -ENOMEM; 194 195 drm_mode_set_name(mode); 196 197 mode->type = DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED; 198 connector->display_info.width_mm = mode->width_mm; 199 connector->display_info.height_mm = mode->height_mm; 200 drm_mode_probed_add(connector, mode); 201 202 return 1; 203 } 204 205 static const struct drm_panel_funcs sofef00_panel_panel_funcs = { 206 .prepare = sofef00_panel_prepare, 207 .unprepare = sofef00_panel_unprepare, 208 .get_modes = sofef00_panel_get_modes, 209 }; 210 211 static int sofef00_panel_bl_update_status(struct backlight_device *bl) 212 { 213 struct mipi_dsi_device *dsi = bl_get_data(bl); 214 int err; 215 u16 brightness = (u16)backlight_get_brightness(bl); 216 217 err = mipi_dsi_dcs_set_display_brightness_large(dsi, brightness); 218 if (err < 0) 219 return err; 220 221 return 0; 222 } 223 224 static const struct backlight_ops sofef00_panel_bl_ops = { 225 .update_status = sofef00_panel_bl_update_status, 226 }; 227 228 static struct backlight_device * 229 sofef00_create_backlight(struct mipi_dsi_device *dsi) 230 { 231 struct device *dev = &dsi->dev; 232 const struct backlight_properties props = { 233 .type = BACKLIGHT_PLATFORM, 234 .brightness = 1023, 235 .max_brightness = 1023, 236 }; 237 238 return devm_backlight_device_register(dev, dev_name(dev), dev, dsi, 239 &sofef00_panel_bl_ops, &props); 240 } 241 242 static int sofef00_panel_probe(struct mipi_dsi_device *dsi) 243 { 244 struct device *dev = &dsi->dev; 245 struct sofef00_panel *ctx; 246 int ret; 247 248 ctx = devm_kzalloc(dev, sizeof(*ctx), GFP_KERNEL); 249 if (!ctx) 250 return -ENOMEM; 251 252 ctx->mode = of_device_get_match_data(dev); 253 254 if (!ctx->mode) { 255 dev_err(dev, "Missing device mode\n"); 256 return -ENODEV; 257 } 258 259 ctx->supply = devm_regulator_get(dev, "vddio"); 260 if (IS_ERR(ctx->supply)) 261 return dev_err_probe(dev, PTR_ERR(ctx->supply), 262 "Failed to get vddio regulator\n"); 263 264 ctx->reset_gpio = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH); 265 if (IS_ERR(ctx->reset_gpio)) 266 return dev_err_probe(dev, PTR_ERR(ctx->reset_gpio), 267 "Failed to get reset-gpios\n"); 268 269 ctx->dsi = dsi; 270 mipi_dsi_set_drvdata(dsi, ctx); 271 272 dsi->lanes = 4; 273 dsi->format = MIPI_DSI_FMT_RGB888; 274 275 drm_panel_init(&ctx->panel, dev, &sofef00_panel_panel_funcs, 276 DRM_MODE_CONNECTOR_DSI); 277 278 ctx->panel.backlight = sofef00_create_backlight(dsi); 279 if (IS_ERR(ctx->panel.backlight)) 280 return dev_err_probe(dev, PTR_ERR(ctx->panel.backlight), 281 "Failed to create backlight\n"); 282 283 drm_panel_add(&ctx->panel); 284 285 ret = mipi_dsi_attach(dsi); 286 if (ret < 0) { 287 dev_err(dev, "Failed to attach to DSI host: %d\n", ret); 288 drm_panel_remove(&ctx->panel); 289 return ret; 290 } 291 292 return 0; 293 } 294 295 static void sofef00_panel_remove(struct mipi_dsi_device *dsi) 296 { 297 struct sofef00_panel *ctx = mipi_dsi_get_drvdata(dsi); 298 int ret; 299 300 ret = mipi_dsi_detach(dsi); 301 if (ret < 0) 302 dev_err(&dsi->dev, "Failed to detach from DSI host: %d\n", ret); 303 304 drm_panel_remove(&ctx->panel); 305 } 306 307 static const struct of_device_id sofef00_panel_of_match[] = { 308 { // OnePlus 6 / enchilada 309 .compatible = "samsung,sofef00", 310 .data = &enchilada_panel_mode, 311 }, 312 { // OnePlus 6T / fajita 313 .compatible = "samsung,s6e3fc2x01", 314 .data = &fajita_panel_mode, 315 }, 316 { /* sentinel */ } 317 }; 318 MODULE_DEVICE_TABLE(of, sofef00_panel_of_match); 319 320 static struct mipi_dsi_driver sofef00_panel_driver = { 321 .probe = sofef00_panel_probe, 322 .remove = sofef00_panel_remove, 323 .driver = { 324 .name = "panel-oneplus6", 325 .of_match_table = sofef00_panel_of_match, 326 }, 327 }; 328 329 module_mipi_dsi_driver(sofef00_panel_driver); 330 331 MODULE_AUTHOR("Caleb Connolly <caleb@connolly.tech>"); 332 MODULE_DESCRIPTION("DRM driver for Samsung AMOLED DSI panels found in OnePlus 6/6T phones"); 333 MODULE_LICENSE("GPL v2"); 334