of_display_timing.c 7.3 KB

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