pwm-twl.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. /*
  2. * Driver for TWL4030/6030 Generic Pulse Width Modulator
  3. *
  4. * Copyright (C) 2012 Texas Instruments
  5. * Author: Peter Ujfalusi <peter.ujfalusi@ti.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License version 2 as published by
  9. * the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful, but WITHOUT
  12. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  14. * more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along with
  17. * this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include <linux/module.h>
  20. #include <linux/of.h>
  21. #include <linux/platform_device.h>
  22. #include <linux/pwm.h>
  23. #include <linux/i2c/twl.h>
  24. #include <linux/slab.h>
  25. /*
  26. * This driver handles the PWMs of TWL4030 and TWL6030.
  27. * The TRM names for the PWMs on TWL4030 are: PWM0, PWM1
  28. * TWL6030 also have two PWMs named in the TRM as PWM1, PWM2
  29. */
  30. #define TWL_PWM_MAX 0x7f
  31. /* Registers, bits and macro for TWL4030 */
  32. #define TWL4030_GPBR1_REG 0x0c
  33. #define TWL4030_PMBR1_REG 0x0d
  34. /* GPBR1 register bits */
  35. #define TWL4030_PWMXCLK_ENABLE (1 << 0)
  36. #define TWL4030_PWMX_ENABLE (1 << 2)
  37. #define TWL4030_PWMX_BITS (TWL4030_PWMX_ENABLE | TWL4030_PWMXCLK_ENABLE)
  38. #define TWL4030_PWM_TOGGLE(pwm, x) ((x) << (pwm))
  39. /* PMBR1 register bits */
  40. #define TWL4030_GPIO6_PWM0_MUTE_MASK (0x03 << 2)
  41. #define TWL4030_GPIO6_PWM0_MUTE_PWM0 (0x01 << 2)
  42. #define TWL4030_GPIO7_VIBRASYNC_PWM1_MASK (0x03 << 4)
  43. #define TWL4030_GPIO7_VIBRASYNC_PWM1_PWM1 (0x03 << 4)
  44. /* Register, bits and macro for TWL6030 */
  45. #define TWL6030_TOGGLE3_REG 0x92
  46. #define TWL6030_PWMXR (1 << 0)
  47. #define TWL6030_PWMXS (1 << 1)
  48. #define TWL6030_PWMXEN (1 << 2)
  49. #define TWL6030_PWM_TOGGLE(pwm, x) ((x) << (pwm * 3))
  50. struct twl_pwm_chip {
  51. struct pwm_chip chip;
  52. struct mutex mutex;
  53. u8 twl6030_toggle3;
  54. u8 twl4030_pwm_mux;
  55. };
  56. static inline struct twl_pwm_chip *to_twl(struct pwm_chip *chip)
  57. {
  58. return container_of(chip, struct twl_pwm_chip, chip);
  59. }
  60. static int twl_pwm_config(struct pwm_chip *chip, struct pwm_device *pwm,
  61. int duty_ns, int period_ns)
  62. {
  63. int duty_cycle = DIV_ROUND_UP(duty_ns * TWL_PWM_MAX, period_ns) + 1;
  64. u8 pwm_config[2] = { 1, 0 };
  65. int base, ret;
  66. /*
  67. * To configure the duty period:
  68. * On-cycle is set to 1 (the minimum allowed value)
  69. * The off time of 0 is not configurable, so the mapping is:
  70. * 0 -> off cycle = 2,
  71. * 1 -> off cycle = 2,
  72. * 2 -> off cycle = 3,
  73. * 126 - > off cycle 127,
  74. * 127 - > off cycle 1
  75. * When on cycle == off cycle the PWM will be always on
  76. */
  77. if (duty_cycle == 1)
  78. duty_cycle = 2;
  79. else if (duty_cycle > TWL_PWM_MAX)
  80. duty_cycle = 1;
  81. base = pwm->hwpwm * 3;
  82. pwm_config[1] = duty_cycle;
  83. ret = twl_i2c_write(TWL_MODULE_PWM, pwm_config, base, 2);
  84. if (ret < 0)
  85. dev_err(chip->dev, "%s: Failed to configure PWM\n", pwm->label);
  86. return ret;
  87. }
  88. static int twl4030_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm)
  89. {
  90. struct twl_pwm_chip *twl = to_twl(chip);
  91. int ret;
  92. u8 val;
  93. mutex_lock(&twl->mutex);
  94. ret = twl_i2c_read_u8(TWL4030_MODULE_INTBR, &val, TWL4030_GPBR1_REG);
  95. if (ret < 0) {
  96. dev_err(chip->dev, "%s: Failed to read GPBR1\n", pwm->label);
  97. goto out;
  98. }
  99. val |= TWL4030_PWM_TOGGLE(pwm->hwpwm, TWL4030_PWMXCLK_ENABLE);
  100. ret = twl_i2c_write_u8(TWL4030_MODULE_INTBR, val, TWL4030_GPBR1_REG);
  101. if (ret < 0)
  102. dev_err(chip->dev, "%s: Failed to enable PWM\n", pwm->label);
  103. val |= TWL4030_PWM_TOGGLE(pwm->hwpwm, TWL4030_PWMX_ENABLE);
  104. ret = twl_i2c_write_u8(TWL4030_MODULE_INTBR, val, TWL4030_GPBR1_REG);
  105. if (ret < 0)
  106. dev_err(chip->dev, "%s: Failed to enable PWM\n", pwm->label);
  107. out:
  108. mutex_unlock(&twl->mutex);
  109. return ret;
  110. }
  111. static void twl4030_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm)
  112. {
  113. struct twl_pwm_chip *twl = to_twl(chip);
  114. int ret;
  115. u8 val;
  116. mutex_lock(&twl->mutex);
  117. ret = twl_i2c_read_u8(TWL4030_MODULE_INTBR, &val, TWL4030_GPBR1_REG);
  118. if (ret < 0) {
  119. dev_err(chip->dev, "%s: Failed to read GPBR1\n", pwm->label);
  120. goto out;
  121. }
  122. val &= ~TWL4030_PWM_TOGGLE(pwm->hwpwm, TWL4030_PWMX_ENABLE);
  123. ret = twl_i2c_write_u8(TWL4030_MODULE_INTBR, val, TWL4030_GPBR1_REG);
  124. if (ret < 0)
  125. dev_err(chip->dev, "%s: Failed to disable PWM\n", pwm->label);
  126. val &= ~TWL4030_PWM_TOGGLE(pwm->hwpwm, TWL4030_PWMXCLK_ENABLE);
  127. ret = twl_i2c_write_u8(TWL4030_MODULE_INTBR, val, TWL4030_GPBR1_REG);
  128. if (ret < 0)
  129. dev_err(chip->dev, "%s: Failed to disable PWM\n", pwm->label);
  130. out:
  131. mutex_unlock(&twl->mutex);
  132. }
  133. static int twl4030_pwm_request(struct pwm_chip *chip, struct pwm_device *pwm)
  134. {
  135. struct twl_pwm_chip *twl = to_twl(chip);
  136. int ret;
  137. u8 val, mask, bits;
  138. if (pwm->hwpwm == 1) {
  139. mask = TWL4030_GPIO7_VIBRASYNC_PWM1_MASK;
  140. bits = TWL4030_GPIO7_VIBRASYNC_PWM1_PWM1;
  141. } else {
  142. mask = TWL4030_GPIO6_PWM0_MUTE_MASK;
  143. bits = TWL4030_GPIO6_PWM0_MUTE_PWM0;
  144. }
  145. mutex_lock(&twl->mutex);
  146. ret = twl_i2c_read_u8(TWL4030_MODULE_INTBR, &val, TWL4030_PMBR1_REG);
  147. if (ret < 0) {
  148. dev_err(chip->dev, "%s: Failed to read PMBR1\n", pwm->label);
  149. goto out;
  150. }
  151. /* Save the current MUX configuration for the PWM */
  152. twl->twl4030_pwm_mux &= ~mask;
  153. twl->twl4030_pwm_mux |= (val & mask);
  154. /* Select PWM functionality */
  155. val &= ~mask;
  156. val |= bits;
  157. ret = twl_i2c_write_u8(TWL4030_MODULE_INTBR, val, TWL4030_PMBR1_REG);
  158. if (ret < 0)
  159. dev_err(chip->dev, "%s: Failed to request PWM\n", pwm->label);
  160. out:
  161. mutex_unlock(&twl->mutex);
  162. return ret;
  163. }
  164. static void twl4030_pwm_free(struct pwm_chip *chip, struct pwm_device *pwm)
  165. {
  166. struct twl_pwm_chip *twl = to_twl(chip);
  167. int ret;
  168. u8 val, mask;
  169. if (pwm->hwpwm == 1)
  170. mask = TWL4030_GPIO7_VIBRASYNC_PWM1_MASK;
  171. else
  172. mask = TWL4030_GPIO6_PWM0_MUTE_MASK;
  173. mutex_lock(&twl->mutex);
  174. ret = twl_i2c_read_u8(TWL4030_MODULE_INTBR, &val, TWL4030_PMBR1_REG);
  175. if (ret < 0) {
  176. dev_err(chip->dev, "%s: Failed to read PMBR1\n", pwm->label);
  177. goto out;
  178. }
  179. /* Restore the MUX configuration for the PWM */
  180. val &= ~mask;
  181. val |= (twl->twl4030_pwm_mux & mask);
  182. ret = twl_i2c_write_u8(TWL4030_MODULE_INTBR, val, TWL4030_PMBR1_REG);
  183. if (ret < 0)
  184. dev_err(chip->dev, "%s: Failed to free PWM\n", pwm->label);
  185. out:
  186. mutex_unlock(&twl->mutex);
  187. }
  188. static int twl6030_pwm_enable(struct pwm_chip *chip, struct pwm_device *pwm)
  189. {
  190. struct twl_pwm_chip *twl = to_twl(chip);
  191. int ret;
  192. u8 val;
  193. mutex_lock(&twl->mutex);
  194. val = twl->twl6030_toggle3;
  195. val |= TWL6030_PWM_TOGGLE(pwm->hwpwm, TWL6030_PWMXS | TWL6030_PWMXEN);
  196. val &= ~TWL6030_PWM_TOGGLE(pwm->hwpwm, TWL6030_PWMXR);
  197. ret = twl_i2c_write_u8(TWL6030_MODULE_ID1, val, TWL6030_TOGGLE3_REG);
  198. if (ret < 0) {
  199. dev_err(chip->dev, "%s: Failed to enable PWM\n", pwm->label);
  200. goto out;
  201. }
  202. twl->twl6030_toggle3 = val;
  203. out:
  204. mutex_unlock(&twl->mutex);
  205. return ret;
  206. }
  207. static void twl6030_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm)
  208. {
  209. struct twl_pwm_chip *twl = to_twl(chip);
  210. int ret;
  211. u8 val;
  212. mutex_lock(&twl->mutex);
  213. val = twl->twl6030_toggle3;
  214. val |= TWL6030_PWM_TOGGLE(pwm->hwpwm, TWL6030_PWMXR);
  215. val &= ~TWL6030_PWM_TOGGLE(pwm->hwpwm, TWL6030_PWMXS | TWL6030_PWMXEN);
  216. ret = twl_i2c_write_u8(TWL6030_MODULE_ID1, val, TWL6030_TOGGLE3_REG);
  217. if (ret < 0) {
  218. dev_err(chip->dev, "%s: Failed to disable PWM\n", pwm->label);
  219. goto out;
  220. }
  221. val |= TWL6030_PWM_TOGGLE(pwm->hwpwm, TWL6030_PWMXEN);
  222. ret = twl_i2c_write_u8(TWL6030_MODULE_ID1, val, TWL6030_TOGGLE3_REG);
  223. if (ret < 0) {
  224. dev_err(chip->dev, "%s: Failed to disable PWM\n", pwm->label);
  225. goto out;
  226. }
  227. val &= ~TWL6030_PWM_TOGGLE(pwm->hwpwm, TWL6030_PWMXEN);
  228. ret = twl_i2c_write_u8(TWL6030_MODULE_ID1, val, TWL6030_TOGGLE3_REG);
  229. if (ret < 0) {
  230. dev_err(chip->dev, "%s: Failed to disable PWM\n", pwm->label);
  231. goto out;
  232. }
  233. twl->twl6030_toggle3 = val;
  234. out:
  235. mutex_unlock(&twl->mutex);
  236. }
  237. static const struct pwm_ops twl4030_pwm_ops = {
  238. .config = twl_pwm_config,
  239. .enable = twl4030_pwm_enable,
  240. .disable = twl4030_pwm_disable,
  241. .request = twl4030_pwm_request,
  242. .free = twl4030_pwm_free,
  243. .owner = THIS_MODULE,
  244. };
  245. static const struct pwm_ops twl6030_pwm_ops = {
  246. .config = twl_pwm_config,
  247. .enable = twl6030_pwm_enable,
  248. .disable = twl6030_pwm_disable,
  249. .owner = THIS_MODULE,
  250. };
  251. static int twl_pwm_probe(struct platform_device *pdev)
  252. {
  253. struct twl_pwm_chip *twl;
  254. int ret;
  255. twl = devm_kzalloc(&pdev->dev, sizeof(*twl), GFP_KERNEL);
  256. if (!twl)
  257. return -ENOMEM;
  258. if (twl_class_is_4030())
  259. twl->chip.ops = &twl4030_pwm_ops;
  260. else
  261. twl->chip.ops = &twl6030_pwm_ops;
  262. twl->chip.dev = &pdev->dev;
  263. twl->chip.base = -1;
  264. twl->chip.npwm = 2;
  265. twl->chip.can_sleep = true;
  266. mutex_init(&twl->mutex);
  267. ret = pwmchip_add(&twl->chip);
  268. if (ret < 0)
  269. return ret;
  270. platform_set_drvdata(pdev, twl);
  271. return 0;
  272. }
  273. static int twl_pwm_remove(struct platform_device *pdev)
  274. {
  275. struct twl_pwm_chip *twl = platform_get_drvdata(pdev);
  276. return pwmchip_remove(&twl->chip);
  277. }
  278. #ifdef CONFIG_OF
  279. static const struct of_device_id twl_pwm_of_match[] = {
  280. { .compatible = "ti,twl4030-pwm" },
  281. { .compatible = "ti,twl6030-pwm" },
  282. { },
  283. };
  284. MODULE_DEVICE_TABLE(of, twl_pwm_of_match);
  285. #endif
  286. static struct platform_driver twl_pwm_driver = {
  287. .driver = {
  288. .name = "twl-pwm",
  289. .of_match_table = of_match_ptr(twl_pwm_of_match),
  290. },
  291. .probe = twl_pwm_probe,
  292. .remove = twl_pwm_remove,
  293. };
  294. module_platform_driver(twl_pwm_driver);
  295. MODULE_AUTHOR("Peter Ujfalusi <peter.ujfalusi@ti.com>");
  296. MODULE_DESCRIPTION("PWM driver for TWL4030 and TWL6030");
  297. MODULE_ALIAS("platform:twl-pwm");
  298. MODULE_LICENSE("GPL");