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 | aa49761 | 2014-09-25 15:32:22 +0300 | [diff] [blame^] | 56 | #define MAX_PROP_SIZE 32 |
| 57 | static 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 | |
| 112 | out_free: |
| 113 | devm_kfree(dev, vreg); |
| 114 | vreg = NULL; |
| 115 | out: |
| 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 | */ |
| 130 | static 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); |
| 145 | out: |
| 146 | return err; |
| 147 | } |
| 148 | |
Vinayak Holikatti | 03b1781 | 2013-02-26 18:04:45 +0530 | [diff] [blame] | 149 | #ifdef CONFIG_PM |
| 150 | /** |
| 151 | * ufshcd_pltfrm_suspend - suspend power management function |
| 152 | * @dev: pointer to device handle |
| 153 | * |
| 154 | * |
| 155 | * Returns 0 |
| 156 | */ |
| 157 | static 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 | */ |
| 179 | static 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 Thumma | 6269473 | 2013-07-30 00:36:00 +0530 | [diff] [blame] | 199 | #ifdef CONFIG_PM_RUNTIME |
| 200 | static 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 | } |
| 209 | static 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 | } |
| 218 | static 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 Holikatti | 03b1781 | 2013-02-26 18:04:45 +0530 | [diff] [blame] | 233 | /** |
| 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 | */ |
| 239 | static 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 Jeon | 2953f85 | 2013-06-27 13:31:54 +0900 | [diff] [blame] | 244 | int irq, err; |
Vinayak Holikatti | 03b1781 | 2013-02-26 18:04:45 +0530 | [diff] [blame] | 245 | struct device *dev = &pdev->dev; |
| 246 | |
| 247 | mem_res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
Seungwon Jeon | 2953f85 | 2013-06-27 13:31:54 +0900 | [diff] [blame] | 248 | mmio_base = devm_ioremap_resource(dev, mem_res); |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 249 | if (IS_ERR(*(void **)&mmio_base)) { |
| 250 | err = PTR_ERR(*(void **)&mmio_base); |
Seungwon Jeon | 2953f85 | 2013-06-27 13:31:54 +0900 | [diff] [blame] | 251 | goto out; |
Vinayak Holikatti | 03b1781 | 2013-02-26 18:04:45 +0530 | [diff] [blame] | 252 | } |
| 253 | |
Seungwon Jeon | 2953f85 | 2013-06-27 13:31:54 +0900 | [diff] [blame] | 254 | irq = platform_get_irq(pdev, 0); |
| 255 | if (irq < 0) { |
| 256 | dev_err(dev, "IRQ resource not available\n"); |
Vinayak Holikatti | 03b1781 | 2013-02-26 18:04:45 +0530 | [diff] [blame] | 257 | err = -ENODEV; |
Seungwon Jeon | 2953f85 | 2013-06-27 13:31:54 +0900 | [diff] [blame] | 258 | goto out; |
Vinayak Holikatti | 03b1781 | 2013-02-26 18:04:45 +0530 | [diff] [blame] | 259 | } |
| 260 | |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 261 | 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 Thumma | aa49761 | 2014-09-25 15:32:22 +0300 | [diff] [blame^] | 269 | 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 Thumma | 6269473 | 2013-07-30 00:36:00 +0530 | [diff] [blame] | 276 | pm_runtime_set_active(&pdev->dev); |
| 277 | pm_runtime_enable(&pdev->dev); |
| 278 | |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 279 | err = ufshcd_init(hba, mmio_base, irq); |
Vinayak Holikatti | 03b1781 | 2013-02-26 18:04:45 +0530 | [diff] [blame] | 280 | if (err) { |
Seungwon Jeon | 2953f85 | 2013-06-27 13:31:54 +0900 | [diff] [blame] | 281 | dev_err(dev, "Intialization failed\n"); |
Sujit Reddy Thumma | 6269473 | 2013-07-30 00:36:00 +0530 | [diff] [blame] | 282 | goto out_disable_rpm; |
Vinayak Holikatti | 03b1781 | 2013-02-26 18:04:45 +0530 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | platform_set_drvdata(pdev, hba); |
| 286 | |
Sujit Reddy Thumma | 6269473 | 2013-07-30 00:36:00 +0530 | [diff] [blame] | 287 | return 0; |
| 288 | |
| 289 | out_disable_rpm: |
| 290 | pm_runtime_disable(&pdev->dev); |
| 291 | pm_runtime_set_suspended(&pdev->dev); |
Seungwon Jeon | 2953f85 | 2013-06-27 13:31:54 +0900 | [diff] [blame] | 292 | out: |
Vinayak Holikatti | 03b1781 | 2013-02-26 18:04:45 +0530 | [diff] [blame] | 293 | 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 | */ |
| 302 | static int ufshcd_pltfrm_remove(struct platform_device *pdev) |
| 303 | { |
Vinayak Holikatti | 03b1781 | 2013-02-26 18:04:45 +0530 | [diff] [blame] | 304 | struct ufs_hba *hba = platform_get_drvdata(pdev); |
| 305 | |
Sujit Reddy Thumma | 6269473 | 2013-07-30 00:36:00 +0530 | [diff] [blame] | 306 | pm_runtime_get_sync(&(pdev)->dev); |
Vinayak Holikatti | 03b1781 | 2013-02-26 18:04:45 +0530 | [diff] [blame] | 307 | ufshcd_remove(hba); |
Vinayak Holikatti | 03b1781 | 2013-02-26 18:04:45 +0530 | [diff] [blame] | 308 | return 0; |
| 309 | } |
| 310 | |
| 311 | static const struct of_device_id ufs_of_match[] = { |
| 312 | { .compatible = "jedec,ufs-1.1"}, |
Akinobu Mita | 2e2930a | 2013-06-26 22:39:32 +0530 | [diff] [blame] | 313 | {}, |
Vinayak Holikatti | 03b1781 | 2013-02-26 18:04:45 +0530 | [diff] [blame] | 314 | }; |
| 315 | |
| 316 | static const struct dev_pm_ops ufshcd_dev_pm_ops = { |
| 317 | .suspend = ufshcd_pltfrm_suspend, |
| 318 | .resume = ufshcd_pltfrm_resume, |
Sujit Reddy Thumma | 6269473 | 2013-07-30 00:36:00 +0530 | [diff] [blame] | 319 | .runtime_suspend = ufshcd_pltfrm_runtime_suspend, |
| 320 | .runtime_resume = ufshcd_pltfrm_runtime_resume, |
| 321 | .runtime_idle = ufshcd_pltfrm_runtime_idle, |
Vinayak Holikatti | 03b1781 | 2013-02-26 18:04:45 +0530 | [diff] [blame] | 322 | }; |
| 323 | |
| 324 | static 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 | |
| 335 | module_platform_driver(ufshcd_pltfrm_driver); |
| 336 | |
| 337 | MODULE_AUTHOR("Santosh Yaragnavi <santosh.sy@samsung.com>"); |
| 338 | MODULE_AUTHOR("Vinayak Holikatti <h.vinayak@samsung.com>"); |
| 339 | MODULE_DESCRIPTION("UFS host controller Pltform bus based glue driver"); |
| 340 | MODULE_LICENSE("GPL"); |
| 341 | MODULE_VERSION(UFSHCD_DRIVER_VERSION); |