blob: 9692185f5f3e324fa2cf77937f1e459216fc02cb [file] [log] [blame]
Duy Truong790f06d2013-02-13 16:38:12 -08001/* Copyright (c) 2012, The Linux Foundation. All rights reserved.
Sagar Dharia7e42e7f2012-03-21 15:29:04 -06002 *
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 int n = 0;
26 int ret = 0;
27
28 if (!ctrl->dev.of_node)
29 return -EINVAL;
30
31 for_each_child_of_node(ctrl->dev.of_node, node) {
32 struct property *prop;
33 struct slim_device *slim;
34 char *name;
35 prop = of_find_property(node, "elemental-addr", NULL);
36 if (!prop || prop->length != 6) {
37 dev_err(&ctrl->dev, "of_slim: invalid E-addr");
38 continue;
39 }
40 name = kzalloc(SLIMBUS_NAME_SIZE, GFP_KERNEL);
41 if (!name) {
42 dev_err(&ctrl->dev, "of_slim: out of memory");
43 ret = -ENOMEM;
44 goto of_slim_err;
45 }
46 if (of_modalias_node(node, name, SLIMBUS_NAME_SIZE) < 0) {
47 dev_err(&ctrl->dev, "of_slim: modalias failure on %s\n",
48 node->full_name);
49 kfree(name);
50 continue;
51 }
52 slim = kzalloc(sizeof(struct slim_device), GFP_KERNEL);
53 if (!slim) {
54 dev_err(&ctrl->dev, "of_slim: out of memory");
55 ret = -ENOMEM;
56 kfree(name);
57 goto of_slim_err;
58 }
59 memcpy(slim->e_addr, prop->value, 6);
60
61 binfo = krealloc(binfo, (n + 1) * sizeof(struct slim_boardinfo),
62 GFP_KERNEL);
63 if (!binfo) {
64 dev_err(&ctrl->dev, "out of memory");
65 kfree(name);
66 kfree(slim);
67 return -ENOMEM;
68 }
Kiran Kandi1756d8c2012-07-31 19:30:52 -070069
70 slim->dev.of_node = of_node_get(node);
Sagar Dharia7e42e7f2012-03-21 15:29:04 -060071 slim->name = (const char *)name;
72 binfo[n].bus_num = ctrl->nr;
73 binfo[n].slim_slave = slim;
74 n++;
75 }
76 return slim_register_board_info(binfo, n);
77of_slim_err:
78 n--;
79 while (n >= 0) {
80 kfree(binfo[n].slim_slave->name);
81 kfree(binfo[n].slim_slave);
82 }
83 kfree(binfo);
84 return ret;
85}