pwm-crc.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*
  2. * Copyright (C) 2015 Intel Corporation. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License version
  6. * 2 as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * Author: Shobhit Kumar <shobhit.kumar@intel.com>
  14. */
  15. #include <linux/platform_device.h>
  16. #include <linux/regmap.h>
  17. #include <linux/mfd/intel_soc_pmic.h>
  18. #include <linux/pwm.h>
  19. #define PWM0_CLK_DIV 0x4B
  20. #define PWM_OUTPUT_ENABLE BIT(7)
  21. #define PWM_DIV_CLK_0 0x00 /* DIVIDECLK = BASECLK */
  22. #define PWM_DIV_CLK_100 0x63 /* DIVIDECLK = BASECLK/100 */
  23. #define PWM_DIV_CLK_128 0x7F /* DIVIDECLK = BASECLK/128 */
  24. #define PWM0_DUTY_CYCLE 0x4E
  25. #define BACKLIGHT_EN 0x51
  26. #define PWM_MAX_LEVEL 0xFF
  27. #define PWM_BASE_CLK 6000000 /* 6 MHz */
  28. #define PWM_MAX_PERIOD_NS 21333 /* 46.875KHz */
  29. /**
  30. * struct crystalcove_pwm - Crystal Cove PWM controller
  31. * @chip: the abstract pwm_chip structure.
  32. * @regmap: the regmap from the parent device.
  33. */
  34. struct crystalcove_pwm {
  35. struct pwm_chip chip;
  36. struct regmap *regmap;
  37. };
  38. static inline struct crystalcove_pwm *to_crc_pwm(struct pwm_chip *pc)
  39. {
  40. return container_of(pc, struct crystalcove_pwm, chip);
  41. }
  42. static int crc_pwm_enable(struct pwm_chip *c, struct pwm_device *pwm)
  43. {
  44. struct crystalcove_pwm *crc_pwm = to_crc_pwm(c);
  45. regmap_write(crc_pwm->regmap, BACKLIGHT_EN, 1);
  46. return 0;
  47. }
  48. static void crc_pwm_disable(struct pwm_chip *c, struct pwm_device *pwm)
  49. {
  50. struct crystalcove_pwm *crc_pwm = to_crc_pwm(c);
  51. regmap_write(crc_pwm->regmap, BACKLIGHT_EN, 0);
  52. }
  53. static int crc_pwm_config(struct pwm_chip *c, struct pwm_device *pwm,
  54. int duty_ns, int period_ns)
  55. {
  56. struct crystalcove_pwm *crc_pwm = to_crc_pwm(c);
  57. struct device *dev = crc_pwm->chip.dev;
  58. int level;
  59. if (period_ns > PWM_MAX_PERIOD_NS) {
  60. dev_err(dev, "un-supported period_ns\n");
  61. return -EINVAL;
  62. }
  63. if (pwm->period != period_ns) {
  64. int clk_div;
  65. /* changing the clk divisor, need to disable fisrt */
  66. crc_pwm_disable(c, pwm);
  67. clk_div = PWM_BASE_CLK * period_ns / NSEC_PER_SEC;
  68. regmap_write(crc_pwm->regmap, PWM0_CLK_DIV,
  69. clk_div | PWM_OUTPUT_ENABLE);
  70. /* enable back */
  71. crc_pwm_enable(c, pwm);
  72. }
  73. /* change the pwm duty cycle */
  74. level = duty_ns * PWM_MAX_LEVEL / period_ns;
  75. regmap_write(crc_pwm->regmap, PWM0_DUTY_CYCLE, level);
  76. return 0;
  77. }
  78. static const struct pwm_ops crc_pwm_ops = {
  79. .config = crc_pwm_config,
  80. .enable = crc_pwm_enable,
  81. .disable = crc_pwm_disable,
  82. };
  83. static int crystalcove_pwm_probe(struct platform_device *pdev)
  84. {
  85. struct crystalcove_pwm *pwm;
  86. struct device *dev = pdev->dev.parent;
  87. struct intel_soc_pmic *pmic = dev_get_drvdata(dev);
  88. pwm = devm_kzalloc(&pdev->dev, sizeof(*pwm), GFP_KERNEL);
  89. if (!pwm)
  90. return -ENOMEM;
  91. pwm->chip.dev = &pdev->dev;
  92. pwm->chip.ops = &crc_pwm_ops;
  93. pwm->chip.base = -1;
  94. pwm->chip.npwm = 1;
  95. /* get the PMIC regmap */
  96. pwm->regmap = pmic->regmap;
  97. platform_set_drvdata(pdev, pwm);
  98. return pwmchip_add(&pwm->chip);
  99. }
  100. static int crystalcove_pwm_remove(struct platform_device *pdev)
  101. {
  102. struct crystalcove_pwm *pwm = platform_get_drvdata(pdev);
  103. return pwmchip_remove(&pwm->chip);
  104. }
  105. static struct platform_driver crystalcove_pwm_driver = {
  106. .probe = crystalcove_pwm_probe,
  107. .remove = crystalcove_pwm_remove,
  108. .driver = {
  109. .name = "crystal_cove_pwm",
  110. },
  111. };
  112. builtin_platform_driver(crystalcove_pwm_driver);