1
2
3
4
5
6
7#define pr_fmt(fmt) "nuvoton-kcs-bmc: " fmt
8
9#include <linux/atomic.h>
10#include <linux/errno.h>
11#include <linux/interrupt.h>
12#include <linux/io.h>
13#include <linux/mfd/syscon.h>
14#include <linux/module.h>
15#include <linux/of.h>
16#include <linux/platform_device.h>
17#include <linux/regmap.h>
18#include <linux/slab.h>
19
20#include "kcs_bmc.h"
21
22#define DEVICE_NAME "npcm-kcs-bmc"
23#define KCS_CHANNEL_MAX 3
24
25#define KCS1ST 0x0C
26#define KCS2ST 0x1E
27#define KCS3ST 0x30
28
29#define KCS1DO 0x0E
30#define KCS2DO 0x20
31#define KCS3DO 0x32
32
33#define KCS1DI 0x10
34#define KCS2DI 0x22
35#define KCS3DI 0x34
36
37#define KCS1CTL 0x18
38#define KCS2CTL 0x2A
39#define KCS3CTL 0x3C
40#define KCS_CTL_IBFIE BIT(0)
41
42#define KCS1IE 0x1C
43#define KCS2IE 0x2E
44#define KCS3IE 0x40
45#define KCS_IE_IRQE BIT(0)
46#define KCS_IE_HIRQE BIT(3)
47
48
49
50
51
52
53
54
55
56
57
58
59struct npcm7xx_kcs_reg {
60 u32 sts;
61 u32 dob;
62 u32 dib;
63 u32 ctl;
64 u32 ie;
65};
66
67struct npcm7xx_kcs_bmc {
68 struct regmap *map;
69
70 const struct npcm7xx_kcs_reg *reg;
71};
72
73static const struct npcm7xx_kcs_reg npcm7xx_kcs_reg_tbl[KCS_CHANNEL_MAX] = {
74 { .sts = KCS1ST, .dob = KCS1DO, .dib = KCS1DI, .ctl = KCS1CTL, .ie = KCS1IE },
75 { .sts = KCS2ST, .dob = KCS2DO, .dib = KCS2DI, .ctl = KCS2CTL, .ie = KCS2IE },
76 { .sts = KCS3ST, .dob = KCS3DO, .dib = KCS3DI, .ctl = KCS3CTL, .ie = KCS3IE },
77};
78
79static u8 npcm7xx_kcs_inb(struct kcs_bmc *kcs_bmc, u32 reg)
80{
81 struct npcm7xx_kcs_bmc *priv = kcs_bmc_priv(kcs_bmc);
82 u32 val = 0;
83 int rc;
84
85 rc = regmap_read(priv->map, reg, &val);
86 WARN(rc != 0, "regmap_read() failed: %d\n", rc);
87
88 return rc == 0 ? (u8)val : 0;
89}
90
91static void npcm7xx_kcs_outb(struct kcs_bmc *kcs_bmc, u32 reg, u8 data)
92{
93 struct npcm7xx_kcs_bmc *priv = kcs_bmc_priv(kcs_bmc);
94 int rc;
95
96 rc = regmap_write(priv->map, reg, data);
97 WARN(rc != 0, "regmap_write() failed: %d\n", rc);
98}
99
100static void npcm7xx_kcs_enable_channel(struct kcs_bmc *kcs_bmc, bool enable)
101{
102 struct npcm7xx_kcs_bmc *priv = kcs_bmc_priv(kcs_bmc);
103
104 regmap_update_bits(priv->map, priv->reg->ctl, KCS_CTL_IBFIE,
105 enable ? KCS_CTL_IBFIE : 0);
106
107 regmap_update_bits(priv->map, priv->reg->ie, KCS_IE_IRQE | KCS_IE_HIRQE,
108 enable ? KCS_IE_IRQE | KCS_IE_HIRQE : 0);
109}
110
111static irqreturn_t npcm7xx_kcs_irq(int irq, void *arg)
112{
113 struct kcs_bmc *kcs_bmc = arg;
114
115 if (!kcs_bmc_handle_event(kcs_bmc))
116 return IRQ_HANDLED;
117
118 return IRQ_NONE;
119}
120
121static int npcm7xx_kcs_config_irq(struct kcs_bmc *kcs_bmc,
122 struct platform_device *pdev)
123{
124 struct device *dev = &pdev->dev;
125 int irq;
126
127 irq = platform_get_irq(pdev, 0);
128 if (irq < 0)
129 return irq;
130
131 return devm_request_irq(dev, irq, npcm7xx_kcs_irq, IRQF_SHARED,
132 dev_name(dev), kcs_bmc);
133}
134
135static int npcm7xx_kcs_probe(struct platform_device *pdev)
136{
137 struct device *dev = &pdev->dev;
138 struct npcm7xx_kcs_bmc *priv;
139 struct kcs_bmc *kcs_bmc;
140 u32 chan;
141 int rc;
142
143 rc = of_property_read_u32(dev->of_node, "kcs_chan", &chan);
144 if (rc != 0 || chan == 0 || chan > KCS_CHANNEL_MAX) {
145 dev_err(dev, "no valid 'kcs_chan' configured\n");
146 return -ENODEV;
147 }
148
149 kcs_bmc = kcs_bmc_alloc(dev, sizeof(*priv), chan);
150 if (!kcs_bmc)
151 return -ENOMEM;
152
153 priv = kcs_bmc_priv(kcs_bmc);
154 priv->map = syscon_node_to_regmap(dev->parent->of_node);
155 if (IS_ERR(priv->map)) {
156 dev_err(dev, "Couldn't get regmap\n");
157 return -ENODEV;
158 }
159 priv->reg = &npcm7xx_kcs_reg_tbl[chan - 1];
160
161 kcs_bmc->ioreg.idr = priv->reg->dib;
162 kcs_bmc->ioreg.odr = priv->reg->dob;
163 kcs_bmc->ioreg.str = priv->reg->sts;
164 kcs_bmc->io_inputb = npcm7xx_kcs_inb;
165 kcs_bmc->io_outputb = npcm7xx_kcs_outb;
166
167 dev_set_drvdata(dev, kcs_bmc);
168
169 npcm7xx_kcs_enable_channel(kcs_bmc, true);
170 rc = npcm7xx_kcs_config_irq(kcs_bmc, pdev);
171 if (rc)
172 return rc;
173
174 rc = misc_register(&kcs_bmc->miscdev);
175 if (rc) {
176 dev_err(dev, "Unable to register device\n");
177 return rc;
178 }
179
180 pr_info("channel=%u idr=0x%x odr=0x%x str=0x%x\n",
181 chan,
182 kcs_bmc->ioreg.idr, kcs_bmc->ioreg.odr, kcs_bmc->ioreg.str);
183
184 return 0;
185}
186
187static int npcm7xx_kcs_remove(struct platform_device *pdev)
188{
189 struct kcs_bmc *kcs_bmc = dev_get_drvdata(&pdev->dev);
190
191 misc_deregister(&kcs_bmc->miscdev);
192
193 return 0;
194}
195
196static const struct of_device_id npcm_kcs_bmc_match[] = {
197 { .compatible = "nuvoton,npcm750-kcs-bmc" },
198 { }
199};
200MODULE_DEVICE_TABLE(of, npcm_kcs_bmc_match);
201
202static struct platform_driver npcm_kcs_bmc_driver = {
203 .driver = {
204 .name = DEVICE_NAME,
205 .of_match_table = npcm_kcs_bmc_match,
206 },
207 .probe = npcm7xx_kcs_probe,
208 .remove = npcm7xx_kcs_remove,
209};
210module_platform_driver(npcm_kcs_bmc_driver);
211
212MODULE_LICENSE("GPL v2");
213MODULE_AUTHOR("Avi Fishman <avifishman70@gmail.com>");
214MODULE_AUTHOR("Haiyue Wang <haiyue.wang@linux.intel.com>");
215MODULE_DESCRIPTION("NPCM7xx device interface to the KCS BMC device");
216