1 // SPDX-License-Identifier: GPL-2.0-only 2 /* Copyright (c) 2020 Casey Connolly <casey.connolly@linaro.org> 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/regulator/consumer.h> 12 #include <linux/backlight.h> 13 14 #include <video/mipi_display.h> 15 16 #include <drm/drm_mipi_dsi.h> 17 #include <drm/drm_modes.h> 18 #include <drm/drm_panel.h> 19 #include <drm/drm_probe_helper.h> 20 21 struct sofef00_panel { 22 struct drm_panel panel; 23 struct mipi_dsi_device *dsi; 24 struct regulator_bulk_data *supplies; 25 struct gpio_desc *reset_gpio; 26 }; 27 28 static const struct regulator_bulk_data sofef00_supplies[] = { 29 { .supply = "vddio" }, 30 { .supply = "vci" }, 31 { .supply = "poc" }, 32 }; 33 34 static inline 35 struct sofef00_panel *to_sofef00_panel(struct drm_panel *panel) 36 { 37 return container_of(panel, struct sofef00_panel, panel); 38 } 39 40 #define sofef00_test_key_on_lvl2(ctx) \ 41 mipi_dsi_dcs_write_seq_multi(ctx, 0xf0, 0x5a, 0x5a) 42 #define sofef00_test_key_off_lvl2(ctx) \ 43 mipi_dsi_dcs_write_seq_multi(ctx, 0xf0, 0xa5, 0xa5) 44 45 static void sofef00_panel_reset(struct sofef00_panel *ctx) 46 { 47 gpiod_set_value_cansleep(ctx->reset_gpio, 0); 48 usleep_range(5000, 6000); 49 gpiod_set_value_cansleep(ctx->reset_gpio, 1); 50 usleep_range(2000, 3000); 51 gpiod_set_value_cansleep(ctx->reset_gpio, 0); 52 usleep_range(12000, 13000); 53 } 54 55 static int sofef00_panel_on(struct sofef00_panel *ctx) 56 { 57 struct mipi_dsi_device *dsi = ctx->dsi; 58 struct mipi_dsi_multi_context dsi_ctx = { .dsi = dsi }; 59 60 dsi->mode_flags |= MIPI_DSI_MODE_LPM; 61 62 mipi_dsi_dcs_exit_sleep_mode_multi(&dsi_ctx); 63 mipi_dsi_usleep_range(&dsi_ctx, 10000, 11000); 64 65 sofef00_test_key_on_lvl2(&dsi_ctx); 66 mipi_dsi_dcs_set_tear_on_multi(&dsi_ctx, MIPI_DSI_DCS_TEAR_MODE_VBLANK); 67 sofef00_test_key_off_lvl2(&dsi_ctx); 68 69 sofef00_test_key_on_lvl2(&dsi_ctx); 70 mipi_dsi_dcs_write_seq_multi(&dsi_ctx, 0xb0, 0x07); 71 mipi_dsi_dcs_write_seq_multi(&dsi_ctx, 0xb6, 0x12); 72 sofef00_test_key_off_lvl2(&dsi_ctx); 73 74 mipi_dsi_dcs_write_seq_multi(&dsi_ctx, MIPI_DCS_WRITE_CONTROL_DISPLAY, 0x20); 75 mipi_dsi_dcs_write_seq_multi(&dsi_ctx, MIPI_DCS_WRITE_POWER_SAVE, 0x00); 76 77 return dsi_ctx.accum_err; 78 } 79 80 static int sofef00_enable(struct drm_panel *panel) 81 { 82 struct sofef00_panel *ctx = to_sofef00_panel(panel); 83 struct mipi_dsi_multi_context dsi_ctx = { .dsi = ctx->dsi }; 84 85 mipi_dsi_dcs_set_display_on_multi(&dsi_ctx); 86 87 return dsi_ctx.accum_err; 88 } 89 90 static int sofef00_panel_off(struct sofef00_panel *ctx) 91 { 92 struct mipi_dsi_device *dsi = ctx->dsi; 93 struct mipi_dsi_multi_context dsi_ctx = { .dsi = dsi }; 94 95 mipi_dsi_dcs_set_display_off_multi(&dsi_ctx); 96 mipi_dsi_msleep(&dsi_ctx, 40); 97 98 mipi_dsi_dcs_enter_sleep_mode_multi(&dsi_ctx); 99 mipi_dsi_msleep(&dsi_ctx, 160); 100 101 return dsi_ctx.accum_err; 102 } 103 104 static int sofef00_panel_prepare(struct drm_panel *panel) 105 { 106 struct sofef00_panel *ctx = to_sofef00_panel(panel); 107 int ret; 108 109 ret = regulator_bulk_enable(ARRAY_SIZE(sofef00_supplies), ctx->supplies); 110 if (ret < 0) 111 return ret; 112 113 sofef00_panel_reset(ctx); 114 115 ret = sofef00_panel_on(ctx); 116 if (ret < 0) { 117 gpiod_set_value_cansleep(ctx->reset_gpio, 1); 118 regulator_bulk_disable(ARRAY_SIZE(sofef00_supplies), ctx->supplies); 119 return ret; 120 } 121 122 return 0; 123 } 124 125 static int sofef00_disable(struct drm_panel *panel) 126 { 127 struct sofef00_panel *ctx = to_sofef00_panel(panel); 128 129 sofef00_panel_off(ctx); 130 131 return 0; 132 } 133 134 static int sofef00_panel_unprepare(struct drm_panel *panel) 135 { 136 struct sofef00_panel *ctx = to_sofef00_panel(panel); 137 138 regulator_bulk_disable(ARRAY_SIZE(sofef00_supplies), ctx->supplies); 139 140 return 0; 141 } 142 143 static const struct drm_display_mode ams628nw01_panel_mode = { 144 .clock = (1080 + 112 + 16 + 36) * (2280 + 36 + 8 + 12) * 60 / 1000, 145 146 .hdisplay = 1080, 147 .hsync_start = 1080 + 112, 148 .hsync_end = 1080 + 112 + 16, 149 .htotal = 1080 + 112 + 16 + 36, 150 151 .vdisplay = 2280, 152 .vsync_start = 2280 + 36, 153 .vsync_end = 2280 + 36 + 8, 154 .vtotal = 2280 + 36 + 8 + 12, 155 156 .width_mm = 68, 157 .height_mm = 145, 158 159 .type = DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED, 160 }; 161 162 static int sofef00_panel_get_modes(struct drm_panel *panel, struct drm_connector *connector) 163 { 164 return drm_connector_helper_get_modes_fixed(connector, &ams628nw01_panel_mode); 165 } 166 167 static const struct drm_panel_funcs sofef00_panel_panel_funcs = { 168 .prepare = sofef00_panel_prepare, 169 .enable = sofef00_enable, 170 .disable = sofef00_disable, 171 .unprepare = sofef00_panel_unprepare, 172 .get_modes = sofef00_panel_get_modes, 173 }; 174 175 static int sofef00_panel_bl_update_status(struct backlight_device *bl) 176 { 177 struct mipi_dsi_device *dsi = bl_get_data(bl); 178 int err; 179 u16 brightness = (u16)backlight_get_brightness(bl); 180 181 dsi->mode_flags &= ~MIPI_DSI_MODE_LPM; 182 183 err = mipi_dsi_dcs_set_display_brightness_large(dsi, brightness); 184 if (err < 0) 185 return err; 186 187 dsi->mode_flags |= MIPI_DSI_MODE_LPM; 188 189 return 0; 190 } 191 192 static const struct backlight_ops sofef00_panel_bl_ops = { 193 .update_status = sofef00_panel_bl_update_status, 194 }; 195 196 static struct backlight_device * 197 sofef00_create_backlight(struct mipi_dsi_device *dsi) 198 { 199 struct device *dev = &dsi->dev; 200 const struct backlight_properties props = { 201 .type = BACKLIGHT_PLATFORM, 202 .brightness = 512, 203 .max_brightness = 1023, 204 }; 205 206 return devm_backlight_device_register(dev, dev_name(dev), dev, dsi, 207 &sofef00_panel_bl_ops, &props); 208 } 209 210 static int sofef00_panel_probe(struct mipi_dsi_device *dsi) 211 { 212 struct device *dev = &dsi->dev; 213 struct sofef00_panel *ctx; 214 int ret; 215 216 ctx = devm_drm_panel_alloc(dev, struct sofef00_panel, panel, 217 &sofef00_panel_panel_funcs, 218 DRM_MODE_CONNECTOR_DSI); 219 if (IS_ERR(ctx)) 220 return PTR_ERR(ctx); 221 222 ret = devm_regulator_bulk_get_const(dev, 223 ARRAY_SIZE(sofef00_supplies), 224 sofef00_supplies, 225 &ctx->supplies); 226 if (ret) 227 return dev_err_probe(dev, ret, "Failed to get regulators\n"); 228 229 ctx->reset_gpio = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH); 230 if (IS_ERR(ctx->reset_gpio)) 231 return dev_err_probe(dev, PTR_ERR(ctx->reset_gpio), 232 "Failed to get reset-gpios\n"); 233 234 ctx->dsi = dsi; 235 mipi_dsi_set_drvdata(dsi, ctx); 236 237 dsi->lanes = 4; 238 dsi->format = MIPI_DSI_FMT_RGB888; 239 dsi->mode_flags = MIPI_DSI_MODE_VIDEO_BURST | 240 MIPI_DSI_CLOCK_NON_CONTINUOUS | MIPI_DSI_MODE_LPM; 241 242 ctx->panel.prepare_prev_first = true; 243 244 ctx->panel.backlight = sofef00_create_backlight(dsi); 245 if (IS_ERR(ctx->panel.backlight)) 246 return dev_err_probe(dev, PTR_ERR(ctx->panel.backlight), 247 "Failed to create backlight\n"); 248 249 drm_panel_add(&ctx->panel); 250 251 ret = mipi_dsi_attach(dsi); 252 if (ret < 0) { 253 dev_err(dev, "Failed to attach to DSI host: %d\n", ret); 254 drm_panel_remove(&ctx->panel); 255 return ret; 256 } 257 258 return 0; 259 } 260 261 static void sofef00_panel_remove(struct mipi_dsi_device *dsi) 262 { 263 struct sofef00_panel *ctx = mipi_dsi_get_drvdata(dsi); 264 int ret; 265 266 ret = mipi_dsi_detach(dsi); 267 if (ret < 0) 268 dev_err(&dsi->dev, "Failed to detach from DSI host: %d\n", ret); 269 270 drm_panel_remove(&ctx->panel); 271 } 272 273 static const struct of_device_id sofef00_panel_of_match[] = { 274 { .compatible = "samsung,sofef00" }, /* legacy */ 275 { .compatible = "samsung,sofef00-ams628nw01" }, 276 { /* sentinel */ } 277 }; 278 MODULE_DEVICE_TABLE(of, sofef00_panel_of_match); 279 280 static struct mipi_dsi_driver sofef00_panel_driver = { 281 .probe = sofef00_panel_probe, 282 .remove = sofef00_panel_remove, 283 .driver = { 284 .name = "panel-samsung-sofef00", 285 .of_match_table = sofef00_panel_of_match, 286 }, 287 }; 288 289 module_mipi_dsi_driver(sofef00_panel_driver); 290 291 MODULE_AUTHOR("Casey Connolly <casey.connolly@linaro.org>"); 292 MODULE_DESCRIPTION("DRM driver for Samsung SOFEF00 DDIC"); 293 MODULE_LICENSE("GPL v2"); 294