pwm-twl-led.c 8.6 KB

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