Vadim Pasternak | 58cbbee | 2016-09-22 21:13:42 +0000 | [diff] [blame] | 1 | /* |
Vadim Pasternak | 58cbbee | 2016-09-22 21:13:42 +0000 | [diff] [blame] | 2 | * Copyright (c) 2016 Mellanox Technologies. All rights reserved. |
| 3 | * Copyright (c) 2016 Vadim Pasternak <vadimp@mellanox.com> |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions are met: |
| 7 | * |
| 8 | * 1. Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * 2. Redistributions in binary form must reproduce the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer in the |
| 12 | * documentation and/or other materials provided with the distribution. |
| 13 | * 3. Neither the names of the copyright holders nor the names of its |
| 14 | * contributors may be used to endorse or promote products derived from |
| 15 | * this software without specific prior written permission. |
| 16 | * |
| 17 | * Alternatively, this software may be distributed under the terms of the |
| 18 | * GNU General Public License ("GPL") version 2 as published by the Free |
| 19 | * Software Foundation. |
| 20 | * |
| 21 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 22 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 24 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
| 25 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 26 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 27 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 28 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 29 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 30 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 31 | * POSSIBILITY OF SUCH DAMAGE. |
| 32 | */ |
| 33 | |
| 34 | #include <linux/device.h> |
| 35 | #include <linux/dmi.h> |
| 36 | #include <linux/i2c.h> |
| 37 | #include <linux/i2c-mux.h> |
| 38 | #include <linux/module.h> |
| 39 | #include <linux/platform_device.h> |
| 40 | #include <linux/platform_data/i2c-mux-reg.h> |
| 41 | |
| 42 | #define MLX_PLAT_DEVICE_NAME "mlxplat" |
| 43 | |
| 44 | /* LPC bus IO offsets */ |
| 45 | #define MLXPLAT_CPLD_LPC_I2C_BASE_ADRR 0x2000 |
| 46 | #define MLXPLAT_CPLD_LPC_REG_BASE_ADRR 0x2500 |
| 47 | #define MLXPLAT_CPLD_LPC_IO_RANGE 0x100 |
| 48 | #define MLXPLAT_CPLD_LPC_I2C_CH1_OFF 0xdb |
| 49 | #define MLXPLAT_CPLD_LPC_I2C_CH2_OFF 0xda |
| 50 | #define MLXPLAT_CPLD_LPC_PIO_OFFSET 0x10000UL |
| 51 | #define MLXPLAT_CPLD_LPC_REG1 ((MLXPLAT_CPLD_LPC_REG_BASE_ADRR + \ |
| 52 | MLXPLAT_CPLD_LPC_I2C_CH1_OFF) | \ |
| 53 | MLXPLAT_CPLD_LPC_PIO_OFFSET) |
| 54 | #define MLXPLAT_CPLD_LPC_REG2 ((MLXPLAT_CPLD_LPC_REG_BASE_ADRR + \ |
| 55 | MLXPLAT_CPLD_LPC_I2C_CH2_OFF) | \ |
| 56 | MLXPLAT_CPLD_LPC_PIO_OFFSET) |
| 57 | |
| 58 | /* Start channel numbers */ |
| 59 | #define MLXPLAT_CPLD_CH1 2 |
| 60 | #define MLXPLAT_CPLD_CH2 10 |
| 61 | |
| 62 | /* Number of LPC attached MUX platform devices */ |
| 63 | #define MLXPLAT_CPLD_LPC_MUX_DEVS 2 |
| 64 | |
| 65 | /* mlxplat_priv - platform private data |
| 66 | * @pdev_i2c - i2c controller platform device |
| 67 | * @pdev_mux - array of mux platform devices |
| 68 | */ |
| 69 | struct mlxplat_priv { |
| 70 | struct platform_device *pdev_i2c; |
| 71 | struct platform_device *pdev_mux[MLXPLAT_CPLD_LPC_MUX_DEVS]; |
| 72 | }; |
| 73 | |
| 74 | /* Regions for LPC I2C controller and LPC base register space */ |
| 75 | static const struct resource mlxplat_lpc_resources[] = { |
| 76 | [0] = DEFINE_RES_NAMED(MLXPLAT_CPLD_LPC_I2C_BASE_ADRR, |
| 77 | MLXPLAT_CPLD_LPC_IO_RANGE, |
| 78 | "mlxplat_cpld_lpc_i2c_ctrl", IORESOURCE_IO), |
| 79 | [1] = DEFINE_RES_NAMED(MLXPLAT_CPLD_LPC_REG_BASE_ADRR, |
| 80 | MLXPLAT_CPLD_LPC_IO_RANGE, |
| 81 | "mlxplat_cpld_lpc_regs", |
| 82 | IORESOURCE_IO), |
| 83 | }; |
| 84 | |
| 85 | /* Platform default channels */ |
| 86 | static const int mlxplat_default_channels[][8] = { |
| 87 | { |
| 88 | MLXPLAT_CPLD_CH1, MLXPLAT_CPLD_CH1 + 1, MLXPLAT_CPLD_CH1 + 2, |
| 89 | MLXPLAT_CPLD_CH1 + 3, MLXPLAT_CPLD_CH1 + 4, MLXPLAT_CPLD_CH1 + |
| 90 | 5, MLXPLAT_CPLD_CH1 + 6, MLXPLAT_CPLD_CH1 + 7 |
| 91 | }, |
| 92 | { |
| 93 | MLXPLAT_CPLD_CH2, MLXPLAT_CPLD_CH2 + 1, MLXPLAT_CPLD_CH2 + 2, |
| 94 | MLXPLAT_CPLD_CH2 + 3, MLXPLAT_CPLD_CH2 + 4, MLXPLAT_CPLD_CH2 + |
| 95 | 5, MLXPLAT_CPLD_CH2 + 6, MLXPLAT_CPLD_CH2 + 7 |
| 96 | }, |
| 97 | }; |
| 98 | |
| 99 | /* Platform channels for MSN21xx system family */ |
| 100 | static const int mlxplat_msn21xx_channels[] = { 1, 2, 3, 4, 5, 6, 7, 8 }; |
| 101 | |
| 102 | /* Platform mux data */ |
| 103 | static struct i2c_mux_reg_platform_data mlxplat_mux_data[] = { |
| 104 | { |
| 105 | .parent = 1, |
| 106 | .base_nr = MLXPLAT_CPLD_CH1, |
| 107 | .write_only = 1, |
| 108 | .reg = (void __iomem *)MLXPLAT_CPLD_LPC_REG1, |
| 109 | .reg_size = 1, |
| 110 | .idle_in_use = 1, |
| 111 | }, |
| 112 | { |
| 113 | .parent = 1, |
| 114 | .base_nr = MLXPLAT_CPLD_CH2, |
| 115 | .write_only = 1, |
| 116 | .reg = (void __iomem *)MLXPLAT_CPLD_LPC_REG2, |
| 117 | .reg_size = 1, |
| 118 | .idle_in_use = 1, |
| 119 | }, |
| 120 | |
| 121 | }; |
| 122 | |
| 123 | static struct platform_device *mlxplat_dev; |
| 124 | |
| 125 | static int __init mlxplat_dmi_default_matched(const struct dmi_system_id *dmi) |
| 126 | { |
| 127 | int i; |
| 128 | |
| 129 | for (i = 0; i < ARRAY_SIZE(mlxplat_mux_data); i++) { |
| 130 | mlxplat_mux_data[i].values = mlxplat_default_channels[i]; |
| 131 | mlxplat_mux_data[i].n_values = |
| 132 | ARRAY_SIZE(mlxplat_default_channels[i]); |
| 133 | } |
| 134 | |
| 135 | return 1; |
| 136 | }; |
| 137 | |
| 138 | static int __init mlxplat_dmi_msn21xx_matched(const struct dmi_system_id *dmi) |
| 139 | { |
| 140 | int i; |
| 141 | |
| 142 | for (i = 0; i < ARRAY_SIZE(mlxplat_mux_data); i++) { |
| 143 | mlxplat_mux_data[i].values = mlxplat_msn21xx_channels; |
| 144 | mlxplat_mux_data[i].n_values = |
| 145 | ARRAY_SIZE(mlxplat_msn21xx_channels); |
| 146 | } |
| 147 | |
| 148 | return 1; |
| 149 | }; |
| 150 | |
| 151 | static struct dmi_system_id mlxplat_dmi_table[] __initdata = { |
| 152 | { |
| 153 | .callback = mlxplat_dmi_default_matched, |
| 154 | .matches = { |
| 155 | DMI_MATCH(DMI_BOARD_VENDOR, "Mellanox Technologies"), |
| 156 | DMI_MATCH(DMI_PRODUCT_NAME, "MSN24"), |
| 157 | }, |
| 158 | }, |
| 159 | { |
| 160 | .callback = mlxplat_dmi_default_matched, |
| 161 | .matches = { |
| 162 | DMI_MATCH(DMI_BOARD_VENDOR, "Mellanox Technologies"), |
| 163 | DMI_MATCH(DMI_PRODUCT_NAME, "MSN27"), |
| 164 | }, |
| 165 | }, |
| 166 | { |
| 167 | .callback = mlxplat_dmi_default_matched, |
| 168 | .matches = { |
| 169 | DMI_MATCH(DMI_BOARD_VENDOR, "Mellanox Technologies"), |
| 170 | DMI_MATCH(DMI_PRODUCT_NAME, "MSB"), |
| 171 | }, |
| 172 | }, |
| 173 | { |
| 174 | .callback = mlxplat_dmi_default_matched, |
| 175 | .matches = { |
| 176 | DMI_MATCH(DMI_BOARD_VENDOR, "Mellanox Technologies"), |
| 177 | DMI_MATCH(DMI_PRODUCT_NAME, "MSX"), |
| 178 | }, |
| 179 | }, |
| 180 | { |
| 181 | .callback = mlxplat_dmi_msn21xx_matched, |
| 182 | .matches = { |
| 183 | DMI_MATCH(DMI_BOARD_VENDOR, "Mellanox Technologies"), |
| 184 | DMI_MATCH(DMI_PRODUCT_NAME, "MSN21"), |
| 185 | }, |
| 186 | }, |
| 187 | { } |
| 188 | }; |
| 189 | |
| 190 | static int __init mlxplat_init(void) |
| 191 | { |
| 192 | struct mlxplat_priv *priv; |
| 193 | int i, err; |
| 194 | |
| 195 | if (!dmi_check_system(mlxplat_dmi_table)) |
| 196 | return -ENODEV; |
| 197 | |
| 198 | mlxplat_dev = platform_device_register_simple(MLX_PLAT_DEVICE_NAME, -1, |
| 199 | mlxplat_lpc_resources, |
| 200 | ARRAY_SIZE(mlxplat_lpc_resources)); |
| 201 | |
Wei Yongjun | 65f7422 | 2016-09-24 11:48:13 +0000 | [diff] [blame] | 202 | if (IS_ERR(mlxplat_dev)) |
| 203 | return PTR_ERR(mlxplat_dev); |
Vadim Pasternak | 58cbbee | 2016-09-22 21:13:42 +0000 | [diff] [blame] | 204 | |
| 205 | priv = devm_kzalloc(&mlxplat_dev->dev, sizeof(struct mlxplat_priv), |
| 206 | GFP_KERNEL); |
| 207 | if (!priv) { |
| 208 | err = -ENOMEM; |
| 209 | goto fail_alloc; |
| 210 | } |
| 211 | platform_set_drvdata(mlxplat_dev, priv); |
| 212 | |
| 213 | priv->pdev_i2c = platform_device_register_simple("i2c_mlxcpld", -1, |
| 214 | NULL, 0); |
| 215 | if (IS_ERR(priv->pdev_i2c)) { |
| 216 | err = PTR_ERR(priv->pdev_i2c); |
| 217 | goto fail_alloc; |
kbuild test robot | c3886c9 | 2016-10-28 01:26:50 +0800 | [diff] [blame^] | 218 | } |
Vadim Pasternak | 58cbbee | 2016-09-22 21:13:42 +0000 | [diff] [blame] | 219 | |
| 220 | for (i = 0; i < ARRAY_SIZE(mlxplat_mux_data); i++) { |
| 221 | priv->pdev_mux[i] = platform_device_register_resndata( |
| 222 | &mlxplat_dev->dev, |
| 223 | "i2c-mux-reg", i, NULL, |
| 224 | 0, &mlxplat_mux_data[i], |
| 225 | sizeof(mlxplat_mux_data[i])); |
| 226 | if (IS_ERR(priv->pdev_mux[i])) { |
| 227 | err = PTR_ERR(priv->pdev_mux[i]); |
| 228 | goto fail_platform_mux_register; |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | return 0; |
| 233 | |
| 234 | fail_platform_mux_register: |
| 235 | for (i--; i > 0 ; i--) |
| 236 | platform_device_unregister(priv->pdev_mux[i]); |
| 237 | platform_device_unregister(priv->pdev_i2c); |
| 238 | fail_alloc: |
| 239 | platform_device_unregister(mlxplat_dev); |
| 240 | |
| 241 | return err; |
| 242 | } |
| 243 | module_init(mlxplat_init); |
| 244 | |
| 245 | static void __exit mlxplat_exit(void) |
| 246 | { |
| 247 | struct mlxplat_priv *priv = platform_get_drvdata(mlxplat_dev); |
| 248 | int i; |
| 249 | |
| 250 | for (i = ARRAY_SIZE(mlxplat_mux_data) - 1; i >= 0 ; i--) |
| 251 | platform_device_unregister(priv->pdev_mux[i]); |
| 252 | |
| 253 | platform_device_unregister(priv->pdev_i2c); |
| 254 | platform_device_unregister(mlxplat_dev); |
| 255 | } |
| 256 | module_exit(mlxplat_exit); |
| 257 | |
| 258 | MODULE_AUTHOR("Vadim Pasternak (vadimp@mellanox.com)"); |
| 259 | MODULE_DESCRIPTION("Mellanox platform driver"); |
| 260 | MODULE_LICENSE("Dual BSD/GPL"); |
| 261 | MODULE_ALIAS("dmi:*:*Mellanox*:MSN24*:"); |
| 262 | MODULE_ALIAS("dmi:*:*Mellanox*:MSN27*:"); |
| 263 | MODULE_ALIAS("dmi:*:*Mellanox*:MSB*:"); |
| 264 | MODULE_ALIAS("dmi:*:*Mellanox*:MSX*:"); |
| 265 | MODULE_ALIAS("dmi:*:*Mellanox*:MSN21*:"); |