blob: 51e47c429a1f0f1d04d5c2cbaa4c4daf371a799b [file] [log] [blame]
Vinayak Holikatti03b17812013-02-26 18:04:45 +05301/*
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
Vinayak Holikatti03b17812013-02-26 18:04:45 +053036#include <linux/platform_device.h>
Sujit Reddy Thumma62694732013-07-30 00:36:00 +053037#include <linux/pm_runtime.h>
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +030038#include <linux/of.h>
Vinayak Holikatti03b17812013-02-26 18:04:45 +053039
Seungwon Jeon2953f852013-06-27 13:31:54 +090040#include "ufshcd.h"
41
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +030042static const struct of_device_id ufs_of_match[];
43static 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
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +030056#define MAX_PROP_SIZE 32
57static int ufshcd_populate_vreg(struct device *dev, const char *name,
58 struct ufs_vreg **out_vreg)
59{
60 int ret = 0;
61 char prop_name[MAX_PROP_SIZE];
62 struct ufs_vreg *vreg = NULL;
63 struct device_node *np = dev->of_node;
64
65 if (!np) {
66 dev_err(dev, "%s: non DT initialization\n", __func__);
67 goto out;
68 }
69
70 snprintf(prop_name, MAX_PROP_SIZE, "%s-supply", name);
71 if (!of_parse_phandle(np, prop_name, 0)) {
72 dev_info(dev, "%s: Unable to find %s regulator, assuming enabled\n",
73 __func__, prop_name);
74 goto out;
75 }
76
77 vreg = devm_kzalloc(dev, sizeof(*vreg), GFP_KERNEL);
78 if (!vreg) {
79 dev_err(dev, "No memory for %s regulator\n", name);
80 goto out;
81 }
82
83 vreg->name = kstrdup(name, GFP_KERNEL);
84
85 snprintf(prop_name, MAX_PROP_SIZE, "%s-max-microamp", name);
86 ret = of_property_read_u32(np, prop_name, &vreg->max_uA);
87 if (ret) {
88 dev_err(dev, "%s: unable to find %s err %d\n",
89 __func__, prop_name, ret);
90 goto out_free;
91 }
92
93 vreg->min_uA = 0;
94 if (!strcmp(name, "vcc")) {
95 if (of_property_read_bool(np, "vcc-supply-1p8")) {
96 vreg->min_uV = UFS_VREG_VCC_1P8_MIN_UV;
97 vreg->max_uV = UFS_VREG_VCC_1P8_MAX_UV;
98 } else {
99 vreg->min_uV = UFS_VREG_VCC_MIN_UV;
100 vreg->max_uV = UFS_VREG_VCC_MAX_UV;
101 }
102 } else if (!strcmp(name, "vccq")) {
103 vreg->min_uV = UFS_VREG_VCCQ_MIN_UV;
104 vreg->max_uV = UFS_VREG_VCCQ_MAX_UV;
105 } else if (!strcmp(name, "vccq2")) {
106 vreg->min_uV = UFS_VREG_VCCQ2_MIN_UV;
107 vreg->max_uV = UFS_VREG_VCCQ2_MAX_UV;
108 }
109
110 goto out;
111
112out_free:
113 devm_kfree(dev, vreg);
114 vreg = NULL;
115out:
116 if (!ret)
117 *out_vreg = vreg;
118 return ret;
119}
120
121/**
122 * ufshcd_parse_regulator_info - get regulator info from device tree
123 * @hba: per adapter instance
124 *
125 * Get regulator info from device tree for vcc, vccq, vccq2 power supplies.
126 * If any of the supplies are not defined it is assumed that they are always-on
127 * and hence return zero. If the property is defined but parsing is failed
128 * then return corresponding error.
129 */
130static int ufshcd_parse_regulator_info(struct ufs_hba *hba)
131{
132 int err;
133 struct device *dev = hba->dev;
134 struct ufs_vreg_info *info = &hba->vreg_info;
135
136 err = ufshcd_populate_vreg(dev, "vcc", &info->vcc);
137 if (err)
138 goto out;
139
140 err = ufshcd_populate_vreg(dev, "vccq", &info->vccq);
141 if (err)
142 goto out;
143
144 err = ufshcd_populate_vreg(dev, "vccq2", &info->vccq2);
145out:
146 return err;
147}
148
Vinayak Holikatti03b17812013-02-26 18:04:45 +0530149#ifdef CONFIG_PM
150/**
151 * ufshcd_pltfrm_suspend - suspend power management function
152 * @dev: pointer to device handle
153 *
154 *
155 * Returns 0
156 */
157static int ufshcd_pltfrm_suspend(struct device *dev)
158{
159 struct platform_device *pdev = to_platform_device(dev);
160 struct ufs_hba *hba = platform_get_drvdata(pdev);
161
162 /*
163 * TODO:
164 * 1. Call ufshcd_suspend
165 * 2. Do bus specific power management
166 */
167
168 disable_irq(hba->irq);
169
170 return 0;
171}
172
173/**
174 * ufshcd_pltfrm_resume - resume power management function
175 * @dev: pointer to device handle
176 *
177 * Returns 0
178 */
179static int ufshcd_pltfrm_resume(struct device *dev)
180{
181 struct platform_device *pdev = to_platform_device(dev);
182 struct ufs_hba *hba = platform_get_drvdata(pdev);
183
184 /*
185 * TODO:
186 * 1. Call ufshcd_resume.
187 * 2. Do bus specific wake up
188 */
189
190 enable_irq(hba->irq);
191
192 return 0;
193}
194#else
195#define ufshcd_pltfrm_suspend NULL
196#define ufshcd_pltfrm_resume NULL
197#endif
198
Sujit Reddy Thumma62694732013-07-30 00:36:00 +0530199#ifdef CONFIG_PM_RUNTIME
200static int ufshcd_pltfrm_runtime_suspend(struct device *dev)
201{
202 struct ufs_hba *hba = dev_get_drvdata(dev);
203
204 if (!hba)
205 return 0;
206
207 return ufshcd_runtime_suspend(hba);
208}
209static int ufshcd_pltfrm_runtime_resume(struct device *dev)
210{
211 struct ufs_hba *hba = dev_get_drvdata(dev);
212
213 if (!hba)
214 return 0;
215
216 return ufshcd_runtime_resume(hba);
217}
218static int ufshcd_pltfrm_runtime_idle(struct device *dev)
219{
220 struct ufs_hba *hba = dev_get_drvdata(dev);
221
222 if (!hba)
223 return 0;
224
225 return ufshcd_runtime_idle(hba);
226}
227#else /* !CONFIG_PM_RUNTIME */
228#define ufshcd_pltfrm_runtime_suspend NULL
229#define ufshcd_pltfrm_runtime_resume NULL
230#define ufshcd_pltfrm_runtime_idle NULL
231#endif /* CONFIG_PM_RUNTIME */
232
Vinayak Holikatti03b17812013-02-26 18:04:45 +0530233/**
234 * ufshcd_pltfrm_probe - probe routine of the driver
235 * @pdev: pointer to Platform device handle
236 *
237 * Returns 0 on success, non-zero value on failure
238 */
239static int ufshcd_pltfrm_probe(struct platform_device *pdev)
240{
241 struct ufs_hba *hba;
242 void __iomem *mmio_base;
243 struct resource *mem_res;
Seungwon Jeon2953f852013-06-27 13:31:54 +0900244 int irq, err;
Vinayak Holikatti03b17812013-02-26 18:04:45 +0530245 struct device *dev = &pdev->dev;
246
247 mem_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Seungwon Jeon2953f852013-06-27 13:31:54 +0900248 mmio_base = devm_ioremap_resource(dev, mem_res);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +0300249 if (IS_ERR(*(void **)&mmio_base)) {
250 err = PTR_ERR(*(void **)&mmio_base);
Seungwon Jeon2953f852013-06-27 13:31:54 +0900251 goto out;
Vinayak Holikatti03b17812013-02-26 18:04:45 +0530252 }
253
Seungwon Jeon2953f852013-06-27 13:31:54 +0900254 irq = platform_get_irq(pdev, 0);
255 if (irq < 0) {
256 dev_err(dev, "IRQ resource not available\n");
Vinayak Holikatti03b17812013-02-26 18:04:45 +0530257 err = -ENODEV;
Seungwon Jeon2953f852013-06-27 13:31:54 +0900258 goto out;
Vinayak Holikatti03b17812013-02-26 18:04:45 +0530259 }
260
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +0300261 err = ufshcd_alloc_host(dev, &hba);
262 if (err) {
263 dev_err(&pdev->dev, "Allocation failed\n");
264 goto out;
265 }
266
267 hba->vops = get_variant_ops(&pdev->dev);
268
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +0300269 err = ufshcd_parse_regulator_info(hba);
270 if (err) {
271 dev_err(&pdev->dev, "%s: regulator init failed %d\n",
272 __func__, err);
273 goto out;
274 }
275
Sujit Reddy Thumma62694732013-07-30 00:36:00 +0530276 pm_runtime_set_active(&pdev->dev);
277 pm_runtime_enable(&pdev->dev);
278
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +0300279 err = ufshcd_init(hba, mmio_base, irq);
Vinayak Holikatti03b17812013-02-26 18:04:45 +0530280 if (err) {
Seungwon Jeon2953f852013-06-27 13:31:54 +0900281 dev_err(dev, "Intialization failed\n");
Sujit Reddy Thumma62694732013-07-30 00:36:00 +0530282 goto out_disable_rpm;
Vinayak Holikatti03b17812013-02-26 18:04:45 +0530283 }
284
285 platform_set_drvdata(pdev, hba);
286
Sujit Reddy Thumma62694732013-07-30 00:36:00 +0530287 return 0;
288
289out_disable_rpm:
290 pm_runtime_disable(&pdev->dev);
291 pm_runtime_set_suspended(&pdev->dev);
Seungwon Jeon2953f852013-06-27 13:31:54 +0900292out:
Vinayak Holikatti03b17812013-02-26 18:04:45 +0530293 return err;
294}
295
296/**
297 * ufshcd_pltfrm_remove - remove platform driver routine
298 * @pdev: pointer to platform device handle
299 *
300 * Returns 0 on success, non-zero value on failure
301 */
302static int ufshcd_pltfrm_remove(struct platform_device *pdev)
303{
Vinayak Holikatti03b17812013-02-26 18:04:45 +0530304 struct ufs_hba *hba = platform_get_drvdata(pdev);
305
Sujit Reddy Thumma62694732013-07-30 00:36:00 +0530306 pm_runtime_get_sync(&(pdev)->dev);
Vinayak Holikatti03b17812013-02-26 18:04:45 +0530307 ufshcd_remove(hba);
Vinayak Holikatti03b17812013-02-26 18:04:45 +0530308 return 0;
309}
310
311static const struct of_device_id ufs_of_match[] = {
312 { .compatible = "jedec,ufs-1.1"},
Akinobu Mita2e2930a2013-06-26 22:39:32 +0530313 {},
Vinayak Holikatti03b17812013-02-26 18:04:45 +0530314};
315
316static const struct dev_pm_ops ufshcd_dev_pm_ops = {
317 .suspend = ufshcd_pltfrm_suspend,
318 .resume = ufshcd_pltfrm_resume,
Sujit Reddy Thumma62694732013-07-30 00:36:00 +0530319 .runtime_suspend = ufshcd_pltfrm_runtime_suspend,
320 .runtime_resume = ufshcd_pltfrm_runtime_resume,
321 .runtime_idle = ufshcd_pltfrm_runtime_idle,
Vinayak Holikatti03b17812013-02-26 18:04:45 +0530322};
323
324static struct platform_driver ufshcd_pltfrm_driver = {
325 .probe = ufshcd_pltfrm_probe,
326 .remove = ufshcd_pltfrm_remove,
327 .driver = {
328 .name = "ufshcd",
329 .owner = THIS_MODULE,
330 .pm = &ufshcd_dev_pm_ops,
331 .of_match_table = ufs_of_match,
332 },
333};
334
335module_platform_driver(ufshcd_pltfrm_driver);
336
337MODULE_AUTHOR("Santosh Yaragnavi <santosh.sy@samsung.com>");
338MODULE_AUTHOR("Vinayak Holikatti <h.vinayak@samsung.com>");
339MODULE_DESCRIPTION("UFS host controller Pltform bus based glue driver");
340MODULE_LICENSE("GPL");
341MODULE_VERSION(UFSHCD_DRIVER_VERSION);