of_spmi.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. /* Copyright (c) 2012, The Linux Foundation. All rights reserved.
  2. *
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License version 2 and
  5. * only version 2 as published by the Free Software Foundation.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. */
  12. #include <linux/spmi.h>
  13. #include <linux/irq.h>
  14. #include <linux/of.h>
  15. #include <linux/of_address.h>
  16. #include <linux/of_irq.h>
  17. #include <linux/of_spmi.h>
  18. #include <linux/slab.h>
  19. #include <linux/module.h>
  20. #include <linux/types.h>
  21. struct of_spmi_dev_info {
  22. struct spmi_controller *ctrl;
  23. struct spmi_boardinfo b_info;
  24. };
  25. struct of_spmi_res_info {
  26. struct device_node *node;
  27. uint32_t num_reg;
  28. uint32_t num_irq;
  29. };
  30. /*
  31. * Initialize r_info structure for safe usage
  32. */
  33. static inline void of_spmi_init_resource(struct of_spmi_res_info *r_info,
  34. struct device_node *node)
  35. {
  36. r_info->node = node;
  37. r_info->num_reg = 0;
  38. r_info->num_irq = 0;
  39. }
  40. /*
  41. * Calculate the number of resources to allocate
  42. *
  43. * The caller is responsible for initializing the of_spmi_res_info structure.
  44. */
  45. static void of_spmi_sum_resources(struct of_spmi_res_info *r_info,
  46. bool has_reg)
  47. {
  48. struct of_irq oirq;
  49. uint64_t size;
  50. uint32_t flags;
  51. int i = 0;
  52. while (of_irq_map_one(r_info->node, i, &oirq) == 0)
  53. i++;
  54. r_info->num_irq += i;
  55. if (!has_reg)
  56. return;
  57. /*
  58. * We can't use of_address_to_resource here since it includes
  59. * address translation; and address translation assumes that no
  60. * parent buses have a size-cell of 0. But SPMI does have a
  61. * size-cell of 0.
  62. */
  63. i = 0;
  64. while (of_get_address(r_info->node, i, &size, &flags) != NULL)
  65. i++;
  66. r_info->num_reg += i;
  67. }
  68. /*
  69. * Allocate dev_node array for spmi_device - used with spmi-dev-container
  70. */
  71. static inline int of_spmi_alloc_devnode_store(struct of_spmi_dev_info *d_info,
  72. uint32_t num_dev_node)
  73. {
  74. d_info->b_info.num_dev_node = num_dev_node;
  75. d_info->b_info.dev_node = kzalloc(sizeof(struct spmi_resource) *
  76. num_dev_node, GFP_KERNEL);
  77. if (!d_info->b_info.dev_node)
  78. return -ENOMEM;
  79. return 0;
  80. }
  81. /*
  82. * Allocate enough memory to handle the resources associated with the
  83. * primary node.
  84. */
  85. static int of_spmi_allocate_node_resources(struct of_spmi_dev_info *d_info,
  86. struct of_spmi_res_info *r_info)
  87. {
  88. uint32_t num_irq = r_info->num_irq, num_reg = r_info->num_reg;
  89. struct resource *res = NULL;
  90. if (num_irq || num_reg) {
  91. res = kzalloc(sizeof(*res) * (num_irq + num_reg), GFP_KERNEL);
  92. if (!res)
  93. return -ENOMEM;
  94. }
  95. d_info->b_info.res.num_resources = num_reg + num_irq;
  96. d_info->b_info.res.resource = res;
  97. return 0;
  98. }
  99. /*
  100. * Allocate enough memory to handle the resources associated with the
  101. * spmi-dev-container nodes.
  102. */
  103. static int of_spmi_allocate_devnode_resources(struct of_spmi_dev_info *d_info,
  104. struct of_spmi_res_info *r_info,
  105. uint32_t idx)
  106. {
  107. uint32_t num_irq = r_info->num_irq, num_reg = r_info->num_reg;
  108. struct resource *res = NULL;
  109. if (num_irq || num_reg) {
  110. res = kzalloc(sizeof(*res) * (num_irq + num_reg), GFP_KERNEL);
  111. if (!res)
  112. return -ENOMEM;
  113. }
  114. d_info->b_info.dev_node[idx].num_resources = num_reg + num_irq;
  115. d_info->b_info.dev_node[idx].resource = res;
  116. return 0;
  117. }
  118. /*
  119. * free node resources - used with primary node
  120. */
  121. static void of_spmi_free_node_resources(struct of_spmi_dev_info *d_info)
  122. {
  123. kfree(d_info->b_info.res.resource);
  124. }
  125. /*
  126. * free devnode resources - used with spmi-dev-container
  127. */
  128. static void of_spmi_free_devnode_resources(struct of_spmi_dev_info *d_info)
  129. {
  130. int i;
  131. for (i = 0; i < d_info->b_info.num_dev_node; i++)
  132. kfree(d_info->b_info.dev_node[i].resource);
  133. kfree(d_info->b_info.dev_node);
  134. }
  135. static void of_spmi_populate_resources(struct of_spmi_dev_info *d_info,
  136. struct of_spmi_res_info *r_info,
  137. struct resource *res)
  138. {
  139. uint32_t num_irq = r_info->num_irq, num_reg = r_info->num_reg;
  140. int i;
  141. const __be32 *addrp;
  142. uint64_t size;
  143. uint32_t flags;
  144. if ((num_irq || num_reg) && (res != NULL)) {
  145. for (i = 0; i < num_reg; i++, res++) {
  146. /* Addresses are always 16 bits */
  147. addrp = of_get_address(r_info->node, i, &size, &flags);
  148. BUG_ON(!addrp);
  149. res->start = be32_to_cpup(addrp);
  150. res->end = res->start + size - 1;
  151. res->flags = flags;
  152. of_property_read_string_index(r_info->node, "reg-names",
  153. i, &res->name);
  154. }
  155. WARN_ON(of_irq_to_resource_table(r_info->node, res, num_irq) !=
  156. num_irq);
  157. }
  158. }
  159. /*
  160. * Gather primary node resources and populate.
  161. */
  162. static void of_spmi_populate_node_resources(struct of_spmi_dev_info *d_info,
  163. struct of_spmi_res_info *r_info)
  164. {
  165. struct resource *res;
  166. res = d_info->b_info.res.resource;
  167. d_info->b_info.res.of_node = r_info->node;
  168. of_property_read_string(r_info->node, "label",
  169. &d_info->b_info.res.label);
  170. of_spmi_populate_resources(d_info, r_info, res);
  171. }
  172. /*
  173. * Gather node devnode resources and populate - used with spmi-dev-container.
  174. */
  175. static void of_spmi_populate_devnode_resources(struct of_spmi_dev_info *d_info,
  176. struct of_spmi_res_info *r_info,
  177. int idx)
  178. {
  179. struct resource *res;
  180. res = d_info->b_info.dev_node[idx].resource;
  181. d_info->b_info.dev_node[idx].of_node = r_info->node;
  182. of_property_read_string(r_info->node, "label",
  183. &d_info->b_info.dev_node[idx].label);
  184. of_spmi_populate_resources(d_info, r_info, res);
  185. }
  186. /*
  187. * create a single spmi_device
  188. */
  189. static int of_spmi_create_device(struct of_spmi_dev_info *d_info,
  190. struct device_node *node)
  191. {
  192. struct spmi_controller *ctrl = d_info->ctrl;
  193. struct spmi_boardinfo *b_info = &d_info->b_info;
  194. void *result;
  195. int rc;
  196. rc = of_modalias_node(node, b_info->name, sizeof(b_info->name));
  197. if (rc < 0) {
  198. dev_err(&ctrl->dev, "of_spmi modalias failure on %s\n",
  199. node->full_name);
  200. return rc;
  201. }
  202. b_info->of_node = of_node_get(node);
  203. result = spmi_new_device(ctrl, b_info);
  204. if (result == NULL) {
  205. dev_err(&ctrl->dev, "of_spmi: Failure registering %s\n",
  206. node->full_name);
  207. of_node_put(node);
  208. return -ENODEV;
  209. }
  210. return 0;
  211. }
  212. /*
  213. * Walks all children of a node containing the spmi-dev-container
  214. * binding. This special type of spmi_device can include resources
  215. * from more than one device node.
  216. */
  217. static void of_spmi_walk_dev_container(struct of_spmi_dev_info *d_info,
  218. struct device_node *container)
  219. {
  220. struct of_spmi_res_info r_info = {};
  221. struct spmi_controller *ctrl = d_info->ctrl;
  222. struct device_node *node;
  223. int rc, i, num_dev_node = 0;
  224. if (!of_device_is_available(container))
  225. return;
  226. /*
  227. * Count the total number of device_nodes so we know how much
  228. * device_store to allocate.
  229. */
  230. for_each_child_of_node(container, node) {
  231. if (!of_device_is_available(node))
  232. continue;
  233. num_dev_node++;
  234. }
  235. rc = of_spmi_alloc_devnode_store(d_info, num_dev_node);
  236. if (rc) {
  237. dev_err(&ctrl->dev, "%s: unable to allocate devnode resources\n",
  238. __func__);
  239. return;
  240. }
  241. i = 0;
  242. for_each_child_of_node(container, node) {
  243. if (!of_device_is_available(node))
  244. continue;
  245. of_spmi_init_resource(&r_info, node);
  246. of_spmi_sum_resources(&r_info, true);
  247. rc = of_spmi_allocate_devnode_resources(d_info, &r_info, i);
  248. if (rc) {
  249. dev_err(&ctrl->dev, "%s: unable to allocate"
  250. " resources\n", __func__);
  251. of_spmi_free_devnode_resources(d_info);
  252. return;
  253. }
  254. of_spmi_populate_devnode_resources(d_info, &r_info, i);
  255. i++;
  256. }
  257. of_spmi_init_resource(&r_info, container);
  258. of_spmi_sum_resources(&r_info, true);
  259. rc = of_spmi_allocate_node_resources(d_info, &r_info);
  260. if (rc) {
  261. dev_err(&ctrl->dev, "%s: unable to allocate resources\n",
  262. __func__);
  263. of_spmi_free_node_resources(d_info);
  264. }
  265. of_spmi_populate_node_resources(d_info, &r_info);
  266. rc = of_spmi_create_device(d_info, container);
  267. if (rc) {
  268. dev_err(&ctrl->dev, "%s: unable to create device for"
  269. " node %s\n", __func__, container->full_name);
  270. of_spmi_free_devnode_resources(d_info);
  271. return;
  272. }
  273. }
  274. /*
  275. * Walks all children of a node containing the spmi-slave-container
  276. * binding. This indicates that all spmi_devices created from this
  277. * point all share the same slave_id.
  278. */
  279. static void of_spmi_walk_slave_container(struct of_spmi_dev_info *d_info,
  280. struct device_node *container)
  281. {
  282. struct spmi_controller *ctrl = d_info->ctrl;
  283. struct device_node *node;
  284. int rc;
  285. for_each_child_of_node(container, node) {
  286. struct of_spmi_res_info r_info;
  287. if (!of_device_is_available(node))
  288. continue;
  289. /**
  290. * Check to see if this node contains children which
  291. * should be all created as the same spmi_device.
  292. */
  293. if (of_get_property(node, "spmi-dev-container", NULL)) {
  294. of_spmi_walk_dev_container(d_info, node);
  295. continue;
  296. }
  297. of_spmi_init_resource(&r_info, node);
  298. of_spmi_sum_resources(&r_info, true);
  299. rc = of_spmi_allocate_node_resources(d_info, &r_info);
  300. if (rc) {
  301. dev_err(&ctrl->dev, "%s: unable to allocate"
  302. " resources\n", __func__);
  303. goto slave_err;
  304. }
  305. of_spmi_populate_node_resources(d_info, &r_info);
  306. rc = of_spmi_create_device(d_info, node);
  307. if (rc) {
  308. dev_err(&ctrl->dev, "%s: unable to create device for"
  309. " node %s\n", __func__, node->full_name);
  310. goto slave_err;
  311. }
  312. }
  313. return;
  314. slave_err:
  315. of_spmi_free_node_resources(d_info);
  316. }
  317. int of_spmi_register_devices(struct spmi_controller *ctrl)
  318. {
  319. struct device_node *node = ctrl->dev.of_node;
  320. /* Only register child devices if the ctrl has a node pointer set */
  321. if (!node)
  322. return -ENODEV;
  323. if (of_get_property(node, "spmi-slave-container", NULL)) {
  324. dev_err(&ctrl->dev, "%s: structural error: spmi-slave-container"
  325. " is prohibited at the root level\n", __func__);
  326. return -EINVAL;
  327. } else if (of_get_property(node, "spmi-dev-container", NULL)) {
  328. dev_err(&ctrl->dev, "%s: structural error: spmi-dev-container"
  329. " is prohibited at the root level\n", __func__);
  330. return -EINVAL;
  331. }
  332. /**
  333. * Make best effort to launch as many nodes as possible. If there are
  334. * syntax errors, we will simply ignore that subtree and keep going.
  335. */
  336. for_each_child_of_node(ctrl->dev.of_node, node) {
  337. struct of_spmi_dev_info d_info = {};
  338. const __be32 *slave_id;
  339. int len, rc, have_dev_container = 0;
  340. slave_id = of_get_property(node, "reg", &len);
  341. if (!slave_id) {
  342. dev_err(&ctrl->dev, "%s: invalid sid "
  343. "on %s\n", __func__, node->full_name);
  344. continue;
  345. }
  346. d_info.b_info.slave_id = be32_to_cpup(slave_id);
  347. d_info.ctrl = ctrl;
  348. if (of_get_property(node, "spmi-dev-container", NULL))
  349. have_dev_container = 1;
  350. if (of_get_property(node, "spmi-slave-container", NULL)) {
  351. if (have_dev_container)
  352. of_spmi_walk_dev_container(&d_info, node);
  353. else
  354. of_spmi_walk_slave_container(&d_info, node);
  355. } else {
  356. struct of_spmi_res_info r_info;
  357. /**
  358. * A dev container at the second level without a slave
  359. * container is considered an error.
  360. */
  361. if (have_dev_container) {
  362. dev_err(&ctrl->dev, "%s: structural error,"
  363. " node %s has spmi-dev-container without"
  364. " specifying spmi-slave-container\n",
  365. __func__, node->full_name);
  366. continue;
  367. }
  368. if (!of_device_is_available(node))
  369. continue;
  370. of_spmi_init_resource(&r_info, node);
  371. of_spmi_sum_resources(&r_info, false);
  372. rc = of_spmi_allocate_node_resources(&d_info, &r_info);
  373. if (rc) {
  374. dev_err(&ctrl->dev, "%s: unable to allocate"
  375. " resources\n", __func__);
  376. of_spmi_free_node_resources(&d_info);
  377. continue;
  378. }
  379. of_spmi_populate_node_resources(&d_info, &r_info);
  380. rc = of_spmi_create_device(&d_info, node);
  381. if (rc) {
  382. dev_err(&ctrl->dev, "%s: unable to create"
  383. " device\n", __func__);
  384. of_spmi_free_node_resources(&d_info);
  385. continue;
  386. }
  387. }
  388. }
  389. return 0;
  390. }
  391. EXPORT_SYMBOL(of_spmi_register_devices);
  392. MODULE_LICENSE("GPL v2");