1abd46274SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
244b4dc61SKeerthy /*
344b4dc61SKeerthy * Driver for TPS65218 Integrated power management chipsets
444b4dc61SKeerthy *
54f4ed454SAlexander A. Klimov * Copyright (C) 2014 Texas Instruments Incorporated - https://www.ti.com/
644b4dc61SKeerthy */
744b4dc61SKeerthy
844b4dc61SKeerthy #include <linux/kernel.h>
944b4dc61SKeerthy #include <linux/device.h>
1044b4dc61SKeerthy #include <linux/module.h>
1144b4dc61SKeerthy #include <linux/platform_device.h>
1244b4dc61SKeerthy #include <linux/init.h>
1344b4dc61SKeerthy #include <linux/i2c.h>
1444b4dc61SKeerthy #include <linux/slab.h>
1544b4dc61SKeerthy #include <linux/regmap.h>
1644b4dc61SKeerthy #include <linux/err.h>
1744b4dc61SKeerthy #include <linux/of.h>
1844b4dc61SKeerthy #include <linux/irq.h>
1944b4dc61SKeerthy #include <linux/interrupt.h>
2044b4dc61SKeerthy #include <linux/mutex.h>
2144b4dc61SKeerthy
2244b4dc61SKeerthy #include <linux/mfd/core.h>
2344b4dc61SKeerthy #include <linux/mfd/tps65218.h>
2444b4dc61SKeerthy
2544b4dc61SKeerthy #define TPS65218_PASSWORD_REGS_UNLOCK 0x7D
2644b4dc61SKeerthy
274531156dSKeerthy static const struct mfd_cell tps65218_cells[] = {
284531156dSKeerthy {
294531156dSKeerthy .name = "tps65218-pwrbutton",
304531156dSKeerthy .of_compatible = "ti,tps65218-pwrbutton",
314531156dSKeerthy },
324531156dSKeerthy {
334531156dSKeerthy .name = "tps65218-gpio",
344531156dSKeerthy .of_compatible = "ti,tps65218-gpio",
354531156dSKeerthy },
364531156dSKeerthy { .name = "tps65218-regulator", },
374531156dSKeerthy };
384531156dSKeerthy
3944b4dc61SKeerthy /**
4044b4dc61SKeerthy * tps65218_reg_write: Write a single tps65218 register.
4144b4dc61SKeerthy *
429c3739eeSLee Jones * @tps: Device to write to.
4344b4dc61SKeerthy * @reg: Register to write to.
4444b4dc61SKeerthy * @val: Value to write.
4544b4dc61SKeerthy * @level: Password protected level
4644b4dc61SKeerthy */
tps65218_reg_write(struct tps65218 * tps,unsigned int reg,unsigned int val,unsigned int level)4744b4dc61SKeerthy int tps65218_reg_write(struct tps65218 *tps, unsigned int reg,
4844b4dc61SKeerthy unsigned int val, unsigned int level)
4944b4dc61SKeerthy {
5044b4dc61SKeerthy int ret;
5144b4dc61SKeerthy unsigned int xor_reg_val;
5244b4dc61SKeerthy
5344b4dc61SKeerthy switch (level) {
5444b4dc61SKeerthy case TPS65218_PROTECT_NONE:
5544b4dc61SKeerthy return regmap_write(tps->regmap, reg, val);
5644b4dc61SKeerthy case TPS65218_PROTECT_L1:
5744b4dc61SKeerthy xor_reg_val = reg ^ TPS65218_PASSWORD_REGS_UNLOCK;
5844b4dc61SKeerthy ret = regmap_write(tps->regmap, TPS65218_REG_PASSWORD,
5944b4dc61SKeerthy xor_reg_val);
6044b4dc61SKeerthy if (ret < 0)
6144b4dc61SKeerthy return ret;
6244b4dc61SKeerthy
6344b4dc61SKeerthy return regmap_write(tps->regmap, reg, val);
6444b4dc61SKeerthy default:
6544b4dc61SKeerthy return -EINVAL;
6644b4dc61SKeerthy }
6744b4dc61SKeerthy }
6844b4dc61SKeerthy EXPORT_SYMBOL_GPL(tps65218_reg_write);
6944b4dc61SKeerthy
7044b4dc61SKeerthy /**
7144b4dc61SKeerthy * tps65218_update_bits: Modify bits w.r.t mask, val and level.
7244b4dc61SKeerthy *
739c3739eeSLee Jones * @tps: Device to write to.
7444b4dc61SKeerthy * @reg: Register to read-write to.
7544b4dc61SKeerthy * @mask: Mask.
7644b4dc61SKeerthy * @val: Value to write.
7744b4dc61SKeerthy * @level: Password protected level
7844b4dc61SKeerthy */
tps65218_update_bits(struct tps65218 * tps,unsigned int reg,unsigned int mask,unsigned int val,unsigned int level)7944b4dc61SKeerthy static int tps65218_update_bits(struct tps65218 *tps, unsigned int reg,
8044b4dc61SKeerthy unsigned int mask, unsigned int val, unsigned int level)
8144b4dc61SKeerthy {
8244b4dc61SKeerthy int ret;
8344b4dc61SKeerthy unsigned int data;
8444b4dc61SKeerthy
850aced355SKeerthy ret = regmap_read(tps->regmap, reg, &data);
8644b4dc61SKeerthy if (ret) {
8744b4dc61SKeerthy dev_err(tps->dev, "Read from reg 0x%x failed\n", reg);
8844b4dc61SKeerthy return ret;
8944b4dc61SKeerthy }
9044b4dc61SKeerthy
9144b4dc61SKeerthy data &= ~mask;
9244b4dc61SKeerthy data |= val & mask;
9344b4dc61SKeerthy
9444b4dc61SKeerthy mutex_lock(&tps->tps_lock);
9544b4dc61SKeerthy ret = tps65218_reg_write(tps, reg, data, level);
9644b4dc61SKeerthy if (ret)
9744b4dc61SKeerthy dev_err(tps->dev, "Write for reg 0x%x failed\n", reg);
9844b4dc61SKeerthy mutex_unlock(&tps->tps_lock);
9944b4dc61SKeerthy
10044b4dc61SKeerthy return ret;
10144b4dc61SKeerthy }
10244b4dc61SKeerthy
tps65218_set_bits(struct tps65218 * tps,unsigned int reg,unsigned int mask,unsigned int val,unsigned int level)10344b4dc61SKeerthy int tps65218_set_bits(struct tps65218 *tps, unsigned int reg,
10444b4dc61SKeerthy unsigned int mask, unsigned int val, unsigned int level)
10544b4dc61SKeerthy {
10644b4dc61SKeerthy return tps65218_update_bits(tps, reg, mask, val, level);
10744b4dc61SKeerthy }
10844b4dc61SKeerthy EXPORT_SYMBOL_GPL(tps65218_set_bits);
10944b4dc61SKeerthy
tps65218_clear_bits(struct tps65218 * tps,unsigned int reg,unsigned int mask,unsigned int level)11044b4dc61SKeerthy int tps65218_clear_bits(struct tps65218 *tps, unsigned int reg,
11144b4dc61SKeerthy unsigned int mask, unsigned int level)
11244b4dc61SKeerthy {
11344b4dc61SKeerthy return tps65218_update_bits(tps, reg, mask, 0, level);
11444b4dc61SKeerthy }
11544b4dc61SKeerthy EXPORT_SYMBOL_GPL(tps65218_clear_bits);
11644b4dc61SKeerthy
117773328daSFelipe Balbi static const struct regmap_range tps65218_yes_ranges[] = {
118773328daSFelipe Balbi regmap_reg_range(TPS65218_REG_INT1, TPS65218_REG_INT2),
119773328daSFelipe Balbi regmap_reg_range(TPS65218_REG_STATUS, TPS65218_REG_STATUS),
120773328daSFelipe Balbi };
121773328daSFelipe Balbi
122773328daSFelipe Balbi static const struct regmap_access_table tps65218_volatile_table = {
123773328daSFelipe Balbi .yes_ranges = tps65218_yes_ranges,
124773328daSFelipe Balbi .n_yes_ranges = ARRAY_SIZE(tps65218_yes_ranges),
125773328daSFelipe Balbi };
126773328daSFelipe Balbi
12718bb399fSKrzysztof Kozlowski static const struct regmap_config tps65218_regmap_config = {
12844b4dc61SKeerthy .reg_bits = 8,
12944b4dc61SKeerthy .val_bits = 8,
13044b4dc61SKeerthy .cache_type = REGCACHE_RBTREE,
131773328daSFelipe Balbi .volatile_table = &tps65218_volatile_table,
13244b4dc61SKeerthy };
13344b4dc61SKeerthy
13444b4dc61SKeerthy static const struct regmap_irq tps65218_irqs[] = {
13544b4dc61SKeerthy /* INT1 IRQs */
13644b4dc61SKeerthy [TPS65218_PRGC_IRQ] = {
13744b4dc61SKeerthy .mask = TPS65218_INT1_PRGC,
13844b4dc61SKeerthy },
13944b4dc61SKeerthy [TPS65218_CC_AQC_IRQ] = {
14044b4dc61SKeerthy .mask = TPS65218_INT1_CC_AQC,
14144b4dc61SKeerthy },
14244b4dc61SKeerthy [TPS65218_HOT_IRQ] = {
14344b4dc61SKeerthy .mask = TPS65218_INT1_HOT,
14444b4dc61SKeerthy },
14544b4dc61SKeerthy [TPS65218_PB_IRQ] = {
14644b4dc61SKeerthy .mask = TPS65218_INT1_PB,
14744b4dc61SKeerthy },
14844b4dc61SKeerthy [TPS65218_AC_IRQ] = {
14944b4dc61SKeerthy .mask = TPS65218_INT1_AC,
15044b4dc61SKeerthy },
15144b4dc61SKeerthy [TPS65218_VPRG_IRQ] = {
15244b4dc61SKeerthy .mask = TPS65218_INT1_VPRG,
15344b4dc61SKeerthy },
15444b4dc61SKeerthy [TPS65218_INVALID1_IRQ] = {
15544b4dc61SKeerthy },
15644b4dc61SKeerthy [TPS65218_INVALID2_IRQ] = {
15744b4dc61SKeerthy },
15844b4dc61SKeerthy /* INT2 IRQs*/
15944b4dc61SKeerthy [TPS65218_LS1_I_IRQ] = {
16044b4dc61SKeerthy .mask = TPS65218_INT2_LS1_I,
16144b4dc61SKeerthy .reg_offset = 1,
16244b4dc61SKeerthy },
16344b4dc61SKeerthy [TPS65218_LS2_I_IRQ] = {
16444b4dc61SKeerthy .mask = TPS65218_INT2_LS2_I,
16544b4dc61SKeerthy .reg_offset = 1,
16644b4dc61SKeerthy },
16744b4dc61SKeerthy [TPS65218_LS3_I_IRQ] = {
16844b4dc61SKeerthy .mask = TPS65218_INT2_LS3_I,
16944b4dc61SKeerthy .reg_offset = 1,
17044b4dc61SKeerthy },
17144b4dc61SKeerthy [TPS65218_LS1_F_IRQ] = {
17244b4dc61SKeerthy .mask = TPS65218_INT2_LS1_F,
17344b4dc61SKeerthy .reg_offset = 1,
17444b4dc61SKeerthy },
17544b4dc61SKeerthy [TPS65218_LS2_F_IRQ] = {
17644b4dc61SKeerthy .mask = TPS65218_INT2_LS2_F,
17744b4dc61SKeerthy .reg_offset = 1,
17844b4dc61SKeerthy },
17944b4dc61SKeerthy [TPS65218_LS3_F_IRQ] = {
18044b4dc61SKeerthy .mask = TPS65218_INT2_LS3_F,
18144b4dc61SKeerthy .reg_offset = 1,
18244b4dc61SKeerthy },
18344b4dc61SKeerthy [TPS65218_INVALID3_IRQ] = {
18444b4dc61SKeerthy },
18544b4dc61SKeerthy [TPS65218_INVALID4_IRQ] = {
18644b4dc61SKeerthy },
18744b4dc61SKeerthy };
18844b4dc61SKeerthy
18944b4dc61SKeerthy static struct regmap_irq_chip tps65218_irq_chip = {
19044b4dc61SKeerthy .name = "tps65218",
19144b4dc61SKeerthy .irqs = tps65218_irqs,
19244b4dc61SKeerthy .num_irqs = ARRAY_SIZE(tps65218_irqs),
19344b4dc61SKeerthy
19444b4dc61SKeerthy .num_regs = 2,
19544b4dc61SKeerthy .mask_base = TPS65218_REG_INT_MASK1,
196f29ae369SFelipe Balbi .status_base = TPS65218_REG_INT1,
19744b4dc61SKeerthy };
19844b4dc61SKeerthy
19944b4dc61SKeerthy static const struct of_device_id of_tps65218_match_table[] = {
20044b4dc61SKeerthy { .compatible = "ti,tps65218", },
2018320513eSStephen Boyd {}
20244b4dc61SKeerthy };
2034895e493SJavier Martinez Canillas MODULE_DEVICE_TABLE(of, of_tps65218_match_table);
20444b4dc61SKeerthy
tps65218_voltage_set_strict(struct tps65218 * tps)205d57f7287SChristian Hohnstaedt static int tps65218_voltage_set_strict(struct tps65218 *tps)
206d57f7287SChristian Hohnstaedt {
207d57f7287SChristian Hohnstaedt u32 strict;
208d57f7287SChristian Hohnstaedt
209d57f7287SChristian Hohnstaedt if (of_property_read_u32(tps->dev->of_node,
210d57f7287SChristian Hohnstaedt "ti,strict-supply-voltage-supervision",
211d57f7287SChristian Hohnstaedt &strict))
212d57f7287SChristian Hohnstaedt return 0;
213d57f7287SChristian Hohnstaedt
214d57f7287SChristian Hohnstaedt if (strict != 0 && strict != 1) {
215d57f7287SChristian Hohnstaedt dev_err(tps->dev,
216d57f7287SChristian Hohnstaedt "Invalid ti,strict-supply-voltage-supervision value\n");
217d57f7287SChristian Hohnstaedt return -EINVAL;
218d57f7287SChristian Hohnstaedt }
219d57f7287SChristian Hohnstaedt
220d57f7287SChristian Hohnstaedt tps65218_update_bits(tps, TPS65218_REG_CONFIG1,
221d57f7287SChristian Hohnstaedt TPS65218_CONFIG1_STRICT,
222d57f7287SChristian Hohnstaedt strict ? TPS65218_CONFIG1_STRICT : 0,
223d57f7287SChristian Hohnstaedt TPS65218_PROTECT_L1);
224d57f7287SChristian Hohnstaedt return 0;
225d57f7287SChristian Hohnstaedt }
226d57f7287SChristian Hohnstaedt
tps65218_voltage_set_uv_hyst(struct tps65218 * tps)227d57f7287SChristian Hohnstaedt static int tps65218_voltage_set_uv_hyst(struct tps65218 *tps)
228d57f7287SChristian Hohnstaedt {
229d57f7287SChristian Hohnstaedt u32 hyst;
230d57f7287SChristian Hohnstaedt
231d57f7287SChristian Hohnstaedt if (of_property_read_u32(tps->dev->of_node,
232d57f7287SChristian Hohnstaedt "ti,under-voltage-hyst-microvolt", &hyst))
233d57f7287SChristian Hohnstaedt return 0;
234d57f7287SChristian Hohnstaedt
235d57f7287SChristian Hohnstaedt if (hyst != 400000 && hyst != 200000) {
236d57f7287SChristian Hohnstaedt dev_err(tps->dev,
237d57f7287SChristian Hohnstaedt "Invalid ti,under-voltage-hyst-microvolt value\n");
238d57f7287SChristian Hohnstaedt return -EINVAL;
239d57f7287SChristian Hohnstaedt }
240d57f7287SChristian Hohnstaedt
241d57f7287SChristian Hohnstaedt tps65218_update_bits(tps, TPS65218_REG_CONFIG2,
242d57f7287SChristian Hohnstaedt TPS65218_CONFIG2_UVLOHYS,
243d57f7287SChristian Hohnstaedt hyst == 400000 ? TPS65218_CONFIG2_UVLOHYS : 0,
244d57f7287SChristian Hohnstaedt TPS65218_PROTECT_L1);
245d57f7287SChristian Hohnstaedt return 0;
246d57f7287SChristian Hohnstaedt }
247d57f7287SChristian Hohnstaedt
tps65218_voltage_set_uvlo(struct tps65218 * tps)248d57f7287SChristian Hohnstaedt static int tps65218_voltage_set_uvlo(struct tps65218 *tps)
249d57f7287SChristian Hohnstaedt {
250d57f7287SChristian Hohnstaedt u32 uvlo;
251d57f7287SChristian Hohnstaedt int uvloval;
252d57f7287SChristian Hohnstaedt
253d57f7287SChristian Hohnstaedt if (of_property_read_u32(tps->dev->of_node,
254d57f7287SChristian Hohnstaedt "ti,under-voltage-limit-microvolt", &uvlo))
255d57f7287SChristian Hohnstaedt return 0;
256d57f7287SChristian Hohnstaedt
257d57f7287SChristian Hohnstaedt switch (uvlo) {
258d57f7287SChristian Hohnstaedt case 2750000:
259d57f7287SChristian Hohnstaedt uvloval = TPS65218_CONFIG1_UVLO_2750000;
260d57f7287SChristian Hohnstaedt break;
261d57f7287SChristian Hohnstaedt case 2950000:
262d57f7287SChristian Hohnstaedt uvloval = TPS65218_CONFIG1_UVLO_2950000;
263d57f7287SChristian Hohnstaedt break;
264d57f7287SChristian Hohnstaedt case 3250000:
265d57f7287SChristian Hohnstaedt uvloval = TPS65218_CONFIG1_UVLO_3250000;
266d57f7287SChristian Hohnstaedt break;
267d57f7287SChristian Hohnstaedt case 3350000:
268d57f7287SChristian Hohnstaedt uvloval = TPS65218_CONFIG1_UVLO_3350000;
269d57f7287SChristian Hohnstaedt break;
270d57f7287SChristian Hohnstaedt default:
271d57f7287SChristian Hohnstaedt dev_err(tps->dev,
272d57f7287SChristian Hohnstaedt "Invalid ti,under-voltage-limit-microvolt value\n");
273d57f7287SChristian Hohnstaedt return -EINVAL;
274d57f7287SChristian Hohnstaedt }
275d57f7287SChristian Hohnstaedt
276d57f7287SChristian Hohnstaedt tps65218_update_bits(tps, TPS65218_REG_CONFIG1,
277d57f7287SChristian Hohnstaedt TPS65218_CONFIG1_UVLO_MASK, uvloval,
278d57f7287SChristian Hohnstaedt TPS65218_PROTECT_L1);
279d57f7287SChristian Hohnstaedt return 0;
280d57f7287SChristian Hohnstaedt }
281d57f7287SChristian Hohnstaedt
tps65218_probe(struct i2c_client * client)2823d984091SUwe Kleine-König static int tps65218_probe(struct i2c_client *client)
28344b4dc61SKeerthy {
28444b4dc61SKeerthy struct tps65218 *tps;
28544b4dc61SKeerthy int ret;
286f11fa179STero Kristo unsigned int chipid;
28744b4dc61SKeerthy
28844b4dc61SKeerthy tps = devm_kzalloc(&client->dev, sizeof(*tps), GFP_KERNEL);
28944b4dc61SKeerthy if (!tps)
29044b4dc61SKeerthy return -ENOMEM;
29144b4dc61SKeerthy
29244b4dc61SKeerthy i2c_set_clientdata(client, tps);
29344b4dc61SKeerthy tps->dev = &client->dev;
29444b4dc61SKeerthy tps->irq = client->irq;
29544b4dc61SKeerthy tps->regmap = devm_regmap_init_i2c(client, &tps65218_regmap_config);
29644b4dc61SKeerthy if (IS_ERR(tps->regmap)) {
29744b4dc61SKeerthy ret = PTR_ERR(tps->regmap);
29844b4dc61SKeerthy dev_err(tps->dev, "Failed to allocate register map: %d\n",
29944b4dc61SKeerthy ret);
30044b4dc61SKeerthy return ret;
30144b4dc61SKeerthy }
30244b4dc61SKeerthy
30344b4dc61SKeerthy mutex_init(&tps->tps_lock);
30444b4dc61SKeerthy
30575d4c5e0SKeerthy ret = devm_regmap_add_irq_chip(&client->dev, tps->regmap, tps->irq,
30644b4dc61SKeerthy IRQF_ONESHOT, 0, &tps65218_irq_chip,
30744b4dc61SKeerthy &tps->irq_data);
30844b4dc61SKeerthy if (ret < 0)
30944b4dc61SKeerthy return ret;
31044b4dc61SKeerthy
3110aced355SKeerthy ret = regmap_read(tps->regmap, TPS65218_REG_CHIPID, &chipid);
312f11fa179STero Kristo if (ret) {
313f11fa179STero Kristo dev_err(tps->dev, "Failed to read chipid: %d\n", ret);
314f11fa179STero Kristo return ret;
315f11fa179STero Kristo }
316f11fa179STero Kristo
317f11fa179STero Kristo tps->rev = chipid & TPS65218_CHIPID_REV_MASK;
318f11fa179STero Kristo
319d57f7287SChristian Hohnstaedt ret = tps65218_voltage_set_strict(tps);
320d57f7287SChristian Hohnstaedt if (ret)
321d57f7287SChristian Hohnstaedt return ret;
322d57f7287SChristian Hohnstaedt
323d57f7287SChristian Hohnstaedt ret = tps65218_voltage_set_uvlo(tps);
324d57f7287SChristian Hohnstaedt if (ret)
325d57f7287SChristian Hohnstaedt return ret;
326d57f7287SChristian Hohnstaedt
327d57f7287SChristian Hohnstaedt ret = tps65218_voltage_set_uv_hyst(tps);
328d57f7287SChristian Hohnstaedt if (ret)
329d57f7287SChristian Hohnstaedt return ret;
330d57f7287SChristian Hohnstaedt
3314531156dSKeerthy ret = mfd_add_devices(tps->dev, PLATFORM_DEVID_AUTO, tps65218_cells,
3324531156dSKeerthy ARRAY_SIZE(tps65218_cells), NULL, 0,
3334531156dSKeerthy regmap_irq_get_domain(tps->irq_data));
3344531156dSKeerthy
33544b4dc61SKeerthy return ret;
33644b4dc61SKeerthy }
33744b4dc61SKeerthy
33844b4dc61SKeerthy static const struct i2c_device_id tps65218_id_table[] = {
33944b4dc61SKeerthy { "tps65218", TPS65218 },
34044b4dc61SKeerthy { },
34144b4dc61SKeerthy };
34244b4dc61SKeerthy MODULE_DEVICE_TABLE(i2c, tps65218_id_table);
34344b4dc61SKeerthy
34444b4dc61SKeerthy static struct i2c_driver tps65218_driver = {
34544b4dc61SKeerthy .driver = {
34644b4dc61SKeerthy .name = "tps65218",
34744b4dc61SKeerthy .of_match_table = of_tps65218_match_table,
34844b4dc61SKeerthy },
349*9816d859SUwe Kleine-König .probe = tps65218_probe,
35044b4dc61SKeerthy .id_table = tps65218_id_table,
35144b4dc61SKeerthy };
35244b4dc61SKeerthy
35344b4dc61SKeerthy module_i2c_driver(tps65218_driver);
35444b4dc61SKeerthy
35544b4dc61SKeerthy MODULE_AUTHOR("J Keerthy <j-keerthy@ti.com>");
35644b4dc61SKeerthy MODULE_DESCRIPTION("TPS65218 chip family multi-function driver");
35744b4dc61SKeerthy MODULE_LICENSE("GPL v2");
358