xref: /openbmc/linux/drivers/mfd/rt5120.c (revision 7587cdef)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2022 Richtek Technology Corp.
4  * Author: ChiYuan Huang <cy_huang@richtek.com>
5  */
6 
7 #include <linux/i2c.h>
8 #include <linux/kernel.h>
9 #include <linux/mfd/core.h>
10 #include <linux/module.h>
11 #include <linux/mod_devicetable.h>
12 #include <linux/regmap.h>
13 
14 #define RT5120_REG_INTENABLE	0x1D
15 #define RT5120_REG_INTSTAT	0x1E
16 #define RT5120_REG_FZCMODE	0x44
17 
18 #define RT5120_INT_HOTDIE	0
19 #define RT5120_INT_PWRKEY_REL	5
20 #define RT5120_INT_PWRKEY_PRESS	6
21 
22 static const struct regmap_range rt5120_rd_yes_ranges[] = {
23 	regmap_reg_range(0x03, 0x13),
24 	regmap_reg_range(0x1c, 0x20),
25 	regmap_reg_range(0x44, 0x44),
26 };
27 
28 static const struct regmap_range rt5120_wr_yes_ranges[] = {
29 	regmap_reg_range(0x06, 0x13),
30 	regmap_reg_range(0x1c, 0x20),
31 	regmap_reg_range(0x44, 0x44),
32 };
33 
34 static const struct regmap_access_table rt5120_rd_table = {
35 	.yes_ranges = rt5120_rd_yes_ranges,
36 	.n_yes_ranges = ARRAY_SIZE(rt5120_rd_yes_ranges),
37 };
38 
39 static const struct regmap_access_table rt5120_wr_table = {
40 	.yes_ranges = rt5120_wr_yes_ranges,
41 	.n_yes_ranges = ARRAY_SIZE(rt5120_wr_yes_ranges),
42 };
43 
44 static const struct regmap_config rt5120_regmap_config = {
45 	.reg_bits = 8,
46 	.val_bits = 8,
47 	.max_register = RT5120_REG_FZCMODE,
48 
49 	.wr_table = &rt5120_wr_table,
50 	.rd_table = &rt5120_rd_table,
51 };
52 
53 static const struct regmap_irq rt5120_irqs[] = {
54 	REGMAP_IRQ_REG_LINE(RT5120_INT_HOTDIE, 8),
55 	REGMAP_IRQ_REG_LINE(RT5120_INT_PWRKEY_REL, 8),
56 	REGMAP_IRQ_REG_LINE(RT5120_INT_PWRKEY_PRESS, 8),
57 };
58 
59 static const struct regmap_irq_chip rt5120_irq_chip = {
60 	.name = "rt5120-pmic",
61 	.status_base = RT5120_REG_INTSTAT,
62 	.mask_base = RT5120_REG_INTENABLE,
63 	.ack_base = RT5120_REG_INTSTAT,
64 	.mask_invert = true,
65 	.use_ack = true,
66 	.num_regs = 1,
67 	.irqs = rt5120_irqs,
68 	.num_irqs = ARRAY_SIZE(rt5120_irqs),
69 };
70 
71 static const struct resource rt5120_regulator_resources[] = {
72 	DEFINE_RES_IRQ(RT5120_INT_HOTDIE),
73 };
74 
75 static const struct resource rt5120_pwrkey_resources[] = {
76 	DEFINE_RES_IRQ_NAMED(RT5120_INT_PWRKEY_PRESS, "pwrkey-press"),
77 	DEFINE_RES_IRQ_NAMED(RT5120_INT_PWRKEY_REL, "pwrkey-release"),
78 };
79 
80 static const struct mfd_cell rt5120_devs[] = {
81 	MFD_CELL_RES("rt5120-regulator", rt5120_regulator_resources),
82 	MFD_CELL_OF("rt5120-pwrkey", rt5120_pwrkey_resources, NULL, 0, 0, "richtek,rt5120-pwrkey"),
83 };
84 
85 static int rt5120_probe(struct i2c_client *i2c)
86 {
87 	struct device *dev = &i2c->dev;
88 	struct regmap *regmap;
89 	struct regmap_irq_chip_data *irq_data;
90 	int ret;
91 
92 	regmap = devm_regmap_init_i2c(i2c, &rt5120_regmap_config);
93 	if (IS_ERR(regmap))
94 		return dev_err_probe(dev, PTR_ERR(regmap),
95 				     "Failed to init regmap\n");
96 
97 	ret = devm_regmap_add_irq_chip(dev, regmap, i2c->irq, IRQF_ONESHOT, 0,
98 				       &rt5120_irq_chip, &irq_data);
99 	if (ret)
100 		return dev_err_probe(dev, ret, "Failed to add IRQ chip\n");
101 
102 	return devm_mfd_add_devices(dev, PLATFORM_DEVID_AUTO, rt5120_devs,
103 				    ARRAY_SIZE(rt5120_devs), NULL, 0,
104 				    regmap_irq_get_domain(irq_data));
105 }
106 
107 static const struct of_device_id rt5120_device_match_table[] = {
108 	{ .compatible = "richtek,rt5120" },
109 	{}
110 };
111 MODULE_DEVICE_TABLE(of, rt5120_device_match_table);
112 
113 static struct i2c_driver rt5120_driver = {
114 	.driver = {
115 		.name = "rt5120",
116 		.of_match_table = rt5120_device_match_table,
117 	},
118 	.probe_new = rt5120_probe,
119 };
120 module_i2c_driver(rt5120_driver);
121 
122 MODULE_AUTHOR("ChiYuan Huang <cy_huang@richtek.com>");
123 MODULE_DESCRIPTION("Richtek RT5120 I2C driver");
124 MODULE_LICENSE("GPL v2");
125