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)
8 * Copyright 2009 Analog Devices Inc.
10 * Derived from da903x:
11 * Copyright (C) 2008 Compulab, Ltd.
12 * Mike Rapoport <mike@compulab.co.il>
14 * Copyright (C) 2006-2008 Marvell International Ltd.
15 * Eric Miao <eric.miao@marvell.com>
17 * Licensed under the GPL-2 or later.
20 #include <linux/kernel.h>
21 #include <linux/module.h>
22 #include <linux/platform_device.h>
23 #include <linux/init.h>
24 #include <linux/slab.h>
25 #include <linux/interrupt.h>
26 #include <linux/irq.h>
27 #include <linux/err.h>
28 #include <linux/i2c.h>
30 #include <linux/mfd/adp5520.h>
33 struct i2c_client
*client
;
36 struct blocking_notifier_head notifier_list
;
41 static int __adp5520_read(struct i2c_client
*client
,
42 int reg
, uint8_t *val
)
46 ret
= i2c_smbus_read_byte_data(client
, reg
);
48 dev_err(&client
->dev
, "failed reading at 0x%02x\n", reg
);
56 static int __adp5520_write(struct i2c_client
*client
,
61 ret
= i2c_smbus_write_byte_data(client
, reg
, val
);
63 dev_err(&client
->dev
, "failed writing 0x%02x to 0x%02x\n",
70 static int __adp5520_ack_bits(struct i2c_client
*client
, int reg
,
73 struct adp5520_chip
*chip
= i2c_get_clientdata(client
);
77 mutex_lock(&chip
->lock
);
79 ret
= __adp5520_read(client
, reg
, ®_val
);
83 ret
= __adp5520_write(client
, reg
, reg_val
);
86 mutex_unlock(&chip
->lock
);
90 int adp5520_write(struct device
*dev
, int reg
, uint8_t val
)
92 return __adp5520_write(to_i2c_client(dev
), reg
, val
);
94 EXPORT_SYMBOL_GPL(adp5520_write
);
96 int adp5520_read(struct device
*dev
, int reg
, uint8_t *val
)
98 return __adp5520_read(to_i2c_client(dev
), reg
, val
);
100 EXPORT_SYMBOL_GPL(adp5520_read
);
102 int adp5520_set_bits(struct device
*dev
, int reg
, uint8_t bit_mask
)
104 struct adp5520_chip
*chip
= dev_get_drvdata(dev
);
108 mutex_lock(&chip
->lock
);
110 ret
= __adp5520_read(chip
->client
, reg
, ®_val
);
112 if (!ret
&& ((reg_val
& bit_mask
) == 0)) {
114 ret
= __adp5520_write(chip
->client
, reg
, reg_val
);
117 mutex_unlock(&chip
->lock
);
120 EXPORT_SYMBOL_GPL(adp5520_set_bits
);
122 int adp5520_clr_bits(struct device
*dev
, int reg
, uint8_t bit_mask
)
124 struct adp5520_chip
*chip
= dev_get_drvdata(dev
);
128 mutex_lock(&chip
->lock
);
130 ret
= __adp5520_read(chip
->client
, reg
, ®_val
);
132 if (!ret
&& (reg_val
& bit_mask
)) {
133 reg_val
&= ~bit_mask
;
134 ret
= __adp5520_write(chip
->client
, reg
, reg_val
);
137 mutex_unlock(&chip
->lock
);
140 EXPORT_SYMBOL_GPL(adp5520_clr_bits
);
142 int adp5520_register_notifier(struct device
*dev
, struct notifier_block
*nb
,
145 struct adp5520_chip
*chip
= dev_get_drvdata(dev
);
148 adp5520_set_bits(chip
->dev
, ADP5520_INTERRUPT_ENABLE
,
149 events
& (ADP5520_KP_IEN
| ADP5520_KR_IEN
|
150 ADP5520_OVP_IEN
| ADP5520_CMPR_IEN
));
152 return blocking_notifier_chain_register(&chip
->notifier_list
,
158 EXPORT_SYMBOL_GPL(adp5520_register_notifier
);
160 int adp5520_unregister_notifier(struct device
*dev
, struct notifier_block
*nb
,
163 struct adp5520_chip
*chip
= dev_get_drvdata(dev
);
165 adp5520_clr_bits(chip
->dev
, ADP5520_INTERRUPT_ENABLE
,
166 events
& (ADP5520_KP_IEN
| ADP5520_KR_IEN
|
167 ADP5520_OVP_IEN
| ADP5520_CMPR_IEN
));
169 return blocking_notifier_chain_unregister(&chip
->notifier_list
, nb
);
171 EXPORT_SYMBOL_GPL(adp5520_unregister_notifier
);
173 static irqreturn_t
adp5520_irq_thread(int irq
, void *data
)
175 struct adp5520_chip
*chip
= data
;
180 ret
= __adp5520_read(chip
->client
, ADP5520_MODE_STATUS
, ®_val
);
184 events
= reg_val
& (ADP5520_OVP_INT
| ADP5520_CMPR_INT
|
185 ADP5520_GPI_INT
| ADP5520_KR_INT
| ADP5520_KP_INT
);
187 blocking_notifier_call_chain(&chip
->notifier_list
, events
, NULL
);
188 /* ACK, Sticky bits are W1C */
189 __adp5520_ack_bits(chip
->client
, ADP5520_MODE_STATUS
, events
);
195 static int __remove_subdev(struct device
*dev
, void *unused
)
197 platform_device_unregister(to_platform_device(dev
));
201 static int adp5520_remove_subdevs(struct adp5520_chip
*chip
)
203 return device_for_each_child(chip
->dev
, NULL
, __remove_subdev
);
206 static int __devinit
adp5520_probe(struct i2c_client
*client
,
207 const struct i2c_device_id
*id
)
209 struct adp5520_platform_data
*pdata
= client
->dev
.platform_data
;
210 struct platform_device
*pdev
;
211 struct adp5520_chip
*chip
;
214 if (!i2c_check_functionality(client
->adapter
,
215 I2C_FUNC_SMBUS_BYTE_DATA
)) {
216 dev_err(&client
->dev
, "SMBUS Word Data not Supported\n");
221 dev_err(&client
->dev
, "missing platform data\n");
225 chip
= kzalloc(sizeof(*chip
), GFP_KERNEL
);
229 i2c_set_clientdata(client
, chip
);
230 chip
->client
= client
;
232 chip
->dev
= &client
->dev
;
233 chip
->irq
= client
->irq
;
234 chip
->id
= id
->driver_data
;
235 mutex_init(&chip
->lock
);
238 BLOCKING_INIT_NOTIFIER_HEAD(&chip
->notifier_list
);
240 ret
= request_threaded_irq(chip
->irq
, NULL
, adp5520_irq_thread
,
241 IRQF_TRIGGER_LOW
| IRQF_ONESHOT
,
244 dev_err(&client
->dev
, "failed to request irq %d\n",
250 ret
= adp5520_write(chip
->dev
, ADP5520_MODE_STATUS
, ADP5520_nSTNBY
);
252 dev_err(&client
->dev
, "failed to write\n");
257 pdev
= platform_device_register_data(chip
->dev
, "adp5520-keys",
258 chip
->id
, pdata
->keys
, sizeof(*pdata
->keys
));
261 goto out_remove_subdevs
;
266 pdev
= platform_device_register_data(chip
->dev
, "adp5520-gpio",
267 chip
->id
, pdata
->gpio
, sizeof(*pdata
->gpio
));
270 goto out_remove_subdevs
;
275 pdev
= platform_device_register_data(chip
->dev
, "adp5520-led",
276 chip
->id
, pdata
->leds
, sizeof(*pdata
->leds
));
279 goto out_remove_subdevs
;
283 if (pdata
->backlight
) {
284 pdev
= platform_device_register_data(chip
->dev
,
288 sizeof(*pdata
->backlight
));
291 goto out_remove_subdevs
;
298 adp5520_remove_subdevs(chip
);
302 free_irq(chip
->irq
, chip
);
310 static int __devexit
adp5520_remove(struct i2c_client
*client
)
312 struct adp5520_chip
*chip
= dev_get_drvdata(&client
->dev
);
315 free_irq(chip
->irq
, chip
);
317 adp5520_remove_subdevs(chip
);
318 adp5520_write(chip
->dev
, ADP5520_MODE_STATUS
, 0);
324 static int adp5520_suspend(struct i2c_client
*client
,
327 struct adp5520_chip
*chip
= dev_get_drvdata(&client
->dev
);
329 adp5520_clr_bits(chip
->dev
, ADP5520_MODE_STATUS
, ADP5520_nSTNBY
);
333 static int adp5520_resume(struct i2c_client
*client
)
335 struct adp5520_chip
*chip
= dev_get_drvdata(&client
->dev
);
337 adp5520_set_bits(chip
->dev
, ADP5520_MODE_STATUS
, ADP5520_nSTNBY
);
341 #define adp5520_suspend NULL
342 #define adp5520_resume NULL
345 static const struct i2c_device_id adp5520_id
[] = {
346 { "pmic-adp5520", ID_ADP5520
},
347 { "pmic-adp5501", ID_ADP5501
},
350 MODULE_DEVICE_TABLE(i2c
, adp5520_id
);
352 static struct i2c_driver adp5520_driver
= {
355 .owner
= THIS_MODULE
,
357 .probe
= adp5520_probe
,
358 .remove
= __devexit_p(adp5520_remove
),
359 .suspend
= adp5520_suspend
,
360 .resume
= adp5520_resume
,
361 .id_table
= adp5520_id
,
364 static int __init
adp5520_init(void)
366 return i2c_add_driver(&adp5520_driver
);
368 module_init(adp5520_init
);
370 static void __exit
adp5520_exit(void)
372 i2c_del_driver(&adp5520_driver
);
374 module_exit(adp5520_exit
);
376 MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
377 MODULE_DESCRIPTION("ADP5520(01) PMIC-MFD Driver");
378 MODULE_LICENSE("GPL");