1 /* da9063-i2c.c: Interrupt support for Dialog DA9063
3 * Copyright 2012 Dialog Semiconductor Ltd.
4 * Copyright 2013 Philipp Zabel, Pengutronix
6 * Author: Krystian Garbaciak <krystian.garbaciak@diasemi.com>
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.
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>
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>
29 #include <linux/regulator/of_regulator.h>
31 static const struct regmap_range da9063_ad_readable_ranges[] = {
33 .range_min = DA9063_REG_PAGE_CON,
34 .range_max = DA9063_AD_REG_SECOND_D,
36 .range_min = DA9063_REG_SEQ,
37 .range_max = DA9063_REG_ID_32_31,
39 .range_min = DA9063_REG_SEQ_A,
40 .range_max = DA9063_REG_AUTO3_LOW,
42 .range_min = DA9063_REG_T_OFFSET,
43 .range_max = DA9063_AD_REG_GP_ID_19,
45 .range_min = DA9063_REG_CHIP_ID,
46 .range_max = DA9063_REG_CHIP_VARIANT,
50 static const struct regmap_range da9063_ad_writeable_ranges[] = {
52 .range_min = DA9063_REG_PAGE_CON,
53 .range_max = DA9063_REG_PAGE_CON,
55 .range_min = DA9063_REG_FAULT_LOG,
56 .range_max = DA9063_REG_VSYS_MON,
58 .range_min = DA9063_REG_COUNT_S,
59 .range_max = DA9063_AD_REG_ALARM_Y,
61 .range_min = DA9063_REG_SEQ,
62 .range_max = DA9063_REG_ID_32_31,
64 .range_min = DA9063_REG_SEQ_A,
65 .range_max = DA9063_REG_AUTO3_LOW,
67 .range_min = DA9063_REG_CONFIG_I,
68 .range_max = DA9063_AD_REG_MON_REG_4,
70 .range_min = DA9063_AD_REG_GP_ID_0,
71 .range_max = DA9063_AD_REG_GP_ID_19,
75 static const struct regmap_range da9063_ad_volatile_ranges[] = {
77 .range_min = DA9063_REG_STATUS_A,
78 .range_max = DA9063_REG_EVENT_D,
80 .range_min = DA9063_REG_CONTROL_F,
81 .range_max = DA9063_REG_CONTROL_F,
83 .range_min = DA9063_REG_ADC_MAN,
84 .range_max = DA9063_REG_ADC_MAN,
86 .range_min = DA9063_REG_ADC_RES_L,
87 .range_max = DA9063_AD_REG_SECOND_D,
89 .range_min = DA9063_AD_REG_MON_REG_5,
90 .range_max = DA9063_AD_REG_MON_REG_6,
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),
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),
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),
109 static const struct regmap_range da9063_bb_readable_ranges[] = {
111 .range_min = DA9063_REG_PAGE_CON,
112 .range_max = DA9063_BB_REG_SECOND_D,
114 .range_min = DA9063_REG_SEQ,
115 .range_max = DA9063_REG_ID_32_31,
117 .range_min = DA9063_REG_SEQ_A,
118 .range_max = DA9063_REG_AUTO3_LOW,
120 .range_min = DA9063_REG_T_OFFSET,
121 .range_max = DA9063_BB_REG_GP_ID_19,
123 .range_min = DA9063_REG_CHIP_ID,
124 .range_max = DA9063_REG_CHIP_VARIANT,
128 static const struct regmap_range da9063_bb_writeable_ranges[] = {
130 .range_min = DA9063_REG_PAGE_CON,
131 .range_max = DA9063_REG_PAGE_CON,
133 .range_min = DA9063_REG_FAULT_LOG,
134 .range_max = DA9063_REG_VSYS_MON,
136 .range_min = DA9063_REG_COUNT_S,
137 .range_max = DA9063_BB_REG_ALARM_Y,
139 .range_min = DA9063_REG_SEQ,
140 .range_max = DA9063_REG_ID_32_31,
142 .range_min = DA9063_REG_SEQ_A,
143 .range_max = DA9063_REG_AUTO3_LOW,
145 .range_min = DA9063_REG_CONFIG_I,
146 .range_max = DA9063_BB_REG_MON_REG_4,
148 .range_min = DA9063_BB_REG_GP_ID_0,
149 .range_max = DA9063_BB_REG_GP_ID_19,
153 static const struct regmap_range da9063_bb_volatile_ranges[] = {
155 .range_min = DA9063_REG_STATUS_A,
156 .range_max = DA9063_REG_EVENT_D,
158 .range_min = DA9063_REG_CONTROL_F,
159 .range_max = DA9063_REG_CONTROL_F,
161 .range_min = DA9063_REG_ADC_MAN,
162 .range_max = DA9063_REG_ADC_MAN,
164 .range_min = DA9063_REG_ADC_RES_L,
165 .range_max = DA9063_BB_REG_SECOND_D,
167 .range_min = DA9063_BB_REG_MON_REG_5,
168 .range_max = DA9063_BB_REG_MON_REG_6,
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),
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),
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),
187 static const struct regmap_range_cfg da9063_range_cfg[] = {
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,
199 static struct regmap_config da9063_regmap_config = {
202 .ranges = da9063_range_cfg,
203 .num_ranges = ARRAY_SIZE(da9063_range_cfg),
204 .max_register = DA9063_REG_CHIP_VARIANT,
206 .cache_type = REGCACHE_RBTREE,
209 static const struct of_device_id da9063_dt_ids[] = {
210 { .compatible = "dlg,da9063", },
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)
217 struct da9063 *da9063;
220 da9063 = devm_kzalloc(&i2c->dev, sizeof(struct da9063), GFP_KERNEL);
224 i2c_set_clientdata(i2c, da9063);
225 da9063->dev = &i2c->dev;
226 da9063->chip_irq = i2c->irq;
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;
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;
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",
246 return da9063_device_init(da9063, i2c->irq);
249 static int da9063_i2c_remove(struct i2c_client *i2c)
251 struct da9063 *da9063 = i2c_get_clientdata(i2c);
253 da9063_device_exit(da9063);
258 static const struct i2c_device_id da9063_i2c_id[] = {
259 {"da9063", PMIC_DA9063},
262 MODULE_DEVICE_TABLE(i2c, da9063_i2c_id);
264 static struct i2c_driver da9063_i2c_driver = {
267 .of_match_table = of_match_ptr(da9063_dt_ids),
269 .probe = da9063_i2c_probe,
270 .remove = da9063_i2c_remove,
271 .id_table = da9063_i2c_id,
274 module_i2c_driver(da9063_i2c_driver);