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.
20 #include <linux/i2c.h>
21 #include <linux/gpio.h>
22 #include <linux/slab.h>
23 #include <linux/delay.h>
24 #include <linux/errno.h>
25 #include <linux/kernel.h>
26 #include <linux/module.h>
27 #include <linux/of_gpio.h>
28 #include <linux/interrupt.h>
29 #include <linux/platform_device.h>
30 #include <linux/platform_data/i2c-cbus-gpio.h>
33 * Bit counts are derived from Nokia implementation. These should be checked
34 * if other CBUS implementations appear.
36 #define CBUS_ADDR_BITS 3
37 #define CBUS_REG_BITS 5
40 spinlock_t lock
; /* host lock */
48 * cbus_send_bit - sends one bit over the bus
49 * @host: the host we're using
50 * @bit: one bit of information to send
52 static void cbus_send_bit(struct cbus_host
*host
, unsigned bit
)
54 gpio_set_value(host
->dat_gpio
, bit
? 1 : 0);
55 gpio_set_value(host
->clk_gpio
, 1);
56 gpio_set_value(host
->clk_gpio
, 0);
60 * cbus_send_data - sends @len amount of data over the bus
61 * @host: the host we're using
62 * @data: the data to send
63 * @len: size of the transfer
65 static void cbus_send_data(struct cbus_host
*host
, unsigned data
, unsigned len
)
69 for (i
= len
; i
> 0; i
--)
70 cbus_send_bit(host
, data
& (1 << (i
- 1)));
74 * cbus_receive_bit - receives one bit from the bus
75 * @host: the host we're using
77 static int cbus_receive_bit(struct cbus_host
*host
)
81 gpio_set_value(host
->clk_gpio
, 1);
82 ret
= gpio_get_value(host
->dat_gpio
);
83 gpio_set_value(host
->clk_gpio
, 0);
88 * cbus_receive_word - receives 16-bit word from the bus
89 * @host: the host we're using
91 static int cbus_receive_word(struct cbus_host
*host
)
96 for (i
= 16; i
> 0; i
--) {
97 int bit
= cbus_receive_bit(host
);
109 * cbus_transfer - transfers data over the bus
110 * @host: the host we're using
111 * @rw: read/write flag
112 * @dev: device address
113 * @reg: register address
114 * @data: if @rw == I2C_SBUS_WRITE data to send otherwise 0
116 static int cbus_transfer(struct cbus_host
*host
, char rw
, unsigned dev
,
117 unsigned reg
, unsigned data
)
122 /* We don't want interrupts disturbing our transfer */
123 spin_lock_irqsave(&host
->lock
, flags
);
125 /* Reset state and start of transfer, SEL stays down during transfer */
126 gpio_set_value(host
->sel_gpio
, 0);
128 /* Set the DAT pin to output */
129 gpio_direction_output(host
->dat_gpio
, 1);
131 /* Send the device address */
132 cbus_send_data(host
, dev
, CBUS_ADDR_BITS
);
134 /* Send the rw flag */
135 cbus_send_bit(host
, rw
== I2C_SMBUS_READ
);
137 /* Send the register address */
138 cbus_send_data(host
, reg
, CBUS_REG_BITS
);
140 if (rw
== I2C_SMBUS_WRITE
) {
141 cbus_send_data(host
, data
, 16);
144 ret
= gpio_direction_input(host
->dat_gpio
);
146 dev_dbg(host
->dev
, "failed setting direction\n");
149 gpio_set_value(host
->clk_gpio
, 1);
151 ret
= cbus_receive_word(host
);
153 dev_dbg(host
->dev
, "failed receiving data\n");
158 /* Indicate end of transfer, SEL goes up until next transfer */
159 gpio_set_value(host
->sel_gpio
, 1);
160 gpio_set_value(host
->clk_gpio
, 1);
161 gpio_set_value(host
->clk_gpio
, 0);
164 spin_unlock_irqrestore(&host
->lock
, flags
);
169 static int cbus_i2c_smbus_xfer(struct i2c_adapter
*adapter
,
171 unsigned short flags
,
175 union i2c_smbus_data
*data
)
177 struct cbus_host
*chost
= i2c_get_adapdata(adapter
);
180 if (size
!= I2C_SMBUS_WORD_DATA
)
183 ret
= cbus_transfer(chost
, read_write
== I2C_SMBUS_READ
, addr
,
184 command
, data
->word
);
188 if (read_write
== I2C_SMBUS_READ
)
194 static u32
cbus_i2c_func(struct i2c_adapter
*adapter
)
196 return I2C_FUNC_SMBUS_READ_WORD_DATA
| I2C_FUNC_SMBUS_WRITE_WORD_DATA
;
199 static const struct i2c_algorithm cbus_i2c_algo
= {
200 .smbus_xfer
= cbus_i2c_smbus_xfer
,
201 .functionality
= cbus_i2c_func
,
204 static int cbus_i2c_remove(struct platform_device
*pdev
)
206 struct i2c_adapter
*adapter
= platform_get_drvdata(pdev
);
208 i2c_del_adapter(adapter
);
213 static int cbus_i2c_probe(struct platform_device
*pdev
)
215 struct i2c_adapter
*adapter
;
216 struct cbus_host
*chost
;
219 adapter
= devm_kzalloc(&pdev
->dev
, sizeof(struct i2c_adapter
),
224 chost
= devm_kzalloc(&pdev
->dev
, sizeof(*chost
), GFP_KERNEL
);
228 if (pdev
->dev
.of_node
) {
229 struct device_node
*dnode
= pdev
->dev
.of_node
;
230 if (of_gpio_count(dnode
) != 3)
232 chost
->clk_gpio
= of_get_gpio(dnode
, 0);
233 chost
->dat_gpio
= of_get_gpio(dnode
, 1);
234 chost
->sel_gpio
= of_get_gpio(dnode
, 2);
235 } else if (dev_get_platdata(&pdev
->dev
)) {
236 struct i2c_cbus_platform_data
*pdata
=
237 dev_get_platdata(&pdev
->dev
);
238 chost
->clk_gpio
= pdata
->clk_gpio
;
239 chost
->dat_gpio
= pdata
->dat_gpio
;
240 chost
->sel_gpio
= pdata
->sel_gpio
;
245 adapter
->owner
= THIS_MODULE
;
246 adapter
->class = I2C_CLASS_HWMON
;
247 adapter
->dev
.parent
= &pdev
->dev
;
248 adapter
->dev
.of_node
= pdev
->dev
.of_node
;
249 adapter
->nr
= pdev
->id
;
250 adapter
->timeout
= HZ
;
251 adapter
->algo
= &cbus_i2c_algo
;
252 strlcpy(adapter
->name
, "CBUS I2C adapter", sizeof(adapter
->name
));
254 spin_lock_init(&chost
->lock
);
255 chost
->dev
= &pdev
->dev
;
257 ret
= devm_gpio_request_one(&pdev
->dev
, chost
->clk_gpio
,
258 GPIOF_OUT_INIT_LOW
, "CBUS clk");
262 ret
= devm_gpio_request_one(&pdev
->dev
, chost
->dat_gpio
, GPIOF_IN
,
267 ret
= devm_gpio_request_one(&pdev
->dev
, chost
->sel_gpio
,
268 GPIOF_OUT_INIT_HIGH
, "CBUS sel");
272 i2c_set_adapdata(adapter
, chost
);
273 platform_set_drvdata(pdev
, adapter
);
275 return i2c_add_numbered_adapter(adapter
);
278 #if defined(CONFIG_OF)
279 static const struct of_device_id i2c_cbus_dt_ids
[] = {
280 { .compatible
= "i2c-cbus-gpio", },
283 MODULE_DEVICE_TABLE(of
, i2c_cbus_dt_ids
);
286 static struct platform_driver cbus_i2c_driver
= {
287 .probe
= cbus_i2c_probe
,
288 .remove
= cbus_i2c_remove
,
290 .owner
= THIS_MODULE
,
291 .name
= "i2c-cbus-gpio",
292 .of_match_table
= of_match_ptr(i2c_cbus_dt_ids
),
295 module_platform_driver(cbus_i2c_driver
);
297 MODULE_ALIAS("platform:i2c-cbus-gpio");
298 MODULE_DESCRIPTION("CBUS I2C driver");
299 MODULE_AUTHOR("Juha Yrjölä");
300 MODULE_AUTHOR("David Weinehall");
301 MODULE_AUTHOR("Mikko Ylinen");
302 MODULE_AUTHOR("Felipe Balbi");
303 MODULE_AUTHOR("Aaro Koskinen <aaro.koskinen@iki.fi>");
304 MODULE_LICENSE("GPL");