blob: 0eb15e611b671f243265e9d74a9ccd3e5573c075 [file] [log] [blame]
Krzysztof Kozlowskid7d8d7a2018-08-07 18:11:23 +02001// SPDX-License-Identifier: GPL-2.0+
2//
3// max8998.c - mfd core driver for the Maxim 8998
4//
5// Copyright (C) 2009-2010 Samsung Electronics
6// Kyungmin Park <kyungmin.park@samsung.com>
7// Marek Szyprowski <m.szyprowski@samsung.com>
Kyungmin Park156f2522010-06-16 09:04:16 +02008
Tomasz Figaee999fb2013-06-25 16:08:10 +02009#include <linux/err.h>
Kyungmin Park156f2522010-06-16 09:04:16 +020010#include <linux/init.h>
11#include <linux/slab.h>
12#include <linux/i2c.h>
MyungJoo Hamcdd137c2010-12-23 17:53:36 +090013#include <linux/interrupt.h>
Tomasz Figaee999fb2013-06-25 16:08:10 +020014#include <linux/of.h>
Krzysztof Kozlowskifac61e62021-05-26 08:47:00 -040015#include <linux/of_device.h>
Tomasz Figaee999fb2013-06-25 16:08:10 +020016#include <linux/of_irq.h>
MyungJoo Hamcdd137c2010-12-23 17:53:36 +090017#include <linux/pm_runtime.h>
Kyungmin Park156f2522010-06-16 09:04:16 +020018#include <linux/mutex.h>
19#include <linux/mfd/core.h>
20#include <linux/mfd/max8998.h>
21#include <linux/mfd/max8998-private.h>
22
Joonyoung Shim9b16c0a2010-08-06 11:28:08 +090023#define RTC_I2C_ADDR (0x0c >> 1)
24
Geert Uytterhoeven7c0517b2013-11-18 14:33:00 +010025static const struct mfd_cell max8998_devs[] = {
Kyungmin Park156f2522010-06-16 09:04:16 +020026 {
27 .name = "max8998-pmic",
Joonyoung Shim9b16c0a2010-08-06 11:28:08 +090028 }, {
29 .name = "max8998-rtc",
Donggeun Kimbb4ce972011-06-24 19:04:18 +090030 }, {
31 .name = "max8998-battery",
Joonyoung Shim9b16c0a2010-08-06 11:28:08 +090032 },
Kyungmin Park156f2522010-06-16 09:04:16 +020033};
34
Geert Uytterhoeven7c0517b2013-11-18 14:33:00 +010035static const struct mfd_cell lp3974_devs[] = {
MyungJoo Ham337ce5d2011-01-04 14:17:39 +090036 {
37 .name = "lp3974-pmic",
38 }, {
39 .name = "lp3974-rtc",
40 },
41};
42
Joonyoung Shim676e02d2010-08-06 11:28:06 +090043int max8998_read_reg(struct i2c_client *i2c, u8 reg, u8 *dest)
Kyungmin Park156f2522010-06-16 09:04:16 +020044{
Joonyoung Shim676e02d2010-08-06 11:28:06 +090045 struct max8998_dev *max8998 = i2c_get_clientdata(i2c);
Kyungmin Park156f2522010-06-16 09:04:16 +020046 int ret;
47
48 mutex_lock(&max8998->iolock);
Joonyoung Shim676e02d2010-08-06 11:28:06 +090049 ret = i2c_smbus_read_byte_data(i2c, reg);
Kyungmin Park156f2522010-06-16 09:04:16 +020050 mutex_unlock(&max8998->iolock);
51 if (ret < 0)
52 return ret;
53
54 ret &= 0xff;
55 *dest = ret;
56 return 0;
57}
Joonyoung Shim676e02d2010-08-06 11:28:06 +090058EXPORT_SYMBOL(max8998_read_reg);
Kyungmin Park156f2522010-06-16 09:04:16 +020059
Joonyoung Shim2c7e6f52010-09-10 18:36:39 +020060int max8998_bulk_read(struct i2c_client *i2c, u8 reg, int count, u8 *buf)
61{
62 struct max8998_dev *max8998 = i2c_get_clientdata(i2c);
63 int ret;
64
65 mutex_lock(&max8998->iolock);
66 ret = i2c_smbus_read_i2c_block_data(i2c, reg, count, buf);
67 mutex_unlock(&max8998->iolock);
68 if (ret < 0)
69 return ret;
70
71 return 0;
72}
73EXPORT_SYMBOL(max8998_bulk_read);
74
Joonyoung Shim676e02d2010-08-06 11:28:06 +090075int max8998_write_reg(struct i2c_client *i2c, u8 reg, u8 value)
Kyungmin Park156f2522010-06-16 09:04:16 +020076{
Joonyoung Shim676e02d2010-08-06 11:28:06 +090077 struct max8998_dev *max8998 = i2c_get_clientdata(i2c);
Kyungmin Park156f2522010-06-16 09:04:16 +020078 int ret;
79
80 mutex_lock(&max8998->iolock);
Joonyoung Shim676e02d2010-08-06 11:28:06 +090081 ret = i2c_smbus_write_byte_data(i2c, reg, value);
Kyungmin Park156f2522010-06-16 09:04:16 +020082 mutex_unlock(&max8998->iolock);
83 return ret;
84}
Joonyoung Shim676e02d2010-08-06 11:28:06 +090085EXPORT_SYMBOL(max8998_write_reg);
Kyungmin Park156f2522010-06-16 09:04:16 +020086
Joonyoung Shim9b16c0a2010-08-06 11:28:08 +090087int max8998_bulk_write(struct i2c_client *i2c, u8 reg, int count, u8 *buf)
88{
89 struct max8998_dev *max8998 = i2c_get_clientdata(i2c);
90 int ret;
91
92 mutex_lock(&max8998->iolock);
93 ret = i2c_smbus_write_i2c_block_data(i2c, reg, count, buf);
94 mutex_unlock(&max8998->iolock);
95 if (ret < 0)
96 return ret;
97
98 return 0;
99}
100EXPORT_SYMBOL(max8998_bulk_write);
101
Joonyoung Shim676e02d2010-08-06 11:28:06 +0900102int max8998_update_reg(struct i2c_client *i2c, u8 reg, u8 val, u8 mask)
Kyungmin Park156f2522010-06-16 09:04:16 +0200103{
Joonyoung Shim676e02d2010-08-06 11:28:06 +0900104 struct max8998_dev *max8998 = i2c_get_clientdata(i2c);
Kyungmin Park156f2522010-06-16 09:04:16 +0200105 int ret;
106
107 mutex_lock(&max8998->iolock);
Joonyoung Shim676e02d2010-08-06 11:28:06 +0900108 ret = i2c_smbus_read_byte_data(i2c, reg);
Kyungmin Park156f2522010-06-16 09:04:16 +0200109 if (ret >= 0) {
110 u8 old_val = ret & 0xff;
111 u8 new_val = (val & mask) | (old_val & (~mask));
Joonyoung Shim676e02d2010-08-06 11:28:06 +0900112 ret = i2c_smbus_write_byte_data(i2c, reg, new_val);
Kyungmin Park156f2522010-06-16 09:04:16 +0200113 }
114 mutex_unlock(&max8998->iolock);
115 return ret;
116}
Joonyoung Shim676e02d2010-08-06 11:28:06 +0900117EXPORT_SYMBOL(max8998_update_reg);
Kyungmin Park156f2522010-06-16 09:04:16 +0200118
Tomasz Figaee999fb2013-06-25 16:08:10 +0200119#ifdef CONFIG_OF
Krzysztof Kozlowskie9205742014-05-13 12:58:48 +0200120static const struct of_device_id max8998_dt_match[] = {
Tomasz Figaee999fb2013-06-25 16:08:10 +0200121 { .compatible = "maxim,max8998", .data = (void *)TYPE_MAX8998 },
122 { .compatible = "national,lp3974", .data = (void *)TYPE_LP3974 },
123 { .compatible = "ti,lp3974", .data = (void *)TYPE_LP3974 },
124 {},
125};
Tomasz Figaee999fb2013-06-25 16:08:10 +0200126#endif
127
128/*
129 * Only the common platform data elements for max8998 are parsed here from the
130 * device tree. Other sub-modules of max8998 such as pmic, rtc and others have
131 * to parse their own platform data elements from device tree.
132 *
133 * The max8998 platform data structure is instantiated here and the drivers for
134 * the sub-modules need not instantiate another instance while parsing their
135 * platform data.
136 */
137static struct max8998_platform_data *max8998_i2c_parse_dt_pdata(
138 struct device *dev)
139{
140 struct max8998_platform_data *pd;
141
142 pd = devm_kzalloc(dev, sizeof(*pd), GFP_KERNEL);
143 if (!pd)
144 return ERR_PTR(-ENOMEM);
145
146 pd->ono = irq_of_parse_and_map(dev->of_node, 1);
147
148 /*
149 * ToDo: the 'wakeup' member in the platform data is more of a linux
150 * specfic information. Hence, there is no binding for that yet and
151 * not parsed here.
152 */
153 return pd;
154}
155
Lee Jones8bace2d2014-02-03 08:22:30 +0000156static inline unsigned long max8998_i2c_get_driver_data(struct i2c_client *i2c,
Tomasz Figaee999fb2013-06-25 16:08:10 +0200157 const struct i2c_device_id *id)
158{
Krzysztof Kozlowskifac61e62021-05-26 08:47:00 -0400159 if (i2c->dev.of_node)
160 return (unsigned long)of_device_get_match_data(&i2c->dev);
Tomasz Figaee999fb2013-06-25 16:08:10 +0200161
Lee Jones8bace2d2014-02-03 08:22:30 +0000162 return id->driver_data;
Tomasz Figaee999fb2013-06-25 16:08:10 +0200163}
164
Kyungmin Park156f2522010-06-16 09:04:16 +0200165static int max8998_i2c_probe(struct i2c_client *i2c,
166 const struct i2c_device_id *id)
167{
Jingoo Han334a41ce2013-07-30 17:10:05 +0900168 struct max8998_platform_data *pdata = dev_get_platdata(&i2c->dev);
Kyungmin Park156f2522010-06-16 09:04:16 +0200169 struct max8998_dev *max8998;
170 int ret = 0;
171
Jingoo Han0010dd32013-08-20 16:04:42 +0900172 max8998 = devm_kzalloc(&i2c->dev, sizeof(struct max8998_dev),
173 GFP_KERNEL);
Axel Lin8f1f1512010-08-09 14:48:16 +0800174 if (max8998 == NULL)
Kyungmin Park156f2522010-06-16 09:04:16 +0200175 return -ENOMEM;
Kyungmin Park156f2522010-06-16 09:04:16 +0200176
Tomasz Figaee999fb2013-06-25 16:08:10 +0200177 if (IS_ENABLED(CONFIG_OF) && i2c->dev.of_node) {
178 pdata = max8998_i2c_parse_dt_pdata(&i2c->dev);
Christophe JAILLET2042f3c2017-07-05 07:50:54 +0200179 if (IS_ERR(pdata))
180 return PTR_ERR(pdata);
Tomasz Figaee999fb2013-06-25 16:08:10 +0200181 }
182
Kyungmin Park156f2522010-06-16 09:04:16 +0200183 i2c_set_clientdata(i2c, max8998);
184 max8998->dev = &i2c->dev;
Joonyoung Shim676e02d2010-08-06 11:28:06 +0900185 max8998->i2c = i2c;
Joonyoung Shim2c7e6f52010-09-10 18:36:39 +0200186 max8998->irq = i2c->irq;
Tomasz Figaee999fb2013-06-25 16:08:10 +0200187 max8998->type = max8998_i2c_get_driver_data(i2c, id);
188 max8998->pdata = pdata;
Joonyoung Shim2c7e6f52010-09-10 18:36:39 +0200189 if (pdata) {
190 max8998->ono = pdata->ono;
191 max8998->irq_base = pdata->irq_base;
MyungJoo Hamcdd137c2010-12-23 17:53:36 +0900192 max8998->wakeup = pdata->wakeup;
Joonyoung Shim2c7e6f52010-09-10 18:36:39 +0200193 }
Kyungmin Park156f2522010-06-16 09:04:16 +0200194 mutex_init(&max8998->iolock);
195
Wolfram Sang7a99c8f2019-07-22 19:26:19 +0200196 max8998->rtc = i2c_new_dummy_device(i2c->adapter, RTC_I2C_ADDR);
197 if (IS_ERR(max8998->rtc)) {
Krzysztof Kozlowskied26f872014-02-11 11:03:35 +0100198 dev_err(&i2c->dev, "Failed to allocate I2C device for RTC\n");
Wolfram Sang7a99c8f2019-07-22 19:26:19 +0200199 return PTR_ERR(max8998->rtc);
Krzysztof Kozlowskied26f872014-02-11 11:03:35 +0100200 }
Joonyoung Shim9b16c0a2010-08-06 11:28:08 +0900201 i2c_set_clientdata(max8998->rtc, max8998);
202
Joonyoung Shim2c7e6f52010-09-10 18:36:39 +0200203 max8998_irq_init(max8998);
204
MyungJoo Hamcdd137c2010-12-23 17:53:36 +0900205 pm_runtime_set_active(max8998->dev);
206
Tomasz Figaee999fb2013-06-25 16:08:10 +0200207 switch (max8998->type) {
MyungJoo Ham337ce5d2011-01-04 14:17:39 +0900208 case TYPE_LP3974:
209 ret = mfd_add_devices(max8998->dev, -1,
Mark Brown0848c942012-09-11 15:16:36 +0800210 lp3974_devs, ARRAY_SIZE(lp3974_devs),
211 NULL, 0, NULL);
MyungJoo Ham337ce5d2011-01-04 14:17:39 +0900212 break;
213 case TYPE_MAX8998:
214 ret = mfd_add_devices(max8998->dev, -1,
Mark Brown0848c942012-09-11 15:16:36 +0800215 max8998_devs, ARRAY_SIZE(max8998_devs),
216 NULL, 0, NULL);
MyungJoo Ham337ce5d2011-01-04 14:17:39 +0900217 break;
218 default:
219 ret = -EINVAL;
220 }
221
Kyungmin Park156f2522010-06-16 09:04:16 +0200222 if (ret < 0)
223 goto err;
224
Jonghwan Choi7ef73592011-11-29 17:17:51 +0900225 device_init_wakeup(max8998->dev, max8998->wakeup);
226
Kyungmin Park156f2522010-06-16 09:04:16 +0200227 return ret;
228
229err:
230 mfd_remove_devices(max8998->dev);
Axel Lin74845522010-10-22 08:31:49 +0800231 max8998_irq_exit(max8998);
232 i2c_unregister_device(max8998->rtc);
Kyungmin Park156f2522010-06-16 09:04:16 +0200233 return ret;
234}
235
Kyungmin Park156f2522010-06-16 09:04:16 +0200236static const struct i2c_device_id max8998_i2c_id[] = {
Lukasz Majewski509bd472010-09-27 14:32:24 +0200237 { "max8998", TYPE_MAX8998 },
238 { "lp3974", TYPE_LP3974},
Kyungmin Parkf8539dd2010-08-23 13:46:49 +0900239 { }
Kyungmin Park156f2522010-06-16 09:04:16 +0200240};
Kyungmin Park156f2522010-06-16 09:04:16 +0200241
MyungJoo Hamcdd137c2010-12-23 17:53:36 +0900242static int max8998_suspend(struct device *dev)
243{
Geliang Tang1b5420e2015-12-28 23:00:14 +0800244 struct i2c_client *i2c = to_i2c_client(dev);
MyungJoo Hamcdd137c2010-12-23 17:53:36 +0900245 struct max8998_dev *max8998 = i2c_get_clientdata(i2c);
246
Jonghwan Choi7ef73592011-11-29 17:17:51 +0900247 if (device_may_wakeup(dev))
Thomas Gleixnerd5bb1222011-03-25 11:12:32 +0000248 irq_set_irq_wake(max8998->irq, 1);
MyungJoo Hamcdd137c2010-12-23 17:53:36 +0900249 return 0;
250}
251
252static int max8998_resume(struct device *dev)
253{
Geliang Tang1b5420e2015-12-28 23:00:14 +0800254 struct i2c_client *i2c = to_i2c_client(dev);
MyungJoo Hamcdd137c2010-12-23 17:53:36 +0900255 struct max8998_dev *max8998 = i2c_get_clientdata(i2c);
256
Jonghwan Choi7ef73592011-11-29 17:17:51 +0900257 if (device_may_wakeup(dev))
Thomas Gleixnerd5bb1222011-03-25 11:12:32 +0000258 irq_set_irq_wake(max8998->irq, 0);
MyungJoo Hamcdd137c2010-12-23 17:53:36 +0900259 /*
260 * In LP3974, if IRQ registers are not "read & clear"
261 * when it's set during sleep, the interrupt becomes
262 * disabled.
263 */
264 return max8998_irq_resume(i2c_get_clientdata(i2c));
265}
266
267struct max8998_reg_dump {
268 u8 addr;
269 u8 val;
270};
271#define SAVE_ITEM(x) { .addr = (x), .val = 0x0, }
Mark Brown44be0a42011-01-20 21:47:31 +0000272static struct max8998_reg_dump max8998_dump[] = {
MyungJoo Hamcdd137c2010-12-23 17:53:36 +0900273 SAVE_ITEM(MAX8998_REG_IRQM1),
274 SAVE_ITEM(MAX8998_REG_IRQM2),
275 SAVE_ITEM(MAX8998_REG_IRQM3),
276 SAVE_ITEM(MAX8998_REG_IRQM4),
277 SAVE_ITEM(MAX8998_REG_STATUSM1),
278 SAVE_ITEM(MAX8998_REG_STATUSM2),
279 SAVE_ITEM(MAX8998_REG_CHGR1),
280 SAVE_ITEM(MAX8998_REG_CHGR2),
281 SAVE_ITEM(MAX8998_REG_LDO_ACTIVE_DISCHARGE1),
282 SAVE_ITEM(MAX8998_REG_LDO_ACTIVE_DISCHARGE1),
283 SAVE_ITEM(MAX8998_REG_BUCK_ACTIVE_DISCHARGE3),
284 SAVE_ITEM(MAX8998_REG_ONOFF1),
285 SAVE_ITEM(MAX8998_REG_ONOFF2),
286 SAVE_ITEM(MAX8998_REG_ONOFF3),
287 SAVE_ITEM(MAX8998_REG_ONOFF4),
288 SAVE_ITEM(MAX8998_REG_BUCK1_VOLTAGE1),
289 SAVE_ITEM(MAX8998_REG_BUCK1_VOLTAGE2),
290 SAVE_ITEM(MAX8998_REG_BUCK1_VOLTAGE3),
291 SAVE_ITEM(MAX8998_REG_BUCK1_VOLTAGE4),
292 SAVE_ITEM(MAX8998_REG_BUCK2_VOLTAGE1),
293 SAVE_ITEM(MAX8998_REG_BUCK2_VOLTAGE2),
294 SAVE_ITEM(MAX8998_REG_LDO2_LDO3),
295 SAVE_ITEM(MAX8998_REG_LDO4),
296 SAVE_ITEM(MAX8998_REG_LDO5),
297 SAVE_ITEM(MAX8998_REG_LDO6),
298 SAVE_ITEM(MAX8998_REG_LDO7),
299 SAVE_ITEM(MAX8998_REG_LDO8_LDO9),
300 SAVE_ITEM(MAX8998_REG_LDO10_LDO11),
301 SAVE_ITEM(MAX8998_REG_LDO12),
302 SAVE_ITEM(MAX8998_REG_LDO13),
303 SAVE_ITEM(MAX8998_REG_LDO14),
304 SAVE_ITEM(MAX8998_REG_LDO15),
305 SAVE_ITEM(MAX8998_REG_LDO16),
306 SAVE_ITEM(MAX8998_REG_LDO17),
307 SAVE_ITEM(MAX8998_REG_BKCHR),
308 SAVE_ITEM(MAX8998_REG_LBCNFG1),
309 SAVE_ITEM(MAX8998_REG_LBCNFG2),
310};
311/* Save registers before hibernation */
312static int max8998_freeze(struct device *dev)
313{
Geliang Tang1b5420e2015-12-28 23:00:14 +0800314 struct i2c_client *i2c = to_i2c_client(dev);
MyungJoo Hamcdd137c2010-12-23 17:53:36 +0900315 int i;
316
317 for (i = 0; i < ARRAY_SIZE(max8998_dump); i++)
318 max8998_read_reg(i2c, max8998_dump[i].addr,
319 &max8998_dump[i].val);
320
321 return 0;
322}
323
324/* Restore registers after hibernation */
325static int max8998_restore(struct device *dev)
326{
Geliang Tang1b5420e2015-12-28 23:00:14 +0800327 struct i2c_client *i2c = to_i2c_client(dev);
MyungJoo Hamcdd137c2010-12-23 17:53:36 +0900328 int i;
329
330 for (i = 0; i < ARRAY_SIZE(max8998_dump); i++)
331 max8998_write_reg(i2c, max8998_dump[i].addr,
332 max8998_dump[i].val);
333
334 return 0;
335}
336
Mark Brown44be0a42011-01-20 21:47:31 +0000337static const struct dev_pm_ops max8998_pm = {
MyungJoo Hamcdd137c2010-12-23 17:53:36 +0900338 .suspend = max8998_suspend,
339 .resume = max8998_resume,
340 .freeze = max8998_freeze,
341 .restore = max8998_restore,
342};
343
Kyungmin Park156f2522010-06-16 09:04:16 +0200344static struct i2c_driver max8998_i2c_driver = {
345 .driver = {
346 .name = "max8998",
MyungJoo Hamcdd137c2010-12-23 17:53:36 +0900347 .pm = &max8998_pm,
Paul Gortmakerb9e38332016-06-02 20:39:48 -0400348 .suppress_bind_attrs = true,
Tomasz Figaee999fb2013-06-25 16:08:10 +0200349 .of_match_table = of_match_ptr(max8998_dt_match),
Kyungmin Park156f2522010-06-16 09:04:16 +0200350 },
351 .probe = max8998_i2c_probe,
Kyungmin Park156f2522010-06-16 09:04:16 +0200352 .id_table = max8998_i2c_id,
353};
354
355static int __init max8998_i2c_init(void)
356{
357 return i2c_add_driver(&max8998_i2c_driver);
358}
359/* init early so consumer devices can complete system boot */
360subsys_initcall(max8998_i2c_init);