xref: /openbmc/linux/drivers/mfd/88pm80x.c (revision ee89bd6b)
1 /*
2  * I2C driver for Marvell 88PM80x
3  *
4  * Copyright (C) 2012 Marvell International Ltd.
5  * Haojian Zhuang <haojian.zhuang@marvell.com>
6  * Joseph(Yossi) Hanin <yhanin@marvell.com>
7  * Qiao Zhou <zhouqiao@marvell.com>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License version 2 as
11  * published by the Free Software Foundation.
12  */
13 #include <linux/kernel.h>
14 #include <linux/module.h>
15 #include <linux/i2c.h>
16 #include <linux/mfd/88pm80x.h>
17 #include <linux/slab.h>
18 #include <linux/uaccess.h>
19 #include <linux/err.h>
20 
21 /*
22  * workaround: some registers needed by pm805 are defined in pm800, so
23  * need to use this global variable to maintain the relation between
24  * pm800 and pm805. would remove it after HW chip fixes the issue.
25  */
26 static struct pm80x_chip *g_pm80x_chip;
27 
28 const struct regmap_config pm80x_regmap_config = {
29 	.reg_bits = 8,
30 	.val_bits = 8,
31 };
32 EXPORT_SYMBOL_GPL(pm80x_regmap_config);
33 
34 int pm80x_init(struct i2c_client *client,
35 				 const struct i2c_device_id *id)
36 {
37 	struct pm80x_chip *chip;
38 	struct regmap *map;
39 	int ret = 0;
40 
41 	chip =
42 	    devm_kzalloc(&client->dev, sizeof(struct pm80x_chip), GFP_KERNEL);
43 	if (!chip)
44 		return -ENOMEM;
45 
46 	map = devm_regmap_init_i2c(client, &pm80x_regmap_config);
47 	if (IS_ERR(map)) {
48 		ret = PTR_ERR(map);
49 		dev_err(&client->dev, "Failed to allocate register map: %d\n",
50 			ret);
51 		return ret;
52 	}
53 
54 	chip->id = id->driver_data;
55 	if (chip->id < CHIP_PM800 || chip->id > CHIP_PM805)
56 		return -EINVAL;
57 
58 	chip->client = client;
59 	chip->regmap = map;
60 
61 	chip->irq = client->irq;
62 
63 	chip->dev = &client->dev;
64 	dev_set_drvdata(chip->dev, chip);
65 	i2c_set_clientdata(chip->client, chip);
66 
67 	device_init_wakeup(&client->dev, 1);
68 
69 	/*
70 	 * workaround: set g_pm80x_chip to the first probed chip. if the
71 	 * second chip is probed, just point to the companion to each
72 	 * other so that pm805 can access those specific register. would
73 	 * remove it after HW chip fixes the issue.
74 	 */
75 	if (!g_pm80x_chip)
76 		g_pm80x_chip = chip;
77 	else {
78 		chip->companion = g_pm80x_chip->client;
79 		g_pm80x_chip->companion = chip->client;
80 	}
81 
82 	return 0;
83 }
84 EXPORT_SYMBOL_GPL(pm80x_init);
85 
86 int pm80x_deinit(void)
87 {
88 	/*
89 	 * workaround: clear the dependency between pm800 and pm805.
90 	 * would remove it after HW chip fixes the issue.
91 	 */
92 	if (g_pm80x_chip->companion)
93 		g_pm80x_chip->companion = NULL;
94 	else
95 		g_pm80x_chip = NULL;
96 	return 0;
97 }
98 EXPORT_SYMBOL_GPL(pm80x_deinit);
99 
100 #ifdef CONFIG_PM_SLEEP
101 static int pm80x_suspend(struct device *dev)
102 {
103 	struct i2c_client *client = container_of(dev, struct i2c_client, dev);
104 	struct pm80x_chip *chip = i2c_get_clientdata(client);
105 
106 	if (chip && chip->wu_flag)
107 		if (device_may_wakeup(chip->dev))
108 			enable_irq_wake(chip->irq);
109 
110 	return 0;
111 }
112 
113 static int pm80x_resume(struct device *dev)
114 {
115 	struct i2c_client *client = container_of(dev, struct i2c_client, dev);
116 	struct pm80x_chip *chip = i2c_get_clientdata(client);
117 
118 	if (chip && chip->wu_flag)
119 		if (device_may_wakeup(chip->dev))
120 			disable_irq_wake(chip->irq);
121 
122 	return 0;
123 }
124 #endif
125 
126 SIMPLE_DEV_PM_OPS(pm80x_pm_ops, pm80x_suspend, pm80x_resume);
127 EXPORT_SYMBOL_GPL(pm80x_pm_ops);
128 
129 MODULE_DESCRIPTION("I2C Driver for Marvell 88PM80x");
130 MODULE_AUTHOR("Qiao Zhou <zhouqiao@marvell.com>");
131 MODULE_LICENSE("GPL");
132