blob: 63c2e2ed12886abc83e516861b331ea9c3871c28 [file] [log] [blame]
Zhangfei Gao036f29d2014-01-09 22:35:11 +08001/*
2 * Copyright (c) 2013 Linaro Ltd.
3 * Copyright (c) 2013 Hisilicon Limited.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 */
10
Zhangfei Gao036f29d2014-01-09 22:35:11 +080011#include <linux/clk.h>
Zhangfei Gao0293efd2015-05-14 16:45:19 +080012#include <linux/mfd/syscon.h>
Zhangfei Gao036f29d2014-01-09 22:35:11 +080013#include <linux/mmc/host.h>
14#include <linux/mmc/dw_mmc.h>
Zhangfei Gao0293efd2015-05-14 16:45:19 +080015#include <linux/module.h>
Zhangfei Gao036f29d2014-01-09 22:35:11 +080016#include <linux/of_address.h>
Zhangfei Gao0293efd2015-05-14 16:45:19 +080017#include <linux/platform_device.h>
18#include <linux/regmap.h>
19#include <linux/regulator/consumer.h>
Zhangfei Gao036f29d2014-01-09 22:35:11 +080020
21#include "dw_mmc.h"
22#include "dw_mmc-pltfm.h"
23
Zhangfei Gao0293efd2015-05-14 16:45:19 +080024/*
25 * hi6220 sd only support io voltage 1.8v and 3v
26 * Also need config AO_SCTRL_SEL18 accordingly
27 */
28#define AO_SCTRL_SEL18 BIT(10)
29#define AO_SCTRL_CTRL3 0x40C
30
31struct k3_priv {
32 struct regmap *reg;
33};
34
Zhangfei Gao036f29d2014-01-09 22:35:11 +080035static void dw_mci_k3_set_ios(struct dw_mci *host, struct mmc_ios *ios)
36{
Zhangfei Gao036f29d2014-01-09 22:35:11 +080037 int ret;
38
Zhangfei Gao0e662442014-01-13 17:14:29 +080039 ret = clk_set_rate(host->ciu_clk, ios->clock);
Zhangfei Gao036f29d2014-01-09 22:35:11 +080040 if (ret)
Zhangfei Gao0e662442014-01-13 17:14:29 +080041 dev_warn(host->dev, "failed to set rate %uHz\n", ios->clock);
Zhangfei Gao036f29d2014-01-09 22:35:11 +080042
43 host->bus_hz = clk_get_rate(host->ciu_clk);
44}
45
Zhangfei Gao036f29d2014-01-09 22:35:11 +080046static const struct dw_mci_drv_data k3_drv_data = {
47 .set_ios = dw_mci_k3_set_ios,
Zhangfei Gao036f29d2014-01-09 22:35:11 +080048};
49
Zhangfei Gao0293efd2015-05-14 16:45:19 +080050static int dw_mci_hi6220_parse_dt(struct dw_mci *host)
51{
52 struct k3_priv *priv;
53
54 priv = devm_kzalloc(host->dev, sizeof(*priv), GFP_KERNEL);
55 if (!priv)
56 return -ENOMEM;
57
58 priv->reg = syscon_regmap_lookup_by_phandle(host->dev->of_node,
59 "hisilicon,peripheral-syscon");
60 if (IS_ERR(priv->reg))
61 priv->reg = NULL;
62
63 host->priv = priv;
64 return 0;
65}
66
67static int dw_mci_hi6220_switch_voltage(struct mmc_host *mmc, struct mmc_ios *ios)
68{
69 struct dw_mci_slot *slot = mmc_priv(mmc);
70 struct k3_priv *priv;
71 struct dw_mci *host;
72 int min_uv, max_uv;
73 int ret;
74
75 host = slot->host;
76 priv = host->priv;
77
78 if (!priv || !priv->reg)
79 return 0;
80
81 if (ios->signal_voltage == MMC_SIGNAL_VOLTAGE_330) {
82 ret = regmap_update_bits(priv->reg, AO_SCTRL_CTRL3,
83 AO_SCTRL_SEL18, 0);
84 min_uv = 3000000;
85 max_uv = 3000000;
86 } else if (ios->signal_voltage == MMC_SIGNAL_VOLTAGE_180) {
87 ret = regmap_update_bits(priv->reg, AO_SCTRL_CTRL3,
88 AO_SCTRL_SEL18, AO_SCTRL_SEL18);
89 min_uv = 1800000;
90 max_uv = 1800000;
91 } else {
92 dev_dbg(host->dev, "voltage not supported\n");
93 return -EINVAL;
94 }
95
96 if (ret) {
97 dev_dbg(host->dev, "switch voltage failed\n");
98 return ret;
99 }
100
101 if (IS_ERR_OR_NULL(mmc->supply.vqmmc))
102 return 0;
103
104 ret = regulator_set_voltage(mmc->supply.vqmmc, min_uv, max_uv);
105 if (ret) {
106 dev_dbg(host->dev, "Regulator set error %d: %d - %d\n",
107 ret, min_uv, max_uv);
108 return ret;
109 }
110
111 return 0;
112}
113
114static void dw_mci_hi6220_set_ios(struct dw_mci *host, struct mmc_ios *ios)
115{
116 int ret;
117 unsigned int clock;
118
119 clock = (ios->clock <= 25000000) ? 25000000 : ios->clock;
120
121 ret = clk_set_rate(host->biu_clk, clock);
122 if (ret)
123 dev_warn(host->dev, "failed to set rate %uHz\n", clock);
124
125 host->bus_hz = clk_get_rate(host->biu_clk);
126}
127
128static const struct dw_mci_drv_data hi6220_data = {
129 .switch_voltage = dw_mci_hi6220_switch_voltage,
130 .set_ios = dw_mci_hi6220_set_ios,
131 .parse_dt = dw_mci_hi6220_parse_dt,
132};
133
Zhangfei Gao036f29d2014-01-09 22:35:11 +0800134static const struct of_device_id dw_mci_k3_match[] = {
135 { .compatible = "hisilicon,hi4511-dw-mshc", .data = &k3_drv_data, },
Zhangfei Gao0293efd2015-05-14 16:45:19 +0800136 { .compatible = "hisilicon,hi6220-dw-mshc", .data = &hi6220_data, },
Zhangfei Gao036f29d2014-01-09 22:35:11 +0800137 {},
138};
139MODULE_DEVICE_TABLE(of, dw_mci_k3_match);
140
141static int dw_mci_k3_probe(struct platform_device *pdev)
142{
143 const struct dw_mci_drv_data *drv_data;
144 const struct of_device_id *match;
145
146 match = of_match_node(dw_mci_k3_match, pdev->dev.of_node);
147 drv_data = match->data;
148
149 return dw_mci_pltfm_register(pdev, drv_data);
150}
151
Felipe Balbi370aede2014-02-25 08:57:44 -0600152#ifdef CONFIG_PM_SLEEP
Zhangfei Gao036f29d2014-01-09 22:35:11 +0800153static int dw_mci_k3_suspend(struct device *dev)
154{
155 struct dw_mci *host = dev_get_drvdata(dev);
156 int ret;
157
158 ret = dw_mci_suspend(host);
159 if (!ret)
160 clk_disable_unprepare(host->ciu_clk);
161
162 return ret;
163}
164
165static int dw_mci_k3_resume(struct device *dev)
166{
167 struct dw_mci *host = dev_get_drvdata(dev);
168 int ret;
169
170 ret = clk_prepare_enable(host->ciu_clk);
171 if (ret) {
172 dev_err(host->dev, "failed to enable ciu clock\n");
173 return ret;
174 }
175
176 return dw_mci_resume(host);
177}
Felipe Balbi370aede2014-02-25 08:57:44 -0600178#endif /* CONFIG_PM_SLEEP */
Zhangfei Gao036f29d2014-01-09 22:35:11 +0800179
Wei Yongjun85136b72014-01-14 10:03:33 +0800180static SIMPLE_DEV_PM_OPS(dw_mci_k3_pmops, dw_mci_k3_suspend, dw_mci_k3_resume);
Zhangfei Gao036f29d2014-01-09 22:35:11 +0800181
182static struct platform_driver dw_mci_k3_pltfm_driver = {
183 .probe = dw_mci_k3_probe,
184 .remove = dw_mci_pltfm_remove,
185 .driver = {
186 .name = "dwmmc_k3",
187 .of_match_table = dw_mci_k3_match,
188 .pm = &dw_mci_k3_pmops,
189 },
190};
191
192module_platform_driver(dw_mci_k3_pltfm_driver);
193
194MODULE_DESCRIPTION("K3 Specific DW-MSHC Driver Extension");
195MODULE_LICENSE("GPL v2");
Zhangfei Gao7026fd62015-05-14 16:59:44 +0800196MODULE_ALIAS("platform:dwmmc_k3");