blob: 98192d4863e4c2864d5cc5ed68945b8883aa4cb0 [file] [log] [blame]
rjw1f884582022-01-06 17:20:42 +08001/*
2 * ROHM BD9571MWV-M MFD driver
3 *
4 * Copyright (C) 2017 Marek Vasut <marek.vasut+renesas@gmail.com>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 *
10 * This program is distributed "as is" WITHOUT ANY WARRANTY of any
11 * kind, whether expressed or implied; without even the implied warranty
12 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License version 2 for more details.
14 *
15 * Based on the TPS65086 driver
16 */
17
18#include <linux/i2c.h>
19#include <linux/interrupt.h>
20#include <linux/mfd/core.h>
21#include <linux/module.h>
22
23#include <linux/mfd/bd9571mwv.h>
24
25static const struct mfd_cell bd9571mwv_cells[] = {
26 { .name = "bd9571mwv-regulator", },
27 { .name = "bd9571mwv-gpio", },
28};
29
30static const struct regmap_range bd9571mwv_readable_yes_ranges[] = {
31 regmap_reg_range(BD9571MWV_VENDOR_CODE, BD9571MWV_PRODUCT_REVISION),
32 regmap_reg_range(BD9571MWV_AVS_SET_MONI, BD9571MWV_AVS_DVFS_VID(3)),
33 regmap_reg_range(BD9571MWV_VD18_VID, BD9571MWV_VD33_VID),
34 regmap_reg_range(BD9571MWV_DVFS_VINIT, BD9571MWV_DVFS_VINIT),
35 regmap_reg_range(BD9571MWV_DVFS_SETVMAX, BD9571MWV_DVFS_MONIVDAC),
36 regmap_reg_range(BD9571MWV_GPIO_IN, BD9571MWV_GPIO_IN),
37 regmap_reg_range(BD9571MWV_GPIO_INT, BD9571MWV_GPIO_INTMASK),
38 regmap_reg_range(BD9571MWV_INT_INTREQ, BD9571MWV_INT_INTMASK),
39};
40
41static const struct regmap_access_table bd9571mwv_readable_table = {
42 .yes_ranges = bd9571mwv_readable_yes_ranges,
43 .n_yes_ranges = ARRAY_SIZE(bd9571mwv_readable_yes_ranges),
44};
45
46static const struct regmap_range bd9571mwv_writable_yes_ranges[] = {
47 regmap_reg_range(BD9571MWV_AVS_VD09_VID(0), BD9571MWV_AVS_VD09_VID(3)),
48 regmap_reg_range(BD9571MWV_DVFS_SETVID, BD9571MWV_DVFS_SETVID),
49 regmap_reg_range(BD9571MWV_GPIO_DIR, BD9571MWV_GPIO_OUT),
50 regmap_reg_range(BD9571MWV_GPIO_INT_SET, BD9571MWV_GPIO_INTMASK),
51 regmap_reg_range(BD9571MWV_INT_INTREQ, BD9571MWV_INT_INTMASK),
52};
53
54static const struct regmap_access_table bd9571mwv_writable_table = {
55 .yes_ranges = bd9571mwv_writable_yes_ranges,
56 .n_yes_ranges = ARRAY_SIZE(bd9571mwv_writable_yes_ranges),
57};
58
59static const struct regmap_range bd9571mwv_volatile_yes_ranges[] = {
60 regmap_reg_range(BD9571MWV_DVFS_MONIVDAC, BD9571MWV_DVFS_MONIVDAC),
61 regmap_reg_range(BD9571MWV_GPIO_IN, BD9571MWV_GPIO_IN),
62 regmap_reg_range(BD9571MWV_GPIO_INT, BD9571MWV_GPIO_INT),
63 regmap_reg_range(BD9571MWV_INT_INTREQ, BD9571MWV_INT_INTREQ),
64};
65
66static const struct regmap_access_table bd9571mwv_volatile_table = {
67 .yes_ranges = bd9571mwv_volatile_yes_ranges,
68 .n_yes_ranges = ARRAY_SIZE(bd9571mwv_volatile_yes_ranges),
69};
70
71static const struct regmap_config bd9571mwv_regmap_config = {
72 .reg_bits = 8,
73 .val_bits = 8,
74 .cache_type = REGCACHE_RBTREE,
75 .rd_table = &bd9571mwv_readable_table,
76 .wr_table = &bd9571mwv_writable_table,
77 .volatile_table = &bd9571mwv_volatile_table,
78 .max_register = 0xff,
79};
80
81static const struct regmap_irq bd9571mwv_irqs[] = {
82 REGMAP_IRQ_REG(BD9571MWV_IRQ_MD1, 0,
83 BD9571MWV_INT_INTREQ_MD1_INT),
84 REGMAP_IRQ_REG(BD9571MWV_IRQ_MD2_E1, 0,
85 BD9571MWV_INT_INTREQ_MD2_E1_INT),
86 REGMAP_IRQ_REG(BD9571MWV_IRQ_MD2_E2, 0,
87 BD9571MWV_INT_INTREQ_MD2_E2_INT),
88 REGMAP_IRQ_REG(BD9571MWV_IRQ_PROT_ERR, 0,
89 BD9571MWV_INT_INTREQ_PROT_ERR_INT),
90 REGMAP_IRQ_REG(BD9571MWV_IRQ_GP, 0,
91 BD9571MWV_INT_INTREQ_GP_INT),
92 REGMAP_IRQ_REG(BD9571MWV_IRQ_128H_OF, 0,
93 BD9571MWV_INT_INTREQ_128H_OF_INT),
94 REGMAP_IRQ_REG(BD9571MWV_IRQ_WDT_OF, 0,
95 BD9571MWV_INT_INTREQ_WDT_OF_INT),
96 REGMAP_IRQ_REG(BD9571MWV_IRQ_BKUP_TRG, 0,
97 BD9571MWV_INT_INTREQ_BKUP_TRG_INT),
98};
99
100static struct regmap_irq_chip bd9571mwv_irq_chip = {
101 .name = "bd9571mwv",
102 .status_base = BD9571MWV_INT_INTREQ,
103 .mask_base = BD9571MWV_INT_INTMASK,
104 .ack_base = BD9571MWV_INT_INTREQ,
105 .init_ack_masked = true,
106 .num_regs = 1,
107 .irqs = bd9571mwv_irqs,
108 .num_irqs = ARRAY_SIZE(bd9571mwv_irqs),
109};
110
111static int bd9571mwv_identify(struct bd9571mwv *bd)
112{
113 struct device *dev = bd->dev;
114 unsigned int value;
115 int ret;
116
117 ret = regmap_read(bd->regmap, BD9571MWV_VENDOR_CODE, &value);
118 if (ret) {
119 dev_err(dev, "Failed to read vendor code register (ret=%i)\n",
120 ret);
121 return ret;
122 }
123
124 if (value != BD9571MWV_VENDOR_CODE_VAL) {
125 dev_err(dev, "Invalid vendor code ID %02x (expected %02x)\n",
126 value, BD9571MWV_VENDOR_CODE_VAL);
127 return -EINVAL;
128 }
129
130 ret = regmap_read(bd->regmap, BD9571MWV_PRODUCT_CODE, &value);
131 if (ret) {
132 dev_err(dev, "Failed to read product code register (ret=%i)\n",
133 ret);
134 return ret;
135 }
136
137 if (value != BD9571MWV_PRODUCT_CODE_VAL) {
138 dev_err(dev, "Invalid product code ID %02x (expected %02x)\n",
139 value, BD9571MWV_PRODUCT_CODE_VAL);
140 return -EINVAL;
141 }
142
143 ret = regmap_read(bd->regmap, BD9571MWV_PRODUCT_REVISION, &value);
144 if (ret) {
145 dev_err(dev, "Failed to read revision register (ret=%i)\n",
146 ret);
147 return ret;
148 }
149
150 dev_info(dev, "Device: BD9571MWV rev. %d\n", value & 0xff);
151
152 return 0;
153}
154
155static int bd9571mwv_probe(struct i2c_client *client,
156 const struct i2c_device_id *ids)
157{
158 struct bd9571mwv *bd;
159 int ret;
160
161 bd = devm_kzalloc(&client->dev, sizeof(*bd), GFP_KERNEL);
162 if (!bd)
163 return -ENOMEM;
164
165 i2c_set_clientdata(client, bd);
166 bd->dev = &client->dev;
167 bd->irq = client->irq;
168
169 bd->regmap = devm_regmap_init_i2c(client, &bd9571mwv_regmap_config);
170 if (IS_ERR(bd->regmap)) {
171 dev_err(bd->dev, "Failed to initialize register map\n");
172 return PTR_ERR(bd->regmap);
173 }
174
175 ret = bd9571mwv_identify(bd);
176 if (ret)
177 return ret;
178
179 ret = regmap_add_irq_chip(bd->regmap, bd->irq, IRQF_ONESHOT, 0,
180 &bd9571mwv_irq_chip, &bd->irq_data);
181 if (ret) {
182 dev_err(bd->dev, "Failed to register IRQ chip\n");
183 return ret;
184 }
185
186 ret = mfd_add_devices(bd->dev, PLATFORM_DEVID_AUTO, bd9571mwv_cells,
187 ARRAY_SIZE(bd9571mwv_cells), NULL, 0,
188 regmap_irq_get_domain(bd->irq_data));
189 if (ret) {
190 regmap_del_irq_chip(bd->irq, bd->irq_data);
191 return ret;
192 }
193
194 return 0;
195}
196
197static int bd9571mwv_remove(struct i2c_client *client)
198{
199 struct bd9571mwv *bd = i2c_get_clientdata(client);
200
201 regmap_del_irq_chip(bd->irq, bd->irq_data);
202
203 return 0;
204}
205
206static const struct of_device_id bd9571mwv_of_match_table[] = {
207 { .compatible = "rohm,bd9571mwv", },
208 { /* sentinel */ }
209};
210MODULE_DEVICE_TABLE(of, bd9571mwv_of_match_table);
211
212static const struct i2c_device_id bd9571mwv_id_table[] = {
213 { "bd9571mwv", 0 },
214 { /* sentinel */ }
215};
216MODULE_DEVICE_TABLE(i2c, bd9571mwv_id_table);
217
218static struct i2c_driver bd9571mwv_driver = {
219 .driver = {
220 .name = "bd9571mwv",
221 .of_match_table = bd9571mwv_of_match_table,
222 },
223 .probe = bd9571mwv_probe,
224 .remove = bd9571mwv_remove,
225 .id_table = bd9571mwv_id_table,
226};
227module_i2c_driver(bd9571mwv_driver);
228
229MODULE_AUTHOR("Marek Vasut <marek.vasut+renesas@gmail.com>");
230MODULE_DESCRIPTION("BD9571MWV PMIC Driver");
231MODULE_LICENSE("GPL v2");