lm3533-ctrlbank.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*
  2. * lm3533-ctrlbank.c -- LM3533 Generic Control Bank interface
  3. *
  4. * Copyright (C) 2011-2012 Texas Instruments
  5. *
  6. * Author: Johan Hovold <jhovold@gmail.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of the GNU General Public License as published by the
  10. * Free Software Foundation; either version 2 of the License, or (at your
  11. * option) any later version.
  12. */
  13. #include <linux/device.h>
  14. #include <linux/module.h>
  15. #include <linux/mfd/lm3533.h>
  16. #define LM3533_MAX_CURRENT_MIN 5000
  17. #define LM3533_MAX_CURRENT_MAX 29800
  18. #define LM3533_MAX_CURRENT_STEP 800
  19. #define LM3533_BRIGHTNESS_MAX 255
  20. #define LM3533_PWM_MAX 0x3f
  21. #define LM3533_REG_PWM_BASE 0x14
  22. #define LM3533_REG_MAX_CURRENT_BASE 0x1f
  23. #define LM3533_REG_CTRLBANK_ENABLE 0x27
  24. #define LM3533_REG_BRIGHTNESS_BASE 0x40
  25. static inline u8 lm3533_ctrlbank_get_reg(struct lm3533_ctrlbank *cb, u8 base)
  26. {
  27. return base + cb->id;
  28. }
  29. int lm3533_ctrlbank_enable(struct lm3533_ctrlbank *cb)
  30. {
  31. u8 mask;
  32. int ret;
  33. dev_dbg(cb->dev, "%s - %d\n", __func__, cb->id);
  34. mask = 1 << cb->id;
  35. ret = lm3533_update(cb->lm3533, LM3533_REG_CTRLBANK_ENABLE,
  36. mask, mask);
  37. if (ret)
  38. dev_err(cb->dev, "failed to enable ctrlbank %d\n", cb->id);
  39. return ret;
  40. }
  41. EXPORT_SYMBOL_GPL(lm3533_ctrlbank_enable);
  42. int lm3533_ctrlbank_disable(struct lm3533_ctrlbank *cb)
  43. {
  44. u8 mask;
  45. int ret;
  46. dev_dbg(cb->dev, "%s - %d\n", __func__, cb->id);
  47. mask = 1 << cb->id;
  48. ret = lm3533_update(cb->lm3533, LM3533_REG_CTRLBANK_ENABLE, 0, mask);
  49. if (ret)
  50. dev_err(cb->dev, "failed to disable ctrlbank %d\n", cb->id);
  51. return ret;
  52. }
  53. EXPORT_SYMBOL_GPL(lm3533_ctrlbank_disable);
  54. /*
  55. * Full-scale current.
  56. *
  57. * imax 5000 - 29800 uA (800 uA step)
  58. */
  59. int lm3533_ctrlbank_set_max_current(struct lm3533_ctrlbank *cb, u16 imax)
  60. {
  61. u8 reg;
  62. u8 val;
  63. int ret;
  64. if (imax < LM3533_MAX_CURRENT_MIN || imax > LM3533_MAX_CURRENT_MAX)
  65. return -EINVAL;
  66. val = (imax - LM3533_MAX_CURRENT_MIN) / LM3533_MAX_CURRENT_STEP;
  67. reg = lm3533_ctrlbank_get_reg(cb, LM3533_REG_MAX_CURRENT_BASE);
  68. ret = lm3533_write(cb->lm3533, reg, val);
  69. if (ret)
  70. dev_err(cb->dev, "failed to set max current\n");
  71. return ret;
  72. }
  73. EXPORT_SYMBOL_GPL(lm3533_ctrlbank_set_max_current);
  74. #define lm3533_ctrlbank_set(_name, _NAME) \
  75. int lm3533_ctrlbank_set_##_name(struct lm3533_ctrlbank *cb, u8 val) \
  76. { \
  77. u8 reg; \
  78. int ret; \
  79. \
  80. if (val > LM3533_##_NAME##_MAX) \
  81. return -EINVAL; \
  82. \
  83. reg = lm3533_ctrlbank_get_reg(cb, LM3533_REG_##_NAME##_BASE); \
  84. ret = lm3533_write(cb->lm3533, reg, val); \
  85. if (ret) \
  86. dev_err(cb->dev, "failed to set " #_name "\n"); \
  87. \
  88. return ret; \
  89. } \
  90. EXPORT_SYMBOL_GPL(lm3533_ctrlbank_set_##_name);
  91. #define lm3533_ctrlbank_get(_name, _NAME) \
  92. int lm3533_ctrlbank_get_##_name(struct lm3533_ctrlbank *cb, u8 *val) \
  93. { \
  94. u8 reg; \
  95. int ret; \
  96. \
  97. reg = lm3533_ctrlbank_get_reg(cb, LM3533_REG_##_NAME##_BASE); \
  98. ret = lm3533_read(cb->lm3533, reg, val); \
  99. if (ret) \
  100. dev_err(cb->dev, "failed to get " #_name "\n"); \
  101. \
  102. return ret; \
  103. } \
  104. EXPORT_SYMBOL_GPL(lm3533_ctrlbank_get_##_name);
  105. lm3533_ctrlbank_set(brightness, BRIGHTNESS);
  106. lm3533_ctrlbank_get(brightness, BRIGHTNESS);
  107. /*
  108. * PWM-input control mask:
  109. *
  110. * bit 5 - PWM-input enabled in Zone 4
  111. * bit 4 - PWM-input enabled in Zone 3
  112. * bit 3 - PWM-input enabled in Zone 2
  113. * bit 2 - PWM-input enabled in Zone 1
  114. * bit 1 - PWM-input enabled in Zone 0
  115. * bit 0 - PWM-input enabled
  116. */
  117. lm3533_ctrlbank_set(pwm, PWM);
  118. lm3533_ctrlbank_get(pwm, PWM);
  119. MODULE_AUTHOR("Johan Hovold <jhovold@gmail.com>");
  120. MODULE_DESCRIPTION("LM3533 Control Bank interface");
  121. MODULE_LICENSE("GPL");