Skip to content

Commit 10b60d6

Browse files
marcanjgouly
authored andcommitted
power: supply: macsmc_power: Driver for Apple SMC power/battery stats
This driver implements support for battery stats on top of the macsmc framework, to support Apple M1 Mac machines. Co-authored-by: Joey Gouly <joey.gouly@arm.com> Signed-off-by: Hector Martin <marcan@marcan.st>
1 parent b232bd4 commit 10b60d6

3 files changed

Lines changed: 263 additions & 0 deletions

File tree

drivers/power/supply/Kconfig

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1018,4 +1018,11 @@ config FUEL_GAUGE_MM8013
10181018
the state of charge, temperature, cycle count, actual and design
10191019
capacity, etc.
10201020

1021+
config CHARGER_MACSMC
1022+
tristate "Apple SMC Charger / Battery support"
1023+
depends on APPLE_SMC
1024+
help
1025+
Say Y here to enable support for the charger and battery controls on
1026+
Apple SMC controllers, as used on Apple Silicon Macs.
1027+
10211028
endif # POWER_SUPPLY

drivers/power/supply/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ obj-$(CONFIG_CHARGER_GPIO) += gpio-charger.o
7575
obj-$(CONFIG_CHARGER_MANAGER) += charger-manager.o
7676
obj-$(CONFIG_CHARGER_LT3651) += lt3651-charger.o
7777
obj-$(CONFIG_CHARGER_LTC4162L) += ltc4162-l-charger.o
78+
obj-$(CONFIG_CHARGER_MACSMC) += macsmc_power.o
7879
obj-$(CONFIG_CHARGER_MAX14577) += max14577_charger.o
7980
obj-$(CONFIG_CHARGER_DETECTOR_MAX14656) += max14656_charger_detector.o
8081
obj-$(CONFIG_CHARGER_MAX77650) += max77650-charger.o
Lines changed: 255 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,255 @@
1+
// SPDX-License-Identifier: GPL-2.0-only OR MIT
2+
/*
3+
* Apple SMC Power/Battery Management
4+
* Copyright The Asahi Linux Contributors
5+
*/
6+
7+
#include <linux/module.h>
8+
#include <linux/of.h>
9+
#include <linux/platform_device.h>
10+
#include <linux/mfd/core.h>
11+
#include <linux/mfd/macsmc.h>
12+
#include <linux/power_supply.h>
13+
14+
#define MAX_STRING_LENGTH 256
15+
16+
struct macsmc_power {
17+
struct device *dev;
18+
struct apple_smc *smc;
19+
struct power_supply *psy;
20+
char model_name[MAX_STRING_LENGTH];
21+
char serial_number[MAX_STRING_LENGTH];
22+
23+
struct notifier_block nb;
24+
};
25+
26+
static int macsmc_battery_get_status(struct macsmc_power *power)
27+
{
28+
u8 val;
29+
int ret;
30+
31+
ret = apple_smc_read_u8(power->smc, SMC_KEY(BSFC), &val);
32+
if (ret)
33+
return ret;
34+
if (val == 1)
35+
return POWER_SUPPLY_STATUS_FULL;
36+
37+
ret = apple_smc_read_u8(power->smc, SMC_KEY(CHSC), &val);
38+
if (ret)
39+
return ret;
40+
if (val == 1)
41+
return POWER_SUPPLY_STATUS_CHARGING;
42+
43+
ret = apple_smc_read_u8(power->smc, SMC_KEY(CHCC), &val);
44+
if (ret)
45+
return ret;
46+
if (val == 0)
47+
return POWER_SUPPLY_STATUS_DISCHARGING;
48+
49+
ret = apple_smc_read_u8(power->smc, SMC_KEY(CHCE), &val);
50+
if (ret)
51+
return ret;
52+
if (val == 0)
53+
return POWER_SUPPLY_STATUS_DISCHARGING;
54+
else
55+
return POWER_SUPPLY_STATUS_NOT_CHARGING;
56+
}
57+
58+
static int macsmc_battery_get_property(struct power_supply *psy,
59+
enum power_supply_property psp,
60+
union power_supply_propval *val)
61+
{
62+
struct macsmc_power *power = power_supply_get_drvdata(psy);
63+
int ret = 0;
64+
u16 vu16;
65+
u32 vu32;
66+
s16 vs16;
67+
s32 vs32;
68+
s64 vs64;
69+
70+
switch (psp) {
71+
case POWER_SUPPLY_PROP_STATUS:
72+
val->intval = macsmc_battery_get_status(power);
73+
ret = val->intval < 0 ? val->intval : 0;
74+
break;
75+
case POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW:
76+
ret = apple_smc_read_u16(power->smc, SMC_KEY(B0TE), &vu16);
77+
val->intval = vu16 == 0xffff ? 0 : vu16 * 60;
78+
break;
79+
case POWER_SUPPLY_PROP_TIME_TO_FULL_NOW:
80+
ret = apple_smc_read_u16(power->smc, SMC_KEY(B0TF), &vu16);
81+
val->intval = vu16 == 0xffff ? 0 : vu16 * 60;
82+
break;
83+
case POWER_SUPPLY_PROP_CAPACITY:
84+
ret = apple_smc_read_u16(power->smc, SMC_KEY(BRSC), &vu16);
85+
val->intval = vu16;
86+
break;
87+
case POWER_SUPPLY_PROP_VOLTAGE_NOW:
88+
ret = apple_smc_read_u16(power->smc, SMC_KEY(B0AV), &vu16);
89+
val->intval = vu16 * 1000;
90+
break;
91+
case POWER_SUPPLY_PROP_CURRENT_NOW:
92+
ret = apple_smc_read_s16(power->smc, SMC_KEY(B0AC), &vs16);
93+
val->intval = vs16 * 1000;
94+
break;
95+
case POWER_SUPPLY_PROP_POWER_NOW:
96+
ret = apple_smc_read_s32(power->smc, SMC_KEY(B0AP), &vs32);
97+
val->intval = vs32 * 1000;
98+
break;
99+
case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
100+
ret = apple_smc_read_u16(power->smc, SMC_KEY(BITV), &vu16);
101+
val->intval = vu16 * 1000;
102+
break;
103+
case POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT:
104+
ret = apple_smc_read_u16(power->smc, SMC_KEY(B0RC), &vu16);
105+
val->intval = vu16 * 1000;
106+
break;
107+
case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT:
108+
ret = apple_smc_read_u32(power->smc, SMC_KEY(CSIL), &vu32);
109+
val->intval = vu32 * 1000;
110+
break;
111+
case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX:
112+
ret = apple_smc_read_u16(power->smc, SMC_KEY(B0RI), &vu16);
113+
val->intval = vu16 * 1000;
114+
break;
115+
case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE:
116+
ret = apple_smc_read_u16(power->smc, SMC_KEY(B0RV), &vu16);
117+
val->intval = vu16 * 1000;
118+
break;
119+
case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
120+
ret = apple_smc_read_u16(power->smc, SMC_KEY(B0DC), &vu16);
121+
val->intval = vu16 * 1000;
122+
break;
123+
case POWER_SUPPLY_PROP_CHARGE_FULL:
124+
ret = apple_smc_read_u16(power->smc, SMC_KEY(B0FC), &vu16);
125+
val->intval = vu16 * 1000;
126+
break;
127+
case POWER_SUPPLY_PROP_CHARGE_NOW:
128+
ret = apple_smc_read_u16(power->smc, SMC_KEY(B0RM), &vu16);
129+
val->intval = swab16(vu16) * 1000;
130+
break;
131+
case POWER_SUPPLY_PROP_TEMP:
132+
ret = apple_smc_read_u16(power->smc, SMC_KEY(B0AT), &vu16);
133+
val->intval = vu16 - 2732;
134+
break;
135+
case POWER_SUPPLY_PROP_CHARGE_COUNTER:
136+
ret = apple_smc_read_s64(power->smc, SMC_KEY(BAAC), &vs64);
137+
val->intval = vs64;
138+
break;
139+
case POWER_SUPPLY_PROP_MODEL_NAME:
140+
val->strval = power->model_name;
141+
break;
142+
case POWER_SUPPLY_PROP_SERIAL_NUMBER:
143+
val->strval = power->serial_number;
144+
break;
145+
default:
146+
return -EINVAL;
147+
}
148+
149+
return ret;
150+
}
151+
152+
static enum power_supply_property macsmc_battery_props[] = {
153+
POWER_SUPPLY_PROP_STATUS,
154+
POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
155+
POWER_SUPPLY_PROP_TIME_TO_FULL_NOW,
156+
POWER_SUPPLY_PROP_CAPACITY,
157+
POWER_SUPPLY_PROP_VOLTAGE_NOW,
158+
POWER_SUPPLY_PROP_CURRENT_NOW,
159+
POWER_SUPPLY_PROP_POWER_NOW,
160+
POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
161+
POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT,
162+
POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT,
163+
POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX,
164+
POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE,
165+
POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
166+
POWER_SUPPLY_PROP_CHARGE_FULL,
167+
POWER_SUPPLY_PROP_CHARGE_NOW,
168+
POWER_SUPPLY_PROP_TEMP,
169+
POWER_SUPPLY_PROP_CHARGE_COUNTER,
170+
POWER_SUPPLY_PROP_MODEL_NAME,
171+
POWER_SUPPLY_PROP_SERIAL_NUMBER,
172+
};
173+
174+
static const struct power_supply_desc macsmc_battery_desc = {
175+
.name = "macsmc-battery",
176+
.type = POWER_SUPPLY_TYPE_BATTERY,
177+
.get_property = macsmc_battery_get_property,
178+
.properties = macsmc_battery_props,
179+
.num_properties = ARRAY_SIZE(macsmc_battery_props),
180+
};
181+
182+
static int macsmc_power_event(struct notifier_block *nb, unsigned long event, void *data)
183+
{
184+
struct macsmc_power *power = container_of(nb, struct macsmc_power, nb);
185+
186+
if ((event & 0xffffff00) == 0x71010100) {
187+
bool charging = (event & 0xff) != 0;
188+
189+
dev_info(power->dev, "Charging: %d\n", charging);
190+
power_supply_changed(power->psy);
191+
192+
return NOTIFY_OK;
193+
}
194+
195+
return NOTIFY_DONE;
196+
}
197+
198+
static int macsmc_power_probe(struct platform_device *pdev)
199+
{
200+
struct apple_smc *smc = dev_get_drvdata(pdev->dev.parent);
201+
struct power_supply_config psy_cfg = {};
202+
struct macsmc_power *power;
203+
int ret;
204+
205+
power = devm_kzalloc(&pdev->dev, sizeof(*power), GFP_KERNEL);
206+
if (!power)
207+
return -ENOMEM;
208+
209+
power->dev = &pdev->dev;
210+
power->smc = smc;
211+
dev_set_drvdata(&pdev->dev, power);
212+
213+
/* Ignore devices without a charger/battery */
214+
if (macsmc_battery_get_status(power) <= POWER_SUPPLY_STATUS_UNKNOWN)
215+
return -ENODEV;
216+
217+
/* Fetch string properties */
218+
apple_smc_read(smc, SMC_KEY(BMDN), power->model_name, sizeof(power->model_name) - 1);
219+
apple_smc_read(smc, SMC_KEY(BMSN), power->serial_number, sizeof(power->serial_number) - 1);
220+
221+
psy_cfg.drv_data = power;
222+
power->psy = devm_power_supply_register(&pdev->dev, &macsmc_battery_desc, &psy_cfg);
223+
if (IS_ERR(power->psy)) {
224+
dev_err(&pdev->dev, "Failed to register power supply\n");
225+
ret = PTR_ERR(power->psy);
226+
return ret;
227+
}
228+
229+
power->nb.notifier_call = macsmc_power_event;
230+
apple_smc_register_notifier(power->smc, &power->nb);
231+
232+
return 0;
233+
}
234+
235+
static void macsmc_power_remove(struct platform_device *pdev)
236+
{
237+
struct macsmc_power *power = dev_get_drvdata(&pdev->dev);
238+
239+
apple_smc_unregister_notifier(power->smc, &power->nb);
240+
}
241+
242+
static struct platform_driver macsmc_power_driver = {
243+
.driver = {
244+
.name = "macsmc-power",
245+
.owner = THIS_MODULE,
246+
},
247+
.probe = macsmc_power_probe,
248+
.remove = macsmc_power_remove,
249+
};
250+
module_platform_driver(macsmc_power_driver);
251+
252+
MODULE_LICENSE("Dual MIT/GPL");
253+
MODULE_DESCRIPTION("Apple SMC battery and power management driver");
254+
MODULE_AUTHOR("Hector Martin <marcan@marcan.st>");
255+
MODULE_ALIAS("platform:macsmc-power");

0 commit comments

Comments
 (0)