pwm-twl-led.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. /*
  2. * Driver for TWL4030/6030 Pulse Width Modulator used as LED driver
  3. *
  4. * Copyright (C) 2012 Texas Instruments
  5. * Author: Peter Ujfalusi <peter.ujfalusi@ti.com>
  6. *
  7. * This driver is a complete rewrite of the former pwm-twl6030.c authorded by:
  8. * Hemanth V <hemanthv@ti.com>
  9. *
  10. * This program is free software; you can redistribute it and/or modify it
  11. * under the terms of the GNU General Public License version 2 as published by
  12. * the Free Software Foundation.
  13. *
  14. * This program is distributed in the hope that it will be useful, but WITHOUT
  15. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  16. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  17. * more details.
  18. *
  19. * You should have received a copy of the GNU General Public License along with
  20. * this program. If not, see <http://www.gnu.org/licenses/>.
  21. */
  22. #include <linux/module.h>
  23. #include <linux/platform_device.h>
  24. #include <linux/pwm.h>
  25. #include <linux/i2c/twl.h>
  26. #include <linux/slab.h>
  27. /*
  28. * This driver handles the PWM driven LED terminals of TWL4030 and TWL6030.
  29. * To generate the signal on TWL4030:
  30. * - LEDA uses PWMA
  31. * - LEDB uses PWMB
  32. * TWL6030 has one LED pin with dedicated LEDPWM
  33. */
  34. #define TWL4030_LED_MAX 0x7f
  35. #define TWL6030_LED_MAX 0xff
  36. /* Registers, bits and macro for TWL4030 */
  37. #define TWL4030_LEDEN_REG 0x00
  38. #define TWL4030_PWMA_REG 0x01
  39. #define TWL4030_LEDXON (1 << 0)
  40. #define TWL4030_LEDXPWM (1 << 4)
  41. #define TWL4030_LED_PINS (TWL4030_LEDXON | TWL4030_LEDXPWM)
  42. #define TWL4030_LED_TOGGLE(led, x) ((x) << (led))
  43. /* Register, bits and macro for TWL6030 */
  44. #define TWL6030_LED_PWM_CTRL1 0xf4
  45. #define TWL6030_LED_PWM_CTRL2 0xf5
  46. #define TWL6040_LED_MODE_HW 0x00
  47. #define TWL6040_LED_MODE_ON 0x01
  48. #define TWL6040_LED_MODE_OFF 0x02
  49. #define TWL6040_LED_MODE_MASK 0x03
  50. struct twl_pwmled_chip {
  51. struct pwm_chip chip;
  52. struct mutex mutex;
  53. };
  54. static inline struct twl_pwmled_chip *to_twl(struct pwm_chip *chip)
  55. {
  56. return container_of(chip, struct twl_pwmled_chip, chip);
  57. }
  58. static int twl4030_pwmled_config(struct pwm_chip *chip, struct pwm_device *pwm,
  59. int duty_ns, int period_ns)
  60. {
  61. int duty_cycle = DIV_ROUND_UP(duty_ns * TWL4030_LED_MAX, period_ns) + 1;
  62. u8 pwm_config[2] = { 1, 0 };
  63. int base, ret;
  64. /*
  65. * To configure the duty period:
  66. * On-cycle is set to 1 (the minimum allowed value)
  67. * The off time of 0 is not configurable, so the mapping is:
  68. * 0 -> off cycle = 2,
  69. * 1 -> off cycle = 2,
  70. * 2 -> off cycle = 3,
  71. * 126 - > off cycle 127,
  72. * 127 - > off cycle 1
  73. * When on cycle == off cycle the PWM will be always on
  74. */
  75. if (duty_cycle == 1)
  76. duty_cycle = 2;
  77. else if (duty_cycle > TWL4030_LED_MAX)
  78. duty_cycle = 1;
  79. base = pwm->hwpwm * 2 + TWL4030_PWMA_REG;
  80. pwm_config[1] = duty_cycle;
  81. ret = twl_i2c_write(TWL4030_MODULE_LED, pwm_config, base, 2);
  82. if (ret < 0)
  83. dev_err(chip->dev, "%s: Failed to configure PWM\n", pwm->label);
  84. return ret;
  85. }
  86. static int twl4030_pwmled_enable(struct pwm_chip *chip, struct pwm_device *pwm)
  87. {
  88. struct twl_pwmled_chip *twl = to_twl(chip);
  89. int ret;
  90. u8 val;
  91. mutex_lock(&twl->mutex);
  92. ret = twl_i2c_read_u8(TWL4030_MODULE_LED, &val, TWL4030_LEDEN_REG);
  93. if (ret < 0) {
  94. dev_err(chip->dev, "%s: Failed to read LEDEN\n", pwm->label);
  95. goto out;
  96. }
  97. val |= TWL4030_LED_TOGGLE(pwm->hwpwm, TWL4030_LED_PINS);
  98. ret = twl_i2c_write_u8(TWL4030_MODULE_LED, val, TWL4030_LEDEN_REG);
  99. if (ret < 0)
  100. dev_err(chip->dev, "%s: Failed to enable PWM\n", pwm->label);
  101. out:
  102. mutex_unlock(&twl->mutex);
  103. return ret;
  104. }
  105. static void twl4030_pwmled_disable(struct pwm_chip *chip,
  106. struct pwm_device *pwm)
  107. {
  108. struct twl_pwmled_chip *twl = to_twl(chip);
  109. int ret;
  110. u8 val;
  111. mutex_lock(&twl->mutex);
  112. ret = twl_i2c_read_u8(TWL4030_MODULE_LED, &val, TWL4030_LEDEN_REG);
  113. if (ret < 0) {
  114. dev_err(chip->dev, "%s: Failed to read LEDEN\n", pwm->label);
  115. goto out;
  116. }
  117. val &= ~TWL4030_LED_TOGGLE(pwm->hwpwm, TWL4030_LED_PINS);
  118. ret = twl_i2c_write_u8(TWL4030_MODULE_LED, val, TWL4030_LEDEN_REG);
  119. if (ret < 0)
  120. dev_err(chip->dev, "%s: Failed to disable PWM\n", pwm->label);
  121. out:
  122. mutex_unlock(&twl->mutex);
  123. }
  124. static int twl6030_pwmled_config(struct pwm_chip *chip, struct pwm_device *pwm,
  125. int duty_ns, int period_ns)
  126. {
  127. int duty_cycle = (duty_ns * TWL6030_LED_MAX) / period_ns;
  128. u8 on_time;
  129. int ret;
  130. on_time = duty_cycle & 0xff;
  131. ret = twl_i2c_write_u8(TWL6030_MODULE_ID1, on_time,
  132. TWL6030_LED_PWM_CTRL1);
  133. if (ret < 0)
  134. dev_err(chip->dev, "%s: Failed to configure PWM\n", pwm->label);
  135. return ret;
  136. }
  137. static int twl6030_pwmled_enable(struct pwm_chip *chip, struct pwm_device *pwm)
  138. {
  139. struct twl_pwmled_chip *twl = to_twl(chip);
  140. int ret;
  141. u8 val;
  142. mutex_lock(&twl->mutex);
  143. ret = twl_i2c_read_u8(TWL6030_MODULE_ID1, &val, TWL6030_LED_PWM_CTRL2);
  144. if (ret < 0) {
  145. dev_err(chip->dev, "%s: Failed to read PWM_CTRL2\n",
  146. pwm->label);
  147. goto out;
  148. }
  149. val &= ~TWL6040_LED_MODE_MASK;
  150. val |= TWL6040_LED_MODE_ON;
  151. ret = twl_i2c_write_u8(TWL6030_MODULE_ID1, val, TWL6030_LED_PWM_CTRL2);
  152. if (ret < 0)
  153. dev_err(chip->dev, "%s: Failed to enable PWM\n", pwm->label);
  154. out:
  155. mutex_unlock(&twl->mutex);
  156. return ret;
  157. }
  158. static void twl6030_pwmled_disable(struct pwm_chip *chip,
  159. struct pwm_device *pwm)
  160. {
  161. struct twl_pwmled_chip *twl = to_twl(chip);
  162. int ret;
  163. u8 val;
  164. mutex_lock(&twl->mutex);
  165. ret = twl_i2c_read_u8(TWL6030_MODULE_ID1, &val, TWL6030_LED_PWM_CTRL2);
  166. if (ret < 0) {
  167. dev_err(chip->dev, "%s: Failed to read PWM_CTRL2\n",
  168. pwm->label);
  169. goto out;
  170. }
  171. val &= ~TWL6040_LED_MODE_MASK;
  172. val |= TWL6040_LED_MODE_OFF;
  173. ret = twl_i2c_write_u8(TWL6030_MODULE_ID1, val, TWL6030_LED_PWM_CTRL2);
  174. if (ret < 0)
  175. dev_err(chip->dev, "%s: Failed to disable PWM\n", pwm->label);
  176. out:
  177. mutex_unlock(&twl->mutex);
  178. }
  179. static int twl6030_pwmled_request(struct pwm_chip *chip, struct pwm_device *pwm)
  180. {
  181. struct twl_pwmled_chip *twl = to_twl(chip);
  182. int ret;
  183. u8 val;
  184. mutex_lock(&twl->mutex);
  185. ret = twl_i2c_read_u8(TWL6030_MODULE_ID1, &val, TWL6030_LED_PWM_CTRL2);
  186. if (ret < 0) {
  187. dev_err(chip->dev, "%s: Failed to read PWM_CTRL2\n",
  188. pwm->label);
  189. goto out;
  190. }
  191. val &= ~TWL6040_LED_MODE_MASK;
  192. val |= TWL6040_LED_MODE_OFF;
  193. ret = twl_i2c_write_u8(TWL6030_MODULE_ID1, val, TWL6030_LED_PWM_CTRL2);
  194. if (ret < 0)
  195. dev_err(chip->dev, "%s: Failed to request PWM\n", pwm->label);
  196. out:
  197. mutex_unlock(&twl->mutex);
  198. return ret;
  199. }
  200. static void twl6030_pwmled_free(struct pwm_chip *chip, struct pwm_device *pwm)
  201. {
  202. struct twl_pwmled_chip *twl = to_twl(chip);
  203. int ret;
  204. u8 val;
  205. mutex_lock(&twl->mutex);
  206. ret = twl_i2c_read_u8(TWL6030_MODULE_ID1, &val, TWL6030_LED_PWM_CTRL2);
  207. if (ret < 0) {
  208. dev_err(chip->dev, "%s: Failed to read PWM_CTRL2\n",
  209. pwm->label);
  210. goto out;
  211. }
  212. val &= ~TWL6040_LED_MODE_MASK;
  213. val |= TWL6040_LED_MODE_HW;
  214. ret = twl_i2c_write_u8(TWL6030_MODULE_ID1, val, TWL6030_LED_PWM_CTRL2);
  215. if (ret < 0)
  216. dev_err(chip->dev, "%s: Failed to free PWM\n", pwm->label);
  217. out:
  218. mutex_unlock(&twl->mutex);
  219. }
  220. static const struct pwm_ops twl4030_pwmled_ops = {
  221. .enable = twl4030_pwmled_enable,
  222. .disable = twl4030_pwmled_disable,
  223. .config = twl4030_pwmled_config,
  224. };
  225. static const struct pwm_ops twl6030_pwmled_ops = {
  226. .enable = twl6030_pwmled_enable,
  227. .disable = twl6030_pwmled_disable,
  228. .config = twl6030_pwmled_config,
  229. .request = twl6030_pwmled_request,
  230. .free = twl6030_pwmled_free,
  231. };
  232. static int twl_pwmled_probe(struct platform_device *pdev)
  233. {
  234. struct twl_pwmled_chip *twl;
  235. int ret;
  236. twl = devm_kzalloc(&pdev->dev, sizeof(*twl), GFP_KERNEL);
  237. if (!twl)
  238. return -ENOMEM;
  239. if (twl_class_is_4030()) {
  240. twl->chip.ops = &twl4030_pwmled_ops;
  241. twl->chip.npwm = 2;
  242. } else {
  243. twl->chip.ops = &twl6030_pwmled_ops;
  244. twl->chip.npwm = 1;
  245. }
  246. twl->chip.dev = &pdev->dev;
  247. twl->chip.base = -1;
  248. twl->chip.can_sleep = true;
  249. mutex_init(&twl->mutex);
  250. ret = pwmchip_add(&twl->chip);
  251. if (ret < 0)
  252. return ret;
  253. platform_set_drvdata(pdev, twl);
  254. return 0;
  255. }
  256. static int twl_pwmled_remove(struct platform_device *pdev)
  257. {
  258. struct twl_pwmled_chip *twl = platform_get_drvdata(pdev);
  259. return pwmchip_remove(&twl->chip);
  260. }
  261. #ifdef CONFIG_OF
  262. static struct of_device_id twl_pwmled_of_match[] = {
  263. { .compatible = "ti,twl4030-pwmled" },
  264. { .compatible = "ti,twl6030-pwmled" },
  265. { },
  266. };
  267. MODULE_DEVICE_TABLE(of, twl_pwmled_of_match);
  268. #endif
  269. static struct platform_driver twl_pwmled_driver = {
  270. .driver = {
  271. .name = "twl-pwmled",
  272. .of_match_table = of_match_ptr(twl_pwmled_of_match),
  273. },
  274. .probe = twl_pwmled_probe,
  275. .remove = twl_pwmled_remove,
  276. };
  277. module_platform_driver(twl_pwmled_driver);
  278. MODULE_AUTHOR("Peter Ujfalusi <peter.ujfalusi@ti.com>");
  279. MODULE_DESCRIPTION("PWM driver for TWL4030 and TWL6030 LED outputs");
  280. MODULE_ALIAS("platform:twl-pwmled");
  281. MODULE_LICENSE("GPL");