Pali Rohár | 5e4d24c | 2022-07-28 13:06:24 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | // (C) 2022 Pali Rohár <pali@kernel.org> |
| 3 | |
| 4 | #include <common.h> |
| 5 | #include <dm.h> |
| 6 | #include <i2c.h> |
| 7 | #include <asm/gpio.h> |
| 8 | #include <linux/log2.h> |
| 9 | |
| 10 | enum commands_e { |
| 11 | CMD_GET_STATUS_WORD = 0x01, |
| 12 | CMD_GENERAL_CONTROL = 0x02, |
| 13 | |
| 14 | /* available if STS_FEATURES_SUPPORTED bit set in status word */ |
| 15 | CMD_GET_FEATURES = 0x10, |
| 16 | |
| 17 | /* available if FEAT_EXT_CMDS bit is set in features */ |
| 18 | CMD_GET_EXT_STATUS_DWORD = 0x11, |
| 19 | CMD_EXT_CONTROL = 0x12, |
| 20 | CMD_GET_EXT_CONTROL_STATUS = 0x13, |
| 21 | }; |
| 22 | |
| 23 | /* CMD_GET_STATUS_WORD */ |
| 24 | enum sts_word_e { |
| 25 | STS_MCU_TYPE_MASK = GENMASK(1, 0), |
| 26 | STS_MCU_TYPE_STM32 = 0, |
| 27 | STS_MCU_TYPE_GD32 = 1, |
| 28 | STS_MCU_TYPE_MKL = 2, |
| 29 | STS_FEATURES_SUPPORTED = BIT(2), |
| 30 | STS_USER_REGULATOR_NOT_SUPPORTED = BIT(3), |
| 31 | STS_CARD_DET = BIT(4), |
| 32 | STS_MSATA_IND = BIT(5), |
| 33 | STS_USB30_OVC = BIT(6), |
| 34 | STS_USB31_OVC = BIT(7), |
| 35 | STS_USB30_PWRON = BIT(8), |
| 36 | STS_USB31_PWRON = BIT(9), |
| 37 | STS_ENABLE_4V5 = BIT(10), |
| 38 | STS_BUTTON_MODE = BIT(11), |
| 39 | STS_BUTTON_PRESSED = BIT(12), |
| 40 | STS_BUTTON_COUNTER_MASK = GENMASK(15, 13) |
| 41 | }; |
| 42 | |
| 43 | /* CMD_GENERAL_CONTROL */ |
| 44 | enum ctl_byte_e { |
| 45 | CTL_LIGHT_RST = BIT(0), |
| 46 | CTL_HARD_RST = BIT(1), |
| 47 | /*CTL_RESERVED = BIT(2),*/ |
| 48 | CTL_USB30_PWRON = BIT(3), |
| 49 | CTL_USB31_PWRON = BIT(4), |
| 50 | CTL_ENABLE_4V5 = BIT(5), |
| 51 | CTL_BUTTON_MODE = BIT(6), |
| 52 | CTL_BOOTLOADER = BIT(7) |
| 53 | }; |
| 54 | |
| 55 | /* CMD_GET_FEATURES */ |
| 56 | enum features_e { |
| 57 | FEAT_EXT_CMDS = BIT(1), |
| 58 | }; |
| 59 | |
| 60 | struct turris_omnia_mcu_info { |
| 61 | u16 features; |
| 62 | }; |
| 63 | |
| 64 | static int turris_omnia_mcu_get_function(struct udevice *dev, uint offset) |
| 65 | { |
| 66 | struct turris_omnia_mcu_info *info = dev_get_plat(dev); |
| 67 | |
| 68 | switch (offset) { |
| 69 | /* bank 0 */ |
| 70 | case 0 ... 15: |
| 71 | switch (offset) { |
| 72 | case ilog2(STS_USB30_PWRON): |
| 73 | case ilog2(STS_USB31_PWRON): |
| 74 | case ilog2(STS_ENABLE_4V5): |
| 75 | case ilog2(STS_BUTTON_MODE): |
| 76 | return GPIOF_OUTPUT; |
| 77 | default: |
| 78 | return GPIOF_INPUT; |
| 79 | } |
| 80 | |
| 81 | /* bank 1 - supported only when FEAT_EXT_CMDS is set */ |
| 82 | case (16 + 0) ... (16 + 31): |
| 83 | if (!(info->features & FEAT_EXT_CMDS)) |
| 84 | return -EINVAL; |
| 85 | return GPIOF_INPUT; |
| 86 | |
| 87 | /* bank 2 - supported only when FEAT_EXT_CMDS is set */ |
| 88 | case (16 + 32 + 0) ... (16 + 32 + 15): |
| 89 | if (!(info->features & FEAT_EXT_CMDS)) |
| 90 | return -EINVAL; |
| 91 | return GPIOF_OUTPUT; |
| 92 | |
| 93 | default: |
| 94 | return -EINVAL; |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | static int turris_omnia_mcu_get_value(struct udevice *dev, uint offset) |
| 99 | { |
| 100 | struct turris_omnia_mcu_info *info = dev_get_plat(dev); |
| 101 | u8 val16[2]; |
| 102 | u8 val32[4]; |
| 103 | int ret; |
| 104 | |
| 105 | switch (offset) { |
| 106 | /* bank 0 */ |
| 107 | case 0 ... 15: |
| 108 | ret = dm_i2c_read(dev, CMD_GET_STATUS_WORD, val16, 2); |
| 109 | if (ret) |
| 110 | return ret; |
| 111 | return ((((u16)val16[1] << 8) | val16[0]) >> offset) & 0x1; |
| 112 | |
| 113 | /* bank 1 - supported only when FEAT_EXT_CMDS is set */ |
| 114 | case (16 + 0) ... (16 + 31): |
| 115 | if (!(info->features & FEAT_EXT_CMDS)) |
| 116 | return -EINVAL; |
| 117 | ret = dm_i2c_read(dev, CMD_GET_EXT_STATUS_DWORD, val32, 4); |
| 118 | if (ret) |
| 119 | return ret; |
| 120 | return ((((u32)val32[3] << 24) | ((u32)val32[2] << 16) | |
| 121 | ((u32)val32[1] << 8) | val32[0]) >> (offset - 16)) & 0x1; |
| 122 | |
| 123 | /* bank 2 - supported only when FEAT_EXT_CMDS is set */ |
| 124 | case (16 + 32 + 0) ... (16 + 32 + 15): |
| 125 | if (!(info->features & FEAT_EXT_CMDS)) |
| 126 | return -EINVAL; |
| 127 | ret = dm_i2c_read(dev, CMD_GET_EXT_CONTROL_STATUS, val16, 2); |
| 128 | if (ret) |
| 129 | return ret; |
| 130 | return ((((u16)val16[1] << 8) | val16[0]) >> (offset - 16 - 32)) & 0x1; |
| 131 | |
| 132 | default: |
| 133 | return -EINVAL; |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | static int turris_omnia_mcu_set_value(struct udevice *dev, uint offset, int value) |
| 138 | { |
| 139 | struct turris_omnia_mcu_info *info = dev_get_plat(dev); |
| 140 | u8 val[2]; |
| 141 | int ret; |
| 142 | u8 reg; |
| 143 | |
| 144 | switch (offset) { |
| 145 | /* bank 0 */ |
| 146 | case ilog2(STS_USB30_PWRON): |
| 147 | reg = CMD_GENERAL_CONTROL; |
| 148 | val[1] = CTL_USB30_PWRON; |
| 149 | break; |
| 150 | case ilog2(STS_USB31_PWRON): |
| 151 | reg = CMD_GENERAL_CONTROL; |
| 152 | val[1] = CTL_USB31_PWRON; |
| 153 | break; |
| 154 | case ilog2(STS_ENABLE_4V5): |
| 155 | reg = CMD_GENERAL_CONTROL; |
| 156 | val[1] = CTL_ENABLE_4V5; |
| 157 | break; |
| 158 | case ilog2(STS_BUTTON_MODE): |
| 159 | reg = CMD_GENERAL_CONTROL; |
| 160 | val[1] = CTL_BUTTON_MODE; |
| 161 | break; |
| 162 | |
| 163 | /* bank 2 - supported only when FEAT_EXT_CMDS is set */ |
| 164 | case (16 + 32 + 0) ... (16 + 32 + 15): |
| 165 | if (!(info->features & FEAT_EXT_CMDS)) |
| 166 | return -EINVAL; |
| 167 | reg = CMD_EXT_CONTROL; |
| 168 | val[1] = BIT(offset - 16 - 32); |
| 169 | break; |
| 170 | |
| 171 | default: |
| 172 | return -EINVAL; |
| 173 | } |
| 174 | |
| 175 | val[0] = value ? val[1] : 0; |
| 176 | |
| 177 | ret = dm_i2c_write(dev, reg, val, 2); |
| 178 | if (ret) |
| 179 | return ret; |
| 180 | |
| 181 | return 0; |
| 182 | } |
| 183 | |
| 184 | static int turris_omnia_mcu_direction_input(struct udevice *dev, uint offset) |
| 185 | { |
| 186 | int ret; |
| 187 | |
| 188 | ret = turris_omnia_mcu_get_function(dev, offset); |
| 189 | if (ret < 0) |
| 190 | return ret; |
| 191 | else if (ret != GPIOF_INPUT) |
| 192 | return -EOPNOTSUPP; |
| 193 | |
| 194 | return 0; |
| 195 | } |
| 196 | |
| 197 | static int turris_omnia_mcu_direction_output(struct udevice *dev, uint offset, int value) |
| 198 | { |
| 199 | int ret; |
| 200 | |
| 201 | ret = turris_omnia_mcu_get_function(dev, offset); |
| 202 | if (ret < 0) |
| 203 | return ret; |
| 204 | else if (ret != GPIOF_OUTPUT) |
| 205 | return -EOPNOTSUPP; |
| 206 | |
| 207 | return turris_omnia_mcu_set_value(dev, offset, value); |
| 208 | } |
| 209 | |
| 210 | static int turris_omnia_mcu_xlate(struct udevice *dev, struct gpio_desc *desc, |
| 211 | struct ofnode_phandle_args *args) |
| 212 | { |
| 213 | uint bank, gpio, flags, offset; |
| 214 | int ret; |
| 215 | |
| 216 | if (args->args_count != 3) |
| 217 | return -EINVAL; |
| 218 | |
| 219 | bank = args->args[0]; |
| 220 | gpio = args->args[1]; |
| 221 | flags = args->args[2]; |
| 222 | |
| 223 | switch (bank) { |
| 224 | case 0: |
| 225 | if (gpio >= 16) |
| 226 | return -EINVAL; |
| 227 | offset = gpio; |
| 228 | break; |
| 229 | case 1: |
| 230 | if (gpio >= 32) |
| 231 | return -EINVAL; |
| 232 | offset = 16 + gpio; |
| 233 | break; |
| 234 | case 2: |
| 235 | if (gpio >= 16) |
| 236 | return -EINVAL; |
| 237 | offset = 16 + 32 + gpio; |
| 238 | break; |
| 239 | default: |
| 240 | return -EINVAL; |
| 241 | } |
| 242 | |
| 243 | ret = turris_omnia_mcu_get_function(dev, offset); |
| 244 | if (ret < 0) |
| 245 | return ret; |
| 246 | |
| 247 | desc->offset = offset; |
| 248 | desc->flags = gpio_flags_xlate(flags); |
| 249 | |
| 250 | return 0; |
| 251 | } |
| 252 | |
| 253 | static const struct dm_gpio_ops turris_omnia_mcu_ops = { |
| 254 | .direction_input = turris_omnia_mcu_direction_input, |
| 255 | .direction_output = turris_omnia_mcu_direction_output, |
| 256 | .get_value = turris_omnia_mcu_get_value, |
| 257 | .set_value = turris_omnia_mcu_set_value, |
| 258 | .get_function = turris_omnia_mcu_get_function, |
| 259 | .xlate = turris_omnia_mcu_xlate, |
| 260 | }; |
| 261 | |
| 262 | static int turris_omnia_mcu_probe(struct udevice *dev) |
| 263 | { |
| 264 | struct turris_omnia_mcu_info *info = dev_get_plat(dev); |
| 265 | struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev); |
| 266 | u16 status; |
| 267 | u8 val[2]; |
| 268 | int ret; |
| 269 | |
| 270 | ret = dm_i2c_read(dev, CMD_GET_STATUS_WORD, val, 2); |
| 271 | if (ret) { |
| 272 | printf("Error: turris_omnia_mcu CMD_GET_STATUS_WORD failed: %d\n", ret); |
| 273 | return ret; |
| 274 | } |
| 275 | |
| 276 | status = ((u16)val[1] << 8) | val[0]; |
| 277 | |
| 278 | if (status & STS_FEATURES_SUPPORTED) { |
| 279 | ret = dm_i2c_read(dev, CMD_GET_FEATURES, val, 2); |
| 280 | if (ret) { |
| 281 | printf("Error: turris_omnia_mcu CMD_GET_FEATURES failed: %d\n", ret); |
| 282 | return ret; |
| 283 | } |
| 284 | info->features = ((u16)val[1] << 8) | val[0]; |
| 285 | } |
| 286 | |
| 287 | uc_priv->bank_name = "mcu_"; |
| 288 | |
| 289 | if (info->features & FEAT_EXT_CMDS) |
| 290 | uc_priv->gpio_count = 16 + 32 + 16; |
| 291 | else |
| 292 | uc_priv->gpio_count = 16; |
| 293 | |
| 294 | return 0; |
| 295 | } |
| 296 | |
| 297 | static const struct udevice_id turris_omnia_mcu_ids[] = { |
| 298 | { .compatible = "cznic,turris-omnia-mcu" }, |
| 299 | { } |
| 300 | }; |
| 301 | |
| 302 | U_BOOT_DRIVER(turris_omnia_mcu) = { |
| 303 | .name = "turris-omnia-mcu", |
| 304 | .id = UCLASS_GPIO, |
| 305 | .ops = &turris_omnia_mcu_ops, |
| 306 | .probe = turris_omnia_mcu_probe, |
| 307 | .plat_auto = sizeof(struct turris_omnia_mcu_info), |
| 308 | .of_match = turris_omnia_mcu_ids, |
| 309 | }; |