1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2022 Richtek Technology Corp.
4 * Author: ChiYuan Huang <cy_huang@richtek.com>
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>
14 #define RT5120_REG_INTENABLE 0x1D
15 #define RT5120_REG_INTSTAT 0x1E
16 #define RT5120_REG_FZCMODE 0x44
18 #define RT5120_INT_HOTDIE 0
19 #define RT5120_INT_PWRKEY_REL 5
20 #define RT5120_INT_PWRKEY_PRESS 6
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),
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),
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),
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),
44 static const struct regmap_config rt5120_regmap_config = {
47 .max_register = RT5120_REG_FZCMODE,
49 .wr_table = &rt5120_wr_table,
50 .rd_table = &rt5120_rd_table,
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),
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,
67 .num_irqs = ARRAY_SIZE(rt5120_irqs),
70 static const struct resource rt5120_regulator_resources[] = {
71 DEFINE_RES_IRQ(RT5120_INT_HOTDIE),
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"),
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"),
84 static int rt5120_probe(struct i2c_client *i2c)
86 struct device *dev = &i2c->dev;
87 struct regmap *regmap;
88 struct regmap_irq_chip_data *irq_data;
91 regmap = devm_regmap_init_i2c(i2c, &rt5120_regmap_config);
93 return dev_err_probe(dev, PTR_ERR(regmap),
94 "Failed to init regmap\n");
96 ret = devm_regmap_add_irq_chip(dev, regmap, i2c->irq, IRQF_ONESHOT, 0,
97 &rt5120_irq_chip, &irq_data);
99 return dev_err_probe(dev, ret, "Failed to add IRQ chip\n");
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));
106 static const struct of_device_id rt5120_device_match_table[] = {
107 { .compatible = "richtek,rt5120" },
110 MODULE_DEVICE_TABLE(of, rt5120_device_match_table);
112 static struct i2c_driver rt5120_driver = {
115 .of_match_table = rt5120_device_match_table,
117 .probe = rt5120_probe,
119 module_i2c_driver(rt5120_driver);
121 MODULE_AUTHOR("ChiYuan Huang <cy_huang@richtek.com>");
122 MODULE_DESCRIPTION("Richtek RT5120 I2C driver");
123 MODULE_LICENSE("GPL v2");