1 // SPDX-License-Identifier: GPL-2.0-only
3 * Driver for TPS65218 Integrated power management chipsets
5 * Copyright (C) 2014 Texas Instruments Incorporated - https://www.ti.com/
8 #include <linux/kernel.h>
9 #include <linux/device.h>
10 #include <linux/module.h>
11 #include <linux/platform_device.h>
12 #include <linux/init.h>
13 #include <linux/i2c.h>
14 #include <linux/slab.h>
15 #include <linux/regmap.h>
16 #include <linux/err.h>
18 #include <linux/irq.h>
19 #include <linux/interrupt.h>
20 #include <linux/mutex.h>
22 #include <linux/mfd/core.h>
23 #include <linux/mfd/tps65218.h>
25 #define TPS65218_PASSWORD_REGS_UNLOCK 0x7D
27 static const struct mfd_cell tps65218_cells[] = {
29 .name = "tps65218-pwrbutton",
30 .of_compatible = "ti,tps65218-pwrbutton",
33 .name = "tps65218-gpio",
34 .of_compatible = "ti,tps65218-gpio",
36 { .name = "tps65218-regulator", },
40 * tps65218_reg_write: Write a single tps65218 register.
42 * @tps: Device to write to.
43 * @reg: Register to write to.
44 * @val: Value to write.
45 * @level: Password protected level
47 int tps65218_reg_write(struct tps65218 *tps, unsigned int reg,
48 unsigned int val, unsigned int level)
51 unsigned int xor_reg_val;
54 case TPS65218_PROTECT_NONE:
55 return regmap_write(tps->regmap, reg, val);
56 case TPS65218_PROTECT_L1:
57 xor_reg_val = reg ^ TPS65218_PASSWORD_REGS_UNLOCK;
58 ret = regmap_write(tps->regmap, TPS65218_REG_PASSWORD,
63 return regmap_write(tps->regmap, reg, val);
68 EXPORT_SYMBOL_GPL(tps65218_reg_write);
71 * tps65218_update_bits: Modify bits w.r.t mask, val and level.
73 * @tps: Device to write to.
74 * @reg: Register to read-write to.
76 * @val: Value to write.
77 * @level: Password protected level
79 static int tps65218_update_bits(struct tps65218 *tps, unsigned int reg,
80 unsigned int mask, unsigned int val, unsigned int level)
85 ret = regmap_read(tps->regmap, reg, &data);
87 dev_err(tps->dev, "Read from reg 0x%x failed\n", reg);
94 mutex_lock(&tps->tps_lock);
95 ret = tps65218_reg_write(tps, reg, data, level);
97 dev_err(tps->dev, "Write for reg 0x%x failed\n", reg);
98 mutex_unlock(&tps->tps_lock);
103 int tps65218_set_bits(struct tps65218 *tps, unsigned int reg,
104 unsigned int mask, unsigned int val, unsigned int level)
106 return tps65218_update_bits(tps, reg, mask, val, level);
108 EXPORT_SYMBOL_GPL(tps65218_set_bits);
110 int tps65218_clear_bits(struct tps65218 *tps, unsigned int reg,
111 unsigned int mask, unsigned int level)
113 return tps65218_update_bits(tps, reg, mask, 0, level);
115 EXPORT_SYMBOL_GPL(tps65218_clear_bits);
117 static const struct regmap_range tps65218_yes_ranges[] = {
118 regmap_reg_range(TPS65218_REG_INT1, TPS65218_REG_INT2),
119 regmap_reg_range(TPS65218_REG_STATUS, TPS65218_REG_STATUS),
122 static const struct regmap_access_table tps65218_volatile_table = {
123 .yes_ranges = tps65218_yes_ranges,
124 .n_yes_ranges = ARRAY_SIZE(tps65218_yes_ranges),
127 static const struct regmap_config tps65218_regmap_config = {
130 .cache_type = REGCACHE_RBTREE,
131 .volatile_table = &tps65218_volatile_table,
134 static const struct regmap_irq tps65218_irqs[] = {
136 [TPS65218_PRGC_IRQ] = {
137 .mask = TPS65218_INT1_PRGC,
139 [TPS65218_CC_AQC_IRQ] = {
140 .mask = TPS65218_INT1_CC_AQC,
142 [TPS65218_HOT_IRQ] = {
143 .mask = TPS65218_INT1_HOT,
145 [TPS65218_PB_IRQ] = {
146 .mask = TPS65218_INT1_PB,
148 [TPS65218_AC_IRQ] = {
149 .mask = TPS65218_INT1_AC,
151 [TPS65218_VPRG_IRQ] = {
152 .mask = TPS65218_INT1_VPRG,
154 [TPS65218_INVALID1_IRQ] = {
156 [TPS65218_INVALID2_IRQ] = {
159 [TPS65218_LS1_I_IRQ] = {
160 .mask = TPS65218_INT2_LS1_I,
163 [TPS65218_LS2_I_IRQ] = {
164 .mask = TPS65218_INT2_LS2_I,
167 [TPS65218_LS3_I_IRQ] = {
168 .mask = TPS65218_INT2_LS3_I,
171 [TPS65218_LS1_F_IRQ] = {
172 .mask = TPS65218_INT2_LS1_F,
175 [TPS65218_LS2_F_IRQ] = {
176 .mask = TPS65218_INT2_LS2_F,
179 [TPS65218_LS3_F_IRQ] = {
180 .mask = TPS65218_INT2_LS3_F,
183 [TPS65218_INVALID3_IRQ] = {
185 [TPS65218_INVALID4_IRQ] = {
189 static struct regmap_irq_chip tps65218_irq_chip = {
191 .irqs = tps65218_irqs,
192 .num_irqs = ARRAY_SIZE(tps65218_irqs),
195 .mask_base = TPS65218_REG_INT_MASK1,
196 .status_base = TPS65218_REG_INT1,
199 static const struct of_device_id of_tps65218_match_table[] = {
200 { .compatible = "ti,tps65218", },
203 MODULE_DEVICE_TABLE(of, of_tps65218_match_table);
205 static int tps65218_voltage_set_strict(struct tps65218 *tps)
209 if (of_property_read_u32(tps->dev->of_node,
210 "ti,strict-supply-voltage-supervision",
214 if (strict != 0 && strict != 1) {
216 "Invalid ti,strict-supply-voltage-supervision value\n");
220 tps65218_update_bits(tps, TPS65218_REG_CONFIG1,
221 TPS65218_CONFIG1_STRICT,
222 strict ? TPS65218_CONFIG1_STRICT : 0,
223 TPS65218_PROTECT_L1);
227 static int tps65218_voltage_set_uv_hyst(struct tps65218 *tps)
231 if (of_property_read_u32(tps->dev->of_node,
232 "ti,under-voltage-hyst-microvolt", &hyst))
235 if (hyst != 400000 && hyst != 200000) {
237 "Invalid ti,under-voltage-hyst-microvolt value\n");
241 tps65218_update_bits(tps, TPS65218_REG_CONFIG2,
242 TPS65218_CONFIG2_UVLOHYS,
243 hyst == 400000 ? TPS65218_CONFIG2_UVLOHYS : 0,
244 TPS65218_PROTECT_L1);
248 static int tps65218_voltage_set_uvlo(struct tps65218 *tps)
253 if (of_property_read_u32(tps->dev->of_node,
254 "ti,under-voltage-limit-microvolt", &uvlo))
259 uvloval = TPS65218_CONFIG1_UVLO_2750000;
262 uvloval = TPS65218_CONFIG1_UVLO_2950000;
265 uvloval = TPS65218_CONFIG1_UVLO_3250000;
268 uvloval = TPS65218_CONFIG1_UVLO_3350000;
272 "Invalid ti,under-voltage-limit-microvolt value\n");
276 tps65218_update_bits(tps, TPS65218_REG_CONFIG1,
277 TPS65218_CONFIG1_UVLO_MASK, uvloval,
278 TPS65218_PROTECT_L1);
282 static int tps65218_probe(struct i2c_client *client)
284 struct tps65218 *tps;
288 tps = devm_kzalloc(&client->dev, sizeof(*tps), GFP_KERNEL);
292 i2c_set_clientdata(client, tps);
293 tps->dev = &client->dev;
294 tps->irq = client->irq;
295 tps->regmap = devm_regmap_init_i2c(client, &tps65218_regmap_config);
296 if (IS_ERR(tps->regmap)) {
297 ret = PTR_ERR(tps->regmap);
298 dev_err(tps->dev, "Failed to allocate register map: %d\n",
303 mutex_init(&tps->tps_lock);
305 ret = devm_regmap_add_irq_chip(&client->dev, tps->regmap, tps->irq,
306 IRQF_ONESHOT, 0, &tps65218_irq_chip,
311 ret = regmap_read(tps->regmap, TPS65218_REG_CHIPID, &chipid);
313 dev_err(tps->dev, "Failed to read chipid: %d\n", ret);
317 tps->rev = chipid & TPS65218_CHIPID_REV_MASK;
319 ret = tps65218_voltage_set_strict(tps);
323 ret = tps65218_voltage_set_uvlo(tps);
327 ret = tps65218_voltage_set_uv_hyst(tps);
331 ret = mfd_add_devices(tps->dev, PLATFORM_DEVID_AUTO, tps65218_cells,
332 ARRAY_SIZE(tps65218_cells), NULL, 0,
333 regmap_irq_get_domain(tps->irq_data));
338 static const struct i2c_device_id tps65218_id_table[] = {
339 { "tps65218", TPS65218 },
342 MODULE_DEVICE_TABLE(i2c, tps65218_id_table);
344 static struct i2c_driver tps65218_driver = {
347 .of_match_table = of_tps65218_match_table,
349 .probe = tps65218_probe,
350 .id_table = tps65218_id_table,
353 module_i2c_driver(tps65218_driver);
355 MODULE_AUTHOR("J Keerthy <j-keerthy@ti.com>");
356 MODULE_DESCRIPTION("TPS65218 chip family multi-function driver");
357 MODULE_LICENSE("GPL v2");