blob: 2b3d2401f61e00f30e06154d0d4dabeadaaa4eb9 [file] [log] [blame]
Sagar Dhariabe37c9c2016-11-28 23:06:58 -07001/* Copyright (c) 2012, 2017 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
13/* OF helpers for SLIMbus */
14#include <linux/slimbus/slimbus.h>
15#include <linux/irq.h>
16#include <linux/slab.h>
17#include <linux/of.h>
18#include <linux/of_irq.h>
19#include <linux/of_slimbus.h>
20
21int of_register_slim_devices(struct slim_controller *ctrl)
22{
23 struct device_node *node;
24 struct slim_boardinfo *binfo = NULL;
25 struct slim_boardinfo *temp;
26 int n = 0;
27 int ret = 0;
28
29 if (!ctrl->dev.of_node)
30 return -EINVAL;
31
32 for_each_child_of_node(ctrl->dev.of_node, node) {
33 struct property *prop;
34 struct slim_device *slim;
35 char *name;
36
37 prop = of_find_property(node, "elemental-addr", NULL);
38 if (!prop || prop->length != 6) {
39 dev_err(&ctrl->dev, "of_slim: invalid E-addr");
40 continue;
41 }
42 name = kzalloc(SLIMBUS_NAME_SIZE, GFP_KERNEL);
43 if (!name) {
44 ret = -ENOMEM;
45 goto of_slim_err;
46 }
47 if (of_modalias_node(node, name, SLIMBUS_NAME_SIZE) < 0) {
48 dev_err(&ctrl->dev, "of_slim: modalias failure on %s\n",
49 node->full_name);
50 kfree(name);
51 continue;
52 }
53 slim = kzalloc(sizeof(struct slim_device), GFP_KERNEL);
54 if (!slim) {
55 ret = -ENOMEM;
56 kfree(name);
57 goto of_slim_err;
58 }
59 memcpy(slim->e_addr, prop->value, 6);
60
61 temp = krealloc(binfo, (n + 1) * sizeof(struct slim_boardinfo),
62 GFP_KERNEL);
63 if (!temp) {
64 dev_err(&ctrl->dev, "out of memory");
65 kfree(name);
66 kfree(slim);
67 ret = -ENOMEM;
68 goto of_slim_err;
69 }
70 binfo = temp;
71
72 slim->dev.of_node = of_node_get(node);
73 slim->name = (const char *)name;
74 binfo[n].bus_num = ctrl->nr;
75 binfo[n].slim_slave = slim;
76 n++;
77 }
78 ret = slim_register_board_info(binfo, n);
79 if (!ret)
80 goto of_slim_ret;
81of_slim_err:
82 while (n-- > 0) {
83 kfree(binfo[n].slim_slave->name);
84 kfree(binfo[n].slim_slave);
85 }
86of_slim_ret:
87 kfree(binfo);
88 return ret;
89}