ufshcd-pltfrm.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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. #include <linux/platform_device.h>
  36. #include <linux/pm_runtime.h>
  37. #include <linux/of.h>
  38. #include "ufshcd.h"
  39. #include "ufshcd-pltfrm.h"
  40. static int ufshcd_parse_clock_info(struct ufs_hba *hba)
  41. {
  42. int ret = 0;
  43. int cnt;
  44. int i;
  45. struct device *dev = hba->dev;
  46. struct device_node *np = dev->of_node;
  47. char *name;
  48. u32 *clkfreq = NULL;
  49. struct ufs_clk_info *clki;
  50. int len = 0;
  51. size_t sz = 0;
  52. if (!np)
  53. goto out;
  54. INIT_LIST_HEAD(&hba->clk_list_head);
  55. cnt = of_property_count_strings(np, "clock-names");
  56. if (!cnt || (cnt == -EINVAL)) {
  57. dev_info(dev, "%s: Unable to find clocks, assuming enabled\n",
  58. __func__);
  59. } else if (cnt < 0) {
  60. dev_err(dev, "%s: count clock strings failed, err %d\n",
  61. __func__, cnt);
  62. ret = cnt;
  63. }
  64. if (cnt <= 0)
  65. goto out;
  66. if (!of_get_property(np, "freq-table-hz", &len)) {
  67. dev_info(dev, "freq-table-hz property not specified\n");
  68. goto out;
  69. }
  70. if (len <= 0)
  71. goto out;
  72. sz = len / sizeof(*clkfreq);
  73. if (sz != 2 * cnt) {
  74. dev_err(dev, "%s len mismatch\n", "freq-table-hz");
  75. ret = -EINVAL;
  76. goto out;
  77. }
  78. clkfreq = devm_kzalloc(dev, sz * sizeof(*clkfreq),
  79. GFP_KERNEL);
  80. if (!clkfreq) {
  81. ret = -ENOMEM;
  82. goto out;
  83. }
  84. ret = of_property_read_u32_array(np, "freq-table-hz",
  85. clkfreq, sz);
  86. if (ret && (ret != -EINVAL)) {
  87. dev_err(dev, "%s: error reading array %d\n",
  88. "freq-table-hz", ret);
  89. return ret;
  90. }
  91. for (i = 0; i < sz; i += 2) {
  92. ret = of_property_read_string_index(np,
  93. "clock-names", i/2, (const char **)&name);
  94. if (ret)
  95. goto out;
  96. clki = devm_kzalloc(dev, sizeof(*clki), GFP_KERNEL);
  97. if (!clki) {
  98. ret = -ENOMEM;
  99. goto out;
  100. }
  101. clki->min_freq = clkfreq[i];
  102. clki->max_freq = clkfreq[i+1];
  103. clki->name = kstrdup(name, GFP_KERNEL);
  104. dev_dbg(dev, "%s: min %u max %u name %s\n", "freq-table-hz",
  105. clki->min_freq, clki->max_freq, clki->name);
  106. list_add_tail(&clki->list, &hba->clk_list_head);
  107. }
  108. out:
  109. return ret;
  110. }
  111. #define MAX_PROP_SIZE 32
  112. static int ufshcd_populate_vreg(struct device *dev, const char *name,
  113. struct ufs_vreg **out_vreg)
  114. {
  115. int ret = 0;
  116. char prop_name[MAX_PROP_SIZE];
  117. struct ufs_vreg *vreg = NULL;
  118. struct device_node *np = dev->of_node;
  119. if (!np) {
  120. dev_err(dev, "%s: non DT initialization\n", __func__);
  121. goto out;
  122. }
  123. snprintf(prop_name, MAX_PROP_SIZE, "%s-supply", name);
  124. if (!of_parse_phandle(np, prop_name, 0)) {
  125. dev_info(dev, "%s: Unable to find %s regulator, assuming enabled\n",
  126. __func__, prop_name);
  127. goto out;
  128. }
  129. vreg = devm_kzalloc(dev, sizeof(*vreg), GFP_KERNEL);
  130. if (!vreg)
  131. return -ENOMEM;
  132. vreg->name = kstrdup(name, GFP_KERNEL);
  133. /* if fixed regulator no need further initialization */
  134. snprintf(prop_name, MAX_PROP_SIZE, "%s-fixed-regulator", name);
  135. if (of_property_read_bool(np, prop_name))
  136. goto out;
  137. snprintf(prop_name, MAX_PROP_SIZE, "%s-max-microamp", name);
  138. ret = of_property_read_u32(np, prop_name, &vreg->max_uA);
  139. if (ret) {
  140. dev_err(dev, "%s: unable to find %s err %d\n",
  141. __func__, prop_name, ret);
  142. goto out;
  143. }
  144. vreg->min_uA = 0;
  145. if (!strcmp(name, "vcc")) {
  146. if (of_property_read_bool(np, "vcc-supply-1p8")) {
  147. vreg->min_uV = UFS_VREG_VCC_1P8_MIN_UV;
  148. vreg->max_uV = UFS_VREG_VCC_1P8_MAX_UV;
  149. } else {
  150. vreg->min_uV = UFS_VREG_VCC_MIN_UV;
  151. vreg->max_uV = UFS_VREG_VCC_MAX_UV;
  152. }
  153. } else if (!strcmp(name, "vccq")) {
  154. vreg->min_uV = UFS_VREG_VCCQ_MIN_UV;
  155. vreg->max_uV = UFS_VREG_VCCQ_MAX_UV;
  156. } else if (!strcmp(name, "vccq2")) {
  157. vreg->min_uV = UFS_VREG_VCCQ2_MIN_UV;
  158. vreg->max_uV = UFS_VREG_VCCQ2_MAX_UV;
  159. }
  160. goto out;
  161. out:
  162. if (!ret)
  163. *out_vreg = vreg;
  164. return ret;
  165. }
  166. /**
  167. * ufshcd_parse_regulator_info - get regulator info from device tree
  168. * @hba: per adapter instance
  169. *
  170. * Get regulator info from device tree for vcc, vccq, vccq2 power supplies.
  171. * If any of the supplies are not defined it is assumed that they are always-on
  172. * and hence return zero. If the property is defined but parsing is failed
  173. * then return corresponding error.
  174. */
  175. static int ufshcd_parse_regulator_info(struct ufs_hba *hba)
  176. {
  177. int err;
  178. struct device *dev = hba->dev;
  179. struct ufs_vreg_info *info = &hba->vreg_info;
  180. err = ufshcd_populate_vreg(dev, "vdd-hba", &info->vdd_hba);
  181. if (err)
  182. goto out;
  183. err = ufshcd_populate_vreg(dev, "vcc", &info->vcc);
  184. if (err)
  185. goto out;
  186. err = ufshcd_populate_vreg(dev, "vccq", &info->vccq);
  187. if (err)
  188. goto out;
  189. err = ufshcd_populate_vreg(dev, "vccq2", &info->vccq2);
  190. out:
  191. return err;
  192. }
  193. #ifdef CONFIG_PM
  194. /**
  195. * ufshcd_pltfrm_suspend - suspend power management function
  196. * @dev: pointer to device handle
  197. *
  198. * Returns 0 if successful
  199. * Returns non-zero otherwise
  200. */
  201. int ufshcd_pltfrm_suspend(struct device *dev)
  202. {
  203. return ufshcd_system_suspend(dev_get_drvdata(dev));
  204. }
  205. EXPORT_SYMBOL_GPL(ufshcd_pltfrm_suspend);
  206. /**
  207. * ufshcd_pltfrm_resume - resume power management function
  208. * @dev: pointer to device handle
  209. *
  210. * Returns 0 if successful
  211. * Returns non-zero otherwise
  212. */
  213. int ufshcd_pltfrm_resume(struct device *dev)
  214. {
  215. return ufshcd_system_resume(dev_get_drvdata(dev));
  216. }
  217. EXPORT_SYMBOL_GPL(ufshcd_pltfrm_resume);
  218. int ufshcd_pltfrm_runtime_suspend(struct device *dev)
  219. {
  220. return ufshcd_runtime_suspend(dev_get_drvdata(dev));
  221. }
  222. EXPORT_SYMBOL_GPL(ufshcd_pltfrm_runtime_suspend);
  223. int ufshcd_pltfrm_runtime_resume(struct device *dev)
  224. {
  225. return ufshcd_runtime_resume(dev_get_drvdata(dev));
  226. }
  227. EXPORT_SYMBOL_GPL(ufshcd_pltfrm_runtime_resume);
  228. int ufshcd_pltfrm_runtime_idle(struct device *dev)
  229. {
  230. return ufshcd_runtime_idle(dev_get_drvdata(dev));
  231. }
  232. EXPORT_SYMBOL_GPL(ufshcd_pltfrm_runtime_idle);
  233. #endif /* CONFIG_PM */
  234. void ufshcd_pltfrm_shutdown(struct platform_device *pdev)
  235. {
  236. ufshcd_shutdown((struct ufs_hba *)platform_get_drvdata(pdev));
  237. }
  238. EXPORT_SYMBOL_GPL(ufshcd_pltfrm_shutdown);
  239. /**
  240. * ufshcd_pltfrm_init - probe routine of the driver
  241. * @pdev: pointer to Platform device handle
  242. * @vops: pointer to variant ops
  243. *
  244. * Returns 0 on success, non-zero value on failure
  245. */
  246. int ufshcd_pltfrm_init(struct platform_device *pdev,
  247. struct ufs_hba_variant_ops *vops)
  248. {
  249. struct ufs_hba *hba;
  250. void __iomem *mmio_base;
  251. struct resource *mem_res;
  252. int irq, err;
  253. struct device *dev = &pdev->dev;
  254. mem_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  255. mmio_base = devm_ioremap_resource(dev, mem_res);
  256. if (IS_ERR(*(void **)&mmio_base)) {
  257. err = PTR_ERR(*(void **)&mmio_base);
  258. goto out;
  259. }
  260. irq = platform_get_irq(pdev, 0);
  261. if (irq < 0) {
  262. dev_err(dev, "IRQ resource not available\n");
  263. err = -ENODEV;
  264. goto out;
  265. }
  266. err = ufshcd_alloc_host(dev, &hba);
  267. if (err) {
  268. dev_err(&pdev->dev, "Allocation failed\n");
  269. goto out;
  270. }
  271. hba->vops = vops;
  272. err = ufshcd_parse_clock_info(hba);
  273. if (err) {
  274. dev_err(&pdev->dev, "%s: clock parse failed %d\n",
  275. __func__, err);
  276. goto dealloc_host;
  277. }
  278. err = ufshcd_parse_regulator_info(hba);
  279. if (err) {
  280. dev_err(&pdev->dev, "%s: regulator init failed %d\n",
  281. __func__, err);
  282. goto dealloc_host;
  283. }
  284. pm_runtime_set_active(&pdev->dev);
  285. pm_runtime_enable(&pdev->dev);
  286. err = ufshcd_init(hba, mmio_base, irq);
  287. if (err) {
  288. dev_err(dev, "Intialization failed\n");
  289. goto out_disable_rpm;
  290. }
  291. platform_set_drvdata(pdev, hba);
  292. return 0;
  293. out_disable_rpm:
  294. pm_runtime_disable(&pdev->dev);
  295. pm_runtime_set_suspended(&pdev->dev);
  296. dealloc_host:
  297. ufshcd_dealloc_host(hba);
  298. out:
  299. return err;
  300. }
  301. EXPORT_SYMBOL_GPL(ufshcd_pltfrm_init);
  302. MODULE_AUTHOR("Santosh Yaragnavi <santosh.sy@samsung.com>");
  303. MODULE_AUTHOR("Vinayak Holikatti <h.vinayak@samsung.com>");
  304. MODULE_DESCRIPTION("UFS host controller Pltform bus based glue driver");
  305. MODULE_LICENSE("GPL");
  306. MODULE_VERSION(UFSHCD_DRIVER_VERSION);