Merge branches 'core-fixes-for-linus' and 'irq-fixes-for-linus' of git://git.kernel...
[pandora-kernel.git] / drivers / mfd / ab8500-i2c.c
1 /*
2  * Copyright (C) ST-Ericsson SA 2010
3  * Author: Mattias Wallin <mattias.wallin@stericsson.com> for ST-Ericsson.
4  * License Terms: GNU General Public License v2
5  * This file was based on drivers/mfd/ab8500-spi.c
6  */
7
8 #include <linux/kernel.h>
9 #include <linux/slab.h>
10 #include <linux/init.h>
11 #include <linux/module.h>
12 #include <linux/platform_device.h>
13 #include <linux/mfd/ab8500.h>
14 #include <linux/mfd/db8500-prcmu.h>
15
16 static int ab8500_i2c_write(struct ab8500 *ab8500, u16 addr, u8 data)
17 {
18         int ret;
19
20         ret = prcmu_abb_write((u8)(addr >> 8), (u8)(addr & 0xFF), &data, 1);
21         if (ret < 0)
22                 dev_err(ab8500->dev, "prcmu i2c error %d\n", ret);
23         return ret;
24 }
25
26 static int ab8500_i2c_read(struct ab8500 *ab8500, u16 addr)
27 {
28         int ret;
29         u8 data;
30
31         ret = prcmu_abb_read((u8)(addr >> 8), (u8)(addr & 0xFF), &data, 1);
32         if (ret < 0) {
33                 dev_err(ab8500->dev, "prcmu i2c error %d\n", ret);
34                 return ret;
35         }
36         return (int)data;
37 }
38
39 static int __devinit ab8500_i2c_probe(struct platform_device *plf)
40 {
41         struct ab8500 *ab8500;
42         struct resource *resource;
43         int ret;
44
45         ab8500 = kzalloc(sizeof *ab8500, GFP_KERNEL);
46         if (!ab8500)
47                 return -ENOMEM;
48
49         ab8500->dev = &plf->dev;
50
51         resource = platform_get_resource(plf, IORESOURCE_IRQ, 0);
52         if (!resource) {
53                 kfree(ab8500);
54                 return -ENODEV;
55         }
56
57         ab8500->irq = resource->start;
58
59         ab8500->read = ab8500_i2c_read;
60         ab8500->write = ab8500_i2c_write;
61
62         platform_set_drvdata(plf, ab8500);
63
64         ret = ab8500_init(ab8500);
65         if (ret)
66                 kfree(ab8500);
67
68         return ret;
69 }
70
71 static int __devexit ab8500_i2c_remove(struct platform_device *plf)
72 {
73         struct ab8500 *ab8500 = platform_get_drvdata(plf);
74
75         ab8500_exit(ab8500);
76         kfree(ab8500);
77
78         return 0;
79 }
80
81 static struct platform_driver ab8500_i2c_driver = {
82         .driver = {
83                 .name = "ab8500-i2c",
84                 .owner = THIS_MODULE,
85         },
86         .probe  = ab8500_i2c_probe,
87         .remove = __devexit_p(ab8500_i2c_remove)
88 };
89
90 static int __init ab8500_i2c_init(void)
91 {
92         return platform_driver_register(&ab8500_i2c_driver);
93 }
94
95 static void __exit ab8500_i2c_exit(void)
96 {
97         platform_driver_unregister(&ab8500_i2c_driver);
98 }
99 arch_initcall(ab8500_i2c_init);
100 module_exit(ab8500_i2c_exit);
101
102 MODULE_AUTHOR("Mattias WALLIN <mattias.wallin@stericsson.com");
103 MODULE_DESCRIPTION("AB8500 Core access via PRCMU I2C");
104 MODULE_LICENSE("GPL v2");