xref: /openbmc/linux/drivers/mfd/da9063-i2c.c (revision 30614cf3)
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 static const struct regmap_range da9063_ad_readable_ranges[] = {
29 	{
30 		.range_min = DA9063_REG_PAGE_CON,
31 		.range_max = DA9063_AD_REG_SECOND_D,
32 	}, {
33 		.range_min = DA9063_REG_SEQ,
34 		.range_max = DA9063_REG_ID_32_31,
35 	}, {
36 		.range_min = DA9063_REG_SEQ_A,
37 		.range_max = DA9063_REG_AUTO3_LOW,
38 	}, {
39 		.range_min = DA9063_REG_T_OFFSET,
40 		.range_max = DA9063_AD_REG_GP_ID_19,
41 	}, {
42 		.range_min = DA9063_REG_CHIP_ID,
43 		.range_max = DA9063_REG_CHIP_VARIANT,
44 	},
45 };
46 
47 static const struct regmap_range da9063_ad_writeable_ranges[] = {
48 	{
49 		.range_min = DA9063_REG_PAGE_CON,
50 		.range_max = DA9063_REG_PAGE_CON,
51 	}, {
52 		.range_min = DA9063_REG_FAULT_LOG,
53 		.range_max = DA9063_REG_VSYS_MON,
54 	}, {
55 		.range_min = DA9063_REG_COUNT_S,
56 		.range_max = DA9063_AD_REG_ALARM_Y,
57 	}, {
58 		.range_min = DA9063_REG_SEQ,
59 		.range_max = DA9063_REG_ID_32_31,
60 	}, {
61 		.range_min = DA9063_REG_SEQ_A,
62 		.range_max = DA9063_REG_AUTO3_LOW,
63 	}, {
64 		.range_min = DA9063_REG_CONFIG_I,
65 		.range_max = DA9063_AD_REG_MON_REG_4,
66 	}, {
67 		.range_min = DA9063_AD_REG_GP_ID_0,
68 		.range_max = DA9063_AD_REG_GP_ID_19,
69 	},
70 };
71 
72 static const struct regmap_range da9063_ad_volatile_ranges[] = {
73 	{
74 		.range_min = DA9063_REG_STATUS_A,
75 		.range_max = DA9063_REG_EVENT_D,
76 	}, {
77 		.range_min = DA9063_REG_CONTROL_F,
78 		.range_max = DA9063_REG_CONTROL_F,
79 	}, {
80 		.range_min = DA9063_REG_ADC_MAN,
81 		.range_max = DA9063_REG_ADC_MAN,
82 	}, {
83 		.range_min = DA9063_REG_ADC_RES_L,
84 		.range_max = DA9063_AD_REG_SECOND_D,
85 	}, {
86 		.range_min = DA9063_AD_REG_MON_REG_5,
87 		.range_max = DA9063_AD_REG_MON_REG_6,
88 	},
89 };
90 
91 static const struct regmap_access_table da9063_ad_readable_table = {
92 	.yes_ranges = da9063_ad_readable_ranges,
93 	.n_yes_ranges = ARRAY_SIZE(da9063_ad_readable_ranges),
94 };
95 
96 static const struct regmap_access_table da9063_ad_writeable_table = {
97 	.yes_ranges = da9063_ad_writeable_ranges,
98 	.n_yes_ranges = ARRAY_SIZE(da9063_ad_writeable_ranges),
99 };
100 
101 static const struct regmap_access_table da9063_ad_volatile_table = {
102 	.yes_ranges = da9063_ad_volatile_ranges,
103 	.n_yes_ranges = ARRAY_SIZE(da9063_ad_volatile_ranges),
104 };
105 
106 static const struct regmap_range da9063_bb_readable_ranges[] = {
107 	{
108 		.range_min = DA9063_REG_PAGE_CON,
109 		.range_max = DA9063_BB_REG_SECOND_D,
110 	}, {
111 		.range_min = DA9063_REG_SEQ,
112 		.range_max = DA9063_REG_ID_32_31,
113 	}, {
114 		.range_min = DA9063_REG_SEQ_A,
115 		.range_max = DA9063_REG_AUTO3_LOW,
116 	}, {
117 		.range_min = DA9063_REG_T_OFFSET,
118 		.range_max = DA9063_BB_REG_GP_ID_19,
119 	}, {
120 		.range_min = DA9063_REG_CHIP_ID,
121 		.range_max = DA9063_REG_CHIP_VARIANT,
122 	},
123 };
124 
125 static const struct regmap_range da9063_bb_writeable_ranges[] = {
126 	{
127 		.range_min = DA9063_REG_PAGE_CON,
128 		.range_max = DA9063_REG_PAGE_CON,
129 	}, {
130 		.range_min = DA9063_REG_FAULT_LOG,
131 		.range_max = DA9063_REG_VSYS_MON,
132 	}, {
133 		.range_min = DA9063_REG_COUNT_S,
134 		.range_max = DA9063_BB_REG_ALARM_Y,
135 	}, {
136 		.range_min = DA9063_REG_SEQ,
137 		.range_max = DA9063_REG_ID_32_31,
138 	}, {
139 		.range_min = DA9063_REG_SEQ_A,
140 		.range_max = DA9063_REG_AUTO3_LOW,
141 	}, {
142 		.range_min = DA9063_REG_CONFIG_I,
143 		.range_max = DA9063_BB_REG_MON_REG_4,
144 	}, {
145 		.range_min = DA9063_BB_REG_GP_ID_0,
146 		.range_max = DA9063_BB_REG_GP_ID_19,
147 	},
148 };
149 
150 static const struct regmap_range da9063_bb_volatile_ranges[] = {
151 	{
152 		.range_min = DA9063_REG_STATUS_A,
153 		.range_max = DA9063_REG_EVENT_D,
154 	}, {
155 		.range_min = DA9063_REG_CONTROL_F,
156 		.range_max = DA9063_REG_CONTROL_F,
157 	}, {
158 		.range_min = DA9063_REG_ADC_MAN,
159 		.range_max = DA9063_REG_ADC_MAN,
160 	}, {
161 		.range_min = DA9063_REG_ADC_RES_L,
162 		.range_max = DA9063_BB_REG_SECOND_D,
163 	}, {
164 		.range_min = DA9063_BB_REG_MON_REG_5,
165 		.range_max = DA9063_BB_REG_MON_REG_6,
166 	},
167 };
168 
169 static const struct regmap_access_table da9063_bb_readable_table = {
170 	.yes_ranges = da9063_bb_readable_ranges,
171 	.n_yes_ranges = ARRAY_SIZE(da9063_bb_readable_ranges),
172 };
173 
174 static const struct regmap_access_table da9063_bb_writeable_table = {
175 	.yes_ranges = da9063_bb_writeable_ranges,
176 	.n_yes_ranges = ARRAY_SIZE(da9063_bb_writeable_ranges),
177 };
178 
179 static const struct regmap_access_table da9063_bb_volatile_table = {
180 	.yes_ranges = da9063_bb_volatile_ranges,
181 	.n_yes_ranges = ARRAY_SIZE(da9063_bb_volatile_ranges),
182 };
183 
184 static const struct regmap_range_cfg da9063_range_cfg[] = {
185 	{
186 		.range_min = DA9063_REG_PAGE_CON,
187 		.range_max = DA9063_REG_CHIP_VARIANT,
188 		.selector_reg = DA9063_REG_PAGE_CON,
189 		.selector_mask = 1 << DA9063_I2C_PAGE_SEL_SHIFT,
190 		.selector_shift = DA9063_I2C_PAGE_SEL_SHIFT,
191 		.window_start = 0,
192 		.window_len = 256,
193 	}
194 };
195 
196 static struct regmap_config da9063_regmap_config = {
197 	.reg_bits = 8,
198 	.val_bits = 8,
199 	.ranges = da9063_range_cfg,
200 	.num_ranges = ARRAY_SIZE(da9063_range_cfg),
201 	.max_register = DA9063_REG_CHIP_VARIANT,
202 
203 	.cache_type = REGCACHE_RBTREE,
204 };
205 
206 static int da9063_i2c_probe(struct i2c_client *i2c,
207 	const struct i2c_device_id *id)
208 {
209 	struct da9063 *da9063;
210 	int ret;
211 
212 	da9063 = devm_kzalloc(&i2c->dev, sizeof(struct da9063), GFP_KERNEL);
213 	if (da9063 == NULL)
214 		return -ENOMEM;
215 
216 	i2c_set_clientdata(i2c, da9063);
217 	da9063->dev = &i2c->dev;
218 	da9063->chip_irq = i2c->irq;
219 
220 	if (da9063->variant_code == PMIC_DA9063_AD) {
221 		da9063_regmap_config.rd_table = &da9063_ad_readable_table;
222 		da9063_regmap_config.wr_table = &da9063_ad_writeable_table;
223 		da9063_regmap_config.volatile_table = &da9063_ad_volatile_table;
224 	} else {
225 		da9063_regmap_config.rd_table = &da9063_bb_readable_table;
226 		da9063_regmap_config.wr_table = &da9063_bb_writeable_table;
227 		da9063_regmap_config.volatile_table = &da9063_bb_volatile_table;
228 	}
229 
230 	da9063->regmap = devm_regmap_init_i2c(i2c, &da9063_regmap_config);
231 	if (IS_ERR(da9063->regmap)) {
232 		ret = PTR_ERR(da9063->regmap);
233 		dev_err(da9063->dev, "Failed to allocate register map: %d\n",
234 			ret);
235 		return ret;
236 	}
237 
238 	return da9063_device_init(da9063, i2c->irq);
239 }
240 
241 static int da9063_i2c_remove(struct i2c_client *i2c)
242 {
243 	struct da9063 *da9063 = i2c_get_clientdata(i2c);
244 
245 	da9063_device_exit(da9063);
246 
247 	return 0;
248 }
249 
250 static const struct i2c_device_id da9063_i2c_id[] = {
251 	{"da9063", PMIC_DA9063},
252 	{},
253 };
254 MODULE_DEVICE_TABLE(i2c, da9063_i2c_id);
255 
256 static struct i2c_driver da9063_i2c_driver = {
257 	.driver = {
258 		.name = "da9063",
259 		.owner = THIS_MODULE,
260 	},
261 	.probe    = da9063_i2c_probe,
262 	.remove   = da9063_i2c_remove,
263 	.id_table = da9063_i2c_id,
264 };
265 
266 module_i2c_driver(da9063_i2c_driver);
267