Blame view

drivers/power/goldfish_battery.c 6.65 KB
84d7b7687   Mike Lockwood   power: Add batter...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
  /*
   * Power supply driver for the goldfish emulator
   *
   * Copyright (C) 2008 Google, Inc.
   * Copyright (C) 2012 Intel, Inc.
   * Copyright (C) 2013 Intel, Inc.
   * Author: Mike Lockwood <lockwood@android.com>
   *
   * This software is licensed under the terms of the GNU General Public
   * License version 2, as published by the Free Software Foundation, and
   * may be copied, distributed, and modified under those terms.
   *
   * This program is distributed in the hope that it will be useful,
   * but WITHOUT ANY WARRANTY; without even the implied warranty of
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   * GNU General Public License for more details.
   */
  
  #include <linux/module.h>
  #include <linux/err.h>
  #include <linux/platform_device.h>
  #include <linux/power_supply.h>
  #include <linux/types.h>
  #include <linux/pci.h>
  #include <linux/interrupt.h>
  #include <linux/io.h>
fdb2f37a5   Yu Ning   goldfish: Enable ...
27
  #include <linux/acpi.h>
84d7b7687   Mike Lockwood   power: Add batter...
28
29
30
31
32
  
  struct goldfish_battery_data {
  	void __iomem *reg_base;
  	int irq;
  	spinlock_t lock;
297d716f6   Krzysztof Kozlowski   power_supply: Cha...
33
34
  	struct power_supply *battery;
  	struct power_supply *ac;
84d7b7687   Mike Lockwood   power: Add batter...
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
  };
  
  #define GOLDFISH_BATTERY_READ(data, addr) \
  	(readl(data->reg_base + addr))
  #define GOLDFISH_BATTERY_WRITE(data, addr, x) \
  	(writel(x, data->reg_base + addr))
  
  /*
   * Temporary variable used between goldfish_battery_probe() and
   * goldfish_battery_open().
   */
  static struct goldfish_battery_data *battery_data;
  
  enum {
  	/* status register */
  	BATTERY_INT_STATUS	    = 0x00,
  	/* set this to enable IRQ */
  	BATTERY_INT_ENABLE	    = 0x04,
  
  	BATTERY_AC_ONLINE       = 0x08,
  	BATTERY_STATUS          = 0x0C,
  	BATTERY_HEALTH          = 0x10,
  	BATTERY_PRESENT         = 0x14,
  	BATTERY_CAPACITY        = 0x18,
  
  	BATTERY_STATUS_CHANGED	= 1U << 0,
  	AC_STATUS_CHANGED	= 1U << 1,
  	BATTERY_INT_MASK        = BATTERY_STATUS_CHANGED | AC_STATUS_CHANGED,
  };
  
  
  static int goldfish_ac_get_property(struct power_supply *psy,
  			enum power_supply_property psp,
  			union power_supply_propval *val)
  {
297d716f6   Krzysztof Kozlowski   power_supply: Cha...
70
  	struct goldfish_battery_data *data = power_supply_get_drvdata(psy);
84d7b7687   Mike Lockwood   power: Add batter...
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
  	int ret = 0;
  
  	switch (psp) {
  	case POWER_SUPPLY_PROP_ONLINE:
  		val->intval = GOLDFISH_BATTERY_READ(data, BATTERY_AC_ONLINE);
  		break;
  	default:
  		ret = -EINVAL;
  		break;
  	}
  	return ret;
  }
  
  static int goldfish_battery_get_property(struct power_supply *psy,
  				 enum power_supply_property psp,
  				 union power_supply_propval *val)
  {
297d716f6   Krzysztof Kozlowski   power_supply: Cha...
88
  	struct goldfish_battery_data *data = power_supply_get_drvdata(psy);
84d7b7687   Mike Lockwood   power: Add batter...
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
  	int ret = 0;
  
  	switch (psp) {
  	case POWER_SUPPLY_PROP_STATUS:
  		val->intval = GOLDFISH_BATTERY_READ(data, BATTERY_STATUS);
  		break;
  	case POWER_SUPPLY_PROP_HEALTH:
  		val->intval = GOLDFISH_BATTERY_READ(data, BATTERY_HEALTH);
  		break;
  	case POWER_SUPPLY_PROP_PRESENT:
  		val->intval = GOLDFISH_BATTERY_READ(data, BATTERY_PRESENT);
  		break;
  	case POWER_SUPPLY_PROP_TECHNOLOGY:
  		val->intval = POWER_SUPPLY_TECHNOLOGY_LION;
  		break;
  	case POWER_SUPPLY_PROP_CAPACITY:
  		val->intval = GOLDFISH_BATTERY_READ(data, BATTERY_CAPACITY);
  		break;
  	default:
  		ret = -EINVAL;
  		break;
  	}
  
  	return ret;
  }
  
  static enum power_supply_property goldfish_battery_props[] = {
  	POWER_SUPPLY_PROP_STATUS,
  	POWER_SUPPLY_PROP_HEALTH,
  	POWER_SUPPLY_PROP_PRESENT,
  	POWER_SUPPLY_PROP_TECHNOLOGY,
  	POWER_SUPPLY_PROP_CAPACITY,
  };
  
  static enum power_supply_property goldfish_ac_props[] = {
  	POWER_SUPPLY_PROP_ONLINE,
  };
  
  static irqreturn_t goldfish_battery_interrupt(int irq, void *dev_id)
  {
  	unsigned long irq_flags;
  	struct goldfish_battery_data *data = dev_id;
  	uint32_t status;
  
  	spin_lock_irqsave(&data->lock, irq_flags);
  
  	/* read status flags, which will clear the interrupt */
  	status = GOLDFISH_BATTERY_READ(data, BATTERY_INT_STATUS);
  	status &= BATTERY_INT_MASK;
  
  	if (status & BATTERY_STATUS_CHANGED)
297d716f6   Krzysztof Kozlowski   power_supply: Cha...
140
  		power_supply_changed(data->battery);
84d7b7687   Mike Lockwood   power: Add batter...
141
  	if (status & AC_STATUS_CHANGED)
297d716f6   Krzysztof Kozlowski   power_supply: Cha...
142
  		power_supply_changed(data->ac);
84d7b7687   Mike Lockwood   power: Add batter...
143
144
145
146
  
  	spin_unlock_irqrestore(&data->lock, irq_flags);
  	return status ? IRQ_HANDLED : IRQ_NONE;
  }
297d716f6   Krzysztof Kozlowski   power_supply: Cha...
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
  static const struct power_supply_desc battery_desc = {
  	.properties	= goldfish_battery_props,
  	.num_properties	= ARRAY_SIZE(goldfish_battery_props),
  	.get_property	= goldfish_battery_get_property,
  	.name		= "battery",
  	.type		= POWER_SUPPLY_TYPE_BATTERY,
  };
  
  static const struct power_supply_desc ac_desc = {
  	.properties	= goldfish_ac_props,
  	.num_properties	= ARRAY_SIZE(goldfish_ac_props),
  	.get_property	= goldfish_ac_get_property,
  	.name		= "ac",
  	.type		= POWER_SUPPLY_TYPE_MAINS,
  };
84d7b7687   Mike Lockwood   power: Add batter...
162
163
164
165
166
167
  
  static int goldfish_battery_probe(struct platform_device *pdev)
  {
  	int ret;
  	struct resource *r;
  	struct goldfish_battery_data *data;
297d716f6   Krzysztof Kozlowski   power_supply: Cha...
168
  	struct power_supply_config psy_cfg = {};
84d7b7687   Mike Lockwood   power: Add batter...
169
170
171
172
173
174
  
  	data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
  	if (data == NULL)
  		return -ENOMEM;
  
  	spin_lock_init(&data->lock);
84d7b7687   Mike Lockwood   power: Add batter...
175
176
177
178
179
180
  	r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  	if (r == NULL) {
  		dev_err(&pdev->dev, "platform_get_resource failed
  ");
  		return -ENODEV;
  	}
a92d4c7d0   Silviu-Mihai Popescu   goldfish_battery:...
181
  	data->reg_base = devm_ioremap(&pdev->dev, r->start, resource_size(r));
84d7b7687   Mike Lockwood   power: Add batter...
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
  	if (data->reg_base == NULL) {
  		dev_err(&pdev->dev, "unable to remap MMIO
  ");
  		return -ENOMEM;
  	}
  
  	data->irq = platform_get_irq(pdev, 0);
  	if (data->irq < 0) {
  		dev_err(&pdev->dev, "platform_get_irq failed
  ");
  		return -ENODEV;
  	}
  
  	ret = devm_request_irq(&pdev->dev, data->irq, goldfish_battery_interrupt,
  						IRQF_SHARED, pdev->name, data);
  	if (ret)
  		return ret;
297d716f6   Krzysztof Kozlowski   power_supply: Cha...
199
  	psy_cfg.drv_data = data;
84d7b7687   Mike Lockwood   power: Add batter...
200

297d716f6   Krzysztof Kozlowski   power_supply: Cha...
201
202
203
204
205
206
207
208
209
  	data->ac = power_supply_register(&pdev->dev, &ac_desc, &psy_cfg);
  	if (IS_ERR(data->ac))
  		return PTR_ERR(data->ac);
  
  	data->battery = power_supply_register(&pdev->dev, &battery_desc,
  						&psy_cfg);
  	if (IS_ERR(data->battery)) {
  		power_supply_unregister(data->ac);
  		return PTR_ERR(data->battery);
84d7b7687   Mike Lockwood   power: Add batter...
210
211
212
213
214
215
216
217
218
219
220
221
  	}
  
  	platform_set_drvdata(pdev, data);
  	battery_data = data;
  
  	GOLDFISH_BATTERY_WRITE(data, BATTERY_INT_ENABLE, BATTERY_INT_MASK);
  	return 0;
  }
  
  static int goldfish_battery_remove(struct platform_device *pdev)
  {
  	struct goldfish_battery_data *data = platform_get_drvdata(pdev);
297d716f6   Krzysztof Kozlowski   power_supply: Cha...
222
223
  	power_supply_unregister(data->battery);
  	power_supply_unregister(data->ac);
84d7b7687   Mike Lockwood   power: Add batter...
224
225
226
  	battery_data = NULL;
  	return 0;
  }
65d687a7b   Greg Hackmann   power: goldfish_b...
227
228
229
230
231
  static const struct of_device_id goldfish_battery_of_match[] = {
  	{ .compatible = "google,goldfish-battery", },
  	{},
  };
  MODULE_DEVICE_TABLE(of, goldfish_battery_of_match);
fdb2f37a5   Yu Ning   goldfish: Enable ...
232
233
234
235
236
  static const struct acpi_device_id goldfish_battery_acpi_match[] = {
  	{ "GFSH0001", 0 },
  	{ },
  };
  MODULE_DEVICE_TABLE(acpi, goldfish_battery_acpi_match);
84d7b7687   Mike Lockwood   power: Add batter...
237
238
239
240
  static struct platform_driver goldfish_battery_device = {
  	.probe		= goldfish_battery_probe,
  	.remove		= goldfish_battery_remove,
  	.driver = {
65d687a7b   Greg Hackmann   power: goldfish_b...
241
242
  		.name = "goldfish-battery",
  		.of_match_table = goldfish_battery_of_match,
fdb2f37a5   Yu Ning   goldfish: Enable ...
243
  		.acpi_match_table = ACPI_PTR(goldfish_battery_acpi_match),
84d7b7687   Mike Lockwood   power: Add batter...
244
245
246
247
248
249
250
  	}
  };
  module_platform_driver(goldfish_battery_device);
  
  MODULE_AUTHOR("Mike Lockwood lockwood@android.com");
  MODULE_LICENSE("GPL");
  MODULE_DESCRIPTION("Battery driver for the Goldfish emulator");