device.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. * Copyright (C) 2006 Atmel Corporation
  3. *
  4. * See file CREDITS for list of people who contributed to this
  5. * project.
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License as
  9. * published by the Free Software Foundation; either version 2 of
  10. * the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  20. * MA 02111-1307 USA
  21. */
  22. #include <common.h>
  23. #include <asm/arch/platform.h>
  24. #include "sm.h"
  25. struct device_state {
  26. int refcount;
  27. };
  28. static struct device_state device_state[NR_DEVICES];
  29. static int claim_resource(const struct resource *res)
  30. {
  31. int ret = 0;
  32. switch (res->type) {
  33. case RESOURCE_GPIO:
  34. ret = gpio_set_func(res->u.gpio.gpio_dev,
  35. res->u.gpio.start,
  36. res->u.gpio.nr_pins,
  37. res->u.gpio.func);
  38. break;
  39. case RESOURCE_CLOCK:
  40. ret = pm_enable_clock(res->u.clock.id, res->u.clock.index);
  41. break;
  42. }
  43. return ret;
  44. }
  45. static void free_resource(const struct resource *res)
  46. {
  47. switch (res->type) {
  48. case RESOURCE_GPIO:
  49. gpio_free(res->u.gpio.gpio_dev, res->u.gpio.start,
  50. res->u.gpio.nr_pins);
  51. break;
  52. case RESOURCE_CLOCK:
  53. pm_disable_clock(res->u.clock.id, res->u.clock.index);
  54. break;
  55. }
  56. }
  57. static int init_dev(const struct device *dev)
  58. {
  59. unsigned int i;
  60. int ret = 0;
  61. for (i = 0; i < dev->nr_resources; i++) {
  62. ret = claim_resource(&dev->resource[i]);
  63. if (ret)
  64. goto cleanup;
  65. }
  66. return 0;
  67. cleanup:
  68. while (i--)
  69. free_resource(&dev->resource[i]);
  70. return ret;
  71. }
  72. const struct device *get_device(enum device_id devid)
  73. {
  74. struct device_state *devstate;
  75. const struct device *dev;
  76. unsigned long flags;
  77. int initialized = 0;
  78. int ret = 0;
  79. devstate = &device_state[devid];
  80. dev = &chip_device[devid];
  81. flags = disable_interrupts();
  82. if (devstate->refcount++)
  83. initialized = 1;
  84. if (flags)
  85. enable_interrupts();
  86. if (!initialized)
  87. ret = init_dev(dev);
  88. return ret ? NULL : dev;
  89. }
  90. void put_device(const struct device *dev)
  91. {
  92. struct device_state *devstate;
  93. unsigned long devid, flags;
  94. devid = (unsigned long)(dev - chip_device) / sizeof(struct device);
  95. devstate = &device_state[devid];
  96. flags = disable_interrupts();
  97. devstate--;
  98. if (!devstate) {
  99. unsigned int i;
  100. for (i = 0; i < dev->nr_resources; i++)
  101. free_resource(&dev->resource[i]);
  102. }
  103. if (flags)
  104. enable_interrupts();
  105. }