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_read(struct device *dev, int reg, uint8_t *val)
156 return __da903x_read(to_i2c_client(dev), reg, val);
158 EXPORT_SYMBOL_GPL(da903x_read);
160 int da903x_set_bits(struct device *dev, int reg, uint8_t bit_mask)
162 struct da903x_chip *chip = dev_get_drvdata(dev);
166 mutex_lock(&chip->lock);
168 ret = __da903x_read(chip->client, reg, ®_val);
172 if ((reg_val & bit_mask) == 0) {
174 ret = __da903x_write(chip->client, reg, reg_val);
177 mutex_unlock(&chip->lock);
180 EXPORT_SYMBOL_GPL(da903x_set_bits);
182 int da903x_clr_bits(struct device *dev, int reg, uint8_t bit_mask)
184 struct da903x_chip *chip = dev_get_drvdata(dev);
188 mutex_lock(&chip->lock);
190 ret = __da903x_read(chip->client, reg, ®_val);
194 if (reg_val & bit_mask) {
195 reg_val &= ~bit_mask;
196 ret = __da903x_write(chip->client, reg, reg_val);
199 mutex_unlock(&chip->lock);
202 EXPORT_SYMBOL_GPL(da903x_clr_bits);
204 int da903x_update(struct device *dev, int reg, uint8_t val, uint8_t mask)
206 struct da903x_chip *chip = dev_get_drvdata(dev);
210 mutex_lock(&chip->lock);
212 ret = __da903x_read(chip->client, reg, ®_val);
216 if ((reg_val & mask) != val) {
217 reg_val = (reg_val & ~mask) | val;
218 ret = __da903x_write(chip->client, reg, reg_val);
221 mutex_unlock(&chip->lock);
224 EXPORT_SYMBOL_GPL(da903x_update);
226 int da903x_query_status(struct device *dev, unsigned int sbits)
228 struct da903x_chip *chip = dev_get_drvdata(dev);
229 unsigned int status = 0;
231 chip->ops->read_status(chip, &status);
232 return ((status & sbits) == sbits);
234 EXPORT_SYMBOL(da903x_query_status);
236 static int __devinit da9030_init_chip(struct da903x_chip *chip)
241 err = __da903x_read(chip->client, DA9030_CHIP_ID, &chip_id);
245 err = __da903x_write(chip->client, DA9030_SYS_CTRL_A, 0xE8);
249 dev_info(chip->dev, "DA9030 (CHIP ID: 0x%02x) detected\n", chip_id);
253 static int da9030_unmask_events(struct da903x_chip *chip, unsigned int events)
257 chip->events_mask &= ~events;
259 v[0] = (chip->events_mask & 0xff);
260 v[1] = (chip->events_mask >> 8) & 0xff;
261 v[2] = (chip->events_mask >> 16) & 0xff;
263 return __da903x_writes(chip->client, DA9030_IRQ_MASK_A, 3, v);
266 static int da9030_mask_events(struct da903x_chip *chip, unsigned int events)
270 chip->events_mask |= events;
272 v[0] = (chip->events_mask & 0xff);
273 v[1] = (chip->events_mask >> 8) & 0xff;
274 v[2] = (chip->events_mask >> 16) & 0xff;
276 return __da903x_writes(chip->client, DA9030_IRQ_MASK_A, 3, v);
279 static int da9030_read_events(struct da903x_chip *chip, unsigned int *events)
281 uint8_t v[3] = {0, 0, 0};
284 ret = __da903x_reads(chip->client, DA9030_EVENT_A, 3, v);
288 *events = (v[2] << 16) | (v[1] << 8) | v[0];
292 static int da9030_read_status(struct da903x_chip *chip, unsigned int *status)
294 return __da903x_read(chip->client, DA9030_STATUS, (uint8_t *)status);
297 static int da9034_init_chip(struct da903x_chip *chip)
302 err = __da903x_read(chip->client, DA9034_CHIP_ID, &chip_id);
306 err = __da903x_write(chip->client, DA9034_SYS_CTRL_A, 0xE8);
310 /* avoid SRAM power off during sleep*/
311 __da903x_write(chip->client, 0x10, 0x07);
312 __da903x_write(chip->client, 0x11, 0xff);
313 __da903x_write(chip->client, 0x12, 0xff);
315 /* Enable the ONKEY power down functionality */
316 __da903x_write(chip->client, DA9034_SYS_CTRL_B, 0x20);
317 __da903x_write(chip->client, DA9034_SYS_CTRL_A, 0x60);
319 /* workaround to make LEDs work */
320 __da903x_write(chip->client, 0x90, 0x01);
321 __da903x_write(chip->client, 0xB0, 0x08);
323 /* make ADTV1 and SDTV1 effective */
324 __da903x_write(chip->client, 0x20, 0x00);
326 dev_info(chip->dev, "DA9034 (CHIP ID: 0x%02x) detected\n", chip_id);
330 static int da9034_unmask_events(struct da903x_chip *chip, unsigned int events)
334 chip->events_mask &= ~events;
336 v[0] = (chip->events_mask & 0xff);
337 v[1] = (chip->events_mask >> 8) & 0xff;
338 v[2] = (chip->events_mask >> 16) & 0xff;
339 v[3] = (chip->events_mask >> 24) & 0xff;
341 return __da903x_writes(chip->client, DA9034_IRQ_MASK_A, 4, v);
344 static int da9034_mask_events(struct da903x_chip *chip, unsigned int events)
348 chip->events_mask |= events;
350 v[0] = (chip->events_mask & 0xff);
351 v[1] = (chip->events_mask >> 8) & 0xff;
352 v[2] = (chip->events_mask >> 16) & 0xff;
353 v[3] = (chip->events_mask >> 24) & 0xff;
355 return __da903x_writes(chip->client, DA9034_IRQ_MASK_A, 4, v);
358 static int da9034_read_events(struct da903x_chip *chip, unsigned int *events)
360 uint8_t v[4] = {0, 0, 0, 0};
363 ret = __da903x_reads(chip->client, DA9034_EVENT_A, 4, v);
367 *events = (v[3] << 24) | (v[2] << 16) | (v[1] << 8) | v[0];
371 static int da9034_read_status(struct da903x_chip *chip, unsigned int *status)
373 uint8_t v[2] = {0, 0};
376 ret = __da903x_reads(chip->client, DA9034_STATUS_A, 2, v);
380 *status = (v[1] << 8) | v[0];
384 static void da903x_irq_work(struct work_struct *work)
386 struct da903x_chip *chip =
387 container_of(work, struct da903x_chip, irq_work);
388 unsigned int events = 0;
391 if (chip->ops->read_events(chip, &events))
394 events &= ~chip->events_mask;
398 blocking_notifier_call_chain(
399 &chip->notifier_list, events, NULL);
401 enable_irq(chip->client->irq);
404 static int da903x_irq_handler(int irq, void *data)
406 struct da903x_chip *chip = data;
408 disable_irq_nosync(irq);
409 (void)schedule_work(&chip->irq_work);
414 static struct da903x_chip_ops da903x_ops[] = {
416 .init_chip = da9030_init_chip,
417 .unmask_events = da9030_unmask_events,
418 .mask_events = da9030_mask_events,
419 .read_events = da9030_read_events,
420 .read_status = da9030_read_status,
423 .init_chip = da9034_init_chip,
424 .unmask_events = da9034_unmask_events,
425 .mask_events = da9034_mask_events,
426 .read_events = da9034_read_events,
427 .read_status = da9034_read_status,
431 static const struct i2c_device_id da903x_id_table[] = {
436 MODULE_DEVICE_TABLE(i2c, da903x_id_table);
438 static int __devexit __remove_subdev(struct device *dev, void *unused)
440 platform_device_unregister(to_platform_device(dev));
444 static int __devexit da903x_remove_subdevs(struct da903x_chip *chip)
446 return device_for_each_child(chip->dev, NULL, __remove_subdev);
449 static int __devinit da903x_add_subdevs(struct da903x_chip *chip,
450 struct da903x_platform_data *pdata)
452 struct da903x_subdev_info *subdev;
453 struct platform_device *pdev;
456 for (i = 0; i < pdata->num_subdevs; i++) {
457 subdev = &pdata->subdevs[i];
459 pdev = platform_device_alloc(subdev->name, subdev->id);
461 pdev->dev.parent = chip->dev;
462 pdev->dev.platform_data = subdev->platform_data;
464 ret = platform_device_add(pdev);
471 da903x_remove_subdevs(chip);
475 static int __devinit da903x_probe(struct i2c_client *client,
476 const struct i2c_device_id *id)
478 struct da903x_platform_data *pdata = client->dev.platform_data;
479 struct da903x_chip *chip;
483 chip = kzalloc(sizeof(struct da903x_chip), GFP_KERNEL);
487 chip->client = client;
488 chip->dev = &client->dev;
489 chip->ops = &da903x_ops[id->driver_data];
491 mutex_init(&chip->lock);
492 INIT_WORK(&chip->irq_work, da903x_irq_work);
493 BLOCKING_INIT_NOTIFIER_HEAD(&chip->notifier_list);
495 i2c_set_clientdata(client, chip);
497 ret = chip->ops->init_chip(chip);
501 /* mask and clear all IRQs */
502 chip->events_mask = 0xffffffff;
503 chip->ops->mask_events(chip, chip->events_mask);
504 chip->ops->read_events(chip, &tmp);
506 ret = request_irq(client->irq, da903x_irq_handler,
507 IRQF_DISABLED | IRQF_TRIGGER_FALLING,
510 dev_err(&client->dev, "failed to request irq %d\n",
515 ret = da903x_add_subdevs(chip, pdata);
522 free_irq(client->irq, chip);
524 i2c_set_clientdata(client, NULL);
529 static int __devexit da903x_remove(struct i2c_client *client)
531 struct da903x_chip *chip = i2c_get_clientdata(client);
533 da903x_remove_subdevs(chip);
538 static struct i2c_driver da903x_driver = {
541 .owner = THIS_MODULE,
543 .probe = da903x_probe,
544 .remove = __devexit_p(da903x_remove),
545 .id_table = da903x_id_table,
548 static int __init da903x_init(void)
550 return i2c_add_driver(&da903x_driver);
552 module_init(da903x_init);
554 static void __exit da903x_exit(void)
556 i2c_del_driver(&da903x_driver);
558 module_exit(da903x_exit);
560 MODULE_DESCRIPTION("PMIC Driver for Dialog Semiconductor DA9034");
561 MODULE_AUTHOR("Eric Miao <eric.miao@marvell.com>"
562 "Mike Rapoport <mike@compulab.co.il>");
563 MODULE_LICENSE("GPL");