2 * wm831x-i2c.c -- I2C access for Wolfson WM831x PMICs
4 * Copyright 2009,2010 Wolfson Microelectronics PLC.
6 * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2 of the License, or (at your
11 * option) any later version.
15 #include <linux/kernel.h>
16 #include <linux/module.h>
17 #include <linux/i2c.h>
18 #include <linux/delay.h>
19 #include <linux/mfd/core.h>
20 #include <linux/slab.h>
21 #include <linux/gpio.h>
23 #include <linux/mfd/wm831x/core.h>
24 #include <linux/mfd/wm831x/pdata.h>
26 static int wm831x_i2c_read_device(struct wm831x *wm831x, unsigned short reg,
27 int bytes, void *dest)
29 #if defined(CONFIG_ARCH_RK30)
30 const struct i2c_client *client = wm831x->control_data;
31 struct i2c_adapter *adap=client->adapter;
32 struct i2c_msg msgs[2];
35 const short regs = reg;
36 int scl_rate= 100 * 1000;
40 reg_buf[0] = (regs & 0xff00) >> 8;
41 reg_buf[1] = regs & 0x00ff;
43 msgs[0].addr = client->addr;
44 msgs[0].flags = client->flags;
46 msgs[0].buf = reg_buf;
47 msgs[0].scl_rate = scl_rate;
49 msgs[1].addr = client->addr;
50 msgs[1].flags = client->flags | I2C_M_RD;
51 msgs[1].len = count * 2;
52 msgs[1].buf = (char *)buf;
53 msgs[1].scl_rate = scl_rate;
55 ret = i2c_transfer(adap, msgs, 2);
57 return (ret == 2)? 0 : ret;
59 struct i2c_client *i2c = wm831x->control_data;
61 u16 r = cpu_to_be16(reg);
63 ret = i2c_master_send(i2c, (unsigned char *)&r, 2);
69 ret = i2c_master_recv(i2c, dest, bytes);
78 /* Currently we allocate the write buffer on the stack; this is OK for
79 * small writes - if we need to do large writes this will need to be
82 static int wm831x_i2c_write_device(struct wm831x *wm831x, unsigned short reg,
85 #if defined(CONFIG_ARCH_RK30)
86 const struct i2c_client *client = wm831x->control_data;
87 struct i2c_adapter *adap=client->adapter;
90 const short regs = reg;
91 const short *buf = src;
93 int scl_rate = 100 * 1000;
96 char *tx_buf = (char *)kmalloc(2 * (count + 1), GFP_KERNEL);
99 tx_buf[0] = (regs & 0xff00) >> 8;
100 tx_buf[1] = regs & 0x00ff;
101 for(i = 0; i < count; i++){
102 tx_buf[i*2+3] = (buf[i] & 0xff00) >> 8;
103 tx_buf[i*2+2] = buf[i] & 0x00ff;
106 msg.addr = client->addr;
107 msg.flags = client->flags;
108 msg.len = 2 * (count + 1);
109 msg.buf = (char *)tx_buf;
110 msg.scl_rate = scl_rate;
112 ret = i2c_transfer(adap, &msg, 1);
114 return (ret == 1) ? 0 : ret;
116 struct i2c_client *i2c = wm831x->control_data;
117 unsigned char msg[bytes + 2];
120 reg = cpu_to_be16(reg);
121 memcpy(&msg[0], ®, 2);
122 memcpy(&msg[2], src, bytes);
124 ret = i2c_master_send(i2c, msg, bytes + 2);
134 static int wm831x_i2c_probe(struct i2c_client *i2c,
135 const struct i2c_device_id *id)
137 struct wm831x *wm831x;
140 wm831x = kzalloc(sizeof(struct wm831x), GFP_KERNEL);
144 i2c_set_clientdata(i2c, wm831x);
147 ret = gpio_request(gpio, "wm831x");
149 printk( "failed to request rk gpio irq for wm831x \n");
152 gpio_pull_updown(gpio, GPIOPullUp);
154 printk("failed to pull up gpio irq for wm831x \n");
157 irq = gpio_to_irq(gpio);
159 wm831x->dev = &i2c->dev;
160 wm831x->control_data = i2c;
161 wm831x->read_dev = wm831x_i2c_read_device;
162 wm831x->write_dev = wm831x_i2c_write_device;
164 return wm831x_device_init(wm831x, id->driver_data, irq);
167 static int wm831x_i2c_remove(struct i2c_client *i2c)
169 struct wm831x *wm831x = i2c_get_clientdata(i2c);
171 wm831x_device_exit(wm831x);
176 static int wm831x_i2c_suspend(struct device *dev)
178 struct wm831x *wm831x = dev_get_drvdata(dev);
180 spin_lock(&wm831x->flag_lock);
181 wm831x->flag_suspend = 1;
182 spin_unlock(&wm831x->flag_lock);
184 return wm831x_device_suspend(wm831x);
187 static int wm831x_i2c_resume(struct device *dev)
189 struct wm831x *wm831x = dev_get_drvdata(dev);
191 //set some intterupt again while resume
192 for (i = 0; i < ARRAY_SIZE(wm831x->irq_masks_cur); i++) {
193 //printk("irq_masks_cur[%d]=0x%x\n",i,wm831x->irq_masks_cur[i]);
195 if (wm831x->irq_masks_cur[i] != wm831x->irq_masks_cache[i]) {
196 wm831x->irq_masks_cache[i] = wm831x->irq_masks_cur[i];
197 wm831x_reg_write(wm831x,
198 WM831X_INTERRUPT_STATUS_1_MASK + i,
199 wm831x->irq_masks_cur[i]);
207 void wm831x_i2c_shutdown(struct i2c_client *i2c)
209 struct wm831x *wm831x = i2c_get_clientdata(i2c);
210 // printk("%s\n", __FUNCTION__);
211 // wm831x_device_shutdown(wm831x);
214 static const struct i2c_device_id wm831x_i2c_id[] = {
215 { "wm8310", WM8310 },
216 { "wm8311", WM8311 },
217 { "wm8312", WM8312 },
218 { "wm8320", WM8320 },
219 { "wm8321", WM8321 },
220 { "wm8325", WM8325 },
221 { "wm8326", WM8326 },
224 MODULE_DEVICE_TABLE(i2c, wm831x_i2c_id);
226 static const struct dev_pm_ops wm831x_pm_ops = {
227 .suspend = wm831x_i2c_suspend,
228 .resume = wm831x_i2c_resume,
231 static struct i2c_driver wm831x_i2c_driver = {
234 .owner = THIS_MODULE,
235 .pm = &wm831x_pm_ops,
237 .probe = wm831x_i2c_probe,
238 .remove = wm831x_i2c_remove,
239 .shutdown = wm831x_i2c_shutdown,
240 .id_table = wm831x_i2c_id,
243 static int __init wm831x_i2c_init(void)
247 printk("%s\n", __FUNCTION__);
248 ret = i2c_add_driver(&wm831x_i2c_driver);
250 pr_err("Failed to register wm831x I2C driver: %d\n", ret);
254 subsys_initcall_sync(wm831x_i2c_init);
256 static void __exit wm831x_i2c_exit(void)
258 i2c_del_driver(&wm831x_i2c_driver);
260 module_exit(wm831x_i2c_exit);