pwm-cros-ec.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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/of.h>
  9. #include <linux/platform_data/cros_ec_commands.h>
  10. #include <linux/platform_data/cros_ec_proto.h>
  11. #include <linux/platform_device.h>
  12. #include <linux/pwm.h>
  13. #include <linux/slab.h>
  14. #include <dt-bindings/mfd/cros_ec.h>
  15. /**
  16. * struct cros_ec_pwm_device - Driver data for EC PWM
  17. *
  18. * @ec: Pointer to EC device
  19. * @use_pwm_type: Use PWM types instead of generic channels
  20. */
  21. struct cros_ec_pwm_device {
  22. struct cros_ec_device *ec;
  23. bool use_pwm_type;
  24. };
  25. static inline struct cros_ec_pwm_device *pwm_to_cros_ec_pwm(struct pwm_chip *chip)
  26. {
  27. return pwmchip_get_drvdata(chip);
  28. }
  29. static int cros_ec_dt_type_to_pwm_type(u8 dt_index, u8 *pwm_type)
  30. {
  31. switch (dt_index) {
  32. case CROS_EC_PWM_DT_KB_LIGHT:
  33. *pwm_type = EC_PWM_TYPE_KB_LIGHT;
  34. return 0;
  35. case CROS_EC_PWM_DT_DISPLAY_LIGHT:
  36. *pwm_type = EC_PWM_TYPE_DISPLAY_LIGHT;
  37. return 0;
  38. default:
  39. return -EINVAL;
  40. }
  41. }
  42. static int cros_ec_pwm_set_duty(struct cros_ec_pwm_device *ec_pwm, u8 index,
  43. u16 duty)
  44. {
  45. struct cros_ec_device *ec = ec_pwm->ec;
  46. struct {
  47. struct cros_ec_command msg;
  48. struct ec_params_pwm_set_duty params;
  49. } __packed buf;
  50. struct ec_params_pwm_set_duty *params = &buf.params;
  51. struct cros_ec_command *msg = &buf.msg;
  52. int ret;
  53. memset(&buf, 0, sizeof(buf));
  54. msg->version = 0;
  55. msg->command = EC_CMD_PWM_SET_DUTY;
  56. msg->insize = 0;
  57. msg->outsize = sizeof(*params);
  58. params->duty = duty;
  59. if (ec_pwm->use_pwm_type) {
  60. ret = cros_ec_dt_type_to_pwm_type(index, &params->pwm_type);
  61. if (ret) {
  62. dev_err(ec->dev, "Invalid PWM type index: %d\n", index);
  63. return ret;
  64. }
  65. params->index = 0;
  66. } else {
  67. params->pwm_type = EC_PWM_TYPE_GENERIC;
  68. params->index = index;
  69. }
  70. return cros_ec_cmd_xfer_status(ec, msg);
  71. }
  72. static int cros_ec_pwm_get_duty(struct cros_ec_device *ec, bool use_pwm_type, u8 index)
  73. {
  74. struct {
  75. struct cros_ec_command msg;
  76. union {
  77. struct ec_params_pwm_get_duty params;
  78. struct ec_response_pwm_get_duty resp;
  79. };
  80. } __packed buf;
  81. struct ec_params_pwm_get_duty *params = &buf.params;
  82. struct ec_response_pwm_get_duty *resp = &buf.resp;
  83. struct cros_ec_command *msg = &buf.msg;
  84. int ret;
  85. memset(&buf, 0, sizeof(buf));
  86. msg->version = 0;
  87. msg->command = EC_CMD_PWM_GET_DUTY;
  88. msg->insize = sizeof(*resp);
  89. msg->outsize = sizeof(*params);
  90. if (use_pwm_type) {
  91. ret = cros_ec_dt_type_to_pwm_type(index, &params->pwm_type);
  92. if (ret) {
  93. dev_err(ec->dev, "Invalid PWM type index: %d\n", index);
  94. return ret;
  95. }
  96. params->index = 0;
  97. } else {
  98. params->pwm_type = EC_PWM_TYPE_GENERIC;
  99. params->index = index;
  100. }
  101. ret = cros_ec_cmd_xfer_status(ec, msg);
  102. if (ret < 0)
  103. return ret;
  104. return resp->duty;
  105. }
  106. static int cros_ec_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
  107. const struct pwm_state *state)
  108. {
  109. struct cros_ec_pwm_device *ec_pwm = pwm_to_cros_ec_pwm(chip);
  110. u16 duty_cycle;
  111. int ret;
  112. /* The EC won't let us change the period */
  113. if (state->period != EC_PWM_MAX_DUTY)
  114. return -EINVAL;
  115. if (state->polarity != PWM_POLARITY_NORMAL)
  116. return -EINVAL;
  117. /*
  118. * EC doesn't separate the concept of duty cycle and enabled, but
  119. * kernel does. Translate.
  120. */
  121. duty_cycle = state->enabled ? state->duty_cycle : 0;
  122. ret = cros_ec_pwm_set_duty(ec_pwm, pwm->hwpwm, duty_cycle);
  123. if (ret < 0)
  124. return ret;
  125. return 0;
  126. }
  127. static int cros_ec_pwm_get_state(struct pwm_chip *chip, struct pwm_device *pwm,
  128. struct pwm_state *state)
  129. {
  130. struct cros_ec_pwm_device *ec_pwm = pwm_to_cros_ec_pwm(chip);
  131. int ret;
  132. ret = cros_ec_pwm_get_duty(ec_pwm->ec, ec_pwm->use_pwm_type, pwm->hwpwm);
  133. if (ret < 0) {
  134. dev_err(pwmchip_parent(chip), "error getting initial duty: %d\n", ret);
  135. return ret;
  136. }
  137. state->enabled = (ret > 0);
  138. state->duty_cycle = ret;
  139. state->period = EC_PWM_MAX_DUTY;
  140. state->polarity = PWM_POLARITY_NORMAL;
  141. return 0;
  142. }
  143. static const struct pwm_ops cros_ec_pwm_ops = {
  144. .get_state = cros_ec_pwm_get_state,
  145. .apply = cros_ec_pwm_apply,
  146. };
  147. /*
  148. * Determine the number of supported PWMs. The EC does not return the number
  149. * of PWMs it supports directly, so we have to read the pwm duty cycle for
  150. * subsequent channels until we get an error.
  151. */
  152. static int cros_ec_num_pwms(struct cros_ec_device *ec)
  153. {
  154. int i, ret;
  155. /* The index field is only 8 bits */
  156. for (i = 0; i <= U8_MAX; i++) {
  157. /*
  158. * Note that this function is only called when use_pwm_type is
  159. * false. With use_pwm_type == true the number of PWMs is fixed.
  160. */
  161. ret = cros_ec_pwm_get_duty(ec, false, i);
  162. /*
  163. * We look for SUCCESS, INVALID_COMMAND, or INVALID_PARAM
  164. * responses; everything else is treated as an error.
  165. * The EC error codes map to -EOPNOTSUPP and -EINVAL,
  166. * so check for those.
  167. */
  168. switch (ret) {
  169. case -EOPNOTSUPP: /* invalid command */
  170. return -ENODEV;
  171. case -EINVAL: /* invalid parameter */
  172. return i;
  173. default:
  174. if (ret < 0)
  175. return ret;
  176. break;
  177. }
  178. }
  179. return U8_MAX;
  180. }
  181. static int cros_ec_pwm_probe(struct platform_device *pdev)
  182. {
  183. struct cros_ec_device *ec = dev_get_drvdata(pdev->dev.parent);
  184. struct device *dev = &pdev->dev;
  185. struct device_node *np = pdev->dev.of_node;
  186. struct cros_ec_pwm_device *ec_pwm;
  187. struct pwm_chip *chip;
  188. bool use_pwm_type = false;
  189. unsigned int i, npwm;
  190. int ret;
  191. if (!ec)
  192. return dev_err_probe(dev, -EINVAL, "no parent EC device\n");
  193. if (of_device_is_compatible(np, "google,cros-ec-pwm-type")) {
  194. use_pwm_type = true;
  195. npwm = CROS_EC_PWM_DT_COUNT;
  196. } else {
  197. ret = cros_ec_num_pwms(ec);
  198. if (ret < 0)
  199. return dev_err_probe(dev, ret, "Couldn't find PWMs\n");
  200. npwm = ret;
  201. }
  202. chip = devm_pwmchip_alloc(dev, npwm, sizeof(*ec_pwm));
  203. if (IS_ERR(chip))
  204. return PTR_ERR(chip);
  205. ec_pwm = pwm_to_cros_ec_pwm(chip);
  206. ec_pwm->use_pwm_type = use_pwm_type;
  207. ec_pwm->ec = ec;
  208. /* PWM chip */
  209. chip->ops = &cros_ec_pwm_ops;
  210. /*
  211. * The device tree binding for this device is special as it only uses a
  212. * single cell (for the hwid) and so doesn't provide a default period.
  213. * This isn't a big problem though as the hardware only supports a
  214. * single period length, it's just a bit ugly to make this fit into the
  215. * pwm core abstractions. So initialize the period here, as
  216. * of_pwm_xlate_with_flags() won't do that for us.
  217. */
  218. for (i = 0; i < npwm; ++i)
  219. chip->pwms[i].args.period = EC_PWM_MAX_DUTY;
  220. dev_dbg(dev, "Probed %u PWMs\n", chip->npwm);
  221. ret = devm_pwmchip_add(dev, chip);
  222. if (ret < 0)
  223. return dev_err_probe(dev, ret, "cannot register PWM\n");
  224. return 0;
  225. }
  226. #ifdef CONFIG_OF
  227. static const struct of_device_id cros_ec_pwm_of_match[] = {
  228. { .compatible = "google,cros-ec-pwm" },
  229. { .compatible = "google,cros-ec-pwm-type" },
  230. {},
  231. };
  232. MODULE_DEVICE_TABLE(of, cros_ec_pwm_of_match);
  233. #endif
  234. static struct platform_driver cros_ec_pwm_driver = {
  235. .probe = cros_ec_pwm_probe,
  236. .driver = {
  237. .name = "cros-ec-pwm",
  238. .of_match_table = of_match_ptr(cros_ec_pwm_of_match),
  239. },
  240. };
  241. module_platform_driver(cros_ec_pwm_driver);
  242. MODULE_ALIAS("platform:cros-ec-pwm");
  243. MODULE_DESCRIPTION("ChromeOS EC PWM driver");
  244. MODULE_LICENSE("GPL v2");