pwm-ab8500.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) ST-Ericsson SA 2010
  4. *
  5. * Author: Arun R Murthy <arun.murthy@stericsson.com>
  6. */
  7. #include <linux/err.h>
  8. #include <linux/platform_device.h>
  9. #include <linux/slab.h>
  10. #include <linux/pwm.h>
  11. #include <linux/mfd/abx500.h>
  12. #include <linux/mfd/abx500/ab8500.h>
  13. #include <linux/module.h>
  14. /*
  15. * PWM Out generators
  16. * Bank: 0x10
  17. */
  18. #define AB8500_PWM_OUT_CTRL1_REG 0x60
  19. #define AB8500_PWM_OUT_CTRL2_REG 0x61
  20. #define AB8500_PWM_OUT_CTRL7_REG 0x66
  21. struct ab8500_pwm_chip {
  22. struct pwm_chip chip;
  23. };
  24. static int ab8500_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
  25. int duty_ns, int period_ns)
  26. {
  27. int ret = 0;
  28. unsigned int higher_val, lower_val;
  29. u8 reg;
  30. /*
  31. * get the first 8 bits that are be written to
  32. * AB8500_PWM_OUT_CTRL1_REG[0:7]
  33. */
  34. lower_val = duty_ns & 0x00FF;
  35. /*
  36. * get bits [9:10] that are to be written to
  37. * AB8500_PWM_OUT_CTRL2_REG[0:1]
  38. */
  39. higher_val = ((duty_ns & 0x0300) >> 8);
  40. reg = AB8500_PWM_OUT_CTRL1_REG + ((chip->base - 1) * 2);
  41. ret = abx500_set_register_interruptible(chip->dev, AB8500_MISC,
  42. reg, (u8)lower_val);
  43. if (ret < 0)
  44. return ret;
  45. ret = abx500_set_register_interruptible(chip->dev, AB8500_MISC,
  46. (reg + 1), (u8)higher_val);
  47. return ret;
  48. }
  49. static int ab8500_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm)
  50. {
  51. int ret;
  52. ret = abx500_mask_and_set_register_interruptible(chip->dev,
  53. AB8500_MISC, AB8500_PWM_OUT_CTRL7_REG,
  54. 1 << (chip->base - 1), 1 << (chip->base - 1));
  55. if (ret < 0)
  56. dev_err(chip->dev, "%s: Failed to enable PWM, Error %d\n",
  57. pwm->label, ret);
  58. return ret;
  59. }
  60. static void ab8500_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm)
  61. {
  62. int ret;
  63. ret = abx500_mask_and_set_register_interruptible(chip->dev,
  64. AB8500_MISC, AB8500_PWM_OUT_CTRL7_REG,
  65. 1 << (chip->base - 1), 0);
  66. if (ret < 0)
  67. dev_err(chip->dev, "%s: Failed to disable PWM, Error %d\n",
  68. pwm->label, ret);
  69. }
  70. static const struct pwm_ops ab8500_pwm_ops = {
  71. .config = ab8500_pwm_config,
  72. .enable = ab8500_pwm_enable,
  73. .disable = ab8500_pwm_disable,
  74. .owner = THIS_MODULE,
  75. };
  76. static int ab8500_pwm_probe(struct platform_device *pdev)
  77. {
  78. struct ab8500_pwm_chip *ab8500;
  79. int err;
  80. /*
  81. * Nothing to be done in probe, this is required to get the
  82. * device which is required for ab8500 read and write
  83. */
  84. ab8500 = devm_kzalloc(&pdev->dev, sizeof(*ab8500), GFP_KERNEL);
  85. if (ab8500 == NULL)
  86. return -ENOMEM;
  87. ab8500->chip.dev = &pdev->dev;
  88. ab8500->chip.ops = &ab8500_pwm_ops;
  89. ab8500->chip.base = pdev->id;
  90. ab8500->chip.npwm = 1;
  91. err = pwmchip_add(&ab8500->chip);
  92. if (err < 0)
  93. return err;
  94. dev_dbg(&pdev->dev, "pwm probe successful\n");
  95. platform_set_drvdata(pdev, ab8500);
  96. return 0;
  97. }
  98. static int ab8500_pwm_remove(struct platform_device *pdev)
  99. {
  100. struct ab8500_pwm_chip *ab8500 = platform_get_drvdata(pdev);
  101. int err;
  102. err = pwmchip_remove(&ab8500->chip);
  103. if (err < 0)
  104. return err;
  105. dev_dbg(&pdev->dev, "pwm driver removed\n");
  106. return 0;
  107. }
  108. static struct platform_driver ab8500_pwm_driver = {
  109. .driver = {
  110. .name = "ab8500-pwm",
  111. },
  112. .probe = ab8500_pwm_probe,
  113. .remove = ab8500_pwm_remove,
  114. };
  115. module_platform_driver(ab8500_pwm_driver);
  116. MODULE_AUTHOR("Arun MURTHY <arun.murthy@stericsson.com>");
  117. MODULE_DESCRIPTION("AB8500 Pulse Width Modulation Driver");
  118. MODULE_ALIAS("platform:ab8500-pwm");
  119. MODULE_LICENSE("GPL v2");