144b4dc61SKeerthy /* 244b4dc61SKeerthy * Driver for TPS65218 Integrated power management chipsets 344b4dc61SKeerthy * 444b4dc61SKeerthy * Copyright (C) 2014 Texas Instruments Incorporated - http://www.ti.com/ 544b4dc61SKeerthy * 644b4dc61SKeerthy * This program is free software; you can redistribute it and/or 744b4dc61SKeerthy * modify it under the terms of the GNU General Public License version 2 as 844b4dc61SKeerthy * published by the Free Software Foundation. 944b4dc61SKeerthy * 1044b4dc61SKeerthy * This program is distributed "as is" WITHOUT ANY WARRANTY of any 1144b4dc61SKeerthy * kind, whether expressed or implied; without even the implied warranty 1244b4dc61SKeerthy * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 1344b4dc61SKeerthy * GNU General Public License version 2 for more details. 1444b4dc61SKeerthy */ 1544b4dc61SKeerthy 1644b4dc61SKeerthy #include <linux/kernel.h> 1744b4dc61SKeerthy #include <linux/device.h> 1844b4dc61SKeerthy #include <linux/module.h> 1944b4dc61SKeerthy #include <linux/platform_device.h> 2044b4dc61SKeerthy #include <linux/init.h> 2144b4dc61SKeerthy #include <linux/i2c.h> 2244b4dc61SKeerthy #include <linux/slab.h> 2344b4dc61SKeerthy #include <linux/regmap.h> 2444b4dc61SKeerthy #include <linux/err.h> 2544b4dc61SKeerthy #include <linux/of.h> 2644b4dc61SKeerthy #include <linux/of_device.h> 2744b4dc61SKeerthy #include <linux/irq.h> 2844b4dc61SKeerthy #include <linux/interrupt.h> 2944b4dc61SKeerthy #include <linux/mutex.h> 3044b4dc61SKeerthy 3144b4dc61SKeerthy #include <linux/mfd/core.h> 3244b4dc61SKeerthy #include <linux/mfd/tps65218.h> 3344b4dc61SKeerthy 3444b4dc61SKeerthy #define TPS65218_PASSWORD_REGS_UNLOCK 0x7D 3544b4dc61SKeerthy 364531156dSKeerthy static const struct mfd_cell tps65218_cells[] = { 374531156dSKeerthy { 384531156dSKeerthy .name = "tps65218-pwrbutton", 394531156dSKeerthy .of_compatible = "ti,tps65218-pwrbutton", 404531156dSKeerthy }, 414531156dSKeerthy { 424531156dSKeerthy .name = "tps65218-gpio", 434531156dSKeerthy .of_compatible = "ti,tps65218-gpio", 444531156dSKeerthy }, 454531156dSKeerthy { .name = "tps65218-regulator", }, 464531156dSKeerthy }; 474531156dSKeerthy 4844b4dc61SKeerthy /** 4944b4dc61SKeerthy * tps65218_reg_write: Write a single tps65218 register. 5044b4dc61SKeerthy * 5144b4dc61SKeerthy * @tps65218: Device to write to. 5244b4dc61SKeerthy * @reg: Register to write to. 5344b4dc61SKeerthy * @val: Value to write. 5444b4dc61SKeerthy * @level: Password protected level 5544b4dc61SKeerthy */ 5644b4dc61SKeerthy int tps65218_reg_write(struct tps65218 *tps, unsigned int reg, 5744b4dc61SKeerthy unsigned int val, unsigned int level) 5844b4dc61SKeerthy { 5944b4dc61SKeerthy int ret; 6044b4dc61SKeerthy unsigned int xor_reg_val; 6144b4dc61SKeerthy 6244b4dc61SKeerthy switch (level) { 6344b4dc61SKeerthy case TPS65218_PROTECT_NONE: 6444b4dc61SKeerthy return regmap_write(tps->regmap, reg, val); 6544b4dc61SKeerthy case TPS65218_PROTECT_L1: 6644b4dc61SKeerthy xor_reg_val = reg ^ TPS65218_PASSWORD_REGS_UNLOCK; 6744b4dc61SKeerthy ret = regmap_write(tps->regmap, TPS65218_REG_PASSWORD, 6844b4dc61SKeerthy xor_reg_val); 6944b4dc61SKeerthy if (ret < 0) 7044b4dc61SKeerthy return ret; 7144b4dc61SKeerthy 7244b4dc61SKeerthy return regmap_write(tps->regmap, reg, val); 7344b4dc61SKeerthy default: 7444b4dc61SKeerthy return -EINVAL; 7544b4dc61SKeerthy } 7644b4dc61SKeerthy } 7744b4dc61SKeerthy EXPORT_SYMBOL_GPL(tps65218_reg_write); 7844b4dc61SKeerthy 7944b4dc61SKeerthy /** 8044b4dc61SKeerthy * tps65218_update_bits: Modify bits w.r.t mask, val and level. 8144b4dc61SKeerthy * 8244b4dc61SKeerthy * @tps65218: Device to write to. 8344b4dc61SKeerthy * @reg: Register to read-write to. 8444b4dc61SKeerthy * @mask: Mask. 8544b4dc61SKeerthy * @val: Value to write. 8644b4dc61SKeerthy * @level: Password protected level 8744b4dc61SKeerthy */ 8844b4dc61SKeerthy static int tps65218_update_bits(struct tps65218 *tps, unsigned int reg, 8944b4dc61SKeerthy unsigned int mask, unsigned int val, unsigned int level) 9044b4dc61SKeerthy { 9144b4dc61SKeerthy int ret; 9244b4dc61SKeerthy unsigned int data; 9344b4dc61SKeerthy 940aced355SKeerthy ret = regmap_read(tps->regmap, reg, &data); 9544b4dc61SKeerthy if (ret) { 9644b4dc61SKeerthy dev_err(tps->dev, "Read from reg 0x%x failed\n", reg); 9744b4dc61SKeerthy return ret; 9844b4dc61SKeerthy } 9944b4dc61SKeerthy 10044b4dc61SKeerthy data &= ~mask; 10144b4dc61SKeerthy data |= val & mask; 10244b4dc61SKeerthy 10344b4dc61SKeerthy mutex_lock(&tps->tps_lock); 10444b4dc61SKeerthy ret = tps65218_reg_write(tps, reg, data, level); 10544b4dc61SKeerthy if (ret) 10644b4dc61SKeerthy dev_err(tps->dev, "Write for reg 0x%x failed\n", reg); 10744b4dc61SKeerthy mutex_unlock(&tps->tps_lock); 10844b4dc61SKeerthy 10944b4dc61SKeerthy return ret; 11044b4dc61SKeerthy } 11144b4dc61SKeerthy 11244b4dc61SKeerthy int tps65218_set_bits(struct tps65218 *tps, unsigned int reg, 11344b4dc61SKeerthy unsigned int mask, unsigned int val, unsigned int level) 11444b4dc61SKeerthy { 11544b4dc61SKeerthy return tps65218_update_bits(tps, reg, mask, val, level); 11644b4dc61SKeerthy } 11744b4dc61SKeerthy EXPORT_SYMBOL_GPL(tps65218_set_bits); 11844b4dc61SKeerthy 11944b4dc61SKeerthy int tps65218_clear_bits(struct tps65218 *tps, unsigned int reg, 12044b4dc61SKeerthy unsigned int mask, unsigned int level) 12144b4dc61SKeerthy { 12244b4dc61SKeerthy return tps65218_update_bits(tps, reg, mask, 0, level); 12344b4dc61SKeerthy } 12444b4dc61SKeerthy EXPORT_SYMBOL_GPL(tps65218_clear_bits); 12544b4dc61SKeerthy 126773328daSFelipe Balbi static const struct regmap_range tps65218_yes_ranges[] = { 127773328daSFelipe Balbi regmap_reg_range(TPS65218_REG_INT1, TPS65218_REG_INT2), 128773328daSFelipe Balbi regmap_reg_range(TPS65218_REG_STATUS, TPS65218_REG_STATUS), 129773328daSFelipe Balbi }; 130773328daSFelipe Balbi 131773328daSFelipe Balbi static const struct regmap_access_table tps65218_volatile_table = { 132773328daSFelipe Balbi .yes_ranges = tps65218_yes_ranges, 133773328daSFelipe Balbi .n_yes_ranges = ARRAY_SIZE(tps65218_yes_ranges), 134773328daSFelipe Balbi }; 135773328daSFelipe Balbi 13618bb399fSKrzysztof Kozlowski static const struct regmap_config tps65218_regmap_config = { 13744b4dc61SKeerthy .reg_bits = 8, 13844b4dc61SKeerthy .val_bits = 8, 13944b4dc61SKeerthy .cache_type = REGCACHE_RBTREE, 140773328daSFelipe Balbi .volatile_table = &tps65218_volatile_table, 14144b4dc61SKeerthy }; 14244b4dc61SKeerthy 14344b4dc61SKeerthy static const struct regmap_irq tps65218_irqs[] = { 14444b4dc61SKeerthy /* INT1 IRQs */ 14544b4dc61SKeerthy [TPS65218_PRGC_IRQ] = { 14644b4dc61SKeerthy .mask = TPS65218_INT1_PRGC, 14744b4dc61SKeerthy }, 14844b4dc61SKeerthy [TPS65218_CC_AQC_IRQ] = { 14944b4dc61SKeerthy .mask = TPS65218_INT1_CC_AQC, 15044b4dc61SKeerthy }, 15144b4dc61SKeerthy [TPS65218_HOT_IRQ] = { 15244b4dc61SKeerthy .mask = TPS65218_INT1_HOT, 15344b4dc61SKeerthy }, 15444b4dc61SKeerthy [TPS65218_PB_IRQ] = { 15544b4dc61SKeerthy .mask = TPS65218_INT1_PB, 15644b4dc61SKeerthy }, 15744b4dc61SKeerthy [TPS65218_AC_IRQ] = { 15844b4dc61SKeerthy .mask = TPS65218_INT1_AC, 15944b4dc61SKeerthy }, 16044b4dc61SKeerthy [TPS65218_VPRG_IRQ] = { 16144b4dc61SKeerthy .mask = TPS65218_INT1_VPRG, 16244b4dc61SKeerthy }, 16344b4dc61SKeerthy [TPS65218_INVALID1_IRQ] = { 16444b4dc61SKeerthy }, 16544b4dc61SKeerthy [TPS65218_INVALID2_IRQ] = { 16644b4dc61SKeerthy }, 16744b4dc61SKeerthy /* INT2 IRQs*/ 16844b4dc61SKeerthy [TPS65218_LS1_I_IRQ] = { 16944b4dc61SKeerthy .mask = TPS65218_INT2_LS1_I, 17044b4dc61SKeerthy .reg_offset = 1, 17144b4dc61SKeerthy }, 17244b4dc61SKeerthy [TPS65218_LS2_I_IRQ] = { 17344b4dc61SKeerthy .mask = TPS65218_INT2_LS2_I, 17444b4dc61SKeerthy .reg_offset = 1, 17544b4dc61SKeerthy }, 17644b4dc61SKeerthy [TPS65218_LS3_I_IRQ] = { 17744b4dc61SKeerthy .mask = TPS65218_INT2_LS3_I, 17844b4dc61SKeerthy .reg_offset = 1, 17944b4dc61SKeerthy }, 18044b4dc61SKeerthy [TPS65218_LS1_F_IRQ] = { 18144b4dc61SKeerthy .mask = TPS65218_INT2_LS1_F, 18244b4dc61SKeerthy .reg_offset = 1, 18344b4dc61SKeerthy }, 18444b4dc61SKeerthy [TPS65218_LS2_F_IRQ] = { 18544b4dc61SKeerthy .mask = TPS65218_INT2_LS2_F, 18644b4dc61SKeerthy .reg_offset = 1, 18744b4dc61SKeerthy }, 18844b4dc61SKeerthy [TPS65218_LS3_F_IRQ] = { 18944b4dc61SKeerthy .mask = TPS65218_INT2_LS3_F, 19044b4dc61SKeerthy .reg_offset = 1, 19144b4dc61SKeerthy }, 19244b4dc61SKeerthy [TPS65218_INVALID3_IRQ] = { 19344b4dc61SKeerthy }, 19444b4dc61SKeerthy [TPS65218_INVALID4_IRQ] = { 19544b4dc61SKeerthy }, 19644b4dc61SKeerthy }; 19744b4dc61SKeerthy 19844b4dc61SKeerthy static struct regmap_irq_chip tps65218_irq_chip = { 19944b4dc61SKeerthy .name = "tps65218", 20044b4dc61SKeerthy .irqs = tps65218_irqs, 20144b4dc61SKeerthy .num_irqs = ARRAY_SIZE(tps65218_irqs), 20244b4dc61SKeerthy 20344b4dc61SKeerthy .num_regs = 2, 20444b4dc61SKeerthy .mask_base = TPS65218_REG_INT_MASK1, 205f29ae369SFelipe Balbi .status_base = TPS65218_REG_INT1, 20644b4dc61SKeerthy }; 20744b4dc61SKeerthy 20844b4dc61SKeerthy static const struct of_device_id of_tps65218_match_table[] = { 20944b4dc61SKeerthy { .compatible = "ti,tps65218", }, 2108320513eSStephen Boyd {} 21144b4dc61SKeerthy }; 2124895e493SJavier Martinez Canillas MODULE_DEVICE_TABLE(of, of_tps65218_match_table); 21344b4dc61SKeerthy 21444b4dc61SKeerthy static int tps65218_probe(struct i2c_client *client, 21544b4dc61SKeerthy const struct i2c_device_id *ids) 21644b4dc61SKeerthy { 21744b4dc61SKeerthy struct tps65218 *tps; 21844b4dc61SKeerthy const struct of_device_id *match; 21944b4dc61SKeerthy int ret; 220f11fa179STero Kristo unsigned int chipid; 22144b4dc61SKeerthy 22244b4dc61SKeerthy match = of_match_device(of_tps65218_match_table, &client->dev); 22344b4dc61SKeerthy if (!match) { 22444b4dc61SKeerthy dev_err(&client->dev, 22544b4dc61SKeerthy "Failed to find matching dt id\n"); 22644b4dc61SKeerthy return -EINVAL; 22744b4dc61SKeerthy } 22844b4dc61SKeerthy 22944b4dc61SKeerthy tps = devm_kzalloc(&client->dev, sizeof(*tps), GFP_KERNEL); 23044b4dc61SKeerthy if (!tps) 23144b4dc61SKeerthy return -ENOMEM; 23244b4dc61SKeerthy 23344b4dc61SKeerthy i2c_set_clientdata(client, tps); 23444b4dc61SKeerthy tps->dev = &client->dev; 23544b4dc61SKeerthy tps->irq = client->irq; 23644b4dc61SKeerthy tps->regmap = devm_regmap_init_i2c(client, &tps65218_regmap_config); 23744b4dc61SKeerthy if (IS_ERR(tps->regmap)) { 23844b4dc61SKeerthy ret = PTR_ERR(tps->regmap); 23944b4dc61SKeerthy dev_err(tps->dev, "Failed to allocate register map: %d\n", 24044b4dc61SKeerthy ret); 24144b4dc61SKeerthy return ret; 24244b4dc61SKeerthy } 24344b4dc61SKeerthy 24444b4dc61SKeerthy mutex_init(&tps->tps_lock); 24544b4dc61SKeerthy 24644b4dc61SKeerthy ret = regmap_add_irq_chip(tps->regmap, tps->irq, 24744b4dc61SKeerthy IRQF_ONESHOT, 0, &tps65218_irq_chip, 24844b4dc61SKeerthy &tps->irq_data); 24944b4dc61SKeerthy if (ret < 0) 25044b4dc61SKeerthy return ret; 25144b4dc61SKeerthy 2520aced355SKeerthy ret = regmap_read(tps->regmap, TPS65218_REG_CHIPID, &chipid); 253f11fa179STero Kristo if (ret) { 254f11fa179STero Kristo dev_err(tps->dev, "Failed to read chipid: %d\n", ret); 255f11fa179STero Kristo return ret; 256f11fa179STero Kristo } 257f11fa179STero Kristo 258f11fa179STero Kristo tps->rev = chipid & TPS65218_CHIPID_REV_MASK; 259f11fa179STero Kristo 2604531156dSKeerthy ret = mfd_add_devices(tps->dev, PLATFORM_DEVID_AUTO, tps65218_cells, 2614531156dSKeerthy ARRAY_SIZE(tps65218_cells), NULL, 0, 2624531156dSKeerthy regmap_irq_get_domain(tps->irq_data)); 2634531156dSKeerthy 26444b4dc61SKeerthy if (ret < 0) 26544b4dc61SKeerthy goto err_irq; 26644b4dc61SKeerthy 26744b4dc61SKeerthy return 0; 26844b4dc61SKeerthy 26944b4dc61SKeerthy err_irq: 27044b4dc61SKeerthy regmap_del_irq_chip(tps->irq, tps->irq_data); 27144b4dc61SKeerthy 27244b4dc61SKeerthy return ret; 27344b4dc61SKeerthy } 27444b4dc61SKeerthy 27544b4dc61SKeerthy static int tps65218_remove(struct i2c_client *client) 27644b4dc61SKeerthy { 27744b4dc61SKeerthy struct tps65218 *tps = i2c_get_clientdata(client); 27844b4dc61SKeerthy 27944b4dc61SKeerthy regmap_del_irq_chip(tps->irq, tps->irq_data); 28044b4dc61SKeerthy 28144b4dc61SKeerthy return 0; 28244b4dc61SKeerthy } 28344b4dc61SKeerthy 28444b4dc61SKeerthy static const struct i2c_device_id tps65218_id_table[] = { 28544b4dc61SKeerthy { "tps65218", TPS65218 }, 28644b4dc61SKeerthy { }, 28744b4dc61SKeerthy }; 28844b4dc61SKeerthy MODULE_DEVICE_TABLE(i2c, tps65218_id_table); 28944b4dc61SKeerthy 29044b4dc61SKeerthy static struct i2c_driver tps65218_driver = { 29144b4dc61SKeerthy .driver = { 29244b4dc61SKeerthy .name = "tps65218", 29344b4dc61SKeerthy .of_match_table = of_tps65218_match_table, 29444b4dc61SKeerthy }, 29544b4dc61SKeerthy .probe = tps65218_probe, 29644b4dc61SKeerthy .remove = tps65218_remove, 29744b4dc61SKeerthy .id_table = tps65218_id_table, 29844b4dc61SKeerthy }; 29944b4dc61SKeerthy 30044b4dc61SKeerthy module_i2c_driver(tps65218_driver); 30144b4dc61SKeerthy 30244b4dc61SKeerthy MODULE_AUTHOR("J Keerthy <j-keerthy@ti.com>"); 30344b4dc61SKeerthy MODULE_DESCRIPTION("TPS65218 chip family multi-function driver"); 30444b4dc61SKeerthy MODULE_LICENSE("GPL v2"); 305