Commit | Line | Data |
---|---|---|
31dd6a26 TF |
1 | /* |
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> | |
5c8c8794 | 22 | #include <linux/of.h> |
31dd6a26 TF |
23 | #include <linux/mfd/core.h> |
24 | #include <linux/mfd/tps6507x.h> | |
25 | ||
30fe2b5b | 26 | static const struct mfd_cell tps6507x_devs[] = { |
31dd6a26 TF |
27 | { |
28 | .name = "tps6507x-pmic", | |
29 | }, | |
75259966 TF |
30 | { |
31 | .name = "tps6507x-ts", | |
32 | }, | |
31dd6a26 TF |
33 | }; |
34 | ||
35 | ||
36 | static 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 = ® | |
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 | ||
64 | static 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 | ||
a8d6aa08 | 72 | if (bytes > TPS6507X_MAX_REGISTER) |
31dd6a26 TF |
73 | 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 | ||
0e1a8964 | 86 | static int tps6507x_i2c_probe(struct i2c_client *i2c) |
31dd6a26 TF |
87 | { |
88 | struct tps6507x_dev *tps6507x; | |
31dd6a26 | 89 | |
1881b68b AL |
90 | tps6507x = devm_kzalloc(&i2c->dev, sizeof(struct tps6507x_dev), |
91 | GFP_KERNEL); | |
04a06423 | 92 | if (tps6507x == NULL) |
31dd6a26 | 93 | return -ENOMEM; |
31dd6a26 TF |
94 | |
95 | i2c_set_clientdata(i2c, tps6507x); | |
96 | tps6507x->dev = &i2c->dev; | |
97 | tps6507x->i2c_client = i2c; | |
98 | tps6507x->read_dev = tps6507x_i2c_read_device; | |
99 | tps6507x->write_dev = tps6507x_i2c_write_device; | |
100 | ||
5635d994 LD |
101 | return devm_mfd_add_devices(tps6507x->dev, -1, tps6507x_devs, |
102 | ARRAY_SIZE(tps6507x_devs), NULL, 0, NULL); | |
31dd6a26 TF |
103 | } |
104 | ||
105 | static const struct i2c_device_id tps6507x_i2c_id[] = { | |
7657c9fb BC |
106 | { "tps6507x", 0 }, |
107 | { } | |
31dd6a26 TF |
108 | }; |
109 | MODULE_DEVICE_TABLE(i2c, tps6507x_i2c_id); | |
110 | ||
154c4c7b | 111 | #ifdef CONFIG_OF |
153a4ff4 | 112 | static const struct of_device_id tps6507x_of_match[] = { |
154c4c7b VBM |
113 | {.compatible = "ti,tps6507x", }, |
114 | {}, | |
115 | }; | |
116 | MODULE_DEVICE_TABLE(of, tps6507x_of_match); | |
117 | #endif | |
31dd6a26 TF |
118 | |
119 | static struct i2c_driver tps6507x_i2c_driver = { | |
120 | .driver = { | |
121 | .name = "tps6507x", | |
154c4c7b | 122 | .of_match_table = of_match_ptr(tps6507x_of_match), |
31dd6a26 | 123 | }, |
9816d859 | 124 | .probe = tps6507x_i2c_probe, |
31dd6a26 TF |
125 | .id_table = tps6507x_i2c_id, |
126 | }; | |
127 | ||
128 | static int __init tps6507x_i2c_init(void) | |
129 | { | |
130 | return i2c_add_driver(&tps6507x_i2c_driver); | |
131 | } | |
132 | /* init early so consumer devices can complete system boot */ | |
133 | subsys_initcall(tps6507x_i2c_init); | |
134 | ||
135 | static void __exit tps6507x_i2c_exit(void) | |
136 | { | |
137 | i2c_del_driver(&tps6507x_i2c_driver); | |
138 | } | |
139 | module_exit(tps6507x_i2c_exit); | |
140 | ||
141 | MODULE_DESCRIPTION("TPS6507x chip family multi-function driver"); | |
142 | MODULE_LICENSE("GPL"); |