Merge tag 'mmc-v6.8' of git://git.kernel.org/pub/scm/linux/kernel/git/ulfh/mmc
[linux-block.git] / drivers / mfd / 88pm80x.c
CommitLineData
d2912cb1 1// SPDX-License-Identifier: GPL-2.0-only
70c6cce0
QZ
2/*
3 * I2C driver for Marvell 88PM80x
4 *
5 * Copyright (C) 2012 Marvell International Ltd.
6 * Haojian Zhuang <haojian.zhuang@marvell.com>
7 * Joseph(Yossi) Hanin <yhanin@marvell.com>
8 * Qiao Zhou <zhouqiao@marvell.com>
70c6cce0
QZ
9 */
10#include <linux/kernel.h>
11#include <linux/module.h>
12#include <linux/i2c.h>
13#include <linux/mfd/88pm80x.h>
14#include <linux/slab.h>
15#include <linux/uaccess.h>
16#include <linux/err.h>
17
03dcc544
CX
18/* 88pm80x chips have same definition for chip id register. */
19#define PM80X_CHIP_ID (0x00)
20#define PM80X_CHIP_ID_NUM(x) (((x) >> 5) & 0x7)
21#define PM80X_CHIP_ID_REVISION(x) ((x) & 0x1F)
22
23struct pm80x_chip_mapping {
24 unsigned int id;
25 int type;
26};
27
28static struct pm80x_chip_mapping chip_mapping[] = {
29 /* 88PM800 chip id number */
30 {0x3, CHIP_PM800},
31 /* 88PM805 chip id number */
32 {0x0, CHIP_PM805},
62a2e633
VH
33 /* 88PM860 chip id number */
34 {0x4, CHIP_PM860},
03dcc544
CX
35};
36
5500e396
QZ
37/*
38 * workaround: some registers needed by pm805 are defined in pm800, so
39 * need to use this global variable to maintain the relation between
40 * pm800 and pm805. would remove it after HW chip fixes the issue.
41 */
42static struct pm80x_chip *g_pm80x_chip;
70c6cce0
QZ
43
44const struct regmap_config pm80x_regmap_config = {
45 .reg_bits = 8,
46 .val_bits = 8,
47};
78a73e59 48EXPORT_SYMBOL_GPL(pm80x_regmap_config);
70c6cce0 49
03dcc544
CX
50
51int pm80x_init(struct i2c_client *client)
70c6cce0
QZ
52{
53 struct pm80x_chip *chip;
54 struct regmap *map;
03dcc544
CX
55 unsigned int val;
56 int i, ret = 0;
70c6cce0
QZ
57
58 chip =
59 devm_kzalloc(&client->dev, sizeof(struct pm80x_chip), GFP_KERNEL);
60 if (!chip)
61 return -ENOMEM;
62
63 map = devm_regmap_init_i2c(client, &pm80x_regmap_config);
64 if (IS_ERR(map)) {
65 ret = PTR_ERR(map);
66 dev_err(&client->dev, "Failed to allocate register map: %d\n",
67 ret);
306df798 68 return ret;
70c6cce0
QZ
69 }
70
70c6cce0
QZ
71 chip->client = client;
72 chip->regmap = map;
73
74 chip->irq = client->irq;
75
76 chip->dev = &client->dev;
70c6cce0
QZ
77 i2c_set_clientdata(chip->client, chip);
78
03dcc544
CX
79 ret = regmap_read(chip->regmap, PM80X_CHIP_ID, &val);
80 if (ret < 0) {
81 dev_err(chip->dev, "Failed to read CHIP ID: %d\n", ret);
82 return ret;
83 }
84
85 for (i = 0; i < ARRAY_SIZE(chip_mapping); i++) {
86 if (chip_mapping[i].id == PM80X_CHIP_ID_NUM(val)) {
87 chip->type = chip_mapping[i].type;
88 break;
89 }
90 }
91
92 if (i == ARRAY_SIZE(chip_mapping)) {
93 dev_err(chip->dev,
94 "Failed to detect Marvell 88PM800:ChipID[0x%x]\n", val);
95 return -EINVAL;
96 }
97
70c6cce0
QZ
98 device_init_wakeup(&client->dev, 1);
99
5500e396
QZ
100 /*
101 * workaround: set g_pm80x_chip to the first probed chip. if the
102 * second chip is probed, just point to the companion to each
103 * other so that pm805 can access those specific register. would
104 * remove it after HW chip fixes the issue.
105 */
106 if (!g_pm80x_chip)
107 g_pm80x_chip = chip;
108 else {
109 chip->companion = g_pm80x_chip->client;
110 g_pm80x_chip->companion = chip->client;
111 }
112
70c6cce0 113 return 0;
70c6cce0
QZ
114}
115EXPORT_SYMBOL_GPL(pm80x_init);
116
306df798 117int pm80x_deinit(void)
70c6cce0 118{
5500e396
QZ
119 /*
120 * workaround: clear the dependency between pm800 and pm805.
121 * would remove it after HW chip fixes the issue.
122 */
123 if (g_pm80x_chip->companion)
124 g_pm80x_chip->companion = NULL;
125 else
126 g_pm80x_chip = NULL;
70c6cce0
QZ
127 return 0;
128}
129EXPORT_SYMBOL_GPL(pm80x_deinit);
130
70c6cce0
QZ
131static int pm80x_suspend(struct device *dev)
132{
1b5420e1 133 struct i2c_client *client = to_i2c_client(dev);
70c6cce0
QZ
134 struct pm80x_chip *chip = i2c_get_clientdata(client);
135
136 if (chip && chip->wu_flag)
137 if (device_may_wakeup(chip->dev))
138 enable_irq_wake(chip->irq);
139
140 return 0;
141}
142
143static int pm80x_resume(struct device *dev)
144{
1b5420e1 145 struct i2c_client *client = to_i2c_client(dev);
70c6cce0
QZ
146 struct pm80x_chip *chip = i2c_get_clientdata(client);
147
148 if (chip && chip->wu_flag)
149 if (device_may_wakeup(chip->dev))
150 disable_irq_wake(chip->irq);
151
152 return 0;
153}
70c6cce0 154
19755a0a 155EXPORT_GPL_SIMPLE_DEV_PM_OPS(pm80x_pm_ops, pm80x_suspend, pm80x_resume);
70c6cce0
QZ
156
157MODULE_DESCRIPTION("I2C Driver for Marvell 88PM80x");
158MODULE_AUTHOR("Qiao Zhou <zhouqiao@marvell.com>");
159MODULE_LICENSE("GPL");