video: tegra: host: Register devices in SoC files
[linux-2.6.git] / drivers / video / tegra / host / bus.c
1 /*
2  * drivers/video/tegra/host/bus.c
3  *
4  * Copyright (C) 2010 Google, Inc.
5  * Author: Erik Gilling <konkers@google.com>
6  *
7  * Copyright (C) 2010-2012 NVIDIA Corporation
8  *
9  * This software is licensed under the terms of the GNU General Public
10  * License version 2, as published by the Free Software Foundation, and
11  * may be copied, distributed, and modified under those terms.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  */
19
20 #include <linux/slab.h>
21 #include <linux/pm_runtime.h>
22 #include <linux/export.h>
23 #include <linux/nvhost.h>
24 #include <linux/io.h>
25
26 #include "bus.h"
27 #include "dev.h"
28
29 struct nvhost_bus *nvhost_bus_inst;
30 struct nvhost_master *nvhost;
31
32 struct resource *nvhost_get_resource(struct nvhost_device *dev,
33                                        unsigned int type, unsigned int num)
34 {
35         int i;
36
37         for (i = 0; i < dev->num_resources; i++) {
38                 struct resource *r = &dev->resource[i];
39
40                 if (type == resource_type(r) && num-- == 0)
41                         return r;
42         }
43         return NULL;
44 }
45 EXPORT_SYMBOL_GPL(nvhost_get_resource);
46
47 int nvhost_get_irq(struct nvhost_device *dev, unsigned int num)
48 {
49         struct resource *r = nvhost_get_resource(dev, IORESOURCE_IRQ, num);
50
51         return r ? r->start : -ENXIO;
52 }
53 EXPORT_SYMBOL_GPL(nvhost_get_irq);
54
55 struct resource *nvhost_get_resource_byname(struct nvhost_device *dev,
56                                               unsigned int type,
57                                               const char *name)
58 {
59         int i;
60
61         for (i = 0; i < dev->num_resources; i++) {
62                 struct resource *r = &dev->resource[i];
63
64                 if (type == resource_type(r) && !strcmp(r->name, name))
65                         return r;
66         }
67         return NULL;
68 }
69 EXPORT_SYMBOL_GPL(nvhost_get_resource_byname);
70
71 int nvhost_get_irq_byname(struct nvhost_device *dev, const char *name)
72 {
73         struct resource *r = nvhost_get_resource_byname(dev, IORESOURCE_IRQ,
74                                                           name);
75
76         return r ? r->start : -ENXIO;
77 }
78 EXPORT_SYMBOL_GPL(nvhost_get_irq_byname);
79
80 static struct nvhost_device_id *nvhost_bus_match_id(struct nvhost_device *dev,
81         struct nvhost_device_id *id_table)
82 {
83         while (id_table->name[0]) {
84                 if (strcmp(dev->name, id_table->name) == 0)
85                         return id_table;
86                 id_table++;
87         }
88         return NULL;
89 }
90
91 static int nvhost_bus_match(struct device *_dev, struct device_driver *drv)
92 {
93         struct nvhost_device *dev = to_nvhost_device(_dev);
94         struct nvhost_driver *ndrv = to_nvhost_driver(drv);
95
96         /* check if driver support multiple devices through id_table */
97         if (ndrv->id_table)
98                 return nvhost_bus_match_id(dev, ndrv->id_table) != NULL;
99         else /* driver does not support id_table */
100                 return !strncmp(dev->name, drv->name, strlen(drv->name));
101 }
102
103 static int nvhost_drv_probe(struct device *_dev)
104 {
105         struct nvhost_driver *drv = to_nvhost_driver(_dev->driver);
106         struct nvhost_device *dev = to_nvhost_device(_dev);
107
108         if (drv && drv->probe) {
109                 if (drv->id_table)
110                         return drv->probe(dev, nvhost_bus_match_id(dev, drv->id_table));
111                 else
112                         return drv->probe(dev, NULL);
113         }
114         else
115                 return -ENODEV;
116 }
117
118 static int nvhost_drv_remove(struct device *_dev)
119 {
120         struct nvhost_driver *drv = to_nvhost_driver(_dev->driver);
121         struct nvhost_device *dev = to_nvhost_device(_dev);
122
123         return drv->remove(dev);
124 }
125
126 static void nvhost_drv_shutdown(struct device *_dev)
127 {
128         struct nvhost_driver *drv = to_nvhost_driver(_dev->driver);
129         struct nvhost_device *dev = to_nvhost_device(_dev);
130
131         drv->shutdown(dev);
132 }
133
134 int nvhost_driver_register(struct nvhost_driver *drv)
135 {
136         drv->driver.bus = &nvhost_bus_inst->nvhost_bus_type;
137         if (drv->probe)
138                 drv->driver.probe = nvhost_drv_probe;
139         if (drv->remove)
140                 drv->driver.remove = nvhost_drv_remove;
141         if (drv->shutdown)
142                 drv->driver.shutdown = nvhost_drv_shutdown;
143
144         return driver_register(&drv->driver);
145 }
146 EXPORT_SYMBOL(nvhost_driver_register);
147
148 void nvhost_driver_unregister(struct nvhost_driver *drv)
149 {
150         driver_unregister(&drv->driver);
151 }
152 EXPORT_SYMBOL_GPL(nvhost_driver_unregister);
153
154 int nvhost_add_devices(struct nvhost_device **devs, int num)
155 {
156         int i, ret = 0;
157
158         for (i = 0; i < num; i++) {
159                 ret = nvhost_device_register(devs[i]);
160                 if (ret) {
161                         while (--i >= 0)
162                                 nvhost_device_unregister(devs[i]);
163                         break;
164                 }
165         }
166
167         return ret;
168 }
169 EXPORT_SYMBOL_GPL(nvhost_add_devices);
170
171 int nvhost_device_register(struct nvhost_device *dev)
172 {
173         int i, ret = 0;
174
175         if (!dev)
176                 return -EINVAL;
177
178         device_initialize(&dev->dev);
179
180         /*  If the dev does not have a parent, assign host1x as parent */
181         if (!dev->dev.parent && nvhost && nvhost->dev != dev)
182                 dev->dev.parent = &nvhost->dev->dev;
183
184         dev->dev.bus = &nvhost_bus_inst->nvhost_bus_type;
185
186         if (dev->id != -1)
187                 dev_set_name(&dev->dev, "%s.%d", dev->name,  dev->id);
188         else
189                 dev_set_name(&dev->dev, "%s", dev->name);
190
191         for (i = 0; i < dev->num_resources; i++) {
192                 struct resource *p, *r = &dev->resource[i];
193
194                 if (r->name == NULL)
195                         r->name = dev_name(&dev->dev);
196
197                 p = r->parent;
198                 if (!p) {
199                         if (resource_type(r) == IORESOURCE_MEM)
200                                 p = &iomem_resource;
201                         else if (resource_type(r) == IORESOURCE_IO)
202                                 p = &ioport_resource;
203                 }
204
205                 if (p && insert_resource(p, r)) {
206                         pr_err("%s: failed to claim resource %d\n",
207                                dev_name(&dev->dev), i);
208                         ret = -EBUSY;
209                         goto failed;
210                 }
211         }
212
213         ret = device_add(&dev->dev);
214         if (ret == 0)
215                 return ret;
216
217 failed:
218         while (--i >= 0) {
219                 struct resource *r = &dev->resource[i];
220                 unsigned long type = resource_type(r);
221
222                 if (type == IORESOURCE_MEM || type == IORESOURCE_IO)
223                         release_resource(r);
224         }
225
226         return ret;
227 }
228 EXPORT_SYMBOL_GPL(nvhost_device_register);
229
230 void nvhost_device_unregister(struct nvhost_device *dev)
231 {
232         int i;
233         if (dev) {
234                 device_del(&dev->dev);
235
236                 for (i = 0; i < dev->num_resources; i++) {
237                         struct resource *r = &dev->resource[i];
238                         unsigned long type = resource_type(r);
239
240                         if (type == IORESOURCE_MEM || type == IORESOURCE_IO)
241                                 release_resource(r);
242                 }
243
244                 put_device(&dev->dev);
245         }
246 }
247 EXPORT_SYMBOL_GPL(nvhost_device_unregister);
248
249 void nvhost_device_writel(struct nvhost_device *dev, u32 r, u32 v)
250 {
251         writel(v, dev->aperture + r);
252 }
253 EXPORT_SYMBOL_GPL(nvhost_device_writel);
254
255 u32 nvhost_device_readl(struct nvhost_device *dev, u32 r)
256 {
257         return readl(dev->aperture + r);
258 }
259 EXPORT_SYMBOL_GPL(nvhost_device_readl);
260
261 #ifdef CONFIG_PM_SLEEP
262
263 static int nvhost_legacy_suspend(struct device *dev, pm_message_t mesg)
264 {
265         struct nvhost_driver *pdrv = to_nvhost_driver(dev->driver);
266         struct nvhost_device *pdev = to_nvhost_device(dev);
267         int ret = 0;
268
269         if (dev->driver && pdrv->suspend)
270                 ret = pdrv->suspend(pdev, mesg);
271
272         return ret;
273 }
274
275 static int nvhost_legacy_resume(struct device *dev)
276 {
277         struct nvhost_driver *pdrv = to_nvhost_driver(dev->driver);
278         struct nvhost_device *pdev = to_nvhost_device(dev);
279         int ret = 0;
280
281         if (dev->driver && pdrv->resume)
282                 ret = pdrv->resume(pdev);
283
284         return ret;
285 }
286
287 static int nvhost_pm_prepare(struct device *dev)
288 {
289         struct device_driver *drv = dev->driver;
290         int ret = 0;
291
292         if (drv && drv->pm && drv->pm->prepare)
293                 ret = drv->pm->prepare(dev);
294
295         return ret;
296 }
297
298 static void nvhost_pm_complete(struct device *dev)
299 {
300         struct device_driver *drv = dev->driver;
301
302         if (drv && drv->pm && drv->pm->complete)
303                 drv->pm->complete(dev);
304 }
305
306 #else /* !CONFIG_PM_SLEEP */
307
308 #define nvhost_pm_prepare               NULL
309 #define nvhost_pm_complete              NULL
310
311 #endif /* !CONFIG_PM_SLEEP */
312
313 #ifdef CONFIG_SUSPEND
314
315 int __weak nvhost_pm_suspend(struct device *dev)
316 {
317         struct device_driver *drv = dev->driver;
318         int ret = 0;
319
320         if (!drv)
321                 return 0;
322
323         if (drv->pm) {
324                 if (drv->pm->suspend)
325                         ret = drv->pm->suspend(dev);
326         } else {
327                 ret = nvhost_legacy_suspend(dev, PMSG_SUSPEND);
328         }
329
330         return ret;
331 }
332
333 int __weak nvhost_pm_suspend_noirq(struct device *dev)
334 {
335         struct device_driver *drv = dev->driver;
336         int ret = 0;
337
338         if (!drv)
339                 return 0;
340
341         if (drv->pm) {
342                 if (drv->pm->suspend_noirq)
343                         ret = drv->pm->suspend_noirq(dev);
344         }
345
346         return ret;
347 }
348
349 int __weak nvhost_pm_resume(struct device *dev)
350 {
351         struct device_driver *drv = dev->driver;
352         int ret = 0;
353
354         if (!drv)
355                 return 0;
356
357         if (drv->pm) {
358                 if (drv->pm->resume)
359                         ret = drv->pm->resume(dev);
360         } else {
361                 ret = nvhost_legacy_resume(dev);
362         }
363
364         return ret;
365 }
366
367 int __weak nvhost_pm_resume_noirq(struct device *dev)
368 {
369         struct device_driver *drv = dev->driver;
370         int ret = 0;
371
372         if (!drv)
373                 return 0;
374
375         if (drv->pm) {
376                 if (drv->pm->resume_noirq)
377                         ret = drv->pm->resume_noirq(dev);
378         }
379
380         return ret;
381 }
382
383 #else /* !CONFIG_SUSPEND */
384
385 #define nvhost_pm_suspend               NULL
386 #define nvhost_pm_resume                NULL
387 #define nvhost_pm_suspend_noirq NULL
388 #define nvhost_pm_resume_noirq  NULL
389
390 #endif /* !CONFIG_SUSPEND */
391
392 #ifdef CONFIG_HIBERNATION
393
394 static int nvhost_pm_freeze(struct device *dev)
395 {
396         struct device_driver *drv = dev->driver;
397         int ret = 0;
398
399         if (!drv)
400                 return 0;
401
402         if (drv->pm) {
403                 if (drv->pm->freeze)
404                         ret = drv->pm->freeze(dev);
405         } else {
406                 ret = nvhost_legacy_suspend(dev, PMSG_FREEZE);
407         }
408
409         return ret;
410 }
411
412 static int nvhost_pm_freeze_noirq(struct device *dev)
413 {
414         struct device_driver *drv = dev->driver;
415         int ret = 0;
416
417         if (!drv)
418                 return 0;
419
420         if (drv->pm) {
421                 if (drv->pm->freeze_noirq)
422                         ret = drv->pm->freeze_noirq(dev);
423         }
424
425         return ret;
426 }
427
428 static int nvhost_pm_thaw(struct device *dev)
429 {
430         struct device_driver *drv = dev->driver;
431         int ret = 0;
432
433         if (!drv)
434                 return 0;
435
436         if (drv->pm) {
437                 if (drv->pm->thaw)
438                         ret = drv->pm->thaw(dev);
439         } else {
440                 ret = nvhost_legacy_resume(dev);
441         }
442
443         return ret;
444 }
445
446 static int nvhost_pm_thaw_noirq(struct device *dev)
447 {
448         struct device_driver *drv = dev->driver;
449         int ret = 0;
450
451         if (!drv)
452                 return 0;
453
454         if (drv->pm) {
455                 if (drv->pm->thaw_noirq)
456                         ret = drv->pm->thaw_noirq(dev);
457         }
458
459         return ret;
460 }
461
462 static int nvhost_pm_poweroff(struct device *dev)
463 {
464         struct device_driver *drv = dev->driver;
465         int ret = 0;
466
467         if (!drv)
468                 return 0;
469
470         if (drv->pm) {
471                 if (drv->pm->poweroff)
472                         ret = drv->pm->poweroff(dev);
473         } else {
474                 ret = nvhost_legacy_suspend(dev, PMSG_HIBERNATE);
475         }
476
477         return ret;
478 }
479
480 static int nvhost_pm_poweroff_noirq(struct device *dev)
481 {
482         struct device_driver *drv = dev->driver;
483         int ret = 0;
484
485         if (!drv)
486                 return 0;
487
488         if (drv->pm) {
489                 if (drv->pm->poweroff_noirq)
490                         ret = drv->pm->poweroff_noirq(dev);
491         }
492
493         return ret;
494 }
495
496 static int nvhost_pm_restore(struct device *dev)
497 {
498         struct device_driver *drv = dev->driver;
499         int ret = 0;
500
501         if (!drv)
502                 return 0;
503
504         if (drv->pm) {
505                 if (drv->pm->restore)
506                         ret = drv->pm->restore(dev);
507         } else {
508                 ret = nvhost_legacy_resume(dev);
509         }
510
511         return ret;
512 }
513
514 static int nvhost_pm_restore_noirq(struct device *dev)
515 {
516         struct device_driver *drv = dev->driver;
517         int ret = 0;
518
519         if (!drv)
520                 return 0;
521
522         if (drv->pm) {
523                 if (drv->pm->restore_noirq)
524                         ret = drv->pm->restore_noirq(dev);
525         }
526
527         return ret;
528 }
529
530 #else /* !CONFIG_HIBERNATION */
531
532 #define nvhost_pm_freeze                NULL
533 #define nvhost_pm_thaw          NULL
534 #define nvhost_pm_poweroff              NULL
535 #define nvhost_pm_restore               NULL
536 #define nvhost_pm_freeze_noirq  NULL
537 #define nvhost_pm_thaw_noirq            NULL
538 #define nvhost_pm_poweroff_noirq        NULL
539 #define nvhost_pm_restore_noirq NULL
540
541 #endif /* !CONFIG_HIBERNATION */
542
543 #ifdef CONFIG_PM_RUNTIME
544
545 int __weak nvhost_pm_runtime_suspend(struct device *dev)
546 {
547         return pm_generic_runtime_suspend(dev);
548 };
549
550 int __weak nvhost_pm_runtime_resume(struct device *dev)
551 {
552         return pm_generic_runtime_resume(dev);
553 };
554
555 int __weak nvhost_pm_runtime_idle(struct device *dev)
556 {
557         return pm_generic_runtime_idle(dev);
558 };
559
560 #else /* !CONFIG_PM_RUNTIME */
561
562 #define nvhost_pm_runtime_suspend NULL
563 #define nvhost_pm_runtime_resume NULL
564 #define nvhost_pm_runtime_idle NULL
565
566 #endif /* !CONFIG_PM_RUNTIME */
567
568 static const struct dev_pm_ops nvhost_dev_pm_ops = {
569         .prepare = nvhost_pm_prepare,
570         .complete = nvhost_pm_complete,
571         .suspend = nvhost_pm_suspend,
572         .resume = nvhost_pm_resume,
573         .freeze = nvhost_pm_freeze,
574         .thaw = nvhost_pm_thaw,
575         .poweroff = nvhost_pm_poweroff,
576         .restore = nvhost_pm_restore,
577         .suspend_noirq = nvhost_pm_suspend_noirq,
578         .resume_noirq = nvhost_pm_resume_noirq,
579         .freeze_noirq = nvhost_pm_freeze_noirq,
580         .thaw_noirq = nvhost_pm_thaw_noirq,
581         .poweroff_noirq = nvhost_pm_poweroff_noirq,
582         .restore_noirq = nvhost_pm_restore_noirq,
583         .runtime_suspend = nvhost_pm_runtime_suspend,
584         .runtime_resume = nvhost_pm_runtime_resume,
585         .runtime_idle = nvhost_pm_runtime_idle,
586 };
587
588 static int set_parent(struct device *dev, void *data)
589 {
590         struct nvhost_device *ndev = to_nvhost_device(dev);
591         struct nvhost_master *host = data;
592         if (!dev->parent && ndev != host->dev)
593                 dev->parent = &host->dev->dev;
594         return 0;
595 }
596
597 int nvhost_bus_add_host(struct nvhost_master *host)
598 {
599         nvhost = host;
600
601         /*  Assign host1x as parent to all devices in nvhost bus */
602         bus_for_each_dev(&nvhost_bus_inst->nvhost_bus_type, NULL, host, set_parent);
603
604         return 0;
605 }
606
607 struct nvhost_bus *nvhost_bus_get(void)
608 {
609         return nvhost_bus_inst;
610 }
611
612 int nvhost_bus_init(void)
613 {
614         int err;
615         struct nvhost_chip_support *chip_ops;
616
617         pr_info("host1x bus init\n");
618
619         nvhost_bus_inst = kzalloc(sizeof(*nvhost_bus_inst), GFP_KERNEL);
620         if (nvhost_bus_inst == NULL) {
621                 pr_err("%s: Cannot allocate nvhost_bus\n", __func__);
622                 return -ENOMEM;
623         }
624
625         chip_ops = kzalloc(sizeof(*chip_ops), GFP_KERNEL);
626         if (chip_ops == NULL) {
627                 pr_err("%s: Cannot allocate nvhost_chip_support\n", __func__);
628                 kfree(nvhost_bus_inst);
629                 nvhost_bus_inst = NULL;
630                 return -ENOMEM;
631         }
632
633         nvhost_bus_inst->nvhost_bus_type.name = "nvhost";
634         nvhost_bus_inst->nvhost_bus_type.match = nvhost_bus_match;
635         nvhost_bus_inst->nvhost_bus_type.pm = &nvhost_dev_pm_ops;
636         nvhost_bus_inst->nvhost_chip_ops = chip_ops;
637
638         err = bus_register(&nvhost_bus_inst->nvhost_bus_type);
639
640         return err;
641 }
642 postcore_initcall(nvhost_bus_init);