1 // SPDX-License-Identifier: GPL-2.0
2 //
3 // Copyright (C) 2019 Texas Instruments Incorporated - http://www.ti.com/
4 //
5 
6 #include <linux/clk.h>
7 #include <linux/io.h>
8 #include <linux/kernel.h>
9 #include <linux/module.h>
10 #include <linux/of_platform.h>
11 #include <linux/platform_device.h>
12 #include <linux/pm_runtime.h>
13 
14 #define TI_UFS_SS_CTRL		0x4
15 #define TI_UFS_SS_RST_N_PCS	BIT(0)
16 #define TI_UFS_SS_CLK_26MHZ	BIT(4)
17 
ti_j721e_ufs_probe(struct platform_device * pdev)18 static int ti_j721e_ufs_probe(struct platform_device *pdev)
19 {
20 	struct device *dev = &pdev->dev;
21 	unsigned long clk_rate;
22 	void __iomem *regbase;
23 	struct clk *clk;
24 	u32 reg = 0;
25 	int ret;
26 
27 	regbase = devm_platform_ioremap_resource(pdev, 0);
28 	if (IS_ERR(regbase))
29 		return PTR_ERR(regbase);
30 
31 	pm_runtime_enable(dev);
32 	ret = pm_runtime_get_sync(dev);
33 	if (ret < 0) {
34 		pm_runtime_put_noidle(dev);
35 		goto disable_pm;
36 	}
37 
38 	/* Select MPHY refclk frequency */
39 	clk = devm_clk_get(dev, NULL);
40 	if (IS_ERR(clk)) {
41 		ret = PTR_ERR(clk);
42 		dev_err(dev, "Cannot claim MPHY clock.\n");
43 		goto clk_err;
44 	}
45 	clk_rate = clk_get_rate(clk);
46 	if (clk_rate == 26000000)
47 		reg |= TI_UFS_SS_CLK_26MHZ;
48 	devm_clk_put(dev, clk);
49 
50 	/*  Take UFS slave device out of reset */
51 	reg |= TI_UFS_SS_RST_N_PCS;
52 	writel(reg, regbase + TI_UFS_SS_CTRL);
53 
54 	ret = of_platform_populate(pdev->dev.of_node, NULL, NULL,
55 				   dev);
56 	if (ret) {
57 		dev_err(dev, "failed to populate child nodes %d\n", ret);
58 		goto clk_err;
59 	}
60 
61 	return ret;
62 
63 clk_err:
64 	pm_runtime_put_sync(dev);
65 disable_pm:
66 	pm_runtime_disable(dev);
67 	return ret;
68 }
69 
ti_j721e_ufs_remove(struct platform_device * pdev)70 static int ti_j721e_ufs_remove(struct platform_device *pdev)
71 {
72 	of_platform_depopulate(&pdev->dev);
73 	pm_runtime_put_sync(&pdev->dev);
74 	pm_runtime_disable(&pdev->dev);
75 
76 	return 0;
77 }
78 
79 static const struct of_device_id ti_j721e_ufs_of_match[] = {
80 	{
81 		.compatible = "ti,j721e-ufs",
82 	},
83 	{ },
84 };
85 
86 static struct platform_driver ti_j721e_ufs_driver = {
87 	.probe	= ti_j721e_ufs_probe,
88 	.remove	= ti_j721e_ufs_remove,
89 	.driver	= {
90 		.name   = "ti-j721e-ufs",
91 		.of_match_table = ti_j721e_ufs_of_match,
92 	},
93 };
94 module_platform_driver(ti_j721e_ufs_driver);
95 
96 MODULE_AUTHOR("Vignesh Raghavendra <vigneshr@ti.com>");
97 MODULE_DESCRIPTION("TI UFS host controller glue driver");
98 MODULE_LICENSE("GPL v2");
99