]>
Commit | Line | Data |
---|---|---|
d7d8d7a2 KK |
1 | // 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 <[email protected]> | |
7 | // Marek Szyprowski <[email protected]> | |
156f2528 | 8 | |
ee999fb3 | 9 | #include <linux/err.h> |
156f2528 KP |
10 | #include <linux/init.h> |
11 | #include <linux/slab.h> | |
12 | #include <linux/i2c.h> | |
cdd137c9 | 13 | #include <linux/interrupt.h> |
ee999fb3 TF |
14 | #include <linux/of.h> |
15 | #include <linux/of_irq.h> | |
cdd137c9 | 16 | #include <linux/pm_runtime.h> |
156f2528 KP |
17 | #include <linux/mutex.h> |
18 | #include <linux/mfd/core.h> | |
19 | #include <linux/mfd/max8998.h> | |
20 | #include <linux/mfd/max8998-private.h> | |
21 | ||
9b16c0a4 JS |
22 | #define RTC_I2C_ADDR (0x0c >> 1) |
23 | ||
7c0517b1 | 24 | static const struct mfd_cell max8998_devs[] = { |
156f2528 KP |
25 | { |
26 | .name = "max8998-pmic", | |
9b16c0a4 JS |
27 | }, { |
28 | .name = "max8998-rtc", | |
bb4ce970 DK |
29 | }, { |
30 | .name = "max8998-battery", | |
9b16c0a4 | 31 | }, |
156f2528 KP |
32 | }; |
33 | ||
7c0517b1 | 34 | static const struct mfd_cell lp3974_devs[] = { |
337ce5d1 MH |
35 | { |
36 | .name = "lp3974-pmic", | |
37 | }, { | |
38 | .name = "lp3974-rtc", | |
39 | }, | |
40 | }; | |
41 | ||
676e02d7 | 42 | int max8998_read_reg(struct i2c_client *i2c, u8 reg, u8 *dest) |
156f2528 | 43 | { |
676e02d7 | 44 | struct max8998_dev *max8998 = i2c_get_clientdata(i2c); |
156f2528 KP |
45 | int ret; |
46 | ||
47 | mutex_lock(&max8998->iolock); | |
676e02d7 | 48 | ret = i2c_smbus_read_byte_data(i2c, reg); |
156f2528 KP |
49 | mutex_unlock(&max8998->iolock); |
50 | if (ret < 0) | |
51 | return ret; | |
52 | ||
53 | ret &= 0xff; | |
54 | *dest = ret; | |
55 | return 0; | |
56 | } | |
676e02d7 | 57 | EXPORT_SYMBOL(max8998_read_reg); |
156f2528 | 58 | |
2c7e6f57 JS |
59 | int max8998_bulk_read(struct i2c_client *i2c, u8 reg, int count, u8 *buf) |
60 | { | |
61 | struct max8998_dev *max8998 = i2c_get_clientdata(i2c); | |
62 | int ret; | |
63 | ||
64 | mutex_lock(&max8998->iolock); | |
65 | ret = i2c_smbus_read_i2c_block_data(i2c, reg, count, buf); | |
66 | mutex_unlock(&max8998->iolock); | |
67 | if (ret < 0) | |
68 | return ret; | |
69 | ||
70 | return 0; | |
71 | } | |
72 | EXPORT_SYMBOL(max8998_bulk_read); | |
73 | ||
676e02d7 | 74 | int max8998_write_reg(struct i2c_client *i2c, u8 reg, u8 value) |
156f2528 | 75 | { |
676e02d7 | 76 | struct max8998_dev *max8998 = i2c_get_clientdata(i2c); |
156f2528 KP |
77 | int ret; |
78 | ||
79 | mutex_lock(&max8998->iolock); | |
676e02d7 | 80 | ret = i2c_smbus_write_byte_data(i2c, reg, value); |
156f2528 KP |
81 | mutex_unlock(&max8998->iolock); |
82 | return ret; | |
83 | } | |
676e02d7 | 84 | EXPORT_SYMBOL(max8998_write_reg); |
156f2528 | 85 | |
9b16c0a4 JS |
86 | int max8998_bulk_write(struct i2c_client *i2c, u8 reg, int count, u8 *buf) |
87 | { | |
88 | struct max8998_dev *max8998 = i2c_get_clientdata(i2c); | |
89 | int ret; | |
90 | ||
91 | mutex_lock(&max8998->iolock); | |
92 | ret = i2c_smbus_write_i2c_block_data(i2c, reg, count, buf); | |
93 | mutex_unlock(&max8998->iolock); | |
94 | if (ret < 0) | |
95 | return ret; | |
96 | ||
97 | return 0; | |
98 | } | |
99 | EXPORT_SYMBOL(max8998_bulk_write); | |
100 | ||
676e02d7 | 101 | int max8998_update_reg(struct i2c_client *i2c, u8 reg, u8 val, u8 mask) |
156f2528 | 102 | { |
676e02d7 | 103 | struct max8998_dev *max8998 = i2c_get_clientdata(i2c); |
156f2528 KP |
104 | int ret; |
105 | ||
106 | mutex_lock(&max8998->iolock); | |
676e02d7 | 107 | ret = i2c_smbus_read_byte_data(i2c, reg); |
156f2528 KP |
108 | if (ret >= 0) { |
109 | u8 old_val = ret & 0xff; | |
110 | u8 new_val = (val & mask) | (old_val & (~mask)); | |
676e02d7 | 111 | ret = i2c_smbus_write_byte_data(i2c, reg, new_val); |
156f2528 KP |
112 | } |
113 | mutex_unlock(&max8998->iolock); | |
114 | return ret; | |
115 | } | |
676e02d7 | 116 | EXPORT_SYMBOL(max8998_update_reg); |
156f2528 | 117 | |
ee999fb3 | 118 | #ifdef CONFIG_OF |
e920574d | 119 | static const struct of_device_id max8998_dt_match[] = { |
ee999fb3 TF |
120 | { .compatible = "maxim,max8998", .data = (void *)TYPE_MAX8998 }, |
121 | { .compatible = "national,lp3974", .data = (void *)TYPE_LP3974 }, | |
122 | { .compatible = "ti,lp3974", .data = (void *)TYPE_LP3974 }, | |
123 | {}, | |
124 | }; | |
ee999fb3 TF |
125 | #endif |
126 | ||
127 | /* | |
128 | * Only the common platform data elements for max8998 are parsed here from the | |
129 | * device tree. Other sub-modules of max8998 such as pmic, rtc and others have | |
130 | * to parse their own platform data elements from device tree. | |
131 | * | |
132 | * The max8998 platform data structure is instantiated here and the drivers for | |
133 | * the sub-modules need not instantiate another instance while parsing their | |
134 | * platform data. | |
135 | */ | |
136 | static struct max8998_platform_data *max8998_i2c_parse_dt_pdata( | |
137 | struct device *dev) | |
138 | { | |
139 | struct max8998_platform_data *pd; | |
140 | ||
141 | pd = devm_kzalloc(dev, sizeof(*pd), GFP_KERNEL); | |
142 | if (!pd) | |
143 | return ERR_PTR(-ENOMEM); | |
144 | ||
145 | pd->ono = irq_of_parse_and_map(dev->of_node, 1); | |
146 | ||
147 | /* | |
148 | * ToDo: the 'wakeup' member in the platform data is more of a linux | |
149 | * specfic information. Hence, there is no binding for that yet and | |
150 | * not parsed here. | |
151 | */ | |
152 | return pd; | |
153 | } | |
154 | ||
833eaaf7 | 155 | static int max8998_i2c_probe(struct i2c_client *i2c) |
156f2528 | 156 | { |
334a41ce | 157 | struct max8998_platform_data *pdata = dev_get_platdata(&i2c->dev); |
156f2528 KP |
158 | struct max8998_dev *max8998; |
159 | int ret = 0; | |
160 | ||
0010dd38 JH |
161 | max8998 = devm_kzalloc(&i2c->dev, sizeof(struct max8998_dev), |
162 | GFP_KERNEL); | |
8f1f151e | 163 | if (max8998 == NULL) |
156f2528 | 164 | return -ENOMEM; |
156f2528 | 165 | |
ee999fb3 TF |
166 | if (IS_ENABLED(CONFIG_OF) && i2c->dev.of_node) { |
167 | pdata = max8998_i2c_parse_dt_pdata(&i2c->dev); | |
2042f3c2 CJ |
168 | if (IS_ERR(pdata)) |
169 | return PTR_ERR(pdata); | |
ee999fb3 TF |
170 | } |
171 | ||
156f2528 KP |
172 | i2c_set_clientdata(i2c, max8998); |
173 | max8998->dev = &i2c->dev; | |
676e02d7 | 174 | max8998->i2c = i2c; |
2c7e6f57 | 175 | max8998->irq = i2c->irq; |
c4974eea | 176 | max8998->type = (uintptr_t)i2c_get_match_data(i2c); |
ee999fb3 | 177 | max8998->pdata = pdata; |
2c7e6f57 JS |
178 | if (pdata) { |
179 | max8998->ono = pdata->ono; | |
180 | max8998->irq_base = pdata->irq_base; | |
cdd137c9 | 181 | max8998->wakeup = pdata->wakeup; |
2c7e6f57 | 182 | } |
156f2528 KP |
183 | mutex_init(&max8998->iolock); |
184 | ||
7a99c8f3 WS |
185 | max8998->rtc = i2c_new_dummy_device(i2c->adapter, RTC_I2C_ADDR); |
186 | if (IS_ERR(max8998->rtc)) { | |
ed26f87b | 187 | dev_err(&i2c->dev, "Failed to allocate I2C device for RTC\n"); |
7a99c8f3 | 188 | return PTR_ERR(max8998->rtc); |
ed26f87b | 189 | } |
9b16c0a4 JS |
190 | i2c_set_clientdata(max8998->rtc, max8998); |
191 | ||
2c7e6f57 JS |
192 | max8998_irq_init(max8998); |
193 | ||
cdd137c9 MH |
194 | pm_runtime_set_active(max8998->dev); |
195 | ||
ee999fb3 | 196 | switch (max8998->type) { |
337ce5d1 MH |
197 | case TYPE_LP3974: |
198 | ret = mfd_add_devices(max8998->dev, -1, | |
0848c94f MB |
199 | lp3974_devs, ARRAY_SIZE(lp3974_devs), |
200 | NULL, 0, NULL); | |
337ce5d1 MH |
201 | break; |
202 | case TYPE_MAX8998: | |
203 | ret = mfd_add_devices(max8998->dev, -1, | |
0848c94f MB |
204 | max8998_devs, ARRAY_SIZE(max8998_devs), |
205 | NULL, 0, NULL); | |
337ce5d1 MH |
206 | break; |
207 | default: | |
208 | ret = -EINVAL; | |
209 | } | |
210 | ||
156f2528 KP |
211 | if (ret < 0) |
212 | goto err; | |
213 | ||
7ef73598 JC |
214 | device_init_wakeup(max8998->dev, max8998->wakeup); |
215 | ||
156f2528 KP |
216 | return ret; |
217 | ||
218 | err: | |
219 | mfd_remove_devices(max8998->dev); | |
7484552e AL |
220 | max8998_irq_exit(max8998); |
221 | i2c_unregister_device(max8998->rtc); | |
156f2528 KP |
222 | return ret; |
223 | } | |
224 | ||
156f2528 | 225 | static const struct i2c_device_id max8998_i2c_id[] = { |
509bd476 LM |
226 | { "max8998", TYPE_MAX8998 }, |
227 | { "lp3974", TYPE_LP3974}, | |
f8539ddc | 228 | { } |
156f2528 | 229 | }; |
156f2528 | 230 | |
cdd137c9 MH |
231 | static int max8998_suspend(struct device *dev) |
232 | { | |
1b5420e1 | 233 | struct i2c_client *i2c = to_i2c_client(dev); |
cdd137c9 MH |
234 | struct max8998_dev *max8998 = i2c_get_clientdata(i2c); |
235 | ||
7ef73598 | 236 | if (device_may_wakeup(dev)) |
d5bb1221 | 237 | irq_set_irq_wake(max8998->irq, 1); |
cdd137c9 MH |
238 | return 0; |
239 | } | |
240 | ||
241 | static int max8998_resume(struct device *dev) | |
242 | { | |
1b5420e1 | 243 | struct i2c_client *i2c = to_i2c_client(dev); |
cdd137c9 MH |
244 | struct max8998_dev *max8998 = i2c_get_clientdata(i2c); |
245 | ||
7ef73598 | 246 | if (device_may_wakeup(dev)) |
d5bb1221 | 247 | irq_set_irq_wake(max8998->irq, 0); |
cdd137c9 MH |
248 | /* |
249 | * In LP3974, if IRQ registers are not "read & clear" | |
250 | * when it's set during sleep, the interrupt becomes | |
251 | * disabled. | |
252 | */ | |
253 | return max8998_irq_resume(i2c_get_clientdata(i2c)); | |
254 | } | |
255 | ||
256 | struct max8998_reg_dump { | |
257 | u8 addr; | |
258 | u8 val; | |
259 | }; | |
260 | #define SAVE_ITEM(x) { .addr = (x), .val = 0x0, } | |
44be0a40 | 261 | static struct max8998_reg_dump max8998_dump[] = { |
cdd137c9 MH |
262 | SAVE_ITEM(MAX8998_REG_IRQM1), |
263 | SAVE_ITEM(MAX8998_REG_IRQM2), | |
264 | SAVE_ITEM(MAX8998_REG_IRQM3), | |
265 | SAVE_ITEM(MAX8998_REG_IRQM4), | |
266 | SAVE_ITEM(MAX8998_REG_STATUSM1), | |
267 | SAVE_ITEM(MAX8998_REG_STATUSM2), | |
268 | SAVE_ITEM(MAX8998_REG_CHGR1), | |
269 | SAVE_ITEM(MAX8998_REG_CHGR2), | |
270 | SAVE_ITEM(MAX8998_REG_LDO_ACTIVE_DISCHARGE1), | |
271 | SAVE_ITEM(MAX8998_REG_LDO_ACTIVE_DISCHARGE1), | |
272 | SAVE_ITEM(MAX8998_REG_BUCK_ACTIVE_DISCHARGE3), | |
273 | SAVE_ITEM(MAX8998_REG_ONOFF1), | |
274 | SAVE_ITEM(MAX8998_REG_ONOFF2), | |
275 | SAVE_ITEM(MAX8998_REG_ONOFF3), | |
276 | SAVE_ITEM(MAX8998_REG_ONOFF4), | |
277 | SAVE_ITEM(MAX8998_REG_BUCK1_VOLTAGE1), | |
278 | SAVE_ITEM(MAX8998_REG_BUCK1_VOLTAGE2), | |
279 | SAVE_ITEM(MAX8998_REG_BUCK1_VOLTAGE3), | |
280 | SAVE_ITEM(MAX8998_REG_BUCK1_VOLTAGE4), | |
281 | SAVE_ITEM(MAX8998_REG_BUCK2_VOLTAGE1), | |
282 | SAVE_ITEM(MAX8998_REG_BUCK2_VOLTAGE2), | |
283 | SAVE_ITEM(MAX8998_REG_LDO2_LDO3), | |
284 | SAVE_ITEM(MAX8998_REG_LDO4), | |
285 | SAVE_ITEM(MAX8998_REG_LDO5), | |
286 | SAVE_ITEM(MAX8998_REG_LDO6), | |
287 | SAVE_ITEM(MAX8998_REG_LDO7), | |
288 | SAVE_ITEM(MAX8998_REG_LDO8_LDO9), | |
289 | SAVE_ITEM(MAX8998_REG_LDO10_LDO11), | |
290 | SAVE_ITEM(MAX8998_REG_LDO12), | |
291 | SAVE_ITEM(MAX8998_REG_LDO13), | |
292 | SAVE_ITEM(MAX8998_REG_LDO14), | |
293 | SAVE_ITEM(MAX8998_REG_LDO15), | |
294 | SAVE_ITEM(MAX8998_REG_LDO16), | |
295 | SAVE_ITEM(MAX8998_REG_LDO17), | |
296 | SAVE_ITEM(MAX8998_REG_BKCHR), | |
297 | SAVE_ITEM(MAX8998_REG_LBCNFG1), | |
298 | SAVE_ITEM(MAX8998_REG_LBCNFG2), | |
299 | }; | |
300 | /* Save registers before hibernation */ | |
301 | static int max8998_freeze(struct device *dev) | |
302 | { | |
1b5420e1 | 303 | struct i2c_client *i2c = to_i2c_client(dev); |
cdd137c9 MH |
304 | int i; |
305 | ||
306 | for (i = 0; i < ARRAY_SIZE(max8998_dump); i++) | |
307 | max8998_read_reg(i2c, max8998_dump[i].addr, | |
308 | &max8998_dump[i].val); | |
309 | ||
310 | return 0; | |
311 | } | |
312 | ||
313 | /* Restore registers after hibernation */ | |
314 | static int max8998_restore(struct device *dev) | |
315 | { | |
1b5420e1 | 316 | struct i2c_client *i2c = to_i2c_client(dev); |
cdd137c9 MH |
317 | int i; |
318 | ||
319 | for (i = 0; i < ARRAY_SIZE(max8998_dump); i++) | |
320 | max8998_write_reg(i2c, max8998_dump[i].addr, | |
321 | max8998_dump[i].val); | |
322 | ||
323 | return 0; | |
324 | } | |
325 | ||
44be0a40 | 326 | static const struct dev_pm_ops max8998_pm = { |
cdd137c9 MH |
327 | .suspend = max8998_suspend, |
328 | .resume = max8998_resume, | |
329 | .freeze = max8998_freeze, | |
330 | .restore = max8998_restore, | |
331 | }; | |
332 | ||
156f2528 KP |
333 | static struct i2c_driver max8998_i2c_driver = { |
334 | .driver = { | |
335 | .name = "max8998", | |
cdd137c9 | 336 | .pm = &max8998_pm, |
b9e38338 | 337 | .suppress_bind_attrs = true, |
ee999fb3 | 338 | .of_match_table = of_match_ptr(max8998_dt_match), |
156f2528 | 339 | }, |
9816d859 | 340 | .probe = max8998_i2c_probe, |
156f2528 KP |
341 | .id_table = max8998_i2c_id, |
342 | }; | |
343 | ||
344 | static int __init max8998_i2c_init(void) | |
345 | { | |
346 | return i2c_add_driver(&max8998_i2c_driver); | |
347 | } | |
348 | /* init early so consumer devices can complete system boot */ | |
349 | subsys_initcall(max8998_i2c_init); |