2 * Universal Flash Storage Host controller Platform bus based glue driver
4 * This code is based on drivers/scsi/ufs/ufshcd-pltfrm.c
5 * Copyright (C) 2011-2013 Samsung India Software Operations
8 * Santosh Yaraganavi <santosh.sy@samsung.com>
9 * Vinayak Holikatti <h.vinayak@samsung.com>
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation; either version 2
14 * of the License, or (at your option) any later version.
15 * See the COPYING file in the top-level directory or visit
16 * <http://www.gnu.org/licenses/gpl-2.0.html>
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * This program is provided "AS IS" and "WITH ALL FAULTS" and
24 * without warranty of any kind. You are solely responsible for
25 * determining the appropriateness of using and distributing
26 * the program and assume all risks associated with your exercise
27 * of rights with respect to the program, including but not limited
28 * to infringement of third party rights, the risks and costs of
29 * program errors, damage to or loss of data, programs or equipment,
30 * and unavailability or interruption of operations. Under no
31 * circumstances will the contributor of this Program be liable for
32 * any damages of any kind arising from your use or distribution of
36 #include <linux/platform_device.h>
37 #include <linux/pm_runtime.h>
42 static const struct of_device_id ufs_of_match[];
43 static struct ufs_hba_variant_ops *get_variant_ops(struct device *dev)
46 const struct of_device_id *match;
48 match = of_match_node(ufs_of_match, dev->of_node);
50 return (struct ufs_hba_variant_ops *)match->data;
56 static int ufshcd_parse_clock_info(struct ufs_hba *hba)
61 struct device *dev = hba->dev;
62 struct device_node *np = dev->of_node;
65 struct ufs_clk_info *clki;
72 INIT_LIST_HEAD(&hba->clk_list_head);
74 cnt = of_property_count_strings(np, "clock-names");
75 if (!cnt || (cnt == -EINVAL)) {
76 dev_info(dev, "%s: Unable to find clocks, assuming enabled\n",
79 dev_err(dev, "%s: count clock strings failed, err %d\n",
87 if (!of_get_property(np, "freq-table-hz", &len)) {
88 dev_info(dev, "freq-table-hz property not specified\n");
95 sz = len / sizeof(*clkfreq);
97 dev_err(dev, "%s len mismatch\n", "freq-table-hz");
102 clkfreq = devm_kzalloc(dev, sz * sizeof(*clkfreq),
109 ret = of_property_read_u32_array(np, "freq-table-hz",
111 if (ret && (ret != -EINVAL)) {
112 dev_err(dev, "%s: error reading array %d\n",
113 "freq-table-hz", ret);
117 for (i = 0; i < sz; i += 2) {
118 ret = of_property_read_string_index(np,
119 "clock-names", i/2, (const char **)&name);
123 clki = devm_kzalloc(dev, sizeof(*clki), GFP_KERNEL);
129 clki->min_freq = clkfreq[i];
130 clki->max_freq = clkfreq[i+1];
131 clki->name = kstrdup(name, GFP_KERNEL);
132 dev_dbg(dev, "%s: min %u max %u name %s\n", "freq-table-hz",
133 clki->min_freq, clki->max_freq, clki->name);
134 list_add_tail(&clki->list, &hba->clk_list_head);
140 #define MAX_PROP_SIZE 32
141 static int ufshcd_populate_vreg(struct device *dev, const char *name,
142 struct ufs_vreg **out_vreg)
145 char prop_name[MAX_PROP_SIZE];
146 struct ufs_vreg *vreg = NULL;
147 struct device_node *np = dev->of_node;
150 dev_err(dev, "%s: non DT initialization\n", __func__);
154 snprintf(prop_name, MAX_PROP_SIZE, "%s-supply", name);
155 if (!of_parse_phandle(np, prop_name, 0)) {
156 dev_info(dev, "%s: Unable to find %s regulator, assuming enabled\n",
157 __func__, prop_name);
161 vreg = devm_kzalloc(dev, sizeof(*vreg), GFP_KERNEL);
165 vreg->name = kstrdup(name, GFP_KERNEL);
167 /* if fixed regulator no need further initialization */
168 snprintf(prop_name, MAX_PROP_SIZE, "%s-fixed-regulator", name);
169 if (of_property_read_bool(np, prop_name))
172 snprintf(prop_name, MAX_PROP_SIZE, "%s-max-microamp", name);
173 ret = of_property_read_u32(np, prop_name, &vreg->max_uA);
175 dev_err(dev, "%s: unable to find %s err %d\n",
176 __func__, prop_name, ret);
181 if (!strcmp(name, "vcc")) {
182 if (of_property_read_bool(np, "vcc-supply-1p8")) {
183 vreg->min_uV = UFS_VREG_VCC_1P8_MIN_UV;
184 vreg->max_uV = UFS_VREG_VCC_1P8_MAX_UV;
186 vreg->min_uV = UFS_VREG_VCC_MIN_UV;
187 vreg->max_uV = UFS_VREG_VCC_MAX_UV;
189 } else if (!strcmp(name, "vccq")) {
190 vreg->min_uV = UFS_VREG_VCCQ_MIN_UV;
191 vreg->max_uV = UFS_VREG_VCCQ_MAX_UV;
192 } else if (!strcmp(name, "vccq2")) {
193 vreg->min_uV = UFS_VREG_VCCQ2_MIN_UV;
194 vreg->max_uV = UFS_VREG_VCCQ2_MAX_UV;
200 devm_kfree(dev, vreg);
209 * ufshcd_parse_regulator_info - get regulator info from device tree
210 * @hba: per adapter instance
212 * Get regulator info from device tree for vcc, vccq, vccq2 power supplies.
213 * If any of the supplies are not defined it is assumed that they are always-on
214 * and hence return zero. If the property is defined but parsing is failed
215 * then return corresponding error.
217 static int ufshcd_parse_regulator_info(struct ufs_hba *hba)
220 struct device *dev = hba->dev;
221 struct ufs_vreg_info *info = &hba->vreg_info;
223 err = ufshcd_populate_vreg(dev, "vdd-hba", &info->vdd_hba);
227 err = ufshcd_populate_vreg(dev, "vcc", &info->vcc);
231 err = ufshcd_populate_vreg(dev, "vccq", &info->vccq);
235 err = ufshcd_populate_vreg(dev, "vccq2", &info->vccq2);
242 * ufshcd_pltfrm_suspend - suspend power management function
243 * @dev: pointer to device handle
245 * Returns 0 if successful
246 * Returns non-zero otherwise
248 static int ufshcd_pltfrm_suspend(struct device *dev)
250 return ufshcd_system_suspend(dev_get_drvdata(dev));
254 * ufshcd_pltfrm_resume - resume power management function
255 * @dev: pointer to device handle
257 * Returns 0 if successful
258 * Returns non-zero otherwise
260 static int ufshcd_pltfrm_resume(struct device *dev)
262 return ufshcd_system_resume(dev_get_drvdata(dev));
265 #define ufshcd_pltfrm_suspend NULL
266 #define ufshcd_pltfrm_resume NULL
269 #ifdef CONFIG_PM_RUNTIME
270 static int ufshcd_pltfrm_runtime_suspend(struct device *dev)
272 return ufshcd_runtime_suspend(dev_get_drvdata(dev));
274 static int ufshcd_pltfrm_runtime_resume(struct device *dev)
276 return ufshcd_runtime_resume(dev_get_drvdata(dev));
278 static int ufshcd_pltfrm_runtime_idle(struct device *dev)
280 return ufshcd_runtime_idle(dev_get_drvdata(dev));
282 #else /* !CONFIG_PM_RUNTIME */
283 #define ufshcd_pltfrm_runtime_suspend NULL
284 #define ufshcd_pltfrm_runtime_resume NULL
285 #define ufshcd_pltfrm_runtime_idle NULL
286 #endif /* CONFIG_PM_RUNTIME */
288 static void ufshcd_pltfrm_shutdown(struct platform_device *pdev)
290 ufshcd_shutdown((struct ufs_hba *)platform_get_drvdata(pdev));
294 * ufshcd_pltfrm_probe - probe routine of the driver
295 * @pdev: pointer to Platform device handle
297 * Returns 0 on success, non-zero value on failure
299 static int ufshcd_pltfrm_probe(struct platform_device *pdev)
302 void __iomem *mmio_base;
303 struct resource *mem_res;
305 struct device *dev = &pdev->dev;
307 mem_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
308 mmio_base = devm_ioremap_resource(dev, mem_res);
309 if (IS_ERR(*(void **)&mmio_base)) {
310 err = PTR_ERR(*(void **)&mmio_base);
314 irq = platform_get_irq(pdev, 0);
316 dev_err(dev, "IRQ resource not available\n");
321 err = ufshcd_alloc_host(dev, &hba);
323 dev_err(&pdev->dev, "Allocation failed\n");
327 hba->vops = get_variant_ops(&pdev->dev);
329 err = ufshcd_parse_clock_info(hba);
331 dev_err(&pdev->dev, "%s: clock parse failed %d\n",
335 err = ufshcd_parse_regulator_info(hba);
337 dev_err(&pdev->dev, "%s: regulator init failed %d\n",
342 pm_runtime_set_active(&pdev->dev);
343 pm_runtime_enable(&pdev->dev);
345 err = ufshcd_init(hba, mmio_base, irq);
347 dev_err(dev, "Intialization failed\n");
348 goto out_disable_rpm;
351 platform_set_drvdata(pdev, hba);
356 pm_runtime_disable(&pdev->dev);
357 pm_runtime_set_suspended(&pdev->dev);
363 * ufshcd_pltfrm_remove - remove platform driver routine
364 * @pdev: pointer to platform device handle
366 * Returns 0 on success, non-zero value on failure
368 static int ufshcd_pltfrm_remove(struct platform_device *pdev)
370 struct ufs_hba *hba = platform_get_drvdata(pdev);
372 pm_runtime_get_sync(&(pdev)->dev);
377 static const struct of_device_id ufs_of_match[] = {
378 { .compatible = "jedec,ufs-1.1"},
382 static const struct dev_pm_ops ufshcd_dev_pm_ops = {
383 .suspend = ufshcd_pltfrm_suspend,
384 .resume = ufshcd_pltfrm_resume,
385 .runtime_suspend = ufshcd_pltfrm_runtime_suspend,
386 .runtime_resume = ufshcd_pltfrm_runtime_resume,
387 .runtime_idle = ufshcd_pltfrm_runtime_idle,
390 static struct platform_driver ufshcd_pltfrm_driver = {
391 .probe = ufshcd_pltfrm_probe,
392 .remove = ufshcd_pltfrm_remove,
393 .shutdown = ufshcd_pltfrm_shutdown,
396 .pm = &ufshcd_dev_pm_ops,
397 .of_match_table = ufs_of_match,
401 module_platform_driver(ufshcd_pltfrm_driver);
403 MODULE_AUTHOR("Santosh Yaragnavi <santosh.sy@samsung.com>");
404 MODULE_AUTHOR("Vinayak Holikatti <h.vinayak@samsung.com>");
405 MODULE_DESCRIPTION("UFS host controller Pltform bus based glue driver");
406 MODULE_LICENSE("GPL");
407 MODULE_VERSION(UFSHCD_DRIVER_VERSION);