blob: a081b925d10b2debf94e28ea2c5c500081262309 [file] [log] [blame]
Todd Fischer31dd6a22010-04-08 09:04:55 +02001/*
2 * tps6507x.c -- TPS6507x chip family multi-function driver
3 *
4 * Copyright (c) 2010 RidgeRun (todd.fischer@ridgerun.com)
5 *
6 * Author: Todd Fischer
7 * todd.fischer@ridgerun.com
8 *
9 * Credits:
10 *
11 * Using code from wm831x-*.c, wm8400-core, Wolfson Microelectronics PLC.
12 *
13 * For licencing details see kernel-base/COPYING
14 *
15 */
16
17#include <linux/module.h>
18#include <linux/moduleparam.h>
19#include <linux/init.h>
20#include <linux/slab.h>
21#include <linux/i2c.h>
Sachin Kamat5c8c8792013-10-16 14:26:53 +053022#include <linux/of.h>
Vishwanathrao Badarkhe, Manish154c4c72013-01-24 16:25:17 +053023#include <linux/of_device.h>
Todd Fischer31dd6a22010-04-08 09:04:55 +020024#include <linux/mfd/core.h>
25#include <linux/mfd/tps6507x.h>
26
27static struct mfd_cell tps6507x_devs[] = {
28 {
29 .name = "tps6507x-pmic",
30 },
Todd Fischer75259962010-04-05 17:53:12 -060031 {
32 .name = "tps6507x-ts",
33 },
Todd Fischer31dd6a22010-04-08 09:04:55 +020034};
35
36
37static int tps6507x_i2c_read_device(struct tps6507x_dev *tps6507x, char reg,
38 int bytes, void *dest)
39{
40 struct i2c_client *i2c = tps6507x->i2c_client;
41 struct i2c_msg xfer[2];
42 int ret;
43
44 /* Write register */
45 xfer[0].addr = i2c->addr;
46 xfer[0].flags = 0;
47 xfer[0].len = 1;
48 xfer[0].buf = &reg;
49
50 /* Read data */
51 xfer[1].addr = i2c->addr;
52 xfer[1].flags = I2C_M_RD;
53 xfer[1].len = bytes;
54 xfer[1].buf = dest;
55
56 ret = i2c_transfer(i2c->adapter, xfer, 2);
57 if (ret == 2)
58 ret = 0;
59 else if (ret >= 0)
60 ret = -EIO;
61
62 return ret;
63}
64
65static int tps6507x_i2c_write_device(struct tps6507x_dev *tps6507x, char reg,
66 int bytes, void *src)
67{
68 struct i2c_client *i2c = tps6507x->i2c_client;
69 /* we add 1 byte for device register */
70 u8 msg[TPS6507X_MAX_REGISTER + 1];
71 int ret;
72
Axel Lina8d6aa02010-10-13 10:44:39 +080073 if (bytes > TPS6507X_MAX_REGISTER)
Todd Fischer31dd6a22010-04-08 09:04:55 +020074 return -EINVAL;
75
76 msg[0] = reg;
77 memcpy(&msg[1], src, bytes);
78
79 ret = i2c_master_send(i2c, msg, bytes + 1);
80 if (ret < 0)
81 return ret;
82 if (ret != bytes + 1)
83 return -EIO;
84 return 0;
85}
86
87static int tps6507x_i2c_probe(struct i2c_client *i2c,
88 const struct i2c_device_id *id)
89{
90 struct tps6507x_dev *tps6507x;
Todd Fischer31dd6a22010-04-08 09:04:55 +020091
Axel Lin1881b682012-12-09 20:25:55 +080092 tps6507x = devm_kzalloc(&i2c->dev, sizeof(struct tps6507x_dev),
93 GFP_KERNEL);
Axel Lin04a06422010-08-09 14:49:46 +080094 if (tps6507x == NULL)
Todd Fischer31dd6a22010-04-08 09:04:55 +020095 return -ENOMEM;
Todd Fischer31dd6a22010-04-08 09:04:55 +020096
97 i2c_set_clientdata(i2c, tps6507x);
98 tps6507x->dev = &i2c->dev;
99 tps6507x->i2c_client = i2c;
100 tps6507x->read_dev = tps6507x_i2c_read_device;
101 tps6507x->write_dev = tps6507x_i2c_write_device;
102
Axel Lin1881b682012-12-09 20:25:55 +0800103 return mfd_add_devices(tps6507x->dev, -1, tps6507x_devs,
104 ARRAY_SIZE(tps6507x_devs), NULL, 0, NULL);
Todd Fischer31dd6a22010-04-08 09:04:55 +0200105}
106
107static int tps6507x_i2c_remove(struct i2c_client *i2c)
108{
109 struct tps6507x_dev *tps6507x = i2c_get_clientdata(i2c);
110
111 mfd_remove_devices(tps6507x->dev);
Todd Fischer31dd6a22010-04-08 09:04:55 +0200112 return 0;
113}
114
115static const struct i2c_device_id tps6507x_i2c_id[] = {
116 { "tps6507x", 0 },
117 { }
118};
119MODULE_DEVICE_TABLE(i2c, tps6507x_i2c_id);
120
Vishwanathrao Badarkhe, Manish154c4c72013-01-24 16:25:17 +0530121#ifdef CONFIG_OF
122static struct of_device_id tps6507x_of_match[] = {
123 {.compatible = "ti,tps6507x", },
124 {},
125};
126MODULE_DEVICE_TABLE(of, tps6507x_of_match);
127#endif
Todd Fischer31dd6a22010-04-08 09:04:55 +0200128
129static struct i2c_driver tps6507x_i2c_driver = {
130 .driver = {
131 .name = "tps6507x",
132 .owner = THIS_MODULE,
Vishwanathrao Badarkhe, Manish154c4c72013-01-24 16:25:17 +0530133 .of_match_table = of_match_ptr(tps6507x_of_match),
Todd Fischer31dd6a22010-04-08 09:04:55 +0200134 },
135 .probe = tps6507x_i2c_probe,
136 .remove = tps6507x_i2c_remove,
137 .id_table = tps6507x_i2c_id,
138};
139
140static int __init tps6507x_i2c_init(void)
141{
142 return i2c_add_driver(&tps6507x_i2c_driver);
143}
144/* init early so consumer devices can complete system boot */
145subsys_initcall(tps6507x_i2c_init);
146
147static void __exit tps6507x_i2c_exit(void)
148{
149 i2c_del_driver(&tps6507x_i2c_driver);
150}
151module_exit(tps6507x_i2c_exit);
152
153MODULE_DESCRIPTION("TPS6507x chip family multi-function driver");
154MODULE_LICENSE("GPL");