2 * max8907.c - mfd driver for MAX8907
4 * Copyright (C) 2010 Gyungoh Yoo <jack.yoo@maxim-ic.com>
5 * Copyright (C) 2010-2012, NVIDIA CORPORATION. All rights reserved.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
12 #include <linux/err.h>
13 #include <linux/i2c.h>
14 #include <linux/init.h>
15 #include <linux/interrupt.h>
16 #include <linux/irq.h>
17 #include <linux/mfd/core.h>
18 #include <linux/mfd/max8907.h>
19 #include <linux/module.h>
21 #include <linux/of_device.h>
22 #include <linux/regmap.h>
23 #include <linux/slab.h>
25 static const struct mfd_cell max8907_cells
[] = {
26 { .name
= "max8907-regulator", },
27 { .name
= "max8907-rtc", },
30 static bool max8907_gen_is_volatile_reg(struct device
*dev
, unsigned int reg
)
33 case MAX8907_REG_ON_OFF_IRQ1
:
34 case MAX8907_REG_ON_OFF_STAT
:
35 case MAX8907_REG_ON_OFF_IRQ2
:
36 case MAX8907_REG_CHG_IRQ1
:
37 case MAX8907_REG_CHG_IRQ2
:
38 case MAX8907_REG_CHG_STAT
:
45 static bool max8907_gen_is_precious_reg(struct device
*dev
, unsigned int reg
)
48 case MAX8907_REG_ON_OFF_IRQ1
:
49 case MAX8907_REG_ON_OFF_IRQ2
:
50 case MAX8907_REG_CHG_IRQ1
:
51 case MAX8907_REG_CHG_IRQ2
:
58 static bool max8907_gen_is_writeable_reg(struct device
*dev
, unsigned int reg
)
60 return !max8907_gen_is_volatile_reg(dev
, reg
);
63 static const struct regmap_config max8907_regmap_gen_config
= {
66 .volatile_reg
= max8907_gen_is_volatile_reg
,
67 .precious_reg
= max8907_gen_is_precious_reg
,
68 .writeable_reg
= max8907_gen_is_writeable_reg
,
69 .max_register
= MAX8907_REG_LDO20VOUT
,
70 .cache_type
= REGCACHE_RBTREE
,
73 static bool max8907_rtc_is_volatile_reg(struct device
*dev
, unsigned int reg
)
75 if (reg
<= MAX8907_REG_RTC_YEAR2
)
79 case MAX8907_REG_RTC_STATUS
:
80 case MAX8907_REG_RTC_IRQ
:
87 static bool max8907_rtc_is_precious_reg(struct device
*dev
, unsigned int reg
)
90 case MAX8907_REG_RTC_IRQ
:
97 static bool max8907_rtc_is_writeable_reg(struct device
*dev
, unsigned int reg
)
100 case MAX8907_REG_RTC_STATUS
:
101 case MAX8907_REG_RTC_IRQ
:
108 static const struct regmap_config max8907_regmap_rtc_config
= {
111 .volatile_reg
= max8907_rtc_is_volatile_reg
,
112 .precious_reg
= max8907_rtc_is_precious_reg
,
113 .writeable_reg
= max8907_rtc_is_writeable_reg
,
114 .max_register
= MAX8907_REG_MPL_CNTL
,
115 .cache_type
= REGCACHE_RBTREE
,
118 static const struct regmap_irq max8907_chg_irqs
[] = {
119 { .reg_offset
= 0, .mask
= 1 << 0, },
120 { .reg_offset
= 0, .mask
= 1 << 1, },
121 { .reg_offset
= 0, .mask
= 1 << 2, },
122 { .reg_offset
= 1, .mask
= 1 << 0, },
123 { .reg_offset
= 1, .mask
= 1 << 1, },
124 { .reg_offset
= 1, .mask
= 1 << 2, },
125 { .reg_offset
= 1, .mask
= 1 << 3, },
126 { .reg_offset
= 1, .mask
= 1 << 4, },
127 { .reg_offset
= 1, .mask
= 1 << 5, },
128 { .reg_offset
= 1, .mask
= 1 << 6, },
129 { .reg_offset
= 1, .mask
= 1 << 7, },
132 static const struct regmap_irq_chip max8907_chg_irq_chip
= {
133 .name
= "max8907 chg",
134 .status_base
= MAX8907_REG_CHG_IRQ1
,
135 .mask_base
= MAX8907_REG_CHG_IRQ1_MASK
,
136 .wake_base
= MAX8907_REG_CHG_IRQ1_MASK
,
137 .irq_reg_stride
= MAX8907_REG_CHG_IRQ2
- MAX8907_REG_CHG_IRQ1
,
139 .irqs
= max8907_chg_irqs
,
140 .num_irqs
= ARRAY_SIZE(max8907_chg_irqs
),
143 static const struct regmap_irq max8907_on_off_irqs
[] = {
144 { .reg_offset
= 0, .mask
= 1 << 0, },
145 { .reg_offset
= 0, .mask
= 1 << 1, },
146 { .reg_offset
= 0, .mask
= 1 << 2, },
147 { .reg_offset
= 0, .mask
= 1 << 3, },
148 { .reg_offset
= 0, .mask
= 1 << 4, },
149 { .reg_offset
= 0, .mask
= 1 << 5, },
150 { .reg_offset
= 0, .mask
= 1 << 6, },
151 { .reg_offset
= 0, .mask
= 1 << 7, },
152 { .reg_offset
= 1, .mask
= 1 << 0, },
153 { .reg_offset
= 1, .mask
= 1 << 1, },
156 static const struct regmap_irq_chip max8907_on_off_irq_chip
= {
157 .name
= "max8907 on_off",
158 .status_base
= MAX8907_REG_ON_OFF_IRQ1
,
159 .mask_base
= MAX8907_REG_ON_OFF_IRQ1_MASK
,
160 .irq_reg_stride
= MAX8907_REG_ON_OFF_IRQ2
- MAX8907_REG_ON_OFF_IRQ1
,
162 .irqs
= max8907_on_off_irqs
,
163 .num_irqs
= ARRAY_SIZE(max8907_on_off_irqs
),
166 static const struct regmap_irq max8907_rtc_irqs
[] = {
167 { .reg_offset
= 0, .mask
= 1 << 2, },
168 { .reg_offset
= 0, .mask
= 1 << 3, },
171 static const struct regmap_irq_chip max8907_rtc_irq_chip
= {
172 .name
= "max8907 rtc",
173 .status_base
= MAX8907_REG_RTC_IRQ
,
174 .mask_base
= MAX8907_REG_RTC_IRQ_MASK
,
176 .irqs
= max8907_rtc_irqs
,
177 .num_irqs
= ARRAY_SIZE(max8907_rtc_irqs
),
180 static struct max8907
*max8907_pm_off
;
181 static void max8907_power_off(void)
183 regmap_update_bits(max8907_pm_off
->regmap_gen
, MAX8907_REG_RESET_CNFG
,
184 MAX8907_MASK_POWER_OFF
, MAX8907_MASK_POWER_OFF
);
187 static int max8907_i2c_probe(struct i2c_client
*i2c
,
188 const struct i2c_device_id
*id
)
190 struct max8907
*max8907
;
192 struct max8907_platform_data
*pdata
= dev_get_platdata(&i2c
->dev
);
196 pm_off
= pdata
->pm_off
;
197 else if (i2c
->dev
.of_node
)
198 pm_off
= of_property_read_bool(i2c
->dev
.of_node
,
199 "maxim,system-power-controller");
201 max8907
= devm_kzalloc(&i2c
->dev
, sizeof(struct max8907
), GFP_KERNEL
);
204 goto err_alloc_drvdata
;
207 max8907
->dev
= &i2c
->dev
;
208 dev_set_drvdata(max8907
->dev
, max8907
);
210 max8907
->i2c_gen
= i2c
;
211 i2c_set_clientdata(i2c
, max8907
);
212 max8907
->regmap_gen
= devm_regmap_init_i2c(i2c
,
213 &max8907_regmap_gen_config
);
214 if (IS_ERR(max8907
->regmap_gen
)) {
215 ret
= PTR_ERR(max8907
->regmap_gen
);
216 dev_err(&i2c
->dev
, "gen regmap init failed: %d\n", ret
);
220 max8907
->i2c_rtc
= i2c_new_dummy(i2c
->adapter
, MAX8907_RTC_I2C_ADDR
);
221 if (!max8907
->i2c_rtc
) {
225 i2c_set_clientdata(max8907
->i2c_rtc
, max8907
);
226 max8907
->regmap_rtc
= devm_regmap_init_i2c(max8907
->i2c_rtc
,
227 &max8907_regmap_rtc_config
);
228 if (IS_ERR(max8907
->regmap_rtc
)) {
229 ret
= PTR_ERR(max8907
->regmap_rtc
);
230 dev_err(&i2c
->dev
, "rtc regmap init failed: %d\n", ret
);
234 irq_set_status_flags(max8907
->i2c_gen
->irq
, IRQ_NOAUTOEN
);
236 ret
= regmap_add_irq_chip(max8907
->regmap_gen
, max8907
->i2c_gen
->irq
,
237 IRQF_ONESHOT
| IRQF_SHARED
, -1,
238 &max8907_chg_irq_chip
,
241 dev_err(&i2c
->dev
, "failed to add chg irq chip: %d\n", ret
);
244 ret
= regmap_add_irq_chip(max8907
->regmap_gen
, max8907
->i2c_gen
->irq
,
245 IRQF_ONESHOT
| IRQF_SHARED
, -1,
246 &max8907_on_off_irq_chip
,
247 &max8907
->irqc_on_off
);
249 dev_err(&i2c
->dev
, "failed to add on off irq chip: %d\n", ret
);
250 goto err_irqc_on_off
;
252 ret
= regmap_add_irq_chip(max8907
->regmap_rtc
, max8907
->i2c_gen
->irq
,
253 IRQF_ONESHOT
| IRQF_SHARED
, -1,
254 &max8907_rtc_irq_chip
,
257 dev_err(&i2c
->dev
, "failed to add rtc irq chip: %d\n", ret
);
261 enable_irq(max8907
->i2c_gen
->irq
);
263 ret
= mfd_add_devices(max8907
->dev
, -1, max8907_cells
,
264 ARRAY_SIZE(max8907_cells
), NULL
, 0, NULL
);
266 dev_err(&i2c
->dev
, "failed to add MFD devices %d\n", ret
);
267 goto err_add_devices
;
270 if (pm_off
&& !pm_power_off
) {
271 max8907_pm_off
= max8907
;
272 pm_power_off
= max8907_power_off
;
278 regmap_del_irq_chip(max8907
->i2c_gen
->irq
, max8907
->irqc_rtc
);
280 regmap_del_irq_chip(max8907
->i2c_gen
->irq
, max8907
->irqc_on_off
);
282 regmap_del_irq_chip(max8907
->i2c_gen
->irq
, max8907
->irqc_chg
);
285 i2c_unregister_device(max8907
->i2c_rtc
);
292 static int max8907_i2c_remove(struct i2c_client
*i2c
)
294 struct max8907
*max8907
= i2c_get_clientdata(i2c
);
296 mfd_remove_devices(max8907
->dev
);
298 regmap_del_irq_chip(max8907
->i2c_gen
->irq
, max8907
->irqc_rtc
);
299 regmap_del_irq_chip(max8907
->i2c_gen
->irq
, max8907
->irqc_on_off
);
300 regmap_del_irq_chip(max8907
->i2c_gen
->irq
, max8907
->irqc_chg
);
302 i2c_unregister_device(max8907
->i2c_rtc
);
308 static const struct of_device_id max8907_of_match
[] = {
309 { .compatible
= "maxim,max8907" },
312 MODULE_DEVICE_TABLE(of
, max8907_of_match
);
315 static const struct i2c_device_id max8907_i2c_id
[] = {
319 MODULE_DEVICE_TABLE(i2c
, max8907_i2c_id
);
321 static struct i2c_driver max8907_i2c_driver
= {
324 .of_match_table
= of_match_ptr(max8907_of_match
),
326 .probe
= max8907_i2c_probe
,
327 .remove
= max8907_i2c_remove
,
328 .id_table
= max8907_i2c_id
,
331 static int __init
max8907_i2c_init(void)
335 ret
= i2c_add_driver(&max8907_i2c_driver
);
337 pr_err("Failed to register I2C driver: %d\n", ret
);
341 subsys_initcall(max8907_i2c_init
);
343 static void __exit
max8907_i2c_exit(void)
345 i2c_del_driver(&max8907_i2c_driver
);
347 module_exit(max8907_i2c_exit
);
349 MODULE_DESCRIPTION("MAX8907 multi-function core driver");
350 MODULE_AUTHOR("Gyungoh Yoo <jack.yoo@maxim-ic.com>");
351 MODULE_LICENSE("GPL v2");