blob: 4c33b8063bc32c99e5232735c847111efd0c7efe [file] [log] [blame]
Kyungmin Park156f2522010-06-16 09:04:16 +02001/*
Joonyoung Shim2c7e6f52010-09-10 18:36:39 +02002 * max8998.c - mfd core driver for the Maxim 8998
Kyungmin Park156f2522010-06-16 09:04:16 +02003 *
4 * Copyright (C) 2009-2010 Samsung Electronics
5 * Kyungmin Park <kyungmin.park@samsung.com>
6 * Marek Szyprowski <m.szyprowski@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
Tomasz Figaee999fb2013-06-25 16:08:10 +020023#include <linux/err.h>
Kyungmin Park156f2522010-06-16 09:04:16 +020024#include <linux/init.h>
25#include <linux/slab.h>
26#include <linux/i2c.h>
MyungJoo Hamcdd137c2010-12-23 17:53:36 +090027#include <linux/interrupt.h>
Tomasz Figaee999fb2013-06-25 16:08:10 +020028#include <linux/of.h>
29#include <linux/of_irq.h>
MyungJoo Hamcdd137c2010-12-23 17:53:36 +090030#include <linux/pm_runtime.h>
Kyungmin Park156f2522010-06-16 09:04:16 +020031#include <linux/mutex.h>
32#include <linux/mfd/core.h>
33#include <linux/mfd/max8998.h>
34#include <linux/mfd/max8998-private.h>
35
Joonyoung Shim9b16c0a2010-08-06 11:28:08 +090036#define RTC_I2C_ADDR (0x0c >> 1)
37
Geert Uytterhoeven7c0517b2013-11-18 14:33:00 +010038static const struct mfd_cell max8998_devs[] = {
Kyungmin Park156f2522010-06-16 09:04:16 +020039 {
40 .name = "max8998-pmic",
Joonyoung Shim9b16c0a2010-08-06 11:28:08 +090041 }, {
42 .name = "max8998-rtc",
Donggeun Kimbb4ce972011-06-24 19:04:18 +090043 }, {
44 .name = "max8998-battery",
Joonyoung Shim9b16c0a2010-08-06 11:28:08 +090045 },
Kyungmin Park156f2522010-06-16 09:04:16 +020046};
47
Geert Uytterhoeven7c0517b2013-11-18 14:33:00 +010048static const struct mfd_cell lp3974_devs[] = {
MyungJoo Ham337ce5d2011-01-04 14:17:39 +090049 {
50 .name = "lp3974-pmic",
51 }, {
52 .name = "lp3974-rtc",
53 },
54};
55
Joonyoung Shim676e02d2010-08-06 11:28:06 +090056int max8998_read_reg(struct i2c_client *i2c, u8 reg, u8 *dest)
Kyungmin Park156f2522010-06-16 09:04:16 +020057{
Joonyoung Shim676e02d2010-08-06 11:28:06 +090058 struct max8998_dev *max8998 = i2c_get_clientdata(i2c);
Kyungmin Park156f2522010-06-16 09:04:16 +020059 int ret;
60
61 mutex_lock(&max8998->iolock);
Joonyoung Shim676e02d2010-08-06 11:28:06 +090062 ret = i2c_smbus_read_byte_data(i2c, reg);
Kyungmin Park156f2522010-06-16 09:04:16 +020063 mutex_unlock(&max8998->iolock);
64 if (ret < 0)
65 return ret;
66
67 ret &= 0xff;
68 *dest = ret;
69 return 0;
70}
Joonyoung Shim676e02d2010-08-06 11:28:06 +090071EXPORT_SYMBOL(max8998_read_reg);
Kyungmin Park156f2522010-06-16 09:04:16 +020072
Joonyoung Shim2c7e6f52010-09-10 18:36:39 +020073int max8998_bulk_read(struct i2c_client *i2c, u8 reg, int count, u8 *buf)
74{
75 struct max8998_dev *max8998 = i2c_get_clientdata(i2c);
76 int ret;
77
78 mutex_lock(&max8998->iolock);
79 ret = i2c_smbus_read_i2c_block_data(i2c, reg, count, buf);
80 mutex_unlock(&max8998->iolock);
81 if (ret < 0)
82 return ret;
83
84 return 0;
85}
86EXPORT_SYMBOL(max8998_bulk_read);
87
Joonyoung Shim676e02d2010-08-06 11:28:06 +090088int max8998_write_reg(struct i2c_client *i2c, u8 reg, u8 value)
Kyungmin Park156f2522010-06-16 09:04:16 +020089{
Joonyoung Shim676e02d2010-08-06 11:28:06 +090090 struct max8998_dev *max8998 = i2c_get_clientdata(i2c);
Kyungmin Park156f2522010-06-16 09:04:16 +020091 int ret;
92
93 mutex_lock(&max8998->iolock);
Joonyoung Shim676e02d2010-08-06 11:28:06 +090094 ret = i2c_smbus_write_byte_data(i2c, reg, value);
Kyungmin Park156f2522010-06-16 09:04:16 +020095 mutex_unlock(&max8998->iolock);
96 return ret;
97}
Joonyoung Shim676e02d2010-08-06 11:28:06 +090098EXPORT_SYMBOL(max8998_write_reg);
Kyungmin Park156f2522010-06-16 09:04:16 +020099
Joonyoung Shim9b16c0a2010-08-06 11:28:08 +0900100int max8998_bulk_write(struct i2c_client *i2c, u8 reg, int count, u8 *buf)
101{
102 struct max8998_dev *max8998 = i2c_get_clientdata(i2c);
103 int ret;
104
105 mutex_lock(&max8998->iolock);
106 ret = i2c_smbus_write_i2c_block_data(i2c, reg, count, buf);
107 mutex_unlock(&max8998->iolock);
108 if (ret < 0)
109 return ret;
110
111 return 0;
112}
113EXPORT_SYMBOL(max8998_bulk_write);
114
Joonyoung Shim676e02d2010-08-06 11:28:06 +0900115int max8998_update_reg(struct i2c_client *i2c, u8 reg, u8 val, u8 mask)
Kyungmin Park156f2522010-06-16 09:04:16 +0200116{
Joonyoung Shim676e02d2010-08-06 11:28:06 +0900117 struct max8998_dev *max8998 = i2c_get_clientdata(i2c);
Kyungmin Park156f2522010-06-16 09:04:16 +0200118 int ret;
119
120 mutex_lock(&max8998->iolock);
Joonyoung Shim676e02d2010-08-06 11:28:06 +0900121 ret = i2c_smbus_read_byte_data(i2c, reg);
Kyungmin Park156f2522010-06-16 09:04:16 +0200122 if (ret >= 0) {
123 u8 old_val = ret & 0xff;
124 u8 new_val = (val & mask) | (old_val & (~mask));
Joonyoung Shim676e02d2010-08-06 11:28:06 +0900125 ret = i2c_smbus_write_byte_data(i2c, reg, new_val);
Kyungmin Park156f2522010-06-16 09:04:16 +0200126 }
127 mutex_unlock(&max8998->iolock);
128 return ret;
129}
Joonyoung Shim676e02d2010-08-06 11:28:06 +0900130EXPORT_SYMBOL(max8998_update_reg);
Kyungmin Park156f2522010-06-16 09:04:16 +0200131
Tomasz Figaee999fb2013-06-25 16:08:10 +0200132#ifdef CONFIG_OF
Krzysztof Kozlowskie9205742014-05-13 12:58:48 +0200133static const struct of_device_id max8998_dt_match[] = {
Tomasz Figaee999fb2013-06-25 16:08:10 +0200134 { .compatible = "maxim,max8998", .data = (void *)TYPE_MAX8998 },
135 { .compatible = "national,lp3974", .data = (void *)TYPE_LP3974 },
136 { .compatible = "ti,lp3974", .data = (void *)TYPE_LP3974 },
137 {},
138};
Tomasz Figaee999fb2013-06-25 16:08:10 +0200139#endif
140
141/*
142 * Only the common platform data elements for max8998 are parsed here from the
143 * device tree. Other sub-modules of max8998 such as pmic, rtc and others have
144 * to parse their own platform data elements from device tree.
145 *
146 * The max8998 platform data structure is instantiated here and the drivers for
147 * the sub-modules need not instantiate another instance while parsing their
148 * platform data.
149 */
150static struct max8998_platform_data *max8998_i2c_parse_dt_pdata(
151 struct device *dev)
152{
153 struct max8998_platform_data *pd;
154
155 pd = devm_kzalloc(dev, sizeof(*pd), GFP_KERNEL);
156 if (!pd)
157 return ERR_PTR(-ENOMEM);
158
159 pd->ono = irq_of_parse_and_map(dev->of_node, 1);
160
161 /*
162 * ToDo: the 'wakeup' member in the platform data is more of a linux
163 * specfic information. Hence, there is no binding for that yet and
164 * not parsed here.
165 */
166 return pd;
167}
168
Lee Jones8bace2d2014-02-03 08:22:30 +0000169static inline unsigned long max8998_i2c_get_driver_data(struct i2c_client *i2c,
Tomasz Figaee999fb2013-06-25 16:08:10 +0200170 const struct i2c_device_id *id)
171{
172 if (IS_ENABLED(CONFIG_OF) && i2c->dev.of_node) {
173 const struct of_device_id *match;
174 match = of_match_node(max8998_dt_match, i2c->dev.of_node);
Lee Jones8bace2d2014-02-03 08:22:30 +0000175 return (unsigned long)match->data;
Tomasz Figaee999fb2013-06-25 16:08:10 +0200176 }
177
Lee Jones8bace2d2014-02-03 08:22:30 +0000178 return id->driver_data;
Tomasz Figaee999fb2013-06-25 16:08:10 +0200179}
180
Kyungmin Park156f2522010-06-16 09:04:16 +0200181static int max8998_i2c_probe(struct i2c_client *i2c,
182 const struct i2c_device_id *id)
183{
Jingoo Han334a41c2013-07-30 17:10:05 +0900184 struct max8998_platform_data *pdata = dev_get_platdata(&i2c->dev);
Kyungmin Park156f2522010-06-16 09:04:16 +0200185 struct max8998_dev *max8998;
186 int ret = 0;
187
Jingoo Han0010dd32013-08-20 16:04:42 +0900188 max8998 = devm_kzalloc(&i2c->dev, sizeof(struct max8998_dev),
189 GFP_KERNEL);
Axel Lin8f1f1512010-08-09 14:48:16 +0800190 if (max8998 == NULL)
Kyungmin Park156f2522010-06-16 09:04:16 +0200191 return -ENOMEM;
Kyungmin Park156f2522010-06-16 09:04:16 +0200192
Tomasz Figaee999fb2013-06-25 16:08:10 +0200193 if (IS_ENABLED(CONFIG_OF) && i2c->dev.of_node) {
194 pdata = max8998_i2c_parse_dt_pdata(&i2c->dev);
195 if (IS_ERR(pdata)) {
196 ret = PTR_ERR(pdata);
197 goto err;
198 }
199 }
200
Kyungmin Park156f2522010-06-16 09:04:16 +0200201 i2c_set_clientdata(i2c, max8998);
202 max8998->dev = &i2c->dev;
Joonyoung Shim676e02d2010-08-06 11:28:06 +0900203 max8998->i2c = i2c;
Joonyoung Shim2c7e6f52010-09-10 18:36:39 +0200204 max8998->irq = i2c->irq;
Tomasz Figaee999fb2013-06-25 16:08:10 +0200205 max8998->type = max8998_i2c_get_driver_data(i2c, id);
206 max8998->pdata = pdata;
Joonyoung Shim2c7e6f52010-09-10 18:36:39 +0200207 if (pdata) {
208 max8998->ono = pdata->ono;
209 max8998->irq_base = pdata->irq_base;
MyungJoo Hamcdd137c2010-12-23 17:53:36 +0900210 max8998->wakeup = pdata->wakeup;
Joonyoung Shim2c7e6f52010-09-10 18:36:39 +0200211 }
Kyungmin Park156f2522010-06-16 09:04:16 +0200212 mutex_init(&max8998->iolock);
213
Joonyoung Shim9b16c0a2010-08-06 11:28:08 +0900214 max8998->rtc = i2c_new_dummy(i2c->adapter, RTC_I2C_ADDR);
Krzysztof Kozlowskied26f872014-02-11 11:03:35 +0100215 if (!max8998->rtc) {
216 dev_err(&i2c->dev, "Failed to allocate I2C device for RTC\n");
217 return -ENODEV;
218 }
Joonyoung Shim9b16c0a2010-08-06 11:28:08 +0900219 i2c_set_clientdata(max8998->rtc, max8998);
220
Joonyoung Shim2c7e6f52010-09-10 18:36:39 +0200221 max8998_irq_init(max8998);
222
MyungJoo Hamcdd137c2010-12-23 17:53:36 +0900223 pm_runtime_set_active(max8998->dev);
224
Tomasz Figaee999fb2013-06-25 16:08:10 +0200225 switch (max8998->type) {
MyungJoo Ham337ce5d2011-01-04 14:17:39 +0900226 case TYPE_LP3974:
227 ret = mfd_add_devices(max8998->dev, -1,
Mark Brown0848c942012-09-11 15:16:36 +0800228 lp3974_devs, ARRAY_SIZE(lp3974_devs),
229 NULL, 0, NULL);
MyungJoo Ham337ce5d2011-01-04 14:17:39 +0900230 break;
231 case TYPE_MAX8998:
232 ret = mfd_add_devices(max8998->dev, -1,
Mark Brown0848c942012-09-11 15:16:36 +0800233 max8998_devs, ARRAY_SIZE(max8998_devs),
234 NULL, 0, NULL);
MyungJoo Ham337ce5d2011-01-04 14:17:39 +0900235 break;
236 default:
237 ret = -EINVAL;
238 }
239
Kyungmin Park156f2522010-06-16 09:04:16 +0200240 if (ret < 0)
241 goto err;
242
Jonghwan Choi7ef73592011-11-29 17:17:51 +0900243 device_init_wakeup(max8998->dev, max8998->wakeup);
244
Kyungmin Park156f2522010-06-16 09:04:16 +0200245 return ret;
246
247err:
248 mfd_remove_devices(max8998->dev);
Axel Lin74845522010-10-22 08:31:49 +0800249 max8998_irq_exit(max8998);
250 i2c_unregister_device(max8998->rtc);
Kyungmin Park156f2522010-06-16 09:04:16 +0200251 return ret;
252}
253
Kyungmin Park156f2522010-06-16 09:04:16 +0200254static const struct i2c_device_id max8998_i2c_id[] = {
Lukasz Majewski509bd472010-09-27 14:32:24 +0200255 { "max8998", TYPE_MAX8998 },
256 { "lp3974", TYPE_LP3974},
Kyungmin Parkf8539dd2010-08-23 13:46:49 +0900257 { }
Kyungmin Park156f2522010-06-16 09:04:16 +0200258};
Kyungmin Park156f2522010-06-16 09:04:16 +0200259
MyungJoo Hamcdd137c2010-12-23 17:53:36 +0900260static int max8998_suspend(struct device *dev)
261{
Geliang Tang1b5420e2015-12-28 23:00:14 +0800262 struct i2c_client *i2c = to_i2c_client(dev);
MyungJoo Hamcdd137c2010-12-23 17:53:36 +0900263 struct max8998_dev *max8998 = i2c_get_clientdata(i2c);
264
Jonghwan Choi7ef73592011-11-29 17:17:51 +0900265 if (device_may_wakeup(dev))
Thomas Gleixnerd5bb1222011-03-25 11:12:32 +0000266 irq_set_irq_wake(max8998->irq, 1);
MyungJoo Hamcdd137c2010-12-23 17:53:36 +0900267 return 0;
268}
269
270static int max8998_resume(struct device *dev)
271{
Geliang Tang1b5420e2015-12-28 23:00:14 +0800272 struct i2c_client *i2c = to_i2c_client(dev);
MyungJoo Hamcdd137c2010-12-23 17:53:36 +0900273 struct max8998_dev *max8998 = i2c_get_clientdata(i2c);
274
Jonghwan Choi7ef73592011-11-29 17:17:51 +0900275 if (device_may_wakeup(dev))
Thomas Gleixnerd5bb1222011-03-25 11:12:32 +0000276 irq_set_irq_wake(max8998->irq, 0);
MyungJoo Hamcdd137c2010-12-23 17:53:36 +0900277 /*
278 * In LP3974, if IRQ registers are not "read & clear"
279 * when it's set during sleep, the interrupt becomes
280 * disabled.
281 */
282 return max8998_irq_resume(i2c_get_clientdata(i2c));
283}
284
285struct max8998_reg_dump {
286 u8 addr;
287 u8 val;
288};
289#define SAVE_ITEM(x) { .addr = (x), .val = 0x0, }
Mark Brown44be0a42011-01-20 21:47:31 +0000290static struct max8998_reg_dump max8998_dump[] = {
MyungJoo Hamcdd137c2010-12-23 17:53:36 +0900291 SAVE_ITEM(MAX8998_REG_IRQM1),
292 SAVE_ITEM(MAX8998_REG_IRQM2),
293 SAVE_ITEM(MAX8998_REG_IRQM3),
294 SAVE_ITEM(MAX8998_REG_IRQM4),
295 SAVE_ITEM(MAX8998_REG_STATUSM1),
296 SAVE_ITEM(MAX8998_REG_STATUSM2),
297 SAVE_ITEM(MAX8998_REG_CHGR1),
298 SAVE_ITEM(MAX8998_REG_CHGR2),
299 SAVE_ITEM(MAX8998_REG_LDO_ACTIVE_DISCHARGE1),
300 SAVE_ITEM(MAX8998_REG_LDO_ACTIVE_DISCHARGE1),
301 SAVE_ITEM(MAX8998_REG_BUCK_ACTIVE_DISCHARGE3),
302 SAVE_ITEM(MAX8998_REG_ONOFF1),
303 SAVE_ITEM(MAX8998_REG_ONOFF2),
304 SAVE_ITEM(MAX8998_REG_ONOFF3),
305 SAVE_ITEM(MAX8998_REG_ONOFF4),
306 SAVE_ITEM(MAX8998_REG_BUCK1_VOLTAGE1),
307 SAVE_ITEM(MAX8998_REG_BUCK1_VOLTAGE2),
308 SAVE_ITEM(MAX8998_REG_BUCK1_VOLTAGE3),
309 SAVE_ITEM(MAX8998_REG_BUCK1_VOLTAGE4),
310 SAVE_ITEM(MAX8998_REG_BUCK2_VOLTAGE1),
311 SAVE_ITEM(MAX8998_REG_BUCK2_VOLTAGE2),
312 SAVE_ITEM(MAX8998_REG_LDO2_LDO3),
313 SAVE_ITEM(MAX8998_REG_LDO4),
314 SAVE_ITEM(MAX8998_REG_LDO5),
315 SAVE_ITEM(MAX8998_REG_LDO6),
316 SAVE_ITEM(MAX8998_REG_LDO7),
317 SAVE_ITEM(MAX8998_REG_LDO8_LDO9),
318 SAVE_ITEM(MAX8998_REG_LDO10_LDO11),
319 SAVE_ITEM(MAX8998_REG_LDO12),
320 SAVE_ITEM(MAX8998_REG_LDO13),
321 SAVE_ITEM(MAX8998_REG_LDO14),
322 SAVE_ITEM(MAX8998_REG_LDO15),
323 SAVE_ITEM(MAX8998_REG_LDO16),
324 SAVE_ITEM(MAX8998_REG_LDO17),
325 SAVE_ITEM(MAX8998_REG_BKCHR),
326 SAVE_ITEM(MAX8998_REG_LBCNFG1),
327 SAVE_ITEM(MAX8998_REG_LBCNFG2),
328};
329/* Save registers before hibernation */
330static int max8998_freeze(struct device *dev)
331{
Geliang Tang1b5420e2015-12-28 23:00:14 +0800332 struct i2c_client *i2c = to_i2c_client(dev);
MyungJoo Hamcdd137c2010-12-23 17:53:36 +0900333 int i;
334
335 for (i = 0; i < ARRAY_SIZE(max8998_dump); i++)
336 max8998_read_reg(i2c, max8998_dump[i].addr,
337 &max8998_dump[i].val);
338
339 return 0;
340}
341
342/* Restore registers after hibernation */
343static int max8998_restore(struct device *dev)
344{
Geliang Tang1b5420e2015-12-28 23:00:14 +0800345 struct i2c_client *i2c = to_i2c_client(dev);
MyungJoo Hamcdd137c2010-12-23 17:53:36 +0900346 int i;
347
348 for (i = 0; i < ARRAY_SIZE(max8998_dump); i++)
349 max8998_write_reg(i2c, max8998_dump[i].addr,
350 max8998_dump[i].val);
351
352 return 0;
353}
354
Mark Brown44be0a42011-01-20 21:47:31 +0000355static const struct dev_pm_ops max8998_pm = {
MyungJoo Hamcdd137c2010-12-23 17:53:36 +0900356 .suspend = max8998_suspend,
357 .resume = max8998_resume,
358 .freeze = max8998_freeze,
359 .restore = max8998_restore,
360};
361
Kyungmin Park156f2522010-06-16 09:04:16 +0200362static struct i2c_driver max8998_i2c_driver = {
363 .driver = {
364 .name = "max8998",
MyungJoo Hamcdd137c2010-12-23 17:53:36 +0900365 .pm = &max8998_pm,
Paul Gortmakerb9e38332016-06-02 20:39:48 -0400366 .suppress_bind_attrs = true,
Tomasz Figaee999fb2013-06-25 16:08:10 +0200367 .of_match_table = of_match_ptr(max8998_dt_match),
Kyungmin Park156f2522010-06-16 09:04:16 +0200368 },
369 .probe = max8998_i2c_probe,
Kyungmin Park156f2522010-06-16 09:04:16 +0200370 .id_table = max8998_i2c_id,
371};
372
373static int __init max8998_i2c_init(void)
374{
375 return i2c_add_driver(&max8998_i2c_driver);
376}
377/* init early so consumer devices can complete system boot */
378subsys_initcall(max8998_i2c_init);