apple-mfi-fastcharge.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Fast-charge control for Apple "MFi" devices
  4. *
  5. * Copyright (C) 2019 Bastien Nocera <[email protected]>
  6. */
  7. /* Standard include files */
  8. #include <linux/module.h>
  9. #include <linux/power_supply.h>
  10. #include <linux/slab.h>
  11. #include <linux/usb.h>
  12. MODULE_AUTHOR("Bastien Nocera <[email protected]>");
  13. MODULE_DESCRIPTION("Fast-charge control for Apple \"MFi\" devices");
  14. MODULE_LICENSE("GPL");
  15. #define TRICKLE_CURRENT_MA 0
  16. #define FAST_CURRENT_MA 2500
  17. #define APPLE_VENDOR_ID 0x05ac /* Apple */
  18. /* The product ID is defined as starting with 0x12nn, as per the
  19. * "Choosing an Apple Device USB Configuration" section in
  20. * release R9 (2012) of the "MFi Accessory Hardware Specification"
  21. *
  22. * To distinguish an Apple device, a USB host can check the device
  23. * descriptor of attached USB devices for the following fields:
  24. * ■ Vendor ID: 0x05AC
  25. * ■ Product ID: 0x12nn
  26. *
  27. * Those checks will be done in .match() and .probe().
  28. */
  29. static const struct usb_device_id mfi_fc_id_table[] = {
  30. { .idVendor = APPLE_VENDOR_ID,
  31. .match_flags = USB_DEVICE_ID_MATCH_VENDOR },
  32. {},
  33. };
  34. MODULE_DEVICE_TABLE(usb, mfi_fc_id_table);
  35. /* Driver-local specific stuff */
  36. struct mfi_device {
  37. struct usb_device *udev;
  38. struct power_supply *battery;
  39. int charge_type;
  40. };
  41. static int apple_mfi_fc_set_charge_type(struct mfi_device *mfi,
  42. const union power_supply_propval *val)
  43. {
  44. int current_ma;
  45. int retval;
  46. __u8 request_type;
  47. if (mfi->charge_type == val->intval) {
  48. dev_dbg(&mfi->udev->dev, "charge type %d already set\n",
  49. mfi->charge_type);
  50. return 0;
  51. }
  52. switch (val->intval) {
  53. case POWER_SUPPLY_CHARGE_TYPE_TRICKLE:
  54. current_ma = TRICKLE_CURRENT_MA;
  55. break;
  56. case POWER_SUPPLY_CHARGE_TYPE_FAST:
  57. current_ma = FAST_CURRENT_MA;
  58. break;
  59. default:
  60. return -EINVAL;
  61. }
  62. request_type = USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE;
  63. retval = usb_control_msg(mfi->udev, usb_sndctrlpipe(mfi->udev, 0),
  64. 0x40, /* Vendor‐defined power request */
  65. request_type,
  66. current_ma, /* wValue, current offset */
  67. current_ma, /* wIndex, current offset */
  68. NULL, 0, USB_CTRL_GET_TIMEOUT);
  69. if (retval) {
  70. dev_dbg(&mfi->udev->dev, "retval = %d\n", retval);
  71. return retval;
  72. }
  73. mfi->charge_type = val->intval;
  74. return 0;
  75. }
  76. static int apple_mfi_fc_get_property(struct power_supply *psy,
  77. enum power_supply_property psp,
  78. union power_supply_propval *val)
  79. {
  80. struct mfi_device *mfi = power_supply_get_drvdata(psy);
  81. dev_dbg(&mfi->udev->dev, "prop: %d\n", psp);
  82. switch (psp) {
  83. case POWER_SUPPLY_PROP_CHARGE_TYPE:
  84. val->intval = mfi->charge_type;
  85. break;
  86. case POWER_SUPPLY_PROP_SCOPE:
  87. val->intval = POWER_SUPPLY_SCOPE_DEVICE;
  88. break;
  89. default:
  90. return -ENODATA;
  91. }
  92. return 0;
  93. }
  94. static int apple_mfi_fc_set_property(struct power_supply *psy,
  95. enum power_supply_property psp,
  96. const union power_supply_propval *val)
  97. {
  98. struct mfi_device *mfi = power_supply_get_drvdata(psy);
  99. int ret;
  100. dev_dbg(&mfi->udev->dev, "prop: %d\n", psp);
  101. ret = pm_runtime_get_sync(&mfi->udev->dev);
  102. if (ret < 0) {
  103. pm_runtime_put_noidle(&mfi->udev->dev);
  104. return ret;
  105. }
  106. switch (psp) {
  107. case POWER_SUPPLY_PROP_CHARGE_TYPE:
  108. ret = apple_mfi_fc_set_charge_type(mfi, val);
  109. break;
  110. default:
  111. ret = -EINVAL;
  112. }
  113. pm_runtime_mark_last_busy(&mfi->udev->dev);
  114. pm_runtime_put_autosuspend(&mfi->udev->dev);
  115. return ret;
  116. }
  117. static int apple_mfi_fc_property_is_writeable(struct power_supply *psy,
  118. enum power_supply_property psp)
  119. {
  120. switch (psp) {
  121. case POWER_SUPPLY_PROP_CHARGE_TYPE:
  122. return 1;
  123. default:
  124. return 0;
  125. }
  126. }
  127. static enum power_supply_property apple_mfi_fc_properties[] = {
  128. POWER_SUPPLY_PROP_CHARGE_TYPE,
  129. POWER_SUPPLY_PROP_SCOPE
  130. };
  131. static const struct power_supply_desc apple_mfi_fc_desc = {
  132. .name = "apple_mfi_fastcharge",
  133. .type = POWER_SUPPLY_TYPE_BATTERY,
  134. .properties = apple_mfi_fc_properties,
  135. .num_properties = ARRAY_SIZE(apple_mfi_fc_properties),
  136. .get_property = apple_mfi_fc_get_property,
  137. .set_property = apple_mfi_fc_set_property,
  138. .property_is_writeable = apple_mfi_fc_property_is_writeable
  139. };
  140. static bool mfi_fc_match(struct usb_device *udev)
  141. {
  142. int idProduct;
  143. idProduct = le16_to_cpu(udev->descriptor.idProduct);
  144. /* See comment above mfi_fc_id_table[] */
  145. return (idProduct >= 0x1200 && idProduct <= 0x12ff);
  146. }
  147. static int mfi_fc_probe(struct usb_device *udev)
  148. {
  149. struct power_supply_config battery_cfg = {};
  150. struct mfi_device *mfi = NULL;
  151. int err;
  152. if (!mfi_fc_match(udev))
  153. return -ENODEV;
  154. mfi = kzalloc(sizeof(struct mfi_device), GFP_KERNEL);
  155. if (!mfi)
  156. return -ENOMEM;
  157. battery_cfg.drv_data = mfi;
  158. mfi->charge_type = POWER_SUPPLY_CHARGE_TYPE_TRICKLE;
  159. mfi->battery = power_supply_register(&udev->dev,
  160. &apple_mfi_fc_desc,
  161. &battery_cfg);
  162. if (IS_ERR(mfi->battery)) {
  163. dev_err(&udev->dev, "Can't register battery\n");
  164. err = PTR_ERR(mfi->battery);
  165. kfree(mfi);
  166. return err;
  167. }
  168. mfi->udev = usb_get_dev(udev);
  169. dev_set_drvdata(&udev->dev, mfi);
  170. return 0;
  171. }
  172. static void mfi_fc_disconnect(struct usb_device *udev)
  173. {
  174. struct mfi_device *mfi;
  175. mfi = dev_get_drvdata(&udev->dev);
  176. if (mfi->battery)
  177. power_supply_unregister(mfi->battery);
  178. dev_set_drvdata(&udev->dev, NULL);
  179. usb_put_dev(mfi->udev);
  180. kfree(mfi);
  181. }
  182. static struct usb_device_driver mfi_fc_driver = {
  183. .name = "apple-mfi-fastcharge",
  184. .probe = mfi_fc_probe,
  185. .disconnect = mfi_fc_disconnect,
  186. .id_table = mfi_fc_id_table,
  187. .match = mfi_fc_match,
  188. .generic_subclass = 1,
  189. };
  190. static int __init mfi_fc_driver_init(void)
  191. {
  192. return usb_register_device_driver(&mfi_fc_driver, THIS_MODULE);
  193. }
  194. static void __exit mfi_fc_driver_exit(void)
  195. {
  196. usb_deregister_device_driver(&mfi_fc_driver);
  197. }
  198. module_init(mfi_fc_driver_init);
  199. module_exit(mfi_fc_driver_exit);