GNU Linux-libre 4.14.313-gnu1
[releases.git] / drivers / scsi / ufs / ufshcd-pltfrm.c
1 /*
2  * Universal Flash Storage Host controller Platform bus based glue driver
3  *
4  * This code is based on drivers/scsi/ufs/ufshcd-pltfrm.c
5  * Copyright (C) 2011-2013 Samsung India Software Operations
6  *
7  * Authors:
8  *      Santosh Yaraganavi <santosh.sy@samsung.com>
9  *      Vinayak Holikatti <h.vinayak@samsung.com>
10  *
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>
17  *
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.
22  *
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.
34  */
35
36 #include <linux/platform_device.h>
37 #include <linux/pm_runtime.h>
38 #include <linux/of.h>
39
40 #include "ufshcd.h"
41 #include "ufshcd-pltfrm.h"
42
43 #define UFSHCD_DEFAULT_LANES_PER_DIRECTION              2
44
45 static int ufshcd_parse_clock_info(struct ufs_hba *hba)
46 {
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;
57
58         if (!np)
59                 goto out;
60
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;
69         }
70
71         if (cnt <= 0)
72                 goto out;
73
74         if (!of_get_property(np, "freq-table-hz", &len)) {
75                 dev_info(dev, "freq-table-hz property not specified\n");
76                 goto out;
77         }
78
79         if (len <= 0)
80                 goto out;
81
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;
87         }
88
89         clkfreq = devm_kzalloc(dev, sz * sizeof(*clkfreq),
90                         GFP_KERNEL);
91         if (!clkfreq) {
92                 ret = -ENOMEM;
93                 goto out;
94         }
95
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;
102         }
103
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;
109
110                 clki = devm_kzalloc(dev, sizeof(*clki), GFP_KERNEL);
111                 if (!clki) {
112                         ret = -ENOMEM;
113                         goto out;
114                 }
115
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);
122         }
123 out:
124         return ret;
125 }
126
127 static bool phandle_exists(const struct device_node *np,
128                            const char *phandle_name, int index)
129 {
130         struct device_node *parse_np = of_parse_phandle(np, phandle_name, index);
131
132         if (parse_np)
133                 of_node_put(parse_np);
134
135         return parse_np != NULL;
136 }
137
138 #define MAX_PROP_SIZE 32
139 static int ufshcd_populate_vreg(struct device *dev, const char *name,
140                                 struct ufs_vreg **out_vreg)
141 {
142         int ret = 0;
143         char prop_name[MAX_PROP_SIZE];
144         struct ufs_vreg *vreg = NULL;
145         struct device_node *np = dev->of_node;
146
147         if (!np) {
148                 dev_err(dev, "%s: non DT initialization\n", __func__);
149                 goto out;
150         }
151
152         snprintf(prop_name, MAX_PROP_SIZE, "%s-supply", name);
153         if (!phandle_exists(np, prop_name, 0)) {
154                 dev_info(dev, "%s: Unable to find %s regulator, assuming enabled\n",
155                                 __func__, prop_name);
156                 goto out;
157         }
158
159         vreg = devm_kzalloc(dev, sizeof(*vreg), GFP_KERNEL);
160         if (!vreg)
161                 return -ENOMEM;
162
163         vreg->name = kstrdup(name, GFP_KERNEL);
164
165         /* if fixed regulator no need further initialization */
166         snprintf(prop_name, MAX_PROP_SIZE, "%s-fixed-regulator", name);
167         if (of_property_read_bool(np, prop_name))
168                 goto out;
169
170         snprintf(prop_name, MAX_PROP_SIZE, "%s-max-microamp", name);
171         ret = of_property_read_u32(np, prop_name, &vreg->max_uA);
172         if (ret) {
173                 dev_err(dev, "%s: unable to find %s err %d\n",
174                                 __func__, prop_name, ret);
175                 goto out;
176         }
177
178         vreg->min_uA = 0;
179         if (!strcmp(name, "vcc")) {
180                 if (of_property_read_bool(np, "vcc-supply-1p8")) {
181                         vreg->min_uV = UFS_VREG_VCC_1P8_MIN_UV;
182                         vreg->max_uV = UFS_VREG_VCC_1P8_MAX_UV;
183                 } else {
184                         vreg->min_uV = UFS_VREG_VCC_MIN_UV;
185                         vreg->max_uV = UFS_VREG_VCC_MAX_UV;
186                 }
187         } else if (!strcmp(name, "vccq")) {
188                 vreg->min_uV = UFS_VREG_VCCQ_MIN_UV;
189                 vreg->max_uV = UFS_VREG_VCCQ_MAX_UV;
190         } else if (!strcmp(name, "vccq2")) {
191                 vreg->min_uV = UFS_VREG_VCCQ2_MIN_UV;
192                 vreg->max_uV = UFS_VREG_VCCQ2_MAX_UV;
193         }
194
195         goto out;
196
197 out:
198         if (!ret)
199                 *out_vreg = vreg;
200         return ret;
201 }
202
203 /**
204  * ufshcd_parse_regulator_info - get regulator info from device tree
205  * @hba: per adapter instance
206  *
207  * Get regulator info from device tree for vcc, vccq, vccq2 power supplies.
208  * If any of the supplies are not defined it is assumed that they are always-on
209  * and hence return zero. If the property is defined but parsing is failed
210  * then return corresponding error.
211  */
212 static int ufshcd_parse_regulator_info(struct ufs_hba *hba)
213 {
214         int err;
215         struct device *dev = hba->dev;
216         struct ufs_vreg_info *info = &hba->vreg_info;
217
218         err = ufshcd_populate_vreg(dev, "vdd-hba", &info->vdd_hba);
219         if (err)
220                 goto out;
221
222         err = ufshcd_populate_vreg(dev, "vcc", &info->vcc);
223         if (err)
224                 goto out;
225
226         err = ufshcd_populate_vreg(dev, "vccq", &info->vccq);
227         if (err)
228                 goto out;
229
230         err = ufshcd_populate_vreg(dev, "vccq2", &info->vccq2);
231 out:
232         return err;
233 }
234
235 #ifdef CONFIG_PM
236 /**
237  * ufshcd_pltfrm_suspend - suspend power management function
238  * @dev: pointer to device handle
239  *
240  * Returns 0 if successful
241  * Returns non-zero otherwise
242  */
243 int ufshcd_pltfrm_suspend(struct device *dev)
244 {
245         return ufshcd_system_suspend(dev_get_drvdata(dev));
246 }
247 EXPORT_SYMBOL_GPL(ufshcd_pltfrm_suspend);
248
249 /**
250  * ufshcd_pltfrm_resume - resume power management function
251  * @dev: pointer to device handle
252  *
253  * Returns 0 if successful
254  * Returns non-zero otherwise
255  */
256 int ufshcd_pltfrm_resume(struct device *dev)
257 {
258         return ufshcd_system_resume(dev_get_drvdata(dev));
259 }
260 EXPORT_SYMBOL_GPL(ufshcd_pltfrm_resume);
261
262 int ufshcd_pltfrm_runtime_suspend(struct device *dev)
263 {
264         return ufshcd_runtime_suspend(dev_get_drvdata(dev));
265 }
266 EXPORT_SYMBOL_GPL(ufshcd_pltfrm_runtime_suspend);
267
268 int ufshcd_pltfrm_runtime_resume(struct device *dev)
269 {
270         return ufshcd_runtime_resume(dev_get_drvdata(dev));
271 }
272 EXPORT_SYMBOL_GPL(ufshcd_pltfrm_runtime_resume);
273
274 int ufshcd_pltfrm_runtime_idle(struct device *dev)
275 {
276         return ufshcd_runtime_idle(dev_get_drvdata(dev));
277 }
278 EXPORT_SYMBOL_GPL(ufshcd_pltfrm_runtime_idle);
279
280 #endif /* CONFIG_PM */
281
282 void ufshcd_pltfrm_shutdown(struct platform_device *pdev)
283 {
284         ufshcd_shutdown((struct ufs_hba *)platform_get_drvdata(pdev));
285 }
286 EXPORT_SYMBOL_GPL(ufshcd_pltfrm_shutdown);
287
288 static void ufshcd_init_lanes_per_dir(struct ufs_hba *hba)
289 {
290         struct device *dev = hba->dev;
291         int ret;
292
293         ret = of_property_read_u32(dev->of_node, "lanes-per-direction",
294                 &hba->lanes_per_direction);
295         if (ret) {
296                 dev_dbg(hba->dev,
297                         "%s: failed to read lanes-per-direction, ret=%d\n",
298                         __func__, ret);
299                 hba->lanes_per_direction = UFSHCD_DEFAULT_LANES_PER_DIRECTION;
300         }
301 }
302
303 /**
304  * ufshcd_pltfrm_init - probe routine of the driver
305  * @pdev: pointer to Platform device handle
306  * @vops: pointer to variant ops
307  *
308  * Returns 0 on success, non-zero value on failure
309  */
310 int ufshcd_pltfrm_init(struct platform_device *pdev,
311                        struct ufs_hba_variant_ops *vops)
312 {
313         struct ufs_hba *hba;
314         void __iomem *mmio_base;
315         struct resource *mem_res;
316         int irq, err;
317         struct device *dev = &pdev->dev;
318
319         mem_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
320         mmio_base = devm_ioremap_resource(dev, mem_res);
321         if (IS_ERR(mmio_base)) {
322                 err = PTR_ERR(mmio_base);
323                 goto out;
324         }
325
326         irq = platform_get_irq(pdev, 0);
327         if (irq < 0) {
328                 dev_err(dev, "IRQ resource not available\n");
329                 err = -ENODEV;
330                 goto out;
331         }
332
333         err = ufshcd_alloc_host(dev, &hba);
334         if (err) {
335                 dev_err(&pdev->dev, "Allocation failed\n");
336                 goto out;
337         }
338
339         hba->vops = vops;
340
341         err = ufshcd_parse_clock_info(hba);
342         if (err) {
343                 dev_err(&pdev->dev, "%s: clock parse failed %d\n",
344                                 __func__, err);
345                 goto dealloc_host;
346         }
347         err = ufshcd_parse_regulator_info(hba);
348         if (err) {
349                 dev_err(&pdev->dev, "%s: regulator init failed %d\n",
350                                 __func__, err);
351                 goto dealloc_host;
352         }
353
354         ufshcd_init_lanes_per_dir(hba);
355
356         err = ufshcd_init(hba, mmio_base, irq);
357         if (err) {
358                 dev_err(dev, "Initialization failed\n");
359                 goto dealloc_host;
360         }
361
362         pm_runtime_set_active(&pdev->dev);
363         pm_runtime_enable(&pdev->dev);
364
365         return 0;
366
367 dealloc_host:
368         ufshcd_dealloc_host(hba);
369 out:
370         return err;
371 }
372 EXPORT_SYMBOL_GPL(ufshcd_pltfrm_init);
373
374 MODULE_AUTHOR("Santosh Yaragnavi <santosh.sy@samsung.com>");
375 MODULE_AUTHOR("Vinayak Holikatti <h.vinayak@samsung.com>");
376 MODULE_DESCRIPTION("UFS host controller Platform bus based glue driver");
377 MODULE_LICENSE("GPL");
378 MODULE_VERSION(UFSHCD_DRIVER_VERSION);