blob: dde4e6e3be703b22696172a3a50c400cc09ee25c [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 Thummac6e79da2014-09-25 15:32:23 +030056static 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
67 if (!np)
68 goto out;
69
70 INIT_LIST_HEAD(&hba->clk_list_head);
71
72 cnt = of_property_count_strings(np, "clock-names");
73 if (!cnt || (cnt == -EINVAL)) {
74 dev_info(dev, "%s: Unable to find clocks, assuming enabled\n",
75 __func__);
76 } else if (cnt < 0) {
77 dev_err(dev, "%s: count clock strings failed, err %d\n",
78 __func__, cnt);
79 ret = cnt;
80 }
81
82 if (cnt <= 0)
83 goto out;
84
85 clkfreq = kzalloc(cnt * sizeof(*clkfreq), GFP_KERNEL);
86 if (!clkfreq) {
87 ret = -ENOMEM;
88 dev_err(dev, "%s: memory alloc failed\n", __func__);
89 goto out;
90 }
91
92 ret = of_property_read_u32_array(np,
93 "max-clock-frequency-hz", clkfreq, cnt);
94 if (ret && (ret != -EINVAL)) {
95 dev_err(dev, "%s: invalid max-clock-frequency-hz property, %d\n",
96 __func__, ret);
97 goto out;
98 }
99
100 for (i = 0; i < cnt; i++) {
101 ret = of_property_read_string_index(np,
102 "clock-names", i, (const char **)&name);
103 if (ret)
104 goto out;
105
106 clki = devm_kzalloc(dev, sizeof(*clki), GFP_KERNEL);
107 if (!clki) {
108 ret = -ENOMEM;
109 goto out;
110 }
111
112 clki->max_freq = clkfreq[i];
113 clki->name = kstrdup(name, GFP_KERNEL);
114 list_add_tail(&clki->list, &hba->clk_list_head);
115 }
116out:
117 kfree(clkfreq);
118 return ret;
119}
120
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +0300121#define MAX_PROP_SIZE 32
122static int ufshcd_populate_vreg(struct device *dev, const char *name,
123 struct ufs_vreg **out_vreg)
124{
125 int ret = 0;
126 char prop_name[MAX_PROP_SIZE];
127 struct ufs_vreg *vreg = NULL;
128 struct device_node *np = dev->of_node;
129
130 if (!np) {
131 dev_err(dev, "%s: non DT initialization\n", __func__);
132 goto out;
133 }
134
135 snprintf(prop_name, MAX_PROP_SIZE, "%s-supply", name);
136 if (!of_parse_phandle(np, prop_name, 0)) {
137 dev_info(dev, "%s: Unable to find %s regulator, assuming enabled\n",
138 __func__, prop_name);
139 goto out;
140 }
141
142 vreg = devm_kzalloc(dev, sizeof(*vreg), GFP_KERNEL);
143 if (!vreg) {
144 dev_err(dev, "No memory for %s regulator\n", name);
145 goto out;
146 }
147
148 vreg->name = kstrdup(name, GFP_KERNEL);
149
Raviv Shvili6a771a62014-09-25 15:32:24 +0300150 /* if fixed regulator no need further initialization */
151 snprintf(prop_name, MAX_PROP_SIZE, "%s-fixed-regulator", name);
152 if (of_property_read_bool(np, prop_name))
153 goto out;
154
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +0300155 snprintf(prop_name, MAX_PROP_SIZE, "%s-max-microamp", name);
156 ret = of_property_read_u32(np, prop_name, &vreg->max_uA);
157 if (ret) {
158 dev_err(dev, "%s: unable to find %s err %d\n",
159 __func__, prop_name, ret);
160 goto out_free;
161 }
162
163 vreg->min_uA = 0;
164 if (!strcmp(name, "vcc")) {
165 if (of_property_read_bool(np, "vcc-supply-1p8")) {
166 vreg->min_uV = UFS_VREG_VCC_1P8_MIN_UV;
167 vreg->max_uV = UFS_VREG_VCC_1P8_MAX_UV;
168 } else {
169 vreg->min_uV = UFS_VREG_VCC_MIN_UV;
170 vreg->max_uV = UFS_VREG_VCC_MAX_UV;
171 }
172 } else if (!strcmp(name, "vccq")) {
173 vreg->min_uV = UFS_VREG_VCCQ_MIN_UV;
174 vreg->max_uV = UFS_VREG_VCCQ_MAX_UV;
175 } else if (!strcmp(name, "vccq2")) {
176 vreg->min_uV = UFS_VREG_VCCQ2_MIN_UV;
177 vreg->max_uV = UFS_VREG_VCCQ2_MAX_UV;
178 }
179
180 goto out;
181
182out_free:
183 devm_kfree(dev, vreg);
184 vreg = NULL;
185out:
186 if (!ret)
187 *out_vreg = vreg;
188 return ret;
189}
190
191/**
192 * ufshcd_parse_regulator_info - get regulator info from device tree
193 * @hba: per adapter instance
194 *
195 * Get regulator info from device tree for vcc, vccq, vccq2 power supplies.
196 * If any of the supplies are not defined it is assumed that they are always-on
197 * and hence return zero. If the property is defined but parsing is failed
198 * then return corresponding error.
199 */
200static int ufshcd_parse_regulator_info(struct ufs_hba *hba)
201{
202 int err;
203 struct device *dev = hba->dev;
204 struct ufs_vreg_info *info = &hba->vreg_info;
205
Raviv Shvili6a771a62014-09-25 15:32:24 +0300206 err = ufshcd_populate_vreg(dev, "vdd-hba", &info->vdd_hba);
207 if (err)
208 goto out;
209
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +0300210 err = ufshcd_populate_vreg(dev, "vcc", &info->vcc);
211 if (err)
212 goto out;
213
214 err = ufshcd_populate_vreg(dev, "vccq", &info->vccq);
215 if (err)
216 goto out;
217
218 err = ufshcd_populate_vreg(dev, "vccq2", &info->vccq2);
219out:
220 return err;
221}
222
Vinayak Holikatti03b17812013-02-26 18:04:45 +0530223#ifdef CONFIG_PM
224/**
225 * ufshcd_pltfrm_suspend - suspend power management function
226 * @dev: pointer to device handle
227 *
228 *
229 * Returns 0
230 */
231static int ufshcd_pltfrm_suspend(struct device *dev)
232{
233 struct platform_device *pdev = to_platform_device(dev);
234 struct ufs_hba *hba = platform_get_drvdata(pdev);
235
236 /*
237 * TODO:
238 * 1. Call ufshcd_suspend
239 * 2. Do bus specific power management
240 */
241
242 disable_irq(hba->irq);
243
244 return 0;
245}
246
247/**
248 * ufshcd_pltfrm_resume - resume power management function
249 * @dev: pointer to device handle
250 *
251 * Returns 0
252 */
253static int ufshcd_pltfrm_resume(struct device *dev)
254{
255 struct platform_device *pdev = to_platform_device(dev);
256 struct ufs_hba *hba = platform_get_drvdata(pdev);
257
258 /*
259 * TODO:
260 * 1. Call ufshcd_resume.
261 * 2. Do bus specific wake up
262 */
263
264 enable_irq(hba->irq);
265
266 return 0;
267}
268#else
269#define ufshcd_pltfrm_suspend NULL
270#define ufshcd_pltfrm_resume NULL
271#endif
272
Sujit Reddy Thumma62694732013-07-30 00:36:00 +0530273#ifdef CONFIG_PM_RUNTIME
274static int ufshcd_pltfrm_runtime_suspend(struct device *dev)
275{
276 struct ufs_hba *hba = dev_get_drvdata(dev);
277
278 if (!hba)
279 return 0;
280
281 return ufshcd_runtime_suspend(hba);
282}
283static int ufshcd_pltfrm_runtime_resume(struct device *dev)
284{
285 struct ufs_hba *hba = dev_get_drvdata(dev);
286
287 if (!hba)
288 return 0;
289
290 return ufshcd_runtime_resume(hba);
291}
292static int ufshcd_pltfrm_runtime_idle(struct device *dev)
293{
294 struct ufs_hba *hba = dev_get_drvdata(dev);
295
296 if (!hba)
297 return 0;
298
299 return ufshcd_runtime_idle(hba);
300}
301#else /* !CONFIG_PM_RUNTIME */
302#define ufshcd_pltfrm_runtime_suspend NULL
303#define ufshcd_pltfrm_runtime_resume NULL
304#define ufshcd_pltfrm_runtime_idle NULL
305#endif /* CONFIG_PM_RUNTIME */
306
Vinayak Holikatti03b17812013-02-26 18:04:45 +0530307/**
308 * ufshcd_pltfrm_probe - probe routine of the driver
309 * @pdev: pointer to Platform device handle
310 *
311 * Returns 0 on success, non-zero value on failure
312 */
313static int ufshcd_pltfrm_probe(struct platform_device *pdev)
314{
315 struct ufs_hba *hba;
316 void __iomem *mmio_base;
317 struct resource *mem_res;
Seungwon Jeon2953f852013-06-27 13:31:54 +0900318 int irq, err;
Vinayak Holikatti03b17812013-02-26 18:04:45 +0530319 struct device *dev = &pdev->dev;
320
321 mem_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Seungwon Jeon2953f852013-06-27 13:31:54 +0900322 mmio_base = devm_ioremap_resource(dev, mem_res);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +0300323 if (IS_ERR(*(void **)&mmio_base)) {
324 err = PTR_ERR(*(void **)&mmio_base);
Seungwon Jeon2953f852013-06-27 13:31:54 +0900325 goto out;
Vinayak Holikatti03b17812013-02-26 18:04:45 +0530326 }
327
Seungwon Jeon2953f852013-06-27 13:31:54 +0900328 irq = platform_get_irq(pdev, 0);
329 if (irq < 0) {
330 dev_err(dev, "IRQ resource not available\n");
Vinayak Holikatti03b17812013-02-26 18:04:45 +0530331 err = -ENODEV;
Seungwon Jeon2953f852013-06-27 13:31:54 +0900332 goto out;
Vinayak Holikatti03b17812013-02-26 18:04:45 +0530333 }
334
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +0300335 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 = get_variant_ops(&pdev->dev);
342
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +0300343 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 out;
348 }
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +0300349 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 out;
354 }
355
Sujit Reddy Thumma62694732013-07-30 00:36:00 +0530356 pm_runtime_set_active(&pdev->dev);
357 pm_runtime_enable(&pdev->dev);
358
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +0300359 err = ufshcd_init(hba, mmio_base, irq);
Vinayak Holikatti03b17812013-02-26 18:04:45 +0530360 if (err) {
Seungwon Jeon2953f852013-06-27 13:31:54 +0900361 dev_err(dev, "Intialization failed\n");
Sujit Reddy Thumma62694732013-07-30 00:36:00 +0530362 goto out_disable_rpm;
Vinayak Holikatti03b17812013-02-26 18:04:45 +0530363 }
364
365 platform_set_drvdata(pdev, hba);
366
Sujit Reddy Thumma62694732013-07-30 00:36:00 +0530367 return 0;
368
369out_disable_rpm:
370 pm_runtime_disable(&pdev->dev);
371 pm_runtime_set_suspended(&pdev->dev);
Seungwon Jeon2953f852013-06-27 13:31:54 +0900372out:
Vinayak Holikatti03b17812013-02-26 18:04:45 +0530373 return err;
374}
375
376/**
377 * ufshcd_pltfrm_remove - remove platform driver routine
378 * @pdev: pointer to platform device handle
379 *
380 * Returns 0 on success, non-zero value on failure
381 */
382static int ufshcd_pltfrm_remove(struct platform_device *pdev)
383{
Vinayak Holikatti03b17812013-02-26 18:04:45 +0530384 struct ufs_hba *hba = platform_get_drvdata(pdev);
385
Sujit Reddy Thumma62694732013-07-30 00:36:00 +0530386 pm_runtime_get_sync(&(pdev)->dev);
Vinayak Holikatti03b17812013-02-26 18:04:45 +0530387 ufshcd_remove(hba);
Vinayak Holikatti03b17812013-02-26 18:04:45 +0530388 return 0;
389}
390
391static const struct of_device_id ufs_of_match[] = {
392 { .compatible = "jedec,ufs-1.1"},
Akinobu Mita2e2930a2013-06-26 22:39:32 +0530393 {},
Vinayak Holikatti03b17812013-02-26 18:04:45 +0530394};
395
396static const struct dev_pm_ops ufshcd_dev_pm_ops = {
397 .suspend = ufshcd_pltfrm_suspend,
398 .resume = ufshcd_pltfrm_resume,
Sujit Reddy Thumma62694732013-07-30 00:36:00 +0530399 .runtime_suspend = ufshcd_pltfrm_runtime_suspend,
400 .runtime_resume = ufshcd_pltfrm_runtime_resume,
401 .runtime_idle = ufshcd_pltfrm_runtime_idle,
Vinayak Holikatti03b17812013-02-26 18:04:45 +0530402};
403
404static struct platform_driver ufshcd_pltfrm_driver = {
405 .probe = ufshcd_pltfrm_probe,
406 .remove = ufshcd_pltfrm_remove,
407 .driver = {
408 .name = "ufshcd",
409 .owner = THIS_MODULE,
410 .pm = &ufshcd_dev_pm_ops,
411 .of_match_table = ufs_of_match,
412 },
413};
414
415module_platform_driver(ufshcd_pltfrm_driver);
416
417MODULE_AUTHOR("Santosh Yaragnavi <santosh.sy@samsung.com>");
418MODULE_AUTHOR("Vinayak Holikatti <h.vinayak@samsung.com>");
419MODULE_DESCRIPTION("UFS host controller Pltform bus based glue driver");
420MODULE_LICENSE("GPL");
421MODULE_VERSION(UFSHCD_DRIVER_VERSION);