2 * Copyright (c) 2009-2011 Wind River Systems, Inc.
3 * Copyright (c) 2011 ST Microelectronics (Alessandro Rubini)
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12 * See the GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 #include <linux/kernel.h>
21 #include <linux/module.h>
22 #include <linux/spinlock.h>
23 #include <linux/errno.h>
24 #include <linux/device.h>
25 #include <linux/slab.h>
26 #include <linux/list.h>
28 #include <linux/ioport.h>
29 #include <linux/pci.h>
30 #include <linux/debugfs.h>
31 #include <linux/seq_file.h>
32 #include <linux/platform_device.h>
33 #include <linux/mfd/core.h>
34 #include <linux/mfd/sta2x11-mfd.h>
36 #include <asm/sta2x11.h>
38 /* This describes STA2X11 MFD chip for us, we may have several */
40 struct sta2x11_instance *instance;
42 struct list_head list;
43 void __iomem *sctl_regs;
44 void __iomem *apbreg_regs;
47 static LIST_HEAD(sta2x11_mfd_list);
49 /* Three functions to act on the list */
50 static struct sta2x11_mfd *sta2x11_mfd_find(struct pci_dev *pdev)
52 struct sta2x11_instance *instance;
53 struct sta2x11_mfd *mfd;
55 if (!pdev && !list_empty(&sta2x11_mfd_list)) {
56 pr_warning("%s: Unspecified device, "
57 "using first instance\n", __func__);
58 return list_entry(sta2x11_mfd_list.next,
59 struct sta2x11_mfd, list);
62 instance = sta2x11_get_instance(pdev);
65 list_for_each_entry(mfd, &sta2x11_mfd_list, list) {
66 if (mfd->instance == instance)
72 static int sta2x11_mfd_add(struct pci_dev *pdev, gfp_t flags)
74 struct sta2x11_mfd *mfd = sta2x11_mfd_find(pdev);
75 struct sta2x11_instance *instance;
79 instance = sta2x11_get_instance(pdev);
82 mfd = kzalloc(sizeof(*mfd), flags);
85 INIT_LIST_HEAD(&mfd->list);
86 spin_lock_init(&mfd->lock);
87 mfd->instance = instance;
88 list_add(&mfd->list, &sta2x11_mfd_list);
92 static int mfd_remove(struct pci_dev *pdev)
94 struct sta2x11_mfd *mfd = sta2x11_mfd_find(pdev);
103 /* These two functions are exported and are not expected to fail */
104 u32 sta2x11_sctl_mask(struct pci_dev *pdev, u32 reg, u32 mask, u32 val)
106 struct sta2x11_mfd *mfd = sta2x11_mfd_find(pdev);
111 dev_warn(&pdev->dev, ": can't access sctl regs\n");
114 if (!mfd->sctl_regs) {
115 dev_warn(&pdev->dev, ": system ctl not initialized\n");
118 spin_lock_irqsave(&mfd->lock, flags);
119 r = readl(mfd->sctl_regs + reg);
123 writel(r, mfd->sctl_regs + reg);
124 spin_unlock_irqrestore(&mfd->lock, flags);
127 EXPORT_SYMBOL(sta2x11_sctl_mask);
129 u32 sta2x11_apbreg_mask(struct pci_dev *pdev, u32 reg, u32 mask, u32 val)
131 struct sta2x11_mfd *mfd = sta2x11_mfd_find(pdev);
136 dev_warn(&pdev->dev, ": can't access apb regs\n");
139 if (!mfd->apbreg_regs) {
140 dev_warn(&pdev->dev, ": apb bridge not initialized\n");
143 spin_lock_irqsave(&mfd->lock, flags);
144 r = readl(mfd->apbreg_regs + reg);
148 writel(r, mfd->apbreg_regs + reg);
149 spin_unlock_irqrestore(&mfd->lock, flags);
152 EXPORT_SYMBOL(sta2x11_apbreg_mask);
154 /* Two debugfs files, for our registers (FIXME: one instance only) */
155 #define REG(regname) {.name = #regname, .offset = SCTL_ ## regname}
156 static struct debugfs_reg32 sta2x11_sctl_regs[] = {
157 REG(SCCTL), REG(ARMCFG), REG(SCPLLCTL), REG(SCPLLFCTRL),
158 REG(SCRESFRACT), REG(SCRESCTRL1), REG(SCRESXTRL2), REG(SCPEREN0),
159 REG(SCPEREN1), REG(SCPEREN2), REG(SCGRST), REG(SCPCIPMCR1),
160 REG(SCPCIPMCR2), REG(SCPCIPMSR1), REG(SCPCIPMSR2), REG(SCPCIPMSR3),
161 REG(SCINTREN), REG(SCRISR), REG(SCCLKSTAT0), REG(SCCLKSTAT1),
162 REG(SCCLKSTAT2), REG(SCRSTSTA),
166 static struct debugfs_regset32 sctl_regset = {
167 .regs = sta2x11_sctl_regs,
168 .nregs = ARRAY_SIZE(sta2x11_sctl_regs),
171 #define REG(regname) {.name = #regname, .offset = regname}
172 static struct debugfs_reg32 sta2x11_apbreg_regs[] = {
173 REG(APBREG_BSR), REG(APBREG_PAER), REG(APBREG_PWAC), REG(APBREG_PRAC),
174 REG(APBREG_PCG), REG(APBREG_PUR), REG(APBREG_EMU_PCG),
178 static struct debugfs_regset32 apbreg_regset = {
179 .regs = sta2x11_apbreg_regs,
180 .nregs = ARRAY_SIZE(sta2x11_apbreg_regs),
183 static struct dentry *sta2x11_sctl_debugfs;
184 static struct dentry *sta2x11_apbreg_debugfs;
186 /* Probe for the two platform devices */
187 static int sta2x11_sctl_probe(struct platform_device *dev)
189 struct pci_dev **pdev;
190 struct sta2x11_mfd *mfd;
191 struct resource *res;
193 pdev = dev->dev.platform_data;
194 mfd = sta2x11_mfd_find(*pdev);
198 res = platform_get_resource(dev, IORESOURCE_MEM, 0);
202 if (!request_mem_region(res->start, resource_size(res),
206 mfd->sctl_regs = ioremap(res->start, resource_size(res));
207 if (!mfd->sctl_regs) {
208 release_mem_region(res->start, resource_size(res));
211 sctl_regset.base = mfd->sctl_regs;
212 sta2x11_sctl_debugfs = debugfs_create_regset32("sta2x11-sctl",
218 static int sta2x11_apbreg_probe(struct platform_device *dev)
220 struct pci_dev **pdev;
221 struct sta2x11_mfd *mfd;
222 struct resource *res;
224 pdev = dev->dev.platform_data;
225 dev_dbg(&dev->dev, "%s: pdata is %p\n", __func__, pdev);
226 dev_dbg(&dev->dev, "%s: *pdata is %p\n", __func__, *pdev);
228 mfd = sta2x11_mfd_find(*pdev);
232 res = platform_get_resource(dev, IORESOURCE_MEM, 0);
236 if (!request_mem_region(res->start, resource_size(res),
240 mfd->apbreg_regs = ioremap(res->start, resource_size(res));
241 if (!mfd->apbreg_regs) {
242 release_mem_region(res->start, resource_size(res));
245 dev_dbg(&dev->dev, "%s: regbase %p\n", __func__, mfd->apbreg_regs);
247 apbreg_regset.base = mfd->apbreg_regs;
248 sta2x11_apbreg_debugfs = debugfs_create_regset32("sta2x11-apbreg",
250 NULL, &apbreg_regset);
254 /* The two platform drivers */
255 static struct platform_driver sta2x11_sctl_platform_driver = {
257 .name = "sta2x11-sctl",
258 .owner = THIS_MODULE,
260 .probe = sta2x11_sctl_probe,
263 static int __init sta2x11_sctl_init(void)
265 pr_info("%s\n", __func__);
266 return platform_driver_register(&sta2x11_sctl_platform_driver);
269 static struct platform_driver sta2x11_platform_driver = {
271 .name = "sta2x11-apbreg",
272 .owner = THIS_MODULE,
274 .probe = sta2x11_apbreg_probe,
277 static int __init sta2x11_apbreg_init(void)
279 pr_info("%s\n", __func__);
280 return platform_driver_register(&sta2x11_platform_driver);
284 * What follows is the PCI device that hosts the above two pdevs.
285 * Each logic block is 4kB and they are all consecutive: we use this info.
302 #define CELL_4K(_name, _cell) { \
304 .start = _cell * 4096, .end = _cell * 4096 + 4095, \
305 .flags = IORESOURCE_MEM, \
308 static const struct resource gpio_resources[] = {
310 .name = "sta2x11_gpio", /* 4 consecutive cells, 1 driver */
312 .end = (4 * 4096) - 1,
313 .flags = IORESOURCE_MEM,
316 static const struct resource sctl_resources[] = {
317 CELL_4K("sta2x11-sctl", STA2X11_SCTL),
319 static const struct resource scr_resources[] = {
320 CELL_4K("sta2x11-scr", STA2X11_SCR),
322 static const struct resource time_resources[] = {
323 CELL_4K("sta2x11-time", STA2X11_TIME),
326 static const struct resource apbreg_resources[] = {
327 CELL_4K("sta2x11-apbreg", STA2X11_APBREG),
330 #define DEV(_name, _r) \
331 { .name = _name, .num_resources = ARRAY_SIZE(_r), .resources = _r, }
333 static struct mfd_cell sta2x11_mfd_bar0[] = {
334 DEV("sta2x11-gpio", gpio_resources), /* offset 0: we add pdata later */
335 DEV("sta2x11-sctl", sctl_resources),
336 DEV("sta2x11-scr", scr_resources),
337 DEV("sta2x11-time", time_resources),
340 static struct mfd_cell sta2x11_mfd_bar1[] = {
341 DEV("sta2x11-apbreg", apbreg_resources),
344 static int sta2x11_mfd_suspend(struct pci_dev *pdev, pm_message_t state)
346 pci_save_state(pdev);
347 pci_disable_device(pdev);
348 pci_set_power_state(pdev, pci_choose_state(pdev, state));
353 static int sta2x11_mfd_resume(struct pci_dev *pdev)
357 pci_set_power_state(pdev, 0);
358 err = pci_enable_device(pdev);
361 pci_restore_state(pdev);
366 static int sta2x11_mfd_probe(struct pci_dev *pdev,
367 const struct pci_device_id *pci_id)
370 struct sta2x11_gpio_pdata *gpio_data;
372 dev_info(&pdev->dev, "%s\n", __func__);
374 err = pci_enable_device(pdev);
376 dev_err(&pdev->dev, "Can't enable device.\n");
380 err = pci_enable_msi(pdev);
382 dev_info(&pdev->dev, "Enable msi failed\n");
384 /* Read gpio config data as pci device's platform data */
385 gpio_data = dev_get_platdata(&pdev->dev);
387 dev_warn(&pdev->dev, "no gpio configuration\n");
389 dev_dbg(&pdev->dev, "%s, gpio_data = %p (%p)\n", __func__,
390 gpio_data, &gpio_data);
391 dev_dbg(&pdev->dev, "%s, pdev = %p (%p)\n", __func__,
394 /* platform data is the pci device for all of them */
395 for (i = 0; i < ARRAY_SIZE(sta2x11_mfd_bar0); i++) {
396 sta2x11_mfd_bar0[i].pdata_size = sizeof(pdev);
397 sta2x11_mfd_bar0[i].platform_data = &pdev;
399 sta2x11_mfd_bar1[0].pdata_size = sizeof(pdev);
400 sta2x11_mfd_bar1[0].platform_data = &pdev;
402 /* Record this pdev before mfd_add_devices: their probe looks for it */
403 sta2x11_mfd_add(pdev, GFP_ATOMIC);
406 err = mfd_add_devices(&pdev->dev, -1,
408 ARRAY_SIZE(sta2x11_mfd_bar0),
412 dev_err(&pdev->dev, "mfd_add_devices[0] failed: %d\n", err);
416 err = mfd_add_devices(&pdev->dev, -1,
418 ARRAY_SIZE(sta2x11_mfd_bar1),
422 dev_err(&pdev->dev, "mfd_add_devices[1] failed: %d\n", err);
429 mfd_remove_devices(&pdev->dev);
430 pci_disable_device(pdev);
431 pci_disable_msi(pdev);
435 static DEFINE_PCI_DEVICE_TABLE(sta2x11_mfd_tbl) = {
436 {PCI_DEVICE(PCI_VENDOR_ID_STMICRO, PCI_DEVICE_ID_STMICRO_GPIO)},
440 static struct pci_driver sta2x11_mfd_driver = {
441 .name = "sta2x11-mfd",
442 .id_table = sta2x11_mfd_tbl,
443 .probe = sta2x11_mfd_probe,
444 .suspend = sta2x11_mfd_suspend,
445 .resume = sta2x11_mfd_resume,
448 static int __init sta2x11_mfd_init(void)
450 pr_info("%s\n", __func__);
451 return pci_register_driver(&sta2x11_mfd_driver);
455 * All of this must be ready before "normal" devices like MMCI appear.
456 * But MFD (the pci device) can't be too early. The following choice
457 * prepares platform drivers very early and probe the PCI device later,
458 * but before other PCI devices.
460 subsys_initcall(sta2x11_apbreg_init);
461 subsys_initcall(sta2x11_sctl_init);
462 rootfs_initcall(sta2x11_mfd_init);
464 MODULE_LICENSE("GPL v2");
465 MODULE_AUTHOR("Wind River");
466 MODULE_DESCRIPTION("STA2x11 mfd for GPIO, SCTL and APBREG");
467 MODULE_DEVICE_TABLE(pci, sta2x11_mfd_tbl);