panel-simple.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603
  1. /*
  2. * Copyright (C) 2013, NVIDIA Corporation. All rights reserved.
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sub license,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice (including the
  12. * next paragraph) shall be included in all copies or substantial portions
  13. * of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
  18. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  21. * DEALINGS IN THE SOFTWARE.
  22. */
  23. #include <linux/backlight.h>
  24. #include <linux/gpio/consumer.h>
  25. #include <linux/module.h>
  26. #include <linux/of_platform.h>
  27. #include <linux/platform_device.h>
  28. #include <linux/regulator/consumer.h>
  29. #include <drm/drmP.h>
  30. #include <drm/drm_crtc.h>
  31. #include <drm/drm_mipi_dsi.h>
  32. #include <drm/drm_panel.h>
  33. struct panel_desc {
  34. const struct drm_display_mode *modes;
  35. unsigned int num_modes;
  36. struct {
  37. unsigned int width;
  38. unsigned int height;
  39. } size;
  40. };
  41. struct panel_simple {
  42. struct drm_panel base;
  43. bool enabled;
  44. const struct panel_desc *desc;
  45. struct backlight_device *backlight;
  46. struct regulator *supply;
  47. struct i2c_adapter *ddc;
  48. struct gpio_desc *enable_gpio;
  49. };
  50. static inline struct panel_simple *to_panel_simple(struct drm_panel *panel)
  51. {
  52. return container_of(panel, struct panel_simple, base);
  53. }
  54. static int panel_simple_get_fixed_modes(struct panel_simple *panel)
  55. {
  56. struct drm_connector *connector = panel->base.connector;
  57. struct drm_device *drm = panel->base.drm;
  58. struct drm_display_mode *mode;
  59. unsigned int i, num = 0;
  60. if (!panel->desc)
  61. return 0;
  62. for (i = 0; i < panel->desc->num_modes; i++) {
  63. const struct drm_display_mode *m = &panel->desc->modes[i];
  64. mode = drm_mode_duplicate(drm, m);
  65. if (!mode) {
  66. dev_err(drm->dev, "failed to add mode %ux%u@%u\n",
  67. m->hdisplay, m->vdisplay, m->vrefresh);
  68. continue;
  69. }
  70. drm_mode_set_name(mode);
  71. drm_mode_probed_add(connector, mode);
  72. num++;
  73. }
  74. connector->display_info.width_mm = panel->desc->size.width;
  75. connector->display_info.height_mm = panel->desc->size.height;
  76. return num;
  77. }
  78. static int panel_simple_disable(struct drm_panel *panel)
  79. {
  80. struct panel_simple *p = to_panel_simple(panel);
  81. if (!p->enabled)
  82. return 0;
  83. if (p->backlight) {
  84. p->backlight->props.power = FB_BLANK_POWERDOWN;
  85. backlight_update_status(p->backlight);
  86. }
  87. if (p->enable_gpio)
  88. gpiod_set_value_cansleep(p->enable_gpio, 0);
  89. regulator_disable(p->supply);
  90. p->enabled = false;
  91. return 0;
  92. }
  93. static int panel_simple_enable(struct drm_panel *panel)
  94. {
  95. struct panel_simple *p = to_panel_simple(panel);
  96. int err;
  97. if (p->enabled)
  98. return 0;
  99. err = regulator_enable(p->supply);
  100. if (err < 0) {
  101. dev_err(panel->dev, "failed to enable supply: %d\n", err);
  102. return err;
  103. }
  104. if (p->enable_gpio)
  105. gpiod_set_value_cansleep(p->enable_gpio, 1);
  106. if (p->backlight) {
  107. p->backlight->props.power = FB_BLANK_UNBLANK;
  108. backlight_update_status(p->backlight);
  109. }
  110. p->enabled = true;
  111. return 0;
  112. }
  113. static int panel_simple_get_modes(struct drm_panel *panel)
  114. {
  115. struct panel_simple *p = to_panel_simple(panel);
  116. int num = 0;
  117. /* probe EDID if a DDC bus is available */
  118. if (p->ddc) {
  119. struct edid *edid = drm_get_edid(panel->connector, p->ddc);
  120. drm_mode_connector_update_edid_property(panel->connector, edid);
  121. if (edid) {
  122. num += drm_add_edid_modes(panel->connector, edid);
  123. kfree(edid);
  124. }
  125. }
  126. /* add hard-coded panel modes */
  127. num += panel_simple_get_fixed_modes(p);
  128. return num;
  129. }
  130. static const struct drm_panel_funcs panel_simple_funcs = {
  131. .disable = panel_simple_disable,
  132. .enable = panel_simple_enable,
  133. .get_modes = panel_simple_get_modes,
  134. };
  135. static int panel_simple_probe(struct device *dev, const struct panel_desc *desc)
  136. {
  137. struct device_node *backlight, *ddc;
  138. struct panel_simple *panel;
  139. int err;
  140. panel = devm_kzalloc(dev, sizeof(*panel), GFP_KERNEL);
  141. if (!panel)
  142. return -ENOMEM;
  143. panel->enabled = false;
  144. panel->desc = desc;
  145. panel->supply = devm_regulator_get(dev, "power");
  146. if (IS_ERR(panel->supply))
  147. return PTR_ERR(panel->supply);
  148. panel->enable_gpio = devm_gpiod_get(dev, "enable");
  149. if (IS_ERR(panel->enable_gpio)) {
  150. err = PTR_ERR(panel->enable_gpio);
  151. if (err != -ENOENT) {
  152. dev_err(dev, "failed to request GPIO: %d\n", err);
  153. return err;
  154. }
  155. panel->enable_gpio = NULL;
  156. } else {
  157. err = gpiod_direction_output(panel->enable_gpio, 0);
  158. if (err < 0) {
  159. dev_err(dev, "failed to setup GPIO: %d\n", err);
  160. return err;
  161. }
  162. }
  163. backlight = of_parse_phandle(dev->of_node, "backlight", 0);
  164. if (backlight) {
  165. panel->backlight = of_find_backlight_by_node(backlight);
  166. of_node_put(backlight);
  167. if (!panel->backlight)
  168. return -EPROBE_DEFER;
  169. }
  170. ddc = of_parse_phandle(dev->of_node, "ddc-i2c-bus", 0);
  171. if (ddc) {
  172. panel->ddc = of_find_i2c_adapter_by_node(ddc);
  173. of_node_put(ddc);
  174. if (!panel->ddc) {
  175. err = -EPROBE_DEFER;
  176. goto free_backlight;
  177. }
  178. }
  179. drm_panel_init(&panel->base);
  180. panel->base.dev = dev;
  181. panel->base.funcs = &panel_simple_funcs;
  182. err = drm_panel_add(&panel->base);
  183. if (err < 0)
  184. goto free_ddc;
  185. dev_set_drvdata(dev, panel);
  186. return 0;
  187. free_ddc:
  188. if (panel->ddc)
  189. put_device(&panel->ddc->dev);
  190. free_backlight:
  191. if (panel->backlight)
  192. put_device(&panel->backlight->dev);
  193. return err;
  194. }
  195. static int panel_simple_remove(struct device *dev)
  196. {
  197. struct panel_simple *panel = dev_get_drvdata(dev);
  198. drm_panel_detach(&panel->base);
  199. drm_panel_remove(&panel->base);
  200. panel_simple_disable(&panel->base);
  201. if (panel->ddc)
  202. put_device(&panel->ddc->dev);
  203. if (panel->backlight)
  204. put_device(&panel->backlight->dev);
  205. return 0;
  206. }
  207. static const struct drm_display_mode auo_b101aw03_mode = {
  208. .clock = 51450,
  209. .hdisplay = 1024,
  210. .hsync_start = 1024 + 156,
  211. .hsync_end = 1024 + 156 + 8,
  212. .htotal = 1024 + 156 + 8 + 156,
  213. .vdisplay = 600,
  214. .vsync_start = 600 + 16,
  215. .vsync_end = 600 + 16 + 6,
  216. .vtotal = 600 + 16 + 6 + 16,
  217. .vrefresh = 60,
  218. };
  219. static const struct panel_desc auo_b101aw03 = {
  220. .modes = &auo_b101aw03_mode,
  221. .num_modes = 1,
  222. .size = {
  223. .width = 223,
  224. .height = 125,
  225. },
  226. };
  227. static const struct drm_display_mode chunghwa_claa101wa01a_mode = {
  228. .clock = 72070,
  229. .hdisplay = 1366,
  230. .hsync_start = 1366 + 58,
  231. .hsync_end = 1366 + 58 + 58,
  232. .htotal = 1366 + 58 + 58 + 58,
  233. .vdisplay = 768,
  234. .vsync_start = 768 + 4,
  235. .vsync_end = 768 + 4 + 4,
  236. .vtotal = 768 + 4 + 4 + 4,
  237. .vrefresh = 60,
  238. };
  239. static const struct panel_desc chunghwa_claa101wa01a = {
  240. .modes = &chunghwa_claa101wa01a_mode,
  241. .num_modes = 1,
  242. .size = {
  243. .width = 220,
  244. .height = 120,
  245. },
  246. };
  247. static const struct drm_display_mode chunghwa_claa101wb01_mode = {
  248. .clock = 69300,
  249. .hdisplay = 1366,
  250. .hsync_start = 1366 + 48,
  251. .hsync_end = 1366 + 48 + 32,
  252. .htotal = 1366 + 48 + 32 + 20,
  253. .vdisplay = 768,
  254. .vsync_start = 768 + 16,
  255. .vsync_end = 768 + 16 + 8,
  256. .vtotal = 768 + 16 + 8 + 16,
  257. .vrefresh = 60,
  258. };
  259. static const struct panel_desc chunghwa_claa101wb01 = {
  260. .modes = &chunghwa_claa101wb01_mode,
  261. .num_modes = 1,
  262. .size = {
  263. .width = 223,
  264. .height = 125,
  265. },
  266. };
  267. static const struct drm_display_mode lg_lp129qe_mode = {
  268. .clock = 285250,
  269. .hdisplay = 2560,
  270. .hsync_start = 2560 + 48,
  271. .hsync_end = 2560 + 48 + 32,
  272. .htotal = 2560 + 48 + 32 + 80,
  273. .vdisplay = 1700,
  274. .vsync_start = 1700 + 3,
  275. .vsync_end = 1700 + 3 + 10,
  276. .vtotal = 1700 + 3 + 10 + 36,
  277. .vrefresh = 60,
  278. };
  279. static const struct panel_desc lg_lp129qe = {
  280. .modes = &lg_lp129qe_mode,
  281. .num_modes = 1,
  282. .size = {
  283. .width = 272,
  284. .height = 181,
  285. },
  286. };
  287. static const struct drm_display_mode samsung_ltn101nt05_mode = {
  288. .clock = 54030,
  289. .hdisplay = 1024,
  290. .hsync_start = 1024 + 24,
  291. .hsync_end = 1024 + 24 + 136,
  292. .htotal = 1024 + 24 + 136 + 160,
  293. .vdisplay = 600,
  294. .vsync_start = 600 + 3,
  295. .vsync_end = 600 + 3 + 6,
  296. .vtotal = 600 + 3 + 6 + 61,
  297. .vrefresh = 60,
  298. };
  299. static const struct panel_desc samsung_ltn101nt05 = {
  300. .modes = &samsung_ltn101nt05_mode,
  301. .num_modes = 1,
  302. .size = {
  303. .width = 1024,
  304. .height = 600,
  305. },
  306. };
  307. static const struct of_device_id platform_of_match[] = {
  308. {
  309. .compatible = "auo,b101aw03",
  310. .data = &auo_b101aw03,
  311. }, {
  312. .compatible = "chunghwa,claa101wa01a",
  313. .data = &chunghwa_claa101wa01a
  314. }, {
  315. .compatible = "chunghwa,claa101wb01",
  316. .data = &chunghwa_claa101wb01
  317. }, {
  318. .compatible = "lg,lp129qe",
  319. .data = &lg_lp129qe,
  320. }, {
  321. .compatible = "samsung,ltn101nt05",
  322. .data = &samsung_ltn101nt05,
  323. }, {
  324. .compatible = "simple-panel",
  325. }, {
  326. /* sentinel */
  327. }
  328. };
  329. MODULE_DEVICE_TABLE(of, platform_of_match);
  330. static int panel_simple_platform_probe(struct platform_device *pdev)
  331. {
  332. const struct of_device_id *id;
  333. id = of_match_node(platform_of_match, pdev->dev.of_node);
  334. if (!id)
  335. return -ENODEV;
  336. return panel_simple_probe(&pdev->dev, id->data);
  337. }
  338. static int panel_simple_platform_remove(struct platform_device *pdev)
  339. {
  340. return panel_simple_remove(&pdev->dev);
  341. }
  342. static struct platform_driver panel_simple_platform_driver = {
  343. .driver = {
  344. .name = "panel-simple",
  345. .owner = THIS_MODULE,
  346. .of_match_table = platform_of_match,
  347. },
  348. .probe = panel_simple_platform_probe,
  349. .remove = panel_simple_platform_remove,
  350. };
  351. struct panel_desc_dsi {
  352. struct panel_desc desc;
  353. unsigned long flags;
  354. enum mipi_dsi_pixel_format format;
  355. unsigned int lanes;
  356. };
  357. static const struct drm_display_mode lg_ld070wx3_sl01_mode = {
  358. .clock = 71000,
  359. .hdisplay = 800,
  360. .hsync_start = 800 + 32,
  361. .hsync_end = 800 + 32 + 1,
  362. .htotal = 800 + 32 + 1 + 57,
  363. .vdisplay = 1280,
  364. .vsync_start = 1280 + 28,
  365. .vsync_end = 1280 + 28 + 1,
  366. .vtotal = 1280 + 28 + 1 + 14,
  367. .vrefresh = 60,
  368. };
  369. static const struct panel_desc_dsi lg_ld070wx3_sl01 = {
  370. .desc = {
  371. .modes = &lg_ld070wx3_sl01_mode,
  372. .num_modes = 1,
  373. .size = {
  374. .width = 94,
  375. .height = 151,
  376. },
  377. },
  378. .flags = MIPI_DSI_MODE_VIDEO,
  379. .format = MIPI_DSI_FMT_RGB888,
  380. .lanes = 4,
  381. };
  382. static const struct drm_display_mode lg_lh500wx1_sd03_mode = {
  383. .clock = 67000,
  384. .hdisplay = 720,
  385. .hsync_start = 720 + 12,
  386. .hsync_end = 720 + 12 + 4,
  387. .htotal = 720 + 12 + 4 + 112,
  388. .vdisplay = 1280,
  389. .vsync_start = 1280 + 8,
  390. .vsync_end = 1280 + 8 + 4,
  391. .vtotal = 1280 + 8 + 4 + 12,
  392. .vrefresh = 60,
  393. };
  394. static const struct panel_desc_dsi lg_lh500wx1_sd03 = {
  395. .desc = {
  396. .modes = &lg_lh500wx1_sd03_mode,
  397. .num_modes = 1,
  398. .size = {
  399. .width = 62,
  400. .height = 110,
  401. },
  402. },
  403. .flags = MIPI_DSI_MODE_VIDEO,
  404. .format = MIPI_DSI_FMT_RGB888,
  405. .lanes = 4,
  406. };
  407. static const struct drm_display_mode panasonic_vvx10f004b00_mode = {
  408. .clock = 157200,
  409. .hdisplay = 1920,
  410. .hsync_start = 1920 + 154,
  411. .hsync_end = 1920 + 154 + 16,
  412. .htotal = 1920 + 154 + 16 + 32,
  413. .vdisplay = 1200,
  414. .vsync_start = 1200 + 17,
  415. .vsync_end = 1200 + 17 + 2,
  416. .vtotal = 1200 + 17 + 2 + 16,
  417. .vrefresh = 60,
  418. };
  419. static const struct panel_desc_dsi panasonic_vvx10f004b00 = {
  420. .desc = {
  421. .modes = &panasonic_vvx10f004b00_mode,
  422. .num_modes = 1,
  423. .size = {
  424. .width = 217,
  425. .height = 136,
  426. },
  427. },
  428. .flags = MIPI_DSI_MODE_VIDEO | MIPI_DSI_MODE_VIDEO_SYNC_PULSE,
  429. .format = MIPI_DSI_FMT_RGB888,
  430. .lanes = 4,
  431. };
  432. static const struct of_device_id dsi_of_match[] = {
  433. {
  434. .compatible = "lg,ld070wx3-sl01",
  435. .data = &lg_ld070wx3_sl01
  436. }, {
  437. .compatible = "lg,lh500wx1-sd03",
  438. .data = &lg_lh500wx1_sd03
  439. }, {
  440. .compatible = "panasonic,vvx10f004b00",
  441. .data = &panasonic_vvx10f004b00
  442. }, {
  443. /* sentinel */
  444. }
  445. };
  446. MODULE_DEVICE_TABLE(of, dsi_of_match);
  447. static int panel_simple_dsi_probe(struct mipi_dsi_device *dsi)
  448. {
  449. const struct panel_desc_dsi *desc;
  450. const struct of_device_id *id;
  451. int err;
  452. id = of_match_node(dsi_of_match, dsi->dev.of_node);
  453. if (!id)
  454. return -ENODEV;
  455. desc = id->data;
  456. err = panel_simple_probe(&dsi->dev, &desc->desc);
  457. if (err < 0)
  458. return err;
  459. dsi->mode_flags = desc->flags;
  460. dsi->format = desc->format;
  461. dsi->lanes = desc->lanes;
  462. return mipi_dsi_attach(dsi);
  463. }
  464. static int panel_simple_dsi_remove(struct mipi_dsi_device *dsi)
  465. {
  466. int err;
  467. err = mipi_dsi_detach(dsi);
  468. if (err < 0)
  469. dev_err(&dsi->dev, "failed to detach from DSI host: %d\n", err);
  470. return panel_simple_remove(&dsi->dev);
  471. }
  472. static struct mipi_dsi_driver panel_simple_dsi_driver = {
  473. .driver = {
  474. .name = "panel-simple-dsi",
  475. .owner = THIS_MODULE,
  476. .of_match_table = dsi_of_match,
  477. },
  478. .probe = panel_simple_dsi_probe,
  479. .remove = panel_simple_dsi_remove,
  480. };
  481. static int __init panel_simple_init(void)
  482. {
  483. int err;
  484. err = platform_driver_register(&panel_simple_platform_driver);
  485. if (err < 0)
  486. return err;
  487. if (IS_ENABLED(CONFIG_DRM_MIPI_DSI)) {
  488. err = mipi_dsi_driver_register(&panel_simple_dsi_driver);
  489. if (err < 0)
  490. return err;
  491. }
  492. return 0;
  493. }
  494. module_init(panel_simple_init);
  495. static void __exit panel_simple_exit(void)
  496. {
  497. if (IS_ENABLED(CONFIG_DRM_MIPI_DSI))
  498. mipi_dsi_driver_unregister(&panel_simple_dsi_driver);
  499. platform_driver_unregister(&panel_simple_platform_driver);
  500. }
  501. module_exit(panel_simple_exit);
  502. MODULE_AUTHOR("Thierry Reding <treding@nvidia.com>");
  503. MODULE_DESCRIPTION("DRM Driver for Simple Panels");
  504. MODULE_LICENSE("GPL and additional rights");