blob: 711773e8e64bdd9ee5835c8ae9d738aecf31eeba [file] [log] [blame]
Chanwoo Choi83871c02012-05-14 22:50:39 +02001/*
2 * max77693.c - mfd core driver for the MAX 77693
3 *
4 * Copyright (C) 2012 Samsung Electronics
5 * SangYoung Son <hello.son@smasung.com>
6 *
7 * This program is not provided / owned by Maxim Integrated Products.
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 *
23 * This driver is based on max8997.c
24 */
25
26#include <linux/module.h>
27#include <linux/slab.h>
28#include <linux/i2c.h>
29#include <linux/err.h>
30#include <linux/interrupt.h>
Sachin Kamat71c7f932013-10-18 16:11:57 +053031#include <linux/of.h>
Chanwoo Choi83871c02012-05-14 22:50:39 +020032#include <linux/pm_runtime.h>
33#include <linux/mutex.h>
34#include <linux/mfd/core.h>
35#include <linux/mfd/max77693.h>
36#include <linux/mfd/max77693-private.h>
37#include <linux/regulator/machine.h>
38#include <linux/regmap.h>
39
40#define I2C_ADDR_PMIC (0xCC >> 1) /* Charger, Flash LED */
41#define I2C_ADDR_MUIC (0x4A >> 1)
42#define I2C_ADDR_HAPTIC (0x90 >> 1)
43
Geert Uytterhoeven7c0517b2013-11-18 14:33:00 +010044static const struct mfd_cell max77693_devs[] = {
Chanwoo Choi83871c02012-05-14 22:50:39 +020045 { .name = "max77693-pmic", },
46 { .name = "max77693-charger", },
Chanwoo Choi83871c02012-05-14 22:50:39 +020047 { .name = "max77693-muic", },
48 { .name = "max77693-haptic", },
Jacek Anaszewskia0bc60722014-08-22 11:06:18 +020049 {
50 .name = "max77693-flash",
51 .of_compatible = "maxim,max77693-flash",
52 },
Chanwoo Choi83871c02012-05-14 22:50:39 +020053};
54
Chanwoo Choi83871c02012-05-14 22:50:39 +020055static const struct regmap_config max77693_regmap_config = {
56 .reg_bits = 8,
57 .val_bits = 8,
58 .max_register = MAX77693_PMIC_REG_END,
59};
60
Robert Baldyga342d6692014-05-21 08:52:48 +020061static const struct regmap_irq max77693_led_irqs[] = {
62 { .mask = LED_IRQ_FLED2_OPEN, },
63 { .mask = LED_IRQ_FLED2_SHORT, },
64 { .mask = LED_IRQ_FLED1_OPEN, },
65 { .mask = LED_IRQ_FLED1_SHORT, },
66 { .mask = LED_IRQ_MAX_FLASH, },
67};
68
69static const struct regmap_irq_chip max77693_led_irq_chip = {
70 .name = "max77693-led",
71 .status_base = MAX77693_LED_REG_FLASH_INT,
72 .mask_base = MAX77693_LED_REG_FLASH_INT_MASK,
73 .mask_invert = false,
74 .num_regs = 1,
75 .irqs = max77693_led_irqs,
76 .num_irqs = ARRAY_SIZE(max77693_led_irqs),
77};
78
79static const struct regmap_irq max77693_topsys_irqs[] = {
80 { .mask = TOPSYS_IRQ_T120C_INT, },
81 { .mask = TOPSYS_IRQ_T140C_INT, },
82 { .mask = TOPSYS_IRQ_LOWSYS_INT, },
83};
84
85static const struct regmap_irq_chip max77693_topsys_irq_chip = {
86 .name = "max77693-topsys",
87 .status_base = MAX77693_PMIC_REG_TOPSYS_INT,
88 .mask_base = MAX77693_PMIC_REG_TOPSYS_INT_MASK,
89 .mask_invert = false,
90 .num_regs = 1,
91 .irqs = max77693_topsys_irqs,
92 .num_irqs = ARRAY_SIZE(max77693_topsys_irqs),
93};
94
95static const struct regmap_irq max77693_charger_irqs[] = {
96 { .mask = CHG_IRQ_BYP_I, },
97 { .mask = CHG_IRQ_THM_I, },
98 { .mask = CHG_IRQ_BAT_I, },
99 { .mask = CHG_IRQ_CHG_I, },
100 { .mask = CHG_IRQ_CHGIN_I, },
101};
102
103static const struct regmap_irq_chip max77693_charger_irq_chip = {
104 .name = "max77693-charger",
105 .status_base = MAX77693_CHG_REG_CHG_INT,
106 .mask_base = MAX77693_CHG_REG_CHG_INT_MASK,
107 .mask_invert = false,
108 .num_regs = 1,
109 .irqs = max77693_charger_irqs,
110 .num_irqs = ARRAY_SIZE(max77693_charger_irqs),
111};
112
Krzysztof Kozlowski75ad1322013-12-20 10:35:08 +0100113static const struct regmap_config max77693_regmap_muic_config = {
114 .reg_bits = 8,
115 .val_bits = 8,
116 .max_register = MAX77693_MUIC_REG_END,
117};
118
Robert Baldyga342d6692014-05-21 08:52:48 +0200119static const struct regmap_irq max77693_muic_irqs[] = {
120 { .reg_offset = 0, .mask = MUIC_IRQ_INT1_ADC, },
121 { .reg_offset = 0, .mask = MUIC_IRQ_INT1_ADC_LOW, },
122 { .reg_offset = 0, .mask = MUIC_IRQ_INT1_ADC_ERR, },
123 { .reg_offset = 0, .mask = MUIC_IRQ_INT1_ADC1K, },
124
125 { .reg_offset = 1, .mask = MUIC_IRQ_INT2_CHGTYP, },
126 { .reg_offset = 1, .mask = MUIC_IRQ_INT2_CHGDETREUN, },
127 { .reg_offset = 1, .mask = MUIC_IRQ_INT2_DCDTMR, },
128 { .reg_offset = 1, .mask = MUIC_IRQ_INT2_DXOVP, },
129 { .reg_offset = 1, .mask = MUIC_IRQ_INT2_VBVOLT, },
130 { .reg_offset = 1, .mask = MUIC_IRQ_INT2_VIDRM, },
131
132 { .reg_offset = 2, .mask = MUIC_IRQ_INT3_EOC, },
133 { .reg_offset = 2, .mask = MUIC_IRQ_INT3_CGMBC, },
134 { .reg_offset = 2, .mask = MUIC_IRQ_INT3_OVP, },
135 { .reg_offset = 2, .mask = MUIC_IRQ_INT3_MBCCHG_ERR, },
136 { .reg_offset = 2, .mask = MUIC_IRQ_INT3_CHG_ENABLED, },
137 { .reg_offset = 2, .mask = MUIC_IRQ_INT3_BAT_DET, },
138};
139
140static const struct regmap_irq_chip max77693_muic_irq_chip = {
141 .name = "max77693-muic",
142 .status_base = MAX77693_MUIC_REG_INT1,
143 .mask_base = MAX77693_MUIC_REG_INTMASK1,
144 .mask_invert = true,
145 .num_regs = 3,
146 .irqs = max77693_muic_irqs,
147 .num_irqs = ARRAY_SIZE(max77693_muic_irqs),
148};
149
Chanwoo Choi83871c02012-05-14 22:50:39 +0200150static int max77693_i2c_probe(struct i2c_client *i2c,
151 const struct i2c_device_id *id)
152{
153 struct max77693_dev *max77693;
Robert Baldygad0540f92014-05-21 08:52:47 +0200154 unsigned int reg_data;
Chanwoo Choi83871c02012-05-14 22:50:39 +0200155 int ret = 0;
156
157 max77693 = devm_kzalloc(&i2c->dev,
158 sizeof(struct max77693_dev), GFP_KERNEL);
159 if (max77693 == NULL)
160 return -ENOMEM;
161
Chanwoo Choi83871c02012-05-14 22:50:39 +0200162 i2c_set_clientdata(i2c, max77693);
163 max77693->dev = &i2c->dev;
164 max77693->i2c = i2c;
165 max77693->irq = i2c->irq;
166 max77693->type = id->driver_data;
167
Axel Lin2429d862012-12-25 00:28:36 +0800168 max77693->regmap = devm_regmap_init_i2c(i2c, &max77693_regmap_config);
169 if (IS_ERR(max77693->regmap)) {
170 ret = PTR_ERR(max77693->regmap);
171 dev_err(max77693->dev, "failed to allocate register map: %d\n",
172 ret);
173 return ret;
174 }
Chanwoo Choi83871c02012-05-14 22:50:39 +0200175
Robert Baldygad0540f92014-05-21 08:52:47 +0200176 ret = regmap_read(max77693->regmap, MAX77693_PMIC_REG_PMIC_ID2,
Axel Lin2429d862012-12-25 00:28:36 +0800177 &reg_data);
178 if (ret < 0) {
Chanwoo Choi83871c02012-05-14 22:50:39 +0200179 dev_err(max77693->dev, "device not found on this channel\n");
Axel Lin2429d862012-12-25 00:28:36 +0800180 return ret;
Chanwoo Choi83871c02012-05-14 22:50:39 +0200181 } else
182 dev_info(max77693->dev, "device ID: 0x%x\n", reg_data);
183
184 max77693->muic = i2c_new_dummy(i2c->adapter, I2C_ADDR_MUIC);
Krzysztof Kozlowskiad09dd62014-02-11 11:03:32 +0100185 if (!max77693->muic) {
186 dev_err(max77693->dev, "Failed to allocate I2C device for MUIC\n");
187 return -ENODEV;
188 }
Chanwoo Choi83871c02012-05-14 22:50:39 +0200189 i2c_set_clientdata(max77693->muic, max77693);
190
191 max77693->haptic = i2c_new_dummy(i2c->adapter, I2C_ADDR_HAPTIC);
Krzysztof Kozlowskiad09dd62014-02-11 11:03:32 +0100192 if (!max77693->haptic) {
193 dev_err(max77693->dev, "Failed to allocate I2C device for Haptic\n");
194 ret = -ENODEV;
195 goto err_i2c_haptic;
196 }
Chanwoo Choi83871c02012-05-14 22:50:39 +0200197 i2c_set_clientdata(max77693->haptic, max77693);
198
Chanwoo Choib186b122012-08-21 15:16:23 +0900199 /*
200 * Initialize register map for MUIC device because use regmap-muic
201 * instance of MUIC device when irq of max77693 is initialized
202 * before call max77693-muic probe() function.
203 */
204 max77693->regmap_muic = devm_regmap_init_i2c(max77693->muic,
Krzysztof Kozlowski75ad1322013-12-20 10:35:08 +0100205 &max77693_regmap_muic_config);
Chanwoo Choib186b122012-08-21 15:16:23 +0900206 if (IS_ERR(max77693->regmap_muic)) {
207 ret = PTR_ERR(max77693->regmap_muic);
208 dev_err(max77693->dev,
209 "failed to allocate register map: %d\n", ret);
Axel Lin2429d862012-12-25 00:28:36 +0800210 goto err_regmap_muic;
Chanwoo Choib186b122012-08-21 15:16:23 +0900211 }
212
Robert Baldyga342d6692014-05-21 08:52:48 +0200213 ret = regmap_add_irq_chip(max77693->regmap, max77693->irq,
214 IRQF_ONESHOT | IRQF_SHARED |
215 IRQF_TRIGGER_FALLING, 0,
216 &max77693_led_irq_chip,
217 &max77693->irq_data_led);
218 if (ret) {
219 dev_err(max77693->dev, "failed to add irq chip: %d\n", ret);
220 goto err_regmap_muic;
221 }
222
223 ret = regmap_add_irq_chip(max77693->regmap, max77693->irq,
224 IRQF_ONESHOT | IRQF_SHARED |
225 IRQF_TRIGGER_FALLING, 0,
226 &max77693_topsys_irq_chip,
227 &max77693->irq_data_topsys);
228 if (ret) {
229 dev_err(max77693->dev, "failed to add irq chip: %d\n", ret);
230 goto err_irq_topsys;
231 }
232
233 ret = regmap_add_irq_chip(max77693->regmap, max77693->irq,
234 IRQF_ONESHOT | IRQF_SHARED |
235 IRQF_TRIGGER_FALLING, 0,
236 &max77693_charger_irq_chip,
237 &max77693->irq_data_charger);
238 if (ret) {
239 dev_err(max77693->dev, "failed to add irq chip: %d\n", ret);
240 goto err_irq_charger;
241 }
242
Krzysztof Kozlowski43fc9392014-10-10 10:22:01 +0200243 ret = regmap_add_irq_chip(max77693->regmap_muic, max77693->irq,
Robert Baldyga342d6692014-05-21 08:52:48 +0200244 IRQF_ONESHOT | IRQF_SHARED |
245 IRQF_TRIGGER_FALLING, 0,
246 &max77693_muic_irq_chip,
247 &max77693->irq_data_muic);
248 if (ret) {
249 dev_err(max77693->dev, "failed to add irq chip: %d\n", ret);
250 goto err_irq_muic;
251 }
Chanwoo Choi6592ebb2012-05-14 22:54:20 +0200252
Krzysztof Kozlowskic0acb812014-10-10 12:48:35 +0200253 /* Unmask interrupts from all blocks in interrupt source register */
254 ret = regmap_update_bits(max77693->regmap,
255 MAX77693_PMIC_REG_INTSRC_MASK,
256 SRC_IRQ_ALL, (unsigned int)~SRC_IRQ_ALL);
257 if (ret < 0) {
258 dev_err(max77693->dev,
259 "Could not unmask interrupts in INTSRC: %d\n",
260 ret);
261 goto err_intsrc;
262 }
263
Chanwoo Choi83871c02012-05-14 22:50:39 +0200264 pm_runtime_set_active(max77693->dev);
265
266 ret = mfd_add_devices(max77693->dev, -1, max77693_devs,
Mark Brown0848c942012-09-11 15:16:36 +0800267 ARRAY_SIZE(max77693_devs), NULL, 0, NULL);
Chanwoo Choi83871c02012-05-14 22:50:39 +0200268 if (ret < 0)
269 goto err_mfd;
270
271 return ret;
272
273err_mfd:
Robert Baldyga342d6692014-05-21 08:52:48 +0200274 mfd_remove_devices(max77693->dev);
Krzysztof Kozlowskic0acb812014-10-10 12:48:35 +0200275err_intsrc:
Robert Baldyga342d6692014-05-21 08:52:48 +0200276 regmap_del_irq_chip(max77693->irq, max77693->irq_data_muic);
277err_irq_muic:
278 regmap_del_irq_chip(max77693->irq, max77693->irq_data_charger);
279err_irq_charger:
280 regmap_del_irq_chip(max77693->irq, max77693->irq_data_topsys);
281err_irq_topsys:
282 regmap_del_irq_chip(max77693->irq, max77693->irq_data_led);
Axel Lin2429d862012-12-25 00:28:36 +0800283err_regmap_muic:
Chanwoo Choi83871c02012-05-14 22:50:39 +0200284 i2c_unregister_device(max77693->haptic);
Krzysztof Kozlowskiad09dd62014-02-11 11:03:32 +0100285err_i2c_haptic:
286 i2c_unregister_device(max77693->muic);
Chanwoo Choi83871c02012-05-14 22:50:39 +0200287 return ret;
288}
289
290static int max77693_i2c_remove(struct i2c_client *i2c)
291{
292 struct max77693_dev *max77693 = i2c_get_clientdata(i2c);
293
294 mfd_remove_devices(max77693->dev);
Robert Baldyga342d6692014-05-21 08:52:48 +0200295
296 regmap_del_irq_chip(max77693->irq, max77693->irq_data_muic);
297 regmap_del_irq_chip(max77693->irq, max77693->irq_data_charger);
298 regmap_del_irq_chip(max77693->irq, max77693->irq_data_topsys);
299 regmap_del_irq_chip(max77693->irq, max77693->irq_data_led);
300
Chanwoo Choi83871c02012-05-14 22:50:39 +0200301 i2c_unregister_device(max77693->muic);
302 i2c_unregister_device(max77693->haptic);
303
304 return 0;
305}
306
307static const struct i2c_device_id max77693_i2c_id[] = {
308 { "max77693", TYPE_MAX77693 },
309 { }
310};
311MODULE_DEVICE_TABLE(i2c, max77693_i2c_id);
312
Chanwoo Choi6592ebb2012-05-14 22:54:20 +0200313static int max77693_suspend(struct device *dev)
314{
315 struct i2c_client *i2c = container_of(dev, struct i2c_client, dev);
316 struct max77693_dev *max77693 = i2c_get_clientdata(i2c);
317
Robert Baldyga342d6692014-05-21 08:52:48 +0200318 if (device_may_wakeup(dev)) {
319 enable_irq_wake(max77693->irq);
320 disable_irq(max77693->irq);
321 }
322
Chanwoo Choi6592ebb2012-05-14 22:54:20 +0200323 return 0;
324}
325
326static int max77693_resume(struct device *dev)
327{
328 struct i2c_client *i2c = container_of(dev, struct i2c_client, dev);
329 struct max77693_dev *max77693 = i2c_get_clientdata(i2c);
330
Robert Baldyga342d6692014-05-21 08:52:48 +0200331 if (device_may_wakeup(dev)) {
332 disable_irq_wake(max77693->irq);
333 enable_irq(max77693->irq);
334 }
335
336 return 0;
Chanwoo Choi6592ebb2012-05-14 22:54:20 +0200337}
338
Mark Brown12477a32012-06-05 16:15:59 +0100339static const struct dev_pm_ops max77693_pm = {
Chanwoo Choi6592ebb2012-05-14 22:54:20 +0200340 .suspend = max77693_suspend,
341 .resume = max77693_resume,
342};
343
Andrzej Hajda46571852013-09-27 09:27:46 +0200344#ifdef CONFIG_OF
Krzysztof Kozlowski5ce9a552014-05-13 12:58:52 +0200345static const struct of_device_id max77693_dt_match[] = {
Andrzej Hajda46571852013-09-27 09:27:46 +0200346 { .compatible = "maxim,max77693" },
347 {},
348};
349#endif
350
Chanwoo Choi83871c02012-05-14 22:50:39 +0200351static struct i2c_driver max77693_i2c_driver = {
352 .driver = {
353 .name = "max77693",
354 .owner = THIS_MODULE,
Chanwoo Choi6592ebb2012-05-14 22:54:20 +0200355 .pm = &max77693_pm,
Andrzej Hajda46571852013-09-27 09:27:46 +0200356 .of_match_table = of_match_ptr(max77693_dt_match),
Chanwoo Choi83871c02012-05-14 22:50:39 +0200357 },
358 .probe = max77693_i2c_probe,
359 .remove = max77693_i2c_remove,
360 .id_table = max77693_i2c_id,
361};
362
363static int __init max77693_i2c_init(void)
364{
365 return i2c_add_driver(&max77693_i2c_driver);
366}
367/* init early so consumer devices can complete system boot */
368subsys_initcall(max77693_i2c_init);
369
370static void __exit max77693_i2c_exit(void)
371{
372 i2c_del_driver(&max77693_i2c_driver);
373}
374module_exit(max77693_i2c_exit);
375
376MODULE_DESCRIPTION("MAXIM 77693 multi-function core driver");
377MODULE_AUTHOR("SangYoung, Son <hello.son@samsung.com>");
378MODULE_LICENSE("GPL");