blob: 9e01b84aa8bc7fa905654231aa10175b6518f270 [file] [log] [blame]
Alessandro Rubini35bdd292012-04-12 10:48:44 +02001/*
2 * Copyright (c) 2009-2011 Wind River Systems, Inc.
3 * Copyright (c) 2011 ST Microelectronics (Alessandro Rubini)
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 version 2 as
7 * published by the Free Software Foundation.
8 *
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.
13 *
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
17 *
18 */
19
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>
27#include <linux/io.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>
35
36#include <asm/sta2x11.h>
37
38/* This describes STA2X11 MFD chip for us, we may have several */
39struct sta2x11_mfd {
40 struct sta2x11_instance *instance;
41 spinlock_t lock;
42 struct list_head list;
Davide Ciminaghi1950c712012-11-09 15:19:52 +010043 void __iomem *regs[sta2x11_n_mfd_plat_devs];
Alessandro Rubini35bdd292012-04-12 10:48:44 +020044};
45
46static LIST_HEAD(sta2x11_mfd_list);
47
48/* Three functions to act on the list */
49static struct sta2x11_mfd *sta2x11_mfd_find(struct pci_dev *pdev)
50{
51 struct sta2x11_instance *instance;
52 struct sta2x11_mfd *mfd;
53
54 if (!pdev && !list_empty(&sta2x11_mfd_list)) {
55 pr_warning("%s: Unspecified device, "
56 "using first instance\n", __func__);
57 return list_entry(sta2x11_mfd_list.next,
58 struct sta2x11_mfd, list);
59 }
60
61 instance = sta2x11_get_instance(pdev);
62 if (!instance)
63 return NULL;
64 list_for_each_entry(mfd, &sta2x11_mfd_list, list) {
65 if (mfd->instance == instance)
66 return mfd;
67 }
68 return NULL;
69}
70
71static int __devinit sta2x11_mfd_add(struct pci_dev *pdev, gfp_t flags)
72{
73 struct sta2x11_mfd *mfd = sta2x11_mfd_find(pdev);
74 struct sta2x11_instance *instance;
75
76 if (mfd)
77 return -EBUSY;
78 instance = sta2x11_get_instance(pdev);
79 if (!instance)
80 return -EINVAL;
81 mfd = kzalloc(sizeof(*mfd), flags);
82 if (!mfd)
83 return -ENOMEM;
84 INIT_LIST_HEAD(&mfd->list);
85 spin_lock_init(&mfd->lock);
86 mfd->instance = instance;
87 list_add(&mfd->list, &sta2x11_mfd_list);
88 return 0;
89}
90
91static int __devexit mfd_remove(struct pci_dev *pdev)
92{
93 struct sta2x11_mfd *mfd = sta2x11_mfd_find(pdev);
94
95 if (!mfd)
96 return -ENODEV;
97 list_del(&mfd->list);
98 kfree(mfd);
99 return 0;
100}
101
Davide Ciminaghi1950c712012-11-09 15:19:52 +0100102/* This function is exported and is not expected to fail */
103u32 __sta2x11_mfd_mask(struct pci_dev *pdev, u32 reg, u32 mask, u32 val,
104 enum sta2x11_mfd_plat_dev index)
Alessandro Rubini35bdd292012-04-12 10:48:44 +0200105{
106 struct sta2x11_mfd *mfd = sta2x11_mfd_find(pdev);
107 u32 r;
108 unsigned long flags;
Davide Ciminaghi1950c712012-11-09 15:19:52 +0100109 void __iomem *regs = mfd->regs[index];
Alessandro Rubini35bdd292012-04-12 10:48:44 +0200110
111 if (!mfd) {
112 dev_warn(&pdev->dev, ": can't access sctl regs\n");
113 return 0;
114 }
Davide Ciminaghi1950c712012-11-09 15:19:52 +0100115 if (!regs) {
Alessandro Rubini35bdd292012-04-12 10:48:44 +0200116 dev_warn(&pdev->dev, ": system ctl not initialized\n");
117 return 0;
118 }
119 spin_lock_irqsave(&mfd->lock, flags);
Davide Ciminaghi1950c712012-11-09 15:19:52 +0100120 r = readl(regs + reg);
Alessandro Rubini35bdd292012-04-12 10:48:44 +0200121 r &= ~mask;
122 r |= val;
123 if (mask)
Davide Ciminaghi1950c712012-11-09 15:19:52 +0100124 writel(r, regs + reg);
Alessandro Rubini35bdd292012-04-12 10:48:44 +0200125 spin_unlock_irqrestore(&mfd->lock, flags);
126 return r;
127}
Davide Ciminaghi1950c712012-11-09 15:19:52 +0100128EXPORT_SYMBOL(__sta2x11_mfd_mask);
Alessandro Rubini35bdd292012-04-12 10:48:44 +0200129
130/* Two debugfs files, for our registers (FIXME: one instance only) */
131#define REG(regname) {.name = #regname, .offset = SCTL_ ## regname}
132static struct debugfs_reg32 sta2x11_sctl_regs[] = {
133 REG(SCCTL), REG(ARMCFG), REG(SCPLLCTL), REG(SCPLLFCTRL),
134 REG(SCRESFRACT), REG(SCRESCTRL1), REG(SCRESXTRL2), REG(SCPEREN0),
135 REG(SCPEREN1), REG(SCPEREN2), REG(SCGRST), REG(SCPCIPMCR1),
136 REG(SCPCIPMCR2), REG(SCPCIPMSR1), REG(SCPCIPMSR2), REG(SCPCIPMSR3),
137 REG(SCINTREN), REG(SCRISR), REG(SCCLKSTAT0), REG(SCCLKSTAT1),
138 REG(SCCLKSTAT2), REG(SCRSTSTA),
139};
140#undef REG
141
142static struct debugfs_regset32 sctl_regset = {
143 .regs = sta2x11_sctl_regs,
144 .nregs = ARRAY_SIZE(sta2x11_sctl_regs),
145};
146
147#define REG(regname) {.name = #regname, .offset = regname}
148static struct debugfs_reg32 sta2x11_apbreg_regs[] = {
149 REG(APBREG_BSR), REG(APBREG_PAER), REG(APBREG_PWAC), REG(APBREG_PRAC),
150 REG(APBREG_PCG), REG(APBREG_PUR), REG(APBREG_EMU_PCG),
151};
152#undef REG
153
154static struct debugfs_regset32 apbreg_regset = {
155 .regs = sta2x11_apbreg_regs,
156 .nregs = ARRAY_SIZE(sta2x11_apbreg_regs),
157};
158
Davide Ciminaghi1950c712012-11-09 15:19:52 +0100159#define REG(regname) {.name = #regname, .offset = regname}
160static struct debugfs_reg32 sta2x11_apb_soc_regs_regs[] = {
161 REG(PCIE_EP1_FUNC3_0_INTR_REG), REG(PCIE_EP1_FUNC7_4_INTR_REG),
162 REG(PCIE_EP2_FUNC3_0_INTR_REG), REG(PCIE_EP2_FUNC7_4_INTR_REG),
163 REG(PCIE_EP3_FUNC3_0_INTR_REG), REG(PCIE_EP3_FUNC7_4_INTR_REG),
164 REG(PCIE_EP4_FUNC3_0_INTR_REG), REG(PCIE_EP4_FUNC7_4_INTR_REG),
165 REG(PCIE_INTR_ENABLE0_REG), REG(PCIE_INTR_ENABLE1_REG),
166 REG(PCIE_EP1_FUNC_TC_REG), REG(PCIE_EP2_FUNC_TC_REG),
167 REG(PCIE_EP3_FUNC_TC_REG), REG(PCIE_EP4_FUNC_TC_REG),
168 REG(PCIE_EP1_FUNC_F_REG), REG(PCIE_EP2_FUNC_F_REG),
169 REG(PCIE_EP3_FUNC_F_REG), REG(PCIE_EP4_FUNC_F_REG),
170 REG(PCIE_PAB_AMBA_SW_RST_REG), REG(PCIE_PM_STATUS_0_PORT_0_4),
171 REG(PCIE_PM_STATUS_7_0_EP1), REG(PCIE_PM_STATUS_7_0_EP2),
172 REG(PCIE_PM_STATUS_7_0_EP3), REG(PCIE_PM_STATUS_7_0_EP4),
173 REG(PCIE_DEV_ID_0_EP1_REG), REG(PCIE_CC_REV_ID_0_EP1_REG),
174 REG(PCIE_DEV_ID_1_EP1_REG), REG(PCIE_CC_REV_ID_1_EP1_REG),
175 REG(PCIE_DEV_ID_2_EP1_REG), REG(PCIE_CC_REV_ID_2_EP1_REG),
176 REG(PCIE_DEV_ID_3_EP1_REG), REG(PCIE_CC_REV_ID_3_EP1_REG),
177 REG(PCIE_DEV_ID_4_EP1_REG), REG(PCIE_CC_REV_ID_4_EP1_REG),
178 REG(PCIE_DEV_ID_5_EP1_REG), REG(PCIE_CC_REV_ID_5_EP1_REG),
179 REG(PCIE_DEV_ID_6_EP1_REG), REG(PCIE_CC_REV_ID_6_EP1_REG),
180 REG(PCIE_DEV_ID_7_EP1_REG), REG(PCIE_CC_REV_ID_7_EP1_REG),
181 REG(PCIE_DEV_ID_0_EP2_REG), REG(PCIE_CC_REV_ID_0_EP2_REG),
182 REG(PCIE_DEV_ID_1_EP2_REG), REG(PCIE_CC_REV_ID_1_EP2_REG),
183 REG(PCIE_DEV_ID_2_EP2_REG), REG(PCIE_CC_REV_ID_2_EP2_REG),
184 REG(PCIE_DEV_ID_3_EP2_REG), REG(PCIE_CC_REV_ID_3_EP2_REG),
185 REG(PCIE_DEV_ID_4_EP2_REG), REG(PCIE_CC_REV_ID_4_EP2_REG),
186 REG(PCIE_DEV_ID_5_EP2_REG), REG(PCIE_CC_REV_ID_5_EP2_REG),
187 REG(PCIE_DEV_ID_6_EP2_REG), REG(PCIE_CC_REV_ID_6_EP2_REG),
188 REG(PCIE_DEV_ID_7_EP2_REG), REG(PCIE_CC_REV_ID_7_EP2_REG),
189 REG(PCIE_DEV_ID_0_EP3_REG), REG(PCIE_CC_REV_ID_0_EP3_REG),
190 REG(PCIE_DEV_ID_1_EP3_REG), REG(PCIE_CC_REV_ID_1_EP3_REG),
191 REG(PCIE_DEV_ID_2_EP3_REG), REG(PCIE_CC_REV_ID_2_EP3_REG),
192 REG(PCIE_DEV_ID_3_EP3_REG), REG(PCIE_CC_REV_ID_3_EP3_REG),
193 REG(PCIE_DEV_ID_4_EP3_REG), REG(PCIE_CC_REV_ID_4_EP3_REG),
194 REG(PCIE_DEV_ID_5_EP3_REG), REG(PCIE_CC_REV_ID_5_EP3_REG),
195 REG(PCIE_DEV_ID_6_EP3_REG), REG(PCIE_CC_REV_ID_6_EP3_REG),
196 REG(PCIE_DEV_ID_7_EP3_REG), REG(PCIE_CC_REV_ID_7_EP3_REG),
197 REG(PCIE_DEV_ID_0_EP4_REG), REG(PCIE_CC_REV_ID_0_EP4_REG),
198 REG(PCIE_DEV_ID_1_EP4_REG), REG(PCIE_CC_REV_ID_1_EP4_REG),
199 REG(PCIE_DEV_ID_2_EP4_REG), REG(PCIE_CC_REV_ID_2_EP4_REG),
200 REG(PCIE_DEV_ID_3_EP4_REG), REG(PCIE_CC_REV_ID_3_EP4_REG),
201 REG(PCIE_DEV_ID_4_EP4_REG), REG(PCIE_CC_REV_ID_4_EP4_REG),
202 REG(PCIE_DEV_ID_5_EP4_REG), REG(PCIE_CC_REV_ID_5_EP4_REG),
203 REG(PCIE_DEV_ID_6_EP4_REG), REG(PCIE_CC_REV_ID_6_EP4_REG),
204 REG(PCIE_DEV_ID_7_EP4_REG), REG(PCIE_CC_REV_ID_7_EP4_REG),
205 REG(PCIE_SUBSYS_VEN_ID_REG), REG(PCIE_COMMON_CLOCK_CONFIG_0_4_0),
206 REG(PCIE_MIPHYP_SSC_EN_REG), REG(PCIE_MIPHYP_ADDR_REG),
207 REG(PCIE_L1_ASPM_READY_REG), REG(PCIE_EXT_CFG_RDY_REG),
208 REG(PCIE_SoC_INT_ROUTER_STATUS0_REG),
209 REG(PCIE_SoC_INT_ROUTER_STATUS1_REG),
210 REG(PCIE_SoC_INT_ROUTER_STATUS2_REG),
211 REG(PCIE_SoC_INT_ROUTER_STATUS3_REG),
212 REG(DMA_IP_CTRL_REG), REG(DISP_BRIDGE_PU_PD_CTRL_REG),
213 REG(VIP_PU_PD_CTRL_REG), REG(USB_MLB_PU_PD_CTRL_REG),
214 REG(SDIO_PU_PD_MISCFUNC_CTRL_REG1), REG(SDIO_PU_PD_MISCFUNC_CTRL_REG2),
215 REG(UART_PU_PD_CTRL_REG), REG(ARM_Lock), REG(SYS_IO_CHAR_REG1),
216 REG(SYS_IO_CHAR_REG2), REG(SATA_CORE_ID_REG), REG(SATA_CTRL_REG),
217 REG(I2C_HSFIX_MISC_REG), REG(SPARE2_RESERVED), REG(SPARE3_RESERVED),
218 REG(MASTER_LOCK_REG), REG(SYSTEM_CONFIG_STATUS_REG),
219 REG(MSP_CLK_CTRL_REG), REG(COMPENSATION_REG1), REG(COMPENSATION_REG2),
220 REG(COMPENSATION_REG3), REG(TEST_CTL_REG),
221};
222#undef REG
Alessandro Rubini35bdd292012-04-12 10:48:44 +0200223
Davide Ciminaghi1950c712012-11-09 15:19:52 +0100224static struct debugfs_regset32 apb_soc_regs_regset = {
225 .regs = sta2x11_apb_soc_regs_regs,
226 .nregs = ARRAY_SIZE(sta2x11_apb_soc_regs_regs),
227};
228
229
230static struct dentry *sta2x11_mfd_debugfs[sta2x11_n_mfd_plat_devs];
231
232static struct debugfs_regset32 *sta2x11_mfd_regset[sta2x11_n_mfd_plat_devs] = {
233 [sta2x11_sctl] = &sctl_regset,
234 [sta2x11_apbreg] = &apbreg_regset,
235 [sta2x11_apb_soc_regs] = &apb_soc_regs_regset,
236};
237
238static const char *sta2x11_mfd_names[sta2x11_n_mfd_plat_devs] = {
239 [sta2x11_sctl] = "sta2x11-sctl",
240 [sta2x11_apbreg] = "sta2x11-apbreg",
241 [sta2x11_apb_soc_regs] = "sta2x11-apb-soc-regs",
242};
243
244/* Probe for the three platform devices */
245
246static int sta2x11_mfd_platform_probe(struct platform_device *dev,
247 enum sta2x11_mfd_plat_dev index)
Alessandro Rubini35bdd292012-04-12 10:48:44 +0200248{
249 struct pci_dev **pdev;
250 struct sta2x11_mfd *mfd;
251 struct resource *res;
Davide Ciminaghi1950c712012-11-09 15:19:52 +0100252 const char *name = sta2x11_mfd_names[index];
253 struct debugfs_regset32 *regset = sta2x11_mfd_regset[index];
Alessandro Rubini35bdd292012-04-12 10:48:44 +0200254
255 pdev = dev->dev.platform_data;
256 mfd = sta2x11_mfd_find(*pdev);
257 if (!mfd)
258 return -ENODEV;
259
260 res = platform_get_resource(dev, IORESOURCE_MEM, 0);
261 if (!res)
262 return -ENOMEM;
263
Davide Ciminaghi1950c712012-11-09 15:19:52 +0100264 if (!request_mem_region(res->start, resource_size(res), name))
Alessandro Rubini35bdd292012-04-12 10:48:44 +0200265 return -EBUSY;
266
Davide Ciminaghi1950c712012-11-09 15:19:52 +0100267 mfd->regs[index] = ioremap(res->start, resource_size(res));
268 if (!mfd->regs[index]) {
Alessandro Rubini35bdd292012-04-12 10:48:44 +0200269 release_mem_region(res->start, resource_size(res));
270 return -ENOMEM;
271 }
Davide Ciminaghi1950c712012-11-09 15:19:52 +0100272 regset->base = mfd->regs[index];
273 sta2x11_mfd_debugfs[index] = debugfs_create_regset32(name,
274 S_IFREG | S_IRUGO,
275 NULL, regset);
Alessandro Rubini35bdd292012-04-12 10:48:44 +0200276 return 0;
277}
278
Davide Ciminaghi1950c712012-11-09 15:19:52 +0100279static int sta2x11_sctl_probe(struct platform_device *dev)
280{
281 return sta2x11_mfd_platform_probe(dev, sta2x11_sctl);
282}
283
Alessandro Rubini35bdd292012-04-12 10:48:44 +0200284static int sta2x11_apbreg_probe(struct platform_device *dev)
285{
Davide Ciminaghi1950c712012-11-09 15:19:52 +0100286 return sta2x11_mfd_platform_probe(dev, sta2x11_apbreg);
Alessandro Rubini35bdd292012-04-12 10:48:44 +0200287}
288
Davide Ciminaghi1950c712012-11-09 15:19:52 +0100289static int sta2x11_apb_soc_regs_probe(struct platform_device *dev)
290{
291 return sta2x11_mfd_platform_probe(dev, sta2x11_apb_soc_regs);
292}
293
294/* The three platform drivers */
Alessandro Rubini35bdd292012-04-12 10:48:44 +0200295static struct platform_driver sta2x11_sctl_platform_driver = {
296 .driver = {
297 .name = "sta2x11-sctl",
298 .owner = THIS_MODULE,
299 },
300 .probe = sta2x11_sctl_probe,
301};
302
303static int __init sta2x11_sctl_init(void)
304{
305 pr_info("%s\n", __func__);
306 return platform_driver_register(&sta2x11_sctl_platform_driver);
307}
308
309static struct platform_driver sta2x11_platform_driver = {
310 .driver = {
311 .name = "sta2x11-apbreg",
312 .owner = THIS_MODULE,
313 },
314 .probe = sta2x11_apbreg_probe,
315};
316
317static int __init sta2x11_apbreg_init(void)
318{
319 pr_info("%s\n", __func__);
320 return platform_driver_register(&sta2x11_platform_driver);
321}
322
Davide Ciminaghi1950c712012-11-09 15:19:52 +0100323static struct platform_driver sta2x11_apb_soc_regs_platform_driver = {
324 .driver = {
325 .name = "sta2x11-apb-soc-regs",
326 .owner = THIS_MODULE,
327 },
328 .probe = sta2x11_apb_soc_regs_probe,
329};
330
331static int __init sta2x11_apb_soc_regs_init(void)
332{
333 pr_info("%s\n", __func__);
334 return platform_driver_register(&sta2x11_apb_soc_regs_platform_driver);
335}
336
Alessandro Rubini35bdd292012-04-12 10:48:44 +0200337/*
Davide Ciminaghi1950c712012-11-09 15:19:52 +0100338 * What follows are the PCI devices that host the above pdevs.
Alessandro Rubini35bdd292012-04-12 10:48:44 +0200339 * Each logic block is 4kB and they are all consecutive: we use this info.
340 */
341
Davide Ciminaghi1950c712012-11-09 15:19:52 +0100342/* Mfd 0 device */
343
344/* Mfd 0, Bar 0 */
345enum mfd0_bar0_cells {
Alessandro Rubini35bdd292012-04-12 10:48:44 +0200346 STA2X11_GPIO_0 = 0,
347 STA2X11_GPIO_1,
348 STA2X11_GPIO_2,
349 STA2X11_GPIO_3,
350 STA2X11_SCTL,
351 STA2X11_SCR,
352 STA2X11_TIME,
353};
Davide Ciminaghi1950c712012-11-09 15:19:52 +0100354/* Mfd 0 , Bar 1 */
355enum mfd0_bar1_cells {
Alessandro Rubini35bdd292012-04-12 10:48:44 +0200356 STA2X11_APBREG = 0,
357};
358#define CELL_4K(_name, _cell) { \
359 .name = _name, \
360 .start = _cell * 4096, .end = _cell * 4096 + 4095, \
361 .flags = IORESOURCE_MEM, \
362 }
363
364static const __devinitconst struct resource gpio_resources[] = {
365 {
366 .name = "sta2x11_gpio", /* 4 consecutive cells, 1 driver */
367 .start = 0,
368 .end = (4 * 4096) - 1,
369 .flags = IORESOURCE_MEM,
370 }
371};
372static const __devinitconst struct resource sctl_resources[] = {
373 CELL_4K("sta2x11-sctl", STA2X11_SCTL),
374};
375static const __devinitconst struct resource scr_resources[] = {
376 CELL_4K("sta2x11-scr", STA2X11_SCR),
377};
378static const __devinitconst struct resource time_resources[] = {
379 CELL_4K("sta2x11-time", STA2X11_TIME),
380};
381
382static const __devinitconst struct resource apbreg_resources[] = {
383 CELL_4K("sta2x11-apbreg", STA2X11_APBREG),
384};
385
386#define DEV(_name, _r) \
387 { .name = _name, .num_resources = ARRAY_SIZE(_r), .resources = _r, }
388
Davide Ciminaghi1950c712012-11-09 15:19:52 +0100389static __devinitdata struct mfd_cell sta2x11_mfd0_bar0[] = {
Alessandro Rubini35bdd292012-04-12 10:48:44 +0200390 DEV("sta2x11-gpio", gpio_resources), /* offset 0: we add pdata later */
391 DEV("sta2x11-sctl", sctl_resources),
392 DEV("sta2x11-scr", scr_resources),
393 DEV("sta2x11-time", time_resources),
394};
395
Davide Ciminaghi1950c712012-11-09 15:19:52 +0100396static __devinitdata struct mfd_cell sta2x11_mfd0_bar1[] = {
Alessandro Rubini35bdd292012-04-12 10:48:44 +0200397 DEV("sta2x11-apbreg", apbreg_resources),
398};
399
Davide Ciminaghi1950c712012-11-09 15:19:52 +0100400/* Mfd 1 devices */
401
402/* Mfd 1, Bar 0 */
403enum mfd1_bar0_cells {
404 STA2X11_VIC = 0,
405};
406
407/* Mfd 1, Bar 1 */
408enum mfd1_bar1_cells {
409 STA2X11_APB_SOC_REGS = 0,
410};
411
412static const __devinitconst struct resource vic_resources[] = {
413 CELL_4K("sta2x11-vic", STA2X11_VIC),
414};
415
416static const __devinitconst struct resource apb_soc_regs_resources[] = {
417 CELL_4K("sta2x11-apb-soc-regs", STA2X11_APB_SOC_REGS),
418};
419
420static __devinitdata struct mfd_cell sta2x11_mfd1_bar0[] = {
421 DEV("sta2x11-vic", vic_resources),
422};
423
424static __devinitdata struct mfd_cell sta2x11_mfd1_bar1[] = {
425 DEV("sta2x11-apb-soc-regs", apb_soc_regs_resources),
426};
427
428
Alessandro Rubini35bdd292012-04-12 10:48:44 +0200429static int sta2x11_mfd_suspend(struct pci_dev *pdev, pm_message_t state)
430{
431 pci_save_state(pdev);
432 pci_disable_device(pdev);
433 pci_set_power_state(pdev, pci_choose_state(pdev, state));
434
435 return 0;
436}
437
438static int sta2x11_mfd_resume(struct pci_dev *pdev)
439{
440 int err;
441
442 pci_set_power_state(pdev, 0);
443 err = pci_enable_device(pdev);
444 if (err)
445 return err;
446 pci_restore_state(pdev);
447
448 return 0;
449}
450
Davide Ciminaghi1950c712012-11-09 15:19:52 +0100451struct sta2x11_mfd_bar_setup_data {
452 struct mfd_cell *cells;
453 int ncells;
454};
455
456struct sta2x11_mfd_setup_data {
457 struct sta2x11_mfd_bar_setup_data bars[2];
458};
459
460#define STA2X11_MFD0 0
461#define STA2X11_MFD1 1
462
463static struct sta2x11_mfd_setup_data mfd_setup_data[] = {
464 /* Mfd 0: gpio, sctl, scr, timers / apbregs */
465 [STA2X11_MFD0] = {
466 .bars = {
467 [0] = {
468 .cells = sta2x11_mfd0_bar0,
469 .ncells = ARRAY_SIZE(sta2x11_mfd0_bar0),
470 },
471 [1] = {
472 .cells = sta2x11_mfd0_bar1,
473 .ncells = ARRAY_SIZE(sta2x11_mfd0_bar1),
474 },
475 },
476 },
477 /* Mfd 1: vic / apb-soc-regs */
478 [STA2X11_MFD1] = {
479 .bars = {
480 [0] = {
481 .cells = sta2x11_mfd1_bar0,
482 .ncells = ARRAY_SIZE(sta2x11_mfd1_bar0),
483 },
484 [1] = {
485 .cells = sta2x11_mfd1_bar1,
486 .ncells = ARRAY_SIZE(sta2x11_mfd1_bar1),
487 },
488 },
489 },
490};
491
492static void __devinit sta2x11_mfd_setup(struct pci_dev *pdev,
493 struct sta2x11_mfd_setup_data *sd)
494{
495 int i, j;
496 for (i = 0; i < ARRAY_SIZE(sd->bars); i++)
497 for (j = 0; j < sd->bars[i].ncells; j++) {
498 sd->bars[i].cells[j].pdata_size = sizeof(pdev);
499 sd->bars[i].cells[j].platform_data = &pdev;
500 }
501}
502
Alessandro Rubini35bdd292012-04-12 10:48:44 +0200503static int __devinit sta2x11_mfd_probe(struct pci_dev *pdev,
504 const struct pci_device_id *pci_id)
505{
506 int err, i;
Davide Ciminaghi1950c712012-11-09 15:19:52 +0100507 struct sta2x11_mfd_setup_data *setup_data;
Alessandro Rubini35bdd292012-04-12 10:48:44 +0200508 struct sta2x11_gpio_pdata *gpio_data;
509
510 dev_info(&pdev->dev, "%s\n", __func__);
511
512 err = pci_enable_device(pdev);
513 if (err) {
514 dev_err(&pdev->dev, "Can't enable device.\n");
515 return err;
516 }
517
518 err = pci_enable_msi(pdev);
519 if (err)
520 dev_info(&pdev->dev, "Enable msi failed\n");
521
Davide Ciminaghi1950c712012-11-09 15:19:52 +0100522 setup_data = pci_id->device == PCI_DEVICE_ID_STMICRO_GPIO ?
523 &mfd_setup_data[STA2X11_MFD0] :
524 &mfd_setup_data[STA2X11_MFD1];
525
Alessandro Rubini35bdd292012-04-12 10:48:44 +0200526 /* Read gpio config data as pci device's platform data */
527 gpio_data = dev_get_platdata(&pdev->dev);
528 if (!gpio_data)
529 dev_warn(&pdev->dev, "no gpio configuration\n");
530
531 dev_dbg(&pdev->dev, "%s, gpio_data = %p (%p)\n", __func__,
532 gpio_data, &gpio_data);
533 dev_dbg(&pdev->dev, "%s, pdev = %p (%p)\n", __func__,
534 pdev, &pdev);
535
536 /* platform data is the pci device for all of them */
Davide Ciminaghi1950c712012-11-09 15:19:52 +0100537 sta2x11_mfd_setup(pdev, setup_data);
Alessandro Rubini35bdd292012-04-12 10:48:44 +0200538
539 /* Record this pdev before mfd_add_devices: their probe looks for it */
540 sta2x11_mfd_add(pdev, GFP_ATOMIC);
541
Davide Ciminaghi1950c712012-11-09 15:19:52 +0100542 /* Just 2 bars for all mfd's at present */
543 for (i = 0; i < 2; i++) {
544 err = mfd_add_devices(&pdev->dev, -1,
545 setup_data->bars[i].cells,
546 setup_data->bars[i].ncells,
547 &pdev->resource[i],
548 0, NULL);
549 if (err) {
550 dev_err(&pdev->dev,
551 "mfd_add_devices[%d] failed: %d\n", i, err);
552 goto err_disable;
553 }
Alessandro Rubini35bdd292012-04-12 10:48:44 +0200554 }
555
556 return 0;
557
558err_disable:
559 mfd_remove_devices(&pdev->dev);
560 pci_disable_device(pdev);
561 pci_disable_msi(pdev);
562 return err;
563}
564
565static DEFINE_PCI_DEVICE_TABLE(sta2x11_mfd_tbl) = {
566 {PCI_DEVICE(PCI_VENDOR_ID_STMICRO, PCI_DEVICE_ID_STMICRO_GPIO)},
Davide Ciminaghi1950c712012-11-09 15:19:52 +0100567 {PCI_DEVICE(PCI_VENDOR_ID_STMICRO, PCI_DEVICE_ID_STMICRO_VIC)},
Alessandro Rubini35bdd292012-04-12 10:48:44 +0200568 {0,},
569};
570
571static struct pci_driver sta2x11_mfd_driver = {
572 .name = "sta2x11-mfd",
573 .id_table = sta2x11_mfd_tbl,
574 .probe = sta2x11_mfd_probe,
575 .suspend = sta2x11_mfd_suspend,
576 .resume = sta2x11_mfd_resume,
577};
578
579static int __init sta2x11_mfd_init(void)
580{
581 pr_info("%s\n", __func__);
582 return pci_register_driver(&sta2x11_mfd_driver);
583}
584
585/*
586 * All of this must be ready before "normal" devices like MMCI appear.
587 * But MFD (the pci device) can't be too early. The following choice
588 * prepares platform drivers very early and probe the PCI device later,
589 * but before other PCI devices.
590 */
591subsys_initcall(sta2x11_apbreg_init);
592subsys_initcall(sta2x11_sctl_init);
Davide Ciminaghi1950c712012-11-09 15:19:52 +0100593subsys_initcall(sta2x11_apb_soc_regs_init);
Alessandro Rubini35bdd292012-04-12 10:48:44 +0200594rootfs_initcall(sta2x11_mfd_init);
595
596MODULE_LICENSE("GPL v2");
597MODULE_AUTHOR("Wind River");
598MODULE_DESCRIPTION("STA2x11 mfd for GPIO, SCTL and APBREG");
599MODULE_DEVICE_TABLE(pci, sta2x11_mfd_tbl);