1 // SPDX-License-Identifier: GPL-2.0-only
4 * Copyright (C) 2010 Nokia Corporation. All rights reserved.
5 * Copyright (C) 2014 Sebastian Reichel <sre@kernel.org>
7 * Contact: Carlos Chinea <carlos.chinea@nokia.com>
10 #include <linux/compiler.h>
11 #include <linux/err.h>
12 #include <linux/ioport.h>
14 #include <linux/clk.h>
15 #include <linux/device.h>
16 #include <linux/platform_device.h>
17 #include <linux/dma-mapping.h>
18 #include <linux/dmaengine.h>
19 #include <linux/delay.h>
20 #include <linux/seq_file.h>
21 #include <linux/scatterlist.h>
22 #include <linux/interrupt.h>
23 #include <linux/spinlock.h>
24 #include <linux/debugfs.h>
25 #include <linux/pinctrl/consumer.h>
26 #include <linux/pm_runtime.h>
27 #include <linux/of_platform.h>
28 #include <linux/hsi/hsi.h>
29 #include <linux/idr.h>
31 #include "omap_ssi_regs.h"
34 /* For automatically allocated device IDs */
35 static DEFINE_IDA(platform_omap_ssi_ida);
37 #ifdef CONFIG_DEBUG_FS
38 static int ssi_regs_show(struct seq_file *m, void *p __maybe_unused)
40 struct hsi_controller *ssi = m->private;
41 struct omap_ssi_controller *omap_ssi = hsi_controller_drvdata(ssi);
42 void __iomem *sys = omap_ssi->sys;
44 pm_runtime_get_sync(ssi->device.parent);
45 seq_printf(m, "REVISION\t: 0x%08x\n", readl(sys + SSI_REVISION_REG));
46 seq_printf(m, "SYSCONFIG\t: 0x%08x\n", readl(sys + SSI_SYSCONFIG_REG));
47 seq_printf(m, "SYSSTATUS\t: 0x%08x\n", readl(sys + SSI_SYSSTATUS_REG));
48 pm_runtime_put(ssi->device.parent);
53 static int ssi_gdd_regs_show(struct seq_file *m, void *p __maybe_unused)
55 struct hsi_controller *ssi = m->private;
56 struct omap_ssi_controller *omap_ssi = hsi_controller_drvdata(ssi);
57 void __iomem *gdd = omap_ssi->gdd;
58 void __iomem *sys = omap_ssi->sys;
61 pm_runtime_get_sync(ssi->device.parent);
63 seq_printf(m, "GDD_MPU_STATUS\t: 0x%08x\n",
64 readl(sys + SSI_GDD_MPU_IRQ_STATUS_REG));
65 seq_printf(m, "GDD_MPU_ENABLE\t: 0x%08x\n\n",
66 readl(sys + SSI_GDD_MPU_IRQ_ENABLE_REG));
67 seq_printf(m, "HW_ID\t\t: 0x%08x\n",
68 readl(gdd + SSI_GDD_HW_ID_REG));
69 seq_printf(m, "PPORT_ID\t: 0x%08x\n",
70 readl(gdd + SSI_GDD_PPORT_ID_REG));
71 seq_printf(m, "MPORT_ID\t: 0x%08x\n",
72 readl(gdd + SSI_GDD_MPORT_ID_REG));
73 seq_printf(m, "TEST\t\t: 0x%08x\n",
74 readl(gdd + SSI_GDD_TEST_REG));
75 seq_printf(m, "GCR\t\t: 0x%08x\n",
76 readl(gdd + SSI_GDD_GCR_REG));
78 for (lch = 0; lch < SSI_MAX_GDD_LCH; lch++) {
79 seq_printf(m, "\nGDD LCH %d\n=========\n", lch);
80 seq_printf(m, "CSDP\t\t: 0x%04x\n",
81 readw(gdd + SSI_GDD_CSDP_REG(lch)));
82 seq_printf(m, "CCR\t\t: 0x%04x\n",
83 readw(gdd + SSI_GDD_CCR_REG(lch)));
84 seq_printf(m, "CICR\t\t: 0x%04x\n",
85 readw(gdd + SSI_GDD_CICR_REG(lch)));
86 seq_printf(m, "CSR\t\t: 0x%04x\n",
87 readw(gdd + SSI_GDD_CSR_REG(lch)));
88 seq_printf(m, "CSSA\t\t: 0x%08x\n",
89 readl(gdd + SSI_GDD_CSSA_REG(lch)));
90 seq_printf(m, "CDSA\t\t: 0x%08x\n",
91 readl(gdd + SSI_GDD_CDSA_REG(lch)));
92 seq_printf(m, "CEN\t\t: 0x%04x\n",
93 readw(gdd + SSI_GDD_CEN_REG(lch)));
94 seq_printf(m, "CSAC\t\t: 0x%04x\n",
95 readw(gdd + SSI_GDD_CSAC_REG(lch)));
96 seq_printf(m, "CDAC\t\t: 0x%04x\n",
97 readw(gdd + SSI_GDD_CDAC_REG(lch)));
98 seq_printf(m, "CLNK_CTRL\t: 0x%04x\n",
99 readw(gdd + SSI_GDD_CLNK_CTRL_REG(lch)));
102 pm_runtime_put(ssi->device.parent);
107 DEFINE_SHOW_ATTRIBUTE(ssi_regs);
108 DEFINE_SHOW_ATTRIBUTE(ssi_gdd_regs);
110 static int ssi_debug_add_ctrl(struct hsi_controller *ssi)
112 struct omap_ssi_controller *omap_ssi = hsi_controller_drvdata(ssi);
116 omap_ssi->dir = debugfs_create_dir(dev_name(&ssi->device), NULL);
120 debugfs_create_file("regs", S_IRUGO, omap_ssi->dir, ssi,
123 dir = debugfs_create_dir("gdd", omap_ssi->dir);
126 debugfs_create_file("regs", S_IRUGO, dir, ssi, &ssi_gdd_regs_fops);
130 debugfs_remove_recursive(omap_ssi->dir);
135 static void ssi_debug_remove_ctrl(struct hsi_controller *ssi)
137 struct omap_ssi_controller *omap_ssi = hsi_controller_drvdata(ssi);
139 debugfs_remove_recursive(omap_ssi->dir);
141 #endif /* CONFIG_DEBUG_FS */
144 * FIXME: Horrible HACK needed until we remove the useless wakeline test
145 * in the CMT. To be removed !!!!
147 void ssi_waketest(struct hsi_client *cl, unsigned int enable)
149 struct hsi_port *port = hsi_get_port(cl);
150 struct omap_ssi_port *omap_port = hsi_port_drvdata(port);
151 struct hsi_controller *ssi = to_hsi_controller(port->device.parent);
152 struct omap_ssi_controller *omap_ssi = hsi_controller_drvdata(ssi);
154 omap_port->wktest = !!enable;
155 if (omap_port->wktest) {
156 pm_runtime_get_sync(ssi->device.parent);
157 writel_relaxed(SSI_WAKE(0),
158 omap_ssi->sys + SSI_SET_WAKE_REG(port->num));
160 writel_relaxed(SSI_WAKE(0),
161 omap_ssi->sys + SSI_CLEAR_WAKE_REG(port->num));
162 pm_runtime_put(ssi->device.parent);
165 EXPORT_SYMBOL_GPL(ssi_waketest);
167 static void ssi_gdd_complete(struct hsi_controller *ssi, unsigned int lch)
169 struct omap_ssi_controller *omap_ssi = hsi_controller_drvdata(ssi);
170 struct hsi_msg *msg = omap_ssi->gdd_trn[lch].msg;
171 struct hsi_port *port = to_hsi_port(msg->cl->device.parent);
172 struct omap_ssi_port *omap_port = hsi_port_drvdata(port);
177 spin_lock(&omap_ssi->lock);
179 val = readl(omap_ssi->sys + SSI_GDD_MPU_IRQ_ENABLE_REG);
180 val &= ~SSI_GDD_LCH(lch);
181 writel_relaxed(val, omap_ssi->sys + SSI_GDD_MPU_IRQ_ENABLE_REG);
183 if (msg->ttype == HSI_MSG_READ) {
184 dir = DMA_FROM_DEVICE;
185 val = SSI_DATAAVAILABLE(msg->channel);
186 pm_runtime_put(omap_port->pdev);
189 val = SSI_DATAACCEPT(msg->channel);
190 /* Keep clocks reference for write pio event */
192 dma_unmap_sg(&ssi->device, msg->sgt.sgl, msg->sgt.nents, dir);
193 csr = readw(omap_ssi->gdd + SSI_GDD_CSR_REG(lch));
194 omap_ssi->gdd_trn[lch].msg = NULL; /* release GDD lch */
195 dev_dbg(&port->device, "DMA completed ch %d ttype %d\n",
196 msg->channel, msg->ttype);
197 spin_unlock(&omap_ssi->lock);
198 if (csr & SSI_CSR_TOUR) { /* Timeout error */
199 msg->status = HSI_STATUS_ERROR;
201 spin_lock(&omap_port->lock);
202 list_del(&msg->link); /* Dequeue msg */
203 spin_unlock(&omap_port->lock);
205 list_add_tail(&msg->link, &omap_port->errqueue);
206 schedule_delayed_work(&omap_port->errqueue_work, 0);
209 spin_lock(&omap_port->lock);
210 val |= readl(omap_ssi->sys + SSI_MPU_ENABLE_REG(port->num, 0));
211 writel_relaxed(val, omap_ssi->sys + SSI_MPU_ENABLE_REG(port->num, 0));
212 spin_unlock(&omap_port->lock);
214 msg->status = HSI_STATUS_COMPLETED;
215 msg->actual_len = sg_dma_len(msg->sgt.sgl);
218 static void ssi_gdd_tasklet(unsigned long dev)
220 struct hsi_controller *ssi = (struct hsi_controller *)dev;
221 struct omap_ssi_controller *omap_ssi = hsi_controller_drvdata(ssi);
222 void __iomem *sys = omap_ssi->sys;
226 pm_runtime_get(ssi->device.parent);
228 if (!pm_runtime_active(ssi->device.parent)) {
229 dev_warn(ssi->device.parent, "ssi_gdd_tasklet called without runtime PM!\n");
230 pm_runtime_put(ssi->device.parent);
234 status_reg = readl(sys + SSI_GDD_MPU_IRQ_STATUS_REG);
235 for (lch = 0; lch < SSI_MAX_GDD_LCH; lch++) {
236 if (status_reg & SSI_GDD_LCH(lch))
237 ssi_gdd_complete(ssi, lch);
239 writel_relaxed(status_reg, sys + SSI_GDD_MPU_IRQ_STATUS_REG);
240 status_reg = readl(sys + SSI_GDD_MPU_IRQ_STATUS_REG);
242 pm_runtime_put(ssi->device.parent);
245 tasklet_hi_schedule(&omap_ssi->gdd_tasklet);
247 enable_irq(omap_ssi->gdd_irq);
251 static irqreturn_t ssi_gdd_isr(int irq, void *ssi)
253 struct omap_ssi_controller *omap_ssi = hsi_controller_drvdata(ssi);
255 tasklet_hi_schedule(&omap_ssi->gdd_tasklet);
256 disable_irq_nosync(irq);
261 static unsigned long ssi_get_clk_rate(struct hsi_controller *ssi)
263 struct omap_ssi_controller *omap_ssi = hsi_controller_drvdata(ssi);
264 unsigned long rate = clk_get_rate(omap_ssi->fck);
268 static int ssi_clk_event(struct notifier_block *nb, unsigned long event,
271 struct omap_ssi_controller *omap_ssi = container_of(nb,
272 struct omap_ssi_controller, fck_nb);
273 struct hsi_controller *ssi = to_hsi_controller(omap_ssi->dev);
274 struct clk_notifier_data *clk_data = data;
275 struct omap_ssi_port *omap_port;
279 case PRE_RATE_CHANGE:
280 dev_dbg(&ssi->device, "pre rate change\n");
282 for (i = 0; i < ssi->num_ports; i++) {
283 omap_port = omap_ssi->port[i];
288 /* Workaround for SWBREAK + CAwake down race in CMT */
289 disable_irq(omap_port->wake_irq);
291 /* stop all ssi communication */
292 pinctrl_pm_select_idle_state(omap_port->pdev);
293 udelay(1); /* wait for racing frames */
297 case ABORT_RATE_CHANGE:
298 dev_dbg(&ssi->device, "abort rate change\n");
300 case POST_RATE_CHANGE:
301 dev_dbg(&ssi->device, "post rate change (%lu -> %lu)\n",
302 clk_data->old_rate, clk_data->new_rate);
303 omap_ssi->fck_rate = DIV_ROUND_CLOSEST(clk_data->new_rate, 1000); /* kHz */
305 for (i = 0; i < ssi->num_ports; i++) {
306 omap_port = omap_ssi->port[i];
311 omap_ssi_port_update_fclk(ssi, omap_port);
313 /* resume ssi communication */
314 pinctrl_pm_select_default_state(omap_port->pdev);
315 enable_irq(omap_port->wake_irq);
326 static int ssi_get_iomem(struct platform_device *pd,
327 const char *name, void __iomem **pbase, dma_addr_t *phy)
329 struct resource *mem;
331 struct hsi_controller *ssi = platform_get_drvdata(pd);
333 mem = platform_get_resource_byname(pd, IORESOURCE_MEM, name);
334 base = devm_ioremap_resource(&ssi->device, mem);
336 return PTR_ERR(base);
346 static int ssi_add_controller(struct hsi_controller *ssi,
347 struct platform_device *pd)
349 struct omap_ssi_controller *omap_ssi;
352 omap_ssi = devm_kzalloc(&ssi->device, sizeof(*omap_ssi), GFP_KERNEL);
356 err = ida_simple_get(&platform_omap_ssi_ida, 0, 0, GFP_KERNEL);
361 ssi->owner = THIS_MODULE;
362 ssi->device.parent = &pd->dev;
363 dev_set_name(&ssi->device, "ssi%d", ssi->id);
364 hsi_controller_set_drvdata(ssi, omap_ssi);
365 omap_ssi->dev = &ssi->device;
366 err = ssi_get_iomem(pd, "sys", &omap_ssi->sys, NULL);
369 err = ssi_get_iomem(pd, "gdd", &omap_ssi->gdd, NULL);
372 err = platform_get_irq_byname(pd, "gdd_mpu");
375 omap_ssi->gdd_irq = err;
376 tasklet_init(&omap_ssi->gdd_tasklet, ssi_gdd_tasklet,
378 err = devm_request_irq(&ssi->device, omap_ssi->gdd_irq, ssi_gdd_isr,
381 dev_err(&ssi->device, "Request GDD IRQ %d failed (%d)",
382 omap_ssi->gdd_irq, err);
386 omap_ssi->port = devm_kcalloc(&ssi->device, ssi->num_ports,
387 sizeof(*omap_ssi->port), GFP_KERNEL);
388 if (!omap_ssi->port) {
393 omap_ssi->fck = devm_clk_get(&ssi->device, "ssi_ssr_fck");
394 if (IS_ERR(omap_ssi->fck)) {
395 dev_err(&pd->dev, "Could not acquire clock \"ssi_ssr_fck\": %li\n",
396 PTR_ERR(omap_ssi->fck));
401 omap_ssi->fck_nb.notifier_call = ssi_clk_event;
402 omap_ssi->fck_nb.priority = INT_MAX;
403 clk_notifier_register(omap_ssi->fck, &omap_ssi->fck_nb);
405 /* TODO: find register, which can be used to detect context loss */
406 omap_ssi->get_loss = NULL;
408 omap_ssi->max_speed = UINT_MAX;
409 spin_lock_init(&omap_ssi->lock);
410 err = hsi_register_controller(ssi);
418 ida_simple_remove(&platform_omap_ssi_ida, ssi->id);
422 static int ssi_hw_init(struct hsi_controller *ssi)
424 struct omap_ssi_controller *omap_ssi = hsi_controller_drvdata(ssi);
427 err = pm_runtime_resume_and_get(ssi->device.parent);
429 dev_err(&ssi->device, "runtime PM failed %d\n", err);
433 writel_relaxed(SSI_SWRESET, omap_ssi->gdd + SSI_GDD_GRST_REG);
434 /* Get FCK rate in kHz */
435 omap_ssi->fck_rate = DIV_ROUND_CLOSEST(ssi_get_clk_rate(ssi), 1000);
436 dev_dbg(&ssi->device, "SSI fck rate %lu kHz\n", omap_ssi->fck_rate);
438 writel_relaxed(SSI_CLK_AUTOGATING_ON, omap_ssi->sys + SSI_GDD_GCR_REG);
439 omap_ssi->gdd_gcr = SSI_CLK_AUTOGATING_ON;
440 pm_runtime_put_sync(ssi->device.parent);
445 static void ssi_remove_controller(struct hsi_controller *ssi)
447 struct omap_ssi_controller *omap_ssi = hsi_controller_drvdata(ssi);
449 tasklet_kill(&omap_ssi->gdd_tasklet);
450 hsi_unregister_controller(ssi);
451 clk_notifier_unregister(omap_ssi->fck, &omap_ssi->fck_nb);
452 ida_simple_remove(&platform_omap_ssi_ida, id);
455 static inline int ssi_of_get_available_ports_count(const struct device_node *np)
457 struct device_node *child;
460 for_each_available_child_of_node(np, child)
461 if (of_device_is_compatible(child, "ti,omap3-ssi-port"))
467 static int ssi_remove_ports(struct device *dev, void *c)
469 struct platform_device *pdev = to_platform_device(dev);
474 of_node_clear_flag(dev->of_node, OF_POPULATED);
475 of_device_unregister(pdev);
480 static int ssi_probe(struct platform_device *pd)
482 struct platform_device *childpdev;
483 struct device_node *np = pd->dev.of_node;
484 struct device_node *child;
485 struct hsi_controller *ssi;
490 dev_err(&pd->dev, "missing device tree data\n");
494 num_ports = ssi_of_get_available_ports_count(np);
496 ssi = hsi_alloc_controller(num_ports, GFP_KERNEL);
498 dev_err(&pd->dev, "No memory for controller\n");
502 platform_set_drvdata(pd, ssi);
504 err = ssi_add_controller(ssi, pd);
506 hsi_put_controller(ssi);
510 pm_runtime_enable(&pd->dev);
512 err = ssi_hw_init(ssi);
515 #ifdef CONFIG_DEBUG_FS
516 err = ssi_debug_add_ctrl(ssi);
521 for_each_available_child_of_node(np, child) {
522 if (!of_device_is_compatible(child, "ti,omap3-ssi-port"))
525 childpdev = of_platform_device_create(child, NULL, &pd->dev);
528 dev_err(&pd->dev, "failed to create ssi controller port\n");
534 dev_info(&pd->dev, "ssi controller %d initialized (%d ports)!\n",
538 device_for_each_child(&pd->dev, NULL, ssi_remove_ports);
540 ssi_remove_controller(ssi);
541 pm_runtime_disable(&pd->dev);
543 platform_set_drvdata(pd, NULL);
548 static int ssi_remove(struct platform_device *pd)
550 struct hsi_controller *ssi = platform_get_drvdata(pd);
552 /* cleanup of of_platform_populate() call */
553 device_for_each_child(&pd->dev, NULL, ssi_remove_ports);
555 #ifdef CONFIG_DEBUG_FS
556 ssi_debug_remove_ctrl(ssi);
558 ssi_remove_controller(ssi);
559 platform_set_drvdata(pd, NULL);
561 pm_runtime_disable(&pd->dev);
567 static int omap_ssi_runtime_suspend(struct device *dev)
569 struct hsi_controller *ssi = dev_get_drvdata(dev);
570 struct omap_ssi_controller *omap_ssi = hsi_controller_drvdata(ssi);
572 dev_dbg(dev, "runtime suspend!\n");
574 if (omap_ssi->get_loss)
575 omap_ssi->loss_count =
576 omap_ssi->get_loss(ssi->device.parent);
581 static int omap_ssi_runtime_resume(struct device *dev)
583 struct hsi_controller *ssi = dev_get_drvdata(dev);
584 struct omap_ssi_controller *omap_ssi = hsi_controller_drvdata(ssi);
586 dev_dbg(dev, "runtime resume!\n");
588 if ((omap_ssi->get_loss) && (omap_ssi->loss_count ==
589 omap_ssi->get_loss(ssi->device.parent)))
592 writel_relaxed(omap_ssi->gdd_gcr, omap_ssi->gdd + SSI_GDD_GCR_REG);
597 static const struct dev_pm_ops omap_ssi_pm_ops = {
598 SET_RUNTIME_PM_OPS(omap_ssi_runtime_suspend, omap_ssi_runtime_resume,
602 #define DEV_PM_OPS (&omap_ssi_pm_ops)
604 #define DEV_PM_OPS NULL
608 static const struct of_device_id omap_ssi_of_match[] = {
609 { .compatible = "ti,omap3-ssi", },
612 MODULE_DEVICE_TABLE(of, omap_ssi_of_match);
614 #define omap_ssi_of_match NULL
617 static struct platform_driver ssi_pdriver = {
619 .remove = ssi_remove,
623 .of_match_table = omap_ssi_of_match,
627 static int __init ssi_init(void) {
630 ret = platform_driver_register(&ssi_pdriver);
634 ret = platform_driver_register(&ssi_port_pdriver);
636 platform_driver_unregister(&ssi_pdriver);
642 module_init(ssi_init);
644 static void __exit ssi_exit(void) {
645 platform_driver_unregister(&ssi_port_pdriver);
646 platform_driver_unregister(&ssi_pdriver);
648 module_exit(ssi_exit);
650 MODULE_ALIAS("platform:omap_ssi");
651 MODULE_AUTHOR("Carlos Chinea <carlos.chinea@nokia.com>");
652 MODULE_AUTHOR("Sebastian Reichel <sre@kernel.org>");
653 MODULE_DESCRIPTION("Synchronous Serial Interface Driver");
654 MODULE_LICENSE("GPL v2");