1 | /* |
2 | * CBUS I2C driver for Nokia Internet Tablets. |
3 | * |
4 | * Copyright (C) 2004-2010 Nokia Corporation |
5 | * |
6 | * Based on code written by Juha Yrjölä, David Weinehall, Mikko Ylinen and |
7 | * Felipe Balbi. Converted to I2C driver by Aaro Koskinen. |
8 | * |
9 | * This file is subject to the terms and conditions of the GNU General |
10 | * Public License. See the file "COPYING" in the main directory of this |
11 | * archive for more details. |
12 | * |
13 | * This program is distributed in the hope that it will be useful, |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16 | * GNU General Public License for more details. |
17 | */ |
18 | |
19 | #include <linux/io.h> |
20 | #include <linux/i2c.h> |
21 | #include <linux/slab.h> |
22 | #include <linux/delay.h> |
23 | #include <linux/errno.h> |
24 | #include <linux/kernel.h> |
25 | #include <linux/module.h> |
26 | #include <linux/gpio/consumer.h> |
27 | #include <linux/interrupt.h> |
28 | #include <linux/platform_device.h> |
29 | |
30 | /* |
31 | * Bit counts are derived from Nokia implementation. These should be checked |
32 | * if other CBUS implementations appear. |
33 | */ |
34 | #define CBUS_ADDR_BITS 3 |
35 | #define CBUS_REG_BITS 5 |
36 | |
37 | struct cbus_host { |
38 | spinlock_t lock; /* host lock */ |
39 | struct device *dev; |
40 | struct gpio_desc *clk; |
41 | struct gpio_desc *dat; |
42 | struct gpio_desc *sel; |
43 | }; |
44 | |
45 | /** |
46 | * cbus_send_bit - sends one bit over the bus |
47 | * @host: the host we're using |
48 | * @bit: one bit of information to send |
49 | */ |
50 | static void cbus_send_bit(struct cbus_host *host, unsigned bit) |
51 | { |
52 | gpiod_set_value(desc: host->dat, value: bit ? 1 : 0); |
53 | gpiod_set_value(desc: host->clk, value: 1); |
54 | gpiod_set_value(desc: host->clk, value: 0); |
55 | } |
56 | |
57 | /** |
58 | * cbus_send_data - sends @len amount of data over the bus |
59 | * @host: the host we're using |
60 | * @data: the data to send |
61 | * @len: size of the transfer |
62 | */ |
63 | static void cbus_send_data(struct cbus_host *host, unsigned data, unsigned len) |
64 | { |
65 | int i; |
66 | |
67 | for (i = len; i > 0; i--) |
68 | cbus_send_bit(host, bit: data & (1 << (i - 1))); |
69 | } |
70 | |
71 | /** |
72 | * cbus_receive_bit - receives one bit from the bus |
73 | * @host: the host we're using |
74 | */ |
75 | static int cbus_receive_bit(struct cbus_host *host) |
76 | { |
77 | int ret; |
78 | |
79 | gpiod_set_value(desc: host->clk, value: 1); |
80 | ret = gpiod_get_value(desc: host->dat); |
81 | gpiod_set_value(desc: host->clk, value: 0); |
82 | return ret; |
83 | } |
84 | |
85 | /** |
86 | * cbus_receive_word - receives 16-bit word from the bus |
87 | * @host: the host we're using |
88 | */ |
89 | static int cbus_receive_word(struct cbus_host *host) |
90 | { |
91 | int ret = 0; |
92 | int i; |
93 | |
94 | for (i = 16; i > 0; i--) { |
95 | int bit = cbus_receive_bit(host); |
96 | |
97 | if (bit < 0) |
98 | return bit; |
99 | |
100 | if (bit) |
101 | ret |= 1 << (i - 1); |
102 | } |
103 | return ret; |
104 | } |
105 | |
106 | /** |
107 | * cbus_transfer - transfers data over the bus |
108 | * @host: the host we're using |
109 | * @rw: read/write flag |
110 | * @dev: device address |
111 | * @reg: register address |
112 | * @data: if @rw == I2C_SBUS_WRITE data to send otherwise 0 |
113 | */ |
114 | static int cbus_transfer(struct cbus_host *host, char rw, unsigned dev, |
115 | unsigned reg, unsigned data) |
116 | { |
117 | unsigned long flags; |
118 | int ret; |
119 | |
120 | /* We don't want interrupts disturbing our transfer */ |
121 | spin_lock_irqsave(&host->lock, flags); |
122 | |
123 | /* Reset state and start of transfer, SEL stays down during transfer */ |
124 | gpiod_set_value(desc: host->sel, value: 0); |
125 | |
126 | /* Set the DAT pin to output */ |
127 | gpiod_direction_output(desc: host->dat, value: 1); |
128 | |
129 | /* Send the device address */ |
130 | cbus_send_data(host, data: dev, CBUS_ADDR_BITS); |
131 | |
132 | /* Send the rw flag */ |
133 | cbus_send_bit(host, bit: rw == I2C_SMBUS_READ); |
134 | |
135 | /* Send the register address */ |
136 | cbus_send_data(host, data: reg, CBUS_REG_BITS); |
137 | |
138 | if (rw == I2C_SMBUS_WRITE) { |
139 | cbus_send_data(host, data, len: 16); |
140 | ret = 0; |
141 | } else { |
142 | ret = gpiod_direction_input(desc: host->dat); |
143 | if (ret) { |
144 | dev_dbg(host->dev, "failed setting direction\n" ); |
145 | goto out; |
146 | } |
147 | gpiod_set_value(desc: host->clk, value: 1); |
148 | |
149 | ret = cbus_receive_word(host); |
150 | if (ret < 0) { |
151 | dev_dbg(host->dev, "failed receiving data\n" ); |
152 | goto out; |
153 | } |
154 | } |
155 | |
156 | /* Indicate end of transfer, SEL goes up until next transfer */ |
157 | gpiod_set_value(desc: host->sel, value: 1); |
158 | gpiod_set_value(desc: host->clk, value: 1); |
159 | gpiod_set_value(desc: host->clk, value: 0); |
160 | |
161 | out: |
162 | spin_unlock_irqrestore(lock: &host->lock, flags); |
163 | |
164 | return ret; |
165 | } |
166 | |
167 | static int cbus_i2c_smbus_xfer(struct i2c_adapter *adapter, |
168 | u16 addr, |
169 | unsigned short flags, |
170 | char read_write, |
171 | u8 command, |
172 | int size, |
173 | union i2c_smbus_data *data) |
174 | { |
175 | struct cbus_host *chost = i2c_get_adapdata(adap: adapter); |
176 | int ret; |
177 | |
178 | if (size != I2C_SMBUS_WORD_DATA) |
179 | return -EINVAL; |
180 | |
181 | ret = cbus_transfer(host: chost, rw: read_write == I2C_SMBUS_READ, dev: addr, |
182 | reg: command, data: data->word); |
183 | if (ret < 0) |
184 | return ret; |
185 | |
186 | if (read_write == I2C_SMBUS_READ) |
187 | data->word = ret; |
188 | |
189 | return 0; |
190 | } |
191 | |
192 | static u32 cbus_i2c_func(struct i2c_adapter *adapter) |
193 | { |
194 | return I2C_FUNC_SMBUS_READ_WORD_DATA | I2C_FUNC_SMBUS_WRITE_WORD_DATA; |
195 | } |
196 | |
197 | static const struct i2c_algorithm cbus_i2c_algo = { |
198 | .smbus_xfer = cbus_i2c_smbus_xfer, |
199 | .smbus_xfer_atomic = cbus_i2c_smbus_xfer, |
200 | .functionality = cbus_i2c_func, |
201 | }; |
202 | |
203 | static void cbus_i2c_remove(struct platform_device *pdev) |
204 | { |
205 | struct i2c_adapter *adapter = platform_get_drvdata(pdev); |
206 | |
207 | i2c_del_adapter(adap: adapter); |
208 | } |
209 | |
210 | static int cbus_i2c_probe(struct platform_device *pdev) |
211 | { |
212 | struct i2c_adapter *adapter; |
213 | struct cbus_host *chost; |
214 | |
215 | adapter = devm_kzalloc(dev: &pdev->dev, size: sizeof(struct i2c_adapter), |
216 | GFP_KERNEL); |
217 | if (!adapter) |
218 | return -ENOMEM; |
219 | |
220 | chost = devm_kzalloc(dev: &pdev->dev, size: sizeof(*chost), GFP_KERNEL); |
221 | if (!chost) |
222 | return -ENOMEM; |
223 | |
224 | if (gpiod_count(dev: &pdev->dev, NULL) != 3) |
225 | return -ENODEV; |
226 | chost->clk = devm_gpiod_get_index(dev: &pdev->dev, NULL, idx: 0, flags: GPIOD_OUT_LOW); |
227 | if (IS_ERR(ptr: chost->clk)) |
228 | return PTR_ERR(ptr: chost->clk); |
229 | chost->dat = devm_gpiod_get_index(dev: &pdev->dev, NULL, idx: 1, flags: GPIOD_IN); |
230 | if (IS_ERR(ptr: chost->dat)) |
231 | return PTR_ERR(ptr: chost->dat); |
232 | chost->sel = devm_gpiod_get_index(dev: &pdev->dev, NULL, idx: 2, flags: GPIOD_OUT_HIGH); |
233 | if (IS_ERR(ptr: chost->sel)) |
234 | return PTR_ERR(ptr: chost->sel); |
235 | gpiod_set_consumer_name(desc: chost->clk, name: "CBUS clk" ); |
236 | gpiod_set_consumer_name(desc: chost->dat, name: "CBUS dat" ); |
237 | gpiod_set_consumer_name(desc: chost->sel, name: "CBUS sel" ); |
238 | |
239 | adapter->owner = THIS_MODULE; |
240 | adapter->class = I2C_CLASS_HWMON; |
241 | adapter->dev.parent = &pdev->dev; |
242 | adapter->dev.of_node = pdev->dev.of_node; |
243 | adapter->nr = pdev->id; |
244 | adapter->timeout = HZ; |
245 | adapter->algo = &cbus_i2c_algo; |
246 | strscpy(p: adapter->name, q: "CBUS I2C adapter" , size: sizeof(adapter->name)); |
247 | |
248 | spin_lock_init(&chost->lock); |
249 | chost->dev = &pdev->dev; |
250 | |
251 | i2c_set_adapdata(adap: adapter, data: chost); |
252 | platform_set_drvdata(pdev, data: adapter); |
253 | |
254 | return i2c_add_numbered_adapter(adap: adapter); |
255 | } |
256 | |
257 | #if defined(CONFIG_OF) |
258 | static const struct of_device_id i2c_cbus_dt_ids[] = { |
259 | { .compatible = "i2c-cbus-gpio" , }, |
260 | { } |
261 | }; |
262 | MODULE_DEVICE_TABLE(of, i2c_cbus_dt_ids); |
263 | #endif |
264 | |
265 | static struct platform_driver cbus_i2c_driver = { |
266 | .probe = cbus_i2c_probe, |
267 | .remove_new = cbus_i2c_remove, |
268 | .driver = { |
269 | .name = "i2c-cbus-gpio" , |
270 | .of_match_table = of_match_ptr(i2c_cbus_dt_ids), |
271 | }, |
272 | }; |
273 | module_platform_driver(cbus_i2c_driver); |
274 | |
275 | MODULE_ALIAS("platform:i2c-cbus-gpio" ); |
276 | MODULE_DESCRIPTION("CBUS I2C driver" ); |
277 | MODULE_AUTHOR("Juha Yrjölä" ); |
278 | MODULE_AUTHOR("David Weinehall" ); |
279 | MODULE_AUTHOR("Mikko Ylinen" ); |
280 | MODULE_AUTHOR("Felipe Balbi" ); |
281 | MODULE_AUTHOR("Aaro Koskinen <aaro.koskinen@iki.fi>" ); |
282 | MODULE_LICENSE("GPL" ); |
283 | |