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