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 * Author: Michael Hennerich <michael.hennerich@analog.com>
12 * Derived from da903x:
13 * Copyright (C) 2008 Compulab, Ltd.
14 * Mike Rapoport <mike@compulab.co.il>
16 * Copyright (C) 2006-2008 Marvell International Ltd.
17 * Eric Miao <eric.miao@marvell.com>
19 * Licensed under the GPL-2 or later.
22 #include <linux/kernel.h>
23 #include <linux/init.h>
24 #include <linux/platform_device.h>
25 #include <linux/slab.h>
26 #include <linux/interrupt.h>
27 #include <linux/irq.h>
28 #include <linux/err.h>
29 #include <linux/i2c.h>
31 #include <linux/mfd/adp5520.h>
34 struct i2c_client
*client
;
37 struct blocking_notifier_head notifier_list
;
43 static int __adp5520_read(struct i2c_client
*client
,
44 int reg
, uint8_t *val
)
48 ret
= i2c_smbus_read_byte_data(client
, reg
);
50 dev_err(&client
->dev
, "failed reading at 0x%02x\n", reg
);
58 static int __adp5520_write(struct i2c_client
*client
,
63 ret
= i2c_smbus_write_byte_data(client
, reg
, val
);
65 dev_err(&client
->dev
, "failed writing 0x%02x to 0x%02x\n",
72 static int __adp5520_ack_bits(struct i2c_client
*client
, int reg
,
75 struct adp5520_chip
*chip
= i2c_get_clientdata(client
);
79 mutex_lock(&chip
->lock
);
81 ret
= __adp5520_read(client
, reg
, ®_val
);
85 ret
= __adp5520_write(client
, reg
, reg_val
);
88 mutex_unlock(&chip
->lock
);
92 int adp5520_write(struct device
*dev
, int reg
, uint8_t val
)
94 return __adp5520_write(to_i2c_client(dev
), reg
, val
);
96 EXPORT_SYMBOL_GPL(adp5520_write
);
98 int adp5520_read(struct device
*dev
, int reg
, uint8_t *val
)
100 return __adp5520_read(to_i2c_client(dev
), reg
, val
);
102 EXPORT_SYMBOL_GPL(adp5520_read
);
104 int adp5520_set_bits(struct device
*dev
, int reg
, uint8_t bit_mask
)
106 struct adp5520_chip
*chip
= dev_get_drvdata(dev
);
110 mutex_lock(&chip
->lock
);
112 ret
= __adp5520_read(chip
->client
, reg
, ®_val
);
114 if (!ret
&& ((reg_val
& bit_mask
) != bit_mask
)) {
116 ret
= __adp5520_write(chip
->client
, reg
, reg_val
);
119 mutex_unlock(&chip
->lock
);
122 EXPORT_SYMBOL_GPL(adp5520_set_bits
);
124 int adp5520_clr_bits(struct device
*dev
, int reg
, uint8_t bit_mask
)
126 struct adp5520_chip
*chip
= dev_get_drvdata(dev
);
130 mutex_lock(&chip
->lock
);
132 ret
= __adp5520_read(chip
->client
, reg
, ®_val
);
134 if (!ret
&& (reg_val
& bit_mask
)) {
135 reg_val
&= ~bit_mask
;
136 ret
= __adp5520_write(chip
->client
, reg
, reg_val
);
139 mutex_unlock(&chip
->lock
);
142 EXPORT_SYMBOL_GPL(adp5520_clr_bits
);
144 int adp5520_register_notifier(struct device
*dev
, struct notifier_block
*nb
,
147 struct adp5520_chip
*chip
= dev_get_drvdata(dev
);
150 adp5520_set_bits(chip
->dev
, ADP5520_INTERRUPT_ENABLE
,
151 events
& (ADP5520_KP_IEN
| ADP5520_KR_IEN
|
152 ADP5520_OVP_IEN
| ADP5520_CMPR_IEN
));
154 return blocking_notifier_chain_register(&chip
->notifier_list
,
160 EXPORT_SYMBOL_GPL(adp5520_register_notifier
);
162 int adp5520_unregister_notifier(struct device
*dev
, struct notifier_block
*nb
,
165 struct adp5520_chip
*chip
= dev_get_drvdata(dev
);
167 adp5520_clr_bits(chip
->dev
, ADP5520_INTERRUPT_ENABLE
,
168 events
& (ADP5520_KP_IEN
| ADP5520_KR_IEN
|
169 ADP5520_OVP_IEN
| ADP5520_CMPR_IEN
));
171 return blocking_notifier_chain_unregister(&chip
->notifier_list
, nb
);
173 EXPORT_SYMBOL_GPL(adp5520_unregister_notifier
);
175 static irqreturn_t
adp5520_irq_thread(int irq
, void *data
)
177 struct adp5520_chip
*chip
= data
;
182 ret
= __adp5520_read(chip
->client
, ADP5520_MODE_STATUS
, ®_val
);
186 events
= reg_val
& (ADP5520_OVP_INT
| ADP5520_CMPR_INT
|
187 ADP5520_GPI_INT
| ADP5520_KR_INT
| ADP5520_KP_INT
);
189 blocking_notifier_call_chain(&chip
->notifier_list
, events
, NULL
);
190 /* ACK, Sticky bits are W1C */
191 __adp5520_ack_bits(chip
->client
, ADP5520_MODE_STATUS
, events
);
197 static int __remove_subdev(struct device
*dev
, void *unused
)
199 platform_device_unregister(to_platform_device(dev
));
203 static int adp5520_remove_subdevs(struct adp5520_chip
*chip
)
205 return device_for_each_child(chip
->dev
, NULL
, __remove_subdev
);
208 static int adp5520_probe(struct i2c_client
*client
,
209 const struct i2c_device_id
*id
)
211 struct adp5520_platform_data
*pdata
= dev_get_platdata(&client
->dev
);
212 struct platform_device
*pdev
;
213 struct adp5520_chip
*chip
;
216 if (!i2c_check_functionality(client
->adapter
,
217 I2C_FUNC_SMBUS_BYTE_DATA
)) {
218 dev_err(&client
->dev
, "SMBUS Word Data not Supported\n");
223 dev_err(&client
->dev
, "missing platform data\n");
227 chip
= devm_kzalloc(&client
->dev
, sizeof(*chip
), GFP_KERNEL
);
231 i2c_set_clientdata(client
, chip
);
232 chip
->client
= client
;
234 chip
->dev
= &client
->dev
;
235 chip
->irq
= client
->irq
;
236 chip
->id
= id
->driver_data
;
237 mutex_init(&chip
->lock
);
240 BLOCKING_INIT_NOTIFIER_HEAD(&chip
->notifier_list
);
242 ret
= request_threaded_irq(chip
->irq
, NULL
, adp5520_irq_thread
,
243 IRQF_TRIGGER_LOW
| IRQF_ONESHOT
,
246 dev_err(&client
->dev
, "failed to request irq %d\n",
252 ret
= adp5520_write(chip
->dev
, ADP5520_MODE_STATUS
, ADP5520_nSTNBY
);
254 dev_err(&client
->dev
, "failed to write\n");
259 pdev
= platform_device_register_data(chip
->dev
, "adp5520-keys",
260 chip
->id
, pdata
->keys
, sizeof(*pdata
->keys
));
263 goto out_remove_subdevs
;
268 pdev
= platform_device_register_data(chip
->dev
, "adp5520-gpio",
269 chip
->id
, pdata
->gpio
, sizeof(*pdata
->gpio
));
272 goto out_remove_subdevs
;
277 pdev
= platform_device_register_data(chip
->dev
, "adp5520-led",
278 chip
->id
, pdata
->leds
, sizeof(*pdata
->leds
));
281 goto out_remove_subdevs
;
285 if (pdata
->backlight
) {
286 pdev
= platform_device_register_data(chip
->dev
,
290 sizeof(*pdata
->backlight
));
293 goto out_remove_subdevs
;
300 adp5520_remove_subdevs(chip
);
304 free_irq(chip
->irq
, chip
);
309 #ifdef CONFIG_PM_SLEEP
310 static int adp5520_suspend(struct device
*dev
)
312 struct i2c_client
*client
= to_i2c_client(dev
);
313 struct adp5520_chip
*chip
= dev_get_drvdata(&client
->dev
);
315 adp5520_read(chip
->dev
, ADP5520_MODE_STATUS
, &chip
->mode
);
316 /* All other bits are W1C */
317 chip
->mode
&= ADP5520_BL_EN
| ADP5520_DIM_EN
| ADP5520_nSTNBY
;
318 adp5520_write(chip
->dev
, ADP5520_MODE_STATUS
, 0);
322 static int adp5520_resume(struct device
*dev
)
324 struct i2c_client
*client
= to_i2c_client(dev
);
325 struct adp5520_chip
*chip
= dev_get_drvdata(&client
->dev
);
327 adp5520_write(chip
->dev
, ADP5520_MODE_STATUS
, chip
->mode
);
332 static SIMPLE_DEV_PM_OPS(adp5520_pm
, adp5520_suspend
, adp5520_resume
);
334 static const struct i2c_device_id adp5520_id
[] = {
335 { "pmic-adp5520", ID_ADP5520
},
336 { "pmic-adp5501", ID_ADP5501
},
340 static struct i2c_driver adp5520_driver
= {
344 .suppress_bind_attrs
= true,
346 .probe
= adp5520_probe
,
347 .id_table
= adp5520_id
,
349 builtin_i2c_driver(adp5520_driver
);