Alessandro Rubini | 35bdd29 | 2012-04-12 10:48:44 +0200 | [diff] [blame] | 1 | /* |
| 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 Rubini | 35bdd29 | 2012-04-12 10:48:44 +0200 | [diff] [blame] | 30 | #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 Ciminaghi | d94e255 | 2012-11-09 15:19:53 +0100 | [diff] [blame] | 34 | #include <linux/regmap.h> |
Alessandro Rubini | 35bdd29 | 2012-04-12 10:48:44 +0200 | [diff] [blame] | 35 | |
| 36 | #include <asm/sta2x11.h> |
| 37 | |
Davide Ciminaghi | d94e255 | 2012-11-09 15:19:53 +0100 | [diff] [blame] | 38 | static 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 Rubini | 35bdd29 | 2012-04-12 10:48:44 +0200 | [diff] [blame] | 45 | /* This describes STA2X11 MFD chip for us, we may have several */ |
| 46 | struct sta2x11_mfd { |
| 47 | struct sta2x11_instance *instance; |
Davide Ciminaghi | d94e255 | 2012-11-09 15:19:53 +0100 | [diff] [blame] | 48 | struct regmap *regmap[sta2x11_n_mfd_plat_devs]; |
Davide Ciminaghi | e885ba29 | 2012-11-09 15:19:58 +0100 | [diff] [blame^] | 49 | spinlock_t lock[sta2x11_n_mfd_plat_devs]; |
Alessandro Rubini | 35bdd29 | 2012-04-12 10:48:44 +0200 | [diff] [blame] | 50 | struct list_head list; |
Davide Ciminaghi | 1950c71 | 2012-11-09 15:19:52 +0100 | [diff] [blame] | 51 | void __iomem *regs[sta2x11_n_mfd_plat_devs]; |
Alessandro Rubini | 35bdd29 | 2012-04-12 10:48:44 +0200 | [diff] [blame] | 52 | }; |
| 53 | |
| 54 | static LIST_HEAD(sta2x11_mfd_list); |
| 55 | |
| 56 | /* Three functions to act on the list */ |
| 57 | static 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 | |
| 79 | static int __devinit sta2x11_mfd_add(struct pci_dev *pdev, gfp_t flags) |
| 80 | { |
Davide Ciminaghi | e885ba29 | 2012-11-09 15:19:58 +0100 | [diff] [blame^] | 81 | int i; |
Alessandro Rubini | 35bdd29 | 2012-04-12 10:48:44 +0200 | [diff] [blame] | 82 | 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 Ciminaghi | e885ba29 | 2012-11-09 15:19:58 +0100 | [diff] [blame^] | 94 | for (i = 0; i < ARRAY_SIZE(mfd->lock); i++) |
| 95 | spin_lock_init(&mfd->lock[i]); |
Alessandro Rubini | 35bdd29 | 2012-04-12 10:48:44 +0200 | [diff] [blame] | 96 | mfd->instance = instance; |
| 97 | list_add(&mfd->list, &sta2x11_mfd_list); |
| 98 | return 0; |
| 99 | } |
| 100 | |
| 101 | static 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 Ciminaghi | 1950c71 | 2012-11-09 15:19:52 +0100 | [diff] [blame] | 112 | /* This function is exported and is not expected to fail */ |
| 113 | u32 __sta2x11_mfd_mask(struct pci_dev *pdev, u32 reg, u32 mask, u32 val, |
| 114 | enum sta2x11_mfd_plat_dev index) |
Alessandro Rubini | 35bdd29 | 2012-04-12 10:48:44 +0200 | [diff] [blame] | 115 | { |
| 116 | struct sta2x11_mfd *mfd = sta2x11_mfd_find(pdev); |
| 117 | u32 r; |
| 118 | unsigned long flags; |
Davide Ciminaghi | 1950c71 | 2012-11-09 15:19:52 +0100 | [diff] [blame] | 119 | void __iomem *regs = mfd->regs[index]; |
Alessandro Rubini | 35bdd29 | 2012-04-12 10:48:44 +0200 | [diff] [blame] | 120 | |
| 121 | if (!mfd) { |
| 122 | dev_warn(&pdev->dev, ": can't access sctl regs\n"); |
| 123 | return 0; |
| 124 | } |
Davide Ciminaghi | 1950c71 | 2012-11-09 15:19:52 +0100 | [diff] [blame] | 125 | if (!regs) { |
Alessandro Rubini | 35bdd29 | 2012-04-12 10:48:44 +0200 | [diff] [blame] | 126 | dev_warn(&pdev->dev, ": system ctl not initialized\n"); |
| 127 | return 0; |
| 128 | } |
Davide Ciminaghi | e885ba29 | 2012-11-09 15:19:58 +0100 | [diff] [blame^] | 129 | spin_lock_irqsave(&mfd->lock[index], flags); |
Davide Ciminaghi | 1950c71 | 2012-11-09 15:19:52 +0100 | [diff] [blame] | 130 | r = readl(regs + reg); |
Alessandro Rubini | 35bdd29 | 2012-04-12 10:48:44 +0200 | [diff] [blame] | 131 | r &= ~mask; |
| 132 | r |= val; |
| 133 | if (mask) |
Davide Ciminaghi | 1950c71 | 2012-11-09 15:19:52 +0100 | [diff] [blame] | 134 | writel(r, regs + reg); |
Davide Ciminaghi | e885ba29 | 2012-11-09 15:19:58 +0100 | [diff] [blame^] | 135 | spin_unlock_irqrestore(&mfd->lock[index], flags); |
Alessandro Rubini | 35bdd29 | 2012-04-12 10:48:44 +0200 | [diff] [blame] | 136 | return r; |
| 137 | } |
Davide Ciminaghi | 1950c71 | 2012-11-09 15:19:52 +0100 | [diff] [blame] | 138 | EXPORT_SYMBOL(__sta2x11_mfd_mask); |
Alessandro Rubini | 35bdd29 | 2012-04-12 10:48:44 +0200 | [diff] [blame] | 139 | |
Davide Ciminaghi | 29f5b5a | 2012-11-09 15:19:54 +0100 | [diff] [blame] | 140 | int 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 | } |
| 160 | EXPORT_SYMBOL(sta2x11_mfd_get_regs_data); |
| 161 | |
Davide Ciminaghi | d94e255 | 2012-11-09 15:19:53 +0100 | [diff] [blame] | 162 | /* |
| 163 | * Special sta2x11-mfd regmap lock/unlock functions |
| 164 | */ |
Alessandro Rubini | 35bdd29 | 2012-04-12 10:48:44 +0200 | [diff] [blame] | 165 | |
Davide Ciminaghi | d94e255 | 2012-11-09 15:19:53 +0100 | [diff] [blame] | 166 | static void sta2x11_regmap_lock(void *__lock) |
| 167 | { |
| 168 | spinlock_t *lock = __lock; |
| 169 | spin_lock(lock); |
| 170 | } |
Alessandro Rubini | 35bdd29 | 2012-04-12 10:48:44 +0200 | [diff] [blame] | 171 | |
Davide Ciminaghi | d94e255 | 2012-11-09 15:19:53 +0100 | [diff] [blame] | 172 | static void sta2x11_regmap_unlock(void *__lock) |
| 173 | { |
| 174 | spinlock_t *lock = __lock; |
| 175 | spin_unlock(lock); |
| 176 | } |
Davide Ciminaghi | 1950c71 | 2012-11-09 15:19:52 +0100 | [diff] [blame] | 177 | |
| 178 | static const char *sta2x11_mfd_names[sta2x11_n_mfd_plat_devs] = { |
Davide Ciminaghi | b18adaf | 2012-11-09 15:19:55 +0100 | [diff] [blame] | 179 | [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 Ciminaghi | 1950c71 | 2012-11-09 15:19:52 +0100 | [diff] [blame] | 182 | }; |
| 183 | |
Davide Ciminaghi | d94e255 | 2012-11-09 15:19:53 +0100 | [diff] [blame] | 184 | static bool sta2x11_sctl_writeable_reg(struct device *dev, unsigned int reg) |
| 185 | { |
| 186 | return !__reg_within_range(reg, SCTL_SCPCIECSBRST, SCTL_SCRSTSTA); |
| 187 | } |
| 188 | |
| 189 | static 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 | |
| 199 | static 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 | |
| 218 | static 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 | |
| 227 | static 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 | |
| 238 | static 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 | |
| 249 | static 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 | |
| 266 | static 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 | |
| 277 | static struct regmap_config * |
| 278 | sta2x11_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 Ciminaghi | 1950c71 | 2012-11-09 15:19:52 +0100 | [diff] [blame] | 284 | /* Probe for the three platform devices */ |
| 285 | |
| 286 | static int sta2x11_mfd_platform_probe(struct platform_device *dev, |
| 287 | enum sta2x11_mfd_plat_dev index) |
Alessandro Rubini | 35bdd29 | 2012-04-12 10:48:44 +0200 | [diff] [blame] | 288 | { |
| 289 | struct pci_dev **pdev; |
| 290 | struct sta2x11_mfd *mfd; |
| 291 | struct resource *res; |
Davide Ciminaghi | 1950c71 | 2012-11-09 15:19:52 +0100 | [diff] [blame] | 292 | const char *name = sta2x11_mfd_names[index]; |
Davide Ciminaghi | d94e255 | 2012-11-09 15:19:53 +0100 | [diff] [blame] | 293 | struct regmap_config *regmap_config = sta2x11_mfd_regmap_configs[index]; |
Alessandro Rubini | 35bdd29 | 2012-04-12 10:48:44 +0200 | [diff] [blame] | 294 | |
| 295 | pdev = dev->dev.platform_data; |
| 296 | mfd = sta2x11_mfd_find(*pdev); |
| 297 | if (!mfd) |
| 298 | return -ENODEV; |
Davide Ciminaghi | d94e255 | 2012-11-09 15:19:53 +0100 | [diff] [blame] | 299 | if (!regmap_config) |
| 300 | return -ENODEV; |
Alessandro Rubini | 35bdd29 | 2012-04-12 10:48:44 +0200 | [diff] [blame] | 301 | |
| 302 | res = platform_get_resource(dev, IORESOURCE_MEM, 0); |
| 303 | if (!res) |
| 304 | return -ENOMEM; |
| 305 | |
Davide Ciminaghi | 1950c71 | 2012-11-09 15:19:52 +0100 | [diff] [blame] | 306 | if (!request_mem_region(res->start, resource_size(res), name)) |
Alessandro Rubini | 35bdd29 | 2012-04-12 10:48:44 +0200 | [diff] [blame] | 307 | return -EBUSY; |
| 308 | |
Davide Ciminaghi | 1950c71 | 2012-11-09 15:19:52 +0100 | [diff] [blame] | 309 | mfd->regs[index] = ioremap(res->start, resource_size(res)); |
| 310 | if (!mfd->regs[index]) { |
Alessandro Rubini | 35bdd29 | 2012-04-12 10:48:44 +0200 | [diff] [blame] | 311 | release_mem_region(res->start, resource_size(res)); |
| 312 | return -ENOMEM; |
| 313 | } |
Davide Ciminaghi | d94e255 | 2012-11-09 15:19:53 +0100 | [diff] [blame] | 314 | 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 Rubini | 35bdd29 | 2012-04-12 10:48:44 +0200 | [diff] [blame] | 324 | return 0; |
| 325 | } |
| 326 | |
Davide Ciminaghi | 1950c71 | 2012-11-09 15:19:52 +0100 | [diff] [blame] | 327 | static int sta2x11_sctl_probe(struct platform_device *dev) |
| 328 | { |
| 329 | return sta2x11_mfd_platform_probe(dev, sta2x11_sctl); |
| 330 | } |
| 331 | |
Alessandro Rubini | 35bdd29 | 2012-04-12 10:48:44 +0200 | [diff] [blame] | 332 | static int sta2x11_apbreg_probe(struct platform_device *dev) |
| 333 | { |
Davide Ciminaghi | 1950c71 | 2012-11-09 15:19:52 +0100 | [diff] [blame] | 334 | return sta2x11_mfd_platform_probe(dev, sta2x11_apbreg); |
Alessandro Rubini | 35bdd29 | 2012-04-12 10:48:44 +0200 | [diff] [blame] | 335 | } |
| 336 | |
Davide Ciminaghi | 1950c71 | 2012-11-09 15:19:52 +0100 | [diff] [blame] | 337 | static 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 Rubini | 35bdd29 | 2012-04-12 10:48:44 +0200 | [diff] [blame] | 343 | static struct platform_driver sta2x11_sctl_platform_driver = { |
| 344 | .driver = { |
Davide Ciminaghi | b18adaf | 2012-11-09 15:19:55 +0100 | [diff] [blame] | 345 | .name = STA2X11_MFD_SCTL_NAME, |
Alessandro Rubini | 35bdd29 | 2012-04-12 10:48:44 +0200 | [diff] [blame] | 346 | .owner = THIS_MODULE, |
| 347 | }, |
| 348 | .probe = sta2x11_sctl_probe, |
| 349 | }; |
| 350 | |
| 351 | static int __init sta2x11_sctl_init(void) |
| 352 | { |
| 353 | pr_info("%s\n", __func__); |
| 354 | return platform_driver_register(&sta2x11_sctl_platform_driver); |
| 355 | } |
| 356 | |
| 357 | static struct platform_driver sta2x11_platform_driver = { |
| 358 | .driver = { |
Davide Ciminaghi | b18adaf | 2012-11-09 15:19:55 +0100 | [diff] [blame] | 359 | .name = STA2X11_MFD_APBREG_NAME, |
Alessandro Rubini | 35bdd29 | 2012-04-12 10:48:44 +0200 | [diff] [blame] | 360 | .owner = THIS_MODULE, |
| 361 | }, |
| 362 | .probe = sta2x11_apbreg_probe, |
| 363 | }; |
| 364 | |
| 365 | static int __init sta2x11_apbreg_init(void) |
| 366 | { |
| 367 | pr_info("%s\n", __func__); |
| 368 | return platform_driver_register(&sta2x11_platform_driver); |
| 369 | } |
| 370 | |
Davide Ciminaghi | 1950c71 | 2012-11-09 15:19:52 +0100 | [diff] [blame] | 371 | static struct platform_driver sta2x11_apb_soc_regs_platform_driver = { |
| 372 | .driver = { |
Davide Ciminaghi | b18adaf | 2012-11-09 15:19:55 +0100 | [diff] [blame] | 373 | .name = STA2X11_MFD_APB_SOC_REGS_NAME, |
Davide Ciminaghi | 1950c71 | 2012-11-09 15:19:52 +0100 | [diff] [blame] | 374 | .owner = THIS_MODULE, |
| 375 | }, |
| 376 | .probe = sta2x11_apb_soc_regs_probe, |
| 377 | }; |
| 378 | |
| 379 | static 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 Rubini | 35bdd29 | 2012-04-12 10:48:44 +0200 | [diff] [blame] | 385 | /* |
Davide Ciminaghi | 1950c71 | 2012-11-09 15:19:52 +0100 | [diff] [blame] | 386 | * What follows are the PCI devices that host the above pdevs. |
Alessandro Rubini | 35bdd29 | 2012-04-12 10:48:44 +0200 | [diff] [blame] | 387 | * Each logic block is 4kB and they are all consecutive: we use this info. |
| 388 | */ |
| 389 | |
Davide Ciminaghi | 1950c71 | 2012-11-09 15:19:52 +0100 | [diff] [blame] | 390 | /* Mfd 0 device */ |
| 391 | |
| 392 | /* Mfd 0, Bar 0 */ |
| 393 | enum mfd0_bar0_cells { |
Alessandro Rubini | 35bdd29 | 2012-04-12 10:48:44 +0200 | [diff] [blame] | 394 | 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 Ciminaghi | 1950c71 | 2012-11-09 15:19:52 +0100 | [diff] [blame] | 402 | /* Mfd 0 , Bar 1 */ |
| 403 | enum mfd0_bar1_cells { |
Alessandro Rubini | 35bdd29 | 2012-04-12 10:48:44 +0200 | [diff] [blame] | 404 | 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 | |
| 412 | static const __devinitconst struct resource gpio_resources[] = { |
| 413 | { |
Davide Ciminaghi | b18adaf | 2012-11-09 15:19:55 +0100 | [diff] [blame] | 414 | /* 4 consecutive cells, 1 driver */ |
| 415 | .name = STA2X11_MFD_GPIO_NAME, |
Alessandro Rubini | 35bdd29 | 2012-04-12 10:48:44 +0200 | [diff] [blame] | 416 | .start = 0, |
| 417 | .end = (4 * 4096) - 1, |
| 418 | .flags = IORESOURCE_MEM, |
| 419 | } |
| 420 | }; |
| 421 | static const __devinitconst struct resource sctl_resources[] = { |
Davide Ciminaghi | b18adaf | 2012-11-09 15:19:55 +0100 | [diff] [blame] | 422 | CELL_4K(STA2X11_MFD_SCTL_NAME, STA2X11_SCTL), |
Alessandro Rubini | 35bdd29 | 2012-04-12 10:48:44 +0200 | [diff] [blame] | 423 | }; |
| 424 | static const __devinitconst struct resource scr_resources[] = { |
Davide Ciminaghi | b18adaf | 2012-11-09 15:19:55 +0100 | [diff] [blame] | 425 | CELL_4K(STA2X11_MFD_SCR_NAME, STA2X11_SCR), |
Alessandro Rubini | 35bdd29 | 2012-04-12 10:48:44 +0200 | [diff] [blame] | 426 | }; |
| 427 | static const __devinitconst struct resource time_resources[] = { |
Davide Ciminaghi | b18adaf | 2012-11-09 15:19:55 +0100 | [diff] [blame] | 428 | CELL_4K(STA2X11_MFD_TIME_NAME, STA2X11_TIME), |
Alessandro Rubini | 35bdd29 | 2012-04-12 10:48:44 +0200 | [diff] [blame] | 429 | }; |
| 430 | |
| 431 | static const __devinitconst struct resource apbreg_resources[] = { |
Davide Ciminaghi | b18adaf | 2012-11-09 15:19:55 +0100 | [diff] [blame] | 432 | CELL_4K(STA2X11_MFD_APBREG_NAME, STA2X11_APBREG), |
Alessandro Rubini | 35bdd29 | 2012-04-12 10:48:44 +0200 | [diff] [blame] | 433 | }; |
| 434 | |
| 435 | #define DEV(_name, _r) \ |
| 436 | { .name = _name, .num_resources = ARRAY_SIZE(_r), .resources = _r, } |
| 437 | |
Davide Ciminaghi | 1950c71 | 2012-11-09 15:19:52 +0100 | [diff] [blame] | 438 | static __devinitdata struct mfd_cell sta2x11_mfd0_bar0[] = { |
Davide Ciminaghi | b18adaf | 2012-11-09 15:19:55 +0100 | [diff] [blame] | 439 | /* 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 Rubini | 35bdd29 | 2012-04-12 10:48:44 +0200 | [diff] [blame] | 444 | }; |
| 445 | |
Davide Ciminaghi | 1950c71 | 2012-11-09 15:19:52 +0100 | [diff] [blame] | 446 | static __devinitdata struct mfd_cell sta2x11_mfd0_bar1[] = { |
Davide Ciminaghi | b18adaf | 2012-11-09 15:19:55 +0100 | [diff] [blame] | 447 | DEV(STA2X11_MFD_APBREG_NAME, apbreg_resources), |
Alessandro Rubini | 35bdd29 | 2012-04-12 10:48:44 +0200 | [diff] [blame] | 448 | }; |
| 449 | |
Davide Ciminaghi | 1950c71 | 2012-11-09 15:19:52 +0100 | [diff] [blame] | 450 | /* Mfd 1 devices */ |
| 451 | |
| 452 | /* Mfd 1, Bar 0 */ |
| 453 | enum mfd1_bar0_cells { |
| 454 | STA2X11_VIC = 0, |
| 455 | }; |
| 456 | |
| 457 | /* Mfd 1, Bar 1 */ |
| 458 | enum mfd1_bar1_cells { |
| 459 | STA2X11_APB_SOC_REGS = 0, |
| 460 | }; |
| 461 | |
| 462 | static const __devinitconst struct resource vic_resources[] = { |
Davide Ciminaghi | b18adaf | 2012-11-09 15:19:55 +0100 | [diff] [blame] | 463 | CELL_4K(STA2X11_MFD_VIC_NAME, STA2X11_VIC), |
Davide Ciminaghi | 1950c71 | 2012-11-09 15:19:52 +0100 | [diff] [blame] | 464 | }; |
| 465 | |
| 466 | static const __devinitconst struct resource apb_soc_regs_resources[] = { |
Davide Ciminaghi | b18adaf | 2012-11-09 15:19:55 +0100 | [diff] [blame] | 467 | CELL_4K(STA2X11_MFD_APB_SOC_REGS_NAME, STA2X11_APB_SOC_REGS), |
Davide Ciminaghi | 1950c71 | 2012-11-09 15:19:52 +0100 | [diff] [blame] | 468 | }; |
| 469 | |
| 470 | static __devinitdata struct mfd_cell sta2x11_mfd1_bar0[] = { |
Davide Ciminaghi | b18adaf | 2012-11-09 15:19:55 +0100 | [diff] [blame] | 471 | DEV(STA2X11_MFD_VIC_NAME, vic_resources), |
Davide Ciminaghi | 1950c71 | 2012-11-09 15:19:52 +0100 | [diff] [blame] | 472 | }; |
| 473 | |
| 474 | static __devinitdata struct mfd_cell sta2x11_mfd1_bar1[] = { |
Davide Ciminaghi | b18adaf | 2012-11-09 15:19:55 +0100 | [diff] [blame] | 475 | DEV(STA2X11_MFD_APB_SOC_REGS_NAME, apb_soc_regs_resources), |
Davide Ciminaghi | 1950c71 | 2012-11-09 15:19:52 +0100 | [diff] [blame] | 476 | }; |
| 477 | |
| 478 | |
Alessandro Rubini | 35bdd29 | 2012-04-12 10:48:44 +0200 | [diff] [blame] | 479 | static 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 | |
| 488 | static 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 Ciminaghi | 1950c71 | 2012-11-09 15:19:52 +0100 | [diff] [blame] | 501 | struct sta2x11_mfd_bar_setup_data { |
| 502 | struct mfd_cell *cells; |
| 503 | int ncells; |
| 504 | }; |
| 505 | |
| 506 | struct 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 | |
| 513 | static 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 | |
| 542 | static 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 Rubini | 35bdd29 | 2012-04-12 10:48:44 +0200 | [diff] [blame] | 553 | static int __devinit sta2x11_mfd_probe(struct pci_dev *pdev, |
| 554 | const struct pci_device_id *pci_id) |
| 555 | { |
| 556 | int err, i; |
Davide Ciminaghi | 1950c71 | 2012-11-09 15:19:52 +0100 | [diff] [blame] | 557 | struct sta2x11_mfd_setup_data *setup_data; |
Alessandro Rubini | 35bdd29 | 2012-04-12 10:48:44 +0200 | [diff] [blame] | 558 | |
| 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 Ciminaghi | 1950c71 | 2012-11-09 15:19:52 +0100 | [diff] [blame] | 571 | setup_data = pci_id->device == PCI_DEVICE_ID_STMICRO_GPIO ? |
| 572 | &mfd_setup_data[STA2X11_MFD0] : |
| 573 | &mfd_setup_data[STA2X11_MFD1]; |
| 574 | |
Alessandro Rubini | 35bdd29 | 2012-04-12 10:48:44 +0200 | [diff] [blame] | 575 | /* platform data is the pci device for all of them */ |
Davide Ciminaghi | 1950c71 | 2012-11-09 15:19:52 +0100 | [diff] [blame] | 576 | sta2x11_mfd_setup(pdev, setup_data); |
Alessandro Rubini | 35bdd29 | 2012-04-12 10:48:44 +0200 | [diff] [blame] | 577 | |
| 578 | /* Record this pdev before mfd_add_devices: their probe looks for it */ |
Davide Ciminaghi | 8ec86a3 | 2012-11-09 15:19:56 +0100 | [diff] [blame] | 579 | if (!sta2x11_mfd_find(pdev)) |
| 580 | sta2x11_mfd_add(pdev, GFP_ATOMIC); |
Alessandro Rubini | 35bdd29 | 2012-04-12 10:48:44 +0200 | [diff] [blame] | 581 | |
Davide Ciminaghi | 1950c71 | 2012-11-09 15:19:52 +0100 | [diff] [blame] | 582 | /* 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 Rubini | 35bdd29 | 2012-04-12 10:48:44 +0200 | [diff] [blame] | 594 | } |
| 595 | |
| 596 | return 0; |
| 597 | |
| 598 | err_disable: |
| 599 | mfd_remove_devices(&pdev->dev); |
| 600 | pci_disable_device(pdev); |
| 601 | pci_disable_msi(pdev); |
| 602 | return err; |
| 603 | } |
| 604 | |
| 605 | static DEFINE_PCI_DEVICE_TABLE(sta2x11_mfd_tbl) = { |
| 606 | {PCI_DEVICE(PCI_VENDOR_ID_STMICRO, PCI_DEVICE_ID_STMICRO_GPIO)}, |
Davide Ciminaghi | 1950c71 | 2012-11-09 15:19:52 +0100 | [diff] [blame] | 607 | {PCI_DEVICE(PCI_VENDOR_ID_STMICRO, PCI_DEVICE_ID_STMICRO_VIC)}, |
Alessandro Rubini | 35bdd29 | 2012-04-12 10:48:44 +0200 | [diff] [blame] | 608 | {0,}, |
| 609 | }; |
| 610 | |
| 611 | static 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 | |
| 619 | static 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 | */ |
| 631 | subsys_initcall(sta2x11_apbreg_init); |
| 632 | subsys_initcall(sta2x11_sctl_init); |
Davide Ciminaghi | 1950c71 | 2012-11-09 15:19:52 +0100 | [diff] [blame] | 633 | subsys_initcall(sta2x11_apb_soc_regs_init); |
Alessandro Rubini | 35bdd29 | 2012-04-12 10:48:44 +0200 | [diff] [blame] | 634 | rootfs_initcall(sta2x11_mfd_init); |
| 635 | |
| 636 | MODULE_LICENSE("GPL v2"); |
| 637 | MODULE_AUTHOR("Wind River"); |
| 638 | MODULE_DESCRIPTION("STA2x11 mfd for GPIO, SCTL and APBREG"); |
| 639 | MODULE_DEVICE_TABLE(pci, sta2x11_mfd_tbl); |