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