Blame view

drivers/mfd/88pm80x.c 3.79 KB
d2912cb15   Thomas Gleixner   treewide: Replace...
1
  // SPDX-License-Identifier: GPL-2.0-only
70c6cce04   Qiao Zhou   mfd: Support 88pm...
2
3
4
5
6
7
8
  /*
   * I2C driver for Marvell 88PM80x
   *
   * Copyright (C) 2012 Marvell International Ltd.
   * Haojian Zhuang <haojian.zhuang@marvell.com>
   * Joseph(Yossi) Hanin <yhanin@marvell.com>
   * Qiao Zhou <zhouqiao@marvell.com>
70c6cce04   Qiao Zhou   mfd: Support 88pm...
9
10
11
12
13
14
15
16
   */
  #include <linux/kernel.h>
  #include <linux/module.h>
  #include <linux/i2c.h>
  #include <linux/mfd/88pm80x.h>
  #include <linux/slab.h>
  #include <linux/uaccess.h>
  #include <linux/err.h>
03dcc544b   Chao Xie   mfd: 88pm80x: Cha...
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
  /* 88pm80x chips have same definition for chip id register. */
  #define PM80X_CHIP_ID			(0x00)
  #define PM80X_CHIP_ID_NUM(x)		(((x) >> 5) & 0x7)
  #define PM80X_CHIP_ID_REVISION(x)	((x) & 0x1F)
  
  struct pm80x_chip_mapping {
  	unsigned int	id;
  	int		type;
  };
  
  static struct pm80x_chip_mapping chip_mapping[] = {
  	/* 88PM800 chip id number */
  	{0x3,	CHIP_PM800},
  	/* 88PM805 chip id number */
  	{0x0,	CHIP_PM805},
62a2e6334   Vaibhav Hiremath   mfd: 88pm80x: Add...
32
33
  	/* 88PM860 chip id number */
  	{0x4,	CHIP_PM860},
03dcc544b   Chao Xie   mfd: 88pm80x: Cha...
34
  };
5500e3964   Qiao Zhou   mfd: Add companio...
35
36
37
38
39
40
  /*
   * workaround: some registers needed by pm805 are defined in pm800, so
   * need to use this global variable to maintain the relation between
   * pm800 and pm805. would remove it after HW chip fixes the issue.
   */
  static struct pm80x_chip *g_pm80x_chip;
70c6cce04   Qiao Zhou   mfd: Support 88pm...
41
42
43
44
45
  
  const struct regmap_config pm80x_regmap_config = {
  	.reg_bits = 8,
  	.val_bits = 8,
  };
78a73e59d   Axel Lin   mfd: Export pm80x...
46
  EXPORT_SYMBOL_GPL(pm80x_regmap_config);
70c6cce04   Qiao Zhou   mfd: Support 88pm...
47

03dcc544b   Chao Xie   mfd: 88pm80x: Cha...
48
49
  
  int pm80x_init(struct i2c_client *client)
70c6cce04   Qiao Zhou   mfd: Support 88pm...
50
51
52
  {
  	struct pm80x_chip *chip;
  	struct regmap *map;
03dcc544b   Chao Xie   mfd: 88pm80x: Cha...
53
54
  	unsigned int val;
  	int i, ret = 0;
70c6cce04   Qiao Zhou   mfd: Support 88pm...
55
56
57
58
59
60
61
62
63
64
65
66
  
  	chip =
  	    devm_kzalloc(&client->dev, sizeof(struct pm80x_chip), GFP_KERNEL);
  	if (!chip)
  		return -ENOMEM;
  
  	map = devm_regmap_init_i2c(client, &pm80x_regmap_config);
  	if (IS_ERR(map)) {
  		ret = PTR_ERR(map);
  		dev_err(&client->dev, "Failed to allocate register map: %d
  ",
  			ret);
306df7985   Yi Zhang   mfd: 88pm80x: Rem...
67
  		return ret;
70c6cce04   Qiao Zhou   mfd: Support 88pm...
68
  	}
70c6cce04   Qiao Zhou   mfd: Support 88pm...
69
70
71
72
73
74
75
76
  	chip->client = client;
  	chip->regmap = map;
  
  	chip->irq = client->irq;
  
  	chip->dev = &client->dev;
  	dev_set_drvdata(chip->dev, chip);
  	i2c_set_clientdata(chip->client, chip);
03dcc544b   Chao Xie   mfd: 88pm80x: Cha...
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
  	ret = regmap_read(chip->regmap, PM80X_CHIP_ID, &val);
  	if (ret < 0) {
  		dev_err(chip->dev, "Failed to read CHIP ID: %d
  ", ret);
  		return ret;
  	}
  
  	for (i = 0; i < ARRAY_SIZE(chip_mapping); i++) {
  		if (chip_mapping[i].id == PM80X_CHIP_ID_NUM(val)) {
  			chip->type = chip_mapping[i].type;
  			break;
  		}
  	}
  
  	if (i == ARRAY_SIZE(chip_mapping)) {
  		dev_err(chip->dev,
  			"Failed to detect Marvell 88PM800:ChipID[0x%x]
  ", val);
  		return -EINVAL;
  	}
70c6cce04   Qiao Zhou   mfd: Support 88pm...
97
  	device_init_wakeup(&client->dev, 1);
5500e3964   Qiao Zhou   mfd: Add companio...
98
99
100
101
102
103
104
105
106
107
108
109
  	/*
  	 * workaround: set g_pm80x_chip to the first probed chip. if the
  	 * second chip is probed, just point to the companion to each
  	 * other so that pm805 can access those specific register. would
  	 * remove it after HW chip fixes the issue.
  	 */
  	if (!g_pm80x_chip)
  		g_pm80x_chip = chip;
  	else {
  		chip->companion = g_pm80x_chip->client;
  		g_pm80x_chip->companion = chip->client;
  	}
70c6cce04   Qiao Zhou   mfd: Support 88pm...
110
  	return 0;
70c6cce04   Qiao Zhou   mfd: Support 88pm...
111
112
  }
  EXPORT_SYMBOL_GPL(pm80x_init);
306df7985   Yi Zhang   mfd: 88pm80x: Rem...
113
  int pm80x_deinit(void)
70c6cce04   Qiao Zhou   mfd: Support 88pm...
114
  {
5500e3964   Qiao Zhou   mfd: Add companio...
115
116
117
118
119
120
121
122
  	/*
  	 * workaround: clear the dependency between pm800 and pm805.
  	 * would remove it after HW chip fixes the issue.
  	 */
  	if (g_pm80x_chip->companion)
  		g_pm80x_chip->companion = NULL;
  	else
  		g_pm80x_chip = NULL;
70c6cce04   Qiao Zhou   mfd: Support 88pm...
123
124
125
126
127
128
129
  	return 0;
  }
  EXPORT_SYMBOL_GPL(pm80x_deinit);
  
  #ifdef CONFIG_PM_SLEEP
  static int pm80x_suspend(struct device *dev)
  {
1b5420e1f   Geliang Tang   mfd: Use to_i2c_c...
130
  	struct i2c_client *client = to_i2c_client(dev);
70c6cce04   Qiao Zhou   mfd: Support 88pm...
131
132
133
134
135
136
137
138
139
140
141
  	struct pm80x_chip *chip = i2c_get_clientdata(client);
  
  	if (chip && chip->wu_flag)
  		if (device_may_wakeup(chip->dev))
  			enable_irq_wake(chip->irq);
  
  	return 0;
  }
  
  static int pm80x_resume(struct device *dev)
  {
1b5420e1f   Geliang Tang   mfd: Use to_i2c_c...
142
  	struct i2c_client *client = to_i2c_client(dev);
70c6cce04   Qiao Zhou   mfd: Support 88pm...
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
  	struct pm80x_chip *chip = i2c_get_clientdata(client);
  
  	if (chip && chip->wu_flag)
  		if (device_may_wakeup(chip->dev))
  			disable_irq_wake(chip->irq);
  
  	return 0;
  }
  #endif
  
  SIMPLE_DEV_PM_OPS(pm80x_pm_ops, pm80x_suspend, pm80x_resume);
  EXPORT_SYMBOL_GPL(pm80x_pm_ops);
  
  MODULE_DESCRIPTION("I2C Driver for Marvell 88PM80x");
  MODULE_AUTHOR("Qiao Zhou <zhouqiao@marvell.com>");
  MODULE_LICENSE("GPL");