blob: cd0bf527d7647ab706c38ba42a478751db047315 [file] [log] [blame]
Qiao Zhou70c6cce2012-07-09 14:37:32 +08001/*
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
Qiao Zhou5500e392012-07-09 14:37:33 +080021/*
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 */
26static struct pm80x_chip *g_pm80x_chip;
Qiao Zhou70c6cce2012-07-09 14:37:32 +080027
28const struct regmap_config pm80x_regmap_config = {
29 .reg_bits = 8,
30 .val_bits = 8,
31};
Axel Lin78a73e52012-07-09 22:44:21 +080032EXPORT_SYMBOL_GPL(pm80x_regmap_config);
Qiao Zhou70c6cce2012-07-09 14:37:32 +080033
34int __devinit 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 goto err_regmap_init;
52 }
53
54 chip->id = id->driver_data;
55 if (chip->id < CHIP_PM800 || chip->id > CHIP_PM805) {
56 ret = -EINVAL;
57 goto err_chip_id;
58 }
59
60 chip->client = client;
61 chip->regmap = map;
62
63 chip->irq = client->irq;
64
65 chip->dev = &client->dev;
66 dev_set_drvdata(chip->dev, chip);
67 i2c_set_clientdata(chip->client, chip);
68
69 device_init_wakeup(&client->dev, 1);
70
Qiao Zhou5500e392012-07-09 14:37:33 +080071 /*
72 * workaround: set g_pm80x_chip to the first probed chip. if the
73 * second chip is probed, just point to the companion to each
74 * other so that pm805 can access those specific register. would
75 * remove it after HW chip fixes the issue.
76 */
77 if (!g_pm80x_chip)
78 g_pm80x_chip = chip;
79 else {
80 chip->companion = g_pm80x_chip->client;
81 g_pm80x_chip->companion = chip->client;
82 }
83
Qiao Zhou70c6cce2012-07-09 14:37:32 +080084 return 0;
85
86err_chip_id:
87 regmap_exit(map);
88err_regmap_init:
89 devm_kfree(&client->dev, chip);
90 return ret;
91}
92EXPORT_SYMBOL_GPL(pm80x_init);
93
Axel Lin706c96b2012-07-11 10:01:10 +080094int pm80x_deinit(struct i2c_client *client)
Qiao Zhou70c6cce2012-07-09 14:37:32 +080095{
96 struct pm80x_chip *chip = i2c_get_clientdata(client);
97
Qiao Zhou5500e392012-07-09 14:37:33 +080098 /*
99 * workaround: clear the dependency between pm800 and pm805.
100 * would remove it after HW chip fixes the issue.
101 */
102 if (g_pm80x_chip->companion)
103 g_pm80x_chip->companion = NULL;
104 else
105 g_pm80x_chip = NULL;
106
Qiao Zhou70c6cce2012-07-09 14:37:32 +0800107 regmap_exit(chip->regmap);
108 devm_kfree(&client->dev, chip);
109
110 return 0;
111}
112EXPORT_SYMBOL_GPL(pm80x_deinit);
113
114#ifdef CONFIG_PM_SLEEP
115static int pm80x_suspend(struct device *dev)
116{
117 struct i2c_client *client = container_of(dev, struct i2c_client, dev);
118 struct pm80x_chip *chip = i2c_get_clientdata(client);
119
120 if (chip && chip->wu_flag)
121 if (device_may_wakeup(chip->dev))
122 enable_irq_wake(chip->irq);
123
124 return 0;
125}
126
127static int pm80x_resume(struct device *dev)
128{
129 struct i2c_client *client = container_of(dev, struct i2c_client, dev);
130 struct pm80x_chip *chip = i2c_get_clientdata(client);
131
132 if (chip && chip->wu_flag)
133 if (device_may_wakeup(chip->dev))
134 disable_irq_wake(chip->irq);
135
136 return 0;
137}
138#endif
139
140SIMPLE_DEV_PM_OPS(pm80x_pm_ops, pm80x_suspend, pm80x_resume);
141EXPORT_SYMBOL_GPL(pm80x_pm_ops);
142
143MODULE_DESCRIPTION("I2C Driver for Marvell 88PM80x");
144MODULE_AUTHOR("Qiao Zhou <zhouqiao@marvell.com>");
145MODULE_LICENSE("GPL");