GNU Linux-libre 4.9.337-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         INIT_LIST_HEAD(&hba->clk_list_head);
62
63         cnt = of_property_count_strings(np, "clock-names");
64         if (!cnt || (cnt == -EINVAL)) {
65                 dev_info(dev, "%s: Unable to find clocks, assuming enabled\n",
66                                 __func__);
67         } else if (cnt < 0) {
68                 dev_err(dev, "%s: count clock strings failed, err %d\n",
69                                 __func__, cnt);
70                 ret = cnt;
71         }
72
73         if (cnt <= 0)
74                 goto out;
75
76         if (!of_get_property(np, "freq-table-hz", &len)) {
77                 dev_info(dev, "freq-table-hz property not specified\n");
78                 goto out;
79         }
80
81         if (len <= 0)
82                 goto out;
83
84         sz = len / sizeof(*clkfreq);
85         if (sz != 2 * cnt) {
86                 dev_err(dev, "%s len mismatch\n", "freq-table-hz");
87                 ret = -EINVAL;
88                 goto out;
89         }
90
91         clkfreq = devm_kzalloc(dev, sz * sizeof(*clkfreq),
92                         GFP_KERNEL);
93         if (!clkfreq) {
94                 ret = -ENOMEM;
95                 goto out;
96         }
97
98         ret = of_property_read_u32_array(np, "freq-table-hz",
99                         clkfreq, sz);
100         if (ret && (ret != -EINVAL)) {
101                 dev_err(dev, "%s: error reading array %d\n",
102                                 "freq-table-hz", ret);
103                 return ret;
104         }
105
106         for (i = 0; i < sz; i += 2) {
107                 ret = of_property_read_string_index(np,
108                                 "clock-names", i/2, (const char **)&name);
109                 if (ret)
110                         goto out;
111
112                 clki = devm_kzalloc(dev, sizeof(*clki), GFP_KERNEL);
113                 if (!clki) {
114                         ret = -ENOMEM;
115                         goto out;
116                 }
117
118                 clki->min_freq = clkfreq[i];
119                 clki->max_freq = clkfreq[i+1];
120                 clki->name = kstrdup(name, GFP_KERNEL);
121                 dev_dbg(dev, "%s: min %u max %u name %s\n", "freq-table-hz",
122                                 clki->min_freq, clki->max_freq, clki->name);
123                 list_add_tail(&clki->list, &hba->clk_list_head);
124         }
125 out:
126         return ret;
127 }
128
129 static bool phandle_exists(const struct device_node *np,
130                            const char *phandle_name, int index)
131 {
132         struct device_node *parse_np = of_parse_phandle(np, phandle_name, index);
133
134         if (parse_np)
135                 of_node_put(parse_np);
136
137         return parse_np != NULL;
138 }
139
140 #define MAX_PROP_SIZE 32
141 static int ufshcd_populate_vreg(struct device *dev, const char *name,
142                                 struct ufs_vreg **out_vreg)
143 {
144         int ret = 0;
145         char prop_name[MAX_PROP_SIZE];
146         struct ufs_vreg *vreg = NULL;
147         struct device_node *np = dev->of_node;
148
149         if (!np) {
150                 dev_err(dev, "%s: non DT initialization\n", __func__);
151                 goto out;
152         }
153
154         snprintf(prop_name, MAX_PROP_SIZE, "%s-supply", name);
155         if (!phandle_exists(np, prop_name, 0)) {
156                 dev_info(dev, "%s: Unable to find %s regulator, assuming enabled\n",
157                                 __func__, prop_name);
158                 goto out;
159         }
160
161         vreg = devm_kzalloc(dev, sizeof(*vreg), GFP_KERNEL);
162         if (!vreg)
163                 return -ENOMEM;
164
165         vreg->name = kstrdup(name, GFP_KERNEL);
166
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))
170                 goto out;
171
172         snprintf(prop_name, MAX_PROP_SIZE, "%s-max-microamp", name);
173         ret = of_property_read_u32(np, prop_name, &vreg->max_uA);
174         if (ret) {
175                 dev_err(dev, "%s: unable to find %s err %d\n",
176                                 __func__, prop_name, ret);
177                 goto out;
178         }
179
180         vreg->min_uA = 0;
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;
185                 } else {
186                         vreg->min_uV = UFS_VREG_VCC_MIN_UV;
187                         vreg->max_uV = UFS_VREG_VCC_MAX_UV;
188                 }
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;
195         }
196
197         goto out;
198
199 out:
200         if (!ret)
201                 *out_vreg = vreg;
202         return ret;
203 }
204
205 /**
206  * ufshcd_parse_regulator_info - get regulator info from device tree
207  * @hba: per adapter instance
208  *
209  * Get regulator info from device tree for vcc, vccq, vccq2 power supplies.
210  * If any of the supplies are not defined it is assumed that they are always-on
211  * and hence return zero. If the property is defined but parsing is failed
212  * then return corresponding error.
213  */
214 static int ufshcd_parse_regulator_info(struct ufs_hba *hba)
215 {
216         int err;
217         struct device *dev = hba->dev;
218         struct ufs_vreg_info *info = &hba->vreg_info;
219
220         err = ufshcd_populate_vreg(dev, "vdd-hba", &info->vdd_hba);
221         if (err)
222                 goto out;
223
224         err = ufshcd_populate_vreg(dev, "vcc", &info->vcc);
225         if (err)
226                 goto out;
227
228         err = ufshcd_populate_vreg(dev, "vccq", &info->vccq);
229         if (err)
230                 goto out;
231
232         err = ufshcd_populate_vreg(dev, "vccq2", &info->vccq2);
233 out:
234         return err;
235 }
236
237 #ifdef CONFIG_PM
238 /**
239  * ufshcd_pltfrm_suspend - suspend power management function
240  * @dev: pointer to device handle
241  *
242  * Returns 0 if successful
243  * Returns non-zero otherwise
244  */
245 int ufshcd_pltfrm_suspend(struct device *dev)
246 {
247         return ufshcd_system_suspend(dev_get_drvdata(dev));
248 }
249 EXPORT_SYMBOL_GPL(ufshcd_pltfrm_suspend);
250
251 /**
252  * ufshcd_pltfrm_resume - resume power management function
253  * @dev: pointer to device handle
254  *
255  * Returns 0 if successful
256  * Returns non-zero otherwise
257  */
258 int ufshcd_pltfrm_resume(struct device *dev)
259 {
260         return ufshcd_system_resume(dev_get_drvdata(dev));
261 }
262 EXPORT_SYMBOL_GPL(ufshcd_pltfrm_resume);
263
264 int ufshcd_pltfrm_runtime_suspend(struct device *dev)
265 {
266         return ufshcd_runtime_suspend(dev_get_drvdata(dev));
267 }
268 EXPORT_SYMBOL_GPL(ufshcd_pltfrm_runtime_suspend);
269
270 int ufshcd_pltfrm_runtime_resume(struct device *dev)
271 {
272         return ufshcd_runtime_resume(dev_get_drvdata(dev));
273 }
274 EXPORT_SYMBOL_GPL(ufshcd_pltfrm_runtime_resume);
275
276 int ufshcd_pltfrm_runtime_idle(struct device *dev)
277 {
278         return ufshcd_runtime_idle(dev_get_drvdata(dev));
279 }
280 EXPORT_SYMBOL_GPL(ufshcd_pltfrm_runtime_idle);
281
282 #endif /* CONFIG_PM */
283
284 void ufshcd_pltfrm_shutdown(struct platform_device *pdev)
285 {
286         ufshcd_shutdown((struct ufs_hba *)platform_get_drvdata(pdev));
287 }
288 EXPORT_SYMBOL_GPL(ufshcd_pltfrm_shutdown);
289
290 static void ufshcd_init_lanes_per_dir(struct ufs_hba *hba)
291 {
292         struct device *dev = hba->dev;
293         int ret;
294
295         ret = of_property_read_u32(dev->of_node, "lanes-per-direction",
296                 &hba->lanes_per_direction);
297         if (ret) {
298                 dev_dbg(hba->dev,
299                         "%s: failed to read lanes-per-direction, ret=%d\n",
300                         __func__, ret);
301                 hba->lanes_per_direction = UFSHCD_DEFAULT_LANES_PER_DIRECTION;
302         }
303 }
304
305 /**
306  * ufshcd_pltfrm_init - probe routine of the driver
307  * @pdev: pointer to Platform device handle
308  * @vops: pointer to variant ops
309  *
310  * Returns 0 on success, non-zero value on failure
311  */
312 int ufshcd_pltfrm_init(struct platform_device *pdev,
313                        struct ufs_hba_variant_ops *vops)
314 {
315         struct ufs_hba *hba;
316         void __iomem *mmio_base;
317         struct resource *mem_res;
318         int irq, err;
319         struct device *dev = &pdev->dev;
320
321         mem_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
322         mmio_base = devm_ioremap_resource(dev, mem_res);
323         if (IS_ERR(*(void **)&mmio_base)) {
324                 err = PTR_ERR(*(void **)&mmio_base);
325                 goto out;
326         }
327
328         irq = platform_get_irq(pdev, 0);
329         if (irq < 0) {
330                 dev_err(dev, "IRQ resource not available\n");
331                 err = -ENODEV;
332                 goto out;
333         }
334
335         err = ufshcd_alloc_host(dev, &hba);
336         if (err) {
337                 dev_err(&pdev->dev, "Allocation failed\n");
338                 goto out;
339         }
340
341         hba->vops = vops;
342
343         err = ufshcd_parse_clock_info(hba);
344         if (err) {
345                 dev_err(&pdev->dev, "%s: clock parse failed %d\n",
346                                 __func__, err);
347                 goto dealloc_host;
348         }
349         err = ufshcd_parse_regulator_info(hba);
350         if (err) {
351                 dev_err(&pdev->dev, "%s: regulator init failed %d\n",
352                                 __func__, err);
353                 goto dealloc_host;
354         }
355
356         ufshcd_init_lanes_per_dir(hba);
357
358         err = ufshcd_init(hba, mmio_base, irq);
359         if (err) {
360                 dev_err(dev, "Initialization failed\n");
361                 goto dealloc_host;
362         }
363
364         pm_runtime_set_active(&pdev->dev);
365         pm_runtime_enable(&pdev->dev);
366
367         return 0;
368
369 dealloc_host:
370         ufshcd_dealloc_host(hba);
371 out:
372         return err;
373 }
374 EXPORT_SYMBOL_GPL(ufshcd_pltfrm_init);
375
376 MODULE_AUTHOR("Santosh Yaragnavi <santosh.sy@samsung.com>");
377 MODULE_AUTHOR("Vinayak Holikatti <h.vinayak@samsung.com>");
378 MODULE_DESCRIPTION("UFS host controller Platform bus based glue driver");
379 MODULE_LICENSE("GPL");
380 MODULE_VERSION(UFSHCD_DRIVER_VERSION);