1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * BOE BF060Y8M-AJ0 5.99" MIPI-DSI OLED Panel on SW43404 DriverIC 4 * 5 * Copyright (c) 2020 AngeloGioacchino Del Regno 6 * <angelogioacchino.delregno@somainline.org> 7 */ 8 9 #include <linux/backlight.h> 10 #include <linux/delay.h> 11 #include <linux/gpio/consumer.h> 12 #include <linux/module.h> 13 #include <linux/of.h> 14 #include <linux/regulator/consumer.h> 15 #include <video/mipi_display.h> 16 #include <drm/drm_mipi_dsi.h> 17 #include <drm/drm_modes.h> 18 #include <drm/drm_panel.h> 19 20 #define DCS_ALLOW_HBM_RANGE 0x0c 21 #define DCS_DISALLOW_HBM_RANGE 0x08 22 23 enum boe_bf060y8m_aj0_supplies { 24 BF060Y8M_VREG_VCC, 25 BF060Y8M_VREG_VDDIO, 26 BF060Y8M_VREG_VCI, 27 BF060Y8M_VREG_EL_VDD, 28 BF060Y8M_VREG_EL_VSS, 29 BF060Y8M_VREG_MAX 30 }; 31 32 struct boe_bf060y8m_aj0 { 33 struct drm_panel panel; 34 struct mipi_dsi_device *dsi; 35 struct regulator_bulk_data vregs[BF060Y8M_VREG_MAX]; 36 struct gpio_desc *reset_gpio; 37 }; 38 39 static inline 40 struct boe_bf060y8m_aj0 *to_boe_bf060y8m_aj0(struct drm_panel *panel) 41 { 42 return container_of(panel, struct boe_bf060y8m_aj0, panel); 43 } 44 45 static void boe_bf060y8m_aj0_reset(struct boe_bf060y8m_aj0 *boe) 46 { 47 gpiod_set_value_cansleep(boe->reset_gpio, 0); 48 usleep_range(2000, 3000); 49 gpiod_set_value_cansleep(boe->reset_gpio, 1); 50 usleep_range(15000, 16000); 51 gpiod_set_value_cansleep(boe->reset_gpio, 0); 52 usleep_range(5000, 6000); 53 } 54 55 static int boe_bf060y8m_aj0_on(struct boe_bf060y8m_aj0 *boe) 56 { 57 struct mipi_dsi_device *dsi = boe->dsi; 58 struct mipi_dsi_multi_context dsi_ctx = { .dsi = dsi }; 59 60 mipi_dsi_dcs_write_seq_multi(&dsi_ctx, 0xb0, 0xa5, 0x00); 61 mipi_dsi_dcs_write_seq_multi(&dsi_ctx, 0xb2, 0x00, 0x4c); 62 mipi_dsi_dcs_write_seq_multi(&dsi_ctx, MIPI_DCS_SET_3D_CONTROL, 0x10); 63 mipi_dsi_dcs_write_seq_multi(&dsi_ctx, MIPI_DCS_WRITE_POWER_SAVE, DCS_ALLOW_HBM_RANGE); 64 mipi_dsi_dcs_write_seq_multi(&dsi_ctx, 0xf8, 65 0x00, 0x08, 0x10, 0x00, 0x22, 0x00, 0x00, 0x2d); 66 67 mipi_dsi_dcs_exit_sleep_mode_multi(&dsi_ctx); 68 mipi_dsi_msleep(&dsi_ctx, 30); 69 70 mipi_dsi_dcs_write_seq_multi(&dsi_ctx, 0xb0, 0xa5, 0x00); 71 mipi_dsi_dcs_write_seq_multi(&dsi_ctx, 0xc0, 72 0x08, 0x48, 0x65, 0x33, 0x33, 0x33, 73 0x2a, 0x31, 0x39, 0x20, 0x09); 74 mipi_dsi_dcs_write_seq_multi(&dsi_ctx, 0xc1, 0x00, 0x00, 0x00, 0x1f, 0x1f, 75 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 76 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f); 77 mipi_dsi_dcs_write_seq_multi(&dsi_ctx, 0xe2, 0x20, 0x04, 0x10, 0x12, 0x92, 78 0x4f, 0x8f, 0x44, 0x84, 0x83, 0x83, 0x83, 79 0x5c, 0x5c, 0x5c); 80 mipi_dsi_dcs_write_seq_multi(&dsi_ctx, 0xde, 0x01, 0x2c, 0x00, 0x77, 0x3e); 81 82 mipi_dsi_msleep(&dsi_ctx, 30); 83 84 mipi_dsi_dcs_set_display_on_multi(&dsi_ctx); 85 mipi_dsi_msleep(&dsi_ctx, 50); 86 87 return dsi_ctx.accum_err; 88 } 89 90 static void boe_bf060y8m_aj0_off(struct boe_bf060y8m_aj0 *boe) 91 { 92 struct mipi_dsi_device *dsi = boe->dsi; 93 struct mipi_dsi_multi_context dsi_ctx = { .dsi = dsi }; 94 95 /* OFF commands sent in HS mode */ 96 dsi->mode_flags &= ~MIPI_DSI_MODE_LPM; 97 mipi_dsi_dcs_set_display_off_multi(&dsi_ctx); 98 mipi_dsi_msleep(&dsi_ctx, 20); 99 100 mipi_dsi_dcs_enter_sleep_mode_multi(&dsi_ctx); 101 mipi_dsi_usleep_range(&dsi_ctx, 1000, 2000); 102 dsi->mode_flags |= MIPI_DSI_MODE_LPM; 103 } 104 105 static int boe_bf060y8m_aj0_prepare(struct drm_panel *panel) 106 { 107 struct boe_bf060y8m_aj0 *boe = to_boe_bf060y8m_aj0(panel); 108 int ret; 109 110 /* 111 * Enable EL Driving Voltage first - doing that at the beginning 112 * or at the end of the power sequence doesn't matter, so enable 113 * it here to avoid yet another usleep at the end. 114 */ 115 ret = regulator_enable(boe->vregs[BF060Y8M_VREG_EL_VDD].consumer); 116 if (ret) 117 return ret; 118 ret = regulator_enable(boe->vregs[BF060Y8M_VREG_EL_VSS].consumer); 119 if (ret) 120 goto err_elvss; 121 122 ret = regulator_enable(boe->vregs[BF060Y8M_VREG_VCC].consumer); 123 if (ret) 124 goto err_vcc; 125 usleep_range(1000, 2000); 126 ret = regulator_enable(boe->vregs[BF060Y8M_VREG_VDDIO].consumer); 127 if (ret) 128 goto err_vddio; 129 usleep_range(500, 1000); 130 ret = regulator_enable(boe->vregs[BF060Y8M_VREG_VCI].consumer); 131 if (ret) 132 goto err_vci; 133 usleep_range(2000, 3000); 134 135 boe_bf060y8m_aj0_reset(boe); 136 137 ret = boe_bf060y8m_aj0_on(boe); 138 if (ret < 0) { 139 gpiod_set_value_cansleep(boe->reset_gpio, 1); 140 goto err_on; 141 } 142 143 return 0; 144 145 err_on: 146 regulator_disable(boe->vregs[BF060Y8M_VREG_VCI].consumer); 147 err_vci: 148 regulator_disable(boe->vregs[BF060Y8M_VREG_VDDIO].consumer); 149 err_vddio: 150 regulator_disable(boe->vregs[BF060Y8M_VREG_VCC].consumer); 151 err_vcc: 152 regulator_disable(boe->vregs[BF060Y8M_VREG_EL_VSS].consumer); 153 err_elvss: 154 regulator_disable(boe->vregs[BF060Y8M_VREG_EL_VDD].consumer); 155 return ret; 156 } 157 158 static int boe_bf060y8m_aj0_unprepare(struct drm_panel *panel) 159 { 160 struct boe_bf060y8m_aj0 *boe = to_boe_bf060y8m_aj0(panel); 161 162 boe_bf060y8m_aj0_off(boe); 163 164 gpiod_set_value_cansleep(boe->reset_gpio, 1); 165 regulator_bulk_disable(ARRAY_SIZE(boe->vregs), boe->vregs); 166 167 return 0; 168 } 169 170 static const struct drm_display_mode boe_bf060y8m_aj0_mode = { 171 .clock = 165268, 172 .hdisplay = 1080, 173 .hsync_start = 1080 + 36, 174 .hsync_end = 1080 + 36 + 24, 175 .htotal = 1080 + 36 + 24 + 96, 176 .vdisplay = 2160, 177 .vsync_start = 2160 + 16, 178 .vsync_end = 2160 + 16 + 1, 179 .vtotal = 2160 + 16 + 1 + 15, 180 .width_mm = 68, /* 68.04 mm */ 181 .height_mm = 136, /* 136.08 mm */ 182 }; 183 184 static int boe_bf060y8m_aj0_get_modes(struct drm_panel *panel, 185 struct drm_connector *connector) 186 { 187 struct drm_display_mode *mode; 188 189 mode = drm_mode_duplicate(connector->dev, &boe_bf060y8m_aj0_mode); 190 if (!mode) 191 return -ENOMEM; 192 193 drm_mode_set_name(mode); 194 195 mode->type = DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED; 196 connector->display_info.width_mm = mode->width_mm; 197 connector->display_info.height_mm = mode->height_mm; 198 drm_mode_probed_add(connector, mode); 199 200 return 1; 201 } 202 203 static const struct drm_panel_funcs boe_bf060y8m_aj0_panel_funcs = { 204 .prepare = boe_bf060y8m_aj0_prepare, 205 .unprepare = boe_bf060y8m_aj0_unprepare, 206 .get_modes = boe_bf060y8m_aj0_get_modes, 207 }; 208 209 static int boe_bf060y8m_aj0_bl_update_status(struct backlight_device *bl) 210 { 211 struct mipi_dsi_device *dsi = bl_get_data(bl); 212 u16 brightness = backlight_get_brightness(bl); 213 struct mipi_dsi_multi_context dsi_ctx = { .dsi = dsi }; 214 215 mipi_dsi_dcs_set_display_brightness_multi(&dsi_ctx, brightness); 216 217 return dsi_ctx.accum_err; 218 } 219 220 static int boe_bf060y8m_aj0_bl_get_brightness(struct backlight_device *bl) 221 { 222 struct mipi_dsi_device *dsi = bl_get_data(bl); 223 u16 brightness; 224 int ret; 225 226 ret = mipi_dsi_dcs_get_display_brightness(dsi, &brightness); 227 if (ret < 0) 228 return ret; 229 230 return brightness & 0xff; 231 } 232 233 static const struct backlight_ops boe_bf060y8m_aj0_bl_ops = { 234 .update_status = boe_bf060y8m_aj0_bl_update_status, 235 .get_brightness = boe_bf060y8m_aj0_bl_get_brightness, 236 }; 237 238 static struct backlight_device * 239 boe_bf060y8m_aj0_create_backlight(struct mipi_dsi_device *dsi) 240 { 241 struct device *dev = &dsi->dev; 242 const struct backlight_properties props = { 243 .type = BACKLIGHT_RAW, 244 .brightness = 127, 245 .max_brightness = 255, 246 .scale = BACKLIGHT_SCALE_NON_LINEAR, 247 }; 248 249 return devm_backlight_device_register(dev, dev_name(dev), dev, dsi, 250 &boe_bf060y8m_aj0_bl_ops, &props); 251 } 252 253 static int boe_bf060y8m_aj0_init_vregs(struct boe_bf060y8m_aj0 *boe, 254 struct device *dev) 255 { 256 struct regulator *vreg; 257 int ret; 258 259 boe->vregs[BF060Y8M_VREG_VCC].supply = "vcc"; 260 boe->vregs[BF060Y8M_VREG_VDDIO].supply = "vddio"; 261 boe->vregs[BF060Y8M_VREG_VCI].supply = "vci"; 262 boe->vregs[BF060Y8M_VREG_EL_VDD].supply = "elvdd"; 263 boe->vregs[BF060Y8M_VREG_EL_VSS].supply = "elvss"; 264 ret = devm_regulator_bulk_get(dev, ARRAY_SIZE(boe->vregs), 265 boe->vregs); 266 if (ret < 0) { 267 dev_err(dev, "Failed to get regulators: %d\n", ret); 268 return ret; 269 } 270 271 vreg = boe->vregs[BF060Y8M_VREG_VCC].consumer; 272 ret = regulator_is_supported_voltage(vreg, 2700000, 3600000); 273 if (!ret) 274 return ret; 275 276 vreg = boe->vregs[BF060Y8M_VREG_VDDIO].consumer; 277 ret = regulator_is_supported_voltage(vreg, 1620000, 1980000); 278 if (!ret) 279 return ret; 280 281 vreg = boe->vregs[BF060Y8M_VREG_VCI].consumer; 282 ret = regulator_is_supported_voltage(vreg, 2600000, 3600000); 283 if (!ret) 284 return ret; 285 286 vreg = boe->vregs[BF060Y8M_VREG_EL_VDD].consumer; 287 ret = regulator_is_supported_voltage(vreg, 4400000, 4800000); 288 if (!ret) 289 return ret; 290 291 /* ELVSS is negative: -5.00V to -1.40V */ 292 vreg = boe->vregs[BF060Y8M_VREG_EL_VSS].consumer; 293 ret = regulator_is_supported_voltage(vreg, 1400000, 5000000); 294 if (!ret) 295 return ret; 296 297 /* 298 * Set min/max rated current, known only for VCI and VDDIO and, 299 * in case of failure, just go on gracefully, as this step is not 300 * guaranteed to succeed on all regulator HW but do a debug print 301 * to inform the developer during debugging. 302 * In any case, these two supplies are also optional, so they may 303 * be fixed-regulator which, at the time of writing, does not 304 * support fake current limiting. 305 */ 306 vreg = boe->vregs[BF060Y8M_VREG_VDDIO].consumer; 307 ret = regulator_set_current_limit(vreg, 1500, 2500); 308 if (ret) 309 dev_dbg(dev, "Current limit cannot be set on %s: %d\n", 310 boe->vregs[1].supply, ret); 311 312 vreg = boe->vregs[BF060Y8M_VREG_VCI].consumer; 313 ret = regulator_set_current_limit(vreg, 20000, 40000); 314 if (ret) 315 dev_dbg(dev, "Current limit cannot be set on %s: %d\n", 316 boe->vregs[2].supply, ret); 317 318 return 0; 319 } 320 321 static int boe_bf060y8m_aj0_probe(struct mipi_dsi_device *dsi) 322 { 323 struct device *dev = &dsi->dev; 324 struct boe_bf060y8m_aj0 *boe; 325 int ret; 326 327 boe = devm_drm_panel_alloc(dev, struct boe_bf060y8m_aj0, panel, 328 &boe_bf060y8m_aj0_panel_funcs, 329 DRM_MODE_CONNECTOR_DSI); 330 if (IS_ERR(boe)) 331 return PTR_ERR(boe); 332 333 ret = boe_bf060y8m_aj0_init_vregs(boe, dev); 334 if (ret) 335 return dev_err_probe(dev, ret, 336 "Failed to initialize supplies.\n"); 337 338 boe->reset_gpio = devm_gpiod_get_optional(dev, "reset", GPIOD_ASIS); 339 if (IS_ERR(boe->reset_gpio)) 340 return dev_err_probe(dev, PTR_ERR(boe->reset_gpio), 341 "Failed to get reset-gpios\n"); 342 343 boe->dsi = dsi; 344 mipi_dsi_set_drvdata(dsi, boe); 345 346 dsi->lanes = 4; 347 dsi->format = MIPI_DSI_FMT_RGB888; 348 dsi->mode_flags = MIPI_DSI_MODE_VIDEO | MIPI_DSI_MODE_NO_EOT_PACKET | 349 MIPI_DSI_MODE_VIDEO_SYNC_PULSE | 350 MIPI_DSI_CLOCK_NON_CONTINUOUS | 351 MIPI_DSI_MODE_LPM; 352 353 boe->panel.prepare_prev_first = true; 354 355 boe->panel.backlight = boe_bf060y8m_aj0_create_backlight(dsi); 356 if (IS_ERR(boe->panel.backlight)) 357 return dev_err_probe(dev, PTR_ERR(boe->panel.backlight), 358 "Failed to create backlight\n"); 359 360 drm_panel_add(&boe->panel); 361 362 ret = mipi_dsi_attach(dsi); 363 if (ret < 0) { 364 dev_err(dev, "Failed to attach to DSI host: %d\n", ret); 365 return ret; 366 } 367 368 return 0; 369 } 370 371 static void boe_bf060y8m_aj0_remove(struct mipi_dsi_device *dsi) 372 { 373 struct boe_bf060y8m_aj0 *boe = mipi_dsi_get_drvdata(dsi); 374 int ret; 375 376 ret = mipi_dsi_detach(dsi); 377 if (ret < 0) 378 dev_err(&dsi->dev, "Failed to detach from DSI host: %d\n", ret); 379 380 drm_panel_remove(&boe->panel); 381 } 382 383 static const struct of_device_id boe_bf060y8m_aj0_of_match[] = { 384 { .compatible = "boe,bf060y8m-aj0" }, 385 { /* sentinel */ } 386 }; 387 MODULE_DEVICE_TABLE(of, boe_bf060y8m_aj0_of_match); 388 389 static struct mipi_dsi_driver boe_bf060y8m_aj0_driver = { 390 .probe = boe_bf060y8m_aj0_probe, 391 .remove = boe_bf060y8m_aj0_remove, 392 .driver = { 393 .name = "panel-sw43404-boe-fhd-amoled", 394 .of_match_table = boe_bf060y8m_aj0_of_match, 395 }, 396 }; 397 module_mipi_dsi_driver(boe_bf060y8m_aj0_driver); 398 399 MODULE_AUTHOR("AngeloGioacchino Del Regno <angelogioacchino.delregno@somainline.org>"); 400 MODULE_DESCRIPTION("BOE BF060Y8M-AJ0 MIPI-DSI OLED panel"); 401 MODULE_LICENSE("GPL v2"); 402