blob: a38e9ee1a0d96723e41b826dd19bb57f1a6a7c6b [file] [log] [blame]
Jonghwa Leedae8a962012-06-25 10:34:36 +02001/*
2 * max77686.c - mfd core driver for the Maxim 77686
3 *
4 * Copyright (C) 2012 Samsung Electronics
5 * Chiwoong Byun <woong.byun@smasung.com>
6 * Jonghwa Lee <jonghwa3.lee@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 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 *
22 * This driver is based on max8997.c
23 */
24
25#include <linux/export.h>
26#include <linux/slab.h>
27#include <linux/i2c.h>
Javier Martinez Canillas6f1c1e72014-07-04 22:24:04 +020028#include <linux/irq.h>
29#include <linux/interrupt.h>
Jonghwa Leedae8a962012-06-25 10:34:36 +020030#include <linux/pm_runtime.h>
31#include <linux/module.h>
Jonghwa Leedae8a962012-06-25 10:34:36 +020032#include <linux/mfd/core.h>
33#include <linux/mfd/max77686.h>
34#include <linux/mfd/max77686-private.h>
35#include <linux/err.h>
Sachin Kamat2a048d32013-10-16 14:26:49 +053036#include <linux/of.h>
Jonghwa Leedae8a962012-06-25 10:34:36 +020037
38#define I2C_ADDR_RTC (0x0C >> 1)
39
Geert Uytterhoeven7c0517b2013-11-18 14:33:00 +010040static const struct mfd_cell max77686_devs[] = {
Jonghwa Leedae8a962012-06-25 10:34:36 +020041 { .name = "max77686-pmic", },
42 { .name = "max77686-rtc", },
Olof Johanssonc0fa7e12013-05-10 10:11:59 -070043 { .name = "max77686-clk", },
Jonghwa Leedae8a962012-06-25 10:34:36 +020044};
45
46static struct regmap_config max77686_regmap_config = {
47 .reg_bits = 8,
48 .val_bits = 8,
49};
50
Javier Martinez Canillas6f1c1e72014-07-04 22:24:04 +020051static struct regmap_config max77686_rtc_regmap_config = {
52 .reg_bits = 8,
53 .val_bits = 8,
54};
55
56static const struct regmap_irq max77686_irqs[] = {
57 /* INT1 interrupts */
58 { .reg_offset = 0, .mask = MAX77686_INT1_PWRONF_MSK, },
59 { .reg_offset = 0, .mask = MAX77686_INT1_PWRONR_MSK, },
60 { .reg_offset = 0, .mask = MAX77686_INT1_JIGONBF_MSK, },
61 { .reg_offset = 0, .mask = MAX77686_INT1_JIGONBR_MSK, },
62 { .reg_offset = 0, .mask = MAX77686_INT1_ACOKBF_MSK, },
63 { .reg_offset = 0, .mask = MAX77686_INT1_ACOKBR_MSK, },
64 { .reg_offset = 0, .mask = MAX77686_INT1_ONKEY1S_MSK, },
65 { .reg_offset = 0, .mask = MAX77686_INT1_MRSTB_MSK, },
66 /* INT2 interrupts */
67 { .reg_offset = 1, .mask = MAX77686_INT2_140C_MSK, },
68 { .reg_offset = 1, .mask = MAX77686_INT2_120C_MSK, },
69};
70
71static const struct regmap_irq_chip max77686_irq_chip = {
72 .name = "max77686-pmic",
73 .status_base = MAX77686_REG_INT1,
74 .mask_base = MAX77686_REG_INT1MSK,
75 .num_regs = 2,
76 .irqs = max77686_irqs,
77 .num_irqs = ARRAY_SIZE(max77686_irqs),
78};
79
80static const struct regmap_irq max77686_rtc_irqs[] = {
81 /* RTC interrupts */
82 { .reg_offset = 0, .mask = MAX77686_RTCINT_RTC60S_MSK, },
83 { .reg_offset = 0, .mask = MAX77686_RTCINT_RTCA1_MSK, },
84 { .reg_offset = 0, .mask = MAX77686_RTCINT_RTCA2_MSK, },
85 { .reg_offset = 0, .mask = MAX77686_RTCINT_SMPL_MSK, },
86 { .reg_offset = 0, .mask = MAX77686_RTCINT_RTC1S_MSK, },
87 { .reg_offset = 0, .mask = MAX77686_RTCINT_WTSR_MSK, },
88};
89
90static const struct regmap_irq_chip max77686_rtc_irq_chip = {
91 .name = "max77686-rtc",
92 .status_base = MAX77686_RTC_INT,
93 .mask_base = MAX77686_RTC_INTM,
94 .num_regs = 1,
95 .irqs = max77686_rtc_irqs,
96 .num_irqs = ARRAY_SIZE(max77686_rtc_irqs),
97};
98
Yadwinder Singh Brarc1516f82012-07-06 17:02:55 +053099#ifdef CONFIG_OF
Krzysztof Kozlowskib68ece32014-05-13 12:58:51 +0200100static const struct of_device_id max77686_pmic_dt_match[] = {
Sachin Kamatd1ac2c02013-03-26 10:13:36 +0530101 {.compatible = "maxim,max77686", .data = NULL},
Axel Lin2984fc02012-07-09 22:29:00 +0800102 {},
103};
104
Yadwinder Singh Brarc1516f82012-07-06 17:02:55 +0530105static struct max77686_platform_data *max77686_i2c_parse_dt_pdata(struct device
106 *dev)
107{
108 struct max77686_platform_data *pd;
109
110 pd = devm_kzalloc(dev, sizeof(*pd), GFP_KERNEL);
111 if (!pd) {
112 dev_err(dev, "could not allocate memory for pdata\n");
113 return NULL;
114 }
115
116 dev->platform_data = pd;
117 return pd;
118}
119#else
120static struct max77686_platform_data *max77686_i2c_parse_dt_pdata(struct device
121 *dev)
122{
123 return 0;
124}
125#endif
126
Jonghwa Leedae8a962012-06-25 10:34:36 +0200127static int max77686_i2c_probe(struct i2c_client *i2c,
128 const struct i2c_device_id *id)
129{
Yadwinder Singh Brarc1516f82012-07-06 17:02:55 +0530130 struct max77686_dev *max77686 = NULL;
Jingoo Han334a41c2013-07-30 17:10:05 +0900131 struct max77686_platform_data *pdata = dev_get_platdata(&i2c->dev);
Jonghwa Leedae8a962012-06-25 10:34:36 +0200132 unsigned int data;
133 int ret = 0;
134
Yadwinder Singh Brarc1516f82012-07-06 17:02:55 +0530135 if (i2c->dev.of_node)
136 pdata = max77686_i2c_parse_dt_pdata(&i2c->dev);
137
138 if (!pdata) {
Yadwinder Singh Brarc1516f82012-07-06 17:02:55 +0530139 dev_err(&i2c->dev, "No platform data found.\n");
Lee Jones742413a2013-05-23 16:25:16 +0100140 return -EIO;
Yadwinder Singh Brarc1516f82012-07-06 17:02:55 +0530141 }
142
Lee Jones742413a2013-05-23 16:25:16 +0100143 max77686 = devm_kzalloc(&i2c->dev,
144 sizeof(struct max77686_dev), GFP_KERNEL);
Jonghwa Leedae8a962012-06-25 10:34:36 +0200145 if (max77686 == NULL)
146 return -ENOMEM;
147
Jonghwa Leedae8a962012-06-25 10:34:36 +0200148 i2c_set_clientdata(i2c, max77686);
149 max77686->dev = &i2c->dev;
150 max77686->i2c = i2c;
151 max77686->type = id->driver_data;
152
Jonghwa Leedae8a962012-06-25 10:34:36 +0200153 max77686->wakeup = pdata->wakeup;
Yadwinder Singh Brar2b404592012-07-09 13:21:45 +0200154 max77686->irq = i2c->irq;
Jonghwa Leedae8a962012-06-25 10:34:36 +0200155
Krzysztof Kozlowski74142ff2013-12-20 10:35:07 +0100156 max77686->regmap = devm_regmap_init_i2c(i2c, &max77686_regmap_config);
Axel Lin136d9822012-12-25 00:16:43 +0800157 if (IS_ERR(max77686->regmap)) {
158 ret = PTR_ERR(max77686->regmap);
159 dev_err(max77686->dev, "Failed to allocate register map: %d\n",
160 ret);
Axel Lin136d9822012-12-25 00:16:43 +0800161 return ret;
162 }
163
Jonghwa Leedae8a962012-06-25 10:34:36 +0200164 if (regmap_read(max77686->regmap,
165 MAX77686_REG_DEVICE_ID, &data) < 0) {
166 dev_err(max77686->dev,
167 "device not found on this channel (this is not an error)\n");
Lee Jones742413a2013-05-23 16:25:16 +0100168 return -ENODEV;
Javier Martinez Canillas6f1c1e72014-07-04 22:24:04 +0200169 }
Jonghwa Leedae8a962012-06-25 10:34:36 +0200170
171 max77686->rtc = i2c_new_dummy(i2c->adapter, I2C_ADDR_RTC);
Krzysztof Kozlowskib9e183a2014-02-11 11:03:31 +0100172 if (!max77686->rtc) {
173 dev_err(max77686->dev, "Failed to allocate I2C device for RTC\n");
174 return -ENODEV;
175 }
Jonghwa Leedae8a962012-06-25 10:34:36 +0200176 i2c_set_clientdata(max77686->rtc, max77686);
177
Javier Martinez Canillas6f1c1e72014-07-04 22:24:04 +0200178 max77686->rtc_regmap = devm_regmap_init_i2c(max77686->rtc,
179 &max77686_rtc_regmap_config);
180 if (IS_ERR(max77686->rtc_regmap)) {
181 ret = PTR_ERR(max77686->rtc_regmap);
182 dev_err(max77686->dev, "failed to allocate RTC regmap: %d\n",
183 ret);
184 goto err_unregister_i2c;
185 }
186
187 ret = regmap_add_irq_chip(max77686->regmap, max77686->irq,
188 IRQF_TRIGGER_FALLING | IRQF_ONESHOT |
189 IRQF_SHARED, 0, &max77686_irq_chip,
190 &max77686->irq_data);
191 if (ret != 0) {
192 dev_err(&i2c->dev, "failed to add PMIC irq chip: %d\n", ret);
193 goto err_unregister_i2c;
194 }
195 ret = regmap_add_irq_chip(max77686->rtc_regmap, max77686->irq,
196 IRQF_TRIGGER_FALLING | IRQF_ONESHOT |
197 IRQF_SHARED, 0, &max77686_rtc_irq_chip,
198 &max77686->rtc_irq_data);
199 if (ret != 0) {
200 dev_err(&i2c->dev, "failed to add RTC irq chip: %d\n", ret);
201 goto err_del_irqc;
202 }
Jonghwa Leedae8a962012-06-25 10:34:36 +0200203
204 ret = mfd_add_devices(max77686->dev, -1, max77686_devs,
Mark Brown0848c942012-09-11 15:16:36 +0800205 ARRAY_SIZE(max77686_devs), NULL, 0, NULL);
Lee Jones742413a2013-05-23 16:25:16 +0100206 if (ret < 0) {
Javier Martinez Canillas6f1c1e72014-07-04 22:24:04 +0200207 dev_err(&i2c->dev, "failed to add MFD devices: %d\n", ret);
208 goto err_del_rtc_irqc;
Lee Jones742413a2013-05-23 16:25:16 +0100209 }
Jonghwa Leedae8a962012-06-25 10:34:36 +0200210
Javier Martinez Canillas6f1c1e72014-07-04 22:24:04 +0200211 return 0;
212
213err_del_rtc_irqc:
214 regmap_del_irq_chip(max77686->irq, max77686->rtc_irq_data);
215err_del_irqc:
216 regmap_del_irq_chip(max77686->irq, max77686->irq_data);
217err_unregister_i2c:
218 i2c_unregister_device(max77686->rtc);
219
Jonghwa Leedae8a962012-06-25 10:34:36 +0200220 return ret;
221}
222
223static int max77686_i2c_remove(struct i2c_client *i2c)
224{
225 struct max77686_dev *max77686 = i2c_get_clientdata(i2c);
226
227 mfd_remove_devices(max77686->dev);
Javier Martinez Canillas6f1c1e72014-07-04 22:24:04 +0200228
229 regmap_del_irq_chip(max77686->irq, max77686->rtc_irq_data);
230 regmap_del_irq_chip(max77686->irq, max77686->irq_data);
231
Jonghwa Leedae8a962012-06-25 10:34:36 +0200232 i2c_unregister_device(max77686->rtc);
Jonghwa Leedae8a962012-06-25 10:34:36 +0200233
234 return 0;
235}
236
237static const struct i2c_device_id max77686_i2c_id[] = {
238 { "max77686", TYPE_MAX77686 },
239 { }
240};
241MODULE_DEVICE_TABLE(i2c, max77686_i2c_id);
242
Javier Martinez Canillas2b52b5d2014-07-04 22:24:05 +0200243#ifdef CONFIG_PM_SLEEP
244static int max77686_suspend(struct device *dev)
245{
246 struct i2c_client *i2c = container_of(dev, struct i2c_client, dev);
247 struct max77686_dev *max77686 = i2c_get_clientdata(i2c);
248
249 if (device_may_wakeup(dev))
250 enable_irq_wake(max77686->irq);
251
252 /*
253 * IRQ must be disabled during suspend because if it happens
254 * while suspended it will be handled before resuming I2C.
255 *
256 * When device is woken up from suspend (e.g. by RTC wake alarm),
257 * an interrupt occurs before resuming I2C bus controller.
258 * Interrupt handler tries to read registers but this read
259 * will fail because I2C is still suspended.
260 */
261 disable_irq(max77686->irq);
262
263 return 0;
264}
265
266static int max77686_resume(struct device *dev)
267{
268 struct i2c_client *i2c = container_of(dev, struct i2c_client, dev);
269 struct max77686_dev *max77686 = i2c_get_clientdata(i2c);
270
271 if (device_may_wakeup(dev))
272 disable_irq_wake(max77686->irq);
273
274 enable_irq(max77686->irq);
275
276 return 0;
277}
278#endif /* CONFIG_PM_SLEEP */
279
280static SIMPLE_DEV_PM_OPS(max77686_pm, max77686_suspend, max77686_resume);
281
Jonghwa Leedae8a962012-06-25 10:34:36 +0200282static struct i2c_driver max77686_i2c_driver = {
283 .driver = {
284 .name = "max77686",
285 .owner = THIS_MODULE,
Javier Martinez Canillas2b52b5d2014-07-04 22:24:05 +0200286 .pm = &max77686_pm,
Yadwinder Singh Brarc1516f82012-07-06 17:02:55 +0530287 .of_match_table = of_match_ptr(max77686_pmic_dt_match),
Jonghwa Leedae8a962012-06-25 10:34:36 +0200288 },
289 .probe = max77686_i2c_probe,
290 .remove = max77686_i2c_remove,
291 .id_table = max77686_i2c_id,
292};
293
294static int __init max77686_i2c_init(void)
295{
296 return i2c_add_driver(&max77686_i2c_driver);
297}
298/* init early so consumer devices can complete system boot */
299subsys_initcall(max77686_i2c_init);
300
301static void __exit max77686_i2c_exit(void)
302{
303 i2c_del_driver(&max77686_i2c_driver);
304}
305module_exit(max77686_i2c_exit);
306
307MODULE_DESCRIPTION("MAXIM 77686 multi-function core driver");
308MODULE_AUTHOR("Chiwoong Byun <woong.byun@samsung.com>");
309MODULE_LICENSE("GPL");