blob: 249c139ef04ae3c80e14bd4674a251892f23ecfd [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", },
47 { .name = "max77693-flash", },
48 { .name = "max77693-muic", },
49 { .name = "max77693-haptic", },
50};
51
Chanwoo Choi83871c02012-05-14 22:50:39 +020052static const struct regmap_config max77693_regmap_config = {
53 .reg_bits = 8,
54 .val_bits = 8,
55 .max_register = MAX77693_PMIC_REG_END,
56};
57
Robert Baldyga342d6692014-05-21 08:52:48 +020058static const struct regmap_irq max77693_led_irqs[] = {
59 { .mask = LED_IRQ_FLED2_OPEN, },
60 { .mask = LED_IRQ_FLED2_SHORT, },
61 { .mask = LED_IRQ_FLED1_OPEN, },
62 { .mask = LED_IRQ_FLED1_SHORT, },
63 { .mask = LED_IRQ_MAX_FLASH, },
64};
65
66static const struct regmap_irq_chip max77693_led_irq_chip = {
67 .name = "max77693-led",
68 .status_base = MAX77693_LED_REG_FLASH_INT,
69 .mask_base = MAX77693_LED_REG_FLASH_INT_MASK,
70 .mask_invert = false,
71 .num_regs = 1,
72 .irqs = max77693_led_irqs,
73 .num_irqs = ARRAY_SIZE(max77693_led_irqs),
74};
75
76static const struct regmap_irq max77693_topsys_irqs[] = {
77 { .mask = TOPSYS_IRQ_T120C_INT, },
78 { .mask = TOPSYS_IRQ_T140C_INT, },
79 { .mask = TOPSYS_IRQ_LOWSYS_INT, },
80};
81
82static const struct regmap_irq_chip max77693_topsys_irq_chip = {
83 .name = "max77693-topsys",
84 .status_base = MAX77693_PMIC_REG_TOPSYS_INT,
85 .mask_base = MAX77693_PMIC_REG_TOPSYS_INT_MASK,
86 .mask_invert = false,
87 .num_regs = 1,
88 .irqs = max77693_topsys_irqs,
89 .num_irqs = ARRAY_SIZE(max77693_topsys_irqs),
90};
91
92static const struct regmap_irq max77693_charger_irqs[] = {
93 { .mask = CHG_IRQ_BYP_I, },
94 { .mask = CHG_IRQ_THM_I, },
95 { .mask = CHG_IRQ_BAT_I, },
96 { .mask = CHG_IRQ_CHG_I, },
97 { .mask = CHG_IRQ_CHGIN_I, },
98};
99
100static const struct regmap_irq_chip max77693_charger_irq_chip = {
101 .name = "max77693-charger",
102 .status_base = MAX77693_CHG_REG_CHG_INT,
103 .mask_base = MAX77693_CHG_REG_CHG_INT_MASK,
104 .mask_invert = false,
105 .num_regs = 1,
106 .irqs = max77693_charger_irqs,
107 .num_irqs = ARRAY_SIZE(max77693_charger_irqs),
108};
109
Krzysztof Kozlowski75ad1322013-12-20 10:35:08 +0100110static const struct regmap_config max77693_regmap_muic_config = {
111 .reg_bits = 8,
112 .val_bits = 8,
113 .max_register = MAX77693_MUIC_REG_END,
114};
115
Robert Baldyga342d6692014-05-21 08:52:48 +0200116static const struct regmap_irq max77693_muic_irqs[] = {
117 { .reg_offset = 0, .mask = MUIC_IRQ_INT1_ADC, },
118 { .reg_offset = 0, .mask = MUIC_IRQ_INT1_ADC_LOW, },
119 { .reg_offset = 0, .mask = MUIC_IRQ_INT1_ADC_ERR, },
120 { .reg_offset = 0, .mask = MUIC_IRQ_INT1_ADC1K, },
121
122 { .reg_offset = 1, .mask = MUIC_IRQ_INT2_CHGTYP, },
123 { .reg_offset = 1, .mask = MUIC_IRQ_INT2_CHGDETREUN, },
124 { .reg_offset = 1, .mask = MUIC_IRQ_INT2_DCDTMR, },
125 { .reg_offset = 1, .mask = MUIC_IRQ_INT2_DXOVP, },
126 { .reg_offset = 1, .mask = MUIC_IRQ_INT2_VBVOLT, },
127 { .reg_offset = 1, .mask = MUIC_IRQ_INT2_VIDRM, },
128
129 { .reg_offset = 2, .mask = MUIC_IRQ_INT3_EOC, },
130 { .reg_offset = 2, .mask = MUIC_IRQ_INT3_CGMBC, },
131 { .reg_offset = 2, .mask = MUIC_IRQ_INT3_OVP, },
132 { .reg_offset = 2, .mask = MUIC_IRQ_INT3_MBCCHG_ERR, },
133 { .reg_offset = 2, .mask = MUIC_IRQ_INT3_CHG_ENABLED, },
134 { .reg_offset = 2, .mask = MUIC_IRQ_INT3_BAT_DET, },
135};
136
137static const struct regmap_irq_chip max77693_muic_irq_chip = {
138 .name = "max77693-muic",
139 .status_base = MAX77693_MUIC_REG_INT1,
140 .mask_base = MAX77693_MUIC_REG_INTMASK1,
141 .mask_invert = true,
142 .num_regs = 3,
143 .irqs = max77693_muic_irqs,
144 .num_irqs = ARRAY_SIZE(max77693_muic_irqs),
145};
146
Chanwoo Choi83871c02012-05-14 22:50:39 +0200147static int max77693_i2c_probe(struct i2c_client *i2c,
148 const struct i2c_device_id *id)
149{
150 struct max77693_dev *max77693;
Robert Baldygad0540f92014-05-21 08:52:47 +0200151 unsigned int reg_data;
Chanwoo Choi83871c02012-05-14 22:50:39 +0200152 int ret = 0;
153
154 max77693 = devm_kzalloc(&i2c->dev,
155 sizeof(struct max77693_dev), GFP_KERNEL);
156 if (max77693 == NULL)
157 return -ENOMEM;
158
Chanwoo Choi83871c02012-05-14 22:50:39 +0200159 i2c_set_clientdata(i2c, max77693);
160 max77693->dev = &i2c->dev;
161 max77693->i2c = i2c;
162 max77693->irq = i2c->irq;
163 max77693->type = id->driver_data;
164
Axel Lin2429d862012-12-25 00:28:36 +0800165 max77693->regmap = devm_regmap_init_i2c(i2c, &max77693_regmap_config);
166 if (IS_ERR(max77693->regmap)) {
167 ret = PTR_ERR(max77693->regmap);
168 dev_err(max77693->dev, "failed to allocate register map: %d\n",
169 ret);
170 return ret;
171 }
Chanwoo Choi83871c02012-05-14 22:50:39 +0200172
Robert Baldygad0540f92014-05-21 08:52:47 +0200173 ret = regmap_read(max77693->regmap, MAX77693_PMIC_REG_PMIC_ID2,
Axel Lin2429d862012-12-25 00:28:36 +0800174 &reg_data);
175 if (ret < 0) {
Chanwoo Choi83871c02012-05-14 22:50:39 +0200176 dev_err(max77693->dev, "device not found on this channel\n");
Axel Lin2429d862012-12-25 00:28:36 +0800177 return ret;
Chanwoo Choi83871c02012-05-14 22:50:39 +0200178 } else
179 dev_info(max77693->dev, "device ID: 0x%x\n", reg_data);
180
181 max77693->muic = i2c_new_dummy(i2c->adapter, I2C_ADDR_MUIC);
Krzysztof Kozlowskiad09dd62014-02-11 11:03:32 +0100182 if (!max77693->muic) {
183 dev_err(max77693->dev, "Failed to allocate I2C device for MUIC\n");
184 return -ENODEV;
185 }
Chanwoo Choi83871c02012-05-14 22:50:39 +0200186 i2c_set_clientdata(max77693->muic, max77693);
187
188 max77693->haptic = i2c_new_dummy(i2c->adapter, I2C_ADDR_HAPTIC);
Krzysztof Kozlowskiad09dd62014-02-11 11:03:32 +0100189 if (!max77693->haptic) {
190 dev_err(max77693->dev, "Failed to allocate I2C device for Haptic\n");
191 ret = -ENODEV;
192 goto err_i2c_haptic;
193 }
Chanwoo Choi83871c02012-05-14 22:50:39 +0200194 i2c_set_clientdata(max77693->haptic, max77693);
195
Chanwoo Choib186b122012-08-21 15:16:23 +0900196 /*
197 * Initialize register map for MUIC device because use regmap-muic
198 * instance of MUIC device when irq of max77693 is initialized
199 * before call max77693-muic probe() function.
200 */
201 max77693->regmap_muic = devm_regmap_init_i2c(max77693->muic,
Krzysztof Kozlowski75ad1322013-12-20 10:35:08 +0100202 &max77693_regmap_muic_config);
Chanwoo Choib186b122012-08-21 15:16:23 +0900203 if (IS_ERR(max77693->regmap_muic)) {
204 ret = PTR_ERR(max77693->regmap_muic);
205 dev_err(max77693->dev,
206 "failed to allocate register map: %d\n", ret);
Axel Lin2429d862012-12-25 00:28:36 +0800207 goto err_regmap_muic;
Chanwoo Choib186b122012-08-21 15:16:23 +0900208 }
209
Robert Baldyga342d6692014-05-21 08:52:48 +0200210 ret = regmap_add_irq_chip(max77693->regmap, max77693->irq,
211 IRQF_ONESHOT | IRQF_SHARED |
212 IRQF_TRIGGER_FALLING, 0,
213 &max77693_led_irq_chip,
214 &max77693->irq_data_led);
215 if (ret) {
216 dev_err(max77693->dev, "failed to add irq chip: %d\n", ret);
217 goto err_regmap_muic;
218 }
219
220 ret = regmap_add_irq_chip(max77693->regmap, max77693->irq,
221 IRQF_ONESHOT | IRQF_SHARED |
222 IRQF_TRIGGER_FALLING, 0,
223 &max77693_topsys_irq_chip,
224 &max77693->irq_data_topsys);
225 if (ret) {
226 dev_err(max77693->dev, "failed to add irq chip: %d\n", ret);
227 goto err_irq_topsys;
228 }
229
230 ret = regmap_add_irq_chip(max77693->regmap, max77693->irq,
231 IRQF_ONESHOT | IRQF_SHARED |
232 IRQF_TRIGGER_FALLING, 0,
233 &max77693_charger_irq_chip,
234 &max77693->irq_data_charger);
235 if (ret) {
236 dev_err(max77693->dev, "failed to add irq chip: %d\n", ret);
237 goto err_irq_charger;
238 }
239
240 ret = regmap_add_irq_chip(max77693->regmap, max77693->irq,
241 IRQF_ONESHOT | IRQF_SHARED |
242 IRQF_TRIGGER_FALLING, 0,
243 &max77693_muic_irq_chip,
244 &max77693->irq_data_muic);
245 if (ret) {
246 dev_err(max77693->dev, "failed to add irq chip: %d\n", ret);
247 goto err_irq_muic;
248 }
Chanwoo Choi6592ebb2012-05-14 22:54:20 +0200249
Chanwoo Choi83871c02012-05-14 22:50:39 +0200250 pm_runtime_set_active(max77693->dev);
251
252 ret = mfd_add_devices(max77693->dev, -1, max77693_devs,
Mark Brown0848c942012-09-11 15:16:36 +0800253 ARRAY_SIZE(max77693_devs), NULL, 0, NULL);
Chanwoo Choi83871c02012-05-14 22:50:39 +0200254 if (ret < 0)
255 goto err_mfd;
256
257 return ret;
258
259err_mfd:
Robert Baldyga342d6692014-05-21 08:52:48 +0200260 mfd_remove_devices(max77693->dev);
261 regmap_del_irq_chip(max77693->irq, max77693->irq_data_muic);
262err_irq_muic:
263 regmap_del_irq_chip(max77693->irq, max77693->irq_data_charger);
264err_irq_charger:
265 regmap_del_irq_chip(max77693->irq, max77693->irq_data_topsys);
266err_irq_topsys:
267 regmap_del_irq_chip(max77693->irq, max77693->irq_data_led);
Axel Lin2429d862012-12-25 00:28:36 +0800268err_regmap_muic:
Chanwoo Choi83871c02012-05-14 22:50:39 +0200269 i2c_unregister_device(max77693->haptic);
Krzysztof Kozlowskiad09dd62014-02-11 11:03:32 +0100270err_i2c_haptic:
271 i2c_unregister_device(max77693->muic);
Chanwoo Choi83871c02012-05-14 22:50:39 +0200272 return ret;
273}
274
275static int max77693_i2c_remove(struct i2c_client *i2c)
276{
277 struct max77693_dev *max77693 = i2c_get_clientdata(i2c);
278
279 mfd_remove_devices(max77693->dev);
Robert Baldyga342d6692014-05-21 08:52:48 +0200280
281 regmap_del_irq_chip(max77693->irq, max77693->irq_data_muic);
282 regmap_del_irq_chip(max77693->irq, max77693->irq_data_charger);
283 regmap_del_irq_chip(max77693->irq, max77693->irq_data_topsys);
284 regmap_del_irq_chip(max77693->irq, max77693->irq_data_led);
285
Chanwoo Choi83871c02012-05-14 22:50:39 +0200286 i2c_unregister_device(max77693->muic);
287 i2c_unregister_device(max77693->haptic);
288
289 return 0;
290}
291
292static const struct i2c_device_id max77693_i2c_id[] = {
293 { "max77693", TYPE_MAX77693 },
294 { }
295};
296MODULE_DEVICE_TABLE(i2c, max77693_i2c_id);
297
Chanwoo Choi6592ebb2012-05-14 22:54:20 +0200298static int max77693_suspend(struct device *dev)
299{
300 struct i2c_client *i2c = container_of(dev, struct i2c_client, dev);
301 struct max77693_dev *max77693 = i2c_get_clientdata(i2c);
302
Robert Baldyga342d6692014-05-21 08:52:48 +0200303 if (device_may_wakeup(dev)) {
304 enable_irq_wake(max77693->irq);
305 disable_irq(max77693->irq);
306 }
307
Chanwoo Choi6592ebb2012-05-14 22:54:20 +0200308 return 0;
309}
310
311static int max77693_resume(struct device *dev)
312{
313 struct i2c_client *i2c = container_of(dev, struct i2c_client, dev);
314 struct max77693_dev *max77693 = i2c_get_clientdata(i2c);
315
Robert Baldyga342d6692014-05-21 08:52:48 +0200316 if (device_may_wakeup(dev)) {
317 disable_irq_wake(max77693->irq);
318 enable_irq(max77693->irq);
319 }
320
321 return 0;
Chanwoo Choi6592ebb2012-05-14 22:54:20 +0200322}
323
Mark Brown12477a32012-06-05 16:15:59 +0100324static const struct dev_pm_ops max77693_pm = {
Chanwoo Choi6592ebb2012-05-14 22:54:20 +0200325 .suspend = max77693_suspend,
326 .resume = max77693_resume,
327};
328
Andrzej Hajda46571852013-09-27 09:27:46 +0200329#ifdef CONFIG_OF
Krzysztof Kozlowski5ce9a552014-05-13 12:58:52 +0200330static const struct of_device_id max77693_dt_match[] = {
Andrzej Hajda46571852013-09-27 09:27:46 +0200331 { .compatible = "maxim,max77693" },
332 {},
333};
334#endif
335
Chanwoo Choi83871c02012-05-14 22:50:39 +0200336static struct i2c_driver max77693_i2c_driver = {
337 .driver = {
338 .name = "max77693",
339 .owner = THIS_MODULE,
Chanwoo Choi6592ebb2012-05-14 22:54:20 +0200340 .pm = &max77693_pm,
Andrzej Hajda46571852013-09-27 09:27:46 +0200341 .of_match_table = of_match_ptr(max77693_dt_match),
Chanwoo Choi83871c02012-05-14 22:50:39 +0200342 },
343 .probe = max77693_i2c_probe,
344 .remove = max77693_i2c_remove,
345 .id_table = max77693_i2c_id,
346};
347
348static int __init max77693_i2c_init(void)
349{
350 return i2c_add_driver(&max77693_i2c_driver);
351}
352/* init early so consumer devices can complete system boot */
353subsys_initcall(max77693_i2c_init);
354
355static void __exit max77693_i2c_exit(void)
356{
357 i2c_del_driver(&max77693_i2c_driver);
358}
359module_exit(max77693_i2c_exit);
360
361MODULE_DESCRIPTION("MAXIM 77693 multi-function core driver");
362MODULE_AUTHOR("SangYoung, Son <hello.son@samsung.com>");
363MODULE_LICENSE("GPL");