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
apple_spmi_pack_cmd(u8 opc,u8 sid,u16 saddr,size_t len)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 */
apple_spmi_wait_rx_not_empty(struct spmi_controller * ctrl)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
spmi_read_cmd(struct spmi_controller * ctrl,u8 opc,u8 sid,u16 saddr,u8 * buf,size_t len)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
spmi_write_cmd(struct spmi_controller * ctrl,u8 opc,u8 sid,u16 saddr,const u8 * buf,size_t len)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
apple_spmi_probe(struct platform_device * pdev)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");
169