of_display_timing.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. /*
  2. * OF helpers for parsing display timings
  3. *
  4. * Copyright (c) 2012 Steffen Trumtrar <s.trumtrar@pengutronix.de>, Pengutronix
  5. *
  6. * based on of_videomode.c by Sascha Hauer <s.hauer@pengutronix.de>
  7. *
  8. * This file is released under the GPLv2
  9. */
  10. #include <linux/export.h>
  11. #include <linux/of.h>
  12. #include <linux/slab.h>
  13. #include <video/display_timing.h>
  14. #include <video/of_display_timing.h>
  15. /**
  16. * parse_timing_property - parse timing_entry from device_node
  17. * @np: device_node with the property
  18. * @name: name of the property
  19. * @result: will be set to the return value
  20. *
  21. * DESCRIPTION:
  22. * Every display_timing can be specified with either just the typical value or
  23. * a range consisting of min/typ/max. This function helps handling this
  24. **/
  25. static int parse_timing_property(const struct device_node *np, const char *name,
  26. struct timing_entry *result)
  27. {
  28. struct property *prop;
  29. int length, cells, ret;
  30. prop = of_find_property(np, name, &length);
  31. if (!prop) {
  32. pr_err("%pOF: could not find property %s\n", np, name);
  33. return -EINVAL;
  34. }
  35. cells = length / sizeof(u32);
  36. if (cells == 1) {
  37. ret = of_property_read_u32(np, name, &result->typ);
  38. result->min = result->typ;
  39. result->max = result->typ;
  40. } else if (cells == 3) {
  41. ret = of_property_read_u32_array(np, name, &result->min, cells);
  42. } else {
  43. pr_err("%pOF: illegal timing specification in %s\n", np, name);
  44. return -EINVAL;
  45. }
  46. return ret;
  47. }
  48. /**
  49. * of_parse_display_timing - parse display_timing entry from device_node
  50. * @np: device_node with the properties
  51. **/
  52. static int of_parse_display_timing(const struct device_node *np,
  53. struct display_timing *dt)
  54. {
  55. u32 val = 0;
  56. int ret = 0;
  57. memset(dt, 0, sizeof(*dt));
  58. ret |= parse_timing_property(np, "hback-porch", &dt->hback_porch);
  59. ret |= parse_timing_property(np, "hfront-porch", &dt->hfront_porch);
  60. ret |= parse_timing_property(np, "hactive", &dt->hactive);
  61. ret |= parse_timing_property(np, "hsync-len", &dt->hsync_len);
  62. ret |= parse_timing_property(np, "vback-porch", &dt->vback_porch);
  63. ret |= parse_timing_property(np, "vfront-porch", &dt->vfront_porch);
  64. ret |= parse_timing_property(np, "vactive", &dt->vactive);
  65. ret |= parse_timing_property(np, "vsync-len", &dt->vsync_len);
  66. ret |= parse_timing_property(np, "clock-frequency", &dt->pixelclock);
  67. dt->flags = 0;
  68. if (!of_property_read_u32(np, "vsync-active", &val))
  69. dt->flags |= val ? DISPLAY_FLAGS_VSYNC_HIGH :
  70. DISPLAY_FLAGS_VSYNC_LOW;
  71. if (!of_property_read_u32(np, "hsync-active", &val))
  72. dt->flags |= val ? DISPLAY_FLAGS_HSYNC_HIGH :
  73. DISPLAY_FLAGS_HSYNC_LOW;
  74. if (!of_property_read_u32(np, "de-active", &val))
  75. dt->flags |= val ? DISPLAY_FLAGS_DE_HIGH :
  76. DISPLAY_FLAGS_DE_LOW;
  77. if (!of_property_read_u32(np, "pixelclk-active", &val))
  78. dt->flags |= val ? DISPLAY_FLAGS_PIXDATA_POSEDGE :
  79. DISPLAY_FLAGS_PIXDATA_NEGEDGE;
  80. if (!of_property_read_u32(np, "syncclk-active", &val))
  81. dt->flags |= val ? DISPLAY_FLAGS_SYNC_POSEDGE :
  82. DISPLAY_FLAGS_SYNC_NEGEDGE;
  83. else if (dt->flags & (DISPLAY_FLAGS_PIXDATA_POSEDGE |
  84. DISPLAY_FLAGS_PIXDATA_NEGEDGE))
  85. dt->flags |= dt->flags & DISPLAY_FLAGS_PIXDATA_POSEDGE ?
  86. DISPLAY_FLAGS_SYNC_POSEDGE :
  87. DISPLAY_FLAGS_SYNC_NEGEDGE;
  88. if (of_property_read_bool(np, "interlaced"))
  89. dt->flags |= DISPLAY_FLAGS_INTERLACED;
  90. if (of_property_read_bool(np, "doublescan"))
  91. dt->flags |= DISPLAY_FLAGS_DOUBLESCAN;
  92. if (of_property_read_bool(np, "doubleclk"))
  93. dt->flags |= DISPLAY_FLAGS_DOUBLECLK;
  94. if (ret) {
  95. pr_err("%pOF: error reading timing properties\n", np);
  96. return -EINVAL;
  97. }
  98. return 0;
  99. }
  100. /**
  101. * of_get_display_timing - parse a display_timing entry
  102. * @np: device_node with the timing subnode
  103. * @name: name of the timing node
  104. * @dt: display_timing struct to fill
  105. **/
  106. int of_get_display_timing(const struct device_node *np, const char *name,
  107. struct display_timing *dt)
  108. {
  109. struct device_node *timing_np;
  110. if (!np)
  111. return -EINVAL;
  112. timing_np = of_get_child_by_name(np, name);
  113. if (!timing_np) {
  114. pr_err("%pOF: could not find node '%s'\n", np, name);
  115. return -ENOENT;
  116. }
  117. return of_parse_display_timing(timing_np, dt);
  118. }
  119. EXPORT_SYMBOL_GPL(of_get_display_timing);
  120. /**
  121. * of_get_display_timings - parse all display_timing entries from a device_node
  122. * @np: device_node with the subnodes
  123. **/
  124. struct display_timings *of_get_display_timings(const struct device_node *np)
  125. {
  126. struct device_node *timings_np;
  127. struct device_node *entry;
  128. struct device_node *native_mode;
  129. struct display_timings *disp;
  130. if (!np)
  131. return NULL;
  132. timings_np = of_get_child_by_name(np, "display-timings");
  133. if (!timings_np) {
  134. pr_err("%pOF: could not find display-timings node\n", np);
  135. return NULL;
  136. }
  137. disp = kzalloc(sizeof(*disp), GFP_KERNEL);
  138. if (!disp) {
  139. pr_err("%pOF: could not allocate struct disp'\n", np);
  140. goto dispfail;
  141. }
  142. entry = of_parse_phandle(timings_np, "native-mode", 0);
  143. /* assume first child as native mode if none provided */
  144. if (!entry)
  145. entry = of_get_next_child(timings_np, NULL);
  146. /* if there is no child, it is useless to go on */
  147. if (!entry) {
  148. pr_err("%pOF: no timing specifications given\n", np);
  149. goto entryfail;
  150. }
  151. pr_debug("%pOF: using %s as default timing\n", np, entry->name);
  152. native_mode = entry;
  153. disp->num_timings = of_get_child_count(timings_np);
  154. if (disp->num_timings == 0) {
  155. /* should never happen, as entry was already found above */
  156. pr_err("%pOF: no timings specified\n", np);
  157. goto entryfail;
  158. }
  159. disp->timings = kzalloc(sizeof(struct display_timing *) *
  160. disp->num_timings, GFP_KERNEL);
  161. if (!disp->timings) {
  162. pr_err("%pOF: could not allocate timings array\n", np);
  163. goto entryfail;
  164. }
  165. disp->num_timings = 0;
  166. disp->native_mode = 0;
  167. for_each_child_of_node(timings_np, entry) {
  168. struct display_timing *dt;
  169. int r;
  170. dt = kzalloc(sizeof(*dt), GFP_KERNEL);
  171. if (!dt) {
  172. pr_err("%pOF: could not allocate display_timing struct\n",
  173. np);
  174. goto timingfail;
  175. }
  176. r = of_parse_display_timing(entry, dt);
  177. if (r) {
  178. /*
  179. * to not encourage wrong devicetrees, fail in case of
  180. * an error
  181. */
  182. pr_err("%pOF: error in timing %d\n",
  183. np, disp->num_timings + 1);
  184. kfree(dt);
  185. goto timingfail;
  186. }
  187. if (native_mode == entry)
  188. disp->native_mode = disp->num_timings;
  189. disp->timings[disp->num_timings] = dt;
  190. disp->num_timings++;
  191. }
  192. of_node_put(timings_np);
  193. /*
  194. * native_mode points to the device_node returned by of_parse_phandle
  195. * therefore call of_node_put on it
  196. */
  197. of_node_put(native_mode);
  198. pr_debug("%pOF: got %d timings. Using timing #%d as default\n",
  199. np, disp->num_timings,
  200. disp->native_mode + 1);
  201. return disp;
  202. timingfail:
  203. of_node_put(native_mode);
  204. display_timings_release(disp);
  205. disp = NULL;
  206. entryfail:
  207. kfree(disp);
  208. dispfail:
  209. of_node_put(timings_np);
  210. return NULL;
  211. }
  212. EXPORT_SYMBOL_GPL(of_get_display_timings);
  213. /**
  214. * of_display_timings_exist - check if a display-timings node is provided
  215. * @np: device_node with the timing
  216. **/
  217. int of_display_timings_exist(const struct device_node *np)
  218. {
  219. struct device_node *timings_np;
  220. if (!np)
  221. return -EINVAL;
  222. timings_np = of_parse_phandle(np, "display-timings", 0);
  223. if (!timings_np)
  224. return -EINVAL;
  225. of_node_put(timings_np);
  226. return 1;
  227. }
  228. EXPORT_SYMBOL_GPL(of_display_timings_exist);