1d925da5cSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
20b271258SBeomho Seo /*
30b271258SBeomho Seo * MFD core driver for the Richtek RT5033.
40b271258SBeomho Seo *
50b271258SBeomho Seo * RT5033 comprises multiple sub-devices switcing charger, fuel gauge,
60b271258SBeomho Seo * flash LED, current source, LDO and BUCK regulators.
70b271258SBeomho Seo *
80b271258SBeomho Seo * Copyright (C) 2014 Samsung Electronics, Co., Ltd.
90b271258SBeomho Seo * Author: Beomho Seo <beomho.seo@samsung.com>
100b271258SBeomho Seo */
110b271258SBeomho Seo
120b271258SBeomho Seo #include <linux/err.h>
13dc0c386eSRob Herring #include <linux/mod_devicetable.h>
140b271258SBeomho Seo #include <linux/module.h>
150b271258SBeomho Seo #include <linux/interrupt.h>
160b271258SBeomho Seo #include <linux/mfd/core.h>
170b271258SBeomho Seo #include <linux/mfd/rt5033.h>
180b271258SBeomho Seo #include <linux/mfd/rt5033-private.h>
190b271258SBeomho Seo
200b271258SBeomho Seo static const struct regmap_irq rt5033_irqs[] = {
210b271258SBeomho Seo { .mask = RT5033_PMIC_IRQ_BUCKOCP, },
220b271258SBeomho Seo { .mask = RT5033_PMIC_IRQ_BUCKLV, },
230b271258SBeomho Seo { .mask = RT5033_PMIC_IRQ_SAFELDOLV, },
240b271258SBeomho Seo { .mask = RT5033_PMIC_IRQ_LDOLV, },
250b271258SBeomho Seo { .mask = RT5033_PMIC_IRQ_OT, },
260b271258SBeomho Seo { .mask = RT5033_PMIC_IRQ_VDDA_UV, },
270b271258SBeomho Seo };
280b271258SBeomho Seo
290b271258SBeomho Seo static const struct regmap_irq_chip rt5033_irq_chip = {
300b271258SBeomho Seo .name = "rt5033",
310b271258SBeomho Seo .status_base = RT5033_REG_PMIC_IRQ_STAT,
324627ececSAidan MacDonald .unmask_base = RT5033_REG_PMIC_IRQ_CTRL,
330b271258SBeomho Seo .num_regs = 1,
340b271258SBeomho Seo .irqs = rt5033_irqs,
350b271258SBeomho Seo .num_irqs = ARRAY_SIZE(rt5033_irqs),
360b271258SBeomho Seo };
370b271258SBeomho Seo
380b271258SBeomho Seo static const struct mfd_cell rt5033_devs[] = {
390b271258SBeomho Seo { .name = "rt5033-regulator", },
400b271258SBeomho Seo {
410b271258SBeomho Seo .name = "rt5033-charger",
420b271258SBeomho Seo .of_compatible = "richtek,rt5033-charger",
430b271258SBeomho Seo }, {
44b487c17dSIngi Kim .name = "rt5033-led",
45b487c17dSIngi Kim .of_compatible = "richtek,rt5033-led",
460b271258SBeomho Seo },
470b271258SBeomho Seo };
480b271258SBeomho Seo
490b271258SBeomho Seo static const struct regmap_config rt5033_regmap_config = {
500b271258SBeomho Seo .reg_bits = 8,
510b271258SBeomho Seo .val_bits = 8,
520b271258SBeomho Seo .max_register = RT5033_REG_END,
530b271258SBeomho Seo };
540b271258SBeomho Seo
rt5033_i2c_probe(struct i2c_client * i2c)55e46b578cSUwe Kleine-König static int rt5033_i2c_probe(struct i2c_client *i2c)
560b271258SBeomho Seo {
570b271258SBeomho Seo struct rt5033_dev *rt5033;
58c4747d7cSJakob Hauser unsigned int dev_id, chip_rev;
590b271258SBeomho Seo int ret;
600b271258SBeomho Seo
610b271258SBeomho Seo rt5033 = devm_kzalloc(&i2c->dev, sizeof(*rt5033), GFP_KERNEL);
620b271258SBeomho Seo if (!rt5033)
630b271258SBeomho Seo return -ENOMEM;
640b271258SBeomho Seo
650b271258SBeomho Seo i2c_set_clientdata(i2c, rt5033);
660b271258SBeomho Seo rt5033->dev = &i2c->dev;
670b271258SBeomho Seo rt5033->irq = i2c->irq;
680b271258SBeomho Seo rt5033->wakeup = true;
690b271258SBeomho Seo
700b271258SBeomho Seo rt5033->regmap = devm_regmap_init_i2c(i2c, &rt5033_regmap_config);
710b271258SBeomho Seo if (IS_ERR(rt5033->regmap)) {
720b271258SBeomho Seo dev_err(&i2c->dev, "Failed to allocate register map.\n");
730b271258SBeomho Seo return PTR_ERR(rt5033->regmap);
740b271258SBeomho Seo }
750b271258SBeomho Seo
760b271258SBeomho Seo ret = regmap_read(rt5033->regmap, RT5033_REG_DEVICE_ID, &dev_id);
770b271258SBeomho Seo if (ret) {
780b271258SBeomho Seo dev_err(&i2c->dev, "Device not found\n");
790b271258SBeomho Seo return -ENODEV;
800b271258SBeomho Seo }
81c4747d7cSJakob Hauser chip_rev = dev_id & RT5033_CHIP_REV_MASK;
82c4747d7cSJakob Hauser dev_info(&i2c->dev, "Device found (rev. %d)\n", chip_rev);
830b271258SBeomho Seo
84*ed257122SZhang Changzhong ret = devm_regmap_add_irq_chip(rt5033->dev, rt5033->regmap,
85*ed257122SZhang Changzhong rt5033->irq, IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
860b271258SBeomho Seo 0, &rt5033_irq_chip, &rt5033->irq_data);
870b271258SBeomho Seo if (ret) {
880b271258SBeomho Seo dev_err(&i2c->dev, "Failed to request IRQ %d: %d\n",
890b271258SBeomho Seo rt5033->irq, ret);
900b271258SBeomho Seo return ret;
910b271258SBeomho Seo }
920b271258SBeomho Seo
936b719ebaSLaxman Dewangan ret = devm_mfd_add_devices(rt5033->dev, -1, rt5033_devs,
940b271258SBeomho Seo ARRAY_SIZE(rt5033_devs), NULL, 0,
950b271258SBeomho Seo regmap_irq_get_domain(rt5033->irq_data));
960b271258SBeomho Seo if (ret < 0) {
970b271258SBeomho Seo dev_err(&i2c->dev, "Failed to add RT5033 child devices.\n");
980b271258SBeomho Seo return ret;
990b271258SBeomho Seo }
1000b271258SBeomho Seo
1010b271258SBeomho Seo device_init_wakeup(rt5033->dev, rt5033->wakeup);
1020b271258SBeomho Seo
1030b271258SBeomho Seo return 0;
1040b271258SBeomho Seo }
1050b271258SBeomho Seo
1060b271258SBeomho Seo static const struct i2c_device_id rt5033_i2c_id[] = {
1070b271258SBeomho Seo { "rt5033", },
1080b271258SBeomho Seo { }
1090b271258SBeomho Seo };
1100b271258SBeomho Seo MODULE_DEVICE_TABLE(i2c, rt5033_i2c_id);
1110b271258SBeomho Seo
1120b271258SBeomho Seo static const struct of_device_id rt5033_dt_match[] = {
1130b271258SBeomho Seo { .compatible = "richtek,rt5033", },
1140b271258SBeomho Seo { }
1150b271258SBeomho Seo };
1164895e493SJavier Martinez Canillas MODULE_DEVICE_TABLE(of, rt5033_dt_match);
1170b271258SBeomho Seo
1180b271258SBeomho Seo static struct i2c_driver rt5033_driver = {
1190b271258SBeomho Seo .driver = {
1200b271258SBeomho Seo .name = "rt5033",
121a232bcd2SKrzysztof Kozlowski .of_match_table = rt5033_dt_match,
1220b271258SBeomho Seo },
1239816d859SUwe Kleine-König .probe = rt5033_i2c_probe,
1240b271258SBeomho Seo .id_table = rt5033_i2c_id,
1250b271258SBeomho Seo };
1260b271258SBeomho Seo module_i2c_driver(rt5033_driver);
1270b271258SBeomho Seo
1280b271258SBeomho Seo MODULE_DESCRIPTION("Richtek RT5033 multi-function core driver");
1290b271258SBeomho Seo MODULE_AUTHOR("Beomho Seo <beomho.seo@samsung.com>");
1300b271258SBeomho Seo MODULE_LICENSE("GPL");
131