surface_charger.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * AC driver for 7th-generation Microsoft Surface devices via Surface System
  4. * Aggregator Module (SSAM).
  5. *
  6. * Copyright (C) 2019-2021 Maximilian Luz <[email protected]>
  7. */
  8. #include <asm/unaligned.h>
  9. #include <linux/kernel.h>
  10. #include <linux/module.h>
  11. #include <linux/mutex.h>
  12. #include <linux/power_supply.h>
  13. #include <linux/types.h>
  14. #include <linux/surface_aggregator/device.h>
  15. /* -- SAM interface. -------------------------------------------------------- */
  16. enum sam_event_cid_bat {
  17. SAM_EVENT_CID_BAT_ADP = 0x17,
  18. };
  19. enum sam_battery_sta {
  20. SAM_BATTERY_STA_OK = 0x0f,
  21. SAM_BATTERY_STA_PRESENT = 0x10,
  22. };
  23. /* Get battery status (_STA). */
  24. SSAM_DEFINE_SYNC_REQUEST_CL_R(ssam_bat_get_sta, __le32, {
  25. .target_category = SSAM_SSH_TC_BAT,
  26. .command_id = 0x01,
  27. });
  28. /* Get platform power source for battery (_PSR / DPTF PSRC). */
  29. SSAM_DEFINE_SYNC_REQUEST_CL_R(ssam_bat_get_psrc, __le32, {
  30. .target_category = SSAM_SSH_TC_BAT,
  31. .command_id = 0x0d,
  32. });
  33. /* -- Device structures. ---------------------------------------------------- */
  34. struct spwr_psy_properties {
  35. const char *name;
  36. struct ssam_event_registry registry;
  37. };
  38. struct spwr_ac_device {
  39. struct ssam_device *sdev;
  40. char name[32];
  41. struct power_supply *psy;
  42. struct power_supply_desc psy_desc;
  43. struct ssam_event_notifier notif;
  44. struct mutex lock; /* Guards access to state below. */
  45. __le32 state;
  46. };
  47. /* -- State management. ----------------------------------------------------- */
  48. static int spwr_ac_update_unlocked(struct spwr_ac_device *ac)
  49. {
  50. __le32 old = ac->state;
  51. int status;
  52. lockdep_assert_held(&ac->lock);
  53. status = ssam_retry(ssam_bat_get_psrc, ac->sdev, &ac->state);
  54. if (status < 0)
  55. return status;
  56. return old != ac->state;
  57. }
  58. static int spwr_ac_update(struct spwr_ac_device *ac)
  59. {
  60. int status;
  61. mutex_lock(&ac->lock);
  62. status = spwr_ac_update_unlocked(ac);
  63. mutex_unlock(&ac->lock);
  64. return status;
  65. }
  66. static int spwr_ac_recheck(struct spwr_ac_device *ac)
  67. {
  68. int status;
  69. status = spwr_ac_update(ac);
  70. if (status > 0)
  71. power_supply_changed(ac->psy);
  72. return status >= 0 ? 0 : status;
  73. }
  74. static u32 spwr_notify_ac(struct ssam_event_notifier *nf, const struct ssam_event *event)
  75. {
  76. struct spwr_ac_device *ac;
  77. int status;
  78. ac = container_of(nf, struct spwr_ac_device, notif);
  79. dev_dbg(&ac->sdev->dev, "power event (cid = %#04x, iid = %#04x, tid = %#04x)\n",
  80. event->command_id, event->instance_id, event->target_id);
  81. /*
  82. * Allow events of all targets/instances here. Global adapter status
  83. * seems to be handled via target=1 and instance=1, but events are
  84. * reported on all targets/instances in use.
  85. *
  86. * While it should be enough to just listen on 1/1, listen everywhere to
  87. * make sure we don't miss anything.
  88. */
  89. switch (event->command_id) {
  90. case SAM_EVENT_CID_BAT_ADP:
  91. status = spwr_ac_recheck(ac);
  92. return ssam_notifier_from_errno(status) | SSAM_NOTIF_HANDLED;
  93. default:
  94. return 0;
  95. }
  96. }
  97. /* -- Properties. ----------------------------------------------------------- */
  98. static const enum power_supply_property spwr_ac_props[] = {
  99. POWER_SUPPLY_PROP_ONLINE,
  100. };
  101. static int spwr_ac_get_property(struct power_supply *psy, enum power_supply_property psp,
  102. union power_supply_propval *val)
  103. {
  104. struct spwr_ac_device *ac = power_supply_get_drvdata(psy);
  105. int status;
  106. mutex_lock(&ac->lock);
  107. status = spwr_ac_update_unlocked(ac);
  108. if (status)
  109. goto out;
  110. switch (psp) {
  111. case POWER_SUPPLY_PROP_ONLINE:
  112. val->intval = !!le32_to_cpu(ac->state);
  113. break;
  114. default:
  115. status = -EINVAL;
  116. goto out;
  117. }
  118. out:
  119. mutex_unlock(&ac->lock);
  120. return status;
  121. }
  122. /* -- Device setup. --------------------------------------------------------- */
  123. static char *battery_supplied_to[] = {
  124. "BAT1",
  125. "BAT2",
  126. };
  127. static void spwr_ac_init(struct spwr_ac_device *ac, struct ssam_device *sdev,
  128. struct ssam_event_registry registry, const char *name)
  129. {
  130. mutex_init(&ac->lock);
  131. strncpy(ac->name, name, ARRAY_SIZE(ac->name) - 1);
  132. ac->sdev = sdev;
  133. ac->notif.base.priority = 1;
  134. ac->notif.base.fn = spwr_notify_ac;
  135. ac->notif.event.reg = registry;
  136. ac->notif.event.id.target_category = sdev->uid.category;
  137. ac->notif.event.id.instance = 0;
  138. ac->notif.event.mask = SSAM_EVENT_MASK_NONE;
  139. ac->notif.event.flags = SSAM_EVENT_SEQUENCED;
  140. ac->psy_desc.name = ac->name;
  141. ac->psy_desc.type = POWER_SUPPLY_TYPE_MAINS;
  142. ac->psy_desc.properties = spwr_ac_props;
  143. ac->psy_desc.num_properties = ARRAY_SIZE(spwr_ac_props);
  144. ac->psy_desc.get_property = spwr_ac_get_property;
  145. }
  146. static int spwr_ac_register(struct spwr_ac_device *ac)
  147. {
  148. struct power_supply_config psy_cfg = {};
  149. __le32 sta;
  150. int status;
  151. /* Make sure the device is there and functioning properly. */
  152. status = ssam_retry(ssam_bat_get_sta, ac->sdev, &sta);
  153. if (status)
  154. return status;
  155. if ((le32_to_cpu(sta) & SAM_BATTERY_STA_OK) != SAM_BATTERY_STA_OK)
  156. return -ENODEV;
  157. psy_cfg.drv_data = ac;
  158. psy_cfg.supplied_to = battery_supplied_to;
  159. psy_cfg.num_supplicants = ARRAY_SIZE(battery_supplied_to);
  160. ac->psy = devm_power_supply_register(&ac->sdev->dev, &ac->psy_desc, &psy_cfg);
  161. if (IS_ERR(ac->psy))
  162. return PTR_ERR(ac->psy);
  163. return ssam_device_notifier_register(ac->sdev, &ac->notif);
  164. }
  165. /* -- Driver setup. --------------------------------------------------------- */
  166. static int __maybe_unused surface_ac_resume(struct device *dev)
  167. {
  168. return spwr_ac_recheck(dev_get_drvdata(dev));
  169. }
  170. static SIMPLE_DEV_PM_OPS(surface_ac_pm_ops, NULL, surface_ac_resume);
  171. static int surface_ac_probe(struct ssam_device *sdev)
  172. {
  173. const struct spwr_psy_properties *p;
  174. struct spwr_ac_device *ac;
  175. p = ssam_device_get_match_data(sdev);
  176. if (!p)
  177. return -ENODEV;
  178. ac = devm_kzalloc(&sdev->dev, sizeof(*ac), GFP_KERNEL);
  179. if (!ac)
  180. return -ENOMEM;
  181. spwr_ac_init(ac, sdev, p->registry, p->name);
  182. ssam_device_set_drvdata(sdev, ac);
  183. return spwr_ac_register(ac);
  184. }
  185. static void surface_ac_remove(struct ssam_device *sdev)
  186. {
  187. struct spwr_ac_device *ac = ssam_device_get_drvdata(sdev);
  188. ssam_device_notifier_unregister(sdev, &ac->notif);
  189. }
  190. static const struct spwr_psy_properties spwr_psy_props_adp1 = {
  191. .name = "ADP1",
  192. .registry = SSAM_EVENT_REGISTRY_SAM,
  193. };
  194. static const struct ssam_device_id surface_ac_match[] = {
  195. { SSAM_SDEV(BAT, 0x01, 0x01, 0x01), (unsigned long)&spwr_psy_props_adp1 },
  196. { },
  197. };
  198. MODULE_DEVICE_TABLE(ssam, surface_ac_match);
  199. static struct ssam_device_driver surface_ac_driver = {
  200. .probe = surface_ac_probe,
  201. .remove = surface_ac_remove,
  202. .match_table = surface_ac_match,
  203. .driver = {
  204. .name = "surface_ac",
  205. .pm = &surface_ac_pm_ops,
  206. .probe_type = PROBE_PREFER_ASYNCHRONOUS,
  207. },
  208. };
  209. module_ssam_device_driver(surface_ac_driver);
  210. MODULE_AUTHOR("Maximilian Luz <[email protected]>");
  211. MODULE_DESCRIPTION("AC driver for Surface System Aggregator Module");
  212. MODULE_LICENSE("GPL");