aboutsummaryrefslogtreecommitdiff
path: root/drivers/of/of_spmi.c
blob: 3c51297fcac1204a60587d02439ac354304043fd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
/* Copyright (c) 2012, The Linux Foundation. All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 and
 * only version 2 as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

#include <linux/spmi.h>
#include <linux/irq.h>
#include <linux/of.h>
#include <linux/of_address.h>
#include <linux/of_irq.h>
#include <linux/of_spmi.h>
#include <linux/slab.h>
#include <linux/module.h>
#include <linux/types.h>

struct of_spmi_dev_info {
	struct spmi_controller *ctrl;
	struct spmi_boardinfo b_info;
};

struct of_spmi_res_info {
	struct device_node *node;
	uint32_t num_reg;
	uint32_t num_irq;
};

/*
 * Initialize r_info structure for safe usage
 */
static inline void of_spmi_init_resource(struct of_spmi_res_info *r_info,
					 struct device_node *node)
{
	r_info->node = node;
	r_info->num_reg = 0;
	r_info->num_irq = 0;
}

/*
 * Calculate the number of resources to allocate
 *
 * The caller is responsible for initializing the of_spmi_res_info structure.
 */
static void of_spmi_sum_resources(struct of_spmi_res_info *r_info,
				  bool has_reg)
{
	struct of_irq oirq;
	uint64_t size;
	uint32_t flags;
	int i = 0;

	while (of_irq_map_one(r_info->node, i, &oirq) == 0)
		i++;

	r_info->num_irq += i;

	if (!has_reg)
		return;

	/*
	 * We can't use of_address_to_resource here since it includes
	 * address translation; and address translation assumes that no
	 * parent buses have a size-cell of 0. But SPMI does have a
	 * size-cell of 0.
	 */
	i = 0;
	while (of_get_address(r_info->node, i, &size, &flags) != NULL)
		i++;

	r_info->num_reg += i;
}

/*
 * Allocate dev_node array for spmi_device - used with spmi-dev-container
 */
static inline int of_spmi_alloc_devnode_store(struct of_spmi_dev_info *d_info,
					      uint32_t num_dev_node)
{
	d_info->b_info.num_dev_node = num_dev_node;
	d_info->b_info.dev_node = kzalloc(sizeof(struct spmi_resource) *
						num_dev_node, GFP_KERNEL);
	if (!d_info->b_info.dev_node)
		return -ENOMEM;

	return 0;
}

/*
 * Allocate enough memory to handle the resources associated with the
 * primary node.
 */
static int of_spmi_allocate_node_resources(struct of_spmi_dev_info *d_info,
					   struct of_spmi_res_info *r_info)
{
	uint32_t num_irq = r_info->num_irq, num_reg = r_info->num_reg;
	struct resource *res = NULL;

	if (num_irq || num_reg) {
		res = kzalloc(sizeof(*res) * (num_irq + num_reg), GFP_KERNEL);
		if (!res)
			return -ENOMEM;
	}
	d_info->b_info.res.num_resources = num_reg + num_irq;
	d_info->b_info.res.resource = res;

	return 0;
}

/*
 * Allocate enough memory to handle the resources associated with the
 * spmi-dev-container nodes.
 */
static int of_spmi_allocate_devnode_resources(struct of_spmi_dev_info *d_info,
					      struct of_spmi_res_info *r_info,
					      uint32_t idx)
{
	uint32_t num_irq = r_info->num_irq, num_reg = r_info->num_reg;
	struct resource *res = NULL;

	if (num_irq || num_reg) {
		res = kzalloc(sizeof(*res) * (num_irq + num_reg), GFP_KERNEL);
		if (!res)
			return -ENOMEM;
	}
	d_info->b_info.dev_node[idx].num_resources = num_reg + num_irq;
	d_info->b_info.dev_node[idx].resource = res;

	return 0;
}

/*
 * free node resources - used with primary node
 */
static void of_spmi_free_node_resources(struct of_spmi_dev_info *d_info)
{
	kfree(d_info->b_info.res.resource);
}

/*
 * free devnode resources - used with spmi-dev-container
 */
static void of_spmi_free_devnode_resources(struct of_spmi_dev_info *d_info)
{
	int i;

	for (i = 0; i < d_info->b_info.num_dev_node; i++)
		kfree(d_info->b_info.dev_node[i].resource);

	kfree(d_info->b_info.dev_node);
}

static void of_spmi_populate_resources(struct of_spmi_dev_info *d_info,
				       struct of_spmi_res_info *r_info,
				       struct resource *res)

{
	uint32_t num_irq = r_info->num_irq, num_reg = r_info->num_reg;
	int i;
	const  __be32 *addrp;
	uint64_t size;
	uint32_t flags;

	if ((num_irq || num_reg) && (res != NULL)) {
		for (i = 0; i < num_reg; i++, res++) {
			/* Addresses are always 16 bits */
			addrp = of_get_address(r_info->node, i, &size, &flags);
			BUG_ON(!addrp);
			res->start = be32_to_cpup(addrp);
			res->end = res->start + size - 1;
			res->flags = flags;
			of_property_read_string_index(r_info->node, "reg-names",
								i, &res->name);
		}
		WARN_ON(of_irq_to_resource_table(r_info->node, res, num_irq) !=
								num_irq);
	}
}

/*
 * Gather primary node resources and populate.
 */
static void of_spmi_populate_node_resources(struct of_spmi_dev_info *d_info,
					    struct of_spmi_res_info *r_info)

{
	struct resource *res;

	res = d_info->b_info.res.resource;
	d_info->b_info.res.of_node = r_info->node;
	of_property_read_string(r_info->node, "label",
				&d_info->b_info.res.label);
	of_spmi_populate_resources(d_info, r_info, res);
}

/*
 * Gather node devnode resources and populate - used with spmi-dev-container.
 */
static void of_spmi_populate_devnode_resources(struct of_spmi_dev_info *d_info,
					       struct of_spmi_res_info *r_info,
					       int idx)

{
	struct resource *res;

	res = d_info->b_info.dev_node[idx].resource;
	d_info->b_info.dev_node[idx].of_node = r_info->node;
	of_property_read_string(r_info->node, "label",
				&d_info->b_info.dev_node[idx].label);
	of_spmi_populate_resources(d_info, r_info, res);
}

/*
 * create a single spmi_device
 */
static int of_spmi_create_device(struct of_spmi_dev_info *d_info,
				 struct device_node *node)
{
	struct spmi_controller *ctrl = d_info->ctrl;
	struct spmi_boardinfo *b_info = &d_info->b_info;
	void *result;
	int rc;

	rc = of_modalias_node(node, b_info->name, sizeof(b_info->name));
	if (rc < 0) {
		dev_err(&ctrl->dev, "of_spmi modalias failure on %s\n",
				node->full_name);
		return rc;
	}

	b_info->of_node = of_node_get(node);
	result = spmi_new_device(ctrl, b_info);

	if (result == NULL) {
		dev_err(&ctrl->dev, "of_spmi: Failure registering %s\n",
				node->full_name);
		of_node_put(node);
		return -ENODEV;
	}

	return 0;
}

/*
 * Walks all children of a node containing the spmi-dev-container
 * binding. This special type of spmi_device can include resources
 * from more than one device node.
 */
static void of_spmi_walk_dev_container(struct of_spmi_dev_info *d_info,
					struct device_node *container)
{
	struct of_spmi_res_info r_info = {};
	struct spmi_controller *ctrl = d_info->ctrl;
	struct device_node *node;
	int rc, i, num_dev_node = 0;

	if (!of_device_is_available(container))
		return;

	/*
	 * Count the total number of device_nodes so we know how much
	 * device_store to allocate.
	 */
	for_each_child_of_node(container, node) {
		if (!of_device_is_available(node))
			continue;
		num_dev_node++;
	}

	rc = of_spmi_alloc_devnode_store(d_info, num_dev_node);
	if (rc) {
		dev_err(&ctrl->dev, "%s: unable to allocate devnode resources\n",
								__func__);
		return;
	}

	i = 0;
	for_each_child_of_node(container, node) {
		if (!of_device_is_available(node))
			continue;
		of_spmi_init_resource(&r_info, node);
		of_spmi_sum_resources(&r_info, true);
		rc = of_spmi_allocate_devnode_resources(d_info, &r_info, i);
		if (rc) {
			dev_err(&ctrl->dev, "%s: unable to allocate"
					" resources\n", __func__);
			of_spmi_free_devnode_resources(d_info);
			return;
		}
		of_spmi_populate_devnode_resources(d_info, &r_info, i);
		i++;
	}

	of_spmi_init_resource(&r_info, container);
	of_spmi_sum_resources(&r_info, true);

	rc = of_spmi_allocate_node_resources(d_info, &r_info);
	if (rc) {
		dev_err(&ctrl->dev, "%s: unable to allocate resources\n",
								  __func__);
		of_spmi_free_node_resources(d_info);
	}

	of_spmi_populate_node_resources(d_info, &r_info);


	rc = of_spmi_create_device(d_info, container);
	if (rc) {
		dev_err(&ctrl->dev, "%s: unable to create device for"
				" node %s\n", __func__, container->full_name);
		of_spmi_free_devnode_resources(d_info);
		return;
	}
}

/*
 * Walks all children of a node containing the spmi-slave-container
 * binding. This indicates that all spmi_devices created from this
 * point all share the same slave_id.
 */
static void of_spmi_walk_slave_container(struct of_spmi_dev_info *d_info,
					 struct device_node *container)
{
	struct spmi_controller *ctrl = d_info->ctrl;
	struct device_node *node;
	int rc;

	for_each_child_of_node(container, node) {
		struct of_spmi_res_info r_info;

		if (!of_device_is_available(node))
			continue;

		/**
		 * Check to see if this node contains children which
		 * should be all created as the same spmi_device.
		 */
		if (of_get_property(node, "spmi-dev-container", NULL)) {
			of_spmi_walk_dev_container(d_info, node);
			continue;
		}

		of_spmi_init_resource(&r_info, node);
		of_spmi_sum_resources(&r_info, true);

		rc = of_spmi_allocate_node_resources(d_info, &r_info);
		if (rc) {
			dev_err(&ctrl->dev, "%s: unable to allocate"
						" resources\n", __func__);
			goto slave_err;
		}

		of_spmi_populate_node_resources(d_info, &r_info);

		rc = of_spmi_create_device(d_info, node);
		if (rc) {
			dev_err(&ctrl->dev, "%s: unable to create device for"
				     " node %s\n", __func__, node->full_name);
			goto slave_err;
		}
	}
	return;

slave_err:
	of_spmi_free_node_resources(d_info);
}

int of_spmi_register_devices(struct spmi_controller *ctrl)
{
	struct device_node *node = ctrl->dev.of_node;

	/* Only register child devices if the ctrl has a node pointer set */
	if (!node)
		return -ENODEV;

	if (of_get_property(node, "spmi-slave-container", NULL)) {
		dev_err(&ctrl->dev, "%s: structural error: spmi-slave-container"
			" is prohibited at the root level\n", __func__);
		return -EINVAL;
	} else if (of_get_property(node, "spmi-dev-container", NULL)) {
		dev_err(&ctrl->dev, "%s: structural error: spmi-dev-container"
			" is prohibited at the root level\n", __func__);
		return -EINVAL;
	}

	/**
	 * Make best effort to launch as many nodes as possible. If there are
	 * syntax errors, we will simply ignore that subtree and keep going.
	 */
	for_each_child_of_node(ctrl->dev.of_node, node) {
		struct of_spmi_dev_info d_info = {};
		const __be32 *slave_id;
		int len, rc, have_dev_container = 0;

		slave_id = of_get_property(node, "reg", &len);
		if (!slave_id) {
			dev_err(&ctrl->dev, "%s: invalid sid "
					"on %s\n", __func__, node->full_name);
			continue;
		}

		d_info.b_info.slave_id = be32_to_cpup(slave_id);
		d_info.ctrl = ctrl;

		if (of_get_property(node, "spmi-dev-container", NULL))
			have_dev_container = 1;
		if (of_get_property(node, "spmi-slave-container", NULL)) {
			if (have_dev_container)
				of_spmi_walk_dev_container(&d_info, node);
			else
				of_spmi_walk_slave_container(&d_info, node);
		} else {
			struct of_spmi_res_info r_info;

			/**
			 * A dev container at the second level without a slave
			 * container is considered an error.
			 */
			if (have_dev_container) {
				dev_err(&ctrl->dev, "%s: structural error,"
				     " node %s has spmi-dev-container without"
				     " specifying spmi-slave-container\n",
				     __func__, node->full_name);
				continue;
			}

			if (!of_device_is_available(node))
				continue;

			of_spmi_init_resource(&r_info, node);
			of_spmi_sum_resources(&r_info, false);
			rc = of_spmi_allocate_node_resources(&d_info, &r_info);
			if (rc) {
				dev_err(&ctrl->dev, "%s: unable to allocate"
						" resources\n", __func__);
				of_spmi_free_node_resources(&d_info);
				continue;
			}

			of_spmi_populate_node_resources(&d_info, &r_info);

			rc = of_spmi_create_device(&d_info, node);
			if (rc) {
				dev_err(&ctrl->dev, "%s: unable to create"
						" device\n", __func__);
				of_spmi_free_node_resources(&d_info);
				continue;
			}
		}
	}

	return 0;
}
EXPORT_SYMBOL(of_spmi_register_devices);

MODULE_LICENSE("GPL v2");