Merge branch 'for-3.18' of git://git.kernel.org/pub/scm/linux/kernel/git/tj/cgroup
[firefly-linux-kernel-4.4.55.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
42 static const struct of_device_id ufs_of_match[];
43 static struct ufs_hba_variant_ops *get_variant_ops(struct device *dev)
44 {
45         if (dev->of_node) {
46                 const struct of_device_id *match;
47
48                 match = of_match_node(ufs_of_match, dev->of_node);
49                 if (match)
50                         return (struct ufs_hba_variant_ops *)match->data;
51         }
52
53         return NULL;
54 }
55
56 static int ufshcd_parse_clock_info(struct ufs_hba *hba)
57 {
58         int ret = 0;
59         int cnt;
60         int i;
61         struct device *dev = hba->dev;
62         struct device_node *np = dev->of_node;
63         char *name;
64         u32 *clkfreq = NULL;
65         struct ufs_clk_info *clki;
66         int len = 0;
67         size_t sz = 0;
68
69         if (!np)
70                 goto out;
71
72         INIT_LIST_HEAD(&hba->clk_list_head);
73
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",
77                                 __func__);
78         } else if (cnt < 0) {
79                 dev_err(dev, "%s: count clock strings failed, err %d\n",
80                                 __func__, cnt);
81                 ret = cnt;
82         }
83
84         if (cnt <= 0)
85                 goto out;
86
87         if (!of_get_property(np, "freq-table-hz", &len)) {
88                 dev_info(dev, "freq-table-hz property not specified\n");
89                 goto out;
90         }
91
92         if (len <= 0)
93                 goto out;
94
95         sz = len / sizeof(*clkfreq);
96         if (sz != 2 * cnt) {
97                 dev_err(dev, "%s len mismatch\n", "freq-table-hz");
98                 ret = -EINVAL;
99                 goto out;
100         }
101
102         clkfreq = devm_kzalloc(dev, sz * sizeof(*clkfreq),
103                         GFP_KERNEL);
104         if (!clkfreq) {
105                 dev_err(dev, "%s: no memory\n", "freq-table-hz");
106                 ret = -ENOMEM;
107                 goto out;
108         }
109
110         ret = of_property_read_u32_array(np, "freq-table-hz",
111                         clkfreq, sz);
112         if (ret && (ret != -EINVAL)) {
113                 dev_err(dev, "%s: error reading array %d\n",
114                                 "freq-table-hz", ret);
115                 goto free_clkfreq;
116         }
117
118         for (i = 0; i < sz; i += 2) {
119                 ret = of_property_read_string_index(np,
120                                 "clock-names", i/2, (const char **)&name);
121                 if (ret)
122                         goto free_clkfreq;
123
124                 clki = devm_kzalloc(dev, sizeof(*clki), GFP_KERNEL);
125                 if (!clki) {
126                         ret = -ENOMEM;
127                         goto free_clkfreq;
128                 }
129
130                 clki->min_freq = clkfreq[i];
131                 clki->max_freq = clkfreq[i+1];
132                 clki->name = kstrdup(name, GFP_KERNEL);
133                 dev_dbg(dev, "%s: min %u max %u name %s\n", "freq-table-hz",
134                                 clki->min_freq, clki->max_freq, clki->name);
135                 list_add_tail(&clki->list, &hba->clk_list_head);
136         }
137 free_clkfreq:
138         kfree(clkfreq);
139 out:
140         return ret;
141 }
142
143 #define MAX_PROP_SIZE 32
144 static int ufshcd_populate_vreg(struct device *dev, const char *name,
145                 struct ufs_vreg **out_vreg)
146 {
147         int ret = 0;
148         char prop_name[MAX_PROP_SIZE];
149         struct ufs_vreg *vreg = NULL;
150         struct device_node *np = dev->of_node;
151
152         if (!np) {
153                 dev_err(dev, "%s: non DT initialization\n", __func__);
154                 goto out;
155         }
156
157         snprintf(prop_name, MAX_PROP_SIZE, "%s-supply", name);
158         if (!of_parse_phandle(np, prop_name, 0)) {
159                 dev_info(dev, "%s: Unable to find %s regulator, assuming enabled\n",
160                                 __func__, prop_name);
161                 goto out;
162         }
163
164         vreg = devm_kzalloc(dev, sizeof(*vreg), GFP_KERNEL);
165         if (!vreg) {
166                 dev_err(dev, "No memory for %s regulator\n", name);
167                 goto out;
168         }
169
170         vreg->name = kstrdup(name, GFP_KERNEL);
171
172         /* if fixed regulator no need further initialization */
173         snprintf(prop_name, MAX_PROP_SIZE, "%s-fixed-regulator", name);
174         if (of_property_read_bool(np, prop_name))
175                 goto out;
176
177         snprintf(prop_name, MAX_PROP_SIZE, "%s-max-microamp", name);
178         ret = of_property_read_u32(np, prop_name, &vreg->max_uA);
179         if (ret) {
180                 dev_err(dev, "%s: unable to find %s err %d\n",
181                                 __func__, prop_name, ret);
182                 goto out_free;
183         }
184
185         vreg->min_uA = 0;
186         if (!strcmp(name, "vcc")) {
187                 if (of_property_read_bool(np, "vcc-supply-1p8")) {
188                         vreg->min_uV = UFS_VREG_VCC_1P8_MIN_UV;
189                         vreg->max_uV = UFS_VREG_VCC_1P8_MAX_UV;
190                 } else {
191                         vreg->min_uV = UFS_VREG_VCC_MIN_UV;
192                         vreg->max_uV = UFS_VREG_VCC_MAX_UV;
193                 }
194         } else if (!strcmp(name, "vccq")) {
195                 vreg->min_uV = UFS_VREG_VCCQ_MIN_UV;
196                 vreg->max_uV = UFS_VREG_VCCQ_MAX_UV;
197         } else if (!strcmp(name, "vccq2")) {
198                 vreg->min_uV = UFS_VREG_VCCQ2_MIN_UV;
199                 vreg->max_uV = UFS_VREG_VCCQ2_MAX_UV;
200         }
201
202         goto out;
203
204 out_free:
205         devm_kfree(dev, vreg);
206         vreg = NULL;
207 out:
208         if (!ret)
209                 *out_vreg = vreg;
210         return ret;
211 }
212
213 /**
214  * ufshcd_parse_regulator_info - get regulator info from device tree
215  * @hba: per adapter instance
216  *
217  * Get regulator info from device tree for vcc, vccq, vccq2 power supplies.
218  * If any of the supplies are not defined it is assumed that they are always-on
219  * and hence return zero. If the property is defined but parsing is failed
220  * then return corresponding error.
221  */
222 static int ufshcd_parse_regulator_info(struct ufs_hba *hba)
223 {
224         int err;
225         struct device *dev = hba->dev;
226         struct ufs_vreg_info *info = &hba->vreg_info;
227
228         err = ufshcd_populate_vreg(dev, "vdd-hba", &info->vdd_hba);
229         if (err)
230                 goto out;
231
232         err = ufshcd_populate_vreg(dev, "vcc", &info->vcc);
233         if (err)
234                 goto out;
235
236         err = ufshcd_populate_vreg(dev, "vccq", &info->vccq);
237         if (err)
238                 goto out;
239
240         err = ufshcd_populate_vreg(dev, "vccq2", &info->vccq2);
241 out:
242         return err;
243 }
244
245 #ifdef CONFIG_PM
246 /**
247  * ufshcd_pltfrm_suspend - suspend power management function
248  * @dev: pointer to device handle
249  *
250  * Returns 0 if successful
251  * Returns non-zero otherwise
252  */
253 static int ufshcd_pltfrm_suspend(struct device *dev)
254 {
255         return ufshcd_system_suspend(dev_get_drvdata(dev));
256 }
257
258 /**
259  * ufshcd_pltfrm_resume - resume power management function
260  * @dev: pointer to device handle
261  *
262  * Returns 0 if successful
263  * Returns non-zero otherwise
264  */
265 static int ufshcd_pltfrm_resume(struct device *dev)
266 {
267         return ufshcd_system_resume(dev_get_drvdata(dev));
268 }
269 #else
270 #define ufshcd_pltfrm_suspend   NULL
271 #define ufshcd_pltfrm_resume    NULL
272 #endif
273
274 #ifdef CONFIG_PM_RUNTIME
275 static int ufshcd_pltfrm_runtime_suspend(struct device *dev)
276 {
277         return ufshcd_runtime_suspend(dev_get_drvdata(dev));
278 }
279 static int ufshcd_pltfrm_runtime_resume(struct device *dev)
280 {
281         return ufshcd_runtime_resume(dev_get_drvdata(dev));
282 }
283 static int ufshcd_pltfrm_runtime_idle(struct device *dev)
284 {
285         return ufshcd_runtime_idle(dev_get_drvdata(dev));
286 }
287 #else /* !CONFIG_PM_RUNTIME */
288 #define ufshcd_pltfrm_runtime_suspend   NULL
289 #define ufshcd_pltfrm_runtime_resume    NULL
290 #define ufshcd_pltfrm_runtime_idle      NULL
291 #endif /* CONFIG_PM_RUNTIME */
292
293 static void ufshcd_pltfrm_shutdown(struct platform_device *pdev)
294 {
295         ufshcd_shutdown((struct ufs_hba *)platform_get_drvdata(pdev));
296 }
297
298 /**
299  * ufshcd_pltfrm_probe - probe routine of the driver
300  * @pdev: pointer to Platform device handle
301  *
302  * Returns 0 on success, non-zero value on failure
303  */
304 static int ufshcd_pltfrm_probe(struct platform_device *pdev)
305 {
306         struct ufs_hba *hba;
307         void __iomem *mmio_base;
308         struct resource *mem_res;
309         int irq, err;
310         struct device *dev = &pdev->dev;
311
312         mem_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
313         mmio_base = devm_ioremap_resource(dev, mem_res);
314         if (IS_ERR(*(void **)&mmio_base)) {
315                 err = PTR_ERR(*(void **)&mmio_base);
316                 goto out;
317         }
318
319         irq = platform_get_irq(pdev, 0);
320         if (irq < 0) {
321                 dev_err(dev, "IRQ resource not available\n");
322                 err = -ENODEV;
323                 goto out;
324         }
325
326         err = ufshcd_alloc_host(dev, &hba);
327         if (err) {
328                 dev_err(&pdev->dev, "Allocation failed\n");
329                 goto out;
330         }
331
332         hba->vops = get_variant_ops(&pdev->dev);
333
334         err = ufshcd_parse_clock_info(hba);
335         if (err) {
336                 dev_err(&pdev->dev, "%s: clock parse failed %d\n",
337                                 __func__, err);
338                 goto out;
339         }
340         err = ufshcd_parse_regulator_info(hba);
341         if (err) {
342                 dev_err(&pdev->dev, "%s: regulator init failed %d\n",
343                                 __func__, err);
344                 goto out;
345         }
346
347         pm_runtime_set_active(&pdev->dev);
348         pm_runtime_enable(&pdev->dev);
349
350         err = ufshcd_init(hba, mmio_base, irq);
351         if (err) {
352                 dev_err(dev, "Intialization failed\n");
353                 goto out_disable_rpm;
354         }
355
356         platform_set_drvdata(pdev, hba);
357
358         return 0;
359
360 out_disable_rpm:
361         pm_runtime_disable(&pdev->dev);
362         pm_runtime_set_suspended(&pdev->dev);
363 out:
364         return err;
365 }
366
367 /**
368  * ufshcd_pltfrm_remove - remove platform driver routine
369  * @pdev: pointer to platform device handle
370  *
371  * Returns 0 on success, non-zero value on failure
372  */
373 static int ufshcd_pltfrm_remove(struct platform_device *pdev)
374 {
375         struct ufs_hba *hba =  platform_get_drvdata(pdev);
376
377         pm_runtime_get_sync(&(pdev)->dev);
378         ufshcd_remove(hba);
379         return 0;
380 }
381
382 static const struct of_device_id ufs_of_match[] = {
383         { .compatible = "jedec,ufs-1.1"},
384         {},
385 };
386
387 static const struct dev_pm_ops ufshcd_dev_pm_ops = {
388         .suspend        = ufshcd_pltfrm_suspend,
389         .resume         = ufshcd_pltfrm_resume,
390         .runtime_suspend = ufshcd_pltfrm_runtime_suspend,
391         .runtime_resume  = ufshcd_pltfrm_runtime_resume,
392         .runtime_idle    = ufshcd_pltfrm_runtime_idle,
393 };
394
395 static struct platform_driver ufshcd_pltfrm_driver = {
396         .probe  = ufshcd_pltfrm_probe,
397         .remove = ufshcd_pltfrm_remove,
398         .shutdown = ufshcd_pltfrm_shutdown,
399         .driver = {
400                 .name   = "ufshcd",
401                 .owner  = THIS_MODULE,
402                 .pm     = &ufshcd_dev_pm_ops,
403                 .of_match_table = ufs_of_match,
404         },
405 };
406
407 module_platform_driver(ufshcd_pltfrm_driver);
408
409 MODULE_AUTHOR("Santosh Yaragnavi <santosh.sy@samsung.com>");
410 MODULE_AUTHOR("Vinayak Holikatti <h.vinayak@samsung.com>");
411 MODULE_DESCRIPTION("UFS host controller Pltform bus based glue driver");
412 MODULE_LICENSE("GPL");
413 MODULE_VERSION(UFSHCD_DRIVER_VERSION);