072e6feab6e99957c16c45703bad5079a45b117d
[deliverable/linux.git] / drivers / mfd / hi655x-pmic.c
1 /*
2 * Device driver for MFD hi655x PMIC
3 *
4 * Copyright (c) 2016 Hisilicon.
5 *
6 * Authors:
7 * Chen Feng <puck.chen@hisilicon.com>
8 * Fei Wang <w.f@huawei.com>
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
13 */
14
15 #include <linux/gpio.h>
16 #include <linux/io.h>
17 #include <linux/interrupt.h>
18 #include <linux/init.h>
19 #include <linux/mfd/core.h>
20 #include <linux/mfd/hi655x-pmic.h>
21 #include <linux/module.h>
22 #include <linux/of_gpio.h>
23 #include <linux/of_platform.h>
24 #include <linux/platform_device.h>
25 #include <linux/regmap.h>
26
27 static const struct mfd_cell hi655x_pmic_devs[] = {
28 { .name = "hi655x-regulator", },
29 };
30
31 static const struct regmap_irq hi655x_irqs[] = {
32 { .reg_offset = 0, .mask = OTMP_D1R_INT_MASK },
33 { .reg_offset = 0, .mask = VSYS_2P5_R_INT_MASK },
34 { .reg_offset = 0, .mask = VSYS_UV_D3R_INT_MASK },
35 { .reg_offset = 0, .mask = VSYS_6P0_D200UR_INT_MASK },
36 { .reg_offset = 0, .mask = PWRON_D4SR_INT_MASK },
37 { .reg_offset = 0, .mask = PWRON_D20F_INT_MASK },
38 { .reg_offset = 0, .mask = PWRON_D20R_INT_MASK },
39 { .reg_offset = 0, .mask = RESERVE_INT_MASK },
40 };
41
42 static const struct regmap_irq_chip hi655x_irq_chip = {
43 .name = "hi655x-pmic",
44 .irqs = hi655x_irqs,
45 .num_regs = 1,
46 .num_irqs = ARRAY_SIZE(hi655x_irqs),
47 .status_base = HI655X_IRQ_STAT_BASE,
48 .ack_base = HI655X_IRQ_STAT_BASE,
49 .mask_base = HI655X_IRQ_MASK_BASE,
50 };
51
52 static struct regmap_config hi655x_regmap_config = {
53 .reg_bits = 32,
54 .reg_stride = HI655X_STRIDE,
55 .val_bits = 8,
56 .max_register = HI655X_BUS_ADDR(0xFFF),
57 };
58
59 static void hi655x_local_irq_clear(struct regmap *map)
60 {
61 int i;
62
63 regmap_write(map, HI655X_ANA_IRQM_BASE, HI655X_IRQ_CLR);
64 for (i = 0; i < HI655X_IRQ_ARRAY; i++) {
65 regmap_write(map, HI655X_IRQ_STAT_BASE + i * HI655X_STRIDE,
66 HI655X_IRQ_CLR);
67 }
68 }
69
70 static int hi655x_pmic_probe(struct platform_device *pdev)
71 {
72 int ret;
73 struct hi655x_pmic *pmic;
74 struct device *dev = &pdev->dev;
75 struct device_node *np = dev->of_node;
76 void __iomem *base;
77
78 pmic = devm_kzalloc(dev, sizeof(*pmic), GFP_KERNEL);
79 if (!pmic)
80 return -ENOMEM;
81 pmic->dev = dev;
82
83 pmic->res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
84 if (!pmic->res)
85 return -ENOENT;
86
87 base = devm_ioremap_resource(dev, pmic->res);
88 if (!base)
89 return -ENOMEM;
90
91 pmic->regmap = devm_regmap_init_mmio_clk(dev, NULL, base,
92 &hi655x_regmap_config);
93
94 regmap_read(pmic->regmap, HI655X_BUS_ADDR(HI655X_VER_REG), &pmic->ver);
95 if ((pmic->ver < PMU_VER_START) || (pmic->ver > PMU_VER_END)) {
96 dev_warn(dev, "PMU version %d unsupported\n", pmic->ver);
97 return -EINVAL;
98 }
99
100 hi655x_local_irq_clear(pmic->regmap);
101
102 pmic->gpio = of_get_named_gpio(np, "pmic-gpios", 0);
103 if (!gpio_is_valid(pmic->gpio)) {
104 dev_err(dev, "Failed to get the pmic-gpios\n");
105 return -ENODEV;
106 }
107
108 ret = devm_gpio_request_one(dev, pmic->gpio, GPIOF_IN,
109 "hi655x_pmic_irq");
110 if (ret < 0) {
111 dev_err(dev, "Failed to request gpio %d ret = %d\n",
112 pmic->gpio, ret);
113 return ret;
114 }
115
116 ret = regmap_add_irq_chip(pmic->regmap, gpio_to_irq(pmic->gpio),
117 IRQF_TRIGGER_LOW | IRQF_NO_SUSPEND, 0,
118 &hi655x_irq_chip, &pmic->irq_data);
119 if (ret) {
120 dev_err(dev, "Failed to obtain 'hi655x_pmic_irq' %d\n", ret);
121 return ret;
122 }
123
124 platform_set_drvdata(pdev, pmic);
125
126 ret = mfd_add_devices(dev, PLATFORM_DEVID_AUTO, hi655x_pmic_devs,
127 ARRAY_SIZE(hi655x_pmic_devs), NULL, 0, NULL);
128 if (ret) {
129 dev_err(dev, "Failed to register device %d\n", ret);
130 regmap_del_irq_chip(gpio_to_irq(pmic->gpio), pmic->irq_data);
131 return ret;
132 }
133
134 return 0;
135 }
136
137 static int hi655x_pmic_remove(struct platform_device *pdev)
138 {
139 struct hi655x_pmic *pmic = platform_get_drvdata(pdev);
140
141 regmap_del_irq_chip(gpio_to_irq(pmic->gpio), pmic->irq_data);
142 mfd_remove_devices(&pdev->dev);
143 return 0;
144 }
145
146 static const struct of_device_id hi655x_pmic_match[] = {
147 { .compatible = "hisilicon,hi655x-pmic", },
148 {},
149 };
150
151 static struct platform_driver hi655x_pmic_driver = {
152 .driver = {
153 .name = "hi655x-pmic",
154 .of_match_table = of_match_ptr(hi655x_pmic_match),
155 },
156 .probe = hi655x_pmic_probe,
157 .remove = hi655x_pmic_remove,
158 };
159 module_platform_driver(hi655x_pmic_driver);
160
161 MODULE_AUTHOR("Chen Feng <puck.chen@hisilicon.com>");
162 MODULE_DESCRIPTION("Hisilicon hi655x PMIC driver");
163 MODULE_LICENSE("GPL v2");
This page took 0.035917 seconds and 4 git commands to generate.