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 	.unmask_base = RT5120_REG_INTENABLE,
63 	.ack_base = RT5120_REG_INTSTAT,
64 	.use_ack = true,
65 	.num_regs = 1,
66 	.irqs = rt5120_irqs,
67 	.num_irqs = ARRAY_SIZE(rt5120_irqs),
68 };
69 
70 static const struct resource rt5120_regulator_resources[] = {
71 	DEFINE_RES_IRQ(RT5120_INT_HOTDIE),
72 };
73 
74 static const struct resource rt5120_pwrkey_resources[] = {
75 	DEFINE_RES_IRQ_NAMED(RT5120_INT_PWRKEY_PRESS, "pwrkey-press"),
76 	DEFINE_RES_IRQ_NAMED(RT5120_INT_PWRKEY_REL, "pwrkey-release"),
77 };
78 
79 static const struct mfd_cell rt5120_devs[] = {
80 	MFD_CELL_RES("rt5120-regulator", rt5120_regulator_resources),
81 	MFD_CELL_OF("rt5120-pwrkey", rt5120_pwrkey_resources, NULL, 0, 0, "richtek,rt5120-pwrkey"),
82 };
83 
rt5120_probe(struct i2c_client * i2c)84 static int rt5120_probe(struct i2c_client *i2c)
85 {
86 	struct device *dev = &i2c->dev;
87 	struct regmap *regmap;
88 	struct regmap_irq_chip_data *irq_data;
89 	int ret;
90 
91 	regmap = devm_regmap_init_i2c(i2c, &rt5120_regmap_config);
92 	if (IS_ERR(regmap))
93 		return dev_err_probe(dev, PTR_ERR(regmap),
94 				     "Failed to init regmap\n");
95 
96 	ret = devm_regmap_add_irq_chip(dev, regmap, i2c->irq, IRQF_ONESHOT, 0,
97 				       &rt5120_irq_chip, &irq_data);
98 	if (ret)
99 		return dev_err_probe(dev, ret, "Failed to add IRQ chip\n");
100 
101 	return devm_mfd_add_devices(dev, PLATFORM_DEVID_AUTO, rt5120_devs,
102 				    ARRAY_SIZE(rt5120_devs), NULL, 0,
103 				    regmap_irq_get_domain(irq_data));
104 }
105 
106 static const struct of_device_id rt5120_device_match_table[] = {
107 	{ .compatible = "richtek,rt5120" },
108 	{}
109 };
110 MODULE_DEVICE_TABLE(of, rt5120_device_match_table);
111 
112 static struct i2c_driver rt5120_driver = {
113 	.driver = {
114 		.name = "rt5120",
115 		.of_match_table = rt5120_device_match_table,
116 	},
117 	.probe_new = rt5120_probe,
118 };
119 module_i2c_driver(rt5120_driver);
120 
121 MODULE_AUTHOR("ChiYuan Huang <cy_huang@richtek.com>");
122 MODULE_DESCRIPTION("Richtek RT5120 I2C driver");
123 MODULE_LICENSE("GPL v2");
124