xref: /openbmc/linux/drivers/mfd/da9063-i2c.c (revision 4f3db074)
1 /* da9063-i2c.c: Interrupt support for Dialog DA9063
2  *
3  * Copyright 2012 Dialog Semiconductor Ltd.
4  * Copyright 2013 Philipp Zabel, Pengutronix
5  *
6  * Author: Krystian Garbaciak <krystian.garbaciak@diasemi.com>
7  *
8  *  This program is free software; you can redistribute  it and/or modify it
9  *  under  the terms of  the GNU General  Public License as published by the
10  *  Free Software Foundation;  either version 2 of the  License, or (at your
11  *  option) any later version.
12  *
13  */
14 
15 #include <linux/kernel.h>
16 #include <linux/module.h>
17 #include <linux/i2c.h>
18 #include <linux/regmap.h>
19 #include <linux/delay.h>
20 #include <linux/slab.h>
21 #include <linux/err.h>
22 
23 #include <linux/mfd/core.h>
24 #include <linux/mfd/da9063/core.h>
25 #include <linux/mfd/da9063/pdata.h>
26 #include <linux/mfd/da9063/registers.h>
27 
28 #include <linux/of.h>
29 #include <linux/regulator/of_regulator.h>
30 
31 static const struct regmap_range da9063_ad_readable_ranges[] = {
32 	{
33 		.range_min = DA9063_REG_PAGE_CON,
34 		.range_max = DA9063_AD_REG_SECOND_D,
35 	}, {
36 		.range_min = DA9063_REG_SEQ,
37 		.range_max = DA9063_REG_ID_32_31,
38 	}, {
39 		.range_min = DA9063_REG_SEQ_A,
40 		.range_max = DA9063_REG_AUTO3_LOW,
41 	}, {
42 		.range_min = DA9063_REG_T_OFFSET,
43 		.range_max = DA9063_AD_REG_GP_ID_19,
44 	}, {
45 		.range_min = DA9063_REG_CHIP_ID,
46 		.range_max = DA9063_REG_CHIP_VARIANT,
47 	},
48 };
49 
50 static const struct regmap_range da9063_ad_writeable_ranges[] = {
51 	{
52 		.range_min = DA9063_REG_PAGE_CON,
53 		.range_max = DA9063_REG_PAGE_CON,
54 	}, {
55 		.range_min = DA9063_REG_FAULT_LOG,
56 		.range_max = DA9063_REG_VSYS_MON,
57 	}, {
58 		.range_min = DA9063_REG_COUNT_S,
59 		.range_max = DA9063_AD_REG_ALARM_Y,
60 	}, {
61 		.range_min = DA9063_REG_SEQ,
62 		.range_max = DA9063_REG_ID_32_31,
63 	}, {
64 		.range_min = DA9063_REG_SEQ_A,
65 		.range_max = DA9063_REG_AUTO3_LOW,
66 	}, {
67 		.range_min = DA9063_REG_CONFIG_I,
68 		.range_max = DA9063_AD_REG_MON_REG_4,
69 	}, {
70 		.range_min = DA9063_AD_REG_GP_ID_0,
71 		.range_max = DA9063_AD_REG_GP_ID_19,
72 	},
73 };
74 
75 static const struct regmap_range da9063_ad_volatile_ranges[] = {
76 	{
77 		.range_min = DA9063_REG_STATUS_A,
78 		.range_max = DA9063_REG_EVENT_D,
79 	}, {
80 		.range_min = DA9063_REG_CONTROL_F,
81 		.range_max = DA9063_REG_CONTROL_F,
82 	}, {
83 		.range_min = DA9063_REG_ADC_MAN,
84 		.range_max = DA9063_REG_ADC_MAN,
85 	}, {
86 		.range_min = DA9063_REG_ADC_RES_L,
87 		.range_max = DA9063_AD_REG_SECOND_D,
88 	}, {
89 		.range_min = DA9063_AD_REG_MON_REG_5,
90 		.range_max = DA9063_AD_REG_MON_REG_6,
91 	},
92 };
93 
94 static const struct regmap_access_table da9063_ad_readable_table = {
95 	.yes_ranges = da9063_ad_readable_ranges,
96 	.n_yes_ranges = ARRAY_SIZE(da9063_ad_readable_ranges),
97 };
98 
99 static const struct regmap_access_table da9063_ad_writeable_table = {
100 	.yes_ranges = da9063_ad_writeable_ranges,
101 	.n_yes_ranges = ARRAY_SIZE(da9063_ad_writeable_ranges),
102 };
103 
104 static const struct regmap_access_table da9063_ad_volatile_table = {
105 	.yes_ranges = da9063_ad_volatile_ranges,
106 	.n_yes_ranges = ARRAY_SIZE(da9063_ad_volatile_ranges),
107 };
108 
109 static const struct regmap_range da9063_bb_readable_ranges[] = {
110 	{
111 		.range_min = DA9063_REG_PAGE_CON,
112 		.range_max = DA9063_BB_REG_SECOND_D,
113 	}, {
114 		.range_min = DA9063_REG_SEQ,
115 		.range_max = DA9063_REG_ID_32_31,
116 	}, {
117 		.range_min = DA9063_REG_SEQ_A,
118 		.range_max = DA9063_REG_AUTO3_LOW,
119 	}, {
120 		.range_min = DA9063_REG_T_OFFSET,
121 		.range_max = DA9063_BB_REG_GP_ID_19,
122 	}, {
123 		.range_min = DA9063_REG_CHIP_ID,
124 		.range_max = DA9063_REG_CHIP_VARIANT,
125 	},
126 };
127 
128 static const struct regmap_range da9063_bb_writeable_ranges[] = {
129 	{
130 		.range_min = DA9063_REG_PAGE_CON,
131 		.range_max = DA9063_REG_PAGE_CON,
132 	}, {
133 		.range_min = DA9063_REG_FAULT_LOG,
134 		.range_max = DA9063_REG_VSYS_MON,
135 	}, {
136 		.range_min = DA9063_REG_COUNT_S,
137 		.range_max = DA9063_BB_REG_ALARM_Y,
138 	}, {
139 		.range_min = DA9063_REG_SEQ,
140 		.range_max = DA9063_REG_ID_32_31,
141 	}, {
142 		.range_min = DA9063_REG_SEQ_A,
143 		.range_max = DA9063_REG_AUTO3_LOW,
144 	}, {
145 		.range_min = DA9063_REG_CONFIG_I,
146 		.range_max = DA9063_BB_REG_MON_REG_4,
147 	}, {
148 		.range_min = DA9063_BB_REG_GP_ID_0,
149 		.range_max = DA9063_BB_REG_GP_ID_19,
150 	},
151 };
152 
153 static const struct regmap_range da9063_bb_volatile_ranges[] = {
154 	{
155 		.range_min = DA9063_REG_STATUS_A,
156 		.range_max = DA9063_REG_EVENT_D,
157 	}, {
158 		.range_min = DA9063_REG_CONTROL_F,
159 		.range_max = DA9063_REG_CONTROL_F,
160 	}, {
161 		.range_min = DA9063_REG_ADC_MAN,
162 		.range_max = DA9063_REG_ADC_MAN,
163 	}, {
164 		.range_min = DA9063_REG_ADC_RES_L,
165 		.range_max = DA9063_BB_REG_SECOND_D,
166 	}, {
167 		.range_min = DA9063_BB_REG_MON_REG_5,
168 		.range_max = DA9063_BB_REG_MON_REG_6,
169 	},
170 };
171 
172 static const struct regmap_access_table da9063_bb_readable_table = {
173 	.yes_ranges = da9063_bb_readable_ranges,
174 	.n_yes_ranges = ARRAY_SIZE(da9063_bb_readable_ranges),
175 };
176 
177 static const struct regmap_access_table da9063_bb_writeable_table = {
178 	.yes_ranges = da9063_bb_writeable_ranges,
179 	.n_yes_ranges = ARRAY_SIZE(da9063_bb_writeable_ranges),
180 };
181 
182 static const struct regmap_access_table da9063_bb_volatile_table = {
183 	.yes_ranges = da9063_bb_volatile_ranges,
184 	.n_yes_ranges = ARRAY_SIZE(da9063_bb_volatile_ranges),
185 };
186 
187 static const struct regmap_range_cfg da9063_range_cfg[] = {
188 	{
189 		.range_min = DA9063_REG_PAGE_CON,
190 		.range_max = DA9063_REG_CHIP_VARIANT,
191 		.selector_reg = DA9063_REG_PAGE_CON,
192 		.selector_mask = 1 << DA9063_I2C_PAGE_SEL_SHIFT,
193 		.selector_shift = DA9063_I2C_PAGE_SEL_SHIFT,
194 		.window_start = 0,
195 		.window_len = 256,
196 	}
197 };
198 
199 static struct regmap_config da9063_regmap_config = {
200 	.reg_bits = 8,
201 	.val_bits = 8,
202 	.ranges = da9063_range_cfg,
203 	.num_ranges = ARRAY_SIZE(da9063_range_cfg),
204 	.max_register = DA9063_REG_CHIP_VARIANT,
205 
206 	.cache_type = REGCACHE_RBTREE,
207 };
208 
209 static const struct of_device_id da9063_dt_ids[] = {
210 	{ .compatible = "dlg,da9063", },
211 	{ }
212 };
213 MODULE_DEVICE_TABLE(of, da9063_dt_ids);
214 static int da9063_i2c_probe(struct i2c_client *i2c,
215 	const struct i2c_device_id *id)
216 {
217 	struct da9063 *da9063;
218 	int ret;
219 
220 	da9063 = devm_kzalloc(&i2c->dev, sizeof(struct da9063), GFP_KERNEL);
221 	if (da9063 == NULL)
222 		return -ENOMEM;
223 
224 	i2c_set_clientdata(i2c, da9063);
225 	da9063->dev = &i2c->dev;
226 	da9063->chip_irq = i2c->irq;
227 
228 	if (da9063->variant_code == PMIC_DA9063_AD) {
229 		da9063_regmap_config.rd_table = &da9063_ad_readable_table;
230 		da9063_regmap_config.wr_table = &da9063_ad_writeable_table;
231 		da9063_regmap_config.volatile_table = &da9063_ad_volatile_table;
232 	} else {
233 		da9063_regmap_config.rd_table = &da9063_bb_readable_table;
234 		da9063_regmap_config.wr_table = &da9063_bb_writeable_table;
235 		da9063_regmap_config.volatile_table = &da9063_bb_volatile_table;
236 	}
237 
238 	da9063->regmap = devm_regmap_init_i2c(i2c, &da9063_regmap_config);
239 	if (IS_ERR(da9063->regmap)) {
240 		ret = PTR_ERR(da9063->regmap);
241 		dev_err(da9063->dev, "Failed to allocate register map: %d\n",
242 			ret);
243 		return ret;
244 	}
245 
246 	return da9063_device_init(da9063, i2c->irq);
247 }
248 
249 static int da9063_i2c_remove(struct i2c_client *i2c)
250 {
251 	struct da9063 *da9063 = i2c_get_clientdata(i2c);
252 
253 	da9063_device_exit(da9063);
254 
255 	return 0;
256 }
257 
258 static const struct i2c_device_id da9063_i2c_id[] = {
259 	{"da9063", PMIC_DA9063},
260 	{},
261 };
262 MODULE_DEVICE_TABLE(i2c, da9063_i2c_id);
263 
264 static struct i2c_driver da9063_i2c_driver = {
265 	.driver = {
266 		.name = "da9063",
267 		.owner = THIS_MODULE,
268 		.of_match_table = of_match_ptr(da9063_dt_ids),
269 	},
270 	.probe    = da9063_i2c_probe,
271 	.remove   = da9063_i2c_remove,
272 	.id_table = da9063_i2c_id,
273 };
274 
275 module_i2c_driver(da9063_i2c_driver);
276