diff options
Diffstat (limited to 'drivers/mfd/adp5520.c')
-rw-r--r-- | drivers/mfd/adp5520.c | 378 |
1 files changed, 378 insertions, 0 deletions
diff --git a/drivers/mfd/adp5520.c b/drivers/mfd/adp5520.c new file mode 100644 index 000000000000..401a9b6029e3 --- /dev/null +++ b/drivers/mfd/adp5520.c | |||
@@ -0,0 +1,378 @@ | |||
1 | /* | ||
2 | * Base driver for Analog Devices ADP5520/ADP5501 MFD PMICs | ||
3 | * LCD Backlight: drivers/video/backlight/adp5520_bl | ||
4 | * LEDs : drivers/led/leds-adp5520 | ||
5 | * GPIO : drivers/gpio/adp5520-gpio (ADP5520 only) | ||
6 | * Keys : drivers/input/keyboard/adp5520-keys (ADP5520 only) | ||
7 | * | ||
8 | * Copyright 2009 Analog Devices Inc. | ||
9 | * | ||
10 | * Derived from da903x: | ||
11 | * Copyright (C) 2008 Compulab, Ltd. | ||
12 | * Mike Rapoport <mike@compulab.co.il> | ||
13 | * | ||
14 | * Copyright (C) 2006-2008 Marvell International Ltd. | ||
15 | * Eric Miao <eric.miao@marvell.com> | ||
16 | * | ||
17 | * Licensed under the GPL-2 or later. | ||
18 | */ | ||
19 | |||
20 | #include <linux/kernel.h> | ||
21 | #include <linux/module.h> | ||
22 | #include <linux/platform_device.h> | ||
23 | #include <linux/init.h> | ||
24 | #include <linux/interrupt.h> | ||
25 | #include <linux/irq.h> | ||
26 | #include <linux/err.h> | ||
27 | #include <linux/i2c.h> | ||
28 | |||
29 | #include <linux/mfd/adp5520.h> | ||
30 | |||
31 | struct adp5520_chip { | ||
32 | struct i2c_client *client; | ||
33 | struct device *dev; | ||
34 | struct mutex lock; | ||
35 | struct blocking_notifier_head notifier_list; | ||
36 | int irq; | ||
37 | unsigned long id; | ||
38 | }; | ||
39 | |||
40 | static int __adp5520_read(struct i2c_client *client, | ||
41 | int reg, uint8_t *val) | ||
42 | { | ||
43 | int ret; | ||
44 | |||
45 | ret = i2c_smbus_read_byte_data(client, reg); | ||
46 | if (ret < 0) { | ||
47 | dev_err(&client->dev, "failed reading at 0x%02x\n", reg); | ||
48 | return ret; | ||
49 | } | ||
50 | |||
51 | *val = (uint8_t)ret; | ||
52 | return 0; | ||
53 | } | ||
54 | |||
55 | static int __adp5520_write(struct i2c_client *client, | ||
56 | int reg, uint8_t val) | ||
57 | { | ||
58 | int ret; | ||
59 | |||
60 | ret = i2c_smbus_write_byte_data(client, reg, val); | ||
61 | if (ret < 0) { | ||
62 | dev_err(&client->dev, "failed writing 0x%02x to 0x%02x\n", | ||
63 | val, reg); | ||
64 | return ret; | ||
65 | } | ||
66 | return 0; | ||
67 | } | ||
68 | |||
69 | int __adp5520_ack_bits(struct i2c_client *client, int reg, uint8_t bit_mask) | ||
70 | { | ||
71 | struct adp5520_chip *chip = i2c_get_clientdata(client); | ||
72 | uint8_t reg_val; | ||
73 | int ret; | ||
74 | |||
75 | mutex_lock(&chip->lock); | ||
76 | |||
77 | ret = __adp5520_read(client, reg, ®_val); | ||
78 | |||
79 | if (!ret) { | ||
80 | reg_val |= bit_mask; | ||
81 | ret = __adp5520_write(client, reg, reg_val); | ||
82 | } | ||
83 | |||
84 | mutex_unlock(&chip->lock); | ||
85 | return ret; | ||
86 | } | ||
87 | |||
88 | int adp5520_write(struct device *dev, int reg, uint8_t val) | ||
89 | { | ||
90 | return __adp5520_write(to_i2c_client(dev), reg, val); | ||
91 | } | ||
92 | EXPORT_SYMBOL_GPL(adp5520_write); | ||
93 | |||
94 | int adp5520_read(struct device *dev, int reg, uint8_t *val) | ||
95 | { | ||
96 | return __adp5520_read(to_i2c_client(dev), reg, val); | ||
97 | } | ||
98 | EXPORT_SYMBOL_GPL(adp5520_read); | ||
99 | |||
100 | int adp5520_set_bits(struct device *dev, int reg, uint8_t bit_mask) | ||
101 | { | ||
102 | struct adp5520_chip *chip = dev_get_drvdata(dev); | ||
103 | uint8_t reg_val; | ||
104 | int ret; | ||
105 | |||
106 | mutex_lock(&chip->lock); | ||
107 | |||
108 | ret = __adp5520_read(chip->client, reg, ®_val); | ||
109 | |||
110 | if (!ret && ((reg_val & bit_mask) == 0)) { | ||
111 | reg_val |= bit_mask; | ||
112 | ret = __adp5520_write(chip->client, reg, reg_val); | ||
113 | } | ||
114 | |||
115 | mutex_unlock(&chip->lock); | ||
116 | return ret; | ||
117 | } | ||
118 | EXPORT_SYMBOL_GPL(adp5520_set_bits); | ||
119 | |||
120 | int adp5520_clr_bits(struct device *dev, int reg, uint8_t bit_mask) | ||
121 | { | ||
122 | struct adp5520_chip *chip = dev_get_drvdata(dev); | ||
123 | uint8_t reg_val; | ||
124 | int ret; | ||
125 | |||
126 | mutex_lock(&chip->lock); | ||
127 | |||
128 | ret = __adp5520_read(chip->client, reg, ®_val); | ||
129 | |||
130 | if (!ret && (reg_val & bit_mask)) { | ||
131 | reg_val &= ~bit_mask; | ||
132 | ret = __adp5520_write(chip->client, reg, reg_val); | ||
133 | } | ||
134 | |||
135 | mutex_unlock(&chip->lock); | ||
136 | return ret; | ||
137 | } | ||
138 | EXPORT_SYMBOL_GPL(adp5520_clr_bits); | ||
139 | |||
140 | int adp5520_register_notifier(struct device *dev, struct notifier_block *nb, | ||
141 | unsigned int events) | ||
142 | { | ||
143 | struct adp5520_chip *chip = dev_get_drvdata(dev); | ||
144 | |||
145 | if (chip->irq) { | ||
146 | adp5520_set_bits(chip->dev, ADP5520_INTERRUPT_ENABLE, | ||
147 | events & (ADP5520_KP_IEN | ADP5520_KR_IEN | | ||
148 | ADP5520_OVP_IEN | ADP5520_CMPR_IEN)); | ||
149 | |||
150 | return blocking_notifier_chain_register(&chip->notifier_list, | ||
151 | nb); | ||
152 | } | ||
153 | |||
154 | return -ENODEV; | ||
155 | } | ||
156 | EXPORT_SYMBOL_GPL(adp5520_register_notifier); | ||
157 | |||
158 | int adp5520_unregister_notifier(struct device *dev, struct notifier_block *nb, | ||
159 | unsigned int events) | ||
160 | { | ||
161 | struct adp5520_chip *chip = dev_get_drvdata(dev); | ||
162 | |||
163 | adp5520_clr_bits(chip->dev, ADP5520_INTERRUPT_ENABLE, | ||
164 | events & (ADP5520_KP_IEN | ADP5520_KR_IEN | | ||
165 | ADP5520_OVP_IEN | ADP5520_CMPR_IEN)); | ||
166 | |||
167 | return blocking_notifier_chain_unregister(&chip->notifier_list, nb); | ||
168 | } | ||
169 | EXPORT_SYMBOL_GPL(adp5520_unregister_notifier); | ||
170 | |||
171 | static irqreturn_t adp5520_irq_thread(int irq, void *data) | ||
172 | { | ||
173 | struct adp5520_chip *chip = data; | ||
174 | unsigned int events; | ||
175 | uint8_t reg_val; | ||
176 | int ret; | ||
177 | |||
178 | ret = __adp5520_read(chip->client, ADP5520_MODE_STATUS, ®_val); | ||
179 | if (ret) | ||
180 | goto out; | ||
181 | |||
182 | events = reg_val & (ADP5520_OVP_INT | ADP5520_CMPR_INT | | ||
183 | ADP5520_GPI_INT | ADP5520_KR_INT | ADP5520_KP_INT); | ||
184 | |||
185 | blocking_notifier_call_chain(&chip->notifier_list, events, NULL); | ||
186 | /* ACK, Sticky bits are W1C */ | ||
187 | __adp5520_ack_bits(chip->client, ADP5520_MODE_STATUS, events); | ||
188 | |||
189 | out: | ||
190 | return IRQ_HANDLED; | ||
191 | } | ||
192 | |||
193 | static int __remove_subdev(struct device *dev, void *unused) | ||
194 | { | ||
195 | platform_device_unregister(to_platform_device(dev)); | ||
196 | return 0; | ||
197 | } | ||
198 | |||
199 | static int adp5520_remove_subdevs(struct adp5520_chip *chip) | ||
200 | { | ||
201 | return device_for_each_child(chip->dev, NULL, __remove_subdev); | ||
202 | } | ||
203 | |||
204 | static int __devinit adp5520_probe(struct i2c_client *client, | ||
205 | const struct i2c_device_id *id) | ||
206 | { | ||
207 | struct adp5520_platform_data *pdata = client->dev.platform_data; | ||
208 | struct platform_device *pdev; | ||
209 | struct adp5520_chip *chip; | ||
210 | int ret; | ||
211 | |||
212 | if (!i2c_check_functionality(client->adapter, | ||
213 | I2C_FUNC_SMBUS_BYTE_DATA)) { | ||
214 | dev_err(&client->dev, "SMBUS Word Data not Supported\n"); | ||
215 | return -EIO; | ||
216 | } | ||
217 | |||
218 | if (pdata == NULL) { | ||
219 | dev_err(&client->dev, "missing platform data\n"); | ||
220 | return -ENODEV; | ||
221 | } | ||
222 | |||
223 | chip = kzalloc(sizeof(*chip), GFP_KERNEL); | ||
224 | if (!chip) | ||
225 | return -ENOMEM; | ||
226 | |||
227 | i2c_set_clientdata(client, chip); | ||
228 | chip->client = client; | ||
229 | |||
230 | chip->dev = &client->dev; | ||
231 | chip->irq = client->irq; | ||
232 | chip->id = id->driver_data; | ||
233 | mutex_init(&chip->lock); | ||
234 | |||
235 | if (chip->irq) { | ||
236 | BLOCKING_INIT_NOTIFIER_HEAD(&chip->notifier_list); | ||
237 | |||
238 | ret = request_threaded_irq(chip->irq, NULL, adp5520_irq_thread, | ||
239 | IRQF_TRIGGER_LOW | IRQF_ONESHOT, | ||
240 | "adp5520", chip); | ||
241 | if (ret) { | ||
242 | dev_err(&client->dev, "failed to request irq %d\n", | ||
243 | chip->irq); | ||
244 | goto out_free_chip; | ||
245 | } | ||
246 | } | ||
247 | |||
248 | ret = adp5520_write(chip->dev, ADP5520_MODE_STATUS, ADP5520_nSTNBY); | ||
249 | if (ret) { | ||
250 | dev_err(&client->dev, "failed to write\n"); | ||
251 | goto out_free_irq; | ||
252 | } | ||
253 | |||
254 | if (pdata->keys) { | ||
255 | pdev = platform_device_register_data(chip->dev, "adp5520-keys", | ||
256 | chip->id, pdata->keys, sizeof(*pdata->keys)); | ||
257 | if (IS_ERR(pdev)) { | ||
258 | ret = PTR_ERR(pdev); | ||
259 | goto out_remove_subdevs; | ||
260 | } | ||
261 | } | ||
262 | |||
263 | if (pdata->gpio) { | ||
264 | pdev = platform_device_register_data(chip->dev, "adp5520-gpio", | ||
265 | chip->id, pdata->gpio, sizeof(*pdata->gpio)); | ||
266 | if (IS_ERR(pdev)) { | ||
267 | ret = PTR_ERR(pdev); | ||
268 | goto out_remove_subdevs; | ||
269 | } | ||
270 | } | ||
271 | |||
272 | if (pdata->leds) { | ||
273 | pdev = platform_device_register_data(chip->dev, "adp5520-led", | ||
274 | chip->id, pdata->leds, sizeof(*pdata->leds)); | ||
275 | if (IS_ERR(pdev)) { | ||
276 | ret = PTR_ERR(pdev); | ||
277 | goto out_remove_subdevs; | ||
278 | } | ||
279 | } | ||
280 | |||
281 | if (pdata->backlight) { | ||
282 | pdev = platform_device_register_data(chip->dev, | ||
283 | "adp5520-backlight", | ||
284 | chip->id, | ||
285 | pdata->backlight, | ||
286 | sizeof(*pdata->backlight)); | ||
287 | if (IS_ERR(pdev)) { | ||
288 | ret = PTR_ERR(pdev); | ||
289 | goto out_remove_subdevs; | ||
290 | } | ||
291 | } | ||
292 | |||
293 | return 0; | ||
294 | |||
295 | out_remove_subdevs: | ||
296 | adp5520_remove_subdevs(chip); | ||
297 | |||
298 | out_free_irq: | ||
299 | if (chip->irq) | ||
300 | free_irq(chip->irq, chip); | ||
301 | |||
302 | out_free_chip: | ||
303 | i2c_set_clientdata(client, NULL); | ||
304 | kfree(chip); | ||
305 | |||
306 | return ret; | ||
307 | } | ||
308 | |||
309 | static int __devexit adp5520_remove(struct i2c_client *client) | ||
310 | { | ||
311 | struct adp5520_chip *chip = dev_get_drvdata(&client->dev); | ||
312 | |||
313 | if (chip->irq) | ||
314 | free_irq(chip->irq, chip); | ||
315 | |||
316 | adp5520_remove_subdevs(chip); | ||
317 | adp5520_write(chip->dev, ADP5520_MODE_STATUS, 0); | ||
318 | i2c_set_clientdata(client, NULL); | ||
319 | kfree(chip); | ||
320 | return 0; | ||
321 | } | ||
322 | |||
323 | #ifdef CONFIG_PM | ||
324 | static int adp5520_suspend(struct i2c_client *client, | ||
325 | pm_message_t state) | ||
326 | { | ||
327 | struct adp5520_chip *chip = dev_get_drvdata(&client->dev); | ||
328 | |||
329 | adp5520_clr_bits(chip->dev, ADP5520_MODE_STATUS, ADP5520_nSTNBY); | ||
330 | return 0; | ||
331 | } | ||
332 | |||
333 | static int adp5520_resume(struct i2c_client *client) | ||
334 | { | ||
335 | struct adp5520_chip *chip = dev_get_drvdata(&client->dev); | ||
336 | |||
337 | adp5520_set_bits(chip->dev, ADP5520_MODE_STATUS, ADP5520_nSTNBY); | ||
338 | return 0; | ||
339 | } | ||
340 | #else | ||
341 | #define adp5520_suspend NULL | ||
342 | #define adp5520_resume NULL | ||
343 | #endif | ||
344 | |||
345 | static const struct i2c_device_id adp5520_id[] = { | ||
346 | { "pmic-adp5520", ID_ADP5520 }, | ||
347 | { "pmic-adp5501", ID_ADP5501 }, | ||
348 | { } | ||
349 | }; | ||
350 | MODULE_DEVICE_TABLE(i2c, adp5520_id); | ||
351 | |||
352 | static struct i2c_driver adp5520_driver = { | ||
353 | .driver = { | ||
354 | .name = "adp5520", | ||
355 | .owner = THIS_MODULE, | ||
356 | }, | ||
357 | .probe = adp5520_probe, | ||
358 | .remove = __devexit_p(adp5520_remove), | ||
359 | .suspend = adp5520_suspend, | ||
360 | .resume = adp5520_resume, | ||
361 | .id_table = adp5520_id, | ||
362 | }; | ||
363 | |||
364 | static int __init adp5520_init(void) | ||
365 | { | ||
366 | return i2c_add_driver(&adp5520_driver); | ||
367 | } | ||
368 | module_init(adp5520_init); | ||
369 | |||
370 | static void __exit adp5520_exit(void) | ||
371 | { | ||
372 | i2c_del_driver(&adp5520_driver); | ||
373 | } | ||
374 | module_exit(adp5520_exit); | ||
375 | |||
376 | MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>"); | ||
377 | MODULE_DESCRIPTION("ADP5520(01) PMIC-MFD Driver"); | ||
378 | MODULE_LICENSE("GPL"); | ||