blob: 1819c8fe4d8f65ae000094b5e46d2b3d7fcc12e8 [file] [log] [blame]
Thomas Gleixner45051532019-05-29 16:57:47 -07001// SPDX-License-Identifier: GPL-2.0-only
Alessandro Rubini35bdd292012-04-12 10:48:44 +02002/*
Paul Gortmakercf090912019-01-13 13:36:39 -05003 * STA2x11 mfd for GPIO, SCTL and APBREG
4 *
Alessandro Rubini35bdd292012-04-12 10:48:44 +02005 * Copyright (c) 2009-2011 Wind River Systems, Inc.
Davide Ciminaghib73df692012-11-09 15:20:01 +01006 * Copyright (c) 2011 ST Microelectronics (Alessandro Rubini, Davide Ciminaghi)
Alessandro Rubini35bdd292012-04-12 10:48:44 +02007 */
8
9#include <linux/kernel.h>
Paul Gortmakercf090912019-01-13 13:36:39 -050010#include <linux/init.h>
11#include <linux/export.h>
Alessandro Rubini35bdd292012-04-12 10:48:44 +020012#include <linux/spinlock.h>
13#include <linux/errno.h>
14#include <linux/device.h>
15#include <linux/slab.h>
16#include <linux/list.h>
17#include <linux/io.h>
18#include <linux/ioport.h>
19#include <linux/pci.h>
Alessandro Rubini35bdd292012-04-12 10:48:44 +020020#include <linux/seq_file.h>
21#include <linux/platform_device.h>
22#include <linux/mfd/core.h>
23#include <linux/mfd/sta2x11-mfd.h>
Davide Ciminaghid94e2552012-11-09 15:19:53 +010024#include <linux/regmap.h>
Alessandro Rubini35bdd292012-04-12 10:48:44 +020025
26#include <asm/sta2x11.h>
27
Davide Ciminaghid94e2552012-11-09 15:19:53 +010028static inline int __reg_within_range(unsigned int r,
29 unsigned int start,
30 unsigned int end)
31{
32 return ((r >= start) && (r <= end));
33}
34
Alessandro Rubini35bdd292012-04-12 10:48:44 +020035/* This describes STA2X11 MFD chip for us, we may have several */
36struct sta2x11_mfd {
37 struct sta2x11_instance *instance;
Davide Ciminaghid94e2552012-11-09 15:19:53 +010038 struct regmap *regmap[sta2x11_n_mfd_plat_devs];
Davide Ciminaghie885ba292012-11-09 15:19:58 +010039 spinlock_t lock[sta2x11_n_mfd_plat_devs];
Alessandro Rubini35bdd292012-04-12 10:48:44 +020040 struct list_head list;
Davide Ciminaghi1950c712012-11-09 15:19:52 +010041 void __iomem *regs[sta2x11_n_mfd_plat_devs];
Alessandro Rubini35bdd292012-04-12 10:48:44 +020042};
43
44static LIST_HEAD(sta2x11_mfd_list);
45
46/* Three functions to act on the list */
47static struct sta2x11_mfd *sta2x11_mfd_find(struct pci_dev *pdev)
48{
49 struct sta2x11_instance *instance;
50 struct sta2x11_mfd *mfd;
51
52 if (!pdev && !list_empty(&sta2x11_mfd_list)) {
Joe Perches81d30ed2017-02-16 23:11:36 -080053 pr_warn("%s: Unspecified device, using first instance\n",
54 __func__);
Alessandro Rubini35bdd292012-04-12 10:48:44 +020055 return list_entry(sta2x11_mfd_list.next,
56 struct sta2x11_mfd, list);
57 }
58
59 instance = sta2x11_get_instance(pdev);
60 if (!instance)
61 return NULL;
62 list_for_each_entry(mfd, &sta2x11_mfd_list, list) {
63 if (mfd->instance == instance)
64 return mfd;
65 }
66 return NULL;
67}
68
Bill Pembertonf791be42012-11-19 13:23:04 -050069static int sta2x11_mfd_add(struct pci_dev *pdev, gfp_t flags)
Alessandro Rubini35bdd292012-04-12 10:48:44 +020070{
Davide Ciminaghie885ba292012-11-09 15:19:58 +010071 int i;
Alessandro Rubini35bdd292012-04-12 10:48:44 +020072 struct sta2x11_mfd *mfd = sta2x11_mfd_find(pdev);
73 struct sta2x11_instance *instance;
74
75 if (mfd)
76 return -EBUSY;
77 instance = sta2x11_get_instance(pdev);
78 if (!instance)
79 return -EINVAL;
80 mfd = kzalloc(sizeof(*mfd), flags);
81 if (!mfd)
82 return -ENOMEM;
83 INIT_LIST_HEAD(&mfd->list);
Davide Ciminaghie885ba292012-11-09 15:19:58 +010084 for (i = 0; i < ARRAY_SIZE(mfd->lock); i++)
85 spin_lock_init(&mfd->lock[i]);
Alessandro Rubini35bdd292012-04-12 10:48:44 +020086 mfd->instance = instance;
87 list_add(&mfd->list, &sta2x11_mfd_list);
88 return 0;
89}
90
Davide Ciminaghi1950c712012-11-09 15:19:52 +010091/* This function is exported and is not expected to fail */
92u32 __sta2x11_mfd_mask(struct pci_dev *pdev, u32 reg, u32 mask, u32 val,
93 enum sta2x11_mfd_plat_dev index)
Alessandro Rubini35bdd292012-04-12 10:48:44 +020094{
95 struct sta2x11_mfd *mfd = sta2x11_mfd_find(pdev);
96 u32 r;
97 unsigned long flags;
Wei Yongjun709edec2012-12-02 08:36:22 -050098 void __iomem *regs;
Alessandro Rubini35bdd292012-04-12 10:48:44 +020099
100 if (!mfd) {
101 dev_warn(&pdev->dev, ": can't access sctl regs\n");
102 return 0;
103 }
Wei Yongjun709edec2012-12-02 08:36:22 -0500104
105 regs = mfd->regs[index];
Davide Ciminaghi1950c712012-11-09 15:19:52 +0100106 if (!regs) {
Alessandro Rubini35bdd292012-04-12 10:48:44 +0200107 dev_warn(&pdev->dev, ": system ctl not initialized\n");
108 return 0;
109 }
Davide Ciminaghie885ba292012-11-09 15:19:58 +0100110 spin_lock_irqsave(&mfd->lock[index], flags);
Davide Ciminaghi1950c712012-11-09 15:19:52 +0100111 r = readl(regs + reg);
Alessandro Rubini35bdd292012-04-12 10:48:44 +0200112 r &= ~mask;
113 r |= val;
114 if (mask)
Davide Ciminaghi1950c712012-11-09 15:19:52 +0100115 writel(r, regs + reg);
Davide Ciminaghie885ba292012-11-09 15:19:58 +0100116 spin_unlock_irqrestore(&mfd->lock[index], flags);
Alessandro Rubini35bdd292012-04-12 10:48:44 +0200117 return r;
118}
Davide Ciminaghi1950c712012-11-09 15:19:52 +0100119EXPORT_SYMBOL(__sta2x11_mfd_mask);
Alessandro Rubini35bdd292012-04-12 10:48:44 +0200120
Davide Ciminaghi29f5b5a2012-11-09 15:19:54 +0100121int sta2x11_mfd_get_regs_data(struct platform_device *dev,
122 enum sta2x11_mfd_plat_dev index,
123 void __iomem **regs,
124 spinlock_t **lock)
Alessandro Rubini35bdd292012-04-12 10:48:44 +0200125{
Jingoo Han334a41ce2013-07-30 17:10:05 +0900126 struct pci_dev *pdev = *(struct pci_dev **)dev_get_platdata(&dev->dev);
Davide Ciminaghi29f5b5a2012-11-09 15:19:54 +0100127 struct sta2x11_mfd *mfd;
Alessandro Rubini35bdd292012-04-12 10:48:44 +0200128
Davide Ciminaghi29f5b5a2012-11-09 15:19:54 +0100129 if (!pdev)
130 return -ENODEV;
131 mfd = sta2x11_mfd_find(pdev);
132 if (!mfd)
133 return -ENODEV;
134 if (index >= sta2x11_n_mfd_plat_devs)
135 return -ENODEV;
136 *regs = mfd->regs[index];
137 *lock = &mfd->lock[index];
138 pr_debug("%s %d *regs = %p\n", __func__, __LINE__, *regs);
139 return *regs ? 0 : -ENODEV;
Alessandro Rubini35bdd292012-04-12 10:48:44 +0200140}
Davide Ciminaghi29f5b5a2012-11-09 15:19:54 +0100141EXPORT_SYMBOL(sta2x11_mfd_get_regs_data);
Alessandro Rubini35bdd292012-04-12 10:48:44 +0200142
Davide Ciminaghid94e2552012-11-09 15:19:53 +0100143/*
144 * Special sta2x11-mfd regmap lock/unlock functions
145 */
Alessandro Rubini35bdd292012-04-12 10:48:44 +0200146
Davide Ciminaghid94e2552012-11-09 15:19:53 +0100147static void sta2x11_regmap_lock(void *__lock)
148{
149 spinlock_t *lock = __lock;
150 spin_lock(lock);
151}
Alessandro Rubini35bdd292012-04-12 10:48:44 +0200152
Davide Ciminaghid94e2552012-11-09 15:19:53 +0100153static void sta2x11_regmap_unlock(void *__lock)
154{
155 spinlock_t *lock = __lock;
156 spin_unlock(lock);
157}
Davide Ciminaghi1950c712012-11-09 15:19:52 +0100158
Davide Ciminaghidba6c1a2012-11-09 15:19:59 +0100159/* OTP (one time programmable registers do not require locking */
160static void sta2x11_regmap_nolock(void *__lock)
161{
162}
163
Davide Ciminaghi1950c712012-11-09 15:19:52 +0100164static const char *sta2x11_mfd_names[sta2x11_n_mfd_plat_devs] = {
Davide Ciminaghib18adaf2012-11-09 15:19:55 +0100165 [sta2x11_sctl] = STA2X11_MFD_SCTL_NAME,
166 [sta2x11_apbreg] = STA2X11_MFD_APBREG_NAME,
167 [sta2x11_apb_soc_regs] = STA2X11_MFD_APB_SOC_REGS_NAME,
Davide Ciminaghidba6c1a2012-11-09 15:19:59 +0100168 [sta2x11_scr] = STA2X11_MFD_SCR_NAME,
Alessandro Rubini35bdd292012-04-12 10:48:44 +0200169};
170
Davide Ciminaghid94e2552012-11-09 15:19:53 +0100171static bool sta2x11_sctl_writeable_reg(struct device *dev, unsigned int reg)
172{
173 return !__reg_within_range(reg, SCTL_SCPCIECSBRST, SCTL_SCRSTSTA);
174}
Alessandro Rubini35bdd292012-04-12 10:48:44 +0200175
Davide Ciminaghid94e2552012-11-09 15:19:53 +0100176static struct regmap_config sta2x11_sctl_regmap_config = {
177 .reg_bits = 32,
178 .reg_stride = 4,
179 .val_bits = 32,
180 .lock = sta2x11_regmap_lock,
181 .unlock = sta2x11_regmap_unlock,
182 .max_register = SCTL_SCRSTSTA,
183 .writeable_reg = sta2x11_sctl_writeable_reg,
Alessandro Rubini35bdd292012-04-12 10:48:44 +0200184};
185
Davide Ciminaghidba6c1a2012-11-09 15:19:59 +0100186static bool sta2x11_scr_readable_reg(struct device *dev, unsigned int reg)
187{
188 return (reg == STA2X11_SECR_CR) ||
189 __reg_within_range(reg, STA2X11_SECR_FVR0, STA2X11_SECR_FVR1);
190}
Alessandro Rubini35bdd292012-04-12 10:48:44 +0200191
Davide Ciminaghidba6c1a2012-11-09 15:19:59 +0100192static bool sta2x11_scr_writeable_reg(struct device *dev, unsigned int reg)
193{
194 return false;
195}
196
197static struct regmap_config sta2x11_scr_regmap_config = {
198 .reg_bits = 32,
199 .reg_stride = 4,
200 .val_bits = 32,
201 .lock = sta2x11_regmap_nolock,
202 .unlock = sta2x11_regmap_nolock,
203 .max_register = STA2X11_SECR_FVR1,
204 .readable_reg = sta2x11_scr_readable_reg,
205 .writeable_reg = sta2x11_scr_writeable_reg,
206};
207
Davide Ciminaghid94e2552012-11-09 15:19:53 +0100208static bool sta2x11_apbreg_readable_reg(struct device *dev, unsigned int reg)
209{
210 /* Two blocks (CAN and MLB, SARAC) 0x100 bytes apart */
211 if (reg >= APBREG_BSR_SARAC)
212 reg -= APBREG_BSR_SARAC;
213 switch (reg) {
214 case APBREG_BSR:
215 case APBREG_PAER:
216 case APBREG_PWAC:
217 case APBREG_PRAC:
218 case APBREG_PCG:
219 case APBREG_PUR:
220 case APBREG_EMU_PCG:
221 return true;
222 default:
223 return false;
224 }
225}
226
227static bool sta2x11_apbreg_writeable_reg(struct device *dev, unsigned int reg)
228{
229 if (reg >= APBREG_BSR_SARAC)
230 reg -= APBREG_BSR_SARAC;
231 if (!sta2x11_apbreg_readable_reg(dev, reg))
232 return false;
233 return reg != APBREG_PAER;
234}
235
236static struct regmap_config sta2x11_apbreg_regmap_config = {
237 .reg_bits = 32,
238 .reg_stride = 4,
239 .val_bits = 32,
240 .lock = sta2x11_regmap_lock,
241 .unlock = sta2x11_regmap_unlock,
242 .max_register = APBREG_EMU_PCG_SARAC,
243 .readable_reg = sta2x11_apbreg_readable_reg,
244 .writeable_reg = sta2x11_apbreg_writeable_reg,
245};
246
247static bool sta2x11_apb_soc_regs_readable_reg(struct device *dev,
248 unsigned int reg)
249{
250 return reg <= PCIE_SoC_INT_ROUTER_STATUS3_REG ||
251 __reg_within_range(reg, DMA_IP_CTRL_REG, SPARE3_RESERVED) ||
252 __reg_within_range(reg, MASTER_LOCK_REG,
253 SYSTEM_CONFIG_STATUS_REG) ||
254 reg == MSP_CLK_CTRL_REG ||
255 __reg_within_range(reg, COMPENSATION_REG1, TEST_CTL_REG);
256}
257
258static bool sta2x11_apb_soc_regs_writeable_reg(struct device *dev,
259 unsigned int reg)
260{
261 if (!sta2x11_apb_soc_regs_readable_reg(dev, reg))
262 return false;
263 switch (reg) {
264 case PCIE_COMMON_CLOCK_CONFIG_0_4_0:
265 case SYSTEM_CONFIG_STATUS_REG:
266 case COMPENSATION_REG1:
267 case PCIE_SoC_INT_ROUTER_STATUS0_REG...PCIE_SoC_INT_ROUTER_STATUS3_REG:
268 case PCIE_PM_STATUS_0_PORT_0_4...PCIE_PM_STATUS_7_0_EP4:
269 return false;
270 default:
271 return true;
272 }
273}
274
275static struct regmap_config sta2x11_apb_soc_regs_regmap_config = {
276 .reg_bits = 32,
277 .reg_stride = 4,
278 .val_bits = 32,
279 .lock = sta2x11_regmap_lock,
280 .unlock = sta2x11_regmap_unlock,
281 .max_register = TEST_CTL_REG,
282 .readable_reg = sta2x11_apb_soc_regs_readable_reg,
283 .writeable_reg = sta2x11_apb_soc_regs_writeable_reg,
284};
285
286static struct regmap_config *
287sta2x11_mfd_regmap_configs[sta2x11_n_mfd_plat_devs] = {
288 [sta2x11_sctl] = &sta2x11_sctl_regmap_config,
289 [sta2x11_apbreg] = &sta2x11_apbreg_regmap_config,
290 [sta2x11_apb_soc_regs] = &sta2x11_apb_soc_regs_regmap_config,
Davide Ciminaghidba6c1a2012-11-09 15:19:59 +0100291 [sta2x11_scr] = &sta2x11_scr_regmap_config,
Davide Ciminaghid94e2552012-11-09 15:19:53 +0100292};
293
Davide Ciminaghidba6c1a2012-11-09 15:19:59 +0100294/* Probe for the four platform devices */
Davide Ciminaghi1950c712012-11-09 15:19:52 +0100295
296static int sta2x11_mfd_platform_probe(struct platform_device *dev,
297 enum sta2x11_mfd_plat_dev index)
Alessandro Rubini35bdd292012-04-12 10:48:44 +0200298{
299 struct pci_dev **pdev;
300 struct sta2x11_mfd *mfd;
301 struct resource *res;
Davide Ciminaghi1950c712012-11-09 15:19:52 +0100302 const char *name = sta2x11_mfd_names[index];
Davide Ciminaghid94e2552012-11-09 15:19:53 +0100303 struct regmap_config *regmap_config = sta2x11_mfd_regmap_configs[index];
Alessandro Rubini35bdd292012-04-12 10:48:44 +0200304
Jingoo Han334a41ce2013-07-30 17:10:05 +0900305 pdev = dev_get_platdata(&dev->dev);
Alessandro Rubini35bdd292012-04-12 10:48:44 +0200306 mfd = sta2x11_mfd_find(*pdev);
307 if (!mfd)
308 return -ENODEV;
Davide Ciminaghid94e2552012-11-09 15:19:53 +0100309 if (!regmap_config)
310 return -ENODEV;
Alessandro Rubini35bdd292012-04-12 10:48:44 +0200311
312 res = platform_get_resource(dev, IORESOURCE_MEM, 0);
313 if (!res)
314 return -ENOMEM;
315
Davide Ciminaghi1950c712012-11-09 15:19:52 +0100316 if (!request_mem_region(res->start, resource_size(res), name))
Alessandro Rubini35bdd292012-04-12 10:48:44 +0200317 return -EBUSY;
318
Davide Ciminaghi1950c712012-11-09 15:19:52 +0100319 mfd->regs[index] = ioremap(res->start, resource_size(res));
320 if (!mfd->regs[index]) {
Alessandro Rubini35bdd292012-04-12 10:48:44 +0200321 release_mem_region(res->start, resource_size(res));
322 return -ENOMEM;
323 }
Davide Ciminaghid94e2552012-11-09 15:19:53 +0100324 regmap_config->lock_arg = &mfd->lock;
325 /*
326 No caching, registers could be reached both via regmap and via
327 void __iomem *
328 */
329 regmap_config->cache_type = REGCACHE_NONE;
330 mfd->regmap[index] = devm_regmap_init_mmio(&dev->dev, mfd->regs[index],
331 regmap_config);
Wei Yongjunec9e4ba2013-12-07 14:03:00 +0800332 WARN_ON(IS_ERR(mfd->regmap[index]));
Davide Ciminaghid94e2552012-11-09 15:19:53 +0100333
Alessandro Rubini35bdd292012-04-12 10:48:44 +0200334 return 0;
335}
336
Davide Ciminaghi1950c712012-11-09 15:19:52 +0100337static int sta2x11_sctl_probe(struct platform_device *dev)
338{
339 return sta2x11_mfd_platform_probe(dev, sta2x11_sctl);
340}
341
Alessandro Rubini35bdd292012-04-12 10:48:44 +0200342static int sta2x11_apbreg_probe(struct platform_device *dev)
343{
Davide Ciminaghi1950c712012-11-09 15:19:52 +0100344 return sta2x11_mfd_platform_probe(dev, sta2x11_apbreg);
Alessandro Rubini35bdd292012-04-12 10:48:44 +0200345}
346
Davide Ciminaghi1950c712012-11-09 15:19:52 +0100347static int sta2x11_apb_soc_regs_probe(struct platform_device *dev)
348{
349 return sta2x11_mfd_platform_probe(dev, sta2x11_apb_soc_regs);
350}
351
Davide Ciminaghidba6c1a2012-11-09 15:19:59 +0100352static int sta2x11_scr_probe(struct platform_device *dev)
353{
354 return sta2x11_mfd_platform_probe(dev, sta2x11_scr);
355}
356
Davide Ciminaghi1950c712012-11-09 15:19:52 +0100357/* The three platform drivers */
Alessandro Rubini35bdd292012-04-12 10:48:44 +0200358static struct platform_driver sta2x11_sctl_platform_driver = {
359 .driver = {
Davide Ciminaghib18adaf2012-11-09 15:19:55 +0100360 .name = STA2X11_MFD_SCTL_NAME,
Alessandro Rubini35bdd292012-04-12 10:48:44 +0200361 },
362 .probe = sta2x11_sctl_probe,
363};
364
Alessandro Rubini35bdd292012-04-12 10:48:44 +0200365static struct platform_driver sta2x11_platform_driver = {
366 .driver = {
Davide Ciminaghib18adaf2012-11-09 15:19:55 +0100367 .name = STA2X11_MFD_APBREG_NAME,
Alessandro Rubini35bdd292012-04-12 10:48:44 +0200368 },
369 .probe = sta2x11_apbreg_probe,
370};
371
Davide Ciminaghi1950c712012-11-09 15:19:52 +0100372static struct platform_driver sta2x11_apb_soc_regs_platform_driver = {
373 .driver = {
Davide Ciminaghib18adaf2012-11-09 15:19:55 +0100374 .name = STA2X11_MFD_APB_SOC_REGS_NAME,
Davide Ciminaghi1950c712012-11-09 15:19:52 +0100375 },
376 .probe = sta2x11_apb_soc_regs_probe,
377};
378
Davide Ciminaghidba6c1a2012-11-09 15:19:59 +0100379static struct platform_driver sta2x11_scr_platform_driver = {
380 .driver = {
381 .name = STA2X11_MFD_SCR_NAME,
Davide Ciminaghidba6c1a2012-11-09 15:19:59 +0100382 },
383 .probe = sta2x11_scr_probe,
384};
385
Thierry Redingd91d76d2015-12-02 17:28:11 +0100386static struct platform_driver * const drivers[] = {
387 &sta2x11_platform_driver,
388 &sta2x11_sctl_platform_driver,
389 &sta2x11_apb_soc_regs_platform_driver,
390 &sta2x11_scr_platform_driver,
391};
Davide Ciminaghidba6c1a2012-11-09 15:19:59 +0100392
Thierry Redingd91d76d2015-12-02 17:28:11 +0100393static int __init sta2x11_drivers_init(void)
394{
395 return platform_register_drivers(drivers, ARRAY_SIZE(drivers));
396}
Davide Ciminaghidba6c1a2012-11-09 15:19:59 +0100397
Alessandro Rubini35bdd292012-04-12 10:48:44 +0200398/*
Davide Ciminaghi1950c712012-11-09 15:19:52 +0100399 * What follows are the PCI devices that host the above pdevs.
Alessandro Rubini35bdd292012-04-12 10:48:44 +0200400 * Each logic block is 4kB and they are all consecutive: we use this info.
401 */
402
Davide Ciminaghi1950c712012-11-09 15:19:52 +0100403/* Mfd 0 device */
404
405/* Mfd 0, Bar 0 */
406enum mfd0_bar0_cells {
Alessandro Rubini35bdd292012-04-12 10:48:44 +0200407 STA2X11_GPIO_0 = 0,
408 STA2X11_GPIO_1,
409 STA2X11_GPIO_2,
410 STA2X11_GPIO_3,
411 STA2X11_SCTL,
412 STA2X11_SCR,
413 STA2X11_TIME,
414};
Davide Ciminaghi1950c712012-11-09 15:19:52 +0100415/* Mfd 0 , Bar 1 */
416enum mfd0_bar1_cells {
Alessandro Rubini35bdd292012-04-12 10:48:44 +0200417 STA2X11_APBREG = 0,
418};
419#define CELL_4K(_name, _cell) { \
420 .name = _name, \
421 .start = _cell * 4096, .end = _cell * 4096 + 4095, \
422 .flags = IORESOURCE_MEM, \
423 }
424
Bill Pembertona73e5df2012-11-19 13:25:09 -0500425static const struct resource gpio_resources[] = {
Alessandro Rubini35bdd292012-04-12 10:48:44 +0200426 {
Davide Ciminaghib18adaf2012-11-09 15:19:55 +0100427 /* 4 consecutive cells, 1 driver */
428 .name = STA2X11_MFD_GPIO_NAME,
Alessandro Rubini35bdd292012-04-12 10:48:44 +0200429 .start = 0,
430 .end = (4 * 4096) - 1,
431 .flags = IORESOURCE_MEM,
432 }
433};
Bill Pembertona73e5df2012-11-19 13:25:09 -0500434static const struct resource sctl_resources[] = {
Davide Ciminaghib18adaf2012-11-09 15:19:55 +0100435 CELL_4K(STA2X11_MFD_SCTL_NAME, STA2X11_SCTL),
Alessandro Rubini35bdd292012-04-12 10:48:44 +0200436};
Bill Pembertona73e5df2012-11-19 13:25:09 -0500437static const struct resource scr_resources[] = {
Davide Ciminaghib18adaf2012-11-09 15:19:55 +0100438 CELL_4K(STA2X11_MFD_SCR_NAME, STA2X11_SCR),
Alessandro Rubini35bdd292012-04-12 10:48:44 +0200439};
Bill Pembertona73e5df2012-11-19 13:25:09 -0500440static const struct resource time_resources[] = {
Davide Ciminaghib18adaf2012-11-09 15:19:55 +0100441 CELL_4K(STA2X11_MFD_TIME_NAME, STA2X11_TIME),
Alessandro Rubini35bdd292012-04-12 10:48:44 +0200442};
443
Bill Pembertona73e5df2012-11-19 13:25:09 -0500444static const struct resource apbreg_resources[] = {
Davide Ciminaghib18adaf2012-11-09 15:19:55 +0100445 CELL_4K(STA2X11_MFD_APBREG_NAME, STA2X11_APBREG),
Alessandro Rubini35bdd292012-04-12 10:48:44 +0200446};
447
448#define DEV(_name, _r) \
449 { .name = _name, .num_resources = ARRAY_SIZE(_r), .resources = _r, }
450
Linus Torvalds2dfea382012-12-16 18:55:20 -0800451static struct mfd_cell sta2x11_mfd0_bar0[] = {
Davide Ciminaghib18adaf2012-11-09 15:19:55 +0100452 /* offset 0: we add pdata later */
453 DEV(STA2X11_MFD_GPIO_NAME, gpio_resources),
454 DEV(STA2X11_MFD_SCTL_NAME, sctl_resources),
455 DEV(STA2X11_MFD_SCR_NAME, scr_resources),
456 DEV(STA2X11_MFD_TIME_NAME, time_resources),
Alessandro Rubini35bdd292012-04-12 10:48:44 +0200457};
458
Linus Torvalds2dfea382012-12-16 18:55:20 -0800459static struct mfd_cell sta2x11_mfd0_bar1[] = {
Davide Ciminaghib18adaf2012-11-09 15:19:55 +0100460 DEV(STA2X11_MFD_APBREG_NAME, apbreg_resources),
Alessandro Rubini35bdd292012-04-12 10:48:44 +0200461};
462
Davide Ciminaghi1950c712012-11-09 15:19:52 +0100463/* Mfd 1 devices */
464
465/* Mfd 1, Bar 0 */
466enum mfd1_bar0_cells {
467 STA2X11_VIC = 0,
468};
469
470/* Mfd 1, Bar 1 */
471enum mfd1_bar1_cells {
472 STA2X11_APB_SOC_REGS = 0,
473};
474
Greg Kroah-Hartman612b95c2012-12-21 15:03:15 -0800475static const struct resource vic_resources[] = {
Davide Ciminaghib18adaf2012-11-09 15:19:55 +0100476 CELL_4K(STA2X11_MFD_VIC_NAME, STA2X11_VIC),
Davide Ciminaghi1950c712012-11-09 15:19:52 +0100477};
478
Greg Kroah-Hartman612b95c2012-12-21 15:03:15 -0800479static const struct resource apb_soc_regs_resources[] = {
Davide Ciminaghib18adaf2012-11-09 15:19:55 +0100480 CELL_4K(STA2X11_MFD_APB_SOC_REGS_NAME, STA2X11_APB_SOC_REGS),
Davide Ciminaghi1950c712012-11-09 15:19:52 +0100481};
482
Greg Kroah-Hartman612b95c2012-12-21 15:03:15 -0800483static struct mfd_cell sta2x11_mfd1_bar0[] = {
Davide Ciminaghib18adaf2012-11-09 15:19:55 +0100484 DEV(STA2X11_MFD_VIC_NAME, vic_resources),
Davide Ciminaghi1950c712012-11-09 15:19:52 +0100485};
486
Greg Kroah-Hartman612b95c2012-12-21 15:03:15 -0800487static struct mfd_cell sta2x11_mfd1_bar1[] = {
Davide Ciminaghib18adaf2012-11-09 15:19:55 +0100488 DEV(STA2X11_MFD_APB_SOC_REGS_NAME, apb_soc_regs_resources),
Davide Ciminaghi1950c712012-11-09 15:19:52 +0100489};
490
491
Alessandro Rubini35bdd292012-04-12 10:48:44 +0200492static int sta2x11_mfd_suspend(struct pci_dev *pdev, pm_message_t state)
493{
494 pci_save_state(pdev);
495 pci_disable_device(pdev);
496 pci_set_power_state(pdev, pci_choose_state(pdev, state));
497
498 return 0;
499}
500
501static int sta2x11_mfd_resume(struct pci_dev *pdev)
502{
503 int err;
504
Julia Lawall4d1d9982014-01-03 00:40:29 +0100505 pci_set_power_state(pdev, PCI_D0);
Alessandro Rubini35bdd292012-04-12 10:48:44 +0200506 err = pci_enable_device(pdev);
507 if (err)
508 return err;
509 pci_restore_state(pdev);
510
511 return 0;
512}
513
Davide Ciminaghi1950c712012-11-09 15:19:52 +0100514struct sta2x11_mfd_bar_setup_data {
515 struct mfd_cell *cells;
516 int ncells;
517};
518
519struct sta2x11_mfd_setup_data {
520 struct sta2x11_mfd_bar_setup_data bars[2];
521};
522
523#define STA2X11_MFD0 0
524#define STA2X11_MFD1 1
525
526static struct sta2x11_mfd_setup_data mfd_setup_data[] = {
527 /* Mfd 0: gpio, sctl, scr, timers / apbregs */
528 [STA2X11_MFD0] = {
529 .bars = {
530 [0] = {
531 .cells = sta2x11_mfd0_bar0,
532 .ncells = ARRAY_SIZE(sta2x11_mfd0_bar0),
533 },
534 [1] = {
535 .cells = sta2x11_mfd0_bar1,
536 .ncells = ARRAY_SIZE(sta2x11_mfd0_bar1),
537 },
538 },
539 },
540 /* Mfd 1: vic / apb-soc-regs */
541 [STA2X11_MFD1] = {
542 .bars = {
543 [0] = {
544 .cells = sta2x11_mfd1_bar0,
545 .ncells = ARRAY_SIZE(sta2x11_mfd1_bar0),
546 },
547 [1] = {
548 .cells = sta2x11_mfd1_bar1,
549 .ncells = ARRAY_SIZE(sta2x11_mfd1_bar1),
550 },
551 },
552 },
553};
554
Linus Torvalds2dfea382012-12-16 18:55:20 -0800555static void sta2x11_mfd_setup(struct pci_dev *pdev,
556 struct sta2x11_mfd_setup_data *sd)
Davide Ciminaghi1950c712012-11-09 15:19:52 +0100557{
558 int i, j;
559 for (i = 0; i < ARRAY_SIZE(sd->bars); i++)
560 for (j = 0; j < sd->bars[i].ncells; j++) {
561 sd->bars[i].cells[j].pdata_size = sizeof(pdev);
562 sd->bars[i].cells[j].platform_data = &pdev;
563 }
564}
565
Bill Pembertonf791be42012-11-19 13:23:04 -0500566static int sta2x11_mfd_probe(struct pci_dev *pdev,
Linus Torvalds2dfea382012-12-16 18:55:20 -0800567 const struct pci_device_id *pci_id)
Alessandro Rubini35bdd292012-04-12 10:48:44 +0200568{
569 int err, i;
Davide Ciminaghi1950c712012-11-09 15:19:52 +0100570 struct sta2x11_mfd_setup_data *setup_data;
Alessandro Rubini35bdd292012-04-12 10:48:44 +0200571
572 dev_info(&pdev->dev, "%s\n", __func__);
573
574 err = pci_enable_device(pdev);
575 if (err) {
576 dev_err(&pdev->dev, "Can't enable device.\n");
577 return err;
578 }
579
580 err = pci_enable_msi(pdev);
581 if (err)
582 dev_info(&pdev->dev, "Enable msi failed\n");
583
Davide Ciminaghi1950c712012-11-09 15:19:52 +0100584 setup_data = pci_id->device == PCI_DEVICE_ID_STMICRO_GPIO ?
585 &mfd_setup_data[STA2X11_MFD0] :
586 &mfd_setup_data[STA2X11_MFD1];
Alessandro Rubini35bdd292012-04-12 10:48:44 +0200587
588 /* platform data is the pci device for all of them */
Davide Ciminaghi1950c712012-11-09 15:19:52 +0100589 sta2x11_mfd_setup(pdev, setup_data);
Alessandro Rubini35bdd292012-04-12 10:48:44 +0200590
591 /* Record this pdev before mfd_add_devices: their probe looks for it */
Davide Ciminaghi8ec86a32012-11-09 15:19:56 +0100592 if (!sta2x11_mfd_find(pdev))
593 sta2x11_mfd_add(pdev, GFP_ATOMIC);
Alessandro Rubini35bdd292012-04-12 10:48:44 +0200594
Davide Ciminaghi1950c712012-11-09 15:19:52 +0100595 /* Just 2 bars for all mfd's at present */
596 for (i = 0; i < 2; i++) {
597 err = mfd_add_devices(&pdev->dev, -1,
598 setup_data->bars[i].cells,
599 setup_data->bars[i].ncells,
600 &pdev->resource[i],
601 0, NULL);
602 if (err) {
603 dev_err(&pdev->dev,
604 "mfd_add_devices[%d] failed: %d\n", i, err);
605 goto err_disable;
606 }
Alessandro Rubini35bdd292012-04-12 10:48:44 +0200607 }
608
609 return 0;
610
611err_disable:
612 mfd_remove_devices(&pdev->dev);
613 pci_disable_device(pdev);
614 pci_disable_msi(pdev);
615 return err;
616}
617
Jingoo Han36fcd062013-12-03 08:15:39 +0900618static const struct pci_device_id sta2x11_mfd_tbl[] = {
Alessandro Rubini35bdd292012-04-12 10:48:44 +0200619 {PCI_DEVICE(PCI_VENDOR_ID_STMICRO, PCI_DEVICE_ID_STMICRO_GPIO)},
Davide Ciminaghi1950c712012-11-09 15:19:52 +0100620 {PCI_DEVICE(PCI_VENDOR_ID_STMICRO, PCI_DEVICE_ID_STMICRO_VIC)},
Alessandro Rubini35bdd292012-04-12 10:48:44 +0200621 {0,},
622};
623
624static struct pci_driver sta2x11_mfd_driver = {
625 .name = "sta2x11-mfd",
626 .id_table = sta2x11_mfd_tbl,
627 .probe = sta2x11_mfd_probe,
628 .suspend = sta2x11_mfd_suspend,
629 .resume = sta2x11_mfd_resume,
630};
631
632static int __init sta2x11_mfd_init(void)
633{
634 pr_info("%s\n", __func__);
635 return pci_register_driver(&sta2x11_mfd_driver);
636}
637
638/*
639 * All of this must be ready before "normal" devices like MMCI appear.
640 * But MFD (the pci device) can't be too early. The following choice
641 * prepares platform drivers very early and probe the PCI device later,
642 * but before other PCI devices.
643 */
Thierry Redingd91d76d2015-12-02 17:28:11 +0100644subsys_initcall(sta2x11_drivers_init);
Alessandro Rubini35bdd292012-04-12 10:48:44 +0200645rootfs_initcall(sta2x11_mfd_init);