2 * Base driver for Dialog Semiconductor DA9030/DA9034
4 * Copyright (C) 2008 Compulab, Ltd.
5 * Mike Rapoport <mike@compulab.co.il>
7 * Copyright (C) 2006-2008 Marvell International Ltd.
8 * Eric Miao <eric.miao@marvell.com>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
15 #include <linux/kernel.h>
16 #include <linux/module.h>
17 #include <linux/interrupt.h>
18 #include <linux/platform_device.h>
19 #include <linux/i2c.h>
20 #include <linux/mfd/da903x.h>
22 #define DA9030_CHIP_ID 0x00
23 #define DA9030_EVENT_A 0x01
24 #define DA9030_EVENT_B 0x02
25 #define DA9030_EVENT_C 0x03
26 #define DA9030_STATUS 0x04
27 #define DA9030_IRQ_MASK_A 0x05
28 #define DA9030_IRQ_MASK_B 0x06
29 #define DA9030_IRQ_MASK_C 0x07
30 #define DA9030_SYS_CTRL_A 0x08
31 #define DA9030_SYS_CTRL_B 0x09
32 #define DA9030_FAULT_LOG 0x0a
34 #define DA9034_CHIP_ID 0x00
35 #define DA9034_EVENT_A 0x01
36 #define DA9034_EVENT_B 0x02
37 #define DA9034_EVENT_C 0x03
38 #define DA9034_EVENT_D 0x04
39 #define DA9034_STATUS_A 0x05
40 #define DA9034_STATUS_B 0x06
41 #define DA9034_IRQ_MASK_A 0x07
42 #define DA9034_IRQ_MASK_B 0x08
43 #define DA9034_IRQ_MASK_C 0x09
44 #define DA9034_IRQ_MASK_D 0x0a
45 #define DA9034_SYS_CTRL_A 0x0b
46 #define DA9034_SYS_CTRL_B 0x0c
47 #define DA9034_FAULT_LOG 0x0d
51 struct da903x_chip_ops
{
52 int (*init_chip
)(struct da903x_chip
*);
53 int (*unmask_events
)(struct da903x_chip
*, unsigned int events
);
54 int (*mask_events
)(struct da903x_chip
*, unsigned int events
);
55 int (*read_events
)(struct da903x_chip
*, unsigned int *events
);
56 int (*read_status
)(struct da903x_chip
*, unsigned int *status
);
60 struct i2c_client
*client
;
62 struct da903x_chip_ops
*ops
;
68 struct work_struct irq_work
;
70 struct blocking_notifier_head notifier_list
;
73 static inline int __da903x_read(struct i2c_client
*client
,
74 int reg
, uint8_t *val
)
78 ret
= i2c_smbus_read_byte_data(client
, reg
);
80 dev_err(&client
->dev
, "failed reading at 0x%02x\n", reg
);
88 static inline int __da903x_reads(struct i2c_client
*client
, int reg
,
89 int len
, uint8_t *val
)
93 ret
= i2c_smbus_read_i2c_block_data(client
, reg
, len
, val
);
95 dev_err(&client
->dev
, "failed reading from 0x%02x\n", reg
);
101 static inline int __da903x_write(struct i2c_client
*client
,
102 int reg
, uint8_t val
)
106 ret
= i2c_smbus_write_byte_data(client
, reg
, val
);
108 dev_err(&client
->dev
, "failed writing 0x%02x to 0x%02x\n",
115 static inline int __da903x_writes(struct i2c_client
*client
, int reg
,
116 int len
, uint8_t *val
)
120 ret
= i2c_smbus_write_i2c_block_data(client
, reg
, len
, val
);
122 dev_err(&client
->dev
, "failed writings to 0x%02x\n", reg
);
128 int da903x_register_notifier(struct device
*dev
, struct notifier_block
*nb
,
131 struct da903x_chip
*chip
= dev_get_drvdata(dev
);
133 chip
->ops
->unmask_events(chip
, events
);
134 return blocking_notifier_chain_register(&chip
->notifier_list
, nb
);
136 EXPORT_SYMBOL_GPL(da903x_register_notifier
);
138 int da903x_unregister_notifier(struct device
*dev
, struct notifier_block
*nb
,
141 struct da903x_chip
*chip
= dev_get_drvdata(dev
);
143 chip
->ops
->mask_events(chip
, events
);
144 return blocking_notifier_chain_unregister(&chip
->notifier_list
, nb
);
146 EXPORT_SYMBOL_GPL(da903x_unregister_notifier
);
148 int da903x_write(struct device
*dev
, int reg
, uint8_t val
)
150 return __da903x_write(to_i2c_client(dev
), reg
, val
);
152 EXPORT_SYMBOL_GPL(da903x_write
);
154 int da903x_writes(struct device
*dev
, int reg
, int len
, uint8_t *val
)
156 return __da903x_writes(to_i2c_client(dev
), reg
, len
, val
);
158 EXPORT_SYMBOL_GPL(da903x_writes
);
160 int da903x_read(struct device
*dev
, int reg
, uint8_t *val
)
162 return __da903x_read(to_i2c_client(dev
), reg
, val
);
164 EXPORT_SYMBOL_GPL(da903x_read
);
166 int da903x_reads(struct device
*dev
, int reg
, int len
, uint8_t *val
)
168 return __da903x_reads(to_i2c_client(dev
), reg
, len
, val
);
170 EXPORT_SYMBOL_GPL(da903x_reads
);
172 int da903x_set_bits(struct device
*dev
, int reg
, uint8_t bit_mask
)
174 struct da903x_chip
*chip
= dev_get_drvdata(dev
);
178 mutex_lock(&chip
->lock
);
180 ret
= __da903x_read(chip
->client
, reg
, ®_val
);
184 if ((reg_val
& bit_mask
) == 0) {
186 ret
= __da903x_write(chip
->client
, reg
, reg_val
);
189 mutex_unlock(&chip
->lock
);
192 EXPORT_SYMBOL_GPL(da903x_set_bits
);
194 int da903x_clr_bits(struct device
*dev
, int reg
, uint8_t bit_mask
)
196 struct da903x_chip
*chip
= dev_get_drvdata(dev
);
200 mutex_lock(&chip
->lock
);
202 ret
= __da903x_read(chip
->client
, reg
, ®_val
);
206 if (reg_val
& bit_mask
) {
207 reg_val
&= ~bit_mask
;
208 ret
= __da903x_write(chip
->client
, reg
, reg_val
);
211 mutex_unlock(&chip
->lock
);
214 EXPORT_SYMBOL_GPL(da903x_clr_bits
);
216 int da903x_update(struct device
*dev
, int reg
, uint8_t val
, uint8_t mask
)
218 struct da903x_chip
*chip
= dev_get_drvdata(dev
);
222 mutex_lock(&chip
->lock
);
224 ret
= __da903x_read(chip
->client
, reg
, ®_val
);
228 if ((reg_val
& mask
) != val
) {
229 reg_val
= (reg_val
& ~mask
) | val
;
230 ret
= __da903x_write(chip
->client
, reg
, reg_val
);
233 mutex_unlock(&chip
->lock
);
236 EXPORT_SYMBOL_GPL(da903x_update
);
238 int da903x_query_status(struct device
*dev
, unsigned int sbits
)
240 struct da903x_chip
*chip
= dev_get_drvdata(dev
);
241 unsigned int status
= 0;
243 chip
->ops
->read_status(chip
, &status
);
244 return ((status
& sbits
) == sbits
);
246 EXPORT_SYMBOL(da903x_query_status
);
248 static int __devinit
da9030_init_chip(struct da903x_chip
*chip
)
253 err
= __da903x_read(chip
->client
, DA9030_CHIP_ID
, &chip_id
);
257 err
= __da903x_write(chip
->client
, DA9030_SYS_CTRL_A
, 0xE8);
261 dev_info(chip
->dev
, "DA9030 (CHIP ID: 0x%02x) detected\n", chip_id
);
265 static int da9030_unmask_events(struct da903x_chip
*chip
, unsigned int events
)
269 chip
->events_mask
&= ~events
;
271 v
[0] = (chip
->events_mask
& 0xff);
272 v
[1] = (chip
->events_mask
>> 8) & 0xff;
273 v
[2] = (chip
->events_mask
>> 16) & 0xff;
275 return __da903x_writes(chip
->client
, DA9030_IRQ_MASK_A
, 3, v
);
278 static int da9030_mask_events(struct da903x_chip
*chip
, unsigned int events
)
282 chip
->events_mask
|= events
;
284 v
[0] = (chip
->events_mask
& 0xff);
285 v
[1] = (chip
->events_mask
>> 8) & 0xff;
286 v
[2] = (chip
->events_mask
>> 16) & 0xff;
288 return __da903x_writes(chip
->client
, DA9030_IRQ_MASK_A
, 3, v
);
291 static int da9030_read_events(struct da903x_chip
*chip
, unsigned int *events
)
293 uint8_t v
[3] = {0, 0, 0};
296 ret
= __da903x_reads(chip
->client
, DA9030_EVENT_A
, 3, v
);
300 *events
= (v
[2] << 16) | (v
[1] << 8) | v
[0];
304 static int da9030_read_status(struct da903x_chip
*chip
, unsigned int *status
)
306 return __da903x_read(chip
->client
, DA9030_STATUS
, (uint8_t *)status
);
309 static int da9034_init_chip(struct da903x_chip
*chip
)
314 err
= __da903x_read(chip
->client
, DA9034_CHIP_ID
, &chip_id
);
318 err
= __da903x_write(chip
->client
, DA9034_SYS_CTRL_A
, 0xE8);
322 /* avoid SRAM power off during sleep*/
323 __da903x_write(chip
->client
, 0x10, 0x07);
324 __da903x_write(chip
->client
, 0x11, 0xff);
325 __da903x_write(chip
->client
, 0x12, 0xff);
327 /* Enable the ONKEY power down functionality */
328 __da903x_write(chip
->client
, DA9034_SYS_CTRL_B
, 0x20);
329 __da903x_write(chip
->client
, DA9034_SYS_CTRL_A
, 0x60);
331 /* workaround to make LEDs work */
332 __da903x_write(chip
->client
, 0x90, 0x01);
333 __da903x_write(chip
->client
, 0xB0, 0x08);
335 /* make ADTV1 and SDTV1 effective */
336 __da903x_write(chip
->client
, 0x20, 0x00);
338 dev_info(chip
->dev
, "DA9034 (CHIP ID: 0x%02x) detected\n", chip_id
);
342 static int da9034_unmask_events(struct da903x_chip
*chip
, unsigned int events
)
346 chip
->events_mask
&= ~events
;
348 v
[0] = (chip
->events_mask
& 0xff);
349 v
[1] = (chip
->events_mask
>> 8) & 0xff;
350 v
[2] = (chip
->events_mask
>> 16) & 0xff;
351 v
[3] = (chip
->events_mask
>> 24) & 0xff;
353 return __da903x_writes(chip
->client
, DA9034_IRQ_MASK_A
, 4, v
);
356 static int da9034_mask_events(struct da903x_chip
*chip
, unsigned int events
)
360 chip
->events_mask
|= events
;
362 v
[0] = (chip
->events_mask
& 0xff);
363 v
[1] = (chip
->events_mask
>> 8) & 0xff;
364 v
[2] = (chip
->events_mask
>> 16) & 0xff;
365 v
[3] = (chip
->events_mask
>> 24) & 0xff;
367 return __da903x_writes(chip
->client
, DA9034_IRQ_MASK_A
, 4, v
);
370 static int da9034_read_events(struct da903x_chip
*chip
, unsigned int *events
)
372 uint8_t v
[4] = {0, 0, 0, 0};
375 ret
= __da903x_reads(chip
->client
, DA9034_EVENT_A
, 4, v
);
379 *events
= (v
[3] << 24) | (v
[2] << 16) | (v
[1] << 8) | v
[0];
383 static int da9034_read_status(struct da903x_chip
*chip
, unsigned int *status
)
385 uint8_t v
[2] = {0, 0};
388 ret
= __da903x_reads(chip
->client
, DA9034_STATUS_A
, 2, v
);
392 *status
= (v
[1] << 8) | v
[0];
396 static void da903x_irq_work(struct work_struct
*work
)
398 struct da903x_chip
*chip
=
399 container_of(work
, struct da903x_chip
, irq_work
);
400 unsigned int events
= 0;
403 if (chip
->ops
->read_events(chip
, &events
))
406 events
&= ~chip
->events_mask
;
410 blocking_notifier_call_chain(
411 &chip
->notifier_list
, events
, NULL
);
413 enable_irq(chip
->client
->irq
);
416 static int da903x_irq_handler(int irq
, void *data
)
418 struct da903x_chip
*chip
= data
;
420 disable_irq_nosync(irq
);
421 (void)schedule_work(&chip
->irq_work
);
426 static struct da903x_chip_ops da903x_ops
[] = {
428 .init_chip
= da9030_init_chip
,
429 .unmask_events
= da9030_unmask_events
,
430 .mask_events
= da9030_mask_events
,
431 .read_events
= da9030_read_events
,
432 .read_status
= da9030_read_status
,
435 .init_chip
= da9034_init_chip
,
436 .unmask_events
= da9034_unmask_events
,
437 .mask_events
= da9034_mask_events
,
438 .read_events
= da9034_read_events
,
439 .read_status
= da9034_read_status
,
443 static const struct i2c_device_id da903x_id_table
[] = {
448 MODULE_DEVICE_TABLE(i2c
, da903x_id_table
);
450 static int __remove_subdev(struct device
*dev
, void *unused
)
452 platform_device_unregister(to_platform_device(dev
));
456 static int da903x_remove_subdevs(struct da903x_chip
*chip
)
458 return device_for_each_child(chip
->dev
, NULL
, __remove_subdev
);
461 static int __devinit
da903x_add_subdevs(struct da903x_chip
*chip
,
462 struct da903x_platform_data
*pdata
)
464 struct da903x_subdev_info
*subdev
;
465 struct platform_device
*pdev
;
468 for (i
= 0; i
< pdata
->num_subdevs
; i
++) {
469 subdev
= &pdata
->subdevs
[i
];
471 pdev
= platform_device_alloc(subdev
->name
, subdev
->id
);
473 pdev
->dev
.parent
= chip
->dev
;
474 pdev
->dev
.platform_data
= subdev
->platform_data
;
476 ret
= platform_device_add(pdev
);
483 da903x_remove_subdevs(chip
);
487 static int __devinit
da903x_probe(struct i2c_client
*client
,
488 const struct i2c_device_id
*id
)
490 struct da903x_platform_data
*pdata
= client
->dev
.platform_data
;
491 struct da903x_chip
*chip
;
495 chip
= kzalloc(sizeof(struct da903x_chip
), GFP_KERNEL
);
499 chip
->client
= client
;
500 chip
->dev
= &client
->dev
;
501 chip
->ops
= &da903x_ops
[id
->driver_data
];
503 mutex_init(&chip
->lock
);
504 INIT_WORK(&chip
->irq_work
, da903x_irq_work
);
505 BLOCKING_INIT_NOTIFIER_HEAD(&chip
->notifier_list
);
507 i2c_set_clientdata(client
, chip
);
509 ret
= chip
->ops
->init_chip(chip
);
513 /* mask and clear all IRQs */
514 chip
->events_mask
= 0xffffffff;
515 chip
->ops
->mask_events(chip
, chip
->events_mask
);
516 chip
->ops
->read_events(chip
, &tmp
);
518 ret
= request_irq(client
->irq
, da903x_irq_handler
,
519 IRQF_DISABLED
| IRQF_TRIGGER_FALLING
,
522 dev_err(&client
->dev
, "failed to request irq %d\n",
527 ret
= da903x_add_subdevs(chip
, pdata
);
534 free_irq(client
->irq
, chip
);
536 i2c_set_clientdata(client
, NULL
);
541 static int __devexit
da903x_remove(struct i2c_client
*client
)
543 struct da903x_chip
*chip
= i2c_get_clientdata(client
);
545 da903x_remove_subdevs(chip
);
550 static struct i2c_driver da903x_driver
= {
553 .owner
= THIS_MODULE
,
555 .probe
= da903x_probe
,
556 .remove
= __devexit_p(da903x_remove
),
557 .id_table
= da903x_id_table
,
560 static int __init
da903x_init(void)
562 return i2c_add_driver(&da903x_driver
);
564 module_init(da903x_init
);
566 static void __exit
da903x_exit(void)
568 i2c_del_driver(&da903x_driver
);
570 module_exit(da903x_exit
);
572 MODULE_DESCRIPTION("PMIC Driver for Dialog Semiconductor DA9034");
573 MODULE_AUTHOR("Eric Miao <eric.miao@marvell.com>"
574 "Mike Rapoport <mike@compulab.co.il>");
575 MODULE_LICENSE("GPL");