blob: 20e3b2d81bf0d2949117d4f89243b5b10adb6a0e [file] [log] [blame]
Chanwoo Choi3008ddb2013-11-22 16:51:05 +01001/*
Krzysztof Kozlowskiaee2a572014-04-14 11:17:18 +02002 * max14577.c - mfd core driver for the Maxim 14577/77836
Chanwoo Choi3008ddb2013-11-22 16:51:05 +01003 *
Krzysztof Kozlowskiaee2a572014-04-14 11:17:18 +02004 * Copyright (C) 2014 Samsung Electrnoics
Chanwoo Choi3008ddb2013-11-22 16:51:05 +01005 * 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 Kozlowskiaee2a572014-04-14 11:17:18 +020041static struct mfd_cell max77836_devs[] = {
42 {
43 .name = "max77836-muic",
44 .of_compatible = "maxim,max77836-muic",
45 },
46 {
47 .name = "max77836-regulator",
48 .of_compatible = "maxim,max77836-regulator",
49 },
50 {
51 .name = "max77836-charger",
52 .of_compatible = "maxim,max77836-charger",
53 },
54 {
55 .name = "max77836-battery",
56 .of_compatible = "maxim,max77836-battery",
57 },
58};
59
Krzysztof Kozlowskieccb80c2014-04-14 11:17:14 +020060static struct of_device_id max14577_dt_match[] = {
61 {
62 .compatible = "maxim,max14577",
63 .data = (void *)MAXIM_DEVICE_TYPE_MAX14577,
64 },
Krzysztof Kozlowskiaee2a572014-04-14 11:17:18 +020065 {
66 .compatible = "maxim,max77836",
67 .data = (void *)MAXIM_DEVICE_TYPE_MAX77836,
68 },
Krzysztof Kozlowskieccb80c2014-04-14 11:17:14 +020069 {},
70};
71
Krzysztof Kozlowski575343d2014-04-14 11:17:13 +020072static bool max14577_muic_volatile_reg(struct device *dev, unsigned int reg)
Chanwoo Choi3008ddb2013-11-22 16:51:05 +010073{
74 switch (reg) {
75 case MAX14577_REG_INT1 ... MAX14577_REG_STATUS3:
76 return true;
77 default:
78 break;
79 }
80 return false;
81}
82
Krzysztof Kozlowskiaee2a572014-04-14 11:17:18 +020083static bool max77836_muic_volatile_reg(struct device *dev, unsigned int reg)
84{
85 /* Any max14577 volatile registers are also max77836 volatile. */
86 if (max14577_muic_volatile_reg(dev, reg))
87 return true;
88
89 switch (reg) {
90 case MAX77836_FG_REG_VCELL_MSB ... MAX77836_FG_REG_SOC_LSB:
91 case MAX77836_FG_REG_CRATE_MSB ... MAX77836_FG_REG_CRATE_LSB:
92 case MAX77836_FG_REG_STATUS_H ... MAX77836_FG_REG_STATUS_L:
93 case MAX77836_PMIC_REG_INTSRC:
94 case MAX77836_PMIC_REG_TOPSYS_INT:
95 case MAX77836_PMIC_REG_TOPSYS_STAT:
96 return true;
97 default:
98 break;
99 }
100 return false;
101}
102
Krzysztof Kozlowski575343d2014-04-14 11:17:13 +0200103static const struct regmap_config max14577_muic_regmap_config = {
Chanwoo Choi3008ddb2013-11-22 16:51:05 +0100104 .reg_bits = 8,
105 .val_bits = 8,
Krzysztof Kozlowski575343d2014-04-14 11:17:13 +0200106 .volatile_reg = max14577_muic_volatile_reg,
Chanwoo Choi3008ddb2013-11-22 16:51:05 +0100107 .max_register = MAX14577_REG_END,
108};
109
Krzysztof Kozlowskiaee2a572014-04-14 11:17:18 +0200110static const struct regmap_config max77836_pmic_regmap_config = {
111 .reg_bits = 8,
112 .val_bits = 8,
113 .volatile_reg = max77836_muic_volatile_reg,
114 .max_register = MAX77836_PMIC_REG_END,
115};
116
Chanwoo Choi3008ddb2013-11-22 16:51:05 +0100117static const struct regmap_irq max14577_irqs[] = {
118 /* INT1 interrupts */
Krzysztof Kozlowskic7846852014-04-14 11:17:17 +0200119 { .reg_offset = 0, .mask = MAX14577_INT1_ADC_MASK, },
120 { .reg_offset = 0, .mask = MAX14577_INT1_ADCLOW_MASK, },
121 { .reg_offset = 0, .mask = MAX14577_INT1_ADCERR_MASK, },
Chanwoo Choi3008ddb2013-11-22 16:51:05 +0100122 /* INT2 interrupts */
Krzysztof Kozlowskic7846852014-04-14 11:17:17 +0200123 { .reg_offset = 1, .mask = MAX14577_INT2_CHGTYP_MASK, },
124 { .reg_offset = 1, .mask = MAX14577_INT2_CHGDETRUN_MASK, },
125 { .reg_offset = 1, .mask = MAX14577_INT2_DCDTMR_MASK, },
126 { .reg_offset = 1, .mask = MAX14577_INT2_DBCHG_MASK, },
127 { .reg_offset = 1, .mask = MAX14577_INT2_VBVOLT_MASK, },
Chanwoo Choi3008ddb2013-11-22 16:51:05 +0100128 /* INT3 interrupts */
Krzysztof Kozlowskic7846852014-04-14 11:17:17 +0200129 { .reg_offset = 2, .mask = MAX14577_INT3_EOC_MASK, },
130 { .reg_offset = 2, .mask = MAX14577_INT3_CGMBC_MASK, },
131 { .reg_offset = 2, .mask = MAX14577_INT3_OVP_MASK, },
132 { .reg_offset = 2, .mask = MAX14577_INT3_MBCCHGERR_MASK, },
Chanwoo Choi3008ddb2013-11-22 16:51:05 +0100133};
134
135static const struct regmap_irq_chip max14577_irq_chip = {
136 .name = "max14577",
137 .status_base = MAX14577_REG_INT1,
138 .mask_base = MAX14577_REG_INTMASK1,
Krzysztof Kozlowskiaee2a572014-04-14 11:17:18 +0200139 .mask_invert = true,
Chanwoo Choi3008ddb2013-11-22 16:51:05 +0100140 .num_regs = 3,
141 .irqs = max14577_irqs,
142 .num_irqs = ARRAY_SIZE(max14577_irqs),
143};
144
Krzysztof Kozlowskiaee2a572014-04-14 11:17:18 +0200145static const struct regmap_irq max77836_muic_irqs[] = {
146 /* INT1 interrupts */
147 { .reg_offset = 0, .mask = MAX14577_INT1_ADC_MASK, },
148 { .reg_offset = 0, .mask = MAX14577_INT1_ADCLOW_MASK, },
149 { .reg_offset = 0, .mask = MAX14577_INT1_ADCERR_MASK, },
150 /* INT2 interrupts */
151 { .reg_offset = 1, .mask = MAX14577_INT2_CHGTYP_MASK, },
152 { .reg_offset = 1, .mask = MAX14577_INT2_CHGDETRUN_MASK, },
153 { .reg_offset = 1, .mask = MAX14577_INT2_DCDTMR_MASK, },
154 { .reg_offset = 1, .mask = MAX14577_INT2_DBCHG_MASK, },
155 { .reg_offset = 1, .mask = MAX14577_INT2_VBVOLT_MASK, },
156 { .reg_offset = 1, .mask = MAX77836_INT2_VIDRM_MASK, },
157 /* INT3 interrupts */
158 { .reg_offset = 2, .mask = MAX14577_INT3_EOC_MASK, },
159 { .reg_offset = 2, .mask = MAX14577_INT3_CGMBC_MASK, },
160 { .reg_offset = 2, .mask = MAX14577_INT3_OVP_MASK, },
161 { .reg_offset = 2, .mask = MAX14577_INT3_MBCCHGERR_MASK, },
162};
163
164static const struct regmap_irq_chip max77836_muic_irq_chip = {
165 .name = "max77836-muic",
166 .status_base = MAX14577_REG_INT1,
167 .mask_base = MAX14577_REG_INTMASK1,
168 .mask_invert = true,
169 .num_regs = 3,
170 .irqs = max77836_muic_irqs,
171 .num_irqs = ARRAY_SIZE(max77836_muic_irqs),
172};
173
174static const struct regmap_irq max77836_pmic_irqs[] = {
175 { .reg_offset = 0, .mask = MAX77836_TOPSYS_INT_T120C_MASK, },
176 { .reg_offset = 0, .mask = MAX77836_TOPSYS_INT_T140C_MASK, },
177};
178
179static const struct regmap_irq_chip max77836_pmic_irq_chip = {
180 .name = "max77836-pmic",
181 .status_base = MAX77836_PMIC_REG_TOPSYS_INT,
182 .mask_base = MAX77836_PMIC_REG_TOPSYS_INT_MASK,
183 .mask_invert = false,
184 .num_regs = 1,
185 .irqs = max77836_pmic_irqs,
186 .num_irqs = ARRAY_SIZE(max77836_pmic_irqs),
187};
188
Krzysztof Kozlowskieccb80c2014-04-14 11:17:14 +0200189static void max14577_print_dev_type(struct max14577 *max14577)
190{
191 u8 reg_data, vendor_id, device_id;
192 int ret;
193
194 ret = max14577_read_reg(max14577->regmap, MAX14577_REG_DEVICEID,
195 &reg_data);
196 if (ret) {
197 dev_err(max14577->dev,
198 "Failed to read DEVICEID register: %d\n", ret);
199 return;
200 }
201
202 vendor_id = ((reg_data & DEVID_VENDORID_MASK) >>
203 DEVID_VENDORID_SHIFT);
204 device_id = ((reg_data & DEVID_DEVICEID_MASK) >>
205 DEVID_DEVICEID_SHIFT);
206
207 dev_info(max14577->dev, "Device type: %u (ID: 0x%x, vendor: 0x%x)\n",
208 max14577->dev_type, device_id, vendor_id);
209}
210
Krzysztof Kozlowskiaee2a572014-04-14 11:17:18 +0200211/*
212 * Max77836 specific initialization code for driver probe.
213 * Adds new I2C dummy device, regmap and regmap IRQ chip.
214 * Unmasks Interrupt Source register.
215 *
216 * On success returns 0.
217 * On failure returns errno and reverts any changes done so far (e.g. remove
218 * I2C dummy device), except masking the INT SRC register.
219 */
220static int max77836_init(struct max14577 *max14577)
221{
222 int ret;
223 u8 intsrc_mask;
224
225 max14577->i2c_pmic = i2c_new_dummy(max14577->i2c->adapter,
226 I2C_ADDR_PMIC);
227 if (!max14577->i2c_pmic) {
228 dev_err(max14577->dev, "Failed to register PMIC I2C device\n");
229 return -ENODEV;
230 }
231 i2c_set_clientdata(max14577->i2c_pmic, max14577);
232
233 max14577->regmap_pmic = devm_regmap_init_i2c(max14577->i2c_pmic,
234 &max77836_pmic_regmap_config);
235 if (IS_ERR(max14577->regmap_pmic)) {
236 ret = PTR_ERR(max14577->regmap_pmic);
237 dev_err(max14577->dev, "Failed to allocate PMIC register map: %d\n",
238 ret);
239 goto err;
240 }
241
242 /* Un-mask MAX77836 Interrupt Source register */
243 ret = max14577_read_reg(max14577->regmap_pmic,
244 MAX77836_PMIC_REG_INTSRC_MASK, &intsrc_mask);
245 if (ret < 0) {
246 dev_err(max14577->dev, "Failed to read PMIC register\n");
247 goto err;
248 }
249
250 intsrc_mask &= ~(MAX77836_INTSRC_MASK_TOP_INT_MASK);
251 intsrc_mask &= ~(MAX77836_INTSRC_MASK_MUIC_CHG_INT_MASK);
252 ret = max14577_write_reg(max14577->regmap_pmic,
253 MAX77836_PMIC_REG_INTSRC_MASK, intsrc_mask);
254 if (ret < 0) {
255 dev_err(max14577->dev, "Failed to write PMIC register\n");
256 goto err;
257 }
258
259 ret = regmap_add_irq_chip(max14577->regmap_pmic, max14577->irq,
260 IRQF_TRIGGER_FALLING | IRQF_ONESHOT | IRQF_SHARED,
261 0, &max77836_pmic_irq_chip,
262 &max14577->irq_data_pmic);
263 if (ret != 0) {
264 dev_err(max14577->dev, "Failed to request PMIC IRQ %d: %d\n",
265 max14577->irq, ret);
266 goto err;
267 }
268
269 return 0;
270
271err:
272 i2c_unregister_device(max14577->i2c_pmic);
273
274 return ret;
275}
276
277/*
278 * Max77836 specific de-initialization code for driver remove.
279 */
280static void max77836_remove(struct max14577 *max14577)
281{
282 regmap_del_irq_chip(max14577->irq, max14577->irq_data_pmic);
283 i2c_unregister_device(max14577->i2c_pmic);
284}
285
Chanwoo Choi3008ddb2013-11-22 16:51:05 +0100286static int max14577_i2c_probe(struct i2c_client *i2c,
287 const struct i2c_device_id *id)
288{
289 struct max14577 *max14577;
290 struct max14577_platform_data *pdata = dev_get_platdata(&i2c->dev);
291 struct device_node *np = i2c->dev.of_node;
Chanwoo Choi3008ddb2013-11-22 16:51:05 +0100292 int ret = 0;
Krzysztof Kozlowskiaee2a572014-04-14 11:17:18 +0200293 const struct regmap_irq_chip *irq_chip;
294 struct mfd_cell *mfd_devs;
295 unsigned int mfd_devs_size;
296 int irq_flags;
Chanwoo Choi3008ddb2013-11-22 16:51:05 +0100297
298 if (np) {
299 pdata = devm_kzalloc(&i2c->dev, sizeof(*pdata), GFP_KERNEL);
300 if (!pdata)
301 return -ENOMEM;
302 i2c->dev.platform_data = pdata;
303 }
304
305 if (!pdata) {
Dan Carpenteraab5dc62013-12-04 17:07:12 +0300306 dev_err(&i2c->dev, "No platform data found.\n");
Chanwoo Choi3008ddb2013-11-22 16:51:05 +0100307 return -EINVAL;
308 }
309
310 max14577 = devm_kzalloc(&i2c->dev, sizeof(*max14577), GFP_KERNEL);
311 if (!max14577)
312 return -ENOMEM;
313
314 i2c_set_clientdata(i2c, max14577);
315 max14577->dev = &i2c->dev;
316 max14577->i2c = i2c;
317 max14577->irq = i2c->irq;
318
Krzysztof Kozlowski575343d2014-04-14 11:17:13 +0200319 max14577->regmap = devm_regmap_init_i2c(i2c,
320 &max14577_muic_regmap_config);
Chanwoo Choi3008ddb2013-11-22 16:51:05 +0100321 if (IS_ERR(max14577->regmap)) {
322 ret = PTR_ERR(max14577->regmap);
323 dev_err(max14577->dev, "Failed to allocate register map: %d\n",
324 ret);
325 return ret;
326 }
327
Krzysztof Kozlowskieccb80c2014-04-14 11:17:14 +0200328 if (np) {
329 const struct of_device_id *of_id;
330
331 of_id = of_match_device(max14577_dt_match, &i2c->dev);
332 if (of_id)
333 max14577->dev_type = (unsigned int)of_id->data;
334 } else {
335 max14577->dev_type = id->driver_data;
Chanwoo Choi3008ddb2013-11-22 16:51:05 +0100336 }
Krzysztof Kozlowskieccb80c2014-04-14 11:17:14 +0200337
338 max14577_print_dev_type(max14577);
Chanwoo Choi3008ddb2013-11-22 16:51:05 +0100339
Krzysztof Kozlowskiaee2a572014-04-14 11:17:18 +0200340 switch (max14577->dev_type) {
341 case MAXIM_DEVICE_TYPE_MAX77836:
342 irq_chip = &max77836_muic_irq_chip;
343 mfd_devs = max77836_devs;
344 mfd_devs_size = ARRAY_SIZE(max77836_devs);
345 irq_flags = IRQF_TRIGGER_FALLING | IRQF_ONESHOT | IRQF_SHARED;
346 break;
347 case MAXIM_DEVICE_TYPE_MAX14577:
348 default:
349 irq_chip = &max14577_irq_chip;
350 mfd_devs = max14577_devs;
351 mfd_devs_size = ARRAY_SIZE(max14577_devs);
352 irq_flags = IRQF_TRIGGER_FALLING | IRQF_ONESHOT;
353 break;
354 }
355
Chanwoo Choi3008ddb2013-11-22 16:51:05 +0100356 ret = regmap_add_irq_chip(max14577->regmap, max14577->irq,
Krzysztof Kozlowskiaee2a572014-04-14 11:17:18 +0200357 irq_flags, 0, irq_chip,
Chanwoo Choi3008ddb2013-11-22 16:51:05 +0100358 &max14577->irq_data);
359 if (ret != 0) {
360 dev_err(&i2c->dev, "Failed to request IRQ %d: %d\n",
361 max14577->irq, ret);
362 return ret;
363 }
364
Krzysztof Kozlowskiaee2a572014-04-14 11:17:18 +0200365 /* Max77836 specific initialization code (additional regmap) */
366 if (max14577->dev_type == MAXIM_DEVICE_TYPE_MAX77836) {
367 ret = max77836_init(max14577);
368 if (ret < 0)
369 goto err_max77836;
370 }
371
372 ret = mfd_add_devices(max14577->dev, -1, mfd_devs,
373 mfd_devs_size, NULL, 0,
Chanwoo Choi3008ddb2013-11-22 16:51:05 +0100374 regmap_irq_get_domain(max14577->irq_data));
375 if (ret < 0)
376 goto err_mfd;
377
378 device_init_wakeup(max14577->dev, 1);
379
380 return 0;
381
382err_mfd:
Krzysztof Kozlowskiaee2a572014-04-14 11:17:18 +0200383 if (max14577->dev_type == MAXIM_DEVICE_TYPE_MAX77836)
384 max77836_remove(max14577);
385err_max77836:
Chanwoo Choi3008ddb2013-11-22 16:51:05 +0100386 regmap_del_irq_chip(max14577->irq, max14577->irq_data);
387
388 return ret;
389}
390
391static int max14577_i2c_remove(struct i2c_client *i2c)
392{
393 struct max14577 *max14577 = i2c_get_clientdata(i2c);
394
395 mfd_remove_devices(max14577->dev);
396 regmap_del_irq_chip(max14577->irq, max14577->irq_data);
Krzysztof Kozlowskiaee2a572014-04-14 11:17:18 +0200397 if (max14577->dev_type == MAXIM_DEVICE_TYPE_MAX77836)
398 max77836_remove(max14577);
Chanwoo Choi3008ddb2013-11-22 16:51:05 +0100399
400 return 0;
401}
402
403static const struct i2c_device_id max14577_i2c_id[] = {
Krzysztof Kozlowskieccb80c2014-04-14 11:17:14 +0200404 { "max14577", MAXIM_DEVICE_TYPE_MAX14577, },
Krzysztof Kozlowskiaee2a572014-04-14 11:17:18 +0200405 { "max77836", MAXIM_DEVICE_TYPE_MAX77836, },
Chanwoo Choi3008ddb2013-11-22 16:51:05 +0100406 { }
407};
408MODULE_DEVICE_TABLE(i2c, max14577_i2c_id);
409
Geert Uytterhoeven3edeb1e2014-01-26 11:38:42 +0100410#ifdef CONFIG_PM_SLEEP
Chanwoo Choi3008ddb2013-11-22 16:51:05 +0100411static int max14577_suspend(struct device *dev)
412{
413 struct i2c_client *i2c = container_of(dev, struct i2c_client, dev);
414 struct max14577 *max14577 = i2c_get_clientdata(i2c);
415
416 if (device_may_wakeup(dev)) {
417 enable_irq_wake(max14577->irq);
418 /*
419 * MUIC IRQ must be disabled during suspend if this is
420 * a wake up source because it will be handled before
421 * resuming I2C.
422 *
423 * When device is woken up from suspend (e.g. by ADC change),
424 * an interrupt occurs before resuming I2C bus controller.
425 * Interrupt handler tries to read registers but this read
426 * will fail because I2C is still suspended.
427 */
428 disable_irq(max14577->irq);
429 }
430
431 return 0;
432}
433
434static int max14577_resume(struct device *dev)
435{
436 struct i2c_client *i2c = container_of(dev, struct i2c_client, dev);
437 struct max14577 *max14577 = i2c_get_clientdata(i2c);
438
439 if (device_may_wakeup(dev)) {
440 disable_irq_wake(max14577->irq);
441 enable_irq(max14577->irq);
442 }
443
444 return 0;
445}
Geert Uytterhoeven3edeb1e2014-01-26 11:38:42 +0100446#endif /* CONFIG_PM_SLEEP */
Chanwoo Choi3008ddb2013-11-22 16:51:05 +0100447
Chanwoo Choi3008ddb2013-11-22 16:51:05 +0100448static SIMPLE_DEV_PM_OPS(max14577_pm, max14577_suspend, max14577_resume);
449
450static struct i2c_driver max14577_i2c_driver = {
451 .driver = {
452 .name = "max14577",
453 .owner = THIS_MODULE,
454 .pm = &max14577_pm,
Sachin Kamatae679c12013-12-21 15:50:16 +0530455 .of_match_table = max14577_dt_match,
Chanwoo Choi3008ddb2013-11-22 16:51:05 +0100456 },
457 .probe = max14577_i2c_probe,
458 .remove = max14577_i2c_remove,
459 .id_table = max14577_i2c_id,
460};
461
462static int __init max14577_i2c_init(void)
463{
Krzysztof Kozlowskieccb80c2014-04-14 11:17:14 +0200464 BUILD_BUG_ON(ARRAY_SIZE(max14577_i2c_id) != MAXIM_DEVICE_TYPE_NUM);
465 BUILD_BUG_ON(ARRAY_SIZE(max14577_dt_match) != MAXIM_DEVICE_TYPE_NUM);
466
Chanwoo Choi3008ddb2013-11-22 16:51:05 +0100467 return i2c_add_driver(&max14577_i2c_driver);
468}
469subsys_initcall(max14577_i2c_init);
470
471static void __exit max14577_i2c_exit(void)
472{
473 i2c_del_driver(&max14577_i2c_driver);
474}
475module_exit(max14577_i2c_exit);
476
477MODULE_AUTHOR("Chanwoo Choi <cw00.choi@samsung.com>, Krzysztof Kozlowski <k.kozlowski@samsung.com>");
Krzysztof Kozlowskiaee2a572014-04-14 11:17:18 +0200478MODULE_DESCRIPTION("Maxim 14577/77836 multi-function core driver");
Chanwoo Choi3008ddb2013-11-22 16:51:05 +0100479MODULE_LICENSE("GPL");