xref: /openbmc/linux/drivers/mfd/wm831x-i2c.c (revision 565d76cb)
1 /*
2  * wm831x-i2c.c  --  I2C access for Wolfson WM831x PMICs
3  *
4  * Copyright 2009,2010 Wolfson Microelectronics PLC.
5  *
6  * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
7  *
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.
12  *
13  */
14 
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 
22 #include <linux/mfd/wm831x/core.h>
23 #include <linux/mfd/wm831x/pdata.h>
24 
25 static int wm831x_i2c_read_device(struct wm831x *wm831x, unsigned short reg,
26 				  int bytes, void *dest)
27 {
28 	struct i2c_client *i2c = wm831x->control_data;
29 	int ret;
30 	u16 r = cpu_to_be16(reg);
31 
32 	ret = i2c_master_send(i2c, (unsigned char *)&r, 2);
33 	if (ret < 0)
34 		return ret;
35 	if (ret != 2)
36 		return -EIO;
37 
38 	ret = i2c_master_recv(i2c, dest, bytes);
39 	if (ret < 0)
40 		return ret;
41 	if (ret != bytes)
42 		return -EIO;
43 	return 0;
44 }
45 
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
48  * revised.
49  */
50 static int wm831x_i2c_write_device(struct wm831x *wm831x, unsigned short reg,
51 				   int bytes, void *src)
52 {
53 	struct i2c_client *i2c = wm831x->control_data;
54 	unsigned char msg[bytes + 2];
55 	int ret;
56 
57 	reg = cpu_to_be16(reg);
58 	memcpy(&msg[0], &reg, 2);
59 	memcpy(&msg[2], src, bytes);
60 
61 	ret = i2c_master_send(i2c, msg, bytes + 2);
62 	if (ret < 0)
63 		return ret;
64 	if (ret < bytes + 2)
65 		return -EIO;
66 
67 	return 0;
68 }
69 
70 static int wm831x_i2c_probe(struct i2c_client *i2c,
71 			    const struct i2c_device_id *id)
72 {
73 	struct wm831x *wm831x;
74 
75 	wm831x = kzalloc(sizeof(struct wm831x), GFP_KERNEL);
76 	if (wm831x == NULL)
77 		return -ENOMEM;
78 
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;
84 
85 	return wm831x_device_init(wm831x, id->driver_data, i2c->irq);
86 }
87 
88 static int wm831x_i2c_remove(struct i2c_client *i2c)
89 {
90 	struct wm831x *wm831x = i2c_get_clientdata(i2c);
91 
92 	wm831x_device_exit(wm831x);
93 
94 	return 0;
95 }
96 
97 static int wm831x_i2c_suspend(struct device *dev)
98 {
99 	struct wm831x *wm831x = dev_get_drvdata(dev);
100 
101 	return wm831x_device_suspend(wm831x);
102 }
103 
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 },
111 	{ "wm8326", WM8326 },
112 	{ }
113 };
114 MODULE_DEVICE_TABLE(i2c, wm831x_i2c_id);
115 
116 static const struct dev_pm_ops wm831x_pm_ops = {
117 	.suspend = wm831x_i2c_suspend,
118 };
119 
120 static struct i2c_driver wm831x_i2c_driver = {
121 	.driver = {
122 		.name = "wm831x",
123 		.owner = THIS_MODULE,
124 		.pm = &wm831x_pm_ops,
125 	},
126 	.probe = wm831x_i2c_probe,
127 	.remove = wm831x_i2c_remove,
128 	.id_table = wm831x_i2c_id,
129 };
130 
131 static int __init wm831x_i2c_init(void)
132 {
133 	int ret;
134 
135 	ret = i2c_add_driver(&wm831x_i2c_driver);
136 	if (ret != 0)
137 		pr_err("Failed to register wm831x I2C driver: %d\n", ret);
138 
139 	return ret;
140 }
141 subsys_initcall(wm831x_i2c_init);
142 
143 static void __exit wm831x_i2c_exit(void)
144 {
145 	i2c_del_driver(&wm831x_i2c_driver);
146 }
147 module_exit(wm831x_i2c_exit);
148