summaryrefslogtreecommitdiff
path: root/drivers/rpmsg/omap_rpmsg_resmgr.c
blob: 499f9a6b33fd1cc417fed31dc2bfb1398709f62c (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
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
/*
 * OMAP Remote processor resource manager resources
 *
 * Copyright (C) 2012 Texas Instruments, Inc.
 *
 * Fernando Guzman Lugo <fernando.lugo@ti.com>
 * Miguel Vadillo <vadillo@ti.com>
 *
 * This software is licensed under the terms of the GNU General Public
 * License version 2, as published by the Free Software Foundation, and
 * may be copied, distributed, and modified under those terms.
 */

#define pr_fmt(fmt)    "%s: " fmt, __func__

#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/clk.h>
#include <linux/err.h>
#include <linux/rpmsg_resmgr.h>
#include <linux/pm_runtime.h>
#include <linux/pm_qos.h>
#include <linux/regulator/consumer.h>
#include <linux/regulator/driver.h>
#include <linux/regulator/machine.h>
#include <plat/dma.h>
#include <plat/dmtimer.h>
#include <plat/omap-pm.h>
#include <plat/rpmsg_resmgr.h>
#include "omap_rpmsg_resmgr.h"

#define GPTIMERS_MAX	11

struct rprm_gpt_depot {
	struct rprm_gpt args;
	struct omap_dm_timer *gpt;
};

struct rprm_auxclk_depot {
	struct rprm_auxclk args;
	struct clk *clk;
};

struct rprm_regulator_depot {
	struct rprm_regulator args;
	struct regulator *reg_p;
	u32 orig_uv;
};

/* device handle for resources which use the generic apis */
struct rprm_gen_device_handle {
	struct device *dev;
	struct dev_pm_qos_request req;
};

/* pointer to the constraint ops exported by omap mach module */
static struct omap_rprm_ops *mach_ops;

static inline struct device *__find_device_by_name(const char *name)
{
	return bus_find_device_by_name(&platform_bus_type, NULL, name);
}

static int rprm_gptimer_request(void **handle, void *data, size_t len)
{
	int ret;
	struct rprm_gpt_depot *gptd;
	struct rprm_gpt *gpt = data;

	if (len != sizeof *gpt)
		return -EINVAL;

	if (gpt->id > GPTIMERS_MAX) {
		pr_err("invalid gptimer %u\n", gpt->id);
		return -EINVAL;
	}

	gptd = kmalloc(sizeof *gptd, GFP_KERNEL);
	if (!gptd)
		return -ENOMEM;

	gptd->gpt = omap_dm_timer_request_specific(gpt->id);
	if (!gptd->gpt) {
		ret = -EBUSY;
		goto free_handle;
	}

	ret = omap_dm_timer_set_source(gptd->gpt, gpt->src_clk);
	if (ret) {
		pr_err("invalid source %i\n", gpt->src_clk);
		goto free_gpt;
	}

	omap_dm_timer_enable(gptd->gpt);
	memcpy(&gptd->args, gpt, sizeof *gpt);
	*handle = gptd;
	return 0;

free_gpt:
	omap_dm_timer_free(gptd->gpt);
free_handle:
	kfree(gptd);
	return ret;
}

static int rprm_gptimer_release(void *handle)
{
	struct rprm_gpt_depot *gptd = handle;

	omap_dm_timer_disable(gptd->gpt);
	omap_dm_timer_free(gptd->gpt);
	kfree(gptd);

	return 0;
}

static int rprm_gptimer_get_info(void *handle, char *buf, size_t len)
{
	struct rprm_gpt_depot *gptd = handle;
	struct rprm_gpt *gpt = &gptd->args;

	return snprintf(buf, len,
		"Id:%d\n"
		"Source:%d\n",
		gpt->id, gpt->src_clk);
}

static int rprm_sdma_request(void **handle, void *data, size_t len)
{
	int ret;
	int ch;
	int i;
	struct rprm_sdma *sd;
	struct rprm_sdma *sdma = data;

	if (len != sizeof *sdma)
		return -EINVAL;

	if (sdma->num_chs > MAX_NUM_SDMA_CHANNELS) {
		pr_err("not able to provide %u channels\n", sdma->num_chs);
		return -EINVAL;
	}

	/* Create sdma depot */
	sd = kmalloc(sizeof *sd, GFP_KERNEL);
	if (!sd)
		return -ENOMEM;

	for (i = 0; i < sdma->num_chs; i++) {
		ret = omap_request_dma(0, "rpmsg_resmgr", NULL, NULL, &ch);
		if (ret) {
			pr_err("error %d providing sdma channel %d\n", ret, ch);
			goto err;
		}
		sdma->channels[i] = ch;
		pr_debug("providing sdma ch %d\n", ch);
	}

	*handle = memcpy(sd, sdma, sizeof *sdma);

	return 0;
err:
	while (i--)
		omap_free_dma(sdma->channels[i]);
	kfree(sd);
	return ret;
}

static int rprm_sdma_release(void *handle)
{
	struct rprm_sdma *sd = handle;
	int i = sd->num_chs;

	while (i--) {
		omap_free_dma(sd->channels[i]);
		pr_debug("releasing sdma ch %d\n", sd->channels[i]);
	}
	kfree(sd);

	return 0;
}

static int rprm_sdma_get_info(void *handle, char *buf, size_t len)
{
	struct rprm_sdma *sd = handle;
	int i, ret = 0;

	ret += snprintf(buf, len, "NumChannels:%d\n", sd->num_chs);
	for (i = 0 ; i < sd->num_chs; i++)
		ret += snprintf(buf + ret, len - ret, "Channel[%d]:%d\n", i,
							sd->channels[i]);
	return ret;
}

static int rprm_auxclk_request(void **handle, void *args, size_t len)
{
	int ret;
	struct rprm_auxclk *auxck = args;
	struct rprm_auxclk_depot *acd;
	struct clk *src;
	struct clk *parent;

	if (len != sizeof *auxck)
		return -EINVAL;

	/* Create auxclks depot */
	acd = kmalloc(sizeof *acd, GFP_KERNEL);
	if (!acd)
		return -ENOMEM;

	/* make sure make is NULL terminated */
	auxck->name[sizeof auxck->name - 1] = '\0';
	acd->clk = clk_get(NULL, auxck->name);
	if (!acd->clk) {
		pr_err("unable to get clock %s\n", auxck->name);
		ret = -EIO;
		goto error;
	}
	/*
	 * The parent for an auxiliar clock is set to the auxclkX_ck_src
	 * clock which is the parent of auxclkX_ck
	 */
	src = clk_get_parent(acd->clk);
	if (!src) {
		pr_err("unable to get %s source clock\n", auxck->name);
		ret = -EIO;
		goto error_aux;
	}

	/* make sure pname is NULL terminated */
	auxck->pname[sizeof auxck->pname - 1] = '\0';
	parent = clk_get(NULL, auxck->pname);
	if (!parent) {
		pr_err("unable to get parent clock %s\n", auxck->pname);
		ret = -EIO;
		goto error_aux;
	}

	ret = clk_set_parent(src, parent);
	if (ret) {
		pr_err("unable to set clk %s as parent of %s\n",
						auxck->pname, auxck->name);
		goto error_aux_parent;
	}

	ret = clk_set_rate(parent, auxck->pclk_rate);
	if (ret) {
		pr_err("rate %u not supported by %s\n", auxck->pclk_rate,
								auxck->pname);
		goto error_aux_parent;
	}

	ret = clk_set_rate(acd->clk, auxck->clk_rate);
	if (ret) {
		pr_err("rate %u not supported by %s\n", auxck->clk_rate,
								auxck->name);
		goto error_aux_parent;
	}

	ret = clk_enable(acd->clk);
	if (ret) {
		pr_err("error enabling %s\n", auxck->name);
		goto error_aux_parent;
	}
	clk_put(parent);

	memcpy(&acd->args, auxck, sizeof *auxck);

	*handle = acd;

	return 0;
error_aux_parent:
	clk_put(parent);
error_aux:
	clk_put(acd->clk);
error:
	kfree(acd);

	return ret;
}

static int rprm_auxclk_release(void *handle)
{
	struct rprm_auxclk_depot *acd = handle;

	clk_disable(acd->clk);
	clk_put(acd->clk);

	kfree(acd);

	return 0;
}

static int rprm_auxclk_get_info(void *handle, char *buf, size_t len)
{
	struct rprm_auxclk_depot *acd = handle;
	struct rprm_auxclk *auxck = &acd->args;

	return snprintf(buf, len,
		"name:%s\n"
		"rate:%u\n"
		"parent:%s\n"
		"parent rate:%u\n",
		auxck->name, auxck->clk_rate, auxck->pname, auxck->pclk_rate);
}

#ifdef CONFIG_REGULATOR
static int rprm_regulator_request(void **handle, void *data, size_t len)
{
	int ret;
	struct rprm_regulator *reg = data;
	struct rprm_regulator_depot *rd;

	if (len != sizeof *reg) {
		pr_err("error requesting regulator - invalid length\n");
		return -EINVAL;
	}

	/* create regulator depot */
	rd = kzalloc(sizeof *rd, GFP_KERNEL);
	if (!rd) {
		pr_err("error allocating memory for regulator\n");
		return -ENOMEM;
	}

	/* make sure name is NULL terminated */
	reg->name[sizeof reg->name - 1] = '\0';
	rd->reg_p = regulator_get_exclusive(NULL, reg->name);
	if (IS_ERR_OR_NULL(rd->reg_p)) {
		pr_err("error providing regulator %s\n", reg->name);
		ret = -EINVAL;
		goto error;
	}

	rd->orig_uv = regulator_get_voltage(rd->reg_p);
	ret = regulator_set_voltage(rd->reg_p, reg->min_uv, reg->max_uv);
	if (ret) {
		pr_err("error setting %s voltage\n", reg->name);
		goto error_reg;
	}

	ret = regulator_enable(rd->reg_p);
	if (ret) {
		pr_err("error enabling %s ldo regulator\n", reg->name);
		goto error_enable;
	}

	memcpy(&rd->args, reg, sizeof rd->args);
	*handle = rd;

	return 0;

error_enable:
	/* restore original voltage */
	regulator_set_voltage(rd->reg_p, rd->orig_uv, rd->orig_uv);
error_reg:
	regulator_put(rd->reg_p);
error:
	kfree(rd);

	return ret;
}

static int rprm_regulator_release(void *handle)
{
	int ret;
	struct rprm_regulator_depot *rd = handle;

	ret = regulator_disable(rd->reg_p);
	if (ret) {
		pr_err("error disabling regulator %s\n", rd->args.name);
		return ret;
	}

	/* restore orginal voltage */
	ret = regulator_set_voltage(rd->reg_p, rd->orig_uv, rd->orig_uv);
	if (ret) {
		pr_err("error restoring voltage %u\n", rd->orig_uv);
		return ret;
	}

	regulator_put(rd->reg_p);
	kfree(rd);

	return 0;
}
#else
static inline int rprm_regulator_request(void **handle, void *data, size_t len)
{
	return -1;
}

static inline int rprm_regulator_release(void *handle)
{
	return 0;
}
#endif

static int rprm_regulator_get_info(void *handle, char *buf, size_t len)
{
	struct rprm_regulator_depot *rd = handle;
	struct rprm_regulator *reg = &rd->args;

	return snprintf(buf, len,
		"name:%s\n"
		"min_uV:%d\n"
		"max_uV:%d\n"
		"orig_uV:%d\n",
		reg->name, reg->min_uv, reg->max_uv, rd->orig_uv);
}

static int
_enable_device_exclusive(void **handle, struct device **pdev, const char *name)
{
	struct device *dev = *pdev;
	struct rprm_gen_device_handle *rprm_handle;
	int ret;

	/* if we already have dev do not search it again */
	if (!dev) {
		dev = __find_device_by_name(name);
		if (!dev)
			return -ENOENT;
		if (dev && !pm_runtime_enabled(dev))
			pm_runtime_enable(dev);
		/* update pdev that works as cache to avoid searing again */
		*pdev = dev;
	}

	rprm_handle = kzalloc(sizeof *rprm_handle, GFP_KERNEL);
	if (!rprm_handle)
		return -ENOMEM;

	ret = dev_pm_qos_add_request(dev, &rprm_handle->req,
						PM_QOS_DEFAULT_VALUE);
	if (ret < 0)
		goto err_handle_free;

	ret = pm_runtime_get_sync(dev);
	if (ret) {
		/*
		 * we want exclusive accesss to the device, so if it is already
		 * enabled (ret == 1), call pm_runtime_put because somebody
		 * else is using it and return error.
		 */
		if (ret == 1) {
			pm_runtime_put_sync(dev);
			ret = -EBUSY;
		}

		pr_err("error %d get sync for %s\n", ret, name);
		goto err_qos_free;
	}

	rprm_handle->dev = dev;
	*handle = rprm_handle;

	return 0;

err_qos_free:
	dev_pm_qos_remove_request(&rprm_handle->req);
err_handle_free:
	kfree(rprm_handle);
	return ret;
}

static int _device_release(void *handle)
{
	struct rprm_gen_device_handle *obj = handle;
	struct device *dev = obj->dev;

	dev_pm_qos_remove_request(&obj->req);
	kfree(obj);

	return pm_runtime_put_sync(dev);
}

static int _device_scale(struct device *rdev, void *handle, unsigned long val)
{
	struct rprm_gen_device_handle *obj = handle;

	return 0;

	if (!mach_ops || !mach_ops->device_scale)
			return -ENOSYS;

	return mach_ops->device_scale(rdev, obj->dev, val);
}

static int _device_latency(struct device *rdev, void *handle, unsigned long val)
{
	struct rprm_gen_device_handle *obj = handle;
	int ret = dev_pm_qos_update_request(&obj->req, val);

	return ret -= ret == 1;
}

static int _device_bandwidth(struct device *rdev, void *handle,
							 unsigned long val)
{
	struct rprm_gen_device_handle *obj = handle;

	if (!mach_ops || !mach_ops->set_min_bus_tput)
		return -ENOSYS;

	return mach_ops->set_min_bus_tput(rdev, obj->dev, val);
}

static int rprm_iva_request(void **handle, void *data, size_t len)
{
	static struct device *dev;

	return  _enable_device_exclusive(handle, &dev, "iva.0");
}

static int rprm_iva_seq0_request(void **handle, void *data, size_t len)
{
	static struct device *dev;

	return _enable_device_exclusive(handle, &dev, "iva_seq0.0");
}

static int rprm_iva_seq1_request(void **handle, void *data, size_t len)
{
	static struct device *dev;

	return _enable_device_exclusive(handle, &dev, "iva_seq1.0");
}

static int rprm_fdif_request(void **handle, void *data, size_t len)
{
	static struct device *dev;

	return _enable_device_exclusive(handle, &dev, "fdif");
}

static int rprm_sl2if_request(void **handle, void *data, size_t len)
{
	static struct device *dev;

	return _enable_device_exclusive(handle, &dev, "sl2if");
}

static int rprm_iss_request(void **handle, void *data, size_t len)
{
	static struct device *dev;

	return _enable_device_exclusive(handle, &dev, "iss");
}

static struct rprm_res_ops gptimer_ops = {
	.request = rprm_gptimer_request,
	.release = rprm_gptimer_release,
	.get_info = rprm_gptimer_get_info,
};

static struct rprm_res_ops sdma_ops = {
	.request = rprm_sdma_request,
	.release = rprm_sdma_release,
	.get_info = rprm_sdma_get_info,
};

static struct rprm_res_ops auxclk_ops = {
	.request = rprm_auxclk_request,
	.release = rprm_auxclk_release,
	.get_info = rprm_auxclk_get_info,
};

static struct rprm_res_ops regulator_ops = {
	.request = rprm_regulator_request,
	.release = rprm_regulator_release,
	.get_info = rprm_regulator_get_info,
};

static struct rprm_res_ops iva_ops = {
	.request = rprm_iva_request,
	.release = _device_release,
	.latency = _device_latency,
	.bandwidth = _device_bandwidth,
	.scale = _device_scale,
};

static struct rprm_res_ops iva_seq0_ops = {
	.request = rprm_iva_seq0_request,
	.release = _device_release,
};

static struct rprm_res_ops iva_seq1_ops = {
	.request = rprm_iva_seq1_request,
	.release = _device_release,
};

static struct rprm_res_ops fdif_ops = {
	.request = rprm_fdif_request,
	.release = _device_release,
	.latency = _device_latency,
	.bandwidth = _device_bandwidth,
	.scale = _device_scale,
};

static struct rprm_res_ops sl2if_ops = {
	.request = rprm_sl2if_request,
	.release = _device_release,
};

static struct rprm_res_ops iss_ops = {
	.request = rprm_iss_request,
	.release = _device_release,
	.latency = _device_latency,
	.bandwidth = _device_bandwidth,
};

static struct rprm_res omap_res[] = {
	{
		.name = "omap-gptimer",
		.ops = &gptimer_ops,
	},
	{
		.name = "omap-sdma",
		.ops = &sdma_ops,
	},
	{
		.name = "omap-auxclk",
		.ops = &auxclk_ops,
	},
	{
		.name = "regulator",
		.ops = &regulator_ops,
	},
	{
		.name = "iva",
		.ops = &iva_ops,
	},
	{
		.name = "iva_seq0",
		.ops = &iva_seq0_ops,
	},
	{
		.name = "iva_seq1",
		.ops = &iva_seq1_ops,
	},
	{
		.name = "omap-fdif",
		.ops = &fdif_ops,
	},
	{
		.name = "omap-sl2if",
		.ops = &sl2if_ops,
	},
	{
		.name = "omap-iss",
		.ops = &iss_ops,
	},
};

static int omap_rprm_probe(struct platform_device *pdev)
{
	struct omap_rprm_pdata *pdata = pdev->dev.platform_data;

	/* set iss optional clock */

	iss_opt_clk = pdata->iss_opt_clk;
	mach_ops = pdata->ops;

	return 0;
}

static int omap_rprm_remove(struct platform_device *pdev)
{
	iss_opt_clk = NULL;
	mach_ops = NULL;

	return 0;
}

static struct platform_driver omap_rprm_driver = {
	.probe = omap_rprm_probe,
	.remove = __devexit_p(omap_rprm_remove),
	.driver = {
		.name = "omap-rprm",
		.owner = THIS_MODULE,
	},
};

static int __init omap_rprm_init(void)
{
	int i, ret;

	ret = platform_driver_register(&omap_rprm_driver);
	if (ret)
		return ret;

	for (i = 0; i < ARRAY_SIZE(omap_res); i++) {
		omap_res[i].owner = THIS_MODULE;
		rprm_resource_register(&omap_res[i]);
	}

	return 0;
}

static void __exit omap_rprm_fini(void)
{
	int i;

	for (i = 0; i < ARRAY_SIZE(omap_res); i++)
		rprm_resource_unregister(&omap_res[i]);

	platform_driver_unregister(&omap_rprm_driver);
}
module_init(omap_rprm_init);
module_exit(omap_rprm_fini);

MODULE_LICENSE("GPL v2");
MODULE_DESCRIPTION("Remote Processor Resource Manager OMAP resources");