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/interrupt.h>
25 #include <linux/irq.h>
26 #include <linux/err.h>
27 #include <linux/i2c.h>
29 #include <linux/mfd/adp5520.h>
32 struct i2c_client
*client
;
35 struct blocking_notifier_head notifier_list
;
40 static int __adp5520_read(struct i2c_client
*client
,
41 int reg
, uint8_t *val
)
45 ret
= i2c_smbus_read_byte_data(client
, reg
);
47 dev_err(&client
->dev
, "failed reading at 0x%02x\n", reg
);
55 static int __adp5520_write(struct i2c_client
*client
,
60 ret
= i2c_smbus_write_byte_data(client
, reg
, val
);
62 dev_err(&client
->dev
, "failed writing 0x%02x to 0x%02x\n",
69 int __adp5520_ack_bits(struct i2c_client
*client
, int reg
, uint8_t bit_mask
)
71 struct adp5520_chip
*chip
= i2c_get_clientdata(client
);
75 mutex_lock(&chip
->lock
);
77 ret
= __adp5520_read(client
, reg
, ®_val
);
81 ret
= __adp5520_write(client
, reg
, reg_val
);
84 mutex_unlock(&chip
->lock
);
88 int adp5520_write(struct device
*dev
, int reg
, uint8_t val
)
90 return __adp5520_write(to_i2c_client(dev
), reg
, val
);
92 EXPORT_SYMBOL_GPL(adp5520_write
);
94 int adp5520_read(struct device
*dev
, int reg
, uint8_t *val
)
96 return __adp5520_read(to_i2c_client(dev
), reg
, val
);
98 EXPORT_SYMBOL_GPL(adp5520_read
);
100 int adp5520_set_bits(struct device
*dev
, int reg
, uint8_t bit_mask
)
102 struct adp5520_chip
*chip
= dev_get_drvdata(dev
);
106 mutex_lock(&chip
->lock
);
108 ret
= __adp5520_read(chip
->client
, reg
, ®_val
);
110 if (!ret
&& ((reg_val
& bit_mask
) == 0)) {
112 ret
= __adp5520_write(chip
->client
, reg
, reg_val
);
115 mutex_unlock(&chip
->lock
);
118 EXPORT_SYMBOL_GPL(adp5520_set_bits
);
120 int adp5520_clr_bits(struct device
*dev
, int reg
, uint8_t bit_mask
)
122 struct adp5520_chip
*chip
= dev_get_drvdata(dev
);
126 mutex_lock(&chip
->lock
);
128 ret
= __adp5520_read(chip
->client
, reg
, ®_val
);
130 if (!ret
&& (reg_val
& bit_mask
)) {
131 reg_val
&= ~bit_mask
;
132 ret
= __adp5520_write(chip
->client
, reg
, reg_val
);
135 mutex_unlock(&chip
->lock
);
138 EXPORT_SYMBOL_GPL(adp5520_clr_bits
);
140 int adp5520_register_notifier(struct device
*dev
, struct notifier_block
*nb
,
143 struct adp5520_chip
*chip
= dev_get_drvdata(dev
);
146 adp5520_set_bits(chip
->dev
, ADP5520_INTERRUPT_ENABLE
,
147 events
& (ADP5520_KP_IEN
| ADP5520_KR_IEN
|
148 ADP5520_OVP_IEN
| ADP5520_CMPR_IEN
));
150 return blocking_notifier_chain_register(&chip
->notifier_list
,
156 EXPORT_SYMBOL_GPL(adp5520_register_notifier
);
158 int adp5520_unregister_notifier(struct device
*dev
, struct notifier_block
*nb
,
161 struct adp5520_chip
*chip
= dev_get_drvdata(dev
);
163 adp5520_clr_bits(chip
->dev
, ADP5520_INTERRUPT_ENABLE
,
164 events
& (ADP5520_KP_IEN
| ADP5520_KR_IEN
|
165 ADP5520_OVP_IEN
| ADP5520_CMPR_IEN
));
167 return blocking_notifier_chain_unregister(&chip
->notifier_list
, nb
);
169 EXPORT_SYMBOL_GPL(adp5520_unregister_notifier
);
171 static irqreturn_t
adp5520_irq_thread(int irq
, void *data
)
173 struct adp5520_chip
*chip
= data
;
178 ret
= __adp5520_read(chip
->client
, ADP5520_MODE_STATUS
, ®_val
);
182 events
= reg_val
& (ADP5520_OVP_INT
| ADP5520_CMPR_INT
|
183 ADP5520_GPI_INT
| ADP5520_KR_INT
| ADP5520_KP_INT
);
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
);
193 static int __remove_subdev(struct device
*dev
, void *unused
)
195 platform_device_unregister(to_platform_device(dev
));
199 static int adp5520_remove_subdevs(struct adp5520_chip
*chip
)
201 return device_for_each_child(chip
->dev
, NULL
, __remove_subdev
);
204 static int __devinit
adp5520_probe(struct i2c_client
*client
,
205 const struct i2c_device_id
*id
)
207 struct adp5520_platform_data
*pdata
= client
->dev
.platform_data
;
208 struct platform_device
*pdev
;
209 struct adp5520_chip
*chip
;
212 if (!i2c_check_functionality(client
->adapter
,
213 I2C_FUNC_SMBUS_BYTE_DATA
)) {
214 dev_err(&client
->dev
, "SMBUS Word Data not Supported\n");
219 dev_err(&client
->dev
, "missing platform data\n");
223 chip
= kzalloc(sizeof(*chip
), GFP_KERNEL
);
227 i2c_set_clientdata(client
, chip
);
228 chip
->client
= client
;
230 chip
->dev
= &client
->dev
;
231 chip
->irq
= client
->irq
;
232 chip
->id
= id
->driver_data
;
233 mutex_init(&chip
->lock
);
236 BLOCKING_INIT_NOTIFIER_HEAD(&chip
->notifier_list
);
238 ret
= request_threaded_irq(chip
->irq
, NULL
, adp5520_irq_thread
,
239 IRQF_TRIGGER_LOW
| IRQF_ONESHOT
,
242 dev_err(&client
->dev
, "failed to request irq %d\n",
248 ret
= adp5520_write(chip
->dev
, ADP5520_MODE_STATUS
, ADP5520_nSTNBY
);
250 dev_err(&client
->dev
, "failed to write\n");
255 pdev
= platform_device_register_data(chip
->dev
, "adp5520-keys",
256 chip
->id
, pdata
->keys
, sizeof(*pdata
->keys
));
259 goto out_remove_subdevs
;
264 pdev
= platform_device_register_data(chip
->dev
, "adp5520-gpio",
265 chip
->id
, pdata
->gpio
, sizeof(*pdata
->gpio
));
268 goto out_remove_subdevs
;
273 pdev
= platform_device_register_data(chip
->dev
, "adp5520-led",
274 chip
->id
, pdata
->leds
, sizeof(*pdata
->leds
));
277 goto out_remove_subdevs
;
281 if (pdata
->backlight
) {
282 pdev
= platform_device_register_data(chip
->dev
,
286 sizeof(*pdata
->backlight
));
289 goto out_remove_subdevs
;
296 adp5520_remove_subdevs(chip
);
300 free_irq(chip
->irq
, chip
);
303 i2c_set_clientdata(client
, NULL
);
309 static int __devexit
adp5520_remove(struct i2c_client
*client
)
311 struct adp5520_chip
*chip
= dev_get_drvdata(&client
->dev
);
314 free_irq(chip
->irq
, chip
);
316 adp5520_remove_subdevs(chip
);
317 adp5520_write(chip
->dev
, ADP5520_MODE_STATUS
, 0);
318 i2c_set_clientdata(client
, NULL
);
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");