stk6d2x_cal_pwm.c 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /*
  2. * Copyright (C) 2018 Samsung Electronics Co., Ltd. All rights reserved.
  3. *
  4. * This software is licensed under the terms of the GNU General Public
  5. * License, as published by the Free Software Foundation, and
  6. * may be copied, distributed, and modified under those terms.
  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. * You should have received a copy of the GNU General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #include <stk6d2x.h>
  17. #include <linux/pwm.h>
  18. #define OSC_CAL_FREQ 40
  19. static u16 cal_period = -1;
  20. u16 ams_pwm_gpio_period(struct stk6d2x_data *alps_data);
  21. static struct pwm_device *pwm = NULL;
  22. int stk6d2x_setup_pwm(struct stk6d2x_data *alps_data, bool onoff)
  23. {
  24. stk6d2x_wrapper *stk_wrapper = container_of(alps_data, stk6d2x_wrapper, alps_data);
  25. struct device_node *np = stk_wrapper->dev->of_node;
  26. long period = 1000000000 / OSC_CAL_FREQ;
  27. long duty = period >> 1;
  28. int ret = 0;
  29. if (onoff) {
  30. if (pwm == NULL) {
  31. pwm = devm_of_pwm_get(dev, np, NULL);
  32. if (IS_ERR(pwm)) {
  33. ALS_dbg("pwm_get error %d", PTR_ERR(pwm));
  34. return PTR_ERR(pwm);
  35. }
  36. }
  37. ret = pwm_config(pwm, duty, period);
  38. pwm_enable(pwm);
  39. } else {
  40. pwm_disable(pwm);
  41. pwm_put(pwm);
  42. pwm = NULL;
  43. }
  44. return ret;
  45. }
  46. ssize_t stk6d2x_osc_cal_store(struct device *dev,
  47. struct device_attribute *attr, const char *buf, size_t size)
  48. {
  49. stk6d2x_wrapper *stk_wrapper = dev_get_drvdata(dev);
  50. stk6d2x_data *alps_data = &stk_wrapper->alps_data;
  51. u16 ret;
  52. stk6d2x_osc_cal(alps_data);
  53. return size;
  54. }
  55. void stk6d2x_osc_cal(struct stk6d2x_data *alps_data)
  56. {
  57. u16 ret;
  58. stk6d2x_setup_pwm(alps_data, true);
  59. ret = ams_pwm_gpio_period(alps_data);
  60. if (ret != 0)
  61. cal_period = ret;
  62. ALS_dbg("period : %d", ret);
  63. stk6d2x_setup_pwm(alps_data, false);
  64. }
  65. u16 tsl2585_get_pwm_calibration(void)
  66. {
  67. if (cal_period == 0)
  68. return 0xffff;
  69. return cal_period;
  70. }