Kuninori Morimoto | d7cfd25 | 2018-08-06 03:18:22 +0000 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Laurent Pinchart | 7c9dff5 | 2016-11-18 02:58:18 +0200 | [diff] [blame] | 2 | /* |
Baruch Siach | 6abd061d | 2018-01-22 18:01:42 +0200 | [diff] [blame] | 3 | * Generic LVDS panel driver |
Laurent Pinchart | 7c9dff5 | 2016-11-18 02:58:18 +0200 | [diff] [blame] | 4 | * |
| 5 | * Copyright (C) 2016 Laurent Pinchart |
| 6 | * Copyright (C) 2016 Renesas Electronics Corporation |
| 7 | * |
| 8 | * Contact: Laurent Pinchart (laurent.pinchart@ideasonboard.com) |
Laurent Pinchart | 7c9dff5 | 2016-11-18 02:58:18 +0200 | [diff] [blame] | 9 | */ |
| 10 | |
Laurent Pinchart | 7c9dff5 | 2016-11-18 02:58:18 +0200 | [diff] [blame] | 11 | #include <linux/gpio/consumer.h> |
| 12 | #include <linux/module.h> |
| 13 | #include <linux/of_platform.h> |
| 14 | #include <linux/platform_device.h> |
Maxime Ripard | a1c55bc | 2017-12-21 12:02:28 +0100 | [diff] [blame] | 15 | #include <linux/regulator/consumer.h> |
Laurent Pinchart | 7c9dff5 | 2016-11-18 02:58:18 +0200 | [diff] [blame] | 16 | #include <linux/slab.h> |
| 17 | |
Laurent Pinchart | 7c9dff5 | 2016-11-18 02:58:18 +0200 | [diff] [blame] | 18 | #include <video/display_timing.h> |
| 19 | #include <video/of_display_timing.h> |
| 20 | #include <video/videomode.h> |
| 21 | |
Sam Ravnborg | cb23eae | 2019-05-26 20:05:32 +0200 | [diff] [blame] | 22 | #include <drm/drm_crtc.h> |
Marek Vasut | 7c4dd0a | 2021-10-13 00:42:52 +0200 | [diff] [blame] | 23 | #include <drm/drm_of.h> |
Sam Ravnborg | cb23eae | 2019-05-26 20:05:32 +0200 | [diff] [blame] | 24 | #include <drm/drm_panel.h> |
| 25 | |
Laurent Pinchart | 7c9dff5 | 2016-11-18 02:58:18 +0200 | [diff] [blame] | 26 | struct panel_lvds { |
| 27 | struct drm_panel panel; |
| 28 | struct device *dev; |
| 29 | |
| 30 | const char *label; |
| 31 | unsigned int width; |
| 32 | unsigned int height; |
| 33 | struct videomode video_mode; |
| 34 | unsigned int bus_format; |
| 35 | bool data_mirror; |
| 36 | |
Maxime Ripard | a1c55bc | 2017-12-21 12:02:28 +0100 | [diff] [blame] | 37 | struct regulator *supply; |
Laurent Pinchart | 7c9dff5 | 2016-11-18 02:58:18 +0200 | [diff] [blame] | 38 | |
| 39 | struct gpio_desc *enable_gpio; |
| 40 | struct gpio_desc *reset_gpio; |
Dmitry Osipenko | 58310c2e | 2020-08-14 00:56:08 +0300 | [diff] [blame] | 41 | |
| 42 | enum drm_panel_orientation orientation; |
Laurent Pinchart | 7c9dff5 | 2016-11-18 02:58:18 +0200 | [diff] [blame] | 43 | }; |
| 44 | |
| 45 | static inline struct panel_lvds *to_panel_lvds(struct drm_panel *panel) |
| 46 | { |
| 47 | return container_of(panel, struct panel_lvds, panel); |
| 48 | } |
| 49 | |
Laurent Pinchart | 7c9dff5 | 2016-11-18 02:58:18 +0200 | [diff] [blame] | 50 | static int panel_lvds_unprepare(struct drm_panel *panel) |
| 51 | { |
| 52 | struct panel_lvds *lvds = to_panel_lvds(panel); |
| 53 | |
| 54 | if (lvds->enable_gpio) |
| 55 | gpiod_set_value_cansleep(lvds->enable_gpio, 0); |
| 56 | |
Maxime Ripard | a1c55bc | 2017-12-21 12:02:28 +0100 | [diff] [blame] | 57 | if (lvds->supply) |
| 58 | regulator_disable(lvds->supply); |
| 59 | |
Laurent Pinchart | 7c9dff5 | 2016-11-18 02:58:18 +0200 | [diff] [blame] | 60 | return 0; |
| 61 | } |
| 62 | |
| 63 | static int panel_lvds_prepare(struct drm_panel *panel) |
| 64 | { |
| 65 | struct panel_lvds *lvds = to_panel_lvds(panel); |
| 66 | |
Maxime Ripard | a1c55bc | 2017-12-21 12:02:28 +0100 | [diff] [blame] | 67 | if (lvds->supply) { |
| 68 | int err; |
| 69 | |
| 70 | err = regulator_enable(lvds->supply); |
| 71 | if (err < 0) { |
| 72 | dev_err(lvds->dev, "failed to enable supply: %d\n", |
| 73 | err); |
| 74 | return err; |
| 75 | } |
| 76 | } |
| 77 | |
Laurent Pinchart | 7c9dff5 | 2016-11-18 02:58:18 +0200 | [diff] [blame] | 78 | if (lvds->enable_gpio) |
| 79 | gpiod_set_value_cansleep(lvds->enable_gpio, 1); |
| 80 | |
| 81 | return 0; |
| 82 | } |
| 83 | |
Sam Ravnborg | 0ce8ddd | 2019-12-07 15:03:33 +0100 | [diff] [blame] | 84 | static int panel_lvds_get_modes(struct drm_panel *panel, |
| 85 | struct drm_connector *connector) |
Laurent Pinchart | 7c9dff5 | 2016-11-18 02:58:18 +0200 | [diff] [blame] | 86 | { |
| 87 | struct panel_lvds *lvds = to_panel_lvds(panel); |
Laurent Pinchart | 7c9dff5 | 2016-11-18 02:58:18 +0200 | [diff] [blame] | 88 | struct drm_display_mode *mode; |
| 89 | |
Sam Ravnborg | aa6c436 | 2019-12-07 15:03:35 +0100 | [diff] [blame] | 90 | mode = drm_mode_create(connector->dev); |
Laurent Pinchart | 7c9dff5 | 2016-11-18 02:58:18 +0200 | [diff] [blame] | 91 | if (!mode) |
| 92 | return 0; |
| 93 | |
| 94 | drm_display_mode_from_videomode(&lvds->video_mode, mode); |
| 95 | mode->type |= DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED; |
| 96 | drm_mode_probed_add(connector, mode); |
| 97 | |
| 98 | connector->display_info.width_mm = lvds->width; |
| 99 | connector->display_info.height_mm = lvds->height; |
| 100 | drm_display_info_set_bus_formats(&connector->display_info, |
| 101 | &lvds->bus_format, 1); |
| 102 | connector->display_info.bus_flags = lvds->data_mirror |
| 103 | ? DRM_BUS_FLAG_DATA_LSB_TO_MSB |
| 104 | : DRM_BUS_FLAG_DATA_MSB_TO_LSB; |
Dmitry Osipenko | 58310c2e | 2020-08-14 00:56:08 +0300 | [diff] [blame] | 105 | drm_connector_set_panel_orientation(connector, lvds->orientation); |
Laurent Pinchart | 7c9dff5 | 2016-11-18 02:58:18 +0200 | [diff] [blame] | 106 | |
| 107 | return 1; |
| 108 | } |
| 109 | |
| 110 | static const struct drm_panel_funcs panel_lvds_funcs = { |
Laurent Pinchart | 7c9dff5 | 2016-11-18 02:58:18 +0200 | [diff] [blame] | 111 | .unprepare = panel_lvds_unprepare, |
| 112 | .prepare = panel_lvds_prepare, |
Laurent Pinchart | 7c9dff5 | 2016-11-18 02:58:18 +0200 | [diff] [blame] | 113 | .get_modes = panel_lvds_get_modes, |
| 114 | }; |
| 115 | |
| 116 | static int panel_lvds_parse_dt(struct panel_lvds *lvds) |
| 117 | { |
| 118 | struct device_node *np = lvds->dev->of_node; |
| 119 | struct display_timing timing; |
Laurent Pinchart | 7c9dff5 | 2016-11-18 02:58:18 +0200 | [diff] [blame] | 120 | int ret; |
| 121 | |
Dmitry Osipenko | 58310c2e | 2020-08-14 00:56:08 +0300 | [diff] [blame] | 122 | ret = of_drm_get_panel_orientation(np, &lvds->orientation); |
| 123 | if (ret < 0) { |
| 124 | dev_err(lvds->dev, "%pOF: failed to get orientation %d\n", np, ret); |
| 125 | return ret; |
| 126 | } |
| 127 | |
Laurent Pinchart | 7c9dff5 | 2016-11-18 02:58:18 +0200 | [diff] [blame] | 128 | ret = of_get_display_timing(np, "panel-timing", &timing); |
Douglas Anderson | c2d7e47 | 2019-07-22 11:24:38 -0700 | [diff] [blame] | 129 | if (ret < 0) { |
| 130 | dev_err(lvds->dev, "%pOF: problems parsing panel-timing (%d)\n", |
| 131 | np, ret); |
Laurent Pinchart | 7c9dff5 | 2016-11-18 02:58:18 +0200 | [diff] [blame] | 132 | return ret; |
Douglas Anderson | c2d7e47 | 2019-07-22 11:24:38 -0700 | [diff] [blame] | 133 | } |
Laurent Pinchart | 7c9dff5 | 2016-11-18 02:58:18 +0200 | [diff] [blame] | 134 | |
| 135 | videomode_from_timing(&timing, &lvds->video_mode); |
| 136 | |
| 137 | ret = of_property_read_u32(np, "width-mm", &lvds->width); |
| 138 | if (ret < 0) { |
Rob Herring | 4bf9914 | 2017-07-18 16:43:04 -0500 | [diff] [blame] | 139 | dev_err(lvds->dev, "%pOF: invalid or missing %s DT property\n", |
| 140 | np, "width-mm"); |
Laurent Pinchart | 7c9dff5 | 2016-11-18 02:58:18 +0200 | [diff] [blame] | 141 | return -ENODEV; |
| 142 | } |
| 143 | ret = of_property_read_u32(np, "height-mm", &lvds->height); |
| 144 | if (ret < 0) { |
Rob Herring | 4bf9914 | 2017-07-18 16:43:04 -0500 | [diff] [blame] | 145 | dev_err(lvds->dev, "%pOF: invalid or missing %s DT property\n", |
| 146 | np, "height-mm"); |
Laurent Pinchart | 7c9dff5 | 2016-11-18 02:58:18 +0200 | [diff] [blame] | 147 | return -ENODEV; |
| 148 | } |
| 149 | |
| 150 | of_property_read_string(np, "label", &lvds->label); |
| 151 | |
Marek Vasut | 7c4dd0a | 2021-10-13 00:42:52 +0200 | [diff] [blame] | 152 | ret = drm_of_lvds_get_data_mapping(np); |
Laurent Pinchart | 7c9dff5 | 2016-11-18 02:58:18 +0200 | [diff] [blame] | 153 | if (ret < 0) { |
Rob Herring | 4bf9914 | 2017-07-18 16:43:04 -0500 | [diff] [blame] | 154 | dev_err(lvds->dev, "%pOF: invalid or missing %s DT property\n", |
| 155 | np, "data-mapping"); |
Marek Vasut | 7c4dd0a | 2021-10-13 00:42:52 +0200 | [diff] [blame] | 156 | return ret; |
Laurent Pinchart | 7c9dff5 | 2016-11-18 02:58:18 +0200 | [diff] [blame] | 157 | } |
| 158 | |
Marek Vasut | 7c4dd0a | 2021-10-13 00:42:52 +0200 | [diff] [blame] | 159 | lvds->bus_format = ret; |
Laurent Pinchart | 7c9dff5 | 2016-11-18 02:58:18 +0200 | [diff] [blame] | 160 | |
| 161 | lvds->data_mirror = of_property_read_bool(np, "data-mirror"); |
| 162 | |
| 163 | return 0; |
| 164 | } |
| 165 | |
| 166 | static int panel_lvds_probe(struct platform_device *pdev) |
| 167 | { |
| 168 | struct panel_lvds *lvds; |
Laurent Pinchart | 7c9dff5 | 2016-11-18 02:58:18 +0200 | [diff] [blame] | 169 | int ret; |
| 170 | |
| 171 | lvds = devm_kzalloc(&pdev->dev, sizeof(*lvds), GFP_KERNEL); |
| 172 | if (!lvds) |
| 173 | return -ENOMEM; |
| 174 | |
| 175 | lvds->dev = &pdev->dev; |
| 176 | |
| 177 | ret = panel_lvds_parse_dt(lvds); |
| 178 | if (ret < 0) |
| 179 | return ret; |
| 180 | |
Maxime Ripard | a1c55bc | 2017-12-21 12:02:28 +0100 | [diff] [blame] | 181 | lvds->supply = devm_regulator_get_optional(lvds->dev, "power"); |
| 182 | if (IS_ERR(lvds->supply)) { |
| 183 | ret = PTR_ERR(lvds->supply); |
Maxime Ripard | a0d6053 | 2018-01-10 16:59:41 +0100 | [diff] [blame] | 184 | |
| 185 | if (ret != -ENODEV) { |
| 186 | if (ret != -EPROBE_DEFER) |
| 187 | dev_err(lvds->dev, "failed to request regulator: %d\n", |
| 188 | ret); |
| 189 | return ret; |
| 190 | } |
| 191 | |
| 192 | lvds->supply = NULL; |
Maxime Ripard | a1c55bc | 2017-12-21 12:02:28 +0100 | [diff] [blame] | 193 | } |
| 194 | |
Laurent Pinchart | 7c9dff5 | 2016-11-18 02:58:18 +0200 | [diff] [blame] | 195 | /* Get GPIOs and backlight controller. */ |
| 196 | lvds->enable_gpio = devm_gpiod_get_optional(lvds->dev, "enable", |
| 197 | GPIOD_OUT_LOW); |
| 198 | if (IS_ERR(lvds->enable_gpio)) { |
| 199 | ret = PTR_ERR(lvds->enable_gpio); |
| 200 | dev_err(lvds->dev, "failed to request %s GPIO: %d\n", |
| 201 | "enable", ret); |
| 202 | return ret; |
| 203 | } |
| 204 | |
| 205 | lvds->reset_gpio = devm_gpiod_get_optional(lvds->dev, "reset", |
| 206 | GPIOD_OUT_HIGH); |
| 207 | if (IS_ERR(lvds->reset_gpio)) { |
| 208 | ret = PTR_ERR(lvds->reset_gpio); |
| 209 | dev_err(lvds->dev, "failed to request %s GPIO: %d\n", |
| 210 | "reset", ret); |
| 211 | return ret; |
| 212 | } |
| 213 | |
Laurent Pinchart | 7c9dff5 | 2016-11-18 02:58:18 +0200 | [diff] [blame] | 214 | /* |
| 215 | * TODO: Handle all power supplies specified in the DT node in a generic |
| 216 | * way for panels that don't care about power supply ordering. LVDS |
| 217 | * panels that require a specific power sequence will need a dedicated |
| 218 | * driver. |
| 219 | */ |
| 220 | |
| 221 | /* Register the panel. */ |
Laurent Pinchart | 9a2654c | 2019-09-04 16:28:03 +0300 | [diff] [blame] | 222 | drm_panel_init(&lvds->panel, lvds->dev, &panel_lvds_funcs, |
| 223 | DRM_MODE_CONNECTOR_LVDS); |
Laurent Pinchart | 7c9dff5 | 2016-11-18 02:58:18 +0200 | [diff] [blame] | 224 | |
Sam Ravnborg | 17d5183 | 2019-12-07 15:03:40 +0100 | [diff] [blame] | 225 | ret = drm_panel_of_backlight(&lvds->panel); |
| 226 | if (ret) |
| 227 | return ret; |
| 228 | |
Bernard Zhao | c3ee8c6 | 2020-08-01 20:02:13 +0800 | [diff] [blame] | 229 | drm_panel_add(&lvds->panel); |
Laurent Pinchart | 7c9dff5 | 2016-11-18 02:58:18 +0200 | [diff] [blame] | 230 | |
| 231 | dev_set_drvdata(lvds->dev, lvds); |
| 232 | return 0; |
Laurent Pinchart | 7c9dff5 | 2016-11-18 02:58:18 +0200 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | static int panel_lvds_remove(struct platform_device *pdev) |
| 236 | { |
Julia Lawall | 5dd331d | 2021-02-09 22:13:04 +0100 | [diff] [blame] | 237 | struct panel_lvds *lvds = platform_get_drvdata(pdev); |
Laurent Pinchart | 7c9dff5 | 2016-11-18 02:58:18 +0200 | [diff] [blame] | 238 | |
Laurent Pinchart | 7c9dff5 | 2016-11-18 02:58:18 +0200 | [diff] [blame] | 239 | drm_panel_remove(&lvds->panel); |
| 240 | |
Sam Ravnborg | 17d5183 | 2019-12-07 15:03:40 +0100 | [diff] [blame] | 241 | drm_panel_disable(&lvds->panel); |
Laurent Pinchart | 7c9dff5 | 2016-11-18 02:58:18 +0200 | [diff] [blame] | 242 | |
Laurent Pinchart | 7c9dff5 | 2016-11-18 02:58:18 +0200 | [diff] [blame] | 243 | return 0; |
| 244 | } |
| 245 | |
| 246 | static const struct of_device_id panel_lvds_of_table[] = { |
| 247 | { .compatible = "panel-lvds", }, |
| 248 | { /* Sentinel */ }, |
| 249 | }; |
| 250 | |
| 251 | MODULE_DEVICE_TABLE(of, panel_lvds_of_table); |
| 252 | |
| 253 | static struct platform_driver panel_lvds_driver = { |
| 254 | .probe = panel_lvds_probe, |
| 255 | .remove = panel_lvds_remove, |
| 256 | .driver = { |
| 257 | .name = "panel-lvds", |
| 258 | .of_match_table = panel_lvds_of_table, |
| 259 | }, |
| 260 | }; |
| 261 | |
| 262 | module_platform_driver(panel_lvds_driver); |
| 263 | |
| 264 | MODULE_AUTHOR("Laurent Pinchart <laurent.pinchart@ideasonboard.com>"); |
| 265 | MODULE_DESCRIPTION("LVDS Panel Driver"); |
| 266 | MODULE_LICENSE("GPL"); |