1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Copyright (C) 2015 Red Hat 4 * Copyright (C) 2015 Sony Mobile Communications Inc. 5 * Author: Werner Johansson <werner.johansson@sonymobile.com> 6 * 7 * Based on AUO panel driver by Rob Clark <robdclark@gmail.com> 8 */ 9 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 16 #include <video/mipi_display.h> 17 18 #include <drm/drm_crtc.h> 19 #include <drm/drm_device.h> 20 #include <drm/drm_mipi_dsi.h> 21 #include <drm/drm_panel.h> 22 23 struct sharp_nt_panel { 24 struct drm_panel base; 25 struct mipi_dsi_device *dsi; 26 27 struct regulator *supply; 28 struct gpio_desc *reset_gpio; 29 }; 30 31 static inline struct sharp_nt_panel *to_sharp_nt_panel(struct drm_panel *panel) 32 { 33 return container_of(panel, struct sharp_nt_panel, base); 34 } 35 36 static int sharp_nt_panel_init(struct sharp_nt_panel *sharp_nt) 37 { 38 struct mipi_dsi_device *dsi = sharp_nt->dsi; 39 struct mipi_dsi_multi_context dsi_ctx = { .dsi = dsi }; 40 41 dsi->mode_flags |= MIPI_DSI_MODE_LPM; 42 43 mipi_dsi_dcs_exit_sleep_mode_multi(&dsi_ctx); 44 45 mipi_dsi_msleep(&dsi_ctx, 120); 46 47 /* Novatek two-lane operation */ 48 mipi_dsi_dcs_write_seq_multi(&dsi_ctx, 0xae, 0x03); 49 50 /* Set both MCU and RGB I/F to 24bpp */ 51 mipi_dsi_dcs_set_pixel_format_multi(&dsi_ctx, 52 MIPI_DCS_PIXEL_FMT_24BIT | 53 (MIPI_DCS_PIXEL_FMT_24BIT << 4)); 54 55 return dsi_ctx.accum_err; 56 } 57 58 static int sharp_nt_panel_on(struct sharp_nt_panel *sharp_nt) 59 { 60 struct mipi_dsi_device *dsi = sharp_nt->dsi; 61 struct mipi_dsi_multi_context dsi_ctx = { .dsi = dsi }; 62 63 dsi->mode_flags |= MIPI_DSI_MODE_LPM; 64 65 mipi_dsi_dcs_set_display_on_multi(&dsi_ctx); 66 67 return dsi_ctx.accum_err; 68 } 69 70 static int sharp_nt_panel_off(struct sharp_nt_panel *sharp_nt) 71 { 72 struct mipi_dsi_device *dsi = sharp_nt->dsi; 73 struct mipi_dsi_multi_context dsi_ctx = { .dsi = dsi }; 74 75 dsi->mode_flags &= ~MIPI_DSI_MODE_LPM; 76 77 mipi_dsi_dcs_set_display_off_multi(&dsi_ctx); 78 79 mipi_dsi_dcs_enter_sleep_mode_multi(&dsi_ctx); 80 81 return dsi_ctx.accum_err; 82 } 83 84 static int sharp_nt_panel_unprepare(struct drm_panel *panel) 85 { 86 struct sharp_nt_panel *sharp_nt = to_sharp_nt_panel(panel); 87 int ret; 88 89 ret = sharp_nt_panel_off(sharp_nt); 90 if (ret < 0) { 91 dev_err(panel->dev, "failed to set panel off: %d\n", ret); 92 return ret; 93 } 94 95 regulator_disable(sharp_nt->supply); 96 if (sharp_nt->reset_gpio) 97 gpiod_set_value(sharp_nt->reset_gpio, 0); 98 99 return 0; 100 } 101 102 static int sharp_nt_panel_prepare(struct drm_panel *panel) 103 { 104 struct sharp_nt_panel *sharp_nt = to_sharp_nt_panel(panel); 105 int ret; 106 107 ret = regulator_enable(sharp_nt->supply); 108 if (ret < 0) 109 return ret; 110 111 msleep(20); 112 113 if (sharp_nt->reset_gpio) { 114 gpiod_set_value(sharp_nt->reset_gpio, 1); 115 msleep(1); 116 gpiod_set_value(sharp_nt->reset_gpio, 0); 117 msleep(1); 118 gpiod_set_value(sharp_nt->reset_gpio, 1); 119 msleep(10); 120 } 121 122 ret = sharp_nt_panel_init(sharp_nt); 123 if (ret < 0) { 124 dev_err(panel->dev, "failed to init panel: %d\n", ret); 125 goto poweroff; 126 } 127 128 ret = sharp_nt_panel_on(sharp_nt); 129 if (ret < 0) { 130 dev_err(panel->dev, "failed to set panel on: %d\n", ret); 131 goto poweroff; 132 } 133 134 return 0; 135 136 poweroff: 137 regulator_disable(sharp_nt->supply); 138 if (sharp_nt->reset_gpio) 139 gpiod_set_value(sharp_nt->reset_gpio, 0); 140 return ret; 141 } 142 143 static const struct drm_display_mode default_mode = { 144 .clock = (540 + 48 + 32 + 80) * (960 + 3 + 10 + 15) * 60 / 1000, 145 .hdisplay = 540, 146 .hsync_start = 540 + 48, 147 .hsync_end = 540 + 48 + 32, 148 .htotal = 540 + 48 + 32 + 80, 149 .vdisplay = 960, 150 .vsync_start = 960 + 3, 151 .vsync_end = 960 + 3 + 10, 152 .vtotal = 960 + 3 + 10 + 15, 153 }; 154 155 static int sharp_nt_panel_get_modes(struct drm_panel *panel, 156 struct drm_connector *connector) 157 { 158 struct drm_display_mode *mode; 159 160 mode = drm_mode_duplicate(connector->dev, &default_mode); 161 if (!mode) { 162 dev_err(panel->dev, "failed to add mode %ux%u@%u\n", 163 default_mode.hdisplay, default_mode.vdisplay, 164 drm_mode_vrefresh(&default_mode)); 165 return -ENOMEM; 166 } 167 168 drm_mode_set_name(mode); 169 170 drm_mode_probed_add(connector, mode); 171 172 connector->display_info.width_mm = 54; 173 connector->display_info.height_mm = 95; 174 175 return 1; 176 } 177 178 static const struct drm_panel_funcs sharp_nt_panel_funcs = { 179 .unprepare = sharp_nt_panel_unprepare, 180 .prepare = sharp_nt_panel_prepare, 181 .get_modes = sharp_nt_panel_get_modes, 182 }; 183 184 static int sharp_nt_panel_add(struct sharp_nt_panel *sharp_nt) 185 { 186 struct device *dev = &sharp_nt->dsi->dev; 187 int ret; 188 189 sharp_nt->supply = devm_regulator_get(dev, "avdd"); 190 if (IS_ERR(sharp_nt->supply)) 191 return PTR_ERR(sharp_nt->supply); 192 193 sharp_nt->reset_gpio = devm_gpiod_get(dev, "reset", GPIOD_OUT_LOW); 194 if (IS_ERR(sharp_nt->reset_gpio)) { 195 dev_err(dev, "cannot get reset-gpios %ld\n", 196 PTR_ERR(sharp_nt->reset_gpio)); 197 sharp_nt->reset_gpio = NULL; 198 } else { 199 gpiod_set_value(sharp_nt->reset_gpio, 0); 200 } 201 202 sharp_nt->base.prepare_prev_first = true; 203 204 ret = drm_panel_of_backlight(&sharp_nt->base); 205 if (ret) 206 return ret; 207 208 drm_panel_add(&sharp_nt->base); 209 210 return 0; 211 } 212 213 static void sharp_nt_panel_del(struct sharp_nt_panel *sharp_nt) 214 { 215 if (sharp_nt->base.dev) 216 drm_panel_remove(&sharp_nt->base); 217 } 218 219 static int sharp_nt_panel_probe(struct mipi_dsi_device *dsi) 220 { 221 struct sharp_nt_panel *sharp_nt; 222 int ret; 223 224 dsi->lanes = 2; 225 dsi->format = MIPI_DSI_FMT_RGB888; 226 dsi->mode_flags = MIPI_DSI_MODE_VIDEO | 227 MIPI_DSI_MODE_VIDEO_SYNC_PULSE | 228 MIPI_DSI_MODE_VIDEO_HSE | 229 MIPI_DSI_CLOCK_NON_CONTINUOUS | 230 MIPI_DSI_MODE_NO_EOT_PACKET; 231 232 sharp_nt = devm_drm_panel_alloc(&dsi->dev, __typeof(*sharp_nt), base, 233 &sharp_nt_panel_funcs, 234 DRM_MODE_CONNECTOR_DSI); 235 236 if (IS_ERR(sharp_nt)) 237 return PTR_ERR(sharp_nt); 238 239 mipi_dsi_set_drvdata(dsi, sharp_nt); 240 241 sharp_nt->dsi = dsi; 242 243 ret = sharp_nt_panel_add(sharp_nt); 244 if (ret < 0) 245 return ret; 246 247 ret = mipi_dsi_attach(dsi); 248 if (ret < 0) { 249 sharp_nt_panel_del(sharp_nt); 250 return ret; 251 } 252 253 return 0; 254 } 255 256 static void sharp_nt_panel_remove(struct mipi_dsi_device *dsi) 257 { 258 struct sharp_nt_panel *sharp_nt = mipi_dsi_get_drvdata(dsi); 259 int ret; 260 261 ret = mipi_dsi_detach(dsi); 262 if (ret < 0) 263 dev_err(&dsi->dev, "failed to detach from DSI host: %d\n", ret); 264 265 sharp_nt_panel_del(sharp_nt); 266 } 267 268 static const struct of_device_id sharp_nt_of_match[] = { 269 { .compatible = "sharp,ls043t1le01-qhd", }, 270 { } 271 }; 272 MODULE_DEVICE_TABLE(of, sharp_nt_of_match); 273 274 static struct mipi_dsi_driver sharp_nt_panel_driver = { 275 .driver = { 276 .name = "panel-sharp-ls043t1le01-qhd", 277 .of_match_table = sharp_nt_of_match, 278 }, 279 .probe = sharp_nt_panel_probe, 280 .remove = sharp_nt_panel_remove, 281 }; 282 module_mipi_dsi_driver(sharp_nt_panel_driver); 283 284 MODULE_AUTHOR("Werner Johansson <werner.johansson@sonymobile.com>"); 285 MODULE_DESCRIPTION("Sharp LS043T1LE01 NT35565-based qHD (540x960) video mode panel driver"); 286 MODULE_LICENSE("GPL v2"); 287