Linux 4.16.11
[linux/fpc-iii.git] / drivers / scsi / ufs / ufshcd-pltfrm.c
blobe82bde0772963cbbde59a52712984f56921d39d9
1 /*
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
7 * Authors:
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
33 * this program.
36 #include <linux/platform_device.h>
37 #include <linux/pm_runtime.h>
38 #include <linux/of.h>
40 #include "ufshcd.h"
41 #include "ufshcd-pltfrm.h"
43 #define UFSHCD_DEFAULT_LANES_PER_DIRECTION 2
45 static int ufshcd_parse_clock_info(struct ufs_hba *hba)
47 int ret = 0;
48 int cnt;
49 int i;
50 struct device *dev = hba->dev;
51 struct device_node *np = dev->of_node;
52 char *name;
53 u32 *clkfreq = NULL;
54 struct ufs_clk_info *clki;
55 int len = 0;
56 size_t sz = 0;
58 if (!np)
59 goto out;
61 cnt = of_property_count_strings(np, "clock-names");
62 if (!cnt || (cnt == -EINVAL)) {
63 dev_info(dev, "%s: Unable to find clocks, assuming enabled\n",
64 __func__);
65 } else if (cnt < 0) {
66 dev_err(dev, "%s: count clock strings failed, err %d\n",
67 __func__, cnt);
68 ret = cnt;
71 if (cnt <= 0)
72 goto out;
74 if (!of_get_property(np, "freq-table-hz", &len)) {
75 dev_info(dev, "freq-table-hz property not specified\n");
76 goto out;
79 if (len <= 0)
80 goto out;
82 sz = len / sizeof(*clkfreq);
83 if (sz != 2 * cnt) {
84 dev_err(dev, "%s len mismatch\n", "freq-table-hz");
85 ret = -EINVAL;
86 goto out;
89 clkfreq = devm_kzalloc(dev, sz * sizeof(*clkfreq),
90 GFP_KERNEL);
91 if (!clkfreq) {
92 ret = -ENOMEM;
93 goto out;
96 ret = of_property_read_u32_array(np, "freq-table-hz",
97 clkfreq, sz);
98 if (ret && (ret != -EINVAL)) {
99 dev_err(dev, "%s: error reading array %d\n",
100 "freq-table-hz", ret);
101 return ret;
104 for (i = 0; i < sz; i += 2) {
105 ret = of_property_read_string_index(np,
106 "clock-names", i/2, (const char **)&name);
107 if (ret)
108 goto out;
110 clki = devm_kzalloc(dev, sizeof(*clki), GFP_KERNEL);
111 if (!clki) {
112 ret = -ENOMEM;
113 goto out;
116 clki->min_freq = clkfreq[i];
117 clki->max_freq = clkfreq[i+1];
118 clki->name = kstrdup(name, GFP_KERNEL);
119 dev_dbg(dev, "%s: min %u max %u name %s\n", "freq-table-hz",
120 clki->min_freq, clki->max_freq, clki->name);
121 list_add_tail(&clki->list, &hba->clk_list_head);
123 out:
124 return ret;
127 #define MAX_PROP_SIZE 32
128 static int ufshcd_populate_vreg(struct device *dev, const char *name,
129 struct ufs_vreg **out_vreg)
131 int ret = 0;
132 char prop_name[MAX_PROP_SIZE];
133 struct ufs_vreg *vreg = NULL;
134 struct device_node *np = dev->of_node;
136 if (!np) {
137 dev_err(dev, "%s: non DT initialization\n", __func__);
138 goto out;
141 snprintf(prop_name, MAX_PROP_SIZE, "%s-supply", name);
142 if (!of_parse_phandle(np, prop_name, 0)) {
143 dev_info(dev, "%s: Unable to find %s regulator, assuming enabled\n",
144 __func__, prop_name);
145 goto out;
148 vreg = devm_kzalloc(dev, sizeof(*vreg), GFP_KERNEL);
149 if (!vreg)
150 return -ENOMEM;
152 vreg->name = kstrdup(name, GFP_KERNEL);
154 /* if fixed regulator no need further initialization */
155 snprintf(prop_name, MAX_PROP_SIZE, "%s-fixed-regulator", name);
156 if (of_property_read_bool(np, prop_name))
157 goto out;
159 snprintf(prop_name, MAX_PROP_SIZE, "%s-max-microamp", name);
160 ret = of_property_read_u32(np, prop_name, &vreg->max_uA);
161 if (ret) {
162 dev_err(dev, "%s: unable to find %s err %d\n",
163 __func__, prop_name, ret);
164 goto out;
167 vreg->min_uA = 0;
168 if (!strcmp(name, "vcc")) {
169 if (of_property_read_bool(np, "vcc-supply-1p8")) {
170 vreg->min_uV = UFS_VREG_VCC_1P8_MIN_UV;
171 vreg->max_uV = UFS_VREG_VCC_1P8_MAX_UV;
172 } else {
173 vreg->min_uV = UFS_VREG_VCC_MIN_UV;
174 vreg->max_uV = UFS_VREG_VCC_MAX_UV;
176 } else if (!strcmp(name, "vccq")) {
177 vreg->min_uV = UFS_VREG_VCCQ_MIN_UV;
178 vreg->max_uV = UFS_VREG_VCCQ_MAX_UV;
179 } else if (!strcmp(name, "vccq2")) {
180 vreg->min_uV = UFS_VREG_VCCQ2_MIN_UV;
181 vreg->max_uV = UFS_VREG_VCCQ2_MAX_UV;
184 goto out;
186 out:
187 if (!ret)
188 *out_vreg = vreg;
189 return ret;
193 * ufshcd_parse_regulator_info - get regulator info from device tree
194 * @hba: per adapter instance
196 * Get regulator info from device tree for vcc, vccq, vccq2 power supplies.
197 * If any of the supplies are not defined it is assumed that they are always-on
198 * and hence return zero. If the property is defined but parsing is failed
199 * then return corresponding error.
201 static int ufshcd_parse_regulator_info(struct ufs_hba *hba)
203 int err;
204 struct device *dev = hba->dev;
205 struct ufs_vreg_info *info = &hba->vreg_info;
207 err = ufshcd_populate_vreg(dev, "vdd-hba", &info->vdd_hba);
208 if (err)
209 goto out;
211 err = ufshcd_populate_vreg(dev, "vcc", &info->vcc);
212 if (err)
213 goto out;
215 err = ufshcd_populate_vreg(dev, "vccq", &info->vccq);
216 if (err)
217 goto out;
219 err = ufshcd_populate_vreg(dev, "vccq2", &info->vccq2);
220 out:
221 return err;
224 #ifdef CONFIG_PM
226 * ufshcd_pltfrm_suspend - suspend power management function
227 * @dev: pointer to device handle
229 * Returns 0 if successful
230 * Returns non-zero otherwise
232 int ufshcd_pltfrm_suspend(struct device *dev)
234 return ufshcd_system_suspend(dev_get_drvdata(dev));
236 EXPORT_SYMBOL_GPL(ufshcd_pltfrm_suspend);
239 * ufshcd_pltfrm_resume - resume power management function
240 * @dev: pointer to device handle
242 * Returns 0 if successful
243 * Returns non-zero otherwise
245 int ufshcd_pltfrm_resume(struct device *dev)
247 return ufshcd_system_resume(dev_get_drvdata(dev));
249 EXPORT_SYMBOL_GPL(ufshcd_pltfrm_resume);
251 int ufshcd_pltfrm_runtime_suspend(struct device *dev)
253 return ufshcd_runtime_suspend(dev_get_drvdata(dev));
255 EXPORT_SYMBOL_GPL(ufshcd_pltfrm_runtime_suspend);
257 int ufshcd_pltfrm_runtime_resume(struct device *dev)
259 return ufshcd_runtime_resume(dev_get_drvdata(dev));
261 EXPORT_SYMBOL_GPL(ufshcd_pltfrm_runtime_resume);
263 int ufshcd_pltfrm_runtime_idle(struct device *dev)
265 return ufshcd_runtime_idle(dev_get_drvdata(dev));
267 EXPORT_SYMBOL_GPL(ufshcd_pltfrm_runtime_idle);
269 #endif /* CONFIG_PM */
271 void ufshcd_pltfrm_shutdown(struct platform_device *pdev)
273 ufshcd_shutdown((struct ufs_hba *)platform_get_drvdata(pdev));
275 EXPORT_SYMBOL_GPL(ufshcd_pltfrm_shutdown);
277 static void ufshcd_init_lanes_per_dir(struct ufs_hba *hba)
279 struct device *dev = hba->dev;
280 int ret;
282 ret = of_property_read_u32(dev->of_node, "lanes-per-direction",
283 &hba->lanes_per_direction);
284 if (ret) {
285 dev_dbg(hba->dev,
286 "%s: failed to read lanes-per-direction, ret=%d\n",
287 __func__, ret);
288 hba->lanes_per_direction = UFSHCD_DEFAULT_LANES_PER_DIRECTION;
293 * ufshcd_pltfrm_init - probe routine of the driver
294 * @pdev: pointer to Platform device handle
295 * @vops: pointer to variant ops
297 * Returns 0 on success, non-zero value on failure
299 int ufshcd_pltfrm_init(struct platform_device *pdev,
300 struct ufs_hba_variant_ops *vops)
302 struct ufs_hba *hba;
303 void __iomem *mmio_base;
304 struct resource *mem_res;
305 int irq, err;
306 struct device *dev = &pdev->dev;
308 mem_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
309 mmio_base = devm_ioremap_resource(dev, mem_res);
310 if (IS_ERR(mmio_base)) {
311 err = PTR_ERR(mmio_base);
312 goto out;
315 irq = platform_get_irq(pdev, 0);
316 if (irq < 0) {
317 dev_err(dev, "IRQ resource not available\n");
318 err = -ENODEV;
319 goto out;
322 err = ufshcd_alloc_host(dev, &hba);
323 if (err) {
324 dev_err(&pdev->dev, "Allocation failed\n");
325 goto out;
328 hba->vops = vops;
330 err = ufshcd_parse_clock_info(hba);
331 if (err) {
332 dev_err(&pdev->dev, "%s: clock parse failed %d\n",
333 __func__, err);
334 goto dealloc_host;
336 err = ufshcd_parse_regulator_info(hba);
337 if (err) {
338 dev_err(&pdev->dev, "%s: regulator init failed %d\n",
339 __func__, err);
340 goto dealloc_host;
343 pm_runtime_set_active(&pdev->dev);
344 pm_runtime_enable(&pdev->dev);
346 ufshcd_init_lanes_per_dir(hba);
348 err = ufshcd_init(hba, mmio_base, irq);
349 if (err) {
350 dev_err(dev, "Initialization failed\n");
351 goto out_disable_rpm;
354 platform_set_drvdata(pdev, hba);
356 return 0;
358 out_disable_rpm:
359 pm_runtime_disable(&pdev->dev);
360 pm_runtime_set_suspended(&pdev->dev);
361 dealloc_host:
362 ufshcd_dealloc_host(hba);
363 out:
364 return err;
366 EXPORT_SYMBOL_GPL(ufshcd_pltfrm_init);
368 MODULE_AUTHOR("Santosh Yaragnavi <santosh.sy@samsung.com>");
369 MODULE_AUTHOR("Vinayak Holikatti <h.vinayak@samsung.com>");
370 MODULE_DESCRIPTION("UFS host controller Platform bus based glue driver");
371 MODULE_LICENSE("GPL");
372 MODULE_VERSION(UFSHCD_DRIVER_VERSION);