pwm-cros-ec.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Expose a PWM controlled by the ChromeOS EC to the host processor.
  4. *
  5. * Copyright (C) 2016 Google, Inc.
  6. */
  7. #include <linux/module.h>
  8. #include <linux/platform_data/cros_ec_commands.h>
  9. #include <linux/platform_data/cros_ec_proto.h>
  10. #include <linux/platform_device.h>
  11. #include <linux/pwm.h>
  12. #include <linux/slab.h>
  13. /**
  14. * struct cros_ec_pwm_device - Driver data for EC PWM
  15. *
  16. * @dev: Device node
  17. * @ec: Pointer to EC device
  18. * @chip: PWM controller chip
  19. */
  20. struct cros_ec_pwm_device {
  21. struct device *dev;
  22. struct cros_ec_device *ec;
  23. struct pwm_chip chip;
  24. };
  25. /**
  26. * struct cros_ec_pwm - per-PWM driver data
  27. * @duty_cycle: cached duty cycle
  28. */
  29. struct cros_ec_pwm {
  30. u16 duty_cycle;
  31. };
  32. static inline struct cros_ec_pwm_device *pwm_to_cros_ec_pwm(struct pwm_chip *c)
  33. {
  34. return container_of(c, struct cros_ec_pwm_device, chip);
  35. }
  36. static int cros_ec_pwm_request(struct pwm_chip *chip, struct pwm_device *pwm)
  37. {
  38. struct cros_ec_pwm *channel;
  39. channel = kzalloc(sizeof(*channel), GFP_KERNEL);
  40. if (!channel)
  41. return -ENOMEM;
  42. pwm_set_chip_data(pwm, channel);
  43. return 0;
  44. }
  45. static void cros_ec_pwm_free(struct pwm_chip *chip, struct pwm_device *pwm)
  46. {
  47. struct cros_ec_pwm *channel = pwm_get_chip_data(pwm);
  48. kfree(channel);
  49. }
  50. static int cros_ec_pwm_set_duty(struct cros_ec_device *ec, u8 index, u16 duty)
  51. {
  52. struct {
  53. struct cros_ec_command msg;
  54. struct ec_params_pwm_set_duty params;
  55. } __packed buf;
  56. struct ec_params_pwm_set_duty *params = &buf.params;
  57. struct cros_ec_command *msg = &buf.msg;
  58. memset(&buf, 0, sizeof(buf));
  59. msg->version = 0;
  60. msg->command = EC_CMD_PWM_SET_DUTY;
  61. msg->insize = 0;
  62. msg->outsize = sizeof(*params);
  63. params->duty = duty;
  64. params->pwm_type = EC_PWM_TYPE_GENERIC;
  65. params->index = index;
  66. return cros_ec_cmd_xfer_status(ec, msg);
  67. }
  68. static int cros_ec_pwm_get_duty(struct cros_ec_device *ec, u8 index)
  69. {
  70. struct {
  71. struct cros_ec_command msg;
  72. union {
  73. struct ec_params_pwm_get_duty params;
  74. struct ec_response_pwm_get_duty resp;
  75. };
  76. } __packed buf;
  77. struct ec_params_pwm_get_duty *params = &buf.params;
  78. struct ec_response_pwm_get_duty *resp = &buf.resp;
  79. struct cros_ec_command *msg = &buf.msg;
  80. int ret;
  81. memset(&buf, 0, sizeof(buf));
  82. msg->version = 0;
  83. msg->command = EC_CMD_PWM_GET_DUTY;
  84. msg->insize = sizeof(*resp);
  85. msg->outsize = sizeof(*params);
  86. params->pwm_type = EC_PWM_TYPE_GENERIC;
  87. params->index = index;
  88. ret = cros_ec_cmd_xfer_status(ec, msg);
  89. if (ret < 0)
  90. return ret;
  91. return resp->duty;
  92. }
  93. static int cros_ec_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
  94. const struct pwm_state *state)
  95. {
  96. struct cros_ec_pwm_device *ec_pwm = pwm_to_cros_ec_pwm(chip);
  97. struct cros_ec_pwm *channel = pwm_get_chip_data(pwm);
  98. u16 duty_cycle;
  99. int ret;
  100. /* The EC won't let us change the period */
  101. if (state->period != EC_PWM_MAX_DUTY)
  102. return -EINVAL;
  103. /*
  104. * EC doesn't separate the concept of duty cycle and enabled, but
  105. * kernel does. Translate.
  106. */
  107. duty_cycle = state->enabled ? state->duty_cycle : 0;
  108. ret = cros_ec_pwm_set_duty(ec_pwm->ec, pwm->hwpwm, duty_cycle);
  109. if (ret < 0)
  110. return ret;
  111. channel->duty_cycle = state->duty_cycle;
  112. return 0;
  113. }
  114. static void cros_ec_pwm_get_state(struct pwm_chip *chip, struct pwm_device *pwm,
  115. struct pwm_state *state)
  116. {
  117. struct cros_ec_pwm_device *ec_pwm = pwm_to_cros_ec_pwm(chip);
  118. struct cros_ec_pwm *channel = pwm_get_chip_data(pwm);
  119. int ret;
  120. ret = cros_ec_pwm_get_duty(ec_pwm->ec, pwm->hwpwm);
  121. if (ret < 0) {
  122. dev_err(chip->dev, "error getting initial duty: %d\n", ret);
  123. return;
  124. }
  125. state->enabled = (ret > 0);
  126. state->period = EC_PWM_MAX_DUTY;
  127. /*
  128. * Note that "disabled" and "duty cycle == 0" are treated the same. If
  129. * the cached duty cycle is not zero, used the cached duty cycle. This
  130. * ensures that the configured duty cycle is kept across a disable and
  131. * enable operation and avoids potentially confusing consumers.
  132. *
  133. * For the case of the initial hardware readout, channel->duty_cycle
  134. * will be 0 and the actual duty cycle read from the EC is used.
  135. */
  136. if (ret == 0 && channel->duty_cycle > 0)
  137. state->duty_cycle = channel->duty_cycle;
  138. else
  139. state->duty_cycle = ret;
  140. }
  141. static struct pwm_device *
  142. cros_ec_pwm_xlate(struct pwm_chip *pc, const struct of_phandle_args *args)
  143. {
  144. struct pwm_device *pwm;
  145. if (args->args[0] >= pc->npwm)
  146. return ERR_PTR(-EINVAL);
  147. pwm = pwm_request_from_chip(pc, args->args[0], NULL);
  148. if (IS_ERR(pwm))
  149. return pwm;
  150. /* The EC won't let us change the period */
  151. pwm->args.period = EC_PWM_MAX_DUTY;
  152. return pwm;
  153. }
  154. static const struct pwm_ops cros_ec_pwm_ops = {
  155. .request = cros_ec_pwm_request,
  156. .free = cros_ec_pwm_free,
  157. .get_state = cros_ec_pwm_get_state,
  158. .apply = cros_ec_pwm_apply,
  159. .owner = THIS_MODULE,
  160. };
  161. /*
  162. * Determine the number of supported PWMs. The EC does not return the number
  163. * of PWMs it supports directly, so we have to read the pwm duty cycle for
  164. * subsequent channels until we get an error.
  165. */
  166. static int cros_ec_num_pwms(struct cros_ec_device *ec)
  167. {
  168. int i, ret;
  169. /* The index field is only 8 bits */
  170. for (i = 0; i <= U8_MAX; i++) {
  171. ret = cros_ec_pwm_get_duty(ec, i);
  172. /*
  173. * We look for SUCCESS, INVALID_COMMAND, or INVALID_PARAM
  174. * responses; everything else is treated as an error.
  175. * The EC error codes map to -EOPNOTSUPP and -EINVAL,
  176. * so check for those.
  177. */
  178. switch (ret) {
  179. case -EOPNOTSUPP: /* invalid command */
  180. return -ENODEV;
  181. case -EINVAL: /* invalid parameter */
  182. return i;
  183. default:
  184. if (ret < 0)
  185. return ret;
  186. break;
  187. }
  188. }
  189. return U8_MAX;
  190. }
  191. static int cros_ec_pwm_probe(struct platform_device *pdev)
  192. {
  193. struct cros_ec_device *ec = dev_get_drvdata(pdev->dev.parent);
  194. struct device *dev = &pdev->dev;
  195. struct cros_ec_pwm_device *ec_pwm;
  196. struct pwm_chip *chip;
  197. int ret;
  198. if (!ec) {
  199. dev_err(dev, "no parent EC device\n");
  200. return -EINVAL;
  201. }
  202. ec_pwm = devm_kzalloc(dev, sizeof(*ec_pwm), GFP_KERNEL);
  203. if (!ec_pwm)
  204. return -ENOMEM;
  205. chip = &ec_pwm->chip;
  206. ec_pwm->ec = ec;
  207. /* PWM chip */
  208. chip->dev = dev;
  209. chip->ops = &cros_ec_pwm_ops;
  210. chip->of_xlate = cros_ec_pwm_xlate;
  211. chip->of_pwm_n_cells = 1;
  212. chip->base = -1;
  213. ret = cros_ec_num_pwms(ec);
  214. if (ret < 0) {
  215. dev_err(dev, "Couldn't find PWMs: %d\n", ret);
  216. return ret;
  217. }
  218. chip->npwm = ret;
  219. dev_dbg(dev, "Probed %u PWMs\n", chip->npwm);
  220. ret = pwmchip_add(chip);
  221. if (ret < 0) {
  222. dev_err(dev, "cannot register PWM: %d\n", ret);
  223. return ret;
  224. }
  225. platform_set_drvdata(pdev, ec_pwm);
  226. return ret;
  227. }
  228. static int cros_ec_pwm_remove(struct platform_device *dev)
  229. {
  230. struct cros_ec_pwm_device *ec_pwm = platform_get_drvdata(dev);
  231. struct pwm_chip *chip = &ec_pwm->chip;
  232. return pwmchip_remove(chip);
  233. }
  234. #ifdef CONFIG_OF
  235. static const struct of_device_id cros_ec_pwm_of_match[] = {
  236. { .compatible = "google,cros-ec-pwm" },
  237. {},
  238. };
  239. MODULE_DEVICE_TABLE(of, cros_ec_pwm_of_match);
  240. #endif
  241. static struct platform_driver cros_ec_pwm_driver = {
  242. .probe = cros_ec_pwm_probe,
  243. .remove = cros_ec_pwm_remove,
  244. .driver = {
  245. .name = "cros-ec-pwm",
  246. .of_match_table = of_match_ptr(cros_ec_pwm_of_match),
  247. },
  248. };
  249. module_platform_driver(cros_ec_pwm_driver);
  250. MODULE_ALIAS("platform:cros-ec-pwm");
  251. MODULE_DESCRIPTION("ChromeOS EC PWM driver");
  252. MODULE_LICENSE("GPL v2");