Vinayak Holikatti | 03b1781 | 2013-02-26 18:04:45 +0530 | [diff] [blame] | 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 | |
Vinayak Holikatti | 03b1781 | 2013-02-26 18:04:45 +0530 | [diff] [blame] | 36 | #include <linux/platform_device.h> |
Sujit Reddy Thumma | 6269473 | 2013-07-30 00:36:00 +0530 | [diff] [blame] | 37 | #include <linux/pm_runtime.h> |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 38 | #include <linux/of.h> |
Vinayak Holikatti | 03b1781 | 2013-02-26 18:04:45 +0530 | [diff] [blame] | 39 | |
Seungwon Jeon | 2953f85 | 2013-06-27 13:31:54 +0900 | [diff] [blame] | 40 | #include "ufshcd.h" |
| 41 | |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 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 | |
Sujit Reddy Thumma | c6e79da | 2014-09-25 15:32:23 +0300 | [diff] [blame^] | 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 | |
| 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 | } |
| 116 | out: |
| 117 | kfree(clkfreq); |
| 118 | return ret; |
| 119 | } |
| 120 | |
Sujit Reddy Thumma | aa49761 | 2014-09-25 15:32:22 +0300 | [diff] [blame] | 121 | #define MAX_PROP_SIZE 32 |
| 122 | static 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 | |
| 150 | snprintf(prop_name, MAX_PROP_SIZE, "%s-max-microamp", name); |
| 151 | ret = of_property_read_u32(np, prop_name, &vreg->max_uA); |
| 152 | if (ret) { |
| 153 | dev_err(dev, "%s: unable to find %s err %d\n", |
| 154 | __func__, prop_name, ret); |
| 155 | goto out_free; |
| 156 | } |
| 157 | |
| 158 | vreg->min_uA = 0; |
| 159 | if (!strcmp(name, "vcc")) { |
| 160 | if (of_property_read_bool(np, "vcc-supply-1p8")) { |
| 161 | vreg->min_uV = UFS_VREG_VCC_1P8_MIN_UV; |
| 162 | vreg->max_uV = UFS_VREG_VCC_1P8_MAX_UV; |
| 163 | } else { |
| 164 | vreg->min_uV = UFS_VREG_VCC_MIN_UV; |
| 165 | vreg->max_uV = UFS_VREG_VCC_MAX_UV; |
| 166 | } |
| 167 | } else if (!strcmp(name, "vccq")) { |
| 168 | vreg->min_uV = UFS_VREG_VCCQ_MIN_UV; |
| 169 | vreg->max_uV = UFS_VREG_VCCQ_MAX_UV; |
| 170 | } else if (!strcmp(name, "vccq2")) { |
| 171 | vreg->min_uV = UFS_VREG_VCCQ2_MIN_UV; |
| 172 | vreg->max_uV = UFS_VREG_VCCQ2_MAX_UV; |
| 173 | } |
| 174 | |
| 175 | goto out; |
| 176 | |
| 177 | out_free: |
| 178 | devm_kfree(dev, vreg); |
| 179 | vreg = NULL; |
| 180 | out: |
| 181 | if (!ret) |
| 182 | *out_vreg = vreg; |
| 183 | return ret; |
| 184 | } |
| 185 | |
| 186 | /** |
| 187 | * ufshcd_parse_regulator_info - get regulator info from device tree |
| 188 | * @hba: per adapter instance |
| 189 | * |
| 190 | * Get regulator info from device tree for vcc, vccq, vccq2 power supplies. |
| 191 | * If any of the supplies are not defined it is assumed that they are always-on |
| 192 | * and hence return zero. If the property is defined but parsing is failed |
| 193 | * then return corresponding error. |
| 194 | */ |
| 195 | static int ufshcd_parse_regulator_info(struct ufs_hba *hba) |
| 196 | { |
| 197 | int err; |
| 198 | struct device *dev = hba->dev; |
| 199 | struct ufs_vreg_info *info = &hba->vreg_info; |
| 200 | |
| 201 | err = ufshcd_populate_vreg(dev, "vcc", &info->vcc); |
| 202 | if (err) |
| 203 | goto out; |
| 204 | |
| 205 | err = ufshcd_populate_vreg(dev, "vccq", &info->vccq); |
| 206 | if (err) |
| 207 | goto out; |
| 208 | |
| 209 | err = ufshcd_populate_vreg(dev, "vccq2", &info->vccq2); |
| 210 | out: |
| 211 | return err; |
| 212 | } |
| 213 | |
Vinayak Holikatti | 03b1781 | 2013-02-26 18:04:45 +0530 | [diff] [blame] | 214 | #ifdef CONFIG_PM |
| 215 | /** |
| 216 | * ufshcd_pltfrm_suspend - suspend power management function |
| 217 | * @dev: pointer to device handle |
| 218 | * |
| 219 | * |
| 220 | * Returns 0 |
| 221 | */ |
| 222 | static int ufshcd_pltfrm_suspend(struct device *dev) |
| 223 | { |
| 224 | struct platform_device *pdev = to_platform_device(dev); |
| 225 | struct ufs_hba *hba = platform_get_drvdata(pdev); |
| 226 | |
| 227 | /* |
| 228 | * TODO: |
| 229 | * 1. Call ufshcd_suspend |
| 230 | * 2. Do bus specific power management |
| 231 | */ |
| 232 | |
| 233 | disable_irq(hba->irq); |
| 234 | |
| 235 | return 0; |
| 236 | } |
| 237 | |
| 238 | /** |
| 239 | * ufshcd_pltfrm_resume - resume power management function |
| 240 | * @dev: pointer to device handle |
| 241 | * |
| 242 | * Returns 0 |
| 243 | */ |
| 244 | static int ufshcd_pltfrm_resume(struct device *dev) |
| 245 | { |
| 246 | struct platform_device *pdev = to_platform_device(dev); |
| 247 | struct ufs_hba *hba = platform_get_drvdata(pdev); |
| 248 | |
| 249 | /* |
| 250 | * TODO: |
| 251 | * 1. Call ufshcd_resume. |
| 252 | * 2. Do bus specific wake up |
| 253 | */ |
| 254 | |
| 255 | enable_irq(hba->irq); |
| 256 | |
| 257 | return 0; |
| 258 | } |
| 259 | #else |
| 260 | #define ufshcd_pltfrm_suspend NULL |
| 261 | #define ufshcd_pltfrm_resume NULL |
| 262 | #endif |
| 263 | |
Sujit Reddy Thumma | 6269473 | 2013-07-30 00:36:00 +0530 | [diff] [blame] | 264 | #ifdef CONFIG_PM_RUNTIME |
| 265 | static int ufshcd_pltfrm_runtime_suspend(struct device *dev) |
| 266 | { |
| 267 | struct ufs_hba *hba = dev_get_drvdata(dev); |
| 268 | |
| 269 | if (!hba) |
| 270 | return 0; |
| 271 | |
| 272 | return ufshcd_runtime_suspend(hba); |
| 273 | } |
| 274 | static int ufshcd_pltfrm_runtime_resume(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_resume(hba); |
| 282 | } |
| 283 | static int ufshcd_pltfrm_runtime_idle(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_idle(hba); |
| 291 | } |
| 292 | #else /* !CONFIG_PM_RUNTIME */ |
| 293 | #define ufshcd_pltfrm_runtime_suspend NULL |
| 294 | #define ufshcd_pltfrm_runtime_resume NULL |
| 295 | #define ufshcd_pltfrm_runtime_idle NULL |
| 296 | #endif /* CONFIG_PM_RUNTIME */ |
| 297 | |
Vinayak Holikatti | 03b1781 | 2013-02-26 18:04:45 +0530 | [diff] [blame] | 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; |
Seungwon Jeon | 2953f85 | 2013-06-27 13:31:54 +0900 | [diff] [blame] | 309 | int irq, err; |
Vinayak Holikatti | 03b1781 | 2013-02-26 18:04:45 +0530 | [diff] [blame] | 310 | struct device *dev = &pdev->dev; |
| 311 | |
| 312 | mem_res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
Seungwon Jeon | 2953f85 | 2013-06-27 13:31:54 +0900 | [diff] [blame] | 313 | mmio_base = devm_ioremap_resource(dev, mem_res); |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 314 | if (IS_ERR(*(void **)&mmio_base)) { |
| 315 | err = PTR_ERR(*(void **)&mmio_base); |
Seungwon Jeon | 2953f85 | 2013-06-27 13:31:54 +0900 | [diff] [blame] | 316 | goto out; |
Vinayak Holikatti | 03b1781 | 2013-02-26 18:04:45 +0530 | [diff] [blame] | 317 | } |
| 318 | |
Seungwon Jeon | 2953f85 | 2013-06-27 13:31:54 +0900 | [diff] [blame] | 319 | irq = platform_get_irq(pdev, 0); |
| 320 | if (irq < 0) { |
| 321 | dev_err(dev, "IRQ resource not available\n"); |
Vinayak Holikatti | 03b1781 | 2013-02-26 18:04:45 +0530 | [diff] [blame] | 322 | err = -ENODEV; |
Seungwon Jeon | 2953f85 | 2013-06-27 13:31:54 +0900 | [diff] [blame] | 323 | goto out; |
Vinayak Holikatti | 03b1781 | 2013-02-26 18:04:45 +0530 | [diff] [blame] | 324 | } |
| 325 | |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 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 | |
Sujit Reddy Thumma | c6e79da | 2014-09-25 15:32:23 +0300 | [diff] [blame^] | 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 | } |
Sujit Reddy Thumma | aa49761 | 2014-09-25 15:32:22 +0300 | [diff] [blame] | 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 | |
Sujit Reddy Thumma | 6269473 | 2013-07-30 00:36:00 +0530 | [diff] [blame] | 347 | pm_runtime_set_active(&pdev->dev); |
| 348 | pm_runtime_enable(&pdev->dev); |
| 349 | |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 350 | err = ufshcd_init(hba, mmio_base, irq); |
Vinayak Holikatti | 03b1781 | 2013-02-26 18:04:45 +0530 | [diff] [blame] | 351 | if (err) { |
Seungwon Jeon | 2953f85 | 2013-06-27 13:31:54 +0900 | [diff] [blame] | 352 | dev_err(dev, "Intialization failed\n"); |
Sujit Reddy Thumma | 6269473 | 2013-07-30 00:36:00 +0530 | [diff] [blame] | 353 | goto out_disable_rpm; |
Vinayak Holikatti | 03b1781 | 2013-02-26 18:04:45 +0530 | [diff] [blame] | 354 | } |
| 355 | |
| 356 | platform_set_drvdata(pdev, hba); |
| 357 | |
Sujit Reddy Thumma | 6269473 | 2013-07-30 00:36:00 +0530 | [diff] [blame] | 358 | return 0; |
| 359 | |
| 360 | out_disable_rpm: |
| 361 | pm_runtime_disable(&pdev->dev); |
| 362 | pm_runtime_set_suspended(&pdev->dev); |
Seungwon Jeon | 2953f85 | 2013-06-27 13:31:54 +0900 | [diff] [blame] | 363 | out: |
Vinayak Holikatti | 03b1781 | 2013-02-26 18:04:45 +0530 | [diff] [blame] | 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 | { |
Vinayak Holikatti | 03b1781 | 2013-02-26 18:04:45 +0530 | [diff] [blame] | 375 | struct ufs_hba *hba = platform_get_drvdata(pdev); |
| 376 | |
Sujit Reddy Thumma | 6269473 | 2013-07-30 00:36:00 +0530 | [diff] [blame] | 377 | pm_runtime_get_sync(&(pdev)->dev); |
Vinayak Holikatti | 03b1781 | 2013-02-26 18:04:45 +0530 | [diff] [blame] | 378 | ufshcd_remove(hba); |
Vinayak Holikatti | 03b1781 | 2013-02-26 18:04:45 +0530 | [diff] [blame] | 379 | return 0; |
| 380 | } |
| 381 | |
| 382 | static const struct of_device_id ufs_of_match[] = { |
| 383 | { .compatible = "jedec,ufs-1.1"}, |
Akinobu Mita | 2e2930a | 2013-06-26 22:39:32 +0530 | [diff] [blame] | 384 | {}, |
Vinayak Holikatti | 03b1781 | 2013-02-26 18:04:45 +0530 | [diff] [blame] | 385 | }; |
| 386 | |
| 387 | static const struct dev_pm_ops ufshcd_dev_pm_ops = { |
| 388 | .suspend = ufshcd_pltfrm_suspend, |
| 389 | .resume = ufshcd_pltfrm_resume, |
Sujit Reddy Thumma | 6269473 | 2013-07-30 00:36:00 +0530 | [diff] [blame] | 390 | .runtime_suspend = ufshcd_pltfrm_runtime_suspend, |
| 391 | .runtime_resume = ufshcd_pltfrm_runtime_resume, |
| 392 | .runtime_idle = ufshcd_pltfrm_runtime_idle, |
Vinayak Holikatti | 03b1781 | 2013-02-26 18:04:45 +0530 | [diff] [blame] | 393 | }; |
| 394 | |
| 395 | static struct platform_driver ufshcd_pltfrm_driver = { |
| 396 | .probe = ufshcd_pltfrm_probe, |
| 397 | .remove = ufshcd_pltfrm_remove, |
| 398 | .driver = { |
| 399 | .name = "ufshcd", |
| 400 | .owner = THIS_MODULE, |
| 401 | .pm = &ufshcd_dev_pm_ops, |
| 402 | .of_match_table = ufs_of_match, |
| 403 | }, |
| 404 | }; |
| 405 | |
| 406 | module_platform_driver(ufshcd_pltfrm_driver); |
| 407 | |
| 408 | MODULE_AUTHOR("Santosh Yaragnavi <santosh.sy@samsung.com>"); |
| 409 | MODULE_AUTHOR("Vinayak Holikatti <h.vinayak@samsung.com>"); |
| 410 | MODULE_DESCRIPTION("UFS host controller Pltform bus based glue driver"); |
| 411 | MODULE_LICENSE("GPL"); |
| 412 | MODULE_VERSION(UFSHCD_DRIVER_VERSION); |