parisc iommu: fix panic due to trying to allocate too large region
[linux/fpc-iii.git] / drivers / i2c / busses / i2c-cbus-gpio.c
blob1be13ac11dc5d615050deb2e03c58d530a99fb30
1 /*
2 * CBUS I2C driver for Nokia Internet Tablets.
4 * Copyright (C) 2004-2010 Nokia Corporation
6 * Based on code written by Juha Yrjölä, David Weinehall, Mikko Ylinen and
7 * Felipe Balbi. Converted to I2C driver by Aaro Koskinen.
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.
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.
19 #include <linux/io.h>
20 #include <linux/i2c.h>
21 #include <linux/gpio.h>
22 #include <linux/init.h>
23 #include <linux/slab.h>
24 #include <linux/delay.h>
25 #include <linux/errno.h>
26 #include <linux/kernel.h>
27 #include <linux/module.h>
28 #include <linux/of_gpio.h>
29 #include <linux/interrupt.h>
30 #include <linux/platform_device.h>
31 #include <linux/platform_data/i2c-cbus-gpio.h>
34 * Bit counts are derived from Nokia implementation. These should be checked
35 * if other CBUS implementations appear.
37 #define CBUS_ADDR_BITS 3
38 #define CBUS_REG_BITS 5
40 struct cbus_host {
41 spinlock_t lock; /* host lock */
42 struct device *dev;
43 int clk_gpio;
44 int dat_gpio;
45 int sel_gpio;
48 /**
49 * cbus_send_bit - sends one bit over the bus
50 * @host: the host we're using
51 * @bit: one bit of information to send
53 static void cbus_send_bit(struct cbus_host *host, unsigned bit)
55 gpio_set_value(host->dat_gpio, bit ? 1 : 0);
56 gpio_set_value(host->clk_gpio, 1);
57 gpio_set_value(host->clk_gpio, 0);
60 /**
61 * cbus_send_data - sends @len amount of data over the bus
62 * @host: the host we're using
63 * @data: the data to send
64 * @len: size of the transfer
66 static void cbus_send_data(struct cbus_host *host, unsigned data, unsigned len)
68 int i;
70 for (i = len; i > 0; i--)
71 cbus_send_bit(host, data & (1 << (i - 1)));
74 /**
75 * cbus_receive_bit - receives one bit from the bus
76 * @host: the host we're using
78 static int cbus_receive_bit(struct cbus_host *host)
80 int ret;
82 gpio_set_value(host->clk_gpio, 1);
83 ret = gpio_get_value(host->dat_gpio);
84 gpio_set_value(host->clk_gpio, 0);
85 return ret;
88 /**
89 * cbus_receive_word - receives 16-bit word from the bus
90 * @host: the host we're using
92 static int cbus_receive_word(struct cbus_host *host)
94 int ret = 0;
95 int i;
97 for (i = 16; i > 0; i--) {
98 int bit = cbus_receive_bit(host);
100 if (bit < 0)
101 return bit;
103 if (bit)
104 ret |= 1 << (i - 1);
106 return ret;
110 * cbus_transfer - transfers data over the bus
111 * @host: the host we're using
112 * @rw: read/write flag
113 * @dev: device address
114 * @reg: register address
115 * @data: if @rw == I2C_SBUS_WRITE data to send otherwise 0
117 static int cbus_transfer(struct cbus_host *host, char rw, unsigned dev,
118 unsigned reg, unsigned data)
120 unsigned long flags;
121 int ret;
123 /* We don't want interrupts disturbing our transfer */
124 spin_lock_irqsave(&host->lock, flags);
126 /* Reset state and start of transfer, SEL stays down during transfer */
127 gpio_set_value(host->sel_gpio, 0);
129 /* Set the DAT pin to output */
130 gpio_direction_output(host->dat_gpio, 1);
132 /* Send the device address */
133 cbus_send_data(host, dev, CBUS_ADDR_BITS);
135 /* Send the rw flag */
136 cbus_send_bit(host, rw == I2C_SMBUS_READ);
138 /* Send the register address */
139 cbus_send_data(host, reg, CBUS_REG_BITS);
141 if (rw == I2C_SMBUS_WRITE) {
142 cbus_send_data(host, data, 16);
143 ret = 0;
144 } else {
145 ret = gpio_direction_input(host->dat_gpio);
146 if (ret) {
147 dev_dbg(host->dev, "failed setting direction\n");
148 goto out;
150 gpio_set_value(host->clk_gpio, 1);
152 ret = cbus_receive_word(host);
153 if (ret < 0) {
154 dev_dbg(host->dev, "failed receiving data\n");
155 goto out;
159 /* Indicate end of transfer, SEL goes up until next transfer */
160 gpio_set_value(host->sel_gpio, 1);
161 gpio_set_value(host->clk_gpio, 1);
162 gpio_set_value(host->clk_gpio, 0);
164 out:
165 spin_unlock_irqrestore(&host->lock, flags);
167 return ret;
170 static int cbus_i2c_smbus_xfer(struct i2c_adapter *adapter,
171 u16 addr,
172 unsigned short flags,
173 char read_write,
174 u8 command,
175 int size,
176 union i2c_smbus_data *data)
178 struct cbus_host *chost = i2c_get_adapdata(adapter);
179 int ret;
181 if (size != I2C_SMBUS_WORD_DATA)
182 return -EINVAL;
184 ret = cbus_transfer(chost, read_write == I2C_SMBUS_READ, addr,
185 command, data->word);
186 if (ret < 0)
187 return ret;
189 if (read_write == I2C_SMBUS_READ)
190 data->word = ret;
192 return 0;
195 static u32 cbus_i2c_func(struct i2c_adapter *adapter)
197 return I2C_FUNC_SMBUS_READ_WORD_DATA | I2C_FUNC_SMBUS_WRITE_WORD_DATA;
200 static const struct i2c_algorithm cbus_i2c_algo = {
201 .smbus_xfer = cbus_i2c_smbus_xfer,
202 .functionality = cbus_i2c_func,
205 static int cbus_i2c_remove(struct platform_device *pdev)
207 struct i2c_adapter *adapter = platform_get_drvdata(pdev);
209 i2c_del_adapter(adapter);
211 return 0;
214 static int cbus_i2c_probe(struct platform_device *pdev)
216 struct i2c_adapter *adapter;
217 struct cbus_host *chost;
218 int ret;
220 adapter = devm_kzalloc(&pdev->dev, sizeof(struct i2c_adapter),
221 GFP_KERNEL);
222 if (!adapter)
223 return -ENOMEM;
225 chost = devm_kzalloc(&pdev->dev, sizeof(*chost), GFP_KERNEL);
226 if (!chost)
227 return -ENOMEM;
229 if (pdev->dev.of_node) {
230 struct device_node *dnode = pdev->dev.of_node;
231 if (of_gpio_count(dnode) != 3)
232 return -ENODEV;
233 chost->clk_gpio = of_get_gpio(dnode, 0);
234 chost->dat_gpio = of_get_gpio(dnode, 1);
235 chost->sel_gpio = of_get_gpio(dnode, 2);
236 } else if (pdev->dev.platform_data) {
237 struct i2c_cbus_platform_data *pdata = pdev->dev.platform_data;
238 chost->clk_gpio = pdata->clk_gpio;
239 chost->dat_gpio = pdata->dat_gpio;
240 chost->sel_gpio = pdata->sel_gpio;
241 } else {
242 return -ENODEV;
245 adapter->owner = THIS_MODULE;
246 adapter->class = I2C_CLASS_HWMON;
247 adapter->dev.parent = &pdev->dev;
248 adapter->nr = pdev->id;
249 adapter->timeout = HZ;
250 adapter->algo = &cbus_i2c_algo;
251 strlcpy(adapter->name, "CBUS I2C adapter", sizeof(adapter->name));
253 spin_lock_init(&chost->lock);
254 chost->dev = &pdev->dev;
256 ret = devm_gpio_request_one(&pdev->dev, chost->clk_gpio,
257 GPIOF_OUT_INIT_LOW, "CBUS clk");
258 if (ret)
259 return ret;
261 ret = devm_gpio_request_one(&pdev->dev, chost->dat_gpio, GPIOF_IN,
262 "CBUS data");
263 if (ret)
264 return ret;
266 ret = devm_gpio_request_one(&pdev->dev, chost->sel_gpio,
267 GPIOF_OUT_INIT_HIGH, "CBUS sel");
268 if (ret)
269 return ret;
271 i2c_set_adapdata(adapter, chost);
272 platform_set_drvdata(pdev, adapter);
274 return i2c_add_numbered_adapter(adapter);
277 #if defined(CONFIG_OF)
278 static const struct of_device_id i2c_cbus_dt_ids[] = {
279 { .compatible = "i2c-cbus-gpio", },
282 MODULE_DEVICE_TABLE(of, i2c_cbus_dt_ids);
283 #endif
285 static struct platform_driver cbus_i2c_driver = {
286 .probe = cbus_i2c_probe,
287 .remove = cbus_i2c_remove,
288 .driver = {
289 .owner = THIS_MODULE,
290 .name = "i2c-cbus-gpio",
293 module_platform_driver(cbus_i2c_driver);
295 MODULE_ALIAS("platform:i2c-cbus-gpio");
296 MODULE_DESCRIPTION("CBUS I2C driver");
297 MODULE_AUTHOR("Juha Yrjölä");
298 MODULE_AUTHOR("David Weinehall");
299 MODULE_AUTHOR("Mikko Ylinen");
300 MODULE_AUTHOR("Felipe Balbi");
301 MODULE_AUTHOR("Aaro Koskinen <aaro.koskinen@iki.fi>");
302 MODULE_LICENSE("GPL");