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
|
/*
* drivers/video/tegra/host/bus.c
*
* Copyright (C) 2010 Google, Inc.
* Author: Erik Gilling <konkers@google.com>
*
* Copyright (C) 2010-2011 NVIDIA Corporation
*
* 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.
*
* 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/pm_runtime.h>
#include <linux/nvhost.h>
#include "dev.h"
struct nvhost_master *nvhost;
struct device nvhost_bus = {
.init_name = "nvhost",
};
struct resource *nvhost_get_resource(struct nvhost_device *dev,
unsigned int type, unsigned int num)
{
int i;
for (i = 0; i < dev->num_resources; i++) {
struct resource *r = &dev->resource[i];
if (type == resource_type(r) && num-- == 0)
return r;
}
return NULL;
}
EXPORT_SYMBOL_GPL(nvhost_get_resource);
int nvhost_get_irq(struct nvhost_device *dev, unsigned int num)
{
struct resource *r = nvhost_get_resource(dev, IORESOURCE_IRQ, num);
return r ? r->start : -ENXIO;
}
EXPORT_SYMBOL_GPL(nvhost_get_irq);
struct resource *nvhost_get_resource_byname(struct nvhost_device *dev,
unsigned int type,
const char *name)
{
int i;
for (i = 0; i < dev->num_resources; i++) {
struct resource *r = &dev->resource[i];
if (type == resource_type(r) && !strcmp(r->name, name))
return r;
}
return NULL;
}
EXPORT_SYMBOL_GPL(nvhost_get_resource_byname);
int nvhost_get_irq_byname(struct nvhost_device *dev, const char *name)
{
struct resource *r = nvhost_get_resource_byname(dev, IORESOURCE_IRQ,
name);
return r ? r->start : -ENXIO;
}
EXPORT_SYMBOL_GPL(nvhost_get_irq_byname);
static int nvhost_drv_probe(struct device *_dev)
{
struct nvhost_driver *drv = to_nvhost_driver(_dev->driver);
struct nvhost_device *dev = to_nvhost_device(_dev);
dev->host = nvhost;
return drv->probe(dev);
}
static int nvhost_drv_remove(struct device *_dev)
{
struct nvhost_driver *drv = to_nvhost_driver(_dev->driver);
struct nvhost_device *dev = to_nvhost_device(_dev);
return drv->remove(dev);
}
static void nvhost_drv_shutdown(struct device *_dev)
{
struct nvhost_driver *drv = to_nvhost_driver(_dev->driver);
struct nvhost_device *dev = to_nvhost_device(_dev);
drv->shutdown(dev);
}
int nvhost_driver_register(struct nvhost_driver *drv)
{
drv->driver.bus = &nvhost_bus_type;
if (drv->probe)
drv->driver.probe = nvhost_drv_probe;
if (drv->remove)
drv->driver.remove = nvhost_drv_remove;
if (drv->shutdown)
drv->driver.shutdown = nvhost_drv_shutdown;
return driver_register(&drv->driver);
}
EXPORT_SYMBOL(nvhost_driver_register);
void nvhost_driver_unregister(struct nvhost_driver *drv)
{
driver_unregister(&drv->driver);
}
EXPORT_SYMBOL_GPL(nvhost_driver_unregister);
int nvhost_device_register(struct nvhost_device *dev)
{
int i, ret = 0;
if (!dev)
return -EINVAL;
device_initialize(&dev->dev);
if (!dev->dev.parent)
dev->dev.parent = &nvhost_bus;
dev->dev.bus = &nvhost_bus_type;
if (dev->id != -1)
dev_set_name(&dev->dev, "%s.%d", dev->name, dev->id);
else
dev_set_name(&dev->dev, "%s", dev->name);
for (i = 0; i < dev->num_resources; i++) {
struct resource *p, *r = &dev->resource[i];
if (r->name == NULL)
r->name = dev_name(&dev->dev);
p = r->parent;
if (!p) {
if (resource_type(r) == IORESOURCE_MEM)
p = &iomem_resource;
else if (resource_type(r) == IORESOURCE_IO)
p = &ioport_resource;
}
if (p && insert_resource(p, r)) {
pr_err("%s: failed to claim resource %d\n",
dev_name(&dev->dev), i);
ret = -EBUSY;
goto failed;
}
}
ret = device_add(&dev->dev);
if (ret == 0)
return ret;
failed:
while (--i >= 0) {
struct resource *r = &dev->resource[i];
unsigned long type = resource_type(r);
if (type == IORESOURCE_MEM || type == IORESOURCE_IO)
release_resource(r);
}
return ret;
}
EXPORT_SYMBOL_GPL(nvhost_device_register);
void nvhost_device_unregister(struct nvhost_device *dev)
{
int i;
if (dev) {
device_del(&dev->dev);
for (i = 0; i < dev->num_resources; i++) {
struct resource *r = &dev->resource[i];
unsigned long type = resource_type(r);
if (type == IORESOURCE_MEM || type == IORESOURCE_IO)
release_resource(r);
}
put_device(&dev->dev);
}
}
EXPORT_SYMBOL_GPL(nvhost_device_unregister);
static int nvhost_bus_match(struct device *_dev, struct device_driver *drv)
{
struct nvhost_device *dev = to_nvhost_device(_dev);
pr_info("host1x: %s %s\n", dev->name, drv->name);
return !strncmp(dev->name, drv->name, strlen(drv->name));
}
#ifdef CONFIG_PM_SLEEP
static int nvhost_legacy_suspend(struct device *dev, pm_message_t mesg)
{
struct nvhost_driver *pdrv = to_nvhost_driver(dev->driver);
struct nvhost_device *pdev = to_nvhost_device(dev);
int ret = 0;
if (dev->driver && pdrv->suspend)
ret = pdrv->suspend(pdev, mesg);
return ret;
}
static int nvhost_legacy_resume(struct device *dev)
{
struct nvhost_driver *pdrv = to_nvhost_driver(dev->driver);
struct nvhost_device *pdev = to_nvhost_device(dev);
int ret = 0;
if (dev->driver && pdrv->resume)
ret = pdrv->resume(pdev);
return ret;
}
static int nvhost_pm_prepare(struct device *dev)
{
struct device_driver *drv = dev->driver;
int ret = 0;
if (drv && drv->pm && drv->pm->prepare)
ret = drv->pm->prepare(dev);
return ret;
}
static void nvhost_pm_complete(struct device *dev)
{
struct device_driver *drv = dev->driver;
if (drv && drv->pm && drv->pm->complete)
drv->pm->complete(dev);
}
#else /* !CONFIG_PM_SLEEP */
#define nvhost_pm_prepare NULL
#define nvhost_pm_complete NULL
#endif /* !CONFIG_PM_SLEEP */
#ifdef CONFIG_SUSPEND
int __weak nvhost_pm_suspend(struct device *dev)
{
struct device_driver *drv = dev->driver;
int ret = 0;
if (!drv)
return 0;
if (drv->pm) {
if (drv->pm->suspend)
ret = drv->pm->suspend(dev);
} else {
ret = nvhost_legacy_suspend(dev, PMSG_SUSPEND);
}
return ret;
}
int __weak nvhost_pm_suspend_noirq(struct device *dev)
{
struct device_driver *drv = dev->driver;
int ret = 0;
if (!drv)
return 0;
if (drv->pm) {
if (drv->pm->suspend_noirq)
ret = drv->pm->suspend_noirq(dev);
}
return ret;
}
int __weak nvhost_pm_resume(struct device *dev)
{
struct device_driver *drv = dev->driver;
int ret = 0;
if (!drv)
return 0;
if (drv->pm) {
if (drv->pm->resume)
ret = drv->pm->resume(dev);
} else {
ret = nvhost_legacy_resume(dev);
}
return ret;
}
int __weak nvhost_pm_resume_noirq(struct device *dev)
{
struct device_driver *drv = dev->driver;
int ret = 0;
if (!drv)
return 0;
if (drv->pm) {
if (drv->pm->resume_noirq)
ret = drv->pm->resume_noirq(dev);
}
return ret;
}
#else /* !CONFIG_SUSPEND */
#define nvhost_pm_suspend NULL
#define nvhost_pm_resume NULL
#define nvhost_pm_suspend_noirq NULL
#define nvhost_pm_resume_noirq NULL
#endif /* !CONFIG_SUSPEND */
#ifdef CONFIG_HIBERNATION
static int nvhost_pm_freeze(struct device *dev)
{
struct device_driver *drv = dev->driver;
int ret = 0;
if (!drv)
return 0;
if (drv->pm) {
if (drv->pm->freeze)
ret = drv->pm->freeze(dev);
} else {
ret = nvhost_legacy_suspend(dev, PMSG_FREEZE);
}
return ret;
}
static int nvhost_pm_freeze_noirq(struct device *dev)
{
struct device_driver *drv = dev->driver;
int ret = 0;
if (!drv)
return 0;
if (drv->pm) {
if (drv->pm->freeze_noirq)
ret = drv->pm->freeze_noirq(dev);
}
return ret;
}
static int nvhost_pm_thaw(struct device *dev)
{
struct device_driver *drv = dev->driver;
int ret = 0;
if (!drv)
return 0;
if (drv->pm) {
if (drv->pm->thaw)
ret = drv->pm->thaw(dev);
} else {
ret = nvhost_legacy_resume(dev);
}
return ret;
}
static int nvhost_pm_thaw_noirq(struct device *dev)
{
struct device_driver *drv = dev->driver;
int ret = 0;
if (!drv)
return 0;
if (drv->pm) {
if (drv->pm->thaw_noirq)
ret = drv->pm->thaw_noirq(dev);
}
return ret;
}
static int nvhost_pm_poweroff(struct device *dev)
{
struct device_driver *drv = dev->driver;
int ret = 0;
if (!drv)
return 0;
if (drv->pm) {
if (drv->pm->poweroff)
ret = drv->pm->poweroff(dev);
} else {
ret = nvhost_legacy_suspend(dev, PMSG_HIBERNATE);
}
return ret;
}
static int nvhost_pm_poweroff_noirq(struct device *dev)
{
struct device_driver *drv = dev->driver;
int ret = 0;
if (!drv)
return 0;
if (drv->pm) {
if (drv->pm->poweroff_noirq)
ret = drv->pm->poweroff_noirq(dev);
}
return ret;
}
static int nvhost_pm_restore(struct device *dev)
{
struct device_driver *drv = dev->driver;
int ret = 0;
if (!drv)
return 0;
if (drv->pm) {
if (drv->pm->restore)
ret = drv->pm->restore(dev);
} else {
ret = nvhost_legacy_resume(dev);
}
return ret;
}
static int nvhost_pm_restore_noirq(struct device *dev)
{
struct device_driver *drv = dev->driver;
int ret = 0;
if (!drv)
return 0;
if (drv->pm) {
if (drv->pm->restore_noirq)
ret = drv->pm->restore_noirq(dev);
}
return ret;
}
#else /* !CONFIG_HIBERNATION */
#define nvhost_pm_freeze NULL
#define nvhost_pm_thaw NULL
#define nvhost_pm_poweroff NULL
#define nvhost_pm_restore NULL
#define nvhost_pm_freeze_noirq NULL
#define nvhost_pm_thaw_noirq NULL
#define nvhost_pm_poweroff_noirq NULL
#define nvhost_pm_restore_noirq NULL
#endif /* !CONFIG_HIBERNATION */
#ifdef CONFIG_PM_RUNTIME
int __weak nvhost_pm_runtime_suspend(struct device *dev)
{
return pm_generic_runtime_suspend(dev);
};
int __weak nvhost_pm_runtime_resume(struct device *dev)
{
return pm_generic_runtime_resume(dev);
};
int __weak nvhost_pm_runtime_idle(struct device *dev)
{
return pm_generic_runtime_idle(dev);
};
#else /* !CONFIG_PM_RUNTIME */
#define nvhost_pm_runtime_suspend NULL
#define nvhost_pm_runtime_resume NULL
#define nvhost_pm_runtime_idle NULL
#endif /* !CONFIG_PM_RUNTIME */
static const struct dev_pm_ops nvhost_dev_pm_ops = {
.prepare = nvhost_pm_prepare,
.complete = nvhost_pm_complete,
.suspend = nvhost_pm_suspend,
.resume = nvhost_pm_resume,
.freeze = nvhost_pm_freeze,
.thaw = nvhost_pm_thaw,
.poweroff = nvhost_pm_poweroff,
.restore = nvhost_pm_restore,
.suspend_noirq = nvhost_pm_suspend_noirq,
.resume_noirq = nvhost_pm_resume_noirq,
.freeze_noirq = nvhost_pm_freeze_noirq,
.thaw_noirq = nvhost_pm_thaw_noirq,
.poweroff_noirq = nvhost_pm_poweroff_noirq,
.restore_noirq = nvhost_pm_restore_noirq,
.runtime_suspend = nvhost_pm_runtime_suspend,
.runtime_resume = nvhost_pm_runtime_resume,
.runtime_idle = nvhost_pm_runtime_idle,
};
struct bus_type nvhost_bus_type = {
.name = "nvhost",
.match = nvhost_bus_match,
.pm = &nvhost_dev_pm_ops,
};
EXPORT_SYMBOL(nvhost_bus_type);
int nvhost_bus_register(struct nvhost_master *host)
{
nvhost = host;
return 0;
}
int nvhost_bus_init(void)
{
int err;
pr_info("host1x bus init\n");
err = device_register(&nvhost_bus);
if (err)
return err;
err = bus_register(&nvhost_bus_type);
if (err)
device_unregister(&nvhost_bus);
return err;
}
postcore_initcall(nvhost_bus_init);
|