cpu_resource.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. #include <linux/kernel.h>
  2. #include <linux/init.h>
  3. #include "common-data.h"
  4. #include <linux/types.h>
  5. #include <linux/platform_device.h>
  6. #include <linux/string.h>
  7. #include <mach/irqs.h>
  8. static LIST_HEAD(cpu_module_class);
  9. static struct cpu_module_class * mesonplat_cpu_resource_get_class(const char * name)
  10. {
  11. struct cpu_module_class * cls;
  12. list_for_each_entry(cls,&cpu_module_class,list)
  13. {
  14. if(strcmp(name,cls->name))
  15. continue;
  16. return cls;
  17. }
  18. return NULL;
  19. }
  20. static bool compare_and_copy_class(struct cpu_module_class * a,struct cpu_module_class * b)
  21. {
  22. if(a==b)
  23. return true;
  24. if(a->driver!=NULL&&b->driver!=NULL)
  25. {
  26. if(strcmp(a->driver,b->driver))
  27. return false;
  28. }
  29. if(a->driver==NULL)
  30. a->driver=b->driver;
  31. return true;
  32. }
  33. int32_t __init mesonplat_cpu_resource_add(struct cpu_module_class * cls,struct cpu_module_object* obj)
  34. {
  35. struct cpu_module_class * p;
  36. struct cpu_module_object* first;
  37. int id;
  38. if(cls==NULL || obj==NULL || cls->name==NULL)
  39. return -ENXIO;
  40. p=mesonplat_cpu_resource_get_class(cls->name);
  41. if(p==NULL)
  42. {
  43. INIT_LIST_HEAD(&cls->child);
  44. list_add(&cls->list,&cpu_module_class);
  45. p=cls;
  46. id=0;
  47. }else{
  48. first=list_first_entry(&p->child,typeof(*first),list);
  49. id=first->device.id+1;
  50. if(compare_and_copy_class(p,cls)==false)
  51. return -ENXIO;
  52. }
  53. obj->device.name=cls->driver?cls->driver:cls->name;
  54. obj->device.id=id;
  55. list_add(&obj->list,&p->child);
  56. return 0;
  57. }
  58. int32_t mesonplat_cpu_resource_get_num(const char * name)
  59. {
  60. struct cpu_module_class * cls;
  61. struct cpu_module_object *obj;
  62. int32_t ret=0;
  63. cls=mesonplat_cpu_resource_get_class(name);
  64. if(cls==NULL)
  65. return 0;
  66. list_for_each_entry(obj,&cls->child,list)
  67. {
  68. ret++;
  69. }
  70. return ret;
  71. }
  72. struct cpu_module_object * mesonplat_cpu_resource_get(const char * cls_name,const char * obj_name)
  73. {
  74. struct cpu_module_class * cls;
  75. struct cpu_module_object *obj;
  76. if(cls_name==NULL || obj_name==NULL)
  77. return NULL;
  78. cls=mesonplat_cpu_resource_get_class(cls_name);
  79. if(cls==NULL)
  80. {
  81. return NULL;
  82. }
  83. list_for_each_entry(obj,&cls->child,list)
  84. {
  85. if(strcmp(obj_name,obj->name))
  86. continue;
  87. return obj;
  88. }
  89. return NULL;
  90. }
  91. int32_t mesonplat_resource_modify(const char * cls_name,const char * obj_name, int id,
  92. struct resource * resource,
  93. u32 num_resources,
  94. void * platform_data)
  95. {
  96. struct cpu_module_object *obj;
  97. if(cls_name==NULL || obj_name==NULL)
  98. return -ENXIO;
  99. obj=mesonplat_cpu_resource_get(cls_name,obj_name);
  100. if(obj==NULL)
  101. {
  102. return -ENXIO;
  103. }
  104. obj->device.dev.platform_data=platform_data?platform_data:obj->device.dev.platform_data;
  105. if(resource)
  106. {
  107. obj->device.num_resources=num_resources;
  108. obj->device.resource=resource;
  109. }
  110. if(id>-2)
  111. {
  112. obj->device.id=id;
  113. }
  114. return 0;
  115. }
  116. static int32_t do_mesonplat_platform_device(const char * cls_name,const char * obj_name, int id,
  117. struct resource * resource,
  118. u32 num_resources,
  119. void * platform_data,bool early)
  120. {
  121. int32_t ret;
  122. struct platform_device *devs[1];
  123. struct cpu_module_object *obj;
  124. if(cls_name==NULL || obj_name==NULL)
  125. return -ENXIO;
  126. obj=mesonplat_cpu_resource_get(cls_name,obj_name);
  127. if(obj==NULL)
  128. {
  129. return -ENXIO;
  130. }
  131. obj->device.dev.platform_data=platform_data?platform_data:obj->device.dev.platform_data;
  132. if(resource)
  133. {
  134. obj->device.num_resources=num_resources;
  135. obj->device.resource=resource;
  136. }
  137. if(id>-2)
  138. {
  139. obj->device.id=id;
  140. }
  141. devs[0]=&obj->device;
  142. if(early==true)
  143. {
  144. early_platform_add_devices(devs,1);
  145. return 0;
  146. }
  147. return platform_add_devices(devs,1);
  148. }
  149. int32_t mesonplat_register_device(const char * cls_name,const char * obj_name,void * platform_data)
  150. {
  151. return do_mesonplat_platform_device(cls_name,obj_name,-2,NULL,0,platform_data,false);
  152. }
  153. int32_t mesonplat_register_device_early(const char * cls_name,const char * obj_name,void * platform_data)
  154. {
  155. return do_mesonplat_platform_device(cls_name,obj_name,-2,NULL,0,platform_data,true);
  156. }
  157. #if 0
  158. /**
  159. * End of Meson 3 uart resource description
  160. */
  161. static struct cpu_resource res_table[]={
  162. AVAILABLE_UARTS(),
  163. };
  164. #define dbg_print(a...) printk(a)
  165. bool meson_platform_data_set_default(struct platform_device * pdev,const char * id);
  166. int meson_platform_data_set(struct platform_device * pdev,const char * id ,void * platform_data)
  167. {
  168. int i;
  169. dbg_print("%s request name=%s id=%s\n",__func__,pdev->name,id);
  170. for(i=0;i<ARRAY_SIZE(res_table);i++)
  171. {
  172. if(strcmp(pdev->name,res_table[i].name)!=0 || (strcmp(id,res_table[i].id)!=0))
  173. continue;
  174. if(platform_data)
  175. pdev->dev.platform_data=platform_data;
  176. else if(meson_platform_data_set_default(pdev,id))
  177. return false;
  178. pdev->num_resources=res_table[i].num_resources;
  179. pdev->resource=res_table[i].resource;
  180. return platform_device_register(pdev);
  181. }
  182. return false;
  183. }
  184. #endif