Skip to content

Commit a0aa26a

Browse files
committed
Merge branch 'bits/120-spmi' into asahi-wip
2 parents cbcf9c3 + 466d0df commit a0aa26a

7 files changed

Lines changed: 256 additions & 0 deletions

File tree

MAINTAINERS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2298,10 +2298,12 @@ F: drivers/iommu/io-pgtable-dart.c
22982298
F: drivers/irqchip/irq-apple-aic.c
22992299
F: drivers/nvme/host/apple.c
23002300
F: drivers/nvmem/apple-efuses.c
2301+
F: drivers/nvmem/apple-spmi-nvmem.c
23012302
F: drivers/pinctrl/pinctrl-apple-gpio.c
23022303
F: drivers/pwm/pwm-apple.c
23032304
F: drivers/soc/apple/*
23042305
F: drivers/spi/spi-apple.c
2306+
F: drivers/spmi/spmi-apple-controller.c
23052307
F: drivers/video/backlight/apple_dwi_bl.c
23062308
F: drivers/watchdog/apple_wdt.c
23072309
F: include/dt-bindings/interrupt-controller/apple-aic.h

drivers/nvmem/Kconfig

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,19 @@ config NVMEM_APPLE_EFUSES
4040
This driver can also be built as a module. If so, the module will
4141
be called nvmem-apple-efuses.
4242

43+
config NVMEM_APPLE_SPMI
44+
tristate "Apple SPMI NVMEM"
45+
depends on ARCH_APPLE || COMPILE_TEST
46+
depends on SPMI
47+
select REGMAP_SPMI
48+
help
49+
Say y here to build a driver to expose NVMEM cells for a set of power
50+
and RTC-related settings on a SPMI-attached PMIC present on Apple
51+
devices, such as Apple Silicon Macs.
52+
53+
This driver can also be built as a module. If so, the module
54+
will be called apple-nvmem-spmi.
55+
4356
config NVMEM_BCM_OCOTP
4457
tristate "Broadcom On-Chip OTP Controller support"
4558
depends on ARCH_BCM_IPROC || COMPILE_TEST

drivers/nvmem/Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ obj-y += layouts/
1212
# Devices
1313
obj-$(CONFIG_NVMEM_APPLE_EFUSES) += nvmem-apple-efuses.o
1414
nvmem-apple-efuses-y := apple-efuses.o
15+
obj-$(CONFIG_NVMEM_APPLE_SPMI) += apple_nvmem_spmi.o
16+
apple_nvmem_spmi-y := apple-spmi-nvmem.o
1517
obj-$(CONFIG_NVMEM_BCM_OCOTP) += nvmem-bcm-ocotp.o
1618
nvmem-bcm-ocotp-y := bcm-ocotp.o
1719
obj-$(CONFIG_NVMEM_BRCM_NVRAM) += nvmem_brcm_nvram.o

drivers/nvmem/apple-spmi-nvmem.c

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// SPDX-License-Identifier: GPL-2.0-only OR MIT
2+
/*
3+
* Apple SPMI NVMEM driver
4+
*
5+
* Copyright The Asahi Linux Contributors
6+
*/
7+
8+
#include <linux/kernel.h>
9+
#include <linux/module.h>
10+
#include <linux/nvmem-provider.h>
11+
#include <linux/of.h>
12+
#include <linux/spmi.h>
13+
#include <linux/regmap.h>
14+
15+
static const struct regmap_config apple_spmi_regmap_config = {
16+
.reg_bits = 16,
17+
.val_bits = 8,
18+
.max_register = 0xffff,
19+
};
20+
21+
static int apple_spmi_nvmem_probe(struct spmi_device *sdev)
22+
{
23+
struct regmap *regmap;
24+
struct nvmem_config nvmem_cfg = {
25+
.dev = &sdev->dev,
26+
.name = "spmi_nvmem",
27+
.id = NVMEM_DEVID_AUTO,
28+
.word_size = 1,
29+
.stride = 1,
30+
.size = 0xffff,
31+
.reg_read = (void *)regmap_bulk_read,
32+
.reg_write = (void *)regmap_bulk_write,
33+
};
34+
35+
regmap = devm_regmap_init_spmi_ext(sdev, &apple_spmi_regmap_config);
36+
if (IS_ERR(regmap))
37+
return PTR_ERR(regmap);
38+
39+
nvmem_cfg.priv = regmap;
40+
41+
return PTR_ERR_OR_ZERO(devm_nvmem_register(&sdev->dev, &nvmem_cfg));
42+
}
43+
44+
static const struct of_device_id apple_spmi_nvmem_id_table[] = {
45+
{ .compatible = "apple,spmi-nvmem" },
46+
{ },
47+
};
48+
MODULE_DEVICE_TABLE(of, apple_spmi_nvmem_id_table);
49+
50+
static struct spmi_driver apple_spmi_nvmem_driver = {
51+
.probe = apple_spmi_nvmem_probe,
52+
.driver = {
53+
.name = "apple-spmi-nvmem",
54+
.of_match_table = apple_spmi_nvmem_id_table,
55+
},
56+
};
57+
58+
module_spmi_driver(apple_spmi_nvmem_driver);
59+
60+
MODULE_LICENSE("Dual MIT/GPL");
61+
MODULE_AUTHOR("Hector Martin <marcan@marcan.st>");
62+
MODULE_DESCRIPTION("Apple SPMI NVMEM driver");

drivers/spmi/Kconfig

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,14 @@ menuconfig SPMI
1111

1212
if SPMI
1313

14+
config SPMI_APPLE
15+
tristate "Apple SoC SPMI Controller platform driver"
16+
depends on ARCH_APPLE || COMPILE_TEST
17+
help
18+
If you say yes to this option, support will be included for the
19+
SPMI controller present on many Apple SoCs, including the
20+
t8103 (M1) and t600x (M1 Pro/Max).
21+
1422
config SPMI_HISI3670
1523
tristate "Hisilicon 3670 SPMI Controller"
1624
select IRQ_DOMAIN_HIERARCHY

drivers/spmi/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#
55
obj-$(CONFIG_SPMI) += spmi.o spmi-devres.o
66

7+
obj-$(CONFIG_SPMI_APPLE) += spmi-apple-controller.o
78
obj-$(CONFIG_SPMI_HISI3670) += hisi-spmi-controller.o
89
obj-$(CONFIG_SPMI_MSM_PMIC_ARB) += spmi-pmic-arb.o
910
obj-$(CONFIG_SPMI_MTK_PMIF) += spmi-mtk-pmif.o
Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
/*
3+
* Apple SoC SPMI device driver
4+
*
5+
* Copyright The Asahi Linux Contributors
6+
*
7+
* Inspired by:
8+
* OpenBSD support Copyright (c) 2021 Mark Kettenis <kettenis@openbsd.org>
9+
* Correllium support Copyright (C) 2021 Corellium LLC
10+
* hisi-spmi-controller.c
11+
* spmi-pmic-arb.c Copyright (c) 2021, The Linux Foundation.
12+
*/
13+
14+
#include <linux/io.h>
15+
#include <linux/iopoll.h>
16+
#include <linux/module.h>
17+
#include <linux/mod_devicetable.h>
18+
#include <linux/platform_device.h>
19+
#include <linux/spmi.h>
20+
21+
/* SPMI Controller Registers */
22+
#define SPMI_STATUS_REG 0
23+
#define SPMI_CMD_REG 0x4
24+
#define SPMI_RSP_REG 0x8
25+
26+
#define SPMI_RX_FIFO_EMPTY BIT(24)
27+
28+
#define REG_POLL_INTERVAL_US 10000
29+
#define REG_POLL_TIMEOUT_US (REG_POLL_INTERVAL_US * 5)
30+
31+
struct apple_spmi {
32+
void __iomem *regs;
33+
};
34+
35+
#define poll_reg(spmi, reg, val, cond) \
36+
readl_poll_timeout((spmi)->regs + (reg), (val), (cond), \
37+
REG_POLL_INTERVAL_US, REG_POLL_TIMEOUT_US)
38+
39+
static inline u32 apple_spmi_pack_cmd(u8 opc, u8 sid, u16 saddr, size_t len)
40+
{
41+
return opc | sid << 8 | saddr << 16 | (len - 1) | (1 << 15);
42+
}
43+
44+
/* Wait for Rx FIFO to have something */
45+
static int apple_spmi_wait_rx_not_empty(struct spmi_controller *ctrl)
46+
{
47+
struct apple_spmi *spmi = spmi_controller_get_drvdata(ctrl);
48+
int ret;
49+
u32 status;
50+
51+
ret = poll_reg(spmi, SPMI_STATUS_REG, status, !(status & SPMI_RX_FIFO_EMPTY));
52+
if (ret) {
53+
dev_err(&ctrl->dev,
54+
"failed to wait for RX FIFO not empty\n");
55+
return ret;
56+
}
57+
58+
return 0;
59+
}
60+
61+
static int spmi_read_cmd(struct spmi_controller *ctrl, u8 opc, u8 sid,
62+
u16 saddr, u8 *buf, size_t len)
63+
{
64+
struct apple_spmi *spmi = spmi_controller_get_drvdata(ctrl);
65+
u32 spmi_cmd = apple_spmi_pack_cmd(opc, sid, saddr, len);
66+
u32 rsp;
67+
size_t len_read = 0;
68+
u8 i;
69+
int ret;
70+
71+
writel(spmi_cmd, spmi->regs + SPMI_CMD_REG);
72+
73+
ret = apple_spmi_wait_rx_not_empty(ctrl);
74+
if (ret)
75+
return ret;
76+
77+
/* Discard SPMI reply status */
78+
readl(spmi->regs + SPMI_RSP_REG);
79+
80+
/* Read SPMI data reply */
81+
while (len_read < len) {
82+
rsp = readl(spmi->regs + SPMI_RSP_REG);
83+
i = 0;
84+
while ((len_read < len) && (i < 4)) {
85+
buf[len_read++] = ((0xff << (8 * i)) & rsp) >> (8 * i);
86+
i += 1;
87+
}
88+
}
89+
90+
return 0;
91+
}
92+
93+
static int spmi_write_cmd(struct spmi_controller *ctrl, u8 opc, u8 sid,
94+
u16 saddr, const u8 *buf, size_t len)
95+
{
96+
struct apple_spmi *spmi = spmi_controller_get_drvdata(ctrl);
97+
u32 spmi_cmd = apple_spmi_pack_cmd(opc, sid, saddr, len);
98+
size_t i = 0, j;
99+
int ret;
100+
101+
writel(spmi_cmd, spmi->regs + SPMI_CMD_REG);
102+
103+
while (i < len) {
104+
j = 0;
105+
spmi_cmd = 0;
106+
while ((j < 4) & (i < len))
107+
spmi_cmd |= buf[i++] << (j++ * 8);
108+
109+
writel(spmi_cmd, spmi->regs + SPMI_CMD_REG);
110+
}
111+
112+
ret = apple_spmi_wait_rx_not_empty(ctrl);
113+
if (ret)
114+
return ret;
115+
116+
/* Discard */
117+
readl(spmi->regs + SPMI_RSP_REG);
118+
119+
return 0;
120+
}
121+
122+
static int apple_spmi_probe(struct platform_device *pdev)
123+
{
124+
struct apple_spmi *spmi;
125+
struct spmi_controller *ctrl;
126+
int ret;
127+
128+
ctrl = devm_spmi_controller_alloc(&pdev->dev, sizeof(*spmi));
129+
if (IS_ERR(ctrl))
130+
return -ENOMEM;
131+
132+
spmi = spmi_controller_get_drvdata(ctrl);
133+
134+
spmi->regs = devm_platform_ioremap_resource(pdev, 0);
135+
if (IS_ERR(spmi->regs))
136+
return PTR_ERR(spmi->regs);
137+
138+
ctrl->dev.of_node = pdev->dev.of_node;
139+
140+
ctrl->read_cmd = spmi_read_cmd;
141+
ctrl->write_cmd = spmi_write_cmd;
142+
143+
ret = devm_spmi_controller_add(&pdev->dev, ctrl);
144+
if (ret)
145+
return dev_err_probe(&pdev->dev, ret,
146+
"spmi_controller_add failed\n");
147+
148+
return 0;
149+
}
150+
151+
static const struct of_device_id apple_spmi_match_table[] = {
152+
{ .compatible = "apple,spmi", },
153+
{}
154+
};
155+
MODULE_DEVICE_TABLE(of, apple_spmi_match_table);
156+
157+
static struct platform_driver apple_spmi_driver = {
158+
.probe = apple_spmi_probe,
159+
.driver = {
160+
.name = "apple-spmi",
161+
.of_match_table = apple_spmi_match_table,
162+
},
163+
};
164+
module_platform_driver(apple_spmi_driver);
165+
166+
MODULE_AUTHOR("Jean-Francois Bortolotti <jeff@borto.fr>");
167+
MODULE_DESCRIPTION("Apple SoC SPMI driver");
168+
MODULE_LICENSE("GPL");

0 commit comments

Comments
 (0)