blob: 8d38ef264722b93e2f7cd55c104c56d23d17316a [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];
Alessandro Rubini35bdd292012-04-12 10:48:44 +020049 spinlock_t lock;
50 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{
81 struct sta2x11_mfd *mfd = sta2x11_mfd_find(pdev);
82 struct sta2x11_instance *instance;
83
84 if (mfd)
85 return -EBUSY;
86 instance = sta2x11_get_instance(pdev);
87 if (!instance)
88 return -EINVAL;
89 mfd = kzalloc(sizeof(*mfd), flags);
90 if (!mfd)
91 return -ENOMEM;
92 INIT_LIST_HEAD(&mfd->list);
93 spin_lock_init(&mfd->lock);
94 mfd->instance = instance;
95 list_add(&mfd->list, &sta2x11_mfd_list);
96 return 0;
97}
98
99static int __devexit mfd_remove(struct pci_dev *pdev)
100{
101 struct sta2x11_mfd *mfd = sta2x11_mfd_find(pdev);
102
103 if (!mfd)
104 return -ENODEV;
105 list_del(&mfd->list);
106 kfree(mfd);
107 return 0;
108}
109
Davide Ciminaghi1950c712012-11-09 15:19:52 +0100110/* This function is exported and is not expected to fail */
111u32 __sta2x11_mfd_mask(struct pci_dev *pdev, u32 reg, u32 mask, u32 val,
112 enum sta2x11_mfd_plat_dev index)
Alessandro Rubini35bdd292012-04-12 10:48:44 +0200113{
114 struct sta2x11_mfd *mfd = sta2x11_mfd_find(pdev);
115 u32 r;
116 unsigned long flags;
Davide Ciminaghi1950c712012-11-09 15:19:52 +0100117 void __iomem *regs = mfd->regs[index];
Alessandro Rubini35bdd292012-04-12 10:48:44 +0200118
119 if (!mfd) {
120 dev_warn(&pdev->dev, ": can't access sctl regs\n");
121 return 0;
122 }
Davide Ciminaghi1950c712012-11-09 15:19:52 +0100123 if (!regs) {
Alessandro Rubini35bdd292012-04-12 10:48:44 +0200124 dev_warn(&pdev->dev, ": system ctl not initialized\n");
125 return 0;
126 }
127 spin_lock_irqsave(&mfd->lock, flags);
Davide Ciminaghi1950c712012-11-09 15:19:52 +0100128 r = readl(regs + reg);
Alessandro Rubini35bdd292012-04-12 10:48:44 +0200129 r &= ~mask;
130 r |= val;
131 if (mask)
Davide Ciminaghi1950c712012-11-09 15:19:52 +0100132 writel(r, regs + reg);
Alessandro Rubini35bdd292012-04-12 10:48:44 +0200133 spin_unlock_irqrestore(&mfd->lock, flags);
134 return r;
135}
Davide Ciminaghi1950c712012-11-09 15:19:52 +0100136EXPORT_SYMBOL(__sta2x11_mfd_mask);
Alessandro Rubini35bdd292012-04-12 10:48:44 +0200137
Davide Ciminaghi29f5b5a2012-11-09 15:19:54 +0100138int sta2x11_mfd_get_regs_data(struct platform_device *dev,
139 enum sta2x11_mfd_plat_dev index,
140 void __iomem **regs,
141 spinlock_t **lock)
142{
143 struct pci_dev *pdev = *(struct pci_dev **)(dev->dev.platform_data);
144 struct sta2x11_mfd *mfd;
145
146 if (!pdev)
147 return -ENODEV;
148 mfd = sta2x11_mfd_find(pdev);
149 if (!mfd)
150 return -ENODEV;
151 if (index >= sta2x11_n_mfd_plat_devs)
152 return -ENODEV;
153 *regs = mfd->regs[index];
154 *lock = &mfd->lock[index];
155 pr_debug("%s %d *regs = %p\n", __func__, __LINE__, *regs);
156 return *regs ? 0 : -ENODEV;
157}
158EXPORT_SYMBOL(sta2x11_mfd_get_regs_data);
159
Davide Ciminaghid94e2552012-11-09 15:19:53 +0100160/*
161 * Special sta2x11-mfd regmap lock/unlock functions
162 */
Alessandro Rubini35bdd292012-04-12 10:48:44 +0200163
Davide Ciminaghid94e2552012-11-09 15:19:53 +0100164static void sta2x11_regmap_lock(void *__lock)
165{
166 spinlock_t *lock = __lock;
167 spin_lock(lock);
168}
Alessandro Rubini35bdd292012-04-12 10:48:44 +0200169
Davide Ciminaghid94e2552012-11-09 15:19:53 +0100170static void sta2x11_regmap_unlock(void *__lock)
171{
172 spinlock_t *lock = __lock;
173 spin_unlock(lock);
174}
Davide Ciminaghi1950c712012-11-09 15:19:52 +0100175
176static const char *sta2x11_mfd_names[sta2x11_n_mfd_plat_devs] = {
Davide Ciminaghib18adaf2012-11-09 15:19:55 +0100177 [sta2x11_sctl] = STA2X11_MFD_SCTL_NAME,
178 [sta2x11_apbreg] = STA2X11_MFD_APBREG_NAME,
179 [sta2x11_apb_soc_regs] = STA2X11_MFD_APB_SOC_REGS_NAME,
Davide Ciminaghi1950c712012-11-09 15:19:52 +0100180};
181
Davide Ciminaghid94e2552012-11-09 15:19:53 +0100182static bool sta2x11_sctl_writeable_reg(struct device *dev, unsigned int reg)
183{
184 return !__reg_within_range(reg, SCTL_SCPCIECSBRST, SCTL_SCRSTSTA);
185}
186
187static struct regmap_config sta2x11_sctl_regmap_config = {
188 .reg_bits = 32,
189 .reg_stride = 4,
190 .val_bits = 32,
191 .lock = sta2x11_regmap_lock,
192 .unlock = sta2x11_regmap_unlock,
193 .max_register = SCTL_SCRSTSTA,
194 .writeable_reg = sta2x11_sctl_writeable_reg,
195};
196
197static bool sta2x11_apbreg_readable_reg(struct device *dev, unsigned int reg)
198{
199 /* Two blocks (CAN and MLB, SARAC) 0x100 bytes apart */
200 if (reg >= APBREG_BSR_SARAC)
201 reg -= APBREG_BSR_SARAC;
202 switch (reg) {
203 case APBREG_BSR:
204 case APBREG_PAER:
205 case APBREG_PWAC:
206 case APBREG_PRAC:
207 case APBREG_PCG:
208 case APBREG_PUR:
209 case APBREG_EMU_PCG:
210 return true;
211 default:
212 return false;
213 }
214}
215
216static bool sta2x11_apbreg_writeable_reg(struct device *dev, unsigned int reg)
217{
218 if (reg >= APBREG_BSR_SARAC)
219 reg -= APBREG_BSR_SARAC;
220 if (!sta2x11_apbreg_readable_reg(dev, reg))
221 return false;
222 return reg != APBREG_PAER;
223}
224
225static struct regmap_config sta2x11_apbreg_regmap_config = {
226 .reg_bits = 32,
227 .reg_stride = 4,
228 .val_bits = 32,
229 .lock = sta2x11_regmap_lock,
230 .unlock = sta2x11_regmap_unlock,
231 .max_register = APBREG_EMU_PCG_SARAC,
232 .readable_reg = sta2x11_apbreg_readable_reg,
233 .writeable_reg = sta2x11_apbreg_writeable_reg,
234};
235
236static bool sta2x11_apb_soc_regs_readable_reg(struct device *dev,
237 unsigned int reg)
238{
239 return reg <= PCIE_SoC_INT_ROUTER_STATUS3_REG ||
240 __reg_within_range(reg, DMA_IP_CTRL_REG, SPARE3_RESERVED) ||
241 __reg_within_range(reg, MASTER_LOCK_REG,
242 SYSTEM_CONFIG_STATUS_REG) ||
243 reg == MSP_CLK_CTRL_REG ||
244 __reg_within_range(reg, COMPENSATION_REG1, TEST_CTL_REG);
245}
246
247static bool sta2x11_apb_soc_regs_writeable_reg(struct device *dev,
248 unsigned int reg)
249{
250 if (!sta2x11_apb_soc_regs_readable_reg(dev, reg))
251 return false;
252 switch (reg) {
253 case PCIE_COMMON_CLOCK_CONFIG_0_4_0:
254 case SYSTEM_CONFIG_STATUS_REG:
255 case COMPENSATION_REG1:
256 case PCIE_SoC_INT_ROUTER_STATUS0_REG...PCIE_SoC_INT_ROUTER_STATUS3_REG:
257 case PCIE_PM_STATUS_0_PORT_0_4...PCIE_PM_STATUS_7_0_EP4:
258 return false;
259 default:
260 return true;
261 }
262}
263
264static struct regmap_config sta2x11_apb_soc_regs_regmap_config = {
265 .reg_bits = 32,
266 .reg_stride = 4,
267 .val_bits = 32,
268 .lock = sta2x11_regmap_lock,
269 .unlock = sta2x11_regmap_unlock,
270 .max_register = TEST_CTL_REG,
271 .readable_reg = sta2x11_apb_soc_regs_readable_reg,
272 .writeable_reg = sta2x11_apb_soc_regs_writeable_reg,
273};
274
275static struct regmap_config *
276sta2x11_mfd_regmap_configs[sta2x11_n_mfd_plat_devs] = {
277 [sta2x11_sctl] = &sta2x11_sctl_regmap_config,
278 [sta2x11_apbreg] = &sta2x11_apbreg_regmap_config,
279 [sta2x11_apb_soc_regs] = &sta2x11_apb_soc_regs_regmap_config,
280};
281
Davide Ciminaghi1950c712012-11-09 15:19:52 +0100282/* Probe for the three platform devices */
283
284static int sta2x11_mfd_platform_probe(struct platform_device *dev,
285 enum sta2x11_mfd_plat_dev index)
Alessandro Rubini35bdd292012-04-12 10:48:44 +0200286{
287 struct pci_dev **pdev;
288 struct sta2x11_mfd *mfd;
289 struct resource *res;
Davide Ciminaghi1950c712012-11-09 15:19:52 +0100290 const char *name = sta2x11_mfd_names[index];
Davide Ciminaghid94e2552012-11-09 15:19:53 +0100291 struct regmap_config *regmap_config = sta2x11_mfd_regmap_configs[index];
Alessandro Rubini35bdd292012-04-12 10:48:44 +0200292
293 pdev = dev->dev.platform_data;
294 mfd = sta2x11_mfd_find(*pdev);
295 if (!mfd)
296 return -ENODEV;
Davide Ciminaghid94e2552012-11-09 15:19:53 +0100297 if (!regmap_config)
298 return -ENODEV;
Alessandro Rubini35bdd292012-04-12 10:48:44 +0200299
300 res = platform_get_resource(dev, IORESOURCE_MEM, 0);
301 if (!res)
302 return -ENOMEM;
303
Davide Ciminaghi1950c712012-11-09 15:19:52 +0100304 if (!request_mem_region(res->start, resource_size(res), name))
Alessandro Rubini35bdd292012-04-12 10:48:44 +0200305 return -EBUSY;
306
Davide Ciminaghi1950c712012-11-09 15:19:52 +0100307 mfd->regs[index] = ioremap(res->start, resource_size(res));
308 if (!mfd->regs[index]) {
Alessandro Rubini35bdd292012-04-12 10:48:44 +0200309 release_mem_region(res->start, resource_size(res));
310 return -ENOMEM;
311 }
Davide Ciminaghid94e2552012-11-09 15:19:53 +0100312 regmap_config->lock_arg = &mfd->lock;
313 /*
314 No caching, registers could be reached both via regmap and via
315 void __iomem *
316 */
317 regmap_config->cache_type = REGCACHE_NONE;
318 mfd->regmap[index] = devm_regmap_init_mmio(&dev->dev, mfd->regs[index],
319 regmap_config);
320 WARN_ON(!mfd->regmap[index]);
321
Alessandro Rubini35bdd292012-04-12 10:48:44 +0200322 return 0;
323}
324
Davide Ciminaghi1950c712012-11-09 15:19:52 +0100325static int sta2x11_sctl_probe(struct platform_device *dev)
326{
327 return sta2x11_mfd_platform_probe(dev, sta2x11_sctl);
328}
329
Alessandro Rubini35bdd292012-04-12 10:48:44 +0200330static int sta2x11_apbreg_probe(struct platform_device *dev)
331{
Davide Ciminaghi1950c712012-11-09 15:19:52 +0100332 return sta2x11_mfd_platform_probe(dev, sta2x11_apbreg);
Alessandro Rubini35bdd292012-04-12 10:48:44 +0200333}
334
Davide Ciminaghi1950c712012-11-09 15:19:52 +0100335static int sta2x11_apb_soc_regs_probe(struct platform_device *dev)
336{
337 return sta2x11_mfd_platform_probe(dev, sta2x11_apb_soc_regs);
338}
339
340/* The three platform drivers */
Alessandro Rubini35bdd292012-04-12 10:48:44 +0200341static struct platform_driver sta2x11_sctl_platform_driver = {
342 .driver = {
Davide Ciminaghib18adaf2012-11-09 15:19:55 +0100343 .name = STA2X11_MFD_SCTL_NAME,
Alessandro Rubini35bdd292012-04-12 10:48:44 +0200344 .owner = THIS_MODULE,
345 },
346 .probe = sta2x11_sctl_probe,
347};
348
349static int __init sta2x11_sctl_init(void)
350{
351 pr_info("%s\n", __func__);
352 return platform_driver_register(&sta2x11_sctl_platform_driver);
353}
354
355static struct platform_driver sta2x11_platform_driver = {
356 .driver = {
Davide Ciminaghib18adaf2012-11-09 15:19:55 +0100357 .name = STA2X11_MFD_APBREG_NAME,
Alessandro Rubini35bdd292012-04-12 10:48:44 +0200358 .owner = THIS_MODULE,
359 },
360 .probe = sta2x11_apbreg_probe,
361};
362
363static int __init sta2x11_apbreg_init(void)
364{
365 pr_info("%s\n", __func__);
366 return platform_driver_register(&sta2x11_platform_driver);
367}
368
Davide Ciminaghi1950c712012-11-09 15:19:52 +0100369static struct platform_driver sta2x11_apb_soc_regs_platform_driver = {
370 .driver = {
Davide Ciminaghib18adaf2012-11-09 15:19:55 +0100371 .name = STA2X11_MFD_APB_SOC_REGS_NAME,
Davide Ciminaghi1950c712012-11-09 15:19:52 +0100372 .owner = THIS_MODULE,
373 },
374 .probe = sta2x11_apb_soc_regs_probe,
375};
376
377static int __init sta2x11_apb_soc_regs_init(void)
378{
379 pr_info("%s\n", __func__);
380 return platform_driver_register(&sta2x11_apb_soc_regs_platform_driver);
381}
382
Alessandro Rubini35bdd292012-04-12 10:48:44 +0200383/*
Davide Ciminaghi1950c712012-11-09 15:19:52 +0100384 * What follows are the PCI devices that host the above pdevs.
Alessandro Rubini35bdd292012-04-12 10:48:44 +0200385 * Each logic block is 4kB and they are all consecutive: we use this info.
386 */
387
Davide Ciminaghi1950c712012-11-09 15:19:52 +0100388/* Mfd 0 device */
389
390/* Mfd 0, Bar 0 */
391enum mfd0_bar0_cells {
Alessandro Rubini35bdd292012-04-12 10:48:44 +0200392 STA2X11_GPIO_0 = 0,
393 STA2X11_GPIO_1,
394 STA2X11_GPIO_2,
395 STA2X11_GPIO_3,
396 STA2X11_SCTL,
397 STA2X11_SCR,
398 STA2X11_TIME,
399};
Davide Ciminaghi1950c712012-11-09 15:19:52 +0100400/* Mfd 0 , Bar 1 */
401enum mfd0_bar1_cells {
Alessandro Rubini35bdd292012-04-12 10:48:44 +0200402 STA2X11_APBREG = 0,
403};
404#define CELL_4K(_name, _cell) { \
405 .name = _name, \
406 .start = _cell * 4096, .end = _cell * 4096 + 4095, \
407 .flags = IORESOURCE_MEM, \
408 }
409
410static const __devinitconst struct resource gpio_resources[] = {
411 {
Davide Ciminaghib18adaf2012-11-09 15:19:55 +0100412 /* 4 consecutive cells, 1 driver */
413 .name = STA2X11_MFD_GPIO_NAME,
Alessandro Rubini35bdd292012-04-12 10:48:44 +0200414 .start = 0,
415 .end = (4 * 4096) - 1,
416 .flags = IORESOURCE_MEM,
417 }
418};
419static const __devinitconst struct resource sctl_resources[] = {
Davide Ciminaghib18adaf2012-11-09 15:19:55 +0100420 CELL_4K(STA2X11_MFD_SCTL_NAME, STA2X11_SCTL),
Alessandro Rubini35bdd292012-04-12 10:48:44 +0200421};
422static const __devinitconst struct resource scr_resources[] = {
Davide Ciminaghib18adaf2012-11-09 15:19:55 +0100423 CELL_4K(STA2X11_MFD_SCR_NAME, STA2X11_SCR),
Alessandro Rubini35bdd292012-04-12 10:48:44 +0200424};
425static const __devinitconst struct resource time_resources[] = {
Davide Ciminaghib18adaf2012-11-09 15:19:55 +0100426 CELL_4K(STA2X11_MFD_TIME_NAME, STA2X11_TIME),
Alessandro Rubini35bdd292012-04-12 10:48:44 +0200427};
428
429static const __devinitconst struct resource apbreg_resources[] = {
Davide Ciminaghib18adaf2012-11-09 15:19:55 +0100430 CELL_4K(STA2X11_MFD_APBREG_NAME, STA2X11_APBREG),
Alessandro Rubini35bdd292012-04-12 10:48:44 +0200431};
432
433#define DEV(_name, _r) \
434 { .name = _name, .num_resources = ARRAY_SIZE(_r), .resources = _r, }
435
Davide Ciminaghi1950c712012-11-09 15:19:52 +0100436static __devinitdata struct mfd_cell sta2x11_mfd0_bar0[] = {
Davide Ciminaghib18adaf2012-11-09 15:19:55 +0100437 /* offset 0: we add pdata later */
438 DEV(STA2X11_MFD_GPIO_NAME, gpio_resources),
439 DEV(STA2X11_MFD_SCTL_NAME, sctl_resources),
440 DEV(STA2X11_MFD_SCR_NAME, scr_resources),
441 DEV(STA2X11_MFD_TIME_NAME, time_resources),
Alessandro Rubini35bdd292012-04-12 10:48:44 +0200442};
443
Davide Ciminaghi1950c712012-11-09 15:19:52 +0100444static __devinitdata struct mfd_cell sta2x11_mfd0_bar1[] = {
Davide Ciminaghib18adaf2012-11-09 15:19:55 +0100445 DEV(STA2X11_MFD_APBREG_NAME, apbreg_resources),
Alessandro Rubini35bdd292012-04-12 10:48:44 +0200446};
447
Davide Ciminaghi1950c712012-11-09 15:19:52 +0100448/* Mfd 1 devices */
449
450/* Mfd 1, Bar 0 */
451enum mfd1_bar0_cells {
452 STA2X11_VIC = 0,
453};
454
455/* Mfd 1, Bar 1 */
456enum mfd1_bar1_cells {
457 STA2X11_APB_SOC_REGS = 0,
458};
459
460static const __devinitconst struct resource vic_resources[] = {
Davide Ciminaghib18adaf2012-11-09 15:19:55 +0100461 CELL_4K(STA2X11_MFD_VIC_NAME, STA2X11_VIC),
Davide Ciminaghi1950c712012-11-09 15:19:52 +0100462};
463
464static const __devinitconst struct resource apb_soc_regs_resources[] = {
Davide Ciminaghib18adaf2012-11-09 15:19:55 +0100465 CELL_4K(STA2X11_MFD_APB_SOC_REGS_NAME, STA2X11_APB_SOC_REGS),
Davide Ciminaghi1950c712012-11-09 15:19:52 +0100466};
467
468static __devinitdata struct mfd_cell sta2x11_mfd1_bar0[] = {
Davide Ciminaghib18adaf2012-11-09 15:19:55 +0100469 DEV(STA2X11_MFD_VIC_NAME, vic_resources),
Davide Ciminaghi1950c712012-11-09 15:19:52 +0100470};
471
472static __devinitdata struct mfd_cell sta2x11_mfd1_bar1[] = {
Davide Ciminaghib18adaf2012-11-09 15:19:55 +0100473 DEV(STA2X11_MFD_APB_SOC_REGS_NAME, apb_soc_regs_resources),
Davide Ciminaghi1950c712012-11-09 15:19:52 +0100474};
475
476
Alessandro Rubini35bdd292012-04-12 10:48:44 +0200477static int sta2x11_mfd_suspend(struct pci_dev *pdev, pm_message_t state)
478{
479 pci_save_state(pdev);
480 pci_disable_device(pdev);
481 pci_set_power_state(pdev, pci_choose_state(pdev, state));
482
483 return 0;
484}
485
486static int sta2x11_mfd_resume(struct pci_dev *pdev)
487{
488 int err;
489
490 pci_set_power_state(pdev, 0);
491 err = pci_enable_device(pdev);
492 if (err)
493 return err;
494 pci_restore_state(pdev);
495
496 return 0;
497}
498
Davide Ciminaghi1950c712012-11-09 15:19:52 +0100499struct sta2x11_mfd_bar_setup_data {
500 struct mfd_cell *cells;
501 int ncells;
502};
503
504struct sta2x11_mfd_setup_data {
505 struct sta2x11_mfd_bar_setup_data bars[2];
506};
507
508#define STA2X11_MFD0 0
509#define STA2X11_MFD1 1
510
511static struct sta2x11_mfd_setup_data mfd_setup_data[] = {
512 /* Mfd 0: gpio, sctl, scr, timers / apbregs */
513 [STA2X11_MFD0] = {
514 .bars = {
515 [0] = {
516 .cells = sta2x11_mfd0_bar0,
517 .ncells = ARRAY_SIZE(sta2x11_mfd0_bar0),
518 },
519 [1] = {
520 .cells = sta2x11_mfd0_bar1,
521 .ncells = ARRAY_SIZE(sta2x11_mfd0_bar1),
522 },
523 },
524 },
525 /* Mfd 1: vic / apb-soc-regs */
526 [STA2X11_MFD1] = {
527 .bars = {
528 [0] = {
529 .cells = sta2x11_mfd1_bar0,
530 .ncells = ARRAY_SIZE(sta2x11_mfd1_bar0),
531 },
532 [1] = {
533 .cells = sta2x11_mfd1_bar1,
534 .ncells = ARRAY_SIZE(sta2x11_mfd1_bar1),
535 },
536 },
537 },
538};
539
540static void __devinit sta2x11_mfd_setup(struct pci_dev *pdev,
541 struct sta2x11_mfd_setup_data *sd)
542{
543 int i, j;
544 for (i = 0; i < ARRAY_SIZE(sd->bars); i++)
545 for (j = 0; j < sd->bars[i].ncells; j++) {
546 sd->bars[i].cells[j].pdata_size = sizeof(pdev);
547 sd->bars[i].cells[j].platform_data = &pdev;
548 }
549}
550
Alessandro Rubini35bdd292012-04-12 10:48:44 +0200551static int __devinit sta2x11_mfd_probe(struct pci_dev *pdev,
552 const struct pci_device_id *pci_id)
553{
554 int err, i;
Davide Ciminaghi1950c712012-11-09 15:19:52 +0100555 struct sta2x11_mfd_setup_data *setup_data;
Alessandro Rubini35bdd292012-04-12 10:48:44 +0200556 struct sta2x11_gpio_pdata *gpio_data;
557
558 dev_info(&pdev->dev, "%s\n", __func__);
559
560 err = pci_enable_device(pdev);
561 if (err) {
562 dev_err(&pdev->dev, "Can't enable device.\n");
563 return err;
564 }
565
566 err = pci_enable_msi(pdev);
567 if (err)
568 dev_info(&pdev->dev, "Enable msi failed\n");
569
Davide Ciminaghi1950c712012-11-09 15:19:52 +0100570 setup_data = pci_id->device == PCI_DEVICE_ID_STMICRO_GPIO ?
571 &mfd_setup_data[STA2X11_MFD0] :
572 &mfd_setup_data[STA2X11_MFD1];
573
Alessandro Rubini35bdd292012-04-12 10:48:44 +0200574 /* Read gpio config data as pci device's platform data */
575 gpio_data = dev_get_platdata(&pdev->dev);
576 if (!gpio_data)
577 dev_warn(&pdev->dev, "no gpio configuration\n");
578
579 dev_dbg(&pdev->dev, "%s, gpio_data = %p (%p)\n", __func__,
580 gpio_data, &gpio_data);
581 dev_dbg(&pdev->dev, "%s, pdev = %p (%p)\n", __func__,
582 pdev, &pdev);
583
584 /* platform data is the pci device for all of them */
Davide Ciminaghi1950c712012-11-09 15:19:52 +0100585 sta2x11_mfd_setup(pdev, setup_data);
Alessandro Rubini35bdd292012-04-12 10:48:44 +0200586
587 /* Record this pdev before mfd_add_devices: their probe looks for it */
588 sta2x11_mfd_add(pdev, GFP_ATOMIC);
589
Davide Ciminaghi1950c712012-11-09 15:19:52 +0100590 /* Just 2 bars for all mfd's at present */
591 for (i = 0; i < 2; i++) {
592 err = mfd_add_devices(&pdev->dev, -1,
593 setup_data->bars[i].cells,
594 setup_data->bars[i].ncells,
595 &pdev->resource[i],
596 0, NULL);
597 if (err) {
598 dev_err(&pdev->dev,
599 "mfd_add_devices[%d] failed: %d\n", i, err);
600 goto err_disable;
601 }
Alessandro Rubini35bdd292012-04-12 10:48:44 +0200602 }
603
604 return 0;
605
606err_disable:
607 mfd_remove_devices(&pdev->dev);
608 pci_disable_device(pdev);
609 pci_disable_msi(pdev);
610 return err;
611}
612
613static DEFINE_PCI_DEVICE_TABLE(sta2x11_mfd_tbl) = {
614 {PCI_DEVICE(PCI_VENDOR_ID_STMICRO, PCI_DEVICE_ID_STMICRO_GPIO)},
Davide Ciminaghi1950c712012-11-09 15:19:52 +0100615 {PCI_DEVICE(PCI_VENDOR_ID_STMICRO, PCI_DEVICE_ID_STMICRO_VIC)},
Alessandro Rubini35bdd292012-04-12 10:48:44 +0200616 {0,},
617};
618
619static struct pci_driver sta2x11_mfd_driver = {
620 .name = "sta2x11-mfd",
621 .id_table = sta2x11_mfd_tbl,
622 .probe = sta2x11_mfd_probe,
623 .suspend = sta2x11_mfd_suspend,
624 .resume = sta2x11_mfd_resume,
625};
626
627static int __init sta2x11_mfd_init(void)
628{
629 pr_info("%s\n", __func__);
630 return pci_register_driver(&sta2x11_mfd_driver);
631}
632
633/*
634 * All of this must be ready before "normal" devices like MMCI appear.
635 * But MFD (the pci device) can't be too early. The following choice
636 * prepares platform drivers very early and probe the PCI device later,
637 * but before other PCI devices.
638 */
639subsys_initcall(sta2x11_apbreg_init);
640subsys_initcall(sta2x11_sctl_init);
Davide Ciminaghi1950c712012-11-09 15:19:52 +0100641subsys_initcall(sta2x11_apb_soc_regs_init);
Alessandro Rubini35bdd292012-04-12 10:48:44 +0200642rootfs_initcall(sta2x11_mfd_init);
643
644MODULE_LICENSE("GPL v2");
645MODULE_AUTHOR("Wind River");
646MODULE_DESCRIPTION("STA2x11 mfd for GPIO, SCTL and APBREG");
647MODULE_DEVICE_TABLE(pci, sta2x11_mfd_tbl);