pwm-tipwmss.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*
  2. * TI PWM Subsystem driver
  3. *
  4. * Copyright (C) 2012 Texas Instruments Incorporated - http://www.ti.com/
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. */
  17. #include <linux/module.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/io.h>
  20. #include <linux/err.h>
  21. #include <linux/pm_runtime.h>
  22. #include <linux/of_device.h>
  23. #include "pwm-tipwmss.h"
  24. #define PWMSS_CLKCONFIG 0x8 /* Clock gating reg */
  25. #define PWMSS_CLKSTATUS 0xc /* Clock gating status reg */
  26. struct pwmss_info {
  27. void __iomem *mmio_base;
  28. struct mutex pwmss_lock;
  29. u16 pwmss_clkconfig;
  30. };
  31. u16 pwmss_submodule_state_change(struct device *dev, int set)
  32. {
  33. struct pwmss_info *info = dev_get_drvdata(dev);
  34. u16 val;
  35. mutex_lock(&info->pwmss_lock);
  36. val = readw(info->mmio_base + PWMSS_CLKCONFIG);
  37. val |= set;
  38. writew(val , info->mmio_base + PWMSS_CLKCONFIG);
  39. mutex_unlock(&info->pwmss_lock);
  40. return readw(info->mmio_base + PWMSS_CLKSTATUS);
  41. }
  42. EXPORT_SYMBOL(pwmss_submodule_state_change);
  43. static const struct of_device_id pwmss_of_match[] = {
  44. { .compatible = "ti,am33xx-pwmss" },
  45. {},
  46. };
  47. MODULE_DEVICE_TABLE(of, pwmss_of_match);
  48. static int pwmss_probe(struct platform_device *pdev)
  49. {
  50. int ret;
  51. struct resource *r;
  52. struct pwmss_info *info;
  53. struct device_node *node = pdev->dev.of_node;
  54. info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL);
  55. if (!info) {
  56. dev_err(&pdev->dev, "failed to allocate memory\n");
  57. return -ENOMEM;
  58. }
  59. mutex_init(&info->pwmss_lock);
  60. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  61. if (!r) {
  62. dev_err(&pdev->dev, "no memory resource defined\n");
  63. return -ENODEV;
  64. }
  65. info->mmio_base = devm_ioremap_resource(&pdev->dev, r);
  66. if (IS_ERR(info->mmio_base))
  67. return PTR_ERR(info->mmio_base);
  68. pm_runtime_enable(&pdev->dev);
  69. pm_runtime_get_sync(&pdev->dev);
  70. platform_set_drvdata(pdev, info);
  71. /* Populate all the child nodes here... */
  72. ret = of_platform_populate(node, NULL, NULL, &pdev->dev);
  73. if (ret)
  74. dev_err(&pdev->dev, "no child node found\n");
  75. return ret;
  76. }
  77. static int pwmss_remove(struct platform_device *pdev)
  78. {
  79. struct pwmss_info *info = platform_get_drvdata(pdev);
  80. pm_runtime_put_sync(&pdev->dev);
  81. pm_runtime_disable(&pdev->dev);
  82. mutex_destroy(&info->pwmss_lock);
  83. return 0;
  84. }
  85. static int pwmss_suspend(struct device *dev)
  86. {
  87. struct pwmss_info *info = dev_get_drvdata(dev);
  88. info->pwmss_clkconfig = readw(info->mmio_base + PWMSS_CLKCONFIG);
  89. pm_runtime_put_sync(dev);
  90. return 0;
  91. }
  92. static int pwmss_resume(struct device *dev)
  93. {
  94. struct pwmss_info *info = dev_get_drvdata(dev);
  95. pm_runtime_get_sync(dev);
  96. writew(info->pwmss_clkconfig, info->mmio_base + PWMSS_CLKCONFIG);
  97. return 0;
  98. }
  99. static SIMPLE_DEV_PM_OPS(pwmss_pm_ops, pwmss_suspend, pwmss_resume);
  100. static struct platform_driver pwmss_driver = {
  101. .driver = {
  102. .name = "pwmss",
  103. .owner = THIS_MODULE,
  104. .pm = &pwmss_pm_ops,
  105. .of_match_table = pwmss_of_match,
  106. },
  107. .probe = pwmss_probe,
  108. .remove = pwmss_remove,
  109. };
  110. module_platform_driver(pwmss_driver);
  111. MODULE_DESCRIPTION("PWM Subsystem driver");
  112. MODULE_AUTHOR("Texas Instruments");
  113. MODULE_LICENSE("GPL");