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>
22 #include <linux/mfd/wm831x/core.h>
23 #include <linux/mfd/wm831x/pdata.h>
25 static int wm831x_i2c_read_device(struct wm831x
*wm831x
, unsigned short reg
,
26 int bytes
, void *dest
)
28 struct i2c_client
*i2c
= wm831x
->control_data
;
30 u16 r
= cpu_to_be16(reg
);
32 ret
= i2c_master_send(i2c
, (unsigned char *)&r
, 2);
38 ret
= i2c_master_recv(i2c
, dest
, bytes
);
46 /* Currently we allocate the write buffer on the stack; this is OK for
47 * small writes - if we need to do large writes this will need to be
50 static int wm831x_i2c_write_device(struct wm831x
*wm831x
, unsigned short reg
,
53 struct i2c_client
*i2c
= wm831x
->control_data
;
54 unsigned char msg
[bytes
+ 2];
57 reg
= cpu_to_be16(reg
);
58 memcpy(&msg
[0], ®
, 2);
59 memcpy(&msg
[2], src
, bytes
);
61 ret
= i2c_master_send(i2c
, msg
, bytes
+ 2);
70 static int wm831x_i2c_probe(struct i2c_client
*i2c
,
71 const struct i2c_device_id
*id
)
73 struct wm831x
*wm831x
;
75 wm831x
= kzalloc(sizeof(struct wm831x
), GFP_KERNEL
);
79 i2c_set_clientdata(i2c
, wm831x
);
80 wm831x
->dev
= &i2c
->dev
;
81 wm831x
->control_data
= i2c
;
82 wm831x
->read_dev
= wm831x_i2c_read_device
;
83 wm831x
->write_dev
= wm831x_i2c_write_device
;
85 return wm831x_device_init(wm831x
, id
->driver_data
, i2c
->irq
);
88 static int wm831x_i2c_remove(struct i2c_client
*i2c
)
90 struct wm831x
*wm831x
= i2c_get_clientdata(i2c
);
92 wm831x_device_exit(wm831x
);
97 static int wm831x_i2c_suspend(struct i2c_client
*i2c
, pm_message_t mesg
)
99 struct wm831x
*wm831x
= i2c_get_clientdata(i2c
);
101 return wm831x_device_suspend(wm831x
);
104 static const struct i2c_device_id wm831x_i2c_id
[] = {
105 { "wm8310", WM8310
},
106 { "wm8311", WM8311
},
107 { "wm8312", WM8312
},
108 { "wm8320", WM8320
},
109 { "wm8321", WM8321
},
110 { "wm8325", WM8325
},
113 MODULE_DEVICE_TABLE(i2c
, wm831x_i2c_id
);
116 static struct i2c_driver wm831x_i2c_driver
= {
119 .owner
= THIS_MODULE
,
121 .probe
= wm831x_i2c_probe
,
122 .remove
= wm831x_i2c_remove
,
123 .suspend
= wm831x_i2c_suspend
,
124 .id_table
= wm831x_i2c_id
,
127 static int __init
wm831x_i2c_init(void)
131 ret
= i2c_add_driver(&wm831x_i2c_driver
);
133 pr_err("Failed to register wm831x I2C driver: %d\n", ret
);
137 subsys_initcall(wm831x_i2c_init
);
139 static void __exit
wm831x_i2c_exit(void)
141 i2c_del_driver(&wm831x_i2c_driver
);
143 module_exit(wm831x_i2c_exit
);