apple-mfi-fastcharge.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Fast-charge control for Apple "MFi" devices
  4. *
  5. * Copyright (C) 2019 Bastien Nocera <hadess@hadess.net>
  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 <hadess@hadess.net>");
  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. struct power_supply_desc battery_desc;
  40. int charge_type;
  41. };
  42. static int apple_mfi_fc_set_charge_type(struct mfi_device *mfi,
  43. const union power_supply_propval *val)
  44. {
  45. int current_ma;
  46. int retval;
  47. __u8 request_type;
  48. if (mfi->charge_type == val->intval) {
  49. dev_dbg(&mfi->udev->dev, "charge type %d already set\n",
  50. mfi->charge_type);
  51. return 0;
  52. }
  53. switch (val->intval) {
  54. case POWER_SUPPLY_CHARGE_TYPE_TRICKLE:
  55. current_ma = TRICKLE_CURRENT_MA;
  56. break;
  57. case POWER_SUPPLY_CHARGE_TYPE_FAST:
  58. current_ma = FAST_CURRENT_MA;
  59. break;
  60. default:
  61. return -EINVAL;
  62. }
  63. request_type = USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE;
  64. retval = usb_control_msg(mfi->udev, usb_sndctrlpipe(mfi->udev, 0),
  65. 0x40, /* Vendor‐defined power request */
  66. request_type,
  67. current_ma, /* wValue, current offset */
  68. current_ma, /* wIndex, current offset */
  69. NULL, 0, USB_CTRL_GET_TIMEOUT);
  70. if (retval) {
  71. dev_dbg(&mfi->udev->dev, "retval = %d\n", retval);
  72. return retval;
  73. }
  74. mfi->charge_type = val->intval;
  75. return 0;
  76. }
  77. static int apple_mfi_fc_get_property(struct power_supply *psy,
  78. enum power_supply_property psp,
  79. union power_supply_propval *val)
  80. {
  81. struct mfi_device *mfi = power_supply_get_drvdata(psy);
  82. dev_dbg(&mfi->udev->dev, "prop: %d\n", psp);
  83. switch (psp) {
  84. case POWER_SUPPLY_PROP_CHARGE_TYPE:
  85. val->intval = mfi->charge_type;
  86. break;
  87. case POWER_SUPPLY_PROP_SCOPE:
  88. val->intval = POWER_SUPPLY_SCOPE_DEVICE;
  89. break;
  90. default:
  91. return -ENODATA;
  92. }
  93. return 0;
  94. }
  95. static int apple_mfi_fc_set_property(struct power_supply *psy,
  96. enum power_supply_property psp,
  97. const union power_supply_propval *val)
  98. {
  99. struct mfi_device *mfi = power_supply_get_drvdata(psy);
  100. int ret;
  101. dev_dbg(&mfi->udev->dev, "prop: %d\n", psp);
  102. ret = pm_runtime_get_sync(&mfi->udev->dev);
  103. if (ret < 0) {
  104. pm_runtime_put_noidle(&mfi->udev->dev);
  105. return ret;
  106. }
  107. switch (psp) {
  108. case POWER_SUPPLY_PROP_CHARGE_TYPE:
  109. ret = apple_mfi_fc_set_charge_type(mfi, val);
  110. break;
  111. default:
  112. ret = -EINVAL;
  113. }
  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. char *battery_name;
  152. int err;
  153. if (!mfi_fc_match(udev))
  154. return -ENODEV;
  155. mfi = kzalloc_obj(struct mfi_device);
  156. if (!mfi)
  157. return -ENOMEM;
  158. battery_name = kasprintf(GFP_KERNEL, "apple_mfi_fastcharge_%d-%d",
  159. udev->bus->busnum, udev->devnum);
  160. if (!battery_name) {
  161. err = -ENOMEM;
  162. goto err_free_mfi;
  163. }
  164. mfi->battery_desc = apple_mfi_fc_desc;
  165. mfi->battery_desc.name = battery_name;
  166. battery_cfg.drv_data = mfi;
  167. mfi->charge_type = POWER_SUPPLY_CHARGE_TYPE_TRICKLE;
  168. mfi->battery = power_supply_register(&udev->dev,
  169. &mfi->battery_desc,
  170. &battery_cfg);
  171. if (IS_ERR(mfi->battery)) {
  172. dev_err(&udev->dev, "Can't register battery\n");
  173. err = PTR_ERR(mfi->battery);
  174. goto err_free_name;
  175. }
  176. mfi->udev = usb_get_dev(udev);
  177. dev_set_drvdata(&udev->dev, mfi);
  178. return 0;
  179. err_free_name:
  180. kfree(battery_name);
  181. err_free_mfi:
  182. kfree(mfi);
  183. return err;
  184. }
  185. static void mfi_fc_disconnect(struct usb_device *udev)
  186. {
  187. struct mfi_device *mfi;
  188. mfi = dev_get_drvdata(&udev->dev);
  189. if (mfi->battery)
  190. power_supply_unregister(mfi->battery);
  191. kfree(mfi->battery_desc.name);
  192. dev_set_drvdata(&udev->dev, NULL);
  193. usb_put_dev(mfi->udev);
  194. kfree(mfi);
  195. }
  196. static struct usb_device_driver mfi_fc_driver = {
  197. .name = "apple-mfi-fastcharge",
  198. .probe = mfi_fc_probe,
  199. .disconnect = mfi_fc_disconnect,
  200. .id_table = mfi_fc_id_table,
  201. .match = mfi_fc_match,
  202. .generic_subclass = 1,
  203. };
  204. static int __init mfi_fc_driver_init(void)
  205. {
  206. return usb_register_device_driver(&mfi_fc_driver, THIS_MODULE);
  207. }
  208. static void __exit mfi_fc_driver_exit(void)
  209. {
  210. usb_deregister_device_driver(&mfi_fc_driver);
  211. }
  212. module_init(mfi_fc_driver_init);
  213. module_exit(mfi_fc_driver_exit);