1 /* 2 * LCD panel driver for Sharp LS037V7DW01 3 * 4 * Copyright (C) 2013 Texas Instruments 5 * Author: Tomi Valkeinen <tomi.valkeinen@ti.com> 6 * 7 * This program is free software; you can redistribute it and/or modify it 8 * under the terms of the GNU General Public License version 2 as published by 9 * the Free Software Foundation. 10 */ 11 12 #include <linux/delay.h> 13 #include <linux/gpio.h> 14 #include <linux/module.h> 15 #include <linux/of.h> 16 #include <linux/of_gpio.h> 17 #include <linux/platform_device.h> 18 #include <linux/slab.h> 19 #include <linux/regulator/consumer.h> 20 #include <video/omapdss.h> 21 #include <video/omap-panel-data.h> 22 23 struct panel_drv_data { 24 struct omap_dss_device dssdev; 25 struct omap_dss_device *in; 26 struct regulator *vcc; 27 28 int data_lines; 29 30 struct omap_video_timings videomode; 31 32 struct gpio_desc *resb_gpio; /* low = reset active min 20 us */ 33 struct gpio_desc *ini_gpio; /* high = power on */ 34 struct gpio_desc *mo_gpio; /* low = 480x640, high = 240x320 */ 35 struct gpio_desc *lr_gpio; /* high = conventional horizontal scanning */ 36 struct gpio_desc *ud_gpio; /* high = conventional vertical scanning */ 37 }; 38 39 static const struct omap_video_timings sharp_ls_timings = { 40 .x_res = 480, 41 .y_res = 640, 42 43 .pixelclock = 19200000, 44 45 .hsw = 2, 46 .hfp = 1, 47 .hbp = 28, 48 49 .vsw = 1, 50 .vfp = 1, 51 .vbp = 1, 52 53 .vsync_level = OMAPDSS_SIG_ACTIVE_LOW, 54 .hsync_level = OMAPDSS_SIG_ACTIVE_LOW, 55 .data_pclk_edge = OMAPDSS_DRIVE_SIG_RISING_EDGE, 56 .de_level = OMAPDSS_SIG_ACTIVE_HIGH, 57 .sync_pclk_edge = OMAPDSS_DRIVE_SIG_FALLING_EDGE, 58 }; 59 60 #define to_panel_data(p) container_of(p, struct panel_drv_data, dssdev) 61 62 static int sharp_ls_connect(struct omap_dss_device *dssdev) 63 { 64 struct panel_drv_data *ddata = to_panel_data(dssdev); 65 struct omap_dss_device *in = ddata->in; 66 int r; 67 68 if (omapdss_device_is_connected(dssdev)) 69 return 0; 70 71 r = in->ops.dpi->connect(in, dssdev); 72 if (r) 73 return r; 74 75 return 0; 76 } 77 78 static void sharp_ls_disconnect(struct omap_dss_device *dssdev) 79 { 80 struct panel_drv_data *ddata = to_panel_data(dssdev); 81 struct omap_dss_device *in = ddata->in; 82 83 if (!omapdss_device_is_connected(dssdev)) 84 return; 85 86 in->ops.dpi->disconnect(in, dssdev); 87 } 88 89 static int sharp_ls_enable(struct omap_dss_device *dssdev) 90 { 91 struct panel_drv_data *ddata = to_panel_data(dssdev); 92 struct omap_dss_device *in = ddata->in; 93 int r; 94 95 if (!omapdss_device_is_connected(dssdev)) 96 return -ENODEV; 97 98 if (omapdss_device_is_enabled(dssdev)) 99 return 0; 100 101 if (ddata->data_lines) 102 in->ops.dpi->set_data_lines(in, ddata->data_lines); 103 in->ops.dpi->set_timings(in, &ddata->videomode); 104 105 if (ddata->vcc) { 106 r = regulator_enable(ddata->vcc); 107 if (r != 0) 108 return r; 109 } 110 111 r = in->ops.dpi->enable(in); 112 if (r) { 113 regulator_disable(ddata->vcc); 114 return r; 115 } 116 117 /* wait couple of vsyncs until enabling the LCD */ 118 msleep(50); 119 120 if (ddata->resb_gpio) 121 gpiod_set_value_cansleep(ddata->resb_gpio, 1); 122 123 if (ddata->ini_gpio) 124 gpiod_set_value_cansleep(ddata->ini_gpio, 1); 125 126 dssdev->state = OMAP_DSS_DISPLAY_ACTIVE; 127 128 return 0; 129 } 130 131 static void sharp_ls_disable(struct omap_dss_device *dssdev) 132 { 133 struct panel_drv_data *ddata = to_panel_data(dssdev); 134 struct omap_dss_device *in = ddata->in; 135 136 if (!omapdss_device_is_enabled(dssdev)) 137 return; 138 139 if (ddata->ini_gpio) 140 gpiod_set_value_cansleep(ddata->ini_gpio, 0); 141 142 if (ddata->resb_gpio) 143 gpiod_set_value_cansleep(ddata->resb_gpio, 0); 144 145 /* wait at least 5 vsyncs after disabling the LCD */ 146 147 msleep(100); 148 149 in->ops.dpi->disable(in); 150 151 if (ddata->vcc) 152 regulator_disable(ddata->vcc); 153 154 dssdev->state = OMAP_DSS_DISPLAY_DISABLED; 155 } 156 157 static void sharp_ls_set_timings(struct omap_dss_device *dssdev, 158 struct omap_video_timings *timings) 159 { 160 struct panel_drv_data *ddata = to_panel_data(dssdev); 161 struct omap_dss_device *in = ddata->in; 162 163 ddata->videomode = *timings; 164 dssdev->panel.timings = *timings; 165 166 in->ops.dpi->set_timings(in, timings); 167 } 168 169 static void sharp_ls_get_timings(struct omap_dss_device *dssdev, 170 struct omap_video_timings *timings) 171 { 172 struct panel_drv_data *ddata = to_panel_data(dssdev); 173 174 *timings = ddata->videomode; 175 } 176 177 static int sharp_ls_check_timings(struct omap_dss_device *dssdev, 178 struct omap_video_timings *timings) 179 { 180 struct panel_drv_data *ddata = to_panel_data(dssdev); 181 struct omap_dss_device *in = ddata->in; 182 183 return in->ops.dpi->check_timings(in, timings); 184 } 185 186 static struct omap_dss_driver sharp_ls_ops = { 187 .connect = sharp_ls_connect, 188 .disconnect = sharp_ls_disconnect, 189 190 .enable = sharp_ls_enable, 191 .disable = sharp_ls_disable, 192 193 .set_timings = sharp_ls_set_timings, 194 .get_timings = sharp_ls_get_timings, 195 .check_timings = sharp_ls_check_timings, 196 197 .get_resolution = omapdss_default_get_resolution, 198 }; 199 200 static int sharp_ls_get_gpio(struct device *dev, int gpio, unsigned long flags, 201 char *desc, struct gpio_desc **gpiod) 202 { 203 int r; 204 205 r = devm_gpio_request_one(dev, gpio, flags, desc); 206 if (r) { 207 *gpiod = NULL; 208 return r == -ENOENT ? 0 : r; 209 } 210 211 *gpiod = gpio_to_desc(gpio); 212 213 return 0; 214 } 215 216 static int sharp_ls_probe_pdata(struct platform_device *pdev) 217 { 218 const struct panel_sharp_ls037v7dw01_platform_data *pdata; 219 struct panel_drv_data *ddata = platform_get_drvdata(pdev); 220 struct omap_dss_device *dssdev, *in; 221 int r; 222 223 pdata = dev_get_platdata(&pdev->dev); 224 225 in = omap_dss_find_output(pdata->source); 226 if (in == NULL) { 227 dev_err(&pdev->dev, "failed to find video source '%s'\n", 228 pdata->source); 229 return -EPROBE_DEFER; 230 } 231 232 ddata->in = in; 233 234 ddata->data_lines = pdata->data_lines; 235 236 dssdev = &ddata->dssdev; 237 dssdev->name = pdata->name; 238 239 r = sharp_ls_get_gpio(&pdev->dev, pdata->mo_gpio, GPIOF_OUT_INIT_LOW, 240 "lcd MO", &ddata->mo_gpio); 241 if (r) 242 return r; 243 r = sharp_ls_get_gpio(&pdev->dev, pdata->lr_gpio, GPIOF_OUT_INIT_HIGH, 244 "lcd LR", &ddata->lr_gpio); 245 if (r) 246 return r; 247 r = sharp_ls_get_gpio(&pdev->dev, pdata->ud_gpio, GPIOF_OUT_INIT_HIGH, 248 "lcd UD", &ddata->ud_gpio); 249 if (r) 250 return r; 251 r = sharp_ls_get_gpio(&pdev->dev, pdata->resb_gpio, GPIOF_OUT_INIT_LOW, 252 "lcd RESB", &ddata->resb_gpio); 253 if (r) 254 return r; 255 r = sharp_ls_get_gpio(&pdev->dev, pdata->ini_gpio, GPIOF_OUT_INIT_LOW, 256 "lcd INI", &ddata->ini_gpio); 257 if (r) 258 return r; 259 260 return 0; 261 } 262 263 static int sharp_ls_get_gpio_of(struct device *dev, int index, int val, 264 const char *desc, struct gpio_desc **gpiod) 265 { 266 struct gpio_desc *gd; 267 268 *gpiod = NULL; 269 270 gd = devm_gpiod_get_index(dev, desc, index, GPIOD_OUT_LOW); 271 if (IS_ERR(gd)) 272 return PTR_ERR(gd); 273 274 *gpiod = gd; 275 return 0; 276 } 277 278 static int sharp_ls_probe_of(struct platform_device *pdev) 279 { 280 struct panel_drv_data *ddata = platform_get_drvdata(pdev); 281 struct device_node *node = pdev->dev.of_node; 282 struct omap_dss_device *in; 283 int r; 284 285 ddata->vcc = devm_regulator_get(&pdev->dev, "envdd"); 286 if (IS_ERR(ddata->vcc)) { 287 dev_err(&pdev->dev, "failed to get regulator\n"); 288 return PTR_ERR(ddata->vcc); 289 } 290 291 /* lcd INI */ 292 r = sharp_ls_get_gpio_of(&pdev->dev, 0, 0, "enable", &ddata->ini_gpio); 293 if (r) 294 return r; 295 296 /* lcd RESB */ 297 r = sharp_ls_get_gpio_of(&pdev->dev, 0, 0, "reset", &ddata->resb_gpio); 298 if (r) 299 return r; 300 301 /* lcd MO */ 302 r = sharp_ls_get_gpio_of(&pdev->dev, 0, 0, "mode", &ddata->mo_gpio); 303 if (r) 304 return r; 305 306 /* lcd LR */ 307 r = sharp_ls_get_gpio_of(&pdev->dev, 1, 1, "mode", &ddata->lr_gpio); 308 if (r) 309 return r; 310 311 /* lcd UD */ 312 r = sharp_ls_get_gpio_of(&pdev->dev, 2, 1, "mode", &ddata->ud_gpio); 313 if (r) 314 return r; 315 316 in = omapdss_of_find_source_for_first_ep(node); 317 if (IS_ERR(in)) { 318 dev_err(&pdev->dev, "failed to find video source\n"); 319 return PTR_ERR(in); 320 } 321 322 ddata->in = in; 323 324 return 0; 325 } 326 327 static int sharp_ls_probe(struct platform_device *pdev) 328 { 329 struct panel_drv_data *ddata; 330 struct omap_dss_device *dssdev; 331 int r; 332 333 ddata = devm_kzalloc(&pdev->dev, sizeof(*ddata), GFP_KERNEL); 334 if (ddata == NULL) 335 return -ENOMEM; 336 337 platform_set_drvdata(pdev, ddata); 338 339 if (dev_get_platdata(&pdev->dev)) { 340 r = sharp_ls_probe_pdata(pdev); 341 if (r) 342 return r; 343 } else if (pdev->dev.of_node) { 344 r = sharp_ls_probe_of(pdev); 345 if (r) 346 return r; 347 } else { 348 return -ENODEV; 349 } 350 351 ddata->videomode = sharp_ls_timings; 352 353 dssdev = &ddata->dssdev; 354 dssdev->dev = &pdev->dev; 355 dssdev->driver = &sharp_ls_ops; 356 dssdev->type = OMAP_DISPLAY_TYPE_DPI; 357 dssdev->owner = THIS_MODULE; 358 dssdev->panel.timings = ddata->videomode; 359 dssdev->phy.dpi.data_lines = ddata->data_lines; 360 361 r = omapdss_register_display(dssdev); 362 if (r) { 363 dev_err(&pdev->dev, "Failed to register panel\n"); 364 goto err_reg; 365 } 366 367 return 0; 368 369 err_reg: 370 omap_dss_put_device(ddata->in); 371 return r; 372 } 373 374 static int __exit sharp_ls_remove(struct platform_device *pdev) 375 { 376 struct panel_drv_data *ddata = platform_get_drvdata(pdev); 377 struct omap_dss_device *dssdev = &ddata->dssdev; 378 struct omap_dss_device *in = ddata->in; 379 380 omapdss_unregister_display(dssdev); 381 382 sharp_ls_disable(dssdev); 383 sharp_ls_disconnect(dssdev); 384 385 omap_dss_put_device(in); 386 387 return 0; 388 } 389 390 static const struct of_device_id sharp_ls_of_match[] = { 391 { .compatible = "omapdss,sharp,ls037v7dw01", }, 392 {}, 393 }; 394 395 MODULE_DEVICE_TABLE(of, sharp_ls_of_match); 396 397 static struct platform_driver sharp_ls_driver = { 398 .probe = sharp_ls_probe, 399 .remove = __exit_p(sharp_ls_remove), 400 .driver = { 401 .name = "panel-sharp-ls037v7dw01", 402 .of_match_table = sharp_ls_of_match, 403 .suppress_bind_attrs = true, 404 }, 405 }; 406 407 module_platform_driver(sharp_ls_driver); 408 409 MODULE_AUTHOR("Tomi Valkeinen <tomi.valkeinen@ti.com>"); 410 MODULE_DESCRIPTION("Sharp LS037V7DW01 Panel Driver"); 411 MODULE_LICENSE("GPL"); 412