blob: da65839f1d86ac5600bc0a8fb41c0dcf8bade070 [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>
Alessandro Rubini35bdd292012-04-12 10:48:44 +020030#include <linux/seq_file.h>
31#include <linux/platform_device.h>
32#include <linux/mfd/core.h>
33#include <linux/mfd/sta2x11-mfd.h>
Davide Ciminaghid94e2552012-11-09 15:19:53 +010034#include <linux/regmap.h>
Alessandro Rubini35bdd292012-04-12 10:48:44 +020035
36#include <asm/sta2x11.h>
37
Davide Ciminaghid94e2552012-11-09 15:19:53 +010038static inline int __reg_within_range(unsigned int r,
39 unsigned int start,
40 unsigned int end)
41{
42 return ((r >= start) && (r <= end));
43}
44
Alessandro Rubini35bdd292012-04-12 10:48:44 +020045/* This describes STA2X11 MFD chip for us, we may have several */
46struct sta2x11_mfd {
47 struct sta2x11_instance *instance;
Davide Ciminaghid94e2552012-11-09 15:19:53 +010048 struct regmap *regmap[sta2x11_n_mfd_plat_devs];
Davide Ciminaghie885ba292012-11-09 15:19:58 +010049 spinlock_t lock[sta2x11_n_mfd_plat_devs];
Alessandro Rubini35bdd292012-04-12 10:48:44 +020050 struct list_head list;
Davide Ciminaghi1950c712012-11-09 15:19:52 +010051 void __iomem *regs[sta2x11_n_mfd_plat_devs];
Alessandro Rubini35bdd292012-04-12 10:48:44 +020052};
53
54static LIST_HEAD(sta2x11_mfd_list);
55
56/* Three functions to act on the list */
57static struct sta2x11_mfd *sta2x11_mfd_find(struct pci_dev *pdev)
58{
59 struct sta2x11_instance *instance;
60 struct sta2x11_mfd *mfd;
61
62 if (!pdev && !list_empty(&sta2x11_mfd_list)) {
63 pr_warning("%s: Unspecified device, "
64 "using first instance\n", __func__);
65 return list_entry(sta2x11_mfd_list.next,
66 struct sta2x11_mfd, list);
67 }
68
69 instance = sta2x11_get_instance(pdev);
70 if (!instance)
71 return NULL;
72 list_for_each_entry(mfd, &sta2x11_mfd_list, list) {
73 if (mfd->instance == instance)
74 return mfd;
75 }
76 return NULL;
77}
78
79static int __devinit sta2x11_mfd_add(struct pci_dev *pdev, gfp_t flags)
80{
Davide Ciminaghie885ba292012-11-09 15:19:58 +010081 int i;
Alessandro Rubini35bdd292012-04-12 10:48:44 +020082 struct sta2x11_mfd *mfd = sta2x11_mfd_find(pdev);
83 struct sta2x11_instance *instance;
84
85 if (mfd)
86 return -EBUSY;
87 instance = sta2x11_get_instance(pdev);
88 if (!instance)
89 return -EINVAL;
90 mfd = kzalloc(sizeof(*mfd), flags);
91 if (!mfd)
92 return -ENOMEM;
93 INIT_LIST_HEAD(&mfd->list);
Davide Ciminaghie885ba292012-11-09 15:19:58 +010094 for (i = 0; i < ARRAY_SIZE(mfd->lock); i++)
95 spin_lock_init(&mfd->lock[i]);
Alessandro Rubini35bdd292012-04-12 10:48:44 +020096 mfd->instance = instance;
97 list_add(&mfd->list, &sta2x11_mfd_list);
98 return 0;
99}
100
101static int __devexit mfd_remove(struct pci_dev *pdev)
102{
103 struct sta2x11_mfd *mfd = sta2x11_mfd_find(pdev);
104
105 if (!mfd)
106 return -ENODEV;
107 list_del(&mfd->list);
108 kfree(mfd);
109 return 0;
110}
111
Davide Ciminaghi1950c712012-11-09 15:19:52 +0100112/* This function is exported and is not expected to fail */
113u32 __sta2x11_mfd_mask(struct pci_dev *pdev, u32 reg, u32 mask, u32 val,
114 enum sta2x11_mfd_plat_dev index)
Alessandro Rubini35bdd292012-04-12 10:48:44 +0200115{
116 struct sta2x11_mfd *mfd = sta2x11_mfd_find(pdev);
117 u32 r;
118 unsigned long flags;
Davide Ciminaghi1950c712012-11-09 15:19:52 +0100119 void __iomem *regs = mfd->regs[index];
Alessandro Rubini35bdd292012-04-12 10:48:44 +0200120
121 if (!mfd) {
122 dev_warn(&pdev->dev, ": can't access sctl regs\n");
123 return 0;
124 }
Davide Ciminaghi1950c712012-11-09 15:19:52 +0100125 if (!regs) {
Alessandro Rubini35bdd292012-04-12 10:48:44 +0200126 dev_warn(&pdev->dev, ": system ctl not initialized\n");
127 return 0;
128 }
Davide Ciminaghie885ba292012-11-09 15:19:58 +0100129 spin_lock_irqsave(&mfd->lock[index], flags);
Davide Ciminaghi1950c712012-11-09 15:19:52 +0100130 r = readl(regs + reg);
Alessandro Rubini35bdd292012-04-12 10:48:44 +0200131 r &= ~mask;
132 r |= val;
133 if (mask)
Davide Ciminaghi1950c712012-11-09 15:19:52 +0100134 writel(r, regs + reg);
Davide Ciminaghie885ba292012-11-09 15:19:58 +0100135 spin_unlock_irqrestore(&mfd->lock[index], flags);
Alessandro Rubini35bdd292012-04-12 10:48:44 +0200136 return r;
137}
Davide Ciminaghi1950c712012-11-09 15:19:52 +0100138EXPORT_SYMBOL(__sta2x11_mfd_mask);
Alessandro Rubini35bdd292012-04-12 10:48:44 +0200139
Davide Ciminaghi29f5b5a2012-11-09 15:19:54 +0100140int sta2x11_mfd_get_regs_data(struct platform_device *dev,
141 enum sta2x11_mfd_plat_dev index,
142 void __iomem **regs,
143 spinlock_t **lock)
144{
145 struct pci_dev *pdev = *(struct pci_dev **)(dev->dev.platform_data);
146 struct sta2x11_mfd *mfd;
147
148 if (!pdev)
149 return -ENODEV;
150 mfd = sta2x11_mfd_find(pdev);
151 if (!mfd)
152 return -ENODEV;
153 if (index >= sta2x11_n_mfd_plat_devs)
154 return -ENODEV;
155 *regs = mfd->regs[index];
156 *lock = &mfd->lock[index];
157 pr_debug("%s %d *regs = %p\n", __func__, __LINE__, *regs);
158 return *regs ? 0 : -ENODEV;
159}
160EXPORT_SYMBOL(sta2x11_mfd_get_regs_data);
161
Davide Ciminaghid94e2552012-11-09 15:19:53 +0100162/*
163 * Special sta2x11-mfd regmap lock/unlock functions
164 */
Alessandro Rubini35bdd292012-04-12 10:48:44 +0200165
Davide Ciminaghid94e2552012-11-09 15:19:53 +0100166static void sta2x11_regmap_lock(void *__lock)
167{
168 spinlock_t *lock = __lock;
169 spin_lock(lock);
170}
Alessandro Rubini35bdd292012-04-12 10:48:44 +0200171
Davide Ciminaghid94e2552012-11-09 15:19:53 +0100172static void sta2x11_regmap_unlock(void *__lock)
173{
174 spinlock_t *lock = __lock;
175 spin_unlock(lock);
176}
Davide Ciminaghi1950c712012-11-09 15:19:52 +0100177
178static const char *sta2x11_mfd_names[sta2x11_n_mfd_plat_devs] = {
Davide Ciminaghib18adaf2012-11-09 15:19:55 +0100179 [sta2x11_sctl] = STA2X11_MFD_SCTL_NAME,
180 [sta2x11_apbreg] = STA2X11_MFD_APBREG_NAME,
181 [sta2x11_apb_soc_regs] = STA2X11_MFD_APB_SOC_REGS_NAME,
Davide Ciminaghi1950c712012-11-09 15:19:52 +0100182};
183
Davide Ciminaghid94e2552012-11-09 15:19:53 +0100184static bool sta2x11_sctl_writeable_reg(struct device *dev, unsigned int reg)
185{
186 return !__reg_within_range(reg, SCTL_SCPCIECSBRST, SCTL_SCRSTSTA);
187}
188
189static struct regmap_config sta2x11_sctl_regmap_config = {
190 .reg_bits = 32,
191 .reg_stride = 4,
192 .val_bits = 32,
193 .lock = sta2x11_regmap_lock,
194 .unlock = sta2x11_regmap_unlock,
195 .max_register = SCTL_SCRSTSTA,
196 .writeable_reg = sta2x11_sctl_writeable_reg,
197};
198
199static bool sta2x11_apbreg_readable_reg(struct device *dev, unsigned int reg)
200{
201 /* Two blocks (CAN and MLB, SARAC) 0x100 bytes apart */
202 if (reg >= APBREG_BSR_SARAC)
203 reg -= APBREG_BSR_SARAC;
204 switch (reg) {
205 case APBREG_BSR:
206 case APBREG_PAER:
207 case APBREG_PWAC:
208 case APBREG_PRAC:
209 case APBREG_PCG:
210 case APBREG_PUR:
211 case APBREG_EMU_PCG:
212 return true;
213 default:
214 return false;
215 }
216}
217
218static bool sta2x11_apbreg_writeable_reg(struct device *dev, unsigned int reg)
219{
220 if (reg >= APBREG_BSR_SARAC)
221 reg -= APBREG_BSR_SARAC;
222 if (!sta2x11_apbreg_readable_reg(dev, reg))
223 return false;
224 return reg != APBREG_PAER;
225}
226
227static struct regmap_config sta2x11_apbreg_regmap_config = {
228 .reg_bits = 32,
229 .reg_stride = 4,
230 .val_bits = 32,
231 .lock = sta2x11_regmap_lock,
232 .unlock = sta2x11_regmap_unlock,
233 .max_register = APBREG_EMU_PCG_SARAC,
234 .readable_reg = sta2x11_apbreg_readable_reg,
235 .writeable_reg = sta2x11_apbreg_writeable_reg,
236};
237
238static bool sta2x11_apb_soc_regs_readable_reg(struct device *dev,
239 unsigned int reg)
240{
241 return reg <= PCIE_SoC_INT_ROUTER_STATUS3_REG ||
242 __reg_within_range(reg, DMA_IP_CTRL_REG, SPARE3_RESERVED) ||
243 __reg_within_range(reg, MASTER_LOCK_REG,
244 SYSTEM_CONFIG_STATUS_REG) ||
245 reg == MSP_CLK_CTRL_REG ||
246 __reg_within_range(reg, COMPENSATION_REG1, TEST_CTL_REG);
247}
248
249static bool sta2x11_apb_soc_regs_writeable_reg(struct device *dev,
250 unsigned int reg)
251{
252 if (!sta2x11_apb_soc_regs_readable_reg(dev, reg))
253 return false;
254 switch (reg) {
255 case PCIE_COMMON_CLOCK_CONFIG_0_4_0:
256 case SYSTEM_CONFIG_STATUS_REG:
257 case COMPENSATION_REG1:
258 case PCIE_SoC_INT_ROUTER_STATUS0_REG...PCIE_SoC_INT_ROUTER_STATUS3_REG:
259 case PCIE_PM_STATUS_0_PORT_0_4...PCIE_PM_STATUS_7_0_EP4:
260 return false;
261 default:
262 return true;
263 }
264}
265
266static struct regmap_config sta2x11_apb_soc_regs_regmap_config = {
267 .reg_bits = 32,
268 .reg_stride = 4,
269 .val_bits = 32,
270 .lock = sta2x11_regmap_lock,
271 .unlock = sta2x11_regmap_unlock,
272 .max_register = TEST_CTL_REG,
273 .readable_reg = sta2x11_apb_soc_regs_readable_reg,
274 .writeable_reg = sta2x11_apb_soc_regs_writeable_reg,
275};
276
277static struct regmap_config *
278sta2x11_mfd_regmap_configs[sta2x11_n_mfd_plat_devs] = {
279 [sta2x11_sctl] = &sta2x11_sctl_regmap_config,
280 [sta2x11_apbreg] = &sta2x11_apbreg_regmap_config,
281 [sta2x11_apb_soc_regs] = &sta2x11_apb_soc_regs_regmap_config,
282};
283
Davide Ciminaghi1950c712012-11-09 15:19:52 +0100284/* Probe for the three platform devices */
285
286static int sta2x11_mfd_platform_probe(struct platform_device *dev,
287 enum sta2x11_mfd_plat_dev index)
Alessandro Rubini35bdd292012-04-12 10:48:44 +0200288{
289 struct pci_dev **pdev;
290 struct sta2x11_mfd *mfd;
291 struct resource *res;
Davide Ciminaghi1950c712012-11-09 15:19:52 +0100292 const char *name = sta2x11_mfd_names[index];
Davide Ciminaghid94e2552012-11-09 15:19:53 +0100293 struct regmap_config *regmap_config = sta2x11_mfd_regmap_configs[index];
Alessandro Rubini35bdd292012-04-12 10:48:44 +0200294
295 pdev = dev->dev.platform_data;
296 mfd = sta2x11_mfd_find(*pdev);
297 if (!mfd)
298 return -ENODEV;
Davide Ciminaghid94e2552012-11-09 15:19:53 +0100299 if (!regmap_config)
300 return -ENODEV;
Alessandro Rubini35bdd292012-04-12 10:48:44 +0200301
302 res = platform_get_resource(dev, IORESOURCE_MEM, 0);
303 if (!res)
304 return -ENOMEM;
305
Davide Ciminaghi1950c712012-11-09 15:19:52 +0100306 if (!request_mem_region(res->start, resource_size(res), name))
Alessandro Rubini35bdd292012-04-12 10:48:44 +0200307 return -EBUSY;
308
Davide Ciminaghi1950c712012-11-09 15:19:52 +0100309 mfd->regs[index] = ioremap(res->start, resource_size(res));
310 if (!mfd->regs[index]) {
Alessandro Rubini35bdd292012-04-12 10:48:44 +0200311 release_mem_region(res->start, resource_size(res));
312 return -ENOMEM;
313 }
Davide Ciminaghid94e2552012-11-09 15:19:53 +0100314 regmap_config->lock_arg = &mfd->lock;
315 /*
316 No caching, registers could be reached both via regmap and via
317 void __iomem *
318 */
319 regmap_config->cache_type = REGCACHE_NONE;
320 mfd->regmap[index] = devm_regmap_init_mmio(&dev->dev, mfd->regs[index],
321 regmap_config);
322 WARN_ON(!mfd->regmap[index]);
323
Alessandro Rubini35bdd292012-04-12 10:48:44 +0200324 return 0;
325}
326
Davide Ciminaghi1950c712012-11-09 15:19:52 +0100327static int sta2x11_sctl_probe(struct platform_device *dev)
328{
329 return sta2x11_mfd_platform_probe(dev, sta2x11_sctl);
330}
331
Alessandro Rubini35bdd292012-04-12 10:48:44 +0200332static int sta2x11_apbreg_probe(struct platform_device *dev)
333{
Davide Ciminaghi1950c712012-11-09 15:19:52 +0100334 return sta2x11_mfd_platform_probe(dev, sta2x11_apbreg);
Alessandro Rubini35bdd292012-04-12 10:48:44 +0200335}
336
Davide Ciminaghi1950c712012-11-09 15:19:52 +0100337static int sta2x11_apb_soc_regs_probe(struct platform_device *dev)
338{
339 return sta2x11_mfd_platform_probe(dev, sta2x11_apb_soc_regs);
340}
341
342/* The three platform drivers */
Alessandro Rubini35bdd292012-04-12 10:48:44 +0200343static struct platform_driver sta2x11_sctl_platform_driver = {
344 .driver = {
Davide Ciminaghib18adaf2012-11-09 15:19:55 +0100345 .name = STA2X11_MFD_SCTL_NAME,
Alessandro Rubini35bdd292012-04-12 10:48:44 +0200346 .owner = THIS_MODULE,
347 },
348 .probe = sta2x11_sctl_probe,
349};
350
351static int __init sta2x11_sctl_init(void)
352{
353 pr_info("%s\n", __func__);
354 return platform_driver_register(&sta2x11_sctl_platform_driver);
355}
356
357static struct platform_driver sta2x11_platform_driver = {
358 .driver = {
Davide Ciminaghib18adaf2012-11-09 15:19:55 +0100359 .name = STA2X11_MFD_APBREG_NAME,
Alessandro Rubini35bdd292012-04-12 10:48:44 +0200360 .owner = THIS_MODULE,
361 },
362 .probe = sta2x11_apbreg_probe,
363};
364
365static int __init sta2x11_apbreg_init(void)
366{
367 pr_info("%s\n", __func__);
368 return platform_driver_register(&sta2x11_platform_driver);
369}
370
Davide Ciminaghi1950c712012-11-09 15:19:52 +0100371static struct platform_driver sta2x11_apb_soc_regs_platform_driver = {
372 .driver = {
Davide Ciminaghib18adaf2012-11-09 15:19:55 +0100373 .name = STA2X11_MFD_APB_SOC_REGS_NAME,
Davide Ciminaghi1950c712012-11-09 15:19:52 +0100374 .owner = THIS_MODULE,
375 },
376 .probe = sta2x11_apb_soc_regs_probe,
377};
378
379static int __init sta2x11_apb_soc_regs_init(void)
380{
381 pr_info("%s\n", __func__);
382 return platform_driver_register(&sta2x11_apb_soc_regs_platform_driver);
383}
384
Alessandro Rubini35bdd292012-04-12 10:48:44 +0200385/*
Davide Ciminaghi1950c712012-11-09 15:19:52 +0100386 * What follows are the PCI devices that host the above pdevs.
Alessandro Rubini35bdd292012-04-12 10:48:44 +0200387 * Each logic block is 4kB and they are all consecutive: we use this info.
388 */
389
Davide Ciminaghi1950c712012-11-09 15:19:52 +0100390/* Mfd 0 device */
391
392/* Mfd 0, Bar 0 */
393enum mfd0_bar0_cells {
Alessandro Rubini35bdd292012-04-12 10:48:44 +0200394 STA2X11_GPIO_0 = 0,
395 STA2X11_GPIO_1,
396 STA2X11_GPIO_2,
397 STA2X11_GPIO_3,
398 STA2X11_SCTL,
399 STA2X11_SCR,
400 STA2X11_TIME,
401};
Davide Ciminaghi1950c712012-11-09 15:19:52 +0100402/* Mfd 0 , Bar 1 */
403enum mfd0_bar1_cells {
Alessandro Rubini35bdd292012-04-12 10:48:44 +0200404 STA2X11_APBREG = 0,
405};
406#define CELL_4K(_name, _cell) { \
407 .name = _name, \
408 .start = _cell * 4096, .end = _cell * 4096 + 4095, \
409 .flags = IORESOURCE_MEM, \
410 }
411
412static const __devinitconst struct resource gpio_resources[] = {
413 {
Davide Ciminaghib18adaf2012-11-09 15:19:55 +0100414 /* 4 consecutive cells, 1 driver */
415 .name = STA2X11_MFD_GPIO_NAME,
Alessandro Rubini35bdd292012-04-12 10:48:44 +0200416 .start = 0,
417 .end = (4 * 4096) - 1,
418 .flags = IORESOURCE_MEM,
419 }
420};
421static const __devinitconst struct resource sctl_resources[] = {
Davide Ciminaghib18adaf2012-11-09 15:19:55 +0100422 CELL_4K(STA2X11_MFD_SCTL_NAME, STA2X11_SCTL),
Alessandro Rubini35bdd292012-04-12 10:48:44 +0200423};
424static const __devinitconst struct resource scr_resources[] = {
Davide Ciminaghib18adaf2012-11-09 15:19:55 +0100425 CELL_4K(STA2X11_MFD_SCR_NAME, STA2X11_SCR),
Alessandro Rubini35bdd292012-04-12 10:48:44 +0200426};
427static const __devinitconst struct resource time_resources[] = {
Davide Ciminaghib18adaf2012-11-09 15:19:55 +0100428 CELL_4K(STA2X11_MFD_TIME_NAME, STA2X11_TIME),
Alessandro Rubini35bdd292012-04-12 10:48:44 +0200429};
430
431static const __devinitconst struct resource apbreg_resources[] = {
Davide Ciminaghib18adaf2012-11-09 15:19:55 +0100432 CELL_4K(STA2X11_MFD_APBREG_NAME, STA2X11_APBREG),
Alessandro Rubini35bdd292012-04-12 10:48:44 +0200433};
434
435#define DEV(_name, _r) \
436 { .name = _name, .num_resources = ARRAY_SIZE(_r), .resources = _r, }
437
Davide Ciminaghi1950c712012-11-09 15:19:52 +0100438static __devinitdata struct mfd_cell sta2x11_mfd0_bar0[] = {
Davide Ciminaghib18adaf2012-11-09 15:19:55 +0100439 /* offset 0: we add pdata later */
440 DEV(STA2X11_MFD_GPIO_NAME, gpio_resources),
441 DEV(STA2X11_MFD_SCTL_NAME, sctl_resources),
442 DEV(STA2X11_MFD_SCR_NAME, scr_resources),
443 DEV(STA2X11_MFD_TIME_NAME, time_resources),
Alessandro Rubini35bdd292012-04-12 10:48:44 +0200444};
445
Davide Ciminaghi1950c712012-11-09 15:19:52 +0100446static __devinitdata struct mfd_cell sta2x11_mfd0_bar1[] = {
Davide Ciminaghib18adaf2012-11-09 15:19:55 +0100447 DEV(STA2X11_MFD_APBREG_NAME, apbreg_resources),
Alessandro Rubini35bdd292012-04-12 10:48:44 +0200448};
449
Davide Ciminaghi1950c712012-11-09 15:19:52 +0100450/* Mfd 1 devices */
451
452/* Mfd 1, Bar 0 */
453enum mfd1_bar0_cells {
454 STA2X11_VIC = 0,
455};
456
457/* Mfd 1, Bar 1 */
458enum mfd1_bar1_cells {
459 STA2X11_APB_SOC_REGS = 0,
460};
461
462static const __devinitconst struct resource vic_resources[] = {
Davide Ciminaghib18adaf2012-11-09 15:19:55 +0100463 CELL_4K(STA2X11_MFD_VIC_NAME, STA2X11_VIC),
Davide Ciminaghi1950c712012-11-09 15:19:52 +0100464};
465
466static const __devinitconst struct resource apb_soc_regs_resources[] = {
Davide Ciminaghib18adaf2012-11-09 15:19:55 +0100467 CELL_4K(STA2X11_MFD_APB_SOC_REGS_NAME, STA2X11_APB_SOC_REGS),
Davide Ciminaghi1950c712012-11-09 15:19:52 +0100468};
469
470static __devinitdata struct mfd_cell sta2x11_mfd1_bar0[] = {
Davide Ciminaghib18adaf2012-11-09 15:19:55 +0100471 DEV(STA2X11_MFD_VIC_NAME, vic_resources),
Davide Ciminaghi1950c712012-11-09 15:19:52 +0100472};
473
474static __devinitdata struct mfd_cell sta2x11_mfd1_bar1[] = {
Davide Ciminaghib18adaf2012-11-09 15:19:55 +0100475 DEV(STA2X11_MFD_APB_SOC_REGS_NAME, apb_soc_regs_resources),
Davide Ciminaghi1950c712012-11-09 15:19:52 +0100476};
477
478
Alessandro Rubini35bdd292012-04-12 10:48:44 +0200479static int sta2x11_mfd_suspend(struct pci_dev *pdev, pm_message_t state)
480{
481 pci_save_state(pdev);
482 pci_disable_device(pdev);
483 pci_set_power_state(pdev, pci_choose_state(pdev, state));
484
485 return 0;
486}
487
488static int sta2x11_mfd_resume(struct pci_dev *pdev)
489{
490 int err;
491
492 pci_set_power_state(pdev, 0);
493 err = pci_enable_device(pdev);
494 if (err)
495 return err;
496 pci_restore_state(pdev);
497
498 return 0;
499}
500
Davide Ciminaghi1950c712012-11-09 15:19:52 +0100501struct sta2x11_mfd_bar_setup_data {
502 struct mfd_cell *cells;
503 int ncells;
504};
505
506struct sta2x11_mfd_setup_data {
507 struct sta2x11_mfd_bar_setup_data bars[2];
508};
509
510#define STA2X11_MFD0 0
511#define STA2X11_MFD1 1
512
513static struct sta2x11_mfd_setup_data mfd_setup_data[] = {
514 /* Mfd 0: gpio, sctl, scr, timers / apbregs */
515 [STA2X11_MFD0] = {
516 .bars = {
517 [0] = {
518 .cells = sta2x11_mfd0_bar0,
519 .ncells = ARRAY_SIZE(sta2x11_mfd0_bar0),
520 },
521 [1] = {
522 .cells = sta2x11_mfd0_bar1,
523 .ncells = ARRAY_SIZE(sta2x11_mfd0_bar1),
524 },
525 },
526 },
527 /* Mfd 1: vic / apb-soc-regs */
528 [STA2X11_MFD1] = {
529 .bars = {
530 [0] = {
531 .cells = sta2x11_mfd1_bar0,
532 .ncells = ARRAY_SIZE(sta2x11_mfd1_bar0),
533 },
534 [1] = {
535 .cells = sta2x11_mfd1_bar1,
536 .ncells = ARRAY_SIZE(sta2x11_mfd1_bar1),
537 },
538 },
539 },
540};
541
542static void __devinit sta2x11_mfd_setup(struct pci_dev *pdev,
543 struct sta2x11_mfd_setup_data *sd)
544{
545 int i, j;
546 for (i = 0; i < ARRAY_SIZE(sd->bars); i++)
547 for (j = 0; j < sd->bars[i].ncells; j++) {
548 sd->bars[i].cells[j].pdata_size = sizeof(pdev);
549 sd->bars[i].cells[j].platform_data = &pdev;
550 }
551}
552
Alessandro Rubini35bdd292012-04-12 10:48:44 +0200553static int __devinit sta2x11_mfd_probe(struct pci_dev *pdev,
554 const struct pci_device_id *pci_id)
555{
556 int err, i;
Davide Ciminaghi1950c712012-11-09 15:19:52 +0100557 struct sta2x11_mfd_setup_data *setup_data;
Alessandro Rubini35bdd292012-04-12 10:48:44 +0200558
559 dev_info(&pdev->dev, "%s\n", __func__);
560
561 err = pci_enable_device(pdev);
562 if (err) {
563 dev_err(&pdev->dev, "Can't enable device.\n");
564 return err;
565 }
566
567 err = pci_enable_msi(pdev);
568 if (err)
569 dev_info(&pdev->dev, "Enable msi failed\n");
570
Davide Ciminaghi1950c712012-11-09 15:19:52 +0100571 setup_data = pci_id->device == PCI_DEVICE_ID_STMICRO_GPIO ?
572 &mfd_setup_data[STA2X11_MFD0] :
573 &mfd_setup_data[STA2X11_MFD1];
574
Alessandro Rubini35bdd292012-04-12 10:48:44 +0200575 /* platform data is the pci device for all of them */
Davide Ciminaghi1950c712012-11-09 15:19:52 +0100576 sta2x11_mfd_setup(pdev, setup_data);
Alessandro Rubini35bdd292012-04-12 10:48:44 +0200577
578 /* Record this pdev before mfd_add_devices: their probe looks for it */
Davide Ciminaghi8ec86a32012-11-09 15:19:56 +0100579 if (!sta2x11_mfd_find(pdev))
580 sta2x11_mfd_add(pdev, GFP_ATOMIC);
Alessandro Rubini35bdd292012-04-12 10:48:44 +0200581
Davide Ciminaghi1950c712012-11-09 15:19:52 +0100582 /* Just 2 bars for all mfd's at present */
583 for (i = 0; i < 2; i++) {
584 err = mfd_add_devices(&pdev->dev, -1,
585 setup_data->bars[i].cells,
586 setup_data->bars[i].ncells,
587 &pdev->resource[i],
588 0, NULL);
589 if (err) {
590 dev_err(&pdev->dev,
591 "mfd_add_devices[%d] failed: %d\n", i, err);
592 goto err_disable;
593 }
Alessandro Rubini35bdd292012-04-12 10:48:44 +0200594 }
595
596 return 0;
597
598err_disable:
599 mfd_remove_devices(&pdev->dev);
600 pci_disable_device(pdev);
601 pci_disable_msi(pdev);
602 return err;
603}
604
605static DEFINE_PCI_DEVICE_TABLE(sta2x11_mfd_tbl) = {
606 {PCI_DEVICE(PCI_VENDOR_ID_STMICRO, PCI_DEVICE_ID_STMICRO_GPIO)},
Davide Ciminaghi1950c712012-11-09 15:19:52 +0100607 {PCI_DEVICE(PCI_VENDOR_ID_STMICRO, PCI_DEVICE_ID_STMICRO_VIC)},
Alessandro Rubini35bdd292012-04-12 10:48:44 +0200608 {0,},
609};
610
611static struct pci_driver sta2x11_mfd_driver = {
612 .name = "sta2x11-mfd",
613 .id_table = sta2x11_mfd_tbl,
614 .probe = sta2x11_mfd_probe,
615 .suspend = sta2x11_mfd_suspend,
616 .resume = sta2x11_mfd_resume,
617};
618
619static int __init sta2x11_mfd_init(void)
620{
621 pr_info("%s\n", __func__);
622 return pci_register_driver(&sta2x11_mfd_driver);
623}
624
625/*
626 * All of this must be ready before "normal" devices like MMCI appear.
627 * But MFD (the pci device) can't be too early. The following choice
628 * prepares platform drivers very early and probe the PCI device later,
629 * but before other PCI devices.
630 */
631subsys_initcall(sta2x11_apbreg_init);
632subsys_initcall(sta2x11_sctl_init);
Davide Ciminaghi1950c712012-11-09 15:19:52 +0100633subsys_initcall(sta2x11_apb_soc_regs_init);
Alessandro Rubini35bdd292012-04-12 10:48:44 +0200634rootfs_initcall(sta2x11_mfd_init);
635
636MODULE_LICENSE("GPL v2");
637MODULE_AUTHOR("Wind River");
638MODULE_DESCRIPTION("STA2x11 mfd for GPIO, SCTL and APBREG");
639MODULE_DEVICE_TABLE(pci, sta2x11_mfd_tbl);