2 * Universal Flash Storage Host controller Platform bus based glue driver
4 * This code is based on drivers/scsi/ufs/ufshcd-pltfrm.c
5 * Copyright (C) 2011-2013 Samsung India Software Operations
8 * Santosh Yaraganavi <santosh.sy@samsung.com>
9 * Vinayak Holikatti <h.vinayak@samsung.com>
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>
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.
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
36 #include <linux/platform_device.h>
37 #include <linux/pm_runtime.h>
41 #include "ufshcd-pltfrm.h"
43 #define UFSHCD_DEFAULT_LANES_PER_DIRECTION 2
45 static int ufshcd_parse_clock_info(struct ufs_hba
*hba
)
50 struct device
*dev
= hba
->dev
;
51 struct device_node
*np
= dev
->of_node
;
54 struct ufs_clk_info
*clki
;
61 INIT_LIST_HEAD(&hba
->clk_list_head
);
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",
68 dev_err(dev
, "%s: count clock strings failed, err %d\n",
76 if (!of_get_property(np
, "freq-table-hz", &len
)) {
77 dev_info(dev
, "freq-table-hz property not specified\n");
84 sz
= len
/ sizeof(*clkfreq
);
86 dev_err(dev
, "%s len mismatch\n", "freq-table-hz");
91 clkfreq
= devm_kzalloc(dev
, sz
* sizeof(*clkfreq
),
98 ret
= of_property_read_u32_array(np
, "freq-table-hz",
100 if (ret
&& (ret
!= -EINVAL
)) {
101 dev_err(dev
, "%s: error reading array %d\n",
102 "freq-table-hz", ret
);
106 for (i
= 0; i
< sz
; i
+= 2) {
107 ret
= of_property_read_string_index(np
,
108 "clock-names", i
/2, (const char **)&name
);
112 clki
= devm_kzalloc(dev
, sizeof(*clki
), GFP_KERNEL
);
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
);
129 #define MAX_PROP_SIZE 32
130 static int ufshcd_populate_vreg(struct device
*dev
, const char *name
,
131 struct ufs_vreg
**out_vreg
)
134 char prop_name
[MAX_PROP_SIZE
];
135 struct ufs_vreg
*vreg
= NULL
;
136 struct device_node
*np
= dev
->of_node
;
139 dev_err(dev
, "%s: non DT initialization\n", __func__
);
143 snprintf(prop_name
, MAX_PROP_SIZE
, "%s-supply", name
);
144 if (!of_parse_phandle(np
, prop_name
, 0)) {
145 dev_info(dev
, "%s: Unable to find %s regulator, assuming enabled\n",
146 __func__
, prop_name
);
150 vreg
= devm_kzalloc(dev
, sizeof(*vreg
), GFP_KERNEL
);
154 vreg
->name
= kstrdup(name
, GFP_KERNEL
);
156 /* if fixed regulator no need further initialization */
157 snprintf(prop_name
, MAX_PROP_SIZE
, "%s-fixed-regulator", name
);
158 if (of_property_read_bool(np
, prop_name
))
161 snprintf(prop_name
, MAX_PROP_SIZE
, "%s-max-microamp", name
);
162 ret
= of_property_read_u32(np
, prop_name
, &vreg
->max_uA
);
164 dev_err(dev
, "%s: unable to find %s err %d\n",
165 __func__
, prop_name
, ret
);
170 if (!strcmp(name
, "vcc")) {
171 if (of_property_read_bool(np
, "vcc-supply-1p8")) {
172 vreg
->min_uV
= UFS_VREG_VCC_1P8_MIN_UV
;
173 vreg
->max_uV
= UFS_VREG_VCC_1P8_MAX_UV
;
175 vreg
->min_uV
= UFS_VREG_VCC_MIN_UV
;
176 vreg
->max_uV
= UFS_VREG_VCC_MAX_UV
;
178 } else if (!strcmp(name
, "vccq")) {
179 vreg
->min_uV
= UFS_VREG_VCCQ_MIN_UV
;
180 vreg
->max_uV
= UFS_VREG_VCCQ_MAX_UV
;
181 } else if (!strcmp(name
, "vccq2")) {
182 vreg
->min_uV
= UFS_VREG_VCCQ2_MIN_UV
;
183 vreg
->max_uV
= UFS_VREG_VCCQ2_MAX_UV
;
189 devm_kfree(dev
, vreg
);
198 * ufshcd_parse_regulator_info - get regulator info from device tree
199 * @hba: per adapter instance
201 * Get regulator info from device tree for vcc, vccq, vccq2 power supplies.
202 * If any of the supplies are not defined it is assumed that they are always-on
203 * and hence return zero. If the property is defined but parsing is failed
204 * then return corresponding error.
206 static int ufshcd_parse_regulator_info(struct ufs_hba
*hba
)
209 struct device
*dev
= hba
->dev
;
210 struct ufs_vreg_info
*info
= &hba
->vreg_info
;
212 err
= ufshcd_populate_vreg(dev
, "vdd-hba", &info
->vdd_hba
);
216 err
= ufshcd_populate_vreg(dev
, "vcc", &info
->vcc
);
220 err
= ufshcd_populate_vreg(dev
, "vccq", &info
->vccq
);
224 err
= ufshcd_populate_vreg(dev
, "vccq2", &info
->vccq2
);
231 * ufshcd_pltfrm_suspend - suspend power management function
232 * @dev: pointer to device handle
234 * Returns 0 if successful
235 * Returns non-zero otherwise
237 int ufshcd_pltfrm_suspend(struct device
*dev
)
239 return ufshcd_system_suspend(dev_get_drvdata(dev
));
241 EXPORT_SYMBOL_GPL(ufshcd_pltfrm_suspend
);
244 * ufshcd_pltfrm_resume - resume power management function
245 * @dev: pointer to device handle
247 * Returns 0 if successful
248 * Returns non-zero otherwise
250 int ufshcd_pltfrm_resume(struct device
*dev
)
252 return ufshcd_system_resume(dev_get_drvdata(dev
));
254 EXPORT_SYMBOL_GPL(ufshcd_pltfrm_resume
);
256 int ufshcd_pltfrm_runtime_suspend(struct device
*dev
)
258 return ufshcd_runtime_suspend(dev_get_drvdata(dev
));
260 EXPORT_SYMBOL_GPL(ufshcd_pltfrm_runtime_suspend
);
262 int ufshcd_pltfrm_runtime_resume(struct device
*dev
)
264 return ufshcd_runtime_resume(dev_get_drvdata(dev
));
266 EXPORT_SYMBOL_GPL(ufshcd_pltfrm_runtime_resume
);
268 int ufshcd_pltfrm_runtime_idle(struct device
*dev
)
270 return ufshcd_runtime_idle(dev_get_drvdata(dev
));
272 EXPORT_SYMBOL_GPL(ufshcd_pltfrm_runtime_idle
);
274 #endif /* CONFIG_PM */
276 void ufshcd_pltfrm_shutdown(struct platform_device
*pdev
)
278 ufshcd_shutdown((struct ufs_hba
*)platform_get_drvdata(pdev
));
280 EXPORT_SYMBOL_GPL(ufshcd_pltfrm_shutdown
);
282 static void ufshcd_init_lanes_per_dir(struct ufs_hba
*hba
)
284 struct device
*dev
= hba
->dev
;
287 ret
= of_property_read_u32(dev
->of_node
, "lanes-per-direction",
288 &hba
->lanes_per_direction
);
291 "%s: failed to read lanes-per-direction, ret=%d\n",
293 hba
->lanes_per_direction
= UFSHCD_DEFAULT_LANES_PER_DIRECTION
;
298 * ufshcd_pltfrm_init - probe routine of the driver
299 * @pdev: pointer to Platform device handle
300 * @vops: pointer to variant ops
302 * Returns 0 on success, non-zero value on failure
304 int ufshcd_pltfrm_init(struct platform_device
*pdev
,
305 struct ufs_hba_variant_ops
*vops
)
308 void __iomem
*mmio_base
;
309 struct resource
*mem_res
;
311 struct device
*dev
= &pdev
->dev
;
313 mem_res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
314 mmio_base
= devm_ioremap_resource(dev
, mem_res
);
315 if (IS_ERR(*(void **)&mmio_base
)) {
316 err
= PTR_ERR(*(void **)&mmio_base
);
320 irq
= platform_get_irq(pdev
, 0);
322 dev_err(dev
, "IRQ resource not available\n");
327 err
= ufshcd_alloc_host(dev
, &hba
);
329 dev_err(&pdev
->dev
, "Allocation failed\n");
335 err
= ufshcd_parse_clock_info(hba
);
337 dev_err(&pdev
->dev
, "%s: clock parse failed %d\n",
341 err
= ufshcd_parse_regulator_info(hba
);
343 dev_err(&pdev
->dev
, "%s: regulator init failed %d\n",
348 pm_runtime_set_active(&pdev
->dev
);
349 pm_runtime_enable(&pdev
->dev
);
351 ufshcd_init_lanes_per_dir(hba
);
353 err
= ufshcd_init(hba
, mmio_base
, irq
);
355 dev_err(dev
, "Initialization failed\n");
356 goto out_disable_rpm
;
359 platform_set_drvdata(pdev
, hba
);
364 pm_runtime_disable(&pdev
->dev
);
365 pm_runtime_set_suspended(&pdev
->dev
);
367 ufshcd_dealloc_host(hba
);
371 EXPORT_SYMBOL_GPL(ufshcd_pltfrm_init
);
373 MODULE_AUTHOR("Santosh Yaragnavi <santosh.sy@samsung.com>");
374 MODULE_AUTHOR("Vinayak Holikatti <h.vinayak@samsung.com>");
375 MODULE_DESCRIPTION("UFS host controller Pltform bus based glue driver");
376 MODULE_LICENSE("GPL");
377 MODULE_VERSION(UFSHCD_DRIVER_VERSION
);