Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 
0003 #include <linux/backlight.h>
0004 #include <linux/delay.h>
0005 #include <linux/gpio/consumer.h>
0006 #include <linux/module.h>
0007 #include <linux/of.h>
0008 #include <linux/regulator/consumer.h>
0009 
0010 #include <drm/drm_mipi_dsi.h>
0011 #include <drm/drm_modes.h>
0012 #include <drm/drm_panel.h>
0013 
0014 struct tm5p5_nt35596 {
0015     struct drm_panel panel;
0016     struct mipi_dsi_device *dsi;
0017     struct regulator_bulk_data supplies[2];
0018     struct gpio_desc *reset_gpio;
0019     bool prepared;
0020 };
0021 
0022 static inline struct tm5p5_nt35596 *to_tm5p5_nt35596(struct drm_panel *panel)
0023 {
0024     return container_of(panel, struct tm5p5_nt35596, panel);
0025 }
0026 
0027 #define dsi_generic_write_seq(dsi, seq...) do {             \
0028         static const u8 d[] = { seq };              \
0029         int ret;                        \
0030         ret = mipi_dsi_generic_write(dsi, d, ARRAY_SIZE(d));    \
0031         if (ret < 0)                        \
0032             return ret;                 \
0033     } while (0)
0034 
0035 #define dsi_dcs_write_seq(dsi, seq...) do {             \
0036         static const u8 d[] = { seq };              \
0037         int ret;                        \
0038         ret = mipi_dsi_dcs_write_buffer(dsi, d, ARRAY_SIZE(d)); \
0039         if (ret < 0)                        \
0040             return ret;                 \
0041     } while (0)
0042 
0043 static void tm5p5_nt35596_reset(struct tm5p5_nt35596 *ctx)
0044 {
0045     gpiod_set_value_cansleep(ctx->reset_gpio, 1);
0046     usleep_range(1000, 2000);
0047     gpiod_set_value_cansleep(ctx->reset_gpio, 0);
0048     usleep_range(1000, 2000);
0049     gpiod_set_value_cansleep(ctx->reset_gpio, 1);
0050     usleep_range(15000, 16000);
0051 }
0052 
0053 static int tm5p5_nt35596_on(struct tm5p5_nt35596 *ctx)
0054 {
0055     struct mipi_dsi_device *dsi = ctx->dsi;
0056 
0057     dsi_generic_write_seq(dsi, 0xff, 0x05);
0058     dsi_generic_write_seq(dsi, 0xfb, 0x01);
0059     dsi_generic_write_seq(dsi, 0xc5, 0x31);
0060     dsi_generic_write_seq(dsi, 0xff, 0x04);
0061     dsi_generic_write_seq(dsi, 0x01, 0x84);
0062     dsi_generic_write_seq(dsi, 0x05, 0x25);
0063     dsi_generic_write_seq(dsi, 0x06, 0x01);
0064     dsi_generic_write_seq(dsi, 0x07, 0x20);
0065     dsi_generic_write_seq(dsi, 0x08, 0x06);
0066     dsi_generic_write_seq(dsi, 0x09, 0x08);
0067     dsi_generic_write_seq(dsi, 0x0a, 0x10);
0068     dsi_generic_write_seq(dsi, 0x0b, 0x10);
0069     dsi_generic_write_seq(dsi, 0x0c, 0x10);
0070     dsi_generic_write_seq(dsi, 0x0d, 0x14);
0071     dsi_generic_write_seq(dsi, 0x0e, 0x14);
0072     dsi_generic_write_seq(dsi, 0x0f, 0x14);
0073     dsi_generic_write_seq(dsi, 0x10, 0x14);
0074     dsi_generic_write_seq(dsi, 0x11, 0x14);
0075     dsi_generic_write_seq(dsi, 0x12, 0x14);
0076     dsi_generic_write_seq(dsi, 0x17, 0xf3);
0077     dsi_generic_write_seq(dsi, 0x18, 0xc0);
0078     dsi_generic_write_seq(dsi, 0x19, 0xc0);
0079     dsi_generic_write_seq(dsi, 0x1a, 0xc0);
0080     dsi_generic_write_seq(dsi, 0x1b, 0xb3);
0081     dsi_generic_write_seq(dsi, 0x1c, 0xb3);
0082     dsi_generic_write_seq(dsi, 0x1d, 0xb3);
0083     dsi_generic_write_seq(dsi, 0x1e, 0xb3);
0084     dsi_generic_write_seq(dsi, 0x1f, 0xb3);
0085     dsi_generic_write_seq(dsi, 0x20, 0xb3);
0086     dsi_generic_write_seq(dsi, 0xfb, 0x01);
0087     dsi_generic_write_seq(dsi, 0xff, 0x00);
0088     dsi_generic_write_seq(dsi, 0xfb, 0x01);
0089     dsi_generic_write_seq(dsi, 0x35, 0x01);
0090     dsi_generic_write_seq(dsi, 0xd3, 0x06);
0091     dsi_generic_write_seq(dsi, 0xd4, 0x04);
0092     dsi_generic_write_seq(dsi, 0x5e, 0x0d);
0093     dsi_generic_write_seq(dsi, 0x11, 0x00);
0094     msleep(100);
0095     dsi_generic_write_seq(dsi, 0x29, 0x00);
0096     dsi_generic_write_seq(dsi, 0x53, 0x24);
0097 
0098     return 0;
0099 }
0100 
0101 static int tm5p5_nt35596_off(struct tm5p5_nt35596 *ctx)
0102 {
0103     struct mipi_dsi_device *dsi = ctx->dsi;
0104     struct device *dev = &dsi->dev;
0105     int ret;
0106 
0107     ret = mipi_dsi_dcs_set_display_off(dsi);
0108     if (ret < 0) {
0109         dev_err(dev, "Failed to set display off: %d\n", ret);
0110         return ret;
0111     }
0112     msleep(60);
0113 
0114     ret = mipi_dsi_dcs_enter_sleep_mode(dsi);
0115     if (ret < 0) {
0116         dev_err(dev, "Failed to enter sleep mode: %d\n", ret);
0117         return ret;
0118     }
0119 
0120     dsi_dcs_write_seq(dsi, 0x4f, 0x01);
0121 
0122     return 0;
0123 }
0124 
0125 static int tm5p5_nt35596_prepare(struct drm_panel *panel)
0126 {
0127     struct tm5p5_nt35596 *ctx = to_tm5p5_nt35596(panel);
0128     struct device *dev = &ctx->dsi->dev;
0129     int ret;
0130 
0131     if (ctx->prepared)
0132         return 0;
0133 
0134     ret = regulator_bulk_enable(ARRAY_SIZE(ctx->supplies), ctx->supplies);
0135     if (ret < 0) {
0136         dev_err(dev, "Failed to enable regulators: %d\n", ret);
0137         return ret;
0138     }
0139 
0140     tm5p5_nt35596_reset(ctx);
0141 
0142     ret = tm5p5_nt35596_on(ctx);
0143     if (ret < 0) {
0144         dev_err(dev, "Failed to initialize panel: %d\n", ret);
0145         gpiod_set_value_cansleep(ctx->reset_gpio, 0);
0146         regulator_bulk_disable(ARRAY_SIZE(ctx->supplies),
0147                        ctx->supplies);
0148         return ret;
0149     }
0150 
0151     ctx->prepared = true;
0152     return 0;
0153 }
0154 
0155 static int tm5p5_nt35596_unprepare(struct drm_panel *panel)
0156 {
0157     struct tm5p5_nt35596 *ctx = to_tm5p5_nt35596(panel);
0158     struct device *dev = &ctx->dsi->dev;
0159     int ret;
0160 
0161     if (!ctx->prepared)
0162         return 0;
0163 
0164     ret = tm5p5_nt35596_off(ctx);
0165     if (ret < 0)
0166         dev_err(dev, "Failed to un-initialize panel: %d\n", ret);
0167 
0168     gpiod_set_value_cansleep(ctx->reset_gpio, 0);
0169     regulator_bulk_disable(ARRAY_SIZE(ctx->supplies),
0170                    ctx->supplies);
0171 
0172     ctx->prepared = false;
0173     return 0;
0174 }
0175 
0176 static const struct drm_display_mode tm5p5_nt35596_mode = {
0177     .clock = (1080 + 100 + 8 + 16) * (1920 + 4 + 2 + 4) * 60 / 1000,
0178     .hdisplay = 1080,
0179     .hsync_start = 1080 + 100,
0180     .hsync_end = 1080 + 100 + 8,
0181     .htotal = 1080 + 100 + 8 + 16,
0182     .vdisplay = 1920,
0183     .vsync_start = 1920 + 4,
0184     .vsync_end = 1920 + 4 + 2,
0185     .vtotal = 1920 + 4 + 2 + 4,
0186     .width_mm = 68,
0187     .height_mm = 121,
0188 };
0189 
0190 static int tm5p5_nt35596_get_modes(struct drm_panel *panel,
0191                    struct drm_connector *connector)
0192 {
0193     struct drm_display_mode *mode;
0194 
0195     mode = drm_mode_duplicate(connector->dev, &tm5p5_nt35596_mode);
0196     if (!mode)
0197         return -ENOMEM;
0198 
0199     drm_mode_set_name(mode);
0200 
0201     mode->type = DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED;
0202     connector->display_info.width_mm = mode->width_mm;
0203     connector->display_info.height_mm = mode->height_mm;
0204     drm_mode_probed_add(connector, mode);
0205 
0206     return 1;
0207 }
0208 
0209 static const struct drm_panel_funcs tm5p5_nt35596_panel_funcs = {
0210     .prepare = tm5p5_nt35596_prepare,
0211     .unprepare = tm5p5_nt35596_unprepare,
0212     .get_modes = tm5p5_nt35596_get_modes,
0213 };
0214 
0215 static int tm5p5_nt35596_bl_update_status(struct backlight_device *bl)
0216 {
0217     struct mipi_dsi_device *dsi = bl_get_data(bl);
0218     u16 brightness = backlight_get_brightness(bl);
0219     int ret;
0220 
0221     dsi->mode_flags &= ~MIPI_DSI_MODE_LPM;
0222 
0223     ret = mipi_dsi_dcs_set_display_brightness(dsi, brightness);
0224     if (ret < 0)
0225         return ret;
0226 
0227     dsi->mode_flags |= MIPI_DSI_MODE_LPM;
0228 
0229     return 0;
0230 }
0231 
0232 static int tm5p5_nt35596_bl_get_brightness(struct backlight_device *bl)
0233 {
0234     struct mipi_dsi_device *dsi = bl_get_data(bl);
0235     u16 brightness = bl->props.brightness;
0236     int ret;
0237 
0238     dsi->mode_flags &= ~MIPI_DSI_MODE_LPM;
0239 
0240     ret = mipi_dsi_dcs_get_display_brightness(dsi, &brightness);
0241     if (ret < 0)
0242         return ret;
0243 
0244     dsi->mode_flags |= MIPI_DSI_MODE_LPM;
0245 
0246     return brightness & 0xff;
0247 }
0248 
0249 static const struct backlight_ops tm5p5_nt35596_bl_ops = {
0250     .update_status = tm5p5_nt35596_bl_update_status,
0251     .get_brightness = tm5p5_nt35596_bl_get_brightness,
0252 };
0253 
0254 static struct backlight_device *
0255 tm5p5_nt35596_create_backlight(struct mipi_dsi_device *dsi)
0256 {
0257     struct device *dev = &dsi->dev;
0258     const struct backlight_properties props = {
0259         .type = BACKLIGHT_RAW,
0260         .brightness = 255,
0261         .max_brightness = 255,
0262     };
0263 
0264     return devm_backlight_device_register(dev, dev_name(dev), dev, dsi,
0265                           &tm5p5_nt35596_bl_ops, &props);
0266 }
0267 
0268 static int tm5p5_nt35596_probe(struct mipi_dsi_device *dsi)
0269 {
0270     struct device *dev = &dsi->dev;
0271     struct tm5p5_nt35596 *ctx;
0272     int ret;
0273 
0274     ctx = devm_kzalloc(dev, sizeof(*ctx), GFP_KERNEL);
0275     if (!ctx)
0276         return -ENOMEM;
0277 
0278     ctx->supplies[0].supply = "vdd";
0279     ctx->supplies[1].supply = "vddio";
0280     ret = devm_regulator_bulk_get(dev, ARRAY_SIZE(ctx->supplies),
0281                       ctx->supplies);
0282     if (ret < 0) {
0283         dev_err(dev, "Failed to get regulators: %d\n", ret);
0284         return ret;
0285     }
0286 
0287     ctx->reset_gpio = devm_gpiod_get(dev, "reset", GPIOD_OUT_LOW);
0288     if (IS_ERR(ctx->reset_gpio)) {
0289         ret = PTR_ERR(ctx->reset_gpio);
0290         dev_err(dev, "Failed to get reset-gpios: %d\n", ret);
0291         return ret;
0292     }
0293 
0294     ctx->dsi = dsi;
0295     mipi_dsi_set_drvdata(dsi, ctx);
0296 
0297     dsi->lanes = 4;
0298     dsi->format = MIPI_DSI_FMT_RGB888;
0299     dsi->mode_flags = MIPI_DSI_MODE_VIDEO | MIPI_DSI_MODE_VIDEO_BURST |
0300               MIPI_DSI_MODE_VIDEO_HSE | MIPI_DSI_MODE_NO_EOT_PACKET |
0301               MIPI_DSI_CLOCK_NON_CONTINUOUS | MIPI_DSI_MODE_LPM;
0302 
0303     drm_panel_init(&ctx->panel, dev, &tm5p5_nt35596_panel_funcs,
0304                DRM_MODE_CONNECTOR_DSI);
0305 
0306     ctx->panel.backlight = tm5p5_nt35596_create_backlight(dsi);
0307     if (IS_ERR(ctx->panel.backlight)) {
0308         ret = PTR_ERR(ctx->panel.backlight);
0309         dev_err(dev, "Failed to create backlight: %d\n", ret);
0310         return ret;
0311     }
0312 
0313     drm_panel_add(&ctx->panel);
0314 
0315     ret = mipi_dsi_attach(dsi);
0316     if (ret < 0) {
0317         dev_err(dev, "Failed to attach to DSI host: %d\n", ret);
0318         return ret;
0319     }
0320 
0321     return 0;
0322 }
0323 
0324 static int tm5p5_nt35596_remove(struct mipi_dsi_device *dsi)
0325 {
0326     struct tm5p5_nt35596 *ctx = mipi_dsi_get_drvdata(dsi);
0327     int ret;
0328 
0329     ret = mipi_dsi_detach(dsi);
0330     if (ret < 0)
0331         dev_err(&dsi->dev,
0332             "Failed to detach from DSI host: %d\n", ret);
0333 
0334     drm_panel_remove(&ctx->panel);
0335 
0336     return 0;
0337 }
0338 
0339 static const struct of_device_id tm5p5_nt35596_of_match[] = {
0340     { .compatible = "asus,z00t-tm5p5-n35596" },
0341     { /* sentinel */ }
0342 };
0343 MODULE_DEVICE_TABLE(of, tm5p5_nt35596_of_match);
0344 
0345 static struct mipi_dsi_driver tm5p5_nt35596_driver = {
0346     .probe = tm5p5_nt35596_probe,
0347     .remove = tm5p5_nt35596_remove,
0348     .driver = {
0349         .name = "panel-tm5p5-nt35596",
0350         .of_match_table = tm5p5_nt35596_of_match,
0351     },
0352 };
0353 module_mipi_dsi_driver(tm5p5_nt35596_driver);
0354 
0355 MODULE_AUTHOR("Konrad Dybcio <konradybcio@gmail.com>");
0356 MODULE_DESCRIPTION("DRM driver for tm5p5 nt35596 1080p video mode dsi panel");
0357 MODULE_LICENSE("GPL v2");