blob: 6f39dec9dfdf83c9fa959c587eddcfe869bb0050 [file] [log] [blame]
Chanwoo Choi3008ddb2013-11-22 16:51:05 +01001/*
2 * max14577.c - mfd core driver for the Maxim 14577
3 *
4 * Copyright (C) 2013 Samsung Electrnoics
5 * Chanwoo Choi <cw00.choi@samsung.com>
6 * Krzysztof Kozlowski <k.kozlowski@samsung.com>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * This driver is based on max8997.c
19 */
20
Sachin Kamatc8016d42014-02-12 14:40:11 +053021#include <linux/err.h>
Chanwoo Choi3008ddb2013-11-22 16:51:05 +010022#include <linux/module.h>
23#include <linux/interrupt.h>
Krzysztof Kozlowskieccb80c2014-04-14 11:17:14 +020024#include <linux/of_device.h>
Chanwoo Choi3008ddb2013-11-22 16:51:05 +010025#include <linux/mfd/core.h>
26#include <linux/mfd/max14577.h>
27#include <linux/mfd/max14577-private.h>
28
29static struct mfd_cell max14577_devs[] = {
Krzysztof Kozlowskia0b0ea42014-01-28 13:18:28 +010030 {
31 .name = "max14577-muic",
32 .of_compatible = "maxim,max14577-muic",
33 },
Krzysztof Kozlowski41096802013-11-27 15:16:17 +010034 {
35 .name = "max14577-regulator",
36 .of_compatible = "maxim,max14577-regulator",
37 },
Chanwoo Choi3008ddb2013-11-22 16:51:05 +010038 { .name = "max14577-charger", },
39};
40
Krzysztof Kozlowskieccb80c2014-04-14 11:17:14 +020041static struct of_device_id max14577_dt_match[] = {
42 {
43 .compatible = "maxim,max14577",
44 .data = (void *)MAXIM_DEVICE_TYPE_MAX14577,
45 },
46 {},
47};
48
Krzysztof Kozlowski575343d2014-04-14 11:17:13 +020049static bool max14577_muic_volatile_reg(struct device *dev, unsigned int reg)
Chanwoo Choi3008ddb2013-11-22 16:51:05 +010050{
51 switch (reg) {
52 case MAX14577_REG_INT1 ... MAX14577_REG_STATUS3:
53 return true;
54 default:
55 break;
56 }
57 return false;
58}
59
Krzysztof Kozlowski575343d2014-04-14 11:17:13 +020060static const struct regmap_config max14577_muic_regmap_config = {
Chanwoo Choi3008ddb2013-11-22 16:51:05 +010061 .reg_bits = 8,
62 .val_bits = 8,
Krzysztof Kozlowski575343d2014-04-14 11:17:13 +020063 .volatile_reg = max14577_muic_volatile_reg,
Chanwoo Choi3008ddb2013-11-22 16:51:05 +010064 .max_register = MAX14577_REG_END,
65};
66
67static const struct regmap_irq max14577_irqs[] = {
68 /* INT1 interrupts */
Krzysztof Kozlowskic7846852014-04-14 11:17:17 +020069 { .reg_offset = 0, .mask = MAX14577_INT1_ADC_MASK, },
70 { .reg_offset = 0, .mask = MAX14577_INT1_ADCLOW_MASK, },
71 { .reg_offset = 0, .mask = MAX14577_INT1_ADCERR_MASK, },
Chanwoo Choi3008ddb2013-11-22 16:51:05 +010072 /* INT2 interrupts */
Krzysztof Kozlowskic7846852014-04-14 11:17:17 +020073 { .reg_offset = 1, .mask = MAX14577_INT2_CHGTYP_MASK, },
74 { .reg_offset = 1, .mask = MAX14577_INT2_CHGDETRUN_MASK, },
75 { .reg_offset = 1, .mask = MAX14577_INT2_DCDTMR_MASK, },
76 { .reg_offset = 1, .mask = MAX14577_INT2_DBCHG_MASK, },
77 { .reg_offset = 1, .mask = MAX14577_INT2_VBVOLT_MASK, },
Chanwoo Choi3008ddb2013-11-22 16:51:05 +010078 /* INT3 interrupts */
Krzysztof Kozlowskic7846852014-04-14 11:17:17 +020079 { .reg_offset = 2, .mask = MAX14577_INT3_EOC_MASK, },
80 { .reg_offset = 2, .mask = MAX14577_INT3_CGMBC_MASK, },
81 { .reg_offset = 2, .mask = MAX14577_INT3_OVP_MASK, },
82 { .reg_offset = 2, .mask = MAX14577_INT3_MBCCHGERR_MASK, },
Chanwoo Choi3008ddb2013-11-22 16:51:05 +010083};
84
85static const struct regmap_irq_chip max14577_irq_chip = {
86 .name = "max14577",
87 .status_base = MAX14577_REG_INT1,
88 .mask_base = MAX14577_REG_INTMASK1,
89 .mask_invert = 1,
90 .num_regs = 3,
91 .irqs = max14577_irqs,
92 .num_irqs = ARRAY_SIZE(max14577_irqs),
93};
94
Krzysztof Kozlowskieccb80c2014-04-14 11:17:14 +020095static void max14577_print_dev_type(struct max14577 *max14577)
96{
97 u8 reg_data, vendor_id, device_id;
98 int ret;
99
100 ret = max14577_read_reg(max14577->regmap, MAX14577_REG_DEVICEID,
101 &reg_data);
102 if (ret) {
103 dev_err(max14577->dev,
104 "Failed to read DEVICEID register: %d\n", ret);
105 return;
106 }
107
108 vendor_id = ((reg_data & DEVID_VENDORID_MASK) >>
109 DEVID_VENDORID_SHIFT);
110 device_id = ((reg_data & DEVID_DEVICEID_MASK) >>
111 DEVID_DEVICEID_SHIFT);
112
113 dev_info(max14577->dev, "Device type: %u (ID: 0x%x, vendor: 0x%x)\n",
114 max14577->dev_type, device_id, vendor_id);
115}
116
Chanwoo Choi3008ddb2013-11-22 16:51:05 +0100117static int max14577_i2c_probe(struct i2c_client *i2c,
118 const struct i2c_device_id *id)
119{
120 struct max14577 *max14577;
121 struct max14577_platform_data *pdata = dev_get_platdata(&i2c->dev);
122 struct device_node *np = i2c->dev.of_node;
Chanwoo Choi3008ddb2013-11-22 16:51:05 +0100123 int ret = 0;
124
125 if (np) {
126 pdata = devm_kzalloc(&i2c->dev, sizeof(*pdata), GFP_KERNEL);
127 if (!pdata)
128 return -ENOMEM;
129 i2c->dev.platform_data = pdata;
130 }
131
132 if (!pdata) {
Dan Carpenteraab5dc62013-12-04 17:07:12 +0300133 dev_err(&i2c->dev, "No platform data found.\n");
Chanwoo Choi3008ddb2013-11-22 16:51:05 +0100134 return -EINVAL;
135 }
136
137 max14577 = devm_kzalloc(&i2c->dev, sizeof(*max14577), GFP_KERNEL);
138 if (!max14577)
139 return -ENOMEM;
140
141 i2c_set_clientdata(i2c, max14577);
142 max14577->dev = &i2c->dev;
143 max14577->i2c = i2c;
144 max14577->irq = i2c->irq;
145
Krzysztof Kozlowski575343d2014-04-14 11:17:13 +0200146 max14577->regmap = devm_regmap_init_i2c(i2c,
147 &max14577_muic_regmap_config);
Chanwoo Choi3008ddb2013-11-22 16:51:05 +0100148 if (IS_ERR(max14577->regmap)) {
149 ret = PTR_ERR(max14577->regmap);
150 dev_err(max14577->dev, "Failed to allocate register map: %d\n",
151 ret);
152 return ret;
153 }
154
Krzysztof Kozlowskieccb80c2014-04-14 11:17:14 +0200155 if (np) {
156 const struct of_device_id *of_id;
157
158 of_id = of_match_device(max14577_dt_match, &i2c->dev);
159 if (of_id)
160 max14577->dev_type = (unsigned int)of_id->data;
161 } else {
162 max14577->dev_type = id->driver_data;
Chanwoo Choi3008ddb2013-11-22 16:51:05 +0100163 }
Krzysztof Kozlowskieccb80c2014-04-14 11:17:14 +0200164
165 max14577_print_dev_type(max14577);
Chanwoo Choi3008ddb2013-11-22 16:51:05 +0100166
167 ret = regmap_add_irq_chip(max14577->regmap, max14577->irq,
168 IRQF_TRIGGER_FALLING | IRQF_ONESHOT, 0,
169 &max14577_irq_chip,
170 &max14577->irq_data);
171 if (ret != 0) {
172 dev_err(&i2c->dev, "Failed to request IRQ %d: %d\n",
173 max14577->irq, ret);
174 return ret;
175 }
176
177 ret = mfd_add_devices(max14577->dev, -1, max14577_devs,
178 ARRAY_SIZE(max14577_devs), NULL, 0,
179 regmap_irq_get_domain(max14577->irq_data));
180 if (ret < 0)
181 goto err_mfd;
182
183 device_init_wakeup(max14577->dev, 1);
184
185 return 0;
186
187err_mfd:
188 regmap_del_irq_chip(max14577->irq, max14577->irq_data);
189
190 return ret;
191}
192
193static int max14577_i2c_remove(struct i2c_client *i2c)
194{
195 struct max14577 *max14577 = i2c_get_clientdata(i2c);
196
197 mfd_remove_devices(max14577->dev);
198 regmap_del_irq_chip(max14577->irq, max14577->irq_data);
199
200 return 0;
201}
202
203static const struct i2c_device_id max14577_i2c_id[] = {
Krzysztof Kozlowskieccb80c2014-04-14 11:17:14 +0200204 { "max14577", MAXIM_DEVICE_TYPE_MAX14577, },
Chanwoo Choi3008ddb2013-11-22 16:51:05 +0100205 { }
206};
207MODULE_DEVICE_TABLE(i2c, max14577_i2c_id);
208
Geert Uytterhoeven3edeb1e2014-01-26 11:38:42 +0100209#ifdef CONFIG_PM_SLEEP
Chanwoo Choi3008ddb2013-11-22 16:51:05 +0100210static int max14577_suspend(struct device *dev)
211{
212 struct i2c_client *i2c = container_of(dev, struct i2c_client, dev);
213 struct max14577 *max14577 = i2c_get_clientdata(i2c);
214
215 if (device_may_wakeup(dev)) {
216 enable_irq_wake(max14577->irq);
217 /*
218 * MUIC IRQ must be disabled during suspend if this is
219 * a wake up source because it will be handled before
220 * resuming I2C.
221 *
222 * When device is woken up from suspend (e.g. by ADC change),
223 * an interrupt occurs before resuming I2C bus controller.
224 * Interrupt handler tries to read registers but this read
225 * will fail because I2C is still suspended.
226 */
227 disable_irq(max14577->irq);
228 }
229
230 return 0;
231}
232
233static int max14577_resume(struct device *dev)
234{
235 struct i2c_client *i2c = container_of(dev, struct i2c_client, dev);
236 struct max14577 *max14577 = i2c_get_clientdata(i2c);
237
238 if (device_may_wakeup(dev)) {
239 disable_irq_wake(max14577->irq);
240 enable_irq(max14577->irq);
241 }
242
243 return 0;
244}
Geert Uytterhoeven3edeb1e2014-01-26 11:38:42 +0100245#endif /* CONFIG_PM_SLEEP */
Chanwoo Choi3008ddb2013-11-22 16:51:05 +0100246
Chanwoo Choi3008ddb2013-11-22 16:51:05 +0100247static SIMPLE_DEV_PM_OPS(max14577_pm, max14577_suspend, max14577_resume);
248
249static struct i2c_driver max14577_i2c_driver = {
250 .driver = {
251 .name = "max14577",
252 .owner = THIS_MODULE,
253 .pm = &max14577_pm,
Sachin Kamatae679c12013-12-21 15:50:16 +0530254 .of_match_table = max14577_dt_match,
Chanwoo Choi3008ddb2013-11-22 16:51:05 +0100255 },
256 .probe = max14577_i2c_probe,
257 .remove = max14577_i2c_remove,
258 .id_table = max14577_i2c_id,
259};
260
261static int __init max14577_i2c_init(void)
262{
Krzysztof Kozlowskieccb80c2014-04-14 11:17:14 +0200263 BUILD_BUG_ON(ARRAY_SIZE(max14577_i2c_id) != MAXIM_DEVICE_TYPE_NUM);
264 BUILD_BUG_ON(ARRAY_SIZE(max14577_dt_match) != MAXIM_DEVICE_TYPE_NUM);
265
Chanwoo Choi3008ddb2013-11-22 16:51:05 +0100266 return i2c_add_driver(&max14577_i2c_driver);
267}
268subsys_initcall(max14577_i2c_init);
269
270static void __exit max14577_i2c_exit(void)
271{
272 i2c_del_driver(&max14577_i2c_driver);
273}
274module_exit(max14577_i2c_exit);
275
276MODULE_AUTHOR("Chanwoo Choi <cw00.choi@samsung.com>, Krzysztof Kozlowski <k.kozlowski@samsung.com>");
277MODULE_DESCRIPTION("MAXIM 14577 multi-function core driver");
278MODULE_LICENSE("GPL");