tilcdc_panel.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  1. /*
  2. * Copyright (C) 2012 Texas Instruments
  3. * Author: Rob Clark <robdclark@gmail.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License version 2 as published by
  7. * the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  12. * more details.
  13. *
  14. * You should have received a copy of the GNU General Public License along with
  15. * this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include <linux/pinctrl/pinmux.h>
  18. #include <linux/pinctrl/consumer.h>
  19. #include <linux/backlight.h>
  20. #include <linux/gpio/consumer.h>
  21. #include <video/display_timing.h>
  22. #include <video/of_display_timing.h>
  23. #include <video/videomode.h>
  24. #include "tilcdc_drv.h"
  25. struct panel_module {
  26. struct tilcdc_module base;
  27. struct tilcdc_panel_info *info;
  28. struct display_timings *timings;
  29. struct backlight_device *backlight;
  30. struct gpio_desc *enable_gpio;
  31. };
  32. #define to_panel_module(x) container_of(x, struct panel_module, base)
  33. /*
  34. * Encoder:
  35. */
  36. struct panel_encoder {
  37. struct drm_encoder base;
  38. struct panel_module *mod;
  39. };
  40. #define to_panel_encoder(x) container_of(x, struct panel_encoder, base)
  41. static void panel_encoder_destroy(struct drm_encoder *encoder)
  42. {
  43. struct panel_encoder *panel_encoder = to_panel_encoder(encoder);
  44. drm_encoder_cleanup(encoder);
  45. kfree(panel_encoder);
  46. }
  47. static void panel_encoder_dpms(struct drm_encoder *encoder, int mode)
  48. {
  49. struct panel_encoder *panel_encoder = to_panel_encoder(encoder);
  50. struct backlight_device *backlight = panel_encoder->mod->backlight;
  51. struct gpio_desc *gpio = panel_encoder->mod->enable_gpio;
  52. if (backlight) {
  53. backlight->props.power = mode == DRM_MODE_DPMS_ON ?
  54. FB_BLANK_UNBLANK : FB_BLANK_POWERDOWN;
  55. backlight_update_status(backlight);
  56. }
  57. if (gpio)
  58. gpiod_set_value_cansleep(gpio,
  59. mode == DRM_MODE_DPMS_ON ? 1 : 0);
  60. }
  61. static bool panel_encoder_mode_fixup(struct drm_encoder *encoder,
  62. const struct drm_display_mode *mode,
  63. struct drm_display_mode *adjusted_mode)
  64. {
  65. /* nothing needed */
  66. return true;
  67. }
  68. static void panel_encoder_prepare(struct drm_encoder *encoder)
  69. {
  70. struct panel_encoder *panel_encoder = to_panel_encoder(encoder);
  71. panel_encoder_dpms(encoder, DRM_MODE_DPMS_OFF);
  72. tilcdc_crtc_set_panel_info(encoder->crtc, panel_encoder->mod->info);
  73. }
  74. static void panel_encoder_commit(struct drm_encoder *encoder)
  75. {
  76. panel_encoder_dpms(encoder, DRM_MODE_DPMS_ON);
  77. }
  78. static void panel_encoder_mode_set(struct drm_encoder *encoder,
  79. struct drm_display_mode *mode,
  80. struct drm_display_mode *adjusted_mode)
  81. {
  82. /* nothing needed */
  83. }
  84. static const struct drm_encoder_funcs panel_encoder_funcs = {
  85. .destroy = panel_encoder_destroy,
  86. };
  87. static const struct drm_encoder_helper_funcs panel_encoder_helper_funcs = {
  88. .dpms = panel_encoder_dpms,
  89. .mode_fixup = panel_encoder_mode_fixup,
  90. .prepare = panel_encoder_prepare,
  91. .commit = panel_encoder_commit,
  92. .mode_set = panel_encoder_mode_set,
  93. };
  94. static struct drm_encoder *panel_encoder_create(struct drm_device *dev,
  95. struct panel_module *mod)
  96. {
  97. struct panel_encoder *panel_encoder;
  98. struct drm_encoder *encoder;
  99. int ret;
  100. panel_encoder = kzalloc(sizeof(*panel_encoder), GFP_KERNEL);
  101. if (!panel_encoder) {
  102. dev_err(dev->dev, "allocation failed\n");
  103. return NULL;
  104. }
  105. panel_encoder->mod = mod;
  106. encoder = &panel_encoder->base;
  107. encoder->possible_crtcs = 1;
  108. ret = drm_encoder_init(dev, encoder, &panel_encoder_funcs,
  109. DRM_MODE_ENCODER_LVDS);
  110. if (ret < 0)
  111. goto fail;
  112. drm_encoder_helper_add(encoder, &panel_encoder_helper_funcs);
  113. return encoder;
  114. fail:
  115. panel_encoder_destroy(encoder);
  116. return NULL;
  117. }
  118. /*
  119. * Connector:
  120. */
  121. struct panel_connector {
  122. struct drm_connector base;
  123. struct drm_encoder *encoder; /* our connected encoder */
  124. struct panel_module *mod;
  125. };
  126. #define to_panel_connector(x) container_of(x, struct panel_connector, base)
  127. static void panel_connector_destroy(struct drm_connector *connector)
  128. {
  129. struct panel_connector *panel_connector = to_panel_connector(connector);
  130. drm_connector_unregister(connector);
  131. drm_connector_cleanup(connector);
  132. kfree(panel_connector);
  133. }
  134. static enum drm_connector_status panel_connector_detect(
  135. struct drm_connector *connector,
  136. bool force)
  137. {
  138. return connector_status_connected;
  139. }
  140. static int panel_connector_get_modes(struct drm_connector *connector)
  141. {
  142. struct drm_device *dev = connector->dev;
  143. struct panel_connector *panel_connector = to_panel_connector(connector);
  144. struct display_timings *timings = panel_connector->mod->timings;
  145. int i;
  146. for (i = 0; i < timings->num_timings; i++) {
  147. struct drm_display_mode *mode = drm_mode_create(dev);
  148. struct videomode vm;
  149. if (videomode_from_timings(timings, &vm, i))
  150. break;
  151. drm_display_mode_from_videomode(&vm, mode);
  152. mode->type = DRM_MODE_TYPE_DRIVER;
  153. if (timings->native_mode == i)
  154. mode->type |= DRM_MODE_TYPE_PREFERRED;
  155. drm_mode_set_name(mode);
  156. drm_mode_probed_add(connector, mode);
  157. }
  158. return i;
  159. }
  160. static int panel_connector_mode_valid(struct drm_connector *connector,
  161. struct drm_display_mode *mode)
  162. {
  163. struct tilcdc_drm_private *priv = connector->dev->dev_private;
  164. /* our only constraints are what the crtc can generate: */
  165. return tilcdc_crtc_mode_valid(priv->crtc, mode);
  166. }
  167. static struct drm_encoder *panel_connector_best_encoder(
  168. struct drm_connector *connector)
  169. {
  170. struct panel_connector *panel_connector = to_panel_connector(connector);
  171. return panel_connector->encoder;
  172. }
  173. static const struct drm_connector_funcs panel_connector_funcs = {
  174. .destroy = panel_connector_destroy,
  175. .dpms = drm_helper_connector_dpms,
  176. .detect = panel_connector_detect,
  177. .fill_modes = drm_helper_probe_single_connector_modes,
  178. };
  179. static const struct drm_connector_helper_funcs panel_connector_helper_funcs = {
  180. .get_modes = panel_connector_get_modes,
  181. .mode_valid = panel_connector_mode_valid,
  182. .best_encoder = panel_connector_best_encoder,
  183. };
  184. static struct drm_connector *panel_connector_create(struct drm_device *dev,
  185. struct panel_module *mod, struct drm_encoder *encoder)
  186. {
  187. struct panel_connector *panel_connector;
  188. struct drm_connector *connector;
  189. int ret;
  190. panel_connector = kzalloc(sizeof(*panel_connector), GFP_KERNEL);
  191. if (!panel_connector) {
  192. dev_err(dev->dev, "allocation failed\n");
  193. return NULL;
  194. }
  195. panel_connector->encoder = encoder;
  196. panel_connector->mod = mod;
  197. connector = &panel_connector->base;
  198. drm_connector_init(dev, connector, &panel_connector_funcs,
  199. DRM_MODE_CONNECTOR_LVDS);
  200. drm_connector_helper_add(connector, &panel_connector_helper_funcs);
  201. connector->interlace_allowed = 0;
  202. connector->doublescan_allowed = 0;
  203. ret = drm_mode_connector_attach_encoder(connector, encoder);
  204. if (ret)
  205. goto fail;
  206. drm_connector_register(connector);
  207. return connector;
  208. fail:
  209. panel_connector_destroy(connector);
  210. return NULL;
  211. }
  212. /*
  213. * Module:
  214. */
  215. static int panel_modeset_init(struct tilcdc_module *mod, struct drm_device *dev)
  216. {
  217. struct panel_module *panel_mod = to_panel_module(mod);
  218. struct tilcdc_drm_private *priv = dev->dev_private;
  219. struct drm_encoder *encoder;
  220. struct drm_connector *connector;
  221. encoder = panel_encoder_create(dev, panel_mod);
  222. if (!encoder)
  223. return -ENOMEM;
  224. connector = panel_connector_create(dev, panel_mod, encoder);
  225. if (!connector)
  226. return -ENOMEM;
  227. priv->encoders[priv->num_encoders++] = encoder;
  228. priv->connectors[priv->num_connectors++] = connector;
  229. return 0;
  230. }
  231. static const struct tilcdc_module_ops panel_module_ops = {
  232. .modeset_init = panel_modeset_init,
  233. };
  234. /*
  235. * Device:
  236. */
  237. /* maybe move this somewhere common if it is needed by other outputs? */
  238. static struct tilcdc_panel_info *of_get_panel_info(struct device_node *np)
  239. {
  240. struct device_node *info_np;
  241. struct tilcdc_panel_info *info;
  242. int ret = 0;
  243. if (!np) {
  244. pr_err("%s: no devicenode given\n", __func__);
  245. return NULL;
  246. }
  247. info_np = of_get_child_by_name(np, "panel-info");
  248. if (!info_np) {
  249. pr_err("%s: could not find panel-info node\n", __func__);
  250. return NULL;
  251. }
  252. info = kzalloc(sizeof(*info), GFP_KERNEL);
  253. if (!info) {
  254. pr_err("%s: allocation failed\n", __func__);
  255. of_node_put(info_np);
  256. return NULL;
  257. }
  258. ret |= of_property_read_u32(info_np, "ac-bias", &info->ac_bias);
  259. ret |= of_property_read_u32(info_np, "ac-bias-intrpt", &info->ac_bias_intrpt);
  260. ret |= of_property_read_u32(info_np, "dma-burst-sz", &info->dma_burst_sz);
  261. ret |= of_property_read_u32(info_np, "bpp", &info->bpp);
  262. ret |= of_property_read_u32(info_np, "fdd", &info->fdd);
  263. ret |= of_property_read_u32(info_np, "sync-edge", &info->sync_edge);
  264. ret |= of_property_read_u32(info_np, "sync-ctrl", &info->sync_ctrl);
  265. ret |= of_property_read_u32(info_np, "raster-order", &info->raster_order);
  266. ret |= of_property_read_u32(info_np, "fifo-th", &info->fifo_th);
  267. /* optional: */
  268. info->tft_alt_mode = of_property_read_bool(info_np, "tft-alt-mode");
  269. info->invert_pxl_clk = of_property_read_bool(info_np, "invert-pxl-clk");
  270. if (ret) {
  271. pr_err("%s: error reading panel-info properties\n", __func__);
  272. kfree(info);
  273. of_node_put(info_np);
  274. return NULL;
  275. }
  276. of_node_put(info_np);
  277. return info;
  278. }
  279. static int panel_probe(struct platform_device *pdev)
  280. {
  281. struct device_node *bl_node, *node = pdev->dev.of_node;
  282. struct panel_module *panel_mod;
  283. struct tilcdc_module *mod;
  284. struct pinctrl *pinctrl;
  285. int ret;
  286. /* bail out early if no DT data: */
  287. if (!node) {
  288. dev_err(&pdev->dev, "device-tree data is missing\n");
  289. return -ENXIO;
  290. }
  291. panel_mod = devm_kzalloc(&pdev->dev, sizeof(*panel_mod), GFP_KERNEL);
  292. if (!panel_mod)
  293. return -ENOMEM;
  294. bl_node = of_parse_phandle(node, "backlight", 0);
  295. if (bl_node) {
  296. panel_mod->backlight = of_find_backlight_by_node(bl_node);
  297. of_node_put(bl_node);
  298. if (!panel_mod->backlight)
  299. return -EPROBE_DEFER;
  300. dev_info(&pdev->dev, "found backlight\n");
  301. }
  302. panel_mod->enable_gpio = devm_gpiod_get_optional(&pdev->dev, "enable",
  303. GPIOD_OUT_LOW);
  304. if (IS_ERR(panel_mod->enable_gpio)) {
  305. ret = PTR_ERR(panel_mod->enable_gpio);
  306. dev_err(&pdev->dev, "failed to request enable GPIO\n");
  307. goto fail_backlight;
  308. }
  309. if (panel_mod->enable_gpio)
  310. dev_info(&pdev->dev, "found enable GPIO\n");
  311. mod = &panel_mod->base;
  312. pdev->dev.platform_data = mod;
  313. tilcdc_module_init(mod, "panel", &panel_module_ops);
  314. pinctrl = devm_pinctrl_get_select_default(&pdev->dev);
  315. if (IS_ERR(pinctrl))
  316. dev_warn(&pdev->dev, "pins are not configured\n");
  317. panel_mod->timings = of_get_display_timings(node);
  318. if (!panel_mod->timings) {
  319. dev_err(&pdev->dev, "could not get panel timings\n");
  320. ret = -EINVAL;
  321. goto fail_free;
  322. }
  323. panel_mod->info = of_get_panel_info(node);
  324. if (!panel_mod->info) {
  325. dev_err(&pdev->dev, "could not get panel info\n");
  326. ret = -EINVAL;
  327. goto fail_timings;
  328. }
  329. mod->preferred_bpp = panel_mod->info->bpp;
  330. return 0;
  331. fail_timings:
  332. display_timings_release(panel_mod->timings);
  333. fail_free:
  334. tilcdc_module_cleanup(mod);
  335. fail_backlight:
  336. if (panel_mod->backlight)
  337. put_device(&panel_mod->backlight->dev);
  338. return ret;
  339. }
  340. static int panel_remove(struct platform_device *pdev)
  341. {
  342. struct tilcdc_module *mod = dev_get_platdata(&pdev->dev);
  343. struct panel_module *panel_mod = to_panel_module(mod);
  344. struct backlight_device *backlight = panel_mod->backlight;
  345. if (backlight)
  346. put_device(&backlight->dev);
  347. display_timings_release(panel_mod->timings);
  348. tilcdc_module_cleanup(mod);
  349. kfree(panel_mod->info);
  350. return 0;
  351. }
  352. static struct of_device_id panel_of_match[] = {
  353. { .compatible = "ti,tilcdc,panel", },
  354. { },
  355. };
  356. struct platform_driver panel_driver = {
  357. .probe = panel_probe,
  358. .remove = panel_remove,
  359. .driver = {
  360. .owner = THIS_MODULE,
  361. .name = "panel",
  362. .of_match_table = panel_of_match,
  363. },
  364. };
  365. int __init tilcdc_panel_init(void)
  366. {
  367. return platform_driver_register(&panel_driver);
  368. }
  369. void __exit tilcdc_panel_fini(void)
  370. {
  371. platform_driver_unregister(&panel_driver);
  372. }