0001
0002
0003
0004
0005
0006
0007
0008 #include <linux/bitfield.h>
0009 #include <linux/i2c.h>
0010 #include <linux/interrupt.h>
0011 #include <linux/irq.h>
0012 #include <linux/mfd/core.h>
0013 #include <linux/mfd/stmfx.h>
0014 #include <linux/module.h>
0015 #include <linux/regulator/consumer.h>
0016
0017 static bool stmfx_reg_volatile(struct device *dev, unsigned int reg)
0018 {
0019 switch (reg) {
0020 case STMFX_REG_SYS_CTRL:
0021 case STMFX_REG_IRQ_SRC_EN:
0022 case STMFX_REG_IRQ_PENDING:
0023 case STMFX_REG_IRQ_GPI_PENDING1:
0024 case STMFX_REG_IRQ_GPI_PENDING2:
0025 case STMFX_REG_IRQ_GPI_PENDING3:
0026 case STMFX_REG_GPIO_STATE1:
0027 case STMFX_REG_GPIO_STATE2:
0028 case STMFX_REG_GPIO_STATE3:
0029 case STMFX_REG_IRQ_GPI_SRC1:
0030 case STMFX_REG_IRQ_GPI_SRC2:
0031 case STMFX_REG_IRQ_GPI_SRC3:
0032 case STMFX_REG_GPO_SET1:
0033 case STMFX_REG_GPO_SET2:
0034 case STMFX_REG_GPO_SET3:
0035 case STMFX_REG_GPO_CLR1:
0036 case STMFX_REG_GPO_CLR2:
0037 case STMFX_REG_GPO_CLR3:
0038 return true;
0039 default:
0040 return false;
0041 }
0042 }
0043
0044 static bool stmfx_reg_writeable(struct device *dev, unsigned int reg)
0045 {
0046 return (reg >= STMFX_REG_SYS_CTRL);
0047 }
0048
0049 static const struct regmap_config stmfx_regmap_config = {
0050 .reg_bits = 8,
0051 .reg_stride = 1,
0052 .val_bits = 8,
0053 .max_register = STMFX_REG_MAX,
0054 .volatile_reg = stmfx_reg_volatile,
0055 .writeable_reg = stmfx_reg_writeable,
0056 .cache_type = REGCACHE_RBTREE,
0057 };
0058
0059 static const struct resource stmfx_pinctrl_resources[] = {
0060 DEFINE_RES_IRQ(STMFX_REG_IRQ_SRC_EN_GPIO),
0061 };
0062
0063 static const struct resource stmfx_idd_resources[] = {
0064 DEFINE_RES_IRQ(STMFX_REG_IRQ_SRC_EN_IDD),
0065 DEFINE_RES_IRQ(STMFX_REG_IRQ_SRC_EN_ERROR),
0066 };
0067
0068 static const struct resource stmfx_ts_resources[] = {
0069 DEFINE_RES_IRQ(STMFX_REG_IRQ_SRC_EN_TS_DET),
0070 DEFINE_RES_IRQ(STMFX_REG_IRQ_SRC_EN_TS_NE),
0071 DEFINE_RES_IRQ(STMFX_REG_IRQ_SRC_EN_TS_TH),
0072 DEFINE_RES_IRQ(STMFX_REG_IRQ_SRC_EN_TS_FULL),
0073 DEFINE_RES_IRQ(STMFX_REG_IRQ_SRC_EN_TS_OVF),
0074 };
0075
0076 static struct mfd_cell stmfx_cells[] = {
0077 {
0078 .of_compatible = "st,stmfx-0300-pinctrl",
0079 .name = "stmfx-pinctrl",
0080 .resources = stmfx_pinctrl_resources,
0081 .num_resources = ARRAY_SIZE(stmfx_pinctrl_resources),
0082 },
0083 {
0084 .of_compatible = "st,stmfx-0300-idd",
0085 .name = "stmfx-idd",
0086 .resources = stmfx_idd_resources,
0087 .num_resources = ARRAY_SIZE(stmfx_idd_resources),
0088 },
0089 {
0090 .of_compatible = "st,stmfx-0300-ts",
0091 .name = "stmfx-ts",
0092 .resources = stmfx_ts_resources,
0093 .num_resources = ARRAY_SIZE(stmfx_ts_resources),
0094 },
0095 };
0096
0097 static u8 stmfx_func_to_mask(u32 func)
0098 {
0099 u8 mask = 0;
0100
0101 if (func & STMFX_FUNC_GPIO)
0102 mask |= STMFX_REG_SYS_CTRL_GPIO_EN;
0103
0104 if ((func & STMFX_FUNC_ALTGPIO_LOW) || (func & STMFX_FUNC_ALTGPIO_HIGH))
0105 mask |= STMFX_REG_SYS_CTRL_ALTGPIO_EN;
0106
0107 if (func & STMFX_FUNC_TS)
0108 mask |= STMFX_REG_SYS_CTRL_TS_EN;
0109
0110 if (func & STMFX_FUNC_IDD)
0111 mask |= STMFX_REG_SYS_CTRL_IDD_EN;
0112
0113 return mask;
0114 }
0115
0116 int stmfx_function_enable(struct stmfx *stmfx, u32 func)
0117 {
0118 u32 sys_ctrl;
0119 u8 mask;
0120 int ret;
0121
0122 ret = regmap_read(stmfx->map, STMFX_REG_SYS_CTRL, &sys_ctrl);
0123 if (ret)
0124 return ret;
0125
0126
0127
0128
0129
0130
0131
0132 if (((func & STMFX_FUNC_IDD) || (func & STMFX_FUNC_TS)) &&
0133 (sys_ctrl & STMFX_REG_SYS_CTRL_ALTGPIO_EN)) {
0134 dev_err(stmfx->dev, "ALTGPIO function already enabled\n");
0135 return -EBUSY;
0136 }
0137
0138
0139 if ((func & STMFX_FUNC_ALTGPIO_LOW) &&
0140 (sys_ctrl & STMFX_REG_SYS_CTRL_TS_EN)) {
0141 dev_err(stmfx->dev, "TS in use, aGPIO[3:0] unavailable\n");
0142 return -EBUSY;
0143 }
0144
0145
0146 if ((func & STMFX_FUNC_ALTGPIO_HIGH) &&
0147 (sys_ctrl & STMFX_REG_SYS_CTRL_IDD_EN)) {
0148 dev_err(stmfx->dev, "IDD in use, aGPIO[7:4] unavailable\n");
0149 return -EBUSY;
0150 }
0151
0152 mask = stmfx_func_to_mask(func);
0153
0154 return regmap_update_bits(stmfx->map, STMFX_REG_SYS_CTRL, mask, mask);
0155 }
0156 EXPORT_SYMBOL_GPL(stmfx_function_enable);
0157
0158 int stmfx_function_disable(struct stmfx *stmfx, u32 func)
0159 {
0160 u8 mask = stmfx_func_to_mask(func);
0161
0162 return regmap_update_bits(stmfx->map, STMFX_REG_SYS_CTRL, mask, 0);
0163 }
0164 EXPORT_SYMBOL_GPL(stmfx_function_disable);
0165
0166 static void stmfx_irq_bus_lock(struct irq_data *data)
0167 {
0168 struct stmfx *stmfx = irq_data_get_irq_chip_data(data);
0169
0170 mutex_lock(&stmfx->lock);
0171 }
0172
0173 static void stmfx_irq_bus_sync_unlock(struct irq_data *data)
0174 {
0175 struct stmfx *stmfx = irq_data_get_irq_chip_data(data);
0176
0177 regmap_write(stmfx->map, STMFX_REG_IRQ_SRC_EN, stmfx->irq_src);
0178
0179 mutex_unlock(&stmfx->lock);
0180 }
0181
0182 static void stmfx_irq_mask(struct irq_data *data)
0183 {
0184 struct stmfx *stmfx = irq_data_get_irq_chip_data(data);
0185
0186 stmfx->irq_src &= ~BIT(data->hwirq % 8);
0187 }
0188
0189 static void stmfx_irq_unmask(struct irq_data *data)
0190 {
0191 struct stmfx *stmfx = irq_data_get_irq_chip_data(data);
0192
0193 stmfx->irq_src |= BIT(data->hwirq % 8);
0194 }
0195
0196 static struct irq_chip stmfx_irq_chip = {
0197 .name = "stmfx-core",
0198 .irq_bus_lock = stmfx_irq_bus_lock,
0199 .irq_bus_sync_unlock = stmfx_irq_bus_sync_unlock,
0200 .irq_mask = stmfx_irq_mask,
0201 .irq_unmask = stmfx_irq_unmask,
0202 };
0203
0204 static irqreturn_t stmfx_irq_handler(int irq, void *data)
0205 {
0206 struct stmfx *stmfx = data;
0207 unsigned long bits;
0208 u32 pending, ack;
0209 int n, ret;
0210
0211 ret = regmap_read(stmfx->map, STMFX_REG_IRQ_PENDING, &pending);
0212 if (ret)
0213 return IRQ_NONE;
0214
0215
0216
0217
0218
0219 ack = pending & ~BIT(STMFX_REG_IRQ_SRC_EN_GPIO);
0220 if (ack) {
0221 ret = regmap_write(stmfx->map, STMFX_REG_IRQ_ACK, ack);
0222 if (ret)
0223 return IRQ_NONE;
0224 }
0225
0226 bits = pending;
0227 for_each_set_bit(n, &bits, STMFX_REG_IRQ_SRC_MAX)
0228 handle_nested_irq(irq_find_mapping(stmfx->irq_domain, n));
0229
0230 return IRQ_HANDLED;
0231 }
0232
0233 static int stmfx_irq_map(struct irq_domain *d, unsigned int virq,
0234 irq_hw_number_t hwirq)
0235 {
0236 irq_set_chip_data(virq, d->host_data);
0237 irq_set_chip_and_handler(virq, &stmfx_irq_chip, handle_simple_irq);
0238 irq_set_nested_thread(virq, 1);
0239 irq_set_noprobe(virq);
0240
0241 return 0;
0242 }
0243
0244 static void stmfx_irq_unmap(struct irq_domain *d, unsigned int virq)
0245 {
0246 irq_set_chip_and_handler(virq, NULL, NULL);
0247 irq_set_chip_data(virq, NULL);
0248 }
0249
0250 static const struct irq_domain_ops stmfx_irq_ops = {
0251 .map = stmfx_irq_map,
0252 .unmap = stmfx_irq_unmap,
0253 };
0254
0255 static void stmfx_irq_exit(struct i2c_client *client)
0256 {
0257 struct stmfx *stmfx = i2c_get_clientdata(client);
0258 int hwirq;
0259
0260 for (hwirq = 0; hwirq < STMFX_REG_IRQ_SRC_MAX; hwirq++)
0261 irq_dispose_mapping(irq_find_mapping(stmfx->irq_domain, hwirq));
0262
0263 irq_domain_remove(stmfx->irq_domain);
0264 }
0265
0266 static int stmfx_irq_init(struct i2c_client *client)
0267 {
0268 struct stmfx *stmfx = i2c_get_clientdata(client);
0269 u32 irqoutpin = 0, irqtrigger;
0270 int ret;
0271
0272 stmfx->irq_domain = irq_domain_add_simple(stmfx->dev->of_node,
0273 STMFX_REG_IRQ_SRC_MAX, 0,
0274 &stmfx_irq_ops, stmfx);
0275 if (!stmfx->irq_domain) {
0276 dev_err(stmfx->dev, "Failed to create IRQ domain\n");
0277 return -EINVAL;
0278 }
0279
0280 if (!of_property_read_bool(stmfx->dev->of_node, "drive-open-drain"))
0281 irqoutpin |= STMFX_REG_IRQ_OUT_PIN_TYPE;
0282
0283 irqtrigger = irq_get_trigger_type(client->irq);
0284 if ((irqtrigger & IRQ_TYPE_EDGE_RISING) ||
0285 (irqtrigger & IRQ_TYPE_LEVEL_HIGH))
0286 irqoutpin |= STMFX_REG_IRQ_OUT_PIN_POL;
0287
0288 ret = regmap_write(stmfx->map, STMFX_REG_IRQ_OUT_PIN, irqoutpin);
0289 if (ret)
0290 goto irq_exit;
0291
0292 ret = devm_request_threaded_irq(stmfx->dev, client->irq,
0293 NULL, stmfx_irq_handler,
0294 irqtrigger | IRQF_ONESHOT,
0295 "stmfx", stmfx);
0296 if (ret)
0297 goto irq_exit;
0298
0299 stmfx->irq = client->irq;
0300
0301 return 0;
0302
0303 irq_exit:
0304 stmfx_irq_exit(client);
0305
0306 return ret;
0307 }
0308
0309 static int stmfx_chip_reset(struct stmfx *stmfx)
0310 {
0311 int ret;
0312
0313 ret = regmap_write(stmfx->map, STMFX_REG_SYS_CTRL,
0314 STMFX_REG_SYS_CTRL_SWRST);
0315 if (ret)
0316 return ret;
0317
0318 msleep(STMFX_BOOT_TIME_MS);
0319
0320 return ret;
0321 }
0322
0323 static int stmfx_chip_init(struct i2c_client *client)
0324 {
0325 struct stmfx *stmfx = i2c_get_clientdata(client);
0326 u32 id;
0327 u8 version[2];
0328 int ret;
0329
0330 stmfx->vdd = devm_regulator_get_optional(&client->dev, "vdd");
0331 ret = PTR_ERR_OR_ZERO(stmfx->vdd);
0332 if (ret) {
0333 if (ret == -ENODEV)
0334 stmfx->vdd = NULL;
0335 else
0336 return dev_err_probe(&client->dev, ret, "Failed to get VDD regulator\n");
0337 }
0338
0339 if (stmfx->vdd) {
0340 ret = regulator_enable(stmfx->vdd);
0341 if (ret) {
0342 dev_err(&client->dev, "VDD enable failed: %d\n", ret);
0343 return ret;
0344 }
0345 }
0346
0347 ret = regmap_read(stmfx->map, STMFX_REG_CHIP_ID, &id);
0348 if (ret) {
0349 dev_err(&client->dev, "Error reading chip ID: %d\n", ret);
0350 goto err;
0351 }
0352
0353
0354
0355
0356
0357
0358
0359
0360
0361
0362
0363
0364 if (FIELD_GET(STMFX_REG_CHIP_ID_MASK, ~id) != (client->addr << 1)) {
0365 dev_err(&client->dev, "Unknown chip ID: %#x\n", id);
0366 ret = -EINVAL;
0367 goto err;
0368 }
0369
0370 ret = regmap_bulk_read(stmfx->map, STMFX_REG_FW_VERSION_MSB,
0371 version, ARRAY_SIZE(version));
0372 if (ret) {
0373 dev_err(&client->dev, "Error reading FW version: %d\n", ret);
0374 goto err;
0375 }
0376
0377 dev_info(&client->dev, "STMFX id: %#x, fw version: %x.%02x\n",
0378 id, version[0], version[1]);
0379
0380 ret = stmfx_chip_reset(stmfx);
0381 if (ret) {
0382 dev_err(&client->dev, "Failed to reset chip: %d\n", ret);
0383 goto err;
0384 }
0385
0386 return 0;
0387
0388 err:
0389 if (stmfx->vdd)
0390 return regulator_disable(stmfx->vdd);
0391
0392 return ret;
0393 }
0394
0395 static void stmfx_chip_exit(struct i2c_client *client)
0396 {
0397 struct stmfx *stmfx = i2c_get_clientdata(client);
0398
0399 regmap_write(stmfx->map, STMFX_REG_IRQ_SRC_EN, 0);
0400 regmap_write(stmfx->map, STMFX_REG_SYS_CTRL, 0);
0401
0402 if (stmfx->vdd) {
0403 int ret;
0404
0405 ret = regulator_disable(stmfx->vdd);
0406 if (ret)
0407 dev_err(&client->dev,
0408 "Failed to disable vdd regulator: %pe\n",
0409 ERR_PTR(ret));
0410 }
0411 }
0412
0413 static int stmfx_probe(struct i2c_client *client,
0414 const struct i2c_device_id *id)
0415 {
0416 struct device *dev = &client->dev;
0417 struct stmfx *stmfx;
0418 int ret;
0419
0420 stmfx = devm_kzalloc(dev, sizeof(*stmfx), GFP_KERNEL);
0421 if (!stmfx)
0422 return -ENOMEM;
0423
0424 i2c_set_clientdata(client, stmfx);
0425
0426 stmfx->dev = dev;
0427
0428 stmfx->map = devm_regmap_init_i2c(client, &stmfx_regmap_config);
0429 if (IS_ERR(stmfx->map)) {
0430 ret = PTR_ERR(stmfx->map);
0431 dev_err(dev, "Failed to allocate register map: %d\n", ret);
0432 return ret;
0433 }
0434
0435 mutex_init(&stmfx->lock);
0436
0437 ret = stmfx_chip_init(client);
0438 if (ret) {
0439 if (ret == -ETIMEDOUT)
0440 return -EPROBE_DEFER;
0441 return ret;
0442 }
0443
0444 if (client->irq < 0) {
0445 dev_err(dev, "Failed to get IRQ: %d\n", client->irq);
0446 ret = client->irq;
0447 goto err_chip_exit;
0448 }
0449
0450 ret = stmfx_irq_init(client);
0451 if (ret)
0452 goto err_chip_exit;
0453
0454 ret = devm_mfd_add_devices(dev, PLATFORM_DEVID_NONE,
0455 stmfx_cells, ARRAY_SIZE(stmfx_cells), NULL,
0456 0, stmfx->irq_domain);
0457 if (ret)
0458 goto err_irq_exit;
0459
0460 return 0;
0461
0462 err_irq_exit:
0463 stmfx_irq_exit(client);
0464 err_chip_exit:
0465 stmfx_chip_exit(client);
0466
0467 return ret;
0468 }
0469
0470 static int stmfx_remove(struct i2c_client *client)
0471 {
0472 stmfx_irq_exit(client);
0473
0474 stmfx_chip_exit(client);
0475
0476 return 0;
0477 }
0478
0479 #ifdef CONFIG_PM_SLEEP
0480 static int stmfx_suspend(struct device *dev)
0481 {
0482 struct stmfx *stmfx = dev_get_drvdata(dev);
0483 int ret;
0484
0485 ret = regmap_raw_read(stmfx->map, STMFX_REG_SYS_CTRL,
0486 &stmfx->bkp_sysctrl, sizeof(stmfx->bkp_sysctrl));
0487 if (ret)
0488 return ret;
0489
0490 ret = regmap_raw_read(stmfx->map, STMFX_REG_IRQ_OUT_PIN,
0491 &stmfx->bkp_irqoutpin,
0492 sizeof(stmfx->bkp_irqoutpin));
0493 if (ret)
0494 return ret;
0495
0496 disable_irq(stmfx->irq);
0497
0498 if (stmfx->vdd)
0499 return regulator_disable(stmfx->vdd);
0500
0501 return 0;
0502 }
0503
0504 static int stmfx_resume(struct device *dev)
0505 {
0506 struct stmfx *stmfx = dev_get_drvdata(dev);
0507 int ret;
0508
0509 if (stmfx->vdd) {
0510 ret = regulator_enable(stmfx->vdd);
0511 if (ret) {
0512 dev_err(stmfx->dev,
0513 "VDD enable failed: %d\n", ret);
0514 return ret;
0515 }
0516 }
0517
0518
0519 ret = stmfx_chip_reset(stmfx);
0520 if (ret) {
0521 dev_err(stmfx->dev, "Failed to reset chip: %d\n", ret);
0522 return ret;
0523 }
0524
0525 ret = regmap_raw_write(stmfx->map, STMFX_REG_SYS_CTRL,
0526 &stmfx->bkp_sysctrl, sizeof(stmfx->bkp_sysctrl));
0527 if (ret)
0528 return ret;
0529
0530 ret = regmap_raw_write(stmfx->map, STMFX_REG_IRQ_OUT_PIN,
0531 &stmfx->bkp_irqoutpin,
0532 sizeof(stmfx->bkp_irqoutpin));
0533 if (ret)
0534 return ret;
0535
0536 ret = regmap_raw_write(stmfx->map, STMFX_REG_IRQ_SRC_EN,
0537 &stmfx->irq_src, sizeof(stmfx->irq_src));
0538 if (ret)
0539 return ret;
0540
0541 enable_irq(stmfx->irq);
0542
0543 return 0;
0544 }
0545 #endif
0546
0547 static SIMPLE_DEV_PM_OPS(stmfx_dev_pm_ops, stmfx_suspend, stmfx_resume);
0548
0549 static const struct of_device_id stmfx_of_match[] = {
0550 { .compatible = "st,stmfx-0300", },
0551 {},
0552 };
0553 MODULE_DEVICE_TABLE(of, stmfx_of_match);
0554
0555 static struct i2c_driver stmfx_driver = {
0556 .driver = {
0557 .name = "stmfx-core",
0558 .of_match_table = stmfx_of_match,
0559 .pm = &stmfx_dev_pm_ops,
0560 },
0561 .probe = stmfx_probe,
0562 .remove = stmfx_remove,
0563 };
0564 module_i2c_driver(stmfx_driver);
0565
0566 MODULE_DESCRIPTION("STMFX core driver");
0567 MODULE_AUTHOR("Amelie Delaunay <amelie.delaunay@st.com>");
0568 MODULE_LICENSE("GPL v2");