1304046Sae// SPDX-License-Identifier: GPL-2.0-only 2346211Sae/* 3304046Sae * Copyright (C) 2014 NVIDIA Corporation 4346211Sae */ 5346211Sae 6346211Sae#include <linux/delay.h> 7304046Sae#include <linux/gpio/consumer.h> 8304046Sae#include <linux/module.h> 9304046Sae#include <linux/of.h> 10304046Sae#include <linux/regulator/consumer.h> 11304046Sae 12304046Sae#include <video/mipi_display.h> 13304046Sae 14304046Sae#include <drm/drm_crtc.h> 15304046Sae#include <drm/drm_device.h> 16304046Sae#include <drm/drm_mipi_dsi.h> 17304046Sae#include <drm/drm_panel.h> 18304046Sae 19304046Saestruct sharp_panel { 20304046Sae struct drm_panel base; 21304046Sae /* the datasheet refers to them as DSI-LINK1 and DSI-LINK2 */ 22304046Sae struct mipi_dsi_device *link1; 23304046Sae struct mipi_dsi_device *link2; 24304046Sae 25304046Sae struct regulator *supply; 26304046Sae 27304046Sae bool prepared; 28304046Sae bool enabled; 29304046Sae 30304046Sae const struct drm_display_mode *mode; 31304046Sae}; 32304046Sae 33304046Saestatic inline struct sharp_panel *to_sharp_panel(struct drm_panel *panel) 34304046Sae{ 35304046Sae return container_of(panel, struct sharp_panel, base); 36304046Sae} 37304046Sae 38304046Saestatic void sharp_wait_frames(struct sharp_panel *sharp, unsigned int frames) 39304046Sae{ 40304046Sae unsigned int refresh = drm_mode_vrefresh(sharp->mode); 41304046Sae 42304046Sae if (WARN_ON(frames > refresh)) 43304046Sae return; 44304046Sae 45304046Sae msleep(1000 / (refresh / frames)); 46304046Sae} 47304046Sae 48304046Saestatic int sharp_panel_write(struct sharp_panel *sharp, u16 offset, u8 value) 49304046Sae{ 50304046Sae u8 payload[3] = { offset >> 8, offset & 0xff, value }; 51304046Sae struct mipi_dsi_device *dsi = sharp->link1; 52304046Sae ssize_t err; 53332766Sae 54304046Sae err = mipi_dsi_generic_write(dsi, payload, sizeof(payload)); 55304046Sae if (err < 0) { 56304046Sae dev_err(&dsi->dev, "failed to write %02x to %04x: %zd\n", 57304046Sae value, offset, err); 58304046Sae return err; 59304046Sae } 60304046Sae 61304046Sae err = mipi_dsi_dcs_nop(dsi); 62304046Sae if (err < 0) { 63332766Sae dev_err(&dsi->dev, "failed to send DCS nop: %zd\n", err); 64304046Sae return err; 65346210Sae } 66304046Sae 67304046Sae usleep_range(10, 20); 68304046Sae 69304046Sae return 0; 70304046Sae} 71334836Sae 72334836Saestatic __maybe_unused int sharp_panel_read(struct sharp_panel *sharp, 73334836Sae u16 offset, u8 *value) 74346209Sae{ 75346209Sae ssize_t err; 76346209Sae 77346209Sae cpu_to_be16s(&offset); 78346209Sae 79346209Sae err = mipi_dsi_generic_read(sharp->link1, &offset, sizeof(offset), 80346209Sae value, sizeof(*value)); 81346209Sae if (err < 0) 82346209Sae dev_err(&sharp->link1->dev, "failed to read from %04x: %zd\n", 83346209Sae offset, err); 84346209Sae 85346209Sae return err; 86346209Sae} 87346209Sae 88346209Saestatic int sharp_panel_disable(struct drm_panel *panel) 89346209Sae{ 90346209Sae struct sharp_panel *sharp = to_sharp_panel(panel); 91346209Sae 92346209Sae if (!sharp->enabled) 93346209Sae return 0; 94346209Sae 95346209Sae sharp->enabled = false; 96346209Sae 97346209Sae return 0; 98346209Sae} 99346209Sae 100346209Saestatic int sharp_panel_unprepare(struct drm_panel *panel) 101346209Sae{ 102346209Sae struct sharp_panel *sharp = to_sharp_panel(panel); 103346209Sae int err; 104346209Sae 105346209Sae if (!sharp->prepared) 106346209Sae return 0; 107346209Sae 108346209Sae sharp_wait_frames(sharp, 4); 109346209Sae 110346209Sae err = mipi_dsi_dcs_set_display_off(sharp->link1); 111346209Sae if (err < 0) 112346209Sae dev_err(panel->dev, "failed to set display off: %d\n", err); 113346209Sae 114346209Sae err = mipi_dsi_dcs_enter_sleep_mode(sharp->link1); 115346209Sae if (err < 0) 116346209Sae dev_err(panel->dev, "failed to enter sleep mode: %d\n", err); 117346209Sae 118346209Sae msleep(120); 119346209Sae 120346209Sae regulator_disable(sharp->supply); 121304046Sae 122304046Sae sharp->prepared = false; 123304046Sae 124304046Sae return 0; 125304046Sae} 126304046Sae 127304046Saestatic int sharp_setup_symmetrical_split(struct mipi_dsi_device *left, 128304046Sae struct mipi_dsi_device *right, 129332766Sae const struct drm_display_mode *mode) 130346209Sae{ 131346209Sae int err; 132334836Sae 133304046Sae err = mipi_dsi_dcs_set_column_address(left, 0, mode->hdisplay / 2 - 1); 134304046Sae if (err < 0) { 135304046Sae dev_err(&left->dev, "failed to set column address: %d\n", err); 136304046Sae return err; 137304046Sae } 138334836Sae 139304046Sae err = mipi_dsi_dcs_set_page_address(left, 0, mode->vdisplay - 1); 140304046Sae if (err < 0) { 141304046Sae dev_err(&left->dev, "failed to set page address: %d\n", err); 142304046Sae return err; 143304046Sae } 144346209Sae 145346209Sae err = mipi_dsi_dcs_set_column_address(right, mode->hdisplay / 2, 146346209Sae mode->hdisplay - 1); 147304046Sae if (err < 0) { 148332766Sae dev_err(&right->dev, "failed to set column address: %d\n", err); 149332766Sae return err; 150332766Sae } 151332766Sae 152304046Sae err = mipi_dsi_dcs_set_page_address(right, 0, mode->vdisplay - 1); 153304046Sae if (err < 0) { 154304046Sae dev_err(&right->dev, "failed to set page address: %d\n", err); 155332766Sae return err; 156304046Sae } 157304046Sae 158304046Sae return 0; 159304046Sae} 160304046Sae 161332766Saestatic int sharp_panel_prepare(struct drm_panel *panel) 162332766Sae{ 163332766Sae struct sharp_panel *sharp = to_sharp_panel(panel); 164304046Sae u8 format = MIPI_DCS_PIXEL_FMT_24BIT; 165332766Sae int err; 166332766Sae 167332766Sae if (sharp->prepared) 168332766Sae return 0; 169304046Sae 170304046Sae err = regulator_enable(sharp->supply); 171332766Sae if (err < 0) 172332766Sae return err; 173332766Sae 174332766Sae /* 175304046Sae * According to the datasheet, the panel needs around 10 ms to fully 176332766Sae * power up. At least another 120 ms is required before exiting sleep 177332766Sae * mode to make sure the panel is ready. Throw in another 20 ms for 178332766Sae * good measure. 179332766Sae */ 180304046Sae msleep(150); 181304046Sae 182304046Sae err = mipi_dsi_dcs_exit_sleep_mode(sharp->link1); 183304046Sae if (err < 0) { 184304046Sae dev_err(panel->dev, "failed to exit sleep mode: %d\n", err); 185304046Sae goto poweroff; 186304046Sae } 187332766Sae 188304046Sae /* 189304046Sae * The MIPI DCS specification mandates this delay only between the 190304046Sae * exit_sleep_mode and enter_sleep_mode commands, so it isn't strictly 191304046Sae * necessary here. 192304046Sae */ 193332766Sae /* 194304046Sae msleep(120); 195304046Sae */ 196304046Sae 197304046Sae /* set left-right mode */ 198346209Sae err = sharp_panel_write(sharp, 0x1000, 0x2a); 199346209Sae if (err < 0) { 200334836Sae dev_err(panel->dev, "failed to set left-right mode: %d\n", err); 201334836Sae goto poweroff; 202304046Sae } 203304046Sae 204304046Sae /* enable command mode */ 205304046Sae err = sharp_panel_write(sharp, 0x1001, 0x01); 206304046Sae if (err < 0) { 207304046Sae dev_err(panel->dev, "failed to enable command mode: %d\n", err); 208304046Sae goto poweroff; 209304046Sae } 210304046Sae 211304046Sae err = mipi_dsi_dcs_set_pixel_format(sharp->link1, format); 212304046Sae if (err < 0) { 213304046Sae dev_err(panel->dev, "failed to set pixel format: %d\n", err); 214304046Sae goto poweroff; 215304046Sae } 216304046Sae 217304046Sae /* 218304046Sae * TODO: The device supports both left-right and even-odd split 219304046Sae * configurations, but this driver currently supports only the left- 220304046Sae * right split. To support a different mode a mechanism needs to be 221304046Sae * put in place to communicate the configuration back to the DSI host 222304046Sae * controller. 223304046Sae */ 224345257Sae err = sharp_setup_symmetrical_split(sharp->link1, sharp->link2, 225345257Sae sharp->mode); 226304046Sae if (err < 0) { 227304046Sae dev_err(panel->dev, "failed to set up symmetrical split: %d\n", 228304046Sae err); 229304046Sae goto poweroff; 230304046Sae } 231304046Sae 232346209Sae err = mipi_dsi_dcs_set_display_on(sharp->link1); 233334836Sae if (err < 0) { 234304046Sae dev_err(panel->dev, "failed to set display on: %d\n", err); 235304046Sae goto poweroff; 236334836Sae } 237304046Sae 238304046Sae sharp->prepared = true; 239334836Sae 240334836Sae /* wait for 6 frames before continuing */ 241334836Sae sharp_wait_frames(sharp, 6); 242334836Sae 243334836Sae return 0; 244334836Sae 245334836Saepoweroff: 246346210Sae regulator_disable(sharp->supply); 247334836Sae return err; 248304046Sae} 249334836Sae 250334836Saestatic int sharp_panel_enable(struct drm_panel *panel) 251334836Sae{ 252334836Sae struct sharp_panel *sharp = to_sharp_panel(panel); 253334836Sae 254334836Sae if (sharp->enabled) 255334836Sae return 0; 256334836Sae 257334836Sae sharp->enabled = true; 258334836Sae 259334836Sae return 0; 260304046Sae} 261334836Sae 262346210Saestatic const struct drm_display_mode default_mode = { 263346210Sae .clock = 278000, 264346210Sae .hdisplay = 2560, 265346210Sae .hsync_start = 2560 + 128, 266346210Sae .hsync_end = 2560 + 128 + 64, 267346210Sae .htotal = 2560 + 128 + 64 + 64, 268346210Sae .vdisplay = 1600, 269346210Sae .vsync_start = 1600 + 4, 270346210Sae .vsync_end = 1600 + 4 + 8, 271346210Sae .vtotal = 1600 + 4 + 8 + 32, 272346210Sae}; 273346210Sae 274346210Saestatic int sharp_panel_get_modes(struct drm_panel *panel, 275346210Sae struct drm_connector *connector) 276346210Sae{ 277346210Sae struct drm_display_mode *mode; 278346210Sae 279346210Sae mode = drm_mode_duplicate(connector->dev, &default_mode); 280346210Sae if (!mode) { 281346210Sae dev_err(panel->dev, "failed to add mode %ux%ux@%u\n", 282346210Sae default_mode.hdisplay, default_mode.vdisplay, 283346210Sae drm_mode_vrefresh(&default_mode)); 284346210Sae return -ENOMEM; 285334836Sae } 286304046Sae 287304046Sae drm_mode_set_name(mode); 288346210Sae 289334836Sae drm_mode_probed_add(connector, mode); 290334836Sae 291334836Sae connector->display_info.width_mm = 217; 292334836Sae connector->display_info.height_mm = 136; 293334836Sae 294334836Sae return 1; 295334836Sae} 296334836Sae 297334836Saestatic const struct drm_panel_funcs sharp_panel_funcs = { 298334836Sae .disable = sharp_panel_disable, 299334836Sae .unprepare = sharp_panel_unprepare, 300334836Sae .prepare = sharp_panel_prepare, 301334836Sae .enable = sharp_panel_enable, 302334836Sae .get_modes = sharp_panel_get_modes, 303334836Sae}; 304334836Sae 305334836Saestatic const struct of_device_id sharp_of_match[] = { 306334836Sae { .compatible = "sharp,lq101r1sx01", }, 307334836Sae { } 308334836Sae}; 309334836SaeMODULE_DEVICE_TABLE(of, sharp_of_match); 310334836Sae 311334836Saestatic int sharp_panel_add(struct sharp_panel *sharp) 312334836Sae{ 313334836Sae int ret; 314334836Sae 315304046Sae sharp->mode = &default_mode; 316304046Sae 317346210Sae sharp->supply = devm_regulator_get(&sharp->link1->dev, "power"); 318346210Sae if (IS_ERR(sharp->supply)) 319346210Sae return PTR_ERR(sharp->supply); 320346210Sae 321334836Sae drm_panel_init(&sharp->base, &sharp->link1->dev, &sharp_panel_funcs, 322346210Sae DRM_MODE_CONNECTOR_DSI); 323304046Sae 324304046Sae ret = drm_panel_of_backlight(&sharp->base); 325346210Sae if (ret) 326304046Sae return ret; 327304046Sae 328346210Sae drm_panel_add(&sharp->base); 329304046Sae 330304046Sae return 0; 331304046Sae} 332304046Sae 333346210Saestatic void sharp_panel_del(struct sharp_panel *sharp) 334346210Sae{ 335346210Sae if (sharp->base.dev) 336346210Sae drm_panel_remove(&sharp->base); 337346210Sae 338346210Sae if (sharp->link2) 339304046Sae put_device(&sharp->link2->dev); 340346210Sae} 341346210Sae 342304046Saestatic int sharp_panel_probe(struct mipi_dsi_device *dsi) 343346210Sae{ 344346210Sae struct mipi_dsi_device *secondary = NULL; 345304046Sae struct sharp_panel *sharp; 346304046Sae struct device_node *np; 347304046Sae int err; 348304046Sae 349304046Sae dsi->lanes = 4; 350304046Sae dsi->format = MIPI_DSI_FMT_RGB888; 351304046Sae dsi->mode_flags = MIPI_DSI_MODE_LPM; 352304046Sae 353304046Sae /* Find DSI-LINK1 */ 354304046Sae np = of_parse_phandle(dsi->dev.of_node, "link2", 0); 355304046Sae if (np) { 356304046Sae secondary = of_find_mipi_dsi_device_by_node(np); 357346210Sae of_node_put(np); 358304046Sae 359346210Sae if (!secondary) 360346210Sae return -EPROBE_DEFER; 361346210Sae } 362346210Sae 363346210Sae /* register a panel for only the DSI-LINK1 interface */ 364304046Sae if (secondary) { 365304046Sae sharp = devm_kzalloc(&dsi->dev, sizeof(*sharp), GFP_KERNEL); 366304046Sae if (!sharp) { 367334836Sae put_device(&secondary->dev); 368346210Sae return -ENOMEM; 369304046Sae } 370304046Sae 371304046Sae mipi_dsi_set_drvdata(dsi, sharp); 372304046Sae 373304046Sae sharp->link2 = secondary; 374304046Sae sharp->link1 = dsi; 375304046Sae 376304046Sae err = sharp_panel_add(sharp); 377304046Sae if (err < 0) { 378304046Sae put_device(&secondary->dev); 379346210Sae return err; 380304046Sae } 381304046Sae } 382304046Sae 383304046Sae err = mipi_dsi_attach(dsi); 384304046Sae if (err < 0) { 385304046Sae if (secondary) 386304046Sae sharp_panel_del(sharp); 387304046Sae 388304046Sae return err; 389304046Sae } 390304046Sae 391304046Sae return 0; 392304046Sae} 393304046Sae 394304046Saestatic void sharp_panel_remove(struct mipi_dsi_device *dsi) 395304046Sae{ 396304046Sae struct sharp_panel *sharp = mipi_dsi_get_drvdata(dsi); 397304046Sae int err; 398304046Sae 399304046Sae /* only detach from host for the DSI-LINK2 interface */ 400304046Sae if (!sharp) { 401304046Sae mipi_dsi_detach(dsi); 402304046Sae return; 403346210Sae } 404304046Sae 405304046Sae err = drm_panel_disable(&sharp->base); 406346210Sae if (err < 0) 407304046Sae dev_err(&dsi->dev, "failed to disable panel: %d\n", err); 408304046Sae 409304046Sae err = mipi_dsi_detach(dsi); 410304046Sae if (err < 0) 411304046Sae dev_err(&dsi->dev, "failed to detach from DSI host: %d\n", err); 412346210Sae 413346210Sae sharp_panel_del(sharp); 414304046Sae} 415346210Sae 416346210Saestatic void sharp_panel_shutdown(struct mipi_dsi_device *dsi) 417304046Sae{ 418304046Sae struct sharp_panel *sharp = mipi_dsi_get_drvdata(dsi); 419304046Sae 420304046Sae /* nothing to do for DSI-LINK2 */ 421304046Sae if (!sharp) 422304046Sae return; 423304046Sae 424304046Sae drm_panel_disable(&sharp->base); 425304046Sae} 426304046Sae 427304046Saestatic struct mipi_dsi_driver sharp_panel_driver = { 428304046Sae .driver = { 429346210Sae .name = "panel-sharp-lq101r1sx01", 430346210Sae .of_match_table = sharp_of_match, 431304046Sae }, 432334836Sae .probe = sharp_panel_probe, 433334836Sae .remove = sharp_panel_remove, 434304046Sae .shutdown = sharp_panel_shutdown, 435304046Sae}; 436334836Saemodule_mipi_dsi_driver(sharp_panel_driver); 437304046Sae 438304046SaeMODULE_AUTHOR("Thierry Reding <treding@nvidia.com>"); 439304046SaeMODULE_DESCRIPTION("Sharp LQ101R1SX01 panel driver"); 440304046SaeMODULE_LICENSE("GPL v2"); 441304046Sae