8780512530841759dd015a6c2348cd10bc2159c9
[pandora-kernel.git] / drivers / mfd / wm8350-i2c.c
1 /*
2  * wm8350-i2c.c  --  Generic I2C driver for Wolfson WM8350 PMIC
3  *
4  * Copyright 2007, 2008 Wolfson Microelectronics PLC.
5  *
6  * Author: Liam Girdwood
7  *         linux@wolfsonmicro.com
8  *
9  *  This program is free software; you can redistribute  it and/or modify it
10  *  under  the terms of  the GNU General  Public License as published by the
11  *  Free Software Foundation;  either version 2 of the  License, or (at your
12  *  option) any later version.
13  *
14  */
15
16 #include <linux/module.h>
17 #include <linux/moduleparam.h>
18 #include <linux/init.h>
19 #include <linux/i2c.h>
20 #include <linux/platform_device.h>
21 #include <linux/mfd/wm8350/core.h>
22
23 static int wm8350_i2c_read_device(struct wm8350 *wm8350, char reg,
24                                   int bytes, void *dest)
25 {
26         int ret;
27
28         ret = i2c_master_send(wm8350->i2c_client, &reg, 1);
29         if (ret < 0)
30                 return ret;
31         ret = i2c_master_recv(wm8350->i2c_client, dest, bytes);
32         if (ret < 0)
33                 return ret;
34         if (ret != bytes)
35                 return -EIO;
36         return 0;
37 }
38
39 static int wm8350_i2c_write_device(struct wm8350 *wm8350, char reg,
40                                    int bytes, void *src)
41 {
42         /* we add 1 byte for device register */
43         u8 msg[(WM8350_MAX_REGISTER << 1) + 1];
44         int ret;
45
46         if (bytes > ((WM8350_MAX_REGISTER << 1) + 1))
47                 return -EINVAL;
48
49         msg[0] = reg;
50         memcpy(&msg[1], src, bytes);
51         ret = i2c_master_send(wm8350->i2c_client, msg, bytes + 1);
52         if (ret < 0)
53                 return ret;
54         if (ret != bytes + 1)
55                 return -EIO;
56         return 0;
57 }
58
59 static int wm8350_i2c_probe(struct i2c_client *i2c,
60                             const struct i2c_device_id *id)
61 {
62         struct wm8350 *wm8350;
63         int ret = 0;
64
65         wm8350 = kzalloc(sizeof(struct wm8350), GFP_KERNEL);
66         if (wm8350 == NULL) {
67                 kfree(i2c);
68                 return -ENOMEM;
69         }
70
71         i2c_set_clientdata(i2c, wm8350);
72         wm8350->dev = &i2c->dev;
73         wm8350->i2c_client = i2c;
74         wm8350->read_dev = wm8350_i2c_read_device;
75         wm8350->write_dev = wm8350_i2c_write_device;
76
77         ret = wm8350_device_init(wm8350, i2c->irq, i2c->dev.platform_data);
78         if (ret < 0)
79                 goto err;
80
81         return ret;
82
83 err:
84         kfree(wm8350);
85         return ret;
86 }
87
88 static int wm8350_i2c_remove(struct i2c_client *i2c)
89 {
90         struct wm8350 *wm8350 = i2c_get_clientdata(i2c);
91
92         wm8350_device_exit(wm8350);
93         kfree(wm8350);
94
95         return 0;
96 }
97
98 static const struct i2c_device_id wm8350_i2c_id[] = {
99        { "wm8350", 0 },
100        { "wm8352", 0 },
101        { }
102 };
103 MODULE_DEVICE_TABLE(i2c, wm8350_i2c_id);
104
105
106 static struct i2c_driver wm8350_i2c_driver = {
107         .driver = {
108                    .name = "wm8350",
109                    .owner = THIS_MODULE,
110         },
111         .probe = wm8350_i2c_probe,
112         .remove = wm8350_i2c_remove,
113         .id_table = wm8350_i2c_id,
114 };
115
116 static int __init wm8350_i2c_init(void)
117 {
118         return i2c_add_driver(&wm8350_i2c_driver);
119 }
120 /* init early so consumer devices can complete system boot */
121 subsys_initcall(wm8350_i2c_init);
122
123 static void __exit wm8350_i2c_exit(void)
124 {
125         i2c_del_driver(&wm8350_i2c_driver);
126 }
127 module_exit(wm8350_i2c_exit);
128
129 MODULE_DESCRIPTION("I2C support for the WM8350 AudioPlus PMIC");
130 MODULE_LICENSE("GPL");