1 // SPDX-License-Identifier: GPL-2.0
3 // Copyright (c) 2021 MediaTek Inc.
6 #include <linux/iopoll.h>
7 #include <linux/module.h>
9 #include <linux/platform_device.h>
10 #include <linux/property.h>
11 #include <linux/spmi.h>
13 #define SWINF_IDLE 0x00
14 #define SWINF_WFVLDCLR 0x06
16 #define GET_SWINF(x) (((x) >> 1) & 0x7)
18 #define PMIF_CMD_REG_0 0
19 #define PMIF_CMD_REG 1
20 #define PMIF_CMD_EXT_REG 2
21 #define PMIF_CMD_EXT_REG_LONG 3
23 #define PMIF_DELAY_US 10
24 #define PMIF_TIMEOUT_US (10 * 1000)
26 #define PMIF_CHAN_OFFSET 0x5
28 #define PMIF_MAX_CLKS 3
30 #define SPMI_OP_ST_BUSY 1
42 const u32 *spmimst_regs;
48 void __iomem *spmimst_base;
50 struct clk_bulk_data clks[PMIF_MAX_CLKS];
52 const struct pmif_data *data;
55 static const char * const pmif_clock_names[] = {
56 "pmif_sys_ck", "pmif_tmr_ck", "spmimst_clk_mux",
86 PMIF_SWINF_0_WDATA_31_0,
87 PMIF_SWINF_0_RDATA_31_0,
91 PMIF_SWINF_1_WDATA_31_0,
92 PMIF_SWINF_1_RDATA_31_0,
96 PMIF_SWINF_2_WDATA_31_0,
97 PMIF_SWINF_2_RDATA_31_0,
101 PMIF_SWINF_3_WDATA_31_0,
102 PMIF_SWINF_3_RDATA_31_0,
104 PMIF_SWINF_3_VLD_CLR,
107 static const u32 mt6873_regs[] = {
108 [PMIF_INIT_DONE] = 0x0000,
109 [PMIF_INF_EN] = 0x0024,
110 [PMIF_ARB_EN] = 0x0150,
111 [PMIF_CMDISSUE_EN] = 0x03B4,
112 [PMIF_TIMER_CTRL] = 0x03E0,
113 [PMIF_SPI_MODE_CTRL] = 0x0400,
114 [PMIF_IRQ_EVENT_EN_0] = 0x0418,
115 [PMIF_IRQ_FLAG_0] = 0x0420,
116 [PMIF_IRQ_CLR_0] = 0x0424,
117 [PMIF_IRQ_EVENT_EN_1] = 0x0428,
118 [PMIF_IRQ_FLAG_1] = 0x0430,
119 [PMIF_IRQ_CLR_1] = 0x0434,
120 [PMIF_IRQ_EVENT_EN_2] = 0x0438,
121 [PMIF_IRQ_FLAG_2] = 0x0440,
122 [PMIF_IRQ_CLR_2] = 0x0444,
123 [PMIF_IRQ_EVENT_EN_3] = 0x0448,
124 [PMIF_IRQ_FLAG_3] = 0x0450,
125 [PMIF_IRQ_CLR_3] = 0x0454,
126 [PMIF_IRQ_EVENT_EN_4] = 0x0458,
127 [PMIF_IRQ_FLAG_4] = 0x0460,
128 [PMIF_IRQ_CLR_4] = 0x0464,
129 [PMIF_WDT_EVENT_EN_0] = 0x046C,
130 [PMIF_WDT_FLAG_0] = 0x0470,
131 [PMIF_WDT_EVENT_EN_1] = 0x0474,
132 [PMIF_WDT_FLAG_1] = 0x0478,
133 [PMIF_SWINF_0_ACC] = 0x0C00,
134 [PMIF_SWINF_0_WDATA_31_0] = 0x0C04,
135 [PMIF_SWINF_0_RDATA_31_0] = 0x0C14,
136 [PMIF_SWINF_0_VLD_CLR] = 0x0C24,
137 [PMIF_SWINF_0_STA] = 0x0C28,
138 [PMIF_SWINF_1_ACC] = 0x0C40,
139 [PMIF_SWINF_1_WDATA_31_0] = 0x0C44,
140 [PMIF_SWINF_1_RDATA_31_0] = 0x0C54,
141 [PMIF_SWINF_1_VLD_CLR] = 0x0C64,
142 [PMIF_SWINF_1_STA] = 0x0C68,
143 [PMIF_SWINF_2_ACC] = 0x0C80,
144 [PMIF_SWINF_2_WDATA_31_0] = 0x0C84,
145 [PMIF_SWINF_2_RDATA_31_0] = 0x0C94,
146 [PMIF_SWINF_2_VLD_CLR] = 0x0CA4,
147 [PMIF_SWINF_2_STA] = 0x0CA8,
148 [PMIF_SWINF_3_ACC] = 0x0CC0,
149 [PMIF_SWINF_3_WDATA_31_0] = 0x0CC4,
150 [PMIF_SWINF_3_RDATA_31_0] = 0x0CD4,
151 [PMIF_SWINF_3_VLD_CLR] = 0x0CE4,
152 [PMIF_SWINF_3_STA] = 0x0CE8,
155 static const u32 mt8195_regs[] = {
156 [PMIF_INIT_DONE] = 0x0000,
157 [PMIF_INF_EN] = 0x0024,
158 [PMIF_ARB_EN] = 0x0150,
159 [PMIF_CMDISSUE_EN] = 0x03B8,
160 [PMIF_TIMER_CTRL] = 0x03E4,
161 [PMIF_SPI_MODE_CTRL] = 0x0408,
162 [PMIF_IRQ_EVENT_EN_0] = 0x0420,
163 [PMIF_IRQ_FLAG_0] = 0x0428,
164 [PMIF_IRQ_CLR_0] = 0x042C,
165 [PMIF_IRQ_EVENT_EN_1] = 0x0430,
166 [PMIF_IRQ_FLAG_1] = 0x0438,
167 [PMIF_IRQ_CLR_1] = 0x043C,
168 [PMIF_IRQ_EVENT_EN_2] = 0x0440,
169 [PMIF_IRQ_FLAG_2] = 0x0448,
170 [PMIF_IRQ_CLR_2] = 0x044C,
171 [PMIF_IRQ_EVENT_EN_3] = 0x0450,
172 [PMIF_IRQ_FLAG_3] = 0x0458,
173 [PMIF_IRQ_CLR_3] = 0x045C,
174 [PMIF_IRQ_EVENT_EN_4] = 0x0460,
175 [PMIF_IRQ_FLAG_4] = 0x0468,
176 [PMIF_IRQ_CLR_4] = 0x046C,
177 [PMIF_WDT_EVENT_EN_0] = 0x0474,
178 [PMIF_WDT_FLAG_0] = 0x0478,
179 [PMIF_WDT_EVENT_EN_1] = 0x047C,
180 [PMIF_WDT_FLAG_1] = 0x0480,
181 [PMIF_SWINF_0_ACC] = 0x0800,
182 [PMIF_SWINF_0_WDATA_31_0] = 0x0804,
183 [PMIF_SWINF_0_RDATA_31_0] = 0x0814,
184 [PMIF_SWINF_0_VLD_CLR] = 0x0824,
185 [PMIF_SWINF_0_STA] = 0x0828,
186 [PMIF_SWINF_1_ACC] = 0x0840,
187 [PMIF_SWINF_1_WDATA_31_0] = 0x0844,
188 [PMIF_SWINF_1_RDATA_31_0] = 0x0854,
189 [PMIF_SWINF_1_VLD_CLR] = 0x0864,
190 [PMIF_SWINF_1_STA] = 0x0868,
191 [PMIF_SWINF_2_ACC] = 0x0880,
192 [PMIF_SWINF_2_WDATA_31_0] = 0x0884,
193 [PMIF_SWINF_2_RDATA_31_0] = 0x0894,
194 [PMIF_SWINF_2_VLD_CLR] = 0x08A4,
195 [PMIF_SWINF_2_STA] = 0x08A8,
196 [PMIF_SWINF_3_ACC] = 0x08C0,
197 [PMIF_SWINF_3_WDATA_31_0] = 0x08C4,
198 [PMIF_SWINF_3_RDATA_31_0] = 0x08D4,
199 [PMIF_SWINF_3_VLD_CLR] = 0x08E4,
200 [PMIF_SWINF_3_STA] = 0x08E8,
217 /* MT8195 spmi regs */
227 static const u32 mt6873_spmi_regs[] = {
228 [SPMI_OP_ST_CTRL] = 0x0000,
229 [SPMI_GRP_ID_EN] = 0x0004,
230 [SPMI_OP_ST_STA] = 0x0008,
231 [SPMI_MST_SAMPL] = 0x000c,
232 [SPMI_MST_REQ_EN] = 0x0010,
233 [SPMI_REC_CTRL] = 0x0040,
234 [SPMI_REC0] = 0x0044,
235 [SPMI_REC1] = 0x0048,
236 [SPMI_REC2] = 0x004c,
237 [SPMI_REC3] = 0x0050,
238 [SPMI_REC4] = 0x0054,
239 [SPMI_MST_DBG] = 0x00fc,
242 static const u32 mt8195_spmi_regs[] = {
243 [SPMI_OP_ST_CTRL] = 0x0000,
244 [SPMI_GRP_ID_EN] = 0x0004,
245 [SPMI_OP_ST_STA] = 0x0008,
246 [SPMI_MST_SAMPL] = 0x000C,
247 [SPMI_MST_REQ_EN] = 0x0010,
248 [SPMI_MST_RCS_CTRL] = 0x0014,
249 [SPMI_SLV_3_0_EINT] = 0x0020,
250 [SPMI_SLV_7_4_EINT] = 0x0024,
251 [SPMI_SLV_B_8_EINT] = 0x0028,
252 [SPMI_SLV_F_C_EINT] = 0x002C,
253 [SPMI_REC_CTRL] = 0x0040,
254 [SPMI_REC0] = 0x0044,
255 [SPMI_REC1] = 0x0048,
256 [SPMI_REC2] = 0x004C,
257 [SPMI_REC3] = 0x0050,
258 [SPMI_REC4] = 0x0054,
259 [SPMI_REC_CMD_DEC] = 0x005C,
260 [SPMI_DEC_DBG] = 0x00F8,
261 [SPMI_MST_DBG] = 0x00FC,
264 static u32 pmif_readl(struct pmif *arb, enum pmif_regs reg)
266 return readl(arb->base + arb->data->regs[reg]);
269 static void pmif_writel(struct pmif *arb, u32 val, enum pmif_regs reg)
271 writel(val, arb->base + arb->data->regs[reg]);
274 static void mtk_spmi_writel(struct pmif *arb, u32 val, enum spmi_regs reg)
276 writel(val, arb->spmimst_base + arb->data->spmimst_regs[reg]);
279 static bool pmif_is_fsm_vldclr(struct pmif *arb)
283 reg_rdata = pmif_readl(arb, arb->chan.ch_sta);
285 return GET_SWINF(reg_rdata) == SWINF_WFVLDCLR;
288 static int pmif_arb_cmd(struct spmi_controller *ctrl, u8 opc, u8 sid)
290 struct pmif *arb = spmi_controller_get_drvdata(ctrl);
294 /* Check the opcode */
295 if (opc < SPMI_CMD_RESET || opc > SPMI_CMD_WAKEUP)
298 cmd = opc - SPMI_CMD_RESET;
300 mtk_spmi_writel(arb, (cmd << 0x4) | sid, SPMI_OP_ST_CTRL);
301 ret = readl_poll_timeout_atomic(arb->spmimst_base + arb->data->spmimst_regs[SPMI_OP_ST_STA],
302 rdata, (rdata & SPMI_OP_ST_BUSY) == SPMI_OP_ST_BUSY,
303 PMIF_DELAY_US, PMIF_TIMEOUT_US);
305 dev_err(&ctrl->dev, "timeout, err = %d\n", ret);
310 static int pmif_spmi_read_cmd(struct spmi_controller *ctrl, u8 opc, u8 sid,
311 u16 addr, u8 *buf, size_t len)
313 struct pmif *arb = spmi_controller_get_drvdata(ctrl);
314 struct ch_reg *inf_reg;
318 /* Check for argument validation. */
320 dev_err(&ctrl->dev, "exceed the max slv id\n");
325 dev_err(&ctrl->dev, "pmif supports 1..4 bytes per trans, but:%zu requested", len);
330 if (opc >= 0x60 && opc <= 0x7f)
332 else if ((opc >= 0x20 && opc <= 0x2f) || (opc >= 0x38 && opc <= 0x3f))
333 opc = PMIF_CMD_EXT_REG_LONG;
337 /* Wait for Software Interface FSM state to be IDLE. */
338 inf_reg = &arb->chan;
339 ret = readl_poll_timeout_atomic(arb->base + arb->data->regs[inf_reg->ch_sta],
340 data, GET_SWINF(data) == SWINF_IDLE,
341 PMIF_DELAY_US, PMIF_TIMEOUT_US);
343 /* set channel ready if the data has transferred */
344 if (pmif_is_fsm_vldclr(arb))
345 pmif_writel(arb, 1, inf_reg->ch_rdy);
346 dev_err(&ctrl->dev, "failed to wait for SWINF_IDLE\n");
350 /* Send the command. */
351 cmd = (opc << 30) | (sid << 24) | ((len - 1) << 16) | addr;
352 pmif_writel(arb, cmd, inf_reg->ch_send);
355 * Wait for Software Interface FSM state to be WFVLDCLR,
356 * read the data and clear the valid flag.
358 ret = readl_poll_timeout_atomic(arb->base + arb->data->regs[inf_reg->ch_sta],
359 data, GET_SWINF(data) == SWINF_WFVLDCLR,
360 PMIF_DELAY_US, PMIF_TIMEOUT_US);
362 dev_err(&ctrl->dev, "failed to wait for SWINF_WFVLDCLR\n");
366 data = pmif_readl(arb, inf_reg->rdata);
367 memcpy(buf, &data, len);
368 pmif_writel(arb, 1, inf_reg->ch_rdy);
373 static int pmif_spmi_write_cmd(struct spmi_controller *ctrl, u8 opc, u8 sid,
374 u16 addr, const u8 *buf, size_t len)
376 struct pmif *arb = spmi_controller_get_drvdata(ctrl);
377 struct ch_reg *inf_reg;
382 dev_err(&ctrl->dev, "pmif supports 1..4 bytes per trans, but:%zu requested", len);
387 /* Check the opcode */
388 if (opc >= 0x40 && opc <= 0x5F)
390 else if ((opc <= 0xF) || (opc >= 0x30 && opc <= 0x37))
391 opc = PMIF_CMD_EXT_REG_LONG;
392 else if (opc >= 0x80)
393 opc = PMIF_CMD_REG_0;
397 /* Wait for Software Interface FSM state to be IDLE. */
398 inf_reg = &arb->chan;
399 ret = readl_poll_timeout_atomic(arb->base + arb->data->regs[inf_reg->ch_sta],
400 data, GET_SWINF(data) == SWINF_IDLE,
401 PMIF_DELAY_US, PMIF_TIMEOUT_US);
403 /* set channel ready if the data has transferred */
404 if (pmif_is_fsm_vldclr(arb))
405 pmif_writel(arb, 1, inf_reg->ch_rdy);
406 dev_err(&ctrl->dev, "failed to wait for SWINF_IDLE\n");
410 /* Set the write data. */
411 memcpy(&data, buf, len);
412 pmif_writel(arb, data, inf_reg->wdata);
414 /* Send the command. */
415 cmd = (opc << 30) | BIT(29) | (sid << 24) | ((len - 1) << 16) | addr;
416 pmif_writel(arb, cmd, inf_reg->ch_send);
421 static const struct pmif_data mt6873_pmif_arb = {
423 .spmimst_regs = mt6873_spmi_regs,
427 static const struct pmif_data mt8195_pmif_arb = {
429 .spmimst_regs = mt8195_spmi_regs,
433 static int mtk_spmi_probe(struct platform_device *pdev)
436 struct spmi_controller *ctrl;
440 ctrl = spmi_controller_alloc(&pdev->dev, sizeof(*arb));
444 arb = spmi_controller_get_drvdata(ctrl);
445 arb->data = device_get_match_data(&pdev->dev);
448 dev_err(&pdev->dev, "Cannot get drv_data\n");
452 arb->base = devm_platform_ioremap_resource_byname(pdev, "pmif");
453 if (IS_ERR(arb->base)) {
454 err = PTR_ERR(arb->base);
458 arb->spmimst_base = devm_platform_ioremap_resource_byname(pdev, "spmimst");
459 if (IS_ERR(arb->spmimst_base)) {
460 err = PTR_ERR(arb->spmimst_base);
464 arb->nclks = ARRAY_SIZE(pmif_clock_names);
465 for (i = 0; i < arb->nclks; i++)
466 arb->clks[i].id = pmif_clock_names[i];
468 err = devm_clk_bulk_get(&pdev->dev, arb->nclks, arb->clks);
470 dev_err(&pdev->dev, "Failed to get clocks: %d\n", err);
474 err = clk_bulk_prepare_enable(arb->nclks, arb->clks);
476 dev_err(&pdev->dev, "Failed to enable clocks: %d\n", err);
480 ctrl->cmd = pmif_arb_cmd;
481 ctrl->read_cmd = pmif_spmi_read_cmd;
482 ctrl->write_cmd = pmif_spmi_write_cmd;
484 chan_offset = PMIF_CHAN_OFFSET * arb->data->soc_chan;
485 arb->chan.ch_sta = PMIF_SWINF_0_STA + chan_offset;
486 arb->chan.wdata = PMIF_SWINF_0_WDATA_31_0 + chan_offset;
487 arb->chan.rdata = PMIF_SWINF_0_RDATA_31_0 + chan_offset;
488 arb->chan.ch_send = PMIF_SWINF_0_ACC + chan_offset;
489 arb->chan.ch_rdy = PMIF_SWINF_0_VLD_CLR + chan_offset;
491 platform_set_drvdata(pdev, ctrl);
493 err = spmi_controller_add(ctrl);
495 goto err_domain_remove;
500 clk_bulk_disable_unprepare(arb->nclks, arb->clks);
502 spmi_controller_put(ctrl);
506 static int mtk_spmi_remove(struct platform_device *pdev)
508 struct spmi_controller *ctrl = platform_get_drvdata(pdev);
509 struct pmif *arb = spmi_controller_get_drvdata(ctrl);
511 clk_bulk_disable_unprepare(arb->nclks, arb->clks);
512 spmi_controller_remove(ctrl);
513 spmi_controller_put(ctrl);
517 static const struct of_device_id mtk_spmi_match_table[] = {
519 .compatible = "mediatek,mt6873-spmi",
520 .data = &mt6873_pmif_arb,
522 .compatible = "mediatek,mt8195-spmi",
523 .data = &mt8195_pmif_arb,
528 MODULE_DEVICE_TABLE(of, mtk_spmi_match_table);
530 static struct platform_driver mtk_spmi_driver = {
533 .of_match_table = of_match_ptr(mtk_spmi_match_table),
535 .probe = mtk_spmi_probe,
536 .remove = mtk_spmi_remove,
538 module_platform_driver(mtk_spmi_driver);
540 MODULE_AUTHOR("Hsin-Hsiung Wang <hsin-hsiung.wang@mediatek.com>");
541 MODULE_DESCRIPTION("MediaTek SPMI Driver");
542 MODULE_LICENSE("GPL");