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)? count : 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 return wm831x_device_suspend(wm831x);
183 static int wm831x_i2c_resume(struct device *dev)
185 struct wm831x *wm831x = dev_get_drvdata(dev);
187 //set some intterupt again while resume
188 for (i = 0; i < ARRAY_SIZE(wm831x->irq_masks_cur); i++) {
189 //printk("irq_masks_cur[%d]=0x%x\n",i,wm831x->irq_masks_cur[i]);
191 if (wm831x->irq_masks_cur[i] != wm831x->irq_masks_cache[i]) {
192 wm831x->irq_masks_cache[i] = wm831x->irq_masks_cur[i];
193 wm831x_reg_write(wm831x,
194 WM831X_INTERRUPT_STATUS_1_MASK + i,
195 wm831x->irq_masks_cur[i]);
203 void wm831x_i2c_shutdown(struct i2c_client *i2c)
205 struct wm831x *wm831x = i2c_get_clientdata(i2c);
206 // printk("%s\n", __FUNCTION__);
207 // wm831x_device_shutdown(wm831x);
210 static const struct i2c_device_id wm831x_i2c_id[] = {
211 { "wm8310", WM8310 },
212 { "wm8311", WM8311 },
213 { "wm8312", WM8312 },
214 { "wm8320", WM8320 },
215 { "wm8321", WM8321 },
216 { "wm8325", WM8325 },
217 { "wm8326", WM8326 },
220 MODULE_DEVICE_TABLE(i2c, wm831x_i2c_id);
222 static const struct dev_pm_ops wm831x_pm_ops = {
223 .suspend = wm831x_i2c_suspend,
224 .resume = wm831x_i2c_resume,
227 static struct i2c_driver wm831x_i2c_driver = {
230 .owner = THIS_MODULE,
231 .pm = &wm831x_pm_ops,
233 .probe = wm831x_i2c_probe,
234 .remove = wm831x_i2c_remove,
235 .shutdown = wm831x_i2c_shutdown,
236 .id_table = wm831x_i2c_id,
239 static int __init wm831x_i2c_init(void)
243 printk("%s\n", __FUNCTION__);
244 ret = i2c_add_driver(&wm831x_i2c_driver);
246 pr_err("Failed to register wm831x I2C driver: %d\n", ret);
250 subsys_initcall_sync(wm831x_i2c_init);
252 static void __exit wm831x_i2c_exit(void)
254 i2c_del_driver(&wm831x_i2c_driver);
256 module_exit(wm831x_i2c_exit);