of_display_timing.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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(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_get_display_timing - parse display_timing entry from device_node
  52. * @np: device_node with the properties
  53. **/
  54. static struct display_timing *of_get_display_timing(struct device_node *np)
  55. {
  56. struct display_timing *dt;
  57. u32 val = 0;
  58. int ret = 0;
  59. dt = kzalloc(sizeof(*dt), GFP_KERNEL);
  60. if (!dt) {
  61. pr_err("%s: could not allocate display_timing struct\n",
  62. of_node_full_name(np));
  63. return NULL;
  64. }
  65. ret |= parse_timing_property(np, "hback-porch", &dt->hback_porch);
  66. ret |= parse_timing_property(np, "hfront-porch", &dt->hfront_porch);
  67. ret |= parse_timing_property(np, "hactive", &dt->hactive);
  68. ret |= parse_timing_property(np, "hsync-len", &dt->hsync_len);
  69. ret |= parse_timing_property(np, "vback-porch", &dt->vback_porch);
  70. ret |= parse_timing_property(np, "vfront-porch", &dt->vfront_porch);
  71. ret |= parse_timing_property(np, "vactive", &dt->vactive);
  72. ret |= parse_timing_property(np, "vsync-len", &dt->vsync_len);
  73. ret |= parse_timing_property(np, "clock-frequency", &dt->pixelclock);
  74. dt->dmt_flags = 0;
  75. dt->data_flags = 0;
  76. if (!of_property_read_u32(np, "vsync-active", &val))
  77. dt->dmt_flags |= val ? VESA_DMT_VSYNC_HIGH :
  78. VESA_DMT_VSYNC_LOW;
  79. if (!of_property_read_u32(np, "hsync-active", &val))
  80. dt->dmt_flags |= val ? VESA_DMT_HSYNC_HIGH :
  81. VESA_DMT_HSYNC_LOW;
  82. if (!of_property_read_u32(np, "de-active", &val))
  83. dt->data_flags |= val ? DISPLAY_FLAGS_DE_HIGH :
  84. DISPLAY_FLAGS_DE_LOW;
  85. if (!of_property_read_u32(np, "pixelclk-active", &val))
  86. dt->data_flags |= val ? DISPLAY_FLAGS_PIXDATA_POSEDGE :
  87. DISPLAY_FLAGS_PIXDATA_NEGEDGE;
  88. if (of_property_read_bool(np, "interlaced"))
  89. dt->data_flags |= DISPLAY_FLAGS_INTERLACED;
  90. if (of_property_read_bool(np, "doublescan"))
  91. dt->data_flags |= DISPLAY_FLAGS_DOUBLESCAN;
  92. if (ret) {
  93. pr_err("%s: error reading timing properties\n",
  94. of_node_full_name(np));
  95. kfree(dt);
  96. return NULL;
  97. }
  98. return dt;
  99. }
  100. /**
  101. * of_get_display_timings - parse all display_timing entries from a device_node
  102. * @np: device_node with the subnodes
  103. **/
  104. struct display_timings *of_get_display_timings(struct device_node *np)
  105. {
  106. struct device_node *timings_np;
  107. struct device_node *entry;
  108. struct device_node *native_mode;
  109. struct display_timings *disp;
  110. if (!np) {
  111. pr_err("%s: no devicenode given\n", of_node_full_name(np));
  112. return NULL;
  113. }
  114. timings_np = of_find_node_by_name(np, "display-timings");
  115. if (!timings_np) {
  116. pr_err("%s: could not find display-timings node\n",
  117. of_node_full_name(np));
  118. return NULL;
  119. }
  120. disp = kzalloc(sizeof(*disp), GFP_KERNEL);
  121. if (!disp) {
  122. pr_err("%s: could not allocate struct disp'\n",
  123. of_node_full_name(np));
  124. goto dispfail;
  125. }
  126. entry = of_parse_phandle(timings_np, "native-mode", 0);
  127. /* assume first child as native mode if none provided */
  128. if (!entry)
  129. entry = of_get_next_child(np, NULL);
  130. /* if there is no child, it is useless to go on */
  131. if (!entry) {
  132. pr_err("%s: no timing specifications given\n",
  133. of_node_full_name(np));
  134. goto entryfail;
  135. }
  136. pr_debug("%s: using %s as default timing\n",
  137. of_node_full_name(np), entry->name);
  138. native_mode = entry;
  139. disp->num_timings = of_get_child_count(timings_np);
  140. if (disp->num_timings == 0) {
  141. /* should never happen, as entry was already found above */
  142. pr_err("%s: no timings specified\n", of_node_full_name(np));
  143. goto entryfail;
  144. }
  145. disp->timings = kzalloc(sizeof(struct display_timing *) *
  146. disp->num_timings, GFP_KERNEL);
  147. if (!disp->timings) {
  148. pr_err("%s: could not allocate timings array\n",
  149. of_node_full_name(np));
  150. goto entryfail;
  151. }
  152. disp->num_timings = 0;
  153. disp->native_mode = 0;
  154. for_each_child_of_node(timings_np, entry) {
  155. struct display_timing *dt;
  156. dt = of_get_display_timing(entry);
  157. if (!dt) {
  158. /*
  159. * to not encourage wrong devicetrees, fail in case of
  160. * an error
  161. */
  162. pr_err("%s: error in timing %d\n",
  163. of_node_full_name(np), disp->num_timings + 1);
  164. goto timingfail;
  165. }
  166. if (native_mode == entry)
  167. disp->native_mode = disp->num_timings;
  168. disp->timings[disp->num_timings] = dt;
  169. disp->num_timings++;
  170. }
  171. of_node_put(timings_np);
  172. /*
  173. * native_mode points to the device_node returned by of_parse_phandle
  174. * therefore call of_node_put on it
  175. */
  176. of_node_put(native_mode);
  177. pr_debug("%s: got %d timings. Using timing #%d as default\n",
  178. of_node_full_name(np), disp->num_timings,
  179. disp->native_mode + 1);
  180. return disp;
  181. timingfail:
  182. if (native_mode)
  183. of_node_put(native_mode);
  184. display_timings_release(disp);
  185. entryfail:
  186. kfree(disp);
  187. dispfail:
  188. of_node_put(timings_np);
  189. return NULL;
  190. }
  191. EXPORT_SYMBOL_GPL(of_get_display_timings);
  192. /**
  193. * of_display_timings_exist - check if a display-timings node is provided
  194. * @np: device_node with the timing
  195. **/
  196. int of_display_timings_exist(struct device_node *np)
  197. {
  198. struct device_node *timings_np;
  199. if (!np)
  200. return -EINVAL;
  201. timings_np = of_parse_phandle(np, "display-timings", 0);
  202. if (!timings_np)
  203. return -EINVAL;
  204. of_node_put(timings_np);
  205. return 1;
  206. }
  207. EXPORT_SYMBOL_GPL(of_display_timings_exist);