Commit | Line | Data |
---|---|---|
70c6cce0 QZ |
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 | ||
03dcc544 CX |
21 | /* 88pm80x chips have same definition for chip id register. */ |
22 | #define PM80X_CHIP_ID (0x00) | |
23 | #define PM80X_CHIP_ID_NUM(x) (((x) >> 5) & 0x7) | |
24 | #define PM80X_CHIP_ID_REVISION(x) ((x) & 0x1F) | |
25 | ||
26 | struct pm80x_chip_mapping { | |
27 | unsigned int id; | |
28 | int type; | |
29 | }; | |
30 | ||
31 | static struct pm80x_chip_mapping chip_mapping[] = { | |
32 | /* 88PM800 chip id number */ | |
33 | {0x3, CHIP_PM800}, | |
34 | /* 88PM805 chip id number */ | |
35 | {0x0, CHIP_PM805}, | |
36 | }; | |
37 | ||
5500e396 QZ |
38 | /* |
39 | * workaround: some registers needed by pm805 are defined in pm800, so | |
40 | * need to use this global variable to maintain the relation between | |
41 | * pm800 and pm805. would remove it after HW chip fixes the issue. | |
42 | */ | |
43 | static struct pm80x_chip *g_pm80x_chip; | |
70c6cce0 QZ |
44 | |
45 | const struct regmap_config pm80x_regmap_config = { | |
46 | .reg_bits = 8, | |
47 | .val_bits = 8, | |
48 | }; | |
78a73e59 | 49 | EXPORT_SYMBOL_GPL(pm80x_regmap_config); |
70c6cce0 | 50 | |
03dcc544 CX |
51 | |
52 | int pm80x_init(struct i2c_client *client) | |
70c6cce0 QZ |
53 | { |
54 | struct pm80x_chip *chip; | |
55 | struct regmap *map; | |
03dcc544 CX |
56 | unsigned int val; |
57 | int i, ret = 0; | |
70c6cce0 QZ |
58 | |
59 | chip = | |
60 | devm_kzalloc(&client->dev, sizeof(struct pm80x_chip), GFP_KERNEL); | |
61 | if (!chip) | |
62 | return -ENOMEM; | |
63 | ||
64 | map = devm_regmap_init_i2c(client, &pm80x_regmap_config); | |
65 | if (IS_ERR(map)) { | |
66 | ret = PTR_ERR(map); | |
67 | dev_err(&client->dev, "Failed to allocate register map: %d\n", | |
68 | ret); | |
306df798 | 69 | return ret; |
70c6cce0 QZ |
70 | } |
71 | ||
70c6cce0 QZ |
72 | chip->client = client; |
73 | chip->regmap = map; | |
74 | ||
75 | chip->irq = client->irq; | |
76 | ||
77 | chip->dev = &client->dev; | |
78 | dev_set_drvdata(chip->dev, chip); | |
79 | i2c_set_clientdata(chip->client, chip); | |
80 | ||
03dcc544 CX |
81 | ret = regmap_read(chip->regmap, PM80X_CHIP_ID, &val); |
82 | if (ret < 0) { | |
83 | dev_err(chip->dev, "Failed to read CHIP ID: %d\n", ret); | |
84 | return ret; | |
85 | } | |
86 | ||
87 | for (i = 0; i < ARRAY_SIZE(chip_mapping); i++) { | |
88 | if (chip_mapping[i].id == PM80X_CHIP_ID_NUM(val)) { | |
89 | chip->type = chip_mapping[i].type; | |
90 | break; | |
91 | } | |
92 | } | |
93 | ||
94 | if (i == ARRAY_SIZE(chip_mapping)) { | |
95 | dev_err(chip->dev, | |
96 | "Failed to detect Marvell 88PM800:ChipID[0x%x]\n", val); | |
97 | return -EINVAL; | |
98 | } | |
99 | ||
70c6cce0 QZ |
100 | device_init_wakeup(&client->dev, 1); |
101 | ||
5500e396 QZ |
102 | /* |
103 | * workaround: set g_pm80x_chip to the first probed chip. if the | |
104 | * second chip is probed, just point to the companion to each | |
105 | * other so that pm805 can access those specific register. would | |
106 | * remove it after HW chip fixes the issue. | |
107 | */ | |
108 | if (!g_pm80x_chip) | |
109 | g_pm80x_chip = chip; | |
110 | else { | |
111 | chip->companion = g_pm80x_chip->client; | |
112 | g_pm80x_chip->companion = chip->client; | |
113 | } | |
114 | ||
70c6cce0 | 115 | return 0; |
70c6cce0 QZ |
116 | } |
117 | EXPORT_SYMBOL_GPL(pm80x_init); | |
118 | ||
306df798 | 119 | int pm80x_deinit(void) |
70c6cce0 | 120 | { |
5500e396 QZ |
121 | /* |
122 | * workaround: clear the dependency between pm800 and pm805. | |
123 | * would remove it after HW chip fixes the issue. | |
124 | */ | |
125 | if (g_pm80x_chip->companion) | |
126 | g_pm80x_chip->companion = NULL; | |
127 | else | |
128 | g_pm80x_chip = NULL; | |
70c6cce0 QZ |
129 | return 0; |
130 | } | |
131 | EXPORT_SYMBOL_GPL(pm80x_deinit); | |
132 | ||
133 | #ifdef CONFIG_PM_SLEEP | |
134 | static int pm80x_suspend(struct device *dev) | |
135 | { | |
136 | struct i2c_client *client = container_of(dev, struct i2c_client, dev); | |
137 | struct pm80x_chip *chip = i2c_get_clientdata(client); | |
138 | ||
139 | if (chip && chip->wu_flag) | |
140 | if (device_may_wakeup(chip->dev)) | |
141 | enable_irq_wake(chip->irq); | |
142 | ||
143 | return 0; | |
144 | } | |
145 | ||
146 | static int pm80x_resume(struct device *dev) | |
147 | { | |
148 | struct i2c_client *client = container_of(dev, struct i2c_client, dev); | |
149 | struct pm80x_chip *chip = i2c_get_clientdata(client); | |
150 | ||
151 | if (chip && chip->wu_flag) | |
152 | if (device_may_wakeup(chip->dev)) | |
153 | disable_irq_wake(chip->irq); | |
154 | ||
155 | return 0; | |
156 | } | |
157 | #endif | |
158 | ||
159 | SIMPLE_DEV_PM_OPS(pm80x_pm_ops, pm80x_suspend, pm80x_resume); | |
160 | EXPORT_SYMBOL_GPL(pm80x_pm_ops); | |
161 | ||
162 | MODULE_DESCRIPTION("I2C Driver for Marvell 88PM80x"); | |
163 | MODULE_AUTHOR("Qiao Zhou <zhouqiao@marvell.com>"); | |
164 | MODULE_LICENSE("GPL"); |