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