2 * max8998.c - mfd core driver for the Maxim 8998
4 * Copyright (C) 2009-2010 Samsung Electronics
5 * Kyungmin Park <kyungmin.park@samsung.com>
6 * Marek Szyprowski <m.szyprowski@samsung.com>
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.
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.
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
23 #include <linux/err.h>
24 #include <linux/module.h>
25 #include <linux/moduleparam.h>
26 #include <linux/init.h>
27 #include <linux/slab.h>
28 #include <linux/i2c.h>
29 #include <linux/interrupt.h>
31 #include <linux/of_irq.h>
32 #include <linux/pm_runtime.h>
33 #include <linux/mutex.h>
34 #include <linux/mfd/core.h>
35 #include <linux/mfd/max8998.h>
36 #include <linux/mfd/max8998-private.h>
38 #define RTC_I2C_ADDR (0x0c >> 1)
40 static struct mfd_cell max8998_devs
[] = {
42 .name
= "max8998-pmic",
44 .name
= "max8998-rtc",
46 .name
= "max8998-battery",
50 static struct mfd_cell lp3974_devs
[] = {
52 .name
= "lp3974-pmic",
58 int max8998_read_reg(struct i2c_client
*i2c
, u8 reg
, u8
*dest
)
60 struct max8998_dev
*max8998
= i2c_get_clientdata(i2c
);
63 mutex_lock(&max8998
->iolock
);
64 ret
= i2c_smbus_read_byte_data(i2c
, reg
);
65 mutex_unlock(&max8998
->iolock
);
73 EXPORT_SYMBOL(max8998_read_reg
);
75 int max8998_bulk_read(struct i2c_client
*i2c
, u8 reg
, int count
, u8
*buf
)
77 struct max8998_dev
*max8998
= i2c_get_clientdata(i2c
);
80 mutex_lock(&max8998
->iolock
);
81 ret
= i2c_smbus_read_i2c_block_data(i2c
, reg
, count
, buf
);
82 mutex_unlock(&max8998
->iolock
);
88 EXPORT_SYMBOL(max8998_bulk_read
);
90 int max8998_write_reg(struct i2c_client
*i2c
, u8 reg
, u8 value
)
92 struct max8998_dev
*max8998
= i2c_get_clientdata(i2c
);
95 mutex_lock(&max8998
->iolock
);
96 ret
= i2c_smbus_write_byte_data(i2c
, reg
, value
);
97 mutex_unlock(&max8998
->iolock
);
100 EXPORT_SYMBOL(max8998_write_reg
);
102 int max8998_bulk_write(struct i2c_client
*i2c
, u8 reg
, int count
, u8
*buf
)
104 struct max8998_dev
*max8998
= i2c_get_clientdata(i2c
);
107 mutex_lock(&max8998
->iolock
);
108 ret
= i2c_smbus_write_i2c_block_data(i2c
, reg
, count
, buf
);
109 mutex_unlock(&max8998
->iolock
);
115 EXPORT_SYMBOL(max8998_bulk_write
);
117 int max8998_update_reg(struct i2c_client
*i2c
, u8 reg
, u8 val
, u8 mask
)
119 struct max8998_dev
*max8998
= i2c_get_clientdata(i2c
);
122 mutex_lock(&max8998
->iolock
);
123 ret
= i2c_smbus_read_byte_data(i2c
, reg
);
125 u8 old_val
= ret
& 0xff;
126 u8 new_val
= (val
& mask
) | (old_val
& (~mask
));
127 ret
= i2c_smbus_write_byte_data(i2c
, reg
, new_val
);
129 mutex_unlock(&max8998
->iolock
);
132 EXPORT_SYMBOL(max8998_update_reg
);
135 static struct of_device_id max8998_dt_match
[] = {
136 { .compatible
= "maxim,max8998", .data
= (void *)TYPE_MAX8998
},
137 { .compatible
= "national,lp3974", .data
= (void *)TYPE_LP3974
},
138 { .compatible
= "ti,lp3974", .data
= (void *)TYPE_LP3974
},
141 MODULE_DEVICE_TABLE(of
, max8998_dt_match
);
145 * Only the common platform data elements for max8998 are parsed here from the
146 * device tree. Other sub-modules of max8998 such as pmic, rtc and others have
147 * to parse their own platform data elements from device tree.
149 * The max8998 platform data structure is instantiated here and the drivers for
150 * the sub-modules need not instantiate another instance while parsing their
153 static struct max8998_platform_data
*max8998_i2c_parse_dt_pdata(
156 struct max8998_platform_data
*pd
;
158 pd
= devm_kzalloc(dev
, sizeof(*pd
), GFP_KERNEL
);
160 return ERR_PTR(-ENOMEM
);
162 pd
->ono
= irq_of_parse_and_map(dev
->of_node
, 1);
165 * ToDo: the 'wakeup' member in the platform data is more of a linux
166 * specfic information. Hence, there is no binding for that yet and
172 static inline int max8998_i2c_get_driver_data(struct i2c_client
*i2c
,
173 const struct i2c_device_id
*id
)
175 if (IS_ENABLED(CONFIG_OF
) && i2c
->dev
.of_node
) {
176 const struct of_device_id
*match
;
177 match
= of_match_node(max8998_dt_match
, i2c
->dev
.of_node
);
178 return (int)match
->data
;
181 return (int)id
->driver_data
;
184 static int max8998_i2c_probe(struct i2c_client
*i2c
,
185 const struct i2c_device_id
*id
)
187 struct max8998_platform_data
*pdata
= i2c
->dev
.platform_data
;
188 struct max8998_dev
*max8998
;
191 max8998
= kzalloc(sizeof(struct max8998_dev
), GFP_KERNEL
);
195 if (IS_ENABLED(CONFIG_OF
) && i2c
->dev
.of_node
) {
196 pdata
= max8998_i2c_parse_dt_pdata(&i2c
->dev
);
198 ret
= PTR_ERR(pdata
);
203 i2c_set_clientdata(i2c
, max8998
);
204 max8998
->dev
= &i2c
->dev
;
206 max8998
->irq
= i2c
->irq
;
207 max8998
->type
= max8998_i2c_get_driver_data(i2c
, id
);
208 max8998
->pdata
= pdata
;
210 max8998
->ono
= pdata
->ono
;
211 max8998
->irq_base
= pdata
->irq_base
;
212 max8998
->wakeup
= pdata
->wakeup
;
214 mutex_init(&max8998
->iolock
);
216 max8998
->rtc
= i2c_new_dummy(i2c
->adapter
, RTC_I2C_ADDR
);
217 i2c_set_clientdata(max8998
->rtc
, max8998
);
219 max8998_irq_init(max8998
);
221 pm_runtime_set_active(max8998
->dev
);
223 switch (max8998
->type
) {
225 ret
= mfd_add_devices(max8998
->dev
, -1,
226 lp3974_devs
, ARRAY_SIZE(lp3974_devs
),
230 ret
= mfd_add_devices(max8998
->dev
, -1,
231 max8998_devs
, ARRAY_SIZE(max8998_devs
),
241 device_init_wakeup(max8998
->dev
, max8998
->wakeup
);
246 mfd_remove_devices(max8998
->dev
);
247 max8998_irq_exit(max8998
);
248 i2c_unregister_device(max8998
->rtc
);
253 static int max8998_i2c_remove(struct i2c_client
*i2c
)
255 struct max8998_dev
*max8998
= i2c_get_clientdata(i2c
);
257 mfd_remove_devices(max8998
->dev
);
258 max8998_irq_exit(max8998
);
259 i2c_unregister_device(max8998
->rtc
);
265 static const struct i2c_device_id max8998_i2c_id
[] = {
266 { "max8998", TYPE_MAX8998
},
267 { "lp3974", TYPE_LP3974
},
270 MODULE_DEVICE_TABLE(i2c
, max8998_i2c_id
);
272 static int max8998_suspend(struct device
*dev
)
274 struct i2c_client
*i2c
= container_of(dev
, struct i2c_client
, dev
);
275 struct max8998_dev
*max8998
= i2c_get_clientdata(i2c
);
277 if (device_may_wakeup(dev
))
278 irq_set_irq_wake(max8998
->irq
, 1);
282 static int max8998_resume(struct device
*dev
)
284 struct i2c_client
*i2c
= container_of(dev
, struct i2c_client
, dev
);
285 struct max8998_dev
*max8998
= i2c_get_clientdata(i2c
);
287 if (device_may_wakeup(dev
))
288 irq_set_irq_wake(max8998
->irq
, 0);
290 * In LP3974, if IRQ registers are not "read & clear"
291 * when it's set during sleep, the interrupt becomes
294 return max8998_irq_resume(i2c_get_clientdata(i2c
));
297 struct max8998_reg_dump
{
301 #define SAVE_ITEM(x) { .addr = (x), .val = 0x0, }
302 static struct max8998_reg_dump max8998_dump
[] = {
303 SAVE_ITEM(MAX8998_REG_IRQM1
),
304 SAVE_ITEM(MAX8998_REG_IRQM2
),
305 SAVE_ITEM(MAX8998_REG_IRQM3
),
306 SAVE_ITEM(MAX8998_REG_IRQM4
),
307 SAVE_ITEM(MAX8998_REG_STATUSM1
),
308 SAVE_ITEM(MAX8998_REG_STATUSM2
),
309 SAVE_ITEM(MAX8998_REG_CHGR1
),
310 SAVE_ITEM(MAX8998_REG_CHGR2
),
311 SAVE_ITEM(MAX8998_REG_LDO_ACTIVE_DISCHARGE1
),
312 SAVE_ITEM(MAX8998_REG_LDO_ACTIVE_DISCHARGE1
),
313 SAVE_ITEM(MAX8998_REG_BUCK_ACTIVE_DISCHARGE3
),
314 SAVE_ITEM(MAX8998_REG_ONOFF1
),
315 SAVE_ITEM(MAX8998_REG_ONOFF2
),
316 SAVE_ITEM(MAX8998_REG_ONOFF3
),
317 SAVE_ITEM(MAX8998_REG_ONOFF4
),
318 SAVE_ITEM(MAX8998_REG_BUCK1_VOLTAGE1
),
319 SAVE_ITEM(MAX8998_REG_BUCK1_VOLTAGE2
),
320 SAVE_ITEM(MAX8998_REG_BUCK1_VOLTAGE3
),
321 SAVE_ITEM(MAX8998_REG_BUCK1_VOLTAGE4
),
322 SAVE_ITEM(MAX8998_REG_BUCK2_VOLTAGE1
),
323 SAVE_ITEM(MAX8998_REG_BUCK2_VOLTAGE2
),
324 SAVE_ITEM(MAX8998_REG_LDO2_LDO3
),
325 SAVE_ITEM(MAX8998_REG_LDO4
),
326 SAVE_ITEM(MAX8998_REG_LDO5
),
327 SAVE_ITEM(MAX8998_REG_LDO6
),
328 SAVE_ITEM(MAX8998_REG_LDO7
),
329 SAVE_ITEM(MAX8998_REG_LDO8_LDO9
),
330 SAVE_ITEM(MAX8998_REG_LDO10_LDO11
),
331 SAVE_ITEM(MAX8998_REG_LDO12
),
332 SAVE_ITEM(MAX8998_REG_LDO13
),
333 SAVE_ITEM(MAX8998_REG_LDO14
),
334 SAVE_ITEM(MAX8998_REG_LDO15
),
335 SAVE_ITEM(MAX8998_REG_LDO16
),
336 SAVE_ITEM(MAX8998_REG_LDO17
),
337 SAVE_ITEM(MAX8998_REG_BKCHR
),
338 SAVE_ITEM(MAX8998_REG_LBCNFG1
),
339 SAVE_ITEM(MAX8998_REG_LBCNFG2
),
341 /* Save registers before hibernation */
342 static int max8998_freeze(struct device
*dev
)
344 struct i2c_client
*i2c
= container_of(dev
, struct i2c_client
, dev
);
347 for (i
= 0; i
< ARRAY_SIZE(max8998_dump
); i
++)
348 max8998_read_reg(i2c
, max8998_dump
[i
].addr
,
349 &max8998_dump
[i
].val
);
354 /* Restore registers after hibernation */
355 static int max8998_restore(struct device
*dev
)
357 struct i2c_client
*i2c
= container_of(dev
, struct i2c_client
, dev
);
360 for (i
= 0; i
< ARRAY_SIZE(max8998_dump
); i
++)
361 max8998_write_reg(i2c
, max8998_dump
[i
].addr
,
362 max8998_dump
[i
].val
);
367 static const struct dev_pm_ops max8998_pm
= {
368 .suspend
= max8998_suspend
,
369 .resume
= max8998_resume
,
370 .freeze
= max8998_freeze
,
371 .restore
= max8998_restore
,
374 static struct i2c_driver max8998_i2c_driver
= {
377 .owner
= THIS_MODULE
,
379 .of_match_table
= of_match_ptr(max8998_dt_match
),
381 .probe
= max8998_i2c_probe
,
382 .remove
= max8998_i2c_remove
,
383 .id_table
= max8998_i2c_id
,
386 static int __init
max8998_i2c_init(void)
388 return i2c_add_driver(&max8998_i2c_driver
);
390 /* init early so consumer devices can complete system boot */
391 subsys_initcall(max8998_i2c_init
);
393 static void __exit
max8998_i2c_exit(void)
395 i2c_del_driver(&max8998_i2c_driver
);
397 module_exit(max8998_i2c_exit
);
399 MODULE_DESCRIPTION("MAXIM 8998 multi-function core driver");
400 MODULE_AUTHOR("Kyungmin Park <kyungmin.park@samsung.com>");
401 MODULE_LICENSE("GPL");