blob: 5ad4b772b09797bec97c9759bb9c80e609102651 [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>
Vishwanathrao Badarkhe, Manish154c4c72013-01-24 16:25:17 +053022#include <linux/of_device.h>
Todd Fischer31dd6a22010-04-08 09:04:55 +020023#include <linux/mfd/core.h>
24#include <linux/mfd/tps6507x.h>
25
26static struct mfd_cell tps6507x_devs[] = {
27 {
28 .name = "tps6507x-pmic",
29 },
Todd Fischer75259962010-04-05 17:53:12 -060030 {
31 .name = "tps6507x-ts",
32 },
Todd Fischer31dd6a22010-04-08 09:04:55 +020033};
34
35
36static int tps6507x_i2c_read_device(struct tps6507x_dev *tps6507x, char reg,
37 int bytes, void *dest)
38{
39 struct i2c_client *i2c = tps6507x->i2c_client;
40 struct i2c_msg xfer[2];
41 int ret;
42
43 /* Write register */
44 xfer[0].addr = i2c->addr;
45 xfer[0].flags = 0;
46 xfer[0].len = 1;
47 xfer[0].buf = &reg;
48
49 /* Read data */
50 xfer[1].addr = i2c->addr;
51 xfer[1].flags = I2C_M_RD;
52 xfer[1].len = bytes;
53 xfer[1].buf = dest;
54
55 ret = i2c_transfer(i2c->adapter, xfer, 2);
56 if (ret == 2)
57 ret = 0;
58 else if (ret >= 0)
59 ret = -EIO;
60
61 return ret;
62}
63
64static int tps6507x_i2c_write_device(struct tps6507x_dev *tps6507x, char reg,
65 int bytes, void *src)
66{
67 struct i2c_client *i2c = tps6507x->i2c_client;
68 /* we add 1 byte for device register */
69 u8 msg[TPS6507X_MAX_REGISTER + 1];
70 int ret;
71
Axel Lina8d6aa02010-10-13 10:44:39 +080072 if (bytes > TPS6507X_MAX_REGISTER)
Todd Fischer31dd6a22010-04-08 09:04:55 +020073 return -EINVAL;
74
75 msg[0] = reg;
76 memcpy(&msg[1], src, bytes);
77
78 ret = i2c_master_send(i2c, msg, bytes + 1);
79 if (ret < 0)
80 return ret;
81 if (ret != bytes + 1)
82 return -EIO;
83 return 0;
84}
85
86static int tps6507x_i2c_probe(struct i2c_client *i2c,
87 const struct i2c_device_id *id)
88{
89 struct tps6507x_dev *tps6507x;
Todd Fischer31dd6a22010-04-08 09:04:55 +020090
Axel Lin1881b682012-12-09 20:25:55 +080091 tps6507x = devm_kzalloc(&i2c->dev, sizeof(struct tps6507x_dev),
92 GFP_KERNEL);
Axel Lin04a06422010-08-09 14:49:46 +080093 if (tps6507x == NULL)
Todd Fischer31dd6a22010-04-08 09:04:55 +020094 return -ENOMEM;
Todd Fischer31dd6a22010-04-08 09:04:55 +020095
96 i2c_set_clientdata(i2c, tps6507x);
97 tps6507x->dev = &i2c->dev;
98 tps6507x->i2c_client = i2c;
99 tps6507x->read_dev = tps6507x_i2c_read_device;
100 tps6507x->write_dev = tps6507x_i2c_write_device;
101
Axel Lin1881b682012-12-09 20:25:55 +0800102 return mfd_add_devices(tps6507x->dev, -1, tps6507x_devs,
103 ARRAY_SIZE(tps6507x_devs), NULL, 0, NULL);
Todd Fischer31dd6a22010-04-08 09:04:55 +0200104}
105
106static int tps6507x_i2c_remove(struct i2c_client *i2c)
107{
108 struct tps6507x_dev *tps6507x = i2c_get_clientdata(i2c);
109
110 mfd_remove_devices(tps6507x->dev);
Todd Fischer31dd6a22010-04-08 09:04:55 +0200111 return 0;
112}
113
114static const struct i2c_device_id tps6507x_i2c_id[] = {
115 { "tps6507x", 0 },
116 { }
117};
118MODULE_DEVICE_TABLE(i2c, tps6507x_i2c_id);
119
Vishwanathrao Badarkhe, Manish154c4c72013-01-24 16:25:17 +0530120#ifdef CONFIG_OF
121static struct of_device_id tps6507x_of_match[] = {
122 {.compatible = "ti,tps6507x", },
123 {},
124};
125MODULE_DEVICE_TABLE(of, tps6507x_of_match);
126#endif
Todd Fischer31dd6a22010-04-08 09:04:55 +0200127
128static struct i2c_driver tps6507x_i2c_driver = {
129 .driver = {
130 .name = "tps6507x",
131 .owner = THIS_MODULE,
Vishwanathrao Badarkhe, Manish154c4c72013-01-24 16:25:17 +0530132 .of_match_table = of_match_ptr(tps6507x_of_match),
Todd Fischer31dd6a22010-04-08 09:04:55 +0200133 },
134 .probe = tps6507x_i2c_probe,
135 .remove = tps6507x_i2c_remove,
136 .id_table = tps6507x_i2c_id,
137};
138
139static int __init tps6507x_i2c_init(void)
140{
141 return i2c_add_driver(&tps6507x_i2c_driver);
142}
143/* init early so consumer devices can complete system boot */
144subsys_initcall(tps6507x_i2c_init);
145
146static void __exit tps6507x_i2c_exit(void)
147{
148 i2c_del_driver(&tps6507x_i2c_driver);
149}
150module_exit(tps6507x_i2c_exit);
151
152MODULE_DESCRIPTION("TPS6507x chip family multi-function driver");
153MODULE_LICENSE("GPL");