panel-simple.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548
  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.h>
  25. #include <linux/module.h>
  26. #include <linux/of_gpio.h>
  27. #include <linux/of_platform.h>
  28. #include <linux/platform_device.h>
  29. #include <linux/regulator/consumer.h>
  30. #include <drm/drmP.h>
  31. #include <drm/drm_crtc.h>
  32. #include <drm/drm_mipi_dsi.h>
  33. #include <drm/drm_panel.h>
  34. struct panel_desc {
  35. const struct drm_display_mode *modes;
  36. unsigned int num_modes;
  37. struct {
  38. unsigned int width;
  39. unsigned int height;
  40. } size;
  41. };
  42. /* TODO: convert to gpiod_*() API once it's been merged */
  43. #define GPIO_ACTIVE_LOW (1 << 0)
  44. struct panel_simple {
  45. struct drm_panel base;
  46. bool enabled;
  47. const struct panel_desc *desc;
  48. struct backlight_device *backlight;
  49. struct regulator *supply;
  50. struct i2c_adapter *ddc;
  51. unsigned long enable_gpio_flags;
  52. int enable_gpio;
  53. };
  54. static inline struct panel_simple *to_panel_simple(struct drm_panel *panel)
  55. {
  56. return container_of(panel, struct panel_simple, base);
  57. }
  58. static int panel_simple_get_fixed_modes(struct panel_simple *panel)
  59. {
  60. struct drm_connector *connector = panel->base.connector;
  61. struct drm_device *drm = panel->base.drm;
  62. struct drm_display_mode *mode;
  63. unsigned int i, num = 0;
  64. if (!panel->desc)
  65. return 0;
  66. for (i = 0; i < panel->desc->num_modes; i++) {
  67. const struct drm_display_mode *m = &panel->desc->modes[i];
  68. mode = drm_mode_duplicate(drm, m);
  69. if (!mode) {
  70. dev_err(drm->dev, "failed to add mode %ux%u@%u\n",
  71. m->hdisplay, m->vdisplay, m->vrefresh);
  72. continue;
  73. }
  74. drm_mode_set_name(mode);
  75. drm_mode_probed_add(connector, mode);
  76. num++;
  77. }
  78. connector->display_info.width_mm = panel->desc->size.width;
  79. connector->display_info.height_mm = panel->desc->size.height;
  80. return num;
  81. }
  82. static int panel_simple_disable(struct drm_panel *panel)
  83. {
  84. struct panel_simple *p = to_panel_simple(panel);
  85. if (!p->enabled)
  86. return 0;
  87. if (p->backlight) {
  88. p->backlight->props.power = FB_BLANK_POWERDOWN;
  89. backlight_update_status(p->backlight);
  90. }
  91. if (gpio_is_valid(p->enable_gpio)) {
  92. if (p->enable_gpio_flags & GPIO_ACTIVE_LOW)
  93. gpio_set_value(p->enable_gpio, 1);
  94. else
  95. gpio_set_value(p->enable_gpio, 0);
  96. }
  97. regulator_disable(p->supply);
  98. p->enabled = false;
  99. return 0;
  100. }
  101. static int panel_simple_enable(struct drm_panel *panel)
  102. {
  103. struct panel_simple *p = to_panel_simple(panel);
  104. int err;
  105. if (p->enabled)
  106. return 0;
  107. err = regulator_enable(p->supply);
  108. if (err < 0) {
  109. dev_err(panel->dev, "failed to enable supply: %d\n", err);
  110. return err;
  111. }
  112. if (gpio_is_valid(p->enable_gpio)) {
  113. if (p->enable_gpio_flags & GPIO_ACTIVE_LOW)
  114. gpio_set_value(p->enable_gpio, 0);
  115. else
  116. gpio_set_value(p->enable_gpio, 1);
  117. }
  118. if (p->backlight) {
  119. p->backlight->props.power = FB_BLANK_UNBLANK;
  120. backlight_update_status(p->backlight);
  121. }
  122. p->enabled = true;
  123. return 0;
  124. }
  125. static int panel_simple_get_modes(struct drm_panel *panel)
  126. {
  127. struct panel_simple *p = to_panel_simple(panel);
  128. int num = 0;
  129. /* probe EDID if a DDC bus is available */
  130. if (p->ddc) {
  131. struct edid *edid = drm_get_edid(panel->connector, p->ddc);
  132. drm_mode_connector_update_edid_property(panel->connector, edid);
  133. if (edid) {
  134. num += drm_add_edid_modes(panel->connector, edid);
  135. kfree(edid);
  136. }
  137. }
  138. /* add hard-coded panel modes */
  139. num += panel_simple_get_fixed_modes(p);
  140. return num;
  141. }
  142. static const struct drm_panel_funcs panel_simple_funcs = {
  143. .disable = panel_simple_disable,
  144. .enable = panel_simple_enable,
  145. .get_modes = panel_simple_get_modes,
  146. };
  147. static int panel_simple_probe(struct device *dev, const struct panel_desc *desc)
  148. {
  149. struct device_node *backlight, *ddc;
  150. struct panel_simple *panel;
  151. enum of_gpio_flags flags;
  152. int err;
  153. panel = devm_kzalloc(dev, sizeof(*panel), GFP_KERNEL);
  154. if (!panel)
  155. return -ENOMEM;
  156. panel->enabled = false;
  157. panel->desc = desc;
  158. panel->supply = devm_regulator_get(dev, "power");
  159. if (IS_ERR(panel->supply))
  160. return PTR_ERR(panel->supply);
  161. panel->enable_gpio = of_get_named_gpio_flags(dev->of_node,
  162. "enable-gpios", 0,
  163. &flags);
  164. if (gpio_is_valid(panel->enable_gpio)) {
  165. unsigned int value;
  166. if (flags & OF_GPIO_ACTIVE_LOW)
  167. panel->enable_gpio_flags |= GPIO_ACTIVE_LOW;
  168. err = gpio_request(panel->enable_gpio, "enable");
  169. if (err < 0) {
  170. dev_err(dev, "failed to request GPIO#%u: %d\n",
  171. panel->enable_gpio, err);
  172. return err;
  173. }
  174. value = (panel->enable_gpio_flags & GPIO_ACTIVE_LOW) != 0;
  175. err = gpio_direction_output(panel->enable_gpio, value);
  176. if (err < 0) {
  177. dev_err(dev, "failed to setup GPIO%u: %d\n",
  178. panel->enable_gpio, err);
  179. goto free_gpio;
  180. }
  181. }
  182. backlight = of_parse_phandle(dev->of_node, "backlight", 0);
  183. if (backlight) {
  184. panel->backlight = of_find_backlight_by_node(backlight);
  185. of_node_put(backlight);
  186. if (!panel->backlight) {
  187. err = -EPROBE_DEFER;
  188. goto free_gpio;
  189. }
  190. }
  191. ddc = of_parse_phandle(dev->of_node, "ddc-i2c-bus", 0);
  192. if (ddc) {
  193. panel->ddc = of_find_i2c_adapter_by_node(ddc);
  194. of_node_put(ddc);
  195. if (!panel->ddc) {
  196. err = -EPROBE_DEFER;
  197. goto free_backlight;
  198. }
  199. }
  200. drm_panel_init(&panel->base);
  201. panel->base.dev = dev;
  202. panel->base.funcs = &panel_simple_funcs;
  203. err = drm_panel_add(&panel->base);
  204. if (err < 0)
  205. goto free_ddc;
  206. dev_set_drvdata(dev, panel);
  207. return 0;
  208. free_ddc:
  209. if (panel->ddc)
  210. put_device(&panel->ddc->dev);
  211. free_backlight:
  212. if (panel->backlight)
  213. put_device(&panel->backlight->dev);
  214. free_gpio:
  215. if (gpio_is_valid(panel->enable_gpio))
  216. gpio_free(panel->enable_gpio);
  217. return err;
  218. }
  219. static int panel_simple_remove(struct device *dev)
  220. {
  221. struct panel_simple *panel = dev_get_drvdata(dev);
  222. drm_panel_detach(&panel->base);
  223. drm_panel_remove(&panel->base);
  224. panel_simple_disable(&panel->base);
  225. if (panel->ddc)
  226. put_device(&panel->ddc->dev);
  227. if (panel->backlight)
  228. put_device(&panel->backlight->dev);
  229. if (gpio_is_valid(panel->enable_gpio))
  230. gpio_free(panel->enable_gpio);
  231. regulator_disable(panel->supply);
  232. return 0;
  233. }
  234. static const struct drm_display_mode auo_b101aw03_mode = {
  235. .clock = 51450,
  236. .hdisplay = 1024,
  237. .hsync_start = 1024 + 156,
  238. .hsync_end = 1024 + 156 + 8,
  239. .htotal = 1024 + 156 + 8 + 156,
  240. .vdisplay = 600,
  241. .vsync_start = 600 + 16,
  242. .vsync_end = 600 + 16 + 6,
  243. .vtotal = 600 + 16 + 6 + 16,
  244. .vrefresh = 60,
  245. };
  246. static const struct panel_desc auo_b101aw03 = {
  247. .modes = &auo_b101aw03_mode,
  248. .num_modes = 1,
  249. .size = {
  250. .width = 223,
  251. .height = 125,
  252. },
  253. };
  254. static const struct drm_display_mode chunghwa_claa101wa01a_mode = {
  255. .clock = 72070,
  256. .hdisplay = 1366,
  257. .hsync_start = 1366 + 58,
  258. .hsync_end = 1366 + 58 + 58,
  259. .htotal = 1366 + 58 + 58 + 58,
  260. .vdisplay = 768,
  261. .vsync_start = 768 + 4,
  262. .vsync_end = 768 + 4 + 4,
  263. .vtotal = 768 + 4 + 4 + 4,
  264. .vrefresh = 60,
  265. };
  266. static const struct panel_desc chunghwa_claa101wa01a = {
  267. .modes = &chunghwa_claa101wa01a_mode,
  268. .num_modes = 1,
  269. .size = {
  270. .width = 220,
  271. .height = 120,
  272. },
  273. };
  274. static const struct drm_display_mode chunghwa_claa101wb01_mode = {
  275. .clock = 69300,
  276. .hdisplay = 1366,
  277. .hsync_start = 1366 + 48,
  278. .hsync_end = 1366 + 48 + 32,
  279. .htotal = 1366 + 48 + 32 + 20,
  280. .vdisplay = 768,
  281. .vsync_start = 768 + 16,
  282. .vsync_end = 768 + 16 + 8,
  283. .vtotal = 768 + 16 + 8 + 16,
  284. .vrefresh = 60,
  285. };
  286. static const struct panel_desc chunghwa_claa101wb01 = {
  287. .modes = &chunghwa_claa101wb01_mode,
  288. .num_modes = 1,
  289. .size = {
  290. .width = 223,
  291. .height = 125,
  292. },
  293. };
  294. static const struct drm_display_mode samsung_ltn101nt05_mode = {
  295. .clock = 54030,
  296. .hdisplay = 1024,
  297. .hsync_start = 1024 + 24,
  298. .hsync_end = 1024 + 24 + 136,
  299. .htotal = 1024 + 24 + 136 + 160,
  300. .vdisplay = 600,
  301. .vsync_start = 600 + 3,
  302. .vsync_end = 600 + 3 + 6,
  303. .vtotal = 600 + 3 + 6 + 61,
  304. .vrefresh = 60,
  305. };
  306. static const struct panel_desc samsung_ltn101nt05 = {
  307. .modes = &samsung_ltn101nt05_mode,
  308. .num_modes = 1,
  309. .size = {
  310. .width = 1024,
  311. .height = 600,
  312. },
  313. };
  314. static const struct of_device_id platform_of_match[] = {
  315. {
  316. .compatible = "auo,b101aw03",
  317. .data = &auo_b101aw03,
  318. }, {
  319. .compatible = "chunghwa,claa101wa01a",
  320. .data = &chunghwa_claa101wa01a
  321. }, {
  322. .compatible = "chunghwa,claa101wb01",
  323. .data = &chunghwa_claa101wb01
  324. }, {
  325. .compatible = "samsung,ltn101nt05",
  326. .data = &samsung_ltn101nt05,
  327. }, {
  328. .compatible = "simple-panel",
  329. }, {
  330. /* sentinel */
  331. }
  332. };
  333. MODULE_DEVICE_TABLE(of, platform_of_match);
  334. static int panel_simple_platform_probe(struct platform_device *pdev)
  335. {
  336. const struct of_device_id *id;
  337. id = of_match_node(platform_of_match, pdev->dev.of_node);
  338. if (!id)
  339. return -ENODEV;
  340. return panel_simple_probe(&pdev->dev, id->data);
  341. }
  342. static int panel_simple_platform_remove(struct platform_device *pdev)
  343. {
  344. return panel_simple_remove(&pdev->dev);
  345. }
  346. static struct platform_driver panel_simple_platform_driver = {
  347. .driver = {
  348. .name = "panel-simple",
  349. .owner = THIS_MODULE,
  350. .of_match_table = platform_of_match,
  351. },
  352. .probe = panel_simple_platform_probe,
  353. .remove = panel_simple_platform_remove,
  354. };
  355. struct panel_desc_dsi {
  356. struct panel_desc desc;
  357. enum mipi_dsi_pixel_format format;
  358. unsigned int lanes;
  359. };
  360. static const struct drm_display_mode panasonic_vvx10f004b00_mode = {
  361. .clock = 157200,
  362. .hdisplay = 1920,
  363. .hsync_start = 1920 + 154,
  364. .hsync_end = 1920 + 154 + 16,
  365. .htotal = 1920 + 154 + 16 + 32,
  366. .vdisplay = 1200,
  367. .vsync_start = 1200 + 17,
  368. .vsync_end = 1200 + 17 + 2,
  369. .vtotal = 1200 + 17 + 2 + 16,
  370. .vrefresh = 60,
  371. };
  372. static const struct panel_desc_dsi panasonic_vvx10f004b00 = {
  373. .desc = {
  374. .modes = &panasonic_vvx10f004b00_mode,
  375. .num_modes = 1,
  376. .size = {
  377. .width = 217,
  378. .height = 136,
  379. },
  380. },
  381. .format = MIPI_DSI_FMT_RGB888,
  382. .lanes = 4,
  383. };
  384. static const struct of_device_id dsi_of_match[] = {
  385. {
  386. .compatible = "panasonic,vvx10f004b00",
  387. .data = &panasonic_vvx10f004b00
  388. }, {
  389. /* sentinel */
  390. }
  391. };
  392. MODULE_DEVICE_TABLE(of, dsi_of_match);
  393. static int panel_simple_dsi_probe(struct mipi_dsi_device *dsi)
  394. {
  395. const struct panel_desc_dsi *desc;
  396. const struct of_device_id *id;
  397. int err;
  398. id = of_match_node(dsi_of_match, dsi->dev.of_node);
  399. if (!id)
  400. return -ENODEV;
  401. desc = id->data;
  402. err = panel_simple_probe(&dsi->dev, &desc->desc);
  403. if (err < 0)
  404. return err;
  405. dsi->format = desc->format;
  406. dsi->lanes = desc->lanes;
  407. return mipi_dsi_attach(dsi);
  408. }
  409. static int panel_simple_dsi_remove(struct mipi_dsi_device *dsi)
  410. {
  411. int err;
  412. err = mipi_dsi_detach(dsi);
  413. if (err < 0)
  414. dev_err(&dsi->dev, "failed to detach from DSI host: %d\n", err);
  415. return panel_simple_remove(&dsi->dev);
  416. }
  417. static struct mipi_dsi_driver panel_simple_dsi_driver = {
  418. .driver = {
  419. .name = "panel-simple-dsi",
  420. .owner = THIS_MODULE,
  421. .of_match_table = dsi_of_match,
  422. },
  423. .probe = panel_simple_dsi_probe,
  424. .remove = panel_simple_dsi_remove,
  425. };
  426. static int __init panel_simple_init(void)
  427. {
  428. int err;
  429. err = platform_driver_register(&panel_simple_platform_driver);
  430. if (err < 0)
  431. return err;
  432. if (IS_ENABLED(CONFIG_DRM_MIPI_DSI)) {
  433. err = mipi_dsi_driver_register(&panel_simple_dsi_driver);
  434. if (err < 0)
  435. return err;
  436. }
  437. return 0;
  438. }
  439. module_init(panel_simple_init);
  440. static void __exit panel_simple_exit(void)
  441. {
  442. if (IS_ENABLED(CONFIG_DRM_MIPI_DSI))
  443. mipi_dsi_driver_unregister(&panel_simple_dsi_driver);
  444. platform_driver_unregister(&panel_simple_platform_driver);
  445. }
  446. module_exit(panel_simple_exit);
  447. MODULE_AUTHOR("Thierry Reding <treding@nvidia.com>");
  448. MODULE_DESCRIPTION("DRM Driver for Simple Panels");
  449. MODULE_LICENSE("GPL and additional rights");