2 * max8997.c - mfd core driver for the Maxim 8966 and 8997
4 * Copyright (C) 2011 Samsung Electronics
5 * MyungJoo Ham <myungjoo.ham@samsung.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 * This driver is based on max8998.c
24 #include <linux/err.h>
25 #include <linux/slab.h>
26 #include <linux/i2c.h>
28 #include <linux/of_irq.h>
29 #include <linux/interrupt.h>
30 #include <linux/pm_runtime.h>
31 #include <linux/init.h>
32 #include <linux/mutex.h>
33 #include <linux/mfd/core.h>
34 #include <linux/mfd/max8997.h>
35 #include <linux/mfd/max8997-private.h>
37 #define I2C_ADDR_PMIC (0xCC >> 1)
38 #define I2C_ADDR_MUIC (0x4A >> 1)
39 #define I2C_ADDR_BATTERY (0x6C >> 1)
40 #define I2C_ADDR_RTC (0x0C >> 1)
41 #define I2C_ADDR_HAPTIC (0x90 >> 1)
43 static const struct mfd_cell max8997_devs
[] = {
44 { .name
= "max8997-pmic", },
45 { .name
= "max8997-rtc", },
46 { .name
= "max8997-battery", },
47 { .name
= "max8997-haptic", },
48 { .name
= "max8997-muic", },
49 { .name
= "max8997-led", .id
= 1 },
50 { .name
= "max8997-led", .id
= 2 },
54 static const struct of_device_id max8997_pmic_dt_match
[] = {
55 { .compatible
= "maxim,max8997-pmic", .data
= (void *)TYPE_MAX8997
},
60 int max8997_read_reg(struct i2c_client
*i2c
, u8 reg
, u8
*dest
)
62 struct max8997_dev
*max8997
= i2c_get_clientdata(i2c
);
65 mutex_lock(&max8997
->iolock
);
66 ret
= i2c_smbus_read_byte_data(i2c
, reg
);
67 mutex_unlock(&max8997
->iolock
);
75 EXPORT_SYMBOL_GPL(max8997_read_reg
);
77 int max8997_bulk_read(struct i2c_client
*i2c
, u8 reg
, int count
, u8
*buf
)
79 struct max8997_dev
*max8997
= i2c_get_clientdata(i2c
);
82 mutex_lock(&max8997
->iolock
);
83 ret
= i2c_smbus_read_i2c_block_data(i2c
, reg
, count
, buf
);
84 mutex_unlock(&max8997
->iolock
);
90 EXPORT_SYMBOL_GPL(max8997_bulk_read
);
92 int max8997_write_reg(struct i2c_client
*i2c
, u8 reg
, u8 value
)
94 struct max8997_dev
*max8997
= i2c_get_clientdata(i2c
);
97 mutex_lock(&max8997
->iolock
);
98 ret
= i2c_smbus_write_byte_data(i2c
, reg
, value
);
99 mutex_unlock(&max8997
->iolock
);
102 EXPORT_SYMBOL_GPL(max8997_write_reg
);
104 int max8997_bulk_write(struct i2c_client
*i2c
, u8 reg
, int count
, u8
*buf
)
106 struct max8997_dev
*max8997
= i2c_get_clientdata(i2c
);
109 mutex_lock(&max8997
->iolock
);
110 ret
= i2c_smbus_write_i2c_block_data(i2c
, reg
, count
, buf
);
111 mutex_unlock(&max8997
->iolock
);
117 EXPORT_SYMBOL_GPL(max8997_bulk_write
);
119 int max8997_update_reg(struct i2c_client
*i2c
, u8 reg
, u8 val
, u8 mask
)
121 struct max8997_dev
*max8997
= i2c_get_clientdata(i2c
);
124 mutex_lock(&max8997
->iolock
);
125 ret
= i2c_smbus_read_byte_data(i2c
, reg
);
127 u8 old_val
= ret
& 0xff;
128 u8 new_val
= (val
& mask
) | (old_val
& (~mask
));
129 ret
= i2c_smbus_write_byte_data(i2c
, reg
, new_val
);
131 mutex_unlock(&max8997
->iolock
);
134 EXPORT_SYMBOL_GPL(max8997_update_reg
);
137 * Only the common platform data elements for max8997 are parsed here from the
138 * device tree. Other sub-modules of max8997 such as pmic, rtc and others have
139 * to parse their own platform data elements from device tree.
141 * The max8997 platform data structure is instantiated here and the drivers for
142 * the sub-modules need not instantiate another instance while parsing their
145 static struct max8997_platform_data
*max8997_i2c_parse_dt_pdata(
148 struct max8997_platform_data
*pd
;
150 pd
= devm_kzalloc(dev
, sizeof(*pd
), GFP_KERNEL
);
152 return ERR_PTR(-ENOMEM
);
154 pd
->ono
= irq_of_parse_and_map(dev
->of_node
, 1);
159 static inline unsigned long max8997_i2c_get_driver_data(struct i2c_client
*i2c
,
160 const struct i2c_device_id
*id
)
162 if (IS_ENABLED(CONFIG_OF
) && i2c
->dev
.of_node
) {
163 const struct of_device_id
*match
;
164 match
= of_match_node(max8997_pmic_dt_match
, i2c
->dev
.of_node
);
165 return (unsigned long)match
->data
;
167 return id
->driver_data
;
170 static int max8997_i2c_probe(struct i2c_client
*i2c
,
171 const struct i2c_device_id
*id
)
173 struct max8997_dev
*max8997
;
174 struct max8997_platform_data
*pdata
= dev_get_platdata(&i2c
->dev
);
177 max8997
= devm_kzalloc(&i2c
->dev
, sizeof(struct max8997_dev
),
182 i2c_set_clientdata(i2c
, max8997
);
183 max8997
->dev
= &i2c
->dev
;
185 max8997
->type
= max8997_i2c_get_driver_data(i2c
, id
);
186 max8997
->irq
= i2c
->irq
;
188 if (IS_ENABLED(CONFIG_OF
) && max8997
->dev
->of_node
) {
189 pdata
= max8997_i2c_parse_dt_pdata(max8997
->dev
);
191 return PTR_ERR(pdata
);
197 max8997
->pdata
= pdata
;
198 max8997
->ono
= pdata
->ono
;
200 mutex_init(&max8997
->iolock
);
202 max8997
->rtc
= i2c_new_dummy(i2c
->adapter
, I2C_ADDR_RTC
);
204 dev_err(max8997
->dev
, "Failed to allocate I2C device for RTC\n");
207 i2c_set_clientdata(max8997
->rtc
, max8997
);
209 max8997
->haptic
= i2c_new_dummy(i2c
->adapter
, I2C_ADDR_HAPTIC
);
210 if (!max8997
->haptic
) {
211 dev_err(max8997
->dev
, "Failed to allocate I2C device for Haptic\n");
215 i2c_set_clientdata(max8997
->haptic
, max8997
);
217 max8997
->muic
= i2c_new_dummy(i2c
->adapter
, I2C_ADDR_MUIC
);
218 if (!max8997
->muic
) {
219 dev_err(max8997
->dev
, "Failed to allocate I2C device for MUIC\n");
223 i2c_set_clientdata(max8997
->muic
, max8997
);
225 pm_runtime_set_active(max8997
->dev
);
227 max8997_irq_init(max8997
);
229 ret
= mfd_add_devices(max8997
->dev
, -1, max8997_devs
,
230 ARRAY_SIZE(max8997_devs
),
233 dev_err(max8997
->dev
, "failed to add MFD devices %d\n", ret
);
238 * TODO: enable others (flash, muic, rtc, battery, ...) and
239 * check the return value
242 /* MAX8997 has a power button input. */
243 device_init_wakeup(max8997
->dev
, true);
248 mfd_remove_devices(max8997
->dev
);
249 i2c_unregister_device(max8997
->muic
);
251 i2c_unregister_device(max8997
->haptic
);
253 i2c_unregister_device(max8997
->rtc
);
257 static const struct i2c_device_id max8997_i2c_id
[] = {
258 { "max8997", TYPE_MAX8997
},
259 { "max8966", TYPE_MAX8966
},
263 static u8 max8997_dumpaddr_pmic
[] = {
268 MAX8997_REG_MAINCON1
,
269 MAX8997_REG_MAINCON2
,
270 MAX8997_REG_BUCKRAMP
,
271 MAX8997_REG_BUCK1CTRL
,
272 MAX8997_REG_BUCK1DVS1
,
273 MAX8997_REG_BUCK1DVS2
,
274 MAX8997_REG_BUCK1DVS3
,
275 MAX8997_REG_BUCK1DVS4
,
276 MAX8997_REG_BUCK1DVS5
,
277 MAX8997_REG_BUCK1DVS6
,
278 MAX8997_REG_BUCK1DVS7
,
279 MAX8997_REG_BUCK1DVS8
,
280 MAX8997_REG_BUCK2CTRL
,
281 MAX8997_REG_BUCK2DVS1
,
282 MAX8997_REG_BUCK2DVS2
,
283 MAX8997_REG_BUCK2DVS3
,
284 MAX8997_REG_BUCK2DVS4
,
285 MAX8997_REG_BUCK2DVS5
,
286 MAX8997_REG_BUCK2DVS6
,
287 MAX8997_REG_BUCK2DVS7
,
288 MAX8997_REG_BUCK2DVS8
,
289 MAX8997_REG_BUCK3CTRL
,
290 MAX8997_REG_BUCK3DVS
,
291 MAX8997_REG_BUCK4CTRL
,
292 MAX8997_REG_BUCK4DVS
,
293 MAX8997_REG_BUCK5CTRL
,
294 MAX8997_REG_BUCK5DVS1
,
295 MAX8997_REG_BUCK5DVS2
,
296 MAX8997_REG_BUCK5DVS3
,
297 MAX8997_REG_BUCK5DVS4
,
298 MAX8997_REG_BUCK5DVS5
,
299 MAX8997_REG_BUCK5DVS6
,
300 MAX8997_REG_BUCK5DVS7
,
301 MAX8997_REG_BUCK5DVS8
,
302 MAX8997_REG_BUCK6CTRL
,
303 MAX8997_REG_BUCK6BPSKIPCTRL
,
304 MAX8997_REG_BUCK7CTRL
,
305 MAX8997_REG_BUCK7DVS
,
306 MAX8997_REG_LDO1CTRL
,
307 MAX8997_REG_LDO2CTRL
,
308 MAX8997_REG_LDO3CTRL
,
309 MAX8997_REG_LDO4CTRL
,
310 MAX8997_REG_LDO5CTRL
,
311 MAX8997_REG_LDO6CTRL
,
312 MAX8997_REG_LDO7CTRL
,
313 MAX8997_REG_LDO8CTRL
,
314 MAX8997_REG_LDO9CTRL
,
315 MAX8997_REG_LDO10CTRL
,
316 MAX8997_REG_LDO11CTRL
,
317 MAX8997_REG_LDO12CTRL
,
318 MAX8997_REG_LDO13CTRL
,
319 MAX8997_REG_LDO14CTRL
,
320 MAX8997_REG_LDO15CTRL
,
321 MAX8997_REG_LDO16CTRL
,
322 MAX8997_REG_LDO17CTRL
,
323 MAX8997_REG_LDO18CTRL
,
324 MAX8997_REG_LDO21CTRL
,
325 MAX8997_REG_MBCCTRL1
,
326 MAX8997_REG_MBCCTRL2
,
327 MAX8997_REG_MBCCTRL3
,
328 MAX8997_REG_MBCCTRL4
,
329 MAX8997_REG_MBCCTRL5
,
330 MAX8997_REG_MBCCTRL6
,
331 MAX8997_REG_OTPCGHCVS
,
332 MAX8997_REG_SAFEOUTCTRL
,
337 MAX8997_REG_FLASH1_CUR
,
338 MAX8997_REG_FLASH2_CUR
,
339 MAX8997_REG_MOVIE_CUR
,
340 MAX8997_REG_GSMB_CUR
,
341 MAX8997_REG_BOOST_CNTL
,
342 MAX8997_REG_LEN_CNTL
,
343 MAX8997_REG_FLASH_CNTL
,
344 MAX8997_REG_WDT_CNTL
,
345 MAX8997_REG_MAXFLASH1
,
346 MAX8997_REG_MAXFLASH2
,
347 MAX8997_REG_FLASHSTATUSMASK
,
349 MAX8997_REG_GPIOCNTL1
,
350 MAX8997_REG_GPIOCNTL2
,
351 MAX8997_REG_GPIOCNTL3
,
352 MAX8997_REG_GPIOCNTL4
,
353 MAX8997_REG_GPIOCNTL5
,
354 MAX8997_REG_GPIOCNTL6
,
355 MAX8997_REG_GPIOCNTL7
,
356 MAX8997_REG_GPIOCNTL8
,
357 MAX8997_REG_GPIOCNTL9
,
358 MAX8997_REG_GPIOCNTL10
,
359 MAX8997_REG_GPIOCNTL11
,
360 MAX8997_REG_GPIOCNTL12
,
362 MAX8997_REG_LDO1CONFIG
,
363 MAX8997_REG_LDO2CONFIG
,
364 MAX8997_REG_LDO3CONFIG
,
365 MAX8997_REG_LDO4CONFIG
,
366 MAX8997_REG_LDO5CONFIG
,
367 MAX8997_REG_LDO6CONFIG
,
368 MAX8997_REG_LDO7CONFIG
,
369 MAX8997_REG_LDO8CONFIG
,
370 MAX8997_REG_LDO9CONFIG
,
371 MAX8997_REG_LDO10CONFIG
,
372 MAX8997_REG_LDO11CONFIG
,
373 MAX8997_REG_LDO12CONFIG
,
374 MAX8997_REG_LDO13CONFIG
,
375 MAX8997_REG_LDO14CONFIG
,
376 MAX8997_REG_LDO15CONFIG
,
377 MAX8997_REG_LDO16CONFIG
,
378 MAX8997_REG_LDO17CONFIG
,
379 MAX8997_REG_LDO18CONFIG
,
380 MAX8997_REG_LDO21CONFIG
,
382 MAX8997_REG_DVSOKTIMER1
,
383 MAX8997_REG_DVSOKTIMER2
,
384 MAX8997_REG_DVSOKTIMER4
,
385 MAX8997_REG_DVSOKTIMER5
,
388 static u8 max8997_dumpaddr_muic
[] = {
389 MAX8997_MUIC_REG_INTMASK1
,
390 MAX8997_MUIC_REG_INTMASK2
,
391 MAX8997_MUIC_REG_INTMASK3
,
392 MAX8997_MUIC_REG_CDETCTRL
,
393 MAX8997_MUIC_REG_CONTROL1
,
394 MAX8997_MUIC_REG_CONTROL2
,
395 MAX8997_MUIC_REG_CONTROL3
,
398 static u8 max8997_dumpaddr_haptic
[] = {
399 MAX8997_HAPTIC_REG_CONF1
,
400 MAX8997_HAPTIC_REG_CONF2
,
401 MAX8997_HAPTIC_REG_DRVCONF
,
402 MAX8997_HAPTIC_REG_CYCLECONF1
,
403 MAX8997_HAPTIC_REG_CYCLECONF2
,
404 MAX8997_HAPTIC_REG_SIGCONF1
,
405 MAX8997_HAPTIC_REG_SIGCONF2
,
406 MAX8997_HAPTIC_REG_SIGCONF3
,
407 MAX8997_HAPTIC_REG_SIGCONF4
,
408 MAX8997_HAPTIC_REG_SIGDC1
,
409 MAX8997_HAPTIC_REG_SIGDC2
,
410 MAX8997_HAPTIC_REG_SIGPWMDC1
,
411 MAX8997_HAPTIC_REG_SIGPWMDC2
,
412 MAX8997_HAPTIC_REG_SIGPWMDC3
,
413 MAX8997_HAPTIC_REG_SIGPWMDC4
,
416 static int max8997_freeze(struct device
*dev
)
418 struct i2c_client
*i2c
= to_i2c_client(dev
);
419 struct max8997_dev
*max8997
= i2c_get_clientdata(i2c
);
422 for (i
= 0; i
< ARRAY_SIZE(max8997_dumpaddr_pmic
); i
++)
423 max8997_read_reg(i2c
, max8997_dumpaddr_pmic
[i
],
424 &max8997
->reg_dump
[i
]);
426 for (i
= 0; i
< ARRAY_SIZE(max8997_dumpaddr_muic
); i
++)
427 max8997_read_reg(i2c
, max8997_dumpaddr_muic
[i
],
428 &max8997
->reg_dump
[i
+ MAX8997_REG_PMIC_END
]);
430 for (i
= 0; i
< ARRAY_SIZE(max8997_dumpaddr_haptic
); i
++)
431 max8997_read_reg(i2c
, max8997_dumpaddr_haptic
[i
],
432 &max8997
->reg_dump
[i
+ MAX8997_REG_PMIC_END
+
433 MAX8997_MUIC_REG_END
]);
438 static int max8997_restore(struct device
*dev
)
440 struct i2c_client
*i2c
= to_i2c_client(dev
);
441 struct max8997_dev
*max8997
= i2c_get_clientdata(i2c
);
444 for (i
= 0; i
< ARRAY_SIZE(max8997_dumpaddr_pmic
); i
++)
445 max8997_write_reg(i2c
, max8997_dumpaddr_pmic
[i
],
446 max8997
->reg_dump
[i
]);
448 for (i
= 0; i
< ARRAY_SIZE(max8997_dumpaddr_muic
); i
++)
449 max8997_write_reg(i2c
, max8997_dumpaddr_muic
[i
],
450 max8997
->reg_dump
[i
+ MAX8997_REG_PMIC_END
]);
452 for (i
= 0; i
< ARRAY_SIZE(max8997_dumpaddr_haptic
); i
++)
453 max8997_write_reg(i2c
, max8997_dumpaddr_haptic
[i
],
454 max8997
->reg_dump
[i
+ MAX8997_REG_PMIC_END
+
455 MAX8997_MUIC_REG_END
]);
460 static int max8997_suspend(struct device
*dev
)
462 struct i2c_client
*i2c
= to_i2c_client(dev
);
463 struct max8997_dev
*max8997
= i2c_get_clientdata(i2c
);
465 if (device_may_wakeup(dev
))
466 irq_set_irq_wake(max8997
->irq
, 1);
470 static int max8997_resume(struct device
*dev
)
472 struct i2c_client
*i2c
= to_i2c_client(dev
);
473 struct max8997_dev
*max8997
= i2c_get_clientdata(i2c
);
475 if (device_may_wakeup(dev
))
476 irq_set_irq_wake(max8997
->irq
, 0);
477 return max8997_irq_resume(max8997
);
480 static const struct dev_pm_ops max8997_pm
= {
481 .suspend
= max8997_suspend
,
482 .resume
= max8997_resume
,
483 .freeze
= max8997_freeze
,
484 .restore
= max8997_restore
,
487 static struct i2c_driver max8997_i2c_driver
= {
491 .suppress_bind_attrs
= true,
492 .of_match_table
= of_match_ptr(max8997_pmic_dt_match
),
494 .probe
= max8997_i2c_probe
,
495 .id_table
= max8997_i2c_id
,
498 static int __init
max8997_i2c_init(void)
500 return i2c_add_driver(&max8997_i2c_driver
);
502 /* init early so consumer devices can complete system boot */
503 subsys_initcall(max8997_i2c_init
);