blob: 71aa14a6bfbbb4ffd061aeb91df739fc70684446 [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
21#include <linux/module.h>
22#include <linux/interrupt.h>
23#include <linux/mfd/core.h>
24#include <linux/mfd/max14577.h>
25#include <linux/mfd/max14577-private.h>
26
27static struct mfd_cell max14577_devs[] = {
28 { .name = "max14577-muic", },
Krzysztof Kozlowski41096802013-11-27 15:16:17 +010029 {
30 .name = "max14577-regulator",
31 .of_compatible = "maxim,max14577-regulator",
32 },
Chanwoo Choi3008ddb2013-11-22 16:51:05 +010033 { .name = "max14577-charger", },
34};
35
36static bool max14577_volatile_reg(struct device *dev, unsigned int reg)
37{
38 switch (reg) {
39 case MAX14577_REG_INT1 ... MAX14577_REG_STATUS3:
40 return true;
41 default:
42 break;
43 }
44 return false;
45}
46
47static const struct regmap_config max14577_regmap_config = {
48 .reg_bits = 8,
49 .val_bits = 8,
50 .volatile_reg = max14577_volatile_reg,
51 .max_register = MAX14577_REG_END,
52};
53
54static const struct regmap_irq max14577_irqs[] = {
55 /* INT1 interrupts */
56 { .reg_offset = 0, .mask = INT1_ADC_MASK, },
57 { .reg_offset = 0, .mask = INT1_ADCLOW_MASK, },
58 { .reg_offset = 0, .mask = INT1_ADCERR_MASK, },
59 /* INT2 interrupts */
60 { .reg_offset = 1, .mask = INT2_CHGTYP_MASK, },
61 { .reg_offset = 1, .mask = INT2_CHGDETRUN_MASK, },
62 { .reg_offset = 1, .mask = INT2_DCDTMR_MASK, },
63 { .reg_offset = 1, .mask = INT2_DBCHG_MASK, },
64 { .reg_offset = 1, .mask = INT2_VBVOLT_MASK, },
65 /* INT3 interrupts */
66 { .reg_offset = 2, .mask = INT3_EOC_MASK, },
67 { .reg_offset = 2, .mask = INT3_CGMBC_MASK, },
68 { .reg_offset = 2, .mask = INT3_OVP_MASK, },
69 { .reg_offset = 2, .mask = INT3_MBCCHGERR_MASK, },
70};
71
72static const struct regmap_irq_chip max14577_irq_chip = {
73 .name = "max14577",
74 .status_base = MAX14577_REG_INT1,
75 .mask_base = MAX14577_REG_INTMASK1,
76 .mask_invert = 1,
77 .num_regs = 3,
78 .irqs = max14577_irqs,
79 .num_irqs = ARRAY_SIZE(max14577_irqs),
80};
81
82static int max14577_i2c_probe(struct i2c_client *i2c,
83 const struct i2c_device_id *id)
84{
85 struct max14577 *max14577;
86 struct max14577_platform_data *pdata = dev_get_platdata(&i2c->dev);
87 struct device_node *np = i2c->dev.of_node;
88 u8 reg_data;
89 int ret = 0;
90
91 if (np) {
92 pdata = devm_kzalloc(&i2c->dev, sizeof(*pdata), GFP_KERNEL);
93 if (!pdata)
94 return -ENOMEM;
95 i2c->dev.platform_data = pdata;
96 }
97
98 if (!pdata) {
Dan Carpenteraab5dc62013-12-04 17:07:12 +030099 dev_err(&i2c->dev, "No platform data found.\n");
Chanwoo Choi3008ddb2013-11-22 16:51:05 +0100100 return -EINVAL;
101 }
102
103 max14577 = devm_kzalloc(&i2c->dev, sizeof(*max14577), GFP_KERNEL);
104 if (!max14577)
105 return -ENOMEM;
106
107 i2c_set_clientdata(i2c, max14577);
108 max14577->dev = &i2c->dev;
109 max14577->i2c = i2c;
110 max14577->irq = i2c->irq;
111
112 max14577->regmap = devm_regmap_init_i2c(i2c, &max14577_regmap_config);
113 if (IS_ERR(max14577->regmap)) {
114 ret = PTR_ERR(max14577->regmap);
115 dev_err(max14577->dev, "Failed to allocate register map: %d\n",
116 ret);
117 return ret;
118 }
119
120 ret = max14577_read_reg(max14577->regmap, MAX14577_REG_DEVICEID,
121 &reg_data);
122 if (ret) {
123 dev_err(max14577->dev, "Device not found on this channel: %d\n",
124 ret);
125 return ret;
126 }
127 max14577->vendor_id = ((reg_data & DEVID_VENDORID_MASK) >>
128 DEVID_VENDORID_SHIFT);
129 max14577->device_id = ((reg_data & DEVID_DEVICEID_MASK) >>
130 DEVID_DEVICEID_SHIFT);
131 dev_info(max14577->dev, "Device ID: 0x%x, vendor: 0x%x\n",
132 max14577->device_id, max14577->vendor_id);
133
134 ret = regmap_add_irq_chip(max14577->regmap, max14577->irq,
135 IRQF_TRIGGER_FALLING | IRQF_ONESHOT, 0,
136 &max14577_irq_chip,
137 &max14577->irq_data);
138 if (ret != 0) {
139 dev_err(&i2c->dev, "Failed to request IRQ %d: %d\n",
140 max14577->irq, ret);
141 return ret;
142 }
143
144 ret = mfd_add_devices(max14577->dev, -1, max14577_devs,
145 ARRAY_SIZE(max14577_devs), NULL, 0,
146 regmap_irq_get_domain(max14577->irq_data));
147 if (ret < 0)
148 goto err_mfd;
149
150 device_init_wakeup(max14577->dev, 1);
151
152 return 0;
153
154err_mfd:
155 regmap_del_irq_chip(max14577->irq, max14577->irq_data);
156
157 return ret;
158}
159
160static int max14577_i2c_remove(struct i2c_client *i2c)
161{
162 struct max14577 *max14577 = i2c_get_clientdata(i2c);
163
164 mfd_remove_devices(max14577->dev);
165 regmap_del_irq_chip(max14577->irq, max14577->irq_data);
166
167 return 0;
168}
169
170static const struct i2c_device_id max14577_i2c_id[] = {
171 { "max14577", 0 },
172 { }
173};
174MODULE_DEVICE_TABLE(i2c, max14577_i2c_id);
175
Geert Uytterhoeven3edeb1e2014-01-26 11:38:42 +0100176#ifdef CONFIG_PM_SLEEP
Chanwoo Choi3008ddb2013-11-22 16:51:05 +0100177static int max14577_suspend(struct device *dev)
178{
179 struct i2c_client *i2c = container_of(dev, struct i2c_client, dev);
180 struct max14577 *max14577 = i2c_get_clientdata(i2c);
181
182 if (device_may_wakeup(dev)) {
183 enable_irq_wake(max14577->irq);
184 /*
185 * MUIC IRQ must be disabled during suspend if this is
186 * a wake up source because it will be handled before
187 * resuming I2C.
188 *
189 * When device is woken up from suspend (e.g. by ADC change),
190 * an interrupt occurs before resuming I2C bus controller.
191 * Interrupt handler tries to read registers but this read
192 * will fail because I2C is still suspended.
193 */
194 disable_irq(max14577->irq);
195 }
196
197 return 0;
198}
199
200static int max14577_resume(struct device *dev)
201{
202 struct i2c_client *i2c = container_of(dev, struct i2c_client, dev);
203 struct max14577 *max14577 = i2c_get_clientdata(i2c);
204
205 if (device_may_wakeup(dev)) {
206 disable_irq_wake(max14577->irq);
207 enable_irq(max14577->irq);
208 }
209
210 return 0;
211}
Geert Uytterhoeven3edeb1e2014-01-26 11:38:42 +0100212#endif /* CONFIG_PM_SLEEP */
Chanwoo Choi3008ddb2013-11-22 16:51:05 +0100213
214static struct of_device_id max14577_dt_match[] = {
215 { .compatible = "maxim,max14577", },
216 {},
217};
218
219static SIMPLE_DEV_PM_OPS(max14577_pm, max14577_suspend, max14577_resume);
220
221static struct i2c_driver max14577_i2c_driver = {
222 .driver = {
223 .name = "max14577",
224 .owner = THIS_MODULE,
225 .pm = &max14577_pm,
Sachin Kamatae679c12013-12-21 15:50:16 +0530226 .of_match_table = max14577_dt_match,
Chanwoo Choi3008ddb2013-11-22 16:51:05 +0100227 },
228 .probe = max14577_i2c_probe,
229 .remove = max14577_i2c_remove,
230 .id_table = max14577_i2c_id,
231};
232
233static int __init max14577_i2c_init(void)
234{
235 return i2c_add_driver(&max14577_i2c_driver);
236}
237subsys_initcall(max14577_i2c_init);
238
239static void __exit max14577_i2c_exit(void)
240{
241 i2c_del_driver(&max14577_i2c_driver);
242}
243module_exit(max14577_i2c_exit);
244
245MODULE_AUTHOR("Chanwoo Choi <cw00.choi@samsung.com>, Krzysztof Kozlowski <k.kozlowski@samsung.com>");
246MODULE_DESCRIPTION("MAXIM 14577 multi-function core driver");
247MODULE_LICENSE("GPL");