blob: 21fd8d9a217b01d2992e1225e6dae1e3a043c376 [file] [log] [blame]
Krystian Garbaciak8e685482013-07-29 19:00:43 +02001/* 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
Opensource [Steve Twiss]9cb42e22014-07-21 11:39:33 +010028static const struct regmap_range da9063_ad_readable_ranges[] = {
Krystian Garbaciak8e685482013-07-29 19:00:43 +020029 {
30 .range_min = DA9063_REG_PAGE_CON,
Opensource [Steve Twiss]9cb42e22014-07-21 11:39:33 +010031 .range_max = DA9063_AD_REG_SECOND_D,
Krystian Garbaciak8e685482013-07-29 19:00:43 +020032 }, {
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,
Opensource [Steve Twiss]9cb42e22014-07-21 11:39:33 +010040 .range_max = DA9063_AD_REG_GP_ID_19,
Krystian Garbaciak8e685482013-07-29 19:00:43 +020041 }, {
42 .range_min = DA9063_REG_CHIP_ID,
43 .range_max = DA9063_REG_CHIP_VARIANT,
44 },
45};
46
Opensource [Steve Twiss]9cb42e22014-07-21 11:39:33 +010047static const struct regmap_range da9063_ad_writeable_ranges[] = {
Krystian Garbaciak8e685482013-07-29 19:00:43 +020048 {
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,
Opensource [Steve Twiss]9cb42e22014-07-21 11:39:33 +010056 .range_max = DA9063_AD_REG_ALARM_Y,
Krystian Garbaciak8e685482013-07-29 19:00:43 +020057 }, {
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,
Opensource [Steve Twiss]9cb42e22014-07-21 11:39:33 +010065 .range_max = DA9063_AD_REG_MON_REG_4,
Krystian Garbaciak8e685482013-07-29 19:00:43 +020066 }, {
Opensource [Steve Twiss]9cb42e22014-07-21 11:39:33 +010067 .range_min = DA9063_AD_REG_GP_ID_0,
68 .range_max = DA9063_AD_REG_GP_ID_19,
Krystian Garbaciak8e685482013-07-29 19:00:43 +020069 },
70};
71
Opensource [Steve Twiss]9cb42e22014-07-21 11:39:33 +010072static const struct regmap_range da9063_ad_volatile_ranges[] = {
Krystian Garbaciak8e685482013-07-29 19:00:43 +020073 {
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,
Opensource [Steve Twiss]9cb42e22014-07-21 11:39:33 +010084 .range_max = DA9063_AD_REG_SECOND_D,
Krystian Garbaciak8e685482013-07-29 19:00:43 +020085 }, {
Opensource [Steve Twiss]9cb42e22014-07-21 11:39:33 +010086 .range_min = DA9063_AD_REG_MON_REG_5,
87 .range_max = DA9063_AD_REG_MON_REG_6,
Krystian Garbaciak8e685482013-07-29 19:00:43 +020088 },
89};
90
Opensource [Steve Twiss]9cb42e22014-07-21 11:39:33 +010091static 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),
Krystian Garbaciak8e685482013-07-29 19:00:43 +020094};
95
Opensource [Steve Twiss]9cb42e22014-07-21 11:39:33 +010096static 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),
Krystian Garbaciak8e685482013-07-29 19:00:43 +020099};
100
Opensource [Steve Twiss]9cb42e22014-07-21 11:39:33 +0100101static 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
106static 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
125static 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
150static 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
169static 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
174static 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
179static 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),
Krystian Garbaciak8e685482013-07-29 19:00:43 +0200182};
183
184static 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
196static 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,
Krystian Garbaciak8e685482013-07-29 19:00:43 +0200204};
205
206static 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
Opensource [Steve Twiss]9cb42e22014-07-21 11:39:33 +0100220 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
Krystian Garbaciak8e685482013-07-29 19:00:43 +0200230 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
241static 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
250static const struct i2c_device_id da9063_i2c_id[] = {
251 {"da9063", PMIC_DA9063},
252 {},
253};
254MODULE_DEVICE_TABLE(i2c, da9063_i2c_id);
255
256static 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
266module_i2c_driver(da9063_i2c_driver);