1 // SPDX-License-Identifier: GPL-2.0-only 2 3 #include <linux/array_size.h> 4 #include <linux/delay.h> 5 #include <linux/err.h> 6 #include <linux/gpio/consumer.h> 7 #include <linux/module.h> 8 #include <linux/property.h> 9 #include <linux/regulator/consumer.h> 10 11 #include <video/mipi_display.h> 12 13 #include <drm/drm_mipi_dsi.h> 14 #include <drm/drm_modes.h> 15 #include <drm/drm_panel.h> 16 17 struct hv101hd1 { 18 struct drm_panel panel; 19 struct mipi_dsi_device *dsi; 20 struct regulator_bulk_data *supplies; 21 }; 22 23 static const struct regulator_bulk_data hv101hd1_supplies[] = { 24 { .supply = "vdd" }, 25 { .supply = "vio" }, 26 }; 27 28 static inline struct hv101hd1 *to_hv101hd1(struct drm_panel *panel) 29 { 30 return container_of(panel, struct hv101hd1, panel); 31 } 32 33 static int hv101hd1_prepare(struct drm_panel *panel) 34 { 35 struct hv101hd1 *hv = to_hv101hd1(panel); 36 struct mipi_dsi_multi_context ctx = { .dsi = hv->dsi }; 37 struct device *dev = &hv->dsi->dev; 38 int ret; 39 40 ret = regulator_bulk_enable(ARRAY_SIZE(hv101hd1_supplies), hv->supplies); 41 if (ret) { 42 dev_err(dev, "error enabling regulators (%d)\n", ret); 43 return ret; 44 } 45 46 mipi_dsi_dcs_exit_sleep_mode_multi(&ctx); 47 mipi_dsi_msleep(&ctx, 20); 48 49 mipi_dsi_dcs_set_display_on_multi(&ctx); 50 mipi_dsi_msleep(&ctx, 20); 51 52 return 0; 53 } 54 55 static int hv101hd1_disable(struct drm_panel *panel) 56 { 57 struct hv101hd1 *hv = to_hv101hd1(panel); 58 struct mipi_dsi_multi_context ctx = { .dsi = hv->dsi }; 59 60 mipi_dsi_dcs_set_display_off_multi(&ctx); 61 mipi_dsi_msleep(&ctx, 120); 62 mipi_dsi_dcs_enter_sleep_mode_multi(&ctx); 63 mipi_dsi_msleep(&ctx, 20); 64 65 return 0; 66 } 67 68 static int hv101hd1_unprepare(struct drm_panel *panel) 69 { 70 struct hv101hd1 *hv = to_hv101hd1(panel); 71 72 return regulator_bulk_disable(ARRAY_SIZE(hv101hd1_supplies), 73 hv->supplies); 74 } 75 76 static const struct drm_display_mode hv101hd1_mode = { 77 .clock = (1366 + 74 + 36 + 24) * (768 + 21 + 7 + 4) * 60 / 1000, 78 .hdisplay = 1366, 79 .hsync_start = 1366 + 74, 80 .hsync_end = 1366 + 74 + 36, 81 .htotal = 1366 + 74 + 36 + 24, 82 .vdisplay = 768, 83 .vsync_start = 768 + 21, 84 .vsync_end = 768 + 21 + 7, 85 .vtotal = 768 + 21 + 7 + 4, 86 .width_mm = 140, 87 .height_mm = 220, 88 }; 89 90 static int hv101hd1_get_modes(struct drm_panel *panel, struct drm_connector *connector) 91 { 92 struct drm_display_mode *mode; 93 94 mode = drm_mode_duplicate(connector->dev, &hv101hd1_mode); 95 if (!mode) 96 return -ENOMEM; 97 98 drm_mode_set_name(mode); 99 100 mode->type = DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED; 101 102 connector->display_info.width_mm = mode->width_mm; 103 connector->display_info.height_mm = mode->height_mm; 104 105 drm_mode_probed_add(connector, mode); 106 107 return 1; 108 } 109 110 static const struct drm_panel_funcs hv101hd1_panel_funcs = { 111 .prepare = hv101hd1_prepare, 112 .disable = hv101hd1_disable, 113 .unprepare = hv101hd1_unprepare, 114 .get_modes = hv101hd1_get_modes, 115 }; 116 117 static int hv101hd1_probe(struct mipi_dsi_device *dsi) 118 { 119 struct device *dev = &dsi->dev; 120 struct hv101hd1 *hv; 121 int ret; 122 123 hv = devm_drm_panel_alloc(dev, struct hv101hd1, panel, 124 &hv101hd1_panel_funcs, 125 DRM_MODE_CONNECTOR_DSI); 126 if (IS_ERR(hv)) 127 return PTR_ERR(hv); 128 129 ret = devm_regulator_bulk_get_const(dev, ARRAY_SIZE(hv101hd1_supplies), 130 hv101hd1_supplies, &hv->supplies); 131 if (ret) 132 return dev_err_probe(dev, ret, "failed to get regulators\n"); 133 134 hv->dsi = dsi; 135 mipi_dsi_set_drvdata(dsi, hv); 136 137 dsi->lanes = 2; 138 dsi->format = MIPI_DSI_FMT_RGB888; 139 dsi->mode_flags = MIPI_DSI_MODE_VIDEO | MIPI_DSI_MODE_LPM; 140 141 ret = drm_panel_of_backlight(&hv->panel); 142 if (ret) 143 return dev_err_probe(dev, ret, "Failed to get backlight\n"); 144 145 drm_panel_add(&hv->panel); 146 147 ret = mipi_dsi_attach(dsi); 148 if (ret) { 149 drm_panel_remove(&hv->panel); 150 return dev_err_probe(dev, ret, "Failed to attach to DSI host\n"); 151 } 152 153 return 0; 154 } 155 156 static void hv101hd1_remove(struct mipi_dsi_device *dsi) 157 { 158 struct hv101hd1 *hv = mipi_dsi_get_drvdata(dsi); 159 int ret; 160 161 ret = mipi_dsi_detach(dsi); 162 if (ret < 0) 163 dev_err(&dsi->dev, 164 "Failed to detach from DSI host: %d\n", ret); 165 166 drm_panel_remove(&hv->panel); 167 } 168 169 static const struct of_device_id hv101hd1_of_match[] = { 170 { .compatible = "hydis,hv101hd1" }, 171 { /* sentinel */ } 172 }; 173 MODULE_DEVICE_TABLE(of, hv101hd1_of_match); 174 175 static struct mipi_dsi_driver hv101hd1_driver = { 176 .driver = { 177 .name = "panel-hv101hd1", 178 .of_match_table = hv101hd1_of_match, 179 }, 180 .probe = hv101hd1_probe, 181 .remove = hv101hd1_remove, 182 }; 183 module_mipi_dsi_driver(hv101hd1_driver); 184 185 MODULE_AUTHOR("Svyatoslav Ryhel <clamor95@gmail.com>"); 186 MODULE_DESCRIPTION("DRM driver for Hydis HV101HD1 panel"); 187 MODULE_LICENSE("GPL"); 188