pwm-cros-ec.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /*
  2. * Copyright (C) 2016 Google, Inc
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms of the GNU General Public License version 2, as published by
  6. * the Free Software Foundation.
  7. *
  8. * Expose a PWM controlled by the ChromeOS EC to the host processor.
  9. */
  10. #include <linux/module.h>
  11. #include <linux/mfd/cros_ec.h>
  12. #include <linux/mfd/cros_ec_commands.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/pwm.h>
  15. #include <linux/slab.h>
  16. /**
  17. * struct cros_ec_pwm_device - Driver data for EC PWM
  18. *
  19. * @dev: Device node
  20. * @ec: Pointer to EC device
  21. * @chip: PWM controller chip
  22. */
  23. struct cros_ec_pwm_device {
  24. struct device *dev;
  25. struct cros_ec_device *ec;
  26. struct pwm_chip chip;
  27. };
  28. static inline struct cros_ec_pwm_device *pwm_to_cros_ec_pwm(struct pwm_chip *c)
  29. {
  30. return container_of(c, struct cros_ec_pwm_device, chip);
  31. }
  32. static int cros_ec_pwm_set_duty(struct cros_ec_device *ec, u8 index, u16 duty)
  33. {
  34. struct {
  35. struct cros_ec_command msg;
  36. struct ec_params_pwm_set_duty params;
  37. } __packed buf;
  38. struct ec_params_pwm_set_duty *params = &buf.params;
  39. struct cros_ec_command *msg = &buf.msg;
  40. memset(&buf, 0, sizeof(buf));
  41. msg->version = 0;
  42. msg->command = EC_CMD_PWM_SET_DUTY;
  43. msg->insize = 0;
  44. msg->outsize = sizeof(*params);
  45. params->duty = duty;
  46. params->pwm_type = EC_PWM_TYPE_GENERIC;
  47. params->index = index;
  48. return cros_ec_cmd_xfer_status(ec, msg);
  49. }
  50. static int __cros_ec_pwm_get_duty(struct cros_ec_device *ec, u8 index,
  51. u32 *result)
  52. {
  53. struct {
  54. struct cros_ec_command msg;
  55. union {
  56. struct ec_params_pwm_get_duty params;
  57. struct ec_response_pwm_get_duty resp;
  58. };
  59. } __packed buf;
  60. struct ec_params_pwm_get_duty *params = &buf.params;
  61. struct ec_response_pwm_get_duty *resp = &buf.resp;
  62. struct cros_ec_command *msg = &buf.msg;
  63. int ret;
  64. memset(&buf, 0, sizeof(buf));
  65. msg->version = 0;
  66. msg->command = EC_CMD_PWM_GET_DUTY;
  67. msg->insize = sizeof(*params);
  68. msg->outsize = sizeof(*resp);
  69. params->pwm_type = EC_PWM_TYPE_GENERIC;
  70. params->index = index;
  71. ret = cros_ec_cmd_xfer_status(ec, msg);
  72. if (result)
  73. *result = msg->result;
  74. if (ret < 0)
  75. return ret;
  76. return resp->duty;
  77. }
  78. static int cros_ec_pwm_get_duty(struct cros_ec_device *ec, u8 index)
  79. {
  80. return __cros_ec_pwm_get_duty(ec, index, NULL);
  81. }
  82. static int cros_ec_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
  83. struct pwm_state *state)
  84. {
  85. struct cros_ec_pwm_device *ec_pwm = pwm_to_cros_ec_pwm(chip);
  86. int duty_cycle;
  87. /* The EC won't let us change the period */
  88. if (state->period != EC_PWM_MAX_DUTY)
  89. return -EINVAL;
  90. /*
  91. * EC doesn't separate the concept of duty cycle and enabled, but
  92. * kernel does. Translate.
  93. */
  94. duty_cycle = state->enabled ? state->duty_cycle : 0;
  95. return cros_ec_pwm_set_duty(ec_pwm->ec, pwm->hwpwm, duty_cycle);
  96. }
  97. static void cros_ec_pwm_get_state(struct pwm_chip *chip, struct pwm_device *pwm,
  98. struct pwm_state *state)
  99. {
  100. struct cros_ec_pwm_device *ec_pwm = pwm_to_cros_ec_pwm(chip);
  101. int ret;
  102. ret = cros_ec_pwm_get_duty(ec_pwm->ec, pwm->hwpwm);
  103. if (ret < 0) {
  104. dev_err(chip->dev, "error getting initial duty: %d\n", ret);
  105. return;
  106. }
  107. state->enabled = (ret > 0);
  108. state->period = EC_PWM_MAX_DUTY;
  109. /* Note that "disabled" and "duty cycle == 0" are treated the same */
  110. state->duty_cycle = ret;
  111. }
  112. static struct pwm_device *
  113. cros_ec_pwm_xlate(struct pwm_chip *pc, const struct of_phandle_args *args)
  114. {
  115. struct pwm_device *pwm;
  116. if (args->args[0] >= pc->npwm)
  117. return ERR_PTR(-EINVAL);
  118. pwm = pwm_request_from_chip(pc, args->args[0], NULL);
  119. if (IS_ERR(pwm))
  120. return pwm;
  121. /* The EC won't let us change the period */
  122. pwm->args.period = EC_PWM_MAX_DUTY;
  123. return pwm;
  124. }
  125. static const struct pwm_ops cros_ec_pwm_ops = {
  126. .get_state = cros_ec_pwm_get_state,
  127. .apply = cros_ec_pwm_apply,
  128. .owner = THIS_MODULE,
  129. };
  130. static int cros_ec_num_pwms(struct cros_ec_device *ec)
  131. {
  132. int i, ret;
  133. /* The index field is only 8 bits */
  134. for (i = 0; i <= U8_MAX; i++) {
  135. u32 result = 0;
  136. ret = __cros_ec_pwm_get_duty(ec, i, &result);
  137. /* We want to parse EC protocol errors */
  138. if (ret < 0 && !(ret == -EPROTO && result))
  139. return ret;
  140. /*
  141. * We look for SUCCESS, INVALID_COMMAND, or INVALID_PARAM
  142. * responses; everything else is treated as an error.
  143. */
  144. if (result == EC_RES_INVALID_COMMAND)
  145. return -ENODEV;
  146. else if (result == EC_RES_INVALID_PARAM)
  147. return i;
  148. else if (result)
  149. return -EPROTO;
  150. }
  151. return U8_MAX;
  152. }
  153. static int cros_ec_pwm_probe(struct platform_device *pdev)
  154. {
  155. struct cros_ec_device *ec = dev_get_drvdata(pdev->dev.parent);
  156. struct device *dev = &pdev->dev;
  157. struct cros_ec_pwm_device *ec_pwm;
  158. struct pwm_chip *chip;
  159. int ret;
  160. if (!ec) {
  161. dev_err(dev, "no parent EC device\n");
  162. return -EINVAL;
  163. }
  164. ec_pwm = devm_kzalloc(dev, sizeof(*ec_pwm), GFP_KERNEL);
  165. if (!ec_pwm)
  166. return -ENOMEM;
  167. chip = &ec_pwm->chip;
  168. ec_pwm->ec = ec;
  169. /* PWM chip */
  170. chip->dev = dev;
  171. chip->ops = &cros_ec_pwm_ops;
  172. chip->of_xlate = cros_ec_pwm_xlate;
  173. chip->of_pwm_n_cells = 1;
  174. chip->base = -1;
  175. ret = cros_ec_num_pwms(ec);
  176. if (ret < 0) {
  177. dev_err(dev, "Couldn't find PWMs: %d\n", ret);
  178. return ret;
  179. }
  180. chip->npwm = ret;
  181. dev_dbg(dev, "Probed %u PWMs\n", chip->npwm);
  182. ret = pwmchip_add(chip);
  183. if (ret < 0) {
  184. dev_err(dev, "cannot register PWM: %d\n", ret);
  185. return ret;
  186. }
  187. platform_set_drvdata(pdev, ec_pwm);
  188. return ret;
  189. }
  190. static int cros_ec_pwm_remove(struct platform_device *dev)
  191. {
  192. struct cros_ec_pwm_device *ec_pwm = platform_get_drvdata(dev);
  193. struct pwm_chip *chip = &ec_pwm->chip;
  194. return pwmchip_remove(chip);
  195. }
  196. #ifdef CONFIG_OF
  197. static const struct of_device_id cros_ec_pwm_of_match[] = {
  198. { .compatible = "google,cros-ec-pwm" },
  199. {},
  200. };
  201. MODULE_DEVICE_TABLE(of, cros_ec_pwm_of_match);
  202. #endif
  203. static struct platform_driver cros_ec_pwm_driver = {
  204. .probe = cros_ec_pwm_probe,
  205. .remove = cros_ec_pwm_remove,
  206. .driver = {
  207. .name = "cros-ec-pwm",
  208. .of_match_table = of_match_ptr(cros_ec_pwm_of_match),
  209. },
  210. };
  211. module_platform_driver(cros_ec_pwm_driver);
  212. MODULE_ALIAS("platform:cros-ec-pwm");
  213. MODULE_DESCRIPTION("ChromeOS EC PWM driver");
  214. MODULE_LICENSE("GPL v2");