pwm-ab8500.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) ST-Ericsson SA 2010
  4. *
  5. * Author: Arun R Murthy <[email protected]>
  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. unsigned int hwid;
  24. };
  25. static struct ab8500_pwm_chip *ab8500_pwm_from_chip(struct pwm_chip *chip)
  26. {
  27. return container_of(chip, struct ab8500_pwm_chip, chip);
  28. }
  29. static int ab8500_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
  30. const struct pwm_state *state)
  31. {
  32. int ret;
  33. u8 reg;
  34. unsigned int higher_val, lower_val;
  35. struct ab8500_pwm_chip *ab8500 = ab8500_pwm_from_chip(chip);
  36. if (state->polarity != PWM_POLARITY_NORMAL)
  37. return -EINVAL;
  38. if (!state->enabled) {
  39. ret = abx500_mask_and_set_register_interruptible(chip->dev,
  40. AB8500_MISC, AB8500_PWM_OUT_CTRL7_REG,
  41. 1 << ab8500->hwid, 0);
  42. if (ret < 0)
  43. dev_err(chip->dev, "%s: Failed to disable PWM, Error %d\n",
  44. pwm->label, ret);
  45. return ret;
  46. }
  47. /*
  48. * get the first 8 bits that are be written to
  49. * AB8500_PWM_OUT_CTRL1_REG[0:7]
  50. */
  51. lower_val = state->duty_cycle & 0x00FF;
  52. /*
  53. * get bits [9:10] that are to be written to
  54. * AB8500_PWM_OUT_CTRL2_REG[0:1]
  55. */
  56. higher_val = ((state->duty_cycle & 0x0300) >> 8);
  57. reg = AB8500_PWM_OUT_CTRL1_REG + (ab8500->hwid * 2);
  58. ret = abx500_set_register_interruptible(chip->dev, AB8500_MISC,
  59. reg, (u8)lower_val);
  60. if (ret < 0)
  61. return ret;
  62. ret = abx500_set_register_interruptible(chip->dev, AB8500_MISC,
  63. (reg + 1), (u8)higher_val);
  64. if (ret < 0)
  65. return ret;
  66. ret = abx500_mask_and_set_register_interruptible(chip->dev,
  67. AB8500_MISC, AB8500_PWM_OUT_CTRL7_REG,
  68. 1 << ab8500->hwid, 1 << ab8500->hwid);
  69. if (ret < 0)
  70. dev_err(chip->dev, "%s: Failed to enable PWM, Error %d\n",
  71. pwm->label, ret);
  72. return ret;
  73. }
  74. static const struct pwm_ops ab8500_pwm_ops = {
  75. .apply = ab8500_pwm_apply,
  76. .owner = THIS_MODULE,
  77. };
  78. static int ab8500_pwm_probe(struct platform_device *pdev)
  79. {
  80. struct ab8500_pwm_chip *ab8500;
  81. int err;
  82. if (pdev->id < 1 || pdev->id > 31)
  83. return dev_err_probe(&pdev->dev, -EINVAL, "Invalid device id %d\n", pdev->id);
  84. /*
  85. * Nothing to be done in probe, this is required to get the
  86. * device which is required for ab8500 read and write
  87. */
  88. ab8500 = devm_kzalloc(&pdev->dev, sizeof(*ab8500), GFP_KERNEL);
  89. if (ab8500 == NULL)
  90. return -ENOMEM;
  91. ab8500->chip.dev = &pdev->dev;
  92. ab8500->chip.ops = &ab8500_pwm_ops;
  93. ab8500->chip.npwm = 1;
  94. ab8500->hwid = pdev->id - 1;
  95. err = devm_pwmchip_add(&pdev->dev, &ab8500->chip);
  96. if (err < 0)
  97. return dev_err_probe(&pdev->dev, err, "Failed to add pwm chip\n");
  98. dev_dbg(&pdev->dev, "pwm probe successful\n");
  99. return 0;
  100. }
  101. static struct platform_driver ab8500_pwm_driver = {
  102. .driver = {
  103. .name = "ab8500-pwm",
  104. },
  105. .probe = ab8500_pwm_probe,
  106. };
  107. module_platform_driver(ab8500_pwm_driver);
  108. MODULE_AUTHOR("Arun MURTHY <[email protected]>");
  109. MODULE_DESCRIPTION("AB8500 Pulse Width Modulation Driver");
  110. MODULE_ALIAS("platform:ab8500-pwm");
  111. MODULE_LICENSE("GPL v2");