summaryrefslogtreecommitdiffstats
path: root/drivers/mfd/max8907.c
blob: 8bbe7979db914537bfcd5d5ac3f8e362125a02e8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
// SPDX-License-Identifier: GPL-2.0-only
/*
 * max8907.c - mfd driver for MAX8907
 *
 * Copyright (C) 2010 Gyungoh Yoo <jack.yoo@maxim-ic.com>
 * Copyright (C) 2010-2012, NVIDIA CORPORATION. All rights reserved.
 */

#include <linux/err.h>
#include <linux/i2c.h>
#include <linux/init.h>
#include <linux/interrupt.h>
#include <linux/irq.h>
#include <linux/mfd/core.h>
#include <linux/mfd/max8907.h>
#include <linux/module.h>
#include <linux/of.h>
#include <linux/regmap.h>
#include <linux/slab.h>

static const struct mfd_cell max8907_cells[] = {
	{ .name = "max8907-regulator", },
	{ .name = "max8907-rtc", },
};

static bool max8907_gen_is_volatile_reg(struct device *dev, unsigned int reg)
{
	switch (reg) {
	case MAX8907_REG_ON_OFF_IRQ1:
	case MAX8907_REG_ON_OFF_STAT:
	case MAX8907_REG_ON_OFF_IRQ2:
	case MAX8907_REG_CHG_IRQ1:
	case MAX8907_REG_CHG_IRQ2:
	case MAX8907_REG_CHG_STAT:
		return true;
	default:
		return false;
	}
}

static bool max8907_gen_is_precious_reg(struct device *dev, unsigned int reg)
{
	switch (reg) {
	case MAX8907_REG_ON_OFF_IRQ1:
	case MAX8907_REG_ON_OFF_IRQ2:
	case MAX8907_REG_CHG_IRQ1:
	case MAX8907_REG_CHG_IRQ2:
		return true;
	default:
		return false;
	}
}

static bool max8907_gen_is_writeable_reg(struct device *dev, unsigned int reg)
{
	return !max8907_gen_is_volatile_reg(dev, reg);
}

static const struct regmap_config max8907_regmap_gen_config = {
	.reg_bits = 8,
	.val_bits = 8,
	.volatile_reg = max8907_gen_is_volatile_reg,
	.precious_reg = max8907_gen_is_precious_reg,
	.writeable_reg = max8907_gen_is_writeable_reg,
	.max_register = MAX8907_REG_LDO20VOUT,
	.cache_type = REGCACHE_RBTREE,
};

static bool max8907_rtc_is_volatile_reg(struct device *dev, unsigned int reg)
{
	if (reg <= MAX8907_REG_RTC_YEAR2)
		return true;

	switch (reg) {
	case MAX8907_REG_RTC_STATUS:
	case MAX8907_REG_RTC_IRQ:
		return true;
	default:
		return false;
	}
}

static bool max8907_rtc_is_precious_reg(struct device *dev, unsigned int reg)
{
	switch (reg) {
	case MAX8907_REG_RTC_IRQ:
		return true;
	default:
		return false;
	}
}

static bool max8907_rtc_is_writeable_reg(struct device *dev, unsigned int reg)
{
	switch (reg) {
	case MAX8907_REG_RTC_STATUS:
	case MAX8907_REG_RTC_IRQ:
		return false;
	default:
		return true;
	}
}

static const struct regmap_config max8907_regmap_rtc_config = {
	.reg_bits = 8,
	.val_bits = 8,
	.volatile_reg = max8907_rtc_is_volatile_reg,
	.precious_reg = max8907_rtc_is_precious_reg,
	.writeable_reg = max8907_rtc_is_writeable_reg,
	.max_register = MAX8907_REG_MPL_CNTL,
	.cache_type = REGCACHE_RBTREE,
};

static const struct regmap_irq max8907_chg_irqs[] = {
	{ .reg_offset = 0, .mask = 1 << 0, },
	{ .reg_offset = 0, .mask = 1 << 1, },
	{ .reg_offset = 0, .mask = 1 << 2, },
	{ .reg_offset = 1, .mask = 1 << 0, },
	{ .reg_offset = 1, .mask = 1 << 1, },
	{ .reg_offset = 1, .mask = 1 << 2, },
	{ .reg_offset = 1, .mask = 1 << 3, },
	{ .reg_offset = 1, .mask = 1 << 4, },
	{ .reg_offset = 1, .mask = 1 << 5, },
	{ .reg_offset = 1, .mask = 1 << 6, },
	{ .reg_offset = 1, .mask = 1 << 7, },
};

static const struct regmap_irq_chip max8907_chg_irq_chip = {
	.name = "max8907 chg",
	.status_base = MAX8907_REG_CHG_IRQ1,
	.mask_base = MAX8907_REG_CHG_IRQ1_MASK,
	.wake_base = MAX8907_REG_CHG_IRQ1_MASK,
	.irq_reg_stride = MAX8907_REG_CHG_IRQ2 - MAX8907_REG_CHG_IRQ1,
	.num_regs = 2,
	.irqs = max8907_chg_irqs,
	.num_irqs = ARRAY_SIZE(max8907_chg_irqs),
};

static const struct regmap_irq max8907_on_off_irqs[] = {
	{ .reg_offset = 0, .mask = 1 << 0, },
	{ .reg_offset = 0, .mask = 1 << 1, },
	{ .reg_offset = 0, .mask = 1 << 2, },
	{ .reg_offset = 0, .mask = 1 << 3, },
	{ .reg_offset = 0, .mask = 1 << 4, },
	{ .reg_offset = 0, .mask = 1 << 5, },
	{ .reg_offset = 0, .mask = 1 << 6, },
	{ .reg_offset = 0, .mask = 1 << 7, },
	{ .reg_offset = 1, .mask = 1 << 0, },
	{ .reg_offset = 1, .mask = 1 << 1, },
};

static const struct regmap_irq_chip max8907_on_off_irq_chip = {
	.name = "max8907 on_off",
	.status_base = MAX8907_REG_ON_OFF_IRQ1,
	.mask_base = MAX8907_REG_ON_OFF_IRQ1_MASK,
	.irq_reg_stride = MAX8907_REG_ON_OFF_IRQ2 - MAX8907_REG_ON_OFF_IRQ1,
	.num_regs = 2,
	.irqs = max8907_on_off_irqs,
	.num_irqs = ARRAY_SIZE(max8907_on_off_irqs),
};

static const struct regmap_irq max8907_rtc_irqs[] = {
	{ .reg_offset = 0, .mask = 1 << 2, },
	{ .reg_offset = 0, .mask = 1 << 3, },
};

static const struct regmap_irq_chip max8907_rtc_irq_chip = {
	.name = "max8907 rtc",
	.status_base = MAX8907_REG_RTC_IRQ,
	.mask_base = MAX8907_REG_RTC_IRQ_MASK,
	.num_regs = 1,
	.irqs = max8907_rtc_irqs,
	.num_irqs = ARRAY_SIZE(max8907_rtc_irqs),
};

static struct max8907 *max8907_pm_off;
static void max8907_power_off(void)
{
	regmap_update_bits(max8907_pm_off->regmap_gen, MAX8907_REG_RESET_CNFG,
			MAX8907_MASK_POWER_OFF, MAX8907_MASK_POWER_OFF);
}

static int max8907_i2c_probe(struct i2c_client *i2c)
{
	struct max8907 *max8907;
	int ret;
	struct max8907_platform_data *pdata = dev_get_platdata(&i2c->dev);
	bool pm_off = false;

	if (pdata)
		pm_off = pdata->pm_off;
	else if (i2c->dev.of_node)
		pm_off = of_property_read_bool(i2c->dev.of_node,
					"maxim,system-power-controller");

	max8907 = devm_kzalloc(&i2c->dev, sizeof(struct max8907), GFP_KERNEL);
	if (!max8907) {
		ret = -ENOMEM;
		goto err_alloc_drvdata;
	}

	max8907->dev = &i2c->dev;
	max8907->i2c_gen = i2c;
	i2c_set_clientdata(i2c, max8907);
	max8907->regmap_gen = devm_regmap_init_i2c(i2c,
						&max8907_regmap_gen_config);
	if (IS_ERR(max8907->regmap_gen)) {
		ret = PTR_ERR(max8907->regmap_gen);
		dev_err(&i2c->dev, "gen regmap init failed: %d\n", ret);
		goto err_regmap_gen;
	}

	max8907->i2c_rtc = i2c_new_dummy_device(i2c->adapter, MAX8907_RTC_I2C_ADDR);
	if (IS_ERR(max8907->i2c_rtc)) {
		ret = PTR_ERR(max8907->i2c_rtc);
		goto err_dummy_rtc;
	}
	i2c_set_clientdata(max8907->i2c_rtc, max8907);
	max8907->regmap_rtc = devm_regmap_init_i2c(max8907->i2c_rtc,
						&max8907_regmap_rtc_config);
	if (IS_ERR(max8907->regmap_rtc)) {
		ret = PTR_ERR(max8907->regmap_rtc);
		dev_err(&i2c->dev, "rtc regmap init failed: %d\n", ret);
		goto err_regmap_rtc;
	}

	ret = regmap_add_irq_chip(max8907->regmap_gen, max8907->i2c_gen->irq,
				  IRQF_ONESHOT | IRQF_SHARED,
				  -1, &max8907_chg_irq_chip,
				  &max8907->irqc_chg);
	if (ret != 0) {
		dev_err(&i2c->dev, "failed to add chg irq chip: %d\n", ret);
		goto err_irqc_chg;
	}
	ret = regmap_add_irq_chip(max8907->regmap_gen, max8907->i2c_gen->irq,
				  IRQF_ONESHOT | IRQF_SHARED, -1,
				  &max8907_on_off_irq_chip,
				  &max8907->irqc_on_off);
	if (ret != 0) {
		dev_err(&i2c->dev, "failed to add on off irq chip: %d\n", ret);
		goto err_irqc_on_off;
	}
	ret = regmap_add_irq_chip(max8907->regmap_rtc, max8907->i2c_gen->irq,
				  IRQF_ONESHOT | IRQF_SHARED, -1,
				  &max8907_rtc_irq_chip,
				  &max8907->irqc_rtc);
	if (ret != 0) {
		dev_err(&i2c->dev, "failed to add rtc irq chip: %d\n", ret);
		goto err_irqc_rtc;
	}

	ret = mfd_add_devices(max8907->dev, -1, max8907_cells,
			      ARRAY_SIZE(max8907_cells), NULL, 0, NULL);
	if (ret != 0) {
		dev_err(&i2c->dev, "failed to add MFD devices %d\n", ret);
		goto err_add_devices;
	}

	if (pm_off && !pm_power_off) {
		max8907_pm_off = max8907;
		pm_power_off = max8907_power_off;
	}

	return 0;

err_add_devices:
	regmap_del_irq_chip(max8907->i2c_gen->irq, max8907->irqc_rtc);
err_irqc_rtc:
	regmap_del_irq_chip(max8907->i2c_gen->irq, max8907->irqc_on_off);
err_irqc_on_off:
	regmap_del_irq_chip(max8907->i2c_gen->irq, max8907->irqc_chg);
err_irqc_chg:
err_regmap_rtc:
	i2c_unregister_device(max8907->i2c_rtc);
err_dummy_rtc:
err_regmap_gen:
err_alloc_drvdata:
	return ret;
}

static void max8907_i2c_remove(struct i2c_client *i2c)
{
	struct max8907 *max8907 = i2c_get_clientdata(i2c);

	mfd_remove_devices(max8907->dev);

	regmap_del_irq_chip(max8907->i2c_gen->irq, max8907->irqc_rtc);
	regmap_del_irq_chip(max8907->i2c_gen->irq, max8907->irqc_on_off);
	regmap_del_irq_chip(max8907->i2c_gen->irq, max8907->irqc_chg);

	i2c_unregister_device(max8907->i2c_rtc);
}

#ifdef CONFIG_OF
static const struct of_device_id max8907_of_match[] = {
	{ .compatible = "maxim,max8907" },
	{ },
};
MODULE_DEVICE_TABLE(of, max8907_of_match);
#endif

static const struct i2c_device_id max8907_i2c_id[] = {
	{"max8907", 0},
	{}
};
MODULE_DEVICE_TABLE(i2c, max8907_i2c_id);

static struct i2c_driver max8907_i2c_driver = {
	.driver = {
		.name = "max8907",
		.of_match_table = of_match_ptr(max8907_of_match),
	},
	.probe = max8907_i2c_probe,
	.remove = max8907_i2c_remove,
	.id_table = max8907_i2c_id,
};

static int __init max8907_i2c_init(void)
{
	int ret = -ENODEV;

	ret = i2c_add_driver(&max8907_i2c_driver);
	if (ret != 0)
		pr_err("Failed to register I2C driver: %d\n", ret);

	return ret;
}
subsys_initcall(max8907_i2c_init);

static void __exit max8907_i2c_exit(void)
{
	i2c_del_driver(&max8907_i2c_driver);
}
module_exit(max8907_i2c_exit);

MODULE_DESCRIPTION("MAX8907 multi-function core driver");
MODULE_AUTHOR("Gyungoh Yoo <jack.yoo@maxim-ic.com>");
MODULE_LICENSE("GPL v2");