blob: 455de74c0dd21f421c33f2b7600af8ca1446a98f [file] [log] [blame]
Wolfram Sang9efbc6f2019-03-18 16:57:24 +01001// SPDX-License-Identifier: GPL-2.0+
2/* I2C support for Dialog DA9063
Krystian Garbaciak8e685482013-07-29 19:00:43 +02003 *
4 * Copyright 2012 Dialog Semiconductor Ltd.
5 * Copyright 2013 Philipp Zabel, Pengutronix
6 *
Steve Twiss37778d82016-08-08 14:16:11 +01007 * Author: Krystian Garbaciak, Dialog Semiconductor
Krystian Garbaciak8e685482013-07-29 19:00:43 +02008 */
9
10#include <linux/kernel.h>
11#include <linux/module.h>
12#include <linux/i2c.h>
13#include <linux/regmap.h>
14#include <linux/delay.h>
15#include <linux/slab.h>
16#include <linux/err.h>
17
18#include <linux/mfd/core.h>
19#include <linux/mfd/da9063/core.h>
Krystian Garbaciak8e685482013-07-29 19:00:43 +020020#include <linux/mfd/da9063/registers.h>
21
Steve Twiss71e03de2015-01-20 13:54:25 +000022#include <linux/of.h>
23#include <linux/regulator/of_regulator.h>
24
Opensource [Steve Twiss]9cb42e22014-07-21 11:39:33 +010025static const struct regmap_range da9063_ad_readable_ranges[] = {
Marek Vasut29050862018-06-11 13:58:40 +020026 regmap_reg_range(DA9063_REG_PAGE_CON, DA9063_AD_REG_SECOND_D),
27 regmap_reg_range(DA9063_REG_SEQ, DA9063_REG_ID_32_31),
28 regmap_reg_range(DA9063_REG_SEQ_A, DA9063_REG_AUTO3_LOW),
29 regmap_reg_range(DA9063_REG_T_OFFSET, DA9063_AD_REG_GP_ID_19),
30 regmap_reg_range(DA9063_REG_CHIP_ID, DA9063_REG_CHIP_VARIANT),
Krystian Garbaciak8e685482013-07-29 19:00:43 +020031};
32
Opensource [Steve Twiss]9cb42e22014-07-21 11:39:33 +010033static const struct regmap_range da9063_ad_writeable_ranges[] = {
Marek Vasut29050862018-06-11 13:58:40 +020034 regmap_reg_range(DA9063_REG_PAGE_CON, DA9063_REG_PAGE_CON),
35 regmap_reg_range(DA9063_REG_FAULT_LOG, DA9063_REG_VSYS_MON),
36 regmap_reg_range(DA9063_REG_COUNT_S, DA9063_AD_REG_ALARM_Y),
37 regmap_reg_range(DA9063_REG_SEQ, DA9063_REG_ID_32_31),
38 regmap_reg_range(DA9063_REG_SEQ_A, DA9063_REG_AUTO3_LOW),
39 regmap_reg_range(DA9063_REG_CONFIG_I, DA9063_AD_REG_MON_REG_4),
40 regmap_reg_range(DA9063_AD_REG_GP_ID_0, DA9063_AD_REG_GP_ID_19),
Krystian Garbaciak8e685482013-07-29 19:00:43 +020041};
42
Opensource [Steve Twiss]9cb42e22014-07-21 11:39:33 +010043static const struct regmap_range da9063_ad_volatile_ranges[] = {
Marek Vasut29050862018-06-11 13:58:40 +020044 regmap_reg_range(DA9063_REG_PAGE_CON, DA9063_REG_EVENT_D),
45 regmap_reg_range(DA9063_REG_CONTROL_A, DA9063_REG_CONTROL_B),
46 regmap_reg_range(DA9063_REG_CONTROL_E, DA9063_REG_CONTROL_F),
47 regmap_reg_range(DA9063_REG_BCORE2_CONT, DA9063_REG_LDO11_CONT),
48 regmap_reg_range(DA9063_REG_DVC_1, DA9063_REG_ADC_MAN),
49 regmap_reg_range(DA9063_REG_ADC_RES_L, DA9063_AD_REG_SECOND_D),
50 regmap_reg_range(DA9063_REG_SEQ, DA9063_REG_SEQ),
51 regmap_reg_range(DA9063_REG_EN_32K, DA9063_REG_EN_32K),
52 regmap_reg_range(DA9063_AD_REG_MON_REG_5, DA9063_AD_REG_MON_REG_6),
Krystian Garbaciak8e685482013-07-29 19:00:43 +020053};
54
Opensource [Steve Twiss]9cb42e22014-07-21 11:39:33 +010055static const struct regmap_access_table da9063_ad_readable_table = {
56 .yes_ranges = da9063_ad_readable_ranges,
57 .n_yes_ranges = ARRAY_SIZE(da9063_ad_readable_ranges),
Krystian Garbaciak8e685482013-07-29 19:00:43 +020058};
59
Opensource [Steve Twiss]9cb42e22014-07-21 11:39:33 +010060static const struct regmap_access_table da9063_ad_writeable_table = {
61 .yes_ranges = da9063_ad_writeable_ranges,
62 .n_yes_ranges = ARRAY_SIZE(da9063_ad_writeable_ranges),
Krystian Garbaciak8e685482013-07-29 19:00:43 +020063};
64
Opensource [Steve Twiss]9cb42e22014-07-21 11:39:33 +010065static const struct regmap_access_table da9063_ad_volatile_table = {
66 .yes_ranges = da9063_ad_volatile_ranges,
67 .n_yes_ranges = ARRAY_SIZE(da9063_ad_volatile_ranges),
68};
69
70static const struct regmap_range da9063_bb_readable_ranges[] = {
Marek Vasut29050862018-06-11 13:58:40 +020071 regmap_reg_range(DA9063_REG_PAGE_CON, DA9063_BB_REG_SECOND_D),
72 regmap_reg_range(DA9063_REG_SEQ, DA9063_REG_ID_32_31),
73 regmap_reg_range(DA9063_REG_SEQ_A, DA9063_REG_AUTO3_LOW),
74 regmap_reg_range(DA9063_REG_T_OFFSET, DA9063_BB_REG_GP_ID_19),
75 regmap_reg_range(DA9063_REG_CHIP_ID, DA9063_REG_CHIP_VARIANT),
Opensource [Steve Twiss]9cb42e22014-07-21 11:39:33 +010076};
77
78static const struct regmap_range da9063_bb_writeable_ranges[] = {
Marek Vasut29050862018-06-11 13:58:40 +020079 regmap_reg_range(DA9063_REG_PAGE_CON, DA9063_REG_PAGE_CON),
80 regmap_reg_range(DA9063_REG_FAULT_LOG, DA9063_REG_VSYS_MON),
81 regmap_reg_range(DA9063_REG_COUNT_S, DA9063_BB_REG_ALARM_Y),
82 regmap_reg_range(DA9063_REG_SEQ, DA9063_REG_ID_32_31),
83 regmap_reg_range(DA9063_REG_SEQ_A, DA9063_REG_AUTO3_LOW),
84 regmap_reg_range(DA9063_REG_CONFIG_I, DA9063_BB_REG_MON_REG_4),
85 regmap_reg_range(DA9063_BB_REG_GP_ID_0, DA9063_BB_REG_GP_ID_19),
Opensource [Steve Twiss]9cb42e22014-07-21 11:39:33 +010086};
87
88static const struct regmap_range da9063_bb_volatile_ranges[] = {
Marek Vasut29050862018-06-11 13:58:40 +020089 regmap_reg_range(DA9063_REG_PAGE_CON, DA9063_REG_EVENT_D),
90 regmap_reg_range(DA9063_REG_CONTROL_A, DA9063_REG_CONTROL_B),
91 regmap_reg_range(DA9063_REG_CONTROL_E, DA9063_REG_CONTROL_F),
92 regmap_reg_range(DA9063_REG_BCORE2_CONT, DA9063_REG_LDO11_CONT),
93 regmap_reg_range(DA9063_REG_DVC_1, DA9063_REG_ADC_MAN),
94 regmap_reg_range(DA9063_REG_ADC_RES_L, DA9063_BB_REG_SECOND_D),
95 regmap_reg_range(DA9063_REG_SEQ, DA9063_REG_SEQ),
96 regmap_reg_range(DA9063_REG_EN_32K, DA9063_REG_EN_32K),
97 regmap_reg_range(DA9063_BB_REG_MON_REG_5, DA9063_BB_REG_MON_REG_6),
Opensource [Steve Twiss]9cb42e22014-07-21 11:39:33 +010098};
99
100static const struct regmap_access_table da9063_bb_readable_table = {
101 .yes_ranges = da9063_bb_readable_ranges,
102 .n_yes_ranges = ARRAY_SIZE(da9063_bb_readable_ranges),
103};
104
105static const struct regmap_access_table da9063_bb_writeable_table = {
106 .yes_ranges = da9063_bb_writeable_ranges,
107 .n_yes_ranges = ARRAY_SIZE(da9063_bb_writeable_ranges),
108};
109
110static const struct regmap_access_table da9063_bb_volatile_table = {
111 .yes_ranges = da9063_bb_volatile_ranges,
112 .n_yes_ranges = ARRAY_SIZE(da9063_bb_volatile_ranges),
Krystian Garbaciak8e685482013-07-29 19:00:43 +0200113};
114
Marek Vasut4bd6ad02018-06-11 13:58:47 +0200115static const struct regmap_range da9063l_bb_readable_ranges[] = {
116 regmap_reg_range(DA9063_REG_PAGE_CON, DA9063_REG_MON_A10_RES),
117 regmap_reg_range(DA9063_REG_SEQ, DA9063_REG_ID_32_31),
118 regmap_reg_range(DA9063_REG_SEQ_A, DA9063_REG_AUTO3_LOW),
119 regmap_reg_range(DA9063_REG_T_OFFSET, DA9063_BB_REG_GP_ID_19),
120 regmap_reg_range(DA9063_REG_CHIP_ID, DA9063_REG_CHIP_VARIANT),
121};
122
123static const struct regmap_range da9063l_bb_writeable_ranges[] = {
124 regmap_reg_range(DA9063_REG_PAGE_CON, DA9063_REG_PAGE_CON),
125 regmap_reg_range(DA9063_REG_FAULT_LOG, DA9063_REG_VSYS_MON),
126 regmap_reg_range(DA9063_REG_SEQ, DA9063_REG_ID_32_31),
127 regmap_reg_range(DA9063_REG_SEQ_A, DA9063_REG_AUTO3_LOW),
128 regmap_reg_range(DA9063_REG_CONFIG_I, DA9063_BB_REG_MON_REG_4),
129 regmap_reg_range(DA9063_BB_REG_GP_ID_0, DA9063_BB_REG_GP_ID_19),
130};
131
132static const struct regmap_range da9063l_bb_volatile_ranges[] = {
133 regmap_reg_range(DA9063_REG_PAGE_CON, DA9063_REG_EVENT_D),
134 regmap_reg_range(DA9063_REG_CONTROL_A, DA9063_REG_CONTROL_B),
135 regmap_reg_range(DA9063_REG_CONTROL_E, DA9063_REG_CONTROL_F),
136 regmap_reg_range(DA9063_REG_BCORE2_CONT, DA9063_REG_LDO11_CONT),
137 regmap_reg_range(DA9063_REG_DVC_1, DA9063_REG_ADC_MAN),
138 regmap_reg_range(DA9063_REG_ADC_RES_L, DA9063_REG_MON_A10_RES),
139 regmap_reg_range(DA9063_REG_SEQ, DA9063_REG_SEQ),
140 regmap_reg_range(DA9063_REG_EN_32K, DA9063_REG_EN_32K),
141 regmap_reg_range(DA9063_BB_REG_MON_REG_5, DA9063_BB_REG_MON_REG_6),
142};
143
144static const struct regmap_access_table da9063l_bb_readable_table = {
145 .yes_ranges = da9063l_bb_readable_ranges,
146 .n_yes_ranges = ARRAY_SIZE(da9063l_bb_readable_ranges),
147};
148
149static const struct regmap_access_table da9063l_bb_writeable_table = {
150 .yes_ranges = da9063l_bb_writeable_ranges,
151 .n_yes_ranges = ARRAY_SIZE(da9063l_bb_writeable_ranges),
152};
153
154static const struct regmap_access_table da9063l_bb_volatile_table = {
155 .yes_ranges = da9063l_bb_volatile_ranges,
156 .n_yes_ranges = ARRAY_SIZE(da9063l_bb_volatile_ranges),
157};
158
Krystian Garbaciak8e685482013-07-29 19:00:43 +0200159static const struct regmap_range_cfg da9063_range_cfg[] = {
160 {
161 .range_min = DA9063_REG_PAGE_CON,
162 .range_max = DA9063_REG_CHIP_VARIANT,
163 .selector_reg = DA9063_REG_PAGE_CON,
164 .selector_mask = 1 << DA9063_I2C_PAGE_SEL_SHIFT,
165 .selector_shift = DA9063_I2C_PAGE_SEL_SHIFT,
166 .window_start = 0,
167 .window_len = 256,
168 }
169};
170
171static struct regmap_config da9063_regmap_config = {
172 .reg_bits = 8,
173 .val_bits = 8,
174 .ranges = da9063_range_cfg,
175 .num_ranges = ARRAY_SIZE(da9063_range_cfg),
176 .max_register = DA9063_REG_CHIP_VARIANT,
177
178 .cache_type = REGCACHE_RBTREE,
Krystian Garbaciak8e685482013-07-29 19:00:43 +0200179};
180
Steve Twiss71e03de2015-01-20 13:54:25 +0000181static const struct of_device_id da9063_dt_ids[] = {
182 { .compatible = "dlg,da9063", },
Marek Vasutc2875722018-06-11 13:58:51 +0200183 { .compatible = "dlg,da9063l", },
Steve Twiss71e03de2015-01-20 13:54:25 +0000184 { }
185};
186MODULE_DEVICE_TABLE(of, da9063_dt_ids);
Krystian Garbaciak8e685482013-07-29 19:00:43 +0200187static int da9063_i2c_probe(struct i2c_client *i2c,
Marek Vasut492510d2018-06-11 13:58:45 +0200188 const struct i2c_device_id *id)
Krystian Garbaciak8e685482013-07-29 19:00:43 +0200189{
190 struct da9063 *da9063;
191 int ret;
192
193 da9063 = devm_kzalloc(&i2c->dev, sizeof(struct da9063), GFP_KERNEL);
194 if (da9063 == NULL)
195 return -ENOMEM;
196
197 i2c_set_clientdata(i2c, da9063);
198 da9063->dev = &i2c->dev;
199 da9063->chip_irq = i2c->irq;
Marek Vasut492510d2018-06-11 13:58:45 +0200200 da9063->type = id->driver_data;
Krystian Garbaciak8e685482013-07-29 19:00:43 +0200201
Opensource [Steve Twiss]9cb42e22014-07-21 11:39:33 +0100202 if (da9063->variant_code == PMIC_DA9063_AD) {
203 da9063_regmap_config.rd_table = &da9063_ad_readable_table;
204 da9063_regmap_config.wr_table = &da9063_ad_writeable_table;
205 da9063_regmap_config.volatile_table = &da9063_ad_volatile_table;
Marek Vasut4bd6ad02018-06-11 13:58:47 +0200206 } else if (da9063->type == PMIC_TYPE_DA9063L) {
207 da9063_regmap_config.rd_table = &da9063l_bb_readable_table;
208 da9063_regmap_config.wr_table = &da9063l_bb_writeable_table;
209 da9063_regmap_config.volatile_table = &da9063l_bb_volatile_table;
Opensource [Steve Twiss]9cb42e22014-07-21 11:39:33 +0100210 } else {
211 da9063_regmap_config.rd_table = &da9063_bb_readable_table;
212 da9063_regmap_config.wr_table = &da9063_bb_writeable_table;
213 da9063_regmap_config.volatile_table = &da9063_bb_volatile_table;
214 }
215
Krystian Garbaciak8e685482013-07-29 19:00:43 +0200216 da9063->regmap = devm_regmap_init_i2c(i2c, &da9063_regmap_config);
217 if (IS_ERR(da9063->regmap)) {
218 ret = PTR_ERR(da9063->regmap);
219 dev_err(da9063->dev, "Failed to allocate register map: %d\n",
220 ret);
221 return ret;
222 }
223
224 return da9063_device_init(da9063, i2c->irq);
225}
226
Krystian Garbaciak8e685482013-07-29 19:00:43 +0200227static const struct i2c_device_id da9063_i2c_id[] = {
Marek Vasut492510d2018-06-11 13:58:45 +0200228 { "da9063", PMIC_TYPE_DA9063 },
Marek Vasutc2875722018-06-11 13:58:51 +0200229 { "da9063l", PMIC_TYPE_DA9063L },
Krystian Garbaciak8e685482013-07-29 19:00:43 +0200230 {},
231};
232MODULE_DEVICE_TABLE(i2c, da9063_i2c_id);
233
234static struct i2c_driver da9063_i2c_driver = {
235 .driver = {
236 .name = "da9063",
Steve Twiss71e03de2015-01-20 13:54:25 +0000237 .of_match_table = of_match_ptr(da9063_dt_ids),
Krystian Garbaciak8e685482013-07-29 19:00:43 +0200238 },
239 .probe = da9063_i2c_probe,
Krystian Garbaciak8e685482013-07-29 19:00:43 +0200240 .id_table = da9063_i2c_id,
241};
242
243module_i2c_driver(da9063_i2c_driver);