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