Raymond Tan | 60ae5b9 | 2015-02-02 10:52:51 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Intel Quark MFD PCI driver for I2C & GPIO |
| 3 | * |
| 4 | * Copyright(c) 2014 Intel Corporation. |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify it |
| 7 | * under the terms and conditions of the GNU General Public License, |
| 8 | * version 2, as published by the Free Software Foundation. |
| 9 | * |
| 10 | * This program is distributed in the hope it will be useful, but WITHOUT |
| 11 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 12 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 13 | * more details. |
| 14 | * |
| 15 | * Intel Quark PCI device for I2C and GPIO controller sharing the same |
| 16 | * PCI function. This PCI driver will split the 2 devices into their |
| 17 | * respective drivers. |
| 18 | */ |
| 19 | |
| 20 | #include <linux/kernel.h> |
| 21 | #include <linux/module.h> |
| 22 | #include <linux/pci.h> |
| 23 | #include <linux/mfd/core.h> |
| 24 | #include <linux/clkdev.h> |
| 25 | #include <linux/clk-provider.h> |
| 26 | #include <linux/dmi.h> |
| 27 | #include <linux/platform_data/gpio-dwapb.h> |
| 28 | #include <linux/platform_data/i2c-designware.h> |
| 29 | |
| 30 | /* PCI BAR for register base address */ |
| 31 | #define MFD_I2C_BAR 0 |
| 32 | #define MFD_GPIO_BAR 1 |
| 33 | |
Andy Shevchenko | 918fe70 | 2015-10-23 12:16:43 +0300 | [diff] [blame] | 34 | /* ACPI _ADR value to match the child node */ |
| 35 | #define MFD_ACPI_MATCH_GPIO 0ULL |
| 36 | #define MFD_ACPI_MATCH_I2C 1ULL |
| 37 | |
Raymond Tan | 60ae5b9 | 2015-02-02 10:52:51 +0800 | [diff] [blame] | 38 | /* The base GPIO number under GPIOLIB framework */ |
| 39 | #define INTEL_QUARK_MFD_GPIO_BASE 8 |
| 40 | |
| 41 | /* The default number of South-Cluster GPIO on Quark. */ |
| 42 | #define INTEL_QUARK_MFD_NGPIO 8 |
| 43 | |
| 44 | /* The DesignWare GPIO ports on Quark. */ |
| 45 | #define INTEL_QUARK_GPIO_NPORTS 1 |
| 46 | |
| 47 | #define INTEL_QUARK_IORES_MEM 0 |
| 48 | #define INTEL_QUARK_IORES_IRQ 1 |
| 49 | |
| 50 | #define INTEL_QUARK_I2C_CONTROLLER_CLK "i2c_designware.0" |
| 51 | |
| 52 | /* The Quark I2C controller source clock */ |
| 53 | #define INTEL_QUARK_I2C_CLK_HZ 33000000 |
| 54 | |
Raymond Tan | 60ae5b9 | 2015-02-02 10:52:51 +0800 | [diff] [blame] | 55 | struct intel_quark_mfd { |
Andy Shevchenko | 9caac88 | 2016-02-19 10:42:11 +0200 | [diff] [blame] | 56 | struct device *dev; |
Raymond Tan | 60ae5b9 | 2015-02-02 10:52:51 +0800 | [diff] [blame] | 57 | struct clk *i2c_clk; |
| 58 | struct clk_lookup *i2c_clk_lookup; |
| 59 | }; |
| 60 | |
Jan Kiszka | b518d4a | 2017-05-30 10:17:40 +0200 | [diff] [blame] | 61 | static const struct dmi_system_id dmi_platform_info[] = { |
Raymond Tan | 60ae5b9 | 2015-02-02 10:52:51 +0800 | [diff] [blame] | 62 | { |
Jan Kiszka | b518d4a | 2017-05-30 10:17:40 +0200 | [diff] [blame] | 63 | .matches = { |
| 64 | DMI_EXACT_MATCH(DMI_BOARD_NAME, "Galileo"), |
| 65 | }, |
| 66 | .driver_data = (void *)100000, |
Raymond Tan | 60ae5b9 | 2015-02-02 10:52:51 +0800 | [diff] [blame] | 67 | }, |
| 68 | { |
Jan Kiszka | b518d4a | 2017-05-30 10:17:40 +0200 | [diff] [blame] | 69 | .matches = { |
| 70 | DMI_EXACT_MATCH(DMI_BOARD_NAME, "GalileoGen2"), |
| 71 | }, |
| 72 | .driver_data = (void *)400000, |
Raymond Tan | 60ae5b9 | 2015-02-02 10:52:51 +0800 | [diff] [blame] | 73 | }, |
Jan Kiszka | 842086d | 2017-05-30 10:17:41 +0200 | [diff] [blame] | 74 | { |
| 75 | .matches = { |
| 76 | DMI_EXACT_MATCH(DMI_BOARD_NAME, "SIMATIC IOT2000"), |
| 77 | DMI_EXACT_MATCH(DMI_BOARD_ASSET_TAG, |
| 78 | "6ES7647-0AA00-0YA2"), |
| 79 | }, |
| 80 | .driver_data = (void *)400000, |
| 81 | }, |
| 82 | { |
| 83 | .matches = { |
| 84 | DMI_EXACT_MATCH(DMI_BOARD_NAME, "SIMATIC IOT2000"), |
| 85 | DMI_EXACT_MATCH(DMI_BOARD_ASSET_TAG, |
| 86 | "6ES7647-0AA00-1YA2"), |
| 87 | }, |
| 88 | .driver_data = (void *)400000, |
| 89 | }, |
Andy Shevchenko | bafc1fa | 2015-03-25 20:03:55 +0200 | [diff] [blame] | 90 | {} |
Raymond Tan | 60ae5b9 | 2015-02-02 10:52:51 +0800 | [diff] [blame] | 91 | }; |
| 92 | |
| 93 | static struct resource intel_quark_i2c_res[] = { |
| 94 | [INTEL_QUARK_IORES_MEM] = { |
| 95 | .flags = IORESOURCE_MEM, |
| 96 | }, |
| 97 | [INTEL_QUARK_IORES_IRQ] = { |
| 98 | .flags = IORESOURCE_IRQ, |
| 99 | }, |
| 100 | }; |
| 101 | |
Andy Shevchenko | 918fe70 | 2015-10-23 12:16:43 +0300 | [diff] [blame] | 102 | static struct mfd_cell_acpi_match intel_quark_acpi_match_i2c = { |
| 103 | .adr = MFD_ACPI_MATCH_I2C, |
| 104 | }; |
| 105 | |
Raymond Tan | 60ae5b9 | 2015-02-02 10:52:51 +0800 | [diff] [blame] | 106 | static struct resource intel_quark_gpio_res[] = { |
| 107 | [INTEL_QUARK_IORES_MEM] = { |
| 108 | .flags = IORESOURCE_MEM, |
| 109 | }, |
| 110 | }; |
| 111 | |
Andy Shevchenko | 918fe70 | 2015-10-23 12:16:43 +0300 | [diff] [blame] | 112 | static struct mfd_cell_acpi_match intel_quark_acpi_match_gpio = { |
| 113 | .adr = MFD_ACPI_MATCH_GPIO, |
| 114 | }; |
| 115 | |
Raymond Tan | 60ae5b9 | 2015-02-02 10:52:51 +0800 | [diff] [blame] | 116 | static struct mfd_cell intel_quark_mfd_cells[] = { |
| 117 | { |
Raymond Tan | 60ae5b9 | 2015-02-02 10:52:51 +0800 | [diff] [blame] | 118 | .id = MFD_GPIO_BAR, |
| 119 | .name = "gpio-dwapb", |
Andy Shevchenko | 918fe70 | 2015-10-23 12:16:43 +0300 | [diff] [blame] | 120 | .acpi_match = &intel_quark_acpi_match_gpio, |
Raymond Tan | 60ae5b9 | 2015-02-02 10:52:51 +0800 | [diff] [blame] | 121 | .num_resources = ARRAY_SIZE(intel_quark_gpio_res), |
| 122 | .resources = intel_quark_gpio_res, |
| 123 | .ignore_resource_conflicts = true, |
| 124 | }, |
Andy Shevchenko | 236fd46 | 2015-10-23 12:16:42 +0300 | [diff] [blame] | 125 | { |
| 126 | .id = MFD_I2C_BAR, |
| 127 | .name = "i2c_designware", |
Andy Shevchenko | 918fe70 | 2015-10-23 12:16:43 +0300 | [diff] [blame] | 128 | .acpi_match = &intel_quark_acpi_match_i2c, |
Andy Shevchenko | 236fd46 | 2015-10-23 12:16:42 +0300 | [diff] [blame] | 129 | .num_resources = ARRAY_SIZE(intel_quark_i2c_res), |
| 130 | .resources = intel_quark_i2c_res, |
| 131 | .ignore_resource_conflicts = true, |
| 132 | }, |
Raymond Tan | 60ae5b9 | 2015-02-02 10:52:51 +0800 | [diff] [blame] | 133 | }; |
| 134 | |
| 135 | static const struct pci_device_id intel_quark_mfd_ids[] = { |
| 136 | { PCI_VDEVICE(INTEL, 0x0934), }, |
| 137 | {}, |
| 138 | }; |
| 139 | MODULE_DEVICE_TABLE(pci, intel_quark_mfd_ids); |
| 140 | |
Andy Shevchenko | 9caac88 | 2016-02-19 10:42:11 +0200 | [diff] [blame] | 141 | static int intel_quark_register_i2c_clk(struct device *dev) |
Raymond Tan | 60ae5b9 | 2015-02-02 10:52:51 +0800 | [diff] [blame] | 142 | { |
Andy Shevchenko | 9caac88 | 2016-02-19 10:42:11 +0200 | [diff] [blame] | 143 | struct intel_quark_mfd *quark_mfd = dev_get_drvdata(dev); |
Raymond Tan | 60ae5b9 | 2015-02-02 10:52:51 +0800 | [diff] [blame] | 144 | struct clk *i2c_clk; |
Raymond Tan | 60ae5b9 | 2015-02-02 10:52:51 +0800 | [diff] [blame] | 145 | |
Andy Shevchenko | 9caac88 | 2016-02-19 10:42:11 +0200 | [diff] [blame] | 146 | i2c_clk = clk_register_fixed_rate(dev, |
Raymond Tan | 60ae5b9 | 2015-02-02 10:52:51 +0800 | [diff] [blame] | 147 | INTEL_QUARK_I2C_CONTROLLER_CLK, NULL, |
Stephen Boyd | 36a0c08 | 2016-04-19 18:19:20 -0700 | [diff] [blame] | 148 | 0, INTEL_QUARK_I2C_CLK_HZ); |
Stephen Boyd | c4726ab | 2016-02-08 17:45:28 -0800 | [diff] [blame] | 149 | if (IS_ERR(i2c_clk)) |
| 150 | return PTR_ERR(i2c_clk); |
Raymond Tan | 60ae5b9 | 2015-02-02 10:52:51 +0800 | [diff] [blame] | 151 | |
Raymond Tan | 60ae5b9 | 2015-02-02 10:52:51 +0800 | [diff] [blame] | 152 | quark_mfd->i2c_clk = i2c_clk; |
Stephen Boyd | c4726ab | 2016-02-08 17:45:28 -0800 | [diff] [blame] | 153 | quark_mfd->i2c_clk_lookup = clkdev_create(i2c_clk, NULL, |
| 154 | INTEL_QUARK_I2C_CONTROLLER_CLK); |
Raymond Tan | 60ae5b9 | 2015-02-02 10:52:51 +0800 | [diff] [blame] | 155 | |
Stephen Boyd | c4726ab | 2016-02-08 17:45:28 -0800 | [diff] [blame] | 156 | if (!quark_mfd->i2c_clk_lookup) { |
Andy Shevchenko | 7f0c5ae | 2016-02-19 10:42:10 +0200 | [diff] [blame] | 157 | clk_unregister(quark_mfd->i2c_clk); |
Andy Shevchenko | 9caac88 | 2016-02-19 10:42:11 +0200 | [diff] [blame] | 158 | dev_err(dev, "Fixed clk register failed\n"); |
Stephen Boyd | c4726ab | 2016-02-08 17:45:28 -0800 | [diff] [blame] | 159 | return -ENOMEM; |
| 160 | } |
Raymond Tan | 60ae5b9 | 2015-02-02 10:52:51 +0800 | [diff] [blame] | 161 | |
Stephen Boyd | c4726ab | 2016-02-08 17:45:28 -0800 | [diff] [blame] | 162 | return 0; |
Raymond Tan | 60ae5b9 | 2015-02-02 10:52:51 +0800 | [diff] [blame] | 163 | } |
| 164 | |
Andy Shevchenko | 9caac88 | 2016-02-19 10:42:11 +0200 | [diff] [blame] | 165 | static void intel_quark_unregister_i2c_clk(struct device *dev) |
Raymond Tan | 60ae5b9 | 2015-02-02 10:52:51 +0800 | [diff] [blame] | 166 | { |
Andy Shevchenko | 9caac88 | 2016-02-19 10:42:11 +0200 | [diff] [blame] | 167 | struct intel_quark_mfd *quark_mfd = dev_get_drvdata(dev); |
Raymond Tan | 60ae5b9 | 2015-02-02 10:52:51 +0800 | [diff] [blame] | 168 | |
Andy Shevchenko | 7f0c5ae | 2016-02-19 10:42:10 +0200 | [diff] [blame] | 169 | if (!quark_mfd->i2c_clk_lookup) |
Raymond Tan | 60ae5b9 | 2015-02-02 10:52:51 +0800 | [diff] [blame] | 170 | return; |
| 171 | |
| 172 | clkdev_drop(quark_mfd->i2c_clk_lookup); |
| 173 | clk_unregister(quark_mfd->i2c_clk); |
| 174 | } |
| 175 | |
| 176 | static int intel_quark_i2c_setup(struct pci_dev *pdev, struct mfd_cell *cell) |
| 177 | { |
Jan Kiszka | b518d4a | 2017-05-30 10:17:40 +0200 | [diff] [blame] | 178 | const struct dmi_system_id *dmi_id; |
Raymond Tan | 60ae5b9 | 2015-02-02 10:52:51 +0800 | [diff] [blame] | 179 | struct dw_i2c_platform_data *pdata; |
| 180 | struct resource *res = (struct resource *)cell->resources; |
| 181 | struct device *dev = &pdev->dev; |
Raymond Tan | 60ae5b9 | 2015-02-02 10:52:51 +0800 | [diff] [blame] | 182 | |
| 183 | res[INTEL_QUARK_IORES_MEM].start = |
| 184 | pci_resource_start(pdev, MFD_I2C_BAR); |
| 185 | res[INTEL_QUARK_IORES_MEM].end = |
| 186 | pci_resource_end(pdev, MFD_I2C_BAR); |
| 187 | |
| 188 | res[INTEL_QUARK_IORES_IRQ].start = pdev->irq; |
| 189 | res[INTEL_QUARK_IORES_IRQ].end = pdev->irq; |
| 190 | |
| 191 | pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL); |
| 192 | if (!pdata) |
| 193 | return -ENOMEM; |
| 194 | |
Andy Shevchenko | bafc1fa | 2015-03-25 20:03:55 +0200 | [diff] [blame] | 195 | /* Normal mode by default */ |
| 196 | pdata->i2c_scl_freq = 100000; |
Raymond Tan | 60ae5b9 | 2015-02-02 10:52:51 +0800 | [diff] [blame] | 197 | |
Jan Kiszka | b518d4a | 2017-05-30 10:17:40 +0200 | [diff] [blame] | 198 | dmi_id = dmi_first_match(dmi_platform_info); |
| 199 | if (dmi_id) |
| 200 | pdata->i2c_scl_freq = (uintptr_t)dmi_id->driver_data; |
Raymond Tan | 60ae5b9 | 2015-02-02 10:52:51 +0800 | [diff] [blame] | 201 | |
| 202 | cell->platform_data = pdata; |
| 203 | cell->pdata_size = sizeof(*pdata); |
| 204 | |
| 205 | return 0; |
| 206 | } |
| 207 | |
| 208 | static int intel_quark_gpio_setup(struct pci_dev *pdev, struct mfd_cell *cell) |
| 209 | { |
| 210 | struct dwapb_platform_data *pdata; |
| 211 | struct resource *res = (struct resource *)cell->resources; |
| 212 | struct device *dev = &pdev->dev; |
| 213 | |
| 214 | res[INTEL_QUARK_IORES_MEM].start = |
| 215 | pci_resource_start(pdev, MFD_GPIO_BAR); |
| 216 | res[INTEL_QUARK_IORES_MEM].end = |
| 217 | pci_resource_end(pdev, MFD_GPIO_BAR); |
| 218 | |
| 219 | pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL); |
| 220 | if (!pdata) |
| 221 | return -ENOMEM; |
| 222 | |
| 223 | /* For intel quark x1000, it has only one port: portA */ |
| 224 | pdata->nports = INTEL_QUARK_GPIO_NPORTS; |
| 225 | pdata->properties = devm_kcalloc(dev, pdata->nports, |
| 226 | sizeof(*pdata->properties), |
| 227 | GFP_KERNEL); |
| 228 | if (!pdata->properties) |
| 229 | return -ENOMEM; |
| 230 | |
| 231 | /* Set the properties for portA */ |
Jiang Qiu | 4ba8cfa | 2016-04-28 17:32:02 +0800 | [diff] [blame] | 232 | pdata->properties->fwnode = NULL; |
Raymond Tan | 60ae5b9 | 2015-02-02 10:52:51 +0800 | [diff] [blame] | 233 | pdata->properties->idx = 0; |
| 234 | pdata->properties->ngpio = INTEL_QUARK_MFD_NGPIO; |
| 235 | pdata->properties->gpio_base = INTEL_QUARK_MFD_GPIO_BASE; |
Phil Edworthy | e6ca26a | 2018-04-26 17:19:47 +0100 | [diff] [blame] | 236 | pdata->properties->irq[0] = pdev->irq; |
| 237 | pdata->properties->has_irq = true; |
Raymond Tan | 60ae5b9 | 2015-02-02 10:52:51 +0800 | [diff] [blame] | 238 | pdata->properties->irq_shared = true; |
| 239 | |
| 240 | cell->platform_data = pdata; |
| 241 | cell->pdata_size = sizeof(*pdata); |
| 242 | |
| 243 | return 0; |
| 244 | } |
| 245 | |
| 246 | static int intel_quark_mfd_probe(struct pci_dev *pdev, |
| 247 | const struct pci_device_id *id) |
| 248 | { |
| 249 | struct intel_quark_mfd *quark_mfd; |
| 250 | int ret; |
| 251 | |
| 252 | ret = pcim_enable_device(pdev); |
| 253 | if (ret) |
| 254 | return ret; |
| 255 | |
| 256 | quark_mfd = devm_kzalloc(&pdev->dev, sizeof(*quark_mfd), GFP_KERNEL); |
| 257 | if (!quark_mfd) |
| 258 | return -ENOMEM; |
Raymond Tan | 60ae5b9 | 2015-02-02 10:52:51 +0800 | [diff] [blame] | 259 | |
Andy Shevchenko | 9caac88 | 2016-02-19 10:42:11 +0200 | [diff] [blame] | 260 | quark_mfd->dev = &pdev->dev; |
Andy Shevchenko | 7f0c5ae | 2016-02-19 10:42:10 +0200 | [diff] [blame] | 261 | dev_set_drvdata(&pdev->dev, quark_mfd); |
Raymond Tan | 60ae5b9 | 2015-02-02 10:52:51 +0800 | [diff] [blame] | 262 | |
Andy Shevchenko | 9caac88 | 2016-02-19 10:42:11 +0200 | [diff] [blame] | 263 | ret = intel_quark_register_i2c_clk(&pdev->dev); |
Raymond Tan | 60ae5b9 | 2015-02-02 10:52:51 +0800 | [diff] [blame] | 264 | if (ret) |
| 265 | return ret; |
| 266 | |
Andy Shevchenko | 236fd46 | 2015-10-23 12:16:42 +0300 | [diff] [blame] | 267 | ret = intel_quark_i2c_setup(pdev, &intel_quark_mfd_cells[1]); |
Raymond Tan | 60ae5b9 | 2015-02-02 10:52:51 +0800 | [diff] [blame] | 268 | if (ret) |
Andy Shevchenko | 7f0c5ae | 2016-02-19 10:42:10 +0200 | [diff] [blame] | 269 | goto err_unregister_i2c_clk; |
Raymond Tan | 60ae5b9 | 2015-02-02 10:52:51 +0800 | [diff] [blame] | 270 | |
Andy Shevchenko | 236fd46 | 2015-10-23 12:16:42 +0300 | [diff] [blame] | 271 | ret = intel_quark_gpio_setup(pdev, &intel_quark_mfd_cells[0]); |
Raymond Tan | 60ae5b9 | 2015-02-02 10:52:51 +0800 | [diff] [blame] | 272 | if (ret) |
Andy Shevchenko | 7f0c5ae | 2016-02-19 10:42:10 +0200 | [diff] [blame] | 273 | goto err_unregister_i2c_clk; |
Raymond Tan | 60ae5b9 | 2015-02-02 10:52:51 +0800 | [diff] [blame] | 274 | |
Andy Shevchenko | 7f0c5ae | 2016-02-19 10:42:10 +0200 | [diff] [blame] | 275 | ret = mfd_add_devices(&pdev->dev, 0, intel_quark_mfd_cells, |
| 276 | ARRAY_SIZE(intel_quark_mfd_cells), NULL, 0, |
| 277 | NULL); |
| 278 | if (ret) |
| 279 | goto err_unregister_i2c_clk; |
| 280 | |
| 281 | return 0; |
| 282 | |
| 283 | err_unregister_i2c_clk: |
Andy Shevchenko | 9caac88 | 2016-02-19 10:42:11 +0200 | [diff] [blame] | 284 | intel_quark_unregister_i2c_clk(&pdev->dev); |
Andy Shevchenko | 7f0c5ae | 2016-02-19 10:42:10 +0200 | [diff] [blame] | 285 | return ret; |
Raymond Tan | 60ae5b9 | 2015-02-02 10:52:51 +0800 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | static void intel_quark_mfd_remove(struct pci_dev *pdev) |
| 289 | { |
Andy Shevchenko | 9caac88 | 2016-02-19 10:42:11 +0200 | [diff] [blame] | 290 | intel_quark_unregister_i2c_clk(&pdev->dev); |
Raymond Tan | 60ae5b9 | 2015-02-02 10:52:51 +0800 | [diff] [blame] | 291 | mfd_remove_devices(&pdev->dev); |
| 292 | } |
| 293 | |
| 294 | static struct pci_driver intel_quark_mfd_driver = { |
| 295 | .name = "intel_quark_mfd_i2c_gpio", |
| 296 | .id_table = intel_quark_mfd_ids, |
| 297 | .probe = intel_quark_mfd_probe, |
| 298 | .remove = intel_quark_mfd_remove, |
| 299 | }; |
| 300 | |
| 301 | module_pci_driver(intel_quark_mfd_driver); |
| 302 | |
| 303 | MODULE_AUTHOR("Raymond Tan <raymond.tan@intel.com>"); |
| 304 | MODULE_DESCRIPTION("Intel Quark MFD PCI driver for I2C & GPIO"); |
| 305 | MODULE_LICENSE("GPL v2"); |