cpuidle.c 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /*
  2. * Copyright 2012 Freescale Semiconductor, Inc.
  3. * Copyright 2012 Linaro Ltd.
  4. *
  5. * The code contained herein is licensed under the GNU General Public
  6. * License. You may obtain a copy of the GNU General Public License
  7. * Version 2 or later at the following locations:
  8. *
  9. * http://www.opensource.org/licenses/gpl-license.html
  10. * http://www.gnu.org/copyleft/gpl.html
  11. */
  12. #include <linux/cpuidle.h>
  13. #include <linux/err.h>
  14. #include <linux/hrtimer.h>
  15. #include <linux/io.h>
  16. #include <linux/kernel.h>
  17. #include <linux/slab.h>
  18. static struct cpuidle_device __percpu * imx_cpuidle_devices;
  19. static void __init imx_cpuidle_devices_uninit(void)
  20. {
  21. int cpu_id;
  22. struct cpuidle_device *dev;
  23. for_each_possible_cpu(cpu_id) {
  24. dev = per_cpu_ptr(imx_cpuidle_devices, cpu_id);
  25. cpuidle_unregister_device(dev);
  26. }
  27. free_percpu(imx_cpuidle_devices);
  28. }
  29. int __init imx_cpuidle_init(struct cpuidle_driver *drv)
  30. {
  31. struct cpuidle_device *dev;
  32. int cpu_id, ret;
  33. if (drv->state_count > CPUIDLE_STATE_MAX) {
  34. pr_err("%s: state_count exceeds maximum\n", __func__);
  35. return -EINVAL;
  36. }
  37. ret = cpuidle_register_driver(drv);
  38. if (ret) {
  39. pr_err("%s: Failed to register cpuidle driver with error: %d\n",
  40. __func__, ret);
  41. return ret;
  42. }
  43. imx_cpuidle_devices = alloc_percpu(struct cpuidle_device);
  44. if (imx_cpuidle_devices == NULL) {
  45. ret = -ENOMEM;
  46. goto unregister_drv;
  47. }
  48. /* initialize state data for each cpuidle_device */
  49. for_each_possible_cpu(cpu_id) {
  50. dev = per_cpu_ptr(imx_cpuidle_devices, cpu_id);
  51. dev->cpu = cpu_id;
  52. dev->state_count = drv->state_count;
  53. ret = cpuidle_register_device(dev);
  54. if (ret) {
  55. pr_err("%s: Failed to register cpu %u, error: %d\n",
  56. __func__, cpu_id, ret);
  57. goto uninit;
  58. }
  59. }
  60. return 0;
  61. uninit:
  62. imx_cpuidle_devices_uninit();
  63. unregister_drv:
  64. cpuidle_unregister_driver(drv);
  65. return ret;
  66. }