Phonet: factor common code to send control messages
[linux/fpc-iii.git] / drivers / mfd / tps6586x.c
blobe9018d1394ee7d8a6044c1c252a2f6d38f8470c3
1 /*
2 * Core driver for TI TPS6586x PMIC family
4 * Copyright (c) 2010 CompuLab Ltd.
5 * Mike Rapoport <mike@compulab.co.il>
7 * Based on da903x.c.
8 * Copyright (C) 2008 Compulab, Ltd.
9 * Mike Rapoport <mike@compulab.co.il>
10 * Copyright (C) 2006-2008 Marvell International Ltd.
11 * Eric Miao <eric.miao@marvell.com>
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License version 2 as
15 * published by the Free Software Foundation.
18 #include <linux/interrupt.h>
19 #include <linux/irq.h>
20 #include <linux/kernel.h>
21 #include <linux/module.h>
22 #include <linux/mutex.h>
23 #include <linux/slab.h>
24 #include <linux/gpio.h>
25 #include <linux/i2c.h>
27 #include <linux/mfd/core.h>
28 #include <linux/mfd/tps6586x.h>
30 /* GPIO control registers */
31 #define TPS6586X_GPIOSET1 0x5d
32 #define TPS6586X_GPIOSET2 0x5e
34 /* interrupt control registers */
35 #define TPS6586X_INT_ACK1 0xb5
36 #define TPS6586X_INT_ACK2 0xb6
37 #define TPS6586X_INT_ACK3 0xb7
38 #define TPS6586X_INT_ACK4 0xb8
40 /* interrupt mask registers */
41 #define TPS6586X_INT_MASK1 0xb0
42 #define TPS6586X_INT_MASK2 0xb1
43 #define TPS6586X_INT_MASK3 0xb2
44 #define TPS6586X_INT_MASK4 0xb3
45 #define TPS6586X_INT_MASK5 0xb4
47 /* device id */
48 #define TPS6586X_VERSIONCRC 0xcd
50 struct tps6586x_irq_data {
51 u8 mask_reg;
52 u8 mask_mask;
55 #define TPS6586X_IRQ(_reg, _mask) \
56 { \
57 .mask_reg = (_reg) - TPS6586X_INT_MASK1, \
58 .mask_mask = (_mask), \
61 static const struct tps6586x_irq_data tps6586x_irqs[] = {
62 [TPS6586X_INT_PLDO_0] = TPS6586X_IRQ(TPS6586X_INT_MASK1, 1 << 0),
63 [TPS6586X_INT_PLDO_1] = TPS6586X_IRQ(TPS6586X_INT_MASK1, 1 << 1),
64 [TPS6586X_INT_PLDO_2] = TPS6586X_IRQ(TPS6586X_INT_MASK1, 1 << 2),
65 [TPS6586X_INT_PLDO_3] = TPS6586X_IRQ(TPS6586X_INT_MASK1, 1 << 3),
66 [TPS6586X_INT_PLDO_4] = TPS6586X_IRQ(TPS6586X_INT_MASK1, 1 << 4),
67 [TPS6586X_INT_PLDO_5] = TPS6586X_IRQ(TPS6586X_INT_MASK1, 1 << 5),
68 [TPS6586X_INT_PLDO_6] = TPS6586X_IRQ(TPS6586X_INT_MASK1, 1 << 6),
69 [TPS6586X_INT_PLDO_7] = TPS6586X_IRQ(TPS6586X_INT_MASK1, 1 << 7),
70 [TPS6586X_INT_COMP_DET] = TPS6586X_IRQ(TPS6586X_INT_MASK4, 1 << 0),
71 [TPS6586X_INT_ADC] = TPS6586X_IRQ(TPS6586X_INT_MASK2, 1 << 1),
72 [TPS6586X_INT_PLDO_8] = TPS6586X_IRQ(TPS6586X_INT_MASK2, 1 << 2),
73 [TPS6586X_INT_PLDO_9] = TPS6586X_IRQ(TPS6586X_INT_MASK2, 1 << 3),
74 [TPS6586X_INT_PSM_0] = TPS6586X_IRQ(TPS6586X_INT_MASK2, 1 << 4),
75 [TPS6586X_INT_PSM_1] = TPS6586X_IRQ(TPS6586X_INT_MASK2, 1 << 5),
76 [TPS6586X_INT_PSM_2] = TPS6586X_IRQ(TPS6586X_INT_MASK2, 1 << 6),
77 [TPS6586X_INT_PSM_3] = TPS6586X_IRQ(TPS6586X_INT_MASK2, 1 << 7),
78 [TPS6586X_INT_RTC_ALM1] = TPS6586X_IRQ(TPS6586X_INT_MASK5, 1 << 4),
79 [TPS6586X_INT_ACUSB_OVP] = TPS6586X_IRQ(TPS6586X_INT_MASK5, 0x03),
80 [TPS6586X_INT_USB_DET] = TPS6586X_IRQ(TPS6586X_INT_MASK5, 1 << 2),
81 [TPS6586X_INT_AC_DET] = TPS6586X_IRQ(TPS6586X_INT_MASK5, 1 << 3),
82 [TPS6586X_INT_BAT_DET] = TPS6586X_IRQ(TPS6586X_INT_MASK3, 1 << 0),
83 [TPS6586X_INT_CHG_STAT] = TPS6586X_IRQ(TPS6586X_INT_MASK4, 0xfc),
84 [TPS6586X_INT_CHG_TEMP] = TPS6586X_IRQ(TPS6586X_INT_MASK3, 0x06),
85 [TPS6586X_INT_PP] = TPS6586X_IRQ(TPS6586X_INT_MASK3, 0xf0),
86 [TPS6586X_INT_RESUME] = TPS6586X_IRQ(TPS6586X_INT_MASK5, 1 << 5),
87 [TPS6586X_INT_LOW_SYS] = TPS6586X_IRQ(TPS6586X_INT_MASK5, 1 << 6),
88 [TPS6586X_INT_RTC_ALM2] = TPS6586X_IRQ(TPS6586X_INT_MASK4, 1 << 1),
91 struct tps6586x {
92 struct mutex lock;
93 struct device *dev;
94 struct i2c_client *client;
96 struct gpio_chip gpio;
97 struct irq_chip irq_chip;
98 struct mutex irq_lock;
99 int irq_base;
100 u32 irq_en;
101 u8 mask_cache[5];
102 u8 mask_reg[5];
105 static inline int __tps6586x_read(struct i2c_client *client,
106 int reg, uint8_t *val)
108 int ret;
110 ret = i2c_smbus_read_byte_data(client, reg);
111 if (ret < 0) {
112 dev_err(&client->dev, "failed reading at 0x%02x\n", reg);
113 return ret;
116 *val = (uint8_t)ret;
118 return 0;
121 static inline int __tps6586x_reads(struct i2c_client *client, int reg,
122 int len, uint8_t *val)
124 int ret;
126 ret = i2c_smbus_read_i2c_block_data(client, reg, len, val);
127 if (ret < 0) {
128 dev_err(&client->dev, "failed reading from 0x%02x\n", reg);
129 return ret;
132 return 0;
135 static inline int __tps6586x_write(struct i2c_client *client,
136 int reg, uint8_t val)
138 int ret;
140 ret = i2c_smbus_write_byte_data(client, reg, val);
141 if (ret < 0) {
142 dev_err(&client->dev, "failed writing 0x%02x to 0x%02x\n",
143 val, reg);
144 return ret;
147 return 0;
150 static inline int __tps6586x_writes(struct i2c_client *client, int reg,
151 int len, uint8_t *val)
153 int ret, i;
155 for (i = 0; i < len; i++) {
156 ret = __tps6586x_write(client, reg + i, *(val + i));
157 if (ret < 0)
158 return ret;
161 return 0;
164 int tps6586x_write(struct device *dev, int reg, uint8_t val)
166 return __tps6586x_write(to_i2c_client(dev), reg, val);
168 EXPORT_SYMBOL_GPL(tps6586x_write);
170 int tps6586x_writes(struct device *dev, int reg, int len, uint8_t *val)
172 return __tps6586x_writes(to_i2c_client(dev), reg, len, val);
174 EXPORT_SYMBOL_GPL(tps6586x_writes);
176 int tps6586x_read(struct device *dev, int reg, uint8_t *val)
178 return __tps6586x_read(to_i2c_client(dev), reg, val);
180 EXPORT_SYMBOL_GPL(tps6586x_read);
182 int tps6586x_reads(struct device *dev, int reg, int len, uint8_t *val)
184 return __tps6586x_reads(to_i2c_client(dev), reg, len, val);
186 EXPORT_SYMBOL_GPL(tps6586x_reads);
188 int tps6586x_set_bits(struct device *dev, int reg, uint8_t bit_mask)
190 struct tps6586x *tps6586x = dev_get_drvdata(dev);
191 uint8_t reg_val;
192 int ret = 0;
194 mutex_lock(&tps6586x->lock);
196 ret = __tps6586x_read(to_i2c_client(dev), reg, &reg_val);
197 if (ret)
198 goto out;
200 if ((reg_val & bit_mask) == 0) {
201 reg_val |= bit_mask;
202 ret = __tps6586x_write(to_i2c_client(dev), reg, reg_val);
204 out:
205 mutex_unlock(&tps6586x->lock);
206 return ret;
208 EXPORT_SYMBOL_GPL(tps6586x_set_bits);
210 int tps6586x_clr_bits(struct device *dev, int reg, uint8_t bit_mask)
212 struct tps6586x *tps6586x = dev_get_drvdata(dev);
213 uint8_t reg_val;
214 int ret = 0;
216 mutex_lock(&tps6586x->lock);
218 ret = __tps6586x_read(to_i2c_client(dev), reg, &reg_val);
219 if (ret)
220 goto out;
222 if (reg_val & bit_mask) {
223 reg_val &= ~bit_mask;
224 ret = __tps6586x_write(to_i2c_client(dev), reg, reg_val);
226 out:
227 mutex_unlock(&tps6586x->lock);
228 return ret;
230 EXPORT_SYMBOL_GPL(tps6586x_clr_bits);
232 int tps6586x_update(struct device *dev, int reg, uint8_t val, uint8_t mask)
234 struct tps6586x *tps6586x = dev_get_drvdata(dev);
235 uint8_t reg_val;
236 int ret = 0;
238 mutex_lock(&tps6586x->lock);
240 ret = __tps6586x_read(tps6586x->client, reg, &reg_val);
241 if (ret)
242 goto out;
244 if ((reg_val & mask) != val) {
245 reg_val = (reg_val & ~mask) | val;
246 ret = __tps6586x_write(tps6586x->client, reg, reg_val);
248 out:
249 mutex_unlock(&tps6586x->lock);
250 return ret;
252 EXPORT_SYMBOL_GPL(tps6586x_update);
254 static int tps6586x_gpio_get(struct gpio_chip *gc, unsigned offset)
256 struct tps6586x *tps6586x = container_of(gc, struct tps6586x, gpio);
257 uint8_t val;
258 int ret;
260 ret = __tps6586x_read(tps6586x->client, TPS6586X_GPIOSET2, &val);
261 if (ret)
262 return ret;
264 return !!(val & (1 << offset));
268 static void tps6586x_gpio_set(struct gpio_chip *chip, unsigned offset,
269 int value)
271 struct tps6586x *tps6586x = container_of(chip, struct tps6586x, gpio);
273 __tps6586x_write(tps6586x->client, TPS6586X_GPIOSET2,
274 value << offset);
277 static int tps6586x_gpio_output(struct gpio_chip *gc, unsigned offset,
278 int value)
280 struct tps6586x *tps6586x = container_of(gc, struct tps6586x, gpio);
281 uint8_t val, mask;
283 tps6586x_gpio_set(gc, offset, value);
285 val = 0x1 << (offset * 2);
286 mask = 0x3 << (offset * 2);
288 return tps6586x_update(tps6586x->dev, TPS6586X_GPIOSET1, val, mask);
291 static void tps6586x_gpio_init(struct tps6586x *tps6586x, int gpio_base)
293 int ret;
295 if (!gpio_base)
296 return;
298 tps6586x->gpio.owner = THIS_MODULE;
299 tps6586x->gpio.label = tps6586x->client->name;
300 tps6586x->gpio.dev = tps6586x->dev;
301 tps6586x->gpio.base = gpio_base;
302 tps6586x->gpio.ngpio = 4;
303 tps6586x->gpio.can_sleep = 1;
305 /* FIXME: add handling of GPIOs as dedicated inputs */
306 tps6586x->gpio.direction_output = tps6586x_gpio_output;
307 tps6586x->gpio.set = tps6586x_gpio_set;
308 tps6586x->gpio.get = tps6586x_gpio_get;
310 ret = gpiochip_add(&tps6586x->gpio);
311 if (ret)
312 dev_warn(tps6586x->dev, "GPIO registration failed: %d\n", ret);
315 static int __remove_subdev(struct device *dev, void *unused)
317 platform_device_unregister(to_platform_device(dev));
318 return 0;
321 static int tps6586x_remove_subdevs(struct tps6586x *tps6586x)
323 return device_for_each_child(tps6586x->dev, NULL, __remove_subdev);
326 static void tps6586x_irq_lock(struct irq_data *data)
328 struct tps6586x *tps6586x = irq_data_get_irq_chip_data(data);
330 mutex_lock(&tps6586x->irq_lock);
333 static void tps6586x_irq_enable(struct irq_data *irq_data)
335 struct tps6586x *tps6586x = irq_data_get_irq_chip_data(irq_data);
336 unsigned int __irq = irq_data->irq - tps6586x->irq_base;
337 const struct tps6586x_irq_data *data = &tps6586x_irqs[__irq];
339 tps6586x->mask_reg[data->mask_reg] &= ~data->mask_mask;
340 tps6586x->irq_en |= (1 << __irq);
343 static void tps6586x_irq_disable(struct irq_data *irq_data)
345 struct tps6586x *tps6586x = irq_data_get_irq_chip_data(irq_data);
347 unsigned int __irq = irq_data->irq - tps6586x->irq_base;
348 const struct tps6586x_irq_data *data = &tps6586x_irqs[__irq];
350 tps6586x->mask_reg[data->mask_reg] |= data->mask_mask;
351 tps6586x->irq_en &= ~(1 << __irq);
354 static void tps6586x_irq_sync_unlock(struct irq_data *data)
356 struct tps6586x *tps6586x = irq_data_get_irq_chip_data(data);
357 int i;
359 for (i = 0; i < ARRAY_SIZE(tps6586x->mask_reg); i++) {
360 if (tps6586x->mask_reg[i] != tps6586x->mask_cache[i]) {
361 if (!WARN_ON(tps6586x_write(tps6586x->dev,
362 TPS6586X_INT_MASK1 + i,
363 tps6586x->mask_reg[i])))
364 tps6586x->mask_cache[i] = tps6586x->mask_reg[i];
368 mutex_unlock(&tps6586x->irq_lock);
371 static irqreturn_t tps6586x_irq(int irq, void *data)
373 struct tps6586x *tps6586x = data;
374 u32 acks;
375 int ret = 0;
377 ret = tps6586x_reads(tps6586x->dev, TPS6586X_INT_ACK1,
378 sizeof(acks), (uint8_t *)&acks);
380 if (ret < 0) {
381 dev_err(tps6586x->dev, "failed to read interrupt status\n");
382 return IRQ_NONE;
385 acks = le32_to_cpu(acks);
387 while (acks) {
388 int i = __ffs(acks);
390 if (tps6586x->irq_en & (1 << i))
391 handle_nested_irq(tps6586x->irq_base + i);
393 acks &= ~(1 << i);
396 return IRQ_HANDLED;
399 static int __devinit tps6586x_irq_init(struct tps6586x *tps6586x, int irq,
400 int irq_base)
402 int i, ret;
403 u8 tmp[4];
405 if (!irq_base) {
406 dev_warn(tps6586x->dev, "No interrupt support on IRQ base\n");
407 return -EINVAL;
410 mutex_init(&tps6586x->irq_lock);
411 for (i = 0; i < 5; i++) {
412 tps6586x->mask_cache[i] = 0xff;
413 tps6586x->mask_reg[i] = 0xff;
414 tps6586x_write(tps6586x->dev, TPS6586X_INT_MASK1 + i, 0xff);
417 tps6586x_reads(tps6586x->dev, TPS6586X_INT_ACK1, sizeof(tmp), tmp);
419 tps6586x->irq_base = irq_base;
421 tps6586x->irq_chip.name = "tps6586x";
422 tps6586x->irq_chip.irq_enable = tps6586x_irq_enable;
423 tps6586x->irq_chip.irq_disable = tps6586x_irq_disable;
424 tps6586x->irq_chip.irq_bus_lock = tps6586x_irq_lock;
425 tps6586x->irq_chip.irq_bus_sync_unlock = tps6586x_irq_sync_unlock;
427 for (i = 0; i < ARRAY_SIZE(tps6586x_irqs); i++) {
428 int __irq = i + tps6586x->irq_base;
429 set_irq_chip_data(__irq, tps6586x);
430 set_irq_chip_and_handler(__irq, &tps6586x->irq_chip,
431 handle_simple_irq);
432 set_irq_nested_thread(__irq, 1);
433 #ifdef CONFIG_ARM
434 set_irq_flags(__irq, IRQF_VALID);
435 #endif
438 ret = request_threaded_irq(irq, NULL, tps6586x_irq, IRQF_ONESHOT,
439 "tps6586x", tps6586x);
441 if (!ret) {
442 device_init_wakeup(tps6586x->dev, 1);
443 enable_irq_wake(irq);
446 return ret;
449 static int __devinit tps6586x_add_subdevs(struct tps6586x *tps6586x,
450 struct tps6586x_platform_data *pdata)
452 struct tps6586x_subdev_info *subdev;
453 struct platform_device *pdev;
454 int i, ret = 0;
456 for (i = 0; i < pdata->num_subdevs; i++) {
457 subdev = &pdata->subdevs[i];
459 pdev = platform_device_alloc(subdev->name, subdev->id);
460 if (!pdev) {
461 ret = -ENOMEM;
462 goto failed;
465 pdev->dev.parent = tps6586x->dev;
466 pdev->dev.platform_data = subdev->platform_data;
468 ret = platform_device_add(pdev);
469 if (ret) {
470 platform_device_put(pdev);
471 goto failed;
474 return 0;
476 failed:
477 tps6586x_remove_subdevs(tps6586x);
478 return ret;
481 static int __devinit tps6586x_i2c_probe(struct i2c_client *client,
482 const struct i2c_device_id *id)
484 struct tps6586x_platform_data *pdata = client->dev.platform_data;
485 struct tps6586x *tps6586x;
486 int ret;
488 if (!pdata) {
489 dev_err(&client->dev, "tps6586x requires platform data\n");
490 return -ENOTSUPP;
493 ret = i2c_smbus_read_byte_data(client, TPS6586X_VERSIONCRC);
494 if (ret < 0) {
495 dev_err(&client->dev, "Chip ID read failed: %d\n", ret);
496 return -EIO;
499 dev_info(&client->dev, "VERSIONCRC is %02x\n", ret);
501 tps6586x = kzalloc(sizeof(struct tps6586x), GFP_KERNEL);
502 if (tps6586x == NULL)
503 return -ENOMEM;
505 tps6586x->client = client;
506 tps6586x->dev = &client->dev;
507 i2c_set_clientdata(client, tps6586x);
509 mutex_init(&tps6586x->lock);
511 if (client->irq) {
512 ret = tps6586x_irq_init(tps6586x, client->irq,
513 pdata->irq_base);
514 if (ret) {
515 dev_err(&client->dev, "IRQ init failed: %d\n", ret);
516 goto err_irq_init;
520 ret = tps6586x_add_subdevs(tps6586x, pdata);
521 if (ret) {
522 dev_err(&client->dev, "add devices failed: %d\n", ret);
523 goto err_add_devs;
526 tps6586x_gpio_init(tps6586x, pdata->gpio_base);
528 return 0;
530 err_add_devs:
531 if (client->irq)
532 free_irq(client->irq, tps6586x);
533 err_irq_init:
534 kfree(tps6586x);
535 return ret;
538 static int __devexit tps6586x_i2c_remove(struct i2c_client *client)
540 struct tps6586x *tps6586x = i2c_get_clientdata(client);
541 struct tps6586x_platform_data *pdata = client->dev.platform_data;
542 int ret;
544 if (client->irq)
545 free_irq(client->irq, tps6586x);
547 if (pdata->gpio_base) {
548 ret = gpiochip_remove(&tps6586x->gpio);
549 if (ret)
550 dev_err(&client->dev, "Can't remove gpio chip: %d\n",
551 ret);
554 tps6586x_remove_subdevs(tps6586x);
555 kfree(tps6586x);
556 return 0;
559 static const struct i2c_device_id tps6586x_id_table[] = {
560 { "tps6586x", 0 },
561 { },
563 MODULE_DEVICE_TABLE(i2c, tps6586x_id_table);
565 static struct i2c_driver tps6586x_driver = {
566 .driver = {
567 .name = "tps6586x",
568 .owner = THIS_MODULE,
570 .probe = tps6586x_i2c_probe,
571 .remove = __devexit_p(tps6586x_i2c_remove),
572 .id_table = tps6586x_id_table,
575 static int __init tps6586x_init(void)
577 return i2c_add_driver(&tps6586x_driver);
579 subsys_initcall(tps6586x_init);
581 static void __exit tps6586x_exit(void)
583 i2c_del_driver(&tps6586x_driver);
585 module_exit(tps6586x_exit);
587 MODULE_DESCRIPTION("TPS6586X core driver");
588 MODULE_AUTHOR("Mike Rapoport <mike@compulab.co.il>");
589 MODULE_LICENSE("GPL");