Input: add driver for EETI touchpanels
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / mfd / da903x.c
blob7283d88656af06e6003b3f49b84f6ac8022afa48
1 /*
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
49 struct da903x_chip;
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);
59 struct da903x_chip {
60 struct i2c_client *client;
61 struct device *dev;
62 struct da903x_chip_ops *ops;
64 int type;
65 uint32_t events_mask;
67 struct mutex lock;
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)
76 int ret;
78 ret = i2c_smbus_read_byte_data(client, reg);
79 if (ret < 0) {
80 dev_err(&client->dev, "failed reading at 0x%02x\n", reg);
81 return ret;
84 *val = (uint8_t)ret;
85 return 0;
88 static inline int __da903x_reads(struct i2c_client *client, int reg,
89 int len, uint8_t *val)
91 int ret;
93 ret = i2c_smbus_read_i2c_block_data(client, reg, len, val);
94 if (ret < 0) {
95 dev_err(&client->dev, "failed reading from 0x%02x\n", reg);
96 return ret;
98 return 0;
101 static inline int __da903x_write(struct i2c_client *client,
102 int reg, uint8_t val)
104 int ret;
106 ret = i2c_smbus_write_byte_data(client, reg, val);
107 if (ret < 0) {
108 dev_err(&client->dev, "failed writing 0x%02x to 0x%02x\n",
109 val, reg);
110 return ret;
112 return 0;
115 static inline int __da903x_writes(struct i2c_client *client, int reg,
116 int len, uint8_t *val)
118 int ret;
120 ret = i2c_smbus_write_i2c_block_data(client, reg, len, val);
121 if (ret < 0) {
122 dev_err(&client->dev, "failed writings to 0x%02x\n", reg);
123 return ret;
125 return 0;
128 int da903x_register_notifier(struct device *dev, struct notifier_block *nb,
129 unsigned int events)
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,
139 unsigned int events)
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);
175 uint8_t reg_val;
176 int ret = 0;
178 mutex_lock(&chip->lock);
180 ret = __da903x_read(chip->client, reg, &reg_val);
181 if (ret)
182 goto out;
184 if ((reg_val & bit_mask) == 0) {
185 reg_val |= bit_mask;
186 ret = __da903x_write(chip->client, reg, reg_val);
188 out:
189 mutex_unlock(&chip->lock);
190 return ret;
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);
197 uint8_t reg_val;
198 int ret = 0;
200 mutex_lock(&chip->lock);
202 ret = __da903x_read(chip->client, reg, &reg_val);
203 if (ret)
204 goto out;
206 if (reg_val & bit_mask) {
207 reg_val &= ~bit_mask;
208 ret = __da903x_write(chip->client, reg, reg_val);
210 out:
211 mutex_unlock(&chip->lock);
212 return ret;
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);
219 uint8_t reg_val;
220 int ret = 0;
222 mutex_lock(&chip->lock);
224 ret = __da903x_read(chip->client, reg, &reg_val);
225 if (ret)
226 goto out;
228 if ((reg_val & mask) != val) {
229 reg_val = (reg_val & ~mask) | val;
230 ret = __da903x_write(chip->client, reg, reg_val);
232 out:
233 mutex_unlock(&chip->lock);
234 return ret;
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)
250 uint8_t chip_id;
251 int err;
253 err = __da903x_read(chip->client, DA9030_CHIP_ID, &chip_id);
254 if (err)
255 return err;
257 err = __da903x_write(chip->client, DA9030_SYS_CTRL_A, 0xE8);
258 if (err)
259 return err;
261 dev_info(chip->dev, "DA9030 (CHIP ID: 0x%02x) detected\n", chip_id);
262 return 0;
265 static int da9030_unmask_events(struct da903x_chip *chip, unsigned int events)
267 uint8_t v[3];
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)
280 uint8_t v[3];
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};
294 int ret;
296 ret = __da903x_reads(chip->client, DA9030_EVENT_A, 3, v);
297 if (ret < 0)
298 return ret;
300 *events = (v[2] << 16) | (v[1] << 8) | v[0];
301 return 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)
311 uint8_t chip_id;
312 int err;
314 err = __da903x_read(chip->client, DA9034_CHIP_ID, &chip_id);
315 if (err)
316 return err;
318 err = __da903x_write(chip->client, DA9034_SYS_CTRL_A, 0xE8);
319 if (err)
320 return err;
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);
339 return 0;
342 static int da9034_unmask_events(struct da903x_chip *chip, unsigned int events)
344 uint8_t v[4];
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)
358 uint8_t v[4];
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};
373 int ret;
375 ret = __da903x_reads(chip->client, DA9034_EVENT_A, 4, v);
376 if (ret < 0)
377 return ret;
379 *events = (v[3] << 24) | (v[2] << 16) | (v[1] << 8) | v[0];
380 return 0;
383 static int da9034_read_status(struct da903x_chip *chip, unsigned int *status)
385 uint8_t v[2] = {0, 0};
386 int ret = 0;
388 ret = __da903x_reads(chip->client, DA9034_STATUS_A, 2, v);
389 if (ret)
390 return ret;
392 *status = (v[1] << 8) | v[0];
393 return 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;
402 while (1) {
403 if (chip->ops->read_events(chip, &events))
404 break;
406 events &= ~chip->events_mask;
407 if (events == 0)
408 break;
410 blocking_notifier_call_chain(
411 &chip->notifier_list, events, NULL);
413 enable_irq(chip->client->irq);
416 static irqreturn_t 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);
423 return IRQ_HANDLED;
426 static struct da903x_chip_ops da903x_ops[] = {
427 [0] = {
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,
434 [1] = {
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[] = {
444 { "da9030", 0 },
445 { "da9034", 1 },
446 { },
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));
453 return 0;
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;
466 int i, ret = 0;
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);
477 if (ret)
478 goto failed;
480 return 0;
482 failed:
483 da903x_remove_subdevs(chip);
484 return ret;
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;
492 unsigned int tmp;
493 int ret;
495 chip = kzalloc(sizeof(struct da903x_chip), GFP_KERNEL);
496 if (chip == NULL)
497 return -ENOMEM;
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);
510 if (ret)
511 goto out_free_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,
520 "da903x", chip);
521 if (ret) {
522 dev_err(&client->dev, "failed to request irq %d\n",
523 client->irq);
524 goto out_free_chip;
527 ret = da903x_add_subdevs(chip, pdata);
528 if (ret)
529 goto out_free_irq;
531 return 0;
533 out_free_irq:
534 free_irq(client->irq, chip);
535 out_free_chip:
536 i2c_set_clientdata(client, NULL);
537 kfree(chip);
538 return ret;
541 static int __devexit da903x_remove(struct i2c_client *client)
543 struct da903x_chip *chip = i2c_get_clientdata(client);
545 da903x_remove_subdevs(chip);
546 kfree(chip);
547 return 0;
550 static struct i2c_driver da903x_driver = {
551 .driver = {
552 .name = "da903x",
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");