1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24#include <linux/err.h>
25#include <linux/slab.h>
26#include <linux/i2c.h>
27#include <linux/of.h>
28#include <linux/of_irq.h>
29#include <linux/interrupt.h>
30#include <linux/pm_runtime.h>
31#include <linux/module.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>
36
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)
42
43static 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 },
51};
52
53#ifdef CONFIG_OF
54static const struct of_device_id max8997_pmic_dt_match[] = {
55 { .compatible = "maxim,max8997-pmic", .data = (void *)TYPE_MAX8997 },
56 {},
57};
58#endif
59
60int max8997_read_reg(struct i2c_client *i2c, u8 reg, u8 *dest)
61{
62 struct max8997_dev *max8997 = i2c_get_clientdata(i2c);
63 int ret;
64
65 mutex_lock(&max8997->iolock);
66 ret = i2c_smbus_read_byte_data(i2c, reg);
67 mutex_unlock(&max8997->iolock);
68 if (ret < 0)
69 return ret;
70
71 ret &= 0xff;
72 *dest = ret;
73 return 0;
74}
75EXPORT_SYMBOL_GPL(max8997_read_reg);
76
77int max8997_bulk_read(struct i2c_client *i2c, u8 reg, int count, u8 *buf)
78{
79 struct max8997_dev *max8997 = i2c_get_clientdata(i2c);
80 int ret;
81
82 mutex_lock(&max8997->iolock);
83 ret = i2c_smbus_read_i2c_block_data(i2c, reg, count, buf);
84 mutex_unlock(&max8997->iolock);
85 if (ret < 0)
86 return ret;
87
88 return 0;
89}
90EXPORT_SYMBOL_GPL(max8997_bulk_read);
91
92int max8997_write_reg(struct i2c_client *i2c, u8 reg, u8 value)
93{
94 struct max8997_dev *max8997 = i2c_get_clientdata(i2c);
95 int ret;
96
97 mutex_lock(&max8997->iolock);
98 ret = i2c_smbus_write_byte_data(i2c, reg, value);
99 mutex_unlock(&max8997->iolock);
100 return ret;
101}
102EXPORT_SYMBOL_GPL(max8997_write_reg);
103
104int max8997_bulk_write(struct i2c_client *i2c, u8 reg, int count, u8 *buf)
105{
106 struct max8997_dev *max8997 = i2c_get_clientdata(i2c);
107 int ret;
108
109 mutex_lock(&max8997->iolock);
110 ret = i2c_smbus_write_i2c_block_data(i2c, reg, count, buf);
111 mutex_unlock(&max8997->iolock);
112 if (ret < 0)
113 return ret;
114
115 return 0;
116}
117EXPORT_SYMBOL_GPL(max8997_bulk_write);
118
119int max8997_update_reg(struct i2c_client *i2c, u8 reg, u8 val, u8 mask)
120{
121 struct max8997_dev *max8997 = i2c_get_clientdata(i2c);
122 int ret;
123
124 mutex_lock(&max8997->iolock);
125 ret = i2c_smbus_read_byte_data(i2c, reg);
126 if (ret >= 0) {
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);
130 }
131 mutex_unlock(&max8997->iolock);
132 return ret;
133}
134EXPORT_SYMBOL_GPL(max8997_update_reg);
135
136
137
138
139
140
141
142
143
144
145static struct max8997_platform_data *max8997_i2c_parse_dt_pdata(
146 struct device *dev)
147{
148 struct max8997_platform_data *pd;
149
150 pd = devm_kzalloc(dev, sizeof(*pd), GFP_KERNEL);
151 if (!pd) {
152 dev_err(dev, "could not allocate memory for pdata\n");
153 return ERR_PTR(-ENOMEM);
154 }
155
156 pd->ono = irq_of_parse_and_map(dev->of_node, 1);
157
158
159
160
161
162
163
164 return pd;
165}
166
167static inline unsigned long max8997_i2c_get_driver_data(struct i2c_client *i2c,
168 const struct i2c_device_id *id)
169{
170 if (IS_ENABLED(CONFIG_OF) && i2c->dev.of_node) {
171 const struct of_device_id *match;
172 match = of_match_node(max8997_pmic_dt_match, i2c->dev.of_node);
173 return (unsigned long)match->data;
174 }
175 return id->driver_data;
176}
177
178static int max8997_i2c_probe(struct i2c_client *i2c,
179 const struct i2c_device_id *id)
180{
181 struct max8997_dev *max8997;
182 struct max8997_platform_data *pdata = dev_get_platdata(&i2c->dev);
183 int ret = 0;
184
185 max8997 = devm_kzalloc(&i2c->dev, sizeof(struct max8997_dev),
186 GFP_KERNEL);
187 if (max8997 == NULL)
188 return -ENOMEM;
189
190 i2c_set_clientdata(i2c, max8997);
191 max8997->dev = &i2c->dev;
192 max8997->i2c = i2c;
193 max8997->type = max8997_i2c_get_driver_data(i2c, id);
194 max8997->irq = i2c->irq;
195
196 if (IS_ENABLED(CONFIG_OF) && max8997->dev->of_node) {
197 pdata = max8997_i2c_parse_dt_pdata(max8997->dev);
198 if (IS_ERR(pdata))
199 return PTR_ERR(pdata);
200 }
201
202 if (!pdata)
203 return ret;
204
205 max8997->pdata = pdata;
206 max8997->ono = pdata->ono;
207
208 mutex_init(&max8997->iolock);
209
210 max8997->rtc = i2c_new_dummy(i2c->adapter, I2C_ADDR_RTC);
211 if (!max8997->rtc) {
212 dev_err(max8997->dev, "Failed to allocate I2C device for RTC\n");
213 return -ENODEV;
214 }
215 i2c_set_clientdata(max8997->rtc, max8997);
216
217 max8997->haptic = i2c_new_dummy(i2c->adapter, I2C_ADDR_HAPTIC);
218 if (!max8997->haptic) {
219 dev_err(max8997->dev, "Failed to allocate I2C device for Haptic\n");
220 ret = -ENODEV;
221 goto err_i2c_haptic;
222 }
223 i2c_set_clientdata(max8997->haptic, max8997);
224
225 max8997->muic = i2c_new_dummy(i2c->adapter, I2C_ADDR_MUIC);
226 if (!max8997->muic) {
227 dev_err(max8997->dev, "Failed to allocate I2C device for MUIC\n");
228 ret = -ENODEV;
229 goto err_i2c_muic;
230 }
231 i2c_set_clientdata(max8997->muic, max8997);
232
233 pm_runtime_set_active(max8997->dev);
234
235 max8997_irq_init(max8997);
236
237 ret = mfd_add_devices(max8997->dev, -1, max8997_devs,
238 ARRAY_SIZE(max8997_devs),
239 NULL, 0, NULL);
240 if (ret < 0) {
241 dev_err(max8997->dev, "failed to add MFD devices %d\n", ret);
242 goto err_mfd;
243 }
244
245
246
247
248
249
250
251 device_init_wakeup(max8997->dev, pdata->wakeup);
252
253 return ret;
254
255err_mfd:
256 mfd_remove_devices(max8997->dev);
257 i2c_unregister_device(max8997->muic);
258err_i2c_muic:
259 i2c_unregister_device(max8997->haptic);
260err_i2c_haptic:
261 i2c_unregister_device(max8997->rtc);
262 return ret;
263}
264
265static int max8997_i2c_remove(struct i2c_client *i2c)
266{
267 struct max8997_dev *max8997 = i2c_get_clientdata(i2c);
268
269 mfd_remove_devices(max8997->dev);
270 i2c_unregister_device(max8997->muic);
271 i2c_unregister_device(max8997->haptic);
272 i2c_unregister_device(max8997->rtc);
273
274 return 0;
275}
276
277static const struct i2c_device_id max8997_i2c_id[] = {
278 { "max8997", TYPE_MAX8997 },
279 { "max8966", TYPE_MAX8966 },
280 { }
281};
282MODULE_DEVICE_TABLE(i2c, max8998_i2c_id);
283
284static u8 max8997_dumpaddr_pmic[] = {
285 MAX8997_REG_INT1MSK,
286 MAX8997_REG_INT2MSK,
287 MAX8997_REG_INT3MSK,
288 MAX8997_REG_INT4MSK,
289 MAX8997_REG_MAINCON1,
290 MAX8997_REG_MAINCON2,
291 MAX8997_REG_BUCKRAMP,
292 MAX8997_REG_BUCK1CTRL,
293 MAX8997_REG_BUCK1DVS1,
294 MAX8997_REG_BUCK1DVS2,
295 MAX8997_REG_BUCK1DVS3,
296 MAX8997_REG_BUCK1DVS4,
297 MAX8997_REG_BUCK1DVS5,
298 MAX8997_REG_BUCK1DVS6,
299 MAX8997_REG_BUCK1DVS7,
300 MAX8997_REG_BUCK1DVS8,
301 MAX8997_REG_BUCK2CTRL,
302 MAX8997_REG_BUCK2DVS1,
303 MAX8997_REG_BUCK2DVS2,
304 MAX8997_REG_BUCK2DVS3,
305 MAX8997_REG_BUCK2DVS4,
306 MAX8997_REG_BUCK2DVS5,
307 MAX8997_REG_BUCK2DVS6,
308 MAX8997_REG_BUCK2DVS7,
309 MAX8997_REG_BUCK2DVS8,
310 MAX8997_REG_BUCK3CTRL,
311 MAX8997_REG_BUCK3DVS,
312 MAX8997_REG_BUCK4CTRL,
313 MAX8997_REG_BUCK4DVS,
314 MAX8997_REG_BUCK5CTRL,
315 MAX8997_REG_BUCK5DVS1,
316 MAX8997_REG_BUCK5DVS2,
317 MAX8997_REG_BUCK5DVS3,
318 MAX8997_REG_BUCK5DVS4,
319 MAX8997_REG_BUCK5DVS5,
320 MAX8997_REG_BUCK5DVS6,
321 MAX8997_REG_BUCK5DVS7,
322 MAX8997_REG_BUCK5DVS8,
323 MAX8997_REG_BUCK6CTRL,
324 MAX8997_REG_BUCK6BPSKIPCTRL,
325 MAX8997_REG_BUCK7CTRL,
326 MAX8997_REG_BUCK7DVS,
327 MAX8997_REG_LDO1CTRL,
328 MAX8997_REG_LDO2CTRL,
329 MAX8997_REG_LDO3CTRL,
330 MAX8997_REG_LDO4CTRL,
331 MAX8997_REG_LDO5CTRL,
332 MAX8997_REG_LDO6CTRL,
333 MAX8997_REG_LDO7CTRL,
334 MAX8997_REG_LDO8CTRL,
335 MAX8997_REG_LDO9CTRL,
336 MAX8997_REG_LDO10CTRL,
337 MAX8997_REG_LDO11CTRL,
338 MAX8997_REG_LDO12CTRL,
339 MAX8997_REG_LDO13CTRL,
340 MAX8997_REG_LDO14CTRL,
341 MAX8997_REG_LDO15CTRL,
342 MAX8997_REG_LDO16CTRL,
343 MAX8997_REG_LDO17CTRL,
344 MAX8997_REG_LDO18CTRL,
345 MAX8997_REG_LDO21CTRL,
346 MAX8997_REG_MBCCTRL1,
347 MAX8997_REG_MBCCTRL2,
348 MAX8997_REG_MBCCTRL3,
349 MAX8997_REG_MBCCTRL4,
350 MAX8997_REG_MBCCTRL5,
351 MAX8997_REG_MBCCTRL6,
352 MAX8997_REG_OTPCGHCVS,
353 MAX8997_REG_SAFEOUTCTRL,
354 MAX8997_REG_LBCNFG1,
355 MAX8997_REG_LBCNFG2,
356 MAX8997_REG_BBCCTRL,
357
358 MAX8997_REG_FLASH1_CUR,
359 MAX8997_REG_FLASH2_CUR,
360 MAX8997_REG_MOVIE_CUR,
361 MAX8997_REG_GSMB_CUR,
362 MAX8997_REG_BOOST_CNTL,
363 MAX8997_REG_LEN_CNTL,
364 MAX8997_REG_FLASH_CNTL,
365 MAX8997_REG_WDT_CNTL,
366 MAX8997_REG_MAXFLASH1,
367 MAX8997_REG_MAXFLASH2,
368 MAX8997_REG_FLASHSTATUSMASK,
369
370 MAX8997_REG_GPIOCNTL1,
371 MAX8997_REG_GPIOCNTL2,
372 MAX8997_REG_GPIOCNTL3,
373 MAX8997_REG_GPIOCNTL4,
374 MAX8997_REG_GPIOCNTL5,
375 MAX8997_REG_GPIOCNTL6,
376 MAX8997_REG_GPIOCNTL7,
377 MAX8997_REG_GPIOCNTL8,
378 MAX8997_REG_GPIOCNTL9,
379 MAX8997_REG_GPIOCNTL10,
380 MAX8997_REG_GPIOCNTL11,
381 MAX8997_REG_GPIOCNTL12,
382
383 MAX8997_REG_LDO1CONFIG,
384 MAX8997_REG_LDO2CONFIG,
385 MAX8997_REG_LDO3CONFIG,
386 MAX8997_REG_LDO4CONFIG,
387 MAX8997_REG_LDO5CONFIG,
388 MAX8997_REG_LDO6CONFIG,
389 MAX8997_REG_LDO7CONFIG,
390 MAX8997_REG_LDO8CONFIG,
391 MAX8997_REG_LDO9CONFIG,
392 MAX8997_REG_LDO10CONFIG,
393 MAX8997_REG_LDO11CONFIG,
394 MAX8997_REG_LDO12CONFIG,
395 MAX8997_REG_LDO13CONFIG,
396 MAX8997_REG_LDO14CONFIG,
397 MAX8997_REG_LDO15CONFIG,
398 MAX8997_REG_LDO16CONFIG,
399 MAX8997_REG_LDO17CONFIG,
400 MAX8997_REG_LDO18CONFIG,
401 MAX8997_REG_LDO21CONFIG,
402
403 MAX8997_REG_DVSOKTIMER1,
404 MAX8997_REG_DVSOKTIMER2,
405 MAX8997_REG_DVSOKTIMER4,
406 MAX8997_REG_DVSOKTIMER5,
407};
408
409static u8 max8997_dumpaddr_muic[] = {
410 MAX8997_MUIC_REG_INTMASK1,
411 MAX8997_MUIC_REG_INTMASK2,
412 MAX8997_MUIC_REG_INTMASK3,
413 MAX8997_MUIC_REG_CDETCTRL,
414 MAX8997_MUIC_REG_CONTROL1,
415 MAX8997_MUIC_REG_CONTROL2,
416 MAX8997_MUIC_REG_CONTROL3,
417};
418
419static u8 max8997_dumpaddr_haptic[] = {
420 MAX8997_HAPTIC_REG_CONF1,
421 MAX8997_HAPTIC_REG_CONF2,
422 MAX8997_HAPTIC_REG_DRVCONF,
423 MAX8997_HAPTIC_REG_CYCLECONF1,
424 MAX8997_HAPTIC_REG_CYCLECONF2,
425 MAX8997_HAPTIC_REG_SIGCONF1,
426 MAX8997_HAPTIC_REG_SIGCONF2,
427 MAX8997_HAPTIC_REG_SIGCONF3,
428 MAX8997_HAPTIC_REG_SIGCONF4,
429 MAX8997_HAPTIC_REG_SIGDC1,
430 MAX8997_HAPTIC_REG_SIGDC2,
431 MAX8997_HAPTIC_REG_SIGPWMDC1,
432 MAX8997_HAPTIC_REG_SIGPWMDC2,
433 MAX8997_HAPTIC_REG_SIGPWMDC3,
434 MAX8997_HAPTIC_REG_SIGPWMDC4,
435};
436
437static int max8997_freeze(struct device *dev)
438{
439 struct i2c_client *i2c = container_of(dev, struct i2c_client, dev);
440 struct max8997_dev *max8997 = i2c_get_clientdata(i2c);
441 int i;
442
443 for (i = 0; i < ARRAY_SIZE(max8997_dumpaddr_pmic); i++)
444 max8997_read_reg(i2c, max8997_dumpaddr_pmic[i],
445 &max8997->reg_dump[i]);
446
447 for (i = 0; i < ARRAY_SIZE(max8997_dumpaddr_muic); i++)
448 max8997_read_reg(i2c, max8997_dumpaddr_muic[i],
449 &max8997->reg_dump[i + MAX8997_REG_PMIC_END]);
450
451 for (i = 0; i < ARRAY_SIZE(max8997_dumpaddr_haptic); i++)
452 max8997_read_reg(i2c, max8997_dumpaddr_haptic[i],
453 &max8997->reg_dump[i + MAX8997_REG_PMIC_END +
454 MAX8997_MUIC_REG_END]);
455
456 return 0;
457}
458
459static int max8997_restore(struct device *dev)
460{
461 struct i2c_client *i2c = container_of(dev, struct i2c_client, dev);
462 struct max8997_dev *max8997 = i2c_get_clientdata(i2c);
463 int i;
464
465 for (i = 0; i < ARRAY_SIZE(max8997_dumpaddr_pmic); i++)
466 max8997_write_reg(i2c, max8997_dumpaddr_pmic[i],
467 max8997->reg_dump[i]);
468
469 for (i = 0; i < ARRAY_SIZE(max8997_dumpaddr_muic); i++)
470 max8997_write_reg(i2c, max8997_dumpaddr_muic[i],
471 max8997->reg_dump[i + MAX8997_REG_PMIC_END]);
472
473 for (i = 0; i < ARRAY_SIZE(max8997_dumpaddr_haptic); i++)
474 max8997_write_reg(i2c, max8997_dumpaddr_haptic[i],
475 max8997->reg_dump[i + MAX8997_REG_PMIC_END +
476 MAX8997_MUIC_REG_END]);
477
478 return 0;
479}
480
481static int max8997_suspend(struct device *dev)
482{
483 struct i2c_client *i2c = container_of(dev, struct i2c_client, dev);
484 struct max8997_dev *max8997 = i2c_get_clientdata(i2c);
485
486 if (device_may_wakeup(dev))
487 irq_set_irq_wake(max8997->irq, 1);
488 return 0;
489}
490
491static int max8997_resume(struct device *dev)
492{
493 struct i2c_client *i2c = container_of(dev, struct i2c_client, dev);
494 struct max8997_dev *max8997 = i2c_get_clientdata(i2c);
495
496 if (device_may_wakeup(dev))
497 irq_set_irq_wake(max8997->irq, 0);
498 return max8997_irq_resume(max8997);
499}
500
501static const struct dev_pm_ops max8997_pm = {
502 .suspend = max8997_suspend,
503 .resume = max8997_resume,
504 .freeze = max8997_freeze,
505 .restore = max8997_restore,
506};
507
508static struct i2c_driver max8997_i2c_driver = {
509 .driver = {
510 .name = "max8997",
511 .owner = THIS_MODULE,
512 .pm = &max8997_pm,
513 .of_match_table = of_match_ptr(max8997_pmic_dt_match),
514 },
515 .probe = max8997_i2c_probe,
516 .remove = max8997_i2c_remove,
517 .id_table = max8997_i2c_id,
518};
519
520static int __init max8997_i2c_init(void)
521{
522 return i2c_add_driver(&max8997_i2c_driver);
523}
524
525subsys_initcall(max8997_i2c_init);
526
527static void __exit max8997_i2c_exit(void)
528{
529 i2c_del_driver(&max8997_i2c_driver);
530}
531module_exit(max8997_i2c_exit);
532
533MODULE_DESCRIPTION("MAXIM 8997 multi-function core driver");
534MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
535MODULE_LICENSE("GPL");
536