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/of_device.h>
19 #include <linux/irq.h>
20 #include <linux/interrupt.h>
21 #include <linux/mutex.h>
23 #include <linux/mfd/core.h>
24 #include <linux/mfd/tps65218.h>
26 #define TPS65218_PASSWORD_REGS_UNLOCK 0x7D
28 static const struct mfd_cell tps65218_cells[] = {
30 .name = "tps65218-pwrbutton",
31 .of_compatible = "ti,tps65218-pwrbutton",
34 .name = "tps65218-gpio",
35 .of_compatible = "ti,tps65218-gpio",
37 { .name = "tps65218-regulator", },
41 * tps65218_reg_write: Write a single tps65218 register.
43 * @tps: Device to write to.
44 * @reg: Register to write to.
45 * @val: Value to write.
46 * @level: Password protected level
48 int tps65218_reg_write(struct tps65218 *tps, unsigned int reg,
49 unsigned int val, unsigned int level)
52 unsigned int xor_reg_val;
55 case TPS65218_PROTECT_NONE:
56 return regmap_write(tps->regmap, reg, val);
57 case TPS65218_PROTECT_L1:
58 xor_reg_val = reg ^ TPS65218_PASSWORD_REGS_UNLOCK;
59 ret = regmap_write(tps->regmap, TPS65218_REG_PASSWORD,
64 return regmap_write(tps->regmap, reg, val);
69 EXPORT_SYMBOL_GPL(tps65218_reg_write);
72 * tps65218_update_bits: Modify bits w.r.t mask, val and level.
74 * @tps: Device to write to.
75 * @reg: Register to read-write to.
77 * @val: Value to write.
78 * @level: Password protected level
80 static int tps65218_update_bits(struct tps65218 *tps, unsigned int reg,
81 unsigned int mask, unsigned int val, unsigned int level)
86 ret = regmap_read(tps->regmap, reg, &data);
88 dev_err(tps->dev, "Read from reg 0x%x failed\n", reg);
95 mutex_lock(&tps->tps_lock);
96 ret = tps65218_reg_write(tps, reg, data, level);
98 dev_err(tps->dev, "Write for reg 0x%x failed\n", reg);
99 mutex_unlock(&tps->tps_lock);
104 int tps65218_set_bits(struct tps65218 *tps, unsigned int reg,
105 unsigned int mask, unsigned int val, unsigned int level)
107 return tps65218_update_bits(tps, reg, mask, val, level);
109 EXPORT_SYMBOL_GPL(tps65218_set_bits);
111 int tps65218_clear_bits(struct tps65218 *tps, unsigned int reg,
112 unsigned int mask, unsigned int level)
114 return tps65218_update_bits(tps, reg, mask, 0, level);
116 EXPORT_SYMBOL_GPL(tps65218_clear_bits);
118 static const struct regmap_range tps65218_yes_ranges[] = {
119 regmap_reg_range(TPS65218_REG_INT1, TPS65218_REG_INT2),
120 regmap_reg_range(TPS65218_REG_STATUS, TPS65218_REG_STATUS),
123 static const struct regmap_access_table tps65218_volatile_table = {
124 .yes_ranges = tps65218_yes_ranges,
125 .n_yes_ranges = ARRAY_SIZE(tps65218_yes_ranges),
128 static const struct regmap_config tps65218_regmap_config = {
131 .cache_type = REGCACHE_RBTREE,
132 .volatile_table = &tps65218_volatile_table,
135 static const struct regmap_irq tps65218_irqs[] = {
137 [TPS65218_PRGC_IRQ] = {
138 .mask = TPS65218_INT1_PRGC,
140 [TPS65218_CC_AQC_IRQ] = {
141 .mask = TPS65218_INT1_CC_AQC,
143 [TPS65218_HOT_IRQ] = {
144 .mask = TPS65218_INT1_HOT,
146 [TPS65218_PB_IRQ] = {
147 .mask = TPS65218_INT1_PB,
149 [TPS65218_AC_IRQ] = {
150 .mask = TPS65218_INT1_AC,
152 [TPS65218_VPRG_IRQ] = {
153 .mask = TPS65218_INT1_VPRG,
155 [TPS65218_INVALID1_IRQ] = {
157 [TPS65218_INVALID2_IRQ] = {
160 [TPS65218_LS1_I_IRQ] = {
161 .mask = TPS65218_INT2_LS1_I,
164 [TPS65218_LS2_I_IRQ] = {
165 .mask = TPS65218_INT2_LS2_I,
168 [TPS65218_LS3_I_IRQ] = {
169 .mask = TPS65218_INT2_LS3_I,
172 [TPS65218_LS1_F_IRQ] = {
173 .mask = TPS65218_INT2_LS1_F,
176 [TPS65218_LS2_F_IRQ] = {
177 .mask = TPS65218_INT2_LS2_F,
180 [TPS65218_LS3_F_IRQ] = {
181 .mask = TPS65218_INT2_LS3_F,
184 [TPS65218_INVALID3_IRQ] = {
186 [TPS65218_INVALID4_IRQ] = {
190 static struct regmap_irq_chip tps65218_irq_chip = {
192 .irqs = tps65218_irqs,
193 .num_irqs = ARRAY_SIZE(tps65218_irqs),
196 .mask_base = TPS65218_REG_INT_MASK1,
197 .status_base = TPS65218_REG_INT1,
200 static const struct of_device_id of_tps65218_match_table[] = {
201 { .compatible = "ti,tps65218", },
204 MODULE_DEVICE_TABLE(of, of_tps65218_match_table);
206 static int tps65218_voltage_set_strict(struct tps65218 *tps)
210 if (of_property_read_u32(tps->dev->of_node,
211 "ti,strict-supply-voltage-supervision",
215 if (strict != 0 && strict != 1) {
217 "Invalid ti,strict-supply-voltage-supervision value\n");
221 tps65218_update_bits(tps, TPS65218_REG_CONFIG1,
222 TPS65218_CONFIG1_STRICT,
223 strict ? TPS65218_CONFIG1_STRICT : 0,
224 TPS65218_PROTECT_L1);
228 static int tps65218_voltage_set_uv_hyst(struct tps65218 *tps)
232 if (of_property_read_u32(tps->dev->of_node,
233 "ti,under-voltage-hyst-microvolt", &hyst))
236 if (hyst != 400000 && hyst != 200000) {
238 "Invalid ti,under-voltage-hyst-microvolt value\n");
242 tps65218_update_bits(tps, TPS65218_REG_CONFIG2,
243 TPS65218_CONFIG2_UVLOHYS,
244 hyst == 400000 ? TPS65218_CONFIG2_UVLOHYS : 0,
245 TPS65218_PROTECT_L1);
249 static int tps65218_voltage_set_uvlo(struct tps65218 *tps)
254 if (of_property_read_u32(tps->dev->of_node,
255 "ti,under-voltage-limit-microvolt", &uvlo))
260 uvloval = TPS65218_CONFIG1_UVLO_2750000;
263 uvloval = TPS65218_CONFIG1_UVLO_2950000;
266 uvloval = TPS65218_CONFIG1_UVLO_3250000;
269 uvloval = TPS65218_CONFIG1_UVLO_3350000;
273 "Invalid ti,under-voltage-limit-microvolt value\n");
277 tps65218_update_bits(tps, TPS65218_REG_CONFIG1,
278 TPS65218_CONFIG1_UVLO_MASK, uvloval,
279 TPS65218_PROTECT_L1);
283 static int tps65218_probe(struct i2c_client *client,
284 const struct i2c_device_id *ids)
286 struct tps65218 *tps;
290 tps = devm_kzalloc(&client->dev, sizeof(*tps), GFP_KERNEL);
294 i2c_set_clientdata(client, tps);
295 tps->dev = &client->dev;
296 tps->irq = client->irq;
297 tps->regmap = devm_regmap_init_i2c(client, &tps65218_regmap_config);
298 if (IS_ERR(tps->regmap)) {
299 ret = PTR_ERR(tps->regmap);
300 dev_err(tps->dev, "Failed to allocate register map: %d\n",
305 mutex_init(&tps->tps_lock);
307 ret = devm_regmap_add_irq_chip(&client->dev, tps->regmap, tps->irq,
308 IRQF_ONESHOT, 0, &tps65218_irq_chip,
313 ret = regmap_read(tps->regmap, TPS65218_REG_CHIPID, &chipid);
315 dev_err(tps->dev, "Failed to read chipid: %d\n", ret);
319 tps->rev = chipid & TPS65218_CHIPID_REV_MASK;
321 ret = tps65218_voltage_set_strict(tps);
325 ret = tps65218_voltage_set_uvlo(tps);
329 ret = tps65218_voltage_set_uv_hyst(tps);
333 ret = mfd_add_devices(tps->dev, PLATFORM_DEVID_AUTO, tps65218_cells,
334 ARRAY_SIZE(tps65218_cells), NULL, 0,
335 regmap_irq_get_domain(tps->irq_data));
340 static const struct i2c_device_id tps65218_id_table[] = {
341 { "tps65218", TPS65218 },
344 MODULE_DEVICE_TABLE(i2c, tps65218_id_table);
346 static struct i2c_driver tps65218_driver = {
349 .of_match_table = of_tps65218_match_table,
351 .probe = tps65218_probe,
352 .id_table = tps65218_id_table,
355 module_i2c_driver(tps65218_driver);
357 MODULE_AUTHOR("J Keerthy <j-keerthy@ti.com>");
358 MODULE_DESCRIPTION("TPS65218 chip family multi-function driver");
359 MODULE_LICENSE("GPL v2");