10857ba3cSAaro Koskinen /*
20857ba3cSAaro Koskinen * CBUS I2C driver for Nokia Internet Tablets.
30857ba3cSAaro Koskinen *
40857ba3cSAaro Koskinen * Copyright (C) 2004-2010 Nokia Corporation
50857ba3cSAaro Koskinen *
60857ba3cSAaro Koskinen * Based on code written by Juha Yrjölä, David Weinehall, Mikko Ylinen and
70857ba3cSAaro Koskinen * Felipe Balbi. Converted to I2C driver by Aaro Koskinen.
80857ba3cSAaro Koskinen *
90857ba3cSAaro Koskinen * This file is subject to the terms and conditions of the GNU General
100857ba3cSAaro Koskinen * Public License. See the file "COPYING" in the main directory of this
110857ba3cSAaro Koskinen * archive for more details.
120857ba3cSAaro Koskinen *
130857ba3cSAaro Koskinen * This program is distributed in the hope that it will be useful,
140857ba3cSAaro Koskinen * but WITHOUT ANY WARRANTY; without even the implied warranty of
150857ba3cSAaro Koskinen * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
160857ba3cSAaro Koskinen * GNU General Public License for more details.
170857ba3cSAaro Koskinen */
180857ba3cSAaro Koskinen
190857ba3cSAaro Koskinen #include <linux/io.h>
200857ba3cSAaro Koskinen #include <linux/i2c.h>
210857ba3cSAaro Koskinen #include <linux/slab.h>
220857ba3cSAaro Koskinen #include <linux/delay.h>
230857ba3cSAaro Koskinen #include <linux/errno.h>
240857ba3cSAaro Koskinen #include <linux/kernel.h>
250857ba3cSAaro Koskinen #include <linux/module.h>
262e5a662dSLinus Walleij #include <linux/gpio/consumer.h>
270857ba3cSAaro Koskinen #include <linux/interrupt.h>
280857ba3cSAaro Koskinen #include <linux/platform_device.h>
290857ba3cSAaro Koskinen
300857ba3cSAaro Koskinen /*
310857ba3cSAaro Koskinen * Bit counts are derived from Nokia implementation. These should be checked
320857ba3cSAaro Koskinen * if other CBUS implementations appear.
330857ba3cSAaro Koskinen */
340857ba3cSAaro Koskinen #define CBUS_ADDR_BITS 3
350857ba3cSAaro Koskinen #define CBUS_REG_BITS 5
360857ba3cSAaro Koskinen
370857ba3cSAaro Koskinen struct cbus_host {
380857ba3cSAaro Koskinen spinlock_t lock; /* host lock */
390857ba3cSAaro Koskinen struct device *dev;
402e5a662dSLinus Walleij struct gpio_desc *clk;
412e5a662dSLinus Walleij struct gpio_desc *dat;
422e5a662dSLinus Walleij struct gpio_desc *sel;
430857ba3cSAaro Koskinen };
440857ba3cSAaro Koskinen
450857ba3cSAaro Koskinen /**
460857ba3cSAaro Koskinen * cbus_send_bit - sends one bit over the bus
470857ba3cSAaro Koskinen * @host: the host we're using
480857ba3cSAaro Koskinen * @bit: one bit of information to send
490857ba3cSAaro Koskinen */
cbus_send_bit(struct cbus_host * host,unsigned bit)500857ba3cSAaro Koskinen static void cbus_send_bit(struct cbus_host *host, unsigned bit)
510857ba3cSAaro Koskinen {
522e5a662dSLinus Walleij gpiod_set_value(host->dat, bit ? 1 : 0);
532e5a662dSLinus Walleij gpiod_set_value(host->clk, 1);
542e5a662dSLinus Walleij gpiod_set_value(host->clk, 0);
550857ba3cSAaro Koskinen }
560857ba3cSAaro Koskinen
570857ba3cSAaro Koskinen /**
580857ba3cSAaro Koskinen * cbus_send_data - sends @len amount of data over the bus
590857ba3cSAaro Koskinen * @host: the host we're using
600857ba3cSAaro Koskinen * @data: the data to send
610857ba3cSAaro Koskinen * @len: size of the transfer
620857ba3cSAaro Koskinen */
cbus_send_data(struct cbus_host * host,unsigned data,unsigned len)630857ba3cSAaro Koskinen static void cbus_send_data(struct cbus_host *host, unsigned data, unsigned len)
640857ba3cSAaro Koskinen {
650857ba3cSAaro Koskinen int i;
660857ba3cSAaro Koskinen
670857ba3cSAaro Koskinen for (i = len; i > 0; i--)
680857ba3cSAaro Koskinen cbus_send_bit(host, data & (1 << (i - 1)));
690857ba3cSAaro Koskinen }
700857ba3cSAaro Koskinen
710857ba3cSAaro Koskinen /**
720857ba3cSAaro Koskinen * cbus_receive_bit - receives one bit from the bus
730857ba3cSAaro Koskinen * @host: the host we're using
740857ba3cSAaro Koskinen */
cbus_receive_bit(struct cbus_host * host)750857ba3cSAaro Koskinen static int cbus_receive_bit(struct cbus_host *host)
760857ba3cSAaro Koskinen {
770857ba3cSAaro Koskinen int ret;
780857ba3cSAaro Koskinen
792e5a662dSLinus Walleij gpiod_set_value(host->clk, 1);
802e5a662dSLinus Walleij ret = gpiod_get_value(host->dat);
812e5a662dSLinus Walleij gpiod_set_value(host->clk, 0);
820857ba3cSAaro Koskinen return ret;
830857ba3cSAaro Koskinen }
840857ba3cSAaro Koskinen
850857ba3cSAaro Koskinen /**
860857ba3cSAaro Koskinen * cbus_receive_word - receives 16-bit word from the bus
870857ba3cSAaro Koskinen * @host: the host we're using
880857ba3cSAaro Koskinen */
cbus_receive_word(struct cbus_host * host)890857ba3cSAaro Koskinen static int cbus_receive_word(struct cbus_host *host)
900857ba3cSAaro Koskinen {
910857ba3cSAaro Koskinen int ret = 0;
920857ba3cSAaro Koskinen int i;
930857ba3cSAaro Koskinen
940857ba3cSAaro Koskinen for (i = 16; i > 0; i--) {
950857ba3cSAaro Koskinen int bit = cbus_receive_bit(host);
960857ba3cSAaro Koskinen
970857ba3cSAaro Koskinen if (bit < 0)
980857ba3cSAaro Koskinen return bit;
990857ba3cSAaro Koskinen
1000857ba3cSAaro Koskinen if (bit)
1010857ba3cSAaro Koskinen ret |= 1 << (i - 1);
1020857ba3cSAaro Koskinen }
1030857ba3cSAaro Koskinen return ret;
1040857ba3cSAaro Koskinen }
1050857ba3cSAaro Koskinen
1060857ba3cSAaro Koskinen /**
1070857ba3cSAaro Koskinen * cbus_transfer - transfers data over the bus
1080857ba3cSAaro Koskinen * @host: the host we're using
1090857ba3cSAaro Koskinen * @rw: read/write flag
1100857ba3cSAaro Koskinen * @dev: device address
1110857ba3cSAaro Koskinen * @reg: register address
1120857ba3cSAaro Koskinen * @data: if @rw == I2C_SBUS_WRITE data to send otherwise 0
1130857ba3cSAaro Koskinen */
cbus_transfer(struct cbus_host * host,char rw,unsigned dev,unsigned reg,unsigned data)1140857ba3cSAaro Koskinen static int cbus_transfer(struct cbus_host *host, char rw, unsigned dev,
1150857ba3cSAaro Koskinen unsigned reg, unsigned data)
1160857ba3cSAaro Koskinen {
1170857ba3cSAaro Koskinen unsigned long flags;
1180857ba3cSAaro Koskinen int ret;
1190857ba3cSAaro Koskinen
1200857ba3cSAaro Koskinen /* We don't want interrupts disturbing our transfer */
1210857ba3cSAaro Koskinen spin_lock_irqsave(&host->lock, flags);
1220857ba3cSAaro Koskinen
1230857ba3cSAaro Koskinen /* Reset state and start of transfer, SEL stays down during transfer */
1242e5a662dSLinus Walleij gpiod_set_value(host->sel, 0);
1250857ba3cSAaro Koskinen
1260857ba3cSAaro Koskinen /* Set the DAT pin to output */
1272e5a662dSLinus Walleij gpiod_direction_output(host->dat, 1);
1280857ba3cSAaro Koskinen
1290857ba3cSAaro Koskinen /* Send the device address */
1300857ba3cSAaro Koskinen cbus_send_data(host, dev, CBUS_ADDR_BITS);
1310857ba3cSAaro Koskinen
1320857ba3cSAaro Koskinen /* Send the rw flag */
1330857ba3cSAaro Koskinen cbus_send_bit(host, rw == I2C_SMBUS_READ);
1340857ba3cSAaro Koskinen
1350857ba3cSAaro Koskinen /* Send the register address */
1360857ba3cSAaro Koskinen cbus_send_data(host, reg, CBUS_REG_BITS);
1370857ba3cSAaro Koskinen
1380857ba3cSAaro Koskinen if (rw == I2C_SMBUS_WRITE) {
1390857ba3cSAaro Koskinen cbus_send_data(host, data, 16);
1400857ba3cSAaro Koskinen ret = 0;
1410857ba3cSAaro Koskinen } else {
1422e5a662dSLinus Walleij ret = gpiod_direction_input(host->dat);
1430857ba3cSAaro Koskinen if (ret) {
1440857ba3cSAaro Koskinen dev_dbg(host->dev, "failed setting direction\n");
1450857ba3cSAaro Koskinen goto out;
1460857ba3cSAaro Koskinen }
1472e5a662dSLinus Walleij gpiod_set_value(host->clk, 1);
1480857ba3cSAaro Koskinen
1490857ba3cSAaro Koskinen ret = cbus_receive_word(host);
1500857ba3cSAaro Koskinen if (ret < 0) {
1510857ba3cSAaro Koskinen dev_dbg(host->dev, "failed receiving data\n");
1520857ba3cSAaro Koskinen goto out;
1530857ba3cSAaro Koskinen }
1540857ba3cSAaro Koskinen }
1550857ba3cSAaro Koskinen
1560857ba3cSAaro Koskinen /* Indicate end of transfer, SEL goes up until next transfer */
1572e5a662dSLinus Walleij gpiod_set_value(host->sel, 1);
1582e5a662dSLinus Walleij gpiod_set_value(host->clk, 1);
1592e5a662dSLinus Walleij gpiod_set_value(host->clk, 0);
1600857ba3cSAaro Koskinen
1610857ba3cSAaro Koskinen out:
1620857ba3cSAaro Koskinen spin_unlock_irqrestore(&host->lock, flags);
1630857ba3cSAaro Koskinen
1640857ba3cSAaro Koskinen return ret;
1650857ba3cSAaro Koskinen }
1660857ba3cSAaro Koskinen
cbus_i2c_smbus_xfer(struct i2c_adapter * adapter,u16 addr,unsigned short flags,char read_write,u8 command,int size,union i2c_smbus_data * data)1670857ba3cSAaro Koskinen static int cbus_i2c_smbus_xfer(struct i2c_adapter *adapter,
1680857ba3cSAaro Koskinen u16 addr,
1690857ba3cSAaro Koskinen unsigned short flags,
1700857ba3cSAaro Koskinen char read_write,
1710857ba3cSAaro Koskinen u8 command,
1720857ba3cSAaro Koskinen int size,
1730857ba3cSAaro Koskinen union i2c_smbus_data *data)
1740857ba3cSAaro Koskinen {
1750857ba3cSAaro Koskinen struct cbus_host *chost = i2c_get_adapdata(adapter);
1760857ba3cSAaro Koskinen int ret;
1770857ba3cSAaro Koskinen
1780857ba3cSAaro Koskinen if (size != I2C_SMBUS_WORD_DATA)
1790857ba3cSAaro Koskinen return -EINVAL;
1800857ba3cSAaro Koskinen
1810857ba3cSAaro Koskinen ret = cbus_transfer(chost, read_write == I2C_SMBUS_READ, addr,
1820857ba3cSAaro Koskinen command, data->word);
1830857ba3cSAaro Koskinen if (ret < 0)
1840857ba3cSAaro Koskinen return ret;
1850857ba3cSAaro Koskinen
1860857ba3cSAaro Koskinen if (read_write == I2C_SMBUS_READ)
1870857ba3cSAaro Koskinen data->word = ret;
1880857ba3cSAaro Koskinen
1890857ba3cSAaro Koskinen return 0;
1900857ba3cSAaro Koskinen }
1910857ba3cSAaro Koskinen
cbus_i2c_func(struct i2c_adapter * adapter)1920857ba3cSAaro Koskinen static u32 cbus_i2c_func(struct i2c_adapter *adapter)
1930857ba3cSAaro Koskinen {
1940857ba3cSAaro Koskinen return I2C_FUNC_SMBUS_READ_WORD_DATA | I2C_FUNC_SMBUS_WRITE_WORD_DATA;
1950857ba3cSAaro Koskinen }
1960857ba3cSAaro Koskinen
1970857ba3cSAaro Koskinen static const struct i2c_algorithm cbus_i2c_algo = {
1980857ba3cSAaro Koskinen .smbus_xfer = cbus_i2c_smbus_xfer,
199b1276469SAaro Koskinen .smbus_xfer_atomic = cbus_i2c_smbus_xfer,
2000857ba3cSAaro Koskinen .functionality = cbus_i2c_func,
2010857ba3cSAaro Koskinen };
2020857ba3cSAaro Koskinen
cbus_i2c_remove(struct platform_device * pdev)203*e190a0c3SUwe Kleine-König static void cbus_i2c_remove(struct platform_device *pdev)
2040857ba3cSAaro Koskinen {
2050857ba3cSAaro Koskinen struct i2c_adapter *adapter = platform_get_drvdata(pdev);
2060857ba3cSAaro Koskinen
207bf51a8c5SLars-Peter Clausen i2c_del_adapter(adapter);
2080857ba3cSAaro Koskinen }
2090857ba3cSAaro Koskinen
cbus_i2c_probe(struct platform_device * pdev)2100857ba3cSAaro Koskinen static int cbus_i2c_probe(struct platform_device *pdev)
2110857ba3cSAaro Koskinen {
2120857ba3cSAaro Koskinen struct i2c_adapter *adapter;
2130857ba3cSAaro Koskinen struct cbus_host *chost;
2140857ba3cSAaro Koskinen
2150857ba3cSAaro Koskinen adapter = devm_kzalloc(&pdev->dev, sizeof(struct i2c_adapter),
2160857ba3cSAaro Koskinen GFP_KERNEL);
2170857ba3cSAaro Koskinen if (!adapter)
2180857ba3cSAaro Koskinen return -ENOMEM;
2190857ba3cSAaro Koskinen
2200857ba3cSAaro Koskinen chost = devm_kzalloc(&pdev->dev, sizeof(*chost), GFP_KERNEL);
2210857ba3cSAaro Koskinen if (!chost)
2220857ba3cSAaro Koskinen return -ENOMEM;
2230857ba3cSAaro Koskinen
2242e5a662dSLinus Walleij if (gpiod_count(&pdev->dev, NULL) != 3)
2250857ba3cSAaro Koskinen return -ENODEV;
2262e5a662dSLinus Walleij chost->clk = devm_gpiod_get_index(&pdev->dev, NULL, 0, GPIOD_OUT_LOW);
2272e5a662dSLinus Walleij if (IS_ERR(chost->clk))
2282e5a662dSLinus Walleij return PTR_ERR(chost->clk);
2292e5a662dSLinus Walleij chost->dat = devm_gpiod_get_index(&pdev->dev, NULL, 1, GPIOD_IN);
2302e5a662dSLinus Walleij if (IS_ERR(chost->dat))
2312e5a662dSLinus Walleij return PTR_ERR(chost->dat);
2322e5a662dSLinus Walleij chost->sel = devm_gpiod_get_index(&pdev->dev, NULL, 2, GPIOD_OUT_HIGH);
2332e5a662dSLinus Walleij if (IS_ERR(chost->sel))
2342e5a662dSLinus Walleij return PTR_ERR(chost->sel);
2352e5a662dSLinus Walleij gpiod_set_consumer_name(chost->clk, "CBUS clk");
2362e5a662dSLinus Walleij gpiod_set_consumer_name(chost->dat, "CBUS dat");
2372e5a662dSLinus Walleij gpiod_set_consumer_name(chost->sel, "CBUS sel");
2380857ba3cSAaro Koskinen
2390857ba3cSAaro Koskinen adapter->owner = THIS_MODULE;
2400857ba3cSAaro Koskinen adapter->class = I2C_CLASS_HWMON;
2410857ba3cSAaro Koskinen adapter->dev.parent = &pdev->dev;
242de546c8aSTony Lindgren adapter->dev.of_node = pdev->dev.of_node;
2430857ba3cSAaro Koskinen adapter->nr = pdev->id;
2440857ba3cSAaro Koskinen adapter->timeout = HZ;
2450857ba3cSAaro Koskinen adapter->algo = &cbus_i2c_algo;
246ea1558ceSWolfram Sang strscpy(adapter->name, "CBUS I2C adapter", sizeof(adapter->name));
2470857ba3cSAaro Koskinen
2480857ba3cSAaro Koskinen spin_lock_init(&chost->lock);
2490857ba3cSAaro Koskinen chost->dev = &pdev->dev;
2500857ba3cSAaro Koskinen
2510857ba3cSAaro Koskinen i2c_set_adapdata(adapter, chost);
2520857ba3cSAaro Koskinen platform_set_drvdata(pdev, adapter);
2530857ba3cSAaro Koskinen
2540857ba3cSAaro Koskinen return i2c_add_numbered_adapter(adapter);
2550857ba3cSAaro Koskinen }
2560857ba3cSAaro Koskinen
2570857ba3cSAaro Koskinen #if defined(CONFIG_OF)
2580857ba3cSAaro Koskinen static const struct of_device_id i2c_cbus_dt_ids[] = {
2590857ba3cSAaro Koskinen { .compatible = "i2c-cbus-gpio", },
2600857ba3cSAaro Koskinen { }
2610857ba3cSAaro Koskinen };
2620857ba3cSAaro Koskinen MODULE_DEVICE_TABLE(of, i2c_cbus_dt_ids);
2630857ba3cSAaro Koskinen #endif
2640857ba3cSAaro Koskinen
2650857ba3cSAaro Koskinen static struct platform_driver cbus_i2c_driver = {
2660857ba3cSAaro Koskinen .probe = cbus_i2c_probe,
267*e190a0c3SUwe Kleine-König .remove_new = cbus_i2c_remove,
2680857ba3cSAaro Koskinen .driver = {
2690857ba3cSAaro Koskinen .name = "i2c-cbus-gpio",
270de546c8aSTony Lindgren .of_match_table = of_match_ptr(i2c_cbus_dt_ids),
2710857ba3cSAaro Koskinen },
2720857ba3cSAaro Koskinen };
2730857ba3cSAaro Koskinen module_platform_driver(cbus_i2c_driver);
2740857ba3cSAaro Koskinen
2750857ba3cSAaro Koskinen MODULE_ALIAS("platform:i2c-cbus-gpio");
2760857ba3cSAaro Koskinen MODULE_DESCRIPTION("CBUS I2C driver");
2770857ba3cSAaro Koskinen MODULE_AUTHOR("Juha Yrjölä");
2780857ba3cSAaro Koskinen MODULE_AUTHOR("David Weinehall");
2790857ba3cSAaro Koskinen MODULE_AUTHOR("Mikko Ylinen");
2800857ba3cSAaro Koskinen MODULE_AUTHOR("Felipe Balbi");
2810857ba3cSAaro Koskinen MODULE_AUTHOR("Aaro Koskinen <aaro.koskinen@iki.fi>");
2820857ba3cSAaro Koskinen MODULE_LICENSE("GPL");
283